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 const char *dev_read_string(struct udevice *dev, const char *propname)
49 return ofnode_read_string(dev_ofnode(dev), propname);
52 bool dev_read_bool(struct udevice *dev, const char *propname)
54 return ofnode_read_bool(dev_ofnode(dev), propname);
57 ofnode dev_read_subnode(struct udevice *dev, const char *subnode_name)
59 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
62 ofnode dev_read_first_subnode(struct udevice *dev)
64 return ofnode_first_subnode(dev_ofnode(dev));
67 ofnode dev_read_next_subnode(ofnode node)
69 return ofnode_next_subnode(node);
72 int dev_read_size(struct udevice *dev, const char *propname)
74 return ofnode_read_size(dev_ofnode(dev), propname);
77 fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
79 if (ofnode_is_np(dev_ofnode(dev)))
80 return ofnode_get_addr_index(dev_ofnode(dev), index);
82 return devfdt_get_addr_index(dev, index);
85 void *dev_remap_addr_index(struct udevice *dev, int index)
87 fdt_addr_t addr = dev_read_addr_index(dev, index);
89 if (addr == FDT_ADDR_T_NONE)
92 return map_physmem(addr, 0, MAP_NOCACHE);
95 fdt_addr_t dev_read_addr_name(struct udevice *dev, const char *name)
97 int index = dev_read_stringlist_search(dev, "reg-names", name);
100 return FDT_ADDR_T_NONE;
102 return dev_read_addr_index(dev, index);
105 void *dev_remap_addr_name(struct udevice *dev, const char *name)
107 fdt_addr_t addr = dev_read_addr_name(dev, name);
109 if (addr == FDT_ADDR_T_NONE)
112 return map_physmem(addr, 0, MAP_NOCACHE);
115 fdt_addr_t dev_read_addr(struct udevice *dev)
117 return dev_read_addr_index(dev, 0);
120 void *dev_read_addr_ptr(struct udevice *dev)
122 fdt_addr_t addr = dev_read_addr(dev);
124 return (addr == FDT_ADDR_T_NONE) ? NULL : map_sysmem(addr, 0);
127 void *dev_remap_addr(struct udevice *dev)
129 return dev_remap_addr_index(dev, 0);
132 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *property,
135 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
138 const char *dev_read_name(struct udevice *dev)
140 return ofnode_get_name(dev_ofnode(dev));
143 int dev_read_stringlist_search(struct udevice *dev, const char *property,
146 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
149 int dev_read_string_index(struct udevice *dev, const char *propname, int index,
152 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
155 int dev_read_string_count(struct udevice *dev, const char *propname)
157 return ofnode_read_string_count(dev_ofnode(dev), propname);
160 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
161 const char *cells_name, int cell_count,
162 int index, struct ofnode_phandle_args *out_args)
164 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
165 cells_name, cell_count, index,
169 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
170 const char *cells_name)
172 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
176 int dev_read_addr_cells(struct udevice *dev)
178 return ofnode_read_addr_cells(dev_ofnode(dev));
181 int dev_read_size_cells(struct udevice *dev)
183 return ofnode_read_size_cells(dev_ofnode(dev));
186 int dev_read_simple_addr_cells(struct udevice *dev)
188 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
191 int dev_read_simple_size_cells(struct udevice *dev)
193 return ofnode_read_simple_size_cells(dev_ofnode(dev));
196 int dev_read_phandle(struct udevice *dev)
198 ofnode node = dev_ofnode(dev);
200 if (ofnode_is_np(node))
201 return ofnode_to_np(node)->phandle;
203 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
206 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp)
208 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
211 int dev_read_alias_seq(struct udevice *dev, int *devnump)
213 ofnode node = dev_ofnode(dev);
214 const char *uc_name = dev->uclass->uc_drv->name;
217 if (ofnode_is_np(node)) {
218 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
222 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
223 ofnode_to_offset(node), devnump);
229 int dev_read_u32_array(struct udevice *dev, const char *propname,
230 u32 *out_values, size_t sz)
232 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
235 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
238 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
241 int dev_read_enabled(struct udevice *dev)
243 ofnode node = dev_ofnode(dev);
245 if (ofnode_is_np(node))
246 return of_device_is_available(ofnode_to_np(node));
248 return fdtdec_get_is_enabled(gd->fdt_blob,
249 ofnode_to_offset(node));
252 int dev_read_resource(struct udevice *dev, uint index, struct resource *res)
254 return ofnode_read_resource(dev_ofnode(dev), index, res);
257 int dev_read_resource_byname(struct udevice *dev, const char *name,
258 struct resource *res)
260 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
263 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
265 return ofnode_translate_address(dev_ofnode(dev), in_addr);
268 int dev_read_alias_highest_id(const char *stem)
270 if (of_live_active())
271 return of_alias_get_highest_id(stem);
273 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);