fdt_support.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. err = fdt_check_header(fdt);
  48. if (err < 0) {
  49. printf("fdt_chosen: %s\n", fdt_strerror(err));
  50. return err;
  51. }
  52. if (initrd_start && initrd_end) {
  53. struct fdt_reserve_entry re;
  54. int used;
  55. int total;
  56. int j;
  57. err = fdt_num_reservemap(fdt, &used, &total);
  58. if (err < 0) {
  59. printf("fdt_chosen: %s\n", fdt_strerror(err));
  60. return err;
  61. }
  62. if (used >= total) {
  63. printf("WARNING: "
  64. "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("fdt_chosen: %s\n", fdt_strerror(err));
  82. return err;
  83. }
  84. }
  85. /*
  86. * Find the "chosen" node.
  87. */
  88. nodeoffset = fdt_find_node_by_path (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: could not create /chosen %s.\n",
  105. fdt_strerror(nodeoffset));
  106. return nodeoffset;
  107. }
  108. }
  109. /*
  110. * Update pre-existing properties, create them if non-existant.
  111. */
  112. str = getenv("bootargs");
  113. if (str != NULL) {
  114. err = fdt_setprop(fdt, nodeoffset,
  115. "bootargs", str, strlen(str)+1);
  116. if (err < 0)
  117. printf("WARNING: could not set bootargs %s.\n",
  118. fdt_strerror(err));
  119. }
  120. if (initrd_start && initrd_end) {
  121. tmp = __cpu_to_be32(initrd_start);
  122. err = fdt_setprop(fdt, nodeoffset,
  123. "linux,initrd-start", &tmp, sizeof(tmp));
  124. if (err < 0)
  125. printf("WARNING: "
  126. "could not set linux,initrd-start %s.\n",
  127. fdt_strerror(err));
  128. tmp = __cpu_to_be32(initrd_end);
  129. err = fdt_setprop(fdt, nodeoffset,
  130. "linux,initrd-end", &tmp, sizeof(tmp));
  131. if (err < 0)
  132. printf("WARNING: could not set linux,initrd-end %s.\n",
  133. fdt_strerror(err));
  134. }
  135. #ifdef OF_STDOUT_PATH
  136. err = fdt_setprop(fdt, nodeoffset,
  137. "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
  138. if (err < 0)
  139. printf("WARNING: could not set linux,stdout-path %s.\n",
  140. fdt_strerror(err));
  141. #endif
  142. return err;
  143. }
  144. /********************************************************************/
  145. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  146. /* Function that returns a character from the environment */
  147. extern uchar(*env_get_char) (int);
  148. int fdt_env(void *fdt)
  149. {
  150. int nodeoffset;
  151. int err;
  152. int k, nxt;
  153. int i;
  154. static char tmpenv[256];
  155. err = fdt_check_header(fdt);
  156. if (err < 0) {
  157. printf("fdt_env: %s\n", fdt_strerror(err));
  158. return err;
  159. }
  160. /*
  161. * See if we already have a "u-boot-env" node, delete it if so.
  162. * Then create a new empty node.
  163. */
  164. nodeoffset = fdt_find_node_by_path (fdt, "/u-boot-env");
  165. if (nodeoffset >= 0) {
  166. err = fdt_del_node(fdt, nodeoffset);
  167. if (err < 0) {
  168. printf("fdt_env: %s\n", fdt_strerror(err));
  169. return err;
  170. }
  171. }
  172. /*
  173. * Create a new node "/u-boot-env" (offset 0 is root level)
  174. */
  175. nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
  176. if (nodeoffset < 0) {
  177. printf("WARNING: could not create /u-boot-env %s.\n",
  178. fdt_strerror(nodeoffset));
  179. return nodeoffset;
  180. }
  181. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  182. char *s, *lval, *rval;
  183. /*
  184. * Find the end of the name=definition
  185. */
  186. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  187. ;
  188. s = tmpenv;
  189. for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
  190. *s++ = env_get_char(k);
  191. *s++ = '\0';
  192. lval = tmpenv;
  193. /*
  194. * Find the first '=': it separates the name from the value
  195. */
  196. s = strchr(tmpenv, '=');
  197. if (s != NULL) {
  198. *s++ = '\0';
  199. rval = s;
  200. } else
  201. continue;
  202. err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
  203. if (err < 0) {
  204. printf("WARNING: could not set %s %s.\n",
  205. lval, 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("fdt_bd_t: %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_find_node_by_path (fdt, "/bd_t");
  277. if (nodeoffset >= 0) {
  278. err = fdt_del_node(fdt, nodeoffset);
  279. if (err < 0) {
  280. printf("fdt_bd_t: %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("WARNING: could not create /bd_t %s.\n",
  290. fdt_strerror(nodeoffset));
  291. printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
  292. return nodeoffset;
  293. }
  294. /*
  295. * Use the string/pointer structure to create the entries...
  296. */
  297. for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
  298. tmp = cpu_to_be32(getenv("bootargs"));
  299. err = fdt_setprop(fdt, nodeoffset,
  300. bd_map[i].name, &tmp, sizeof(tmp));
  301. if (err < 0)
  302. printf("WARNING: could not set %s %s.\n",
  303. bd_map[i].name, fdt_strerror(err));
  304. }
  305. /*
  306. * Add a couple of oddball entries...
  307. */
  308. err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
  309. if (err < 0)
  310. printf("WARNING: could not set enetaddr %s.\n",
  311. fdt_strerror(err));
  312. err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
  313. if (err < 0)
  314. printf("WARNING: could not set ethspeed %s.\n",
  315. fdt_strerror(err));
  316. return 0;
  317. }
  318. #endif /* ifdef CONFIG_OF_HAS_BD_T */
  319. #endif /* CONFIG_OF_LIBFDT */