blob: 4956edaac926d112675b41ead4378e07b1910b2f [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
46 time = (req.reply[3] << 24) | (req.reply[4] << 16)
47 | (req.reply[5] << 8) | req.reply[6];
48 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 data += RTC_OFFSET;
Al Viro32722442006-01-12 01:06:13 -080055 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
56 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
57 (data >> 8) & 0xFF, data & 0xFF) < 0)
58 return;
59 while (!req.complete)
60 cuda_poll();
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Al Viro32722442006-01-12 01:06:13 -080063static __u8 cuda_read_pram(int offset)
64{
65 struct adb_request req;
66 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
67 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
68 return 0;
69 while (!req.complete)
70 cuda_poll();
71 return req.reply[3];
72}
73
74static void cuda_write_pram(int offset, __u8 data)
75{
76 struct adb_request req;
77 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
78 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
79 return;
80 while (!req.complete)
81 cuda_poll();
82}
83#else
84#define cuda_read_time() 0
85#define cuda_write_time(n)
86#define cuda_read_pram NULL
87#define cuda_write_pram NULL
88#endif
89
Finn Thaind643e2d2010-06-02 15:58:24 +020090#ifdef CONFIG_ADB_PMU68K
Al Viro32722442006-01-12 01:06:13 -080091static long pmu_read_time(void)
92{
93 struct adb_request req;
94 long time;
95
96 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
97 return 0;
98 while (!req.complete)
99 pmu_poll();
100
Finn Thaind643e2d2010-06-02 15:58:24 +0200101 time = (req.reply[1] << 24) | (req.reply[2] << 16)
102 | (req.reply[3] << 8) | req.reply[4];
Al Viro32722442006-01-12 01:06:13 -0800103 return time - RTC_OFFSET;
104}
105
106static void pmu_write_time(long data)
107{
108 struct adb_request req;
109 data += RTC_OFFSET;
110 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
111 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
112 (data >> 8) & 0xFF, data & 0xFF) < 0)
113 return;
114 while (!req.complete)
115 pmu_poll();
116}
117
118static __u8 pmu_read_pram(int offset)
119{
120 struct adb_request req;
121 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;
132 if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM,
133 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
134 return;
135 while (!req.complete)
136 pmu_poll();
137}
138#else
139#define pmu_read_time() 0
140#define pmu_write_time(n)
141#define pmu_read_pram NULL
142#define pmu_write_pram NULL
143#endif
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
146 * VIA PRAM/RTC access routines
147 *
148 * Must be called with interrupts disabled and
149 * the RTC should be enabled.
150 */
151
152static __u8 via_pram_readbyte(void)
153{
154 int i,reg;
155 __u8 data;
156
157 reg = via1[vBufB] & ~VIA1B_vRTCClk;
158
159 /* Set the RTC data line to be an input. */
160
161 via1[vDirB] &= ~VIA1B_vRTCData;
162
163 /* The bits of the byte come out in MSB order */
164
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);
170 }
171
172 /* Return RTC data line to output state */
173
174 via1[vDirB] |= VIA1B_vRTCData;
175
176 return data;
177}
178
179static void via_pram_writebyte(__u8 data)
180{
181 int i,reg,bit;
182
183 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
184
185 /* The bits of the byte go in in MSB order */
186
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;
192 }
193}
194
195/*
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.
200 *
201 * This function disables all interrupts while running.
202 */
203
204static void via_pram_command(int command, __u8 *data)
205{
206 unsigned long flags;
207 int is_read;
208
209 local_irq_save(flags);
210
211 /* Enable the RTC and make sure the strobe line is high */
212
213 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
214
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;
222 }
223 if (is_read) {
224 *data = via_pram_readbyte();
225 } else {
226 via_pram_writebyte(*data);
227 }
228
229 /* All done, disable the RTC */
230
231 via1[vBufB] |= VIA1B_vRTCEnb;
232
233 local_irq_restore(flags);
234}
235
236static __u8 via_read_pram(int offset)
237{
238 return 0;
239}
240
241static void via_write_pram(int offset, __u8 data)
242{
243}
244
245/*
246 * Return the current time in seconds since January 1, 1904.
247 *
248 * This only works on machines with the VIA-based PRAM/RTC, which
249 * is basically any machine with Mac II-style ADB.
250 */
251
252static long via_read_time(void)
253{
254 union {
Finn Thain75a23852011-07-18 00:06:10 +1000255 __u8 cdata[4];
256 long idata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 } result, last_result;
Finn Thain75a23852011-07-18 00:06:10 +1000258 int count = 1;
259
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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /*
266 * The NetBSD guys say to loop until you get the same reading
267 * twice in a row.
268 */
269
Finn Thain75a23852011-07-18 00:06:10 +1000270 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Finn Thain75a23852011-07-18 00:06:10 +1000276 if (result.idata == last_result.idata)
277 return result.idata - RTC_OFFSET;
278
279 if (++count > 10)
280 break;
281
282 last_result.idata = result.idata;
283 }
284
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400285 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 +1000286 last_result.idata, result.idata);
287
288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/*
292 * Set the current time to a number of seconds since January 1, 1904.
293 *
294 * This only works on machines with the VIA-based PRAM/RTC, which
295 * is basically any machine with Mac II-style ADB.
296 */
297
298static void via_write_time(long time)
299{
300 union {
301 __u8 cdata[4];
302 long idata;
303 } data;
304 __u8 temp;
305
306 /* Clear the write protect bit */
307
308 temp = 0x55;
309 via_pram_command(0x35, &temp);
310
311 data.idata = 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]);
316
317 /* Set the write protect bit */
318
319 temp = 0xD5;
320 via_pram_command(0x35, &temp);
321}
322
323static void via_shutdown(void)
324{
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);
333 }
334}
335
336/*
337 * FIXME: not sure how this is supposed to work exactly...
338 */
339
340static void oss_shutdown(void)
341{
342 oss->rom_ctrl = OSS_POWEROFF;
343}
344
345#ifdef CONFIG_ADB_CUDA
346
347static void cuda_restart(void)
348{
Al Viro32722442006-01-12 01:06:13 -0800349 struct adb_request req;
350 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
351 return;
352 while (!req.complete)
353 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356static void cuda_shutdown(void)
357{
Al Viro32722442006-01-12 01:06:13 -0800358 struct adb_request req;
359 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
360 return;
Finn Thain41e93a32017-08-12 17:11:02 +1000361
362 /* Avoid infinite polling loop when PSU is not under Cuda control */
363 switch (macintosh_config->ident) {
364 case MAC_MODEL_C660:
365 case MAC_MODEL_Q605:
366 case MAC_MODEL_Q605_ACC:
367 case MAC_MODEL_P475:
368 case MAC_MODEL_P475F:
369 return;
370 }
371
Al Viro32722442006-01-12 01:06:13 -0800372 while (!req.complete)
373 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376#endif /* CONFIG_ADB_CUDA */
377
Al Viro32722442006-01-12 01:06:13 -0800378#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380void pmu_restart(void)
381{
Al Viro32722442006-01-12 01:06:13 -0800382 struct adb_request req;
383 if (pmu_request(&req, NULL,
384 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
385 return;
386 while (!req.complete)
387 pmu_poll();
388 if (pmu_request(&req, NULL, 1, PMU_RESET) < 0)
389 return;
390 while (!req.complete)
391 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394void pmu_shutdown(void)
395{
Al Viro32722442006-01-12 01:06:13 -0800396 struct adb_request req;
397 if (pmu_request(&req, NULL,
398 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
399 return;
400 while (!req.complete)
401 pmu_poll();
402 if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0)
403 return;
404 while (!req.complete)
405 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Al Viro32722442006-01-12 01:06:13 -0800408#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410/*
411 *-------------------------------------------------------------------
412 * Below this point are the generic routines; they'll dispatch to the
413 * correct routine for the hardware on which we're running.
414 *-------------------------------------------------------------------
415 */
416
417void mac_pram_read(int offset, __u8 *buffer, int len)
418{
Al Viro32722442006-01-12 01:06:13 -0800419 __u8 (*func)(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int i;
421
Al Viro32722442006-01-12 01:06:13 -0800422 switch(macintosh_config->adb_type) {
Al Viro32722442006-01-12 01:06:13 -0800423 case MAC_ADB_PB1:
424 case MAC_ADB_PB2:
425 func = pmu_read_pram; break;
Finn Thainf74faec2016-12-31 19:56:26 -0500426 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800427 case MAC_ADB_CUDA:
428 func = cuda_read_pram; break;
429 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 func = via_read_pram;
431 }
Al Viro32722442006-01-12 01:06:13 -0800432 if (!func)
433 return;
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
Al Viro32722442006-01-12 01:06:13 -0800444 switch(macintosh_config->adb_type) {
Al Viro32722442006-01-12 01:06:13 -0800445 case MAC_ADB_PB1:
446 case MAC_ADB_PB2:
447 func = pmu_write_pram; break;
Finn Thainf74faec2016-12-31 19:56:26 -0500448 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800449 case MAC_ADB_CUDA:
450 func = cuda_write_pram; break;
451 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 func = via_write_pram;
453 }
Al Viro32722442006-01-12 01:06:13 -0800454 if (!func)
455 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 for (i = 0 ; i < len ; i++) {
457 (*func)(offset++, buffer[i]);
458 }
459}
460
461void mac_poweroff(void)
462{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (oss_present) {
464 oss_shutdown();
465 } else if (macintosh_config->adb_type == MAC_ADB_II) {
466 via_shutdown();
467#ifdef CONFIG_ADB_CUDA
Finn Thainf74faec2016-12-31 19:56:26 -0500468 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
469 macintosh_config->adb_type == MAC_ADB_CUDA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 cuda_shutdown();
471#endif
Al Viro32722442006-01-12 01:06:13 -0800472#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else if (macintosh_config->adb_type == MAC_ADB_PB1
474 || macintosh_config->adb_type == MAC_ADB_PB2) {
475 pmu_shutdown();
476#endif
477 }
Finn Thain558d5ad2017-08-12 17:12:12 +1000478
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400479 pr_crit("It is now safe to turn off your Macintosh.\n");
Finn Thain558d5ad2017-08-12 17:12:12 +1000480 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 while(1);
482}
483
484void mac_reset(void)
485{
486 if (macintosh_config->adb_type == MAC_ADB_II) {
487 unsigned long flags;
488
489 /* need ROMBASE in booter */
490 /* indeed, plus need to MAP THE ROM !! */
491
492 if (mac_bi_data.rombase == 0)
493 mac_bi_data.rombase = 0x40800000;
494
495 /* works on some */
496 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
497
498 if (macintosh_config->ident == MAC_MODEL_SE30) {
499 /*
500 * MSch: Machines known to crash on ROM reset ...
501 */
502 } else {
503 local_irq_save(flags);
504
505 rom_reset();
506
507 local_irq_restore(flags);
508 }
509#ifdef CONFIG_ADB_CUDA
Finn Thainf74faec2016-12-31 19:56:26 -0500510 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
511 macintosh_config->adb_type == MAC_ADB_CUDA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 cuda_restart();
513#endif
Al Viro32722442006-01-12 01:06:13 -0800514#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 } else if (macintosh_config->adb_type == MAC_ADB_PB1
516 || macintosh_config->adb_type == MAC_ADB_PB2) {
517 pmu_restart();
518#endif
519 } else if (CPU_IS_030) {
520
521 /* 030-specific reset routine. The idea is general, but the
522 * specific registers to reset are '030-specific. Until I
523 * have a non-030 machine, I can't test anything else.
524 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
525 */
526
527 unsigned long rombase = 0x40000000;
528
529 /* make a 1-to-1 mapping, using the transparent tran. reg. */
530 unsigned long virt = (unsigned long) mac_reset;
531 unsigned long phys = virt_to_phys(mac_reset);
Al Viro77add9f2006-01-12 01:06:18 -0800532 unsigned long addr = (phys&0xFF000000)|0x8777;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 unsigned long offset = phys-virt;
534 local_irq_disable(); /* lets not screw this up, ok? */
535 __asm__ __volatile__(".chip 68030\n\t"
536 "pmove %0,%/tt0\n\t"
537 ".chip 68k"
Al Viro77add9f2006-01-12 01:06:18 -0800538 : : "m" (addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* Now jump to physical address so we can disable MMU */
540 __asm__ __volatile__(
541 ".chip 68030\n\t"
542 "lea %/pc@(1f),%/a0\n\t"
543 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
544 "addl %0,%/sp\n\t"
545 "pflusha\n\t"
546 "jmp %/a0@\n\t" /* jump into physical memory */
547 "0:.long 0\n\t" /* a constant zero. */
548 /* OK. Now reset everything and jump to reset vector. */
549 "1:\n\t"
550 "lea %/pc@(0b),%/a0\n\t"
551 "pmove %/a0@, %/tc\n\t" /* disable mmu */
552 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
553 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
554 "movel #0, %/a0\n\t"
555 "movec %/a0, %/vbr\n\t" /* clear vector base register */
556 "movec %/a0, %/cacr\n\t" /* disable caches */
557 "movel #0x0808,%/a0\n\t"
558 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
559 "movew #0x2700,%/sr\n\t" /* set up status register */
560 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
561 "movec %/a0, %/isp\n\t"
562 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
563 "reset\n\t" /* reset external devices */
564 "jmp %/a0@\n\t" /* jump to the reset vector */
565 ".chip 68k"
566 : : "r" (offset), "a" (rombase) : "a0");
567 }
568
569 /* should never get here */
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400570 pr_crit("Restart failed. Please restart manually.\n");
Finn Thain558d5ad2017-08-12 17:12:12 +1000571 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 while(1);
573}
574
575/*
576 * This function translates seconds since 1970 into a proper date.
577 *
578 * Algorithm cribbed from glibc2.1, __offtime().
579 */
580#define SECS_PER_MINUTE (60)
581#define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
582#define SECS_PER_DAY (SECS_PER_HOUR * 24)
583
584static void unmktime(unsigned long time, long offset,
585 int *yearp, int *monp, int *dayp,
586 int *hourp, int *minp, int *secp)
587{
588 /* How many days come before each month (0-12). */
589 static const unsigned short int __mon_yday[2][13] =
590 {
591 /* Normal years. */
592 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
593 /* Leap years. */
594 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
595 };
596 long int days, rem, y, wday, yday;
597 const unsigned short int *ip;
598
599 days = time / SECS_PER_DAY;
600 rem = time % SECS_PER_DAY;
601 rem += offset;
602 while (rem < 0) {
603 rem += SECS_PER_DAY;
604 --days;
605 }
606 while (rem >= SECS_PER_DAY) {
607 rem -= SECS_PER_DAY;
608 ++days;
609 }
610 *hourp = rem / SECS_PER_HOUR;
611 rem %= SECS_PER_HOUR;
612 *minp = rem / SECS_PER_MINUTE;
613 *secp = rem % SECS_PER_MINUTE;
614 /* January 1, 1970 was a Thursday. */
615 wday = (4 + days) % 7; /* Day in the week. Not currently used */
616 if (wday < 0) wday += 7;
617 y = 1970;
618
619#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
620#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
621#define __isleap(year) \
622 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
623
624 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
625 {
626 /* Guess a corrected year, assuming 365 days per year. */
627 long int yg = y + days / 365 - (days % 365 < 0);
628
629 /* Adjust DAYS and Y to match the guessed year. */
630 days -= ((yg - y) * 365
631 + LEAPS_THRU_END_OF (yg - 1)
632 - LEAPS_THRU_END_OF (y - 1));
633 y = yg;
634 }
635 *yearp = y - 1900;
636 yday = days; /* day in the year. Not currently used. */
637 ip = __mon_yday[__isleap(y)];
638 for (y = 11; days < (long int) ip[y]; --y)
639 continue;
640 days -= ip[y];
641 *monp = y;
642 *dayp = days + 1; /* day in the month */
643 return;
644}
645
646/*
647 * Read/write the hardware clock.
648 */
649
650int mac_hwclk(int op, struct rtc_time *t)
651{
652 unsigned long now;
653
654 if (!op) { /* read */
Al Viro32722442006-01-12 01:06:13 -0800655 switch (macintosh_config->adb_type) {
656 case MAC_ADB_II:
657 case MAC_ADB_IOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 now = via_read_time();
Al Viro32722442006-01-12 01:06:13 -0800659 break;
Al Viro32722442006-01-12 01:06:13 -0800660 case MAC_ADB_PB1:
661 case MAC_ADB_PB2:
662 now = pmu_read_time();
663 break;
Finn Thainf74faec2016-12-31 19:56:26 -0500664 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800665 case MAC_ADB_CUDA:
666 now = cuda_read_time();
667 break;
668 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 now = 0;
670 }
671
672 t->tm_wday = 0;
673 unmktime(now, 0,
674 &t->tm_year, &t->tm_mon, &t->tm_mday,
675 &t->tm_hour, &t->tm_min, &t->tm_sec);
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400676 pr_debug("%s: read %04d-%02d-%-2d %02d:%02d:%02d\n",
677 __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
678 t->tm_hour, t->tm_min, t->tm_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 } else { /* write */
Geert Uytterhoeven889121b2017-04-08 19:51:15 -0400680 pr_debug("%s: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
681 __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
682 t->tm_hour, t->tm_min, t->tm_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
685 t->tm_hour, t->tm_min, t->tm_sec);
686
Al Viro32722442006-01-12 01:06:13 -0800687 switch (macintosh_config->adb_type) {
688 case MAC_ADB_II:
689 case MAC_ADB_IOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 via_write_time(now);
Al Viro32722442006-01-12 01:06:13 -0800691 break;
Finn Thainf74faec2016-12-31 19:56:26 -0500692 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800693 case MAC_ADB_CUDA:
694 cuda_write_time(now);
695 break;
696 case MAC_ADB_PB1:
697 case MAC_ADB_PB2:
698 pmu_write_time(now);
699 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702 return 0;
703}
704
705/*
706 * Set minutes/seconds in the hardware clock
707 */
708
709int mac_set_clock_mmss (unsigned long nowtime)
710{
711 struct rtc_time now;
712
713 mac_hwclk(0, &now);
714 now.tm_sec = nowtime % 60;
715 now.tm_min = (nowtime / 60) % 60;
716 mac_hwclk(1, &now);
717
718 return 0;
719}