mdio-mux-mmioreg.c 4.7 KB

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