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