hugetlb: introduce generic version of hugetlb_free_pgd_range
[linux/fpc-iii.git] / arch / m68k / mac / misc.c
blob1b083c500b9a170beb6c030d882f9d9f675369c5
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>
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 #ifdef CONFIG_ADB_CUDA
40 static time64_t cuda_read_time(void)
42 struct adb_request req;
43 time64_t time;
45 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
46 return 0;
47 while (!req.complete)
48 cuda_poll();
50 time = (u32)((req.reply[3] << 24) | (req.reply[4] << 16) |
51 (req.reply[5] << 8) | req.reply[6]);
53 return time - RTC_OFFSET;
56 static void cuda_write_time(time64_t time)
58 struct adb_request req;
59 u32 data = lower_32_bits(time + RTC_OFFSET);
61 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
62 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
63 (data >> 8) & 0xFF, data & 0xFF) < 0)
64 return;
65 while (!req.complete)
66 cuda_poll();
69 static __u8 cuda_read_pram(int offset)
71 struct adb_request req;
73 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
74 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
75 return 0;
76 while (!req.complete)
77 cuda_poll();
78 return req.reply[3];
81 static void cuda_write_pram(int offset, __u8 data)
83 struct adb_request req;
85 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
86 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
87 return;
88 while (!req.complete)
89 cuda_poll();
91 #endif /* CONFIG_ADB_CUDA */
93 #ifdef CONFIG_ADB_PMU
94 static time64_t pmu_read_time(void)
96 struct adb_request req;
97 time64_t time;
99 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
100 return 0;
101 pmu_wait_complete(&req);
103 time = (u32)((req.reply[0] << 24) | (req.reply[1] << 16) |
104 (req.reply[2] << 8) | req.reply[3]);
106 return time - RTC_OFFSET;
109 static void pmu_write_time(time64_t time)
111 struct adb_request req;
112 u32 data = lower_32_bits(time + RTC_OFFSET);
114 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
115 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
116 (data >> 8) & 0xFF, data & 0xFF) < 0)
117 return;
118 pmu_wait_complete(&req);
121 static __u8 pmu_read_pram(int offset)
123 struct adb_request req;
125 if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM,
126 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
127 return 0;
128 while (!req.complete)
129 pmu_poll();
130 return req.reply[3];
133 static void pmu_write_pram(int offset, __u8 data)
135 struct adb_request req;
137 if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM,
138 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
139 return;
140 while (!req.complete)
141 pmu_poll();
143 #endif /* CONFIG_ADB_PMU */
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 time64_t via_read_time(void)
254 union {
255 __u8 cdata[4];
256 __u32 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 (time64_t)result.idata - RTC_OFFSET;
279 if (++count > 10)
280 break;
282 last_result.idata = result.idata;
285 pr_err("%s: failed to read a stable value; got 0x%08x then 0x%08x\n",
286 __func__, 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(time64_t time)
300 union {
301 __u8 cdata[4];
302 __u32 idata;
303 } data;
304 __u8 temp;
306 /* Clear the write protect bit */
308 temp = 0x55;
309 via_pram_command(0x35, &temp);
311 data.idata = lower_32_bits(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);
336 static void oss_shutdown(void)
338 oss->rom_ctrl = OSS_POWEROFF;
341 #ifdef CONFIG_ADB_CUDA
342 static void cuda_restart(void)
344 struct adb_request req;
346 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
347 return;
348 while (!req.complete)
349 cuda_poll();
352 static void cuda_shutdown(void)
354 struct adb_request req;
356 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
357 return;
359 /* Avoid infinite polling loop when PSU is not under Cuda control */
360 switch (macintosh_config->ident) {
361 case MAC_MODEL_C660:
362 case MAC_MODEL_Q605:
363 case MAC_MODEL_Q605_ACC:
364 case MAC_MODEL_P475:
365 case MAC_MODEL_P475F:
366 return;
369 while (!req.complete)
370 cuda_poll();
372 #endif /* CONFIG_ADB_CUDA */
375 *-------------------------------------------------------------------
376 * Below this point are the generic routines; they'll dispatch to the
377 * correct routine for the hardware on which we're running.
378 *-------------------------------------------------------------------
381 void mac_pram_read(int offset, __u8 *buffer, int len)
383 __u8 (*func)(int);
384 int i;
386 switch (macintosh_config->adb_type) {
387 case MAC_ADB_IOP:
388 case MAC_ADB_II:
389 case MAC_ADB_PB1:
390 func = via_read_pram;
391 break;
392 #ifdef CONFIG_ADB_CUDA
393 case MAC_ADB_EGRET:
394 case MAC_ADB_CUDA:
395 func = cuda_read_pram;
396 break;
397 #endif
398 #ifdef CONFIG_ADB_PMU
399 case MAC_ADB_PB2:
400 func = pmu_read_pram;
401 break;
402 #endif
403 default:
404 return;
406 for (i = 0 ; i < len ; i++) {
407 buffer[i] = (*func)(offset++);
411 void mac_pram_write(int offset, __u8 *buffer, int len)
413 void (*func)(int, __u8);
414 int i;
416 switch (macintosh_config->adb_type) {
417 case MAC_ADB_IOP:
418 case MAC_ADB_II:
419 case MAC_ADB_PB1:
420 func = via_write_pram;
421 break;
422 #ifdef CONFIG_ADB_CUDA
423 case MAC_ADB_EGRET:
424 case MAC_ADB_CUDA:
425 func = cuda_write_pram;
426 break;
427 #endif
428 #ifdef CONFIG_ADB_PMU
429 case MAC_ADB_PB2:
430 func = pmu_write_pram;
431 break;
432 #endif
433 default:
434 return;
436 for (i = 0 ; i < len ; i++) {
437 (*func)(offset++, buffer[i]);
441 void mac_poweroff(void)
443 if (oss_present) {
444 oss_shutdown();
445 } else if (macintosh_config->adb_type == MAC_ADB_II) {
446 via_shutdown();
447 #ifdef CONFIG_ADB_CUDA
448 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
449 macintosh_config->adb_type == MAC_ADB_CUDA) {
450 cuda_shutdown();
451 #endif
452 #ifdef CONFIG_ADB_PMU
453 } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
454 pmu_shutdown();
455 #endif
458 pr_crit("It is now safe to turn off your Macintosh.\n");
459 local_irq_disable();
460 while(1);
463 void mac_reset(void)
465 if (macintosh_config->adb_type == MAC_ADB_II) {
466 unsigned long flags;
468 /* need ROMBASE in booter */
469 /* indeed, plus need to MAP THE ROM !! */
471 if (mac_bi_data.rombase == 0)
472 mac_bi_data.rombase = 0x40800000;
474 /* works on some */
475 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
477 if (macintosh_config->ident == MAC_MODEL_SE30) {
479 * MSch: Machines known to crash on ROM reset ...
481 } else {
482 local_irq_save(flags);
484 rom_reset();
486 local_irq_restore(flags);
488 #ifdef CONFIG_ADB_CUDA
489 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
490 macintosh_config->adb_type == MAC_ADB_CUDA) {
491 cuda_restart();
492 #endif
493 #ifdef CONFIG_ADB_PMU
494 } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
495 pmu_restart();
496 #endif
497 } else if (CPU_IS_030) {
499 /* 030-specific reset routine. The idea is general, but the
500 * specific registers to reset are '030-specific. Until I
501 * have a non-030 machine, I can't test anything else.
502 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
505 unsigned long rombase = 0x40000000;
507 /* make a 1-to-1 mapping, using the transparent tran. reg. */
508 unsigned long virt = (unsigned long) mac_reset;
509 unsigned long phys = virt_to_phys(mac_reset);
510 unsigned long addr = (phys&0xFF000000)|0x8777;
511 unsigned long offset = phys-virt;
513 local_irq_disable(); /* lets not screw this up, ok? */
514 __asm__ __volatile__(".chip 68030\n\t"
515 "pmove %0,%/tt0\n\t"
516 ".chip 68k"
517 : : "m" (addr));
518 /* Now jump to physical address so we can disable MMU */
519 __asm__ __volatile__(
520 ".chip 68030\n\t"
521 "lea %/pc@(1f),%/a0\n\t"
522 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
523 "addl %0,%/sp\n\t"
524 "pflusha\n\t"
525 "jmp %/a0@\n\t" /* jump into physical memory */
526 "0:.long 0\n\t" /* a constant zero. */
527 /* OK. Now reset everything and jump to reset vector. */
528 "1:\n\t"
529 "lea %/pc@(0b),%/a0\n\t"
530 "pmove %/a0@, %/tc\n\t" /* disable mmu */
531 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
532 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
533 "movel #0, %/a0\n\t"
534 "movec %/a0, %/vbr\n\t" /* clear vector base register */
535 "movec %/a0, %/cacr\n\t" /* disable caches */
536 "movel #0x0808,%/a0\n\t"
537 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
538 "movew #0x2700,%/sr\n\t" /* set up status register */
539 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
540 "movec %/a0, %/isp\n\t"
541 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
542 "reset\n\t" /* reset external devices */
543 "jmp %/a0@\n\t" /* jump to the reset vector */
544 ".chip 68k"
545 : : "r" (offset), "a" (rombase) : "a0");
548 /* should never get here */
549 pr_crit("Restart failed. Please restart manually.\n");
550 local_irq_disable();
551 while(1);
555 * This function translates seconds since 1970 into a proper date.
557 * Algorithm cribbed from glibc2.1, __offtime().
559 * This is roughly same as rtc_time64_to_tm(), which we should probably
560 * use here, but it's only available when CONFIG_RTC_LIB is enabled.
562 #define SECS_PER_MINUTE (60)
563 #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
564 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
566 static void unmktime(time64_t time, long offset,
567 int *yearp, int *monp, int *dayp,
568 int *hourp, int *minp, int *secp)
570 /* How many days come before each month (0-12). */
571 static const unsigned short int __mon_yday[2][13] =
573 /* Normal years. */
574 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
575 /* Leap years. */
576 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
578 int days, rem, y, wday, yday;
579 const unsigned short int *ip;
581 days = div_u64_rem(time, SECS_PER_DAY, &rem);
582 rem += offset;
583 while (rem < 0) {
584 rem += SECS_PER_DAY;
585 --days;
587 while (rem >= SECS_PER_DAY) {
588 rem -= SECS_PER_DAY;
589 ++days;
591 *hourp = rem / SECS_PER_HOUR;
592 rem %= SECS_PER_HOUR;
593 *minp = rem / SECS_PER_MINUTE;
594 *secp = rem % SECS_PER_MINUTE;
595 /* January 1, 1970 was a Thursday. */
596 wday = (4 + days) % 7; /* Day in the week. Not currently used */
597 if (wday < 0) wday += 7;
598 y = 1970;
600 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
601 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
602 #define __isleap(year) \
603 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
605 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
607 /* Guess a corrected year, assuming 365 days per year. */
608 long int yg = y + days / 365 - (days % 365 < 0);
610 /* Adjust DAYS and Y to match the guessed year. */
611 days -= (yg - y) * 365 +
612 LEAPS_THRU_END_OF(yg - 1) - LEAPS_THRU_END_OF(y - 1);
613 y = yg;
615 *yearp = y - 1900;
616 yday = days; /* day in the year. Not currently used. */
617 ip = __mon_yday[__isleap(y)];
618 for (y = 11; days < (long int) ip[y]; --y)
619 continue;
620 days -= ip[y];
621 *monp = y;
622 *dayp = days + 1; /* day in the month */
623 return;
627 * Read/write the hardware clock.
630 int mac_hwclk(int op, struct rtc_time *t)
632 time64_t now;
634 if (!op) { /* read */
635 switch (macintosh_config->adb_type) {
636 case MAC_ADB_IOP:
637 case MAC_ADB_II:
638 case MAC_ADB_PB1:
639 now = via_read_time();
640 break;
641 #ifdef CONFIG_ADB_CUDA
642 case MAC_ADB_EGRET:
643 case MAC_ADB_CUDA:
644 now = cuda_read_time();
645 break;
646 #endif
647 #ifdef CONFIG_ADB_PMU
648 case MAC_ADB_PB2:
649 now = pmu_read_time();
650 break;
651 #endif
652 default:
653 now = 0;
656 t->tm_wday = 0;
657 unmktime(now, 0,
658 &t->tm_year, &t->tm_mon, &t->tm_mday,
659 &t->tm_hour, &t->tm_min, &t->tm_sec);
660 pr_debug("%s: read %04d-%02d-%-2d %02d:%02d:%02d\n",
661 __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
662 t->tm_hour, t->tm_min, t->tm_sec);
663 } else { /* write */
664 pr_debug("%s: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
665 __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
666 t->tm_hour, t->tm_min, t->tm_sec);
668 now = mktime64(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
669 t->tm_hour, t->tm_min, t->tm_sec);
671 switch (macintosh_config->adb_type) {
672 case MAC_ADB_IOP:
673 case MAC_ADB_II:
674 case MAC_ADB_PB1:
675 via_write_time(now);
676 break;
677 #ifdef CONFIG_ADB_CUDA
678 case MAC_ADB_EGRET:
679 case MAC_ADB_CUDA:
680 cuda_write_time(now);
681 break;
682 #endif
683 #ifdef CONFIG_ADB_PMU
684 case MAC_ADB_PB2:
685 pmu_write_time(now);
686 break;
687 #endif
688 default:
689 return -ENODEV;
692 return 0;