xref: /freebsd/sys/arm64/broadcom/brcmmdio/mdio_nexus_iproc.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*-
2  * Copyright (c) 2019 Juniper Networks, Inc.
3  * Copyright (c) 2019 Semihalf.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/rman.h>
36 #include <sys/systm.h>
37 
38 #include <dev/fdt/simplebus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 #include <dev/ofw/ofw_bus.h>
41 
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 
45 #include "mdio_if.h"
46 
47 MALLOC_DEFINE(M_BRCM_IPROC_NEXUS, "Broadcom IPROC MDIO NEXUS",
48     "Broadcom IPROC MDIO NEXUS dynamic memory");
49 
50 struct brcm_mdionexus_softc {
51 	struct simplebus_softc simplebus_sc;
52 	uint32_t mux_id;
53 };
54 
55 /* OFW bus interface */
56 struct brcm_mdionexus_ofw_devinfo {
57 	struct ofw_bus_devinfo	di_dinfo;
58 	struct resource_list	di_rl;
59 };
60 
61 static device_probe_t brcm_mdionexus_fdt_probe;
62 static device_attach_t brcm_mdionexus_fdt_attach;
63 
64 static const struct ofw_bus_devinfo * brcm_mdionexus_ofw_get_devinfo(device_t,
65     device_t);
66 static int brcm_mdionexus_mdio_readreg(device_t, int, int);
67 static int brcm_mdionexus_mdio_writereg(device_t, int, int, int);
68 
69 static device_method_t brcm_mdionexus_fdt_methods[] = {
70 	/* Device interface */
71 	DEVMETHOD(device_probe,		brcm_mdionexus_fdt_probe),
72 	DEVMETHOD(device_attach,	brcm_mdionexus_fdt_attach),
73 
74 	/* Bus interface */
75 	DEVMETHOD(bus_alloc_resource,		bus_generic_alloc_resource),
76 	DEVMETHOD(bus_release_resource,		bus_generic_release_resource),
77 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
78 
79 	/* ofw_bus interface */
80 	DEVMETHOD(ofw_bus_get_devinfo,	brcm_mdionexus_ofw_get_devinfo),
81 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
82 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
83 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
84 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
85 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
86 
87 	/* MDIO interface */
88 	/* MDIO interface */
89 	DEVMETHOD(mdio_readreg,		brcm_mdionexus_mdio_readreg),
90 	DEVMETHOD(mdio_writereg,	brcm_mdionexus_mdio_writereg),
91 
92 	DEVMETHOD_END
93 };
94 
95 DEFINE_CLASS_0(brcm_mdionexus, brcm_mdionexus_fdt_driver, brcm_mdionexus_fdt_methods,
96     sizeof(struct brcm_mdionexus_softc));
97 
98 static driver_t brcm_mdionexus_driver = {
99         "brcm_mdionexus",
100 	brcm_mdionexus_fdt_methods,
101         sizeof(struct brcm_mdionexus_softc)
102 };
103 
104 EARLY_DRIVER_MODULE(brcm_mdionexus, brcm_iproc_mdio, brcm_mdionexus_driver,
105     NULL, NULL, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
106 
107 static int brcm_mdionexus_ofw_bus_attach(device_t);
108 
109 static int
110 brcm_mdionexus_mdio_readreg(device_t dev, int phy, int reg)
111 {
112 	struct brcm_mdionexus_softc *sc;
113 
114 	sc = device_get_softc(dev);
115 
116 	return (MDIO_READREG_MUX(device_get_parent(dev),
117 			sc->mux_id, phy, reg));
118 }
119 
120 static int
121 brcm_mdionexus_mdio_writereg(device_t dev, int phy, int reg, int val)
122 {
123 	struct brcm_mdionexus_softc *sc;
124 
125 	sc = device_get_softc(dev);
126 
127 	return (MDIO_WRITEREG_MUX(device_get_parent(dev),
128 			sc->mux_id, phy, reg, val));
129 }
130 
131 static __inline void
132 get_addr_size_cells(phandle_t node, pcell_t *addr_cells, pcell_t *size_cells)
133 {
134 
135 	*addr_cells = 2;
136 	/* Find address cells if present */
137 	OF_getencprop(node, "#address-cells", addr_cells, sizeof(*addr_cells));
138 
139 	*size_cells = 2;
140 	/* Find size cells if present */
141 	OF_getencprop(node, "#size-cells", size_cells, sizeof(*size_cells));
142 }
143 
144 static int
145 brcm_mdionexus_fdt_probe(device_t dev)
146 {
147 
148 	if (!ofw_bus_status_okay(dev))
149 		return (ENXIO);
150 	device_set_desc(dev, "Broadcom MDIO nexus");
151 	return (BUS_PROBE_SPECIFIC);
152 }
153 
154 static int
155 brcm_mdionexus_fdt_attach(device_t dev)
156 {
157 	struct brcm_mdionexus_softc *sc;
158 	int err;
159 	pcell_t addr_cells, size_cells, buf[2];
160 	phandle_t node;
161 
162 	sc = device_get_softc(dev);
163 
164 	node = ofw_bus_get_node(dev);
165 	get_addr_size_cells(node, &addr_cells, &size_cells);
166 	if ((addr_cells != 1) || (size_cells != 0)) {
167 		device_printf(dev, "Only addr_cells=1 and size_cells=0 are supported\n");
168 		return (EINVAL);
169 	}
170 
171 	if (OF_getencprop(node, "reg", buf, sizeof(pcell_t)) < 0)
172 		return (ENXIO);
173 
174 	sc->mux_id = buf[0];
175 
176 	err = brcm_mdionexus_ofw_bus_attach(dev);
177 	if (err != 0)
178 		return (err);
179 
180 	return (bus_generic_attach(dev));
181 }
182 
183 static const struct ofw_bus_devinfo *
184 brcm_mdionexus_ofw_get_devinfo(device_t bus __unused, device_t child)
185 {
186 	struct brcm_mdionexus_ofw_devinfo *di;
187 
188 	di = device_get_ivars(child);
189 	return (&di->di_dinfo);
190 }
191 
192 static int
193 brcm_mdionexus_ofw_bus_attach(device_t dev)
194 {
195 	struct simplebus_softc *sc;
196 	struct brcm_mdionexus_ofw_devinfo *di;
197 	device_t child;
198 	phandle_t parent, node;
199 
200 	parent = ofw_bus_get_node(dev);
201 	simplebus_init(dev, parent);
202 
203 	sc = (struct simplebus_softc *)device_get_softc(dev);
204 
205 	/* Iterate through all bus subordinates */
206 	for (node = OF_child(parent); node > 0; node = OF_peer(node)) {
207 		/* Allocate and populate devinfo. */
208 		di = malloc(sizeof(*di), M_BRCM_IPROC_NEXUS, M_WAITOK | M_ZERO);
209 		if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node) != 0) {
210 			free(di, M_BRCM_IPROC_NEXUS);
211 			continue;
212 		}
213 
214 		/* Initialize and populate resource list. */
215 		resource_list_init(&di->di_rl);
216 		ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells,
217 		    &di->di_rl);
218 		ofw_bus_intr_to_rl(dev, node, &di->di_rl, NULL);
219 
220 		/* Add newbus device for this FDT node */
221 		child = device_add_child(dev, NULL, -1);
222 		if (child == NULL) {
223 			resource_list_free(&di->di_rl);
224 			ofw_bus_gen_destroy_devinfo(&di->di_dinfo);
225 			free(di, M_BRCM_IPROC_NEXUS);
226 			continue;
227 		}
228 
229 		device_set_ivars(child, di);
230 	}
231 
232 	return (0);
233 }
234