blob: 9602e52d1b1d3dcd8afbe8d5ef31c9fffa044936 [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
Masahiro Yamada71d115f2017-12-30 02:00:05 +090014int dev_read_u32(struct udevice *dev, const char *propname, u32 *outp)
15{
16 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
17}
18
Simon Glasscfdae5c2017-05-18 20:09:04 -060019int dev_read_u32_default(struct udevice *dev, const char *propname, int def)
20{
21 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
22}
23
Simon Glass6df01f92018-12-10 10:37:37 -070024int dev_read_s32(struct udevice *dev, const char *propname, s32 *outp)
25{
26 return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
27}
28
29int dev_read_s32_default(struct udevice *dev, const char *propname, int def)
30{
31 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
32}
33
34int dev_read_u32u(struct udevice *dev, const char *propname, uint *outp)
35{
36 u32 val;
37 int ret;
38
39 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
40 if (ret)
41 return ret;
42 *outp = val;
43
44 return 0;
45}
46
T Karthik Reddy478860d2019-09-02 16:34:30 +020047int dev_read_u64(struct udevice *dev, const char *propname, u64 *outp)
48{
49 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
50}
51
52u64 dev_read_u64_default(struct udevice *dev, const char *propname, u64 def)
53{
54 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
55}
56
Simon Glasscfdae5c2017-05-18 20:09:04 -060057const char *dev_read_string(struct udevice *dev, const char *propname)
58{
59 return ofnode_read_string(dev_ofnode(dev), propname);
60}
61
62bool dev_read_bool(struct udevice *dev, const char *propname)
63{
64 return ofnode_read_bool(dev_ofnode(dev), propname);
65}
66
67ofnode dev_read_subnode(struct udevice *dev, const char *subnode_name)
68{
69 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
70}
71
72ofnode dev_read_first_subnode(struct udevice *dev)
73{
74 return ofnode_first_subnode(dev_ofnode(dev));
75}
76
77ofnode dev_read_next_subnode(ofnode node)
78{
79 return ofnode_next_subnode(node);
80}
81
82int dev_read_size(struct udevice *dev, const char *propname)
83{
84 return ofnode_read_size(dev_ofnode(dev), propname);
85}
86
87fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
88{
89 if (ofnode_is_np(dev_ofnode(dev)))
90 return ofnode_get_addr_index(dev_ofnode(dev), index);
91 else
92 return devfdt_get_addr_index(dev, index);
93}
94
Sekhar Norif677c6f2019-08-01 19:12:56 +053095fdt_addr_t dev_read_addr_size_index(struct udevice *dev, int index,
96 fdt_size_t *size)
97{
98 if (ofnode_is_np(dev_ofnode(dev)))
99 return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
100 else
101 return devfdt_get_addr_size_index(dev, index, size);
102}
103
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200104void *dev_remap_addr_index(struct udevice *dev, int index)
105{
106 fdt_addr_t addr = dev_read_addr_index(dev, index);
107
108 if (addr == FDT_ADDR_T_NONE)
109 return NULL;
110
111 return map_physmem(addr, 0, MAP_NOCACHE);
112}
113
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100114fdt_addr_t dev_read_addr_name(struct udevice *dev, const char *name)
115{
116 int index = dev_read_stringlist_search(dev, "reg-names", name);
117
118 if (index < 0)
119 return FDT_ADDR_T_NONE;
120 else
121 return dev_read_addr_index(dev, index);
122}
123
Sekhar Norif677c6f2019-08-01 19:12:56 +0530124fdt_addr_t dev_read_addr_size_name(struct udevice *dev, const char *name,
125 fdt_size_t *size)
126{
127 int index = dev_read_stringlist_search(dev, "reg-names", name);
128
129 if (index < 0)
130 return FDT_ADDR_T_NONE;
131 else
132 return dev_read_addr_size_index(dev, index, size);
133}
134
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100135void *dev_remap_addr_name(struct udevice *dev, const char *name)
136{
137 fdt_addr_t addr = dev_read_addr_name(dev, name);
138
139 if (addr == FDT_ADDR_T_NONE)
140 return NULL;
141
142 return map_physmem(addr, 0, MAP_NOCACHE);
143}
144
Simon Glasscfdae5c2017-05-18 20:09:04 -0600145fdt_addr_t dev_read_addr(struct udevice *dev)
146{
147 return dev_read_addr_index(dev, 0);
148}
149
Philipp Tomsich7719b392017-09-11 22:04:12 +0200150void *dev_read_addr_ptr(struct udevice *dev)
151{
152 fdt_addr_t addr = dev_read_addr(dev);
153
Simon Glassc47a3882017-09-28 06:35:15 -0600154 return (addr == FDT_ADDR_T_NONE) ? NULL : map_sysmem(addr, 0);
Philipp Tomsich7719b392017-09-11 22:04:12 +0200155}
156
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200157void *dev_remap_addr(struct udevice *dev)
158{
159 return dev_remap_addr_index(dev, 0);
160}
161
Simon Glasscfdae5c2017-05-18 20:09:04 -0600162fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100163 fdt_size_t *sizep)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600164{
165 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
166}
167
168const char *dev_read_name(struct udevice *dev)
169{
170 return ofnode_get_name(dev_ofnode(dev));
171}
172
173int dev_read_stringlist_search(struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100174 const char *string)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600175{
176 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
177}
178
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200179int dev_read_string_index(struct udevice *dev, const char *propname, int index,
180 const char **outp)
181{
182 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
183}
184
185int dev_read_string_count(struct udevice *dev, const char *propname)
186{
187 return ofnode_read_string_count(dev_ofnode(dev), propname);
188}
189
Simon Glasscfdae5c2017-05-18 20:09:04 -0600190int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
Mario Six6eac2222018-01-15 11:07:18 +0100191 const char *cells_name, int cell_count,
192 int index, struct ofnode_phandle_args *out_args)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600193{
194 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
195 cells_name, cell_count, index,
196 out_args);
197}
198
Patrice Chotard99503c12017-11-29 09:06:10 +0100199int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
200 const char *cells_name)
201{
202 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
203 cells_name);
204}
205
Simon Glasscfdae5c2017-05-18 20:09:04 -0600206int dev_read_addr_cells(struct udevice *dev)
207{
208 return ofnode_read_addr_cells(dev_ofnode(dev));
209}
210
211int dev_read_size_cells(struct udevice *dev)
212{
213 return ofnode_read_size_cells(dev_ofnode(dev));
214}
215
Simon Glass4191dc12017-06-12 06:21:31 -0600216int dev_read_simple_addr_cells(struct udevice *dev)
217{
218 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
219}
220
221int dev_read_simple_size_cells(struct udevice *dev)
222{
223 return ofnode_read_simple_size_cells(dev_ofnode(dev));
224}
225
Simon Glasscfdae5c2017-05-18 20:09:04 -0600226int dev_read_phandle(struct udevice *dev)
227{
228 ofnode node = dev_ofnode(dev);
229
230 if (ofnode_is_np(node))
231 return ofnode_to_np(node)->phandle;
232 else
233 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
234}
235
Masahiro Yamadabaebec12017-07-17 12:18:39 +0900236const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600237{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900238 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600239}
240
241int dev_read_alias_seq(struct udevice *dev, int *devnump)
242{
243 ofnode node = dev_ofnode(dev);
244 const char *uc_name = dev->uclass->uc_drv->name;
245 int ret;
246
247 if (ofnode_is_np(node)) {
248 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
249 if (ret >= 0)
250 *devnump = ret;
251 } else {
252 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
253 ofnode_to_offset(node), devnump);
254 }
255
256 return ret;
257}
258
259int dev_read_u32_array(struct udevice *dev, const char *propname,
260 u32 *out_values, size_t sz)
261{
262 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
263}
264
265const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
266 size_t sz)
267{
268 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
269}
Simon Glassfa031f82017-06-12 06:21:30 -0600270
271int dev_read_enabled(struct udevice *dev)
272{
273 ofnode node = dev_ofnode(dev);
274
275 if (ofnode_is_np(node))
276 return of_device_is_available(ofnode_to_np(node));
277 else
278 return fdtdec_get_is_enabled(gd->fdt_blob,
279 ofnode_to_offset(node));
280}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600281
282int dev_read_resource(struct udevice *dev, uint index, struct resource *res)
283{
284 return ofnode_read_resource(dev_ofnode(dev), index, res);
285}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900286
287int dev_read_resource_byname(struct udevice *dev, const char *name,
288 struct resource *res)
289{
290 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
291}
Mario Sixaefac062018-01-15 11:07:19 +0100292
293u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
294{
295 return ofnode_translate_address(dev_ofnode(dev), in_addr);
296}
Michal Simekd0e30a02019-01-31 16:30:59 +0100297
Fabien Dessenne22236e02019-05-31 15:11:30 +0200298u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr)
299{
300 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
301}
302
Michal Simekd0e30a02019-01-31 16:30:59 +0100303int dev_read_alias_highest_id(const char *stem)
304{
305 if (of_live_active())
306 return of_alias_get_highest_id(stem);
307
308 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
309}
Simon Glass23b27592019-09-15 12:08:58 -0600310
311fdt_addr_t dev_read_addr_pci(struct udevice *dev)
312{
313 ulong addr;
314
315 addr = dev_read_addr(dev);
316 if (addr == FDT_ADDR_T_NONE && !of_live_active())
317 addr = devfdt_get_addr_pci(dev);
318
319 return addr;
320}