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