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>
30 * Offset between Unix time (1970-based) and Mac time (1904-based). Cuda and PMU
31 * times wrap in 2040. If we need to handle later times, the read_time functions
32 * need to be changed to interpret wrapped times as post-2040.
35 #define RTC_OFFSET 2082844800
37 static void (*rom_reset)(void);
39 #if IS_ENABLED(CONFIG_NVRAM)
40 #ifdef CONFIG_ADB_CUDA
41 static unsigned char cuda_pram_read_byte(int offset)
43 struct adb_request req;
45 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
46 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
53 static void cuda_pram_write_byte(unsigned char data, int offset)
55 struct adb_request req;
57 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
58 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
63 #endif /* CONFIG_ADB_CUDA */
66 static unsigned char pmu_pram_read_byte(int offset)
68 struct adb_request req;
70 if (pmu_request(&req, NULL, 3, PMU_READ_XPRAM,
71 offset & 0xFF, 1) < 0)
73 pmu_wait_complete(&req);
78 static void pmu_pram_write_byte(unsigned char data, int offset)
80 struct adb_request req;
82 if (pmu_request(&req, NULL, 4, PMU_WRITE_XPRAM,
83 offset & 0xFF, 1, data) < 0)
85 pmu_wait_complete(&req);
87 #endif /* CONFIG_ADB_PMU */
88 #endif /* CONFIG_NVRAM */
91 * VIA PRAM/RTC access routines
93 * Must be called with interrupts disabled and
94 * the RTC should be enabled.
97 static __u8 via_rtc_recv(void)
102 reg = via1[vBufB] & ~VIA1B_vRTCClk;
104 /* Set the RTC data line to be an input. */
106 via1[vDirB] &= ~VIA1B_vRTCData;
108 /* The bits of the byte come out in MSB order */
111 for (i = 0 ; i < 8 ; i++) {
113 via1[vBufB] = reg | VIA1B_vRTCClk;
114 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
117 /* Return RTC data line to output state */
119 via1[vDirB] |= VIA1B_vRTCData;
124 static void via_rtc_send(__u8 data)
128 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
130 /* The bits of the byte go in in MSB order */
132 for (i = 0 ; i < 8 ; i++) {
133 bit = data & 0x80? 1 : 0;
135 via1[vBufB] = reg | bit;
136 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
141 * These values can be found in Inside Macintosh vol. III ch. 2
142 * which has a description of the RTC chip in the original Mac.
145 #define RTC_FLG_READ BIT(7)
146 #define RTC_FLG_WRITE_PROTECT BIT(7)
147 #define RTC_CMD_READ(r) (RTC_FLG_READ | (r << 2))
148 #define RTC_CMD_WRITE(r) (r << 2)
149 #define RTC_REG_SECONDS_0 0
150 #define RTC_REG_SECONDS_1 1
151 #define RTC_REG_SECONDS_2 2
152 #define RTC_REG_SECONDS_3 3
153 #define RTC_REG_WRITE_PROTECT 13
156 * Inside Mac has no information about two-byte RTC commands but
157 * the MAME/MESS source code has the essentials.
160 #define RTC_REG_XPRAM 14
161 #define RTC_CMD_XPRAM_READ (RTC_CMD_READ(RTC_REG_XPRAM) << 8)
162 #define RTC_CMD_XPRAM_WRITE (RTC_CMD_WRITE(RTC_REG_XPRAM) << 8)
163 #define RTC_CMD_XPRAM_ARG(a) (((a & 0xE0) << 3) | ((a & 0x1F) << 2))
166 * Execute a VIA PRAM/RTC command. For read commands
167 * data should point to a one-byte buffer for the
168 * resulting data. For write commands it should point
169 * to the data byte to for the command.
171 * This function disables all interrupts while running.
174 static void via_rtc_command(int command, __u8 *data)
179 local_irq_save(flags);
181 /* The least significant bits must be 0b01 according to Inside Mac */
183 command = (command & ~3) | 1;
185 /* Enable the RTC and make sure the strobe line is high */
187 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
189 if (command & 0xFF00) { /* extended (two-byte) command */
190 via_rtc_send((command & 0xFF00) >> 8);
191 via_rtc_send(command & 0xFF);
192 is_read = command & (RTC_FLG_READ << 8);
193 } else { /* one-byte command */
194 via_rtc_send(command);
195 is_read = command & RTC_FLG_READ;
198 *data = via_rtc_recv();
203 /* All done, disable the RTC */
205 via1[vBufB] |= VIA1B_vRTCEnb;
207 local_irq_restore(flags);
210 #if IS_ENABLED(CONFIG_NVRAM)
211 static unsigned char via_pram_read_byte(int offset)
215 via_rtc_command(RTC_CMD_XPRAM_READ | RTC_CMD_XPRAM_ARG(offset), &temp);
220 static void via_pram_write_byte(unsigned char data, int offset)
225 via_rtc_command(RTC_CMD_WRITE(RTC_REG_WRITE_PROTECT), &temp);
228 via_rtc_command(RTC_CMD_XPRAM_WRITE | RTC_CMD_XPRAM_ARG(offset), &temp);
230 temp = 0x55 | RTC_FLG_WRITE_PROTECT;
231 via_rtc_command(RTC_CMD_WRITE(RTC_REG_WRITE_PROTECT), &temp);
233 #endif /* CONFIG_NVRAM */
236 * Return the current time in seconds since January 1, 1904.
238 * This only works on machines with the VIA-based PRAM/RTC, which
239 * is basically any machine with Mac II-style ADB.
242 static time64_t via_read_time(void)
247 } result, last_result;
250 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_0), &last_result.cdata[3]);
251 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_1), &last_result.cdata[2]);
252 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_2), &last_result.cdata[1]);
253 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_3), &last_result.cdata[0]);
256 * The NetBSD guys say to loop until you get the same reading
261 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_0),
263 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_1),
265 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_2),
267 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_3),
270 if (result.idata == last_result.idata)
271 return (time64_t)result.idata - RTC_OFFSET;
276 last_result.idata = result.idata;
279 pr_err("%s: failed to read a stable value; got 0x%08x then 0x%08x\n",
280 __func__, last_result.idata, result.idata);
286 * Set the current time to a number of seconds since January 1, 1904.
288 * This only works on machines with the VIA-based PRAM/RTC, which
289 * is basically any machine with Mac II-style ADB.
292 static void via_set_rtc_time(struct rtc_time *tm)
301 time = mktime64(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
302 tm->tm_hour, tm->tm_min, tm->tm_sec);
304 /* Clear the write protect bit */
307 via_rtc_command(RTC_CMD_WRITE(RTC_REG_WRITE_PROTECT), &temp);
309 data.idata = lower_32_bits(time + RTC_OFFSET);
310 via_rtc_command(RTC_CMD_WRITE(RTC_REG_SECONDS_0), &data.cdata[3]);
311 via_rtc_command(RTC_CMD_WRITE(RTC_REG_SECONDS_1), &data.cdata[2]);
312 via_rtc_command(RTC_CMD_WRITE(RTC_REG_SECONDS_2), &data.cdata[1]);
313 via_rtc_command(RTC_CMD_WRITE(RTC_REG_SECONDS_3), &data.cdata[0]);
315 /* Set the write protect bit */
317 temp = 0x55 | RTC_FLG_WRITE_PROTECT;
318 via_rtc_command(RTC_CMD_WRITE(RTC_REG_WRITE_PROTECT), &temp);
321 static void via_shutdown(void)
324 via2[rBufB] &= ~0x04;
326 /* Direction of vDirB is output */
328 /* Send a value of 0 on that line */
329 via2[vBufB] &= ~0x04;
334 static void oss_shutdown(void)
336 oss->rom_ctrl = OSS_POWEROFF;
339 #ifdef CONFIG_ADB_CUDA
340 static void cuda_restart(void)
342 struct adb_request req;
344 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
346 while (!req.complete)
350 static void cuda_shutdown(void)
352 struct adb_request req;
354 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
357 /* Avoid infinite polling loop when PSU is not under Cuda control */
358 switch (macintosh_config->ident) {
361 case MAC_MODEL_Q605_ACC:
363 case MAC_MODEL_P475F:
367 while (!req.complete)
370 #endif /* CONFIG_ADB_CUDA */
373 *-------------------------------------------------------------------
374 * Below this point are the generic routines; they'll dispatch to the
375 * correct routine for the hardware on which we're running.
376 *-------------------------------------------------------------------
379 #if IS_ENABLED(CONFIG_NVRAM)
380 unsigned char mac_pram_read_byte(int addr)
382 switch (macintosh_config->adb_type) {
386 return via_pram_read_byte(addr);
387 #ifdef CONFIG_ADB_CUDA
390 return cuda_pram_read_byte(addr);
392 #ifdef CONFIG_ADB_PMU
394 return pmu_pram_read_byte(addr);
401 void mac_pram_write_byte(unsigned char val, int addr)
403 switch (macintosh_config->adb_type) {
407 via_pram_write_byte(val, addr);
409 #ifdef CONFIG_ADB_CUDA
412 cuda_pram_write_byte(val, addr);
415 #ifdef CONFIG_ADB_PMU
417 pmu_pram_write_byte(val, addr);
425 ssize_t mac_pram_get_size(void)
429 #endif /* CONFIG_NVRAM */
431 void mac_poweroff(void)
435 } else if (macintosh_config->adb_type == MAC_ADB_II) {
437 #ifdef CONFIG_ADB_CUDA
438 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
439 macintosh_config->adb_type == MAC_ADB_CUDA) {
442 #ifdef CONFIG_ADB_PMU
443 } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
448 pr_crit("It is now safe to turn off your Macintosh.\n");
455 if (macintosh_config->adb_type == MAC_ADB_II &&
456 macintosh_config->ident != MAC_MODEL_SE30) {
457 /* need ROMBASE in booter */
458 /* indeed, plus need to MAP THE ROM !! */
460 if (mac_bi_data.rombase == 0)
461 mac_bi_data.rombase = 0x40800000;
464 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
468 #ifdef CONFIG_ADB_CUDA
469 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
470 macintosh_config->adb_type == MAC_ADB_CUDA) {
473 #ifdef CONFIG_ADB_PMU
474 } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
477 } else if (CPU_IS_030) {
479 /* 030-specific reset routine. The idea is general, but the
480 * specific registers to reset are '030-specific. Until I
481 * have a non-030 machine, I can't test anything else.
482 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
485 unsigned long rombase = 0x40000000;
487 /* make a 1-to-1 mapping, using the transparent tran. reg. */
488 unsigned long virt = (unsigned long) mac_reset;
489 unsigned long phys = virt_to_phys(mac_reset);
490 unsigned long addr = (phys&0xFF000000)|0x8777;
491 unsigned long offset = phys-virt;
493 local_irq_disable(); /* lets not screw this up, ok? */
494 __asm__ __volatile__(".chip 68030\n\t"
498 /* Now jump to physical address so we can disable MMU */
499 __asm__ __volatile__(
501 "lea %/pc@(1f),%/a0\n\t"
502 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
505 "jmp %/a0@\n\t" /* jump into physical memory */
506 "0:.long 0\n\t" /* a constant zero. */
507 /* OK. Now reset everything and jump to reset vector. */
509 "lea %/pc@(0b),%/a0\n\t"
510 "pmove %/a0@, %/tc\n\t" /* disable mmu */
511 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
512 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
514 "movec %/a0, %/vbr\n\t" /* clear vector base register */
515 "movec %/a0, %/cacr\n\t" /* disable caches */
516 "movel #0x0808,%/a0\n\t"
517 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
518 "movew #0x2700,%/sr\n\t" /* set up status register */
519 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
520 "movec %/a0, %/isp\n\t"
521 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
522 "reset\n\t" /* reset external devices */
523 "jmp %/a0@\n\t" /* jump to the reset vector */
525 : : "r" (offset), "a" (rombase) : "a0");
528 /* should never get here */
529 pr_crit("Restart failed. Please restart manually.\n");
535 * This function translates seconds since 1970 into a proper date.
537 * Algorithm cribbed from glibc2.1, __offtime().
539 * This is roughly same as rtc_time64_to_tm(), which we should probably
540 * use here, but it's only available when CONFIG_RTC_LIB is enabled.
542 #define SECS_PER_MINUTE (60)
543 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
544 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
546 static void unmktime(time64_t time, long offset,
547 int *yearp, int *monp, int *dayp,
548 int *hourp, int *minp, int *secp)
550 /* How many days come before each month (0-12). */
551 static const unsigned short int __mon_yday[2][13] =
554 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
556 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
558 int days, rem, y, wday, yday;
559 const unsigned short int *ip;
561 days = div_u64_rem(time, SECS_PER_DAY, &rem);
567 while (rem >= SECS_PER_DAY) {
571 *hourp = rem / SECS_PER_HOUR;
572 rem %= SECS_PER_HOUR;
573 *minp = rem / SECS_PER_MINUTE;
574 *secp = rem % SECS_PER_MINUTE;
575 /* January 1, 1970 was a Thursday. */
576 wday = (4 + days) % 7; /* Day in the week. Not currently used */
577 if (wday < 0) wday += 7;
580 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
581 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
582 #define __isleap(year) \
583 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
585 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
587 /* Guess a corrected year, assuming 365 days per year. */
588 long int yg = y + days / 365 - (days % 365 < 0);
590 /* Adjust DAYS and Y to match the guessed year. */
591 days -= (yg - y) * 365 +
592 LEAPS_THRU_END_OF(yg - 1) - LEAPS_THRU_END_OF(y - 1);
596 yday = days; /* day in the year. Not currently used. */
597 ip = __mon_yday[__isleap(y)];
598 for (y = 11; days < (long int) ip[y]; --y)
602 *dayp = days + 1; /* day in the month */
607 * Read/write the hardware clock.
610 int mac_hwclk(int op, struct rtc_time *t)
614 if (!op) { /* read */
615 switch (macintosh_config->adb_type) {
619 now = via_read_time();
621 #ifdef CONFIG_ADB_CUDA
624 now = cuda_get_time();
627 #ifdef CONFIG_ADB_PMU
629 now = pmu_get_time();
638 &t->tm_year, &t->tm_mon, &t->tm_mday,
639 &t->tm_hour, &t->tm_min, &t->tm_sec);
640 pr_debug("%s: read %ptR\n", __func__, t);
642 pr_debug("%s: tried to write %ptR\n", __func__, t);
644 switch (macintosh_config->adb_type) {
650 #ifdef CONFIG_ADB_CUDA
653 cuda_set_rtc_time(t);
656 #ifdef CONFIG_ADB_PMU