blob: f4bb73fcb67af2b0d3e1702f43565e7ef8789844 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Miscellaneous Mac68K-specific stuff
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/types.h>
6#include <linux/errno.h>
7#include <linux/miscdevice.h>
8#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
285 pr_err("via_read_time: failed to read a stable value; "
286 "got 0x%08lx then 0x%08lx\n",
287 last_result.idata, result.idata);
288
289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/*
293 * Set the current time to a number of seconds since January 1, 1904.
294 *
295 * This only works on machines with the VIA-based PRAM/RTC, which
296 * is basically any machine with Mac II-style ADB.
297 */
298
299static void via_write_time(long time)
300{
301 union {
302 __u8 cdata[4];
303 long idata;
304 } data;
305 __u8 temp;
306
307 /* Clear the write protect bit */
308
309 temp = 0x55;
310 via_pram_command(0x35, &temp);
311
312 data.idata = time + RTC_OFFSET;
313 via_pram_command(0x01, &data.cdata[3]);
314 via_pram_command(0x05, &data.cdata[2]);
315 via_pram_command(0x09, &data.cdata[1]);
316 via_pram_command(0x0D, &data.cdata[0]);
317
318 /* Set the write protect bit */
319
320 temp = 0xD5;
321 via_pram_command(0x35, &temp);
322}
323
324static void via_shutdown(void)
325{
326 if (rbv_present) {
327 via2[rBufB] &= ~0x04;
328 } else {
329 /* Direction of vDirB is output */
330 via2[vDirB] |= 0x04;
331 /* Send a value of 0 on that line */
332 via2[vBufB] &= ~0x04;
333 mdelay(1000);
334 }
335}
336
337/*
338 * FIXME: not sure how this is supposed to work exactly...
339 */
340
341static void oss_shutdown(void)
342{
343 oss->rom_ctrl = OSS_POWEROFF;
344}
345
346#ifdef CONFIG_ADB_CUDA
347
348static void cuda_restart(void)
349{
Al Viro32722442006-01-12 01:06:13 -0800350 struct adb_request req;
351 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
352 return;
353 while (!req.complete)
354 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357static void cuda_shutdown(void)
358{
Al Viro32722442006-01-12 01:06:13 -0800359 struct adb_request req;
360 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
361 return;
362 while (!req.complete)
363 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366#endif /* CONFIG_ADB_CUDA */
367
Al Viro32722442006-01-12 01:06:13 -0800368#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370void pmu_restart(void)
371{
Al Viro32722442006-01-12 01:06:13 -0800372 struct adb_request req;
373 if (pmu_request(&req, NULL,
374 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
375 return;
376 while (!req.complete)
377 pmu_poll();
378 if (pmu_request(&req, NULL, 1, PMU_RESET) < 0)
379 return;
380 while (!req.complete)
381 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384void pmu_shutdown(void)
385{
Al Viro32722442006-01-12 01:06:13 -0800386 struct adb_request req;
387 if (pmu_request(&req, NULL,
388 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
389 return;
390 while (!req.complete)
391 pmu_poll();
392 if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0)
393 return;
394 while (!req.complete)
395 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Al Viro32722442006-01-12 01:06:13 -0800398#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400/*
401 *-------------------------------------------------------------------
402 * Below this point are the generic routines; they'll dispatch to the
403 * correct routine for the hardware on which we're running.
404 *-------------------------------------------------------------------
405 */
406
407void mac_pram_read(int offset, __u8 *buffer, int len)
408{
Al Viro32722442006-01-12 01:06:13 -0800409 __u8 (*func)(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int i;
411
Al Viro32722442006-01-12 01:06:13 -0800412 switch(macintosh_config->adb_type) {
Al Viro32722442006-01-12 01:06:13 -0800413 case MAC_ADB_PB1:
414 case MAC_ADB_PB2:
415 func = pmu_read_pram; break;
Finn Thainf74faec2016-12-31 19:56:26 -0500416 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800417 case MAC_ADB_CUDA:
418 func = cuda_read_pram; break;
419 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 func = via_read_pram;
421 }
Al Viro32722442006-01-12 01:06:13 -0800422 if (!func)
423 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 for (i = 0 ; i < len ; i++) {
425 buffer[i] = (*func)(offset++);
426 }
427}
428
429void mac_pram_write(int offset, __u8 *buffer, int len)
430{
Al Viro32722442006-01-12 01:06:13 -0800431 void (*func)(int, __u8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int i;
433
Al Viro32722442006-01-12 01:06:13 -0800434 switch(macintosh_config->adb_type) {
Al Viro32722442006-01-12 01:06:13 -0800435 case MAC_ADB_PB1:
436 case MAC_ADB_PB2:
437 func = pmu_write_pram; break;
Finn Thainf74faec2016-12-31 19:56:26 -0500438 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800439 case MAC_ADB_CUDA:
440 func = cuda_write_pram; break;
441 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 func = via_write_pram;
443 }
Al Viro32722442006-01-12 01:06:13 -0800444 if (!func)
445 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 for (i = 0 ; i < len ; i++) {
447 (*func)(offset++, buffer[i]);
448 }
449}
450
451void mac_poweroff(void)
452{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (oss_present) {
454 oss_shutdown();
455 } else if (macintosh_config->adb_type == MAC_ADB_II) {
456 via_shutdown();
457#ifdef CONFIG_ADB_CUDA
Finn Thainf74faec2016-12-31 19:56:26 -0500458 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
459 macintosh_config->adb_type == MAC_ADB_CUDA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 cuda_shutdown();
461#endif
Al Viro32722442006-01-12 01:06:13 -0800462#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 } else if (macintosh_config->adb_type == MAC_ADB_PB1
464 || macintosh_config->adb_type == MAC_ADB_PB2) {
465 pmu_shutdown();
466#endif
467 }
468 local_irq_enable();
469 printk("It is now safe to turn off your Macintosh.\n");
470 while(1);
471}
472
473void mac_reset(void)
474{
475 if (macintosh_config->adb_type == MAC_ADB_II) {
476 unsigned long flags;
477
478 /* need ROMBASE in booter */
479 /* indeed, plus need to MAP THE ROM !! */
480
481 if (mac_bi_data.rombase == 0)
482 mac_bi_data.rombase = 0x40800000;
483
484 /* works on some */
485 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
486
487 if (macintosh_config->ident == MAC_MODEL_SE30) {
488 /*
489 * MSch: Machines known to crash on ROM reset ...
490 */
491 } else {
492 local_irq_save(flags);
493
494 rom_reset();
495
496 local_irq_restore(flags);
497 }
498#ifdef CONFIG_ADB_CUDA
Finn Thainf74faec2016-12-31 19:56:26 -0500499 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
500 macintosh_config->adb_type == MAC_ADB_CUDA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 cuda_restart();
502#endif
Al Viro32722442006-01-12 01:06:13 -0800503#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 } else if (macintosh_config->adb_type == MAC_ADB_PB1
505 || macintosh_config->adb_type == MAC_ADB_PB2) {
506 pmu_restart();
507#endif
508 } else if (CPU_IS_030) {
509
510 /* 030-specific reset routine. The idea is general, but the
511 * specific registers to reset are '030-specific. Until I
512 * have a non-030 machine, I can't test anything else.
513 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
514 */
515
516 unsigned long rombase = 0x40000000;
517
518 /* make a 1-to-1 mapping, using the transparent tran. reg. */
519 unsigned long virt = (unsigned long) mac_reset;
520 unsigned long phys = virt_to_phys(mac_reset);
Al Viro77add9f2006-01-12 01:06:18 -0800521 unsigned long addr = (phys&0xFF000000)|0x8777;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 unsigned long offset = phys-virt;
523 local_irq_disable(); /* lets not screw this up, ok? */
524 __asm__ __volatile__(".chip 68030\n\t"
525 "pmove %0,%/tt0\n\t"
526 ".chip 68k"
Al Viro77add9f2006-01-12 01:06:18 -0800527 : : "m" (addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* Now jump to physical address so we can disable MMU */
529 __asm__ __volatile__(
530 ".chip 68030\n\t"
531 "lea %/pc@(1f),%/a0\n\t"
532 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
533 "addl %0,%/sp\n\t"
534 "pflusha\n\t"
535 "jmp %/a0@\n\t" /* jump into physical memory */
536 "0:.long 0\n\t" /* a constant zero. */
537 /* OK. Now reset everything and jump to reset vector. */
538 "1:\n\t"
539 "lea %/pc@(0b),%/a0\n\t"
540 "pmove %/a0@, %/tc\n\t" /* disable mmu */
541 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
542 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
543 "movel #0, %/a0\n\t"
544 "movec %/a0, %/vbr\n\t" /* clear vector base register */
545 "movec %/a0, %/cacr\n\t" /* disable caches */
546 "movel #0x0808,%/a0\n\t"
547 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
548 "movew #0x2700,%/sr\n\t" /* set up status register */
549 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
550 "movec %/a0, %/isp\n\t"
551 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
552 "reset\n\t" /* reset external devices */
553 "jmp %/a0@\n\t" /* jump to the reset vector */
554 ".chip 68k"
555 : : "r" (offset), "a" (rombase) : "a0");
556 }
557
558 /* should never get here */
559 local_irq_enable();
560 printk ("Restart failed. Please restart manually.\n");
561 while(1);
562}
563
564/*
565 * This function translates seconds since 1970 into a proper date.
566 *
567 * Algorithm cribbed from glibc2.1, __offtime().
568 */
569#define SECS_PER_MINUTE (60)
570#define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
571#define SECS_PER_DAY (SECS_PER_HOUR * 24)
572
573static void unmktime(unsigned long time, long offset,
574 int *yearp, int *monp, int *dayp,
575 int *hourp, int *minp, int *secp)
576{
577 /* How many days come before each month (0-12). */
578 static const unsigned short int __mon_yday[2][13] =
579 {
580 /* Normal years. */
581 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
582 /* Leap years. */
583 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
584 };
585 long int days, rem, y, wday, yday;
586 const unsigned short int *ip;
587
588 days = time / SECS_PER_DAY;
589 rem = time % SECS_PER_DAY;
590 rem += offset;
591 while (rem < 0) {
592 rem += SECS_PER_DAY;
593 --days;
594 }
595 while (rem >= SECS_PER_DAY) {
596 rem -= SECS_PER_DAY;
597 ++days;
598 }
599 *hourp = rem / SECS_PER_HOUR;
600 rem %= SECS_PER_HOUR;
601 *minp = rem / SECS_PER_MINUTE;
602 *secp = rem % SECS_PER_MINUTE;
603 /* January 1, 1970 was a Thursday. */
604 wday = (4 + days) % 7; /* Day in the week. Not currently used */
605 if (wday < 0) wday += 7;
606 y = 1970;
607
608#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
609#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
610#define __isleap(year) \
611 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
612
613 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
614 {
615 /* Guess a corrected year, assuming 365 days per year. */
616 long int yg = y + days / 365 - (days % 365 < 0);
617
618 /* Adjust DAYS and Y to match the guessed year. */
619 days -= ((yg - y) * 365
620 + LEAPS_THRU_END_OF (yg - 1)
621 - LEAPS_THRU_END_OF (y - 1));
622 y = yg;
623 }
624 *yearp = y - 1900;
625 yday = days; /* day in the year. Not currently used. */
626 ip = __mon_yday[__isleap(y)];
627 for (y = 11; days < (long int) ip[y]; --y)
628 continue;
629 days -= ip[y];
630 *monp = y;
631 *dayp = days + 1; /* day in the month */
632 return;
633}
634
635/*
636 * Read/write the hardware clock.
637 */
638
639int mac_hwclk(int op, struct rtc_time *t)
640{
641 unsigned long now;
642
643 if (!op) { /* read */
Al Viro32722442006-01-12 01:06:13 -0800644 switch (macintosh_config->adb_type) {
645 case MAC_ADB_II:
646 case MAC_ADB_IOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 now = via_read_time();
Al Viro32722442006-01-12 01:06:13 -0800648 break;
Al Viro32722442006-01-12 01:06:13 -0800649 case MAC_ADB_PB1:
650 case MAC_ADB_PB2:
651 now = pmu_read_time();
652 break;
Finn Thainf74faec2016-12-31 19:56:26 -0500653 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800654 case MAC_ADB_CUDA:
655 now = cuda_read_time();
656 break;
657 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 now = 0;
659 }
660
661 t->tm_wday = 0;
662 unmktime(now, 0,
663 &t->tm_year, &t->tm_mon, &t->tm_mday,
664 &t->tm_hour, &t->tm_min, &t->tm_sec);
Finn Thain40f7f9c2008-11-18 20:45:20 +0100665#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 printk("mac_hwclk: read %04d-%02d-%-2d %02d:%02d:%02d\n",
Finn Thain40f7f9c2008-11-18 20:45:20 +0100667 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
668 t->tm_hour, t->tm_min, t->tm_sec);
669#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 } else { /* write */
Finn Thain40f7f9c2008-11-18 20:45:20 +0100671#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 printk("mac_hwclk: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
Finn Thain40f7f9c2008-11-18 20:45:20 +0100673 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
674 t->tm_hour, t->tm_min, t->tm_sec);
675#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
678 t->tm_hour, t->tm_min, t->tm_sec);
679
Al Viro32722442006-01-12 01:06:13 -0800680 switch (macintosh_config->adb_type) {
681 case MAC_ADB_II:
682 case MAC_ADB_IOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 via_write_time(now);
Al Viro32722442006-01-12 01:06:13 -0800684 break;
Finn Thainf74faec2016-12-31 19:56:26 -0500685 case MAC_ADB_EGRET:
Al Viro32722442006-01-12 01:06:13 -0800686 case MAC_ADB_CUDA:
687 cuda_write_time(now);
688 break;
689 case MAC_ADB_PB1:
690 case MAC_ADB_PB2:
691 pmu_write_time(now);
692 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695 return 0;
696}
697
698/*
699 * Set minutes/seconds in the hardware clock
700 */
701
702int mac_set_clock_mmss (unsigned long nowtime)
703{
704 struct rtc_time now;
705
706 mac_hwclk(0, &now);
707 now.tm_sec = nowtime % 60;
708 now.tm_min = (nowtime / 60) % 60;
709 mac_hwclk(1, &now);
710
711 return 0;
712}