Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/ppc/simple/misc.c |
| 3 | * |
| 4 | * Misc. bootloader code for many machines. This assumes you have are using |
| 5 | * a 6xx/7xx/74xx CPU in your machine. This assumes the chunk of memory |
| 6 | * below 8MB is free. Finally, it assumes you have a NS16550-style uart for |
| 7 | * your serial console. If a machine meets these requirements, it can quite |
| 8 | * likely use this code during boot. |
| 9 | * |
| 10 | * Author: Matt Porter <mporter@mvista.com> |
| 11 | * Derived from arch/ppc/boot/prep/misc.c |
| 12 | * |
| 13 | * 2001 (c) MontaVista, Software, Inc. This file is licensed under |
| 14 | * the terms of the GNU General Public License version 2. This program |
| 15 | * is licensed "as is" without any warranty of any kind, whether express |
| 16 | * or implied. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/config.h> |
| 21 | #include <linux/string.h> |
| 22 | |
| 23 | #include <asm/page.h> |
| 24 | #include <asm/mmu.h> |
| 25 | #include <asm/bootinfo.h> |
| 26 | #ifdef CONFIG_44x |
| 27 | #include <asm/ibm4xx.h> |
| 28 | #endif |
| 29 | #include <asm/reg.h> |
| 30 | |
| 31 | #include "nonstdio.h" |
| 32 | |
| 33 | /* Default cmdline */ |
| 34 | #ifdef CONFIG_CMDLINE |
| 35 | #define CMDLINE CONFIG_CMDLINE |
| 36 | #else |
| 37 | #define CMDLINE "" |
| 38 | #endif |
| 39 | |
| 40 | /* Keyboard (and VGA console)? */ |
| 41 | #ifdef CONFIG_VGA_CONSOLE |
| 42 | #define HAS_KEYB 1 |
| 43 | #else |
| 44 | #define HAS_KEYB 0 |
| 45 | #endif |
| 46 | |
| 47 | /* Will / Can the user give input? |
| 48 | * Val Henson has requested that Gemini doesn't wait for the |
| 49 | * user to edit the cmdline or not. |
| 50 | */ |
| 51 | #if (defined(CONFIG_SERIAL_8250_CONSOLE) \ |
| 52 | || defined(CONFIG_VGA_CONSOLE) \ |
| 53 | || defined(CONFIG_SERIAL_MPC52xx_CONSOLE) \ |
| 54 | || defined(CONFIG_SERIAL_MPSC_CONSOLE)) \ |
| 55 | && !defined(CONFIG_GEMINI) |
| 56 | #define INTERACTIVE_CONSOLE 1 |
| 57 | #endif |
| 58 | |
| 59 | char *avail_ram; |
| 60 | char *end_avail; |
| 61 | char *zimage_start; |
| 62 | char cmd_preset[] = CMDLINE; |
| 63 | char cmd_buf[256]; |
| 64 | char *cmd_line = cmd_buf; |
| 65 | int keyb_present = HAS_KEYB; |
| 66 | int zimage_size; |
| 67 | |
| 68 | unsigned long com_port; |
| 69 | unsigned long initrd_size = 0; |
| 70 | |
| 71 | /* The linker tells us various locations in the image */ |
| 72 | extern char __image_begin, __image_end; |
| 73 | extern char __ramdisk_begin, __ramdisk_end; |
| 74 | extern char _end[]; |
| 75 | /* Original location */ |
| 76 | extern unsigned long start; |
| 77 | |
| 78 | extern int CRT_tstc(void); |
| 79 | extern unsigned long serial_init(int chan, void *ignored); |
| 80 | extern void serial_close(unsigned long com_port); |
| 81 | extern void gunzip(void *, int, unsigned char *, int *); |
| 82 | extern void serial_fixups(void); |
| 83 | |
| 84 | /* Allow get_mem_size to be hooked into. This is the default. */ |
| 85 | unsigned long __attribute__ ((weak)) |
| 86 | get_mem_size(void) |
| 87 | { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | struct bi_record * |
| 92 | decompress_kernel(unsigned long load_addr, int num_words, unsigned long cksum) |
| 93 | { |
| 94 | #ifdef INTERACTIVE_CONSOLE |
| 95 | int timer = 0; |
| 96 | char ch; |
| 97 | #endif |
| 98 | char *cp; |
| 99 | struct bi_record *rec; |
| 100 | unsigned long initrd_loc = 0, TotalMemory = 0; |
| 101 | |
| 102 | #if defined(CONFIG_SERIAL_8250_CONSOLE) || defined(CONFIG_SERIAL_MPSC_CONSOLE) |
| 103 | com_port = serial_init(0, NULL); |
| 104 | #endif |
| 105 | |
| 106 | #if defined(CONFIG_44x) && defined(PPC44x_EMAC0_MR0) |
| 107 | /* Reset MAL */ |
| 108 | mtdcr(DCRN_MALCR(DCRN_MAL_BASE), MALCR_MMSR); |
| 109 | /* Wait for reset */ |
| 110 | while (mfdcr(DCRN_MALCR(DCRN_MAL_BASE)) & MALCR_MMSR) {}; |
| 111 | /* Reset EMAC */ |
| 112 | *(volatile unsigned long *)PPC44x_EMAC0_MR0 = 0x20000000; |
| 113 | __asm__ __volatile__("eieio"); |
| 114 | #endif |
| 115 | |
| 116 | /* |
| 117 | * Call get_mem_size(), which is memory controller dependent, |
| 118 | * and we must have the correct file linked in here. |
| 119 | */ |
| 120 | TotalMemory = get_mem_size(); |
| 121 | |
| 122 | /* assume the chunk below 8M is free */ |
| 123 | end_avail = (char *)0x00800000; |
| 124 | |
| 125 | /* |
| 126 | * Reveal where we were loaded at and where we |
| 127 | * were relocated to. |
| 128 | */ |
| 129 | puts("loaded at: "); puthex(load_addr); |
| 130 | puts(" "); puthex((unsigned long)(load_addr + (4*num_words))); |
| 131 | puts("\n"); |
| 132 | if ( (unsigned long)load_addr != (unsigned long)&start ) |
| 133 | { |
| 134 | puts("relocated to: "); puthex((unsigned long)&start); |
| 135 | puts(" "); |
| 136 | puthex((unsigned long)((unsigned long)&start + (4*num_words))); |
| 137 | puts("\n"); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * We link ourself to 0x00800000. When we run, we relocate |
| 142 | * ourselves there. So we just need __image_begin for the |
| 143 | * start. -- Tom |
| 144 | */ |
| 145 | zimage_start = (char *)(unsigned long)(&__image_begin); |
| 146 | zimage_size = (unsigned long)(&__image_end) - |
| 147 | (unsigned long)(&__image_begin); |
| 148 | |
| 149 | initrd_size = (unsigned long)(&__ramdisk_end) - |
| 150 | (unsigned long)(&__ramdisk_begin); |
| 151 | |
| 152 | /* |
| 153 | * The zImage and initrd will be between start and _end, so they've |
| 154 | * already been moved once. We're good to go now. -- Tom |
| 155 | */ |
| 156 | avail_ram = (char *)PAGE_ALIGN((unsigned long)_end); |
| 157 | puts("zimage at: "); puthex((unsigned long)zimage_start); |
| 158 | puts(" "); puthex((unsigned long)(zimage_size+zimage_start)); |
| 159 | puts("\n"); |
| 160 | |
| 161 | if ( initrd_size ) { |
| 162 | puts("initrd at: "); |
| 163 | puthex((unsigned long)(&__ramdisk_begin)); |
| 164 | puts(" "); puthex((unsigned long)(&__ramdisk_end));puts("\n"); |
| 165 | } |
| 166 | |
| 167 | avail_ram = (char *)0x00400000; |
| 168 | end_avail = (char *)0x00800000; |
| 169 | puts("avail ram: "); puthex((unsigned long)avail_ram); puts(" "); |
| 170 | puthex((unsigned long)end_avail); puts("\n"); |
| 171 | |
| 172 | if (keyb_present) |
| 173 | CRT_tstc(); /* Forces keyboard to be initialized */ |
| 174 | #ifdef CONFIG_GEMINI |
| 175 | /* |
| 176 | * If cmd_line is empty and cmd_preset is not, copy cmd_preset |
| 177 | * to cmd_line. This way we can override cmd_preset with the |
| 178 | * command line from Smon. |
| 179 | */ |
| 180 | |
| 181 | if ( (cmd_line[0] == '\0') && (cmd_preset[0] != '\0')) |
| 182 | memcpy (cmd_line, cmd_preset, sizeof(cmd_preset)); |
| 183 | #endif |
| 184 | |
| 185 | /* Display standard Linux/PPC boot prompt for kernel args */ |
| 186 | puts("\nLinux/PPC load: "); |
| 187 | cp = cmd_line; |
| 188 | memcpy (cmd_line, cmd_preset, sizeof(cmd_preset)); |
| 189 | while ( *cp ) putc(*cp++); |
| 190 | |
| 191 | #ifdef INTERACTIVE_CONSOLE |
| 192 | /* |
| 193 | * If they have a console, allow them to edit the command line. |
| 194 | * Otherwise, don't bother wasting the five seconds. |
| 195 | */ |
| 196 | while (timer++ < 5*1000) { |
| 197 | if (tstc()) { |
| 198 | while ((ch = getc()) != '\n' && ch != '\r') { |
| 199 | /* Test for backspace/delete */ |
| 200 | if (ch == '\b' || ch == '\177') { |
| 201 | if (cp != cmd_line) { |
| 202 | cp--; |
| 203 | puts("\b \b"); |
| 204 | } |
| 205 | /* Test for ^x/^u (and wipe the line) */ |
| 206 | } else if (ch == '\030' || ch == '\025') { |
| 207 | while (cp != cmd_line) { |
| 208 | cp--; |
| 209 | puts("\b \b"); |
| 210 | } |
| 211 | } else { |
| 212 | *cp++ = ch; |
| 213 | putc(ch); |
| 214 | } |
| 215 | } |
| 216 | break; /* Exit 'timer' loop */ |
| 217 | } |
| 218 | udelay(1000); /* 1 msec */ |
| 219 | } |
| 220 | *cp = 0; |
| 221 | #endif |
| 222 | puts("\n"); |
| 223 | |
| 224 | puts("Uncompressing Linux..."); |
Christoph Hellwig | 9a59f452 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 225 | gunzip(NULL, 0x400000, zimage_start, &zimage_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | puts("done.\n"); |
| 227 | |
| 228 | /* get the bi_rec address */ |
| 229 | rec = bootinfo_addr(zimage_size); |
| 230 | |
| 231 | /* We need to make sure that the initrd and bi_recs do not |
| 232 | * overlap. */ |
| 233 | if ( initrd_size ) { |
| 234 | unsigned long rec_loc = (unsigned long) rec; |
| 235 | initrd_loc = (unsigned long)(&__ramdisk_begin); |
| 236 | /* If the bi_recs are in the middle of the current |
| 237 | * initrd, move the initrd to the next MB |
| 238 | * boundary. */ |
| 239 | if ((rec_loc > initrd_loc) && |
| 240 | ((initrd_loc + initrd_size) > rec_loc)) { |
| 241 | initrd_loc = _ALIGN((unsigned long)(zimage_size) |
| 242 | + (2 << 20) - 1, (2 << 20)); |
| 243 | memmove((void *)initrd_loc, &__ramdisk_begin, |
| 244 | initrd_size); |
| 245 | puts("initrd moved: "); puthex(initrd_loc); |
| 246 | puts(" "); puthex(initrd_loc + initrd_size); |
| 247 | puts("\n"); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | bootinfo_init(rec); |
| 252 | if ( TotalMemory ) |
| 253 | bootinfo_append(BI_MEMSIZE, sizeof(int), (void*)&TotalMemory); |
| 254 | |
| 255 | bootinfo_append(BI_CMD_LINE, strlen(cmd_line)+1, (void*)cmd_line); |
| 256 | |
| 257 | /* add a bi_rec for the initrd if it exists */ |
| 258 | if (initrd_size) { |
| 259 | unsigned long initrd[2]; |
| 260 | |
| 261 | initrd[0] = initrd_loc; |
| 262 | initrd[1] = initrd_size; |
| 263 | |
| 264 | bootinfo_append(BI_INITRD, sizeof(initrd), &initrd); |
| 265 | } |
| 266 | puts("Now booting the kernel\n"); |
| 267 | serial_close(com_port); |
| 268 | |
| 269 | return rec; |
| 270 | } |
| 271 | |
| 272 | void __attribute__ ((weak)) |
| 273 | board_isa_init(void) |
| 274 | { |
| 275 | } |
| 276 | |
| 277 | /* Allow decompress_kernel to be hooked into. This is the default. */ |
| 278 | void * __attribute__ ((weak)) |
| 279 | load_kernel(unsigned long load_addr, int num_words, unsigned long cksum, |
| 280 | void *ign1, void *ign2) |
| 281 | { |
| 282 | board_isa_init(); |
| 283 | return decompress_kernel(load_addr, num_words, cksum); |
| 284 | } |