fdt_support.c 9.2 KB

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