fdtdec.c 30 KB

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