blob: 4307ca45799356c4371f1c54cc29df5e2de7d778 [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
Simon Glass2f9a6122020-01-27 08:49:40 -070016int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp)
Masahiro Yamada71d115f2017-12-30 02:00:05 +090017{
18 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
19}
20
Simon Glass2f9a6122020-01-27 08:49:40 -070021int dev_read_u32_default(const struct udevice *dev, const char *propname,
22 int def)
Simon Glasscfdae5c2017-05-18 20:09:04 -060023{
24 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
25}
26
Dario Binacchi81d80b52020-03-29 18:04:41 +020027int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
28 u32 *outp)
29{
30 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
31}
32
33u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
34 int index, u32 def)
35{
36 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
37 def);
38}
39
Simon Glass2f9a6122020-01-27 08:49:40 -070040int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
Simon Glass6df01f92018-12-10 10:37:37 -070041{
42 return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
43}
44
Simon Glass2f9a6122020-01-27 08:49:40 -070045int dev_read_s32_default(const struct udevice *dev, const char *propname,
46 int def)
Simon Glass6df01f92018-12-10 10:37:37 -070047{
48 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
49}
50
Simon Glass2f9a6122020-01-27 08:49:40 -070051int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp)
Simon Glass6df01f92018-12-10 10:37:37 -070052{
53 u32 val;
54 int ret;
55
56 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
57 if (ret)
58 return ret;
59 *outp = val;
60
61 return 0;
62}
63
Simon Glass2f9a6122020-01-27 08:49:40 -070064int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp)
T Karthik Reddy478860d2019-09-02 16:34:30 +020065{
66 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
67}
68
Simon Glass2f9a6122020-01-27 08:49:40 -070069u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
70 u64 def)
T Karthik Reddy478860d2019-09-02 16:34:30 +020071{
72 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
73}
74
Simon Glass2f9a6122020-01-27 08:49:40 -070075const char *dev_read_string(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -060076{
77 return ofnode_read_string(dev_ofnode(dev), propname);
78}
79
Simon Glass2f9a6122020-01-27 08:49:40 -070080bool dev_read_bool(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -060081{
82 return ofnode_read_bool(dev_ofnode(dev), propname);
83}
84
Simon Glass2f9a6122020-01-27 08:49:40 -070085ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name)
Simon Glasscfdae5c2017-05-18 20:09:04 -060086{
87 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
88}
89
Simon Glass2f9a6122020-01-27 08:49:40 -070090ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -060091{
92 return ofnode_first_subnode(dev_ofnode(dev));
93}
94
95ofnode dev_read_next_subnode(ofnode node)
96{
97 return ofnode_next_subnode(node);
98}
99
Simon Glass2f9a6122020-01-27 08:49:40 -0700100int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600101{
102 return ofnode_read_size(dev_ofnode(dev), propname);
103}
104
Simon Glass2f9a6122020-01-27 08:49:40 -0700105fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600106{
107 if (ofnode_is_np(dev_ofnode(dev)))
108 return ofnode_get_addr_index(dev_ofnode(dev), index);
109 else
110 return devfdt_get_addr_index(dev, index);
111}
112
Simon Glass2f9a6122020-01-27 08:49:40 -0700113fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530114 fdt_size_t *size)
115{
116 if (ofnode_is_np(dev_ofnode(dev)))
117 return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
118 else
119 return devfdt_get_addr_size_index(dev, index, size);
120}
121
Simon Glass2f9a6122020-01-27 08:49:40 -0700122void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200123{
124 fdt_addr_t addr = dev_read_addr_index(dev, index);
125
126 if (addr == FDT_ADDR_T_NONE)
127 return NULL;
128
129 return map_physmem(addr, 0, MAP_NOCACHE);
130}
131
Simon Glass2f9a6122020-01-27 08:49:40 -0700132fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100133{
134 int index = dev_read_stringlist_search(dev, "reg-names", name);
135
136 if (index < 0)
137 return FDT_ADDR_T_NONE;
138 else
139 return dev_read_addr_index(dev, index);
140}
141
Simon Glass2f9a6122020-01-27 08:49:40 -0700142fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530143 fdt_size_t *size)
144{
145 int index = dev_read_stringlist_search(dev, "reg-names", name);
146
147 if (index < 0)
148 return FDT_ADDR_T_NONE;
149 else
150 return dev_read_addr_size_index(dev, index, size);
151}
152
Simon Glass2f9a6122020-01-27 08:49:40 -0700153void *dev_remap_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100154{
155 fdt_addr_t addr = dev_read_addr_name(dev, name);
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(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600164{
165 return dev_read_addr_index(dev, 0);
166}
167
Simon Glass2f9a6122020-01-27 08:49:40 -0700168void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsich7719b392017-09-11 22:04:12 +0200169{
170 fdt_addr_t addr = dev_read_addr(dev);
171
Sean Anderson42db70b2020-06-24 06:41:13 -0400172 return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr;
Philipp Tomsich7719b392017-09-11 22:04:12 +0200173}
174
Simon Glass2f9a6122020-01-27 08:49:40 -0700175void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200176{
177 return dev_remap_addr_index(dev, 0);
178}
179
Simon Glass2f9a6122020-01-27 08:49:40 -0700180fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100181 fdt_size_t *sizep)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600182{
183 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
184}
185
Simon Glass2f9a6122020-01-27 08:49:40 -0700186const char *dev_read_name(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600187{
188 return ofnode_get_name(dev_ofnode(dev));
189}
190
Simon Glass2f9a6122020-01-27 08:49:40 -0700191int dev_read_stringlist_search(const struct udevice *dev, const char *property,
Mario Six6eac2222018-01-15 11:07:18 +0100192 const char *string)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600193{
194 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
195}
196
Simon Glass2f9a6122020-01-27 08:49:40 -0700197int dev_read_string_index(const struct udevice *dev, const char *propname,
198 int index, const char **outp)
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200199{
200 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
201}
202
Simon Glass2f9a6122020-01-27 08:49:40 -0700203int dev_read_string_count(const struct udevice *dev, const char *propname)
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200204{
205 return ofnode_read_string_count(dev_ofnode(dev), propname);
206}
207
Simon Glass2f9a6122020-01-27 08:49:40 -0700208int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
Mario Six6eac2222018-01-15 11:07:18 +0100209 const char *cells_name, int cell_count,
210 int index, struct ofnode_phandle_args *out_args)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600211{
212 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
213 cells_name, cell_count, index,
214 out_args);
215}
216
Simon Glass2f9a6122020-01-27 08:49:40 -0700217int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200218 const char *list_name, const char *cells_name,
219 int cell_count)
Patrice Chotard99503c12017-11-29 09:06:10 +0100220{
221 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200222 cells_name, cell_count);
Patrice Chotard99503c12017-11-29 09:06:10 +0100223}
224
Simon Glass2f9a6122020-01-27 08:49:40 -0700225int dev_read_addr_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600226{
227 return ofnode_read_addr_cells(dev_ofnode(dev));
228}
229
Simon Glass2f9a6122020-01-27 08:49:40 -0700230int dev_read_size_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600231{
232 return ofnode_read_size_cells(dev_ofnode(dev));
233}
234
Simon Glass2f9a6122020-01-27 08:49:40 -0700235int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600236{
237 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
238}
239
Simon Glass2f9a6122020-01-27 08:49:40 -0700240int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600241{
242 return ofnode_read_simple_size_cells(dev_ofnode(dev));
243}
244
Simon Glass2f9a6122020-01-27 08:49:40 -0700245int dev_read_phandle(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600246{
247 ofnode node = dev_ofnode(dev);
248
249 if (ofnode_is_np(node))
250 return ofnode_to_np(node)->phandle;
251 else
252 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
253}
254
Simon Glass2f9a6122020-01-27 08:49:40 -0700255const void *dev_read_prop(const struct udevice *dev, const char *propname,
256 int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600257{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900258 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600259}
260
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100261int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
262{
263 return ofnode_get_first_property(dev_ofnode(dev), prop);
264}
265
266int dev_read_next_prop(struct ofprop *prop)
267{
268 return ofnode_get_next_property(prop);
269}
270
271const void *dev_read_prop_by_prop(struct ofprop *prop,
272 const char **propname, int *lenp)
273{
274 return ofnode_get_property_by_prop(prop, propname, lenp);
275}
276
Simon Glass2f9a6122020-01-27 08:49:40 -0700277int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600278{
279 ofnode node = dev_ofnode(dev);
280 const char *uc_name = dev->uclass->uc_drv->name;
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200281 int ret = -ENOTSUPP;
Simon Glasscfdae5c2017-05-18 20:09:04 -0600282
283 if (ofnode_is_np(node)) {
284 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
Simon Glassa2b58552020-12-16 21:20:12 -0700285 if (ret >= 0) {
Simon Glasscfdae5c2017-05-18 20:09:04 -0600286 *devnump = ret;
Simon Glassa2b58552020-12-16 21:20:12 -0700287 ret = 0;
288 }
Simon Glasscfdae5c2017-05-18 20:09:04 -0600289 } else {
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200290#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600291 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
292 ofnode_to_offset(node), devnump);
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200293#endif
Simon Glasscfdae5c2017-05-18 20:09:04 -0600294 }
295
296 return ret;
297}
298
Simon Glass2f9a6122020-01-27 08:49:40 -0700299int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600300 u32 *out_values, size_t sz)
301{
302 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
303}
304
Simon Glass2f9a6122020-01-27 08:49:40 -0700305const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
306 const char *propname, size_t sz)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600307{
308 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
309}
Simon Glassfa031f82017-06-12 06:21:30 -0600310
Simon Glass2f9a6122020-01-27 08:49:40 -0700311int dev_read_enabled(const struct udevice *dev)
Simon Glassfa031f82017-06-12 06:21:30 -0600312{
313 ofnode node = dev_ofnode(dev);
314
315 if (ofnode_is_np(node))
316 return of_device_is_available(ofnode_to_np(node));
317 else
318 return fdtdec_get_is_enabled(gd->fdt_blob,
319 ofnode_to_offset(node));
320}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600321
Simon Glass2f9a6122020-01-27 08:49:40 -0700322int dev_read_resource(const struct udevice *dev, uint index,
323 struct resource *res)
Simon Glassf7bfcc42017-07-25 08:29:55 -0600324{
325 return ofnode_read_resource(dev_ofnode(dev), index, res);
326}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900327
Simon Glass2f9a6122020-01-27 08:49:40 -0700328int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900329 struct resource *res)
330{
331 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
332}
Mario Sixaefac062018-01-15 11:07:19 +0100333
Simon Glass2f9a6122020-01-27 08:49:40 -0700334u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
Mario Sixaefac062018-01-15 11:07:19 +0100335{
336 return ofnode_translate_address(dev_ofnode(dev), in_addr);
337}
Michal Simekd0e30a02019-01-31 16:30:59 +0100338
Simon Glass2f9a6122020-01-27 08:49:40 -0700339u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
Fabien Dessenne22236e02019-05-31 15:11:30 +0200340{
341 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
342}
343
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +0100344int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
345 dma_addr_t *bus, u64 *size)
346{
347 return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
348}
349
Michal Simekd0e30a02019-01-31 16:30:59 +0100350int dev_read_alias_highest_id(const char *stem)
351{
352 if (of_live_active())
353 return of_alias_get_highest_id(stem);
354
355 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
356}
Simon Glass23b27592019-09-15 12:08:58 -0600357
Simon Glass2f9a6122020-01-27 08:49:40 -0700358fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600359{
360 ulong addr;
361
362 addr = dev_read_addr(dev);
363 if (addr == FDT_ADDR_T_NONE && !of_live_active())
364 addr = devfdt_get_addr_pci(dev);
365
366 return addr;
367}
developerd93c8b42020-05-02 11:35:09 +0200368
369int dev_get_child_count(const struct udevice *dev)
370{
371 return ofnode_get_child_count(dev_ofnode(dev));
372}
Stefan Roese0a9ecc52020-08-05 13:56:11 +0200373
374int dev_read_pci_bus_range(const struct udevice *dev,
375 struct resource *res)
376{
377 const u32 *values;
378 int len;
379
380 values = dev_read_prop(dev, "bus-range", &len);
381 if (!values || len < sizeof(*values) * 2)
382 return -EINVAL;
383
384 res->start = *values++;
385 res->end = *values;
386
387 return 0;
388}
Dario Binacchi836cc9d2020-12-30 00:16:26 +0100389
390int dev_decode_display_timing(const struct udevice *dev, int index,
391 struct display_timing *config)
392{
393 return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
394}