1 // SPDX-License-Identifier: GPL-2.0
3 * Miscellaneous Mac68K-specific stuff
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>
15 #include <linux/adb.h>
16 #include <linux/cuda.h>
17 #include <linux/pmu.h>
19 #include <linux/uaccess.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>
27 #include <asm/machdep.h>
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.
35 #define RTC_OFFSET 2082844800
37 static void (*rom_reset)(void);
39 #ifdef CONFIG_ADB_CUDA
40 static time64_t cuda_read_time(void)
42 struct adb_request req;
45 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
50 time = (u32)((req.reply[3] << 24) | (req.reply[4] << 16) |
51 (req.reply[5] << 8) | req.reply[6]);
53 return time - RTC_OFFSET;
56 static void cuda_write_time(time64_t time)
58 struct adb_request req;
59 u32 data = lower_32_bits(time + RTC_OFFSET);
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)
69 static __u8 cuda_read_pram(int offset)
71 struct adb_request req;
73 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
74 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
81 static void cuda_write_pram(int offset, __u8 data)
83 struct adb_request req;
85 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
86 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
91 #endif /* CONFIG_ADB_CUDA */
93 #ifdef CONFIG_ADB_PMU68K
94 static time64_t pmu_read_time(void)
96 struct adb_request req;
99 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
101 while (!req.complete)
104 time = (u32)((req.reply[1] << 24) | (req.reply[2] << 16) |
105 (req.reply[3] << 8) | req.reply[4]);
107 return time - RTC_OFFSET;
110 static void pmu_write_time(time64_t time)
112 struct adb_request req;
113 u32 data = lower_32_bits(time + RTC_OFFSET);
115 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
116 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
117 (data >> 8) & 0xFF, data & 0xFF) < 0)
119 while (!req.complete)
123 static __u8 pmu_read_pram(int offset)
125 struct adb_request req;
127 if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM,
128 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
130 while (!req.complete)
135 static void pmu_write_pram(int offset, __u8 data)
137 struct adb_request req;
139 if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM,
140 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
142 while (!req.complete)
145 #endif /* CONFIG_ADB_PMU68K */
148 * VIA PRAM/RTC access routines
150 * Must be called with interrupts disabled and
151 * the RTC should be enabled.
154 static __u8 via_pram_readbyte(void)
159 reg = via1[vBufB] & ~VIA1B_vRTCClk;
161 /* Set the RTC data line to be an input. */
163 via1[vDirB] &= ~VIA1B_vRTCData;
165 /* The bits of the byte come out in MSB order */
168 for (i = 0 ; i < 8 ; i++) {
170 via1[vBufB] = reg | VIA1B_vRTCClk;
171 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
174 /* Return RTC data line to output state */
176 via1[vDirB] |= VIA1B_vRTCData;
181 static void via_pram_writebyte(__u8 data)
185 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
187 /* The bits of the byte go in in MSB order */
189 for (i = 0 ; i < 8 ; i++) {
190 bit = data & 0x80? 1 : 0;
192 via1[vBufB] = reg | bit;
193 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
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.
203 * This function disables all interrupts while running.
206 static void via_pram_command(int command, __u8 *data)
211 local_irq_save(flags);
213 /* Enable the RTC and make sure the strobe line is high */
215 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
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;
226 *data = via_pram_readbyte();
228 via_pram_writebyte(*data);
231 /* All done, disable the RTC */
233 via1[vBufB] |= VIA1B_vRTCEnb;
235 local_irq_restore(flags);
238 static __u8 via_read_pram(int offset)
243 static void via_write_pram(int offset, __u8 data)
248 * Return the current time in seconds since January 1, 1904.
250 * This only works on machines with the VIA-based PRAM/RTC, which
251 * is basically any machine with Mac II-style ADB.
254 static time64_t via_read_time(void)
259 } result, last_result;
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]);
268 * The NetBSD guys say to loop until you get the same reading
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]);
278 if (result.idata == last_result.idata)
279 return (time64_t)result.idata - RTC_OFFSET;
284 last_result.idata = result.idata;
287 pr_err("%s: failed to read a stable value; got 0x%08x then 0x%08x\n",
288 __func__, last_result.idata, result.idata);
294 * Set the current time to a number of seconds since January 1, 1904.
296 * This only works on machines with the VIA-based PRAM/RTC, which
297 * is basically any machine with Mac II-style ADB.
300 static void via_write_time(time64_t time)
308 /* Clear the write protect bit */
311 via_pram_command(0x35, &temp);
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]);
319 /* Set the write protect bit */
322 via_pram_command(0x35, &temp);
325 static void via_shutdown(void)
328 via2[rBufB] &= ~0x04;
330 /* Direction of vDirB is output */
332 /* Send a value of 0 on that line */
333 via2[vBufB] &= ~0x04;
338 static void oss_shutdown(void)
340 oss->rom_ctrl = OSS_POWEROFF;
343 #ifdef CONFIG_ADB_CUDA
344 static void cuda_restart(void)
346 struct adb_request req;
348 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
350 while (!req.complete)
354 static void cuda_shutdown(void)
356 struct adb_request req;
358 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
361 /* Avoid infinite polling loop when PSU is not under Cuda control */
362 switch (macintosh_config->ident) {
365 case MAC_MODEL_Q605_ACC:
367 case MAC_MODEL_P475F:
371 while (!req.complete)
374 #endif /* CONFIG_ADB_CUDA */
376 #ifdef CONFIG_ADB_PMU68K
378 void pmu_restart(void)
380 struct adb_request req;
381 if (pmu_request(&req, NULL,
382 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
384 while (!req.complete)
386 if (pmu_request(&req, NULL, 1, PMU_RESET) < 0)
388 while (!req.complete)
392 void pmu_shutdown(void)
394 struct adb_request req;
395 if (pmu_request(&req, NULL,
396 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
398 while (!req.complete)
400 if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0)
402 while (!req.complete)
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 *-------------------------------------------------------------------
415 void mac_pram_read(int offset, __u8 *buffer, int len)
420 switch (macintosh_config->adb_type) {
424 func = via_read_pram;
426 #ifdef CONFIG_ADB_CUDA
429 func = cuda_read_pram;
432 #ifdef CONFIG_ADB_PMU68K
434 func = pmu_read_pram;
440 for (i = 0 ; i < len ; i++) {
441 buffer[i] = (*func)(offset++);
445 void mac_pram_write(int offset, __u8 *buffer, int len)
447 void (*func)(int, __u8);
450 switch (macintosh_config->adb_type) {
454 func = via_write_pram;
456 #ifdef CONFIG_ADB_CUDA
459 func = cuda_write_pram;
462 #ifdef CONFIG_ADB_PMU68K
464 func = pmu_write_pram;
470 for (i = 0 ; i < len ; i++) {
471 (*func)(offset++, buffer[i]);
475 void mac_poweroff(void)
479 } else if (macintosh_config->adb_type == MAC_ADB_II) {
481 #ifdef CONFIG_ADB_CUDA
482 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
483 macintosh_config->adb_type == MAC_ADB_CUDA) {
486 #ifdef CONFIG_ADB_PMU68K
487 } else if (macintosh_config->adb_type == MAC_ADB_PB1
488 || macintosh_config->adb_type == MAC_ADB_PB2) {
493 pr_crit("It is now safe to turn off your Macintosh.\n");
500 if (macintosh_config->adb_type == MAC_ADB_II) {
503 /* need ROMBASE in booter */
504 /* indeed, plus need to MAP THE ROM !! */
506 if (mac_bi_data.rombase == 0)
507 mac_bi_data.rombase = 0x40800000;
510 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
512 if (macintosh_config->ident == MAC_MODEL_SE30) {
514 * MSch: Machines known to crash on ROM reset ...
517 local_irq_save(flags);
521 local_irq_restore(flags);
523 #ifdef CONFIG_ADB_CUDA
524 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
525 macintosh_config->adb_type == MAC_ADB_CUDA) {
528 #ifdef CONFIG_ADB_PMU68K
529 } else if (macintosh_config->adb_type == MAC_ADB_PB1
530 || macintosh_config->adb_type == MAC_ADB_PB2) {
533 } else if (CPU_IS_030) {
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>
541 unsigned long rombase = 0x40000000;
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;
549 local_irq_disable(); /* lets not screw this up, ok? */
550 __asm__ __volatile__(".chip 68030\n\t"
554 /* Now jump to physical address so we can disable MMU */
555 __asm__ __volatile__(
557 "lea %/pc@(1f),%/a0\n\t"
558 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
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. */
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 */
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 */
581 : : "r" (offset), "a" (rombase) : "a0");
584 /* should never get here */
585 pr_crit("Restart failed. Please restart manually.\n");
591 * This function translates seconds since 1970 into a proper date.
593 * Algorithm cribbed from glibc2.1, __offtime().
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.
598 #define SECS_PER_MINUTE (60)
599 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
600 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
602 static void unmktime(time64_t time, long offset,
603 int *yearp, int *monp, int *dayp,
604 int *hourp, int *minp, int *secp)
606 /* How many days come before each month (0-12). */
607 static const unsigned short int __mon_yday[2][13] =
610 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
612 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
614 int days, rem, y, wday, yday;
615 const unsigned short int *ip;
617 days = div_u64_rem(time, SECS_PER_DAY, &rem);
623 while (rem >= SECS_PER_DAY) {
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;
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))
641 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
643 /* Guess a corrected year, assuming 365 days per year. */
644 long int yg = y + days / 365 - (days % 365 < 0);
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);
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)
658 *dayp = days + 1; /* day in the month */
663 * Read/write the hardware clock.
666 int mac_hwclk(int op, struct rtc_time *t)
670 if (!op) { /* read */
671 switch (macintosh_config->adb_type) {
675 now = via_read_time();
677 #ifdef CONFIG_ADB_CUDA
680 now = cuda_read_time();
683 #ifdef CONFIG_ADB_PMU68K
685 now = pmu_read_time();
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);
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);
704 now = mktime64(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
705 t->tm_hour, t->tm_min, t->tm_sec);
707 switch (macintosh_config->adb_type) {
713 #ifdef CONFIG_ADB_CUDA
716 cuda_write_time(now);
719 #ifdef CONFIG_ADB_PMU68K