mdio_bus.c 7.2 KB

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