blob: 99dd2c1e9f44ab48199e22edb116f66710600f29 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Miscellaneous Mac68K-specific stuff
3 */
4
5#include <linux/config.h>
6#include <linux/types.h>
7#include <linux/errno.h>
8#include <linux/miscdevice.h>
9#include <linux/kernel.h>
10#include <linux/delay.h>
11#include <linux/sched.h>
12#include <linux/slab.h>
13#include <linux/time.h>
14#include <linux/rtc.h>
15#include <linux/mm.h>
16
17#include <linux/adb.h>
18#include <linux/cuda.h>
19#include <linux/pmu.h>
20
21#include <asm/uaccess.h>
22#include <asm/io.h>
23#include <asm/rtc.h>
24#include <asm/system.h>
25#include <asm/segment.h>
26#include <asm/setup.h>
27#include <asm/macintosh.h>
28#include <asm/mac_via.h>
29#include <asm/mac_oss.h>
30
31#define BOOTINFO_COMPAT_1_0
32#include <asm/bootinfo.h>
33#include <asm/machdep.h>
34
35/* Offset between Unix time (1970-based) and Mac time (1904-based) */
36
37#define RTC_OFFSET 2082844800
38
39extern struct mac_booter_data mac_bi_data;
40static void (*rom_reset)(void);
41
Al Viro32722442006-01-12 01:06:13 -080042#ifdef CONFIG_ADB_CUDA
43static long cuda_read_time(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Al Viro32722442006-01-12 01:06:13 -080045 struct adb_request req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 long time;
47
Al Viro32722442006-01-12 01:06:13 -080048 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
49 return 0;
50 while (!req.complete)
51 cuda_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 time = (req.reply[3] << 24) | (req.reply[4] << 16)
54 | (req.reply[5] << 8) | req.reply[6];
55 return time - RTC_OFFSET;
56}
57
Al Viro32722442006-01-12 01:06:13 -080058static void cuda_write_time(long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Al Viro32722442006-01-12 01:06:13 -080060 struct adb_request req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 data += RTC_OFFSET;
Al Viro32722442006-01-12 01:06:13 -080062 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
63 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
64 (data >> 8) & 0xFF, data & 0xFF) < 0)
65 return;
66 while (!req.complete)
67 cuda_poll();
68}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Al Viro32722442006-01-12 01:06:13 -080070static __u8 cuda_read_pram(int offset)
71{
72 struct adb_request req;
73 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
74 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
75 return 0;
76 while (!req.complete)
77 cuda_poll();
78 return req.reply[3];
79}
80
81static void cuda_write_pram(int offset, __u8 data)
82{
83 struct adb_request req;
84 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
85 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
86 return;
87 while (!req.complete)
88 cuda_poll();
89}
90#else
91#define cuda_read_time() 0
92#define cuda_write_time(n)
93#define cuda_read_pram NULL
94#define cuda_write_pram NULL
95#endif
96
97#ifdef CONFIG_ADB_PMU68K
98static long pmu_read_time(void)
99{
100 struct adb_request req;
101 long time;
102
103 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
104 return 0;
105 while (!req.complete)
106 pmu_poll();
107
108 time = (req.reply[0] << 24) | (req.reply[1] << 16)
109 | (req.reply[2] << 8) | req.reply[3];
110 return time - RTC_OFFSET;
111}
112
113static void pmu_write_time(long data)
114{
115 struct adb_request req;
116 data += RTC_OFFSET;
117 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
118 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
119 (data >> 8) & 0xFF, data & 0xFF) < 0)
120 return;
121 while (!req.complete)
122 pmu_poll();
123}
124
125static __u8 pmu_read_pram(int offset)
126{
127 struct adb_request req;
128 if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM,
129 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
130 return 0;
131 while (!req.complete)
132 pmu_poll();
133 return req.reply[3];
134}
135
136static void pmu_write_pram(int offset, __u8 data)
137{
138 struct adb_request req;
139 if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM,
140 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
141 return;
142 while (!req.complete)
143 pmu_poll();
144}
145#else
146#define pmu_read_time() 0
147#define pmu_write_time(n)
148#define pmu_read_pram NULL
149#define pmu_write_pram NULL
150#endif
151
152#ifdef CONFIG_ADB_MACIISI
153extern int maciisi_request(struct adb_request *req,
154 void (*done)(struct adb_request *), int nbytes, ...);
155
156static long maciisi_read_time(void)
157{
158 struct adb_request req;
159 long time;
160
161 if (maciisi_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME))
162 return 0;
163
164 time = (req.reply[3] << 24) | (req.reply[4] << 16)
165 | (req.reply[5] << 8) | req.reply[6];
166 return time - RTC_OFFSET;
167}
168
169static void maciisi_write_time(long data)
170{
171 struct adb_request req;
172 data += RTC_OFFSET;
173 maciisi_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 (data >> 24) & 0xFF, (data >> 16) & 0xFF,
175 (data >> 8) & 0xFF, data & 0xFF);
176}
177
Al Viro32722442006-01-12 01:06:13 -0800178static __u8 maciisi_read_pram(int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Al Viro32722442006-01-12 01:06:13 -0800180 struct adb_request req;
181 if (maciisi_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
182 (offset >> 8) & 0xFF, offset & 0xFF))
183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return req.reply[3];
185}
186
Al Viro32722442006-01-12 01:06:13 -0800187static void maciisi_write_pram(int offset, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Al Viro32722442006-01-12 01:06:13 -0800189 struct adb_request req;
190 maciisi_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
191 (offset >> 8) & 0xFF, offset & 0xFF, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
Al Viro32722442006-01-12 01:06:13 -0800193#else
194#define maciisi_read_time() 0
195#define maciisi_write_time(n)
196#define maciisi_read_pram NULL
197#define maciisi_write_pram NULL
198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/*
201 * VIA PRAM/RTC access routines
202 *
203 * Must be called with interrupts disabled and
204 * the RTC should be enabled.
205 */
206
207static __u8 via_pram_readbyte(void)
208{
209 int i,reg;
210 __u8 data;
211
212 reg = via1[vBufB] & ~VIA1B_vRTCClk;
213
214 /* Set the RTC data line to be an input. */
215
216 via1[vDirB] &= ~VIA1B_vRTCData;
217
218 /* The bits of the byte come out in MSB order */
219
220 data = 0;
221 for (i = 0 ; i < 8 ; i++) {
222 via1[vBufB] = reg;
223 via1[vBufB] = reg | VIA1B_vRTCClk;
224 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
225 }
226
227 /* Return RTC data line to output state */
228
229 via1[vDirB] |= VIA1B_vRTCData;
230
231 return data;
232}
233
234static void via_pram_writebyte(__u8 data)
235{
236 int i,reg,bit;
237
238 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
239
240 /* The bits of the byte go in in MSB order */
241
242 for (i = 0 ; i < 8 ; i++) {
243 bit = data & 0x80? 1 : 0;
244 data <<= 1;
245 via1[vBufB] = reg | bit;
246 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
247 }
248}
249
250/*
251 * Execute a VIA PRAM/RTC command. For read commands
252 * data should point to a one-byte buffer for the
253 * resulting data. For write commands it should point
254 * to the data byte to for the command.
255 *
256 * This function disables all interrupts while running.
257 */
258
259static void via_pram_command(int command, __u8 *data)
260{
261 unsigned long flags;
262 int is_read;
263
264 local_irq_save(flags);
265
266 /* Enable the RTC and make sure the strobe line is high */
267
268 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
269
270 if (command & 0xFF00) { /* extended (two-byte) command */
271 via_pram_writebyte((command & 0xFF00) >> 8);
272 via_pram_writebyte(command & 0xFF);
273 is_read = command & 0x8000;
274 } else { /* one-byte command */
275 via_pram_writebyte(command);
276 is_read = command & 0x80;
277 }
278 if (is_read) {
279 *data = via_pram_readbyte();
280 } else {
281 via_pram_writebyte(*data);
282 }
283
284 /* All done, disable the RTC */
285
286 via1[vBufB] |= VIA1B_vRTCEnb;
287
288 local_irq_restore(flags);
289}
290
291static __u8 via_read_pram(int offset)
292{
293 return 0;
294}
295
296static void via_write_pram(int offset, __u8 data)
297{
298}
299
300/*
301 * Return the current time in seconds since January 1, 1904.
302 *
303 * This only works on machines with the VIA-based PRAM/RTC, which
304 * is basically any machine with Mac II-style ADB.
305 */
306
307static long via_read_time(void)
308{
309 union {
310 __u8 cdata[4];
311 long idata;
312 } result, last_result;
313 int ct;
314
315 /*
316 * The NetBSD guys say to loop until you get the same reading
317 * twice in a row.
318 */
319
320 ct = 0;
321 do {
322 if (++ct > 10) {
323 printk("via_read_time: couldn't get valid time, "
324 "last read = 0x%08lx and 0x%08lx\n",
325 last_result.idata, result.idata);
326 break;
327 }
328
329 last_result.idata = result.idata;
330 result.idata = 0;
331
332 via_pram_command(0x81, &result.cdata[3]);
333 via_pram_command(0x85, &result.cdata[2]);
334 via_pram_command(0x89, &result.cdata[1]);
335 via_pram_command(0x8D, &result.cdata[0]);
336 } while (result.idata != last_result.idata);
337
338 return result.idata - RTC_OFFSET;
339}
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);
575 unsigned long offset = phys-virt;
576 local_irq_disable(); /* lets not screw this up, ok? */
577 __asm__ __volatile__(".chip 68030\n\t"
578 "pmove %0,%/tt0\n\t"
579 ".chip 68k"
580 : : "m" ((phys&0xFF000000)|0x8777));
581 /* Now jump to physical address so we can disable MMU */
582 __asm__ __volatile__(
583 ".chip 68030\n\t"
584 "lea %/pc@(1f),%/a0\n\t"
585 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
586 "addl %0,%/sp\n\t"
587 "pflusha\n\t"
588 "jmp %/a0@\n\t" /* jump into physical memory */
589 "0:.long 0\n\t" /* a constant zero. */
590 /* OK. Now reset everything and jump to reset vector. */
591 "1:\n\t"
592 "lea %/pc@(0b),%/a0\n\t"
593 "pmove %/a0@, %/tc\n\t" /* disable mmu */
594 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
595 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
596 "movel #0, %/a0\n\t"
597 "movec %/a0, %/vbr\n\t" /* clear vector base register */
598 "movec %/a0, %/cacr\n\t" /* disable caches */
599 "movel #0x0808,%/a0\n\t"
600 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
601 "movew #0x2700,%/sr\n\t" /* set up status register */
602 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
603 "movec %/a0, %/isp\n\t"
604 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
605 "reset\n\t" /* reset external devices */
606 "jmp %/a0@\n\t" /* jump to the reset vector */
607 ".chip 68k"
608 : : "r" (offset), "a" (rombase) : "a0");
609 }
610
611 /* should never get here */
612 local_irq_enable();
613 printk ("Restart failed. Please restart manually.\n");
614 while(1);
615}
616
617/*
618 * This function translates seconds since 1970 into a proper date.
619 *
620 * Algorithm cribbed from glibc2.1, __offtime().
621 */
622#define SECS_PER_MINUTE (60)
623#define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
624#define SECS_PER_DAY (SECS_PER_HOUR * 24)
625
626static void unmktime(unsigned long time, long offset,
627 int *yearp, int *monp, int *dayp,
628 int *hourp, int *minp, int *secp)
629{
630 /* How many days come before each month (0-12). */
631 static const unsigned short int __mon_yday[2][13] =
632 {
633 /* Normal years. */
634 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
635 /* Leap years. */
636 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
637 };
638 long int days, rem, y, wday, yday;
639 const unsigned short int *ip;
640
641 days = time / SECS_PER_DAY;
642 rem = time % SECS_PER_DAY;
643 rem += offset;
644 while (rem < 0) {
645 rem += SECS_PER_DAY;
646 --days;
647 }
648 while (rem >= SECS_PER_DAY) {
649 rem -= SECS_PER_DAY;
650 ++days;
651 }
652 *hourp = rem / SECS_PER_HOUR;
653 rem %= SECS_PER_HOUR;
654 *minp = rem / SECS_PER_MINUTE;
655 *secp = rem % SECS_PER_MINUTE;
656 /* January 1, 1970 was a Thursday. */
657 wday = (4 + days) % 7; /* Day in the week. Not currently used */
658 if (wday < 0) wday += 7;
659 y = 1970;
660
661#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
662#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
663#define __isleap(year) \
664 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
665
666 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
667 {
668 /* Guess a corrected year, assuming 365 days per year. */
669 long int yg = y + days / 365 - (days % 365 < 0);
670
671 /* Adjust DAYS and Y to match the guessed year. */
672 days -= ((yg - y) * 365
673 + LEAPS_THRU_END_OF (yg - 1)
674 - LEAPS_THRU_END_OF (y - 1));
675 y = yg;
676 }
677 *yearp = y - 1900;
678 yday = days; /* day in the year. Not currently used. */
679 ip = __mon_yday[__isleap(y)];
680 for (y = 11; days < (long int) ip[y]; --y)
681 continue;
682 days -= ip[y];
683 *monp = y;
684 *dayp = days + 1; /* day in the month */
685 return;
686}
687
688/*
689 * Read/write the hardware clock.
690 */
691
692int mac_hwclk(int op, struct rtc_time *t)
693{
694 unsigned long now;
695
696 if (!op) { /* read */
Al Viro32722442006-01-12 01:06:13 -0800697 switch (macintosh_config->adb_type) {
698 case MAC_ADB_II:
699 case MAC_ADB_IOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 now = via_read_time();
Al Viro32722442006-01-12 01:06:13 -0800701 break;
702 case MAC_ADB_IISI:
703 now = maciisi_read_time();
704 break;
705 case MAC_ADB_PB1:
706 case MAC_ADB_PB2:
707 now = pmu_read_time();
708 break;
709 case MAC_ADB_CUDA:
710 now = cuda_read_time();
711 break;
712 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 now = 0;
714 }
715
716 t->tm_wday = 0;
717 unmktime(now, 0,
718 &t->tm_year, &t->tm_mon, &t->tm_mday,
719 &t->tm_hour, &t->tm_min, &t->tm_sec);
720 printk("mac_hwclk: read %04d-%02d-%-2d %02d:%02d:%02d\n",
721 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
722 } else { /* write */
723 printk("mac_hwclk: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
724 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
725
726#if 0 /* it trashes my rtc */
727 now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
728 t->tm_hour, t->tm_min, t->tm_sec);
729
Al Viro32722442006-01-12 01:06:13 -0800730 switch (macintosh_config->adb_type) {
731 case MAC_ADB_II:
732 case MAC_ADB_IOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 via_write_time(now);
Al Viro32722442006-01-12 01:06:13 -0800734 break;
735 case MAC_ADB_CUDA:
736 cuda_write_time(now);
737 break;
738 case MAC_ADB_PB1:
739 case MAC_ADB_PB2:
740 pmu_write_time(now);
741 break;
742 case MAC_ADB_IISI:
743 maciisi_write_time(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745#endif
746 }
747 return 0;
748}
749
750/*
751 * Set minutes/seconds in the hardware clock
752 */
753
754int mac_set_clock_mmss (unsigned long nowtime)
755{
756 struct rtc_time now;
757
758 mac_hwclk(0, &now);
759 now.tm_sec = nowtime % 60;
760 now.tm_min = (nowtime / 60) % 60;
761 mac_hwclk(1, &now);
762
763 return 0;
764}