fdt_support.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. /*
  32. * Global data (for the gd->bd)
  33. */
  34. DECLARE_GLOBAL_DATA_PTR;
  35. /********************************************************************/
  36. int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
  37. {
  38. bd_t *bd = gd->bd;
  39. int nodeoffset;
  40. int err;
  41. u32 tmp; /* used to set 32 bit integer properties */
  42. char *str; /* used to set string properties */
  43. ulong clock;
  44. err = fdt_check_header(fdt);
  45. if (err < 0) {
  46. printf("libfdt: %s\n", fdt_strerror(err));
  47. return err;
  48. }
  49. #warning "Don't double-add the reserved map"
  50. if (initrd_start && initrd_end) {
  51. err = fdt_add_reservemap_entry(fdt,
  52. initrd_start, initrd_end - initrd_start + 1);
  53. if (err < 0) {
  54. printf("libfdt: %s\n", fdt_strerror(err));
  55. return err;
  56. }
  57. }
  58. /*
  59. * Find the "chosen" node.
  60. */
  61. nodeoffset = fdt_path_offset (fdt, "/chosen");
  62. /*
  63. * If we have a "chosen" node already the "force the writing"
  64. * is not set, our job is done.
  65. */
  66. if ((nodeoffset >= 0) && !force)
  67. return 0;
  68. /*
  69. * No "chosen" node in the blob: create it.
  70. */
  71. if (nodeoffset < 0) {
  72. /*
  73. * Create a new node "/chosen" (offset 0 is root level)
  74. */
  75. nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
  76. if (nodeoffset < 0) {
  77. printf("libfdt: %s\n", fdt_strerror(nodeoffset));
  78. return nodeoffset;
  79. }
  80. }
  81. /*
  82. * Update pre-existing properties, create them if non-existant.
  83. */
  84. str = getenv("bootargs");
  85. if (str != NULL) {
  86. err = fdt_setprop(fdt, nodeoffset, "bootargs", str, strlen(str)+1);
  87. if (err < 0)
  88. printf("libfdt: %s\n", fdt_strerror(err));
  89. }
  90. if (initrd_start && initrd_end) {
  91. tmp = __cpu_to_be32(initrd_start);
  92. err = fdt_setprop(fdt, nodeoffset, "linux,initrd-start", &tmp, sizeof(tmp));
  93. if (err < 0)
  94. printf("libfdt: %s\n", fdt_strerror(err));
  95. tmp = __cpu_to_be32(initrd_end);
  96. err = fdt_setprop(fdt, nodeoffset, "linux,initrd-end", &tmp, sizeof(tmp));
  97. if (err < 0)
  98. printf("libfdt: %s\n", fdt_strerror(err));
  99. }
  100. #ifdef OF_STDOUT_PATH
  101. err = fdt_setprop(fdt, nodeoffset, "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
  102. if (err < 0)
  103. printf("libfdt: %s\n", fdt_strerror(err));
  104. #endif
  105. nodeoffset = fdt_path_offset (fdt, "/cpus");
  106. if (nodeoffset >= 0) {
  107. clock = cpu_to_be32(bd->bi_intfreq);
  108. err = fdt_setprop(fdt, nodeoffset, "clock-frequency", &clock, 4);
  109. if (err < 0)
  110. printf("libfdt: %s\n", fdt_strerror(err));
  111. }
  112. #ifdef OF_TBCLK
  113. nodeoffset = fdt_path_offset (fdt, "/cpus/" OF_CPU "/timebase-frequency");
  114. if (nodeoffset >= 0) {
  115. clock = cpu_to_be32(OF_TBCLK);
  116. err = fdt_setprop(fdt, nodeoffset, "clock-frequency", &clock, 4);
  117. if (err < 0)
  118. printf("libfdt: %s\n", fdt_strerror(err));
  119. }
  120. #endif
  121. return err;
  122. }
  123. /********************************************************************/
  124. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  125. /* Function that returns a character from the environment */
  126. extern uchar(*env_get_char) (int);
  127. int fdt_env(void *fdt)
  128. {
  129. int nodeoffset;
  130. int err;
  131. int k, nxt;
  132. int i;
  133. static char tmpenv[256];
  134. err = fdt_check_header(fdt);
  135. if (err < 0) {
  136. printf("libfdt: %s\n", fdt_strerror(err));
  137. return err;
  138. }
  139. /*
  140. * See if we already have a "u-boot-env" node, delete it if so.
  141. * Then create a new empty node.
  142. */
  143. nodeoffset = fdt_path_offset (fdt, "/u-boot-env");
  144. if (nodeoffset >= 0) {
  145. err = fdt_del_node(fdt, nodeoffset);
  146. if (err < 0) {
  147. printf("libfdt: %s\n", fdt_strerror(err));
  148. return err;
  149. }
  150. }
  151. /*
  152. * Create a new node "/u-boot-env" (offset 0 is root level)
  153. */
  154. nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
  155. if (nodeoffset < 0) {
  156. printf("libfdt: %s\n", fdt_strerror(nodeoffset));
  157. return nodeoffset;
  158. }
  159. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  160. char *s, *lval, *rval;
  161. /*
  162. * Find the end of the name=definition
  163. */
  164. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  165. ;
  166. s = tmpenv;
  167. for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
  168. *s++ = env_get_char(k);
  169. *s++ = '\0';
  170. lval = tmpenv;
  171. /*
  172. * Find the first '=': it separates the name from the value
  173. */
  174. s = strchr(tmpenv, '=');
  175. if (s != NULL) {
  176. *s++ = '\0';
  177. rval = s;
  178. } else
  179. continue;
  180. err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
  181. if (err < 0) {
  182. printf("libfdt: %s\n", lval, fdt_strerror(err));
  183. return err;
  184. }
  185. }
  186. return 0;
  187. }
  188. #endif /* CONFIG_OF_HAS_UBOOT_ENV */
  189. /********************************************************************/
  190. #ifdef CONFIG_OF_HAS_BD_T
  191. #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
  192. static const struct {
  193. const char *name;
  194. int offset;
  195. } bd_map[] = {
  196. BDM(memstart),
  197. BDM(memsize),
  198. BDM(flashstart),
  199. BDM(flashsize),
  200. BDM(flashoffset),
  201. BDM(sramstart),
  202. BDM(sramsize),
  203. #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
  204. || defined(CONFIG_E500)
  205. BDM(immr_base),
  206. #endif
  207. #if defined(CONFIG_MPC5xxx)
  208. BDM(mbar_base),
  209. #endif
  210. #if defined(CONFIG_MPC83XX)
  211. BDM(immrbar),
  212. #endif
  213. #if defined(CONFIG_MPC8220)
  214. BDM(mbar_base),
  215. BDM(inpfreq),
  216. BDM(pcifreq),
  217. BDM(pevfreq),
  218. BDM(flbfreq),
  219. BDM(vcofreq),
  220. #endif
  221. BDM(bootflags),
  222. BDM(ip_addr),
  223. BDM(intfreq),
  224. BDM(busfreq),
  225. #ifdef CONFIG_CPM2
  226. BDM(cpmfreq),
  227. BDM(brgfreq),
  228. BDM(sccfreq),
  229. BDM(vco),
  230. #endif
  231. #if defined(CONFIG_MPC5xxx)
  232. BDM(ipbfreq),
  233. BDM(pcifreq),
  234. #endif
  235. BDM(baudrate),
  236. };
  237. int fdt_bd_t(void *fdt)
  238. {
  239. bd_t *bd = gd->bd;
  240. int nodeoffset;
  241. int err;
  242. u32 tmp; /* used to set 32 bit integer properties */
  243. int i;
  244. err = fdt_check_header(fdt);
  245. if (err < 0) {
  246. printf("libfdt: %s\n", fdt_strerror(err));
  247. return err;
  248. }
  249. /*
  250. * See if we already have a "bd_t" node, delete it if so.
  251. * Then create a new empty node.
  252. */
  253. nodeoffset = fdt_path_offset (fdt, "/bd_t");
  254. if (nodeoffset >= 0) {
  255. err = fdt_del_node(fdt, nodeoffset);
  256. if (err < 0) {
  257. printf("libfdt: %s\n", fdt_strerror(err));
  258. return err;
  259. }
  260. }
  261. /*
  262. * Create a new node "/bd_t" (offset 0 is root level)
  263. */
  264. nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
  265. if (nodeoffset < 0) {
  266. printf("libfdt: %s\n", fdt_strerror(nodeoffset));
  267. return nodeoffset;
  268. }
  269. /*
  270. * Use the string/pointer structure to create the entries...
  271. */
  272. for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
  273. tmp = cpu_to_be32(getenv("bootargs"));
  274. err = fdt_setprop(fdt, nodeoffset, bd_map[i].name, &tmp, sizeof(tmp));
  275. if (err < 0)
  276. printf("libfdt: %s\n", fdt_strerror(err));
  277. }
  278. /*
  279. * Add a couple of oddball entries...
  280. */
  281. err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
  282. if (err < 0)
  283. printf("libfdt: %s\n", fdt_strerror(err));
  284. err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
  285. if (err < 0)
  286. printf("libfdt: %s\n", fdt_strerror(err));
  287. return 0;
  288. }
  289. #endif /* CONFIG_OF_HAS_BD_T */
  290. #endif /* CONFIG_OF_LIBFDT */