]>
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>"), |
87f938c9 | 40 | COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"), |
b5220bc6 SG |
41 | }; |
42 | ||
a53f4a29 SG |
43 | const char *fdtdec_get_compatible(enum fdt_compat_id id) |
44 | { | |
45 | /* We allow reading of the 'unknown' ID for testing purposes */ | |
46 | assert(id >= 0 && id < COMPAT_COUNT); | |
47 | return compat_names[id]; | |
48 | } | |
49 | ||
b5220bc6 SG |
50 | /** |
51 | * Look in the FDT for an alias with the given name and return its node. | |
52 | * | |
53 | * @param blob FDT blob | |
54 | * @param name alias name to look up | |
55 | * @return node offset if found, or an error code < 0 otherwise | |
56 | */ | |
57 | static int find_alias_node(const void *blob, const char *name) | |
58 | { | |
59 | const char *path; | |
60 | int alias_node; | |
61 | ||
62 | debug("find_alias_node: %s\n", name); | |
63 | alias_node = fdt_path_offset(blob, "/aliases"); | |
64 | if (alias_node < 0) | |
65 | return alias_node; | |
66 | path = fdt_getprop(blob, alias_node, name, NULL); | |
67 | if (!path) | |
68 | return -FDT_ERR_NOTFOUND; | |
69 | return fdt_path_offset(blob, path); | |
70 | } | |
71 | ||
72 | fdt_addr_t fdtdec_get_addr(const void *blob, int node, | |
73 | const char *prop_name) | |
74 | { | |
75 | const fdt_addr_t *cell; | |
76 | int len; | |
77 | ||
78 | debug("get_addr: %s\n", prop_name); | |
79 | cell = fdt_getprop(blob, node, prop_name, &len); | |
80 | if (cell && (len == sizeof(fdt_addr_t) || | |
81 | len == sizeof(fdt_addr_t) * 2)) | |
82 | return fdt_addr_to_cpu(*cell); | |
83 | return FDT_ADDR_T_NONE; | |
84 | } | |
85 | ||
86 | s32 fdtdec_get_int(const void *blob, int node, const char *prop_name, | |
87 | s32 default_val) | |
88 | { | |
89 | const s32 *cell; | |
90 | int len; | |
91 | ||
92 | debug("get_size: %s\n", prop_name); | |
93 | cell = fdt_getprop(blob, node, prop_name, &len); | |
94 | if (cell && len >= sizeof(s32)) | |
95 | return fdt32_to_cpu(cell[0]); | |
96 | return default_val; | |
97 | } | |
98 | ||
f88fe2de | 99 | int fdtdec_get_is_enabled(const void *blob, int node) |
b5220bc6 SG |
100 | { |
101 | const char *cell; | |
102 | ||
f88fe2de SG |
103 | /* |
104 | * It should say "okay", so only allow that. Some fdts use "ok" but | |
105 | * this is a bug. Please fix your device tree source file. See here | |
106 | * for discussion: | |
107 | * | |
108 | * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html | |
109 | */ | |
b5220bc6 SG |
110 | cell = fdt_getprop(blob, node, "status", NULL); |
111 | if (cell) | |
f88fe2de SG |
112 | return 0 == strcmp(cell, "okay"); |
113 | return 1; | |
b5220bc6 SG |
114 | } |
115 | ||
116 | enum fdt_compat_id fd_dec_lookup(const void *blob, int node) | |
117 | { | |
118 | enum fdt_compat_id id; | |
119 | ||
120 | /* Search our drivers */ | |
121 | for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++) | |
122 | if (0 == fdt_node_check_compatible(blob, node, | |
123 | compat_names[id])) | |
124 | return id; | |
125 | return COMPAT_UNKNOWN; | |
126 | } | |
127 | ||
128 | int fdtdec_next_compatible(const void *blob, int node, | |
129 | enum fdt_compat_id id) | |
130 | { | |
131 | return fdt_node_offset_by_compatible(blob, node, compat_names[id]); | |
132 | } | |
133 | ||
134 | int fdtdec_next_alias(const void *blob, const char *name, | |
135 | enum fdt_compat_id id, int *upto) | |
136 | { | |
137 | #define MAX_STR_LEN 20 | |
138 | char str[MAX_STR_LEN + 20]; | |
139 | int node, err; | |
140 | ||
141 | /* snprintf() is not available */ | |
142 | assert(strlen(name) < MAX_STR_LEN); | |
143 | sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto); | |
b5220bc6 SG |
144 | node = find_alias_node(blob, str); |
145 | if (node < 0) | |
146 | return node; | |
147 | err = fdt_node_check_compatible(blob, node, compat_names[id]); | |
148 | if (err < 0) | |
149 | return err; | |
f88fe2de SG |
150 | if (err) |
151 | return -FDT_ERR_NOTFOUND; | |
152 | (*upto)++; | |
153 | return node; | |
b5220bc6 SG |
154 | } |
155 | ||
a53f4a29 SG |
156 | int fdtdec_find_aliases_for_id(const void *blob, const char *name, |
157 | enum fdt_compat_id id, int *node_list, int maxcount) | |
c6782270 SG |
158 | { |
159 | memset(node_list, '\0', sizeof(*node_list) * maxcount); | |
160 | ||
161 | return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount); | |
162 | } | |
163 | ||
164 | /* TODO: Can we tighten this code up a little? */ | |
165 | int fdtdec_add_aliases_for_id(const void *blob, const char *name, | |
166 | enum fdt_compat_id id, int *node_list, int maxcount) | |
a53f4a29 SG |
167 | { |
168 | int name_len = strlen(name); | |
169 | int nodes[maxcount]; | |
170 | int num_found = 0; | |
171 | int offset, node; | |
172 | int alias_node; | |
173 | int count; | |
174 | int i, j; | |
175 | ||
176 | /* find the alias node if present */ | |
177 | alias_node = fdt_path_offset(blob, "/aliases"); | |
178 | ||
179 | /* | |
180 | * start with nothing, and we can assume that the root node can't | |
181 | * match | |
182 | */ | |
183 | memset(nodes, '\0', sizeof(nodes)); | |
184 | ||
185 | /* First find all the compatible nodes */ | |
186 | for (node = count = 0; node >= 0 && count < maxcount;) { | |
187 | node = fdtdec_next_compatible(blob, node, id); | |
188 | if (node >= 0) | |
189 | nodes[count++] = node; | |
190 | } | |
191 | if (node >= 0) | |
192 | debug("%s: warning: maxcount exceeded with alias '%s'\n", | |
193 | __func__, name); | |
194 | ||
195 | /* Now find all the aliases */ | |
a53f4a29 SG |
196 | for (offset = fdt_first_property_offset(blob, alias_node); |
197 | offset > 0; | |
198 | offset = fdt_next_property_offset(blob, offset)) { | |
199 | const struct fdt_property *prop; | |
200 | const char *path; | |
201 | int number; | |
202 | int found; | |
203 | ||
204 | node = 0; | |
205 | prop = fdt_get_property_by_offset(blob, offset, NULL); | |
206 | path = fdt_string(blob, fdt32_to_cpu(prop->nameoff)); | |
207 | if (prop->len && 0 == strncmp(path, name, name_len)) | |
208 | node = fdt_path_offset(blob, prop->data); | |
209 | if (node <= 0) | |
210 | continue; | |
211 | ||
212 | /* Get the alias number */ | |
213 | number = simple_strtoul(path + name_len, NULL, 10); | |
214 | if (number < 0 || number >= maxcount) { | |
215 | debug("%s: warning: alias '%s' is out of range\n", | |
216 | __func__, path); | |
217 | continue; | |
218 | } | |
219 | ||
220 | /* Make sure the node we found is actually in our list! */ | |
221 | found = -1; | |
222 | for (j = 0; j < count; j++) | |
223 | if (nodes[j] == node) { | |
224 | found = j; | |
225 | break; | |
226 | } | |
227 | ||
228 | if (found == -1) { | |
229 | debug("%s: warning: alias '%s' points to a node " | |
230 | "'%s' that is missing or is not compatible " | |
231 | " with '%s'\n", __func__, path, | |
232 | fdt_get_name(blob, node, NULL), | |
233 | compat_names[id]); | |
234 | continue; | |
235 | } | |
236 | ||
237 | /* | |
238 | * Add this node to our list in the right place, and mark | |
239 | * it as done. | |
240 | */ | |
241 | if (fdtdec_get_is_enabled(blob, node)) { | |
c6782270 SG |
242 | if (node_list[number]) { |
243 | debug("%s: warning: alias '%s' requires that " | |
244 | "a node be placed in the list in a " | |
245 | "position which is already filled by " | |
246 | "node '%s'\n", __func__, path, | |
247 | fdt_get_name(blob, node, NULL)); | |
248 | continue; | |
249 | } | |
a53f4a29 SG |
250 | node_list[number] = node; |
251 | if (number >= num_found) | |
252 | num_found = number + 1; | |
253 | } | |
c6782270 | 254 | nodes[found] = 0; |
a53f4a29 SG |
255 | } |
256 | ||
257 | /* Add any nodes not mentioned by an alias */ | |
258 | for (i = j = 0; i < maxcount; i++) { | |
259 | if (!node_list[i]) { | |
260 | for (; j < maxcount; j++) | |
261 | if (nodes[j] && | |
262 | fdtdec_get_is_enabled(blob, nodes[j])) | |
263 | break; | |
264 | ||
265 | /* Have we run out of nodes to add? */ | |
266 | if (j == maxcount) | |
267 | break; | |
268 | ||
269 | assert(!node_list[i]); | |
270 | node_list[i] = nodes[j++]; | |
271 | if (i >= num_found) | |
272 | num_found = i + 1; | |
273 | } | |
274 | } | |
275 | ||
276 | return num_found; | |
277 | } | |
278 | ||
9a263e55 SG |
279 | int fdtdec_check_fdt(void) |
280 | { | |
281 | /* | |
282 | * We must have an FDT, but we cannot panic() yet since the console | |
283 | * is not ready. So for now, just assert(). Boards which need an early | |
284 | * FDT (prior to console ready) will need to make their own | |
285 | * arrangements and do their own checks. | |
286 | */ | |
287 | assert(!fdtdec_prepare_fdt()); | |
288 | return 0; | |
289 | } | |
290 | ||
b5220bc6 SG |
291 | /* |
292 | * This function is a little odd in that it accesses global data. At some | |
293 | * point if the architecture board.c files merge this will make more sense. | |
294 | * Even now, it is common code. | |
295 | */ | |
9a263e55 | 296 | int fdtdec_prepare_fdt(void) |
b5220bc6 | 297 | { |
9a263e55 SG |
298 | if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob)) { |
299 | printf("No valid FDT found - please append one to U-Boot " | |
300 | "binary, use u-boot-dtb.bin or define " | |
301 | "CONFIG_OF_EMBED\n"); | |
302 | return -1; | |
303 | } | |
b5220bc6 SG |
304 | return 0; |
305 | } | |
d17da655 SG |
306 | |
307 | int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name) | |
308 | { | |
309 | const u32 *phandle; | |
310 | int lookup; | |
311 | ||
312 | phandle = fdt_getprop(blob, node, prop_name, NULL); | |
313 | if (!phandle) | |
314 | return -FDT_ERR_NOTFOUND; | |
315 | ||
316 | lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle)); | |
317 | return lookup; | |
318 | } | |
319 | ||
320 | /** | |
321 | * Look up a property in a node and check that it has a minimum length. | |
322 | * | |
323 | * @param blob FDT blob | |
324 | * @param node node to examine | |
325 | * @param prop_name name of property to find | |
326 | * @param min_len minimum property length in bytes | |
327 | * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not | |
328 | found, or -FDT_ERR_BADLAYOUT if not enough data | |
329 | * @return pointer to cell, which is only valid if err == 0 | |
330 | */ | |
331 | static const void *get_prop_check_min_len(const void *blob, int node, | |
332 | const char *prop_name, int min_len, int *err) | |
333 | { | |
334 | const void *cell; | |
335 | int len; | |
336 | ||
337 | debug("%s: %s\n", __func__, prop_name); | |
338 | cell = fdt_getprop(blob, node, prop_name, &len); | |
339 | if (!cell) | |
340 | *err = -FDT_ERR_NOTFOUND; | |
341 | else if (len < min_len) | |
342 | *err = -FDT_ERR_BADLAYOUT; | |
343 | else | |
344 | *err = 0; | |
345 | return cell; | |
346 | } | |
347 | ||
348 | int fdtdec_get_int_array(const void *blob, int node, const char *prop_name, | |
349 | u32 *array, int count) | |
350 | { | |
351 | const u32 *cell; | |
352 | int i, err = 0; | |
353 | ||
354 | debug("%s: %s\n", __func__, prop_name); | |
355 | cell = get_prop_check_min_len(blob, node, prop_name, | |
356 | sizeof(u32) * count, &err); | |
357 | if (!err) { | |
358 | for (i = 0; i < count; i++) | |
359 | array[i] = fdt32_to_cpu(cell[i]); | |
360 | } | |
361 | return err; | |
362 | } | |
363 | ||
364 | int fdtdec_get_bool(const void *blob, int node, const char *prop_name) | |
365 | { | |
366 | const s32 *cell; | |
367 | int len; | |
368 | ||
369 | debug("%s: %s\n", __func__, prop_name); | |
370 | cell = fdt_getprop(blob, node, prop_name, &len); | |
371 | return cell != NULL; | |
372 | } | |
ed3ee5cd SG |
373 | |
374 | /** | |
375 | * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no | |
376 | * terminating item. | |
377 | * | |
378 | * @param blob FDT blob to use | |
379 | * @param node Node to look at | |
380 | * @param prop_name Node property name | |
381 | * @param gpio Array of gpio elements to fill from FDT. This will be | |
382 | * untouched if either 0 or an error is returned | |
383 | * @param max_count Maximum number of elements allowed | |
384 | * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would | |
385 | * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing. | |
386 | */ | |
387 | static int fdtdec_decode_gpios(const void *blob, int node, | |
388 | const char *prop_name, struct fdt_gpio_state *gpio, | |
389 | int max_count) | |
390 | { | |
391 | const struct fdt_property *prop; | |
392 | const u32 *cell; | |
393 | const char *name; | |
394 | int len, i; | |
395 | ||
396 | debug("%s: %s\n", __func__, prop_name); | |
397 | assert(max_count > 0); | |
398 | prop = fdt_get_property(blob, node, prop_name, &len); | |
399 | if (!prop) { | |
400 | debug("FDT: %s: property '%s' missing\n", __func__, prop_name); | |
401 | return -FDT_ERR_NOTFOUND; | |
402 | } | |
403 | ||
404 | /* We will use the name to tag the GPIO */ | |
405 | name = fdt_string(blob, fdt32_to_cpu(prop->nameoff)); | |
406 | cell = (u32 *)prop->data; | |
407 | len /= sizeof(u32) * 3; /* 3 cells per GPIO record */ | |
408 | if (len > max_count) { | |
409 | debug("FDT: %s: too many GPIOs / cells for " | |
410 | "property '%s'\n", __func__, prop_name); | |
411 | return -FDT_ERR_BADLAYOUT; | |
412 | } | |
413 | ||
414 | /* Read out the GPIO data from the cells */ | |
415 | for (i = 0; i < len; i++, cell += 3) { | |
416 | gpio[i].gpio = fdt32_to_cpu(cell[1]); | |
417 | gpio[i].flags = fdt32_to_cpu(cell[2]); | |
418 | gpio[i].name = name; | |
419 | } | |
420 | ||
421 | return len; | |
422 | } | |
423 | ||
424 | int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name, | |
425 | struct fdt_gpio_state *gpio) | |
426 | { | |
427 | int err; | |
428 | ||
429 | debug("%s: %s\n", __func__, prop_name); | |
430 | gpio->gpio = FDT_GPIO_NONE; | |
431 | gpio->name = NULL; | |
432 | err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1); | |
433 | return err == 1 ? 0 : err; | |
434 | } | |
435 | ||
436 | int fdtdec_setup_gpio(struct fdt_gpio_state *gpio) | |
437 | { | |
438 | /* | |
439 | * Return success if there is no GPIO defined. This is used for | |
440 | * optional GPIOs) | |
441 | */ | |
442 | if (!fdt_gpio_isvalid(gpio)) | |
443 | return 0; | |
444 | ||
445 | if (gpio_request(gpio->gpio, gpio->name)) | |
446 | return -1; | |
447 | return 0; | |
448 | } |