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>
29 /* Offset between Unix time (1970-based) and Mac time (1904-based) */
31 #define RTC_OFFSET 2082844800
33 static void (*rom_reset
)(void);
35 #ifdef CONFIG_ADB_CUDA
36 static long cuda_read_time(void)
38 struct adb_request req
;
41 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_GET_TIME
) < 0)
46 time
= (req
.reply
[3] << 24) | (req
.reply
[4] << 16) |
47 (req
.reply
[5] << 8) | req
.reply
[6];
48 return time
- RTC_OFFSET
;
51 static void cuda_write_time(long data
)
53 struct adb_request req
;
56 if (cuda_request(&req
, NULL
, 6, CUDA_PACKET
, CUDA_SET_TIME
,
57 (data
>> 24) & 0xFF, (data
>> 16) & 0xFF,
58 (data
>> 8) & 0xFF, data
& 0xFF) < 0)
64 static __u8
cuda_read_pram(int offset
)
66 struct adb_request req
;
68 if (cuda_request(&req
, NULL
, 4, CUDA_PACKET
, CUDA_GET_PRAM
,
69 (offset
>> 8) & 0xFF, offset
& 0xFF) < 0)
76 static void cuda_write_pram(int offset
, __u8 data
)
78 struct adb_request req
;
80 if (cuda_request(&req
, NULL
, 5, CUDA_PACKET
, CUDA_SET_PRAM
,
81 (offset
>> 8) & 0xFF, offset
& 0xFF, data
) < 0)
86 #endif /* CONFIG_ADB_CUDA */
88 #ifdef CONFIG_ADB_PMU68K
89 static long pmu_read_time(void)
91 struct adb_request req
;
94 if (pmu_request(&req
, NULL
, 1, PMU_READ_RTC
) < 0)
99 time
= (req
.reply
[1] << 24) | (req
.reply
[2] << 16) |
100 (req
.reply
[3] << 8) | req
.reply
[4];
101 return time
- RTC_OFFSET
;
104 static void pmu_write_time(long data
)
106 struct adb_request req
;
109 if (pmu_request(&req
, NULL
, 5, PMU_SET_RTC
,
110 (data
>> 24) & 0xFF, (data
>> 16) & 0xFF,
111 (data
>> 8) & 0xFF, data
& 0xFF) < 0)
113 while (!req
.complete
)
117 static __u8
pmu_read_pram(int offset
)
119 struct adb_request req
;
121 if (pmu_request(&req
, NULL
, 3, PMU_READ_NVRAM
,
122 (offset
>> 8) & 0xFF, offset
& 0xFF) < 0)
124 while (!req
.complete
)
129 static void pmu_write_pram(int offset
, __u8 data
)
131 struct adb_request req
;
133 if (pmu_request(&req
, NULL
, 4, PMU_WRITE_NVRAM
,
134 (offset
>> 8) & 0xFF, offset
& 0xFF, data
) < 0)
136 while (!req
.complete
)
139 #endif /* CONFIG_ADB_PMU68K */
142 * VIA PRAM/RTC access routines
144 * Must be called with interrupts disabled and
145 * the RTC should be enabled.
148 static __u8
via_pram_readbyte(void)
153 reg
= via1
[vBufB
] & ~VIA1B_vRTCClk
;
155 /* Set the RTC data line to be an input. */
157 via1
[vDirB
] &= ~VIA1B_vRTCData
;
159 /* The bits of the byte come out in MSB order */
162 for (i
= 0 ; i
< 8 ; i
++) {
164 via1
[vBufB
] = reg
| VIA1B_vRTCClk
;
165 data
= (data
<< 1) | (via1
[vBufB
] & VIA1B_vRTCData
);
168 /* Return RTC data line to output state */
170 via1
[vDirB
] |= VIA1B_vRTCData
;
175 static void via_pram_writebyte(__u8 data
)
179 reg
= via1
[vBufB
] & ~(VIA1B_vRTCClk
| VIA1B_vRTCData
);
181 /* The bits of the byte go in in MSB order */
183 for (i
= 0 ; i
< 8 ; i
++) {
184 bit
= data
& 0x80? 1 : 0;
186 via1
[vBufB
] = reg
| bit
;
187 via1
[vBufB
] = reg
| bit
| VIA1B_vRTCClk
;
192 * Execute a VIA PRAM/RTC command. For read commands
193 * data should point to a one-byte buffer for the
194 * resulting data. For write commands it should point
195 * to the data byte to for the command.
197 * This function disables all interrupts while running.
200 static void via_pram_command(int command
, __u8
*data
)
205 local_irq_save(flags
);
207 /* Enable the RTC and make sure the strobe line is high */
209 via1
[vBufB
] = (via1
[vBufB
] | VIA1B_vRTCClk
) & ~VIA1B_vRTCEnb
;
211 if (command
& 0xFF00) { /* extended (two-byte) command */
212 via_pram_writebyte((command
& 0xFF00) >> 8);
213 via_pram_writebyte(command
& 0xFF);
214 is_read
= command
& 0x8000;
215 } else { /* one-byte command */
216 via_pram_writebyte(command
);
217 is_read
= command
& 0x80;
220 *data
= via_pram_readbyte();
222 via_pram_writebyte(*data
);
225 /* All done, disable the RTC */
227 via1
[vBufB
] |= VIA1B_vRTCEnb
;
229 local_irq_restore(flags
);
232 static __u8
via_read_pram(int offset
)
237 static void via_write_pram(int offset
, __u8 data
)
242 * Return the current time in seconds since January 1, 1904.
244 * This only works on machines with the VIA-based PRAM/RTC, which
245 * is basically any machine with Mac II-style ADB.
248 static long via_read_time(void)
253 } result
, last_result
;
256 via_pram_command(0x81, &last_result
.cdata
[3]);
257 via_pram_command(0x85, &last_result
.cdata
[2]);
258 via_pram_command(0x89, &last_result
.cdata
[1]);
259 via_pram_command(0x8D, &last_result
.cdata
[0]);
262 * The NetBSD guys say to loop until you get the same reading
267 via_pram_command(0x81, &result
.cdata
[3]);
268 via_pram_command(0x85, &result
.cdata
[2]);
269 via_pram_command(0x89, &result
.cdata
[1]);
270 via_pram_command(0x8D, &result
.cdata
[0]);
272 if (result
.idata
== last_result
.idata
)
273 return result
.idata
- RTC_OFFSET
;
278 last_result
.idata
= result
.idata
;
281 pr_err("via_read_time: failed to read a stable value; got 0x%08lx then 0x%08lx\n",
282 last_result
.idata
, result
.idata
);
288 * Set the current time to a number of seconds since January 1, 1904.
290 * This only works on machines with the VIA-based PRAM/RTC, which
291 * is basically any machine with Mac II-style ADB.
294 static void via_write_time(long time
)
302 /* Clear the write protect bit */
305 via_pram_command(0x35, &temp
);
307 data
.idata
= time
+ RTC_OFFSET
;
308 via_pram_command(0x01, &data
.cdata
[3]);
309 via_pram_command(0x05, &data
.cdata
[2]);
310 via_pram_command(0x09, &data
.cdata
[1]);
311 via_pram_command(0x0D, &data
.cdata
[0]);
313 /* Set the write protect bit */
316 via_pram_command(0x35, &temp
);
319 static void via_shutdown(void)
322 via2
[rBufB
] &= ~0x04;
324 /* Direction of vDirB is output */
326 /* Send a value of 0 on that line */
327 via2
[vBufB
] &= ~0x04;
332 static void oss_shutdown(void)
334 oss
->rom_ctrl
= OSS_POWEROFF
;
337 #ifdef CONFIG_ADB_CUDA
338 static void cuda_restart(void)
340 struct adb_request req
;
342 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_RESET_SYSTEM
) < 0)
344 while (!req
.complete
)
348 static void cuda_shutdown(void)
350 struct adb_request req
;
352 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_POWERDOWN
) < 0)
355 /* Avoid infinite polling loop when PSU is not under Cuda control */
356 switch (macintosh_config
->ident
) {
359 case MAC_MODEL_Q605_ACC
:
361 case MAC_MODEL_P475F
:
365 while (!req
.complete
)
368 #endif /* CONFIG_ADB_CUDA */
370 #ifdef CONFIG_ADB_PMU68K
372 void pmu_restart(void)
374 struct adb_request req
;
375 if (pmu_request(&req
, NULL
,
376 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|PMU_INT_TICK
) < 0)
378 while (!req
.complete
)
380 if (pmu_request(&req
, NULL
, 1, PMU_RESET
) < 0)
382 while (!req
.complete
)
386 void pmu_shutdown(void)
388 struct adb_request req
;
389 if (pmu_request(&req
, NULL
,
390 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|PMU_INT_TICK
) < 0)
392 while (!req
.complete
)
394 if (pmu_request(&req
, NULL
, 5, PMU_SHUTDOWN
, 'M', 'A', 'T', 'T') < 0)
396 while (!req
.complete
)
403 *-------------------------------------------------------------------
404 * Below this point are the generic routines; they'll dispatch to the
405 * correct routine for the hardware on which we're running.
406 *-------------------------------------------------------------------
409 void mac_pram_read(int offset
, __u8
*buffer
, int len
)
414 switch (macintosh_config
->adb_type
) {
418 func
= via_read_pram
;
420 #ifdef CONFIG_ADB_CUDA
423 func
= cuda_read_pram
;
426 #ifdef CONFIG_ADB_PMU68K
428 func
= pmu_read_pram
;
434 for (i
= 0 ; i
< len
; i
++) {
435 buffer
[i
] = (*func
)(offset
++);
439 void mac_pram_write(int offset
, __u8
*buffer
, int len
)
441 void (*func
)(int, __u8
);
444 switch (macintosh_config
->adb_type
) {
448 func
= via_write_pram
;
450 #ifdef CONFIG_ADB_CUDA
453 func
= cuda_write_pram
;
456 #ifdef CONFIG_ADB_PMU68K
458 func
= pmu_write_pram
;
464 for (i
= 0 ; i
< len
; i
++) {
465 (*func
)(offset
++, buffer
[i
]);
469 void mac_poweroff(void)
473 } else if (macintosh_config
->adb_type
== MAC_ADB_II
) {
475 #ifdef CONFIG_ADB_CUDA
476 } else if (macintosh_config
->adb_type
== MAC_ADB_EGRET
||
477 macintosh_config
->adb_type
== MAC_ADB_CUDA
) {
480 #ifdef CONFIG_ADB_PMU68K
481 } else if (macintosh_config
->adb_type
== MAC_ADB_PB1
482 || macintosh_config
->adb_type
== MAC_ADB_PB2
) {
487 pr_crit("It is now safe to turn off your Macintosh.\n");
494 if (macintosh_config
->adb_type
== MAC_ADB_II
) {
497 /* need ROMBASE in booter */
498 /* indeed, plus need to MAP THE ROM !! */
500 if (mac_bi_data
.rombase
== 0)
501 mac_bi_data
.rombase
= 0x40800000;
504 rom_reset
= (void *) (mac_bi_data
.rombase
+ 0xa);
506 if (macintosh_config
->ident
== MAC_MODEL_SE30
) {
508 * MSch: Machines known to crash on ROM reset ...
511 local_irq_save(flags
);
515 local_irq_restore(flags
);
517 #ifdef CONFIG_ADB_CUDA
518 } else if (macintosh_config
->adb_type
== MAC_ADB_EGRET
||
519 macintosh_config
->adb_type
== MAC_ADB_CUDA
) {
522 #ifdef CONFIG_ADB_PMU68K
523 } else if (macintosh_config
->adb_type
== MAC_ADB_PB1
524 || macintosh_config
->adb_type
== MAC_ADB_PB2
) {
527 } else if (CPU_IS_030
) {
529 /* 030-specific reset routine. The idea is general, but the
530 * specific registers to reset are '030-specific. Until I
531 * have a non-030 machine, I can't test anything else.
532 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
535 unsigned long rombase
= 0x40000000;
537 /* make a 1-to-1 mapping, using the transparent tran. reg. */
538 unsigned long virt
= (unsigned long) mac_reset
;
539 unsigned long phys
= virt_to_phys(mac_reset
);
540 unsigned long addr
= (phys
&0xFF000000)|0x8777;
541 unsigned long offset
= phys
-virt
;
543 local_irq_disable(); /* lets not screw this up, ok? */
544 __asm__
__volatile__(".chip 68030\n\t"
548 /* Now jump to physical address so we can disable MMU */
549 __asm__
__volatile__(
551 "lea %/pc@(1f),%/a0\n\t"
552 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
555 "jmp %/a0@\n\t" /* jump into physical memory */
556 "0:.long 0\n\t" /* a constant zero. */
557 /* OK. Now reset everything and jump to reset vector. */
559 "lea %/pc@(0b),%/a0\n\t"
560 "pmove %/a0@, %/tc\n\t" /* disable mmu */
561 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
562 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
564 "movec %/a0, %/vbr\n\t" /* clear vector base register */
565 "movec %/a0, %/cacr\n\t" /* disable caches */
566 "movel #0x0808,%/a0\n\t"
567 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
568 "movew #0x2700,%/sr\n\t" /* set up status register */
569 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
570 "movec %/a0, %/isp\n\t"
571 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
572 "reset\n\t" /* reset external devices */
573 "jmp %/a0@\n\t" /* jump to the reset vector */
575 : : "r" (offset
), "a" (rombase
) : "a0");
578 /* should never get here */
579 pr_crit("Restart failed. Please restart manually.\n");
585 * This function translates seconds since 1970 into a proper date.
587 * Algorithm cribbed from glibc2.1, __offtime().
589 #define SECS_PER_MINUTE (60)
590 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
591 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
593 static void unmktime(unsigned long time
, long offset
,
594 int *yearp
, int *monp
, int *dayp
,
595 int *hourp
, int *minp
, int *secp
)
597 /* How many days come before each month (0-12). */
598 static const unsigned short int __mon_yday
[2][13] =
601 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
603 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
605 long int days
, rem
, y
, wday
, yday
;
606 const unsigned short int *ip
;
608 days
= time
/ SECS_PER_DAY
;
609 rem
= time
% SECS_PER_DAY
;
615 while (rem
>= SECS_PER_DAY
) {
619 *hourp
= rem
/ SECS_PER_HOUR
;
620 rem
%= SECS_PER_HOUR
;
621 *minp
= rem
/ SECS_PER_MINUTE
;
622 *secp
= rem
% SECS_PER_MINUTE
;
623 /* January 1, 1970 was a Thursday. */
624 wday
= (4 + days
) % 7; /* Day in the week. Not currently used */
625 if (wday
< 0) wday
+= 7;
628 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
629 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
630 #define __isleap(year) \
631 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
633 while (days
< 0 || days
>= (__isleap (y
) ? 366 : 365))
635 /* Guess a corrected year, assuming 365 days per year. */
636 long int yg
= y
+ days
/ 365 - (days
% 365 < 0);
638 /* Adjust DAYS and Y to match the guessed year. */
639 days
-= (yg
- y
) * 365 +
640 LEAPS_THRU_END_OF(yg
- 1) - LEAPS_THRU_END_OF(y
- 1);
644 yday
= days
; /* day in the year. Not currently used. */
645 ip
= __mon_yday
[__isleap(y
)];
646 for (y
= 11; days
< (long int) ip
[y
]; --y
)
650 *dayp
= days
+ 1; /* day in the month */
655 * Read/write the hardware clock.
658 int mac_hwclk(int op
, struct rtc_time
*t
)
662 if (!op
) { /* read */
663 switch (macintosh_config
->adb_type
) {
667 now
= via_read_time();
669 #ifdef CONFIG_ADB_CUDA
672 now
= cuda_read_time();
675 #ifdef CONFIG_ADB_PMU68K
677 now
= pmu_read_time();
686 &t
->tm_year
, &t
->tm_mon
, &t
->tm_mday
,
687 &t
->tm_hour
, &t
->tm_min
, &t
->tm_sec
);
688 pr_debug("%s: read %04d-%02d-%-2d %02d:%02d:%02d\n",
689 __func__
, t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
690 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
692 pr_debug("%s: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
693 __func__
, t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
694 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
696 now
= mktime(t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
697 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
699 switch (macintosh_config
->adb_type
) {
705 #ifdef CONFIG_ADB_CUDA
708 cuda_write_time(now
);
711 #ifdef CONFIG_ADB_PMU68K
724 * Set minutes/seconds in the hardware clock
727 int mac_set_clock_mmss (unsigned long nowtime
)
732 now
.tm_sec
= nowtime
% 60;
733 now
.tm_min
= (nowtime
/ 60) % 60;