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 338297758aSRafal Jaworowski #include "opt_platform.h" 34bba6f0a9SMarius Strobl #include <sys/param.h> 35bba6f0a9SMarius Strobl #include <sys/systm.h> 36bba6f0a9SMarius Strobl #include <sys/bus.h> 37bba6f0a9SMarius Strobl #include <sys/errno.h> 3894b4a038SNathan Whitehorn #include <sys/libkern.h> 39bba6f0a9SMarius Strobl 40c47d4cdeSIan Lepore #include <machine/resource.h> 41c47d4cdeSIan Lepore 4294b4a038SNathan Whitehorn #include <dev/ofw/ofw_bus.h> 43bba6f0a9SMarius Strobl #include <dev/ofw/ofw_bus_subr.h> 44bba6f0a9SMarius Strobl #include <dev/ofw/openfirm.h> 45bba6f0a9SMarius Strobl 46bba6f0a9SMarius Strobl #include "ofw_bus_if.h" 47bba6f0a9SMarius Strobl 48bba6f0a9SMarius Strobl int 49bba6f0a9SMarius Strobl ofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node) 50bba6f0a9SMarius Strobl { 51bba6f0a9SMarius Strobl 52bba6f0a9SMarius Strobl if (obd == NULL) 53bba6f0a9SMarius Strobl return (ENOMEM); 54bba6f0a9SMarius Strobl /* The 'name' property is considered mandatory. */ 55bba6f0a9SMarius Strobl if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1) 56bba6f0a9SMarius Strobl return (EINVAL); 57bba6f0a9SMarius Strobl OF_getprop_alloc(node, "compatible", 1, (void **)&obd->obd_compat); 58bba6f0a9SMarius Strobl OF_getprop_alloc(node, "device_type", 1, (void **)&obd->obd_type); 59bba6f0a9SMarius Strobl OF_getprop_alloc(node, "model", 1, (void **)&obd->obd_model); 60e4be5a16SNathan Whitehorn OF_getprop_alloc(node, "status", 1, (void **)&obd->obd_status); 61bba6f0a9SMarius Strobl obd->obd_node = node; 62bba6f0a9SMarius Strobl return (0); 63bba6f0a9SMarius Strobl } 64bba6f0a9SMarius Strobl 65bba6f0a9SMarius Strobl void 66bba6f0a9SMarius Strobl ofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd) 67bba6f0a9SMarius Strobl { 68bba6f0a9SMarius Strobl 69bba6f0a9SMarius Strobl if (obd == NULL) 70bba6f0a9SMarius Strobl return; 71bba6f0a9SMarius Strobl if (obd->obd_compat != NULL) 72bba6f0a9SMarius Strobl free(obd->obd_compat, M_OFWPROP); 73bba6f0a9SMarius Strobl if (obd->obd_model != NULL) 74bba6f0a9SMarius Strobl free(obd->obd_model, M_OFWPROP); 75bba6f0a9SMarius Strobl if (obd->obd_name != NULL) 76bba6f0a9SMarius Strobl free(obd->obd_name, M_OFWPROP); 77bba6f0a9SMarius Strobl if (obd->obd_type != NULL) 78bba6f0a9SMarius Strobl free(obd->obd_type, M_OFWPROP); 79e4be5a16SNathan Whitehorn if (obd->obd_status != NULL) 80e4be5a16SNathan Whitehorn free(obd->obd_status, M_OFWPROP); 81bba6f0a9SMarius Strobl } 82bba6f0a9SMarius Strobl 8394b4a038SNathan Whitehorn int 8494b4a038SNathan Whitehorn ofw_bus_gen_child_pnpinfo_str(device_t cbdev, device_t child, char *buf, 8594b4a038SNathan Whitehorn size_t buflen) 8694b4a038SNathan Whitehorn { 87481d6b54SMarius Strobl 8894b4a038SNathan Whitehorn if (ofw_bus_get_name(child) != NULL) { 8994b4a038SNathan Whitehorn strlcat(buf, "name=", buflen); 9094b4a038SNathan Whitehorn strlcat(buf, ofw_bus_get_name(child), buflen); 9194b4a038SNathan Whitehorn } 9294b4a038SNathan Whitehorn 9394b4a038SNathan Whitehorn if (ofw_bus_get_compat(child) != NULL) { 9494b4a038SNathan Whitehorn strlcat(buf, " compat=", buflen); 9594b4a038SNathan Whitehorn strlcat(buf, ofw_bus_get_compat(child), buflen); 9694b4a038SNathan Whitehorn } 9794b4a038SNathan Whitehorn return (0); 9894b4a038SNathan Whitehorn }; 99bba6f0a9SMarius Strobl 100bba6f0a9SMarius Strobl const char * 101bba6f0a9SMarius Strobl ofw_bus_gen_get_compat(device_t bus, device_t dev) 102bba6f0a9SMarius Strobl { 103bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 104bba6f0a9SMarius Strobl 105bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 106bba6f0a9SMarius Strobl if (obd == NULL) 107bba6f0a9SMarius Strobl return (NULL); 108bba6f0a9SMarius Strobl return (obd->obd_compat); 109bba6f0a9SMarius Strobl } 110bba6f0a9SMarius Strobl 111bba6f0a9SMarius Strobl const char * 112bba6f0a9SMarius Strobl ofw_bus_gen_get_model(device_t bus, device_t dev) 113bba6f0a9SMarius Strobl { 114bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 115bba6f0a9SMarius Strobl 116bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 117bba6f0a9SMarius Strobl if (obd == NULL) 118bba6f0a9SMarius Strobl return (NULL); 119bba6f0a9SMarius Strobl return (obd->obd_model); 120bba6f0a9SMarius Strobl } 121bba6f0a9SMarius Strobl 122bba6f0a9SMarius Strobl const char * 123bba6f0a9SMarius Strobl ofw_bus_gen_get_name(device_t bus, device_t dev) 124bba6f0a9SMarius Strobl { 125bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 126bba6f0a9SMarius Strobl 127bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 128bba6f0a9SMarius Strobl if (obd == NULL) 129bba6f0a9SMarius Strobl return (NULL); 130bba6f0a9SMarius Strobl return (obd->obd_name); 131bba6f0a9SMarius Strobl } 132bba6f0a9SMarius Strobl 133bba6f0a9SMarius Strobl phandle_t 134bba6f0a9SMarius Strobl ofw_bus_gen_get_node(device_t bus, device_t dev) 135bba6f0a9SMarius Strobl { 136bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 137bba6f0a9SMarius Strobl 138bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 139bba6f0a9SMarius Strobl if (obd == NULL) 140bba6f0a9SMarius Strobl return (0); 141bba6f0a9SMarius Strobl return (obd->obd_node); 142bba6f0a9SMarius Strobl } 143bba6f0a9SMarius Strobl 144bba6f0a9SMarius Strobl const char * 145bba6f0a9SMarius Strobl ofw_bus_gen_get_type(device_t bus, device_t dev) 146bba6f0a9SMarius Strobl { 147bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 148bba6f0a9SMarius Strobl 149bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 150bba6f0a9SMarius Strobl if (obd == NULL) 151bba6f0a9SMarius Strobl return (NULL); 152bba6f0a9SMarius Strobl return (obd->obd_type); 153bba6f0a9SMarius Strobl } 15494b4a038SNathan Whitehorn 155e4be5a16SNathan Whitehorn const char * 156e4be5a16SNathan Whitehorn ofw_bus_get_status(device_t dev) 157e4be5a16SNathan Whitehorn { 158e4be5a16SNathan Whitehorn const struct ofw_bus_devinfo *obd; 159e4be5a16SNathan Whitehorn 160e4be5a16SNathan Whitehorn obd = OFW_BUS_GET_DEVINFO(device_get_parent(dev), dev); 161e4be5a16SNathan Whitehorn if (obd == NULL) 162e4be5a16SNathan Whitehorn return (NULL); 163e4be5a16SNathan Whitehorn 164e4be5a16SNathan Whitehorn return (obd->obd_status); 165e4be5a16SNathan Whitehorn } 166e4be5a16SNathan Whitehorn 167e4be5a16SNathan Whitehorn int 168e4be5a16SNathan Whitehorn ofw_bus_status_okay(device_t dev) 169e4be5a16SNathan Whitehorn { 170e4be5a16SNathan Whitehorn const char *status; 171e4be5a16SNathan Whitehorn 172e4be5a16SNathan Whitehorn status = ofw_bus_get_status(dev); 173e4be5a16SNathan Whitehorn if (status == NULL || strcmp(status, "okay") == 0) 174e4be5a16SNathan Whitehorn return (1); 175e4be5a16SNathan Whitehorn 176e4be5a16SNathan Whitehorn return (0); 177e4be5a16SNathan Whitehorn } 178e4be5a16SNathan Whitehorn 1798297758aSRafal Jaworowski int 1808297758aSRafal Jaworowski ofw_bus_is_compatible(device_t dev, const char *onecompat) 1818297758aSRafal Jaworowski { 1828297758aSRafal Jaworowski phandle_t node; 1838297758aSRafal Jaworowski const char *compat; 1848297758aSRafal Jaworowski int len, onelen, l; 1858297758aSRafal Jaworowski 1868297758aSRafal Jaworowski if ((compat = ofw_bus_get_compat(dev)) == NULL) 1878297758aSRafal Jaworowski return (0); 1888297758aSRafal Jaworowski 1890d8d9edaSNathan Whitehorn if ((node = ofw_bus_get_node(dev)) == -1) 1908297758aSRafal Jaworowski return (0); 1918297758aSRafal Jaworowski 1928297758aSRafal Jaworowski /* Get total 'compatible' prop len */ 1938297758aSRafal Jaworowski if ((len = OF_getproplen(node, "compatible")) <= 0) 1948297758aSRafal Jaworowski return (0); 1958297758aSRafal Jaworowski 1968297758aSRafal Jaworowski onelen = strlen(onecompat); 1978297758aSRafal Jaworowski 1988297758aSRafal Jaworowski while (len > 0) { 19926ca4965SHiroki Sato if (strlen(compat) == onelen && 20026ca4965SHiroki Sato strncasecmp(compat, onecompat, onelen) == 0) 2018297758aSRafal Jaworowski /* Found it. */ 2028297758aSRafal Jaworowski return (1); 2038297758aSRafal Jaworowski 2048297758aSRafal Jaworowski /* Slide to the next sub-string. */ 2058297758aSRafal Jaworowski l = strlen(compat) + 1; 2068297758aSRafal Jaworowski compat += l; 2078297758aSRafal Jaworowski len -= l; 2088297758aSRafal Jaworowski } 2098297758aSRafal Jaworowski return (0); 2108297758aSRafal Jaworowski } 2118297758aSRafal Jaworowski 2128297758aSRafal Jaworowski int 2138297758aSRafal Jaworowski ofw_bus_is_compatible_strict(device_t dev, const char *compatible) 2148297758aSRafal Jaworowski { 2158297758aSRafal Jaworowski const char *compat; 21626ca4965SHiroki Sato size_t len; 2178297758aSRafal Jaworowski 2188297758aSRafal Jaworowski if ((compat = ofw_bus_get_compat(dev)) == NULL) 2198297758aSRafal Jaworowski return (0); 2208297758aSRafal Jaworowski 22126ca4965SHiroki Sato len = strlen(compatible); 22226ca4965SHiroki Sato if (strlen(compat) == len && 22326ca4965SHiroki Sato strncasecmp(compat, compatible, len) == 0) 2248297758aSRafal Jaworowski return (1); 2258297758aSRafal Jaworowski 2268297758aSRafal Jaworowski return (0); 2278297758aSRafal Jaworowski } 2288297758aSRafal Jaworowski 229498fa7c1SIan Lepore const struct ofw_compat_data * 230498fa7c1SIan Lepore ofw_bus_search_compatible(device_t dev, const struct ofw_compat_data *compat) 231498fa7c1SIan Lepore { 232498fa7c1SIan Lepore 233498fa7c1SIan Lepore if (compat == NULL) 234498fa7c1SIan Lepore return NULL; 235498fa7c1SIan Lepore 236498fa7c1SIan Lepore for (; compat->ocd_str != NULL; ++compat) { 237498fa7c1SIan Lepore if (ofw_bus_is_compatible(dev, compat->ocd_str)) 238498fa7c1SIan Lepore break; 239498fa7c1SIan Lepore } 240498fa7c1SIan Lepore 241498fa7c1SIan Lepore return (compat); 242498fa7c1SIan Lepore } 243498fa7c1SIan Lepore 24426ca4965SHiroki Sato int 24526ca4965SHiroki Sato ofw_bus_has_prop(device_t dev, const char *propname) 24626ca4965SHiroki Sato { 24726ca4965SHiroki Sato phandle_t node; 24826ca4965SHiroki Sato 24926ca4965SHiroki Sato if ((node = ofw_bus_get_node(dev)) == -1) 25026ca4965SHiroki Sato return (0); 25126ca4965SHiroki Sato 25226ca4965SHiroki Sato return (OF_hasprop(node, propname)); 25326ca4965SHiroki Sato } 25426ca4965SHiroki Sato 25594b4a038SNathan Whitehorn void 25694b4a038SNathan Whitehorn ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz) 25794b4a038SNathan Whitehorn { 25894b4a038SNathan Whitehorn pcell_t addrc; 25994b4a038SNathan Whitehorn int msksz; 26094b4a038SNathan Whitehorn 261d3a0a0f3SNathan Whitehorn if (OF_getencprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1) 26294b4a038SNathan Whitehorn addrc = 2; 26394b4a038SNathan Whitehorn ii->opi_addrc = addrc * sizeof(pcell_t); 26494b4a038SNathan Whitehorn 265d3a0a0f3SNathan Whitehorn ii->opi_imapsz = OF_getencprop_alloc(node, "interrupt-map", 1, 26694b4a038SNathan Whitehorn (void **)&ii->opi_imap); 26794b4a038SNathan Whitehorn if (ii->opi_imapsz > 0) { 268d3a0a0f3SNathan Whitehorn msksz = OF_getencprop_alloc(node, "interrupt-map-mask", 1, 26994b4a038SNathan Whitehorn (void **)&ii->opi_imapmsk); 27094b4a038SNathan Whitehorn /* 271481d6b54SMarius Strobl * Failure to get the mask is ignored; a full mask is used 272481d6b54SMarius Strobl * then. We barf on bad mask sizes, however. 27394b4a038SNathan Whitehorn */ 274481d6b54SMarius Strobl if (msksz != -1 && msksz != ii->opi_addrc + intrsz) 27594b4a038SNathan Whitehorn panic("ofw_bus_setup_iinfo: bad interrupt-map-mask " 27694b4a038SNathan Whitehorn "property!"); 27794b4a038SNathan Whitehorn } 27894b4a038SNathan Whitehorn } 27994b4a038SNathan Whitehorn 28094b4a038SNathan Whitehorn int 28194b4a038SNathan Whitehorn ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg, 28294b4a038SNathan Whitehorn int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz, 28395e3bfe8SNathan Whitehorn phandle_t *iparent) 28494b4a038SNathan Whitehorn { 28595e3bfe8SNathan Whitehorn uint8_t maskbuf[regsz + pintrsz]; 28694b4a038SNathan Whitehorn int rv; 28794b4a038SNathan Whitehorn 28894b4a038SNathan Whitehorn if (ii->opi_imapsz <= 0) 28994b4a038SNathan Whitehorn return (0); 29094b4a038SNathan Whitehorn KASSERT(regsz >= ii->opi_addrc, 29194b4a038SNathan Whitehorn ("ofw_bus_lookup_imap: register size too small: %d < %d", 29294b4a038SNathan Whitehorn regsz, ii->opi_addrc)); 2936064b6acSNathan Whitehorn if (node != -1) { 294d3a0a0f3SNathan Whitehorn rv = OF_getencprop(node, "reg", reg, regsz); 29594b4a038SNathan Whitehorn if (rv < regsz) 2966064b6acSNathan Whitehorn panic("ofw_bus_lookup_imap: cannot get reg property"); 2976064b6acSNathan Whitehorn } 29894b4a038SNathan Whitehorn return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc, 29994b4a038SNathan Whitehorn ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr, 300eaef5f0aSNathan Whitehorn mintrsz, iparent)); 30194b4a038SNathan Whitehorn } 30294b4a038SNathan Whitehorn 30394b4a038SNathan Whitehorn /* 30494b4a038SNathan Whitehorn * Map an interrupt using the firmware reg, interrupt-map and 30594b4a038SNathan Whitehorn * interrupt-map-mask properties. 30694b4a038SNathan Whitehorn * The interrupt property to be mapped must be of size intrsz, and pointed to 30794b4a038SNathan Whitehorn * by intr. The regs property of the node for which the mapping is done must 30894b4a038SNathan Whitehorn * be passed as regs. This property is an array of register specifications; 30994b4a038SNathan Whitehorn * the size of the address part of such a specification must be passed as 31094b4a038SNathan Whitehorn * physsz. Only the first element of the property is used. 31194b4a038SNathan Whitehorn * imap and imapsz hold the interrupt mask and it's size. 31294b4a038SNathan Whitehorn * imapmsk is a pointer to the interrupt-map-mask property, which must have 31394b4a038SNathan Whitehorn * a size of physsz + intrsz; it may be NULL, in which case a full mask is 31494b4a038SNathan Whitehorn * assumed. 31594b4a038SNathan Whitehorn * maskbuf must point to a buffer of length physsz + intrsz. 31694b4a038SNathan Whitehorn * The interrupt is returned in result, which must point to a buffer of length 31794b4a038SNathan Whitehorn * rintrsz (which gives the expected size of the mapped interrupt). 318cb6d9d6cSNathan Whitehorn * Returns number of cells in the interrupt if a mapping was found, 0 otherwise. 31994b4a038SNathan Whitehorn */ 32094b4a038SNathan Whitehorn int 32194b4a038SNathan Whitehorn ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz, 32294b4a038SNathan Whitehorn void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result, 323eaef5f0aSNathan Whitehorn int rintrsz, phandle_t *iparent) 32494b4a038SNathan Whitehorn { 32594b4a038SNathan Whitehorn phandle_t parent; 326481d6b54SMarius Strobl uint8_t *ref = maskbuf; 327481d6b54SMarius Strobl uint8_t *uiintr = intr; 328481d6b54SMarius Strobl uint8_t *uiregs = regs; 329481d6b54SMarius Strobl uint8_t *uiimapmsk = imapmsk; 330481d6b54SMarius Strobl uint8_t *mptr; 33194b4a038SNathan Whitehorn pcell_t pintrsz; 33294b4a038SNathan Whitehorn int i, rsz, tsz; 33394b4a038SNathan Whitehorn 33494b4a038SNathan Whitehorn rsz = -1; 33594b4a038SNathan Whitehorn if (imapmsk != NULL) { 33694b4a038SNathan Whitehorn for (i = 0; i < physsz; i++) 33794b4a038SNathan Whitehorn ref[i] = uiregs[i] & uiimapmsk[i]; 33894b4a038SNathan Whitehorn for (i = 0; i < intrsz; i++) 33994b4a038SNathan Whitehorn ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i]; 34094b4a038SNathan Whitehorn } else { 34194b4a038SNathan Whitehorn bcopy(regs, ref, physsz); 34294b4a038SNathan Whitehorn bcopy(intr, ref + physsz, intrsz); 34394b4a038SNathan Whitehorn } 34494b4a038SNathan Whitehorn 34594b4a038SNathan Whitehorn mptr = imap; 34694b4a038SNathan Whitehorn i = imapsz; 34794b4a038SNathan Whitehorn while (i > 0) { 34894b4a038SNathan Whitehorn bcopy(mptr + physsz + intrsz, &parent, sizeof(parent)); 349752ba930SIan Lepore if (OF_searchencprop(OF_node_from_xref(parent), 350d3a0a0f3SNathan Whitehorn "#interrupt-cells", &pintrsz, sizeof(pintrsz)) == -1) 35194b4a038SNathan Whitehorn pintrsz = 1; /* default */ 35294b4a038SNathan Whitehorn pintrsz *= sizeof(pcell_t); 353acb97117SNathan Whitehorn 354481d6b54SMarius Strobl /* Compute the map stride size. */ 355acb97117SNathan Whitehorn tsz = physsz + intrsz + sizeof(phandle_t) + pintrsz; 356acb97117SNathan Whitehorn KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map")); 35794b4a038SNathan Whitehorn 35894b4a038SNathan Whitehorn if (bcmp(ref, mptr, physsz + intrsz) == 0) { 35994b4a038SNathan Whitehorn bcopy(mptr + physsz + intrsz + sizeof(parent), 360cb6d9d6cSNathan Whitehorn result, MIN(rintrsz, pintrsz)); 361eaef5f0aSNathan Whitehorn 362eaef5f0aSNathan Whitehorn if (iparent != NULL) 363eaef5f0aSNathan Whitehorn *iparent = parent; 364cb6d9d6cSNathan Whitehorn return (pintrsz/sizeof(pcell_t)); 36594b4a038SNathan Whitehorn } 36694b4a038SNathan Whitehorn mptr += tsz; 36794b4a038SNathan Whitehorn i -= tsz; 36894b4a038SNathan Whitehorn } 36994b4a038SNathan Whitehorn return (0); 37094b4a038SNathan Whitehorn } 3716064b6acSNathan Whitehorn 372c47d4cdeSIan Lepore int 373*4b3d9160SZbigniew Bodek ofw_bus_reg_to_rl(device_t dev, phandle_t node, pcell_t acells, pcell_t scells, 374*4b3d9160SZbigniew Bodek struct resource_list *rl) 375*4b3d9160SZbigniew Bodek { 376*4b3d9160SZbigniew Bodek uint64_t phys, size; 377*4b3d9160SZbigniew Bodek ssize_t i, j, rid, nreg, ret; 378*4b3d9160SZbigniew Bodek uint32_t *reg; 379*4b3d9160SZbigniew Bodek char *name; 380*4b3d9160SZbigniew Bodek 381*4b3d9160SZbigniew Bodek /* 382*4b3d9160SZbigniew Bodek * This may be just redundant when having ofw_bus_devinfo 383*4b3d9160SZbigniew Bodek * but makes this routine independent of it. 384*4b3d9160SZbigniew Bodek */ 385*4b3d9160SZbigniew Bodek ret = OF_getencprop_alloc(node, "name", sizeof(*name), (void **)&name); 386*4b3d9160SZbigniew Bodek if (ret == -1) 387*4b3d9160SZbigniew Bodek name = NULL; 388*4b3d9160SZbigniew Bodek 389*4b3d9160SZbigniew Bodek ret = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)®); 390*4b3d9160SZbigniew Bodek nreg = (ret == -1) ? 0 : ret; 391*4b3d9160SZbigniew Bodek 392*4b3d9160SZbigniew Bodek if (nreg % (acells + scells) != 0) { 393*4b3d9160SZbigniew Bodek if (bootverbose) 394*4b3d9160SZbigniew Bodek device_printf(dev, "Malformed reg property on <%s>\n", 395*4b3d9160SZbigniew Bodek (name == NULL) ? "unknown" : name); 396*4b3d9160SZbigniew Bodek nreg = 0; 397*4b3d9160SZbigniew Bodek } 398*4b3d9160SZbigniew Bodek 399*4b3d9160SZbigniew Bodek for (i = 0, rid = 0; i < nreg; i += acells + scells, rid++) { 400*4b3d9160SZbigniew Bodek phys = size = 0; 401*4b3d9160SZbigniew Bodek for (j = 0; j < acells; j++) { 402*4b3d9160SZbigniew Bodek phys <<= 32; 403*4b3d9160SZbigniew Bodek phys |= reg[i + j]; 404*4b3d9160SZbigniew Bodek } 405*4b3d9160SZbigniew Bodek for (j = 0; j < scells; j++) { 406*4b3d9160SZbigniew Bodek size <<= 32; 407*4b3d9160SZbigniew Bodek size |= reg[i + acells + j]; 408*4b3d9160SZbigniew Bodek } 409*4b3d9160SZbigniew Bodek /* Skip the dummy reg property of glue devices like ssm(4). */ 410*4b3d9160SZbigniew Bodek if (size != 0) 411*4b3d9160SZbigniew Bodek resource_list_add(rl, SYS_RES_MEMORY, rid, 412*4b3d9160SZbigniew Bodek phys, phys + size - 1, size); 413*4b3d9160SZbigniew Bodek } 414*4b3d9160SZbigniew Bodek free(name, M_OFWPROP); 415*4b3d9160SZbigniew Bodek free(reg, M_OFWPROP); 416*4b3d9160SZbigniew Bodek 417*4b3d9160SZbigniew Bodek return (0); 418*4b3d9160SZbigniew Bodek } 419*4b3d9160SZbigniew Bodek 420*4b3d9160SZbigniew Bodek int 421c47d4cdeSIan Lepore ofw_bus_intr_to_rl(device_t dev, phandle_t node, struct resource_list *rl) 422c47d4cdeSIan Lepore { 423c47d4cdeSIan Lepore phandle_t iparent; 424c47d4cdeSIan Lepore uint32_t icells, *intr; 425c47d4cdeSIan Lepore int err, i, irqnum, nintr, rid; 426c47d4cdeSIan Lepore boolean_t extended; 427c47d4cdeSIan Lepore 428c47d4cdeSIan Lepore nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr), 429c47d4cdeSIan Lepore (void **)&intr); 430c47d4cdeSIan Lepore if (nintr > 0) { 431c47d4cdeSIan Lepore if (OF_searchencprop(node, "interrupt-parent", &iparent, 432c47d4cdeSIan Lepore sizeof(iparent)) == -1) { 43308b96b9fSNathan Whitehorn for (iparent = node; iparent != 0; 43408b96b9fSNathan Whitehorn iparent = OF_parent(node)) { 43508b96b9fSNathan Whitehorn if (OF_hasprop(iparent, "interrupt-controller")) 43608b96b9fSNathan Whitehorn break; 43708b96b9fSNathan Whitehorn } 43808b96b9fSNathan Whitehorn if (iparent == 0) { 439c47d4cdeSIan Lepore device_printf(dev, "No interrupt-parent found, " 440c47d4cdeSIan Lepore "assuming direct parent\n"); 441c47d4cdeSIan Lepore iparent = OF_parent(node); 442c47d4cdeSIan Lepore } 44308b96b9fSNathan Whitehorn iparent = OF_xref_from_node(iparent); 44408b96b9fSNathan Whitehorn } 445c47d4cdeSIan Lepore if (OF_searchencprop(OF_node_from_xref(iparent), 446c47d4cdeSIan Lepore "#interrupt-cells", &icells, sizeof(icells)) == -1) { 447c47d4cdeSIan Lepore device_printf(dev, "Missing #interrupt-cells " 448c47d4cdeSIan Lepore "property, assuming <1>\n"); 449c47d4cdeSIan Lepore icells = 1; 450c47d4cdeSIan Lepore } 451c47d4cdeSIan Lepore if (icells < 1 || icells > nintr) { 452c47d4cdeSIan Lepore device_printf(dev, "Invalid #interrupt-cells property " 453c47d4cdeSIan Lepore "value <%d>, assuming <1>\n", icells); 454c47d4cdeSIan Lepore icells = 1; 455c47d4cdeSIan Lepore } 456c47d4cdeSIan Lepore extended = false; 457c47d4cdeSIan Lepore } else { 458c47d4cdeSIan Lepore nintr = OF_getencprop_alloc(node, "interrupts-extended", 459c47d4cdeSIan Lepore sizeof(*intr), (void **)&intr); 460c47d4cdeSIan Lepore if (nintr <= 0) 461c47d4cdeSIan Lepore return (0); 462c47d4cdeSIan Lepore extended = true; 463c47d4cdeSIan Lepore } 464c47d4cdeSIan Lepore err = 0; 465c47d4cdeSIan Lepore rid = 0; 466c47d4cdeSIan Lepore for (i = 0; i < nintr; i += icells) { 467c47d4cdeSIan Lepore if (extended) { 468c47d4cdeSIan Lepore iparent = intr[i++]; 469c47d4cdeSIan Lepore if (OF_searchencprop(OF_node_from_xref(iparent), 470c47d4cdeSIan Lepore "#interrupt-cells", &icells, sizeof(icells)) == -1) { 471c47d4cdeSIan Lepore device_printf(dev, "Missing #interrupt-cells " 472c47d4cdeSIan Lepore "property\n"); 473c47d4cdeSIan Lepore err = ENOENT; 474c47d4cdeSIan Lepore break; 475c47d4cdeSIan Lepore } 476c47d4cdeSIan Lepore if (icells < 1 || (i + icells) > nintr) { 477c47d4cdeSIan Lepore device_printf(dev, "Invalid #interrupt-cells " 478c47d4cdeSIan Lepore "property value <%d>\n", icells); 479c47d4cdeSIan Lepore err = ERANGE; 480c47d4cdeSIan Lepore break; 481c47d4cdeSIan Lepore } 482c47d4cdeSIan Lepore } 483c47d4cdeSIan Lepore irqnum = ofw_bus_map_intr(dev, iparent, icells, &intr[i]); 484c47d4cdeSIan Lepore resource_list_add(rl, SYS_RES_IRQ, rid++, irqnum, irqnum, 1); 485c47d4cdeSIan Lepore } 486c47d4cdeSIan Lepore free(intr, M_OFWPROP); 487c47d4cdeSIan Lepore return (err); 488c47d4cdeSIan Lepore } 48908b96b9fSNathan Whitehorn 490