Home | History | Annotate | Line # | Download | only in fdt
fdt_subr.c revision 1.25
      1 /* $NetBSD: fdt_subr.c,v 1.25 2019/01/27 02:08:41 pgoyette 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.25 2019/01/27 02:08:41 pgoyette Exp $");
     31 
     32 #include "opt_fdt.h"
     33 
     34 #include "opt_fdt.h"
     35 
     36 #include <sys/param.h>
     37 #include <sys/bus.h>
     38 
     39 #include <libfdt.h>
     40 #include <dev/fdt/fdtvar.h>
     41 
     42 static const void *fdt_data;
     43 
     44 static struct fdt_conslist fdt_console_list =
     45     TAILQ_HEAD_INITIALIZER(fdt_console_list);
     46 
     47 bool
     48 fdtbus_set_data(const void *data)
     49 {
     50 	KASSERT(fdt_data == NULL);
     51 	if (fdt_check_header(data) != 0) {
     52 		return false;
     53 	}
     54 	fdt_data = data;
     55 	return true;
     56 }
     57 
     58 const void *
     59 fdtbus_get_data(void)
     60 {
     61 	return fdt_data;
     62 }
     63 
     64 int
     65 fdtbus_offset2phandle(int offset)
     66 {
     67 	if (offset < 0)
     68 		return 0;
     69 
     70 	return offset + fdt_off_dt_struct(fdt_data);
     71 }
     72 
     73 int
     74 fdtbus_phandle2offset(int phandle)
     75 {
     76 	const int dtoff = fdt_off_dt_struct(fdt_data);
     77 
     78 	if (phandle == -1)
     79 		phandle = dtoff;
     80 
     81 	if (phandle < dtoff)
     82 		return -1;
     83 
     84 	return phandle - dtoff;
     85 }
     86 
     87 static bool fdtbus_decoderegprop = true;
     88 
     89 void
     90 fdtbus_set_decoderegprop(bool decode)
     91 {
     92 	fdtbus_decoderegprop = decode;
     93 }
     94 
     95 static int
     96 fdtbus_get_addr_cells(int phandle)
     97 {
     98 	uint32_t addr_cells;
     99 
    100 	if (of_getprop_uint32(phandle, "#address-cells", &addr_cells))
    101 		addr_cells = 2;
    102 
    103 	return addr_cells;
    104 }
    105 
    106 static int
    107 fdtbus_get_size_cells(int phandle)
    108 {
    109 	uint32_t size_cells;
    110 
    111 	if (of_getprop_uint32(phandle, "#size-cells", &size_cells))
    112 		size_cells = 0;
    113 
    114 	return size_cells;
    115 }
    116 
    117 int
    118 fdtbus_get_phandle(int phandle, const char *prop)
    119 {
    120 	u_int phandle_ref;
    121 	const u_int *buf;
    122 	int len;
    123 
    124 	buf = fdt_getprop(fdtbus_get_data(),
    125 	    fdtbus_phandle2offset(phandle), prop, &len);
    126 	if (buf == NULL || len < sizeof(phandle_ref))
    127 		return -1;
    128 
    129 	phandle_ref = be32dec(buf);
    130 
    131 	return fdtbus_get_phandle_from_native(phandle_ref);
    132 }
    133 
    134 int
    135 fdtbus_get_phandle_from_native(int phandle)
    136 {
    137 	const int off = fdt_node_offset_by_phandle(fdt_data, phandle);
    138 	if (off < 0) {
    139 		return -1;
    140 	}
    141 	return fdtbus_offset2phandle(off);
    142 }
    143 
    144 bool
    145 fdtbus_get_path(int phandle, char *buf, size_t buflen)
    146 {
    147 	const int off = fdtbus_phandle2offset(phandle);
    148 	if (off < 0) {
    149 		return false;
    150 	}
    151 	if (fdt_get_path(fdt_data, off, buf, (int)buflen) != 0) {
    152 		return false;
    153 	}
    154 	return true;
    155 }
    156 
    157 static uint64_t
    158 fdtbus_get_cells(const uint8_t *buf, int cells)
    159 {
    160 	switch (cells) {
    161 	case 0:		return 0;
    162 	case 1: 	return be32dec(buf);
    163 	case 2:		return ((uint64_t)be32dec(buf)<<32)|be32dec(buf+4);
    164 	default:	panic("fdtbus_get_cells: bad cells val %d\n", cells);
    165 	}
    166 }
    167 
    168 static uint64_t
    169 fdtbus_decode_range(int phandle, uint64_t paddr)
    170 {
    171 	const int parent = OF_parent(phandle);
    172 	if (parent == -1)
    173 		return paddr;
    174 
    175 	if (!fdtbus_decoderegprop)
    176 		return paddr;
    177 
    178 	const uint8_t *buf;
    179 	int len;
    180 
    181 	buf = fdt_getprop(fdtbus_get_data(),
    182 	    fdtbus_phandle2offset(phandle), "ranges", &len);
    183 	if (buf == NULL)
    184 		return paddr;
    185 
    186 	if (len == 0) {
    187 		/* pass through to parent */
    188 		return fdtbus_decode_range(parent, paddr);
    189 	}
    190 
    191 	const int addr_cells = fdtbus_get_addr_cells(phandle);
    192 	const int size_cells = fdtbus_get_size_cells(phandle);
    193 	const int paddr_cells = fdtbus_get_addr_cells(OF_parent(parent));
    194 	if (addr_cells == -1 || size_cells == -1 || paddr_cells == -1)
    195 		return paddr;
    196 
    197 	while (len > 0) {
    198 		uint64_t cba, pba, cl;
    199 		cba = fdtbus_get_cells(buf, addr_cells);
    200 		buf += addr_cells * 4;
    201 		pba = fdtbus_get_cells(buf, paddr_cells);
    202 		buf += paddr_cells * 4;
    203 		cl = fdtbus_get_cells(buf, size_cells);
    204 		buf += size_cells * 4;
    205 
    206 		if (paddr >= cba && paddr < cba + cl)
    207 			return fdtbus_decode_range(parent, pba) + (paddr - cba);
    208 
    209 		len -= (addr_cells + paddr_cells + size_cells) * 4;
    210 	}
    211 
    212 	/* No mapping found */
    213 	return paddr;
    214 }
    215 
    216 int
    217 fdtbus_get_reg_byname(int phandle, const char *name, bus_addr_t *paddr,
    218     bus_size_t *psize)
    219 {
    220 	const char *reg_names, *p;
    221 	u_int index;
    222 	int len, resid;
    223 	int error = ENOENT;
    224 
    225 	reg_names = fdtbus_get_prop(phandle, "reg-names", &len);
    226 	if (len <= 0)
    227 		return error;
    228 
    229 	p = reg_names;
    230 	for (index = 0, resid = len; resid > 0; index++) {
    231 		if (strcmp(p, name) == 0) {
    232 			error = fdtbus_get_reg(phandle, index, paddr, psize);
    233 			break;
    234 		}
    235 		resid -= strlen(p);
    236 		p += strlen(p) + 1;
    237 	}
    238 
    239 	return error;
    240 }
    241 
    242 int
    243 fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize)
    244 {
    245 	uint64_t addr, size;
    246 	int error;
    247 
    248 	error = fdtbus_get_reg64(phandle, index, &addr, &size);
    249 	if (error)
    250 		return error;
    251 
    252 	if (sizeof(bus_addr_t) == 4 && (addr + size) > 0x100000000)
    253 		return ERANGE;
    254 
    255 	if (paddr)
    256 		*paddr = (bus_addr_t)addr;
    257 	if (psize)
    258 		*psize = (bus_size_t)size;
    259 
    260 	return 0;
    261 }
    262 
    263 int
    264 fdtbus_get_reg64(int phandle, u_int index, uint64_t *paddr, uint64_t *psize)
    265 {
    266 	uint64_t addr, size;
    267 	const uint8_t *buf;
    268 	int len;
    269 
    270 	const int addr_cells = fdtbus_get_addr_cells(OF_parent(phandle));
    271 	const int size_cells = fdtbus_get_size_cells(OF_parent(phandle));
    272 	if (addr_cells == -1 || size_cells == -1)
    273 		return EINVAL;
    274 
    275 	buf = fdt_getprop(fdtbus_get_data(),
    276 	    fdtbus_phandle2offset(phandle), "reg", &len);
    277 	if (buf == NULL || len <= 0)
    278 		return EINVAL;
    279 
    280 	const u_int reglen = size_cells * 4 + addr_cells * 4;
    281 	if (reglen == 0)
    282 		return EINVAL;
    283 
    284 	if (index >= len / reglen)
    285 		return ENXIO;
    286 
    287 	buf += index * reglen;
    288 	addr = fdtbus_get_cells(buf, addr_cells);
    289 	buf += addr_cells * 4;
    290 	size = fdtbus_get_cells(buf, size_cells);
    291 
    292 	if (paddr) {
    293 		*paddr = fdtbus_decode_range(OF_parent(phandle), addr);
    294 		const char *name = fdt_get_name(fdtbus_get_data(),
    295 		    fdtbus_phandle2offset(phandle), NULL);
    296 		aprint_debug("fdt: [%s] decoded addr #%u: %" PRIx64
    297 		    " -> %" PRIx64 "\n", name, index, addr, *paddr);
    298 	}
    299 	if (psize)
    300 		*psize = size;
    301 
    302 	return 0;
    303 }
    304 
    305 #if defined(FDT)
    306 const struct fdt_console *
    307 fdtbus_get_console(void)
    308 {
    309 	static const struct fdt_console_info *booted_console = NULL;
    310 
    311 	if (booted_console == NULL) {
    312 		__link_set_decl(fdt_consoles, struct fdt_console_info);
    313 		struct fdt_console_info * const *info;
    314 		const struct fdt_console_info *best_info = NULL;
    315 		const int phandle = fdtbus_get_stdout_phandle();
    316 		int best_match = 0;
    317 
    318 		__link_set_foreach(info, fdt_consoles) {
    319 			const int match = (*info)->ops->match(phandle);
    320 			if (match > best_match) {
    321 				best_match = match;
    322 				best_info = *info;
    323 			}
    324 		}
    325 
    326 		booted_console = best_info;
    327 	}
    328 
    329 	return booted_console == NULL ? NULL : booted_console->ops;
    330 }
    331 #endif
    332 
    333 const char *
    334 fdtbus_get_stdout_path(void)
    335 {
    336 	const char *prop;
    337 
    338 	const int off = fdt_path_offset(fdtbus_get_data(), "/chosen");
    339 	if (off < 0)
    340 		return NULL;
    341 
    342 	prop = fdt_getprop(fdtbus_get_data(), off, "stdout-path", NULL);
    343 	if (prop != NULL)
    344 		return prop;
    345 
    346 	/* If the stdout-path property is not found, assume serial0 */
    347 	return "serial0:115200n8";
    348 }
    349 
    350 int
    351 fdtbus_get_stdout_phandle(void)
    352 {
    353 	const char *prop, *p;
    354 	int off, len;
    355 
    356 	prop = fdtbus_get_stdout_path();
    357 	if (prop == NULL)
    358 		return -1;
    359 
    360 	p = strchr(prop, ':');
    361 	len = p == NULL ? strlen(prop) : (p - prop);
    362 	if (*prop != '/') {
    363 		/* Alias */
    364 		prop = fdt_get_alias_namelen(fdtbus_get_data(), prop, len);
    365 		if (prop == NULL)
    366 			return -1;
    367 		len = strlen(prop);
    368 	}
    369 	off = fdt_path_offset_namelen(fdtbus_get_data(), prop, len);
    370 	if (off < 0)
    371 		return -1;
    372 
    373 	return fdtbus_offset2phandle(off);
    374 }
    375 
    376 int
    377 fdtbus_get_stdout_speed(void)
    378 {
    379 	const char *prop, *p;
    380 
    381 	prop = fdtbus_get_stdout_path();
    382 	if (prop == NULL)
    383 		return -1;
    384 
    385 	p = strchr(prop, ':');
    386 	if (p == NULL)
    387 		return -1;
    388 
    389 	return (int)strtoul(p + 1, NULL, 10);
    390 }
    391 
    392 tcflag_t
    393 fdtbus_get_stdout_flags(void)
    394 {
    395 	const char *prop, *p;
    396 	tcflag_t flags = TTYDEF_CFLAG;
    397 	char *ep;
    398 
    399 	prop = fdtbus_get_stdout_path();
    400 	if (prop == NULL)
    401 		return flags;
    402 
    403 	p = strchr(prop, ':');
    404 	if (p == NULL)
    405 		return flags;
    406 
    407 	ep = NULL;
    408 	(void)strtoul(p + 1, &ep, 10);
    409 	if (ep == NULL)
    410 		return flags;
    411 
    412 	/* <baud>{<parity>{<bits>{<flow>}}} */
    413 	while (*ep) {
    414 		switch (*ep) {
    415 		/* parity */
    416 		case 'n':	flags &= ~(PARENB|PARODD); break;
    417 		case 'e':	flags &= ~PARODD; flags |= PARENB; break;
    418 		case 'o':	flags |= (PARENB|PARODD); break;
    419 		/* bits */
    420 		case '5':	flags &= ~CSIZE; flags |= CS5; break;
    421 		case '6':	flags &= ~CSIZE; flags |= CS6; break;
    422 		case '7':	flags &= ~CSIZE; flags |= CS7; break;
    423 		case '8':	flags &= ~CSIZE; flags |= CS8; break;
    424 		/* flow */
    425 		case 'r':	flags |= CRTSCTS; break;
    426 		}
    427 		ep++;
    428 	}
    429 
    430 	return flags;
    431 }
    432 
    433 bool
    434 fdtbus_status_okay(int phandle)
    435 {
    436 	const int off = fdtbus_phandle2offset(phandle);
    437 
    438 	const char *prop = fdt_getprop(fdtbus_get_data(), off, "status", NULL);
    439 	if (prop == NULL)
    440 		return true;
    441 
    442 	return strncmp(prop, "ok", 2) == 0;
    443 }
    444 
    445 const void *
    446 fdtbus_get_prop(int phandle, const char *prop, int *plen)
    447 {
    448 	const int off = fdtbus_phandle2offset(phandle);
    449 
    450 	return fdt_getprop(fdtbus_get_data(), off, prop, plen);
    451 }
    452 
    453 const char *
    454 fdtbus_get_string(int phandle, const char *prop)
    455 {
    456 	const int off = fdtbus_phandle2offset(phandle);
    457 
    458 	if (strcmp(prop, "name") == 0)
    459 		return fdt_get_name(fdtbus_get_data(), off, NULL);
    460 	else
    461 		return fdt_getprop(fdtbus_get_data(), off, prop, NULL);
    462 }
    463 
    464 const char *
    465 fdtbus_get_string_index(int phandle, const char *prop, u_int index)
    466 {
    467 	const char *names, *name;
    468 	int len, cur;
    469 
    470 	if ((len = OF_getproplen(phandle, prop)) < 0)
    471 		return NULL;
    472 
    473 	names = fdtbus_get_string(phandle, prop);
    474 
    475 	for (name = names, cur = 0; len > 0;
    476 	     len -= strlen(name) + 1, name += strlen(name) + 1, cur++) {
    477 		if (index == cur)
    478 			return name;
    479 	}
    480 
    481 	return NULL;
    482 }
    483