2 * Miscellaneous Mac68K-specific stuff
5 #include <linux/types.h>
6 #include <linux/errno.h>
7 #include <linux/miscdevice.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 <asm/uaccess.h>
22 #include <asm/segment.h>
23 #include <asm/setup.h>
24 #include <asm/macintosh.h>
25 #include <asm/mac_via.h>
26 #include <asm/mac_oss.h>
28 #define BOOTINFO_COMPAT_1_0
29 #include <asm/bootinfo.h>
30 #include <asm/machdep.h>
32 /* Offset between Unix time (1970-based) and Mac time (1904-based) */
34 #define RTC_OFFSET 2082844800
36 static void (*rom_reset
)(void);
38 #ifdef CONFIG_ADB_CUDA
39 static long cuda_read_time(void)
41 struct adb_request req
;
44 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_GET_TIME
) < 0)
49 time
= (req
.reply
[3] << 24) | (req
.reply
[4] << 16)
50 | (req
.reply
[5] << 8) | req
.reply
[6];
51 return time
- RTC_OFFSET
;
54 static void cuda_write_time(long data
)
56 struct adb_request req
;
58 if (cuda_request(&req
, NULL
, 6, CUDA_PACKET
, CUDA_SET_TIME
,
59 (data
>> 24) & 0xFF, (data
>> 16) & 0xFF,
60 (data
>> 8) & 0xFF, data
& 0xFF) < 0)
66 static __u8
cuda_read_pram(int offset
)
68 struct adb_request req
;
69 if (cuda_request(&req
, NULL
, 4, CUDA_PACKET
, CUDA_GET_PRAM
,
70 (offset
>> 8) & 0xFF, offset
& 0xFF) < 0)
77 static void cuda_write_pram(int offset
, __u8 data
)
79 struct adb_request req
;
80 if (cuda_request(&req
, NULL
, 5, CUDA_PACKET
, CUDA_SET_PRAM
,
81 (offset
>> 8) & 0xFF, offset
& 0xFF, data
) < 0)
87 #define cuda_read_time() 0
88 #define cuda_write_time(n)
89 #define cuda_read_pram NULL
90 #define cuda_write_pram NULL
93 #ifdef CONFIG_ADB_PMU68K
94 static long 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
= (req
.reply
[1] << 24) | (req
.reply
[2] << 16)
105 | (req
.reply
[3] << 8) | req
.reply
[4];
106 return time
- RTC_OFFSET
;
109 static void pmu_write_time(long data
)
111 struct adb_request req
;
113 if (pmu_request(&req
, NULL
, 5, PMU_SET_RTC
,
114 (data
>> 24) & 0xFF, (data
>> 16) & 0xFF,
115 (data
>> 8) & 0xFF, data
& 0xFF) < 0)
117 while (!req
.complete
)
121 static __u8
pmu_read_pram(int offset
)
123 struct adb_request req
;
124 if (pmu_request(&req
, NULL
, 3, PMU_READ_NVRAM
,
125 (offset
>> 8) & 0xFF, offset
& 0xFF) < 0)
127 while (!req
.complete
)
132 static void pmu_write_pram(int offset
, __u8 data
)
134 struct adb_request req
;
135 if (pmu_request(&req
, NULL
, 4, PMU_WRITE_NVRAM
,
136 (offset
>> 8) & 0xFF, offset
& 0xFF, data
) < 0)
138 while (!req
.complete
)
142 #define pmu_read_time() 0
143 #define pmu_write_time(n)
144 #define pmu_read_pram NULL
145 #define pmu_write_pram NULL
148 #if 0 /* def CONFIG_ADB_MACIISI */
149 extern int maciisi_request(struct adb_request
*req
,
150 void (*done
)(struct adb_request
*), int nbytes
, ...);
152 static long maciisi_read_time(void)
154 struct adb_request req
;
157 if (maciisi_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_GET_TIME
))
160 time
= (req
.reply
[3] << 24) | (req
.reply
[4] << 16)
161 | (req
.reply
[5] << 8) | req
.reply
[6];
162 return time
- RTC_OFFSET
;
165 static void maciisi_write_time(long data
)
167 struct adb_request req
;
169 maciisi_request(&req
, NULL
, 6, CUDA_PACKET
, CUDA_SET_TIME
,
170 (data
>> 24) & 0xFF, (data
>> 16) & 0xFF,
171 (data
>> 8) & 0xFF, data
& 0xFF);
174 static __u8
maciisi_read_pram(int offset
)
176 struct adb_request req
;
177 if (maciisi_request(&req
, NULL
, 4, CUDA_PACKET
, CUDA_GET_PRAM
,
178 (offset
>> 8) & 0xFF, offset
& 0xFF))
183 static void maciisi_write_pram(int offset
, __u8 data
)
185 struct adb_request req
;
186 maciisi_request(&req
, NULL
, 5, CUDA_PACKET
, CUDA_SET_PRAM
,
187 (offset
>> 8) & 0xFF, offset
& 0xFF, data
);
190 #define maciisi_read_time() 0
191 #define maciisi_write_time(n)
192 #define maciisi_read_pram NULL
193 #define maciisi_write_pram NULL
197 * VIA PRAM/RTC access routines
199 * Must be called with interrupts disabled and
200 * the RTC should be enabled.
203 static __u8
via_pram_readbyte(void)
208 reg
= via1
[vBufB
] & ~VIA1B_vRTCClk
;
210 /* Set the RTC data line to be an input. */
212 via1
[vDirB
] &= ~VIA1B_vRTCData
;
214 /* The bits of the byte come out in MSB order */
217 for (i
= 0 ; i
< 8 ; i
++) {
219 via1
[vBufB
] = reg
| VIA1B_vRTCClk
;
220 data
= (data
<< 1) | (via1
[vBufB
] & VIA1B_vRTCData
);
223 /* Return RTC data line to output state */
225 via1
[vDirB
] |= VIA1B_vRTCData
;
230 static void via_pram_writebyte(__u8 data
)
234 reg
= via1
[vBufB
] & ~(VIA1B_vRTCClk
| VIA1B_vRTCData
);
236 /* The bits of the byte go in in MSB order */
238 for (i
= 0 ; i
< 8 ; i
++) {
239 bit
= data
& 0x80? 1 : 0;
241 via1
[vBufB
] = reg
| bit
;
242 via1
[vBufB
] = reg
| bit
| VIA1B_vRTCClk
;
247 * Execute a VIA PRAM/RTC command. For read commands
248 * data should point to a one-byte buffer for the
249 * resulting data. For write commands it should point
250 * to the data byte to for the command.
252 * This function disables all interrupts while running.
255 static void via_pram_command(int command
, __u8
*data
)
260 local_irq_save(flags
);
262 /* Enable the RTC and make sure the strobe line is high */
264 via1
[vBufB
] = (via1
[vBufB
] | VIA1B_vRTCClk
) & ~VIA1B_vRTCEnb
;
266 if (command
& 0xFF00) { /* extended (two-byte) command */
267 via_pram_writebyte((command
& 0xFF00) >> 8);
268 via_pram_writebyte(command
& 0xFF);
269 is_read
= command
& 0x8000;
270 } else { /* one-byte command */
271 via_pram_writebyte(command
);
272 is_read
= command
& 0x80;
275 *data
= via_pram_readbyte();
277 via_pram_writebyte(*data
);
280 /* All done, disable the RTC */
282 via1
[vBufB
] |= VIA1B_vRTCEnb
;
284 local_irq_restore(flags
);
287 static __u8
via_read_pram(int offset
)
292 static void via_write_pram(int offset
, __u8 data
)
297 * Return the current time in seconds since January 1, 1904.
299 * This only works on machines with the VIA-based PRAM/RTC, which
300 * is basically any machine with Mac II-style ADB.
303 static long via_read_time(void)
308 } result
, last_result
;
311 via_pram_command(0x81, &last_result
.cdata
[3]);
312 via_pram_command(0x85, &last_result
.cdata
[2]);
313 via_pram_command(0x89, &last_result
.cdata
[1]);
314 via_pram_command(0x8D, &last_result
.cdata
[0]);
317 * The NetBSD guys say to loop until you get the same reading
322 via_pram_command(0x81, &result
.cdata
[3]);
323 via_pram_command(0x85, &result
.cdata
[2]);
324 via_pram_command(0x89, &result
.cdata
[1]);
325 via_pram_command(0x8D, &result
.cdata
[0]);
327 if (result
.idata
== last_result
.idata
)
328 return result
.idata
- RTC_OFFSET
;
333 last_result
.idata
= result
.idata
;
336 pr_err("via_read_time: failed to read a stable value; "
337 "got 0x%08lx then 0x%08lx\n",
338 last_result
.idata
, result
.idata
);
344 * Set the current time to a number of seconds since January 1, 1904.
346 * This only works on machines with the VIA-based PRAM/RTC, which
347 * is basically any machine with Mac II-style ADB.
350 static void via_write_time(long time
)
358 /* Clear the write protect bit */
361 via_pram_command(0x35, &temp
);
363 data
.idata
= time
+ RTC_OFFSET
;
364 via_pram_command(0x01, &data
.cdata
[3]);
365 via_pram_command(0x05, &data
.cdata
[2]);
366 via_pram_command(0x09, &data
.cdata
[1]);
367 via_pram_command(0x0D, &data
.cdata
[0]);
369 /* Set the write protect bit */
372 via_pram_command(0x35, &temp
);
375 static void via_shutdown(void)
378 via2
[rBufB
] &= ~0x04;
380 /* Direction of vDirB is output */
382 /* Send a value of 0 on that line */
383 via2
[vBufB
] &= ~0x04;
389 * FIXME: not sure how this is supposed to work exactly...
392 static void oss_shutdown(void)
394 oss
->rom_ctrl
= OSS_POWEROFF
;
397 #ifdef CONFIG_ADB_CUDA
399 static void cuda_restart(void)
401 struct adb_request req
;
402 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_RESET_SYSTEM
) < 0)
404 while (!req
.complete
)
408 static void cuda_shutdown(void)
410 struct adb_request req
;
411 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_POWERDOWN
) < 0)
413 while (!req
.complete
)
417 #endif /* CONFIG_ADB_CUDA */
419 #ifdef CONFIG_ADB_PMU68K
421 void pmu_restart(void)
423 struct adb_request req
;
424 if (pmu_request(&req
, NULL
,
425 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|PMU_INT_TICK
) < 0)
427 while (!req
.complete
)
429 if (pmu_request(&req
, NULL
, 1, PMU_RESET
) < 0)
431 while (!req
.complete
)
435 void pmu_shutdown(void)
437 struct adb_request req
;
438 if (pmu_request(&req
, NULL
,
439 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|PMU_INT_TICK
) < 0)
441 while (!req
.complete
)
443 if (pmu_request(&req
, NULL
, 5, PMU_SHUTDOWN
, 'M', 'A', 'T', 'T') < 0)
445 while (!req
.complete
)
452 *-------------------------------------------------------------------
453 * Below this point are the generic routines; they'll dispatch to the
454 * correct routine for the hardware on which we're running.
455 *-------------------------------------------------------------------
458 void mac_pram_read(int offset
, __u8
*buffer
, int len
)
463 switch(macintosh_config
->adb_type
) {
465 func
= maciisi_read_pram
; break;
468 func
= pmu_read_pram
; break;
470 func
= cuda_read_pram
; break;
472 func
= via_read_pram
;
476 for (i
= 0 ; i
< len
; i
++) {
477 buffer
[i
] = (*func
)(offset
++);
481 void mac_pram_write(int offset
, __u8
*buffer
, int len
)
483 void (*func
)(int, __u8
);
486 switch(macintosh_config
->adb_type
) {
488 func
= maciisi_write_pram
; break;
491 func
= pmu_write_pram
; break;
493 func
= cuda_write_pram
; break;
495 func
= via_write_pram
;
499 for (i
= 0 ; i
< len
; i
++) {
500 (*func
)(offset
++, buffer
[i
]);
504 void mac_poweroff(void)
507 * MAC_ADB_IISI may need to be moved up here if it doesn't actually
508 * work using the ADB packet method. --David Kilzer
513 } else if (macintosh_config
->adb_type
== MAC_ADB_II
) {
515 #ifdef CONFIG_ADB_CUDA
516 } else if (macintosh_config
->adb_type
== MAC_ADB_CUDA
) {
519 #ifdef CONFIG_ADB_PMU68K
520 } else if (macintosh_config
->adb_type
== MAC_ADB_PB1
521 || macintosh_config
->adb_type
== MAC_ADB_PB2
) {
526 printk("It is now safe to turn off your Macintosh.\n");
532 if (macintosh_config
->adb_type
== MAC_ADB_II
) {
535 /* need ROMBASE in booter */
536 /* indeed, plus need to MAP THE ROM !! */
538 if (mac_bi_data
.rombase
== 0)
539 mac_bi_data
.rombase
= 0x40800000;
542 rom_reset
= (void *) (mac_bi_data
.rombase
+ 0xa);
544 if (macintosh_config
->ident
== MAC_MODEL_SE30
) {
546 * MSch: Machines known to crash on ROM reset ...
549 local_irq_save(flags
);
553 local_irq_restore(flags
);
555 #ifdef CONFIG_ADB_CUDA
556 } else if (macintosh_config
->adb_type
== MAC_ADB_CUDA
) {
559 #ifdef CONFIG_ADB_PMU68K
560 } else if (macintosh_config
->adb_type
== MAC_ADB_PB1
561 || macintosh_config
->adb_type
== MAC_ADB_PB2
) {
564 } else if (CPU_IS_030
) {
566 /* 030-specific reset routine. The idea is general, but the
567 * specific registers to reset are '030-specific. Until I
568 * have a non-030 machine, I can't test anything else.
569 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
572 unsigned long rombase
= 0x40000000;
574 /* make a 1-to-1 mapping, using the transparent tran. reg. */
575 unsigned long virt
= (unsigned long) mac_reset
;
576 unsigned long phys
= virt_to_phys(mac_reset
);
577 unsigned long addr
= (phys
&0xFF000000)|0x8777;
578 unsigned long offset
= phys
-virt
;
579 local_irq_disable(); /* lets not screw this up, ok? */
580 __asm__
__volatile__(".chip 68030\n\t"
584 /* Now jump to physical address so we can disable MMU */
585 __asm__
__volatile__(
587 "lea %/pc@(1f),%/a0\n\t"
588 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
591 "jmp %/a0@\n\t" /* jump into physical memory */
592 "0:.long 0\n\t" /* a constant zero. */
593 /* OK. Now reset everything and jump to reset vector. */
595 "lea %/pc@(0b),%/a0\n\t"
596 "pmove %/a0@, %/tc\n\t" /* disable mmu */
597 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
598 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
600 "movec %/a0, %/vbr\n\t" /* clear vector base register */
601 "movec %/a0, %/cacr\n\t" /* disable caches */
602 "movel #0x0808,%/a0\n\t"
603 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
604 "movew #0x2700,%/sr\n\t" /* set up status register */
605 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
606 "movec %/a0, %/isp\n\t"
607 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
608 "reset\n\t" /* reset external devices */
609 "jmp %/a0@\n\t" /* jump to the reset vector */
611 : : "r" (offset
), "a" (rombase
) : "a0");
614 /* should never get here */
616 printk ("Restart failed. Please restart manually.\n");
621 * This function translates seconds since 1970 into a proper date.
623 * Algorithm cribbed from glibc2.1, __offtime().
625 #define SECS_PER_MINUTE (60)
626 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
627 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
629 static void unmktime(unsigned long time
, long offset
,
630 int *yearp
, int *monp
, int *dayp
,
631 int *hourp
, int *minp
, int *secp
)
633 /* How many days come before each month (0-12). */
634 static const unsigned short int __mon_yday
[2][13] =
637 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
639 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
641 long int days
, rem
, y
, wday
, yday
;
642 const unsigned short int *ip
;
644 days
= time
/ SECS_PER_DAY
;
645 rem
= time
% SECS_PER_DAY
;
651 while (rem
>= SECS_PER_DAY
) {
655 *hourp
= rem
/ SECS_PER_HOUR
;
656 rem
%= SECS_PER_HOUR
;
657 *minp
= rem
/ SECS_PER_MINUTE
;
658 *secp
= rem
% SECS_PER_MINUTE
;
659 /* January 1, 1970 was a Thursday. */
660 wday
= (4 + days
) % 7; /* Day in the week. Not currently used */
661 if (wday
< 0) wday
+= 7;
664 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
665 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
666 #define __isleap(year) \
667 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
669 while (days
< 0 || days
>= (__isleap (y
) ? 366 : 365))
671 /* Guess a corrected year, assuming 365 days per year. */
672 long int yg
= y
+ days
/ 365 - (days
% 365 < 0);
674 /* Adjust DAYS and Y to match the guessed year. */
675 days
-= ((yg
- y
) * 365
676 + LEAPS_THRU_END_OF (yg
- 1)
677 - LEAPS_THRU_END_OF (y
- 1));
681 yday
= days
; /* day in the year. Not currently used. */
682 ip
= __mon_yday
[__isleap(y
)];
683 for (y
= 11; days
< (long int) ip
[y
]; --y
)
687 *dayp
= days
+ 1; /* day in the month */
692 * Read/write the hardware clock.
695 int mac_hwclk(int op
, struct rtc_time
*t
)
699 if (!op
) { /* read */
700 switch (macintosh_config
->adb_type
) {
703 now
= via_read_time();
706 now
= maciisi_read_time();
710 now
= pmu_read_time();
713 now
= cuda_read_time();
721 &t
->tm_year
, &t
->tm_mon
, &t
->tm_mday
,
722 &t
->tm_hour
, &t
->tm_min
, &t
->tm_sec
);
724 printk("mac_hwclk: read %04d-%02d-%-2d %02d:%02d:%02d\n",
725 t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
726 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
730 printk("mac_hwclk: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
731 t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
732 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
735 now
= mktime(t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
736 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
738 switch (macintosh_config
->adb_type
) {
744 cuda_write_time(now
);
751 maciisi_write_time(now
);
758 * Set minutes/seconds in the hardware clock
761 int mac_set_clock_mmss (unsigned long nowtime
)
766 now
.tm_sec
= nowtime
% 60;
767 now
.tm_min
= (nowtime
/ 60) % 60;