fdtdec.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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(SMSC_LAN9215, "smsc,lan9215"),
  50. COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
  51. COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
  52. COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
  53. COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
  54. COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
  55. COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
  56. COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
  57. COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
  58. };
  59. const char *fdtdec_get_compatible(enum fdt_compat_id id)
  60. {
  61. /* We allow reading of the 'unknown' ID for testing purposes */
  62. assert(id >= 0 && id < COMPAT_COUNT);
  63. return compat_names[id];
  64. }
  65. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  66. const char *prop_name)
  67. {
  68. const fdt_addr_t *cell;
  69. int len;
  70. debug("%s: %s: ", __func__, prop_name);
  71. cell = fdt_getprop(blob, node, prop_name, &len);
  72. if (cell && (len == sizeof(fdt_addr_t) ||
  73. len == sizeof(fdt_addr_t) * 2)) {
  74. fdt_addr_t addr = fdt_addr_to_cpu(*cell);
  75. debug("%p\n", (void *)addr);
  76. return addr;
  77. }
  78. debug("(not found)\n");
  79. return FDT_ADDR_T_NONE;
  80. }
  81. s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
  82. s32 default_val)
  83. {
  84. const s32 *cell;
  85. int len;
  86. debug("%s: %s: ", __func__, prop_name);
  87. cell = fdt_getprop(blob, node, prop_name, &len);
  88. if (cell && len >= sizeof(s32)) {
  89. s32 val = fdt32_to_cpu(cell[0]);
  90. debug("%#x (%d)\n", val, val);
  91. return val;
  92. }
  93. debug("(not found)\n");
  94. return default_val;
  95. }
  96. uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
  97. uint64_t default_val)
  98. {
  99. const uint64_t *cell64;
  100. int length;
  101. cell64 = fdt_getprop(blob, node, prop_name, &length);
  102. if (!cell64 || length < sizeof(*cell64))
  103. return default_val;
  104. return fdt64_to_cpu(*cell64);
  105. }
  106. int fdtdec_get_is_enabled(const void *blob, int node)
  107. {
  108. const char *cell;
  109. /*
  110. * It should say "okay", so only allow that. Some fdts use "ok" but
  111. * this is a bug. Please fix your device tree source file. See here
  112. * for discussion:
  113. *
  114. * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
  115. */
  116. cell = fdt_getprop(blob, node, "status", NULL);
  117. if (cell)
  118. return 0 == strcmp(cell, "okay");
  119. return 1;
  120. }
  121. enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
  122. {
  123. enum fdt_compat_id id;
  124. /* Search our drivers */
  125. for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
  126. if (0 == fdt_node_check_compatible(blob, node,
  127. compat_names[id]))
  128. return id;
  129. return COMPAT_UNKNOWN;
  130. }
  131. int fdtdec_next_compatible(const void *blob, int node,
  132. enum fdt_compat_id id)
  133. {
  134. return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
  135. }
  136. int fdtdec_next_compatible_subnode(const void *blob, int node,
  137. enum fdt_compat_id id, int *depthp)
  138. {
  139. do {
  140. node = fdt_next_node(blob, node, depthp);
  141. } while (*depthp > 1);
  142. /* If this is a direct subnode, and compatible, return it */
  143. if (*depthp == 1 && 0 == fdt_node_check_compatible(
  144. blob, node, compat_names[id]))
  145. return node;
  146. return -FDT_ERR_NOTFOUND;
  147. }
  148. int fdtdec_next_alias(const void *blob, const char *name,
  149. enum fdt_compat_id id, int *upto)
  150. {
  151. #define MAX_STR_LEN 20
  152. char str[MAX_STR_LEN + 20];
  153. int node, err;
  154. /* snprintf() is not available */
  155. assert(strlen(name) < MAX_STR_LEN);
  156. sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
  157. node = fdt_path_offset(blob, str);
  158. if (node < 0)
  159. return node;
  160. err = fdt_node_check_compatible(blob, node, compat_names[id]);
  161. if (err < 0)
  162. return err;
  163. if (err)
  164. return -FDT_ERR_NOTFOUND;
  165. (*upto)++;
  166. return node;
  167. }
  168. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  169. enum fdt_compat_id id, int *node_list, int maxcount)
  170. {
  171. memset(node_list, '\0', sizeof(*node_list) * maxcount);
  172. return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
  173. }
  174. /* TODO: Can we tighten this code up a little? */
  175. int fdtdec_add_aliases_for_id(const void *blob, const char *name,
  176. enum fdt_compat_id id, int *node_list, int maxcount)
  177. {
  178. int name_len = strlen(name);
  179. int nodes[maxcount];
  180. int num_found = 0;
  181. int offset, node;
  182. int alias_node;
  183. int count;
  184. int i, j;
  185. /* find the alias node if present */
  186. alias_node = fdt_path_offset(blob, "/aliases");
  187. /*
  188. * start with nothing, and we can assume that the root node can't
  189. * match
  190. */
  191. memset(nodes, '\0', sizeof(nodes));
  192. /* First find all the compatible nodes */
  193. for (node = count = 0; node >= 0 && count < maxcount;) {
  194. node = fdtdec_next_compatible(blob, node, id);
  195. if (node >= 0)
  196. nodes[count++] = node;
  197. }
  198. if (node >= 0)
  199. debug("%s: warning: maxcount exceeded with alias '%s'\n",
  200. __func__, name);
  201. /* Now find all the aliases */
  202. for (offset = fdt_first_property_offset(blob, alias_node);
  203. offset > 0;
  204. offset = fdt_next_property_offset(blob, offset)) {
  205. const struct fdt_property *prop;
  206. const char *path;
  207. int number;
  208. int found;
  209. node = 0;
  210. prop = fdt_get_property_by_offset(blob, offset, NULL);
  211. path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  212. if (prop->len && 0 == strncmp(path, name, name_len))
  213. node = fdt_path_offset(blob, prop->data);
  214. if (node <= 0)
  215. continue;
  216. /* Get the alias number */
  217. number = simple_strtoul(path + name_len, NULL, 10);
  218. if (number < 0 || number >= maxcount) {
  219. debug("%s: warning: alias '%s' is out of range\n",
  220. __func__, path);
  221. continue;
  222. }
  223. /* Make sure the node we found is actually in our list! */
  224. found = -1;
  225. for (j = 0; j < count; j++)
  226. if (nodes[j] == node) {
  227. found = j;
  228. break;
  229. }
  230. if (found == -1) {
  231. debug("%s: warning: alias '%s' points to a node "
  232. "'%s' that is missing or is not compatible "
  233. " with '%s'\n", __func__, path,
  234. fdt_get_name(blob, node, NULL),
  235. compat_names[id]);
  236. continue;
  237. }
  238. /*
  239. * Add this node to our list in the right place, and mark
  240. * it as done.
  241. */
  242. if (fdtdec_get_is_enabled(blob, node)) {
  243. if (node_list[number]) {
  244. debug("%s: warning: alias '%s' requires that "
  245. "a node be placed in the list in a "
  246. "position which is already filled by "
  247. "node '%s'\n", __func__, path,
  248. fdt_get_name(blob, node, NULL));
  249. continue;
  250. }
  251. node_list[number] = node;
  252. if (number >= num_found)
  253. num_found = number + 1;
  254. }
  255. nodes[found] = 0;
  256. }
  257. /* Add any nodes not mentioned by an alias */
  258. for (i = j = 0; i < maxcount; i++) {
  259. if (!node_list[i]) {
  260. for (; j < maxcount; j++)
  261. if (nodes[j] &&
  262. fdtdec_get_is_enabled(blob, nodes[j]))
  263. break;
  264. /* Have we run out of nodes to add? */
  265. if (j == maxcount)
  266. break;
  267. assert(!node_list[i]);
  268. node_list[i] = nodes[j++];
  269. if (i >= num_found)
  270. num_found = i + 1;
  271. }
  272. }
  273. return num_found;
  274. }
  275. int fdtdec_check_fdt(void)
  276. {
  277. /*
  278. * We must have an FDT, but we cannot panic() yet since the console
  279. * is not ready. So for now, just assert(). Boards which need an early
  280. * FDT (prior to console ready) will need to make their own
  281. * arrangements and do their own checks.
  282. */
  283. assert(!fdtdec_prepare_fdt());
  284. return 0;
  285. }
  286. /*
  287. * This function is a little odd in that it accesses global data. At some
  288. * point if the architecture board.c files merge this will make more sense.
  289. * Even now, it is common code.
  290. */
  291. int fdtdec_prepare_fdt(void)
  292. {
  293. if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob)) {
  294. printf("No valid FDT found - please append one to U-Boot "
  295. "binary, use u-boot-dtb.bin or define "
  296. "CONFIG_OF_EMBED\n");
  297. return -1;
  298. }
  299. return 0;
  300. }
  301. int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
  302. {
  303. const u32 *phandle;
  304. int lookup;
  305. debug("%s: %s\n", __func__, prop_name);
  306. phandle = fdt_getprop(blob, node, prop_name, NULL);
  307. if (!phandle)
  308. return -FDT_ERR_NOTFOUND;
  309. lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
  310. return lookup;
  311. }
  312. /**
  313. * Look up a property in a node and check that it has a minimum length.
  314. *
  315. * @param blob FDT blob
  316. * @param node node to examine
  317. * @param prop_name name of property to find
  318. * @param min_len minimum property length in bytes
  319. * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
  320. found, or -FDT_ERR_BADLAYOUT if not enough data
  321. * @return pointer to cell, which is only valid if err == 0
  322. */
  323. static const void *get_prop_check_min_len(const void *blob, int node,
  324. const char *prop_name, int min_len, int *err)
  325. {
  326. const void *cell;
  327. int len;
  328. debug("%s: %s\n", __func__, prop_name);
  329. cell = fdt_getprop(blob, node, prop_name, &len);
  330. if (!cell)
  331. *err = -FDT_ERR_NOTFOUND;
  332. else if (len < min_len)
  333. *err = -FDT_ERR_BADLAYOUT;
  334. else
  335. *err = 0;
  336. return cell;
  337. }
  338. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  339. u32 *array, int count)
  340. {
  341. const u32 *cell;
  342. int i, err = 0;
  343. debug("%s: %s\n", __func__, prop_name);
  344. cell = get_prop_check_min_len(blob, node, prop_name,
  345. sizeof(u32) * count, &err);
  346. if (!err) {
  347. for (i = 0; i < count; i++)
  348. array[i] = fdt32_to_cpu(cell[i]);
  349. }
  350. return err;
  351. }
  352. const u32 *fdtdec_locate_array(const void *blob, int node,
  353. const char *prop_name, int count)
  354. {
  355. const u32 *cell;
  356. int err;
  357. cell = get_prop_check_min_len(blob, node, prop_name,
  358. sizeof(u32) * count, &err);
  359. return err ? NULL : cell;
  360. }
  361. int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
  362. {
  363. const s32 *cell;
  364. int len;
  365. debug("%s: %s\n", __func__, prop_name);
  366. cell = fdt_getprop(blob, node, prop_name, &len);
  367. return cell != NULL;
  368. }
  369. /**
  370. * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
  371. * terminating item.
  372. *
  373. * @param blob FDT blob to use
  374. * @param node Node to look at
  375. * @param prop_name Node property name
  376. * @param gpio Array of gpio elements to fill from FDT. This will be
  377. * untouched if either 0 or an error is returned
  378. * @param max_count Maximum number of elements allowed
  379. * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
  380. * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
  381. */
  382. int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
  383. struct fdt_gpio_state *gpio, int max_count)
  384. {
  385. const struct fdt_property *prop;
  386. const u32 *cell;
  387. const char *name;
  388. int len, i;
  389. debug("%s: %s\n", __func__, prop_name);
  390. assert(max_count > 0);
  391. prop = fdt_get_property(blob, node, prop_name, &len);
  392. if (!prop) {
  393. debug("%s: property '%s' missing\n", __func__, prop_name);
  394. return -FDT_ERR_NOTFOUND;
  395. }
  396. /* We will use the name to tag the GPIO */
  397. name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  398. cell = (u32 *)prop->data;
  399. len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
  400. if (len > max_count) {
  401. debug(" %s: too many GPIOs / cells for "
  402. "property '%s'\n", __func__, prop_name);
  403. return -FDT_ERR_BADLAYOUT;
  404. }
  405. /* Read out the GPIO data from the cells */
  406. for (i = 0; i < len; i++, cell += 3) {
  407. gpio[i].gpio = fdt32_to_cpu(cell[1]);
  408. gpio[i].flags = fdt32_to_cpu(cell[2]);
  409. gpio[i].name = name;
  410. }
  411. return len;
  412. }
  413. int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
  414. struct fdt_gpio_state *gpio)
  415. {
  416. int err;
  417. debug("%s: %s\n", __func__, prop_name);
  418. gpio->gpio = FDT_GPIO_NONE;
  419. gpio->name = NULL;
  420. err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
  421. return err == 1 ? 0 : err;
  422. }
  423. int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
  424. {
  425. int val;
  426. if (!fdt_gpio_isvalid(gpio))
  427. return -1;
  428. val = gpio_get_value(gpio->gpio);
  429. return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
  430. }
  431. int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
  432. {
  433. if (!fdt_gpio_isvalid(gpio))
  434. return -1;
  435. val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
  436. return gpio_set_value(gpio->gpio, val);
  437. }
  438. int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
  439. {
  440. /*
  441. * Return success if there is no GPIO defined. This is used for
  442. * optional GPIOs)
  443. */
  444. if (!fdt_gpio_isvalid(gpio))
  445. return 0;
  446. if (gpio_request(gpio->gpio, gpio->name))
  447. return -1;
  448. return 0;
  449. }
  450. int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
  451. u8 *array, int count)
  452. {
  453. const u8 *cell;
  454. int err;
  455. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  456. if (!err)
  457. memcpy(array, cell, count);
  458. return err;
  459. }
  460. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  461. const char *prop_name, int count)
  462. {
  463. const u8 *cell;
  464. int err;
  465. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  466. if (err)
  467. return NULL;
  468. return cell;
  469. }
  470. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  471. int default_val)
  472. {
  473. int config_node;
  474. debug("%s: %s\n", __func__, prop_name);
  475. config_node = fdt_path_offset(blob, "/config");
  476. if (config_node < 0)
  477. return default_val;
  478. return fdtdec_get_int(blob, config_node, prop_name, default_val);
  479. }
  480. int fdtdec_get_config_bool(const void *blob, const char *prop_name)
  481. {
  482. int config_node;
  483. const void *prop;
  484. debug("%s: %s\n", __func__, prop_name);
  485. config_node = fdt_path_offset(blob, "/config");
  486. if (config_node < 0)
  487. return 0;
  488. prop = fdt_get_property(blob, config_node, prop_name, NULL);
  489. return prop != NULL;
  490. }
  491. char *fdtdec_get_config_string(const void *blob, const char *prop_name)
  492. {
  493. const char *nodep;
  494. int nodeoffset;
  495. int len;
  496. debug("%s: %s\n", __func__, prop_name);
  497. nodeoffset = fdt_path_offset(blob, "/config");
  498. if (nodeoffset < 0)
  499. return NULL;
  500. nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
  501. if (!nodep)
  502. return NULL;
  503. return (char *)nodep;
  504. }
  505. int fdtdec_decode_region(const void *blob, int node,
  506. const char *prop_name, void **ptrp, size_t *size)
  507. {
  508. const fdt_addr_t *cell;
  509. int len;
  510. debug("%s: %s\n", __func__, prop_name);
  511. cell = fdt_getprop(blob, node, prop_name, &len);
  512. if (!cell || (len != sizeof(fdt_addr_t) * 2))
  513. return -1;
  514. *ptrp = (void *)fdt_addr_to_cpu(*cell);
  515. *size = fdt_size_to_cpu(cell[1]);
  516. debug("%s: size=%zx\n", __func__, *size);
  517. return 0;
  518. }