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
47MALLOC_DEFINE(M_BRCM_IPROC_NEXUS, "Broadcom IPROC MDIO NEXUS",
48    "Broadcom IPROC MDIO NEXUS dynamic memory");
49
50struct brcm_mdionexus_softc {
51	struct simplebus_softc simplebus_sc;
52	uint32_t mux_id;
53};
54
55/* OFW bus interface */
56struct brcm_mdionexus_ofw_devinfo {
57	struct ofw_bus_devinfo	di_dinfo;
58	struct resource_list	di_rl;
59};
60
61static device_probe_t brcm_mdionexus_fdt_probe;
62static device_attach_t brcm_mdionexus_fdt_attach;
63
64static const struct ofw_bus_devinfo * brcm_mdionexus_ofw_get_devinfo(device_t,
65    device_t);
66static int brcm_mdionexus_mdio_readreg(device_t, int, int);
67static int brcm_mdionexus_mdio_writereg(device_t, int, int, int);
68
69static 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
95DEFINE_CLASS_0(brcm_mdionexus, brcm_mdionexus_fdt_driver, brcm_mdionexus_fdt_methods,
96    sizeof(struct brcm_mdionexus_softc));
97
98static devclass_t brcm_mdionexus_fdt_devclass;
99
100static driver_t brcm_mdionexus_driver = {
101        "brcm_mdionexus",
102	brcm_mdionexus_fdt_methods,
103        sizeof(struct brcm_mdionexus_softc)
104};
105EARLY_DRIVER_MODULE(brcm_mdionexus, brcm_iproc_mdio, brcm_mdionexus_driver,
106    brcm_mdionexus_fdt_devclass, NULL, NULL, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
107
108static int brcm_mdionexus_ofw_bus_attach(device_t);
109
110static int
111brcm_mdionexus_mdio_readreg(device_t dev, int phy, int reg)
112{
113	struct brcm_mdionexus_softc *sc;
114
115	sc = device_get_softc(dev);
116
117	return (MDIO_READREG_MUX(device_get_parent(dev),
118			sc->mux_id, phy, reg));
119}
120
121static int
122brcm_mdionexus_mdio_writereg(device_t dev, int phy, int reg, int val)
123{
124	struct brcm_mdionexus_softc *sc;
125
126	sc = device_get_softc(dev);
127
128	return (MDIO_WRITEREG_MUX(device_get_parent(dev),
129			sc->mux_id, phy, reg, val));
130}
131
132static __inline void
133get_addr_size_cells(phandle_t node, pcell_t *addr_cells, pcell_t *size_cells)
134{
135
136	*addr_cells = 2;
137	/* Find address cells if present */
138	OF_getencprop(node, "#address-cells", addr_cells, sizeof(*addr_cells));
139
140	*size_cells = 2;
141	/* Find size cells if present */
142	OF_getencprop(node, "#size-cells", size_cells, sizeof(*size_cells));
143}
144
145static int
146brcm_mdionexus_fdt_probe(device_t dev)
147{
148
149	if (!ofw_bus_status_okay(dev))
150		return (ENXIO);
151	device_set_desc(dev, "Broadcom MDIO nexus");
152	return (BUS_PROBE_SPECIFIC);
153}
154
155static int
156brcm_mdionexus_fdt_attach(device_t dev)
157{
158	struct brcm_mdionexus_softc *sc;
159	int err;
160	pcell_t addr_cells, size_cells, buf[2];
161	phandle_t node;
162
163	sc = device_get_softc(dev);
164
165	node = ofw_bus_get_node(dev);
166	get_addr_size_cells(node, &addr_cells, &size_cells);
167	if ((addr_cells != 1) || (size_cells != 0)) {
168		device_printf(dev, "Only addr_cells=1 and size_cells=0 are supported\n");
169		return (EINVAL);
170	}
171
172	if (OF_getencprop(node, "reg", buf, sizeof(pcell_t)) < 0)
173		return (ENXIO);
174
175	sc->mux_id = buf[0];
176
177	err = brcm_mdionexus_ofw_bus_attach(dev);
178	if (err != 0)
179		return (err);
180
181	return (bus_generic_attach(dev));
182}
183
184static const struct ofw_bus_devinfo *
185brcm_mdionexus_ofw_get_devinfo(device_t bus __unused, device_t child)
186{
187	struct brcm_mdionexus_ofw_devinfo *di;
188
189	di = device_get_ivars(child);
190	return (&di->di_dinfo);
191}
192
193static int
194brcm_mdionexus_ofw_bus_attach(device_t dev)
195{
196	struct simplebus_softc *sc;
197	struct brcm_mdionexus_ofw_devinfo *di;
198	device_t child;
199	phandle_t parent, node;
200
201	parent = ofw_bus_get_node(dev);
202	simplebus_init(dev, parent);
203
204	sc = (struct simplebus_softc *)device_get_softc(dev);
205
206	/* Iterate through all bus subordinates */
207	for (node = OF_child(parent); node > 0; node = OF_peer(node)) {
208		/* Allocate and populate devinfo. */
209		di = malloc(sizeof(*di), M_BRCM_IPROC_NEXUS, M_WAITOK | M_ZERO);
210		if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node) != 0) {
211			free(di, M_BRCM_IPROC_NEXUS);
212			continue;
213		}
214
215		/* Initialize and populate resource list. */
216		resource_list_init(&di->di_rl);
217		ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells,
218		    &di->di_rl);
219		ofw_bus_intr_to_rl(dev, node, &di->di_rl, NULL);
220
221		/* Add newbus device for this FDT node */
222		child = device_add_child(dev, NULL, -1);
223		if (child == NULL) {
224			resource_list_free(&di->di_rl);
225			ofw_bus_gen_destroy_devinfo(&di->di_dinfo);
226			free(di, M_BRCM_IPROC_NEXUS);
227			continue;
228		}
229
230		device_set_ivars(child, di);
231	}
232
233	return (0);
234}
235