]>
Commit | Line | Data |
---|---|---|
b5220bc6 SG |
1 | /* |
2 | * Copyright (c) 2011 The Chromium OS Authors. | |
3 | * See file CREDITS for list of people who contributed to this | |
4 | * project. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License as | |
8 | * published by the Free Software Foundation; either version 2 of | |
9 | * the License, or (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
19 | * MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | #include <common.h> | |
23 | #include <serial.h> | |
24 | #include <libfdt.h> | |
25 | #include <fdtdec.h> | |
26 | ||
ed3ee5cd SG |
27 | /* we need the generic GPIO interface here */ |
28 | #include <asm-generic/gpio.h> | |
29 | ||
b5220bc6 SG |
30 | DECLARE_GLOBAL_DATA_PTR; |
31 | ||
32 | /* | |
33 | * Here are the type we know about. One day we might allow drivers to | |
34 | * register. For now we just put them here. The COMPAT macro allows us to | |
35 | * turn this into a sparse list later, and keeps the ID with the name. | |
36 | */ | |
37 | #define COMPAT(id, name) name | |
38 | static const char * const compat_names[COMPAT_COUNT] = { | |
f88fe2de | 39 | COMPAT(UNKNOWN, "<none>"), |
b5220bc6 SG |
40 | }; |
41 | ||
a53f4a29 SG |
42 | const char *fdtdec_get_compatible(enum fdt_compat_id id) |
43 | { | |
44 | /* We allow reading of the 'unknown' ID for testing purposes */ | |
45 | assert(id >= 0 && id < COMPAT_COUNT); | |
46 | return compat_names[id]; | |
47 | } | |
48 | ||
b5220bc6 SG |
49 | /** |
50 | * Look in the FDT for an alias with the given name and return its node. | |
51 | * | |
52 | * @param blob FDT blob | |
53 | * @param name alias name to look up | |
54 | * @return node offset if found, or an error code < 0 otherwise | |
55 | */ | |
56 | static int find_alias_node(const void *blob, const char *name) | |
57 | { | |
58 | const char *path; | |
59 | int alias_node; | |
60 | ||
61 | debug("find_alias_node: %s\n", name); | |
62 | alias_node = fdt_path_offset(blob, "/aliases"); | |
63 | if (alias_node < 0) | |
64 | return alias_node; | |
65 | path = fdt_getprop(blob, alias_node, name, NULL); | |
66 | if (!path) | |
67 | return -FDT_ERR_NOTFOUND; | |
68 | return fdt_path_offset(blob, path); | |
69 | } | |
70 | ||
71 | fdt_addr_t fdtdec_get_addr(const void *blob, int node, | |
72 | const char *prop_name) | |
73 | { | |
74 | const fdt_addr_t *cell; | |
75 | int len; | |
76 | ||
77 | debug("get_addr: %s\n", prop_name); | |
78 | cell = fdt_getprop(blob, node, prop_name, &len); | |
79 | if (cell && (len == sizeof(fdt_addr_t) || | |
80 | len == sizeof(fdt_addr_t) * 2)) | |
81 | return fdt_addr_to_cpu(*cell); | |
82 | return FDT_ADDR_T_NONE; | |
83 | } | |
84 | ||
85 | s32 fdtdec_get_int(const void *blob, int node, const char *prop_name, | |
86 | s32 default_val) | |
87 | { | |
88 | const s32 *cell; | |
89 | int len; | |
90 | ||
91 | debug("get_size: %s\n", prop_name); | |
92 | cell = fdt_getprop(blob, node, prop_name, &len); | |
93 | if (cell && len >= sizeof(s32)) | |
94 | return fdt32_to_cpu(cell[0]); | |
95 | return default_val; | |
96 | } | |
97 | ||
f88fe2de | 98 | int fdtdec_get_is_enabled(const void *blob, int node) |
b5220bc6 SG |
99 | { |
100 | const char *cell; | |
101 | ||
f88fe2de SG |
102 | /* |
103 | * It should say "okay", so only allow that. Some fdts use "ok" but | |
104 | * this is a bug. Please fix your device tree source file. See here | |
105 | * for discussion: | |
106 | * | |
107 | * http://www.mail-archive.com/[email protected]/msg71598.html | |
108 | */ | |
b5220bc6 SG |
109 | cell = fdt_getprop(blob, node, "status", NULL); |
110 | if (cell) | |
f88fe2de SG |
111 | return 0 == strcmp(cell, "okay"); |
112 | return 1; | |
b5220bc6 SG |
113 | } |
114 | ||
115 | enum fdt_compat_id fd_dec_lookup(const void *blob, int node) | |
116 | { | |
117 | enum fdt_compat_id id; | |
118 | ||
119 | /* Search our drivers */ | |
120 | for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++) | |
121 | if (0 == fdt_node_check_compatible(blob, node, | |
122 | compat_names[id])) | |
123 | return id; | |
124 | return COMPAT_UNKNOWN; | |
125 | } | |
126 | ||
127 | int fdtdec_next_compatible(const void *blob, int node, | |
128 | enum fdt_compat_id id) | |
129 | { | |
130 | return fdt_node_offset_by_compatible(blob, node, compat_names[id]); | |
131 | } | |
132 | ||
133 | int fdtdec_next_alias(const void *blob, const char *name, | |
134 | enum fdt_compat_id id, int *upto) | |
135 | { | |
136 | #define MAX_STR_LEN 20 | |
137 | char str[MAX_STR_LEN + 20]; | |
138 | int node, err; | |
139 | ||
140 | /* snprintf() is not available */ | |
141 | assert(strlen(name) < MAX_STR_LEN); | |
142 | sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto); | |
b5220bc6 SG |
143 | node = find_alias_node(blob, str); |
144 | if (node < 0) | |
145 | return node; | |
146 | err = fdt_node_check_compatible(blob, node, compat_names[id]); | |
147 | if (err < 0) | |
148 | return err; | |
f88fe2de SG |
149 | if (err) |
150 | return -FDT_ERR_NOTFOUND; | |
151 | (*upto)++; | |
152 | return node; | |
b5220bc6 SG |
153 | } |
154 | ||
a53f4a29 SG |
155 | /* TODO: Can we tighten this code up a little? */ |
156 | int fdtdec_find_aliases_for_id(const void *blob, const char *name, | |
157 | enum fdt_compat_id id, int *node_list, int maxcount) | |
158 | { | |
159 | int name_len = strlen(name); | |
160 | int nodes[maxcount]; | |
161 | int num_found = 0; | |
162 | int offset, node; | |
163 | int alias_node; | |
164 | int count; | |
165 | int i, j; | |
166 | ||
167 | /* find the alias node if present */ | |
168 | alias_node = fdt_path_offset(blob, "/aliases"); | |
169 | ||
170 | /* | |
171 | * start with nothing, and we can assume that the root node can't | |
172 | * match | |
173 | */ | |
174 | memset(nodes, '\0', sizeof(nodes)); | |
175 | ||
176 | /* First find all the compatible nodes */ | |
177 | for (node = count = 0; node >= 0 && count < maxcount;) { | |
178 | node = fdtdec_next_compatible(blob, node, id); | |
179 | if (node >= 0) | |
180 | nodes[count++] = node; | |
181 | } | |
182 | if (node >= 0) | |
183 | debug("%s: warning: maxcount exceeded with alias '%s'\n", | |
184 | __func__, name); | |
185 | ||
186 | /* Now find all the aliases */ | |
187 | memset(node_list, '\0', sizeof(*node_list) * maxcount); | |
188 | ||
189 | for (offset = fdt_first_property_offset(blob, alias_node); | |
190 | offset > 0; | |
191 | offset = fdt_next_property_offset(blob, offset)) { | |
192 | const struct fdt_property *prop; | |
193 | const char *path; | |
194 | int number; | |
195 | int found; | |
196 | ||
197 | node = 0; | |
198 | prop = fdt_get_property_by_offset(blob, offset, NULL); | |
199 | path = fdt_string(blob, fdt32_to_cpu(prop->nameoff)); | |
200 | if (prop->len && 0 == strncmp(path, name, name_len)) | |
201 | node = fdt_path_offset(blob, prop->data); | |
202 | if (node <= 0) | |
203 | continue; | |
204 | ||
205 | /* Get the alias number */ | |
206 | number = simple_strtoul(path + name_len, NULL, 10); | |
207 | if (number < 0 || number >= maxcount) { | |
208 | debug("%s: warning: alias '%s' is out of range\n", | |
209 | __func__, path); | |
210 | continue; | |
211 | } | |
212 | ||
213 | /* Make sure the node we found is actually in our list! */ | |
214 | found = -1; | |
215 | for (j = 0; j < count; j++) | |
216 | if (nodes[j] == node) { | |
217 | found = j; | |
218 | break; | |
219 | } | |
220 | ||
221 | if (found == -1) { | |
222 | debug("%s: warning: alias '%s' points to a node " | |
223 | "'%s' that is missing or is not compatible " | |
224 | " with '%s'\n", __func__, path, | |
225 | fdt_get_name(blob, node, NULL), | |
226 | compat_names[id]); | |
227 | continue; | |
228 | } | |
229 | ||
230 | /* | |
231 | * Add this node to our list in the right place, and mark | |
232 | * it as done. | |
233 | */ | |
234 | if (fdtdec_get_is_enabled(blob, node)) { | |
235 | node_list[number] = node; | |
236 | if (number >= num_found) | |
237 | num_found = number + 1; | |
238 | } | |
239 | nodes[j] = 0; | |
240 | } | |
241 | ||
242 | /* Add any nodes not mentioned by an alias */ | |
243 | for (i = j = 0; i < maxcount; i++) { | |
244 | if (!node_list[i]) { | |
245 | for (; j < maxcount; j++) | |
246 | if (nodes[j] && | |
247 | fdtdec_get_is_enabled(blob, nodes[j])) | |
248 | break; | |
249 | ||
250 | /* Have we run out of nodes to add? */ | |
251 | if (j == maxcount) | |
252 | break; | |
253 | ||
254 | assert(!node_list[i]); | |
255 | node_list[i] = nodes[j++]; | |
256 | if (i >= num_found) | |
257 | num_found = i + 1; | |
258 | } | |
259 | } | |
260 | ||
261 | return num_found; | |
262 | } | |
263 | ||
b5220bc6 SG |
264 | /* |
265 | * This function is a little odd in that it accesses global data. At some | |
266 | * point if the architecture board.c files merge this will make more sense. | |
267 | * Even now, it is common code. | |
268 | */ | |
269 | int fdtdec_check_fdt(void) | |
270 | { | |
271 | /* We must have an fdt */ | |
f88fe2de | 272 | if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob)) |
b5220bc6 SG |
273 | panic("No valid fdt found - please append one to U-Boot\n" |
274 | "binary or define CONFIG_OF_EMBED\n"); | |
275 | return 0; | |
276 | } | |
d17da655 SG |
277 | |
278 | int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name) | |
279 | { | |
280 | const u32 *phandle; | |
281 | int lookup; | |
282 | ||
283 | phandle = fdt_getprop(blob, node, prop_name, NULL); | |
284 | if (!phandle) | |
285 | return -FDT_ERR_NOTFOUND; | |
286 | ||
287 | lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle)); | |
288 | return lookup; | |
289 | } | |
290 | ||
291 | /** | |
292 | * Look up a property in a node and check that it has a minimum length. | |
293 | * | |
294 | * @param blob FDT blob | |
295 | * @param node node to examine | |
296 | * @param prop_name name of property to find | |
297 | * @param min_len minimum property length in bytes | |
298 | * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not | |
299 | found, or -FDT_ERR_BADLAYOUT if not enough data | |
300 | * @return pointer to cell, which is only valid if err == 0 | |
301 | */ | |
302 | static const void *get_prop_check_min_len(const void *blob, int node, | |
303 | const char *prop_name, int min_len, int *err) | |
304 | { | |
305 | const void *cell; | |
306 | int len; | |
307 | ||
308 | debug("%s: %s\n", __func__, prop_name); | |
309 | cell = fdt_getprop(blob, node, prop_name, &len); | |
310 | if (!cell) | |
311 | *err = -FDT_ERR_NOTFOUND; | |
312 | else if (len < min_len) | |
313 | *err = -FDT_ERR_BADLAYOUT; | |
314 | else | |
315 | *err = 0; | |
316 | return cell; | |
317 | } | |
318 | ||
319 | int fdtdec_get_int_array(const void *blob, int node, const char *prop_name, | |
320 | u32 *array, int count) | |
321 | { | |
322 | const u32 *cell; | |
323 | int i, err = 0; | |
324 | ||
325 | debug("%s: %s\n", __func__, prop_name); | |
326 | cell = get_prop_check_min_len(blob, node, prop_name, | |
327 | sizeof(u32) * count, &err); | |
328 | if (!err) { | |
329 | for (i = 0; i < count; i++) | |
330 | array[i] = fdt32_to_cpu(cell[i]); | |
331 | } | |
332 | return err; | |
333 | } | |
334 | ||
335 | int fdtdec_get_bool(const void *blob, int node, const char *prop_name) | |
336 | { | |
337 | const s32 *cell; | |
338 | int len; | |
339 | ||
340 | debug("%s: %s\n", __func__, prop_name); | |
341 | cell = fdt_getprop(blob, node, prop_name, &len); | |
342 | return cell != NULL; | |
343 | } | |
ed3ee5cd SG |
344 | |
345 | /** | |
346 | * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no | |
347 | * terminating item. | |
348 | * | |
349 | * @param blob FDT blob to use | |
350 | * @param node Node to look at | |
351 | * @param prop_name Node property name | |
352 | * @param gpio Array of gpio elements to fill from FDT. This will be | |
353 | * untouched if either 0 or an error is returned | |
354 | * @param max_count Maximum number of elements allowed | |
355 | * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would | |
356 | * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing. | |
357 | */ | |
358 | static int fdtdec_decode_gpios(const void *blob, int node, | |
359 | const char *prop_name, struct fdt_gpio_state *gpio, | |
360 | int max_count) | |
361 | { | |
362 | const struct fdt_property *prop; | |
363 | const u32 *cell; | |
364 | const char *name; | |
365 | int len, i; | |
366 | ||
367 | debug("%s: %s\n", __func__, prop_name); | |
368 | assert(max_count > 0); | |
369 | prop = fdt_get_property(blob, node, prop_name, &len); | |
370 | if (!prop) { | |
371 | debug("FDT: %s: property '%s' missing\n", __func__, prop_name); | |
372 | return -FDT_ERR_NOTFOUND; | |
373 | } | |
374 | ||
375 | /* We will use the name to tag the GPIO */ | |
376 | name = fdt_string(blob, fdt32_to_cpu(prop->nameoff)); | |
377 | cell = (u32 *)prop->data; | |
378 | len /= sizeof(u32) * 3; /* 3 cells per GPIO record */ | |
379 | if (len > max_count) { | |
380 | debug("FDT: %s: too many GPIOs / cells for " | |
381 | "property '%s'\n", __func__, prop_name); | |
382 | return -FDT_ERR_BADLAYOUT; | |
383 | } | |
384 | ||
385 | /* Read out the GPIO data from the cells */ | |
386 | for (i = 0; i < len; i++, cell += 3) { | |
387 | gpio[i].gpio = fdt32_to_cpu(cell[1]); | |
388 | gpio[i].flags = fdt32_to_cpu(cell[2]); | |
389 | gpio[i].name = name; | |
390 | } | |
391 | ||
392 | return len; | |
393 | } | |
394 | ||
395 | int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name, | |
396 | struct fdt_gpio_state *gpio) | |
397 | { | |
398 | int err; | |
399 | ||
400 | debug("%s: %s\n", __func__, prop_name); | |
401 | gpio->gpio = FDT_GPIO_NONE; | |
402 | gpio->name = NULL; | |
403 | err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1); | |
404 | return err == 1 ? 0 : err; | |
405 | } | |
406 | ||
407 | int fdtdec_setup_gpio(struct fdt_gpio_state *gpio) | |
408 | { | |
409 | /* | |
410 | * Return success if there is no GPIO defined. This is used for | |
411 | * optional GPIOs) | |
412 | */ | |
413 | if (!fdt_gpio_isvalid(gpio)) | |
414 | return 0; | |
415 | ||
416 | if (gpio_request(gpio->gpio, gpio->name)) | |
417 | return -1; | |
418 | return 0; | |
419 | } |