fdtdec.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  4. */
  5. #ifndef USE_HOSTCC
  6. #include <boot_fit.h>
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <serial.h>
  11. #include <libfdt.h>
  12. #include <fdt_support.h>
  13. #include <fdtdec.h>
  14. #include <asm/sections.h>
  15. #include <linux/ctype.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. /*
  18. * Here are the type we know about. One day we might allow drivers to
  19. * register. For now we just put them here. The COMPAT macro allows us to
  20. * turn this into a sparse list later, and keeps the ID with the name.
  21. *
  22. * NOTE: This list is basically a TODO list for things that need to be
  23. * converted to driver model. So don't add new things here unless there is a
  24. * good reason why driver-model conversion is infeasible. Examples include
  25. * things which are used before driver model is available.
  26. */
  27. #define COMPAT(id, name) name
  28. static const char * const compat_names[COMPAT_COUNT] = {
  29. COMPAT(UNKNOWN, "<none>"),
  30. COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
  31. COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
  32. COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
  33. COMPAT(NVIDIA_TEGRA124_PMC, "nvidia,tegra124-pmc"),
  34. COMPAT(NVIDIA_TEGRA186_SDMMC, "nvidia,tegra186-sdhci"),
  35. COMPAT(NVIDIA_TEGRA210_SDMMC, "nvidia,tegra210-sdhci"),
  36. COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
  37. COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
  38. COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
  39. COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
  40. COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
  41. COMPAT(SMSC_LAN9215, "smsc,lan9215"),
  42. COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
  43. COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
  44. COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
  45. COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
  46. COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
  47. COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
  48. COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
  49. COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
  50. COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
  51. COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
  52. COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686"),
  53. COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
  54. COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
  55. COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
  56. COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
  57. COMPAT(INTEL_MICROCODE, "intel,microcode"),
  58. COMPAT(AMS_AS3722, "ams,as3722"),
  59. COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
  60. COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
  61. COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"),
  62. COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"),
  63. COMPAT(INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
  64. COMPAT(INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
  65. COMPAT(INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"),
  66. COMPAT(COMPAT_SUNXI_NAND, "allwinner,sun4i-a10-nand"),
  67. };
  68. const char *fdtdec_get_compatible(enum fdt_compat_id id)
  69. {
  70. /* We allow reading of the 'unknown' ID for testing purposes */
  71. assert(id >= 0 && id < COMPAT_COUNT);
  72. return compat_names[id];
  73. }
  74. fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
  75. const char *prop_name, int index, int na, int ns,
  76. fdt_size_t *sizep, bool translate)
  77. {
  78. const fdt32_t *prop, *prop_end;
  79. const fdt32_t *prop_addr, *prop_size, *prop_after_size;
  80. int len;
  81. fdt_addr_t addr;
  82. debug("%s: %s: ", __func__, prop_name);
  83. if (na > (sizeof(fdt_addr_t) / sizeof(fdt32_t))) {
  84. debug("(na too large for fdt_addr_t type)\n");
  85. return FDT_ADDR_T_NONE;
  86. }
  87. if (ns > (sizeof(fdt_size_t) / sizeof(fdt32_t))) {
  88. debug("(ns too large for fdt_size_t type)\n");
  89. return FDT_ADDR_T_NONE;
  90. }
  91. prop = fdt_getprop(blob, node, prop_name, &len);
  92. if (!prop) {
  93. debug("(not found)\n");
  94. return FDT_ADDR_T_NONE;
  95. }
  96. prop_end = prop + (len / sizeof(*prop));
  97. prop_addr = prop + (index * (na + ns));
  98. prop_size = prop_addr + na;
  99. prop_after_size = prop_size + ns;
  100. if (prop_after_size > prop_end) {
  101. debug("(not enough data: expected >= %d cells, got %d cells)\n",
  102. (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
  103. return FDT_ADDR_T_NONE;
  104. }
  105. #if CONFIG_IS_ENABLED(OF_LIBFDT)
  106. if (translate)
  107. addr = fdt_translate_address(blob, node, prop_addr);
  108. else
  109. #endif
  110. addr = fdtdec_get_number(prop_addr, na);
  111. if (sizep) {
  112. *sizep = fdtdec_get_number(prop_size, ns);
  113. debug("addr=%08llx, size=%llx\n", (unsigned long long)addr,
  114. (unsigned long long)*sizep);
  115. } else {
  116. debug("addr=%08llx\n", (unsigned long long)addr);
  117. }
  118. return addr;
  119. }
  120. fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
  121. int node, const char *prop_name, int index, fdt_size_t *sizep,
  122. bool translate)
  123. {
  124. int na, ns;
  125. debug("%s: ", __func__);
  126. na = fdt_address_cells(blob, parent);
  127. if (na < 1) {
  128. debug("(bad #address-cells)\n");
  129. return FDT_ADDR_T_NONE;
  130. }
  131. ns = fdt_size_cells(blob, parent);
  132. if (ns < 0) {
  133. debug("(bad #size-cells)\n");
  134. return FDT_ADDR_T_NONE;
  135. }
  136. debug("na=%d, ns=%d, ", na, ns);
  137. return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na,
  138. ns, sizep, translate);
  139. }
  140. fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
  141. const char *prop_name, int index, 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,
  163. const char *prop_name)
  164. {
  165. return fdtdec_get_addr_size(blob, node, prop_name, NULL);
  166. }
  167. #if defined(CONFIG_PCI) && defined(CONFIG_DM_PCI)
  168. int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
  169. const char *prop_name, struct fdt_pci_addr *addr)
  170. {
  171. const u32 *cell;
  172. int len;
  173. int ret = -ENOENT;
  174. debug("%s: %s: ", __func__, prop_name);
  175. /*
  176. * If we follow the pci bus bindings strictly, we should check
  177. * the value of the node's parent node's #address-cells and
  178. * #size-cells. They need to be 3 and 2 accordingly. However,
  179. * for simplicity we skip the check here.
  180. */
  181. cell = fdt_getprop(blob, node, prop_name, &len);
  182. if (!cell)
  183. goto fail;
  184. if ((len % FDT_PCI_REG_SIZE) == 0) {
  185. int num = len / FDT_PCI_REG_SIZE;
  186. int i;
  187. for (i = 0; i < num; i++) {
  188. debug("pci address #%d: %08lx %08lx %08lx\n", i,
  189. (ulong)fdt32_to_cpu(cell[0]),
  190. (ulong)fdt32_to_cpu(cell[1]),
  191. (ulong)fdt32_to_cpu(cell[2]));
  192. if ((fdt32_to_cpu(*cell) & type) == type) {
  193. addr->phys_hi = fdt32_to_cpu(cell[0]);
  194. addr->phys_mid = fdt32_to_cpu(cell[1]);
  195. addr->phys_lo = fdt32_to_cpu(cell[1]);
  196. break;
  197. } else {
  198. cell += (FDT_PCI_ADDR_CELLS +
  199. FDT_PCI_SIZE_CELLS);
  200. }
  201. }
  202. if (i == num) {
  203. ret = -ENXIO;
  204. goto fail;
  205. }
  206. return 0;
  207. } else {
  208. ret = -EINVAL;
  209. }
  210. fail:
  211. debug("(not found)\n");
  212. return ret;
  213. }
  214. int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
  215. {
  216. const char *list, *end;
  217. int len;
  218. list = fdt_getprop(blob, node, "compatible", &len);
  219. if (!list)
  220. return -ENOENT;
  221. end = list + len;
  222. while (list < end) {
  223. char *s;
  224. len = strlen(list);
  225. if (len >= strlen("pciVVVV,DDDD")) {
  226. s = strstr(list, "pci");
  227. /*
  228. * check if the string is something like pciVVVV,DDDD.RR
  229. * or just pciVVVV,DDDD
  230. */
  231. if (s && s[7] == ',' &&
  232. (s[12] == '.' || s[12] == 0)) {
  233. s += 3;
  234. *vendor = simple_strtol(s, NULL, 16);
  235. s += 5;
  236. *device = simple_strtol(s, NULL, 16);
  237. return 0;
  238. }
  239. }
  240. list += (len + 1);
  241. }
  242. return -ENOENT;
  243. }
  244. int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
  245. u32 *bar)
  246. {
  247. int barnum;
  248. /* extract the bar number from fdt_pci_addr */
  249. barnum = addr->phys_hi & 0xff;
  250. if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
  251. return -EINVAL;
  252. barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
  253. *bar = dm_pci_read_bar32(dev, barnum);
  254. return 0;
  255. }
  256. #endif
  257. uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
  258. uint64_t default_val)
  259. {
  260. const uint64_t *cell64;
  261. int length;
  262. cell64 = fdt_getprop(blob, node, prop_name, &length);
  263. if (!cell64 || length < sizeof(*cell64))
  264. return default_val;
  265. return fdt64_to_cpu(*cell64);
  266. }
  267. int fdtdec_get_is_enabled(const void *blob, int node)
  268. {
  269. const char *cell;
  270. /*
  271. * It should say "okay", so only allow that. Some fdts use "ok" but
  272. * this is a bug. Please fix your device tree source file. See here
  273. * for discussion:
  274. *
  275. * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
  276. */
  277. cell = fdt_getprop(blob, node, "status", NULL);
  278. if (cell)
  279. return 0 == strcmp(cell, "okay");
  280. return 1;
  281. }
  282. enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
  283. {
  284. enum fdt_compat_id id;
  285. /* Search our drivers */
  286. for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
  287. if (0 == fdt_node_check_compatible(blob, node,
  288. compat_names[id]))
  289. return id;
  290. return COMPAT_UNKNOWN;
  291. }
  292. int fdtdec_next_compatible(const void *blob, int node,
  293. enum fdt_compat_id id)
  294. {
  295. return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
  296. }
  297. int fdtdec_next_compatible_subnode(const void *blob, int node,
  298. enum fdt_compat_id id, int *depthp)
  299. {
  300. do {
  301. node = fdt_next_node(blob, node, depthp);
  302. } while (*depthp > 1);
  303. /* If this is a direct subnode, and compatible, return it */
  304. if (*depthp == 1 && 0 == fdt_node_check_compatible(
  305. blob, node, compat_names[id]))
  306. return node;
  307. return -FDT_ERR_NOTFOUND;
  308. }
  309. int fdtdec_next_alias(const void *blob, const char *name,
  310. enum fdt_compat_id id, int *upto)
  311. {
  312. #define MAX_STR_LEN 20
  313. char str[MAX_STR_LEN + 20];
  314. int node, err;
  315. /* snprintf() is not available */
  316. assert(strlen(name) < MAX_STR_LEN);
  317. sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
  318. node = fdt_path_offset(blob, str);
  319. if (node < 0)
  320. return node;
  321. err = fdt_node_check_compatible(blob, node, compat_names[id]);
  322. if (err < 0)
  323. return err;
  324. if (err)
  325. return -FDT_ERR_NOTFOUND;
  326. (*upto)++;
  327. return node;
  328. }
  329. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  330. enum fdt_compat_id id, int *node_list, int maxcount)
  331. {
  332. memset(node_list, '\0', sizeof(*node_list) * maxcount);
  333. return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
  334. }
  335. /* TODO: Can we tighten this code up a little? */
  336. int fdtdec_add_aliases_for_id(const void *blob, const char *name,
  337. enum fdt_compat_id id, int *node_list, int maxcount)
  338. {
  339. int name_len = strlen(name);
  340. int nodes[maxcount];
  341. int num_found = 0;
  342. int offset, node;
  343. int alias_node;
  344. int count;
  345. int i, j;
  346. /* find the alias node if present */
  347. alias_node = fdt_path_offset(blob, "/aliases");
  348. /*
  349. * start with nothing, and we can assume that the root node can't
  350. * match
  351. */
  352. memset(nodes, '\0', sizeof(nodes));
  353. /* First find all the compatible nodes */
  354. for (node = count = 0; node >= 0 && count < maxcount;) {
  355. node = fdtdec_next_compatible(blob, node, id);
  356. if (node >= 0)
  357. nodes[count++] = node;
  358. }
  359. if (node >= 0)
  360. debug("%s: warning: maxcount exceeded with alias '%s'\n",
  361. __func__, name);
  362. /* Now find all the aliases */
  363. for (offset = fdt_first_property_offset(blob, alias_node);
  364. offset > 0;
  365. offset = fdt_next_property_offset(blob, offset)) {
  366. const struct fdt_property *prop;
  367. const char *path;
  368. int number;
  369. int found;
  370. node = 0;
  371. prop = fdt_get_property_by_offset(blob, offset, NULL);
  372. path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  373. if (prop->len && 0 == strncmp(path, name, name_len))
  374. node = fdt_path_offset(blob, prop->data);
  375. if (node <= 0)
  376. continue;
  377. /* Get the alias number */
  378. number = simple_strtoul(path + name_len, NULL, 10);
  379. if (number < 0 || number >= maxcount) {
  380. debug("%s: warning: alias '%s' is out of range\n",
  381. __func__, path);
  382. continue;
  383. }
  384. /* Make sure the node we found is actually in our list! */
  385. found = -1;
  386. for (j = 0; j < count; j++)
  387. if (nodes[j] == node) {
  388. found = j;
  389. break;
  390. }
  391. if (found == -1) {
  392. debug("%s: warning: alias '%s' points to a node "
  393. "'%s' that is missing or is not compatible "
  394. " with '%s'\n", __func__, path,
  395. fdt_get_name(blob, node, NULL),
  396. compat_names[id]);
  397. continue;
  398. }
  399. /*
  400. * Add this node to our list in the right place, and mark
  401. * it as done.
  402. */
  403. if (fdtdec_get_is_enabled(blob, node)) {
  404. if (node_list[number]) {
  405. debug("%s: warning: alias '%s' requires that "
  406. "a node be placed in the list in a "
  407. "position which is already filled by "
  408. "node '%s'\n", __func__, path,
  409. fdt_get_name(blob, node, NULL));
  410. continue;
  411. }
  412. node_list[number] = node;
  413. if (number >= num_found)
  414. num_found = number + 1;
  415. }
  416. nodes[found] = 0;
  417. }
  418. /* Add any nodes not mentioned by an alias */
  419. for (i = j = 0; i < maxcount; i++) {
  420. if (!node_list[i]) {
  421. for (; j < maxcount; j++)
  422. if (nodes[j] &&
  423. fdtdec_get_is_enabled(blob, nodes[j]))
  424. break;
  425. /* Have we run out of nodes to add? */
  426. if (j == maxcount)
  427. break;
  428. assert(!node_list[i]);
  429. node_list[i] = nodes[j++];
  430. if (i >= num_found)
  431. num_found = i + 1;
  432. }
  433. }
  434. return num_found;
  435. }
  436. int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
  437. int *seqp)
  438. {
  439. int base_len = strlen(base);
  440. const char *find_name;
  441. int find_namelen;
  442. int prop_offset;
  443. int aliases;
  444. find_name = fdt_get_name(blob, offset, &find_namelen);
  445. debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
  446. aliases = fdt_path_offset(blob, "/aliases");
  447. for (prop_offset = fdt_first_property_offset(blob, aliases);
  448. prop_offset > 0;
  449. prop_offset = fdt_next_property_offset(blob, prop_offset)) {
  450. const char *prop;
  451. const char *name;
  452. const char *slash;
  453. int len, val;
  454. prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
  455. debug(" - %s, %s\n", name, prop);
  456. if (len < find_namelen || *prop != '/' || prop[len - 1] ||
  457. strncmp(name, base, base_len))
  458. continue;
  459. slash = strrchr(prop, '/');
  460. if (strcmp(slash + 1, find_name))
  461. continue;
  462. val = trailing_strtol(name);
  463. if (val != -1) {
  464. *seqp = val;
  465. debug("Found seq %d\n", *seqp);
  466. return 0;
  467. }
  468. }
  469. debug("Not found\n");
  470. return -ENOENT;
  471. }
  472. const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
  473. {
  474. int chosen_node;
  475. if (!blob)
  476. return NULL;
  477. chosen_node = fdt_path_offset(blob, "/chosen");
  478. return fdt_getprop(blob, chosen_node, name, NULL);
  479. }
  480. int fdtdec_get_chosen_node(const void *blob, const char *name)
  481. {
  482. const char *prop;
  483. prop = fdtdec_get_chosen_prop(blob, name);
  484. if (!prop)
  485. return -FDT_ERR_NOTFOUND;
  486. return fdt_path_offset(blob, prop);
  487. }
  488. int fdtdec_check_fdt(void)
  489. {
  490. /*
  491. * We must have an FDT, but we cannot panic() yet since the console
  492. * is not ready. So for now, just assert(). Boards which need an early
  493. * FDT (prior to console ready) will need to make their own
  494. * arrangements and do their own checks.
  495. */
  496. assert(!fdtdec_prepare_fdt());
  497. return 0;
  498. }
  499. /*
  500. * This function is a little odd in that it accesses global data. At some
  501. * point if the architecture board.c files merge this will make more sense.
  502. * Even now, it is common code.
  503. */
  504. int fdtdec_prepare_fdt(void)
  505. {
  506. if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
  507. fdt_check_header(gd->fdt_blob)) {
  508. #ifdef CONFIG_SPL_BUILD
  509. puts("Missing DTB\n");
  510. #else
  511. 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");
  512. # ifdef DEBUG
  513. if (gd->fdt_blob) {
  514. printf("fdt_blob=%p\n", gd->fdt_blob);
  515. print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4,
  516. 32, 0);
  517. }
  518. # endif
  519. #endif
  520. return -1;
  521. }
  522. return 0;
  523. }
  524. int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
  525. {
  526. const u32 *phandle;
  527. int lookup;
  528. debug("%s: %s\n", __func__, prop_name);
  529. phandle = fdt_getprop(blob, node, prop_name, NULL);
  530. if (!phandle)
  531. return -FDT_ERR_NOTFOUND;
  532. lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
  533. return lookup;
  534. }
  535. /**
  536. * Look up a property in a node and check that it has a minimum length.
  537. *
  538. * @param blob FDT blob
  539. * @param node node to examine
  540. * @param prop_name name of property to find
  541. * @param min_len minimum property length in bytes
  542. * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
  543. found, or -FDT_ERR_BADLAYOUT if not enough data
  544. * @return pointer to cell, which is only valid if err == 0
  545. */
  546. static const void *get_prop_check_min_len(const void *blob, int node,
  547. const char *prop_name, int min_len, int *err)
  548. {
  549. const void *cell;
  550. int len;
  551. debug("%s: %s\n", __func__, prop_name);
  552. cell = fdt_getprop(blob, node, prop_name, &len);
  553. if (!cell)
  554. *err = -FDT_ERR_NOTFOUND;
  555. else if (len < min_len)
  556. *err = -FDT_ERR_BADLAYOUT;
  557. else
  558. *err = 0;
  559. return cell;
  560. }
  561. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  562. u32 *array, int count)
  563. {
  564. const u32 *cell;
  565. int i, err = 0;
  566. debug("%s: %s\n", __func__, prop_name);
  567. cell = get_prop_check_min_len(blob, node, prop_name,
  568. sizeof(u32) * count, &err);
  569. if (!err) {
  570. for (i = 0; i < count; i++)
  571. array[i] = fdt32_to_cpu(cell[i]);
  572. }
  573. return err;
  574. }
  575. int fdtdec_get_int_array_count(const void *blob, int node,
  576. const char *prop_name, u32 *array, int count)
  577. {
  578. const u32 *cell;
  579. int len, elems;
  580. int i;
  581. debug("%s: %s\n", __func__, prop_name);
  582. cell = fdt_getprop(blob, node, prop_name, &len);
  583. if (!cell)
  584. return -FDT_ERR_NOTFOUND;
  585. elems = len / sizeof(u32);
  586. if (count > elems)
  587. count = elems;
  588. for (i = 0; i < count; i++)
  589. array[i] = fdt32_to_cpu(cell[i]);
  590. return count;
  591. }
  592. const u32 *fdtdec_locate_array(const void *blob, int node,
  593. const char *prop_name, int count)
  594. {
  595. const u32 *cell;
  596. int err;
  597. cell = get_prop_check_min_len(blob, node, prop_name,
  598. sizeof(u32) * count, &err);
  599. return err ? NULL : cell;
  600. }
  601. int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
  602. {
  603. const s32 *cell;
  604. int len;
  605. debug("%s: %s\n", __func__, prop_name);
  606. cell = fdt_getprop(blob, node, prop_name, &len);
  607. return cell != NULL;
  608. }
  609. int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
  610. const char *list_name,
  611. const char *cells_name,
  612. int cell_count, int index,
  613. struct fdtdec_phandle_args *out_args)
  614. {
  615. const __be32 *list, *list_end;
  616. int rc = 0, size, cur_index = 0;
  617. uint32_t count = 0;
  618. int node = -1;
  619. int phandle;
  620. /* Retrieve the phandle list property */
  621. list = fdt_getprop(blob, src_node, list_name, &size);
  622. if (!list)
  623. return -ENOENT;
  624. list_end = list + size / sizeof(*list);
  625. /* Loop over the phandles until all the requested entry is found */
  626. while (list < list_end) {
  627. rc = -EINVAL;
  628. count = 0;
  629. /*
  630. * If phandle is 0, then it is an empty entry with no
  631. * arguments. Skip forward to the next entry.
  632. */
  633. phandle = be32_to_cpup(list++);
  634. if (phandle) {
  635. /*
  636. * Find the provider node and parse the #*-cells
  637. * property to determine the argument length.
  638. *
  639. * This is not needed if the cell count is hard-coded
  640. * (i.e. cells_name not set, but cell_count is set),
  641. * except when we're going to return the found node
  642. * below.
  643. */
  644. if (cells_name || cur_index == index) {
  645. node = fdt_node_offset_by_phandle(blob,
  646. phandle);
  647. if (!node) {
  648. debug("%s: could not find phandle\n",
  649. fdt_get_name(blob, src_node,
  650. NULL));
  651. goto err;
  652. }
  653. }
  654. if (cells_name) {
  655. count = fdtdec_get_int(blob, node, cells_name,
  656. -1);
  657. if (count == -1) {
  658. debug("%s: could not get %s for %s\n",
  659. fdt_get_name(blob, src_node,
  660. NULL),
  661. cells_name,
  662. fdt_get_name(blob, node,
  663. NULL));
  664. goto err;
  665. }
  666. } else {
  667. count = cell_count;
  668. }
  669. /*
  670. * Make sure that the arguments actually fit in the
  671. * remaining property data length
  672. */
  673. if (list + count > list_end) {
  674. debug("%s: arguments longer than property\n",
  675. fdt_get_name(blob, src_node, NULL));
  676. goto err;
  677. }
  678. }
  679. /*
  680. * All of the error cases above bail out of the loop, so at
  681. * this point, the parsing is successful. If the requested
  682. * index matches, then fill the out_args structure and return,
  683. * or return -ENOENT for an empty entry.
  684. */
  685. rc = -ENOENT;
  686. if (cur_index == index) {
  687. if (!phandle)
  688. goto err;
  689. if (out_args) {
  690. int i;
  691. if (count > MAX_PHANDLE_ARGS) {
  692. debug("%s: too many arguments %d\n",
  693. fdt_get_name(blob, src_node,
  694. NULL), count);
  695. count = MAX_PHANDLE_ARGS;
  696. }
  697. out_args->node = node;
  698. out_args->args_count = count;
  699. for (i = 0; i < count; i++) {
  700. out_args->args[i] =
  701. be32_to_cpup(list++);
  702. }
  703. }
  704. /* Found it! return success */
  705. return 0;
  706. }
  707. node = -1;
  708. list += count;
  709. cur_index++;
  710. }
  711. /*
  712. * Result will be one of:
  713. * -ENOENT : index is for empty phandle
  714. * -EINVAL : parsing error on data
  715. * [1..n] : Number of phandle (count mode; when index = -1)
  716. */
  717. rc = index < 0 ? cur_index : -ENOENT;
  718. err:
  719. return rc;
  720. }
  721. int fdtdec_get_child_count(const void *blob, int node)
  722. {
  723. int subnode;
  724. int num = 0;
  725. fdt_for_each_subnode(subnode, blob, node)
  726. num++;
  727. return num;
  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. int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
  785. fdt_addr_t *basep, fdt_size_t *sizep)
  786. {
  787. const fdt_addr_t *cell;
  788. int len;
  789. debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
  790. prop_name);
  791. cell = fdt_getprop(blob, node, prop_name, &len);
  792. if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
  793. debug("cell=%p, len=%d\n", cell, len);
  794. return -1;
  795. }
  796. *basep = fdt_addr_to_cpu(*cell);
  797. *sizep = fdt_size_to_cpu(cell[1]);
  798. debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
  799. (ulong)*sizep);
  800. return 0;
  801. }
  802. /**
  803. * Read a flash entry from the fdt
  804. *
  805. * @param blob FDT blob
  806. * @param node Offset of node to read
  807. * @param name Name of node being read
  808. * @param entry Place to put offset and size of this node
  809. * @return 0 if ok, -ve on error
  810. */
  811. int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
  812. struct fmap_entry *entry)
  813. {
  814. const char *prop;
  815. u32 reg[2];
  816. if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
  817. debug("Node '%s' has bad/missing 'reg' property\n", name);
  818. return -FDT_ERR_NOTFOUND;
  819. }
  820. entry->offset = reg[0];
  821. entry->length = reg[1];
  822. entry->used = fdtdec_get_int(blob, node, "used", entry->length);
  823. prop = fdt_getprop(blob, node, "compress", NULL);
  824. entry->compress_algo = prop && !strcmp(prop, "lzo") ?
  825. FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
  826. prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
  827. entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
  828. entry->hash = (uint8_t *)prop;
  829. return 0;
  830. }
  831. u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
  832. {
  833. u64 number = 0;
  834. while (cells--)
  835. number = (number << 32) | fdt32_to_cpu(*ptr++);
  836. return number;
  837. }
  838. int fdt_get_resource(const void *fdt, int node, const char *property,
  839. unsigned int index, struct fdt_resource *res)
  840. {
  841. const fdt32_t *ptr, *end;
  842. int na, ns, len, parent;
  843. unsigned int i = 0;
  844. parent = fdt_parent_offset(fdt, node);
  845. if (parent < 0)
  846. return parent;
  847. na = fdt_address_cells(fdt, parent);
  848. ns = fdt_size_cells(fdt, parent);
  849. ptr = fdt_getprop(fdt, node, property, &len);
  850. if (!ptr)
  851. return len;
  852. end = ptr + len / sizeof(*ptr);
  853. while (ptr + na + ns <= end) {
  854. if (i == index) {
  855. res->start = res->end = fdtdec_get_number(ptr, na);
  856. res->end += fdtdec_get_number(&ptr[na], ns) - 1;
  857. return 0;
  858. }
  859. ptr += na + ns;
  860. i++;
  861. }
  862. return -FDT_ERR_NOTFOUND;
  863. }
  864. int fdt_get_named_resource(const void *fdt, int node, const char *property,
  865. const char *prop_names, const char *name,
  866. struct fdt_resource *res)
  867. {
  868. int index;
  869. index = fdt_stringlist_search(fdt, node, prop_names, name);
  870. if (index < 0)
  871. return index;
  872. return fdt_get_resource(fdt, node, property, index, res);
  873. }
  874. int fdtdec_decode_memory_region(const void *blob, int config_node,
  875. const char *mem_type, const char *suffix,
  876. fdt_addr_t *basep, fdt_size_t *sizep)
  877. {
  878. char prop_name[50];
  879. const char *mem;
  880. fdt_size_t size, offset_size;
  881. fdt_addr_t base, offset;
  882. int node;
  883. if (config_node == -1) {
  884. config_node = fdt_path_offset(blob, "/config");
  885. if (config_node < 0) {
  886. debug("%s: Cannot find /config node\n", __func__);
  887. return -ENOENT;
  888. }
  889. }
  890. if (!suffix)
  891. suffix = "";
  892. snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
  893. suffix);
  894. mem = fdt_getprop(blob, config_node, prop_name, NULL);
  895. if (!mem) {
  896. debug("%s: No memory type for '%s', using /memory\n", __func__,
  897. prop_name);
  898. mem = "/memory";
  899. }
  900. node = fdt_path_offset(blob, mem);
  901. if (node < 0) {
  902. debug("%s: Failed to find node '%s': %s\n", __func__, mem,
  903. fdt_strerror(node));
  904. return -ENOENT;
  905. }
  906. /*
  907. * Not strictly correct - the memory may have multiple banks. We just
  908. * use the first
  909. */
  910. if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
  911. debug("%s: Failed to decode memory region %s\n", __func__,
  912. mem);
  913. return -EINVAL;
  914. }
  915. snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
  916. suffix);
  917. if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
  918. &offset_size)) {
  919. debug("%s: Failed to decode memory region '%s'\n", __func__,
  920. prop_name);
  921. return -EINVAL;
  922. }
  923. *basep = base + offset;
  924. *sizep = offset_size;
  925. return 0;
  926. }
  927. static int decode_timing_property(const void *blob, int node, const char *name,
  928. struct timing_entry *result)
  929. {
  930. int length, ret = 0;
  931. const u32 *prop;
  932. prop = fdt_getprop(blob, node, name, &length);
  933. if (!prop) {
  934. debug("%s: could not find property %s\n",
  935. fdt_get_name(blob, node, NULL), name);
  936. return length;
  937. }
  938. if (length == sizeof(u32)) {
  939. result->typ = fdtdec_get_int(blob, node, name, 0);
  940. result->min = result->typ;
  941. result->max = result->typ;
  942. } else {
  943. ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
  944. }
  945. return ret;
  946. }
  947. int fdtdec_decode_display_timing(const void *blob, int parent, int index,
  948. struct display_timing *dt)
  949. {
  950. int i, node, timings_node;
  951. u32 val = 0;
  952. int ret = 0;
  953. timings_node = fdt_subnode_offset(blob, parent, "display-timings");
  954. if (timings_node < 0)
  955. return timings_node;
  956. for (i = 0, node = fdt_first_subnode(blob, timings_node);
  957. node > 0 && i != index;
  958. node = fdt_next_subnode(blob, node))
  959. i++;
  960. if (node < 0)
  961. return node;
  962. memset(dt, 0, sizeof(*dt));
  963. ret |= decode_timing_property(blob, node, "hback-porch",
  964. &dt->hback_porch);
  965. ret |= decode_timing_property(blob, node, "hfront-porch",
  966. &dt->hfront_porch);
  967. ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
  968. ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
  969. ret |= decode_timing_property(blob, node, "vback-porch",
  970. &dt->vback_porch);
  971. ret |= decode_timing_property(blob, node, "vfront-porch",
  972. &dt->vfront_porch);
  973. ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
  974. ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
  975. ret |= decode_timing_property(blob, node, "clock-frequency",
  976. &dt->pixelclock);
  977. dt->flags = 0;
  978. val = fdtdec_get_int(blob, node, "vsync-active", -1);
  979. if (val != -1) {
  980. dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
  981. DISPLAY_FLAGS_VSYNC_LOW;
  982. }
  983. val = fdtdec_get_int(blob, node, "hsync-active", -1);
  984. if (val != -1) {
  985. dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
  986. DISPLAY_FLAGS_HSYNC_LOW;
  987. }
  988. val = fdtdec_get_int(blob, node, "de-active", -1);
  989. if (val != -1) {
  990. dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
  991. DISPLAY_FLAGS_DE_LOW;
  992. }
  993. val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
  994. if (val != -1) {
  995. dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
  996. DISPLAY_FLAGS_PIXDATA_NEGEDGE;
  997. }
  998. if (fdtdec_get_bool(blob, node, "interlaced"))
  999. dt->flags |= DISPLAY_FLAGS_INTERLACED;
  1000. if (fdtdec_get_bool(blob, node, "doublescan"))
  1001. dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
  1002. if (fdtdec_get_bool(blob, node, "doubleclk"))
  1003. dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
  1004. return ret;
  1005. }
  1006. int fdtdec_setup_memory_size(void)
  1007. {
  1008. int ret, mem;
  1009. struct fdt_resource res;
  1010. mem = fdt_path_offset(gd->fdt_blob, "/memory");
  1011. if (mem < 0) {
  1012. debug("%s: Missing /memory node\n", __func__);
  1013. return -EINVAL;
  1014. }
  1015. ret = fdt_get_resource(gd->fdt_blob, mem, "reg", 0, &res);
  1016. if (ret != 0) {
  1017. debug("%s: Unable to decode first memory bank\n", __func__);
  1018. return -EINVAL;
  1019. }
  1020. gd->ram_size = (phys_size_t)(res.end - res.start + 1);
  1021. debug("%s: Initial DRAM size %llx\n", __func__, (u64)gd->ram_size);
  1022. return 0;
  1023. }
  1024. #if defined(CONFIG_NR_DRAM_BANKS)
  1025. int fdtdec_setup_memory_banksize(void)
  1026. {
  1027. int bank, ret, mem;
  1028. struct fdt_resource res;
  1029. mem = fdt_path_offset(gd->fdt_blob, "/memory");
  1030. if (mem < 0) {
  1031. debug("%s: Missing /memory node\n", __func__);
  1032. return -EINVAL;
  1033. }
  1034. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  1035. ret = fdt_get_resource(gd->fdt_blob, mem, "reg", bank, &res);
  1036. if (ret == -FDT_ERR_NOTFOUND)
  1037. break;
  1038. if (ret != 0)
  1039. return -EINVAL;
  1040. gd->bd->bi_dram[bank].start = (phys_addr_t)res.start;
  1041. gd->bd->bi_dram[bank].size =
  1042. (phys_size_t)(res.end - res.start + 1);
  1043. debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n",
  1044. __func__, bank,
  1045. (unsigned long long)gd->bd->bi_dram[bank].start,
  1046. (unsigned long long)gd->bd->bi_dram[bank].size);
  1047. }
  1048. return 0;
  1049. }
  1050. #endif
  1051. int fdtdec_setup(void)
  1052. {
  1053. #if CONFIG_IS_ENABLED(OF_CONTROL)
  1054. # ifdef CONFIG_OF_EMBED
  1055. /* Get a pointer to the FDT */
  1056. gd->fdt_blob = __dtb_dt_begin;
  1057. # elif defined CONFIG_OF_SEPARATE
  1058. # ifdef CONFIG_SPL_BUILD
  1059. /* FDT is at end of BSS unless it is in a different memory region */
  1060. if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
  1061. gd->fdt_blob = (ulong *)&_image_binary_end;
  1062. else
  1063. gd->fdt_blob = (ulong *)&__bss_end;
  1064. # elif defined CONFIG_FIT_EMBED
  1065. gd->fdt_blob = locate_dtb_in_fit(&_end);
  1066. if (gd->fdt_blob == NULL || gd->fdt_blob <= ((void *)&_end)) {
  1067. puts("Failed to find proper dtb in embedded FIT Image\n");
  1068. return -1;
  1069. }
  1070. # else
  1071. /* FDT is at end of image */
  1072. gd->fdt_blob = (ulong *)&_end;
  1073. # endif
  1074. # elif defined(CONFIG_OF_HOSTFILE)
  1075. if (sandbox_read_fdt_from_file()) {
  1076. puts("Failed to read control FDT\n");
  1077. return -1;
  1078. }
  1079. # endif
  1080. # ifndef CONFIG_SPL_BUILD
  1081. /* Allow the early environment to override the fdt address */
  1082. gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
  1083. (uintptr_t)gd->fdt_blob);
  1084. # endif
  1085. #endif
  1086. return fdtdec_prepare_fdt();
  1087. }
  1088. #endif /* !USE_HOSTCC */