xref: /openbmc/u-boot/drivers/core/read.c (revision 57efeb04)
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 
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 
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 
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 
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 
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 
47 const char *dev_read_string(struct udevice *dev, const char *propname)
48 {
49 	return ofnode_read_string(dev_ofnode(dev), propname);
50 }
51 
52 bool dev_read_bool(struct udevice *dev, const char *propname)
53 {
54 	return ofnode_read_bool(dev_ofnode(dev), propname);
55 }
56 
57 ofnode dev_read_subnode(struct udevice *dev, const char *subnode_name)
58 {
59 	return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
60 }
61 
62 ofnode dev_read_first_subnode(struct udevice *dev)
63 {
64 	return ofnode_first_subnode(dev_ofnode(dev));
65 }
66 
67 ofnode dev_read_next_subnode(ofnode node)
68 {
69 	return ofnode_next_subnode(node);
70 }
71 
72 int dev_read_size(struct udevice *dev, const char *propname)
73 {
74 	return ofnode_read_size(dev_ofnode(dev), propname);
75 }
76 
77 fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
78 {
79 	if (ofnode_is_np(dev_ofnode(dev)))
80 		return ofnode_get_addr_index(dev_ofnode(dev), index);
81 	else
82 		return devfdt_get_addr_index(dev, index);
83 }
84 
85 void *dev_remap_addr_index(struct udevice *dev, int index)
86 {
87 	fdt_addr_t addr = dev_read_addr_index(dev, index);
88 
89 	if (addr == FDT_ADDR_T_NONE)
90 		return NULL;
91 
92 	return map_physmem(addr, 0, MAP_NOCACHE);
93 }
94 
95 fdt_addr_t dev_read_addr_name(struct udevice *dev, const char *name)
96 {
97 	int index = dev_read_stringlist_search(dev, "reg-names", name);
98 
99 	if (index < 0)
100 		return FDT_ADDR_T_NONE;
101 	else
102 		return dev_read_addr_index(dev, index);
103 }
104 
105 void *dev_remap_addr_name(struct udevice *dev, const char *name)
106 {
107 	fdt_addr_t addr = dev_read_addr_name(dev, name);
108 
109 	if (addr == FDT_ADDR_T_NONE)
110 		return NULL;
111 
112 	return map_physmem(addr, 0, MAP_NOCACHE);
113 }
114 
115 fdt_addr_t dev_read_addr(struct udevice *dev)
116 {
117 	return dev_read_addr_index(dev, 0);
118 }
119 
120 void *dev_read_addr_ptr(struct udevice *dev)
121 {
122 	fdt_addr_t addr = dev_read_addr(dev);
123 
124 	return (addr == FDT_ADDR_T_NONE) ? NULL : map_sysmem(addr, 0);
125 }
126 
127 void *dev_remap_addr(struct udevice *dev)
128 {
129 	return dev_remap_addr_index(dev, 0);
130 }
131 
132 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *property,
133 			      fdt_size_t *sizep)
134 {
135 	return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
136 }
137 
138 const char *dev_read_name(struct udevice *dev)
139 {
140 	return ofnode_get_name(dev_ofnode(dev));
141 }
142 
143 int dev_read_stringlist_search(struct udevice *dev, const char *property,
144 			       const char *string)
145 {
146 	return ofnode_stringlist_search(dev_ofnode(dev), property, string);
147 }
148 
149 int dev_read_string_index(struct udevice *dev, const char *propname, int index,
150 			  const char **outp)
151 {
152 	return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
153 }
154 
155 int dev_read_string_count(struct udevice *dev, const char *propname)
156 {
157 	return ofnode_read_string_count(dev_ofnode(dev), propname);
158 }
159 
160 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
161 			       const char *cells_name, int cell_count,
162 			       int index, struct ofnode_phandle_args *out_args)
163 {
164 	return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
165 					      cells_name, cell_count, index,
166 					      out_args);
167 }
168 
169 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
170 				const char *cells_name)
171 {
172 	return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
173 					      cells_name);
174 }
175 
176 int dev_read_addr_cells(struct udevice *dev)
177 {
178 	return ofnode_read_addr_cells(dev_ofnode(dev));
179 }
180 
181 int dev_read_size_cells(struct udevice *dev)
182 {
183 	return ofnode_read_size_cells(dev_ofnode(dev));
184 }
185 
186 int dev_read_simple_addr_cells(struct udevice *dev)
187 {
188 	return ofnode_read_simple_addr_cells(dev_ofnode(dev));
189 }
190 
191 int dev_read_simple_size_cells(struct udevice *dev)
192 {
193 	return ofnode_read_simple_size_cells(dev_ofnode(dev));
194 }
195 
196 int dev_read_phandle(struct udevice *dev)
197 {
198 	ofnode node = dev_ofnode(dev);
199 
200 	if (ofnode_is_np(node))
201 		return ofnode_to_np(node)->phandle;
202 	else
203 		return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
204 }
205 
206 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp)
207 {
208 	return ofnode_get_property(dev_ofnode(dev), propname, lenp);
209 }
210 
211 int dev_read_alias_seq(struct udevice *dev, int *devnump)
212 {
213 	ofnode node = dev_ofnode(dev);
214 	const char *uc_name = dev->uclass->uc_drv->name;
215 	int ret;
216 
217 	if (ofnode_is_np(node)) {
218 		ret = of_alias_get_id(ofnode_to_np(node), uc_name);
219 		if (ret >= 0)
220 			*devnump = ret;
221 	} else {
222 		ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
223 					   ofnode_to_offset(node), devnump);
224 	}
225 
226 	return ret;
227 }
228 
229 int dev_read_u32_array(struct udevice *dev, const char *propname,
230 		       u32 *out_values, size_t sz)
231 {
232 	return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
233 }
234 
235 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
236 				     size_t sz)
237 {
238 	return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
239 }
240 
241 int dev_read_enabled(struct udevice *dev)
242 {
243 	ofnode node = dev_ofnode(dev);
244 
245 	if (ofnode_is_np(node))
246 		return of_device_is_available(ofnode_to_np(node));
247 	else
248 		return fdtdec_get_is_enabled(gd->fdt_blob,
249 					     ofnode_to_offset(node));
250 }
251 
252 int dev_read_resource(struct udevice *dev, uint index, struct resource *res)
253 {
254 	return ofnode_read_resource(dev_ofnode(dev), index, res);
255 }
256 
257 int dev_read_resource_byname(struct udevice *dev, const char *name,
258 			     struct resource *res)
259 {
260 	return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
261 }
262 
263 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
264 {
265 	return ofnode_translate_address(dev_ofnode(dev), in_addr);
266 }
267 
268 int dev_read_alias_highest_id(const char *stem)
269 {
270 	if (of_live_active())
271 		return of_alias_get_highest_id(stem);
272 
273 	return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
274 }
275