fdt_support.c 8.1 KB

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