2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2017-05-19 02:08:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Google, Inc
|
|
|
|
* Written by Simon Glass <sjg@chromium.org>
|
|
|
|
*/
|
|
|
|
|
2022-09-07 02:27:26 +00:00
|
|
|
#define LOG_CATEGORY LOGC_DT
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <fdtdec.h>
|
|
|
|
#include <fdt_support.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.h>
|
2018-03-04 16:20:11 +00:00
|
|
|
#include <linux/libfdt.h>
|
2017-05-19 02:08:58 +00:00
|
|
|
#include <dm/of_access.h>
|
2017-05-19 02:09:01 +00:00
|
|
|
#include <dm/of_addr.h>
|
2017-05-19 02:08:58 +00:00
|
|
|
#include <dm/ofnode.h>
|
|
|
|
#include <linux/err.h>
|
2017-07-25 14:29:55 +00:00
|
|
|
#include <linux/ioport.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2017-05-19 02:08:58 +00:00
|
|
|
|
2022-09-07 02:27:26 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
#if CONFIG_IS_ENABLED(OFNODE_MULTI_TREE)
|
|
|
|
static void *oftree_list[CONFIG_OFNODE_MULTI_TREE_MAX];
|
|
|
|
static int oftree_count;
|
|
|
|
|
|
|
|
void oftree_reset(void)
|
|
|
|
{
|
|
|
|
if (gd->flags & GD_FLG_RELOC) {
|
|
|
|
oftree_count = 0;
|
|
|
|
oftree_list[oftree_count++] = (void *)gd->fdt_blob;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int oftree_find(const void *fdt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < oftree_count; i++) {
|
|
|
|
if (fdt == oftree_list[i])
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static oftree oftree_ensure(void *fdt)
|
|
|
|
{
|
|
|
|
oftree tree;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (gd->flags & GD_FLG_RELOC) {
|
|
|
|
i = oftree_find(fdt);
|
|
|
|
if (i == -1) {
|
|
|
|
if (oftree_count == CONFIG_OFNODE_MULTI_TREE_MAX) {
|
|
|
|
log_warning("Too many registered device trees (max %d)\n",
|
|
|
|
CONFIG_OFNODE_MULTI_TREE_MAX);
|
|
|
|
return oftree_null();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register the new tree */
|
|
|
|
i = oftree_count++;
|
|
|
|
oftree_list[i] = fdt;
|
|
|
|
log_debug("oftree: registered tree %d: %p\n", i, fdt);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (fdt != gd->fdt_blob) {
|
|
|
|
log_debug("Cannot only access control FDT before relocation\n");
|
|
|
|
return oftree_null();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tree.fdt = fdt;
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ofnode_lookup_fdt(ofnode node)
|
|
|
|
{
|
|
|
|
if (gd->flags & GD_FLG_RELOC) {
|
|
|
|
uint i = OFTREE_TREE_ID(node.of_offset);
|
|
|
|
|
|
|
|
if (i > oftree_count) {
|
|
|
|
log_debug("Invalid tree ID %x\n", i);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return oftree_list[i];
|
|
|
|
} else {
|
|
|
|
return (void *)gd->fdt_blob;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ofnode_to_fdt(ofnode node)
|
|
|
|
{
|
|
|
|
#ifdef OF_CHECKS
|
|
|
|
if (of_live_active())
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && ofnode_valid(node))
|
|
|
|
return ofnode_lookup_fdt(node);
|
|
|
|
|
|
|
|
/* Use the control FDT by default */
|
|
|
|
return (void *)gd->fdt_blob;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ofnode_to_offset() - convert an ofnode to a flat DT offset
|
|
|
|
*
|
|
|
|
* This cannot be called if the reference contains a node pointer.
|
|
|
|
*
|
|
|
|
* @node: Reference containing offset (possibly invalid)
|
|
|
|
* Return: DT offset (can be -1)
|
|
|
|
*/
|
|
|
|
int ofnode_to_offset(ofnode node)
|
|
|
|
{
|
|
|
|
#ifdef OF_CHECKS
|
|
|
|
if (of_live_active())
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && node.of_offset >= 0)
|
|
|
|
return OFTREE_OFFSET(node.of_offset);
|
|
|
|
|
|
|
|
return node.of_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
oftree oftree_from_fdt(void *fdt)
|
|
|
|
{
|
|
|
|
oftree tree;
|
|
|
|
|
|
|
|
if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE))
|
|
|
|
return oftree_ensure(fdt);
|
|
|
|
|
|
|
|
tree.fdt = fdt;
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* noffset_to_ofnode() - convert a DT offset to an ofnode
|
|
|
|
*
|
|
|
|
* @other_node: Node in the same tree to use as a reference
|
|
|
|
* @of_offset: DT offset (either valid, or -1)
|
|
|
|
* Return: reference to the associated DT offset
|
|
|
|
*/
|
|
|
|
ofnode noffset_to_ofnode(ofnode other_node, int of_offset)
|
|
|
|
{
|
|
|
|
ofnode node;
|
|
|
|
|
|
|
|
if (of_live_active())
|
|
|
|
node.np = NULL;
|
|
|
|
else if (!CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) || of_offset < 0 ||
|
|
|
|
!ofnode_valid(other_node))
|
|
|
|
node.of_offset = of_offset;
|
|
|
|
else
|
|
|
|
node.of_offset = OFTREE_MAKE_NODE(other_node.of_offset,
|
|
|
|
of_offset);
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !OFNODE_MULTI_TREE */
|
|
|
|
|
|
|
|
static inline int oftree_find(const void *fdt)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* OFNODE_MULTI_TREE */
|
|
|
|
|
2022-09-07 02:27:24 +00:00
|
|
|
/**
|
|
|
|
* ofnode_from_tree_offset() - get an ofnode from a tree offset (flat tree)
|
|
|
|
*
|
2022-09-07 02:27:26 +00:00
|
|
|
* Looks up the tree and returns an ofnode with the correct of_offset (i.e.
|
|
|
|
* containing the tree ID).
|
2022-09-07 02:27:24 +00:00
|
|
|
*
|
2022-09-07 02:27:26 +00:00
|
|
|
* If @offset is < 0 then this returns an ofnode with that offset and no tree
|
|
|
|
* ID.
|
2022-09-07 02:27:24 +00:00
|
|
|
*
|
|
|
|
* @tree: tree to check
|
|
|
|
* @offset: offset within that tree (can be < 0)
|
2022-09-07 02:27:26 +00:00
|
|
|
* @return node for that offset, with the correct ID
|
2022-09-07 02:27:24 +00:00
|
|
|
*/
|
|
|
|
static ofnode ofnode_from_tree_offset(oftree tree, int offset)
|
|
|
|
{
|
|
|
|
ofnode node;
|
|
|
|
|
2022-09-07 02:27:26 +00:00
|
|
|
if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && offset >= 0) {
|
|
|
|
int tree_id = oftree_find(tree.fdt);
|
|
|
|
|
|
|
|
if (tree_id == -1)
|
|
|
|
return ofnode_null();
|
|
|
|
node.of_offset = OFTREE_NODE(tree_id, offset);
|
|
|
|
} else {
|
|
|
|
node.of_offset = offset;
|
|
|
|
}
|
2022-09-07 02:27:24 +00:00
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-07-21 15:58:30 +00:00
|
|
|
bool ofnode_name_eq(ofnode node, const char *name)
|
|
|
|
{
|
|
|
|
const char *node_name;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
|
|
|
|
node_name = ofnode_get_name(node);
|
|
|
|
len = strchrnul(node_name, '@') - node_name;
|
|
|
|
|
|
|
|
return (strlen(name) == len) && !strncmp(node_name, name, len);
|
|
|
|
}
|
|
|
|
|
2022-06-14 13:21:30 +00:00
|
|
|
int ofnode_read_u8(ofnode node, const char *propname, u8 *outp)
|
|
|
|
{
|
|
|
|
const u8 *cell;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
debug("%s: %s: ", __func__, propname);
|
|
|
|
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return of_read_u8(ofnode_to_np(node), propname, outp);
|
|
|
|
|
|
|
|
cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
|
|
|
|
&len);
|
|
|
|
if (!cell || len < sizeof(*cell)) {
|
|
|
|
debug("(not found)\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
*outp = *cell;
|
|
|
|
debug("%#x (%d)\n", *outp, *outp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def)
|
|
|
|
{
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
ofnode_read_u8(node, propname, &def);
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ofnode_read_u16(ofnode node, const char *propname, u16 *outp)
|
|
|
|
{
|
|
|
|
const fdt16_t *cell;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
debug("%s: %s: ", __func__, propname);
|
|
|
|
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return of_read_u16(ofnode_to_np(node), propname, outp);
|
|
|
|
|
|
|
|
cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
|
|
|
|
&len);
|
|
|
|
if (!cell || len < sizeof(*cell)) {
|
|
|
|
debug("(not found)\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
*outp = be16_to_cpup(cell);
|
|
|
|
debug("%#x (%d)\n", *outp, *outp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def)
|
|
|
|
{
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
ofnode_read_u16(node, propname, &def);
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
|
|
|
|
{
|
2020-03-29 16:04:42 +00:00
|
|
|
return ofnode_read_u32_index(node, propname, 0, outp);
|
2017-05-19 02:08:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 17:48:20 +00:00
|
|
|
u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
|
2017-05-19 02:08:58 +00:00
|
|
|
{
|
|
|
|
assert(ofnode_valid(node));
|
2020-03-29 16:04:42 +00:00
|
|
|
ofnode_read_u32_index(node, propname, 0, &def);
|
2017-05-19 02:08:58 +00:00
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2020-03-29 16:04:41 +00:00
|
|
|
int ofnode_read_u32_index(ofnode node, const char *propname, int index,
|
|
|
|
u32 *outp)
|
|
|
|
{
|
|
|
|
const fdt32_t *cell;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
debug("%s: %s: ", __func__, propname);
|
|
|
|
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return of_read_u32_index(ofnode_to_np(node), propname, index,
|
|
|
|
outp);
|
|
|
|
|
2022-09-07 02:27:20 +00:00
|
|
|
cell = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
|
|
|
|
propname, &len);
|
2020-03-29 16:04:41 +00:00
|
|
|
if (!cell) {
|
|
|
|
debug("(not found)\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < (sizeof(int) * (index + 1))) {
|
|
|
|
debug("(not large enough)\n");
|
|
|
|
return -EOVERFLOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
*outp = fdt32_to_cpu(cell[index]);
|
|
|
|
debug("%#x (%d)\n", *outp, *outp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
|
|
|
|
u32 def)
|
|
|
|
{
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
ofnode_read_u32_index(node, propname, index, &def);
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
|
|
|
|
{
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
ofnode_read_u32(node, propname, (u32 *)&def);
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2018-06-11 19:07:10 +00:00
|
|
|
int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
|
|
|
|
{
|
2019-10-22 08:05:22 +00:00
|
|
|
const unaligned_fdt64_t *cell;
|
2018-06-11 19:07:10 +00:00
|
|
|
int len;
|
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
debug("%s: %s: ", __func__, propname);
|
|
|
|
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return of_read_u64(ofnode_to_np(node), propname, outp);
|
|
|
|
|
2022-09-07 02:27:20 +00:00
|
|
|
cell = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
|
|
|
|
propname, &len);
|
2018-06-11 19:07:10 +00:00
|
|
|
if (!cell || len < sizeof(*cell)) {
|
|
|
|
debug("(not found)\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
*outp = fdt64_to_cpu(cell[0]);
|
|
|
|
debug("%#llx (%lld)\n", (unsigned long long)*outp,
|
|
|
|
(unsigned long long)*outp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-02 14:34:30 +00:00
|
|
|
u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
|
2018-06-11 19:07:10 +00:00
|
|
|
{
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
ofnode_read_u64(node, propname, &def);
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
bool ofnode_read_bool(ofnode node, const char *propname)
|
|
|
|
{
|
2017-06-22 07:54:07 +00:00
|
|
|
const void *prop;
|
2017-05-19 02:08:58 +00:00
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
debug("%s: %s: ", __func__, propname);
|
|
|
|
|
2017-06-22 07:54:07 +00:00
|
|
|
prop = ofnode_get_property(node, propname, NULL);
|
|
|
|
|
|
|
|
debug("%s\n", prop ? "true" : "false");
|
2017-05-19 02:08:58 +00:00
|
|
|
|
2017-06-22 07:54:07 +00:00
|
|
|
return prop ? true : false;
|
2017-05-19 02:08:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:44 +00:00
|
|
|
const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
|
2017-05-19 02:08:58 +00:00
|
|
|
{
|
2020-01-27 15:49:44 +00:00
|
|
|
const char *val = NULL;
|
|
|
|
int len;
|
2017-05-19 02:08:58 +00:00
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
debug("%s: %s: ", __func__, propname);
|
|
|
|
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
struct property *prop = of_find_property(
|
2020-01-27 15:49:44 +00:00
|
|
|
ofnode_to_np(node), propname, &len);
|
2017-05-19 02:08:58 +00:00
|
|
|
|
|
|
|
if (prop) {
|
2020-01-27 15:49:44 +00:00
|
|
|
val = prop->value;
|
2017-05-19 02:08:58 +00:00
|
|
|
len = prop->length;
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-07 02:27:20 +00:00
|
|
|
val = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
|
2017-05-19 02:08:58 +00:00
|
|
|
propname, &len);
|
|
|
|
}
|
2020-01-27 15:49:44 +00:00
|
|
|
if (!val) {
|
2017-05-19 02:08:58 +00:00
|
|
|
debug("<not found>\n");
|
2020-01-27 15:49:44 +00:00
|
|
|
if (sizep)
|
|
|
|
*sizep = -FDT_ERR_NOTFOUND;
|
2017-05-19 02:08:58 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-01-27 15:49:44 +00:00
|
|
|
if (sizep)
|
|
|
|
*sizep = len;
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ofnode_read_string(ofnode node, const char *propname)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
str = ofnode_read_prop(node, propname, &len);
|
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
if (strnlen(str, len) >= len) {
|
|
|
|
debug("<invalid>\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
debug("%s\n", str);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:45 +00:00
|
|
|
int ofnode_read_size(ofnode node, const char *propname)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!ofnode_read_prop(node, propname, &len))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
|
|
|
|
{
|
|
|
|
ofnode subnode;
|
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
debug("%s: %s: ", __func__, subnode_name);
|
|
|
|
|
|
|
|
if (ofnode_is_np(node)) {
|
2022-09-07 02:27:04 +00:00
|
|
|
struct device_node *np = ofnode_to_np(node);
|
2017-05-19 02:08:58 +00:00
|
|
|
|
|
|
|
for (np = np->child; np; np = np->sibling) {
|
|
|
|
if (!strcmp(subnode_name, np->name))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
subnode = np_to_ofnode(np);
|
|
|
|
} else {
|
2022-09-07 02:27:20 +00:00
|
|
|
int ooffset = fdt_subnode_offset(ofnode_to_fdt(node),
|
2017-05-19 02:08:58 +00:00
|
|
|
ofnode_to_offset(node), subnode_name);
|
2022-09-07 02:27:23 +00:00
|
|
|
subnode = noffset_to_ofnode(node, ooffset);
|
2017-05-19 02:08:58 +00:00
|
|
|
}
|
|
|
|
debug("%s\n", ofnode_valid(subnode) ?
|
|
|
|
ofnode_get_name(subnode) : "<none>");
|
|
|
|
|
|
|
|
return subnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ofnode_read_u32_array(ofnode node, const char *propname,
|
|
|
|
u32 *out_values, size_t sz)
|
|
|
|
{
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
debug("%s: %s: ", __func__, propname);
|
|
|
|
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
return of_read_u32_array(ofnode_to_np(node), propname,
|
|
|
|
out_values, sz);
|
|
|
|
} else {
|
2022-09-07 02:27:18 +00:00
|
|
|
int ret;
|
|
|
|
|
2022-09-07 02:27:20 +00:00
|
|
|
ret = fdtdec_get_int_array(ofnode_to_fdt(node),
|
2022-09-07 02:27:18 +00:00
|
|
|
ofnode_to_offset(node), propname,
|
|
|
|
out_values, sz);
|
|
|
|
|
|
|
|
/* get the error right, but space is more important in SPL */
|
|
|
|
if (!IS_ENABLED(CONFIG_SPL_BUILD)) {
|
|
|
|
if (ret == -FDT_ERR_NOTFOUND)
|
|
|
|
return -EINVAL;
|
|
|
|
else if (ret == -FDT_ERR_BADLAYOUT)
|
|
|
|
return -EOVERFLOW;
|
|
|
|
}
|
|
|
|
return ret;
|
2017-05-19 02:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:25:06 +00:00
|
|
|
#if !CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
|
2020-11-29 00:50:02 +00:00
|
|
|
bool ofnode_is_enabled(ofnode node)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
return of_device_is_available(ofnode_to_np(node));
|
|
|
|
} else {
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdtdec_get_is_enabled(ofnode_to_fdt(node),
|
2020-11-29 00:50:02 +00:00
|
|
|
ofnode_to_offset(node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
ofnode ofnode_first_subnode(ofnode node)
|
|
|
|
{
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return np_to_ofnode(node.np->child);
|
|
|
|
|
2022-09-07 02:27:23 +00:00
|
|
|
return noffset_to_ofnode(node,
|
2022-09-07 02:27:20 +00:00
|
|
|
fdt_first_subnode(ofnode_to_fdt(node), ofnode_to_offset(node)));
|
2017-05-19 02:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ofnode ofnode_next_subnode(ofnode node)
|
|
|
|
{
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return np_to_ofnode(node.np->sibling);
|
|
|
|
|
2022-09-07 02:27:23 +00:00
|
|
|
return noffset_to_ofnode(node,
|
2022-09-07 02:27:20 +00:00
|
|
|
fdt_next_subnode(ofnode_to_fdt(node), ofnode_to_offset(node)));
|
2017-05-19 02:08:58 +00:00
|
|
|
}
|
2020-12-17 00:25:06 +00:00
|
|
|
#endif /* !DM_INLINE_OFNODE */
|
2017-05-19 02:08:58 +00:00
|
|
|
|
2018-02-23 16:38:49 +00:00
|
|
|
ofnode ofnode_get_parent(ofnode node)
|
|
|
|
{
|
|
|
|
ofnode parent;
|
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
|
|
|
|
else
|
2022-09-07 02:27:20 +00:00
|
|
|
parent.of_offset = fdt_parent_offset(ofnode_to_fdt(node),
|
2018-02-23 16:38:49 +00:00
|
|
|
ofnode_to_offset(node));
|
|
|
|
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
const char *ofnode_get_name(ofnode node)
|
|
|
|
{
|
2019-07-19 03:23:47 +00:00
|
|
|
if (!ofnode_valid(node)) {
|
|
|
|
debug("%s node not valid\n", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
if (ofnode_is_np(node))
|
2022-09-07 02:27:15 +00:00
|
|
|
return node.np->name;
|
2017-05-19 02:08:58 +00:00
|
|
|
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdt_get_name(ofnode_to_fdt(node), ofnode_to_offset(node), NULL);
|
2017-05-19 02:08:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 12:08:18 +00:00
|
|
|
int ofnode_get_path(ofnode node, char *buf, int buflen)
|
|
|
|
{
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
if (strlen(node.np->full_name) >= buflen)
|
|
|
|
return -ENOSPC;
|
|
|
|
|
|
|
|
strcpy(buf, node.np->full_name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
int res;
|
|
|
|
|
2022-09-07 02:27:20 +00:00
|
|
|
res = fdt_get_path(ofnode_to_fdt(node), ofnode_to_offset(node), buf,
|
2021-05-26 12:08:18 +00:00
|
|
|
buflen);
|
|
|
|
if (!res)
|
|
|
|
return res;
|
|
|
|
else if (res == -FDT_ERR_NOSPACE)
|
|
|
|
return -ENOSPC;
|
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 16:38:50 +00:00
|
|
|
ofnode ofnode_get_by_phandle(uint phandle)
|
|
|
|
{
|
|
|
|
ofnode node;
|
|
|
|
|
|
|
|
if (of_live_active())
|
2022-09-07 02:26:57 +00:00
|
|
|
node = np_to_ofnode(of_find_node_by_phandle(NULL, phandle));
|
2018-02-23 16:38:50 +00:00
|
|
|
else
|
|
|
|
node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
|
|
|
|
phandle);
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2022-09-07 02:27:22 +00:00
|
|
|
ofnode oftree_get_by_phandle(oftree tree, uint phandle)
|
|
|
|
{
|
|
|
|
ofnode node;
|
|
|
|
|
|
|
|
if (of_live_active())
|
|
|
|
node = np_to_ofnode(of_find_node_by_phandle(tree.np, phandle));
|
|
|
|
else
|
2022-09-07 02:27:27 +00:00
|
|
|
node = ofnode_from_tree_offset(tree,
|
2022-09-07 02:27:22 +00:00
|
|
|
fdt_node_offset_by_phandle(oftree_lookup_fdt(tree),
|
2022-09-07 02:27:27 +00:00
|
|
|
phandle));
|
2022-09-07 02:27:22 +00:00
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-05-26 12:08:17 +00:00
|
|
|
static fdt_addr_t __ofnode_get_addr_size_index(ofnode node, int index,
|
|
|
|
fdt_size_t *size, bool translate)
|
2017-05-19 02:09:01 +00:00
|
|
|
{
|
2018-11-19 06:14:47 +00:00
|
|
|
int na, ns;
|
|
|
|
|
2021-07-12 07:40:20 +00:00
|
|
|
if (size)
|
|
|
|
*size = FDT_SIZE_T_NONE;
|
2021-04-12 06:51:11 +00:00
|
|
|
|
2017-05-19 02:09:01 +00:00
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
const __be32 *prop_val;
|
2019-09-25 14:55:50 +00:00
|
|
|
u64 size64;
|
2017-05-19 02:09:01 +00:00
|
|
|
uint flags;
|
|
|
|
|
2019-09-25 14:55:50 +00:00
|
|
|
prop_val = of_get_address(ofnode_to_np(node), index, &size64,
|
|
|
|
&flags);
|
2017-05-19 02:09:01 +00:00
|
|
|
if (!prop_val)
|
|
|
|
return FDT_ADDR_T_NONE;
|
2021-07-12 07:40:20 +00:00
|
|
|
|
2019-09-25 14:55:50 +00:00
|
|
|
if (size)
|
|
|
|
*size = size64;
|
2017-12-20 08:52:12 +00:00
|
|
|
|
2018-03-12 13:53:33 +00:00
|
|
|
ns = of_n_size_cells(ofnode_to_np(node));
|
|
|
|
|
2021-05-26 12:08:17 +00:00
|
|
|
if (translate && IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
|
2017-12-20 08:52:12 +00:00
|
|
|
return of_translate_address(ofnode_to_np(node), prop_val);
|
|
|
|
} else {
|
|
|
|
na = of_n_addr_cells(ofnode_to_np(node));
|
|
|
|
return of_read_number(prop_val, na);
|
|
|
|
}
|
2017-05-19 02:09:01 +00:00
|
|
|
} else {
|
2018-11-19 06:14:47 +00:00
|
|
|
na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
|
|
|
|
ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdtdec_get_addr_size_fixed(ofnode_to_fdt(node),
|
2018-11-19 06:14:47 +00:00
|
|
|
ofnode_to_offset(node), "reg",
|
2021-05-26 12:08:17 +00:00
|
|
|
index, na, ns, size,
|
|
|
|
translate);
|
2017-05-19 02:09:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 12:08:17 +00:00
|
|
|
fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
|
|
|
|
{
|
|
|
|
return __ofnode_get_addr_size_index(node, index, size, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fdt_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
|
|
|
|
fdt_size_t *size)
|
|
|
|
{
|
|
|
|
return __ofnode_get_addr_size_index(node, index, size, false);
|
|
|
|
}
|
|
|
|
|
2019-04-24 11:49:53 +00:00
|
|
|
fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
|
|
|
|
{
|
|
|
|
fdt_size_t size;
|
|
|
|
|
|
|
|
return ofnode_get_addr_size_index(node, index, &size);
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:09:01 +00:00
|
|
|
fdt_addr_t ofnode_get_addr(ofnode node)
|
|
|
|
{
|
|
|
|
return ofnode_get_addr_index(node, 0);
|
|
|
|
}
|
|
|
|
|
2021-04-12 06:51:11 +00:00
|
|
|
fdt_size_t ofnode_get_size(ofnode node)
|
|
|
|
{
|
|
|
|
fdt_size_t size;
|
|
|
|
|
|
|
|
ofnode_get_addr_size_index(node, 0, &size);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
int ofnode_stringlist_search(ofnode node, const char *property,
|
|
|
|
const char *string)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
return of_property_match_string(ofnode_to_np(node),
|
|
|
|
property, string);
|
|
|
|
} else {
|
|
|
|
int ret;
|
|
|
|
|
2022-09-07 02:27:20 +00:00
|
|
|
ret = fdt_stringlist_search(ofnode_to_fdt(node),
|
2017-05-19 02:08:58 +00:00
|
|
|
ofnode_to_offset(node), property,
|
|
|
|
string);
|
|
|
|
if (ret == -FDT_ERR_NOTFOUND)
|
|
|
|
return -ENODATA;
|
|
|
|
else if (ret < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ofnode_read_string_index(ofnode node, const char *property, int index,
|
|
|
|
const char **outp)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
return of_property_read_string_index(ofnode_to_np(node),
|
|
|
|
property, index, outp);
|
|
|
|
} else {
|
|
|
|
int len;
|
|
|
|
|
2022-09-07 02:27:20 +00:00
|
|
|
*outp = fdt_stringlist_get(ofnode_to_fdt(node),
|
|
|
|
ofnode_to_offset(node),
|
2017-05-19 02:08:58 +00:00
|
|
|
property, index, &len);
|
|
|
|
if (len < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 12:21:28 +00:00
|
|
|
int ofnode_read_string_count(ofnode node, const char *property)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
return of_property_count_strings(ofnode_to_np(node), property);
|
|
|
|
} else {
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdt_stringlist_count(ofnode_to_fdt(node),
|
2017-06-12 12:21:28 +00:00
|
|
|
ofnode_to_offset(node), property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:26:07 +00:00
|
|
|
int ofnode_read_string_list(ofnode node, const char *property,
|
|
|
|
const char ***listp)
|
|
|
|
{
|
|
|
|
const char **prop;
|
|
|
|
int count;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
*listp = NULL;
|
|
|
|
count = ofnode_read_string_count(node, property);
|
|
|
|
if (count < 0)
|
|
|
|
return count;
|
|
|
|
if (!count)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
prop = calloc(count + 1, sizeof(char *));
|
|
|
|
if (!prop)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
ofnode_read_string_index(node, property, i, &prop[i]);
|
|
|
|
prop[count] = NULL;
|
|
|
|
*listp = prop;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
|
|
|
|
struct ofnode_phandle_args *out)
|
|
|
|
{
|
|
|
|
assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
|
|
|
|
out->node = offset_to_ofnode(in->node);
|
|
|
|
out->args_count = in->args_count;
|
|
|
|
memcpy(out->args, in->args, sizeof(out->args));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
|
|
|
|
struct ofnode_phandle_args *out)
|
|
|
|
{
|
|
|
|
assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
|
|
|
|
out->node = np_to_ofnode(in->np);
|
|
|
|
out->args_count = in->args_count;
|
|
|
|
memcpy(out->args, in->args, sizeof(out->args));
|
|
|
|
}
|
|
|
|
|
|
|
|
int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
|
|
|
|
const char *cells_name, int cell_count,
|
|
|
|
int index,
|
|
|
|
struct ofnode_phandle_args *out_args)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
struct of_phandle_args args;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = of_parse_phandle_with_args(ofnode_to_np(node),
|
2020-09-10 16:26:17 +00:00
|
|
|
list_name, cells_name,
|
|
|
|
cell_count, index,
|
2018-01-15 10:07:17 +00:00
|
|
|
&args);
|
2017-05-19 02:08:58 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ofnode_from_of_phandle_args(&args, out_args);
|
|
|
|
} else {
|
|
|
|
struct fdtdec_phandle_args args;
|
|
|
|
int ret;
|
|
|
|
|
2022-09-07 02:27:20 +00:00
|
|
|
ret = fdtdec_parse_phandle_with_args(ofnode_to_fdt(node),
|
2018-01-15 10:07:17 +00:00
|
|
|
ofnode_to_offset(node),
|
|
|
|
list_name, cells_name,
|
|
|
|
cell_count, index, &args);
|
2017-05-19 02:08:58 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ofnode_from_fdtdec_phandle_args(&args, out_args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-18 09:57:08 +00:00
|
|
|
int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
|
2020-09-25 07:41:14 +00:00
|
|
|
const char *cells_name, int cell_count)
|
2017-07-18 09:57:08 +00:00
|
|
|
{
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return of_count_phandle_with_args(ofnode_to_np(node),
|
2020-09-25 07:41:14 +00:00
|
|
|
list_name, cells_name, cell_count);
|
2017-07-18 09:57:08 +00:00
|
|
|
else
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdtdec_parse_phandle_with_args(ofnode_to_fdt(node),
|
2017-07-18 09:57:08 +00:00
|
|
|
ofnode_to_offset(node), list_name, cells_name,
|
2020-09-25 07:41:14 +00:00
|
|
|
cell_count, -1, NULL);
|
2017-07-18 09:57:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
ofnode ofnode_path(const char *path)
|
|
|
|
{
|
|
|
|
if (of_live_active())
|
|
|
|
return np_to_ofnode(of_find_node_by_path(path));
|
|
|
|
else
|
|
|
|
return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
|
|
|
|
}
|
|
|
|
|
2022-09-07 02:27:24 +00:00
|
|
|
ofnode oftree_root(oftree tree)
|
2022-07-30 21:52:08 +00:00
|
|
|
{
|
2022-09-07 02:27:24 +00:00
|
|
|
if (of_live_active()) {
|
|
|
|
return np_to_ofnode(tree.np);
|
|
|
|
} else {
|
|
|
|
return ofnode_from_tree_offset(tree, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ofnode oftree_path(oftree tree, const char *path)
|
|
|
|
{
|
|
|
|
if (of_live_active()) {
|
2022-07-30 21:52:08 +00:00
|
|
|
return np_to_ofnode(of_find_node_opts_by_path(tree.np, path,
|
|
|
|
NULL));
|
2022-09-07 02:27:24 +00:00
|
|
|
} else if (*path != '/' && tree.fdt != gd->fdt_blob) {
|
2022-07-30 21:52:08 +00:00
|
|
|
return ofnode_null(); /* Aliases only on control FDT */
|
2022-09-07 02:27:24 +00:00
|
|
|
} else {
|
|
|
|
int offset = fdt_path_offset(tree.fdt, path);
|
|
|
|
|
|
|
|
return ofnode_from_tree_offset(tree, offset);
|
|
|
|
}
|
2022-07-30 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:46 +00:00
|
|
|
const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
|
2017-05-19 02:08:58 +00:00
|
|
|
{
|
|
|
|
ofnode chosen_node;
|
|
|
|
|
|
|
|
chosen_node = ofnode_path("/chosen");
|
|
|
|
|
2020-01-27 15:49:46 +00:00
|
|
|
return ofnode_read_prop(chosen_node, propname, sizep);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ofnode_read_chosen_string(const char *propname)
|
|
|
|
{
|
|
|
|
return ofnode_read_chosen_prop(propname, NULL);
|
2017-05-19 02:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ofnode ofnode_get_chosen_node(const char *name)
|
|
|
|
{
|
|
|
|
const char *prop;
|
|
|
|
|
2020-01-27 15:49:46 +00:00
|
|
|
prop = ofnode_read_chosen_prop(name, NULL);
|
2017-05-19 02:08:58 +00:00
|
|
|
if (!prop)
|
|
|
|
return ofnode_null();
|
|
|
|
|
|
|
|
return ofnode_path(prop);
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:51:08 +00:00
|
|
|
const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
|
|
|
|
{
|
|
|
|
ofnode node;
|
|
|
|
|
|
|
|
node = ofnode_path("/aliases");
|
|
|
|
|
|
|
|
return ofnode_read_prop(node, propname, sizep);
|
|
|
|
}
|
|
|
|
|
|
|
|
ofnode ofnode_get_aliases_node(const char *name)
|
|
|
|
{
|
|
|
|
const char *prop;
|
|
|
|
|
|
|
|
prop = ofnode_read_aliases_prop(name, NULL);
|
|
|
|
if (!prop)
|
|
|
|
return ofnode_null();
|
|
|
|
|
|
|
|
debug("%s: node_path: %s\n", __func__, prop);
|
|
|
|
|
|
|
|
return ofnode_path(prop);
|
|
|
|
}
|
|
|
|
|
2020-05-02 09:35:09 +00:00
|
|
|
int ofnode_get_child_count(ofnode parent)
|
|
|
|
{
|
|
|
|
ofnode child;
|
|
|
|
int num = 0;
|
|
|
|
|
|
|
|
ofnode_for_each_subnode(child, parent)
|
|
|
|
num++;
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
static int decode_timing_property(ofnode node, const char *name,
|
|
|
|
struct timing_entry *result)
|
|
|
|
{
|
|
|
|
int length, ret = 0;
|
|
|
|
|
|
|
|
length = ofnode_read_size(node, name);
|
|
|
|
if (length < 0) {
|
|
|
|
debug("%s: could not find property %s\n",
|
|
|
|
ofnode_get_name(node), name);
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length == sizeof(u32)) {
|
|
|
|
result->typ = ofnode_read_u32_default(node, name, 0);
|
|
|
|
result->min = result->typ;
|
|
|
|
result->max = result->typ;
|
|
|
|
} else {
|
|
|
|
ret = ofnode_read_u32_array(node, name, &result->min, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ofnode_decode_display_timing(ofnode parent, int index,
|
|
|
|
struct display_timing *dt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ofnode timings, node;
|
|
|
|
u32 val = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
timings = ofnode_find_subnode(parent, "display-timings");
|
|
|
|
if (!ofnode_valid(timings))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-08-05 21:45:54 +00:00
|
|
|
i = 0;
|
|
|
|
ofnode_for_each_subnode(node, timings) {
|
|
|
|
if (i++ == index)
|
|
|
|
break;
|
|
|
|
}
|
2017-05-19 02:08:58 +00:00
|
|
|
|
|
|
|
if (!ofnode_valid(node))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
memset(dt, 0, sizeof(*dt));
|
|
|
|
|
|
|
|
ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
|
|
|
|
ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
|
|
|
|
ret |= decode_timing_property(node, "hactive", &dt->hactive);
|
|
|
|
ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
|
|
|
|
ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
|
|
|
|
ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
|
|
|
|
ret |= decode_timing_property(node, "vactive", &dt->vactive);
|
|
|
|
ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
|
|
|
|
ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
|
|
|
|
|
|
|
|
dt->flags = 0;
|
|
|
|
val = ofnode_read_u32_default(node, "vsync-active", -1);
|
|
|
|
if (val != -1) {
|
|
|
|
dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
|
|
|
|
DISPLAY_FLAGS_VSYNC_LOW;
|
|
|
|
}
|
|
|
|
val = ofnode_read_u32_default(node, "hsync-active", -1);
|
|
|
|
if (val != -1) {
|
|
|
|
dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
|
|
|
|
DISPLAY_FLAGS_HSYNC_LOW;
|
|
|
|
}
|
|
|
|
val = ofnode_read_u32_default(node, "de-active", -1);
|
|
|
|
if (val != -1) {
|
|
|
|
dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
|
|
|
|
DISPLAY_FLAGS_DE_LOW;
|
|
|
|
}
|
|
|
|
val = ofnode_read_u32_default(node, "pixelclk-active", -1);
|
|
|
|
if (val != -1) {
|
|
|
|
dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
|
|
|
|
DISPLAY_FLAGS_PIXDATA_NEGEDGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ofnode_read_bool(node, "interlaced"))
|
|
|
|
dt->flags |= DISPLAY_FLAGS_INTERLACED;
|
|
|
|
if (ofnode_read_bool(node, "doublescan"))
|
|
|
|
dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
|
|
|
|
if (ofnode_read_bool(node, "doubleclk"))
|
|
|
|
dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-22 07:54:05 +00:00
|
|
|
const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
|
2017-05-19 02:08:58 +00:00
|
|
|
{
|
2017-06-22 07:54:04 +00:00
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return of_get_property(ofnode_to_np(node), propname, lenp);
|
|
|
|
else
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
|
2017-05-19 02:08:58 +00:00
|
|
|
propname, lenp);
|
|
|
|
}
|
|
|
|
|
2022-09-07 02:27:13 +00:00
|
|
|
int ofnode_first_property(ofnode node, struct ofprop *prop)
|
2020-01-13 10:34:56 +00:00
|
|
|
{
|
|
|
|
prop->node = node;
|
|
|
|
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
prop->prop = of_get_first_property(ofnode_to_np(prop->node));
|
|
|
|
if (!prop->prop)
|
|
|
|
return -FDT_ERR_NOTFOUND;
|
|
|
|
} else {
|
|
|
|
prop->offset =
|
2022-09-07 02:27:20 +00:00
|
|
|
fdt_first_property_offset(ofnode_to_fdt(node),
|
2020-01-13 10:34:56 +00:00
|
|
|
ofnode_to_offset(prop->node));
|
|
|
|
if (prop->offset < 0)
|
|
|
|
return prop->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-07 02:27:13 +00:00
|
|
|
int ofnode_next_property(struct ofprop *prop)
|
2020-01-13 10:34:56 +00:00
|
|
|
{
|
|
|
|
if (ofnode_is_np(prop->node)) {
|
|
|
|
prop->prop = of_get_next_property(ofnode_to_np(prop->node),
|
|
|
|
prop->prop);
|
|
|
|
if (!prop->prop)
|
|
|
|
return -FDT_ERR_NOTFOUND;
|
|
|
|
} else {
|
2022-09-07 02:27:20 +00:00
|
|
|
prop->offset =
|
|
|
|
fdt_next_property_offset(ofnode_to_fdt(prop->node),
|
|
|
|
prop->offset);
|
2020-01-13 10:34:56 +00:00
|
|
|
if (prop->offset < 0)
|
|
|
|
return prop->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-07 02:27:14 +00:00
|
|
|
const void *ofprop_get_property(const struct ofprop *prop,
|
|
|
|
const char **propname, int *lenp)
|
2020-01-13 10:34:56 +00:00
|
|
|
{
|
|
|
|
if (ofnode_is_np(prop->node))
|
|
|
|
return of_get_property_by_prop(ofnode_to_np(prop->node),
|
|
|
|
prop->prop, propname, lenp);
|
|
|
|
else
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdt_getprop_by_offset(ofnode_to_fdt(prop->node),
|
2020-01-13 10:34:56 +00:00
|
|
|
prop->offset,
|
|
|
|
propname, lenp);
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
|
|
|
|
fdt_size_t *sizep)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
int na, ns;
|
|
|
|
int psize;
|
|
|
|
const struct device_node *np = ofnode_to_np(node);
|
2017-09-20 11:50:41 +00:00
|
|
|
const __be32 *prop = of_get_property(np, property, &psize);
|
2017-05-19 02:08:58 +00:00
|
|
|
|
2017-09-20 11:50:41 +00:00
|
|
|
if (!prop)
|
|
|
|
return FDT_ADDR_T_NONE;
|
2017-05-19 02:08:58 +00:00
|
|
|
na = of_n_addr_cells(np);
|
2018-10-01 10:37:19 +00:00
|
|
|
ns = of_n_size_cells(np);
|
2017-05-19 02:09:27 +00:00
|
|
|
*sizep = of_read_number(prop + na, ns);
|
2018-10-01 10:37:20 +00:00
|
|
|
|
2021-05-01 15:05:26 +00:00
|
|
|
if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
|
2018-10-01 10:37:20 +00:00
|
|
|
return of_translate_address(np, prop);
|
|
|
|
else
|
|
|
|
return of_read_number(prop, na);
|
2017-05-19 02:08:58 +00:00
|
|
|
} else {
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdtdec_get_addr_size(ofnode_to_fdt(node),
|
2017-05-19 02:08:58 +00:00
|
|
|
ofnode_to_offset(node), property,
|
|
|
|
sizep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
|
|
|
|
size_t sz)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
const struct device_node *np = ofnode_to_np(node);
|
|
|
|
int psize;
|
|
|
|
const __be32 *prop = of_get_property(np, propname, &psize);
|
|
|
|
|
|
|
|
if (!prop || sz != psize)
|
|
|
|
return NULL;
|
|
|
|
return (uint8_t *)prop;
|
|
|
|
|
|
|
|
} else {
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdtdec_locate_byte_array(ofnode_to_fdt(node),
|
2017-05-19 02:08:58 +00:00
|
|
|
ofnode_to_offset(node), propname, sz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
|
|
|
|
const char *propname, struct fdt_pci_addr *addr)
|
|
|
|
{
|
2017-06-22 08:57:50 +00:00
|
|
|
const fdt32_t *cell;
|
2017-05-19 02:08:58 +00:00
|
|
|
int len;
|
|
|
|
int ret = -ENOENT;
|
|
|
|
|
|
|
|
debug("%s: %s: ", __func__, propname);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we follow the pci bus bindings strictly, we should check
|
|
|
|
* the value of the node's parent node's #address-cells and
|
|
|
|
* #size-cells. They need to be 3 and 2 accordingly. However,
|
|
|
|
* for simplicity we skip the check here.
|
|
|
|
*/
|
2017-06-22 07:54:05 +00:00
|
|
|
cell = ofnode_get_property(node, propname, &len);
|
2017-05-19 02:08:58 +00:00
|
|
|
if (!cell)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if ((len % FDT_PCI_REG_SIZE) == 0) {
|
|
|
|
int num = len / FDT_PCI_REG_SIZE;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
debug("pci address #%d: %08lx %08lx %08lx\n", i,
|
|
|
|
(ulong)fdt32_to_cpu(cell[0]),
|
|
|
|
(ulong)fdt32_to_cpu(cell[1]),
|
|
|
|
(ulong)fdt32_to_cpu(cell[2]));
|
|
|
|
if ((fdt32_to_cpu(*cell) & type) == type) {
|
|
|
|
addr->phys_hi = fdt32_to_cpu(cell[0]);
|
|
|
|
addr->phys_mid = fdt32_to_cpu(cell[1]);
|
2019-09-25 14:55:46 +00:00
|
|
|
addr->phys_lo = fdt32_to_cpu(cell[2]);
|
2017-05-19 02:08:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-01-15 10:07:17 +00:00
|
|
|
|
|
|
|
cell += (FDT_PCI_ADDR_CELLS +
|
|
|
|
FDT_PCI_SIZE_CELLS);
|
2017-05-19 02:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i == num) {
|
|
|
|
ret = -ENXIO;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-15 10:07:17 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
fail:
|
|
|
|
debug("(not found)\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-08-03 08:14:35 +00:00
|
|
|
int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
|
|
|
|
{
|
|
|
|
const char *list, *end;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
list = ofnode_get_property(node, "compatible", &len);
|
|
|
|
if (!list)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
end = list + len;
|
|
|
|
while (list < end) {
|
|
|
|
len = strlen(list);
|
|
|
|
if (len >= strlen("pciVVVV,DDDD")) {
|
|
|
|
char *s = strstr(list, "pci");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check if the string is something like pciVVVV,DDDD.RR
|
|
|
|
* or just pciVVVV,DDDD
|
|
|
|
*/
|
|
|
|
if (s && s[7] == ',' &&
|
|
|
|
(s[12] == '.' || s[12] == 0)) {
|
|
|
|
s += 3;
|
|
|
|
*vendor = simple_strtol(s, NULL, 16);
|
|
|
|
|
|
|
|
s += 5;
|
|
|
|
*device = simple_strtol(s, NULL, 16);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list += (len + 1);
|
2022-02-23 14:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device)
|
|
|
|
{
|
|
|
|
const char *list, *end;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
list = ofnode_get_property(node, "compatible", &len);
|
|
|
|
|
|
|
|
if (!list)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
end = list + len;
|
|
|
|
while (list < end) {
|
|
|
|
len = strlen(list);
|
|
|
|
|
|
|
|
if (len >= strlen("ethernet-phy-idVVVV,DDDD")) {
|
|
|
|
char *s = strstr(list, "ethernet-phy-id");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check if the string is something like
|
|
|
|
* ethernet-phy-idVVVV,DDDD
|
|
|
|
*/
|
|
|
|
if (s && s[19] == '.') {
|
|
|
|
s += strlen("ethernet-phy-id");
|
|
|
|
*vendor = simple_strtol(s, NULL, 16);
|
|
|
|
s += 5;
|
|
|
|
*device = simple_strtol(s, NULL, 16);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list += (len + 1);
|
2018-08-03 08:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:08:58 +00:00
|
|
|
int ofnode_read_addr_cells(ofnode node)
|
|
|
|
{
|
2020-07-25 19:38:49 +00:00
|
|
|
if (ofnode_is_np(node)) {
|
2017-05-19 02:08:58 +00:00
|
|
|
return of_n_addr_cells(ofnode_to_np(node));
|
2020-07-25 19:38:49 +00:00
|
|
|
} else {
|
2022-09-07 02:27:20 +00:00
|
|
|
int parent = fdt_parent_offset(ofnode_to_fdt(node),
|
2020-07-25 19:38:49 +00:00
|
|
|
ofnode_to_offset(node));
|
|
|
|
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdt_address_cells(ofnode_to_fdt(node), parent);
|
2020-07-25 19:38:49 +00:00
|
|
|
}
|
2017-05-19 02:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ofnode_read_size_cells(ofnode node)
|
|
|
|
{
|
2020-07-25 19:38:49 +00:00
|
|
|
if (ofnode_is_np(node)) {
|
2017-05-19 02:08:58 +00:00
|
|
|
return of_n_size_cells(ofnode_to_np(node));
|
2020-07-25 19:38:49 +00:00
|
|
|
} else {
|
2022-09-07 02:27:20 +00:00
|
|
|
int parent = fdt_parent_offset(ofnode_to_fdt(node),
|
2020-07-25 19:38:49 +00:00
|
|
|
ofnode_to_offset(node));
|
|
|
|
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdt_size_cells(ofnode_to_fdt(node), parent);
|
2020-07-25 19:38:49 +00:00
|
|
|
}
|
2017-06-12 12:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ofnode_read_simple_addr_cells(ofnode node)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return of_simple_addr_cells(ofnode_to_np(node));
|
|
|
|
else
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdt_address_cells(ofnode_to_fdt(node),
|
|
|
|
ofnode_to_offset(node));
|
2017-06-12 12:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ofnode_read_simple_size_cells(ofnode node)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return of_simple_size_cells(ofnode_to_np(node));
|
2017-05-19 02:08:58 +00:00
|
|
|
else
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdt_size_cells(ofnode_to_fdt(node),
|
|
|
|
ofnode_to_offset(node));
|
2017-05-19 02:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ofnode_pre_reloc(ofnode node)
|
|
|
|
{
|
2019-02-11 11:49:57 +00:00
|
|
|
#if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
|
|
|
|
/* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
|
|
|
|
* had property dm-pre-reloc or u-boot,dm-spl/tpl.
|
|
|
|
* They are removed in final dtb (fdtgrep 2nd pass)
|
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
#else
|
2017-06-22 07:54:03 +00:00
|
|
|
if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
|
2017-05-19 02:08:58 +00:00
|
|
|
return true;
|
2018-10-01 18:22:18 +00:00
|
|
|
if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
|
|
|
|
return true;
|
2017-05-19 02:08:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In regular builds individual spl and tpl handling both
|
|
|
|
* count as handled pre-relocation for later second init.
|
|
|
|
*/
|
2017-06-22 07:54:03 +00:00
|
|
|
if (ofnode_read_bool(node, "u-boot,dm-spl") ||
|
|
|
|
ofnode_read_bool(node, "u-boot,dm-tpl"))
|
2017-05-19 02:08:58 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2019-02-11 11:49:57 +00:00
|
|
|
#endif
|
2017-05-19 02:08:58 +00:00
|
|
|
}
|
2017-07-25 14:29:55 +00:00
|
|
|
|
|
|
|
int ofnode_read_resource(ofnode node, uint index, struct resource *res)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
return of_address_to_resource(ofnode_to_np(node), index, res);
|
|
|
|
} else {
|
|
|
|
struct fdt_resource fres;
|
|
|
|
int ret;
|
|
|
|
|
2022-09-07 02:27:20 +00:00
|
|
|
ret = fdt_get_resource(ofnode_to_fdt(node),
|
|
|
|
ofnode_to_offset(node),
|
2017-07-25 14:29:55 +00:00
|
|
|
"reg", index, &fres);
|
|
|
|
if (ret < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
memset(res, '\0', sizeof(*res));
|
|
|
|
res->start = fres.start;
|
|
|
|
res->end = fres.end;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2017-08-25 16:12:30 +00:00
|
|
|
|
|
|
|
int ofnode_read_resource_byname(ofnode node, const char *name,
|
|
|
|
struct resource *res)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
index = ofnode_stringlist_search(node, "reg-names", name);
|
|
|
|
if (index < 0)
|
|
|
|
return index;
|
|
|
|
|
|
|
|
return ofnode_read_resource(node, index, res);
|
|
|
|
}
|
2018-01-15 10:07:19 +00:00
|
|
|
|
|
|
|
u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return of_translate_address(ofnode_to_np(node), in_addr);
|
|
|
|
else
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdt_translate_address(ofnode_to_fdt(node),
|
|
|
|
ofnode_to_offset(node), in_addr);
|
2018-01-15 10:07:19 +00:00
|
|
|
}
|
2018-04-19 03:14:02 +00:00
|
|
|
|
2019-05-31 13:11:30 +00:00
|
|
|
u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return of_translate_dma_address(ofnode_to_np(node), in_addr);
|
|
|
|
else
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdt_translate_dma_address(ofnode_to_fdt(node),
|
|
|
|
ofnode_to_offset(node), in_addr);
|
2019-05-31 13:11:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 12:55:22 +00:00
|
|
|
int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus, u64 *size)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return of_get_dma_range(ofnode_to_np(node), cpu, bus, size);
|
|
|
|
else
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdt_get_dma_range(ofnode_to_fdt(node),
|
|
|
|
ofnode_to_offset(node),
|
2021-01-12 12:55:22 +00:00
|
|
|
cpu, bus, size);
|
|
|
|
}
|
|
|
|
|
2018-04-19 03:14:02 +00:00
|
|
|
int ofnode_device_is_compatible(ofnode node, const char *compat)
|
|
|
|
{
|
|
|
|
if (ofnode_is_np(node))
|
|
|
|
return of_device_is_compatible(ofnode_to_np(node), compat,
|
|
|
|
NULL, NULL);
|
|
|
|
else
|
2022-09-07 02:27:20 +00:00
|
|
|
return !fdt_node_check_compatible(ofnode_to_fdt(node),
|
2018-04-19 03:14:02 +00:00
|
|
|
ofnode_to_offset(node),
|
|
|
|
compat);
|
|
|
|
}
|
2018-06-11 19:07:13 +00:00
|
|
|
|
|
|
|
ofnode ofnode_by_compatible(ofnode from, const char *compat)
|
|
|
|
{
|
|
|
|
if (of_live_active()) {
|
|
|
|
return np_to_ofnode(of_find_compatible_node(
|
|
|
|
(struct device_node *)ofnode_to_np(from), NULL,
|
|
|
|
compat));
|
|
|
|
} else {
|
2022-09-07 02:27:23 +00:00
|
|
|
return noffset_to_ofnode(from,
|
|
|
|
fdt_node_offset_by_compatible(ofnode_to_fdt(from),
|
2022-09-07 02:27:20 +00:00
|
|
|
ofnode_to_offset(from), compat));
|
2018-06-11 19:07:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-20 09:09:58 +00:00
|
|
|
|
|
|
|
ofnode ofnode_by_prop_value(ofnode from, const char *propname,
|
|
|
|
const void *propval, int proplen)
|
|
|
|
{
|
|
|
|
if (of_live_active()) {
|
|
|
|
return np_to_ofnode(of_find_node_by_prop_value(
|
|
|
|
(struct device_node *)ofnode_to_np(from), propname,
|
|
|
|
propval, proplen));
|
|
|
|
} else {
|
2022-09-07 02:27:23 +00:00
|
|
|
return noffset_to_ofnode(from,
|
|
|
|
fdt_node_offset_by_prop_value(ofnode_to_fdt(from),
|
|
|
|
ofnode_to_offset(from), propname, propval,
|
|
|
|
proplen));
|
2018-08-20 09:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-26 06:46:48 +00:00
|
|
|
|
2022-07-30 21:52:10 +00:00
|
|
|
int ofnode_write_prop(ofnode node, const char *propname, const void *value,
|
2022-09-07 02:27:32 +00:00
|
|
|
int len, bool copy)
|
2018-06-26 06:46:48 +00:00
|
|
|
{
|
2022-09-07 02:27:32 +00:00
|
|
|
if (of_live_active()) {
|
|
|
|
void *newval;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (copy) {
|
|
|
|
newval = malloc(len);
|
|
|
|
if (!newval)
|
|
|
|
return log_ret(-ENOMEM);
|
|
|
|
memcpy(newval, value, len);
|
|
|
|
value = newval;
|
|
|
|
}
|
|
|
|
ret = of_write_prop(ofnode_to_np(node), propname, len, value);
|
|
|
|
if (ret && copy)
|
|
|
|
free(newval);
|
|
|
|
return ret;
|
|
|
|
} else {
|
2022-09-07 02:27:20 +00:00
|
|
|
return fdt_setprop(ofnode_to_fdt(node), ofnode_to_offset(node),
|
2022-07-30 21:52:13 +00:00
|
|
|
propname, value, len);
|
2022-09-07 02:27:32 +00:00
|
|
|
}
|
2018-06-26 06:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ofnode_write_string(ofnode node, const char *propname, const char *value)
|
|
|
|
{
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
|
|
|
|
debug("%s: %s = %s", __func__, propname, value);
|
|
|
|
|
2022-09-07 02:27:32 +00:00
|
|
|
return ofnode_write_prop(node, propname, value, strlen(value) + 1,
|
|
|
|
false);
|
2018-06-26 06:46:48 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 21:52:14 +00:00
|
|
|
int ofnode_write_u32(ofnode node, const char *propname, u32 value)
|
|
|
|
{
|
|
|
|
fdt32_t *val;
|
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
|
|
|
|
log_debug("%s = %x", propname, value);
|
|
|
|
val = malloc(sizeof(*val));
|
|
|
|
if (!val)
|
|
|
|
return -ENOMEM;
|
|
|
|
*val = cpu_to_fdt32(value);
|
|
|
|
|
2022-09-07 02:27:32 +00:00
|
|
|
return ofnode_write_prop(node, propname, val, sizeof(value), false);
|
2022-07-30 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 06:46:48 +00:00
|
|
|
int ofnode_set_enabled(ofnode node, bool value)
|
|
|
|
{
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
return ofnode_write_string(node, "status", "okay");
|
|
|
|
else
|
2019-07-05 16:23:17 +00:00
|
|
|
return ofnode_write_string(node, "status", "disabled");
|
2018-06-26 06:46:48 +00:00
|
|
|
}
|
2021-08-07 13:24:01 +00:00
|
|
|
|
|
|
|
bool ofnode_conf_read_bool(const char *prop_name)
|
|
|
|
{
|
|
|
|
ofnode node;
|
|
|
|
|
|
|
|
node = ofnode_path("/config");
|
|
|
|
if (!ofnode_valid(node))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ofnode_read_bool(node, prop_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ofnode_conf_read_int(const char *prop_name, int default_val)
|
|
|
|
{
|
|
|
|
ofnode node;
|
|
|
|
|
|
|
|
node = ofnode_path("/config");
|
|
|
|
if (!ofnode_valid(node))
|
|
|
|
return default_val;
|
|
|
|
|
|
|
|
return ofnode_read_u32_default(node, prop_name, default_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ofnode_conf_read_str(const char *prop_name)
|
|
|
|
{
|
|
|
|
ofnode node;
|
|
|
|
|
|
|
|
node = ofnode_path("/config");
|
|
|
|
if (!ofnode_valid(node))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return ofnode_read_string(node, prop_name);
|
|
|
|
}
|
2022-04-06 22:32:57 +00:00
|
|
|
|
|
|
|
ofnode ofnode_get_phy_node(ofnode node)
|
|
|
|
{
|
|
|
|
/* DT node properties that reference a PHY node */
|
|
|
|
static const char * const phy_handle_str[] = {
|
|
|
|
"phy-handle", "phy", "phy-device",
|
|
|
|
};
|
|
|
|
struct ofnode_phandle_args args = {
|
|
|
|
.node = ofnode_null()
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(phy_handle_str); i++)
|
|
|
|
if (!ofnode_parse_phandle_with_args(node, phy_handle_str[i],
|
|
|
|
NULL, 0, 0, &args))
|
|
|
|
break;
|
|
|
|
|
|
|
|
return args.node;
|
|
|
|
}
|
2022-04-06 22:33:01 +00:00
|
|
|
|
|
|
|
phy_interface_t ofnode_read_phy_mode(ofnode node)
|
|
|
|
{
|
|
|
|
const char *mode;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
|
|
|
|
mode = ofnode_read_string(node, "phy-mode");
|
|
|
|
if (!mode)
|
|
|
|
mode = ofnode_read_string(node, "phy-connection-type");
|
|
|
|
|
|
|
|
if (!mode)
|
2022-04-06 22:33:03 +00:00
|
|
|
return PHY_INTERFACE_MODE_NA;
|
2022-04-06 22:33:01 +00:00
|
|
|
|
2022-04-06 22:33:02 +00:00
|
|
|
for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
|
2022-04-06 22:33:01 +00:00
|
|
|
if (!strcmp(mode, phy_interface_strings[i]))
|
|
|
|
return i;
|
|
|
|
|
|
|
|
debug("%s: Invalid PHY interface '%s'\n", __func__, mode);
|
|
|
|
|
2022-04-06 22:33:03 +00:00
|
|
|
return PHY_INTERFACE_MODE_NA;
|
2022-04-06 22:33:01 +00:00
|
|
|
}
|
2022-09-07 02:27:02 +00:00
|
|
|
|
|
|
|
int ofnode_add_subnode(ofnode node, const char *name, ofnode *subnodep)
|
|
|
|
{
|
|
|
|
ofnode subnode;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
assert(ofnode_valid(node));
|
|
|
|
|
|
|
|
if (ofnode_is_np(node)) {
|
|
|
|
struct device_node *np, *child;
|
|
|
|
|
|
|
|
np = (struct device_node *)ofnode_to_np(node);
|
|
|
|
ret = of_add_subnode(np, name, -1, &child);
|
|
|
|
if (ret && ret != -EEXIST)
|
|
|
|
return ret;
|
|
|
|
subnode = np_to_ofnode(child);
|
|
|
|
} else {
|
2022-09-07 02:27:20 +00:00
|
|
|
void *fdt = ofnode_to_fdt(node);
|
2022-09-07 02:27:02 +00:00
|
|
|
int poffset = ofnode_to_offset(node);
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
offset = fdt_add_subnode(fdt, poffset, name);
|
|
|
|
if (offset == -FDT_ERR_EXISTS) {
|
|
|
|
offset = fdt_subnode_offset(fdt, poffset, name);
|
|
|
|
ret = -EEXIST;
|
|
|
|
}
|
|
|
|
if (offset < 0)
|
|
|
|
return -EINVAL;
|
2022-09-07 02:27:23 +00:00
|
|
|
subnode = noffset_to_ofnode(node, offset);
|
2022-09-07 02:27:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*subnodep = subnode;
|
|
|
|
|
|
|
|
return ret; /* 0 or -EEXIST */
|
|
|
|
}
|
2022-09-07 02:27:33 +00:00
|
|
|
|
|
|
|
int ofnode_copy_props(ofnode src, ofnode dst)
|
|
|
|
{
|
|
|
|
struct ofprop prop;
|
|
|
|
|
|
|
|
ofnode_for_each_prop(prop, src) {
|
|
|
|
const char *name;
|
|
|
|
const char *val;
|
|
|
|
int len, ret;
|
|
|
|
|
|
|
|
val = ofprop_get_property(&prop, &name, &len);
|
|
|
|
if (!val) {
|
|
|
|
log_debug("Cannot read prop (err=%d)\n", len);
|
|
|
|
return log_msg_ret("get", -EINVAL);
|
|
|
|
}
|
|
|
|
ret = ofnode_write_prop(dst, name, val, len, true);
|
|
|
|
if (ret) {
|
|
|
|
log_debug("Cannot write prop (err=%d)\n", ret);
|
|
|
|
return log_msg_ret("wr", -EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|