mdio_bus.c 8.8 KB

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