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 const char *dev_read_string(struct udevice *dev, const char *propname)
26 return ofnode_read_string(dev_ofnode(dev), propname);
29 bool dev_read_bool(struct udevice *dev, const char *propname)
31 return ofnode_read_bool(dev_ofnode(dev), propname);
34 ofnode dev_read_subnode(struct udevice *dev, const char *subnode_name)
36 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
39 ofnode dev_read_first_subnode(struct udevice *dev)
41 return ofnode_first_subnode(dev_ofnode(dev));
44 ofnode dev_read_next_subnode(ofnode node)
46 return ofnode_next_subnode(node);
49 int dev_read_size(struct udevice *dev, const char *propname)
51 return ofnode_read_size(dev_ofnode(dev), propname);
54 fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
56 if (ofnode_is_np(dev_ofnode(dev)))
57 return ofnode_get_addr_index(dev_ofnode(dev), index);
59 return devfdt_get_addr_index(dev, index);
62 void *dev_remap_addr_index(struct udevice *dev, int index)
64 fdt_addr_t addr = dev_read_addr_index(dev, index);
66 if (addr == FDT_ADDR_T_NONE)
69 return map_physmem(addr, 0, MAP_NOCACHE);
72 fdt_addr_t dev_read_addr_name(struct udevice *dev, const char *name)
74 int index = dev_read_stringlist_search(dev, "reg-names", name);
77 return FDT_ADDR_T_NONE;
79 return dev_read_addr_index(dev, index);
82 void *dev_remap_addr_name(struct udevice *dev, const char *name)
84 fdt_addr_t addr = dev_read_addr_name(dev, name);
86 if (addr == FDT_ADDR_T_NONE)
89 return map_physmem(addr, 0, MAP_NOCACHE);
92 fdt_addr_t dev_read_addr(struct udevice *dev)
94 return dev_read_addr_index(dev, 0);
97 void *dev_read_addr_ptr(struct udevice *dev)
99 fdt_addr_t addr = dev_read_addr(dev);
101 return (addr == FDT_ADDR_T_NONE) ? NULL : map_sysmem(addr, 0);
104 void *dev_remap_addr(struct udevice *dev)
106 return dev_remap_addr_index(dev, 0);
109 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *property,
112 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
115 const char *dev_read_name(struct udevice *dev)
117 return ofnode_get_name(dev_ofnode(dev));
120 int dev_read_stringlist_search(struct udevice *dev, const char *property,
123 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
126 int dev_read_string_index(struct udevice *dev, const char *propname, int index,
129 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
132 int dev_read_string_count(struct udevice *dev, const char *propname)
134 return ofnode_read_string_count(dev_ofnode(dev), propname);
137 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
138 const char *cells_name, int cell_count,
139 int index, struct ofnode_phandle_args *out_args)
141 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
142 cells_name, cell_count, index,
146 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
147 const char *cells_name)
149 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
153 int dev_read_addr_cells(struct udevice *dev)
155 return ofnode_read_addr_cells(dev_ofnode(dev));
158 int dev_read_size_cells(struct udevice *dev)
160 return ofnode_read_size_cells(dev_ofnode(dev));
163 int dev_read_simple_addr_cells(struct udevice *dev)
165 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
168 int dev_read_simple_size_cells(struct udevice *dev)
170 return ofnode_read_simple_size_cells(dev_ofnode(dev));
173 int dev_read_phandle(struct udevice *dev)
175 ofnode node = dev_ofnode(dev);
177 if (ofnode_is_np(node))
178 return ofnode_to_np(node)->phandle;
180 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
183 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp)
185 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
188 int dev_read_alias_seq(struct udevice *dev, int *devnump)
190 ofnode node = dev_ofnode(dev);
191 const char *uc_name = dev->uclass->uc_drv->name;
194 if (ofnode_is_np(node)) {
195 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
199 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
200 ofnode_to_offset(node), devnump);
206 int dev_read_u32_array(struct udevice *dev, const char *propname,
207 u32 *out_values, size_t sz)
209 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
212 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
215 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
218 int dev_read_enabled(struct udevice *dev)
220 ofnode node = dev_ofnode(dev);
222 if (ofnode_is_np(node))
223 return of_device_is_available(ofnode_to_np(node));
225 return fdtdec_get_is_enabled(gd->fdt_blob,
226 ofnode_to_offset(node));
229 int dev_read_resource(struct udevice *dev, uint index, struct resource *res)
231 return ofnode_read_resource(dev_ofnode(dev), index, res);
234 int dev_read_resource_byname(struct udevice *dev, const char *name,
235 struct resource *res)
237 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
240 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
242 return ofnode_translate_address(dev_ofnode(dev), in_addr);