mdio_bus.c 9.2 KB

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