blob: 1f999b1b316c01fabb643c5ccfa631a50549a130 [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
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +02007#include <asm/types.h>
8#include <asm/io.h>
Simon Glasscfdae5c2017-05-18 20:09:04 -06009#include <common.h>
10#include <dm.h>
Simon Glassc47a3882017-09-28 06:35:15 -060011#include <mapmem.h>
Simon Glasscfdae5c2017-05-18 20:09:04 -060012#include <dm/of_access.h>
13
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
Simon Glass2f9a6122020-01-27 08:49:40 -070025int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
Simon Glass6df01f92018-12-10 10:37:37 -070026{
27 return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
28}
29
Simon Glass2f9a6122020-01-27 08:49:40 -070030int dev_read_s32_default(const struct udevice *dev, const char *propname,
31 int def)
Simon Glass6df01f92018-12-10 10:37:37 -070032{
33 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
34}
35
Simon Glass2f9a6122020-01-27 08:49:40 -070036int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp)
Simon Glass6df01f92018-12-10 10:37:37 -070037{
38 u32 val;
39 int ret;
40
41 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
42 if (ret)
43 return ret;
44 *outp = val;
45
46 return 0;
47}
48
Simon Glass2f9a6122020-01-27 08:49:40 -070049int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp)
T Karthik Reddy478860d2019-09-02 16:34:30 +020050{
51 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
52}
53
Simon Glass2f9a6122020-01-27 08:49:40 -070054u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
55 u64 def)
T Karthik Reddy478860d2019-09-02 16:34:30 +020056{
57 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
58}
59
Simon Glass2f9a6122020-01-27 08:49:40 -070060const char *dev_read_string(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -060061{
62 return ofnode_read_string(dev_ofnode(dev), propname);
63}
64
Simon Glass2f9a6122020-01-27 08:49:40 -070065bool dev_read_bool(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -060066{
67 return ofnode_read_bool(dev_ofnode(dev), propname);
68}
69
Simon Glass2f9a6122020-01-27 08:49:40 -070070ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name)
Simon Glasscfdae5c2017-05-18 20:09:04 -060071{
72 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
73}
74
Simon Glass2f9a6122020-01-27 08:49:40 -070075ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -060076{
77 return ofnode_first_subnode(dev_ofnode(dev));
78}
79
80ofnode dev_read_next_subnode(ofnode node)
81{
82 return ofnode_next_subnode(node);
83}
84
Simon Glass2f9a6122020-01-27 08:49:40 -070085int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -060086{
87 return ofnode_read_size(dev_ofnode(dev), propname);
88}
89
Simon Glass2f9a6122020-01-27 08:49:40 -070090fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index)
Simon Glasscfdae5c2017-05-18 20:09:04 -060091{
92 if (ofnode_is_np(dev_ofnode(dev)))
93 return ofnode_get_addr_index(dev_ofnode(dev), index);
94 else
95 return devfdt_get_addr_index(dev, index);
96}
97
Simon Glass2f9a6122020-01-27 08:49:40 -070098fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
Sekhar Norif677c6f2019-08-01 19:12:56 +053099 fdt_size_t *size)
100{
101 if (ofnode_is_np(dev_ofnode(dev)))
102 return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
103 else
104 return devfdt_get_addr_size_index(dev, index, size);
105}
106
Simon Glass2f9a6122020-01-27 08:49:40 -0700107void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200108{
109 fdt_addr_t addr = dev_read_addr_index(dev, index);
110
111 if (addr == FDT_ADDR_T_NONE)
112 return NULL;
113
114 return map_physmem(addr, 0, MAP_NOCACHE);
115}
116
Simon Glass2f9a6122020-01-27 08:49:40 -0700117fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100118{
119 int index = dev_read_stringlist_search(dev, "reg-names", name);
120
121 if (index < 0)
122 return FDT_ADDR_T_NONE;
123 else
124 return dev_read_addr_index(dev, index);
125}
126
Simon Glass2f9a6122020-01-27 08:49:40 -0700127fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530128 fdt_size_t *size)
129{
130 int index = dev_read_stringlist_search(dev, "reg-names", name);
131
132 if (index < 0)
133 return FDT_ADDR_T_NONE;
134 else
135 return dev_read_addr_size_index(dev, index, size);
136}
137
Simon Glass2f9a6122020-01-27 08:49:40 -0700138void *dev_remap_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100139{
140 fdt_addr_t addr = dev_read_addr_name(dev, name);
141
142 if (addr == FDT_ADDR_T_NONE)
143 return NULL;
144
145 return map_physmem(addr, 0, MAP_NOCACHE);
146}
147
Simon Glass2f9a6122020-01-27 08:49:40 -0700148fdt_addr_t dev_read_addr(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600149{
150 return dev_read_addr_index(dev, 0);
151}
152
Simon Glass2f9a6122020-01-27 08:49:40 -0700153void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsich7719b392017-09-11 22:04:12 +0200154{
155 fdt_addr_t addr = dev_read_addr(dev);
156
Simon Glassc47a3882017-09-28 06:35:15 -0600157 return (addr == FDT_ADDR_T_NONE) ? NULL : map_sysmem(addr, 0);
Philipp Tomsich7719b392017-09-11 22:04:12 +0200158}
159
Simon Glass2f9a6122020-01-27 08:49:40 -0700160void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200161{
162 return dev_remap_addr_index(dev, 0);
163}
164
Simon Glass2f9a6122020-01-27 08:49:40 -0700165fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100166 fdt_size_t *sizep)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600167{
168 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
169}
170
Simon Glass2f9a6122020-01-27 08:49:40 -0700171const char *dev_read_name(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600172{
173 return ofnode_get_name(dev_ofnode(dev));
174}
175
Simon Glass2f9a6122020-01-27 08:49:40 -0700176int dev_read_stringlist_search(const struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100177 const char *string)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600178{
179 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
180}
181
Simon Glass2f9a6122020-01-27 08:49:40 -0700182int dev_read_string_index(const struct udevice *dev, const char *propname,
183 int index, const char **outp)
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200184{
185 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
186}
187
Simon Glass2f9a6122020-01-27 08:49:40 -0700188int dev_read_string_count(const struct udevice *dev, const char *propname)
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200189{
190 return ofnode_read_string_count(dev_ofnode(dev), propname);
191}
192
Simon Glass2f9a6122020-01-27 08:49:40 -0700193int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
Mario Six6eac2222018-01-15 11:07:18 +0100194 const char *cells_name, int cell_count,
195 int index, struct ofnode_phandle_args *out_args)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600196{
197 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
198 cells_name, cell_count, index,
199 out_args);
200}
201
Simon Glass2f9a6122020-01-27 08:49:40 -0700202int dev_count_phandle_with_args(const struct udevice *dev,
203 const char *list_name, const char *cells_name)
Patrice Chotard99503c12017-11-29 09:06:10 +0100204{
205 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
206 cells_name);
207}
208
Simon Glass2f9a6122020-01-27 08:49:40 -0700209int dev_read_addr_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600210{
211 return ofnode_read_addr_cells(dev_ofnode(dev));
212}
213
Simon Glass2f9a6122020-01-27 08:49:40 -0700214int dev_read_size_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600215{
216 return ofnode_read_size_cells(dev_ofnode(dev));
217}
218
Simon Glass2f9a6122020-01-27 08:49:40 -0700219int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600220{
221 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
222}
223
Simon Glass2f9a6122020-01-27 08:49:40 -0700224int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600225{
226 return ofnode_read_simple_size_cells(dev_ofnode(dev));
227}
228
Simon Glass2f9a6122020-01-27 08:49:40 -0700229int dev_read_phandle(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600230{
231 ofnode node = dev_ofnode(dev);
232
233 if (ofnode_is_np(node))
234 return ofnode_to_np(node)->phandle;
235 else
236 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
237}
238
Simon Glass2f9a6122020-01-27 08:49:40 -0700239const void *dev_read_prop(const struct udevice *dev, const char *propname,
240 int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600241{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900242 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600243}
244
Simon Glass2f9a6122020-01-27 08:49:40 -0700245int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600246{
247 ofnode node = dev_ofnode(dev);
248 const char *uc_name = dev->uclass->uc_drv->name;
249 int ret;
250
251 if (ofnode_is_np(node)) {
252 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
253 if (ret >= 0)
254 *devnump = ret;
255 } else {
256 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
257 ofnode_to_offset(node), devnump);
258 }
259
260 return ret;
261}
262
Simon Glass2f9a6122020-01-27 08:49:40 -0700263int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600264 u32 *out_values, size_t sz)
265{
266 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
267}
268
Simon Glass2f9a6122020-01-27 08:49:40 -0700269const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
270 const char *propname, size_t sz)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600271{
272 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
273}
Simon Glassfa031f82017-06-12 06:21:30 -0600274
Simon Glass2f9a6122020-01-27 08:49:40 -0700275int dev_read_enabled(const struct udevice *dev)
Simon Glassfa031f82017-06-12 06:21:30 -0600276{
277 ofnode node = dev_ofnode(dev);
278
279 if (ofnode_is_np(node))
280 return of_device_is_available(ofnode_to_np(node));
281 else
282 return fdtdec_get_is_enabled(gd->fdt_blob,
283 ofnode_to_offset(node));
284}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600285
Simon Glass2f9a6122020-01-27 08:49:40 -0700286int dev_read_resource(const struct udevice *dev, uint index,
287 struct resource *res)
Simon Glassf7bfcc42017-07-25 08:29:55 -0600288{
289 return ofnode_read_resource(dev_ofnode(dev), index, res);
290}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900291
Simon Glass2f9a6122020-01-27 08:49:40 -0700292int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900293 struct resource *res)
294{
295 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
296}
Mario Sixaefac062018-01-15 11:07:19 +0100297
Simon Glass2f9a6122020-01-27 08:49:40 -0700298u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
Mario Sixaefac062018-01-15 11:07:19 +0100299{
300 return ofnode_translate_address(dev_ofnode(dev), in_addr);
301}
Michal Simekd0e30a02019-01-31 16:30:59 +0100302
Simon Glass2f9a6122020-01-27 08:49:40 -0700303u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
Fabien Dessenne22236e02019-05-31 15:11:30 +0200304{
305 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
306}
307
Michal Simekd0e30a02019-01-31 16:30:59 +0100308int dev_read_alias_highest_id(const char *stem)
309{
310 if (of_live_active())
311 return of_alias_get_highest_id(stem);
312
313 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
314}
Simon Glass23b27592019-09-15 12:08:58 -0600315
Simon Glass2f9a6122020-01-27 08:49:40 -0700316fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600317{
318 ulong addr;
319
320 addr = dev_read_addr(dev);
321 if (addr == FDT_ADDR_T_NONE && !of_live_active())
322 addr = devfdt_get_addr_pci(dev);
323
324 return addr;
325}