mdio_bus.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/mii.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/phy.h>
  33. #include <asm/io.h>
  34. #include <asm/irq.h>
  35. #include <asm/uaccess.h>
  36. /**
  37. * mdiobus_alloc - allocate a mii_bus structure
  38. *
  39. * Description: called by a bus driver to allocate an mii_bus
  40. * structure to fill in.
  41. */
  42. struct mii_bus *mdiobus_alloc(void)
  43. {
  44. struct mii_bus *bus;
  45. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  46. if (bus != NULL)
  47. bus->state = MDIOBUS_ALLOCATED;
  48. return bus;
  49. }
  50. EXPORT_SYMBOL(mdiobus_alloc);
  51. /**
  52. * mdiobus_release - mii_bus device release callback
  53. * @d: the target struct device that contains the mii_bus
  54. *
  55. * Description: called when the last reference to an mii_bus is
  56. * dropped, to free the underlying memory.
  57. */
  58. static void mdiobus_release(struct device *d)
  59. {
  60. struct mii_bus *bus = to_mii_bus(d);
  61. BUG_ON(bus->state != MDIOBUS_RELEASED &&
  62. /* for compatibility with error handling in drivers */
  63. bus->state != MDIOBUS_ALLOCATED);
  64. kfree(bus);
  65. }
  66. static struct class mdio_bus_class = {
  67. .name = "mdio_bus",
  68. .dev_release = mdiobus_release,
  69. };
  70. /**
  71. * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
  72. * @bus: target mii_bus
  73. *
  74. * Description: Called by a bus driver to bring up all the PHYs
  75. * on a given bus, and attach them to the bus.
  76. *
  77. * Returns 0 on success or < 0 on error.
  78. */
  79. int mdiobus_register(struct mii_bus *bus)
  80. {
  81. int i, err;
  82. if (NULL == bus || NULL == bus->name ||
  83. NULL == bus->read ||
  84. NULL == bus->write)
  85. return -EINVAL;
  86. BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
  87. bus->state != MDIOBUS_UNREGISTERED);
  88. bus->dev.parent = bus->parent;
  89. bus->dev.class = &mdio_bus_class;
  90. bus->dev.groups = NULL;
  91. dev_set_name(&bus->dev, bus->id);
  92. err = device_register(&bus->dev);
  93. if (err) {
  94. printk(KERN_ERR "mii_bus %s failed to register\n", bus->id);
  95. return -EINVAL;
  96. }
  97. mutex_init(&bus->mdio_lock);
  98. if (bus->reset)
  99. bus->reset(bus);
  100. for (i = 0; i < PHY_MAX_ADDR; i++) {
  101. bus->phy_map[i] = NULL;
  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. }
  133. }
  134. EXPORT_SYMBOL(mdiobus_unregister);
  135. /**
  136. * mdiobus_free - free a struct mii_bus
  137. * @bus: mii_bus to free
  138. *
  139. * This function releases the reference to the underlying device
  140. * object in the mii_bus. If this is the last reference, the mii_bus
  141. * will be freed.
  142. */
  143. void mdiobus_free(struct mii_bus *bus)
  144. {
  145. /*
  146. * For compatibility with error handling in drivers.
  147. */
  148. if (bus->state == MDIOBUS_ALLOCATED) {
  149. kfree(bus);
  150. return;
  151. }
  152. BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
  153. bus->state = MDIOBUS_RELEASED;
  154. put_device(&bus->dev);
  155. }
  156. EXPORT_SYMBOL(mdiobus_free);
  157. struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
  158. {
  159. struct phy_device *phydev;
  160. int err;
  161. phydev = get_phy_device(bus, addr);
  162. if (IS_ERR(phydev) || phydev == NULL)
  163. return phydev;
  164. /* There's a PHY at this address
  165. * We need to set:
  166. * 1) IRQ
  167. * 2) bus_id
  168. * 3) parent
  169. * 4) bus
  170. * 5) mii_bus
  171. * And, we need to register it */
  172. phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
  173. phydev->dev.parent = bus->parent;
  174. phydev->dev.bus = &mdio_bus_type;
  175. dev_set_name(&phydev->dev, PHY_ID_FMT, bus->id, addr);
  176. phydev->bus = bus;
  177. /* Run all of the fixups for this PHY */
  178. phy_scan_fixups(phydev);
  179. err = device_register(&phydev->dev);
  180. if (err) {
  181. printk(KERN_ERR "phy %d failed to register\n", addr);
  182. phy_device_free(phydev);
  183. phydev = NULL;
  184. }
  185. bus->phy_map[addr] = phydev;
  186. return phydev;
  187. }
  188. EXPORT_SYMBOL(mdiobus_scan);
  189. /**
  190. * mdiobus_read - Convenience function for reading a given MII mgmt register
  191. * @bus: the mii_bus struct
  192. * @addr: the phy address
  193. * @regnum: register number to read
  194. *
  195. * NOTE: MUST NOT be called from interrupt context,
  196. * because the bus read/write functions may wait for an interrupt
  197. * to conclude the operation.
  198. */
  199. int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum)
  200. {
  201. int retval;
  202. BUG_ON(in_interrupt());
  203. mutex_lock(&bus->mdio_lock);
  204. retval = bus->read(bus, addr, regnum);
  205. mutex_unlock(&bus->mdio_lock);
  206. return retval;
  207. }
  208. EXPORT_SYMBOL(mdiobus_read);
  209. /**
  210. * mdiobus_write - Convenience function for writing a given MII mgmt register
  211. * @bus: the mii_bus struct
  212. * @addr: the phy address
  213. * @regnum: register number to write
  214. * @val: value to write to @regnum
  215. *
  216. * NOTE: MUST NOT be called from interrupt context,
  217. * because the bus read/write functions may wait for an interrupt
  218. * to conclude the operation.
  219. */
  220. int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val)
  221. {
  222. int err;
  223. BUG_ON(in_interrupt());
  224. mutex_lock(&bus->mdio_lock);
  225. err = bus->write(bus, addr, regnum, val);
  226. mutex_unlock(&bus->mdio_lock);
  227. return err;
  228. }
  229. EXPORT_SYMBOL(mdiobus_write);
  230. /**
  231. * mdio_bus_match - determine if given PHY driver supports the given PHY device
  232. * @dev: target PHY device
  233. * @drv: given PHY driver
  234. *
  235. * Description: Given a PHY device, and a PHY driver, return 1 if
  236. * the driver supports the device. Otherwise, return 0.
  237. */
  238. static int mdio_bus_match(struct device *dev, struct device_driver *drv)
  239. {
  240. struct phy_device *phydev = to_phy_device(dev);
  241. struct phy_driver *phydrv = to_phy_driver(drv);
  242. return ((phydrv->phy_id & phydrv->phy_id_mask) ==
  243. (phydev->phy_id & phydrv->phy_id_mask));
  244. }
  245. /* Suspend and resume. Copied from platform_suspend and
  246. * platform_resume
  247. */
  248. static int mdio_bus_suspend(struct device * dev, pm_message_t state)
  249. {
  250. int ret = 0;
  251. struct device_driver *drv = dev->driver;
  252. struct phy_driver *phydrv = to_phy_driver(drv);
  253. struct phy_device *phydev = to_phy_device(dev);
  254. if ((!device_may_wakeup(phydev->dev.parent)) &&
  255. (phydrv && phydrv->suspend))
  256. ret = phydrv->suspend(phydev);
  257. return ret;
  258. }
  259. static int mdio_bus_resume(struct device * dev)
  260. {
  261. int ret = 0;
  262. struct device_driver *drv = dev->driver;
  263. struct phy_driver *phydrv = to_phy_driver(drv);
  264. struct phy_device *phydev = to_phy_device(dev);
  265. if ((!device_may_wakeup(phydev->dev.parent)) &&
  266. (phydrv && phydrv->resume))
  267. ret = phydrv->resume(phydev);
  268. return ret;
  269. }
  270. struct bus_type mdio_bus_type = {
  271. .name = "mdio_bus",
  272. .match = mdio_bus_match,
  273. .suspend = mdio_bus_suspend,
  274. .resume = mdio_bus_resume,
  275. };
  276. EXPORT_SYMBOL(mdio_bus_type);
  277. int __init mdio_bus_init(void)
  278. {
  279. int ret;
  280. ret = class_register(&mdio_bus_class);
  281. if (!ret) {
  282. ret = bus_register(&mdio_bus_type);
  283. if (ret)
  284. class_unregister(&mdio_bus_class);
  285. }
  286. return ret;
  287. }
  288. void mdio_bus_exit(void)
  289. {
  290. class_unregister(&mdio_bus_class);
  291. bus_unregister(&mdio_bus_type);
  292. }