fdtdec.c 26 KB

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