]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/m68k/mac/misc.c
m68k/mac: Remove obsolete comment
[mirror_ubuntu-jammy-kernel.git] / arch / m68k / mac / misc.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * Miscellaneous Mac68K-specific stuff
4 */
5
1da177e4
LT
6#include <linux/types.h>
7#include <linux/errno.h>
1da177e4
LT
8#include <linux/kernel.h>
9#include <linux/delay.h>
10#include <linux/sched.h>
1da177e4
LT
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
7c0f6ba6 19#include <linux/uaccess.h>
1da177e4 20#include <asm/io.h>
1da177e4
LT
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>
26
1da177e4
LT
27#include <asm/machdep.h>
28
5b9bfb8e
AB
29/*
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.
33 */
1da177e4
LT
34
35#define RTC_OFFSET 2082844800
36
1da177e4
LT
37static void (*rom_reset)(void);
38
3272244c 39#ifdef CONFIG_ADB_CUDA
3272244c 40static __u8 cuda_read_pram(int offset)
1da177e4 41{
3272244c 42 struct adb_request req;
31b1c780 43
3272244c 44 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
31b1c780 45 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
3272244c
AV
46 return 0;
47 while (!req.complete)
48 cuda_poll();
49 return req.reply[3];
50}
1da177e4 51
3272244c
AV
52static void cuda_write_pram(int offset, __u8 data)
53{
54 struct adb_request req;
31b1c780 55
3272244c 56 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
31b1c780 57 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
3272244c
AV
58 return;
59 while (!req.complete)
60 cuda_poll();
61}
6df2afba 62#endif /* CONFIG_ADB_CUDA */
3272244c 63
ebd72227 64#ifdef CONFIG_ADB_PMU
3272244c
AV
65static __u8 pmu_read_pram(int offset)
66{
67 struct adb_request req;
31b1c780 68
3272244c
AV
69 if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM,
70 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
71 return 0;
72 while (!req.complete)
73 pmu_poll();
74 return req.reply[3];
75}
1da177e4 76
3272244c 77static void pmu_write_pram(int offset, __u8 data)
1da177e4 78{
3272244c 79 struct adb_request req;
31b1c780 80
3272244c
AV
81 if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM,
82 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
83 return;
84 while (!req.complete)
85 pmu_poll();
86}
ebd72227 87#endif /* CONFIG_ADB_PMU */
1da177e4 88
1da177e4
LT
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_pram_readbyte(void)
97{
31b1c780
FT
98 int i, reg;
99 __u8 data;
1da177e4
LT
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_pram_writebyte(__u8 data)
124{
31b1c780 125 int i, reg, bit;
1da177e4
LT
126
127 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
128
129 /* The bits of the byte go in 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 * Execute a VIA PRAM/RTC command. For read commands
141 * data should point to a one-byte buffer for the
142 * resulting data. For write commands it should point
143 * to the data byte to for the command.
144 *
145 * This function disables all interrupts while running.
146 */
147
148static void via_pram_command(int command, __u8 *data)
149{
150 unsigned long flags;
31b1c780 151 int is_read;
1da177e4
LT
152
153 local_irq_save(flags);
154
155 /* Enable the RTC and make sure the strobe line is high */
156
157 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
158
159 if (command & 0xFF00) { /* extended (two-byte) command */
160 via_pram_writebyte((command & 0xFF00) >> 8);
161 via_pram_writebyte(command & 0xFF);
162 is_read = command & 0x8000;
163 } else { /* one-byte command */
164 via_pram_writebyte(command);
165 is_read = command & 0x80;
166 }
167 if (is_read) {
168 *data = via_pram_readbyte();
169 } else {
170 via_pram_writebyte(*data);
171 }
172
173 /* All done, disable the RTC */
174
175 via1[vBufB] |= VIA1B_vRTCEnb;
176
177 local_irq_restore(flags);
178}
179
180static __u8 via_read_pram(int offset)
181{
182 return 0;
183}
184
185static void via_write_pram(int offset, __u8 data)
186{
187}
188
189/*
190 * Return the current time in seconds since January 1, 1904.
191 *
192 * This only works on machines with the VIA-based PRAM/RTC, which
193 * is basically any machine with Mac II-style ADB.
194 */
195
5b9bfb8e 196static time64_t via_read_time(void)
1da177e4
LT
197{
198 union {
75a23850 199 __u8 cdata[4];
5b9bfb8e 200 __u32 idata;
1da177e4 201 } result, last_result;
75a23850
FT
202 int count = 1;
203
204 via_pram_command(0x81, &last_result.cdata[3]);
205 via_pram_command(0x85, &last_result.cdata[2]);
206 via_pram_command(0x89, &last_result.cdata[1]);
207 via_pram_command(0x8D, &last_result.cdata[0]);
1da177e4
LT
208
209 /*
210 * The NetBSD guys say to loop until you get the same reading
211 * twice in a row.
212 */
213
75a23850 214 while (1) {
1da177e4
LT
215 via_pram_command(0x81, &result.cdata[3]);
216 via_pram_command(0x85, &result.cdata[2]);
217 via_pram_command(0x89, &result.cdata[1]);
218 via_pram_command(0x8D, &result.cdata[0]);
1da177e4 219
75a23850 220 if (result.idata == last_result.idata)
5b9bfb8e 221 return (time64_t)result.idata - RTC_OFFSET;
75a23850
FT
222
223 if (++count > 10)
224 break;
225
226 last_result.idata = result.idata;
227 }
228
5b9bfb8e
AB
229 pr_err("%s: failed to read a stable value; got 0x%08x then 0x%08x\n",
230 __func__, last_result.idata, result.idata);
75a23850
FT
231
232 return 0;
1da177e4
LT
233}
234
235/*
236 * Set the current time to a number of seconds since January 1, 1904.
237 *
238 * This only works on machines with the VIA-based PRAM/RTC, which
239 * is basically any machine with Mac II-style ADB.
240 */
241
0792a2c8 242static void via_set_rtc_time(struct rtc_time *tm)
1da177e4
LT
243{
244 union {
31b1c780 245 __u8 cdata[4];
5b9bfb8e 246 __u32 idata;
1da177e4 247 } data;
31b1c780 248 __u8 temp;
0792a2c8
FT
249 time64_t time;
250
251 time = mktime64(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
252 tm->tm_hour, tm->tm_min, tm->tm_sec);
1da177e4
LT
253
254 /* Clear the write protect bit */
255
256 temp = 0x55;
257 via_pram_command(0x35, &temp);
258
5b9bfb8e 259 data.idata = lower_32_bits(time + RTC_OFFSET);
1da177e4
LT
260 via_pram_command(0x01, &data.cdata[3]);
261 via_pram_command(0x05, &data.cdata[2]);
262 via_pram_command(0x09, &data.cdata[1]);
263 via_pram_command(0x0D, &data.cdata[0]);
264
265 /* Set the write protect bit */
266
267 temp = 0xD5;
268 via_pram_command(0x35, &temp);
269}
270
271static void via_shutdown(void)
272{
273 if (rbv_present) {
274 via2[rBufB] &= ~0x04;
275 } else {
276 /* Direction of vDirB is output */
277 via2[vDirB] |= 0x04;
278 /* Send a value of 0 on that line */
279 via2[vBufB] &= ~0x04;
280 mdelay(1000);
281 }
282}
283
1da177e4
LT
284static void oss_shutdown(void)
285{
286 oss->rom_ctrl = OSS_POWEROFF;
287}
288
289#ifdef CONFIG_ADB_CUDA
1da177e4
LT
290static void cuda_restart(void)
291{
3272244c 292 struct adb_request req;
31b1c780 293
3272244c
AV
294 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
295 return;
296 while (!req.complete)
297 cuda_poll();
1da177e4
LT
298}
299
300static void cuda_shutdown(void)
301{
3272244c 302 struct adb_request req;
31b1c780 303
3272244c
AV
304 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
305 return;
41e93a30
FT
306
307 /* Avoid infinite polling loop when PSU is not under Cuda control */
308 switch (macintosh_config->ident) {
309 case MAC_MODEL_C660:
310 case MAC_MODEL_Q605:
311 case MAC_MODEL_Q605_ACC:
312 case MAC_MODEL_P475:
313 case MAC_MODEL_P475F:
314 return;
315 }
316
3272244c
AV
317 while (!req.complete)
318 cuda_poll();
1da177e4 319}
1da177e4
LT
320#endif /* CONFIG_ADB_CUDA */
321
1da177e4
LT
322/*
323 *-------------------------------------------------------------------
324 * Below this point are the generic routines; they'll dispatch to the
325 * correct routine for the hardware on which we're running.
326 *-------------------------------------------------------------------
327 */
328
329void mac_pram_read(int offset, __u8 *buffer, int len)
330{
3272244c 331 __u8 (*func)(int);
1da177e4
LT
332 int i;
333
31b1c780 334 switch (macintosh_config->adb_type) {
6df2afba
FT
335 case MAC_ADB_IOP:
336 case MAC_ADB_II:
3272244c 337 case MAC_ADB_PB1:
6df2afba 338 func = via_read_pram;
31b1c780 339 break;
6df2afba 340#ifdef CONFIG_ADB_CUDA
f74faec6 341 case MAC_ADB_EGRET:
3272244c 342 case MAC_ADB_CUDA:
31b1c780
FT
343 func = cuda_read_pram;
344 break;
6df2afba 345#endif
ebd72227 346#ifdef CONFIG_ADB_PMU
6df2afba
FT
347 case MAC_ADB_PB2:
348 func = pmu_read_pram;
349 break;
350#endif
3272244c 351 default:
3272244c 352 return;
6df2afba 353 }
1da177e4
LT
354 for (i = 0 ; i < len ; i++) {
355 buffer[i] = (*func)(offset++);
356 }
357}
358
359void mac_pram_write(int offset, __u8 *buffer, int len)
360{
3272244c 361 void (*func)(int, __u8);
1da177e4
LT
362 int i;
363
31b1c780 364 switch (macintosh_config->adb_type) {
6df2afba
FT
365 case MAC_ADB_IOP:
366 case MAC_ADB_II:
3272244c 367 case MAC_ADB_PB1:
6df2afba 368 func = via_write_pram;
31b1c780 369 break;
6df2afba 370#ifdef CONFIG_ADB_CUDA
f74faec6 371 case MAC_ADB_EGRET:
3272244c 372 case MAC_ADB_CUDA:
31b1c780
FT
373 func = cuda_write_pram;
374 break;
6df2afba 375#endif
ebd72227 376#ifdef CONFIG_ADB_PMU
6df2afba
FT
377 case MAC_ADB_PB2:
378 func = pmu_write_pram;
379 break;
380#endif
3272244c 381 default:
3272244c 382 return;
6df2afba 383 }
1da177e4
LT
384 for (i = 0 ; i < len ; i++) {
385 (*func)(offset++, buffer[i]);
386 }
387}
388
389void mac_poweroff(void)
390{
1da177e4
LT
391 if (oss_present) {
392 oss_shutdown();
393 } else if (macintosh_config->adb_type == MAC_ADB_II) {
394 via_shutdown();
395#ifdef CONFIG_ADB_CUDA
f74faec6
FT
396 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
397 macintosh_config->adb_type == MAC_ADB_CUDA) {
1da177e4
LT
398 cuda_shutdown();
399#endif
ebd72227 400#ifdef CONFIG_ADB_PMU
54c99077 401 } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
1da177e4
LT
402 pmu_shutdown();
403#endif
404 }
558d5ad2 405
889121b4 406 pr_crit("It is now safe to turn off your Macintosh.\n");
558d5ad2 407 local_irq_disable();
1da177e4
LT
408 while(1);
409}
410
411void mac_reset(void)
412{
413 if (macintosh_config->adb_type == MAC_ADB_II) {
414 unsigned long flags;
415
416 /* need ROMBASE in booter */
417 /* indeed, plus need to MAP THE ROM !! */
418
419 if (mac_bi_data.rombase == 0)
420 mac_bi_data.rombase = 0x40800000;
421
422 /* works on some */
423 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
424
425 if (macintosh_config->ident == MAC_MODEL_SE30) {
426 /*
427 * MSch: Machines known to crash on ROM reset ...
428 */
429 } else {
430 local_irq_save(flags);
431
432 rom_reset();
433
434 local_irq_restore(flags);
435 }
436#ifdef CONFIG_ADB_CUDA
f74faec6
FT
437 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
438 macintosh_config->adb_type == MAC_ADB_CUDA) {
1da177e4
LT
439 cuda_restart();
440#endif
ebd72227 441#ifdef CONFIG_ADB_PMU
54c99077 442 } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
1da177e4
LT
443 pmu_restart();
444#endif
445 } else if (CPU_IS_030) {
446
447 /* 030-specific reset routine. The idea is general, but the
448 * specific registers to reset are '030-specific. Until I
449 * have a non-030 machine, I can't test anything else.
450 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
451 */
452
453 unsigned long rombase = 0x40000000;
454
455 /* make a 1-to-1 mapping, using the transparent tran. reg. */
456 unsigned long virt = (unsigned long) mac_reset;
457 unsigned long phys = virt_to_phys(mac_reset);
77add9f3 458 unsigned long addr = (phys&0xFF000000)|0x8777;
1da177e4 459 unsigned long offset = phys-virt;
31b1c780 460
1da177e4
LT
461 local_irq_disable(); /* lets not screw this up, ok? */
462 __asm__ __volatile__(".chip 68030\n\t"
463 "pmove %0,%/tt0\n\t"
464 ".chip 68k"
77add9f3 465 : : "m" (addr));
1da177e4
LT
466 /* Now jump to physical address so we can disable MMU */
467 __asm__ __volatile__(
31b1c780 468 ".chip 68030\n\t"
1da177e4
LT
469 "lea %/pc@(1f),%/a0\n\t"
470 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
471 "addl %0,%/sp\n\t"
472 "pflusha\n\t"
473 "jmp %/a0@\n\t" /* jump into physical memory */
474 "0:.long 0\n\t" /* a constant zero. */
475 /* OK. Now reset everything and jump to reset vector. */
476 "1:\n\t"
477 "lea %/pc@(0b),%/a0\n\t"
478 "pmove %/a0@, %/tc\n\t" /* disable mmu */
479 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
480 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
481 "movel #0, %/a0\n\t"
482 "movec %/a0, %/vbr\n\t" /* clear vector base register */
483 "movec %/a0, %/cacr\n\t" /* disable caches */
484 "movel #0x0808,%/a0\n\t"
485 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
486 "movew #0x2700,%/sr\n\t" /* set up status register */
487 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
488 "movec %/a0, %/isp\n\t"
489 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
490 "reset\n\t" /* reset external devices */
491 "jmp %/a0@\n\t" /* jump to the reset vector */
492 ".chip 68k"
493 : : "r" (offset), "a" (rombase) : "a0");
494 }
495
496 /* should never get here */
889121b4 497 pr_crit("Restart failed. Please restart manually.\n");
558d5ad2 498 local_irq_disable();
1da177e4
LT
499 while(1);
500}
501
502/*
503 * This function translates seconds since 1970 into a proper date.
504 *
505 * Algorithm cribbed from glibc2.1, __offtime().
5b9bfb8e
AB
506 *
507 * This is roughly same as rtc_time64_to_tm(), which we should probably
508 * use here, but it's only available when CONFIG_RTC_LIB is enabled.
1da177e4
LT
509 */
510#define SECS_PER_MINUTE (60)
511#define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
512#define SECS_PER_DAY (SECS_PER_HOUR * 24)
513
5b9bfb8e 514static void unmktime(time64_t time, long offset,
1da177e4
LT
515 int *yearp, int *monp, int *dayp,
516 int *hourp, int *minp, int *secp)
517{
518 /* How many days come before each month (0-12). */
519 static const unsigned short int __mon_yday[2][13] =
520 {
521 /* Normal years. */
522 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
523 /* Leap years. */
524 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
525 };
5b9bfb8e 526 int days, rem, y, wday, yday;
1da177e4
LT
527 const unsigned short int *ip;
528
5b9bfb8e 529 days = div_u64_rem(time, SECS_PER_DAY, &rem);
1da177e4
LT
530 rem += offset;
531 while (rem < 0) {
532 rem += SECS_PER_DAY;
533 --days;
534 }
535 while (rem >= SECS_PER_DAY) {
536 rem -= SECS_PER_DAY;
537 ++days;
538 }
539 *hourp = rem / SECS_PER_HOUR;
540 rem %= SECS_PER_HOUR;
541 *minp = rem / SECS_PER_MINUTE;
542 *secp = rem % SECS_PER_MINUTE;
543 /* January 1, 1970 was a Thursday. */
544 wday = (4 + days) % 7; /* Day in the week. Not currently used */
545 if (wday < 0) wday += 7;
546 y = 1970;
547
548#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
549#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
550#define __isleap(year) \
551 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
552
553 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
554 {
555 /* Guess a corrected year, assuming 365 days per year. */
556 long int yg = y + days / 365 - (days % 365 < 0);
557
558 /* Adjust DAYS and Y to match the guessed year. */
31b1c780
FT
559 days -= (yg - y) * 365 +
560 LEAPS_THRU_END_OF(yg - 1) - LEAPS_THRU_END_OF(y - 1);
1da177e4
LT
561 y = yg;
562 }
563 *yearp = y - 1900;
564 yday = days; /* day in the year. Not currently used. */
565 ip = __mon_yday[__isleap(y)];
566 for (y = 11; days < (long int) ip[y]; --y)
567 continue;
568 days -= ip[y];
569 *monp = y;
570 *dayp = days + 1; /* day in the month */
571 return;
572}
573
574/*
575 * Read/write the hardware clock.
576 */
577
578int mac_hwclk(int op, struct rtc_time *t)
579{
5b9bfb8e 580 time64_t now;
1da177e4
LT
581
582 if (!op) { /* read */
3272244c 583 switch (macintosh_config->adb_type) {
3272244c 584 case MAC_ADB_IOP:
6df2afba 585 case MAC_ADB_II:
3272244c 586 case MAC_ADB_PB1:
6df2afba 587 now = via_read_time();
3272244c 588 break;
6df2afba 589#ifdef CONFIG_ADB_CUDA
f74faec6 590 case MAC_ADB_EGRET:
3272244c 591 case MAC_ADB_CUDA:
0792a2c8 592 now = cuda_get_time();
3272244c 593 break;
6df2afba 594#endif
ebd72227 595#ifdef CONFIG_ADB_PMU
6df2afba 596 case MAC_ADB_PB2:
0792a2c8 597 now = pmu_get_time();
6df2afba
FT
598 break;
599#endif
3272244c 600 default:
1da177e4
LT
601 now = 0;
602 }
603
604 t->tm_wday = 0;
605 unmktime(now, 0,
606 &t->tm_year, &t->tm_mon, &t->tm_mday,
607 &t->tm_hour, &t->tm_min, &t->tm_sec);
90625444 608 pr_debug("%s: read %ptR\n", __func__, t);
1da177e4 609 } else { /* write */
90625444 610 pr_debug("%s: tried to write %ptR\n", __func__, t);
1da177e4 611
3272244c 612 switch (macintosh_config->adb_type) {
3272244c 613 case MAC_ADB_IOP:
6df2afba
FT
614 case MAC_ADB_II:
615 case MAC_ADB_PB1:
0792a2c8 616 via_set_rtc_time(t);
3272244c 617 break;
6df2afba 618#ifdef CONFIG_ADB_CUDA
f74faec6 619 case MAC_ADB_EGRET:
3272244c 620 case MAC_ADB_CUDA:
0792a2c8 621 cuda_set_rtc_time(t);
3272244c 622 break;
6df2afba 623#endif
ebd72227 624#ifdef CONFIG_ADB_PMU
3272244c 625 case MAC_ADB_PB2:
0792a2c8 626 pmu_set_rtc_time(t);
3272244c 627 break;
6df2afba
FT
628#endif
629 default:
630 return -ENODEV;
1da177e4 631 }
1da177e4
LT
632 }
633 return 0;
634}