blob: fc74d64814f8af62655084d56eef1448a9ce263c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass47a0fd32017-05-18 20:09:04 -06002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass47a0fd32017-05-18 20:09:04 -06005 */
6
7#include <common.h>
8#include <dm.h>
9#include <dm/of_access.h>
Stefan Roese979afd12020-04-29 09:08:44 +020010#include <mapmem.h>
11#include <asm/types.h>
12#include <asm/io.h>
Stefan Roese68f81b82020-08-05 13:56:11 +020013#include <linux/ioport.h>
Simon Glass47a0fd32017-05-18 20:09:04 -060014
Simon Glass88b3a372020-01-27 08:49:40 -070015int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp)
Masahiro Yamada3ab48f62017-12-30 02:00:05 +090016{
17 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
18}
19
Simon Glass88b3a372020-01-27 08:49:40 -070020int dev_read_u32_default(const struct udevice *dev, const char *propname,
21 int def)
Simon Glass47a0fd32017-05-18 20:09:04 -060022{
23 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
24}
25
Dario Binacchi4bb70752020-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 Glass88b3a372020-01-27 08:49:40 -070039int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
Simon Glassa1b17e42018-12-10 10:37:37 -070040{
41 return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
42}
43
Simon Glass88b3a372020-01-27 08:49:40 -070044int dev_read_s32_default(const struct udevice *dev, const char *propname,
45 int def)
Simon Glassa1b17e42018-12-10 10:37:37 -070046{
47 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
48}
49
Simon Glass88b3a372020-01-27 08:49:40 -070050int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp)
Simon Glassa1b17e42018-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 Glass88b3a372020-01-27 08:49:40 -070063int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp)
T Karthik Reddy3f3d7712019-09-02 16:34:30 +020064{
65 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
66}
67
Simon Glass88b3a372020-01-27 08:49:40 -070068u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
69 u64 def)
T Karthik Reddy3f3d7712019-09-02 16:34:30 +020070{
71 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
72}
73
Simon Glass88b3a372020-01-27 08:49:40 -070074const char *dev_read_string(const struct udevice *dev, const char *propname)
Simon Glass47a0fd32017-05-18 20:09:04 -060075{
76 return ofnode_read_string(dev_ofnode(dev), propname);
77}
78
Simon Glass88b3a372020-01-27 08:49:40 -070079bool dev_read_bool(const struct udevice *dev, const char *propname)
Simon Glass47a0fd32017-05-18 20:09:04 -060080{
81 return ofnode_read_bool(dev_ofnode(dev), propname);
82}
83
Simon Glass88b3a372020-01-27 08:49:40 -070084ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name)
Simon Glass47a0fd32017-05-18 20:09:04 -060085{
86 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
87}
88
Simon Glass88b3a372020-01-27 08:49:40 -070089ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glass47a0fd32017-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 Glass88b3a372020-01-27 08:49:40 -070099int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glass47a0fd32017-05-18 20:09:04 -0600100{
101 return ofnode_read_size(dev_ofnode(dev), propname);
102}
103
Simon Glass88b3a372020-01-27 08:49:40 -0700104fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index)
Simon Glass47a0fd32017-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 Glass88b3a372020-01-27 08:49:40 -0700112fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
Sekhar Norif5b90472019-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 Glass88b3a372020-01-27 08:49:40 -0700121void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas30a90f52018-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 Glass88b3a372020-01-27 08:49:40 -0700131fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojas79598822018-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 Glass88b3a372020-01-27 08:49:40 -0700141fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif5b90472019-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 Glass88b3a372020-01-27 08:49:40 -0700152void *dev_remap_addr_name(const struct udevice *dev, const char *name)
Álvaro Fernández Rojas79598822018-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 Glass88b3a372020-01-27 08:49:40 -0700162fdt_addr_t dev_read_addr(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600163{
164 return dev_read_addr_index(dev, 0);
165}
166
Simon Glass88b3a372020-01-27 08:49:40 -0700167void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200168{
169 fdt_addr_t addr = dev_read_addr(dev);
170
Sean Anderson082faeb2020-06-24 06:41:13 -0400171 return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr;
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200172}
173
Simon Glass88b3a372020-01-27 08:49:40 -0700174void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200175{
176 return dev_remap_addr_index(dev, 0);
177}
178
Simon Glass88b3a372020-01-27 08:49:40 -0700179fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *property,
Mario Six83a462a2018-01-15 11:07:18 +0100180 fdt_size_t *sizep)
Simon Glass47a0fd32017-05-18 20:09:04 -0600181{
182 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
183}
184
Simon Glass88b3a372020-01-27 08:49:40 -0700185const char *dev_read_name(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600186{
187 return ofnode_get_name(dev_ofnode(dev));
188}
189
Simon Glass88b3a372020-01-27 08:49:40 -0700190int dev_read_stringlist_search(const struct udevice *dev, const char *property,
Mario Six83a462a2018-01-15 11:07:18 +0100191 const char *string)
Simon Glass47a0fd32017-05-18 20:09:04 -0600192{
193 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
194}
195
Simon Glass88b3a372020-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 Hiblotb5a144a2017-09-21 17:03:09 +0200198{
199 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
200}
201
Simon Glass88b3a372020-01-27 08:49:40 -0700202int dev_read_string_count(const struct udevice *dev, const char *propname)
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200203{
204 return ofnode_read_string_count(dev_ofnode(dev), propname);
205}
206
Simon Glass88b3a372020-01-27 08:49:40 -0700207int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
Mario Six83a462a2018-01-15 11:07:18 +0100208 const char *cells_name, int cell_count,
209 int index, struct ofnode_phandle_args *out_args)
Simon Glass47a0fd32017-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 Glass88b3a372020-01-27 08:49:40 -0700216int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunay89f68302020-09-25 09:41:14 +0200217 const char *list_name, const char *cells_name,
218 int cell_count)
Patrice Chotardea8cd652017-11-29 09:06:10 +0100219{
220 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
Patrick Delaunay89f68302020-09-25 09:41:14 +0200221 cells_name, cell_count);
Patrice Chotardea8cd652017-11-29 09:06:10 +0100222}
223
Simon Glass88b3a372020-01-27 08:49:40 -0700224int dev_read_addr_cells(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600225{
226 return ofnode_read_addr_cells(dev_ofnode(dev));
227}
228
Simon Glass88b3a372020-01-27 08:49:40 -0700229int dev_read_size_cells(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600230{
231 return ofnode_read_size_cells(dev_ofnode(dev));
232}
233
Simon Glass88b3a372020-01-27 08:49:40 -0700234int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass878d68c2017-06-12 06:21:31 -0600235{
236 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
237}
238
Simon Glass88b3a372020-01-27 08:49:40 -0700239int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass878d68c2017-06-12 06:21:31 -0600240{
241 return ofnode_read_simple_size_cells(dev_ofnode(dev));
242}
243
Simon Glass88b3a372020-01-27 08:49:40 -0700244int dev_read_phandle(const struct udevice *dev)
Simon Glass47a0fd32017-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 Glass88b3a372020-01-27 08:49:40 -0700254const void *dev_read_prop(const struct udevice *dev, const char *propname,
255 int *lenp)
Simon Glass47a0fd32017-05-18 20:09:04 -0600256{
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900257 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glass47a0fd32017-05-18 20:09:04 -0600258}
259
Patrick Delaunayce891fca2020-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 Glass88b3a372020-01-27 08:49:40 -0700276int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glass47a0fd32017-05-18 20:09:04 -0600277{
278 ofnode node = dev_ofnode(dev);
279 const char *uc_name = dev->uclass->uc_drv->name;
Marcel Ziswiler45224e82019-05-20 02:44:57 +0200280 int ret = -ENOTSUPP;
Simon Glass47a0fd32017-05-18 20:09:04 -0600281
282 if (ofnode_is_np(node)) {
283 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
Simon Glass4153e3a2020-12-16 21:20:12 -0700284 if (ret >= 0) {
Simon Glass47a0fd32017-05-18 20:09:04 -0600285 *devnump = ret;
Simon Glass4153e3a2020-12-16 21:20:12 -0700286 ret = 0;
287 }
Simon Glass47a0fd32017-05-18 20:09:04 -0600288 } else {
Marcel Ziswiler45224e82019-05-20 02:44:57 +0200289#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass47a0fd32017-05-18 20:09:04 -0600290 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
291 ofnode_to_offset(node), devnump);
Marcel Ziswiler45224e82019-05-20 02:44:57 +0200292#endif
Simon Glass47a0fd32017-05-18 20:09:04 -0600293 }
294
295 return ret;
296}
297
Simon Glass88b3a372020-01-27 08:49:40 -0700298int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glass47a0fd32017-05-18 20:09:04 -0600299 u32 *out_values, size_t sz)
300{
301 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
302}
303
Simon Glass88b3a372020-01-27 08:49:40 -0700304const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
305 const char *propname, size_t sz)
Simon Glass47a0fd32017-05-18 20:09:04 -0600306{
307 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
308}
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600309
Simon Glass88b3a372020-01-27 08:49:40 -0700310int dev_read_enabled(const struct udevice *dev)
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600311{
312 ofnode node = dev_ofnode(dev);
313
314 if (ofnode_is_np(node))
315 return of_device_is_available(ofnode_to_np(node));
316 else
317 return fdtdec_get_is_enabled(gd->fdt_blob,
318 ofnode_to_offset(node));
319}
Simon Glassdcf98852017-07-25 08:29:55 -0600320
Simon Glass88b3a372020-01-27 08:49:40 -0700321int dev_read_resource(const struct udevice *dev, uint index,
322 struct resource *res)
Simon Glassdcf98852017-07-25 08:29:55 -0600323{
324 return ofnode_read_resource(dev_ofnode(dev), index, res);
325}
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900326
Simon Glass88b3a372020-01-27 08:49:40 -0700327int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900328 struct resource *res)
329{
330 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
331}
Mario Six147c6072018-01-15 11:07:19 +0100332
Simon Glass88b3a372020-01-27 08:49:40 -0700333u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
Mario Six147c6072018-01-15 11:07:19 +0100334{
335 return ofnode_translate_address(dev_ofnode(dev), in_addr);
336}
Michal Simek83e4c7e2019-01-31 16:30:59 +0100337
Simon Glass88b3a372020-01-27 08:49:40 -0700338u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
Fabien Dessenne641067f2019-05-31 15:11:30 +0200339{
340 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
341}
342
Michal Simek83e4c7e2019-01-31 16:30:59 +0100343int dev_read_alias_highest_id(const char *stem)
344{
345 if (of_live_active())
346 return of_alias_get_highest_id(stem);
347
348 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
349}
Simon Glass33c215a2019-09-15 12:08:58 -0600350
Simon Glass88b3a372020-01-27 08:49:40 -0700351fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass33c215a2019-09-15 12:08:58 -0600352{
353 ulong addr;
354
355 addr = dev_read_addr(dev);
356 if (addr == FDT_ADDR_T_NONE && !of_live_active())
357 addr = devfdt_get_addr_pci(dev);
358
359 return addr;
360}
Chunfeng Yun89b84b82020-05-02 11:35:09 +0200361
362int dev_get_child_count(const struct udevice *dev)
363{
364 return ofnode_get_child_count(dev_ofnode(dev));
365}
Stefan Roese68f81b82020-08-05 13:56:11 +0200366
367int dev_read_pci_bus_range(const struct udevice *dev,
368 struct resource *res)
369{
370 const u32 *values;
371 int len;
372
373 values = dev_read_prop(dev, "bus-range", &len);
374 if (!values || len < sizeof(*values) * 2)
375 return -EINVAL;
376
377 res->start = *values++;
378 res->end = *values;
379
380 return 0;
381}