fdtdec.c 41 KB

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