fdtdec.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  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 <hang.h>
  10. #include <init.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <net.h>
  14. #include <dm/of_extra.h>
  15. #include <env.h>
  16. #include <errno.h>
  17. #include <fdtdec.h>
  18. #include <fdt_support.h>
  19. #include <gzip.h>
  20. #include <mapmem.h>
  21. #include <linux/libfdt.h>
  22. #include <serial.h>
  23. #include <asm/sections.h>
  24. #include <linux/ctype.h>
  25. #include <linux/lzo.h>
  26. #include <linux/ioport.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. /*
  29. * Here are the type we know about. One day we might allow drivers to
  30. * register. For now we just put them here. The COMPAT macro allows us to
  31. * turn this into a sparse list later, and keeps the ID with the name.
  32. *
  33. * NOTE: This list is basically a TODO list for things that need to be
  34. * converted to driver model. So don't add new things here unless there is a
  35. * good reason why driver-model conversion is infeasible. Examples include
  36. * things which are used before driver model is available.
  37. */
  38. #define COMPAT(id, name) name
  39. static const char * const compat_names[COMPAT_COUNT] = {
  40. COMPAT(UNKNOWN, "<none>"),
  41. COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
  42. COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
  43. COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
  44. COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
  45. COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
  46. COMPAT(SMSC_LAN9215, "smsc,lan9215"),
  47. COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
  48. COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
  49. COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
  50. COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
  51. COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
  52. COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
  53. COMPAT(GENERIC_SPI_FLASH, "jedec,spi-nor"),
  54. COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
  55. COMPAT(INTEL_MICROCODE, "intel,microcode"),
  56. COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
  57. COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
  58. COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"),
  59. COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"),
  60. COMPAT(INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
  61. COMPAT(INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
  62. COMPAT(INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"),
  63. COMPAT(COMPAT_SUNXI_NAND, "allwinner,sun4i-a10-nand"),
  64. COMPAT(ALTERA_SOCFPGA_CLK, "altr,clk-mgr"),
  65. COMPAT(ALTERA_SOCFPGA_PINCTRL_SINGLE, "pinctrl-single"),
  66. COMPAT(ALTERA_SOCFPGA_H2F_BRG, "altr,socfpga-hps2fpga-bridge"),
  67. COMPAT(ALTERA_SOCFPGA_LWH2F_BRG, "altr,socfpga-lwhps2fpga-bridge"),
  68. COMPAT(ALTERA_SOCFPGA_F2H_BRG, "altr,socfpga-fpga2hps-bridge"),
  69. COMPAT(ALTERA_SOCFPGA_F2SDR0, "altr,socfpga-fpga2sdram0-bridge"),
  70. COMPAT(ALTERA_SOCFPGA_F2SDR1, "altr,socfpga-fpga2sdram1-bridge"),
  71. COMPAT(ALTERA_SOCFPGA_F2SDR2, "altr,socfpga-fpga2sdram2-bridge"),
  72. COMPAT(ALTERA_SOCFPGA_FPGA0, "altr,socfpga-a10-fpga-mgr"),
  73. COMPAT(ALTERA_SOCFPGA_NOC, "altr,socfpga-a10-noc"),
  74. COMPAT(ALTERA_SOCFPGA_CLK_INIT, "altr,socfpga-a10-clk-init")
  75. };
  76. const char *fdtdec_get_compatible(enum fdt_compat_id id)
  77. {
  78. /* We allow reading of the 'unknown' ID for testing purposes */
  79. assert(id >= 0 && id < COMPAT_COUNT);
  80. return compat_names[id];
  81. }
  82. fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
  83. const char *prop_name, int index, int na,
  84. int ns, fdt_size_t *sizep,
  85. bool translate)
  86. {
  87. const fdt32_t *prop, *prop_end;
  88. const fdt32_t *prop_addr, *prop_size, *prop_after_size;
  89. int len;
  90. fdt_addr_t addr;
  91. debug("%s: %s: ", __func__, prop_name);
  92. prop = fdt_getprop(blob, node, prop_name, &len);
  93. if (!prop) {
  94. debug("(not found)\n");
  95. return FDT_ADDR_T_NONE;
  96. }
  97. prop_end = prop + (len / sizeof(*prop));
  98. prop_addr = prop + (index * (na + ns));
  99. prop_size = prop_addr + na;
  100. prop_after_size = prop_size + ns;
  101. if (prop_after_size > prop_end) {
  102. debug("(not enough data: expected >= %d cells, got %d cells)\n",
  103. (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
  104. return FDT_ADDR_T_NONE;
  105. }
  106. #if CONFIG_IS_ENABLED(OF_TRANSLATE)
  107. if (translate)
  108. addr = fdt_translate_address(blob, node, prop_addr);
  109. else
  110. #endif
  111. addr = fdtdec_get_number(prop_addr, na);
  112. if (sizep) {
  113. *sizep = fdtdec_get_number(prop_size, ns);
  114. debug("addr=%08llx, size=%llx\n", (unsigned long long)addr,
  115. (unsigned long long)*sizep);
  116. } else {
  117. debug("addr=%08llx\n", (unsigned long long)addr);
  118. }
  119. return addr;
  120. }
  121. fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
  122. int node, const char *prop_name,
  123. int index, fdt_size_t *sizep,
  124. bool translate)
  125. {
  126. int na, ns;
  127. debug("%s: ", __func__);
  128. na = fdt_address_cells(blob, parent);
  129. if (na < 1) {
  130. debug("(bad #address-cells)\n");
  131. return FDT_ADDR_T_NONE;
  132. }
  133. ns = fdt_size_cells(blob, parent);
  134. if (ns < 0) {
  135. debug("(bad #size-cells)\n");
  136. return FDT_ADDR_T_NONE;
  137. }
  138. debug("na=%d, ns=%d, ", na, ns);
  139. return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na,
  140. ns, sizep, translate);
  141. }
  142. fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
  143. const char *prop_name, int index,
  144. fdt_size_t *sizep,
  145. bool translate)
  146. {
  147. int parent;
  148. debug("%s: ", __func__);
  149. parent = fdt_parent_offset(blob, node);
  150. if (parent < 0) {
  151. debug("(no parent found)\n");
  152. return FDT_ADDR_T_NONE;
  153. }
  154. return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name,
  155. index, sizep, translate);
  156. }
  157. fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
  158. const char *prop_name, fdt_size_t *sizep)
  159. {
  160. int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0;
  161. return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0,
  162. sizeof(fdt_addr_t) / sizeof(fdt32_t),
  163. ns, sizep, false);
  164. }
  165. fdt_addr_t fdtdec_get_addr(const void *blob, int node, const char *prop_name)
  166. {
  167. return fdtdec_get_addr_size(blob, node, prop_name, NULL);
  168. }
  169. #if CONFIG_IS_ENABLED(PCI) && defined(CONFIG_DM_PCI)
  170. int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
  171. {
  172. const char *list, *end;
  173. int len;
  174. list = fdt_getprop(blob, node, "compatible", &len);
  175. if (!list)
  176. return -ENOENT;
  177. end = list + len;
  178. while (list < end) {
  179. len = strlen(list);
  180. if (len >= strlen("pciVVVV,DDDD")) {
  181. char *s = strstr(list, "pci");
  182. /*
  183. * check if the string is something like pciVVVV,DDDD.RR
  184. * or just pciVVVV,DDDD
  185. */
  186. if (s && s[7] == ',' &&
  187. (s[12] == '.' || s[12] == 0)) {
  188. s += 3;
  189. *vendor = simple_strtol(s, NULL, 16);
  190. s += 5;
  191. *device = simple_strtol(s, NULL, 16);
  192. return 0;
  193. }
  194. }
  195. list += (len + 1);
  196. }
  197. return -ENOENT;
  198. }
  199. int fdtdec_get_pci_bar32(const struct udevice *dev, struct fdt_pci_addr *addr,
  200. u32 *bar)
  201. {
  202. int barnum;
  203. /* extract the bar number from fdt_pci_addr */
  204. barnum = addr->phys_hi & 0xff;
  205. if (barnum < PCI_BASE_ADDRESS_0 || barnum > PCI_CARDBUS_CIS)
  206. return -EINVAL;
  207. barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
  208. *bar = dm_pci_read_bar32(dev, barnum);
  209. return 0;
  210. }
  211. int fdtdec_get_pci_bus_range(const void *blob, int node,
  212. struct fdt_resource *res)
  213. {
  214. const u32 *values;
  215. int len;
  216. values = fdt_getprop(blob, node, "bus-range", &len);
  217. if (!values || len < sizeof(*values) * 2)
  218. return -EINVAL;
  219. res->start = fdt32_to_cpu(*values++);
  220. res->end = fdt32_to_cpu(*values);
  221. return 0;
  222. }
  223. #endif
  224. uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
  225. uint64_t default_val)
  226. {
  227. const unaligned_fdt64_t *cell64;
  228. int length;
  229. cell64 = fdt_getprop(blob, node, prop_name, &length);
  230. if (!cell64 || length < sizeof(*cell64))
  231. return default_val;
  232. return fdt64_to_cpu(*cell64);
  233. }
  234. int fdtdec_get_is_enabled(const void *blob, int node)
  235. {
  236. const char *cell;
  237. /*
  238. * It should say "okay", so only allow that. Some fdts use "ok" but
  239. * this is a bug. Please fix your device tree source file. See here
  240. * for discussion:
  241. *
  242. * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
  243. */
  244. cell = fdt_getprop(blob, node, "status", NULL);
  245. if (cell)
  246. return strcmp(cell, "okay") == 0;
  247. return 1;
  248. }
  249. enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
  250. {
  251. enum fdt_compat_id id;
  252. /* Search our drivers */
  253. for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
  254. if (fdt_node_check_compatible(blob, node,
  255. compat_names[id]) == 0)
  256. return id;
  257. return COMPAT_UNKNOWN;
  258. }
  259. int fdtdec_next_compatible(const void *blob, int node, enum fdt_compat_id id)
  260. {
  261. return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
  262. }
  263. int fdtdec_next_compatible_subnode(const void *blob, int node,
  264. enum fdt_compat_id id, int *depthp)
  265. {
  266. do {
  267. node = fdt_next_node(blob, node, depthp);
  268. } while (*depthp > 1);
  269. /* If this is a direct subnode, and compatible, return it */
  270. if (*depthp == 1 && 0 == fdt_node_check_compatible(
  271. blob, node, compat_names[id]))
  272. return node;
  273. return -FDT_ERR_NOTFOUND;
  274. }
  275. int fdtdec_next_alias(const void *blob, const char *name, enum fdt_compat_id id,
  276. int *upto)
  277. {
  278. #define MAX_STR_LEN 20
  279. char str[MAX_STR_LEN + 20];
  280. int node, err;
  281. /* snprintf() is not available */
  282. assert(strlen(name) < MAX_STR_LEN);
  283. sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
  284. node = fdt_path_offset(blob, str);
  285. if (node < 0)
  286. return node;
  287. err = fdt_node_check_compatible(blob, node, compat_names[id]);
  288. if (err < 0)
  289. return err;
  290. if (err)
  291. return -FDT_ERR_NOTFOUND;
  292. (*upto)++;
  293. return node;
  294. }
  295. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  296. enum fdt_compat_id id, int *node_list,
  297. int maxcount)
  298. {
  299. memset(node_list, '\0', sizeof(*node_list) * maxcount);
  300. return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
  301. }
  302. /* TODO: Can we tighten this code up a little? */
  303. int fdtdec_add_aliases_for_id(const void *blob, const char *name,
  304. enum fdt_compat_id id, int *node_list,
  305. int maxcount)
  306. {
  307. int name_len = strlen(name);
  308. int nodes[maxcount];
  309. int num_found = 0;
  310. int offset, node;
  311. int alias_node;
  312. int count;
  313. int i, j;
  314. /* find the alias node if present */
  315. alias_node = fdt_path_offset(blob, "/aliases");
  316. /*
  317. * start with nothing, and we can assume that the root node can't
  318. * match
  319. */
  320. memset(nodes, '\0', sizeof(nodes));
  321. /* First find all the compatible nodes */
  322. for (node = count = 0; node >= 0 && count < maxcount;) {
  323. node = fdtdec_next_compatible(blob, node, id);
  324. if (node >= 0)
  325. nodes[count++] = node;
  326. }
  327. if (node >= 0)
  328. debug("%s: warning: maxcount exceeded with alias '%s'\n",
  329. __func__, name);
  330. /* Now find all the aliases */
  331. for (offset = fdt_first_property_offset(blob, alias_node);
  332. offset > 0;
  333. offset = fdt_next_property_offset(blob, offset)) {
  334. const struct fdt_property *prop;
  335. const char *path;
  336. int number;
  337. int found;
  338. node = 0;
  339. prop = fdt_get_property_by_offset(blob, offset, NULL);
  340. path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  341. if (prop->len && 0 == strncmp(path, name, name_len))
  342. node = fdt_path_offset(blob, prop->data);
  343. if (node <= 0)
  344. continue;
  345. /* Get the alias number */
  346. number = simple_strtoul(path + name_len, NULL, 10);
  347. if (number < 0 || number >= maxcount) {
  348. debug("%s: warning: alias '%s' is out of range\n",
  349. __func__, path);
  350. continue;
  351. }
  352. /* Make sure the node we found is actually in our list! */
  353. found = -1;
  354. for (j = 0; j < count; j++)
  355. if (nodes[j] == node) {
  356. found = j;
  357. break;
  358. }
  359. if (found == -1) {
  360. debug("%s: warning: alias '%s' points to a node "
  361. "'%s' that is missing or is not compatible "
  362. " with '%s'\n", __func__, path,
  363. fdt_get_name(blob, node, NULL),
  364. compat_names[id]);
  365. continue;
  366. }
  367. /*
  368. * Add this node to our list in the right place, and mark
  369. * it as done.
  370. */
  371. if (fdtdec_get_is_enabled(blob, node)) {
  372. if (node_list[number]) {
  373. debug("%s: warning: alias '%s' requires that "
  374. "a node be placed in the list in a "
  375. "position which is already filled by "
  376. "node '%s'\n", __func__, path,
  377. fdt_get_name(blob, node, NULL));
  378. continue;
  379. }
  380. node_list[number] = node;
  381. if (number >= num_found)
  382. num_found = number + 1;
  383. }
  384. nodes[found] = 0;
  385. }
  386. /* Add any nodes not mentioned by an alias */
  387. for (i = j = 0; i < maxcount; i++) {
  388. if (!node_list[i]) {
  389. for (; j < maxcount; j++)
  390. if (nodes[j] &&
  391. fdtdec_get_is_enabled(blob, nodes[j]))
  392. break;
  393. /* Have we run out of nodes to add? */
  394. if (j == maxcount)
  395. break;
  396. assert(!node_list[i]);
  397. node_list[i] = nodes[j++];
  398. if (i >= num_found)
  399. num_found = i + 1;
  400. }
  401. }
  402. return num_found;
  403. }
  404. int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
  405. int *seqp)
  406. {
  407. int base_len = strlen(base);
  408. const char *find_name;
  409. int find_namelen;
  410. int prop_offset;
  411. int aliases;
  412. find_name = fdt_get_name(blob, offset, &find_namelen);
  413. debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
  414. aliases = fdt_path_offset(blob, "/aliases");
  415. for (prop_offset = fdt_first_property_offset(blob, aliases);
  416. prop_offset > 0;
  417. prop_offset = fdt_next_property_offset(blob, prop_offset)) {
  418. const char *prop;
  419. const char *name;
  420. const char *slash;
  421. int len, val;
  422. prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
  423. debug(" - %s, %s\n", name, prop);
  424. if (len < find_namelen || *prop != '/' || prop[len - 1] ||
  425. strncmp(name, base, base_len))
  426. continue;
  427. slash = strrchr(prop, '/');
  428. if (strcmp(slash + 1, find_name))
  429. continue;
  430. /*
  431. * Adding an extra check to distinguish DT nodes with
  432. * same name
  433. */
  434. if (IS_ENABLED(CONFIG_PHANDLE_CHECK_SEQ)) {
  435. if (fdt_get_phandle(blob, offset) !=
  436. fdt_get_phandle(blob, fdt_path_offset(blob, prop)))
  437. continue;
  438. }
  439. val = trailing_strtol(name);
  440. if (val != -1) {
  441. *seqp = val;
  442. debug("Found seq %d\n", *seqp);
  443. return 0;
  444. }
  445. }
  446. debug("Not found\n");
  447. return -ENOENT;
  448. }
  449. int fdtdec_get_alias_highest_id(const void *blob, const char *base)
  450. {
  451. int base_len = strlen(base);
  452. int prop_offset;
  453. int aliases;
  454. int max = -1;
  455. debug("Looking for highest alias id for '%s'\n", base);
  456. aliases = fdt_path_offset(blob, "/aliases");
  457. for (prop_offset = fdt_first_property_offset(blob, aliases);
  458. prop_offset > 0;
  459. prop_offset = fdt_next_property_offset(blob, prop_offset)) {
  460. const char *prop;
  461. const char *name;
  462. int len, val;
  463. prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
  464. debug(" - %s, %s\n", name, prop);
  465. if (*prop != '/' || prop[len - 1] ||
  466. strncmp(name, base, base_len))
  467. continue;
  468. val = trailing_strtol(name);
  469. if (val > max) {
  470. debug("Found seq %d\n", val);
  471. max = val;
  472. }
  473. }
  474. return max;
  475. }
  476. const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
  477. {
  478. int chosen_node;
  479. if (!blob)
  480. return NULL;
  481. chosen_node = fdt_path_offset(blob, "/chosen");
  482. return fdt_getprop(blob, chosen_node, name, NULL);
  483. }
  484. int fdtdec_get_chosen_node(const void *blob, const char *name)
  485. {
  486. const char *prop;
  487. prop = fdtdec_get_chosen_prop(blob, name);
  488. if (!prop)
  489. return -FDT_ERR_NOTFOUND;
  490. return fdt_path_offset(blob, prop);
  491. }
  492. int fdtdec_check_fdt(void)
  493. {
  494. /*
  495. * We must have an FDT, but we cannot panic() yet since the console
  496. * is not ready. So for now, just assert(). Boards which need an early
  497. * FDT (prior to console ready) will need to make their own
  498. * arrangements and do their own checks.
  499. */
  500. assert(!fdtdec_prepare_fdt());
  501. return 0;
  502. }
  503. /*
  504. * This function is a little odd in that it accesses global data. At some
  505. * point if the architecture board.c files merge this will make more sense.
  506. * Even now, it is common code.
  507. */
  508. int fdtdec_prepare_fdt(void)
  509. {
  510. if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
  511. fdt_check_header(gd->fdt_blob)) {
  512. #ifdef CONFIG_SPL_BUILD
  513. puts("Missing DTB\n");
  514. #else
  515. puts("No valid device tree binary found - please append one to U-Boot binary, use u-boot-dtb.bin or define CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
  516. # ifdef DEBUG
  517. if (gd->fdt_blob) {
  518. printf("fdt_blob=%p\n", gd->fdt_blob);
  519. print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4,
  520. 32, 0);
  521. }
  522. # endif
  523. #endif
  524. return -1;
  525. }
  526. return 0;
  527. }
  528. int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
  529. {
  530. const u32 *phandle;
  531. int lookup;
  532. debug("%s: %s\n", __func__, prop_name);
  533. phandle = fdt_getprop(blob, node, prop_name, NULL);
  534. if (!phandle)
  535. return -FDT_ERR_NOTFOUND;
  536. lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
  537. return lookup;
  538. }
  539. /**
  540. * Look up a property in a node and check that it has a minimum length.
  541. *
  542. * @param blob FDT blob
  543. * @param node node to examine
  544. * @param prop_name name of property to find
  545. * @param min_len minimum property length in bytes
  546. * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
  547. found, or -FDT_ERR_BADLAYOUT if not enough data
  548. * @return pointer to cell, which is only valid if err == 0
  549. */
  550. static const void *get_prop_check_min_len(const void *blob, int node,
  551. const char *prop_name, int min_len,
  552. int *err)
  553. {
  554. const void *cell;
  555. int len;
  556. debug("%s: %s\n", __func__, prop_name);
  557. cell = fdt_getprop(blob, node, prop_name, &len);
  558. if (!cell)
  559. *err = -FDT_ERR_NOTFOUND;
  560. else if (len < min_len)
  561. *err = -FDT_ERR_BADLAYOUT;
  562. else
  563. *err = 0;
  564. return cell;
  565. }
  566. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  567. u32 *array, int count)
  568. {
  569. const u32 *cell;
  570. int err = 0;
  571. debug("%s: %s\n", __func__, prop_name);
  572. cell = get_prop_check_min_len(blob, node, prop_name,
  573. sizeof(u32) * count, &err);
  574. if (!err) {
  575. int i;
  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 < 0) {
  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_byte_array(const void *blob, int node, const char *prop_name,
  728. u8 *array, int count)
  729. {
  730. const u8 *cell;
  731. int err;
  732. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  733. if (!err)
  734. memcpy(array, cell, count);
  735. return err;
  736. }
  737. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  738. const char *prop_name, 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. return NULL;
  745. return cell;
  746. }
  747. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  748. int default_val)
  749. {
  750. int config_node;
  751. debug("%s: %s\n", __func__, prop_name);
  752. config_node = fdt_path_offset(blob, "/config");
  753. if (config_node < 0)
  754. return default_val;
  755. return fdtdec_get_int(blob, config_node, prop_name, default_val);
  756. }
  757. int fdtdec_get_config_bool(const void *blob, const char *prop_name)
  758. {
  759. int config_node;
  760. const void *prop;
  761. debug("%s: %s\n", __func__, prop_name);
  762. config_node = fdt_path_offset(blob, "/config");
  763. if (config_node < 0)
  764. return 0;
  765. prop = fdt_get_property(blob, config_node, prop_name, NULL);
  766. return prop != NULL;
  767. }
  768. char *fdtdec_get_config_string(const void *blob, const char *prop_name)
  769. {
  770. const char *nodep;
  771. int nodeoffset;
  772. int len;
  773. debug("%s: %s\n", __func__, prop_name);
  774. nodeoffset = fdt_path_offset(blob, "/config");
  775. if (nodeoffset < 0)
  776. return NULL;
  777. nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
  778. if (!nodep)
  779. return NULL;
  780. return (char *)nodep;
  781. }
  782. u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
  783. {
  784. u64 number = 0;
  785. while (cells--)
  786. number = (number << 32) | fdt32_to_cpu(*ptr++);
  787. return number;
  788. }
  789. int fdt_get_resource(const void *fdt, int node, const char *property,
  790. unsigned int index, struct fdt_resource *res)
  791. {
  792. const fdt32_t *ptr, *end;
  793. int na, ns, len, parent;
  794. unsigned int i = 0;
  795. parent = fdt_parent_offset(fdt, node);
  796. if (parent < 0)
  797. return parent;
  798. na = fdt_address_cells(fdt, parent);
  799. ns = fdt_size_cells(fdt, parent);
  800. ptr = fdt_getprop(fdt, node, property, &len);
  801. if (!ptr)
  802. return len;
  803. end = ptr + len / sizeof(*ptr);
  804. while (ptr + na + ns <= end) {
  805. if (i == index) {
  806. res->start = fdtdec_get_number(ptr, na);
  807. res->end = res->start;
  808. res->end += fdtdec_get_number(&ptr[na], ns) - 1;
  809. return 0;
  810. }
  811. ptr += na + ns;
  812. i++;
  813. }
  814. return -FDT_ERR_NOTFOUND;
  815. }
  816. int fdt_get_named_resource(const void *fdt, int node, const char *property,
  817. const char *prop_names, const char *name,
  818. struct fdt_resource *res)
  819. {
  820. int index;
  821. index = fdt_stringlist_search(fdt, node, prop_names, name);
  822. if (index < 0)
  823. return index;
  824. return fdt_get_resource(fdt, node, property, index, res);
  825. }
  826. static int decode_timing_property(const void *blob, int node, const char *name,
  827. struct timing_entry *result)
  828. {
  829. int length, ret = 0;
  830. const u32 *prop;
  831. prop = fdt_getprop(blob, node, name, &length);
  832. if (!prop) {
  833. debug("%s: could not find property %s\n",
  834. fdt_get_name(blob, node, NULL), name);
  835. return length;
  836. }
  837. if (length == sizeof(u32)) {
  838. result->typ = fdtdec_get_int(blob, node, name, 0);
  839. result->min = result->typ;
  840. result->max = result->typ;
  841. } else {
  842. ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
  843. }
  844. return ret;
  845. }
  846. int fdtdec_decode_display_timing(const void *blob, int parent, int index,
  847. struct display_timing *dt)
  848. {
  849. int i, node, timings_node;
  850. u32 val = 0;
  851. int ret = 0;
  852. timings_node = fdt_subnode_offset(blob, parent, "display-timings");
  853. if (timings_node < 0)
  854. return timings_node;
  855. for (i = 0, node = fdt_first_subnode(blob, timings_node);
  856. node > 0 && i != index;
  857. node = fdt_next_subnode(blob, node))
  858. i++;
  859. if (node < 0)
  860. return node;
  861. memset(dt, 0, sizeof(*dt));
  862. ret |= decode_timing_property(blob, node, "hback-porch",
  863. &dt->hback_porch);
  864. ret |= decode_timing_property(blob, node, "hfront-porch",
  865. &dt->hfront_porch);
  866. ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
  867. ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
  868. ret |= decode_timing_property(blob, node, "vback-porch",
  869. &dt->vback_porch);
  870. ret |= decode_timing_property(blob, node, "vfront-porch",
  871. &dt->vfront_porch);
  872. ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
  873. ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
  874. ret |= decode_timing_property(blob, node, "clock-frequency",
  875. &dt->pixelclock);
  876. dt->flags = 0;
  877. val = fdtdec_get_int(blob, node, "vsync-active", -1);
  878. if (val != -1) {
  879. dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
  880. DISPLAY_FLAGS_VSYNC_LOW;
  881. }
  882. val = fdtdec_get_int(blob, node, "hsync-active", -1);
  883. if (val != -1) {
  884. dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
  885. DISPLAY_FLAGS_HSYNC_LOW;
  886. }
  887. val = fdtdec_get_int(blob, node, "de-active", -1);
  888. if (val != -1) {
  889. dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
  890. DISPLAY_FLAGS_DE_LOW;
  891. }
  892. val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
  893. if (val != -1) {
  894. dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
  895. DISPLAY_FLAGS_PIXDATA_NEGEDGE;
  896. }
  897. if (fdtdec_get_bool(blob, node, "interlaced"))
  898. dt->flags |= DISPLAY_FLAGS_INTERLACED;
  899. if (fdtdec_get_bool(blob, node, "doublescan"))
  900. dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
  901. if (fdtdec_get_bool(blob, node, "doubleclk"))
  902. dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
  903. return ret;
  904. }
  905. int fdtdec_setup_mem_size_base(void)
  906. {
  907. int ret;
  908. ofnode mem;
  909. struct resource res;
  910. mem = ofnode_path("/memory");
  911. if (!ofnode_valid(mem)) {
  912. debug("%s: Missing /memory node\n", __func__);
  913. return -EINVAL;
  914. }
  915. ret = ofnode_read_resource(mem, 0, &res);
  916. if (ret != 0) {
  917. debug("%s: Unable to decode first memory bank\n", __func__);
  918. return -EINVAL;
  919. }
  920. gd->ram_size = (phys_size_t)(res.end - res.start + 1);
  921. gd->ram_base = (unsigned long)res.start;
  922. debug("%s: Initial DRAM size %llx\n", __func__,
  923. (unsigned long long)gd->ram_size);
  924. return 0;
  925. }
  926. ofnode get_next_memory_node(ofnode mem)
  927. {
  928. do {
  929. mem = ofnode_by_prop_value(mem, "device_type", "memory", 7);
  930. } while (!ofnode_is_available(mem));
  931. return mem;
  932. }
  933. int fdtdec_setup_memory_banksize(void)
  934. {
  935. int bank, ret, reg = 0;
  936. struct resource res;
  937. ofnode mem = ofnode_null();
  938. mem = get_next_memory_node(mem);
  939. if (!ofnode_valid(mem)) {
  940. debug("%s: Missing /memory node\n", __func__);
  941. return -EINVAL;
  942. }
  943. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  944. ret = ofnode_read_resource(mem, reg++, &res);
  945. if (ret < 0) {
  946. reg = 0;
  947. mem = get_next_memory_node(mem);
  948. if (!ofnode_valid(mem))
  949. break;
  950. ret = ofnode_read_resource(mem, reg++, &res);
  951. if (ret < 0)
  952. break;
  953. }
  954. if (ret != 0)
  955. return -EINVAL;
  956. gd->bd->bi_dram[bank].start = (phys_addr_t)res.start;
  957. gd->bd->bi_dram[bank].size =
  958. (phys_size_t)(res.end - res.start + 1);
  959. debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n",
  960. __func__, bank,
  961. (unsigned long long)gd->bd->bi_dram[bank].start,
  962. (unsigned long long)gd->bd->bi_dram[bank].size);
  963. }
  964. return 0;
  965. }
  966. int fdtdec_setup_mem_size_base_lowest(void)
  967. {
  968. int bank, ret, reg = 0;
  969. struct resource res;
  970. unsigned long base;
  971. phys_size_t size;
  972. ofnode mem = ofnode_null();
  973. gd->ram_base = (unsigned long)~0;
  974. mem = get_next_memory_node(mem);
  975. if (!ofnode_valid(mem)) {
  976. debug("%s: Missing /memory node\n", __func__);
  977. return -EINVAL;
  978. }
  979. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  980. ret = ofnode_read_resource(mem, reg++, &res);
  981. if (ret < 0) {
  982. reg = 0;
  983. mem = get_next_memory_node(mem);
  984. if (!ofnode_valid(mem))
  985. break;
  986. ret = ofnode_read_resource(mem, reg++, &res);
  987. if (ret < 0)
  988. break;
  989. }
  990. if (ret != 0)
  991. return -EINVAL;
  992. base = (unsigned long)res.start;
  993. size = (phys_size_t)(res.end - res.start + 1);
  994. if (gd->ram_base > base && size) {
  995. gd->ram_base = base;
  996. gd->ram_size = size;
  997. debug("%s: Initial DRAM base %lx size %lx\n",
  998. __func__, base, (unsigned long)size);
  999. }
  1000. }
  1001. return 0;
  1002. }
  1003. #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
  1004. # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_GZIP) ||\
  1005. CONFIG_IS_ENABLED(MULTI_DTB_FIT_LZO)
  1006. static int uncompress_blob(const void *src, ulong sz_src, void **dstp)
  1007. {
  1008. size_t sz_out = CONFIG_VAL(MULTI_DTB_FIT_UNCOMPRESS_SZ);
  1009. bool gzip = 0, lzo = 0;
  1010. ulong sz_in = sz_src;
  1011. void *dst;
  1012. int rc;
  1013. if (CONFIG_IS_ENABLED(GZIP))
  1014. if (gzip_parse_header(src, sz_in) >= 0)
  1015. gzip = 1;
  1016. if (CONFIG_IS_ENABLED(LZO))
  1017. if (!gzip && lzop_is_valid_header(src))
  1018. lzo = 1;
  1019. if (!gzip && !lzo)
  1020. return -EBADMSG;
  1021. if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC)) {
  1022. dst = malloc(sz_out);
  1023. if (!dst) {
  1024. puts("uncompress_blob: Unable to allocate memory\n");
  1025. return -ENOMEM;
  1026. }
  1027. } else {
  1028. # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_USER_DEFINED_AREA)
  1029. dst = (void *)CONFIG_VAL(MULTI_DTB_FIT_USER_DEF_ADDR);
  1030. # else
  1031. return -ENOTSUPP;
  1032. # endif
  1033. }
  1034. if (CONFIG_IS_ENABLED(GZIP) && gzip)
  1035. rc = gunzip(dst, sz_out, (u8 *)src, &sz_in);
  1036. else if (CONFIG_IS_ENABLED(LZO) && lzo)
  1037. rc = lzop_decompress(src, sz_in, dst, &sz_out);
  1038. else
  1039. hang();
  1040. if (rc < 0) {
  1041. /* not a valid compressed blob */
  1042. puts("uncompress_blob: Unable to uncompress\n");
  1043. if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC))
  1044. free(dst);
  1045. return -EBADMSG;
  1046. }
  1047. *dstp = dst;
  1048. return 0;
  1049. }
  1050. # else
  1051. static int uncompress_blob(const void *src, ulong sz_src, void **dstp)
  1052. {
  1053. *dstp = (void *)src;
  1054. return 0;
  1055. }
  1056. # endif
  1057. #endif
  1058. #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
  1059. /*
  1060. * For CONFIG_OF_SEPARATE, the board may optionally implement this to
  1061. * provide and/or fixup the fdt.
  1062. */
  1063. __weak void *board_fdt_blob_setup(void)
  1064. {
  1065. void *fdt_blob = NULL;
  1066. #ifdef CONFIG_SPL_BUILD
  1067. /* FDT is at end of BSS unless it is in a different memory region */
  1068. if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
  1069. fdt_blob = (ulong *)&_image_binary_end;
  1070. else
  1071. fdt_blob = (ulong *)&__bss_end;
  1072. #else
  1073. /* FDT is at end of image */
  1074. fdt_blob = (ulong *)&_end;
  1075. #endif
  1076. return fdt_blob;
  1077. }
  1078. #endif
  1079. int fdtdec_set_ethernet_mac_address(void *fdt, const u8 *mac, size_t size)
  1080. {
  1081. const char *path;
  1082. int offset, err;
  1083. if (!is_valid_ethaddr(mac))
  1084. return -EINVAL;
  1085. path = fdt_get_alias(fdt, "ethernet");
  1086. if (!path)
  1087. return 0;
  1088. debug("ethernet alias found: %s\n", path);
  1089. offset = fdt_path_offset(fdt, path);
  1090. if (offset < 0) {
  1091. debug("ethernet alias points to absent node %s\n", path);
  1092. return -ENOENT;
  1093. }
  1094. err = fdt_setprop_inplace(fdt, offset, "local-mac-address", mac, size);
  1095. if (err < 0)
  1096. return err;
  1097. debug("MAC address: %pM\n", mac);
  1098. return 0;
  1099. }
  1100. static int fdtdec_init_reserved_memory(void *blob)
  1101. {
  1102. int na, ns, node, err;
  1103. fdt32_t value;
  1104. /* inherit #address-cells and #size-cells from the root node */
  1105. na = fdt_address_cells(blob, 0);
  1106. ns = fdt_size_cells(blob, 0);
  1107. node = fdt_add_subnode(blob, 0, "reserved-memory");
  1108. if (node < 0)
  1109. return node;
  1110. err = fdt_setprop(blob, node, "ranges", NULL, 0);
  1111. if (err < 0)
  1112. return err;
  1113. value = cpu_to_fdt32(ns);
  1114. err = fdt_setprop(blob, node, "#size-cells", &value, sizeof(value));
  1115. if (err < 0)
  1116. return err;
  1117. value = cpu_to_fdt32(na);
  1118. err = fdt_setprop(blob, node, "#address-cells", &value, sizeof(value));
  1119. if (err < 0)
  1120. return err;
  1121. return node;
  1122. }
  1123. int fdtdec_add_reserved_memory(void *blob, const char *basename,
  1124. const struct fdt_memory *carveout,
  1125. uint32_t *phandlep, bool no_map)
  1126. {
  1127. fdt32_t cells[4] = {}, *ptr = cells;
  1128. uint32_t upper, lower, phandle;
  1129. int parent, node, na, ns, err;
  1130. fdt_size_t size;
  1131. char name[64];
  1132. /* create an empty /reserved-memory node if one doesn't exist */
  1133. parent = fdt_path_offset(blob, "/reserved-memory");
  1134. if (parent < 0) {
  1135. parent = fdtdec_init_reserved_memory(blob);
  1136. if (parent < 0)
  1137. return parent;
  1138. }
  1139. /* only 1 or 2 #address-cells and #size-cells are supported */
  1140. na = fdt_address_cells(blob, parent);
  1141. if (na < 1 || na > 2)
  1142. return -FDT_ERR_BADNCELLS;
  1143. ns = fdt_size_cells(blob, parent);
  1144. if (ns < 1 || ns > 2)
  1145. return -FDT_ERR_BADNCELLS;
  1146. /* find a matching node and return the phandle to that */
  1147. fdt_for_each_subnode(node, blob, parent) {
  1148. const char *name = fdt_get_name(blob, node, NULL);
  1149. fdt_addr_t addr;
  1150. fdt_size_t size;
  1151. addr = fdtdec_get_addr_size_fixed(blob, node, "reg", 0, na, ns,
  1152. &size, false);
  1153. if (addr == FDT_ADDR_T_NONE) {
  1154. debug("failed to read address/size for %s\n", name);
  1155. continue;
  1156. }
  1157. if (addr == carveout->start && (addr + size - 1) ==
  1158. carveout->end) {
  1159. if (phandlep)
  1160. *phandlep = fdt_get_phandle(blob, node);
  1161. return 0;
  1162. }
  1163. }
  1164. /*
  1165. * Unpack the start address and generate the name of the new node
  1166. * base on the basename and the unit-address.
  1167. */
  1168. upper = upper_32_bits(carveout->start);
  1169. lower = lower_32_bits(carveout->start);
  1170. if (na > 1 && upper > 0)
  1171. snprintf(name, sizeof(name), "%s@%x,%x", basename, upper,
  1172. lower);
  1173. else {
  1174. if (upper > 0) {
  1175. debug("address %08x:%08x exceeds addressable space\n",
  1176. upper, lower);
  1177. return -FDT_ERR_BADVALUE;
  1178. }
  1179. snprintf(name, sizeof(name), "%s@%x", basename, lower);
  1180. }
  1181. node = fdt_add_subnode(blob, parent, name);
  1182. if (node < 0)
  1183. return node;
  1184. if (phandlep) {
  1185. err = fdt_generate_phandle(blob, &phandle);
  1186. if (err < 0)
  1187. return err;
  1188. err = fdtdec_set_phandle(blob, node, phandle);
  1189. if (err < 0)
  1190. return err;
  1191. }
  1192. /* store one or two address cells */
  1193. if (na > 1)
  1194. *ptr++ = cpu_to_fdt32(upper);
  1195. *ptr++ = cpu_to_fdt32(lower);
  1196. /* store one or two size cells */
  1197. size = carveout->end - carveout->start + 1;
  1198. upper = upper_32_bits(size);
  1199. lower = lower_32_bits(size);
  1200. if (ns > 1)
  1201. *ptr++ = cpu_to_fdt32(upper);
  1202. *ptr++ = cpu_to_fdt32(lower);
  1203. err = fdt_setprop(blob, node, "reg", cells, (na + ns) * sizeof(*cells));
  1204. if (err < 0)
  1205. return err;
  1206. if (no_map) {
  1207. err = fdt_setprop(blob, node, "no-map", NULL, 0);
  1208. if (err < 0)
  1209. return err;
  1210. }
  1211. /* return the phandle for the new node for the caller to use */
  1212. if (phandlep)
  1213. *phandlep = phandle;
  1214. return 0;
  1215. }
  1216. int fdtdec_get_carveout(const void *blob, const char *node, const char *name,
  1217. unsigned int index, struct fdt_memory *carveout)
  1218. {
  1219. const fdt32_t *prop;
  1220. uint32_t phandle;
  1221. int offset, len;
  1222. fdt_size_t size;
  1223. offset = fdt_path_offset(blob, node);
  1224. if (offset < 0)
  1225. return offset;
  1226. prop = fdt_getprop(blob, offset, name, &len);
  1227. if (!prop) {
  1228. debug("failed to get %s for %s\n", name, node);
  1229. return -FDT_ERR_NOTFOUND;
  1230. }
  1231. if ((len % sizeof(phandle)) != 0) {
  1232. debug("invalid phandle property\n");
  1233. return -FDT_ERR_BADPHANDLE;
  1234. }
  1235. if (len < (sizeof(phandle) * (index + 1))) {
  1236. debug("invalid phandle index\n");
  1237. return -FDT_ERR_BADPHANDLE;
  1238. }
  1239. phandle = fdt32_to_cpu(prop[index]);
  1240. offset = fdt_node_offset_by_phandle(blob, phandle);
  1241. if (offset < 0) {
  1242. debug("failed to find node for phandle %u\n", phandle);
  1243. return offset;
  1244. }
  1245. carveout->start = fdtdec_get_addr_size_auto_noparent(blob, offset,
  1246. "reg", 0, &size,
  1247. true);
  1248. if (carveout->start == FDT_ADDR_T_NONE) {
  1249. debug("failed to read address/size from \"reg\" property\n");
  1250. return -FDT_ERR_NOTFOUND;
  1251. }
  1252. carveout->end = carveout->start + size - 1;
  1253. return 0;
  1254. }
  1255. int fdtdec_set_carveout(void *blob, const char *node, const char *prop_name,
  1256. unsigned int index, const char *name,
  1257. const struct fdt_memory *carveout)
  1258. {
  1259. uint32_t phandle;
  1260. int err, offset, len;
  1261. fdt32_t value;
  1262. void *prop;
  1263. err = fdtdec_add_reserved_memory(blob, name, carveout, &phandle, false);
  1264. if (err < 0) {
  1265. debug("failed to add reserved memory: %d\n", err);
  1266. return err;
  1267. }
  1268. offset = fdt_path_offset(blob, node);
  1269. if (offset < 0) {
  1270. debug("failed to find offset for node %s: %d\n", node, offset);
  1271. return offset;
  1272. }
  1273. value = cpu_to_fdt32(phandle);
  1274. if (!fdt_getprop(blob, offset, prop_name, &len)) {
  1275. if (len == -FDT_ERR_NOTFOUND)
  1276. len = 0;
  1277. else
  1278. return len;
  1279. }
  1280. if ((index + 1) * sizeof(value) > len) {
  1281. err = fdt_setprop_placeholder(blob, offset, prop_name,
  1282. (index + 1) * sizeof(value),
  1283. &prop);
  1284. if (err < 0) {
  1285. debug("failed to resize reserved memory property: %s\n",
  1286. fdt_strerror(err));
  1287. return err;
  1288. }
  1289. }
  1290. err = fdt_setprop_inplace_namelen_partial(blob, offset, prop_name,
  1291. strlen(prop_name),
  1292. index * sizeof(value),
  1293. &value, sizeof(value));
  1294. if (err < 0) {
  1295. debug("failed to update %s property for node %s: %s\n",
  1296. prop_name, node, fdt_strerror(err));
  1297. return err;
  1298. }
  1299. return 0;
  1300. }
  1301. __weak int fdtdec_board_setup(const void *fdt_blob)
  1302. {
  1303. return 0;
  1304. }
  1305. int fdtdec_setup(void)
  1306. {
  1307. int ret;
  1308. #if CONFIG_IS_ENABLED(OF_CONTROL)
  1309. # if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
  1310. void *fdt_blob;
  1311. # endif
  1312. # ifdef CONFIG_OF_EMBED
  1313. /* Get a pointer to the FDT */
  1314. # ifdef CONFIG_SPL_BUILD
  1315. gd->fdt_blob = __dtb_dt_spl_begin;
  1316. # else
  1317. gd->fdt_blob = __dtb_dt_begin;
  1318. # endif
  1319. # elif defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
  1320. /* Allow the board to override the fdt address. */
  1321. gd->fdt_blob = board_fdt_blob_setup();
  1322. # elif defined(CONFIG_OF_HOSTFILE)
  1323. if (sandbox_read_fdt_from_file()) {
  1324. puts("Failed to read control FDT\n");
  1325. return -1;
  1326. }
  1327. # elif defined(CONFIG_OF_PRIOR_STAGE)
  1328. gd->fdt_blob = (void *)prior_stage_fdt_address;
  1329. # endif
  1330. # ifndef CONFIG_SPL_BUILD
  1331. /* Allow the early environment to override the fdt address */
  1332. gd->fdt_blob = map_sysmem
  1333. (env_get_ulong("fdtcontroladdr", 16,
  1334. (unsigned long)map_to_sysmem(gd->fdt_blob)), 0);
  1335. # endif
  1336. # if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
  1337. /*
  1338. * Try and uncompress the blob.
  1339. * Unfortunately there is no way to know how big the input blob really
  1340. * is. So let us set the maximum input size arbitrarily high. 16MB
  1341. * ought to be more than enough for packed DTBs.
  1342. */
  1343. if (uncompress_blob(gd->fdt_blob, 0x1000000, &fdt_blob) == 0)
  1344. gd->fdt_blob = fdt_blob;
  1345. /*
  1346. * Check if blob is a FIT images containings DTBs.
  1347. * If so, pick the most relevant
  1348. */
  1349. fdt_blob = locate_dtb_in_fit(gd->fdt_blob);
  1350. if (fdt_blob) {
  1351. gd->multi_dtb_fit = gd->fdt_blob;
  1352. gd->fdt_blob = fdt_blob;
  1353. }
  1354. # endif
  1355. #endif
  1356. ret = fdtdec_prepare_fdt();
  1357. if (!ret)
  1358. ret = fdtdec_board_setup(gd->fdt_blob);
  1359. return ret;
  1360. }
  1361. #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
  1362. int fdtdec_resetup(int *rescan)
  1363. {
  1364. void *fdt_blob;
  1365. /*
  1366. * If the current DTB is part of a compressed FIT image,
  1367. * try to locate the best match from the uncompressed
  1368. * FIT image stillpresent there. Save the time and space
  1369. * required to uncompress it again.
  1370. */
  1371. if (gd->multi_dtb_fit) {
  1372. fdt_blob = locate_dtb_in_fit(gd->multi_dtb_fit);
  1373. if (fdt_blob == gd->fdt_blob) {
  1374. /*
  1375. * The best match did not change. no need to tear down
  1376. * the DM and rescan the fdt.
  1377. */
  1378. *rescan = 0;
  1379. return 0;
  1380. }
  1381. *rescan = 1;
  1382. gd->fdt_blob = fdt_blob;
  1383. return fdtdec_prepare_fdt();
  1384. }
  1385. /*
  1386. * If multi_dtb_fit is NULL, it means that blob appended to u-boot is
  1387. * not a FIT image containings DTB, but a single DTB. There is no need
  1388. * to teard down DM and rescan the DT in this case.
  1389. */
  1390. *rescan = 0;
  1391. return 0;
  1392. }
  1393. #endif
  1394. int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
  1395. phys_addr_t *basep, phys_size_t *sizep,
  1396. struct bd_info *bd)
  1397. {
  1398. int addr_cells, size_cells;
  1399. const u32 *cell, *end;
  1400. u64 total_size, size, addr;
  1401. int node, child;
  1402. bool auto_size;
  1403. int bank;
  1404. int len;
  1405. debug("%s: board_id=%d\n", __func__, board_id);
  1406. if (!area)
  1407. area = "/memory";
  1408. node = fdt_path_offset(blob, area);
  1409. if (node < 0) {
  1410. debug("No %s node found\n", area);
  1411. return -ENOENT;
  1412. }
  1413. cell = fdt_getprop(blob, node, "reg", &len);
  1414. if (!cell) {
  1415. debug("No reg property found\n");
  1416. return -ENOENT;
  1417. }
  1418. addr_cells = fdt_address_cells(blob, node);
  1419. size_cells = fdt_size_cells(blob, node);
  1420. /* Check the board id and mask */
  1421. for (child = fdt_first_subnode(blob, node);
  1422. child >= 0;
  1423. child = fdt_next_subnode(blob, child)) {
  1424. int match_mask, match_value;
  1425. match_mask = fdtdec_get_int(blob, child, "match-mask", -1);
  1426. match_value = fdtdec_get_int(blob, child, "match-value", -1);
  1427. if (match_value >= 0 &&
  1428. ((board_id & match_mask) == match_value)) {
  1429. /* Found matching mask */
  1430. debug("Found matching mask %d\n", match_mask);
  1431. node = child;
  1432. cell = fdt_getprop(blob, node, "reg", &len);
  1433. if (!cell) {
  1434. debug("No memory-banks property found\n");
  1435. return -EINVAL;
  1436. }
  1437. break;
  1438. }
  1439. }
  1440. /* Note: if no matching subnode was found we use the parent node */
  1441. if (bd) {
  1442. memset(bd->bi_dram, '\0', sizeof(bd->bi_dram[0]) *
  1443. CONFIG_NR_DRAM_BANKS);
  1444. }
  1445. auto_size = fdtdec_get_bool(blob, node, "auto-size");
  1446. total_size = 0;
  1447. end = cell + len / 4 - addr_cells - size_cells;
  1448. debug("cell at %p, end %p\n", cell, end);
  1449. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  1450. if (cell > end)
  1451. break;
  1452. addr = 0;
  1453. if (addr_cells == 2)
  1454. addr += (u64)fdt32_to_cpu(*cell++) << 32UL;
  1455. addr += fdt32_to_cpu(*cell++);
  1456. if (bd)
  1457. bd->bi_dram[bank].start = addr;
  1458. if (basep && !bank)
  1459. *basep = (phys_addr_t)addr;
  1460. size = 0;
  1461. if (size_cells == 2)
  1462. size += (u64)fdt32_to_cpu(*cell++) << 32UL;
  1463. size += fdt32_to_cpu(*cell++);
  1464. if (auto_size) {
  1465. u64 new_size;
  1466. debug("Auto-sizing %llx, size %llx: ", addr, size);
  1467. new_size = get_ram_size((long *)(uintptr_t)addr, size);
  1468. if (new_size == size) {
  1469. debug("OK\n");
  1470. } else {
  1471. debug("sized to %llx\n", new_size);
  1472. size = new_size;
  1473. }
  1474. }
  1475. if (bd)
  1476. bd->bi_dram[bank].size = size;
  1477. total_size += size;
  1478. }
  1479. debug("Memory size %llu\n", total_size);
  1480. if (sizep)
  1481. *sizep = (phys_size_t)total_size;
  1482. return 0;
  1483. }
  1484. #endif /* !USE_HOSTCC */