fdtdec.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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/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(GENERIC_SPI_FLASH, "spi-flash"),
  49. COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
  50. COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
  51. COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
  52. COMPAT(INTEL_MICROCODE, "intel,microcode"),
  53. COMPAT(AMS_AS3722, "ams,as3722"),
  54. COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
  55. COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
  56. COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"),
  57. COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"),
  58. COMPAT(INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
  59. COMPAT(INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
  60. COMPAT(INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"),
  61. COMPAT(COMPAT_SUNXI_NAND, "allwinner,sun4i-a10-nand"),
  62. COMPAT(ALTERA_SOCFPGA_CLK, "altr,clk-mgr"),
  63. COMPAT(ALTERA_SOCFPGA_PINCTRL_SINGLE, "pinctrl-single"),
  64. COMPAT(ALTERA_SOCFPGA_H2F_BRG, "altr,socfpga-hps2fpga-bridge"),
  65. COMPAT(ALTERA_SOCFPGA_LWH2F_BRG, "altr,socfpga-lwhps2fpga-bridge"),
  66. COMPAT(ALTERA_SOCFPGA_F2H_BRG, "altr,socfpga-fpga2hps-bridge"),
  67. COMPAT(ALTERA_SOCFPGA_F2SDR0, "altr,socfpga-fpga2sdram0-bridge"),
  68. COMPAT(ALTERA_SOCFPGA_F2SDR1, "altr,socfpga-fpga2sdram1-bridge"),
  69. COMPAT(ALTERA_SOCFPGA_F2SDR2, "altr,socfpga-fpga2sdram2-bridge"),
  70. COMPAT(ALTERA_SOCFPGA_FPGA0, "altr,socfpga-a10-fpga-mgr"),
  71. COMPAT(ALTERA_SOCFPGA_NOC, "altr,socfpga-a10-noc"),
  72. COMPAT(ALTERA_SOCFPGA_CLK_INIT, "altr,socfpga-a10-clk-init")
  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,
  82. int ns, fdt_size_t *sizep,
  83. bool translate)
  84. {
  85. const fdt32_t *prop, *prop_end;
  86. const fdt32_t *prop_addr, *prop_size, *prop_after_size;
  87. int len;
  88. fdt_addr_t addr;
  89. debug("%s: %s: ", __func__, prop_name);
  90. if (na > (sizeof(fdt_addr_t) / sizeof(fdt32_t))) {
  91. debug("(na too large for fdt_addr_t type)\n");
  92. return FDT_ADDR_T_NONE;
  93. }
  94. if (ns > (sizeof(fdt_size_t) / sizeof(fdt32_t))) {
  95. debug("(ns too large for fdt_size_t type)\n");
  96. return FDT_ADDR_T_NONE;
  97. }
  98. prop = fdt_getprop(blob, node, prop_name, &len);
  99. if (!prop) {
  100. debug("(not found)\n");
  101. return FDT_ADDR_T_NONE;
  102. }
  103. prop_end = prop + (len / sizeof(*prop));
  104. prop_addr = prop + (index * (na + ns));
  105. prop_size = prop_addr + na;
  106. prop_after_size = prop_size + ns;
  107. if (prop_after_size > prop_end) {
  108. debug("(not enough data: expected >= %d cells, got %d cells)\n",
  109. (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
  110. return FDT_ADDR_T_NONE;
  111. }
  112. #if CONFIG_IS_ENABLED(OF_TRANSLATE)
  113. if (translate)
  114. addr = fdt_translate_address(blob, node, prop_addr);
  115. else
  116. #endif
  117. addr = fdtdec_get_number(prop_addr, na);
  118. if (sizep) {
  119. *sizep = fdtdec_get_number(prop_size, ns);
  120. debug("addr=%08llx, size=%llx\n", (unsigned long long)addr,
  121. (unsigned long long)*sizep);
  122. } else {
  123. debug("addr=%08llx\n", (unsigned long long)addr);
  124. }
  125. return addr;
  126. }
  127. fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
  128. int node, const char *prop_name,
  129. int index, fdt_size_t *sizep,
  130. bool translate)
  131. {
  132. int na, ns;
  133. debug("%s: ", __func__);
  134. na = fdt_address_cells(blob, parent);
  135. if (na < 1) {
  136. debug("(bad #address-cells)\n");
  137. return FDT_ADDR_T_NONE;
  138. }
  139. ns = fdt_size_cells(blob, parent);
  140. if (ns < 0) {
  141. debug("(bad #size-cells)\n");
  142. return FDT_ADDR_T_NONE;
  143. }
  144. debug("na=%d, ns=%d, ", na, ns);
  145. return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na,
  146. ns, sizep, translate);
  147. }
  148. fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
  149. const char *prop_name, int index,
  150. fdt_size_t *sizep,
  151. bool translate)
  152. {
  153. int parent;
  154. debug("%s: ", __func__);
  155. parent = fdt_parent_offset(blob, node);
  156. if (parent < 0) {
  157. debug("(no parent found)\n");
  158. return FDT_ADDR_T_NONE;
  159. }
  160. return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name,
  161. index, sizep, translate);
  162. }
  163. fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
  164. const char *prop_name, fdt_size_t *sizep)
  165. {
  166. int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0;
  167. return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0,
  168. sizeof(fdt_addr_t) / sizeof(fdt32_t),
  169. ns, sizep, false);
  170. }
  171. fdt_addr_t fdtdec_get_addr(const void *blob, int node, const char *prop_name)
  172. {
  173. return fdtdec_get_addr_size(blob, node, prop_name, NULL);
  174. }
  175. #if defined(CONFIG_PCI) && defined(CONFIG_DM_PCI)
  176. int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
  177. const char *prop_name, struct fdt_pci_addr *addr)
  178. {
  179. const u32 *cell;
  180. int len;
  181. int ret = -ENOENT;
  182. debug("%s: %s: ", __func__, prop_name);
  183. /*
  184. * If we follow the pci bus bindings strictly, we should check
  185. * the value of the node's parent node's #address-cells and
  186. * #size-cells. They need to be 3 and 2 accordingly. However,
  187. * for simplicity we skip the check here.
  188. */
  189. cell = fdt_getprop(blob, node, prop_name, &len);
  190. if (!cell)
  191. goto fail;
  192. if ((len % FDT_PCI_REG_SIZE) == 0) {
  193. int num = len / FDT_PCI_REG_SIZE;
  194. int i;
  195. for (i = 0; i < num; i++) {
  196. debug("pci address #%d: %08lx %08lx %08lx\n", i,
  197. (ulong)fdt32_to_cpu(cell[0]),
  198. (ulong)fdt32_to_cpu(cell[1]),
  199. (ulong)fdt32_to_cpu(cell[2]));
  200. if ((fdt32_to_cpu(*cell) & type) == type) {
  201. addr->phys_hi = fdt32_to_cpu(cell[0]);
  202. addr->phys_mid = fdt32_to_cpu(cell[1]);
  203. addr->phys_lo = fdt32_to_cpu(cell[1]);
  204. break;
  205. }
  206. cell += (FDT_PCI_ADDR_CELLS +
  207. FDT_PCI_SIZE_CELLS);
  208. }
  209. if (i == num) {
  210. ret = -ENXIO;
  211. goto fail;
  212. }
  213. return 0;
  214. }
  215. ret = -EINVAL;
  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. len = strlen(list);
  230. if (len >= strlen("pciVVVV,DDDD")) {
  231. char *s = strstr(list, "pci");
  232. /*
  233. * check if the string is something like pciVVVV,DDDD.RR
  234. * or just pciVVVV,DDDD
  235. */
  236. if (s && s[7] == ',' &&
  237. (s[12] == '.' || s[12] == 0)) {
  238. s += 3;
  239. *vendor = simple_strtol(s, NULL, 16);
  240. s += 5;
  241. *device = simple_strtol(s, NULL, 16);
  242. return 0;
  243. }
  244. }
  245. list += (len + 1);
  246. }
  247. return -ENOENT;
  248. }
  249. int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
  250. u32 *bar)
  251. {
  252. int barnum;
  253. /* extract the bar number from fdt_pci_addr */
  254. barnum = addr->phys_hi & 0xff;
  255. if (barnum < PCI_BASE_ADDRESS_0 || barnum > PCI_CARDBUS_CIS)
  256. return -EINVAL;
  257. barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
  258. *bar = dm_pci_read_bar32(dev, barnum);
  259. return 0;
  260. }
  261. #endif
  262. uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
  263. uint64_t default_val)
  264. {
  265. const uint64_t *cell64;
  266. int length;
  267. cell64 = fdt_getprop(blob, node, prop_name, &length);
  268. if (!cell64 || length < sizeof(*cell64))
  269. return default_val;
  270. return fdt64_to_cpu(*cell64);
  271. }
  272. int fdtdec_get_is_enabled(const void *blob, int node)
  273. {
  274. const char *cell;
  275. /*
  276. * It should say "okay", so only allow that. Some fdts use "ok" but
  277. * this is a bug. Please fix your device tree source file. See here
  278. * for discussion:
  279. *
  280. * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
  281. */
  282. cell = fdt_getprop(blob, node, "status", NULL);
  283. if (cell)
  284. return strcmp(cell, "okay") == 0;
  285. return 1;
  286. }
  287. enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
  288. {
  289. enum fdt_compat_id id;
  290. /* Search our drivers */
  291. for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
  292. if (fdt_node_check_compatible(blob, node,
  293. compat_names[id]) == 0)
  294. return id;
  295. return COMPAT_UNKNOWN;
  296. }
  297. int fdtdec_next_compatible(const void *blob, int node, 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, enum fdt_compat_id id,
  314. 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,
  335. int maxcount)
  336. {
  337. memset(node_list, '\0', sizeof(*node_list) * maxcount);
  338. return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
  339. }
  340. /* TODO: Can we tighten this code up a little? */
  341. int fdtdec_add_aliases_for_id(const void *blob, const char *name,
  342. enum fdt_compat_id id, int *node_list,
  343. 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,
  554. int *err)
  555. {
  556. const void *cell;
  557. int len;
  558. debug("%s: %s\n", __func__, prop_name);
  559. cell = fdt_getprop(blob, node, prop_name, &len);
  560. if (!cell)
  561. *err = -FDT_ERR_NOTFOUND;
  562. else if (len < min_len)
  563. *err = -FDT_ERR_BADLAYOUT;
  564. else
  565. *err = 0;
  566. return cell;
  567. }
  568. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  569. u32 *array, int count)
  570. {
  571. const u32 *cell;
  572. int err = 0;
  573. debug("%s: %s\n", __func__, prop_name);
  574. cell = get_prop_check_min_len(blob, node, prop_name,
  575. sizeof(u32) * count, &err);
  576. if (!err) {
  577. int i;
  578. for (i = 0; i < count; i++)
  579. array[i] = fdt32_to_cpu(cell[i]);
  580. }
  581. return err;
  582. }
  583. int fdtdec_get_int_array_count(const void *blob, int node,
  584. const char *prop_name, u32 *array, int count)
  585. {
  586. const u32 *cell;
  587. int len, elems;
  588. int i;
  589. debug("%s: %s\n", __func__, prop_name);
  590. cell = fdt_getprop(blob, node, prop_name, &len);
  591. if (!cell)
  592. return -FDT_ERR_NOTFOUND;
  593. elems = len / sizeof(u32);
  594. if (count > elems)
  595. count = elems;
  596. for (i = 0; i < count; i++)
  597. array[i] = fdt32_to_cpu(cell[i]);
  598. return count;
  599. }
  600. const u32 *fdtdec_locate_array(const void *blob, int node,
  601. const char *prop_name, int count)
  602. {
  603. const u32 *cell;
  604. int err;
  605. cell = get_prop_check_min_len(blob, node, prop_name,
  606. sizeof(u32) * count, &err);
  607. return err ? NULL : cell;
  608. }
  609. int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
  610. {
  611. const s32 *cell;
  612. int len;
  613. debug("%s: %s\n", __func__, prop_name);
  614. cell = fdt_getprop(blob, node, prop_name, &len);
  615. return cell != NULL;
  616. }
  617. int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
  618. const char *list_name,
  619. const char *cells_name,
  620. int cell_count, int index,
  621. struct fdtdec_phandle_args *out_args)
  622. {
  623. const __be32 *list, *list_end;
  624. int rc = 0, size, cur_index = 0;
  625. uint32_t count = 0;
  626. int node = -1;
  627. int phandle;
  628. /* Retrieve the phandle list property */
  629. list = fdt_getprop(blob, src_node, list_name, &size);
  630. if (!list)
  631. return -ENOENT;
  632. list_end = list + size / sizeof(*list);
  633. /* Loop over the phandles until all the requested entry is found */
  634. while (list < list_end) {
  635. rc = -EINVAL;
  636. count = 0;
  637. /*
  638. * If phandle is 0, then it is an empty entry with no
  639. * arguments. Skip forward to the next entry.
  640. */
  641. phandle = be32_to_cpup(list++);
  642. if (phandle) {
  643. /*
  644. * Find the provider node and parse the #*-cells
  645. * property to determine the argument length.
  646. *
  647. * This is not needed if the cell count is hard-coded
  648. * (i.e. cells_name not set, but cell_count is set),
  649. * except when we're going to return the found node
  650. * below.
  651. */
  652. if (cells_name || cur_index == index) {
  653. node = fdt_node_offset_by_phandle(blob,
  654. phandle);
  655. if (!node) {
  656. debug("%s: could not find phandle\n",
  657. fdt_get_name(blob, src_node,
  658. NULL));
  659. goto err;
  660. }
  661. }
  662. if (cells_name) {
  663. count = fdtdec_get_int(blob, node, cells_name,
  664. -1);
  665. if (count == -1) {
  666. debug("%s: could not get %s for %s\n",
  667. fdt_get_name(blob, src_node,
  668. NULL),
  669. cells_name,
  670. fdt_get_name(blob, node,
  671. NULL));
  672. goto err;
  673. }
  674. } else {
  675. count = cell_count;
  676. }
  677. /*
  678. * Make sure that the arguments actually fit in the
  679. * remaining property data length
  680. */
  681. if (list + count > list_end) {
  682. debug("%s: arguments longer than property\n",
  683. fdt_get_name(blob, src_node, NULL));
  684. goto err;
  685. }
  686. }
  687. /*
  688. * All of the error cases above bail out of the loop, so at
  689. * this point, the parsing is successful. If the requested
  690. * index matches, then fill the out_args structure and return,
  691. * or return -ENOENT for an empty entry.
  692. */
  693. rc = -ENOENT;
  694. if (cur_index == index) {
  695. if (!phandle)
  696. goto err;
  697. if (out_args) {
  698. int i;
  699. if (count > MAX_PHANDLE_ARGS) {
  700. debug("%s: too many arguments %d\n",
  701. fdt_get_name(blob, src_node,
  702. NULL), count);
  703. count = MAX_PHANDLE_ARGS;
  704. }
  705. out_args->node = node;
  706. out_args->args_count = count;
  707. for (i = 0; i < count; i++) {
  708. out_args->args[i] =
  709. be32_to_cpup(list++);
  710. }
  711. }
  712. /* Found it! return success */
  713. return 0;
  714. }
  715. node = -1;
  716. list += count;
  717. cur_index++;
  718. }
  719. /*
  720. * Result will be one of:
  721. * -ENOENT : index is for empty phandle
  722. * -EINVAL : parsing error on data
  723. * [1..n] : Number of phandle (count mode; when index = -1)
  724. */
  725. rc = index < 0 ? cur_index : -ENOENT;
  726. err:
  727. return rc;
  728. }
  729. int fdtdec_get_child_count(const void *blob, int node)
  730. {
  731. int subnode;
  732. int num = 0;
  733. fdt_for_each_subnode(subnode, blob, node)
  734. num++;
  735. return num;
  736. }
  737. int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
  738. u8 *array, int count)
  739. {
  740. const u8 *cell;
  741. int err;
  742. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  743. if (!err)
  744. memcpy(array, cell, count);
  745. return err;
  746. }
  747. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  748. const char *prop_name, int count)
  749. {
  750. const u8 *cell;
  751. int err;
  752. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  753. if (err)
  754. return NULL;
  755. return cell;
  756. }
  757. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  758. int default_val)
  759. {
  760. int config_node;
  761. debug("%s: %s\n", __func__, prop_name);
  762. config_node = fdt_path_offset(blob, "/config");
  763. if (config_node < 0)
  764. return default_val;
  765. return fdtdec_get_int(blob, config_node, prop_name, default_val);
  766. }
  767. int fdtdec_get_config_bool(const void *blob, const char *prop_name)
  768. {
  769. int config_node;
  770. const void *prop;
  771. debug("%s: %s\n", __func__, prop_name);
  772. config_node = fdt_path_offset(blob, "/config");
  773. if (config_node < 0)
  774. return 0;
  775. prop = fdt_get_property(blob, config_node, prop_name, NULL);
  776. return prop != NULL;
  777. }
  778. char *fdtdec_get_config_string(const void *blob, const char *prop_name)
  779. {
  780. const char *nodep;
  781. int nodeoffset;
  782. int len;
  783. debug("%s: %s\n", __func__, prop_name);
  784. nodeoffset = fdt_path_offset(blob, "/config");
  785. if (nodeoffset < 0)
  786. return NULL;
  787. nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
  788. if (!nodep)
  789. return NULL;
  790. return (char *)nodep;
  791. }
  792. u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
  793. {
  794. u64 number = 0;
  795. while (cells--)
  796. number = (number << 32) | fdt32_to_cpu(*ptr++);
  797. return number;
  798. }
  799. int fdt_get_resource(const void *fdt, int node, const char *property,
  800. unsigned int index, struct fdt_resource *res)
  801. {
  802. const fdt32_t *ptr, *end;
  803. int na, ns, len, parent;
  804. unsigned int i = 0;
  805. parent = fdt_parent_offset(fdt, node);
  806. if (parent < 0)
  807. return parent;
  808. na = fdt_address_cells(fdt, parent);
  809. ns = fdt_size_cells(fdt, parent);
  810. ptr = fdt_getprop(fdt, node, property, &len);
  811. if (!ptr)
  812. return len;
  813. end = ptr + len / sizeof(*ptr);
  814. while (ptr + na + ns <= end) {
  815. if (i == index) {
  816. res->start = fdtdec_get_number(ptr, na);
  817. res->end = res->start;
  818. res->end += fdtdec_get_number(&ptr[na], ns) - 1;
  819. return 0;
  820. }
  821. ptr += na + ns;
  822. i++;
  823. }
  824. return -FDT_ERR_NOTFOUND;
  825. }
  826. int fdt_get_named_resource(const void *fdt, int node, const char *property,
  827. const char *prop_names, const char *name,
  828. struct fdt_resource *res)
  829. {
  830. int index;
  831. index = fdt_stringlist_search(fdt, node, prop_names, name);
  832. if (index < 0)
  833. return index;
  834. return fdt_get_resource(fdt, node, property, index, res);
  835. }
  836. static int decode_timing_property(const void *blob, int node, const char *name,
  837. struct timing_entry *result)
  838. {
  839. int length, ret = 0;
  840. const u32 *prop;
  841. prop = fdt_getprop(blob, node, name, &length);
  842. if (!prop) {
  843. debug("%s: could not find property %s\n",
  844. fdt_get_name(blob, node, NULL), name);
  845. return length;
  846. }
  847. if (length == sizeof(u32)) {
  848. result->typ = fdtdec_get_int(blob, node, name, 0);
  849. result->min = result->typ;
  850. result->max = result->typ;
  851. } else {
  852. ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
  853. }
  854. return ret;
  855. }
  856. int fdtdec_decode_display_timing(const void *blob, int parent, int index,
  857. struct display_timing *dt)
  858. {
  859. int i, node, timings_node;
  860. u32 val = 0;
  861. int ret = 0;
  862. timings_node = fdt_subnode_offset(blob, parent, "display-timings");
  863. if (timings_node < 0)
  864. return timings_node;
  865. for (i = 0, node = fdt_first_subnode(blob, timings_node);
  866. node > 0 && i != index;
  867. node = fdt_next_subnode(blob, node))
  868. i++;
  869. if (node < 0)
  870. return node;
  871. memset(dt, 0, sizeof(*dt));
  872. ret |= decode_timing_property(blob, node, "hback-porch",
  873. &dt->hback_porch);
  874. ret |= decode_timing_property(blob, node, "hfront-porch",
  875. &dt->hfront_porch);
  876. ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
  877. ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
  878. ret |= decode_timing_property(blob, node, "vback-porch",
  879. &dt->vback_porch);
  880. ret |= decode_timing_property(blob, node, "vfront-porch",
  881. &dt->vfront_porch);
  882. ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
  883. ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
  884. ret |= decode_timing_property(blob, node, "clock-frequency",
  885. &dt->pixelclock);
  886. dt->flags = 0;
  887. val = fdtdec_get_int(blob, node, "vsync-active", -1);
  888. if (val != -1) {
  889. dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
  890. DISPLAY_FLAGS_VSYNC_LOW;
  891. }
  892. val = fdtdec_get_int(blob, node, "hsync-active", -1);
  893. if (val != -1) {
  894. dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
  895. DISPLAY_FLAGS_HSYNC_LOW;
  896. }
  897. val = fdtdec_get_int(blob, node, "de-active", -1);
  898. if (val != -1) {
  899. dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
  900. DISPLAY_FLAGS_DE_LOW;
  901. }
  902. val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
  903. if (val != -1) {
  904. dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
  905. DISPLAY_FLAGS_PIXDATA_NEGEDGE;
  906. }
  907. if (fdtdec_get_bool(blob, node, "interlaced"))
  908. dt->flags |= DISPLAY_FLAGS_INTERLACED;
  909. if (fdtdec_get_bool(blob, node, "doublescan"))
  910. dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
  911. if (fdtdec_get_bool(blob, node, "doubleclk"))
  912. dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
  913. return ret;
  914. }
  915. int fdtdec_setup_mem_size_base(void)
  916. {
  917. int ret, mem;
  918. struct fdt_resource res;
  919. mem = fdt_path_offset(gd->fdt_blob, "/memory");
  920. if (mem < 0) {
  921. debug("%s: Missing /memory node\n", __func__);
  922. return -EINVAL;
  923. }
  924. ret = fdt_get_resource(gd->fdt_blob, mem, "reg", 0, &res);
  925. if (ret != 0) {
  926. debug("%s: Unable to decode first memory bank\n", __func__);
  927. return -EINVAL;
  928. }
  929. gd->ram_size = (phys_size_t)(res.end - res.start + 1);
  930. gd->ram_base = (unsigned long)res.start;
  931. debug("%s: Initial DRAM size %llx\n", __func__,
  932. (unsigned long long)gd->ram_size);
  933. return 0;
  934. }
  935. #if defined(CONFIG_NR_DRAM_BANKS)
  936. static int get_next_memory_node(const void *blob, int mem)
  937. {
  938. do {
  939. mem = fdt_node_offset_by_prop_value(gd->fdt_blob, mem,
  940. "device_type", "memory", 7);
  941. } while (!fdtdec_get_is_enabled(blob, mem));
  942. return mem;
  943. }
  944. int fdtdec_setup_memory_banksize(void)
  945. {
  946. int bank, ret, mem, reg = 0;
  947. struct fdt_resource res;
  948. mem = get_next_memory_node(gd->fdt_blob, -1);
  949. if (mem < 0) {
  950. debug("%s: Missing /memory node\n", __func__);
  951. return -EINVAL;
  952. }
  953. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  954. ret = fdt_get_resource(gd->fdt_blob, mem, "reg", reg++, &res);
  955. if (ret == -FDT_ERR_NOTFOUND) {
  956. reg = 0;
  957. mem = get_next_memory_node(gd->fdt_blob, mem);
  958. if (mem == -FDT_ERR_NOTFOUND)
  959. break;
  960. ret = fdt_get_resource(gd->fdt_blob, mem, "reg", reg++, &res);
  961. if (ret == -FDT_ERR_NOTFOUND)
  962. break;
  963. }
  964. if (ret != 0) {
  965. return -EINVAL;
  966. }
  967. gd->bd->bi_dram[bank].start = (phys_addr_t)res.start;
  968. gd->bd->bi_dram[bank].size =
  969. (phys_size_t)(res.end - res.start + 1);
  970. debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n",
  971. __func__, bank,
  972. (unsigned long long)gd->bd->bi_dram[bank].start,
  973. (unsigned long long)gd->bd->bi_dram[bank].size);
  974. }
  975. return 0;
  976. }
  977. #endif
  978. #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
  979. # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_GZIP) ||\
  980. CONFIG_IS_ENABLED(MULTI_DTB_FIT_LZO)
  981. static int uncompress_blob(const void *src, ulong sz_src, void **dstp)
  982. {
  983. size_t sz_out = CONFIG_SPL_MULTI_DTB_FIT_UNCOMPRESS_SZ;
  984. ulong sz_in = sz_src;
  985. void *dst;
  986. int rc;
  987. if (CONFIG_IS_ENABLED(GZIP))
  988. if (gzip_parse_header(src, sz_in) < 0)
  989. return -1;
  990. if (CONFIG_IS_ENABLED(LZO))
  991. if (!lzop_is_valid_header(src))
  992. return -EBADMSG;
  993. if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC)) {
  994. dst = malloc(sz_out);
  995. if (!dst) {
  996. puts("uncompress_blob: Unable to allocate memory\n");
  997. return -ENOMEM;
  998. }
  999. } else {
  1000. # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_USER_DEFINED_AREA)
  1001. dst = (void *)CONFIG_VAL(MULTI_DTB_FIT_USER_DEF_ADDR);
  1002. # else
  1003. return -ENOTSUPP;
  1004. # endif
  1005. }
  1006. if (CONFIG_IS_ENABLED(GZIP))
  1007. rc = gunzip(dst, sz_out, (u8 *)src, &sz_in);
  1008. else if (CONFIG_IS_ENABLED(LZO))
  1009. rc = lzop_decompress(src, sz_in, dst, &sz_out);
  1010. if (rc < 0) {
  1011. /* not a valid compressed blob */
  1012. puts("uncompress_blob: Unable to uncompress\n");
  1013. if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC))
  1014. free(dst);
  1015. return -EBADMSG;
  1016. }
  1017. *dstp = dst;
  1018. return 0;
  1019. }
  1020. # else
  1021. static int uncompress_blob(const void *src, ulong sz_src, void **dstp)
  1022. {
  1023. return -ENOTSUPP;
  1024. }
  1025. # endif
  1026. #endif
  1027. #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
  1028. /*
  1029. * For CONFIG_OF_SEPARATE, the board may optionally implement this to
  1030. * provide and/or fixup the fdt.
  1031. */
  1032. __weak void *board_fdt_blob_setup(void)
  1033. {
  1034. void *fdt_blob = NULL;
  1035. #ifdef CONFIG_SPL_BUILD
  1036. /* FDT is at end of BSS unless it is in a different memory region */
  1037. if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
  1038. fdt_blob = (ulong *)&_image_binary_end;
  1039. else
  1040. fdt_blob = (ulong *)&__bss_end;
  1041. #else
  1042. /* FDT is at end of image */
  1043. fdt_blob = (ulong *)&_end;
  1044. #endif
  1045. return fdt_blob;
  1046. }
  1047. #endif
  1048. int fdtdec_setup(void)
  1049. {
  1050. #if CONFIG_IS_ENABLED(OF_CONTROL)
  1051. # if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
  1052. void *fdt_blob;
  1053. # endif
  1054. # ifdef CONFIG_OF_EMBED
  1055. /* Get a pointer to the FDT */
  1056. # ifdef CONFIG_SPL_BUILD
  1057. gd->fdt_blob = __dtb_dt_spl_begin;
  1058. # else
  1059. gd->fdt_blob = __dtb_dt_begin;
  1060. # endif
  1061. # elif defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
  1062. /* Allow the board to override the fdt address. */
  1063. gd->fdt_blob = board_fdt_blob_setup();
  1064. # elif defined(CONFIG_OF_HOSTFILE)
  1065. if (sandbox_read_fdt_from_file()) {
  1066. puts("Failed to read control FDT\n");
  1067. return -1;
  1068. }
  1069. # endif
  1070. # ifndef CONFIG_SPL_BUILD
  1071. /* Allow the early environment to override the fdt address */
  1072. # if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
  1073. gd->fdt_blob = (void *)prior_stage_fdt_address;
  1074. # else
  1075. gd->fdt_blob = (void *)env_get_ulong("fdtcontroladdr", 16,
  1076. (uintptr_t)gd->fdt_blob);
  1077. # endif
  1078. # endif
  1079. # if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
  1080. /*
  1081. * Try and uncompress the blob.
  1082. * Unfortunately there is no way to know how big the input blob really
  1083. * is. So let us set the maximum input size arbitrarily high. 16MB
  1084. * ought to be more than enough for packed DTBs.
  1085. */
  1086. if (uncompress_blob(gd->fdt_blob, 0x1000000, &fdt_blob) == 0)
  1087. gd->fdt_blob = fdt_blob;
  1088. /*
  1089. * Check if blob is a FIT images containings DTBs.
  1090. * If so, pick the most relevant
  1091. */
  1092. fdt_blob = locate_dtb_in_fit(gd->fdt_blob);
  1093. if (fdt_blob)
  1094. gd->fdt_blob = fdt_blob;
  1095. # endif
  1096. #endif
  1097. return fdtdec_prepare_fdt();
  1098. }
  1099. #ifdef CONFIG_NR_DRAM_BANKS
  1100. int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
  1101. phys_addr_t *basep, phys_size_t *sizep, bd_t *bd)
  1102. {
  1103. int addr_cells, size_cells;
  1104. const u32 *cell, *end;
  1105. u64 total_size, size, addr;
  1106. int node, child;
  1107. bool auto_size;
  1108. int bank;
  1109. int len;
  1110. debug("%s: board_id=%d\n", __func__, board_id);
  1111. if (!area)
  1112. area = "/memory";
  1113. node = fdt_path_offset(blob, area);
  1114. if (node < 0) {
  1115. debug("No %s node found\n", area);
  1116. return -ENOENT;
  1117. }
  1118. cell = fdt_getprop(blob, node, "reg", &len);
  1119. if (!cell) {
  1120. debug("No reg property found\n");
  1121. return -ENOENT;
  1122. }
  1123. addr_cells = fdt_address_cells(blob, node);
  1124. size_cells = fdt_size_cells(blob, node);
  1125. /* Check the board id and mask */
  1126. for (child = fdt_first_subnode(blob, node);
  1127. child >= 0;
  1128. child = fdt_next_subnode(blob, child)) {
  1129. int match_mask, match_value;
  1130. match_mask = fdtdec_get_int(blob, child, "match-mask", -1);
  1131. match_value = fdtdec_get_int(blob, child, "match-value", -1);
  1132. if (match_value >= 0 &&
  1133. ((board_id & match_mask) == match_value)) {
  1134. /* Found matching mask */
  1135. debug("Found matching mask %d\n", match_mask);
  1136. node = child;
  1137. cell = fdt_getprop(blob, node, "reg", &len);
  1138. if (!cell) {
  1139. debug("No memory-banks property found\n");
  1140. return -EINVAL;
  1141. }
  1142. break;
  1143. }
  1144. }
  1145. /* Note: if no matching subnode was found we use the parent node */
  1146. if (bd) {
  1147. memset(bd->bi_dram, '\0', sizeof(bd->bi_dram[0]) *
  1148. CONFIG_NR_DRAM_BANKS);
  1149. }
  1150. auto_size = fdtdec_get_bool(blob, node, "auto-size");
  1151. total_size = 0;
  1152. end = cell + len / 4 - addr_cells - size_cells;
  1153. debug("cell at %p, end %p\n", cell, end);
  1154. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  1155. if (cell > end)
  1156. break;
  1157. addr = 0;
  1158. if (addr_cells == 2)
  1159. addr += (u64)fdt32_to_cpu(*cell++) << 32UL;
  1160. addr += fdt32_to_cpu(*cell++);
  1161. if (bd)
  1162. bd->bi_dram[bank].start = addr;
  1163. if (basep && !bank)
  1164. *basep = (phys_addr_t)addr;
  1165. size = 0;
  1166. if (size_cells == 2)
  1167. size += (u64)fdt32_to_cpu(*cell++) << 32UL;
  1168. size += fdt32_to_cpu(*cell++);
  1169. if (auto_size) {
  1170. u64 new_size;
  1171. debug("Auto-sizing %llx, size %llx: ", addr, size);
  1172. new_size = get_ram_size((long *)(uintptr_t)addr, size);
  1173. if (new_size == size) {
  1174. debug("OK\n");
  1175. } else {
  1176. debug("sized to %llx\n", new_size);
  1177. size = new_size;
  1178. }
  1179. }
  1180. if (bd)
  1181. bd->bi_dram[bank].size = size;
  1182. total_size += size;
  1183. }
  1184. debug("Memory size %llu\n", total_size);
  1185. if (sizep)
  1186. *sizep = (phys_size_t)total_size;
  1187. return 0;
  1188. }
  1189. #endif /* CONFIG_NR_DRAM_BANKS */
  1190. #endif /* !USE_HOSTCC */