fdtdec.c 26 KB

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