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> 37*910905c7SZbigniew Bodek #include <sys/rman.h> 38bc7b9300SIan Lepore 39bc7b9300SIan Lepore #include <machine/bus.h> 40bc7b9300SIan Lepore 41bc7b9300SIan Lepore #include <dev/ofw/openfirm.h> 42bc7b9300SIan Lepore #include <dev/ofw/ofw_subr.h> 43a259e55bSZbigniew Bodek #include <dev/ofw/ofw_bus_subr.h> 44a259e55bSZbigniew Bodek #include <dev/ofw/ofw_pci.h> 45bc7b9300SIan Lepore 46bc7b9300SIan Lepore static void 47bc7b9300SIan Lepore get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip) 48bc7b9300SIan Lepore { 49bc7b9300SIan Lepore char type[64]; 50bc7b9300SIan Lepore uint32_t addr, size; 51bc7b9300SIan Lepore int pci, res; 52bc7b9300SIan Lepore 53bc7b9300SIan Lepore res = OF_getencprop(node, "#address-cells", &addr, sizeof(addr)); 54bc7b9300SIan Lepore if (res == -1) 55bc7b9300SIan Lepore addr = 2; 56bc7b9300SIan Lepore res = OF_getencprop(node, "#size-cells", &size, sizeof(size)); 57bc7b9300SIan Lepore if (res == -1) 58bc7b9300SIan Lepore size = 1; 59bc7b9300SIan Lepore pci = 0; 60bc7b9300SIan Lepore if (addr == 3 && size == 2) { 61bc7b9300SIan Lepore res = OF_getprop(node, "device_type", type, sizeof(type)); 62bc7b9300SIan Lepore if (res != -1) { 63bc7b9300SIan Lepore type[sizeof(type) - 1] = '\0'; 64bc7b9300SIan Lepore pci = (strcmp(type, "pci") == 0) ? 1 : 0; 65bc7b9300SIan Lepore } 66bc7b9300SIan Lepore } 67bc7b9300SIan Lepore if (addrp != NULL) 68bc7b9300SIan Lepore *addrp = addr; 69bc7b9300SIan Lepore if (sizep != NULL) 70bc7b9300SIan Lepore *sizep = size; 71bc7b9300SIan Lepore if (pcip != NULL) 72bc7b9300SIan Lepore *pcip = pci; 73bc7b9300SIan Lepore } 74bc7b9300SIan Lepore 75bc7b9300SIan Lepore int 76bc7b9300SIan Lepore ofw_reg_to_paddr(phandle_t dev, int regno, bus_addr_t *paddr, 77bc7b9300SIan Lepore bus_size_t *psize, pcell_t *ppci_hi) 78bc7b9300SIan Lepore { 79bc7b9300SIan Lepore pcell_t cell[32], pci_hi; 80b958a08eSAndrew Turner uint64_t addr, raddr, baddr; 81b958a08eSAndrew Turner uint64_t size, rsize; 82bc7b9300SIan Lepore uint32_t c, nbridge, naddr, nsize; 83bc7b9300SIan Lepore phandle_t bridge, parent; 84bc7b9300SIan Lepore u_int spc, rspc; 85bc7b9300SIan Lepore int pci, pcib, res; 86bc7b9300SIan Lepore 87bc7b9300SIan Lepore /* Sanity checking. */ 88bc7b9300SIan Lepore if (dev == 0) 89bc7b9300SIan Lepore return (EINVAL); 90bc7b9300SIan Lepore bridge = OF_parent(dev); 91bc7b9300SIan Lepore if (bridge == 0) 92bc7b9300SIan Lepore return (EINVAL); 93bc7b9300SIan Lepore if (regno < 0) 94bc7b9300SIan Lepore return (EINVAL); 95bc7b9300SIan Lepore if (paddr == NULL || psize == NULL) 96bc7b9300SIan Lepore return (EINVAL); 97bc7b9300SIan Lepore 98bc7b9300SIan Lepore get_addr_props(bridge, &naddr, &nsize, &pci); 99bc7b9300SIan Lepore res = OF_getencprop(dev, (pci) ? "assigned-addresses" : "reg", 100bc7b9300SIan Lepore cell, sizeof(cell)); 101bc7b9300SIan Lepore if (res == -1) 102bc7b9300SIan Lepore return (ENXIO); 103bc7b9300SIan Lepore if (res % sizeof(cell[0])) 104bc7b9300SIan Lepore return (ENXIO); 105bc7b9300SIan Lepore res /= sizeof(cell[0]); 106bc7b9300SIan Lepore regno *= naddr + nsize; 107bc7b9300SIan Lepore if (regno + naddr + nsize > res) 108bc7b9300SIan Lepore return (EINVAL); 109bc7b9300SIan Lepore pci_hi = pci ? cell[regno] : OFW_PADDR_NOT_PCI; 110bc7b9300SIan Lepore spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK; 111bc7b9300SIan Lepore addr = 0; 112bc7b9300SIan Lepore for (c = 0; c < naddr; c++) 113bc7b9300SIan Lepore addr = ((uint64_t)addr << 32) | cell[regno++]; 114bc7b9300SIan Lepore size = 0; 115bc7b9300SIan Lepore for (c = 0; c < nsize; c++) 116bc7b9300SIan Lepore size = ((uint64_t)size << 32) | cell[regno++]; 117bc7b9300SIan Lepore /* 118bc7b9300SIan Lepore * Map the address range in the bridge's decoding window as given 119bc7b9300SIan Lepore * by the "ranges" property. If a node doesn't have such property 120bc7b9300SIan Lepore * or the property is empty, we assume an identity mapping. The 121bc7b9300SIan Lepore * standard says a missing property indicates no possible mapping. 122bc7b9300SIan Lepore * This code is more liberal since the intended use is to get a 123bc7b9300SIan Lepore * console running early, and a printf to warn of malformed data 124bc7b9300SIan Lepore * is probably futile before the console is fully set up. 125bc7b9300SIan Lepore */ 126bc7b9300SIan Lepore parent = OF_parent(bridge); 127bc7b9300SIan Lepore while (parent != 0) { 128bc7b9300SIan Lepore get_addr_props(parent, &nbridge, NULL, &pcib); 129bc7b9300SIan Lepore res = OF_getencprop(bridge, "ranges", cell, sizeof(cell)); 130bc7b9300SIan Lepore if (res < 1) 131bc7b9300SIan Lepore goto next; 132bc7b9300SIan Lepore if (res % sizeof(cell[0])) 133bc7b9300SIan Lepore return (ENXIO); 134bc7b9300SIan Lepore /* Capture pci_hi if we just transitioned onto a PCI bus. */ 135bc7b9300SIan Lepore if (pcib && pci_hi == OFW_PADDR_NOT_PCI) { 136bc7b9300SIan Lepore pci_hi = cell[0]; 137bc7b9300SIan Lepore spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK; 138bc7b9300SIan Lepore } 139bc7b9300SIan Lepore res /= sizeof(cell[0]); 140bc7b9300SIan Lepore regno = 0; 141bc7b9300SIan Lepore while (regno < res) { 14267bcc941SIan Lepore rspc = (pci ? cell[regno] : OFW_PADDR_NOT_PCI) & 14367bcc941SIan Lepore OFW_PCI_PHYS_HI_SPACEMASK; 144bc7b9300SIan Lepore if (rspc != spc) { 145bc7b9300SIan Lepore regno += naddr + nbridge + nsize; 146bc7b9300SIan Lepore continue; 147bc7b9300SIan Lepore } 148bc7b9300SIan Lepore raddr = 0; 149bc7b9300SIan Lepore for (c = 0; c < naddr; c++) 150bc7b9300SIan Lepore raddr = ((uint64_t)raddr << 32) | cell[regno++]; 151bc7b9300SIan Lepore rspc = (pcib) 152bc7b9300SIan Lepore ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK 153bc7b9300SIan Lepore : OFW_PADDR_NOT_PCI; 154bc7b9300SIan Lepore baddr = 0; 155bc7b9300SIan Lepore for (c = 0; c < nbridge; c++) 156bc7b9300SIan Lepore baddr = ((uint64_t)baddr << 32) | cell[regno++]; 157bc7b9300SIan Lepore rsize = 0; 158bc7b9300SIan Lepore for (c = 0; c < nsize; c++) 159bc7b9300SIan Lepore rsize = ((uint64_t)rsize << 32) | cell[regno++]; 160bc7b9300SIan Lepore if (addr < raddr || addr >= raddr + rsize) 161bc7b9300SIan Lepore continue; 162bc7b9300SIan Lepore addr = addr - raddr + baddr; 163bc7b9300SIan Lepore if (rspc != OFW_PADDR_NOT_PCI) 164bc7b9300SIan Lepore spc = rspc; 165bc7b9300SIan Lepore } 166bc7b9300SIan Lepore next: 167bc7b9300SIan Lepore bridge = parent; 168bc7b9300SIan Lepore parent = OF_parent(bridge); 169bc7b9300SIan Lepore get_addr_props(bridge, &naddr, &nsize, &pci); 170bc7b9300SIan Lepore } 171bc7b9300SIan Lepore 172b958a08eSAndrew Turner KASSERT(addr <= BUS_SPACE_MAXADDR, 1737900c60aSAndrew Turner ("Bus sddress is too large: %jx", (uintmax_t)addr)); 174b958a08eSAndrew Turner KASSERT(size <= BUS_SPACE_MAXSIZE, 1757900c60aSAndrew Turner ("Bus size is too large: %jx", (uintmax_t)size)); 176b958a08eSAndrew Turner 177bc7b9300SIan Lepore *paddr = addr; 178bc7b9300SIan Lepore *psize = size; 179bc7b9300SIan Lepore if (ppci_hi != NULL) 180bc7b9300SIan Lepore *ppci_hi = pci_hi; 181bc7b9300SIan Lepore 182bc7b9300SIan Lepore return (0); 183bc7b9300SIan Lepore } 184