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