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