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$"); 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. */ 57129051Smariusstatic u_int64_t isa_io_base; 58129051Smariusstatic u_int64_t isa_io_limit; 59129051Smariusstatic u_int64_t isa_mem_base; 60129051Smariusstatic u_int64_t isa_mem_limit; 6186232Stmm 6286232Stmmdevice_t isa_bus_device; 6386232Stmm 6486232Stmmstatic phandle_t isab_node; 65137819Smariusstatic struct isa_ranges *isab_ranges; 66137819Smariusstatic int isab_nrange; 67137819Smariusstatic struct ofw_bus_iinfo isa_iinfo; 68117119Stmm 6986232Stmm/* 70137819Smarius * XXX: This is really partly PCI-specific, but unfortunately is 7186232Stmm * differently enough to have to duplicate it here... 7286232Stmm */ 7386232Stmm#define ISAB_RANGE_PHYS(r) \ 7486232Stmm (((u_int64_t)(r)->phys_mid << 32) | (u_int64_t)(r)->phys_lo) 7586232Stmm#define ISAB_RANGE_SPACE(r) (((r)->phys_hi >> 24) & 0x03) 7686232Stmm 7786232Stmm#define ISAR_SPACE_IO 0x01 7886232Stmm#define ISAR_SPACE_MEM 0x02 7986232Stmm 8086232Stmm#define INRANGE(x, start, end) ((x) >= (start) && (x) <= (end)) 8186232Stmm 82137819Smariusstatic void isa_setup_children(device_t, phandle_t); 8386232Stmm 8486232Stmmvoid 8586232Stmmisa_init(device_t dev) 8686232Stmm{ 8786232Stmm device_t bridge; 88137819Smarius int i; 8986232Stmm 9086232Stmm /* The parent of the bus must be a PCI-ISA bridge. */ 9186232Stmm bridge = device_get_parent(dev); 92133589Smarius isab_node = ofw_bus_get_node(bridge); 93137819Smarius isab_nrange = OF_getprop_alloc(isab_node, "ranges", 94137819Smarius sizeof(*isab_ranges), (void **)&isab_ranges); 95137819Smarius if (isab_nrange <= 0) 9686232Stmm panic("isa_init: cannot get bridge range property"); 97117119Stmm 98117119Stmm ofw_bus_setup_iinfo(isab_node, &isa_iinfo, sizeof(ofw_isa_intr_t)); 99117119Stmm 100137819Smarius isa_setup_children(dev, isab_node); 101137819Smarius 102137819Smarius for (i = isab_nrange - 1; i >= 0; i--) { 103137819Smarius switch(ISAB_RANGE_SPACE(&isab_ranges[i])) { 10486232Stmm case ISAR_SPACE_IO: 10586232Stmm /* This is probably always 0. */ 106137819Smarius isa_io_base = ISAB_RANGE_PHYS(&isab_ranges[i]); 107137819Smarius isa_io_limit = isab_ranges[i].size; 10886232Stmm break; 10986232Stmm case ISAR_SPACE_MEM: 11086232Stmm /* This is probably always 0. */ 111137819Smarius isa_mem_base = ISAB_RANGE_PHYS(&isab_ranges[i]); 112137819Smarius isa_mem_limit = isab_ranges[i].size; 11386232Stmm break; 11486232Stmm } 11586232Stmm } 11686232Stmm} 11786232Stmm 118159289Smariusstatic const struct { 119207241Smarius const char *const name; 120137819Smarius uint32_t id; 121262613Sdim} ofw_isa_pnp_map[] = { 122137819Smarius { "SUNW,lomh", 0x0000ae4e }, /* SUN0000 */ 123137819Smarius { "dma", 0x0002d041 }, /* PNP0200 */ 124137819Smarius { "floppy", 0x0007d041 }, /* PNP0700 */ 125137819Smarius { "rtc", 0x000bd041 }, /* PNP0B00 */ 126137819Smarius { "flashprom", 0x0100ae4e }, /* SUN0001 */ 127137819Smarius { "parallel", 0x0104d041 }, /* PNP0401 */ 128137819Smarius { "serial", 0x0105d041 }, /* PNP0501 */ 129207241Smarius { "su", 0x0105d041 }, /* PNP0501 */ 130159289Smarius { "i2c", 0x0200ae4e }, /* SUN0002 */ 131159289Smarius { "rmc-comm", 0x0300ae4e }, /* SUN0003 */ 132137819Smarius { "kb_ps2", 0x0303d041 }, /* PNP0303 */ 133137819Smarius { "kdmouse", 0x030fd041 }, /* PNP0F03 */ 134190111Smarius { "bscbus", 0x0400ae4e }, /* SUN0004 */ 135137819Smarius { "power", 0x0c0cd041 }, /* PNP0C0C */ 136137819Smarius { NULL, 0x0 } 137137819Smarius}; 138137819Smarius 139137819Smariusstatic void 140137819Smariusisa_setup_children(device_t dev, phandle_t parent) 14186232Stmm{ 142137819Smarius struct isa_regs *regs; 143146407Smarius struct resource_list *rl; 144137819Smarius device_t cdev; 145137819Smarius u_int64_t end, start; 146137819Smarius ofw_isa_intr_t *intrs, rintr; 147137819Smarius phandle_t node; 148137819Smarius uint32_t *drqs, *regidx; 149146407Smarius int i, ndrq, nintr, nreg, nregidx, rid, rtype; 150137819Smarius char *name; 15186232Stmm 152137819Smarius /* 153137819Smarius * Loop through children and fake up PnP devices for them. 154137819Smarius * Their resources are added as fully mapped and specified because 155137819Smarius * adjusting the resources and the resource list entries respectively 156137819Smarius * in isa_alloc_resource() causes trouble with drivers which use 157137819Smarius * rman_get_start(), pass-through or allocate and release resources 158137819Smarius * multiple times, etc. Adjusting the resources might be better off 159137819Smarius * in a bus_activate_resource method but the common ISA code doesn't 160137819Smarius * allow for an isa_activate_resource(). 161137819Smarius */ 162137819Smarius for (node = OF_child(parent); node != 0; node = OF_peer(node)) { 163137819Smarius if ((OF_getprop_alloc(node, "name", 1, (void **)&name)) == -1) 164137819Smarius continue; 165137819Smarius 166137819Smarius /* 167137819Smarius * Keyboard and mouse controllers hang off of the `8042' 168137819Smarius * node but we have no real use for the `8042' itself. 169137819Smarius */ 170137819Smarius if (strcmp(name, "8042") == 0) { 171137819Smarius isa_setup_children(dev, node); 172300173Sgonzo OF_prop_free(name); 173137819Smarius continue; 174137819Smarius } 175137819Smarius 176146407Smarius for (i = 0; ofw_isa_pnp_map[i].name != NULL; i++) 177146407Smarius if (strcmp(ofw_isa_pnp_map[i].name, name) == 0) 178137819Smarius break; 179146407Smarius if (ofw_isa_pnp_map[i].name == NULL) { 180173121Smarius device_printf(dev, "no PnP map entry for node " 181137819Smarius "0x%lx: %s\n", (unsigned long)node, name); 182300173Sgonzo OF_prop_free(name); 183137819Smarius continue; 184137819Smarius } 185137819Smarius 186138506Simp if ((cdev = BUS_ADD_CHILD(dev, ISA_ORDER_PNPBIOS, NULL, -1)) == 187137819Smarius NULL) 188137819Smarius panic("isa_setup_children: BUS_ADD_CHILD failed"); 189146407Smarius isa_set_logicalid(cdev, ofw_isa_pnp_map[i].id); 190146407Smarius isa_set_vendorid(cdev, ofw_isa_pnp_map[i].id); 191137819Smarius 192146407Smarius rl = BUS_GET_RESOURCE_LIST(dev, cdev); 193137819Smarius nreg = OF_getprop_alloc(node, "reg", sizeof(*regs), 194137819Smarius (void **)®s); 195137819Smarius for (i = 0; i < nreg; i++) { 196137819Smarius start = ISA_REG_PHYS(®s[i]); 197137819Smarius end = start + regs[i].size - 1; 198137819Smarius rtype = ofw_isa_range_map(isab_ranges, isab_nrange, 199137819Smarius &start, &end, NULL); 200146407Smarius rid = 0; 201146407Smarius while (resource_list_find(rl, rtype, rid) != NULL) 202146407Smarius rid++; 203146407Smarius bus_set_resource(cdev, rtype, rid, start, 204137819Smarius end - start + 1); 205137819Smarius } 206137819Smarius if (nreg == -1 && parent != isab_node) { 207137819Smarius /* 208137819Smarius * The "reg" property still might be an index into 209137819Smarius * the set of registers of the parent device like 210137819Smarius * with the nodes hanging off of the `8042' node. 211137819Smarius */ 212137819Smarius nregidx = OF_getprop_alloc(node, "reg", sizeof(*regidx), 213137819Smarius (void **)®idx); 214137819Smarius if (nregidx > 2) 215137819Smarius panic("isa_setup_children: impossible number " 216137819Smarius "of register indices"); 217137819Smarius if (nregidx != -1 && (nreg = OF_getprop_alloc(parent, 218137819Smarius "reg", sizeof(*regs), (void **)®s)) >= nregidx) { 219137819Smarius for (i = 0; i < nregidx; i++) { 220137819Smarius start = ISA_REG_PHYS(®s[regidx[i]]); 221137819Smarius end = start + regs[regidx[i]].size - 1; 222137819Smarius rtype = ofw_isa_range_map(isab_ranges, 223137819Smarius isab_nrange, &start, &end, NULL); 224146407Smarius rid = 0; 225146407Smarius while (resource_list_find(rl, rtype, 226146407Smarius rid) != NULL) 227146407Smarius rid++; 228146407Smarius bus_set_resource(cdev, rtype, rid, 229146407Smarius start, end - start + 1); 230137819Smarius } 231137819Smarius } 232137819Smarius if (regidx != NULL) 233300173Sgonzo OF_prop_free(regidx); 234137819Smarius } 235137819Smarius if (regs != NULL) 236300173Sgonzo OF_prop_free(regs); 237137819Smarius 238137819Smarius nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs), 239137819Smarius (void **)&intrs); 240137819Smarius for (i = 0; i < nintr; i++) { 241137819Smarius if (intrs[i] > 7) 242137819Smarius panic("isa_setup_children: intr too large"); 243137819Smarius rintr = ofw_isa_route_intr(device_get_parent(dev), node, 244137819Smarius &isa_iinfo, intrs[i]); 245173121Smarius if (rintr == PCI_INVALID_IRQ) { 246173121Smarius device_printf(dev, "could not map ISA " 247173121Smarius "interrupt %d for node 0x%lx: %s\n", 248173121Smarius intrs[i], (unsigned long)node, name); 249173121Smarius continue; 250173121Smarius } 251137819Smarius bus_set_resource(cdev, SYS_RES_IRQ, i, rintr, 1); 252137819Smarius } 253137819Smarius if (intrs != NULL) 254300173Sgonzo OF_prop_free(intrs); 255137819Smarius 256137819Smarius ndrq = OF_getprop_alloc(node, "dma-channel", sizeof(*drqs), 257137819Smarius (void **)&drqs); 258137819Smarius for (i = 0; i < ndrq; i++) 259137819Smarius bus_set_resource(cdev, SYS_RES_DRQ, i, drqs[i], 1); 260137819Smarius if (drqs != NULL) 261300173Sgonzo OF_prop_free(drqs); 262137819Smarius 263137819Smarius /* 264137819Smarius * Devices using DMA hang off of the `dma' node instead of 265137819Smarius * directly from the ISA bridge node. 266137819Smarius */ 267137819Smarius if (strcmp(name, "dma") == 0) 268137819Smarius isa_setup_children(dev, node); 269137819Smarius 270300173Sgonzo OF_prop_free(name); 27186232Stmm } 27286232Stmm} 27386232Stmm 27486232Stmmstruct resource * 27586232Stmmisa_alloc_resource(device_t bus, device_t child, int type, int *rid, 276294883Sjhibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 27786232Stmm{ 27886232Stmm /* 279127146Sjmg * Consider adding a resource definition. 28086232Stmm */ 28186232Stmm int passthrough = (device_get_parent(child) != bus); 282295832Sjhibbits int isdefault = RMAN_IS_DEFAULT_RANGE(start, end); 283146407Smarius struct resource_list *rl; 28486232Stmm struct resource_list_entry *rle; 28586232Stmm u_long base, limit; 28686232Stmm 287146407Smarius rl = BUS_GET_RESOURCE_LIST(bus, child); 28886232Stmm if (!passthrough && !isdefault) { 28986232Stmm rle = resource_list_find(rl, type, *rid); 29086232Stmm if (!rle) { 29186232Stmm if (*rid < 0) 292146407Smarius return (NULL); 29386232Stmm switch (type) { 29486232Stmm case SYS_RES_IRQ: 29586232Stmm if (*rid >= ISA_NIRQ) 296146407Smarius return (NULL); 29786232Stmm break; 29886232Stmm case SYS_RES_DRQ: 29986232Stmm if (*rid >= ISA_NDRQ) 300146407Smarius return (NULL); 30186232Stmm break; 30286232Stmm case SYS_RES_MEMORY: 30386232Stmm if (*rid >= ISA_NMEM) 304146407Smarius return (NULL); 30586232Stmm break; 30686232Stmm case SYS_RES_IOPORT: 30786232Stmm if (*rid >= ISA_NPORT) 308146407Smarius return (NULL); 30986232Stmm break; 31086232Stmm default: 311146407Smarius return (NULL); 31286232Stmm } 31386232Stmm resource_list_add(rl, type, *rid, start, end, count); 31486232Stmm } 31586232Stmm } 31686232Stmm 31786232Stmm /* 318137819Smarius * Sanity check if the resource in the respective entry is fully 319137819Smarius * mapped and specified and its type allocable. A driver could 320137819Smarius * have added an out of range resource on its own. 32186232Stmm */ 322137819Smarius if (!passthrough) { 323137819Smarius if ((rle = resource_list_find(rl, type, *rid)) == NULL) 32486232Stmm return (NULL); 325137819Smarius base = limit = 0; 32686232Stmm switch (type) { 32786232Stmm case SYS_RES_MEMORY: 328137819Smarius base = isa_mem_base; 329137819Smarius limit = base + isa_mem_limit; 330137819Smarius break; 33186232Stmm case SYS_RES_IOPORT: 332137819Smarius base = isa_io_base; 333137819Smarius limit = base + isa_io_limit; 33486232Stmm break; 33586232Stmm case SYS_RES_IRQ: 336137819Smarius if (rle->start != rle->end || rle->start <= 7) 33786232Stmm return (NULL); 33886232Stmm break; 339137819Smarius case SYS_RES_DRQ: 340137819Smarius break; 341137819Smarius default: 342137819Smarius return (NULL); 34386232Stmm } 344137819Smarius if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 345137819Smarius if (!INRANGE(rle->start, base, limit) || 346137819Smarius !INRANGE(rle->end, base, limit)) 347137819Smarius return (NULL); 348137819Smarius } 34986232Stmm } 35086232Stmm 351137819Smarius return (resource_list_alloc(rl, bus, child, type, rid, start, end, 352137819Smarius count, flags)); 35386232Stmm} 35486232Stmm 35586232Stmmint 35686232Stmmisa_release_resource(device_t bus, device_t child, int type, int rid, 357173121Smarius struct resource *res) 35886232Stmm{ 35986232Stmm 360146407Smarius return (bus_generic_rl_release_resource(bus, child, type, rid, res)); 36186232Stmm} 362