fdtdec.c 12 KB

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