mdio_bus.c 11 KB

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