fdt_support.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 (%s).\n",
  119. fdt_strerror(nodeoffset));
  120. return nodeoffset;
  121. }
  122. }
  123. /*
  124. * Update pre-existing properties, create them if non-existant.
  125. */
  126. str = getenv("bootargs");
  127. if (str != NULL) {
  128. err = fdt_setprop(fdt, nodeoffset,
  129. "bootargs", str, strlen(str)+1);
  130. if (err < 0)
  131. printf("WARNING fdt_chosen: "
  132. "could not set bootargs (%s).\n",
  133. fdt_strerror(err));
  134. }
  135. if (initrd_start && initrd_end) {
  136. tmp = __cpu_to_be32(initrd_start);
  137. err = fdt_setprop(fdt, nodeoffset,
  138. "linux,initrd-start", &tmp, sizeof(tmp));
  139. if (err < 0)
  140. printf("WARNING fdt_chosen: "
  141. "could not set linux,initrd-start (%s).\n",
  142. fdt_strerror(err));
  143. tmp = __cpu_to_be32(initrd_end);
  144. err = fdt_setprop(fdt, nodeoffset,
  145. "linux,initrd-end", &tmp, sizeof(tmp));
  146. if (err < 0)
  147. printf("WARNING fdt_chosen: "
  148. "could not set linux,initrd-end (%s).\n",
  149. fdt_strerror(err));
  150. }
  151. #ifdef OF_STDOUT_PATH
  152. err = fdt_setprop(fdt, nodeoffset,
  153. "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
  154. if (err < 0)
  155. printf("WARNING fdt_chosen: "
  156. "could not set linux,stdout-path (%s).\n",
  157. fdt_strerror(err));
  158. #endif
  159. return err;
  160. }
  161. /********************************************************************/
  162. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  163. /* Function that returns a character from the environment */
  164. extern uchar(*env_get_char) (int);
  165. int fdt_env(void *fdt)
  166. {
  167. int nodeoffset;
  168. int err;
  169. int k, nxt;
  170. int i;
  171. static char tmpenv[256];
  172. err = fdt_check_header(fdt);
  173. if (err < 0) {
  174. printf("libfdt: %s\n", fdt_strerror(err));
  175. return err;
  176. }
  177. /*
  178. * See if we already have a "u-boot-env" node, delete it if so.
  179. * Then create a new empty node.
  180. */
  181. nodeoffset = fdt_find_node_by_path (fdt, "/u-boot-env");
  182. if (nodeoffset >= 0) {
  183. err = fdt_del_node(fdt, nodeoffset);
  184. if (err < 0) {
  185. printf("libfdt: %s\n", fdt_strerror(err));
  186. return err;
  187. }
  188. }
  189. /*
  190. * Create a new node "/u-boot-env" (offset 0 is root level)
  191. */
  192. nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
  193. if (nodeoffset < 0) {
  194. printf("WARNING fdt_env: "
  195. "could not create the /u-boot-env node (%s).\n",
  196. fdt_strerror(nodeoffset));
  197. return nodeoffset;
  198. }
  199. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  200. char *s, *lval, *rval;
  201. /*
  202. * Find the end of the name=definition
  203. */
  204. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  205. ;
  206. s = tmpenv;
  207. for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
  208. *s++ = env_get_char(k);
  209. *s++ = '\0';
  210. lval = tmpenv;
  211. /*
  212. * Find the first '=': it separates the name from the value
  213. */
  214. s = strchr(tmpenv, '=');
  215. if (s != NULL) {
  216. *s++ = '\0';
  217. rval = s;
  218. } else
  219. continue;
  220. err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
  221. if (err < 0) {
  222. printf("WARNING fdt_env: "
  223. "could not set %s (%s).\n",
  224. lval, fdt_strerror(err));
  225. return err;
  226. }
  227. }
  228. return 0;
  229. }
  230. #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
  231. /********************************************************************/
  232. #ifdef CONFIG_OF_HAS_BD_T
  233. #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
  234. static const struct {
  235. const char *name;
  236. int offset;
  237. } bd_map[] = {
  238. BDM(memstart),
  239. BDM(memsize),
  240. BDM(flashstart),
  241. BDM(flashsize),
  242. BDM(flashoffset),
  243. BDM(sramstart),
  244. BDM(sramsize),
  245. #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
  246. || defined(CONFIG_E500)
  247. BDM(immr_base),
  248. #endif
  249. #if defined(CONFIG_MPC5xxx)
  250. BDM(mbar_base),
  251. #endif
  252. #if defined(CONFIG_MPC83XX)
  253. BDM(immrbar),
  254. #endif
  255. #if defined(CONFIG_MPC8220)
  256. BDM(mbar_base),
  257. BDM(inpfreq),
  258. BDM(pcifreq),
  259. BDM(pevfreq),
  260. BDM(flbfreq),
  261. BDM(vcofreq),
  262. #endif
  263. BDM(bootflags),
  264. BDM(ip_addr),
  265. BDM(intfreq),
  266. BDM(busfreq),
  267. #ifdef CONFIG_CPM2
  268. BDM(cpmfreq),
  269. BDM(brgfreq),
  270. BDM(sccfreq),
  271. BDM(vco),
  272. #endif
  273. #if defined(CONFIG_MPC5xxx)
  274. BDM(ipbfreq),
  275. BDM(pcifreq),
  276. #endif
  277. BDM(baudrate),
  278. };
  279. int fdt_bd_t(void *fdt)
  280. {
  281. bd_t *bd = gd->bd;
  282. int nodeoffset;
  283. int err;
  284. u32 tmp; /* used to set 32 bit integer properties */
  285. int i;
  286. err = fdt_check_header(fdt);
  287. if (err < 0) {
  288. printf("libfdt: %s\n", fdt_strerror(err));
  289. return err;
  290. }
  291. /*
  292. * See if we already have a "bd_t" node, delete it if so.
  293. * Then create a new empty node.
  294. */
  295. nodeoffset = fdt_find_node_by_path (fdt, "/bd_t");
  296. if (nodeoffset >= 0) {
  297. err = fdt_del_node(fdt, nodeoffset);
  298. if (err < 0) {
  299. printf("libfdt: %s\n", fdt_strerror(err));
  300. return err;
  301. }
  302. }
  303. /*
  304. * Create a new node "/bd_t" (offset 0 is root level)
  305. */
  306. nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
  307. if (nodeoffset < 0) {
  308. printf("WARNING fdt_bd_t: "
  309. "could not create the /bd_t node (%s).\n",
  310. fdt_strerror(nodeoffset));
  311. printf("libfdt: %s\n", fdt_strerror(nodeoffset));
  312. return nodeoffset;
  313. }
  314. /*
  315. * Use the string/pointer structure to create the entries...
  316. */
  317. for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
  318. tmp = cpu_to_be32(getenv("bootargs"));
  319. err = fdt_setprop(fdt, nodeoffset,
  320. bd_map[i].name, &tmp, sizeof(tmp));
  321. if (err < 0)
  322. printf("WARNING fdt_bd_t: "
  323. "could not set %s (%s).\n",
  324. bd_map[i].name, fdt_strerror(err));
  325. }
  326. /*
  327. * Add a couple of oddball entries...
  328. */
  329. err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
  330. if (err < 0)
  331. printf("WARNING fdt_bd_t: "
  332. "could not set enetaddr (%s).\n",
  333. fdt_strerror(err));
  334. err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
  335. if (err < 0)
  336. printf("WARNING fdt_bd_t: "
  337. "could not set ethspeed (%s).\n",
  338. fdt_strerror(err));
  339. return 0;
  340. }
  341. #endif /* ifdef CONFIG_OF_HAS_BD_T */
  342. #endif /* CONFIG_OF_LIBFDT */