1*ba7319e9SDmitry Salychev /*-
2*ba7319e9SDmitry Salychev * SPDX-License-Identifier: BSD-2-Clause
3*ba7319e9SDmitry Salychev *
4*ba7319e9SDmitry Salychev * Copyright © 2021-2022 Bjoern A. Zeeb
5*ba7319e9SDmitry Salychev *
6*ba7319e9SDmitry Salychev * Redistribution and use in source and binary forms, with or without
7*ba7319e9SDmitry Salychev * modification, are permitted provided that the following conditions
8*ba7319e9SDmitry Salychev * are met:
9*ba7319e9SDmitry Salychev * 1. Redistributions of source code must retain the above copyright
10*ba7319e9SDmitry Salychev * notice, this list of conditions and the following disclaimer.
11*ba7319e9SDmitry Salychev * 2. Redistributions in binary form must reproduce the above copyright
12*ba7319e9SDmitry Salychev * notice, this list of conditions and the following disclaimer in the
13*ba7319e9SDmitry Salychev * documentation and/or other materials provided with the distribution.
14*ba7319e9SDmitry Salychev *
15*ba7319e9SDmitry Salychev * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*ba7319e9SDmitry Salychev * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*ba7319e9SDmitry Salychev * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*ba7319e9SDmitry Salychev * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*ba7319e9SDmitry Salychev * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*ba7319e9SDmitry Salychev * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*ba7319e9SDmitry Salychev * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*ba7319e9SDmitry Salychev * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*ba7319e9SDmitry Salychev * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*ba7319e9SDmitry Salychev * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*ba7319e9SDmitry Salychev * SUCH DAMAGE.
26*ba7319e9SDmitry Salychev */
27*ba7319e9SDmitry Salychev
28*ba7319e9SDmitry Salychev #include <sys/param.h>
29*ba7319e9SDmitry Salychev #include <sys/kernel.h>
30*ba7319e9SDmitry Salychev #include <sys/bus.h>
31*ba7319e9SDmitry Salychev #include <sys/rman.h>
32*ba7319e9SDmitry Salychev #include <sys/malloc.h>
33*ba7319e9SDmitry Salychev #include <sys/module.h>
34*ba7319e9SDmitry Salychev #include <sys/endian.h>
35*ba7319e9SDmitry Salychev #include <sys/socket.h>
36*ba7319e9SDmitry Salychev
37*ba7319e9SDmitry Salychev #include <machine/bus.h>
38*ba7319e9SDmitry Salychev #include <machine/resource.h>
39*ba7319e9SDmitry Salychev
40*ba7319e9SDmitry Salychev #include <dev/ofw/ofw_bus.h>
41*ba7319e9SDmitry Salychev #include <dev/ofw/ofw_bus_subr.h>
42*ba7319e9SDmitry Salychev #include <dev/fdt/simplebus.h>
43*ba7319e9SDmitry Salychev
44*ba7319e9SDmitry Salychev #include <net/if.h>
45*ba7319e9SDmitry Salychev #include <net/if_var.h>
46*ba7319e9SDmitry Salychev #include <net/if_media.h>
47*ba7319e9SDmitry Salychev
48*ba7319e9SDmitry Salychev #include <dev/mii/mii.h>
49*ba7319e9SDmitry Salychev #include <dev/mii/miivar.h>
50*ba7319e9SDmitry Salychev
51*ba7319e9SDmitry Salychev #include "memac_mdio.h"
52*ba7319e9SDmitry Salychev #include "memac_mdio_if.h"
53*ba7319e9SDmitry Salychev #include "ofw_bus_if.h"
54*ba7319e9SDmitry Salychev #include "miibus_if.h"
55*ba7319e9SDmitry Salychev
56*ba7319e9SDmitry Salychev /* -------------------------------------------------------------------------- */
57*ba7319e9SDmitry Salychev
58*ba7319e9SDmitry Salychev struct memacphy_softc_fdt {
59*ba7319e9SDmitry Salychev struct memacphy_softc_common scc;
60*ba7319e9SDmitry Salychev uint32_t reg;
61*ba7319e9SDmitry Salychev phandle_t xref;
62*ba7319e9SDmitry Salychev };
63*ba7319e9SDmitry Salychev
64*ba7319e9SDmitry Salychev static void
memacphy_fdt_miibus_statchg(device_t dev)65*ba7319e9SDmitry Salychev memacphy_fdt_miibus_statchg(device_t dev)
66*ba7319e9SDmitry Salychev {
67*ba7319e9SDmitry Salychev struct memacphy_softc_fdt *sc;
68*ba7319e9SDmitry Salychev
69*ba7319e9SDmitry Salychev sc = device_get_softc(dev);
70*ba7319e9SDmitry Salychev memacphy_miibus_statchg(&sc->scc);
71*ba7319e9SDmitry Salychev }
72*ba7319e9SDmitry Salychev
73*ba7319e9SDmitry Salychev static int
memacphy_fdt_set_ni_dev(device_t dev,device_t nidev)74*ba7319e9SDmitry Salychev memacphy_fdt_set_ni_dev(device_t dev, device_t nidev)
75*ba7319e9SDmitry Salychev {
76*ba7319e9SDmitry Salychev struct memacphy_softc_fdt *sc;
77*ba7319e9SDmitry Salychev
78*ba7319e9SDmitry Salychev sc = device_get_softc(dev);
79*ba7319e9SDmitry Salychev return (memacphy_set_ni_dev(&sc->scc, nidev));
80*ba7319e9SDmitry Salychev }
81*ba7319e9SDmitry Salychev
82*ba7319e9SDmitry Salychev static int
memacphy_fdt_get_phy_loc(device_t dev,int * phy_loc)83*ba7319e9SDmitry Salychev memacphy_fdt_get_phy_loc(device_t dev, int *phy_loc)
84*ba7319e9SDmitry Salychev {
85*ba7319e9SDmitry Salychev struct memacphy_softc_fdt *sc;
86*ba7319e9SDmitry Salychev
87*ba7319e9SDmitry Salychev sc = device_get_softc(dev);
88*ba7319e9SDmitry Salychev return (memacphy_get_phy_loc(&sc->scc, phy_loc));
89*ba7319e9SDmitry Salychev }
90*ba7319e9SDmitry Salychev
91*ba7319e9SDmitry Salychev static int
memacphy_fdt_probe(device_t dev)92*ba7319e9SDmitry Salychev memacphy_fdt_probe(device_t dev)
93*ba7319e9SDmitry Salychev {
94*ba7319e9SDmitry Salychev
95*ba7319e9SDmitry Salychev if (!ofw_bus_status_okay(dev))
96*ba7319e9SDmitry Salychev return (ENXIO);
97*ba7319e9SDmitry Salychev
98*ba7319e9SDmitry Salychev device_set_desc(dev, "MEMAC PHY (fdt)");
99*ba7319e9SDmitry Salychev return (BUS_PROBE_DEFAULT);
100*ba7319e9SDmitry Salychev }
101*ba7319e9SDmitry Salychev
102*ba7319e9SDmitry Salychev static int
memacphy_fdt_attach(device_t dev)103*ba7319e9SDmitry Salychev memacphy_fdt_attach(device_t dev)
104*ba7319e9SDmitry Salychev {
105*ba7319e9SDmitry Salychev struct memacphy_softc_fdt *sc;
106*ba7319e9SDmitry Salychev phandle_t node;
107*ba7319e9SDmitry Salychev ssize_t s;
108*ba7319e9SDmitry Salychev int error;
109*ba7319e9SDmitry Salychev
110*ba7319e9SDmitry Salychev sc = device_get_softc(dev);
111*ba7319e9SDmitry Salychev sc->scc.dev = dev;
112*ba7319e9SDmitry Salychev node = ofw_bus_get_node(dev);
113*ba7319e9SDmitry Salychev
114*ba7319e9SDmitry Salychev s = device_get_property(dev, "reg", &sc->reg, sizeof(sc->reg),
115*ba7319e9SDmitry Salychev DEVICE_PROP_UINT32);
116*ba7319e9SDmitry Salychev if (s != -1)
117*ba7319e9SDmitry Salychev sc->scc.phy = sc->reg;
118*ba7319e9SDmitry Salychev else
119*ba7319e9SDmitry Salychev sc->scc.phy = -1;
120*ba7319e9SDmitry Salychev sc->xref = OF_xref_from_node(node);
121*ba7319e9SDmitry Salychev
122*ba7319e9SDmitry Salychev error = OF_device_register_xref(sc->xref, dev);
123*ba7319e9SDmitry Salychev if (error != 0)
124*ba7319e9SDmitry Salychev device_printf(dev, "Failed to register xref %#x\n", sc->xref);
125*ba7319e9SDmitry Salychev
126*ba7319e9SDmitry Salychev if (bootverbose)
127*ba7319e9SDmitry Salychev device_printf(dev, "node %#x '%s': reg %#x xref %#x phy %u\n",
128*ba7319e9SDmitry Salychev node, ofw_bus_get_name(dev), sc->reg, sc->xref, sc->scc.phy);
129*ba7319e9SDmitry Salychev
130*ba7319e9SDmitry Salychev if (sc->scc.phy == -1)
131*ba7319e9SDmitry Salychev error = ENXIO;
132*ba7319e9SDmitry Salychev return (error);
133*ba7319e9SDmitry Salychev }
134*ba7319e9SDmitry Salychev
135*ba7319e9SDmitry Salychev static device_method_t memacphy_fdt_methods[] = {
136*ba7319e9SDmitry Salychev /* Device interface */
137*ba7319e9SDmitry Salychev DEVMETHOD(device_probe, memacphy_fdt_probe),
138*ba7319e9SDmitry Salychev DEVMETHOD(device_attach, memacphy_fdt_attach),
139*ba7319e9SDmitry Salychev DEVMETHOD(device_detach, bus_generic_detach),
140*ba7319e9SDmitry Salychev
141*ba7319e9SDmitry Salychev /* MII interface */
142*ba7319e9SDmitry Salychev DEVMETHOD(miibus_readreg, memacphy_miibus_readreg),
143*ba7319e9SDmitry Salychev DEVMETHOD(miibus_writereg, memacphy_miibus_writereg),
144*ba7319e9SDmitry Salychev DEVMETHOD(miibus_statchg, memacphy_fdt_miibus_statchg),
145*ba7319e9SDmitry Salychev
146*ba7319e9SDmitry Salychev /* memac */
147*ba7319e9SDmitry Salychev DEVMETHOD(memac_mdio_set_ni_dev, memacphy_fdt_set_ni_dev),
148*ba7319e9SDmitry Salychev DEVMETHOD(memac_mdio_get_phy_loc, memacphy_fdt_get_phy_loc),
149*ba7319e9SDmitry Salychev
150*ba7319e9SDmitry Salychev DEVMETHOD_END
151*ba7319e9SDmitry Salychev };
152*ba7319e9SDmitry Salychev
153*ba7319e9SDmitry Salychev DEFINE_CLASS_0(memacphy_fdt, memacphy_fdt_driver, memacphy_fdt_methods,
154*ba7319e9SDmitry Salychev sizeof(struct memacphy_softc_fdt));
155*ba7319e9SDmitry Salychev
156*ba7319e9SDmitry Salychev EARLY_DRIVER_MODULE(memacphy_fdt, memac_mdio_fdt, memacphy_fdt_driver, 0, 0,
157*ba7319e9SDmitry Salychev BUS_PASS_SUPPORTDEV);
158*ba7319e9SDmitry Salychev DRIVER_MODULE(miibus, memacphy_fdt, miibus_driver, 0, 0);
159*ba7319e9SDmitry Salychev MODULE_DEPEND(memacphy_fdt, miibus, 1, 1, 1);
160*ba7319e9SDmitry Salychev
161*ba7319e9SDmitry Salychev /* -------------------------------------------------------------------------- */
162*ba7319e9SDmitry Salychev
163*ba7319e9SDmitry Salychev /*
164*ba7319e9SDmitry Salychev * Order in this softc is important; memac_mdio_fdt_attach() calls
165*ba7319e9SDmitry Salychev * simplebus_init() which expects sb_sc at the beginning.
166*ba7319e9SDmitry Salychev */
167*ba7319e9SDmitry Salychev struct memac_mdio_softc_fdt {
168*ba7319e9SDmitry Salychev struct simplebus_softc sb_sc; /* Must stay first. */
169*ba7319e9SDmitry Salychev struct memac_mdio_softc_common scc;
170*ba7319e9SDmitry Salychev };
171*ba7319e9SDmitry Salychev
172*ba7319e9SDmitry Salychev static int
memac_fdt_miibus_readreg(device_t dev,int phy,int reg)173*ba7319e9SDmitry Salychev memac_fdt_miibus_readreg(device_t dev, int phy, int reg)
174*ba7319e9SDmitry Salychev {
175*ba7319e9SDmitry Salychev struct memac_mdio_softc_fdt *sc;
176*ba7319e9SDmitry Salychev
177*ba7319e9SDmitry Salychev sc = device_get_softc(dev);
178*ba7319e9SDmitry Salychev return (memac_miibus_readreg(&sc->scc, phy, reg));
179*ba7319e9SDmitry Salychev }
180*ba7319e9SDmitry Salychev
181*ba7319e9SDmitry Salychev static int
memac_fdt_miibus_writereg(device_t dev,int phy,int reg,int data)182*ba7319e9SDmitry Salychev memac_fdt_miibus_writereg(device_t dev, int phy, int reg, int data)
183*ba7319e9SDmitry Salychev {
184*ba7319e9SDmitry Salychev struct memac_mdio_softc_fdt *sc;
185*ba7319e9SDmitry Salychev
186*ba7319e9SDmitry Salychev sc = device_get_softc(dev);
187*ba7319e9SDmitry Salychev return (memac_miibus_writereg(&sc->scc, phy, reg, data));
188*ba7319e9SDmitry Salychev }
189*ba7319e9SDmitry Salychev
190*ba7319e9SDmitry Salychev static struct ofw_compat_data compat_data[] = {
191*ba7319e9SDmitry Salychev { "fsl,fman-memac-mdio", 1 },
192*ba7319e9SDmitry Salychev { NULL, 0 }
193*ba7319e9SDmitry Salychev };
194*ba7319e9SDmitry Salychev
195*ba7319e9SDmitry Salychev static int
memac_mdio_fdt_probe(device_t dev)196*ba7319e9SDmitry Salychev memac_mdio_fdt_probe(device_t dev)
197*ba7319e9SDmitry Salychev {
198*ba7319e9SDmitry Salychev
199*ba7319e9SDmitry Salychev if (!ofw_bus_status_okay(dev))
200*ba7319e9SDmitry Salychev return (ENXIO);
201*ba7319e9SDmitry Salychev
202*ba7319e9SDmitry Salychev if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
203*ba7319e9SDmitry Salychev return (ENXIO);
204*ba7319e9SDmitry Salychev
205*ba7319e9SDmitry Salychev device_set_desc(dev, "Freescale XGMAC MDIO Bus (FDT)");
206*ba7319e9SDmitry Salychev return (BUS_PROBE_DEFAULT);
207*ba7319e9SDmitry Salychev }
208*ba7319e9SDmitry Salychev
209*ba7319e9SDmitry Salychev static int
memac_mdio_fdt_probe_child(device_t bus,phandle_t child)210*ba7319e9SDmitry Salychev memac_mdio_fdt_probe_child(device_t bus, phandle_t child)
211*ba7319e9SDmitry Salychev {
212*ba7319e9SDmitry Salychev device_t childdev;
213*ba7319e9SDmitry Salychev
214*ba7319e9SDmitry Salychev /* Make sure we do not aliready have a device. */
215*ba7319e9SDmitry Salychev childdev = ofw_bus_find_child_device_by_phandle(bus, child);
216*ba7319e9SDmitry Salychev if (childdev != NULL)
217*ba7319e9SDmitry Salychev return (0);
218*ba7319e9SDmitry Salychev
219*ba7319e9SDmitry Salychev childdev = simplebus_add_device(bus, child, 0, NULL, -1, NULL);
220*ba7319e9SDmitry Salychev if (childdev == NULL)
221*ba7319e9SDmitry Salychev return (ENXIO);
222*ba7319e9SDmitry Salychev
223*ba7319e9SDmitry Salychev return (device_probe_and_attach(childdev));
224*ba7319e9SDmitry Salychev }
225*ba7319e9SDmitry Salychev
226*ba7319e9SDmitry Salychev static int
memac_mdio_fdt_attach(device_t dev)227*ba7319e9SDmitry Salychev memac_mdio_fdt_attach(device_t dev)
228*ba7319e9SDmitry Salychev {
229*ba7319e9SDmitry Salychev struct memac_mdio_softc_fdt *sc;
230*ba7319e9SDmitry Salychev phandle_t node, child;
231*ba7319e9SDmitry Salychev int error;
232*ba7319e9SDmitry Salychev
233*ba7319e9SDmitry Salychev sc = device_get_softc(dev);
234*ba7319e9SDmitry Salychev sc->scc.dev = dev;
235*ba7319e9SDmitry Salychev
236*ba7319e9SDmitry Salychev error = memac_mdio_generic_attach(&sc->scc);
237*ba7319e9SDmitry Salychev if (error != 0)
238*ba7319e9SDmitry Salychev return (error);
239*ba7319e9SDmitry Salychev
240*ba7319e9SDmitry Salychev /* Attach the *phy* children represented in the device tree. */
241*ba7319e9SDmitry Salychev bus_generic_probe(dev);
242*ba7319e9SDmitry Salychev bus_enumerate_hinted_children(dev);
243*ba7319e9SDmitry Salychev node = ofw_bus_get_node(dev);
244*ba7319e9SDmitry Salychev simplebus_init(dev, node);
245*ba7319e9SDmitry Salychev for (child = OF_child(node); child > 0; child = OF_peer(child)) {
246*ba7319e9SDmitry Salychev if (!OF_hasprop(child, "reg"))
247*ba7319e9SDmitry Salychev continue;
248*ba7319e9SDmitry Salychev if (memac_mdio_fdt_probe_child(dev, child) != 0)
249*ba7319e9SDmitry Salychev continue;
250*ba7319e9SDmitry Salychev }
251*ba7319e9SDmitry Salychev
252*ba7319e9SDmitry Salychev return (0);
253*ba7319e9SDmitry Salychev }
254*ba7319e9SDmitry Salychev
255*ba7319e9SDmitry Salychev static int
memac_mdio_fdt_detach(device_t dev)256*ba7319e9SDmitry Salychev memac_mdio_fdt_detach(device_t dev)
257*ba7319e9SDmitry Salychev {
258*ba7319e9SDmitry Salychev struct memac_mdio_softc_fdt *sc;
259*ba7319e9SDmitry Salychev
260*ba7319e9SDmitry Salychev sc = device_get_softc(dev);
261*ba7319e9SDmitry Salychev return (memac_mdio_generic_detach(&sc->scc));
262*ba7319e9SDmitry Salychev }
263*ba7319e9SDmitry Salychev
264*ba7319e9SDmitry Salychev static const struct ofw_bus_devinfo *
memac_simplebus_get_devinfo(device_t bus,device_t child)265*ba7319e9SDmitry Salychev memac_simplebus_get_devinfo(device_t bus, device_t child)
266*ba7319e9SDmitry Salychev {
267*ba7319e9SDmitry Salychev
268*ba7319e9SDmitry Salychev return (OFW_BUS_GET_DEVINFO(device_get_parent(bus), child));
269*ba7319e9SDmitry Salychev }
270*ba7319e9SDmitry Salychev
271*ba7319e9SDmitry Salychev static device_method_t memac_mdio_fdt_methods[] = {
272*ba7319e9SDmitry Salychev /* Device interface */
273*ba7319e9SDmitry Salychev DEVMETHOD(device_probe, memac_mdio_fdt_probe),
274*ba7319e9SDmitry Salychev DEVMETHOD(device_attach, memac_mdio_fdt_attach),
275*ba7319e9SDmitry Salychev DEVMETHOD(device_detach, memac_mdio_fdt_detach),
276*ba7319e9SDmitry Salychev
277*ba7319e9SDmitry Salychev /* MII interface */
278*ba7319e9SDmitry Salychev DEVMETHOD(miibus_readreg, memac_fdt_miibus_readreg),
279*ba7319e9SDmitry Salychev DEVMETHOD(miibus_writereg, memac_fdt_miibus_writereg),
280*ba7319e9SDmitry Salychev
281*ba7319e9SDmitry Salychev /* OFW/simplebus */
282*ba7319e9SDmitry Salychev DEVMETHOD(ofw_bus_get_devinfo, memac_simplebus_get_devinfo),
283*ba7319e9SDmitry Salychev DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
284*ba7319e9SDmitry Salychev DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
285*ba7319e9SDmitry Salychev DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
286*ba7319e9SDmitry Salychev DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
287*ba7319e9SDmitry Salychev DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
288*ba7319e9SDmitry Salychev
289*ba7319e9SDmitry Salychev /* Bus interface */
290*ba7319e9SDmitry Salychev DEVMETHOD(bus_add_child, bus_generic_add_child),
291*ba7319e9SDmitry Salychev DEVMETHOD(bus_read_ivar, memac_mdio_read_ivar),
292*ba7319e9SDmitry Salychev DEVMETHOD(bus_get_property, memac_mdio_get_property),
293*ba7319e9SDmitry Salychev
294*ba7319e9SDmitry Salychev DEVMETHOD_END
295*ba7319e9SDmitry Salychev };
296*ba7319e9SDmitry Salychev
297*ba7319e9SDmitry Salychev DEFINE_CLASS_0(memac_mdio_fdt, memac_mdio_fdt_driver, memac_mdio_fdt_methods,
298*ba7319e9SDmitry Salychev sizeof(struct memac_mdio_softc_fdt));
299*ba7319e9SDmitry Salychev
300*ba7319e9SDmitry Salychev EARLY_DRIVER_MODULE(memac_mdio_fdt, simplebus, memac_mdio_fdt_driver, 0, 0,
301*ba7319e9SDmitry Salychev BUS_PASS_SUPPORTDEV);
302*ba7319e9SDmitry Salychev
303*ba7319e9SDmitry Salychev DRIVER_MODULE(miibus, memac_mdio_fdt, miibus_driver, 0, 0);
304*ba7319e9SDmitry Salychev MODULE_DEPEND(memac_mdio_fdt, miibus, 1, 1, 1);
305*ba7319e9SDmitry Salychev MODULE_VERSION(memac_mdio_fdt, 1);
306