1
2
3
4
5
6
7#include <common.h>
8#include <dm.h>
9#include <dm/of_access.h>
10#include <mapmem.h>
11#include <asm/global_data.h>
12#include <asm/types.h>
13#include <asm/io.h>
14#include <linux/ioport.h>
15
16int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp)
17{
18 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
19}
20
21int dev_read_u32_default(const struct udevice *dev, const char *propname,
22 int def)
23{
24 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
25}
26
27int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
28 u32 *outp)
29{
30 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
31}
32
33u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
34 int index, u32 def)
35{
36 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
37 def);
38}
39
40int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
41{
42 return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
43}
44
45int dev_read_s32_default(const struct udevice *dev, const char *propname,
46 int def)
47{
48 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
49}
50
51int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp)
52{
53 u32 val;
54 int ret;
55
56 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
57 if (ret)
58 return ret;
59 *outp = val;
60
61 return 0;
62}
63
64int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp)
65{
66 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
67}
68
69u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
70 u64 def)
71{
72 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
73}
74
75const char *dev_read_string(const struct udevice *dev, const char *propname)
76{
77 return ofnode_read_string(dev_ofnode(dev), propname);
78}
79
80bool dev_read_bool(const struct udevice *dev, const char *propname)
81{
82 return ofnode_read_bool(dev_ofnode(dev), propname);
83}
84
85ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name)
86{
87 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
88}
89
90ofnode dev_read_first_subnode(const struct udevice *dev)
91{
92 return ofnode_first_subnode(dev_ofnode(dev));
93}
94
95ofnode dev_read_next_subnode(ofnode node)
96{
97 return ofnode_next_subnode(node);
98}
99
100int dev_read_size(const struct udevice *dev, const char *propname)
101{
102 return ofnode_read_size(dev_ofnode(dev), propname);
103}
104
105fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index)
106{
107 if (ofnode_is_np(dev_ofnode(dev)))
108 return ofnode_get_addr_index(dev_ofnode(dev), index);
109 else
110 return devfdt_get_addr_index(dev, index);
111}
112
113fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
114 fdt_size_t *size)
115{
116 if (ofnode_is_np(dev_ofnode(dev)))
117 return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
118 else
119 return devfdt_get_addr_size_index(dev, index, size);
120}
121
122void *dev_remap_addr_index(const struct udevice *dev, int index)
123{
124 fdt_addr_t addr = dev_read_addr_index(dev, index);
125
126 if (addr == FDT_ADDR_T_NONE)
127 return NULL;
128
129 return map_physmem(addr, 0, MAP_NOCACHE);
130}
131
132fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name)
133{
134 int index = dev_read_stringlist_search(dev, "reg-names", name);
135
136 if (index < 0)
137 return FDT_ADDR_T_NONE;
138 else
139 return dev_read_addr_index(dev, index);
140}
141
142fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
143 fdt_size_t *size)
144{
145 int index = dev_read_stringlist_search(dev, "reg-names", name);
146
147 if (index < 0)
148 return FDT_ADDR_T_NONE;
149 else
150 return dev_read_addr_size_index(dev, index, size);
151}
152
153void *dev_remap_addr_name(const struct udevice *dev, const char *name)
154{
155 fdt_addr_t addr = dev_read_addr_name(dev, name);
156
157 if (addr == FDT_ADDR_T_NONE)
158 return NULL;
159
160 return map_physmem(addr, 0, MAP_NOCACHE);
161}
162
163fdt_addr_t dev_read_addr(const struct udevice *dev)
164{
165 return dev_read_addr_index(dev, 0);
166}
167
168void *dev_read_addr_ptr(const struct udevice *dev)
169{
170 fdt_addr_t addr = dev_read_addr(dev);
171
172 return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr;
173}
174
175void *dev_remap_addr(const struct udevice *dev)
176{
177 return dev_remap_addr_index(dev, 0);
178}
179
180fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *property,
181 fdt_size_t *sizep)
182{
183 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
184}
185
186const char *dev_read_name(const struct udevice *dev)
187{
188 return ofnode_get_name(dev_ofnode(dev));
189}
190
191int dev_read_stringlist_search(const struct udevice *dev, const char *property,
192 const char *string)
193{
194 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
195}
196
197int dev_read_string_index(const struct udevice *dev, const char *propname,
198 int index, const char **outp)
199{
200 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
201}
202
203int dev_read_string_count(const struct udevice *dev, const char *propname)
204{
205 return ofnode_read_string_count(dev_ofnode(dev), propname);
206}
207
208int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
209 const char *cells_name, int cell_count,
210 int index, struct ofnode_phandle_args *out_args)
211{
212 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
213 cells_name, cell_count, index,
214 out_args);
215}
216
217int dev_count_phandle_with_args(const struct udevice *dev,
218 const char *list_name, const char *cells_name,
219 int cell_count)
220{
221 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
222 cells_name, cell_count);
223}
224
225int dev_read_addr_cells(const struct udevice *dev)
226{
227 return ofnode_read_addr_cells(dev_ofnode(dev));
228}
229
230int dev_read_size_cells(const struct udevice *dev)
231{
232 return ofnode_read_size_cells(dev_ofnode(dev));
233}
234
235int dev_read_simple_addr_cells(const struct udevice *dev)
236{
237 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
238}
239
240int dev_read_simple_size_cells(const struct udevice *dev)
241{
242 return ofnode_read_simple_size_cells(dev_ofnode(dev));
243}
244
245int dev_read_phandle(const struct udevice *dev)
246{
247 ofnode node = dev_ofnode(dev);
248
249 if (ofnode_is_np(node))
250 return ofnode_to_np(node)->phandle;
251 else
252 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
253}
254
255const void *dev_read_prop(const struct udevice *dev, const char *propname,
256 int *lenp)
257{
258 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
259}
260
261int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
262{
263 return ofnode_get_first_property(dev_ofnode(dev), prop);
264}
265
266int dev_read_next_prop(struct ofprop *prop)
267{
268 return ofnode_get_next_property(prop);
269}
270
271const void *dev_read_prop_by_prop(struct ofprop *prop,
272 const char **propname, int *lenp)
273{
274 return ofnode_get_property_by_prop(prop, propname, lenp);
275}
276
277int dev_read_alias_seq(const struct udevice *dev, int *devnump)
278{
279 ofnode node = dev_ofnode(dev);
280 const char *uc_name = dev->uclass->uc_drv->name;
281 int ret = -ENOTSUPP;
282
283 if (ofnode_is_np(node)) {
284 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
285 if (ret >= 0) {
286 *devnump = ret;
287 ret = 0;
288 }
289 } else {
290#if CONFIG_IS_ENABLED(OF_CONTROL)
291 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
292 ofnode_to_offset(node), devnump);
293#endif
294 }
295
296 return ret;
297}
298
299int dev_read_u32_array(const struct udevice *dev, const char *propname,
300 u32 *out_values, size_t sz)
301{
302 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
303}
304
305const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
306 const char *propname, size_t sz)
307{
308 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
309}
310
311int dev_read_enabled(const struct udevice *dev)
312{
313 ofnode node = dev_ofnode(dev);
314
315 if (ofnode_is_np(node))
316 return of_device_is_available(ofnode_to_np(node));
317 else
318 return fdtdec_get_is_enabled(gd->fdt_blob,
319 ofnode_to_offset(node));
320}
321
322int dev_read_resource(const struct udevice *dev, uint index,
323 struct resource *res)
324{
325 return ofnode_read_resource(dev_ofnode(dev), index, res);
326}
327
328int dev_read_resource_byname(const struct udevice *dev, const char *name,
329 struct resource *res)
330{
331 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
332}
333
334u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
335{
336 return ofnode_translate_address(dev_ofnode(dev), in_addr);
337}
338
339u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
340{
341 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
342}
343
344int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
345 dma_addr_t *bus, u64 *size)
346{
347 return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
348}
349
350int dev_read_alias_highest_id(const char *stem)
351{
352 if (of_live_active())
353 return of_alias_get_highest_id(stem);
354
355 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
356}
357
358fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
359{
360 ulong addr;
361
362 addr = dev_read_addr(dev);
363 if (addr == FDT_ADDR_T_NONE && !of_live_active())
364 addr = devfdt_get_addr_pci(dev);
365
366 return addr;
367}
368
369int dev_get_child_count(const struct udevice *dev)
370{
371 return ofnode_get_child_count(dev_ofnode(dev));
372}
373
374int dev_read_pci_bus_range(const struct udevice *dev,
375 struct resource *res)
376{
377 const u32 *values;
378 int len;
379
380 values = dev_read_prop(dev, "bus-range", &len);
381 if (!values || len < sizeof(*values) * 2)
382 return -EINVAL;
383
384 res->start = *values++;
385 res->end = *values;
386
387 return 0;
388}
389
390int dev_decode_display_timing(const struct udevice *dev, int index,
391 struct display_timing *config)
392{
393 return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
394}
395