GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / m68k / mac / misc.c
blobd09950519a050b6218ba0ef1b3bd4cdc2c6ce4eb
1 /*
2 * Miscellaneous Mac68K-specific stuff
3 */
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>
13 #include <linux/mm.h>
15 #include <linux/adb.h>
16 #include <linux/cuda.h>
17 #include <linux/pmu.h>
19 #include <asm/uaccess.h>
20 #include <asm/io.h>
21 #include <asm/rtc.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;
43 long time;
45 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
46 return 0;
47 while (!req.complete)
48 cuda_poll();
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;
58 data += RTC_OFFSET;
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)
62 return;
63 while (!req.complete)
64 cuda_poll();
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)
72 return 0;
73 while (!req.complete)
74 cuda_poll();
75 return req.reply[3];
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)
83 return;
84 while (!req.complete)
85 cuda_poll();
87 #else
88 #define cuda_read_time() 0
89 #define cuda_write_time(n)
90 #define cuda_read_pram NULL
91 #define cuda_write_pram NULL
92 #endif
94 #ifdef CONFIG_ADB_PMU68K
95 static long pmu_read_time(void)
97 struct adb_request req;
98 long time;
100 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
101 return 0;
102 while (!req.complete)
103 pmu_poll();
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;
113 data += RTC_OFFSET;
114 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
115 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
116 (data >> 8) & 0xFF, data & 0xFF) < 0)
117 return;
118 while (!req.complete)
119 pmu_poll();
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)
127 return 0;
128 while (!req.complete)
129 pmu_poll();
130 return req.reply[3];
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)
138 return;
139 while (!req.complete)
140 pmu_poll();
142 #else
143 #define pmu_read_time() 0
144 #define pmu_write_time(n)
145 #define pmu_read_pram NULL
146 #define pmu_write_pram NULL
147 #endif
149 #define maciisi_read_time() 0
150 #define maciisi_write_time(n)
151 #define maciisi_read_pram NULL
152 #define maciisi_write_pram NULL
155 * VIA PRAM/RTC access routines
157 * Must be called with interrupts disabled and
158 * the RTC should be enabled.
161 static __u8 via_pram_readbyte(void)
163 int i,reg;
164 __u8 data;
166 reg = via1[vBufB] & ~VIA1B_vRTCClk;
168 /* Set the RTC data line to be an input. */
170 via1[vDirB] &= ~VIA1B_vRTCData;
172 /* The bits of the byte come out in MSB order */
174 data = 0;
175 for (i = 0 ; i < 8 ; i++) {
176 via1[vBufB] = reg;
177 via1[vBufB] = reg | VIA1B_vRTCClk;
178 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
181 /* Return RTC data line to output state */
183 via1[vDirB] |= VIA1B_vRTCData;
185 return data;
188 static void via_pram_writebyte(__u8 data)
190 int i,reg,bit;
192 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
194 /* The bits of the byte go in in MSB order */
196 for (i = 0 ; i < 8 ; i++) {
197 bit = data & 0x80? 1 : 0;
198 data <<= 1;
199 via1[vBufB] = reg | bit;
200 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
205 * Execute a VIA PRAM/RTC command. For read commands
206 * data should point to a one-byte buffer for the
207 * resulting data. For write commands it should point
208 * to the data byte to for the command.
210 * This function disables all interrupts while running.
213 static void via_pram_command(int command, __u8 *data)
215 unsigned long flags;
216 int is_read;
218 local_irq_save(flags);
220 /* Enable the RTC and make sure the strobe line is high */
222 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
224 if (command & 0xFF00) { /* extended (two-byte) command */
225 via_pram_writebyte((command & 0xFF00) >> 8);
226 via_pram_writebyte(command & 0xFF);
227 is_read = command & 0x8000;
228 } else { /* one-byte command */
229 via_pram_writebyte(command);
230 is_read = command & 0x80;
232 if (is_read) {
233 *data = via_pram_readbyte();
234 } else {
235 via_pram_writebyte(*data);
238 /* All done, disable the RTC */
240 via1[vBufB] |= VIA1B_vRTCEnb;
242 local_irq_restore(flags);
245 static __u8 via_read_pram(int offset)
247 return 0;
250 static void via_write_pram(int offset, __u8 data)
255 * Return the current time in seconds since January 1, 1904.
257 * This only works on machines with the VIA-based PRAM/RTC, which
258 * is basically any machine with Mac II-style ADB.
261 static long via_read_time(void)
263 union {
264 __u8 cdata[4];
265 long idata;
266 } result, last_result;
267 int ct;
270 * The NetBSD guys say to loop until you get the same reading
271 * twice in a row.
274 ct = 0;
275 do {
276 if (++ct > 10) {
277 printk("via_read_time: couldn't get valid time, "
278 "last read = 0x%08lx and 0x%08lx\n",
279 last_result.idata, result.idata);
280 break;
283 last_result.idata = result.idata;
284 result.idata = 0;
286 via_pram_command(0x81, &result.cdata[3]);
287 via_pram_command(0x85, &result.cdata[2]);
288 via_pram_command(0x89, &result.cdata[1]);
289 via_pram_command(0x8D, &result.cdata[0]);
290 } while (result.idata != last_result.idata);
292 return result.idata - RTC_OFFSET;
296 * Set the current time to a number of seconds since January 1, 1904.
298 * This only works on machines with the VIA-based PRAM/RTC, which
299 * is basically any machine with Mac II-style ADB.
302 static void via_write_time(long time)
304 union {
305 __u8 cdata[4];
306 long idata;
307 } data;
308 __u8 temp;
310 /* Clear the write protect bit */
312 temp = 0x55;
313 via_pram_command(0x35, &temp);
315 data.idata = time + RTC_OFFSET;
316 via_pram_command(0x01, &data.cdata[3]);
317 via_pram_command(0x05, &data.cdata[2]);
318 via_pram_command(0x09, &data.cdata[1]);
319 via_pram_command(0x0D, &data.cdata[0]);
321 /* Set the write protect bit */
323 temp = 0xD5;
324 via_pram_command(0x35, &temp);
327 static void via_shutdown(void)
329 if (rbv_present) {
330 via2[rBufB] &= ~0x04;
331 } else {
332 /* Direction of vDirB is output */
333 via2[vDirB] |= 0x04;
334 /* Send a value of 0 on that line */
335 via2[vBufB] &= ~0x04;
336 mdelay(1000);
341 static void oss_shutdown(void)
343 oss->rom_ctrl = OSS_POWEROFF;
346 #ifdef CONFIG_ADB_CUDA
348 static void cuda_restart(void)
350 struct adb_request req;
351 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
352 return;
353 while (!req.complete)
354 cuda_poll();
357 static void cuda_shutdown(void)
359 struct adb_request req;
360 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
361 return;
362 while (!req.complete)
363 cuda_poll();
366 #endif /* CONFIG_ADB_CUDA */
368 #ifdef CONFIG_ADB_PMU68K
370 void pmu_restart(void)
372 struct adb_request req;
373 if (pmu_request(&req, NULL,
374 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
375 return;
376 while (!req.complete)
377 pmu_poll();
378 if (pmu_request(&req, NULL, 1, PMU_RESET) < 0)
379 return;
380 while (!req.complete)
381 pmu_poll();
384 void pmu_shutdown(void)
386 struct adb_request req;
387 if (pmu_request(&req, NULL,
388 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
389 return;
390 while (!req.complete)
391 pmu_poll();
392 if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0)
393 return;
394 while (!req.complete)
395 pmu_poll();
398 #endif
401 *-------------------------------------------------------------------
402 * Below this point are the generic routines; they'll dispatch to the
403 * correct routine for the hardware on which we're running.
404 *-------------------------------------------------------------------
407 void mac_pram_read(int offset, __u8 *buffer, int len)
409 __u8 (*func)(int);
410 int i;
412 switch(macintosh_config->adb_type) {
413 case MAC_ADB_IISI:
414 func = maciisi_read_pram; break;
415 case MAC_ADB_PB1:
416 case MAC_ADB_PB2:
417 func = pmu_read_pram; break;
418 case MAC_ADB_CUDA:
419 func = cuda_read_pram; break;
420 default:
421 func = via_read_pram;
423 if (!func)
424 return;
425 for (i = 0 ; i < len ; i++) {
426 buffer[i] = (*func)(offset++);
430 void mac_pram_write(int offset, __u8 *buffer, int len)
432 void (*func)(int, __u8);
433 int i;
435 switch(macintosh_config->adb_type) {
436 case MAC_ADB_IISI:
437 func = maciisi_write_pram; break;
438 case MAC_ADB_PB1:
439 case MAC_ADB_PB2:
440 func = pmu_write_pram; break;
441 case MAC_ADB_CUDA:
442 func = cuda_write_pram; break;
443 default:
444 func = via_write_pram;
446 if (!func)
447 return;
448 for (i = 0 ; i < len ; i++) {
449 (*func)(offset++, buffer[i]);
453 void mac_poweroff(void)
456 * MAC_ADB_IISI may need to be moved up here if it doesn't actually
457 * work using the ADB packet method. --David Kilzer
460 if (oss_present) {
461 oss_shutdown();
462 } else if (macintosh_config->adb_type == MAC_ADB_II) {
463 via_shutdown();
464 #ifdef CONFIG_ADB_CUDA
465 } else if (macintosh_config->adb_type == MAC_ADB_CUDA) {
466 cuda_shutdown();
467 #endif
468 #ifdef CONFIG_ADB_PMU68K
469 } else if (macintosh_config->adb_type == MAC_ADB_PB1
470 || macintosh_config->adb_type == MAC_ADB_PB2) {
471 pmu_shutdown();
472 #endif
474 local_irq_enable();
475 printk("It is now safe to turn off your Macintosh.\n");
476 while(1);
479 void mac_reset(void)
481 if (macintosh_config->adb_type == MAC_ADB_II) {
482 unsigned long flags;
484 /* need ROMBASE in booter */
485 /* indeed, plus need to MAP THE ROM !! */
487 if (mac_bi_data.rombase == 0)
488 mac_bi_data.rombase = 0x40800000;
490 /* works on some */
491 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
493 if (macintosh_config->ident == MAC_MODEL_SE30) {
495 * MSch: Machines known to crash on ROM reset ...
497 } else {
498 local_irq_save(flags);
500 rom_reset();
502 local_irq_restore(flags);
504 #ifdef CONFIG_ADB_CUDA
505 } else if (macintosh_config->adb_type == MAC_ADB_CUDA) {
506 cuda_restart();
507 #endif
508 #ifdef CONFIG_ADB_PMU68K
509 } else if (macintosh_config->adb_type == MAC_ADB_PB1
510 || macintosh_config->adb_type == MAC_ADB_PB2) {
511 pmu_restart();
512 #endif
513 } else if (CPU_IS_030) {
515 /* 030-specific reset routine. The idea is general, but the
516 * specific registers to reset are '030-specific. Until I
517 * have a non-030 machine, I can't test anything else.
518 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
521 unsigned long rombase = 0x40000000;
523 /* make a 1-to-1 mapping, using the transparent tran. reg. */
524 unsigned long virt = (unsigned long) mac_reset;
525 unsigned long phys = virt_to_phys(mac_reset);
526 unsigned long addr = (phys&0xFF000000)|0x8777;
527 unsigned long offset = phys-virt;
528 local_irq_disable(); /* lets not screw this up, ok? */
529 __asm__ __volatile__(".chip 68030\n\t"
530 "pmove %0,%/tt0\n\t"
531 ".chip 68k"
532 : : "m" (addr));
533 /* Now jump to physical address so we can disable MMU */
534 __asm__ __volatile__(
535 ".chip 68030\n\t"
536 "lea %/pc@(1f),%/a0\n\t"
537 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
538 "addl %0,%/sp\n\t"
539 "pflusha\n\t"
540 "jmp %/a0@\n\t" /* jump into physical memory */
541 "0:.long 0\n\t" /* a constant zero. */
542 /* OK. Now reset everything and jump to reset vector. */
543 "1:\n\t"
544 "lea %/pc@(0b),%/a0\n\t"
545 "pmove %/a0@, %/tc\n\t" /* disable mmu */
546 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
547 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
548 "movel #0, %/a0\n\t"
549 "movec %/a0, %/vbr\n\t" /* clear vector base register */
550 "movec %/a0, %/cacr\n\t" /* disable caches */
551 "movel #0x0808,%/a0\n\t"
552 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
553 "movew #0x2700,%/sr\n\t" /* set up status register */
554 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
555 "movec %/a0, %/isp\n\t"
556 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
557 "reset\n\t" /* reset external devices */
558 "jmp %/a0@\n\t" /* jump to the reset vector */
559 ".chip 68k"
560 : : "r" (offset), "a" (rombase) : "a0");
563 /* should never get here */
564 local_irq_enable();
565 printk ("Restart failed. Please restart manually.\n");
566 while(1);
570 * This function translates seconds since 1970 into a proper date.
572 * Algorithm cribbed from glibc2.1, __offtime().
574 #define SECS_PER_MINUTE (60)
575 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
576 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
578 static void unmktime(unsigned long time, long offset,
579 int *yearp, int *monp, int *dayp,
580 int *hourp, int *minp, int *secp)
582 /* How many days come before each month (0-12). */
583 static const unsigned short int __mon_yday[2][13] =
585 /* Normal years. */
586 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
587 /* Leap years. */
588 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
590 long int days, rem, y, wday, yday;
591 const unsigned short int *ip;
593 days = time / SECS_PER_DAY;
594 rem = time % SECS_PER_DAY;
595 rem += offset;
596 while (rem < 0) {
597 rem += SECS_PER_DAY;
598 --days;
600 while (rem >= SECS_PER_DAY) {
601 rem -= SECS_PER_DAY;
602 ++days;
604 *hourp = rem / SECS_PER_HOUR;
605 rem %= SECS_PER_HOUR;
606 *minp = rem / SECS_PER_MINUTE;
607 *secp = rem % SECS_PER_MINUTE;
608 /* January 1, 1970 was a Thursday. */
609 wday = (4 + days) % 7; /* Day in the week. Not currently used */
610 if (wday < 0) wday += 7;
611 y = 1970;
613 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
614 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
615 #define __isleap(year) \
616 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
618 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
620 /* Guess a corrected year, assuming 365 days per year. */
621 long int yg = y + days / 365 - (days % 365 < 0);
623 /* Adjust DAYS and Y to match the guessed year. */
624 days -= ((yg - y) * 365
625 + LEAPS_THRU_END_OF (yg - 1)
626 - LEAPS_THRU_END_OF (y - 1));
627 y = yg;
629 *yearp = y - 1900;
630 yday = days; /* day in the year. Not currently used. */
631 ip = __mon_yday[__isleap(y)];
632 for (y = 11; days < (long int) ip[y]; --y)
633 continue;
634 days -= ip[y];
635 *monp = y;
636 *dayp = days + 1; /* day in the month */
637 return;
641 * Read/write the hardware clock.
644 int mac_hwclk(int op, struct rtc_time *t)
646 unsigned long now;
648 if (!op) { /* read */
649 switch (macintosh_config->adb_type) {
650 case MAC_ADB_II:
651 case MAC_ADB_IOP:
652 now = via_read_time();
653 break;
654 case MAC_ADB_IISI:
655 now = maciisi_read_time();
656 break;
657 case MAC_ADB_PB1:
658 case MAC_ADB_PB2:
659 now = pmu_read_time();
660 break;
661 case MAC_ADB_CUDA:
662 now = cuda_read_time();
663 break;
664 default:
665 now = 0;
668 t->tm_wday = 0;
669 unmktime(now, 0,
670 &t->tm_year, &t->tm_mon, &t->tm_mday,
671 &t->tm_hour, &t->tm_min, &t->tm_sec);
672 } else { /* write */
674 now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
675 t->tm_hour, t->tm_min, t->tm_sec);
677 switch (macintosh_config->adb_type) {
678 case MAC_ADB_II:
679 case MAC_ADB_IOP:
680 via_write_time(now);
681 break;
682 case MAC_ADB_CUDA:
683 cuda_write_time(now);
684 break;
685 case MAC_ADB_PB1:
686 case MAC_ADB_PB2:
687 pmu_write_time(now);
688 break;
689 case MAC_ADB_IISI:
690 maciisi_write_time(now);
693 return 0;
697 * Set minutes/seconds in the hardware clock
700 int mac_set_clock_mmss (unsigned long nowtime)
702 struct rtc_time now;
704 mac_hwclk(0, &now);
705 now.tm_sec = nowtime % 60;
706 now.tm_min = (nowtime / 60) % 60;
707 mac_hwclk(1, &now);
709 return 0;