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/string.h> 32 #include <linux/delay.h> 33 #include <linux/ioport.h> 34 #include <linux/major.h> 35 #include <linux/initrd.h> 36 #include <linux/console.h> 37 #include <linux/pci.h> 38 #include <linux/adb.h> 39 #include <linux/cuda.h> 40 #include <linux/pmu.h> 41 #include <linux/irq.h> 42 #include <linux/seq_file.h> 43 #include <linux/root_dev.h> 44 #include <linux/bitops.h> 45 #include <linux/suspend.h> 46 #include <linux/string_choices.h> 47 #include <linux/of.h> 48 #include <linux/of_platform.h> 49 50 #include <asm/reg.h> 51 #include <asm/sections.h> 52 #include <asm/io.h> 53 #include <asm/pci-bridge.h> 54 #include <asm/ohare.h> 55 #include <asm/mediabay.h> 56 #include <asm/machdep.h> 57 #include <asm/dma.h> 58 #include <asm/cputable.h> 59 #include <asm/btext.h> 60 #include <asm/pmac_feature.h> 61 #include <asm/time.h> 62 #include <asm/mmu_context.h> 63 #include <asm/iommu.h> 64 #include <asm/smu.h> 65 #include <asm/pmc.h> 66 #include <asm/udbg.h> 67 68 #include "pmac.h" 69 70 #undef SHOW_GATWICK_IRQS 71 72 static int has_l2cache; 73 74 int pmac_newworld; 75 76 static int current_root_goodness = -1; 77 78 /* sda1 - slightly silly choice */ 79 #define DEFAULT_ROOT_DEVICE MKDEV(SCSI_DISK0_MAJOR, 1) 80 81 sys_ctrler_t sys_ctrler = SYS_CTRLER_UNKNOWN; 82 EXPORT_SYMBOL(sys_ctrler); 83 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_property_read_bool(np, "cache-unified") && 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 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 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 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 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 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, str_enabled_disabled((*l2cr) & 0x80000000)); 241 } 242 of_node_put(np); 243 break; 244 } 245 } 246 } 247 #endif 248 249 static void __init pmac_setup_arch(void) 250 { 251 struct device_node *cpu, *ic; 252 const int *fp; 253 unsigned long pvr; 254 255 pvr = PVR_VER(mfspr(SPRN_PVR)); 256 257 /* Set loops_per_jiffy to a half-way reasonable value, 258 for use until calibrate_delay gets called. */ 259 loops_per_jiffy = 50000000 / HZ; 260 261 for_each_of_cpu_node(cpu) { 262 fp = of_get_property(cpu, "clock-frequency", NULL); 263 if (fp != NULL) { 264 if (pvr >= 0x30 && pvr < 0x80) 265 /* PPC970 etc. */ 266 loops_per_jiffy = *fp / (3 * HZ); 267 else if (pvr == 4 || pvr >= 8) 268 /* 604, G3, G4 etc. */ 269 loops_per_jiffy = *fp / HZ; 270 else 271 /* 603, etc. */ 272 loops_per_jiffy = *fp / (2 * HZ); 273 of_node_put(cpu); 274 break; 275 } 276 } 277 278 /* See if newworld or oldworld */ 279 ic = of_find_node_with_property(NULL, "interrupt-controller"); 280 if (ic) { 281 pmac_newworld = 1; 282 of_node_put(ic); 283 } 284 285 #ifdef CONFIG_PPC32 286 ohare_init(); 287 l2cr_init(); 288 #endif /* CONFIG_PPC32 */ 289 290 find_via_cuda(); 291 find_via_pmu(); 292 smu_init(); 293 294 #if IS_ENABLED(CONFIG_NVRAM) 295 pmac_nvram_init(); 296 #endif 297 #ifdef CONFIG_PPC32 298 #ifdef CONFIG_BLK_DEV_INITRD 299 if (initrd_start) 300 ROOT_DEV = Root_RAM0; 301 else 302 #endif 303 ROOT_DEV = DEFAULT_ROOT_DEVICE; 304 #endif 305 306 #ifdef CONFIG_ADB 307 if (strstr(boot_command_line, "adb_sync")) { 308 extern int __adb_probe_sync; 309 __adb_probe_sync = 1; 310 } 311 #endif /* CONFIG_ADB */ 312 } 313 314 static int initializing = 1; 315 316 static int pmac_late_init(void) 317 { 318 initializing = 0; 319 return 0; 320 } 321 machine_late_initcall(powermac, pmac_late_init); 322 323 void note_bootable_part(dev_t dev, int part, int goodness); 324 /* 325 * This is __ref because we check for "initializing" before 326 * touching any of the __init sensitive things and "initializing" 327 * will be false after __init time. This can't be __init because it 328 * can be called whenever a disk is first accessed. 329 */ 330 void __ref note_bootable_part(dev_t dev, int part, int goodness) 331 { 332 char *p; 333 334 if (!initializing) 335 return; 336 if ((goodness <= current_root_goodness) && 337 ROOT_DEV != DEFAULT_ROOT_DEVICE) 338 return; 339 p = strstr(boot_command_line, "root="); 340 if (p != NULL && (p == boot_command_line || p[-1] == ' ')) 341 return; 342 343 ROOT_DEV = dev + part; 344 current_root_goodness = goodness; 345 } 346 347 #ifdef CONFIG_ADB_CUDA 348 static void __noreturn cuda_restart(void) 349 { 350 struct adb_request req; 351 352 cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM); 353 for (;;) 354 cuda_poll(); 355 } 356 357 static void __noreturn cuda_shutdown(void) 358 { 359 struct adb_request req; 360 361 cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN); 362 for (;;) 363 cuda_poll(); 364 } 365 366 #else 367 #define cuda_restart() 368 #define cuda_shutdown() 369 #endif 370 371 #ifndef CONFIG_ADB_PMU 372 #define pmu_restart() 373 #define pmu_shutdown() 374 #endif 375 376 #ifndef CONFIG_PMAC_SMU 377 #define smu_restart() 378 #define smu_shutdown() 379 #endif 380 381 static void __noreturn pmac_restart(char *cmd) 382 { 383 switch (sys_ctrler) { 384 case SYS_CTRLER_CUDA: 385 cuda_restart(); 386 break; 387 case SYS_CTRLER_PMU: 388 pmu_restart(); 389 break; 390 case SYS_CTRLER_SMU: 391 smu_restart(); 392 break; 393 default: ; 394 } 395 while (1) ; 396 } 397 398 static void __noreturn pmac_power_off(void) 399 { 400 switch (sys_ctrler) { 401 case SYS_CTRLER_CUDA: 402 cuda_shutdown(); 403 break; 404 case SYS_CTRLER_PMU: 405 pmu_shutdown(); 406 break; 407 case SYS_CTRLER_SMU: 408 smu_shutdown(); 409 break; 410 default: ; 411 } 412 while (1) ; 413 } 414 415 static void __noreturn 416 pmac_halt(void) 417 { 418 pmac_power_off(); 419 } 420 421 /* 422 * Early initialization. 423 */ 424 static void __init pmac_init(void) 425 { 426 /* Enable early btext debug if requested */ 427 if (strstr(boot_command_line, "btextdbg")) { 428 udbg_adb_init_early(); 429 register_early_udbg_console(); 430 } 431 432 /* Probe motherboard chipset */ 433 pmac_feature_init(); 434 435 /* Initialize debug stuff */ 436 udbg_scc_init(!!strstr(boot_command_line, "sccdbg")); 437 udbg_adb_init(!!strstr(boot_command_line, "btextdbg")); 438 439 #ifdef CONFIG_PPC64 440 iommu_init_early_dart(&pmac_pci_controller_ops); 441 #endif 442 443 /* SMP Init has to be done early as we need to patch up 444 * cpu_possible_mask before interrupt stacks are allocated 445 * or kaboom... 446 */ 447 #ifdef CONFIG_SMP 448 pmac_setup_smp(); 449 #endif 450 } 451 452 static int __init pmac_declare_of_platform_devices(void) 453 { 454 struct device_node *np; 455 456 np = of_find_node_by_name(NULL, "valkyrie"); 457 if (np) { 458 of_platform_device_create(np, "valkyrie", NULL); 459 of_node_put(np); 460 } 461 np = of_find_node_by_name(NULL, "platinum"); 462 if (np) { 463 of_platform_device_create(np, "platinum", NULL); 464 of_node_put(np); 465 } 466 np = of_find_node_by_type(NULL, "smu"); 467 if (np) { 468 of_platform_device_create(np, "smu", NULL); 469 of_node_put(np); 470 } 471 np = of_find_node_by_type(NULL, "fcu"); 472 if (np == NULL) { 473 /* Some machines have strangely broken device-tree */ 474 np = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/fan@15e"); 475 } 476 if (np) { 477 of_platform_device_create(np, "temperature", NULL); 478 of_node_put(np); 479 } 480 481 return 0; 482 } 483 machine_device_initcall(powermac, pmac_declare_of_platform_devices); 484 485 #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE 486 /* 487 * This is called very early, as part of console_init() (typically just after 488 * time_init()). This function is respondible for trying to find a good 489 * default console on serial ports. It tries to match the open firmware 490 * default output with one of the available serial console drivers. 491 */ 492 static int __init check_pmac_serial_console(void) 493 { 494 struct device_node *prom_stdout = NULL; 495 int offset = 0; 496 const char *name; 497 #ifdef CONFIG_SERIAL_PMACZILOG_TTYS 498 char *devname = "ttyS"; 499 #else 500 char *devname = "ttyPZ"; 501 #endif 502 503 pr_debug(" -> check_pmac_serial_console()\n"); 504 505 /* The user has requested a console so this is already set up. */ 506 if (strstr(boot_command_line, "console=")) { 507 pr_debug(" console was specified !\n"); 508 return -EBUSY; 509 } 510 511 if (!of_chosen) { 512 pr_debug(" of_chosen is NULL !\n"); 513 return -ENODEV; 514 } 515 516 /* We are getting a weird phandle from OF ... */ 517 /* ... So use the full path instead */ 518 name = of_get_property(of_chosen, "linux,stdout-path", NULL); 519 if (name == NULL) { 520 pr_debug(" no linux,stdout-path !\n"); 521 return -ENODEV; 522 } 523 prom_stdout = of_find_node_by_path(name); 524 if (!prom_stdout) { 525 pr_debug(" can't find stdout package %s !\n", name); 526 return -ENODEV; 527 } 528 pr_debug("stdout is %pOF\n", prom_stdout); 529 530 if (of_node_name_eq(prom_stdout, "ch-a")) 531 offset = 0; 532 else if (of_node_name_eq(prom_stdout, "ch-b")) 533 offset = 1; 534 else 535 goto not_found; 536 of_node_put(prom_stdout); 537 538 pr_debug("Found serial console at %s%d\n", devname, offset); 539 540 return add_preferred_console(devname, offset, NULL); 541 542 not_found: 543 pr_debug("No preferred console found !\n"); 544 of_node_put(prom_stdout); 545 return -ENODEV; 546 } 547 console_initcall(check_pmac_serial_console); 548 549 #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */ 550 551 /* 552 * Called very early, MMU is off, device-tree isn't unflattened 553 */ 554 static int __init pmac_probe(void) 555 { 556 if (!of_machine_is_compatible("Power Macintosh") && 557 !of_machine_is_compatible("MacRISC")) 558 return 0; 559 560 #ifdef CONFIG_PPC32 561 /* isa_io_base gets set in pmac_pci_init */ 562 DMA_MODE_READ = 1; 563 DMA_MODE_WRITE = 2; 564 #endif /* CONFIG_PPC32 */ 565 566 pm_power_off = pmac_power_off; 567 568 pmac_init(); 569 570 return 1; 571 } 572 573 define_machine(powermac) { 574 .name = "PowerMac", 575 .probe = pmac_probe, 576 .setup_arch = pmac_setup_arch, 577 .discover_phbs = pmac_pci_init, 578 .show_cpuinfo = pmac_show_cpuinfo, 579 .init_IRQ = pmac_pic_init, 580 .get_irq = NULL, /* changed later */ 581 .pci_irq_fixup = pmac_pci_irq_fixup, 582 .restart = pmac_restart, 583 .halt = pmac_halt, 584 .time_init = pmac_time_init, 585 .get_boot_time = pmac_get_boot_time, 586 .set_rtc_time = pmac_set_rtc_time, 587 .get_rtc_time = pmac_get_rtc_time, 588 .calibrate_decr = pmac_calibrate_decr, 589 .feature_call = pmac_do_feature_call, 590 .progress = udbg_progress, 591 #ifdef CONFIG_PPC64 592 .power_save = power4_idle, 593 .enable_pmcs = power4_enable_pmcs, 594 #endif /* CONFIG_PPC64 */ 595 #ifdef CONFIG_PPC32 596 .pcibios_after_init = pmac_pcibios_after_init, 597 .phys_mem_access_prot = pci_phys_mem_access_prot, 598 #endif 599 }; 600