mdio-mux.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. int phy_irq[PHY_MAX_ADDR];
  32. };
  33. /*
  34. * The parent bus' lock is used to order access to the switch_fn.
  35. */
  36. static int mdio_mux_read(struct mii_bus *bus, int phy_id, int regnum)
  37. {
  38. struct mdio_mux_child_bus *cb = bus->priv;
  39. struct mdio_mux_parent_bus *pb = cb->parent;
  40. int r;
  41. mutex_lock(&pb->mii_bus->mdio_lock);
  42. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  43. if (r)
  44. goto out;
  45. pb->current_child = cb->bus_number;
  46. r = pb->mii_bus->read(pb->mii_bus, phy_id, regnum);
  47. out:
  48. mutex_unlock(&pb->mii_bus->mdio_lock);
  49. return r;
  50. }
  51. /*
  52. * The parent bus' lock is used to order access to the switch_fn.
  53. */
  54. static int mdio_mux_write(struct mii_bus *bus, int phy_id,
  55. int regnum, u16 val)
  56. {
  57. struct mdio_mux_child_bus *cb = bus->priv;
  58. struct mdio_mux_parent_bus *pb = cb->parent;
  59. int r;
  60. mutex_lock(&pb->mii_bus->mdio_lock);
  61. r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
  62. if (r)
  63. goto out;
  64. pb->current_child = cb->bus_number;
  65. r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val);
  66. out:
  67. mutex_unlock(&pb->mii_bus->mdio_lock);
  68. return r;
  69. }
  70. static int parent_count;
  71. int mdio_mux_init(struct device *dev,
  72. int (*switch_fn)(int cur, int desired, void *data),
  73. void **mux_handle,
  74. void *data)
  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. parent_bus_node = of_parse_phandle(dev->of_node, "mdio-parent-bus", 0);
  85. if (!parent_bus_node)
  86. return -ENODEV;
  87. parent_bus = of_mdio_find_bus(parent_bus_node);
  88. if (parent_bus == NULL) {
  89. ret_val = -EPROBE_DEFER;
  90. goto err_parent_bus;
  91. }
  92. pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
  93. if (pb == NULL) {
  94. ret_val = -ENOMEM;
  95. goto err_parent_bus;
  96. }
  97. pb->switch_data = data;
  98. pb->switch_fn = switch_fn;
  99. pb->current_child = -1;
  100. pb->parent_id = parent_count++;
  101. pb->mii_bus = parent_bus;
  102. ret_val = -ENODEV;
  103. for_each_child_of_node(dev->of_node, child_bus_node) {
  104. u32 v;
  105. r = of_property_read_u32(child_bus_node, "reg", &v);
  106. if (r)
  107. continue;
  108. cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
  109. if (cb == NULL) {
  110. dev_err(dev,
  111. "Error: Failed to allocate memory for child\n");
  112. ret_val = -ENOMEM;
  113. break;
  114. }
  115. cb->bus_number = v;
  116. cb->parent = pb;
  117. cb->mii_bus = mdiobus_alloc();
  118. cb->mii_bus->priv = cb;
  119. cb->mii_bus->irq = cb->phy_irq;
  120. cb->mii_bus->name = "mdio_mux";
  121. snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%x.%x",
  122. pb->parent_id, v);
  123. cb->mii_bus->parent = dev;
  124. cb->mii_bus->read = mdio_mux_read;
  125. cb->mii_bus->write = mdio_mux_write;
  126. r = of_mdiobus_register(cb->mii_bus, child_bus_node);
  127. if (r) {
  128. mdiobus_free(cb->mii_bus);
  129. devm_kfree(dev, cb);
  130. } else {
  131. of_node_get(child_bus_node);
  132. cb->next = pb->children;
  133. pb->children = cb;
  134. }
  135. }
  136. if (pb->children) {
  137. *mux_handle = pb;
  138. dev_info(dev, "Version " DRV_VERSION "\n");
  139. return 0;
  140. }
  141. err_parent_bus:
  142. of_node_put(parent_bus_node);
  143. return ret_val;
  144. }
  145. EXPORT_SYMBOL_GPL(mdio_mux_init);
  146. void mdio_mux_uninit(void *mux_handle)
  147. {
  148. struct mdio_mux_parent_bus *pb = mux_handle;
  149. struct mdio_mux_child_bus *cb = pb->children;
  150. while (cb) {
  151. mdiobus_unregister(cb->mii_bus);
  152. mdiobus_free(cb->mii_bus);
  153. cb = cb->next;
  154. }
  155. }
  156. EXPORT_SYMBOL_GPL(mdio_mux_uninit);
  157. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  158. MODULE_VERSION(DRV_VERSION);
  159. MODULE_AUTHOR("David Daney");
  160. MODULE_LICENSE("GPL");