mdio_bus.c 7.2 KB

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