isa.c revision 178579
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 178579 2008-04-26 11:01:38Z 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} 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 { "power", 0x0c0cd041 }, /* PNP0C0C */ 134 { NULL, 0x0 } 135}; 136 137static void 138isa_setup_children(device_t dev, phandle_t parent) 139{ 140 struct isa_regs *regs; 141 struct resource_list *rl; 142 device_t cdev; 143 u_int64_t end, start; 144 ofw_isa_intr_t *intrs, rintr; 145 phandle_t node; 146 uint32_t *drqs, *regidx; 147 int i, ndrq, nintr, nreg, nregidx, rid, rtype; 148 char *name; 149 150 /* 151 * Loop through children and fake up PnP devices for them. 152 * Their resources are added as fully mapped and specified because 153 * adjusting the resources and the resource list entries respectively 154 * in isa_alloc_resource() causes trouble with drivers which use 155 * rman_get_start(), pass-through or allocate and release resources 156 * multiple times, etc. Adjusting the resources might be better off 157 * in a bus_activate_resource method but the common ISA code doesn't 158 * allow for an isa_activate_resource(). 159 */ 160 for (node = OF_child(parent); node != 0; node = OF_peer(node)) { 161 if ((OF_getprop_alloc(node, "name", 1, (void **)&name)) == -1) 162 continue; 163 164 /* 165 * Keyboard and mouse controllers hang off of the `8042' 166 * node but we have no real use for the `8042' itself. 167 */ 168 if (strcmp(name, "8042") == 0) { 169 isa_setup_children(dev, node); 170 free(name, M_OFWPROP); 171 continue; 172 } 173 174 for (i = 0; ofw_isa_pnp_map[i].name != NULL; i++) 175 if (strcmp(ofw_isa_pnp_map[i].name, name) == 0) 176 break; 177 if (ofw_isa_pnp_map[i].name == NULL) { 178 device_printf(dev, "no PnP map entry for node " 179 "0x%lx: %s\n", (unsigned long)node, name); 180 free(name, M_OFWPROP); 181 continue; 182 } 183 184 if ((cdev = BUS_ADD_CHILD(dev, ISA_ORDER_PNPBIOS, NULL, -1)) == 185 NULL) 186 panic("isa_setup_children: BUS_ADD_CHILD failed"); 187 isa_set_logicalid(cdev, ofw_isa_pnp_map[i].id); 188 isa_set_vendorid(cdev, ofw_isa_pnp_map[i].id); 189 190 rl = BUS_GET_RESOURCE_LIST(dev, cdev); 191 nreg = OF_getprop_alloc(node, "reg", sizeof(*regs), 192 (void **)®s); 193 for (i = 0; i < nreg; i++) { 194 start = ISA_REG_PHYS(®s[i]); 195 end = start + regs[i].size - 1; 196 rtype = ofw_isa_range_map(isab_ranges, isab_nrange, 197 &start, &end, NULL); 198 rid = 0; 199 while (resource_list_find(rl, rtype, rid) != NULL) 200 rid++; 201 bus_set_resource(cdev, rtype, rid, start, 202 end - start + 1); 203 } 204 if (nreg == -1 && parent != isab_node) { 205 /* 206 * The "reg" property still might be an index into 207 * the set of registers of the parent device like 208 * with the nodes hanging off of the `8042' node. 209 */ 210 nregidx = OF_getprop_alloc(node, "reg", sizeof(*regidx), 211 (void **)®idx); 212 if (nregidx > 2) 213 panic("isa_setup_children: impossible number " 214 "of register indices"); 215 if (nregidx != -1 && (nreg = OF_getprop_alloc(parent, 216 "reg", sizeof(*regs), (void **)®s)) >= nregidx) { 217 for (i = 0; i < nregidx; i++) { 218 start = ISA_REG_PHYS(®s[regidx[i]]); 219 end = start + regs[regidx[i]].size - 1; 220 rtype = ofw_isa_range_map(isab_ranges, 221 isab_nrange, &start, &end, NULL); 222 rid = 0; 223 while (resource_list_find(rl, rtype, 224 rid) != NULL) 225 rid++; 226 bus_set_resource(cdev, rtype, rid, 227 start, end - start + 1); 228 } 229 } 230 if (regidx != NULL) 231 free(regidx, M_OFWPROP); 232 } 233 if (regs != NULL) 234 free(regs, M_OFWPROP); 235 236 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs), 237 (void **)&intrs); 238 for (i = 0; i < nintr; i++) { 239 if (intrs[i] > 7) 240 panic("isa_setup_children: intr too large"); 241 rintr = ofw_isa_route_intr(device_get_parent(dev), node, 242 &isa_iinfo, intrs[i]); 243 if (rintr == PCI_INVALID_IRQ) { 244 device_printf(dev, "could not map ISA " 245 "interrupt %d for node 0x%lx: %s\n", 246 intrs[i], (unsigned long)node, name); 247 continue; 248 } 249 bus_set_resource(cdev, SYS_RES_IRQ, i, rintr, 1); 250 } 251 if (intrs != NULL) 252 free(intrs, M_OFWPROP); 253 254 ndrq = OF_getprop_alloc(node, "dma-channel", sizeof(*drqs), 255 (void **)&drqs); 256 for (i = 0; i < ndrq; i++) 257 bus_set_resource(cdev, SYS_RES_DRQ, i, drqs[i], 1); 258 if (drqs != NULL) 259 free(drqs, M_OFWPROP); 260 261 /* 262 * Devices using DMA hang off of the `dma' node instead of 263 * directly from the ISA bridge node. 264 */ 265 if (strcmp(name, "dma") == 0) 266 isa_setup_children(dev, node); 267 268 free(name, M_OFWPROP); 269 } 270} 271 272struct resource * 273isa_alloc_resource(device_t bus, device_t child, int type, int *rid, 274 u_long start, u_long end, u_long count, u_int flags) 275{ 276 /* 277 * Consider adding a resource definition. 278 */ 279 int passthrough = (device_get_parent(child) != bus); 280 int isdefault = (start == 0UL && end == ~0UL); 281 struct resource_list *rl; 282 struct resource_list_entry *rle; 283 u_long base, limit; 284 285 rl = BUS_GET_RESOURCE_LIST(bus, child); 286 if (!passthrough && !isdefault) { 287 rle = resource_list_find(rl, type, *rid); 288 if (!rle) { 289 if (*rid < 0) 290 return (NULL); 291 switch (type) { 292 case SYS_RES_IRQ: 293 if (*rid >= ISA_NIRQ) 294 return (NULL); 295 break; 296 case SYS_RES_DRQ: 297 if (*rid >= ISA_NDRQ) 298 return (NULL); 299 break; 300 case SYS_RES_MEMORY: 301 if (*rid >= ISA_NMEM) 302 return (NULL); 303 break; 304 case SYS_RES_IOPORT: 305 if (*rid >= ISA_NPORT) 306 return (NULL); 307 break; 308 default: 309 return (NULL); 310 } 311 resource_list_add(rl, type, *rid, start, end, count); 312 } 313 } 314 315 /* 316 * Sanity check if the resource in the respective entry is fully 317 * mapped and specified and its type allocable. A driver could 318 * have added an out of range resource on its own. 319 */ 320 if (!passthrough) { 321 if ((rle = resource_list_find(rl, type, *rid)) == NULL) 322 return (NULL); 323 base = limit = 0; 324 switch (type) { 325 case SYS_RES_MEMORY: 326 base = isa_mem_base; 327 limit = base + isa_mem_limit; 328 break; 329 case SYS_RES_IOPORT: 330 base = isa_io_base; 331 limit = base + isa_io_limit; 332 break; 333 case SYS_RES_IRQ: 334 if (rle->start != rle->end || rle->start <= 7) 335 return (NULL); 336 break; 337 case SYS_RES_DRQ: 338 break; 339 default: 340 return (NULL); 341 } 342 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 343 if (!INRANGE(rle->start, base, limit) || 344 !INRANGE(rle->end, base, limit)) 345 return (NULL); 346 } 347 } 348 349 return (resource_list_alloc(rl, bus, child, type, rid, start, end, 350 count, flags)); 351} 352 353int 354isa_release_resource(device_t bus, device_t child, int type, int rid, 355 struct resource *res) 356{ 357 358 return (bus_generic_rl_release_resource(bus, child, type, rid, res)); 359} 360 361int 362isa_setup_intr(device_t dev, device_t child, struct resource *irq, int flags, 363 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep) 364{ 365 366 /* 367 * Just pass through. This is going to be handled by either 368 * one of the parent PCI buses or the nexus device. 369 * The interrupt had been routed before it was added to the 370 * resource list of the child. 371 */ 372 return (bus_generic_setup_intr(dev, child, irq, flags, filter, intr, 373 arg, cookiep)); 374} 375 376int 377isa_teardown_intr(device_t dev, device_t child, struct resource *irq, 378 void *cookie) 379{ 380 381 return (bus_generic_teardown_intr(dev, child, irq, cookie)); 382} 383