mdio-mux.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2011, 2012 Cavium, Inc.
  7. */
  8. #include <linux/platform_device.h>
  9. #include <linux/mdio-mux.h>
  10. #include <linux/of_mdio.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/phy.h>
  14. #define DRV_VERSION "1.0"
  15. #define DRV_DESCRIPTION "MDIO bus multiplexer driver"
  16. struct mdio_mux_child_bus;
  17. struct mdio_mux_parent_bus {
  18. struct mii_bus *mii_bus;
  19. int current_child;
  20. int parent_id;
  21. void *switch_data;
  22. int (*switch_fn)(int current_child, int desired_child, void *data);
  23. /* List of our children linked through their next fields. */
  24. struct mdio_mux_child_bus *children;
  25. };
  26. struct mdio_mux_child_bus {
  27. struct mii_bus *mii_bus;
  28. struct mdio_mux_parent_bus *parent;
  29. struct mdio_mux_child_bus *next;
  30. int bus_number;
  31. int phy_irq[PHY_MAX_ADDR];
  32. };
  33. /*
  34. * The parent bus' lock is used to order access to the switch_fn.
  35. */
  36. static int mdio_mux_read(struct mii_bus *bus, int phy_id, int regnum)
  37. {
  38. struct mdio_mux_child_bus *cb = bus->priv;
  39. struct mdio_mux_parent_bus *pb = cb->parent;
  40. int r;
  41. /* In theory multiple mdio_mux could be stacked, thus creating
  42. * more than a single level of nesting. But in practice,
  43. * SINGLE_DEPTH_NESTING will cover the vast majority of use
  44. * cases. We use it, instead of trying to handle the general
  45. * case.
  46. */
  47. mutex_lock_nested(&pb->mii_bus->mdio_lock, SINGLE_DEPTH_NESTING);
  48. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  49. if (r)
  50. goto out;
  51. pb->current_child = cb->bus_number;
  52. r = pb->mii_bus->read(pb->mii_bus, phy_id, regnum);
  53. out:
  54. mutex_unlock(&pb->mii_bus->mdio_lock);
  55. return r;
  56. }
  57. /*
  58. * The parent bus' lock is used to order access to the switch_fn.
  59. */
  60. static int mdio_mux_write(struct mii_bus *bus, int phy_id,
  61. int regnum, u16 val)
  62. {
  63. struct mdio_mux_child_bus *cb = bus->priv;
  64. struct mdio_mux_parent_bus *pb = cb->parent;
  65. int r;
  66. mutex_lock_nested(&pb->mii_bus->mdio_lock, SINGLE_DEPTH_NESTING);
  67. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  68. if (r)
  69. goto out;
  70. pb->current_child = cb->bus_number;
  71. r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val);
  72. out:
  73. mutex_unlock(&pb->mii_bus->mdio_lock);
  74. return r;
  75. }
  76. static int parent_count;
  77. int mdio_mux_init(struct device *dev,
  78. int (*switch_fn)(int cur, int desired, void *data),
  79. void **mux_handle,
  80. void *data)
  81. {
  82. struct device_node *parent_bus_node;
  83. struct device_node *child_bus_node;
  84. int r, ret_val;
  85. struct mii_bus *parent_bus;
  86. struct mdio_mux_parent_bus *pb;
  87. struct mdio_mux_child_bus *cb;
  88. if (!dev->of_node)
  89. return -ENODEV;
  90. parent_bus_node = of_parse_phandle(dev->of_node, "mdio-parent-bus", 0);
  91. if (!parent_bus_node)
  92. return -ENODEV;
  93. pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
  94. if (pb == NULL) {
  95. ret_val = -ENOMEM;
  96. goto err_parent_bus;
  97. }
  98. parent_bus = of_mdio_find_bus(parent_bus_node);
  99. if (parent_bus == NULL) {
  100. ret_val = -EPROBE_DEFER;
  101. goto err_parent_bus;
  102. }
  103. pb->switch_data = data;
  104. pb->switch_fn = switch_fn;
  105. pb->current_child = -1;
  106. pb->parent_id = parent_count++;
  107. pb->mii_bus = parent_bus;
  108. ret_val = -ENODEV;
  109. for_each_available_child_of_node(dev->of_node, child_bus_node) {
  110. u32 v;
  111. r = of_property_read_u32(child_bus_node, "reg", &v);
  112. if (r)
  113. continue;
  114. cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
  115. if (cb == NULL) {
  116. dev_err(dev,
  117. "Error: Failed to allocate memory for child\n");
  118. ret_val = -ENOMEM;
  119. of_node_put(child_bus_node);
  120. break;
  121. }
  122. cb->bus_number = v;
  123. cb->parent = pb;
  124. cb->mii_bus = mdiobus_alloc();
  125. if (!cb->mii_bus) {
  126. ret_val = -ENOMEM;
  127. of_node_put(child_bus_node);
  128. break;
  129. }
  130. cb->mii_bus->priv = cb;
  131. cb->mii_bus->irq = cb->phy_irq;
  132. cb->mii_bus->name = "mdio_mux";
  133. snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%x.%x",
  134. pb->parent_id, v);
  135. cb->mii_bus->parent = dev;
  136. cb->mii_bus->read = mdio_mux_read;
  137. cb->mii_bus->write = mdio_mux_write;
  138. r = of_mdiobus_register(cb->mii_bus, child_bus_node);
  139. if (r) {
  140. mdiobus_free(cb->mii_bus);
  141. devm_kfree(dev, cb);
  142. } else {
  143. of_node_get(child_bus_node);
  144. cb->next = pb->children;
  145. pb->children = cb;
  146. }
  147. }
  148. if (pb->children) {
  149. *mux_handle = pb;
  150. dev_info(dev, "Version " DRV_VERSION "\n");
  151. return 0;
  152. }
  153. /* balance the reference of_mdio_find_bus() took */
  154. put_device(&pb->mii_bus->dev);
  155. err_parent_bus:
  156. of_node_put(parent_bus_node);
  157. return ret_val;
  158. }
  159. EXPORT_SYMBOL_GPL(mdio_mux_init);
  160. void mdio_mux_uninit(void *mux_handle)
  161. {
  162. struct mdio_mux_parent_bus *pb = mux_handle;
  163. struct mdio_mux_child_bus *cb = pb->children;
  164. while (cb) {
  165. mdiobus_unregister(cb->mii_bus);
  166. mdiobus_free(cb->mii_bus);
  167. cb = cb->next;
  168. }
  169. /* balance the reference of_mdio_find_bus() in mdio_mux_init() took */
  170. put_device(&pb->mii_bus->dev);
  171. }
  172. EXPORT_SYMBOL_GPL(mdio_mux_uninit);
  173. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  174. MODULE_VERSION(DRV_VERSION);
  175. MODULE_AUTHOR("David Daney");
  176. MODULE_LICENSE("GPL");