isa.c revision 138506
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 138506 2004-12-07 05:30:02Z imp $"); 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 156struct ofw_isa_pnp_map { 157 const char *name; 158 uint32_t id; 159}; 160 161static struct ofw_isa_pnp_map pnp_map[] = { 162 { "SUNW,lomh", 0x0000ae4e }, /* SUN0000 */ 163 { "dma", 0x0002d041 }, /* PNP0200 */ 164 { "floppy", 0x0007d041 }, /* PNP0700 */ 165 { "rtc", 0x000bd041 }, /* PNP0B00 */ 166 { "flashprom", 0x0100ae4e }, /* SUN0001 */ 167 { "parallel", 0x0104d041 }, /* PNP0401 */ 168 { "serial", 0x0105d041 }, /* PNP0501 */ 169 { "kb_ps2", 0x0303d041 }, /* PNP0303 */ 170 { "kdmouse", 0x030fd041 }, /* PNP0F03 */ 171 { "power", 0x0c0cd041 }, /* PNP0C0C */ 172 { NULL, 0x0 } 173}; 174 175static void 176isa_setup_children(device_t dev, phandle_t parent) 177{ 178 struct isa_regs *regs; 179 device_t cdev; 180 u_int64_t end, start; 181 ofw_isa_intr_t *intrs, rintr; 182 phandle_t node; 183 uint32_t *drqs, *regidx; 184 int i, ndrq, nintr, nreg, nregidx, rtype; 185 char *name; 186 187 /* 188 * Loop through children and fake up PnP devices for them. 189 * Their resources are added as fully mapped and specified because 190 * adjusting the resources and the resource list entries respectively 191 * in isa_alloc_resource() causes trouble with drivers which use 192 * rman_get_start(), pass-through or allocate and release resources 193 * multiple times, etc. Adjusting the resources might be better off 194 * in a bus_activate_resource method but the common ISA code doesn't 195 * allow for an isa_activate_resource(). 196 */ 197 for (node = OF_child(parent); node != 0; node = OF_peer(node)) { 198 if ((OF_getprop_alloc(node, "name", 1, (void **)&name)) == -1) 199 continue; 200 201 /* 202 * Keyboard and mouse controllers hang off of the `8042' 203 * node but we have no real use for the `8042' itself. 204 */ 205 if (strcmp(name, "8042") == 0) { 206 isa_setup_children(dev, node); 207 free(name, M_OFWPROP); 208 continue; 209 } 210 211 for (i = 0; pnp_map[i].name != NULL; i++) 212 if (strcmp(pnp_map[i].name, name) == 0) 213 break; 214 if (i == (sizeof(pnp_map) / sizeof(*pnp_map)) - 1) { 215 printf("isa_setup_children: no PnP map entry for node " 216 "0x%lx: %s\n", (unsigned long)node, name); 217 continue; 218 } 219 220 if ((cdev = BUS_ADD_CHILD(dev, ISA_ORDER_PNPBIOS, NULL, -1)) == 221 NULL) 222 panic("isa_setup_children: BUS_ADD_CHILD failed"); 223 isa_set_logicalid(cdev, pnp_map[i].id); 224 isa_set_vendorid(cdev, pnp_map[i].id); 225 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 bus_set_resource(cdev, rtype, i, start, 234 end - start + 1); 235 } 236 if (nreg == -1 && parent != isab_node) { 237 /* 238 * The "reg" property still might be an index into 239 * the set of registers of the parent device like 240 * with the nodes hanging off of the `8042' node. 241 */ 242 nregidx = OF_getprop_alloc(node, "reg", sizeof(*regidx), 243 (void **)®idx); 244 if (nregidx > 2) 245 panic("isa_setup_children: impossible number " 246 "of register indices"); 247 if (nregidx != -1 && (nreg = OF_getprop_alloc(parent, 248 "reg", sizeof(*regs), (void **)®s)) >= nregidx) { 249 for (i = 0; i < nregidx; i++) { 250 start = ISA_REG_PHYS(®s[regidx[i]]); 251 end = start + regs[regidx[i]].size - 1; 252 rtype = ofw_isa_range_map(isab_ranges, 253 isab_nrange, &start, &end, NULL); 254 bus_set_resource(cdev, rtype, i, start, 255 end - start + 1); 256 } 257 } 258 if (regidx != NULL) 259 free(regidx, M_OFWPROP); 260 } 261 if (regs != NULL) 262 free(regs, M_OFWPROP); 263 264 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs), 265 (void **)&intrs); 266 for (i = 0; i < nintr; i++) { 267 if (intrs[i] > 7) 268 panic("isa_setup_children: intr too large"); 269 rintr = ofw_isa_route_intr(device_get_parent(dev), node, 270 &isa_iinfo, intrs[i]); 271 if (rintr == PCI_INVALID_IRQ) 272 panic("isa_setup_children: could not map ISA " 273 "interrupt %d", intrs[i]); 274 isa_ino[intrs[i]] = rintr; 275 bus_set_resource(cdev, SYS_RES_IRQ, i, rintr, 1); 276 } 277 if (intrs != NULL) 278 free(intrs, M_OFWPROP); 279 280 ndrq = OF_getprop_alloc(node, "dma-channel", sizeof(*drqs), 281 (void **)&drqs); 282 for (i = 0; i < ndrq; i++) 283 bus_set_resource(cdev, SYS_RES_DRQ, i, drqs[i], 1); 284 if (drqs != NULL) 285 free(drqs, M_OFWPROP); 286 287 /* 288 * Devices using DMA hang off of the `dma' node instead of 289 * directly from the ISA bridge node. 290 */ 291 if (strcmp(name, "dma") == 0) 292 isa_setup_children(dev, node); 293 294 free(name, M_OFWPROP); 295 } 296} 297 298struct resource * 299isa_alloc_resource(device_t bus, device_t child, int type, int *rid, 300 u_long start, u_long end, u_long count, u_int flags) 301{ 302 /* 303 * Consider adding a resource definition. 304 */ 305 int passthrough = (device_get_parent(child) != bus); 306 int isdefault = (start == 0UL && end == ~0UL); 307 struct isa_device* idev = DEVTOISA(child); 308 struct resource_list *rl = &idev->id_resources; 309 struct resource_list_entry *rle; 310 u_long base, limit; 311 312 if (!passthrough && !isdefault) { 313 rle = resource_list_find(rl, type, *rid); 314 if (!rle) { 315 if (*rid < 0) 316 return 0; 317 switch (type) { 318 case SYS_RES_IRQ: 319 if (*rid >= ISA_NIRQ) 320 return 0; 321 break; 322 case SYS_RES_DRQ: 323 if (*rid >= ISA_NDRQ) 324 return 0; 325 break; 326 case SYS_RES_MEMORY: 327 if (*rid >= ISA_NMEM) 328 return 0; 329 break; 330 case SYS_RES_IOPORT: 331 if (*rid >= ISA_NPORT) 332 return 0; 333 break; 334 default: 335 return 0; 336 } 337 resource_list_add(rl, type, *rid, start, end, count); 338 } 339 } 340 341 /* 342 * Sanity check if the resource in the respective entry is fully 343 * mapped and specified and its type allocable. A driver could 344 * have added an out of range resource on its own. 345 */ 346 if (!passthrough) { 347 if ((rle = resource_list_find(rl, type, *rid)) == NULL) 348 return (NULL); 349 base = limit = 0; 350 switch (type) { 351 case SYS_RES_MEMORY: 352 if (isa_mem_bt == NULL) 353 return (NULL); 354 base = isa_mem_base; 355 limit = base + isa_mem_limit; 356 break; 357 case SYS_RES_IOPORT: 358 if (isa_io_bt == NULL) 359 return (NULL); 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 struct isa_device* idev = DEVTOISA(child); 388 struct resource_list *rl = &idev->id_resources; 389 390 return (resource_list_release(rl, bus, child, type, rid, res)); 391} 392 393int 394isa_setup_intr(device_t dev, device_t child, 395 struct resource *irq, int flags, 396 driver_intr_t *intr, void *arg, void **cookiep) 397{ 398 399 /* 400 * Just pass through. This is going to be handled by either one of 401 * the parent PCI buses or the nexus device. 402 * The interrupt was routed at allocation time. 403 */ 404 return (BUS_SETUP_INTR(device_get_parent(dev), child, irq, flags, intr, 405 arg, cookiep)); 406} 407 408int 409isa_teardown_intr(device_t dev, device_t child, 410 struct resource *irq, void *cookie) 411{ 412 413 return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie)); 414} 415