fdtdec.c 22 KB

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