blob: 076125824cab007ba858ccb9508bfb5ea48b77fa [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>
Stefan Roese0a9ecc52020-08-05 13:56:11 +020013#include <linux/ioport.h>
Simon Glasscfdae5c2017-05-18 20:09:04 -060014
Simon Glass2f9a6122020-01-27 08:49:40 -070015int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp)
Masahiro Yamada71d115f2017-12-30 02:00:05 +090016{
17 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
18}
19
Simon Glass2f9a6122020-01-27 08:49:40 -070020int dev_read_u32_default(const struct udevice *dev, const char *propname,
21 int def)
Simon Glasscfdae5c2017-05-18 20:09:04 -060022{
23 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
24}
25
Dario Binacchi81d80b52020-03-29 18:04:41 +020026int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
27 u32 *outp)
28{
29 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
30}
31
32u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
33 int index, u32 def)
34{
35 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
36 def);
37}
38
Simon Glass2f9a6122020-01-27 08:49:40 -070039int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
Simon Glass6df01f92018-12-10 10:37:37 -070040{
41 return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
42}
43
Simon Glass2f9a6122020-01-27 08:49:40 -070044int dev_read_s32_default(const struct udevice *dev, const char *propname,
45 int def)
Simon Glass6df01f92018-12-10 10:37:37 -070046{
47 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
48}
49
Simon Glass2f9a6122020-01-27 08:49:40 -070050int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp)
Simon Glass6df01f92018-12-10 10:37:37 -070051{
52 u32 val;
53 int ret;
54
55 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
56 if (ret)
57 return ret;
58 *outp = val;
59
60 return 0;
61}
62
Simon Glass2f9a6122020-01-27 08:49:40 -070063int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp)
T Karthik Reddy478860d2019-09-02 16:34:30 +020064{
65 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
66}
67
Simon Glass2f9a6122020-01-27 08:49:40 -070068u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
69 u64 def)
T Karthik Reddy478860d2019-09-02 16:34:30 +020070{
71 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
72}
73
Simon Glass2f9a6122020-01-27 08:49:40 -070074const char *dev_read_string(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -060075{
76 return ofnode_read_string(dev_ofnode(dev), propname);
77}
78
Simon Glass2f9a6122020-01-27 08:49:40 -070079bool dev_read_bool(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -060080{
81 return ofnode_read_bool(dev_ofnode(dev), propname);
82}
83
Simon Glass2f9a6122020-01-27 08:49:40 -070084ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name)
Simon Glasscfdae5c2017-05-18 20:09:04 -060085{
86 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
87}
88
Simon Glass2f9a6122020-01-27 08:49:40 -070089ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -060090{
91 return ofnode_first_subnode(dev_ofnode(dev));
92}
93
94ofnode dev_read_next_subnode(ofnode node)
95{
96 return ofnode_next_subnode(node);
97}
98
Simon Glass2f9a6122020-01-27 08:49:40 -070099int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600100{
101 return ofnode_read_size(dev_ofnode(dev), propname);
102}
103
Simon Glass2f9a6122020-01-27 08:49:40 -0700104fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600105{
106 if (ofnode_is_np(dev_ofnode(dev)))
107 return ofnode_get_addr_index(dev_ofnode(dev), index);
108 else
109 return devfdt_get_addr_index(dev, index);
110}
111
Simon Glass2f9a6122020-01-27 08:49:40 -0700112fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530113 fdt_size_t *size)
114{
115 if (ofnode_is_np(dev_ofnode(dev)))
116 return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
117 else
118 return devfdt_get_addr_size_index(dev, index, size);
119}
120
Simon Glass2f9a6122020-01-27 08:49:40 -0700121void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200122{
123 fdt_addr_t addr = dev_read_addr_index(dev, index);
124
125 if (addr == FDT_ADDR_T_NONE)
126 return NULL;
127
128 return map_physmem(addr, 0, MAP_NOCACHE);
129}
130
Simon Glass2f9a6122020-01-27 08:49:40 -0700131fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100132{
133 int index = dev_read_stringlist_search(dev, "reg-names", name);
134
135 if (index < 0)
136 return FDT_ADDR_T_NONE;
137 else
138 return dev_read_addr_index(dev, index);
139}
140
Simon Glass2f9a6122020-01-27 08:49:40 -0700141fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530142 fdt_size_t *size)
143{
144 int index = dev_read_stringlist_search(dev, "reg-names", name);
145
146 if (index < 0)
147 return FDT_ADDR_T_NONE;
148 else
149 return dev_read_addr_size_index(dev, index, size);
150}
151
Simon Glass2f9a6122020-01-27 08:49:40 -0700152void *dev_remap_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100153{
154 fdt_addr_t addr = dev_read_addr_name(dev, name);
155
156 if (addr == FDT_ADDR_T_NONE)
157 return NULL;
158
159 return map_physmem(addr, 0, MAP_NOCACHE);
160}
161
Simon Glass2f9a6122020-01-27 08:49:40 -0700162fdt_addr_t dev_read_addr(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600163{
164 return dev_read_addr_index(dev, 0);
165}
166
Simon Glass2f9a6122020-01-27 08:49:40 -0700167void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsich7719b392017-09-11 22:04:12 +0200168{
169 fdt_addr_t addr = dev_read_addr(dev);
170
Sean Anderson42db70b2020-06-24 06:41:13 -0400171 return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr;
Philipp Tomsich7719b392017-09-11 22:04:12 +0200172}
173
Simon Glass2f9a6122020-01-27 08:49:40 -0700174void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200175{
176 return dev_remap_addr_index(dev, 0);
177}
178
Simon Glass2f9a6122020-01-27 08:49:40 -0700179fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100180 fdt_size_t *sizep)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600181{
182 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
183}
184
Simon Glass2f9a6122020-01-27 08:49:40 -0700185const char *dev_read_name(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600186{
187 return ofnode_get_name(dev_ofnode(dev));
188}
189
Simon Glass2f9a6122020-01-27 08:49:40 -0700190int dev_read_stringlist_search(const struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100191 const char *string)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600192{
193 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
194}
195
Simon Glass2f9a6122020-01-27 08:49:40 -0700196int dev_read_string_index(const struct udevice *dev, const char *propname,
197 int index, const char **outp)
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200198{
199 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
200}
201
Simon Glass2f9a6122020-01-27 08:49:40 -0700202int dev_read_string_count(const struct udevice *dev, const char *propname)
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200203{
204 return ofnode_read_string_count(dev_ofnode(dev), propname);
205}
206
Simon Glass2f9a6122020-01-27 08:49:40 -0700207int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
Mario Six6eac2222018-01-15 11:07:18 +0100208 const char *cells_name, int cell_count,
209 int index, struct ofnode_phandle_args *out_args)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600210{
211 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
212 cells_name, cell_count, index,
213 out_args);
214}
215
Simon Glass2f9a6122020-01-27 08:49:40 -0700216int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200217 const char *list_name, const char *cells_name,
218 int cell_count)
Patrice Chotard99503c12017-11-29 09:06:10 +0100219{
220 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200221 cells_name, cell_count);
Patrice Chotard99503c12017-11-29 09:06:10 +0100222}
223
Simon Glass2f9a6122020-01-27 08:49:40 -0700224int dev_read_addr_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600225{
226 return ofnode_read_addr_cells(dev_ofnode(dev));
227}
228
Simon Glass2f9a6122020-01-27 08:49:40 -0700229int dev_read_size_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600230{
231 return ofnode_read_size_cells(dev_ofnode(dev));
232}
233
Simon Glass2f9a6122020-01-27 08:49:40 -0700234int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600235{
236 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
237}
238
Simon Glass2f9a6122020-01-27 08:49:40 -0700239int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600240{
241 return ofnode_read_simple_size_cells(dev_ofnode(dev));
242}
243
Simon Glass2f9a6122020-01-27 08:49:40 -0700244int dev_read_phandle(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600245{
246 ofnode node = dev_ofnode(dev);
247
248 if (ofnode_is_np(node))
249 return ofnode_to_np(node)->phandle;
250 else
251 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
252}
253
Simon Glass2f9a6122020-01-27 08:49:40 -0700254const void *dev_read_prop(const struct udevice *dev, const char *propname,
255 int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600256{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900257 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600258}
259
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100260int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
261{
262 return ofnode_get_first_property(dev_ofnode(dev), prop);
263}
264
265int dev_read_next_prop(struct ofprop *prop)
266{
267 return ofnode_get_next_property(prop);
268}
269
270const void *dev_read_prop_by_prop(struct ofprop *prop,
271 const char **propname, int *lenp)
272{
273 return ofnode_get_property_by_prop(prop, propname, lenp);
274}
275
Simon Glass2f9a6122020-01-27 08:49:40 -0700276int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600277{
278 ofnode node = dev_ofnode(dev);
279 const char *uc_name = dev->uclass->uc_drv->name;
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200280 int ret = -ENOTSUPP;
Simon Glasscfdae5c2017-05-18 20:09:04 -0600281
282 if (ofnode_is_np(node)) {
283 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
284 if (ret >= 0)
285 *devnump = ret;
286 } else {
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200287#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600288 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
289 ofnode_to_offset(node), devnump);
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200290#endif
Simon Glasscfdae5c2017-05-18 20:09:04 -0600291 }
292
293 return ret;
294}
295
Simon Glass2f9a6122020-01-27 08:49:40 -0700296int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600297 u32 *out_values, size_t sz)
298{
299 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
300}
301
Simon Glass2f9a6122020-01-27 08:49:40 -0700302const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
303 const char *propname, size_t sz)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600304{
305 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
306}
Simon Glassfa031f82017-06-12 06:21:30 -0600307
Simon Glass2f9a6122020-01-27 08:49:40 -0700308int dev_read_enabled(const struct udevice *dev)
Simon Glassfa031f82017-06-12 06:21:30 -0600309{
310 ofnode node = dev_ofnode(dev);
311
312 if (ofnode_is_np(node))
313 return of_device_is_available(ofnode_to_np(node));
314 else
315 return fdtdec_get_is_enabled(gd->fdt_blob,
316 ofnode_to_offset(node));
317}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600318
Simon Glass2f9a6122020-01-27 08:49:40 -0700319int dev_read_resource(const struct udevice *dev, uint index,
320 struct resource *res)
Simon Glassf7bfcc42017-07-25 08:29:55 -0600321{
322 return ofnode_read_resource(dev_ofnode(dev), index, res);
323}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900324
Simon Glass2f9a6122020-01-27 08:49:40 -0700325int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900326 struct resource *res)
327{
328 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
329}
Mario Sixaefac062018-01-15 11:07:19 +0100330
Simon Glass2f9a6122020-01-27 08:49:40 -0700331u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
Mario Sixaefac062018-01-15 11:07:19 +0100332{
333 return ofnode_translate_address(dev_ofnode(dev), in_addr);
334}
Michal Simekd0e30a02019-01-31 16:30:59 +0100335
Simon Glass2f9a6122020-01-27 08:49:40 -0700336u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
Fabien Dessenne22236e02019-05-31 15:11:30 +0200337{
338 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
339}
340
Michal Simekd0e30a02019-01-31 16:30:59 +0100341int dev_read_alias_highest_id(const char *stem)
342{
343 if (of_live_active())
344 return of_alias_get_highest_id(stem);
345
346 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
347}
Simon Glass23b27592019-09-15 12:08:58 -0600348
Simon Glass2f9a6122020-01-27 08:49:40 -0700349fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600350{
351 ulong addr;
352
353 addr = dev_read_addr(dev);
354 if (addr == FDT_ADDR_T_NONE && !of_live_active())
355 addr = devfdt_get_addr_pci(dev);
356
357 return addr;
358}
developerd93c8b42020-05-02 11:35:09 +0200359
360int dev_get_child_count(const struct udevice *dev)
361{
362 return ofnode_get_child_count(dev_ofnode(dev));
363}
Stefan Roese0a9ecc52020-08-05 13:56:11 +0200364
365int dev_read_pci_bus_range(const struct udevice *dev,
366 struct resource *res)
367{
368 const u32 *values;
369 int len;
370
371 values = dev_read_prop(dev, "bus-range", &len);
372 if (!values || len < sizeof(*values) * 2)
373 return -EINVAL;
374
375 res->start = *values++;
376 res->end = *values;
377
378 return 0;
379}