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