blob: 8bb456bc3f01254ac7de66bc6824e291b02854cd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasscfdae5c2017-05-18 20:09:04 -06002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glasscfdae5c2017-05-18 20:09:04 -06005 */
6
7#include <common.h>
8#include <dm.h>
9#include <dm/of_access.h>
Stefan Roesea8c43062020-04-29 09:08:44 +020010#include <mapmem.h>
11#include <asm/types.h>
12#include <asm/io.h>
Simon Glasscfdae5c2017-05-18 20:09:04 -060013
Simon Glass2f9a6122020-01-27 08:49:40 -070014int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp)
Masahiro Yamada71d115f2017-12-30 02:00:05 +090015{
16 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
17}
18
Simon Glass2f9a6122020-01-27 08:49:40 -070019int dev_read_u32_default(const struct udevice *dev, const char *propname,
20 int def)
Simon Glasscfdae5c2017-05-18 20:09:04 -060021{
22 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
23}
24
Dario Binacchi81d80b52020-03-29 18:04:41 +020025int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
26 u32 *outp)
27{
28 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
29}
30
31u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
32 int index, u32 def)
33{
34 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
35 def);
36}
37
Simon Glass2f9a6122020-01-27 08:49:40 -070038int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
Simon Glass6df01f92018-12-10 10:37:37 -070039{
40 return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
41}
42
Simon Glass2f9a6122020-01-27 08:49:40 -070043int dev_read_s32_default(const struct udevice *dev, const char *propname,
44 int def)
Simon Glass6df01f92018-12-10 10:37:37 -070045{
46 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
47}
48
Simon Glass2f9a6122020-01-27 08:49:40 -070049int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp)
Simon Glass6df01f92018-12-10 10:37:37 -070050{
51 u32 val;
52 int ret;
53
54 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
55 if (ret)
56 return ret;
57 *outp = val;
58
59 return 0;
60}
61
Simon Glass2f9a6122020-01-27 08:49:40 -070062int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp)
T Karthik Reddy478860d2019-09-02 16:34:30 +020063{
64 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
65}
66
Simon Glass2f9a6122020-01-27 08:49:40 -070067u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
68 u64 def)
T Karthik Reddy478860d2019-09-02 16:34:30 +020069{
70 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
71}
72
Simon Glass2f9a6122020-01-27 08:49:40 -070073const char *dev_read_string(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -060074{
75 return ofnode_read_string(dev_ofnode(dev), propname);
76}
77
Simon Glass2f9a6122020-01-27 08:49:40 -070078bool dev_read_bool(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -060079{
80 return ofnode_read_bool(dev_ofnode(dev), propname);
81}
82
Simon Glass2f9a6122020-01-27 08:49:40 -070083ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name)
Simon Glasscfdae5c2017-05-18 20:09:04 -060084{
85 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
86}
87
Simon Glass2f9a6122020-01-27 08:49:40 -070088ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -060089{
90 return ofnode_first_subnode(dev_ofnode(dev));
91}
92
93ofnode dev_read_next_subnode(ofnode node)
94{
95 return ofnode_next_subnode(node);
96}
97
Simon Glass2f9a6122020-01-27 08:49:40 -070098int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -060099{
100 return ofnode_read_size(dev_ofnode(dev), propname);
101}
102
Simon Glass2f9a6122020-01-27 08:49:40 -0700103fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600104{
105 if (ofnode_is_np(dev_ofnode(dev)))
106 return ofnode_get_addr_index(dev_ofnode(dev), index);
107 else
108 return devfdt_get_addr_index(dev, index);
109}
110
Simon Glass2f9a6122020-01-27 08:49:40 -0700111fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530112 fdt_size_t *size)
113{
114 if (ofnode_is_np(dev_ofnode(dev)))
115 return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
116 else
117 return devfdt_get_addr_size_index(dev, index, size);
118}
119
Simon Glass2f9a6122020-01-27 08:49:40 -0700120void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200121{
122 fdt_addr_t addr = dev_read_addr_index(dev, index);
123
124 if (addr == FDT_ADDR_T_NONE)
125 return NULL;
126
127 return map_physmem(addr, 0, MAP_NOCACHE);
128}
129
Simon Glass2f9a6122020-01-27 08:49:40 -0700130fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100131{
132 int index = dev_read_stringlist_search(dev, "reg-names", name);
133
134 if (index < 0)
135 return FDT_ADDR_T_NONE;
136 else
137 return dev_read_addr_index(dev, index);
138}
139
Simon Glass2f9a6122020-01-27 08:49:40 -0700140fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530141 fdt_size_t *size)
142{
143 int index = dev_read_stringlist_search(dev, "reg-names", name);
144
145 if (index < 0)
146 return FDT_ADDR_T_NONE;
147 else
148 return dev_read_addr_size_index(dev, index, size);
149}
150
Simon Glass2f9a6122020-01-27 08:49:40 -0700151void *dev_remap_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100152{
153 fdt_addr_t addr = dev_read_addr_name(dev, name);
154
155 if (addr == FDT_ADDR_T_NONE)
156 return NULL;
157
158 return map_physmem(addr, 0, MAP_NOCACHE);
159}
160
Simon Glass2f9a6122020-01-27 08:49:40 -0700161fdt_addr_t dev_read_addr(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600162{
163 return dev_read_addr_index(dev, 0);
164}
165
Simon Glass2f9a6122020-01-27 08:49:40 -0700166void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsich7719b392017-09-11 22:04:12 +0200167{
168 fdt_addr_t addr = dev_read_addr(dev);
169
Sean Anderson42db70b2020-06-24 06:41:13 -0400170 return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr;
Philipp Tomsich7719b392017-09-11 22:04:12 +0200171}
172
Simon Glass2f9a6122020-01-27 08:49:40 -0700173void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200174{
175 return dev_remap_addr_index(dev, 0);
176}
177
Simon Glass2f9a6122020-01-27 08:49:40 -0700178fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100179 fdt_size_t *sizep)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600180{
181 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
182}
183
Simon Glass2f9a6122020-01-27 08:49:40 -0700184const char *dev_read_name(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600185{
186 return ofnode_get_name(dev_ofnode(dev));
187}
188
Simon Glass2f9a6122020-01-27 08:49:40 -0700189int dev_read_stringlist_search(const struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100190 const char *string)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600191{
192 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
193}
194
Simon Glass2f9a6122020-01-27 08:49:40 -0700195int dev_read_string_index(const struct udevice *dev, const char *propname,
196 int index, const char **outp)
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200197{
198 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
199}
200
Simon Glass2f9a6122020-01-27 08:49:40 -0700201int dev_read_string_count(const struct udevice *dev, const char *propname)
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200202{
203 return ofnode_read_string_count(dev_ofnode(dev), propname);
204}
205
Simon Glass2f9a6122020-01-27 08:49:40 -0700206int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
Mario Six6eac2222018-01-15 11:07:18 +0100207 const char *cells_name, int cell_count,
208 int index, struct ofnode_phandle_args *out_args)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600209{
210 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
211 cells_name, cell_count, index,
212 out_args);
213}
214
Simon Glass2f9a6122020-01-27 08:49:40 -0700215int dev_count_phandle_with_args(const struct udevice *dev,
216 const char *list_name, const char *cells_name)
Patrice Chotard99503c12017-11-29 09:06:10 +0100217{
218 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
219 cells_name);
220}
221
Simon Glass2f9a6122020-01-27 08:49:40 -0700222int dev_read_addr_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600223{
224 return ofnode_read_addr_cells(dev_ofnode(dev));
225}
226
Simon Glass2f9a6122020-01-27 08:49:40 -0700227int dev_read_size_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600228{
229 return ofnode_read_size_cells(dev_ofnode(dev));
230}
231
Simon Glass2f9a6122020-01-27 08:49:40 -0700232int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600233{
234 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
235}
236
Simon Glass2f9a6122020-01-27 08:49:40 -0700237int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600238{
239 return ofnode_read_simple_size_cells(dev_ofnode(dev));
240}
241
Simon Glass2f9a6122020-01-27 08:49:40 -0700242int dev_read_phandle(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600243{
244 ofnode node = dev_ofnode(dev);
245
246 if (ofnode_is_np(node))
247 return ofnode_to_np(node)->phandle;
248 else
249 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
250}
251
Simon Glass2f9a6122020-01-27 08:49:40 -0700252const void *dev_read_prop(const struct udevice *dev, const char *propname,
253 int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600254{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900255 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600256}
257
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100258int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
259{
260 return ofnode_get_first_property(dev_ofnode(dev), prop);
261}
262
263int dev_read_next_prop(struct ofprop *prop)
264{
265 return ofnode_get_next_property(prop);
266}
267
268const void *dev_read_prop_by_prop(struct ofprop *prop,
269 const char **propname, int *lenp)
270{
271 return ofnode_get_property_by_prop(prop, propname, lenp);
272}
273
Simon Glass2f9a6122020-01-27 08:49:40 -0700274int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600275{
276 ofnode node = dev_ofnode(dev);
277 const char *uc_name = dev->uclass->uc_drv->name;
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200278 int ret = -ENOTSUPP;
Simon Glasscfdae5c2017-05-18 20:09:04 -0600279
280 if (ofnode_is_np(node)) {
281 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
282 if (ret >= 0)
283 *devnump = ret;
284 } else {
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200285#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600286 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
287 ofnode_to_offset(node), devnump);
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200288#endif
Simon Glasscfdae5c2017-05-18 20:09:04 -0600289 }
290
291 return ret;
292}
293
Simon Glass2f9a6122020-01-27 08:49:40 -0700294int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600295 u32 *out_values, size_t sz)
296{
297 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
298}
299
Simon Glass2f9a6122020-01-27 08:49:40 -0700300const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
301 const char *propname, size_t sz)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600302{
303 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
304}
Simon Glassfa031f82017-06-12 06:21:30 -0600305
Simon Glass2f9a6122020-01-27 08:49:40 -0700306int dev_read_enabled(const struct udevice *dev)
Simon Glassfa031f82017-06-12 06:21:30 -0600307{
308 ofnode node = dev_ofnode(dev);
309
310 if (ofnode_is_np(node))
311 return of_device_is_available(ofnode_to_np(node));
312 else
313 return fdtdec_get_is_enabled(gd->fdt_blob,
314 ofnode_to_offset(node));
315}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600316
Simon Glass2f9a6122020-01-27 08:49:40 -0700317int dev_read_resource(const struct udevice *dev, uint index,
318 struct resource *res)
Simon Glassf7bfcc42017-07-25 08:29:55 -0600319{
320 return ofnode_read_resource(dev_ofnode(dev), index, res);
321}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900322
Simon Glass2f9a6122020-01-27 08:49:40 -0700323int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900324 struct resource *res)
325{
326 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
327}
Mario Sixaefac062018-01-15 11:07:19 +0100328
Simon Glass2f9a6122020-01-27 08:49:40 -0700329u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
Mario Sixaefac062018-01-15 11:07:19 +0100330{
331 return ofnode_translate_address(dev_ofnode(dev), in_addr);
332}
Michal Simekd0e30a02019-01-31 16:30:59 +0100333
Simon Glass2f9a6122020-01-27 08:49:40 -0700334u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
Fabien Dessenne22236e02019-05-31 15:11:30 +0200335{
336 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
337}
338
Michal Simekd0e30a02019-01-31 16:30:59 +0100339int dev_read_alias_highest_id(const char *stem)
340{
341 if (of_live_active())
342 return of_alias_get_highest_id(stem);
343
344 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
345}
Simon Glass23b27592019-09-15 12:08:58 -0600346
Simon Glass2f9a6122020-01-27 08:49:40 -0700347fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600348{
349 ulong addr;
350
351 addr = dev_read_addr(dev);
352 if (addr == FDT_ADDR_T_NONE && !of_live_active())
353 addr = devfdt_get_addr_pci(dev);
354
355 return addr;
356}
developerd93c8b42020-05-02 11:35:09 +0200357
358int dev_get_child_count(const struct udevice *dev)
359{
360 return ofnode_get_child_count(dev_ofnode(dev));
361}