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 extern struct mac_booter_data mac_bi_data
;
39 static void (*rom_reset
)(void);
41 #ifdef CONFIG_ADB_CUDA
42 static long cuda_read_time(void)
44 struct adb_request req
;
47 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_GET_TIME
) < 0)
52 time
= (req
.reply
[3] << 24) | (req
.reply
[4] << 16)
53 | (req
.reply
[5] << 8) | req
.reply
[6];
54 return time
- RTC_OFFSET
;
57 static void cuda_write_time(long data
)
59 struct adb_request req
;
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
;
72 if (cuda_request(&req
, NULL
, 4, CUDA_PACKET
, CUDA_GET_PRAM
,
73 (offset
>> 8) & 0xFF, offset
& 0xFF) < 0)
80 static void cuda_write_pram(int offset
, __u8 data
)
82 struct adb_request req
;
83 if (cuda_request(&req
, NULL
, 5, CUDA_PACKET
, CUDA_SET_PRAM
,
84 (offset
>> 8) & 0xFF, offset
& 0xFF, data
) < 0)
90 #define cuda_read_time() 0
91 #define cuda_write_time(n)
92 #define cuda_read_pram NULL
93 #define cuda_write_pram NULL
96 #ifdef CONFIG_ADB_PMU68K
97 static long pmu_read_time(void)
99 struct adb_request req
;
102 if (pmu_request(&req
, NULL
, 1, PMU_READ_RTC
) < 0)
104 while (!req
.complete
)
107 time
= (req
.reply
[0] << 24) | (req
.reply
[1] << 16)
108 | (req
.reply
[2] << 8) | req
.reply
[3];
109 return time
- RTC_OFFSET
;
112 static void pmu_write_time(long data
)
114 struct adb_request req
;
116 if (pmu_request(&req
, NULL
, 5, PMU_SET_RTC
,
117 (data
>> 24) & 0xFF, (data
>> 16) & 0xFF,
118 (data
>> 8) & 0xFF, data
& 0xFF) < 0)
120 while (!req
.complete
)
124 static __u8
pmu_read_pram(int offset
)
126 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
;
138 if (pmu_request(&req
, NULL
, 4, PMU_WRITE_NVRAM
,
139 (offset
>> 8) & 0xFF, offset
& 0xFF, data
) < 0)
141 while (!req
.complete
)
145 #define pmu_read_time() 0
146 #define pmu_write_time(n)
147 #define pmu_read_pram NULL
148 #define pmu_write_pram NULL
151 #ifdef CONFIG_ADB_MACIISI
152 extern int maciisi_request(struct adb_request
*req
,
153 void (*done
)(struct adb_request
*), int nbytes
, ...);
155 static long maciisi_read_time(void)
157 struct adb_request req
;
160 if (maciisi_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_GET_TIME
))
163 time
= (req
.reply
[3] << 24) | (req
.reply
[4] << 16)
164 | (req
.reply
[5] << 8) | req
.reply
[6];
165 return time
- RTC_OFFSET
;
168 static void maciisi_write_time(long data
)
170 struct adb_request req
;
172 maciisi_request(&req
, NULL
, 6, CUDA_PACKET
, CUDA_SET_TIME
,
173 (data
>> 24) & 0xFF, (data
>> 16) & 0xFF,
174 (data
>> 8) & 0xFF, data
& 0xFF);
177 static __u8
maciisi_read_pram(int offset
)
179 struct adb_request req
;
180 if (maciisi_request(&req
, NULL
, 4, CUDA_PACKET
, CUDA_GET_PRAM
,
181 (offset
>> 8) & 0xFF, offset
& 0xFF))
186 static void maciisi_write_pram(int offset
, __u8 data
)
188 struct adb_request req
;
189 maciisi_request(&req
, NULL
, 5, CUDA_PACKET
, CUDA_SET_PRAM
,
190 (offset
>> 8) & 0xFF, offset
& 0xFF, data
);
193 #define maciisi_read_time() 0
194 #define maciisi_write_time(n)
195 #define maciisi_read_pram NULL
196 #define maciisi_write_pram NULL
200 * VIA PRAM/RTC access routines
202 * Must be called with interrupts disabled and
203 * the RTC should be enabled.
206 static __u8
via_pram_readbyte(void)
211 reg
= via1
[vBufB
] & ~VIA1B_vRTCClk
;
213 /* Set the RTC data line to be an input. */
215 via1
[vDirB
] &= ~VIA1B_vRTCData
;
217 /* The bits of the byte come out in MSB order */
220 for (i
= 0 ; i
< 8 ; i
++) {
222 via1
[vBufB
] = reg
| VIA1B_vRTCClk
;
223 data
= (data
<< 1) | (via1
[vBufB
] & VIA1B_vRTCData
);
226 /* Return RTC data line to output state */
228 via1
[vDirB
] |= VIA1B_vRTCData
;
233 static void via_pram_writebyte(__u8 data
)
237 reg
= via1
[vBufB
] & ~(VIA1B_vRTCClk
| VIA1B_vRTCData
);
239 /* The bits of the byte go in in MSB order */
241 for (i
= 0 ; i
< 8 ; i
++) {
242 bit
= data
& 0x80? 1 : 0;
244 via1
[vBufB
] = reg
| bit
;
245 via1
[vBufB
] = reg
| bit
| VIA1B_vRTCClk
;
250 * Execute a VIA PRAM/RTC command. For read commands
251 * data should point to a one-byte buffer for the
252 * resulting data. For write commands it should point
253 * to the data byte to for the command.
255 * This function disables all interrupts while running.
258 static void via_pram_command(int command
, __u8
*data
)
263 local_irq_save(flags
);
265 /* Enable the RTC and make sure the strobe line is high */
267 via1
[vBufB
] = (via1
[vBufB
] | VIA1B_vRTCClk
) & ~VIA1B_vRTCEnb
;
269 if (command
& 0xFF00) { /* extended (two-byte) command */
270 via_pram_writebyte((command
& 0xFF00) >> 8);
271 via_pram_writebyte(command
& 0xFF);
272 is_read
= command
& 0x8000;
273 } else { /* one-byte command */
274 via_pram_writebyte(command
);
275 is_read
= command
& 0x80;
278 *data
= via_pram_readbyte();
280 via_pram_writebyte(*data
);
283 /* All done, disable the RTC */
285 via1
[vBufB
] |= VIA1B_vRTCEnb
;
287 local_irq_restore(flags
);
290 static __u8
via_read_pram(int offset
)
295 static void via_write_pram(int offset
, __u8 data
)
300 * Return the current time in seconds since January 1, 1904.
302 * This only works on machines with the VIA-based PRAM/RTC, which
303 * is basically any machine with Mac II-style ADB.
306 static long via_read_time(void)
311 } result
, last_result
;
315 * The NetBSD guys say to loop until you get the same reading
322 printk("via_read_time: couldn't get valid time, "
323 "last read = 0x%08lx and 0x%08lx\n",
324 last_result
.idata
, result
.idata
);
328 last_result
.idata
= result
.idata
;
331 via_pram_command(0x81, &result
.cdata
[3]);
332 via_pram_command(0x85, &result
.cdata
[2]);
333 via_pram_command(0x89, &result
.cdata
[1]);
334 via_pram_command(0x8D, &result
.cdata
[0]);
335 } while (result
.idata
!= last_result
.idata
);
337 return result
.idata
- RTC_OFFSET
;
341 * Set the current time to a number of seconds since January 1, 1904.
343 * This only works on machines with the VIA-based PRAM/RTC, which
344 * is basically any machine with Mac II-style ADB.
347 static void via_write_time(long time
)
355 /* Clear the write protect bit */
358 via_pram_command(0x35, &temp
);
360 data
.idata
= time
+ RTC_OFFSET
;
361 via_pram_command(0x01, &data
.cdata
[3]);
362 via_pram_command(0x05, &data
.cdata
[2]);
363 via_pram_command(0x09, &data
.cdata
[1]);
364 via_pram_command(0x0D, &data
.cdata
[0]);
366 /* Set the write protect bit */
369 via_pram_command(0x35, &temp
);
372 static void via_shutdown(void)
375 via2
[rBufB
] &= ~0x04;
377 /* Direction of vDirB is output */
379 /* Send a value of 0 on that line */
380 via2
[vBufB
] &= ~0x04;
386 * FIXME: not sure how this is supposed to work exactly...
389 static void oss_shutdown(void)
391 oss
->rom_ctrl
= OSS_POWEROFF
;
394 #ifdef CONFIG_ADB_CUDA
396 static void cuda_restart(void)
398 struct adb_request req
;
399 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_RESET_SYSTEM
) < 0)
401 while (!req
.complete
)
405 static void cuda_shutdown(void)
407 struct adb_request req
;
408 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_POWERDOWN
) < 0)
410 while (!req
.complete
)
414 #endif /* CONFIG_ADB_CUDA */
416 #ifdef CONFIG_ADB_PMU68K
418 void pmu_restart(void)
420 struct adb_request req
;
421 if (pmu_request(&req
, NULL
,
422 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|PMU_INT_TICK
) < 0)
424 while (!req
.complete
)
426 if (pmu_request(&req
, NULL
, 1, PMU_RESET
) < 0)
428 while (!req
.complete
)
432 void pmu_shutdown(void)
434 struct adb_request req
;
435 if (pmu_request(&req
, NULL
,
436 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|PMU_INT_TICK
) < 0)
438 while (!req
.complete
)
440 if (pmu_request(&req
, NULL
, 5, PMU_SHUTDOWN
, 'M', 'A', 'T', 'T') < 0)
442 while (!req
.complete
)
449 *-------------------------------------------------------------------
450 * Below this point are the generic routines; they'll dispatch to the
451 * correct routine for the hardware on which we're running.
452 *-------------------------------------------------------------------
455 void mac_pram_read(int offset
, __u8
*buffer
, int len
)
460 switch(macintosh_config
->adb_type
) {
462 func
= maciisi_read_pram
; break;
465 func
= pmu_read_pram
; break;
467 func
= cuda_read_pram
; break;
469 func
= via_read_pram
;
473 for (i
= 0 ; i
< len
; i
++) {
474 buffer
[i
] = (*func
)(offset
++);
478 void mac_pram_write(int offset
, __u8
*buffer
, int len
)
480 void (*func
)(int, __u8
);
483 switch(macintosh_config
->adb_type
) {
485 func
= maciisi_write_pram
; break;
488 func
= pmu_write_pram
; break;
490 func
= cuda_write_pram
; break;
492 func
= via_write_pram
;
496 for (i
= 0 ; i
< len
; i
++) {
497 (*func
)(offset
++, buffer
[i
]);
501 void mac_poweroff(void)
504 * MAC_ADB_IISI may need to be moved up here if it doesn't actually
505 * work using the ADB packet method. --David Kilzer
510 } else if (macintosh_config
->adb_type
== MAC_ADB_II
) {
512 #ifdef CONFIG_ADB_CUDA
513 } else if (macintosh_config
->adb_type
== MAC_ADB_CUDA
) {
516 #ifdef CONFIG_ADB_PMU68K
517 } else if (macintosh_config
->adb_type
== MAC_ADB_PB1
518 || macintosh_config
->adb_type
== MAC_ADB_PB2
) {
523 printk("It is now safe to turn off your Macintosh.\n");
529 if (macintosh_config
->adb_type
== MAC_ADB_II
) {
532 /* need ROMBASE in booter */
533 /* indeed, plus need to MAP THE ROM !! */
535 if (mac_bi_data
.rombase
== 0)
536 mac_bi_data
.rombase
= 0x40800000;
539 rom_reset
= (void *) (mac_bi_data
.rombase
+ 0xa);
541 if (macintosh_config
->ident
== MAC_MODEL_SE30
) {
543 * MSch: Machines known to crash on ROM reset ...
546 local_irq_save(flags
);
550 local_irq_restore(flags
);
552 #ifdef CONFIG_ADB_CUDA
553 } else if (macintosh_config
->adb_type
== MAC_ADB_CUDA
) {
556 #ifdef CONFIG_ADB_PMU68K
557 } else if (macintosh_config
->adb_type
== MAC_ADB_PB1
558 || macintosh_config
->adb_type
== MAC_ADB_PB2
) {
561 } else if (CPU_IS_030
) {
563 /* 030-specific reset routine. The idea is general, but the
564 * specific registers to reset are '030-specific. Until I
565 * have a non-030 machine, I can't test anything else.
566 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
569 unsigned long rombase
= 0x40000000;
571 /* make a 1-to-1 mapping, using the transparent tran. reg. */
572 unsigned long virt
= (unsigned long) mac_reset
;
573 unsigned long phys
= virt_to_phys(mac_reset
);
574 unsigned long addr
= (phys
&0xFF000000)|0x8777;
575 unsigned long offset
= phys
-virt
;
576 local_irq_disable(); /* lets not screw this up, ok? */
577 __asm__
__volatile__(".chip 68030\n\t"
581 /* Now jump to physical address so we can disable MMU */
582 __asm__
__volatile__(
584 "lea %/pc@(1f),%/a0\n\t"
585 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
588 "jmp %/a0@\n\t" /* jump into physical memory */
589 "0:.long 0\n\t" /* a constant zero. */
590 /* OK. Now reset everything and jump to reset vector. */
592 "lea %/pc@(0b),%/a0\n\t"
593 "pmove %/a0@, %/tc\n\t" /* disable mmu */
594 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
595 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
597 "movec %/a0, %/vbr\n\t" /* clear vector base register */
598 "movec %/a0, %/cacr\n\t" /* disable caches */
599 "movel #0x0808,%/a0\n\t"
600 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
601 "movew #0x2700,%/sr\n\t" /* set up status register */
602 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
603 "movec %/a0, %/isp\n\t"
604 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
605 "reset\n\t" /* reset external devices */
606 "jmp %/a0@\n\t" /* jump to the reset vector */
608 : : "r" (offset
), "a" (rombase
) : "a0");
611 /* should never get here */
613 printk ("Restart failed. Please restart manually.\n");
618 * This function translates seconds since 1970 into a proper date.
620 * Algorithm cribbed from glibc2.1, __offtime().
622 #define SECS_PER_MINUTE (60)
623 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
624 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
626 static void unmktime(unsigned long time
, long offset
,
627 int *yearp
, int *monp
, int *dayp
,
628 int *hourp
, int *minp
, int *secp
)
630 /* How many days come before each month (0-12). */
631 static const unsigned short int __mon_yday
[2][13] =
634 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
636 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
638 long int days
, rem
, y
, wday
, yday
;
639 const unsigned short int *ip
;
641 days
= time
/ SECS_PER_DAY
;
642 rem
= time
% SECS_PER_DAY
;
648 while (rem
>= SECS_PER_DAY
) {
652 *hourp
= rem
/ SECS_PER_HOUR
;
653 rem
%= SECS_PER_HOUR
;
654 *minp
= rem
/ SECS_PER_MINUTE
;
655 *secp
= rem
% SECS_PER_MINUTE
;
656 /* January 1, 1970 was a Thursday. */
657 wday
= (4 + days
) % 7; /* Day in the week. Not currently used */
658 if (wday
< 0) wday
+= 7;
661 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
662 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
663 #define __isleap(year) \
664 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
666 while (days
< 0 || days
>= (__isleap (y
) ? 366 : 365))
668 /* Guess a corrected year, assuming 365 days per year. */
669 long int yg
= y
+ days
/ 365 - (days
% 365 < 0);
671 /* Adjust DAYS and Y to match the guessed year. */
672 days
-= ((yg
- y
) * 365
673 + LEAPS_THRU_END_OF (yg
- 1)
674 - LEAPS_THRU_END_OF (y
- 1));
678 yday
= days
; /* day in the year. Not currently used. */
679 ip
= __mon_yday
[__isleap(y
)];
680 for (y
= 11; days
< (long int) ip
[y
]; --y
)
684 *dayp
= days
+ 1; /* day in the month */
689 * Read/write the hardware clock.
692 int mac_hwclk(int op
, struct rtc_time
*t
)
696 if (!op
) { /* read */
697 switch (macintosh_config
->adb_type
) {
700 now
= via_read_time();
703 now
= maciisi_read_time();
707 now
= pmu_read_time();
710 now
= cuda_read_time();
718 &t
->tm_year
, &t
->tm_mon
, &t
->tm_mday
,
719 &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
, t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
723 printk("mac_hwclk: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
724 t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
, t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
726 #if 0 /* it trashes my rtc */
727 now
= mktime(t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
728 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
730 switch (macintosh_config
->adb_type
) {
736 cuda_write_time(now
);
743 maciisi_write_time(now
);
751 * Set minutes/seconds in the hardware clock
754 int mac_set_clock_mmss (unsigned long nowtime
)
759 now
.tm_sec
= nowtime
% 60;
760 now
.tm_min
= (nowtime
/ 60) % 60;