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