fdtdec.c 12 KB

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