fdtdec.c 33 KB

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