1 /*
2  * arch/ppc/boot/prep/misc.c
3  *
4  * Adapted for PowerPC by Gary Thomas
5  *
6  * Rewritten by Cort Dougan (cort@cs.nmt.edu)
7  * One day to be replaced by a single bootloader for chrp/prep/pmac. -- Cort
8  */
9 
10 #include <linux/types.h>
11 #include <asm/residual.h>
12 #include <linux/config.h>
13 #include <linux/threads.h>
14 #include <linux/elf.h>
15 #include <linux/pci_ids.h>
16 #include <asm/page.h>
17 #include <asm/processor.h>
18 #include <asm/bootinfo.h>
19 #include <asm/mmu.h>
20 #include <asm/byteorder.h>
21 #include "of1275.h"
22 #include "nonstdio.h"
23 #include "zlib.h"
24 
25 #ifdef CONFIG_CMDLINE
26 #define CMDLINE CONFIG_CMDLINE
27 #else
28 #define CMDLINE ""
29 #endif
30 
31 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_VGA_CONSOLE)
32 #define INTERACTIVE_CONSOLE	1
33 #endif
34 
35 /*
36  * Please send me load/board info and such data for hardware not
37  * listed here so I can keep track since things are getting tricky
38  * with the different load addrs with different firmware.  This will
39  * help to avoid breaking the load/boot process.
40  * -- Cort
41  */
42 char *avail_ram;
43 char *end_avail;
44 
45 /* The linker tells us where the image is. */
46 extern char __image_begin, __image_end;
47 extern char __ramdisk_begin, __ramdisk_end;
48 extern char _end[];
49 
50 char cmd_preset[] = CMDLINE;
51 char cmd_buf[256];
52 char *cmd_line = cmd_buf;
53 
54 int keyb_present = 1;	/* keyboard controller is present by default */
55 RESIDUAL hold_resid_buf;
56 RESIDUAL *hold_residual = &hold_resid_buf;
57 unsigned long initrd_size = 0;
58 
59 char *zimage_start;
60 int zimage_size;
61 
62 unsigned long com_port;
63 #ifdef CONFIG_VGA_CONSOLE
64 char *vidmem = (char *)0xC00B8000;
65 int lines = 25, cols = 80;
66 int orig_x, orig_y = 24;
67 #endif /* CONFIG_VGA_CONSOLE */
68 
69 extern int CRT_tstc(void);
70 extern int vga_init(unsigned char *ISA_mem);
71 extern void gunzip(void *, int, unsigned char *, int *);
72 
73 extern unsigned long serial_init(int chan, void *ignored);
74 extern void serial_fixups(void);
75 
76 void
writel(unsigned int val,unsigned int address)77 writel(unsigned int val, unsigned int address)
78 {
79 	/* Ensure I/O operations complete */
80 	__asm__ volatile("eieio");
81 	*(unsigned int *)address = cpu_to_le32(val);
82 }
83 
84 unsigned int
readl(unsigned int address)85 readl(unsigned int address)
86 {
87 	/* Ensure I/O operations complete */
88 	__asm__ volatile("eieio");
89 	return le32_to_cpu(*(unsigned int *)address);
90 }
91 
92 #define PCI_CFG_ADDR(dev,off)	((0x80<<24) | (dev<<8) | (off&0xfc))
93 #define PCI_CFG_DATA(off)	(0x80000cfc+(off&3))
94 
95 static void
pci_read_config_32(unsigned char devfn,unsigned char offset,unsigned int * val)96 pci_read_config_32(unsigned char devfn,
97 		unsigned char offset,
98 		unsigned int *val)
99 {
100 	writel(PCI_CFG_ADDR(devfn,offset), 0x80000cf8);
101 	*val = readl(PCI_CFG_DATA(offset));
102 	return;
103 }
104 
105 #ifdef CONFIG_VGA_CONSOLE
106 void
scroll(void)107 scroll(void)
108 {
109 	int i;
110 
111 	memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
112 	for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
113 		vidmem[i] = ' ';
114 }
115 #endif /* CONFIG_VGA_CONSOLE */
116 
117 static void
get_of_args(void)118 get_of_args(void)
119 {
120 	int size;
121 	phandle chosen;
122 	char buf[256];
123 
124 	/* Get bootargs property of /chosen node */
125 	if (!(chosen = finddevice("/chosen")))
126 		return;
127 
128 	if (getprop(chosen, "bootargs", buf, sizeof(buf)) != 4)
129 		return;
130 
131 	if (size > sizeof(cmd_buf))
132 		size = sizeof(cmd_buf);
133 
134 	if (size > 1) /* includes null-terminator */
135 		memcpy(cmd_buf, buf, size);
136 }
137 
138 unsigned long
decompress_kernel(unsigned long load_addr,int num_words,unsigned long cksum,RESIDUAL * residual,void * OFW_interface)139 decompress_kernel(unsigned long load_addr, int num_words, unsigned long cksum,
140 		  RESIDUAL *residual, void *OFW_interface)
141 {
142 #ifdef INTERACTIVE_CONSOLE
143 	int timer = 0;
144 	char ch;
145 #endif
146 	extern unsigned long start;
147 	char *cp;
148 	/* Default to 32MiB memory. */
149 	unsigned long TotalMemory = 0x02000000;
150 	int start_multi = 0;
151 	unsigned int pci_viddid, pci_did, tulip_pci_base, tulip_base;
152 
153 	/* If we have Open Firmware, initialise it immediately */
154 	if (OFW_interface)
155 		ofinit(OFW_interface);
156 
157 	serial_fixups();
158 	com_port = serial_init(0, NULL);
159 #if defined(CONFIG_VGA_CONSOLE)
160 	vga_init((unsigned char *)0xC0000000);
161 #endif /* CONFIG_VGA_CONSOLE */
162 
163 	/*
164 	 * Tell the user where we were loaded at and where we were relocated
165 	 * to for debugging this process.
166 	 */
167 	puts("loaded at:     "); puthex(load_addr);
168 	puts(" "); puthex((unsigned long)(load_addr + (4*num_words))); puts("\n");
169 	if ( (unsigned long)load_addr != (unsigned long)&start ) {
170 		puts("relocated to:  "); puthex((unsigned long)&start);
171 		puts(" ");
172 		puthex((unsigned long)((unsigned long)&start + (4*num_words)));
173 		puts("\n");
174 	}
175 
176 	if (residual) {
177 		/*
178 		 * Tell the user where the residual data is.
179 		 */
180 		puts("board data at: "); puthex((unsigned long)residual);
181 		puts(" ");
182 		puthex((unsigned long)((unsigned long)residual +
183 					sizeof(RESIDUAL)));
184 		puts("\nrelocated to:  ");puthex((unsigned long)hold_residual);
185 		puts(" ");
186 		puthex((unsigned long)((unsigned long)hold_residual +
187 					sizeof(RESIDUAL)));
188 		puts("\n");
189 
190 		/* Is this Motorola PPCBug? */
191 		if ((1 & residual->VitalProductData.FirmwareSupports) &&
192 		    (1 == residual->VitalProductData.FirmwareSupplier)) {
193 			unsigned char base_mod;
194 			unsigned char board_type = inb(0x800) & 0xF0;
195 
196 			/*
197 			 * Reset the onboard 21x4x Ethernet
198 			 * Motorola Ethernet is at IDSEL 14 (devfn 0x70)
199 			 */
200 			pci_read_config_32(0x70, 0x00, &pci_viddid);
201 			pci_did = (pci_viddid & 0xffff0000) >> 16;
202 			/* Be sure we've really found a 21x4x chip */
203 			if (((pci_viddid & 0xffff) == PCI_VENDOR_ID_DEC) &&
204 				((pci_did == PCI_DEVICE_ID_DEC_TULIP_FAST) ||
205 				(pci_did == PCI_DEVICE_ID_DEC_TULIP) ||
206 				(pci_did == PCI_DEVICE_ID_DEC_TULIP_PLUS) ||
207 				(pci_did == PCI_DEVICE_ID_DEC_21142))) {
208 				pci_read_config_32(0x70,
209 						0x10,
210 						&tulip_pci_base);
211 				/* Get the physical base address */
212 				tulip_base =
213 					(tulip_pci_base & ~0x03UL) + 0x80000000;
214 				/* Strobe the 21x4x reset bit in CSR0 */
215 				writel(0x1, tulip_base);
216 			}
217 
218 			/* If this is genesis 2 board then check for no
219 			 * keyboard controller and more than one processor.
220 			 */
221 			if (board_type == 0xe0) {
222 				base_mod = inb(0x803);
223 				/* if a MVME2300/2400 or a Sitka then no keyboard */
224 				if((base_mod == 0xFA) || (base_mod == 0xF9) ||
225 				   (base_mod == 0xE1)) {
226 					keyb_present = 0;	/* no keyboard */
227 				}
228 			}
229 			/* If this is a multiprocessor system then
230 			 * park the other processor so that the
231 			 * kernel knows where to find them.
232 			 */
233 			if (residual->MaxNumCpus > 1)
234 				start_multi = 1;
235 		}
236 		memcpy(hold_residual,residual,sizeof(RESIDUAL));
237 
238 		/* Copy the memory info. */
239 		if (residual->TotalMemory)
240 			TotalMemory = residual->TotalMemory;
241 	} else {
242 		/* Tell the user we didn't find anything. */
243 		puts("No residual data found.\n");
244 
245 		/*
246 		 * This is a 'best guess' check.  We want to make sure
247 		 * we don't try this on a PReP box without OF
248 		 *     -- Cort
249 		 */
250 		while (OFW_interface)
251 		{
252 			phandle dev_handle;
253 			int mem_info[2];
254 
255 			/* get handle to memory description */
256 			if (!(dev_handle = finddevice("/memory@0")))
257 				break;
258 
259 			/* get the info */
260 			if (getprop(dev_handle, "reg", mem_info,
261 						sizeof(mem_info)) !=8)
262 				break;
263 
264 			TotalMemory = mem_info[1];
265 			break;
266 		}
267         }
268 
269 	/* assume the chunk below 8M is free */
270 	end_avail = (char *)0x00800000;
271 
272 	/*
273 	 * We link ourself to 0x00800000.  When we run, we relocate
274 	 * ourselves there.  So we just need __image_begin for the
275 	 * start. -- Tom
276 	 */
277 	zimage_start = (char *)(unsigned long)(&__image_begin);
278 	zimage_size = (unsigned long)(&__image_end) -
279 			(unsigned long)(&__image_begin);
280 
281 	initrd_size = (unsigned long)(&__ramdisk_end) -
282 		(unsigned long)(&__ramdisk_begin);
283 
284 	/*
285 	 * The zImage and initrd will be between start and _end, so they've
286 	 * already been moved once.  We're good to go now. -- Tom
287 	 */
288 	avail_ram = (char *)PAGE_ALIGN((unsigned long)_end);
289 	puts("zimage at:     "); puthex((unsigned long)zimage_start);
290 	puts(" "); puthex((unsigned long)(zimage_size+zimage_start));
291 	puts("\n");
292 
293 	if ( initrd_size ) {
294 		puts("initrd at:     ");
295 		puthex((unsigned long)(&__ramdisk_begin));
296 		puts(" "); puthex((unsigned long)(&__ramdisk_end));puts("\n");
297 	}
298 
299 	avail_ram = (char *)0x00400000;
300 	end_avail = (char *)0x00800000;
301 	puts("avail ram:     "); puthex((unsigned long)avail_ram); puts(" ");
302 	puthex((unsigned long)end_avail); puts("\n");
303 
304 	if (keyb_present)
305 		CRT_tstc();  /* Forces keyboard to be initialized */
306 
307 	/* If we have OF then try to get args from there */
308 	if (OFW_interface)
309 		get_of_args();
310 
311 	puts("\nLinux/PPC load: ");
312 	cp = cmd_line;
313 	memcpy (cmd_line, cmd_preset, sizeof(cmd_preset));
314 	while ( *cp )
315 		putc(*cp++);
316 #ifdef INTERACTIVE_CONSOLE
317 	/*
318 	 * If they have a console, allow them to edit the command line.
319 	 * Otherwise, don't bother wasting the five seconds.
320 	 */
321 	while (timer++ < 5*1000) {
322 		if (tstc()) {
323 			while ((ch = getc()) != '\n' && ch != '\r') {
324 				/* Test for backspace/delete */
325 				if (ch == '\b' || ch == '\177') {
326 					if (cp != cmd_line) {
327 						cp--;
328 						puts("\b \b");
329 					}
330 				/* Test for ^x/^u (and wipe the line) */
331 				} else if (ch == '\030' || ch == '\025') {
332 					while (cp != cmd_line) {
333 						cp--;
334 						puts("\b \b");
335 					}
336 				} else {
337 					*cp++ = ch;
338 					putc(ch);
339 				}
340 			}
341 			break;  /* Exit 'timer' loop */
342 		}
343 		udelay(1000);  /* 1 msec */
344 	}
345 #endif
346 	*cp = 0;
347 	puts("\nUncompressing Linux...");
348 
349 	/*
350 	 * If we have OF, then we have deferred setting the MSR.
351 	 * We must set it now because we are about to overwrite
352 	 * the exception table.  The new MSR value will disable
353 	 * machine check exceptions and point the exception table
354 	 * to the ROM.
355 	 */
356 	if (OFW_interface) {
357 		mtmsr(MSR_IP | MSR_FP);
358 		asm volatile("isync");
359 	}
360 
361 	gunzip(0, 0x400000, zimage_start, &zimage_size);
362 	puts("done.\n");
363 
364 	if (start_multi) {
365 		puts("Parking cpu1 at 0xc0\n");
366 		residual->VitalProductData.SmpIar = (unsigned long)0xc0;
367 		residual->Cpus[1].CpuState = CPU_GOOD;
368 		hold_residual->VitalProductData.Reserved5 = 0xdeadbeef;
369 	}
370 
371 	{
372 		struct bi_record *rec;
373 		unsigned long initrd_loc;
374 		unsigned long rec_loc = _ALIGN((unsigned long)(zimage_size) +
375 				(1 << 20) - 1, (1 << 20));
376 
377 		rec = (struct bi_record *)rec_loc;
378 
379 		/* We need to make sure that the initrd and bi_recs do not
380 		 * overlap. */
381 		if ( initrd_size ) {
382 			initrd_loc = (unsigned long)(&__ramdisk_begin);
383 			/* If the bi_recs are in the middle of the current
384 			 * initrd, move the initrd to the next MB
385 			 * boundary. */
386 			if ((rec_loc > initrd_loc) &&
387 					((initrd_loc + initrd_size)
388 					 > rec_loc)) {
389 				initrd_loc = _ALIGN((unsigned long)(zimage_size)
390 						+ (2 << 20) - 1, (2 << 20));
391 			 	memmove((void *)initrd_loc, &__ramdisk_begin,
392 					 initrd_size);
393 		         	puts("initrd moved:  "); puthex(initrd_loc);
394 			 	puts(" "); puthex(initrd_loc + initrd_size);
395 			 	puts("\n");
396 			}
397 		}
398 
399 		rec->tag = BI_FIRST;
400 		rec->size = sizeof(struct bi_record);
401 		rec = (struct bi_record *)((unsigned long)rec + rec->size);
402 
403 		rec->tag = BI_MEMSIZE;
404 		rec->data[0] = TotalMemory;
405 		rec->size = sizeof(struct bi_record) + sizeof(unsigned long);
406 		rec = (struct bi_record *)((unsigned long)rec + rec->size);
407 
408 		rec->tag = BI_BOOTLOADER_ID;
409 		memcpy( (void *)rec->data, "prepboot", 9);
410 		rec->size = sizeof(struct bi_record) + 8 + 1;
411 		rec = (struct bi_record *)((unsigned long)rec + rec->size);
412 
413 		rec->tag = BI_MACHTYPE;
414 		rec->data[0] = _MACH_prep;
415 		rec->data[1] = 0;
416 		rec->size = sizeof(struct bi_record) + 2 *
417 			sizeof(unsigned long);
418 		rec = (struct bi_record *)((unsigned long)rec + rec->size);
419 
420 		rec->tag = BI_CMD_LINE;
421 		memcpy( (char *)rec->data, cmd_line, strlen(cmd_line)+1);
422 		rec->size = sizeof(struct bi_record) + strlen(cmd_line) + 1;
423 		rec = (struct bi_record *)((unsigned long)rec + rec->size);
424 
425 		if ( initrd_size ) {
426 			rec->tag = BI_INITRD;
427 			rec->data[0] = initrd_loc;
428 			rec->data[1] = initrd_size;
429 			rec->size = sizeof(struct bi_record) + 2 *
430 				sizeof(unsigned long);
431 			rec = (struct bi_record *)((unsigned long)rec +
432 					rec->size);
433 		}
434 
435 		rec->tag = BI_LAST;
436 		rec->size = sizeof(struct bi_record);
437 		rec = (struct bi_record *)((unsigned long)rec + rec->size);
438 	}
439 	puts("Now booting the kernel\n");
440 	return (unsigned long)hold_residual;
441 }
442