Home | History | Annotate | Line # | Download | only in fdt
fdt_subr.c revision 1.33
      1  1.33   thorpej /* $NetBSD: fdt_subr.c,v 1.33 2020/02/16 20:28:18 thorpej Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include <sys/cdefs.h>
     30  1.33   thorpej __KERNEL_RCSID(0, "$NetBSD: fdt_subr.c,v 1.33 2020/02/16 20:28:18 thorpej Exp $");
     31  1.24     skrll 
     32  1.24     skrll #include "opt_fdt.h"
     33   1.1  jmcneill 
     34   1.1  jmcneill #include <sys/param.h>
     35   1.1  jmcneill #include <sys/bus.h>
     36   1.1  jmcneill 
     37   1.1  jmcneill #include <libfdt.h>
     38   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     39  1.33   thorpej #include <dev/fdt/fdt_private.h>
     40   1.1  jmcneill 
     41   1.3  jmcneill static const void *fdt_data;
     42   1.3  jmcneill 
     43  1.12  jmcneill static struct fdt_conslist fdt_console_list =
     44  1.12  jmcneill     TAILQ_HEAD_INITIALIZER(fdt_console_list);
     45  1.12  jmcneill 
     46   1.3  jmcneill bool
     47  1.32   thorpej fdtbus_init(const void *data)
     48   1.3  jmcneill {
     49   1.3  jmcneill 	KASSERT(fdt_data == NULL);
     50   1.3  jmcneill 	if (fdt_check_header(data) != 0) {
     51   1.3  jmcneill 		return false;
     52   1.3  jmcneill 	}
     53   1.3  jmcneill 	fdt_data = data;
     54  1.33   thorpej 
     55  1.33   thorpej 	/* Now that we have a FDT blob, initialize other bits that need it. */
     56  1.33   thorpej 	fdtbus_intr_init();
     57  1.33   thorpej 
     58   1.3  jmcneill 	return true;
     59   1.3  jmcneill }
     60   1.3  jmcneill 
     61   1.3  jmcneill const void *
     62   1.3  jmcneill fdtbus_get_data(void)
     63   1.3  jmcneill {
     64   1.3  jmcneill 	return fdt_data;
     65   1.3  jmcneill }
     66   1.3  jmcneill 
     67   1.3  jmcneill int
     68   1.3  jmcneill fdtbus_offset2phandle(int offset)
     69   1.3  jmcneill {
     70   1.3  jmcneill 	if (offset < 0)
     71   1.3  jmcneill 		return 0;
     72   1.3  jmcneill 
     73   1.3  jmcneill 	return offset + fdt_off_dt_struct(fdt_data);
     74   1.3  jmcneill }
     75   1.3  jmcneill 
     76   1.3  jmcneill int
     77   1.3  jmcneill fdtbus_phandle2offset(int phandle)
     78   1.3  jmcneill {
     79   1.3  jmcneill 	const int dtoff = fdt_off_dt_struct(fdt_data);
     80   1.3  jmcneill 
     81   1.3  jmcneill 	if (phandle == -1)
     82   1.3  jmcneill 		phandle = dtoff;
     83   1.3  jmcneill 
     84   1.3  jmcneill 	if (phandle < dtoff)
     85   1.3  jmcneill 		return -1;
     86   1.3  jmcneill 
     87   1.3  jmcneill 	return phandle - dtoff;
     88   1.3  jmcneill }
     89   1.3  jmcneill 
     90  1.20     skrll static bool fdtbus_decoderegprop = true;
     91  1.20     skrll 
     92  1.20     skrll void
     93  1.20     skrll fdtbus_set_decoderegprop(bool decode)
     94  1.20     skrll {
     95  1.20     skrll 	fdtbus_decoderegprop = decode;
     96  1.20     skrll }
     97   1.3  jmcneill 
     98   1.1  jmcneill static int
     99   1.1  jmcneill fdtbus_get_addr_cells(int phandle)
    100   1.1  jmcneill {
    101   1.4  jmcneill 	uint32_t addr_cells;
    102   1.1  jmcneill 
    103  1.13  jmcneill 	if (of_getprop_uint32(phandle, "#address-cells", &addr_cells))
    104   1.1  jmcneill 		addr_cells = 2;
    105   1.1  jmcneill 
    106   1.1  jmcneill 	return addr_cells;
    107   1.1  jmcneill }
    108   1.1  jmcneill 
    109   1.1  jmcneill static int
    110   1.1  jmcneill fdtbus_get_size_cells(int phandle)
    111   1.1  jmcneill {
    112   1.4  jmcneill 	uint32_t size_cells;
    113   1.1  jmcneill 
    114  1.13  jmcneill 	if (of_getprop_uint32(phandle, "#size-cells", &size_cells))
    115   1.1  jmcneill 		size_cells = 0;
    116   1.1  jmcneill 
    117   1.1  jmcneill 	return size_cells;
    118   1.1  jmcneill }
    119   1.1  jmcneill 
    120   1.1  jmcneill int
    121   1.1  jmcneill fdtbus_get_phandle(int phandle, const char *prop)
    122   1.1  jmcneill {
    123   1.1  jmcneill 	u_int phandle_ref;
    124  1.17  jmcneill 	const u_int *buf;
    125   1.1  jmcneill 	int len;
    126   1.1  jmcneill 
    127  1.17  jmcneill 	buf = fdt_getprop(fdtbus_get_data(),
    128  1.17  jmcneill 	    fdtbus_phandle2offset(phandle), prop, &len);
    129  1.17  jmcneill 	if (buf == NULL || len < sizeof(phandle_ref))
    130   1.1  jmcneill 		return -1;
    131   1.1  jmcneill 
    132  1.17  jmcneill 	phandle_ref = be32dec(buf);
    133   1.1  jmcneill 
    134   1.5  jmcneill 	return fdtbus_get_phandle_from_native(phandle_ref);
    135   1.5  jmcneill }
    136   1.5  jmcneill 
    137   1.5  jmcneill int
    138  1.30   hkenken fdtbus_get_phandle_with_data(int phandle, const char *prop, const char *cells,
    139  1.30   hkenken     int index, struct fdt_phandle_data *data)
    140  1.30   hkenken {
    141  1.30   hkenken 	int len;
    142  1.30   hkenken 	const int offset = 1;
    143  1.30   hkenken 
    144  1.30   hkenken 	const u_int *p = fdtbus_get_prop(phandle, prop, &len);
    145  1.30   hkenken 	if (p == NULL || len <= 0)
    146  1.30   hkenken 		return EINVAL;
    147  1.30   hkenken 
    148  1.30   hkenken 	for (int i = 0; len > 0; i++) {
    149  1.30   hkenken 		u_int phandle_ref = be32toh(*p);
    150  1.30   hkenken 		const u_int iparent = fdtbus_get_phandle_from_native(phandle_ref);
    151  1.30   hkenken 		uint32_t cells_num;
    152  1.30   hkenken 		of_getprop_uint32(iparent, cells, &cells_num);
    153  1.30   hkenken 
    154  1.30   hkenken 		if (index == i) {
    155  1.30   hkenken 			if (data != NULL) {
    156  1.30   hkenken 				data->phandle = iparent;
    157  1.30   hkenken 				data->count = cells_num;
    158  1.30   hkenken 				data->values = p + offset;
    159  1.30   hkenken 			}
    160  1.30   hkenken 			goto done;
    161  1.30   hkenken 		}
    162  1.30   hkenken 
    163  1.30   hkenken 		const u_int reclen = offset + cells_num;
    164  1.30   hkenken 		len -= reclen * sizeof(u_int);
    165  1.30   hkenken 		p += reclen;
    166  1.30   hkenken 	}
    167  1.30   hkenken 	return EINVAL;
    168  1.30   hkenken 
    169  1.30   hkenken done:
    170  1.30   hkenken 	return 0;
    171  1.30   hkenken }
    172  1.30   hkenken 
    173  1.30   hkenken int
    174   1.5  jmcneill fdtbus_get_phandle_from_native(int phandle)
    175   1.5  jmcneill {
    176   1.5  jmcneill 	const int off = fdt_node_offset_by_phandle(fdt_data, phandle);
    177   1.1  jmcneill 	if (off < 0) {
    178   1.1  jmcneill 		return -1;
    179   1.1  jmcneill 	}
    180   1.3  jmcneill 	return fdtbus_offset2phandle(off);
    181   1.1  jmcneill }
    182   1.1  jmcneill 
    183   1.6  jmcneill bool
    184   1.6  jmcneill fdtbus_get_path(int phandle, char *buf, size_t buflen)
    185   1.6  jmcneill {
    186   1.6  jmcneill 	const int off = fdtbus_phandle2offset(phandle);
    187   1.6  jmcneill 	if (off < 0) {
    188   1.6  jmcneill 		return false;
    189   1.6  jmcneill 	}
    190   1.6  jmcneill 	if (fdt_get_path(fdt_data, off, buf, (int)buflen) != 0) {
    191   1.6  jmcneill 		return false;
    192   1.6  jmcneill 	}
    193   1.6  jmcneill 	return true;
    194   1.6  jmcneill }
    195   1.6  jmcneill 
    196  1.13  jmcneill static uint64_t
    197  1.13  jmcneill fdtbus_get_cells(const uint8_t *buf, int cells)
    198  1.13  jmcneill {
    199  1.13  jmcneill 	switch (cells) {
    200  1.13  jmcneill 	case 0:		return 0;
    201  1.13  jmcneill 	case 1: 	return be32dec(buf);
    202  1.22  jakllsch 	case 2:		return ((uint64_t)be32dec(buf)<<32)|be32dec(buf+4);
    203  1.13  jmcneill 	default:	panic("fdtbus_get_cells: bad cells val %d\n", cells);
    204  1.13  jmcneill 	}
    205  1.13  jmcneill }
    206  1.13  jmcneill 
    207  1.13  jmcneill static uint64_t
    208  1.13  jmcneill fdtbus_decode_range(int phandle, uint64_t paddr)
    209  1.13  jmcneill {
    210  1.13  jmcneill 	const int parent = OF_parent(phandle);
    211  1.13  jmcneill 	if (parent == -1)
    212  1.13  jmcneill 		return paddr;
    213  1.20     skrll 
    214  1.20     skrll 	if (!fdtbus_decoderegprop)
    215  1.20     skrll 		return paddr;
    216  1.20     skrll 
    217  1.13  jmcneill 	const uint8_t *buf;
    218  1.13  jmcneill 	int len;
    219  1.13  jmcneill 
    220  1.13  jmcneill 	buf = fdt_getprop(fdtbus_get_data(),
    221  1.13  jmcneill 	    fdtbus_phandle2offset(phandle), "ranges", &len);
    222  1.13  jmcneill 	if (buf == NULL)
    223  1.13  jmcneill 		return paddr;
    224  1.13  jmcneill 
    225  1.13  jmcneill 	if (len == 0) {
    226  1.13  jmcneill 		/* pass through to parent */
    227  1.13  jmcneill 		return fdtbus_decode_range(parent, paddr);
    228  1.13  jmcneill 	}
    229  1.13  jmcneill 
    230  1.13  jmcneill 	const int addr_cells = fdtbus_get_addr_cells(phandle);
    231  1.13  jmcneill 	const int size_cells = fdtbus_get_size_cells(phandle);
    232  1.31  jmcneill 	const int paddr_cells = fdtbus_get_addr_cells(parent);
    233  1.13  jmcneill 	if (addr_cells == -1 || size_cells == -1 || paddr_cells == -1)
    234  1.13  jmcneill 		return paddr;
    235  1.13  jmcneill 
    236  1.13  jmcneill 	while (len > 0) {
    237  1.13  jmcneill 		uint64_t cba, pba, cl;
    238  1.13  jmcneill 		cba = fdtbus_get_cells(buf, addr_cells);
    239  1.13  jmcneill 		buf += addr_cells * 4;
    240  1.13  jmcneill 		pba = fdtbus_get_cells(buf, paddr_cells);
    241  1.13  jmcneill 		buf += paddr_cells * 4;
    242  1.13  jmcneill 		cl = fdtbus_get_cells(buf, size_cells);
    243  1.13  jmcneill 		buf += size_cells * 4;
    244  1.13  jmcneill 
    245  1.31  jmcneill #ifdef FDTBUS_DEBUG
    246  1.31  jmcneill 		printf("%s: %s: cba=0x%#" PRIx64 ", pba=0x%#" PRIx64 ", cl=0x%#" PRIx64 "\n", __func__, fdt_get_name(fdtbus_get_data(), fdtbus_phandle2offset(phandle), NULL), cba, pba, cl);
    247  1.31  jmcneill #endif
    248  1.31  jmcneill 
    249  1.13  jmcneill 		if (paddr >= cba && paddr < cba + cl)
    250  1.13  jmcneill 			return fdtbus_decode_range(parent, pba) + (paddr - cba);
    251  1.13  jmcneill 
    252  1.13  jmcneill 		len -= (addr_cells + paddr_cells + size_cells) * 4;
    253  1.13  jmcneill 	}
    254  1.13  jmcneill 
    255  1.13  jmcneill 	/* No mapping found */
    256  1.13  jmcneill 	return paddr;
    257  1.13  jmcneill }
    258  1.13  jmcneill 
    259   1.1  jmcneill int
    260  1.18  jmcneill fdtbus_get_reg_byname(int phandle, const char *name, bus_addr_t *paddr,
    261  1.18  jmcneill     bus_size_t *psize)
    262  1.18  jmcneill {
    263  1.18  jmcneill 	u_int index;
    264  1.29  jakllsch 	int error;
    265  1.18  jmcneill 
    266  1.29  jakllsch 	error = fdtbus_get_index(phandle, "reg-names", name, &index);
    267  1.29  jakllsch 	if (error != 0)
    268  1.29  jakllsch 		return ENOENT;
    269  1.18  jmcneill 
    270  1.29  jakllsch 	return fdtbus_get_reg(phandle, index, paddr, psize);
    271  1.18  jmcneill }
    272  1.18  jmcneill 
    273  1.18  jmcneill int
    274   1.1  jmcneill fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize)
    275   1.1  jmcneill {
    276  1.11  jmcneill 	uint64_t addr, size;
    277  1.11  jmcneill 	int error;
    278  1.11  jmcneill 
    279  1.11  jmcneill 	error = fdtbus_get_reg64(phandle, index, &addr, &size);
    280  1.11  jmcneill 	if (error)
    281  1.11  jmcneill 		return error;
    282  1.11  jmcneill 
    283  1.11  jmcneill 	if (sizeof(bus_addr_t) == 4 && (addr + size) > 0x100000000)
    284  1.11  jmcneill 		return ERANGE;
    285  1.11  jmcneill 
    286  1.11  jmcneill 	if (paddr)
    287  1.11  jmcneill 		*paddr = (bus_addr_t)addr;
    288  1.11  jmcneill 	if (psize)
    289  1.11  jmcneill 		*psize = (bus_size_t)size;
    290  1.11  jmcneill 
    291  1.11  jmcneill 	return 0;
    292  1.11  jmcneill }
    293  1.11  jmcneill 
    294  1.11  jmcneill int
    295  1.11  jmcneill fdtbus_get_reg64(int phandle, u_int index, uint64_t *paddr, uint64_t *psize)
    296  1.11  jmcneill {
    297  1.11  jmcneill 	uint64_t addr, size;
    298   1.7  jmcneill 	const uint8_t *buf;
    299   1.1  jmcneill 	int len;
    300   1.1  jmcneill 
    301  1.13  jmcneill 	const int addr_cells = fdtbus_get_addr_cells(OF_parent(phandle));
    302  1.13  jmcneill 	const int size_cells = fdtbus_get_size_cells(OF_parent(phandle));
    303   1.1  jmcneill 	if (addr_cells == -1 || size_cells == -1)
    304  1.11  jmcneill 		return EINVAL;
    305   1.1  jmcneill 
    306   1.7  jmcneill 	buf = fdt_getprop(fdtbus_get_data(),
    307   1.7  jmcneill 	    fdtbus_phandle2offset(phandle), "reg", &len);
    308   1.7  jmcneill 	if (buf == NULL || len <= 0)
    309  1.11  jmcneill 		return EINVAL;
    310   1.7  jmcneill 
    311   1.1  jmcneill 	const u_int reglen = size_cells * 4 + addr_cells * 4;
    312   1.1  jmcneill 	if (reglen == 0)
    313  1.11  jmcneill 		return EINVAL;
    314   1.1  jmcneill 
    315   1.7  jmcneill 	if (index >= len / reglen)
    316  1.11  jmcneill 		return ENXIO;
    317   1.1  jmcneill 
    318  1.13  jmcneill 	buf += index * reglen;
    319  1.13  jmcneill 	addr = fdtbus_get_cells(buf, addr_cells);
    320  1.13  jmcneill 	buf += addr_cells * 4;
    321  1.13  jmcneill 	size = fdtbus_get_cells(buf, size_cells);
    322  1.13  jmcneill 
    323  1.13  jmcneill 	if (paddr) {
    324  1.13  jmcneill 		*paddr = fdtbus_decode_range(OF_parent(phandle), addr);
    325  1.27  jmcneill #ifdef FDTBUS_DEBUG
    326  1.13  jmcneill 		const char *name = fdt_get_name(fdtbus_get_data(),
    327  1.13  jmcneill 		    fdtbus_phandle2offset(phandle), NULL);
    328  1.27  jmcneill 		printf("fdt: [%s] decoded addr #%u: %" PRIx64
    329  1.23  christos 		    " -> %" PRIx64 "\n", name, index, addr, *paddr);
    330  1.27  jmcneill #endif
    331   1.1  jmcneill 	}
    332   1.1  jmcneill 	if (psize)
    333   1.1  jmcneill 		*psize = size;
    334   1.1  jmcneill 
    335   1.1  jmcneill 	return 0;
    336   1.1  jmcneill }
    337   1.8  jmcneill 
    338  1.24     skrll #if defined(FDT)
    339  1.12  jmcneill const struct fdt_console *
    340  1.12  jmcneill fdtbus_get_console(void)
    341  1.12  jmcneill {
    342  1.12  jmcneill 	static const struct fdt_console_info *booted_console = NULL;
    343  1.12  jmcneill 
    344  1.12  jmcneill 	if (booted_console == NULL) {
    345  1.12  jmcneill 		__link_set_decl(fdt_consoles, struct fdt_console_info);
    346  1.12  jmcneill 		struct fdt_console_info * const *info;
    347  1.12  jmcneill 		const struct fdt_console_info *best_info = NULL;
    348  1.12  jmcneill 		const int phandle = fdtbus_get_stdout_phandle();
    349  1.12  jmcneill 		int best_match = 0;
    350  1.12  jmcneill 
    351  1.12  jmcneill 		__link_set_foreach(info, fdt_consoles) {
    352  1.12  jmcneill 			const int match = (*info)->ops->match(phandle);
    353  1.12  jmcneill 			if (match > best_match) {
    354  1.12  jmcneill 				best_match = match;
    355  1.12  jmcneill 				best_info = *info;
    356  1.12  jmcneill 			}
    357  1.12  jmcneill 		}
    358  1.12  jmcneill 
    359  1.12  jmcneill 		booted_console = best_info;
    360  1.12  jmcneill 	}
    361  1.12  jmcneill 
    362  1.12  jmcneill 	return booted_console == NULL ? NULL : booted_console->ops;
    363  1.12  jmcneill }
    364  1.24     skrll #endif
    365  1.12  jmcneill 
    366   1.8  jmcneill const char *
    367   1.8  jmcneill fdtbus_get_stdout_path(void)
    368   1.8  jmcneill {
    369   1.9  jmcneill 	const char *prop;
    370   1.9  jmcneill 
    371   1.8  jmcneill 	const int off = fdt_path_offset(fdtbus_get_data(), "/chosen");
    372   1.8  jmcneill 	if (off < 0)
    373   1.8  jmcneill 		return NULL;
    374   1.9  jmcneill 
    375   1.9  jmcneill 	prop = fdt_getprop(fdtbus_get_data(), off, "stdout-path", NULL);
    376   1.9  jmcneill 	if (prop != NULL)
    377   1.9  jmcneill 		return prop;
    378   1.9  jmcneill 
    379   1.9  jmcneill 	/* If the stdout-path property is not found, assume serial0 */
    380   1.9  jmcneill 	return "serial0:115200n8";
    381   1.8  jmcneill }
    382   1.8  jmcneill 
    383   1.8  jmcneill int
    384   1.8  jmcneill fdtbus_get_stdout_phandle(void)
    385   1.8  jmcneill {
    386   1.8  jmcneill 	const char *prop, *p;
    387   1.8  jmcneill 	int off, len;
    388   1.8  jmcneill 
    389   1.8  jmcneill 	prop = fdtbus_get_stdout_path();
    390   1.8  jmcneill 	if (prop == NULL)
    391   1.8  jmcneill 		return -1;
    392   1.8  jmcneill 
    393   1.8  jmcneill 	p = strchr(prop, ':');
    394   1.8  jmcneill 	len = p == NULL ? strlen(prop) : (p - prop);
    395   1.8  jmcneill 	if (*prop != '/') {
    396   1.8  jmcneill 		/* Alias */
    397   1.8  jmcneill 		prop = fdt_get_alias_namelen(fdtbus_get_data(), prop, len);
    398   1.8  jmcneill 		if (prop == NULL)
    399   1.8  jmcneill 			return -1;
    400   1.8  jmcneill 		len = strlen(prop);
    401   1.8  jmcneill 	}
    402   1.8  jmcneill 	off = fdt_path_offset_namelen(fdtbus_get_data(), prop, len);
    403   1.8  jmcneill 	if (off < 0)
    404   1.8  jmcneill 		return -1;
    405   1.8  jmcneill 
    406   1.8  jmcneill 	return fdtbus_offset2phandle(off);
    407   1.8  jmcneill }
    408   1.8  jmcneill 
    409   1.8  jmcneill int
    410   1.8  jmcneill fdtbus_get_stdout_speed(void)
    411   1.8  jmcneill {
    412   1.8  jmcneill 	const char *prop, *p;
    413   1.8  jmcneill 
    414   1.8  jmcneill 	prop = fdtbus_get_stdout_path();
    415   1.8  jmcneill 	if (prop == NULL)
    416   1.8  jmcneill 		return -1;
    417   1.8  jmcneill 
    418   1.8  jmcneill 	p = strchr(prop, ':');
    419   1.8  jmcneill 	if (p == NULL)
    420   1.8  jmcneill 		return -1;
    421   1.8  jmcneill 
    422   1.8  jmcneill 	return (int)strtoul(p + 1, NULL, 10);
    423   1.8  jmcneill }
    424  1.10  jmcneill 
    425  1.12  jmcneill tcflag_t
    426  1.12  jmcneill fdtbus_get_stdout_flags(void)
    427  1.12  jmcneill {
    428  1.12  jmcneill 	const char *prop, *p;
    429  1.12  jmcneill 	tcflag_t flags = TTYDEF_CFLAG;
    430  1.12  jmcneill 	char *ep;
    431  1.12  jmcneill 
    432  1.12  jmcneill 	prop = fdtbus_get_stdout_path();
    433  1.12  jmcneill 	if (prop == NULL)
    434  1.12  jmcneill 		return flags;
    435  1.12  jmcneill 
    436  1.12  jmcneill 	p = strchr(prop, ':');
    437  1.12  jmcneill 	if (p == NULL)
    438  1.12  jmcneill 		return flags;
    439  1.12  jmcneill 
    440  1.12  jmcneill 	ep = NULL;
    441  1.12  jmcneill 	(void)strtoul(p + 1, &ep, 10);
    442  1.12  jmcneill 	if (ep == NULL)
    443  1.12  jmcneill 		return flags;
    444  1.12  jmcneill 
    445  1.12  jmcneill 	/* <baud>{<parity>{<bits>{<flow>}}} */
    446  1.12  jmcneill 	while (*ep) {
    447  1.12  jmcneill 		switch (*ep) {
    448  1.12  jmcneill 		/* parity */
    449  1.12  jmcneill 		case 'n':	flags &= ~(PARENB|PARODD); break;
    450  1.12  jmcneill 		case 'e':	flags &= ~PARODD; flags |= PARENB; break;
    451  1.12  jmcneill 		case 'o':	flags |= (PARENB|PARODD); break;
    452  1.12  jmcneill 		/* bits */
    453  1.12  jmcneill 		case '5':	flags &= ~CSIZE; flags |= CS5; break;
    454  1.12  jmcneill 		case '6':	flags &= ~CSIZE; flags |= CS6; break;
    455  1.12  jmcneill 		case '7':	flags &= ~CSIZE; flags |= CS7; break;
    456  1.12  jmcneill 		case '8':	flags &= ~CSIZE; flags |= CS8; break;
    457  1.12  jmcneill 		/* flow */
    458  1.12  jmcneill 		case 'r':	flags |= CRTSCTS; break;
    459  1.12  jmcneill 		}
    460  1.12  jmcneill 		ep++;
    461  1.12  jmcneill 	}
    462  1.12  jmcneill 
    463  1.12  jmcneill 	return flags;
    464  1.12  jmcneill }
    465  1.12  jmcneill 
    466  1.10  jmcneill bool
    467  1.10  jmcneill fdtbus_status_okay(int phandle)
    468  1.10  jmcneill {
    469  1.10  jmcneill 	const int off = fdtbus_phandle2offset(phandle);
    470  1.10  jmcneill 
    471  1.10  jmcneill 	const char *prop = fdt_getprop(fdtbus_get_data(), off, "status", NULL);
    472  1.10  jmcneill 	if (prop == NULL)
    473  1.10  jmcneill 		return true;
    474  1.10  jmcneill 
    475  1.10  jmcneill 	return strncmp(prop, "ok", 2) == 0;
    476  1.10  jmcneill }
    477  1.14  jmcneill 
    478  1.15  jmcneill const void *
    479  1.15  jmcneill fdtbus_get_prop(int phandle, const char *prop, int *plen)
    480  1.15  jmcneill {
    481  1.15  jmcneill 	const int off = fdtbus_phandle2offset(phandle);
    482  1.15  jmcneill 
    483  1.15  jmcneill 	return fdt_getprop(fdtbus_get_data(), off, prop, plen);
    484  1.15  jmcneill }
    485  1.15  jmcneill 
    486  1.14  jmcneill const char *
    487  1.14  jmcneill fdtbus_get_string(int phandle, const char *prop)
    488  1.14  jmcneill {
    489  1.14  jmcneill 	const int off = fdtbus_phandle2offset(phandle);
    490  1.14  jmcneill 
    491  1.19  jmcneill 	if (strcmp(prop, "name") == 0)
    492  1.19  jmcneill 		return fdt_get_name(fdtbus_get_data(), off, NULL);
    493  1.19  jmcneill 	else
    494  1.19  jmcneill 		return fdt_getprop(fdtbus_get_data(), off, prop, NULL);
    495  1.14  jmcneill }
    496  1.16  jmcneill 
    497  1.16  jmcneill const char *
    498  1.16  jmcneill fdtbus_get_string_index(int phandle, const char *prop, u_int index)
    499  1.16  jmcneill {
    500  1.16  jmcneill 	const char *names, *name;
    501  1.16  jmcneill 	int len, cur;
    502  1.16  jmcneill 
    503  1.16  jmcneill 	if ((len = OF_getproplen(phandle, prop)) < 0)
    504  1.16  jmcneill 		return NULL;
    505  1.16  jmcneill 
    506  1.16  jmcneill 	names = fdtbus_get_string(phandle, prop);
    507  1.16  jmcneill 
    508  1.16  jmcneill 	for (name = names, cur = 0; len > 0;
    509  1.21    bouyer 	     len -= strlen(name) + 1, name += strlen(name) + 1, cur++) {
    510  1.16  jmcneill 		if (index == cur)
    511  1.16  jmcneill 			return name;
    512  1.16  jmcneill 	}
    513  1.16  jmcneill 
    514  1.16  jmcneill 	return NULL;
    515  1.16  jmcneill }
    516  1.29  jakllsch 
    517  1.29  jakllsch int
    518  1.29  jakllsch fdtbus_get_index(int phandle, const char *prop, const char *name, u_int *idx)
    519  1.29  jakllsch {
    520  1.29  jakllsch 	const char *p;
    521  1.29  jakllsch 	size_t pl;
    522  1.29  jakllsch 	u_int index;
    523  1.29  jakllsch 	int len;
    524  1.29  jakllsch 
    525  1.29  jakllsch 	p = fdtbus_get_prop(phandle, prop, &len);
    526  1.29  jakllsch 	if (p == NULL || len <= 0)
    527  1.29  jakllsch 		return -1;
    528  1.29  jakllsch 
    529  1.29  jakllsch 	for (index = 0; len > 0;
    530  1.29  jakllsch 	    pl = strlen(p) + 1, len -= pl, p += pl, index++) {
    531  1.29  jakllsch 		if (strcmp(p, name) == 0) {
    532  1.29  jakllsch 			*idx = index;
    533  1.29  jakllsch 			return 0;
    534  1.29  jakllsch 		}
    535  1.29  jakllsch 	}
    536  1.29  jakllsch 
    537  1.29  jakllsch 	return -1;
    538  1.29  jakllsch }
    539