blob: 5749473a6cae0ed13a6a434cea203aaadf77b9c9 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Stefan Roesea8c43062020-04-29 09:08:44 +020012#include <asm/types.h>
13#include <asm/io.h>
Stefan Roese0a9ecc52020-08-05 13:56:11 +020014#include <linux/ioport.h>
Simon Glasscfdae5c2017-05-18 20:09:04 -060015
Stefan Herbrechtsmeier1b090e62022-06-14 15:21:30 +020016int dev_read_u8(const struct udevice *dev, const char *propname, u8 *outp)
17{
18 return ofnode_read_u8(dev_ofnode(dev), propname, outp);
19}
20
21u8 dev_read_u8_default(const struct udevice *dev, const char *propname, u8 def)
22{
23 return ofnode_read_u8_default(dev_ofnode(dev), propname, def);
24}
25
26int dev_read_u16(const struct udevice *dev, const char *propname, u16 *outp)
27{
28 return ofnode_read_u16(dev_ofnode(dev), propname, outp);
29}
30
31u16 dev_read_u16_default(const struct udevice *dev, const char *propname,
32 u16 def)
33{
34 return ofnode_read_u16_default(dev_ofnode(dev), propname, def);
35}
36
Simon Glass2f9a6122020-01-27 08:49:40 -070037int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp)
Masahiro Yamada71d115f2017-12-30 02:00:05 +090038{
39 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
40}
41
Simon Glass2f9a6122020-01-27 08:49:40 -070042int dev_read_u32_default(const struct udevice *dev, const char *propname,
43 int def)
Simon Glasscfdae5c2017-05-18 20:09:04 -060044{
45 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
46}
47
Dario Binacchi81d80b52020-03-29 18:04:41 +020048int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
49 u32 *outp)
50{
51 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
52}
53
54u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
55 int index, u32 def)
56{
57 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
58 def);
59}
60
Simon Glass2f9a6122020-01-27 08:49:40 -070061int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
Simon Glass6df01f92018-12-10 10:37:37 -070062{
63 return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
64}
65
Simon Glass2f9a6122020-01-27 08:49:40 -070066int dev_read_s32_default(const struct udevice *dev, const char *propname,
67 int def)
Simon Glass6df01f92018-12-10 10:37:37 -070068{
69 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
70}
71
Simon Glass2f9a6122020-01-27 08:49:40 -070072int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp)
Simon Glass6df01f92018-12-10 10:37:37 -070073{
74 u32 val;
75 int ret;
76
77 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
78 if (ret)
79 return ret;
80 *outp = val;
81
82 return 0;
83}
84
Simon Glass2f9a6122020-01-27 08:49:40 -070085int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp)
T Karthik Reddy478860d2019-09-02 16:34:30 +020086{
87 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
88}
89
Simon Glass2f9a6122020-01-27 08:49:40 -070090u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
91 u64 def)
T Karthik Reddy478860d2019-09-02 16:34:30 +020092{
93 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
94}
95
Simon Glass2f9a6122020-01-27 08:49:40 -070096const char *dev_read_string(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -060097{
98 return ofnode_read_string(dev_ofnode(dev), propname);
99}
100
Simon Glass2f9a6122020-01-27 08:49:40 -0700101bool dev_read_bool(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600102{
103 return ofnode_read_bool(dev_ofnode(dev), propname);
104}
105
Simon Glass2f9a6122020-01-27 08:49:40 -0700106ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600107{
108 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
109}
110
Simon Glass2f9a6122020-01-27 08:49:40 -0700111ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600112{
113 return ofnode_first_subnode(dev_ofnode(dev));
114}
115
116ofnode dev_read_next_subnode(ofnode node)
117{
118 return ofnode_next_subnode(node);
119}
120
Simon Glass2f9a6122020-01-27 08:49:40 -0700121int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600122{
123 return ofnode_read_size(dev_ofnode(dev), propname);
124}
125
Simon Glass2f9a6122020-01-27 08:49:40 -0700126fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600127{
128 if (ofnode_is_np(dev_ofnode(dev)))
129 return ofnode_get_addr_index(dev_ofnode(dev), index);
130 else
131 return devfdt_get_addr_index(dev, index);
132}
133
Johan Jonker54ab0442023-03-13 01:31:25 +0100134void *dev_read_addr_index_ptr(const struct udevice *dev, int index)
135{
136 fdt_addr_t addr = dev_read_addr_index(dev, index);
137
138 if (addr == FDT_ADDR_T_NONE)
139 return NULL;
140
141 return map_sysmem(addr, 0);
142}
143
Simon Glass2f9a6122020-01-27 08:49:40 -0700144fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530145 fdt_size_t *size)
146{
147 if (ofnode_is_np(dev_ofnode(dev)))
148 return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
149 else
150 return devfdt_get_addr_size_index(dev, index, size);
151}
152
Simon Glass2f9a6122020-01-27 08:49:40 -0700153void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200154{
155 fdt_addr_t addr = dev_read_addr_index(dev, index);
156
157 if (addr == FDT_ADDR_T_NONE)
158 return NULL;
159
160 return map_physmem(addr, 0, MAP_NOCACHE);
161}
162
Simon Glass2f9a6122020-01-27 08:49:40 -0700163fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100164{
165 int index = dev_read_stringlist_search(dev, "reg-names", name);
166
167 if (index < 0)
168 return FDT_ADDR_T_NONE;
169 else
170 return dev_read_addr_index(dev, index);
171}
172
Simon Glass2f9a6122020-01-27 08:49:40 -0700173fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530174 fdt_size_t *size)
175{
176 int index = dev_read_stringlist_search(dev, "reg-names", name);
177
178 if (index < 0)
179 return FDT_ADDR_T_NONE;
180 else
181 return dev_read_addr_size_index(dev, index, size);
182}
183
Simon Glass2f9a6122020-01-27 08:49:40 -0700184void *dev_remap_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100185{
186 fdt_addr_t addr = dev_read_addr_name(dev, name);
187
188 if (addr == FDT_ADDR_T_NONE)
189 return NULL;
190
191 return map_physmem(addr, 0, MAP_NOCACHE);
192}
193
Simon Glass2f9a6122020-01-27 08:49:40 -0700194fdt_addr_t dev_read_addr(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600195{
196 return dev_read_addr_index(dev, 0);
197}
198
Simon Glass2f9a6122020-01-27 08:49:40 -0700199void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsich7719b392017-09-11 22:04:12 +0200200{
201 fdt_addr_t addr = dev_read_addr(dev);
202
Johan Jonker54ab0442023-03-13 01:31:25 +0100203 if (addr == FDT_ADDR_T_NONE)
204 return NULL;
205
206 return map_sysmem(addr, 0);
Philipp Tomsich7719b392017-09-11 22:04:12 +0200207}
208
Simon Glass2f9a6122020-01-27 08:49:40 -0700209void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200210{
211 return dev_remap_addr_index(dev, 0);
212}
213
John Keepingab62d0b2023-06-01 15:11:19 +0100214fdt_addr_t dev_read_addr_size(const struct udevice *dev, fdt_size_t *sizep)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600215{
John Keepingab62d0b2023-06-01 15:11:19 +0100216 return dev_read_addr_size_index(dev, 0, sizep);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600217}
218
Simon Glass2f9a6122020-01-27 08:49:40 -0700219const char *dev_read_name(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600220{
221 return ofnode_get_name(dev_ofnode(dev));
222}
223
Simon Glass2f9a6122020-01-27 08:49:40 -0700224int dev_read_stringlist_search(const struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100225 const char *string)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600226{
227 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
228}
229
Simon Glass2f9a6122020-01-27 08:49:40 -0700230int dev_read_string_index(const struct udevice *dev, const char *propname,
231 int index, const char **outp)
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200232{
233 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
234}
235
Simon Glass2f9a6122020-01-27 08:49:40 -0700236int dev_read_string_count(const struct udevice *dev, const char *propname)
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200237{
238 return ofnode_read_string_count(dev_ofnode(dev), propname);
239}
240
Simon Glass9580bfc2021-10-23 17:26:07 -0600241int dev_read_string_list(const struct udevice *dev, const char *propname,
242 const char ***listp)
243{
244 return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
245}
246
Simon Glass2f9a6122020-01-27 08:49:40 -0700247int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
Mario Six6eac2222018-01-15 11:07:18 +0100248 const char *cells_name, int cell_count,
249 int index, struct ofnode_phandle_args *out_args)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600250{
251 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
252 cells_name, cell_count, index,
253 out_args);
254}
255
Simon Glass2f9a6122020-01-27 08:49:40 -0700256int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200257 const char *list_name, const char *cells_name,
258 int cell_count)
Patrice Chotard99503c12017-11-29 09:06:10 +0100259{
260 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200261 cells_name, cell_count);
Patrice Chotard99503c12017-11-29 09:06:10 +0100262}
263
Simon Glass2f9a6122020-01-27 08:49:40 -0700264int dev_read_addr_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600265{
266 return ofnode_read_addr_cells(dev_ofnode(dev));
267}
268
Simon Glass2f9a6122020-01-27 08:49:40 -0700269int dev_read_size_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600270{
271 return ofnode_read_size_cells(dev_ofnode(dev));
272}
273
Simon Glass2f9a6122020-01-27 08:49:40 -0700274int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600275{
276 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
277}
278
Simon Glass2f9a6122020-01-27 08:49:40 -0700279int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600280{
281 return ofnode_read_simple_size_cells(dev_ofnode(dev));
282}
283
Simon Glass2f9a6122020-01-27 08:49:40 -0700284int dev_read_phandle(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600285{
286 ofnode node = dev_ofnode(dev);
287
288 if (ofnode_is_np(node))
289 return ofnode_to_np(node)->phandle;
290 else
291 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
292}
293
Simon Glass2f9a6122020-01-27 08:49:40 -0700294const void *dev_read_prop(const struct udevice *dev, const char *propname,
295 int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600296{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900297 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600298}
299
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100300int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
301{
Simon Glassfec058d2022-09-06 20:27:13 -0600302 return ofnode_first_property(dev_ofnode(dev), prop);
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100303}
304
305int dev_read_next_prop(struct ofprop *prop)
306{
Simon Glassfec058d2022-09-06 20:27:13 -0600307 return ofnode_next_property(prop);
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100308}
309
310const void *dev_read_prop_by_prop(struct ofprop *prop,
311 const char **propname, int *lenp)
312{
Simon Glassd0aff8b2022-09-06 20:27:14 -0600313 return ofprop_get_property(prop, propname, lenp);
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100314}
315
Simon Glass2f9a6122020-01-27 08:49:40 -0700316int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600317{
318 ofnode node = dev_ofnode(dev);
319 const char *uc_name = dev->uclass->uc_drv->name;
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200320 int ret = -ENOTSUPP;
Simon Glasscfdae5c2017-05-18 20:09:04 -0600321
322 if (ofnode_is_np(node)) {
323 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
Simon Glassa2b58552020-12-16 21:20:12 -0700324 if (ret >= 0) {
Simon Glasscfdae5c2017-05-18 20:09:04 -0600325 *devnump = ret;
Simon Glassa2b58552020-12-16 21:20:12 -0700326 ret = 0;
327 }
Simon Glasscfdae5c2017-05-18 20:09:04 -0600328 } else {
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200329#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600330 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
331 ofnode_to_offset(node), devnump);
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200332#endif
Simon Glasscfdae5c2017-05-18 20:09:04 -0600333 }
334
335 return ret;
336}
337
Simon Glass2f9a6122020-01-27 08:49:40 -0700338int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600339 u32 *out_values, size_t sz)
340{
341 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
342}
343
Simon Glass2f9a6122020-01-27 08:49:40 -0700344const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
345 const char *propname, size_t sz)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600346{
347 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
348}
Simon Glassfa031f82017-06-12 06:21:30 -0600349
Simon Glass2f9a6122020-01-27 08:49:40 -0700350int dev_read_enabled(const struct udevice *dev)
Simon Glassfa031f82017-06-12 06:21:30 -0600351{
352 ofnode node = dev_ofnode(dev);
353
354 if (ofnode_is_np(node))
355 return of_device_is_available(ofnode_to_np(node));
356 else
357 return fdtdec_get_is_enabled(gd->fdt_blob,
358 ofnode_to_offset(node));
359}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600360
Simon Glass2f9a6122020-01-27 08:49:40 -0700361int dev_read_resource(const struct udevice *dev, uint index,
362 struct resource *res)
Simon Glassf7bfcc42017-07-25 08:29:55 -0600363{
364 return ofnode_read_resource(dev_ofnode(dev), index, res);
365}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900366
Simon Glass2f9a6122020-01-27 08:49:40 -0700367int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900368 struct resource *res)
369{
370 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
371}
Mario Sixaefac062018-01-15 11:07:19 +0100372
Simon Glass2f9a6122020-01-27 08:49:40 -0700373u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
Mario Sixaefac062018-01-15 11:07:19 +0100374{
375 return ofnode_translate_address(dev_ofnode(dev), in_addr);
376}
Michal Simekd0e30a02019-01-31 16:30:59 +0100377
Simon Glass2f9a6122020-01-27 08:49:40 -0700378u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
Fabien Dessenne22236e02019-05-31 15:11:30 +0200379{
380 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
381}
382
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +0100383int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
384 dma_addr_t *bus, u64 *size)
385{
386 return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
387}
388
Michal Simekd0e30a02019-01-31 16:30:59 +0100389int dev_read_alias_highest_id(const char *stem)
390{
391 if (of_live_active())
392 return of_alias_get_highest_id(stem);
393
394 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
395}
Simon Glass23b27592019-09-15 12:08:58 -0600396
Simon Glass2f9a6122020-01-27 08:49:40 -0700397fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600398{
399 ulong addr;
400
401 addr = dev_read_addr(dev);
402 if (addr == FDT_ADDR_T_NONE && !of_live_active())
403 addr = devfdt_get_addr_pci(dev);
404
405 return addr;
406}
developerd93c8b42020-05-02 11:35:09 +0200407
408int dev_get_child_count(const struct udevice *dev)
409{
410 return ofnode_get_child_count(dev_ofnode(dev));
411}
Stefan Roese0a9ecc52020-08-05 13:56:11 +0200412
413int dev_read_pci_bus_range(const struct udevice *dev,
414 struct resource *res)
415{
416 const u32 *values;
417 int len;
418
419 values = dev_read_prop(dev, "bus-range", &len);
420 if (!values || len < sizeof(*values) * 2)
421 return -EINVAL;
422
423 res->start = *values++;
424 res->end = *values;
425
426 return 0;
427}
Dario Binacchi836cc9d2020-12-30 00:16:26 +0100428
429int dev_decode_display_timing(const struct udevice *dev, int index,
430 struct display_timing *config)
431{
432 return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
433}
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200434
Nikhil M Jainff407062023-01-31 15:35:14 +0530435int dev_decode_panel_timing(const struct udevice *dev,
436 struct display_timing *config)
437{
438 return ofnode_decode_panel_timing(dev_ofnode(dev), config);
439}
440
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200441ofnode dev_get_phy_node(const struct udevice *dev)
442{
443 return ofnode_get_phy_node(dev_ofnode(dev));
444}
Marek Behúnbc194772022-04-07 00:33:01 +0200445
446phy_interface_t dev_read_phy_mode(const struct udevice *dev)
447{
448 return ofnode_read_phy_mode(dev_ofnode(dev));
449}