isa.c revision 173121
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 173121 2007-10-28 22:08:37Z 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 ofw_pci_intr_t isa_ino[8]; 68static struct ofw_bus_iinfo isa_iinfo; 69 70/* 71 * XXX: This is really partly PCI-specific, but unfortunately is 72 * differently enough to have to duplicate it here... 73 */ 74#define ISAB_RANGE_PHYS(r) \ 75 (((u_int64_t)(r)->phys_mid << 32) | (u_int64_t)(r)->phys_lo) 76#define ISAB_RANGE_SPACE(r) (((r)->phys_hi >> 24) & 0x03) 77 78#define ISAR_SPACE_IO 0x01 79#define ISAR_SPACE_MEM 0x02 80 81#define INRANGE(x, start, end) ((x) >= (start) && (x) <= (end)) 82 83static void isa_setup_children(device_t, phandle_t); 84 85intrmask_t 86isa_irq_pending(void) 87{ 88 intrmask_t pending; 89 int i; 90 91 /* XXX: Is this correct? */ 92 for (i = 7, pending = 0; i >= 0; i--) { 93 pending <<= 1; 94 if (isa_ino[i] != PCI_INVALID_IRQ) { 95 pending |= (OFW_PCI_INTR_PENDING(isa_bus_device, 96 isa_ino[i]) == 0) ? 0 : 1; 97 } 98 } 99 return (pending); 100} 101 102void 103isa_init(device_t dev) 104{ 105 device_t bridge; 106 int i; 107 108 /* The parent of the bus must be a PCI-ISA bridge. */ 109 bridge = device_get_parent(dev); 110 isab_node = ofw_bus_get_node(bridge); 111 isab_nrange = OF_getprop_alloc(isab_node, "ranges", 112 sizeof(*isab_ranges), (void **)&isab_ranges); 113 if (isab_nrange <= 0) 114 panic("isa_init: cannot get bridge range property"); 115 116 ofw_bus_setup_iinfo(isab_node, &isa_iinfo, sizeof(ofw_isa_intr_t)); 117 118 /* 119 * This is really a bad kludge; however, it is needed to provide 120 * isa_irq_pending(), which is unfortunately still used by some 121 * drivers. 122 * XXX: The only driver still using isa_irq_pending() is sio(4) 123 * which we don't use on sparc64. Should we just drop support 124 * for isa_irq_pending()? 125 */ 126 for (i = 0; i < 8; i++) 127 isa_ino[i] = PCI_INVALID_IRQ; 128 129 isa_setup_children(dev, isab_node); 130 131 for (i = isab_nrange - 1; i >= 0; i--) { 132 switch(ISAB_RANGE_SPACE(&isab_ranges[i])) { 133 case ISAR_SPACE_IO: 134 /* This is probably always 0. */ 135 isa_io_base = ISAB_RANGE_PHYS(&isab_ranges[i]); 136 isa_io_limit = isab_ranges[i].size; 137 break; 138 case ISAR_SPACE_MEM: 139 /* This is probably always 0. */ 140 isa_mem_base = ISAB_RANGE_PHYS(&isab_ranges[i]); 141 isa_mem_limit = isab_ranges[i].size; 142 break; 143 } 144 } 145} 146 147static const struct { 148 const char *name; 149 uint32_t id; 150} ofw_isa_pnp_map[] = { 151 { "SUNW,lomh", 0x0000ae4e }, /* SUN0000 */ 152 { "dma", 0x0002d041 }, /* PNP0200 */ 153 { "floppy", 0x0007d041 }, /* PNP0700 */ 154 { "rtc", 0x000bd041 }, /* PNP0B00 */ 155 { "flashprom", 0x0100ae4e }, /* SUN0001 */ 156 { "parallel", 0x0104d041 }, /* PNP0401 */ 157 { "serial", 0x0105d041 }, /* PNP0501 */ 158 { "i2c", 0x0200ae4e }, /* SUN0002 */ 159 { "rmc-comm", 0x0300ae4e }, /* SUN0003 */ 160 { "kb_ps2", 0x0303d041 }, /* PNP0303 */ 161 { "kdmouse", 0x030fd041 }, /* PNP0F03 */ 162 { "power", 0x0c0cd041 }, /* PNP0C0C */ 163 { NULL, 0x0 } 164}; 165 166static void 167isa_setup_children(device_t dev, phandle_t parent) 168{ 169 struct isa_regs *regs; 170 struct resource_list *rl; 171 device_t cdev; 172 u_int64_t end, start; 173 ofw_isa_intr_t *intrs, rintr; 174 phandle_t node; 175 uint32_t *drqs, *regidx; 176 int i, ndrq, nintr, nreg, nregidx, rid, rtype; 177 char *name; 178 179 /* 180 * Loop through children and fake up PnP devices for them. 181 * Their resources are added as fully mapped and specified because 182 * adjusting the resources and the resource list entries respectively 183 * in isa_alloc_resource() causes trouble with drivers which use 184 * rman_get_start(), pass-through or allocate and release resources 185 * multiple times, etc. Adjusting the resources might be better off 186 * in a bus_activate_resource method but the common ISA code doesn't 187 * allow for an isa_activate_resource(). 188 */ 189 for (node = OF_child(parent); node != 0; node = OF_peer(node)) { 190 if ((OF_getprop_alloc(node, "name", 1, (void **)&name)) == -1) 191 continue; 192 193 /* 194 * Keyboard and mouse controllers hang off of the `8042' 195 * node but we have no real use for the `8042' itself. 196 */ 197 if (strcmp(name, "8042") == 0) { 198 isa_setup_children(dev, node); 199 free(name, M_OFWPROP); 200 continue; 201 } 202 203 for (i = 0; ofw_isa_pnp_map[i].name != NULL; i++) 204 if (strcmp(ofw_isa_pnp_map[i].name, name) == 0) 205 break; 206 if (ofw_isa_pnp_map[i].name == NULL) { 207 device_printf(dev, "no PnP map entry for node " 208 "0x%lx: %s\n", (unsigned long)node, name); 209 free(name, M_OFWPROP); 210 continue; 211 } 212 213 if ((cdev = BUS_ADD_CHILD(dev, ISA_ORDER_PNPBIOS, NULL, -1)) == 214 NULL) 215 panic("isa_setup_children: BUS_ADD_CHILD failed"); 216 isa_set_logicalid(cdev, ofw_isa_pnp_map[i].id); 217 isa_set_vendorid(cdev, ofw_isa_pnp_map[i].id); 218 219 rl = BUS_GET_RESOURCE_LIST(dev, cdev); 220 nreg = OF_getprop_alloc(node, "reg", sizeof(*regs), 221 (void **)®s); 222 for (i = 0; i < nreg; i++) { 223 start = ISA_REG_PHYS(®s[i]); 224 end = start + regs[i].size - 1; 225 rtype = ofw_isa_range_map(isab_ranges, isab_nrange, 226 &start, &end, NULL); 227 rid = 0; 228 while (resource_list_find(rl, rtype, rid) != NULL) 229 rid++; 230 bus_set_resource(cdev, rtype, rid, start, 231 end - start + 1); 232 } 233 if (nreg == -1 && parent != isab_node) { 234 /* 235 * The "reg" property still might be an index into 236 * the set of registers of the parent device like 237 * with the nodes hanging off of the `8042' node. 238 */ 239 nregidx = OF_getprop_alloc(node, "reg", sizeof(*regidx), 240 (void **)®idx); 241 if (nregidx > 2) 242 panic("isa_setup_children: impossible number " 243 "of register indices"); 244 if (nregidx != -1 && (nreg = OF_getprop_alloc(parent, 245 "reg", sizeof(*regs), (void **)®s)) >= nregidx) { 246 for (i = 0; i < nregidx; i++) { 247 start = ISA_REG_PHYS(®s[regidx[i]]); 248 end = start + regs[regidx[i]].size - 1; 249 rtype = ofw_isa_range_map(isab_ranges, 250 isab_nrange, &start, &end, NULL); 251 rid = 0; 252 while (resource_list_find(rl, rtype, 253 rid) != NULL) 254 rid++; 255 bus_set_resource(cdev, rtype, rid, 256 start, end - start + 1); 257 } 258 } 259 if (regidx != NULL) 260 free(regidx, M_OFWPROP); 261 } 262 if (regs != NULL) 263 free(regs, M_OFWPROP); 264 265 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs), 266 (void **)&intrs); 267 for (i = 0; i < nintr; i++) { 268 if (intrs[i] > 7) 269 panic("isa_setup_children: intr too large"); 270 rintr = ofw_isa_route_intr(device_get_parent(dev), node, 271 &isa_iinfo, intrs[i]); 272 if (rintr == PCI_INVALID_IRQ) { 273 device_printf(dev, "could not map ISA " 274 "interrupt %d for node 0x%lx: %s\n", 275 intrs[i], (unsigned long)node, name); 276 continue; 277 } 278 isa_ino[intrs[i]] = rintr; 279 bus_set_resource(cdev, SYS_RES_IRQ, i, rintr, 1); 280 } 281 if (intrs != NULL) 282 free(intrs, M_OFWPROP); 283 284 ndrq = OF_getprop_alloc(node, "dma-channel", sizeof(*drqs), 285 (void **)&drqs); 286 for (i = 0; i < ndrq; i++) 287 bus_set_resource(cdev, SYS_RES_DRQ, i, drqs[i], 1); 288 if (drqs != NULL) 289 free(drqs, M_OFWPROP); 290 291 /* 292 * Devices using DMA hang off of the `dma' node instead of 293 * directly from the ISA bridge node. 294 */ 295 if (strcmp(name, "dma") == 0) 296 isa_setup_children(dev, node); 297 298 free(name, M_OFWPROP); 299 } 300} 301 302struct resource * 303isa_alloc_resource(device_t bus, device_t child, int type, int *rid, 304 u_long start, u_long end, u_long count, u_int flags) 305{ 306 /* 307 * Consider adding a resource definition. 308 */ 309 int passthrough = (device_get_parent(child) != bus); 310 int isdefault = (start == 0UL && end == ~0UL); 311 struct resource_list *rl; 312 struct resource_list_entry *rle; 313 u_long base, limit; 314 315 rl = BUS_GET_RESOURCE_LIST(bus, child); 316 if (!passthrough && !isdefault) { 317 rle = resource_list_find(rl, type, *rid); 318 if (!rle) { 319 if (*rid < 0) 320 return (NULL); 321 switch (type) { 322 case SYS_RES_IRQ: 323 if (*rid >= ISA_NIRQ) 324 return (NULL); 325 break; 326 case SYS_RES_DRQ: 327 if (*rid >= ISA_NDRQ) 328 return (NULL); 329 break; 330 case SYS_RES_MEMORY: 331 if (*rid >= ISA_NMEM) 332 return (NULL); 333 break; 334 case SYS_RES_IOPORT: 335 if (*rid >= ISA_NPORT) 336 return (NULL); 337 break; 338 default: 339 return (NULL); 340 } 341 resource_list_add(rl, type, *rid, start, end, count); 342 } 343 } 344 345 /* 346 * Sanity check if the resource in the respective entry is fully 347 * mapped and specified and its type allocable. A driver could 348 * have added an out of range resource on its own. 349 */ 350 if (!passthrough) { 351 if ((rle = resource_list_find(rl, type, *rid)) == NULL) 352 return (NULL); 353 base = limit = 0; 354 switch (type) { 355 case SYS_RES_MEMORY: 356 base = isa_mem_base; 357 limit = base + isa_mem_limit; 358 break; 359 case SYS_RES_IOPORT: 360 base = isa_io_base; 361 limit = base + isa_io_limit; 362 break; 363 case SYS_RES_IRQ: 364 if (rle->start != rle->end || rle->start <= 7) 365 return (NULL); 366 break; 367 case SYS_RES_DRQ: 368 break; 369 default: 370 return (NULL); 371 } 372 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 373 if (!INRANGE(rle->start, base, limit) || 374 !INRANGE(rle->end, base, limit)) 375 return (NULL); 376 } 377 } 378 379 return (resource_list_alloc(rl, bus, child, type, rid, start, end, 380 count, flags)); 381} 382 383int 384isa_release_resource(device_t bus, device_t child, int type, int rid, 385 struct resource *res) 386{ 387 388 return (bus_generic_rl_release_resource(bus, child, type, rid, res)); 389} 390 391int 392isa_setup_intr(device_t dev, device_t child, struct resource *irq, int flags, 393 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep) 394{ 395 396 /* 397 * Just pass through. This is going to be handled by either 398 * one of the parent PCI buses or the nexus device. 399 * The interrupt had been routed before it was added to the 400 * resource list of the child. 401 */ 402 return (bus_generic_setup_intr(dev, child, irq, flags, filter, intr, 403 arg, cookiep)); 404} 405 406int 407isa_teardown_intr(device_t dev, device_t child, struct resource *irq, 408 void *cookie) 409{ 410 411 return (bus_generic_teardown_intr(dev, child, irq, cookie)); 412} 413