1 /*
2  * Common prep/pmac/chrp boot and setup code.
3  */
4 
5 #include <linux/config.h>
6 #include <linux/module.h>
7 #include <linux/string.h>
8 #include <linux/sched.h>
9 #include <linux/init.h>
10 #include <linux/reboot.h>
11 #include <linux/delay.h>
12 #include <linux/blk.h>
13 #include <linux/ide.h>
14 #include <linux/tty.h>
15 #include <linux/bootmem.h>
16 #include <linux/seq_file.h>
17 
18 #include <asm/residual.h>
19 #include <asm/io.h>
20 #include <asm/prom.h>
21 #include <asm/processor.h>
22 #include <asm/pgtable.h>
23 #include <asm/bootinfo.h>
24 #include <asm/setup.h>
25 #include <asm/amigappc.h>
26 #include <asm/smp.h>
27 #include <asm/elf.h>
28 #include <asm/cputable.h>
29 #include <asm/bootx.h>
30 #include <asm/btext.h>
31 #include <asm/machdep.h>
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <asm/pmac_feature.h>
35 #include <asm/kgdb.h>
36 
37 extern void platform_init(unsigned long r3, unsigned long r4,
38 		unsigned long r5, unsigned long r6, unsigned long r7);
39 extern void bootx_init(unsigned long r4, unsigned long phys);
40 extern void identify_cpu(unsigned long offset, unsigned long cpu);
41 extern void do_cpu_ftr_fixups(unsigned long offset);
42 extern void reloc_got2(unsigned long offset);
43 
44 #ifdef CONFIG_XMON
45 extern void xmon_map_scc(void);
46 #endif
47 
48 extern boot_infos_t *boot_infos;
49 char saved_command_line[512];
50 extern char cmd_line[512];
51 unsigned char aux_device_present;
52 struct ide_machdep_calls ppc_ide_md;
53 char *sysmap;
54 unsigned long sysmap_size;
55 
56 /* Used with the BI_MEMSIZE bootinfo parameter to store the memory
57    size value reported by the boot loader. */
58 unsigned long boot_mem_size;
59 
60 unsigned long ISA_DMA_THRESHOLD;
61 unsigned long DMA_MODE_READ, DMA_MODE_WRITE;
62 
63 #ifdef CONFIG_ALL_PPC
64 int _machine = 0;
65 
66 extern void prep_init(unsigned long r3, unsigned long r4,
67 		unsigned long r5, unsigned long r6, unsigned long r7);
68 extern void pmac_init(unsigned long r3, unsigned long r4,
69 		unsigned long r5, unsigned long r6, unsigned long r7);
70 extern void chrp_init(unsigned long r3, unsigned long r4,
71 		unsigned long r5, unsigned long r6, unsigned long r7);
72 #endif /* CONFIG_ALL_PPC */
73 
74 #ifdef CONFIG_MAGIC_SYSRQ
75 unsigned long SYSRQ_KEY;
76 #endif /* CONFIG_MAGIC_SYSRQ */
77 
78 #ifdef CONFIG_VGA_CONSOLE
79 unsigned long vgacon_remap_base;
80 #endif
81 
82 struct machdep_calls ppc_md;
83 
84 /*
85  * These are used in binfmt_elf.c to put aux entries on the stack
86  * for each elf executable being started.
87  */
88 int dcache_bsize;
89 int icache_bsize;
90 int ucache_bsize;
91 
92 #if defined(CONFIG_VGA_CONSOLE) || defined(CONFIG_FB)
93 struct screen_info screen_info = {
94 	0, 25,			/* orig-x, orig-y */
95 	0,			/* unused */
96 	0,			/* orig-video-page */
97 	0,			/* orig-video-mode */
98 	80,			/* orig-video-cols */
99 	0,0,0,			/* ega_ax, ega_bx, ega_cx */
100 	25,			/* orig-video-lines */
101 	1,			/* orig-video-isVGA */
102 	16			/* orig-video-points */
103 };
104 #endif /* CONFIG_VGA_CONSOLE || CONFIG_FB */
105 
machine_restart(char * cmd)106 void machine_restart(char *cmd)
107 {
108 	ppc_md.restart(cmd);
109 }
110 
machine_power_off(void)111 void machine_power_off(void)
112 {
113 	ppc_md.power_off();
114 }
115 
machine_halt(void)116 void machine_halt(void)
117 {
118 	ppc_md.halt();
119 }
120 
121 #ifdef CONFIG_TAU
122 extern u32 cpu_temp(unsigned long cpu);
123 extern u32 cpu_temp_both(unsigned long cpu);
124 #endif /* CONFIG_TAU */
125 
show_cpuinfo(struct seq_file * m,void * v)126 int show_cpuinfo(struct seq_file *m, void *v)
127 {
128 	int i = (int) v - 1;
129 	int err = 0;
130 	unsigned int pvr;
131 	unsigned short maj, min;
132 	unsigned long lpj;
133 
134 	if (i >= NR_CPUS) {
135 		/* Show summary information */
136 #ifdef CONFIG_SMP
137 		unsigned long bogosum = 0;
138 		for (i = 0; i < smp_num_cpus; ++i)
139 			if (cpu_online_map & (1 << i))
140 				bogosum += cpu_data[i].loops_per_jiffy;
141 		seq_printf(m, "total bogomips\t: %lu.%02lu\n",
142 			   bogosum/(500000/HZ), bogosum/(5000/HZ) % 100);
143 #endif /* CONFIG_SMP */
144 
145 		if (ppc_md.show_cpuinfo != NULL)
146 			err = ppc_md.show_cpuinfo(m);
147 		return err;
148 	}
149 
150 #ifdef CONFIG_SMP
151 	if (!(cpu_online_map & (1 << i)))
152 		return 0;
153 	pvr = cpu_data[i].pvr;
154 	lpj = cpu_data[i].loops_per_jiffy;
155 #else
156 	pvr = mfspr(PVR);
157 	lpj = loops_per_jiffy;
158 #endif
159 
160 	seq_printf(m, "processor\t: %u\n", i);
161 	seq_printf(m, "cpu\t\t: ");
162 
163 	if (cur_cpu_spec[i]->pvr_mask)
164 		seq_printf(m, "%s", cur_cpu_spec[i]->cpu_name);
165 	else
166 		seq_printf(m, "unknown (%08x)", pvr);
167 #ifdef CONFIG_ALTIVEC
168 	if (cur_cpu_spec[i]->cpu_features & CPU_FTR_ALTIVEC)
169 		seq_printf(m, ", altivec supported");
170 #endif
171 	seq_printf(m, "\n");
172 
173 #ifdef CONFIG_TAU
174 	if (cur_cpu_spec[i]->cpu_features & CPU_FTR_TAU) {
175 #ifdef CONFIG_TAU_AVERAGE
176 		/* more straightforward, but potentially misleading */
177 		seq_printf(m,  "temperature \t: %u C (uncalibrated)\n",
178 			   cpu_temp(i));
179 #else
180 		/* show the actual temp sensor range */
181 		u32 temp;
182 		temp = cpu_temp_both(i);
183 		seq_printf(m, "temperature \t: %u-%u C (uncalibrated)\n",
184 			   temp & 0xff, temp >> 16);
185 #endif
186 	}
187 #endif /* CONFIG_TAU */
188 
189 	if (ppc_md.show_percpuinfo != NULL) {
190 		err = ppc_md.show_percpuinfo(m, i);
191 		if (err)
192 			return err;
193 	}
194 
195 	switch (PVR_VER(pvr)) {
196 	case 0x0020:	/* 403 family */
197 		maj = PVR_MAJ(pvr) + 1;
198 		min = PVR_MIN(pvr);
199 		break;
200 	case 0x1008:	/* 740P/750P ?? */
201 		maj = ((pvr >> 8) & 0xFF) - 1;
202 		min = pvr & 0xFF;
203 		break;
204 	default:
205 		maj = (pvr >> 8) & 0xFF;
206 		min = pvr & 0xFF;
207 		break;
208 	}
209 
210 	seq_printf(m, "revision\t: %hd.%hd (pvr %04x %04x)\n",
211 		   maj, min, PVR_VER(pvr), PVR_REV(pvr));
212 
213 	seq_printf(m, "bogomips\t: %lu.%02lu\n",
214 		   lpj / (500000/HZ), (lpj / (5000/HZ)) % 100);
215 
216 #ifdef CONFIG_SMP
217 	seq_printf(m, "\n");
218 #endif
219 
220 	return 0;
221 }
222 
223 
c_start(struct seq_file * m,loff_t * pos)224 static void *c_start(struct seq_file *m, loff_t *pos)
225 {
226 	int i = *pos;
227 
228 	return i <= NR_CPUS? (void *) (i + 1): NULL;
229 }
230 
c_next(struct seq_file * m,void * v,loff_t * pos)231 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
232 {
233 	++*pos;
234 	return c_start(m, pos);
235 }
236 
c_stop(struct seq_file * m,void * v)237 static void c_stop(struct seq_file *m, void *v)
238 {
239 }
240 
241 struct seq_operations cpuinfo_op = {
242 	start:	c_start,
243 	next:	c_next,
244 	stop:	c_stop,
245 	show:	show_cpuinfo,
246 };
247 
248 /*
249  * We're called here very early in the boot.  We determine the machine
250  * type and call the appropriate low-level setup functions.
251  *  -- Cort <cort@fsmlabs.com>
252  *
253  * Note that the kernel may be running at an address which is different
254  * from the address that it was linked at, so we must use RELOC/PTRRELOC
255  * to access static data (including strings).  -- paulus
256  */
257 __init
258 unsigned long
early_init(int r3,int r4,int r5)259 early_init(int r3, int r4, int r5)
260 {
261 	extern char __bss_start[], _end[];
262  	unsigned long phys;
263 	unsigned long offset = reloc_offset();
264 
265  	/* Default */
266  	phys = offset + KERNELBASE;
267 
268 	/* First zero the BSS -- use memset, some arches don't have
269 	 * caches on yet */
270 	memset_io(PTRRELOC(&__bss_start), 0, _end - __bss_start);
271 
272 	/*
273 	 * Identify the CPU type and fix up code sections
274 	 * that depend on which cpu we have.
275 	 */
276 	identify_cpu(offset, 0);
277 	do_cpu_ftr_fixups(offset);
278 
279 #if defined(CONFIG_ALL_PPC)
280 	reloc_got2(offset);
281 
282 	/* If we came here from BootX, clear the screen,
283 	 * set up some pointers and return. */
284 	if ((r3 == 0x426f6f58) && (r5 == 0))
285 		bootx_init(r4, phys);
286 
287 	/*
288 	 * don't do anything on prep
289 	 * for now, don't use bootinfo because it breaks yaboot 0.5
290 	 * and assume that if we didn't find a magic number, we have OF
291 	 */
292 	else if (*(unsigned long *)(0) != 0xdeadc0de)
293 		phys = prom_init(r3, r4, (prom_entry)r5);
294 
295 	reloc_got2(-offset);
296 #endif
297 
298 	return phys;
299 }
300 
301 #ifdef CONFIG_ALL_PPC
302 void __init
intuit_machine_type(void)303 intuit_machine_type(void)
304 {
305 	char *model;
306 	struct device_node *root;
307 
308 	/* ask the OF info if we're a chrp or pmac */
309 	root = find_path_device("/");
310 	if (root != 0) {
311 		/* assume pmac unless proven to be chrp -- Cort */
312 		_machine = _MACH_Pmac;
313 		model = get_property(root, "device_type", NULL);
314 		if (model && !strncmp("chrp", model, 4))
315 			_machine = _MACH_chrp;
316 		else {
317 			model = get_property(root, "model", NULL);
318 			if (model && !strncmp(model, "IBM", 3))
319 				_machine = _MACH_chrp;
320 		}
321 	}
322 }
323 
324 /*
325  * The ALL_PPC version of platform_init...
326  */
327 void __init
platform_init(unsigned long r3,unsigned long r4,unsigned long r5,unsigned long r6,unsigned long r7)328 platform_init(unsigned long r3, unsigned long r4, unsigned long r5,
329 	      unsigned long r6, unsigned long r7)
330 {
331 #ifdef CONFIG_BOOTX_TEXT
332 	if (boot_text_mapped) {
333 		btext_clearscreen();
334 		btext_welcome();
335 	}
336 #endif
337 
338 	parse_bootinfo(find_bootinfo());
339 
340 	/* if we didn't get any bootinfo telling us what we are... */
341 	if (_machine == 0) {
342 		/* prep boot loader tells us if we're prep or not */
343 		if ( *(unsigned long *)(KERNELBASE) == (0xdeadc0de) )
344 			_machine = _MACH_prep;
345 	}
346 
347 	/* not much more to do here, if prep */
348 	if (_machine == _MACH_prep) {
349 		prep_init(r3, r4, r5, r6, r7);
350 		return;
351 	}
352 
353 	/* prom_init has already been called from __start */
354 	if (boot_infos)
355 		relocate_nodes();
356 
357 	/* If we aren't PReP, we can find out if we're Pmac
358 	 * or CHRP with this. */
359 	if (_machine == 0)
360 		intuit_machine_type();
361 
362 	/* finish_device_tree may need _machine defined. */
363 	finish_device_tree();
364 
365 	/*
366 	 * If we were booted via quik, r3 points to the physical
367 	 * address of the command-line parameters.
368 	 * If we were booted from an xcoff image (i.e. netbooted or
369 	 * booted from floppy), we get the command line from the
370 	 * bootargs property of the /chosen node.
371 	 * If an initial ramdisk is present, r3 and r4
372 	 * are used for initrd_start and initrd_size,
373 	 * otherwise they contain 0xdeadbeef.
374 	 */
375 	if (r3 >= 0x4000 && r3 < 0x800000 && r4 == 0) {
376 		cmd_line[0] = 0;
377 		strncpy(cmd_line, (char *)r3 + KERNELBASE,
378 			sizeof(cmd_line));
379 	} else if (boot_infos != 0) {
380 		/* booted by BootX - check for ramdisk */
381 		if (boot_infos->kernelParamsOffset != 0)
382 			strncpy(cmd_line, (char *) boot_infos
383 				+ boot_infos->kernelParamsOffset,
384 				sizeof(cmd_line));
385 #ifdef CONFIG_BLK_DEV_INITRD
386 		if (boot_infos->ramDisk) {
387 			initrd_start = (unsigned long) boot_infos
388 				+ boot_infos->ramDisk;
389 			initrd_end = initrd_start + boot_infos->ramDiskSize;
390 			initrd_below_start_ok = 1;
391 		}
392 #endif
393 	} else {
394 		struct device_node *chosen;
395 		char *p;
396 
397 #ifdef CONFIG_BLK_DEV_INITRD
398 		if (r3 && r4 && r4 != 0xdeadbeef) {
399 			if (r3 < KERNELBASE)
400 				r3 += KERNELBASE;
401 			initrd_start = r3;
402 			initrd_end = r3 + r4;
403 			ROOT_DEV = MKDEV(RAMDISK_MAJOR, 0);
404 			initrd_below_start_ok = 1;
405 		}
406 #endif
407 		chosen = find_devices("chosen");
408 		if (chosen != NULL) {
409 			p = get_property(chosen, "bootargs", NULL);
410 			if (p && *p) {
411 				cmd_line[0] = 0;
412 				strncpy(cmd_line, p, sizeof(cmd_line));
413 			}
414 		}
415 	}
416 	cmd_line[sizeof(cmd_line) - 1] = 0;
417 
418 	switch (_machine) {
419 	case _MACH_Pmac:
420 		pmac_init(r3, r4, r5, r6, r7);
421 		break;
422 	case _MACH_chrp:
423 		chrp_init(r3, r4, r5, r6, r7);
424 		break;
425 	}
426 }
427 #endif /* CONFIG_ALL_PPC */
428 
429 #ifndef CONFIG_APUS
find_bootinfo(void)430 struct bi_record *find_bootinfo(void)
431 {
432 	struct bi_record *rec;
433 	extern char __bss_start[];
434 
435 	rec = (struct bi_record *)_ALIGN((ulong)__bss_start+(1<<20)-1,(1<<20));
436 	if ( rec->tag != BI_FIRST ) {
437 		/*
438 		 * This 0x10000 offset is a terrible hack but it will go away when
439 		 * we have the bootloader handle all the relocation and
440 		 * prom calls -- Cort
441 		 */
442 		rec = (struct bi_record *)_ALIGN((ulong)__bss_start+0x10000+(1<<20)-1,(1<<20));
443 		if ( rec->tag != BI_FIRST )
444 			return NULL;
445 	}
446 	return rec;
447 }
448 
parse_bootinfo(struct bi_record * rec)449 void parse_bootinfo(struct bi_record *rec)
450 {
451 	if (rec == NULL || rec->tag != BI_FIRST)
452 		return;
453 	while (rec->tag != BI_LAST) {
454 		ulong *data = rec->data;
455 		switch (rec->tag) {
456 		case BI_CMD_LINE:
457 			memcpy(cmd_line, (void *)data, rec->size -
458 					sizeof(struct bi_record));
459 			break;
460 		case BI_SYSMAP:
461 			sysmap = (char *)((data[0] >= (KERNELBASE)) ? data[0] :
462 					  (data[0]+KERNELBASE));
463 			sysmap_size = data[1];
464 			break;
465 #ifdef CONFIG_BLK_DEV_INITRD
466 		case BI_INITRD:
467 			initrd_start = data[0] + KERNELBASE;
468 			initrd_end = data[0] + data[1] + KERNELBASE;
469 			break;
470 #endif /* CONFIG_BLK_DEV_INITRD */
471 #ifdef CONFIG_ALL_PPC
472 		case BI_MACHTYPE:
473 			_machine = data[0];
474 			break;
475 #endif /* CONFIG_ALL_PPC */
476 		case BI_MEMSIZE:
477 			boot_mem_size = data[0];
478 			break;
479 		case BI_BOARD_INFO:
480 			/* data is typically a bd_t */
481 			if (ppc_md.board_info)
482 				ppc_md.board_info((void *)data,
483 					rec->size - sizeof(struct bi_record));
484 			break;
485 		}
486 		rec = (struct bi_record *)((ulong)rec + rec->size);
487 	}
488 }
489 #endif /* CONFIG_APUS */
490 
491 /*
492  * Find out what kind of machine we're on and save any data we need
493  * from the early boot process (devtree is copied on pmac by prom_init()).
494  * This is called very early on the boot process, after a minimal
495  * MMU environment has been set up but before MMU_init is called.
496  */
497 void __init
machine_init(unsigned long r3,unsigned long r4,unsigned long r5,unsigned long r6,unsigned long r7)498 machine_init(unsigned long r3, unsigned long r4, unsigned long r5,
499 	     unsigned long r6, unsigned long r7)
500 {
501 #ifdef CONFIG_CMDLINE
502 	strcpy(cmd_line, CONFIG_CMDLINE);
503 #endif /* CONFIG_CMDLINE */
504 
505 	platform_init(r3, r4, r5, r6, r7);
506 
507 	if (ppc_md.progress)
508 		ppc_md.progress("id mach(): done", 0x200);
509 }
510 
511 /* Checks "l2cr=xxxx" command-line option */
ppc_setup_l2cr(char * str)512 int __init ppc_setup_l2cr(char *str)
513 {
514 	if (cur_cpu_spec[0]->cpu_features & CPU_FTR_L2CR) {
515 		unsigned long val = simple_strtoul(str, NULL, 0);
516 		printk(KERN_INFO "l2cr set to %lx\n", val);
517                 _set_L2CR(0);           /* force invalidate by disable cache */
518                 _set_L2CR(val);         /* and enable it */
519 	}
520 	return 1;
521 }
522 __setup("l2cr=", ppc_setup_l2cr);
523 
arch_discover_root(void)524 void __init arch_discover_root(void)
525 {
526 	if (ppc_md.discover_root != NULL)
527 		ppc_md.discover_root();
528 }
529 
ppc_init(void)530 void __init ppc_init(void)
531 {
532 	/* clear the progress line */
533 	if ( ppc_md.progress ) ppc_md.progress("             ", 0xffff);
534 
535 	if (ppc_md.init != NULL) {
536 		ppc_md.init();
537 	}
538 }
539 
540 /* Warning, IO base is not yet inited */
setup_arch(char ** cmdline_p)541 void __init setup_arch(char **cmdline_p)
542 {
543 	extern int panic_timeout;
544 	extern char _etext[], _edata[];
545 	extern char *klimit;
546 	extern void do_init_bootmem(void);
547 
548 	/* so udelay does something sensible, assume <= 1000 bogomips */
549 	loops_per_jiffy = 500000000 / HZ;
550 
551 #ifdef CONFIG_ALL_PPC
552 	/* This could be called "early setup arch", it must be done
553 	 * now because xmon need it
554 	 */
555 	if (_machine == _MACH_Pmac)
556 		pmac_feature_init();	/* New cool way */
557 #endif /* CONFIG_ALL_PPC */
558 
559 #ifdef CONFIG_XMON
560 	xmon_map_scc();
561 	if (strstr(cmd_line, "xmon"))
562 		xmon(0);
563 #endif /* CONFIG_XMON */
564 	if ( ppc_md.progress ) ppc_md.progress("setup_arch: enter", 0x3eab);
565 
566 #if defined(CONFIG_KGDB)
567 	kgdb_map_scc();
568 	set_debug_traps();
569 	if (strstr(cmd_line, "gdb")) {
570 		if (ppc_md.progress)
571 			ppc_md.progress("setup_arch: kgdb breakpoint", 0x4000);
572 		printk("kgdb breakpoint activated\n");
573 		breakpoint();
574 	}
575 #endif
576 
577 	/*
578 	 * Set cache line size based on type of cpu as a default.
579 	 * Systems with OF can look in the properties on the cpu node(s)
580 	 * for a possibly more accurate value.
581 	 */
582 	if (cur_cpu_spec[0]->cpu_features & CPU_FTR_SPLIT_ID_CACHE) {
583 		dcache_bsize = cur_cpu_spec[0]->dcache_bsize;
584 		icache_bsize = cur_cpu_spec[0]->icache_bsize;
585 		ucache_bsize = 0;
586 	} else
587 		ucache_bsize = dcache_bsize = icache_bsize
588 			= cur_cpu_spec[0]->dcache_bsize;
589 
590 	/* reboot on panic */
591 	panic_timeout = 180;
592 
593 	init_mm.start_code = PAGE_OFFSET;
594 	init_mm.end_code = (unsigned long) _etext;
595 	init_mm.end_data = (unsigned long) _edata;
596 	init_mm.brk = (unsigned long) klimit;
597 
598 	/* Save unparsed command line copy for /proc/cmdline */
599 	strcpy(saved_command_line, cmd_line);
600 	*cmdline_p = cmd_line;
601 
602 	/* set up the bootmem stuff with available memory */
603 	do_init_bootmem();
604 	if ( ppc_md.progress ) ppc_md.progress("setup_arch: bootmem", 0x3eab);
605 
606 #ifdef CONFIG_PPC_OCP
607 	/* Initialize OCP device list */
608 	ocp_early_init();
609 	if ( ppc_md.progress ) ppc_md.progress("setup_arch: ocp_early_init", 0x3eab);
610 #endif
611 
612 	ppc_md.setup_arch();
613 	if ( ppc_md.progress ) ppc_md.progress("arch: exit", 0x3eab);
614 
615 	paging_init();
616 	sort_exception_table();
617 
618 	/* this is for modules since _machine can be a define -- Cort */
619 	ppc_md.ppc_machine = _machine;
620 }
621