fdtdec.c 15 KB

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