1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <dm/of_access.h>
10 #include <mapmem.h>
11 #include <asm/types.h>
12 #include <asm/io.h>
13
dev_read_u32(const struct udevice * dev,const char * propname,u32 * outp)14 int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp)
15 {
16 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
17 }
18
dev_read_u32_default(const struct udevice * dev,const char * propname,int def)19 int dev_read_u32_default(const struct udevice *dev, const char *propname,
20 int def)
21 {
22 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
23 }
24
dev_read_u32_index(struct udevice * dev,const char * propname,int index,u32 * outp)25 int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
26 u32 *outp)
27 {
28 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
29 }
30
dev_read_u32_index_default(struct udevice * dev,const char * propname,int index,u32 def)31 u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
32 int index, u32 def)
33 {
34 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
35 def);
36 }
37
dev_read_s32(const struct udevice * dev,const char * propname,s32 * outp)38 int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
39 {
40 return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
41 }
42
dev_read_s32_default(const struct udevice * dev,const char * propname,int def)43 int dev_read_s32_default(const struct udevice *dev, const char *propname,
44 int def)
45 {
46 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
47 }
48
dev_read_u32u(const struct udevice * dev,const char * propname,uint * outp)49 int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp)
50 {
51 u32 val;
52 int ret;
53
54 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
55 if (ret)
56 return ret;
57 *outp = val;
58
59 return 0;
60 }
61
dev_read_u64(const struct udevice * dev,const char * propname,u64 * outp)62 int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp)
63 {
64 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
65 }
66
dev_read_u64_default(const struct udevice * dev,const char * propname,u64 def)67 u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
68 u64 def)
69 {
70 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
71 }
72
dev_read_string(const struct udevice * dev,const char * propname)73 const char *dev_read_string(const struct udevice *dev, const char *propname)
74 {
75 return ofnode_read_string(dev_ofnode(dev), propname);
76 }
77
dev_read_bool(const struct udevice * dev,const char * propname)78 bool dev_read_bool(const struct udevice *dev, const char *propname)
79 {
80 return ofnode_read_bool(dev_ofnode(dev), propname);
81 }
82
dev_read_subnode(const struct udevice * dev,const char * subnode_name)83 ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name)
84 {
85 return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
86 }
87
dev_read_first_subnode(const struct udevice * dev)88 ofnode dev_read_first_subnode(const struct udevice *dev)
89 {
90 return ofnode_first_subnode(dev_ofnode(dev));
91 }
92
dev_read_next_subnode(ofnode node)93 ofnode dev_read_next_subnode(ofnode node)
94 {
95 return ofnode_next_subnode(node);
96 }
97
dev_read_size(const struct udevice * dev,const char * propname)98 int dev_read_size(const struct udevice *dev, const char *propname)
99 {
100 return ofnode_read_size(dev_ofnode(dev), propname);
101 }
102
dev_read_addr_index(const struct udevice * dev,int index)103 fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index)
104 {
105 if (ofnode_is_np(dev_ofnode(dev)))
106 return ofnode_get_addr_index(dev_ofnode(dev), index);
107 else
108 return devfdt_get_addr_index(dev, index);
109 }
110
dev_read_addr_size_index(const struct udevice * dev,int index,fdt_size_t * size)111 fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
112 fdt_size_t *size)
113 {
114 if (ofnode_is_np(dev_ofnode(dev)))
115 return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
116 else
117 return devfdt_get_addr_size_index(dev, index, size);
118 }
119
dev_remap_addr_index(const struct udevice * dev,int index)120 void *dev_remap_addr_index(const struct udevice *dev, int index)
121 {
122 fdt_addr_t addr = dev_read_addr_index(dev, index);
123
124 if (addr == FDT_ADDR_T_NONE)
125 return NULL;
126
127 return map_physmem(addr, 0, MAP_NOCACHE);
128 }
129
dev_read_addr_name(const struct udevice * dev,const char * name)130 fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name)
131 {
132 int index = dev_read_stringlist_search(dev, "reg-names", name);
133
134 if (index < 0)
135 return FDT_ADDR_T_NONE;
136 else
137 return dev_read_addr_index(dev, index);
138 }
139
dev_read_addr_size_name(const struct udevice * dev,const char * name,fdt_size_t * size)140 fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
141 fdt_size_t *size)
142 {
143 int index = dev_read_stringlist_search(dev, "reg-names", name);
144
145 if (index < 0)
146 return FDT_ADDR_T_NONE;
147 else
148 return dev_read_addr_size_index(dev, index, size);
149 }
150
dev_remap_addr_name(const struct udevice * dev,const char * name)151 void *dev_remap_addr_name(const struct udevice *dev, const char *name)
152 {
153 fdt_addr_t addr = dev_read_addr_name(dev, name);
154
155 if (addr == FDT_ADDR_T_NONE)
156 return NULL;
157
158 return map_physmem(addr, 0, MAP_NOCACHE);
159 }
160
dev_read_addr(const struct udevice * dev)161 fdt_addr_t dev_read_addr(const struct udevice *dev)
162 {
163 return dev_read_addr_index(dev, 0);
164 }
165
dev_read_addr_ptr(const struct udevice * dev)166 void *dev_read_addr_ptr(const struct udevice *dev)
167 {
168 fdt_addr_t addr = dev_read_addr(dev);
169
170 return (addr == FDT_ADDR_T_NONE) ? NULL : map_sysmem(addr, 0);
171 }
172
dev_remap_addr(const struct udevice * dev)173 void *dev_remap_addr(const struct udevice *dev)
174 {
175 return dev_remap_addr_index(dev, 0);
176 }
177
dev_read_addr_size(const struct udevice * dev,const char * property,fdt_size_t * sizep)178 fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *property,
179 fdt_size_t *sizep)
180 {
181 return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
182 }
183
dev_read_name(const struct udevice * dev)184 const char *dev_read_name(const struct udevice *dev)
185 {
186 return ofnode_get_name(dev_ofnode(dev));
187 }
188
dev_read_stringlist_search(const struct udevice * dev,const char * property,const char * string)189 int dev_read_stringlist_search(const struct udevice *dev, const char *property,
190 const char *string)
191 {
192 return ofnode_stringlist_search(dev_ofnode(dev), property, string);
193 }
194
dev_read_string_index(const struct udevice * dev,const char * propname,int index,const char ** outp)195 int dev_read_string_index(const struct udevice *dev, const char *propname,
196 int index, const char **outp)
197 {
198 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
199 }
200
dev_read_string_count(const struct udevice * dev,const char * propname)201 int dev_read_string_count(const struct udevice *dev, const char *propname)
202 {
203 return ofnode_read_string_count(dev_ofnode(dev), propname);
204 }
205
dev_read_phandle_with_args(const struct udevice * dev,const char * list_name,const char * cells_name,int cell_count,int index,struct ofnode_phandle_args * out_args)206 int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
207 const char *cells_name, int cell_count,
208 int index, struct ofnode_phandle_args *out_args)
209 {
210 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
211 cells_name, cell_count, index,
212 out_args);
213 }
214
dev_count_phandle_with_args(const struct udevice * dev,const char * list_name,const char * cells_name)215 int dev_count_phandle_with_args(const struct udevice *dev,
216 const char *list_name, const char *cells_name)
217 {
218 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
219 cells_name);
220 }
221
dev_read_addr_cells(const struct udevice * dev)222 int dev_read_addr_cells(const struct udevice *dev)
223 {
224 return ofnode_read_addr_cells(dev_ofnode(dev));
225 }
226
dev_read_size_cells(const struct udevice * dev)227 int dev_read_size_cells(const struct udevice *dev)
228 {
229 return ofnode_read_size_cells(dev_ofnode(dev));
230 }
231
dev_read_simple_addr_cells(const struct udevice * dev)232 int dev_read_simple_addr_cells(const struct udevice *dev)
233 {
234 return ofnode_read_simple_addr_cells(dev_ofnode(dev));
235 }
236
dev_read_simple_size_cells(const struct udevice * dev)237 int dev_read_simple_size_cells(const struct udevice *dev)
238 {
239 return ofnode_read_simple_size_cells(dev_ofnode(dev));
240 }
241
dev_read_phandle(const struct udevice * dev)242 int dev_read_phandle(const struct udevice *dev)
243 {
244 ofnode node = dev_ofnode(dev);
245
246 if (ofnode_is_np(node))
247 return ofnode_to_np(node)->phandle;
248 else
249 return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
250 }
251
dev_read_prop(const struct udevice * dev,const char * propname,int * lenp)252 const void *dev_read_prop(const struct udevice *dev, const char *propname,
253 int *lenp)
254 {
255 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
256 }
257
dev_read_first_prop(const struct udevice * dev,struct ofprop * prop)258 int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
259 {
260 return ofnode_get_first_property(dev_ofnode(dev), prop);
261 }
262
dev_read_next_prop(struct ofprop * prop)263 int dev_read_next_prop(struct ofprop *prop)
264 {
265 return ofnode_get_next_property(prop);
266 }
267
dev_read_prop_by_prop(struct ofprop * prop,const char ** propname,int * lenp)268 const void *dev_read_prop_by_prop(struct ofprop *prop,
269 const char **propname, int *lenp)
270 {
271 return ofnode_get_property_by_prop(prop, propname, lenp);
272 }
273
dev_read_alias_seq(const struct udevice * dev,int * devnump)274 int dev_read_alias_seq(const struct udevice *dev, int *devnump)
275 {
276 ofnode node = dev_ofnode(dev);
277 const char *uc_name = dev->uclass->uc_drv->name;
278 int ret;
279
280 if (ofnode_is_np(node)) {
281 ret = of_alias_get_id(ofnode_to_np(node), uc_name);
282 if (ret >= 0)
283 *devnump = ret;
284 } else {
285 ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
286 ofnode_to_offset(node), devnump);
287 }
288
289 return ret;
290 }
291
dev_read_u32_array(const struct udevice * dev,const char * propname,u32 * out_values,size_t sz)292 int dev_read_u32_array(const struct udevice *dev, const char *propname,
293 u32 *out_values, size_t sz)
294 {
295 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
296 }
297
dev_read_u8_array_ptr(const struct udevice * dev,const char * propname,size_t sz)298 const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
299 const char *propname, size_t sz)
300 {
301 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
302 }
303
dev_read_enabled(const struct udevice * dev)304 int dev_read_enabled(const struct udevice *dev)
305 {
306 ofnode node = dev_ofnode(dev);
307
308 if (ofnode_is_np(node))
309 return of_device_is_available(ofnode_to_np(node));
310 else
311 return fdtdec_get_is_enabled(gd->fdt_blob,
312 ofnode_to_offset(node));
313 }
314
dev_read_resource(const struct udevice * dev,uint index,struct resource * res)315 int dev_read_resource(const struct udevice *dev, uint index,
316 struct resource *res)
317 {
318 return ofnode_read_resource(dev_ofnode(dev), index, res);
319 }
320
dev_read_resource_byname(const struct udevice * dev,const char * name,struct resource * res)321 int dev_read_resource_byname(const struct udevice *dev, const char *name,
322 struct resource *res)
323 {
324 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
325 }
326
dev_translate_address(const struct udevice * dev,const fdt32_t * in_addr)327 u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
328 {
329 return ofnode_translate_address(dev_ofnode(dev), in_addr);
330 }
331
dev_translate_dma_address(const struct udevice * dev,const fdt32_t * in_addr)332 u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
333 {
334 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
335 }
336
dev_read_alias_highest_id(const char * stem)337 int dev_read_alias_highest_id(const char *stem)
338 {
339 if (of_live_active())
340 return of_alias_get_highest_id(stem);
341
342 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
343 }
344
dev_read_addr_pci(const struct udevice * dev)345 fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
346 {
347 ulong addr;
348
349 addr = dev_read_addr(dev);
350 if (addr == FDT_ADDR_T_NONE && !of_live_active())
351 addr = devfdt_get_addr_pci(dev);
352
353 return addr;
354 }
355
dev_get_child_count(const struct udevice * dev)356 int dev_get_child_count(const struct udevice *dev)
357 {
358 return ofnode_get_child_count(dev_ofnode(dev));
359 }
360