mdio-mux.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. {
  75. struct device_node *parent_bus_node;
  76. struct device_node *child_bus_node;
  77. int r, ret_val;
  78. struct mii_bus *parent_bus;
  79. struct mdio_mux_parent_bus *pb;
  80. struct mdio_mux_child_bus *cb;
  81. if (!dev->of_node)
  82. return -ENODEV;
  83. parent_bus_node = of_parse_phandle(dev->of_node, "mdio-parent-bus", 0);
  84. if (!parent_bus_node)
  85. return -ENODEV;
  86. pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
  87. if (pb == NULL) {
  88. ret_val = -ENOMEM;
  89. goto err_parent_bus;
  90. }
  91. parent_bus = of_mdio_find_bus(parent_bus_node);
  92. if (parent_bus == NULL) {
  93. ret_val = -EPROBE_DEFER;
  94. goto err_parent_bus;
  95. }
  96. pb->switch_data = data;
  97. pb->switch_fn = switch_fn;
  98. pb->current_child = -1;
  99. pb->parent_id = parent_count++;
  100. pb->mii_bus = parent_bus;
  101. ret_val = -ENODEV;
  102. for_each_available_child_of_node(dev->of_node, child_bus_node) {
  103. u32 v;
  104. r = of_property_read_u32(child_bus_node, "reg", &v);
  105. if (r)
  106. continue;
  107. cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
  108. if (cb == NULL) {
  109. dev_err(dev,
  110. "Error: Failed to allocate memory for child\n");
  111. ret_val = -ENOMEM;
  112. of_node_put(child_bus_node);
  113. break;
  114. }
  115. cb->bus_number = v;
  116. cb->parent = pb;
  117. cb->mii_bus = mdiobus_alloc();
  118. if (!cb->mii_bus) {
  119. ret_val = -ENOMEM;
  120. of_node_put(child_bus_node);
  121. break;
  122. }
  123. cb->mii_bus->priv = cb;
  124. cb->mii_bus->name = "mdio_mux";
  125. snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%x.%x",
  126. pb->parent_id, v);
  127. cb->mii_bus->parent = dev;
  128. cb->mii_bus->read = mdio_mux_read;
  129. cb->mii_bus->write = mdio_mux_write;
  130. r = of_mdiobus_register(cb->mii_bus, child_bus_node);
  131. if (r) {
  132. mdiobus_free(cb->mii_bus);
  133. devm_kfree(dev, cb);
  134. } else {
  135. of_node_get(child_bus_node);
  136. cb->next = pb->children;
  137. pb->children = cb;
  138. }
  139. }
  140. if (pb->children) {
  141. *mux_handle = pb;
  142. dev_info(dev, "Version " DRV_VERSION "\n");
  143. return 0;
  144. }
  145. /* balance the reference of_mdio_find_bus() took */
  146. put_device(&pb->mii_bus->dev);
  147. err_parent_bus:
  148. of_node_put(parent_bus_node);
  149. return ret_val;
  150. }
  151. EXPORT_SYMBOL_GPL(mdio_mux_init);
  152. void mdio_mux_uninit(void *mux_handle)
  153. {
  154. struct mdio_mux_parent_bus *pb = mux_handle;
  155. struct mdio_mux_child_bus *cb = pb->children;
  156. while (cb) {
  157. mdiobus_unregister(cb->mii_bus);
  158. mdiobus_free(cb->mii_bus);
  159. cb = cb->next;
  160. }
  161. /* balance the reference of_mdio_find_bus() in mdio_mux_init() took */
  162. put_device(&pb->mii_bus->dev);
  163. }
  164. EXPORT_SYMBOL_GPL(mdio_mux_uninit);
  165. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  166. MODULE_VERSION(DRV_VERSION);
  167. MODULE_AUTHOR("David Daney");
  168. MODULE_LICENSE("GPL");