fdtdec.c 22 KB

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