blob: 70f6115530af8c800d79660d3fc8aec2e56155f0 [file] [log] [blame]
Timur Tabi6cc2ff82012-08-24 09:10:53 +00001/*
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>
Timur Tabi16fa9e12012-09-07 16:30:57 -050015#include <linux/of_address.h>
Timur Tabi6cc2ff82012-08-24 09:10:53 +000016#include <linux/of_mdio.h>
17#include <linux/module.h>
Timur Tabi6cc2ff82012-08-24 09:10:53 +000018#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;
Neil Armstrong9a4c8032016-11-04 16:51:22 +010024 unsigned int iosize;
25 unsigned int mask;
Timur Tabi6cc2ff82012-08-24 09:10:53 +000026};
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 */
45static 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) {
Neil Armstrong9a4c8032016-11-04 16:51:22 +010051 void __iomem *p = ioremap(s->phys, s->iosize);
Timur Tabi6cc2ff82012-08-24 09:10:53 +000052 if (!p)
53 return -ENOMEM;
54
Neil Armstrong9a4c8032016-11-04 16:51:22 +010055 switch (s->iosize) {
56 case sizeof(uint8_t): {
57 uint8_t x, y;
58
59 x = ioread8(p);
60 y = (x & ~s->mask) | desired_child;
61 if (x != y) {
62 iowrite8((x & ~s->mask) | desired_child, p);
63 pr_debug("%s: %02x -> %02x\n", __func__, x, y);
64 }
65
66 break;
67 }
68 case sizeof(uint16_t): {
69 uint16_t x, y;
70
71 x = ioread16(p);
72 y = (x & ~s->mask) | desired_child;
73 if (x != y) {
74 iowrite16((x & ~s->mask) | desired_child, p);
75 pr_debug("%s: %04x -> %04x\n", __func__, x, y);
76 }
77
78 break;
79 }
80 case sizeof(uint32_t): {
81 uint32_t x, y;
82
83 x = ioread32(p);
84 y = (x & ~s->mask) | desired_child;
85 if (x != y) {
86 iowrite32((x & ~s->mask) | desired_child, p);
87 pr_debug("%s: %08x -> %08x\n", __func__, x, y);
88 }
89
90 break;
91 }
Timur Tabi6cc2ff82012-08-24 09:10:53 +000092 }
93
94 iounmap(p);
95 }
96
97 return 0;
98}
99
Bill Pemberton633d1592012-12-03 09:24:14 -0500100static int mdio_mux_mmioreg_probe(struct platform_device *pdev)
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000101{
102 struct device_node *np2, *np = pdev->dev.of_node;
103 struct mdio_mux_mmioreg_state *s;
104 struct resource res;
105 const __be32 *iprop;
106 int len, ret;
107
Rob Herringf7ce9102017-07-18 16:43:19 -0500108 dev_dbg(&pdev->dev, "probing node %pOF\n", np);
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000109
110 s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
111 if (!s)
112 return -ENOMEM;
113
114 ret = of_address_to_resource(np, 0, &res);
115 if (ret) {
Rob Herringf7ce9102017-07-18 16:43:19 -0500116 dev_err(&pdev->dev, "could not obtain memory map for node %pOF\n",
117 np);
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000118 return ret;
119 }
120 s->phys = res.start;
121
Neil Armstrong9a4c8032016-11-04 16:51:22 +0100122 s->iosize = resource_size(&res);
123 if (s->iosize != sizeof(uint8_t) &&
124 s->iosize != sizeof(uint16_t) &&
125 s->iosize != sizeof(uint32_t)) {
126 dev_err(&pdev->dev, "only 8/16/32-bit registers are supported\n");
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000127 return -EINVAL;
128 }
129
130 iprop = of_get_property(np, "mux-mask", &len);
131 if (!iprop || len != sizeof(uint32_t)) {
132 dev_err(&pdev->dev, "missing or invalid mux-mask property\n");
133 return -ENODEV;
134 }
Neil Armstrong9a4c8032016-11-04 16:51:22 +0100135 if (be32_to_cpup(iprop) >= BIT(s->iosize * 8)) {
136 dev_err(&pdev->dev, "only 8/16/32-bit registers are supported\n");
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000137 return -EINVAL;
138 }
139 s->mask = be32_to_cpup(iprop);
140
141 /*
142 * Verify that the 'reg' property of each child MDIO bus does not
143 * set any bits outside of the 'mask'.
144 */
145 for_each_available_child_of_node(np, np2) {
146 iprop = of_get_property(np2, "reg", &len);
147 if (!iprop || len != sizeof(uint32_t)) {
Rob Herringf7ce9102017-07-18 16:43:19 -0500148 dev_err(&pdev->dev, "mdio-mux child node %pOF is "
149 "missing a 'reg' property\n", np2);
Julia Lawall02862342015-10-25 14:57:03 +0100150 of_node_put(np2);
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000151 return -ENODEV;
152 }
153 if (be32_to_cpup(iprop) & ~s->mask) {
Rob Herringf7ce9102017-07-18 16:43:19 -0500154 dev_err(&pdev->dev, "mdio-mux child node %pOF has "
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000155 "a 'reg' value with unmasked bits\n",
Rob Herringf7ce9102017-07-18 16:43:19 -0500156 np2);
Julia Lawall02862342015-10-25 14:57:03 +0100157 of_node_put(np2);
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000158 return -ENODEV;
159 }
160 }
161
Corentin Labbe5482a972017-09-04 18:30:14 +0200162 ret = mdio_mux_init(&pdev->dev, pdev->dev.of_node,
163 mdio_mux_mmioreg_switch_fn,
Pramod Kumarf20e6652016-06-10 11:03:45 +0530164 &s->mux_handle, s, NULL);
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000165 if (ret) {
Jerome Brunet630b21a2018-03-06 12:10:45 +0100166 if (ret != -EPROBE_DEFER)
167 dev_err(&pdev->dev,
168 "failed to register mdio-mux bus %pOF\n", np);
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000169 return ret;
170 }
171
172 pdev->dev.platform_data = s;
173
174 return 0;
175}
176
Bill Pemberton633d1592012-12-03 09:24:14 -0500177static int mdio_mux_mmioreg_remove(struct platform_device *pdev)
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000178{
179 struct mdio_mux_mmioreg_state *s = dev_get_platdata(&pdev->dev);
180
181 mdio_mux_uninit(s->mux_handle);
182
183 return 0;
184}
185
Fabian Frederickd8a7dad2015-03-17 19:40:23 +0100186static const struct of_device_id mdio_mux_mmioreg_match[] = {
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000187 {
188 .compatible = "mdio-mux-mmioreg",
189 },
190 {},
191};
192MODULE_DEVICE_TABLE(of, mdio_mux_mmioreg_match);
193
194static struct platform_driver mdio_mux_mmioreg_driver = {
195 .driver = {
196 .name = "mdio-mux-mmioreg",
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000197 .of_match_table = mdio_mux_mmioreg_match,
198 },
199 .probe = mdio_mux_mmioreg_probe,
Bill Pemberton633d1592012-12-03 09:24:14 -0500200 .remove = mdio_mux_mmioreg_remove,
Timur Tabi6cc2ff82012-08-24 09:10:53 +0000201};
202
203module_platform_driver(mdio_mux_mmioreg_driver);
204
205MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
206MODULE_DESCRIPTION("Memory-mapped device MDIO MUX driver");
207MODULE_LICENSE("GPL v2");