blob: 46dfcb675ccc54c454fe9661425c970bf2c7892d [file] [log] [blame]
Simon Glassb5220bc2011-10-24 19:15:32 +00001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Wolfgang Denk1a459662013-07-08 09:37:19 +02003 * SPDX-License-Identifier: GPL-2.0+
Simon Glassb5220bc2011-10-24 19:15:32 +00004 */
5
Heiko Schocher29a23f92014-03-03 12:19:30 +01006#ifndef USE_HOSTCC
Simon Glassb5220bc2011-10-24 19:15:32 +00007#include <common.h>
Simon Glass5c33c9f2014-07-23 06:55:09 -06008#include <errno.h>
Simon Glassb5220bc2011-10-24 19:15:32 +00009#include <serial.h>
10#include <libfdt.h>
11#include <fdtdec.h>
Simon Glassb45122f2015-02-27 22:06:34 -070012#include <asm/sections.h>
Simon Glass5c33c9f2014-07-23 06:55:09 -060013#include <linux/ctype.h>
Simon Glassb5220bc2011-10-24 19:15:32 +000014
15DECLARE_GLOBAL_DATA_PTR;
16
17/*
18 * Here are the type we know about. One day we might allow drivers to
19 * register. For now we just put them here. The COMPAT macro allows us to
20 * turn this into a sparse list later, and keeps the ID with the name.
21 */
22#define COMPAT(id, name) name
23static const char * const compat_names[COMPAT_COUNT] = {
Simon Glassf88fe2d2012-02-27 10:52:34 +000024 COMPAT(UNKNOWN, "<none>"),
Simon Glass87f938c2012-02-27 10:52:49 +000025 COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
Jim Lin7e44d932013-06-21 19:05:47 +080026 COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"),
27 COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
Jimmy Zhang0e35ad02012-04-02 13:18:52 +000028 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
29 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
Rakesh Iyer6642a682012-04-17 09:01:35 +000030 COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
Jim Lin312693c2012-07-29 20:53:29 +000031 COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
Simon Glasse1ae0d12012-10-17 13:24:49 +000032 COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
Simon Glass00f37322015-04-14 21:03:40 -060033 COMPAT(NVIDIA_TEGRA124_DC, "nvidia,tegra124-dc"),
34 COMPAT(NVIDIA_TEGRA124_SOR, "nvidia,tegra124-sor"),
35 COMPAT(NVIDIA_TEGRA124_PMC, "nvidia,tegra124-pmc"),
Wei Ni87540de2012-10-17 13:24:50 +000036 COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
Stephen Warrena73ca472014-01-24 12:46:06 -070037 COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
Tom Warrenf4e4e0b2013-03-04 14:07:18 -070038 COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
Tom Warrenc9aa8312013-02-21 12:31:30 +000039 COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
Thierry Redingf3158282014-12-09 22:25:12 -070040 COMPAT(NVIDIA_TEGRA124_PCIE, "nvidia,tegra124-pcie"),
41 COMPAT(NVIDIA_TEGRA30_PCIE, "nvidia,tegra30-pcie"),
42 COMPAT(NVIDIA_TEGRA20_PCIE, "nvidia,tegra20-pcie"),
Thierry Reding79c7a902014-12-09 22:25:09 -070043 COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
Hatim RVcc9fe332012-12-11 00:52:46 +000044 COMPAT(SMSC_LAN9215, "smsc,lan9215"),
45 COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
Rajeshwari Shindec34253d2012-12-26 20:03:10 +000046 COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
Rajeshwari Shinde72dbff12012-12-26 20:03:16 +000047 COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
48 COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
Hung-ying Tyan713cb682013-05-15 18:27:32 +080049 COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
Rajeshwari Shinde6abd1622013-01-07 23:35:05 +000050 COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
Vivek Gautam108b85b2013-09-14 14:02:48 +053051 COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
Akshay Saraswat618766c2013-02-25 01:13:01 +000052 COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
Ajay Kumard7377b52013-02-21 23:53:00 +000053 COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
Piotr Wilczekde461c52014-03-07 14:59:39 +010054 COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
Ajay Kumar1e4706a2013-02-21 23:53:05 +000055 COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
Jaehoon Chung7d3ca0f2014-05-16 13:59:51 +090056 COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
Piotr Wilczek3577fe82014-03-07 14:59:41 +010057 COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
Rajeshwari Shindeee1e3c22013-06-24 16:47:20 +053058 COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
Przemyslaw Marczakf37df0f2015-04-20 20:07:45 +020059 COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686"),
Simon Glassbb8215f2013-03-11 06:08:08 +000060 COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
Rajeshwari Shinde7772bb72013-02-14 19:46:15 +000061 COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
Rong Changf6267992013-04-12 10:44:57 +000062 COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
Simon Glassf0e57b12015-05-04 11:31:00 -060063 COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645tt"),
naveen krishna chatradhiecbd7e12013-04-29 15:58:52 -070064 COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
Simon Glass7d95f2a2014-02-27 13:26:19 -070065 COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"),
Tom Wai-Hong Tamac1058f2014-05-20 06:01:36 -060066 COMPAT(TI_TPS65090, "ti,tps65090"),
Simon Glassa9cf6da2014-05-20 06:01:42 -060067 COMPAT(COMPAT_NXP_PTN3460, "nxp,ptn3460"),
Ajay Kumar45c480c2014-09-05 16:53:33 +053068 COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
Vadim Bendebury9e8f6642014-09-05 16:53:34 +053069 COMPAT(PARADE_PS8625, "parade,ps8625"),
Simon Glass77f9b1f2014-11-12 22:42:21 -070070 COMPAT(INTEL_MICROCODE, "intel,microcode"),
Simon Glass65dd74a2014-11-12 22:42:28 -070071 COMPAT(MEMORY_SPD, "memory-spd"),
Simon Glass3ac83932014-11-14 18:18:38 -070072 COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"),
Simon Glassbb80be32014-11-24 21:18:16 -070073 COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"),
Simon Glasseffcf062014-11-14 20:56:36 -070074 COMPAT(INTEL_GMA, "intel,gma"),
Thierry Reding6173c452014-12-09 22:25:05 -070075 COMPAT(AMS_AS3722, "ams,as3722"),
Simon Glass5da38082015-01-19 22:16:06 -070076 COMPAT(INTEL_ICH_SPI, "intel,ich-spi"),
Bin Mengc89ada02015-02-05 23:42:26 +080077 COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
Gabriel Huau5318f182015-05-25 22:27:37 -070078 COMPAT(INTEL_X86_PINCTRL, "intel,x86-pinctrl"),
Masahiro Yamada6462cde2015-03-11 15:54:46 +090079 COMPAT(SOCIONEXT_XHCI, "socionext,uniphier-xhci"),
Simon Glass90b16d12015-03-26 09:29:29 -060080 COMPAT(COMPAT_INTEL_PCH, "intel,bd82x6x"),
Bin Meng9c7dea62015-05-25 22:35:04 +080081 COMPAT(COMPAT_INTEL_IRQ_ROUTER, "intel,irq-router"),
Simon Glassb5220bc2011-10-24 19:15:32 +000082};
83
Simon Glassa53f4a22012-01-17 08:20:50 +000084const char *fdtdec_get_compatible(enum fdt_compat_id id)
85{
86 /* We allow reading of the 'unknown' ID for testing purposes */
87 assert(id >= 0 && id < COMPAT_COUNT);
88 return compat_names[id];
89}
90
Simon Glass4397a2a2013-03-19 04:58:51 +000091fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
92 const char *prop_name, fdt_size_t *sizep)
Simon Glassb5220bc2011-10-24 19:15:32 +000093{
94 const fdt_addr_t *cell;
95 int len;
96
Simon Glass1cb23232012-07-12 05:25:01 +000097 debug("%s: %s: ", __func__, prop_name);
Simon Glassb5220bc2011-10-24 19:15:32 +000098 cell = fdt_getprop(blob, node, prop_name, &len);
Simon Glass4397a2a2013-03-19 04:58:51 +000099 if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
100 len == sizeof(fdt_addr_t) * 2)) {
Simon Glass1cb23232012-07-12 05:25:01 +0000101 fdt_addr_t addr = fdt_addr_to_cpu(*cell);
Simon Glass4397a2a2013-03-19 04:58:51 +0000102 if (sizep) {
103 const fdt_size_t *size;
Simon Glass1cb23232012-07-12 05:25:01 +0000104
Simon Glass4397a2a2013-03-19 04:58:51 +0000105 size = (fdt_size_t *)((char *)cell +
106 sizeof(fdt_addr_t));
107 *sizep = fdt_size_to_cpu(*size);
Simon Glass370b6c52013-11-10 10:26:54 -0700108 debug("addr=%08lx, size=%08x\n",
109 (ulong)addr, *sizep);
Simon Glass4397a2a2013-03-19 04:58:51 +0000110 } else {
Simon Glass370b6c52013-11-10 10:26:54 -0700111 debug("%08lx\n", (ulong)addr);
Simon Glass4397a2a2013-03-19 04:58:51 +0000112 }
Simon Glass1cb23232012-07-12 05:25:01 +0000113 return addr;
114 }
115 debug("(not found)\n");
Simon Glassb5220bc2011-10-24 19:15:32 +0000116 return FDT_ADDR_T_NONE;
117}
118
Simon Glass4397a2a2013-03-19 04:58:51 +0000119fdt_addr_t fdtdec_get_addr(const void *blob, int node,
120 const char *prop_name)
121{
122 return fdtdec_get_addr_size(blob, node, prop_name, NULL);
123}
124
Bin Menga62e84d2014-12-31 16:05:11 +0800125#ifdef CONFIG_PCI
126int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
127 const char *prop_name, struct fdt_pci_addr *addr)
128{
129 const u32 *cell;
130 int len;
131 int ret = -ENOENT;
132
133 debug("%s: %s: ", __func__, prop_name);
134
135 /*
136 * If we follow the pci bus bindings strictly, we should check
137 * the value of the node's parent node's #address-cells and
138 * #size-cells. They need to be 3 and 2 accordingly. However,
139 * for simplicity we skip the check here.
140 */
141 cell = fdt_getprop(blob, node, prop_name, &len);
142 if (!cell)
143 goto fail;
144
145 if ((len % FDT_PCI_REG_SIZE) == 0) {
146 int num = len / FDT_PCI_REG_SIZE;
147 int i;
148
149 for (i = 0; i < num; i++) {
150 debug("pci address #%d: %08lx %08lx %08lx\n", i,
151 (ulong)fdt_addr_to_cpu(cell[0]),
152 (ulong)fdt_addr_to_cpu(cell[1]),
153 (ulong)fdt_addr_to_cpu(cell[2]));
154 if ((fdt_addr_to_cpu(*cell) & type) == type) {
155 addr->phys_hi = fdt_addr_to_cpu(cell[0]);
156 addr->phys_mid = fdt_addr_to_cpu(cell[1]);
157 addr->phys_lo = fdt_addr_to_cpu(cell[2]);
158 break;
159 } else {
160 cell += (FDT_PCI_ADDR_CELLS +
161 FDT_PCI_SIZE_CELLS);
162 }
163 }
164
Simon Glass106cce92015-03-05 12:25:19 -0700165 if (i == num) {
166 ret = -ENXIO;
Bin Menga62e84d2014-12-31 16:05:11 +0800167 goto fail;
Simon Glass106cce92015-03-05 12:25:19 -0700168 }
Bin Menga62e84d2014-12-31 16:05:11 +0800169
170 return 0;
171 } else {
172 ret = -EINVAL;
173 }
174
175fail:
176 debug("(not found)\n");
177 return ret;
178}
179
180int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
181{
182 const char *list, *end;
183 int len;
184
185 list = fdt_getprop(blob, node, "compatible", &len);
186 if (!list)
187 return -ENOENT;
188
189 end = list + len;
190 while (list < end) {
191 char *s;
192
193 len = strlen(list);
194 if (len >= strlen("pciVVVV,DDDD")) {
195 s = strstr(list, "pci");
196
197 /*
198 * check if the string is something like pciVVVV,DDDD.RR
199 * or just pciVVVV,DDDD
200 */
201 if (s && s[7] == ',' &&
202 (s[12] == '.' || s[12] == 0)) {
203 s += 3;
204 *vendor = simple_strtol(s, NULL, 16);
205
206 s += 5;
207 *device = simple_strtol(s, NULL, 16);
208
209 return 0;
210 }
211 } else {
212 list += (len + 1);
213 }
214 }
215
216 return -ENOENT;
217}
218
219int fdtdec_get_pci_bdf(const void *blob, int node,
220 struct fdt_pci_addr *addr, pci_dev_t *bdf)
221{
222 u16 dt_vendor, dt_device, vendor, device;
223 int ret;
224
225 /* get vendor id & device id from the compatible string */
226 ret = fdtdec_get_pci_vendev(blob, node, &dt_vendor, &dt_device);
227 if (ret)
228 return ret;
229
230 /* extract the bdf from fdt_pci_addr */
231 *bdf = addr->phys_hi & 0xffff00;
232
233 /* read vendor id & device id based on bdf */
234 pci_read_config_word(*bdf, PCI_VENDOR_ID, &vendor);
235 pci_read_config_word(*bdf, PCI_DEVICE_ID, &device);
236
237 /*
238 * Note there are two places in the device tree to fully describe
239 * a pci device: one is via compatible string with a format of
240 * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in
241 * the device node's reg address property. We read the vendor id
242 * and device id based on bdf and compare the values with the
243 * "VVVV,DDDD". If they are the same, then we are good to use bdf
244 * to read device's bar. But if they are different, we have to rely
245 * on the vendor id and device id extracted from the compatible
246 * string and locate the real bdf by pci_find_device(). This is
247 * because normally we may only know device's device number and
248 * function number when writing device tree. The bus number is
249 * dynamically assigned during the pci enumeration process.
250 */
251 if ((dt_vendor != vendor) || (dt_device != device)) {
252 *bdf = pci_find_device(dt_vendor, dt_device, 0);
253 if (*bdf == -1)
254 return -ENODEV;
255 }
256
257 return 0;
258}
259
260int fdtdec_get_pci_bar32(const void *blob, int node,
261 struct fdt_pci_addr *addr, u32 *bar)
262{
263 pci_dev_t bdf;
264 int barnum;
265 int ret;
266
267 /* get pci devices's bdf */
268 ret = fdtdec_get_pci_bdf(blob, node, addr, &bdf);
269 if (ret)
270 return ret;
271
272 /* extract the bar number from fdt_pci_addr */
273 barnum = addr->phys_hi & 0xff;
274 if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
275 return -EINVAL;
276
277 barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
278 *bar = pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf)), bdf, barnum);
279
280 return 0;
281}
282#endif
283
Che-Liang Chiouaadef0a2012-10-25 16:31:05 +0000284uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
285 uint64_t default_val)
286{
287 const uint64_t *cell64;
288 int length;
289
290 cell64 = fdt_getprop(blob, node, prop_name, &length);
291 if (!cell64 || length < sizeof(*cell64))
292 return default_val;
293
294 return fdt64_to_cpu(*cell64);
295}
296
Simon Glassf88fe2d2012-02-27 10:52:34 +0000297int fdtdec_get_is_enabled(const void *blob, int node)
Simon Glassb5220bc2011-10-24 19:15:32 +0000298{
299 const char *cell;
300
Simon Glassf88fe2d2012-02-27 10:52:34 +0000301 /*
302 * It should say "okay", so only allow that. Some fdts use "ok" but
303 * this is a bug. Please fix your device tree source file. See here
304 * for discussion:
305 *
306 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
307 */
Simon Glassb5220bc2011-10-24 19:15:32 +0000308 cell = fdt_getprop(blob, node, "status", NULL);
309 if (cell)
Simon Glassf88fe2d2012-02-27 10:52:34 +0000310 return 0 == strcmp(cell, "okay");
311 return 1;
Simon Glassb5220bc2011-10-24 19:15:32 +0000312}
313
Gerald Van Baren7cde3972012-11-12 23:13:54 -0500314enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
Simon Glassb5220bc2011-10-24 19:15:32 +0000315{
316 enum fdt_compat_id id;
317
318 /* Search our drivers */
319 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
320 if (0 == fdt_node_check_compatible(blob, node,
321 compat_names[id]))
322 return id;
323 return COMPAT_UNKNOWN;
324}
325
326int fdtdec_next_compatible(const void *blob, int node,
327 enum fdt_compat_id id)
328{
329 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
330}
331
Simon Glass3ddecfc2012-04-02 13:18:42 +0000332int fdtdec_next_compatible_subnode(const void *blob, int node,
333 enum fdt_compat_id id, int *depthp)
334{
335 do {
336 node = fdt_next_node(blob, node, depthp);
337 } while (*depthp > 1);
338
339 /* If this is a direct subnode, and compatible, return it */
340 if (*depthp == 1 && 0 == fdt_node_check_compatible(
341 blob, node, compat_names[id]))
342 return node;
343
344 return -FDT_ERR_NOTFOUND;
345}
346
Simon Glassb5220bc2011-10-24 19:15:32 +0000347int fdtdec_next_alias(const void *blob, const char *name,
348 enum fdt_compat_id id, int *upto)
349{
350#define MAX_STR_LEN 20
351 char str[MAX_STR_LEN + 20];
352 int node, err;
353
354 /* snprintf() is not available */
355 assert(strlen(name) < MAX_STR_LEN);
356 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
Simon Glass00878472012-10-31 14:02:42 +0000357 node = fdt_path_offset(blob, str);
Simon Glassb5220bc2011-10-24 19:15:32 +0000358 if (node < 0)
359 return node;
360 err = fdt_node_check_compatible(blob, node, compat_names[id]);
361 if (err < 0)
362 return err;
Simon Glassf88fe2d2012-02-27 10:52:34 +0000363 if (err)
364 return -FDT_ERR_NOTFOUND;
365 (*upto)++;
366 return node;
Simon Glassb5220bc2011-10-24 19:15:32 +0000367}
368
Simon Glassa53f4a22012-01-17 08:20:50 +0000369int fdtdec_find_aliases_for_id(const void *blob, const char *name,
370 enum fdt_compat_id id, int *node_list, int maxcount)
371{
Simon Glassc6782272012-02-03 15:13:53 +0000372 memset(node_list, '\0', sizeof(*node_list) * maxcount);
373
374 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
375}
376
377/* TODO: Can we tighten this code up a little? */
378int fdtdec_add_aliases_for_id(const void *blob, const char *name,
379 enum fdt_compat_id id, int *node_list, int maxcount)
380{
Simon Glassa53f4a22012-01-17 08:20:50 +0000381 int name_len = strlen(name);
382 int nodes[maxcount];
383 int num_found = 0;
384 int offset, node;
385 int alias_node;
386 int count;
387 int i, j;
388
389 /* find the alias node if present */
390 alias_node = fdt_path_offset(blob, "/aliases");
391
392 /*
393 * start with nothing, and we can assume that the root node can't
394 * match
395 */
396 memset(nodes, '\0', sizeof(nodes));
397
398 /* First find all the compatible nodes */
399 for (node = count = 0; node >= 0 && count < maxcount;) {
400 node = fdtdec_next_compatible(blob, node, id);
401 if (node >= 0)
402 nodes[count++] = node;
403 }
404 if (node >= 0)
405 debug("%s: warning: maxcount exceeded with alias '%s'\n",
406 __func__, name);
407
408 /* Now find all the aliases */
Simon Glassa53f4a22012-01-17 08:20:50 +0000409 for (offset = fdt_first_property_offset(blob, alias_node);
410 offset > 0;
411 offset = fdt_next_property_offset(blob, offset)) {
412 const struct fdt_property *prop;
413 const char *path;
414 int number;
415 int found;
416
417 node = 0;
418 prop = fdt_get_property_by_offset(blob, offset, NULL);
419 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
420 if (prop->len && 0 == strncmp(path, name, name_len))
421 node = fdt_path_offset(blob, prop->data);
422 if (node <= 0)
423 continue;
424
425 /* Get the alias number */
426 number = simple_strtoul(path + name_len, NULL, 10);
427 if (number < 0 || number >= maxcount) {
428 debug("%s: warning: alias '%s' is out of range\n",
429 __func__, path);
430 continue;
431 }
432
433 /* Make sure the node we found is actually in our list! */
434 found = -1;
435 for (j = 0; j < count; j++)
436 if (nodes[j] == node) {
437 found = j;
438 break;
439 }
440
441 if (found == -1) {
442 debug("%s: warning: alias '%s' points to a node "
443 "'%s' that is missing or is not compatible "
444 " with '%s'\n", __func__, path,
445 fdt_get_name(blob, node, NULL),
446 compat_names[id]);
447 continue;
448 }
449
450 /*
451 * Add this node to our list in the right place, and mark
452 * it as done.
453 */
454 if (fdtdec_get_is_enabled(blob, node)) {
Simon Glassc6782272012-02-03 15:13:53 +0000455 if (node_list[number]) {
456 debug("%s: warning: alias '%s' requires that "
457 "a node be placed in the list in a "
458 "position which is already filled by "
459 "node '%s'\n", __func__, path,
460 fdt_get_name(blob, node, NULL));
461 continue;
462 }
Simon Glassa53f4a22012-01-17 08:20:50 +0000463 node_list[number] = node;
464 if (number >= num_found)
465 num_found = number + 1;
466 }
Simon Glassc6782272012-02-03 15:13:53 +0000467 nodes[found] = 0;
Simon Glassa53f4a22012-01-17 08:20:50 +0000468 }
469
470 /* Add any nodes not mentioned by an alias */
471 for (i = j = 0; i < maxcount; i++) {
472 if (!node_list[i]) {
473 for (; j < maxcount; j++)
474 if (nodes[j] &&
475 fdtdec_get_is_enabled(blob, nodes[j]))
476 break;
477
478 /* Have we run out of nodes to add? */
479 if (j == maxcount)
480 break;
481
482 assert(!node_list[i]);
483 node_list[i] = nodes[j++];
484 if (i >= num_found)
485 num_found = i + 1;
486 }
487 }
488
489 return num_found;
490}
491
Simon Glass5c33c9f2014-07-23 06:55:09 -0600492int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
493 int *seqp)
494{
495 int base_len = strlen(base);
496 const char *find_name;
497 int find_namelen;
498 int prop_offset;
499 int aliases;
500
501 find_name = fdt_get_name(blob, offset, &find_namelen);
502 debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
503
504 aliases = fdt_path_offset(blob, "/aliases");
505 for (prop_offset = fdt_first_property_offset(blob, aliases);
506 prop_offset > 0;
507 prop_offset = fdt_next_property_offset(blob, prop_offset)) {
508 const char *prop;
509 const char *name;
510 const char *slash;
511 const char *p;
512 int len;
513
514 prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
515 debug(" - %s, %s\n", name, prop);
516 if (len < find_namelen || *prop != '/' || prop[len - 1] ||
517 strncmp(name, base, base_len))
518 continue;
519
520 slash = strrchr(prop, '/');
521 if (strcmp(slash + 1, find_name))
522 continue;
Simon Glassa88340d2014-11-11 10:46:20 -0700523 for (p = name + strlen(name) - 1; p > name; p--) {
524 if (!isdigit(*p)) {
525 *seqp = simple_strtoul(p + 1, NULL, 10);
Simon Glass5c33c9f2014-07-23 06:55:09 -0600526 debug("Found seq %d\n", *seqp);
527 return 0;
528 }
529 }
530 }
531
532 debug("Not found\n");
533 return -ENOENT;
534}
535
Simon Glassaac07d42014-09-04 16:27:24 -0600536int fdtdec_get_chosen_node(const void *blob, const char *name)
537{
538 const char *prop;
539 int chosen_node;
540 int len;
541
542 if (!blob)
543 return -FDT_ERR_NOTFOUND;
544 chosen_node = fdt_path_offset(blob, "/chosen");
545 prop = fdt_getprop(blob, chosen_node, name, &len);
546 if (!prop)
547 return -FDT_ERR_NOTFOUND;
548 return fdt_path_offset(blob, prop);
549}
550
Simon Glass9a263e52012-03-28 10:08:24 +0000551int fdtdec_check_fdt(void)
552{
553 /*
554 * We must have an FDT, but we cannot panic() yet since the console
555 * is not ready. So for now, just assert(). Boards which need an early
556 * FDT (prior to console ready) will need to make their own
557 * arrangements and do their own checks.
558 */
559 assert(!fdtdec_prepare_fdt());
560 return 0;
561}
562
Simon Glassb5220bc2011-10-24 19:15:32 +0000563/*
564 * This function is a little odd in that it accesses global data. At some
565 * point if the architecture board.c files merge this will make more sense.
566 * Even now, it is common code.
567 */
Simon Glass9a263e52012-03-28 10:08:24 +0000568int fdtdec_prepare_fdt(void)
Simon Glassb5220bc2011-10-24 19:15:32 +0000569{
Simon Glassc309c2d2013-04-20 08:42:46 +0000570 if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
571 fdt_check_header(gd->fdt_blob)) {
Simon Glass66312372015-02-27 22:06:32 -0700572#ifdef CONFIG_SPL_BUILD
573 puts("Missing DTB\n");
574#else
575 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");
576#endif
Simon Glass9a263e52012-03-28 10:08:24 +0000577 return -1;
578 }
Simon Glassb5220bc2011-10-24 19:15:32 +0000579 return 0;
580}
Simon Glassd17da652012-02-27 10:52:35 +0000581
582int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
583{
584 const u32 *phandle;
585 int lookup;
586
Simon Glass1cb23232012-07-12 05:25:01 +0000587 debug("%s: %s\n", __func__, prop_name);
Simon Glassd17da652012-02-27 10:52:35 +0000588 phandle = fdt_getprop(blob, node, prop_name, NULL);
589 if (!phandle)
590 return -FDT_ERR_NOTFOUND;
591
592 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
593 return lookup;
594}
595
596/**
597 * Look up a property in a node and check that it has a minimum length.
598 *
599 * @param blob FDT blob
600 * @param node node to examine
601 * @param prop_name name of property to find
602 * @param min_len minimum property length in bytes
603 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
604 found, or -FDT_ERR_BADLAYOUT if not enough data
605 * @return pointer to cell, which is only valid if err == 0
606 */
607static const void *get_prop_check_min_len(const void *blob, int node,
608 const char *prop_name, int min_len, int *err)
609{
610 const void *cell;
611 int len;
612
613 debug("%s: %s\n", __func__, prop_name);
614 cell = fdt_getprop(blob, node, prop_name, &len);
615 if (!cell)
616 *err = -FDT_ERR_NOTFOUND;
617 else if (len < min_len)
618 *err = -FDT_ERR_BADLAYOUT;
619 else
620 *err = 0;
621 return cell;
622}
623
624int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
625 u32 *array, int count)
626{
627 const u32 *cell;
628 int i, err = 0;
629
630 debug("%s: %s\n", __func__, prop_name);
631 cell = get_prop_check_min_len(blob, node, prop_name,
632 sizeof(u32) * count, &err);
633 if (!err) {
634 for (i = 0; i < count; i++)
635 array[i] = fdt32_to_cpu(cell[i]);
636 }
637 return err;
638}
639
Simon Glassa9f04d42014-11-10 18:00:19 -0700640int fdtdec_get_int_array_count(const void *blob, int node,
641 const char *prop_name, u32 *array, int count)
642{
643 const u32 *cell;
644 int len, elems;
645 int i;
646
647 debug("%s: %s\n", __func__, prop_name);
648 cell = fdt_getprop(blob, node, prop_name, &len);
649 if (!cell)
650 return -FDT_ERR_NOTFOUND;
651 elems = len / sizeof(u32);
652 if (count > elems)
653 count = elems;
654 for (i = 0; i < count; i++)
655 array[i] = fdt32_to_cpu(cell[i]);
656
657 return count;
658}
659
Simon Glass96875e72012-04-02 13:18:41 +0000660const u32 *fdtdec_locate_array(const void *blob, int node,
661 const char *prop_name, int count)
662{
663 const u32 *cell;
664 int err;
665
666 cell = get_prop_check_min_len(blob, node, prop_name,
667 sizeof(u32) * count, &err);
668 return err ? NULL : cell;
669}
670
Simon Glassd17da652012-02-27 10:52:35 +0000671int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
672{
673 const s32 *cell;
674 int len;
675
676 debug("%s: %s\n", __func__, prop_name);
677 cell = fdt_getprop(blob, node, prop_name, &len);
678 return cell != NULL;
679}
Simon Glassed3ee5c2012-02-27 10:52:36 +0000680
Simon Glass57068a72015-01-05 20:05:26 -0700681int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
682 const char *list_name,
683 const char *cells_name,
684 int cell_count, int index,
685 struct fdtdec_phandle_args *out_args)
686{
687 const __be32 *list, *list_end;
688 int rc = 0, size, cur_index = 0;
689 uint32_t count = 0;
690 int node = -1;
691 int phandle;
692
693 /* Retrieve the phandle list property */
694 list = fdt_getprop(blob, src_node, list_name, &size);
695 if (!list)
696 return -ENOENT;
697 list_end = list + size / sizeof(*list);
698
699 /* Loop over the phandles until all the requested entry is found */
700 while (list < list_end) {
701 rc = -EINVAL;
702 count = 0;
703
704 /*
705 * If phandle is 0, then it is an empty entry with no
706 * arguments. Skip forward to the next entry.
707 */
708 phandle = be32_to_cpup(list++);
709 if (phandle) {
710 /*
711 * Find the provider node and parse the #*-cells
712 * property to determine the argument length.
713 *
714 * This is not needed if the cell count is hard-coded
715 * (i.e. cells_name not set, but cell_count is set),
716 * except when we're going to return the found node
717 * below.
718 */
719 if (cells_name || cur_index == index) {
720 node = fdt_node_offset_by_phandle(blob,
721 phandle);
722 if (!node) {
723 debug("%s: could not find phandle\n",
724 fdt_get_name(blob, src_node,
725 NULL));
726 goto err;
727 }
728 }
729
730 if (cells_name) {
731 count = fdtdec_get_int(blob, node, cells_name,
732 -1);
733 if (count == -1) {
734 debug("%s: could not get %s for %s\n",
735 fdt_get_name(blob, src_node,
736 NULL),
737 cells_name,
738 fdt_get_name(blob, node,
739 NULL));
740 goto err;
741 }
742 } else {
743 count = cell_count;
744 }
745
746 /*
747 * Make sure that the arguments actually fit in the
748 * remaining property data length
749 */
750 if (list + count > list_end) {
751 debug("%s: arguments longer than property\n",
752 fdt_get_name(blob, src_node, NULL));
753 goto err;
754 }
755 }
756
757 /*
758 * All of the error cases above bail out of the loop, so at
759 * this point, the parsing is successful. If the requested
760 * index matches, then fill the out_args structure and return,
761 * or return -ENOENT for an empty entry.
762 */
763 rc = -ENOENT;
764 if (cur_index == index) {
765 if (!phandle)
766 goto err;
767
768 if (out_args) {
769 int i;
770
771 if (count > MAX_PHANDLE_ARGS) {
772 debug("%s: too many arguments %d\n",
773 fdt_get_name(blob, src_node,
774 NULL), count);
775 count = MAX_PHANDLE_ARGS;
776 }
777 out_args->node = node;
778 out_args->args_count = count;
779 for (i = 0; i < count; i++) {
780 out_args->args[i] =
781 be32_to_cpup(list++);
782 }
783 }
784
785 /* Found it! return success */
786 return 0;
787 }
788
789 node = -1;
790 list += count;
791 cur_index++;
792 }
793
794 /*
795 * Result will be one of:
796 * -ENOENT : index is for empty phandle
797 * -EINVAL : parsing error on data
798 * [1..n] : Number of phandle (count mode; when index = -1)
799 */
800 rc = index < 0 ? cur_index : -ENOENT;
801 err:
802 return rc;
803}
804
Anton Staffbed4d892012-04-17 09:01:28 +0000805int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
806 u8 *array, int count)
807{
808 const u8 *cell;
809 int err;
810
811 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
812 if (!err)
813 memcpy(array, cell, count);
814 return err;
815}
816
817const u8 *fdtdec_locate_byte_array(const void *blob, int node,
818 const char *prop_name, int count)
819{
820 const u8 *cell;
821 int err;
822
823 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
824 if (err)
825 return NULL;
826 return cell;
827}
Abhilash Kesavan09258f12012-10-25 16:30:58 +0000828
Abhilash Kesavan09258f12012-10-25 16:30:58 +0000829int fdtdec_get_config_int(const void *blob, const char *prop_name,
830 int default_val)
831{
832 int config_node;
833
834 debug("%s: %s\n", __func__, prop_name);
835 config_node = fdt_path_offset(blob, "/config");
836 if (config_node < 0)
837 return default_val;
838 return fdtdec_get_int(blob, config_node, prop_name, default_val);
839}
Simon Glass332ab0d2012-10-25 16:30:59 +0000840
Gabe Black79289c02012-10-25 16:31:04 +0000841int fdtdec_get_config_bool(const void *blob, const char *prop_name)
842{
843 int config_node;
844 const void *prop;
845
846 debug("%s: %s\n", __func__, prop_name);
847 config_node = fdt_path_offset(blob, "/config");
848 if (config_node < 0)
849 return 0;
850 prop = fdt_get_property(blob, config_node, prop_name, NULL);
851
852 return prop != NULL;
853}
854
Simon Glass332ab0d2012-10-25 16:30:59 +0000855char *fdtdec_get_config_string(const void *blob, const char *prop_name)
856{
857 const char *nodep;
858 int nodeoffset;
859 int len;
860
861 debug("%s: %s\n", __func__, prop_name);
862 nodeoffset = fdt_path_offset(blob, "/config");
863 if (nodeoffset < 0)
864 return NULL;
865
866 nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
867 if (!nodep)
868 return NULL;
869
870 return (char *)nodep;
871}
Simon Glassf20c4612012-10-25 16:31:00 +0000872
Simon Glass76489832014-10-23 18:58:51 -0600873int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
874 fdt_addr_t *basep, fdt_size_t *sizep)
Simon Glassf20c4612012-10-25 16:31:00 +0000875{
876 const fdt_addr_t *cell;
877 int len;
878
Simon Glass76489832014-10-23 18:58:51 -0600879 debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
880 prop_name);
Simon Glassf20c4612012-10-25 16:31:00 +0000881 cell = fdt_getprop(blob, node, prop_name, &len);
Simon Glass76489832014-10-23 18:58:51 -0600882 if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
883 debug("cell=%p, len=%d\n", cell, len);
Simon Glassf20c4612012-10-25 16:31:00 +0000884 return -1;
Simon Glass76489832014-10-23 18:58:51 -0600885 }
Simon Glassf20c4612012-10-25 16:31:00 +0000886
Simon Glass76489832014-10-23 18:58:51 -0600887 *basep = fdt_addr_to_cpu(*cell);
888 *sizep = fdt_size_to_cpu(cell[1]);
889 debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
890 (ulong)*sizep);
891
Simon Glassf20c4612012-10-25 16:31:00 +0000892 return 0;
893}
Simon Glass006e73b2014-02-27 13:26:01 -0700894
895/**
896 * Read a flash entry from the fdt
897 *
898 * @param blob FDT blob
899 * @param node Offset of node to read
900 * @param name Name of node being read
901 * @param entry Place to put offset and size of this node
902 * @return 0 if ok, -ve on error
903 */
904int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
905 struct fmap_entry *entry)
906{
Simon Glassf3cc44f2014-10-23 18:58:52 -0600907 const char *prop;
Simon Glass006e73b2014-02-27 13:26:01 -0700908 u32 reg[2];
909
910 if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
911 debug("Node '%s' has bad/missing 'reg' property\n", name);
912 return -FDT_ERR_NOTFOUND;
913 }
914 entry->offset = reg[0];
915 entry->length = reg[1];
Simon Glassf3cc44f2014-10-23 18:58:52 -0600916 entry->used = fdtdec_get_int(blob, node, "used", entry->length);
917 prop = fdt_getprop(blob, node, "compress", NULL);
918 entry->compress_algo = prop && !strcmp(prop, "lzo") ?
919 FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
920 prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
921 entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
922 entry->hash = (uint8_t *)prop;
Simon Glass006e73b2014-02-27 13:26:01 -0700923
924 return 0;
925}
Thierry Reding56f42242014-08-26 17:33:53 +0200926
Simon Glass5f7bfdd2015-03-05 12:25:14 -0700927u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
Thierry Reding56f42242014-08-26 17:33:53 +0200928{
929 u64 number = 0;
930
931 while (cells--)
932 number = (number << 32) | fdt32_to_cpu(*ptr++);
933
934 return number;
935}
936
937int fdt_get_resource(const void *fdt, int node, const char *property,
938 unsigned int index, struct fdt_resource *res)
939{
940 const fdt32_t *ptr, *end;
941 int na, ns, len, parent;
942 unsigned int i = 0;
943
944 parent = fdt_parent_offset(fdt, node);
945 if (parent < 0)
946 return parent;
947
948 na = fdt_address_cells(fdt, parent);
949 ns = fdt_size_cells(fdt, parent);
950
951 ptr = fdt_getprop(fdt, node, property, &len);
952 if (!ptr)
953 return len;
954
955 end = ptr + len / sizeof(*ptr);
956
957 while (ptr + na + ns <= end) {
958 if (i == index) {
959 res->start = res->end = fdtdec_get_number(ptr, na);
960 res->end += fdtdec_get_number(&ptr[na], ns) - 1;
961 return 0;
962 }
963
964 ptr += na + ns;
965 i++;
966 }
967
968 return -FDT_ERR_NOTFOUND;
969}
970
971int fdt_get_named_resource(const void *fdt, int node, const char *property,
972 const char *prop_names, const char *name,
973 struct fdt_resource *res)
974{
975 int index;
976
977 index = fdt_find_string(fdt, node, prop_names, name);
978 if (index < 0)
979 return index;
980
981 return fdt_get_resource(fdt, node, property, index, res);
982}
Thierry Reding9f85eee2014-08-26 17:33:54 +0200983
Simon Glass26403872014-10-23 18:58:56 -0600984int fdtdec_decode_memory_region(const void *blob, int config_node,
985 const char *mem_type, const char *suffix,
986 fdt_addr_t *basep, fdt_size_t *sizep)
987{
988 char prop_name[50];
989 const char *mem;
990 fdt_size_t size, offset_size;
991 fdt_addr_t base, offset;
992 int node;
993
994 if (config_node == -1) {
995 config_node = fdt_path_offset(blob, "/config");
996 if (config_node < 0) {
997 debug("%s: Cannot find /config node\n", __func__);
998 return -ENOENT;
999 }
1000 }
1001 if (!suffix)
1002 suffix = "";
1003
1004 snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
1005 suffix);
1006 mem = fdt_getprop(blob, config_node, prop_name, NULL);
1007 if (!mem) {
1008 debug("%s: No memory type for '%s', using /memory\n", __func__,
1009 prop_name);
1010 mem = "/memory";
1011 }
1012
1013 node = fdt_path_offset(blob, mem);
1014 if (node < 0) {
1015 debug("%s: Failed to find node '%s': %s\n", __func__, mem,
1016 fdt_strerror(node));
1017 return -ENOENT;
1018 }
1019
1020 /*
1021 * Not strictly correct - the memory may have multiple banks. We just
1022 * use the first
1023 */
1024 if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
1025 debug("%s: Failed to decode memory region %s\n", __func__,
1026 mem);
1027 return -EINVAL;
1028 }
1029
1030 snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
1031 suffix);
1032 if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
1033 &offset_size)) {
1034 debug("%s: Failed to decode memory region '%s'\n", __func__,
1035 prop_name);
1036 return -EINVAL;
1037 }
1038
1039 *basep = base + offset;
1040 *sizep = offset_size;
1041
1042 return 0;
1043}
Simon Glassb45122f2015-02-27 22:06:34 -07001044
Simon Glass12e67112015-04-14 21:03:21 -06001045static int decode_timing_property(const void *blob, int node, const char *name,
1046 struct timing_entry *result)
1047{
1048 int length, ret = 0;
1049 const u32 *prop;
1050
1051 prop = fdt_getprop(blob, node, name, &length);
1052 if (!prop) {
1053 debug("%s: could not find property %s\n",
1054 fdt_get_name(blob, node, NULL), name);
1055 return length;
1056 }
1057
1058 if (length == sizeof(u32)) {
1059 result->typ = fdtdec_get_int(blob, node, name, 0);
1060 result->min = result->typ;
1061 result->max = result->typ;
1062 } else {
1063 ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
1064 }
1065
1066 return ret;
1067}
1068
1069int fdtdec_decode_display_timing(const void *blob, int parent, int index,
1070 struct display_timing *dt)
1071{
1072 int i, node, timings_node;
1073 u32 val = 0;
1074 int ret = 0;
1075
1076 timings_node = fdt_subnode_offset(blob, parent, "display-timings");
1077 if (timings_node < 0)
1078 return timings_node;
1079
1080 for (i = 0, node = fdt_first_subnode(blob, timings_node);
1081 node > 0 && i != index;
1082 node = fdt_next_subnode(blob, node))
1083 i++;
1084
1085 if (node < 0)
1086 return node;
1087
1088 memset(dt, 0, sizeof(*dt));
1089
1090 ret |= decode_timing_property(blob, node, "hback-porch",
1091 &dt->hback_porch);
1092 ret |= decode_timing_property(blob, node, "hfront-porch",
1093 &dt->hfront_porch);
1094 ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
1095 ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
1096 ret |= decode_timing_property(blob, node, "vback-porch",
1097 &dt->vback_porch);
1098 ret |= decode_timing_property(blob, node, "vfront-porch",
1099 &dt->vfront_porch);
1100 ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
1101 ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
1102 ret |= decode_timing_property(blob, node, "clock-frequency",
1103 &dt->pixelclock);
1104
1105 dt->flags = 0;
1106 val = fdtdec_get_int(blob, node, "vsync-active", -1);
1107 if (val != -1) {
1108 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
1109 DISPLAY_FLAGS_VSYNC_LOW;
1110 }
1111 val = fdtdec_get_int(blob, node, "hsync-active", -1);
1112 if (val != -1) {
1113 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
1114 DISPLAY_FLAGS_HSYNC_LOW;
1115 }
1116 val = fdtdec_get_int(blob, node, "de-active", -1);
1117 if (val != -1) {
1118 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
1119 DISPLAY_FLAGS_DE_LOW;
1120 }
1121 val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
1122 if (val != -1) {
1123 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
1124 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
1125 }
1126
1127 if (fdtdec_get_bool(blob, node, "interlaced"))
1128 dt->flags |= DISPLAY_FLAGS_INTERLACED;
1129 if (fdtdec_get_bool(blob, node, "doublescan"))
1130 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
1131 if (fdtdec_get_bool(blob, node, "doubleclk"))
1132 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
1133
1134 return 0;
1135}
1136
Simon Glass08793612015-02-27 22:06:35 -07001137int fdtdec_setup(void)
Simon Glassb45122f2015-02-27 22:06:34 -07001138{
1139#ifdef CONFIG_OF_CONTROL
1140# ifdef CONFIG_OF_EMBED
1141 /* Get a pointer to the FDT */
1142 gd->fdt_blob = __dtb_dt_begin;
1143# elif defined CONFIG_OF_SEPARATE
1144# ifdef CONFIG_SPL_BUILD
1145 /* FDT is at end of BSS */
1146 gd->fdt_blob = (ulong *)&__bss_end;
1147# else
1148 /* FDT is at end of image */
1149 gd->fdt_blob = (ulong *)&_end;
Heiko Schocher29a23f92014-03-03 12:19:30 +01001150#endif
Simon Glassb45122f2015-02-27 22:06:34 -07001151# elif defined(CONFIG_OF_HOSTFILE)
1152 if (sandbox_read_fdt_from_file()) {
1153 puts("Failed to read control FDT\n");
1154 return -1;
1155 }
1156# endif
1157# ifndef CONFIG_SPL_BUILD
1158 /* Allow the early environment to override the fdt address */
1159 gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
1160 (uintptr_t)gd->fdt_blob);
1161# endif
1162#endif
Simon Glass08793612015-02-27 22:06:35 -07001163 return fdtdec_prepare_fdt();
Simon Glassb45122f2015-02-27 22:06:34 -07001164}
1165
1166#endif /* !USE_HOSTCC */