fdtdec.c 20 KB

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