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); 173f3856d8fSAndrew Turner if (status == NULL || strcmp(status, "okay") == 0 || 174f3856d8fSAndrew Turner strcmp(status, "ok") == 0) 175e4be5a16SNathan Whitehorn return (1); 176e4be5a16SNathan Whitehorn 177e4be5a16SNathan Whitehorn return (0); 178e4be5a16SNathan Whitehorn } 179e4be5a16SNathan Whitehorn 18072a638c7SAndrew Turner static int 18172a638c7SAndrew Turner ofw_bus_node_is_compatible(const char *compat, int len, const char *onecompat) 18272a638c7SAndrew Turner { 18372a638c7SAndrew Turner int onelen, l, ret; 18472a638c7SAndrew Turner 18572a638c7SAndrew Turner onelen = strlen(onecompat); 18672a638c7SAndrew Turner 18772a638c7SAndrew Turner ret = 0; 18872a638c7SAndrew Turner while (len > 0) { 18972a638c7SAndrew Turner if (strlen(compat) == onelen && 19072a638c7SAndrew Turner strncasecmp(compat, onecompat, onelen) == 0) { 19172a638c7SAndrew Turner /* Found it. */ 19272a638c7SAndrew Turner ret = 1; 19372a638c7SAndrew Turner break; 19472a638c7SAndrew Turner } 19572a638c7SAndrew Turner 19672a638c7SAndrew Turner /* Slide to the next sub-string. */ 19772a638c7SAndrew Turner l = strlen(compat) + 1; 19872a638c7SAndrew Turner compat += l; 19972a638c7SAndrew Turner len -= l; 20072a638c7SAndrew Turner } 20172a638c7SAndrew Turner 20272a638c7SAndrew Turner return (ret); 20372a638c7SAndrew Turner } 20472a638c7SAndrew Turner 2058297758aSRafal Jaworowski int 2068297758aSRafal Jaworowski ofw_bus_is_compatible(device_t dev, const char *onecompat) 2078297758aSRafal Jaworowski { 2088297758aSRafal Jaworowski phandle_t node; 2098297758aSRafal Jaworowski const char *compat; 21072a638c7SAndrew Turner int len; 2118297758aSRafal Jaworowski 2128297758aSRafal Jaworowski if ((compat = ofw_bus_get_compat(dev)) == NULL) 2138297758aSRafal Jaworowski return (0); 2148297758aSRafal Jaworowski 2150d8d9edaSNathan Whitehorn if ((node = ofw_bus_get_node(dev)) == -1) 2168297758aSRafal Jaworowski return (0); 2178297758aSRafal Jaworowski 2188297758aSRafal Jaworowski /* Get total 'compatible' prop len */ 2198297758aSRafal Jaworowski if ((len = OF_getproplen(node, "compatible")) <= 0) 2208297758aSRafal Jaworowski return (0); 2218297758aSRafal Jaworowski 22272a638c7SAndrew Turner return (ofw_bus_node_is_compatible(compat, len, onecompat)); 2238297758aSRafal Jaworowski } 2248297758aSRafal Jaworowski 2258297758aSRafal Jaworowski int 2268297758aSRafal Jaworowski ofw_bus_is_compatible_strict(device_t dev, const char *compatible) 2278297758aSRafal Jaworowski { 2288297758aSRafal Jaworowski const char *compat; 22926ca4965SHiroki Sato size_t len; 2308297758aSRafal Jaworowski 2318297758aSRafal Jaworowski if ((compat = ofw_bus_get_compat(dev)) == NULL) 2328297758aSRafal Jaworowski return (0); 2338297758aSRafal Jaworowski 23426ca4965SHiroki Sato len = strlen(compatible); 23526ca4965SHiroki Sato if (strlen(compat) == len && 23626ca4965SHiroki Sato strncasecmp(compat, compatible, len) == 0) 2378297758aSRafal Jaworowski return (1); 2388297758aSRafal Jaworowski 2398297758aSRafal Jaworowski return (0); 2408297758aSRafal Jaworowski } 2418297758aSRafal Jaworowski 242498fa7c1SIan Lepore const struct ofw_compat_data * 243498fa7c1SIan Lepore ofw_bus_search_compatible(device_t dev, const struct ofw_compat_data *compat) 244498fa7c1SIan Lepore { 245498fa7c1SIan Lepore 246498fa7c1SIan Lepore if (compat == NULL) 247498fa7c1SIan Lepore return NULL; 248498fa7c1SIan Lepore 249498fa7c1SIan Lepore for (; compat->ocd_str != NULL; ++compat) { 250498fa7c1SIan Lepore if (ofw_bus_is_compatible(dev, compat->ocd_str)) 251498fa7c1SIan Lepore break; 252498fa7c1SIan Lepore } 253498fa7c1SIan Lepore 254498fa7c1SIan Lepore return (compat); 255498fa7c1SIan Lepore } 256498fa7c1SIan Lepore 25726ca4965SHiroki Sato int 25826ca4965SHiroki Sato ofw_bus_has_prop(device_t dev, const char *propname) 25926ca4965SHiroki Sato { 26026ca4965SHiroki Sato phandle_t node; 26126ca4965SHiroki Sato 26226ca4965SHiroki Sato if ((node = ofw_bus_get_node(dev)) == -1) 26326ca4965SHiroki Sato return (0); 26426ca4965SHiroki Sato 26526ca4965SHiroki Sato return (OF_hasprop(node, propname)); 26626ca4965SHiroki Sato } 26726ca4965SHiroki Sato 26894b4a038SNathan Whitehorn void 26994b4a038SNathan Whitehorn ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz) 27094b4a038SNathan Whitehorn { 27194b4a038SNathan Whitehorn pcell_t addrc; 27294b4a038SNathan Whitehorn int msksz; 27394b4a038SNathan Whitehorn 274d3a0a0f3SNathan Whitehorn if (OF_getencprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1) 27594b4a038SNathan Whitehorn addrc = 2; 27694b4a038SNathan Whitehorn ii->opi_addrc = addrc * sizeof(pcell_t); 27794b4a038SNathan Whitehorn 278d3a0a0f3SNathan Whitehorn ii->opi_imapsz = OF_getencprop_alloc(node, "interrupt-map", 1, 27994b4a038SNathan Whitehorn (void **)&ii->opi_imap); 28094b4a038SNathan Whitehorn if (ii->opi_imapsz > 0) { 281d3a0a0f3SNathan Whitehorn msksz = OF_getencprop_alloc(node, "interrupt-map-mask", 1, 28294b4a038SNathan Whitehorn (void **)&ii->opi_imapmsk); 28394b4a038SNathan Whitehorn /* 284481d6b54SMarius Strobl * Failure to get the mask is ignored; a full mask is used 285481d6b54SMarius Strobl * then. We barf on bad mask sizes, however. 28694b4a038SNathan Whitehorn */ 287481d6b54SMarius Strobl if (msksz != -1 && msksz != ii->opi_addrc + intrsz) 28894b4a038SNathan Whitehorn panic("ofw_bus_setup_iinfo: bad interrupt-map-mask " 28994b4a038SNathan Whitehorn "property!"); 29094b4a038SNathan Whitehorn } 29194b4a038SNathan Whitehorn } 29294b4a038SNathan Whitehorn 29394b4a038SNathan Whitehorn int 29494b4a038SNathan Whitehorn ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg, 29594b4a038SNathan Whitehorn int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz, 29695e3bfe8SNathan Whitehorn phandle_t *iparent) 29794b4a038SNathan Whitehorn { 29895e3bfe8SNathan Whitehorn uint8_t maskbuf[regsz + pintrsz]; 29994b4a038SNathan Whitehorn int rv; 30094b4a038SNathan Whitehorn 30194b4a038SNathan Whitehorn if (ii->opi_imapsz <= 0) 30294b4a038SNathan Whitehorn return (0); 30394b4a038SNathan Whitehorn KASSERT(regsz >= ii->opi_addrc, 30494b4a038SNathan Whitehorn ("ofw_bus_lookup_imap: register size too small: %d < %d", 30594b4a038SNathan Whitehorn regsz, ii->opi_addrc)); 3066064b6acSNathan Whitehorn if (node != -1) { 307d3a0a0f3SNathan Whitehorn rv = OF_getencprop(node, "reg", reg, regsz); 30894b4a038SNathan Whitehorn if (rv < regsz) 3096064b6acSNathan Whitehorn panic("ofw_bus_lookup_imap: cannot get reg property"); 3106064b6acSNathan Whitehorn } 31194b4a038SNathan Whitehorn return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc, 31294b4a038SNathan Whitehorn ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr, 313eaef5f0aSNathan Whitehorn mintrsz, iparent)); 31494b4a038SNathan Whitehorn } 31594b4a038SNathan Whitehorn 31694b4a038SNathan Whitehorn /* 31794b4a038SNathan Whitehorn * Map an interrupt using the firmware reg, interrupt-map and 31894b4a038SNathan Whitehorn * interrupt-map-mask properties. 31994b4a038SNathan Whitehorn * The interrupt property to be mapped must be of size intrsz, and pointed to 32094b4a038SNathan Whitehorn * by intr. The regs property of the node for which the mapping is done must 32194b4a038SNathan Whitehorn * be passed as regs. This property is an array of register specifications; 32294b4a038SNathan Whitehorn * the size of the address part of such a specification must be passed as 32394b4a038SNathan Whitehorn * physsz. Only the first element of the property is used. 32494b4a038SNathan Whitehorn * imap and imapsz hold the interrupt mask and it's size. 32594b4a038SNathan Whitehorn * imapmsk is a pointer to the interrupt-map-mask property, which must have 32694b4a038SNathan Whitehorn * a size of physsz + intrsz; it may be NULL, in which case a full mask is 32794b4a038SNathan Whitehorn * assumed. 32894b4a038SNathan Whitehorn * maskbuf must point to a buffer of length physsz + intrsz. 32994b4a038SNathan Whitehorn * The interrupt is returned in result, which must point to a buffer of length 33094b4a038SNathan Whitehorn * rintrsz (which gives the expected size of the mapped interrupt). 331cb6d9d6cSNathan Whitehorn * Returns number of cells in the interrupt if a mapping was found, 0 otherwise. 33294b4a038SNathan Whitehorn */ 33394b4a038SNathan Whitehorn int 33494b4a038SNathan Whitehorn ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz, 33594b4a038SNathan Whitehorn void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result, 336eaef5f0aSNathan Whitehorn int rintrsz, phandle_t *iparent) 33794b4a038SNathan Whitehorn { 33894b4a038SNathan Whitehorn phandle_t parent; 339481d6b54SMarius Strobl uint8_t *ref = maskbuf; 340481d6b54SMarius Strobl uint8_t *uiintr = intr; 341481d6b54SMarius Strobl uint8_t *uiregs = regs; 342481d6b54SMarius Strobl uint8_t *uiimapmsk = imapmsk; 343481d6b54SMarius Strobl uint8_t *mptr; 344122493cfSAndrew Turner pcell_t paddrsz; 34594b4a038SNathan Whitehorn pcell_t pintrsz; 34694b4a038SNathan Whitehorn int i, rsz, tsz; 34794b4a038SNathan Whitehorn 34894b4a038SNathan Whitehorn rsz = -1; 34994b4a038SNathan Whitehorn if (imapmsk != NULL) { 35094b4a038SNathan Whitehorn for (i = 0; i < physsz; i++) 35194b4a038SNathan Whitehorn ref[i] = uiregs[i] & uiimapmsk[i]; 35294b4a038SNathan Whitehorn for (i = 0; i < intrsz; i++) 35394b4a038SNathan Whitehorn ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i]; 35494b4a038SNathan Whitehorn } else { 35594b4a038SNathan Whitehorn bcopy(regs, ref, physsz); 35694b4a038SNathan Whitehorn bcopy(intr, ref + physsz, intrsz); 35794b4a038SNathan Whitehorn } 35894b4a038SNathan Whitehorn 35994b4a038SNathan Whitehorn mptr = imap; 36094b4a038SNathan Whitehorn i = imapsz; 361122493cfSAndrew Turner paddrsz = 0; 36294b4a038SNathan Whitehorn while (i > 0) { 36394b4a038SNathan Whitehorn bcopy(mptr + physsz + intrsz, &parent, sizeof(parent)); 3649f4a7eaeSNathan Whitehorn #ifndef OFW_IMAP_NO_IPARENT_ADDR_CELLS 365122493cfSAndrew Turner /* 3669f4a7eaeSNathan Whitehorn * Find if we need to read the parent address data. 3679f4a7eaeSNathan Whitehorn * CHRP-derived OF bindings, including ePAPR-compliant FDTs, 3689f4a7eaeSNathan Whitehorn * use this as an optional part of the specifier. 369122493cfSAndrew Turner */ 370122493cfSAndrew Turner if (OF_getencprop(OF_node_from_xref(parent), 371122493cfSAndrew Turner "#address-cells", &paddrsz, sizeof(paddrsz)) == -1) 372122493cfSAndrew Turner paddrsz = 0; /* default */ 373122493cfSAndrew Turner paddrsz *= sizeof(pcell_t); 374122493cfSAndrew Turner #endif 375122493cfSAndrew Turner 376752ba930SIan Lepore if (OF_searchencprop(OF_node_from_xref(parent), 377d3a0a0f3SNathan Whitehorn "#interrupt-cells", &pintrsz, sizeof(pintrsz)) == -1) 37894b4a038SNathan Whitehorn pintrsz = 1; /* default */ 37994b4a038SNathan Whitehorn pintrsz *= sizeof(pcell_t); 380acb97117SNathan Whitehorn 381481d6b54SMarius Strobl /* Compute the map stride size. */ 382122493cfSAndrew Turner tsz = physsz + intrsz + sizeof(phandle_t) + paddrsz + pintrsz; 383acb97117SNathan Whitehorn KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map")); 38494b4a038SNathan Whitehorn 38594b4a038SNathan Whitehorn if (bcmp(ref, mptr, physsz + intrsz) == 0) { 386122493cfSAndrew Turner bcopy(mptr + physsz + intrsz + sizeof(parent) + paddrsz, 387cb6d9d6cSNathan Whitehorn result, MIN(rintrsz, pintrsz)); 388eaef5f0aSNathan Whitehorn 389eaef5f0aSNathan Whitehorn if (iparent != NULL) 390eaef5f0aSNathan Whitehorn *iparent = parent; 391cb6d9d6cSNathan Whitehorn return (pintrsz/sizeof(pcell_t)); 39294b4a038SNathan Whitehorn } 39394b4a038SNathan Whitehorn mptr += tsz; 39494b4a038SNathan Whitehorn i -= tsz; 39594b4a038SNathan Whitehorn } 39694b4a038SNathan Whitehorn return (0); 39794b4a038SNathan Whitehorn } 3986064b6acSNathan Whitehorn 399c47d4cdeSIan Lepore int 4004b3d9160SZbigniew Bodek ofw_bus_reg_to_rl(device_t dev, phandle_t node, pcell_t acells, pcell_t scells, 4014b3d9160SZbigniew Bodek struct resource_list *rl) 4024b3d9160SZbigniew Bodek { 4034b3d9160SZbigniew Bodek uint64_t phys, size; 4044b3d9160SZbigniew Bodek ssize_t i, j, rid, nreg, ret; 4054b3d9160SZbigniew Bodek uint32_t *reg; 4064b3d9160SZbigniew Bodek char *name; 4074b3d9160SZbigniew Bodek 4084b3d9160SZbigniew Bodek /* 4094b3d9160SZbigniew Bodek * This may be just redundant when having ofw_bus_devinfo 4104b3d9160SZbigniew Bodek * but makes this routine independent of it. 4114b3d9160SZbigniew Bodek */ 4127339f782SOleksandr Tymoshenko ret = OF_getprop_alloc(node, "name", sizeof(*name), (void **)&name); 4134b3d9160SZbigniew Bodek if (ret == -1) 4144b3d9160SZbigniew Bodek name = NULL; 4154b3d9160SZbigniew Bodek 4164b3d9160SZbigniew Bodek ret = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)®); 4174b3d9160SZbigniew Bodek nreg = (ret == -1) ? 0 : ret; 4184b3d9160SZbigniew Bodek 4194b3d9160SZbigniew Bodek if (nreg % (acells + scells) != 0) { 4204b3d9160SZbigniew Bodek if (bootverbose) 4214b3d9160SZbigniew Bodek device_printf(dev, "Malformed reg property on <%s>\n", 4224b3d9160SZbigniew Bodek (name == NULL) ? "unknown" : name); 4234b3d9160SZbigniew Bodek nreg = 0; 4244b3d9160SZbigniew Bodek } 4254b3d9160SZbigniew Bodek 4264b3d9160SZbigniew Bodek for (i = 0, rid = 0; i < nreg; i += acells + scells, rid++) { 4274b3d9160SZbigniew Bodek phys = size = 0; 4284b3d9160SZbigniew Bodek for (j = 0; j < acells; j++) { 4294b3d9160SZbigniew Bodek phys <<= 32; 4304b3d9160SZbigniew Bodek phys |= reg[i + j]; 4314b3d9160SZbigniew Bodek } 4324b3d9160SZbigniew Bodek for (j = 0; j < scells; j++) { 4334b3d9160SZbigniew Bodek size <<= 32; 4344b3d9160SZbigniew Bodek size |= reg[i + acells + j]; 4354b3d9160SZbigniew Bodek } 4364b3d9160SZbigniew Bodek /* Skip the dummy reg property of glue devices like ssm(4). */ 4374b3d9160SZbigniew Bodek if (size != 0) 4384b3d9160SZbigniew Bodek resource_list_add(rl, SYS_RES_MEMORY, rid, 4394b3d9160SZbigniew Bodek phys, phys + size - 1, size); 4404b3d9160SZbigniew Bodek } 4414b3d9160SZbigniew Bodek free(name, M_OFWPROP); 4424b3d9160SZbigniew Bodek free(reg, M_OFWPROP); 4434b3d9160SZbigniew Bodek 4444b3d9160SZbigniew Bodek return (0); 4454b3d9160SZbigniew Bodek } 4464b3d9160SZbigniew Bodek 447821a61efSMichal Meloun /* 448821a61efSMichal Meloun * Get interrupt parent for given node. 449821a61efSMichal Meloun * Returns 0 if interrupt parent doesn't exist. 450821a61efSMichal Meloun */ 451821a61efSMichal Meloun phandle_t 452821a61efSMichal Meloun ofw_bus_find_iparent(phandle_t node) 453821a61efSMichal Meloun { 454821a61efSMichal Meloun phandle_t iparent; 455821a61efSMichal Meloun 456821a61efSMichal Meloun if (OF_searchencprop(node, "interrupt-parent", &iparent, 457821a61efSMichal Meloun sizeof(iparent)) == -1) { 458821a61efSMichal Meloun for (iparent = node; iparent != 0; 459821a61efSMichal Meloun iparent = OF_parent(iparent)) { 460821a61efSMichal Meloun if (OF_hasprop(iparent, "interrupt-controller")) 461821a61efSMichal Meloun break; 462821a61efSMichal Meloun } 463821a61efSMichal Meloun iparent = OF_xref_from_node(iparent); 464821a61efSMichal Meloun } 465821a61efSMichal Meloun return (iparent); 466821a61efSMichal Meloun } 467821a61efSMichal Meloun 4684b3d9160SZbigniew Bodek int 469a8c5ea04SRuslan Bukin ofw_bus_intr_to_rl(device_t dev, phandle_t node, 470a8c5ea04SRuslan Bukin struct resource_list *rl, int *rlen) 471c47d4cdeSIan Lepore { 472c47d4cdeSIan Lepore phandle_t iparent; 473c47d4cdeSIan Lepore uint32_t icells, *intr; 474c47d4cdeSIan Lepore int err, i, irqnum, nintr, rid; 475c47d4cdeSIan Lepore boolean_t extended; 476c47d4cdeSIan Lepore 477c47d4cdeSIan Lepore nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr), 478c47d4cdeSIan Lepore (void **)&intr); 479c47d4cdeSIan Lepore if (nintr > 0) { 480821a61efSMichal Meloun iparent = ofw_bus_find_iparent(node); 48108b96b9fSNathan Whitehorn if (iparent == 0) { 482c47d4cdeSIan Lepore device_printf(dev, "No interrupt-parent found, " 483c47d4cdeSIan Lepore "assuming direct parent\n"); 484c47d4cdeSIan Lepore iparent = OF_parent(node); 48508b96b9fSNathan Whitehorn iparent = OF_xref_from_node(iparent); 48608b96b9fSNathan Whitehorn } 487c47d4cdeSIan Lepore if (OF_searchencprop(OF_node_from_xref(iparent), 488c47d4cdeSIan Lepore "#interrupt-cells", &icells, sizeof(icells)) == -1) { 489c47d4cdeSIan Lepore device_printf(dev, "Missing #interrupt-cells " 490c47d4cdeSIan Lepore "property, assuming <1>\n"); 491c47d4cdeSIan Lepore icells = 1; 492c47d4cdeSIan Lepore } 493c47d4cdeSIan Lepore if (icells < 1 || icells > nintr) { 494c47d4cdeSIan Lepore device_printf(dev, "Invalid #interrupt-cells property " 495c47d4cdeSIan Lepore "value <%d>, assuming <1>\n", icells); 496c47d4cdeSIan Lepore icells = 1; 497c47d4cdeSIan Lepore } 498c47d4cdeSIan Lepore extended = false; 499c47d4cdeSIan Lepore } else { 500c47d4cdeSIan Lepore nintr = OF_getencprop_alloc(node, "interrupts-extended", 501c47d4cdeSIan Lepore sizeof(*intr), (void **)&intr); 502c47d4cdeSIan Lepore if (nintr <= 0) 503c47d4cdeSIan Lepore return (0); 504c47d4cdeSIan Lepore extended = true; 505c47d4cdeSIan Lepore } 506c47d4cdeSIan Lepore err = 0; 507c47d4cdeSIan Lepore rid = 0; 508c47d4cdeSIan Lepore for (i = 0; i < nintr; i += icells) { 509c47d4cdeSIan Lepore if (extended) { 510c47d4cdeSIan Lepore iparent = intr[i++]; 511c47d4cdeSIan Lepore if (OF_searchencprop(OF_node_from_xref(iparent), 512c47d4cdeSIan Lepore "#interrupt-cells", &icells, sizeof(icells)) == -1) { 513c47d4cdeSIan Lepore device_printf(dev, "Missing #interrupt-cells " 514c47d4cdeSIan Lepore "property\n"); 515c47d4cdeSIan Lepore err = ENOENT; 516c47d4cdeSIan Lepore break; 517c47d4cdeSIan Lepore } 518c47d4cdeSIan Lepore if (icells < 1 || (i + icells) > nintr) { 519c47d4cdeSIan Lepore device_printf(dev, "Invalid #interrupt-cells " 520c47d4cdeSIan Lepore "property value <%d>\n", icells); 521c47d4cdeSIan Lepore err = ERANGE; 522c47d4cdeSIan Lepore break; 523c47d4cdeSIan Lepore } 524c47d4cdeSIan Lepore } 525c47d4cdeSIan Lepore irqnum = ofw_bus_map_intr(dev, iparent, icells, &intr[i]); 526c47d4cdeSIan Lepore resource_list_add(rl, SYS_RES_IRQ, rid++, irqnum, irqnum, 1); 527c47d4cdeSIan Lepore } 528a8c5ea04SRuslan Bukin if (rlen != NULL) 529a8c5ea04SRuslan Bukin *rlen = rid; 530c47d4cdeSIan Lepore free(intr, M_OFWPROP); 531c47d4cdeSIan Lepore return (err); 532c47d4cdeSIan Lepore } 53308b96b9fSNathan Whitehorn 53472a638c7SAndrew Turner phandle_t 53537c1967cSOleksandr Tymoshenko ofw_bus_find_child(phandle_t start, const char *child_name) 53637c1967cSOleksandr Tymoshenko { 53737c1967cSOleksandr Tymoshenko char *name; 53837c1967cSOleksandr Tymoshenko int ret; 53937c1967cSOleksandr Tymoshenko phandle_t child; 54037c1967cSOleksandr Tymoshenko 54137c1967cSOleksandr Tymoshenko for (child = OF_child(start); child != 0; child = OF_peer(child)) { 5427339f782SOleksandr Tymoshenko ret = OF_getprop_alloc(child, "name", sizeof(*name), (void **)&name); 54337c1967cSOleksandr Tymoshenko if (ret == -1) 54437c1967cSOleksandr Tymoshenko continue; 54537c1967cSOleksandr Tymoshenko if (strcmp(name, child_name) == 0) { 54637c1967cSOleksandr Tymoshenko free(name, M_OFWPROP); 54737c1967cSOleksandr Tymoshenko return (child); 54837c1967cSOleksandr Tymoshenko } 54937c1967cSOleksandr Tymoshenko 55037c1967cSOleksandr Tymoshenko free(name, M_OFWPROP); 55137c1967cSOleksandr Tymoshenko } 55237c1967cSOleksandr Tymoshenko 55337c1967cSOleksandr Tymoshenko return (0); 55437c1967cSOleksandr Tymoshenko } 55537c1967cSOleksandr Tymoshenko 55637c1967cSOleksandr Tymoshenko phandle_t 55772a638c7SAndrew Turner ofw_bus_find_compatible(phandle_t node, const char *onecompat) 55872a638c7SAndrew Turner { 55972a638c7SAndrew Turner phandle_t child, ret; 56072a638c7SAndrew Turner void *compat; 56172a638c7SAndrew Turner int len; 56272a638c7SAndrew Turner 56372a638c7SAndrew Turner /* 56472a638c7SAndrew Turner * Traverse all children of 'start' node, and find first with 56572a638c7SAndrew Turner * matching 'compatible' property. 56672a638c7SAndrew Turner */ 56772a638c7SAndrew Turner for (child = OF_child(node); child != 0; child = OF_peer(child)) { 568a5b53ce4SAndrew Turner len = OF_getprop_alloc(child, "compatible", 1, &compat); 56972a638c7SAndrew Turner if (len >= 0) { 57072a638c7SAndrew Turner ret = ofw_bus_node_is_compatible(compat, len, 57172a638c7SAndrew Turner onecompat); 57272a638c7SAndrew Turner free(compat, M_OFWPROP); 57372a638c7SAndrew Turner if (ret != 0) 57472a638c7SAndrew Turner return (child); 57572a638c7SAndrew Turner } 57672a638c7SAndrew Turner 57772a638c7SAndrew Turner ret = ofw_bus_find_compatible(child, onecompat); 57872a638c7SAndrew Turner if (ret != 0) 57972a638c7SAndrew Turner return (ret); 58072a638c7SAndrew Turner } 58172a638c7SAndrew Turner return (0); 58272a638c7SAndrew Turner } 583bb39ff4cSWarner Losh 584bb39ff4cSWarner Losh /** 585bb39ff4cSWarner Losh * @brief Return child of bus whose phandle is node 586bb39ff4cSWarner Losh * 587bb39ff4cSWarner Losh * A direct child of @p will be returned if it its phandle in the 588bb39ff4cSWarner Losh * OFW tree is @p node. Otherwise, NULL is returned. 589bb39ff4cSWarner Losh * 590bb39ff4cSWarner Losh * @param bus The bus to examine 591bb39ff4cSWarner Losh * @param node The phandle_t to look for. 592bb39ff4cSWarner Losh */ 593bb39ff4cSWarner Losh device_t 594bb39ff4cSWarner Losh ofw_bus_find_child_device_by_phandle(device_t bus, phandle_t node) 595bb39ff4cSWarner Losh { 596bb39ff4cSWarner Losh device_t *children, retval, child; 597bb39ff4cSWarner Losh int nkid, i; 598bb39ff4cSWarner Losh 599bb39ff4cSWarner Losh /* 600bb39ff4cSWarner Losh * Nothing can match the flag value for no node. 601bb39ff4cSWarner Losh */ 602bb39ff4cSWarner Losh if (node == -1) 603bb39ff4cSWarner Losh return (NULL); 604bb39ff4cSWarner Losh 605bb39ff4cSWarner Losh /* 606bb39ff4cSWarner Losh * Search the children for a match. We microoptimize 607bb39ff4cSWarner Losh * a bit by not using ofw_bus_get since we already know 608bb39ff4cSWarner Losh * the parent. We do not recurse. 609bb39ff4cSWarner Losh */ 610bb39ff4cSWarner Losh if (device_get_children(bus, &children, &nkid) != 0) 611bb39ff4cSWarner Losh return (NULL); 612bb39ff4cSWarner Losh retval = NULL; 613bb39ff4cSWarner Losh for (i = 0; i < nkid; i++) { 614bb39ff4cSWarner Losh child = children[i]; 615bb39ff4cSWarner Losh if (OFW_BUS_GET_NODE(bus, child) == node) { 616bb39ff4cSWarner Losh retval = child; 617bb39ff4cSWarner Losh break; 618bb39ff4cSWarner Losh } 619bb39ff4cSWarner Losh } 620bb39ff4cSWarner Losh free(children, M_TEMP); 621bb39ff4cSWarner Losh 622bb39ff4cSWarner Losh return (retval); 623bb39ff4cSWarner Losh } 6240b757c47SMichal Meloun 6250b757c47SMichal Meloun /* 6260b757c47SMichal Meloun * Parse property that contain list of xrefs and values 6270b757c47SMichal Meloun * (like standard "clocks" and "resets" properties) 6280b757c47SMichal Meloun * Input arguments: 6290b757c47SMichal Meloun * node - consumers device node 6300b757c47SMichal Meloun * list_name - name of parsed list - "clocks" 6310b757c47SMichal Meloun * cells_name - name of size property - "#clock-cells" 632*91ef8da0SAdrian Chadd * idx - the index of the requested list entry, or, if -1, an indication 633*91ef8da0SAdrian Chadd * to return the number of entries in the parsed list. 6340b757c47SMichal Meloun * Output arguments: 6350b757c47SMichal Meloun * producer - handle of producer 636*91ef8da0SAdrian Chadd * ncells - number of cells in result or the number of items in the list when 637*91ef8da0SAdrian Chadd * idx == -1. 6380b757c47SMichal Meloun * cells - array of decoded cells 6390b757c47SMichal Meloun */ 640*91ef8da0SAdrian Chadd static int 641*91ef8da0SAdrian Chadd ofw_bus_parse_xref_list_internal(phandle_t node, const char *list_name, 6420b757c47SMichal Meloun const char *cells_name, int idx, phandle_t *producer, int *ncells, 6430b757c47SMichal Meloun pcell_t **cells) 6440b757c47SMichal Meloun { 6450b757c47SMichal Meloun phandle_t pnode; 6460b757c47SMichal Meloun phandle_t *elems; 6470b757c47SMichal Meloun uint32_t pcells; 6480b757c47SMichal Meloun int rv, i, j, nelems, cnt; 6490b757c47SMichal Meloun 6500b757c47SMichal Meloun elems = NULL; 6510b757c47SMichal Meloun nelems = OF_getencprop_alloc(node, list_name, sizeof(*elems), 6520b757c47SMichal Meloun (void **)&elems); 6530b757c47SMichal Meloun if (nelems <= 0) 6540b757c47SMichal Meloun return (ENOENT); 655*91ef8da0SAdrian Chadd rv = (idx == -1) ? 0 : ENOENT; 6560b757c47SMichal Meloun for (i = 0, cnt = 0; i < nelems; i += pcells, cnt++) { 6570b757c47SMichal Meloun pnode = elems[i++]; 6580b757c47SMichal Meloun if (OF_getencprop(OF_node_from_xref(pnode), 6590b757c47SMichal Meloun cells_name, &pcells, sizeof(pcells)) == -1) { 6600b757c47SMichal Meloun printf("Missing %s property\n", cells_name); 6610b757c47SMichal Meloun rv = ENOENT; 6620b757c47SMichal Meloun break; 6630b757c47SMichal Meloun } 6640b757c47SMichal Meloun 6650b757c47SMichal Meloun if ((i + pcells) > nelems) { 6660b757c47SMichal Meloun printf("Invalid %s property value <%d>\n", cells_name, 6670b757c47SMichal Meloun pcells); 6680b757c47SMichal Meloun rv = ERANGE; 6690b757c47SMichal Meloun break; 6700b757c47SMichal Meloun } 6710b757c47SMichal Meloun if (cnt == idx) { 6720b757c47SMichal Meloun *cells= malloc(pcells * sizeof(**cells), M_OFWPROP, 6730b757c47SMichal Meloun M_WAITOK); 6740b757c47SMichal Meloun *producer = pnode; 6750b757c47SMichal Meloun *ncells = pcells; 6760b757c47SMichal Meloun for (j = 0; j < pcells; j++) 6770b757c47SMichal Meloun (*cells)[j] = elems[i + j]; 6780b757c47SMichal Meloun rv = 0; 6790b757c47SMichal Meloun break; 6800b757c47SMichal Meloun } 6810b757c47SMichal Meloun } 6820b757c47SMichal Meloun if (elems != NULL) 6830b757c47SMichal Meloun free(elems, M_OFWPROP); 684*91ef8da0SAdrian Chadd if (idx == -1 && rv == 0) 685*91ef8da0SAdrian Chadd *ncells = cnt; 6860b757c47SMichal Meloun return (rv); 6870b757c47SMichal Meloun } 6880b757c47SMichal Meloun 6890b757c47SMichal Meloun /* 690*91ef8da0SAdrian Chadd * Parse property that contain list of xrefs and values 691*91ef8da0SAdrian Chadd * (like standard "clocks" and "resets" properties) 692*91ef8da0SAdrian Chadd * Input arguments: 693*91ef8da0SAdrian Chadd * node - consumers device node 694*91ef8da0SAdrian Chadd * list_name - name of parsed list - "clocks" 695*91ef8da0SAdrian Chadd * cells_name - name of size property - "#clock-cells" 696*91ef8da0SAdrian Chadd * idx - the index of the requested list entry (>= 0) 697*91ef8da0SAdrian Chadd * Output arguments: 698*91ef8da0SAdrian Chadd * producer - handle of producer 699*91ef8da0SAdrian Chadd * ncells - number of cells in result 700*91ef8da0SAdrian Chadd * cells - array of decoded cells 701*91ef8da0SAdrian Chadd */ 702*91ef8da0SAdrian Chadd int 703*91ef8da0SAdrian Chadd ofw_bus_parse_xref_list_alloc(phandle_t node, const char *list_name, 704*91ef8da0SAdrian Chadd const char *cells_name, int idx, phandle_t *producer, int *ncells, 705*91ef8da0SAdrian Chadd pcell_t **cells) 706*91ef8da0SAdrian Chadd { 707*91ef8da0SAdrian Chadd 708*91ef8da0SAdrian Chadd KASSERT(idx >= 0, 709*91ef8da0SAdrian Chadd ("ofw_bus_parse_xref_list_alloc: negative index supplied")); 710*91ef8da0SAdrian Chadd 711*91ef8da0SAdrian Chadd return (ofw_bus_parse_xref_list_internal(node, list_name, cells_name, 712*91ef8da0SAdrian Chadd idx, producer, ncells, cells)); 713*91ef8da0SAdrian Chadd } 714*91ef8da0SAdrian Chadd 715*91ef8da0SAdrian Chadd /* 716*91ef8da0SAdrian Chadd * Parse property that contain list of xrefs and values 717*91ef8da0SAdrian Chadd * (like standard "clocks" and "resets" properties) 718*91ef8da0SAdrian Chadd * and determine the number of items in the list 719*91ef8da0SAdrian Chadd * Input arguments: 720*91ef8da0SAdrian Chadd * node - consumers device node 721*91ef8da0SAdrian Chadd * list_name - name of parsed list - "clocks" 722*91ef8da0SAdrian Chadd * cells_name - name of size property - "#clock-cells" 723*91ef8da0SAdrian Chadd * Output arguments: 724*91ef8da0SAdrian Chadd * count - number of items in list 725*91ef8da0SAdrian Chadd */ 726*91ef8da0SAdrian Chadd int 727*91ef8da0SAdrian Chadd ofw_bus_parse_xref_list_get_length(phandle_t node, const char *list_name, 728*91ef8da0SAdrian Chadd const char *cells_name, int *count) 729*91ef8da0SAdrian Chadd { 730*91ef8da0SAdrian Chadd 731*91ef8da0SAdrian Chadd return (ofw_bus_parse_xref_list_internal(node, list_name, cells_name, 732*91ef8da0SAdrian Chadd -1, NULL, count, NULL)); 733*91ef8da0SAdrian Chadd } 734*91ef8da0SAdrian Chadd 735*91ef8da0SAdrian Chadd /* 7360b757c47SMichal Meloun * Find index of string in string list property (case sensitive). 7370b757c47SMichal Meloun */ 7380b757c47SMichal Meloun int 7390b757c47SMichal Meloun ofw_bus_find_string_index(phandle_t node, const char *list_name, 7400b757c47SMichal Meloun const char *name, int *idx) 7410b757c47SMichal Meloun { 7420b757c47SMichal Meloun char *elems; 7430b757c47SMichal Meloun int rv, i, cnt, nelems; 7440b757c47SMichal Meloun 7450b757c47SMichal Meloun elems = NULL; 7460b757c47SMichal Meloun nelems = OF_getprop_alloc(node, list_name, 1, (void **)&elems); 7470b757c47SMichal Meloun if (nelems <= 0) 7480b757c47SMichal Meloun return (ENOENT); 7490b757c47SMichal Meloun 7500b757c47SMichal Meloun rv = ENOENT; 7510b757c47SMichal Meloun for (i = 0, cnt = 0; i < nelems; cnt++) { 7520b757c47SMichal Meloun if (strcmp(elems + i, name) == 0) { 7530b757c47SMichal Meloun *idx = cnt; 7540b757c47SMichal Meloun rv = 0; 7550b757c47SMichal Meloun break; 7560b757c47SMichal Meloun } 7570b757c47SMichal Meloun i += strlen(elems + i) + 1; 7580b757c47SMichal Meloun } 7590b757c47SMichal Meloun 7600b757c47SMichal Meloun if (elems != NULL) 7610b757c47SMichal Meloun free(elems, M_OFWPROP); 7620b757c47SMichal Meloun return (rv); 7630b757c47SMichal Meloun } 7640b757c47SMichal Meloun 7650b757c47SMichal Meloun /* 7660b757c47SMichal Meloun * Create zero terminated array of strings from string list property. 7670b757c47SMichal Meloun */ 7680b757c47SMichal Meloun int 7690b757c47SMichal Meloun ofw_bus_string_list_to_array(phandle_t node, const char *list_name, 770086a6314SMichal Meloun const char ***out_array) 7710b757c47SMichal Meloun { 7720b757c47SMichal Meloun char *elems, *tptr; 773086a6314SMichal Meloun const char **array; 7740b757c47SMichal Meloun int i, cnt, nelems, len; 7750b757c47SMichal Meloun 7760b757c47SMichal Meloun elems = NULL; 7770b757c47SMichal Meloun nelems = OF_getprop_alloc(node, list_name, 1, (void **)&elems); 7780b757c47SMichal Meloun if (nelems <= 0) 7790b757c47SMichal Meloun return (nelems); 7800b757c47SMichal Meloun 7810b757c47SMichal Meloun /* Count number of strings. */ 7820b757c47SMichal Meloun for (i = 0, cnt = 0; i < nelems; cnt++) 7830b757c47SMichal Meloun i += strlen(elems + i) + 1; 7840b757c47SMichal Meloun 7850b757c47SMichal Meloun /* Allocate space for arrays and all strings. */ 786086a6314SMichal Meloun array = malloc((cnt + 1) * sizeof(char *) + nelems, M_OFWPROP, 7870b757c47SMichal Meloun M_WAITOK); 7880b757c47SMichal Meloun 7890b757c47SMichal Meloun /* Get address of first string. */ 790086a6314SMichal Meloun tptr = (char *)(array + cnt + 1); 7910b757c47SMichal Meloun 7920b757c47SMichal Meloun /* Copy strings. */ 7930b757c47SMichal Meloun memcpy(tptr, elems, nelems); 7940b757c47SMichal Meloun free(elems, M_OFWPROP); 7950b757c47SMichal Meloun 7960b757c47SMichal Meloun /* Fill string pointers. */ 7970b757c47SMichal Meloun for (i = 0, cnt = 0; i < nelems; cnt++) { 798086a6314SMichal Meloun len = strlen(tptr) + 1; 799086a6314SMichal Meloun array[cnt] = tptr; 8000b757c47SMichal Meloun i += len; 8010b757c47SMichal Meloun tptr += len; 8020b757c47SMichal Meloun } 803086a6314SMichal Meloun array[cnt] = 0; 804086a6314SMichal Meloun *out_array = array; 8050b757c47SMichal Meloun 8060b757c47SMichal Meloun return (cnt); 8070b757c47SMichal Meloun } 808