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 fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
137 if (ofnode_is_np(dev_ofnode(dev)))
138 return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
140 return devfdt_get_addr_size_index(dev, index, size);
143 void *dev_remap_addr_index(const struct udevice *dev, int index)
145 fdt_addr_t addr = dev_read_addr_index(dev, index);
147 if (addr == FDT_ADDR_T_NONE)
150 return map_physmem(addr, 0, MAP_NOCACHE);
153 fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name)
155 int index = dev_read_stringlist_search(dev, "reg-names", name);
158 return FDT_ADDR_T_NONE;
160 return dev_read_addr_index(dev, index);
163 fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
166 int index = dev_read_stringlist_search(dev, "reg-names", name);
169 return FDT_ADDR_T_NONE;
171 return dev_read_addr_size_index(dev, index, size);
174 void *dev_remap_addr_name(const struct udevice *dev, const char *name)
176 fdt_addr_t addr = dev_read_addr_name(dev, name);
178 if (addr == FDT_ADDR_T_NONE)
181 return map_physmem(addr, 0, MAP_NOCACHE);
184 fdt_addr_t dev_read_addr(const struct udevice *dev)
186 return dev_read_addr_index(dev, 0);
189 void *dev_read_addr_ptr(const struct udevice *dev)
191 fdt_addr_t addr = dev_read_addr(dev);
193 return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr;
196 void *dev_remap_addr(const struct udevice *dev)
198 return dev_remap_addr_index(dev, 0);
201 fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *property,
204 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
207 const char *dev_read_name(const struct udevice *dev)
209 return ofnode_get_name(dev_ofnode(dev));
212 int dev_read_stringlist_search(const struct udevice *dev, const char *property,
215 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
218 int dev_read_string_index(const struct udevice *dev, const char *propname,
219 int index, const char **outp)
221 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
224 int dev_read_string_count(const struct udevice *dev, const char *propname)
226 return ofnode_read_string_count(dev_ofnode(dev), propname);
229 int dev_read_string_list(const struct udevice *dev, const char *propname,
232 return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
235 int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
236 const char *cells_name, int cell_count,
237 int index, struct ofnode_phandle_args *out_args)
239 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
240 cells_name, cell_count, index,
244 int dev_count_phandle_with_args(const struct udevice *dev,
245 const char *list_name, const char *cells_name,
248 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
249 cells_name, cell_count);
252 int dev_read_addr_cells(const struct udevice *dev)
254 return ofnode_read_addr_cells(dev_ofnode(dev));
257 int dev_read_size_cells(const struct udevice *dev)
259 return ofnode_read_size_cells(dev_ofnode(dev));
262 int dev_read_simple_addr_cells(const struct udevice *dev)
264 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
267 int dev_read_simple_size_cells(const struct udevice *dev)
269 return ofnode_read_simple_size_cells(dev_ofnode(dev));
272 int dev_read_phandle(const struct udevice *dev)
274 ofnode node = dev_ofnode(dev);
276 if (ofnode_is_np(node))
277 return ofnode_to_np(node)->phandle;
279 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
282 const void *dev_read_prop(const struct udevice *dev, const char *propname,
285 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
288 int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
290 return ofnode_first_property(dev_ofnode(dev), prop);
293 int dev_read_next_prop(struct ofprop *prop)
295 return ofnode_next_property(prop);
298 const void *dev_read_prop_by_prop(struct ofprop *prop,
299 const char **propname, int *lenp)
301 return ofprop_get_property(prop, propname, lenp);
304 int dev_read_alias_seq(const struct udevice *dev, int *devnump)
306 ofnode node = dev_ofnode(dev);
307 const char *uc_name = dev->uclass->uc_drv->name;
310 if (ofnode_is_np(node)) {
311 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
317 #if CONFIG_IS_ENABLED(OF_CONTROL)
318 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
319 ofnode_to_offset(node), devnump);
326 int dev_read_u32_array(const struct udevice *dev, const char *propname,
327 u32 *out_values, size_t sz)
329 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
332 const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
333 const char *propname, size_t sz)
335 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
338 int dev_read_enabled(const struct udevice *dev)
340 ofnode node = dev_ofnode(dev);
342 if (ofnode_is_np(node))
343 return of_device_is_available(ofnode_to_np(node));
345 return fdtdec_get_is_enabled(gd->fdt_blob,
346 ofnode_to_offset(node));
349 int dev_read_resource(const struct udevice *dev, uint index,
350 struct resource *res)
352 return ofnode_read_resource(dev_ofnode(dev), index, res);
355 int dev_read_resource_byname(const struct udevice *dev, const char *name,
356 struct resource *res)
358 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
361 u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
363 return ofnode_translate_address(dev_ofnode(dev), in_addr);
366 u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
368 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
371 int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
372 dma_addr_t *bus, u64 *size)
374 return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
377 int dev_read_alias_highest_id(const char *stem)
379 if (of_live_active())
380 return of_alias_get_highest_id(stem);
382 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
385 fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
389 addr = dev_read_addr(dev);
390 if (addr == FDT_ADDR_T_NONE && !of_live_active())
391 addr = devfdt_get_addr_pci(dev);
396 int dev_get_child_count(const struct udevice *dev)
398 return ofnode_get_child_count(dev_ofnode(dev));
401 int dev_read_pci_bus_range(const struct udevice *dev,
402 struct resource *res)
407 values = dev_read_prop(dev, "bus-range", &len);
408 if (!values || len < sizeof(*values) * 2)
411 res->start = *values++;
417 int dev_decode_display_timing(const struct udevice *dev, int index,
418 struct display_timing *config)
420 return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
423 ofnode dev_get_phy_node(const struct udevice *dev)
425 return ofnode_get_phy_node(dev_ofnode(dev));
428 phy_interface_t dev_read_phy_mode(const struct udevice *dev)
430 return ofnode_read_phy_mode(dev_ofnode(dev));