xref: /freebsd/sys/dev/ofw/ofw_firmware.c (revision c4fde87a3236b500246d4fce76dbca2a3d64f73a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2022 Beckhoff Automation GmbH & Co. KG
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 #include <sys/cdefs.h>
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/malloc.h>
35 #include <sys/bus.h>
36 #include <sys/cpu.h>
37 #include <machine/bus.h>
38 
39 #include <dev/fdt/simplebus.h>
40 
41 #include <dev/ofw/openfirm.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 
45 struct ofw_firmware_softc {
46 	device_t		dev;
47 };
48 
49 static struct simplebus_devinfo *
50 ofw_firmware_setup_dinfo(device_t dev, phandle_t node,
51     struct simplebus_devinfo *di)
52 {
53 	struct simplebus_softc *sc;
54 	struct simplebus_devinfo *ndi;
55 
56 	sc = device_get_softc_class(dev, &simplebus_driver);
57 	if (di == NULL)
58 		ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
59 	else
60 		ndi = di;
61 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
62 		if (di == NULL)
63 			free(ndi, M_DEVBUF);
64 		return (NULL);
65 	}
66 
67 	/* reg resources is from the parent but interrupts is on the node itself */
68 	resource_list_init(&ndi->rl);
69 	ofw_bus_reg_to_rl(dev, OF_parent(node), sc->acells, sc->scells, &ndi->rl);
70 	ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
71 
72 	return (ndi);
73 }
74 
75 static device_t
76 ofw_firmware_add_device(device_t dev, phandle_t node, u_int order,
77     const char *name, int unit, struct simplebus_devinfo *di)
78 {
79 	struct simplebus_devinfo *ndi;
80 	device_t cdev;
81 
82 	if ((ndi = ofw_firmware_setup_dinfo(dev, node, di)) == NULL)
83 		return (NULL);
84 	cdev = device_add_child_ordered(dev, order, name, unit);
85 	if (cdev == NULL) {
86 		device_printf(dev, "<%s>: device_add_child failed\n",
87 		    ndi->obdinfo.obd_name);
88 		resource_list_free(&ndi->rl);
89 		ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
90 		if (di == NULL)
91 			free(ndi, M_DEVBUF);
92 		return (NULL);
93 	}
94 	device_set_ivars(cdev, ndi);
95 
96 	return(cdev);
97 }
98 
99 static int
100 ofw_firmware_probe(device_t dev)
101 {
102 	const char *name, *compat;
103 	phandle_t root, parent;
104 
105 	name = ofw_bus_get_name(dev);
106 	if (name == NULL || strcmp(name, "firmware") != 0)
107 		return (ENXIO);
108 	parent = OF_parent(ofw_bus_get_node(dev));
109 	root = OF_finddevice("/");
110 	if (parent != root)
111 		return (ENXIO);
112 	compat = ofw_bus_get_compat(dev);
113 	if (compat != NULL)
114 		return (ENXIO);
115 
116 	device_set_desc(dev, "OFW Firmware Group");
117 	return (BUS_PROBE_GENERIC);
118 }
119 
120 static int
121 ofw_firmware_attach(device_t dev)
122 {
123 	struct ofw_firmware_softc *sc;
124 	struct simplebus_softc *sb_sc;
125 	phandle_t node, child;
126 	device_t cdev;
127 
128 	sc = device_get_softc(dev);
129 	sb_sc = device_get_softc_class(dev, &simplebus_driver);
130 	sc->dev = dev;
131 	node = ofw_bus_get_node(dev);
132 
133 	if (OF_getencprop(node, "#address-cells", &sb_sc->acells,
134 	    sizeof(sb_sc->acells)) == -1) {
135 		if (OF_getencprop(OF_parent(node), "#address-cells",
136 		     &sb_sc->acells, sizeof(sb_sc->acells)) == -1) {
137 			sb_sc->acells = 2;
138 		}
139 	}
140 	if (OF_getencprop(node, "#size-cells", &sb_sc->scells,
141 	    sizeof(sb_sc->scells)) == -1) {
142 		if (OF_getencprop(OF_parent(node), "#size-cells",
143 		    &sb_sc->scells, sizeof(sb_sc->scells)) == -1) {
144 			sb_sc->scells = 1;
145 		}
146 	}
147 
148 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
149 		cdev = ofw_firmware_add_device(dev, child, 0, NULL, -1, NULL);
150 		if (cdev != NULL)
151 			device_probe_and_attach(cdev);
152 	}
153 
154 	bus_attach_children(dev);
155 	return (0);
156 }
157 
158 static device_method_t ofw_firmware_methods[] = {
159 	/* device_if */
160 	DEVMETHOD(device_probe, 	ofw_firmware_probe),
161 	DEVMETHOD(device_attach, 	ofw_firmware_attach),
162 
163 	DEVMETHOD_END
164 };
165 
166 DEFINE_CLASS_1(ofw_firmware, ofw_firmware_driver, ofw_firmware_methods,
167   sizeof(struct ofw_firmware_softc), simplebus_driver);
168 
169 EARLY_DRIVER_MODULE(ofw_firmware, simplebus, ofw_firmware_driver, 0, 0,
170     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
171 MODULE_VERSION(ofw_firmware, 1);
172