isa.c revision 256281
168349Sobrien/*- 268349Sobrien * Copyright (c) 1998 Doug Rabson 368349Sobrien * Copyright (c) 2001 Thomas Moestl <tmm@FreeBSD.org> 468349Sobrien * All rights reserved. 568349Sobrien * 668349Sobrien * Redistribution and use in source and binary forms, with or without 768349Sobrien * modification, are permitted provided that the following conditions 868349Sobrien * are met: 968349Sobrien * 1. Redistributions of source code must retain the above copyright 1068349Sobrien * notice, this list of conditions and the following disclaimer. 1168349Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1268349Sobrien * notice, this list of conditions and the following disclaimer in the 1368349Sobrien * documentation and/or other materials provided with the distribution. 1468349Sobrien * 1568349Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1668349Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1768349Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1868349Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1968349Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2068349Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2168349Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2268349Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2368349Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2468349Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2568349Sobrien * SUCH DAMAGE. 2668349Sobrien * 2768349Sobrien * from: FreeBSD: src/sys/alpha/isa/isa.c,v 1.26 2001/07/11 2868349Sobrien */ 2968349Sobrien 3068349Sobrien#include <sys/cdefs.h> 3168349Sobrien__FBSDID("$FreeBSD: stable/10/sys/sparc64/isa/isa.c 221526 2011-05-06 13:48:53Z jhb $"); 3268349Sobrien 3368349Sobrien#include <sys/param.h> 3468349Sobrien#include <sys/systm.h> 3568349Sobrien#include <sys/bus.h> 3668349Sobrien 3768349Sobrien#include <machine/bus.h> 3868349Sobrien 3968349Sobrien#include <sys/rman.h> 4068349Sobrien 4168349Sobrien#include <isa/isareg.h> 4268349Sobrien#include <isa/isavar.h> 4368349Sobrien#include <isa/isa_common.h> 4468349Sobrien 4568349Sobrien#include <dev/ofw/ofw_bus.h> 4668349Sobrien#include <dev/ofw/openfirm.h> 4768349Sobrien 4868349Sobrien#include <machine/resource.h> 4968349Sobrien 5068349Sobrien#include <dev/pci/pcireg.h> 5168349Sobrien#include <dev/pci/pcivar.h> 5268349Sobrien 5368349Sobrien#include <sparc64/pci/ofw_pci.h> 5468349Sobrien#include <sparc64/isa/ofw_isa.h> 5568349Sobrien 5668349Sobrien/* There can be only one ISA bus, so it is safe to use globals. */ 5768349Sobrienstatic u_int64_t isa_io_base; 5868349Sobrienstatic u_int64_t isa_io_limit; 5968349Sobrienstatic u_int64_t isa_mem_base; 6068349Sobrienstatic u_int64_t isa_mem_limit; 6168349Sobrien 6268349Sobriendevice_t isa_bus_device; 6368349Sobrien 6468349Sobrienstatic phandle_t isab_node; 6568349Sobrienstatic struct isa_ranges *isab_ranges; 6668349Sobrienstatic int isab_nrange; 6768349Sobrienstatic struct ofw_bus_iinfo isa_iinfo; 6868349Sobrien 6968349Sobrien/* 7068349Sobrien * XXX: This is really partly PCI-specific, but unfortunately is 7168349Sobrien * differently enough to have to duplicate it here... 7268349Sobrien */ 7368349Sobrien#define ISAB_RANGE_PHYS(r) \ 7468349Sobrien (((u_int64_t)(r)->phys_mid << 32) | (u_int64_t)(r)->phys_lo) 7568349Sobrien#define ISAB_RANGE_SPACE(r) (((r)->phys_hi >> 24) & 0x03) 7668349Sobrien 7768349Sobrien#define ISAR_SPACE_IO 0x01 7868349Sobrien#define ISAR_SPACE_MEM 0x02 7968349Sobrien 8068349Sobrien#define INRANGE(x, start, end) ((x) >= (start) && (x) <= (end)) 8168349Sobrien 8268349Sobrienstatic void isa_setup_children(device_t, phandle_t); 8368349Sobrien 8468349Sobrienvoid 8568349Sobrienisa_init(device_t dev) 8668349Sobrien{ 8768349Sobrien device_t bridge; 8868349Sobrien int i; 8968349Sobrien 9068349Sobrien /* The parent of the bus must be a PCI-ISA bridge. */ 9168349Sobrien bridge = device_get_parent(dev); 9268349Sobrien isab_node = ofw_bus_get_node(bridge); 9368349Sobrien isab_nrange = OF_getprop_alloc(isab_node, "ranges", 9468349Sobrien sizeof(*isab_ranges), (void **)&isab_ranges); 9568349Sobrien if (isab_nrange <= 0) 9668349Sobrien panic("isa_init: cannot get bridge range property"); 9768349Sobrien 9868349Sobrien ofw_bus_setup_iinfo(isab_node, &isa_iinfo, sizeof(ofw_isa_intr_t)); 9968349Sobrien 10068349Sobrien isa_setup_children(dev, isab_node); 10168349Sobrien 10268349Sobrien for (i = isab_nrange - 1; i >= 0; i--) { 10368349Sobrien switch(ISAB_RANGE_SPACE(&isab_ranges[i])) { 10468349Sobrien case ISAR_SPACE_IO: 10568349Sobrien /* This is probably always 0. */ 10668349Sobrien isa_io_base = ISAB_RANGE_PHYS(&isab_ranges[i]); 10768349Sobrien isa_io_limit = isab_ranges[i].size; 10868349Sobrien break; 10968349Sobrien case ISAR_SPACE_MEM: 11068349Sobrien /* This is probably always 0. */ 11168349Sobrien isa_mem_base = ISAB_RANGE_PHYS(&isab_ranges[i]); 11268349Sobrien isa_mem_limit = isab_ranges[i].size; 11368349Sobrien break; 11468349Sobrien } 11568349Sobrien } 11668349Sobrien} 11768349Sobrien 11868349Sobrienstatic const struct { 11968349Sobrien const char *const name; 120186690Sobrien uint32_t id; 121186690Sobrien} const ofw_isa_pnp_map[] = { 122186690Sobrien { "SUNW,lomh", 0x0000ae4e }, /* SUN0000 */ 123186690Sobrien { "dma", 0x0002d041 }, /* PNP0200 */ 124186690Sobrien { "floppy", 0x0007d041 }, /* PNP0700 */ 125186690Sobrien { "rtc", 0x000bd041 }, /* PNP0B00 */ 12668349Sobrien { "flashprom", 0x0100ae4e }, /* SUN0001 */ 12768349Sobrien { "parallel", 0x0104d041 }, /* PNP0401 */ 128133359Sobrien { "serial", 0x0105d041 }, /* PNP0501 */ 129133359Sobrien { "su", 0x0105d041 }, /* PNP0501 */ 130133359Sobrien { "i2c", 0x0200ae4e }, /* SUN0002 */ 131133359Sobrien { "rmc-comm", 0x0300ae4e }, /* SUN0003 */ 132133359Sobrien { "kb_ps2", 0x0303d041 }, /* PNP0303 */ 133133359Sobrien { "kdmouse", 0x030fd041 }, /* PNP0F03 */ 134133359Sobrien { "bscbus", 0x0400ae4e }, /* SUN0004 */ 135133359Sobrien { "power", 0x0c0cd041 }, /* PNP0C0C */ 136133359Sobrien { NULL, 0x0 } 137133359Sobrien}; 138133359Sobrien 139133359Sobrienstatic void 140133359Sobrienisa_setup_children(device_t dev, phandle_t parent) 141133359Sobrien{ 142133359Sobrien struct isa_regs *regs; 143133359Sobrien struct resource_list *rl; 144133359Sobrien device_t cdev; 145133359Sobrien u_int64_t end, start; 146133359Sobrien ofw_isa_intr_t *intrs, rintr; 147133359Sobrien phandle_t node; 148133359Sobrien uint32_t *drqs, *regidx; 149133359Sobrien int i, ndrq, nintr, nreg, nregidx, rid, rtype; 150133359Sobrien char *name; 151133359Sobrien 152133359Sobrien /* 153133359Sobrien * Loop through children and fake up PnP devices for them. 154133359Sobrien * Their resources are added as fully mapped and specified because 155133359Sobrien * adjusting the resources and the resource list entries respectively 156133359Sobrien * in isa_alloc_resource() causes trouble with drivers which use 157133359Sobrien * rman_get_start(), pass-through or allocate and release resources 158133359Sobrien * multiple times, etc. Adjusting the resources might be better off 159133359Sobrien * in a bus_activate_resource method but the common ISA code doesn't 160133359Sobrien * allow for an isa_activate_resource(). 161133359Sobrien */ 162139368Sobrien for (node = OF_child(parent); node != 0; node = OF_peer(node)) { 163139368Sobrien if ((OF_getprop_alloc(node, "name", 1, (void **)&name)) == -1) 164139368Sobrien continue; 165139368Sobrien 166139368Sobrien /* 167139368Sobrien * Keyboard and mouse controllers hang off of the `8042' 168139368Sobrien * node but we have no real use for the `8042' itself. 169139368Sobrien */ 170159764Sobrien if (strcmp(name, "8042") == 0) { 171159764Sobrien isa_setup_children(dev, node); 172159764Sobrien free(name, M_OFWPROP); 173139368Sobrien continue; 174169962Sobrien } 175169962Sobrien 176169962Sobrien for (i = 0; ofw_isa_pnp_map[i].name != NULL; i++) 177169962Sobrien if (strcmp(ofw_isa_pnp_map[i].name, name) == 0) 178169962Sobrien break; 179169962Sobrien if (ofw_isa_pnp_map[i].name == NULL) { 180169962Sobrien device_printf(dev, "no PnP map entry for node " 181169962Sobrien "0x%lx: %s\n", (unsigned long)node, name); 182169962Sobrien free(name, M_OFWPROP); 183169962Sobrien continue; 184169962Sobrien } 185169962Sobrien 186169962Sobrien if ((cdev = BUS_ADD_CHILD(dev, ISA_ORDER_PNPBIOS, NULL, -1)) == 187169962Sobrien NULL) 188169962Sobrien panic("isa_setup_children: BUS_ADD_CHILD failed"); 189169962Sobrien isa_set_logicalid(cdev, ofw_isa_pnp_map[i].id); 190169962Sobrien isa_set_vendorid(cdev, ofw_isa_pnp_map[i].id); 191169962Sobrien 192169962Sobrien rl = BUS_GET_RESOURCE_LIST(dev, cdev); 193169962Sobrien nreg = OF_getprop_alloc(node, "reg", sizeof(*regs), 194169962Sobrien (void **)®s); 195169962Sobrien for (i = 0; i < nreg; i++) { 196169962Sobrien start = ISA_REG_PHYS(®s[i]); 197169962Sobrien end = start + regs[i].size - 1; 198169962Sobrien rtype = ofw_isa_range_map(isab_ranges, isab_nrange, 199169962Sobrien &start, &end, NULL); 200175296Sobrien rid = 0; 201175296Sobrien while (resource_list_find(rl, rtype, rid) != NULL) 202175296Sobrien rid++; 203175296Sobrien bus_set_resource(cdev, rtype, rid, start, 204175296Sobrien end - start + 1); 205175296Sobrien } 206175296Sobrien if (nreg == -1 && parent != isab_node) { 207175296Sobrien /* 208175296Sobrien * The "reg" property still might be an index into 209175296Sobrien * the set of registers of the parent device like 210175296Sobrien * with the nodes hanging off of the `8042' node. 211175296Sobrien */ 212175296Sobrien nregidx = OF_getprop_alloc(node, "reg", sizeof(*regidx), 213175296Sobrien (void **)®idx); 214175296Sobrien if (nregidx > 2) 215175296Sobrien panic("isa_setup_children: impossible number " 216175296Sobrien "of register indices"); 217175296Sobrien if (nregidx != -1 && (nreg = OF_getprop_alloc(parent, 218175296Sobrien "reg", sizeof(*regs), (void **)®s)) >= nregidx) { 219175296Sobrien for (i = 0; i < nregidx; i++) { 220175296Sobrien start = ISA_REG_PHYS(®s[regidx[i]]); 221175296Sobrien end = start + regs[regidx[i]].size - 1; 222175296Sobrien rtype = ofw_isa_range_map(isab_ranges, 223175296Sobrien isab_nrange, &start, &end, NULL); 224175296Sobrien rid = 0; 225175296Sobrien while (resource_list_find(rl, rtype, 226175296Sobrien rid) != NULL) 227175296Sobrien rid++; 228175296Sobrien bus_set_resource(cdev, rtype, rid, 229175296Sobrien start, end - start + 1); 230175296Sobrien } 231175296Sobrien } 232175296Sobrien if (regidx != NULL) 233175296Sobrien free(regidx, M_OFWPROP); 234175296Sobrien } 235175296Sobrien if (regs != NULL) 236175296Sobrien free(regs, M_OFWPROP); 237175296Sobrien 238175296Sobrien nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs), 239175296Sobrien (void **)&intrs); 240175296Sobrien for (i = 0; i < nintr; i++) { 241175296Sobrien if (intrs[i] > 7) 242175296Sobrien panic("isa_setup_children: intr too large"); 243175296Sobrien rintr = ofw_isa_route_intr(device_get_parent(dev), node, 244175296Sobrien &isa_iinfo, intrs[i]); 245175296Sobrien if (rintr == PCI_INVALID_IRQ) { 246175296Sobrien device_printf(dev, "could not map ISA " 247175296Sobrien "interrupt %d for node 0x%lx: %s\n", 248175296Sobrien intrs[i], (unsigned long)node, name); 249175296Sobrien continue; 250175296Sobrien } 251175296Sobrien bus_set_resource(cdev, SYS_RES_IRQ, i, rintr, 1); 252175296Sobrien } 253175296Sobrien if (intrs != NULL) 254175296Sobrien free(intrs, M_OFWPROP); 255 256 ndrq = OF_getprop_alloc(node, "dma-channel", sizeof(*drqs), 257 (void **)&drqs); 258 for (i = 0; i < ndrq; i++) 259 bus_set_resource(cdev, SYS_RES_DRQ, i, drqs[i], 1); 260 if (drqs != NULL) 261 free(drqs, M_OFWPROP); 262 263 /* 264 * Devices using DMA hang off of the `dma' node instead of 265 * directly from the ISA bridge node. 266 */ 267 if (strcmp(name, "dma") == 0) 268 isa_setup_children(dev, node); 269 270 free(name, M_OFWPROP); 271 } 272} 273 274struct resource * 275isa_alloc_resource(device_t bus, device_t child, int type, int *rid, 276 u_long start, u_long end, u_long count, u_int flags) 277{ 278 /* 279 * Consider adding a resource definition. 280 */ 281 int passthrough = (device_get_parent(child) != bus); 282 int isdefault = (start == 0UL && end == ~0UL); 283 struct resource_list *rl; 284 struct resource_list_entry *rle; 285 u_long base, limit; 286 287 rl = BUS_GET_RESOURCE_LIST(bus, child); 288 if (!passthrough && !isdefault) { 289 rle = resource_list_find(rl, type, *rid); 290 if (!rle) { 291 if (*rid < 0) 292 return (NULL); 293 switch (type) { 294 case SYS_RES_IRQ: 295 if (*rid >= ISA_NIRQ) 296 return (NULL); 297 break; 298 case SYS_RES_DRQ: 299 if (*rid >= ISA_NDRQ) 300 return (NULL); 301 break; 302 case SYS_RES_MEMORY: 303 if (*rid >= ISA_NMEM) 304 return (NULL); 305 break; 306 case SYS_RES_IOPORT: 307 if (*rid >= ISA_NPORT) 308 return (NULL); 309 break; 310 default: 311 return (NULL); 312 } 313 resource_list_add(rl, type, *rid, start, end, count); 314 } 315 } 316 317 /* 318 * Sanity check if the resource in the respective entry is fully 319 * mapped and specified and its type allocable. A driver could 320 * have added an out of range resource on its own. 321 */ 322 if (!passthrough) { 323 if ((rle = resource_list_find(rl, type, *rid)) == NULL) 324 return (NULL); 325 base = limit = 0; 326 switch (type) { 327 case SYS_RES_MEMORY: 328 base = isa_mem_base; 329 limit = base + isa_mem_limit; 330 break; 331 case SYS_RES_IOPORT: 332 base = isa_io_base; 333 limit = base + isa_io_limit; 334 break; 335 case SYS_RES_IRQ: 336 if (rle->start != rle->end || rle->start <= 7) 337 return (NULL); 338 break; 339 case SYS_RES_DRQ: 340 break; 341 default: 342 return (NULL); 343 } 344 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 345 if (!INRANGE(rle->start, base, limit) || 346 !INRANGE(rle->end, base, limit)) 347 return (NULL); 348 } 349 } 350 351 return (resource_list_alloc(rl, bus, child, type, rid, start, end, 352 count, flags)); 353} 354 355int 356isa_release_resource(device_t bus, device_t child, int type, int rid, 357 struct resource *res) 358{ 359 360 return (bus_generic_rl_release_resource(bus, child, type, rid, res)); 361} 362