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