fdtdec.c 15 KB

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