Home | History | Annotate | Line # | Download | only in fdt
fdt_subr.c revision 1.8
      1 /* $NetBSD: fdt_subr.c,v 1.8 2017/04/21 23:35:01 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) 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.8 2017/04/21 23:35:01 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 bool
    147 fdtbus_get_path(int phandle, char *buf, size_t buflen)
    148 {
    149 	const int off = fdtbus_phandle2offset(phandle);
    150 	if (off < 0) {
    151 		return false;
    152 	}
    153 	if (fdt_get_path(fdt_data, off, buf, (int)buflen) != 0) {
    154 		return false;
    155 	}
    156 	return true;
    157 }
    158 
    159 int
    160 fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize)
    161 {
    162 	bus_addr_t addr;
    163 	bus_size_t size;
    164 	const uint8_t *buf;
    165 	int len;
    166 
    167 	const int addr_cells = fdtbus_get_addr_cells(phandle);
    168 	const int size_cells = fdtbus_get_size_cells(phandle);
    169 	if (addr_cells == -1 || size_cells == -1)
    170 		return -1;
    171 
    172 	buf = fdt_getprop(fdtbus_get_data(),
    173 	    fdtbus_phandle2offset(phandle), "reg", &len);
    174 	if (buf == NULL || len <= 0)
    175 		return -1;
    176 
    177 	const u_int reglen = size_cells * 4 + addr_cells * 4;
    178 	if (reglen == 0)
    179 		return -1;
    180 
    181 	if (index >= len / reglen)
    182 		return -1;
    183 
    184 	switch (addr_cells) {
    185 	case 0:
    186 		addr = 0;
    187 		break;
    188 	case 1:
    189 		addr = be32dec(&buf[index * reglen + 0]);
    190 		break;
    191 	case 2:
    192 		addr = be64dec(&buf[index * reglen + 0]);
    193 		break;
    194 	default:
    195 		panic("fdtbus_get_reg: unsupported addr_cells %d", addr_cells);
    196 	}
    197 
    198 	switch (size_cells) {
    199 	case 0:
    200 		size = 0;
    201 		break;
    202 	case 1:
    203 		size = be32dec(&buf[index * reglen + addr_cells * 4]);
    204 		break;
    205 	case 2:
    206 		size = be64dec(&buf[index * reglen + addr_cells * 4]);
    207 		break;
    208 	default:
    209 		panic("fdtbus_get_reg: unsupported size_cells %d", size_cells);
    210 	}
    211 
    212 	if (paddr)
    213 		*paddr = addr;
    214 	if (psize)
    215 		*psize = size;
    216 
    217 	return 0;
    218 }
    219 
    220 const char *
    221 fdtbus_get_stdout_path(void)
    222 {
    223 	const int off = fdt_path_offset(fdtbus_get_data(), "/chosen");
    224 	if (off < 0)
    225 		return NULL;
    226 
    227 	return fdt_getprop(fdtbus_get_data(), off, "stdout-path", NULL);
    228 }
    229 
    230 int
    231 fdtbus_get_stdout_phandle(void)
    232 {
    233 	const char *prop, *p;
    234 	int off, len;
    235 
    236 	prop = fdtbus_get_stdout_path();
    237 	if (prop == NULL)
    238 		return -1;
    239 
    240 	p = strchr(prop, ':');
    241 	len = p == NULL ? strlen(prop) : (p - prop);
    242 	if (*prop != '/') {
    243 		/* Alias */
    244 		prop = fdt_get_alias_namelen(fdtbus_get_data(), prop, len);
    245 		if (prop == NULL)
    246 			return -1;
    247 		len = strlen(prop);
    248 	}
    249 	off = fdt_path_offset_namelen(fdtbus_get_data(), prop, len);
    250 	if (off < 0)
    251 		return -1;
    252 
    253 	return fdtbus_offset2phandle(off);
    254 }
    255 
    256 int
    257 fdtbus_get_stdout_speed(void)
    258 {
    259 	const char *prop, *p;
    260 
    261 	prop = fdtbus_get_stdout_path();
    262 	if (prop == NULL)
    263 		return -1;
    264 
    265 	p = strchr(prop, ':');
    266 	if (p == NULL)
    267 		return -1;
    268 
    269 	return (int)strtoul(p + 1, NULL, 10);
    270 }
    271