fdt_support.c 9.0 KB

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