fdtdec.c 15 KB

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