mdio-mux.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
  41. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  42. if (r)
  43. goto out;
  44. pb->current_child = cb->bus_number;
  45. r = pb->mii_bus->read(pb->mii_bus, phy_id, regnum);
  46. out:
  47. mutex_unlock(&pb->mii_bus->mdio_lock);
  48. return r;
  49. }
  50. /*
  51. * The parent bus' lock is used to order access to the switch_fn.
  52. */
  53. static int mdio_mux_write(struct mii_bus *bus, int phy_id,
  54. int regnum, u16 val)
  55. {
  56. struct mdio_mux_child_bus *cb = bus->priv;
  57. struct mdio_mux_parent_bus *pb = cb->parent;
  58. int r;
  59. mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
  60. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  61. if (r)
  62. goto out;
  63. pb->current_child = cb->bus_number;
  64. r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val);
  65. out:
  66. mutex_unlock(&pb->mii_bus->mdio_lock);
  67. return r;
  68. }
  69. static int parent_count;
  70. int mdio_mux_init(struct device *dev,
  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 (!dev->of_node)
  83. return -ENODEV;
  84. if (!mux_bus) {
  85. parent_bus_node = of_parse_phandle(dev->of_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. }
  98. pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
  99. if (pb == NULL) {
  100. ret_val = -ENOMEM;
  101. goto err_pb_kz;
  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. devm_kfree(dev, cb);
  128. of_node_put(child_bus_node);
  129. break;
  130. }
  131. cb->mii_bus->priv = cb;
  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. cb->next = pb->children;
  144. pb->children = cb;
  145. }
  146. }
  147. if (pb->children) {
  148. *mux_handle = pb;
  149. dev_info(dev, "Version " DRV_VERSION "\n");
  150. return 0;
  151. }
  152. devm_kfree(dev, pb);
  153. err_pb_kz:
  154. /* balance the reference of_mdio_find_bus() took */
  155. if (!mux_bus)
  156. put_device(&parent_bus->dev);
  157. err_parent_bus:
  158. of_node_put(parent_bus_node);
  159. return ret_val;
  160. }
  161. EXPORT_SYMBOL_GPL(mdio_mux_init);
  162. void mdio_mux_uninit(void *mux_handle)
  163. {
  164. struct mdio_mux_parent_bus *pb = mux_handle;
  165. struct mdio_mux_child_bus *cb = pb->children;
  166. while (cb) {
  167. mdiobus_unregister(cb->mii_bus);
  168. mdiobus_free(cb->mii_bus);
  169. cb = cb->next;
  170. }
  171. /* balance the reference of_mdio_find_bus() in mdio_mux_init() took */
  172. put_device(&pb->mii_bus->dev);
  173. }
  174. EXPORT_SYMBOL_GPL(mdio_mux_uninit);
  175. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  176. MODULE_VERSION(DRV_VERSION);
  177. MODULE_AUTHOR("David Daney");
  178. MODULE_LICENSE("GPL");