blob: 86f3f881706e046bffc8338502922a6a8e40c194 [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,
217 const char *list_name, const char *cells_name)
Patrice Chotard99503c12017-11-29 09:06:10 +0100218{
219 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
220 cells_name);
221}
222
Simon Glass2f9a6122020-01-27 08:49:40 -0700223int dev_read_addr_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600224{
225 return ofnode_read_addr_cells(dev_ofnode(dev));
226}
227
Simon Glass2f9a6122020-01-27 08:49:40 -0700228int dev_read_size_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600229{
230 return ofnode_read_size_cells(dev_ofnode(dev));
231}
232
Simon Glass2f9a6122020-01-27 08:49:40 -0700233int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600234{
235 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
236}
237
Simon Glass2f9a6122020-01-27 08:49:40 -0700238int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600239{
240 return ofnode_read_simple_size_cells(dev_ofnode(dev));
241}
242
Simon Glass2f9a6122020-01-27 08:49:40 -0700243int dev_read_phandle(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600244{
245 ofnode node = dev_ofnode(dev);
246
247 if (ofnode_is_np(node))
248 return ofnode_to_np(node)->phandle;
249 else
250 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
251}
252
Simon Glass2f9a6122020-01-27 08:49:40 -0700253const void *dev_read_prop(const struct udevice *dev, const char *propname,
254 int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600255{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900256 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600257}
258
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100259int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
260{
261 return ofnode_get_first_property(dev_ofnode(dev), prop);
262}
263
264int dev_read_next_prop(struct ofprop *prop)
265{
266 return ofnode_get_next_property(prop);
267}
268
269const void *dev_read_prop_by_prop(struct ofprop *prop,
270 const char **propname, int *lenp)
271{
272 return ofnode_get_property_by_prop(prop, propname, lenp);
273}
274
Simon Glass2f9a6122020-01-27 08:49:40 -0700275int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600276{
277 ofnode node = dev_ofnode(dev);
278 const char *uc_name = dev->uclass->uc_drv->name;
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200279 int ret = -ENOTSUPP;
Simon Glasscfdae5c2017-05-18 20:09:04 -0600280
281 if (ofnode_is_np(node)) {
282 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
283 if (ret >= 0)
284 *devnump = ret;
285 } else {
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200286#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600287 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
288 ofnode_to_offset(node), devnump);
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200289#endif
Simon Glasscfdae5c2017-05-18 20:09:04 -0600290 }
291
292 return ret;
293}
294
Simon Glass2f9a6122020-01-27 08:49:40 -0700295int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600296 u32 *out_values, size_t sz)
297{
298 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
299}
300
Simon Glass2f9a6122020-01-27 08:49:40 -0700301const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
302 const char *propname, size_t sz)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600303{
304 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
305}
Simon Glassfa031f82017-06-12 06:21:30 -0600306
Simon Glass2f9a6122020-01-27 08:49:40 -0700307int dev_read_enabled(const struct udevice *dev)
Simon Glassfa031f82017-06-12 06:21:30 -0600308{
309 ofnode node = dev_ofnode(dev);
310
311 if (ofnode_is_np(node))
312 return of_device_is_available(ofnode_to_np(node));
313 else
314 return fdtdec_get_is_enabled(gd->fdt_blob,
315 ofnode_to_offset(node));
316}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600317
Simon Glass2f9a6122020-01-27 08:49:40 -0700318int dev_read_resource(const struct udevice *dev, uint index,
319 struct resource *res)
Simon Glassf7bfcc42017-07-25 08:29:55 -0600320{
321 return ofnode_read_resource(dev_ofnode(dev), index, res);
322}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900323
Simon Glass2f9a6122020-01-27 08:49:40 -0700324int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900325 struct resource *res)
326{
327 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
328}
Mario Sixaefac062018-01-15 11:07:19 +0100329
Simon Glass2f9a6122020-01-27 08:49:40 -0700330u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
Mario Sixaefac062018-01-15 11:07:19 +0100331{
332 return ofnode_translate_address(dev_ofnode(dev), in_addr);
333}
Michal Simekd0e30a02019-01-31 16:30:59 +0100334
Simon Glass2f9a6122020-01-27 08:49:40 -0700335u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
Fabien Dessenne22236e02019-05-31 15:11:30 +0200336{
337 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
338}
339
Michal Simekd0e30a02019-01-31 16:30:59 +0100340int dev_read_alias_highest_id(const char *stem)
341{
342 if (of_live_active())
343 return of_alias_get_highest_id(stem);
344
345 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
346}
Simon Glass23b27592019-09-15 12:08:58 -0600347
Simon Glass2f9a6122020-01-27 08:49:40 -0700348fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600349{
350 ulong addr;
351
352 addr = dev_read_addr(dev);
353 if (addr == FDT_ADDR_T_NONE && !of_live_active())
354 addr = devfdt_get_addr_pci(dev);
355
356 return addr;
357}
developerd93c8b42020-05-02 11:35:09 +0200358
359int dev_get_child_count(const struct udevice *dev)
360{
361 return ofnode_get_child_count(dev_ofnode(dev));
362}
Stefan Roese0a9ecc52020-08-05 13:56:11 +0200363
364int dev_read_pci_bus_range(const struct udevice *dev,
365 struct resource *res)
366{
367 const u32 *values;
368 int len;
369
370 values = dev_read_prop(dev, "bus-range", &len);
371 if (!values || len < sizeof(*values) * 2)
372 return -EINVAL;
373
374 res->start = *values++;
375 res->end = *values;
376
377 return 0;
378}