fdtdec.c 13 KB

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