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 | { |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 49 | int nodeoffset; |
| 50 | int err; |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 51 | u32 tmp; /* used to set 32 bit integer properties */ |
| 52 | char *str; /* used to set string properties */ |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 53 | |
| 54 | err = fdt_check_header(fdt); |
| 55 | if (err < 0) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 56 | printf("fdt_chosen: %s\n", fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 57 | return err; |
| 58 | } |
| 59 | |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 60 | if (initrd_start && initrd_end) { |
Gerald Van Baren | 7651f8b | 2007-04-19 23:14:39 -0400 | [diff] [blame] | 61 | struct fdt_reserve_entry re; |
Gerald Van Baren | c28abb9 | 2007-04-14 22:51:24 -0400 | [diff] [blame] | 62 | int used; |
| 63 | int total; |
| 64 | int j; |
| 65 | |
| 66 | err = fdt_num_reservemap(fdt, &used, &total); |
| 67 | if (err < 0) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 68 | printf("fdt_chosen: %s\n", fdt_strerror(err)); |
Gerald Van Baren | c28abb9 | 2007-04-14 22:51:24 -0400 | [diff] [blame] | 69 | return err; |
| 70 | } |
| 71 | if (used >= total) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 72 | printf("WARNING: " |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 73 | "no room in the reserved map (%d of %d)\n", |
Gerald Van Baren | c28abb9 | 2007-04-14 22:51:24 -0400 | [diff] [blame] | 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); |
Gerald Van Baren | 7651f8b | 2007-04-19 23:14:39 -0400 | [diff] [blame] | 83 | if (re.address == initrd_start) { |
Gerald Van Baren | c28abb9 | 2007-04-14 22:51:24 -0400 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | } |
| 87 | err = fdt_replace_reservemap_entry(fdt, j, |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 88 | initrd_start, initrd_end - initrd_start + 1); |
| 89 | if (err < 0) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 90 | printf("fdt_chosen: %s\n", fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 91 | return err; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Find the "chosen" node. |
| 97 | */ |
Gerald Van Baren | 1a86116 | 2007-06-06 22:47:58 -0400 | [diff] [blame] | 98 | nodeoffset = fdt_find_node_by_path (fdt, "/chosen"); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 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) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 116 | printf("WARNING: could not create /chosen %s.\n", |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 117 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 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) { |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 127 | err = fdt_setprop(fdt, nodeoffset, |
| 128 | "bootargs", str, strlen(str)+1); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 129 | if (err < 0) |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 130 | printf("WARNING: could not set bootargs %s.\n", |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 131 | fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 132 | } |
| 133 | if (initrd_start && initrd_end) { |
| 134 | tmp = __cpu_to_be32(initrd_start); |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 135 | err = fdt_setprop(fdt, nodeoffset, |
| 136 | "linux,initrd-start", &tmp, sizeof(tmp)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 137 | if (err < 0) |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 138 | printf("WARNING: " |
| 139 | "could not set linux,initrd-start %s.\n", |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 140 | fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 141 | tmp = __cpu_to_be32(initrd_end); |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 142 | err = fdt_setprop(fdt, nodeoffset, |
| 143 | "linux,initrd-end", &tmp, sizeof(tmp)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 144 | if (err < 0) |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 145 | printf("WARNING: could not set linux,initrd-end %s.\n", |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 146 | fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 147 | } |
| 148 | #ifdef OF_STDOUT_PATH |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 149 | err = fdt_setprop(fdt, nodeoffset, |
| 150 | "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 151 | if (err < 0) |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 152 | printf("WARNING: could not set linux,stdout-path %s.\n", |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 153 | fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 154 | #endif |
| 155 | |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 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) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 177 | printf("fdt_env: %s\n", fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 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 | */ |
Gerald Van Baren | 1a86116 | 2007-06-06 22:47:58 -0400 | [diff] [blame] | 185 | nodeoffset = fdt_find_node_by_path (fdt, "/u-boot-env"); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 186 | if (nodeoffset >= 0) { |
| 187 | err = fdt_del_node(fdt, nodeoffset); |
| 188 | if (err < 0) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 189 | printf("fdt_env: %s\n", fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 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) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 198 | printf("WARNING: could not create /u-boot-env %s.\n", |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 199 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 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) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 227 | printf("WARNING: could not set %s %s.\n", |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 228 | lval, fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 229 | return err; |
| 230 | } |
| 231 | } |
| 232 | return 0; |
| 233 | } |
Gerald Van Baren | c28abb9 | 2007-04-14 22:51:24 -0400 | [diff] [blame] | 234 | #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */ |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 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; |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 294 | u32 tmp; /* used to set 32 bit integer properties */ |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 295 | int i; |
| 296 | |
| 297 | err = fdt_check_header(fdt); |
| 298 | if (err < 0) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 299 | printf("fdt_bd_t: %s\n", fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 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 | */ |
Gerald Van Baren | 1a86116 | 2007-06-06 22:47:58 -0400 | [diff] [blame] | 307 | nodeoffset = fdt_find_node_by_path (fdt, "/bd_t"); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 308 | if (nodeoffset >= 0) { |
| 309 | err = fdt_del_node(fdt, nodeoffset); |
| 310 | if (err < 0) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 311 | printf("fdt_bd_t: %s\n", fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 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) { |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 320 | printf("WARNING: could not create /bd_t %s.\n", |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 321 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 322 | printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 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")); |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 330 | err = fdt_setprop(fdt, nodeoffset, |
| 331 | bd_map[i].name, &tmp, sizeof(tmp)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 332 | if (err < 0) |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 333 | printf("WARNING: could not set %s %s.\n", |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 334 | bd_map[i].name, fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 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) |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 341 | printf("WARNING: could not set enetaddr %s.\n", |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 342 | fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 343 | err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4); |
| 344 | if (err < 0) |
Gerald Van Baren | 5fe6be6 | 2007-08-07 21:14:22 -0400 | [diff] [blame] | 345 | printf("WARNING: could not set ethspeed %s.\n", |
Gerald Van Baren | 35ec398 | 2007-05-25 22:08:57 -0400 | [diff] [blame] | 346 | fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 347 | return 0; |
| 348 | } |
Gerald Van Baren | c28abb9 | 2007-04-14 22:51:24 -0400 | [diff] [blame] | 349 | #endif /* ifdef CONFIG_OF_HAS_BD_T */ |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 350 | |
| 351 | #endif /* CONFIG_OF_LIBFDT */ |