blob: 707b61aea2030c8f7de9058a2546a8eb85ce1639 [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
19#include <asm/uaccess.h>
20#include <asm/io.h>
21#include <asm/rtc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/segment.h>
23#include <asm/setup.h>
24#include <asm/macintosh.h>
25#include <asm/mac_via.h>
26#include <asm/mac_oss.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/machdep.h>
29
30/* Offset between Unix time (1970-based) and Mac time (1904-based) */
31
32#define RTC_OFFSET 2082844800
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static void (*rom_reset)(void);
35
Al Viro32722442006-01-12 01:06:13 -080036#ifdef CONFIG_ADB_CUDA
37static long cuda_read_time(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Al Viro32722442006-01-12 01:06:13 -080039 struct adb_request req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 long time;
41
Al Viro32722442006-01-12 01:06:13 -080042 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
43 return 0;
44 while (!req.complete)
45 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 time = (req.reply[3] << 24) | (req.reply[4] << 16)
48 | (req.reply[5] << 8) | req.reply[6];
49 return time - RTC_OFFSET;
50}
51
Al Viro32722442006-01-12 01:06:13 -080052static void cuda_write_time(long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Al Viro32722442006-01-12 01:06:13 -080054 struct adb_request req;
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,
57 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
58 (data >> 8) & 0xFF, data & 0xFF) < 0)
59 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;
67 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
68 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
69 return 0;
70 while (!req.complete)
71 cuda_poll();
72 return req.reply[3];
73}
74
75static void cuda_write_pram(int offset, __u8 data)
76{
77 struct adb_request req;
78 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
79 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
80 return;
81 while (!req.complete)
82 cuda_poll();
83}
84#else
85#define cuda_read_time() 0
86#define cuda_write_time(n)
87#define cuda_read_pram NULL
88#define cuda_write_pram NULL
89#endif
90
Finn Thaind643e2d2010-06-02 15:58:24 +020091#ifdef CONFIG_ADB_PMU68K
Al Viro32722442006-01-12 01:06:13 -080092static long pmu_read_time(void)
93{
94 struct adb_request req;
95 long time;
96
97 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
98 return 0;
99 while (!req.complete)
100 pmu_poll();
101
Finn Thaind643e2d2010-06-02 15:58:24 +0200102 time = (req.reply[1] << 24) | (req.reply[2] << 16)
103 | (req.reply[3] << 8) | req.reply[4];
Al Viro32722442006-01-12 01:06:13 -0800104 return time - RTC_OFFSET;
105}
106
107static void pmu_write_time(long data)
108{
109 struct adb_request req;
110 data += RTC_OFFSET;
111 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
112 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
113 (data >> 8) & 0xFF, data & 0xFF) < 0)
114 return;
115 while (!req.complete)
116 pmu_poll();
117}
118
119static __u8 pmu_read_pram(int offset)
120{
121 struct adb_request req;
122 if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM,
123 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
124 return 0;
125 while (!req.complete)
126 pmu_poll();
127 return req.reply[3];
128}
129
130static void pmu_write_pram(int offset, __u8 data)
131{
132 struct adb_request req;
133 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}
139#else
140#define pmu_read_time() 0
141#define pmu_write_time(n)
142#define pmu_read_pram NULL
143#define pmu_write_pram NULL
144#endif
145
Finn Thain40f7f9c2008-11-18 20:45:20 +0100146#if 0 /* def CONFIG_ADB_MACIISI */
Al Viro32722442006-01-12 01:06:13 -0800147extern int maciisi_request(struct adb_request *req,
148 void (*done)(struct adb_request *), int nbytes, ...);
149
150static long maciisi_read_time(void)
151{
152 struct adb_request req;
153 long time;
154
155 if (maciisi_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME))
156 return 0;
157
158 time = (req.reply[3] << 24) | (req.reply[4] << 16)
159 | (req.reply[5] << 8) | req.reply[6];
160 return time - RTC_OFFSET;
161}
162
163static void maciisi_write_time(long data)
164{
165 struct adb_request req;
166 data += RTC_OFFSET;
167 maciisi_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
169 (data >> 8) & 0xFF, data & 0xFF);
170}
171
Al Viro32722442006-01-12 01:06:13 -0800172static __u8 maciisi_read_pram(int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Al Viro32722442006-01-12 01:06:13 -0800174 struct adb_request req;
175 if (maciisi_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
176 (offset >> 8) & 0xFF, offset & 0xFF))
177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return req.reply[3];
179}
180
Al Viro32722442006-01-12 01:06:13 -0800181static void maciisi_write_pram(int offset, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Al Viro32722442006-01-12 01:06:13 -0800183 struct adb_request req;
184 maciisi_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
185 (offset >> 8) & 0xFF, offset & 0xFF, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Al Viro32722442006-01-12 01:06:13 -0800187#else
188#define maciisi_read_time() 0
189#define maciisi_write_time(n)
190#define maciisi_read_pram NULL
191#define maciisi_write_pram NULL
192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194/*
195 * VIA PRAM/RTC access routines
196 *
197 * Must be called with interrupts disabled and
198 * the RTC should be enabled.
199 */
200
201static __u8 via_pram_readbyte(void)
202{
203 int i,reg;
204 __u8 data;
205
206 reg = via1[vBufB] & ~VIA1B_vRTCClk;
207
208 /* Set the RTC data line to be an input. */
209
210 via1[vDirB] &= ~VIA1B_vRTCData;
211
212 /* The bits of the byte come out in MSB order */
213
214 data = 0;
215 for (i = 0 ; i < 8 ; i++) {
216 via1[vBufB] = reg;
217 via1[vBufB] = reg | VIA1B_vRTCClk;
218 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
219 }
220
221 /* Return RTC data line to output state */
222
223 via1[vDirB] |= VIA1B_vRTCData;
224
225 return data;
226}
227
228static void via_pram_writebyte(__u8 data)
229{
230 int i,reg,bit;
231
232 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
233
234 /* The bits of the byte go in in MSB order */
235
236 for (i = 0 ; i < 8 ; i++) {
237 bit = data & 0x80? 1 : 0;
238 data <<= 1;
239 via1[vBufB] = reg | bit;
240 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
241 }
242}
243
244/*
245 * Execute a VIA PRAM/RTC command. For read commands
246 * data should point to a one-byte buffer for the
247 * resulting data. For write commands it should point
248 * to the data byte to for the command.
249 *
250 * This function disables all interrupts while running.
251 */
252
253static void via_pram_command(int command, __u8 *data)
254{
255 unsigned long flags;
256 int is_read;
257
258 local_irq_save(flags);
259
260 /* Enable the RTC and make sure the strobe line is high */
261
262 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
263
264 if (command & 0xFF00) { /* extended (two-byte) command */
265 via_pram_writebyte((command & 0xFF00) >> 8);
266 via_pram_writebyte(command & 0xFF);
267 is_read = command & 0x8000;
268 } else { /* one-byte command */
269 via_pram_writebyte(command);
270 is_read = command & 0x80;
271 }
272 if (is_read) {
273 *data = via_pram_readbyte();
274 } else {
275 via_pram_writebyte(*data);
276 }
277
278 /* All done, disable the RTC */
279
280 via1[vBufB] |= VIA1B_vRTCEnb;
281
282 local_irq_restore(flags);
283}
284
285static __u8 via_read_pram(int offset)
286{
287 return 0;
288}
289
290static void via_write_pram(int offset, __u8 data)
291{
292}
293
294/*
295 * Return the current time in seconds since January 1, 1904.
296 *
297 * This only works on machines with the VIA-based PRAM/RTC, which
298 * is basically any machine with Mac II-style ADB.
299 */
300
301static long via_read_time(void)
302{
303 union {
Finn Thain75a23852011-07-18 00:06:10 +1000304 __u8 cdata[4];
305 long idata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 } result, last_result;
Finn Thain75a23852011-07-18 00:06:10 +1000307 int count = 1;
308
309 via_pram_command(0x81, &last_result.cdata[3]);
310 via_pram_command(0x85, &last_result.cdata[2]);
311 via_pram_command(0x89, &last_result.cdata[1]);
312 via_pram_command(0x8D, &last_result.cdata[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /*
315 * The NetBSD guys say to loop until you get the same reading
316 * twice in a row.
317 */
318
Finn Thain75a23852011-07-18 00:06:10 +1000319 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 via_pram_command(0x81, &result.cdata[3]);
321 via_pram_command(0x85, &result.cdata[2]);
322 via_pram_command(0x89, &result.cdata[1]);
323 via_pram_command(0x8D, &result.cdata[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Finn Thain75a23852011-07-18 00:06:10 +1000325 if (result.idata == last_result.idata)
326 return result.idata - RTC_OFFSET;
327
328 if (++count > 10)
329 break;
330
331 last_result.idata = result.idata;
332 }
333
334 pr_err("via_read_time: failed to read a stable value; "
335 "got 0x%08lx then 0x%08lx\n",
336 last_result.idata, result.idata);
337
338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341/*
342 * Set the current time to a number of seconds since January 1, 1904.
343 *
344 * This only works on machines with the VIA-based PRAM/RTC, which
345 * is basically any machine with Mac II-style ADB.
346 */
347
348static void via_write_time(long time)
349{
350 union {
351 __u8 cdata[4];
352 long idata;
353 } data;
354 __u8 temp;
355
356 /* Clear the write protect bit */
357
358 temp = 0x55;
359 via_pram_command(0x35, &temp);
360
361 data.idata = time + RTC_OFFSET;
362 via_pram_command(0x01, &data.cdata[3]);
363 via_pram_command(0x05, &data.cdata[2]);
364 via_pram_command(0x09, &data.cdata[1]);
365 via_pram_command(0x0D, &data.cdata[0]);
366
367 /* Set the write protect bit */
368
369 temp = 0xD5;
370 via_pram_command(0x35, &temp);
371}
372
373static void via_shutdown(void)
374{
375 if (rbv_present) {
376 via2[rBufB] &= ~0x04;
377 } else {
378 /* Direction of vDirB is output */
379 via2[vDirB] |= 0x04;
380 /* Send a value of 0 on that line */
381 via2[vBufB] &= ~0x04;
382 mdelay(1000);
383 }
384}
385
386/*
387 * FIXME: not sure how this is supposed to work exactly...
388 */
389
390static void oss_shutdown(void)
391{
392 oss->rom_ctrl = OSS_POWEROFF;
393}
394
395#ifdef CONFIG_ADB_CUDA
396
397static void cuda_restart(void)
398{
Al Viro32722442006-01-12 01:06:13 -0800399 struct adb_request req;
400 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
401 return;
402 while (!req.complete)
403 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406static void cuda_shutdown(void)
407{
Al Viro32722442006-01-12 01:06:13 -0800408 struct adb_request req;
409 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
410 return;
411 while (!req.complete)
412 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
415#endif /* CONFIG_ADB_CUDA */
416
Al Viro32722442006-01-12 01:06:13 -0800417#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419void pmu_restart(void)
420{
Al Viro32722442006-01-12 01:06:13 -0800421 struct adb_request req;
422 if (pmu_request(&req, NULL,
423 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
424 return;
425 while (!req.complete)
426 pmu_poll();
427 if (pmu_request(&req, NULL, 1, PMU_RESET) < 0)
428 return;
429 while (!req.complete)
430 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433void pmu_shutdown(void)
434{
Al Viro32722442006-01-12 01:06:13 -0800435 struct adb_request req;
436 if (pmu_request(&req, NULL,
437 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
438 return;
439 while (!req.complete)
440 pmu_poll();
441 if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0)
442 return;
443 while (!req.complete)
444 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Al Viro32722442006-01-12 01:06:13 -0800447#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449/*
450 *-------------------------------------------------------------------
451 * Below this point are the generic routines; they'll dispatch to the
452 * correct routine for the hardware on which we're running.
453 *-------------------------------------------------------------------
454 */
455
456void mac_pram_read(int offset, __u8 *buffer, int len)
457{
Al Viro32722442006-01-12 01:06:13 -0800458 __u8 (*func)(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int i;
460
Al Viro32722442006-01-12 01:06:13 -0800461 switch(macintosh_config->adb_type) {
462 case MAC_ADB_IISI:
463 func = maciisi_read_pram; break;
464 case MAC_ADB_PB1:
465 case MAC_ADB_PB2:
466 func = pmu_read_pram; break;
467 case MAC_ADB_CUDA:
468 func = cuda_read_pram; break;
469 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 func = via_read_pram;
471 }
Al Viro32722442006-01-12 01:06:13 -0800472 if (!func)
473 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 for (i = 0 ; i < len ; i++) {
475 buffer[i] = (*func)(offset++);
476 }
477}
478
479void mac_pram_write(int offset, __u8 *buffer, int len)
480{
Al Viro32722442006-01-12 01:06:13 -0800481 void (*func)(int, __u8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int i;
483
Al Viro32722442006-01-12 01:06:13 -0800484 switch(macintosh_config->adb_type) {
485 case MAC_ADB_IISI:
486 func = maciisi_write_pram; break;
487 case MAC_ADB_PB1:
488 case MAC_ADB_PB2:
489 func = pmu_write_pram; break;
490 case MAC_ADB_CUDA:
491 func = cuda_write_pram; break;
492 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 func = via_write_pram;
494 }
Al Viro32722442006-01-12 01:06:13 -0800495 if (!func)
496 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 for (i = 0 ; i < len ; i++) {
498 (*func)(offset++, buffer[i]);
499 }
500}
501
502void mac_poweroff(void)
503{
504 /*
505 * MAC_ADB_IISI may need to be moved up here if it doesn't actually
506 * work using the ADB packet method. --David Kilzer
507 */
508
509 if (oss_present) {
510 oss_shutdown();
511 } else if (macintosh_config->adb_type == MAC_ADB_II) {
512 via_shutdown();
513#ifdef CONFIG_ADB_CUDA
514 } else if (macintosh_config->adb_type == MAC_ADB_CUDA) {
515 cuda_shutdown();
516#endif
Al Viro32722442006-01-12 01:06:13 -0800517#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 } else if (macintosh_config->adb_type == MAC_ADB_PB1
519 || macintosh_config->adb_type == MAC_ADB_PB2) {
520 pmu_shutdown();
521#endif
522 }
523 local_irq_enable();
524 printk("It is now safe to turn off your Macintosh.\n");
525 while(1);
526}
527
528void mac_reset(void)
529{
530 if (macintosh_config->adb_type == MAC_ADB_II) {
531 unsigned long flags;
532
533 /* need ROMBASE in booter */
534 /* indeed, plus need to MAP THE ROM !! */
535
536 if (mac_bi_data.rombase == 0)
537 mac_bi_data.rombase = 0x40800000;
538
539 /* works on some */
540 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
541
542 if (macintosh_config->ident == MAC_MODEL_SE30) {
543 /*
544 * MSch: Machines known to crash on ROM reset ...
545 */
546 } else {
547 local_irq_save(flags);
548
549 rom_reset();
550
551 local_irq_restore(flags);
552 }
553#ifdef CONFIG_ADB_CUDA
554 } else if (macintosh_config->adb_type == MAC_ADB_CUDA) {
555 cuda_restart();
556#endif
Al Viro32722442006-01-12 01:06:13 -0800557#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 } else if (macintosh_config->adb_type == MAC_ADB_PB1
559 || macintosh_config->adb_type == MAC_ADB_PB2) {
560 pmu_restart();
561#endif
562 } else if (CPU_IS_030) {
563
564 /* 030-specific reset routine. The idea is general, but the
565 * specific registers to reset are '030-specific. Until I
566 * have a non-030 machine, I can't test anything else.
567 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
568 */
569
570 unsigned long rombase = 0x40000000;
571
572 /* make a 1-to-1 mapping, using the transparent tran. reg. */
573 unsigned long virt = (unsigned long) mac_reset;
574 unsigned long phys = virt_to_phys(mac_reset);
Al Viro77add9f2006-01-12 01:06:18 -0800575 unsigned long addr = (phys&0xFF000000)|0x8777;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 unsigned long offset = phys-virt;
577 local_irq_disable(); /* lets not screw this up, ok? */
578 __asm__ __volatile__(".chip 68030\n\t"
579 "pmove %0,%/tt0\n\t"
580 ".chip 68k"
Al Viro77add9f2006-01-12 01:06:18 -0800581 : : "m" (addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Now jump to physical address so we can disable MMU */
583 __asm__ __volatile__(
584 ".chip 68030\n\t"
585 "lea %/pc@(1f),%/a0\n\t"
586 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
587 "addl %0,%/sp\n\t"
588 "pflusha\n\t"
589 "jmp %/a0@\n\t" /* jump into physical memory */
590 "0:.long 0\n\t" /* a constant zero. */
591 /* OK. Now reset everything and jump to reset vector. */
592 "1:\n\t"
593 "lea %/pc@(0b),%/a0\n\t"
594 "pmove %/a0@, %/tc\n\t" /* disable mmu */
595 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
596 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
597 "movel #0, %/a0\n\t"
598 "movec %/a0, %/vbr\n\t" /* clear vector base register */
599 "movec %/a0, %/cacr\n\t" /* disable caches */
600 "movel #0x0808,%/a0\n\t"
601 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
602 "movew #0x2700,%/sr\n\t" /* set up status register */
603 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
604 "movec %/a0, %/isp\n\t"
605 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
606 "reset\n\t" /* reset external devices */
607 "jmp %/a0@\n\t" /* jump to the reset vector */
608 ".chip 68k"
609 : : "r" (offset), "a" (rombase) : "a0");
610 }
611
612 /* should never get here */
613 local_irq_enable();
614 printk ("Restart failed. Please restart manually.\n");
615 while(1);
616}
617
618/*
619 * This function translates seconds since 1970 into a proper date.
620 *
621 * Algorithm cribbed from glibc2.1, __offtime().
622 */
623#define SECS_PER_MINUTE (60)
624#define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
625#define SECS_PER_DAY (SECS_PER_HOUR * 24)
626
627static void unmktime(unsigned long time, long offset,
628 int *yearp, int *monp, int *dayp,
629 int *hourp, int *minp, int *secp)
630{
631 /* How many days come before each month (0-12). */
632 static const unsigned short int __mon_yday[2][13] =
633 {
634 /* Normal years. */
635 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
636 /* Leap years. */
637 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
638 };
639 long int days, rem, y, wday, yday;
640 const unsigned short int *ip;
641
642 days = time / SECS_PER_DAY;
643 rem = time % SECS_PER_DAY;
644 rem += offset;
645 while (rem < 0) {
646 rem += SECS_PER_DAY;
647 --days;
648 }
649 while (rem >= SECS_PER_DAY) {
650 rem -= SECS_PER_DAY;
651 ++days;
652 }
653 *hourp = rem / SECS_PER_HOUR;
654 rem %= SECS_PER_HOUR;
655 *minp = rem / SECS_PER_MINUTE;
656 *secp = rem % SECS_PER_MINUTE;
657 /* January 1, 1970 was a Thursday. */
658 wday = (4 + days) % 7; /* Day in the week. Not currently used */
659 if (wday < 0) wday += 7;
660 y = 1970;
661
662#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
663#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
664#define __isleap(year) \
665 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
666
667 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
668 {
669 /* Guess a corrected year, assuming 365 days per year. */
670 long int yg = y + days / 365 - (days % 365 < 0);
671
672 /* Adjust DAYS and Y to match the guessed year. */
673 days -= ((yg - y) * 365
674 + LEAPS_THRU_END_OF (yg - 1)
675 - LEAPS_THRU_END_OF (y - 1));
676 y = yg;
677 }
678 *yearp = y - 1900;
679 yday = days; /* day in the year. Not currently used. */
680 ip = __mon_yday[__isleap(y)];
681 for (y = 11; days < (long int) ip[y]; --y)
682 continue;
683 days -= ip[y];
684 *monp = y;
685 *dayp = days + 1; /* day in the month */
686 return;
687}
688
689/*
690 * Read/write the hardware clock.
691 */
692
693int mac_hwclk(int op, struct rtc_time *t)
694{
695 unsigned long now;
696
697 if (!op) { /* read */
Al Viro32722442006-01-12 01:06:13 -0800698 switch (macintosh_config->adb_type) {
699 case MAC_ADB_II:
700 case MAC_ADB_IOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 now = via_read_time();
Al Viro32722442006-01-12 01:06:13 -0800702 break;
703 case MAC_ADB_IISI:
704 now = maciisi_read_time();
705 break;
706 case MAC_ADB_PB1:
707 case MAC_ADB_PB2:
708 now = pmu_read_time();
709 break;
710 case MAC_ADB_CUDA:
711 now = cuda_read_time();
712 break;
713 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 now = 0;
715 }
716
717 t->tm_wday = 0;
718 unmktime(now, 0,
719 &t->tm_year, &t->tm_mon, &t->tm_mday,
720 &t->tm_hour, &t->tm_min, &t->tm_sec);
Finn Thain40f7f9c2008-11-18 20:45:20 +0100721#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 printk("mac_hwclk: read %04d-%02d-%-2d %02d:%02d:%02d\n",
Finn Thain40f7f9c2008-11-18 20:45:20 +0100723 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
724 t->tm_hour, t->tm_min, t->tm_sec);
725#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 } else { /* write */
Finn Thain40f7f9c2008-11-18 20:45:20 +0100727#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 printk("mac_hwclk: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
Finn Thain40f7f9c2008-11-18 20:45:20 +0100729 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
730 t->tm_hour, t->tm_min, t->tm_sec);
731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
734 t->tm_hour, t->tm_min, t->tm_sec);
735
Al Viro32722442006-01-12 01:06:13 -0800736 switch (macintosh_config->adb_type) {
737 case MAC_ADB_II:
738 case MAC_ADB_IOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 via_write_time(now);
Al Viro32722442006-01-12 01:06:13 -0800740 break;
741 case MAC_ADB_CUDA:
742 cuda_write_time(now);
743 break;
744 case MAC_ADB_PB1:
745 case MAC_ADB_PB2:
746 pmu_write_time(now);
747 break;
748 case MAC_ADB_IISI:
749 maciisi_write_time(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752 return 0;
753}
754
755/*
756 * Set minutes/seconds in the hardware clock
757 */
758
759int mac_set_clock_mmss (unsigned long nowtime)
760{
761 struct rtc_time now;
762
763 mac_hwclk(0, &now);
764 now.tm_sec = nowtime % 60;
765 now.tm_min = (nowtime / 60) % 60;
766 mac_hwclk(1, &now);
767
768 return 0;
769}