diff options
| author | guo <[email protected]> | 2023-07-06 16:52:30 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-06 16:52:30 +0800 |
| commit | 78867912c114966d49e003db2ffb3407a5017b46 (patch) | |
| tree | e8b34c99bbf57feb4e1e976993d01ef6f9d3fd13 /components/drivers/include | |
| parent | c7be260665c9c753b343141968755d47deadd753 (diff) | |
| parent | fa10a99c3ccecd6113d30842a05e1b853cf7d1cb (diff) | |
Merge pull request #7788 from ErikChanHub/master
【dd2.0】Support Open Firmware API
Diffstat (limited to 'components/drivers/include')
| -rw-r--r-- | components/drivers/include/drivers/byteorder.h | 58 | ||||
| -rwxr-xr-x | components/drivers/include/drivers/core/rtdm.h | 50 | ||||
| -rw-r--r-- | components/drivers/include/drivers/misc.h | 86 | ||||
| -rw-r--r-- | components/drivers/include/drivers/ofw.h | 433 | ||||
| -rwxr-xr-x | components/drivers/include/drivers/ofw_fdt.h | 78 | ||||
| -rwxr-xr-x | components/drivers/include/drivers/ofw_io.h | 32 | ||||
| -rwxr-xr-x | components/drivers/include/drivers/ofw_irq.h | 28 | ||||
| -rwxr-xr-x | components/drivers/include/drivers/ofw_raw.h | 66 | ||||
| -rwxr-xr-x | components/drivers/include/drivers/pic.h | 167 | ||||
| -rw-r--r-- | components/drivers/include/rtdevice.h | 16 |
10 files changed, 1014 insertions, 0 deletions
diff --git a/components/drivers/include/drivers/byteorder.h b/components/drivers/include/drivers/byteorder.h new file mode 100644 index 0000000000..30decb5440 --- /dev/null +++ b/components/drivers/include/drivers/byteorder.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-25 GuEe-GUI the first version + */ + +#ifndef __BYTEORDER__ +#define __BYTEORDER__ + +#ifdef __CHECKER__ +#define __bitwise __attribute__((bitwise)) +#else +#define __bitwise +#endif + +typedef rt_uint16_t __bitwise rt_le16_t; +typedef rt_uint32_t __bitwise rt_le32_t; +typedef rt_uint64_t __bitwise rt_le64_t; +typedef rt_uint16_t __bitwise rt_be16_t; +typedef rt_uint32_t __bitwise rt_be32_t; +typedef rt_uint64_t __bitwise rt_be64_t; + +/* gcc defines __BIG_ENDIAN__ on big endian targets */ +#if defined(__BIG_ENDIAN__) || defined(ARCH_CPU_BIG_ENDIAN) +#define rt_cpu_to_be16(x) (x) +#define rt_cpu_to_be32(x) (x) +#define rt_cpu_to_be64(x) (x) +#define rt_be16_to_cpu(x) (x) +#define rt_be32_to_cpu(x) (x) +#define rt_be64_to_cpu(x) (x) +#define rt_le16_to_cpu(x) __builtin_bswap16(x) +#define rt_le32_to_cpu(x) __builtin_bswap32(x) +#define rt_le64_to_cpu(x) __builtin_bswap64(x) +#define rt_cpu_to_le16(x) __builtin_bswap16(x) +#define rt_cpu_to_le32(x) __builtin_bswap32(x) +#define rt_cpu_to_le64(x) __builtin_bswap64(x) +#else +#define rt_cpu_to_be16(x) __builtin_bswap16(x) +#define rt_cpu_to_be32(x) __builtin_bswap32(x) +#define rt_cpu_to_be64(x) __builtin_bswap64(x) +#define rt_be16_to_cpu(x) __builtin_bswap16(x) +#define rt_be32_to_cpu(x) __builtin_bswap32(x) +#define rt_be64_to_cpu(x) __builtin_bswap64(x) +#define rt_le16_to_cpu(x) (x) +#define rt_le32_to_cpu(x) (x) +#define rt_le64_to_cpu(x) (x) +#define rt_cpu_to_le16(x) (x) +#define rt_cpu_to_le32(x) (x) +#define rt_cpu_to_le64(x) (x) +#endif /* __BIG_ENDIAN__ || ARCH_CPU_BIG_ENDIAN */ + +#undef __bitwise + +#endif /* __BYTEORDER__ */ diff --git a/components/drivers/include/drivers/core/rtdm.h b/components/drivers/include/drivers/core/rtdm.h new file mode 100755 index 0000000000..95d3c259b8 --- /dev/null +++ b/components/drivers/include/drivers/core/rtdm.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-04-20 ErikChan the first version + */ + +#ifndef __RT_DM_H__ +#define __RT_DM_H__ + +#include <rthw.h> +#include <rtdef.h> +#include <drivers/misc.h> +#include <drivers/byteorder.h> + +#ifndef RT_CPUS_NR +#define RT_CPUS_NR 1 +#endif + +#ifndef RT_USING_SMP +extern int rt_hw_cpu_id(void); +#endif + +void rt_dm_secondary_cpu_init(void); + +int rt_dm_set_dev_name_auto(rt_device_t dev, const char *prefix); +int rt_dm_get_dev_name_id(rt_device_t dev); + +int rt_dm_set_dev_name(rt_device_t dev, const char *format, ...); +const char *rt_dm_get_dev_name(rt_device_t dev); + +/* init cpu, memory, interrupt-controller, bus... */ +#define INIT_CORE_EXPORT(fn) INIT_EXPORT(fn, "1.0") +/* init pci/pcie, usb platform driver... */ +#define INIT_FRAMEWORK_EXPORT(fn) INIT_EXPORT(fn, "1.1") +/* init platform, user code... */ +#define INIT_PLATFORM_EXPORT(fn) INIT_EXPORT(fn, "1.2") +/* init sys-timer, clk, pinctrl... */ +#define INIT_SUBSYS_EXPORT(fn) INIT_EXPORT(fn, "1.3") +/* init early drivers */ +#define INIT_DRIVER_EARLY_EXPORT(fn) INIT_EXPORT(fn, "1.4") +/* init in secondary_cpu_c_start */ +#define INIT_SECONDARY_CPU_EXPORT(fn) INIT_EXPORT(fn, "7") +/* init after mount fs */ +#define INIT_FS_EXPORT(fn) INIT_EXPORT(fn, "6.0") + +#endif /* __RT_DM_H__ */ diff --git a/components/drivers/include/drivers/misc.h b/components/drivers/include/drivers/misc.h new file mode 100644 index 0000000000..aa2b715512 --- /dev/null +++ b/components/drivers/include/drivers/misc.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-25 GuEe-GUI the first version + */ + +#ifndef __MISC_H__ +#define __MISC_H__ + +#include <rtdef.h> + +#ifdef ARCH_CPU_64BIT +#define RT_BITS_PER_LONG 64 +#else +#define RT_BITS_PER_LONG 32 +#endif +#define RT_BITS_PER_LONG_LONG 64 + +#define RT_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) + +#define RT_DIV_ROUND_CLOSEST(x, divisor) \ +({ \ + typeof(x) __x = x; \ + typeof(divisor) __d = divisor; \ + (((typeof(x))-1) > 0 || \ + ((typeof(divisor))-1) > 0 || \ + (((__x) > 0) == ((__d) > 0))) ? \ + (((__x) + ((__d) / 2)) / (__d)) : \ + (((__x) - ((__d) / 2)) / (__d)); \ +}) + +#define RT_BIT(n) (1UL << (n)) +#define RT_BIT_ULL(n) (1ULL << (n)) +#define RT_BIT_MASK(nr) (1UL << ((nr) % RT_BITS_PER_LONG)) +#define RT_BIT_WORD(nr) ((nr) / RT_BITS_PER_LONG) + +#define RT_BITS_PER_BYTE 8 +#define RT_BITS_PER_TYPE(type) (sizeof(type) * RT_BITS_PER_BYTE) +#define RT_BITS_TO_BYTES(nr) RT_DIV_ROUND_UP(nr, RT_BITS_PER_TYPE(char)) +#define RT_BITS_TO_LONGS(nr) RT_DIV_ROUND_UP(nr, RT_BITS_PER_TYPE(long)) + +#define RT_GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (RT_BITS_PER_LONG - 1 - (h)))) +#define RT_GENMASK_ULL(h, l) (((~0ULL) << (l)) & (~0ULL >> (RT_BITS_PER_LONG_LONG - 1 - (h)))) + +#define RT_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) + +#define rt_min(x, y) \ +({ \ + typeof(x) _x = (x); \ + typeof(y) _y = (y); \ + (void) (&_x == &_y); \ + _x < _y ? _x : _y; \ +}) + +#define rt_max(x, y) \ +({ \ + typeof(x) _x = (x); \ + typeof(y) _y = (y); \ + (void) (&_x == &_y); \ + _x > _y ? _x : _y; \ +}) + +#define rt_min_t(type, x, y) \ +({ \ + type _x = (x); \ + type _y = (y); \ + _x < _y ? _x: _y; \ +}) + +#define rt_clamp(val, lo, hi) rt_min((typeof(val))rt_max(val, lo), hi) + +#define rt_do_div(n, base) \ +({ \ + rt_uint32_t _base = (base), _rem; \ + _rem = ((rt_uint64_t)(n)) % _base; \ + (n) = ((rt_uint64_t)(n)) / _base; \ + if (_rem > _base / 2) \ + ++(n); \ + _rem; \ +}) + +#endif /* __MISC_H__ */ diff --git a/components/drivers/include/drivers/ofw.h b/components/drivers/include/drivers/ofw.h new file mode 100644 index 0000000000..c760cf7e83 --- /dev/null +++ b/components/drivers/include/drivers/ofw.h @@ -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__ */ diff --git a/components/drivers/include/drivers/ofw_fdt.h b/components/drivers/include/drivers/ofw_fdt.h new file mode 100755 index 0000000000..a9c951ca40 --- /dev/null +++ b/components/drivers/include/drivers/ofw_fdt.h @@ -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__ */ diff --git a/components/drivers/include/drivers/ofw_io.h b/components/drivers/include/drivers/ofw_io.h new file mode 100755 index 0000000000..5fdfe8f5e6 --- /dev/null +++ b/components/drivers/include/drivers/ofw_io.h @@ -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__ */ diff --git a/components/drivers/include/drivers/ofw_irq.h b/components/drivers/include/drivers/ofw_irq.h new file mode 100755 index 0000000000..e9b8482eac --- /dev/null +++ b/components/drivers/include/drivers/ofw_irq.h @@ -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__ */ diff --git a/components/drivers/include/drivers/ofw_raw.h b/components/drivers/include/drivers/ofw_raw.h new file mode 100755 index 0000000000..13736c3010 --- /dev/null +++ b/components/drivers/include/drivers/ofw_raw.h @@ -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__ */ diff --git a/components/drivers/include/drivers/pic.h b/components/drivers/include/drivers/pic.h new file mode 100755 index 0000000000..5c6ac24c50 --- /dev/null +++ b/components/drivers/include/drivers/pic.h @@ -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__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index afce028b0e..06e5c7158a 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -169,6 +169,22 @@ extern "C" { #include "drivers/lcd.h" #endif +#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 } #endif |
