blob: 96766c7876a57eda94086d6d575ae876d03c0c87 [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
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +02007#include <asm/types.h>
8#include <asm/io.h>
Simon Glass47a0fd32017-05-18 20:09:04 -06009#include <common.h>
10#include <dm.h>
Simon Glass8ccc2db2017-09-28 06:35:15 -060011#include <mapmem.h>
Simon Glass47a0fd32017-05-18 20:09:04 -060012#include <dm/of_access.h>
13
Masahiro Yamada3ab48f62017-12-30 02:00:05 +090014int dev_read_u32(struct udevice *dev, const char *propname, u32 *outp)
15{
16 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
17}
18
Simon Glass47a0fd32017-05-18 20:09:04 -060019int dev_read_u32_default(struct udevice *dev, const char *propname, int def)
20{
21 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
22}
23
24const char *dev_read_string(struct udevice *dev, const char *propname)
25{
26 return ofnode_read_string(dev_ofnode(dev), propname);
27}
28
29bool dev_read_bool(struct udevice *dev, const char *propname)
30{
31 return ofnode_read_bool(dev_ofnode(dev), propname);
32}
33
34ofnode dev_read_subnode(struct udevice *dev, const char *subnode_name)
35{
36 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
37}
38
39ofnode dev_read_first_subnode(struct udevice *dev)
40{
41 return ofnode_first_subnode(dev_ofnode(dev));
42}
43
44ofnode dev_read_next_subnode(ofnode node)
45{
46 return ofnode_next_subnode(node);
47}
48
49int dev_read_size(struct udevice *dev, const char *propname)
50{
51 return ofnode_read_size(dev_ofnode(dev), propname);
52}
53
54fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
55{
56 if (ofnode_is_np(dev_ofnode(dev)))
57 return ofnode_get_addr_index(dev_ofnode(dev), index);
58 else
59 return devfdt_get_addr_index(dev, index);
60}
61
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +020062void *dev_remap_addr_index(struct udevice *dev, int index)
63{
64 fdt_addr_t addr = dev_read_addr_index(dev, index);
65
66 if (addr == FDT_ADDR_T_NONE)
67 return NULL;
68
69 return map_physmem(addr, 0, MAP_NOCACHE);
70}
71
Simon Glass47a0fd32017-05-18 20:09:04 -060072fdt_addr_t dev_read_addr(struct udevice *dev)
73{
74 return dev_read_addr_index(dev, 0);
75}
76
Philipp Tomsichc131c8b2017-09-11 22:04:12 +020077void *dev_read_addr_ptr(struct udevice *dev)
78{
79 fdt_addr_t addr = dev_read_addr(dev);
80
Simon Glass8ccc2db2017-09-28 06:35:15 -060081 return (addr == FDT_ADDR_T_NONE) ? NULL : map_sysmem(addr, 0);
Philipp Tomsichc131c8b2017-09-11 22:04:12 +020082}
83
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +020084void *dev_remap_addr(struct udevice *dev)
85{
86 return dev_remap_addr_index(dev, 0);
87}
88
Simon Glass47a0fd32017-05-18 20:09:04 -060089fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *property,
Mario Six83a462a2018-01-15 11:07:18 +010090 fdt_size_t *sizep)
Simon Glass47a0fd32017-05-18 20:09:04 -060091{
92 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
93}
94
95const char *dev_read_name(struct udevice *dev)
96{
97 return ofnode_get_name(dev_ofnode(dev));
98}
99
100int dev_read_stringlist_search(struct udevice *dev, const char *property,
Mario Six83a462a2018-01-15 11:07:18 +0100101 const char *string)
Simon Glass47a0fd32017-05-18 20:09:04 -0600102{
103 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
104}
105
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200106int dev_read_string_index(struct udevice *dev, const char *propname, int index,
107 const char **outp)
108{
109 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
110}
111
112int dev_read_string_count(struct udevice *dev, const char *propname)
113{
114 return ofnode_read_string_count(dev_ofnode(dev), propname);
115}
116
Simon Glass47a0fd32017-05-18 20:09:04 -0600117int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
Mario Six83a462a2018-01-15 11:07:18 +0100118 const char *cells_name, int cell_count,
119 int index, struct ofnode_phandle_args *out_args)
Simon Glass47a0fd32017-05-18 20:09:04 -0600120{
121 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
122 cells_name, cell_count, index,
123 out_args);
124}
125
Patrice Chotardea8cd652017-11-29 09:06:10 +0100126int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
127 const char *cells_name)
128{
129 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
130 cells_name);
131}
132
Simon Glass47a0fd32017-05-18 20:09:04 -0600133int dev_read_addr_cells(struct udevice *dev)
134{
135 return ofnode_read_addr_cells(dev_ofnode(dev));
136}
137
138int dev_read_size_cells(struct udevice *dev)
139{
140 return ofnode_read_size_cells(dev_ofnode(dev));
141}
142
Simon Glass878d68c2017-06-12 06:21:31 -0600143int dev_read_simple_addr_cells(struct udevice *dev)
144{
145 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
146}
147
148int dev_read_simple_size_cells(struct udevice *dev)
149{
150 return ofnode_read_simple_size_cells(dev_ofnode(dev));
151}
152
Simon Glass47a0fd32017-05-18 20:09:04 -0600153int dev_read_phandle(struct udevice *dev)
154{
155 ofnode node = dev_ofnode(dev);
156
157 if (ofnode_is_np(node))
158 return ofnode_to_np(node)->phandle;
159 else
160 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
161}
162
Masahiro Yamadafd736212017-07-17 12:18:39 +0900163const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp)
Simon Glass47a0fd32017-05-18 20:09:04 -0600164{
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900165 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glass47a0fd32017-05-18 20:09:04 -0600166}
167
168int dev_read_alias_seq(struct udevice *dev, int *devnump)
169{
170 ofnode node = dev_ofnode(dev);
171 const char *uc_name = dev->uclass->uc_drv->name;
172 int ret;
173
174 if (ofnode_is_np(node)) {
175 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
176 if (ret >= 0)
177 *devnump = ret;
178 } else {
179 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
180 ofnode_to_offset(node), devnump);
181 }
182
183 return ret;
184}
185
186int dev_read_u32_array(struct udevice *dev, const char *propname,
187 u32 *out_values, size_t sz)
188{
189 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
190}
191
192const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
193 size_t sz)
194{
195 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
196}
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600197
198int dev_read_enabled(struct udevice *dev)
199{
200 ofnode node = dev_ofnode(dev);
201
202 if (ofnode_is_np(node))
203 return of_device_is_available(ofnode_to_np(node));
204 else
205 return fdtdec_get_is_enabled(gd->fdt_blob,
206 ofnode_to_offset(node));
207}
Simon Glassdcf98852017-07-25 08:29:55 -0600208
209int dev_read_resource(struct udevice *dev, uint index, struct resource *res)
210{
211 return ofnode_read_resource(dev_ofnode(dev), index, res);
212}
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900213
214int dev_read_resource_byname(struct udevice *dev, const char *name,
215 struct resource *res)
216{
217 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
218}
Mario Six147c6072018-01-15 11:07:19 +0100219
220u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
221{
222 return ofnode_translate_address(dev_ofnode(dev), in_addr);
223}