fdtdec.c 15 KB

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