Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Miscellaneous Mac68K-specific stuff |
| 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/types.h> |
| 7 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
| 9 | #include <linux/delay.h> |
| 10 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/machdep.h> |
| 28 | |
| 29 | /* Offset between Unix time (1970-based) and Mac time (1904-based) */ |
| 30 | |
| 31 | #define RTC_OFFSET 2082844800 |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | static void (*rom_reset)(void); |
| 34 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 35 | #ifdef CONFIG_ADB_CUDA |
| 36 | static long cuda_read_time(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 38 | struct adb_request req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | long time; |
| 40 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 41 | if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0) |
| 42 | return 0; |
| 43 | while (!req.complete) |
| 44 | cuda_poll(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 46 | time = (req.reply[3] << 24) | (req.reply[4] << 16) | |
| 47 | (req.reply[5] << 8) | req.reply[6]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | return time - RTC_OFFSET; |
| 49 | } |
| 50 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 51 | static void cuda_write_time(long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 53 | struct adb_request req; |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | data += RTC_OFFSET; |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 56 | if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME, |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 57 | (data >> 24) & 0xFF, (data >> 16) & 0xFF, |
| 58 | (data >> 8) & 0xFF, data & 0xFF) < 0) |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 59 | return; |
| 60 | while (!req.complete) |
| 61 | cuda_poll(); |
| 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 64 | static __u8 cuda_read_pram(int offset) |
| 65 | { |
| 66 | struct adb_request req; |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 67 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 68 | if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM, |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 69 | (offset >> 8) & 0xFF, offset & 0xFF) < 0) |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 70 | return 0; |
| 71 | while (!req.complete) |
| 72 | cuda_poll(); |
| 73 | return req.reply[3]; |
| 74 | } |
| 75 | |
| 76 | static void cuda_write_pram(int offset, __u8 data) |
| 77 | { |
| 78 | struct adb_request req; |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 79 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 80 | if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM, |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 81 | (offset >> 8) & 0xFF, offset & 0xFF, data) < 0) |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 82 | return; |
| 83 | while (!req.complete) |
| 84 | cuda_poll(); |
| 85 | } |
| 86 | #else |
| 87 | #define cuda_read_time() 0 |
| 88 | #define cuda_write_time(n) |
| 89 | #define cuda_read_pram NULL |
| 90 | #define cuda_write_pram NULL |
| 91 | #endif |
| 92 | |
Finn Thain | d643e2d | 2010-06-02 15:58:24 +0200 | [diff] [blame] | 93 | #ifdef CONFIG_ADB_PMU68K |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 94 | static long pmu_read_time(void) |
| 95 | { |
| 96 | struct adb_request req; |
| 97 | long time; |
| 98 | |
| 99 | if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0) |
| 100 | return 0; |
| 101 | while (!req.complete) |
| 102 | pmu_poll(); |
| 103 | |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 104 | time = (req.reply[1] << 24) | (req.reply[2] << 16) | |
| 105 | (req.reply[3] << 8) | req.reply[4]; |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 106 | return time - RTC_OFFSET; |
| 107 | } |
| 108 | |
| 109 | static void pmu_write_time(long data) |
| 110 | { |
| 111 | struct adb_request req; |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 112 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 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 | |
| 122 | static __u8 pmu_read_pram(int offset) |
| 123 | { |
| 124 | struct adb_request req; |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 125 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 126 | if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM, |
| 127 | (offset >> 8) & 0xFF, offset & 0xFF) < 0) |
| 128 | return 0; |
| 129 | while (!req.complete) |
| 130 | pmu_poll(); |
| 131 | return req.reply[3]; |
| 132 | } |
| 133 | |
| 134 | static void pmu_write_pram(int offset, __u8 data) |
| 135 | { |
| 136 | struct adb_request req; |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 137 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 138 | if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM, |
| 139 | (offset >> 8) & 0xFF, offset & 0xFF, data) < 0) |
| 140 | return; |
| 141 | while (!req.complete) |
| 142 | pmu_poll(); |
| 143 | } |
| 144 | #else |
| 145 | #define pmu_read_time() 0 |
| 146 | #define pmu_write_time(n) |
| 147 | #define pmu_read_pram NULL |
| 148 | #define pmu_write_pram NULL |
| 149 | #endif |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* |
| 152 | * VIA PRAM/RTC access routines |
| 153 | * |
| 154 | * Must be called with interrupts disabled and |
| 155 | * the RTC should be enabled. |
| 156 | */ |
| 157 | |
| 158 | static __u8 via_pram_readbyte(void) |
| 159 | { |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 160 | int i, reg; |
| 161 | __u8 data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | reg = via1[vBufB] & ~VIA1B_vRTCClk; |
| 164 | |
| 165 | /* Set the RTC data line to be an input. */ |
| 166 | |
| 167 | via1[vDirB] &= ~VIA1B_vRTCData; |
| 168 | |
| 169 | /* The bits of the byte come out in MSB order */ |
| 170 | |
| 171 | data = 0; |
| 172 | for (i = 0 ; i < 8 ; i++) { |
| 173 | via1[vBufB] = reg; |
| 174 | via1[vBufB] = reg | VIA1B_vRTCClk; |
| 175 | data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData); |
| 176 | } |
| 177 | |
| 178 | /* Return RTC data line to output state */ |
| 179 | |
| 180 | via1[vDirB] |= VIA1B_vRTCData; |
| 181 | |
| 182 | return data; |
| 183 | } |
| 184 | |
| 185 | static void via_pram_writebyte(__u8 data) |
| 186 | { |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 187 | int i, reg, bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData); |
| 190 | |
| 191 | /* The bits of the byte go in in MSB order */ |
| 192 | |
| 193 | for (i = 0 ; i < 8 ; i++) { |
| 194 | bit = data & 0x80? 1 : 0; |
| 195 | data <<= 1; |
| 196 | via1[vBufB] = reg | bit; |
| 197 | via1[vBufB] = reg | bit | VIA1B_vRTCClk; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Execute a VIA PRAM/RTC command. For read commands |
| 203 | * data should point to a one-byte buffer for the |
| 204 | * resulting data. For write commands it should point |
| 205 | * to the data byte to for the command. |
| 206 | * |
| 207 | * This function disables all interrupts while running. |
| 208 | */ |
| 209 | |
| 210 | static void via_pram_command(int command, __u8 *data) |
| 211 | { |
| 212 | unsigned long flags; |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 213 | int is_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | local_irq_save(flags); |
| 216 | |
| 217 | /* Enable the RTC and make sure the strobe line is high */ |
| 218 | |
| 219 | via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb; |
| 220 | |
| 221 | if (command & 0xFF00) { /* extended (two-byte) command */ |
| 222 | via_pram_writebyte((command & 0xFF00) >> 8); |
| 223 | via_pram_writebyte(command & 0xFF); |
| 224 | is_read = command & 0x8000; |
| 225 | } else { /* one-byte command */ |
| 226 | via_pram_writebyte(command); |
| 227 | is_read = command & 0x80; |
| 228 | } |
| 229 | if (is_read) { |
| 230 | *data = via_pram_readbyte(); |
| 231 | } else { |
| 232 | via_pram_writebyte(*data); |
| 233 | } |
| 234 | |
| 235 | /* All done, disable the RTC */ |
| 236 | |
| 237 | via1[vBufB] |= VIA1B_vRTCEnb; |
| 238 | |
| 239 | local_irq_restore(flags); |
| 240 | } |
| 241 | |
| 242 | static __u8 via_read_pram(int offset) |
| 243 | { |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static void via_write_pram(int offset, __u8 data) |
| 248 | { |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Return the current time in seconds since January 1, 1904. |
| 253 | * |
| 254 | * This only works on machines with the VIA-based PRAM/RTC, which |
| 255 | * is basically any machine with Mac II-style ADB. |
| 256 | */ |
| 257 | |
| 258 | static long via_read_time(void) |
| 259 | { |
| 260 | union { |
Finn Thain | 75a2385 | 2011-07-18 00:06:10 +1000 | [diff] [blame] | 261 | __u8 cdata[4]; |
| 262 | long idata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } result, last_result; |
Finn Thain | 75a2385 | 2011-07-18 00:06:10 +1000 | [diff] [blame] | 264 | int count = 1; |
| 265 | |
| 266 | via_pram_command(0x81, &last_result.cdata[3]); |
| 267 | via_pram_command(0x85, &last_result.cdata[2]); |
| 268 | via_pram_command(0x89, &last_result.cdata[1]); |
| 269 | via_pram_command(0x8D, &last_result.cdata[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | * The NetBSD guys say to loop until you get the same reading |
| 273 | * twice in a row. |
| 274 | */ |
| 275 | |
Finn Thain | 75a2385 | 2011-07-18 00:06:10 +1000 | [diff] [blame] | 276 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | via_pram_command(0x81, &result.cdata[3]); |
| 278 | via_pram_command(0x85, &result.cdata[2]); |
| 279 | via_pram_command(0x89, &result.cdata[1]); |
| 280 | via_pram_command(0x8D, &result.cdata[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Finn Thain | 75a2385 | 2011-07-18 00:06:10 +1000 | [diff] [blame] | 282 | if (result.idata == last_result.idata) |
| 283 | return result.idata - RTC_OFFSET; |
| 284 | |
| 285 | if (++count > 10) |
| 286 | break; |
| 287 | |
| 288 | last_result.idata = result.idata; |
| 289 | } |
| 290 | |
Geert Uytterhoeven | 889121b | 2017-04-08 19:51:15 -0400 | [diff] [blame] | 291 | pr_err("via_read_time: failed to read a stable value; got 0x%08lx then 0x%08lx\n", |
Finn Thain | 75a2385 | 2011-07-18 00:06:10 +1000 | [diff] [blame] | 292 | last_result.idata, result.idata); |
| 293 | |
| 294 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Set the current time to a number of 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 | |
| 304 | static void via_write_time(long time) |
| 305 | { |
| 306 | union { |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 307 | __u8 cdata[4]; |
| 308 | long idata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } data; |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 310 | __u8 temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | /* Clear the write protect bit */ |
| 313 | |
| 314 | temp = 0x55; |
| 315 | via_pram_command(0x35, &temp); |
| 316 | |
| 317 | data.idata = time + RTC_OFFSET; |
| 318 | via_pram_command(0x01, &data.cdata[3]); |
| 319 | via_pram_command(0x05, &data.cdata[2]); |
| 320 | via_pram_command(0x09, &data.cdata[1]); |
| 321 | via_pram_command(0x0D, &data.cdata[0]); |
| 322 | |
| 323 | /* Set the write protect bit */ |
| 324 | |
| 325 | temp = 0xD5; |
| 326 | via_pram_command(0x35, &temp); |
| 327 | } |
| 328 | |
| 329 | static void via_shutdown(void) |
| 330 | { |
| 331 | if (rbv_present) { |
| 332 | via2[rBufB] &= ~0x04; |
| 333 | } else { |
| 334 | /* Direction of vDirB is output */ |
| 335 | via2[vDirB] |= 0x04; |
| 336 | /* Send a value of 0 on that line */ |
| 337 | via2[vBufB] &= ~0x04; |
| 338 | mdelay(1000); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * FIXME: not sure how this is supposed to work exactly... |
| 344 | */ |
| 345 | |
| 346 | static void oss_shutdown(void) |
| 347 | { |
| 348 | oss->rom_ctrl = OSS_POWEROFF; |
| 349 | } |
| 350 | |
| 351 | #ifdef CONFIG_ADB_CUDA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | static void cuda_restart(void) |
| 353 | { |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 354 | struct adb_request req; |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 355 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 356 | if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0) |
| 357 | return; |
| 358 | while (!req.complete) |
| 359 | cuda_poll(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static void cuda_shutdown(void) |
| 363 | { |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 364 | struct adb_request req; |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 365 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 366 | if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0) |
| 367 | return; |
Finn Thain | 41e93a3 | 2017-08-12 17:11:02 +1000 | [diff] [blame] | 368 | |
| 369 | /* Avoid infinite polling loop when PSU is not under Cuda control */ |
| 370 | switch (macintosh_config->ident) { |
| 371 | case MAC_MODEL_C660: |
| 372 | case MAC_MODEL_Q605: |
| 373 | case MAC_MODEL_Q605_ACC: |
| 374 | case MAC_MODEL_P475: |
| 375 | case MAC_MODEL_P475F: |
| 376 | return; |
| 377 | } |
| 378 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 379 | while (!req.complete) |
| 380 | cuda_poll(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | #endif /* CONFIG_ADB_CUDA */ |
| 383 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 384 | #ifdef CONFIG_ADB_PMU68K |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
| 386 | void pmu_restart(void) |
| 387 | { |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 388 | struct adb_request req; |
| 389 | if (pmu_request(&req, NULL, |
| 390 | 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0) |
| 391 | return; |
| 392 | while (!req.complete) |
| 393 | pmu_poll(); |
| 394 | if (pmu_request(&req, NULL, 1, PMU_RESET) < 0) |
| 395 | return; |
| 396 | while (!req.complete) |
| 397 | pmu_poll(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void pmu_shutdown(void) |
| 401 | { |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 402 | struct adb_request req; |
| 403 | if (pmu_request(&req, NULL, |
| 404 | 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0) |
| 405 | return; |
| 406 | while (!req.complete) |
| 407 | pmu_poll(); |
| 408 | if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0) |
| 409 | return; |
| 410 | while (!req.complete) |
| 411 | pmu_poll(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 414 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
| 416 | /* |
| 417 | *------------------------------------------------------------------- |
| 418 | * Below this point are the generic routines; they'll dispatch to the |
| 419 | * correct routine for the hardware on which we're running. |
| 420 | *------------------------------------------------------------------- |
| 421 | */ |
| 422 | |
| 423 | void mac_pram_read(int offset, __u8 *buffer, int len) |
| 424 | { |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 425 | __u8 (*func)(int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | int i; |
| 427 | |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 428 | switch (macintosh_config->adb_type) { |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 429 | case MAC_ADB_PB1: |
| 430 | case MAC_ADB_PB2: |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 431 | func = pmu_read_pram; |
| 432 | break; |
Finn Thain | f74faec | 2016-12-31 19:56:26 -0500 | [diff] [blame] | 433 | case MAC_ADB_EGRET: |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 434 | case MAC_ADB_CUDA: |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 435 | func = cuda_read_pram; |
| 436 | break; |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 437 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | func = via_read_pram; |
| 439 | } |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 440 | if (!func) |
| 441 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | for (i = 0 ; i < len ; i++) { |
| 443 | buffer[i] = (*func)(offset++); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | void mac_pram_write(int offset, __u8 *buffer, int len) |
| 448 | { |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 449 | void (*func)(int, __u8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | int i; |
| 451 | |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 452 | switch (macintosh_config->adb_type) { |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 453 | case MAC_ADB_PB1: |
| 454 | case MAC_ADB_PB2: |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 455 | func = pmu_write_pram; |
| 456 | break; |
Finn Thain | f74faec | 2016-12-31 19:56:26 -0500 | [diff] [blame] | 457 | case MAC_ADB_EGRET: |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 458 | case MAC_ADB_CUDA: |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 459 | func = cuda_write_pram; |
| 460 | break; |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 461 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | func = via_write_pram; |
| 463 | } |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 464 | if (!func) |
| 465 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | for (i = 0 ; i < len ; i++) { |
| 467 | (*func)(offset++, buffer[i]); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | void mac_poweroff(void) |
| 472 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | if (oss_present) { |
| 474 | oss_shutdown(); |
| 475 | } else if (macintosh_config->adb_type == MAC_ADB_II) { |
| 476 | via_shutdown(); |
| 477 | #ifdef CONFIG_ADB_CUDA |
Finn Thain | f74faec | 2016-12-31 19:56:26 -0500 | [diff] [blame] | 478 | } else if (macintosh_config->adb_type == MAC_ADB_EGRET || |
| 479 | macintosh_config->adb_type == MAC_ADB_CUDA) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | cuda_shutdown(); |
| 481 | #endif |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 482 | #ifdef CONFIG_ADB_PMU68K |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } else if (macintosh_config->adb_type == MAC_ADB_PB1 |
| 484 | || macintosh_config->adb_type == MAC_ADB_PB2) { |
| 485 | pmu_shutdown(); |
| 486 | #endif |
| 487 | } |
Finn Thain | 558d5ad | 2017-08-12 17:12:12 +1000 | [diff] [blame] | 488 | |
Geert Uytterhoeven | 889121b | 2017-04-08 19:51:15 -0400 | [diff] [blame] | 489 | pr_crit("It is now safe to turn off your Macintosh.\n"); |
Finn Thain | 558d5ad | 2017-08-12 17:12:12 +1000 | [diff] [blame] | 490 | local_irq_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | while(1); |
| 492 | } |
| 493 | |
| 494 | void mac_reset(void) |
| 495 | { |
| 496 | if (macintosh_config->adb_type == MAC_ADB_II) { |
| 497 | unsigned long flags; |
| 498 | |
| 499 | /* need ROMBASE in booter */ |
| 500 | /* indeed, plus need to MAP THE ROM !! */ |
| 501 | |
| 502 | if (mac_bi_data.rombase == 0) |
| 503 | mac_bi_data.rombase = 0x40800000; |
| 504 | |
| 505 | /* works on some */ |
| 506 | rom_reset = (void *) (mac_bi_data.rombase + 0xa); |
| 507 | |
| 508 | if (macintosh_config->ident == MAC_MODEL_SE30) { |
| 509 | /* |
| 510 | * MSch: Machines known to crash on ROM reset ... |
| 511 | */ |
| 512 | } else { |
| 513 | local_irq_save(flags); |
| 514 | |
| 515 | rom_reset(); |
| 516 | |
| 517 | local_irq_restore(flags); |
| 518 | } |
| 519 | #ifdef CONFIG_ADB_CUDA |
Finn Thain | f74faec | 2016-12-31 19:56:26 -0500 | [diff] [blame] | 520 | } else if (macintosh_config->adb_type == MAC_ADB_EGRET || |
| 521 | macintosh_config->adb_type == MAC_ADB_CUDA) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | cuda_restart(); |
| 523 | #endif |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 524 | #ifdef CONFIG_ADB_PMU68K |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } else if (macintosh_config->adb_type == MAC_ADB_PB1 |
| 526 | || macintosh_config->adb_type == MAC_ADB_PB2) { |
| 527 | pmu_restart(); |
| 528 | #endif |
| 529 | } else if (CPU_IS_030) { |
| 530 | |
| 531 | /* 030-specific reset routine. The idea is general, but the |
| 532 | * specific registers to reset are '030-specific. Until I |
| 533 | * have a non-030 machine, I can't test anything else. |
| 534 | * -- C. Scott Ananian <cananian@alumni.princeton.edu> |
| 535 | */ |
| 536 | |
| 537 | unsigned long rombase = 0x40000000; |
| 538 | |
| 539 | /* make a 1-to-1 mapping, using the transparent tran. reg. */ |
| 540 | unsigned long virt = (unsigned long) mac_reset; |
| 541 | unsigned long phys = virt_to_phys(mac_reset); |
Al Viro | 77add9f | 2006-01-12 01:06:18 -0800 | [diff] [blame] | 542 | unsigned long addr = (phys&0xFF000000)|0x8777; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | unsigned long offset = phys-virt; |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | local_irq_disable(); /* lets not screw this up, ok? */ |
| 546 | __asm__ __volatile__(".chip 68030\n\t" |
| 547 | "pmove %0,%/tt0\n\t" |
| 548 | ".chip 68k" |
Al Viro | 77add9f | 2006-01-12 01:06:18 -0800 | [diff] [blame] | 549 | : : "m" (addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | /* Now jump to physical address so we can disable MMU */ |
| 551 | __asm__ __volatile__( |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 552 | ".chip 68030\n\t" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | "lea %/pc@(1f),%/a0\n\t" |
| 554 | "addl %0,%/a0\n\t"/* fixup target address and stack ptr */ |
| 555 | "addl %0,%/sp\n\t" |
| 556 | "pflusha\n\t" |
| 557 | "jmp %/a0@\n\t" /* jump into physical memory */ |
| 558 | "0:.long 0\n\t" /* a constant zero. */ |
| 559 | /* OK. Now reset everything and jump to reset vector. */ |
| 560 | "1:\n\t" |
| 561 | "lea %/pc@(0b),%/a0\n\t" |
| 562 | "pmove %/a0@, %/tc\n\t" /* disable mmu */ |
| 563 | "pmove %/a0@, %/tt0\n\t" /* disable tt0 */ |
| 564 | "pmove %/a0@, %/tt1\n\t" /* disable tt1 */ |
| 565 | "movel #0, %/a0\n\t" |
| 566 | "movec %/a0, %/vbr\n\t" /* clear vector base register */ |
| 567 | "movec %/a0, %/cacr\n\t" /* disable caches */ |
| 568 | "movel #0x0808,%/a0\n\t" |
| 569 | "movec %/a0, %/cacr\n\t" /* flush i&d caches */ |
| 570 | "movew #0x2700,%/sr\n\t" /* set up status register */ |
| 571 | "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */ |
| 572 | "movec %/a0, %/isp\n\t" |
| 573 | "movel %1@(0x4),%/a0\n\t" /* load reset vector */ |
| 574 | "reset\n\t" /* reset external devices */ |
| 575 | "jmp %/a0@\n\t" /* jump to the reset vector */ |
| 576 | ".chip 68k" |
| 577 | : : "r" (offset), "a" (rombase) : "a0"); |
| 578 | } |
| 579 | |
| 580 | /* should never get here */ |
Geert Uytterhoeven | 889121b | 2017-04-08 19:51:15 -0400 | [diff] [blame] | 581 | pr_crit("Restart failed. Please restart manually.\n"); |
Finn Thain | 558d5ad | 2017-08-12 17:12:12 +1000 | [diff] [blame] | 582 | local_irq_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | while(1); |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * This function translates seconds since 1970 into a proper date. |
| 588 | * |
| 589 | * Algorithm cribbed from glibc2.1, __offtime(). |
| 590 | */ |
| 591 | #define SECS_PER_MINUTE (60) |
| 592 | #define SECS_PER_HOUR (SECS_PER_MINUTE * 60) |
| 593 | #define SECS_PER_DAY (SECS_PER_HOUR * 24) |
| 594 | |
| 595 | static void unmktime(unsigned long time, long offset, |
| 596 | int *yearp, int *monp, int *dayp, |
| 597 | int *hourp, int *minp, int *secp) |
| 598 | { |
| 599 | /* How many days come before each month (0-12). */ |
| 600 | static const unsigned short int __mon_yday[2][13] = |
| 601 | { |
| 602 | /* Normal years. */ |
| 603 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, |
| 604 | /* Leap years. */ |
| 605 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } |
| 606 | }; |
| 607 | long int days, rem, y, wday, yday; |
| 608 | const unsigned short int *ip; |
| 609 | |
| 610 | days = time / SECS_PER_DAY; |
| 611 | rem = time % SECS_PER_DAY; |
| 612 | rem += offset; |
| 613 | while (rem < 0) { |
| 614 | rem += SECS_PER_DAY; |
| 615 | --days; |
| 616 | } |
| 617 | while (rem >= SECS_PER_DAY) { |
| 618 | rem -= SECS_PER_DAY; |
| 619 | ++days; |
| 620 | } |
| 621 | *hourp = rem / SECS_PER_HOUR; |
| 622 | rem %= SECS_PER_HOUR; |
| 623 | *minp = rem / SECS_PER_MINUTE; |
| 624 | *secp = rem % SECS_PER_MINUTE; |
| 625 | /* January 1, 1970 was a Thursday. */ |
| 626 | wday = (4 + days) % 7; /* Day in the week. Not currently used */ |
| 627 | if (wday < 0) wday += 7; |
| 628 | y = 1970; |
| 629 | |
| 630 | #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0)) |
| 631 | #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400)) |
| 632 | #define __isleap(year) \ |
| 633 | ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) |
| 634 | |
| 635 | while (days < 0 || days >= (__isleap (y) ? 366 : 365)) |
| 636 | { |
| 637 | /* Guess a corrected year, assuming 365 days per year. */ |
| 638 | long int yg = y + days / 365 - (days % 365 < 0); |
| 639 | |
| 640 | /* Adjust DAYS and Y to match the guessed year. */ |
Finn Thain | 31b1c78 | 2018-03-11 23:38:28 -0400 | [diff] [blame^] | 641 | days -= (yg - y) * 365 + |
| 642 | LEAPS_THRU_END_OF(yg - 1) - LEAPS_THRU_END_OF(y - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | y = yg; |
| 644 | } |
| 645 | *yearp = y - 1900; |
| 646 | yday = days; /* day in the year. Not currently used. */ |
| 647 | ip = __mon_yday[__isleap(y)]; |
| 648 | for (y = 11; days < (long int) ip[y]; --y) |
| 649 | continue; |
| 650 | days -= ip[y]; |
| 651 | *monp = y; |
| 652 | *dayp = days + 1; /* day in the month */ |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Read/write the hardware clock. |
| 658 | */ |
| 659 | |
| 660 | int mac_hwclk(int op, struct rtc_time *t) |
| 661 | { |
| 662 | unsigned long now; |
| 663 | |
| 664 | if (!op) { /* read */ |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 665 | switch (macintosh_config->adb_type) { |
| 666 | case MAC_ADB_II: |
| 667 | case MAC_ADB_IOP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | now = via_read_time(); |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 669 | break; |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 670 | case MAC_ADB_PB1: |
| 671 | case MAC_ADB_PB2: |
| 672 | now = pmu_read_time(); |
| 673 | break; |
Finn Thain | f74faec | 2016-12-31 19:56:26 -0500 | [diff] [blame] | 674 | case MAC_ADB_EGRET: |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 675 | case MAC_ADB_CUDA: |
| 676 | now = cuda_read_time(); |
| 677 | break; |
| 678 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | now = 0; |
| 680 | } |
| 681 | |
| 682 | t->tm_wday = 0; |
| 683 | unmktime(now, 0, |
| 684 | &t->tm_year, &t->tm_mon, &t->tm_mday, |
| 685 | &t->tm_hour, &t->tm_min, &t->tm_sec); |
Geert Uytterhoeven | 889121b | 2017-04-08 19:51:15 -0400 | [diff] [blame] | 686 | pr_debug("%s: read %04d-%02d-%-2d %02d:%02d:%02d\n", |
| 687 | __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, |
| 688 | t->tm_hour, t->tm_min, t->tm_sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } else { /* write */ |
Geert Uytterhoeven | 889121b | 2017-04-08 19:51:15 -0400 | [diff] [blame] | 690 | pr_debug("%s: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n", |
| 691 | __func__, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, |
| 692 | t->tm_hour, t->tm_min, t->tm_sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, |
| 695 | t->tm_hour, t->tm_min, t->tm_sec); |
| 696 | |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 697 | switch (macintosh_config->adb_type) { |
| 698 | case MAC_ADB_II: |
| 699 | case MAC_ADB_IOP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | via_write_time(now); |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 701 | break; |
Finn Thain | f74faec | 2016-12-31 19:56:26 -0500 | [diff] [blame] | 702 | case MAC_ADB_EGRET: |
Al Viro | 3272244 | 2006-01-12 01:06:13 -0800 | [diff] [blame] | 703 | case MAC_ADB_CUDA: |
| 704 | cuda_write_time(now); |
| 705 | break; |
| 706 | case MAC_ADB_PB1: |
| 707 | case MAC_ADB_PB2: |
| 708 | pmu_write_time(now); |
| 709 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Set minutes/seconds in the hardware clock |
| 717 | */ |
| 718 | |
| 719 | int mac_set_clock_mmss (unsigned long nowtime) |
| 720 | { |
| 721 | struct rtc_time now; |
| 722 | |
| 723 | mac_hwclk(0, &now); |
| 724 | now.tm_sec = nowtime % 60; |
| 725 | now.tm_min = (nowtime / 60) % 60; |
| 726 | mac_hwclk(1, &now); |
| 727 | |
| 728 | return 0; |
| 729 | } |