fdtdec.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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(SOCIONEXT_XHCI, "socionext,uniphier-xhci"),
  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 !defined(CONFIG_SPL_BUILD) && defined(CONFIG_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(blob, subnode, 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_find_string(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(void)
  1007. {
  1008. #if CONFIG_IS_ENABLED(OF_CONTROL)
  1009. # ifdef CONFIG_OF_EMBED
  1010. /* Get a pointer to the FDT */
  1011. gd->fdt_blob = __dtb_dt_begin;
  1012. # elif defined CONFIG_OF_SEPARATE
  1013. # ifdef CONFIG_SPL_BUILD
  1014. /* FDT is at end of BSS unless it is in a different memory region */
  1015. if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
  1016. gd->fdt_blob = (ulong *)&_image_binary_end;
  1017. else
  1018. gd->fdt_blob = (ulong *)&__bss_end;
  1019. # else
  1020. /* FDT is at end of image */
  1021. gd->fdt_blob = (ulong *)&_end;
  1022. # endif
  1023. # elif defined(CONFIG_OF_HOSTFILE)
  1024. if (sandbox_read_fdt_from_file()) {
  1025. puts("Failed to read control FDT\n");
  1026. return -1;
  1027. }
  1028. # endif
  1029. # ifndef CONFIG_SPL_BUILD
  1030. /* Allow the early environment to override the fdt address */
  1031. gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
  1032. (uintptr_t)gd->fdt_blob);
  1033. # endif
  1034. #endif
  1035. return fdtdec_prepare_fdt();
  1036. }
  1037. #endif /* !USE_HOSTCC */