fdtdec.c 18 KB

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