mdio_bus_provider.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* MDIO Bus provider interface
  3. *
  4. * Author: Andy Fleming
  5. *
  6. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/errno.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/ethtool.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/micrel_phy.h>
  19. #include <linux/mii.h>
  20. #include <linux/mm.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_mdio.h>
  24. #include <linux/phy.h>
  25. #include <linux/slab.h>
  26. #include <linux/string.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/unistd.h>
  29. /**
  30. * mdiobus_alloc_size - allocate a mii_bus structure
  31. * @size: extra amount of memory to allocate for private storage.
  32. * If non-zero, then bus->priv is points to that memory.
  33. *
  34. * Description: called by a bus driver to allocate an mii_bus
  35. * structure to fill in.
  36. */
  37. struct mii_bus *mdiobus_alloc_size(size_t size)
  38. {
  39. struct mii_bus *bus;
  40. size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
  41. size_t alloc_size;
  42. int i;
  43. /* If we alloc extra space, it should be aligned */
  44. if (size)
  45. alloc_size = aligned_size + size;
  46. else
  47. alloc_size = sizeof(*bus);
  48. bus = kzalloc(alloc_size, GFP_KERNEL);
  49. if (!bus)
  50. return NULL;
  51. bus->state = MDIOBUS_ALLOCATED;
  52. if (size)
  53. bus->priv = (void *)bus + aligned_size;
  54. /* Initialise the interrupts to polling and 64-bit seqcounts */
  55. for (i = 0; i < PHY_MAX_ADDR; i++) {
  56. bus->irq[i] = PHY_POLL;
  57. u64_stats_init(&bus->stats[i].syncp);
  58. }
  59. return bus;
  60. }
  61. EXPORT_SYMBOL(mdiobus_alloc_size);
  62. #if IS_ENABLED(CONFIG_OF_MDIO)
  63. /* Walk the list of subnodes of a mdio bus and look for a node that
  64. * matches the mdio device's address with its 'reg' property. If
  65. * found, set the of_node pointer for the mdio device. This allows
  66. * auto-probed phy devices to be supplied with information passed in
  67. * via DT.
  68. * If a PHY package is found, PHY is searched also there.
  69. */
  70. static int of_mdiobus_find_phy(struct device *dev, struct mdio_device *mdiodev,
  71. struct device_node *np)
  72. {
  73. struct device_node *child;
  74. for_each_available_child_of_node(np, child) {
  75. int addr;
  76. if (of_node_name_eq(child, "ethernet-phy-package")) {
  77. /* Validate PHY package reg presence */
  78. if (!of_property_present(child, "reg")) {
  79. of_node_put(child);
  80. return -EINVAL;
  81. }
  82. if (!of_mdiobus_find_phy(dev, mdiodev, child)) {
  83. /* The refcount for the PHY package will be
  84. * incremented later when PHY join the Package.
  85. */
  86. of_node_put(child);
  87. return 0;
  88. }
  89. continue;
  90. }
  91. addr = of_mdio_parse_addr(dev, child);
  92. if (addr < 0)
  93. continue;
  94. if (addr == mdiodev->addr) {
  95. device_set_node(dev, of_fwnode_handle(child));
  96. /* The refcount on "child" is passed to the mdio
  97. * device. Do _not_ use of_node_put(child) here.
  98. */
  99. return 0;
  100. }
  101. }
  102. return -ENODEV;
  103. }
  104. static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
  105. struct mdio_device *mdiodev)
  106. {
  107. struct device *dev = &mdiodev->dev;
  108. if (dev->of_node || !bus->dev.of_node)
  109. return;
  110. of_mdiobus_find_phy(dev, mdiodev, bus->dev.of_node);
  111. }
  112. #endif
  113. static struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr, bool c45)
  114. {
  115. struct phy_device *phydev = ERR_PTR(-ENODEV);
  116. struct fwnode_handle *fwnode;
  117. char node_name[16];
  118. int err;
  119. phydev = get_phy_device(bus, addr, c45);
  120. if (IS_ERR(phydev))
  121. return phydev;
  122. #if IS_ENABLED(CONFIG_OF_MDIO)
  123. /* For DT, see if the auto-probed phy has a corresponding child
  124. * in the bus node, and set the of_node pointer in this case.
  125. */
  126. of_mdiobus_link_mdiodev(bus, &phydev->mdio);
  127. #endif
  128. /* Search for a swnode for the phy in the swnode hierarchy of the bus.
  129. * If there is no swnode for the phy provided, just ignore it.
  130. */
  131. if (dev_fwnode(&bus->dev) && !dev_fwnode(&phydev->mdio.dev)) {
  132. snprintf(node_name, sizeof(node_name), "ethernet-phy@%d",
  133. addr);
  134. fwnode = fwnode_get_named_child_node(dev_fwnode(&bus->dev),
  135. node_name);
  136. if (fwnode)
  137. device_set_node(&phydev->mdio.dev, fwnode);
  138. }
  139. err = phy_device_register(phydev);
  140. if (err) {
  141. phy_device_free(phydev);
  142. return ERR_PTR(-ENODEV);
  143. }
  144. return phydev;
  145. }
  146. /**
  147. * mdiobus_scan_c22 - scan one address on a bus for C22 MDIO devices.
  148. * @bus: mii_bus to scan
  149. * @addr: address on bus to scan
  150. *
  151. * This function scans one address on the MDIO bus, looking for
  152. * devices which can be identified using a vendor/product ID in
  153. * registers 2 and 3. Not all MDIO devices have such registers, but
  154. * PHY devices typically do. Hence this function assumes anything
  155. * found is a PHY, or can be treated as a PHY. Other MDIO devices,
  156. * such as switches, will probably not be found during the scan.
  157. */
  158. struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr)
  159. {
  160. return mdiobus_scan(bus, addr, false);
  161. }
  162. EXPORT_SYMBOL(mdiobus_scan_c22);
  163. /**
  164. * mdiobus_scan_c45 - scan one address on a bus for C45 MDIO devices.
  165. * @bus: mii_bus to scan
  166. * @addr: address on bus to scan
  167. *
  168. * This function scans one address on the MDIO bus, looking for
  169. * devices which can be identified using a vendor/product ID in
  170. * registers 2 and 3. Not all MDIO devices have such registers, but
  171. * PHY devices typically do. Hence this function assumes anything
  172. * found is a PHY, or can be treated as a PHY. Other MDIO devices,
  173. * such as switches, will probably not be found during the scan.
  174. */
  175. static struct phy_device *mdiobus_scan_c45(struct mii_bus *bus, int addr)
  176. {
  177. return mdiobus_scan(bus, addr, true);
  178. }
  179. static int mdiobus_scan_bus_c22(struct mii_bus *bus)
  180. {
  181. int i;
  182. for (i = 0; i < PHY_MAX_ADDR; i++) {
  183. if ((bus->phy_mask & BIT(i)) == 0) {
  184. struct phy_device *phydev;
  185. phydev = mdiobus_scan_c22(bus, i);
  186. if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
  187. return PTR_ERR(phydev);
  188. }
  189. }
  190. return 0;
  191. }
  192. static int mdiobus_scan_bus_c45(struct mii_bus *bus)
  193. {
  194. int i;
  195. for (i = 0; i < PHY_MAX_ADDR; i++) {
  196. if ((bus->phy_mask & BIT(i)) == 0) {
  197. struct phy_device *phydev;
  198. /* Don't scan C45 if we already have a C22 device */
  199. if (bus->mdio_map[i])
  200. continue;
  201. phydev = mdiobus_scan_c45(bus, i);
  202. if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
  203. return PTR_ERR(phydev);
  204. }
  205. }
  206. return 0;
  207. }
  208. /* There are some C22 PHYs which do bad things when where is a C45
  209. * transaction on the bus, like accepting a read themselves, and
  210. * stomping over the true devices reply, to performing a write to
  211. * themselves which was intended for another device. Now that C22
  212. * devices have been found, see if any of them are bad for C45, and if we
  213. * should skip the C45 scan.
  214. */
  215. static bool mdiobus_prevent_c45_scan(struct mii_bus *bus)
  216. {
  217. struct phy_device *phydev;
  218. mdiobus_for_each_phy(bus, phydev) {
  219. u32 oui = phydev->phy_id >> 10;
  220. if (oui == MICREL_OUI)
  221. return true;
  222. }
  223. return false;
  224. }
  225. /**
  226. * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
  227. * @bus: target mii_bus
  228. * @owner: module containing bus accessor functions
  229. *
  230. * Description: Called by a bus driver to bring up all the PHYs
  231. * on a given bus, and attach them to the bus. Drivers should use
  232. * mdiobus_register() rather than __mdiobus_register() unless they
  233. * need to pass a specific owner module. MDIO devices which are not
  234. * PHYs will not be brought up by this function. They are expected
  235. * to be explicitly listed in DT and instantiated by of_mdiobus_register().
  236. *
  237. * Returns 0 on success or < 0 on error.
  238. */
  239. int __mdiobus_register(struct mii_bus *bus, struct module *owner)
  240. {
  241. struct mdio_device *mdiodev;
  242. struct gpio_desc *gpiod;
  243. bool prevent_c45_scan;
  244. int i, err;
  245. if (!bus || !bus->name)
  246. return -EINVAL;
  247. /* An access method always needs both read and write operations */
  248. if (!!bus->read != !!bus->write || !!bus->read_c45 != !!bus->write_c45)
  249. return -EINVAL;
  250. /* At least one method is mandatory */
  251. if (!bus->read && !bus->read_c45)
  252. return -EINVAL;
  253. if (bus->parent && bus->parent->of_node)
  254. bus->parent->of_node->fwnode.flags |=
  255. FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD;
  256. WARN(bus->state != MDIOBUS_ALLOCATED &&
  257. bus->state != MDIOBUS_UNREGISTERED,
  258. "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id);
  259. bus->owner = owner;
  260. bus->dev.parent = bus->parent;
  261. bus->dev.class = &mdio_bus_class;
  262. bus->dev.groups = NULL;
  263. dev_set_name(&bus->dev, "%s", bus->id);
  264. /* If the bus state is allocated, we're registering a fresh bus
  265. * that may have a fwnode associated with it. Grab a reference
  266. * to the fwnode. This will be dropped when the bus is released.
  267. * If the bus was set to unregistered, it means that the bus was
  268. * previously registered, and we've already grabbed a reference.
  269. */
  270. if (bus->state == MDIOBUS_ALLOCATED)
  271. fwnode_handle_get(dev_fwnode(&bus->dev));
  272. /* We need to set state to MDIOBUS_UNREGISTERED to correctly release
  273. * the device in mdiobus_free()
  274. *
  275. * State will be updated later in this function in case of success
  276. */
  277. bus->state = MDIOBUS_UNREGISTERED;
  278. err = device_register(&bus->dev);
  279. if (err) {
  280. pr_err("mii_bus %s failed to register\n", bus->id);
  281. return -EINVAL;
  282. }
  283. mutex_init(&bus->mdio_lock);
  284. mutex_init(&bus->shared_lock);
  285. /* assert bus level PHY GPIO reset */
  286. gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH);
  287. if (IS_ERR(gpiod)) {
  288. err = dev_err_probe(&bus->dev, PTR_ERR(gpiod),
  289. "mii_bus %s couldn't get reset GPIO\n",
  290. bus->id);
  291. device_del(&bus->dev);
  292. return err;
  293. } else if (gpiod) {
  294. bus->reset_gpiod = gpiod;
  295. fsleep(bus->reset_delay_us);
  296. gpiod_set_value_cansleep(gpiod, 0);
  297. if (bus->reset_post_delay_us > 0)
  298. fsleep(bus->reset_post_delay_us);
  299. }
  300. if (bus->reset) {
  301. err = bus->reset(bus);
  302. if (err)
  303. goto error_reset_gpiod;
  304. }
  305. if (bus->read) {
  306. err = mdiobus_scan_bus_c22(bus);
  307. if (err)
  308. goto error;
  309. }
  310. prevent_c45_scan = mdiobus_prevent_c45_scan(bus);
  311. if (!prevent_c45_scan && bus->read_c45) {
  312. err = mdiobus_scan_bus_c45(bus);
  313. if (err)
  314. goto error;
  315. }
  316. bus->state = MDIOBUS_REGISTERED;
  317. dev_dbg(&bus->dev, "probed\n");
  318. return 0;
  319. error:
  320. for (i = 0; i < PHY_MAX_ADDR; i++) {
  321. mdiodev = bus->mdio_map[i];
  322. if (!mdiodev)
  323. continue;
  324. mdiodev->device_remove(mdiodev);
  325. mdiodev->device_free(mdiodev);
  326. }
  327. error_reset_gpiod:
  328. /* Put PHYs in RESET to save power */
  329. if (bus->reset_gpiod)
  330. gpiod_set_value_cansleep(bus->reset_gpiod, 1);
  331. device_del(&bus->dev);
  332. return err;
  333. }
  334. EXPORT_SYMBOL(__mdiobus_register);
  335. void mdiobus_unregister(struct mii_bus *bus)
  336. {
  337. struct mdio_device *mdiodev;
  338. int i;
  339. if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED))
  340. return;
  341. bus->state = MDIOBUS_UNREGISTERED;
  342. for (i = 0; i < PHY_MAX_ADDR; i++) {
  343. mdiodev = bus->mdio_map[i];
  344. if (!mdiodev)
  345. continue;
  346. mdiodev->device_remove(mdiodev);
  347. mdiodev->device_free(mdiodev);
  348. }
  349. /* Put PHYs in RESET to save power */
  350. if (bus->reset_gpiod)
  351. gpiod_set_value_cansleep(bus->reset_gpiod, 1);
  352. device_del(&bus->dev);
  353. }
  354. EXPORT_SYMBOL(mdiobus_unregister);
  355. /**
  356. * mdiobus_free - free a struct mii_bus
  357. * @bus: mii_bus to free
  358. *
  359. * This function releases the reference to the underlying device
  360. * object in the mii_bus. If this is the last reference, the mii_bus
  361. * will be freed.
  362. */
  363. void mdiobus_free(struct mii_bus *bus)
  364. {
  365. /* For compatibility with error handling in drivers. */
  366. if (bus->state == MDIOBUS_ALLOCATED) {
  367. kfree(bus);
  368. return;
  369. }
  370. WARN(bus->state != MDIOBUS_UNREGISTERED,
  371. "%s: not in UNREGISTERED state\n", bus->id);
  372. bus->state = MDIOBUS_RELEASED;
  373. put_device(&bus->dev);
  374. }
  375. EXPORT_SYMBOL(mdiobus_free);