mdio-mux.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011, 2012 Cavium, Inc.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/mdio-mux.h>
  7. #include <linux/module.h>
  8. #include <linux/of_mdio.h>
  9. #include <linux/phy.h>
  10. #include <linux/platform_device.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. static int mdio_mux_read_c45(struct mii_bus *bus, int phy_id, int dev_addr,
  47. int regnum)
  48. {
  49. struct mdio_mux_child_bus *cb = bus->priv;
  50. struct mdio_mux_parent_bus *pb = cb->parent;
  51. int r;
  52. mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
  53. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  54. if (r)
  55. goto out;
  56. pb->current_child = cb->bus_number;
  57. r = pb->mii_bus->read_c45(pb->mii_bus, phy_id, dev_addr, regnum);
  58. out:
  59. mutex_unlock(&pb->mii_bus->mdio_lock);
  60. return r;
  61. }
  62. /*
  63. * The parent bus' lock is used to order access to the switch_fn.
  64. */
  65. static int mdio_mux_write(struct mii_bus *bus, int phy_id,
  66. int regnum, u16 val)
  67. {
  68. struct mdio_mux_child_bus *cb = bus->priv;
  69. struct mdio_mux_parent_bus *pb = cb->parent;
  70. int r;
  71. mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
  72. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  73. if (r)
  74. goto out;
  75. pb->current_child = cb->bus_number;
  76. r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val);
  77. out:
  78. mutex_unlock(&pb->mii_bus->mdio_lock);
  79. return r;
  80. }
  81. static int mdio_mux_write_c45(struct mii_bus *bus, int phy_id, int dev_addr,
  82. int regnum, u16 val)
  83. {
  84. struct mdio_mux_child_bus *cb = bus->priv;
  85. struct mdio_mux_parent_bus *pb = cb->parent;
  86. int r;
  87. mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
  88. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  89. if (r)
  90. goto out;
  91. pb->current_child = cb->bus_number;
  92. r = pb->mii_bus->write_c45(pb->mii_bus, phy_id, dev_addr, regnum, val);
  93. out:
  94. mutex_unlock(&pb->mii_bus->mdio_lock);
  95. return r;
  96. }
  97. static int parent_count;
  98. static void mdio_mux_uninit_children(struct mdio_mux_parent_bus *pb)
  99. {
  100. struct mdio_mux_child_bus *cb = pb->children;
  101. while (cb) {
  102. mdiobus_unregister(cb->mii_bus);
  103. mdiobus_free(cb->mii_bus);
  104. cb = cb->next;
  105. }
  106. }
  107. int mdio_mux_init(struct device *dev,
  108. struct device_node *mux_node,
  109. int (*switch_fn)(int cur, int desired, void *data),
  110. void **mux_handle,
  111. void *data,
  112. struct mii_bus *mux_bus)
  113. {
  114. struct device_node *parent_bus_node;
  115. struct device_node *child_bus_node;
  116. int r, ret_val;
  117. struct mii_bus *parent_bus;
  118. struct mdio_mux_parent_bus *pb;
  119. struct mdio_mux_child_bus *cb;
  120. if (!mux_node)
  121. return -ENODEV;
  122. if (!mux_bus) {
  123. parent_bus_node = of_parse_phandle(mux_node,
  124. "mdio-parent-bus", 0);
  125. if (!parent_bus_node)
  126. return -ENODEV;
  127. parent_bus = of_mdio_find_bus(parent_bus_node);
  128. if (!parent_bus) {
  129. ret_val = -EPROBE_DEFER;
  130. goto err_parent_bus;
  131. }
  132. } else {
  133. parent_bus_node = NULL;
  134. parent_bus = mux_bus;
  135. get_device(&parent_bus->dev);
  136. }
  137. pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
  138. if (!pb) {
  139. ret_val = -ENOMEM;
  140. goto err_pb_kz;
  141. }
  142. pb->switch_data = data;
  143. pb->switch_fn = switch_fn;
  144. pb->current_child = -1;
  145. pb->parent_id = parent_count++;
  146. pb->mii_bus = parent_bus;
  147. ret_val = -ENODEV;
  148. for_each_available_child_of_node(mux_node, child_bus_node) {
  149. int v;
  150. r = of_property_read_u32(child_bus_node, "reg", &v);
  151. if (r) {
  152. dev_err(dev,
  153. "Error: Failed to find reg for child %pOF: %pe\n",
  154. child_bus_node, ERR_PTR(r));
  155. continue;
  156. }
  157. cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
  158. if (!cb) {
  159. ret_val = -ENOMEM;
  160. goto err_loop;
  161. }
  162. cb->bus_number = v;
  163. cb->parent = pb;
  164. cb->mii_bus = mdiobus_alloc();
  165. if (!cb->mii_bus) {
  166. ret_val = -ENOMEM;
  167. goto err_loop;
  168. }
  169. cb->mii_bus->priv = cb;
  170. cb->mii_bus->name = "mdio_mux";
  171. snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x.%x",
  172. cb->mii_bus->name, pb->parent_id, v);
  173. cb->mii_bus->parent = dev;
  174. if (parent_bus->read)
  175. cb->mii_bus->read = mdio_mux_read;
  176. if (parent_bus->write)
  177. cb->mii_bus->write = mdio_mux_write;
  178. if (parent_bus->read_c45)
  179. cb->mii_bus->read_c45 = mdio_mux_read_c45;
  180. if (parent_bus->write_c45)
  181. cb->mii_bus->write_c45 = mdio_mux_write_c45;
  182. r = of_mdiobus_register(cb->mii_bus, child_bus_node);
  183. if (r) {
  184. mdiobus_free(cb->mii_bus);
  185. if (r == -EPROBE_DEFER) {
  186. ret_val = r;
  187. goto err_loop;
  188. }
  189. devm_kfree(dev, cb);
  190. dev_err(dev,
  191. "Error: Failed to register MDIO bus for child %pOF: %pe\n",
  192. child_bus_node, ERR_PTR(r));
  193. } else {
  194. cb->next = pb->children;
  195. pb->children = cb;
  196. }
  197. }
  198. if (pb->children) {
  199. *mux_handle = pb;
  200. return 0;
  201. }
  202. dev_err(dev, "Error: No acceptable child buses found\n");
  203. err_loop:
  204. mdio_mux_uninit_children(pb);
  205. of_node_put(child_bus_node);
  206. err_pb_kz:
  207. put_device(&parent_bus->dev);
  208. err_parent_bus:
  209. of_node_put(parent_bus_node);
  210. return ret_val;
  211. }
  212. EXPORT_SYMBOL_GPL(mdio_mux_init);
  213. void mdio_mux_uninit(void *mux_handle)
  214. {
  215. struct mdio_mux_parent_bus *pb = mux_handle;
  216. mdio_mux_uninit_children(pb);
  217. put_device(&pb->mii_bus->dev);
  218. }
  219. EXPORT_SYMBOL_GPL(mdio_mux_uninit);
  220. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  221. MODULE_AUTHOR("David Daney");
  222. MODULE_LICENSE("GPL v2");