1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2023-04-20     ErikChan     the first version
9  */
10 
11 #ifndef __CORE_DM_H__
12 #define __CORE_DM_H__
13 
14 #include <rthw.h>
15 #include <rtdef.h>
16 #include <bitmap.h>
17 #include <ioremap.h>
18 #include <drivers/misc.h>
19 #include <drivers/byteorder.h>
20 #include <drivers/core/master_id.h>
21 
22 #ifndef RT_CPUS_NR
23 #define RT_CPUS_NR 1
24 #endif
25 
26 #ifndef RT_USING_SMP
27 extern int rt_hw_cpu_id(void);
28 #endif
29 
30 #ifndef RT_MAX_IPI
31 #define RT_MAX_IPI 0
32 #endif
33 
34 void rt_dm_secondary_cpu_init(void);
35 
36 /* ID Allocation */
37 struct rt_dm_ida
38 {
39     rt_uint8_t master_id;
40 
41 #define RT_DM_IDA_NUM 256
42     RT_BITMAP_DECLARE(map, RT_DM_IDA_NUM);
43     struct rt_spinlock lock;
44 };
45 
46 #define RT_DM_IDA_INIT(id)  { .master_id = MASTER_ID_##id }
47 #define rt_dm_ida_init(ida, id)         \
48 do {                                    \
49     (ida)->master_id = MASTER_ID_##id;  \
50     rt_spin_lock_init(&(ida)->lock);    \
51 } while (0)
52 
53 int rt_dm_ida_alloc(struct rt_dm_ida *ida);
54 rt_bool_t rt_dm_ida_take(struct rt_dm_ida *ida, int id);
55 void rt_dm_ida_free(struct rt_dm_ida *ida, int id);
56 
57 rt_device_t rt_dm_device_find(int master_id, int device_id);
58 
59 int rt_dm_dev_set_name_auto(rt_device_t dev, const char *prefix);
60 int rt_dm_dev_get_name_id(rt_device_t dev);
61 
62 int rt_dm_dev_set_name(rt_device_t dev, const char *format, ...);
63 const char *rt_dm_dev_get_name(rt_device_t dev);
64 
65 int rt_dm_dev_get_address_count(rt_device_t dev);
66 rt_err_t rt_dm_dev_get_address(rt_device_t dev, int index,
67         rt_uint64_t *out_address, rt_uint64_t *out_size);
68 rt_err_t rt_dm_dev_get_address_by_name(rt_device_t dev, const char *name,
69         rt_uint64_t *out_address, rt_uint64_t *out_size);
70 int rt_dm_dev_get_address_array(rt_device_t dev, int nr, rt_uint64_t *out_regs);
71 
72 void *rt_dm_dev_iomap(rt_device_t dev, int index);
73 void *rt_dm_dev_iomap_by_name(rt_device_t dev, const char *name);
74 
75 int rt_dm_dev_get_irq_count(rt_device_t dev);
76 int rt_dm_dev_get_irq(rt_device_t dev, int index);
77 int rt_dm_dev_get_irq_by_name(rt_device_t dev, const char *name);
78 
79 void rt_dm_dev_bind_fwdata(rt_device_t dev, void *fw_np, void *data);
80 void rt_dm_dev_unbind_fwdata(rt_device_t dev, void *fw_np);
81 
82 int rt_dm_dev_prop_read_u8_array_index(rt_device_t dev, const char *propname,
83         int index, int nr, rt_uint8_t *out_values);
84 int rt_dm_dev_prop_read_u16_array_index(rt_device_t dev, const char *propname,
85         int index, int nr, rt_uint16_t *out_values);
86 int rt_dm_dev_prop_read_u32_array_index(rt_device_t dev, const char *propname,
87         int index, int nr, rt_uint32_t *out_values);
88 int rt_dm_dev_prop_read_u64_array_index(rt_device_t dev, const char *propname,
89         int index, int nr, rt_uint64_t *out_values);
90 int rt_dm_dev_prop_read_string_array_index(rt_device_t dev, const char *propname,
91         int index, int nr, const char **out_strings);
92 
93 int rt_dm_dev_prop_count_of_size(rt_device_t dev, const char *propname, int size);
94 int rt_dm_dev_prop_index_of_string(rt_device_t dev, const char *propname, const char *string);
95 
96 rt_bool_t rt_dm_dev_prop_read_bool(rt_device_t dev, const char *propname);
97 
rt_dm_dev_prop_read_u8_index(rt_device_t dev,const char * propname,int index,rt_uint8_t * out_value)98 rt_inline rt_err_t rt_dm_dev_prop_read_u8_index(rt_device_t dev, const char *propname,
99         int index, rt_uint8_t *out_value)
100 {
101     int nr = rt_dm_dev_prop_read_u8_array_index(dev, propname, index, 1, out_value);
102 
103     return nr > 0 ? RT_EOK : (rt_err_t)nr;
104 }
105 
rt_dm_dev_prop_read_u16_index(rt_device_t dev,const char * propname,int index,rt_uint16_t * out_value)106 rt_inline rt_err_t rt_dm_dev_prop_read_u16_index(rt_device_t dev, const char *propname,
107         int index, rt_uint16_t *out_value)
108 {
109     int nr = rt_dm_dev_prop_read_u16_array_index(dev, propname, index, 1, out_value);
110 
111     return nr > 0 ? RT_EOK : (rt_err_t)nr;
112 }
113 
rt_dm_dev_prop_read_u32_index(rt_device_t dev,const char * propname,int index,rt_uint32_t * out_value)114 rt_inline rt_err_t rt_dm_dev_prop_read_u32_index(rt_device_t dev, const char *propname,
115         int index, rt_uint32_t *out_value)
116 {
117     int nr = rt_dm_dev_prop_read_u32_array_index(dev, propname, index, 1, out_value);
118 
119     return nr > 0 ? RT_EOK : (rt_err_t)nr;
120 }
121 
rt_dm_dev_prop_read_u64_index(rt_device_t dev,const char * propname,int index,rt_uint64_t * out_value)122 rt_inline rt_err_t rt_dm_dev_prop_read_u64_index(rt_device_t dev, const char *propname,
123         int index, rt_uint64_t *out_value)
124 {
125     int nr = rt_dm_dev_prop_read_u64_array_index(dev, propname, index, 1, out_value);
126 
127     return nr > 0 ? RT_EOK : (rt_err_t)nr;
128 }
129 
rt_dm_dev_prop_read_string_index(rt_device_t dev,const char * propname,int index,const char ** out_string)130 rt_inline rt_err_t rt_dm_dev_prop_read_string_index(rt_device_t dev, const char *propname,
131         int index, const char **out_string)
132 {
133     int nr = rt_dm_dev_prop_read_string_array_index(dev, propname, index, 1, out_string);
134 
135     return nr > 0 ? RT_EOK : (rt_err_t)nr;
136 }
137 
rt_dm_dev_prop_read_u8(rt_device_t dev,const char * propname,rt_uint8_t * out_value)138 rt_inline rt_err_t rt_dm_dev_prop_read_u8(rt_device_t dev, const char *propname,
139         rt_uint8_t *out_value)
140 {
141     return rt_dm_dev_prop_read_u8_index(dev, propname, 0, out_value);
142 }
143 
rt_dm_dev_prop_read_u16(rt_device_t dev,const char * propname,rt_uint16_t * out_value)144 rt_inline rt_err_t rt_dm_dev_prop_read_u16(rt_device_t dev, const char *propname,
145         rt_uint16_t *out_value)
146 {
147     return rt_dm_dev_prop_read_u16_index(dev, propname, 0, out_value);
148 }
149 
rt_dm_dev_prop_read_u32(rt_device_t dev,const char * propname,rt_uint32_t * out_value)150 rt_inline rt_err_t rt_dm_dev_prop_read_u32(rt_device_t dev, const char *propname,
151         rt_uint32_t *out_value)
152 {
153     return rt_dm_dev_prop_read_u32_index(dev, propname, 0, out_value);
154 }
155 
rt_dm_dev_prop_read_s32(rt_device_t dev,const char * propname,rt_int32_t * out_value)156 rt_inline rt_err_t rt_dm_dev_prop_read_s32(rt_device_t dev, const char *propname,
157         rt_int32_t *out_value)
158 {
159     return rt_dm_dev_prop_read_u32_index(dev, propname, 0, (rt_uint32_t *)out_value);
160 }
161 
rt_dm_dev_prop_read_u64(rt_device_t dev,const char * propname,rt_uint64_t * out_value)162 rt_inline rt_err_t rt_dm_dev_prop_read_u64(rt_device_t dev, const char *propname,
163         rt_uint64_t *out_value)
164 {
165     return rt_dm_dev_prop_read_u64_index(dev, propname, 0, out_value);
166 }
167 
rt_dm_dev_prop_read_string(rt_device_t dev,const char * propname,const char ** out_string)168 rt_inline rt_err_t rt_dm_dev_prop_read_string(rt_device_t dev, const char *propname,
169         const char **out_string)
170 {
171     return rt_dm_dev_prop_read_string_index(dev, propname, 0, out_string);
172 }
173 
rt_dm_dev_prop_count_of_u8(rt_device_t dev,const char * propname)174 rt_inline int rt_dm_dev_prop_count_of_u8(rt_device_t dev, const char *propname)
175 {
176     return rt_dm_dev_prop_count_of_size(dev, propname, sizeof(rt_uint8_t));
177 }
178 
rt_dm_dev_prop_count_of_u16(rt_device_t dev,const char * propname)179 rt_inline int rt_dm_dev_prop_count_of_u16(rt_device_t dev, const char *propname)
180 {
181     return rt_dm_dev_prop_count_of_size(dev, propname, sizeof(rt_uint16_t));
182 }
183 
rt_dm_dev_prop_count_of_u32(rt_device_t dev,const char * propname)184 rt_inline int rt_dm_dev_prop_count_of_u32(rt_device_t dev, const char *propname)
185 {
186     return rt_dm_dev_prop_count_of_size(dev, propname, sizeof(rt_uint32_t));
187 }
188 
rt_dm_dev_prop_count_of_u64(rt_device_t dev,const char * propname)189 rt_inline int rt_dm_dev_prop_count_of_u64(rt_device_t dev, const char *propname)
190 {
191     return rt_dm_dev_prop_count_of_size(dev, propname, sizeof(rt_uint64_t));
192 }
193 
194 #endif /* __RT_DM_H__ */
195