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/init.h>
  18. #include <linux/phy.h>
  19. #include <linux/mdio-mux.h>
  20. struct mdio_mux_mmioreg_state {
  21. void *mux_handle;
  22. phys_addr_t phys;
  23. uint8_t 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 *p = ioremap(s->phys, 1);
  48. uint8_t x, y;
  49. if (!p)
  50. return -ENOMEM;
  51. x = ioread8(p);
  52. y = (x & ~s->mask) | desired_child;
  53. if (x != y) {
  54. iowrite8((x & ~s->mask) | desired_child, p);
  55. pr_debug("%s: %02x -> %02x\n", __func__, x, y);
  56. }
  57. iounmap(p);
  58. }
  59. return 0;
  60. }
  61. static int mdio_mux_mmioreg_probe(struct platform_device *pdev)
  62. {
  63. struct device_node *np2, *np = pdev->dev.of_node;
  64. struct mdio_mux_mmioreg_state *s;
  65. struct resource res;
  66. const __be32 *iprop;
  67. int len, ret;
  68. dev_dbg(&pdev->dev, "probing node %s\n", np->full_name);
  69. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  70. if (!s)
  71. return -ENOMEM;
  72. ret = of_address_to_resource(np, 0, &res);
  73. if (ret) {
  74. dev_err(&pdev->dev, "could not obtain memory map for node %s\n",
  75. np->full_name);
  76. return ret;
  77. }
  78. s->phys = res.start;
  79. if (resource_size(&res) != sizeof(uint8_t)) {
  80. dev_err(&pdev->dev, "only 8-bit registers are supported\n");
  81. return -EINVAL;
  82. }
  83. iprop = of_get_property(np, "mux-mask", &len);
  84. if (!iprop || len != sizeof(uint32_t)) {
  85. dev_err(&pdev->dev, "missing or invalid mux-mask property\n");
  86. return -ENODEV;
  87. }
  88. if (be32_to_cpup(iprop) > 255) {
  89. dev_err(&pdev->dev, "only 8-bit registers are supported\n");
  90. return -EINVAL;
  91. }
  92. s->mask = be32_to_cpup(iprop);
  93. /*
  94. * Verify that the 'reg' property of each child MDIO bus does not
  95. * set any bits outside of the 'mask'.
  96. */
  97. for_each_available_child_of_node(np, np2) {
  98. iprop = of_get_property(np2, "reg", &len);
  99. if (!iprop || len != sizeof(uint32_t)) {
  100. dev_err(&pdev->dev, "mdio-mux child node %s is "
  101. "missing a 'reg' property\n", np2->full_name);
  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. return -ENODEV;
  109. }
  110. }
  111. ret = mdio_mux_init(&pdev->dev, mdio_mux_mmioreg_switch_fn,
  112. &s->mux_handle, s);
  113. if (ret) {
  114. dev_err(&pdev->dev, "failed to register mdio-mux bus %s\n",
  115. np->full_name);
  116. return ret;
  117. }
  118. pdev->dev.platform_data = s;
  119. return 0;
  120. }
  121. static int mdio_mux_mmioreg_remove(struct platform_device *pdev)
  122. {
  123. struct mdio_mux_mmioreg_state *s = dev_get_platdata(&pdev->dev);
  124. mdio_mux_uninit(s->mux_handle);
  125. return 0;
  126. }
  127. static struct of_device_id mdio_mux_mmioreg_match[] = {
  128. {
  129. .compatible = "mdio-mux-mmioreg",
  130. },
  131. {},
  132. };
  133. MODULE_DEVICE_TABLE(of, mdio_mux_mmioreg_match);
  134. static struct platform_driver mdio_mux_mmioreg_driver = {
  135. .driver = {
  136. .name = "mdio-mux-mmioreg",
  137. .owner = THIS_MODULE,
  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");