fdtdec.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  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 <linux/libfdt.h>
  14. #include <serial.h>
  15. #include <asm/sections.h>
  16. #include <linux/ctype.h>
  17. #include <linux/ioport.h>
  18. #include <linux/lzo.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /*
  21. * Here are the type we know about. One day we might allow drivers to
  22. * register. For now we just put them here. The COMPAT macro allows us to
  23. * turn this into a sparse list later, and keeps the ID with the name.
  24. *
  25. * NOTE: This list is basically a TODO list for things that need to be
  26. * converted to driver model. So don't add new things here unless there is a
  27. * good reason why driver-model conversion is infeasible. Examples include
  28. * things which are used before driver model is available.
  29. */
  30. #define COMPAT(id, name) name
  31. static const char * const compat_names[COMPAT_COUNT] = {
  32. COMPAT(UNKNOWN, "<none>"),
  33. COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
  34. COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
  35. COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
  36. COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
  37. COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
  38. COMPAT(SMSC_LAN9215, "smsc,lan9215"),
  39. COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
  40. COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
  41. COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
  42. COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
  43. COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
  44. COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
  45. COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
  46. COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
  47. COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
  48. COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
  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. COMPAT(ALTERA_SOCFPGA_CLK_INIT, "altr,socfpga-a10-clk-init")
  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,
  83. int ns, fdt_size_t *sizep,
  84. bool translate)
  85. {
  86. const fdt32_t *prop, *prop_end;
  87. const fdt32_t *prop_addr, *prop_size, *prop_after_size;
  88. int len;
  89. fdt_addr_t addr;
  90. debug("%s: %s: ", __func__, prop_name);
  91. if (na > (sizeof(fdt_addr_t) / sizeof(fdt32_t))) {
  92. debug("(na too large for fdt_addr_t type)\n");
  93. return FDT_ADDR_T_NONE;
  94. }
  95. if (ns > (sizeof(fdt_size_t) / sizeof(fdt32_t))) {
  96. debug("(ns too large for fdt_size_t type)\n");
  97. return FDT_ADDR_T_NONE;
  98. }
  99. prop = fdt_getprop(blob, node, prop_name, &len);
  100. if (!prop) {
  101. debug("(not found)\n");
  102. return FDT_ADDR_T_NONE;
  103. }
  104. prop_end = prop + (len / sizeof(*prop));
  105. prop_addr = prop + (index * (na + ns));
  106. prop_size = prop_addr + na;
  107. prop_after_size = prop_size + ns;
  108. if (prop_after_size > prop_end) {
  109. debug("(not enough data: expected >= %d cells, got %d cells)\n",
  110. (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
  111. return FDT_ADDR_T_NONE;
  112. }
  113. #if CONFIG_IS_ENABLED(OF_TRANSLATE)
  114. if (translate)
  115. addr = fdt_translate_address(blob, node, prop_addr);
  116. else
  117. #endif
  118. addr = fdtdec_get_number(prop_addr, na);
  119. if (sizep) {
  120. *sizep = fdtdec_get_number(prop_size, ns);
  121. debug("addr=%08llx, size=%llx\n", (unsigned long long)addr,
  122. (unsigned long long)*sizep);
  123. } else {
  124. debug("addr=%08llx\n", (unsigned long long)addr);
  125. }
  126. return addr;
  127. }
  128. fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
  129. int node, const char *prop_name,
  130. int index, fdt_size_t *sizep,
  131. bool translate)
  132. {
  133. int na, ns;
  134. debug("%s: ", __func__);
  135. na = fdt_address_cells(blob, parent);
  136. if (na < 1) {
  137. debug("(bad #address-cells)\n");
  138. return FDT_ADDR_T_NONE;
  139. }
  140. ns = fdt_size_cells(blob, parent);
  141. if (ns < 0) {
  142. debug("(bad #size-cells)\n");
  143. return FDT_ADDR_T_NONE;
  144. }
  145. debug("na=%d, ns=%d, ", na, ns);
  146. return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na,
  147. ns, sizep, translate);
  148. }
  149. fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
  150. const char *prop_name, int index,
  151. fdt_size_t *sizep,
  152. bool translate)
  153. {
  154. int parent;
  155. debug("%s: ", __func__);
  156. parent = fdt_parent_offset(blob, node);
  157. if (parent < 0) {
  158. debug("(no parent found)\n");
  159. return FDT_ADDR_T_NONE;
  160. }
  161. return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name,
  162. index, sizep, translate);
  163. }
  164. fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
  165. const char *prop_name, fdt_size_t *sizep)
  166. {
  167. int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0;
  168. return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0,
  169. sizeof(fdt_addr_t) / sizeof(fdt32_t),
  170. ns, sizep, false);
  171. }
  172. fdt_addr_t fdtdec_get_addr(const void *blob, int node, const char *prop_name)
  173. {
  174. return fdtdec_get_addr_size(blob, node, prop_name, NULL);
  175. }
  176. #if defined(CONFIG_PCI) && defined(CONFIG_DM_PCI)
  177. int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
  178. const char *prop_name, struct fdt_pci_addr *addr)
  179. {
  180. const u32 *cell;
  181. int len;
  182. int ret = -ENOENT;
  183. debug("%s: %s: ", __func__, prop_name);
  184. /*
  185. * If we follow the pci bus bindings strictly, we should check
  186. * the value of the node's parent node's #address-cells and
  187. * #size-cells. They need to be 3 and 2 accordingly. However,
  188. * for simplicity we skip the check here.
  189. */
  190. cell = fdt_getprop(blob, node, prop_name, &len);
  191. if (!cell)
  192. goto fail;
  193. if ((len % FDT_PCI_REG_SIZE) == 0) {
  194. int num = len / FDT_PCI_REG_SIZE;
  195. int i;
  196. for (i = 0; i < num; i++) {
  197. debug("pci address #%d: %08lx %08lx %08lx\n", i,
  198. (ulong)fdt32_to_cpu(cell[0]),
  199. (ulong)fdt32_to_cpu(cell[1]),
  200. (ulong)fdt32_to_cpu(cell[2]));
  201. if ((fdt32_to_cpu(*cell) & type) == type) {
  202. addr->phys_hi = fdt32_to_cpu(cell[0]);
  203. addr->phys_mid = fdt32_to_cpu(cell[1]);
  204. addr->phys_lo = fdt32_to_cpu(cell[1]);
  205. break;
  206. }
  207. cell += (FDT_PCI_ADDR_CELLS +
  208. FDT_PCI_SIZE_CELLS);
  209. }
  210. if (i == num) {
  211. ret = -ENXIO;
  212. goto fail;
  213. }
  214. return 0;
  215. }
  216. ret = -EINVAL;
  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. len = strlen(list);
  231. if (len >= strlen("pciVVVV,DDDD")) {
  232. char *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 strcmp(cell, "okay") == 0;
  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 (fdt_node_check_compatible(blob, node,
  294. compat_names[id]) == 0)
  295. return id;
  296. return COMPAT_UNKNOWN;
  297. }
  298. int fdtdec_next_compatible(const void *blob, int node, enum fdt_compat_id id)
  299. {
  300. return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
  301. }
  302. int fdtdec_next_compatible_subnode(const void *blob, int node,
  303. enum fdt_compat_id id, int *depthp)
  304. {
  305. do {
  306. node = fdt_next_node(blob, node, depthp);
  307. } while (*depthp > 1);
  308. /* If this is a direct subnode, and compatible, return it */
  309. if (*depthp == 1 && 0 == fdt_node_check_compatible(
  310. blob, node, compat_names[id]))
  311. return node;
  312. return -FDT_ERR_NOTFOUND;
  313. }
  314. int fdtdec_next_alias(const void *blob, const char *name, enum fdt_compat_id id,
  315. int *upto)
  316. {
  317. #define MAX_STR_LEN 20
  318. char str[MAX_STR_LEN + 20];
  319. int node, err;
  320. /* snprintf() is not available */
  321. assert(strlen(name) < MAX_STR_LEN);
  322. sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
  323. node = fdt_path_offset(blob, str);
  324. if (node < 0)
  325. return node;
  326. err = fdt_node_check_compatible(blob, node, compat_names[id]);
  327. if (err < 0)
  328. return err;
  329. if (err)
  330. return -FDT_ERR_NOTFOUND;
  331. (*upto)++;
  332. return node;
  333. }
  334. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  335. enum fdt_compat_id id, int *node_list,
  336. 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,
  344. 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,
  555. int *err)
  556. {
  557. const void *cell;
  558. int len;
  559. debug("%s: %s\n", __func__, prop_name);
  560. cell = fdt_getprop(blob, node, prop_name, &len);
  561. if (!cell)
  562. *err = -FDT_ERR_NOTFOUND;
  563. else if (len < min_len)
  564. *err = -FDT_ERR_BADLAYOUT;
  565. else
  566. *err = 0;
  567. return cell;
  568. }
  569. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  570. u32 *array, int count)
  571. {
  572. const u32 *cell;
  573. int err = 0;
  574. debug("%s: %s\n", __func__, prop_name);
  575. cell = get_prop_check_min_len(blob, node, prop_name,
  576. sizeof(u32) * count, &err);
  577. if (!err) {
  578. int i;
  579. for (i = 0; i < count; i++)
  580. array[i] = fdt32_to_cpu(cell[i]);
  581. }
  582. return err;
  583. }
  584. int fdtdec_get_int_array_count(const void *blob, int node,
  585. const char *prop_name, u32 *array, int count)
  586. {
  587. const u32 *cell;
  588. int len, elems;
  589. int i;
  590. debug("%s: %s\n", __func__, prop_name);
  591. cell = fdt_getprop(blob, node, prop_name, &len);
  592. if (!cell)
  593. return -FDT_ERR_NOTFOUND;
  594. elems = len / sizeof(u32);
  595. if (count > elems)
  596. count = elems;
  597. for (i = 0; i < count; i++)
  598. array[i] = fdt32_to_cpu(cell[i]);
  599. return count;
  600. }
  601. const u32 *fdtdec_locate_array(const void *blob, int node,
  602. const char *prop_name, int count)
  603. {
  604. const u32 *cell;
  605. int err;
  606. cell = get_prop_check_min_len(blob, node, prop_name,
  607. sizeof(u32) * count, &err);
  608. return err ? NULL : cell;
  609. }
  610. int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
  611. {
  612. const s32 *cell;
  613. int len;
  614. debug("%s: %s\n", __func__, prop_name);
  615. cell = fdt_getprop(blob, node, prop_name, &len);
  616. return cell != NULL;
  617. }
  618. int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
  619. const char *list_name,
  620. const char *cells_name,
  621. int cell_count, int index,
  622. struct fdtdec_phandle_args *out_args)
  623. {
  624. const __be32 *list, *list_end;
  625. int rc = 0, size, cur_index = 0;
  626. uint32_t count = 0;
  627. int node = -1;
  628. int phandle;
  629. /* Retrieve the phandle list property */
  630. list = fdt_getprop(blob, src_node, list_name, &size);
  631. if (!list)
  632. return -ENOENT;
  633. list_end = list + size / sizeof(*list);
  634. /* Loop over the phandles until all the requested entry is found */
  635. while (list < list_end) {
  636. rc = -EINVAL;
  637. count = 0;
  638. /*
  639. * If phandle is 0, then it is an empty entry with no
  640. * arguments. Skip forward to the next entry.
  641. */
  642. phandle = be32_to_cpup(list++);
  643. if (phandle) {
  644. /*
  645. * Find the provider node and parse the #*-cells
  646. * property to determine the argument length.
  647. *
  648. * This is not needed if the cell count is hard-coded
  649. * (i.e. cells_name not set, but cell_count is set),
  650. * except when we're going to return the found node
  651. * below.
  652. */
  653. if (cells_name || cur_index == index) {
  654. node = fdt_node_offset_by_phandle(blob,
  655. phandle);
  656. if (!node) {
  657. debug("%s: could not find phandle\n",
  658. fdt_get_name(blob, src_node,
  659. NULL));
  660. goto err;
  661. }
  662. }
  663. if (cells_name) {
  664. count = fdtdec_get_int(blob, node, cells_name,
  665. -1);
  666. if (count == -1) {
  667. debug("%s: could not get %s for %s\n",
  668. fdt_get_name(blob, src_node,
  669. NULL),
  670. cells_name,
  671. fdt_get_name(blob, node,
  672. NULL));
  673. goto err;
  674. }
  675. } else {
  676. count = cell_count;
  677. }
  678. /*
  679. * Make sure that the arguments actually fit in the
  680. * remaining property data length
  681. */
  682. if (list + count > list_end) {
  683. debug("%s: arguments longer than property\n",
  684. fdt_get_name(blob, src_node, NULL));
  685. goto err;
  686. }
  687. }
  688. /*
  689. * All of the error cases above bail out of the loop, so at
  690. * this point, the parsing is successful. If the requested
  691. * index matches, then fill the out_args structure and return,
  692. * or return -ENOENT for an empty entry.
  693. */
  694. rc = -ENOENT;
  695. if (cur_index == index) {
  696. if (!phandle)
  697. goto err;
  698. if (out_args) {
  699. int i;
  700. if (count > MAX_PHANDLE_ARGS) {
  701. debug("%s: too many arguments %d\n",
  702. fdt_get_name(blob, src_node,
  703. NULL), count);
  704. count = MAX_PHANDLE_ARGS;
  705. }
  706. out_args->node = node;
  707. out_args->args_count = count;
  708. for (i = 0; i < count; i++) {
  709. out_args->args[i] =
  710. be32_to_cpup(list++);
  711. }
  712. }
  713. /* Found it! return success */
  714. return 0;
  715. }
  716. node = -1;
  717. list += count;
  718. cur_index++;
  719. }
  720. /*
  721. * Result will be one of:
  722. * -ENOENT : index is for empty phandle
  723. * -EINVAL : parsing error on data
  724. * [1..n] : Number of phandle (count mode; when index = -1)
  725. */
  726. rc = index < 0 ? cur_index : -ENOENT;
  727. err:
  728. return rc;
  729. }
  730. int fdtdec_get_child_count(const void *blob, int node)
  731. {
  732. int subnode;
  733. int num = 0;
  734. fdt_for_each_subnode(subnode, blob, node)
  735. num++;
  736. return num;
  737. }
  738. int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
  739. u8 *array, int count)
  740. {
  741. const u8 *cell;
  742. int err;
  743. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  744. if (!err)
  745. memcpy(array, cell, count);
  746. return err;
  747. }
  748. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  749. const char *prop_name, int count)
  750. {
  751. const u8 *cell;
  752. int err;
  753. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  754. if (err)
  755. return NULL;
  756. return cell;
  757. }
  758. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  759. int default_val)
  760. {
  761. int config_node;
  762. debug("%s: %s\n", __func__, prop_name);
  763. config_node = fdt_path_offset(blob, "/config");
  764. if (config_node < 0)
  765. return default_val;
  766. return fdtdec_get_int(blob, config_node, prop_name, default_val);
  767. }
  768. int fdtdec_get_config_bool(const void *blob, const char *prop_name)
  769. {
  770. int config_node;
  771. const void *prop;
  772. debug("%s: %s\n", __func__, prop_name);
  773. config_node = fdt_path_offset(blob, "/config");
  774. if (config_node < 0)
  775. return 0;
  776. prop = fdt_get_property(blob, config_node, prop_name, NULL);
  777. return prop != NULL;
  778. }
  779. char *fdtdec_get_config_string(const void *blob, const char *prop_name)
  780. {
  781. const char *nodep;
  782. int nodeoffset;
  783. int len;
  784. debug("%s: %s\n", __func__, prop_name);
  785. nodeoffset = fdt_path_offset(blob, "/config");
  786. if (nodeoffset < 0)
  787. return NULL;
  788. nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
  789. if (!nodep)
  790. return NULL;
  791. return (char *)nodep;
  792. }
  793. u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
  794. {
  795. u64 number = 0;
  796. while (cells--)
  797. number = (number << 32) | fdt32_to_cpu(*ptr++);
  798. return number;
  799. }
  800. int fdt_get_resource(const void *fdt, int node, const char *property,
  801. unsigned int index, struct fdt_resource *res)
  802. {
  803. const fdt32_t *ptr, *end;
  804. int na, ns, len, parent;
  805. unsigned int i = 0;
  806. parent = fdt_parent_offset(fdt, node);
  807. if (parent < 0)
  808. return parent;
  809. na = fdt_address_cells(fdt, parent);
  810. ns = fdt_size_cells(fdt, parent);
  811. ptr = fdt_getprop(fdt, node, property, &len);
  812. if (!ptr)
  813. return len;
  814. end = ptr + len / sizeof(*ptr);
  815. while (ptr + na + ns <= end) {
  816. if (i == index) {
  817. res->start = fdtdec_get_number(ptr, na);
  818. res->end = res->start;
  819. res->end += fdtdec_get_number(&ptr[na], ns) - 1;
  820. return 0;
  821. }
  822. ptr += na + ns;
  823. i++;
  824. }
  825. return -FDT_ERR_NOTFOUND;
  826. }
  827. int fdt_get_named_resource(const void *fdt, int node, const char *property,
  828. const char *prop_names, const char *name,
  829. struct fdt_resource *res)
  830. {
  831. int index;
  832. index = fdt_stringlist_search(fdt, node, prop_names, name);
  833. if (index < 0)
  834. return index;
  835. return fdt_get_resource(fdt, node, property, index, res);
  836. }
  837. static int decode_timing_property(const void *blob, int node, const char *name,
  838. struct timing_entry *result)
  839. {
  840. int length, ret = 0;
  841. const u32 *prop;
  842. prop = fdt_getprop(blob, node, name, &length);
  843. if (!prop) {
  844. debug("%s: could not find property %s\n",
  845. fdt_get_name(blob, node, NULL), name);
  846. return length;
  847. }
  848. if (length == sizeof(u32)) {
  849. result->typ = fdtdec_get_int(blob, node, name, 0);
  850. result->min = result->typ;
  851. result->max = result->typ;
  852. } else {
  853. ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
  854. }
  855. return ret;
  856. }
  857. int fdtdec_decode_display_timing(const void *blob, int parent, int index,
  858. struct display_timing *dt)
  859. {
  860. int i, node, timings_node;
  861. u32 val = 0;
  862. int ret = 0;
  863. timings_node = fdt_subnode_offset(blob, parent, "display-timings");
  864. if (timings_node < 0)
  865. return timings_node;
  866. for (i = 0, node = fdt_first_subnode(blob, timings_node);
  867. node > 0 && i != index;
  868. node = fdt_next_subnode(blob, node))
  869. i++;
  870. if (node < 0)
  871. return node;
  872. memset(dt, 0, sizeof(*dt));
  873. ret |= decode_timing_property(blob, node, "hback-porch",
  874. &dt->hback_porch);
  875. ret |= decode_timing_property(blob, node, "hfront-porch",
  876. &dt->hfront_porch);
  877. ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
  878. ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
  879. ret |= decode_timing_property(blob, node, "vback-porch",
  880. &dt->vback_porch);
  881. ret |= decode_timing_property(blob, node, "vfront-porch",
  882. &dt->vfront_porch);
  883. ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
  884. ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
  885. ret |= decode_timing_property(blob, node, "clock-frequency",
  886. &dt->pixelclock);
  887. dt->flags = 0;
  888. val = fdtdec_get_int(blob, node, "vsync-active", -1);
  889. if (val != -1) {
  890. dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
  891. DISPLAY_FLAGS_VSYNC_LOW;
  892. }
  893. val = fdtdec_get_int(blob, node, "hsync-active", -1);
  894. if (val != -1) {
  895. dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
  896. DISPLAY_FLAGS_HSYNC_LOW;
  897. }
  898. val = fdtdec_get_int(blob, node, "de-active", -1);
  899. if (val != -1) {
  900. dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
  901. DISPLAY_FLAGS_DE_LOW;
  902. }
  903. val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
  904. if (val != -1) {
  905. dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
  906. DISPLAY_FLAGS_PIXDATA_NEGEDGE;
  907. }
  908. if (fdtdec_get_bool(blob, node, "interlaced"))
  909. dt->flags |= DISPLAY_FLAGS_INTERLACED;
  910. if (fdtdec_get_bool(blob, node, "doublescan"))
  911. dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
  912. if (fdtdec_get_bool(blob, node, "doubleclk"))
  913. dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
  914. return ret;
  915. }
  916. int fdtdec_setup_mem_size_base(void)
  917. {
  918. int ret, mem;
  919. struct fdt_resource res;
  920. mem = fdt_path_offset(gd->fdt_blob, "/memory");
  921. if (mem < 0) {
  922. debug("%s: Missing /memory node\n", __func__);
  923. return -EINVAL;
  924. }
  925. ret = fdt_get_resource(gd->fdt_blob, mem, "reg", 0, &res);
  926. if (ret != 0) {
  927. debug("%s: Unable to decode first memory bank\n", __func__);
  928. return -EINVAL;
  929. }
  930. gd->ram_size = (phys_size_t)(res.end - res.start + 1);
  931. gd->ram_base = (unsigned long)res.start;
  932. debug("%s: Initial DRAM size %llx\n", __func__,
  933. (unsigned long long)gd->ram_size);
  934. return 0;
  935. }
  936. #if defined(CONFIG_NR_DRAM_BANKS)
  937. static ofnode get_next_memory_node(ofnode mem)
  938. {
  939. do {
  940. mem = ofnode_by_prop_value(mem, "device_type", "memory", 7);
  941. } while (ofnode_valid(mem) && !ofnode_is_available(mem));
  942. return mem;
  943. }
  944. int fdtdec_setup_memory_banksize(void)
  945. {
  946. int bank, reg = 0;
  947. struct resource res;
  948. ofnode mem;
  949. mem = get_next_memory_node(ofnode_null());
  950. if (!ofnode_valid(mem))
  951. goto missing_node;
  952. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  953. while (ofnode_read_resource(mem, reg++, &res)) {
  954. reg = 0;
  955. mem = get_next_memory_node(mem);
  956. if (!ofnode_valid(mem)) {
  957. if (bank)
  958. return 0;
  959. goto missing_node;
  960. }
  961. }
  962. gd->bd->bi_dram[bank].start = (phys_addr_t)res.start;
  963. gd->bd->bi_dram[bank].size =
  964. (phys_size_t)(res.end - res.start + 1);
  965. debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n",
  966. __func__, bank,
  967. (unsigned long long)gd->bd->bi_dram[bank].start,
  968. (unsigned long long)gd->bd->bi_dram[bank].size);
  969. }
  970. return 0;
  971. missing_node:
  972. debug("%s: Missing /memory node\n", __func__);
  973. return -EINVAL;
  974. }
  975. #endif
  976. #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
  977. # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_GZIP) ||\
  978. CONFIG_IS_ENABLED(MULTI_DTB_FIT_LZO)
  979. static int uncompress_blob(const void *src, ulong sz_src, void **dstp)
  980. {
  981. size_t sz_out = CONFIG_SPL_MULTI_DTB_FIT_UNCOMPRESS_SZ;
  982. ulong sz_in = sz_src;
  983. void *dst;
  984. int rc;
  985. if (CONFIG_IS_ENABLED(GZIP))
  986. if (gzip_parse_header(src, sz_in) < 0)
  987. return -1;
  988. if (CONFIG_IS_ENABLED(LZO))
  989. if (!lzop_is_valid_header(src))
  990. return -EBADMSG;
  991. if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC)) {
  992. dst = malloc(sz_out);
  993. if (!dst) {
  994. puts("uncompress_blob: Unable to allocate memory\n");
  995. return -ENOMEM;
  996. }
  997. } else {
  998. # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_USER_DEFINED_AREA)
  999. dst = (void *)CONFIG_VAL(MULTI_DTB_FIT_USER_DEF_ADDR);
  1000. # else
  1001. return -ENOTSUPP;
  1002. # endif
  1003. }
  1004. if (CONFIG_IS_ENABLED(GZIP))
  1005. rc = gunzip(dst, sz_out, (u8 *)src, &sz_in);
  1006. else if (CONFIG_IS_ENABLED(LZO))
  1007. rc = lzop_decompress(src, sz_in, dst, &sz_out);
  1008. if (rc < 0) {
  1009. /* not a valid compressed blob */
  1010. puts("uncompress_blob: Unable to uncompress\n");
  1011. if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC))
  1012. free(dst);
  1013. return -EBADMSG;
  1014. }
  1015. *dstp = dst;
  1016. return 0;
  1017. }
  1018. # else
  1019. static int uncompress_blob(const void *src, ulong sz_src, void **dstp)
  1020. {
  1021. return -ENOTSUPP;
  1022. }
  1023. # endif
  1024. #endif
  1025. #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
  1026. /*
  1027. * For CONFIG_OF_SEPARATE, the board may optionally implement this to
  1028. * provide and/or fixup the fdt.
  1029. */
  1030. __weak void *board_fdt_blob_setup(void)
  1031. {
  1032. void *fdt_blob = NULL;
  1033. #ifdef CONFIG_SPL_BUILD
  1034. /* FDT is at end of BSS unless it is in a different memory region */
  1035. if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
  1036. fdt_blob = (ulong *)&_image_binary_end;
  1037. else
  1038. fdt_blob = (ulong *)&__bss_end;
  1039. #else
  1040. /* FDT is at end of image */
  1041. fdt_blob = (ulong *)&_end;
  1042. #endif
  1043. return fdt_blob;
  1044. }
  1045. #endif
  1046. int fdtdec_setup(void)
  1047. {
  1048. #if CONFIG_IS_ENABLED(OF_CONTROL)
  1049. # if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
  1050. void *fdt_blob;
  1051. # endif
  1052. # ifdef CONFIG_OF_EMBED
  1053. /* Get a pointer to the FDT */
  1054. # ifdef CONFIG_SPL_BUILD
  1055. gd->fdt_blob = __dtb_dt_spl_begin;
  1056. # else
  1057. gd->fdt_blob = __dtb_dt_begin;
  1058. # endif
  1059. # elif defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
  1060. /* Allow the board to override the fdt address. */
  1061. gd->fdt_blob = board_fdt_blob_setup();
  1062. # elif defined(CONFIG_OF_HOSTFILE)
  1063. if (sandbox_read_fdt_from_file()) {
  1064. puts("Failed to read control FDT\n");
  1065. return -1;
  1066. }
  1067. # endif
  1068. # ifndef CONFIG_SPL_BUILD
  1069. /* Allow the early environment to override the fdt address */
  1070. # if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
  1071. gd->fdt_blob = (void *)prior_stage_fdt_address;
  1072. # else
  1073. gd->fdt_blob = (void *)env_get_ulong("fdtcontroladdr", 16,
  1074. (uintptr_t)gd->fdt_blob);
  1075. # endif
  1076. # endif
  1077. # if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
  1078. /*
  1079. * Try and uncompress the blob.
  1080. * Unfortunately there is no way to know how big the input blob really
  1081. * is. So let us set the maximum input size arbitrarily high. 16MB
  1082. * ought to be more than enough for packed DTBs.
  1083. */
  1084. if (uncompress_blob(gd->fdt_blob, 0x1000000, &fdt_blob) == 0)
  1085. gd->fdt_blob = fdt_blob;
  1086. /*
  1087. * Check if blob is a FIT images containings DTBs.
  1088. * If so, pick the most relevant
  1089. */
  1090. fdt_blob = locate_dtb_in_fit(gd->fdt_blob);
  1091. if (fdt_blob)
  1092. gd->fdt_blob = fdt_blob;
  1093. # endif
  1094. #endif
  1095. return fdtdec_prepare_fdt();
  1096. }
  1097. #ifdef CONFIG_NR_DRAM_BANKS
  1098. int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
  1099. phys_addr_t *basep, phys_size_t *sizep, bd_t *bd)
  1100. {
  1101. int addr_cells, size_cells;
  1102. const u32 *cell, *end;
  1103. u64 total_size, size, addr;
  1104. int node, child;
  1105. bool auto_size;
  1106. int bank;
  1107. int len;
  1108. debug("%s: board_id=%d\n", __func__, board_id);
  1109. if (!area)
  1110. area = "/memory";
  1111. node = fdt_path_offset(blob, area);
  1112. if (node < 0) {
  1113. debug("No %s node found\n", area);
  1114. return -ENOENT;
  1115. }
  1116. cell = fdt_getprop(blob, node, "reg", &len);
  1117. if (!cell) {
  1118. debug("No reg property found\n");
  1119. return -ENOENT;
  1120. }
  1121. addr_cells = fdt_address_cells(blob, node);
  1122. size_cells = fdt_size_cells(blob, node);
  1123. /* Check the board id and mask */
  1124. for (child = fdt_first_subnode(blob, node);
  1125. child >= 0;
  1126. child = fdt_next_subnode(blob, child)) {
  1127. int match_mask, match_value;
  1128. match_mask = fdtdec_get_int(blob, child, "match-mask", -1);
  1129. match_value = fdtdec_get_int(blob, child, "match-value", -1);
  1130. if (match_value >= 0 &&
  1131. ((board_id & match_mask) == match_value)) {
  1132. /* Found matching mask */
  1133. debug("Found matching mask %d\n", match_mask);
  1134. node = child;
  1135. cell = fdt_getprop(blob, node, "reg", &len);
  1136. if (!cell) {
  1137. debug("No memory-banks property found\n");
  1138. return -EINVAL;
  1139. }
  1140. break;
  1141. }
  1142. }
  1143. /* Note: if no matching subnode was found we use the parent node */
  1144. if (bd) {
  1145. memset(bd->bi_dram, '\0', sizeof(bd->bi_dram[0]) *
  1146. CONFIG_NR_DRAM_BANKS);
  1147. }
  1148. auto_size = fdtdec_get_bool(blob, node, "auto-size");
  1149. total_size = 0;
  1150. end = cell + len / 4 - addr_cells - size_cells;
  1151. debug("cell at %p, end %p\n", cell, end);
  1152. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  1153. if (cell > end)
  1154. break;
  1155. addr = 0;
  1156. if (addr_cells == 2)
  1157. addr += (u64)fdt32_to_cpu(*cell++) << 32UL;
  1158. addr += fdt32_to_cpu(*cell++);
  1159. if (bd)
  1160. bd->bi_dram[bank].start = addr;
  1161. if (basep && !bank)
  1162. *basep = (phys_addr_t)addr;
  1163. size = 0;
  1164. if (size_cells == 2)
  1165. size += (u64)fdt32_to_cpu(*cell++) << 32UL;
  1166. size += fdt32_to_cpu(*cell++);
  1167. if (auto_size) {
  1168. u64 new_size;
  1169. debug("Auto-sizing %llx, size %llx: ", addr, size);
  1170. new_size = get_ram_size((long *)(uintptr_t)addr, size);
  1171. if (new_size == size) {
  1172. debug("OK\n");
  1173. } else {
  1174. debug("sized to %llx\n", new_size);
  1175. size = new_size;
  1176. }
  1177. }
  1178. if (bd)
  1179. bd->bi_dram[bank].size = size;
  1180. total_size += size;
  1181. }
  1182. debug("Memory size %llu\n", total_size);
  1183. if (sizep)
  1184. *sizep = (phys_size_t)total_size;
  1185. return 0;
  1186. }
  1187. #endif /* CONFIG_NR_DRAM_BANKS */
  1188. #endif /* !USE_HOSTCC */