]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/m68k/mac/misc.c
Merge tag 'hwmon-for-v5.15-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-jammy-kernel.git] / arch / m68k / mac / misc.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * Miscellaneous Mac68K-specific stuff
4 */
5
1da177e4
LT
6#include <linux/types.h>
7#include <linux/errno.h>
1da177e4
LT
8#include <linux/kernel.h>
9#include <linux/delay.h>
10#include <linux/sched.h>
1da177e4
LT
11#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
7c0f6ba6 19#include <linux/uaccess.h>
1da177e4 20#include <asm/io.h>
1da177e4
LT
21#include <asm/setup.h>
22#include <asm/macintosh.h>
23#include <asm/mac_via.h>
24#include <asm/mac_oss.h>
25
1da177e4
LT
26#include <asm/machdep.h>
27
5b9bfb8e
AB
28/*
29 * Offset between Unix time (1970-based) and Mac time (1904-based). Cuda and PMU
30 * times wrap in 2040. If we need to handle later times, the read_time functions
31 * need to be changed to interpret wrapped times as post-2040.
32 */
1da177e4
LT
33
34#define RTC_OFFSET 2082844800
35
1da177e4
LT
36static void (*rom_reset)(void);
37
d3b41b6b 38#if IS_ENABLED(CONFIG_NVRAM)
3272244c 39#ifdef CONFIG_ADB_CUDA
cda67df5 40static unsigned char cuda_pram_read_byte(int offset)
1da177e4 41{
3272244c 42 struct adb_request req;
31b1c780 43
3272244c 44 if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
31b1c780 45 (offset >> 8) & 0xFF, offset & 0xFF) < 0)
3272244c
AV
46 return 0;
47 while (!req.complete)
48 cuda_poll();
49 return req.reply[3];
50}
1da177e4 51
cda67df5 52static void cuda_pram_write_byte(unsigned char data, int offset)
3272244c
AV
53{
54 struct adb_request req;
31b1c780 55
3272244c 56 if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
31b1c780 57 (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
3272244c
AV
58 return;
59 while (!req.complete)
60 cuda_poll();
61}
6df2afba 62#endif /* CONFIG_ADB_CUDA */
3272244c 63
ebd72227 64#ifdef CONFIG_ADB_PMU
cda67df5 65static unsigned char pmu_pram_read_byte(int offset)
3272244c
AV
66{
67 struct adb_request req;
31b1c780 68
aefcb746
FT
69 if (pmu_request(&req, NULL, 3, PMU_READ_XPRAM,
70 offset & 0xFF, 1) < 0)
3272244c 71 return 0;
aefcb746
FT
72 pmu_wait_complete(&req);
73
74 return req.reply[0];
3272244c 75}
1da177e4 76
cda67df5 77static void pmu_pram_write_byte(unsigned char data, int offset)
1da177e4 78{
3272244c 79 struct adb_request req;
31b1c780 80
aefcb746
FT
81 if (pmu_request(&req, NULL, 4, PMU_WRITE_XPRAM,
82 offset & 0xFF, 1, data) < 0)
3272244c 83 return;
aefcb746 84 pmu_wait_complete(&req);
3272244c 85}
ebd72227 86#endif /* CONFIG_ADB_PMU */
d3b41b6b 87#endif /* CONFIG_NVRAM */
1da177e4 88
1da177e4
LT
89/*
90 * VIA PRAM/RTC access routines
91 *
92 * Must be called with interrupts disabled and
93 * the RTC should be enabled.
94 */
95
cda67df5 96static __u8 via_rtc_recv(void)
1da177e4 97{
31b1c780
FT
98 int i, reg;
99 __u8 data;
1da177e4
LT
100
101 reg = via1[vBufB] & ~VIA1B_vRTCClk;
102
103 /* Set the RTC data line to be an input. */
104
105 via1[vDirB] &= ~VIA1B_vRTCData;
106
107 /* The bits of the byte come out in MSB order */
108
109 data = 0;
110 for (i = 0 ; i < 8 ; i++) {
111 via1[vBufB] = reg;
112 via1[vBufB] = reg | VIA1B_vRTCClk;
113 data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
114 }
115
116 /* Return RTC data line to output state */
117
118 via1[vDirB] |= VIA1B_vRTCData;
119
120 return data;
121}
122
cda67df5 123static void via_rtc_send(__u8 data)
1da177e4 124{
31b1c780 125 int i, reg, bit;
1da177e4
LT
126
127 reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
128
129 /* The bits of the byte go in in MSB order */
130
131 for (i = 0 ; i < 8 ; i++) {
132 bit = data & 0x80? 1 : 0;
133 data <<= 1;
134 via1[vBufB] = reg | bit;
135 via1[vBufB] = reg | bit | VIA1B_vRTCClk;
136 }
137}
138
a71fa0e3
FT
139/*
140 * These values can be found in Inside Macintosh vol. III ch. 2
141 * which has a description of the RTC chip in the original Mac.
142 */
143
144#define RTC_FLG_READ BIT(7)
145#define RTC_FLG_WRITE_PROTECT BIT(7)
146#define RTC_CMD_READ(r) (RTC_FLG_READ | (r << 2))
147#define RTC_CMD_WRITE(r) (r << 2)
148#define RTC_REG_SECONDS_0 0
149#define RTC_REG_SECONDS_1 1
150#define RTC_REG_SECONDS_2 2
151#define RTC_REG_SECONDS_3 3
152#define RTC_REG_WRITE_PROTECT 13
153
aefcb746
FT
154/*
155 * Inside Mac has no information about two-byte RTC commands but
156 * the MAME/MESS source code has the essentials.
157 */
158
159#define RTC_REG_XPRAM 14
160#define RTC_CMD_XPRAM_READ (RTC_CMD_READ(RTC_REG_XPRAM) << 8)
161#define RTC_CMD_XPRAM_WRITE (RTC_CMD_WRITE(RTC_REG_XPRAM) << 8)
162#define RTC_CMD_XPRAM_ARG(a) (((a & 0xE0) << 3) | ((a & 0x1F) << 2))
163
1da177e4
LT
164/*
165 * Execute a VIA PRAM/RTC command. For read commands
166 * data should point to a one-byte buffer for the
167 * resulting data. For write commands it should point
168 * to the data byte to for the command.
169 *
170 * This function disables all interrupts while running.
171 */
172
a71fa0e3 173static void via_rtc_command(int command, __u8 *data)
1da177e4
LT
174{
175 unsigned long flags;
31b1c780 176 int is_read;
1da177e4
LT
177
178 local_irq_save(flags);
179
a71fa0e3
FT
180 /* The least significant bits must be 0b01 according to Inside Mac */
181
182 command = (command & ~3) | 1;
183
1da177e4
LT
184 /* Enable the RTC and make sure the strobe line is high */
185
186 via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
187
188 if (command & 0xFF00) { /* extended (two-byte) command */
cda67df5
FT
189 via_rtc_send((command & 0xFF00) >> 8);
190 via_rtc_send(command & 0xFF);
a71fa0e3 191 is_read = command & (RTC_FLG_READ << 8);
1da177e4 192 } else { /* one-byte command */
cda67df5 193 via_rtc_send(command);
a71fa0e3 194 is_read = command & RTC_FLG_READ;
1da177e4
LT
195 }
196 if (is_read) {
cda67df5 197 *data = via_rtc_recv();
1da177e4 198 } else {
cda67df5 199 via_rtc_send(*data);
1da177e4
LT
200 }
201
202 /* All done, disable the RTC */
203
204 via1[vBufB] |= VIA1B_vRTCEnb;
205
206 local_irq_restore(flags);
207}
208
d3b41b6b 209#if IS_ENABLED(CONFIG_NVRAM)
cda67df5 210static unsigned char via_pram_read_byte(int offset)
1da177e4 211{
aefcb746
FT
212 unsigned char temp;
213
214 via_rtc_command(RTC_CMD_XPRAM_READ | RTC_CMD_XPRAM_ARG(offset), &temp);
215
216 return temp;
1da177e4
LT
217}
218
cda67df5 219static void via_pram_write_byte(unsigned char data, int offset)
1da177e4 220{
aefcb746
FT
221 unsigned char temp;
222
223 temp = 0x55;
224 via_rtc_command(RTC_CMD_WRITE(RTC_REG_WRITE_PROTECT), &temp);
225
226 temp = data;
227 via_rtc_command(RTC_CMD_XPRAM_WRITE | RTC_CMD_XPRAM_ARG(offset), &temp);
228
229 temp = 0x55 | RTC_FLG_WRITE_PROTECT;
230 via_rtc_command(RTC_CMD_WRITE(RTC_REG_WRITE_PROTECT), &temp);
1da177e4 231}
d3b41b6b 232#endif /* CONFIG_NVRAM */
1da177e4
LT
233
234/*
235 * Return the current time in seconds since January 1, 1904.
236 *
237 * This only works on machines with the VIA-based PRAM/RTC, which
238 * is basically any machine with Mac II-style ADB.
239 */
240
5b9bfb8e 241static time64_t via_read_time(void)
1da177e4
LT
242{
243 union {
75a23850 244 __u8 cdata[4];
5b9bfb8e 245 __u32 idata;
1da177e4 246 } result, last_result;
75a23850
FT
247 int count = 1;
248
a71fa0e3
FT
249 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_0), &last_result.cdata[3]);
250 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_1), &last_result.cdata[2]);
251 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_2), &last_result.cdata[1]);
252 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_3), &last_result.cdata[0]);
1da177e4
LT
253
254 /*
255 * The NetBSD guys say to loop until you get the same reading
256 * twice in a row.
257 */
258
75a23850 259 while (1) {
a71fa0e3
FT
260 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_0),
261 &result.cdata[3]);
262 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_1),
263 &result.cdata[2]);
264 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_2),
265 &result.cdata[1]);
266 via_rtc_command(RTC_CMD_READ(RTC_REG_SECONDS_3),
267 &result.cdata[0]);
1da177e4 268
75a23850 269 if (result.idata == last_result.idata)
5b9bfb8e 270 return (time64_t)result.idata - RTC_OFFSET;
75a23850
FT
271
272 if (++count > 10)
273 break;
274
275 last_result.idata = result.idata;
276 }
277
5b9bfb8e
AB
278 pr_err("%s: failed to read a stable value; got 0x%08x then 0x%08x\n",
279 __func__, last_result.idata, result.idata);
75a23850
FT
280
281 return 0;
1da177e4
LT
282}
283
284/*
285 * Set the current time to a number of seconds since January 1, 1904.
286 *
287 * This only works on machines with the VIA-based PRAM/RTC, which
288 * is basically any machine with Mac II-style ADB.
289 */
290
0792a2c8 291static void via_set_rtc_time(struct rtc_time *tm)
1da177e4
LT
292{
293 union {
31b1c780 294 __u8 cdata[4];
5b9bfb8e 295 __u32 idata;
1da177e4 296 } data;
31b1c780 297 __u8 temp;
0792a2c8
FT
298 time64_t time;
299
300 time = mktime64(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
301 tm->tm_hour, tm->tm_min, tm->tm_sec);
1da177e4
LT
302
303 /* Clear the write protect bit */
304
305 temp = 0x55;
a71fa0e3 306 via_rtc_command(RTC_CMD_WRITE(RTC_REG_WRITE_PROTECT), &temp);
1da177e4 307
5b9bfb8e 308 data.idata = lower_32_bits(time + RTC_OFFSET);
a71fa0e3
FT
309 via_rtc_command(RTC_CMD_WRITE(RTC_REG_SECONDS_0), &data.cdata[3]);
310 via_rtc_command(RTC_CMD_WRITE(RTC_REG_SECONDS_1), &data.cdata[2]);
311 via_rtc_command(RTC_CMD_WRITE(RTC_REG_SECONDS_2), &data.cdata[1]);
312 via_rtc_command(RTC_CMD_WRITE(RTC_REG_SECONDS_3), &data.cdata[0]);
1da177e4
LT
313
314 /* Set the write protect bit */
315
a71fa0e3
FT
316 temp = 0x55 | RTC_FLG_WRITE_PROTECT;
317 via_rtc_command(RTC_CMD_WRITE(RTC_REG_WRITE_PROTECT), &temp);
1da177e4
LT
318}
319
320static void via_shutdown(void)
321{
322 if (rbv_present) {
323 via2[rBufB] &= ~0x04;
324 } else {
325 /* Direction of vDirB is output */
326 via2[vDirB] |= 0x04;
327 /* Send a value of 0 on that line */
328 via2[vBufB] &= ~0x04;
329 mdelay(1000);
330 }
331}
332
1da177e4
LT
333static void oss_shutdown(void)
334{
335 oss->rom_ctrl = OSS_POWEROFF;
336}
337
338#ifdef CONFIG_ADB_CUDA
1da177e4
LT
339static void cuda_restart(void)
340{
3272244c 341 struct adb_request req;
31b1c780 342
3272244c
AV
343 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
344 return;
345 while (!req.complete)
346 cuda_poll();
1da177e4
LT
347}
348
349static void cuda_shutdown(void)
350{
3272244c 351 struct adb_request req;
31b1c780 352
3272244c
AV
353 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
354 return;
41e93a30
FT
355
356 /* Avoid infinite polling loop when PSU is not under Cuda control */
357 switch (macintosh_config->ident) {
358 case MAC_MODEL_C660:
359 case MAC_MODEL_Q605:
360 case MAC_MODEL_Q605_ACC:
361 case MAC_MODEL_P475:
362 case MAC_MODEL_P475F:
363 return;
364 }
365
3272244c
AV
366 while (!req.complete)
367 cuda_poll();
1da177e4 368}
1da177e4
LT
369#endif /* CONFIG_ADB_CUDA */
370
1da177e4
LT
371/*
372 *-------------------------------------------------------------------
373 * Below this point are the generic routines; they'll dispatch to the
374 * correct routine for the hardware on which we're running.
375 *-------------------------------------------------------------------
376 */
377
d3b41b6b 378#if IS_ENABLED(CONFIG_NVRAM)
cda67df5 379unsigned char mac_pram_read_byte(int addr)
1da177e4 380{
31b1c780 381 switch (macintosh_config->adb_type) {
6df2afba
FT
382 case MAC_ADB_IOP:
383 case MAC_ADB_II:
3272244c 384 case MAC_ADB_PB1:
cda67df5 385 return via_pram_read_byte(addr);
6df2afba 386#ifdef CONFIG_ADB_CUDA
f74faec6 387 case MAC_ADB_EGRET:
3272244c 388 case MAC_ADB_CUDA:
cda67df5 389 return cuda_pram_read_byte(addr);
6df2afba 390#endif
ebd72227 391#ifdef CONFIG_ADB_PMU
6df2afba 392 case MAC_ADB_PB2:
cda67df5 393 return pmu_pram_read_byte(addr);
6df2afba 394#endif
3272244c 395 default:
cda67df5 396 return 0xFF;
1da177e4
LT
397 }
398}
399
cda67df5 400void mac_pram_write_byte(unsigned char val, int addr)
1da177e4 401{
31b1c780 402 switch (macintosh_config->adb_type) {
6df2afba
FT
403 case MAC_ADB_IOP:
404 case MAC_ADB_II:
3272244c 405 case MAC_ADB_PB1:
cda67df5 406 via_pram_write_byte(val, addr);
31b1c780 407 break;
6df2afba 408#ifdef CONFIG_ADB_CUDA
f74faec6 409 case MAC_ADB_EGRET:
3272244c 410 case MAC_ADB_CUDA:
cda67df5 411 cuda_pram_write_byte(val, addr);
31b1c780 412 break;
6df2afba 413#endif
ebd72227 414#ifdef CONFIG_ADB_PMU
6df2afba 415 case MAC_ADB_PB2:
cda67df5 416 pmu_pram_write_byte(val, addr);
6df2afba
FT
417 break;
418#endif
3272244c 419 default:
cda67df5 420 break;
1da177e4
LT
421 }
422}
423
d3b41b6b
FT
424ssize_t mac_pram_get_size(void)
425{
426 return 256;
427}
428#endif /* CONFIG_NVRAM */
429
1da177e4
LT
430void mac_poweroff(void)
431{
1da177e4
LT
432 if (oss_present) {
433 oss_shutdown();
434 } else if (macintosh_config->adb_type == MAC_ADB_II) {
435 via_shutdown();
436#ifdef CONFIG_ADB_CUDA
f74faec6
FT
437 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
438 macintosh_config->adb_type == MAC_ADB_CUDA) {
1da177e4
LT
439 cuda_shutdown();
440#endif
ebd72227 441#ifdef CONFIG_ADB_PMU
54c99077 442 } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
1da177e4
LT
443 pmu_shutdown();
444#endif
445 }
558d5ad2 446
889121b4 447 pr_crit("It is now safe to turn off your Macintosh.\n");
558d5ad2 448 local_irq_disable();
1da177e4
LT
449 while(1);
450}
451
452void mac_reset(void)
453{
9c0e91f6
FT
454 if (macintosh_config->adb_type == MAC_ADB_II &&
455 macintosh_config->ident != MAC_MODEL_SE30) {
1da177e4
LT
456 /* need ROMBASE in booter */
457 /* indeed, plus need to MAP THE ROM !! */
458
459 if (mac_bi_data.rombase == 0)
460 mac_bi_data.rombase = 0x40800000;
461
462 /* works on some */
463 rom_reset = (void *) (mac_bi_data.rombase + 0xa);
464
9c0e91f6
FT
465 local_irq_disable();
466 rom_reset();
1da177e4 467#ifdef CONFIG_ADB_CUDA
f74faec6
FT
468 } else if (macintosh_config->adb_type == MAC_ADB_EGRET ||
469 macintosh_config->adb_type == MAC_ADB_CUDA) {
1da177e4
LT
470 cuda_restart();
471#endif
ebd72227 472#ifdef CONFIG_ADB_PMU
54c99077 473 } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
1da177e4
LT
474 pmu_restart();
475#endif
476 } else if (CPU_IS_030) {
477
478 /* 030-specific reset routine. The idea is general, but the
479 * specific registers to reset are '030-specific. Until I
480 * have a non-030 machine, I can't test anything else.
481 * -- C. Scott Ananian <cananian@alumni.princeton.edu>
482 */
483
484 unsigned long rombase = 0x40000000;
485
486 /* make a 1-to-1 mapping, using the transparent tran. reg. */
487 unsigned long virt = (unsigned long) mac_reset;
488 unsigned long phys = virt_to_phys(mac_reset);
77add9f3 489 unsigned long addr = (phys&0xFF000000)|0x8777;
1da177e4 490 unsigned long offset = phys-virt;
31b1c780 491
1da177e4
LT
492 local_irq_disable(); /* lets not screw this up, ok? */
493 __asm__ __volatile__(".chip 68030\n\t"
494 "pmove %0,%/tt0\n\t"
495 ".chip 68k"
77add9f3 496 : : "m" (addr));
1da177e4
LT
497 /* Now jump to physical address so we can disable MMU */
498 __asm__ __volatile__(
31b1c780 499 ".chip 68030\n\t"
1da177e4
LT
500 "lea %/pc@(1f),%/a0\n\t"
501 "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
502 "addl %0,%/sp\n\t"
503 "pflusha\n\t"
504 "jmp %/a0@\n\t" /* jump into physical memory */
505 "0:.long 0\n\t" /* a constant zero. */
506 /* OK. Now reset everything and jump to reset vector. */
507 "1:\n\t"
508 "lea %/pc@(0b),%/a0\n\t"
509 "pmove %/a0@, %/tc\n\t" /* disable mmu */
510 "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
511 "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
512 "movel #0, %/a0\n\t"
513 "movec %/a0, %/vbr\n\t" /* clear vector base register */
514 "movec %/a0, %/cacr\n\t" /* disable caches */
515 "movel #0x0808,%/a0\n\t"
516 "movec %/a0, %/cacr\n\t" /* flush i&d caches */
517 "movew #0x2700,%/sr\n\t" /* set up status register */
518 "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
519 "movec %/a0, %/isp\n\t"
520 "movel %1@(0x4),%/a0\n\t" /* load reset vector */
521 "reset\n\t" /* reset external devices */
522 "jmp %/a0@\n\t" /* jump to the reset vector */
523 ".chip 68k"
524 : : "r" (offset), "a" (rombase) : "a0");
525 }
526
527 /* should never get here */
889121b4 528 pr_crit("Restart failed. Please restart manually.\n");
558d5ad2 529 local_irq_disable();
1da177e4
LT
530 while(1);
531}
532
533/*
534 * This function translates seconds since 1970 into a proper date.
535 *
536 * Algorithm cribbed from glibc2.1, __offtime().
5b9bfb8e
AB
537 *
538 * This is roughly same as rtc_time64_to_tm(), which we should probably
539 * use here, but it's only available when CONFIG_RTC_LIB is enabled.
1da177e4
LT
540 */
541#define SECS_PER_MINUTE (60)
542#define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
543#define SECS_PER_DAY (SECS_PER_HOUR * 24)
544
5b9bfb8e 545static void unmktime(time64_t time, long offset,
1da177e4
LT
546 int *yearp, int *monp, int *dayp,
547 int *hourp, int *minp, int *secp)
548{
549 /* How many days come before each month (0-12). */
550 static const unsigned short int __mon_yday[2][13] =
551 {
552 /* Normal years. */
553 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
554 /* Leap years. */
555 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
556 };
5b9bfb8e 557 int days, rem, y, wday, yday;
1da177e4
LT
558 const unsigned short int *ip;
559
5b9bfb8e 560 days = div_u64_rem(time, SECS_PER_DAY, &rem);
1da177e4
LT
561 rem += offset;
562 while (rem < 0) {
563 rem += SECS_PER_DAY;
564 --days;
565 }
566 while (rem >= SECS_PER_DAY) {
567 rem -= SECS_PER_DAY;
568 ++days;
569 }
570 *hourp = rem / SECS_PER_HOUR;
571 rem %= SECS_PER_HOUR;
572 *minp = rem / SECS_PER_MINUTE;
573 *secp = rem % SECS_PER_MINUTE;
574 /* January 1, 1970 was a Thursday. */
575 wday = (4 + days) % 7; /* Day in the week. Not currently used */
576 if (wday < 0) wday += 7;
577 y = 1970;
578
579#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
580#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
581#define __isleap(year) \
582 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
583
584 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
585 {
586 /* Guess a corrected year, assuming 365 days per year. */
587 long int yg = y + days / 365 - (days % 365 < 0);
588
589 /* Adjust DAYS and Y to match the guessed year. */
31b1c780
FT
590 days -= (yg - y) * 365 +
591 LEAPS_THRU_END_OF(yg - 1) - LEAPS_THRU_END_OF(y - 1);
1da177e4
LT
592 y = yg;
593 }
594 *yearp = y - 1900;
595 yday = days; /* day in the year. Not currently used. */
596 ip = __mon_yday[__isleap(y)];
597 for (y = 11; days < (long int) ip[y]; --y)
598 continue;
599 days -= ip[y];
600 *monp = y;
601 *dayp = days + 1; /* day in the month */
602 return;
603}
604
605/*
606 * Read/write the hardware clock.
607 */
608
609int mac_hwclk(int op, struct rtc_time *t)
610{
5b9bfb8e 611 time64_t now;
1da177e4
LT
612
613 if (!op) { /* read */
3272244c 614 switch (macintosh_config->adb_type) {
3272244c 615 case MAC_ADB_IOP:
6df2afba 616 case MAC_ADB_II:
3272244c 617 case MAC_ADB_PB1:
6df2afba 618 now = via_read_time();
3272244c 619 break;
6df2afba 620#ifdef CONFIG_ADB_CUDA
f74faec6 621 case MAC_ADB_EGRET:
3272244c 622 case MAC_ADB_CUDA:
0792a2c8 623 now = cuda_get_time();
3272244c 624 break;
6df2afba 625#endif
ebd72227 626#ifdef CONFIG_ADB_PMU
6df2afba 627 case MAC_ADB_PB2:
0792a2c8 628 now = pmu_get_time();
6df2afba
FT
629 break;
630#endif
3272244c 631 default:
1da177e4
LT
632 now = 0;
633 }
634
635 t->tm_wday = 0;
636 unmktime(now, 0,
637 &t->tm_year, &t->tm_mon, &t->tm_mday,
638 &t->tm_hour, &t->tm_min, &t->tm_sec);
90625444 639 pr_debug("%s: read %ptR\n", __func__, t);
1da177e4 640 } else { /* write */
90625444 641 pr_debug("%s: tried to write %ptR\n", __func__, t);
1da177e4 642
3272244c 643 switch (macintosh_config->adb_type) {
3272244c 644 case MAC_ADB_IOP:
6df2afba
FT
645 case MAC_ADB_II:
646 case MAC_ADB_PB1:
0792a2c8 647 via_set_rtc_time(t);
3272244c 648 break;
6df2afba 649#ifdef CONFIG_ADB_CUDA
f74faec6 650 case MAC_ADB_EGRET:
3272244c 651 case MAC_ADB_CUDA:
0792a2c8 652 cuda_set_rtc_time(t);
3272244c 653 break;
6df2afba 654#endif
ebd72227 655#ifdef CONFIG_ADB_PMU
3272244c 656 case MAC_ADB_PB2:
0792a2c8 657 pmu_set_rtc_time(t);
3272244c 658 break;
6df2afba
FT
659#endif
660 default:
661 return -ENODEV;
1da177e4 662 }
1da177e4
LT
663 }
664 return 0;
665}