fdtdec.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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 <fdtdec.h>
  12. #include <asm/sections.h>
  13. #include <linux/ctype.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. /*
  16. * Here are the type we know about. One day we might allow drivers to
  17. * register. For now we just put them here. The COMPAT macro allows us to
  18. * turn this into a sparse list later, and keeps the ID with the name.
  19. */
  20. #define COMPAT(id, name) name
  21. static const char * const compat_names[COMPAT_COUNT] = {
  22. COMPAT(UNKNOWN, "<none>"),
  23. COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
  24. COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
  25. COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
  26. COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
  27. COMPAT(NVIDIA_TEGRA124_DC, "nvidia,tegra124-dc"),
  28. COMPAT(NVIDIA_TEGRA124_SOR, "nvidia,tegra124-sor"),
  29. COMPAT(NVIDIA_TEGRA124_PMC, "nvidia,tegra124-pmc"),
  30. COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
  31. COMPAT(NVIDIA_TEGRA210_SDMMC, "nvidia,tegra210-sdhci"),
  32. COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
  33. COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
  34. COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
  35. COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
  36. COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
  37. COMPAT(SMSC_LAN9215, "smsc,lan9215"),
  38. COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
  39. COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
  40. COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
  41. COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
  42. COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
  43. COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
  44. COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
  45. COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
  46. COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
  47. COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
  48. COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
  49. COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
  50. COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
  51. COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
  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(MEMORY_SPD, "memory-spd"),
  59. COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"),
  60. COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"),
  61. COMPAT(INTEL_GMA, "intel,gma"),
  62. COMPAT(AMS_AS3722, "ams,as3722"),
  63. COMPAT(INTEL_ICH_SPI, "intel,ich-spi"),
  64. COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
  65. COMPAT(INTEL_X86_PINCTRL, "intel,x86-pinctrl"),
  66. COMPAT(SOCIONEXT_XHCI, "socionext,uniphier-xhci"),
  67. COMPAT(COMPAT_INTEL_PCH, "intel,bd82x6x"),
  68. COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
  69. COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"),
  70. COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"),
  71. COMPAT(COMPAT_INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
  72. COMPAT(COMPAT_INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
  73. COMPAT(COMPAT_INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"),
  74. };
  75. const char *fdtdec_get_compatible(enum fdt_compat_id id)
  76. {
  77. /* We allow reading of the 'unknown' ID for testing purposes */
  78. assert(id >= 0 && id < COMPAT_COUNT);
  79. return compat_names[id];
  80. }
  81. fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
  82. const char *prop_name, int index, int na, int ns,
  83. fdt_size_t *sizep)
  84. {
  85. const fdt32_t *prop, *prop_end;
  86. const fdt32_t *prop_addr, *prop_size, *prop_after_size;
  87. int len;
  88. fdt_addr_t addr;
  89. debug("%s: %s: ", __func__, prop_name);
  90. if (na > (sizeof(fdt_addr_t) / sizeof(fdt32_t))) {
  91. debug("(na too large for fdt_addr_t type)\n");
  92. return FDT_ADDR_T_NONE;
  93. }
  94. if (ns > (sizeof(fdt_size_t) / sizeof(fdt32_t))) {
  95. debug("(ns too large for fdt_size_t type)\n");
  96. return FDT_ADDR_T_NONE;
  97. }
  98. prop = fdt_getprop(blob, node, prop_name, &len);
  99. if (!prop) {
  100. debug("(not found)\n");
  101. return FDT_ADDR_T_NONE;
  102. }
  103. prop_end = prop + (len / sizeof(*prop));
  104. prop_addr = prop + (index * (na + ns));
  105. prop_size = prop_addr + na;
  106. prop_after_size = prop_size + ns;
  107. if (prop_after_size > prop_end) {
  108. debug("(not enough data: expected >= %d cells, got %d cells)\n",
  109. (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
  110. return FDT_ADDR_T_NONE;
  111. }
  112. addr = fdtdec_get_number(prop_addr, na);
  113. if (sizep) {
  114. *sizep = fdtdec_get_number(prop_size, ns);
  115. debug("addr=%08llx, size=%llx\n", (u64)addr, (u64)*sizep);
  116. } else {
  117. debug("addr=%08llx\n", (u64)addr);
  118. }
  119. return addr;
  120. }
  121. fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
  122. int node, const char *prop_name, int index, fdt_size_t *sizep)
  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);
  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. {
  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);
  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);
  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_byte_array(const void *blob, int node, const char *prop_name,
  721. u8 *array, int count)
  722. {
  723. const u8 *cell;
  724. int err;
  725. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  726. if (!err)
  727. memcpy(array, cell, count);
  728. return err;
  729. }
  730. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  731. const char *prop_name, int count)
  732. {
  733. const u8 *cell;
  734. int err;
  735. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  736. if (err)
  737. return NULL;
  738. return cell;
  739. }
  740. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  741. int default_val)
  742. {
  743. int config_node;
  744. debug("%s: %s\n", __func__, prop_name);
  745. config_node = fdt_path_offset(blob, "/config");
  746. if (config_node < 0)
  747. return default_val;
  748. return fdtdec_get_int(blob, config_node, prop_name, default_val);
  749. }
  750. int fdtdec_get_config_bool(const void *blob, const char *prop_name)
  751. {
  752. int config_node;
  753. const void *prop;
  754. debug("%s: %s\n", __func__, prop_name);
  755. config_node = fdt_path_offset(blob, "/config");
  756. if (config_node < 0)
  757. return 0;
  758. prop = fdt_get_property(blob, config_node, prop_name, NULL);
  759. return prop != NULL;
  760. }
  761. char *fdtdec_get_config_string(const void *blob, const char *prop_name)
  762. {
  763. const char *nodep;
  764. int nodeoffset;
  765. int len;
  766. debug("%s: %s\n", __func__, prop_name);
  767. nodeoffset = fdt_path_offset(blob, "/config");
  768. if (nodeoffset < 0)
  769. return NULL;
  770. nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
  771. if (!nodep)
  772. return NULL;
  773. return (char *)nodep;
  774. }
  775. int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
  776. fdt_addr_t *basep, fdt_size_t *sizep)
  777. {
  778. const fdt_addr_t *cell;
  779. int len;
  780. debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
  781. prop_name);
  782. cell = fdt_getprop(blob, node, prop_name, &len);
  783. if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
  784. debug("cell=%p, len=%d\n", cell, len);
  785. return -1;
  786. }
  787. *basep = fdt_addr_to_cpu(*cell);
  788. *sizep = fdt_size_to_cpu(cell[1]);
  789. debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
  790. (ulong)*sizep);
  791. return 0;
  792. }
  793. /**
  794. * Read a flash entry from the fdt
  795. *
  796. * @param blob FDT blob
  797. * @param node Offset of node to read
  798. * @param name Name of node being read
  799. * @param entry Place to put offset and size of this node
  800. * @return 0 if ok, -ve on error
  801. */
  802. int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
  803. struct fmap_entry *entry)
  804. {
  805. const char *prop;
  806. u32 reg[2];
  807. if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
  808. debug("Node '%s' has bad/missing 'reg' property\n", name);
  809. return -FDT_ERR_NOTFOUND;
  810. }
  811. entry->offset = reg[0];
  812. entry->length = reg[1];
  813. entry->used = fdtdec_get_int(blob, node, "used", entry->length);
  814. prop = fdt_getprop(blob, node, "compress", NULL);
  815. entry->compress_algo = prop && !strcmp(prop, "lzo") ?
  816. FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
  817. prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
  818. entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
  819. entry->hash = (uint8_t *)prop;
  820. return 0;
  821. }
  822. u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
  823. {
  824. u64 number = 0;
  825. while (cells--)
  826. number = (number << 32) | fdt32_to_cpu(*ptr++);
  827. return number;
  828. }
  829. int fdt_get_resource(const void *fdt, int node, const char *property,
  830. unsigned int index, struct fdt_resource *res)
  831. {
  832. const fdt32_t *ptr, *end;
  833. int na, ns, len, parent;
  834. unsigned int i = 0;
  835. parent = fdt_parent_offset(fdt, node);
  836. if (parent < 0)
  837. return parent;
  838. na = fdt_address_cells(fdt, parent);
  839. ns = fdt_size_cells(fdt, parent);
  840. ptr = fdt_getprop(fdt, node, property, &len);
  841. if (!ptr)
  842. return len;
  843. end = ptr + len / sizeof(*ptr);
  844. while (ptr + na + ns <= end) {
  845. if (i == index) {
  846. res->start = res->end = fdtdec_get_number(ptr, na);
  847. res->end += fdtdec_get_number(&ptr[na], ns) - 1;
  848. return 0;
  849. }
  850. ptr += na + ns;
  851. i++;
  852. }
  853. return -FDT_ERR_NOTFOUND;
  854. }
  855. int fdt_get_named_resource(const void *fdt, int node, const char *property,
  856. const char *prop_names, const char *name,
  857. struct fdt_resource *res)
  858. {
  859. int index;
  860. index = fdt_find_string(fdt, node, prop_names, name);
  861. if (index < 0)
  862. return index;
  863. return fdt_get_resource(fdt, node, property, index, res);
  864. }
  865. int fdtdec_decode_memory_region(const void *blob, int config_node,
  866. const char *mem_type, const char *suffix,
  867. fdt_addr_t *basep, fdt_size_t *sizep)
  868. {
  869. char prop_name[50];
  870. const char *mem;
  871. fdt_size_t size, offset_size;
  872. fdt_addr_t base, offset;
  873. int node;
  874. if (config_node == -1) {
  875. config_node = fdt_path_offset(blob, "/config");
  876. if (config_node < 0) {
  877. debug("%s: Cannot find /config node\n", __func__);
  878. return -ENOENT;
  879. }
  880. }
  881. if (!suffix)
  882. suffix = "";
  883. snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
  884. suffix);
  885. mem = fdt_getprop(blob, config_node, prop_name, NULL);
  886. if (!mem) {
  887. debug("%s: No memory type for '%s', using /memory\n", __func__,
  888. prop_name);
  889. mem = "/memory";
  890. }
  891. node = fdt_path_offset(blob, mem);
  892. if (node < 0) {
  893. debug("%s: Failed to find node '%s': %s\n", __func__, mem,
  894. fdt_strerror(node));
  895. return -ENOENT;
  896. }
  897. /*
  898. * Not strictly correct - the memory may have multiple banks. We just
  899. * use the first
  900. */
  901. if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
  902. debug("%s: Failed to decode memory region %s\n", __func__,
  903. mem);
  904. return -EINVAL;
  905. }
  906. snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
  907. suffix);
  908. if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
  909. &offset_size)) {
  910. debug("%s: Failed to decode memory region '%s'\n", __func__,
  911. prop_name);
  912. return -EINVAL;
  913. }
  914. *basep = base + offset;
  915. *sizep = offset_size;
  916. return 0;
  917. }
  918. static int decode_timing_property(const void *blob, int node, const char *name,
  919. struct timing_entry *result)
  920. {
  921. int length, ret = 0;
  922. const u32 *prop;
  923. prop = fdt_getprop(blob, node, name, &length);
  924. if (!prop) {
  925. debug("%s: could not find property %s\n",
  926. fdt_get_name(blob, node, NULL), name);
  927. return length;
  928. }
  929. if (length == sizeof(u32)) {
  930. result->typ = fdtdec_get_int(blob, node, name, 0);
  931. result->min = result->typ;
  932. result->max = result->typ;
  933. } else {
  934. ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
  935. }
  936. return ret;
  937. }
  938. int fdtdec_decode_display_timing(const void *blob, int parent, int index,
  939. struct display_timing *dt)
  940. {
  941. int i, node, timings_node;
  942. u32 val = 0;
  943. int ret = 0;
  944. timings_node = fdt_subnode_offset(blob, parent, "display-timings");
  945. if (timings_node < 0)
  946. return timings_node;
  947. for (i = 0, node = fdt_first_subnode(blob, timings_node);
  948. node > 0 && i != index;
  949. node = fdt_next_subnode(blob, node))
  950. i++;
  951. if (node < 0)
  952. return node;
  953. memset(dt, 0, sizeof(*dt));
  954. ret |= decode_timing_property(blob, node, "hback-porch",
  955. &dt->hback_porch);
  956. ret |= decode_timing_property(blob, node, "hfront-porch",
  957. &dt->hfront_porch);
  958. ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
  959. ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
  960. ret |= decode_timing_property(blob, node, "vback-porch",
  961. &dt->vback_porch);
  962. ret |= decode_timing_property(blob, node, "vfront-porch",
  963. &dt->vfront_porch);
  964. ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
  965. ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
  966. ret |= decode_timing_property(blob, node, "clock-frequency",
  967. &dt->pixelclock);
  968. dt->flags = 0;
  969. val = fdtdec_get_int(blob, node, "vsync-active", -1);
  970. if (val != -1) {
  971. dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
  972. DISPLAY_FLAGS_VSYNC_LOW;
  973. }
  974. val = fdtdec_get_int(blob, node, "hsync-active", -1);
  975. if (val != -1) {
  976. dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
  977. DISPLAY_FLAGS_HSYNC_LOW;
  978. }
  979. val = fdtdec_get_int(blob, node, "de-active", -1);
  980. if (val != -1) {
  981. dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
  982. DISPLAY_FLAGS_DE_LOW;
  983. }
  984. val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
  985. if (val != -1) {
  986. dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
  987. DISPLAY_FLAGS_PIXDATA_NEGEDGE;
  988. }
  989. if (fdtdec_get_bool(blob, node, "interlaced"))
  990. dt->flags |= DISPLAY_FLAGS_INTERLACED;
  991. if (fdtdec_get_bool(blob, node, "doublescan"))
  992. dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
  993. if (fdtdec_get_bool(blob, node, "doubleclk"))
  994. dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
  995. return 0;
  996. }
  997. int fdtdec_setup(void)
  998. {
  999. #if CONFIG_IS_ENABLED(OF_CONTROL)
  1000. # ifdef CONFIG_OF_EMBED
  1001. /* Get a pointer to the FDT */
  1002. gd->fdt_blob = __dtb_dt_begin;
  1003. # elif defined CONFIG_OF_SEPARATE
  1004. # ifdef CONFIG_SPL_BUILD
  1005. /* FDT is at end of BSS unless it is in a different memory region */
  1006. if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
  1007. gd->fdt_blob = (ulong *)&_image_binary_end;
  1008. else
  1009. gd->fdt_blob = (ulong *)&__bss_end;
  1010. # else
  1011. /* FDT is at end of image */
  1012. gd->fdt_blob = (ulong *)&_end;
  1013. # endif
  1014. # elif defined(CONFIG_OF_HOSTFILE)
  1015. if (sandbox_read_fdt_from_file()) {
  1016. puts("Failed to read control FDT\n");
  1017. return -1;
  1018. }
  1019. # endif
  1020. # ifndef CONFIG_SPL_BUILD
  1021. /* Allow the early environment to override the fdt address */
  1022. gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
  1023. (uintptr_t)gd->fdt_blob);
  1024. # endif
  1025. #endif
  1026. return fdtdec_prepare_fdt();
  1027. }
  1028. #endif /* !USE_HOSTCC */