1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
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
dev_read_u32(const struct udevice * dev,const char * propname,u32 * outp)16 int 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
dev_read_u32_default(const struct udevice * dev,const char * propname,int def)21 int 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
dev_read_u32_index(struct udevice * dev,const char * propname,int index,u32 * outp)27 int 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
dev_read_u32_index_default(struct udevice * dev,const char * propname,int index,u32 def)33 u32 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
dev_read_s32(const struct udevice * dev,const char * propname,s32 * outp)40 int 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
dev_read_s32_default(const struct udevice * dev,const char * propname,int def)45 int 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
dev_read_u32u(const struct udevice * dev,const char * propname,uint * outp)51 int 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
dev_read_u64(const struct udevice * dev,const char * propname,u64 * outp)64 int 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
dev_read_u64_default(const struct udevice * dev,const char * propname,u64 def)69 u64 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
dev_read_string(const struct udevice * dev,const char * propname)75 const char *dev_read_string(const struct udevice *dev, const char *propname)
76 {
77 return ofnode_read_string(dev_ofnode(dev), propname);
78 }
79
dev_read_bool(const struct udevice * dev,const char * propname)80 bool dev_read_bool(const struct udevice *dev, const char *propname)
81 {
82 return ofnode_read_bool(dev_ofnode(dev), propname);
83 }
84
dev_read_subnode(const struct udevice * dev,const char * subnode_name)85 ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name)
86 {
87 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
88 }
89
dev_read_first_subnode(const struct udevice * dev)90 ofnode dev_read_first_subnode(const struct udevice *dev)
91 {
92 return ofnode_first_subnode(dev_ofnode(dev));
93 }
94
dev_read_next_subnode(ofnode node)95 ofnode dev_read_next_subnode(ofnode node)
96 {
97 return ofnode_next_subnode(node);
98 }
99
dev_read_size(const struct udevice * dev,const char * propname)100 int dev_read_size(const struct udevice *dev, const char *propname)
101 {
102 return ofnode_read_size(dev_ofnode(dev), propname);
103 }
104
dev_read_addr_index(const struct udevice * dev,int index)105 fdt_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
dev_read_addr_size_index(const struct udevice * dev,int index,fdt_size_t * size)113 fdt_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
dev_remap_addr_index(const struct udevice * dev,int index)122 void *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
dev_read_addr_name(const struct udevice * dev,const char * name)132 fdt_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
dev_read_addr_size_name(const struct udevice * dev,const char * name,fdt_size_t * size)142 fdt_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
dev_remap_addr_name(const struct udevice * dev,const char * name)153 void *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
dev_read_addr(const struct udevice * dev)163 fdt_addr_t dev_read_addr(const struct udevice *dev)
164 {
165 return dev_read_addr_index(dev, 0);
166 }
167
dev_read_addr_ptr(const struct udevice * dev)168 void *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
dev_remap_addr(const struct udevice * dev)175 void *dev_remap_addr(const struct udevice *dev)
176 {
177 return dev_remap_addr_index(dev, 0);
178 }
179
dev_read_addr_size(const struct udevice * dev,const char * property,fdt_size_t * sizep)180 fdt_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
dev_read_name(const struct udevice * dev)186 const char *dev_read_name(const struct udevice *dev)
187 {
188 return ofnode_get_name(dev_ofnode(dev));
189 }
190
dev_read_stringlist_search(const struct udevice * dev,const char * property,const char * string)191 int 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
dev_read_string_index(const struct udevice * dev,const char * propname,int index,const char ** outp)197 int 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
dev_read_string_count(const struct udevice * dev,const char * propname)203 int dev_read_string_count(const struct udevice *dev, const char *propname)
204 {
205 return ofnode_read_string_count(dev_ofnode(dev), propname);
206 }
207
dev_read_phandle_with_args(const struct udevice * dev,const char * list_name,const char * cells_name,int cell_count,int index,struct ofnode_phandle_args * out_args)208 int 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
dev_count_phandle_with_args(const struct udevice * dev,const char * list_name,const char * cells_name,int cell_count)217 int 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
dev_read_addr_cells(const struct udevice * dev)225 int dev_read_addr_cells(const struct udevice *dev)
226 {
227 return ofnode_read_addr_cells(dev_ofnode(dev));
228 }
229
dev_read_size_cells(const struct udevice * dev)230 int dev_read_size_cells(const struct udevice *dev)
231 {
232 return ofnode_read_size_cells(dev_ofnode(dev));
233 }
234
dev_read_simple_addr_cells(const struct udevice * dev)235 int dev_read_simple_addr_cells(const struct udevice *dev)
236 {
237 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
238 }
239
dev_read_simple_size_cells(const struct udevice * dev)240 int dev_read_simple_size_cells(const struct udevice *dev)
241 {
242 return ofnode_read_simple_size_cells(dev_ofnode(dev));
243 }
244
dev_read_phandle(const struct udevice * dev)245 int 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
dev_read_prop(const struct udevice * dev,const char * propname,int * lenp)255 const 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
dev_read_first_prop(const struct udevice * dev,struct ofprop * prop)261 int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
262 {
263 return ofnode_get_first_property(dev_ofnode(dev), prop);
264 }
265
dev_read_next_prop(struct ofprop * prop)266 int dev_read_next_prop(struct ofprop *prop)
267 {
268 return ofnode_get_next_property(prop);
269 }
270
dev_read_prop_by_prop(struct ofprop * prop,const char ** propname,int * lenp)271 const 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
dev_read_alias_seq(const struct udevice * dev,int * devnump)277 int 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
dev_read_u32_array(const struct udevice * dev,const char * propname,u32 * out_values,size_t sz)299 int 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
dev_read_u8_array_ptr(const struct udevice * dev,const char * propname,size_t sz)305 const 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
dev_read_enabled(const struct udevice * dev)311 int 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
dev_read_resource(const struct udevice * dev,uint index,struct resource * res)322 int 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
dev_read_resource_byname(const struct udevice * dev,const char * name,struct resource * res)328 int 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
dev_translate_address(const struct udevice * dev,const fdt32_t * in_addr)334 u64 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
dev_translate_dma_address(const struct udevice * dev,const fdt32_t * in_addr)339 u64 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
dev_get_dma_range(const struct udevice * dev,phys_addr_t * cpu,dma_addr_t * bus,u64 * size)344 int 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
dev_read_alias_highest_id(const char * stem)350 int 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
dev_read_addr_pci(const struct udevice * dev)358 fdt_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
dev_get_child_count(const struct udevice * dev)369 int dev_get_child_count(const struct udevice *dev)
370 {
371 return ofnode_get_child_count(dev_ofnode(dev));
372 }
373
dev_read_pci_bus_range(const struct udevice * dev,struct resource * res)374 int 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
dev_decode_display_timing(const struct udevice * dev,int index,struct display_timing * config)390 int 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