1*bc7b9300SIan Lepore /*- 2*bc7b9300SIan Lepore * Copyright (c) 2015 Ian Lepore <ian@freebsd.org> 3*bc7b9300SIan Lepore * All rights reserved. 4*bc7b9300SIan Lepore * 5*bc7b9300SIan Lepore * Redistribution and use in source and binary forms, with or without 6*bc7b9300SIan Lepore * modification, are permitted provided that the following conditions 7*bc7b9300SIan Lepore * are met: 8*bc7b9300SIan Lepore * 1. Redistributions of source code must retain the above copyright 9*bc7b9300SIan Lepore * notice, this list of conditions and the following disclaimer. 10*bc7b9300SIan Lepore * 2. Redistributions in binary form must reproduce the above copyright 11*bc7b9300SIan Lepore * notice, this list of conditions and the following disclaimer in the 12*bc7b9300SIan Lepore * documentation and/or other materials provided with the distribution. 13*bc7b9300SIan Lepore * 14*bc7b9300SIan Lepore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15*bc7b9300SIan Lepore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16*bc7b9300SIan Lepore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17*bc7b9300SIan Lepore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18*bc7b9300SIan Lepore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19*bc7b9300SIan Lepore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20*bc7b9300SIan Lepore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*bc7b9300SIan Lepore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22*bc7b9300SIan Lepore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23*bc7b9300SIan Lepore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24*bc7b9300SIan Lepore * SUCH DAMAGE. 25*bc7b9300SIan Lepore * 26*bc7b9300SIan Lepore * The initial ofw_reg_to_paddr() implementation has been copied from powerpc 27*bc7b9300SIan Lepore * ofw_machdep.c OF_decode_addr(). It was added by Marcel Moolenaar, who did not 28*bc7b9300SIan Lepore * assert copyright with the addition but still deserves credit for the work. 29*bc7b9300SIan Lepore */ 30*bc7b9300SIan Lepore 31*bc7b9300SIan Lepore #include <sys/cdefs.h> 32*bc7b9300SIan Lepore __FBSDID("$FreeBSD$"); 33*bc7b9300SIan Lepore 34*bc7b9300SIan Lepore #include <sys/param.h> 35*bc7b9300SIan Lepore #include <sys/bus.h> 36*bc7b9300SIan Lepore #include <sys/libkern.h> 37*bc7b9300SIan Lepore 38*bc7b9300SIan Lepore #include <machine/bus.h> 39*bc7b9300SIan Lepore 40*bc7b9300SIan Lepore #include <dev/ofw/openfirm.h> 41*bc7b9300SIan Lepore #include <dev/ofw/ofw_pci.h> 42*bc7b9300SIan Lepore #include <dev/ofw/ofw_subr.h> 43*bc7b9300SIan Lepore 44*bc7b9300SIan Lepore static void 45*bc7b9300SIan Lepore get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip) 46*bc7b9300SIan Lepore { 47*bc7b9300SIan Lepore char type[64]; 48*bc7b9300SIan Lepore uint32_t addr, size; 49*bc7b9300SIan Lepore int pci, res; 50*bc7b9300SIan Lepore 51*bc7b9300SIan Lepore res = OF_getencprop(node, "#address-cells", &addr, sizeof(addr)); 52*bc7b9300SIan Lepore if (res == -1) 53*bc7b9300SIan Lepore addr = 2; 54*bc7b9300SIan Lepore res = OF_getencprop(node, "#size-cells", &size, sizeof(size)); 55*bc7b9300SIan Lepore if (res == -1) 56*bc7b9300SIan Lepore size = 1; 57*bc7b9300SIan Lepore pci = 0; 58*bc7b9300SIan Lepore if (addr == 3 && size == 2) { 59*bc7b9300SIan Lepore res = OF_getprop(node, "device_type", type, sizeof(type)); 60*bc7b9300SIan Lepore if (res != -1) { 61*bc7b9300SIan Lepore type[sizeof(type) - 1] = '\0'; 62*bc7b9300SIan Lepore pci = (strcmp(type, "pci") == 0) ? 1 : 0; 63*bc7b9300SIan Lepore } 64*bc7b9300SIan Lepore } 65*bc7b9300SIan Lepore if (addrp != NULL) 66*bc7b9300SIan Lepore *addrp = addr; 67*bc7b9300SIan Lepore if (sizep != NULL) 68*bc7b9300SIan Lepore *sizep = size; 69*bc7b9300SIan Lepore if (pcip != NULL) 70*bc7b9300SIan Lepore *pcip = pci; 71*bc7b9300SIan Lepore } 72*bc7b9300SIan Lepore 73*bc7b9300SIan Lepore int 74*bc7b9300SIan Lepore ofw_reg_to_paddr(phandle_t dev, int regno, bus_addr_t *paddr, 75*bc7b9300SIan Lepore bus_size_t *psize, pcell_t *ppci_hi) 76*bc7b9300SIan Lepore { 77*bc7b9300SIan Lepore pcell_t cell[32], pci_hi; 78*bc7b9300SIan Lepore bus_addr_t addr, raddr, baddr; 79*bc7b9300SIan Lepore bus_size_t size, rsize; 80*bc7b9300SIan Lepore uint32_t c, nbridge, naddr, nsize; 81*bc7b9300SIan Lepore phandle_t bridge, parent; 82*bc7b9300SIan Lepore u_int spc, rspc; 83*bc7b9300SIan Lepore int pci, pcib, res; 84*bc7b9300SIan Lepore 85*bc7b9300SIan Lepore /* Sanity checking. */ 86*bc7b9300SIan Lepore if (dev == 0) 87*bc7b9300SIan Lepore return (EINVAL); 88*bc7b9300SIan Lepore bridge = OF_parent(dev); 89*bc7b9300SIan Lepore if (bridge == 0) 90*bc7b9300SIan Lepore return (EINVAL); 91*bc7b9300SIan Lepore if (regno < 0) 92*bc7b9300SIan Lepore return (EINVAL); 93*bc7b9300SIan Lepore if (paddr == NULL || psize == NULL) 94*bc7b9300SIan Lepore return (EINVAL); 95*bc7b9300SIan Lepore 96*bc7b9300SIan Lepore get_addr_props(bridge, &naddr, &nsize, &pci); 97*bc7b9300SIan Lepore res = OF_getencprop(dev, (pci) ? "assigned-addresses" : "reg", 98*bc7b9300SIan Lepore cell, sizeof(cell)); 99*bc7b9300SIan Lepore if (res == -1) 100*bc7b9300SIan Lepore return (ENXIO); 101*bc7b9300SIan Lepore if (res % sizeof(cell[0])) 102*bc7b9300SIan Lepore return (ENXIO); 103*bc7b9300SIan Lepore res /= sizeof(cell[0]); 104*bc7b9300SIan Lepore regno *= naddr + nsize; 105*bc7b9300SIan Lepore if (regno + naddr + nsize > res) 106*bc7b9300SIan Lepore return (EINVAL); 107*bc7b9300SIan Lepore pci_hi = pci ? cell[regno] : OFW_PADDR_NOT_PCI; 108*bc7b9300SIan Lepore spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK; 109*bc7b9300SIan Lepore addr = 0; 110*bc7b9300SIan Lepore for (c = 0; c < naddr; c++) 111*bc7b9300SIan Lepore addr = ((uint64_t)addr << 32) | cell[regno++]; 112*bc7b9300SIan Lepore size = 0; 113*bc7b9300SIan Lepore for (c = 0; c < nsize; c++) 114*bc7b9300SIan Lepore size = ((uint64_t)size << 32) | cell[regno++]; 115*bc7b9300SIan Lepore /* 116*bc7b9300SIan Lepore * Map the address range in the bridge's decoding window as given 117*bc7b9300SIan Lepore * by the "ranges" property. If a node doesn't have such property 118*bc7b9300SIan Lepore * or the property is empty, we assume an identity mapping. The 119*bc7b9300SIan Lepore * standard says a missing property indicates no possible mapping. 120*bc7b9300SIan Lepore * This code is more liberal since the intended use is to get a 121*bc7b9300SIan Lepore * console running early, and a printf to warn of malformed data 122*bc7b9300SIan Lepore * is probably futile before the console is fully set up. 123*bc7b9300SIan Lepore */ 124*bc7b9300SIan Lepore parent = OF_parent(bridge); 125*bc7b9300SIan Lepore while (parent != 0) { 126*bc7b9300SIan Lepore get_addr_props(parent, &nbridge, NULL, &pcib); 127*bc7b9300SIan Lepore res = OF_getencprop(bridge, "ranges", cell, sizeof(cell)); 128*bc7b9300SIan Lepore if (res < 1) 129*bc7b9300SIan Lepore goto next; 130*bc7b9300SIan Lepore if (res % sizeof(cell[0])) 131*bc7b9300SIan Lepore return (ENXIO); 132*bc7b9300SIan Lepore /* Capture pci_hi if we just transitioned onto a PCI bus. */ 133*bc7b9300SIan Lepore if (pcib && pci_hi == OFW_PADDR_NOT_PCI) { 134*bc7b9300SIan Lepore pci_hi = cell[0]; 135*bc7b9300SIan Lepore spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK; 136*bc7b9300SIan Lepore } 137*bc7b9300SIan Lepore res /= sizeof(cell[0]); 138*bc7b9300SIan Lepore regno = 0; 139*bc7b9300SIan Lepore while (regno < res) { 140*bc7b9300SIan Lepore rspc = (pci) 141*bc7b9300SIan Lepore ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK 142*bc7b9300SIan Lepore : OFW_PADDR_NOT_PCI; 143*bc7b9300SIan Lepore if (rspc != spc) { 144*bc7b9300SIan Lepore regno += naddr + nbridge + nsize; 145*bc7b9300SIan Lepore continue; 146*bc7b9300SIan Lepore } 147*bc7b9300SIan Lepore raddr = 0; 148*bc7b9300SIan Lepore for (c = 0; c < naddr; c++) 149*bc7b9300SIan Lepore raddr = ((uint64_t)raddr << 32) | cell[regno++]; 150*bc7b9300SIan Lepore rspc = (pcib) 151*bc7b9300SIan Lepore ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK 152*bc7b9300SIan Lepore : OFW_PADDR_NOT_PCI; 153*bc7b9300SIan Lepore baddr = 0; 154*bc7b9300SIan Lepore for (c = 0; c < nbridge; c++) 155*bc7b9300SIan Lepore baddr = ((uint64_t)baddr << 32) | cell[regno++]; 156*bc7b9300SIan Lepore rsize = 0; 157*bc7b9300SIan Lepore for (c = 0; c < nsize; c++) 158*bc7b9300SIan Lepore rsize = ((uint64_t)rsize << 32) | cell[regno++]; 159*bc7b9300SIan Lepore if (addr < raddr || addr >= raddr + rsize) 160*bc7b9300SIan Lepore continue; 161*bc7b9300SIan Lepore addr = addr - raddr + baddr; 162*bc7b9300SIan Lepore if (rspc != OFW_PADDR_NOT_PCI) 163*bc7b9300SIan Lepore spc = rspc; 164*bc7b9300SIan Lepore } 165*bc7b9300SIan Lepore next: 166*bc7b9300SIan Lepore bridge = parent; 167*bc7b9300SIan Lepore parent = OF_parent(bridge); 168*bc7b9300SIan Lepore get_addr_props(bridge, &naddr, &nsize, &pci); 169*bc7b9300SIan Lepore } 170*bc7b9300SIan Lepore 171*bc7b9300SIan Lepore *paddr = addr; 172*bc7b9300SIan Lepore *psize = size; 173*bc7b9300SIan Lepore if (ppci_hi != NULL) 174*bc7b9300SIan Lepore *ppci_hi = pci_hi; 175*bc7b9300SIan Lepore 176*bc7b9300SIan Lepore return (0); 177*bc7b9300SIan Lepore } 178