fdtdec.c 18 KB

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