fdt_support.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * (C) Copyright 2007
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <linux/ctype.h>
  25. #include <linux/types.h>
  26. #ifdef CONFIG_OF_LIBFDT
  27. #include <asm/global_data.h>
  28. #include <fdt.h>
  29. #include <libfdt.h>
  30. #include <fdt_support.h>
  31. #ifdef CONFIG_OF_BOARD_SETUP
  32. void ft_board_setup(void *blob, bd_t *bd);
  33. #endif
  34. /*
  35. * Global data (for the gd->bd)
  36. */
  37. DECLARE_GLOBAL_DATA_PTR;
  38. /*
  39. * fdt points to our working device tree.
  40. */
  41. struct fdt_header *fdt;
  42. /********************************************************************/
  43. int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
  44. {
  45. bd_t *bd = gd->bd;
  46. int nodeoffset;
  47. int err;
  48. u32 tmp; /* used to set 32 bit integer properties */
  49. char *str; /* used to set string properties */
  50. err = fdt_check_header(fdt);
  51. if (err < 0) {
  52. printf("libfdt: %s\n", fdt_strerror(err));
  53. return err;
  54. }
  55. #ifdef CONFIG_OF_BOARD_SETUP
  56. /*
  57. * ft_board_setup() sets various board-specific properties to
  58. * the proper values.
  59. *
  60. * STRICTLY SPEAKING, this is out of place, but it isn't clear
  61. * where a better place would be.
  62. */
  63. ft_board_setup(fdt, bd);
  64. #endif
  65. if (initrd_start && initrd_end) {
  66. struct fdt_reserve_entry re;
  67. int used;
  68. int total;
  69. int j;
  70. err = fdt_num_reservemap(fdt, &used, &total);
  71. if (err < 0) {
  72. printf("libfdt: %s\n", fdt_strerror(err));
  73. return err;
  74. }
  75. if (used >= total) {
  76. printf("WARNING fdt_chosen: "
  77. "no room in the reserved map (%d of %d)\n",
  78. used, total);
  79. return -1;
  80. }
  81. /*
  82. * Look for an existing entry and update it. If we don't find
  83. * the entry, we will j be the next available slot.
  84. */
  85. for (j = 0; j < used; j++) {
  86. err = fdt_get_reservemap(fdt, j, &re);
  87. if (re.address == initrd_start) {
  88. break;
  89. }
  90. }
  91. err = fdt_replace_reservemap_entry(fdt, j,
  92. initrd_start, initrd_end - initrd_start + 1);
  93. if (err < 0) {
  94. printf("libfdt: %s\n", fdt_strerror(err));
  95. return err;
  96. }
  97. }
  98. /*
  99. * Find the "chosen" node.
  100. */
  101. nodeoffset = fdt_find_node_by_path (fdt, "/chosen");
  102. /*
  103. * If we have a "chosen" node already the "force the writing"
  104. * is not set, our job is done.
  105. */
  106. if ((nodeoffset >= 0) && !force)
  107. return 0;
  108. /*
  109. * No "chosen" node in the blob: create it.
  110. */
  111. if (nodeoffset < 0) {
  112. /*
  113. * Create a new node "/chosen" (offset 0 is root level)
  114. */
  115. nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
  116. if (nodeoffset < 0) {
  117. printf("WARNING fdt_chosen: "
  118. "could not create the \"/chosen node\" "
  119. "(libfdt error %s).\n",
  120. fdt_strerror(nodeoffset));
  121. return nodeoffset;
  122. }
  123. }
  124. /*
  125. * Update pre-existing properties, create them if non-existant.
  126. */
  127. str = getenv("bootargs");
  128. if (str != NULL) {
  129. err = fdt_setprop(fdt, nodeoffset,
  130. "bootargs", str, strlen(str)+1);
  131. if (err < 0)
  132. printf("WARNING fdt_chosen: "
  133. "could not set \"bootargs\" "
  134. "(libfdt error %s).\n",
  135. fdt_strerror(err));
  136. }
  137. if (initrd_start && initrd_end) {
  138. tmp = __cpu_to_be32(initrd_start);
  139. err = fdt_setprop(fdt, nodeoffset,
  140. "linux,initrd-start", &tmp, sizeof(tmp));
  141. if (err < 0)
  142. printf("WARNING fdt_chosen: "
  143. "could not set \"linux,initrd-start\" "
  144. "(libfdt error %s).\n",
  145. fdt_strerror(err));
  146. tmp = __cpu_to_be32(initrd_end);
  147. err = fdt_setprop(fdt, nodeoffset,
  148. "linux,initrd-end", &tmp, sizeof(tmp));
  149. if (err < 0)
  150. printf("WARNING fdt_chosen: "
  151. "could not set \"linux,initrd-end\" "
  152. "(libfdt error %s).\n",
  153. fdt_strerror(err));
  154. }
  155. #ifdef OF_STDOUT_PATH
  156. err = fdt_setprop(fdt, nodeoffset,
  157. "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
  158. if (err < 0)
  159. printf("WARNING fdt_chosen: "
  160. "could not set \"linux,stdout-path\" "
  161. "(libfdt error %s).\n",
  162. fdt_strerror(err));
  163. #endif
  164. return err;
  165. }
  166. /********************************************************************/
  167. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  168. /* Function that returns a character from the environment */
  169. extern uchar(*env_get_char) (int);
  170. int fdt_env(void *fdt)
  171. {
  172. int nodeoffset;
  173. int err;
  174. int k, nxt;
  175. int i;
  176. static char tmpenv[256];
  177. err = fdt_check_header(fdt);
  178. if (err < 0) {
  179. printf("libfdt: %s\n", fdt_strerror(err));
  180. return err;
  181. }
  182. /*
  183. * See if we already have a "u-boot-env" node, delete it if so.
  184. * Then create a new empty node.
  185. */
  186. nodeoffset = fdt_find_node_by_path (fdt, "/u-boot-env");
  187. if (nodeoffset >= 0) {
  188. err = fdt_del_node(fdt, nodeoffset);
  189. if (err < 0) {
  190. printf("libfdt: %s\n", fdt_strerror(err));
  191. return err;
  192. }
  193. }
  194. /*
  195. * Create a new node "/u-boot-env" (offset 0 is root level)
  196. */
  197. nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
  198. if (nodeoffset < 0) {
  199. printf("WARNING fdt_env: "
  200. "could not create the \"/u-boot-env node\" "
  201. "(libfdt error %s).\n",
  202. fdt_strerror(nodeoffset));
  203. return nodeoffset;
  204. }
  205. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  206. char *s, *lval, *rval;
  207. /*
  208. * Find the end of the name=definition
  209. */
  210. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  211. ;
  212. s = tmpenv;
  213. for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
  214. *s++ = env_get_char(k);
  215. *s++ = '\0';
  216. lval = tmpenv;
  217. /*
  218. * Find the first '=': it separates the name from the value
  219. */
  220. s = strchr(tmpenv, '=');
  221. if (s != NULL) {
  222. *s++ = '\0';
  223. rval = s;
  224. } else
  225. continue;
  226. err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
  227. if (err < 0) {
  228. printf("WARNING fdt_env: "
  229. "could not set \"%s\" "
  230. "(libfdt error %s).\n",
  231. lval, fdt_strerror(err));
  232. return err;
  233. }
  234. }
  235. return 0;
  236. }
  237. #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
  238. /********************************************************************/
  239. #ifdef CONFIG_OF_HAS_BD_T
  240. #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
  241. static const struct {
  242. const char *name;
  243. int offset;
  244. } bd_map[] = {
  245. BDM(memstart),
  246. BDM(memsize),
  247. BDM(flashstart),
  248. BDM(flashsize),
  249. BDM(flashoffset),
  250. BDM(sramstart),
  251. BDM(sramsize),
  252. #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
  253. || defined(CONFIG_E500)
  254. BDM(immr_base),
  255. #endif
  256. #if defined(CONFIG_MPC5xxx)
  257. BDM(mbar_base),
  258. #endif
  259. #if defined(CONFIG_MPC83XX)
  260. BDM(immrbar),
  261. #endif
  262. #if defined(CONFIG_MPC8220)
  263. BDM(mbar_base),
  264. BDM(inpfreq),
  265. BDM(pcifreq),
  266. BDM(pevfreq),
  267. BDM(flbfreq),
  268. BDM(vcofreq),
  269. #endif
  270. BDM(bootflags),
  271. BDM(ip_addr),
  272. BDM(intfreq),
  273. BDM(busfreq),
  274. #ifdef CONFIG_CPM2
  275. BDM(cpmfreq),
  276. BDM(brgfreq),
  277. BDM(sccfreq),
  278. BDM(vco),
  279. #endif
  280. #if defined(CONFIG_MPC5xxx)
  281. BDM(ipbfreq),
  282. BDM(pcifreq),
  283. #endif
  284. BDM(baudrate),
  285. };
  286. int fdt_bd_t(void *fdt)
  287. {
  288. bd_t *bd = gd->bd;
  289. int nodeoffset;
  290. int err;
  291. u32 tmp; /* used to set 32 bit integer properties */
  292. int i;
  293. err = fdt_check_header(fdt);
  294. if (err < 0) {
  295. printf("libfdt: %s\n", fdt_strerror(err));
  296. return err;
  297. }
  298. /*
  299. * See if we already have a "bd_t" node, delete it if so.
  300. * Then create a new empty node.
  301. */
  302. nodeoffset = fdt_find_node_by_path (fdt, "/bd_t");
  303. if (nodeoffset >= 0) {
  304. err = fdt_del_node(fdt, nodeoffset);
  305. if (err < 0) {
  306. printf("libfdt: %s\n", fdt_strerror(err));
  307. return err;
  308. }
  309. }
  310. /*
  311. * Create a new node "/bd_t" (offset 0 is root level)
  312. */
  313. nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
  314. if (nodeoffset < 0) {
  315. printf("WARNING fdt_bd_t: "
  316. "could not create the \"/bd_t node\" "
  317. "(libfdt error %s).\n",
  318. fdt_strerror(nodeoffset));
  319. printf("libfdt: %s\n", fdt_strerror(nodeoffset));
  320. return nodeoffset;
  321. }
  322. /*
  323. * Use the string/pointer structure to create the entries...
  324. */
  325. for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
  326. tmp = cpu_to_be32(getenv("bootargs"));
  327. err = fdt_setprop(fdt, nodeoffset,
  328. bd_map[i].name, &tmp, sizeof(tmp));
  329. if (err < 0)
  330. printf("WARNING fdt_bd_t: "
  331. "could not set \"%s\" "
  332. "(libfdt error %s).\n",
  333. bd_map[i].name, fdt_strerror(err));
  334. }
  335. /*
  336. * Add a couple of oddball entries...
  337. */
  338. err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
  339. if (err < 0)
  340. printf("WARNING fdt_bd_t: "
  341. "could not set \"enetaddr\" "
  342. "(libfdt error %s).\n",
  343. fdt_strerror(err));
  344. err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
  345. if (err < 0)
  346. printf("WARNING fdt_bd_t: "
  347. "could not set \"ethspeed\" "
  348. "(libfdt error %s).\n",
  349. fdt_strerror(err));
  350. return 0;
  351. }
  352. #endif /* ifdef CONFIG_OF_HAS_BD_T */
  353. #endif /* CONFIG_OF_LIBFDT */