ofw_machdep.c revision 206448
186229Stmm/*- 286229Stmm * Copyright (c) 2001 by Thomas Moestl <tmm@FreeBSD.org>. 3200925Smarius * Copyright (c) 2005 - 2009 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 206448 2010-04-10 10:44:41Z 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]; 101200925Smarius uint64_t cend, cstart, end, phys, pphys, sz, start; 102141753Smarius pcell_t addrc, szc, paddrc; 103141753Smarius phandle_t bus, lbus, pbus; 104141753Smarius uint32_t banks[10 * 5]; /* 10 PCI banks */ 105200925Smarius uint32_t cspc, pspc, 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); 151200925Smarius bank *= addrc + szc; 152200925Smarius spc = phys_hi_mask_space(name, banks[bank]); 153200925Smarius /* Skip the high cell for 3-cell addresses. */ 154200925Smarius bank += addrc - 2; 155141753Smarius phys = 0; 156141753Smarius for (i = 0; i < MIN(2, addrc); i++) 157200925Smarius phys = ((uint64_t)phys << 32) | banks[bank++]; 158141753Smarius sz = 0; 159141753Smarius for (i = 0; i < szc; i++) 160200925Smarius sz = ((uint64_t)sz << 32) | banks[bank++]; 161141753Smarius start = phys; 162141753Smarius end = phys + sz - 1; 163119697Smarcel 164141753Smarius /* 165141753Smarius * Map upward in the device tree at every bridge we encounter 166141753Smarius * using their "ranges" properties. 167141753Smarius * The "ranges" property of a bridge is an array of a structure 168141753Smarius * consisting of that bridge's #address-cells 32-bit child-phys 169141753Smarius * cells, its parent bridge #address-cells 32-bit parent-phys 170141753Smarius * cells and that bridge's #size-cells 32-bit size cells. 171141753Smarius * If a bridge doesn't have a "ranges" property no mapping is 172141753Smarius * necessary at that bridge. 173141753Smarius */ 174200925Smarius cspc = 0; 175141753Smarius lbus = bus; 176141753Smarius while ((pbus = OF_parent(bus)) != 0) { 177141753Smarius if (OF_getprop(pbus, "#address-cells", &paddrc, 178141753Smarius sizeof(paddrc)) == -1) 179141753Smarius paddrc = 2; 180146473Smarius if (paddrc < 2 || paddrc > 3) 181141753Smarius return (ENXIO); 182141753Smarius nbank = OF_getprop(bus, "ranges", &banks, sizeof(banks)); 183141753Smarius if (nbank == -1) { 184141753Smarius if (OF_getprop(pbus, "name", name, sizeof(name)) == -1) 185119697Smarcel return (ENXIO); 186141753Smarius name[sizeof(name) - 1] = '\0'; 187141753Smarius goto skip; 188141753Smarius } 189206448Smarius if (OF_getprop(bus, "#size-cells", &szc, sizeof(szc)) == -1) 190206448Smarius szc = 1; 191206448Smarius if (szc < 1 || szc > 2) 192206448Smarius return (ENXIO); 193141753Smarius nbank /= sizeof(banks[0]) * (addrc + paddrc + szc); 194200925Smarius bank = 0; 195141753Smarius for (i = 0; i < nbank; i++) { 196200925Smarius cspc = phys_hi_mask_space(name, banks[bank]); 197200925Smarius if (cspc != spc) { 198200925Smarius bank += addrc + paddrc + szc; 199141753Smarius continue; 200200925Smarius } 201200925Smarius /* Skip the high cell for 3-cell addresses. */ 202200925Smarius bank += addrc - 2; 203141753Smarius phys = 0; 204141753Smarius for (j = 0; j < MIN(2, addrc); j++) 205200925Smarius phys = ((uint64_t)phys << 32) | banks[bank++]; 206200925Smarius pspc = banks[bank]; 207200925Smarius /* Skip the high cell for 3-cell addresses. */ 208200925Smarius bank += paddrc - 2; 209200925Smarius pphys = 0; 210200925Smarius for (j = 0; j < MIN(2, paddrc); j++) 211200925Smarius pphys = 212200925Smarius ((uint64_t)pphys << 32) | banks[bank++]; 213141753Smarius sz = 0; 214141753Smarius for (j = 0; j < szc; j++) 215200925Smarius sz = ((uint64_t)sz << 32) | banks[bank++]; 216141753Smarius cstart = phys; 217141753Smarius cend = phys + sz - 1; 218141753Smarius if (start < cstart || start > cend) 219141753Smarius continue; 220141753Smarius if (end < cstart || end > cend) 221141753Smarius return (ENXIO); 222119697Smarcel if (OF_getprop(pbus, "name", name, sizeof(name)) == -1) 223119697Smarcel return (ENXIO); 224119697Smarcel name[sizeof(name) - 1] = '\0'; 225200925Smarius spc = phys_hi_mask_space(name, pspc); 226200925Smarius start += pphys - cstart; 227200925Smarius end += pphys - cstart; 228141753Smarius break; 229119697Smarcel } 230141753Smarius if (i == nbank) 231119697Smarcel return (ENXIO); 232206448Smarius lbus = bus; 233141753Smarius skip: 234141753Smarius addrc = paddrc; 235141753Smarius bus = pbus; 236141753Smarius } 237119697Smarcel 238141753Smarius *addr = start; 239200925Smarius /* Determine the bus space based on the last bus we mapped. */ 240166058Smarius if (OF_parent(lbus) == 0) { 241166096Smarius *space = NEXUS_BUS_SPACE; 242166058Smarius return (0); 243166058Smarius } 244141753Smarius if (OF_getprop(lbus, "name", name, sizeof(name)) == -1) 245141753Smarius return (ENXIO); 246141753Smarius name[sizeof(name) - 1] = '\0'; 247200925Smarius if (strcmp(name, "central") == 0 || strcmp(name, "ebus") == 0 || 248200925Smarius strcmp(name, "upa") == 0) { 249166096Smarius *space = NEXUS_BUS_SPACE; 250141753Smarius return (0); 251141753Smarius } else if (strcmp(name, "pci") == 0) { 252200925Smarius switch (cspc) { 253141753Smarius case OFW_PCI_PHYS_HI_SPACE_IO: 254141753Smarius *space = PCI_IO_BUS_SPACE; 255141753Smarius return (0); 256141753Smarius case OFW_PCI_PHYS_HI_SPACE_MEM32: 257141753Smarius *space = PCI_MEMORY_BUS_SPACE; 258141753Smarius return (0); 259119697Smarcel } 260119697Smarcel } else if (strcmp(name, "sbus") == 0) { 261141753Smarius *space = SBUS_BUS_SPACE; 262141753Smarius return (0); 263119697Smarcel } 264119697Smarcel return (ENXIO); 265119697Smarcel} 266