blob: cdd78be03e22280f1e0e877befd1556249e1e02b [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
24const char *dev_read_string(struct udevice *dev, const char *propname)
25{
26 return ofnode_read_string(dev_ofnode(dev), propname);
27}
28
29bool dev_read_bool(struct udevice *dev, const char *propname)
30{
31 return ofnode_read_bool(dev_ofnode(dev), propname);
32}
33
34ofnode dev_read_subnode(struct udevice *dev, const char *subnode_name)
35{
36 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
37}
38
39ofnode dev_read_first_subnode(struct udevice *dev)
40{
41 return ofnode_first_subnode(dev_ofnode(dev));
42}
43
44ofnode dev_read_next_subnode(ofnode node)
45{
46 return ofnode_next_subnode(node);
47}
48
49int dev_read_size(struct udevice *dev, const char *propname)
50{
51 return ofnode_read_size(dev_ofnode(dev), propname);
52}
53
54fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
55{
56 if (ofnode_is_np(dev_ofnode(dev)))
57 return ofnode_get_addr_index(dev_ofnode(dev), index);
58 else
59 return devfdt_get_addr_index(dev, index);
60}
61
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +020062void *dev_remap_addr_index(struct udevice *dev, int index)
63{
64 fdt_addr_t addr = dev_read_addr_index(dev, index);
65
66 if (addr == FDT_ADDR_T_NONE)
67 return NULL;
68
69 return map_physmem(addr, 0, MAP_NOCACHE);
70}
71
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +010072fdt_addr_t dev_read_addr_name(struct udevice *dev, const char *name)
73{
74 int index = dev_read_stringlist_search(dev, "reg-names", name);
75
76 if (index < 0)
77 return FDT_ADDR_T_NONE;
78 else
79 return dev_read_addr_index(dev, index);
80}
81
82void *dev_remap_addr_name(struct udevice *dev, const char *name)
83{
84 fdt_addr_t addr = dev_read_addr_name(dev, name);
85
86 if (addr == FDT_ADDR_T_NONE)
87 return NULL;
88
89 return map_physmem(addr, 0, MAP_NOCACHE);
90}
91
Simon Glasscfdae5c2017-05-18 20:09:04 -060092fdt_addr_t dev_read_addr(struct udevice *dev)
93{
94 return dev_read_addr_index(dev, 0);
95}
96
Philipp Tomsich7719b392017-09-11 22:04:12 +020097void *dev_read_addr_ptr(struct udevice *dev)
98{
99 fdt_addr_t addr = dev_read_addr(dev);
100
Simon Glassc47a3882017-09-28 06:35:15 -0600101 return (addr == FDT_ADDR_T_NONE) ? NULL : map_sysmem(addr, 0);
Philipp Tomsich7719b392017-09-11 22:04:12 +0200102}
103
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200104void *dev_remap_addr(struct udevice *dev)
105{
106 return dev_remap_addr_index(dev, 0);
107}
108
Simon Glasscfdae5c2017-05-18 20:09:04 -0600109fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100110 fdt_size_t *sizep)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600111{
112 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
113}
114
115const char *dev_read_name(struct udevice *dev)
116{
117 return ofnode_get_name(dev_ofnode(dev));
118}
119
120int dev_read_stringlist_search(struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100121 const char *string)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600122{
123 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
124}
125
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200126int dev_read_string_index(struct udevice *dev, const char *propname, int index,
127 const char **outp)
128{
129 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
130}
131
132int dev_read_string_count(struct udevice *dev, const char *propname)
133{
134 return ofnode_read_string_count(dev_ofnode(dev), propname);
135}
136
Simon Glasscfdae5c2017-05-18 20:09:04 -0600137int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
Mario Six6eac2222018-01-15 11:07:18 +0100138 const char *cells_name, int cell_count,
139 int index, struct ofnode_phandle_args *out_args)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600140{
141 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
142 cells_name, cell_count, index,
143 out_args);
144}
145
Patrice Chotard99503c12017-11-29 09:06:10 +0100146int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
147 const char *cells_name)
148{
149 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
150 cells_name);
151}
152
Simon Glasscfdae5c2017-05-18 20:09:04 -0600153int dev_read_addr_cells(struct udevice *dev)
154{
155 return ofnode_read_addr_cells(dev_ofnode(dev));
156}
157
158int dev_read_size_cells(struct udevice *dev)
159{
160 return ofnode_read_size_cells(dev_ofnode(dev));
161}
162
Simon Glass4191dc12017-06-12 06:21:31 -0600163int dev_read_simple_addr_cells(struct udevice *dev)
164{
165 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
166}
167
168int dev_read_simple_size_cells(struct udevice *dev)
169{
170 return ofnode_read_simple_size_cells(dev_ofnode(dev));
171}
172
Simon Glasscfdae5c2017-05-18 20:09:04 -0600173int dev_read_phandle(struct udevice *dev)
174{
175 ofnode node = dev_ofnode(dev);
176
177 if (ofnode_is_np(node))
178 return ofnode_to_np(node)->phandle;
179 else
180 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
181}
182
Masahiro Yamadabaebec12017-07-17 12:18:39 +0900183const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600184{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900185 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600186}
187
188int dev_read_alias_seq(struct udevice *dev, int *devnump)
189{
190 ofnode node = dev_ofnode(dev);
191 const char *uc_name = dev->uclass->uc_drv->name;
192 int ret;
193
194 if (ofnode_is_np(node)) {
195 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
196 if (ret >= 0)
197 *devnump = ret;
198 } else {
199 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
200 ofnode_to_offset(node), devnump);
201 }
202
203 return ret;
204}
205
206int dev_read_u32_array(struct udevice *dev, const char *propname,
207 u32 *out_values, size_t sz)
208{
209 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
210}
211
212const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
213 size_t sz)
214{
215 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
216}
Simon Glassfa031f82017-06-12 06:21:30 -0600217
218int dev_read_enabled(struct udevice *dev)
219{
220 ofnode node = dev_ofnode(dev);
221
222 if (ofnode_is_np(node))
223 return of_device_is_available(ofnode_to_np(node));
224 else
225 return fdtdec_get_is_enabled(gd->fdt_blob,
226 ofnode_to_offset(node));
227}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600228
229int dev_read_resource(struct udevice *dev, uint index, struct resource *res)
230{
231 return ofnode_read_resource(dev_ofnode(dev), index, res);
232}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900233
234int dev_read_resource_byname(struct udevice *dev, const char *name,
235 struct resource *res)
236{
237 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
238}
Mario Sixaefac062018-01-15 11:07:19 +0100239
240u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
241{
242 return ofnode_translate_address(dev_ofnode(dev), in_addr);
243}