fdtdec.c 30 KB

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