fdtdec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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_alias(const void *blob, const char *name,
  120. enum fdt_compat_id id, int *upto)
  121. {
  122. #define MAX_STR_LEN 20
  123. char str[MAX_STR_LEN + 20];
  124. int node, err;
  125. /* snprintf() is not available */
  126. assert(strlen(name) < MAX_STR_LEN);
  127. sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
  128. node = find_alias_node(blob, str);
  129. if (node < 0)
  130. return node;
  131. err = fdt_node_check_compatible(blob, node, compat_names[id]);
  132. if (err < 0)
  133. return err;
  134. if (err)
  135. return -FDT_ERR_NOTFOUND;
  136. (*upto)++;
  137. return node;
  138. }
  139. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  140. enum fdt_compat_id id, int *node_list, int maxcount)
  141. {
  142. memset(node_list, '\0', sizeof(*node_list) * maxcount);
  143. return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
  144. }
  145. /* TODO: Can we tighten this code up a little? */
  146. int fdtdec_add_aliases_for_id(const void *blob, const char *name,
  147. enum fdt_compat_id id, int *node_list, int maxcount)
  148. {
  149. int name_len = strlen(name);
  150. int nodes[maxcount];
  151. int num_found = 0;
  152. int offset, node;
  153. int alias_node;
  154. int count;
  155. int i, j;
  156. /* find the alias node if present */
  157. alias_node = fdt_path_offset(blob, "/aliases");
  158. /*
  159. * start with nothing, and we can assume that the root node can't
  160. * match
  161. */
  162. memset(nodes, '\0', sizeof(nodes));
  163. /* First find all the compatible nodes */
  164. for (node = count = 0; node >= 0 && count < maxcount;) {
  165. node = fdtdec_next_compatible(blob, node, id);
  166. if (node >= 0)
  167. nodes[count++] = node;
  168. }
  169. if (node >= 0)
  170. debug("%s: warning: maxcount exceeded with alias '%s'\n",
  171. __func__, name);
  172. /* Now find all the aliases */
  173. for (offset = fdt_first_property_offset(blob, alias_node);
  174. offset > 0;
  175. offset = fdt_next_property_offset(blob, offset)) {
  176. const struct fdt_property *prop;
  177. const char *path;
  178. int number;
  179. int found;
  180. node = 0;
  181. prop = fdt_get_property_by_offset(blob, offset, NULL);
  182. path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  183. if (prop->len && 0 == strncmp(path, name, name_len))
  184. node = fdt_path_offset(blob, prop->data);
  185. if (node <= 0)
  186. continue;
  187. /* Get the alias number */
  188. number = simple_strtoul(path + name_len, NULL, 10);
  189. if (number < 0 || number >= maxcount) {
  190. debug("%s: warning: alias '%s' is out of range\n",
  191. __func__, path);
  192. continue;
  193. }
  194. /* Make sure the node we found is actually in our list! */
  195. found = -1;
  196. for (j = 0; j < count; j++)
  197. if (nodes[j] == node) {
  198. found = j;
  199. break;
  200. }
  201. if (found == -1) {
  202. debug("%s: warning: alias '%s' points to a node "
  203. "'%s' that is missing or is not compatible "
  204. " with '%s'\n", __func__, path,
  205. fdt_get_name(blob, node, NULL),
  206. compat_names[id]);
  207. continue;
  208. }
  209. /*
  210. * Add this node to our list in the right place, and mark
  211. * it as done.
  212. */
  213. if (fdtdec_get_is_enabled(blob, node)) {
  214. if (node_list[number]) {
  215. debug("%s: warning: alias '%s' requires that "
  216. "a node be placed in the list in a "
  217. "position which is already filled by "
  218. "node '%s'\n", __func__, path,
  219. fdt_get_name(blob, node, NULL));
  220. continue;
  221. }
  222. node_list[number] = node;
  223. if (number >= num_found)
  224. num_found = number + 1;
  225. }
  226. nodes[found] = 0;
  227. }
  228. /* Add any nodes not mentioned by an alias */
  229. for (i = j = 0; i < maxcount; i++) {
  230. if (!node_list[i]) {
  231. for (; j < maxcount; j++)
  232. if (nodes[j] &&
  233. fdtdec_get_is_enabled(blob, nodes[j]))
  234. break;
  235. /* Have we run out of nodes to add? */
  236. if (j == maxcount)
  237. break;
  238. assert(!node_list[i]);
  239. node_list[i] = nodes[j++];
  240. if (i >= num_found)
  241. num_found = i + 1;
  242. }
  243. }
  244. return num_found;
  245. }
  246. int fdtdec_check_fdt(void)
  247. {
  248. /*
  249. * We must have an FDT, but we cannot panic() yet since the console
  250. * is not ready. So for now, just assert(). Boards which need an early
  251. * FDT (prior to console ready) will need to make their own
  252. * arrangements and do their own checks.
  253. */
  254. assert(!fdtdec_prepare_fdt());
  255. return 0;
  256. }
  257. /*
  258. * This function is a little odd in that it accesses global data. At some
  259. * point if the architecture board.c files merge this will make more sense.
  260. * Even now, it is common code.
  261. */
  262. int fdtdec_prepare_fdt(void)
  263. {
  264. if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob)) {
  265. printf("No valid FDT found - please append one to U-Boot "
  266. "binary, use u-boot-dtb.bin or define "
  267. "CONFIG_OF_EMBED\n");
  268. return -1;
  269. }
  270. return 0;
  271. }
  272. int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
  273. {
  274. const u32 *phandle;
  275. int lookup;
  276. phandle = fdt_getprop(blob, node, prop_name, NULL);
  277. if (!phandle)
  278. return -FDT_ERR_NOTFOUND;
  279. lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
  280. return lookup;
  281. }
  282. /**
  283. * Look up a property in a node and check that it has a minimum length.
  284. *
  285. * @param blob FDT blob
  286. * @param node node to examine
  287. * @param prop_name name of property to find
  288. * @param min_len minimum property length in bytes
  289. * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
  290. found, or -FDT_ERR_BADLAYOUT if not enough data
  291. * @return pointer to cell, which is only valid if err == 0
  292. */
  293. static const void *get_prop_check_min_len(const void *blob, int node,
  294. const char *prop_name, int min_len, int *err)
  295. {
  296. const void *cell;
  297. int len;
  298. debug("%s: %s\n", __func__, prop_name);
  299. cell = fdt_getprop(blob, node, prop_name, &len);
  300. if (!cell)
  301. *err = -FDT_ERR_NOTFOUND;
  302. else if (len < min_len)
  303. *err = -FDT_ERR_BADLAYOUT;
  304. else
  305. *err = 0;
  306. return cell;
  307. }
  308. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  309. u32 *array, int count)
  310. {
  311. const u32 *cell;
  312. int i, err = 0;
  313. debug("%s: %s\n", __func__, prop_name);
  314. cell = get_prop_check_min_len(blob, node, prop_name,
  315. sizeof(u32) * count, &err);
  316. if (!err) {
  317. for (i = 0; i < count; i++)
  318. array[i] = fdt32_to_cpu(cell[i]);
  319. }
  320. return err;
  321. }
  322. const u32 *fdtdec_locate_array(const void *blob, int node,
  323. const char *prop_name, int count)
  324. {
  325. const u32 *cell;
  326. int err;
  327. cell = get_prop_check_min_len(blob, node, prop_name,
  328. sizeof(u32) * count, &err);
  329. return err ? NULL : cell;
  330. }
  331. int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
  332. {
  333. const s32 *cell;
  334. int len;
  335. debug("%s: %s\n", __func__, prop_name);
  336. cell = fdt_getprop(blob, node, prop_name, &len);
  337. return cell != NULL;
  338. }
  339. /**
  340. * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
  341. * terminating item.
  342. *
  343. * @param blob FDT blob to use
  344. * @param node Node to look at
  345. * @param prop_name Node property name
  346. * @param gpio Array of gpio elements to fill from FDT. This will be
  347. * untouched if either 0 or an error is returned
  348. * @param max_count Maximum number of elements allowed
  349. * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
  350. * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
  351. */
  352. static int fdtdec_decode_gpios(const void *blob, int node,
  353. const char *prop_name, struct fdt_gpio_state *gpio,
  354. int max_count)
  355. {
  356. const struct fdt_property *prop;
  357. const u32 *cell;
  358. const char *name;
  359. int len, i;
  360. debug("%s: %s\n", __func__, prop_name);
  361. assert(max_count > 0);
  362. prop = fdt_get_property(blob, node, prop_name, &len);
  363. if (!prop) {
  364. debug("FDT: %s: property '%s' missing\n", __func__, prop_name);
  365. return -FDT_ERR_NOTFOUND;
  366. }
  367. /* We will use the name to tag the GPIO */
  368. name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  369. cell = (u32 *)prop->data;
  370. len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
  371. if (len > max_count) {
  372. debug("FDT: %s: too many GPIOs / cells for "
  373. "property '%s'\n", __func__, prop_name);
  374. return -FDT_ERR_BADLAYOUT;
  375. }
  376. /* Read out the GPIO data from the cells */
  377. for (i = 0; i < len; i++, cell += 3) {
  378. gpio[i].gpio = fdt32_to_cpu(cell[1]);
  379. gpio[i].flags = fdt32_to_cpu(cell[2]);
  380. gpio[i].name = name;
  381. }
  382. return len;
  383. }
  384. int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
  385. struct fdt_gpio_state *gpio)
  386. {
  387. int err;
  388. debug("%s: %s\n", __func__, prop_name);
  389. gpio->gpio = FDT_GPIO_NONE;
  390. gpio->name = NULL;
  391. err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
  392. return err == 1 ? 0 : err;
  393. }
  394. int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
  395. {
  396. /*
  397. * Return success if there is no GPIO defined. This is used for
  398. * optional GPIOs)
  399. */
  400. if (!fdt_gpio_isvalid(gpio))
  401. return 0;
  402. if (gpio_request(gpio->gpio, gpio->name))
  403. return -1;
  404. return 0;
  405. }