2 * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>.
3 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/errno.h>
37 #include <sys/libkern.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 #include <dev/ofw/openfirm.h>
43 #include "ofw_bus_if.h"
46 ofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node)
51 /* The 'name' property is considered mandatory. */
52 if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1)
54 OF_getprop_alloc(node, "compatible", 1, (void **)&obd->obd_compat);
55 OF_getprop_alloc(node, "device_type", 1, (void **)&obd->obd_type);
56 OF_getprop_alloc(node, "model", 1, (void **)&obd->obd_model);
62 ofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd)
67 if (obd->obd_compat != NULL)
68 free(obd->obd_compat, M_OFWPROP);
69 if (obd->obd_model != NULL)
70 free(obd->obd_model, M_OFWPROP);
71 if (obd->obd_name != NULL)
72 free(obd->obd_name, M_OFWPROP);
73 if (obd->obd_type != NULL)
74 free(obd->obd_type, M_OFWPROP);
78 ofw_bus_gen_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
82 if (ofw_bus_get_name(child) != NULL) {
83 strlcat(buf, "name=", buflen);
84 strlcat(buf, ofw_bus_get_name(child), buflen);
87 if (ofw_bus_get_compat(child) != NULL) {
88 strlcat(buf, " compat=", buflen);
89 strlcat(buf, ofw_bus_get_compat(child), buflen);
95 ofw_bus_gen_get_compat(device_t bus, device_t dev)
97 const struct ofw_bus_devinfo *obd;
99 obd = OFW_BUS_GET_DEVINFO(bus, dev);
102 return (obd->obd_compat);
106 ofw_bus_gen_get_model(device_t bus, device_t dev)
108 const struct ofw_bus_devinfo *obd;
110 obd = OFW_BUS_GET_DEVINFO(bus, dev);
113 return (obd->obd_model);
117 ofw_bus_gen_get_name(device_t bus, device_t dev)
119 const struct ofw_bus_devinfo *obd;
121 obd = OFW_BUS_GET_DEVINFO(bus, dev);
124 return (obd->obd_name);
128 ofw_bus_gen_get_node(device_t bus, device_t dev)
130 const struct ofw_bus_devinfo *obd;
132 obd = OFW_BUS_GET_DEVINFO(bus, dev);
135 return (obd->obd_node);
139 ofw_bus_gen_get_type(device_t bus, device_t dev)
141 const struct ofw_bus_devinfo *obd;
143 obd = OFW_BUS_GET_DEVINFO(bus, dev);
146 return (obd->obd_type);
150 ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz)
155 if (OF_getprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1)
157 ii->opi_addrc = addrc * sizeof(pcell_t);
159 ii->opi_imapsz = OF_getprop_alloc(node, "interrupt-map", 1,
160 (void **)&ii->opi_imap);
161 if (ii->opi_imapsz > 0) {
162 msksz = OF_getprop_alloc(node, "interrupt-map-mask", 1,
163 (void **)&ii->opi_imapmsk);
165 * Failure to get the mask is ignored; a full mask is used
166 * then. We barf on bad mask sizes, however.
168 if (msksz != -1 && msksz != ii->opi_addrc + intrsz)
169 panic("ofw_bus_setup_iinfo: bad interrupt-map-mask "
175 ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg,
176 int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz,
181 if (ii->opi_imapsz <= 0)
183 KASSERT(regsz >= ii->opi_addrc,
184 ("ofw_bus_lookup_imap: register size too small: %d < %d",
185 regsz, ii->opi_addrc));
186 rv = OF_getprop(node, "reg", reg, regsz);
188 panic("ofw_bus_lookup_imap: could not get reg property");
189 return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc,
190 ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr,
195 * Map an interrupt using the firmware reg, interrupt-map and
196 * interrupt-map-mask properties.
197 * The interrupt property to be mapped must be of size intrsz, and pointed to
198 * by intr. The regs property of the node for which the mapping is done must
199 * be passed as regs. This property is an array of register specifications;
200 * the size of the address part of such a specification must be passed as
201 * physsz. Only the first element of the property is used.
202 * imap and imapsz hold the interrupt mask and it's size.
203 * imapmsk is a pointer to the interrupt-map-mask property, which must have
204 * a size of physsz + intrsz; it may be NULL, in which case a full mask is
206 * maskbuf must point to a buffer of length physsz + intrsz.
207 * The interrupt is returned in result, which must point to a buffer of length
208 * rintrsz (which gives the expected size of the mapped interrupt).
209 * Returns 1 if a mapping was found, 0 otherwise.
212 ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz,
213 void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result,
217 uint8_t *ref = maskbuf;
218 uint8_t *uiintr = intr;
219 uint8_t *uiregs = regs;
220 uint8_t *uiimapmsk = imapmsk;
226 if (imapmsk != NULL) {
227 for (i = 0; i < physsz; i++)
228 ref[i] = uiregs[i] & uiimapmsk[i];
229 for (i = 0; i < intrsz; i++)
230 ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i];
232 bcopy(regs, ref, physsz);
233 bcopy(intr, ref + physsz, intrsz);
239 bcopy(mptr + physsz + intrsz, &parent, sizeof(parent));
240 if (OF_searchprop(parent, "#interrupt-cells",
241 &pintrsz, sizeof(pintrsz)) == -1)
242 pintrsz = 1; /* default */
243 pintrsz *= sizeof(pcell_t);
245 /* Compute the map stride size. */
246 tsz = physsz + intrsz + sizeof(phandle_t) + pintrsz;
247 KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map"));
250 * XXX: Apple hardware uses a second cell to set information
251 * on the interrupt trigger type. This information should
252 * be used somewhere to program the PIC.
255 if (bcmp(ref, mptr, physsz + intrsz) == 0) {
256 bcopy(mptr + physsz + intrsz + sizeof(parent),