fdtdec.c 15 KB

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