mdio_bus.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. bus->phy_map[i] = NULL;
  103. if ((bus->phy_mask & (1 << i)) == 0) {
  104. struct phy_device *phydev;
  105. phydev = mdiobus_scan(bus, i);
  106. if (IS_ERR(phydev)) {
  107. err = PTR_ERR(phydev);
  108. goto error;
  109. }
  110. }
  111. }
  112. bus->state = MDIOBUS_REGISTERED;
  113. pr_info("%s: probed\n", bus->name);
  114. return 0;
  115. error:
  116. while (--i >= 0) {
  117. if (bus->phy_map[i])
  118. device_unregister(&bus->phy_map[i]->dev);
  119. }
  120. device_del(&bus->dev);
  121. return err;
  122. }
  123. EXPORT_SYMBOL(mdiobus_register);
  124. void mdiobus_unregister(struct mii_bus *bus)
  125. {
  126. int i;
  127. BUG_ON(bus->state != MDIOBUS_REGISTERED);
  128. bus->state = MDIOBUS_UNREGISTERED;
  129. device_del(&bus->dev);
  130. for (i = 0; i < PHY_MAX_ADDR; i++) {
  131. if (bus->phy_map[i])
  132. device_unregister(&bus->phy_map[i]->dev);
  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. /* There's a PHY at this address
  166. * We need to set:
  167. * 1) IRQ
  168. * 2) bus_id
  169. * 3) parent
  170. * 4) bus
  171. * 5) mii_bus
  172. * And, we need to register it */
  173. phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
  174. phydev->dev.parent = bus->parent;
  175. phydev->dev.bus = &mdio_bus_type;
  176. dev_set_name(&phydev->dev, PHY_ID_FMT, bus->id, addr);
  177. phydev->bus = bus;
  178. /* Run all of the fixups for this PHY */
  179. phy_scan_fixups(phydev);
  180. err = device_register(&phydev->dev);
  181. if (err) {
  182. printk(KERN_ERR "phy %d failed to register\n", addr);
  183. phy_device_free(phydev);
  184. phydev = NULL;
  185. }
  186. bus->phy_map[addr] = phydev;
  187. return phydev;
  188. }
  189. EXPORT_SYMBOL(mdiobus_scan);
  190. /**
  191. * mdiobus_read - Convenience function for reading a given MII mgmt register
  192. * @bus: the mii_bus struct
  193. * @addr: the phy address
  194. * @regnum: register number to read
  195. *
  196. * NOTE: MUST NOT be called from interrupt context,
  197. * because the bus read/write functions may wait for an interrupt
  198. * to conclude the operation.
  199. */
  200. int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum)
  201. {
  202. int retval;
  203. BUG_ON(in_interrupt());
  204. mutex_lock(&bus->mdio_lock);
  205. retval = bus->read(bus, addr, regnum);
  206. mutex_unlock(&bus->mdio_lock);
  207. return retval;
  208. }
  209. EXPORT_SYMBOL(mdiobus_read);
  210. /**
  211. * mdiobus_write - Convenience function for writing a given MII mgmt register
  212. * @bus: the mii_bus struct
  213. * @addr: the phy address
  214. * @regnum: register number to write
  215. * @val: value to write to @regnum
  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_write(struct mii_bus *bus, int addr, u16 regnum, u16 val)
  222. {
  223. int err;
  224. BUG_ON(in_interrupt());
  225. mutex_lock(&bus->mdio_lock);
  226. err = bus->write(bus, addr, regnum, val);
  227. mutex_unlock(&bus->mdio_lock);
  228. return err;
  229. }
  230. EXPORT_SYMBOL(mdiobus_write);
  231. /**
  232. * mdio_bus_match - determine if given PHY driver supports the given PHY device
  233. * @dev: target PHY device
  234. * @drv: given PHY driver
  235. *
  236. * Description: Given a PHY device, and a PHY driver, return 1 if
  237. * the driver supports the device. Otherwise, return 0.
  238. */
  239. static int mdio_bus_match(struct device *dev, struct device_driver *drv)
  240. {
  241. struct phy_device *phydev = to_phy_device(dev);
  242. struct phy_driver *phydrv = to_phy_driver(drv);
  243. return ((phydrv->phy_id & phydrv->phy_id_mask) ==
  244. (phydev->phy_id & phydrv->phy_id_mask));
  245. }
  246. static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
  247. {
  248. struct device_driver *drv = phydev->dev.driver;
  249. struct phy_driver *phydrv = to_phy_driver(drv);
  250. struct net_device *netdev = phydev->attached_dev;
  251. if (!drv || !phydrv->suspend)
  252. return false;
  253. /* PHY not attached? May suspend. */
  254. if (!netdev)
  255. return true;
  256. /*
  257. * Don't suspend PHY if the attched netdev parent may wakeup.
  258. * The parent may point to a PCI device, as in tg3 driver.
  259. */
  260. if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
  261. return false;
  262. /*
  263. * Also don't suspend PHY if the netdev itself may wakeup. This
  264. * is the case for devices w/o underlaying pwr. mgmt. aware bus,
  265. * e.g. SoC devices.
  266. */
  267. if (device_may_wakeup(&netdev->dev))
  268. return false;
  269. return true;
  270. }
  271. /* Suspend and resume. Copied from platform_suspend and
  272. * platform_resume
  273. */
  274. static int mdio_bus_suspend(struct device * dev, pm_message_t state)
  275. {
  276. struct phy_driver *phydrv = to_phy_driver(dev->driver);
  277. struct phy_device *phydev = to_phy_device(dev);
  278. if (!mdio_bus_phy_may_suspend(phydev))
  279. return 0;
  280. return phydrv->suspend(phydev);
  281. }
  282. static int mdio_bus_resume(struct device * dev)
  283. {
  284. struct phy_driver *phydrv = to_phy_driver(dev->driver);
  285. struct phy_device *phydev = to_phy_device(dev);
  286. if (!mdio_bus_phy_may_suspend(phydev))
  287. return 0;
  288. return phydrv->resume(phydev);
  289. }
  290. struct bus_type mdio_bus_type = {
  291. .name = "mdio_bus",
  292. .match = mdio_bus_match,
  293. .suspend = mdio_bus_suspend,
  294. .resume = mdio_bus_resume,
  295. };
  296. EXPORT_SYMBOL(mdio_bus_type);
  297. int __init mdio_bus_init(void)
  298. {
  299. int ret;
  300. ret = class_register(&mdio_bus_class);
  301. if (!ret) {
  302. ret = bus_register(&mdio_bus_type);
  303. if (ret)
  304. class_unregister(&mdio_bus_class);
  305. }
  306. return ret;
  307. }
  308. void mdio_bus_exit(void)
  309. {
  310. class_unregister(&mdio_bus_class);
  311. bus_unregister(&mdio_bus_type);
  312. }