1bba6f0a9SMarius Strobl /*- 294b4a038SNathan Whitehorn * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>. 3bba6f0a9SMarius Strobl * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org> 4bba6f0a9SMarius Strobl * All rights reserved. 5bba6f0a9SMarius Strobl * 6bba6f0a9SMarius Strobl * Redistribution and use in source and binary forms, with or without 7bba6f0a9SMarius Strobl * modification, are permitted provided that the following conditions 8bba6f0a9SMarius Strobl * are met: 9bba6f0a9SMarius Strobl * 1. Redistributions of source code must retain the above copyright 10bba6f0a9SMarius Strobl * notice, this list of conditions, and the following disclaimer, 11bba6f0a9SMarius Strobl * without modification, immediately at the beginning of the file. 12bba6f0a9SMarius Strobl * 2. Redistributions in binary form must reproduce the above copyright 13bba6f0a9SMarius Strobl * notice, this list of conditions and the following disclaimer in 14bba6f0a9SMarius Strobl * the documentation and/or other materials provided with the 15bba6f0a9SMarius Strobl * distribution. 16bba6f0a9SMarius Strobl * 17bba6f0a9SMarius Strobl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18bba6f0a9SMarius Strobl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19bba6f0a9SMarius Strobl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20bba6f0a9SMarius Strobl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21bba6f0a9SMarius Strobl * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22bba6f0a9SMarius Strobl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23bba6f0a9SMarius Strobl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24bba6f0a9SMarius Strobl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25bba6f0a9SMarius Strobl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26bba6f0a9SMarius Strobl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27bba6f0a9SMarius Strobl * SUCH DAMAGE. 28bba6f0a9SMarius Strobl */ 29bba6f0a9SMarius Strobl 30bba6f0a9SMarius Strobl #include <sys/cdefs.h> 31bba6f0a9SMarius Strobl __FBSDID("$FreeBSD$"); 32bba6f0a9SMarius Strobl 33bba6f0a9SMarius Strobl #include <sys/param.h> 34bba6f0a9SMarius Strobl #include <sys/systm.h> 35bba6f0a9SMarius Strobl #include <sys/bus.h> 36bba6f0a9SMarius Strobl #include <sys/errno.h> 3794b4a038SNathan Whitehorn #include <sys/libkern.h> 38bba6f0a9SMarius Strobl 3994b4a038SNathan Whitehorn #include <dev/ofw/ofw_bus.h> 40bba6f0a9SMarius Strobl #include <dev/ofw/ofw_bus_subr.h> 41bba6f0a9SMarius Strobl #include <dev/ofw/openfirm.h> 42bba6f0a9SMarius Strobl 43bba6f0a9SMarius Strobl #include "ofw_bus_if.h" 44bba6f0a9SMarius Strobl 45bba6f0a9SMarius Strobl int 46bba6f0a9SMarius Strobl ofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node) 47bba6f0a9SMarius Strobl { 48bba6f0a9SMarius Strobl 49bba6f0a9SMarius Strobl if (obd == NULL) 50bba6f0a9SMarius Strobl return (ENOMEM); 51bba6f0a9SMarius Strobl /* The 'name' property is considered mandatory. */ 52bba6f0a9SMarius Strobl if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1) 53bba6f0a9SMarius Strobl return (EINVAL); 54bba6f0a9SMarius Strobl OF_getprop_alloc(node, "compatible", 1, (void **)&obd->obd_compat); 55bba6f0a9SMarius Strobl OF_getprop_alloc(node, "device_type", 1, (void **)&obd->obd_type); 56bba6f0a9SMarius Strobl OF_getprop_alloc(node, "model", 1, (void **)&obd->obd_model); 57bba6f0a9SMarius Strobl obd->obd_node = node; 58bba6f0a9SMarius Strobl return (0); 59bba6f0a9SMarius Strobl } 60bba6f0a9SMarius Strobl 61bba6f0a9SMarius Strobl void 62bba6f0a9SMarius Strobl ofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd) 63bba6f0a9SMarius Strobl { 64bba6f0a9SMarius Strobl 65bba6f0a9SMarius Strobl if (obd == NULL) 66bba6f0a9SMarius Strobl return; 67bba6f0a9SMarius Strobl if (obd->obd_compat != NULL) 68bba6f0a9SMarius Strobl free(obd->obd_compat, M_OFWPROP); 69bba6f0a9SMarius Strobl if (obd->obd_model != NULL) 70bba6f0a9SMarius Strobl free(obd->obd_model, M_OFWPROP); 71bba6f0a9SMarius Strobl if (obd->obd_name != NULL) 72bba6f0a9SMarius Strobl free(obd->obd_name, M_OFWPROP); 73bba6f0a9SMarius Strobl if (obd->obd_type != NULL) 74bba6f0a9SMarius Strobl free(obd->obd_type, M_OFWPROP); 75bba6f0a9SMarius Strobl } 76bba6f0a9SMarius Strobl 7794b4a038SNathan Whitehorn int 7894b4a038SNathan Whitehorn ofw_bus_gen_child_pnpinfo_str(device_t cbdev, device_t child, char *buf, 7994b4a038SNathan Whitehorn size_t buflen) 8094b4a038SNathan Whitehorn { 8194b4a038SNathan Whitehorn if (ofw_bus_get_name(child) != NULL) { 8294b4a038SNathan Whitehorn strlcat(buf, "name=", buflen); 8394b4a038SNathan Whitehorn strlcat(buf, ofw_bus_get_name(child), buflen); 8494b4a038SNathan Whitehorn } 8594b4a038SNathan Whitehorn 8694b4a038SNathan Whitehorn if (ofw_bus_get_compat(child) != NULL) { 8794b4a038SNathan Whitehorn strlcat(buf, " compat=", buflen); 8894b4a038SNathan Whitehorn strlcat(buf, ofw_bus_get_compat(child), buflen); 8994b4a038SNathan Whitehorn } 9094b4a038SNathan Whitehorn 9194b4a038SNathan Whitehorn return (0); 9294b4a038SNathan Whitehorn }; 93bba6f0a9SMarius Strobl 94bba6f0a9SMarius Strobl const char * 95bba6f0a9SMarius Strobl ofw_bus_gen_get_compat(device_t bus, device_t dev) 96bba6f0a9SMarius Strobl { 97bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 98bba6f0a9SMarius Strobl 99bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 100bba6f0a9SMarius Strobl if (obd == NULL) 101bba6f0a9SMarius Strobl return (NULL); 102bba6f0a9SMarius Strobl return (obd->obd_compat); 103bba6f0a9SMarius Strobl } 104bba6f0a9SMarius Strobl 105bba6f0a9SMarius Strobl const char * 106bba6f0a9SMarius Strobl ofw_bus_gen_get_model(device_t bus, device_t dev) 107bba6f0a9SMarius Strobl { 108bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 109bba6f0a9SMarius Strobl 110bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 111bba6f0a9SMarius Strobl if (obd == NULL) 112bba6f0a9SMarius Strobl return (NULL); 113bba6f0a9SMarius Strobl return (obd->obd_model); 114bba6f0a9SMarius Strobl } 115bba6f0a9SMarius Strobl 116bba6f0a9SMarius Strobl const char * 117bba6f0a9SMarius Strobl ofw_bus_gen_get_name(device_t bus, device_t dev) 118bba6f0a9SMarius Strobl { 119bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 120bba6f0a9SMarius Strobl 121bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 122bba6f0a9SMarius Strobl if (obd == NULL) 123bba6f0a9SMarius Strobl return (NULL); 124bba6f0a9SMarius Strobl return (obd->obd_name); 125bba6f0a9SMarius Strobl } 126bba6f0a9SMarius Strobl 127bba6f0a9SMarius Strobl phandle_t 128bba6f0a9SMarius Strobl ofw_bus_gen_get_node(device_t bus, device_t dev) 129bba6f0a9SMarius Strobl { 130bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 131bba6f0a9SMarius Strobl 132bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 133bba6f0a9SMarius Strobl if (obd == NULL) 134bba6f0a9SMarius Strobl return (0); 135bba6f0a9SMarius Strobl return (obd->obd_node); 136bba6f0a9SMarius Strobl } 137bba6f0a9SMarius Strobl 138bba6f0a9SMarius Strobl const char * 139bba6f0a9SMarius Strobl ofw_bus_gen_get_type(device_t bus, device_t dev) 140bba6f0a9SMarius Strobl { 141bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 142bba6f0a9SMarius Strobl 143bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 144bba6f0a9SMarius Strobl if (obd == NULL) 145bba6f0a9SMarius Strobl return (NULL); 146bba6f0a9SMarius Strobl return (obd->obd_type); 147bba6f0a9SMarius Strobl } 14894b4a038SNathan Whitehorn 14994b4a038SNathan Whitehorn void 15094b4a038SNathan Whitehorn ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz) 15194b4a038SNathan Whitehorn { 15294b4a038SNathan Whitehorn pcell_t addrc; 15394b4a038SNathan Whitehorn int msksz; 15494b4a038SNathan Whitehorn 15594b4a038SNathan Whitehorn if (OF_getprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1) 15694b4a038SNathan Whitehorn addrc = 2; 15794b4a038SNathan Whitehorn ii->opi_addrc = addrc * sizeof(pcell_t); 15894b4a038SNathan Whitehorn 15994b4a038SNathan Whitehorn ii->opi_imapsz = OF_getprop_alloc(node, "interrupt-map", 1, 16094b4a038SNathan Whitehorn (void **)&ii->opi_imap); 16194b4a038SNathan Whitehorn if (ii->opi_imapsz > 0) { 16294b4a038SNathan Whitehorn msksz = OF_getprop_alloc(node, "interrupt-map-mask", 1, 16394b4a038SNathan Whitehorn (void **)&ii->opi_imapmsk); 16494b4a038SNathan Whitehorn /* 16594b4a038SNathan Whitehorn * Failure to get the mask is ignored; a full mask is used then. 16694b4a038SNathan Whitehorn * Barf on bad mask sizes, however. 16794b4a038SNathan Whitehorn */ 16894b4a038SNathan Whitehorn if (msksz != -1 && msksz != ii->opi_addrc + intrsz) { 16994b4a038SNathan Whitehorn panic("ofw_bus_setup_iinfo: bad interrupt-map-mask " 17094b4a038SNathan Whitehorn "property!"); 17194b4a038SNathan Whitehorn } 17294b4a038SNathan Whitehorn } 17394b4a038SNathan Whitehorn 17494b4a038SNathan Whitehorn } 17594b4a038SNathan Whitehorn 17694b4a038SNathan Whitehorn int 17794b4a038SNathan Whitehorn ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg, 17894b4a038SNathan Whitehorn int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz, 17994b4a038SNathan Whitehorn void *maskbuf) 18094b4a038SNathan Whitehorn { 18194b4a038SNathan Whitehorn int rv; 18294b4a038SNathan Whitehorn 18394b4a038SNathan Whitehorn if (ii->opi_imapsz <= 0) 18494b4a038SNathan Whitehorn return (0); 18594b4a038SNathan Whitehorn KASSERT(regsz >= ii->opi_addrc, 18694b4a038SNathan Whitehorn ("ofw_bus_lookup_imap: register size too small: %d < %d", 18794b4a038SNathan Whitehorn regsz, ii->opi_addrc)); 18894b4a038SNathan Whitehorn rv = OF_getprop(node, "reg", reg, regsz); 18994b4a038SNathan Whitehorn if (rv < regsz) 19094b4a038SNathan Whitehorn panic("ofw_bus_lookup_imap: could not get reg property"); 19194b4a038SNathan Whitehorn return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc, 19294b4a038SNathan Whitehorn ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr, 19394b4a038SNathan Whitehorn mintrsz)); 19494b4a038SNathan Whitehorn } 19594b4a038SNathan Whitehorn 19694b4a038SNathan Whitehorn /* 19794b4a038SNathan Whitehorn * Map an interrupt using the firmware reg, interrupt-map and 19894b4a038SNathan Whitehorn * interrupt-map-mask properties. 19994b4a038SNathan Whitehorn * The interrupt property to be mapped must be of size intrsz, and pointed to 20094b4a038SNathan Whitehorn * by intr. The regs property of the node for which the mapping is done must 20194b4a038SNathan Whitehorn * be passed as regs. This property is an array of register specifications; 20294b4a038SNathan Whitehorn * the size of the address part of such a specification must be passed as 20394b4a038SNathan Whitehorn * physsz. Only the first element of the property is used. 20494b4a038SNathan Whitehorn * imap and imapsz hold the interrupt mask and it's size. 20594b4a038SNathan Whitehorn * imapmsk is a pointer to the interrupt-map-mask property, which must have 20694b4a038SNathan Whitehorn * a size of physsz + intrsz; it may be NULL, in which case a full mask is 20794b4a038SNathan Whitehorn * assumed. 20894b4a038SNathan Whitehorn * maskbuf must point to a buffer of length physsz + intrsz. 20994b4a038SNathan Whitehorn * The interrupt is returned in result, which must point to a buffer of length 21094b4a038SNathan Whitehorn * rintrsz (which gives the expected size of the mapped interrupt). 21194b4a038SNathan Whitehorn * Returns 1 if a mapping was found, 0 otherwise. 21294b4a038SNathan Whitehorn */ 21394b4a038SNathan Whitehorn int 21494b4a038SNathan Whitehorn ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz, 21594b4a038SNathan Whitehorn void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result, 21694b4a038SNathan Whitehorn int rintrsz) 21794b4a038SNathan Whitehorn { 21894b4a038SNathan Whitehorn phandle_t parent; 21994b4a038SNathan Whitehorn u_int8_t *ref = maskbuf; 22094b4a038SNathan Whitehorn u_int8_t *uiintr = intr; 22194b4a038SNathan Whitehorn u_int8_t *uiregs = regs; 22294b4a038SNathan Whitehorn u_int8_t *uiimapmsk = imapmsk; 22394b4a038SNathan Whitehorn u_int8_t *mptr; 22494b4a038SNathan Whitehorn pcell_t pintrsz; 22594b4a038SNathan Whitehorn int i, rsz, tsz; 22694b4a038SNathan Whitehorn 22794b4a038SNathan Whitehorn rsz = -1; 22894b4a038SNathan Whitehorn if (imapmsk != NULL) { 22994b4a038SNathan Whitehorn for (i = 0; i < physsz; i++) 23094b4a038SNathan Whitehorn ref[i] = uiregs[i] & uiimapmsk[i]; 23194b4a038SNathan Whitehorn for (i = 0; i < intrsz; i++) 23294b4a038SNathan Whitehorn ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i]; 23394b4a038SNathan Whitehorn } else { 23494b4a038SNathan Whitehorn bcopy(regs, ref, physsz); 23594b4a038SNathan Whitehorn bcopy(intr, ref + physsz, intrsz); 23694b4a038SNathan Whitehorn } 23794b4a038SNathan Whitehorn 23894b4a038SNathan Whitehorn mptr = imap; 23994b4a038SNathan Whitehorn i = imapsz; 24094b4a038SNathan Whitehorn while (i > 0) { 24194b4a038SNathan Whitehorn bcopy(mptr + physsz + intrsz, &parent, sizeof(parent)); 242acb97117SNathan Whitehorn if (OF_searchprop(parent, "#interrupt-cells", 24394b4a038SNathan Whitehorn &pintrsz, sizeof(pintrsz)) == -1) 24494b4a038SNathan Whitehorn pintrsz = 1; /* default */ 24594b4a038SNathan Whitehorn pintrsz *= sizeof(pcell_t); 246acb97117SNathan Whitehorn 247acb97117SNathan Whitehorn /* Compute the map stride size */ 248acb97117SNathan Whitehorn tsz = physsz + intrsz + sizeof(phandle_t) + pintrsz; 249acb97117SNathan Whitehorn KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map")); 25094b4a038SNathan Whitehorn 25194b4a038SNathan Whitehorn /* 25294b4a038SNathan Whitehorn * XXX: Apple hardware uses a second cell to set information 25394b4a038SNathan Whitehorn * on the interrupt trigger type. This information should 25494b4a038SNathan Whitehorn * be used somewhere to program the PIC. 25594b4a038SNathan Whitehorn */ 25694b4a038SNathan Whitehorn 25794b4a038SNathan Whitehorn if (bcmp(ref, mptr, physsz + intrsz) == 0) { 25894b4a038SNathan Whitehorn bcopy(mptr + physsz + intrsz + sizeof(parent), 25994b4a038SNathan Whitehorn result, rintrsz); 26094b4a038SNathan Whitehorn return (1); 26194b4a038SNathan Whitehorn } 26294b4a038SNathan Whitehorn mptr += tsz; 26394b4a038SNathan Whitehorn i -= tsz; 26494b4a038SNathan Whitehorn } 26594b4a038SNathan Whitehorn return (0); 26694b4a038SNathan Whitehorn } 267