xref: /freebsd/sys/dev/fdt/simple_mfd.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Rubicon Communications, LLC (Netgate)
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 AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 
38 #include <machine/bus.h>
39 
40 #include <dev/fdt/simplebus.h>
41 
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 
45 struct simple_mfd_softc {
46 	struct simplebus_softc	sc;
47 };
48 
49 device_t simple_mfd_add_device(device_t dev, phandle_t node, u_int order,
50     const char *name, int unit, struct simplebus_devinfo *di);
51 struct simplebus_devinfo *simple_mfd_setup_dinfo(device_t dev, phandle_t node, struct simplebus_devinfo *di);
52 
53 static int
54 simple_mfd_probe(device_t dev)
55 {
56 
57 	if (!ofw_bus_status_okay(dev))
58 		return (ENXIO);
59 	if (!ofw_bus_is_compatible(dev, "simple-mfd"))
60 		return (ENXIO);
61 
62 	device_set_desc(dev, "Simple MFD (Multi-Functions Device)");
63 
64 	return (BUS_PROBE_GENERIC);
65 }
66 
67 static int
68 simple_mfd_attach(device_t dev)
69 {
70 	struct simple_mfd_softc *sc;
71 	phandle_t node, child;
72 	device_t cdev;
73 
74 	sc = device_get_softc(dev);
75 	node = ofw_bus_get_node(dev);
76 
77 	/* Parse address-cells and size-cells from the parent node as a fallback */
78 	if (OF_getencprop(node, "#address-cells", &sc->sc.acells,
79 	    sizeof(sc->sc.acells)) == -1) {
80 		if (OF_getencprop(OF_parent(node), "#address-cells", &sc->sc.acells,
81 		    sizeof(sc->sc.acells)) == -1) {
82 			sc->sc.acells = 2;
83 		}
84 	}
85 	if (OF_getencprop(node, "#size-cells", &sc->sc.scells,
86 	    sizeof(sc->sc.scells)) == -1) {
87 		if (OF_getencprop(OF_parent(node), "#size-cells", &sc->sc.scells,
88 		    sizeof(sc->sc.scells)) == -1) {
89 			sc->sc.scells = 1;
90 		}
91 	}
92 
93 	/* If the node has a ranges prop, parse it so children mapping will be done correctly */
94 	if (OF_hasprop(node, "ranges")) {
95 		if (simplebus_fill_ranges(node, &sc->sc) < 0) {
96 			device_printf(dev, "could not get ranges\n");
97 			return (ENXIO);
98 		}
99 	}
100 
101 	/* Attach child devices */
102 	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
103 		cdev = simple_mfd_add_device(dev, child, 0, NULL, -1, NULL);
104 		if (cdev != NULL)
105 			device_probe_and_attach(cdev);
106 	}
107 
108 	return (bus_generic_attach(dev));
109 }
110 
111 struct simplebus_devinfo *
112 simple_mfd_setup_dinfo(device_t dev, phandle_t node,
113     struct simplebus_devinfo *di)
114 {
115 	struct simplebus_softc *sc;
116 	struct simplebus_devinfo *ndi;
117 
118 	sc = device_get_softc(dev);
119 	if (di == NULL)
120 		ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
121 	else
122 		ndi = di;
123 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
124 		if (di == NULL)
125 			free(ndi, M_DEVBUF);
126 		return (NULL);
127 	}
128 
129 	/* reg resources is from the parent but interrupts is on the node itself */
130 	resource_list_init(&ndi->rl);
131 	ofw_bus_reg_to_rl(dev, OF_parent(node), sc->acells, sc->scells, &ndi->rl);
132 	ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
133 
134 	return (ndi);
135 }
136 
137 device_t
138 simple_mfd_add_device(device_t dev, phandle_t node, u_int order,
139     const char *name, int unit, struct simplebus_devinfo *di)
140 {
141 	struct simplebus_devinfo *ndi;
142 	device_t cdev;
143 
144 	if ((ndi = simple_mfd_setup_dinfo(dev, node, di)) == NULL)
145 		return (NULL);
146 	cdev = device_add_child_ordered(dev, order, name, unit);
147 	if (cdev == NULL) {
148 		device_printf(dev, "<%s>: device_add_child failed\n",
149 		    ndi->obdinfo.obd_name);
150 		resource_list_free(&ndi->rl);
151 		ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
152 		if (di == NULL)
153 			free(ndi, M_DEVBUF);
154 		return (NULL);
155 	}
156 	device_set_ivars(cdev, ndi);
157 
158 	return(cdev);
159 }
160 
161 static device_method_t simple_mfd_methods[] = {
162 	/* Device interface */
163 	DEVMETHOD(device_probe,		simple_mfd_probe),
164 	DEVMETHOD(device_attach,	simple_mfd_attach),
165 
166 	DEVMETHOD_END
167 };
168 
169 DEFINE_CLASS_1(simple_mfd, simple_mfd_driver, simple_mfd_methods,
170   sizeof(struct simple_mfd_softc), simplebus_driver);
171 
172 static devclass_t simple_mfd_devclass;
173 
174 EARLY_DRIVER_MODULE(simple_mfd, simplebus, simple_mfd_driver,
175     simple_mfd_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_LATE);
176 MODULE_VERSION(simple_mfd, 1);
177