| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2000, 2001, 2002, 2003 Broadcom Corporation | 
|  | 3 | * | 
|  | 4 | * This program is free software; you can redistribute it and/or | 
|  | 5 | * modify it under the terms of the GNU General Public License | 
|  | 6 | * as published by the Free Software Foundation; either version 2 | 
|  | 7 | * of the License, or (at your option) any later version. | 
|  | 8 | * | 
|  | 9 | * This program is distributed in the hope that it will be useful, | 
|  | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 12 | * GNU General Public License for more details. | 
|  | 13 | * | 
|  | 14 | * You should have received a copy of the GNU General Public License | 
|  | 15 | * along with this program; if not, write to the Free Software | 
|  | 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
|  | 17 | */ | 
|  | 18 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/init.h> | 
|  | 20 | #include <linux/kernel.h> | 
|  | 21 | #include <linux/linkage.h> | 
|  | 22 | #include <linux/mm.h> | 
|  | 23 | #include <linux/blkdev.h> | 
|  | 24 | #include <linux/bootmem.h> | 
| Ralf Baechle | fcdb27a | 2006-01-18 17:37:07 +0000 | [diff] [blame] | 25 | #include <linux/pm.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/smp.h> | 
|  | 27 |  | 
|  | 28 | #include <asm/bootinfo.h> | 
|  | 29 | #include <asm/reboot.h> | 
|  | 30 | #include <asm/sibyte/board.h> | 
| Ralf Baechle | 87353d8 | 2007-11-19 12:23:51 +0000 | [diff] [blame] | 31 | #include <asm/smp-ops.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 |  | 
| Aurelien Jarno | df78b5c | 2007-09-05 08:58:26 +0200 | [diff] [blame] | 33 | #include <asm/fw/cfe/cfe_api.h> | 
|  | 34 | #include <asm/fw/cfe/cfe_error.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 |  | 
|  | 36 | /* Max ram addressable in 32-bit segments */ | 
| Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 37 | #ifdef CONFIG_64BIT | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #define MAX_RAM_SIZE (~0ULL) | 
|  | 39 | #else | 
|  | 40 | #ifdef CONFIG_HIGHMEM | 
|  | 41 | #ifdef CONFIG_64BIT_PHYS_ADDR | 
|  | 42 | #define MAX_RAM_SIZE (~0ULL) | 
|  | 43 | #else | 
|  | 44 | #define MAX_RAM_SIZE (0xffffffffULL) | 
|  | 45 | #endif | 
|  | 46 | #else | 
|  | 47 | #define MAX_RAM_SIZE (0x1fffffffULL) | 
|  | 48 | #endif | 
|  | 49 | #endif | 
|  | 50 |  | 
|  | 51 | #define SIBYTE_MAX_MEM_REGIONS 8 | 
|  | 52 | phys_t board_mem_region_addrs[SIBYTE_MAX_MEM_REGIONS]; | 
|  | 53 | phys_t board_mem_region_sizes[SIBYTE_MAX_MEM_REGIONS]; | 
|  | 54 | unsigned int board_mem_region_count; | 
|  | 55 |  | 
|  | 56 | int cfe_cons_handle; | 
|  | 57 |  | 
|  | 58 | #ifdef CONFIG_BLK_DEV_INITRD | 
|  | 59 | extern unsigned long initrd_start, initrd_end; | 
|  | 60 | #endif | 
|  | 61 |  | 
|  | 62 | #ifdef CONFIG_KGDB | 
|  | 63 | extern int kgdb_port; | 
|  | 64 | #endif | 
|  | 65 |  | 
| Robert P. J. Day | b3f6df9 | 2007-05-25 14:32:28 -0400 | [diff] [blame] | 66 | static void __noreturn cfe_linux_exit(void *arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { | 
|  | 68 | int warm = *(int *)arg; | 
|  | 69 |  | 
|  | 70 | if (smp_processor_id()) { | 
|  | 71 | static int reboot_smp; | 
|  | 72 |  | 
|  | 73 | /* Don't repeat the process from another CPU */ | 
|  | 74 | if (!reboot_smp) { | 
|  | 75 | /* Get CPU 0 to do the cfe_exit */ | 
|  | 76 | reboot_smp = 1; | 
|  | 77 | smp_call_function(cfe_linux_exit, arg, 1, 0); | 
|  | 78 | } | 
|  | 79 | } else { | 
|  | 80 | printk("Passing control back to CFE...\n"); | 
|  | 81 | cfe_exit(warm, 0); | 
|  | 82 | printk("cfe_exit returned??\n"); | 
|  | 83 | } | 
|  | 84 | while (1); | 
|  | 85 | } | 
|  | 86 |  | 
| Robert P. J. Day | b3f6df9 | 2007-05-25 14:32:28 -0400 | [diff] [blame] | 87 | static void __noreturn cfe_linux_restart(char *command) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { | 
|  | 89 | static const int zero; | 
|  | 90 |  | 
|  | 91 | cfe_linux_exit((void *)&zero); | 
|  | 92 | } | 
|  | 93 |  | 
| Robert P. J. Day | b3f6df9 | 2007-05-25 14:32:28 -0400 | [diff] [blame] | 94 | static void __noreturn cfe_linux_halt(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { | 
|  | 96 | static const int one = 1; | 
|  | 97 |  | 
|  | 98 | cfe_linux_exit((void *)&one); | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | static __init void prom_meminit(void) | 
|  | 102 | { | 
|  | 103 | u64 addr, size, type; /* regardless of 64BIT_PHYS_ADDR */ | 
|  | 104 | int mem_flags = 0; | 
|  | 105 | unsigned int idx; | 
|  | 106 | int rd_flag; | 
|  | 107 | #ifdef CONFIG_BLK_DEV_INITRD | 
|  | 108 | unsigned long initrd_pstart; | 
|  | 109 | unsigned long initrd_pend; | 
|  | 110 |  | 
|  | 111 | initrd_pstart = CPHYSADDR(initrd_start); | 
|  | 112 | initrd_pend = CPHYSADDR(initrd_end); | 
|  | 113 | if (initrd_start && | 
|  | 114 | ((initrd_pstart > MAX_RAM_SIZE) | 
|  | 115 | || (initrd_pend > MAX_RAM_SIZE))) { | 
|  | 116 | panic("initrd out of addressable memory"); | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | #endif /* INITRD */ | 
|  | 120 |  | 
|  | 121 | for (idx = 0; cfe_enummem(idx, mem_flags, &addr, &size, &type) != CFE_ERR_NOMORE; | 
|  | 122 | idx++) { | 
|  | 123 | rd_flag = 0; | 
|  | 124 | if (type == CFE_MI_AVAILABLE) { | 
|  | 125 | /* | 
|  | 126 | * See if this block contains (any portion of) the | 
|  | 127 | * ramdisk | 
|  | 128 | */ | 
|  | 129 | #ifdef CONFIG_BLK_DEV_INITRD | 
|  | 130 | if (initrd_start) { | 
|  | 131 | if ((initrd_pstart > addr) && | 
|  | 132 | (initrd_pstart < (addr + size))) { | 
|  | 133 | add_memory_region(addr, | 
|  | 134 | initrd_pstart - addr, | 
|  | 135 | BOOT_MEM_RAM); | 
|  | 136 | rd_flag = 1; | 
|  | 137 | } | 
|  | 138 | if ((initrd_pend > addr) && | 
|  | 139 | (initrd_pend < (addr + size))) { | 
|  | 140 | add_memory_region(initrd_pend, | 
|  | 141 | (addr + size) - initrd_pend, | 
|  | 142 | BOOT_MEM_RAM); | 
|  | 143 | rd_flag = 1; | 
|  | 144 | } | 
|  | 145 | } | 
|  | 146 | #endif | 
|  | 147 | if (!rd_flag) { | 
|  | 148 | if (addr > MAX_RAM_SIZE) | 
|  | 149 | continue; | 
|  | 150 | if (addr+size > MAX_RAM_SIZE) | 
|  | 151 | size = MAX_RAM_SIZE - (addr+size) + 1; | 
|  | 152 | /* | 
|  | 153 | * memcpy/__copy_user prefetch, which | 
|  | 154 | * will cause a bus error for | 
|  | 155 | * KSEG/KUSEG addrs not backed by RAM. | 
|  | 156 | * Hence, reserve some padding for the | 
|  | 157 | * prefetch distance. | 
|  | 158 | */ | 
|  | 159 | if (size > 512) | 
|  | 160 | size -= 512; | 
|  | 161 | add_memory_region(addr, size, BOOT_MEM_RAM); | 
|  | 162 | } | 
|  | 163 | board_mem_region_addrs[board_mem_region_count] = addr; | 
|  | 164 | board_mem_region_sizes[board_mem_region_count] = size; | 
|  | 165 | board_mem_region_count++; | 
|  | 166 | if (board_mem_region_count == | 
|  | 167 | SIBYTE_MAX_MEM_REGIONS) { | 
|  | 168 | /* | 
|  | 169 | * Too many regions.  Need to configure more | 
|  | 170 | */ | 
|  | 171 | while(1); | 
|  | 172 | } | 
|  | 173 | } | 
|  | 174 | } | 
|  | 175 | #ifdef CONFIG_BLK_DEV_INITRD | 
|  | 176 | if (initrd_start) { | 
|  | 177 | add_memory_region(initrd_pstart, initrd_pend - initrd_pstart, | 
|  | 178 | BOOT_MEM_RESERVED); | 
|  | 179 | } | 
|  | 180 | #endif | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | #ifdef CONFIG_BLK_DEV_INITRD | 
|  | 184 | static int __init initrd_setup(char *str) | 
|  | 185 | { | 
|  | 186 | char rdarg[64]; | 
|  | 187 | int idx; | 
|  | 188 | char *tmp, *endptr; | 
|  | 189 | unsigned long initrd_size; | 
|  | 190 |  | 
|  | 191 | /* Make a copy of the initrd argument so we can smash it up here */ | 
|  | 192 | for (idx = 0; idx < sizeof(rdarg)-1; idx++) { | 
|  | 193 | if (!str[idx] || (str[idx] == ' ')) break; | 
|  | 194 | rdarg[idx] = str[idx]; | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | rdarg[idx] = 0; | 
|  | 198 | str = rdarg; | 
|  | 199 |  | 
|  | 200 | /* | 
|  | 201 | *Initrd location comes in the form "<hex size of ramdisk in bytes>@<location in memory>" | 
|  | 202 | *  e.g. initrd=3abfd@80010000.  This is set up by the loader. | 
|  | 203 | */ | 
|  | 204 | for (tmp = str; *tmp != '@'; tmp++) { | 
|  | 205 | if (!*tmp) { | 
|  | 206 | goto fail; | 
|  | 207 | } | 
|  | 208 | } | 
|  | 209 | *tmp = 0; | 
|  | 210 | tmp++; | 
|  | 211 | if (!*tmp) { | 
|  | 212 | goto fail; | 
|  | 213 | } | 
|  | 214 | initrd_size = simple_strtoul(str, &endptr, 16); | 
|  | 215 | if (*endptr) { | 
|  | 216 | *(tmp-1) = '@'; | 
|  | 217 | goto fail; | 
|  | 218 | } | 
|  | 219 | *(tmp-1) = '@'; | 
|  | 220 | initrd_start = simple_strtoul(tmp, &endptr, 16); | 
|  | 221 | if (*endptr) { | 
|  | 222 | goto fail; | 
|  | 223 | } | 
|  | 224 | initrd_end = initrd_start + initrd_size; | 
| Ralf Baechle | 36a8853 | 2007-03-01 11:56:43 +0000 | [diff] [blame] | 225 | printk("Found initrd of %lx@%lx\n", initrd_size, initrd_start); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | return 1; | 
|  | 227 | fail: | 
| Ralf Baechle | 36a8853 | 2007-03-01 11:56:43 +0000 | [diff] [blame] | 228 | printk("Bad initrd argument.  Disabling initrd\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | initrd_start = 0; | 
|  | 230 | initrd_end = 0; | 
|  | 231 | return 1; | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | #endif | 
|  | 235 |  | 
| Ralf Baechle | 87353d8 | 2007-11-19 12:23:51 +0000 | [diff] [blame] | 236 | extern struct plat_smp_ops sb_smp_ops; | 
|  | 237 | extern struct plat_smp_ops bcm1480_smp_ops; | 
|  | 238 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | /* | 
|  | 240 | * prom_init is called just after the cpu type is determined, from setup_arch() | 
|  | 241 | */ | 
|  | 242 | void __init prom_init(void) | 
|  | 243 | { | 
|  | 244 | uint64_t cfe_ept, cfe_handle; | 
|  | 245 | unsigned int cfe_eptseal; | 
|  | 246 | int argc = fw_arg0; | 
|  | 247 | char **envp = (char **) fw_arg2; | 
|  | 248 | int *prom_vec = (int *) fw_arg3; | 
|  | 249 | #ifdef CONFIG_KGDB | 
|  | 250 | char *arg; | 
|  | 251 | #endif | 
|  | 252 |  | 
|  | 253 | _machine_restart   = cfe_linux_restart; | 
|  | 254 | _machine_halt      = cfe_linux_halt; | 
| Ralf Baechle | fcdb27a | 2006-01-18 17:37:07 +0000 | [diff] [blame] | 255 | pm_power_off = cfe_linux_halt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 |  | 
|  | 257 | /* | 
|  | 258 | * Check if a loader was used; if NOT, the 4 arguments are | 
|  | 259 | * what CFE gives us (handle, 0, EPT and EPTSEAL) | 
|  | 260 | */ | 
|  | 261 | if (argc < 0) { | 
|  | 262 | cfe_handle = (uint64_t)(long)argc; | 
|  | 263 | cfe_ept = (long)envp; | 
|  | 264 | cfe_eptseal = (uint32_t)(unsigned long)prom_vec; | 
|  | 265 | } else { | 
|  | 266 | if ((int32_t)(long)prom_vec < 0) { | 
|  | 267 | /* | 
|  | 268 | * Old loader; all it gives us is the handle, | 
|  | 269 | * so use the "known" entrypoint and assume | 
|  | 270 | * the seal. | 
|  | 271 | */ | 
|  | 272 | cfe_handle = (uint64_t)(long)prom_vec; | 
|  | 273 | cfe_ept = (uint64_t)((int32_t)0x9fc00500); | 
|  | 274 | cfe_eptseal = CFE_EPTSEAL; | 
|  | 275 | } else { | 
|  | 276 | /* | 
|  | 277 | * Newer loaders bundle the handle/ept/eptseal | 
|  | 278 | * Note: prom_vec is in the loader's useg | 
|  | 279 | * which is still alive in the TLB. | 
|  | 280 | */ | 
|  | 281 | cfe_handle = (uint64_t)((int32_t *)prom_vec)[0]; | 
|  | 282 | cfe_ept = (uint64_t)((int32_t *)prom_vec)[2]; | 
|  | 283 | cfe_eptseal = (unsigned int)((uint32_t *)prom_vec)[3]; | 
|  | 284 | } | 
|  | 285 | } | 
|  | 286 | if (cfe_eptseal != CFE_EPTSEAL) { | 
|  | 287 | /* too early for panic to do any good */ | 
| Ralf Baechle | 36a8853 | 2007-03-01 11:56:43 +0000 | [diff] [blame] | 288 | printk("CFE's entrypoint seal doesn't match. Spinning."); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | while (1) ; | 
|  | 290 | } | 
|  | 291 | cfe_init(cfe_handle, cfe_ept); | 
| Ralf Baechle | 42a3b4f | 2005-09-03 15:56:17 -0700 | [diff] [blame] | 292 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | * Get the handle for (at least) prom_putchar, possibly for | 
|  | 294 | * boot console | 
|  | 295 | */ | 
|  | 296 | cfe_cons_handle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE); | 
|  | 297 | if (cfe_getenv("LINUX_CMDLINE", arcs_cmdline, CL_SIZE) < 0) { | 
|  | 298 | if (argc < 0) { | 
|  | 299 | /* | 
|  | 300 | * It's OK for direct boot to not provide a | 
|  | 301 | *  command line | 
|  | 302 | */ | 
|  | 303 | strcpy(arcs_cmdline, "root=/dev/ram0 "); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } else { | 
|  | 305 | /* The loader should have set the command line */ | 
|  | 306 | /* too early for panic to do any good */ | 
| Ralf Baechle | 36a8853 | 2007-03-01 11:56:43 +0000 | [diff] [blame] | 307 | printk("LINUX_CMDLINE not defined in cfe."); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | while (1) ; | 
|  | 309 | } | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | #ifdef CONFIG_KGDB | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 313 | if ((arg = strstr(arcs_cmdline, "kgdb=duart")) != NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | kgdb_port = (arg[10] == '0') ? 0 : 1; | 
|  | 315 | else | 
|  | 316 | kgdb_port = 1; | 
|  | 317 | #endif | 
|  | 318 |  | 
|  | 319 | #ifdef CONFIG_BLK_DEV_INITRD | 
|  | 320 | { | 
|  | 321 | char *ptr; | 
|  | 322 | /* Need to find out early whether we've got an initrd.  So scan | 
|  | 323 | the list looking now */ | 
|  | 324 | for (ptr = arcs_cmdline; *ptr; ptr++) { | 
|  | 325 | while (*ptr == ' ') { | 
|  | 326 | ptr++; | 
|  | 327 | } | 
|  | 328 | if (!strncmp(ptr, "initrd=", 7)) { | 
|  | 329 | initrd_setup(ptr+7); | 
|  | 330 | break; | 
|  | 331 | } else { | 
|  | 332 | while (*ptr && (*ptr != ' ')) { | 
|  | 333 | ptr++; | 
|  | 334 | } | 
|  | 335 | } | 
|  | 336 | } | 
|  | 337 | } | 
|  | 338 | #endif /* CONFIG_BLK_DEV_INITRD */ | 
|  | 339 |  | 
|  | 340 | /* Not sure this is needed, but it's the safe way. */ | 
|  | 341 | arcs_cmdline[CL_SIZE-1] = 0; | 
|  | 342 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | prom_meminit(); | 
| Ralf Baechle | 87353d8 | 2007-11-19 12:23:51 +0000 | [diff] [blame] | 344 |  | 
|  | 345 | #if defined(CONFIG_SIBYTE_BCM112X) || defined(CONFIG_SIBYTE_SB1250) | 
|  | 346 | register_smp_ops(&sb_smp_ops); | 
|  | 347 | #endif | 
|  | 348 | #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80) | 
|  | 349 | register_smp_ops(&bcm1480_smp_ops); | 
|  | 350 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } | 
|  | 352 |  | 
| Atsushi Nemoto | c44e8d5 | 2006-12-30 00:43:59 +0900 | [diff] [blame] | 353 | void __init prom_free_prom_memory(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { | 
|  | 355 | /* Not sure what I'm supposed to do here.  Nothing, I think */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } | 
|  | 357 |  | 
|  | 358 | void prom_putchar(char c) | 
|  | 359 | { | 
|  | 360 | int ret; | 
|  | 361 |  | 
|  | 362 | while ((ret = cfe_write(cfe_cons_handle, &c, 1)) == 0) | 
|  | 363 | ; | 
|  | 364 | } |