misc.c 7.43 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5
/*
 * arch/ppc/common/misc-simple.c
 *
 * Misc. bootloader code for many machines.  This assumes you have are using
 * 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
Linus Torvalds's avatar
Linus Torvalds committed
7 8
 * your serial console.  If a machine meets these requirements, it can quite
 * likely use this code during boot.
9
 *
Linus Torvalds's avatar
Linus Torvalds committed
10 11 12
 * Author: Matt Porter <mporter@mvista.com>
 * Derived from arch/ppc/boot/prep/misc.c
 *
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 16
 * is licensed "as is" without any warranty of any kind, whether express
 * or implied.
Linus Torvalds's avatar
Linus Torvalds committed
17 18 19 20 21
 */

#include <linux/types.h>
#include <linux/elf.h>
#include <linux/config.h>
22
#include <linux/string.h>
Linus Torvalds's avatar
Linus Torvalds committed
23 24 25 26

#include <asm/page.h>
#include <asm/processor.h>
#include <asm/mmu.h>
27
#include <asm/bootinfo.h>
28 29 30
#ifdef CONFIG_44x
#include <asm/ibm4xx.h>
#endif
Linus Torvalds's avatar
Linus Torvalds committed
31

Linus Torvalds's avatar
Linus Torvalds committed
32
#include "nonstdio.h"
Linus Torvalds's avatar
Linus Torvalds committed
33 34
#include "zlib.h"

35
/* Default cmdline */
Linus Torvalds's avatar
Linus Torvalds committed
36 37 38 39 40
#ifdef CONFIG_CMDLINE
#define CMDLINE CONFIG_CMDLINE
#else
#define CMDLINE ""
#endif
41 42 43 44 45 46 47 48 49 50 51

/* Keyboard (and VGA console)? */
#ifdef CONFIG_VGA_CONSOLE
#define HAS_KEYB 1
#else
#define HAS_KEYB 0
#endif

char *avail_ram;
char *end_avail;
char *zimage_start;
Linus Torvalds's avatar
Linus Torvalds committed
52 53 54
char cmd_preset[] = CMDLINE;
char cmd_buf[256];
char *cmd_line = cmd_buf;
55
int keyb_present = HAS_KEYB;
Linus Torvalds's avatar
Linus Torvalds committed
56 57
int zimage_size;

58 59 60 61 62 63 64 65 66 67 68
unsigned long com_port;
unsigned long initrd_size = 0;

/* The linker tells us various locations in the image */
extern char __image_begin, __image_end;
extern char __ramdisk_begin, __ramdisk_end;
extern char _end[];
/* Original location */
extern unsigned long start;

extern int CRT_tstc(void);
Tom Rini's avatar
Tom Rini committed
69
extern unsigned long get_mem_size(void);
70 71
extern unsigned long serial_init(int chan, void *ignored);
extern void serial_close(unsigned long com_port);
Linus Torvalds's avatar
Linus Torvalds committed
72
extern void gunzip(void *, int, unsigned char *, int *);
73
extern void serial_fixups(void);
Linus Torvalds's avatar
Linus Torvalds committed
74

