fdtdec.c 30 KB

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