fdtdec.c 30 KB

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