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