fdtdec.c 20 KB

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