fdt_support.c 9.4 KB

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