mdio-mux.c 4.7 KB

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