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