75
struct bi_record *
Linus Torvalds's avatar
Linus Torvalds committed
76 77 78 79
decompress_kernel(unsigned long load_addr, int num_words, unsigned long cksum)
{
	int timer = 0;
	char *cp, ch;
80 81
	struct bi_record *rec;
	unsigned long TotalMemory = 0, rec_loc, initrd_loc;
Linus Torvalds's avatar
Linus Torvalds committed
82

83
	serial_fixups();
84
	com_port = serial_init(0, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
85

86 87 88 89 90 91 92 93 94 95
#ifdef CONFIG_44x
	/* Reset MAL */
	mtdcr(DCRN_MALCR(DCRN_MAL_BASE), MALCR_MMSR);
	/* Wait for reset */
	while (mfdcr(DCRN_MALCR(DCRN_MAL_BASE)) & MALCR_MMSR) {};
	/* Reset EMAC */
	*(volatile unsigned long *)PPC44x_EMAC0_MR0 = 0x20000000;
	__asm__ __volatile__("eieio");
#endif

Tom Rini's avatar
Tom Rini committed
96 97
#if defined(CONFIG_LOPEC) || defined(CONFIG_PAL4)
	/*
98
	 * Call get_mem_size(), which is memory controller dependent,
Tom Rini's avatar
Tom Rini committed
99 100 101 102 103
	 * and we must have the correct file linked in here.
	 */
	TotalMemory = get_mem_size();
#endif

Linus Torvalds's avatar
Linus Torvalds committed
104 105 106 107 108 109 110 111
	/* assume the chunk below 8M is free */
	end_avail = (char *)0x00800000;

	/*
	 * Reveal where we were loaded at and where we
	 * were relocated to.
	 */
	puts("loaded at:     "); puthex(load_addr);
112 113
	puts(" "); puthex((unsigned long)(load_addr + (4*num_words)));
	puts("\n");
Linus Torvalds's avatar
Linus Torvalds committed
114 115 116 117 118 119 120 121
	if ( (unsigned long)load_addr != (unsigned long)&start )
	{
		puts("relocated to:  "); puthex((unsigned long)&start);
		puts(" ");
		puthex((unsigned long)((unsigned long)&start + (4*num_words)));
		puts("\n");
	}

122 123 124 125 126 127 128 129
	/*
	 * We link ourself to 0x00800000.  When we run, we relocate
	 * ourselves there.  So we just need __image_begin for the
	 * start. -- Tom
	 */
	zimage_start = (char *)(unsigned long)(&__image_begin);
	zimage_size = (unsigned long)(&__image_end) -
			(unsigned long)(&__image_begin);
Linus Torvalds's avatar
Linus Torvalds committed
130

131 132
	initrd_size = (unsigned long)(&__ramdisk_end) -
		(unsigned long)(&__ramdisk_begin);
Linus Torvalds's avatar
Linus Torvalds committed
133 134

	/*
135 136
	 * The zImage and initrd will be between start and _end, so they've
	 * already been moved once.  We're good to go now. -- Tom
Linus Torvalds's avatar
Linus Torvalds committed
137 138 139
	 */
	avail_ram = (char *)PAGE_ALIGN((unsigned long)_end);
	puts("zimage at:     "); puthex((unsigned long)zimage_start);
140 141
	puts(" "); puthex((unsigned long)(zimage_size+zimage_start));
	puts("\n");
Linus Torvalds's avatar
Linus Torvalds committed
142

143 144 145 146
	if ( initrd_size ) {
		puts("initrd at:     ");
		puthex((unsigned long)(&__ramdisk_begin));
		puts(" "); puthex((unsigned long)(&__ramdisk_end));puts("\n");
Linus Torvalds's avatar
Linus Torvalds committed
147 148 149 150 151 152 153
	}

	avail_ram = (char *)0x00400000;
	end_avail = (char *)0x00800000;
	puts("avail ram:     "); puthex((unsigned long)avail_ram); puts(" ");
	puthex((unsigned long)end_avail); puts("\n");

154 155 156 157 158 159 160 161 162 163 164 165 166
	if (keyb_present)
		CRT_tstc();  /* Forces keyboard to be initialized */
#ifdef CONFIG_GEMINI
	/*
	 * If cmd_line is empty and cmd_preset is not, copy cmd_preset
	 * to cmd_line.  This way we can override cmd_preset with the
	 * command line from Smon.
	 */

	if ( (cmd_line[0] == '\0') && (cmd_preset[0] != '\0'))
		memcpy (cmd_line, cmd_preset, sizeof(cmd_preset));
#endif

Linus Torvalds's avatar
Linus Torvalds committed
167 168 169 170 171
	/* Display standard Linux/PPC boot prompt for kernel args */
	puts("\nLinux/PPC load: ");
	cp = cmd_line;
	memcpy (cmd_line, cmd_preset, sizeof(cmd_preset));
	while ( *cp ) putc(*cp++);
172 173 174 175

#ifndef CONFIG_GEMINI
	/* Val Henson has requested that Gemini doesn't wait for the
	 * user to edit the cmdline or not. */
Linus Torvalds's avatar
Linus Torvalds committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	while (timer++ < 5*1000) {
		if (tstc()) {
			while ((ch = getc()) != '\n' && ch != '\r') {
				/* Test for backspace/delete */
				if (ch == '\b' || ch == '\177') {
					if (cp != cmd_line) {
						cp--;
						puts("\b \b");
					}
				/* Test for ^x/^u (and wipe the line) */
				} else if (ch == '\030' || ch == '\025') {
					while (cp != cmd_line) {
						cp--;
						puts("\b \b");
					}
				} else {
					*cp++ = ch;
					putc(ch);
				}
			}
			break;  /* Exit 'timer' loop */
		}
		udelay(1000);  /* 1 msec */
	}
	*cp = 0;
201
#endif
Linus Torvalds's avatar
Linus Torvalds committed
202 203 204 205 206 207
	puts("\n");

	puts("Uncompressing Linux...");
	gunzip(0, 0x400000, zimage_start, &zimage_size);
	puts("done.\n");

208 209 210
	/*
	 * Create bi_recs for cmd_line and initrds
	 */
211
	rec_loc = _ALIGN((unsigned long)(zimage_size) +
212
			(1 << 20) - 1, (1 << 20));
213
	rec = (struct bi_record *)rec_loc;
214

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	/* We need to make sure that the initrd and bi_recs do not
	 * overlap. */
	if ( initrd_size ) {
		initrd_loc = (unsigned long)(&__ramdisk_begin);
		/* If the bi_recs are in the middle of the current
		 * initrd, move the initrd to the next MB
		 * boundary. */
		if ((rec_loc > initrd_loc) &&
				((initrd_loc + initrd_size) > rec_loc)) {
			initrd_loc = _ALIGN((unsigned long)(zimage_size)
					+ (2 << 20) - 1, (2 << 20));
		 	memmove((void *)initrd_loc, &__ramdisk_begin,
				 initrd_size);
	         	puts("initrd moved:  "); puthex(initrd_loc);
		 	puts(" "); puthex(initrd_loc + initrd_size);
		 	puts("\n");
		}
	}
 
234 235 236 237
	rec->tag = BI_FIRST;
	rec->size = sizeof(struct bi_record);
	rec = (struct bi_record *)((unsigned long)rec + rec->size);

Tom Rini's avatar
Tom Rini committed
238 239 240 241 242 243 244
	if ( TotalMemory ) {
		rec->tag = BI_MEMSIZE;
		rec->data[0] = TotalMemory;
		rec->size = sizeof(struct bi_record) + sizeof(unsigned long);
		rec = (struct bi_record *)((unsigned long)rec + rec->size);
	}

245 246 247 248 249 250 251
	rec->tag = BI_CMD_LINE;
	memcpy( (char *)rec->data, cmd_line, strlen(cmd_line)+1);
	rec->size = sizeof(struct bi_record) + strlen(cmd_line) + 1;
	rec = (struct bi_record *)((unsigned long)rec + rec->size);

	if ( initrd_size ) {
		rec->tag = BI_INITRD;
252
		rec->data[0] = initrd_loc;
253 254 255 256 257 258 259 260 261 262
		rec->data[1] = initrd_size;
		rec->size = sizeof(struct bi_record) + 2 *
			sizeof(unsigned long);
		rec = (struct bi_record *)((unsigned long)rec +
				rec->size);
	}

	rec->tag = BI_LAST;
	rec->size = sizeof(struct bi_record);
	rec = (struct bi_record *)((unsigned long)rec + rec->size);
Linus Torvalds's avatar
Linus Torvalds committed
263
	puts("Now booting the kernel\n");
264 265
	serial_close(com_port);

266
	return (struct bi_record *)rec_loc;
Linus Torvalds's avatar
Linus Torvalds committed
267
}
268 269 270 271 272 273 274

/* Allow decompress_kernel to be hooked into.  This is the default. */
void * __attribute__ ((weak))
load_kernel(unsigned long load_addr, int num_words, unsigned long cksum)
{
		return decompress_kernel(load_addr, num_words, cksum);
}