1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Powermac setup and early boot code plus other random bits.
4  *
5  *  PowerPC version
6  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7  *
8  *  Adapted for Power Macintosh by Paul Mackerras
9  *    Copyright (C) 1996 Paul Mackerras (paulus@samba.org)
10  *
11  *  Derived from "arch/alpha/kernel/setup.c"
12  *    Copyright (C) 1995 Linus Torvalds
13  *
14  *  Maintained by Benjamin Herrenschmidt (benh@kernel.crashing.org)
15  */
16 
17 /*
18  * bootup setup stuff..
19  */
20 
21 #include <linux/init.h>
22 #include <linux/errno.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include <linux/stddef.h>
27 #include <linux/unistd.h>
28 #include <linux/ptrace.h>
29 #include <linux/export.h>
30 #include <linux/user.h>
31 #include <linux/tty.h>
32 #include <linux/string.h>
33 #include <linux/delay.h>
34 #include <linux/ioport.h>
35 #include <linux/major.h>
36 #include <linux/initrd.h>
37 #include <linux/vt_kern.h>
38 #include <linux/console.h>
39 #include <linux/pci.h>
40 #include <linux/adb.h>
41 #include <linux/cuda.h>
42 #include <linux/pmu.h>
43 #include <linux/irq.h>
44 #include <linux/seq_file.h>
45 #include <linux/root_dev.h>
46 #include <linux/bitops.h>
47 #include <linux/suspend.h>
48 #include <linux/of_device.h>
49 #include <linux/of_platform.h>
50 
51 #include <asm/reg.h>
52 #include <asm/sections.h>
53 #include <asm/prom.h>
54 #include <asm/io.h>
55 #include <asm/pci-bridge.h>
56 #include <asm/ohare.h>
57 #include <asm/mediabay.h>
58 #include <asm/machdep.h>
59 #include <asm/dma.h>
60 #include <asm/cputable.h>
61 #include <asm/btext.h>
62 #include <asm/pmac_feature.h>
63 #include <asm/time.h>
64 #include <asm/mmu_context.h>
65 #include <asm/iommu.h>
66 #include <asm/smu.h>
67 #include <asm/pmc.h>
68 #include <asm/udbg.h>
69 
70 #include "pmac.h"
71 
72 #undef SHOW_GATWICK_IRQS
73 
74 int ppc_override_l2cr = 0;
75 int ppc_override_l2cr_value;
76 int has_l2cache = 0;
77 
78 int pmac_newworld;
79 
80 static int current_root_goodness = -1;
81 
82 #define DEFAULT_ROOT_DEVICE Root_SDA1	/* sda1 - slightly silly choice */
83 
84 #ifdef CONFIG_PPC64
85 int sccdbg;
86 #endif
87 
88 sys_ctrler_t sys_ctrler = SYS_CTRLER_UNKNOWN;
89 EXPORT_SYMBOL(sys_ctrler);
90 
91 static void pmac_show_cpuinfo(struct seq_file *m)
92 {
93 	struct device_node *np;
94 	const char *pp;
95 	int plen;
96 	int mbmodel;
97 	unsigned int mbflags;
98 	char* mbname;
99 
100 	mbmodel = pmac_call_feature(PMAC_FTR_GET_MB_INFO, NULL,
101 				    PMAC_MB_INFO_MODEL, 0);
102 	mbflags = pmac_call_feature(PMAC_FTR_GET_MB_INFO, NULL,
103 				    PMAC_MB_INFO_FLAGS, 0);
104 	if (pmac_call_feature(PMAC_FTR_GET_MB_INFO, NULL, PMAC_MB_INFO_NAME,
105 			      (long) &mbname) != 0)
106 		mbname = "Unknown";
107 
108 	/* find motherboard type */
109 	seq_printf(m, "machine\t\t: ");
110 	np = of_find_node_by_path("/");
111 	if (np != NULL) {
112 		pp = of_get_property(np, "model", NULL);
113 		if (pp != NULL)
114 			seq_printf(m, "%s\n", pp);
115 		else
116 			seq_printf(m, "PowerMac\n");
117 		pp = of_get_property(np, "compatible", &plen);
118 		if (pp != NULL) {
119 			seq_printf(m, "motherboard\t:");
120 			while (plen > 0) {
121 				int l = strlen(pp) + 1;
122 				seq_printf(m, " %s", pp);
123 				plen -= l;
124 				pp += l;
125 			}
126 			seq_printf(m, "\n");
127 		}
128 		of_node_put(np);
129 	} else
130 		seq_printf(m, "PowerMac\n");
131 
132 	/* print parsed model */
133 	seq_printf(m, "detected as\t: %d (%s)\n", mbmodel, mbname);
134 	seq_printf(m, "pmac flags\t: %08x\n", mbflags);
135 
136 	/* find l2 cache info */
137 	np = of_find_node_by_name(NULL, "l2-cache");
138 	if (np == NULL)
139 		np = of_find_node_by_type(NULL, "cache");
140 	if (np != NULL) {
141 		const unsigned int *ic =
142 			of_get_property(np, "i-cache-size", NULL);
143 		const unsigned int *dc =
144 			of_get_property(np, "d-cache-size", NULL);
145 		seq_printf(m, "L2 cache\t:");
146 		has_l2cache = 1;
147 		if (of_get_property(np, "cache-unified", NULL) && dc) {
148 			seq_printf(m, " %dK unified", *dc / 1024);
149 		} else {
150 			if (ic)
151 				seq_printf(m, " %dK instruction", *ic / 1024);
152 			if (dc)
153 				seq_printf(m, "%s %dK data",
154 					   (ic? " +": ""), *dc / 1024);
155 		}
156 		pp = of_get_property(np, "ram-type", NULL);
157 		if (pp)
158 			seq_printf(m, " %s", pp);
159 		seq_printf(m, "\n");
160 		of_node_put(np);
161 	}
162 
163 	/* Indicate newworld/oldworld */
164 	seq_printf(m, "pmac-generation\t: %s\n",
165 		   pmac_newworld ? "NewWorld" : "OldWorld");
166 }
167 
168 #ifndef CONFIG_ADB_CUDA
169 int __init find_via_cuda(void)
170 {
171 	struct device_node *dn = of_find_node_by_name(NULL, "via-cuda");
172 
173 	if (!dn)
174 		return 0;
175 	of_node_put(dn);
176 	printk("WARNING ! Your machine is CUDA-based but your kernel\n");
177 	printk("          wasn't compiled with CONFIG_ADB_CUDA option !\n");
178 	return 0;
179 }
180 #endif
181 
182 #ifndef CONFIG_ADB_PMU
183 int __init find_via_pmu(void)
184 {
185 	struct device_node *dn = of_find_node_by_name(NULL, "via-pmu");
186 
187 	if (!dn)
188 		return 0;
189 	of_node_put(dn);
190 	printk("WARNING ! Your machine is PMU-based but your kernel\n");
191 	printk("          wasn't compiled with CONFIG_ADB_PMU option !\n");
192 	return 0;
193 }
194 #endif
195 
196 #ifndef CONFIG_PMAC_SMU
197 int __init smu_init(void)
198 {
199 	/* should check and warn if SMU is present */
200 	return 0;
201 }
202 #endif
203 
204 #ifdef CONFIG_PPC32
205 static volatile u32 *sysctrl_regs;
206 
207 static void __init ohare_init(void)
208 {
209 	struct device_node *dn;
210 
211 	/* this area has the CPU identification register
212 	   and some registers used by smp boards */
213 	sysctrl_regs = (volatile u32 *) ioremap(0xf8000000, 0x1000);
214 
215 	/*
216 	 * Turn on the L2 cache.
217 	 * We assume that we have a PSX memory controller iff
218 	 * we have an ohare I/O controller.
219 	 */
220 	dn = of_find_node_by_name(NULL, "ohare");
221 	if (dn) {
222 		of_node_put(dn);
223 		if (((sysctrl_regs[2] >> 24) & 0xf) >= 3) {
224 			if (sysctrl_regs[4] & 0x10)
225 				sysctrl_regs[4] |= 0x04000020;
226 			else
227 				sysctrl_regs[4] |= 0x04000000;
228 			if(has_l2cache)
229 				printk(KERN_INFO "Level 2 cache enabled\n");
230 		}
231 	}
232 }
233 
234 static void __init l2cr_init(void)
235 {
236 	/* Checks "l2cr-value" property in the registry */
237 	if (cpu_has_feature(CPU_FTR_L2CR)) {
238 		struct device_node *np;
239 
240 		for_each_of_cpu_node(np) {
241 			const unsigned int *l2cr =
242 				of_get_property(np, "l2cr-value", NULL);
243 			if (l2cr) {
244 				ppc_override_l2cr = 1;
245 				ppc_override_l2cr_value = *l2cr;
246 				_set_L2CR(0);
247 				_set_L2CR(ppc_override_l2cr_value);
248 			}
249 			of_node_put(np);
250 			break;
251 		}
252 	}
253 
254 	if (ppc_override_l2cr)
255 		printk(KERN_INFO "L2CR overridden (0x%x), "
256 		       "backside cache is %s\n",
257 		       ppc_override_l2cr_value,
258 		       (ppc_override_l2cr_value & 0x80000000)
259 				? "enabled" : "disabled");
260 }
261 #endif
262 
263 static void __init pmac_setup_arch(void)
264 {
265 	struct device_node *cpu, *ic;
266 	const int *fp;
267 	unsigned long pvr;
268 
269 	pvr = PVR_VER(mfspr(SPRN_PVR));
270 
271 	/* Set loops_per_jiffy to a half-way reasonable value,
272 	   for use until calibrate_delay gets called. */
273 	loops_per_jiffy = 50000000 / HZ;
274 
275 	for_each_of_cpu_node(cpu) {
276 		fp = of_get_property(cpu, "clock-frequency", NULL);
277 		if (fp != NULL) {
278 			if (pvr >= 0x30 && pvr < 0x80)
279 				/* PPC970 etc. */
280 				loops_per_jiffy = *fp / (3 * HZ);
281 			else if (pvr == 4 || pvr >= 8)
282 				/* 604, G3, G4 etc. */
283 				loops_per_jiffy = *fp / HZ;
284 			else
285 				/* 603, etc. */
286 				loops_per_jiffy = *fp / (2 * HZ);
287 			of_node_put(cpu);
288 			break;
289 		}
290 	}
291 
292 	/* See if newworld or oldworld */
293 	ic = of_find_node_with_property(NULL, "interrupt-controller");
294 	if (ic) {
295 		pmac_newworld = 1;
296 		of_node_put(ic);
297 	}
298 
299 #ifdef CONFIG_PPC32
300 	ohare_init();
301 	l2cr_init();
302 #endif /* CONFIG_PPC32 */
303 
304 	find_via_cuda();
305 	find_via_pmu();
306 	smu_init();
307 
308 #if IS_ENABLED(CONFIG_NVRAM)
309 	pmac_nvram_init();
310 #endif
311 #ifdef CONFIG_PPC32
312 #ifdef CONFIG_BLK_DEV_INITRD
313 	if (initrd_start)
314 		ROOT_DEV = Root_RAM0;
315 	else
316 #endif
317 		ROOT_DEV = DEFAULT_ROOT_DEVICE;
318 #endif
319 
320 #ifdef CONFIG_ADB
321 	if (strstr(boot_command_line, "adb_sync")) {
322 		extern int __adb_probe_sync;
323 		__adb_probe_sync = 1;
324 	}
325 #endif /* CONFIG_ADB */
326 }
327 
328 #ifdef CONFIG_SCSI
329 void note_scsi_host(struct device_node *node, void *host)
330 {
331 }
332 EXPORT_SYMBOL(note_scsi_host);
333 #endif
334 
335 static int initializing = 1;
336 
337 static int pmac_late_init(void)
338 {
339 	initializing = 0;
340 	return 0;
341 }
342 machine_late_initcall(powermac, pmac_late_init);
343 
344 void note_bootable_part(dev_t dev, int part, int goodness);
345 /*
346  * This is __ref because we check for "initializing" before
347  * touching any of the __init sensitive things and "initializing"
348  * will be false after __init time. This can't be __init because it
349  * can be called whenever a disk is first accessed.
350  */
351 void __ref note_bootable_part(dev_t dev, int part, int goodness)
352 {
353 	char *p;
354 
355 	if (!initializing)
356 		return;
357 	if ((goodness <= current_root_goodness) &&
358 	    ROOT_DEV != DEFAULT_ROOT_DEVICE)
359 		return;
360 	p = strstr(boot_command_line, "root=");
361 	if (p != NULL && (p == boot_command_line || p[-1] == ' '))
362 		return;
363 
364 	ROOT_DEV = dev + part;
365 	current_root_goodness = goodness;
366 }
367 
368 #ifdef CONFIG_ADB_CUDA
369 static void __noreturn cuda_restart(void)
370 {
371 	struct adb_request req;
372 
373 	cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM);
374 	for (;;)
375 		cuda_poll();
376 }
377 
378 static void __noreturn cuda_shutdown(void)
379 {
380 	struct adb_request req;
381 
382 	cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN);
383 	for (;;)
384 		cuda_poll();
385 }
386 
387 #else
388 #define cuda_restart()
389 #define cuda_shutdown()
390 #endif
391 
392 #ifndef CONFIG_ADB_PMU
393 #define pmu_restart()
394 #define pmu_shutdown()
395 #endif
396 
397 #ifndef CONFIG_PMAC_SMU
398 #define smu_restart()
399 #define smu_shutdown()
400 #endif
401 
402 static void __noreturn pmac_restart(char *cmd)
403 {
404 	switch (sys_ctrler) {
405 	case SYS_CTRLER_CUDA:
406 		cuda_restart();
407 		break;
408 	case SYS_CTRLER_PMU:
409 		pmu_restart();
410 		break;
411 	case SYS_CTRLER_SMU:
412 		smu_restart();
413 		break;
414 	default: ;
415 	}
416 	while (1) ;
417 }
418 
419 static void __noreturn pmac_power_off(void)
420 {
421 	switch (sys_ctrler) {
422 	case SYS_CTRLER_CUDA:
423 		cuda_shutdown();
424 		break;
425 	case SYS_CTRLER_PMU:
426 		pmu_shutdown();
427 		break;
428 	case SYS_CTRLER_SMU:
429 		smu_shutdown();
430 		break;
431 	default: ;
432 	}
433 	while (1) ;
434 }
435 
436 static void __noreturn
437 pmac_halt(void)
438 {
439 	pmac_power_off();
440 }
441 
442 /*
443  * Early initialization.
444  */
445 static void __init pmac_init(void)
446 {
447 	/* Enable early btext debug if requested */
448 	if (strstr(boot_command_line, "btextdbg")) {
449 		udbg_adb_init_early();
450 		register_early_udbg_console();
451 	}
452 
453 	/* Probe motherboard chipset */
454 	pmac_feature_init();
455 
456 	/* Initialize debug stuff */
457 	udbg_scc_init(!!strstr(boot_command_line, "sccdbg"));
458 	udbg_adb_init(!!strstr(boot_command_line, "btextdbg"));
459 
460 #ifdef CONFIG_PPC64
461 	iommu_init_early_dart(&pmac_pci_controller_ops);
462 #endif
463 
464 	/* SMP Init has to be done early as we need to patch up
465 	 * cpu_possible_mask before interrupt stacks are allocated
466 	 * or kaboom...
467 	 */
468 #ifdef CONFIG_SMP
469 	pmac_setup_smp();
470 #endif
471 }
472 
473 static int __init pmac_declare_of_platform_devices(void)
474 {
475 	struct device_node *np;
476 
477 	np = of_find_node_by_name(NULL, "valkyrie");
478 	if (np) {
479 		of_platform_device_create(np, "valkyrie", NULL);
480 		of_node_put(np);
481 	}
482 	np = of_find_node_by_name(NULL, "platinum");
483 	if (np) {
484 		of_platform_device_create(np, "platinum", NULL);
485 		of_node_put(np);
486 	}
487         np = of_find_node_by_type(NULL, "smu");
488         if (np) {
489 		of_platform_device_create(np, "smu", NULL);
490 		of_node_put(np);
491 	}
492 	np = of_find_node_by_type(NULL, "fcu");
493 	if (np == NULL) {
494 		/* Some machines have strangely broken device-tree */
495 		np = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/fan@15e");
496 	}
497 	if (np) {
498 		of_platform_device_create(np, "temperature", NULL);
499 		of_node_put(np);
500 	}
501 
502 	return 0;
503 }
504 machine_device_initcall(powermac, pmac_declare_of_platform_devices);
505 
506 #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
507 /*
508  * This is called very early, as part of console_init() (typically just after
509  * time_init()). This function is respondible for trying to find a good
510  * default console on serial ports. It tries to match the open firmware
511  * default output with one of the available serial console drivers.
512  */
513 static int __init check_pmac_serial_console(void)
514 {
515 	struct device_node *prom_stdout = NULL;
516 	int offset = 0;
517 	const char *name;
518 #ifdef CONFIG_SERIAL_PMACZILOG_TTYS
519 	char *devname = "ttyS";
520 #else
521 	char *devname = "ttyPZ";
522 #endif
523 
524 	pr_debug(" -> check_pmac_serial_console()\n");
525 
526 	/* The user has requested a console so this is already set up. */
527 	if (strstr(boot_command_line, "console=")) {
528 		pr_debug(" console was specified !\n");
529 		return -EBUSY;
530 	}
531 
532 	if (!of_chosen) {
533 		pr_debug(" of_chosen is NULL !\n");
534 		return -ENODEV;
535 	}
536 
537 	/* We are getting a weird phandle from OF ... */
538 	/* ... So use the full path instead */
539 	name = of_get_property(of_chosen, "linux,stdout-path", NULL);
540 	if (name == NULL) {
541 		pr_debug(" no linux,stdout-path !\n");
542 		return -ENODEV;
543 	}
544 	prom_stdout = of_find_node_by_path(name);
545 	if (!prom_stdout) {
546 		pr_debug(" can't find stdout package %s !\n", name);
547 		return -ENODEV;
548 	}
549 	pr_debug("stdout is %pOF\n", prom_stdout);
550 
551 	if (of_node_name_eq(prom_stdout, "ch-a"))
552 		offset = 0;
553 	else if (of_node_name_eq(prom_stdout, "ch-b"))
554 		offset = 1;
555 	else
556 		goto not_found;
557 	of_node_put(prom_stdout);
558 
559 	pr_debug("Found serial console at %s%d\n", devname, offset);
560 
561 	return add_preferred_console(devname, offset, NULL);
562 
563  not_found:
564 	pr_debug("No preferred console found !\n");
565 	of_node_put(prom_stdout);
566 	return -ENODEV;
567 }
568 console_initcall(check_pmac_serial_console);
569 
570 #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
571 
572 /*
573  * Called very early, MMU is off, device-tree isn't unflattened
574  */
575 static int __init pmac_probe(void)
576 {
577 	if (!of_machine_is_compatible("Power Macintosh") &&
578 	    !of_machine_is_compatible("MacRISC"))
579 		return 0;
580 
581 #ifdef CONFIG_PPC32
582 	/* isa_io_base gets set in pmac_pci_init */
583 	DMA_MODE_READ = 1;
584 	DMA_MODE_WRITE = 2;
585 #endif /* CONFIG_PPC32 */
586 
587 	pm_power_off = pmac_power_off;
588 
589 	pmac_init();
590 
591 	return 1;
592 }
593 
594 define_machine(powermac) {
595 	.name			= "PowerMac",
596 	.probe			= pmac_probe,
597 	.setup_arch		= pmac_setup_arch,
598 	.discover_phbs		= pmac_pci_init,
599 	.show_cpuinfo		= pmac_show_cpuinfo,
600 	.init_IRQ		= pmac_pic_init,
601 	.get_irq		= NULL,	/* changed later */
602 	.pci_irq_fixup		= pmac_pci_irq_fixup,
603 	.restart		= pmac_restart,
604 	.halt			= pmac_halt,
605 	.time_init		= pmac_time_init,
606 	.get_boot_time		= pmac_get_boot_time,
607 	.set_rtc_time		= pmac_set_rtc_time,
608 	.get_rtc_time		= pmac_get_rtc_time,
609 	.calibrate_decr		= pmac_calibrate_decr,
610 	.feature_call		= pmac_do_feature_call,
611 	.progress		= udbg_progress,
612 #ifdef CONFIG_PPC64
613 	.power_save		= power4_idle,
614 	.enable_pmcs		= power4_enable_pmcs,
615 #endif /* CONFIG_PPC64 */
616 #ifdef CONFIG_PPC32
617 	.pcibios_after_init	= pmac_pcibios_after_init,
618 	.phys_mem_access_prot	= pci_phys_mem_access_prot,
619 #endif
620 };
621