blob: c68054361615a32eef13ab46c17c99d75793baa0 [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}
Finn Thain6df2afba2018-03-11 23:38:28 -040086#endif /* CONFIG_ADB_CUDA */
Al Viro32722442006-01-12 01:06:13 -080087
Finn Thaind643e2d2010-06-02 15:58:24 +020088#ifdef CONFIG_ADB_PMU68K
Al Viro32722442006-01-12 01:06:13 -080089static long pmu_read_time(void)
90{
91 struct adb_request req;
92 long time;
93
94 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
95 return 0;
96 while (!req.complete)
97 pmu_poll();
98
Finn Thain31b1c782018-03-11 23:38:28 -040099 time = (req.reply[1] << 24) | (req.reply[2] << 16) |
100 (req.reply[3] << 8) | req.reply[4];
Al Viro32722442006-01-12 01:06:13 -0800101 return time - RTC_OFFSET;
102}
103
104static void pmu_write_time(long data)
105{
106 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -0400107
Al Viro32722442006-01-12 01:06:13 -0800108 data += RTC_OFFSET;
109 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
110 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
111 (data >> 8) & 0xFF, data & 0xFF) < 0)
112 return;
113 while (!req.complete)
114 pmu_poll();
115}
116
117static __u8 pmu_read_pram(int offset)
118{
119 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -0400120
Al Viro32722442006-01-12 01:06:13 -0800121 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];
127}
128
129static void pmu_write_pram(int offset, __u8 data)
130{
131 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -0400132
Al Viro32722442006-01-12 01:06:13 -0800133 if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM,
134 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
135 return;
136 while (!req.complete)
137 pmu_poll();
138}
Finn Thain6df2afba2018-03-11 23:38:28 -0400139#endif /* CONFIG_ADB_PMU68K */
Al Viro32722442006-01-12 01:06:13 -0800140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
142 * VIA PRAM/RTC access routines
143 *
144 * Must be called with interrupts disabled and
145 * the RTC should be enabled.
146 */
147
148static __u8 via_pram_readbyte(void)
149{
Finn Thain31b1c782018-03-11 23:38:28 -0400150 int i, reg;
151 __u8 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 reg = via1[vBufB] & ~VIA1B_vRTCClk;
154
155 /* Set the RTC data line to be an input. */
156
157 via1[vDirB] &= ~VIA1B_vRTCData;
158
159 /* The bits of the byte come out in MSB order */
160
161 data = 0;
162 for (i = 0 ; i < 8 ; i++) {
163 via1[vBufB] = reg;
164 via1[vBufB] = reg | VIA1B_vRTCClk;
165 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
166 }
167
168 /* Return RTC data line to output state */
169
170 via1[vDirB] |= VIA1B_vRTCData;
171
172 return data;
173}
174
175static void via_pram_writebyte(__u8 data)
176{
Finn Thain31b1c782018-03-11 23:38:28 -0400177 int i, reg, bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
180
181 /* The bits of the byte go in in MSB order */
182
183 for (i = 0 ; i < 8 ; i++) {
184 bit = data & 0x80? 1 : 0;
185 data <<= 1;
186 via1[vBufB] = reg | bit;
187 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
188 }
189}
190
191/*
192 * Execute a VIA PRAM/RTC command. For read commands
193 * data should point to a one-byte buffer for the
194 * resulting data. For write commands it should point
195 * to the data byte to for the command.
196 *
197 * This function disables all interrupts while running.
198 */
199
200static void via_pram_command(int command, __u8 *data)
201{
202 unsigned long flags;
Finn Thain31b1c782018-03-11 23:38:28 -0400203 int is_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 local_irq_save(flags);
206
207 /* Enable the RTC and make sure the strobe line is high */
208
209 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
210
211 if (command & 0xFF00) { /* extended (two-byte) command */
212 via_pram_writebyte((command & 0xFF00) >> 8);
213 via_pram_writebyte(command & 0xFF);
214 is_read = command & 0x8000;
215 } else { /* one-byte command */
216 via_pram_writebyte(command);
217 is_read = command & 0x80;
218 }
219 if (is_read) {
220 *data = via_pram_readbyte();
221 } else {
222 via_pram_writebyte(*data);
223 }
224
225 /* All done, disable the RTC */
226
227 via1[vBufB] |= VIA1B_vRTCEnb;
228
229 local_irq_restore(flags);
230}
231
232static __u8 via_read_pram(int offset)
233{
234 return 0;
235}
236
237static void via_write_pram(int offset, __u8 data)
238{
239}
240
241/*
242 * Return the current time in seconds since January 1, 1904.
243 *
244 * This only works on machines with the VIA-based PRAM/RTC, which
245 * is basically any machine with Mac II-style ADB.
246 */
247
248static long via_read_time(void)
249{
250 union {
Finn Thain75a23852011-07-18 00:06:10 +1000251 __u8 cdata[4];
252 long idata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 } result, last_result;
Finn Thain75a23852011-07-18 00:06:10 +1000254 int count = 1;
255
256 via_pram_command(0x81, &last_result.cdata[3]);
257 via_pram_command(0x85, &last_result.cdata[2]);
258 via_pram_command(0x89, &last_result.cdata[1]);
259 via_pram_command(0x8D, &last_result.cdata[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /*
262 * The NetBSD guys say to loop until you get the same reading
263 * twice in a row.
264 */
265
Finn Thain75a23852011-07-18 00:06:10 +1000266 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 via_pram_command(0x81, &result.cdata[3]);
268 via_pram_command(0x85, &result.cdata[2]);
269 via_pram_command(0x89, &result.cdata[1]);
270 via_pram_command(0x8D, &result.cdata[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Finn Thain75a23852011-07-18 00:06:10 +1000272 if (result.idata == last_result.idata)
273 return result.idata - RTC_OFFSET;
274
275 if (++count > 10)
276 break;
277
278 last_result.idata = result.idata;
279 }
280
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400281 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 +1000282 last_result.idata, result.idata);
283
284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/*
288 * Set the current time to a number of seconds since January 1, 1904.
289 *
290 * This only works on machines with the VIA-based PRAM/RTC, which
291 * is basically any machine with Mac II-style ADB.
292 */
293
294static void via_write_time(long time)
295{
296 union {
Finn Thain31b1c782018-03-11 23:38:28 -0400297 __u8 cdata[4];
298 long idata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 } data;
Finn Thain31b1c782018-03-11 23:38:28 -0400300 __u8 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 /* Clear the write protect bit */
303
304 temp = 0x55;
305 via_pram_command(0x35, &temp);
306
307 data.idata = time + RTC_OFFSET;
308 via_pram_command(0x01, &data.cdata[3]);
309 via_pram_command(0x05, &data.cdata[2]);
310 via_pram_command(0x09, &data.cdata[1]);
311 via_pram_command(0x0D, &data.cdata[0]);
312
313 /* Set the write protect bit */
314
315 temp = 0xD5;
316 via_pram_command(0x35, &temp);
317}
318
319static void via_shutdown(void)
320{
321 if (rbv_present) {
322 via2[rBufB] &= ~0x04;
323 } else {
324 /* Direction of vDirB is output */
325 via2[vDirB] |= 0x04;
326 /* Send a value of 0 on that line */
327 via2[vBufB] &= ~0x04;
328 mdelay(1000);
329 }
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static void oss_shutdown(void)
333{
334 oss->rom_ctrl = OSS_POWEROFF;
335}
336
337#ifdef CONFIG_ADB_CUDA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static void cuda_restart(void)
339{
Al Viro32722442006-01-12 01:06:13 -0800340 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -0400341
Al Viro32722442006-01-12 01:06:13 -0800342 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
343 return;
344 while (!req.complete)
345 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348static void cuda_shutdown(void)
349{
Al Viro32722442006-01-12 01:06:13 -0800350 struct adb_request req;
Finn Thain31b1c782018-03-11 23:38:28 -0400351
Al Viro32722442006-01-12 01:06:13 -0800352 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
353 return;
Finn Thain41e93a32017-08-12 17:11:02 +1000354
355 /* Avoid infinite polling loop when PSU is not under Cuda control */
356 switch (macintosh_config->ident) {
357 case MAC_MODEL_C660:
358 case MAC_MODEL_Q605:
359 case MAC_MODEL_Q605_ACC:
360 case MAC_MODEL_P475:
361 case MAC_MODEL_P475F:
362 return;
363 }
364
Al Viro32722442006-01-12 01:06:13 -0800365 while (!req.complete)
366 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#endif /* CONFIG_ADB_CUDA */
369
Al Viro32722442006-01-12 01:06:13 -0800370#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372void pmu_restart(void)
373{
Al Viro32722442006-01-12 01:06:13 -0800374 struct adb_request req;
375 if (pmu_request(&req, NULL,
376 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
377 return;
378 while (!req.complete)
379 pmu_poll();
380 if (pmu_request(&req, NULL, 1, PMU_RESET) < 0)
381 return;
382 while (!req.complete)
383 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386void pmu_shutdown(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, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0)
395 return;
396 while (!req.complete)
397 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Al Viro32722442006-01-12 01:06:13 -0800400#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402/*
403 *-------------------------------------------------------------------
404 * Below this point are the generic routines; they'll dispatch to the
405 * correct routine for the hardware on which we're running.
406 *-------------------------------------------------------------------
407 */
408
409void mac_pram_read(int offset, __u8 *buffer, int len)
410{
Al Viro32722442006-01-12 01:06:13 -0800411 __u8 (*func)(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 int i;
413
Finn Thain31b1c782018-03-11 23:38:28 -0400414 switch (macintosh_config->adb_type) {
Finn Thain6df2afba2018-03-11 23:38:28 -0400415 case MAC_ADB_IOP:
416 case MAC_ADB_II:
Al Viro32722442006-01-12 01:06:13 -0800417 case MAC_ADB_PB1:
Finn Thain6df2afba2018-03-11 23:38:28 -0400418 func = via_read_pram;
Finn Thain31b1c782018-03-11 23:38:28 -0400419 break;
Finn Thain6df2afba2018-03-11 23:38:28 -0400420#ifdef CONFIG_ADB_CUDA
Finn Thainf74faec2016-12-31 19:56:26 -0500421 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800422 case MAC_ADB_CUDA:
Finn Thain31b1c782018-03-11 23:38:28 -0400423 func = cuda_read_pram;
424 break;
Finn Thain6df2afba2018-03-11 23:38:28 -0400425#endif
426#ifdef CONFIG_ADB_PMU68K
427 case MAC_ADB_PB2:
428 func = pmu_read_pram;
429 break;
430#endif
Al Viro32722442006-01-12 01:06:13 -0800431 default:
Al Viro32722442006-01-12 01:06:13 -0800432 return;
Finn Thain6df2afba2018-03-11 23:38:28 -0400433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 for (i = 0 ; i < len ; i++) {
435 buffer[i] = (*func)(offset++);
436 }
437}
438
439void mac_pram_write(int offset, __u8 *buffer, int len)
440{
Al Viro32722442006-01-12 01:06:13 -0800441 void (*func)(int, __u8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int i;
443
Finn Thain31b1c782018-03-11 23:38:28 -0400444 switch (macintosh_config->adb_type) {
Finn Thain6df2afba2018-03-11 23:38:28 -0400445 case MAC_ADB_IOP:
446 case MAC_ADB_II:
Al Viro32722442006-01-12 01:06:13 -0800447 case MAC_ADB_PB1:
Finn Thain6df2afba2018-03-11 23:38:28 -0400448 func = via_write_pram;
Finn Thain31b1c782018-03-11 23:38:28 -0400449 break;
Finn Thain6df2afba2018-03-11 23:38:28 -0400450#ifdef CONFIG_ADB_CUDA
Finn Thainf74faec2016-12-31 19:56:26 -0500451 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800452 case MAC_ADB_CUDA:
Finn Thain31b1c782018-03-11 23:38:28 -0400453 func = cuda_write_pram;
454 break;
Finn Thain6df2afba2018-03-11 23:38:28 -0400455#endif
456#ifdef CONFIG_ADB_PMU68K
457 case MAC_ADB_PB2:
458 func = pmu_write_pram;
459 break;
460#endif
Al Viro32722442006-01-12 01:06:13 -0800461 default:
Al Viro32722442006-01-12 01:06:13 -0800462 return;
Finn Thain6df2afba2018-03-11 23:38:28 -0400463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 for (i = 0 ; i < len ; i++) {
465 (*func)(offset++, buffer[i]);
466 }
467}
468
469void mac_poweroff(void)
470{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (oss_present) {
472 oss_shutdown();
473 } else if (macintosh_config->adb_type == MAC_ADB_II) {
474 via_shutdown();
475#ifdef CONFIG_ADB_CUDA
Finn Thainf74faec2016-12-31 19:56:26 -0500476 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
477 macintosh_config->adb_type == MAC_ADB_CUDA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 cuda_shutdown();
479#endif
Al Viro32722442006-01-12 01:06:13 -0800480#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 } else if (macintosh_config->adb_type == MAC_ADB_PB1
482 || macintosh_config->adb_type == MAC_ADB_PB2) {
483 pmu_shutdown();
484#endif
485 }
Finn Thain558d5ad2017-08-12 17:12:12 +1000486
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400487 pr_crit("It is now safe to turn off your Macintosh.\n");
Finn Thain558d5ad2017-08-12 17:12:12 +1000488 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 while(1);
490}
491
492void mac_reset(void)
493{
494 if (macintosh_config->adb_type == MAC_ADB_II) {
495 unsigned long flags;
496
497 /* need ROMBASE in booter */
498 /* indeed, plus need to MAP THE ROM !! */
499
500 if (mac_bi_data.rombase == 0)
501 mac_bi_data.rombase = 0x40800000;
502
503 /* works on some */
504 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
505
506 if (macintosh_config->ident == MAC_MODEL_SE30) {
507 /*
508 * MSch: Machines known to crash on ROM reset ...
509 */
510 } else {
511 local_irq_save(flags);
512
513 rom_reset();
514
515 local_irq_restore(flags);
516 }
517#ifdef CONFIG_ADB_CUDA
Finn Thainf74faec2016-12-31 19:56:26 -0500518 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
519 macintosh_config->adb_type == MAC_ADB_CUDA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 cuda_restart();
521#endif
Al Viro32722442006-01-12 01:06:13 -0800522#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 } else if (macintosh_config->adb_type == MAC_ADB_PB1
524 || macintosh_config->adb_type == MAC_ADB_PB2) {
525 pmu_restart();
526#endif
527 } else if (CPU_IS_030) {
528
529 /* 030-specific reset routine. The idea is general, but the
530 * specific registers to reset are '030-specific. Until I
531 * have a non-030 machine, I can't test anything else.
532 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
533 */
534
535 unsigned long rombase = 0x40000000;
536
537 /* make a 1-to-1 mapping, using the transparent tran. reg. */
538 unsigned long virt = (unsigned long) mac_reset;
539 unsigned long phys = virt_to_phys(mac_reset);
Al Viro77add9f2006-01-12 01:06:18 -0800540 unsigned long addr = (phys&0xFF000000)|0x8777;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 unsigned long offset = phys-virt;
Finn Thain31b1c782018-03-11 23:38:28 -0400542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 local_irq_disable(); /* lets not screw this up, ok? */
544 __asm__ __volatile__(".chip 68030\n\t"
545 "pmove %0,%/tt0\n\t"
546 ".chip 68k"
Al Viro77add9f2006-01-12 01:06:18 -0800547 : : "m" (addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* Now jump to physical address so we can disable MMU */
549 __asm__ __volatile__(
Finn Thain31b1c782018-03-11 23:38:28 -0400550 ".chip 68030\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 "lea %/pc@(1f),%/a0\n\t"
552 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
553 "addl %0,%/sp\n\t"
554 "pflusha\n\t"
555 "jmp %/a0@\n\t" /* jump into physical memory */
556 "0:.long 0\n\t" /* a constant zero. */
557 /* OK. Now reset everything and jump to reset vector. */
558 "1:\n\t"
559 "lea %/pc@(0b),%/a0\n\t"
560 "pmove %/a0@, %/tc\n\t" /* disable mmu */
561 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
562 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
563 "movel #0, %/a0\n\t"
564 "movec %/a0, %/vbr\n\t" /* clear vector base register */
565 "movec %/a0, %/cacr\n\t" /* disable caches */
566 "movel #0x0808,%/a0\n\t"
567 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
568 "movew #0x2700,%/sr\n\t" /* set up status register */
569 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
570 "movec %/a0, %/isp\n\t"
571 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
572 "reset\n\t" /* reset external devices */
573 "jmp %/a0@\n\t" /* jump to the reset vector */
574 ".chip 68k"
575 : : "r" (offset), "a" (rombase) : "a0");
576 }
577
578 /* should never get here */
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400579 pr_crit("Restart failed. Please restart manually.\n");
Finn Thain558d5ad2017-08-12 17:12:12 +1000580 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 while(1);
582}
583
584/*
585 * This function translates seconds since 1970 into a proper date.
586 *
587 * Algorithm cribbed from glibc2.1, __offtime().
588 */
589#define SECS_PER_MINUTE (60)
590#define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
591#define SECS_PER_DAY (SECS_PER_HOUR * 24)
592
593static void unmktime(unsigned long time, long offset,
594 int *yearp, int *monp, int *dayp,
595 int *hourp, int *minp, int *secp)
596{
597 /* How many days come before each month (0-12). */
598 static const unsigned short int __mon_yday[2][13] =
599 {
600 /* Normal years. */
601 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
602 /* Leap years. */
603 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
604 };
605 long int days, rem, y, wday, yday;
606 const unsigned short int *ip;
607
608 days = time / SECS_PER_DAY;
609 rem = time % SECS_PER_DAY;
610 rem += offset;
611 while (rem < 0) {
612 rem += SECS_PER_DAY;
613 --days;
614 }
615 while (rem >= SECS_PER_DAY) {
616 rem -= SECS_PER_DAY;
617 ++days;
618 }
619 *hourp = rem / SECS_PER_HOUR;
620 rem %= SECS_PER_HOUR;
621 *minp = rem / SECS_PER_MINUTE;
622 *secp = rem % SECS_PER_MINUTE;
623 /* January 1, 1970 was a Thursday. */
624 wday = (4 + days) % 7; /* Day in the week. Not currently used */
625 if (wday < 0) wday += 7;
626 y = 1970;
627
628#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
629#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
630#define __isleap(year) \
631 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
632
633 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
634 {
635 /* Guess a corrected year, assuming 365 days per year. */
636 long int yg = y + days / 365 - (days % 365 < 0);
637
638 /* Adjust DAYS and Y to match the guessed year. */
Finn Thain31b1c782018-03-11 23:38:28 -0400639 days -= (yg - y) * 365 +
640 LEAPS_THRU_END_OF(yg - 1) - LEAPS_THRU_END_OF(y - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 y = yg;
642 }
643 *yearp = y - 1900;
644 yday = days; /* day in the year. Not currently used. */
645 ip = __mon_yday[__isleap(y)];
646 for (y = 11; days < (long int) ip[y]; --y)
647 continue;
648 days -= ip[y];
649 *monp = y;
650 *dayp = days + 1; /* day in the month */
651 return;
652}
653
654/*
655 * Read/write the hardware clock.
656 */
657
658int mac_hwclk(int op, struct rtc_time *t)
659{
660 unsigned long now;
661
662 if (!op) { /* read */
Al Viro32722442006-01-12 01:06:13 -0800663 switch (macintosh_config->adb_type) {
Al Viro32722442006-01-12 01:06:13 -0800664 case MAC_ADB_IOP:
Finn Thain6df2afba2018-03-11 23:38:28 -0400665 case MAC_ADB_II:
666 case MAC_ADB_PB1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 now = via_read_time();
Al Viro32722442006-01-12 01:06:13 -0800668 break;
Finn Thain6df2afba2018-03-11 23:38:28 -0400669#ifdef CONFIG_ADB_CUDA
Finn Thainf74faec2016-12-31 19:56:26 -0500670 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800671 case MAC_ADB_CUDA:
672 now = cuda_read_time();
673 break;
Finn Thain6df2afba2018-03-11 23:38:28 -0400674#endif
675#ifdef CONFIG_ADB_PMU68K
676 case MAC_ADB_PB2:
677 now = pmu_read_time();
678 break;
679#endif
Al Viro32722442006-01-12 01:06:13 -0800680 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 now = 0;
682 }
683
684 t->tm_wday = 0;
685 unmktime(now, 0,
686 &t->tm_year, &t->tm_mon, &t->tm_mday,
687 &t->tm_hour, &t->tm_min, &t->tm_sec);
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400688 pr_debug("%s: read %04d-%02d-%-2d %02d:%02d:%02d\n",
689 __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
690 t->tm_hour, t->tm_min, t->tm_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 } else { /* write */
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400692 pr_debug("%s: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
693 __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
694 t->tm_hour, t->tm_min, t->tm_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
697 t->tm_hour, t->tm_min, t->tm_sec);
698
Al Viro32722442006-01-12 01:06:13 -0800699 switch (macintosh_config->adb_type) {
Al Viro32722442006-01-12 01:06:13 -0800700 case MAC_ADB_IOP:
Finn Thain6df2afba2018-03-11 23:38:28 -0400701 case MAC_ADB_II:
702 case MAC_ADB_PB1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 via_write_time(now);
Al Viro32722442006-01-12 01:06:13 -0800704 break;
Finn Thain6df2afba2018-03-11 23:38:28 -0400705#ifdef CONFIG_ADB_CUDA
Finn Thainf74faec2016-12-31 19:56:26 -0500706 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800707 case MAC_ADB_CUDA:
708 cuda_write_time(now);
709 break;
Finn Thain6df2afba2018-03-11 23:38:28 -0400710#endif
711#ifdef CONFIG_ADB_PMU68K
Al Viro32722442006-01-12 01:06:13 -0800712 case MAC_ADB_PB2:
713 pmu_write_time(now);
714 break;
Finn Thain6df2afba2018-03-11 23:38:28 -0400715#endif
716 default:
717 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720 return 0;
721}
722
723/*
724 * Set minutes/seconds in the hardware clock
725 */
726
727int mac_set_clock_mmss (unsigned long nowtime)
728{
729 struct rtc_time now;
730
731 mac_hwclk(0, &now);
732 now.tm_sec = nowtime % 60;
733 now.tm_min = (nowtime / 60) % 60;
734 mac_hwclk(1, &now);
735
736 return 0;
737}