fdtdec.c 15 KB

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