Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / m68k / mac / misc.c
blob4956edaac926d112675b41ead4378e07b1910b2f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Miscellaneous Mac68K-specific stuff
4 */
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>
15 #include <linux/adb.h>
16 #include <linux/cuda.h>
17 #include <linux/pmu.h>
19 #include <linux/uaccess.h>
20 #include <asm/io.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;
39 long time;
41 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
42 return 0;
43 while (!req.complete)
44 cuda_poll();
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;
54 data += RTC_OFFSET;
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)
58 return;
59 while (!req.complete)
60 cuda_poll();
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)
68 return 0;
69 while (!req.complete)
70 cuda_poll();
71 return req.reply[3];
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)
79 return;
80 while (!req.complete)
81 cuda_poll();
83 #else
84 #define cuda_read_time() 0
85 #define cuda_write_time(n)
86 #define cuda_read_pram NULL
87 #define cuda_write_pram NULL
88 #endif
90 #ifdef CONFIG_ADB_PMU68K
91 static long pmu_read_time(void)
93 struct adb_request req;
94 long time;
96 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
97 return 0;
98 while (!req.complete)
99 pmu_poll();
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;
109 data += RTC_OFFSET;
110 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
111 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
112 (data >> 8) & 0xFF, data & 0xFF) < 0)
113 return;
114 while (!req.complete)
115 pmu_poll();
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)
123 return 0;
124 while (!req.complete)
125 pmu_poll();
126 return req.reply[3];
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)
134 return;
135 while (!req.complete)
136 pmu_poll();
138 #else
139 #define pmu_read_time() 0
140 #define pmu_write_time(n)
141 #define pmu_read_pram NULL
142 #define pmu_write_pram NULL
143 #endif
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)
154 int i,reg;
155 __u8 data;
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 */
165 data = 0;
166 for (i = 0 ; i < 8 ; i++) {
167 via1[vBufB] = reg;
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;
176 return data;
179 static void via_pram_writebyte(__u8 data)
181 int i,reg,bit;
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;
189 data <<= 1;
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)
206 unsigned long flags;
207 int is_read;
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;
223 if (is_read) {
224 *data = via_pram_readbyte();
225 } else {
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)
238 return 0;
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)
254 union {
255 __u8 cdata[4];
256 long idata;
257 } result, last_result;
258 int count = 1;
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
267 * twice in a row.
270 while (1) {
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;
279 if (++count > 10)
280 break;
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);
288 return 0;
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)
300 union {
301 __u8 cdata[4];
302 long idata;
303 } data;
304 __u8 temp;
306 /* Clear the write protect bit */
308 temp = 0x55;
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 */
319 temp = 0xD5;
320 via_pram_command(0x35, &temp);
323 static void via_shutdown(void)
325 if (rbv_present) {
326 via2[rBufB] &= ~0x04;
327 } else {
328 /* Direction of vDirB is output */
329 via2[vDirB] |= 0x04;
330 /* Send a value of 0 on that line */
331 via2[vBufB] &= ~0x04;
332 mdelay(1000);
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)
351 return;
352 while (!req.complete)
353 cuda_poll();
356 static void cuda_shutdown(void)
358 struct adb_request req;
359 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
360 return;
362 /* Avoid infinite polling loop when PSU is not under Cuda control */
363 switch (macintosh_config->ident) {
364 case MAC_MODEL_C660:
365 case MAC_MODEL_Q605:
366 case MAC_MODEL_Q605_ACC:
367 case MAC_MODEL_P475:
368 case MAC_MODEL_P475F:
369 return;
372 while (!req.complete)
373 cuda_poll();
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)
385 return;
386 while (!req.complete)
387 pmu_poll();
388 if (pmu_request(&req, NULL, 1, PMU_RESET) < 0)
389 return;
390 while (!req.complete)
391 pmu_poll();
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)
399 return;
400 while (!req.complete)
401 pmu_poll();
402 if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0)
403 return;
404 while (!req.complete)
405 pmu_poll();
408 #endif
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)
419 __u8 (*func)(int);
420 int i;
422 switch(macintosh_config->adb_type) {
423 case MAC_ADB_PB1:
424 case MAC_ADB_PB2:
425 func = pmu_read_pram; break;
426 case MAC_ADB_EGRET:
427 case MAC_ADB_CUDA:
428 func = cuda_read_pram; break;
429 default:
430 func = via_read_pram;
432 if (!func)
433 return;
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);
442 int i;
444 switch(macintosh_config->adb_type) {
445 case MAC_ADB_PB1:
446 case MAC_ADB_PB2:
447 func = pmu_write_pram; break;
448 case MAC_ADB_EGRET:
449 case MAC_ADB_CUDA:
450 func = cuda_write_pram; break;
451 default:
452 func = via_write_pram;
454 if (!func)
455 return;
456 for (i = 0 ; i < len ; i++) {
457 (*func)(offset++, buffer[i]);
461 void mac_poweroff(void)
463 if (oss_present) {
464 oss_shutdown();
465 } else if (macintosh_config->adb_type == MAC_ADB_II) {
466 via_shutdown();
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_shutdown();
471 #endif
472 #ifdef CONFIG_ADB_PMU68K
473 } else if (macintosh_config->adb_type == MAC_ADB_PB1
474 || macintosh_config->adb_type == MAC_ADB_PB2) {
475 pmu_shutdown();
476 #endif
479 pr_crit("It is now safe to turn off your Macintosh.\n");
480 local_irq_disable();
481 while(1);
484 void mac_reset(void)
486 if (macintosh_config->adb_type == MAC_ADB_II) {
487 unsigned long flags;
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;
495 /* works on some */
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 ...
502 } else {
503 local_irq_save(flags);
505 rom_reset();
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) {
512 cuda_restart();
513 #endif
514 #ifdef CONFIG_ADB_PMU68K
515 } else if (macintosh_config->adb_type == MAC_ADB_PB1
516 || macintosh_config->adb_type == MAC_ADB_PB2) {
517 pmu_restart();
518 #endif
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"
536 "pmove %0,%/tt0\n\t"
537 ".chip 68k"
538 : : "m" (addr));
539 /* Now jump to physical address so we can disable MMU */
540 __asm__ __volatile__(
541 ".chip 68030\n\t"
542 "lea %/pc@(1f),%/a0\n\t"
543 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
544 "addl %0,%/sp\n\t"
545 "pflusha\n\t"
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. */
549 "1:\n\t"
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 */
554 "movel #0, %/a0\n\t"
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 */
565 ".chip 68k"
566 : : "r" (offset), "a" (rombase) : "a0");
569 /* should never get here */
570 pr_crit("Restart failed. Please restart manually.\n");
571 local_irq_disable();
572 while(1);
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] =
591 /* Normal years. */
592 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
593 /* Leap years. */
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;
601 rem += offset;
602 while (rem < 0) {
603 rem += SECS_PER_DAY;
604 --days;
606 while (rem >= SECS_PER_DAY) {
607 rem -= SECS_PER_DAY;
608 ++days;
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;
617 y = 1970;
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));
633 y = yg;
635 *yearp = y - 1900;
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)
639 continue;
640 days -= ip[y];
641 *monp = y;
642 *dayp = days + 1; /* day in the month */
643 return;
647 * Read/write the hardware clock.
650 int mac_hwclk(int op, struct rtc_time *t)
652 unsigned long now;
654 if (!op) { /* read */
655 switch (macintosh_config->adb_type) {
656 case MAC_ADB_II:
657 case MAC_ADB_IOP:
658 now = via_read_time();
659 break;
660 case MAC_ADB_PB1:
661 case MAC_ADB_PB2:
662 now = pmu_read_time();
663 break;
664 case MAC_ADB_EGRET:
665 case MAC_ADB_CUDA:
666 now = cuda_read_time();
667 break;
668 default:
669 now = 0;
672 t->tm_wday = 0;
673 unmktime(now, 0,
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);
679 } else { /* write */
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) {
688 case MAC_ADB_II:
689 case MAC_ADB_IOP:
690 via_write_time(now);
691 break;
692 case MAC_ADB_EGRET:
693 case MAC_ADB_CUDA:
694 cuda_write_time(now);
695 break;
696 case MAC_ADB_PB1:
697 case MAC_ADB_PB2:
698 pmu_write_time(now);
699 break;
702 return 0;
706 * Set minutes/seconds in the hardware clock
709 int mac_set_clock_mmss (unsigned long nowtime)
711 struct rtc_time now;
713 mac_hwclk(0, &now);
714 now.tm_sec = nowtime % 60;
715 now.tm_min = (nowtime / 60) % 60;
716 mac_hwclk(1, &now);
718 return 0;