ofw_bus_subr.c revision 209298
18876Srgrimes/*-
24Srgrimes * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>.
34Srgrimes * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
44Srgrimes * All rights reserved.
58876Srgrimes *
64Srgrimes * Redistribution and use in source and binary forms, with or without
74Srgrimes * modification, are permitted provided that the following conditions
84Srgrimes * are met:
94Srgrimes * 1. Redistributions of source code must retain the above copyright
104Srgrimes *    notice, this list of conditions, and the following disclaimer,
118876Srgrimes *    without modification, immediately at the beginning of the file.
128876Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
134Srgrimes *    notice, this list of conditions and the following disclaimer in
144Srgrimes *    the documentation and/or other materials provided with the
158876Srgrimes *    distribution.
164Srgrimes *
178876Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
184Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
194Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
204Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
214Srgrimes * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228876Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
234Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
244Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
254Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2612734Sbde * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
274Srgrimes * SUCH DAMAGE.
28623Srgrimes */
294Srgrimes
304Srgrimes#include <sys/cdefs.h>
314Srgrimes__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_bus_subr.c 209298 2010-06-18 14:06:27Z nwhitehorn $");
324Srgrimes
334Srgrimes#include "opt_platform.h"
342056Swollman#include <sys/param.h>
352056Swollman#include <sys/systm.h>
3612734Sbde#include <sys/bus.h>
372056Swollman#include <sys/errno.h>
384Srgrimes#include <sys/libkern.h>
394Srgrimes
404Srgrimes#include <dev/ofw/ofw_bus.h>
4112515Sphk#include <dev/ofw/ofw_bus_subr.h>
4212473Sbde#include <dev/ofw/openfirm.h>
434Srgrimes
4412515Sphk#include "ofw_bus_if.h"
4512515Sphk
4612515Sphkint
4712515Sphkofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node)
4812515Sphk{
494Srgrimes
504Srgrimes	if (obd == NULL)
514Srgrimes		return (ENOMEM);
524Srgrimes	/* The 'name' property is considered mandatory. */
534Srgrimes	if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1)
5412515Sphk		return (EINVAL);
5512515Sphk	OF_getprop_alloc(node, "compatible", 1, (void **)&obd->obd_compat);
564Srgrimes	OF_getprop_alloc(node, "device_type", 1, (void **)&obd->obd_type);
5712515Sphk	OF_getprop_alloc(node, "model", 1, (void **)&obd->obd_model);
584Srgrimes	obd->obd_node = node;
594Srgrimes	return (0);
604Srgrimes}
614Srgrimes
624Srgrimesvoid
634Srgrimesofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd)
644Srgrimes{
654Srgrimes
664Srgrimes	if (obd == NULL)
674Srgrimes		return;
684Srgrimes	if (obd->obd_compat != NULL)
694Srgrimes		free(obd->obd_compat, M_OFWPROP);
704Srgrimes	if (obd->obd_model != NULL)
714Srgrimes		free(obd->obd_model, M_OFWPROP);
724Srgrimes	if (obd->obd_name != NULL)
734Srgrimes		free(obd->obd_name, M_OFWPROP);
744Srgrimes	if (obd->obd_type != NULL)
754Srgrimes		free(obd->obd_type, M_OFWPROP);
764Srgrimes}
774Srgrimes
784Srgrimesint
794Srgrimesofw_bus_gen_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
804Srgrimes    size_t buflen)
814Srgrimes{
824Srgrimes
834Srgrimes	if (ofw_bus_get_name(child) != NULL) {
844Srgrimes		strlcat(buf, "name=", buflen);
854Srgrimes		strlcat(buf, ofw_bus_get_name(child), buflen);
864Srgrimes	}
874Srgrimes
884Srgrimes	if (ofw_bus_get_compat(child) != NULL) {
894Srgrimes		strlcat(buf, " compat=", buflen);
904Srgrimes		strlcat(buf, ofw_bus_get_compat(child), buflen);
914Srgrimes	}
924Srgrimes	return (0);
934Srgrimes};
944Srgrimes
954Srgrimesconst char *
964Srgrimesofw_bus_gen_get_compat(device_t bus, device_t dev)
9712515Sphk{
9812515Sphk	const struct ofw_bus_devinfo *obd;
994Srgrimes
1004Srgrimes	obd = OFW_BUS_GET_DEVINFO(bus, dev);
1014Srgrimes	if (obd == NULL)
1024Srgrimes		return (NULL);
1034Srgrimes	return (obd->obd_compat);
1044Srgrimes}
1054Srgrimes
1064Srgrimesconst char *
1074Srgrimesofw_bus_gen_get_model(device_t bus, device_t dev)
1084Srgrimes{
1094Srgrimes	const struct ofw_bus_devinfo *obd;
1104Srgrimes
11112515Sphk	obd = OFW_BUS_GET_DEVINFO(bus, dev);
1124Srgrimes	if (obd == NULL)
113798Swollman		return (NULL);
1144Srgrimes	return (obd->obd_model);
1154Srgrimes}
1164Srgrimes
1174Srgrimesconst char *
11812473Sbdeofw_bus_gen_get_name(device_t bus, device_t dev)
1194Srgrimes{
1204Srgrimes	const struct ofw_bus_devinfo *obd;
1214Srgrimes
1224Srgrimes	obd = OFW_BUS_GET_DEVINFO(bus, dev);
1234Srgrimes	if (obd == NULL)
1244Srgrimes		return (NULL);
1254Srgrimes	return (obd->obd_name);
126798Swollman}
1274Srgrimes
1284Srgrimesphandle_t
1294Srgrimesofw_bus_gen_get_node(device_t bus, device_t dev)
1304Srgrimes{
13112473Sbde	const struct ofw_bus_devinfo *obd;
1324Srgrimes
1334Srgrimes	obd = OFW_BUS_GET_DEVINFO(bus, dev);
1344Srgrimes	if (obd == NULL)
1354Srgrimes		return (0);
1364Srgrimes	return (obd->obd_node);
1374Srgrimes}
1384Srgrimes
1394Srgrimesconst char *
14012473Sbdeofw_bus_gen_get_type(device_t bus, device_t dev)
14112473Sbde{
14212473Sbde	const struct ofw_bus_devinfo *obd;
14312473Sbde
14412473Sbde	obd = OFW_BUS_GET_DEVINFO(bus, dev);
1454Srgrimes	if (obd == NULL)
1464Srgrimes		return (NULL);
1474Srgrimes	return (obd->obd_type);
1484Srgrimes}
1494Srgrimes
1504Srgrimesint
1514Srgrimesofw_bus_is_compatible(device_t dev, const char *onecompat)
1524Srgrimes{
1534Srgrimes	phandle_t node;
1544Srgrimes	const char *compat;
1554Srgrimes	int len, onelen, l;
1564Srgrimes
1574Srgrimes	if ((compat = ofw_bus_get_compat(dev)) == NULL)
1584Srgrimes		return (0);
1594Srgrimes
1604Srgrimes	if ((node = ofw_bus_get_node(dev)) == 0)
1614Srgrimes		return (0);
1624Srgrimes
1634Srgrimes	/* Get total 'compatible' prop len */
1644Srgrimes	if ((len = OF_getproplen(node, "compatible")) <= 0)
1654Srgrimes		return (0);
1664Srgrimes
1674Srgrimes	onelen = strlen(onecompat);
1684Srgrimes
1694Srgrimes	while (len > 0) {
1704Srgrimes		if (strncasecmp(compat, onecompat, onelen) == 0)
1714Srgrimes			/* Found it. */
1724Srgrimes			return (1);
1734Srgrimes
174		/* Slide to the next sub-string. */
175		l = strlen(compat) + 1;
176		compat += l;
177		len -= l;
178	}
179	return (0);
180}
181
182int
183ofw_bus_is_compatible_strict(device_t dev, const char *compatible)
184{
185	const char *compat;
186
187	if ((compat = ofw_bus_get_compat(dev)) == NULL)
188		return (0);
189
190	if (strncasecmp(compat, compatible, strlen(compatible)) == 0)
191		return (1);
192
193	return (0);
194}
195
196#ifndef FDT
197void
198ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz)
199{
200	pcell_t addrc;
201	int msksz;
202
203	if (OF_getprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1)
204		addrc = 2;
205	ii->opi_addrc = addrc * sizeof(pcell_t);
206
207	ii->opi_imapsz = OF_getprop_alloc(node, "interrupt-map", 1,
208	    (void **)&ii->opi_imap);
209	if (ii->opi_imapsz > 0) {
210		msksz = OF_getprop_alloc(node, "interrupt-map-mask", 1,
211		    (void **)&ii->opi_imapmsk);
212		/*
213		 * Failure to get the mask is ignored; a full mask is used
214		 * then.  We barf on bad mask sizes, however.
215		 */
216		if (msksz != -1 && msksz != ii->opi_addrc + intrsz)
217			panic("ofw_bus_setup_iinfo: bad interrupt-map-mask "
218			    "property!");
219	}
220}
221
222int
223ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg,
224    int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz,
225    phandle_t *iparent, void *maskbuf)
226{
227	int rv;
228
229	if (ii->opi_imapsz <= 0)
230		return (0);
231	KASSERT(regsz >= ii->opi_addrc,
232	    ("ofw_bus_lookup_imap: register size too small: %d < %d",
233		regsz, ii->opi_addrc));
234	rv = OF_getprop(node, "reg", reg, regsz);
235	if (rv < regsz)
236		panic("ofw_bus_lookup_imap: could not get reg property");
237	return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc,
238	    ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr,
239	    mintrsz, iparent));
240}
241
242/*
243 * Map an interrupt using the firmware reg, interrupt-map and
244 * interrupt-map-mask properties.
245 * The interrupt property to be mapped must be of size intrsz, and pointed to
246 * by intr.  The regs property of the node for which the mapping is done must
247 * be passed as regs. This property is an array of register specifications;
248 * the size of the address part of such a specification must be passed as
249 * physsz.  Only the first element of the property is used.
250 * imap and imapsz hold the interrupt mask and it's size.
251 * imapmsk is a pointer to the interrupt-map-mask property, which must have
252 * a size of physsz + intrsz; it may be NULL, in which case a full mask is
253 * assumed.
254 * maskbuf must point to a buffer of length physsz + intrsz.
255 * The interrupt is returned in result, which must point to a buffer of length
256 * rintrsz (which gives the expected size of the mapped interrupt).
257 * Returns 1 if a mapping was found, 0 otherwise.
258 */
259int
260ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz,
261    void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result,
262    int rintrsz, phandle_t *iparent)
263{
264	phandle_t parent;
265	uint8_t *ref = maskbuf;
266	uint8_t *uiintr = intr;
267	uint8_t *uiregs = regs;
268	uint8_t *uiimapmsk = imapmsk;
269	uint8_t *mptr;
270	pcell_t pintrsz;
271	int i, rsz, tsz;
272
273	rsz = -1;
274	if (imapmsk != NULL) {
275		for (i = 0; i < physsz; i++)
276			ref[i] = uiregs[i] & uiimapmsk[i];
277		for (i = 0; i < intrsz; i++)
278			ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i];
279	} else {
280		bcopy(regs, ref, physsz);
281		bcopy(intr, ref + physsz, intrsz);
282	}
283
284	mptr = imap;
285	i = imapsz;
286	while (i > 0) {
287		bcopy(mptr + physsz + intrsz, &parent, sizeof(parent));
288		if (OF_searchprop(parent, "#interrupt-cells",
289		    &pintrsz, sizeof(pintrsz)) == -1)
290			pintrsz = 1;	/* default */
291		pintrsz *= sizeof(pcell_t);
292
293		/* Compute the map stride size. */
294		tsz = physsz + intrsz + sizeof(phandle_t) + pintrsz;
295		KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map"));
296
297		/*
298		 * XXX: Apple hardware uses a second cell to set information
299		 * on the interrupt trigger type.  This information should
300		 * be used somewhere to program the PIC.
301		 */
302
303		if (bcmp(ref, mptr, physsz + intrsz) == 0) {
304			bcopy(mptr + physsz + intrsz + sizeof(parent),
305			    result, rintrsz);
306
307			if (iparent != NULL)
308				*iparent = parent;
309			return (1);
310		}
311		mptr += tsz;
312		i -= tsz;
313	}
314	return (0);
315}
316#endif /* !FDT */
317