blob: 6bda077a34b9ce46e5b2456bd14762f950d60f62 [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
Simon Glasscfdae5c2017-05-18 20:09:04 -060047const char *dev_read_string(struct udevice *dev, const char *propname)
48{
49 return ofnode_read_string(dev_ofnode(dev), propname);
50}
51
52bool dev_read_bool(struct udevice *dev, const char *propname)
53{
54 return ofnode_read_bool(dev_ofnode(dev), propname);
55}
56
57ofnode dev_read_subnode(struct udevice *dev, const char *subnode_name)
58{
59 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
60}
61
62ofnode dev_read_first_subnode(struct udevice *dev)
63{
64 return ofnode_first_subnode(dev_ofnode(dev));
65}
66
67ofnode dev_read_next_subnode(ofnode node)
68{
69 return ofnode_next_subnode(node);
70}
71
72int dev_read_size(struct udevice *dev, const char *propname)
73{
74 return ofnode_read_size(dev_ofnode(dev), propname);
75}
76
77fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
78{
79 if (ofnode_is_np(dev_ofnode(dev)))
80 return ofnode_get_addr_index(dev_ofnode(dev), index);
81 else
82 return devfdt_get_addr_index(dev, index);
83}
84
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +020085void *dev_remap_addr_index(struct udevice *dev, int index)
86{
87 fdt_addr_t addr = dev_read_addr_index(dev, index);
88
89 if (addr == FDT_ADDR_T_NONE)
90 return NULL;
91
92 return map_physmem(addr, 0, MAP_NOCACHE);
93}
94
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +010095fdt_addr_t dev_read_addr_name(struct udevice *dev, const char *name)
96{
97 int index = dev_read_stringlist_search(dev, "reg-names", name);
98
99 if (index < 0)
100 return FDT_ADDR_T_NONE;
101 else
102 return dev_read_addr_index(dev, index);
103}
104
105void *dev_remap_addr_name(struct udevice *dev, const char *name)
106{
107 fdt_addr_t addr = dev_read_addr_name(dev, name);
108
109 if (addr == FDT_ADDR_T_NONE)
110 return NULL;
111
112 return map_physmem(addr, 0, MAP_NOCACHE);
113}
114
Simon Glasscfdae5c2017-05-18 20:09:04 -0600115fdt_addr_t dev_read_addr(struct udevice *dev)
116{
117 return dev_read_addr_index(dev, 0);
118}
119
Philipp Tomsich7719b392017-09-11 22:04:12 +0200120void *dev_read_addr_ptr(struct udevice *dev)
121{
122 fdt_addr_t addr = dev_read_addr(dev);
123
Simon Glassc47a3882017-09-28 06:35:15 -0600124 return (addr == FDT_ADDR_T_NONE) ? NULL : map_sysmem(addr, 0);
Philipp Tomsich7719b392017-09-11 22:04:12 +0200125}
126
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200127void *dev_remap_addr(struct udevice *dev)
128{
129 return dev_remap_addr_index(dev, 0);
130}
131
Simon Glasscfdae5c2017-05-18 20:09:04 -0600132fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100133 fdt_size_t *sizep)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600134{
135 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
136}
137
138const char *dev_read_name(struct udevice *dev)
139{
140 return ofnode_get_name(dev_ofnode(dev));
141}
142
143int dev_read_stringlist_search(struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100144 const char *string)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600145{
146 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
147}
148
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200149int dev_read_string_index(struct udevice *dev, const char *propname, int index,
150 const char **outp)
151{
152 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
153}
154
155int dev_read_string_count(struct udevice *dev, const char *propname)
156{
157 return ofnode_read_string_count(dev_ofnode(dev), propname);
158}
159
Simon Glasscfdae5c2017-05-18 20:09:04 -0600160int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
Mario Six6eac2222018-01-15 11:07:18 +0100161 const char *cells_name, int cell_count,
162 int index, struct ofnode_phandle_args *out_args)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600163{
164 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
165 cells_name, cell_count, index,
166 out_args);
167}
168
Patrice Chotard99503c12017-11-29 09:06:10 +0100169int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
170 const char *cells_name)
171{
172 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
173 cells_name);
174}
175
Simon Glasscfdae5c2017-05-18 20:09:04 -0600176int dev_read_addr_cells(struct udevice *dev)
177{
178 return ofnode_read_addr_cells(dev_ofnode(dev));
179}
180
181int dev_read_size_cells(struct udevice *dev)
182{
183 return ofnode_read_size_cells(dev_ofnode(dev));
184}
185
Simon Glass4191dc12017-06-12 06:21:31 -0600186int dev_read_simple_addr_cells(struct udevice *dev)
187{
188 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
189}
190
191int dev_read_simple_size_cells(struct udevice *dev)
192{
193 return ofnode_read_simple_size_cells(dev_ofnode(dev));
194}
195
Simon Glasscfdae5c2017-05-18 20:09:04 -0600196int dev_read_phandle(struct udevice *dev)
197{
198 ofnode node = dev_ofnode(dev);
199
200 if (ofnode_is_np(node))
201 return ofnode_to_np(node)->phandle;
202 else
203 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
204}
205
Masahiro Yamadabaebec12017-07-17 12:18:39 +0900206const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600207{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900208 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600209}
210
211int dev_read_alias_seq(struct udevice *dev, int *devnump)
212{
213 ofnode node = dev_ofnode(dev);
214 const char *uc_name = dev->uclass->uc_drv->name;
215 int ret;
216
217 if (ofnode_is_np(node)) {
218 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
219 if (ret >= 0)
220 *devnump = ret;
221 } else {
222 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
223 ofnode_to_offset(node), devnump);
224 }
225
226 return ret;
227}
228
229int dev_read_u32_array(struct udevice *dev, const char *propname,
230 u32 *out_values, size_t sz)
231{
232 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
233}
234
235const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
236 size_t sz)
237{
238 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
239}
Simon Glassfa031f82017-06-12 06:21:30 -0600240
241int dev_read_enabled(struct udevice *dev)
242{
243 ofnode node = dev_ofnode(dev);
244
245 if (ofnode_is_np(node))
246 return of_device_is_available(ofnode_to_np(node));
247 else
248 return fdtdec_get_is_enabled(gd->fdt_blob,
249 ofnode_to_offset(node));
250}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600251
252int dev_read_resource(struct udevice *dev, uint index, struct resource *res)
253{
254 return ofnode_read_resource(dev_ofnode(dev), index, res);
255}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900256
257int dev_read_resource_byname(struct udevice *dev, const char *name,
258 struct resource *res)
259{
260 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
261}
Mario Sixaefac062018-01-15 11:07:19 +0100262
263u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
264{
265 return ofnode_translate_address(dev_ofnode(dev), in_addr);
266}
Michal Simekd0e30a02019-01-31 16:30:59 +0100267
268int dev_read_alias_highest_id(const char *stem)
269{
270 if (of_live_active())
271 return of_alias_get_highest_id(stem);
272
273 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
274}