fdt: samsung: Drop unused fdt_compat_id values
[oweals/u-boot.git] / lib / fdtdec.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011 The Chromium OS Authors.
4  */
5
6 #ifndef USE_HOSTCC
7 #include <common.h>
8 #include <boot_fit.h>
9 #include <dm.h>
10 #include <dm/of_extra.h>
11 #include <errno.h>
12 #include <fdtdec.h>
13 #include <fdt_support.h>
14 #include <mapmem.h>
15 #include <linux/libfdt.h>
16 #include <serial.h>
17 #include <asm/sections.h>
18 #include <linux/ctype.h>
19 #include <linux/lzo.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 /*
24  * Here are the type we know about. One day we might allow drivers to
25  * register. For now we just put them here. The COMPAT macro allows us to
26  * turn this into a sparse list later, and keeps the ID with the name.
27  *
28  * NOTE: This list is basically a TODO list for things that need to be
29  * converted to driver model. So don't add new things here unless there is a
30  * good reason why driver-model conversion is infeasible. Examples include
31  * things which are used before driver model is available.
32  */
33 #define COMPAT(id, name) name
34 static const char * const compat_names[COMPAT_COUNT] = {
35         COMPAT(UNKNOWN, "<none>"),
36         COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
37         COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
38         COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
39         COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
40         COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
41         COMPAT(SMSC_LAN9215, "smsc,lan9215"),
42         COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
43         COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
44         COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
45         COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
46         COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
47         COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
48         COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
49         COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
50         COMPAT(INTEL_MICROCODE, "intel,microcode"),
51         COMPAT(AMS_AS3722, "ams,as3722"),
52         COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
53         COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
54         COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"),
55         COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"),
56         COMPAT(INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
57         COMPAT(INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
58         COMPAT(INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"),
59         COMPAT(COMPAT_SUNXI_NAND, "allwinner,sun4i-a10-nand"),
60         COMPAT(ALTERA_SOCFPGA_CLK, "altr,clk-mgr"),
61         COMPAT(ALTERA_SOCFPGA_PINCTRL_SINGLE, "pinctrl-single"),
62         COMPAT(ALTERA_SOCFPGA_H2F_BRG, "altr,socfpga-hps2fpga-bridge"),
63         COMPAT(ALTERA_SOCFPGA_LWH2F_BRG, "altr,socfpga-lwhps2fpga-bridge"),
64         COMPAT(ALTERA_SOCFPGA_F2H_BRG, "altr,socfpga-fpga2hps-bridge"),
65         COMPAT(ALTERA_SOCFPGA_F2SDR0, "altr,socfpga-fpga2sdram0-bridge"),
66         COMPAT(ALTERA_SOCFPGA_F2SDR1, "altr,socfpga-fpga2sdram1-bridge"),
67         COMPAT(ALTERA_SOCFPGA_F2SDR2, "altr,socfpga-fpga2sdram2-bridge"),
68         COMPAT(ALTERA_SOCFPGA_FPGA0, "altr,socfpga-a10-fpga-mgr"),
69         COMPAT(ALTERA_SOCFPGA_NOC, "altr,socfpga-a10-noc"),
70         COMPAT(ALTERA_SOCFPGA_CLK_INIT, "altr,socfpga-a10-clk-init")
71 };
72
73 const char *fdtdec_get_compatible(enum fdt_compat_id id)
74 {
75         /* We allow reading of the 'unknown' ID for testing purposes */
76         assert(id >= 0 && id < COMPAT_COUNT);
77         return compat_names[id];
78 }
79
80 fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
81                                       const char *prop_name, int index, int na,
82                                       int ns, fdt_size_t *sizep,
83                                       bool translate)
84 {
85         const fdt32_t *prop, *prop_end;
86         const fdt32_t *prop_addr, *prop_size, *prop_after_size;
87         int len;
88         fdt_addr_t addr;
89
90         debug("%s: %s: ", __func__, prop_name);
91
92         prop = fdt_getprop(blob, node, prop_name, &len);
93         if (!prop) {
94                 debug("(not found)\n");
95                 return FDT_ADDR_T_NONE;
96         }
97         prop_end = prop + (len / sizeof(*prop));
98
99         prop_addr = prop + (index * (na + ns));
100         prop_size = prop_addr + na;
101         prop_after_size = prop_size + ns;
102         if (prop_after_size > prop_end) {
103                 debug("(not enough data: expected >= %d cells, got %d cells)\n",
104                       (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
105                 return FDT_ADDR_T_NONE;
106         }
107
108 #if CONFIG_IS_ENABLED(OF_TRANSLATE)
109         if (translate)
110                 addr = fdt_translate_address(blob, node, prop_addr);
111         else
112 #endif
113                 addr = fdtdec_get_number(prop_addr, na);
114
115         if (sizep) {
116                 *sizep = fdtdec_get_number(prop_size, ns);
117                 debug("addr=%08llx, size=%llx\n", (unsigned long long)addr,
118                       (unsigned long long)*sizep);
119         } else {
120                 debug("addr=%08llx\n", (unsigned long long)addr);
121         }
122
123         return addr;
124 }
125
126 fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
127                                             int node, const char *prop_name,
128                                             int index, fdt_size_t *sizep,
129                                             bool translate)
130 {
131         int na, ns;
132
133         debug("%s: ", __func__);
134
135         na = fdt_address_cells(blob, parent);
136         if (na < 1) {
137                 debug("(bad #address-cells)\n");
138                 return FDT_ADDR_T_NONE;
139         }
140
141         ns = fdt_size_cells(blob, parent);
142         if (ns < 0) {
143                 debug("(bad #size-cells)\n");
144                 return FDT_ADDR_T_NONE;
145         }
146
147         debug("na=%d, ns=%d, ", na, ns);
148
149         return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na,
150                                           ns, sizep, translate);
151 }
152
153 fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
154                                               const char *prop_name, int index,
155                                               fdt_size_t *sizep,
156                                               bool translate)
157 {
158         int parent;
159
160         debug("%s: ", __func__);
161
162         parent = fdt_parent_offset(blob, node);
163         if (parent < 0) {
164                 debug("(no parent found)\n");
165                 return FDT_ADDR_T_NONE;
166         }
167
168         return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name,
169                                                 index, sizep, translate);
170 }
171
172 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
173                                 const char *prop_name, fdt_size_t *sizep)
174 {
175         int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0;
176
177         return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0,
178                                           sizeof(fdt_addr_t) / sizeof(fdt32_t),
179                                           ns, sizep, false);
180 }
181
182 fdt_addr_t fdtdec_get_addr(const void *blob, int node, const char *prop_name)
183 {
184         return fdtdec_get_addr_size(blob, node, prop_name, NULL);
185 }
186
187 #if CONFIG_IS_ENABLED(PCI) && defined(CONFIG_DM_PCI)
188 int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
189                         const char *prop_name, struct fdt_pci_addr *addr)
190 {
191         const u32 *cell;
192         int len;
193         int ret = -ENOENT;
194
195         debug("%s: %s: ", __func__, prop_name);
196
197         /*
198          * If we follow the pci bus bindings strictly, we should check
199          * the value of the node's parent node's #address-cells and
200          * #size-cells. They need to be 3 and 2 accordingly. However,
201          * for simplicity we skip the check here.
202          */
203         cell = fdt_getprop(blob, node, prop_name, &len);
204         if (!cell)
205                 goto fail;
206
207         if ((len % FDT_PCI_REG_SIZE) == 0) {
208                 int num = len / FDT_PCI_REG_SIZE;
209                 int i;
210
211                 for (i = 0; i < num; i++) {
212                         debug("pci address #%d: %08lx %08lx %08lx\n", i,
213                               (ulong)fdt32_to_cpu(cell[0]),
214                               (ulong)fdt32_to_cpu(cell[1]),
215                               (ulong)fdt32_to_cpu(cell[2]));
216                         if ((fdt32_to_cpu(*cell) & type) == type) {
217                                 addr->phys_hi = fdt32_to_cpu(cell[0]);
218                                 addr->phys_mid = fdt32_to_cpu(cell[1]);
219                                 addr->phys_lo = fdt32_to_cpu(cell[1]);
220                                 break;
221                         }
222
223                         cell += (FDT_PCI_ADDR_CELLS +
224                                  FDT_PCI_SIZE_CELLS);
225                 }
226
227                 if (i == num) {
228                         ret = -ENXIO;
229                         goto fail;
230                 }
231
232                 return 0;
233         }
234
235         ret = -EINVAL;
236
237 fail:
238         debug("(not found)\n");
239         return ret;
240 }
241
242 int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
243 {
244         const char *list, *end;
245         int len;
246
247         list = fdt_getprop(blob, node, "compatible", &len);
248         if (!list)
249                 return -ENOENT;
250
251         end = list + len;
252         while (list < end) {
253                 len = strlen(list);
254                 if (len >= strlen("pciVVVV,DDDD")) {
255                         char *s = strstr(list, "pci");
256
257                         /*
258                          * check if the string is something like pciVVVV,DDDD.RR
259                          * or just pciVVVV,DDDD
260                          */
261                         if (s && s[7] == ',' &&
262                             (s[12] == '.' || s[12] == 0)) {
263                                 s += 3;
264                                 *vendor = simple_strtol(s, NULL, 16);
265
266                                 s += 5;
267                                 *device = simple_strtol(s, NULL, 16);
268
269                                 return 0;
270                         }
271                 }
272                 list += (len + 1);
273         }
274
275         return -ENOENT;
276 }
277
278 int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
279                          u32 *bar)
280 {
281         int barnum;
282
283         /* extract the bar number from fdt_pci_addr */
284         barnum = addr->phys_hi & 0xff;
285         if (barnum < PCI_BASE_ADDRESS_0 || barnum > PCI_CARDBUS_CIS)
286                 return -EINVAL;
287
288         barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
289         *bar = dm_pci_read_bar32(dev, barnum);
290
291         return 0;
292 }
293 #endif
294
295 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
296                            uint64_t default_val)
297 {
298         const uint64_t *cell64;
299         int length;
300
301         cell64 = fdt_getprop(blob, node, prop_name, &length);
302         if (!cell64 || length < sizeof(*cell64))
303                 return default_val;
304
305         return fdt64_to_cpu(*cell64);
306 }
307
308 int fdtdec_get_is_enabled(const void *blob, int node)
309 {
310         const char *cell;
311
312         /*
313          * It should say "okay", so only allow that. Some fdts use "ok" but
314          * this is a bug. Please fix your device tree source file. See here
315          * for discussion:
316          *
317          * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
318          */
319         cell = fdt_getprop(blob, node, "status", NULL);
320         if (cell)
321                 return strcmp(cell, "okay") == 0;
322         return 1;
323 }
324
325 enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
326 {
327         enum fdt_compat_id id;
328
329         /* Search our drivers */
330         for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
331                 if (fdt_node_check_compatible(blob, node,
332                                               compat_names[id]) == 0)
333                         return id;
334         return COMPAT_UNKNOWN;
335 }
336
337 int fdtdec_next_compatible(const void *blob, int node, enum fdt_compat_id id)
338 {
339         return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
340 }
341
342 int fdtdec_next_compatible_subnode(const void *blob, int node,
343                                    enum fdt_compat_id id, int *depthp)
344 {
345         do {
346                 node = fdt_next_node(blob, node, depthp);
347         } while (*depthp > 1);
348
349         /* If this is a direct subnode, and compatible, return it */
350         if (*depthp == 1 && 0 == fdt_node_check_compatible(
351                                                 blob, node, compat_names[id]))
352                 return node;
353
354         return -FDT_ERR_NOTFOUND;
355 }
356
357 int fdtdec_next_alias(const void *blob, const char *name, enum fdt_compat_id id,
358                       int *upto)
359 {
360 #define MAX_STR_LEN 20
361         char str[MAX_STR_LEN + 20];
362         int node, err;
363
364         /* snprintf() is not available */
365         assert(strlen(name) < MAX_STR_LEN);
366         sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
367         node = fdt_path_offset(blob, str);
368         if (node < 0)
369                 return node;
370         err = fdt_node_check_compatible(blob, node, compat_names[id]);
371         if (err < 0)
372                 return err;
373         if (err)
374                 return -FDT_ERR_NOTFOUND;
375         (*upto)++;
376         return node;
377 }
378
379 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
380                                enum fdt_compat_id id, int *node_list,
381                                int maxcount)
382 {
383         memset(node_list, '\0', sizeof(*node_list) * maxcount);
384
385         return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
386 }
387
388 /* TODO: Can we tighten this code up a little? */
389 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
390                               enum fdt_compat_id id, int *node_list,
391                               int maxcount)
392 {
393         int name_len = strlen(name);
394         int nodes[maxcount];
395         int num_found = 0;
396         int offset, node;
397         int alias_node;
398         int count;
399         int i, j;
400
401         /* find the alias node if present */
402         alias_node = fdt_path_offset(blob, "/aliases");
403
404         /*
405          * start with nothing, and we can assume that the root node can't
406          * match
407          */
408         memset(nodes, '\0', sizeof(nodes));
409
410         /* First find all the compatible nodes */
411         for (node = count = 0; node >= 0 && count < maxcount;) {
412                 node = fdtdec_next_compatible(blob, node, id);
413                 if (node >= 0)
414                         nodes[count++] = node;
415         }
416         if (node >= 0)
417                 debug("%s: warning: maxcount exceeded with alias '%s'\n",
418                       __func__, name);
419
420         /* Now find all the aliases */
421         for (offset = fdt_first_property_offset(blob, alias_node);
422                         offset > 0;
423                         offset = fdt_next_property_offset(blob, offset)) {
424                 const struct fdt_property *prop;
425                 const char *path;
426                 int number;
427                 int found;
428
429                 node = 0;
430                 prop = fdt_get_property_by_offset(blob, offset, NULL);
431                 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
432                 if (prop->len && 0 == strncmp(path, name, name_len))
433                         node = fdt_path_offset(blob, prop->data);
434                 if (node <= 0)
435                         continue;
436
437                 /* Get the alias number */
438                 number = simple_strtoul(path + name_len, NULL, 10);
439                 if (number < 0 || number >= maxcount) {
440                         debug("%s: warning: alias '%s' is out of range\n",
441                               __func__, path);
442                         continue;
443                 }
444
445                 /* Make sure the node we found is actually in our list! */
446                 found = -1;
447                 for (j = 0; j < count; j++)
448                         if (nodes[j] == node) {
449                                 found = j;
450                                 break;
451                         }
452
453                 if (found == -1) {
454                         debug("%s: warning: alias '%s' points to a node "
455                                 "'%s' that is missing or is not compatible "
456                                 " with '%s'\n", __func__, path,
457                                 fdt_get_name(blob, node, NULL),
458                                compat_names[id]);
459                         continue;
460                 }
461
462                 /*
463                  * Add this node to our list in the right place, and mark
464                  * it as done.
465                  */
466                 if (fdtdec_get_is_enabled(blob, node)) {
467                         if (node_list[number]) {
468                                 debug("%s: warning: alias '%s' requires that "
469                                       "a node be placed in the list in a "
470                                       "position which is already filled by "
471                                       "node '%s'\n", __func__, path,
472                                       fdt_get_name(blob, node, NULL));
473                                 continue;
474                         }
475                         node_list[number] = node;
476                         if (number >= num_found)
477                                 num_found = number + 1;
478                 }
479                 nodes[found] = 0;
480         }
481
482         /* Add any nodes not mentioned by an alias */
483         for (i = j = 0; i < maxcount; i++) {
484                 if (!node_list[i]) {
485                         for (; j < maxcount; j++)
486                                 if (nodes[j] &&
487                                     fdtdec_get_is_enabled(blob, nodes[j]))
488                                         break;
489
490                         /* Have we run out of nodes to add? */
491                         if (j == maxcount)
492                                 break;
493
494                         assert(!node_list[i]);
495                         node_list[i] = nodes[j++];
496                         if (i >= num_found)
497                                 num_found = i + 1;
498                 }
499         }
500
501         return num_found;
502 }
503
504 int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
505                          int *seqp)
506 {
507         int base_len = strlen(base);
508         const char *find_name;
509         int find_namelen;
510         int prop_offset;
511         int aliases;
512
513         find_name = fdt_get_name(blob, offset, &find_namelen);
514         debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
515
516         aliases = fdt_path_offset(blob, "/aliases");
517         for (prop_offset = fdt_first_property_offset(blob, aliases);
518              prop_offset > 0;
519              prop_offset = fdt_next_property_offset(blob, prop_offset)) {
520                 const char *prop;
521                 const char *name;
522                 const char *slash;
523                 int len, val;
524
525                 prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
526                 debug("   - %s, %s\n", name, prop);
527                 if (len < find_namelen || *prop != '/' || prop[len - 1] ||
528                     strncmp(name, base, base_len))
529                         continue;
530
531                 slash = strrchr(prop, '/');
532                 if (strcmp(slash + 1, find_name))
533                         continue;
534                 val = trailing_strtol(name);
535                 if (val != -1) {
536                         *seqp = val;
537                         debug("Found seq %d\n", *seqp);
538                         return 0;
539                 }
540         }
541
542         debug("Not found\n");
543         return -ENOENT;
544 }
545
546 const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
547 {
548         int chosen_node;
549
550         if (!blob)
551                 return NULL;
552         chosen_node = fdt_path_offset(blob, "/chosen");
553         return fdt_getprop(blob, chosen_node, name, NULL);
554 }
555
556 int fdtdec_get_chosen_node(const void *blob, const char *name)
557 {
558         const char *prop;
559
560         prop = fdtdec_get_chosen_prop(blob, name);
561         if (!prop)
562                 return -FDT_ERR_NOTFOUND;
563         return fdt_path_offset(blob, prop);
564 }
565
566 int fdtdec_check_fdt(void)
567 {
568         /*
569          * We must have an FDT, but we cannot panic() yet since the console
570          * is not ready. So for now, just assert(). Boards which need an early
571          * FDT (prior to console ready) will need to make their own
572          * arrangements and do their own checks.
573          */
574         assert(!fdtdec_prepare_fdt());
575         return 0;
576 }
577
578 /*
579  * This function is a little odd in that it accesses global data. At some
580  * point if the architecture board.c files merge this will make more sense.
581  * Even now, it is common code.
582  */
583 int fdtdec_prepare_fdt(void)
584 {
585         if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
586             fdt_check_header(gd->fdt_blob)) {
587 #ifdef CONFIG_SPL_BUILD
588                 puts("Missing DTB\n");
589 #else
590                 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");
591 # ifdef DEBUG
592                 if (gd->fdt_blob) {
593                         printf("fdt_blob=%p\n", gd->fdt_blob);
594                         print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4,
595                                      32, 0);
596                 }
597 # endif
598 #endif
599                 return -1;
600         }
601         return 0;
602 }
603
604 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
605 {
606         const u32 *phandle;
607         int lookup;
608
609         debug("%s: %s\n", __func__, prop_name);
610         phandle = fdt_getprop(blob, node, prop_name, NULL);
611         if (!phandle)
612                 return -FDT_ERR_NOTFOUND;
613
614         lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
615         return lookup;
616 }
617
618 /**
619  * Look up a property in a node and check that it has a minimum length.
620  *
621  * @param blob          FDT blob
622  * @param node          node to examine
623  * @param prop_name     name of property to find
624  * @param min_len       minimum property length in bytes
625  * @param err           0 if ok, or -FDT_ERR_NOTFOUND if the property is not
626                         found, or -FDT_ERR_BADLAYOUT if not enough data
627  * @return pointer to cell, which is only valid if err == 0
628  */
629 static const void *get_prop_check_min_len(const void *blob, int node,
630                                           const char *prop_name, int min_len,
631                                           int *err)
632 {
633         const void *cell;
634         int len;
635
636         debug("%s: %s\n", __func__, prop_name);
637         cell = fdt_getprop(blob, node, prop_name, &len);
638         if (!cell)
639                 *err = -FDT_ERR_NOTFOUND;
640         else if (len < min_len)
641                 *err = -FDT_ERR_BADLAYOUT;
642         else
643                 *err = 0;
644         return cell;
645 }
646
647 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
648                          u32 *array, int count)
649 {
650         const u32 *cell;
651         int err = 0;
652
653         debug("%s: %s\n", __func__, prop_name);
654         cell = get_prop_check_min_len(blob, node, prop_name,
655                                       sizeof(u32) * count, &err);
656         if (!err) {
657                 int i;
658
659                 for (i = 0; i < count; i++)
660                         array[i] = fdt32_to_cpu(cell[i]);
661         }
662         return err;
663 }
664
665 int fdtdec_get_int_array_count(const void *blob, int node,
666                                const char *prop_name, u32 *array, int count)
667 {
668         const u32 *cell;
669         int len, elems;
670         int i;
671
672         debug("%s: %s\n", __func__, prop_name);
673         cell = fdt_getprop(blob, node, prop_name, &len);
674         if (!cell)
675                 return -FDT_ERR_NOTFOUND;
676         elems = len / sizeof(u32);
677         if (count > elems)
678                 count = elems;
679         for (i = 0; i < count; i++)
680                 array[i] = fdt32_to_cpu(cell[i]);
681
682         return count;
683 }
684
685 const u32 *fdtdec_locate_array(const void *blob, int node,
686                                const char *prop_name, int count)
687 {
688         const u32 *cell;
689         int err;
690
691         cell = get_prop_check_min_len(blob, node, prop_name,
692                                       sizeof(u32) * count, &err);
693         return err ? NULL : cell;
694 }
695
696 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
697 {
698         const s32 *cell;
699         int len;
700
701         debug("%s: %s\n", __func__, prop_name);
702         cell = fdt_getprop(blob, node, prop_name, &len);
703         return cell != NULL;
704 }
705
706 int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
707                                    const char *list_name,
708                                    const char *cells_name,
709                                    int cell_count, int index,
710                                    struct fdtdec_phandle_args *out_args)
711 {
712         const __be32 *list, *list_end;
713         int rc = 0, size, cur_index = 0;
714         uint32_t count = 0;
715         int node = -1;
716         int phandle;
717
718         /* Retrieve the phandle list property */
719         list = fdt_getprop(blob, src_node, list_name, &size);
720         if (!list)
721                 return -ENOENT;
722         list_end = list + size / sizeof(*list);
723
724         /* Loop over the phandles until all the requested entry is found */
725         while (list < list_end) {
726                 rc = -EINVAL;
727                 count = 0;
728
729                 /*
730                  * If phandle is 0, then it is an empty entry with no
731                  * arguments.  Skip forward to the next entry.
732                  */
733                 phandle = be32_to_cpup(list++);
734                 if (phandle) {
735                         /*
736                          * Find the provider node and parse the #*-cells
737                          * property to determine the argument length.
738                          *
739                          * This is not needed if the cell count is hard-coded
740                          * (i.e. cells_name not set, but cell_count is set),
741                          * except when we're going to return the found node
742                          * below.
743                          */
744                         if (cells_name || cur_index == index) {
745                                 node = fdt_node_offset_by_phandle(blob,
746                                                                   phandle);
747                                 if (!node) {
748                                         debug("%s: could not find phandle\n",
749                                               fdt_get_name(blob, src_node,
750                                                            NULL));
751                                         goto err;
752                                 }
753                         }
754
755                         if (cells_name) {
756                                 count = fdtdec_get_int(blob, node, cells_name,
757                                                        -1);
758                                 if (count == -1) {
759                                         debug("%s: could not get %s for %s\n",
760                                               fdt_get_name(blob, src_node,
761                                                            NULL),
762                                               cells_name,
763                                               fdt_get_name(blob, node,
764                                                            NULL));
765                                         goto err;
766                                 }
767                         } else {
768                                 count = cell_count;
769                         }
770
771                         /*
772                          * Make sure that the arguments actually fit in the
773                          * remaining property data length
774                          */
775                         if (list + count > list_end) {
776                                 debug("%s: arguments longer than property\n",
777                                       fdt_get_name(blob, src_node, NULL));
778                                 goto err;
779                         }
780                 }
781
782                 /*
783                  * All of the error cases above bail out of the loop, so at
784                  * this point, the parsing is successful. If the requested
785                  * index matches, then fill the out_args structure and return,
786                  * or return -ENOENT for an empty entry.
787                  */
788                 rc = -ENOENT;
789                 if (cur_index == index) {
790                         if (!phandle)
791                                 goto err;
792
793                         if (out_args) {
794                                 int i;
795
796                                 if (count > MAX_PHANDLE_ARGS) {
797                                         debug("%s: too many arguments %d\n",
798                                               fdt_get_name(blob, src_node,
799                                                            NULL), count);
800                                         count = MAX_PHANDLE_ARGS;
801                                 }
802                                 out_args->node = node;
803                                 out_args->args_count = count;
804                                 for (i = 0; i < count; i++) {
805                                         out_args->args[i] =
806                                                         be32_to_cpup(list++);
807                                 }
808                         }
809
810                         /* Found it! return success */
811                         return 0;
812                 }
813
814                 node = -1;
815                 list += count;
816                 cur_index++;
817         }
818
819         /*
820          * Result will be one of:
821          * -ENOENT : index is for empty phandle
822          * -EINVAL : parsing error on data
823          * [1..n]  : Number of phandle (count mode; when index = -1)
824          */
825         rc = index < 0 ? cur_index : -ENOENT;
826  err:
827         return rc;
828 }
829
830 int fdtdec_get_child_count(const void *blob, int node)
831 {
832         int subnode;
833         int num = 0;
834
835         fdt_for_each_subnode(subnode, blob, node)
836                 num++;
837
838         return num;
839 }
840
841 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
842                           u8 *array, int count)
843 {
844         const u8 *cell;
845         int err;
846
847         cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
848         if (!err)
849                 memcpy(array, cell, count);
850         return err;
851 }
852
853 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
854                                    const char *prop_name, int count)
855 {
856         const u8 *cell;
857         int err;
858
859         cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
860         if (err)
861                 return NULL;
862         return cell;
863 }
864
865 int fdtdec_get_config_int(const void *blob, const char *prop_name,
866                           int default_val)
867 {
868         int config_node;
869
870         debug("%s: %s\n", __func__, prop_name);
871         config_node = fdt_path_offset(blob, "/config");
872         if (config_node < 0)
873                 return default_val;
874         return fdtdec_get_int(blob, config_node, prop_name, default_val);
875 }
876
877 int fdtdec_get_config_bool(const void *blob, const char *prop_name)
878 {
879         int config_node;
880         const void *prop;
881
882         debug("%s: %s\n", __func__, prop_name);
883         config_node = fdt_path_offset(blob, "/config");
884         if (config_node < 0)
885                 return 0;
886         prop = fdt_get_property(blob, config_node, prop_name, NULL);
887
888         return prop != NULL;
889 }
890
891 char *fdtdec_get_config_string(const void *blob, const char *prop_name)
892 {
893         const char *nodep;
894         int nodeoffset;
895         int len;
896
897         debug("%s: %s\n", __func__, prop_name);
898         nodeoffset = fdt_path_offset(blob, "/config");
899         if (nodeoffset < 0)
900                 return NULL;
901
902         nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
903         if (!nodep)
904                 return NULL;
905
906         return (char *)nodep;
907 }
908
909 u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
910 {
911         u64 number = 0;
912
913         while (cells--)
914                 number = (number << 32) | fdt32_to_cpu(*ptr++);
915
916         return number;
917 }
918
919 int fdt_get_resource(const void *fdt, int node, const char *property,
920                      unsigned int index, struct fdt_resource *res)
921 {
922         const fdt32_t *ptr, *end;
923         int na, ns, len, parent;
924         unsigned int i = 0;
925
926         parent = fdt_parent_offset(fdt, node);
927         if (parent < 0)
928                 return parent;
929
930         na = fdt_address_cells(fdt, parent);
931         ns = fdt_size_cells(fdt, parent);
932
933         ptr = fdt_getprop(fdt, node, property, &len);
934         if (!ptr)
935                 return len;
936
937         end = ptr + len / sizeof(*ptr);
938
939         while (ptr + na + ns <= end) {
940                 if (i == index) {
941                         res->start = fdtdec_get_number(ptr, na);
942                         res->end = res->start;
943                         res->end += fdtdec_get_number(&ptr[na], ns) - 1;
944                         return 0;
945                 }
946
947                 ptr += na + ns;
948                 i++;
949         }
950
951         return -FDT_ERR_NOTFOUND;
952 }
953
954 int fdt_get_named_resource(const void *fdt, int node, const char *property,
955                            const char *prop_names, const char *name,
956                            struct fdt_resource *res)
957 {
958         int index;
959
960         index = fdt_stringlist_search(fdt, node, prop_names, name);
961         if (index < 0)
962                 return index;
963
964         return fdt_get_resource(fdt, node, property, index, res);
965 }
966
967 static int decode_timing_property(const void *blob, int node, const char *name,
968                                   struct timing_entry *result)
969 {
970         int length, ret = 0;
971         const u32 *prop;
972
973         prop = fdt_getprop(blob, node, name, &length);
974         if (!prop) {
975                 debug("%s: could not find property %s\n",
976                       fdt_get_name(blob, node, NULL), name);
977                 return length;
978         }
979
980         if (length == sizeof(u32)) {
981                 result->typ = fdtdec_get_int(blob, node, name, 0);
982                 result->min = result->typ;
983                 result->max = result->typ;
984         } else {
985                 ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
986         }
987
988         return ret;
989 }
990
991 int fdtdec_decode_display_timing(const void *blob, int parent, int index,
992                                  struct display_timing *dt)
993 {
994         int i, node, timings_node;
995         u32 val = 0;
996         int ret = 0;
997
998         timings_node = fdt_subnode_offset(blob, parent, "display-timings");
999         if (timings_node < 0)
1000                 return timings_node;
1001
1002         for (i = 0, node = fdt_first_subnode(blob, timings_node);
1003              node > 0 && i != index;
1004              node = fdt_next_subnode(blob, node))
1005                 i++;
1006
1007         if (node < 0)
1008                 return node;
1009
1010         memset(dt, 0, sizeof(*dt));
1011
1012         ret |= decode_timing_property(blob, node, "hback-porch",
1013                                       &dt->hback_porch);
1014         ret |= decode_timing_property(blob, node, "hfront-porch",
1015                                       &dt->hfront_porch);
1016         ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
1017         ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
1018         ret |= decode_timing_property(blob, node, "vback-porch",
1019                                       &dt->vback_porch);
1020         ret |= decode_timing_property(blob, node, "vfront-porch",
1021                                       &dt->vfront_porch);
1022         ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
1023         ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
1024         ret |= decode_timing_property(blob, node, "clock-frequency",
1025                                       &dt->pixelclock);
1026
1027         dt->flags = 0;
1028         val = fdtdec_get_int(blob, node, "vsync-active", -1);
1029         if (val != -1) {
1030                 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
1031                                 DISPLAY_FLAGS_VSYNC_LOW;
1032         }
1033         val = fdtdec_get_int(blob, node, "hsync-active", -1);
1034         if (val != -1) {
1035                 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
1036                                 DISPLAY_FLAGS_HSYNC_LOW;
1037         }
1038         val = fdtdec_get_int(blob, node, "de-active", -1);
1039         if (val != -1) {
1040                 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
1041                                 DISPLAY_FLAGS_DE_LOW;
1042         }
1043         val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
1044         if (val != -1) {
1045                 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
1046                                 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
1047         }
1048
1049         if (fdtdec_get_bool(blob, node, "interlaced"))
1050                 dt->flags |= DISPLAY_FLAGS_INTERLACED;
1051         if (fdtdec_get_bool(blob, node, "doublescan"))
1052                 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
1053         if (fdtdec_get_bool(blob, node, "doubleclk"))
1054                 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
1055
1056         return ret;
1057 }
1058
1059 int fdtdec_setup_mem_size_base(void)
1060 {
1061         int ret, mem;
1062         struct fdt_resource res;
1063
1064         mem = fdt_path_offset(gd->fdt_blob, "/memory");
1065         if (mem < 0) {
1066                 debug("%s: Missing /memory node\n", __func__);
1067                 return -EINVAL;
1068         }
1069
1070         ret = fdt_get_resource(gd->fdt_blob, mem, "reg", 0, &res);
1071         if (ret != 0) {
1072                 debug("%s: Unable to decode first memory bank\n", __func__);
1073                 return -EINVAL;
1074         }
1075
1076         gd->ram_size = (phys_size_t)(res.end - res.start + 1);
1077         gd->ram_base = (unsigned long)res.start;
1078         debug("%s: Initial DRAM size %llx\n", __func__,
1079               (unsigned long long)gd->ram_size);
1080
1081         return 0;
1082 }
1083
1084 #if defined(CONFIG_NR_DRAM_BANKS)
1085
1086 static int get_next_memory_node(const void *blob, int mem)
1087 {
1088         do {
1089                 mem = fdt_node_offset_by_prop_value(gd->fdt_blob, mem,
1090                                                     "device_type", "memory", 7);
1091         } while (!fdtdec_get_is_enabled(blob, mem));
1092
1093         return mem;
1094 }
1095
1096 int fdtdec_setup_memory_banksize(void)
1097 {
1098         int bank, ret, mem, reg = 0;
1099         struct fdt_resource res;
1100
1101         mem = get_next_memory_node(gd->fdt_blob, -1);
1102         if (mem < 0) {
1103                 debug("%s: Missing /memory node\n", __func__);
1104                 return -EINVAL;
1105         }
1106
1107         for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
1108                 ret = fdt_get_resource(gd->fdt_blob, mem, "reg", reg++, &res);
1109                 if (ret == -FDT_ERR_NOTFOUND) {
1110                         reg = 0;
1111                         mem = get_next_memory_node(gd->fdt_blob, mem);
1112                         if (mem == -FDT_ERR_NOTFOUND)
1113                                 break;
1114
1115                         ret = fdt_get_resource(gd->fdt_blob, mem, "reg", reg++, &res);
1116                         if (ret == -FDT_ERR_NOTFOUND)
1117                                 break;
1118                 }
1119                 if (ret != 0) {
1120                         return -EINVAL;
1121                 }
1122
1123                 gd->bd->bi_dram[bank].start = (phys_addr_t)res.start;
1124                 gd->bd->bi_dram[bank].size =
1125                         (phys_size_t)(res.end - res.start + 1);
1126
1127                 debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n",
1128                       __func__, bank,
1129                       (unsigned long long)gd->bd->bi_dram[bank].start,
1130                       (unsigned long long)gd->bd->bi_dram[bank].size);
1131         }
1132
1133         return 0;
1134 }
1135 #endif
1136
1137 #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
1138 # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_GZIP) ||\
1139         CONFIG_IS_ENABLED(MULTI_DTB_FIT_LZO)
1140 static int uncompress_blob(const void *src, ulong sz_src, void **dstp)
1141 {
1142         size_t sz_out = CONFIG_SPL_MULTI_DTB_FIT_UNCOMPRESS_SZ;
1143         ulong sz_in = sz_src;
1144         void *dst;
1145         int rc;
1146
1147         if (CONFIG_IS_ENABLED(GZIP))
1148                 if (gzip_parse_header(src, sz_in) < 0)
1149                         return -1;
1150         if (CONFIG_IS_ENABLED(LZO))
1151                 if (!lzop_is_valid_header(src))
1152                         return -EBADMSG;
1153
1154         if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC)) {
1155                 dst = malloc(sz_out);
1156                 if (!dst) {
1157                         puts("uncompress_blob: Unable to allocate memory\n");
1158                         return -ENOMEM;
1159                 }
1160         } else  {
1161 #  if CONFIG_IS_ENABLED(MULTI_DTB_FIT_USER_DEFINED_AREA)
1162                 dst = (void *)CONFIG_VAL(MULTI_DTB_FIT_USER_DEF_ADDR);
1163 #  else
1164                 return -ENOTSUPP;
1165 #  endif
1166         }
1167
1168         if (CONFIG_IS_ENABLED(GZIP))
1169                 rc = gunzip(dst, sz_out, (u8 *)src, &sz_in);
1170         else if (CONFIG_IS_ENABLED(LZO))
1171                 rc = lzop_decompress(src, sz_in, dst, &sz_out);
1172
1173         if (rc < 0) {
1174                 /* not a valid compressed blob */
1175                 puts("uncompress_blob: Unable to uncompress\n");
1176                 if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC))
1177                         free(dst);
1178                 return -EBADMSG;
1179         }
1180         *dstp = dst;
1181         return 0;
1182 }
1183 # else
1184 static int uncompress_blob(const void *src, ulong sz_src, void **dstp)
1185 {
1186         *dstp = (void *)src;
1187         return 0;
1188 }
1189 # endif
1190 #endif
1191
1192 #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
1193 /*
1194  * For CONFIG_OF_SEPARATE, the board may optionally implement this to
1195  * provide and/or fixup the fdt.
1196  */
1197 __weak void *board_fdt_blob_setup(void)
1198 {
1199         void *fdt_blob = NULL;
1200 #ifdef CONFIG_SPL_BUILD
1201         /* FDT is at end of BSS unless it is in a different memory region */
1202         if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
1203                 fdt_blob = (ulong *)&_image_binary_end;
1204         else
1205                 fdt_blob = (ulong *)&__bss_end;
1206 #else
1207         /* FDT is at end of image */
1208         fdt_blob = (ulong *)&_end;
1209 #endif
1210         return fdt_blob;
1211 }
1212 #endif
1213
1214 int fdtdec_setup(void)
1215 {
1216 #if CONFIG_IS_ENABLED(OF_CONTROL)
1217 # if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
1218         void *fdt_blob;
1219 # endif
1220 # ifdef CONFIG_OF_EMBED
1221         /* Get a pointer to the FDT */
1222 #  ifdef CONFIG_SPL_BUILD
1223         gd->fdt_blob = __dtb_dt_spl_begin;
1224 #  else
1225         gd->fdt_blob = __dtb_dt_begin;
1226 #  endif
1227 # elif defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
1228         /* Allow the board to override the fdt address. */
1229         gd->fdt_blob = board_fdt_blob_setup();
1230 # elif defined(CONFIG_OF_HOSTFILE)
1231         if (sandbox_read_fdt_from_file()) {
1232                 puts("Failed to read control FDT\n");
1233                 return -1;
1234         }
1235 # endif
1236 # ifndef CONFIG_SPL_BUILD
1237         /* Allow the early environment to override the fdt address */
1238 #  if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
1239         gd->fdt_blob = (void *)prior_stage_fdt_address;
1240 #  else
1241         gd->fdt_blob = map_sysmem
1242                 (env_get_ulong("fdtcontroladdr", 16,
1243                                (unsigned long)map_to_sysmem(gd->fdt_blob)), 0);
1244 #  endif
1245 # endif
1246
1247 # if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
1248         /*
1249          * Try and uncompress the blob.
1250          * Unfortunately there is no way to know how big the input blob really
1251          * is. So let us set the maximum input size arbitrarily high. 16MB
1252          * ought to be more than enough for packed DTBs.
1253          */
1254         if (uncompress_blob(gd->fdt_blob, 0x1000000, &fdt_blob) == 0)
1255                 gd->fdt_blob = fdt_blob;
1256
1257         /*
1258          * Check if blob is a FIT images containings DTBs.
1259          * If so, pick the most relevant
1260          */
1261         fdt_blob = locate_dtb_in_fit(gd->fdt_blob);
1262         if (fdt_blob) {
1263                 gd->multi_dtb_fit = gd->fdt_blob;
1264                 gd->fdt_blob = fdt_blob;
1265         }
1266
1267 # endif
1268 #endif
1269
1270         return fdtdec_prepare_fdt();
1271 }
1272
1273 #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
1274 int fdtdec_resetup(int *rescan)
1275 {
1276         void *fdt_blob;
1277
1278         /*
1279          * If the current DTB is part of a compressed FIT image,
1280          * try to locate the best match from the uncompressed
1281          * FIT image stillpresent there. Save the time and space
1282          * required to uncompress it again.
1283          */
1284         if (gd->multi_dtb_fit) {
1285                 fdt_blob = locate_dtb_in_fit(gd->multi_dtb_fit);
1286
1287                 if (fdt_blob == gd->fdt_blob) {
1288                         /*
1289                          * The best match did not change. no need to tear down
1290                          * the DM and rescan the fdt.
1291                          */
1292                         *rescan = 0;
1293                         return 0;
1294                 }
1295
1296                 *rescan = 1;
1297                 gd->fdt_blob = fdt_blob;
1298                 return fdtdec_prepare_fdt();
1299         }
1300
1301         /*
1302          * If multi_dtb_fit is NULL, it means that blob appended to u-boot is
1303          * not a FIT image containings DTB, but a single DTB. There is no need
1304          * to teard down DM and rescan the DT in this case.
1305          */
1306         *rescan = 0;
1307         return 0;
1308 }
1309 #endif
1310
1311 #ifdef CONFIG_NR_DRAM_BANKS
1312 int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
1313                            phys_addr_t *basep, phys_size_t *sizep, bd_t *bd)
1314 {
1315         int addr_cells, size_cells;
1316         const u32 *cell, *end;
1317         u64 total_size, size, addr;
1318         int node, child;
1319         bool auto_size;
1320         int bank;
1321         int len;
1322
1323         debug("%s: board_id=%d\n", __func__, board_id);
1324         if (!area)
1325                 area = "/memory";
1326         node = fdt_path_offset(blob, area);
1327         if (node < 0) {
1328                 debug("No %s node found\n", area);
1329                 return -ENOENT;
1330         }
1331
1332         cell = fdt_getprop(blob, node, "reg", &len);
1333         if (!cell) {
1334                 debug("No reg property found\n");
1335                 return -ENOENT;
1336         }
1337
1338         addr_cells = fdt_address_cells(blob, node);
1339         size_cells = fdt_size_cells(blob, node);
1340
1341         /* Check the board id and mask */
1342         for (child = fdt_first_subnode(blob, node);
1343              child >= 0;
1344              child = fdt_next_subnode(blob, child)) {
1345                 int match_mask, match_value;
1346
1347                 match_mask = fdtdec_get_int(blob, child, "match-mask", -1);
1348                 match_value = fdtdec_get_int(blob, child, "match-value", -1);
1349
1350                 if (match_value >= 0 &&
1351                     ((board_id & match_mask) == match_value)) {
1352                         /* Found matching mask */
1353                         debug("Found matching mask %d\n", match_mask);
1354                         node = child;
1355                         cell = fdt_getprop(blob, node, "reg", &len);
1356                         if (!cell) {
1357                                 debug("No memory-banks property found\n");
1358                                 return -EINVAL;
1359                         }
1360                         break;
1361                 }
1362         }
1363         /* Note: if no matching subnode was found we use the parent node */
1364
1365         if (bd) {
1366                 memset(bd->bi_dram, '\0', sizeof(bd->bi_dram[0]) *
1367                                                 CONFIG_NR_DRAM_BANKS);
1368         }
1369
1370         auto_size = fdtdec_get_bool(blob, node, "auto-size");
1371
1372         total_size = 0;
1373         end = cell + len / 4 - addr_cells - size_cells;
1374         debug("cell at %p, end %p\n", cell, end);
1375         for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
1376                 if (cell > end)
1377                         break;
1378                 addr = 0;
1379                 if (addr_cells == 2)
1380                         addr += (u64)fdt32_to_cpu(*cell++) << 32UL;
1381                 addr += fdt32_to_cpu(*cell++);
1382                 if (bd)
1383                         bd->bi_dram[bank].start = addr;
1384                 if (basep && !bank)
1385                         *basep = (phys_addr_t)addr;
1386
1387                 size = 0;
1388                 if (size_cells == 2)
1389                         size += (u64)fdt32_to_cpu(*cell++) << 32UL;
1390                 size += fdt32_to_cpu(*cell++);
1391
1392                 if (auto_size) {
1393                         u64 new_size;
1394
1395                         debug("Auto-sizing %llx, size %llx: ", addr, size);
1396                         new_size = get_ram_size((long *)(uintptr_t)addr, size);
1397                         if (new_size == size) {
1398                                 debug("OK\n");
1399                         } else {
1400                                 debug("sized to %llx\n", new_size);
1401                                 size = new_size;
1402                         }
1403                 }
1404
1405                 if (bd)
1406                         bd->bi_dram[bank].size = size;
1407                 total_size += size;
1408         }
1409
1410         debug("Memory size %llu\n", total_size);
1411         if (sizep)
1412                 *sizep = (phys_size_t)total_size;
1413
1414         return 0;
1415 }
1416 #endif /* CONFIG_NR_DRAM_BANKS */
1417
1418 #endif /* !USE_HOSTCC */