fdtdec.c 15 KB

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