Home | History | Annotate | Line # | Download | only in fdt
fdt_subr.c revision 1.12
      1 /* $NetBSD: fdt_subr.c,v 1.12 2017/05/29 23:13:03 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.12 2017/05/29 23:13:03 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 static struct fdt_conslist fdt_console_list =
     42     TAILQ_HEAD_INITIALIZER(fdt_console_list);
     43 
     44 bool
     45 fdtbus_set_data(const void *data)
     46 {
     47 	KASSERT(fdt_data == NULL);
     48 	if (fdt_check_header(data) != 0) {
     49 		return false;
     50 	}
     51 	fdt_data = data;
     52 	return true;
     53 }
     54 
     55 const void *
     56 fdtbus_get_data(void)
     57 {
     58 	return fdt_data;
     59 }
     60 
     61 int
     62 fdtbus_offset2phandle(int offset)
     63 {
     64 	if (offset < 0)
     65 		return 0;
     66 
     67 	return offset + fdt_off_dt_struct(fdt_data);
     68 }
     69 
     70 int
     71 fdtbus_phandle2offset(int phandle)
     72 {
     73 	const int dtoff = fdt_off_dt_struct(fdt_data);
     74 
     75 	if (phandle == -1)
     76 		phandle = dtoff;
     77 
     78 	if (phandle < dtoff)
     79 		return -1;
     80 
     81 	return phandle - dtoff;
     82 }
     83 
     84 
     85 static int
     86 fdtbus_get_addr_cells(int phandle)
     87 {
     88 	uint32_t addr_cells;
     89 
     90 	const int parent = OF_parent(phandle);
     91 	if (parent == -1)
     92 		return -1;
     93 
     94 	if (of_getprop_uint32(parent, "#address-cells", &addr_cells))
     95 		addr_cells = 2;
     96 
     97 	return addr_cells;
     98 }
     99 
    100 static int
    101 fdtbus_get_size_cells(int phandle)
    102 {
    103 	uint32_t size_cells;
    104 
    105 	const int parent = OF_parent(phandle);
    106 	if (parent == -1)
    107 		return -1;
    108 
    109 	if (of_getprop_uint32(parent, "#size-cells", &size_cells))
    110 		size_cells = 0;
    111 
    112 	return size_cells;
    113 }
    114 
    115 int
    116 fdtbus_get_phandle(int phandle, const char *prop)
    117 {
    118 	u_int phandle_ref;
    119 	u_int *buf;
    120 	int len;
    121 
    122 	len = OF_getproplen(phandle, prop);
    123 	if (len < sizeof(phandle_ref))
    124 		return -1;
    125 
    126 	buf = kmem_alloc(len, KM_SLEEP);
    127 
    128 	if (OF_getprop(phandle, prop, buf, len) != len) {
    129 		kmem_free(buf, len);
    130 		return -1;
    131 	}
    132 
    133 	phandle_ref = fdt32_to_cpu(buf[0]);
    134 	kmem_free(buf, len);
    135 
    136 	return fdtbus_get_phandle_from_native(phandle_ref);
    137 }
    138 
    139 int
    140 fdtbus_get_phandle_from_native(int phandle)
    141 {
    142 	const int off = fdt_node_offset_by_phandle(fdt_data, phandle);
    143 	if (off < 0) {
    144 		return -1;
    145 	}
    146 	return fdtbus_offset2phandle(off);
    147 }
    148 
    149 bool
    150 fdtbus_get_path(int phandle, char *buf, size_t buflen)
    151 {
    152 	const int off = fdtbus_phandle2offset(phandle);
    153 	if (off < 0) {
    154 		return false;
    155 	}
    156 	if (fdt_get_path(fdt_data, off, buf, (int)buflen) != 0) {
    157 		return false;
    158 	}
    159 	return true;
    160 }
    161 
    162 int
    163 fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize)
    164 {
    165 	uint64_t addr, size;
    166 	int error;
    167 
    168 	error = fdtbus_get_reg64(phandle, index, &addr, &size);
    169 	if (error)
    170 		return error;
    171 
    172 	if (sizeof(bus_addr_t) == 4 && (addr + size) > 0x100000000)
    173 		return ERANGE;
    174 
    175 	if (paddr)
    176 		*paddr = (bus_addr_t)addr;
    177 	if (psize)
    178 		*psize = (bus_size_t)size;
    179 
    180 	return 0;
    181 }
    182 
    183 int
    184 fdtbus_get_reg64(int phandle, u_int index, uint64_t *paddr, uint64_t *psize)
    185 {
    186 	uint64_t addr, size;
    187 	const uint8_t *buf;
    188 	int len;
    189 
    190 	const int addr_cells = fdtbus_get_addr_cells(phandle);
    191 	const int size_cells = fdtbus_get_size_cells(phandle);
    192 	if (addr_cells == -1 || size_cells == -1)
    193 		return EINVAL;
    194 
    195 	buf = fdt_getprop(fdtbus_get_data(),
    196 	    fdtbus_phandle2offset(phandle), "reg", &len);
    197 	if (buf == NULL || len <= 0)
    198 		return EINVAL;
    199 
    200 	const u_int reglen = size_cells * 4 + addr_cells * 4;
    201 	if (reglen == 0)
    202 		return EINVAL;
    203 
    204 	if (index >= len / reglen)
    205 		return ENXIO;
    206 
    207 	switch (addr_cells) {
    208 	case 0:
    209 		addr = 0;
    210 		break;
    211 	case 1:
    212 		addr = be32dec(&buf[index * reglen + 0]);
    213 		break;
    214 	case 2:
    215 		addr = be64dec(&buf[index * reglen + 0]);
    216 		break;
    217 	default:
    218 		panic("fdtbus_get_reg: unsupported addr_cells %d", addr_cells);
    219 	}
    220 
    221 	switch (size_cells) {
    222 	case 0:
    223 		size = 0;
    224 		break;
    225 	case 1:
    226 		size = be32dec(&buf[index * reglen + addr_cells * 4]);
    227 		break;
    228 	case 2:
    229 		size = be64dec(&buf[index * reglen + addr_cells * 4]);
    230 		break;
    231 	default:
    232 		panic("fdtbus_get_reg: unsupported size_cells %d", size_cells);
    233 	}
    234 
    235 	if (paddr)
    236 		*paddr = addr;
    237 	if (psize)
    238 		*psize = size;
    239 
    240 	return 0;
    241 }
    242 
    243 const struct fdt_console *
    244 fdtbus_get_console(void)
    245 {
    246 	static const struct fdt_console_info *booted_console = NULL;
    247 
    248 	if (booted_console == NULL) {
    249 		__link_set_decl(fdt_consoles, struct fdt_console_info);
    250 		struct fdt_console_info * const *info;
    251 		const struct fdt_console_info *best_info = NULL;
    252 		const int phandle = fdtbus_get_stdout_phandle();
    253 		int best_match = 0;
    254 
    255 		__link_set_foreach(info, fdt_consoles) {
    256 			const int match = (*info)->ops->match(phandle);
    257 			if (match > best_match) {
    258 				best_match = match;
    259 				best_info = *info;
    260 			}
    261 		}
    262 
    263 		booted_console = best_info;
    264 	}
    265 
    266 	return booted_console == NULL ? NULL : booted_console->ops;
    267 }
    268 
    269 const char *
    270 fdtbus_get_stdout_path(void)
    271 {
    272 	const char *prop;
    273 
    274 	const int off = fdt_path_offset(fdtbus_get_data(), "/chosen");
    275 	if (off < 0)
    276 		return NULL;
    277 
    278 	prop = fdt_getprop(fdtbus_get_data(), off, "stdout-path", NULL);
    279 	if (prop != NULL)
    280 		return prop;
    281 
    282 	/* If the stdout-path property is not found, assume serial0 */
    283 	return "serial0:115200n8";
    284 }
    285 
    286 int
    287 fdtbus_get_stdout_phandle(void)
    288 {
    289 	const char *prop, *p;
    290 	int off, len;
    291 
    292 	prop = fdtbus_get_stdout_path();
    293 	if (prop == NULL)
    294 		return -1;
    295 
    296 	p = strchr(prop, ':');
    297 	len = p == NULL ? strlen(prop) : (p - prop);
    298 	if (*prop != '/') {
    299 		/* Alias */
    300 		prop = fdt_get_alias_namelen(fdtbus_get_data(), prop, len);
    301 		if (prop == NULL)
    302 			return -1;
    303 		len = strlen(prop);
    304 	}
    305 	off = fdt_path_offset_namelen(fdtbus_get_data(), prop, len);
    306 	if (off < 0)
    307 		return -1;
    308 
    309 	return fdtbus_offset2phandle(off);
    310 }
    311 
    312 int
    313 fdtbus_get_stdout_speed(void)
    314 {
    315 	const char *prop, *p;
    316 
    317 	prop = fdtbus_get_stdout_path();
    318 	if (prop == NULL)
    319 		return -1;
    320 
    321 	p = strchr(prop, ':');
    322 	if (p == NULL)
    323 		return -1;
    324 
    325 	return (int)strtoul(p + 1, NULL, 10);
    326 }
    327 
    328 tcflag_t
    329 fdtbus_get_stdout_flags(void)
    330 {
    331 	const char *prop, *p;
    332 	tcflag_t flags = TTYDEF_CFLAG;
    333 	char *ep;
    334 
    335 	prop = fdtbus_get_stdout_path();
    336 	if (prop == NULL)
    337 		return flags;
    338 
    339 	p = strchr(prop, ':');
    340 	if (p == NULL)
    341 		return flags;
    342 
    343 	ep = NULL;
    344 	(void)strtoul(p + 1, &ep, 10);
    345 	if (ep == NULL)
    346 		return flags;
    347 
    348 	/* <baud>{<parity>{<bits>{<flow>}}} */
    349 	while (*ep) {
    350 		switch (*ep) {
    351 		/* parity */
    352 		case 'n':	flags &= ~(PARENB|PARODD); break;
    353 		case 'e':	flags &= ~PARODD; flags |= PARENB; break;
    354 		case 'o':	flags |= (PARENB|PARODD); break;
    355 		/* bits */
    356 		case '5':	flags &= ~CSIZE; flags |= CS5; break;
    357 		case '6':	flags &= ~CSIZE; flags |= CS6; break;
    358 		case '7':	flags &= ~CSIZE; flags |= CS7; break;
    359 		case '8':	flags &= ~CSIZE; flags |= CS8; break;
    360 		/* flow */
    361 		case 'r':	flags |= CRTSCTS; break;
    362 		}
    363 		ep++;
    364 	}
    365 
    366 	return flags;
    367 }
    368 
    369 bool
    370 fdtbus_status_okay(int phandle)
    371 {
    372 	const int off = fdtbus_phandle2offset(phandle);
    373 
    374 	const char *prop = fdt_getprop(fdtbus_get_data(), off, "status", NULL);
    375 	if (prop == NULL)
    376 		return true;
    377 
    378 	return strncmp(prop, "ok", 2) == 0;
    379 }
    380