fdtdec.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  4. */
  5. #ifndef USE_HOSTCC
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <serial.h>
  10. #include <libfdt.h>
  11. #include <fdt_support.h>
  12. #include <fdtdec.h>
  13. #include <asm/sections.h>
  14. #include <linux/ctype.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. /*
  17. * Here are the type we know about. One day we might allow drivers to
  18. * register. For now we just put them here. The COMPAT macro allows us to
  19. * turn this into a sparse list later, and keeps the ID with the name.
  20. *
  21. * NOTE: This list is basically a TODO list for things that need to be
  22. * converted to driver model. So don't add new things here unless there is a
  23. * good reason why driver-model conversion is infeasible. Examples include
  24. * things which are used before driver model is available.
  25. */
  26. #define COMPAT(id, name) name
  27. static const char * const compat_names[COMPAT_COUNT] = {
  28. COMPAT(UNKNOWN, "<none>"),
  29. COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
  30. COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
  31. COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
  32. COMPAT(NVIDIA_TEGRA124_PMC, "nvidia,tegra124-pmc"),
  33. COMPAT(NVIDIA_TEGRA186_SDMMC, "nvidia,tegra186-sdhci"),
  34. COMPAT(NVIDIA_TEGRA210_SDMMC, "nvidia,tegra210-sdhci"),
  35. COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
  36. COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
  37. COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
  38. COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
  39. COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
  40. COMPAT(SMSC_LAN9215, "smsc,lan9215"),
  41. COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
  42. COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
  43. COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
  44. COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
  45. COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
  46. COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
  47. COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
  48. COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
  49. COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
  50. COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
  51. COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686"),
  52. COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
  53. COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
  54. COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
  55. COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
  56. COMPAT(INTEL_MICROCODE, "intel,microcode"),
  57. COMPAT(AMS_AS3722, "ams,as3722"),
  58. COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
  59. COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
  60. COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"),
  61. COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"),
  62. COMPAT(INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
  63. COMPAT(INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
  64. COMPAT(INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"),
  65. COMPAT(COMPAT_SUNXI_NAND, "allwinner,sun4i-a10-nand"),
  66. COMPAT(ALTERA_SOCFPGA_CLK, "altr,clk-mgr"),
  67. COMPAT(ALTERA_SOCFPGA_PINCTRL_SINGLE, "pinctrl-single"),
  68. COMPAT(ALTERA_SOCFPGA_H2F_BRG, "altr,socfpga-hps2fpga-bridge"),
  69. COMPAT(ALTERA_SOCFPGA_LWH2F_BRG, "altr,socfpga-lwhps2fpga-bridge"),
  70. COMPAT(ALTERA_SOCFPGA_F2H_BRG, "altr,socfpga-fpga2hps-bridge"),
  71. COMPAT(ALTERA_SOCFPGA_F2SDR0, "altr,socfpga-fpga2sdram0-bridge"),
  72. COMPAT(ALTERA_SOCFPGA_F2SDR1, "altr,socfpga-fpga2sdram1-bridge"),
  73. COMPAT(ALTERA_SOCFPGA_F2SDR2, "altr,socfpga-fpga2sdram2-bridge"),
  74. };
  75. const char *fdtdec_get_compatible(enum fdt_compat_id id)
  76. {
  77. /* We allow reading of the 'unknown' ID for testing purposes */
  78. assert(id >= 0 && id < COMPAT_COUNT);
  79. return compat_names[id];
  80. }
  81. fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
  82. const char *prop_name, int index, int na, int ns,
  83. fdt_size_t *sizep, 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. debug("%s: %s: ", __func__, prop_name);
  90. if (na > (sizeof(fdt_addr_t) / sizeof(fdt32_t))) {
  91. debug("(na too large for fdt_addr_t type)\n");
  92. return FDT_ADDR_T_NONE;
  93. }
  94. if (ns > (sizeof(fdt_size_t) / sizeof(fdt32_t))) {
  95. debug("(ns too large for fdt_size_t type)\n");
  96. return FDT_ADDR_T_NONE;
  97. }
  98. prop = fdt_getprop(blob, node, prop_name, &len);
  99. if (!prop) {
  100. debug("(not found)\n");
  101. return FDT_ADDR_T_NONE;
  102. }
  103. prop_end = prop + (len / sizeof(*prop));
  104. prop_addr = prop + (index * (na + ns));
  105. prop_size = prop_addr + na;
  106. prop_after_size = prop_size + ns;
  107. if (prop_after_size > prop_end) {
  108. debug("(not enough data: expected >= %d cells, got %d cells)\n",
  109. (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
  110. return FDT_ADDR_T_NONE;
  111. }
  112. #if CONFIG_IS_ENABLED(OF_TRANSLATE)
  113. if (translate)
  114. addr = fdt_translate_address(blob, node, prop_addr);
  115. else
  116. #endif
  117. addr = fdtdec_get_number(prop_addr, na);
  118. if (sizep) {
  119. *sizep = fdtdec_get_number(prop_size, ns);
  120. debug("addr=%08llx, size=%llx\n", (unsigned long long)addr,
  121. (unsigned long long)*sizep);
  122. } else {
  123. debug("addr=%08llx\n", (unsigned long long)addr);
  124. }
  125. return addr;
  126. }
  127. fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
  128. int node, const char *prop_name, int index, fdt_size_t *sizep,
  129. bool translate)
  130. {
  131. int na, ns;
  132. debug("%s: ", __func__);
  133. na = fdt_address_cells(blob, parent);
  134. if (na < 1) {
  135. debug("(bad #address-cells)\n");
  136. return FDT_ADDR_T_NONE;
  137. }
  138. ns = fdt_size_cells(blob, parent);
  139. if (ns < 0) {
  140. debug("(bad #size-cells)\n");
  141. return FDT_ADDR_T_NONE;
  142. }
  143. debug("na=%d, ns=%d, ", na, ns);
  144. return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na,
  145. ns, sizep, translate);
  146. }
  147. fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
  148. const char *prop_name, int index, fdt_size_t *sizep,
  149. bool translate)
  150. {
  151. int parent;
  152. debug("%s: ", __func__);
  153. parent = fdt_parent_offset(blob, node);
  154. if (parent < 0) {
  155. debug("(no parent found)\n");
  156. return FDT_ADDR_T_NONE;
  157. }
  158. return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name,
  159. index, sizep, translate);
  160. }
  161. fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
  162. const char *prop_name, fdt_size_t *sizep)
  163. {
  164. int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0;
  165. return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0,
  166. sizeof(fdt_addr_t) / sizeof(fdt32_t),
  167. ns, sizep, false);
  168. }
  169. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  170. const char *prop_name)
  171. {
  172. return fdtdec_get_addr_size(blob, node, prop_name, NULL);
  173. }
  174. #if defined(CONFIG_PCI) && defined(CONFIG_DM_PCI)
  175. int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
  176. const char *prop_name, struct fdt_pci_addr *addr)
  177. {
  178. const u32 *cell;
  179. int len;
  180. int ret = -ENOENT;
  181. debug("%s: %s: ", __func__, prop_name);
  182. /*
  183. * If we follow the pci bus bindings strictly, we should check
  184. * the value of the node's parent node's #address-cells and
  185. * #size-cells. They need to be 3 and 2 accordingly. However,
  186. * for simplicity we skip the check here.
  187. */
  188. cell = fdt_getprop(blob, node, prop_name, &len);
  189. if (!cell)
  190. goto fail;
  191. if ((len % FDT_PCI_REG_SIZE) == 0) {
  192. int num = len / FDT_PCI_REG_SIZE;
  193. int i;
  194. for (i = 0; i < num; i++) {
  195. debug("pci address #%d: %08lx %08lx %08lx\n", i,
  196. (ulong)fdt32_to_cpu(cell[0]),
  197. (ulong)fdt32_to_cpu(cell[1]),
  198. (ulong)fdt32_to_cpu(cell[2]));
  199. if ((fdt32_to_cpu(*cell) & type) == type) {
  200. addr->phys_hi = fdt32_to_cpu(cell[0]);
  201. addr->phys_mid = fdt32_to_cpu(cell[1]);
  202. addr->phys_lo = fdt32_to_cpu(cell[1]);
  203. break;
  204. } else {
  205. cell += (FDT_PCI_ADDR_CELLS +
  206. FDT_PCI_SIZE_CELLS);
  207. }
  208. }
  209. if (i == num) {
  210. ret = -ENXIO;
  211. goto fail;
  212. }
  213. return 0;
  214. } else {
  215. ret = -EINVAL;
  216. }
  217. fail:
  218. debug("(not found)\n");
  219. return ret;
  220. }
  221. int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
  222. {
  223. const char *list, *end;
  224. int len;
  225. list = fdt_getprop(blob, node, "compatible", &len);
  226. if (!list)
  227. return -ENOENT;
  228. end = list + len;
  229. while (list < end) {
  230. char *s;
  231. len = strlen(list);
  232. if (len >= strlen("pciVVVV,DDDD")) {
  233. s = strstr(list, "pci");
  234. /*
  235. * check if the string is something like pciVVVV,DDDD.RR
  236. * or just pciVVVV,DDDD
  237. */
  238. if (s && s[7] == ',' &&
  239. (s[12] == '.' || s[12] == 0)) {
  240. s += 3;
  241. *vendor = simple_strtol(s, NULL, 16);
  242. s += 5;
  243. *device = simple_strtol(s, NULL, 16);
  244. return 0;
  245. }
  246. }
  247. list += (len + 1);
  248. }
  249. return -ENOENT;
  250. }
  251. int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
  252. u32 *bar)
  253. {
  254. int barnum;
  255. /* extract the bar number from fdt_pci_addr */
  256. barnum = addr->phys_hi & 0xff;
  257. if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
  258. return -EINVAL;
  259. barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
  260. *bar = dm_pci_read_bar32(dev, barnum);
  261. return 0;
  262. }
  263. #endif
  264. uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
  265. uint64_t default_val)
  266. {
  267. const uint64_t *cell64;
  268. int length;
  269. cell64 = fdt_getprop(blob, node, prop_name, &length);
  270. if (!cell64 || length < sizeof(*cell64))
  271. return default_val;
  272. return fdt64_to_cpu(*cell64);
  273. }
  274. int fdtdec_get_is_enabled(const void *blob, int node)
  275. {
  276. const char *cell;
  277. /*
  278. * It should say "okay", so only allow that. Some fdts use "ok" but
  279. * this is a bug. Please fix your device tree source file. See here
  280. * for discussion:
  281. *
  282. * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
  283. */
  284. cell = fdt_getprop(blob, node, "status", NULL);
  285. if (cell)
  286. return 0 == strcmp(cell, "okay");
  287. return 1;
  288. }
  289. enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
  290. {
  291. enum fdt_compat_id id;
  292. /* Search our drivers */
  293. for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
  294. if (0 == fdt_node_check_compatible(blob, node,
  295. compat_names[id]))
  296. return id;
  297. return COMPAT_UNKNOWN;
  298. }
  299. int fdtdec_next_compatible(const void *blob, int node,
  300. enum fdt_compat_id id)
  301. {
  302. return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
  303. }
  304. int fdtdec_next_compatible_subnode(const void *blob, int node,
  305. enum fdt_compat_id id, int *depthp)
  306. {
  307. do {
  308. node = fdt_next_node(blob, node, depthp);
  309. } while (*depthp > 1);
  310. /* If this is a direct subnode, and compatible, return it */
  311. if (*depthp == 1 && 0 == fdt_node_check_compatible(
  312. blob, node, compat_names[id]))
  313. return node;
  314. return -FDT_ERR_NOTFOUND;
  315. }
  316. int fdtdec_next_alias(const void *blob, const char *name,
  317. enum fdt_compat_id id, int *upto)
  318. {
  319. #define MAX_STR_LEN 20
  320. char str[MAX_STR_LEN + 20];
  321. int node, err;
  322. /* snprintf() is not available */
  323. assert(strlen(name) < MAX_STR_LEN);
  324. sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
  325. node = fdt_path_offset(blob, str);
  326. if (node < 0)
  327. return node;
  328. err = fdt_node_check_compatible(blob, node, compat_names[id]);
  329. if (err < 0)
  330. return err;
  331. if (err)
  332. return -FDT_ERR_NOTFOUND;
  333. (*upto)++;
  334. return node;
  335. }
  336. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  337. enum fdt_compat_id id, int *node_list, int maxcount)
  338. {
  339. memset(node_list, '\0', sizeof(*node_list) * maxcount);
  340. return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
  341. }
  342. /* TODO: Can we tighten this code up a little? */
  343. int fdtdec_add_aliases_for_id(const void *blob, const char *name,
  344. enum fdt_compat_id id, int *node_list, int maxcount)
  345. {
  346. int name_len = strlen(name);
  347. int nodes[maxcount];
  348. int num_found = 0;
  349. int offset, node;
  350. int alias_node;
  351. int count;
  352. int i, j;
  353. /* find the alias node if present */
  354. alias_node = fdt_path_offset(blob, "/aliases");
  355. /*
  356. * start with nothing, and we can assume that the root node can't
  357. * match
  358. */
  359. memset(nodes, '\0', sizeof(nodes));
  360. /* First find all the compatible nodes */
  361. for (node = count = 0; node >= 0 && count < maxcount;) {
  362. node = fdtdec_next_compatible(blob, node, id);
  363. if (node >= 0)
  364. nodes[count++] = node;
  365. }
  366. if (node >= 0)
  367. debug("%s: warning: maxcount exceeded with alias '%s'\n",
  368. __func__, name);
  369. /* Now find all the aliases */
  370. for (offset = fdt_first_property_offset(blob, alias_node);
  371. offset > 0;
  372. offset = fdt_next_property_offset(blob, offset)) {
  373. const struct fdt_property *prop;
  374. const char *path;
  375. int number;
  376. int found;
  377. node = 0;
  378. prop = fdt_get_property_by_offset(blob, offset, NULL);
  379. path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  380. if (prop->len && 0 == strncmp(path, name, name_len))
  381. node = fdt_path_offset(blob, prop->data);
  382. if (node <= 0)
  383. continue;
  384. /* Get the alias number */
  385. number = simple_strtoul(path + name_len, NULL, 10);
  386. if (number < 0 || number >= maxcount) {
  387. debug("%s: warning: alias '%s' is out of range\n",
  388. __func__, path);
  389. continue;
  390. }
  391. /* Make sure the node we found is actually in our list! */
  392. found = -1;
  393. for (j = 0; j < count; j++)
  394. if (nodes[j] == node) {
  395. found = j;
  396. break;
  397. }
  398. if (found == -1) {
  399. debug("%s: warning: alias '%s' points to a node "
  400. "'%s' that is missing or is not compatible "
  401. " with '%s'\n", __func__, path,
  402. fdt_get_name(blob, node, NULL),
  403. compat_names[id]);
  404. continue;
  405. }
  406. /*
  407. * Add this node to our list in the right place, and mark
  408. * it as done.
  409. */
  410. if (fdtdec_get_is_enabled(blob, node)) {
  411. if (node_list[number]) {
  412. debug("%s: warning: alias '%s' requires that "
  413. "a node be placed in the list in a "
  414. "position which is already filled by "
  415. "node '%s'\n", __func__, path,
  416. fdt_get_name(blob, node, NULL));
  417. continue;
  418. }
  419. node_list[number] = node;
  420. if (number >= num_found)
  421. num_found = number + 1;
  422. }
  423. nodes[found] = 0;
  424. }
  425. /* Add any nodes not mentioned by an alias */
  426. for (i = j = 0; i < maxcount; i++) {
  427. if (!node_list[i]) {
  428. for (; j < maxcount; j++)
  429. if (nodes[j] &&
  430. fdtdec_get_is_enabled(blob, nodes[j]))
  431. break;
  432. /* Have we run out of nodes to add? */
  433. if (j == maxcount)
  434. break;
  435. assert(!node_list[i]);
  436. node_list[i] = nodes[j++];
  437. if (i >= num_found)
  438. num_found = i + 1;
  439. }
  440. }
  441. return num_found;
  442. }
  443. int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
  444. int *seqp)
  445. {
  446. int base_len = strlen(base);
  447. const char *find_name;
  448. int find_namelen;
  449. int prop_offset;
  450. int aliases;
  451. find_name = fdt_get_name(blob, offset, &find_namelen);
  452. debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
  453. aliases = fdt_path_offset(blob, "/aliases");
  454. for (prop_offset = fdt_first_property_offset(blob, aliases);
  455. prop_offset > 0;
  456. prop_offset = fdt_next_property_offset(blob, prop_offset)) {
  457. const char *prop;
  458. const char *name;
  459. const char *slash;
  460. int len, val;
  461. prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
  462. debug(" - %s, %s\n", name, prop);
  463. if (len < find_namelen || *prop != '/' || prop[len - 1] ||
  464. strncmp(name, base, base_len))
  465. continue;
  466. slash = strrchr(prop, '/');
  467. if (strcmp(slash + 1, find_name))
  468. continue;
  469. val = trailing_strtol(name);
  470. if (val != -1) {
  471. *seqp = val;
  472. debug("Found seq %d\n", *seqp);
  473. return 0;
  474. }
  475. }
  476. debug("Not found\n");
  477. return -ENOENT;
  478. }
  479. const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
  480. {
  481. int chosen_node;
  482. if (!blob)
  483. return NULL;
  484. chosen_node = fdt_path_offset(blob, "/chosen");
  485. return fdt_getprop(blob, chosen_node, name, NULL);
  486. }
  487. int fdtdec_get_chosen_node(const void *blob, const char *name)
  488. {
  489. const char *prop;
  490. prop = fdtdec_get_chosen_prop(blob, name);
  491. if (!prop)
  492. return -FDT_ERR_NOTFOUND;
  493. return fdt_path_offset(blob, prop);
  494. }
  495. int fdtdec_check_fdt(void)
  496. {
  497. /*
  498. * We must have an FDT, but we cannot panic() yet since the console
  499. * is not ready. So for now, just assert(). Boards which need an early
  500. * FDT (prior to console ready) will need to make their own
  501. * arrangements and do their own checks.
  502. */
  503. assert(!fdtdec_prepare_fdt());
  504. return 0;
  505. }
  506. /*
  507. * This function is a little odd in that it accesses global data. At some
  508. * point if the architecture board.c files merge this will make more sense.
  509. * Even now, it is common code.
  510. */
  511. int fdtdec_prepare_fdt(void)
  512. {
  513. if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
  514. fdt_check_header(gd->fdt_blob)) {
  515. #ifdef CONFIG_SPL_BUILD
  516. puts("Missing DTB\n");
  517. #else
  518. 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");
  519. # ifdef DEBUG
  520. if (gd->fdt_blob) {
  521. printf("fdt_blob=%p\n", gd->fdt_blob);
  522. print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4,
  523. 32, 0);
  524. }
  525. # endif
  526. #endif
  527. return -1;
  528. }
  529. return 0;
  530. }
  531. int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
  532. {
  533. const u32 *phandle;
  534. int lookup;
  535. debug("%s: %s\n", __func__, prop_name);
  536. phandle = fdt_getprop(blob, node, prop_name, NULL);
  537. if (!phandle)
  538. return -FDT_ERR_NOTFOUND;
  539. lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
  540. return lookup;
  541. }
  542. /**
  543. * Look up a property in a node and check that it has a minimum length.
  544. *
  545. * @param blob FDT blob
  546. * @param node node to examine
  547. * @param prop_name name of property to find
  548. * @param min_len minimum property length in bytes
  549. * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
  550. found, or -FDT_ERR_BADLAYOUT if not enough data
  551. * @return pointer to cell, which is only valid if err == 0
  552. */
  553. static const void *get_prop_check_min_len(const void *blob, int node,
  554. const char *prop_name, int min_len, int *err)
  555. {
  556. const void *cell;
  557. int len;
  558. debug("%s: %s\n", __func__, prop_name);
  559. cell = fdt_getprop(blob, node, prop_name, &len);
  560. if (!cell)
  561. *err = -FDT_ERR_NOTFOUND;
  562. else if (len < min_len)
  563. *err = -FDT_ERR_BADLAYOUT;
  564. else
  565. *err = 0;
  566. return cell;
  567. }
  568. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  569. u32 *array, int count)
  570. {
  571. const u32 *cell;
  572. int i, err = 0;
  573. debug("%s: %s\n", __func__, prop_name);
  574. cell = get_prop_check_min_len(blob, node, prop_name,
  575. sizeof(u32) * count, &err);
  576. if (!err) {
  577. for (i = 0; i < count; i++)
  578. array[i] = fdt32_to_cpu(cell[i]);
  579. }
  580. return err;
  581. }
  582. int fdtdec_get_int_array_count(const void *blob, int node,
  583. const char *prop_name, u32 *array, int count)
  584. {
  585. const u32 *cell;
  586. int len, elems;
  587. int i;
  588. debug("%s: %s\n", __func__, prop_name);
  589. cell = fdt_getprop(blob, node, prop_name, &len);
  590. if (!cell)
  591. return -FDT_ERR_NOTFOUND;
  592. elems = len / sizeof(u32);
  593. if (count > elems)
  594. count = elems;
  595. for (i = 0; i < count; i++)
  596. array[i] = fdt32_to_cpu(cell[i]);
  597. return count;
  598. }
  599. const u32 *fdtdec_locate_array(const void *blob, int node,
  600. const char *prop_name, int count)
  601. {
  602. const u32 *cell;
  603. int err;
  604. cell = get_prop_check_min_len(blob, node, prop_name,
  605. sizeof(u32) * count, &err);
  606. return err ? NULL : cell;
  607. }
  608. int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
  609. {
  610. const s32 *cell;
  611. int len;
  612. debug("%s: %s\n", __func__, prop_name);
  613. cell = fdt_getprop(blob, node, prop_name, &len);
  614. return cell != NULL;
  615. }
  616. int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
  617. const char *list_name,
  618. const char *cells_name,
  619. int cell_count, int index,
  620. struct fdtdec_phandle_args *out_args)
  621. {
  622. const __be32 *list, *list_end;
  623. int rc = 0, size, cur_index = 0;
  624. uint32_t count = 0;
  625. int node = -1;
  626. int phandle;
  627. /* Retrieve the phandle list property */
  628. list = fdt_getprop(blob, src_node, list_name, &size);
  629. if (!list)
  630. return -ENOENT;
  631. list_end = list + size / sizeof(*list);
  632. /* Loop over the phandles until all the requested entry is found */
  633. while (list < list_end) {
  634. rc = -EINVAL;
  635. count = 0;
  636. /*
  637. * If phandle is 0, then it is an empty entry with no
  638. * arguments. Skip forward to the next entry.
  639. */
  640. phandle = be32_to_cpup(list++);
  641. if (phandle) {
  642. /*
  643. * Find the provider node and parse the #*-cells
  644. * property to determine the argument length.
  645. *
  646. * This is not needed if the cell count is hard-coded
  647. * (i.e. cells_name not set, but cell_count is set),
  648. * except when we're going to return the found node
  649. * below.
  650. */
  651. if (cells_name || cur_index == index) {
  652. node = fdt_node_offset_by_phandle(blob,
  653. phandle);
  654. if (!node) {
  655. debug("%s: could not find phandle\n",
  656. fdt_get_name(blob, src_node,
  657. NULL));
  658. goto err;
  659. }
  660. }
  661. if (cells_name) {
  662. count = fdtdec_get_int(blob, node, cells_name,
  663. -1);
  664. if (count == -1) {
  665. debug("%s: could not get %s for %s\n",
  666. fdt_get_name(blob, src_node,
  667. NULL),
  668. cells_name,
  669. fdt_get_name(blob, node,
  670. NULL));
  671. goto err;
  672. }
  673. } else {
  674. count = cell_count;
  675. }
  676. /*
  677. * Make sure that the arguments actually fit in the
  678. * remaining property data length
  679. */
  680. if (list + count > list_end) {
  681. debug("%s: arguments longer than property\n",
  682. fdt_get_name(blob, src_node, NULL));
  683. goto err;
  684. }
  685. }
  686. /*
  687. * All of the error cases above bail out of the loop, so at
  688. * this point, the parsing is successful. If the requested
  689. * index matches, then fill the out_args structure and return,
  690. * or return -ENOENT for an empty entry.
  691. */
  692. rc = -ENOENT;
  693. if (cur_index == index) {
  694. if (!phandle)
  695. goto err;
  696. if (out_args) {
  697. int i;
  698. if (count > MAX_PHANDLE_ARGS) {
  699. debug("%s: too many arguments %d\n",
  700. fdt_get_name(blob, src_node,
  701. NULL), count);
  702. count = MAX_PHANDLE_ARGS;
  703. }
  704. out_args->node = node;
  705. out_args->args_count = count;
  706. for (i = 0; i < count; i++) {
  707. out_args->args[i] =
  708. be32_to_cpup(list++);
  709. }
  710. }
  711. /* Found it! return success */
  712. return 0;
  713. }
  714. node = -1;
  715. list += count;
  716. cur_index++;
  717. }
  718. /*
  719. * Result will be one of:
  720. * -ENOENT : index is for empty phandle
  721. * -EINVAL : parsing error on data
  722. * [1..n] : Number of phandle (count mode; when index = -1)
  723. */
  724. rc = index < 0 ? cur_index : -ENOENT;
  725. err:
  726. return rc;
  727. }
  728. int fdtdec_get_child_count(const void *blob, int node)
  729. {
  730. int subnode;
  731. int num = 0;
  732. fdt_for_each_subnode(subnode, blob, node)
  733. num++;
  734. return num;
  735. }
  736. int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
  737. u8 *array, int count)
  738. {
  739. const u8 *cell;
  740. int err;
  741. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  742. if (!err)
  743. memcpy(array, cell, count);
  744. return err;
  745. }
  746. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  747. const char *prop_name, int count)
  748. {
  749. const u8 *cell;
  750. int err;
  751. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  752. if (err)
  753. return NULL;
  754. return cell;
  755. }
  756. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  757. int default_val)
  758. {
  759. int config_node;
  760. debug("%s: %s\n", __func__, prop_name);
  761. config_node = fdt_path_offset(blob, "/config");
  762. if (config_node < 0)
  763. return default_val;
  764. return fdtdec_get_int(blob, config_node, prop_name, default_val);
  765. }
  766. int fdtdec_get_config_bool(const void *blob, const char *prop_name)
  767. {
  768. int config_node;
  769. const void *prop;
  770. debug("%s: %s\n", __func__, prop_name);
  771. config_node = fdt_path_offset(blob, "/config");
  772. if (config_node < 0)
  773. return 0;
  774. prop = fdt_get_property(blob, config_node, prop_name, NULL);
  775. return prop != NULL;
  776. }
  777. char *fdtdec_get_config_string(const void *blob, const char *prop_name)
  778. {
  779. const char *nodep;
  780. int nodeoffset;
  781. int len;
  782. debug("%s: %s\n", __func__, prop_name);
  783. nodeoffset = fdt_path_offset(blob, "/config");
  784. if (nodeoffset < 0)
  785. return NULL;
  786. nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
  787. if (!nodep)
  788. return NULL;
  789. return (char *)nodep;
  790. }
  791. int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
  792. fdt_addr_t *basep, fdt_size_t *sizep)
  793. {
  794. const fdt_addr_t *cell;
  795. int len;
  796. debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
  797. prop_name);
  798. cell = fdt_getprop(blob, node, prop_name, &len);
  799. if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
  800. debug("cell=%p, len=%d\n", cell, len);
  801. return -1;
  802. }
  803. *basep = fdt_addr_to_cpu(*cell);
  804. *sizep = fdt_size_to_cpu(cell[1]);
  805. debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
  806. (ulong)*sizep);
  807. return 0;
  808. }
  809. /**
  810. * Read a flash entry from the fdt
  811. *
  812. * @param blob FDT blob
  813. * @param node Offset of node to read
  814. * @param name Name of node being read
  815. * @param entry Place to put offset and size of this node
  816. * @return 0 if ok, -ve on error
  817. */
  818. int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
  819. struct fmap_entry *entry)
  820. {
  821. const char *prop;
  822. u32 reg[2];
  823. if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
  824. debug("Node '%s' has bad/missing 'reg' property\n", name);
  825. return -FDT_ERR_NOTFOUND;
  826. }
  827. entry->offset = reg[0];
  828. entry->length = reg[1];
  829. entry->used = fdtdec_get_int(blob, node, "used", entry->length);
  830. prop = fdt_getprop(blob, node, "compress", NULL);
  831. entry->compress_algo = prop && !strcmp(prop, "lzo") ?
  832. FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
  833. prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
  834. entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
  835. entry->hash = (uint8_t *)prop;
  836. return 0;
  837. }
  838. u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
  839. {
  840. u64 number = 0;
  841. while (cells--)
  842. number = (number << 32) | fdt32_to_cpu(*ptr++);
  843. return number;
  844. }
  845. int fdt_get_resource(const void *fdt, int node, const char *property,
  846. unsigned int index, struct fdt_resource *res)
  847. {
  848. const fdt32_t *ptr, *end;
  849. int na, ns, len, parent;
  850. unsigned int i = 0;
  851. parent = fdt_parent_offset(fdt, node);
  852. if (parent < 0)
  853. return parent;
  854. na = fdt_address_cells(fdt, parent);
  855. ns = fdt_size_cells(fdt, parent);
  856. ptr = fdt_getprop(fdt, node, property, &len);
  857. if (!ptr)
  858. return len;
  859. end = ptr + len / sizeof(*ptr);
  860. while (ptr + na + ns <= end) {
  861. if (i == index) {
  862. res->start = res->end = fdtdec_get_number(ptr, na);
  863. res->end += fdtdec_get_number(&ptr[na], ns) - 1;
  864. return 0;
  865. }
  866. ptr += na + ns;
  867. i++;
  868. }
  869. return -FDT_ERR_NOTFOUND;
  870. }
  871. int fdt_get_named_resource(const void *fdt, int node, const char *property,
  872. const char *prop_names, const char *name,
  873. struct fdt_resource *res)
  874. {
  875. int index;
  876. index = fdt_stringlist_search(fdt, node, prop_names, name);
  877. if (index < 0)
  878. return index;
  879. return fdt_get_resource(fdt, node, property, index, res);
  880. }
  881. int fdtdec_decode_memory_region(const void *blob, int config_node,
  882. const char *mem_type, const char *suffix,
  883. fdt_addr_t *basep, fdt_size_t *sizep)
  884. {
  885. char prop_name[50];
  886. const char *mem;
  887. fdt_size_t size, offset_size;
  888. fdt_addr_t base, offset;
  889. int node;
  890. if (config_node == -1) {
  891. config_node = fdt_path_offset(blob, "/config");
  892. if (config_node < 0) {
  893. debug("%s: Cannot find /config node\n", __func__);
  894. return -ENOENT;
  895. }
  896. }
  897. if (!suffix)
  898. suffix = "";
  899. snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
  900. suffix);
  901. mem = fdt_getprop(blob, config_node, prop_name, NULL);
  902. if (!mem) {
  903. debug("%s: No memory type for '%s', using /memory\n", __func__,
  904. prop_name);
  905. mem = "/memory";
  906. }
  907. node = fdt_path_offset(blob, mem);
  908. if (node < 0) {
  909. debug("%s: Failed to find node '%s': %s\n", __func__, mem,
  910. fdt_strerror(node));
  911. return -ENOENT;
  912. }
  913. /*
  914. * Not strictly correct - the memory may have multiple banks. We just
  915. * use the first
  916. */
  917. if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
  918. debug("%s: Failed to decode memory region %s\n", __func__,
  919. mem);
  920. return -EINVAL;
  921. }
  922. snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
  923. suffix);
  924. if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
  925. &offset_size)) {
  926. debug("%s: Failed to decode memory region '%s'\n", __func__,
  927. prop_name);
  928. return -EINVAL;
  929. }
  930. *basep = base + offset;
  931. *sizep = offset_size;
  932. return 0;
  933. }
  934. static int decode_timing_property(const void *blob, int node, const char *name,
  935. struct timing_entry *result)
  936. {
  937. int length, ret = 0;
  938. const u32 *prop;
  939. prop = fdt_getprop(blob, node, name, &length);
  940. if (!prop) {
  941. debug("%s: could not find property %s\n",
  942. fdt_get_name(blob, node, NULL), name);
  943. return length;
  944. }
  945. if (length == sizeof(u32)) {
  946. result->typ = fdtdec_get_int(blob, node, name, 0);
  947. result->min = result->typ;
  948. result->max = result->typ;
  949. } else {
  950. ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
  951. }
  952. return ret;
  953. }
  954. int fdtdec_decode_display_timing(const void *blob, int parent, int index,
  955. struct display_timing *dt)
  956. {
  957. int i, node, timings_node;
  958. u32 val = 0;
  959. int ret = 0;
  960. timings_node = fdt_subnode_offset(blob, parent, "display-timings");
  961. if (timings_node < 0)
  962. return timings_node;
  963. for (i = 0, node = fdt_first_subnode(blob, timings_node);
  964. node > 0 && i != index;
  965. node = fdt_next_subnode(blob, node))
  966. i++;
  967. if (node < 0)
  968. return node;
  969. memset(dt, 0, sizeof(*dt));
  970. ret |= decode_timing_property(blob, node, "hback-porch",
  971. &dt->hback_porch);
  972. ret |= decode_timing_property(blob, node, "hfront-porch",
  973. &dt->hfront_porch);
  974. ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
  975. ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
  976. ret |= decode_timing_property(blob, node, "vback-porch",
  977. &dt->vback_porch);
  978. ret |= decode_timing_property(blob, node, "vfront-porch",
  979. &dt->vfront_porch);
  980. ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
  981. ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
  982. ret |= decode_timing_property(blob, node, "clock-frequency",
  983. &dt->pixelclock);
  984. dt->flags = 0;
  985. val = fdtdec_get_int(blob, node, "vsync-active", -1);
  986. if (val != -1) {
  987. dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
  988. DISPLAY_FLAGS_VSYNC_LOW;
  989. }
  990. val = fdtdec_get_int(blob, node, "hsync-active", -1);
  991. if (val != -1) {
  992. dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
  993. DISPLAY_FLAGS_HSYNC_LOW;
  994. }
  995. val = fdtdec_get_int(blob, node, "de-active", -1);
  996. if (val != -1) {
  997. dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
  998. DISPLAY_FLAGS_DE_LOW;
  999. }
  1000. val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
  1001. if (val != -1) {
  1002. dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
  1003. DISPLAY_FLAGS_PIXDATA_NEGEDGE;
  1004. }
  1005. if (fdtdec_get_bool(blob, node, "interlaced"))
  1006. dt->flags |= DISPLAY_FLAGS_INTERLACED;
  1007. if (fdtdec_get_bool(blob, node, "doublescan"))
  1008. dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
  1009. if (fdtdec_get_bool(blob, node, "doubleclk"))
  1010. dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
  1011. return ret;
  1012. }
  1013. int fdtdec_setup_memory_size(void)
  1014. {
  1015. int ret, mem;
  1016. struct fdt_resource res;
  1017. mem = fdt_path_offset(gd->fdt_blob, "/memory");
  1018. if (mem < 0) {
  1019. debug("%s: Missing /memory node\n", __func__);
  1020. return -EINVAL;
  1021. }
  1022. ret = fdt_get_resource(gd->fdt_blob, mem, "reg", 0, &res);
  1023. if (ret != 0) {
  1024. debug("%s: Unable to decode first memory bank\n", __func__);
  1025. return -EINVAL;
  1026. }
  1027. gd->ram_size = (phys_size_t)(res.end - res.start + 1);
  1028. debug("%s: Initial DRAM size %llx\n", __func__, (u64)gd->ram_size);
  1029. return 0;
  1030. }
  1031. #if defined(CONFIG_NR_DRAM_BANKS)
  1032. int fdtdec_setup_memory_banksize(void)
  1033. {
  1034. int bank, ret, mem;
  1035. struct fdt_resource res;
  1036. mem = fdt_path_offset(gd->fdt_blob, "/memory");
  1037. if (mem < 0) {
  1038. debug("%s: Missing /memory node\n", __func__);
  1039. return -EINVAL;
  1040. }
  1041. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  1042. ret = fdt_get_resource(gd->fdt_blob, mem, "reg", bank, &res);
  1043. if (ret == -FDT_ERR_NOTFOUND)
  1044. break;
  1045. if (ret != 0)
  1046. return -EINVAL;
  1047. gd->bd->bi_dram[bank].start = (phys_addr_t)res.start;
  1048. gd->bd->bi_dram[bank].size =
  1049. (phys_size_t)(res.end - res.start + 1);
  1050. debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n",
  1051. __func__, bank,
  1052. (unsigned long long)gd->bd->bi_dram[bank].start,
  1053. (unsigned long long)gd->bd->bi_dram[bank].size);
  1054. }
  1055. return 0;
  1056. }
  1057. #endif
  1058. int fdtdec_setup(void)
  1059. {
  1060. #if CONFIG_IS_ENABLED(OF_CONTROL)
  1061. # ifdef CONFIG_OF_EMBED
  1062. /* Get a pointer to the FDT */
  1063. gd->fdt_blob = __dtb_dt_begin;
  1064. # elif defined CONFIG_OF_SEPARATE
  1065. # ifdef CONFIG_SPL_BUILD
  1066. /* FDT is at end of BSS unless it is in a different memory region */
  1067. if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
  1068. gd->fdt_blob = (ulong *)&_image_binary_end;
  1069. else
  1070. gd->fdt_blob = (ulong *)&__bss_end;
  1071. # else
  1072. /* FDT is at end of image */
  1073. gd->fdt_blob = (ulong *)&_end;
  1074. # endif
  1075. # elif defined(CONFIG_OF_BOARD)
  1076. /* Allow the board to override the fdt address. */
  1077. gd->fdt_blob = board_fdt_blob_setup();
  1078. # elif defined(CONFIG_OF_HOSTFILE)
  1079. if (sandbox_read_fdt_from_file()) {
  1080. puts("Failed to read control FDT\n");
  1081. return -1;
  1082. }
  1083. # endif
  1084. # ifndef CONFIG_SPL_BUILD
  1085. /* Allow the early environment to override the fdt address */
  1086. gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
  1087. (uintptr_t)gd->fdt_blob);
  1088. # endif
  1089. #endif
  1090. return fdtdec_prepare_fdt();
  1091. }
  1092. #endif /* !USE_HOSTCC */