xref: /openbmc/u-boot/drivers/core/read.c (revision dd4671cb)
1 /*
2  * Copyright (c) 2017 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <mapmem.h>
11 #include <dm/of_access.h>
12 
13 int dev_read_u32(struct udevice *dev, const char *propname, u32 *outp)
14 {
15 	return ofnode_read_u32(dev_ofnode(dev), propname, outp);
16 }
17 
18 int dev_read_u32_default(struct udevice *dev, const char *propname, int def)
19 {
20 	return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
21 }
22 
23 const char *dev_read_string(struct udevice *dev, const char *propname)
24 {
25 	return ofnode_read_string(dev_ofnode(dev), propname);
26 }
27 
28 bool dev_read_bool(struct udevice *dev, const char *propname)
29 {
30 	return ofnode_read_bool(dev_ofnode(dev), propname);
31 }
32 
33 ofnode dev_read_subnode(struct udevice *dev, const char *subnode_name)
34 {
35 	return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
36 }
37 
38 ofnode dev_read_first_subnode(struct udevice *dev)
39 {
40 	return ofnode_first_subnode(dev_ofnode(dev));
41 }
42 
43 ofnode dev_read_next_subnode(ofnode node)
44 {
45 	return ofnode_next_subnode(node);
46 }
47 
48 int dev_read_size(struct udevice *dev, const char *propname)
49 {
50 	return ofnode_read_size(dev_ofnode(dev), propname);
51 }
52 
53 fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
54 {
55 	if (ofnode_is_np(dev_ofnode(dev)))
56 		return ofnode_get_addr_index(dev_ofnode(dev), index);
57 	else
58 		return devfdt_get_addr_index(dev, index);
59 }
60 
61 fdt_addr_t dev_read_addr(struct udevice *dev)
62 {
63 	return dev_read_addr_index(dev, 0);
64 }
65 
66 void *dev_read_addr_ptr(struct udevice *dev)
67 {
68 	fdt_addr_t addr = dev_read_addr(dev);
69 
70 	return (addr == FDT_ADDR_T_NONE) ? NULL : map_sysmem(addr, 0);
71 }
72 
73 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *property,
74 			      fdt_size_t *sizep)
75 {
76 	return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
77 }
78 
79 const char *dev_read_name(struct udevice *dev)
80 {
81 	return ofnode_get_name(dev_ofnode(dev));
82 }
83 
84 int dev_read_stringlist_search(struct udevice *dev, const char *property,
85 			       const char *string)
86 {
87 	return ofnode_stringlist_search(dev_ofnode(dev), property, string);
88 }
89 
90 int dev_read_string_index(struct udevice *dev, const char *propname, int index,
91 			  const char **outp)
92 {
93 	return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
94 }
95 
96 int dev_read_string_count(struct udevice *dev, const char *propname)
97 {
98 	return ofnode_read_string_count(dev_ofnode(dev), propname);
99 }
100 
101 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
102 			       const char *cells_name, int cell_count,
103 			       int index, struct ofnode_phandle_args *out_args)
104 {
105 	return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
106 					      cells_name, cell_count, index,
107 					      out_args);
108 }
109 
110 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
111 				const char *cells_name)
112 {
113 	return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
114 					      cells_name);
115 }
116 
117 int dev_read_addr_cells(struct udevice *dev)
118 {
119 	return ofnode_read_addr_cells(dev_ofnode(dev));
120 }
121 
122 int dev_read_size_cells(struct udevice *dev)
123 {
124 	return ofnode_read_size_cells(dev_ofnode(dev));
125 }
126 
127 int dev_read_simple_addr_cells(struct udevice *dev)
128 {
129 	return ofnode_read_simple_addr_cells(dev_ofnode(dev));
130 }
131 
132 int dev_read_simple_size_cells(struct udevice *dev)
133 {
134 	return ofnode_read_simple_size_cells(dev_ofnode(dev));
135 }
136 
137 int dev_read_phandle(struct udevice *dev)
138 {
139 	ofnode node = dev_ofnode(dev);
140 
141 	if (ofnode_is_np(node))
142 		return ofnode_to_np(node)->phandle;
143 	else
144 		return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
145 }
146 
147 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp)
148 {
149 	return ofnode_get_property(dev_ofnode(dev), propname, lenp);
150 }
151 
152 int dev_read_alias_seq(struct udevice *dev, int *devnump)
153 {
154 	ofnode node = dev_ofnode(dev);
155 	const char *uc_name = dev->uclass->uc_drv->name;
156 	int ret;
157 
158 	if (ofnode_is_np(node)) {
159 		ret = of_alias_get_id(ofnode_to_np(node), uc_name);
160 		if (ret >= 0)
161 			*devnump = ret;
162 	} else {
163 		ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
164 					   ofnode_to_offset(node), devnump);
165 	}
166 
167 	return ret;
168 }
169 
170 int dev_read_u32_array(struct udevice *dev, const char *propname,
171 		       u32 *out_values, size_t sz)
172 {
173 	return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
174 }
175 
176 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
177 				     size_t sz)
178 {
179 	return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
180 }
181 
182 int dev_read_enabled(struct udevice *dev)
183 {
184 	ofnode node = dev_ofnode(dev);
185 
186 	if (ofnode_is_np(node))
187 		return of_device_is_available(ofnode_to_np(node));
188 	else
189 		return fdtdec_get_is_enabled(gd->fdt_blob,
190 					     ofnode_to_offset(node));
191 }
192 
193 int dev_read_resource(struct udevice *dev, uint index, struct resource *res)
194 {
195 	return ofnode_read_resource(dev_ofnode(dev), index, res);
196 }
197 
198 int dev_read_resource_byname(struct udevice *dev, const char *name,
199 			     struct resource *res)
200 {
201 	return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
202 }
203 
204 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
205 {
206 	return ofnode_translate_address(dev_ofnode(dev), in_addr);
207 }
208