mdio_bus.c 11 KB

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