fdtdec.c 21 KB

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