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>
111bf34366SCalvin Johnson #include <linux/mdio-mux.h>
121bf34366SCalvin 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>
161bf34366SCalvin 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 */
mdio_mux_mmioreg_switch_fn(int current_child,int desired_child,void * data)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
mdio_mux_mmioreg_probe(struct platform_device * pdev)97a9770eacSAndrew Lunn static int mdio_mux_mmioreg_probe(struct platform_device *pdev)
98a9770eacSAndrew Lunn {
99b00f7f4fSJinjie Ruan struct device_node *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);
112*4078513fSJinjie Ruan if (ret)
113*4078513fSJinjie Ruan return dev_err_probe(&pdev->dev, ret,
114*4078513fSJinjie Ruan "could not obtain memory map for node %pOF\n", np);
115a9770eacSAndrew Lunn s->phys = res.start;
116a9770eacSAndrew Lunn
117a9770eacSAndrew Lunn s->iosize = resource_size(&res);
118a9770eacSAndrew Lunn if (s->iosize != sizeof(uint8_t) &&
119a9770eacSAndrew Lunn s->iosize != sizeof(uint16_t) &&
120*4078513fSJinjie Ruan s->iosize != sizeof(uint32_t))
121*4078513fSJinjie Ruan return dev_err_probe(&pdev->dev, -EINVAL,
122*4078513fSJinjie Ruan "only 8/16/32-bit registers are supported\n");
123a9770eacSAndrew Lunn
124a9770eacSAndrew Lunn iprop = of_get_property(np, "mux-mask", &len);
125*4078513fSJinjie Ruan if (!iprop || len != sizeof(uint32_t))
126*4078513fSJinjie Ruan return dev_err_probe(&pdev->dev, -ENODEV,
127*4078513fSJinjie Ruan "missing or invalid mux-mask property\n");
128*4078513fSJinjie Ruan if (be32_to_cpup(iprop) >= BIT(s->iosize * 8))
129*4078513fSJinjie Ruan return dev_err_probe(&pdev->dev, -EINVAL,
130*4078513fSJinjie Ruan "only 8/16/32-bit registers are supported\n");
131a9770eacSAndrew Lunn s->mask = be32_to_cpup(iprop);
132a9770eacSAndrew Lunn
133a9770eacSAndrew Lunn /*
134a9770eacSAndrew Lunn * Verify that the 'reg' property of each child MDIO bus does not
135a9770eacSAndrew Lunn * set any bits outside of the 'mask'.
136a9770eacSAndrew Lunn */
137b00f7f4fSJinjie Ruan for_each_available_child_of_node_scoped(np, np2) {
138b30a1f30SRob Herring u64 reg;
139b30a1f30SRob Herring
140*4078513fSJinjie Ruan if (of_property_read_reg(np2, 0, ®, NULL))
141*4078513fSJinjie Ruan return dev_err_probe(&pdev->dev, -ENODEV,
142*4078513fSJinjie Ruan "mdio-mux child node %pOF is missing a 'reg' property\n",
143a9770eacSAndrew Lunn np2);
144*4078513fSJinjie Ruan if ((u32)reg & ~s->mask)
145*4078513fSJinjie Ruan return dev_err_probe(&pdev->dev, -ENODEV,
146*4078513fSJinjie Ruan "mdio-mux child node %pOF has a 'reg' value with unmasked bits\n",
147*4078513fSJinjie Ruan np2);
148a9770eacSAndrew Lunn }
149a9770eacSAndrew Lunn
150a9770eacSAndrew Lunn ret = mdio_mux_init(&pdev->dev, pdev->dev.of_node,
151a9770eacSAndrew Lunn mdio_mux_mmioreg_switch_fn,
152a9770eacSAndrew Lunn &s->mux_handle, s, NULL);
153770aac8dSYang Yingliang if (ret)
154770aac8dSYang Yingliang return dev_err_probe(&pdev->dev, ret,
155a9770eacSAndrew Lunn "failed to register mdio-mux bus %pOF\n", np);
156a9770eacSAndrew Lunn
157a9770eacSAndrew Lunn pdev->dev.platform_data = s;
158a9770eacSAndrew Lunn
159a9770eacSAndrew Lunn return 0;
160a9770eacSAndrew Lunn }
161a9770eacSAndrew Lunn
mdio_mux_mmioreg_remove(struct platform_device * pdev)16270edec67SUwe Kleine-König static void mdio_mux_mmioreg_remove(struct platform_device *pdev)
163a9770eacSAndrew Lunn {
164a9770eacSAndrew Lunn struct mdio_mux_mmioreg_state *s = dev_get_platdata(&pdev->dev);
165a9770eacSAndrew Lunn
166a9770eacSAndrew Lunn mdio_mux_uninit(s->mux_handle);
167a9770eacSAndrew Lunn }
168a9770eacSAndrew Lunn
169a9770eacSAndrew Lunn static const struct of_device_id mdio_mux_mmioreg_match[] = {
170a9770eacSAndrew Lunn {
171a9770eacSAndrew Lunn .compatible = "mdio-mux-mmioreg",
172a9770eacSAndrew Lunn },
173a9770eacSAndrew Lunn {},
174a9770eacSAndrew Lunn };
175a9770eacSAndrew Lunn MODULE_DEVICE_TABLE(of, mdio_mux_mmioreg_match);
176a9770eacSAndrew Lunn
177a9770eacSAndrew Lunn static struct platform_driver mdio_mux_mmioreg_driver = {
178a9770eacSAndrew Lunn .driver = {
179a9770eacSAndrew Lunn .name = "mdio-mux-mmioreg",
180a9770eacSAndrew Lunn .of_match_table = mdio_mux_mmioreg_match,
181a9770eacSAndrew Lunn },
182a9770eacSAndrew Lunn .probe = mdio_mux_mmioreg_probe,
18370edec67SUwe Kleine-König .remove_new = mdio_mux_mmioreg_remove,
184a9770eacSAndrew Lunn };
185a9770eacSAndrew Lunn
186a9770eacSAndrew Lunn module_platform_driver(mdio_mux_mmioreg_driver);
187a9770eacSAndrew Lunn
188a9770eacSAndrew Lunn MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
189a9770eacSAndrew Lunn MODULE_DESCRIPTION("Memory-mapped device MDIO MUX driver");
190a9770eacSAndrew Lunn MODULE_LICENSE("GPL v2");
191