fdtdec.c 23 KB

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