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