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_u32(const struct udevice *dev, const char *propname, u32 *outp)
18 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
21 int dev_read_u32_default(const struct udevice *dev, const char *propname,
24 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
27 int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
30 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
33 u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
36 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
40 int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
42 return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
45 int dev_read_s32_default(const struct udevice *dev, const char *propname,
48 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
51 int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp)
56 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
64 int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp)
66 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
69 u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
72 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
75 const char *dev_read_string(const struct udevice *dev, const char *propname)
77 return ofnode_read_string(dev_ofnode(dev), propname);
80 bool dev_read_bool(const struct udevice *dev, const char *propname)
82 return ofnode_read_bool(dev_ofnode(dev), propname);
85 ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name)
87 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
90 ofnode dev_read_first_subnode(const struct udevice *dev)
92 return ofnode_first_subnode(dev_ofnode(dev));
95 ofnode dev_read_next_subnode(ofnode node)
97 return ofnode_next_subnode(node);
100 int dev_read_size(const struct udevice *dev, const char *propname)
102 return ofnode_read_size(dev_ofnode(dev), propname);
105 fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index)
107 if (ofnode_is_np(dev_ofnode(dev)))
108 return ofnode_get_addr_index(dev_ofnode(dev), index);
110 return devfdt_get_addr_index(dev, index);
113 fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
116 if (ofnode_is_np(dev_ofnode(dev)))
117 return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
119 return devfdt_get_addr_size_index(dev, index, size);
122 void *dev_remap_addr_index(const struct udevice *dev, int index)
124 fdt_addr_t addr = dev_read_addr_index(dev, index);
126 if (addr == FDT_ADDR_T_NONE)
129 return map_physmem(addr, 0, MAP_NOCACHE);
132 fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name)
134 int index = dev_read_stringlist_search(dev, "reg-names", name);
137 return FDT_ADDR_T_NONE;
139 return dev_read_addr_index(dev, index);
142 fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
145 int index = dev_read_stringlist_search(dev, "reg-names", name);
148 return FDT_ADDR_T_NONE;
150 return dev_read_addr_size_index(dev, index, size);
153 void *dev_remap_addr_name(const struct udevice *dev, const char *name)
155 fdt_addr_t addr = dev_read_addr_name(dev, name);
157 if (addr == FDT_ADDR_T_NONE)
160 return map_physmem(addr, 0, MAP_NOCACHE);
163 fdt_addr_t dev_read_addr(const struct udevice *dev)
165 return dev_read_addr_index(dev, 0);
168 void *dev_read_addr_ptr(const struct udevice *dev)
170 fdt_addr_t addr = dev_read_addr(dev);
172 return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr;
175 void *dev_remap_addr(const struct udevice *dev)
177 return dev_remap_addr_index(dev, 0);
180 fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *property,
183 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
186 const char *dev_read_name(const struct udevice *dev)
188 return ofnode_get_name(dev_ofnode(dev));
191 int dev_read_stringlist_search(const struct udevice *dev, const char *property,
194 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
197 int dev_read_string_index(const struct udevice *dev, const char *propname,
198 int index, const char **outp)
200 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
203 int dev_read_string_count(const struct udevice *dev, const char *propname)
205 return ofnode_read_string_count(dev_ofnode(dev), propname);
208 int dev_read_string_list(const struct udevice *dev, const char *propname,
211 return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
214 int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
215 const char *cells_name, int cell_count,
216 int index, struct ofnode_phandle_args *out_args)
218 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
219 cells_name, cell_count, index,
223 int dev_count_phandle_with_args(const struct udevice *dev,
224 const char *list_name, const char *cells_name,
227 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
228 cells_name, cell_count);
231 int dev_read_addr_cells(const struct udevice *dev)
233 return ofnode_read_addr_cells(dev_ofnode(dev));
236 int dev_read_size_cells(const struct udevice *dev)
238 return ofnode_read_size_cells(dev_ofnode(dev));
241 int dev_read_simple_addr_cells(const struct udevice *dev)
243 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
246 int dev_read_simple_size_cells(const struct udevice *dev)
248 return ofnode_read_simple_size_cells(dev_ofnode(dev));
251 int dev_read_phandle(const struct udevice *dev)
253 ofnode node = dev_ofnode(dev);
255 if (ofnode_is_np(node))
256 return ofnode_to_np(node)->phandle;
258 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
261 const void *dev_read_prop(const struct udevice *dev, const char *propname,
264 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
267 int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
269 return ofnode_get_first_property(dev_ofnode(dev), prop);
272 int dev_read_next_prop(struct ofprop *prop)
274 return ofnode_get_next_property(prop);
277 const void *dev_read_prop_by_prop(struct ofprop *prop,
278 const char **propname, int *lenp)
280 return ofnode_get_property_by_prop(prop, propname, lenp);
283 int dev_read_alias_seq(const struct udevice *dev, int *devnump)
285 ofnode node = dev_ofnode(dev);
286 const char *uc_name = dev->uclass->uc_drv->name;
289 if (ofnode_is_np(node)) {
290 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
296 #if CONFIG_IS_ENABLED(OF_CONTROL)
297 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
298 ofnode_to_offset(node), devnump);
305 int dev_read_u32_array(const struct udevice *dev, const char *propname,
306 u32 *out_values, size_t sz)
308 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
311 const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
312 const char *propname, size_t sz)
314 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
317 int dev_read_enabled(const struct udevice *dev)
319 ofnode node = dev_ofnode(dev);
321 if (ofnode_is_np(node))
322 return of_device_is_available(ofnode_to_np(node));
324 return fdtdec_get_is_enabled(gd->fdt_blob,
325 ofnode_to_offset(node));
328 int dev_read_resource(const struct udevice *dev, uint index,
329 struct resource *res)
331 return ofnode_read_resource(dev_ofnode(dev), index, res);
334 int dev_read_resource_byname(const struct udevice *dev, const char *name,
335 struct resource *res)
337 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
340 u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
342 return ofnode_translate_address(dev_ofnode(dev), in_addr);
345 u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
347 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
350 int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
351 dma_addr_t *bus, u64 *size)
353 return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
356 int dev_read_alias_highest_id(const char *stem)
358 if (of_live_active())
359 return of_alias_get_highest_id(stem);
361 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
364 fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
368 addr = dev_read_addr(dev);
369 if (addr == FDT_ADDR_T_NONE && !of_live_active())
370 addr = devfdt_get_addr_pci(dev);
375 int dev_get_child_count(const struct udevice *dev)
377 return ofnode_get_child_count(dev_ofnode(dev));
380 int dev_read_pci_bus_range(const struct udevice *dev,
381 struct resource *res)
386 values = dev_read_prop(dev, "bus-range", &len);
387 if (!values || len < sizeof(*values) * 2)
390 res->start = *values++;
396 int dev_decode_display_timing(const struct udevice *dev, int index,
397 struct display_timing *config)
399 return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
402 ofnode dev_get_phy_node(const struct udevice *dev)
404 return ofnode_get_phy_node(dev_ofnode(dev));
407 phy_interface_t dev_read_phy_mode(const struct udevice *dev)
409 return ofnode_read_phy_mode(dev_ofnode(dev));