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