fdtdec.c 11 KB

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