1 // SPDX-License-Identifier: GPL-2.0
3 * Miscellaneous Mac68K-specific stuff
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>
15 #include <linux/adb.h>
16 #include <linux/cuda.h>
17 #include <linux/pmu.h>
19 #include <linux/uaccess.h>
21 #include <asm/segment.h>
22 #include <asm/setup.h>
23 #include <asm/macintosh.h>
24 #include <asm/mac_via.h>
25 #include <asm/mac_oss.h>
27 #include <asm/machdep.h>
29 /* Offset between Unix time (1970-based) and Mac time (1904-based) */
31 #define RTC_OFFSET 2082844800
33 static void (*rom_reset)(void);
35 #ifdef CONFIG_ADB_CUDA
36 static long cuda_read_time(void)
38 struct adb_request req;
41 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
46 time = (req.reply[3] << 24) | (req.reply[4] << 16)
47 | (req.reply[5] << 8) | req.reply[6];
48 return time - RTC_OFFSET;
51 static void cuda_write_time(long data)
53 struct adb_request req;
55 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
56 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
57 (data >> 8) & 0xFF, data & 0xFF) < 0)
63 static __u8 cuda_read_pram(int offset)
65 struct adb_request req;
66 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
67 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
74 static void cuda_write_pram(int offset, __u8 data)
76 struct adb_request req;
77 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
78 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
84 #define cuda_read_time() 0
85 #define cuda_write_time(n)
86 #define cuda_read_pram NULL
87 #define cuda_write_pram NULL
90 #ifdef CONFIG_ADB_PMU68K
91 static long pmu_read_time(void)
93 struct adb_request req;
96 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
101 time = (req.reply[1] << 24) | (req.reply[2] << 16)
102 | (req.reply[3] << 8) | req.reply[4];
103 return time - RTC_OFFSET;
106 static void pmu_write_time(long data)
108 struct adb_request req;
110 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
111 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
112 (data >> 8) & 0xFF, data & 0xFF) < 0)
114 while (!req.complete)
118 static __u8 pmu_read_pram(int offset)
120 struct adb_request req;
121 if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM,
122 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
124 while (!req.complete)
129 static void pmu_write_pram(int offset, __u8 data)
131 struct adb_request req;
132 if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM,
133 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
135 while (!req.complete)
139 #define pmu_read_time() 0
140 #define pmu_write_time(n)
141 #define pmu_read_pram NULL
142 #define pmu_write_pram NULL
146 * VIA PRAM/RTC access routines
148 * Must be called with interrupts disabled and
149 * the RTC should be enabled.
152 static __u8 via_pram_readbyte(void)
157 reg = via1[vBufB] & ~VIA1B_vRTCClk;
159 /* Set the RTC data line to be an input. */
161 via1[vDirB] &= ~VIA1B_vRTCData;
163 /* The bits of the byte come out in MSB order */
166 for (i = 0 ; i < 8 ; i++) {
168 via1[vBufB] = reg | VIA1B_vRTCClk;
169 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
172 /* Return RTC data line to output state */
174 via1[vDirB] |= VIA1B_vRTCData;
179 static void via_pram_writebyte(__u8 data)
183 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
185 /* The bits of the byte go in in MSB order */
187 for (i = 0 ; i < 8 ; i++) {
188 bit = data & 0x80? 1 : 0;
190 via1[vBufB] = reg | bit;
191 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
196 * Execute a VIA PRAM/RTC command. For read commands
197 * data should point to a one-byte buffer for the
198 * resulting data. For write commands it should point
199 * to the data byte to for the command.
201 * This function disables all interrupts while running.
204 static void via_pram_command(int command, __u8 *data)
209 local_irq_save(flags);
211 /* Enable the RTC and make sure the strobe line is high */
213 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
215 if (command & 0xFF00) { /* extended (two-byte) command */
216 via_pram_writebyte((command & 0xFF00) >> 8);
217 via_pram_writebyte(command & 0xFF);
218 is_read = command & 0x8000;
219 } else { /* one-byte command */
220 via_pram_writebyte(command);
221 is_read = command & 0x80;
224 *data = via_pram_readbyte();
226 via_pram_writebyte(*data);
229 /* All done, disable the RTC */
231 via1[vBufB] |= VIA1B_vRTCEnb;
233 local_irq_restore(flags);
236 static __u8 via_read_pram(int offset)
241 static void via_write_pram(int offset, __u8 data)
246 * Return the current time in seconds since January 1, 1904.
248 * This only works on machines with the VIA-based PRAM/RTC, which
249 * is basically any machine with Mac II-style ADB.
252 static long via_read_time(void)
257 } result, last_result;
260 via_pram_command(0x81, &last_result.cdata[3]);
261 via_pram_command(0x85, &last_result.cdata[2]);
262 via_pram_command(0x89, &last_result.cdata[1]);
263 via_pram_command(0x8D, &last_result.cdata[0]);
266 * The NetBSD guys say to loop until you get the same reading
271 via_pram_command(0x81, &result.cdata[3]);
272 via_pram_command(0x85, &result.cdata[2]);
273 via_pram_command(0x89, &result.cdata[1]);
274 via_pram_command(0x8D, &result.cdata[0]);
276 if (result.idata == last_result.idata)
277 return result.idata - RTC_OFFSET;
282 last_result.idata = result.idata;
285 pr_err("via_read_time: failed to read a stable value; got 0x%08lx then 0x%08lx\n",
286 last_result.idata, result.idata);
292 * Set the current time to a number of seconds since January 1, 1904.
294 * This only works on machines with the VIA-based PRAM/RTC, which
295 * is basically any machine with Mac II-style ADB.
298 static void via_write_time(long time)
306 /* Clear the write protect bit */
309 via_pram_command(0x35, &temp);
311 data.idata = time + RTC_OFFSET;
312 via_pram_command(0x01, &data.cdata[3]);
313 via_pram_command(0x05, &data.cdata[2]);
314 via_pram_command(0x09, &data.cdata[1]);
315 via_pram_command(0x0D, &data.cdata[0]);
317 /* Set the write protect bit */
320 via_pram_command(0x35, &temp);
323 static void via_shutdown(void)
326 via2[rBufB] &= ~0x04;
328 /* Direction of vDirB is output */
330 /* Send a value of 0 on that line */
331 via2[vBufB] &= ~0x04;
337 * FIXME: not sure how this is supposed to work exactly...
340 static void oss_shutdown(void)
342 oss->rom_ctrl = OSS_POWEROFF;
345 #ifdef CONFIG_ADB_CUDA
347 static void cuda_restart(void)
349 struct adb_request req;
350 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
352 while (!req.complete)
356 static void cuda_shutdown(void)
358 struct adb_request req;
359 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
362 /* Avoid infinite polling loop when PSU is not under Cuda control */
363 switch (macintosh_config->ident) {
366 case MAC_MODEL_Q605_ACC:
368 case MAC_MODEL_P475F:
372 while (!req.complete)
376 #endif /* CONFIG_ADB_CUDA */
378 #ifdef CONFIG_ADB_PMU68K
380 void pmu_restart(void)
382 struct adb_request req;
383 if (pmu_request(&req, NULL,
384 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
386 while (!req.complete)
388 if (pmu_request(&req, NULL, 1, PMU_RESET) < 0)
390 while (!req.complete)
394 void pmu_shutdown(void)
396 struct adb_request req;
397 if (pmu_request(&req, NULL,
398 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
400 while (!req.complete)
402 if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0)
404 while (!req.complete)
411 *-------------------------------------------------------------------
412 * Below this point are the generic routines; they'll dispatch to the
413 * correct routine for the hardware on which we're running.
414 *-------------------------------------------------------------------
417 void mac_pram_read(int offset, __u8 *buffer, int len)
422 switch(macintosh_config->adb_type) {
425 func = pmu_read_pram; break;
428 func = cuda_read_pram; break;
430 func = via_read_pram;
434 for (i = 0 ; i < len ; i++) {
435 buffer[i] = (*func)(offset++);
439 void mac_pram_write(int offset, __u8 *buffer, int len)
441 void (*func)(int, __u8);
444 switch(macintosh_config->adb_type) {
447 func = pmu_write_pram; break;
450 func = cuda_write_pram; break;
452 func = via_write_pram;
456 for (i = 0 ; i < len ; i++) {
457 (*func)(offset++, buffer[i]);
461 void mac_poweroff(void)
465 } else if (macintosh_config->adb_type == MAC_ADB_II) {
467 #ifdef CONFIG_ADB_CUDA
468 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
469 macintosh_config->adb_type == MAC_ADB_CUDA) {
472 #ifdef CONFIG_ADB_PMU68K
473 } else if (macintosh_config->adb_type == MAC_ADB_PB1
474 || macintosh_config->adb_type == MAC_ADB_PB2) {
479 pr_crit("It is now safe to turn off your Macintosh.\n");
486 if (macintosh_config->adb_type == MAC_ADB_II) {
489 /* need ROMBASE in booter */
490 /* indeed, plus need to MAP THE ROM !! */
492 if (mac_bi_data.rombase == 0)
493 mac_bi_data.rombase = 0x40800000;
496 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
498 if (macintosh_config->ident == MAC_MODEL_SE30) {
500 * MSch: Machines known to crash on ROM reset ...
503 local_irq_save(flags);
507 local_irq_restore(flags);
509 #ifdef CONFIG_ADB_CUDA
510 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
511 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) {
519 } else if (CPU_IS_030) {
521 /* 030-specific reset routine. The idea is general, but the
522 * specific registers to reset are '030-specific. Until I
523 * have a non-030 machine, I can't test anything else.
524 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
527 unsigned long rombase = 0x40000000;
529 /* make a 1-to-1 mapping, using the transparent tran. reg. */
530 unsigned long virt = (unsigned long) mac_reset;
531 unsigned long phys = virt_to_phys(mac_reset);
532 unsigned long addr = (phys&0xFF000000)|0x8777;
533 unsigned long offset = phys-virt;
534 local_irq_disable(); /* lets not screw this up, ok? */
535 __asm__ __volatile__(".chip 68030\n\t"
539 /* Now jump to physical address so we can disable MMU */
540 __asm__ __volatile__(
542 "lea %/pc@(1f),%/a0\n\t"
543 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
546 "jmp %/a0@\n\t" /* jump into physical memory */
547 "0:.long 0\n\t" /* a constant zero. */
548 /* OK. Now reset everything and jump to reset vector. */
550 "lea %/pc@(0b),%/a0\n\t"
551 "pmove %/a0@, %/tc\n\t" /* disable mmu */
552 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
553 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
555 "movec %/a0, %/vbr\n\t" /* clear vector base register */
556 "movec %/a0, %/cacr\n\t" /* disable caches */
557 "movel #0x0808,%/a0\n\t"
558 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
559 "movew #0x2700,%/sr\n\t" /* set up status register */
560 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
561 "movec %/a0, %/isp\n\t"
562 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
563 "reset\n\t" /* reset external devices */
564 "jmp %/a0@\n\t" /* jump to the reset vector */
566 : : "r" (offset), "a" (rombase) : "a0");
569 /* should never get here */
570 pr_crit("Restart failed. Please restart manually.\n");
576 * This function translates seconds since 1970 into a proper date.
578 * Algorithm cribbed from glibc2.1, __offtime().
580 #define SECS_PER_MINUTE (60)
581 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
582 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
584 static void unmktime(unsigned long time, long offset,
585 int *yearp, int *monp, int *dayp,
586 int *hourp, int *minp, int *secp)
588 /* How many days come before each month (0-12). */
589 static const unsigned short int __mon_yday[2][13] =
592 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
594 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
596 long int days, rem, y, wday, yday;
597 const unsigned short int *ip;
599 days = time / SECS_PER_DAY;
600 rem = time % SECS_PER_DAY;
606 while (rem >= SECS_PER_DAY) {
610 *hourp = rem / SECS_PER_HOUR;
611 rem %= SECS_PER_HOUR;
612 *minp = rem / SECS_PER_MINUTE;
613 *secp = rem % SECS_PER_MINUTE;
614 /* January 1, 1970 was a Thursday. */
615 wday = (4 + days) % 7; /* Day in the week. Not currently used */
616 if (wday < 0) wday += 7;
619 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
620 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
621 #define __isleap(year) \
622 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
624 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
626 /* Guess a corrected year, assuming 365 days per year. */
627 long int yg = y + days / 365 - (days % 365 < 0);
629 /* Adjust DAYS and Y to match the guessed year. */
630 days -= ((yg - y) * 365
631 + LEAPS_THRU_END_OF (yg - 1)
632 - LEAPS_THRU_END_OF (y - 1));
636 yday = days; /* day in the year. Not currently used. */
637 ip = __mon_yday[__isleap(y)];
638 for (y = 11; days < (long int) ip[y]; --y)
642 *dayp = days + 1; /* day in the month */
647 * Read/write the hardware clock.
650 int mac_hwclk(int op, struct rtc_time *t)
654 if (!op) { /* read */
655 switch (macintosh_config->adb_type) {
658 now = via_read_time();
662 now = pmu_read_time();
666 now = cuda_read_time();
674 &t->tm_year, &t->tm_mon, &t->tm_mday,
675 &t->tm_hour, &t->tm_min, &t->tm_sec);
676 pr_debug("%s: read %04d-%02d-%-2d %02d:%02d:%02d\n",
677 __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
678 t->tm_hour, t->tm_min, t->tm_sec);
680 pr_debug("%s: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
681 __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
682 t->tm_hour, t->tm_min, t->tm_sec);
684 now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
685 t->tm_hour, t->tm_min, t->tm_sec);
687 switch (macintosh_config->adb_type) {
694 cuda_write_time(now);
706 * Set minutes/seconds in the hardware clock
709 int mac_set_clock_mmss (unsigned long nowtime)
714 now.tm_sec = nowtime % 60;
715 now.tm_min = (nowtime / 60) % 60;