fdtdec.c 13 KB

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