fdtdec.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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(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(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
  46. COMPAT(GOOGLE_CROS_EC, "google,cros-ec"),
  47. COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
  48. COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
  49. COMPAT(SAMSUNG_EXYNOS5_XHCI, "samsung,exynos5250-xhci"),
  50. COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
  51. COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
  52. COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
  53. COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
  54. COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
  55. COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
  56. COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
  57. COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
  58. COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
  59. COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
  60. COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
  61. COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
  62. COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
  63. COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"),
  64. COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
  65. COMPAT(SANDBOX_HOST_EMULATION, "sandbox,host-emulation"),
  66. COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"),
  67. COMPAT(TI_TPS65090, "ti,tps65090"),
  68. COMPAT(COMPAT_NXP_PTN3460, "nxp,ptn3460"),
  69. COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
  70. COMPAT(PARADE_PS8625, "parade,ps8625"),
  71. };
  72. const char *fdtdec_get_compatible(enum fdt_compat_id id)
  73. {
  74. /* We allow reading of the 'unknown' ID for testing purposes */
  75. assert(id >= 0 && id < COMPAT_COUNT);
  76. return compat_names[id];
  77. }
  78. fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
  79. const char *prop_name, fdt_size_t *sizep)
  80. {
  81. const fdt_addr_t *cell;
  82. int len;
  83. debug("%s: %s: ", __func__, prop_name);
  84. cell = fdt_getprop(blob, node, prop_name, &len);
  85. if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
  86. len == sizeof(fdt_addr_t) * 2)) {
  87. fdt_addr_t addr = fdt_addr_to_cpu(*cell);
  88. if (sizep) {
  89. const fdt_size_t *size;
  90. size = (fdt_size_t *)((char *)cell +
  91. sizeof(fdt_addr_t));
  92. *sizep = fdt_size_to_cpu(*size);
  93. debug("addr=%08lx, size=%08x\n",
  94. (ulong)addr, *sizep);
  95. } else {
  96. debug("%08lx\n", (ulong)addr);
  97. }
  98. return addr;
  99. }
  100. debug("(not found)\n");
  101. return FDT_ADDR_T_NONE;
  102. }
  103. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  104. const char *prop_name)
  105. {
  106. return fdtdec_get_addr_size(blob, node, prop_name, NULL);
  107. }
  108. uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
  109. uint64_t default_val)
  110. {
  111. const uint64_t *cell64;
  112. int length;
  113. cell64 = fdt_getprop(blob, node, prop_name, &length);
  114. if (!cell64 || length < sizeof(*cell64))
  115. return default_val;
  116. return fdt64_to_cpu(*cell64);
  117. }
  118. int fdtdec_get_is_enabled(const void *blob, int node)
  119. {
  120. const char *cell;
  121. /*
  122. * It should say "okay", so only allow that. Some fdts use "ok" but
  123. * this is a bug. Please fix your device tree source file. See here
  124. * for discussion:
  125. *
  126. * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
  127. */
  128. cell = fdt_getprop(blob, node, "status", NULL);
  129. if (cell)
  130. return 0 == strcmp(cell, "okay");
  131. return 1;
  132. }
  133. enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
  134. {
  135. enum fdt_compat_id id;
  136. /* Search our drivers */
  137. for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
  138. if (0 == fdt_node_check_compatible(blob, node,
  139. compat_names[id]))
  140. return id;
  141. return COMPAT_UNKNOWN;
  142. }
  143. int fdtdec_next_compatible(const void *blob, int node,
  144. enum fdt_compat_id id)
  145. {
  146. return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
  147. }
  148. int fdtdec_next_compatible_subnode(const void *blob, int node,
  149. enum fdt_compat_id id, int *depthp)
  150. {
  151. do {
  152. node = fdt_next_node(blob, node, depthp);
  153. } while (*depthp > 1);
  154. /* If this is a direct subnode, and compatible, return it */
  155. if (*depthp == 1 && 0 == fdt_node_check_compatible(
  156. blob, node, compat_names[id]))
  157. return node;
  158. return -FDT_ERR_NOTFOUND;
  159. }
  160. int fdtdec_next_alias(const void *blob, const char *name,
  161. enum fdt_compat_id id, int *upto)
  162. {
  163. #define MAX_STR_LEN 20
  164. char str[MAX_STR_LEN + 20];
  165. int node, err;
  166. /* snprintf() is not available */
  167. assert(strlen(name) < MAX_STR_LEN);
  168. sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
  169. node = fdt_path_offset(blob, str);
  170. if (node < 0)
  171. return node;
  172. err = fdt_node_check_compatible(blob, node, compat_names[id]);
  173. if (err < 0)
  174. return err;
  175. if (err)
  176. return -FDT_ERR_NOTFOUND;
  177. (*upto)++;
  178. return node;
  179. }
  180. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  181. enum fdt_compat_id id, int *node_list, int maxcount)
  182. {
  183. memset(node_list, '\0', sizeof(*node_list) * maxcount);
  184. return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
  185. }
  186. /* TODO: Can we tighten this code up a little? */
  187. int fdtdec_add_aliases_for_id(const void *blob, const char *name,
  188. enum fdt_compat_id id, int *node_list, int maxcount)
  189. {
  190. int name_len = strlen(name);
  191. int nodes[maxcount];
  192. int num_found = 0;
  193. int offset, node;
  194. int alias_node;
  195. int count;
  196. int i, j;
  197. /* find the alias node if present */
  198. alias_node = fdt_path_offset(blob, "/aliases");
  199. /*
  200. * start with nothing, and we can assume that the root node can't
  201. * match
  202. */
  203. memset(nodes, '\0', sizeof(nodes));
  204. /* First find all the compatible nodes */
  205. for (node = count = 0; node >= 0 && count < maxcount;) {
  206. node = fdtdec_next_compatible(blob, node, id);
  207. if (node >= 0)
  208. nodes[count++] = node;
  209. }
  210. if (node >= 0)
  211. debug("%s: warning: maxcount exceeded with alias '%s'\n",
  212. __func__, name);
  213. /* Now find all the aliases */
  214. for (offset = fdt_first_property_offset(blob, alias_node);
  215. offset > 0;
  216. offset = fdt_next_property_offset(blob, offset)) {
  217. const struct fdt_property *prop;
  218. const char *path;
  219. int number;
  220. int found;
  221. node = 0;
  222. prop = fdt_get_property_by_offset(blob, offset, NULL);
  223. path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  224. if (prop->len && 0 == strncmp(path, name, name_len))
  225. node = fdt_path_offset(blob, prop->data);
  226. if (node <= 0)
  227. continue;
  228. /* Get the alias number */
  229. number = simple_strtoul(path + name_len, NULL, 10);
  230. if (number < 0 || number >= maxcount) {
  231. debug("%s: warning: alias '%s' is out of range\n",
  232. __func__, path);
  233. continue;
  234. }
  235. /* Make sure the node we found is actually in our list! */
  236. found = -1;
  237. for (j = 0; j < count; j++)
  238. if (nodes[j] == node) {
  239. found = j;
  240. break;
  241. }
  242. if (found == -1) {
  243. debug("%s: warning: alias '%s' points to a node "
  244. "'%s' that is missing or is not compatible "
  245. " with '%s'\n", __func__, path,
  246. fdt_get_name(blob, node, NULL),
  247. compat_names[id]);
  248. continue;
  249. }
  250. /*
  251. * Add this node to our list in the right place, and mark
  252. * it as done.
  253. */
  254. if (fdtdec_get_is_enabled(blob, node)) {
  255. if (node_list[number]) {
  256. debug("%s: warning: alias '%s' requires that "
  257. "a node be placed in the list in a "
  258. "position which is already filled by "
  259. "node '%s'\n", __func__, path,
  260. fdt_get_name(blob, node, NULL));
  261. continue;
  262. }
  263. node_list[number] = node;
  264. if (number >= num_found)
  265. num_found = number + 1;
  266. }
  267. nodes[found] = 0;
  268. }
  269. /* Add any nodes not mentioned by an alias */
  270. for (i = j = 0; i < maxcount; i++) {
  271. if (!node_list[i]) {
  272. for (; j < maxcount; j++)
  273. if (nodes[j] &&
  274. fdtdec_get_is_enabled(blob, nodes[j]))
  275. break;
  276. /* Have we run out of nodes to add? */
  277. if (j == maxcount)
  278. break;
  279. assert(!node_list[i]);
  280. node_list[i] = nodes[j++];
  281. if (i >= num_found)
  282. num_found = i + 1;
  283. }
  284. }
  285. return num_found;
  286. }
  287. int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
  288. int *seqp)
  289. {
  290. int base_len = strlen(base);
  291. const char *find_name;
  292. int find_namelen;
  293. int prop_offset;
  294. int aliases;
  295. find_name = fdt_get_name(blob, offset, &find_namelen);
  296. debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
  297. aliases = fdt_path_offset(blob, "/aliases");
  298. for (prop_offset = fdt_first_property_offset(blob, aliases);
  299. prop_offset > 0;
  300. prop_offset = fdt_next_property_offset(blob, prop_offset)) {
  301. const char *prop;
  302. const char *name;
  303. const char *slash;
  304. const char *p;
  305. int len;
  306. prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
  307. debug(" - %s, %s\n", name, prop);
  308. if (len < find_namelen || *prop != '/' || prop[len - 1] ||
  309. strncmp(name, base, base_len))
  310. continue;
  311. slash = strrchr(prop, '/');
  312. if (strcmp(slash + 1, find_name))
  313. continue;
  314. for (p = name; *p; p++) {
  315. if (isdigit(*p)) {
  316. *seqp = simple_strtoul(p, NULL, 10);
  317. debug("Found seq %d\n", *seqp);
  318. return 0;
  319. }
  320. }
  321. }
  322. debug("Not found\n");
  323. return -ENOENT;
  324. }
  325. int fdtdec_get_alias_node(const void *blob, const char *name)
  326. {
  327. const char *prop;
  328. int alias_node;
  329. int len;
  330. if (!blob)
  331. return -FDT_ERR_NOTFOUND;
  332. alias_node = fdt_path_offset(blob, "/aliases");
  333. prop = fdt_getprop(blob, alias_node, name, &len);
  334. if (!prop)
  335. return -FDT_ERR_NOTFOUND;
  336. return fdt_path_offset(blob, prop);
  337. }
  338. int fdtdec_get_chosen_node(const void *blob, const char *name)
  339. {
  340. const char *prop;
  341. int chosen_node;
  342. int len;
  343. if (!blob)
  344. return -FDT_ERR_NOTFOUND;
  345. chosen_node = fdt_path_offset(blob, "/chosen");
  346. prop = fdt_getprop(blob, chosen_node, name, &len);
  347. if (!prop)
  348. return -FDT_ERR_NOTFOUND;
  349. return fdt_path_offset(blob, prop);
  350. }
  351. int fdtdec_check_fdt(void)
  352. {
  353. /*
  354. * We must have an FDT, but we cannot panic() yet since the console
  355. * is not ready. So for now, just assert(). Boards which need an early
  356. * FDT (prior to console ready) will need to make their own
  357. * arrangements and do their own checks.
  358. */
  359. assert(!fdtdec_prepare_fdt());
  360. return 0;
  361. }
  362. /*
  363. * This function is a little odd in that it accesses global data. At some
  364. * point if the architecture board.c files merge this will make more sense.
  365. * Even now, it is common code.
  366. */
  367. int fdtdec_prepare_fdt(void)
  368. {
  369. if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
  370. fdt_check_header(gd->fdt_blob)) {
  371. printf("No valid FDT found - please append one to U-Boot "
  372. "binary, use u-boot-dtb.bin or define "
  373. "CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
  374. return -1;
  375. }
  376. return 0;
  377. }
  378. int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
  379. {
  380. const u32 *phandle;
  381. int lookup;
  382. debug("%s: %s\n", __func__, prop_name);
  383. phandle = fdt_getprop(blob, node, prop_name, NULL);
  384. if (!phandle)
  385. return -FDT_ERR_NOTFOUND;
  386. lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
  387. return lookup;
  388. }
  389. /**
  390. * Look up a property in a node and check that it has a minimum length.
  391. *
  392. * @param blob FDT blob
  393. * @param node node to examine
  394. * @param prop_name name of property to find
  395. * @param min_len minimum property length in bytes
  396. * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
  397. found, or -FDT_ERR_BADLAYOUT if not enough data
  398. * @return pointer to cell, which is only valid if err == 0
  399. */
  400. static const void *get_prop_check_min_len(const void *blob, int node,
  401. const char *prop_name, int min_len, int *err)
  402. {
  403. const void *cell;
  404. int len;
  405. debug("%s: %s\n", __func__, prop_name);
  406. cell = fdt_getprop(blob, node, prop_name, &len);
  407. if (!cell)
  408. *err = -FDT_ERR_NOTFOUND;
  409. else if (len < min_len)
  410. *err = -FDT_ERR_BADLAYOUT;
  411. else
  412. *err = 0;
  413. return cell;
  414. }
  415. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  416. u32 *array, int count)
  417. {
  418. const u32 *cell;
  419. int i, err = 0;
  420. debug("%s: %s\n", __func__, prop_name);
  421. cell = get_prop_check_min_len(blob, node, prop_name,
  422. sizeof(u32) * count, &err);
  423. if (!err) {
  424. for (i = 0; i < count; i++)
  425. array[i] = fdt32_to_cpu(cell[i]);
  426. }
  427. return err;
  428. }
  429. const u32 *fdtdec_locate_array(const void *blob, int node,
  430. const char *prop_name, int count)
  431. {
  432. const u32 *cell;
  433. int err;
  434. cell = get_prop_check_min_len(blob, node, prop_name,
  435. sizeof(u32) * count, &err);
  436. return err ? NULL : cell;
  437. }
  438. int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
  439. {
  440. const s32 *cell;
  441. int len;
  442. debug("%s: %s\n", __func__, prop_name);
  443. cell = fdt_getprop(blob, node, prop_name, &len);
  444. return cell != NULL;
  445. }
  446. /**
  447. * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
  448. * terminating item.
  449. *
  450. * @param blob FDT blob to use
  451. * @param node Node to look at
  452. * @param prop_name Node property name
  453. * @param gpio Array of gpio elements to fill from FDT. This will be
  454. * untouched if either 0 or an error is returned
  455. * @param max_count Maximum number of elements allowed
  456. * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
  457. * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
  458. */
  459. int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
  460. struct fdt_gpio_state *gpio, int max_count)
  461. {
  462. const struct fdt_property *prop;
  463. const u32 *cell;
  464. const char *name;
  465. int len, i;
  466. debug("%s: %s\n", __func__, prop_name);
  467. assert(max_count > 0);
  468. prop = fdt_get_property(blob, node, prop_name, &len);
  469. if (!prop) {
  470. debug("%s: property '%s' missing\n", __func__, prop_name);
  471. return -FDT_ERR_NOTFOUND;
  472. }
  473. /* We will use the name to tag the GPIO */
  474. name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  475. cell = (u32 *)prop->data;
  476. len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
  477. if (len > max_count) {
  478. debug(" %s: too many GPIOs / cells for "
  479. "property '%s'\n", __func__, prop_name);
  480. return -FDT_ERR_BADLAYOUT;
  481. }
  482. /* Read out the GPIO data from the cells */
  483. for (i = 0; i < len; i++, cell += 3) {
  484. gpio[i].gpio = fdt32_to_cpu(cell[1]);
  485. gpio[i].flags = fdt32_to_cpu(cell[2]);
  486. gpio[i].name = name;
  487. }
  488. return len;
  489. }
  490. int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
  491. struct fdt_gpio_state *gpio)
  492. {
  493. int err;
  494. debug("%s: %s\n", __func__, prop_name);
  495. gpio->gpio = FDT_GPIO_NONE;
  496. gpio->name = NULL;
  497. err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
  498. return err == 1 ? 0 : err;
  499. }
  500. int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
  501. {
  502. int val;
  503. if (!fdt_gpio_isvalid(gpio))
  504. return -1;
  505. val = gpio_get_value(gpio->gpio);
  506. return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
  507. }
  508. int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
  509. {
  510. if (!fdt_gpio_isvalid(gpio))
  511. return -1;
  512. val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
  513. return gpio_set_value(gpio->gpio, val);
  514. }
  515. int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
  516. {
  517. /*
  518. * Return success if there is no GPIO defined. This is used for
  519. * optional GPIOs)
  520. */
  521. if (!fdt_gpio_isvalid(gpio))
  522. return 0;
  523. if (gpio_request(gpio->gpio, gpio->name))
  524. return -1;
  525. return 0;
  526. }
  527. int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
  528. u8 *array, int count)
  529. {
  530. const u8 *cell;
  531. int err;
  532. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  533. if (!err)
  534. memcpy(array, cell, count);
  535. return err;
  536. }
  537. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  538. const char *prop_name, int count)
  539. {
  540. const u8 *cell;
  541. int err;
  542. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  543. if (err)
  544. return NULL;
  545. return cell;
  546. }
  547. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  548. int default_val)
  549. {
  550. int config_node;
  551. debug("%s: %s\n", __func__, prop_name);
  552. config_node = fdt_path_offset(blob, "/config");
  553. if (config_node < 0)
  554. return default_val;
  555. return fdtdec_get_int(blob, config_node, prop_name, default_val);
  556. }
  557. int fdtdec_get_config_bool(const void *blob, const char *prop_name)
  558. {
  559. int config_node;
  560. const void *prop;
  561. debug("%s: %s\n", __func__, prop_name);
  562. config_node = fdt_path_offset(blob, "/config");
  563. if (config_node < 0)
  564. return 0;
  565. prop = fdt_get_property(blob, config_node, prop_name, NULL);
  566. return prop != NULL;
  567. }
  568. char *fdtdec_get_config_string(const void *blob, const char *prop_name)
  569. {
  570. const char *nodep;
  571. int nodeoffset;
  572. int len;
  573. debug("%s: %s\n", __func__, prop_name);
  574. nodeoffset = fdt_path_offset(blob, "/config");
  575. if (nodeoffset < 0)
  576. return NULL;
  577. nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
  578. if (!nodep)
  579. return NULL;
  580. return (char *)nodep;
  581. }
  582. int fdtdec_decode_region(const void *blob, int node,
  583. const char *prop_name, void **ptrp, size_t *size)
  584. {
  585. const fdt_addr_t *cell;
  586. int len;
  587. debug("%s: %s\n", __func__, prop_name);
  588. cell = fdt_getprop(blob, node, prop_name, &len);
  589. if (!cell || (len != sizeof(fdt_addr_t) * 2))
  590. return -1;
  591. *ptrp = map_sysmem(fdt_addr_to_cpu(*cell), *size);
  592. *size = fdt_size_to_cpu(cell[1]);
  593. debug("%s: size=%zx\n", __func__, *size);
  594. return 0;
  595. }
  596. /**
  597. * Read a flash entry from the fdt
  598. *
  599. * @param blob FDT blob
  600. * @param node Offset of node to read
  601. * @param name Name of node being read
  602. * @param entry Place to put offset and size of this node
  603. * @return 0 if ok, -ve on error
  604. */
  605. int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
  606. struct fmap_entry *entry)
  607. {
  608. u32 reg[2];
  609. if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
  610. debug("Node '%s' has bad/missing 'reg' property\n", name);
  611. return -FDT_ERR_NOTFOUND;
  612. }
  613. entry->offset = reg[0];
  614. entry->length = reg[1];
  615. return 0;
  616. }
  617. #endif