1 /* 2 * Copyright (C) 2017 Pengutronix, Juergen Borleis <kernel@pengutronix.de> 3 * 4 * Partially based on a patch from 5 * Copyright (c) 2014 Stefan Roese <sr@denx.de> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * version 2, as published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 */ 17 #include <linux/kernel.h> 18 #include <linux/module.h> 19 #include <linux/mdio.h> 20 #include <linux/phy.h> 21 #include <linux/of.h> 22 23 #include "lan9303.h" 24 25 /* Generate phy-addr and -reg from the input address */ 26 #define PHY_ADDR(x) ((((x) >> 6) + 0x10) & 0x1f) 27 #define PHY_REG(x) (((x) >> 1) & 0x1f) 28 29 struct lan9303_mdio { 30 struct mdio_device *device; 31 struct lan9303 chip; 32 }; 33 34 static void lan9303_mdio_real_write(struct mdio_device *mdio, int reg, u16 val) 35 { 36 mdio->bus->write(mdio->bus, PHY_ADDR(reg), PHY_REG(reg), val); 37 } 38 39 static int lan9303_mdio_write(void *ctx, uint32_t reg, uint32_t val) 40 { 41 struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx; 42 43 reg <<= 2; /* reg num to offset */ 44 mutex_lock(&sw_dev->device->bus->mdio_lock); 45 lan9303_mdio_real_write(sw_dev->device, reg, val & 0xffff); 46 lan9303_mdio_real_write(sw_dev->device, reg + 2, (val >> 16) & 0xffff); 47 mutex_unlock(&sw_dev->device->bus->mdio_lock); 48 49 return 0; 50 } 51 52 static u16 lan9303_mdio_real_read(struct mdio_device *mdio, int reg) 53 { 54 return mdio->bus->read(mdio->bus, PHY_ADDR(reg), PHY_REG(reg)); 55 } 56 57 static int lan9303_mdio_read(void *ctx, uint32_t reg, uint32_t *val) 58 { 59 struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx; 60 61 reg <<= 2; /* reg num to offset */ 62 mutex_lock(&sw_dev->device->bus->mdio_lock); 63 *val = lan9303_mdio_real_read(sw_dev->device, reg); 64 *val |= (lan9303_mdio_real_read(sw_dev->device, reg + 2) << 16); 65 mutex_unlock(&sw_dev->device->bus->mdio_lock); 66 67 return 0; 68 } 69 70 static int lan9303_mdio_phy_write(struct lan9303 *chip, int phy, int reg, 71 u16 val) 72 { 73 struct lan9303_mdio *sw_dev = dev_get_drvdata(chip->dev); 74 75 return mdiobus_write_nested(sw_dev->device->bus, phy, reg, val); 76 } 77 78 static int lan9303_mdio_phy_read(struct lan9303 *chip, int phy, int reg) 79 { 80 struct lan9303_mdio *sw_dev = dev_get_drvdata(chip->dev); 81 82 return mdiobus_read_nested(sw_dev->device->bus, phy, reg); 83 } 84 85 static const struct lan9303_phy_ops lan9303_mdio_phy_ops = { 86 .phy_read = lan9303_mdio_phy_read, 87 .phy_write = lan9303_mdio_phy_write, 88 }; 89 90 static const struct regmap_config lan9303_mdio_regmap_config = { 91 .reg_bits = 8, 92 .val_bits = 32, 93 .reg_stride = 1, 94 .can_multi_write = true, 95 .max_register = 0x0ff, /* address bits 0..1 are not used */ 96 .reg_format_endian = REGMAP_ENDIAN_LITTLE, 97 98 .volatile_table = &lan9303_register_set, 99 .wr_table = &lan9303_register_set, 100 .rd_table = &lan9303_register_set, 101 102 .reg_read = lan9303_mdio_read, 103 .reg_write = lan9303_mdio_write, 104 105 .cache_type = REGCACHE_NONE, 106 }; 107 108 static int lan9303_mdio_probe(struct mdio_device *mdiodev) 109 { 110 struct lan9303_mdio *sw_dev; 111 int ret; 112 113 sw_dev = devm_kzalloc(&mdiodev->dev, sizeof(struct lan9303_mdio), 114 GFP_KERNEL); 115 if (!sw_dev) 116 return -ENOMEM; 117 118 sw_dev->chip.regmap = devm_regmap_init(&mdiodev->dev, NULL, sw_dev, 119 &lan9303_mdio_regmap_config); 120 if (IS_ERR(sw_dev->chip.regmap)) { 121 ret = PTR_ERR(sw_dev->chip.regmap); 122 dev_err(&mdiodev->dev, "regmap init failed: %d\n", ret); 123 return ret; 124 } 125 126 /* link forward and backward */ 127 sw_dev->device = mdiodev; 128 dev_set_drvdata(&mdiodev->dev, sw_dev); 129 sw_dev->chip.dev = &mdiodev->dev; 130 131 sw_dev->chip.ops = &lan9303_mdio_phy_ops; 132 133 ret = lan9303_probe(&sw_dev->chip, mdiodev->dev.of_node); 134 if (ret != 0) 135 return ret; 136 137 dev_info(&mdiodev->dev, "LAN9303 MDIO driver loaded successfully\n"); 138 139 return 0; 140 } 141 142 static void lan9303_mdio_remove(struct mdio_device *mdiodev) 143 { 144 struct lan9303_mdio *sw_dev = dev_get_drvdata(&mdiodev->dev); 145 146 if (!sw_dev) 147 return; 148 149 lan9303_remove(&sw_dev->chip); 150 } 151 152 /*-------------------------------------------------------------------------*/ 153 154 static const struct of_device_id lan9303_mdio_of_match[] = { 155 { .compatible = "smsc,lan9303-mdio" }, 156 { /* sentinel */ }, 157 }; 158 MODULE_DEVICE_TABLE(of, lan9303_mdio_of_match); 159 160 static struct mdio_driver lan9303_mdio_driver = { 161 .mdiodrv.driver = { 162 .name = "LAN9303_MDIO", 163 .of_match_table = of_match_ptr(lan9303_mdio_of_match), 164 }, 165 .probe = lan9303_mdio_probe, 166 .remove = lan9303_mdio_remove, 167 }; 168 mdio_module_driver(lan9303_mdio_driver); 169 170 MODULE_AUTHOR("Stefan Roese <sr@denx.de>, Juergen Borleis <kernel@pengutronix.de>"); 171 MODULE_DESCRIPTION("Driver for SMSC/Microchip LAN9303 three port ethernet switch in MDIO managed mode"); 172 MODULE_LICENSE("GPL v2"); 173