2010-06-02 17:17:45 +00:00
|
|
|
/*-
|
2017-11-27 14:52:40 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2014-11-20 17:03:40 +00:00
|
|
|
* Copyright (c) 2009-2014 The FreeBSD Foundation
|
2010-06-02 17:17:45 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-11-20 17:03:40 +00:00
|
|
|
* This software was developed by Andrew Turner under sponsorship from
|
|
|
|
* the FreeBSD Foundation.
|
2010-06-02 17:17:45 +00:00
|
|
|
* This software was developed by Semihalf under sponsorship from
|
|
|
|
* the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/bus.h>
|
2012-08-15 03:49:10 +00:00
|
|
|
#include <sys/limits.h>
|
2016-08-06 18:48:47 +00:00
|
|
|
#include <sys/sysctl.h>
|
2010-06-02 17:17:45 +00:00
|
|
|
|
|
|
|
#include <machine/resource.h>
|
|
|
|
|
|
|
|
#include <dev/fdt/fdt_common.h>
|
|
|
|
#include <dev/ofw/ofw_bus.h>
|
|
|
|
#include <dev/ofw/ofw_bus_subr.h>
|
|
|
|
#include <dev/ofw/openfirm.h>
|
|
|
|
|
|
|
|
#include "ofw_bus_if.h"
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define debugf(fmt, args...) do { printf("%s(): ", __func__); \
|
|
|
|
printf(fmt,##args); } while (0)
|
|
|
|
#else
|
|
|
|
#define debugf(fmt, args...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define FDT_COMPAT_LEN 255
|
|
|
|
|
|
|
|
#define FDT_REG_CELLS 4
|
2017-08-09 00:45:25 +00:00
|
|
|
#define FDT_RANGES_SIZE 48
|
2010-06-02 17:17:45 +00:00
|
|
|
|
2020-02-26 14:26:36 +00:00
|
|
|
SYSCTL_NODE(_hw, OID_AUTO, fdt, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
|
|
|
|
"Flattened Device Tree");
|
2016-08-06 18:48:47 +00:00
|
|
|
|
2010-06-02 17:17:45 +00:00
|
|
|
vm_paddr_t fdt_immr_pa;
|
|
|
|
vm_offset_t fdt_immr_va;
|
|
|
|
vm_offset_t fdt_immr_size;
|
|
|
|
|
2013-10-21 21:13:01 +00:00
|
|
|
struct fdt_ic_list fdt_ic_list_head = SLIST_HEAD_INITIALIZER(fdt_ic_list_head);
|
|
|
|
|
2014-11-20 17:03:40 +00:00
|
|
|
static int
|
|
|
|
fdt_get_range_by_busaddr(phandle_t node, u_long addr, u_long *base,
|
|
|
|
u_long *size)
|
|
|
|
{
|
|
|
|
pcell_t ranges[32], *rangesptr;
|
|
|
|
pcell_t addr_cells, size_cells, par_addr_cells;
|
|
|
|
u_long bus_addr, par_bus_addr, pbase, psize;
|
|
|
|
int err, i, len, tuple_size, tuples;
|
|
|
|
|
2014-12-19 13:07:36 +00:00
|
|
|
if (node == 0) {
|
|
|
|
*base = 0;
|
|
|
|
*size = ULONG_MAX;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-11-20 17:03:40 +00:00
|
|
|
if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0)
|
|
|
|
return (ENXIO);
|
|
|
|
/*
|
|
|
|
* Process 'ranges' property.
|
|
|
|
*/
|
|
|
|
par_addr_cells = fdt_parent_addr_cells(node);
|
|
|
|
if (par_addr_cells > 2) {
|
|
|
|
return (ERANGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = OF_getproplen(node, "ranges");
|
|
|
|
if (len < 0)
|
|
|
|
return (-1);
|
|
|
|
if (len > sizeof(ranges))
|
|
|
|
return (ENOMEM);
|
|
|
|
if (len == 0) {
|
2014-12-19 13:07:36 +00:00
|
|
|
return (fdt_get_range_by_busaddr(OF_parent(node), addr,
|
|
|
|
base, size));
|
2014-11-20 17:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
tuple_size = addr_cells + par_addr_cells + size_cells;
|
|
|
|
tuples = len / (tuple_size * sizeof(cell_t));
|
|
|
|
|
2014-12-15 09:40:25 +00:00
|
|
|
if (par_addr_cells > 2 || addr_cells > 2 || size_cells > 2)
|
2014-11-20 17:03:40 +00:00
|
|
|
return (ERANGE);
|
2014-12-15 09:40:25 +00:00
|
|
|
|
2014-11-20 17:03:40 +00:00
|
|
|
*base = 0;
|
|
|
|
*size = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < tuples; i++) {
|
|
|
|
rangesptr = &ranges[i * tuple_size];
|
|
|
|
|
|
|
|
bus_addr = fdt_data_get((void *)rangesptr, addr_cells);
|
|
|
|
if (bus_addr != addr)
|
|
|
|
continue;
|
|
|
|
rangesptr += addr_cells;
|
|
|
|
|
|
|
|
par_bus_addr = fdt_data_get((void *)rangesptr, par_addr_cells);
|
|
|
|
rangesptr += par_addr_cells;
|
|
|
|
|
|
|
|
err = fdt_get_range_by_busaddr(OF_parent(node), par_bus_addr,
|
|
|
|
&pbase, &psize);
|
|
|
|
if (err > 0)
|
|
|
|
return (err);
|
|
|
|
if (err == 0)
|
|
|
|
*base = pbase;
|
|
|
|
else
|
|
|
|
*base = par_bus_addr;
|
|
|
|
|
|
|
|
*size = fdt_data_get((void *)rangesptr, size_cells);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2010-06-02 17:17:45 +00:00
|
|
|
int
|
2012-08-15 03:49:10 +00:00
|
|
|
fdt_get_range(phandle_t node, int range_id, u_long *base, u_long *size)
|
2010-06-02 17:17:45 +00:00
|
|
|
{
|
2017-08-09 00:45:25 +00:00
|
|
|
pcell_t ranges[FDT_RANGES_SIZE], *rangesptr;
|
2010-06-02 17:17:45 +00:00
|
|
|
pcell_t addr_cells, size_cells, par_addr_cells;
|
2014-11-20 17:03:40 +00:00
|
|
|
u_long par_bus_addr, pbase, psize;
|
2017-12-25 04:48:39 +00:00
|
|
|
int err, len;
|
2010-06-02 17:17:45 +00:00
|
|
|
|
|
|
|
if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0)
|
|
|
|
return (ENXIO);
|
|
|
|
/*
|
|
|
|
* Process 'ranges' property.
|
|
|
|
*/
|
|
|
|
par_addr_cells = fdt_parent_addr_cells(node);
|
|
|
|
if (par_addr_cells > 2)
|
|
|
|
return (ERANGE);
|
|
|
|
|
|
|
|
len = OF_getproplen(node, "ranges");
|
|
|
|
if (len > sizeof(ranges))
|
|
|
|
return (ENOMEM);
|
2012-08-15 03:49:10 +00:00
|
|
|
if (len == 0) {
|
|
|
|
*base = 0;
|
|
|
|
*size = ULONG_MAX;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(range_id < len))
|
|
|
|
return (ERANGE);
|
2010-06-02 17:17:45 +00:00
|
|
|
|
|
|
|
if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
2014-12-15 09:40:25 +00:00
|
|
|
if (par_addr_cells > 2 || addr_cells > 2 || size_cells > 2)
|
2010-06-02 17:17:45 +00:00
|
|
|
return (ERANGE);
|
2014-12-15 09:40:25 +00:00
|
|
|
|
2012-08-15 03:49:10 +00:00
|
|
|
*base = 0;
|
|
|
|
*size = 0;
|
|
|
|
rangesptr = &ranges[range_id];
|
2010-06-02 17:17:45 +00:00
|
|
|
|
2012-08-15 03:49:10 +00:00
|
|
|
*base = fdt_data_get((void *)rangesptr, addr_cells);
|
2010-06-02 17:17:45 +00:00
|
|
|
rangesptr += addr_cells;
|
2014-11-20 17:03:40 +00:00
|
|
|
|
|
|
|
par_bus_addr = fdt_data_get((void *)rangesptr, par_addr_cells);
|
2010-06-02 17:17:45 +00:00
|
|
|
rangesptr += par_addr_cells;
|
2014-11-20 17:03:40 +00:00
|
|
|
|
|
|
|
err = fdt_get_range_by_busaddr(OF_parent(node), par_bus_addr,
|
|
|
|
&pbase, &psize);
|
|
|
|
if (err == 0)
|
|
|
|
*base += pbase;
|
|
|
|
else
|
|
|
|
*base += par_bus_addr;
|
|
|
|
|
2012-08-15 03:49:10 +00:00
|
|
|
*size = fdt_data_get((void *)rangesptr, size_cells);
|
|
|
|
return (0);
|
|
|
|
}
|
2010-06-02 17:17:45 +00:00
|
|
|
|
2018-04-08 15:21:12 +00:00
|
|
|
int
|
|
|
|
fdt_immr_addr(vm_offset_t immr_va)
|
|
|
|
{
|
|
|
|
phandle_t node;
|
|
|
|
u_long base, size;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to access the SOC node directly i.e. through /aliases/.
|
|
|
|
*/
|
|
|
|
if ((node = OF_finddevice("soc")) != -1)
|
2018-04-08 22:07:06 +00:00
|
|
|
if (ofw_bus_node_is_compatible(node, "simple-bus"))
|
2018-04-08 15:21:12 +00:00
|
|
|
goto moveon;
|
|
|
|
/*
|
|
|
|
* Find the node the long way.
|
|
|
|
*/
|
|
|
|
if ((node = OF_finddevice("/")) == -1)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
if ((node = fdt_find_compatible(node, "simple-bus", 0)) == 0)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
moveon:
|
|
|
|
if ((r = fdt_get_range(node, 0, &base, &size)) == 0) {
|
|
|
|
fdt_immr_pa = base;
|
|
|
|
fdt_immr_va = immr_va;
|
|
|
|
fdt_immr_size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
|
2010-06-02 17:17:45 +00:00
|
|
|
int
|
|
|
|
fdt_is_compatible_strict(phandle_t node, const char *compatible)
|
|
|
|
{
|
|
|
|
char compat[FDT_COMPAT_LEN];
|
|
|
|
|
|
|
|
if (OF_getproplen(node, "compatible") <= 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (strncasecmp(compat, compatible, FDT_COMPAT_LEN) == 0)
|
|
|
|
/* This fits. */
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
phandle_t
|
|
|
|
fdt_find_compatible(phandle_t start, const char *compat, int strict)
|
|
|
|
{
|
|
|
|
phandle_t child;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Traverse all children of 'start' node, and find first with
|
|
|
|
* matching 'compatible' property.
|
|
|
|
*/
|
|
|
|
for (child = OF_child(start); child != 0; child = OF_peer(child))
|
2018-04-08 22:07:06 +00:00
|
|
|
if (ofw_bus_node_is_compatible(child, compat)) {
|
2010-06-02 17:17:45 +00:00
|
|
|
if (strict)
|
|
|
|
if (!fdt_is_compatible_strict(child, compat))
|
|
|
|
continue;
|
|
|
|
return (child);
|
|
|
|
}
|
|
|
|
return (0);
|
2014-02-16 03:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
phandle_t
|
|
|
|
fdt_depth_search_compatible(phandle_t start, const char *compat, int strict)
|
|
|
|
{
|
|
|
|
phandle_t child, node;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Depth-search all descendants of 'start' node, and find first with
|
|
|
|
* matching 'compatible' property.
|
|
|
|
*/
|
|
|
|
for (node = OF_child(start); node != 0; node = OF_peer(node)) {
|
2018-04-08 22:07:06 +00:00
|
|
|
if (ofw_bus_node_is_compatible(node, compat) &&
|
2014-02-16 03:00:59 +00:00
|
|
|
(strict == 0 || fdt_is_compatible_strict(node, compat))) {
|
|
|
|
return (node);
|
|
|
|
}
|
2014-02-16 03:34:07 +00:00
|
|
|
child = fdt_depth_search_compatible(node, compat, strict);
|
2014-02-16 03:00:59 +00:00
|
|
|
if (child != 0)
|
|
|
|
return (child);
|
|
|
|
}
|
|
|
|
return (0);
|
2010-06-02 17:17:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fdt_parent_addr_cells(phandle_t node)
|
|
|
|
{
|
|
|
|
pcell_t addr_cells;
|
|
|
|
|
|
|
|
/* Find out #address-cells of the superior bus. */
|
|
|
|
if (OF_searchprop(OF_parent(node), "#address-cells", &addr_cells,
|
|
|
|
sizeof(addr_cells)) <= 0)
|
2014-11-20 17:03:40 +00:00
|
|
|
return (2);
|
2010-06-02 17:17:45 +00:00
|
|
|
|
|
|
|
return ((int)fdt32_to_cpu(addr_cells));
|
|
|
|
}
|
|
|
|
|
|
|
|
u_long
|
|
|
|
fdt_data_get(void *data, int cells)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (cells == 1)
|
|
|
|
return (fdt32_to_cpu(*((uint32_t *)data)));
|
|
|
|
|
|
|
|
return (fdt64_to_cpu(*((uint64_t *)data)));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fdt_addrsize_cells(phandle_t node, int *addr_cells, int *size_cells)
|
|
|
|
{
|
|
|
|
pcell_t cell;
|
|
|
|
int cell_size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve #{address,size}-cells.
|
|
|
|
*/
|
|
|
|
cell_size = sizeof(cell);
|
2016-11-11 14:19:12 +00:00
|
|
|
if (OF_getencprop(node, "#address-cells", &cell, cell_size) < cell_size)
|
2017-04-30 07:31:48 +00:00
|
|
|
cell = 2;
|
2016-11-11 14:19:12 +00:00
|
|
|
*addr_cells = (int)cell;
|
2010-06-02 17:17:45 +00:00
|
|
|
|
2016-11-11 14:19:12 +00:00
|
|
|
if (OF_getencprop(node, "#size-cells", &cell, cell_size) < cell_size)
|
2010-06-02 17:17:45 +00:00
|
|
|
cell = 1;
|
2016-11-11 14:19:12 +00:00
|
|
|
*size_cells = (int)cell;
|
2010-06-02 17:17:45 +00:00
|
|
|
|
|
|
|
if (*addr_cells > 3 || *size_cells > 2)
|
|
|
|
return (ERANGE);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fdt_data_to_res(pcell_t *data, int addr_cells, int size_cells, u_long *start,
|
|
|
|
u_long *count)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Address portion. */
|
2014-12-15 09:40:25 +00:00
|
|
|
if (addr_cells > 2)
|
2010-06-02 17:17:45 +00:00
|
|
|
return (ERANGE);
|
|
|
|
|
|
|
|
*start = fdt_data_get((void *)data, addr_cells);
|
|
|
|
data += addr_cells;
|
|
|
|
|
|
|
|
/* Size portion. */
|
2014-12-15 09:40:25 +00:00
|
|
|
if (size_cells > 2)
|
2010-06-02 17:17:45 +00:00
|
|
|
return (ERANGE);
|
|
|
|
|
|
|
|
*count = fdt_data_get((void *)data, size_cells);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fdt_regsize(phandle_t node, u_long *base, u_long *size)
|
|
|
|
{
|
|
|
|
pcell_t reg[4];
|
|
|
|
int addr_cells, len, size_cells;
|
|
|
|
|
|
|
|
if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells))
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
if ((sizeof(pcell_t) * (addr_cells + size_cells)) > sizeof(reg))
|
|
|
|
return (ENOMEM);
|
|
|
|
|
|
|
|
len = OF_getprop(node, "reg", ®, sizeof(reg));
|
|
|
|
if (len <= 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
*base = fdt_data_get(®[0], addr_cells);
|
|
|
|
*size = fdt_data_get(®[addr_cells], size_cells);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-03-04 19:22:52 +00:00
|
|
|
fdt_get_phyaddr(phandle_t node, device_t dev, int *phy_addr, void **phy_sc)
|
2010-06-02 17:17:45 +00:00
|
|
|
{
|
|
|
|
phandle_t phy_node;
|
|
|
|
pcell_t phy_handle, phy_reg;
|
2012-03-04 19:22:52 +00:00
|
|
|
uint32_t i;
|
|
|
|
device_t parent, child;
|
2010-06-02 17:17:45 +00:00
|
|
|
|
2013-10-23 14:04:09 +00:00
|
|
|
if (OF_getencprop(node, "phy-handle", (void *)&phy_handle,
|
2010-06-02 17:17:45 +00:00
|
|
|
sizeof(phy_handle)) <= 0)
|
|
|
|
return (ENXIO);
|
|
|
|
|
2014-09-01 18:51:01 +00:00
|
|
|
phy_node = OF_node_from_xref(phy_handle);
|
2010-06-02 17:17:45 +00:00
|
|
|
|
2016-11-11 14:19:12 +00:00
|
|
|
if (OF_getencprop(phy_node, "reg", (void *)&phy_reg,
|
2010-06-02 17:17:45 +00:00
|
|
|
sizeof(phy_reg)) <= 0)
|
|
|
|
return (ENXIO);
|
|
|
|
|
2016-11-11 14:19:12 +00:00
|
|
|
*phy_addr = phy_reg;
|
2012-03-04 19:22:52 +00:00
|
|
|
|
2019-04-03 21:01:53 +00:00
|
|
|
if (phy_sc == NULL)
|
|
|
|
return (0);
|
|
|
|
|
2012-03-04 19:22:52 +00:00
|
|
|
/*
|
|
|
|
* Search for softc used to communicate with phy.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 1: Search for ancestor of the phy-node with a "phy-handle"
|
|
|
|
* property set.
|
|
|
|
*/
|
|
|
|
phy_node = OF_parent(phy_node);
|
|
|
|
while (phy_node != 0) {
|
|
|
|
if (OF_getprop(phy_node, "phy-handle", (void *)&phy_handle,
|
|
|
|
sizeof(phy_handle)) > 0)
|
|
|
|
break;
|
|
|
|
phy_node = OF_parent(phy_node);
|
|
|
|
}
|
|
|
|
if (phy_node == 0)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 2: For each device with the same parent and name as ours
|
|
|
|
* compare its node with the one found in step 1, ancestor of phy
|
|
|
|
* node (stored in phy_node).
|
|
|
|
*/
|
|
|
|
parent = device_get_parent(dev);
|
|
|
|
i = 0;
|
|
|
|
child = device_find_child(parent, device_get_name(dev), i);
|
|
|
|
while (child != NULL) {
|
|
|
|
if (ofw_bus_get_node(child) == phy_node)
|
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
child = device_find_child(parent, device_get_name(dev), i);
|
|
|
|
}
|
|
|
|
if (child == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use softc of the device found.
|
|
|
|
*/
|
|
|
|
*phy_sc = (void *)device_get_softc(child);
|
|
|
|
|
2010-06-02 17:17:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2012-11-30 03:08:49 +00:00
|
|
|
int
|
|
|
|
fdt_get_reserved_regions(struct mem_region *mr, int *mrcnt)
|
|
|
|
{
|
|
|
|
pcell_t reserve[FDT_REG_CELLS * FDT_MEM_REGIONS];
|
|
|
|
pcell_t *reservep;
|
|
|
|
phandle_t memory, root;
|
|
|
|
int addr_cells, size_cells;
|
2017-12-25 04:48:39 +00:00
|
|
|
int i, res_len, rv, tuple_size, tuples;
|
2012-11-30 03:08:49 +00:00
|
|
|
|
|
|
|
root = OF_finddevice("/");
|
|
|
|
memory = OF_finddevice("/memory");
|
|
|
|
if (memory == -1) {
|
|
|
|
rv = ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
|
|
|
|
&size_cells)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (addr_cells > 2) {
|
|
|
|
rv = ERANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
|
|
|
|
|
|
|
|
res_len = OF_getproplen(root, "memreserve");
|
|
|
|
if (res_len <= 0 || res_len > sizeof(reserve)) {
|
|
|
|
rv = ERANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OF_getprop(root, "memreserve", reserve, res_len) <= 0) {
|
|
|
|
rv = ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
tuples = res_len / tuple_size;
|
|
|
|
reservep = (pcell_t *)&reserve;
|
|
|
|
for (i = 0; i < tuples; i++) {
|
|
|
|
|
|
|
|
rv = fdt_data_to_res(reservep, addr_cells, size_cells,
|
|
|
|
(u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size);
|
|
|
|
|
|
|
|
if (rv != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
reservep += addr_cells + size_cells;
|
|
|
|
}
|
|
|
|
|
|
|
|
*mrcnt = i;
|
|
|
|
rv = 0;
|
|
|
|
out:
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
2018-05-22 16:03:41 +00:00
|
|
|
int
|
|
|
|
fdt_get_reserved_mem(struct mem_region *reserved, int *mreserved)
|
|
|
|
{
|
|
|
|
pcell_t reg[FDT_REG_CELLS];
|
|
|
|
phandle_t child, root;
|
|
|
|
int addr_cells, size_cells;
|
|
|
|
int i, rv;
|
|
|
|
|
|
|
|
root = OF_finddevice("/reserved-memory");
|
|
|
|
if (root == -1) {
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((rv = fdt_addrsize_cells(root, &addr_cells, &size_cells)) != 0)
|
|
|
|
return (rv);
|
|
|
|
|
|
|
|
if (addr_cells + size_cells > FDT_REG_CELLS)
|
|
|
|
panic("Too many address and size cells %d %d", addr_cells,
|
|
|
|
size_cells);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for (child = OF_child(root); child != 0; child = OF_peer(child)) {
|
|
|
|
if (!OF_hasprop(child, "no-map"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rv = OF_getprop(child, "reg", reg, sizeof(reg));
|
|
|
|
if (rv <= 0)
|
|
|
|
/* XXX: Does a no-map of a dynamic range make sense? */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fdt_data_to_res(reg, addr_cells, size_cells,
|
|
|
|
(u_long *)&reserved[i].mr_start,
|
|
|
|
(u_long *)&reserved[i].mr_size);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*mreserved = i;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-06-02 17:17:45 +00:00
|
|
|
int
|
2016-03-01 11:39:07 +00:00
|
|
|
fdt_get_mem_regions(struct mem_region *mr, int *mrcnt, uint64_t *memsize)
|
2010-06-02 17:17:45 +00:00
|
|
|
{
|
|
|
|
pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS];
|
|
|
|
pcell_t *regp;
|
|
|
|
phandle_t memory;
|
2016-03-01 11:39:07 +00:00
|
|
|
uint64_t memory_size;
|
2010-06-02 17:17:45 +00:00
|
|
|
int addr_cells, size_cells;
|
2017-12-25 04:48:39 +00:00
|
|
|
int i, reg_len, rv, tuple_size, tuples;
|
2010-06-02 17:17:45 +00:00
|
|
|
|
|
|
|
memory = OF_finddevice("/memory");
|
2011-12-02 15:24:39 +00:00
|
|
|
if (memory == -1) {
|
2010-06-02 17:17:45 +00:00
|
|
|
rv = ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
|
|
|
|
&size_cells)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (addr_cells > 2) {
|
|
|
|
rv = ERANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
|
|
|
|
reg_len = OF_getproplen(memory, "reg");
|
|
|
|
if (reg_len <= 0 || reg_len > sizeof(reg)) {
|
|
|
|
rv = ERANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OF_getprop(memory, "reg", reg, reg_len) <= 0) {
|
|
|
|
rv = ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memory_size = 0;
|
|
|
|
tuples = reg_len / tuple_size;
|
|
|
|
regp = (pcell_t *)®
|
|
|
|
for (i = 0; i < tuples; i++) {
|
|
|
|
|
|
|
|
rv = fdt_data_to_res(regp, addr_cells, size_cells,
|
|
|
|
(u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size);
|
|
|
|
|
|
|
|
if (rv != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
regp += addr_cells + size_cells;
|
|
|
|
memory_size += mr[i].mr_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memory_size == 0) {
|
|
|
|
rv = ERANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
*mrcnt = i;
|
2016-03-01 09:45:27 +00:00
|
|
|
if (memsize != NULL)
|
|
|
|
*memsize = memory_size;
|
2010-06-02 17:17:45 +00:00
|
|
|
rv = 0;
|
|
|
|
out:
|
|
|
|
return (rv);
|
|
|
|
}
|
2012-09-14 09:36:35 +00:00
|
|
|
|
2016-03-26 06:59:01 +00:00
|
|
|
int
|
|
|
|
fdt_get_chosen_bootargs(char *bootargs, size_t max_size)
|
|
|
|
{
|
|
|
|
phandle_t chosen;
|
|
|
|
|
|
|
|
chosen = OF_finddevice("/chosen");
|
|
|
|
if (chosen == -1)
|
|
|
|
return (ENXIO);
|
|
|
|
if (OF_getprop(chosen, "bootargs", bootargs, max_size) == -1)
|
|
|
|
return (ENXIO);
|
|
|
|
return (0);
|
2016-05-11 18:20:02 +00:00
|
|
|
}
|