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