fdtdec.c 30 KB

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