isa.c revision 159289
186232Stmm/*- 286232Stmm * Copyright (c) 1998 Doug Rabson 386232Stmm * Copyright (c) 2001 Thomas Moestl <tmm@FreeBSD.org> 486232Stmm * All rights reserved. 586232Stmm * 686232Stmm * Redistribution and use in source and binary forms, with or without 786232Stmm * modification, are permitted provided that the following conditions 886232Stmm * are met: 986232Stmm * 1. Redistributions of source code must retain the above copyright 1086232Stmm * notice, this list of conditions and the following disclaimer. 1186232Stmm * 2. Redistributions in binary form must reproduce the above copyright 1286232Stmm * notice, this list of conditions and the following disclaimer in the 1386232Stmm * documentation and/or other materials provided with the distribution. 1486232Stmm * 1586232Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1686232Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1786232Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1886232Stmm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1986232Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2086232Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2186232Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2286232Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2386232Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2486232Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2586232Stmm * SUCH DAMAGE. 2686232Stmm * 2786232Stmm * from: FreeBSD: src/sys/alpha/isa/isa.c,v 1.26 2001/07/11 2886232Stmm */ 2986232Stmm 30137819Smarius#include <sys/cdefs.h> 31137819Smarius__FBSDID("$FreeBSD: head/sys/sparc64/isa/isa.c 159289 2006-06-05 17:48:54Z marius $"); 32137819Smarius 3386232Stmm#include <sys/param.h> 3486232Stmm#include <sys/systm.h> 3586232Stmm#include <sys/bus.h> 36129051Smarius 3786232Stmm#include <machine/bus.h> 38129051Smarius 3986232Stmm#include <sys/rman.h> 4086232Stmm 4186232Stmm#include <isa/isareg.h> 4286232Stmm#include <isa/isavar.h> 4386232Stmm#include <isa/isa_common.h> 4486232Stmm 45133589Smarius#include <dev/ofw/ofw_bus.h> 46119338Simp#include <dev/ofw/openfirm.h> 4786232Stmm 4886232Stmm#include <machine/resource.h> 4986232Stmm 50129051Smarius#include <dev/pci/pcireg.h> 51129051Smarius#include <dev/pci/pcivar.h> 52129051Smarius 5386232Stmm#include <sparc64/pci/ofw_pci.h> 5486232Stmm#include <sparc64/isa/ofw_isa.h> 5586232Stmm 5686232Stmm/* There can be only one ISA bus, so it is safe to use globals. */ 5786232Stmmbus_space_tag_t isa_io_bt = NULL; 5886232Stmmbus_space_handle_t isa_io_hdl; 5986232Stmmbus_space_tag_t isa_mem_bt = NULL; 6086232Stmmbus_space_handle_t isa_mem_hdl; 6186232Stmm 62129051Smariusstatic u_int64_t isa_io_base; 63129051Smariusstatic u_int64_t isa_io_limit; 64129051Smariusstatic u_int64_t isa_mem_base; 65129051Smariusstatic u_int64_t isa_mem_limit; 6686232Stmm 6786232Stmmdevice_t isa_bus_device; 6886232Stmm 6986232Stmmstatic phandle_t isab_node; 70137819Smariusstatic struct isa_ranges *isab_ranges; 71137819Smariusstatic int isab_nrange; 72117119Stmmstatic ofw_pci_intr_t isa_ino[8]; 73137819Smariusstatic struct ofw_bus_iinfo isa_iinfo; 74117119Stmm 7586232Stmm/* 76137819Smarius * XXX: This is really partly PCI-specific, but unfortunately is 7786232Stmm * differently enough to have to duplicate it here... 7886232Stmm */ 7986232Stmm#define ISAB_RANGE_PHYS(r) \ 8086232Stmm (((u_int64_t)(r)->phys_mid << 32) | (u_int64_t)(r)->phys_lo) 8186232Stmm#define ISAB_RANGE_SPACE(r) (((r)->phys_hi >> 24) & 0x03) 8286232Stmm 8386232Stmm#define ISAR_SPACE_IO 0x01 8486232Stmm#define ISAR_SPACE_MEM 0x02 8586232Stmm 8686232Stmm#define INRANGE(x, start, end) ((x) >= (start) && (x) <= (end)) 8786232Stmm 88137819Smariusstatic void isa_setup_children(device_t, phandle_t); 8986232Stmm 9086232Stmmintrmask_t 9186232Stmmisa_irq_pending(void) 9286232Stmm{ 9386232Stmm intrmask_t pending; 9486232Stmm int i; 9586232Stmm 9686232Stmm /* XXX: Is this correct? */ 9786232Stmm for (i = 7, pending = 0; i >= 0; i--) { 98117119Stmm pending <<= 1; 99117119Stmm if (isa_ino[i] != PCI_INVALID_IRQ) { 100117119Stmm pending |= (OFW_PCI_INTR_PENDING(isa_bus_device, 10186232Stmm isa_ino[i]) == 0) ? 0 : 1; 10286232Stmm } 10386232Stmm } 10486232Stmm return (pending); 10586232Stmm} 10686232Stmm 10786232Stmmvoid 10886232Stmmisa_init(device_t dev) 10986232Stmm{ 11086232Stmm device_t bridge; 111137819Smarius int i; 11286232Stmm 11386232Stmm /* The parent of the bus must be a PCI-ISA bridge. */ 11486232Stmm bridge = device_get_parent(dev); 115133589Smarius isab_node = ofw_bus_get_node(bridge); 116137819Smarius isab_nrange = OF_getprop_alloc(isab_node, "ranges", 117137819Smarius sizeof(*isab_ranges), (void **)&isab_ranges); 118137819Smarius if (isab_nrange <= 0) 11986232Stmm panic("isa_init: cannot get bridge range property"); 120117119Stmm 121117119Stmm ofw_bus_setup_iinfo(isab_node, &isa_iinfo, sizeof(ofw_isa_intr_t)); 122117119Stmm 12393067Stmm /* 124117119Stmm * This is really a bad kludge; however, it is needed to provide 125117119Stmm * isa_irq_pending(), which is unfortunately still used by some 126117119Stmm * drivers. 127137819Smarius * XXX: The only driver still using isa_irq_pending() is sio(4) 128137819Smarius * which we don't use on sparc64. Should we just drop support 129137819Smarius * for isa_irq_pending()? 13093067Stmm */ 13193067Stmm for (i = 0; i < 8; i++) 132117119Stmm isa_ino[i] = PCI_INVALID_IRQ; 13386232Stmm 134137819Smarius isa_setup_children(dev, isab_node); 135137819Smarius 136137819Smarius for (i = isab_nrange - 1; i >= 0; i--) { 137137819Smarius switch(ISAB_RANGE_SPACE(&isab_ranges[i])) { 13886232Stmm case ISAR_SPACE_IO: 13986232Stmm /* This is probably always 0. */ 140137819Smarius isa_io_base = ISAB_RANGE_PHYS(&isab_ranges[i]); 141137819Smarius isa_io_limit = isab_ranges[i].size; 142117119Stmm isa_io_hdl = OFW_PCI_GET_BUS_HANDLE(bridge, 143117119Stmm SYS_RES_IOPORT, isa_io_base, &isa_io_bt); 14486232Stmm break; 14586232Stmm case ISAR_SPACE_MEM: 14686232Stmm /* This is probably always 0. */ 147137819Smarius isa_mem_base = ISAB_RANGE_PHYS(&isab_ranges[i]); 148137819Smarius isa_mem_limit = isab_ranges[i].size; 149117119Stmm isa_mem_hdl = OFW_PCI_GET_BUS_HANDLE(bridge, 150117119Stmm SYS_RES_MEMORY, isa_mem_base, &isa_mem_bt); 15186232Stmm break; 15286232Stmm } 15386232Stmm } 15486232Stmm} 15586232Stmm 156159289Smariusstatic const struct { 157137819Smarius const char *name; 158137819Smarius uint32_t id; 159146407Smarius} ofw_isa_pnp_map[] = { 160137819Smarius { "SUNW,lomh", 0x0000ae4e }, /* SUN0000 */ 161137819Smarius { "dma", 0x0002d041 }, /* PNP0200 */ 162137819Smarius { "floppy", 0x0007d041 }, /* PNP0700 */ 163137819Smarius { "rtc", 0x000bd041 }, /* PNP0B00 */ 164137819Smarius { "flashprom", 0x0100ae4e }, /* SUN0001 */ 165137819Smarius { "parallel", 0x0104d041 }, /* PNP0401 */ 166137819Smarius { "serial", 0x0105d041 }, /* PNP0501 */ 167159289Smarius { "i2c", 0x0200ae4e }, /* SUN0002 */ 168159289Smarius { "rmc-comm", 0x0300ae4e }, /* SUN0003 */ 169137819Smarius { "kb_ps2", 0x0303d041 }, /* PNP0303 */ 170137819Smarius { "kdmouse", 0x030fd041 }, /* PNP0F03 */ 171137819Smarius { "power", 0x0c0cd041 }, /* PNP0C0C */ 172137819Smarius { NULL, 0x0 } 173137819Smarius}; 174137819Smarius 175137819Smariusstatic void 176137819Smariusisa_setup_children(device_t dev, phandle_t parent) 17786232Stmm{ 178137819Smarius struct isa_regs *regs; 179146407Smarius struct resource_list *rl; 180137819Smarius device_t cdev; 181137819Smarius u_int64_t end, start; 182137819Smarius ofw_isa_intr_t *intrs, rintr; 183137819Smarius phandle_t node; 184137819Smarius uint32_t *drqs, *regidx; 185146407Smarius int i, ndrq, nintr, nreg, nregidx, rid, rtype; 186137819Smarius char *name; 18786232Stmm 188137819Smarius /* 189137819Smarius * Loop through children and fake up PnP devices for them. 190137819Smarius * Their resources are added as fully mapped and specified because 191137819Smarius * adjusting the resources and the resource list entries respectively 192137819Smarius * in isa_alloc_resource() causes trouble with drivers which use 193137819Smarius * rman_get_start(), pass-through or allocate and release resources 194137819Smarius * multiple times, etc. Adjusting the resources might be better off 195137819Smarius * in a bus_activate_resource method but the common ISA code doesn't 196137819Smarius * allow for an isa_activate_resource(). 197137819Smarius */ 198137819Smarius for (node = OF_child(parent); node != 0; node = OF_peer(node)) { 199137819Smarius if ((OF_getprop_alloc(node, "name", 1, (void **)&name)) == -1) 200137819Smarius continue; 201137819Smarius 202137819Smarius /* 203137819Smarius * Keyboard and mouse controllers hang off of the `8042' 204137819Smarius * node but we have no real use for the `8042' itself. 205137819Smarius */ 206137819Smarius if (strcmp(name, "8042") == 0) { 207137819Smarius isa_setup_children(dev, node); 208137819Smarius free(name, M_OFWPROP); 209137819Smarius continue; 210137819Smarius } 211137819Smarius 212146407Smarius for (i = 0; ofw_isa_pnp_map[i].name != NULL; i++) 213146407Smarius if (strcmp(ofw_isa_pnp_map[i].name, name) == 0) 214137819Smarius break; 215146407Smarius if (ofw_isa_pnp_map[i].name == NULL) { 216137819Smarius printf("isa_setup_children: no PnP map entry for node " 217137819Smarius "0x%lx: %s\n", (unsigned long)node, name); 218159289Smarius free(name, M_OFWPROP); 219137819Smarius continue; 220137819Smarius } 221137819Smarius 222138506Simp if ((cdev = BUS_ADD_CHILD(dev, ISA_ORDER_PNPBIOS, NULL, -1)) == 223137819Smarius NULL) 224137819Smarius panic("isa_setup_children: BUS_ADD_CHILD failed"); 225146407Smarius isa_set_logicalid(cdev, ofw_isa_pnp_map[i].id); 226146407Smarius isa_set_vendorid(cdev, ofw_isa_pnp_map[i].id); 227137819Smarius 228146407Smarius rl = BUS_GET_RESOURCE_LIST(dev, cdev); 229137819Smarius nreg = OF_getprop_alloc(node, "reg", sizeof(*regs), 230137819Smarius (void **)®s); 231137819Smarius for (i = 0; i < nreg; i++) { 232137819Smarius start = ISA_REG_PHYS(®s[i]); 233137819Smarius end = start + regs[i].size - 1; 234137819Smarius rtype = ofw_isa_range_map(isab_ranges, isab_nrange, 235137819Smarius &start, &end, NULL); 236146407Smarius rid = 0; 237146407Smarius while (resource_list_find(rl, rtype, rid) != NULL) 238146407Smarius rid++; 239146407Smarius bus_set_resource(cdev, rtype, rid, start, 240137819Smarius end - start + 1); 241137819Smarius } 242137819Smarius if (nreg == -1 && parent != isab_node) { 243137819Smarius /* 244137819Smarius * The "reg" property still might be an index into 245137819Smarius * the set of registers of the parent device like 246137819Smarius * with the nodes hanging off of the `8042' node. 247137819Smarius */ 248137819Smarius nregidx = OF_getprop_alloc(node, "reg", sizeof(*regidx), 249137819Smarius (void **)®idx); 250137819Smarius if (nregidx > 2) 251137819Smarius panic("isa_setup_children: impossible number " 252137819Smarius "of register indices"); 253137819Smarius if (nregidx != -1 && (nreg = OF_getprop_alloc(parent, 254137819Smarius "reg", sizeof(*regs), (void **)®s)) >= nregidx) { 255137819Smarius for (i = 0; i < nregidx; i++) { 256137819Smarius start = ISA_REG_PHYS(®s[regidx[i]]); 257137819Smarius end = start + regs[regidx[i]].size - 1; 258137819Smarius rtype = ofw_isa_range_map(isab_ranges, 259137819Smarius isab_nrange, &start, &end, NULL); 260146407Smarius rid = 0; 261146407Smarius while (resource_list_find(rl, rtype, 262146407Smarius rid) != NULL) 263146407Smarius rid++; 264146407Smarius bus_set_resource(cdev, rtype, rid, 265146407Smarius start, end - start + 1); 266137819Smarius } 267137819Smarius } 268137819Smarius if (regidx != NULL) 269137819Smarius free(regidx, M_OFWPROP); 270137819Smarius } 271137819Smarius if (regs != NULL) 272137819Smarius free(regs, M_OFWPROP); 273137819Smarius 274137819Smarius nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs), 275137819Smarius (void **)&intrs); 276137819Smarius for (i = 0; i < nintr; i++) { 277137819Smarius if (intrs[i] > 7) 278137819Smarius panic("isa_setup_children: intr too large"); 279137819Smarius rintr = ofw_isa_route_intr(device_get_parent(dev), node, 280137819Smarius &isa_iinfo, intrs[i]); 281137819Smarius if (rintr == PCI_INVALID_IRQ) 282137819Smarius panic("isa_setup_children: could not map ISA " 283137819Smarius "interrupt %d", intrs[i]); 284137819Smarius isa_ino[intrs[i]] = rintr; 285137819Smarius bus_set_resource(cdev, SYS_RES_IRQ, i, rintr, 1); 286137819Smarius } 287137819Smarius if (intrs != NULL) 288137819Smarius free(intrs, M_OFWPROP); 289137819Smarius 290137819Smarius ndrq = OF_getprop_alloc(node, "dma-channel", sizeof(*drqs), 291137819Smarius (void **)&drqs); 292137819Smarius for (i = 0; i < ndrq; i++) 293137819Smarius bus_set_resource(cdev, SYS_RES_DRQ, i, drqs[i], 1); 294137819Smarius if (drqs != NULL) 295137819Smarius free(drqs, M_OFWPROP); 296137819Smarius 297137819Smarius /* 298137819Smarius * Devices using DMA hang off of the `dma' node instead of 299137819Smarius * directly from the ISA bridge node. 300137819Smarius */ 301137819Smarius if (strcmp(name, "dma") == 0) 302137819Smarius isa_setup_children(dev, node); 303137819Smarius 304137819Smarius free(name, M_OFWPROP); 30586232Stmm } 30686232Stmm} 30786232Stmm 30886232Stmmstruct resource * 30986232Stmmisa_alloc_resource(device_t bus, device_t child, int type, int *rid, 31086232Stmm u_long start, u_long end, u_long count, u_int flags) 31186232Stmm{ 31286232Stmm /* 313127146Sjmg * Consider adding a resource definition. 31486232Stmm */ 31586232Stmm int passthrough = (device_get_parent(child) != bus); 31686232Stmm int isdefault = (start == 0UL && end == ~0UL); 317146407Smarius struct resource_list *rl; 31886232Stmm struct resource_list_entry *rle; 31986232Stmm u_long base, limit; 32086232Stmm 321146407Smarius rl = BUS_GET_RESOURCE_LIST(bus, child); 32286232Stmm if (!passthrough && !isdefault) { 32386232Stmm rle = resource_list_find(rl, type, *rid); 32486232Stmm if (!rle) { 32586232Stmm if (*rid < 0) 326146407Smarius return (NULL); 32786232Stmm switch (type) { 32886232Stmm case SYS_RES_IRQ: 32986232Stmm if (*rid >= ISA_NIRQ) 330146407Smarius return (NULL); 33186232Stmm break; 33286232Stmm case SYS_RES_DRQ: 33386232Stmm if (*rid >= ISA_NDRQ) 334146407Smarius return (NULL); 33586232Stmm break; 33686232Stmm case SYS_RES_MEMORY: 33786232Stmm if (*rid >= ISA_NMEM) 338146407Smarius return (NULL); 33986232Stmm break; 34086232Stmm case SYS_RES_IOPORT: 34186232Stmm if (*rid >= ISA_NPORT) 342146407Smarius return (NULL); 34386232Stmm break; 34486232Stmm default: 345146407Smarius return (NULL); 34686232Stmm } 34786232Stmm resource_list_add(rl, type, *rid, start, end, count); 34886232Stmm } 34986232Stmm } 35086232Stmm 35186232Stmm /* 352137819Smarius * Sanity check if the resource in the respective entry is fully 353137819Smarius * mapped and specified and its type allocable. A driver could 354137819Smarius * have added an out of range resource on its own. 35586232Stmm */ 356137819Smarius if (!passthrough) { 357137819Smarius if ((rle = resource_list_find(rl, type, *rid)) == NULL) 35886232Stmm return (NULL); 359137819Smarius base = limit = 0; 36086232Stmm switch (type) { 36186232Stmm case SYS_RES_MEMORY: 362137819Smarius if (isa_mem_bt == NULL) 363137819Smarius return (NULL); 364137819Smarius base = isa_mem_base; 365137819Smarius limit = base + isa_mem_limit; 366137819Smarius break; 36786232Stmm case SYS_RES_IOPORT: 368137819Smarius if (isa_io_bt == NULL) 36993681Stmm return (NULL); 370137819Smarius base = isa_io_base; 371137819Smarius limit = base + isa_io_limit; 37286232Stmm break; 37386232Stmm case SYS_RES_IRQ: 374137819Smarius if (rle->start != rle->end || rle->start <= 7) 37586232Stmm return (NULL); 37686232Stmm break; 377137819Smarius case SYS_RES_DRQ: 378137819Smarius break; 379137819Smarius default: 380137819Smarius return (NULL); 38186232Stmm } 382137819Smarius if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 383137819Smarius if (!INRANGE(rle->start, base, limit) || 384137819Smarius !INRANGE(rle->end, base, limit)) 385137819Smarius return (NULL); 386137819Smarius } 38786232Stmm } 38886232Stmm 389137819Smarius return (resource_list_alloc(rl, bus, child, type, rid, start, end, 390137819Smarius count, flags)); 39186232Stmm} 39286232Stmm 39386232Stmmint 39486232Stmmisa_release_resource(device_t bus, device_t child, int type, int rid, 39586232Stmm struct resource *res) 39686232Stmm{ 39786232Stmm 398146407Smarius return (bus_generic_rl_release_resource(bus, child, type, rid, res)); 39986232Stmm} 40086232Stmm 40186232Stmmint 40286232Stmmisa_setup_intr(device_t dev, device_t child, 40386232Stmm struct resource *irq, int flags, 40486232Stmm driver_intr_t *intr, void *arg, void **cookiep) 40586232Stmm{ 40686232Stmm 40786232Stmm /* 408146407Smarius * Just pass through. This is going to be handled by either 409146407Smarius * one of the parent PCI buses or the nexus device. 410146407Smarius * The interrupt had been routed before it was added to the 411146407Smarius * resource list of the child. 41286232Stmm */ 41386232Stmm return (BUS_SETUP_INTR(device_get_parent(dev), child, irq, flags, intr, 41486232Stmm arg, cookiep)); 41586232Stmm} 41686232Stmm 41786232Stmmint 41886232Stmmisa_teardown_intr(device_t dev, device_t child, 41986232Stmm struct resource *irq, void *cookie) 42086232Stmm{ 42186232Stmm 42286232Stmm return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie)); 42386232Stmm} 424