mdio-mux.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. };
  32. /*
  33. * The parent bus' lock is used to order access to the switch_fn.
  34. */
  35. static int mdio_mux_read(struct mii_bus *bus, int phy_id, int regnum)
  36. {
  37. struct mdio_mux_child_bus *cb = bus->priv;
  38. struct mdio_mux_parent_bus *pb = cb->parent;
  39. int r;
  40. /* In theory multiple mdio_mux could be stacked, thus creating
  41. * more than a single level of nesting. But in practice,
  42. * SINGLE_DEPTH_NESTING will cover the vast majority of use
  43. * cases. We use it, instead of trying to handle the general
  44. * case.
  45. */
  46. mutex_lock_nested(&pb->mii_bus->mdio_lock, SINGLE_DEPTH_NESTING);
  47. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  48. if (r)
  49. goto out;
  50. pb->current_child = cb->bus_number;
  51. r = pb->mii_bus->read(pb->mii_bus, phy_id, regnum);
  52. out:
  53. mutex_unlock(&pb->mii_bus->mdio_lock);
  54. return r;
  55. }
  56. /*
  57. * The parent bus' lock is used to order access to the switch_fn.
  58. */
  59. static int mdio_mux_write(struct mii_bus *bus, int phy_id,
  60. int regnum, u16 val)
  61. {
  62. struct mdio_mux_child_bus *cb = bus->priv;
  63. struct mdio_mux_parent_bus *pb = cb->parent;
  64. int r;
  65. mutex_lock_nested(&pb->mii_bus->mdio_lock, SINGLE_DEPTH_NESTING);
  66. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  67. if (r)
  68. goto out;
  69. pb->current_child = cb->bus_number;
  70. r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val);
  71. out:
  72. mutex_unlock(&pb->mii_bus->mdio_lock);
  73. return r;
  74. }
  75. static int parent_count;
  76. int mdio_mux_init(struct device *dev,
  77. int (*switch_fn)(int cur, int desired, void *data),
  78. void **mux_handle,
  79. void *data)
  80. {
  81. struct device_node *parent_bus_node;
  82. struct device_node *child_bus_node;
  83. int r, ret_val;
  84. struct mii_bus *parent_bus;
  85. struct mdio_mux_parent_bus *pb;
  86. struct mdio_mux_child_bus *cb;
  87. if (!dev->of_node)
  88. return -ENODEV;
  89. parent_bus_node = of_parse_phandle(dev->of_node, "mdio-parent-bus", 0);
  90. if (!parent_bus_node)
  91. return -ENODEV;
  92. pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
  93. if (pb == NULL) {
  94. ret_val = -ENOMEM;
  95. goto err_parent_bus;
  96. }
  97. parent_bus = of_mdio_find_bus(parent_bus_node);
  98. if (parent_bus == NULL) {
  99. ret_val = -EPROBE_DEFER;
  100. goto err_parent_bus;
  101. }
  102. pb->switch_data = data;
  103. pb->switch_fn = switch_fn;
  104. pb->current_child = -1;
  105. pb->parent_id = parent_count++;
  106. pb->mii_bus = parent_bus;
  107. ret_val = -ENODEV;
  108. for_each_available_child_of_node(dev->of_node, child_bus_node) {
  109. u32 v;
  110. r = of_property_read_u32(child_bus_node, "reg", &v);
  111. if (r)
  112. continue;
  113. cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
  114. if (cb == NULL) {
  115. dev_err(dev,
  116. "Error: Failed to allocate memory for child\n");
  117. ret_val = -ENOMEM;
  118. of_node_put(child_bus_node);
  119. break;
  120. }
  121. cb->bus_number = v;
  122. cb->parent = pb;
  123. cb->mii_bus = mdiobus_alloc();
  124. if (!cb->mii_bus) {
  125. ret_val = -ENOMEM;
  126. of_node_put(child_bus_node);
  127. break;
  128. }
  129. cb->mii_bus->priv = cb;
  130. cb->mii_bus->name = "mdio_mux";
  131. snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%x.%x",
  132. pb->parent_id, v);
  133. cb->mii_bus->parent = dev;
  134. cb->mii_bus->read = mdio_mux_read;
  135. cb->mii_bus->write = mdio_mux_write;
  136. r = of_mdiobus_register(cb->mii_bus, child_bus_node);
  137. if (r) {
  138. mdiobus_free(cb->mii_bus);
  139. devm_kfree(dev, cb);
  140. } else {
  141. of_node_get(child_bus_node);
  142. cb->next = pb->children;
  143. pb->children = cb;
  144. }
  145. }
  146. if (pb->children) {
  147. *mux_handle = pb;
  148. dev_info(dev, "Version " DRV_VERSION "\n");
  149. return 0;
  150. }
  151. /* balance the reference of_mdio_find_bus() took */
  152. put_device(&pb->mii_bus->dev);
  153. err_parent_bus:
  154. of_node_put(parent_bus_node);
  155. return ret_val;
  156. }
  157. EXPORT_SYMBOL_GPL(mdio_mux_init);
  158. void mdio_mux_uninit(void *mux_handle)
  159. {
  160. struct mdio_mux_parent_bus *pb = mux_handle;
  161. struct mdio_mux_child_bus *cb = pb->children;
  162. while (cb) {
  163. mdiobus_unregister(cb->mii_bus);
  164. mdiobus_free(cb->mii_bus);
  165. cb = cb->next;
  166. }
  167. /* balance the reference of_mdio_find_bus() in mdio_mux_init() took */
  168. put_device(&pb->mii_bus->dev);
  169. }
  170. EXPORT_SYMBOL_GPL(mdio_mux_uninit);
  171. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  172. MODULE_VERSION(DRV_VERSION);
  173. MODULE_AUTHOR("David Daney");
  174. MODULE_LICENSE("GPL");