fdtdec.c 11 KB

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