mdio-mux-mmioreg.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. uint8_t mask;
  23. };
  24. /*
  25. * MDIO multiplexing switch function
  26. *
  27. * This function is called by the mdio-mux layer when it thinks the mdio bus
  28. * multiplexer needs to switch.
  29. *
  30. * 'current_child' is the current value of the mux register (masked via
  31. * s->mask).
  32. *
  33. * 'desired_child' is the value of the 'reg' property of the target child MDIO
  34. * node.
  35. *
  36. * The first time this function is called, current_child == -1.
  37. *
  38. * If current_child == desired_child, then the mux is already set to the
  39. * correct bus.
  40. */
  41. static int mdio_mux_mmioreg_switch_fn(int current_child, int desired_child,
  42. void *data)
  43. {
  44. struct mdio_mux_mmioreg_state *s = data;
  45. if (current_child ^ desired_child) {
  46. void __iomem *p = ioremap(s->phys, 1);
  47. uint8_t x, y;
  48. if (!p)
  49. return -ENOMEM;
  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. iounmap(p);
  57. }
  58. return 0;
  59. }
  60. static int mdio_mux_mmioreg_probe(struct platform_device *pdev)
  61. {
  62. struct device_node *np2, *np = pdev->dev.of_node;
  63. struct mdio_mux_mmioreg_state *s;
  64. struct resource res;
  65. const __be32 *iprop;
  66. int len, ret;
  67. dev_dbg(&pdev->dev, "probing node %s\n", np->full_name);
  68. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  69. if (!s)
  70. return -ENOMEM;
  71. ret = of_address_to_resource(np, 0, &res);
  72. if (ret) {
  73. dev_err(&pdev->dev, "could not obtain memory map for node %s\n",
  74. np->full_name);
  75. return ret;
  76. }
  77. s->phys = res.start;
  78. if (resource_size(&res) != sizeof(uint8_t)) {
  79. dev_err(&pdev->dev, "only 8-bit registers are supported\n");
  80. return -EINVAL;
  81. }
  82. iprop = of_get_property(np, "mux-mask", &len);
  83. if (!iprop || len != sizeof(uint32_t)) {
  84. dev_err(&pdev->dev, "missing or invalid mux-mask property\n");
  85. return -ENODEV;
  86. }
  87. if (be32_to_cpup(iprop) > 255) {
  88. dev_err(&pdev->dev, "only 8-bit registers are supported\n");
  89. return -EINVAL;
  90. }
  91. s->mask = be32_to_cpup(iprop);
  92. /*
  93. * Verify that the 'reg' property of each child MDIO bus does not
  94. * set any bits outside of the 'mask'.
  95. */
  96. for_each_available_child_of_node(np, np2) {
  97. iprop = of_get_property(np2, "reg", &len);
  98. if (!iprop || len != sizeof(uint32_t)) {
  99. dev_err(&pdev->dev, "mdio-mux child node %s is "
  100. "missing a 'reg' property\n", np2->full_name);
  101. of_node_put(np2);
  102. return -ENODEV;
  103. }
  104. if (be32_to_cpup(iprop) & ~s->mask) {
  105. dev_err(&pdev->dev, "mdio-mux child node %s has "
  106. "a 'reg' value with unmasked bits\n",
  107. np2->full_name);
  108. of_node_put(np2);
  109. return -ENODEV;
  110. }
  111. }
  112. ret = mdio_mux_init(&pdev->dev, mdio_mux_mmioreg_switch_fn,
  113. &s->mux_handle, s);
  114. if (ret) {
  115. dev_err(&pdev->dev, "failed to register mdio-mux bus %s\n",
  116. np->full_name);
  117. return ret;
  118. }
  119. pdev->dev.platform_data = s;
  120. return 0;
  121. }
  122. static int mdio_mux_mmioreg_remove(struct platform_device *pdev)
  123. {
  124. struct mdio_mux_mmioreg_state *s = dev_get_platdata(&pdev->dev);
  125. mdio_mux_uninit(s->mux_handle);
  126. return 0;
  127. }
  128. static const struct of_device_id mdio_mux_mmioreg_match[] = {
  129. {
  130. .compatible = "mdio-mux-mmioreg",
  131. },
  132. {},
  133. };
  134. MODULE_DEVICE_TABLE(of, mdio_mux_mmioreg_match);
  135. static struct platform_driver mdio_mux_mmioreg_driver = {
  136. .driver = {
  137. .name = "mdio-mux-mmioreg",
  138. .of_match_table = mdio_mux_mmioreg_match,
  139. },
  140. .probe = mdio_mux_mmioreg_probe,
  141. .remove = mdio_mux_mmioreg_remove,
  142. };
  143. module_platform_driver(mdio_mux_mmioreg_driver);
  144. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  145. MODULE_DESCRIPTION("Memory-mapped device MDIO MUX driver");
  146. MODULE_LICENSE("GPL v2");