fdtdec.c 42 KB

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