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