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