]>
Commit | Line | Data |
---|---|---|
b5220bc6 SG |
1 | /* |
2 | * Copyright (c) 2011 The Chromium OS Authors. | |
1a459660 | 3 | * SPDX-License-Identifier: GPL-2.0+ |
b5220bc6 SG |
4 | */ |
5 | ||
29a23f9d | 6 | #ifndef USE_HOSTCC |
b5220bc6 | 7 | #include <common.h> |
5c33c9fd | 8 | #include <errno.h> |
b5220bc6 SG |
9 | #include <serial.h> |
10 | #include <libfdt.h> | |
11 | #include <fdtdec.h> | |
5c33c9fd | 12 | #include <linux/ctype.h> |
b5220bc6 SG |
13 | |
14 | DECLARE_GLOBAL_DATA_PTR; | |
15 | ||
16 | /* | |
17 | * Here are the type we know about. One day we might allow drivers to | |
18 | * register. For now we just put them here. The COMPAT macro allows us to | |
19 | * turn this into a sparse list later, and keeps the ID with the name. | |
20 | */ | |
21 | #define COMPAT(id, name) name | |
22 | static const char * const compat_names[COMPAT_COUNT] = { | |
f88fe2de | 23 | COMPAT(UNKNOWN, "<none>"), |
87f938c9 | 24 | COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"), |
7e44d932 JL |
25 | COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"), |
26 | COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"), | |
0e35ad05 JZ |
27 | COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"), |
28 | COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"), | |
6642a681 | 29 | COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"), |
312693c3 | 30 | COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"), |
e1ae0d1f | 31 | COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"), |
87540de3 | 32 | COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"), |
a73ca478 | 33 | COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"), |
f4e4e0b0 | 34 | COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"), |
c9aa831e | 35 | COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"), |
f315828b TR |
36 | COMPAT(NVIDIA_TEGRA124_PCIE, "nvidia,tegra124-pcie"), |
37 | COMPAT(NVIDIA_TEGRA30_PCIE, "nvidia,tegra30-pcie"), | |
38 | COMPAT(NVIDIA_TEGRA20_PCIE, "nvidia,tegra20-pcie"), | |
79c7a90f | 39 | COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"), |
cc9fe33a HR |
40 | COMPAT(SMSC_LAN9215, "smsc,lan9215"), |
41 | COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"), | |
c34253d1 | 42 | COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"), |
72dbff12 RS |
43 | COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"), |
44 | COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"), | |
713cb680 | 45 | COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"), |
6abd1620 | 46 | COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"), |
108b85be | 47 | COMPAT(SAMSUNG_EXYNOS5_XHCI, "samsung,exynos5250-xhci"), |
6abd1620 | 48 | COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"), |
108b85be | 49 | COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"), |
618766c0 | 50 | COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"), |
d7377b51 | 51 | COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"), |
de461c52 | 52 | COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"), |
1e4706a7 | 53 | COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"), |
7d3ca0f8 | 54 | COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"), |
3577fe8b | 55 | COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"), |
ee1e3c2f | 56 | COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"), |
cd577e2b | 57 | COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"), |
bb8215f4 | 58 | COMPAT(GENERIC_SPI_FLASH, "spi-flash"), |
7772bb78 | 59 | COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"), |
f6267998 | 60 | COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"), |
ec34fa5e | 61 | COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"), |
ecbd7e1e | 62 | COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"), |
7d95f2a3 | 63 | COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"), |
ac1058fd | 64 | COMPAT(TI_TPS65090, "ti,tps65090"), |
a9cf6da9 | 65 | COMPAT(COMPAT_NXP_PTN3460, "nxp,ptn3460"), |
45c480c9 | 66 | COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"), |
9e8f664e | 67 | COMPAT(PARADE_PS8625, "parade,ps8625"), |
77f9b1fb | 68 | COMPAT(INTEL_MICROCODE, "intel,microcode"), |
65dd74a6 | 69 | COMPAT(MEMORY_SPD, "memory-spd"), |
3ac83935 | 70 | COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"), |
bb80be39 | 71 | COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"), |
effcf067 | 72 | COMPAT(INTEL_GMA, "intel,gma"), |
6173c45b | 73 | COMPAT(AMS_AS3722, "ams,as3722"), |
5da38086 | 74 | COMPAT(INTEL_ICH_SPI, "intel,ich-spi"), |
c89ada01 | 75 | COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"), |
6462cded | 76 | COMPAT(SOCIONEXT_XHCI, "socionext,uniphier-xhci"), |
90b16d14 | 77 | COMPAT(COMPAT_INTEL_PCH, "intel,bd82x6x"), |
b5220bc6 SG |
78 | }; |
79 | ||
a53f4a29 SG |
80 | const char *fdtdec_get_compatible(enum fdt_compat_id id) |
81 | { | |
82 | /* We allow reading of the 'unknown' ID for testing purposes */ | |
83 | assert(id >= 0 && id < COMPAT_COUNT); | |
84 | return compat_names[id]; | |
85 | } | |
86 | ||
4397a2a8 SG |
87 | fdt_addr_t fdtdec_get_addr_size(const void *blob, int node, |
88 | const char *prop_name, fdt_size_t *sizep) | |
b5220bc6 SG |
89 | { |
90 | const fdt_addr_t *cell; | |
91 | int len; | |
92 | ||
1cb2323b | 93 | debug("%s: %s: ", __func__, prop_name); |
b5220bc6 | 94 | cell = fdt_getprop(blob, node, prop_name, &len); |
4397a2a8 SG |
95 | if (cell && ((!sizep && len == sizeof(fdt_addr_t)) || |
96 | len == sizeof(fdt_addr_t) * 2)) { | |
1cb2323b | 97 | fdt_addr_t addr = fdt_addr_to_cpu(*cell); |
4397a2a8 SG |
98 | if (sizep) { |
99 | const fdt_size_t *size; | |
1cb2323b | 100 | |
4397a2a8 SG |
101 | size = (fdt_size_t *)((char *)cell + |
102 | sizeof(fdt_addr_t)); | |
103 | *sizep = fdt_size_to_cpu(*size); | |
370b6c5c SG |
104 | debug("addr=%08lx, size=%08x\n", |
105 | (ulong)addr, *sizep); | |
4397a2a8 | 106 | } else { |
370b6c5c | 107 | debug("%08lx\n", (ulong)addr); |
4397a2a8 | 108 | } |
1cb2323b SG |
109 | return addr; |
110 | } | |
111 | debug("(not found)\n"); | |
b5220bc6 SG |
112 | return FDT_ADDR_T_NONE; |
113 | } | |
114 | ||
4397a2a8 SG |
115 | fdt_addr_t fdtdec_get_addr(const void *blob, int node, |
116 | const char *prop_name) | |
117 | { | |
118 | return fdtdec_get_addr_size(blob, node, prop_name, NULL); | |
119 | } | |
120 | ||
a62e84d7 BM |
121 | #ifdef CONFIG_PCI |
122 | int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type, | |
123 | const char *prop_name, struct fdt_pci_addr *addr) | |
124 | { | |
125 | const u32 *cell; | |
126 | int len; | |
127 | int ret = -ENOENT; | |
128 | ||
129 | debug("%s: %s: ", __func__, prop_name); | |
130 | ||
131 | /* | |
132 | * If we follow the pci bus bindings strictly, we should check | |
133 | * the value of the node's parent node's #address-cells and | |
134 | * #size-cells. They need to be 3 and 2 accordingly. However, | |
135 | * for simplicity we skip the check here. | |
136 | */ | |
137 | cell = fdt_getprop(blob, node, prop_name, &len); | |
138 | if (!cell) | |
139 | goto fail; | |
140 | ||
141 | if ((len % FDT_PCI_REG_SIZE) == 0) { | |
142 | int num = len / FDT_PCI_REG_SIZE; | |
143 | int i; | |
144 | ||
145 | for (i = 0; i < num; i++) { | |
146 | debug("pci address #%d: %08lx %08lx %08lx\n", i, | |
147 | (ulong)fdt_addr_to_cpu(cell[0]), | |
148 | (ulong)fdt_addr_to_cpu(cell[1]), | |
149 | (ulong)fdt_addr_to_cpu(cell[2])); | |
150 | if ((fdt_addr_to_cpu(*cell) & type) == type) { | |
151 | addr->phys_hi = fdt_addr_to_cpu(cell[0]); | |
152 | addr->phys_mid = fdt_addr_to_cpu(cell[1]); | |
153 | addr->phys_lo = fdt_addr_to_cpu(cell[2]); | |
154 | break; | |
155 | } else { | |
156 | cell += (FDT_PCI_ADDR_CELLS + | |
157 | FDT_PCI_SIZE_CELLS); | |
158 | } | |
159 | } | |
160 | ||
106cce96 SG |
161 | if (i == num) { |
162 | ret = -ENXIO; | |
a62e84d7 | 163 | goto fail; |
106cce96 | 164 | } |
a62e84d7 BM |
165 | |
166 | return 0; | |
167 | } else { | |
168 | ret = -EINVAL; | |
169 | } | |
170 | ||
171 | fail: | |
172 | debug("(not found)\n"); | |
173 | return ret; | |
174 | } | |
175 | ||
176 | int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device) | |
177 | { | |
178 | const char *list, *end; | |
179 | int len; | |
180 | ||
181 | list = fdt_getprop(blob, node, "compatible", &len); | |
182 | if (!list) | |
183 | return -ENOENT; | |
184 | ||
185 | end = list + len; | |
186 | while (list < end) { | |
187 | char *s; | |
188 | ||
189 | len = strlen(list); | |
190 | if (len >= strlen("pciVVVV,DDDD")) { | |
191 | s = strstr(list, "pci"); | |
192 | ||
193 | /* | |
194 | * check if the string is something like pciVVVV,DDDD.RR | |
195 | * or just pciVVVV,DDDD | |
196 | */ | |
197 | if (s && s[7] == ',' && | |
198 | (s[12] == '.' || s[12] == 0)) { | |
199 | s += 3; | |
200 | *vendor = simple_strtol(s, NULL, 16); | |
201 | ||
202 | s += 5; | |
203 | *device = simple_strtol(s, NULL, 16); | |
204 | ||
205 | return 0; | |
206 | } | |
207 | } else { | |
208 | list += (len + 1); | |
209 | } | |
210 | } | |
211 | ||
212 | return -ENOENT; | |
213 | } | |
214 | ||
215 | int fdtdec_get_pci_bdf(const void *blob, int node, | |
216 | struct fdt_pci_addr *addr, pci_dev_t *bdf) | |
217 | { | |
218 | u16 dt_vendor, dt_device, vendor, device; | |
219 | int ret; | |
220 | ||
221 | /* get vendor id & device id from the compatible string */ | |
222 | ret = fdtdec_get_pci_vendev(blob, node, &dt_vendor, &dt_device); | |
223 | if (ret) | |
224 | return ret; | |
225 | ||
226 | /* extract the bdf from fdt_pci_addr */ | |
227 | *bdf = addr->phys_hi & 0xffff00; | |
228 | ||
229 | /* read vendor id & device id based on bdf */ | |
230 | pci_read_config_word(*bdf, PCI_VENDOR_ID, &vendor); | |
231 | pci_read_config_word(*bdf, PCI_DEVICE_ID, &device); | |
232 | ||
233 | /* | |
234 | * Note there are two places in the device tree to fully describe | |
235 | * a pci device: one is via compatible string with a format of | |
236 | * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in | |
237 | * the device node's reg address property. We read the vendor id | |
238 | * and device id based on bdf and compare the values with the | |
239 | * "VVVV,DDDD". If they are the same, then we are good to use bdf | |
240 | * to read device's bar. But if they are different, we have to rely | |
241 | * on the vendor id and device id extracted from the compatible | |
242 | * string and locate the real bdf by pci_find_device(). This is | |
243 | * because normally we may only know device's device number and | |
244 | * function number when writing device tree. The bus number is | |
245 | * dynamically assigned during the pci enumeration process. | |
246 | */ | |
247 | if ((dt_vendor != vendor) || (dt_device != device)) { | |
248 | *bdf = pci_find_device(dt_vendor, dt_device, 0); | |
249 | if (*bdf == -1) | |
250 | return -ENODEV; | |
251 | } | |
252 | ||
253 | return 0; | |
254 | } | |
255 | ||
256 | int fdtdec_get_pci_bar32(const void *blob, int node, | |
257 | struct fdt_pci_addr *addr, u32 *bar) | |
258 | { | |
259 | pci_dev_t bdf; | |
260 | int barnum; | |
261 | int ret; | |
262 | ||
263 | /* get pci devices's bdf */ | |
264 | ret = fdtdec_get_pci_bdf(blob, node, addr, &bdf); | |
265 | if (ret) | |
266 | return ret; | |
267 | ||
268 | /* extract the bar number from fdt_pci_addr */ | |
269 | barnum = addr->phys_hi & 0xff; | |
270 | if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS)) | |
271 | return -EINVAL; | |
272 | ||
273 | barnum = (barnum - PCI_BASE_ADDRESS_0) / 4; | |
274 | *bar = pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf)), bdf, barnum); | |
275 | ||
276 | return 0; | |
277 | } | |
278 | #endif | |
279 | ||
aadef0a1 CLC |
280 | uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name, |
281 | uint64_t default_val) | |
282 | { | |
283 | const uint64_t *cell64; | |
284 | int length; | |
285 | ||
286 | cell64 = fdt_getprop(blob, node, prop_name, &length); | |
287 | if (!cell64 || length < sizeof(*cell64)) | |
288 | return default_val; | |
289 | ||
290 | return fdt64_to_cpu(*cell64); | |
291 | } | |
292 | ||
f88fe2de | 293 | int fdtdec_get_is_enabled(const void *blob, int node) |
b5220bc6 SG |
294 | { |
295 | const char *cell; | |
296 | ||
f88fe2de SG |
297 | /* |
298 | * It should say "okay", so only allow that. Some fdts use "ok" but | |
299 | * this is a bug. Please fix your device tree source file. See here | |
300 | * for discussion: | |
301 | * | |
302 | * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html | |
303 | */ | |
b5220bc6 SG |
304 | cell = fdt_getprop(blob, node, "status", NULL); |
305 | if (cell) | |
f88fe2de SG |
306 | return 0 == strcmp(cell, "okay"); |
307 | return 1; | |
b5220bc6 SG |
308 | } |
309 | ||
7cde397b | 310 | enum fdt_compat_id fdtdec_lookup(const void *blob, int node) |
b5220bc6 SG |
311 | { |
312 | enum fdt_compat_id id; | |
313 | ||
314 | /* Search our drivers */ | |
315 | for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++) | |
316 | if (0 == fdt_node_check_compatible(blob, node, | |
317 | compat_names[id])) | |
318 | return id; | |
319 | return COMPAT_UNKNOWN; | |
320 | } | |
321 | ||
322 | int fdtdec_next_compatible(const void *blob, int node, | |
323 | enum fdt_compat_id id) | |
324 | { | |
325 | return fdt_node_offset_by_compatible(blob, node, compat_names[id]); | |
326 | } | |
327 | ||
3ddecfc7 SG |
328 | int fdtdec_next_compatible_subnode(const void *blob, int node, |
329 | enum fdt_compat_id id, int *depthp) | |
330 | { | |
331 | do { | |
332 | node = fdt_next_node(blob, node, depthp); | |
333 | } while (*depthp > 1); | |
334 | ||
335 | /* If this is a direct subnode, and compatible, return it */ | |
336 | if (*depthp == 1 && 0 == fdt_node_check_compatible( | |
337 | blob, node, compat_names[id])) | |
338 | return node; | |
339 | ||
340 | return -FDT_ERR_NOTFOUND; | |
341 | } | |
342 | ||
b5220bc6 SG |
343 | int fdtdec_next_alias(const void *blob, const char *name, |
344 | enum fdt_compat_id id, int *upto) | |
345 | { | |
346 | #define MAX_STR_LEN 20 | |
347 | char str[MAX_STR_LEN + 20]; | |
348 | int node, err; | |
349 | ||
350 | /* snprintf() is not available */ | |
351 | assert(strlen(name) < MAX_STR_LEN); | |
352 | sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto); | |
00878476 | 353 | node = fdt_path_offset(blob, str); |
b5220bc6 SG |
354 | if (node < 0) |
355 | return node; | |
356 | err = fdt_node_check_compatible(blob, node, compat_names[id]); | |
357 | if (err < 0) | |
358 | return err; | |
f88fe2de SG |
359 | if (err) |
360 | return -FDT_ERR_NOTFOUND; | |
361 | (*upto)++; | |
362 | return node; | |
b5220bc6 SG |
363 | } |
364 | ||
a53f4a29 SG |
365 | int fdtdec_find_aliases_for_id(const void *blob, const char *name, |
366 | enum fdt_compat_id id, int *node_list, int maxcount) | |
c6782270 SG |
367 | { |
368 | memset(node_list, '\0', sizeof(*node_list) * maxcount); | |
369 | ||
370 | return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount); | |
371 | } | |
372 | ||
373 | /* TODO: Can we tighten this code up a little? */ | |
374 | int fdtdec_add_aliases_for_id(const void *blob, const char *name, | |
375 | enum fdt_compat_id id, int *node_list, int maxcount) | |
a53f4a29 SG |
376 | { |
377 | int name_len = strlen(name); | |
378 | int nodes[maxcount]; | |
379 | int num_found = 0; | |
380 | int offset, node; | |
381 | int alias_node; | |
382 | int count; | |
383 | int i, j; | |
384 | ||
385 | /* find the alias node if present */ | |
386 | alias_node = fdt_path_offset(blob, "/aliases"); | |
387 | ||
388 | /* | |
389 | * start with nothing, and we can assume that the root node can't | |
390 | * match | |
391 | */ | |
392 | memset(nodes, '\0', sizeof(nodes)); | |
393 | ||
394 | /* First find all the compatible nodes */ | |
395 | for (node = count = 0; node >= 0 && count < maxcount;) { | |
396 | node = fdtdec_next_compatible(blob, node, id); | |
397 | if (node >= 0) | |
398 | nodes[count++] = node; | |
399 | } | |
400 | if (node >= 0) | |
401 | debug("%s: warning: maxcount exceeded with alias '%s'\n", | |
402 | __func__, name); | |
403 | ||
404 | /* Now find all the aliases */ | |
a53f4a29 SG |
405 | for (offset = fdt_first_property_offset(blob, alias_node); |
406 | offset > 0; | |
407 | offset = fdt_next_property_offset(blob, offset)) { | |
408 | const struct fdt_property *prop; | |
409 | const char *path; | |
410 | int number; | |
411 | int found; | |
412 | ||
413 | node = 0; | |
414 | prop = fdt_get_property_by_offset(blob, offset, NULL); | |
415 | path = fdt_string(blob, fdt32_to_cpu(prop->nameoff)); | |
416 | if (prop->len && 0 == strncmp(path, name, name_len)) | |
417 | node = fdt_path_offset(blob, prop->data); | |
418 | if (node <= 0) | |
419 | continue; | |
420 | ||
421 | /* Get the alias number */ | |
422 | number = simple_strtoul(path + name_len, NULL, 10); | |
423 | if (number < 0 || number >= maxcount) { | |
424 | debug("%s: warning: alias '%s' is out of range\n", | |
425 | __func__, path); | |
426 | continue; | |
427 | } | |
428 | ||
429 | /* Make sure the node we found is actually in our list! */ | |
430 | found = -1; | |
431 | for (j = 0; j < count; j++) | |
432 | if (nodes[j] == node) { | |
433 | found = j; | |
434 | break; | |
435 | } | |
436 | ||
437 | if (found == -1) { | |
438 | debug("%s: warning: alias '%s' points to a node " | |
439 | "'%s' that is missing or is not compatible " | |
440 | " with '%s'\n", __func__, path, | |
441 | fdt_get_name(blob, node, NULL), | |
442 | compat_names[id]); | |
443 | continue; | |
444 | } | |
445 | ||
446 | /* | |
447 | * Add this node to our list in the right place, and mark | |
448 | * it as done. | |
449 | */ | |
450 | if (fdtdec_get_is_enabled(blob, node)) { | |
c6782270 SG |
451 | if (node_list[number]) { |
452 | debug("%s: warning: alias '%s' requires that " | |
453 | "a node be placed in the list in a " | |
454 | "position which is already filled by " | |
455 | "node '%s'\n", __func__, path, | |
456 | fdt_get_name(blob, node, NULL)); | |
457 | continue; | |
458 | } | |
a53f4a29 SG |
459 | node_list[number] = node; |
460 | if (number >= num_found) | |
461 | num_found = number + 1; | |
462 | } | |
c6782270 | 463 | nodes[found] = 0; |
a53f4a29 SG |
464 | } |
465 | ||
466 | /* Add any nodes not mentioned by an alias */ | |
467 | for (i = j = 0; i < maxcount; i++) { | |
468 | if (!node_list[i]) { | |
469 | for (; j < maxcount; j++) | |
470 | if (nodes[j] && | |
471 | fdtdec_get_is_enabled(blob, nodes[j])) | |
472 | break; | |
473 | ||
474 | /* Have we run out of nodes to add? */ | |
475 | if (j == maxcount) | |
476 | break; | |
477 | ||
478 | assert(!node_list[i]); | |
479 | node_list[i] = nodes[j++]; | |
480 | if (i >= num_found) | |
481 | num_found = i + 1; | |
482 | } | |
483 | } | |
484 | ||
485 | return num_found; | |
486 | } | |
487 | ||
5c33c9fd SG |
488 | int fdtdec_get_alias_seq(const void *blob, const char *base, int offset, |
489 | int *seqp) | |
490 | { | |
491 | int base_len = strlen(base); | |
492 | const char *find_name; | |
493 | int find_namelen; | |
494 | int prop_offset; | |
495 | int aliases; | |
496 | ||
497 | find_name = fdt_get_name(blob, offset, &find_namelen); | |
498 | debug("Looking for '%s' at %d, name %s\n", base, offset, find_name); | |
499 | ||
500 | aliases = fdt_path_offset(blob, "/aliases"); | |
501 | for (prop_offset = fdt_first_property_offset(blob, aliases); | |
502 | prop_offset > 0; | |
503 | prop_offset = fdt_next_property_offset(blob, prop_offset)) { | |
504 | const char *prop; | |
505 | const char *name; | |
506 | const char *slash; | |
507 | const char *p; | |
508 | int len; | |
509 | ||
510 | prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len); | |
511 | debug(" - %s, %s\n", name, prop); | |
512 | if (len < find_namelen || *prop != '/' || prop[len - 1] || | |
513 | strncmp(name, base, base_len)) | |
514 | continue; | |
515 | ||
516 | slash = strrchr(prop, '/'); | |
517 | if (strcmp(slash + 1, find_name)) | |
518 | continue; | |
a88340df SG |
519 | for (p = name + strlen(name) - 1; p > name; p--) { |
520 | if (!isdigit(*p)) { | |
521 | *seqp = simple_strtoul(p + 1, NULL, 10); | |
5c33c9fd SG |
522 | debug("Found seq %d\n", *seqp); |
523 | return 0; | |
524 | } | |
525 | } | |
526 | } | |
527 | ||
528 | debug("Not found\n"); | |
529 | return -ENOENT; | |
530 | } | |
531 | ||
aac07d49 SG |
532 | int fdtdec_get_chosen_node(const void *blob, const char *name) |
533 | { | |
534 | const char *prop; | |
535 | int chosen_node; | |
536 | int len; | |
537 | ||
538 | if (!blob) | |
539 | return -FDT_ERR_NOTFOUND; | |
540 | chosen_node = fdt_path_offset(blob, "/chosen"); | |
541 | prop = fdt_getprop(blob, chosen_node, name, &len); | |
542 | if (!prop) | |
543 | return -FDT_ERR_NOTFOUND; | |
544 | return fdt_path_offset(blob, prop); | |
545 | } | |
546 | ||
9a263e55 SG |
547 | int fdtdec_check_fdt(void) |
548 | { | |
549 | /* | |
550 | * We must have an FDT, but we cannot panic() yet since the console | |
551 | * is not ready. So for now, just assert(). Boards which need an early | |
552 | * FDT (prior to console ready) will need to make their own | |
553 | * arrangements and do their own checks. | |
554 | */ | |
555 | assert(!fdtdec_prepare_fdt()); | |
556 | return 0; | |
557 | } | |
558 | ||
b5220bc6 SG |
559 | /* |
560 | * This function is a little odd in that it accesses global data. At some | |
561 | * point if the architecture board.c files merge this will make more sense. | |
562 | * Even now, it is common code. | |
563 | */ | |
9a263e55 | 564 | int fdtdec_prepare_fdt(void) |
b5220bc6 | 565 | { |
c309c2da SG |
566 | if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) || |
567 | fdt_check_header(gd->fdt_blob)) { | |
66312374 SG |
568 | #ifdef CONFIG_SPL_BUILD |
569 | puts("Missing DTB\n"); | |
570 | #else | |
571 | puts("No valid device tree binary found - please append one to U-Boot binary, use u-boot-dtb.bin or define CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n"); | |
572 | #endif | |
9a263e55 SG |
573 | return -1; |
574 | } | |
b5220bc6 SG |
575 | return 0; |
576 | } | |
d17da655 SG |
577 | |
578 | int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name) | |
579 | { | |
580 | const u32 *phandle; | |
581 | int lookup; | |
582 | ||
1cb2323b | 583 | debug("%s: %s\n", __func__, prop_name); |
d17da655 SG |
584 | phandle = fdt_getprop(blob, node, prop_name, NULL); |
585 | if (!phandle) | |
586 | return -FDT_ERR_NOTFOUND; | |
587 | ||
588 | lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle)); | |
589 | return lookup; | |
590 | } | |
591 | ||
592 | /** | |
593 | * Look up a property in a node and check that it has a minimum length. | |
594 | * | |
595 | * @param blob FDT blob | |
596 | * @param node node to examine | |
597 | * @param prop_name name of property to find | |
598 | * @param min_len minimum property length in bytes | |
599 | * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not | |
600 | found, or -FDT_ERR_BADLAYOUT if not enough data | |
601 | * @return pointer to cell, which is only valid if err == 0 | |
602 | */ | |
603 | static const void *get_prop_check_min_len(const void *blob, int node, | |
604 | const char *prop_name, int min_len, int *err) | |
605 | { | |
606 | const void *cell; | |
607 | int len; | |
608 | ||
609 | debug("%s: %s\n", __func__, prop_name); | |
610 | cell = fdt_getprop(blob, node, prop_name, &len); | |
611 | if (!cell) | |
612 | *err = -FDT_ERR_NOTFOUND; | |
613 | else if (len < min_len) | |
614 | *err = -FDT_ERR_BADLAYOUT; | |
615 | else | |
616 | *err = 0; | |
617 | return cell; | |
618 | } | |
619 | ||
620 | int fdtdec_get_int_array(const void *blob, int node, const char *prop_name, | |
621 | u32 *array, int count) | |
622 | { | |
623 | const u32 *cell; | |
624 | int i, err = 0; | |
625 | ||
626 | debug("%s: %s\n", __func__, prop_name); | |
627 | cell = get_prop_check_min_len(blob, node, prop_name, | |
628 | sizeof(u32) * count, &err); | |
629 | if (!err) { | |
630 | for (i = 0; i < count; i++) | |
631 | array[i] = fdt32_to_cpu(cell[i]); | |
632 | } | |
633 | return err; | |
634 | } | |
635 | ||
a9f04d49 SG |
636 | int fdtdec_get_int_array_count(const void *blob, int node, |
637 | const char *prop_name, u32 *array, int count) | |
638 | { | |
639 | const u32 *cell; | |
640 | int len, elems; | |
641 | int i; | |
642 | ||
643 | debug("%s: %s\n", __func__, prop_name); | |
644 | cell = fdt_getprop(blob, node, prop_name, &len); | |
645 | if (!cell) | |
646 | return -FDT_ERR_NOTFOUND; | |
647 | elems = len / sizeof(u32); | |
648 | if (count > elems) | |
649 | count = elems; | |
650 | for (i = 0; i < count; i++) | |
651 | array[i] = fdt32_to_cpu(cell[i]); | |
652 | ||
653 | return count; | |
654 | } | |
655 | ||
96875e7d SG |
656 | const u32 *fdtdec_locate_array(const void *blob, int node, |
657 | const char *prop_name, int count) | |
658 | { | |
659 | const u32 *cell; | |
660 | int err; | |
661 | ||
662 | cell = get_prop_check_min_len(blob, node, prop_name, | |
663 | sizeof(u32) * count, &err); | |
664 | return err ? NULL : cell; | |
665 | } | |
666 | ||
d17da655 SG |
667 | int fdtdec_get_bool(const void *blob, int node, const char *prop_name) |
668 | { | |
669 | const s32 *cell; | |
670 | int len; | |
671 | ||
672 | debug("%s: %s\n", __func__, prop_name); | |
673 | cell = fdt_getprop(blob, node, prop_name, &len); | |
674 | return cell != NULL; | |
675 | } | |
ed3ee5cd | 676 | |
57068a7a SG |
677 | int fdtdec_parse_phandle_with_args(const void *blob, int src_node, |
678 | const char *list_name, | |
679 | const char *cells_name, | |
680 | int cell_count, int index, | |
681 | struct fdtdec_phandle_args *out_args) | |
682 | { | |
683 | const __be32 *list, *list_end; | |
684 | int rc = 0, size, cur_index = 0; | |
685 | uint32_t count = 0; | |
686 | int node = -1; | |
687 | int phandle; | |
688 | ||
689 | /* Retrieve the phandle list property */ | |
690 | list = fdt_getprop(blob, src_node, list_name, &size); | |
691 | if (!list) | |
692 | return -ENOENT; | |
693 | list_end = list + size / sizeof(*list); | |
694 | ||
695 | /* Loop over the phandles until all the requested entry is found */ | |
696 | while (list < list_end) { | |
697 | rc = -EINVAL; | |
698 | count = 0; | |
699 | ||
700 | /* | |
701 | * If phandle is 0, then it is an empty entry with no | |
702 | * arguments. Skip forward to the next entry. | |
703 | */ | |
704 | phandle = be32_to_cpup(list++); | |
705 | if (phandle) { | |
706 | /* | |
707 | * Find the provider node and parse the #*-cells | |
708 | * property to determine the argument length. | |
709 | * | |
710 | * This is not needed if the cell count is hard-coded | |
711 | * (i.e. cells_name not set, but cell_count is set), | |
712 | * except when we're going to return the found node | |
713 | * below. | |
714 | */ | |
715 | if (cells_name || cur_index == index) { | |
716 | node = fdt_node_offset_by_phandle(blob, | |
717 | phandle); | |
718 | if (!node) { | |
719 | debug("%s: could not find phandle\n", | |
720 | fdt_get_name(blob, src_node, | |
721 | NULL)); | |
722 | goto err; | |
723 | } | |
724 | } | |
725 | ||
726 | if (cells_name) { | |
727 | count = fdtdec_get_int(blob, node, cells_name, | |
728 | -1); | |
729 | if (count == -1) { | |
730 | debug("%s: could not get %s for %s\n", | |
731 | fdt_get_name(blob, src_node, | |
732 | NULL), | |
733 | cells_name, | |
734 | fdt_get_name(blob, node, | |
735 | NULL)); | |
736 | goto err; | |
737 | } | |
738 | } else { | |
739 | count = cell_count; | |
740 | } | |
741 | ||
742 | /* | |
743 | * Make sure that the arguments actually fit in the | |
744 | * remaining property data length | |
745 | */ | |
746 | if (list + count > list_end) { | |
747 | debug("%s: arguments longer than property\n", | |
748 | fdt_get_name(blob, src_node, NULL)); | |
749 | goto err; | |
750 | } | |
751 | } | |
752 | ||
753 | /* | |
754 | * All of the error cases above bail out of the loop, so at | |
755 | * this point, the parsing is successful. If the requested | |
756 | * index matches, then fill the out_args structure and return, | |
757 | * or return -ENOENT for an empty entry. | |
758 | */ | |
759 | rc = -ENOENT; | |
760 | if (cur_index == index) { | |
761 | if (!phandle) | |
762 | goto err; | |
763 | ||
764 | if (out_args) { | |
765 | int i; | |
766 | ||
767 | if (count > MAX_PHANDLE_ARGS) { | |
768 | debug("%s: too many arguments %d\n", | |
769 | fdt_get_name(blob, src_node, | |
770 | NULL), count); | |
771 | count = MAX_PHANDLE_ARGS; | |
772 | } | |
773 | out_args->node = node; | |
774 | out_args->args_count = count; | |
775 | for (i = 0; i < count; i++) { | |
776 | out_args->args[i] = | |
777 | be32_to_cpup(list++); | |
778 | } | |
779 | } | |
780 | ||
781 | /* Found it! return success */ | |
782 | return 0; | |
783 | } | |
784 | ||
785 | node = -1; | |
786 | list += count; | |
787 | cur_index++; | |
788 | } | |
789 | ||
790 | /* | |
791 | * Result will be one of: | |
792 | * -ENOENT : index is for empty phandle | |
793 | * -EINVAL : parsing error on data | |
794 | * [1..n] : Number of phandle (count mode; when index = -1) | |
795 | */ | |
796 | rc = index < 0 ? cur_index : -ENOENT; | |
797 | err: | |
798 | return rc; | |
799 | } | |
800 | ||
bed4d892 AS |
801 | int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name, |
802 | u8 *array, int count) | |
803 | { | |
804 | const u8 *cell; | |
805 | int err; | |
806 | ||
807 | cell = get_prop_check_min_len(blob, node, prop_name, count, &err); | |
808 | if (!err) | |
809 | memcpy(array, cell, count); | |
810 | return err; | |
811 | } | |
812 | ||
813 | const u8 *fdtdec_locate_byte_array(const void *blob, int node, | |
814 | const char *prop_name, int count) | |
815 | { | |
816 | const u8 *cell; | |
817 | int err; | |
818 | ||
819 | cell = get_prop_check_min_len(blob, node, prop_name, count, &err); | |
820 | if (err) | |
821 | return NULL; | |
822 | return cell; | |
823 | } | |
09258f1e | 824 | |
09258f1e AK |
825 | int fdtdec_get_config_int(const void *blob, const char *prop_name, |
826 | int default_val) | |
827 | { | |
828 | int config_node; | |
829 | ||
830 | debug("%s: %s\n", __func__, prop_name); | |
831 | config_node = fdt_path_offset(blob, "/config"); | |
832 | if (config_node < 0) | |
833 | return default_val; | |
834 | return fdtdec_get_int(blob, config_node, prop_name, default_val); | |
835 | } | |
332ab0d5 | 836 | |
79289c0b GB |
837 | int fdtdec_get_config_bool(const void *blob, const char *prop_name) |
838 | { | |
839 | int config_node; | |
840 | const void *prop; | |
841 | ||
842 | debug("%s: %s\n", __func__, prop_name); | |
843 | config_node = fdt_path_offset(blob, "/config"); | |
844 | if (config_node < 0) | |
845 | return 0; | |
846 | prop = fdt_get_property(blob, config_node, prop_name, NULL); | |
847 | ||
848 | return prop != NULL; | |
849 | } | |
850 | ||
332ab0d5 SG |
851 | char *fdtdec_get_config_string(const void *blob, const char *prop_name) |
852 | { | |
853 | const char *nodep; | |
854 | int nodeoffset; | |
855 | int len; | |
856 | ||
857 | debug("%s: %s\n", __func__, prop_name); | |
858 | nodeoffset = fdt_path_offset(blob, "/config"); | |
859 | if (nodeoffset < 0) | |
860 | return NULL; | |
861 | ||
862 | nodep = fdt_getprop(blob, nodeoffset, prop_name, &len); | |
863 | if (!nodep) | |
864 | return NULL; | |
865 | ||
866 | return (char *)nodep; | |
867 | } | |
f20c4619 | 868 | |
76489832 SG |
869 | int fdtdec_decode_region(const void *blob, int node, const char *prop_name, |
870 | fdt_addr_t *basep, fdt_size_t *sizep) | |
f20c4619 SG |
871 | { |
872 | const fdt_addr_t *cell; | |
873 | int len; | |
874 | ||
76489832 SG |
875 | debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL), |
876 | prop_name); | |
f20c4619 | 877 | cell = fdt_getprop(blob, node, prop_name, &len); |
76489832 SG |
878 | if (!cell || (len < sizeof(fdt_addr_t) * 2)) { |
879 | debug("cell=%p, len=%d\n", cell, len); | |
f20c4619 | 880 | return -1; |
76489832 SG |
881 | } |
882 | ||
883 | *basep = fdt_addr_to_cpu(*cell); | |
884 | *sizep = fdt_size_to_cpu(cell[1]); | |
885 | debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep, | |
886 | (ulong)*sizep); | |
f20c4619 | 887 | |
f20c4619 SG |
888 | return 0; |
889 | } | |
006e73b9 SG |
890 | |
891 | /** | |
892 | * Read a flash entry from the fdt | |
893 | * | |
894 | * @param blob FDT blob | |
895 | * @param node Offset of node to read | |
896 | * @param name Name of node being read | |
897 | * @param entry Place to put offset and size of this node | |
898 | * @return 0 if ok, -ve on error | |
899 | */ | |
900 | int fdtdec_read_fmap_entry(const void *blob, int node, const char *name, | |
901 | struct fmap_entry *entry) | |
902 | { | |
f3cc44f9 | 903 | const char *prop; |
006e73b9 SG |
904 | u32 reg[2]; |
905 | ||
906 | if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) { | |
907 | debug("Node '%s' has bad/missing 'reg' property\n", name); | |
908 | return -FDT_ERR_NOTFOUND; | |
909 | } | |
910 | entry->offset = reg[0]; | |
911 | entry->length = reg[1]; | |
f3cc44f9 SG |
912 | entry->used = fdtdec_get_int(blob, node, "used", entry->length); |
913 | prop = fdt_getprop(blob, node, "compress", NULL); | |
914 | entry->compress_algo = prop && !strcmp(prop, "lzo") ? | |
915 | FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE; | |
916 | prop = fdt_getprop(blob, node, "hash", &entry->hash_size); | |
917 | entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE; | |
918 | entry->hash = (uint8_t *)prop; | |
006e73b9 SG |
919 | |
920 | return 0; | |
921 | } | |
56f42242 | 922 | |
5f7bfdd6 | 923 | u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells) |
56f42242 TR |
924 | { |
925 | u64 number = 0; | |
926 | ||
927 | while (cells--) | |
928 | number = (number << 32) | fdt32_to_cpu(*ptr++); | |
929 | ||
930 | return number; | |
931 | } | |
932 | ||
933 | int fdt_get_resource(const void *fdt, int node, const char *property, | |
934 | unsigned int index, struct fdt_resource *res) | |
935 | { | |
936 | const fdt32_t *ptr, *end; | |
937 | int na, ns, len, parent; | |
938 | unsigned int i = 0; | |
939 | ||
940 | parent = fdt_parent_offset(fdt, node); | |
941 | if (parent < 0) | |
942 | return parent; | |
943 | ||
944 | na = fdt_address_cells(fdt, parent); | |
945 | ns = fdt_size_cells(fdt, parent); | |
946 | ||
947 | ptr = fdt_getprop(fdt, node, property, &len); | |
948 | if (!ptr) | |
949 | return len; | |
950 | ||
951 | end = ptr + len / sizeof(*ptr); | |
952 | ||
953 | while (ptr + na + ns <= end) { | |
954 | if (i == index) { | |
955 | res->start = res->end = fdtdec_get_number(ptr, na); | |
956 | res->end += fdtdec_get_number(&ptr[na], ns) - 1; | |
957 | return 0; | |
958 | } | |
959 | ||
960 | ptr += na + ns; | |
961 | i++; | |
962 | } | |
963 | ||
964 | return -FDT_ERR_NOTFOUND; | |
965 | } | |
966 | ||
967 | int fdt_get_named_resource(const void *fdt, int node, const char *property, | |
968 | const char *prop_names, const char *name, | |
969 | struct fdt_resource *res) | |
970 | { | |
971 | int index; | |
972 | ||
973 | index = fdt_find_string(fdt, node, prop_names, name); | |
974 | if (index < 0) | |
975 | return index; | |
976 | ||
977 | return fdt_get_resource(fdt, node, property, index, res); | |
978 | } | |
9f85eee7 | 979 | |
26403871 SG |
980 | int fdtdec_decode_memory_region(const void *blob, int config_node, |
981 | const char *mem_type, const char *suffix, | |
982 | fdt_addr_t *basep, fdt_size_t *sizep) | |
983 | { | |
984 | char prop_name[50]; | |
985 | const char *mem; | |
986 | fdt_size_t size, offset_size; | |
987 | fdt_addr_t base, offset; | |
988 | int node; | |
989 | ||
990 | if (config_node == -1) { | |
991 | config_node = fdt_path_offset(blob, "/config"); | |
992 | if (config_node < 0) { | |
993 | debug("%s: Cannot find /config node\n", __func__); | |
994 | return -ENOENT; | |
995 | } | |
996 | } | |
997 | if (!suffix) | |
998 | suffix = ""; | |
999 | ||
1000 | snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type, | |
1001 | suffix); | |
1002 | mem = fdt_getprop(blob, config_node, prop_name, NULL); | |
1003 | if (!mem) { | |
1004 | debug("%s: No memory type for '%s', using /memory\n", __func__, | |
1005 | prop_name); | |
1006 | mem = "/memory"; | |
1007 | } | |
1008 | ||
1009 | node = fdt_path_offset(blob, mem); | |
1010 | if (node < 0) { | |
1011 | debug("%s: Failed to find node '%s': %s\n", __func__, mem, | |
1012 | fdt_strerror(node)); | |
1013 | return -ENOENT; | |
1014 | } | |
1015 | ||
1016 | /* | |
1017 | * Not strictly correct - the memory may have multiple banks. We just | |
1018 | * use the first | |
1019 | */ | |
1020 | if (fdtdec_decode_region(blob, node, "reg", &base, &size)) { | |
1021 | debug("%s: Failed to decode memory region %s\n", __func__, | |
1022 | mem); | |
1023 | return -EINVAL; | |
1024 | } | |
1025 | ||
1026 | snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type, | |
1027 | suffix); | |
1028 | if (fdtdec_decode_region(blob, config_node, prop_name, &offset, | |
1029 | &offset_size)) { | |
1030 | debug("%s: Failed to decode memory region '%s'\n", __func__, | |
1031 | prop_name); | |
1032 | return -EINVAL; | |
1033 | } | |
1034 | ||
1035 | *basep = base + offset; | |
1036 | *sizep = offset_size; | |
1037 | ||
1038 | return 0; | |
1039 | } | |
29a23f9d | 1040 | #endif |