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/system.h>
23 #include <asm/segment.h>
24 #include <asm/setup.h>
25 #include <asm/macintosh.h>
26 #include <asm/mac_via.h>
27 #include <asm/mac_oss.h>
29 #define BOOTINFO_COMPAT_1_0
30 #include <asm/bootinfo.h>
31 #include <asm/machdep.h>
33 /* Offset between Unix time (1970-based) and Mac time (1904-based) */
35 #define RTC_OFFSET 2082844800
37 static void (*rom_reset
)(void);
39 #ifdef CONFIG_ADB_CUDA
40 static long cuda_read_time(void)
42 struct adb_request req
;
45 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_GET_TIME
) < 0)
50 time
= (req
.reply
[3] << 24) | (req
.reply
[4] << 16)
51 | (req
.reply
[5] << 8) | req
.reply
[6];
52 return time
- RTC_OFFSET
;
55 static void cuda_write_time(long data
)
57 struct adb_request req
;
59 if (cuda_request(&req
, NULL
, 6, CUDA_PACKET
, CUDA_SET_TIME
,
60 (data
>> 24) & 0xFF, (data
>> 16) & 0xFF,
61 (data
>> 8) & 0xFF, data
& 0xFF) < 0)
67 static __u8
cuda_read_pram(int offset
)
69 struct adb_request req
;
70 if (cuda_request(&req
, NULL
, 4, CUDA_PACKET
, CUDA_GET_PRAM
,
71 (offset
>> 8) & 0xFF, offset
& 0xFF) < 0)
78 static void cuda_write_pram(int offset
, __u8 data
)
80 struct adb_request req
;
81 if (cuda_request(&req
, NULL
, 5, CUDA_PACKET
, CUDA_SET_PRAM
,
82 (offset
>> 8) & 0xFF, offset
& 0xFF, data
) < 0)
88 #define cuda_read_time() 0
89 #define cuda_write_time(n)
90 #define cuda_read_pram NULL
91 #define cuda_write_pram NULL
94 #ifdef CONFIG_ADB_PMU68K
95 static long pmu_read_time(void)
97 struct adb_request req
;
100 if (pmu_request(&req
, NULL
, 1, PMU_READ_RTC
) < 0)
102 while (!req
.complete
)
105 time
= (req
.reply
[1] << 24) | (req
.reply
[2] << 16)
106 | (req
.reply
[3] << 8) | req
.reply
[4];
107 return time
- RTC_OFFSET
;
110 static void pmu_write_time(long data
)
112 struct adb_request req
;
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 while (!req
.complete
)
122 static __u8
pmu_read_pram(int offset
)
124 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
;
136 if (pmu_request(&req
, NULL
, 4, PMU_WRITE_NVRAM
,
137 (offset
>> 8) & 0xFF, offset
& 0xFF, data
) < 0)
139 while (!req
.complete
)
143 #define pmu_read_time() 0
144 #define pmu_write_time(n)
145 #define pmu_read_pram NULL
146 #define pmu_write_pram NULL
149 #if 0 /* def CONFIG_ADB_MACIISI */
150 extern int maciisi_request(struct adb_request
*req
,
151 void (*done
)(struct adb_request
*), int nbytes
, ...);
153 static long maciisi_read_time(void)
155 struct adb_request req
;
158 if (maciisi_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_GET_TIME
))
161 time
= (req
.reply
[3] << 24) | (req
.reply
[4] << 16)
162 | (req
.reply
[5] << 8) | req
.reply
[6];
163 return time
- RTC_OFFSET
;
166 static void maciisi_write_time(long data
)
168 struct adb_request req
;
170 maciisi_request(&req
, NULL
, 6, CUDA_PACKET
, CUDA_SET_TIME
,
171 (data
>> 24) & 0xFF, (data
>> 16) & 0xFF,
172 (data
>> 8) & 0xFF, data
& 0xFF);
175 static __u8
maciisi_read_pram(int offset
)
177 struct adb_request req
;
178 if (maciisi_request(&req
, NULL
, 4, CUDA_PACKET
, CUDA_GET_PRAM
,
179 (offset
>> 8) & 0xFF, offset
& 0xFF))
184 static void maciisi_write_pram(int offset
, __u8 data
)
186 struct adb_request req
;
187 maciisi_request(&req
, NULL
, 5, CUDA_PACKET
, CUDA_SET_PRAM
,
188 (offset
>> 8) & 0xFF, offset
& 0xFF, data
);
191 #define maciisi_read_time() 0
192 #define maciisi_write_time(n)
193 #define maciisi_read_pram NULL
194 #define maciisi_write_pram NULL
198 * VIA PRAM/RTC access routines
200 * Must be called with interrupts disabled and
201 * the RTC should be enabled.
204 static __u8
via_pram_readbyte(void)
209 reg
= via1
[vBufB
] & ~VIA1B_vRTCClk
;
211 /* Set the RTC data line to be an input. */
213 via1
[vDirB
] &= ~VIA1B_vRTCData
;
215 /* The bits of the byte come out in MSB order */
218 for (i
= 0 ; i
< 8 ; i
++) {
220 via1
[vBufB
] = reg
| VIA1B_vRTCClk
;
221 data
= (data
<< 1) | (via1
[vBufB
] & VIA1B_vRTCData
);
224 /* Return RTC data line to output state */
226 via1
[vDirB
] |= VIA1B_vRTCData
;
231 static void via_pram_writebyte(__u8 data
)
235 reg
= via1
[vBufB
] & ~(VIA1B_vRTCClk
| VIA1B_vRTCData
);
237 /* The bits of the byte go in in MSB order */
239 for (i
= 0 ; i
< 8 ; i
++) {
240 bit
= data
& 0x80? 1 : 0;
242 via1
[vBufB
] = reg
| bit
;
243 via1
[vBufB
] = reg
| bit
| VIA1B_vRTCClk
;
248 * Execute a VIA PRAM/RTC command. For read commands
249 * data should point to a one-byte buffer for the
250 * resulting data. For write commands it should point
251 * to the data byte to for the command.
253 * This function disables all interrupts while running.
256 static void via_pram_command(int command
, __u8
*data
)
261 local_irq_save(flags
);
263 /* Enable the RTC and make sure the strobe line is high */
265 via1
[vBufB
] = (via1
[vBufB
] | VIA1B_vRTCClk
) & ~VIA1B_vRTCEnb
;
267 if (command
& 0xFF00) { /* extended (two-byte) command */
268 via_pram_writebyte((command
& 0xFF00) >> 8);
269 via_pram_writebyte(command
& 0xFF);
270 is_read
= command
& 0x8000;
271 } else { /* one-byte command */
272 via_pram_writebyte(command
);
273 is_read
= command
& 0x80;
276 *data
= via_pram_readbyte();
278 via_pram_writebyte(*data
);
281 /* All done, disable the RTC */
283 via1
[vBufB
] |= VIA1B_vRTCEnb
;
285 local_irq_restore(flags
);
288 static __u8
via_read_pram(int offset
)
293 static void via_write_pram(int offset
, __u8 data
)
298 * Return the current time in seconds since January 1, 1904.
300 * This only works on machines with the VIA-based PRAM/RTC, which
301 * is basically any machine with Mac II-style ADB.
304 static long via_read_time(void)
309 } result
, last_result
;
313 * The NetBSD guys say to loop until you get the same reading
320 printk("via_read_time: couldn't get valid time, "
321 "last read = 0x%08lx and 0x%08lx\n",
322 last_result
.idata
, result
.idata
);
326 last_result
.idata
= result
.idata
;
329 via_pram_command(0x81, &result
.cdata
[3]);
330 via_pram_command(0x85, &result
.cdata
[2]);
331 via_pram_command(0x89, &result
.cdata
[1]);
332 via_pram_command(0x8D, &result
.cdata
[0]);
333 } while (result
.idata
!= last_result
.idata
);
335 return result
.idata
- RTC_OFFSET
;
339 * Set the current time to a number of seconds since January 1, 1904.
341 * This only works on machines with the VIA-based PRAM/RTC, which
342 * is basically any machine with Mac II-style ADB.
345 static void via_write_time(long time
)
353 /* Clear the write protect bit */
356 via_pram_command(0x35, &temp
);
358 data
.idata
= time
+ RTC_OFFSET
;
359 via_pram_command(0x01, &data
.cdata
[3]);
360 via_pram_command(0x05, &data
.cdata
[2]);
361 via_pram_command(0x09, &data
.cdata
[1]);
362 via_pram_command(0x0D, &data
.cdata
[0]);
364 /* Set the write protect bit */
367 via_pram_command(0x35, &temp
);
370 static void via_shutdown(void)
373 via2
[rBufB
] &= ~0x04;
375 /* Direction of vDirB is output */
377 /* Send a value of 0 on that line */
378 via2
[vBufB
] &= ~0x04;
384 * FIXME: not sure how this is supposed to work exactly...
387 static void oss_shutdown(void)
389 oss
->rom_ctrl
= OSS_POWEROFF
;
392 #ifdef CONFIG_ADB_CUDA
394 static void cuda_restart(void)
396 struct adb_request req
;
397 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_RESET_SYSTEM
) < 0)
399 while (!req
.complete
)
403 static void cuda_shutdown(void)
405 struct adb_request req
;
406 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_POWERDOWN
) < 0)
408 while (!req
.complete
)
412 #endif /* CONFIG_ADB_CUDA */
414 #ifdef CONFIG_ADB_PMU68K
416 void pmu_restart(void)
418 struct adb_request req
;
419 if (pmu_request(&req
, NULL
,
420 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|PMU_INT_TICK
) < 0)
422 while (!req
.complete
)
424 if (pmu_request(&req
, NULL
, 1, PMU_RESET
) < 0)
426 while (!req
.complete
)
430 void pmu_shutdown(void)
432 struct adb_request req
;
433 if (pmu_request(&req
, NULL
,
434 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|PMU_INT_TICK
) < 0)
436 while (!req
.complete
)
438 if (pmu_request(&req
, NULL
, 5, PMU_SHUTDOWN
, 'M', 'A', 'T', 'T') < 0)
440 while (!req
.complete
)
447 *-------------------------------------------------------------------
448 * Below this point are the generic routines; they'll dispatch to the
449 * correct routine for the hardware on which we're running.
450 *-------------------------------------------------------------------
453 void mac_pram_read(int offset
, __u8
*buffer
, int len
)
458 switch(macintosh_config
->adb_type
) {
460 func
= maciisi_read_pram
; break;
463 func
= pmu_read_pram
; break;
465 func
= cuda_read_pram
; break;
467 func
= via_read_pram
;
471 for (i
= 0 ; i
< len
; i
++) {
472 buffer
[i
] = (*func
)(offset
++);
476 void mac_pram_write(int offset
, __u8
*buffer
, int len
)
478 void (*func
)(int, __u8
);
481 switch(macintosh_config
->adb_type
) {
483 func
= maciisi_write_pram
; break;
486 func
= pmu_write_pram
; break;
488 func
= cuda_write_pram
; break;
490 func
= via_write_pram
;
494 for (i
= 0 ; i
< len
; i
++) {
495 (*func
)(offset
++, buffer
[i
]);
499 void mac_poweroff(void)
502 * MAC_ADB_IISI may need to be moved up here if it doesn't actually
503 * work using the ADB packet method. --David Kilzer
508 } else if (macintosh_config
->adb_type
== MAC_ADB_II
) {
510 #ifdef CONFIG_ADB_CUDA
511 } else if (macintosh_config
->adb_type
== MAC_ADB_CUDA
) {
514 #ifdef CONFIG_ADB_PMU68K
515 } else if (macintosh_config
->adb_type
== MAC_ADB_PB1
516 || macintosh_config
->adb_type
== MAC_ADB_PB2
) {
521 printk("It is now safe to turn off your Macintosh.\n");
527 if (macintosh_config
->adb_type
== MAC_ADB_II
) {
530 /* need ROMBASE in booter */
531 /* indeed, plus need to MAP THE ROM !! */
533 if (mac_bi_data
.rombase
== 0)
534 mac_bi_data
.rombase
= 0x40800000;
537 rom_reset
= (void *) (mac_bi_data
.rombase
+ 0xa);
539 if (macintosh_config
->ident
== MAC_MODEL_SE30
) {
541 * MSch: Machines known to crash on ROM reset ...
544 local_irq_save(flags
);
548 local_irq_restore(flags
);
550 #ifdef CONFIG_ADB_CUDA
551 } else if (macintosh_config
->adb_type
== MAC_ADB_CUDA
) {
554 #ifdef CONFIG_ADB_PMU68K
555 } else if (macintosh_config
->adb_type
== MAC_ADB_PB1
556 || macintosh_config
->adb_type
== MAC_ADB_PB2
) {
559 } else if (CPU_IS_030
) {
561 /* 030-specific reset routine. The idea is general, but the
562 * specific registers to reset are '030-specific. Until I
563 * have a non-030 machine, I can't test anything else.
564 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
567 unsigned long rombase
= 0x40000000;
569 /* make a 1-to-1 mapping, using the transparent tran. reg. */
570 unsigned long virt
= (unsigned long) mac_reset
;
571 unsigned long phys
= virt_to_phys(mac_reset
);
572 unsigned long addr
= (phys
&0xFF000000)|0x8777;
573 unsigned long offset
= phys
-virt
;
574 local_irq_disable(); /* lets not screw this up, ok? */
575 __asm__
__volatile__(".chip 68030\n\t"
579 /* Now jump to physical address so we can disable MMU */
580 __asm__
__volatile__(
582 "lea %/pc@(1f),%/a0\n\t"
583 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
586 "jmp %/a0@\n\t" /* jump into physical memory */
587 "0:.long 0\n\t" /* a constant zero. */
588 /* OK. Now reset everything and jump to reset vector. */
590 "lea %/pc@(0b),%/a0\n\t"
591 "pmove %/a0@, %/tc\n\t" /* disable mmu */
592 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
593 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
595 "movec %/a0, %/vbr\n\t" /* clear vector base register */
596 "movec %/a0, %/cacr\n\t" /* disable caches */
597 "movel #0x0808,%/a0\n\t"
598 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
599 "movew #0x2700,%/sr\n\t" /* set up status register */
600 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
601 "movec %/a0, %/isp\n\t"
602 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
603 "reset\n\t" /* reset external devices */
604 "jmp %/a0@\n\t" /* jump to the reset vector */
606 : : "r" (offset
), "a" (rombase
) : "a0");
609 /* should never get here */
611 printk ("Restart failed. Please restart manually.\n");
616 * This function translates seconds since 1970 into a proper date.
618 * Algorithm cribbed from glibc2.1, __offtime().
620 #define SECS_PER_MINUTE (60)
621 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
622 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
624 static void unmktime(unsigned long time
, long offset
,
625 int *yearp
, int *monp
, int *dayp
,
626 int *hourp
, int *minp
, int *secp
)
628 /* How many days come before each month (0-12). */
629 static const unsigned short int __mon_yday
[2][13] =
632 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
634 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
636 long int days
, rem
, y
, wday
, yday
;
637 const unsigned short int *ip
;
639 days
= time
/ SECS_PER_DAY
;
640 rem
= time
% SECS_PER_DAY
;
646 while (rem
>= SECS_PER_DAY
) {
650 *hourp
= rem
/ SECS_PER_HOUR
;
651 rem
%= SECS_PER_HOUR
;
652 *minp
= rem
/ SECS_PER_MINUTE
;
653 *secp
= rem
% SECS_PER_MINUTE
;
654 /* January 1, 1970 was a Thursday. */
655 wday
= (4 + days
) % 7; /* Day in the week. Not currently used */
656 if (wday
< 0) wday
+= 7;
659 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
660 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
661 #define __isleap(year) \
662 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
664 while (days
< 0 || days
>= (__isleap (y
) ? 366 : 365))
666 /* Guess a corrected year, assuming 365 days per year. */
667 long int yg
= y
+ days
/ 365 - (days
% 365 < 0);
669 /* Adjust DAYS and Y to match the guessed year. */
670 days
-= ((yg
- y
) * 365
671 + LEAPS_THRU_END_OF (yg
- 1)
672 - LEAPS_THRU_END_OF (y
- 1));
676 yday
= days
; /* day in the year. Not currently used. */
677 ip
= __mon_yday
[__isleap(y
)];
678 for (y
= 11; days
< (long int) ip
[y
]; --y
)
682 *dayp
= days
+ 1; /* day in the month */
687 * Read/write the hardware clock.
690 int mac_hwclk(int op
, struct rtc_time
*t
)
694 if (!op
) { /* read */
695 switch (macintosh_config
->adb_type
) {
698 now
= via_read_time();
701 now
= maciisi_read_time();
705 now
= pmu_read_time();
708 now
= cuda_read_time();
716 &t
->tm_year
, &t
->tm_mon
, &t
->tm_mday
,
717 &t
->tm_hour
, &t
->tm_min
, &t
->tm_sec
);
719 printk("mac_hwclk: read %04d-%02d-%-2d %02d:%02d:%02d\n",
720 t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
721 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
725 printk("mac_hwclk: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
726 t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
727 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
730 now
= mktime(t
->tm_year
+ 1900, t
->tm_mon
+ 1, t
->tm_mday
,
731 t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
733 switch (macintosh_config
->adb_type
) {
739 cuda_write_time(now
);
746 maciisi_write_time(now
);
753 * Set minutes/seconds in the hardware clock
756 int mac_set_clock_mmss (unsigned long nowtime
)
761 now
.tm_sec
= nowtime
% 60;
762 now
.tm_min
= (nowtime
/ 60) % 60;