mdio-mux-mmioreg.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Simple memory-mapped device MDIO MUX driver
  3. *
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * Copyright 2012 Freescale Semiconductor, Inc.
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. #include <linux/platform_device.h>
  13. #include <linux/device.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_mdio.h>
  16. #include <linux/module.h>
  17. #include <linux/phy.h>
  18. #include <linux/mdio-mux.h>
  19. struct mdio_mux_mmioreg_state {
  20. void *mux_handle;
  21. phys_addr_t phys;
  22. unsigned int iosize;
  23. unsigned int mask;
  24. };
  25. /*
  26. * MDIO multiplexing switch function
  27. *
  28. * This function is called by the mdio-mux layer when it thinks the mdio bus
  29. * multiplexer needs to switch.
  30. *
  31. * 'current_child' is the current value of the mux register (masked via
  32. * s->mask).
  33. *
  34. * 'desired_child' is the value of the 'reg' property of the target child MDIO
  35. * node.
  36. *
  37. * The first time this function is called, current_child == -1.
  38. *
  39. * If current_child == desired_child, then the mux is already set to the
  40. * correct bus.
  41. */
  42. static int mdio_mux_mmioreg_switch_fn(int current_child, int desired_child,
  43. void *data)
  44. {
  45. struct mdio_mux_mmioreg_state *s = data;
  46. if (current_child ^ desired_child) {
  47. void __iomem *p = ioremap(s->phys, s->iosize);
  48. if (!p)
  49. return -ENOMEM;
  50. switch (s->iosize) {
  51. case sizeof(uint8_t): {
  52. uint8_t x, y;
  53. x = ioread8(p);
  54. y = (x & ~s->mask) | desired_child;
  55. if (x != y) {
  56. iowrite8((x & ~s->mask) | desired_child, p);
  57. pr_debug("%s: %02x -> %02x\n", __func__, x, y);
  58. }
  59. break;
  60. }
  61. case sizeof(uint16_t): {
  62. uint16_t x, y;
  63. x = ioread16(p);
  64. y = (x & ~s->mask) | desired_child;
  65. if (x != y) {
  66. iowrite16((x & ~s->mask) | desired_child, p);
  67. pr_debug("%s: %04x -> %04x\n", __func__, x, y);
  68. }
  69. break;
  70. }
  71. case sizeof(uint32_t): {
  72. uint32_t x, y;
  73. x = ioread32(p);
  74. y = (x & ~s->mask) | desired_child;
  75. if (x != y) {
  76. iowrite32((x & ~s->mask) | desired_child, p);
  77. pr_debug("%s: %08x -> %08x\n", __func__, x, y);
  78. }
  79. break;
  80. }
  81. }
  82. iounmap(p);
  83. }
  84. return 0;
  85. }
  86. static int mdio_mux_mmioreg_probe(struct platform_device *pdev)
  87. {
  88. struct device_node *np2, *np = pdev->dev.of_node;
  89. struct mdio_mux_mmioreg_state *s;
  90. struct resource res;
  91. const __be32 *iprop;
  92. int len, ret;
  93. dev_dbg(&pdev->dev, "probing node %s\n", np->full_name);
  94. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  95. if (!s)
  96. return -ENOMEM;
  97. ret = of_address_to_resource(np, 0, &res);
  98. if (ret) {
  99. dev_err(&pdev->dev, "could not obtain memory map for node %s\n",
  100. np->full_name);
  101. return ret;
  102. }
  103. s->phys = res.start;
  104. s->iosize = resource_size(&res);
  105. if (s->iosize != sizeof(uint8_t) &&
  106. s->iosize != sizeof(uint16_t) &&
  107. s->iosize != sizeof(uint32_t)) {
  108. dev_err(&pdev->dev, "only 8/16/32-bit registers are supported\n");
  109. return -EINVAL;
  110. }
  111. iprop = of_get_property(np, "mux-mask", &len);
  112. if (!iprop || len != sizeof(uint32_t)) {
  113. dev_err(&pdev->dev, "missing or invalid mux-mask property\n");
  114. return -ENODEV;
  115. }
  116. if (be32_to_cpup(iprop) >= BIT(s->iosize * 8)) {
  117. dev_err(&pdev->dev, "only 8/16/32-bit registers are supported\n");
  118. return -EINVAL;
  119. }
  120. s->mask = be32_to_cpup(iprop);
  121. /*
  122. * Verify that the 'reg' property of each child MDIO bus does not
  123. * set any bits outside of the 'mask'.
  124. */
  125. for_each_available_child_of_node(np, np2) {
  126. iprop = of_get_property(np2, "reg", &len);
  127. if (!iprop || len != sizeof(uint32_t)) {
  128. dev_err(&pdev->dev, "mdio-mux child node %s is "
  129. "missing a 'reg' property\n", np2->full_name);
  130. of_node_put(np2);
  131. return -ENODEV;
  132. }
  133. if (be32_to_cpup(iprop) & ~s->mask) {
  134. dev_err(&pdev->dev, "mdio-mux child node %s has "
  135. "a 'reg' value with unmasked bits\n",
  136. np2->full_name);
  137. of_node_put(np2);
  138. return -ENODEV;
  139. }
  140. }
  141. ret = mdio_mux_init(&pdev->dev, mdio_mux_mmioreg_switch_fn,
  142. &s->mux_handle, s, NULL);
  143. if (ret) {
  144. dev_err(&pdev->dev, "failed to register mdio-mux bus %s\n",
  145. np->full_name);
  146. return ret;
  147. }
  148. pdev->dev.platform_data = s;
  149. return 0;
  150. }
  151. static int mdio_mux_mmioreg_remove(struct platform_device *pdev)
  152. {
  153. struct mdio_mux_mmioreg_state *s = dev_get_platdata(&pdev->dev);
  154. mdio_mux_uninit(s->mux_handle);
  155. return 0;
  156. }
  157. static const struct of_device_id mdio_mux_mmioreg_match[] = {
  158. {
  159. .compatible = "mdio-mux-mmioreg",
  160. },
  161. {},
  162. };
  163. MODULE_DEVICE_TABLE(of, mdio_mux_mmioreg_match);
  164. static struct platform_driver mdio_mux_mmioreg_driver = {
  165. .driver = {
  166. .name = "mdio-mux-mmioreg",
  167. .of_match_table = mdio_mux_mmioreg_match,
  168. },
  169. .probe = mdio_mux_mmioreg_probe,
  170. .remove = mdio_mux_mmioreg_remove,
  171. };
  172. module_platform_driver(mdio_mux_mmioreg_driver);
  173. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  174. MODULE_DESCRIPTION("Memory-mapped device MDIO MUX driver");
  175. MODULE_LICENSE("GPL v2");