fdtdec.c 15 KB

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