Home | History | Annotate | Line # | Download | only in fdt
fdt_subr.c revision 1.2
      1  1.2  jmcneill /* $NetBSD: fdt_subr.c,v 1.2 2015/12/16 12:03:44 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.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_subr.c,v 1.2 2015/12/16 12:03:44 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/ofw/openfirm.h>
     38  1.1  jmcneill #include <dev/fdt/fdt_openfirm.h>
     39  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     40  1.1  jmcneill 
     41  1.1  jmcneill static int
     42  1.1  jmcneill fdtbus_get_addr_cells(int phandle)
     43  1.1  jmcneill {
     44  1.1  jmcneill 	int val, addr_cells, error;
     45  1.1  jmcneill 
     46  1.1  jmcneill 	const int parent = OF_parent(phandle);
     47  1.1  jmcneill 	if (parent == -1)
     48  1.1  jmcneill 		return -1;
     49  1.1  jmcneill 
     50  1.1  jmcneill 	error = OF_getprop(parent, "#address-cells", &val, sizeof(val));
     51  1.1  jmcneill 	if (error <= 0) {
     52  1.1  jmcneill 		addr_cells = 2;
     53  1.1  jmcneill 	} else {
     54  1.1  jmcneill 		addr_cells = fdt32_to_cpu(val);
     55  1.1  jmcneill 	}
     56  1.1  jmcneill 
     57  1.1  jmcneill 	return addr_cells;
     58  1.1  jmcneill }
     59  1.1  jmcneill 
     60  1.1  jmcneill static int
     61  1.1  jmcneill fdtbus_get_size_cells(int phandle)
     62  1.1  jmcneill {
     63  1.1  jmcneill 	int val, size_cells, error;
     64  1.1  jmcneill 
     65  1.1  jmcneill 	const int parent = OF_parent(phandle);
     66  1.1  jmcneill 	if (parent == -1)
     67  1.1  jmcneill 		return -1;
     68  1.1  jmcneill 
     69  1.1  jmcneill 	error = OF_getprop(parent, "#size-cells", &val, sizeof(val));
     70  1.1  jmcneill 	if (error <= 0) {
     71  1.1  jmcneill 		size_cells = 0;
     72  1.1  jmcneill 	} else {
     73  1.1  jmcneill 		size_cells = fdt32_to_cpu(val);
     74  1.1  jmcneill 	}
     75  1.1  jmcneill 
     76  1.1  jmcneill 	return size_cells;
     77  1.1  jmcneill }
     78  1.1  jmcneill 
     79  1.1  jmcneill int
     80  1.1  jmcneill fdtbus_get_phandle(int phandle, const char *prop)
     81  1.1  jmcneill {
     82  1.1  jmcneill 	u_int phandle_ref;
     83  1.1  jmcneill 	u_int *buf;
     84  1.1  jmcneill 	int len;
     85  1.1  jmcneill 
     86  1.1  jmcneill 	len = OF_getproplen(phandle, prop);
     87  1.1  jmcneill 	if (len < sizeof(phandle_ref))
     88  1.1  jmcneill 		return -1;
     89  1.1  jmcneill 
     90  1.1  jmcneill 	buf = kmem_alloc(len, KM_SLEEP);
     91  1.1  jmcneill 
     92  1.1  jmcneill 	if (OF_getprop(phandle, prop, buf, len) != len) {
     93  1.1  jmcneill 		kmem_free(buf, len);
     94  1.1  jmcneill 		return -1;
     95  1.1  jmcneill 	}
     96  1.1  jmcneill 
     97  1.1  jmcneill 	phandle_ref = fdt32_to_cpu(buf[0]);
     98  1.1  jmcneill 	kmem_free(buf, len);
     99  1.1  jmcneill 
    100  1.1  jmcneill 	const void *data = fdt_openfirm_get_data();
    101  1.1  jmcneill 	const int off = fdt_node_offset_by_phandle(data, phandle_ref);
    102  1.1  jmcneill 	if (off < 0) {
    103  1.1  jmcneill 		return -1;
    104  1.1  jmcneill 	}
    105  1.1  jmcneill 
    106  1.1  jmcneill 	return fdt_openfirm_get_phandle(off);
    107  1.1  jmcneill }
    108  1.1  jmcneill 
    109  1.1  jmcneill int
    110  1.1  jmcneill fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize)
    111  1.1  jmcneill {
    112  1.1  jmcneill 	bus_addr_t addr;
    113  1.1  jmcneill 	bus_size_t size;
    114  1.1  jmcneill 	uint8_t *buf;
    115  1.1  jmcneill 	int len;
    116  1.1  jmcneill 
    117  1.1  jmcneill 	const int addr_cells = fdtbus_get_addr_cells(phandle);
    118  1.1  jmcneill 	const int size_cells = fdtbus_get_size_cells(phandle);
    119  1.1  jmcneill 	if (addr_cells == -1 || size_cells == -1)
    120  1.1  jmcneill 		return -1;
    121  1.1  jmcneill 
    122  1.1  jmcneill 	const u_int reglen = size_cells * 4 + addr_cells * 4;
    123  1.1  jmcneill 	if (reglen == 0)
    124  1.1  jmcneill 		return -1;
    125  1.1  jmcneill 
    126  1.1  jmcneill 	len = OF_getproplen(phandle, "reg");
    127  1.1  jmcneill 	if (len <= 0)
    128  1.1  jmcneill 		return -1;
    129  1.1  jmcneill 
    130  1.1  jmcneill 	const u_int nregs = len / reglen;
    131  1.1  jmcneill 
    132  1.1  jmcneill 	if (index >= nregs)
    133  1.1  jmcneill 		return -1;
    134  1.1  jmcneill 
    135  1.1  jmcneill 	buf = kmem_alloc(len, KM_SLEEP);
    136  1.1  jmcneill 	if (buf == NULL)
    137  1.1  jmcneill 		return -1;
    138  1.1  jmcneill 
    139  1.1  jmcneill 	len = OF_getprop(phandle, "reg", buf, len);
    140  1.1  jmcneill 
    141  1.1  jmcneill 	switch (addr_cells) {
    142  1.1  jmcneill 	case 0:
    143  1.1  jmcneill 		addr = 0;
    144  1.1  jmcneill 		break;
    145  1.1  jmcneill 	case 1:
    146  1.1  jmcneill 		addr = be32dec(&buf[index * reglen + 0]);
    147  1.1  jmcneill 		break;
    148  1.1  jmcneill 	case 2:
    149  1.1  jmcneill 		addr = be64dec(&buf[index * reglen + 0]);
    150  1.1  jmcneill 		break;
    151  1.1  jmcneill 	default:
    152  1.1  jmcneill 		panic("fdtbus_get_reg: unsupported addr_cells %d", addr_cells);
    153  1.1  jmcneill 	}
    154  1.1  jmcneill 
    155  1.1  jmcneill 	switch (size_cells) {
    156  1.1  jmcneill 	case 0:
    157  1.1  jmcneill 		size = 0;
    158  1.1  jmcneill 		break;
    159  1.1  jmcneill 	case 1:
    160  1.1  jmcneill 		size = be32dec(&buf[index * reglen + addr_cells * 4]);
    161  1.1  jmcneill 		break;
    162  1.1  jmcneill 	case 2:
    163  1.1  jmcneill 		size = be64dec(&buf[index * reglen + addr_cells * 4]);
    164  1.1  jmcneill 		break;
    165  1.1  jmcneill 	default:
    166  1.1  jmcneill 		panic("fdtbus_get_reg: unsupported size_cells %d", size_cells);
    167  1.1  jmcneill 	}
    168  1.1  jmcneill 
    169  1.1  jmcneill 	if (paddr)
    170  1.1  jmcneill 		*paddr = addr;
    171  1.1  jmcneill 	if (psize)
    172  1.1  jmcneill 		*psize = size;
    173  1.1  jmcneill 
    174  1.1  jmcneill 	kmem_free(buf, len);
    175  1.1  jmcneill 
    176  1.1  jmcneill 	return 0;
    177  1.1  jmcneill }
    178