1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
12 #include <dm/of_access.h>
14 int dev_read_u32(struct udevice *dev, const char *propname, u32 *outp)
16 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
19 int dev_read_u32_default(struct udevice *dev, const char *propname, int def)
21 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
24 int dev_read_s32(struct udevice *dev, const char *propname, s32 *outp)
26 return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
29 int dev_read_s32_default(struct udevice *dev, const char *propname, int def)
31 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
34 int dev_read_u32u(struct udevice *dev, const char *propname, uint *outp)
39 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
47 int dev_read_u64(struct udevice *dev, const char *propname, u64 *outp)
49 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
52 u64 dev_read_u64_default(struct udevice *dev, const char *propname, u64 def)
54 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
57 const char *dev_read_string(struct udevice *dev, const char *propname)
59 return ofnode_read_string(dev_ofnode(dev), propname);
62 bool dev_read_bool(struct udevice *dev, const char *propname)
64 return ofnode_read_bool(dev_ofnode(dev), propname);
67 ofnode dev_read_subnode(struct udevice *dev, const char *subnode_name)
69 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
72 ofnode dev_read_first_subnode(struct udevice *dev)
74 return ofnode_first_subnode(dev_ofnode(dev));
77 ofnode dev_read_next_subnode(ofnode node)
79 return ofnode_next_subnode(node);
82 int dev_read_size(struct udevice *dev, const char *propname)
84 return ofnode_read_size(dev_ofnode(dev), propname);
87 fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
89 if (ofnode_is_np(dev_ofnode(dev)))
90 return ofnode_get_addr_index(dev_ofnode(dev), index);
92 return devfdt_get_addr_index(dev, index);
95 fdt_addr_t dev_read_addr_size_index(struct udevice *dev, int index,
98 if (ofnode_is_np(dev_ofnode(dev)))
99 return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
101 return devfdt_get_addr_size_index(dev, index, size);
104 void *dev_remap_addr_index(struct udevice *dev, int index)
106 fdt_addr_t addr = dev_read_addr_index(dev, index);
108 if (addr == FDT_ADDR_T_NONE)
111 return map_physmem(addr, 0, MAP_NOCACHE);
114 fdt_addr_t dev_read_addr_name(struct udevice *dev, const char *name)
116 int index = dev_read_stringlist_search(dev, "reg-names", name);
119 return FDT_ADDR_T_NONE;
121 return dev_read_addr_index(dev, index);
124 fdt_addr_t dev_read_addr_size_name(struct udevice *dev, const char *name,
127 int index = dev_read_stringlist_search(dev, "reg-names", name);
130 return FDT_ADDR_T_NONE;
132 return dev_read_addr_size_index(dev, index, size);
135 void *dev_remap_addr_name(struct udevice *dev, const char *name)
137 fdt_addr_t addr = dev_read_addr_name(dev, name);
139 if (addr == FDT_ADDR_T_NONE)
142 return map_physmem(addr, 0, MAP_NOCACHE);
145 fdt_addr_t dev_read_addr(struct udevice *dev)
147 return dev_read_addr_index(dev, 0);
150 void *dev_read_addr_ptr(struct udevice *dev)
152 fdt_addr_t addr = dev_read_addr(dev);
154 return (addr == FDT_ADDR_T_NONE) ? NULL : map_sysmem(addr, 0);
157 void *dev_remap_addr(struct udevice *dev)
159 return dev_remap_addr_index(dev, 0);
162 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *property,
165 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
168 const char *dev_read_name(struct udevice *dev)
170 return ofnode_get_name(dev_ofnode(dev));
173 int dev_read_stringlist_search(struct udevice *dev, const char *property,
176 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
179 int dev_read_string_index(struct udevice *dev, const char *propname, int index,
182 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
185 int dev_read_string_count(struct udevice *dev, const char *propname)
187 return ofnode_read_string_count(dev_ofnode(dev), propname);
190 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
191 const char *cells_name, int cell_count,
192 int index, struct ofnode_phandle_args *out_args)
194 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
195 cells_name, cell_count, index,
199 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
200 const char *cells_name)
202 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
206 int dev_read_addr_cells(struct udevice *dev)
208 return ofnode_read_addr_cells(dev_ofnode(dev));
211 int dev_read_size_cells(struct udevice *dev)
213 return ofnode_read_size_cells(dev_ofnode(dev));
216 int dev_read_simple_addr_cells(struct udevice *dev)
218 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
221 int dev_read_simple_size_cells(struct udevice *dev)
223 return ofnode_read_simple_size_cells(dev_ofnode(dev));
226 int dev_read_phandle(struct udevice *dev)
228 ofnode node = dev_ofnode(dev);
230 if (ofnode_is_np(node))
231 return ofnode_to_np(node)->phandle;
233 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
236 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp)
238 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
241 int dev_read_alias_seq(struct udevice *dev, int *devnump)
243 ofnode node = dev_ofnode(dev);
244 const char *uc_name = dev->uclass->uc_drv->name;
247 if (ofnode_is_np(node)) {
248 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
252 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
253 ofnode_to_offset(node), devnump);
259 int dev_read_u32_array(struct udevice *dev, const char *propname,
260 u32 *out_values, size_t sz)
262 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
265 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
268 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
271 int dev_read_enabled(struct udevice *dev)
273 ofnode node = dev_ofnode(dev);
275 if (ofnode_is_np(node))
276 return of_device_is_available(ofnode_to_np(node));
278 return fdtdec_get_is_enabled(gd->fdt_blob,
279 ofnode_to_offset(node));
282 int dev_read_resource(struct udevice *dev, uint index, struct resource *res)
284 return ofnode_read_resource(dev_ofnode(dev), index, res);
287 int dev_read_resource_byname(struct udevice *dev, const char *name,
288 struct resource *res)
290 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
293 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
295 return ofnode_translate_address(dev_ofnode(dev), in_addr);
298 u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr)
300 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
303 int dev_read_alias_highest_id(const char *stem)
305 if (of_live_active())
306 return of_alias_get_highest_id(stem);
308 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
311 fdt_addr_t dev_read_addr_pci(struct udevice *dev)
315 addr = dev_read_addr(dev);
316 if (addr == FDT_ADDR_T_NONE && !of_live_active())
317 addr = devfdt_get_addr_pci(dev);