1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Miscellaneous Mac68K-specific stuff 4 */ 5 6 #include <linux/types.h> 7 #include <linux/errno.h> 8 #include <linux/kernel.h> 9 #include <linux/delay.h> 10 #include <linux/sched.h> 11 #include <linux/time.h> 12 #include <linux/rtc.h> 13 #include <linux/mm.h> 14 15 #include <linux/adb.h> 16 #include <linux/cuda.h> 17 #include <linux/pmu.h> 18 19 #include <linux/uaccess.h> 20 #include <asm/io.h> 21 #include <asm/segment.h> 22 #include <asm/setup.h> 23 #include <asm/macintosh.h> 24 #include <asm/mac_via.h> 25 #include <asm/mac_oss.h> 26 27 #include <asm/machdep.h> 28 29 /* 30 * Offset between Unix time (1970-based) and Mac time (1904-based). Cuda and PMU 31 * times wrap in 2040. If we need to handle later times, the read_time functions 32 * need to be changed to interpret wrapped times as post-2040. 33 */ 34 35 #define RTC_OFFSET 2082844800 36 37 static void (*rom_reset)(void); 38 39 #ifdef CONFIG_ADB_CUDA 40 static time64_t cuda_read_time(void) 41 { 42 struct adb_request req; 43 time64_t time; 44 45 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0) 46 return 0; 47 while (!req.complete) 48 cuda_poll(); 49 50 time = (u32)((req.reply[3] << 24) | (req.reply[4] << 16) | 51 (req.reply[5] << 8) | req.reply[6]); 52 53 return time - RTC_OFFSET; 54 } 55 56 static void cuda_write_time(time64_t time) 57 { 58 struct adb_request req; 59 u32 data = lower_32_bits(time + RTC_OFFSET); 60 61 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME, 62 (data >> 24) & 0xFF, (data >> 16) & 0xFF, 63 (data >> 8) & 0xFF, data & 0xFF) < 0) 64 return; 65 while (!req.complete) 66 cuda_poll(); 67 } 68 69 static __u8 cuda_read_pram(int offset) 70 { 71 struct adb_request req; 72 73 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM, 74 (offset >> 8) & 0xFF, offset & 0xFF) < 0) 75 return 0; 76 while (!req.complete) 77 cuda_poll(); 78 return req.reply[3]; 79 } 80 81 static void cuda_write_pram(int offset, __u8 data) 82 { 83 struct adb_request req; 84 85 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM, 86 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0) 87 return; 88 while (!req.complete) 89 cuda_poll(); 90 } 91 #endif /* CONFIG_ADB_CUDA */ 92 93 #ifdef CONFIG_ADB_PMU68K 94 static time64_t pmu_read_time(void) 95 { 96 struct adb_request req; 97 time64_t time; 98 99 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0) 100 return 0; 101 while (!req.complete) 102 pmu_poll(); 103 104 time = (u32)((req.reply[1] << 24) | (req.reply[2] << 16) | 105 (req.reply[3] << 8) | req.reply[4]); 106 107 return time - RTC_OFFSET; 108 } 109 110 static void pmu_write_time(time64_t time) 111 { 112 struct adb_request req; 113 u32 data = lower_32_bits(time + RTC_OFFSET); 114 115 if (pmu_request(&req, NULL, 5, PMU_SET_RTC, 116 (data >> 24) & 0xFF, (data >> 16) & 0xFF, 117 (data >> 8) & 0xFF, data & 0xFF) < 0) 118 return; 119 while (!req.complete) 120 pmu_poll(); 121 } 122 123 static __u8 pmu_read_pram(int offset) 124 { 125 struct adb_request req; 126 127 if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM, 128 (offset >> 8) & 0xFF, offset & 0xFF) < 0) 129 return 0; 130 while (!req.complete) 131 pmu_poll(); 132 return req.reply[3]; 133 } 134 135 static void pmu_write_pram(int offset, __u8 data) 136 { 137 struct adb_request req; 138 139 if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM, 140 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0) 141 return; 142 while (!req.complete) 143 pmu_poll(); 144 } 145 #endif /* CONFIG_ADB_PMU68K */ 146 147 /* 148 * VIA PRAM/RTC access routines 149 * 150 * Must be called with interrupts disabled and 151 * the RTC should be enabled. 152 */ 153 154 static __u8 via_pram_readbyte(void) 155 { 156 int i, reg; 157 __u8 data; 158 159 reg = via1[vBufB] & ~VIA1B_vRTCClk; 160 161 /* Set the RTC data line to be an input. */ 162 163 via1[vDirB] &= ~VIA1B_vRTCData; 164 165 /* The bits of the byte come out in MSB order */ 166 167 data = 0; 168 for (i = 0 ; i < 8 ; i++) { 169 via1[vBufB] = reg; 170 via1[vBufB] = reg | VIA1B_vRTCClk; 171 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData); 172 } 173 174 /* Return RTC data line to output state */ 175 176 via1[vDirB] |= VIA1B_vRTCData; 177 178 return data; 179 } 180 181 static void via_pram_writebyte(__u8 data) 182 { 183 int i, reg, bit; 184 185 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData); 186 187 /* The bits of the byte go in in MSB order */ 188 189 for (i = 0 ; i < 8 ; i++) { 190 bit = data & 0x80? 1 : 0; 191 data <<= 1; 192 via1[vBufB] = reg | bit; 193 via1[vBufB] = reg | bit | VIA1B_vRTCClk; 194 } 195 } 196 197 /* 198 * Execute a VIA PRAM/RTC command. For read commands 199 * data should point to a one-byte buffer for the 200 * resulting data. For write commands it should point 201 * to the data byte to for the command. 202 * 203 * This function disables all interrupts while running. 204 */ 205 206 static void via_pram_command(int command, __u8 *data) 207 { 208 unsigned long flags; 209 int is_read; 210 211 local_irq_save(flags); 212 213 /* Enable the RTC and make sure the strobe line is high */ 214 215 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb; 216 217 if (command & 0xFF00) { /* extended (two-byte) command */ 218 via_pram_writebyte((command & 0xFF00) >> 8); 219 via_pram_writebyte(command & 0xFF); 220 is_read = command & 0x8000; 221 } else { /* one-byte command */ 222 via_pram_writebyte(command); 223 is_read = command & 0x80; 224 } 225 if (is_read) { 226 *data = via_pram_readbyte(); 227 } else { 228 via_pram_writebyte(*data); 229 } 230 231 /* All done, disable the RTC */ 232 233 via1[vBufB] |= VIA1B_vRTCEnb; 234 235 local_irq_restore(flags); 236 } 237 238 static __u8 via_read_pram(int offset) 239 { 240 return 0; 241 } 242 243 static void via_write_pram(int offset, __u8 data) 244 { 245 } 246 247 /* 248 * Return the current time in seconds since January 1, 1904. 249 * 250 * This only works on machines with the VIA-based PRAM/RTC, which 251 * is basically any machine with Mac II-style ADB. 252 */ 253 254 static time64_t via_read_time(void) 255 { 256 union { 257 __u8 cdata[4]; 258 __u32 idata; 259 } result, last_result; 260 int count = 1; 261 262 via_pram_command(0x81, &last_result.cdata[3]); 263 via_pram_command(0x85, &last_result.cdata[2]); 264 via_pram_command(0x89, &last_result.cdata[1]); 265 via_pram_command(0x8D, &last_result.cdata[0]); 266 267 /* 268 * The NetBSD guys say to loop until you get the same reading 269 * twice in a row. 270 */ 271 272 while (1) { 273 via_pram_command(0x81, &result.cdata[3]); 274 via_pram_command(0x85, &result.cdata[2]); 275 via_pram_command(0x89, &result.cdata[1]); 276 via_pram_command(0x8D, &result.cdata[0]); 277 278 if (result.idata == last_result.idata) 279 return (time64_t)result.idata - RTC_OFFSET; 280 281 if (++count > 10) 282 break; 283 284 last_result.idata = result.idata; 285 } 286 287 pr_err("%s: failed to read a stable value; got 0x%08x then 0x%08x\n", 288 __func__, last_result.idata, result.idata); 289 290 return 0; 291 } 292 293 /* 294 * Set the current time to a number of seconds since January 1, 1904. 295 * 296 * This only works on machines with the VIA-based PRAM/RTC, which 297 * is basically any machine with Mac II-style ADB. 298 */ 299 300 static void via_write_time(time64_t time) 301 { 302 union { 303 __u8 cdata[4]; 304 __u32 idata; 305 } data; 306 __u8 temp; 307 308 /* Clear the write protect bit */ 309 310 temp = 0x55; 311 via_pram_command(0x35, &temp); 312 313 data.idata = lower_32_bits(time + RTC_OFFSET); 314 via_pram_command(0x01, &data.cdata[3]); 315 via_pram_command(0x05, &data.cdata[2]); 316 via_pram_command(0x09, &data.cdata[1]); 317 via_pram_command(0x0D, &data.cdata[0]); 318 319 /* Set the write protect bit */ 320 321 temp = 0xD5; 322 via_pram_command(0x35, &temp); 323 } 324 325 static void via_shutdown(void) 326 { 327 if (rbv_present) { 328 via2[rBufB] &= ~0x04; 329 } else { 330 /* Direction of vDirB is output */ 331 via2[vDirB] |= 0x04; 332 /* Send a value of 0 on that line */ 333 via2[vBufB] &= ~0x04; 334 mdelay(1000); 335 } 336 } 337 338 static void oss_shutdown(void) 339 { 340 oss->rom_ctrl = OSS_POWEROFF; 341 } 342 343 #ifdef CONFIG_ADB_CUDA 344 static void cuda_restart(void) 345 { 346 struct adb_request req; 347 348 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0) 349 return; 350 while (!req.complete) 351 cuda_poll(); 352 } 353 354 static void cuda_shutdown(void) 355 { 356 struct adb_request req; 357 358 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0) 359 return; 360 361 /* Avoid infinite polling loop when PSU is not under Cuda control */ 362 switch (macintosh_config->ident) { 363 case MAC_MODEL_C660: 364 case MAC_MODEL_Q605: 365 case MAC_MODEL_Q605_ACC: 366 case MAC_MODEL_P475: 367 case MAC_MODEL_P475F: 368 return; 369 } 370 371 while (!req.complete) 372 cuda_poll(); 373 } 374 #endif /* CONFIG_ADB_CUDA */ 375 376 #ifdef CONFIG_ADB_PMU68K 377 378 void pmu_restart(void) 379 { 380 struct adb_request req; 381 if (pmu_request(&req, NULL, 382 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0) 383 return; 384 while (!req.complete) 385 pmu_poll(); 386 if (pmu_request(&req, NULL, 1, PMU_RESET) < 0) 387 return; 388 while (!req.complete) 389 pmu_poll(); 390 } 391 392 void pmu_shutdown(void) 393 { 394 struct adb_request req; 395 if (pmu_request(&req, NULL, 396 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0) 397 return; 398 while (!req.complete) 399 pmu_poll(); 400 if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0) 401 return; 402 while (!req.complete) 403 pmu_poll(); 404 } 405 406 #endif 407 408 /* 409 *------------------------------------------------------------------- 410 * Below this point are the generic routines; they'll dispatch to the 411 * correct routine for the hardware on which we're running. 412 *------------------------------------------------------------------- 413 */ 414 415 void mac_pram_read(int offset, __u8 *buffer, int len) 416 { 417 __u8 (*func)(int); 418 int i; 419 420 switch (macintosh_config->adb_type) { 421 case MAC_ADB_IOP: 422 case MAC_ADB_II: 423 case MAC_ADB_PB1: 424 func = via_read_pram; 425 break; 426 #ifdef CONFIG_ADB_CUDA 427 case MAC_ADB_EGRET: 428 case MAC_ADB_CUDA: 429 func = cuda_read_pram; 430 break; 431 #endif 432 #ifdef CONFIG_ADB_PMU68K 433 case MAC_ADB_PB2: 434 func = pmu_read_pram; 435 break; 436 #endif 437 default: 438 return; 439 } 440 for (i = 0 ; i < len ; i++) { 441 buffer[i] = (*func)(offset++); 442 } 443 } 444 445 void mac_pram_write(int offset, __u8 *buffer, int len) 446 { 447 void (*func)(int, __u8); 448 int i; 449 450 switch (macintosh_config->adb_type) { 451 case MAC_ADB_IOP: 452 case MAC_ADB_II: 453 case MAC_ADB_PB1: 454 func = via_write_pram; 455 break; 456 #ifdef CONFIG_ADB_CUDA 457 case MAC_ADB_EGRET: 458 case MAC_ADB_CUDA: 459 func = cuda_write_pram; 460 break; 461 #endif 462 #ifdef CONFIG_ADB_PMU68K 463 case MAC_ADB_PB2: 464 func = pmu_write_pram; 465 break; 466 #endif 467 default: 468 return; 469 } 470 for (i = 0 ; i < len ; i++) { 471 (*func)(offset++, buffer[i]); 472 } 473 } 474 475 void mac_poweroff(void) 476 { 477 if (oss_present) { 478 oss_shutdown(); 479 } else if (macintosh_config->adb_type == MAC_ADB_II) { 480 via_shutdown(); 481 #ifdef CONFIG_ADB_CUDA 482 } else if (macintosh_config->adb_type == MAC_ADB_EGRET || 483 macintosh_config->adb_type == MAC_ADB_CUDA) { 484 cuda_shutdown(); 485 #endif 486 #ifdef CONFIG_ADB_PMU68K 487 } else if (macintosh_config->adb_type == MAC_ADB_PB1 488 || macintosh_config->adb_type == MAC_ADB_PB2) { 489 pmu_shutdown(); 490 #endif 491 } 492 493 pr_crit("It is now safe to turn off your Macintosh.\n"); 494 local_irq_disable(); 495 while(1); 496 } 497 498 void mac_reset(void) 499 { 500 if (macintosh_config->adb_type == MAC_ADB_II) { 501 unsigned long flags; 502 503 /* need ROMBASE in booter */ 504 /* indeed, plus need to MAP THE ROM !! */ 505 506 if (mac_bi_data.rombase == 0) 507 mac_bi_data.rombase = 0x40800000; 508 509 /* works on some */ 510 rom_reset = (void *) (mac_bi_data.rombase + 0xa); 511 512 if (macintosh_config->ident == MAC_MODEL_SE30) { 513 /* 514 * MSch: Machines known to crash on ROM reset ... 515 */ 516 } else { 517 local_irq_save(flags); 518 519 rom_reset(); 520 521 local_irq_restore(flags); 522 } 523 #ifdef CONFIG_ADB_CUDA 524 } else if (macintosh_config->adb_type == MAC_ADB_EGRET || 525 macintosh_config->adb_type == MAC_ADB_CUDA) { 526 cuda_restart(); 527 #endif 528 #ifdef CONFIG_ADB_PMU68K 529 } else if (macintosh_config->adb_type == MAC_ADB_PB1 530 || macintosh_config->adb_type == MAC_ADB_PB2) { 531 pmu_restart(); 532 #endif 533 } else if (CPU_IS_030) { 534 535 /* 030-specific reset routine. The idea is general, but the 536 * specific registers to reset are '030-specific. Until I 537 * have a non-030 machine, I can't test anything else. 538 * -- C. Scott Ananian <cananian@alumni.princeton.edu> 539 */ 540 541 unsigned long rombase = 0x40000000; 542 543 /* make a 1-to-1 mapping, using the transparent tran. reg. */ 544 unsigned long virt = (unsigned long) mac_reset; 545 unsigned long phys = virt_to_phys(mac_reset); 546 unsigned long addr = (phys&0xFF000000)|0x8777; 547 unsigned long offset = phys-virt; 548 549 local_irq_disable(); /* lets not screw this up, ok? */ 550 __asm__ __volatile__(".chip 68030\n\t" 551 "pmove %0,%/tt0\n\t" 552 ".chip 68k" 553 : : "m" (addr)); 554 /* Now jump to physical address so we can disable MMU */ 555 __asm__ __volatile__( 556 ".chip 68030\n\t" 557 "lea %/pc@(1f),%/a0\n\t" 558 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */ 559 "addl %0,%/sp\n\t" 560 "pflusha\n\t" 561 "jmp %/a0@\n\t" /* jump into physical memory */ 562 "0:.long 0\n\t" /* a constant zero. */ 563 /* OK. Now reset everything and jump to reset vector. */ 564 "1:\n\t" 565 "lea %/pc@(0b),%/a0\n\t" 566 "pmove %/a0@, %/tc\n\t" /* disable mmu */ 567 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */ 568 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */ 569 "movel #0, %/a0\n\t" 570 "movec %/a0, %/vbr\n\t" /* clear vector base register */ 571 "movec %/a0, %/cacr\n\t" /* disable caches */ 572 "movel #0x0808,%/a0\n\t" 573 "movec %/a0, %/cacr\n\t" /* flush i&d caches */ 574 "movew #0x2700,%/sr\n\t" /* set up status register */ 575 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */ 576 "movec %/a0, %/isp\n\t" 577 "movel %1@(0x4),%/a0\n\t" /* load reset vector */ 578 "reset\n\t" /* reset external devices */ 579 "jmp %/a0@\n\t" /* jump to the reset vector */ 580 ".chip 68k" 581 : : "r" (offset), "a" (rombase) : "a0"); 582 } 583 584 /* should never get here */ 585 pr_crit("Restart failed. Please restart manually.\n"); 586 local_irq_disable(); 587 while(1); 588 } 589 590 /* 591 * This function translates seconds since 1970 into a proper date. 592 * 593 * Algorithm cribbed from glibc2.1, __offtime(). 594 * 595 * This is roughly same as rtc_time64_to_tm(), which we should probably 596 * use here, but it's only available when CONFIG_RTC_LIB is enabled. 597 */ 598 #define SECS_PER_MINUTE (60) 599 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60) 600 #define SECS_PER_DAY (SECS_PER_HOUR * 24) 601 602 static void unmktime(time64_t time, long offset, 603 int *yearp, int *monp, int *dayp, 604 int *hourp, int *minp, int *secp) 605 { 606 /* How many days come before each month (0-12). */ 607 static const unsigned short int __mon_yday[2][13] = 608 { 609 /* Normal years. */ 610 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, 611 /* Leap years. */ 612 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } 613 }; 614 int days, rem, y, wday, yday; 615 const unsigned short int *ip; 616 617 days = div_u64_rem(time, SECS_PER_DAY, &rem); 618 rem += offset; 619 while (rem < 0) { 620 rem += SECS_PER_DAY; 621 --days; 622 } 623 while (rem >= SECS_PER_DAY) { 624 rem -= SECS_PER_DAY; 625 ++days; 626 } 627 *hourp = rem / SECS_PER_HOUR; 628 rem %= SECS_PER_HOUR; 629 *minp = rem / SECS_PER_MINUTE; 630 *secp = rem % SECS_PER_MINUTE; 631 /* January 1, 1970 was a Thursday. */ 632 wday = (4 + days) % 7; /* Day in the week. Not currently used */ 633 if (wday < 0) wday += 7; 634 y = 1970; 635 636 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0)) 637 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400)) 638 #define __isleap(year) \ 639 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) 640 641 while (days < 0 || days >= (__isleap (y) ? 366 : 365)) 642 { 643 /* Guess a corrected year, assuming 365 days per year. */ 644 long int yg = y + days / 365 - (days % 365 < 0); 645 646 /* Adjust DAYS and Y to match the guessed year. */ 647 days -= (yg - y) * 365 + 648 LEAPS_THRU_END_OF(yg - 1) - LEAPS_THRU_END_OF(y - 1); 649 y = yg; 650 } 651 *yearp = y - 1900; 652 yday = days; /* day in the year. Not currently used. */ 653 ip = __mon_yday[__isleap(y)]; 654 for (y = 11; days < (long int) ip[y]; --y) 655 continue; 656 days -= ip[y]; 657 *monp = y; 658 *dayp = days + 1; /* day in the month */ 659 return; 660 } 661 662 /* 663 * Read/write the hardware clock. 664 */ 665 666 int mac_hwclk(int op, struct rtc_time *t) 667 { 668 time64_t now; 669 670 if (!op) { /* read */ 671 switch (macintosh_config->adb_type) { 672 case MAC_ADB_IOP: 673 case MAC_ADB_II: 674 case MAC_ADB_PB1: 675 now = via_read_time(); 676 break; 677 #ifdef CONFIG_ADB_CUDA 678 case MAC_ADB_EGRET: 679 case MAC_ADB_CUDA: 680 now = cuda_read_time(); 681 break; 682 #endif 683 #ifdef CONFIG_ADB_PMU68K 684 case MAC_ADB_PB2: 685 now = pmu_read_time(); 686 break; 687 #endif 688 default: 689 now = 0; 690 } 691 692 t->tm_wday = 0; 693 unmktime(now, 0, 694 &t->tm_year, &t->tm_mon, &t->tm_mday, 695 &t->tm_hour, &t->tm_min, &t->tm_sec); 696 pr_debug("%s: read %04d-%02d-%-2d %02d:%02d:%02d\n", 697 __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 698 t->tm_hour, t->tm_min, t->tm_sec); 699 } else { /* write */ 700 pr_debug("%s: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n", 701 __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 702 t->tm_hour, t->tm_min, t->tm_sec); 703 704 now = mktime64(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 705 t->tm_hour, t->tm_min, t->tm_sec); 706 707 switch (macintosh_config->adb_type) { 708 case MAC_ADB_IOP: 709 case MAC_ADB_II: 710 case MAC_ADB_PB1: 711 via_write_time(now); 712 break; 713 #ifdef CONFIG_ADB_CUDA 714 case MAC_ADB_EGRET: 715 case MAC_ADB_CUDA: 716 cuda_write_time(now); 717 break; 718 #endif 719 #ifdef CONFIG_ADB_PMU68K 720 case MAC_ADB_PB2: 721 pmu_write_time(now); 722 break; 723 #endif 724 default: 725 return -ENODEV; 726 } 727 } 728 return 0; 729 } 730