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 24 #include <common.h> 25 #include <linux/ctype.h> 26 #include <linux/types.h> 27 28 #ifdef CONFIG_OF_LIBFDT 29 30 #include <asm/global_data.h> 31 #include <fdt.h> 32 #include <libfdt.h> 33 #include <fdt_support.h> 34 35 /* 36 * Global data (for the gd->bd) 37 */ 38 DECLARE_GLOBAL_DATA_PTR; 39 40 /* 41 * fdt points to our working device tree. 42 */ 43 struct fdt_header *fdt; 44 45 /********************************************************************/ 46 47 int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force) 48 { 49 int nodeoffset; 50 int err; 51 u32 tmp; /* used to set 32 bit integer properties */ 52 char *str; /* used to set string properties */ 53 54 err = fdt_check_header(fdt); 55 if (err < 0) { 56 printf("fdt_chosen: %s\n", fdt_strerror(err)); 57 return err; 58 } 59 60 if (initrd_start && initrd_end) { 61 struct fdt_reserve_entry re; 62 int used; 63 int total; 64 int j; 65 66 err = fdt_num_reservemap(fdt, &used, &total); 67 if (err < 0) { 68 printf("fdt_chosen: %s\n", fdt_strerror(err)); 69 return err; 70 } 71 if (used >= total) { 72 printf("WARNING: " 73 "no room in the reserved map (%d of %d)\n", 74 used, total); 75 return -1; 76 } 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 < used; j++) { 82 err = fdt_get_reservemap(fdt, j, &re); 83 if (re.address == initrd_start) { 84 break; 85 } 86 } 87 err = fdt_replace_reservemap_entry(fdt, j, 88 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 /* 96 * Find the "chosen" node. 97 */ 98 nodeoffset = fdt_find_node_by_path (fdt, "/chosen"); 99 100 /* 101 * If we have a "chosen" node already the "force the writing" 102 * is not set, our job is done. 103 */ 104 if ((nodeoffset >= 0) && !force) 105 return 0; 106 107 /* 108 * No "chosen" node in the blob: create it. 109 */ 110 if (nodeoffset < 0) { 111 /* 112 * Create a new node "/chosen" (offset 0 is root level) 113 */ 114 nodeoffset = fdt_add_subnode(fdt, 0, "chosen"); 115 if (nodeoffset < 0) { 116 printf("WARNING: could not create /chosen %s.\n", 117 fdt_strerror(nodeoffset)); 118 return nodeoffset; 119 } 120 } 121 122 /* 123 * Update pre-existing properties, create them if non-existant. 124 */ 125 str = getenv("bootargs"); 126 if (str != NULL) { 127 err = fdt_setprop(fdt, nodeoffset, 128 "bootargs", str, strlen(str)+1); 129 if (err < 0) 130 printf("WARNING: could not set bootargs %s.\n", 131 fdt_strerror(err)); 132 } 133 if (initrd_start && initrd_end) { 134 tmp = __cpu_to_be32(initrd_start); 135 err = fdt_setprop(fdt, nodeoffset, 136 "linux,initrd-start", &tmp, sizeof(tmp)); 137 if (err < 0) 138 printf("WARNING: " 139 "could not set linux,initrd-start %s.\n", 140 fdt_strerror(err)); 141 tmp = __cpu_to_be32(initrd_end); 142 err = fdt_setprop(fdt, nodeoffset, 143 "linux,initrd-end", &tmp, sizeof(tmp)); 144 if (err < 0) 145 printf("WARNING: could not set linux,initrd-end %s.\n", 146 fdt_strerror(err)); 147 } 148 #ifdef OF_STDOUT_PATH 149 err = fdt_setprop(fdt, nodeoffset, 150 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1); 151 if (err < 0) 152 printf("WARNING: could not set linux,stdout-path %s.\n", 153 fdt_strerror(err)); 154 #endif 155 156 return err; 157 } 158 159 /********************************************************************/ 160 161 #ifdef CONFIG_OF_HAS_UBOOT_ENV 162 163 /* Function that returns a character from the environment */ 164 extern uchar(*env_get_char) (int); 165 166 167 int fdt_env(void *fdt) 168 { 169 int nodeoffset; 170 int err; 171 int k, nxt; 172 int i; 173 static char tmpenv[256]; 174 175 err = fdt_check_header(fdt); 176 if (err < 0) { 177 printf("fdt_env: %s\n", fdt_strerror(err)); 178 return err; 179 } 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 203 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { 204 char *s, *lval, *rval; 205 206 /* 207 * Find the end of the name=definition 208 */ 209 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) 210 ; 211 s = tmpenv; 212 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k) 213 *s++ = env_get_char(k); 214 *s++ = '\0'; 215 lval = tmpenv; 216 /* 217 * Find the first '=': it separates the name from the value 218 */ 219 s = strchr(tmpenv, '='); 220 if (s != NULL) { 221 *s++ = '\0'; 222 rval = s; 223 } else 224 continue; 225 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1); 226 if (err < 0) { 227 printf("WARNING: could not set %s %s.\n", 228 lval, fdt_strerror(err)); 229 return err; 230 } 231 } 232 return 0; 233 } 234 #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */ 235 236 /********************************************************************/ 237 238 #ifdef CONFIG_OF_HAS_BD_T 239 240 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) } 241 242 static const struct { 243 const char *name; 244 int offset; 245 } bd_map[] = { 246 BDM(memstart), 247 BDM(memsize), 248 BDM(flashstart), 249 BDM(flashsize), 250 BDM(flashoffset), 251 BDM(sramstart), 252 BDM(sramsize), 253 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \ 254 || defined(CONFIG_E500) 255 BDM(immr_base), 256 #endif 257 #if defined(CONFIG_MPC5xxx) 258 BDM(mbar_base), 259 #endif 260 #if defined(CONFIG_MPC83XX) 261 BDM(immrbar), 262 #endif 263 #if defined(CONFIG_MPC8220) 264 BDM(mbar_base), 265 BDM(inpfreq), 266 BDM(pcifreq), 267 BDM(pevfreq), 268 BDM(flbfreq), 269 BDM(vcofreq), 270 #endif 271 BDM(bootflags), 272 BDM(ip_addr), 273 BDM(intfreq), 274 BDM(busfreq), 275 #ifdef CONFIG_CPM2 276 BDM(cpmfreq), 277 BDM(brgfreq), 278 BDM(sccfreq), 279 BDM(vco), 280 #endif 281 #if defined(CONFIG_MPC5xxx) 282 BDM(ipbfreq), 283 BDM(pcifreq), 284 #endif 285 BDM(baudrate), 286 }; 287 288 289 int fdt_bd_t(void *fdt) 290 { 291 bd_t *bd = gd->bd; 292 int nodeoffset; 293 int err; 294 u32 tmp; /* used to set 32 bit integer properties */ 295 int i; 296 297 err = fdt_check_header(fdt); 298 if (err < 0) { 299 printf("fdt_bd_t: %s\n", fdt_strerror(err)); 300 return err; 301 } 302 303 /* 304 * See if we already have a "bd_t" node, delete it if so. 305 * Then create a new empty node. 306 */ 307 nodeoffset = fdt_find_node_by_path (fdt, "/bd_t"); 308 if (nodeoffset >= 0) { 309 err = fdt_del_node(fdt, nodeoffset); 310 if (err < 0) { 311 printf("fdt_bd_t: %s\n", fdt_strerror(err)); 312 return err; 313 } 314 } 315 /* 316 * Create a new node "/bd_t" (offset 0 is root level) 317 */ 318 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t"); 319 if (nodeoffset < 0) { 320 printf("WARNING: could not create /bd_t %s.\n", 321 fdt_strerror(nodeoffset)); 322 printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset)); 323 return nodeoffset; 324 } 325 /* 326 * Use the string/pointer structure to create the entries... 327 */ 328 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) { 329 tmp = cpu_to_be32(getenv("bootargs")); 330 err = fdt_setprop(fdt, nodeoffset, 331 bd_map[i].name, &tmp, sizeof(tmp)); 332 if (err < 0) 333 printf("WARNING: could not set %s %s.\n", 334 bd_map[i].name, fdt_strerror(err)); 335 } 336 /* 337 * Add a couple of oddball entries... 338 */ 339 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6); 340 if (err < 0) 341 printf("WARNING: could not set enetaddr %s.\n", 342 fdt_strerror(err)); 343 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4); 344 if (err < 0) 345 printf("WARNING: could not set ethspeed %s.\n", 346 fdt_strerror(err)); 347 return 0; 348 } 349 #endif /* ifdef CONFIG_OF_HAS_BD_T */ 350 351 #endif /* CONFIG_OF_LIBFDT */ 352