mdio-mux.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011, 2012 Cavium, Inc.
  4. */
  5. #include <linux/platform_device.h>
  6. #include <linux/mdio-mux.h>
  7. #include <linux/of_mdio.h>
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/phy.h>
  11. #define DRV_DESCRIPTION "MDIO bus multiplexer driver"
  12. struct mdio_mux_child_bus;
  13. struct mdio_mux_parent_bus {
  14. struct mii_bus *mii_bus;
  15. int current_child;
  16. int parent_id;
  17. void *switch_data;
  18. int (*switch_fn)(int current_child, int desired_child, void *data);
  19. /* List of our children linked through their next fields. */
  20. struct mdio_mux_child_bus *children;
  21. };
  22. struct mdio_mux_child_bus {
  23. struct mii_bus *mii_bus;
  24. struct mdio_mux_parent_bus *parent;
  25. struct mdio_mux_child_bus *next;
  26. int bus_number;
  27. };
  28. /*
  29. * The parent bus' lock is used to order access to the switch_fn.
  30. */
  31. static int mdio_mux_read(struct mii_bus *bus, int phy_id, int regnum)
  32. {
  33. struct mdio_mux_child_bus *cb = bus->priv;
  34. struct mdio_mux_parent_bus *pb = cb->parent;
  35. int r;
  36. mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
  37. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  38. if (r)
  39. goto out;
  40. pb->current_child = cb->bus_number;
  41. r = pb->mii_bus->read(pb->mii_bus, phy_id, regnum);
  42. out:
  43. mutex_unlock(&pb->mii_bus->mdio_lock);
  44. return r;
  45. }
  46. /*
  47. * The parent bus' lock is used to order access to the switch_fn.
  48. */
  49. static int mdio_mux_write(struct mii_bus *bus, int phy_id,
  50. int regnum, u16 val)
  51. {
  52. struct mdio_mux_child_bus *cb = bus->priv;
  53. struct mdio_mux_parent_bus *pb = cb->parent;
  54. int r;
  55. mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
  56. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  57. if (r)
  58. goto out;
  59. pb->current_child = cb->bus_number;
  60. r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val);
  61. out:
  62. mutex_unlock(&pb->mii_bus->mdio_lock);
  63. return r;
  64. }
  65. static int parent_count;
  66. static void mdio_mux_uninit_children(struct mdio_mux_parent_bus *pb)
  67. {
  68. struct mdio_mux_child_bus *cb = pb->children;
  69. while (cb) {
  70. mdiobus_unregister(cb->mii_bus);
  71. mdiobus_free(cb->mii_bus);
  72. cb = cb->next;
  73. }
  74. }
  75. int mdio_mux_init(struct device *dev,
  76. struct device_node *mux_node,
  77. int (*switch_fn)(int cur, int desired, void *data),
  78. void **mux_handle,
  79. void *data,
  80. struct mii_bus *mux_bus)
  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 (!mux_node)
  89. return -ENODEV;
  90. if (!mux_bus) {
  91. parent_bus_node = of_parse_phandle(mux_node,
  92. "mdio-parent-bus", 0);
  93. if (!parent_bus_node)
  94. return -ENODEV;
  95. parent_bus = of_mdio_find_bus(parent_bus_node);
  96. if (!parent_bus) {
  97. ret_val = -EPROBE_DEFER;
  98. goto err_parent_bus;
  99. }
  100. } else {
  101. parent_bus_node = NULL;
  102. parent_bus = mux_bus;
  103. get_device(&parent_bus->dev);
  104. }
  105. pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
  106. if (!pb) {
  107. ret_val = -ENOMEM;
  108. goto err_pb_kz;
  109. }
  110. pb->switch_data = data;
  111. pb->switch_fn = switch_fn;
  112. pb->current_child = -1;
  113. pb->parent_id = parent_count++;
  114. pb->mii_bus = parent_bus;
  115. ret_val = -ENODEV;
  116. for_each_available_child_of_node(mux_node, child_bus_node) {
  117. int v;
  118. r = of_property_read_u32(child_bus_node, "reg", &v);
  119. if (r) {
  120. dev_err(dev,
  121. "Error: Failed to find reg for child %pOF\n",
  122. child_bus_node);
  123. continue;
  124. }
  125. cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
  126. if (!cb) {
  127. ret_val = -ENOMEM;
  128. goto err_loop;
  129. }
  130. cb->bus_number = v;
  131. cb->parent = pb;
  132. cb->mii_bus = mdiobus_alloc();
  133. if (!cb->mii_bus) {
  134. ret_val = -ENOMEM;
  135. goto err_loop;
  136. }
  137. cb->mii_bus->priv = cb;
  138. cb->mii_bus->name = "mdio_mux";
  139. snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%x.%x",
  140. pb->parent_id, v);
  141. cb->mii_bus->parent = dev;
  142. cb->mii_bus->read = mdio_mux_read;
  143. cb->mii_bus->write = mdio_mux_write;
  144. r = of_mdiobus_register(cb->mii_bus, child_bus_node);
  145. if (r) {
  146. mdiobus_free(cb->mii_bus);
  147. if (r == -EPROBE_DEFER) {
  148. ret_val = r;
  149. goto err_loop;
  150. }
  151. devm_kfree(dev, cb);
  152. dev_err(dev,
  153. "Error: Failed to register MDIO bus for child %pOF\n",
  154. child_bus_node);
  155. } else {
  156. cb->next = pb->children;
  157. pb->children = cb;
  158. }
  159. }
  160. if (pb->children) {
  161. *mux_handle = pb;
  162. return 0;
  163. }
  164. dev_err(dev, "Error: No acceptable child buses found\n");
  165. devm_kfree(dev, pb);
  166. err_loop:
  167. mdio_mux_uninit_children(pb);
  168. of_node_put(child_bus_node);
  169. err_pb_kz:
  170. put_device(&parent_bus->dev);
  171. err_parent_bus:
  172. of_node_put(parent_bus_node);
  173. return ret_val;
  174. }
  175. EXPORT_SYMBOL_GPL(mdio_mux_init);
  176. void mdio_mux_uninit(void *mux_handle)
  177. {
  178. struct mdio_mux_parent_bus *pb = mux_handle;
  179. mdio_mux_uninit_children(pb);
  180. put_device(&pb->mii_bus->dev);
  181. }
  182. EXPORT_SYMBOL_GPL(mdio_mux_uninit);
  183. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  184. MODULE_AUTHOR("David Daney");
  185. MODULE_LICENSE("GPL v2");