xref: /openbmc/linux/arch/m68k/kernel/setup_mm.c (revision ef4290e6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/arch/m68k/kernel/setup.c
4  *
5  *  Copyright (C) 1995  Hamish Macdonald
6  */
7 
8 /*
9  * This file handles the architecture-dependent parts of system setup
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/sched.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/fs.h>
18 #include <linux/console.h>
19 #include <linux/errno.h>
20 #include <linux/string.h>
21 #include <linux/init.h>
22 #include <linux/memblock.h>
23 #include <linux/proc_fs.h>
24 #include <linux/seq_file.h>
25 #include <linux/module.h>
26 #include <linux/nvram.h>
27 #include <linux/initrd.h>
28 #include <linux/random.h>
29 
30 #include <asm/bootinfo.h>
31 #include <asm/byteorder.h>
32 #include <asm/sections.h>
33 #include <asm/setup.h>
34 #include <asm/fpu.h>
35 #include <asm/irq.h>
36 #include <asm/io.h>
37 #include <asm/machdep.h>
38 #ifdef CONFIG_AMIGA
39 #include <asm/amigahw.h>
40 #endif
41 #include <asm/atarihw.h>
42 #ifdef CONFIG_ATARI
43 #include <asm/atari_stram.h>
44 #endif
45 #ifdef CONFIG_SUN3X
46 #include <asm/dvma.h>
47 #endif
48 #include <asm/macintosh.h>
49 #include <asm/natfeat.h>
50 #include <asm/config.h>
51 
52 #if !FPSTATESIZE || !NR_IRQS
53 #warning No CPU/platform type selected, your kernel will not work!
54 #warning Are you building an allnoconfig kernel?
55 #endif
56 
57 unsigned long m68k_machtype;
58 EXPORT_SYMBOL(m68k_machtype);
59 unsigned long m68k_cputype;
60 EXPORT_SYMBOL(m68k_cputype);
61 unsigned long m68k_fputype;
62 unsigned long m68k_mmutype;
63 EXPORT_SYMBOL(m68k_mmutype);
64 #ifdef CONFIG_VME
65 unsigned long vme_brdtype;
66 EXPORT_SYMBOL(vme_brdtype);
67 #endif
68 
69 int m68k_is040or060;
70 EXPORT_SYMBOL(m68k_is040or060);
71 
72 extern unsigned long availmem;
73 
74 int m68k_num_memory;
75 EXPORT_SYMBOL(m68k_num_memory);
76 int m68k_realnum_memory;
77 EXPORT_SYMBOL(m68k_realnum_memory);
78 unsigned long m68k_memoffset;
79 struct m68k_mem_info m68k_memory[NUM_MEMINFO];
80 EXPORT_SYMBOL(m68k_memory);
81 
82 static struct m68k_mem_info m68k_ramdisk __initdata;
83 
84 static char m68k_command_line[CL_SIZE] __initdata;
85 
86 void (*mach_sched_init) (void) __initdata = NULL;
87 /* machine dependent irq functions */
88 void (*mach_init_IRQ) (void) __initdata = NULL;
89 void (*mach_get_model) (char *model);
90 void (*mach_get_hardware_list) (struct seq_file *m);
91 void (*mach_reset)( void );
92 void (*mach_halt)( void );
93 #ifdef CONFIG_HEARTBEAT
94 void (*mach_heartbeat) (int);
95 EXPORT_SYMBOL(mach_heartbeat);
96 #endif
97 #ifdef CONFIG_M68K_L2_CACHE
98 void (*mach_l2_flush) (int);
99 #endif
100 #if defined(CONFIG_ISA) && defined(MULTI_ISA)
101 int isa_type;
102 int isa_sex;
103 EXPORT_SYMBOL(isa_type);
104 EXPORT_SYMBOL(isa_sex);
105 #endif
106 
107 #define MASK_256K 0xfffc0000
108 
109 extern void paging_init(void);
110 
111 static void __init m68k_parse_bootinfo(const struct bi_record *record)
112 {
113 	const struct bi_record *first_record = record;
114 	uint16_t tag;
115 
116 	while ((tag = be16_to_cpu(record->tag)) != BI_LAST) {
117 		int unknown = 0;
118 		const void *data = record->data;
119 		uint16_t size = be16_to_cpu(record->size);
120 
121 		switch (tag) {
122 		case BI_MACHTYPE:
123 		case BI_CPUTYPE:
124 		case BI_FPUTYPE:
125 		case BI_MMUTYPE:
126 			/* Already set up by head.S */
127 			break;
128 
129 		case BI_MEMCHUNK:
130 			if (m68k_num_memory < NUM_MEMINFO) {
131 				const struct mem_info *m = data;
132 				m68k_memory[m68k_num_memory].addr =
133 					be32_to_cpu(m->addr);
134 				m68k_memory[m68k_num_memory].size =
135 					be32_to_cpu(m->size);
136 				m68k_num_memory++;
137 			} else
138 				pr_warn("%s: too many memory chunks\n",
139 					__func__);
140 			break;
141 
142 		case BI_RAMDISK:
143 			{
144 				const struct mem_info *m = data;
145 				m68k_ramdisk.addr = be32_to_cpu(m->addr);
146 				m68k_ramdisk.size = be32_to_cpu(m->size);
147 			}
148 			break;
149 
150 		case BI_COMMAND_LINE:
151 			strscpy(m68k_command_line, data,
152 				sizeof(m68k_command_line));
153 			break;
154 
155 		case BI_RNG_SEED: {
156 			u16 len = be16_to_cpup(data);
157 			add_bootloader_randomness(data + 2, len);
158 			/*
159 			 * Zero the data to preserve forward secrecy, and zero the
160 			 * length to prevent kexec from using it.
161 			 */
162 			memzero_explicit((void *)data, len + 2);
163 			break;
164 		}
165 
166 		default:
167 			if (MACH_IS_AMIGA)
168 				unknown = amiga_parse_bootinfo(record);
169 			else if (MACH_IS_ATARI)
170 				unknown = atari_parse_bootinfo(record);
171 			else if (MACH_IS_MAC)
172 				unknown = mac_parse_bootinfo(record);
173 			else if (MACH_IS_Q40)
174 				unknown = q40_parse_bootinfo(record);
175 			else if (MACH_IS_BVME6000)
176 				unknown = bvme6000_parse_bootinfo(record);
177 			else if (MACH_IS_MVME16x)
178 				unknown = mvme16x_parse_bootinfo(record);
179 			else if (MACH_IS_MVME147)
180 				unknown = mvme147_parse_bootinfo(record);
181 			else if (MACH_IS_HP300)
182 				unknown = hp300_parse_bootinfo(record);
183 			else if (MACH_IS_APOLLO)
184 				unknown = apollo_parse_bootinfo(record);
185 			else if (MACH_IS_VIRT)
186 				unknown = virt_parse_bootinfo(record);
187 			else
188 				unknown = 1;
189 		}
190 		if (unknown)
191 			pr_warn("%s: unknown tag 0x%04x ignored\n", __func__,
192 				tag);
193 		record = (struct bi_record *)((unsigned long)record + size);
194 	}
195 
196 	save_bootinfo(first_record);
197 
198 	m68k_realnum_memory = m68k_num_memory;
199 #ifdef CONFIG_SINGLE_MEMORY_CHUNK
200 	if (m68k_num_memory > 1) {
201 		pr_warn("%s: ignoring last %i chunks of physical memory\n",
202 			__func__, (m68k_num_memory - 1));
203 		m68k_num_memory = 1;
204 	}
205 #endif
206 }
207 
208 void __init setup_arch(char **cmdline_p)
209 {
210 	/* The bootinfo is located right after the kernel */
211 	if (!CPU_IS_COLDFIRE)
212 		m68k_parse_bootinfo((const struct bi_record *)_end);
213 
214 	if (CPU_IS_040)
215 		m68k_is040or060 = 4;
216 	else if (CPU_IS_060)
217 		m68k_is040or060 = 6;
218 
219 	/* FIXME: m68k_fputype is passed in by Penguin booter, which can
220 	 * be confused by software FPU emulation. BEWARE.
221 	 * We should really do our own FPU check at startup.
222 	 * [what do we do with buggy 68LC040s? if we have problems
223 	 *  with them, we should add a test to check_bugs() below] */
224 #if defined(CONFIG_FPU) && !defined(CONFIG_M68KFPU_EMU_ONLY)
225 	/* clear the fpu if we have one */
226 	if (m68k_fputype & (FPU_68881|FPU_68882|FPU_68040|FPU_68060|FPU_COLDFIRE)) {
227 		volatile int zero = 0;
228 		asm volatile ("frestore %0" : : "m" (zero));
229 	}
230 #endif
231 
232 	if (CPU_IS_060) {
233 		u32 pcr;
234 
235 		asm (".chip 68060; movec %%pcr,%0; .chip 68k"
236 		     : "=d" (pcr));
237 		if (((pcr >> 8) & 0xff) <= 5) {
238 			pr_warn("Enabling workaround for errata I14\n");
239 			asm (".chip 68060; movec %0,%%pcr; .chip 68k"
240 			     : : "d" (pcr | 0x20));
241 		}
242 	}
243 
244 	setup_initial_init_mm((void *)PAGE_OFFSET, _etext, _edata, _end);
245 
246 #if defined(CONFIG_BOOTPARAM)
247 	strncpy(m68k_command_line, CONFIG_BOOTPARAM_STRING, CL_SIZE);
248 	m68k_command_line[CL_SIZE - 1] = 0;
249 #endif /* CONFIG_BOOTPARAM */
250 	process_uboot_commandline(&m68k_command_line[0], CL_SIZE);
251 	*cmdline_p = m68k_command_line;
252 	memcpy(boot_command_line, *cmdline_p, CL_SIZE);
253 
254 	parse_early_param();
255 
256 	switch (m68k_machtype) {
257 #ifdef CONFIG_AMIGA
258 	case MACH_AMIGA:
259 		config_amiga();
260 		break;
261 #endif
262 #ifdef CONFIG_ATARI
263 	case MACH_ATARI:
264 		config_atari();
265 		break;
266 #endif
267 #ifdef CONFIG_MAC
268 	case MACH_MAC:
269 		config_mac();
270 		break;
271 #endif
272 #ifdef CONFIG_SUN3
273 	case MACH_SUN3:
274 		config_sun3();
275 		break;
276 #endif
277 #ifdef CONFIG_APOLLO
278 	case MACH_APOLLO:
279 		config_apollo();
280 		break;
281 #endif
282 #ifdef CONFIG_MVME147
283 	case MACH_MVME147:
284 		config_mvme147();
285 		break;
286 #endif
287 #ifdef CONFIG_MVME16x
288 	case MACH_MVME16x:
289 		config_mvme16x();
290 		break;
291 #endif
292 #ifdef CONFIG_BVME6000
293 	case MACH_BVME6000:
294 		config_bvme6000();
295 		break;
296 #endif
297 #ifdef CONFIG_HP300
298 	case MACH_HP300:
299 		config_hp300();
300 		break;
301 #endif
302 #ifdef CONFIG_Q40
303 	case MACH_Q40:
304 		config_q40();
305 		break;
306 #endif
307 #ifdef CONFIG_SUN3X
308 	case MACH_SUN3X:
309 		config_sun3x();
310 		break;
311 #endif
312 #ifdef CONFIG_COLDFIRE
313 	case MACH_M54XX:
314 	case MACH_M5441X:
315 		cf_bootmem_alloc();
316 		cf_mmu_context_init();
317 		config_BSP(NULL, 0);
318 		break;
319 #endif
320 #ifdef CONFIG_VIRT
321 	case MACH_VIRT:
322 		config_virt();
323 		break;
324 #endif
325 	default:
326 		panic("No configuration setup");
327 	}
328 
329 #ifdef CONFIG_BLK_DEV_INITRD
330 	if (m68k_ramdisk.size) {
331 		memblock_reserve(m68k_ramdisk.addr, m68k_ramdisk.size);
332 		initrd_start = (unsigned long)phys_to_virt(m68k_ramdisk.addr);
333 		initrd_end = initrd_start + m68k_ramdisk.size;
334 		pr_info("initrd: %08lx - %08lx\n", initrd_start, initrd_end);
335 	}
336 #endif
337 
338 	paging_init();
339 
340 #ifdef CONFIG_NATFEAT
341 	nf_init();
342 #endif
343 
344 #ifdef CONFIG_ATARI
345 	if (MACH_IS_ATARI)
346 		atari_stram_reserve_pages((void *)availmem);
347 #endif
348 #ifdef CONFIG_SUN3X
349 	if (MACH_IS_SUN3X) {
350 		dvma_init();
351 	}
352 #endif
353 
354 /* set ISA defs early as possible */
355 #if defined(CONFIG_ISA) && defined(MULTI_ISA)
356 	if (MACH_IS_Q40) {
357 		isa_type = ISA_TYPE_Q40;
358 		isa_sex = 0;
359 	}
360 #ifdef CONFIG_AMIGA_PCMCIA
361 	if (MACH_IS_AMIGA && AMIGAHW_PRESENT(PCMCIA)) {
362 		isa_type = ISA_TYPE_AG;
363 		isa_sex = 1;
364 	}
365 #endif
366 #ifdef CONFIG_ATARI_ROM_ISA
367 	if (MACH_IS_ATARI) {
368 		isa_type = ISA_TYPE_ENEC;
369 		isa_sex = 0;
370 	}
371 #endif
372 #endif
373 }
374 
375 static int show_cpuinfo(struct seq_file *m, void *v)
376 {
377 	const char *cpu, *mmu, *fpu;
378 	unsigned long clockfreq, clockfactor;
379 
380 #define LOOP_CYCLES_68020	(8)
381 #define LOOP_CYCLES_68030	(8)
382 #define LOOP_CYCLES_68040	(3)
383 #define LOOP_CYCLES_68060	(1)
384 #define LOOP_CYCLES_COLDFIRE	(2)
385 
386 	if (CPU_IS_020) {
387 		cpu = "68020";
388 		clockfactor = LOOP_CYCLES_68020;
389 	} else if (CPU_IS_030) {
390 		cpu = "68030";
391 		clockfactor = LOOP_CYCLES_68030;
392 	} else if (CPU_IS_040) {
393 		cpu = "68040";
394 		clockfactor = LOOP_CYCLES_68040;
395 	} else if (CPU_IS_060) {
396 		cpu = "68060";
397 		clockfactor = LOOP_CYCLES_68060;
398 	} else if (CPU_IS_COLDFIRE) {
399 		cpu = "ColdFire";
400 		clockfactor = LOOP_CYCLES_COLDFIRE;
401 	} else {
402 		cpu = "680x0";
403 		clockfactor = 0;
404 	}
405 
406 #ifdef CONFIG_M68KFPU_EMU_ONLY
407 	fpu = "none(soft float)";
408 #else
409 	if (m68k_fputype & FPU_68881)
410 		fpu = "68881";
411 	else if (m68k_fputype & FPU_68882)
412 		fpu = "68882";
413 	else if (m68k_fputype & FPU_68040)
414 		fpu = "68040";
415 	else if (m68k_fputype & FPU_68060)
416 		fpu = "68060";
417 	else if (m68k_fputype & FPU_SUNFPA)
418 		fpu = "Sun FPA";
419 	else if (m68k_fputype & FPU_COLDFIRE)
420 		fpu = "ColdFire";
421 	else
422 		fpu = "none";
423 #endif
424 
425 	if (m68k_mmutype & MMU_68851)
426 		mmu = "68851";
427 	else if (m68k_mmutype & MMU_68030)
428 		mmu = "68030";
429 	else if (m68k_mmutype & MMU_68040)
430 		mmu = "68040";
431 	else if (m68k_mmutype & MMU_68060)
432 		mmu = "68060";
433 	else if (m68k_mmutype & MMU_SUN3)
434 		mmu = "Sun-3";
435 	else if (m68k_mmutype & MMU_APOLLO)
436 		mmu = "Apollo";
437 	else if (m68k_mmutype & MMU_COLDFIRE)
438 		mmu = "ColdFire";
439 	else
440 		mmu = "unknown";
441 
442 	clockfreq = loops_per_jiffy * HZ * clockfactor;
443 
444 	seq_printf(m, "CPU:\t\t%s\n"
445 		   "MMU:\t\t%s\n"
446 		   "FPU:\t\t%s\n"
447 		   "Clocking:\t%lu.%1luMHz\n"
448 		   "BogoMips:\t%lu.%02lu\n"
449 		   "Calibration:\t%lu loops\n",
450 		   cpu, mmu, fpu,
451 		   clockfreq/1000000,(clockfreq/100000)%10,
452 		   loops_per_jiffy/(500000/HZ),(loops_per_jiffy/(5000/HZ))%100,
453 		   loops_per_jiffy);
454 	return 0;
455 }
456 
457 static void *c_start(struct seq_file *m, loff_t *pos)
458 {
459 	return *pos < 1 ? (void *)1 : NULL;
460 }
461 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
462 {
463 	++*pos;
464 	return NULL;
465 }
466 static void c_stop(struct seq_file *m, void *v)
467 {
468 }
469 const struct seq_operations cpuinfo_op = {
470 	.start	= c_start,
471 	.next	= c_next,
472 	.stop	= c_stop,
473 	.show	= show_cpuinfo,
474 };
475 
476 #ifdef CONFIG_PROC_HARDWARE
477 static int hardware_proc_show(struct seq_file *m, void *v)
478 {
479 	char model[80];
480 	unsigned long mem;
481 	int i;
482 
483 	if (mach_get_model)
484 		mach_get_model(model);
485 	else
486 		strcpy(model, "Unknown m68k");
487 
488 	seq_printf(m, "Model:\t\t%s\n", model);
489 	for (mem = 0, i = 0; i < m68k_num_memory; i++)
490 		mem += m68k_memory[i].size;
491 	seq_printf(m, "System Memory:\t%ldK\n", mem >> 10);
492 
493 	if (mach_get_hardware_list)
494 		mach_get_hardware_list(m);
495 
496 	return 0;
497 }
498 
499 static int __init proc_hardware_init(void)
500 {
501 	proc_create_single("hardware", 0, NULL, hardware_proc_show);
502 	return 0;
503 }
504 module_init(proc_hardware_init);
505 #endif
506 
507 void check_bugs(void)
508 {
509 #if defined(CONFIG_FPU) && !defined(CONFIG_M68KFPU_EMU)
510 	if (m68k_fputype == 0) {
511 		pr_emerg("*** YOU DO NOT HAVE A FLOATING POINT UNIT, "
512 			"WHICH IS REQUIRED BY LINUX/M68K ***\n");
513 		pr_emerg("Upgrade your hardware or join the FPU "
514 			"emulation project\n");
515 		panic("no FPU");
516 	}
517 #endif /* !CONFIG_M68KFPU_EMU */
518 }
519 
520 #ifdef CONFIG_ADB
521 static int __init adb_probe_sync_enable (char *str) {
522 	extern int __adb_probe_sync;
523 	__adb_probe_sync = 1;
524 	return 1;
525 }
526 
527 __setup("adb_sync", adb_probe_sync_enable);
528 #endif /* CONFIG_ADB */
529 
530 #if IS_ENABLED(CONFIG_NVRAM)
531 #ifdef CONFIG_MAC
532 static unsigned char m68k_nvram_read_byte(int addr)
533 {
534 	if (MACH_IS_MAC)
535 		return mac_pram_read_byte(addr);
536 	return 0xff;
537 }
538 
539 static void m68k_nvram_write_byte(unsigned char val, int addr)
540 {
541 	if (MACH_IS_MAC)
542 		mac_pram_write_byte(val, addr);
543 }
544 #endif /* CONFIG_MAC */
545 
546 #ifdef CONFIG_ATARI
547 static ssize_t m68k_nvram_read(char *buf, size_t count, loff_t *ppos)
548 {
549 	if (MACH_IS_ATARI)
550 		return atari_nvram_read(buf, count, ppos);
551 	else if (MACH_IS_MAC)
552 		return nvram_read_bytes(buf, count, ppos);
553 	return -EINVAL;
554 }
555 
556 static ssize_t m68k_nvram_write(char *buf, size_t count, loff_t *ppos)
557 {
558 	if (MACH_IS_ATARI)
559 		return atari_nvram_write(buf, count, ppos);
560 	else if (MACH_IS_MAC)
561 		return nvram_write_bytes(buf, count, ppos);
562 	return -EINVAL;
563 }
564 
565 static long m68k_nvram_set_checksum(void)
566 {
567 	if (MACH_IS_ATARI)
568 		return atari_nvram_set_checksum();
569 	return -EINVAL;
570 }
571 
572 static long m68k_nvram_initialize(void)
573 {
574 	if (MACH_IS_ATARI)
575 		return atari_nvram_initialize();
576 	return -EINVAL;
577 }
578 #endif /* CONFIG_ATARI */
579 
580 static ssize_t m68k_nvram_get_size(void)
581 {
582 	if (MACH_IS_ATARI)
583 		return atari_nvram_get_size();
584 	else if (MACH_IS_MAC)
585 		return mac_pram_get_size();
586 	return -ENODEV;
587 }
588 
589 /* Atari device drivers call .read (to get checksum validation) whereas
590  * Mac and PowerMac device drivers just use .read_byte.
591  */
592 const struct nvram_ops arch_nvram_ops = {
593 #ifdef CONFIG_MAC
594 	.read_byte      = m68k_nvram_read_byte,
595 	.write_byte     = m68k_nvram_write_byte,
596 #endif
597 #ifdef CONFIG_ATARI
598 	.read           = m68k_nvram_read,
599 	.write          = m68k_nvram_write,
600 	.set_checksum   = m68k_nvram_set_checksum,
601 	.initialize     = m68k_nvram_initialize,
602 #endif
603 	.get_size       = m68k_nvram_get_size,
604 };
605 EXPORT_SYMBOL(arch_nvram_ops);
606 #endif /* CONFIG_NVRAM */
607