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 4094b4a038SNathan Whitehorn #include <dev/ofw/ofw_bus.h> 41bba6f0a9SMarius Strobl #include <dev/ofw/ofw_bus_subr.h> 42bba6f0a9SMarius Strobl #include <dev/ofw/openfirm.h> 43bba6f0a9SMarius Strobl 44bba6f0a9SMarius Strobl #include "ofw_bus_if.h" 45bba6f0a9SMarius Strobl 46bba6f0a9SMarius Strobl int 47bba6f0a9SMarius Strobl ofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node) 48bba6f0a9SMarius Strobl { 49bba6f0a9SMarius Strobl 50bba6f0a9SMarius Strobl if (obd == NULL) 51bba6f0a9SMarius Strobl return (ENOMEM); 52bba6f0a9SMarius Strobl /* The 'name' property is considered mandatory. */ 53bba6f0a9SMarius Strobl if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1) 54bba6f0a9SMarius Strobl return (EINVAL); 55bba6f0a9SMarius Strobl OF_getprop_alloc(node, "compatible", 1, (void **)&obd->obd_compat); 56bba6f0a9SMarius Strobl OF_getprop_alloc(node, "device_type", 1, (void **)&obd->obd_type); 57bba6f0a9SMarius Strobl OF_getprop_alloc(node, "model", 1, (void **)&obd->obd_model); 58bba6f0a9SMarius Strobl obd->obd_node = node; 59bba6f0a9SMarius Strobl return (0); 60bba6f0a9SMarius Strobl } 61bba6f0a9SMarius Strobl 62bba6f0a9SMarius Strobl void 63bba6f0a9SMarius Strobl ofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd) 64bba6f0a9SMarius Strobl { 65bba6f0a9SMarius Strobl 66bba6f0a9SMarius Strobl if (obd == NULL) 67bba6f0a9SMarius Strobl return; 68bba6f0a9SMarius Strobl if (obd->obd_compat != NULL) 69bba6f0a9SMarius Strobl free(obd->obd_compat, M_OFWPROP); 70bba6f0a9SMarius Strobl if (obd->obd_model != NULL) 71bba6f0a9SMarius Strobl free(obd->obd_model, M_OFWPROP); 72bba6f0a9SMarius Strobl if (obd->obd_name != NULL) 73bba6f0a9SMarius Strobl free(obd->obd_name, M_OFWPROP); 74bba6f0a9SMarius Strobl if (obd->obd_type != NULL) 75bba6f0a9SMarius Strobl free(obd->obd_type, M_OFWPROP); 76bba6f0a9SMarius Strobl } 77bba6f0a9SMarius Strobl 7894b4a038SNathan Whitehorn int 7994b4a038SNathan Whitehorn ofw_bus_gen_child_pnpinfo_str(device_t cbdev, device_t child, char *buf, 8094b4a038SNathan Whitehorn size_t buflen) 8194b4a038SNathan Whitehorn { 82481d6b54SMarius Strobl 8394b4a038SNathan Whitehorn if (ofw_bus_get_name(child) != NULL) { 8494b4a038SNathan Whitehorn strlcat(buf, "name=", buflen); 8594b4a038SNathan Whitehorn strlcat(buf, ofw_bus_get_name(child), buflen); 8694b4a038SNathan Whitehorn } 8794b4a038SNathan Whitehorn 8894b4a038SNathan Whitehorn if (ofw_bus_get_compat(child) != NULL) { 8994b4a038SNathan Whitehorn strlcat(buf, " compat=", buflen); 9094b4a038SNathan Whitehorn strlcat(buf, ofw_bus_get_compat(child), buflen); 9194b4a038SNathan Whitehorn } 9294b4a038SNathan Whitehorn return (0); 9394b4a038SNathan Whitehorn }; 94bba6f0a9SMarius Strobl 95bba6f0a9SMarius Strobl const char * 96bba6f0a9SMarius Strobl ofw_bus_gen_get_compat(device_t bus, device_t dev) 97bba6f0a9SMarius Strobl { 98bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 99bba6f0a9SMarius Strobl 100bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 101bba6f0a9SMarius Strobl if (obd == NULL) 102bba6f0a9SMarius Strobl return (NULL); 103bba6f0a9SMarius Strobl return (obd->obd_compat); 104bba6f0a9SMarius Strobl } 105bba6f0a9SMarius Strobl 106bba6f0a9SMarius Strobl const char * 107bba6f0a9SMarius Strobl ofw_bus_gen_get_model(device_t bus, device_t dev) 108bba6f0a9SMarius Strobl { 109bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 110bba6f0a9SMarius Strobl 111bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 112bba6f0a9SMarius Strobl if (obd == NULL) 113bba6f0a9SMarius Strobl return (NULL); 114bba6f0a9SMarius Strobl return (obd->obd_model); 115bba6f0a9SMarius Strobl } 116bba6f0a9SMarius Strobl 117bba6f0a9SMarius Strobl const char * 118bba6f0a9SMarius Strobl ofw_bus_gen_get_name(device_t bus, device_t dev) 119bba6f0a9SMarius Strobl { 120bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 121bba6f0a9SMarius Strobl 122bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 123bba6f0a9SMarius Strobl if (obd == NULL) 124bba6f0a9SMarius Strobl return (NULL); 125bba6f0a9SMarius Strobl return (obd->obd_name); 126bba6f0a9SMarius Strobl } 127bba6f0a9SMarius Strobl 128bba6f0a9SMarius Strobl phandle_t 129bba6f0a9SMarius Strobl ofw_bus_gen_get_node(device_t bus, device_t dev) 130bba6f0a9SMarius Strobl { 131bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 132bba6f0a9SMarius Strobl 133bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 134bba6f0a9SMarius Strobl if (obd == NULL) 135bba6f0a9SMarius Strobl return (0); 136bba6f0a9SMarius Strobl return (obd->obd_node); 137bba6f0a9SMarius Strobl } 138bba6f0a9SMarius Strobl 139bba6f0a9SMarius Strobl const char * 140bba6f0a9SMarius Strobl ofw_bus_gen_get_type(device_t bus, device_t dev) 141bba6f0a9SMarius Strobl { 142bba6f0a9SMarius Strobl const struct ofw_bus_devinfo *obd; 143bba6f0a9SMarius Strobl 144bba6f0a9SMarius Strobl obd = OFW_BUS_GET_DEVINFO(bus, dev); 145bba6f0a9SMarius Strobl if (obd == NULL) 146bba6f0a9SMarius Strobl return (NULL); 147bba6f0a9SMarius Strobl return (obd->obd_type); 148bba6f0a9SMarius Strobl } 14994b4a038SNathan Whitehorn 1508297758aSRafal Jaworowski int 1518297758aSRafal Jaworowski ofw_bus_is_compatible(device_t dev, const char *onecompat) 1528297758aSRafal Jaworowski { 1538297758aSRafal Jaworowski phandle_t node; 1548297758aSRafal Jaworowski const char *compat; 1558297758aSRafal Jaworowski int len, onelen, l; 1568297758aSRafal Jaworowski 1578297758aSRafal Jaworowski if ((compat = ofw_bus_get_compat(dev)) == NULL) 1588297758aSRafal Jaworowski return (0); 1598297758aSRafal Jaworowski 1600d8d9edaSNathan Whitehorn if ((node = ofw_bus_get_node(dev)) == -1) 1618297758aSRafal Jaworowski return (0); 1628297758aSRafal Jaworowski 1638297758aSRafal Jaworowski /* Get total 'compatible' prop len */ 1648297758aSRafal Jaworowski if ((len = OF_getproplen(node, "compatible")) <= 0) 1658297758aSRafal Jaworowski return (0); 1668297758aSRafal Jaworowski 1678297758aSRafal Jaworowski onelen = strlen(onecompat); 1688297758aSRafal Jaworowski 1698297758aSRafal Jaworowski while (len > 0) { 17026ca4965SHiroki Sato if (strlen(compat) == onelen && 17126ca4965SHiroki Sato strncasecmp(compat, onecompat, onelen) == 0) 1728297758aSRafal Jaworowski /* Found it. */ 1738297758aSRafal Jaworowski return (1); 1748297758aSRafal Jaworowski 1758297758aSRafal Jaworowski /* Slide to the next sub-string. */ 1768297758aSRafal Jaworowski l = strlen(compat) + 1; 1778297758aSRafal Jaworowski compat += l; 1788297758aSRafal Jaworowski len -= l; 1798297758aSRafal Jaworowski } 1808297758aSRafal Jaworowski return (0); 1818297758aSRafal Jaworowski } 1828297758aSRafal Jaworowski 1838297758aSRafal Jaworowski int 1848297758aSRafal Jaworowski ofw_bus_is_compatible_strict(device_t dev, const char *compatible) 1858297758aSRafal Jaworowski { 1868297758aSRafal Jaworowski const char *compat; 18726ca4965SHiroki Sato size_t len; 1888297758aSRafal Jaworowski 1898297758aSRafal Jaworowski if ((compat = ofw_bus_get_compat(dev)) == NULL) 1908297758aSRafal Jaworowski return (0); 1918297758aSRafal Jaworowski 19226ca4965SHiroki Sato len = strlen(compatible); 19326ca4965SHiroki Sato if (strlen(compat) == len && 19426ca4965SHiroki Sato strncasecmp(compat, compatible, len) == 0) 1958297758aSRafal Jaworowski return (1); 1968297758aSRafal Jaworowski 1978297758aSRafal Jaworowski return (0); 1988297758aSRafal Jaworowski } 1998297758aSRafal Jaworowski 200498fa7c1SIan Lepore const struct ofw_compat_data * 201498fa7c1SIan Lepore ofw_bus_search_compatible(device_t dev, const struct ofw_compat_data *compat) 202498fa7c1SIan Lepore { 203498fa7c1SIan Lepore 204498fa7c1SIan Lepore if (compat == NULL) 205498fa7c1SIan Lepore return NULL; 206498fa7c1SIan Lepore 207498fa7c1SIan Lepore for (; compat->ocd_str != NULL; ++compat) { 208498fa7c1SIan Lepore if (ofw_bus_is_compatible(dev, compat->ocd_str)) 209498fa7c1SIan Lepore break; 210498fa7c1SIan Lepore } 211498fa7c1SIan Lepore 212498fa7c1SIan Lepore return (compat); 213498fa7c1SIan Lepore } 214498fa7c1SIan Lepore 21526ca4965SHiroki Sato int 21626ca4965SHiroki Sato ofw_bus_has_prop(device_t dev, const char *propname) 21726ca4965SHiroki Sato { 21826ca4965SHiroki Sato phandle_t node; 21926ca4965SHiroki Sato 22026ca4965SHiroki Sato if ((node = ofw_bus_get_node(dev)) == -1) 22126ca4965SHiroki Sato return (0); 22226ca4965SHiroki Sato 22326ca4965SHiroki Sato return (OF_hasprop(node, propname)); 22426ca4965SHiroki Sato } 22526ca4965SHiroki Sato 22694b4a038SNathan Whitehorn void 22794b4a038SNathan Whitehorn ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz) 22894b4a038SNathan Whitehorn { 22994b4a038SNathan Whitehorn pcell_t addrc; 23094b4a038SNathan Whitehorn int msksz; 23194b4a038SNathan Whitehorn 232d3a0a0f3SNathan Whitehorn if (OF_getencprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1) 23394b4a038SNathan Whitehorn addrc = 2; 23494b4a038SNathan Whitehorn ii->opi_addrc = addrc * sizeof(pcell_t); 23594b4a038SNathan Whitehorn 236d3a0a0f3SNathan Whitehorn ii->opi_imapsz = OF_getencprop_alloc(node, "interrupt-map", 1, 23794b4a038SNathan Whitehorn (void **)&ii->opi_imap); 23894b4a038SNathan Whitehorn if (ii->opi_imapsz > 0) { 239d3a0a0f3SNathan Whitehorn msksz = OF_getencprop_alloc(node, "interrupt-map-mask", 1, 24094b4a038SNathan Whitehorn (void **)&ii->opi_imapmsk); 24194b4a038SNathan Whitehorn /* 242481d6b54SMarius Strobl * Failure to get the mask is ignored; a full mask is used 243481d6b54SMarius Strobl * then. We barf on bad mask sizes, however. 24494b4a038SNathan Whitehorn */ 245481d6b54SMarius Strobl if (msksz != -1 && msksz != ii->opi_addrc + intrsz) 24694b4a038SNathan Whitehorn panic("ofw_bus_setup_iinfo: bad interrupt-map-mask " 24794b4a038SNathan Whitehorn "property!"); 24894b4a038SNathan Whitehorn } 24994b4a038SNathan Whitehorn } 25094b4a038SNathan Whitehorn 25194b4a038SNathan Whitehorn int 25294b4a038SNathan Whitehorn ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg, 25394b4a038SNathan Whitehorn int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz, 254eaef5f0aSNathan Whitehorn phandle_t *iparent, void *maskbuf) 25594b4a038SNathan Whitehorn { 25694b4a038SNathan Whitehorn int rv; 25794b4a038SNathan Whitehorn 25894b4a038SNathan Whitehorn if (ii->opi_imapsz <= 0) 25994b4a038SNathan Whitehorn return (0); 26094b4a038SNathan Whitehorn KASSERT(regsz >= ii->opi_addrc, 26194b4a038SNathan Whitehorn ("ofw_bus_lookup_imap: register size too small: %d < %d", 26294b4a038SNathan Whitehorn regsz, ii->opi_addrc)); 2636064b6acSNathan Whitehorn if (node != -1) { 264d3a0a0f3SNathan Whitehorn rv = OF_getencprop(node, "reg", reg, regsz); 26594b4a038SNathan Whitehorn if (rv < regsz) 2666064b6acSNathan Whitehorn panic("ofw_bus_lookup_imap: cannot get reg property"); 2676064b6acSNathan Whitehorn } 26894b4a038SNathan Whitehorn return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc, 26994b4a038SNathan Whitehorn ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr, 270eaef5f0aSNathan Whitehorn mintrsz, iparent)); 27194b4a038SNathan Whitehorn } 27294b4a038SNathan Whitehorn 27394b4a038SNathan Whitehorn /* 27494b4a038SNathan Whitehorn * Map an interrupt using the firmware reg, interrupt-map and 27594b4a038SNathan Whitehorn * interrupt-map-mask properties. 27694b4a038SNathan Whitehorn * The interrupt property to be mapped must be of size intrsz, and pointed to 27794b4a038SNathan Whitehorn * by intr. The regs property of the node for which the mapping is done must 27894b4a038SNathan Whitehorn * be passed as regs. This property is an array of register specifications; 27994b4a038SNathan Whitehorn * the size of the address part of such a specification must be passed as 28094b4a038SNathan Whitehorn * physsz. Only the first element of the property is used. 28194b4a038SNathan Whitehorn * imap and imapsz hold the interrupt mask and it's size. 28294b4a038SNathan Whitehorn * imapmsk is a pointer to the interrupt-map-mask property, which must have 28394b4a038SNathan Whitehorn * a size of physsz + intrsz; it may be NULL, in which case a full mask is 28494b4a038SNathan Whitehorn * assumed. 28594b4a038SNathan Whitehorn * maskbuf must point to a buffer of length physsz + intrsz. 28694b4a038SNathan Whitehorn * The interrupt is returned in result, which must point to a buffer of length 28794b4a038SNathan Whitehorn * rintrsz (which gives the expected size of the mapped interrupt). 28894b4a038SNathan Whitehorn * Returns 1 if a mapping was found, 0 otherwise. 28994b4a038SNathan Whitehorn */ 29094b4a038SNathan Whitehorn int 29194b4a038SNathan Whitehorn ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz, 29294b4a038SNathan Whitehorn void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result, 293eaef5f0aSNathan Whitehorn int rintrsz, phandle_t *iparent) 29494b4a038SNathan Whitehorn { 29594b4a038SNathan Whitehorn phandle_t parent; 296481d6b54SMarius Strobl uint8_t *ref = maskbuf; 297481d6b54SMarius Strobl uint8_t *uiintr = intr; 298481d6b54SMarius Strobl uint8_t *uiregs = regs; 299481d6b54SMarius Strobl uint8_t *uiimapmsk = imapmsk; 300481d6b54SMarius Strobl uint8_t *mptr; 30194b4a038SNathan Whitehorn pcell_t pintrsz; 30294b4a038SNathan Whitehorn int i, rsz, tsz; 30394b4a038SNathan Whitehorn 30494b4a038SNathan Whitehorn rsz = -1; 30594b4a038SNathan Whitehorn if (imapmsk != NULL) { 30694b4a038SNathan Whitehorn for (i = 0; i < physsz; i++) 30794b4a038SNathan Whitehorn ref[i] = uiregs[i] & uiimapmsk[i]; 30894b4a038SNathan Whitehorn for (i = 0; i < intrsz; i++) 30994b4a038SNathan Whitehorn ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i]; 31094b4a038SNathan Whitehorn } else { 31194b4a038SNathan Whitehorn bcopy(regs, ref, physsz); 31294b4a038SNathan Whitehorn bcopy(intr, ref + physsz, intrsz); 31394b4a038SNathan Whitehorn } 31494b4a038SNathan Whitehorn 31594b4a038SNathan Whitehorn mptr = imap; 31694b4a038SNathan Whitehorn i = imapsz; 31794b4a038SNathan Whitehorn while (i > 0) { 31894b4a038SNathan Whitehorn bcopy(mptr + physsz + intrsz, &parent, sizeof(parent)); 319d3a0a0f3SNathan Whitehorn if (OF_searchencprop(OF_xref_phandle(parent), 320d3a0a0f3SNathan Whitehorn "#interrupt-cells", &pintrsz, sizeof(pintrsz)) == -1) 32194b4a038SNathan Whitehorn pintrsz = 1; /* default */ 32294b4a038SNathan Whitehorn pintrsz *= sizeof(pcell_t); 323acb97117SNathan Whitehorn 324481d6b54SMarius Strobl /* Compute the map stride size. */ 325acb97117SNathan Whitehorn tsz = physsz + intrsz + sizeof(phandle_t) + pintrsz; 326acb97117SNathan Whitehorn KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map")); 32794b4a038SNathan Whitehorn 32894b4a038SNathan Whitehorn /* 32994b4a038SNathan Whitehorn * XXX: Apple hardware uses a second cell to set information 33094b4a038SNathan Whitehorn * on the interrupt trigger type. This information should 33194b4a038SNathan Whitehorn * be used somewhere to program the PIC. 33294b4a038SNathan Whitehorn */ 33394b4a038SNathan Whitehorn 33494b4a038SNathan Whitehorn if (bcmp(ref, mptr, physsz + intrsz) == 0) { 33594b4a038SNathan Whitehorn bcopy(mptr + physsz + intrsz + sizeof(parent), 33694b4a038SNathan Whitehorn result, rintrsz); 337eaef5f0aSNathan Whitehorn 338eaef5f0aSNathan Whitehorn if (iparent != NULL) 339eaef5f0aSNathan Whitehorn *iparent = parent; 34094b4a038SNathan Whitehorn return (1); 34194b4a038SNathan Whitehorn } 34294b4a038SNathan Whitehorn mptr += tsz; 34394b4a038SNathan Whitehorn i -= tsz; 34494b4a038SNathan Whitehorn } 34594b4a038SNathan Whitehorn return (0); 34694b4a038SNathan Whitehorn } 3476064b6acSNathan Whitehorn 348