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