ofw_machdep.c revision 200924
186229Stmm/*- 286229Stmm * Copyright (c) 2001 by Thomas Moestl <tmm@FreeBSD.org>. 3141753Smarius * Copyright (c) 2005 by Marius Strobl <marius@FreeBSD.org>. 486229Stmm * All rights reserved. 586229Stmm * 686229Stmm * Redistribution and use in source and binary forms, with or without 786229Stmm * modification, are permitted provided that the following conditions 886229Stmm * are met: 986229Stmm * 1. Redistributions of source code must retain the above copyright 1086229Stmm * notice, this list of conditions and the following disclaimer. 1186229Stmm * 2. Redistributions in binary form must reproduce the above copyright 1286229Stmm * notice, this list of conditions and the following disclaimer in the 1386229Stmm * documentation and/or other materials provided with the distribution. 1486229Stmm * 1586229Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1686229Stmm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1786229Stmm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1886229Stmm * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 1986229Stmm * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 2086229Stmm * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 2186229Stmm * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 2286229Stmm * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2386229Stmm * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 2486229Stmm * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2586229Stmm */ 26200924Smarius 27146473Smarius#include <sys/cdefs.h> 28146473Smarius__FBSDID("$FreeBSD: head/sys/sparc64/sparc64/ofw_machdep.c 200924 2009-12-23 22:11:33Z marius $"); 2986229Stmm 3086229Stmm/* 31133862Smarius * Some Open Firmware helper functions that are likely machine dependent. 3286229Stmm */ 3386229Stmm 3486229Stmm#include <sys/param.h> 35133589Smarius#include <sys/bus.h> 3686229Stmm#include <sys/systm.h> 3786229Stmm 3888370Stmm#include <net/ethernet.h> 3988370Stmm 40133589Smarius#include <dev/ofw/ofw_bus.h> 41141753Smarius#include <dev/ofw/ofw_pci.h> 42119338Simp#include <dev/ofw/openfirm.h> 4386229Stmm 44119697Smarcel#include <machine/bus.h> 4586229Stmm#include <machine/idprom.h> 4686229Stmm#include <machine/ofw_machdep.h> 4786229Stmm 4886229Stmmvoid 4986229StmmOF_getetheraddr(device_t dev, u_char *addr) 5086229Stmm{ 51133728Smarius char buf[sizeof("true")]; 5286229Stmm phandle_t node; 5386229Stmm struct idprom idp; 5486229Stmm 55133728Smarius if ((node = OF_finddevice("/options")) > 0 && 56133728Smarius OF_getprop(node, "local-mac-address?", buf, sizeof(buf)) > 0) { 57133728Smarius buf[sizeof(buf) - 1] = '\0'; 58133728Smarius if (strcmp(buf, "true") == 0 && 59133728Smarius (node = ofw_bus_get_node(dev)) > 0 && 60133728Smarius OF_getprop(node, "local-mac-address", addr, 61133728Smarius ETHER_ADDR_LEN) == ETHER_ADDR_LEN) 62133728Smarius return; 63133728Smarius } 64133728Smarius 6586229Stmm node = OF_peer(0); 6686229Stmm if (node <= 0 || OF_getprop(node, "idprom", &idp, sizeof(idp)) == -1) 67200924Smarius panic("Could not determine the machine Ethernet address"); 6888370Stmm bcopy(&idp.id_ether, addr, ETHER_ADDR_LEN); 6986229Stmm} 70119697Smarcel 71141753Smariusstatic __inline uint32_t 72141753Smariusphys_hi_mask_space(const char *bus, uint32_t phys_hi) 73141753Smarius{ 74141753Smarius 75141753Smarius if (strcmp(bus, "ebus") == 0 || strcmp(bus, "isa") == 0) 76200924Smarius phys_hi &= 0x1; 77141753Smarius else if (strcmp(bus, "pci") == 0) 78200924Smarius phys_hi &= OFW_PCI_PHYS_HI_SPACEMASK; 79141753Smarius /* The phys.hi cells of the other busses only contain space bits. */ 80200924Smarius return (phys_hi); 81141753Smarius} 82141753Smarius 83141753Smarius/* 84141753Smarius * Return the physical address and the bus space to use for a node 85141753Smarius * referenced by its package handle and the index of the register bank 86200924Smarius * to decode. Intended to be used to together with sparc64_fake_bustag() 87141753Smarius * by console drivers in early boot only. 88141753Smarius * Works by mapping the address of the node's bank given in the address 89141753Smarius * space of its parent upward in the device tree at each bridge along the 90141753Smarius * path. 91141753Smarius * Currently only really deals with max. 64-bit addresses, i.e. addresses 92200924Smarius * consisting of max. 2 phys cells (phys.hi and phys.lo). If we encounter 93141753Smarius * a 3 phys cells address (as with PCI addresses) we assume phys.hi can 94141753Smarius * be ignored except for the space bits (generally contained in phys.hi) 95141753Smarius * and treat phys.mid as phys.hi. 96141753Smarius */ 97119697Smarcelint 98141753SmariusOF_decode_addr(phandle_t node, int bank, int *space, bus_addr_t *addr) 99119697Smarcel{ 100119697Smarcel char name[32]; 101141753Smarius uint64_t cend, cstart, end, phys, sz, start; 102141753Smarius pcell_t addrc, szc, paddrc; 103141753Smarius phandle_t bus, lbus, pbus; 104141753Smarius uint32_t banks[10 * 5]; /* 10 PCI banks */ 105141753Smarius uint32_t cspace, spc; 106141753Smarius int i, j, nbank; 107119697Smarcel 108141753Smarius /* 109141753Smarius * In general the addresses are contained in the "reg" property 110200924Smarius * of a node. The first address in the "reg" property of a PCI 111141753Smarius * node however is the address of its configuration registers in 112200924Smarius * the configuration space of the host bridge. Additional entries 113200924Smarius * denote the memory and I/O addresses. For relocatable addresses 114141753Smarius * the "reg" property contains the BAR, for non-relocatable 115200924Smarius * addresses it contains the absolute PCI address. The PCI-only 116141753Smarius * "assigned-addresses" property however always contains the 117141753Smarius * absolute PCI addresses. 118141753Smarius * The "assigned-addresses" and "reg" properties are arrays of 119141753Smarius * address structures consisting of #address-cells 32-bit phys 120200924Smarius * cells and #size-cells 32-bit size cells. If a parent lacks 121141753Smarius * the "#address-cells" or "#size-cells" property the default 122141753Smarius * for #address-cells to use is 2 and for #size-cells 1. 123141753Smarius */ 124119697Smarcel bus = OF_parent(node); 125123866Sobrien if (bus == 0) 126119697Smarcel return (ENXIO); 127119697Smarcel if (OF_getprop(bus, "name", name, sizeof(name)) == -1) 128119697Smarcel return (ENXIO); 129119697Smarcel name[sizeof(name) - 1] = '\0'; 130141753Smarius if (OF_getprop(bus, "#address-cells", &addrc, sizeof(addrc)) == -1) 131141753Smarius addrc = 2; 132141753Smarius if (OF_getprop(bus, "#size-cells", &szc, sizeof(szc)) == -1) 133141753Smarius szc = 1; 134146473Smarius if (addrc < 2 || addrc > 3 || szc < 1 || szc > 2) 135141753Smarius return (ENXIO); 136141753Smarius if (strcmp(name, "pci") == 0) { 137141753Smarius if (addrc > 3) 138119697Smarcel return (ENXIO); 139141753Smarius nbank = OF_getprop(node, "assigned-addresses", &banks, 140141753Smarius sizeof(banks)); 141141753Smarius } else { 142141753Smarius if (addrc > 2) 143119697Smarcel return (ENXIO); 144141753Smarius nbank = OF_getprop(node, "reg", &banks, sizeof(banks)); 145141753Smarius } 146141753Smarius if (nbank == -1) 147141753Smarius return (ENXIO); 148141753Smarius nbank /= sizeof(banks[0]) * (addrc + szc); 149141753Smarius if (bank < 0 || bank > nbank - 1) 150141753Smarius return (ENXIO); 151141753Smarius phys = 0; 152141753Smarius for (i = 0; i < MIN(2, addrc); i++) 153141753Smarius phys |= (uint64_t)banks[(addrc + szc) * bank + addrc - 2 + i] << 154141753Smarius 32 * (MIN(2, addrc) - i - 1); 155141753Smarius sz = 0; 156141753Smarius for (i = 0; i < szc; i++) 157141753Smarius sz |= (uint64_t)banks[(addrc + szc) * bank + addrc + i] << 158141753Smarius 32 * (szc - i - 1); 159141753Smarius start = phys; 160141753Smarius end = phys + sz - 1; 161141753Smarius spc = phys_hi_mask_space(name, banks[(addrc + szc) * bank]); 162119697Smarcel 163141753Smarius /* 164141753Smarius * Map upward in the device tree at every bridge we encounter 165141753Smarius * using their "ranges" properties. 166141753Smarius * The "ranges" property of a bridge is an array of a structure 167141753Smarius * consisting of that bridge's #address-cells 32-bit child-phys 168141753Smarius * cells, its parent bridge #address-cells 32-bit parent-phys 169141753Smarius * cells and that bridge's #size-cells 32-bit size cells. 170141753Smarius * If a bridge doesn't have a "ranges" property no mapping is 171141753Smarius * necessary at that bridge. 172141753Smarius */ 173141753Smarius cspace = 0; 174141753Smarius lbus = bus; 175141753Smarius while ((pbus = OF_parent(bus)) != 0) { 176141753Smarius if (OF_getprop(pbus, "#address-cells", &paddrc, 177141753Smarius sizeof(paddrc)) == -1) 178141753Smarius paddrc = 2; 179146473Smarius if (paddrc < 2 || paddrc > 3) 180141753Smarius return (ENXIO); 181141753Smarius nbank = OF_getprop(bus, "ranges", &banks, sizeof(banks)); 182141753Smarius if (nbank == -1) { 183141753Smarius if (OF_getprop(pbus, "name", name, sizeof(name)) == -1) 184119697Smarcel return (ENXIO); 185141753Smarius name[sizeof(name) - 1] = '\0'; 186141753Smarius goto skip; 187141753Smarius } 188141753Smarius if (lbus != bus) { 189141753Smarius if (OF_getprop(bus, "#size-cells", &szc, 190141753Smarius sizeof(szc)) == -1) 191141753Smarius szc = 1; 192146473Smarius if (szc < 1 || szc > 2) 193141753Smarius return (ENXIO); 194141753Smarius } 195141753Smarius nbank /= sizeof(banks[0]) * (addrc + paddrc + szc); 196141753Smarius for (i = 0; i < nbank; i++) { 197141753Smarius cspace = phys_hi_mask_space(name, 198141753Smarius banks[(addrc + paddrc + szc) * i]); 199141753Smarius if (cspace != spc) 200141753Smarius continue; 201141753Smarius phys = 0; 202141753Smarius for (j = 0; j < MIN(2, addrc); j++) 203141753Smarius phys |= (uint64_t)banks[ 204141753Smarius (addrc + paddrc + szc) * i + 205141753Smarius addrc - 2 + j] << 206141753Smarius 32 * (MIN(2, addrc) - j - 1); 207141753Smarius sz = 0; 208141753Smarius for (j = 0; j < szc; j++) 209141753Smarius sz |= (uint64_t)banks[ 210141753Smarius (addrc + paddrc + szc) * i + addrc + 211141753Smarius paddrc + j] << 212141753Smarius 32 * (szc - j - 1); 213141753Smarius cstart = phys; 214141753Smarius cend = phys + sz - 1; 215141753Smarius if (start < cstart || start > cend) 216141753Smarius continue; 217141753Smarius if (end < cstart || end > cend) 218141753Smarius return (ENXIO); 219141753Smarius phys = 0; 220141753Smarius for (j = 0; j < MIN(2, paddrc); j++) 221141753Smarius phys |= (uint64_t)banks[ 222141753Smarius (addrc + paddrc + szc) * i + addrc + 223141753Smarius paddrc - 2 + j] << 224141753Smarius 32 * (MIN(2, paddrc) - j - 1); 225141753Smarius start += phys - cstart; 226141753Smarius end += phys - cstart; 227119697Smarcel if (OF_getprop(pbus, "name", name, sizeof(name)) == -1) 228119697Smarcel return (ENXIO); 229119697Smarcel name[sizeof(name) - 1] = '\0'; 230141753Smarius spc = phys_hi_mask_space(name, 231141753Smarius banks[(addrc + paddrc + szc) * i + addrc]); 232141753Smarius break; 233119697Smarcel } 234141753Smarius if (i == nbank) 235119697Smarcel return (ENXIO); 236141753Smarius skip: 237141753Smarius addrc = paddrc; 238141753Smarius lbus = bus; 239141753Smarius bus = pbus; 240141753Smarius } 241119697Smarcel 242141753Smarius /* Done with mapping. Return the bus space as used by FreeBSD. */ 243141753Smarius *addr = start; 244166058Smarius if (OF_parent(lbus) == 0) { 245166096Smarius *space = NEXUS_BUS_SPACE; 246166058Smarius return (0); 247166058Smarius } 248141753Smarius if (OF_getprop(lbus, "name", name, sizeof(name)) == -1) 249141753Smarius return (ENXIO); 250141753Smarius name[sizeof(name) - 1] = '\0'; 251166058Smarius if (strcmp(name, "central") == 0 || strcmp(name, "upa") == 0) { 252166096Smarius *space = NEXUS_BUS_SPACE; 253141753Smarius return (0); 254141753Smarius } else if (strcmp(name, "pci") == 0) { 255141753Smarius switch (cspace) { 256141753Smarius case OFW_PCI_PHYS_HI_SPACE_IO: 257141753Smarius *space = PCI_IO_BUS_SPACE; 258141753Smarius return (0); 259141753Smarius case OFW_PCI_PHYS_HI_SPACE_MEM32: 260141753Smarius *space = PCI_MEMORY_BUS_SPACE; 261141753Smarius return (0); 262119697Smarcel } 263119697Smarcel } else if (strcmp(name, "sbus") == 0) { 264141753Smarius *space = SBUS_BUS_SPACE; 265141753Smarius return (0); 266119697Smarcel } 267119697Smarcel return (ENXIO); 268119697Smarcel} 269