Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Miscellaneous Mac68K-specific stuff
4 */
5
6#include <linux/types.h>
7#include <linux/errno.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>
14
15#include <linux/adb.h>
16#include <linux/cuda.h>
17#include <linux/pmu.h>
18
19#include <linux/uaccess.h>
20#include <asm/io.h>
21#include <asm/setup.h>
22#include <asm/macintosh.h>
23#include <asm/mac_via.h>
24#include <asm/mac_oss.h>
25
26#include <asm/machdep.h>
27
28/*
29 * Offset between Unix time (1970-based) and Mac time (1904-based). Cuda and PMU
30 * times wrap in 2040. If we need to handle later times, the read_time functions
31 * need to be changed to interpret wrapped times as post-2040.
32 */
33
34#define RTC_OFFSET 2082844800
35
36static void (*rom_reset)(void);
37
38#if IS_ENABLED(CONFIG_NVRAM)
39#ifdef CONFIG_ADB_CUDA
40static unsigned char cuda_pram_read_byte(int offset)
41{
42 struct adb_request req;
43
44 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
45 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
46 return 0;
47 while (!req.complete)
48 cuda_poll();
49 return req.reply[3];
50}
51
52static void cuda_pram_write_byte(unsigned char data, int offset)
53{
54 struct adb_request req;
55
56 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
57 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
58 return;
59 while (!req.complete)
60 cuda_poll();
61}
62#endif /* CONFIG_ADB_CUDA */
63
64#ifdef CONFIG_ADB_PMU
65static unsigned char pmu_pram_read_byte(int offset)
66{
67 struct adb_request req;
68
69 if (pmu_request(&req, NULL, 3, PMU_READ_XPRAM,
70 offset & 0xFF, 1) < 0)
71 return 0;
72 pmu_wait_complete(&req);
73
74 return req.reply[0];
75}
76
77static void pmu_pram_write_byte(unsigned char data, int offset)
78{
79 struct adb_request req;
80
81 if (pmu_request(&req, NULL, 4, PMU_WRITE_XPRAM,
82 offset & 0xFF, 1, data) < 0)
83 return;
84 pmu_wait_complete(&req);
85}
86#endif /* CONFIG_ADB_PMU */
87#endif /* CONFIG_NVRAM */
88
89/*
90 * VIA PRAM/RTC access routines
91 *
92 * Must be called with interrupts disabled and
93 * the RTC should be enabled.
94 */
95
96static __u8 via_rtc_recv(void)
97{
98 int i, reg;
99 __u8 data;
100
101 reg = via1[vBufB] & ~VIA1B_vRTCClk;
102
103 /* Set the RTC data line to be an input. */
104
105 via1[vDirB] &= ~VIA1B_vRTCData;
106
107 /* The bits of the byte come out in MSB order */
108
109 data = 0;
110 for (i = 0 ; i < 8 ; i++) {
111 via1[vBufB] = reg;
112 via1[vBufB] = reg | VIA1B_vRTCClk;
113 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
114 }
115
116 /* Return RTC data line to output state */
117
118 via1[vDirB] |= VIA1B_vRTCData;
119
120 return data;
121}
122
123static void via_rtc_send(__u8 data)
124{
125 int i, reg, bit;
126
127 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
128
129 /* The bits of the byte go into the RTC in MSB order */
130
131 for (i = 0 ; i < 8 ; i++) {
132 bit = data & 0x80? 1 : 0;
133 data <<= 1;
134 via1[vBufB] = reg | bit;
135 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
136 }
137}
138
139/*
140 * These values can be found in Inside Macintosh vol. III ch. 2
141 * which has a description of the RTC chip in the original Mac.
142 */
143
144#define RTC_FLG_READ BIT(7)
145#define RTC_FLG_WRITE_PROTECT BIT(7)
146#define RTC_CMD_READ(r) (RTC_FLG_READ | (r << 2))
147#define RTC_CMD_WRITE(r) (r << 2)
148#define RTC_REG_SECONDS_0 0
149#define RTC_REG_SECONDS_1 1
150#define RTC_REG_SECONDS_2 2
151#define RTC_REG_SECONDS_3 3
152#define RTC_REG_WRITE_PROTECT 13
153
154/*
155 * Inside Mac has no information about two-byte RTC commands but
156 * the MAME/MESS source code has the essentials.
157 */
158
159#define RTC_REG_XPRAM 14
160#define RTC_CMD_XPRAM_READ (RTC_CMD_READ(RTC_REG_XPRAM) << 8)
161#define RTC_CMD_XPRAM_WRITE (RTC_CMD_WRITE(RTC_REG_XPRAM) << 8)
162#define RTC_CMD_XPRAM_ARG(a) (((a & 0xE0) << 3) | ((a & 0x1F) << 2))
163
164/*
165 * Execute a VIA PRAM/RTC command. For read commands
166 * data should point to a one-byte buffer for the
167 * resulting data. For write commands it should point
168 * to the data byte to for the command.
169 *
170 * This function disables all interrupts while running.
171 */
172
173static void via_rtc_command(int command, __u8 *data)
174{
175 unsigned long flags;
176 int is_read;
177
178 local_irq_save(flags);
179
180 /* The least significant bits must be 0b01 according to Inside Mac */
181
182 command = (command & ~3) | 1;
183
184 /* Enable the RTC and make sure the strobe line is high */
185
186 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
187
188 if (command & 0xFF00) { /* extended (two-byte) command */
189 via_rtc_send((command & 0xFF00) >> 8);
190 via_rtc_send(command & 0xFF);
191 is_read = command & (RTC_FLG_READ << 8);
192 } else { /* one-byte command */
193 via_rtc_send(command);
194 is_read = command & RTC_FLG_READ;
195 }
196 if (is_read) {
197 *data = via_rtc_recv();
198 } else {
199 via_rtc_send(*data);
200 }
201
202 /* All done, disable the RTC */
203
204 via1[vBufB] |= VIA1B_vRTCEnb;
205
206 local_irq_restore(flags);
207}
208
209#if IS_ENABLED(CONFIG_NVRAM)
210static unsigned char via_pram_read_byte(int offset)
211{
212 unsigned char temp;
213
214 via_rtc_command(RTC_CMD_XPRAM_READ | RTC_CMD_XPRAM_ARG(offset), &temp);
215
216 return temp;
217}
218
219static void via_pram_write_byte(unsigned char data, int offset)
220{
221 unsigned char temp;
222
223 temp = 0x55;
224 via_rtc_command(RTC_CMD_WRITE(RTC_REG_WRITE_PROTECT), &temp);
225
226 temp = data;
227 via_rtc_command(RTC_CMD_XPRAM_WRITE | RTC_CMD_XPRAM_ARG(offset), &temp);
228
229 temp = 0x55 | RTC_FLG_WRITE_PROTECT;
230 via_rtc_command(RTC_CMD_WRITE(RTC_REG_WRITE_PROTECT), &temp);
231}
232#endif /* CONFIG_NVRAM */
233
234/*
235 * Return the current time in seconds since January 1, 1904.
236 *
237 * This only works on machines with the VIA-based PRAM/RTC, which
238 * is basically any machine with Mac II-style ADB.
239 */
240
241static time64_t via_read_time(void)
242{
243 union {
244 __u8 cdata[4];
245 __u32 idata;
246 } result, last_result;
247 int count = 1;
248
249 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_0), &last_result.cdata[3]);
250 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_1), &last_result.cdata[2]);
251 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_2), &last_result.cdata[1]);
252 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_3), &last_result.cdata[0]);
253
254 /*
255 * The NetBSD guys say to loop until you get the same reading
256 * twice in a row.
257 */
258
259 while (1) {
260 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_0),
261 &result.cdata[3]);
262 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_1),
263 &result.cdata[2]);
264 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_2),
265 &result.cdata[1]);
266 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_3),
267 &result.cdata[0]);
268
269 if (result.idata == last_result.idata)
270 return (time64_t)result.idata - RTC_OFFSET;
271
272 if (++count > 10)
273 break;
274
275 last_result.idata = result.idata;
276 }
277
278 pr_err("%s: failed to read a stable value; got 0x%08x then 0x%08x\n",
279 __func__, last_result.idata, result.idata);
280
281 return 0;
282}
283
284/*
285 * Set the current time to a number of seconds since January 1, 1904.
286 *
287 * This only works on machines with the VIA-based PRAM/RTC, which
288 * is basically any machine with Mac II-style ADB.
289 */
290
291static void via_set_rtc_time(struct rtc_time *tm)
292{
293 union {
294 __u8 cdata[4];
295 __u32 idata;
296 } data;
297 __u8 temp;
298 time64_t time;
299
300 time = mktime64(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
301 tm->tm_hour, tm->tm_min, tm->tm_sec);
302
303 /* Clear the write protect bit */
304
305 temp = 0x55;
306 via_rtc_command(RTC_CMD_WRITE(RTC_REG_WRITE_PROTECT), &temp);
307
308 data.idata = lower_32_bits(time + RTC_OFFSET);
309 via_rtc_command(RTC_CMD_WRITE(RTC_REG_SECONDS_0), &data.cdata[3]);
310 via_rtc_command(RTC_CMD_WRITE(RTC_REG_SECONDS_1), &data.cdata[2]);
311 via_rtc_command(RTC_CMD_WRITE(RTC_REG_SECONDS_2), &data.cdata[1]);
312 via_rtc_command(RTC_CMD_WRITE(RTC_REG_SECONDS_3), &data.cdata[0]);
313
314 /* Set the write protect bit */
315
316 temp = 0x55 | RTC_FLG_WRITE_PROTECT;
317 via_rtc_command(RTC_CMD_WRITE(RTC_REG_WRITE_PROTECT), &temp);
318}
319
320static void via_shutdown(void)
321{
322 if (rbv_present) {
323 via2[rBufB] &= ~0x04;
324 } else {
325 /* Direction of vDirB is output */
326 via2[vDirB] |= 0x04;
327 /* Send a value of 0 on that line */
328 via2[vBufB] &= ~0x04;
329 mdelay(1000);
330 }
331}
332
333static void oss_shutdown(void)
334{
335 oss->rom_ctrl = OSS_POWEROFF;
336}
337
338#ifdef CONFIG_ADB_CUDA
339static void cuda_restart(void)
340{
341 struct adb_request req;
342
343 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
344 return;
345 while (!req.complete)
346 cuda_poll();
347}
348
349static void cuda_shutdown(void)
350{
351 struct adb_request req;
352
353 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
354 return;
355
356 /* Avoid infinite polling loop when PSU is not under Cuda control */
357 switch (macintosh_config->ident) {
358 case MAC_MODEL_C660:
359 case MAC_MODEL_Q605:
360 case MAC_MODEL_Q605_ACC:
361 case MAC_MODEL_P475:
362 case MAC_MODEL_P475F:
363 return;
364 }
365
366 while (!req.complete)
367 cuda_poll();
368}
369#endif /* CONFIG_ADB_CUDA */
370
371/*
372 *-------------------------------------------------------------------
373 * Below this point are the generic routines; they'll dispatch to the
374 * correct routine for the hardware on which we're running.
375 *-------------------------------------------------------------------
376 */
377
378#if IS_ENABLED(CONFIG_NVRAM)
379unsigned char mac_pram_read_byte(int addr)
380{
381 switch (macintosh_config->adb_type) {
382 case MAC_ADB_IOP:
383 case MAC_ADB_II:
384 case MAC_ADB_PB1:
385 return via_pram_read_byte(addr);
386#ifdef CONFIG_ADB_CUDA
387 case MAC_ADB_EGRET:
388 case MAC_ADB_CUDA:
389 return cuda_pram_read_byte(addr);
390#endif
391#ifdef CONFIG_ADB_PMU
392 case MAC_ADB_PB2:
393 return pmu_pram_read_byte(addr);
394#endif
395 default:
396 return 0xFF;
397 }
398}
399
400void mac_pram_write_byte(unsigned char val, int addr)
401{
402 switch (macintosh_config->adb_type) {
403 case MAC_ADB_IOP:
404 case MAC_ADB_II:
405 case MAC_ADB_PB1:
406 via_pram_write_byte(val, addr);
407 break;
408#ifdef CONFIG_ADB_CUDA
409 case MAC_ADB_EGRET:
410 case MAC_ADB_CUDA:
411 cuda_pram_write_byte(val, addr);
412 break;
413#endif
414#ifdef CONFIG_ADB_PMU
415 case MAC_ADB_PB2:
416 pmu_pram_write_byte(val, addr);
417 break;
418#endif
419 default:
420 break;
421 }
422}
423
424ssize_t mac_pram_get_size(void)
425{
426 return 256;
427}
428#endif /* CONFIG_NVRAM */
429
430void mac_poweroff(void)
431{
432 if (oss_present) {
433 oss_shutdown();
434 } else if (macintosh_config->adb_type == MAC_ADB_II) {
435 via_shutdown();
436#ifdef CONFIG_ADB_CUDA
437 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
438 macintosh_config->adb_type == MAC_ADB_CUDA) {
439 cuda_shutdown();
440#endif
441#ifdef CONFIG_ADB_PMU
442 } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
443 pmu_shutdown();
444#endif
445 }
446
447 pr_crit("It is now safe to turn off your Macintosh.\n");
448 local_irq_disable();
449 while(1);
450}
451
452void mac_reset(void)
453{
454 if (macintosh_config->adb_type == MAC_ADB_II &&
455 macintosh_config->ident != MAC_MODEL_SE30) {
456 /* need ROMBASE in booter */
457 /* indeed, plus need to MAP THE ROM !! */
458
459 if (mac_bi_data.rombase == 0)
460 mac_bi_data.rombase = 0x40800000;
461
462 /* works on some */
463 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
464
465 local_irq_disable();
466 rom_reset();
467#ifdef CONFIG_ADB_CUDA
468 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
469 macintosh_config->adb_type == MAC_ADB_CUDA) {
470 cuda_restart();
471#endif
472#ifdef CONFIG_ADB_PMU
473 } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
474 pmu_restart();
475#endif
476 } else if (CPU_IS_030) {
477
478 /* 030-specific reset routine. The idea is general, but the
479 * specific registers to reset are '030-specific. Until I
480 * have a non-030 machine, I can't test anything else.
481 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
482 */
483
484 unsigned long rombase = 0x40000000;
485
486 /* make a 1-to-1 mapping, using the transparent tran. reg. */
487 unsigned long virt = (unsigned long) mac_reset;
488 unsigned long phys = virt_to_phys(mac_reset);
489 unsigned long addr = (phys&0xFF000000)|0x8777;
490 unsigned long offset = phys-virt;
491
492 local_irq_disable(); /* lets not screw this up, ok? */
493 __asm__ __volatile__(".chip 68030\n\t"
494 "pmove %0,%/tt0\n\t"
495 ".chip 68k"
496 : : "m" (addr));
497 /* Now jump to physical address so we can disable MMU */
498 __asm__ __volatile__(
499 ".chip 68030\n\t"
500 "lea %/pc@(1f),%/a0\n\t"
501 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
502 "addl %0,%/sp\n\t"
503 "pflusha\n\t"
504 "jmp %/a0@\n\t" /* jump into physical memory */
505 "0:.long 0\n\t" /* a constant zero. */
506 /* OK. Now reset everything and jump to reset vector. */
507 "1:\n\t"
508 "lea %/pc@(0b),%/a0\n\t"
509 "pmove %/a0@, %/tc\n\t" /* disable mmu */
510 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
511 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
512 "movel #0, %/a0\n\t"
513 "movec %/a0, %/vbr\n\t" /* clear vector base register */
514 "movec %/a0, %/cacr\n\t" /* disable caches */
515 "movel #0x0808,%/a0\n\t"
516 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
517 "movew #0x2700,%/sr\n\t" /* set up status register */
518 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
519 "movec %/a0, %/isp\n\t"
520 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
521 "reset\n\t" /* reset external devices */
522 "jmp %/a0@\n\t" /* jump to the reset vector */
523 ".chip 68k"
524 : : "r" (offset), "a" (rombase) : "a0");
525 }
526
527 /* should never get here */
528 pr_crit("Restart failed. Please restart manually.\n");
529 local_irq_disable();
530 while(1);
531}
532
533/*
534 * This function translates seconds since 1970 into a proper date.
535 *
536 * Algorithm cribbed from glibc2.1, __offtime().
537 *
538 * This is roughly same as rtc_time64_to_tm(), which we should probably
539 * use here, but it's only available when CONFIG_RTC_LIB is enabled.
540 */
541#define SECS_PER_MINUTE (60)
542#define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
543#define SECS_PER_DAY (SECS_PER_HOUR * 24)
544
545static void unmktime(time64_t time, long offset,
546 int *yearp, int *monp, int *dayp,
547 int *hourp, int *minp, int *secp)
548{
549 /* How many days come before each month (0-12). */
550 static const unsigned short int __mon_yday[2][13] =
551 {
552 /* Normal years. */
553 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
554 /* Leap years. */
555 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
556 };
557 int days, rem, y, wday, yday;
558 const unsigned short int *ip;
559
560 days = div_u64_rem(time, SECS_PER_DAY, &rem);
561 rem += offset;
562 while (rem < 0) {
563 rem += SECS_PER_DAY;
564 --days;
565 }
566 while (rem >= SECS_PER_DAY) {
567 rem -= SECS_PER_DAY;
568 ++days;
569 }
570 *hourp = rem / SECS_PER_HOUR;
571 rem %= SECS_PER_HOUR;
572 *minp = rem / SECS_PER_MINUTE;
573 *secp = rem % SECS_PER_MINUTE;
574 /* January 1, 1970 was a Thursday. */
575 wday = (4 + days) % 7; /* Day in the week. Not currently used */
576 if (wday < 0) wday += 7;
577 y = 1970;
578
579#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
580#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
581#define __isleap(year) \
582 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
583
584 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
585 {
586 /* Guess a corrected year, assuming 365 days per year. */
587 long int yg = y + days / 365 - (days % 365 < 0);
588
589 /* Adjust DAYS and Y to match the guessed year. */
590 days -= (yg - y) * 365 +
591 LEAPS_THRU_END_OF(yg - 1) - LEAPS_THRU_END_OF(y - 1);
592 y = yg;
593 }
594 *yearp = y - 1900;
595 yday = days; /* day in the year. Not currently used. */
596 ip = __mon_yday[__isleap(y)];
597 for (y = 11; days < (long int) ip[y]; --y)
598 continue;
599 days -= ip[y];
600 *monp = y;
601 *dayp = days + 1; /* day in the month */
602 return;
603}
604
605/*
606 * Read/write the hardware clock.
607 */
608
609int mac_hwclk(int op, struct rtc_time *t)
610{
611 time64_t now;
612
613 if (!op) { /* read */
614 switch (macintosh_config->adb_type) {
615 case MAC_ADB_IOP:
616 case MAC_ADB_II:
617 case MAC_ADB_PB1:
618 now = via_read_time();
619 break;
620#ifdef CONFIG_ADB_CUDA
621 case MAC_ADB_EGRET:
622 case MAC_ADB_CUDA:
623 now = cuda_get_time();
624 break;
625#endif
626#ifdef CONFIG_ADB_PMU
627 case MAC_ADB_PB2:
628 now = pmu_get_time();
629 break;
630#endif
631 default:
632 now = 0;
633 }
634
635 t->tm_wday = 0;
636 unmktime(now, 0,
637 &t->tm_year, &t->tm_mon, &t->tm_mday,
638 &t->tm_hour, &t->tm_min, &t->tm_sec);
639 pr_debug("%s: read %ptR\n", __func__, t);
640 } else { /* write */
641 pr_debug("%s: tried to write %ptR\n", __func__, t);
642
643 switch (macintosh_config->adb_type) {
644 case MAC_ADB_IOP:
645 case MAC_ADB_II:
646 case MAC_ADB_PB1:
647 via_set_rtc_time(t);
648 break;
649#ifdef CONFIG_ADB_CUDA
650 case MAC_ADB_EGRET:
651 case MAC_ADB_CUDA:
652 cuda_set_rtc_time(t);
653 break;
654#endif
655#ifdef CONFIG_ADB_PMU
656 case MAC_ADB_PB2:
657 pmu_set_rtc_time(t);
658 break;
659#endif
660 default:
661 return -ENODEV;
662 }
663 }
664 return 0;
665}
1/*
2 * Miscellaneous Mac68K-specific stuff
3 */
4
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>
14
15#include <linux/adb.h>
16#include <linux/cuda.h>
17#include <linux/pmu.h>
18
19#include <asm/uaccess.h>
20#include <asm/io.h>
21#include <asm/rtc.h>
22#include <asm/segment.h>
23#include <asm/setup.h>
24#include <asm/macintosh.h>
25#include <asm/mac_via.h>
26#include <asm/mac_oss.h>
27
28#define BOOTINFO_COMPAT_1_0
29#include <asm/bootinfo.h>
30#include <asm/machdep.h>
31
32/* Offset between Unix time (1970-based) and Mac time (1904-based) */
33
34#define RTC_OFFSET 2082844800
35
36static void (*rom_reset)(void);
37
38#ifdef CONFIG_ADB_CUDA
39static long cuda_read_time(void)
40{
41 struct adb_request req;
42 long time;
43
44 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
45 return 0;
46 while (!req.complete)
47 cuda_poll();
48
49 time = (req.reply[3] << 24) | (req.reply[4] << 16)
50 | (req.reply[5] << 8) | req.reply[6];
51 return time - RTC_OFFSET;
52}
53
54static void cuda_write_time(long data)
55{
56 struct adb_request req;
57 data += RTC_OFFSET;
58 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
59 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
60 (data >> 8) & 0xFF, data & 0xFF) < 0)
61 return;
62 while (!req.complete)
63 cuda_poll();
64}
65
66static __u8 cuda_read_pram(int offset)
67{
68 struct adb_request req;
69 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
70 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
71 return 0;
72 while (!req.complete)
73 cuda_poll();
74 return req.reply[3];
75}
76
77static void cuda_write_pram(int offset, __u8 data)
78{
79 struct adb_request req;
80 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
81 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
82 return;
83 while (!req.complete)
84 cuda_poll();
85}
86#else
87#define cuda_read_time() 0
88#define cuda_write_time(n)
89#define cuda_read_pram NULL
90#define cuda_write_pram NULL
91#endif
92
93#ifdef CONFIG_ADB_PMU68K
94static long pmu_read_time(void)
95{
96 struct adb_request req;
97 long time;
98
99 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
100 return 0;
101 while (!req.complete)
102 pmu_poll();
103
104 time = (req.reply[1] << 24) | (req.reply[2] << 16)
105 | (req.reply[3] << 8) | req.reply[4];
106 return time - RTC_OFFSET;
107}
108
109static void pmu_write_time(long data)
110{
111 struct adb_request req;
112 data += RTC_OFFSET;
113 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
114 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
115 (data >> 8) & 0xFF, data & 0xFF) < 0)
116 return;
117 while (!req.complete)
118 pmu_poll();
119}
120
121static __u8 pmu_read_pram(int offset)
122{
123 struct adb_request req;
124 if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM,
125 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
126 return 0;
127 while (!req.complete)
128 pmu_poll();
129 return req.reply[3];
130}
131
132static void pmu_write_pram(int offset, __u8 data)
133{
134 struct adb_request req;
135 if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM,
136 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
137 return;
138 while (!req.complete)
139 pmu_poll();
140}
141#else
142#define pmu_read_time() 0
143#define pmu_write_time(n)
144#define pmu_read_pram NULL
145#define pmu_write_pram NULL
146#endif
147
148#if 0 /* def CONFIG_ADB_MACIISI */
149extern int maciisi_request(struct adb_request *req,
150 void (*done)(struct adb_request *), int nbytes, ...);
151
152static long maciisi_read_time(void)
153{
154 struct adb_request req;
155 long time;
156
157 if (maciisi_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME))
158 return 0;
159
160 time = (req.reply[3] << 24) | (req.reply[4] << 16)
161 | (req.reply[5] << 8) | req.reply[6];
162 return time - RTC_OFFSET;
163}
164
165static void maciisi_write_time(long data)
166{
167 struct adb_request req;
168 data += RTC_OFFSET;
169 maciisi_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
170 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
171 (data >> 8) & 0xFF, data & 0xFF);
172}
173
174static __u8 maciisi_read_pram(int offset)
175{
176 struct adb_request req;
177 if (maciisi_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
178 (offset >> 8) & 0xFF, offset & 0xFF))
179 return 0;
180 return req.reply[3];
181}
182
183static void maciisi_write_pram(int offset, __u8 data)
184{
185 struct adb_request req;
186 maciisi_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
187 (offset >> 8) & 0xFF, offset & 0xFF, data);
188}
189#else
190#define maciisi_read_time() 0
191#define maciisi_write_time(n)
192#define maciisi_read_pram NULL
193#define maciisi_write_pram NULL
194#endif
195
196/*
197 * VIA PRAM/RTC access routines
198 *
199 * Must be called with interrupts disabled and
200 * the RTC should be enabled.
201 */
202
203static __u8 via_pram_readbyte(void)
204{
205 int i,reg;
206 __u8 data;
207
208 reg = via1[vBufB] & ~VIA1B_vRTCClk;
209
210 /* Set the RTC data line to be an input. */
211
212 via1[vDirB] &= ~VIA1B_vRTCData;
213
214 /* The bits of the byte come out in MSB order */
215
216 data = 0;
217 for (i = 0 ; i < 8 ; i++) {
218 via1[vBufB] = reg;
219 via1[vBufB] = reg | VIA1B_vRTCClk;
220 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
221 }
222
223 /* Return RTC data line to output state */
224
225 via1[vDirB] |= VIA1B_vRTCData;
226
227 return data;
228}
229
230static void via_pram_writebyte(__u8 data)
231{
232 int i,reg,bit;
233
234 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
235
236 /* The bits of the byte go in in MSB order */
237
238 for (i = 0 ; i < 8 ; i++) {
239 bit = data & 0x80? 1 : 0;
240 data <<= 1;
241 via1[vBufB] = reg | bit;
242 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
243 }
244}
245
246/*
247 * Execute a VIA PRAM/RTC command. For read commands
248 * data should point to a one-byte buffer for the
249 * resulting data. For write commands it should point
250 * to the data byte to for the command.
251 *
252 * This function disables all interrupts while running.
253 */
254
255static void via_pram_command(int command, __u8 *data)
256{
257 unsigned long flags;
258 int is_read;
259
260 local_irq_save(flags);
261
262 /* Enable the RTC and make sure the strobe line is high */
263
264 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
265
266 if (command & 0xFF00) { /* extended (two-byte) command */
267 via_pram_writebyte((command & 0xFF00) >> 8);
268 via_pram_writebyte(command & 0xFF);
269 is_read = command & 0x8000;
270 } else { /* one-byte command */
271 via_pram_writebyte(command);
272 is_read = command & 0x80;
273 }
274 if (is_read) {
275 *data = via_pram_readbyte();
276 } else {
277 via_pram_writebyte(*data);
278 }
279
280 /* All done, disable the RTC */
281
282 via1[vBufB] |= VIA1B_vRTCEnb;
283
284 local_irq_restore(flags);
285}
286
287static __u8 via_read_pram(int offset)
288{
289 return 0;
290}
291
292static void via_write_pram(int offset, __u8 data)
293{
294}
295
296/*
297 * Return the current time in seconds since January 1, 1904.
298 *
299 * This only works on machines with the VIA-based PRAM/RTC, which
300 * is basically any machine with Mac II-style ADB.
301 */
302
303static long via_read_time(void)
304{
305 union {
306 __u8 cdata[4];
307 long idata;
308 } result, last_result;
309 int count = 1;
310
311 via_pram_command(0x81, &last_result.cdata[3]);
312 via_pram_command(0x85, &last_result.cdata[2]);
313 via_pram_command(0x89, &last_result.cdata[1]);
314 via_pram_command(0x8D, &last_result.cdata[0]);
315
316 /*
317 * The NetBSD guys say to loop until you get the same reading
318 * twice in a row.
319 */
320
321 while (1) {
322 via_pram_command(0x81, &result.cdata[3]);
323 via_pram_command(0x85, &result.cdata[2]);
324 via_pram_command(0x89, &result.cdata[1]);
325 via_pram_command(0x8D, &result.cdata[0]);
326
327 if (result.idata == last_result.idata)
328 return result.idata - RTC_OFFSET;
329
330 if (++count > 10)
331 break;
332
333 last_result.idata = result.idata;
334 }
335
336 pr_err("via_read_time: failed to read a stable value; "
337 "got 0x%08lx then 0x%08lx\n",
338 last_result.idata, result.idata);
339
340 return 0;
341}
342
343/*
344 * Set the current time to a number of seconds since January 1, 1904.
345 *
346 * This only works on machines with the VIA-based PRAM/RTC, which
347 * is basically any machine with Mac II-style ADB.
348 */
349
350static void via_write_time(long time)
351{
352 union {
353 __u8 cdata[4];
354 long idata;
355 } data;
356 __u8 temp;
357
358 /* Clear the write protect bit */
359
360 temp = 0x55;
361 via_pram_command(0x35, &temp);
362
363 data.idata = time + RTC_OFFSET;
364 via_pram_command(0x01, &data.cdata[3]);
365 via_pram_command(0x05, &data.cdata[2]);
366 via_pram_command(0x09, &data.cdata[1]);
367 via_pram_command(0x0D, &data.cdata[0]);
368
369 /* Set the write protect bit */
370
371 temp = 0xD5;
372 via_pram_command(0x35, &temp);
373}
374
375static void via_shutdown(void)
376{
377 if (rbv_present) {
378 via2[rBufB] &= ~0x04;
379 } else {
380 /* Direction of vDirB is output */
381 via2[vDirB] |= 0x04;
382 /* Send a value of 0 on that line */
383 via2[vBufB] &= ~0x04;
384 mdelay(1000);
385 }
386}
387
388/*
389 * FIXME: not sure how this is supposed to work exactly...
390 */
391
392static void oss_shutdown(void)
393{
394 oss->rom_ctrl = OSS_POWEROFF;
395}
396
397#ifdef CONFIG_ADB_CUDA
398
399static void cuda_restart(void)
400{
401 struct adb_request req;
402 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
403 return;
404 while (!req.complete)
405 cuda_poll();
406}
407
408static void cuda_shutdown(void)
409{
410 struct adb_request req;
411 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
412 return;
413 while (!req.complete)
414 cuda_poll();
415}
416
417#endif /* CONFIG_ADB_CUDA */
418
419#ifdef CONFIG_ADB_PMU68K
420
421void pmu_restart(void)
422{
423 struct adb_request req;
424 if (pmu_request(&req, NULL,
425 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
426 return;
427 while (!req.complete)
428 pmu_poll();
429 if (pmu_request(&req, NULL, 1, PMU_RESET) < 0)
430 return;
431 while (!req.complete)
432 pmu_poll();
433}
434
435void pmu_shutdown(void)
436{
437 struct adb_request req;
438 if (pmu_request(&req, NULL,
439 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
440 return;
441 while (!req.complete)
442 pmu_poll();
443 if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0)
444 return;
445 while (!req.complete)
446 pmu_poll();
447}
448
449#endif
450
451/*
452 *-------------------------------------------------------------------
453 * Below this point are the generic routines; they'll dispatch to the
454 * correct routine for the hardware on which we're running.
455 *-------------------------------------------------------------------
456 */
457
458void mac_pram_read(int offset, __u8 *buffer, int len)
459{
460 __u8 (*func)(int);
461 int i;
462
463 switch(macintosh_config->adb_type) {
464 case MAC_ADB_IISI:
465 func = maciisi_read_pram; break;
466 case MAC_ADB_PB1:
467 case MAC_ADB_PB2:
468 func = pmu_read_pram; break;
469 case MAC_ADB_CUDA:
470 func = cuda_read_pram; break;
471 default:
472 func = via_read_pram;
473 }
474 if (!func)
475 return;
476 for (i = 0 ; i < len ; i++) {
477 buffer[i] = (*func)(offset++);
478 }
479}
480
481void mac_pram_write(int offset, __u8 *buffer, int len)
482{
483 void (*func)(int, __u8);
484 int i;
485
486 switch(macintosh_config->adb_type) {
487 case MAC_ADB_IISI:
488 func = maciisi_write_pram; break;
489 case MAC_ADB_PB1:
490 case MAC_ADB_PB2:
491 func = pmu_write_pram; break;
492 case MAC_ADB_CUDA:
493 func = cuda_write_pram; break;
494 default:
495 func = via_write_pram;
496 }
497 if (!func)
498 return;
499 for (i = 0 ; i < len ; i++) {
500 (*func)(offset++, buffer[i]);
501 }
502}
503
504void mac_poweroff(void)
505{
506 /*
507 * MAC_ADB_IISI may need to be moved up here if it doesn't actually
508 * work using the ADB packet method. --David Kilzer
509 */
510
511 if (oss_present) {
512 oss_shutdown();
513 } else if (macintosh_config->adb_type == MAC_ADB_II) {
514 via_shutdown();
515#ifdef CONFIG_ADB_CUDA
516 } else if (macintosh_config->adb_type == MAC_ADB_CUDA) {
517 cuda_shutdown();
518#endif
519#ifdef CONFIG_ADB_PMU68K
520 } else if (macintosh_config->adb_type == MAC_ADB_PB1
521 || macintosh_config->adb_type == MAC_ADB_PB2) {
522 pmu_shutdown();
523#endif
524 }
525 local_irq_enable();
526 printk("It is now safe to turn off your Macintosh.\n");
527 while(1);
528}
529
530void mac_reset(void)
531{
532 if (macintosh_config->adb_type == MAC_ADB_II) {
533 unsigned long flags;
534
535 /* need ROMBASE in booter */
536 /* indeed, plus need to MAP THE ROM !! */
537
538 if (mac_bi_data.rombase == 0)
539 mac_bi_data.rombase = 0x40800000;
540
541 /* works on some */
542 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
543
544 if (macintosh_config->ident == MAC_MODEL_SE30) {
545 /*
546 * MSch: Machines known to crash on ROM reset ...
547 */
548 } else {
549 local_irq_save(flags);
550
551 rom_reset();
552
553 local_irq_restore(flags);
554 }
555#ifdef CONFIG_ADB_CUDA
556 } else if (macintosh_config->adb_type == MAC_ADB_CUDA) {
557 cuda_restart();
558#endif
559#ifdef CONFIG_ADB_PMU68K
560 } else if (macintosh_config->adb_type == MAC_ADB_PB1
561 || macintosh_config->adb_type == MAC_ADB_PB2) {
562 pmu_restart();
563#endif
564 } else if (CPU_IS_030) {
565
566 /* 030-specific reset routine. The idea is general, but the
567 * specific registers to reset are '030-specific. Until I
568 * have a non-030 machine, I can't test anything else.
569 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
570 */
571
572 unsigned long rombase = 0x40000000;
573
574 /* make a 1-to-1 mapping, using the transparent tran. reg. */
575 unsigned long virt = (unsigned long) mac_reset;
576 unsigned long phys = virt_to_phys(mac_reset);
577 unsigned long addr = (phys&0xFF000000)|0x8777;
578 unsigned long offset = phys-virt;
579 local_irq_disable(); /* lets not screw this up, ok? */
580 __asm__ __volatile__(".chip 68030\n\t"
581 "pmove %0,%/tt0\n\t"
582 ".chip 68k"
583 : : "m" (addr));
584 /* Now jump to physical address so we can disable MMU */
585 __asm__ __volatile__(
586 ".chip 68030\n\t"
587 "lea %/pc@(1f),%/a0\n\t"
588 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
589 "addl %0,%/sp\n\t"
590 "pflusha\n\t"
591 "jmp %/a0@\n\t" /* jump into physical memory */
592 "0:.long 0\n\t" /* a constant zero. */
593 /* OK. Now reset everything and jump to reset vector. */
594 "1:\n\t"
595 "lea %/pc@(0b),%/a0\n\t"
596 "pmove %/a0@, %/tc\n\t" /* disable mmu */
597 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
598 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
599 "movel #0, %/a0\n\t"
600 "movec %/a0, %/vbr\n\t" /* clear vector base register */
601 "movec %/a0, %/cacr\n\t" /* disable caches */
602 "movel #0x0808,%/a0\n\t"
603 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
604 "movew #0x2700,%/sr\n\t" /* set up status register */
605 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
606 "movec %/a0, %/isp\n\t"
607 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
608 "reset\n\t" /* reset external devices */
609 "jmp %/a0@\n\t" /* jump to the reset vector */
610 ".chip 68k"
611 : : "r" (offset), "a" (rombase) : "a0");
612 }
613
614 /* should never get here */
615 local_irq_enable();
616 printk ("Restart failed. Please restart manually.\n");
617 while(1);
618}
619
620/*
621 * This function translates seconds since 1970 into a proper date.
622 *
623 * Algorithm cribbed from glibc2.1, __offtime().
624 */
625#define SECS_PER_MINUTE (60)
626#define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
627#define SECS_PER_DAY (SECS_PER_HOUR * 24)
628
629static void unmktime(unsigned long time, long offset,
630 int *yearp, int *monp, int *dayp,
631 int *hourp, int *minp, int *secp)
632{
633 /* How many days come before each month (0-12). */
634 static const unsigned short int __mon_yday[2][13] =
635 {
636 /* Normal years. */
637 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
638 /* Leap years. */
639 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
640 };
641 long int days, rem, y, wday, yday;
642 const unsigned short int *ip;
643
644 days = time / SECS_PER_DAY;
645 rem = time % SECS_PER_DAY;
646 rem += offset;
647 while (rem < 0) {
648 rem += SECS_PER_DAY;
649 --days;
650 }
651 while (rem >= SECS_PER_DAY) {
652 rem -= SECS_PER_DAY;
653 ++days;
654 }
655 *hourp = rem / SECS_PER_HOUR;
656 rem %= SECS_PER_HOUR;
657 *minp = rem / SECS_PER_MINUTE;
658 *secp = rem % SECS_PER_MINUTE;
659 /* January 1, 1970 was a Thursday. */
660 wday = (4 + days) % 7; /* Day in the week. Not currently used */
661 if (wday < 0) wday += 7;
662 y = 1970;
663
664#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
665#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
666#define __isleap(year) \
667 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
668
669 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
670 {
671 /* Guess a corrected year, assuming 365 days per year. */
672 long int yg = y + days / 365 - (days % 365 < 0);
673
674 /* Adjust DAYS and Y to match the guessed year. */
675 days -= ((yg - y) * 365
676 + LEAPS_THRU_END_OF (yg - 1)
677 - LEAPS_THRU_END_OF (y - 1));
678 y = yg;
679 }
680 *yearp = y - 1900;
681 yday = days; /* day in the year. Not currently used. */
682 ip = __mon_yday[__isleap(y)];
683 for (y = 11; days < (long int) ip[y]; --y)
684 continue;
685 days -= ip[y];
686 *monp = y;
687 *dayp = days + 1; /* day in the month */
688 return;
689}
690
691/*
692 * Read/write the hardware clock.
693 */
694
695int mac_hwclk(int op, struct rtc_time *t)
696{
697 unsigned long now;
698
699 if (!op) { /* read */
700 switch (macintosh_config->adb_type) {
701 case MAC_ADB_II:
702 case MAC_ADB_IOP:
703 now = via_read_time();
704 break;
705 case MAC_ADB_IISI:
706 now = maciisi_read_time();
707 break;
708 case MAC_ADB_PB1:
709 case MAC_ADB_PB2:
710 now = pmu_read_time();
711 break;
712 case MAC_ADB_CUDA:
713 now = cuda_read_time();
714 break;
715 default:
716 now = 0;
717 }
718
719 t->tm_wday = 0;
720 unmktime(now, 0,
721 &t->tm_year, &t->tm_mon, &t->tm_mday,
722 &t->tm_hour, &t->tm_min, &t->tm_sec);
723#if 0
724 printk("mac_hwclk: read %04d-%02d-%-2d %02d:%02d:%02d\n",
725 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
726 t->tm_hour, t->tm_min, t->tm_sec);
727#endif
728 } else { /* write */
729#if 0
730 printk("mac_hwclk: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
731 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
732 t->tm_hour, t->tm_min, t->tm_sec);
733#endif
734
735 now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
736 t->tm_hour, t->tm_min, t->tm_sec);
737
738 switch (macintosh_config->adb_type) {
739 case MAC_ADB_II:
740 case MAC_ADB_IOP:
741 via_write_time(now);
742 break;
743 case MAC_ADB_CUDA:
744 cuda_write_time(now);
745 break;
746 case MAC_ADB_PB1:
747 case MAC_ADB_PB2:
748 pmu_write_time(now);
749 break;
750 case MAC_ADB_IISI:
751 maciisi_write_time(now);
752 }
753 }
754 return 0;
755}
756
757/*
758 * Set minutes/seconds in the hardware clock
759 */
760
761int mac_set_clock_mmss (unsigned long nowtime)
762{
763 struct rtc_time now;
764
765 mac_hwclk(0, &now);
766 now.tm_sec = nowtime % 60;
767 now.tm_min = (nowtime / 60) % 60;
768 mac_hwclk(1, &now);
769
770 return 0;
771}