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