fdtdec.c 14 KB

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