fdtdec.c 33 KB

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