isa.c revision 190111
1/*- 2 * Copyright (c) 1998 Doug Rabson 3 * Copyright (c) 2001 Thomas Moestl <tmm@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 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * from: FreeBSD: src/sys/alpha/isa/isa.c,v 1.26 2001/07/11 28 */ 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: head/sys/sparc64/isa/isa.c 190111 2009-03-19 20:57:59Z marius $"); 32 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/bus.h> 36 37#include <machine/bus.h> 38 39#include <sys/rman.h> 40 41#include <isa/isareg.h> 42#include <isa/isavar.h> 43#include <isa/isa_common.h> 44 45#include <dev/ofw/ofw_bus.h> 46#include <dev/ofw/openfirm.h> 47 48#include <machine/resource.h> 49 50#include <dev/pci/pcireg.h> 51#include <dev/pci/pcivar.h> 52 53#include <sparc64/pci/ofw_pci.h> 54#include <sparc64/isa/ofw_isa.h> 55 56/* There can be only one ISA bus, so it is safe to use globals. */ 57static u_int64_t isa_io_base; 58static u_int64_t isa_io_limit; 59static u_int64_t isa_mem_base; 60static u_int64_t isa_mem_limit; 61 62device_t isa_bus_device; 63 64static phandle_t isab_node; 65static struct isa_ranges *isab_ranges; 66static int isab_nrange; 67static struct ofw_bus_iinfo isa_iinfo; 68 69/* 70 * XXX: This is really partly PCI-specific, but unfortunately is 71 * differently enough to have to duplicate it here... 72 */ 73#define ISAB_RANGE_PHYS(r) \ 74 (((u_int64_t)(r)->phys_mid << 32) | (u_int64_t)(r)->phys_lo) 75#define ISAB_RANGE_SPACE(r) (((r)->phys_hi >> 24) & 0x03) 76 77#define ISAR_SPACE_IO 0x01 78#define ISAR_SPACE_MEM 0x02 79 80#define INRANGE(x, start, end) ((x) >= (start) && (x) <= (end)) 81 82static void isa_setup_children(device_t, phandle_t); 83 84void 85isa_init(device_t dev) 86{ 87 device_t bridge; 88 int i; 89 90 /* The parent of the bus must be a PCI-ISA bridge. */ 91 bridge = device_get_parent(dev); 92 isab_node = ofw_bus_get_node(bridge); 93 isab_nrange = OF_getprop_alloc(isab_node, "ranges", 94 sizeof(*isab_ranges), (void **)&isab_ranges); 95 if (isab_nrange <= 0) 96 panic("isa_init: cannot get bridge range property"); 97 98 ofw_bus_setup_iinfo(isab_node, &isa_iinfo, sizeof(ofw_isa_intr_t)); 99 100 isa_setup_children(dev, isab_node); 101 102 for (i = isab_nrange - 1; i >= 0; i--) { 103 switch(ISAB_RANGE_SPACE(&isab_ranges[i])) { 104 case ISAR_SPACE_IO: 105 /* This is probably always 0. */ 106 isa_io_base = ISAB_RANGE_PHYS(&isab_ranges[i]); 107 isa_io_limit = isab_ranges[i].size; 108 break; 109 case ISAR_SPACE_MEM: 110 /* This is probably always 0. */ 111 isa_mem_base = ISAB_RANGE_PHYS(&isab_ranges[i]); 112 isa_mem_limit = isab_ranges[i].size; 113 break; 114 } 115 } 116} 117 118static const struct { 119 const char *name; 120 uint32_t id; 121} const ofw_isa_pnp_map[] = { 122 { "SUNW,lomh", 0x0000ae4e }, /* SUN0000 */ 123 { "dma", 0x0002d041 }, /* PNP0200 */ 124 { "floppy", 0x0007d041 }, /* PNP0700 */ 125 { "rtc", 0x000bd041 }, /* PNP0B00 */ 126 { "flashprom", 0x0100ae4e }, /* SUN0001 */ 127 { "parallel", 0x0104d041 }, /* PNP0401 */ 128 { "serial", 0x0105d041 }, /* PNP0501 */ 129 { "i2c", 0x0200ae4e }, /* SUN0002 */ 130 { "rmc-comm", 0x0300ae4e }, /* SUN0003 */ 131 { "kb_ps2", 0x0303d041 }, /* PNP0303 */ 132 { "kdmouse", 0x030fd041 }, /* PNP0F03 */ 133 { "bscbus", 0x0400ae4e }, /* SUN0004 */ 134 { "power", 0x0c0cd041 }, /* PNP0C0C */ 135 { NULL, 0x0 } 136}; 137 138static void 139isa_setup_children(device_t dev, phandle_t parent) 140{ 141 struct isa_regs *regs; 142 struct resource_list *rl; 143 device_t cdev; 144 u_int64_t end, start; 145 ofw_isa_intr_t *intrs, rintr; 146 phandle_t node; 147 uint32_t *drqs, *regidx; 148 int i, ndrq, nintr, nreg, nregidx, rid, rtype; 149 char *name; 150 151 /* 152 * Loop through children and fake up PnP devices for them. 153 * Their resources are added as fully mapped and specified because 154 * adjusting the resources and the resource list entries respectively 155 * in isa_alloc_resource() causes trouble with drivers which use 156 * rman_get_start(), pass-through or allocate and release resources 157 * multiple times, etc. Adjusting the resources might be better off 158 * in a bus_activate_resource method but the common ISA code doesn't 159 * allow for an isa_activate_resource(). 160 */ 161 for (node = OF_child(parent); node != 0; node = OF_peer(node)) { 162 if ((OF_getprop_alloc(node, "name", 1, (void **)&name)) == -1) 163 continue; 164 165 /* 166 * Keyboard and mouse controllers hang off of the `8042' 167 * node but we have no real use for the `8042' itself. 168 */ 169 if (strcmp(name, "8042") == 0) { 170 isa_setup_children(dev, node); 171 free(name, M_OFWPROP); 172 continue; 173 } 174 175 for (i = 0; ofw_isa_pnp_map[i].name != NULL; i++) 176 if (strcmp(ofw_isa_pnp_map[i].name, name) == 0) 177 break; 178 if (ofw_isa_pnp_map[i].name == NULL) { 179 device_printf(dev, "no PnP map entry for node " 180 "0x%lx: %s\n", (unsigned long)node, name); 181 free(name, M_OFWPROP); 182 continue; 183 } 184 185 if ((cdev = BUS_ADD_CHILD(dev, ISA_ORDER_PNPBIOS, NULL, -1)) == 186 NULL) 187 panic("isa_setup_children: BUS_ADD_CHILD failed"); 188 isa_set_logicalid(cdev, ofw_isa_pnp_map[i].id); 189 isa_set_vendorid(cdev, ofw_isa_pnp_map[i].id); 190 191 rl = BUS_GET_RESOURCE_LIST(dev, cdev); 192 nreg = OF_getprop_alloc(node, "reg", sizeof(*regs), 193 (void **)®s); 194 for (i = 0; i < nreg; i++) { 195 start = ISA_REG_PHYS(®s[i]); 196 end = start + regs[i].size - 1; 197 rtype = ofw_isa_range_map(isab_ranges, isab_nrange, 198 &start, &end, NULL); 199 rid = 0; 200 while (resource_list_find(rl, rtype, rid) != NULL) 201 rid++; 202 bus_set_resource(cdev, rtype, rid, start, 203 end - start + 1); 204 } 205 if (nreg == -1 && parent != isab_node) { 206 /* 207 * The "reg" property still might be an index into 208 * the set of registers of the parent device like 209 * with the nodes hanging off of the `8042' node. 210 */ 211 nregidx = OF_getprop_alloc(node, "reg", sizeof(*regidx), 212 (void **)®idx); 213 if (nregidx > 2) 214 panic("isa_setup_children: impossible number " 215 "of register indices"); 216 if (nregidx != -1 && (nreg = OF_getprop_alloc(parent, 217 "reg", sizeof(*regs), (void **)®s)) >= nregidx) { 218 for (i = 0; i < nregidx; i++) { 219 start = ISA_REG_PHYS(®s[regidx[i]]); 220 end = start + regs[regidx[i]].size - 1; 221 rtype = ofw_isa_range_map(isab_ranges, 222 isab_nrange, &start, &end, NULL); 223 rid = 0; 224 while (resource_list_find(rl, rtype, 225 rid) != NULL) 226 rid++; 227 bus_set_resource(cdev, rtype, rid, 228 start, end - start + 1); 229 } 230 } 231 if (regidx != NULL) 232 free(regidx, M_OFWPROP); 233 } 234 if (regs != NULL) 235 free(regs, M_OFWPROP); 236 237 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs), 238 (void **)&intrs); 239 for (i = 0; i < nintr; i++) { 240 if (intrs[i] > 7) 241 panic("isa_setup_children: intr too large"); 242 rintr = ofw_isa_route_intr(device_get_parent(dev), node, 243 &isa_iinfo, intrs[i]); 244 if (rintr == PCI_INVALID_IRQ) { 245 device_printf(dev, "could not map ISA " 246 "interrupt %d for node 0x%lx: %s\n", 247 intrs[i], (unsigned long)node, name); 248 continue; 249 } 250 bus_set_resource(cdev, SYS_RES_IRQ, i, rintr, 1); 251 } 252 if (intrs != NULL) 253 free(intrs, M_OFWPROP); 254 255 ndrq = OF_getprop_alloc(node, "dma-channel", sizeof(*drqs), 256 (void **)&drqs); 257 for (i = 0; i < ndrq; i++) 258 bus_set_resource(cdev, SYS_RES_DRQ, i, drqs[i], 1); 259 if (drqs != NULL) 260 free(drqs, M_OFWPROP); 261 262 /* 263 * Devices using DMA hang off of the `dma' node instead of 264 * directly from the ISA bridge node. 265 */ 266 if (strcmp(name, "dma") == 0) 267 isa_setup_children(dev, node); 268 269 free(name, M_OFWPROP); 270 } 271} 272 273struct resource * 274isa_alloc_resource(device_t bus, device_t child, int type, int *rid, 275 u_long start, u_long end, u_long count, u_int flags) 276{ 277 /* 278 * Consider adding a resource definition. 279 */ 280 int passthrough = (device_get_parent(child) != bus); 281 int isdefault = (start == 0UL && end == ~0UL); 282 struct resource_list *rl; 283 struct resource_list_entry *rle; 284 u_long base, limit; 285 286 rl = BUS_GET_RESOURCE_LIST(bus, child); 287 if (!passthrough && !isdefault) { 288 rle = resource_list_find(rl, type, *rid); 289 if (!rle) { 290 if (*rid < 0) 291 return (NULL); 292 switch (type) { 293 case SYS_RES_IRQ: 294 if (*rid >= ISA_NIRQ) 295 return (NULL); 296 break; 297 case SYS_RES_DRQ: 298 if (*rid >= ISA_NDRQ) 299 return (NULL); 300 break; 301 case SYS_RES_MEMORY: 302 if (*rid >= ISA_NMEM) 303 return (NULL); 304 break; 305 case SYS_RES_IOPORT: 306 if (*rid >= ISA_NPORT) 307 return (NULL); 308 break; 309 default: 310 return (NULL); 311 } 312 resource_list_add(rl, type, *rid, start, end, count); 313 } 314 } 315 316 /* 317 * Sanity check if the resource in the respective entry is fully 318 * mapped and specified and its type allocable. A driver could 319 * have added an out of range resource on its own. 320 */ 321 if (!passthrough) { 322 if ((rle = resource_list_find(rl, type, *rid)) == NULL) 323 return (NULL); 324 base = limit = 0; 325 switch (type) { 326 case SYS_RES_MEMORY: 327 base = isa_mem_base; 328 limit = base + isa_mem_limit; 329 break; 330 case SYS_RES_IOPORT: 331 base = isa_io_base; 332 limit = base + isa_io_limit; 333 break; 334 case SYS_RES_IRQ: 335 if (rle->start != rle->end || rle->start <= 7) 336 return (NULL); 337 break; 338 case SYS_RES_DRQ: 339 break; 340 default: 341 return (NULL); 342 } 343 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 344 if (!INRANGE(rle->start, base, limit) || 345 !INRANGE(rle->end, base, limit)) 346 return (NULL); 347 } 348 } 349 350 return (resource_list_alloc(rl, bus, child, type, rid, start, end, 351 count, flags)); 352} 353 354int 355isa_release_resource(device_t bus, device_t child, int type, int rid, 356 struct resource *res) 357{ 358 359 return (bus_generic_rl_release_resource(bus, child, type, rid, res)); 360} 361 362int 363isa_setup_intr(device_t dev, device_t child, struct resource *irq, int flags, 364 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep) 365{ 366 367 /* 368 * Just pass through. This is going to be handled by either 369 * one of the parent PCI buses or the nexus device. 370 * The interrupt had been routed before it was added to the 371 * resource list of the child. 372 */ 373 return (bus_generic_setup_intr(dev, child, irq, flags, filter, intr, 374 arg, cookiep)); 375} 376 377int 378isa_teardown_intr(device_t dev, device_t child, struct resource *irq, 379 void *cookie) 380{ 381 382 return (bus_generic_teardown_intr(dev, child, irq, cookie)); 383} 384