blob: eb915551de6900d79920f549694d900c04d89ca1 [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>
22#include <asm/system.h>
23#include <asm/segment.h>
24#include <asm/setup.h>
25#include <asm/macintosh.h>
26#include <asm/mac_via.h>
27#include <asm/mac_oss.h>
28
29#define BOOTINFO_COMPAT_1_0
30#include <asm/bootinfo.h>
31#include <asm/machdep.h>
32
33/* Offset between Unix time (1970-based) and Mac time (1904-based) */
34
35#define RTC_OFFSET 2082844800
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static void (*rom_reset)(void);
38
Al Viro32722442006-01-12 01:06:13 -080039#ifdef CONFIG_ADB_CUDA
40static long cuda_read_time(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Al Viro32722442006-01-12 01:06:13 -080042 struct adb_request req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 long time;
44
Al Viro32722442006-01-12 01:06:13 -080045 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
46 return 0;
47 while (!req.complete)
48 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 time = (req.reply[3] << 24) | (req.reply[4] << 16)
51 | (req.reply[5] << 8) | req.reply[6];
52 return time - RTC_OFFSET;
53}
54
Al Viro32722442006-01-12 01:06:13 -080055static void cuda_write_time(long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Al Viro32722442006-01-12 01:06:13 -080057 struct adb_request req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 data += RTC_OFFSET;
Al Viro32722442006-01-12 01:06:13 -080059 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
60 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
61 (data >> 8) & 0xFF, data & 0xFF) < 0)
62 return;
63 while (!req.complete)
64 cuda_poll();
65}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Al Viro32722442006-01-12 01:06:13 -080067static __u8 cuda_read_pram(int offset)
68{
69 struct adb_request req;
70 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
71 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
72 return 0;
73 while (!req.complete)
74 cuda_poll();
75 return req.reply[3];
76}
77
78static void cuda_write_pram(int offset, __u8 data)
79{
80 struct adb_request req;
81 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
82 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
83 return;
84 while (!req.complete)
85 cuda_poll();
86}
87#else
88#define cuda_read_time() 0
89#define cuda_write_time(n)
90#define cuda_read_pram NULL
91#define cuda_write_pram NULL
92#endif
93
Finn Thaind643e2d2010-06-02 15:58:24 +020094#ifdef CONFIG_ADB_PMU68K
Al Viro32722442006-01-12 01:06:13 -080095static long pmu_read_time(void)
96{
97 struct adb_request req;
98 long time;
99
100 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
101 return 0;
102 while (!req.complete)
103 pmu_poll();
104
Finn Thaind643e2d2010-06-02 15:58:24 +0200105 time = (req.reply[1] << 24) | (req.reply[2] << 16)
106 | (req.reply[3] << 8) | req.reply[4];
Al Viro32722442006-01-12 01:06:13 -0800107 return time - RTC_OFFSET;
108}
109
110static void pmu_write_time(long data)
111{
112 struct adb_request req;
113 data += RTC_OFFSET;
114 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
115 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
116 (data >> 8) & 0xFF, data & 0xFF) < 0)
117 return;
118 while (!req.complete)
119 pmu_poll();
120}
121
122static __u8 pmu_read_pram(int offset)
123{
124 struct adb_request req;
125 if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM,
126 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
127 return 0;
128 while (!req.complete)
129 pmu_poll();
130 return req.reply[3];
131}
132
133static void pmu_write_pram(int offset, __u8 data)
134{
135 struct adb_request req;
136 if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM,
137 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
138 return;
139 while (!req.complete)
140 pmu_poll();
141}
142#else
143#define pmu_read_time() 0
144#define pmu_write_time(n)
145#define pmu_read_pram NULL
146#define pmu_write_pram NULL
147#endif
148
Finn Thain40f7f9c2008-11-18 20:45:20 +0100149#if 0 /* def CONFIG_ADB_MACIISI */
Al Viro32722442006-01-12 01:06:13 -0800150extern int maciisi_request(struct adb_request *req,
151 void (*done)(struct adb_request *), int nbytes, ...);
152
153static long maciisi_read_time(void)
154{
155 struct adb_request req;
156 long time;
157
158 if (maciisi_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME))
159 return 0;
160
161 time = (req.reply[3] << 24) | (req.reply[4] << 16)
162 | (req.reply[5] << 8) | req.reply[6];
163 return time - RTC_OFFSET;
164}
165
166static void maciisi_write_time(long data)
167{
168 struct adb_request req;
169 data += RTC_OFFSET;
170 maciisi_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
172 (data >> 8) & 0xFF, data & 0xFF);
173}
174
Al Viro32722442006-01-12 01:06:13 -0800175static __u8 maciisi_read_pram(int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Al Viro32722442006-01-12 01:06:13 -0800177 struct adb_request req;
178 if (maciisi_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
179 (offset >> 8) & 0xFF, offset & 0xFF))
180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return req.reply[3];
182}
183
Al Viro32722442006-01-12 01:06:13 -0800184static void maciisi_write_pram(int offset, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Al Viro32722442006-01-12 01:06:13 -0800186 struct adb_request req;
187 maciisi_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
188 (offset >> 8) & 0xFF, offset & 0xFF, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
Al Viro32722442006-01-12 01:06:13 -0800190#else
191#define maciisi_read_time() 0
192#define maciisi_write_time(n)
193#define maciisi_read_pram NULL
194#define maciisi_write_pram NULL
195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197/*
198 * VIA PRAM/RTC access routines
199 *
200 * Must be called with interrupts disabled and
201 * the RTC should be enabled.
202 */
203
204static __u8 via_pram_readbyte(void)
205{
206 int i,reg;
207 __u8 data;
208
209 reg = via1[vBufB] & ~VIA1B_vRTCClk;
210
211 /* Set the RTC data line to be an input. */
212
213 via1[vDirB] &= ~VIA1B_vRTCData;
214
215 /* The bits of the byte come out in MSB order */
216
217 data = 0;
218 for (i = 0 ; i < 8 ; i++) {
219 via1[vBufB] = reg;
220 via1[vBufB] = reg | VIA1B_vRTCClk;
221 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
222 }
223
224 /* Return RTC data line to output state */
225
226 via1[vDirB] |= VIA1B_vRTCData;
227
228 return data;
229}
230
231static void via_pram_writebyte(__u8 data)
232{
233 int i,reg,bit;
234
235 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
236
237 /* The bits of the byte go in in MSB order */
238
239 for (i = 0 ; i < 8 ; i++) {
240 bit = data & 0x80? 1 : 0;
241 data <<= 1;
242 via1[vBufB] = reg | bit;
243 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
244 }
245}
246
247/*
248 * Execute a VIA PRAM/RTC command. For read commands
249 * data should point to a one-byte buffer for the
250 * resulting data. For write commands it should point
251 * to the data byte to for the command.
252 *
253 * This function disables all interrupts while running.
254 */
255
256static void via_pram_command(int command, __u8 *data)
257{
258 unsigned long flags;
259 int is_read;
260
261 local_irq_save(flags);
262
263 /* Enable the RTC and make sure the strobe line is high */
264
265 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
266
267 if (command & 0xFF00) { /* extended (two-byte) command */
268 via_pram_writebyte((command & 0xFF00) >> 8);
269 via_pram_writebyte(command & 0xFF);
270 is_read = command & 0x8000;
271 } else { /* one-byte command */
272 via_pram_writebyte(command);
273 is_read = command & 0x80;
274 }
275 if (is_read) {
276 *data = via_pram_readbyte();
277 } else {
278 via_pram_writebyte(*data);
279 }
280
281 /* All done, disable the RTC */
282
283 via1[vBufB] |= VIA1B_vRTCEnb;
284
285 local_irq_restore(flags);
286}
287
288static __u8 via_read_pram(int offset)
289{
290 return 0;
291}
292
293static void via_write_pram(int offset, __u8 data)
294{
295}
296
297/*
298 * Return the current time in seconds since January 1, 1904.
299 *
300 * This only works on machines with the VIA-based PRAM/RTC, which
301 * is basically any machine with Mac II-style ADB.
302 */
303
304static long via_read_time(void)
305{
306 union {
Finn Thain75a23852011-07-18 00:06:10 +1000307 __u8 cdata[4];
308 long idata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 } result, last_result;
Finn Thain75a23852011-07-18 00:06:10 +1000310 int count = 1;
311
312 via_pram_command(0x81, &last_result.cdata[3]);
313 via_pram_command(0x85, &last_result.cdata[2]);
314 via_pram_command(0x89, &last_result.cdata[1]);
315 via_pram_command(0x8D, &last_result.cdata[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /*
318 * The NetBSD guys say to loop until you get the same reading
319 * twice in a row.
320 */
321
Finn Thain75a23852011-07-18 00:06:10 +1000322 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 via_pram_command(0x81, &result.cdata[3]);
324 via_pram_command(0x85, &result.cdata[2]);
325 via_pram_command(0x89, &result.cdata[1]);
326 via_pram_command(0x8D, &result.cdata[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Finn Thain75a23852011-07-18 00:06:10 +1000328 if (result.idata == last_result.idata)
329 return result.idata - RTC_OFFSET;
330
331 if (++count > 10)
332 break;
333
334 last_result.idata = result.idata;
335 }
336
337 pr_err("via_read_time: failed to read a stable value; "
338 "got 0x%08lx then 0x%08lx\n",
339 last_result.idata, result.idata);
340
341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344/*
345 * Set the current time to a number of seconds since January 1, 1904.
346 *
347 * This only works on machines with the VIA-based PRAM/RTC, which
348 * is basically any machine with Mac II-style ADB.
349 */
350
351static void via_write_time(long time)
352{
353 union {
354 __u8 cdata[4];
355 long idata;
356 } data;
357 __u8 temp;
358
359 /* Clear the write protect bit */
360
361 temp = 0x55;
362 via_pram_command(0x35, &temp);
363
364 data.idata = time + RTC_OFFSET;
365 via_pram_command(0x01, &data.cdata[3]);
366 via_pram_command(0x05, &data.cdata[2]);
367 via_pram_command(0x09, &data.cdata[1]);
368 via_pram_command(0x0D, &data.cdata[0]);
369
370 /* Set the write protect bit */
371
372 temp = 0xD5;
373 via_pram_command(0x35, &temp);
374}
375
376static void via_shutdown(void)
377{
378 if (rbv_present) {
379 via2[rBufB] &= ~0x04;
380 } else {
381 /* Direction of vDirB is output */
382 via2[vDirB] |= 0x04;
383 /* Send a value of 0 on that line */
384 via2[vBufB] &= ~0x04;
385 mdelay(1000);
386 }
387}
388
389/*
390 * FIXME: not sure how this is supposed to work exactly...
391 */
392
393static void oss_shutdown(void)
394{
395 oss->rom_ctrl = OSS_POWEROFF;
396}
397
398#ifdef CONFIG_ADB_CUDA
399
400static void cuda_restart(void)
401{
Al Viro32722442006-01-12 01:06:13 -0800402 struct adb_request req;
403 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
404 return;
405 while (!req.complete)
406 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409static void cuda_shutdown(void)
410{
Al Viro32722442006-01-12 01:06:13 -0800411 struct adb_request req;
412 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
413 return;
414 while (!req.complete)
415 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
418#endif /* CONFIG_ADB_CUDA */
419
Al Viro32722442006-01-12 01:06:13 -0800420#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422void pmu_restart(void)
423{
Al Viro32722442006-01-12 01:06:13 -0800424 struct adb_request req;
425 if (pmu_request(&req, NULL,
426 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
427 return;
428 while (!req.complete)
429 pmu_poll();
430 if (pmu_request(&req, NULL, 1, PMU_RESET) < 0)
431 return;
432 while (!req.complete)
433 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436void pmu_shutdown(void)
437{
Al Viro32722442006-01-12 01:06:13 -0800438 struct adb_request req;
439 if (pmu_request(&req, NULL,
440 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
441 return;
442 while (!req.complete)
443 pmu_poll();
444 if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0)
445 return;
446 while (!req.complete)
447 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Al Viro32722442006-01-12 01:06:13 -0800450#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452/*
453 *-------------------------------------------------------------------
454 * Below this point are the generic routines; they'll dispatch to the
455 * correct routine for the hardware on which we're running.
456 *-------------------------------------------------------------------
457 */
458
459void mac_pram_read(int offset, __u8 *buffer, int len)
460{
Al Viro32722442006-01-12 01:06:13 -0800461 __u8 (*func)(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int i;
463
Al Viro32722442006-01-12 01:06:13 -0800464 switch(macintosh_config->adb_type) {
465 case MAC_ADB_IISI:
466 func = maciisi_read_pram; break;
467 case MAC_ADB_PB1:
468 case MAC_ADB_PB2:
469 func = pmu_read_pram; break;
470 case MAC_ADB_CUDA:
471 func = cuda_read_pram; break;
472 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 func = via_read_pram;
474 }
Al Viro32722442006-01-12 01:06:13 -0800475 if (!func)
476 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 for (i = 0 ; i < len ; i++) {
478 buffer[i] = (*func)(offset++);
479 }
480}
481
482void mac_pram_write(int offset, __u8 *buffer, int len)
483{
Al Viro32722442006-01-12 01:06:13 -0800484 void (*func)(int, __u8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int i;
486
Al Viro32722442006-01-12 01:06:13 -0800487 switch(macintosh_config->adb_type) {
488 case MAC_ADB_IISI:
489 func = maciisi_write_pram; break;
490 case MAC_ADB_PB1:
491 case MAC_ADB_PB2:
492 func = pmu_write_pram; break;
493 case MAC_ADB_CUDA:
494 func = cuda_write_pram; break;
495 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 func = via_write_pram;
497 }
Al Viro32722442006-01-12 01:06:13 -0800498 if (!func)
499 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 for (i = 0 ; i < len ; i++) {
501 (*func)(offset++, buffer[i]);
502 }
503}
504
505void mac_poweroff(void)
506{
507 /*
508 * MAC_ADB_IISI may need to be moved up here if it doesn't actually
509 * work using the ADB packet method. --David Kilzer
510 */
511
512 if (oss_present) {
513 oss_shutdown();
514 } else if (macintosh_config->adb_type == MAC_ADB_II) {
515 via_shutdown();
516#ifdef CONFIG_ADB_CUDA
517 } else if (macintosh_config->adb_type == MAC_ADB_CUDA) {
518 cuda_shutdown();
519#endif
Al Viro32722442006-01-12 01:06:13 -0800520#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 } else if (macintosh_config->adb_type == MAC_ADB_PB1
522 || macintosh_config->adb_type == MAC_ADB_PB2) {
523 pmu_shutdown();
524#endif
525 }
526 local_irq_enable();
527 printk("It is now safe to turn off your Macintosh.\n");
528 while(1);
529}
530
531void mac_reset(void)
532{
533 if (macintosh_config->adb_type == MAC_ADB_II) {
534 unsigned long flags;
535
536 /* need ROMBASE in booter */
537 /* indeed, plus need to MAP THE ROM !! */
538
539 if (mac_bi_data.rombase == 0)
540 mac_bi_data.rombase = 0x40800000;
541
542 /* works on some */
543 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
544
545 if (macintosh_config->ident == MAC_MODEL_SE30) {
546 /*
547 * MSch: Machines known to crash on ROM reset ...
548 */
549 } else {
550 local_irq_save(flags);
551
552 rom_reset();
553
554 local_irq_restore(flags);
555 }
556#ifdef CONFIG_ADB_CUDA
557 } else if (macintosh_config->adb_type == MAC_ADB_CUDA) {
558 cuda_restart();
559#endif
Al Viro32722442006-01-12 01:06:13 -0800560#ifdef CONFIG_ADB_PMU68K
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 } else if (macintosh_config->adb_type == MAC_ADB_PB1
562 || macintosh_config->adb_type == MAC_ADB_PB2) {
563 pmu_restart();
564#endif
565 } else if (CPU_IS_030) {
566
567 /* 030-specific reset routine. The idea is general, but the
568 * specific registers to reset are '030-specific. Until I
569 * have a non-030 machine, I can't test anything else.
570 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
571 */
572
573 unsigned long rombase = 0x40000000;
574
575 /* make a 1-to-1 mapping, using the transparent tran. reg. */
576 unsigned long virt = (unsigned long) mac_reset;
577 unsigned long phys = virt_to_phys(mac_reset);
Al Viro77add9f2006-01-12 01:06:18 -0800578 unsigned long addr = (phys&0xFF000000)|0x8777;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 unsigned long offset = phys-virt;
580 local_irq_disable(); /* lets not screw this up, ok? */
581 __asm__ __volatile__(".chip 68030\n\t"
582 "pmove %0,%/tt0\n\t"
583 ".chip 68k"
Al Viro77add9f2006-01-12 01:06:18 -0800584 : : "m" (addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Now jump to physical address so we can disable MMU */
586 __asm__ __volatile__(
587 ".chip 68030\n\t"
588 "lea %/pc@(1f),%/a0\n\t"
589 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
590 "addl %0,%/sp\n\t"
591 "pflusha\n\t"
592 "jmp %/a0@\n\t" /* jump into physical memory */
593 "0:.long 0\n\t" /* a constant zero. */
594 /* OK. Now reset everything and jump to reset vector. */
595 "1:\n\t"
596 "lea %/pc@(0b),%/a0\n\t"
597 "pmove %/a0@, %/tc\n\t" /* disable mmu */
598 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
599 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
600 "movel #0, %/a0\n\t"
601 "movec %/a0, %/vbr\n\t" /* clear vector base register */
602 "movec %/a0, %/cacr\n\t" /* disable caches */
603 "movel #0x0808,%/a0\n\t"
604 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
605 "movew #0x2700,%/sr\n\t" /* set up status register */
606 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
607 "movec %/a0, %/isp\n\t"
608 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
609 "reset\n\t" /* reset external devices */
610 "jmp %/a0@\n\t" /* jump to the reset vector */
611 ".chip 68k"
612 : : "r" (offset), "a" (rombase) : "a0");
613 }
614
615 /* should never get here */
616 local_irq_enable();
617 printk ("Restart failed. Please restart manually.\n");
618 while(1);
619}
620
621/*
622 * This function translates seconds since 1970 into a proper date.
623 *
624 * Algorithm cribbed from glibc2.1, __offtime().
625 */
626#define SECS_PER_MINUTE (60)
627#define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
628#define SECS_PER_DAY (SECS_PER_HOUR * 24)
629
630static void unmktime(unsigned long time, long offset,
631 int *yearp, int *monp, int *dayp,
632 int *hourp, int *minp, int *secp)
633{
634 /* How many days come before each month (0-12). */
635 static const unsigned short int __mon_yday[2][13] =
636 {
637 /* Normal years. */
638 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
639 /* Leap years. */
640 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
641 };
642 long int days, rem, y, wday, yday;
643 const unsigned short int *ip;
644
645 days = time / SECS_PER_DAY;
646 rem = time % SECS_PER_DAY;
647 rem += offset;
648 while (rem < 0) {
649 rem += SECS_PER_DAY;
650 --days;
651 }
652 while (rem >= SECS_PER_DAY) {
653 rem -= SECS_PER_DAY;
654 ++days;
655 }
656 *hourp = rem / SECS_PER_HOUR;
657 rem %= SECS_PER_HOUR;
658 *minp = rem / SECS_PER_MINUTE;
659 *secp = rem % SECS_PER_MINUTE;
660 /* January 1, 1970 was a Thursday. */
661 wday = (4 + days) % 7; /* Day in the week. Not currently used */
662 if (wday < 0) wday += 7;
663 y = 1970;
664
665#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
666#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
667#define __isleap(year) \
668 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
669
670 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
671 {
672 /* Guess a corrected year, assuming 365 days per year. */
673 long int yg = y + days / 365 - (days % 365 < 0);
674
675 /* Adjust DAYS and Y to match the guessed year. */
676 days -= ((yg - y) * 365
677 + LEAPS_THRU_END_OF (yg - 1)
678 - LEAPS_THRU_END_OF (y - 1));
679 y = yg;
680 }
681 *yearp = y - 1900;
682 yday = days; /* day in the year. Not currently used. */
683 ip = __mon_yday[__isleap(y)];
684 for (y = 11; days < (long int) ip[y]; --y)
685 continue;
686 days -= ip[y];
687 *monp = y;
688 *dayp = days + 1; /* day in the month */
689 return;
690}
691
692/*
693 * Read/write the hardware clock.
694 */
695
696int mac_hwclk(int op, struct rtc_time *t)
697{
698 unsigned long now;
699
700 if (!op) { /* read */
Al Viro32722442006-01-12 01:06:13 -0800701 switch (macintosh_config->adb_type) {
702 case MAC_ADB_II:
703 case MAC_ADB_IOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 now = via_read_time();
Al Viro32722442006-01-12 01:06:13 -0800705 break;
706 case MAC_ADB_IISI:
707 now = maciisi_read_time();
708 break;
709 case MAC_ADB_PB1:
710 case MAC_ADB_PB2:
711 now = pmu_read_time();
712 break;
713 case MAC_ADB_CUDA:
714 now = cuda_read_time();
715 break;
716 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 now = 0;
718 }
719
720 t->tm_wday = 0;
721 unmktime(now, 0,
722 &t->tm_year, &t->tm_mon, &t->tm_mday,
723 &t->tm_hour, &t->tm_min, &t->tm_sec);
Finn Thain40f7f9c2008-11-18 20:45:20 +0100724#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 printk("mac_hwclk: read %04d-%02d-%-2d %02d:%02d:%02d\n",
Finn Thain40f7f9c2008-11-18 20:45:20 +0100726 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
727 t->tm_hour, t->tm_min, t->tm_sec);
728#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 } else { /* write */
Finn Thain40f7f9c2008-11-18 20:45:20 +0100730#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 printk("mac_hwclk: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
Finn Thain40f7f9c2008-11-18 20:45:20 +0100732 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
733 t->tm_hour, t->tm_min, t->tm_sec);
734#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
737 t->tm_hour, t->tm_min, t->tm_sec);
738
Al Viro32722442006-01-12 01:06:13 -0800739 switch (macintosh_config->adb_type) {
740 case MAC_ADB_II:
741 case MAC_ADB_IOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 via_write_time(now);
Al Viro32722442006-01-12 01:06:13 -0800743 break;
744 case MAC_ADB_CUDA:
745 cuda_write_time(now);
746 break;
747 case MAC_ADB_PB1:
748 case MAC_ADB_PB2:
749 pmu_write_time(now);
750 break;
751 case MAC_ADB_IISI:
752 maciisi_write_time(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755 return 0;
756}
757
758/*
759 * Set minutes/seconds in the hardware clock
760 */
761
762int mac_set_clock_mmss (unsigned long nowtime)
763{
764 struct rtc_time now;
765
766 mac_hwclk(0, &now);
767 now.tm_sec = nowtime % 60;
768 now.tm_min = (nowtime / 60) % 60;
769 mac_hwclk(1, &now);
770
771 return 0;
772}