isa.c revision 93681
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 * $FreeBSD: head/sys/sparc64/isa/isa.c 93681 2002-04-02 17:23:45Z tmm $ 30 */ 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/kernel.h> 35#include <sys/module.h> 36#include <sys/bus.h> 37#include <machine/bus.h> 38#include <sys/malloc.h> 39#include <sys/proc.h> 40#include <sys/rman.h> 41#include <sys/interrupt.h> 42 43#include <isa/isareg.h> 44#include <isa/isavar.h> 45#include <isa/isa_common.h> 46 47#include <pci/pcireg.h> 48#include <pci/pcivar.h> 49 50#include <ofw/ofw_pci.h> 51#include <ofw/openfirm.h> 52 53#include <machine/intr_machdep.h> 54#include <machine/ofw_bus.h> 55#include <machine/resource.h> 56 57#include <sparc64/pci/ofw_pci.h> 58#include <sparc64/isa/ofw_isa.h> 59 60#include "sparcbus_if.h" 61 62/* There can be only one ISA bus, so it is safe to use globals. */ 63bus_space_tag_t isa_io_bt = NULL; 64bus_space_handle_t isa_io_hdl; 65bus_space_tag_t isa_mem_bt = NULL; 66bus_space_handle_t isa_mem_hdl; 67 68u_int64_t isa_io_base; 69u_int64_t isa_io_limit; 70u_int64_t isa_mem_base; 71u_int64_t isa_mem_limit; 72 73device_t isa_bus_device; 74 75static phandle_t isab_node; 76static u_int32_t isa_ino[8]; 77 78/* 79 * XXX: This is really partly partly PCI-specific, but unfortunately is 80 * differently enough to have to duplicate it here... 81 */ 82#define ISAB_RANGE_PHYS(r) \ 83 (((u_int64_t)(r)->phys_mid << 32) | (u_int64_t)(r)->phys_lo) 84#define ISAB_RANGE_SPACE(r) (((r)->phys_hi >> 24) & 0x03) 85 86#define ISAR_SPACE_IO 0x01 87#define ISAR_SPACE_MEM 0x02 88 89#define INRANGE(x, start, end) ((x) >= (start) && (x) <= (end)) 90 91static int isa_route_intr_res(device_t, u_long, u_long); 92 93intrmask_t 94isa_irq_pending(void) 95{ 96 intrmask_t pending; 97 int i; 98 99 /* XXX: Is this correct? */ 100 for (i = 7, pending = 0; i >= 0; i--) { 101 pending <<= 1; 102 if (isa_ino[i] != ORIR_NOTFOUND) { 103 pending |= (SPARCBUS_INTR_PENDING(isa_bus_device, 104 isa_ino[i]) == 0) ? 0 : 1; 105 } 106 } 107 return (pending); 108} 109 110void 111isa_init(device_t dev) 112{ 113 device_t bridge; 114 phandle_t node; 115 u_int32_t ino; 116 struct isa_ranges *br; 117 int nbr, i; 118 119 /* The parent of the bus must be a PCI-ISA bridge. */ 120 bridge = device_get_parent(dev); 121 isab_node = ofw_pci_node(bridge); 122 nbr = OF_getprop_alloc(isab_node, "ranges", sizeof(*br), (void **)&br); 123 if (nbr <= 0) 124 panic("isa_init: cannot get bridge range property"); 125 /* 126 * This is really a bad kluge; however, it is needed to provide 127 * isa_irq_pending(). 128 */ 129 for (i = 0; i < 8; i++) 130 isa_ino[i] = ORIR_NOTFOUND; 131 for (node = OF_child(isab_node); node != 0; node = OF_peer(node)) { 132 if (OF_getprop(node, "interrupts", &ino, sizeof(ino)) == -1) 133 continue; 134 if (ino > 7) 135 panic("isa_init: XXX: ino too large"); 136 isa_ino[ino] = ofw_bus_route_intr(node, ino); 137 } 138 139 for (nbr -= 1; nbr >= 0; nbr--) { 140 switch(ISAB_RANGE_SPACE(br + nbr)) { 141 case ISAR_SPACE_IO: 142 /* This is probably always 0. */ 143 isa_io_base = ISAB_RANGE_PHYS(&br[nbr]); 144 isa_io_limit = br[nbr].size; 145 isa_io_hdl = SPARCBUS_GET_BUS_HANDLE(bridge, SBBT_IO, 146 isa_io_base, &isa_io_bt); 147 break; 148 case ISAR_SPACE_MEM: 149 /* This is probably always 0. */ 150 isa_mem_base = ISAB_RANGE_PHYS(&br[nbr]); 151 isa_mem_limit = br[nbr].size; 152 isa_mem_hdl = SPARCBUS_GET_BUS_HANDLE(bridge, SBBT_MEM, 153 isa_mem_base, &isa_mem_bt); 154 break; 155 } 156 } 157 free(br, M_OFWPROP); 158} 159 160static int 161isa_route_intr_res(device_t bus, u_long start, u_long end) 162{ 163 int res; 164 165 if (start != end) { 166 panic("isa_route_intr_res: allocation of interrupt range not " 167 "supported (0x%lx - 0x%lx)", start, end); 168 } 169 if (start > 7) 170 panic("isa_route_intr_res: start out of isa range"); 171 res = isa_ino[start]; 172 if (res == 255) 173 device_printf(bus, "could not map interrupt %d\n", res); 174 return (res); 175} 176 177struct resource * 178isa_alloc_resource(device_t bus, device_t child, int type, int *rid, 179 u_long start, u_long end, u_long count, u_int flags) 180{ 181 /* 182 * Consider adding a resource definition. We allow rid 0-1 for 183 * irq and drq, 0-3 for memory and 0-7 for ports which is 184 * sufficient for isapnp. 185 */ 186 int passthrough = (device_get_parent(child) != bus); 187 int isdefault = (start == 0UL && end == ~0UL); 188 struct isa_device* idev = DEVTOISA(child); 189 struct resource_list *rl = &idev->id_resources; 190 struct resource_list_entry *rle; 191 u_long base, limit; 192 193 if (!passthrough && !isdefault) { 194 rle = resource_list_find(rl, type, *rid); 195 if (!rle) { 196 if (*rid < 0) 197 return 0; 198 switch (type) { 199 case SYS_RES_IRQ: 200 if (*rid >= ISA_NIRQ) 201 return 0; 202 break; 203 case SYS_RES_DRQ: 204 if (*rid >= ISA_NDRQ) 205 return 0; 206 break; 207 case SYS_RES_MEMORY: 208 if (*rid >= ISA_NMEM) 209 return 0; 210 break; 211 case SYS_RES_IOPORT: 212 if (*rid >= ISA_NPORT) 213 return 0; 214 break; 215 default: 216 return 0; 217 } 218 resource_list_add(rl, type, *rid, start, end, count); 219 } 220 } 221 222 /* 223 * Add the base, change default allocations to be between base and 224 * limit, and reject allocations if a resource type is not enabled. 225 */ 226 base = limit = 0; 227 switch(type) { 228 case SYS_RES_MEMORY: 229 if (isa_mem_bt == NULL) 230 return (NULL); 231 base = isa_mem_base; 232 limit = base + isa_mem_limit; 233 break; 234 case SYS_RES_IOPORT: 235 if (isa_io_bt == NULL) 236 return (NULL); 237 base = isa_io_base; 238 limit = base + isa_io_limit; 239 break; 240 case SYS_RES_IRQ: 241 if (isdefault && passthrough) 242 panic("isa_alloc_resource: cannot pass through default " 243 "irq allocation"); 244 if (!isdefault) { 245 start = end = isa_route_intr_res(bus, start, end); 246 if (start == 255) 247 return (NULL); 248 } 249 break; 250 default: 251 panic("isa_alloc_resource: unsupported resource type %d", type); 252 } 253 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 254 start = ulmin(start + base, limit); 255 end = ulmin(end + base, limit); 256 } 257 258 /* 259 * This inlines a modified resource_list_alloc(); this is needed 260 * because the resources need to have offsets added to them, which 261 * cannot be done beforehand without patching the resource list entries 262 * (which is ugly). 263 */ 264 if (passthrough) { 265 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 266 type, rid, start, end, count, flags)); 267 } 268 269 rle = resource_list_find(rl, type, *rid); 270 if (rle == NULL) 271 return (NULL); /* no resource of that type/rid */ 272 273 if (rle->res != NULL) 274 panic("isa_alloc_resource: resource entry is busy"); 275 276 if (isdefault) { 277 start = rle->start; 278 count = ulmax(count, rle->count); 279 end = ulmax(rle->end, start + count - 1); 280 switch (type) { 281 case SYS_RES_MEMORY: 282 case SYS_RES_IOPORT: 283 start += base; 284 end += base; 285 if (!INRANGE(start, base, limit) || 286 !INRANGE(end, base, limit)) 287 return (NULL); 288 break; 289 case SYS_RES_IRQ: 290 start = end = isa_route_intr_res(bus, start, end); 291 if (start == 255) 292 return (NULL); 293 break; 294 } 295 } 296 297 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 298 type, rid, start, end, count, flags); 299 300 /* 301 * Record the new range. 302 */ 303 if (rle->res != NULL) { 304 rle->start = rman_get_start(rle->res) - base; 305 rle->end = rman_get_end(rle->res) - base; 306 rle->count = count; 307 } 308 309 return (rle->res); 310} 311 312int 313isa_release_resource(device_t bus, device_t child, int type, int rid, 314 struct resource *res) 315{ 316 struct isa_device* idev = DEVTOISA(child); 317 struct resource_list *rl = &idev->id_resources; 318 319 return (resource_list_release(rl, bus, child, type, rid, res)); 320} 321 322int 323isa_setup_intr(device_t dev, device_t child, 324 struct resource *irq, int flags, 325 driver_intr_t *intr, void *arg, void **cookiep) 326{ 327 328 /* 329 * Just pass through. This is going to be handled by either one of 330 * the parent PCI buses or the nexus device. 331 * The interrupt was routed at allocation time. 332 */ 333 return (BUS_SETUP_INTR(device_get_parent(dev), child, irq, flags, intr, 334 arg, cookiep)); 335} 336 337int 338isa_teardown_intr(device_t dev, device_t child, 339 struct resource *irq, void *cookie) 340{ 341 342 return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie)); 343} 344