1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
9 #include <dm/of_access.h>
11 #include <asm/global_data.h>
12 #include <asm/types.h>
14 #include <linux/ioport.h>
16 int dev_read_u8(const struct udevice *dev, const char *propname, u8 *outp)
18 return ofnode_read_u8(dev_ofnode(dev), propname, outp);
21 u8 dev_read_u8_default(const struct udevice *dev, const char *propname, u8 def)
23 return ofnode_read_u8_default(dev_ofnode(dev), propname, def);
26 int dev_read_u16(const struct udevice *dev, const char *propname, u16 *outp)
28 return ofnode_read_u16(dev_ofnode(dev), propname, outp);
31 u16 dev_read_u16_default(const struct udevice *dev, const char *propname,
34 return ofnode_read_u16_default(dev_ofnode(dev), propname, def);
37 int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp)
39 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
42 int dev_read_u32_default(const struct udevice *dev, const char *propname,
45 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
48 int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
51 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
54 u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
57 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
61 int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
63 return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
66 int dev_read_s32_default(const struct udevice *dev, const char *propname,
69 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
72 int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp)
77 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
85 int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp)
87 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
90 u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
93 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
96 const char *dev_read_string(const struct udevice *dev, const char *propname)
98 return ofnode_read_string(dev_ofnode(dev), propname);
101 bool dev_read_bool(const struct udevice *dev, const char *propname)
103 return ofnode_read_bool(dev_ofnode(dev), propname);
106 ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name)
108 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
111 ofnode dev_read_first_subnode(const struct udevice *dev)
113 return ofnode_first_subnode(dev_ofnode(dev));
116 ofnode dev_read_next_subnode(ofnode node)
118 return ofnode_next_subnode(node);
121 int dev_read_size(const struct udevice *dev, const char *propname)
123 return ofnode_read_size(dev_ofnode(dev), propname);
126 fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index)
128 if (ofnode_is_np(dev_ofnode(dev)))
129 return ofnode_get_addr_index(dev_ofnode(dev), index);
131 return devfdt_get_addr_index(dev, index);
134 void *dev_read_addr_index_ptr(const struct udevice *dev, int index)
136 fdt_addr_t addr = dev_read_addr_index(dev, index);
138 if (addr == FDT_ADDR_T_NONE)
141 return map_sysmem(addr, 0);
144 fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
147 if (ofnode_is_np(dev_ofnode(dev)))
148 return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
150 return devfdt_get_addr_size_index(dev, index, size);
153 void *dev_read_addr_size_index_ptr(const struct udevice *dev, int index,
156 fdt_addr_t addr = dev_read_addr_size_index(dev, index, size);
158 if (addr == FDT_ADDR_T_NONE)
161 return map_sysmem(addr, 0);
164 void *dev_remap_addr_index(const struct udevice *dev, int index)
166 fdt_addr_t addr = dev_read_addr_index(dev, index);
168 if (addr == FDT_ADDR_T_NONE)
171 return map_physmem(addr, 0, MAP_NOCACHE);
174 fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name)
176 int index = dev_read_stringlist_search(dev, "reg-names", name);
179 return FDT_ADDR_T_NONE;
181 return dev_read_addr_index(dev, index);
184 fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
187 int index = dev_read_stringlist_search(dev, "reg-names", name);
190 return FDT_ADDR_T_NONE;
192 return dev_read_addr_size_index(dev, index, size);
195 void *dev_remap_addr_name(const struct udevice *dev, const char *name)
197 fdt_addr_t addr = dev_read_addr_name(dev, name);
199 if (addr == FDT_ADDR_T_NONE)
202 return map_physmem(addr, 0, MAP_NOCACHE);
205 fdt_addr_t dev_read_addr(const struct udevice *dev)
207 return dev_read_addr_index(dev, 0);
210 void *dev_read_addr_ptr(const struct udevice *dev)
212 fdt_addr_t addr = dev_read_addr(dev);
214 if (addr == FDT_ADDR_T_NONE)
217 return map_sysmem(addr, 0);
220 void *dev_remap_addr(const struct udevice *dev)
222 return dev_remap_addr_index(dev, 0);
225 fdt_addr_t dev_read_addr_size(const struct udevice *dev, fdt_size_t *sizep)
227 return dev_read_addr_size_index(dev, 0, sizep);
230 const char *dev_read_name(const struct udevice *dev)
232 return ofnode_get_name(dev_ofnode(dev));
235 int dev_read_stringlist_search(const struct udevice *dev, const char *property,
238 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
241 int dev_read_string_index(const struct udevice *dev, const char *propname,
242 int index, const char **outp)
244 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
247 int dev_read_string_count(const struct udevice *dev, const char *propname)
249 return ofnode_read_string_count(dev_ofnode(dev), propname);
252 int dev_read_string_list(const struct udevice *dev, const char *propname,
255 return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
258 int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
259 const char *cells_name, int cell_count,
260 int index, struct ofnode_phandle_args *out_args)
262 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
263 cells_name, cell_count, index,
267 int dev_count_phandle_with_args(const struct udevice *dev,
268 const char *list_name, const char *cells_name,
271 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
272 cells_name, cell_count);
275 int dev_read_addr_cells(const struct udevice *dev)
277 return ofnode_read_addr_cells(dev_ofnode(dev));
280 int dev_read_size_cells(const struct udevice *dev)
282 return ofnode_read_size_cells(dev_ofnode(dev));
285 int dev_read_simple_addr_cells(const struct udevice *dev)
287 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
290 int dev_read_simple_size_cells(const struct udevice *dev)
292 return ofnode_read_simple_size_cells(dev_ofnode(dev));
295 int dev_read_phandle(const struct udevice *dev)
297 ofnode node = dev_ofnode(dev);
299 if (ofnode_is_np(node))
300 return ofnode_to_np(node)->phandle;
302 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
305 const void *dev_read_prop(const struct udevice *dev, const char *propname,
308 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
311 int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
313 return ofnode_first_property(dev_ofnode(dev), prop);
316 int dev_read_next_prop(struct ofprop *prop)
318 return ofnode_next_property(prop);
321 const void *dev_read_prop_by_prop(struct ofprop *prop,
322 const char **propname, int *lenp)
324 return ofprop_get_property(prop, propname, lenp);
327 int dev_read_alias_seq(const struct udevice *dev, int *devnump)
329 ofnode node = dev_ofnode(dev);
330 const char *uc_name = dev->uclass->uc_drv->name;
333 if (ofnode_is_np(node)) {
334 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
340 #if CONFIG_IS_ENABLED(OF_CONTROL)
341 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
342 ofnode_to_offset(node), devnump);
349 int dev_read_u32_array(const struct udevice *dev, const char *propname,
350 u32 *out_values, size_t sz)
352 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
355 const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
356 const char *propname, size_t sz)
358 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
361 int dev_read_enabled(const struct udevice *dev)
363 ofnode node = dev_ofnode(dev);
365 if (ofnode_is_np(node))
366 return of_device_is_available(ofnode_to_np(node));
368 return fdtdec_get_is_enabled(gd->fdt_blob,
369 ofnode_to_offset(node));
372 int dev_read_resource(const struct udevice *dev, uint index,
373 struct resource *res)
375 return ofnode_read_resource(dev_ofnode(dev), index, res);
378 int dev_read_resource_byname(const struct udevice *dev, const char *name,
379 struct resource *res)
381 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
384 u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
386 return ofnode_translate_address(dev_ofnode(dev), in_addr);
389 u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
391 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
394 int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
395 dma_addr_t *bus, u64 *size)
397 return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
400 int dev_read_alias_highest_id(const char *stem)
402 if (of_live_active())
403 return of_alias_get_highest_id(stem);
405 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
408 fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
412 addr = dev_read_addr(dev);
413 if (addr == FDT_ADDR_T_NONE && !of_live_active())
414 addr = devfdt_get_addr_pci(dev);
419 int dev_get_child_count(const struct udevice *dev)
421 return ofnode_get_child_count(dev_ofnode(dev));
424 int dev_read_pci_bus_range(const struct udevice *dev,
425 struct resource *res)
430 values = dev_read_prop(dev, "bus-range", &len);
431 if (!values || len < sizeof(*values) * 2)
434 res->start = *values++;
440 int dev_decode_display_timing(const struct udevice *dev, int index,
441 struct display_timing *config)
443 return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
446 int dev_decode_panel_timing(const struct udevice *dev,
447 struct display_timing *config)
449 return ofnode_decode_panel_timing(dev_ofnode(dev), config);
452 ofnode dev_get_phy_node(const struct udevice *dev)
454 return ofnode_get_phy_node(dev_ofnode(dev));
457 phy_interface_t dev_read_phy_mode(const struct udevice *dev)
459 return ofnode_read_phy_mode(dev_ofnode(dev));