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