mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-09-22 03:09:16 +08:00
Drivers: Support Open Firmware API and model of PIC
We support OFW API to replace fdt old API, and add IRQ, IO, Platform-Bus, CPUs ... OFW node contorl. To support work with Device Tree or ACPI in drivers that use IRQ, we make a programmable interrupt controller driver's model. Signed-off-by: GuEe-GUI <GuEe-GUI@github.com>
This commit is contained in:
@@ -773,6 +773,9 @@ menuconfig RT_USING_VIRTIO
|
||||
default y
|
||||
endif
|
||||
|
||||
source "$RTT_DIR/components/drivers/ofw/Kconfig"
|
||||
source "$RTT_DIR/components/drivers/pic/Kconfig"
|
||||
|
||||
menu "Using USB"
|
||||
config RT_USING_USB
|
||||
bool
|
||||
|
||||
@@ -0,0 +1,433 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-08-25 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#ifndef __OFW_H__
|
||||
#define __OFW_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include <ref.h>
|
||||
#include <bitmap.h>
|
||||
|
||||
#include <libfdt/libfdt.h>
|
||||
|
||||
typedef rt_uint32_t rt_phandle;
|
||||
|
||||
struct rt_ofw_prop
|
||||
{
|
||||
const char *name;
|
||||
int length;
|
||||
void *value;
|
||||
|
||||
struct rt_ofw_prop *next;
|
||||
};
|
||||
|
||||
struct rt_ofw_node
|
||||
{
|
||||
const char *name;
|
||||
/* full_name is 'path/tag' or 'path/tag@reg' */
|
||||
const char *full_name;
|
||||
/* phandles range from 1 to 2^32-2 (0xfffffffe) */
|
||||
rt_phandle phandle;
|
||||
|
||||
struct rt_ofw_prop *props;
|
||||
struct rt_ofw_node *parent;
|
||||
struct rt_ofw_node *child;
|
||||
struct rt_ofw_node *sibling;
|
||||
|
||||
struct rt_ref ref;
|
||||
|
||||
#define RT_OFW_F_SYSTEM 0 /* node is system node */
|
||||
#define RT_OFW_F_READLY 1 /* node has driver */
|
||||
#define RT_OFW_F_PLATFORM 2 /* node is platform device */
|
||||
#define RT_OFW_F_OVERLAY 3 /* node is from overlay */
|
||||
rt_bitmap_t flags;
|
||||
|
||||
/* RT-Thread object prototype */
|
||||
void *rt_data;
|
||||
};
|
||||
|
||||
#define RT_OFW_MAX_CELL_ARGS 16
|
||||
|
||||
struct rt_ofw_cell_args
|
||||
{
|
||||
void *data;
|
||||
|
||||
int args_count;
|
||||
rt_uint32_t args[RT_OFW_MAX_CELL_ARGS];
|
||||
};
|
||||
|
||||
struct rt_ofw_node_id
|
||||
{
|
||||
/* The name string should consist name property (deprecated) */
|
||||
char name[32];
|
||||
|
||||
/*
|
||||
* The type string should consist device_type property, such as pci, memory
|
||||
* serial. Because it's deprecated in <devicetree-basics>, we can use other
|
||||
* name (like "ttyS" or "ttyAMA" ...) to config with /chosen.
|
||||
*/
|
||||
char type[32];
|
||||
|
||||
/*
|
||||
* The compatible string should consist only of lowercase letters, digits
|
||||
* and dashes, and should start with a letter. A single comma is typically
|
||||
* only used following a vendor prefix. Underscores should not be used.
|
||||
*/
|
||||
char compatible[128];
|
||||
|
||||
const void *data;
|
||||
};
|
||||
|
||||
struct rt_ofw_stub
|
||||
{
|
||||
const struct rt_ofw_node_id *ids;
|
||||
rt_err_t (*handler)(struct rt_ofw_node *np, const struct rt_ofw_node_id *id);
|
||||
};
|
||||
|
||||
#define RT_OFW_SYMBOL(_class, _level) \
|
||||
rt_section(".rt_ofw_data." #_class "." #_level)
|
||||
|
||||
#define RT_OFW_SYMBOL_TYPE_RANGE(_class, _type, _start, _end) \
|
||||
static const rt_used RT_OFW_SYMBOL(_class, 0) _type _start; \
|
||||
static const rt_used RT_OFW_SYMBOL(_class, end) _type _end; \
|
||||
|
||||
#define RT_OFW_STUB_EXPORT(_name, _ids, _class, _handler, ...) \
|
||||
static const struct rt_ofw_stub __rt_ofw_##_name \
|
||||
rt_used RT_OFW_SYMBOL(_class, __VA_ARGS__ _) = \
|
||||
{ \
|
||||
.ids = _ids, \
|
||||
.handler = _handler, \
|
||||
}
|
||||
|
||||
#define RT_OFW_STUB_RANGE_EXPORT(_class, _start, _end) \
|
||||
RT_OFW_SYMBOL_TYPE_RANGE(_class, struct rt_ofw_stub, _start = {}, _end = {})
|
||||
|
||||
#define rt_ofw_data(np) ((struct rt_ofw_node *)np)->rt_data
|
||||
|
||||
rt_inline rt_bool_t rt_ofw_node_test_flag(const struct rt_ofw_node *np, int flag)
|
||||
{
|
||||
return rt_bitmap_test_bit((rt_bitmap_t *)&np->flags, flag);
|
||||
}
|
||||
|
||||
rt_inline void rt_ofw_node_set_flag(struct rt_ofw_node *np, int flag)
|
||||
{
|
||||
rt_bitmap_set_bit(&np->flags, flag);
|
||||
}
|
||||
|
||||
rt_inline rt_bool_t rt_ofw_node_test_and_set_flag(struct rt_ofw_node *np, int flag)
|
||||
{
|
||||
rt_bool_t res = rt_ofw_node_test_flag(np, flag);
|
||||
|
||||
rt_ofw_node_set_flag(np, flag);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
rt_inline void rt_ofw_node_clear_flag(struct rt_ofw_node *np, int flag)
|
||||
{
|
||||
rt_bitmap_clear_bit(&np->flags, flag);
|
||||
}
|
||||
|
||||
rt_err_t rt_ofw_node_destroy(struct rt_ofw_node *np);
|
||||
|
||||
struct rt_ofw_node *rt_ofw_node_get(struct rt_ofw_node *np);
|
||||
void rt_ofw_node_put(struct rt_ofw_node *np);
|
||||
|
||||
rt_bool_t rt_ofw_node_tag_equ(const struct rt_ofw_node *np, const char *tag);
|
||||
rt_bool_t rt_ofw_node_tag_prefix(const struct rt_ofw_node *np, const char *prefix);
|
||||
|
||||
rt_inline const char *rt_ofw_node_name(const struct rt_ofw_node *np)
|
||||
{
|
||||
return np ? np->name : "<no-node>";
|
||||
}
|
||||
|
||||
rt_inline const char *rt_ofw_node_full_name(const struct rt_ofw_node *np)
|
||||
{
|
||||
return np ? np->full_name : "<no-node>";
|
||||
}
|
||||
|
||||
rt_bool_t rt_ofw_machine_is_compatible(const char *compatible);
|
||||
rt_bool_t rt_ofw_node_is_available(const struct rt_ofw_node *np);
|
||||
rt_bool_t rt_ofw_node_is_compatible(const struct rt_ofw_node *np, const char *compatible);
|
||||
|
||||
struct rt_ofw_node_id *rt_ofw_prop_match(struct rt_ofw_prop *prop, const struct rt_ofw_node_id *ids);
|
||||
struct rt_ofw_node_id *rt_ofw_node_match(struct rt_ofw_node *np, const struct rt_ofw_node_id *ids);
|
||||
|
||||
struct rt_ofw_node *rt_ofw_find_node_by_tag(struct rt_ofw_node *from, const char *tag);
|
||||
struct rt_ofw_node *rt_ofw_find_node_by_prop_r(struct rt_ofw_node *from, const char *propname,
|
||||
const struct rt_ofw_prop **out_prop);
|
||||
|
||||
rt_inline struct rt_ofw_node *rt_ofw_find_node_by_prop(struct rt_ofw_node *from, const char *propname)
|
||||
{
|
||||
return rt_ofw_find_node_by_prop_r(from, propname, RT_NULL);
|
||||
}
|
||||
|
||||
struct rt_ofw_node *rt_ofw_find_node_by_name(struct rt_ofw_node *from, const char *name);
|
||||
struct rt_ofw_node *rt_ofw_find_node_by_type(struct rt_ofw_node *from, const char *type);
|
||||
struct rt_ofw_node *rt_ofw_find_node_by_compatible(struct rt_ofw_node *from, const char *compatible);
|
||||
struct rt_ofw_node *rt_ofw_find_node_by_ids_r(struct rt_ofw_node *from, const struct rt_ofw_node_id *ids,
|
||||
const struct rt_ofw_node_id **out_id);
|
||||
struct rt_ofw_node *rt_ofw_find_node_by_path(const char *path);
|
||||
struct rt_ofw_node *rt_ofw_find_node_by_phandle(rt_phandle phandle);
|
||||
|
||||
rt_inline struct rt_ofw_node *rt_ofw_find_node_by_ids(struct rt_ofw_node *from, const struct rt_ofw_node_id *ids)
|
||||
{
|
||||
return rt_ofw_find_node_by_ids_r(from, ids, RT_NULL);
|
||||
}
|
||||
|
||||
struct rt_ofw_node *rt_ofw_get_parent(const struct rt_ofw_node *np);
|
||||
struct rt_ofw_node *rt_ofw_get_child_by_tag(const struct rt_ofw_node *parent, const char *tag);
|
||||
struct rt_ofw_node *rt_ofw_get_child_by_compatible(const struct rt_ofw_node *parent, const char *compatible);
|
||||
|
||||
int rt_ofw_get_child_count(const struct rt_ofw_node *np);
|
||||
int rt_ofw_get_available_child_count(const struct rt_ofw_node *np);
|
||||
|
||||
struct rt_ofw_node *rt_ofw_get_next_node(struct rt_ofw_node *prev);
|
||||
struct rt_ofw_node *rt_ofw_get_next_parent(struct rt_ofw_node *prev);
|
||||
struct rt_ofw_node *rt_ofw_get_next_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev);
|
||||
struct rt_ofw_node *rt_ofw_get_next_available_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev);
|
||||
|
||||
struct rt_ofw_node *rt_ofw_get_cpu_node(int cpu, int *thread, rt_bool_t (*match_cpu_hwid)(int cpu, rt_uint64_t hwid));
|
||||
struct rt_ofw_node *rt_ofw_get_next_cpu_node(struct rt_ofw_node *prev);
|
||||
struct rt_ofw_node *rt_ofw_get_cpu_state_node(struct rt_ofw_node *cpu_np, int index);
|
||||
rt_uint64_t rt_ofw_get_cpu_id(struct rt_ofw_node *cpu_np);
|
||||
rt_uint64_t rt_ofw_get_cpu_hwid(struct rt_ofw_node *cpu_np, unsigned int thread);
|
||||
|
||||
struct rt_ofw_node *rt_ofw_get_alias_node(const char *tag, int id);
|
||||
int rt_ofw_get_alias_id(struct rt_ofw_node *np, const char *tag);
|
||||
int rt_ofw_get_alias_last_id(const char *tag);
|
||||
|
||||
struct rt_ofw_node *rt_ofw_parse_phandle(const struct rt_ofw_node *np, const char *phandle_name, int index);
|
||||
rt_err_t rt_ofw_parse_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name,
|
||||
int index, struct rt_ofw_cell_args *out_args);
|
||||
int rt_ofw_count_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name);
|
||||
|
||||
struct rt_ofw_prop *rt_ofw_get_prop(const struct rt_ofw_node *np, const char *name, rt_ssize_t *out_length);
|
||||
|
||||
rt_inline const void *rt_ofw_prop_read_raw(const struct rt_ofw_node *np, const char *name, rt_ssize_t *out_length)
|
||||
{
|
||||
struct rt_ofw_prop *prop = rt_ofw_get_prop(np, name, out_length);
|
||||
|
||||
return prop ? prop->value : RT_NULL;
|
||||
}
|
||||
|
||||
int rt_ofw_prop_read_u8_array_index(const struct rt_ofw_node *np, const char *propname,
|
||||
int index, int nr, rt_uint8_t *out_values);
|
||||
int rt_ofw_prop_read_u16_array_index(const struct rt_ofw_node *np, const char *propname,
|
||||
int index, int nr, rt_uint16_t *out_values);
|
||||
int rt_ofw_prop_read_u32_array_index(const struct rt_ofw_node *np, const char *propname,
|
||||
int index, int nr, rt_uint32_t *out_values);
|
||||
int rt_ofw_prop_read_u64_array_index(const struct rt_ofw_node *np, const char *propname,
|
||||
int index, int nr, rt_uint64_t *out_values);
|
||||
int rt_ofw_prop_read_string_array_index(const struct rt_ofw_node *np, const char *propname,
|
||||
int index, int nr, const char **out_strings);
|
||||
|
||||
int rt_ofw_prop_count_of_size(const struct rt_ofw_node *np, const char *propname, int size);
|
||||
int rt_ofw_prop_index_of_string(const struct rt_ofw_node *np, const char *propname, const char *string);
|
||||
|
||||
const fdt32_t *rt_ofw_prop_next_u32(struct rt_ofw_prop *prop, const fdt32_t *cur, rt_uint32_t *out_value);
|
||||
const char *rt_ofw_prop_next_string(struct rt_ofw_prop *prop, const char *cur);
|
||||
|
||||
rt_inline rt_err_t rt_ofw_prop_read_u8_index(const struct rt_ofw_node *np, const char *propname,
|
||||
int index, rt_uint8_t *out_value)
|
||||
{
|
||||
int nr = rt_ofw_prop_read_u8_array_index(np, propname, index, 1, out_value);
|
||||
|
||||
return nr > 0 ? RT_EOK : (rt_err_t)nr;
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_ofw_prop_read_u16_index(const struct rt_ofw_node *np, const char *propname,
|
||||
int index, rt_uint16_t *out_value)
|
||||
{
|
||||
int nr = rt_ofw_prop_read_u16_array_index(np, propname, index, 1, out_value);
|
||||
|
||||
return nr > 0 ? RT_EOK : (rt_err_t)nr;
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_ofw_prop_read_u32_index(const struct rt_ofw_node *np, const char *propname,
|
||||
int index, rt_uint32_t *out_value)
|
||||
{
|
||||
int nr = rt_ofw_prop_read_u32_array_index(np, propname, index, 1, out_value);
|
||||
|
||||
return nr > 0 ? RT_EOK : (rt_err_t)nr;
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_ofw_prop_read_u64_index(const struct rt_ofw_node *np, const char *propname,
|
||||
int index, rt_uint64_t *out_value)
|
||||
{
|
||||
int nr = rt_ofw_prop_read_u64_array_index(np, propname, index, 1, out_value);
|
||||
|
||||
return nr > 0 ? RT_EOK : (rt_err_t)nr;
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_ofw_prop_read_string_index(const struct rt_ofw_node *np, const char *propname,
|
||||
int index, const char **out_string)
|
||||
{
|
||||
int nr = rt_ofw_prop_read_string_array_index(np, propname, index, 1, out_string);
|
||||
|
||||
return nr > 0 ? RT_EOK : (rt_err_t)nr;
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_ofw_prop_read_u8(const struct rt_ofw_node *np, const char *propname,
|
||||
rt_uint8_t *out_value)
|
||||
{
|
||||
return rt_ofw_prop_read_u8_index(np, propname, 0, out_value);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_ofw_prop_read_u16(const struct rt_ofw_node *np, const char *propname,
|
||||
rt_uint16_t *out_value)
|
||||
{
|
||||
return rt_ofw_prop_read_u16_index(np, propname, 0, out_value);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_ofw_prop_read_u32(const struct rt_ofw_node *np, const char *propname,
|
||||
rt_uint32_t *out_value)
|
||||
{
|
||||
return rt_ofw_prop_read_u32_index(np, propname, 0, out_value);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_ofw_prop_read_s32(const struct rt_ofw_node *np, const char *propname,
|
||||
rt_int32_t *out_value)
|
||||
{
|
||||
return rt_ofw_prop_read_u32_index(np, propname, 0, (rt_uint32_t *)out_value);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_ofw_prop_read_u64(const struct rt_ofw_node *np, const char *propname,
|
||||
rt_uint64_t *out_value)
|
||||
{
|
||||
return rt_ofw_prop_read_u64_index(np, propname, 0, out_value);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_ofw_prop_read_string(const struct rt_ofw_node *np, const char *propname,
|
||||
const char **out_string)
|
||||
{
|
||||
return rt_ofw_prop_read_string_index(np, propname, 0, out_string);
|
||||
}
|
||||
|
||||
rt_inline rt_bool_t rt_ofw_prop_read_bool(const struct rt_ofw_node *np, const char *propname)
|
||||
{
|
||||
return rt_ofw_get_prop(np, propname, RT_NULL) ? RT_TRUE : RT_FALSE;
|
||||
}
|
||||
|
||||
rt_inline int rt_ofw_prop_count_of_u8(const struct rt_ofw_node *np, const char *propname)
|
||||
{
|
||||
return rt_ofw_prop_count_of_size(np, propname, sizeof(rt_uint8_t));
|
||||
}
|
||||
|
||||
rt_inline int rt_ofw_prop_count_of_u16(const struct rt_ofw_node *np, const char *propname)
|
||||
{
|
||||
return rt_ofw_prop_count_of_size(np, propname, sizeof(rt_uint16_t));
|
||||
}
|
||||
|
||||
rt_inline int rt_ofw_prop_count_of_u32(const struct rt_ofw_node *np, const char *propname)
|
||||
{
|
||||
return rt_ofw_prop_count_of_size(np, propname, sizeof(rt_uint32_t));
|
||||
}
|
||||
|
||||
rt_inline int rt_ofw_prop_count_of_u64(const struct rt_ofw_node *np, const char *propname)
|
||||
{
|
||||
return rt_ofw_prop_count_of_size(np, propname, sizeof(rt_uint64_t));
|
||||
}
|
||||
|
||||
rt_inline const char *rt_ofw_node_type(const struct rt_ofw_node *np)
|
||||
{
|
||||
return rt_ofw_prop_read_raw(np, "device_type", RT_NULL);
|
||||
}
|
||||
|
||||
rt_inline rt_bool_t rt_ofw_node_is_type(const struct rt_ofw_node *np, const char *type)
|
||||
{
|
||||
const char *get_type = rt_ofw_node_type(np);
|
||||
|
||||
return np && get_type && type && !rt_strcmp(get_type, type);
|
||||
}
|
||||
|
||||
#define rt_ofw_foreach_node_by_tag(np, name) \
|
||||
for (np = rt_ofw_find_node_by_tag(RT_NULL, name); np; \
|
||||
np = rt_ofw_find_node_by_tag(np, name))
|
||||
|
||||
#define rt_ofw_foreach_node_by_prop(np, prop_name) \
|
||||
for (np = rt_ofw_find_node_by_prop(RT_NULL, prop_name); \
|
||||
np; np = rt_ofw_find_node_by_prop(np, prop_name))
|
||||
|
||||
#define rt_ofw_foreach_node_by_prop_r(np, prop_name, prop) \
|
||||
for (np = rt_ofw_find_node_by_prop_r(RT_NULL, prop_name, prop); \
|
||||
np; np = rt_ofw_find_node_by_prop_r(np, prop_name, prop))
|
||||
|
||||
#define rt_ofw_foreach_node_by_name(np, name) \
|
||||
for (np = rt_ofw_find_node_by_name(RT_NULL, name); np; \
|
||||
np = rt_ofw_find_node_by_name(np, name))
|
||||
|
||||
#define rt_ofw_foreach_node_by_type(np, type) \
|
||||
for (np = rt_ofw_find_node_by_type(RT_NULL, type); np; \
|
||||
np = rt_ofw_find_node_by_type(np, type))
|
||||
|
||||
#define rt_ofw_foreach_node_by_compatible(np, type, compatible) \
|
||||
for (np = rt_ofw_find_node_by_compatible(RT_NULL, type, compatible); np; \
|
||||
np = rt_ofw_find_node_by_compatible(np, type, compatible))
|
||||
|
||||
#define rt_ofw_foreach_node_by_ids_r(np, id, ids) \
|
||||
for (np = rt_ofw_find_node_by_ids_r(RT_NULL, ids, id); \
|
||||
np; np = rt_ofw_find_node_by_ids_r(np, ids, id))
|
||||
|
||||
#define rt_ofw_foreach_node_by_ids(np, ids) \
|
||||
for (np = rt_ofw_find_node_by_ids(RT_NULL, ids); np; \
|
||||
np = rt_ofw_find_node_by_ids(np, ids))
|
||||
|
||||
#define rt_ofw_foreach_nodes(from, np) \
|
||||
for (np = rt_ofw_get_next_node(from); \
|
||||
np; np = rt_ofw_get_next_node(np))
|
||||
|
||||
#define rt_ofw_foreach_allnodes(np) \
|
||||
rt_ofw_foreach_nodes(RT_NULL, np)
|
||||
|
||||
#define rt_ofw_foreach_parent_node(np) \
|
||||
for (np = rt_ofw_get_next_parent(rt_ofw_node_get(np)); \
|
||||
np; np = rt_ofw_get_next_parent(np))
|
||||
|
||||
#define rt_ofw_foreach_child_node(parent, child) \
|
||||
for (child = rt_ofw_get_next_child(parent, RT_NULL); \
|
||||
child; child = rt_ofw_get_next_child(parent, child))
|
||||
|
||||
#define rt_ofw_foreach_available_child_node(parent, child) \
|
||||
for (child = rt_ofw_get_next_available_child(parent, RT_NULL); child; \
|
||||
child = rt_ofw_get_next_available_child(parent, child))
|
||||
|
||||
#define rt_ofw_foreach_cpu_node(cpu_np) \
|
||||
for (cpu_np = rt_ofw_get_next_cpu_node(RT_NULL); \
|
||||
cpu_np; cpu_np = rt_ofw_get_next_cpu_node(cpu_np))
|
||||
|
||||
#define rt_ofw_foreach_prop(np, prop) \
|
||||
for (prop = np->props; prop; prop = prop->next)
|
||||
|
||||
#define rt_ofw_foreach_prop_u32(np, propname, prop, p, u) \
|
||||
for (prop = rt_ofw_get_prop(np, propname, RT_NULL), \
|
||||
p = rt_ofw_prop_next_u32(prop, RT_NULL, &u); p; \
|
||||
p = rt_ofw_prop_next_u32(prop, p, &u))
|
||||
|
||||
#define rt_ofw_foreach_prop_string(np, propname, prop, s) \
|
||||
for (prop = rt_ofw_get_prop(np, propname, RT_NULL), \
|
||||
s = rt_ofw_prop_next_string(prop, RT_NULL); s; \
|
||||
s = rt_ofw_prop_next_string(prop, s))
|
||||
|
||||
#define rt_ofw_foreach_stub(stub, stub_start, stub_end) \
|
||||
for (stub = stub_start; stub <= stub_end; ++stub)
|
||||
|
||||
struct rt_ofw_stub *rt_ofw_stub_probe_range(struct rt_ofw_node *np,
|
||||
const struct rt_ofw_stub *stub_start, const struct rt_ofw_stub *stub_end);
|
||||
|
||||
rt_err_t rt_ofw_console_setup(void);
|
||||
const char *rt_ofw_bootargs_select(const char *key, int index);
|
||||
|
||||
#ifdef RT_USING_CONSOLE
|
||||
void rt_ofw_node_dump_dts(struct rt_ofw_node *np, rt_bool_t sibling_too);
|
||||
#endif
|
||||
|
||||
#endif /* __OFW_H__ */
|
||||
Executable
+78
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-08-25 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#ifndef __OFW_FDT_H__
|
||||
#define __OFW_FDT_H__
|
||||
|
||||
#include <mm_page.h>
|
||||
#include <drivers/ofw.h>
|
||||
|
||||
struct rt_fdt_earlycon
|
||||
{
|
||||
union { rt_ubase_t mmio, port; };
|
||||
union { rt_ubase_t size, width; };
|
||||
|
||||
void *fdt;
|
||||
long nodeoffset;
|
||||
|
||||
void *data;
|
||||
void (*console_putc)(void *data, char c);
|
||||
|
||||
#define FDT_EARLYCON_KICK_UPDATE 0
|
||||
#define FDT_EARLYCON_KICK_COMPLETED 1
|
||||
void (*console_kick)(struct rt_fdt_earlycon *earlycon, int why);
|
||||
|
||||
long msg_idx;
|
||||
char msg[RT_FDT_EARLYCON_MSG_SIZE * 1024];
|
||||
};
|
||||
|
||||
struct rt_fdt_earlycon_id
|
||||
{
|
||||
char *name;
|
||||
char *type;
|
||||
char *compatible;
|
||||
rt_err_t (*setup)(struct rt_fdt_earlycon *earlycon, const char *options);
|
||||
};
|
||||
|
||||
#define RT_FDT_EARLYCON_OPTION_SIGNATURE '\n'
|
||||
|
||||
#define RT_FDT_EARLYCON_EXPORT(_name, _type, _compatible, _setup) \
|
||||
static const struct rt_fdt_earlycon_id __rt_fdt_##_name##_earlycon \
|
||||
rt_used RT_OFW_SYMBOL(earlycon, _) = \
|
||||
{ \
|
||||
.name = #_name, \
|
||||
.type = _type, \
|
||||
.compatible = _compatible, \
|
||||
.setup = _setup, \
|
||||
}
|
||||
|
||||
const char *rt_fdt_node_name(const char *full_name);
|
||||
rt_uint64_t rt_fdt_read_number(const fdt32_t *cell, int size);
|
||||
rt_uint64_t rt_fdt_next_cell(const fdt32_t **cellptr, int size);
|
||||
rt_uint64_t rt_fdt_translate_address(void *fdt, int nodeoffset, rt_uint64_t address);
|
||||
rt_bool_t rt_fdt_device_is_available(void *fdt, int nodeoffset);
|
||||
|
||||
rt_err_t rt_fdt_commit_memregion_early(rt_region_t *region, rt_bool_t is_reserved);
|
||||
rt_err_t rt_fdt_commit_memregion_request(rt_region_t **out_region, rt_size_t *out_nr, rt_bool_t is_reserved);
|
||||
|
||||
rt_err_t rt_fdt_prefetch(void *fdt);
|
||||
rt_err_t rt_fdt_scan_root(void);
|
||||
rt_err_t rt_fdt_scan_memory(void);
|
||||
rt_err_t rt_fdt_scan_initrd(rt_uint64_t *ranges);
|
||||
rt_err_t rt_fdt_model_dump(void);
|
||||
rt_err_t rt_fdt_boot_dump(void);
|
||||
void rt_fdt_earlycon_output(const char *str);
|
||||
void rt_fdt_earlycon_kick(int why);
|
||||
rt_err_t rt_fdt_scan_chosen_stdout(void);
|
||||
rt_err_t rt_fdt_unflatten(void);
|
||||
|
||||
struct rt_ofw_node *rt_fdt_unflatten_single(void *fdt);
|
||||
|
||||
#endif /* __OFW_FDT_H__ */
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-08-25 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#ifndef __OFW_IO_H__
|
||||
#define __OFW_IO_H__
|
||||
|
||||
#include <drivers/ofw.h>
|
||||
|
||||
int rt_ofw_bus_addr_cells(struct rt_ofw_node *np);
|
||||
int rt_ofw_bus_size_cells(struct rt_ofw_node *np);
|
||||
int rt_ofw_io_addr_cells(struct rt_ofw_node *np);
|
||||
int rt_ofw_io_size_cells(struct rt_ofw_node *np);
|
||||
|
||||
int rt_ofw_get_address_count(struct rt_ofw_node *np);
|
||||
rt_err_t rt_ofw_get_address(struct rt_ofw_node *np, int index, rt_uint64_t *out_address, rt_uint64_t *out_size);
|
||||
rt_err_t rt_ofw_get_address_by_name(struct rt_ofw_node *np, const char *name,
|
||||
rt_uint64_t *out_address, rt_uint64_t *out_size);
|
||||
int rt_ofw_get_address_array(struct rt_ofw_node *np, int nr, rt_uint64_t *out_regs);
|
||||
|
||||
rt_uint64_t rt_ofw_translate_address(struct rt_ofw_node *np, const char *range_type, rt_uint64_t address);
|
||||
|
||||
void *rt_ofw_iomap(struct rt_ofw_node *np, int index);
|
||||
void *rt_ofw_iomap_by_name(struct rt_ofw_node *np, const char *name);
|
||||
|
||||
#endif /* __OFW_IO_H__ */
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-08-25 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#ifndef __OFW_IRQ_H__
|
||||
#define __OFW_IRQ_H__
|
||||
|
||||
#include <drivers/ofw.h>
|
||||
|
||||
int rt_ofw_irq_cells(struct rt_ofw_node *np);
|
||||
|
||||
rt_err_t rt_ofw_parse_irq_map(struct rt_ofw_node *np, struct rt_ofw_cell_args *irq_args);
|
||||
rt_err_t rt_ofw_parse_irq_cells(struct rt_ofw_node *np, int index, struct rt_ofw_cell_args *out_irq_args);
|
||||
|
||||
struct rt_ofw_node *rt_ofw_find_irq_parent(struct rt_ofw_node *np, int *out_interrupt_cells);
|
||||
int rt_ofw_map_irq(struct rt_ofw_cell_args *irq_args);
|
||||
|
||||
int rt_ofw_get_irq_count(struct rt_ofw_node *np);
|
||||
int rt_ofw_get_irq(struct rt_ofw_node *np, int index);
|
||||
int rt_ofw_get_irq_by_name(struct rt_ofw_node *np, const char *name);
|
||||
|
||||
#endif /* __OFW_IRQ_H__ */
|
||||
Executable
+66
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-08-25 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#ifndef __OFW_RAW_H__
|
||||
#define __OFW_RAW_H__
|
||||
|
||||
#include <libfdt/libfdt.h>
|
||||
|
||||
#define FDT_SIZE_KB 1024
|
||||
#define FDT_SIZE_MB (1024 * FDT_SIZE_KB)
|
||||
|
||||
#define FDT_SIZE_MAX (2 * FDT_SIZE_MB)
|
||||
#define FDT_PADDING_SIZE (1 * FDT_SIZE_KB)
|
||||
|
||||
typedef uint8_t fdt8_t;
|
||||
|
||||
static inline uint8_t fdt8_to_cpu(fdt8_t x)
|
||||
{
|
||||
return (uint8_t)x;
|
||||
}
|
||||
|
||||
int fdt_add_subnode_possible(void *fdt, int parentoffset, const char *name);
|
||||
int fdt_add_mem_rsv_possible(void *fdt, size_t addr, size_t size);
|
||||
|
||||
#define fdt_setprop_cstring(fdt, nodeoffset, name, str) \
|
||||
fdt_setprop((fdt), (nodeoffset), (name), (str), sizeof(str))
|
||||
|
||||
#define fdt_prop_cells_ops(ops, fdt, nodeoffset, prop, ...) \
|
||||
({ \
|
||||
int ret = 0; \
|
||||
uint32_t tmp[] = { __VA_ARGS__ }; \
|
||||
for (int i = 0; i < sizeof(tmp) / sizeof(tmp[0]); ++i) \
|
||||
{ \
|
||||
tmp[i] = cpu_to_fdt32(tmp[i]); \
|
||||
} \
|
||||
ret += ops(fdt, nodeoffset, prop, tmp, sizeof(tmp)); \
|
||||
ret; \
|
||||
})
|
||||
|
||||
#define fdt_setprop_cells(fdt, nodeoffset, prop, ...) \
|
||||
fdt_prop_cells_ops(fdt_setprop, fdt, nodeoffset, prop, __VA_ARGS__)
|
||||
|
||||
#define fdt_appendprop_cells(fdt, nodeoffset, prop, ...) \
|
||||
fdt_prop_cells_ops(fdt_appendprop, fdt, nodeoffset, prop, __VA_ARGS__)
|
||||
|
||||
int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name, uint64_t val, bool is_u64);
|
||||
int fdt_getprop_u8(void *fdt, int nodeoffset, const char *name, uint8_t *out_value, int *lenp);
|
||||
int fdt_getprop_s8(void *fdt, int nodeoffset, const char *name, int8_t *out_value, int *lenp);
|
||||
int fdt_getprop_u16(void *fdt, int nodeoffset, const char *name, uint16_t *out_value, int *lenp);
|
||||
int fdt_getprop_s16(void *fdt, int nodeoffset, const char *name, int16_t *out_value, int *lenp);
|
||||
int fdt_getprop_u32(void *fdt, int nodeoffset, const char *name, uint32_t *out_value, int *lenp);
|
||||
int fdt_getprop_s32(void *fdt, int nodeoffset, const char *name, int32_t *out_value, int *lenp);
|
||||
|
||||
int fdt_io_addr_cells(void *fdt, int nodeoffset);
|
||||
int fdt_io_size_cells(void *fdt, int nodeoffset);
|
||||
|
||||
int fdt_install_initrd(void *fdt, char *os_name, size_t initrd_addr, size_t initrd_size);
|
||||
|
||||
#endif /* __OFW_RAW_H__ */
|
||||
Executable
+167
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-08-24 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#ifndef __PIC_H__
|
||||
#define __PIC_H__
|
||||
|
||||
#include <rthw.h>
|
||||
|
||||
#include <bitmap.h>
|
||||
#include <drivers/ofw.h>
|
||||
#include <drivers/core/rtdm.h>
|
||||
|
||||
struct rt_pci_msi_desc;
|
||||
struct rt_pci_msi_msg;
|
||||
|
||||
struct rt_pic_ops;
|
||||
struct rt_pic_irq;
|
||||
|
||||
struct rt_pic
|
||||
{
|
||||
rt_list_t list;
|
||||
|
||||
struct rt_pic_ops *ops;
|
||||
|
||||
void *priv_data;
|
||||
void *user_data;
|
||||
|
||||
struct rt_pic *parent;
|
||||
|
||||
int irq_start;
|
||||
rt_size_t irq_nr;
|
||||
struct rt_pic_irq *pirqs;
|
||||
};
|
||||
|
||||
struct rt_pic_ops
|
||||
{
|
||||
const char *name;
|
||||
|
||||
rt_err_t (*irq_init)(struct rt_pic *pic);
|
||||
rt_err_t (*irq_finit)(struct rt_pic *pic);
|
||||
|
||||
void (*irq_enable)(struct rt_pic_irq *pirq);
|
||||
void (*irq_disable)(struct rt_pic_irq *pirq);
|
||||
void (*irq_ack)(struct rt_pic_irq *pirq);
|
||||
void (*irq_mask)(struct rt_pic_irq *pirq);
|
||||
void (*irq_unmask)(struct rt_pic_irq *pirq);
|
||||
void (*irq_eoi)(struct rt_pic_irq *pirq);
|
||||
|
||||
rt_err_t (*irq_set_priority)(struct rt_pic_irq *pirq, rt_uint32_t priority);
|
||||
rt_err_t (*irq_set_affinity)(struct rt_pic_irq *pirq, rt_bitmap_t *affinity);
|
||||
rt_err_t (*irq_set_triger_mode)(struct rt_pic_irq *pirq, rt_uint32_t mode);
|
||||
|
||||
void (*irq_send_ipi)(struct rt_pic_irq *pirq, rt_bitmap_t *cpumask);
|
||||
|
||||
void (*irq_compose_msi_msg)(struct rt_pic_irq *pirq, struct rt_pci_msi_msg *msg);
|
||||
void (*irq_write_msi_msg)(struct rt_pic_irq *pirq, struct rt_pci_msi_msg *msg);
|
||||
int (*irq_alloc_msi)(struct rt_pic *pic, struct rt_pci_msi_desc *msi_desc);
|
||||
void (*irq_free_msi)(struct rt_pic *pic, int irq);
|
||||
|
||||
int (*irq_map)(struct rt_pic *pic, int hwirq, rt_uint32_t mode);
|
||||
rt_err_t (*irq_parse)(struct rt_pic *pic, struct rt_ofw_cell_args *args, struct rt_pic_irq *out_pirq);
|
||||
};
|
||||
|
||||
struct rt_pic_isr
|
||||
{
|
||||
rt_list_t list;
|
||||
|
||||
#define RT_IRQ_F_NONE 0
|
||||
int flags;
|
||||
struct rt_irq_desc action;
|
||||
};
|
||||
|
||||
struct rt_pic_irq
|
||||
{
|
||||
int irq;
|
||||
int hwirq;
|
||||
|
||||
#define RT_IRQ_MODE_NONE 0
|
||||
#define RT_IRQ_MODE_EDGE_RISING 1
|
||||
#define RT_IRQ_MODE_EDGE_FALLING 2
|
||||
#define RT_IRQ_MODE_EDGE_BOTH (RT_IRQ_MODE_EDGE_FALLING | RT_IRQ_MODE_EDGE_RISING)
|
||||
#define RT_IRQ_MODE_LEVEL_HIGH 4
|
||||
#define RT_IRQ_MODE_LEVEL_LOW 8
|
||||
#define RT_IRQ_MODE_LEVEL_MASK (RT_IRQ_MODE_LEVEL_LOW | RT_IRQ_MODE_LEVEL_HIGH)
|
||||
#define RT_IRQ_MODE_MASK 0xf
|
||||
rt_uint32_t mode;
|
||||
|
||||
rt_uint32_t priority;
|
||||
RT_DECLARE_BITMAP(affinity, RT_CPUS_NR);
|
||||
|
||||
struct rt_pci_msi_desc *msi_desc;
|
||||
|
||||
struct rt_pic_isr isr;
|
||||
|
||||
struct rt_spinlock rw_lock;
|
||||
|
||||
struct rt_pic *pic;
|
||||
};
|
||||
|
||||
rt_err_t rt_pic_linear_irq(struct rt_pic *pic, rt_size_t irq_nr);
|
||||
|
||||
int rt_pic_config_ipi(struct rt_pic *pic, int ipi_index, int hwirq);
|
||||
int rt_pic_config_irq(struct rt_pic *pic, int irq_index, int hwirq);
|
||||
|
||||
rt_inline struct rt_pic_irq *rt_pic_find_irq(struct rt_pic *pic, int irq_index)
|
||||
{
|
||||
/* This is a quickly interface */
|
||||
RT_ASSERT(pic != RT_NULL);
|
||||
RT_ASSERT(pic->pirqs != RT_NULL);
|
||||
RT_ASSERT(irq_index < pic->irq_nr);
|
||||
|
||||
return &pic->pirqs[irq_index];
|
||||
}
|
||||
|
||||
struct rt_pic_irq *rt_pic_find_ipi(struct rt_pic *pic, int ipi_index);
|
||||
|
||||
int rt_pic_cascade(struct rt_pic *pic, struct rt_pic *parent_pic, int hwirq, rt_uint32_t mode);
|
||||
void rt_pic_uncascade(struct rt_pic *pic, int irq);
|
||||
|
||||
rt_err_t rt_pic_attach_irq(int irq, rt_isr_handler_t handler, void *uid, const char *name, int flags);
|
||||
rt_err_t rt_pic_detach_irq(int irq, void *uid);
|
||||
|
||||
rt_err_t rt_pic_add_traps(rt_bool_t (*handler)(void *), void *data);
|
||||
rt_err_t rt_pic_do_traps(void);
|
||||
rt_err_t rt_pic_handle_isr(struct rt_pic_irq *pirq);
|
||||
|
||||
/* User-implemented extensions */
|
||||
rt_err_t rt_pic_user_extends(struct rt_pic *pic);
|
||||
|
||||
rt_err_t rt_pic_irq_init(void);
|
||||
rt_err_t rt_pic_irq_finit(void);
|
||||
void rt_pic_irq_enable(int irq);
|
||||
void rt_pic_irq_disable(int irq);
|
||||
void rt_pic_irq_ack(int irq);
|
||||
void rt_pic_irq_mask(int irq);
|
||||
void rt_pic_irq_unmask(int irq);
|
||||
void rt_pic_irq_eoi(int irq);
|
||||
rt_err_t rt_pic_irq_set_priority(int irq, rt_uint32_t priority);
|
||||
rt_uint32_t rt_pic_irq_get_priority(int irq);
|
||||
rt_err_t rt_pic_irq_set_affinity(int irq, rt_bitmap_t *affinity);
|
||||
rt_err_t rt_pic_irq_get_affinity(int irq, rt_bitmap_t *out_affinity);
|
||||
rt_err_t rt_pic_irq_set_triger_mode(int irq, rt_uint32_t mode);
|
||||
rt_uint32_t rt_pic_irq_get_triger_mode(int irq);
|
||||
void rt_pic_irq_send_ipi(int irq, rt_bitmap_t *cpumask);
|
||||
|
||||
void rt_pic_irq_parent_enable(struct rt_pic *ppic, struct rt_pic_irq *pirq);
|
||||
void rt_pic_irq_parent_disable(struct rt_pic *ppic, struct rt_pic_irq *pirq);
|
||||
void rt_pic_irq_parent_ack(struct rt_pic *ppic, struct rt_pic_irq *pirq);
|
||||
void rt_pic_irq_parent_mask(struct rt_pic *ppic, struct rt_pic_irq *pirq);
|
||||
void rt_pic_irq_parent_unmask(struct rt_pic *ppic, struct rt_pic_irq *pirq);
|
||||
void rt_pic_irq_parent_eoi(struct rt_pic *ppic, struct rt_pic_irq *pirq);
|
||||
rt_err_t rt_pic_irq_parent_set_priority(struct rt_pic *ppic, struct rt_pic_irq *pirq, rt_uint32_t priority);
|
||||
rt_err_t rt_pic_irq_parent_set_affinity(struct rt_pic *ppic, struct rt_pic_irq *pirq, rt_bitmap_t *affinity);
|
||||
rt_err_t rt_pic_irq_parent_set_triger_mode(struct rt_pic *ppic, struct rt_pic_irq *pirq, rt_uint32_t mode);
|
||||
|
||||
#define RT_PIC_OFW_DECLARE(name, ids, handler) RT_OFW_STUB_EXPORT(name, ids, pic, handler)
|
||||
|
||||
rt_err_t rt_pic_init(void);
|
||||
|
||||
#endif /* __PIC_H__ */
|
||||
@@ -171,6 +171,18 @@ extern "C" {
|
||||
|
||||
#ifdef RT_USING_DM
|
||||
#include "drivers/core/rtdm.h"
|
||||
|
||||
#ifdef RT_USING_OFW
|
||||
#include "drivers/ofw.h"
|
||||
#include "drivers/ofw_fdt.h"
|
||||
#include "drivers/ofw_io.h"
|
||||
#include "drivers/ofw_irq.h"
|
||||
#include "drivers/ofw_raw.h"
|
||||
#endif /* RT_USING_OFW */
|
||||
|
||||
#ifdef RT_USING_PIC
|
||||
#include "drivers/pic.h"
|
||||
#endif
|
||||
#endif /* RT_USING_DM */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
menuconfig RT_USING_OFW
|
||||
bool "Using Open Firmware (OFW)"
|
||||
select RT_USING_ADT
|
||||
select RT_USING_ADT_REF
|
||||
select RT_USING_ADT_BITMAP
|
||||
depends on RT_USING_DM
|
||||
default n
|
||||
|
||||
config RT_USING_BUILTIN_FDT
|
||||
bool "Using builtin fdt in kernel"
|
||||
depends on RT_USING_OFW
|
||||
default n
|
||||
|
||||
config RT_BUILTIN_FDT_PATH
|
||||
string "Builtin fdt path, will rebuild if have dts"
|
||||
depends on RT_USING_BUILTIN_FDT
|
||||
default "rtthread.dtb"
|
||||
|
||||
config RT_FDT_EARLYCON_MSG_SIZE
|
||||
int "Earlycon message buffer size (KB)"
|
||||
depends on RT_USING_OFW
|
||||
default 128
|
||||
@@ -0,0 +1,22 @@
|
||||
from building import *
|
||||
|
||||
objs = []
|
||||
|
||||
if not GetDepend(['RT_USING_OFW']):
|
||||
Return('objs')
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
list = os.listdir(cwd)
|
||||
CPPPATH = [cwd, cwd + '/../include']
|
||||
|
||||
src = Glob('*.c')
|
||||
|
||||
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
|
||||
|
||||
for d in list:
|
||||
path = os.path.join(cwd, d)
|
||||
if os.path.isfile(os.path.join(path, 'SConscript')):
|
||||
objs = objs + SConscript(os.path.join(d, 'SConscript'))
|
||||
objs = objs + group
|
||||
|
||||
Return('objs')
|
||||
File diff suppressed because it is too large
Load Diff
Executable
+1108
File diff suppressed because it is too large
Load Diff
Executable
+421
@@ -0,0 +1,421 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-08-25 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include <ioremap.h>
|
||||
#include <drivers/ofw.h>
|
||||
#include <drivers/ofw_io.h>
|
||||
#include <drivers/ofw_fdt.h>
|
||||
|
||||
#define DBG_TAG "rtdm.ofw"
|
||||
#define DBG_LVL DBG_INFO
|
||||
#include <rtdbg.h>
|
||||
|
||||
#include "ofw_internal.h"
|
||||
|
||||
static int ofw_bus_addr_cells(struct rt_ofw_node *np)
|
||||
{
|
||||
int res = OFW_ROOT_NODE_ADDR_CELLS_DEFAULT;
|
||||
|
||||
for (rt_uint32_t cells; np; np = np->parent)
|
||||
{
|
||||
if (!rt_ofw_prop_read_u32(np, "#address-cells", &cells))
|
||||
{
|
||||
res = cells;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int ofw_bus_size_cells(struct rt_ofw_node *np)
|
||||
{
|
||||
int res = OFW_ROOT_NODE_SIZE_CELLS_DEFAULT;
|
||||
|
||||
for (rt_uint32_t cells; np; np = np->parent)
|
||||
{
|
||||
if (!rt_ofw_prop_read_u32(np, "#size-cells", &cells))
|
||||
{
|
||||
res = cells;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int rt_ofw_bus_addr_cells(struct rt_ofw_node *np)
|
||||
{
|
||||
return np ? ofw_bus_addr_cells(np) : -RT_EINVAL;
|
||||
}
|
||||
|
||||
int rt_ofw_bus_size_cells(struct rt_ofw_node *np)
|
||||
{
|
||||
return np ? ofw_bus_size_cells(np) : -RT_EINVAL;
|
||||
}
|
||||
|
||||
int rt_ofw_io_addr_cells(struct rt_ofw_node *np)
|
||||
{
|
||||
return np ? ofw_bus_addr_cells(np->parent ? np->parent : np) : -RT_EINVAL;
|
||||
}
|
||||
|
||||
int rt_ofw_io_size_cells(struct rt_ofw_node *np)
|
||||
{
|
||||
return np ? ofw_bus_size_cells(np->parent ? np->parent : np) : -RT_EINVAL;
|
||||
}
|
||||
|
||||
int rt_ofw_get_address_count(struct rt_ofw_node *np)
|
||||
{
|
||||
int count;
|
||||
|
||||
if (np)
|
||||
{
|
||||
rt_ssize_t len;
|
||||
|
||||
count = 0;
|
||||
|
||||
if (rt_ofw_get_prop(np, "reg", &len))
|
||||
{
|
||||
count = len / (sizeof(fdt32_t) * (rt_ofw_io_addr_cells(np) + rt_ofw_io_size_cells(np)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
count = -RT_EINVAL;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
static rt_err_t ofw_get_address(struct rt_ofw_node *np, int index, rt_uint64_t *out_address, rt_uint64_t *out_size)
|
||||
{
|
||||
rt_ssize_t len;
|
||||
rt_err_t err = RT_EOK;
|
||||
int addr_cells = rt_ofw_io_addr_cells(np);
|
||||
int size_cells = rt_ofw_io_size_cells(np);
|
||||
int skip_cells = (addr_cells + size_cells) * index;
|
||||
const fdt32_t *cell = rt_ofw_prop_read_raw(np, "reg", &len);
|
||||
|
||||
if (cell && skip_cells < (len / sizeof(*cell)))
|
||||
{
|
||||
cell += skip_cells;
|
||||
*out_address = rt_fdt_next_cell(&cell, addr_cells);
|
||||
*out_address = rt_ofw_translate_address(np, RT_NULL, *out_address);
|
||||
*out_size = rt_fdt_read_number(cell, size_cells);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = -RT_EINVAL;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_err_t rt_ofw_get_address(struct rt_ofw_node *np, int index, rt_uint64_t *out_address, rt_uint64_t *out_size)
|
||||
{
|
||||
rt_err_t err;
|
||||
|
||||
if (np && index >= 0 && (out_address || out_size))
|
||||
{
|
||||
rt_uint64_t address, size;
|
||||
|
||||
err = ofw_get_address(np, index, &address, &size);
|
||||
|
||||
if (!err)
|
||||
{
|
||||
if (out_address)
|
||||
{
|
||||
*out_address = address;
|
||||
}
|
||||
if (out_size)
|
||||
{
|
||||
*out_size = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
err = -RT_EINVAL;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static rt_err_t ofw_get_address_by_name(struct rt_ofw_node *np, const char *name,
|
||||
rt_uint64_t *out_address, rt_uint64_t *out_size)
|
||||
|
||||
{
|
||||
int index = 0;
|
||||
rt_err_t err = RT_EOK;
|
||||
const char *reg_name;
|
||||
struct rt_ofw_prop *prop;
|
||||
|
||||
rt_ofw_foreach_prop_string(np, "reg-names", prop, reg_name)
|
||||
{
|
||||
if (!rt_strcmp(name, reg_name))
|
||||
{
|
||||
err = rt_ofw_get_address(np, index, out_address, out_size);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
++index;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_err_t rt_ofw_get_address_by_name(struct rt_ofw_node *np, const char *name,
|
||||
rt_uint64_t *out_address, rt_uint64_t *out_size)
|
||||
{
|
||||
rt_err_t err;
|
||||
|
||||
if (np && name && (out_address || out_size))
|
||||
{
|
||||
rt_uint64_t address, size;
|
||||
|
||||
err = ofw_get_address_by_name(np, name, &address, &size);
|
||||
|
||||
if (!err)
|
||||
{
|
||||
if (out_address)
|
||||
{
|
||||
*out_address = address;
|
||||
}
|
||||
if (out_size)
|
||||
{
|
||||
*out_size = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
err = -RT_EINVAL;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int rt_ofw_get_address_array(struct rt_ofw_node *np, int nr, rt_uint64_t *out_regs)
|
||||
{
|
||||
int count;
|
||||
|
||||
if (np && nr > 0 && out_regs)
|
||||
{
|
||||
rt_ssize_t len;
|
||||
int max_nr;
|
||||
int addr_cells = rt_ofw_io_addr_cells(np);
|
||||
int size_cells = rt_ofw_io_size_cells(np);
|
||||
const fdt32_t *cell = rt_ofw_prop_read_raw(np, "reg", &len);
|
||||
|
||||
max_nr = len / (sizeof(*cell) * (addr_cells + size_cells));
|
||||
|
||||
if (nr > max_nr)
|
||||
{
|
||||
nr = max_nr;
|
||||
}
|
||||
|
||||
count = nr;
|
||||
|
||||
while (nr --> 0)
|
||||
{
|
||||
*out_regs = rt_fdt_next_cell(&cell, addr_cells);
|
||||
*out_regs = rt_ofw_translate_address(np, RT_NULL, *out_regs);
|
||||
++out_regs;
|
||||
|
||||
*out_regs = rt_fdt_next_cell(&cell, size_cells);
|
||||
++out_regs;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
count = -RT_EINVAL;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
static struct bus_ranges *ofw_bus_ranges(struct rt_ofw_node *np, struct rt_ofw_prop *prop)
|
||||
{
|
||||
const fdt32_t *cell;
|
||||
struct bus_ranges *ranges = RT_NULL;
|
||||
int child_address_cells, child_size_cells, parent_address_cells, groups;
|
||||
rt_uint64_t *child_addr, *parent_addr, *child_size;
|
||||
|
||||
/*
|
||||
* Address Translation Example:
|
||||
*
|
||||
* / {
|
||||
* #address-cells = <1>;
|
||||
* #size-cells = <1>;
|
||||
*
|
||||
* soc {
|
||||
* compatible = "simple-bus";
|
||||
* #address-cells = <1>;
|
||||
* #size-cells = <1>;
|
||||
* ranges = <0x0 0xe0000000 0x00100000>;
|
||||
*
|
||||
* serial@4600 {
|
||||
* device_type = "serial";
|
||||
* reg = <0x4600 0x100>;
|
||||
* clock-frequency = <0>;
|
||||
* };
|
||||
* };
|
||||
* }
|
||||
*
|
||||
* The soc node specifies a ranges property of <0x0 0xe0000000 0x00100000>;
|
||||
* This property value specifies that for a 1024 KB range of address space, a
|
||||
* child node addressed at physical 0x0 maps to a parent address of physical
|
||||
* 0xe0000000. With this mapping, the serial device node can be addressed by a
|
||||
* load or store at address 0xe0004600, an offset of 0x4600 (specified in reg)
|
||||
* plus the 0xe0000000 mapping specified in ranges:
|
||||
*
|
||||
* bus-address = parent-bus-address + (reg-address - child-bus-address)
|
||||
*/
|
||||
|
||||
do {
|
||||
child_address_cells = rt_ofw_bus_addr_cells(np);
|
||||
child_size_cells = rt_ofw_bus_size_cells(np);
|
||||
parent_address_cells = rt_ofw_io_addr_cells(np);
|
||||
|
||||
if (child_address_cells < 0 || child_size_cells < 0 || parent_address_cells < 0)
|
||||
{
|
||||
LOG_D("%s read address/size cells fail: child[%d, %d] parent[%d]",
|
||||
np->full_name, child_address_cells, child_size_cells, parent_address_cells);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
groups = prop->length / sizeof(*cell);
|
||||
groups /= child_address_cells + child_size_cells + parent_address_cells;
|
||||
|
||||
ranges = rt_malloc(sizeof(*ranges) + sizeof(rt_uint64_t) * 3 * groups);
|
||||
|
||||
if (!ranges)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
ranges->nr = groups;
|
||||
ranges->child_addr = (void *)ranges + sizeof(*ranges);
|
||||
ranges->parent_addr = &ranges->child_addr[groups];
|
||||
ranges->child_size = &ranges->parent_addr[groups];
|
||||
|
||||
cell = prop->value;
|
||||
|
||||
child_addr = ranges->child_addr;
|
||||
parent_addr = ranges->parent_addr;
|
||||
child_size = ranges->child_size;
|
||||
|
||||
while (groups --> 0)
|
||||
{
|
||||
*child_addr++ = rt_fdt_next_cell(&cell, child_address_cells);
|
||||
*parent_addr++ = rt_fdt_next_cell(&cell, parent_address_cells);
|
||||
*child_size++ = rt_fdt_next_cell(&cell, child_size_cells);
|
||||
}
|
||||
|
||||
rt_ofw_data(np) = ranges;
|
||||
} while (0);
|
||||
|
||||
return ranges;
|
||||
}
|
||||
|
||||
rt_uint64_t rt_ofw_translate_address(struct rt_ofw_node *np, const char *range_type, rt_uint64_t address)
|
||||
{
|
||||
rt_uint64_t cpu_addr = address;
|
||||
|
||||
if (!range_type)
|
||||
{
|
||||
range_type = "ranges";
|
||||
}
|
||||
|
||||
rt_ofw_foreach_parent_node(np)
|
||||
{
|
||||
rt_ssize_t len;
|
||||
struct rt_ofw_prop *prop;
|
||||
struct bus_ranges *ranges;
|
||||
|
||||
prop = rt_ofw_get_prop(np, range_type, &len);
|
||||
|
||||
if (!prop || !len)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ranges = rt_ofw_data(np);
|
||||
|
||||
if (!ranges)
|
||||
{
|
||||
ranges = ofw_bus_ranges(np, prop);
|
||||
}
|
||||
|
||||
if (ranges)
|
||||
{
|
||||
for (int i = 0; i < ranges->nr; ++i)
|
||||
{
|
||||
rt_uint64_t child_addr = ranges->child_addr[i];
|
||||
rt_uint64_t child_size = ranges->child_size[i];
|
||||
|
||||
if (address >= child_addr && address < child_addr + child_size)
|
||||
{
|
||||
cpu_addr = address + (ranges->parent_addr[i] - child_addr);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cpu_addr = ~0ULL;
|
||||
}
|
||||
|
||||
rt_ofw_node_put(np);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return cpu_addr;
|
||||
}
|
||||
|
||||
void *rt_ofw_iomap(struct rt_ofw_node *np, int index)
|
||||
{
|
||||
void *iomem = RT_NULL;
|
||||
|
||||
if (np)
|
||||
{
|
||||
rt_uint64_t regs[2];
|
||||
|
||||
if (!ofw_get_address(np, index, ®s[0], ®s[1]))
|
||||
{
|
||||
iomem = rt_ioremap((void *)regs[0], (size_t)regs[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return iomem;
|
||||
}
|
||||
|
||||
void *rt_ofw_iomap_by_name(struct rt_ofw_node *np, const char *name)
|
||||
{
|
||||
void *iomem = RT_NULL;
|
||||
|
||||
if (np)
|
||||
{
|
||||
rt_uint64_t regs[2];
|
||||
|
||||
if (!ofw_get_address_by_name(np, name, ®s[0], ®s[1]))
|
||||
{
|
||||
iomem = rt_ioremap((void *)regs[0], (size_t)regs[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return iomem;
|
||||
}
|
||||
Executable
+648
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,10 @@
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c')
|
||||
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
@@ -0,0 +1,339 @@
|
||||
// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
|
||||
/*
|
||||
* libfdt - Flat Device Tree manipulation
|
||||
* Copyright (C) 2006 David Gibson, IBM Corporation.
|
||||
*/
|
||||
#include "libfdt_env.h"
|
||||
|
||||
#include <fdt.h>
|
||||
#include <libfdt.h>
|
||||
|
||||
#include "libfdt_internal.h"
|
||||
|
||||
/*
|
||||
* Minimal sanity check for a read-only tree. fdt_ro_probe_() checks
|
||||
* that the given buffer contains what appears to be a flattened
|
||||
* device tree with sane information in its header.
|
||||
*/
|
||||
int32_t fdt_ro_probe_(const void *fdt)
|
||||
{
|
||||
uint32_t totalsize = fdt_totalsize(fdt);
|
||||
|
||||
if (can_assume(VALID_DTB))
|
||||
return totalsize;
|
||||
|
||||
/* The device tree must be at an 8-byte aligned address */
|
||||
if ((uintptr_t)fdt & 7)
|
||||
return -FDT_ERR_ALIGNMENT;
|
||||
|
||||
if (fdt_magic(fdt) == FDT_MAGIC) {
|
||||
/* Complete tree */
|
||||
if (!can_assume(LATEST)) {
|
||||
if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
|
||||
return -FDT_ERR_BADVERSION;
|
||||
if (fdt_last_comp_version(fdt) >
|
||||
FDT_LAST_SUPPORTED_VERSION)
|
||||
return -FDT_ERR_BADVERSION;
|
||||
}
|
||||
} else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
|
||||
/* Unfinished sequential-write blob */
|
||||
if (!can_assume(VALID_INPUT) && fdt_size_dt_struct(fdt) == 0)
|
||||
return -FDT_ERR_BADSTATE;
|
||||
} else {
|
||||
return -FDT_ERR_BADMAGIC;
|
||||
}
|
||||
|
||||
if (totalsize < INT32_MAX)
|
||||
return totalsize;
|
||||
else
|
||||
return -FDT_ERR_TRUNCATED;
|
||||
}
|
||||
|
||||
static int check_off_(uint32_t hdrsize, uint32_t totalsize, uint32_t off)
|
||||
{
|
||||
return (off >= hdrsize) && (off <= totalsize);
|
||||
}
|
||||
|
||||
static int check_block_(uint32_t hdrsize, uint32_t totalsize,
|
||||
uint32_t base, uint32_t size)
|
||||
{
|
||||
if (!check_off_(hdrsize, totalsize, base))
|
||||
return 0; /* block start out of bounds */
|
||||
if ((base + size) < base)
|
||||
return 0; /* overflow */
|
||||
if (!check_off_(hdrsize, totalsize, base + size))
|
||||
return 0; /* block end out of bounds */
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t fdt_header_size_(uint32_t version)
|
||||
{
|
||||
if (version <= 1)
|
||||
return FDT_V1_SIZE;
|
||||
else if (version <= 2)
|
||||
return FDT_V2_SIZE;
|
||||
else if (version <= 3)
|
||||
return FDT_V3_SIZE;
|
||||
else if (version <= 16)
|
||||
return FDT_V16_SIZE;
|
||||
else
|
||||
return FDT_V17_SIZE;
|
||||
}
|
||||
|
||||
size_t fdt_header_size(const void *fdt)
|
||||
{
|
||||
return can_assume(LATEST) ? FDT_V17_SIZE :
|
||||
fdt_header_size_(fdt_version(fdt));
|
||||
}
|
||||
|
||||
int fdt_check_header(const void *fdt)
|
||||
{
|
||||
size_t hdrsize;
|
||||
|
||||
/* The device tree must be at an 8-byte aligned address */
|
||||
if ((uintptr_t)fdt & 7)
|
||||
return -FDT_ERR_ALIGNMENT;
|
||||
|
||||
if (fdt_magic(fdt) != FDT_MAGIC)
|
||||
return -FDT_ERR_BADMAGIC;
|
||||
if (!can_assume(LATEST)) {
|
||||
if ((fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
|
||||
|| (fdt_last_comp_version(fdt) >
|
||||
FDT_LAST_SUPPORTED_VERSION))
|
||||
return -FDT_ERR_BADVERSION;
|
||||
if (fdt_version(fdt) < fdt_last_comp_version(fdt))
|
||||
return -FDT_ERR_BADVERSION;
|
||||
}
|
||||
hdrsize = fdt_header_size(fdt);
|
||||
if (!can_assume(VALID_DTB)) {
|
||||
if ((fdt_totalsize(fdt) < hdrsize)
|
||||
|| (fdt_totalsize(fdt) > INT_MAX))
|
||||
return -FDT_ERR_TRUNCATED;
|
||||
|
||||
/* Bounds check memrsv block */
|
||||
if (!check_off_(hdrsize, fdt_totalsize(fdt),
|
||||
fdt_off_mem_rsvmap(fdt)))
|
||||
return -FDT_ERR_TRUNCATED;
|
||||
|
||||
/* Bounds check structure block */
|
||||
if (!can_assume(LATEST) && fdt_version(fdt) < 17) {
|
||||
if (!check_off_(hdrsize, fdt_totalsize(fdt),
|
||||
fdt_off_dt_struct(fdt)))
|
||||
return -FDT_ERR_TRUNCATED;
|
||||
} else {
|
||||
if (!check_block_(hdrsize, fdt_totalsize(fdt),
|
||||
fdt_off_dt_struct(fdt),
|
||||
fdt_size_dt_struct(fdt)))
|
||||
return -FDT_ERR_TRUNCATED;
|
||||
}
|
||||
|
||||
/* Bounds check strings block */
|
||||
if (!check_block_(hdrsize, fdt_totalsize(fdt),
|
||||
fdt_off_dt_strings(fdt),
|
||||
fdt_size_dt_strings(fdt)))
|
||||
return -FDT_ERR_TRUNCATED;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
|
||||
{
|
||||
unsigned int uoffset = offset;
|
||||
unsigned int absoffset = offset + fdt_off_dt_struct(fdt);
|
||||
|
||||
if (offset < 0)
|
||||
return NULL;
|
||||
|
||||
if (!can_assume(VALID_INPUT))
|
||||
if ((absoffset < uoffset)
|
||||
|| ((absoffset + len) < absoffset)
|
||||
|| (absoffset + len) > fdt_totalsize(fdt))
|
||||
return NULL;
|
||||
|
||||
if (can_assume(LATEST) || fdt_version(fdt) >= 0x11)
|
||||
if (((uoffset + len) < uoffset)
|
||||
|| ((offset + len) > fdt_size_dt_struct(fdt)))
|
||||
return NULL;
|
||||
|
||||
return fdt_offset_ptr_(fdt, offset);
|
||||
}
|
||||
|
||||
uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
|
||||
{
|
||||
const fdt32_t *tagp, *lenp;
|
||||
uint32_t tag, len, sum;
|
||||
int offset = startoffset;
|
||||
const char *p;
|
||||
|
||||
*nextoffset = -FDT_ERR_TRUNCATED;
|
||||
tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
|
||||
if (!can_assume(VALID_DTB) && !tagp)
|
||||
return FDT_END; /* premature end */
|
||||
tag = fdt32_to_cpu(*tagp);
|
||||
offset += FDT_TAGSIZE;
|
||||
|
||||
*nextoffset = -FDT_ERR_BADSTRUCTURE;
|
||||
switch (tag) {
|
||||
case FDT_BEGIN_NODE:
|
||||
/* skip name */
|
||||
do {
|
||||
p = fdt_offset_ptr(fdt, offset++, 1);
|
||||
} while (p && (*p != '\0'));
|
||||
if (!can_assume(VALID_DTB) && !p)
|
||||
return FDT_END; /* premature end */
|
||||
break;
|
||||
|
||||
case FDT_PROP:
|
||||
lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
|
||||
if (!can_assume(VALID_DTB) && !lenp)
|
||||
return FDT_END; /* premature end */
|
||||
|
||||
len = fdt32_to_cpu(*lenp);
|
||||
sum = len + offset;
|
||||
if (!can_assume(VALID_DTB) &&
|
||||
(INT_MAX <= sum || sum < (uint32_t) offset))
|
||||
return FDT_END; /* premature end */
|
||||
|
||||
/* skip-name offset, length and value */
|
||||
offset += sizeof(struct fdt_property) - FDT_TAGSIZE + len;
|
||||
|
||||
if (!can_assume(LATEST) &&
|
||||
fdt_version(fdt) < 0x10 && len >= 8 &&
|
||||
((offset - len) % 8) != 0)
|
||||
offset += 4;
|
||||
break;
|
||||
|
||||
case FDT_END:
|
||||
case FDT_END_NODE:
|
||||
case FDT_NOP:
|
||||
break;
|
||||
|
||||
default:
|
||||
return FDT_END;
|
||||
}
|
||||
|
||||
if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
|
||||
return FDT_END; /* premature end */
|
||||
|
||||
*nextoffset = FDT_TAGALIGN(offset);
|
||||
return tag;
|
||||
}
|
||||
|
||||
int fdt_check_node_offset_(const void *fdt, int offset)
|
||||
{
|
||||
if (!can_assume(VALID_INPUT)
|
||||
&& ((offset < 0) || (offset % FDT_TAGSIZE)))
|
||||
return -FDT_ERR_BADOFFSET;
|
||||
|
||||
if (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)
|
||||
return -FDT_ERR_BADOFFSET;
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
int fdt_check_prop_offset_(const void *fdt, int offset)
|
||||
{
|
||||
if (!can_assume(VALID_INPUT)
|
||||
&& ((offset < 0) || (offset % FDT_TAGSIZE)))
|
||||
return -FDT_ERR_BADOFFSET;
|
||||
|
||||
if (fdt_next_tag(fdt, offset, &offset) != FDT_PROP)
|
||||
return -FDT_ERR_BADOFFSET;
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
int fdt_next_node(const void *fdt, int offset, int *depth)
|
||||
{
|
||||
int nextoffset = 0;
|
||||
uint32_t tag;
|
||||
|
||||
if (offset >= 0)
|
||||
if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0)
|
||||
return nextoffset;
|
||||
|
||||
do {
|
||||
offset = nextoffset;
|
||||
tag = fdt_next_tag(fdt, offset, &nextoffset);
|
||||
|
||||
switch (tag) {
|
||||
case FDT_PROP:
|
||||
case FDT_NOP:
|
||||
break;
|
||||
|
||||
case FDT_BEGIN_NODE:
|
||||
if (depth)
|
||||
(*depth)++;
|
||||
break;
|
||||
|
||||
case FDT_END_NODE:
|
||||
if (depth && ((--(*depth)) < 0))
|
||||
return nextoffset;
|
||||
break;
|
||||
|
||||
case FDT_END:
|
||||
if ((nextoffset >= 0)
|
||||
|| ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
|
||||
return -FDT_ERR_NOTFOUND;
|
||||
else
|
||||
return nextoffset;
|
||||
}
|
||||
} while (tag != FDT_BEGIN_NODE);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
int fdt_first_subnode(const void *fdt, int offset)
|
||||
{
|
||||
int depth = 0;
|
||||
|
||||
offset = fdt_next_node(fdt, offset, &depth);
|
||||
if (offset < 0 || depth != 1)
|
||||
return -FDT_ERR_NOTFOUND;
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
int fdt_next_subnode(const void *fdt, int offset)
|
||||
{
|
||||
int depth = 1;
|
||||
|
||||
/*
|
||||
* With respect to the parent, the depth of the next subnode will be
|
||||
* the same as the last.
|
||||
*/
|
||||
do {
|
||||
offset = fdt_next_node(fdt, offset, &depth);
|
||||
if (offset < 0 || depth < 1)
|
||||
return -FDT_ERR_NOTFOUND;
|
||||
} while (depth > 1);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
const char *fdt_find_string_(const char *strtab, int tabsize, const char *s)
|
||||
{
|
||||
int len = strlen(s) + 1;
|
||||
const char *last = strtab + tabsize - len;
|
||||
const char *p;
|
||||
|
||||
for (p = strtab; p <= last; p++)
|
||||
if (memcmp(p, s, len) == 0)
|
||||
return p;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int fdt_move(const void *fdt, void *buf, int bufsize)
|
||||
{
|
||||
if (!can_assume(VALID_INPUT) && bufsize < 0)
|
||||
return -FDT_ERR_NOSPACE;
|
||||
|
||||
FDT_RO_PROBE(fdt);
|
||||
|
||||
if (fdt_totalsize(fdt) > (unsigned int)bufsize)
|
||||
return -FDT_ERR_NOSPACE;
|
||||
|
||||
memmove(buf, fdt, fdt_totalsize(fdt));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
|
||||
#ifndef FDT_H
|
||||
#define FDT_H
|
||||
/*
|
||||
* libfdt - Flat Device Tree manipulation
|
||||
* Copyright (C) 2006 David Gibson, IBM Corporation.
|
||||
* Copyright 2012 Kim Phillips, Freescale Semiconductor.
|
||||
*/
|
||||
|
||||
#ifndef __ASSEMBLY__
|
||||
|
||||
struct fdt_header {
|
||||
fdt32_t magic; /* magic word FDT_MAGIC */
|
||||
fdt32_t totalsize; /* total size of DT block */
|
||||
fdt32_t off_dt_struct; /* offset to structure */
|
||||
fdt32_t off_dt_strings; /* offset to strings */
|
||||
fdt32_t off_mem_rsvmap; /* offset to memory reserve map */
|
||||
fdt32_t version; /* format version */
|
||||
fdt32_t last_comp_version; /* last compatible version */
|
||||
|
||||
/* version 2 fields below */
|
||||
fdt32_t boot_cpuid_phys; /* Which physical CPU id we're
|
||||
booting on */
|
||||
/* version 3 fields below */
|
||||
fdt32_t size_dt_strings; /* size of the strings block */
|
||||
|
||||
/* version 17 fields below */
|
||||
fdt32_t size_dt_struct; /* size of the structure block */
|
||||
};
|
||||
|
||||
struct fdt_reserve_entry {
|
||||
fdt64_t address;
|
||||
fdt64_t size;
|
||||
};
|
||||
|
||||
struct fdt_node_header {
|
||||
fdt32_t tag;
|
||||
char name[];
|
||||
};
|
||||
|
||||
struct fdt_property {
|
||||
fdt32_t tag;
|
||||
fdt32_t len;
|
||||
fdt32_t nameoff;
|
||||
char data[];
|
||||
};
|
||||
|
||||
#endif /* !__ASSEMBLY */
|
||||
|
||||
#define FDT_MAGIC 0xd00dfeed /* 4: version, 4: total size */
|
||||
#define FDT_TAGSIZE sizeof(fdt32_t)
|
||||
|
||||
#define FDT_BEGIN_NODE 0x1 /* Start node: full name */
|
||||
#define FDT_END_NODE 0x2 /* End node */
|
||||
#define FDT_PROP 0x3 /* Property: name off,
|
||||
size, content */
|
||||
#define FDT_NOP 0x4 /* nop */
|
||||
#define FDT_END 0x9
|
||||
|
||||
#define FDT_V1_SIZE (7*sizeof(fdt32_t))
|
||||
#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))
|
||||
#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))
|
||||
#define FDT_V16_SIZE FDT_V3_SIZE
|
||||
#define FDT_V17_SIZE (FDT_V16_SIZE + sizeof(fdt32_t))
|
||||
|
||||
#endif /* FDT_H */
|
||||
@@ -0,0 +1,101 @@
|
||||
// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
|
||||
/*
|
||||
* libfdt - Flat Device Tree manipulation
|
||||
* Copyright (C) 2014 David Gibson <david@gibson.dropbear.id.au>
|
||||
* Copyright (C) 2018 embedded brains GmbH
|
||||
*/
|
||||
#include "libfdt_env.h"
|
||||
|
||||
#include <fdt.h>
|
||||
#include <libfdt.h>
|
||||
|
||||
#include "libfdt_internal.h"
|
||||
|
||||
static int fdt_cells(const void *fdt, int nodeoffset, const char *name)
|
||||
{
|
||||
const fdt32_t *c;
|
||||
uint32_t val;
|
||||
int len;
|
||||
|
||||
c = fdt_getprop(fdt, nodeoffset, name, &len);
|
||||
if (!c)
|
||||
return len;
|
||||
|
||||
if (len != sizeof(*c))
|
||||
return -FDT_ERR_BADNCELLS;
|
||||
|
||||
val = fdt32_to_cpu(*c);
|
||||
if (val > FDT_MAX_NCELLS)
|
||||
return -FDT_ERR_BADNCELLS;
|
||||
|
||||
return (int)val;
|
||||
}
|
||||
|
||||
int fdt_address_cells(const void *fdt, int nodeoffset)
|
||||
{
|
||||
int val;
|
||||
|
||||
val = fdt_cells(fdt, nodeoffset, "#address-cells");
|
||||
if (val == 0)
|
||||
return -FDT_ERR_BADNCELLS;
|
||||
if (val == -FDT_ERR_NOTFOUND)
|
||||
return 2;
|
||||
return val;
|
||||
}
|
||||
|
||||
int fdt_size_cells(const void *fdt, int nodeoffset)
|
||||
{
|
||||
int val;
|
||||
|
||||
val = fdt_cells(fdt, nodeoffset, "#size-cells");
|
||||
if (val == -FDT_ERR_NOTFOUND)
|
||||
return 1;
|
||||
return val;
|
||||
}
|
||||
|
||||
/* This function assumes that [address|size]_cells is 1 or 2 */
|
||||
int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset,
|
||||
const char *name, uint64_t addr, uint64_t size)
|
||||
{
|
||||
int addr_cells, size_cells, ret;
|
||||
uint8_t data[sizeof(fdt64_t) * 2], *prop;
|
||||
|
||||
ret = fdt_address_cells(fdt, parent);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
addr_cells = ret;
|
||||
|
||||
ret = fdt_size_cells(fdt, parent);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
size_cells = ret;
|
||||
|
||||
/* check validity of address */
|
||||
prop = data;
|
||||
if (addr_cells == 1) {
|
||||
if ((addr > UINT32_MAX) || (((uint64_t) UINT32_MAX + 1 - addr) < size))
|
||||
return -FDT_ERR_BADVALUE;
|
||||
|
||||
fdt32_st(prop, (uint32_t)addr);
|
||||
} else if (addr_cells == 2) {
|
||||
fdt64_st(prop, addr);
|
||||
} else {
|
||||
return -FDT_ERR_BADNCELLS;
|
||||
}
|
||||
|
||||
/* check validity of size */
|
||||
prop += addr_cells * sizeof(fdt32_t);
|
||||
if (size_cells == 1) {
|
||||
if (size > UINT32_MAX)
|
||||
return -FDT_ERR_BADVALUE;
|
||||
|
||||
fdt32_st(prop, (uint32_t)size);
|
||||
} else if (size_cells == 2) {
|
||||
fdt64_st(prop, size);
|
||||
} else {
|
||||
return -FDT_ERR_BADNCELLS;
|
||||
}
|
||||
|
||||
return fdt_appendprop(fdt, nodeoffset, name, data,
|
||||
(addr_cells + size_cells) * sizeof(fdt32_t));
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
|
||||
/*
|
||||
* libfdt - Flat Device Tree manipulation
|
||||
* Copyright (C) 2012 David Gibson, IBM Corporation.
|
||||
*/
|
||||
#include "libfdt_env.h"
|
||||
|
||||
#include <fdt.h>
|
||||
#include <libfdt.h>
|
||||
|
||||
#include "libfdt_internal.h"
|
||||
|
||||
int fdt_create_empty_tree(void *buf, int bufsize)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = fdt_create(buf, bufsize);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
err = fdt_finish_reservemap(buf);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
err = fdt_begin_node(buf, "");
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
err = fdt_end_node(buf);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
err = fdt_finish(buf);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
return fdt_open_into(buf, buf, bufsize);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,60 @@
|
||||
// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
|
||||
/*
|
||||
* libfdt - Flat Device Tree manipulation
|
||||
* Copyright (C) 2006 David Gibson, IBM Corporation.
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "libfdt_env.h"
|
||||
|
||||
#include <fdt.h>
|
||||
#include <libfdt.h>
|
||||
|
||||
#include "libfdt_internal.h"
|
||||
|
||||
struct fdt_errtabent {
|
||||
const char *str;
|
||||
};
|
||||
|
||||
#define FDT_ERRTABENT(val) \
|
||||
[(val)] = { .str = #val, }
|
||||
|
||||
static struct fdt_errtabent fdt_errtable[] = {
|
||||
FDT_ERRTABENT(FDT_ERR_NOTFOUND),
|
||||
FDT_ERRTABENT(FDT_ERR_EXISTS),
|
||||
FDT_ERRTABENT(FDT_ERR_NOSPACE),
|
||||
|
||||
FDT_ERRTABENT(FDT_ERR_BADOFFSET),
|
||||
FDT_ERRTABENT(FDT_ERR_BADPATH),
|
||||
FDT_ERRTABENT(FDT_ERR_BADPHANDLE),
|
||||
FDT_ERRTABENT(FDT_ERR_BADSTATE),
|
||||
|
||||
FDT_ERRTABENT(FDT_ERR_TRUNCATED),
|
||||
FDT_ERRTABENT(FDT_ERR_BADMAGIC),
|
||||
FDT_ERRTABENT(FDT_ERR_BADVERSION),
|
||||
FDT_ERRTABENT(FDT_ERR_BADSTRUCTURE),
|
||||
FDT_ERRTABENT(FDT_ERR_BADLAYOUT),
|
||||
FDT_ERRTABENT(FDT_ERR_INTERNAL),
|
||||
FDT_ERRTABENT(FDT_ERR_BADNCELLS),
|
||||
FDT_ERRTABENT(FDT_ERR_BADVALUE),
|
||||
FDT_ERRTABENT(FDT_ERR_BADOVERLAY),
|
||||
FDT_ERRTABENT(FDT_ERR_NOPHANDLES),
|
||||
FDT_ERRTABENT(FDT_ERR_BADFLAGS),
|
||||
FDT_ERRTABENT(FDT_ERR_ALIGNMENT),
|
||||
};
|
||||
#define FDT_ERRTABSIZE ((int)(sizeof(fdt_errtable) / sizeof(fdt_errtable[0])))
|
||||
|
||||
const char *fdt_strerror(int errval)
|
||||
{
|
||||
if (errval > 0)
|
||||
return "<valid offset/length>";
|
||||
else if (errval == 0)
|
||||
return "<no error>";
|
||||
else if (-errval < FDT_ERRTABSIZE) {
|
||||
const char *s = fdt_errtable[-errval].str;
|
||||
|
||||
if (s)
|
||||
return s;
|
||||
}
|
||||
|
||||
return "<unknown error>";
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
|
||||
/*
|
||||
* libfdt - Flat Device Tree manipulation
|
||||
* Copyright (C) 2006 David Gibson, IBM Corporation.
|
||||
*/
|
||||
#include "libfdt_env.h"
|
||||
|
||||
#include <fdt.h>
|
||||
#include <libfdt.h>
|
||||
|
||||
#include "libfdt_internal.h"
|
||||
|
||||
static int fdt_sw_probe_(void *fdt)
|
||||
{
|
||||
if (!can_assume(VALID_INPUT)) {
|
||||
if (fdt_magic(fdt) == FDT_MAGIC)
|
||||
return -FDT_ERR_BADSTATE;
|
||||
else if (fdt_magic(fdt) != FDT_SW_MAGIC)
|
||||
return -FDT_ERR_BADMAGIC;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define FDT_SW_PROBE(fdt) \
|
||||
{ \
|
||||
int err; \
|
||||
if ((err = fdt_sw_probe_(fdt)) != 0) \
|
||||
return err; \
|
||||
}
|
||||
|
||||
/* 'memrsv' state: Initial state after fdt_create()
|
||||
*
|
||||
* Allowed functions:
|
||||
* fdt_add_reservemap_entry()
|
||||
* fdt_finish_reservemap() [moves to 'struct' state]
|
||||
*/
|
||||
static int fdt_sw_probe_memrsv_(void *fdt)
|
||||
{
|
||||
int err = fdt_sw_probe_(fdt);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
if (!can_assume(VALID_INPUT) && fdt_off_dt_strings(fdt) != 0)
|
||||
return -FDT_ERR_BADSTATE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define FDT_SW_PROBE_MEMRSV(fdt) \
|
||||
{ \
|
||||
int err; \
|
||||
if ((err = fdt_sw_probe_memrsv_(fdt)) != 0) \
|
||||
return err; \
|
||||
}
|
||||
|
||||
/* 'struct' state: Enter this state after fdt_finish_reservemap()
|
||||
*
|
||||
* Allowed functions:
|
||||
* fdt_begin_node()
|
||||
* fdt_end_node()
|
||||
* fdt_property*()
|
||||
* fdt_finish() [moves to 'complete' state]
|
||||
*/
|
||||
static int fdt_sw_probe_struct_(void *fdt)
|
||||
{
|
||||
int err = fdt_sw_probe_(fdt);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
if (!can_assume(VALID_INPUT) &&
|
||||
fdt_off_dt_strings(fdt) != fdt_totalsize(fdt))
|
||||
return -FDT_ERR_BADSTATE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define FDT_SW_PROBE_STRUCT(fdt) \
|
||||
{ \
|
||||
int err; \
|
||||
if ((err = fdt_sw_probe_struct_(fdt)) != 0) \
|
||||
return err; \
|
||||
}
|
||||
|
||||
static inline uint32_t sw_flags(void *fdt)
|
||||
{
|
||||
/* assert: (fdt_magic(fdt) == FDT_SW_MAGIC) */
|
||||
return fdt_last_comp_version(fdt);
|
||||
}
|
||||
|
||||
/* 'complete' state: Enter this state after fdt_finish()
|
||||
*
|
||||
* Allowed functions: none
|
||||
*/
|
||||
|
||||
static void *fdt_grab_space_(void *fdt, size_t len)
|
||||
{
|
||||
unsigned int offset = fdt_size_dt_struct(fdt);
|
||||
unsigned int spaceleft;
|
||||
|
||||
spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
|
||||
- fdt_size_dt_strings(fdt);
|
||||
|
||||
if ((offset + len < offset) || (offset + len > spaceleft))
|
||||
return NULL;
|
||||
|
||||
fdt_set_size_dt_struct(fdt, offset + len);
|
||||
return fdt_offset_ptr_w_(fdt, offset);
|
||||
}
|
||||
|
||||
int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags)
|
||||
{
|
||||
const int hdrsize = FDT_ALIGN(sizeof(struct fdt_header),
|
||||
sizeof(struct fdt_reserve_entry));
|
||||
void *fdt = buf;
|
||||
|
||||
if (bufsize < hdrsize)
|
||||
return -FDT_ERR_NOSPACE;
|
||||
|
||||
if (flags & ~FDT_CREATE_FLAGS_ALL)
|
||||
return -FDT_ERR_BADFLAGS;
|
||||
|
||||
memset(buf, 0, bufsize);
|
||||
|
||||
/*
|
||||
* magic and last_comp_version keep intermediate state during the fdt
|
||||
* creation process, which is replaced with the proper FDT format by
|
||||
* fdt_finish().
|
||||
*
|
||||
* flags should be accessed with sw_flags().
|
||||
*/
|
||||
fdt_set_magic(fdt, FDT_SW_MAGIC);
|
||||
fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
|
||||
fdt_set_last_comp_version(fdt, flags);
|
||||
|
||||
fdt_set_totalsize(fdt, bufsize);
|
||||
|
||||
fdt_set_off_mem_rsvmap(fdt, hdrsize);
|
||||
fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
|
||||
fdt_set_off_dt_strings(fdt, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fdt_create(void *buf, int bufsize)
|
||||
{
|
||||
return fdt_create_with_flags(buf, bufsize, 0);
|
||||
}
|
||||
|
||||
int fdt_resize(void *fdt, void *buf, int bufsize)
|
||||
{
|
||||
size_t headsize, tailsize;
|
||||
char *oldtail, *newtail;
|
||||
|
||||
FDT_SW_PROBE(fdt);
|
||||
|
||||
if (bufsize < 0)
|
||||
return -FDT_ERR_NOSPACE;
|
||||
|
||||
headsize = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
|
||||
tailsize = fdt_size_dt_strings(fdt);
|
||||
|
||||
if (!can_assume(VALID_DTB) &&
|
||||
headsize + tailsize > fdt_totalsize(fdt))
|
||||
return -FDT_ERR_INTERNAL;
|
||||
|
||||
if ((headsize + tailsize) > (unsigned)bufsize)
|
||||
return -FDT_ERR_NOSPACE;
|
||||
|
||||
oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
|
||||
newtail = (char *)buf + bufsize - tailsize;
|
||||
|
||||
/* Two cases to avoid clobbering data if the old and new
|
||||
* buffers partially overlap */
|
||||
if (buf <= fdt) {
|
||||
memmove(buf, fdt, headsize);
|
||||
memmove(newtail, oldtail, tailsize);
|
||||
} else {
|
||||
memmove(newtail, oldtail, tailsize);
|
||||
memmove(buf, fdt, headsize);
|
||||
}
|
||||
|
||||
fdt_set_totalsize(buf, bufsize);
|
||||
if (fdt_off_dt_strings(buf))
|
||||
fdt_set_off_dt_strings(buf, bufsize);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
|
||||
{
|
||||
struct fdt_reserve_entry *re;
|
||||
int offset;
|
||||
|
||||
FDT_SW_PROBE_MEMRSV(fdt);
|
||||
|
||||
offset = fdt_off_dt_struct(fdt);
|
||||
if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
|
||||
return -FDT_ERR_NOSPACE;
|
||||
|
||||
re = (struct fdt_reserve_entry *)((char *)fdt + offset);
|
||||
re->address = cpu_to_fdt64(addr);
|
||||
re->size = cpu_to_fdt64(size);
|
||||
|
||||
fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fdt_finish_reservemap(void *fdt)
|
||||
{
|
||||
int err = fdt_add_reservemap_entry(fdt, 0, 0);
|
||||
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
fdt_set_off_dt_strings(fdt, fdt_totalsize(fdt));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fdt_begin_node(void *fdt, const char *name)
|
||||
{
|
||||
struct fdt_node_header *nh;
|
||||
int namelen;
|
||||
|
||||
FDT_SW_PROBE_STRUCT(fdt);
|
||||
|
||||
namelen = strlen(name) + 1;
|
||||
nh = fdt_grab_space_(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
|
||||
if (! nh)
|
||||
return -FDT_ERR_NOSPACE;
|
||||
|
||||
nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
|
||||
memcpy(nh->name, name, namelen);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fdt_end_node(void *fdt)
|
||||
{
|
||||
fdt32_t *en;
|
||||
|
||||
FDT_SW_PROBE_STRUCT(fdt);
|
||||
|
||||
en = fdt_grab_space_(fdt, FDT_TAGSIZE);
|
||||
if (! en)
|
||||
return -FDT_ERR_NOSPACE;
|
||||
|
||||
*en = cpu_to_fdt32(FDT_END_NODE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fdt_add_string_(void *fdt, const char *s)
|
||||
{
|
||||
char *strtab = (char *)fdt + fdt_totalsize(fdt);
|
||||
unsigned int strtabsize = fdt_size_dt_strings(fdt);
|
||||
unsigned int len = strlen(s) + 1;
|
||||
unsigned int struct_top, offset;
|
||||
|
||||
offset = strtabsize + len;
|
||||
struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
|
||||
if (fdt_totalsize(fdt) - offset < struct_top)
|
||||
return 0; /* no more room :( */
|
||||
|
||||
memcpy(strtab - offset, s, len);
|
||||
fdt_set_size_dt_strings(fdt, strtabsize + len);
|
||||
return -offset;
|
||||
}
|
||||
|
||||
/* Must only be used to roll back in case of error */
|
||||
static void fdt_del_last_string_(void *fdt, const char *s)
|
||||
{
|
||||
int strtabsize = fdt_size_dt_strings(fdt);
|
||||
int len = strlen(s) + 1;
|
||||
|
||||
fdt_set_size_dt_strings(fdt, strtabsize - len);
|
||||
}
|
||||
|
||||
static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
|
||||
{
|
||||
char *strtab = (char *)fdt + fdt_totalsize(fdt);
|
||||
int strtabsize = fdt_size_dt_strings(fdt);
|
||||
const char *p;
|
||||
|
||||
*allocated = 0;
|
||||
|
||||
p = fdt_find_string_(strtab - strtabsize, strtabsize, s);
|
||||
if (p)
|
||||
return p - strtab;
|
||||
|
||||
*allocated = 1;
|
||||
|
||||
return fdt_add_string_(fdt, s);
|
||||
}
|
||||
|
||||
int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp)
|
||||
{
|
||||
struct fdt_property *prop;
|
||||
int nameoff;
|
||||
int allocated;
|
||||
|
||||
FDT_SW_PROBE_STRUCT(fdt);
|
||||
|
||||
/* String de-duplication can be slow, _NO_NAME_DEDUP skips it */
|
||||
if (sw_flags(fdt) & FDT_CREATE_FLAG_NO_NAME_DEDUP) {
|
||||
allocated = 1;
|
||||
nameoff = fdt_add_string_(fdt, name);
|
||||
} else {
|
||||
nameoff = fdt_find_add_string_(fdt, name, &allocated);
|
||||
}
|
||||
if (nameoff == 0)
|
||||
return -FDT_ERR_NOSPACE;
|
||||
|
||||
prop = fdt_grab_space_(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
|
||||
if (! prop) {
|
||||
if (allocated)
|
||||
fdt_del_last_string_(fdt, name);
|
||||
return -FDT_ERR_NOSPACE;
|
||||
}
|
||||
|
||||
prop->tag = cpu_to_fdt32(FDT_PROP);
|
||||
prop->nameoff = cpu_to_fdt32(nameoff);
|
||||
prop->len = cpu_to_fdt32(len);
|
||||
*valp = prop->data;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fdt_property(void *fdt, const char *name, const void *val, int len)
|
||||
{
|
||||
void *ptr;
|
||||
int ret;
|
||||
|
||||
ret = fdt_property_placeholder(fdt, name, len, &ptr);
|
||||
if (ret)
|
||||
return ret;
|
||||
memcpy(ptr, val, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fdt_finish(void *fdt)
|
||||
{
|
||||
char *p = (char *)fdt;
|
||||
fdt32_t *end;
|
||||
int oldstroffset, newstroffset;
|
||||
uint32_t tag;
|
||||
int offset, nextoffset;
|
||||
|
||||
FDT_SW_PROBE_STRUCT(fdt);
|
||||
|
||||
/* Add terminator */
|
||||
end = fdt_grab_space_(fdt, sizeof(*end));
|
||||
if (! end)
|
||||
return -FDT_ERR_NOSPACE;
|
||||
*end = cpu_to_fdt32(FDT_END);
|
||||
|
||||
/* Relocate the string table */
|
||||
oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
|
||||
newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
|
||||
memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
|
||||
fdt_set_off_dt_strings(fdt, newstroffset);
|
||||
|
||||
/* Walk the structure, correcting string offsets */
|
||||
offset = 0;
|
||||
while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
|
||||
if (tag == FDT_PROP) {
|
||||
struct fdt_property *prop =
|
||||
fdt_offset_ptr_w_(fdt, offset);
|
||||
int nameoff;
|
||||
|
||||
nameoff = fdt32_to_cpu(prop->nameoff);
|
||||
nameoff += fdt_size_dt_strings(fdt);
|
||||
prop->nameoff = cpu_to_fdt32(nameoff);
|
||||
}
|
||||
offset = nextoffset;
|
||||
}
|
||||
if (nextoffset < 0)
|
||||
return nextoffset;
|
||||
|
||||
/* Finally, adjust the header */
|
||||
fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
|
||||
|
||||
/* And fix up fields that were keeping intermediate state. */
|
||||
fdt_set_last_comp_version(fdt, FDT_LAST_COMPATIBLE_VERSION);
|
||||
fdt_set_magic(fdt, FDT_MAGIC);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
|
||||
/*
|
||||
* libfdt - Flat Device Tree manipulation
|
||||
* Copyright (C) 2006 David Gibson, IBM Corporation.
|
||||
*/
|
||||
#include "libfdt_env.h"
|
||||
|
||||
#include <fdt.h>
|
||||
#include <libfdt.h>
|
||||
|
||||
#include "libfdt_internal.h"
|
||||
|
||||
int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
|
||||
const char *name, int namelen,
|
||||
uint32_t idx, const void *val,
|
||||
int len)
|
||||
{
|
||||
void *propval;
|
||||
int proplen;
|
||||
|
||||
propval = fdt_getprop_namelen_w(fdt, nodeoffset, name, namelen,
|
||||
&proplen);
|
||||
if (!propval)
|
||||
return proplen;
|
||||
|
||||
if ((unsigned)proplen < (len + idx))
|
||||
return -FDT_ERR_NOSPACE;
|
||||
|
||||
memcpy((char *)propval + idx, val, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
|
||||
const void *val, int len)
|
||||
{
|
||||
const void *propval;
|
||||
int proplen;
|
||||
|
||||
propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
|
||||
if (!propval)
|
||||
return proplen;
|
||||
|
||||
if (proplen != len)
|
||||
return -FDT_ERR_NOSPACE;
|
||||
|
||||
return fdt_setprop_inplace_namelen_partial(fdt, nodeoffset, name,
|
||||
strlen(name), 0,
|
||||
val, len);
|
||||
}
|
||||
|
||||
static void fdt_nop_region_(void *start, int len)
|
||||
{
|
||||
fdt32_t *p;
|
||||
|
||||
for (p = start; (char *)p < ((char *)start + len); p++)
|
||||
*p = cpu_to_fdt32(FDT_NOP);
|
||||
}
|
||||
|
||||
int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
|
||||
{
|
||||
struct fdt_property *prop;
|
||||
int len;
|
||||
|
||||
prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
|
||||
if (!prop)
|
||||
return len;
|
||||
|
||||
fdt_nop_region_(prop, len + sizeof(*prop));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fdt_node_end_offset_(void *fdt, int offset)
|
||||
{
|
||||
int depth = 0;
|
||||
|
||||
while ((offset >= 0) && (depth >= 0))
|
||||
offset = fdt_next_node(fdt, offset, &depth);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
int fdt_nop_node(void *fdt, int nodeoffset)
|
||||
{
|
||||
int endoffset;
|
||||
|
||||
endoffset = fdt_node_end_offset_(fdt, nodeoffset);
|
||||
if (endoffset < 0)
|
||||
return endoffset;
|
||||
|
||||
fdt_nop_region_(fdt_offset_ptr_w(fdt, nodeoffset, 0),
|
||||
endoffset - nodeoffset);
|
||||
return 0;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,100 @@
|
||||
/* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
|
||||
#ifndef LIBFDT_ENV_H
|
||||
#define LIBFDT_ENV_H
|
||||
/*
|
||||
* libfdt - Flat Device Tree manipulation
|
||||
* Copyright (C) 2006 David Gibson, IBM Corporation.
|
||||
* Copyright 2012 Kim Phillips, Freescale Semiconductor.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#ifdef __CHECKER__
|
||||
#define FDT_FORCE __attribute__((force))
|
||||
#define FDT_BITWISE __attribute__((bitwise))
|
||||
#else
|
||||
#define FDT_FORCE
|
||||
#define FDT_BITWISE
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#define strnlen rt_strnlen
|
||||
|
||||
typedef uint16_t FDT_BITWISE fdt16_t;
|
||||
typedef uint32_t FDT_BITWISE fdt32_t;
|
||||
typedef uint64_t FDT_BITWISE fdt64_t;
|
||||
|
||||
#define EXTRACT_BYTE(x, n) ((unsigned long long)((uint8_t *)&x)[n])
|
||||
#define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
|
||||
#define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
|
||||
(EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
|
||||
#define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
|
||||
(EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
|
||||
(EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
|
||||
(EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
|
||||
|
||||
static inline uint16_t fdt16_to_cpu(fdt16_t x)
|
||||
{
|
||||
return (FDT_FORCE uint16_t)CPU_TO_FDT16(x);
|
||||
}
|
||||
static inline fdt16_t cpu_to_fdt16(uint16_t x)
|
||||
{
|
||||
return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x);
|
||||
}
|
||||
|
||||
static inline uint32_t fdt32_to_cpu(fdt32_t x)
|
||||
{
|
||||
return (FDT_FORCE uint32_t)CPU_TO_FDT32(x);
|
||||
}
|
||||
static inline fdt32_t cpu_to_fdt32(uint32_t x)
|
||||
{
|
||||
return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x);
|
||||
}
|
||||
|
||||
static inline uint64_t fdt64_to_cpu(fdt64_t x)
|
||||
{
|
||||
return (FDT_FORCE uint64_t)CPU_TO_FDT64(x);
|
||||
}
|
||||
static inline fdt64_t cpu_to_fdt64(uint64_t x)
|
||||
{
|
||||
return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x);
|
||||
}
|
||||
#undef CPU_TO_FDT64
|
||||
#undef CPU_TO_FDT32
|
||||
#undef CPU_TO_FDT16
|
||||
#undef EXTRACT_BYTE
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <AvailabilityMacros.h>
|
||||
|
||||
/* strnlen() is not available on Mac OS < 10.7 */
|
||||
# if !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED < \
|
||||
MAC_OS_X_VERSION_10_7)
|
||||
|
||||
#define strnlen fdt_strnlen
|
||||
|
||||
/*
|
||||
* fdt_strnlen: returns the length of a string or max_count - which ever is
|
||||
* smallest.
|
||||
* Input 1 string: the string whose size is to be determined
|
||||
* Input 2 max_count: the maximum value returned by this function
|
||||
* Output: length of the string or max_count (the smallest of the two)
|
||||
*/
|
||||
static inline size_t fdt_strnlen(const char *string, size_t max_count)
|
||||
{
|
||||
const char *p = memchr(string, 0, max_count);
|
||||
return p ? p - string : max_count;
|
||||
}
|
||||
|
||||
#endif /* !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED <
|
||||
MAC_OS_X_VERSION_10_7) */
|
||||
|
||||
#endif /* __APPLE__ */
|
||||
|
||||
#endif /* LIBFDT_ENV_H */
|
||||
@@ -0,0 +1,192 @@
|
||||
/* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
|
||||
#ifndef LIBFDT_INTERNAL_H
|
||||
#define LIBFDT_INTERNAL_H
|
||||
/*
|
||||
* libfdt - Flat Device Tree manipulation
|
||||
* Copyright (C) 2006 David Gibson, IBM Corporation.
|
||||
*/
|
||||
#include <fdt.h>
|
||||
|
||||
#define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
|
||||
#define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE))
|
||||
|
||||
int32_t fdt_ro_probe_(const void *fdt);
|
||||
#define FDT_RO_PROBE(fdt) \
|
||||
{ \
|
||||
int32_t totalsize_; \
|
||||
if ((totalsize_ = fdt_ro_probe_(fdt)) < 0) \
|
||||
return totalsize_; \
|
||||
}
|
||||
|
||||
int fdt_check_node_offset_(const void *fdt, int offset);
|
||||
int fdt_check_prop_offset_(const void *fdt, int offset);
|
||||
const char *fdt_find_string_(const char *strtab, int tabsize, const char *s);
|
||||
int fdt_node_end_offset_(void *fdt, int nodeoffset);
|
||||
|
||||
static inline const void *fdt_offset_ptr_(const void *fdt, int offset)
|
||||
{
|
||||
return (const char *)fdt + fdt_off_dt_struct(fdt) + offset;
|
||||
}
|
||||
|
||||
static inline void *fdt_offset_ptr_w_(void *fdt, int offset)
|
||||
{
|
||||
return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset);
|
||||
}
|
||||
|
||||
static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n)
|
||||
{
|
||||
const struct fdt_reserve_entry *rsv_table =
|
||||
(const struct fdt_reserve_entry *)
|
||||
((const char *)fdt + fdt_off_mem_rsvmap(fdt));
|
||||
|
||||
return rsv_table + n;
|
||||
}
|
||||
static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n)
|
||||
{
|
||||
return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n);
|
||||
}
|
||||
|
||||
/*
|
||||
* Internal helpers to access tructural elements of the device tree
|
||||
* blob (rather than for exaple reading integers from within property
|
||||
* values). We assume that we are either given a naturally aligned
|
||||
* address for the platform or if we are not, we are on a platform
|
||||
* where unaligned memory reads will be handled in a graceful manner.
|
||||
* If not the external helpers fdtXX_ld() from libfdt.h can be used
|
||||
* instead.
|
||||
*/
|
||||
static inline uint32_t fdt32_ld_(const fdt32_t *p)
|
||||
{
|
||||
return fdt32_to_cpu(*p);
|
||||
}
|
||||
|
||||
static inline uint64_t fdt64_ld_(const fdt64_t *p)
|
||||
{
|
||||
return fdt64_to_cpu(*p);
|
||||
}
|
||||
|
||||
#define FDT_SW_MAGIC (~FDT_MAGIC)
|
||||
|
||||
/**********************************************************************/
|
||||
/* Checking controls */
|
||||
/**********************************************************************/
|
||||
|
||||
#ifndef FDT_ASSUME_MASK
|
||||
#define FDT_ASSUME_MASK 0
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Defines assumptions which can be enabled. Each of these can be enabled
|
||||
* individually. For maximum safety, don't enable any assumptions!
|
||||
*
|
||||
* For minimal code size and no safety, use ASSUME_PERFECT at your own risk.
|
||||
* You should have another method of validating the device tree, such as a
|
||||
* signature or hash check before using libfdt.
|
||||
*
|
||||
* For situations where security is not a concern it may be safe to enable
|
||||
* ASSUME_SANE.
|
||||
*/
|
||||
enum {
|
||||
/*
|
||||
* This does essentially no checks. Only the latest device-tree
|
||||
* version is correctly handled. Inconsistencies or errors in the device
|
||||
* tree may cause undefined behaviour or crashes. Invalid parameters
|
||||
* passed to libfdt may do the same.
|
||||
*
|
||||
* If an error occurs when modifying the tree it may leave the tree in
|
||||
* an intermediate (but valid) state. As an example, adding a property
|
||||
* where there is insufficient space may result in the property name
|
||||
* being added to the string table even though the property itself is
|
||||
* not added to the struct section.
|
||||
*
|
||||
* Only use this if you have a fully validated device tree with
|
||||
* the latest supported version and wish to minimise code size.
|
||||
*/
|
||||
ASSUME_PERFECT = 0xff,
|
||||
|
||||
/*
|
||||
* This assumes that the device tree is sane. i.e. header metadata
|
||||
* and basic hierarchy are correct.
|
||||
*
|
||||
* With this assumption enabled, normal device trees produced by libfdt
|
||||
* and the compiler should be handled safely. Malicious device trees and
|
||||
* complete garbage may cause libfdt to behave badly or crash. Truncated
|
||||
* device trees (e.g. those only partially loaded) can also cause
|
||||
* problems.
|
||||
*
|
||||
* Note: Only checks that relate exclusively to the device tree itself
|
||||
* (not the parameters passed to libfdt) are disabled by this
|
||||
* assumption. This includes checking headers, tags and the like.
|
||||
*/
|
||||
ASSUME_VALID_DTB = 1 << 0,
|
||||
|
||||
/*
|
||||
* This builds on ASSUME_VALID_DTB and further assumes that libfdt
|
||||
* functions are called with valid parameters, i.e. not trigger
|
||||
* FDT_ERR_BADOFFSET or offsets that are out of bounds. It disables any
|
||||
* extensive checking of parameters and the device tree, making various
|
||||
* assumptions about correctness.
|
||||
*
|
||||
* It doesn't make sense to enable this assumption unless
|
||||
* ASSUME_VALID_DTB is also enabled.
|
||||
*/
|
||||
ASSUME_VALID_INPUT = 1 << 1,
|
||||
|
||||
/*
|
||||
* This disables checks for device-tree version and removes all code
|
||||
* which handles older versions.
|
||||
*
|
||||
* Only enable this if you know you have a device tree with the latest
|
||||
* version.
|
||||
*/
|
||||
ASSUME_LATEST = 1 << 2,
|
||||
|
||||
/*
|
||||
* This assumes that it is OK for a failed addition to the device tree,
|
||||
* due to lack of space or some other problem, to skip any rollback
|
||||
* steps (such as dropping the property name from the string table).
|
||||
* This is safe to enable in most circumstances, even though it may
|
||||
* leave the tree in a sub-optimal state.
|
||||
*/
|
||||
ASSUME_NO_ROLLBACK = 1 << 3,
|
||||
|
||||
/*
|
||||
* This assumes that the device tree components appear in a 'convenient'
|
||||
* order, i.e. the memory reservation block first, then the structure
|
||||
* block and finally the string block.
|
||||
*
|
||||
* This order is not specified by the device-tree specification,
|
||||
* but is expected by libfdt. The device-tree compiler always created
|
||||
* device trees with this order.
|
||||
*
|
||||
* This assumption disables a check in fdt_open_into() and removes the
|
||||
* ability to fix the problem there. This is safe if you know that the
|
||||
* device tree is correctly ordered. See fdt_blocks_misordered_().
|
||||
*/
|
||||
ASSUME_LIBFDT_ORDER = 1 << 4,
|
||||
|
||||
/*
|
||||
* This assumes that libfdt itself does not have any internal bugs. It
|
||||
* drops certain checks that should never be needed unless libfdt has an
|
||||
* undiscovered bug.
|
||||
*
|
||||
* This can generally be considered safe to enable.
|
||||
*/
|
||||
ASSUME_LIBFDT_FLAWLESS = 1 << 5,
|
||||
};
|
||||
|
||||
/**
|
||||
* can_assume_() - check if a particular assumption is enabled
|
||||
*
|
||||
* @mask: Mask to check (ASSUME_...)
|
||||
* @return true if that assumption is enabled, else false
|
||||
*/
|
||||
static inline bool can_assume_(int mask)
|
||||
{
|
||||
return FDT_ASSUME_MASK & mask;
|
||||
}
|
||||
|
||||
/** helper macros for checking assumptions */
|
||||
#define can_assume(_assume) can_assume_(ASSUME_ ## _assume)
|
||||
|
||||
#endif /* LIBFDT_INTERNAL_H */
|
||||
Executable
+573
File diff suppressed because it is too large
Load Diff
Executable
+74
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-08-25 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#ifndef __OFW_INTERNAL_H__
|
||||
#define __OFW_INTERNAL_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include <drivers/ofw.h>
|
||||
|
||||
#define OFW_PHANDLE_MIN 1
|
||||
#define OFW_PHANDLE_MAX FDT_MAX_PHANDLE
|
||||
|
||||
#define OFW_NODE_MAX_DEPTH 64
|
||||
#define OFW_NODE_MIN_HASH 128
|
||||
|
||||
#define OFW_ROOT_NODE_ADDR_CELLS_DEFAULT 1
|
||||
#define OFW_ROOT_NODE_SIZE_CELLS_DEFAULT 1
|
||||
|
||||
struct fdt_info
|
||||
{
|
||||
/* Always "/", because we save "ofw" information in root node. */
|
||||
char name[sizeof("/")];
|
||||
|
||||
/* Information start */
|
||||
void *fdt;
|
||||
|
||||
/* Only root can use */
|
||||
struct fdt_reserve_entry *rsvmap;
|
||||
rt_size_t rsvmap_nr;
|
||||
};
|
||||
|
||||
struct alias_info
|
||||
{
|
||||
rt_list_t list;
|
||||
|
||||
int id;
|
||||
const char *tag;
|
||||
rt_size_t tag_len;
|
||||
|
||||
struct rt_ofw_node *np;
|
||||
};
|
||||
|
||||
struct bus_ranges
|
||||
{
|
||||
rt_size_t nr;
|
||||
|
||||
rt_uint64_t *child_addr;
|
||||
rt_uint64_t *parent_addr;
|
||||
rt_uint64_t *child_size;
|
||||
};
|
||||
|
||||
extern struct rt_ofw_node *ofw_node_root;
|
||||
extern struct rt_ofw_node *ofw_node_cpus;
|
||||
extern struct rt_ofw_node *ofw_node_chosen;
|
||||
extern struct rt_ofw_node *ofw_node_aliases;
|
||||
extern struct rt_ofw_node *ofw_node_reserved_memory;
|
||||
|
||||
extern struct rt_fdt_earlycon fdt_earlycon;
|
||||
|
||||
#define ofw_static_cast(to_type, value) \
|
||||
(to_type)(((value) >> ((sizeof(value) - sizeof(to_type)) * 8)))
|
||||
|
||||
rt_err_t ofw_alias_scan(void);
|
||||
rt_err_t ofw_phandle_hash_reset(rt_phandle min, rt_phandle max);
|
||||
|
||||
#endif /* __OFW_INTERNAL_H__ */
|
||||
Executable
+197
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-10-19 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#include <drivers/ofw_raw.h>
|
||||
|
||||
int fdt_add_subnode_possible(void *fdt, int parentoffset, const char *name)
|
||||
{
|
||||
int nodeoffset;
|
||||
|
||||
if ((nodeoffset = fdt_add_subnode(fdt, parentoffset, name)) < 0)
|
||||
{
|
||||
fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + FDT_PADDING_SIZE);
|
||||
nodeoffset = fdt_add_subnode(fdt, parentoffset, name);
|
||||
}
|
||||
|
||||
return nodeoffset;
|
||||
}
|
||||
|
||||
int fdt_add_mem_rsv_possible(void *fdt, size_t addr, size_t size)
|
||||
{
|
||||
int err = 0;
|
||||
|
||||
if (fdt_add_mem_rsv(fdt, addr, size) < 0)
|
||||
{
|
||||
fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + FDT_PADDING_SIZE);
|
||||
err = fdt_add_mem_rsv(fdt, addr, size);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name, uint64_t val, bool is_u64)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (is_u64)
|
||||
{
|
||||
err = fdt_setprop_u64(fdt, nodeoffset, name, val);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#define FDT_RAW_GET_VAL_FLAG(std_type, s, sz) \
|
||||
int fdt_getprop_##std_type##sz(void *fdt, int nodeoffset, \
|
||||
const char *name, s##int##sz##_t *out_value, int *lenp) \
|
||||
{ \
|
||||
int err = -FDT_ERR_NOTFOUND; \
|
||||
if (fdt && nodeoffset >= 0 && name && out_value) \
|
||||
{ \
|
||||
const fdt##sz##_t *ptr; \
|
||||
if ((ptr = fdt_getprop(fdt, nodeoffset, name, lenp))) \
|
||||
{ \
|
||||
*out_value = fdt##sz##_to_cpu(*ptr); \
|
||||
err = 0; \
|
||||
} \
|
||||
} \
|
||||
return err; \
|
||||
}
|
||||
|
||||
#define FDT_RAW_GET_VAL(size) \
|
||||
FDT_RAW_GET_VAL_FLAG(u, u, size) \
|
||||
FDT_RAW_GET_VAL_FLAG(s, , size)
|
||||
|
||||
FDT_RAW_GET_VAL(64)
|
||||
FDT_RAW_GET_VAL(32)
|
||||
FDT_RAW_GET_VAL(16)
|
||||
FDT_RAW_GET_VAL(8)
|
||||
|
||||
#undef FDT_RAW_GET_VAL
|
||||
#undef FDT_RAW_GET_VAL_FLAG
|
||||
|
||||
int fdt_io_addr_cells(void *fdt, int nodeoffset)
|
||||
{
|
||||
int cells = -1;
|
||||
int parentoffset = fdt_parent_offset(fdt, nodeoffset);
|
||||
|
||||
for (; parentoffset >= 0 ; parentoffset = fdt_parent_offset(fdt, parentoffset))
|
||||
{
|
||||
const fdt32_t *cells_tmp = fdt_getprop(fdt, parentoffset, "#address-cells", NULL);
|
||||
|
||||
if (cells_tmp)
|
||||
{
|
||||
cells = fdt32_to_cpu(*cells_tmp);
|
||||
}
|
||||
}
|
||||
|
||||
if (cells < 0)
|
||||
{
|
||||
cells = fdt_address_cells(fdt, nodeoffset);
|
||||
}
|
||||
|
||||
return cells;
|
||||
}
|
||||
|
||||
int fdt_io_size_cells(void *fdt, int nodeoffset)
|
||||
{
|
||||
int cells = -1;
|
||||
int parentoffset = fdt_parent_offset(fdt, nodeoffset);
|
||||
|
||||
for (; parentoffset >= 0 ; parentoffset = fdt_parent_offset(fdt, parentoffset))
|
||||
{
|
||||
const fdt32_t *cells_tmp = fdt_getprop(fdt, parentoffset, "#size-cells", NULL);
|
||||
|
||||
if (cells_tmp)
|
||||
{
|
||||
cells = fdt32_to_cpu(*cells_tmp);
|
||||
}
|
||||
}
|
||||
|
||||
if (cells < 0)
|
||||
{
|
||||
cells = fdt_size_cells(fdt, nodeoffset);
|
||||
}
|
||||
|
||||
return cells;
|
||||
}
|
||||
|
||||
int fdt_install_initrd(void *fdt, char *os_name, size_t initrd_addr, size_t initrd_size)
|
||||
{
|
||||
int err = -FDT_ERR_NOTFOUND;
|
||||
int chosen_offset = -1, root_off = fdt_path_offset(fdt, "/");
|
||||
|
||||
if (root_off >= 0)
|
||||
{
|
||||
chosen_offset = fdt_subnode_offset(fdt, root_off, "chosen");
|
||||
|
||||
if (chosen_offset == -FDT_ERR_NOTFOUND)
|
||||
{
|
||||
chosen_offset = fdt_add_subnode_possible(fdt, root_off, "chosen");
|
||||
}
|
||||
}
|
||||
|
||||
if (chosen_offset >= 0)
|
||||
{
|
||||
uint64_t addr, size;
|
||||
|
||||
err = 0;
|
||||
|
||||
/* Update the entry */
|
||||
for (int i = fdt_num_mem_rsv(fdt) - 1; i >= 0; --i)
|
||||
{
|
||||
fdt_get_mem_rsv(fdt, i, &addr, &size);
|
||||
|
||||
if (addr == initrd_addr)
|
||||
{
|
||||
fdt_del_mem_rsv(fdt, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Add the memory */
|
||||
if (fdt_add_mem_rsv(fdt, initrd_addr, initrd_size) < 0)
|
||||
{
|
||||
/* Move the memory */
|
||||
fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + FDT_PADDING_SIZE);
|
||||
|
||||
if (fdt_add_mem_rsv(fdt, initrd_addr, initrd_size) < 0)
|
||||
{
|
||||
err = -FDT_ERR_NOSPACE;
|
||||
}
|
||||
}
|
||||
|
||||
if (!err)
|
||||
{
|
||||
size_t name_len;
|
||||
char initrd_name[64];
|
||||
bool is_u64 = (fdt_io_addr_cells(fdt, root_off) == 2);
|
||||
|
||||
if (!os_name)
|
||||
{
|
||||
os_name = "rt-thread";
|
||||
}
|
||||
|
||||
name_len = strlen(initrd_name);
|
||||
|
||||
strncpy(&initrd_name[name_len], ",initrd-start", sizeof(initrd_name) - name_len);
|
||||
fdt_setprop_uxx(fdt, chosen_offset, initrd_name, initrd_addr, is_u64);
|
||||
|
||||
strncpy(&initrd_name[name_len], ",initrd-end", sizeof(initrd_name) - name_len);
|
||||
fdt_setprop_uxx(fdt, chosen_offset, initrd_name, initrd_addr + initrd_size, is_u64);
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
menuconfig RT_USING_PIC
|
||||
bool "Using Programmable Interrupt Controller (PIC)"
|
||||
select RT_USING_BITMAP
|
||||
depends on RT_USING_DM
|
||||
default n
|
||||
|
||||
config MAX_HANDLERS
|
||||
int "IRQ max handlers"
|
||||
depends on RT_USING_PIC
|
||||
range 1 4294967294
|
||||
default 256
|
||||
@@ -0,0 +1,15 @@
|
||||
from building import *
|
||||
|
||||
group = []
|
||||
|
||||
if not GetDepend(['RT_USING_PIC']):
|
||||
Return('group')
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
CPPPATH = [cwd + '/../include']
|
||||
|
||||
src = ['pic.c']
|
||||
|
||||
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user