ofw_bus_subr.c revision 186128
1/*- 2 * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>. 3 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions, and the following disclaimer, 11 * without modification, immediately at the beginning of the file. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_bus_subr.c 186128 2008-12-15 15:31:10Z nwhitehorn $"); 32 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/bus.h> 36#include <sys/errno.h> 37#include <sys/libkern.h> 38 39#include <dev/ofw/ofw_bus.h> 40#include <dev/ofw/ofw_bus_subr.h> 41#include <dev/ofw/openfirm.h> 42 43#include "ofw_bus_if.h" 44 45int 46ofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node) 47{ 48 49 if (obd == NULL) 50 return (ENOMEM); 51 /* The 'name' property is considered mandatory. */ 52 if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1) 53 return (EINVAL); 54 OF_getprop_alloc(node, "compatible", 1, (void **)&obd->obd_compat); 55 OF_getprop_alloc(node, "device_type", 1, (void **)&obd->obd_type); 56 OF_getprop_alloc(node, "model", 1, (void **)&obd->obd_model); 57 obd->obd_node = node; 58 return (0); 59} 60 61void 62ofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd) 63{ 64 65 if (obd == NULL) 66 return; 67 if (obd->obd_compat != NULL) 68 free(obd->obd_compat, M_OFWPROP); 69 if (obd->obd_model != NULL) 70 free(obd->obd_model, M_OFWPROP); 71 if (obd->obd_name != NULL) 72 free(obd->obd_name, M_OFWPROP); 73 if (obd->obd_type != NULL) 74 free(obd->obd_type, M_OFWPROP); 75} 76 77int 78ofw_bus_gen_child_pnpinfo_str(device_t cbdev, device_t child, char *buf, 79 size_t buflen) 80{ 81 if (ofw_bus_get_name(child) != NULL) { 82 strlcat(buf, "name=", buflen); 83 strlcat(buf, ofw_bus_get_name(child), buflen); 84 } 85 86 if (ofw_bus_get_compat(child) != NULL) { 87 strlcat(buf, " compat=", buflen); 88 strlcat(buf, ofw_bus_get_compat(child), buflen); 89 } 90 91 return (0); 92}; 93 94const char * 95ofw_bus_gen_get_compat(device_t bus, device_t dev) 96{ 97 const struct ofw_bus_devinfo *obd; 98 99 obd = OFW_BUS_GET_DEVINFO(bus, dev); 100 if (obd == NULL) 101 return (NULL); 102 return (obd->obd_compat); 103} 104 105const char * 106ofw_bus_gen_get_model(device_t bus, device_t dev) 107{ 108 const struct ofw_bus_devinfo *obd; 109 110 obd = OFW_BUS_GET_DEVINFO(bus, dev); 111 if (obd == NULL) 112 return (NULL); 113 return (obd->obd_model); 114} 115 116const char * 117ofw_bus_gen_get_name(device_t bus, device_t dev) 118{ 119 const struct ofw_bus_devinfo *obd; 120 121 obd = OFW_BUS_GET_DEVINFO(bus, dev); 122 if (obd == NULL) 123 return (NULL); 124 return (obd->obd_name); 125} 126 127phandle_t 128ofw_bus_gen_get_node(device_t bus, device_t dev) 129{ 130 const struct ofw_bus_devinfo *obd; 131 132 obd = OFW_BUS_GET_DEVINFO(bus, dev); 133 if (obd == NULL) 134 return (0); 135 return (obd->obd_node); 136} 137 138const char * 139ofw_bus_gen_get_type(device_t bus, device_t dev) 140{ 141 const struct ofw_bus_devinfo *obd; 142 143 obd = OFW_BUS_GET_DEVINFO(bus, dev); 144 if (obd == NULL) 145 return (NULL); 146 return (obd->obd_type); 147} 148 149static int 150ofw_bus_searchprop(phandle_t node, char *propname, void *buf, int buflen) 151{ 152 int rv; 153 154 for (; node != 0; node = OF_parent(node)) { 155 if ((rv = OF_getprop(node, propname, buf, buflen)) != -1) 156 return (rv); 157 } 158 return (-1); 159} 160 161void 162ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz) 163{ 164 pcell_t addrc; 165 int msksz; 166 167 if (OF_getprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1) 168 addrc = 2; 169 ii->opi_addrc = addrc * sizeof(pcell_t); 170 171 ii->opi_imapsz = OF_getprop_alloc(node, "interrupt-map", 1, 172 (void **)&ii->opi_imap); 173 if (ii->opi_imapsz > 0) { 174 msksz = OF_getprop_alloc(node, "interrupt-map-mask", 1, 175 (void **)&ii->opi_imapmsk); 176 /* 177 * Failure to get the mask is ignored; a full mask is used then. 178 * Barf on bad mask sizes, however. 179 */ 180 if (msksz != -1 && msksz != ii->opi_addrc + intrsz) { 181 panic("ofw_bus_setup_iinfo: bad interrupt-map-mask " 182 "property!"); 183 } 184 } 185 186} 187 188int 189ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg, 190 int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz, 191 void *maskbuf) 192{ 193 int rv; 194 195 if (ii->opi_imapsz <= 0) 196 return (0); 197 KASSERT(regsz >= ii->opi_addrc, 198 ("ofw_bus_lookup_imap: register size too small: %d < %d", 199 regsz, ii->opi_addrc)); 200 rv = OF_getprop(node, "reg", reg, regsz); 201 if (rv < regsz) 202 panic("ofw_bus_lookup_imap: could not get reg property"); 203 return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc, 204 ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr, 205 mintrsz)); 206} 207 208/* 209 * Map an interrupt using the firmware reg, interrupt-map and 210 * interrupt-map-mask properties. 211 * The interrupt property to be mapped must be of size intrsz, and pointed to 212 * by intr. The regs property of the node for which the mapping is done must 213 * be passed as regs. This property is an array of register specifications; 214 * the size of the address part of such a specification must be passed as 215 * physsz. Only the first element of the property is used. 216 * imap and imapsz hold the interrupt mask and it's size. 217 * imapmsk is a pointer to the interrupt-map-mask property, which must have 218 * a size of physsz + intrsz; it may be NULL, in which case a full mask is 219 * assumed. 220 * maskbuf must point to a buffer of length physsz + intrsz. 221 * The interrupt is returned in result, which must point to a buffer of length 222 * rintrsz (which gives the expected size of the mapped interrupt). 223 * Returns 1 if a mapping was found, 0 otherwise. 224 */ 225int 226ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz, 227 void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result, 228 int rintrsz) 229{ 230 phandle_t parent; 231 u_int8_t *ref = maskbuf; 232 u_int8_t *uiintr = intr; 233 u_int8_t *uiregs = regs; 234 u_int8_t *uiimapmsk = imapmsk; 235 u_int8_t *mptr; 236 pcell_t pintrsz; 237 int i, rsz, tsz; 238 239 rsz = -1; 240 if (imapmsk != NULL) { 241 for (i = 0; i < physsz; i++) 242 ref[i] = uiregs[i] & uiimapmsk[i]; 243 for (i = 0; i < intrsz; i++) 244 ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i]; 245 } else { 246 bcopy(regs, ref, physsz); 247 bcopy(intr, ref + physsz, intrsz); 248 } 249 250 mptr = imap; 251 i = imapsz; 252 tsz = physsz + intrsz + sizeof(phandle_t) + rintrsz; 253 while (i > 0) { 254 KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map")); 255 bcopy(mptr + physsz + intrsz, &parent, sizeof(parent)); 256 if (ofw_bus_searchprop(parent, "#interrupt-cells", 257 &pintrsz, sizeof(pintrsz)) == -1) 258 pintrsz = 1; /* default */ 259 pintrsz *= sizeof(pcell_t); 260 if (pintrsz != rintrsz) 261 panic("ofw_bus_search_intrmap: expected interrupt cell " 262 "size incorrect: %d > %d", rintrsz, pintrsz); 263 264 /* 265 * XXX: Apple hardware uses a second cell to set information 266 * on the interrupt trigger type. This information should 267 * be used somewhere to program the PIC. 268 */ 269 270 if (bcmp(ref, mptr, physsz + intrsz) == 0) { 271 bcopy(mptr + physsz + intrsz + sizeof(parent), 272 result, rintrsz); 273 return (1); 274 } 275 mptr += tsz; 276 i -= tsz; 277 } 278 return (0); 279} 280