blob: 31f9e78a06251d7e0e343315e563ad53a6e9f6e4 [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 Glass9580bfc2021-10-23 17:26:07 -0600208int dev_read_string_list(const struct udevice *dev, const char *propname,
209 const char ***listp)
210{
211 return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
212}
213
Simon Glass2f9a6122020-01-27 08:49:40 -0700214int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
Mario Six6eac2222018-01-15 11:07:18 +0100215 const char *cells_name, int cell_count,
216 int index, struct ofnode_phandle_args *out_args)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600217{
218 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
219 cells_name, cell_count, index,
220 out_args);
221}
222
Simon Glass2f9a6122020-01-27 08:49:40 -0700223int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200224 const char *list_name, const char *cells_name,
225 int cell_count)
Patrice Chotard99503c12017-11-29 09:06:10 +0100226{
227 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200228 cells_name, cell_count);
Patrice Chotard99503c12017-11-29 09:06:10 +0100229}
230
Simon Glass2f9a6122020-01-27 08:49:40 -0700231int dev_read_addr_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600232{
233 return ofnode_read_addr_cells(dev_ofnode(dev));
234}
235
Simon Glass2f9a6122020-01-27 08:49:40 -0700236int dev_read_size_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600237{
238 return ofnode_read_size_cells(dev_ofnode(dev));
239}
240
Simon Glass2f9a6122020-01-27 08:49:40 -0700241int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600242{
243 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
244}
245
Simon Glass2f9a6122020-01-27 08:49:40 -0700246int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600247{
248 return ofnode_read_simple_size_cells(dev_ofnode(dev));
249}
250
Simon Glass2f9a6122020-01-27 08:49:40 -0700251int dev_read_phandle(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600252{
253 ofnode node = dev_ofnode(dev);
254
255 if (ofnode_is_np(node))
256 return ofnode_to_np(node)->phandle;
257 else
258 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
259}
260
Simon Glass2f9a6122020-01-27 08:49:40 -0700261const void *dev_read_prop(const struct udevice *dev, const char *propname,
262 int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600263{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900264 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600265}
266
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100267int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
268{
269 return ofnode_get_first_property(dev_ofnode(dev), prop);
270}
271
272int dev_read_next_prop(struct ofprop *prop)
273{
274 return ofnode_get_next_property(prop);
275}
276
277const void *dev_read_prop_by_prop(struct ofprop *prop,
278 const char **propname, int *lenp)
279{
280 return ofnode_get_property_by_prop(prop, propname, lenp);
281}
282
Simon Glass2f9a6122020-01-27 08:49:40 -0700283int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600284{
285 ofnode node = dev_ofnode(dev);
286 const char *uc_name = dev->uclass->uc_drv->name;
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200287 int ret = -ENOTSUPP;
Simon Glasscfdae5c2017-05-18 20:09:04 -0600288
289 if (ofnode_is_np(node)) {
290 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
Simon Glassa2b58552020-12-16 21:20:12 -0700291 if (ret >= 0) {
Simon Glasscfdae5c2017-05-18 20:09:04 -0600292 *devnump = ret;
Simon Glassa2b58552020-12-16 21:20:12 -0700293 ret = 0;
294 }
Simon Glasscfdae5c2017-05-18 20:09:04 -0600295 } else {
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200296#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600297 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
298 ofnode_to_offset(node), devnump);
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +0200299#endif
Simon Glasscfdae5c2017-05-18 20:09:04 -0600300 }
301
302 return ret;
303}
304
Simon Glass2f9a6122020-01-27 08:49:40 -0700305int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600306 u32 *out_values, size_t sz)
307{
308 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
309}
310
Simon Glass2f9a6122020-01-27 08:49:40 -0700311const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
312 const char *propname, size_t sz)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600313{
314 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
315}
Simon Glassfa031f82017-06-12 06:21:30 -0600316
Simon Glass2f9a6122020-01-27 08:49:40 -0700317int dev_read_enabled(const struct udevice *dev)
Simon Glassfa031f82017-06-12 06:21:30 -0600318{
319 ofnode node = dev_ofnode(dev);
320
321 if (ofnode_is_np(node))
322 return of_device_is_available(ofnode_to_np(node));
323 else
324 return fdtdec_get_is_enabled(gd->fdt_blob,
325 ofnode_to_offset(node));
326}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600327
Simon Glass2f9a6122020-01-27 08:49:40 -0700328int dev_read_resource(const struct udevice *dev, uint index,
329 struct resource *res)
Simon Glassf7bfcc42017-07-25 08:29:55 -0600330{
331 return ofnode_read_resource(dev_ofnode(dev), index, res);
332}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900333
Simon Glass2f9a6122020-01-27 08:49:40 -0700334int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900335 struct resource *res)
336{
337 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
338}
Mario Sixaefac062018-01-15 11:07:19 +0100339
Simon Glass2f9a6122020-01-27 08:49:40 -0700340u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
Mario Sixaefac062018-01-15 11:07:19 +0100341{
342 return ofnode_translate_address(dev_ofnode(dev), in_addr);
343}
Michal Simekd0e30a02019-01-31 16:30:59 +0100344
Simon Glass2f9a6122020-01-27 08:49:40 -0700345u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
Fabien Dessenne22236e02019-05-31 15:11:30 +0200346{
347 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
348}
349
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +0100350int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
351 dma_addr_t *bus, u64 *size)
352{
353 return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
354}
355
Michal Simekd0e30a02019-01-31 16:30:59 +0100356int dev_read_alias_highest_id(const char *stem)
357{
358 if (of_live_active())
359 return of_alias_get_highest_id(stem);
360
361 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
362}
Simon Glass23b27592019-09-15 12:08:58 -0600363
Simon Glass2f9a6122020-01-27 08:49:40 -0700364fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600365{
366 ulong addr;
367
368 addr = dev_read_addr(dev);
369 if (addr == FDT_ADDR_T_NONE && !of_live_active())
370 addr = devfdt_get_addr_pci(dev);
371
372 return addr;
373}
developerd93c8b42020-05-02 11:35:09 +0200374
375int dev_get_child_count(const struct udevice *dev)
376{
377 return ofnode_get_child_count(dev_ofnode(dev));
378}
Stefan Roese0a9ecc52020-08-05 13:56:11 +0200379
380int dev_read_pci_bus_range(const struct udevice *dev,
381 struct resource *res)
382{
383 const u32 *values;
384 int len;
385
386 values = dev_read_prop(dev, "bus-range", &len);
387 if (!values || len < sizeof(*values) * 2)
388 return -EINVAL;
389
390 res->start = *values++;
391 res->end = *values;
392
393 return 0;
394}
Dario Binacchi836cc9d2020-12-30 00:16:26 +0100395
396int dev_decode_display_timing(const struct udevice *dev, int index,
397 struct display_timing *config)
398{
399 return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
400}