1 /* $NetBSD: fdt_subr.c,v 1.5 2015/12/22 21:42:11 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: fdt_subr.c,v 1.5 2015/12/22 21:42:11 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/kmem.h> 35 36 #include <libfdt.h> 37 #include <dev/fdt/fdtvar.h> 38 39 static const void *fdt_data; 40 41 bool 42 fdtbus_set_data(const void *data) 43 { 44 KASSERT(fdt_data == NULL); 45 if (fdt_check_header(data) != 0) { 46 return false; 47 } 48 fdt_data = data; 49 return true; 50 } 51 52 const void * 53 fdtbus_get_data(void) 54 { 55 return fdt_data; 56 } 57 58 int 59 fdtbus_offset2phandle(int offset) 60 { 61 if (offset < 0) 62 return 0; 63 64 return offset + fdt_off_dt_struct(fdt_data); 65 } 66 67 int 68 fdtbus_phandle2offset(int phandle) 69 { 70 const int dtoff = fdt_off_dt_struct(fdt_data); 71 72 if (phandle == -1) 73 phandle = dtoff; 74 75 if (phandle < dtoff) 76 return -1; 77 78 return phandle - dtoff; 79 } 80 81 82 static int 83 fdtbus_get_addr_cells(int phandle) 84 { 85 uint32_t addr_cells; 86 87 const int parent = OF_parent(phandle); 88 if (parent == -1) 89 return -1; 90 91 if (of_getprop_uint32(parent, "#address-cells", &addr_cells)) 92 addr_cells = 2; 93 94 return addr_cells; 95 } 96 97 static int 98 fdtbus_get_size_cells(int phandle) 99 { 100 uint32_t size_cells; 101 102 const int parent = OF_parent(phandle); 103 if (parent == -1) 104 return -1; 105 106 if (of_getprop_uint32(parent, "#size-cells", &size_cells)) 107 size_cells = 0; 108 109 return size_cells; 110 } 111 112 int 113 fdtbus_get_phandle(int phandle, const char *prop) 114 { 115 u_int phandle_ref; 116 u_int *buf; 117 int len; 118 119 len = OF_getproplen(phandle, prop); 120 if (len < sizeof(phandle_ref)) 121 return -1; 122 123 buf = kmem_alloc(len, KM_SLEEP); 124 125 if (OF_getprop(phandle, prop, buf, len) != len) { 126 kmem_free(buf, len); 127 return -1; 128 } 129 130 phandle_ref = fdt32_to_cpu(buf[0]); 131 kmem_free(buf, len); 132 133 return fdtbus_get_phandle_from_native(phandle_ref); 134 } 135 136 int 137 fdtbus_get_phandle_from_native(int phandle) 138 { 139 const int off = fdt_node_offset_by_phandle(fdt_data, phandle); 140 if (off < 0) { 141 return -1; 142 } 143 return fdtbus_offset2phandle(off); 144 } 145 146 int 147 fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize) 148 { 149 bus_addr_t addr; 150 bus_size_t size; 151 uint8_t *buf; 152 int len; 153 154 const int addr_cells = fdtbus_get_addr_cells(phandle); 155 const int size_cells = fdtbus_get_size_cells(phandle); 156 if (addr_cells == -1 || size_cells == -1) 157 return -1; 158 159 const u_int reglen = size_cells * 4 + addr_cells * 4; 160 if (reglen == 0) 161 return -1; 162 163 len = OF_getproplen(phandle, "reg"); 164 if (len <= 0) 165 return -1; 166 167 const u_int nregs = len / reglen; 168 169 if (index >= nregs) 170 return -1; 171 172 buf = kmem_alloc(len, KM_SLEEP); 173 if (buf == NULL) 174 return -1; 175 176 len = OF_getprop(phandle, "reg", buf, len); 177 178 switch (addr_cells) { 179 case 0: 180 addr = 0; 181 break; 182 case 1: 183 addr = be32dec(&buf[index * reglen + 0]); 184 break; 185 case 2: 186 addr = be64dec(&buf[index * reglen + 0]); 187 break; 188 default: 189 panic("fdtbus_get_reg: unsupported addr_cells %d", addr_cells); 190 } 191 192 switch (size_cells) { 193 case 0: 194 size = 0; 195 break; 196 case 1: 197 size = be32dec(&buf[index * reglen + addr_cells * 4]); 198 break; 199 case 2: 200 size = be64dec(&buf[index * reglen + addr_cells * 4]); 201 break; 202 default: 203 panic("fdtbus_get_reg: unsupported size_cells %d", size_cells); 204 } 205 206 if (paddr) 207 *paddr = addr; 208 if (psize) 209 *psize = size; 210 211 kmem_free(buf, len); 212 213 return 0; 214 } 215