blob: 50387638557d026f63297861ee12a60624c573f9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Miscellaneous Mac68K-specific stuff
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/types.h>
7#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/kernel.h>
9#include <linux/delay.h>
10#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080019#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/machdep.h>
28
29/* Offset between Unix time (1970-based) and Mac time (1904-based) */
30
31#define RTC_OFFSET 2082844800
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static void (*rom_reset)(void);
34
Al Viro32722442006-01-12 01:06:13 -080035#ifdef CONFIG_ADB_CUDA
36static long cuda_read_time(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Al Viro32722442006-01-12 01:06:13 -080038 struct adb_request req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 long time;
40
Al Viro32722442006-01-12 01:06:13 -080041 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
42 return 0;
43 while (!req.complete)
44 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Finn Thain31b1c782018-03-11 23:38:28 -040046 time = (req.reply[3] << 24) | (req.reply[4] << 16) |
47 (req.reply[5] << 8) | req.reply[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return time - RTC_OFFSET;
49}
50
Al Viro32722442006-01-12 01:06:13 -080051static void cuda_write_time(long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Al Viro32722442006-01-12 01:06:13 -080053 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -040054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 data += RTC_OFFSET;
Al Viro32722442006-01-12 01:06:13 -080056 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
Finn Thain31b1c782018-03-11 23:38:28 -040057 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
58 (data >> 8) & 0xFF, data & 0xFF) < 0)
Al Viro32722442006-01-12 01:06:13 -080059 return;
60 while (!req.complete)
61 cuda_poll();
62}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Al Viro32722442006-01-12 01:06:13 -080064static __u8 cuda_read_pram(int offset)
65{
66 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -040067
Al Viro32722442006-01-12 01:06:13 -080068 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
Finn Thain31b1c782018-03-11 23:38:28 -040069 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
Al Viro32722442006-01-12 01:06:13 -080070 return 0;
71 while (!req.complete)
72 cuda_poll();
73 return req.reply[3];
74}
75
76static void cuda_write_pram(int offset, __u8 data)
77{
78 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -040079
Al Viro32722442006-01-12 01:06:13 -080080 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
Finn Thain31b1c782018-03-11 23:38:28 -040081 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
Al Viro32722442006-01-12 01:06:13 -080082 return;
83 while (!req.complete)
84 cuda_poll();
85}
86#else
87#define cuda_read_time() 0
88#define cuda_write_time(n)
89#define cuda_read_pram NULL
90#define cuda_write_pram NULL
91#endif
92
Finn Thaind643e2d2010-06-02 15:58:24 +020093#ifdef CONFIG_ADB_PMU68K
Al Viro32722442006-01-12 01:06:13 -080094static long pmu_read_time(void)
95{
96 struct adb_request req;
97 long time;
98
99 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
100 return 0;
101 while (!req.complete)
102 pmu_poll();
103
Finn Thain31b1c782018-03-11 23:38:28 -0400104 time = (req.reply[1] << 24) | (req.reply[2] << 16) |
105 (req.reply[3] << 8) | req.reply[4];
Al Viro32722442006-01-12 01:06:13 -0800106 return time - RTC_OFFSET;
107}
108
109static void pmu_write_time(long data)
110{
111 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -0400112
Al Viro32722442006-01-12 01:06:13 -0800113 data += 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 while (!req.complete)
119 pmu_poll();
120}
121
122static __u8 pmu_read_pram(int offset)
123{
124 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -0400125
Al Viro32722442006-01-12 01:06:13 -0800126 if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM,
127 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
128 return 0;
129 while (!req.complete)
130 pmu_poll();
131 return req.reply[3];
132}
133
134static void pmu_write_pram(int offset, __u8 data)
135{
136 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -0400137
Al Viro32722442006-01-12 01:06:13 -0800138 if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM,
139 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
140 return;
141 while (!req.complete)
142 pmu_poll();
143}
144#else
145#define pmu_read_time() 0
146#define pmu_write_time(n)
147#define pmu_read_pram NULL
148#define pmu_write_pram NULL
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
152 * VIA PRAM/RTC access routines
153 *
154 * Must be called with interrupts disabled and
155 * the RTC should be enabled.
156 */
157
158static __u8 via_pram_readbyte(void)
159{
Finn Thain31b1c782018-03-11 23:38:28 -0400160 int i, reg;
161 __u8 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 reg = via1[vBufB] & ~VIA1B_vRTCClk;
164
165 /* Set the RTC data line to be an input. */
166
167 via1[vDirB] &= ~VIA1B_vRTCData;
168
169 /* The bits of the byte come out in MSB order */
170
171 data = 0;
172 for (i = 0 ; i < 8 ; i++) {
173 via1[vBufB] = reg;
174 via1[vBufB] = reg | VIA1B_vRTCClk;
175 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
176 }
177
178 /* Return RTC data line to output state */
179
180 via1[vDirB] |= VIA1B_vRTCData;
181
182 return data;
183}
184
185static void via_pram_writebyte(__u8 data)
186{
Finn Thain31b1c782018-03-11 23:38:28 -0400187 int i, reg, bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
190
191 /* The bits of the byte go in in MSB order */
192
193 for (i = 0 ; i < 8 ; i++) {
194 bit = data & 0x80? 1 : 0;
195 data <<= 1;
196 via1[vBufB] = reg | bit;
197 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
198 }
199}
200
201/*
202 * Execute a VIA PRAM/RTC command. For read commands
203 * data should point to a one-byte buffer for the
204 * resulting data. For write commands it should point
205 * to the data byte to for the command.
206 *
207 * This function disables all interrupts while running.
208 */
209
210static void via_pram_command(int command, __u8 *data)
211{
212 unsigned long flags;
Finn Thain31b1c782018-03-11 23:38:28 -0400213 int is_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 local_irq_save(flags);
216
217 /* Enable the RTC and make sure the strobe line is high */
218
219 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
220
221 if (command & 0xFF00) { /* extended (two-byte) command */
222 via_pram_writebyte((command & 0xFF00) >> 8);
223 via_pram_writebyte(command & 0xFF);
224 is_read = command & 0x8000;
225 } else { /* one-byte command */
226 via_pram_writebyte(command);
227 is_read = command & 0x80;
228 }
229 if (is_read) {
230 *data = via_pram_readbyte();
231 } else {
232 via_pram_writebyte(*data);
233 }
234
235 /* All done, disable the RTC */
236
237 via1[vBufB] |= VIA1B_vRTCEnb;
238
239 local_irq_restore(flags);
240}
241
242static __u8 via_read_pram(int offset)
243{
244 return 0;
245}
246
247static void via_write_pram(int offset, __u8 data)
248{
249}
250
251/*
252 * Return the current time in seconds since January 1, 1904.
253 *
254 * This only works on machines with the VIA-based PRAM/RTC, which
255 * is basically any machine with Mac II-style ADB.
256 */
257
258static long via_read_time(void)
259{
260 union {
Finn Thain75a23852011-07-18 00:06:10 +1000261 __u8 cdata[4];
262 long idata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 } result, last_result;
Finn Thain75a23852011-07-18 00:06:10 +1000264 int count = 1;
265
266 via_pram_command(0x81, &last_result.cdata[3]);
267 via_pram_command(0x85, &last_result.cdata[2]);
268 via_pram_command(0x89, &last_result.cdata[1]);
269 via_pram_command(0x8D, &last_result.cdata[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 /*
272 * The NetBSD guys say to loop until you get the same reading
273 * twice in a row.
274 */
275
Finn Thain75a23852011-07-18 00:06:10 +1000276 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 via_pram_command(0x81, &result.cdata[3]);
278 via_pram_command(0x85, &result.cdata[2]);
279 via_pram_command(0x89, &result.cdata[1]);
280 via_pram_command(0x8D, &result.cdata[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Finn Thain75a23852011-07-18 00:06:10 +1000282 if (result.idata == last_result.idata)
283 return result.idata - RTC_OFFSET;
284
285 if (++count > 10)
286 break;
287
288 last_result.idata = result.idata;
289 }
290
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400291 pr_err("via_read_time: failed to read a stable value; got 0x%08lx then 0x%08lx\n",
Finn Thain75a23852011-07-18 00:06:10 +1000292 last_result.idata, result.idata);
293
294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297/*
298 * Set the current time to a number of seconds since January 1, 1904.
299 *
300 * This only works on machines with the VIA-based PRAM/RTC, which
301 * is basically any machine with Mac II-style ADB.
302 */
303
304static void via_write_time(long time)
305{
306 union {
Finn Thain31b1c782018-03-11 23:38:28 -0400307 __u8 cdata[4];
308 long idata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 } data;
Finn Thain31b1c782018-03-11 23:38:28 -0400310 __u8 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /* Clear the write protect bit */
313
314 temp = 0x55;
315 via_pram_command(0x35, &temp);
316
317 data.idata = time + RTC_OFFSET;
318 via_pram_command(0x01, &data.cdata[3]);
319 via_pram_command(0x05, &data.cdata[2]);
320 via_pram_command(0x09, &data.cdata[1]);
321 via_pram_command(0x0D, &data.cdata[0]);
322
323 /* Set the write protect bit */
324
325 temp = 0xD5;
326 via_pram_command(0x35, &temp);
327}
328
329static void via_shutdown(void)
330{
331 if (rbv_present) {
332 via2[rBufB] &= ~0x04;
333 } else {
334 /* Direction of vDirB is output */
335 via2[vDirB] |= 0x04;
336 /* Send a value of 0 on that line */
337 via2[vBufB] &= ~0x04;
338 mdelay(1000);
339 }
340}
341
342/*
343 * FIXME: not sure how this is supposed to work exactly...
344 */
345
346static void oss_shutdown(void)
347{
348 oss->rom_ctrl = OSS_POWEROFF;
349}
350
351#ifdef CONFIG_ADB_CUDA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352static void cuda_restart(void)
353{
Al Viro32722442006-01-12 01:06:13 -0800354 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -0400355
Al Viro32722442006-01-12 01:06:13 -0800356 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
357 return;
358 while (!req.complete)
359 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362static void cuda_shutdown(void)
363{
Al Viro32722442006-01-12 01:06:13 -0800364 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -0400365
Al Viro32722442006-01-12 01:06:13 -0800366 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
367 return;
Finn Thain41e93a32017-08-12 17:11:02 +1000368
369 /* Avoid infinite polling loop when PSU is not under Cuda control */
370 switch (macintosh_config->ident) {
371 case MAC_MODEL_C660:
372 case MAC_MODEL_Q605:
373 case MAC_MODEL_Q605_ACC:
374 case MAC_MODEL_P475:
375 case MAC_MODEL_P475F:
376 return;
377 }
378
Al Viro32722442006-01-12 01:06:13 -0800379 while (!req.complete)
380 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382#endif /* CONFIG_ADB_CUDA */
383
Al Viro32722442006-01-12 01:06:13 -0800384#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386void pmu_restart(void)
387{
Al Viro32722442006-01-12 01:06:13 -0800388 struct adb_request req;
389 if (pmu_request(&req, NULL,
390 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
391 return;
392 while (!req.complete)
393 pmu_poll();
394 if (pmu_request(&req, NULL, 1, PMU_RESET) < 0)
395 return;
396 while (!req.complete)
397 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400void pmu_shutdown(void)
401{
Al Viro32722442006-01-12 01:06:13 -0800402 struct adb_request req;
403 if (pmu_request(&req, NULL,
404 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
405 return;
406 while (!req.complete)
407 pmu_poll();
408 if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0)
409 return;
410 while (!req.complete)
411 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Al Viro32722442006-01-12 01:06:13 -0800414#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416/*
417 *-------------------------------------------------------------------
418 * Below this point are the generic routines; they'll dispatch to the
419 * correct routine for the hardware on which we're running.
420 *-------------------------------------------------------------------
421 */
422
423void mac_pram_read(int offset, __u8 *buffer, int len)
424{
Al Viro32722442006-01-12 01:06:13 -0800425 __u8 (*func)(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int i;
427
Finn Thain31b1c782018-03-11 23:38:28 -0400428 switch (macintosh_config->adb_type) {
Al Viro32722442006-01-12 01:06:13 -0800429 case MAC_ADB_PB1:
430 case MAC_ADB_PB2:
Finn Thain31b1c782018-03-11 23:38:28 -0400431 func = pmu_read_pram;
432 break;
Finn Thainf74faec2016-12-31 19:56:26 -0500433 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800434 case MAC_ADB_CUDA:
Finn Thain31b1c782018-03-11 23:38:28 -0400435 func = cuda_read_pram;
436 break;
Al Viro32722442006-01-12 01:06:13 -0800437 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 func = via_read_pram;
439 }
Al Viro32722442006-01-12 01:06:13 -0800440 if (!func)
441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 for (i = 0 ; i < len ; i++) {
443 buffer[i] = (*func)(offset++);
444 }
445}
446
447void mac_pram_write(int offset, __u8 *buffer, int len)
448{
Al Viro32722442006-01-12 01:06:13 -0800449 void (*func)(int, __u8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int i;
451
Finn Thain31b1c782018-03-11 23:38:28 -0400452 switch (macintosh_config->adb_type) {
Al Viro32722442006-01-12 01:06:13 -0800453 case MAC_ADB_PB1:
454 case MAC_ADB_PB2:
Finn Thain31b1c782018-03-11 23:38:28 -0400455 func = pmu_write_pram;
456 break;
Finn Thainf74faec2016-12-31 19:56:26 -0500457 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800458 case MAC_ADB_CUDA:
Finn Thain31b1c782018-03-11 23:38:28 -0400459 func = cuda_write_pram;
460 break;
Al Viro32722442006-01-12 01:06:13 -0800461 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 func = via_write_pram;
463 }
Al Viro32722442006-01-12 01:06:13 -0800464 if (!func)
465 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 for (i = 0 ; i < len ; i++) {
467 (*func)(offset++, buffer[i]);
468 }
469}
470
471void mac_poweroff(void)
472{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (oss_present) {
474 oss_shutdown();
475 } else if (macintosh_config->adb_type == MAC_ADB_II) {
476 via_shutdown();
477#ifdef CONFIG_ADB_CUDA
Finn Thainf74faec2016-12-31 19:56:26 -0500478 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
479 macintosh_config->adb_type == MAC_ADB_CUDA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 cuda_shutdown();
481#endif
Al Viro32722442006-01-12 01:06:13 -0800482#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 } else if (macintosh_config->adb_type == MAC_ADB_PB1
484 || macintosh_config->adb_type == MAC_ADB_PB2) {
485 pmu_shutdown();
486#endif
487 }
Finn Thain558d5ad2017-08-12 17:12:12 +1000488
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400489 pr_crit("It is now safe to turn off your Macintosh.\n");
Finn Thain558d5ad2017-08-12 17:12:12 +1000490 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 while(1);
492}
493
494void mac_reset(void)
495{
496 if (macintosh_config->adb_type == MAC_ADB_II) {
497 unsigned long flags;
498
499 /* need ROMBASE in booter */
500 /* indeed, plus need to MAP THE ROM !! */
501
502 if (mac_bi_data.rombase == 0)
503 mac_bi_data.rombase = 0x40800000;
504
505 /* works on some */
506 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
507
508 if (macintosh_config->ident == MAC_MODEL_SE30) {
509 /*
510 * MSch: Machines known to crash on ROM reset ...
511 */
512 } else {
513 local_irq_save(flags);
514
515 rom_reset();
516
517 local_irq_restore(flags);
518 }
519#ifdef CONFIG_ADB_CUDA
Finn Thainf74faec2016-12-31 19:56:26 -0500520 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
521 macintosh_config->adb_type == MAC_ADB_CUDA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 cuda_restart();
523#endif
Al Viro32722442006-01-12 01:06:13 -0800524#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 } else if (macintosh_config->adb_type == MAC_ADB_PB1
526 || macintosh_config->adb_type == MAC_ADB_PB2) {
527 pmu_restart();
528#endif
529 } else if (CPU_IS_030) {
530
531 /* 030-specific reset routine. The idea is general, but the
532 * specific registers to reset are '030-specific. Until I
533 * have a non-030 machine, I can't test anything else.
534 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
535 */
536
537 unsigned long rombase = 0x40000000;
538
539 /* make a 1-to-1 mapping, using the transparent tran. reg. */
540 unsigned long virt = (unsigned long) mac_reset;
541 unsigned long phys = virt_to_phys(mac_reset);
Al Viro77add9f2006-01-12 01:06:18 -0800542 unsigned long addr = (phys&0xFF000000)|0x8777;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 unsigned long offset = phys-virt;
Finn Thain31b1c782018-03-11 23:38:28 -0400544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 local_irq_disable(); /* lets not screw this up, ok? */
546 __asm__ __volatile__(".chip 68030\n\t"
547 "pmove %0,%/tt0\n\t"
548 ".chip 68k"
Al Viro77add9f2006-01-12 01:06:18 -0800549 : : "m" (addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* Now jump to physical address so we can disable MMU */
551 __asm__ __volatile__(
Finn Thain31b1c782018-03-11 23:38:28 -0400552 ".chip 68030\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 "lea %/pc@(1f),%/a0\n\t"
554 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
555 "addl %0,%/sp\n\t"
556 "pflusha\n\t"
557 "jmp %/a0@\n\t" /* jump into physical memory */
558 "0:.long 0\n\t" /* a constant zero. */
559 /* OK. Now reset everything and jump to reset vector. */
560 "1:\n\t"
561 "lea %/pc@(0b),%/a0\n\t"
562 "pmove %/a0@, %/tc\n\t" /* disable mmu */
563 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
564 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
565 "movel #0, %/a0\n\t"
566 "movec %/a0, %/vbr\n\t" /* clear vector base register */
567 "movec %/a0, %/cacr\n\t" /* disable caches */
568 "movel #0x0808,%/a0\n\t"
569 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
570 "movew #0x2700,%/sr\n\t" /* set up status register */
571 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
572 "movec %/a0, %/isp\n\t"
573 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
574 "reset\n\t" /* reset external devices */
575 "jmp %/a0@\n\t" /* jump to the reset vector */
576 ".chip 68k"
577 : : "r" (offset), "a" (rombase) : "a0");
578 }
579
580 /* should never get here */
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400581 pr_crit("Restart failed. Please restart manually.\n");
Finn Thain558d5ad2017-08-12 17:12:12 +1000582 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 while(1);
584}
585
586/*
587 * This function translates seconds since 1970 into a proper date.
588 *
589 * Algorithm cribbed from glibc2.1, __offtime().
590 */
591#define SECS_PER_MINUTE (60)
592#define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
593#define SECS_PER_DAY (SECS_PER_HOUR * 24)
594
595static void unmktime(unsigned long time, long offset,
596 int *yearp, int *monp, int *dayp,
597 int *hourp, int *minp, int *secp)
598{
599 /* How many days come before each month (0-12). */
600 static const unsigned short int __mon_yday[2][13] =
601 {
602 /* Normal years. */
603 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
604 /* Leap years. */
605 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
606 };
607 long int days, rem, y, wday, yday;
608 const unsigned short int *ip;
609
610 days = time / SECS_PER_DAY;
611 rem = time % SECS_PER_DAY;
612 rem += offset;
613 while (rem < 0) {
614 rem += SECS_PER_DAY;
615 --days;
616 }
617 while (rem >= SECS_PER_DAY) {
618 rem -= SECS_PER_DAY;
619 ++days;
620 }
621 *hourp = rem / SECS_PER_HOUR;
622 rem %= SECS_PER_HOUR;
623 *minp = rem / SECS_PER_MINUTE;
624 *secp = rem % SECS_PER_MINUTE;
625 /* January 1, 1970 was a Thursday. */
626 wday = (4 + days) % 7; /* Day in the week. Not currently used */
627 if (wday < 0) wday += 7;
628 y = 1970;
629
630#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
631#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
632#define __isleap(year) \
633 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
634
635 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
636 {
637 /* Guess a corrected year, assuming 365 days per year. */
638 long int yg = y + days / 365 - (days % 365 < 0);
639
640 /* Adjust DAYS and Y to match the guessed year. */
Finn Thain31b1c782018-03-11 23:38:28 -0400641 days -= (yg - y) * 365 +
642 LEAPS_THRU_END_OF(yg - 1) - LEAPS_THRU_END_OF(y - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 y = yg;
644 }
645 *yearp = y - 1900;
646 yday = days; /* day in the year. Not currently used. */
647 ip = __mon_yday[__isleap(y)];
648 for (y = 11; days < (long int) ip[y]; --y)
649 continue;
650 days -= ip[y];
651 *monp = y;
652 *dayp = days + 1; /* day in the month */
653 return;
654}
655
656/*
657 * Read/write the hardware clock.
658 */
659
660int mac_hwclk(int op, struct rtc_time *t)
661{
662 unsigned long now;
663
664 if (!op) { /* read */
Al Viro32722442006-01-12 01:06:13 -0800665 switch (macintosh_config->adb_type) {
666 case MAC_ADB_II:
667 case MAC_ADB_IOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 now = via_read_time();
Al Viro32722442006-01-12 01:06:13 -0800669 break;
Al Viro32722442006-01-12 01:06:13 -0800670 case MAC_ADB_PB1:
671 case MAC_ADB_PB2:
672 now = pmu_read_time();
673 break;
Finn Thainf74faec2016-12-31 19:56:26 -0500674 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800675 case MAC_ADB_CUDA:
676 now = cuda_read_time();
677 break;
678 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 now = 0;
680 }
681
682 t->tm_wday = 0;
683 unmktime(now, 0,
684 &t->tm_year, &t->tm_mon, &t->tm_mday,
685 &t->tm_hour, &t->tm_min, &t->tm_sec);
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400686 pr_debug("%s: read %04d-%02d-%-2d %02d:%02d:%02d\n",
687 __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
688 t->tm_hour, t->tm_min, t->tm_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 } else { /* write */
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400690 pr_debug("%s: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
691 __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
692 t->tm_hour, t->tm_min, t->tm_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
695 t->tm_hour, t->tm_min, t->tm_sec);
696
Al Viro32722442006-01-12 01:06:13 -0800697 switch (macintosh_config->adb_type) {
698 case MAC_ADB_II:
699 case MAC_ADB_IOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 via_write_time(now);
Al Viro32722442006-01-12 01:06:13 -0800701 break;
Finn Thainf74faec2016-12-31 19:56:26 -0500702 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800703 case MAC_ADB_CUDA:
704 cuda_write_time(now);
705 break;
706 case MAC_ADB_PB1:
707 case MAC_ADB_PB2:
708 pmu_write_time(now);
709 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 return 0;
713}
714
715/*
716 * Set minutes/seconds in the hardware clock
717 */
718
719int mac_set_clock_mmss (unsigned long nowtime)
720{
721 struct rtc_time now;
722
723 mac_hwclk(0, &now);
724 now.tm_sec = nowtime % 60;
725 now.tm_min = (nowtime / 60) % 60;
726 mac_hwclk(1, &now);
727
728 return 0;
729}