ofw_isa.c revision 119291
1327952Sdim/* 2292915Sdim * Copyright (c) 1999, 2000 Matthew R. Green 3353358Sdim * Copyright (c) 2001, 2003 Thomas Moestl <tmm@FreeBSD.org> 4353358Sdim * All rights reserved. 5353358Sdim * 6292915Sdim * Redistribution and use in source and binary forms, with or without 7292915Sdim * modification, are permitted provided that the following conditions 8292915Sdim * are met: 9292915Sdim * 1. Redistributions of source code must retain the above copyright 10292915Sdim * notice, this list of conditions and the following disclaimer. 11327952Sdim * 2. Redistributions in binary form must reproduce the above copyright 12327952Sdim * notice, this list of conditions and the following disclaimer in the 13292915Sdim * documentation and/or other materials provided with the distribution. 14327952Sdim * 3. The name of the author may not be used to endorse or promote products 15292915Sdim * derived from this software without specific prior written permission. 16292915Sdim * 17292915Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18292915Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19292915Sdim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20292915Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21292915Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22292915Sdim * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23314564Sdim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24314564Sdim * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25314564Sdim * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26314564Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27292915Sdim * SUCH DAMAGE. 28314564Sdim * 29314564Sdim * from: NetBSD: ebus.c,v 1.26 2001/09/10 16:27:53 eeh Exp 30292915Sdim * 31292915Sdim * $FreeBSD: head/sys/sparc64/isa/ofw_isa.c 117119 2003-07-01 14:52:47Z tmm $ 32314564Sdim */ 33292915Sdim 34314564Sdim/* 35314564Sdim * Helper functions which can be used in both ISA and EBus code. 36292915Sdim */ 37314564Sdim 38360784Sdim#include "opt_ofw_pci.h" 39292915Sdim 40314564Sdim#include <sys/param.h> 41292915Sdim#include <sys/systm.h> 42314564Sdim#include <sys/bus.h> 43314564Sdim 44292915Sdim#include <ofw/openfirm.h> 45292915Sdim#include <ofw/ofw_pci.h> 46314564Sdim 47314564Sdim#include <machine/bus.h> 48314564Sdim#include <machine/resource.h> 49314564Sdim#include <machine/ofw_bus.h> 50292915Sdim 51360784Sdim#include <sparc64/pci/ofw_pci.h> 52360784Sdim#include <sparc64/isa/ofw_isa.h> 53292915Sdim 54292915Sdim#include "pcib_if.h" 55292915Sdim 56314564Sdim/* XXX: this only supports PCI as parent bus right now. */ 57327952Sdimint 58327952Sdimofw_isa_map_iorange(struct isa_ranges *range, int nrange, u_long *start, 59292915Sdim u_long *end) 60314564Sdim{ 61314564Sdim u_int64_t offs, cstart, cend; 62292915Sdim int i; 63314564Sdim 64292915Sdim for (i = 0; i < nrange; i++) { 65292915Sdim cstart = ((u_int64_t)range[i].child_hi << 32) | 66292915Sdim range[i].child_lo; 67292915Sdim cend = cstart + range[i].size; 68292915Sdim if (*start < cstart || *start > cend) 69292915Sdim continue; 70292915Sdim if (*end < cstart || *end > cend) { 71292915Sdim panic("ofw_isa_map_iorange: iorange crosses pci " 72292915Sdim "ranges (%#lx not in %#lx - %#lx)", *end, cstart, 73314564Sdim cend); 74292915Sdim } 75292915Sdim offs = (((u_int64_t)range[i].phys_mid << 32) | 76292915Sdim range[i].phys_lo); 77292915Sdim *start = *start + offs - cstart; 78292915Sdim *end = *end + offs - cstart; 79292915Sdim /* Isolate address space and find the right tag */ 80314564Sdim switch (ISA_RANGE_PS(&range[i])) { 81292915Sdim case PCI_CS_IO: 82292915Sdim return (SYS_RES_IOPORT); 83292915Sdim case PCI_CS_MEM32: 84292915Sdim return (SYS_RES_MEMORY); 85292915Sdim default: 86292915Sdim panic("ofw_isa_map_iorange: illegal space %x", 87292915Sdim ISA_RANGE_PS(&range[i])); 88292915Sdim break; 89292915Sdim } 90292915Sdim } 91292915Sdim panic("ofw_isa_map_iorange: could not map range %#lx - %#lx", 92292915Sdim *start, *end); 93327952Sdim} 94327952Sdim 95292915Sdim#ifdef OFW_NEWPCI 96292915Sdimofw_pci_intr_t 97292915Sdimofw_isa_route_intr(device_t bridge, phandle_t node, struct ofw_bus_iinfo *ii, 98292915Sdim ofw_isa_intr_t intr) 99292915Sdim{ 100292915Sdim struct isa_regs reg; 101292915Sdim u_int8_t maskbuf[sizeof(reg) + sizeof(intr)]; 102292915Sdim device_t pbridge; 103292915Sdim ofw_isa_intr_t mintr; 104292915Sdim 105292915Sdim pbridge = device_get_parent(device_get_parent(bridge)); 106292915Sdim /* 107292915Sdim * If we get a match from using the map, the resulting INO is 108292915Sdim * fully specified, so we may not continue to map. 109292915Sdim */ 110292915Sdim if (!ofw_bus_lookup_imap(node, ii, ®, sizeof(reg), 111327952Sdim &intr, sizeof(intr), &mintr, sizeof(mintr), maskbuf)) { 112327952Sdim /* Try routing at the parent bridge. */ 113314564Sdim mintr = PCIB_ROUTE_INTERRUPT(pbridge, bridge, intr); 114292915Sdim } 115327952Sdim return (mintr); 116327952Sdim} 117327952Sdim#endif /* OFW_NEWPCI */ 118327952Sdim