fdtdec.c 15 KB

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