mdio_bus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * drivers/net/phy/mdio_bus.c
  3. *
  4. * MDIO Bus interface
  5. *
  6. * Author: Andy Fleming
  7. *
  8. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/errno.h>
  20. #include <linux/unistd.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/device.h>
  26. #include <linux/of_device.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/mm.h>
  32. #include <linux/module.h>
  33. #include <linux/mii.h>
  34. #include <linux/ethtool.h>
  35. #include <linux/phy.h>
  36. #include <asm/io.h>
  37. #include <asm/irq.h>
  38. #include <asm/uaccess.h>
  39. /**
  40. * mdiobus_alloc_size - allocate a mii_bus structure
  41. * @size: extra amount of memory to allocate for private storage.
  42. * If non-zero, then bus->priv is points to that memory.
  43. *
  44. * Description: called by a bus driver to allocate an mii_bus
  45. * structure to fill in.
  46. */
  47. struct mii_bus *mdiobus_alloc_size(size_t size)
  48. {
  49. struct mii_bus *bus;
  50. size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
  51. size_t alloc_size;
  52. /* If we alloc extra space, it should be aligned */
  53. if (size)
  54. alloc_size = aligned_size + size;
  55. else
  56. alloc_size = sizeof(*bus);
  57. bus = kzalloc(alloc_size, GFP_KERNEL);
  58. if (bus) {
  59. bus->state = MDIOBUS_ALLOCATED;
  60. if (size)
  61. bus->priv = (void *)bus + aligned_size;
  62. }
  63. return bus;
  64. }
  65. EXPORT_SYMBOL(mdiobus_alloc_size);
  66. /**
  67. * mdiobus_release - mii_bus device release callback
  68. * @d: the target struct device that contains the mii_bus
  69. *
  70. * Description: called when the last reference to an mii_bus is
  71. * dropped, to free the underlying memory.
  72. */
  73. static void mdiobus_release(struct device *d)
  74. {
  75. struct mii_bus *bus = to_mii_bus(d);
  76. BUG_ON(bus->state != MDIOBUS_RELEASED &&
  77. /* for compatibility with error handling in drivers */
  78. bus->state != MDIOBUS_ALLOCATED);
  79. kfree(bus);
  80. }
  81. static struct class mdio_bus_class = {
  82. .name = "mdio_bus",
  83. .dev_release = mdiobus_release,
  84. };
  85. #if IS_ENABLED(CONFIG_OF_MDIO)
  86. /* Helper function for of_mdio_find_bus */
  87. static int of_mdio_bus_match(struct device *dev, void *mdio_bus_np)
  88. {
  89. return dev->of_node == mdio_bus_np;
  90. }
  91. /**
  92. * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
  93. * @mdio_bus_np: Pointer to the mii_bus.
  94. *
  95. * Returns a pointer to the mii_bus, or NULL if none found.
  96. *
  97. * Because the association of a device_node and mii_bus is made via
  98. * of_mdiobus_register(), the mii_bus cannot be found before it is
  99. * registered with of_mdiobus_register().
  100. *
  101. */
  102. struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
  103. {
  104. struct device *d;
  105. if (!mdio_bus_np)
  106. return NULL;
  107. d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
  108. of_mdio_bus_match);
  109. return d ? to_mii_bus(d) : NULL;
  110. }
  111. EXPORT_SYMBOL(of_mdio_find_bus);
  112. #endif
  113. /**
  114. * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
  115. * @bus: target mii_bus
  116. *
  117. * Description: Called by a bus driver to bring up all the PHYs
  118. * on a given bus, and attach them to the bus.
  119. *
  120. * Returns 0 on success or < 0 on error.
  121. */
  122. int mdiobus_register(struct mii_bus *bus)
  123. {
  124. int i, err;
  125. if (NULL == bus || NULL == bus->name ||
  126. NULL == bus->read ||
  127. NULL == bus->write)
  128. return -EINVAL;
  129. BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
  130. bus->state != MDIOBUS_UNREGISTERED);
  131. bus->dev.parent = bus->parent;
  132. bus->dev.class = &mdio_bus_class;
  133. bus->dev.groups = NULL;
  134. dev_set_name(&bus->dev, "%s", bus->id);
  135. err = device_register(&bus->dev);
  136. if (err) {
  137. pr_err("mii_bus %s failed to register\n", bus->id);
  138. return -EINVAL;
  139. }
  140. mutex_init(&bus->mdio_lock);
  141. if (bus->reset)
  142. bus->reset(bus);
  143. for (i = 0; i < PHY_MAX_ADDR; i++) {
  144. if ((bus->phy_mask & (1 << i)) == 0) {
  145. struct phy_device *phydev;
  146. phydev = mdiobus_scan(bus, i);
  147. if (IS_ERR(phydev)) {
  148. err = PTR_ERR(phydev);
  149. goto error;
  150. }
  151. }
  152. }
  153. bus->state = MDIOBUS_REGISTERED;
  154. pr_info("%s: probed\n", bus->name);
  155. return 0;
  156. error:
  157. while (--i >= 0) {
  158. if (bus->phy_map[i])
  159. device_unregister(&bus->phy_map[i]->dev);
  160. }
  161. device_del(&bus->dev);
  162. return err;
  163. }
  164. EXPORT_SYMBOL(mdiobus_register);
  165. void mdiobus_unregister(struct mii_bus *bus)
  166. {
  167. int i;
  168. BUG_ON(bus->state != MDIOBUS_REGISTERED);
  169. bus->state = MDIOBUS_UNREGISTERED;
  170. device_del(&bus->dev);
  171. for (i = 0; i < PHY_MAX_ADDR; i++) {
  172. if (bus->phy_map[i])
  173. device_unregister(&bus->phy_map[i]->dev);
  174. bus->phy_map[i] = NULL;
  175. }
  176. }
  177. EXPORT_SYMBOL(mdiobus_unregister);
  178. /**
  179. * mdiobus_free - free a struct mii_bus
  180. * @bus: mii_bus to free
  181. *
  182. * This function releases the reference to the underlying device
  183. * object in the mii_bus. If this is the last reference, the mii_bus
  184. * will be freed.
  185. */
  186. void mdiobus_free(struct mii_bus *bus)
  187. {
  188. /*
  189. * For compatibility with error handling in drivers.
  190. */
  191. if (bus->state == MDIOBUS_ALLOCATED) {
  192. kfree(bus);
  193. return;
  194. }
  195. BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
  196. bus->state = MDIOBUS_RELEASED;
  197. put_device(&bus->dev);
  198. }
  199. EXPORT_SYMBOL(mdiobus_free);
  200. struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
  201. {
  202. struct phy_device *phydev;
  203. int err;
  204. phydev = get_phy_device(bus, addr, false);
  205. if (IS_ERR(phydev) || phydev == NULL)
  206. return phydev;
  207. err = phy_device_register(phydev);
  208. if (err) {
  209. phy_device_free(phydev);
  210. return NULL;
  211. }
  212. return phydev;
  213. }
  214. EXPORT_SYMBOL(mdiobus_scan);
  215. /**
  216. * mdiobus_read - Convenience function for reading a given MII mgmt register
  217. * @bus: the mii_bus struct
  218. * @addr: the phy address
  219. * @regnum: register number to read
  220. *
  221. * NOTE: MUST NOT be called from interrupt context,
  222. * because the bus read/write functions may wait for an interrupt
  223. * to conclude the operation.
  224. */
  225. int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
  226. {
  227. int retval;
  228. BUG_ON(in_interrupt());
  229. mutex_lock(&bus->mdio_lock);
  230. retval = bus->read(bus, addr, regnum);
  231. mutex_unlock(&bus->mdio_lock);
  232. return retval;
  233. }
  234. EXPORT_SYMBOL(mdiobus_read);
  235. /**
  236. * mdiobus_write - Convenience function for writing a given MII mgmt register
  237. * @bus: the mii_bus struct
  238. * @addr: the phy address
  239. * @regnum: register number to write
  240. * @val: value to write to @regnum
  241. *
  242. * NOTE: MUST NOT be called from interrupt context,
  243. * because the bus read/write functions may wait for an interrupt
  244. * to conclude the operation.
  245. */
  246. int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  247. {
  248. int err;
  249. BUG_ON(in_interrupt());
  250. mutex_lock(&bus->mdio_lock);
  251. err = bus->write(bus, addr, regnum, val);
  252. mutex_unlock(&bus->mdio_lock);
  253. return err;
  254. }
  255. EXPORT_SYMBOL(mdiobus_write);
  256. /**
  257. * mdio_bus_match - determine if given PHY driver supports the given PHY device
  258. * @dev: target PHY device
  259. * @drv: given PHY driver
  260. *
  261. * Description: Given a PHY device, and a PHY driver, return 1 if
  262. * the driver supports the device. Otherwise, return 0.
  263. */
  264. static int mdio_bus_match(struct device *dev, struct device_driver *drv)
  265. {
  266. struct phy_device *phydev = to_phy_device(dev);
  267. struct phy_driver *phydrv = to_phy_driver(drv);
  268. if (of_driver_match_device(dev, drv))
  269. return 1;
  270. if (phydrv->match_phy_device)
  271. return phydrv->match_phy_device(phydev);
  272. return ((phydrv->phy_id & phydrv->phy_id_mask) ==
  273. (phydev->phy_id & phydrv->phy_id_mask));
  274. }
  275. #ifdef CONFIG_PM
  276. static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
  277. {
  278. struct device_driver *drv = phydev->dev.driver;
  279. struct phy_driver *phydrv = to_phy_driver(drv);
  280. struct net_device *netdev = phydev->attached_dev;
  281. if (!drv || !phydrv->suspend)
  282. return false;
  283. /* PHY not attached? May suspend. */
  284. if (!netdev)
  285. return true;
  286. /*
  287. * Don't suspend PHY if the attched netdev parent may wakeup.
  288. * The parent may point to a PCI device, as in tg3 driver.
  289. */
  290. if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
  291. return false;
  292. /*
  293. * Also don't suspend PHY if the netdev itself may wakeup. This
  294. * is the case for devices w/o underlaying pwr. mgmt. aware bus,
  295. * e.g. SoC devices.
  296. */
  297. if (device_may_wakeup(&netdev->dev))
  298. return false;
  299. return true;
  300. }
  301. static int mdio_bus_suspend(struct device *dev)
  302. {
  303. struct phy_driver *phydrv = to_phy_driver(dev->driver);
  304. struct phy_device *phydev = to_phy_device(dev);
  305. /*
  306. * We must stop the state machine manually, otherwise it stops out of
  307. * control, possibly with the phydev->lock held. Upon resume, netdev
  308. * may call phy routines that try to grab the same lock, and that may
  309. * lead to a deadlock.
  310. */
  311. if (phydev->attached_dev && phydev->adjust_link)
  312. phy_stop_machine(phydev);
  313. if (!mdio_bus_phy_may_suspend(phydev))
  314. return 0;
  315. return phydrv->suspend(phydev);
  316. }
  317. static int mdio_bus_resume(struct device *dev)
  318. {
  319. struct phy_driver *phydrv = to_phy_driver(dev->driver);
  320. struct phy_device *phydev = to_phy_device(dev);
  321. int ret;
  322. if (!mdio_bus_phy_may_suspend(phydev))
  323. goto no_resume;
  324. ret = phydrv->resume(phydev);
  325. if (ret < 0)
  326. return ret;
  327. no_resume:
  328. if (phydev->attached_dev && phydev->adjust_link)
  329. phy_start_machine(phydev, NULL);
  330. return 0;
  331. }
  332. static int mdio_bus_restore(struct device *dev)
  333. {
  334. struct phy_device *phydev = to_phy_device(dev);
  335. struct net_device *netdev = phydev->attached_dev;
  336. int ret;
  337. if (!netdev)
  338. return 0;
  339. ret = phy_init_hw(phydev);
  340. if (ret < 0)
  341. return ret;
  342. /* The PHY needs to renegotiate. */
  343. phydev->link = 0;
  344. phydev->state = PHY_UP;
  345. phy_start_machine(phydev, NULL);
  346. return 0;
  347. }
  348. static struct dev_pm_ops mdio_bus_pm_ops = {
  349. .suspend = mdio_bus_suspend,
  350. .resume = mdio_bus_resume,
  351. .freeze = mdio_bus_suspend,
  352. .thaw = mdio_bus_resume,
  353. .restore = mdio_bus_restore,
  354. };
  355. #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
  356. #else
  357. #define MDIO_BUS_PM_OPS NULL
  358. #endif /* CONFIG_PM */
  359. struct bus_type mdio_bus_type = {
  360. .name = "mdio_bus",
  361. .match = mdio_bus_match,
  362. .pm = MDIO_BUS_PM_OPS,
  363. };
  364. EXPORT_SYMBOL(mdio_bus_type);
  365. int __init mdio_bus_init(void)
  366. {
  367. int ret;
  368. ret = class_register(&mdio_bus_class);
  369. if (!ret) {
  370. ret = bus_register(&mdio_bus_type);
  371. if (ret)
  372. class_unregister(&mdio_bus_class);
  373. }
  374. return ret;
  375. }
  376. void mdio_bus_exit(void)
  377. {
  378. class_unregister(&mdio_bus_class);
  379. bus_unregister(&mdio_bus_type);
  380. }