mdio_bus.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. *
  54. * Description: called when the last reference to an mii_bus is
  55. * dropped, to free the underlying memory.
  56. */
  57. static void mdiobus_release(struct device *d)
  58. {
  59. struct mii_bus *bus = to_mii_bus(d);
  60. BUG_ON(bus->state != MDIOBUS_RELEASED);
  61. kfree(bus);
  62. }
  63. static struct class mdio_bus_class = {
  64. .name = "mdio_bus",
  65. .dev_release = mdiobus_release,
  66. };
  67. /**
  68. * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
  69. * @bus: target mii_bus
  70. *
  71. * Description: Called by a bus driver to bring up all the PHYs
  72. * on a given bus, and attach them to the bus.
  73. *
  74. * Returns 0 on success or < 0 on error.
  75. */
  76. int mdiobus_register(struct mii_bus *bus)
  77. {
  78. int i;
  79. int err = 0;
  80. if (NULL == bus || NULL == bus->name ||
  81. NULL == bus->read ||
  82. NULL == bus->write)
  83. return -EINVAL;
  84. BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
  85. bus->state != MDIOBUS_UNREGISTERED);
  86. bus->dev.parent = bus->parent;
  87. bus->dev.class = &mdio_bus_class;
  88. bus->dev.groups = NULL;
  89. memcpy(bus->dev.bus_id, bus->id, MII_BUS_ID_SIZE);
  90. err = device_register(&bus->dev);
  91. if (err) {
  92. printk(KERN_ERR "mii_bus %s failed to register\n", bus->id);
  93. return -EINVAL;
  94. }
  95. bus->state = MDIOBUS_REGISTERED;
  96. mutex_init(&bus->mdio_lock);
  97. if (bus->reset)
  98. bus->reset(bus);
  99. for (i = 0; i < PHY_MAX_ADDR; i++) {
  100. bus->phy_map[i] = NULL;
  101. if ((bus->phy_mask & (1 << i)) == 0) {
  102. struct phy_device *phydev;
  103. phydev = mdiobus_scan(bus, i);
  104. if (IS_ERR(phydev))
  105. err = PTR_ERR(phydev);
  106. }
  107. }
  108. pr_info("%s: probed\n", bus->name);
  109. return err;
  110. }
  111. EXPORT_SYMBOL(mdiobus_register);
  112. void mdiobus_unregister(struct mii_bus *bus)
  113. {
  114. int i;
  115. BUG_ON(bus->state != MDIOBUS_REGISTERED);
  116. bus->state = MDIOBUS_UNREGISTERED;
  117. device_unregister(&bus->dev);
  118. for (i = 0; i < PHY_MAX_ADDR; i++) {
  119. if (bus->phy_map[i])
  120. device_unregister(&bus->phy_map[i]->dev);
  121. }
  122. }
  123. EXPORT_SYMBOL(mdiobus_unregister);
  124. /**
  125. * mdiobus_free - free a struct mii_bus
  126. * @bus: mii_bus to free
  127. *
  128. * This function releases the reference to the underlying device
  129. * object in the mii_bus. If this is the last reference, the mii_bus
  130. * will be freed.
  131. */
  132. void mdiobus_free(struct mii_bus *bus)
  133. {
  134. /*
  135. * For compatibility with error handling in drivers.
  136. */
  137. if (bus->state == MDIOBUS_ALLOCATED) {
  138. kfree(bus);
  139. return;
  140. }
  141. BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
  142. bus->state = MDIOBUS_RELEASED;
  143. put_device(&bus->dev);
  144. }
  145. EXPORT_SYMBOL(mdiobus_free);
  146. struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
  147. {
  148. struct phy_device *phydev;
  149. int err;
  150. phydev = get_phy_device(bus, addr);
  151. if (IS_ERR(phydev) || phydev == NULL)
  152. return phydev;
  153. /* There's a PHY at this address
  154. * We need to set:
  155. * 1) IRQ
  156. * 2) bus_id
  157. * 3) parent
  158. * 4) bus
  159. * 5) mii_bus
  160. * And, we need to register it */
  161. phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
  162. phydev->dev.parent = bus->parent;
  163. phydev->dev.bus = &mdio_bus_type;
  164. snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, addr);
  165. phydev->bus = bus;
  166. /* Run all of the fixups for this PHY */
  167. phy_scan_fixups(phydev);
  168. err = device_register(&phydev->dev);
  169. if (err) {
  170. printk(KERN_ERR "phy %d failed to register\n", addr);
  171. phy_device_free(phydev);
  172. phydev = NULL;
  173. }
  174. bus->phy_map[addr] = phydev;
  175. return phydev;
  176. }
  177. EXPORT_SYMBOL(mdiobus_scan);
  178. /**
  179. * mdiobus_read - Convenience function for reading a given MII mgmt register
  180. * @bus: the mii_bus struct
  181. * @addr: the phy address
  182. * @regnum: register number to read
  183. *
  184. * NOTE: MUST NOT be called from interrupt context,
  185. * because the bus read/write functions may wait for an interrupt
  186. * to conclude the operation.
  187. */
  188. int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum)
  189. {
  190. int retval;
  191. BUG_ON(in_interrupt());
  192. mutex_lock(&bus->mdio_lock);
  193. retval = bus->read(bus, addr, regnum);
  194. mutex_unlock(&bus->mdio_lock);
  195. return retval;
  196. }
  197. EXPORT_SYMBOL(mdiobus_read);
  198. /**
  199. * mdiobus_write - Convenience function for writing a given MII mgmt register
  200. * @bus: the mii_bus struct
  201. * @addr: the phy address
  202. * @regnum: register number to write
  203. * @val: value to write to @regnum
  204. *
  205. * NOTE: MUST NOT be called from interrupt context,
  206. * because the bus read/write functions may wait for an interrupt
  207. * to conclude the operation.
  208. */
  209. int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val)
  210. {
  211. int err;
  212. BUG_ON(in_interrupt());
  213. mutex_lock(&bus->mdio_lock);
  214. err = bus->write(bus, addr, regnum, val);
  215. mutex_unlock(&bus->mdio_lock);
  216. return err;
  217. }
  218. EXPORT_SYMBOL(mdiobus_write);
  219. /**
  220. * mdio_bus_match - determine if given PHY driver supports the given PHY device
  221. * @dev: target PHY device
  222. * @drv: given PHY driver
  223. *
  224. * Description: Given a PHY device, and a PHY driver, return 1 if
  225. * the driver supports the device. Otherwise, return 0.
  226. */
  227. static int mdio_bus_match(struct device *dev, struct device_driver *drv)
  228. {
  229. struct phy_device *phydev = to_phy_device(dev);
  230. struct phy_driver *phydrv = to_phy_driver(drv);
  231. return ((phydrv->phy_id & phydrv->phy_id_mask) ==
  232. (phydev->phy_id & phydrv->phy_id_mask));
  233. }
  234. /* Suspend and resume. Copied from platform_suspend and
  235. * platform_resume
  236. */
  237. static int mdio_bus_suspend(struct device * dev, pm_message_t state)
  238. {
  239. int ret = 0;
  240. struct device_driver *drv = dev->driver;
  241. if (drv && drv->suspend)
  242. ret = drv->suspend(dev, state);
  243. return ret;
  244. }
  245. static int mdio_bus_resume(struct device * dev)
  246. {
  247. int ret = 0;
  248. struct device_driver *drv = dev->driver;
  249. if (drv && drv->resume)
  250. ret = drv->resume(dev);
  251. return ret;
  252. }
  253. struct bus_type mdio_bus_type = {
  254. .name = "mdio_bus",
  255. .match = mdio_bus_match,
  256. .suspend = mdio_bus_suspend,
  257. .resume = mdio_bus_resume,
  258. };
  259. EXPORT_SYMBOL(mdio_bus_type);
  260. int __init mdio_bus_init(void)
  261. {
  262. int ret;
  263. ret = class_register(&mdio_bus_class);
  264. if (!ret) {
  265. ret = bus_register(&mdio_bus_type);
  266. if (ret)
  267. class_unregister(&mdio_bus_class);
  268. }
  269. return ret;
  270. }
  271. void mdio_bus_exit(void)
  272. {
  273. class_unregister(&mdio_bus_class);
  274. bus_unregister(&mdio_bus_type);
  275. }