fdtdec.c 13 KB

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