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