fdt_support.c 8.1 KB

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