1 /*
2 * Miscellaneous Mac68K-specific stuff
3 */
4
5 #include <linux/config.h>
6 #include <linux/types.h>
7 #include <linux/errno.h>
8 #include <linux/miscdevice.h>
9 #include <linux/kernel.h>
10 #include <linux/delay.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <linux/time.h>
14 #include <linux/rtc.h>
15 #include <linux/mm.h>
16
17 #include <linux/adb.h>
18 #include <linux/cuda.h>
19 #include <linux/pmu.h>
20
21 #include <asm/uaccess.h>
22 #include <asm/io.h>
23 #include <asm/rtc.h>
24 #include <asm/system.h>
25 #include <asm/segment.h>
26 #include <asm/setup.h>
27 #include <asm/macintosh.h>
28 #include <asm/mac_via.h>
29 #include <asm/mac_oss.h>
30
31 #define BOOTINFO_COMPAT_1_0
32 #include <asm/bootinfo.h>
33 #include <asm/machdep.h>
34
35 /* Offset between Unix time (1970-based) and Mac time (1904-based) */
36
37 #define RTC_OFFSET 2082844800
38
39 extern struct mac_booter_data mac_bi_data;
40 static void (*rom_reset)(void);
41
42 #ifdef CONFIG_ADB
43 /*
44 * Return the current time as the number of seconds since January 1, 1904.
45 */
46
adb_read_time(void)47 static long adb_read_time(void)
48 {
49 volatile struct adb_request req;
50 long time;
51
52 adb_request((struct adb_request *) &req, NULL,
53 ADBREQ_RAW|ADBREQ_SYNC,
54 2, CUDA_PACKET, CUDA_GET_TIME);
55
56 time = (req.reply[3] << 24) | (req.reply[4] << 16)
57 | (req.reply[5] << 8) | req.reply[6];
58 return time - RTC_OFFSET;
59 }
60
61 /*
62 * Set the current system time
63 */
64
adb_write_time(long data)65 static void adb_write_time(long data)
66 {
67 volatile struct adb_request req;
68
69 data += RTC_OFFSET;
70
71 adb_request((struct adb_request *) &req, NULL,
72 ADBREQ_RAW|ADBREQ_SYNC,
73 6, CUDA_PACKET, CUDA_SET_TIME,
74 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
75 (data >> 8) & 0xFF, data & 0xFF);
76 }
77
78 /*
79 * Get a byte from the NVRAM
80 */
81
adb_read_pram(int offset)82 static __u8 adb_read_pram(int offset)
83 {
84 volatile struct adb_request req;
85
86 adb_request((struct adb_request *) &req, NULL,
87 ADBREQ_RAW|ADBREQ_SYNC,
88 4, CUDA_PACKET, CUDA_GET_PRAM,
89 (offset >> 8) & 0xFF, offset & 0xFF);
90 return req.reply[3];
91 }
92
93 /*
94 * Write a byte to the NVRAM
95 */
96
adb_write_pram(int offset,__u8 data)97 static void adb_write_pram(int offset, __u8 data)
98 {
99 volatile struct adb_request req;
100
101 adb_request((struct adb_request *) &req, NULL,
102 ADBREQ_RAW|ADBREQ_SYNC,
103 5, CUDA_PACKET, CUDA_SET_PRAM,
104 (offset >> 8) & 0xFF, offset & 0xFF,
105 data);
106 }
107 #endif /* CONFIG_ADB */
108
109 /*
110 * VIA PRAM/RTC access routines
111 *
112 * Must be called with interrupts disabled and
113 * the RTC should be enabled.
114 */
115
via_pram_readbyte(void)116 static __u8 via_pram_readbyte(void)
117 {
118 int i,reg;
119 __u8 data;
120
121 reg = via1[vBufB] & ~VIA1B_vRTCClk;
122
123 /* Set the RTC data line to be an input. */
124
125 via1[vDirB] &= ~VIA1B_vRTCData;
126
127 /* The bits of the byte come out in MSB order */
128
129 data = 0;
130 for (i = 0 ; i < 8 ; i++) {
131 via1[vBufB] = reg;
132 via1[vBufB] = reg | VIA1B_vRTCClk;
133 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
134 }
135
136 /* Return RTC data line to output state */
137
138 via1[vDirB] |= VIA1B_vRTCData;
139
140 return data;
141 }
142
via_pram_writebyte(__u8 data)143 static void via_pram_writebyte(__u8 data)
144 {
145 int i,reg,bit;
146
147 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
148
149 /* The bits of the byte go in in MSB order */
150
151 for (i = 0 ; i < 8 ; i++) {
152 bit = data & 0x80? 1 : 0;
153 data <<= 1;
154 via1[vBufB] = reg | bit;
155 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
156 }
157 }
158
159 /*
160 * Execute a VIA PRAM/RTC command. For read commands
161 * data should point to a one-byte buffer for the
162 * resulting data. For write commands it should point
163 * to the data byte to for the command.
164 *
165 * This function disables all interrupts while running.
166 */
167
via_pram_command(int command,__u8 * data)168 static void via_pram_command(int command, __u8 *data)
169 {
170 unsigned long cpu_flags;
171 int is_read;
172
173 save_flags(cpu_flags);
174 cli();
175
176 /* Enable the RTC and make sure the strobe line is high */
177
178 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
179
180 if (command & 0xFF00) { /* extended (two-byte) command */
181 via_pram_writebyte((command & 0xFF00) >> 8);
182 via_pram_writebyte(command & 0xFF);
183 is_read = command & 0x8000;
184 } else { /* one-byte command */
185 via_pram_writebyte(command);
186 is_read = command & 0x80;
187 }
188 if (is_read) {
189 *data = via_pram_readbyte();
190 } else {
191 via_pram_writebyte(*data);
192 }
193
194 /* All done, disable the RTC */
195
196 via1[vBufB] |= VIA1B_vRTCEnb;
197
198 restore_flags(cpu_flags);
199 }
200
via_read_pram(int offset)201 static __u8 via_read_pram(int offset)
202 {
203 return 0;
204 }
205
via_write_pram(int offset,__u8 data)206 static void via_write_pram(int offset, __u8 data)
207 {
208 }
209
210 /*
211 * Return the current time in seconds since January 1, 1904.
212 *
213 * This only works on machines with the VIA-based PRAM/RTC, which
214 * is basically any machine with Mac II-style ADB.
215 */
216
via_read_time(void)217 static long via_read_time(void)
218 {
219 union {
220 __u8 cdata[4];
221 long idata;
222 } result, last_result;
223 int ct;
224
225 /*
226 * The NetBSD guys say to loop until you get the same reading
227 * twice in a row.
228 */
229
230 ct = 0;
231 do {
232 if (++ct > 10) {
233 printk("via_read_time: couldn't get valid time, "
234 "last read = 0x%08lx and 0x%08lx\n",
235 last_result.idata, result.idata);
236 break;
237 }
238
239 last_result.idata = result.idata;
240 result.idata = 0;
241
242 via_pram_command(0x81, &result.cdata[3]);
243 via_pram_command(0x85, &result.cdata[2]);
244 via_pram_command(0x89, &result.cdata[1]);
245 via_pram_command(0x8D, &result.cdata[0]);
246 } while (result.idata != last_result.idata);
247
248 return result.idata - RTC_OFFSET;
249 }
250
251 /*
252 * Set the current time to a number of seconds since January 1, 1904.
253 *
254 * This only works on machines with the VIA-based PRAM/RTC, which
255 * is basically any machine with Mac II-style ADB.
256 */
257
via_write_time(long time)258 static void via_write_time(long time)
259 {
260 union {
261 __u8 cdata[4];
262 long idata;
263 } data;
264 __u8 temp;
265
266 /* Clear the write protect bit */
267
268 temp = 0x55;
269 via_pram_command(0x35, &temp);
270
271 data.idata = time + RTC_OFFSET;
272 via_pram_command(0x01, &data.cdata[3]);
273 via_pram_command(0x05, &data.cdata[2]);
274 via_pram_command(0x09, &data.cdata[1]);
275 via_pram_command(0x0D, &data.cdata[0]);
276
277 /* Set the write protect bit */
278
279 temp = 0xD5;
280 via_pram_command(0x35, &temp);
281 }
282
via_shutdown(void)283 static void via_shutdown(void)
284 {
285 if (rbv_present) {
286 via2[rBufB] &= ~0x04;
287 } else {
288 /* Direction of vDirB is output */
289 via2[vDirB] |= 0x04;
290 /* Send a value of 0 on that line */
291 via2[vBufB] &= ~0x04;
292 mdelay(1000);
293 }
294 }
295
296 /*
297 * FIXME: not sure how this is supposed to work exactly...
298 */
299
oss_shutdown(void)300 static void oss_shutdown(void)
301 {
302 oss->rom_ctrl = OSS_POWEROFF;
303 }
304
305 #ifdef CONFIG_ADB_CUDA
306
cuda_restart(void)307 static void cuda_restart(void)
308 {
309 adb_request(NULL, NULL, ADBREQ_RAW|ADBREQ_SYNC,
310 2, CUDA_PACKET, CUDA_RESET_SYSTEM);
311 }
312
cuda_shutdown(void)313 static void cuda_shutdown(void)
314 {
315 adb_request(NULL, NULL, ADBREQ_RAW|ADBREQ_SYNC,
316 2, CUDA_PACKET, CUDA_POWERDOWN);
317 }
318
319 #endif /* CONFIG_ADB_CUDA */
320
321 #ifdef CONFIG_ADB_PMU
322
pmu_restart(void)323 void pmu_restart(void)
324 {
325 adb_request(NULL, NULL, ADBREQ_RAW|ADBREQ_SYNC,
326 3, PMU_PACKET, PMU_SET_INTR_MASK,
327 PMU_INT_ADB|PMU_INT_TICK);
328
329 adb_request(NULL, NULL, ADBREQ_RAW|ADBREQ_SYNC,
330 2, PMU_PACKET, PMU_RESET);
331 }
332
pmu_shutdown(void)333 void pmu_shutdown(void)
334 {
335 adb_request(NULL, NULL, ADBREQ_RAW|ADBREQ_SYNC,
336 3, PMU_PACKET, PMU_SET_INTR_MASK,
337 PMU_INT_ADB|PMU_INT_TICK);
338
339 adb_request(NULL, NULL, ADBREQ_RAW|ADBREQ_SYNC,
340 6, PMU_PACKET, PMU_SHUTDOWN,
341 'M', 'A', 'T', 'T');
342 }
343
344 #endif /* CONFIG_ADB_PMU */
345
346 /*
347 *-------------------------------------------------------------------
348 * Below this point are the generic routines; they'll dispatch to the
349 * correct routine for the hardware on which we're running.
350 *-------------------------------------------------------------------
351 */
352
mac_pram_read(int offset,__u8 * buffer,int len)353 void mac_pram_read(int offset, __u8 *buffer, int len)
354 {
355 __u8 (*func)(int) = NULL;
356 int i;
357
358 if (macintosh_config->adb_type == MAC_ADB_IISI ||
359 macintosh_config->adb_type == MAC_ADB_PB1 ||
360 macintosh_config->adb_type == MAC_ADB_PB2 ||
361 macintosh_config->adb_type == MAC_ADB_CUDA) {
362 #ifdef CONFIG_ADB
363 func = adb_read_pram;
364 #else
365 return;
366 #endif
367 } else {
368 func = via_read_pram;
369 }
370 for (i = 0 ; i < len ; i++) {
371 buffer[i] = (*func)(offset++);
372 }
373 }
374
mac_pram_write(int offset,__u8 * buffer,int len)375 void mac_pram_write(int offset, __u8 *buffer, int len)
376 {
377 void (*func)(int, __u8) = NULL;
378 int i;
379
380 if (macintosh_config->adb_type == MAC_ADB_IISI ||
381 macintosh_config->adb_type == MAC_ADB_PB1 ||
382 macintosh_config->adb_type == MAC_ADB_PB2 ||
383 macintosh_config->adb_type == MAC_ADB_CUDA) {
384 #ifdef CONFIG_ADB
385 func = adb_write_pram;
386 #else
387 return;
388 #endif
389 } else {
390 func = via_write_pram;
391 }
392 for (i = 0 ; i < len ; i++) {
393 (*func)(offset++, buffer[i]);
394 }
395 }
396
mac_poweroff(void)397 void mac_poweroff(void)
398 {
399 /*
400 * MAC_ADB_IISI may need to be moved up here if it doesn't actually
401 * work using the ADB packet method. --David Kilzer
402 */
403
404 if (oss_present) {
405 oss_shutdown();
406 } else if (macintosh_config->adb_type == MAC_ADB_II) {
407 via_shutdown();
408 #ifdef CONFIG_ADB_CUDA
409 } else if (macintosh_config->adb_type == MAC_ADB_CUDA) {
410 cuda_shutdown();
411 #endif
412 #ifdef CONFIG_ADB_PMU
413 } else if (macintosh_config->adb_type == MAC_ADB_PB1
414 || macintosh_config->adb_type == MAC_ADB_PB2) {
415 pmu_shutdown();
416 #endif
417 }
418 sti();
419 printk("It is now safe to turn off your Macintosh.\n");
420 while(1);
421 }
422
mac_reset(void)423 void mac_reset(void)
424 {
425 if (macintosh_config->adb_type == MAC_ADB_II) {
426 unsigned long cpu_flags;
427
428 /* need ROMBASE in booter */
429 /* indeed, plus need to MAP THE ROM !! */
430
431 if (mac_bi_data.rombase == 0)
432 mac_bi_data.rombase = 0x40800000;
433
434 /* works on some */
435 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
436
437 if (macintosh_config->ident == MAC_MODEL_SE30) {
438 /*
439 * MSch: Machines known to crash on ROM reset ...
440 */
441 } else {
442 save_flags(cpu_flags);
443 cli();
444
445 rom_reset();
446
447 restore_flags(cpu_flags);
448 }
449 #ifdef CONFIG_ADB_CUDA
450 } else if (macintosh_config->adb_type == MAC_ADB_CUDA) {
451 cuda_restart();
452 #endif
453 #ifdef CONFIG_ADB_PMU
454 } else if (macintosh_config->adb_type == MAC_ADB_PB1
455 || macintosh_config->adb_type == MAC_ADB_PB2) {
456 pmu_restart();
457 #endif
458 } else if (CPU_IS_030) {
459
460 /* 030-specific reset routine. The idea is general, but the
461 * specific registers to reset are '030-specific. Until I
462 * have a non-030 machine, I can't test anything else.
463 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
464 */
465
466 unsigned long rombase = 0x40000000;
467
468 /* make a 1-to-1 mapping, using the transparent tran. reg. */
469 unsigned long virt = (unsigned long) mac_reset;
470 unsigned long phys = virt_to_phys(mac_reset);
471 unsigned long offset = phys-virt;
472 cli(); /* lets not screw this up, ok? */
473 __asm__ __volatile__(".chip 68030\n\t"
474 "pmove %0,%/tt0\n\t"
475 ".chip 68k"
476 : : "m" ((phys&0xFF000000)|0x8777));
477 /* Now jump to physical address so we can disable MMU */
478 __asm__ __volatile__(
479 ".chip 68030\n\t"
480 "lea %/pc@(1f),%/a0\n\t"
481 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
482 "addl %0,%/sp\n\t"
483 "pflusha\n\t"
484 "jmp %/a0@\n\t" /* jump into physical memory */
485 "0:.long 0\n\t" /* a constant zero. */
486 /* OK. Now reset everything and jump to reset vector. */
487 "1:\n\t"
488 "lea %/pc@(0b),%/a0\n\t"
489 "pmove %/a0@, %/tc\n\t" /* disable mmu */
490 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
491 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
492 "movel #0, %/a0\n\t"
493 "movec %/a0, %/vbr\n\t" /* clear vector base register */
494 "movec %/a0, %/cacr\n\t" /* disable caches */
495 "movel #0x0808,%/a0\n\t"
496 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
497 "movew #0x2700,%/sr\n\t" /* set up status register */
498 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
499 "movec %/a0, %/isp\n\t"
500 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
501 "reset\n\t" /* reset external devices */
502 "jmp %/a0@\n\t" /* jump to the reset vector */
503 ".chip 68k"
504 : : "r" (offset), "a" (rombase) : "a0");
505 }
506
507 /* should never get here */
508 sti();
509 printk ("Restart failed. Please restart manually.\n");
510 while(1);
511 }
512
513 /*
514 * This function translates seconds since 1970 into a proper date.
515 *
516 * Algorithm cribbed from glibc2.1, __offtime().
517 */
518 #define SECS_PER_MINUTE (60)
519 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
520 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
521
unmktime(unsigned long time,long offset,int * yearp,int * monp,int * dayp,int * hourp,int * minp,int * secp)522 static void unmktime(unsigned long time, long offset,
523 int *yearp, int *monp, int *dayp,
524 int *hourp, int *minp, int *secp)
525 {
526 /* How many days come before each month (0-12). */
527 static const unsigned short int __mon_yday[2][13] =
528 {
529 /* Normal years. */
530 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
531 /* Leap years. */
532 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
533 };
534 long int days, rem, y, wday, yday;
535 const unsigned short int *ip;
536
537 days = time / SECS_PER_DAY;
538 rem = time % SECS_PER_DAY;
539 rem += offset;
540 while (rem < 0) {
541 rem += SECS_PER_DAY;
542 --days;
543 }
544 while (rem >= SECS_PER_DAY) {
545 rem -= SECS_PER_DAY;
546 ++days;
547 }
548 *hourp = rem / SECS_PER_HOUR;
549 rem %= SECS_PER_HOUR;
550 *minp = rem / SECS_PER_MINUTE;
551 *secp = rem % SECS_PER_MINUTE;
552 /* January 1, 1970 was a Thursday. */
553 wday = (4 + days) % 7; /* Day in the week. Not currently used */
554 if (wday < 0) wday += 7;
555 y = 1970;
556
557 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
558 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
559 #define __isleap(year) \
560 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
561
562 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
563 {
564 /* Guess a corrected year, assuming 365 days per year. */
565 long int yg = y + days / 365 - (days % 365 < 0);
566
567 /* Adjust DAYS and Y to match the guessed year. */
568 days -= ((yg - y) * 365
569 + LEAPS_THRU_END_OF (yg - 1)
570 - LEAPS_THRU_END_OF (y - 1));
571 y = yg;
572 }
573 *yearp = y - 1900;
574 yday = days; /* day in the year. Not currently used. */
575 ip = __mon_yday[__isleap(y)];
576 for (y = 11; days < (long int) ip[y]; --y)
577 continue;
578 days -= ip[y];
579 *monp = y;
580 *dayp = days + 1; /* day in the month */
581 return;
582 }
583
584 /*
585 * Return the boot time for use in initializing the kernel clock.
586 *
587 * I'd like to read the hardware clock here but many machines read
588 * the PRAM through ADB, and interrupts aren't initialized when this
589 * is called so ADB obviously won't work.
590 */
591
mac_gettod(int * yearp,int * monp,int * dayp,int * hourp,int * minp,int * secp)592 void mac_gettod(int *yearp, int *monp, int *dayp,
593 int *hourp, int *minp, int *secp)
594 {
595 /* Yes the GMT bias is backwards. It looks like Penguin is
596 screwing up the boottime it gives us... This works for me
597 in Canada/Eastern but it might be wrong everywhere else. */
598 unmktime(mac_bi_data.boottime, -mac_bi_data.gmtbias * 60,
599 yearp, monp, dayp, hourp, minp, secp);
600 /* For some reason this is off by one */
601 *monp = *monp + 1;
602 }
603
604 /*
605 * Read/write the hardware clock.
606 */
607
mac_hwclk(int op,struct rtc_time * t)608 int mac_hwclk(int op, struct rtc_time *t)
609 {
610 unsigned long now;
611
612 if (!op) { /* read */
613 if (macintosh_config->adb_type == MAC_ADB_II) {
614 now = via_read_time();
615 } else
616 #ifdef CONFIG_ADB
617 if ((macintosh_config->adb_type == MAC_ADB_IISI) ||
618 (macintosh_config->adb_type == MAC_ADB_PB1) ||
619 (macintosh_config->adb_type == MAC_ADB_PB2) ||
620 (macintosh_config->adb_type == MAC_ADB_CUDA)) {
621 now = adb_read_time();
622 } else
623 #endif
624 if (macintosh_config->adb_type == MAC_ADB_IOP) {
625 now = via_read_time();
626 } else {
627 now = 0;
628 }
629
630 t->tm_wday = 0;
631 unmktime(now, 0,
632 &t->tm_year, &t->tm_mon, &t->tm_mday,
633 &t->tm_hour, &t->tm_min, &t->tm_sec);
634 printk("mac_hwclk: read %04d-%02d-%-2d %02d:%02d:%02d\n",
635 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
636 } else { /* write */
637 printk("mac_hwclk: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
638 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
639
640 #if 0 /* it trashes my rtc */
641 now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
642 t->tm_hour, t->tm_min, t->tm_sec);
643
644 if (macintosh_config->adb_type == MAC_ADB_II) {
645 via_write_time(now);
646 } else if ((macintosh_config->adb_type == MAC_ADB_IISI) ||
647 (macintosh_config->adb_type == MAC_ADB_PB1) ||
648 (macintosh_config->adb_type == MAC_ADB_PB2) ||
649 (macintosh_config->adb_type == MAC_ADB_CUDA)) {
650 adb_write_time(now);
651 } else if (macintosh_config->adb_type == MAC_ADB_IOP) {
652 via_write_time(now);
653 }
654 #endif
655 }
656 return 0;
657 }
658
659 /*
660 * Set minutes/seconds in the hardware clock
661 */
662
mac_set_clock_mmss(unsigned long nowtime)663 int mac_set_clock_mmss (unsigned long nowtime)
664 {
665 struct rtc_time now;
666
667 mac_hwclk(0, &now);
668 now.tm_sec = nowtime % 60;
669 now.tm_min = (nowtime / 60) % 60;
670 mac_hwclk(1, &now);
671
672 return 0;
673 }
674