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 */
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 pmu_wait_complete(&req
);
103 time
= (u32
)((req
.reply
[0] << 24) | (req
.reply
[1] << 16) |
104 (req
.reply
[2] << 8) | req
.reply
[3]);
106 return time
- RTC_OFFSET
;
109 static void pmu_write_time(time64_t time
)
111 struct adb_request req
;
112 u32 data
= lower_32_bits(time
+ RTC_OFFSET
);
114 if (pmu_request(&req
, NULL
, 5, PMU_SET_RTC
,
115 (data
>> 24) & 0xFF, (data
>> 16) & 0xFF,
116 (data
>> 8) & 0xFF, data
& 0xFF) < 0)
118 pmu_wait_complete(&req
);
121 static __u8
pmu_read_pram(int offset
)
123 struct adb_request req
;
125 if (pmu_request(&req
, NULL
, 3, PMU_READ_NVRAM
,
126 (offset
>> 8) & 0xFF, offset
& 0xFF) < 0)
128 while (!req
.complete
)
133 static void pmu_write_pram(int offset
, __u8 data
)
135 struct adb_request req
;
137 if (pmu_request(&req
, NULL
, 4, PMU_WRITE_NVRAM
,
138 (offset
>> 8) & 0xFF, offset
& 0xFF, data
) < 0)
140 while (!req
.complete
)
143 #endif /* CONFIG_ADB_PMU */
146 * VIA PRAM/RTC access routines
148 * Must be called with interrupts disabled and
149 * the RTC should be enabled.
152 static __u8
via_pram_readbyte(void)
157 reg
= via1
[vBufB
] & ~VIA1B_vRTCClk
;
159 /* Set the RTC data line to be an input. */
161 via1
[vDirB
] &= ~VIA1B_vRTCData
;
163 /* The bits of the byte come out in MSB order */
166 for (i
= 0 ; i
< 8 ; i
++) {
168 via1
[vBufB
] = reg
| VIA1B_vRTCClk
;
169 data
= (data
<< 1) | (via1
[vBufB
] & VIA1B_vRTCData
);
172 /* Return RTC data line to output state */
174 via1
[vDirB
] |= VIA1B_vRTCData
;
179 static void via_pram_writebyte(__u8 data
)
183 reg
= via1
[vBufB
] & ~(VIA1B_vRTCClk
| VIA1B_vRTCData
);
185 /* The bits of the byte go in in MSB order */
187 for (i
= 0 ; i
< 8 ; i
++) {
188 bit
= data
& 0x80? 1 : 0;
190 via1
[vBufB
] = reg
| bit
;
191 via1
[vBufB
] = reg
| bit
| VIA1B_vRTCClk
;
196 * Execute a VIA PRAM/RTC command. For read commands
197 * data should point to a one-byte buffer for the
198 * resulting data. For write commands it should point
199 * to the data byte to for the command.
201 * This function disables all interrupts while running.
204 static void via_pram_command(int command
, __u8
*data
)
209 local_irq_save(flags
);
211 /* Enable the RTC and make sure the strobe line is high */
213 via1
[vBufB
] = (via1
[vBufB
] | VIA1B_vRTCClk
) & ~VIA1B_vRTCEnb
;
215 if (command
& 0xFF00) { /* extended (two-byte) command */
216 via_pram_writebyte((command
& 0xFF00) >> 8);
217 via_pram_writebyte(command
& 0xFF);
218 is_read
= command
& 0x8000;
219 } else { /* one-byte command */
220 via_pram_writebyte(command
);
221 is_read
= command
& 0x80;
224 *data
= via_pram_readbyte();
226 via_pram_writebyte(*data
);
229 /* All done, disable the RTC */
231 via1
[vBufB
] |= VIA1B_vRTCEnb
;
233 local_irq_restore(flags
);
236 static __u8
via_read_pram(int offset
)
241 static void via_write_pram(int offset
, __u8 data
)
246 * Return the current time in seconds since January 1, 1904.
248 * This only works on machines with the VIA-based PRAM/RTC, which
249 * is basically any machine with Mac II-style ADB.
252 static time64_t
via_read_time(void)
257 } result
, last_result
;
260 via_pram_command(0x81, &last_result
.cdata
[3]);
261 via_pram_command(0x85, &last_result
.cdata
[2]);
262 via_pram_command(0x89, &last_result
.cdata
[1]);
263 via_pram_command(0x8D, &last_result
.cdata
[0]);
266 * The NetBSD guys say to loop until you get the same reading
271 via_pram_command(0x81, &result
.cdata
[3]);
272 via_pram_command(0x85, &result
.cdata
[2]);
273 via_pram_command(0x89, &result
.cdata
[1]);
274 via_pram_command(0x8D, &result
.cdata
[0]);
276 if (result
.idata
== last_result
.idata
)
277 return (time64_t
)result
.idata
- RTC_OFFSET
;
282 last_result
.idata
= result
.idata
;
285 pr_err("%s: failed to read a stable value; got 0x%08x then 0x%08x\n",
286 __func__
, last_result
.idata
, result
.idata
);
292 * Set the current time to a number of seconds since January 1, 1904.
294 * This only works on machines with the VIA-based PRAM/RTC, which
295 * is basically any machine with Mac II-style ADB.
298 static void via_write_time(time64_t time
)
306 /* Clear the write protect bit */
309 via_pram_command(0x35, &temp
);
311 data
.idata
= lower_32_bits(time
+ RTC_OFFSET
);
312 via_pram_command(0x01, &data
.cdata
[3]);
313 via_pram_command(0x05, &data
.cdata
[2]);
314 via_pram_command(0x09, &data
.cdata
[1]);
315 via_pram_command(0x0D, &data
.cdata
[0]);
317 /* Set the write protect bit */
320 via_pram_command(0x35, &temp
);
323 static void via_shutdown(void)
326 via2
[rBufB
] &= ~0x04;
328 /* Direction of vDirB is output */
330 /* Send a value of 0 on that line */
331 via2
[vBufB
] &= ~0x04;
336 static void oss_shutdown(void)
338 oss
->rom_ctrl
= OSS_POWEROFF
;
341 #ifdef CONFIG_ADB_CUDA
342 static void cuda_restart(void)
344 struct adb_request req
;
346 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_RESET_SYSTEM
) < 0)
348 while (!req
.complete
)
352 static void cuda_shutdown(void)
354 struct adb_request req
;
356 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_POWERDOWN
) < 0)
359 /* Avoid infinite polling loop when PSU is not under Cuda control */
360 switch (macintosh_config
->ident
) {
363 case MAC_MODEL_Q605_ACC
:
365 case MAC_MODEL_P475F
:
369 while (!req
.complete
)
372 #endif /* CONFIG_ADB_CUDA */
375 *-------------------------------------------------------------------
376 * Below this point are the generic routines; they'll dispatch to the
377 * correct routine for the hardware on which we're running.
378 *-------------------------------------------------------------------
381 void mac_pram_read(int offset
, __u8
*buffer
, int len
)
386 switch (macintosh_config
->adb_type
) {
390 func
= via_read_pram
;
392 #ifdef CONFIG_ADB_CUDA
395 func
= cuda_read_pram
;
398 #ifdef CONFIG_ADB_PMU
400 func
= pmu_read_pram
;
406 for (i
= 0 ; i
< len
; i
++) {
407 buffer
[i
] = (*func
)(offset
++);
411 void mac_pram_write(int offset
, __u8
*buffer
, int len
)
413 void (*func
)(int, __u8
);
416 switch (macintosh_config
->adb_type
) {
420 func
= via_write_pram
;
422 #ifdef CONFIG_ADB_CUDA
425 func
= cuda_write_pram
;
428 #ifdef CONFIG_ADB_PMU
430 func
= pmu_write_pram
;
436 for (i
= 0 ; i
< len
; i
++) {
437 (*func
)(offset
++, buffer
[i
]);
441 void mac_poweroff(void)
445 } else if (macintosh_config
->adb_type
== MAC_ADB_II
) {
447 #ifdef CONFIG_ADB_CUDA
448 } else if (macintosh_config
->adb_type
== MAC_ADB_EGRET
||
449 macintosh_config
->adb_type
== MAC_ADB_CUDA
) {
452 #ifdef CONFIG_ADB_PMU
453 } else if (macintosh_config
->adb_type
== MAC_ADB_PB2
) {
458 pr_crit("It is now safe to turn off your Macintosh.\n");
465 if (macintosh_config
->adb_type
== MAC_ADB_II
) {
468 /* need ROMBASE in booter */
469 /* indeed, plus need to MAP THE ROM !! */
471 if (mac_bi_data
.rombase
== 0)
472 mac_bi_data
.rombase
= 0x40800000;
475 rom_reset
= (void *) (mac_bi_data
.rombase
+ 0xa);
477 if (macintosh_config
->ident
== MAC_MODEL_SE30
) {
479 * MSch: Machines known to crash on ROM reset ...
482 local_irq_save(flags
);
486 local_irq_restore(flags
);
488 #ifdef CONFIG_ADB_CUDA
489 } else if (macintosh_config
->adb_type
== MAC_ADB_EGRET
||
490 macintosh_config
->adb_type
== MAC_ADB_CUDA
) {
493 #ifdef CONFIG_ADB_PMU
494 } else if (macintosh_config
->adb_type
== MAC_ADB_PB2
) {
497 } else if (CPU_IS_030
) {
499 /* 030-specific reset routine. The idea is general, but the
500 * specific registers to reset are '030-specific. Until I
501 * have a non-030 machine, I can't test anything else.
502 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
505 unsigned long rombase
= 0x40000000;
507 /* make a 1-to-1 mapping, using the transparent tran. reg. */
508 unsigned long virt
= (unsigned long) mac_reset
;
509 unsigned long phys
= virt_to_phys(mac_reset
);
510 unsigned long addr
= (phys
&0xFF000000)|0x8777;
511 unsigned long offset
= phys
-virt
;
513 local_irq_disable(); /* lets not screw this up, ok? */
514 __asm__
__volatile__(".chip 68030\n\t"
518 /* Now jump to physical address so we can disable MMU */
519 __asm__
__volatile__(
521 "lea %/pc@(1f),%/a0\n\t"
522 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
525 "jmp %/a0@\n\t" /* jump into physical memory */
526 "0:.long 0\n\t" /* a constant zero. */
527 /* OK. Now reset everything and jump to reset vector. */
529 "lea %/pc@(0b),%/a0\n\t"
530 "pmove %/a0@, %/tc\n\t" /* disable mmu */
531 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
532 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
534 "movec %/a0, %/vbr\n\t" /* clear vector base register */
535 "movec %/a0, %/cacr\n\t" /* disable caches */
536 "movel #0x0808,%/a0\n\t"
537 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
538 "movew #0x2700,%/sr\n\t" /* set up status register */
539 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
540 "movec %/a0, %/isp\n\t"
541 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
542 "reset\n\t" /* reset external devices */
543 "jmp %/a0@\n\t" /* jump to the reset vector */
545 : : "r" (offset
), "a" (rombase
) : "a0");
548 /* should never get here */
549 pr_crit("Restart failed. Please restart manually.\n");
555 * This function translates seconds since 1970 into a proper date.
557 * Algorithm cribbed from glibc2.1, __offtime().
559 * This is roughly same as rtc_time64_to_tm(), which we should probably
560 * use here, but it's only available when CONFIG_RTC_LIB is enabled.
562 #define SECS_PER_MINUTE (60)
563 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
564 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
566 static void unmktime(time64_t time
, long offset
,
567 int *yearp
, int *monp
, int *dayp
,
568 int *hourp
, int *minp
, int *secp
)
570 /* How many days come before each month (0-12). */
571 static const unsigned short int __mon_yday
[2][13] =
574 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
576 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
578 int days
, rem
, y
, wday
, yday
;
579 const unsigned short int *ip
;
581 days
= div_u64_rem(time
, SECS_PER_DAY
, &rem
);
587 while (rem
>= SECS_PER_DAY
) {
591 *hourp
= rem
/ SECS_PER_HOUR
;
592 rem
%= SECS_PER_HOUR
;
593 *minp
= rem
/ SECS_PER_MINUTE
;
594 *secp
= rem
% SECS_PER_MINUTE
;
595 /* January 1, 1970 was a Thursday. */
596 wday
= (4 + days
) % 7; /* Day in the week. Not currently used */
597 if (wday
< 0) wday
+= 7;
600 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
601 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
602 #define __isleap(year) \
603 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
605 while (days
< 0 || days
>= (__isleap (y
) ? 366 : 365))
607 /* Guess a corrected year, assuming 365 days per year. */
608 long int yg
= y
+ days
/ 365 - (days
% 365 < 0);
610 /* Adjust DAYS and Y to match the guessed year. */
611 days
-= (yg
- y
) * 365 +
612 LEAPS_THRU_END_OF(yg
- 1) - LEAPS_THRU_END_OF(y
- 1);
616 yday
= days
; /* day in the year. Not currently used. */
617 ip
= __mon_yday
[__isleap(y
)];
618 for (y
= 11; days
< (long int) ip
[y
]; --y
)
622 *dayp
= days
+ 1; /* day in the month */
627 * Read/write the hardware clock.
630 int mac_hwclk(int op
, struct rtc_time
*t
)
634 if (!op
) { /* read */
635 switch (macintosh_config
->adb_type
) {
639 now
= via_read_time();
641 #ifdef CONFIG_ADB_CUDA
644 now
= cuda_read_time();
647 #ifdef CONFIG_ADB_PMU
649 now
= pmu_read_time();
658 &t
->tm_year
, &t
->tm_mon
, &t
->tm_mday
,
659 &t
->tm_hour
, &t
->tm_min
, &t
->tm_sec
);
660 pr_debug("%s: read %04d-%02d-%-2d %02d:%02d:%02d\n",
661 __func__
, t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
662 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
664 pr_debug("%s: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
665 __func__
, t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
666 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
668 now
= mktime64(t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
669 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
671 switch (macintosh_config
->adb_type
) {
677 #ifdef CONFIG_ADB_CUDA
680 cuda_write_time(now
);
683 #ifdef CONFIG_ADB_PMU