ofw_machdep.c revision 133728
1/*- 2 * Copyright (c) 2001 by Thomas Moestl <tmm@FreeBSD.org>. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 23 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * $FreeBSD: head/sys/sparc64/sparc64/ofw_machdep.c 133728 2004-08-14 21:43:37Z marius $ 26 */ 27 28/* 29 * Some OpenFirmware helper functions that are likely machine dependent. 30 */ 31 32#include <sys/param.h> 33#include <sys/bus.h> 34#include <sys/systm.h> 35 36#include <net/ethernet.h> 37 38#include <dev/ofw/ofw_bus.h> 39#include <dev/ofw/openfirm.h> 40 41#include <machine/bus.h> 42#include <machine/idprom.h> 43#include <machine/ofw_machdep.h> 44#include <machine/ofw_upa.h> 45#include <machine/resource.h> 46 47#include <sparc64/pci/ofw_pci.h> 48#include <sparc64/isa/ofw_isa.h> 49#include <sparc64/sbus/ofw_sbus.h> 50 51void 52OF_getetheraddr(device_t dev, u_char *addr) 53{ 54 char buf[sizeof("true")]; 55 phandle_t node; 56 struct idprom idp; 57 58 if ((node = OF_finddevice("/options")) > 0 && 59 OF_getprop(node, "local-mac-address?", buf, sizeof(buf)) > 0) { 60 buf[sizeof(buf) - 1] = '\0'; 61 if (strcmp(buf, "true") == 0 && 62 (node = ofw_bus_get_node(dev)) > 0 && 63 OF_getprop(node, "local-mac-address", addr, 64 ETHER_ADDR_LEN) == ETHER_ADDR_LEN) 65 return; 66 } 67 68 node = OF_peer(0); 69 if (node <= 0 || OF_getprop(node, "idprom", &idp, sizeof(idp)) == -1) 70 panic("Could not determine the machine ethernet address"); 71 bcopy(&idp.id_ether, addr, ETHER_ADDR_LEN); 72} 73 74int 75OF_decode_addr(phandle_t node, int *space, bus_addr_t *addr) 76{ 77 char name[32]; 78 union { 79 struct isa_ranges isa[4]; 80 struct sbus_ranges sbus[8]; 81 struct upa_ranges upa[4]; 82 } range; 83 union { 84 struct isa_regs isa; 85 struct sbus_regs sbus; 86 } reg; 87 phandle_t bus, pbus; 88 u_long child, dummy, phys; 89 int cs, i, rsz, type; 90 91 bus = OF_parent(node); 92 if (bus == 0) 93 return (ENXIO); 94 if (OF_getprop(bus, "name", name, sizeof(name)) == -1) 95 return (ENXIO); 96 name[sizeof(name) - 1] = '\0'; 97 if (strcmp(name, "ebus") == 0 || strcmp(name, "isa") == 0) { 98 if (OF_getprop(node, "reg", ®.isa, sizeof(reg.isa)) == -1) 99 return (ENXIO); 100 rsz = OF_getprop(bus, "ranges", range.isa, sizeof(range.isa)); 101 if (rsz == -1) 102 return (ENXIO); 103 phys = ISA_REG_PHYS(®.isa); 104 dummy = phys + 1; 105 type = ofw_isa_range_map(range.isa, rsz / sizeof(*range.isa), 106 &phys, &dummy, NULL); 107 if (type == SYS_RES_MEMORY) { 108 cs = PCI_CS_MEM32; 109 *space = PCI_MEMORY_BUS_SPACE; 110 } else { 111 cs = PCI_CS_IO; 112 *space = PCI_IO_BUS_SPACE; 113 } 114 115 /* Find the topmost PCI node (the host bridge) */ 116 while (1) { 117 pbus = OF_parent(bus); 118 if (pbus == 0) 119 return (ENXIO); 120 if (OF_getprop(pbus, "name", name, sizeof(name)) == -1) 121 return (ENXIO); 122 name[sizeof(name) - 1] = '\0'; 123 if (strcmp(name, "pci") != 0) 124 break; 125 bus = pbus; 126 } 127 128 /* There wasn't a PCI bridge. */ 129 if (bus == OF_parent(node)) 130 return (ENXIO); 131 132 /* Make sure we reached the UPA/PCI node. */ 133 if (OF_getprop(pbus, "device_type", name, sizeof(name)) == -1) 134 return (ENXIO); 135 name[sizeof(name) - 1] = '\0'; 136 if (strcmp(name, "upa") != 0) 137 return (ENXIO); 138 139 rsz = OF_getprop(bus, "ranges", range.upa, sizeof(range.upa)); 140 if (rsz == -1) 141 return (ENXIO); 142 for (i = 0; i < (rsz / sizeof(range.upa[0])); i++) { 143 child = UPA_RANGE_CHILD(&range.upa[i]); 144 if (UPA_RANGE_CS(&range.upa[i]) == cs && 145 phys >= child && 146 phys - child < UPA_RANGE_SIZE(&range.upa[i])) { 147 *addr = UPA_RANGE_PHYS(&range.upa[i]) + phys; 148 return (0); 149 } 150 } 151 } else if (strcmp(name, "sbus") == 0) { 152 if (OF_getprop(node, "reg", ®.sbus, sizeof(reg.sbus)) == -1) 153 return (ENXIO); 154 rsz = OF_getprop(bus, "ranges", range.sbus, 155 sizeof(range.sbus)); 156 if (rsz == -1) 157 return (ENXIO); 158 for (i = 0; i < (rsz / sizeof(range.sbus[0])); i++) { 159 if (reg.sbus.sbr_slot != range.sbus[i].cspace) 160 continue; 161 if (reg.sbus.sbr_offset < range.sbus[i].coffset || 162 reg.sbus.sbr_offset >= range.sbus[i].coffset + 163 range.sbus[i].size) 164 continue; 165 /* Found it... */ 166 phys = range.sbus[i].poffset | 167 ((bus_addr_t)range.sbus[i].pspace << 32); 168 phys += reg.sbus.sbr_offset - range.sbus[i].coffset; 169 *addr = phys; 170 *space = SBUS_BUS_SPACE; 171 return (0); 172 } 173 } 174 return (ENXIO); 175} 176