xref: /linux/drivers/net/mdio/mdio-mux-mmioreg.c (revision a9770eac511ad82390b9f4a3c1728e078c387ac7)
1*a9770eacSAndrew Lunn // SPDX-License-Identifier: GPL-2.0
2*a9770eacSAndrew Lunn /*
3*a9770eacSAndrew Lunn  * Simple memory-mapped device MDIO MUX driver
4*a9770eacSAndrew Lunn  *
5*a9770eacSAndrew Lunn  * Author: Timur Tabi <timur@freescale.com>
6*a9770eacSAndrew Lunn  *
7*a9770eacSAndrew Lunn  * Copyright 2012 Freescale Semiconductor, Inc.
8*a9770eacSAndrew Lunn  */
9*a9770eacSAndrew Lunn 
10*a9770eacSAndrew Lunn #include <linux/platform_device.h>
11*a9770eacSAndrew Lunn #include <linux/device.h>
12*a9770eacSAndrew Lunn #include <linux/of_address.h>
13*a9770eacSAndrew Lunn #include <linux/of_mdio.h>
14*a9770eacSAndrew Lunn #include <linux/module.h>
15*a9770eacSAndrew Lunn #include <linux/phy.h>
16*a9770eacSAndrew Lunn #include <linux/mdio-mux.h>
17*a9770eacSAndrew Lunn 
18*a9770eacSAndrew Lunn struct mdio_mux_mmioreg_state {
19*a9770eacSAndrew Lunn 	void *mux_handle;
20*a9770eacSAndrew Lunn 	phys_addr_t phys;
21*a9770eacSAndrew Lunn 	unsigned int iosize;
22*a9770eacSAndrew Lunn 	unsigned int mask;
23*a9770eacSAndrew Lunn };
24*a9770eacSAndrew Lunn 
25*a9770eacSAndrew Lunn /*
26*a9770eacSAndrew Lunn  * MDIO multiplexing switch function
27*a9770eacSAndrew Lunn  *
28*a9770eacSAndrew Lunn  * This function is called by the mdio-mux layer when it thinks the mdio bus
29*a9770eacSAndrew Lunn  * multiplexer needs to switch.
30*a9770eacSAndrew Lunn  *
31*a9770eacSAndrew Lunn  * 'current_child' is the current value of the mux register (masked via
32*a9770eacSAndrew Lunn  * s->mask).
33*a9770eacSAndrew Lunn  *
34*a9770eacSAndrew Lunn  * 'desired_child' is the value of the 'reg' property of the target child MDIO
35*a9770eacSAndrew Lunn  * node.
36*a9770eacSAndrew Lunn  *
37*a9770eacSAndrew Lunn  * The first time this function is called, current_child == -1.
38*a9770eacSAndrew Lunn  *
39*a9770eacSAndrew Lunn  * If current_child == desired_child, then the mux is already set to the
40*a9770eacSAndrew Lunn  * correct bus.
41*a9770eacSAndrew Lunn  */
42*a9770eacSAndrew Lunn static int mdio_mux_mmioreg_switch_fn(int current_child, int desired_child,
43*a9770eacSAndrew Lunn 				      void *data)
44*a9770eacSAndrew Lunn {
45*a9770eacSAndrew Lunn 	struct mdio_mux_mmioreg_state *s = data;
46*a9770eacSAndrew Lunn 
47*a9770eacSAndrew Lunn 	if (current_child ^ desired_child) {
48*a9770eacSAndrew Lunn 		void __iomem *p = ioremap(s->phys, s->iosize);
49*a9770eacSAndrew Lunn 		if (!p)
50*a9770eacSAndrew Lunn 			return -ENOMEM;
51*a9770eacSAndrew Lunn 
52*a9770eacSAndrew Lunn 		switch (s->iosize) {
53*a9770eacSAndrew Lunn 		case sizeof(uint8_t): {
54*a9770eacSAndrew Lunn 			uint8_t x, y;
55*a9770eacSAndrew Lunn 
56*a9770eacSAndrew Lunn 			x = ioread8(p);
57*a9770eacSAndrew Lunn 			y = (x & ~s->mask) | desired_child;
58*a9770eacSAndrew Lunn 			if (x != y) {
59*a9770eacSAndrew Lunn 				iowrite8((x & ~s->mask) | desired_child, p);
60*a9770eacSAndrew Lunn 				pr_debug("%s: %02x -> %02x\n", __func__, x, y);
61*a9770eacSAndrew Lunn 			}
62*a9770eacSAndrew Lunn 
63*a9770eacSAndrew Lunn 			break;
64*a9770eacSAndrew Lunn 		}
65*a9770eacSAndrew Lunn 		case sizeof(uint16_t): {
66*a9770eacSAndrew Lunn 			uint16_t x, y;
67*a9770eacSAndrew Lunn 
68*a9770eacSAndrew Lunn 			x = ioread16(p);
69*a9770eacSAndrew Lunn 			y = (x & ~s->mask) | desired_child;
70*a9770eacSAndrew Lunn 			if (x != y) {
71*a9770eacSAndrew Lunn 				iowrite16((x & ~s->mask) | desired_child, p);
72*a9770eacSAndrew Lunn 				pr_debug("%s: %04x -> %04x\n", __func__, x, y);
73*a9770eacSAndrew Lunn 			}
74*a9770eacSAndrew Lunn 
75*a9770eacSAndrew Lunn 			break;
76*a9770eacSAndrew Lunn 		}
77*a9770eacSAndrew Lunn 		case sizeof(uint32_t): {
78*a9770eacSAndrew Lunn 			uint32_t x, y;
79*a9770eacSAndrew Lunn 
80*a9770eacSAndrew Lunn 			x = ioread32(p);
81*a9770eacSAndrew Lunn 			y = (x & ~s->mask) | desired_child;
82*a9770eacSAndrew Lunn 			if (x != y) {
83*a9770eacSAndrew Lunn 				iowrite32((x & ~s->mask) | desired_child, p);
84*a9770eacSAndrew Lunn 				pr_debug("%s: %08x -> %08x\n", __func__, x, y);
85*a9770eacSAndrew Lunn 			}
86*a9770eacSAndrew Lunn 
87*a9770eacSAndrew Lunn 			break;
88*a9770eacSAndrew Lunn 		}
89*a9770eacSAndrew Lunn 		}
90*a9770eacSAndrew Lunn 
91*a9770eacSAndrew Lunn 		iounmap(p);
92*a9770eacSAndrew Lunn 	}
93*a9770eacSAndrew Lunn 
94*a9770eacSAndrew Lunn 	return 0;
95*a9770eacSAndrew Lunn }
96*a9770eacSAndrew Lunn 
97*a9770eacSAndrew Lunn static int mdio_mux_mmioreg_probe(struct platform_device *pdev)
98*a9770eacSAndrew Lunn {
99*a9770eacSAndrew Lunn 	struct device_node *np2, *np = pdev->dev.of_node;
100*a9770eacSAndrew Lunn 	struct mdio_mux_mmioreg_state *s;
101*a9770eacSAndrew Lunn 	struct resource res;
102*a9770eacSAndrew Lunn 	const __be32 *iprop;
103*a9770eacSAndrew Lunn 	int len, ret;
104*a9770eacSAndrew Lunn 
105*a9770eacSAndrew Lunn 	dev_dbg(&pdev->dev, "probing node %pOF\n", np);
106*a9770eacSAndrew Lunn 
107*a9770eacSAndrew Lunn 	s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
108*a9770eacSAndrew Lunn 	if (!s)
109*a9770eacSAndrew Lunn 		return -ENOMEM;
110*a9770eacSAndrew Lunn 
111*a9770eacSAndrew Lunn 	ret = of_address_to_resource(np, 0, &res);
112*a9770eacSAndrew Lunn 	if (ret) {
113*a9770eacSAndrew Lunn 		dev_err(&pdev->dev, "could not obtain memory map for node %pOF\n",
114*a9770eacSAndrew Lunn 			np);
115*a9770eacSAndrew Lunn 		return ret;
116*a9770eacSAndrew Lunn 	}
117*a9770eacSAndrew Lunn 	s->phys = res.start;
118*a9770eacSAndrew Lunn 
119*a9770eacSAndrew Lunn 	s->iosize = resource_size(&res);
120*a9770eacSAndrew Lunn 	if (s->iosize != sizeof(uint8_t) &&
121*a9770eacSAndrew Lunn 	    s->iosize != sizeof(uint16_t) &&
122*a9770eacSAndrew Lunn 	    s->iosize != sizeof(uint32_t)) {
123*a9770eacSAndrew Lunn 		dev_err(&pdev->dev, "only 8/16/32-bit registers are supported\n");
124*a9770eacSAndrew Lunn 		return -EINVAL;
125*a9770eacSAndrew Lunn 	}
126*a9770eacSAndrew Lunn 
127*a9770eacSAndrew Lunn 	iprop = of_get_property(np, "mux-mask", &len);
128*a9770eacSAndrew Lunn 	if (!iprop || len != sizeof(uint32_t)) {
129*a9770eacSAndrew Lunn 		dev_err(&pdev->dev, "missing or invalid mux-mask property\n");
130*a9770eacSAndrew Lunn 		return -ENODEV;
131*a9770eacSAndrew Lunn 	}
132*a9770eacSAndrew Lunn 	if (be32_to_cpup(iprop) >= BIT(s->iosize * 8)) {
133*a9770eacSAndrew Lunn 		dev_err(&pdev->dev, "only 8/16/32-bit registers are supported\n");
134*a9770eacSAndrew Lunn 		return -EINVAL;
135*a9770eacSAndrew Lunn 	}
136*a9770eacSAndrew Lunn 	s->mask = be32_to_cpup(iprop);
137*a9770eacSAndrew Lunn 
138*a9770eacSAndrew Lunn 	/*
139*a9770eacSAndrew Lunn 	 * Verify that the 'reg' property of each child MDIO bus does not
140*a9770eacSAndrew Lunn 	 * set any bits outside of the 'mask'.
141*a9770eacSAndrew Lunn 	 */
142*a9770eacSAndrew Lunn 	for_each_available_child_of_node(np, np2) {
143*a9770eacSAndrew Lunn 		iprop = of_get_property(np2, "reg", &len);
144*a9770eacSAndrew Lunn 		if (!iprop || len != sizeof(uint32_t)) {
145*a9770eacSAndrew Lunn 			dev_err(&pdev->dev, "mdio-mux child node %pOF is "
146*a9770eacSAndrew Lunn 				"missing a 'reg' property\n", np2);
147*a9770eacSAndrew Lunn 			of_node_put(np2);
148*a9770eacSAndrew Lunn 			return -ENODEV;
149*a9770eacSAndrew Lunn 		}
150*a9770eacSAndrew Lunn 		if (be32_to_cpup(iprop) & ~s->mask) {
151*a9770eacSAndrew Lunn 			dev_err(&pdev->dev, "mdio-mux child node %pOF has "
152*a9770eacSAndrew Lunn 				"a 'reg' value with unmasked bits\n",
153*a9770eacSAndrew Lunn 				np2);
154*a9770eacSAndrew Lunn 			of_node_put(np2);
155*a9770eacSAndrew Lunn 			return -ENODEV;
156*a9770eacSAndrew Lunn 		}
157*a9770eacSAndrew Lunn 	}
158*a9770eacSAndrew Lunn 
159*a9770eacSAndrew Lunn 	ret = mdio_mux_init(&pdev->dev, pdev->dev.of_node,
160*a9770eacSAndrew Lunn 			    mdio_mux_mmioreg_switch_fn,
161*a9770eacSAndrew Lunn 			    &s->mux_handle, s, NULL);
162*a9770eacSAndrew Lunn 	if (ret) {
163*a9770eacSAndrew Lunn 		if (ret != -EPROBE_DEFER)
164*a9770eacSAndrew Lunn 			dev_err(&pdev->dev,
165*a9770eacSAndrew Lunn 				"failed to register mdio-mux bus %pOF\n", np);
166*a9770eacSAndrew Lunn 		return ret;
167*a9770eacSAndrew Lunn 	}
168*a9770eacSAndrew Lunn 
169*a9770eacSAndrew Lunn 	pdev->dev.platform_data = s;
170*a9770eacSAndrew Lunn 
171*a9770eacSAndrew Lunn 	return 0;
172*a9770eacSAndrew Lunn }
173*a9770eacSAndrew Lunn 
174*a9770eacSAndrew Lunn static int mdio_mux_mmioreg_remove(struct platform_device *pdev)
175*a9770eacSAndrew Lunn {
176*a9770eacSAndrew Lunn 	struct mdio_mux_mmioreg_state *s = dev_get_platdata(&pdev->dev);
177*a9770eacSAndrew Lunn 
178*a9770eacSAndrew Lunn 	mdio_mux_uninit(s->mux_handle);
179*a9770eacSAndrew Lunn 
180*a9770eacSAndrew Lunn 	return 0;
181*a9770eacSAndrew Lunn }
182*a9770eacSAndrew Lunn 
183*a9770eacSAndrew Lunn static const struct of_device_id mdio_mux_mmioreg_match[] = {
184*a9770eacSAndrew Lunn 	{
185*a9770eacSAndrew Lunn 		.compatible = "mdio-mux-mmioreg",
186*a9770eacSAndrew Lunn 	},
187*a9770eacSAndrew Lunn 	{},
188*a9770eacSAndrew Lunn };
189*a9770eacSAndrew Lunn MODULE_DEVICE_TABLE(of, mdio_mux_mmioreg_match);
190*a9770eacSAndrew Lunn 
191*a9770eacSAndrew Lunn static struct platform_driver mdio_mux_mmioreg_driver = {
192*a9770eacSAndrew Lunn 	.driver = {
193*a9770eacSAndrew Lunn 		.name		= "mdio-mux-mmioreg",
194*a9770eacSAndrew Lunn 		.of_match_table = mdio_mux_mmioreg_match,
195*a9770eacSAndrew Lunn 	},
196*a9770eacSAndrew Lunn 	.probe		= mdio_mux_mmioreg_probe,
197*a9770eacSAndrew Lunn 	.remove		= mdio_mux_mmioreg_remove,
198*a9770eacSAndrew Lunn };
199*a9770eacSAndrew Lunn 
200*a9770eacSAndrew Lunn module_platform_driver(mdio_mux_mmioreg_driver);
201*a9770eacSAndrew Lunn 
202*a9770eacSAndrew Lunn MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
203*a9770eacSAndrew Lunn MODULE_DESCRIPTION("Memory-mapped device MDIO MUX driver");
204*a9770eacSAndrew Lunn MODULE_LICENSE("GPL v2");
205