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/slab.h>
12 #include <linux/time.h>
13 #include <linux/rtc.h>
16 #include <linux/adb.h>
17 #include <linux/cuda.h>
18 #include <linux/pmu.h>
20 #include <asm/uaccess.h>
23 #include <asm/system.h>
24 #include <asm/segment.h>
25 #include <asm/setup.h>
26 #include <asm/macintosh.h>
27 #include <asm/mac_via.h>
28 #include <asm/mac_oss.h>
30 #define BOOTINFO_COMPAT_1_0
31 #include <asm/bootinfo.h>
32 #include <asm/machdep.h>
34 /* Offset between Unix time (1970-based) and Mac time (1904-based) */
36 #define RTC_OFFSET 2082844800
38 static void (*rom_reset
)(void);
40 #ifdef CONFIG_ADB_CUDA
41 static long cuda_read_time(void)
43 struct adb_request req
;
46 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_GET_TIME
) < 0)
51 time
= (req
.reply
[3] << 24) | (req
.reply
[4] << 16)
52 | (req
.reply
[5] << 8) | req
.reply
[6];
53 return time
- RTC_OFFSET
;
56 static void cuda_write_time(long data
)
58 struct adb_request req
;
60 if (cuda_request(&req
, NULL
, 6, CUDA_PACKET
, CUDA_SET_TIME
,
61 (data
>> 24) & 0xFF, (data
>> 16) & 0xFF,
62 (data
>> 8) & 0xFF, data
& 0xFF) < 0)
68 static __u8
cuda_read_pram(int offset
)
70 struct adb_request req
;
71 if (cuda_request(&req
, NULL
, 4, CUDA_PACKET
, CUDA_GET_PRAM
,
72 (offset
>> 8) & 0xFF, offset
& 0xFF) < 0)
79 static void cuda_write_pram(int offset
, __u8 data
)
81 struct adb_request req
;
82 if (cuda_request(&req
, NULL
, 5, CUDA_PACKET
, CUDA_SET_PRAM
,
83 (offset
>> 8) & 0xFF, offset
& 0xFF, data
) < 0)
89 #define cuda_read_time() 0
90 #define cuda_write_time(n)
91 #define cuda_read_pram NULL
92 #define cuda_write_pram NULL
95 #if 0 /* def CONFIG_ADB_PMU68K */
96 static long pmu_read_time(void)
98 struct adb_request req
;
101 if (pmu_request(&req
, NULL
, 1, PMU_READ_RTC
) < 0)
103 while (!req
.complete
)
106 time
= (req
.reply
[0] << 24) | (req
.reply
[1] << 16)
107 | (req
.reply
[2] << 8) | req
.reply
[3];
108 return time
- RTC_OFFSET
;
111 static void pmu_write_time(long data
)
113 struct adb_request req
;
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
;
126 if (pmu_request(&req
, NULL
, 3, PMU_READ_NVRAM
,
127 (offset
>> 8) & 0xFF, offset
& 0xFF) < 0)
129 while (!req
.complete
)
134 static void pmu_write_pram(int offset
, __u8 data
)
136 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
)
144 #define pmu_read_time() 0
145 #define pmu_write_time(n)
146 #define pmu_read_pram NULL
147 #define pmu_write_pram NULL
150 #if 0 /* def CONFIG_ADB_MACIISI */
151 extern int maciisi_request(struct adb_request
*req
,
152 void (*done
)(struct adb_request
*), int nbytes
, ...);
154 static long maciisi_read_time(void)
156 struct adb_request req
;
159 if (maciisi_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_GET_TIME
))
162 time
= (req
.reply
[3] << 24) | (req
.reply
[4] << 16)
163 | (req
.reply
[5] << 8) | req
.reply
[6];
164 return time
- RTC_OFFSET
;
167 static void maciisi_write_time(long data
)
169 struct adb_request req
;
171 maciisi_request(&req
, NULL
, 6, CUDA_PACKET
, CUDA_SET_TIME
,
172 (data
>> 24) & 0xFF, (data
>> 16) & 0xFF,
173 (data
>> 8) & 0xFF, data
& 0xFF);
176 static __u8
maciisi_read_pram(int offset
)
178 struct adb_request req
;
179 if (maciisi_request(&req
, NULL
, 4, CUDA_PACKET
, CUDA_GET_PRAM
,
180 (offset
>> 8) & 0xFF, offset
& 0xFF))
185 static void maciisi_write_pram(int offset
, __u8 data
)
187 struct adb_request req
;
188 maciisi_request(&req
, NULL
, 5, CUDA_PACKET
, CUDA_SET_PRAM
,
189 (offset
>> 8) & 0xFF, offset
& 0xFF, data
);
192 #define maciisi_read_time() 0
193 #define maciisi_write_time(n)
194 #define maciisi_read_pram NULL
195 #define maciisi_write_pram NULL
199 * VIA PRAM/RTC access routines
201 * Must be called with interrupts disabled and
202 * the RTC should be enabled.
205 static __u8
via_pram_readbyte(void)
210 reg
= via1
[vBufB
] & ~VIA1B_vRTCClk
;
212 /* Set the RTC data line to be an input. */
214 via1
[vDirB
] &= ~VIA1B_vRTCData
;
216 /* The bits of the byte come out in MSB order */
219 for (i
= 0 ; i
< 8 ; i
++) {
221 via1
[vBufB
] = reg
| VIA1B_vRTCClk
;
222 data
= (data
<< 1) | (via1
[vBufB
] & VIA1B_vRTCData
);
225 /* Return RTC data line to output state */
227 via1
[vDirB
] |= VIA1B_vRTCData
;
232 static void via_pram_writebyte(__u8 data
)
236 reg
= via1
[vBufB
] & ~(VIA1B_vRTCClk
| VIA1B_vRTCData
);
238 /* The bits of the byte go in in MSB order */
240 for (i
= 0 ; i
< 8 ; i
++) {
241 bit
= data
& 0x80? 1 : 0;
243 via1
[vBufB
] = reg
| bit
;
244 via1
[vBufB
] = reg
| bit
| VIA1B_vRTCClk
;
249 * Execute a VIA PRAM/RTC command. For read commands
250 * data should point to a one-byte buffer for the
251 * resulting data. For write commands it should point
252 * to the data byte to for the command.
254 * This function disables all interrupts while running.
257 static void via_pram_command(int command
, __u8
*data
)
262 local_irq_save(flags
);
264 /* Enable the RTC and make sure the strobe line is high */
266 via1
[vBufB
] = (via1
[vBufB
] | VIA1B_vRTCClk
) & ~VIA1B_vRTCEnb
;
268 if (command
& 0xFF00) { /* extended (two-byte) command */
269 via_pram_writebyte((command
& 0xFF00) >> 8);
270 via_pram_writebyte(command
& 0xFF);
271 is_read
= command
& 0x8000;
272 } else { /* one-byte command */
273 via_pram_writebyte(command
);
274 is_read
= command
& 0x80;
277 *data
= via_pram_readbyte();
279 via_pram_writebyte(*data
);
282 /* All done, disable the RTC */
284 via1
[vBufB
] |= VIA1B_vRTCEnb
;
286 local_irq_restore(flags
);
289 static __u8
via_read_pram(int offset
)
294 static void via_write_pram(int offset
, __u8 data
)
299 * Return the current time in seconds since January 1, 1904.
301 * This only works on machines with the VIA-based PRAM/RTC, which
302 * is basically any machine with Mac II-style ADB.
305 static long via_read_time(void)
310 } result
, last_result
;
314 * The NetBSD guys say to loop until you get the same reading
321 printk("via_read_time: couldn't get valid time, "
322 "last read = 0x%08lx and 0x%08lx\n",
323 last_result
.idata
, result
.idata
);
327 last_result
.idata
= result
.idata
;
330 via_pram_command(0x81, &result
.cdata
[3]);
331 via_pram_command(0x85, &result
.cdata
[2]);
332 via_pram_command(0x89, &result
.cdata
[1]);
333 via_pram_command(0x8D, &result
.cdata
[0]);
334 } while (result
.idata
!= last_result
.idata
);
336 return result
.idata
- RTC_OFFSET
;
340 * Set the current time to a number of seconds since January 1, 1904.
342 * This only works on machines with the VIA-based PRAM/RTC, which
343 * is basically any machine with Mac II-style ADB.
346 static void via_write_time(long time
)
354 /* Clear the write protect bit */
357 via_pram_command(0x35, &temp
);
359 data
.idata
= time
+ RTC_OFFSET
;
360 via_pram_command(0x01, &data
.cdata
[3]);
361 via_pram_command(0x05, &data
.cdata
[2]);
362 via_pram_command(0x09, &data
.cdata
[1]);
363 via_pram_command(0x0D, &data
.cdata
[0]);
365 /* Set the write protect bit */
368 via_pram_command(0x35, &temp
);
371 static void via_shutdown(void)
374 via2
[rBufB
] &= ~0x04;
376 /* Direction of vDirB is output */
378 /* Send a value of 0 on that line */
379 via2
[vBufB
] &= ~0x04;
385 * FIXME: not sure how this is supposed to work exactly...
388 static void oss_shutdown(void)
390 oss
->rom_ctrl
= OSS_POWEROFF
;
393 #ifdef CONFIG_ADB_CUDA
395 static void cuda_restart(void)
397 struct adb_request req
;
398 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_RESET_SYSTEM
) < 0)
400 while (!req
.complete
)
404 static void cuda_shutdown(void)
406 struct adb_request req
;
407 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_POWERDOWN
) < 0)
409 while (!req
.complete
)
413 #endif /* CONFIG_ADB_CUDA */
415 #ifdef CONFIG_ADB_PMU68K
417 void pmu_restart(void)
419 struct adb_request req
;
420 if (pmu_request(&req
, NULL
,
421 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|PMU_INT_TICK
) < 0)
423 while (!req
.complete
)
425 if (pmu_request(&req
, NULL
, 1, PMU_RESET
) < 0)
427 while (!req
.complete
)
431 void pmu_shutdown(void)
433 struct adb_request req
;
434 if (pmu_request(&req
, NULL
,
435 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|PMU_INT_TICK
) < 0)
437 while (!req
.complete
)
439 if (pmu_request(&req
, NULL
, 5, PMU_SHUTDOWN
, 'M', 'A', 'T', 'T') < 0)
441 while (!req
.complete
)
448 *-------------------------------------------------------------------
449 * Below this point are the generic routines; they'll dispatch to the
450 * correct routine for the hardware on which we're running.
451 *-------------------------------------------------------------------
454 void mac_pram_read(int offset
, __u8
*buffer
, int len
)
459 switch(macintosh_config
->adb_type
) {
461 func
= maciisi_read_pram
; break;
464 func
= pmu_read_pram
; break;
466 func
= cuda_read_pram
; break;
468 func
= via_read_pram
;
472 for (i
= 0 ; i
< len
; i
++) {
473 buffer
[i
] = (*func
)(offset
++);
477 void mac_pram_write(int offset
, __u8
*buffer
, int len
)
479 void (*func
)(int, __u8
);
482 switch(macintosh_config
->adb_type
) {
484 func
= maciisi_write_pram
; break;
487 func
= pmu_write_pram
; break;
489 func
= cuda_write_pram
; break;
491 func
= via_write_pram
;
495 for (i
= 0 ; i
< len
; i
++) {
496 (*func
)(offset
++, buffer
[i
]);
500 void mac_poweroff(void)
503 * MAC_ADB_IISI may need to be moved up here if it doesn't actually
504 * work using the ADB packet method. --David Kilzer
509 } else if (macintosh_config
->adb_type
== MAC_ADB_II
) {
511 #ifdef CONFIG_ADB_CUDA
512 } else if (macintosh_config
->adb_type
== MAC_ADB_CUDA
) {
515 #ifdef CONFIG_ADB_PMU68K
516 } else if (macintosh_config
->adb_type
== MAC_ADB_PB1
517 || macintosh_config
->adb_type
== MAC_ADB_PB2
) {
522 printk("It is now safe to turn off your Macintosh.\n");
528 if (macintosh_config
->adb_type
== MAC_ADB_II
) {
531 /* need ROMBASE in booter */
532 /* indeed, plus need to MAP THE ROM !! */
534 if (mac_bi_data
.rombase
== 0)
535 mac_bi_data
.rombase
= 0x40800000;
538 rom_reset
= (void *) (mac_bi_data
.rombase
+ 0xa);
540 if (macintosh_config
->ident
== MAC_MODEL_SE30
) {
542 * MSch: Machines known to crash on ROM reset ...
545 local_irq_save(flags
);
549 local_irq_restore(flags
);
551 #ifdef CONFIG_ADB_CUDA
552 } else if (macintosh_config
->adb_type
== MAC_ADB_CUDA
) {
555 #ifdef CONFIG_ADB_PMU68K
556 } else if (macintosh_config
->adb_type
== MAC_ADB_PB1
557 || macintosh_config
->adb_type
== MAC_ADB_PB2
) {
560 } else if (CPU_IS_030
) {
562 /* 030-specific reset routine. The idea is general, but the
563 * specific registers to reset are '030-specific. Until I
564 * have a non-030 machine, I can't test anything else.
565 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
568 unsigned long rombase
= 0x40000000;
570 /* make a 1-to-1 mapping, using the transparent tran. reg. */
571 unsigned long virt
= (unsigned long) mac_reset
;
572 unsigned long phys
= virt_to_phys(mac_reset
);
573 unsigned long addr
= (phys
&0xFF000000)|0x8777;
574 unsigned long offset
= phys
-virt
;
575 local_irq_disable(); /* lets not screw this up, ok? */
576 __asm__
__volatile__(".chip 68030\n\t"
580 /* Now jump to physical address so we can disable MMU */
581 __asm__
__volatile__(
583 "lea %/pc@(1f),%/a0\n\t"
584 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
587 "jmp %/a0@\n\t" /* jump into physical memory */
588 "0:.long 0\n\t" /* a constant zero. */
589 /* OK. Now reset everything and jump to reset vector. */
591 "lea %/pc@(0b),%/a0\n\t"
592 "pmove %/a0@, %/tc\n\t" /* disable mmu */
593 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
594 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
596 "movec %/a0, %/vbr\n\t" /* clear vector base register */
597 "movec %/a0, %/cacr\n\t" /* disable caches */
598 "movel #0x0808,%/a0\n\t"
599 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
600 "movew #0x2700,%/sr\n\t" /* set up status register */
601 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
602 "movec %/a0, %/isp\n\t"
603 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
604 "reset\n\t" /* reset external devices */
605 "jmp %/a0@\n\t" /* jump to the reset vector */
607 : : "r" (offset
), "a" (rombase
) : "a0");
610 /* should never get here */
612 printk ("Restart failed. Please restart manually.\n");
617 * This function translates seconds since 1970 into a proper date.
619 * Algorithm cribbed from glibc2.1, __offtime().
621 #define SECS_PER_MINUTE (60)
622 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
623 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
625 static void unmktime(unsigned long time
, long offset
,
626 int *yearp
, int *monp
, int *dayp
,
627 int *hourp
, int *minp
, int *secp
)
629 /* How many days come before each month (0-12). */
630 static const unsigned short int __mon_yday
[2][13] =
633 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
635 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
637 long int days
, rem
, y
, wday
, yday
;
638 const unsigned short int *ip
;
640 days
= time
/ SECS_PER_DAY
;
641 rem
= time
% SECS_PER_DAY
;
647 while (rem
>= SECS_PER_DAY
) {
651 *hourp
= rem
/ SECS_PER_HOUR
;
652 rem
%= SECS_PER_HOUR
;
653 *minp
= rem
/ SECS_PER_MINUTE
;
654 *secp
= rem
% SECS_PER_MINUTE
;
655 /* January 1, 1970 was a Thursday. */
656 wday
= (4 + days
) % 7; /* Day in the week. Not currently used */
657 if (wday
< 0) wday
+= 7;
660 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
661 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
662 #define __isleap(year) \
663 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
665 while (days
< 0 || days
>= (__isleap (y
) ? 366 : 365))
667 /* Guess a corrected year, assuming 365 days per year. */
668 long int yg
= y
+ days
/ 365 - (days
% 365 < 0);
670 /* Adjust DAYS and Y to match the guessed year. */
671 days
-= ((yg
- y
) * 365
672 + LEAPS_THRU_END_OF (yg
- 1)
673 - LEAPS_THRU_END_OF (y
- 1));
677 yday
= days
; /* day in the year. Not currently used. */
678 ip
= __mon_yday
[__isleap(y
)];
679 for (y
= 11; days
< (long int) ip
[y
]; --y
)
683 *dayp
= days
+ 1; /* day in the month */
688 * Read/write the hardware clock.
691 int mac_hwclk(int op
, struct rtc_time
*t
)
695 if (!op
) { /* read */
696 switch (macintosh_config
->adb_type
) {
699 now
= via_read_time();
702 now
= maciisi_read_time();
706 now
= pmu_read_time();
709 now
= cuda_read_time();
717 &t
->tm_year
, &t
->tm_mon
, &t
->tm_mday
,
718 &t
->tm_hour
, &t
->tm_min
, &t
->tm_sec
);
720 printk("mac_hwclk: read %04d-%02d-%-2d %02d:%02d:%02d\n",
721 t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
722 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
726 printk("mac_hwclk: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
727 t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
728 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
731 now
= mktime(t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
732 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
734 switch (macintosh_config
->adb_type
) {
740 cuda_write_time(now
);
747 maciisi_write_time(now
);
754 * Set minutes/seconds in the hardware clock
757 int mac_set_clock_mmss (unsigned long nowtime
)
762 now
.tm_sec
= nowtime
% 60;
763 now
.tm_min
= (nowtime
/ 60) % 60;