fdtdec.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. #include <common.h>
  22. #include <serial.h>
  23. #include <libfdt.h>
  24. #include <fdtdec.h>
  25. #include <asm/gpio.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. /*
  28. * Here are the type we know about. One day we might allow drivers to
  29. * register. For now we just put them here. The COMPAT macro allows us to
  30. * turn this into a sparse list later, and keeps the ID with the name.
  31. */
  32. #define COMPAT(id, name) name
  33. static const char * const compat_names[COMPAT_COUNT] = {
  34. COMPAT(UNKNOWN, "<none>"),
  35. COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
  36. COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"),
  37. COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
  38. COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
  39. COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
  40. COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
  41. COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
  42. COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
  43. COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
  44. COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
  45. COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
  46. COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
  47. COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"),
  48. COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"),
  49. COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"),
  50. COMPAT(SMSC_LAN9215, "smsc,lan9215"),
  51. COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
  52. COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
  53. COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
  54. COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
  55. COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
  56. COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
  57. COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
  58. COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
  59. COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
  60. COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
  61. COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
  62. COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
  63. COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
  64. };
  65. const char *fdtdec_get_compatible(enum fdt_compat_id id)
  66. {
  67. /* We allow reading of the 'unknown' ID for testing purposes */
  68. assert(id >= 0 && id < COMPAT_COUNT);
  69. return compat_names[id];
  70. }
  71. fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
  72. const char *prop_name, fdt_size_t *sizep)
  73. {
  74. const fdt_addr_t *cell;
  75. int len;
  76. debug("%s: %s: ", __func__, prop_name);
  77. cell = fdt_getprop(blob, node, prop_name, &len);
  78. if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
  79. len == sizeof(fdt_addr_t) * 2)) {
  80. fdt_addr_t addr = fdt_addr_to_cpu(*cell);
  81. if (sizep) {
  82. const fdt_size_t *size;
  83. size = (fdt_size_t *)((char *)cell +
  84. sizeof(fdt_addr_t));
  85. *sizep = fdt_size_to_cpu(*size);
  86. debug("addr=%p, size=%p\n", (void *)addr,
  87. (void *)*sizep);
  88. } else {
  89. debug("%p\n", (void *)addr);
  90. }
  91. return addr;
  92. }
  93. debug("(not found)\n");
  94. return FDT_ADDR_T_NONE;
  95. }
  96. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  97. const char *prop_name)
  98. {
  99. return fdtdec_get_addr_size(blob, node, prop_name, NULL);
  100. }
  101. s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
  102. s32 default_val)
  103. {
  104. const s32 *cell;
  105. int len;
  106. debug("%s: %s: ", __func__, prop_name);
  107. cell = fdt_getprop(blob, node, prop_name, &len);
  108. if (cell && len >= sizeof(s32)) {
  109. s32 val = fdt32_to_cpu(cell[0]);
  110. debug("%#x (%d)\n", val, val);
  111. return val;
  112. }
  113. debug("(not found)\n");
  114. return default_val;
  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_check_fdt(void)
  296. {
  297. /*
  298. * We must have an FDT, but we cannot panic() yet since the console
  299. * is not ready. So for now, just assert(). Boards which need an early
  300. * FDT (prior to console ready) will need to make their own
  301. * arrangements and do their own checks.
  302. */
  303. assert(!fdtdec_prepare_fdt());
  304. return 0;
  305. }
  306. /*
  307. * This function is a little odd in that it accesses global data. At some
  308. * point if the architecture board.c files merge this will make more sense.
  309. * Even now, it is common code.
  310. */
  311. int fdtdec_prepare_fdt(void)
  312. {
  313. if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob)) {
  314. printf("No valid FDT found - please append one to U-Boot "
  315. "binary, use u-boot-dtb.bin or define "
  316. "CONFIG_OF_EMBED\n");
  317. return -1;
  318. }
  319. return 0;
  320. }
  321. int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
  322. {
  323. const u32 *phandle;
  324. int lookup;
  325. debug("%s: %s\n", __func__, prop_name);
  326. phandle = fdt_getprop(blob, node, prop_name, NULL);
  327. if (!phandle)
  328. return -FDT_ERR_NOTFOUND;
  329. lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
  330. return lookup;
  331. }
  332. /**
  333. * Look up a property in a node and check that it has a minimum length.
  334. *
  335. * @param blob FDT blob
  336. * @param node node to examine
  337. * @param prop_name name of property to find
  338. * @param min_len minimum property length in bytes
  339. * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
  340. found, or -FDT_ERR_BADLAYOUT if not enough data
  341. * @return pointer to cell, which is only valid if err == 0
  342. */
  343. static const void *get_prop_check_min_len(const void *blob, int node,
  344. const char *prop_name, int min_len, int *err)
  345. {
  346. const void *cell;
  347. int len;
  348. debug("%s: %s\n", __func__, prop_name);
  349. cell = fdt_getprop(blob, node, prop_name, &len);
  350. if (!cell)
  351. *err = -FDT_ERR_NOTFOUND;
  352. else if (len < min_len)
  353. *err = -FDT_ERR_BADLAYOUT;
  354. else
  355. *err = 0;
  356. return cell;
  357. }
  358. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  359. u32 *array, int count)
  360. {
  361. const u32 *cell;
  362. int i, err = 0;
  363. debug("%s: %s\n", __func__, prop_name);
  364. cell = get_prop_check_min_len(blob, node, prop_name,
  365. sizeof(u32) * count, &err);
  366. if (!err) {
  367. for (i = 0; i < count; i++)
  368. array[i] = fdt32_to_cpu(cell[i]);
  369. }
  370. return err;
  371. }
  372. const u32 *fdtdec_locate_array(const void *blob, int node,
  373. const char *prop_name, int count)
  374. {
  375. const u32 *cell;
  376. int err;
  377. cell = get_prop_check_min_len(blob, node, prop_name,
  378. sizeof(u32) * count, &err);
  379. return err ? NULL : cell;
  380. }
  381. int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
  382. {
  383. const s32 *cell;
  384. int len;
  385. debug("%s: %s\n", __func__, prop_name);
  386. cell = fdt_getprop(blob, node, prop_name, &len);
  387. return cell != NULL;
  388. }
  389. /**
  390. * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
  391. * terminating item.
  392. *
  393. * @param blob FDT blob to use
  394. * @param node Node to look at
  395. * @param prop_name Node property name
  396. * @param gpio Array of gpio elements to fill from FDT. This will be
  397. * untouched if either 0 or an error is returned
  398. * @param max_count Maximum number of elements allowed
  399. * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
  400. * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
  401. */
  402. int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
  403. struct fdt_gpio_state *gpio, int max_count)
  404. {
  405. const struct fdt_property *prop;
  406. const u32 *cell;
  407. const char *name;
  408. int len, i;
  409. debug("%s: %s\n", __func__, prop_name);
  410. assert(max_count > 0);
  411. prop = fdt_get_property(blob, node, prop_name, &len);
  412. if (!prop) {
  413. debug("%s: property '%s' missing\n", __func__, prop_name);
  414. return -FDT_ERR_NOTFOUND;
  415. }
  416. /* We will use the name to tag the GPIO */
  417. name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  418. cell = (u32 *)prop->data;
  419. len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
  420. if (len > max_count) {
  421. debug(" %s: too many GPIOs / cells for "
  422. "property '%s'\n", __func__, prop_name);
  423. return -FDT_ERR_BADLAYOUT;
  424. }
  425. /* Read out the GPIO data from the cells */
  426. for (i = 0; i < len; i++, cell += 3) {
  427. gpio[i].gpio = fdt32_to_cpu(cell[1]);
  428. gpio[i].flags = fdt32_to_cpu(cell[2]);
  429. gpio[i].name = name;
  430. }
  431. return len;
  432. }
  433. int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
  434. struct fdt_gpio_state *gpio)
  435. {
  436. int err;
  437. debug("%s: %s\n", __func__, prop_name);
  438. gpio->gpio = FDT_GPIO_NONE;
  439. gpio->name = NULL;
  440. err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
  441. return err == 1 ? 0 : err;
  442. }
  443. int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
  444. {
  445. int val;
  446. if (!fdt_gpio_isvalid(gpio))
  447. return -1;
  448. val = gpio_get_value(gpio->gpio);
  449. return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
  450. }
  451. int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
  452. {
  453. if (!fdt_gpio_isvalid(gpio))
  454. return -1;
  455. val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
  456. return gpio_set_value(gpio->gpio, val);
  457. }
  458. int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
  459. {
  460. /*
  461. * Return success if there is no GPIO defined. This is used for
  462. * optional GPIOs)
  463. */
  464. if (!fdt_gpio_isvalid(gpio))
  465. return 0;
  466. if (gpio_request(gpio->gpio, gpio->name))
  467. return -1;
  468. return 0;
  469. }
  470. int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
  471. u8 *array, int count)
  472. {
  473. const u8 *cell;
  474. int err;
  475. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  476. if (!err)
  477. memcpy(array, cell, count);
  478. return err;
  479. }
  480. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  481. const char *prop_name, int count)
  482. {
  483. const u8 *cell;
  484. int err;
  485. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  486. if (err)
  487. return NULL;
  488. return cell;
  489. }
  490. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  491. int default_val)
  492. {
  493. int config_node;
  494. debug("%s: %s\n", __func__, prop_name);
  495. config_node = fdt_path_offset(blob, "/config");
  496. if (config_node < 0)
  497. return default_val;
  498. return fdtdec_get_int(blob, config_node, prop_name, default_val);
  499. }
  500. int fdtdec_get_config_bool(const void *blob, const char *prop_name)
  501. {
  502. int config_node;
  503. const void *prop;
  504. debug("%s: %s\n", __func__, prop_name);
  505. config_node = fdt_path_offset(blob, "/config");
  506. if (config_node < 0)
  507. return 0;
  508. prop = fdt_get_property(blob, config_node, prop_name, NULL);
  509. return prop != NULL;
  510. }
  511. char *fdtdec_get_config_string(const void *blob, const char *prop_name)
  512. {
  513. const char *nodep;
  514. int nodeoffset;
  515. int len;
  516. debug("%s: %s\n", __func__, prop_name);
  517. nodeoffset = fdt_path_offset(blob, "/config");
  518. if (nodeoffset < 0)
  519. return NULL;
  520. nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
  521. if (!nodep)
  522. return NULL;
  523. return (char *)nodep;
  524. }
  525. int fdtdec_decode_region(const void *blob, int node,
  526. const char *prop_name, void **ptrp, size_t *size)
  527. {
  528. const fdt_addr_t *cell;
  529. int len;
  530. debug("%s: %s\n", __func__, prop_name);
  531. cell = fdt_getprop(blob, node, prop_name, &len);
  532. if (!cell || (len != sizeof(fdt_addr_t) * 2))
  533. return -1;
  534. *ptrp = (void *)fdt_addr_to_cpu(*cell);
  535. *size = fdt_size_to_cpu(cell[1]);
  536. debug("%s: size=%zx\n", __func__, *size);
  537. return 0;
  538. }