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