Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Device driver for the via-cuda on Apple Powermacs. |
| 3 | * |
| 4 | * The VIA (versatile interface adapter) interfaces to the CUDA, |
| 5 | * a 6805 microprocessor core which controls the ADB (Apple Desktop |
| 6 | * Bus) which connects to the keyboard and mouse. The CUDA also |
| 7 | * controls system power and the RTC (real time clock) chip. |
| 8 | * |
| 9 | * Copyright (C) 1996 Paul Mackerras. |
| 10 | */ |
| 11 | #include <stdarg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/types.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/adb.h> |
| 17 | #include <linux/cuda.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #ifdef CONFIG_PPC |
| 21 | #include <asm/prom.h> |
| 22 | #include <asm/machdep.h> |
| 23 | #else |
| 24 | #include <asm/macintosh.h> |
| 25 | #include <asm/macints.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/mac_via.h> |
| 27 | #endif |
| 28 | #include <asm/io.h> |
| 29 | #include <asm/system.h> |
| 30 | #include <linux/init.h> |
| 31 | |
| 32 | static volatile unsigned char __iomem *via; |
| 33 | static DEFINE_SPINLOCK(cuda_lock); |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | /* VIA registers - spaced 0x200 bytes apart */ |
| 36 | #define RS 0x200 /* skip between registers */ |
| 37 | #define B 0 /* B-side data */ |
| 38 | #define A RS /* A-side data */ |
| 39 | #define DIRB (2*RS) /* B-side direction (1=output) */ |
| 40 | #define DIRA (3*RS) /* A-side direction (1=output) */ |
| 41 | #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */ |
| 42 | #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */ |
| 43 | #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */ |
| 44 | #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */ |
| 45 | #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */ |
| 46 | #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */ |
| 47 | #define SR (10*RS) /* Shift register */ |
| 48 | #define ACR (11*RS) /* Auxiliary control register */ |
| 49 | #define PCR (12*RS) /* Peripheral control register */ |
| 50 | #define IFR (13*RS) /* Interrupt flag register */ |
| 51 | #define IER (14*RS) /* Interrupt enable register */ |
| 52 | #define ANH (15*RS) /* A-side data, no handshake */ |
| 53 | |
| 54 | /* Bits in B data register: all active low */ |
| 55 | #define TREQ 0x08 /* Transfer request (input) */ |
| 56 | #define TACK 0x10 /* Transfer acknowledge (output) */ |
| 57 | #define TIP 0x20 /* Transfer in progress (output) */ |
| 58 | |
| 59 | /* Bits in ACR */ |
| 60 | #define SR_CTRL 0x1c /* Shift register control bits */ |
| 61 | #define SR_EXT 0x0c /* Shift on external clock */ |
| 62 | #define SR_OUT 0x10 /* Shift out if 1 */ |
| 63 | |
| 64 | /* Bits in IFR and IER */ |
| 65 | #define IER_SET 0x80 /* set bits in IER */ |
| 66 | #define IER_CLR 0 /* clear bits in IER */ |
| 67 | #define SR_INT 0x04 /* Shift register full/empty */ |
| 68 | |
| 69 | static enum cuda_state { |
| 70 | idle, |
| 71 | sent_first_byte, |
| 72 | sending, |
| 73 | reading, |
| 74 | read_done, |
| 75 | awaiting_reply |
| 76 | } cuda_state; |
| 77 | |
| 78 | static struct adb_request *current_req; |
| 79 | static struct adb_request *last_req; |
| 80 | static unsigned char cuda_rbuf[16]; |
| 81 | static unsigned char *reply_ptr; |
| 82 | static int reading_reply; |
| 83 | static int data_index; |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 84 | static int cuda_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | #ifdef CONFIG_PPC |
| 86 | static struct device_node *vias; |
| 87 | #endif |
Olaf Hering | 8727585 | 2007-02-10 21:35:12 +0100 | [diff] [blame] | 88 | static int cuda_fully_inited; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | #ifdef CONFIG_ADB |
| 91 | static int cuda_probe(void); |
| 92 | static int cuda_init(void); |
| 93 | static int cuda_send_request(struct adb_request *req, int sync); |
| 94 | static int cuda_adb_autopoll(int devs); |
| 95 | static int cuda_reset_adb_bus(void); |
| 96 | #endif /* CONFIG_ADB */ |
| 97 | |
| 98 | static int cuda_init_via(void); |
| 99 | static void cuda_start(void); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 100 | static irqreturn_t cuda_interrupt(int irq, void *arg); |
| 101 | static void cuda_input(unsigned char *buf, int nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | void cuda_poll(void); |
| 103 | static int cuda_write(struct adb_request *req); |
| 104 | |
| 105 | int cuda_request(struct adb_request *req, |
| 106 | void (*done)(struct adb_request *), int nbytes, ...); |
| 107 | |
| 108 | #ifdef CONFIG_ADB |
| 109 | struct adb_driver via_cuda_driver = { |
| 110 | "CUDA", |
| 111 | cuda_probe, |
| 112 | cuda_init, |
| 113 | cuda_send_request, |
| 114 | cuda_adb_autopoll, |
| 115 | cuda_poll, |
| 116 | cuda_reset_adb_bus |
| 117 | }; |
| 118 | #endif /* CONFIG_ADB */ |
| 119 | |
| 120 | #ifdef CONFIG_PPC |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 121 | int __init find_via_cuda(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | struct adb_request req; |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 124 | phys_addr_t taddr; |
Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame] | 125 | const u32 *reg; |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 126 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | if (vias != 0) |
| 129 | return 1; |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 130 | vias = of_find_node_by_name(NULL, "via-cuda"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if (vias == 0) |
| 132 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Stephen Rothwell | 01b2726 | 2007-04-27 13:41:15 +1000 | [diff] [blame] | 134 | reg = of_get_property(vias, "reg", NULL); |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 135 | if (reg == NULL) { |
| 136 | printk(KERN_ERR "via-cuda: No \"reg\" property !\n"); |
| 137 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 139 | taddr = of_translate_address(vias, reg); |
| 140 | if (taddr == 0) { |
| 141 | printk(KERN_ERR "via-cuda: Can't translate address !\n"); |
| 142 | goto fail; |
| 143 | } |
| 144 | via = ioremap(taddr, 0x2000); |
| 145 | if (via == NULL) { |
| 146 | printk(KERN_ERR "via-cuda: Can't map address !\n"); |
| 147 | goto fail; |
| 148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | cuda_state = idle; |
| 151 | sys_ctrler = SYS_CTRLER_CUDA; |
| 152 | |
| 153 | err = cuda_init_via(); |
| 154 | if (err) { |
| 155 | printk(KERN_ERR "cuda_init_via() failed\n"); |
| 156 | via = NULL; |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /* Clear and enable interrupts, but only on PPC. On 68K it's done */ |
| 161 | /* for us by the main VIA driver in arch/m68k/mac/via.c */ |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */ |
| 164 | out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | /* enable autopoll */ |
| 167 | cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1); |
| 168 | while (!req.complete) |
| 169 | cuda_poll(); |
| 170 | |
| 171 | return 1; |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 172 | |
| 173 | fail: |
| 174 | of_node_put(vias); |
| 175 | vias = NULL; |
| 176 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | #endif /* CONFIG_PPC */ |
| 179 | |
| 180 | static int __init via_cuda_start(void) |
| 181 | { |
| 182 | if (via == NULL) |
| 183 | return -ENODEV; |
| 184 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 185 | #ifdef CONFIG_MAC |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 186 | cuda_irq = IRQ_MAC_ADB; |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 187 | #else /* CONFIG_MAC */ |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 188 | cuda_irq = irq_of_parse_and_map(vias, 0); |
| 189 | if (cuda_irq == NO_IRQ) { |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 190 | printk(KERN_ERR "via-cuda: can't map interrupts for %s\n", |
| 191 | vias->full_name); |
| 192 | return -ENODEV; |
| 193 | } |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 194 | #endif /* CONFIG_MAC */ |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 195 | |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 196 | if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) { |
| 197 | printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | return -EAGAIN; |
| 199 | } |
| 200 | |
| 201 | printk("Macintosh CUDA driver v0.5 for Unified ADB.\n"); |
| 202 | |
| 203 | cuda_fully_inited = 1; |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | device_initcall(via_cuda_start); |
| 208 | |
| 209 | #ifdef CONFIG_ADB |
| 210 | static int |
| 211 | cuda_probe(void) |
| 212 | { |
| 213 | #ifdef CONFIG_PPC |
| 214 | if (sys_ctrler != SYS_CTRLER_CUDA) |
| 215 | return -ENODEV; |
| 216 | #else |
| 217 | if (macintosh_config->adb_type != MAC_ADB_CUDA) |
| 218 | return -ENODEV; |
| 219 | via = via1; |
| 220 | #endif |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int __init |
| 225 | cuda_init(void) |
| 226 | { |
| 227 | #ifdef CONFIG_PPC |
| 228 | if (via == NULL) |
| 229 | return -ENODEV; |
| 230 | return 0; |
| 231 | #else |
| 232 | int err = cuda_init_via(); |
| 233 | if (err) { |
| 234 | printk(KERN_ERR "cuda_init_via() failed\n"); |
| 235 | return -ENODEV; |
| 236 | } |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 237 | out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | return via_cuda_start(); |
| 240 | #endif |
| 241 | } |
| 242 | #endif /* CONFIG_ADB */ |
| 243 | |
| 244 | #define WAIT_FOR(cond, what) \ |
| 245 | do { \ |
| 246 | int x; \ |
| 247 | for (x = 1000; !(cond); --x) { \ |
| 248 | if (x == 0) { \ |
| 249 | printk("Timeout waiting for " what "\n"); \ |
| 250 | return -ENXIO; \ |
| 251 | } \ |
| 252 | udelay(100); \ |
| 253 | } \ |
| 254 | } while (0) |
| 255 | |
| 256 | static int |
| 257 | cuda_init_via(void) |
| 258 | { |
| 259 | out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */ |
| 260 | out_8(&via[B], in_8(&via[B]) | TACK | TIP); /* negate them */ |
| 261 | out_8(&via[ACR] ,(in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */ |
| 262 | (void)in_8(&via[SR]); /* clear any left-over data */ |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 263 | #ifdef CONFIG_PPC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | out_8(&via[IER], 0x7f); /* disable interrupts from VIA */ |
| 265 | (void)in_8(&via[IER]); |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 266 | #else |
| 267 | out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | #endif |
| 269 | |
| 270 | /* delay 4ms and then clear any pending interrupt */ |
| 271 | mdelay(4); |
| 272 | (void)in_8(&via[SR]); |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 273 | out_8(&via[IFR], SR_INT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | /* sync with the CUDA - assert TACK without TIP */ |
| 276 | out_8(&via[B], in_8(&via[B]) & ~TACK); |
| 277 | |
| 278 | /* wait for the CUDA to assert TREQ in response */ |
| 279 | WAIT_FOR((in_8(&via[B]) & TREQ) == 0, "CUDA response to sync"); |
| 280 | |
| 281 | /* wait for the interrupt and then clear it */ |
| 282 | WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)"); |
| 283 | (void)in_8(&via[SR]); |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 284 | out_8(&via[IFR], SR_INT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | /* finish the sync by negating TACK */ |
| 287 | out_8(&via[B], in_8(&via[B]) | TACK); |
| 288 | |
| 289 | /* wait for the CUDA to negate TREQ and the corresponding interrupt */ |
| 290 | WAIT_FOR(in_8(&via[B]) & TREQ, "CUDA response to sync (3)"); |
| 291 | WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)"); |
| 292 | (void)in_8(&via[SR]); |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 293 | out_8(&via[IFR], SR_INT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | out_8(&via[B], in_8(&via[B]) | TIP); /* should be unnecessary */ |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | #ifdef CONFIG_ADB |
| 300 | /* Send an ADB command */ |
| 301 | static int |
| 302 | cuda_send_request(struct adb_request *req, int sync) |
| 303 | { |
| 304 | int i; |
| 305 | |
| 306 | if ((via == NULL) || !cuda_fully_inited) { |
| 307 | req->complete = 1; |
| 308 | return -ENXIO; |
| 309 | } |
| 310 | |
| 311 | req->reply_expected = 1; |
| 312 | |
| 313 | i = cuda_write(req); |
| 314 | if (i) |
| 315 | return i; |
| 316 | |
| 317 | if (sync) { |
| 318 | while (!req->complete) |
| 319 | cuda_poll(); |
| 320 | } |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | |
| 325 | /* Enable/disable autopolling */ |
| 326 | static int |
| 327 | cuda_adb_autopoll(int devs) |
| 328 | { |
| 329 | struct adb_request req; |
| 330 | |
| 331 | if ((via == NULL) || !cuda_fully_inited) |
| 332 | return -ENXIO; |
| 333 | |
| 334 | cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0)); |
| 335 | while (!req.complete) |
| 336 | cuda_poll(); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /* Reset adb bus - how do we do this?? */ |
| 341 | static int |
| 342 | cuda_reset_adb_bus(void) |
| 343 | { |
| 344 | struct adb_request req; |
| 345 | |
| 346 | if ((via == NULL) || !cuda_fully_inited) |
| 347 | return -ENXIO; |
| 348 | |
| 349 | cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */ |
| 350 | while (!req.complete) |
| 351 | cuda_poll(); |
| 352 | return 0; |
| 353 | } |
| 354 | #endif /* CONFIG_ADB */ |
| 355 | /* Construct and send a cuda request */ |
| 356 | int |
| 357 | cuda_request(struct adb_request *req, void (*done)(struct adb_request *), |
| 358 | int nbytes, ...) |
| 359 | { |
| 360 | va_list list; |
| 361 | int i; |
| 362 | |
| 363 | if (via == NULL) { |
| 364 | req->complete = 1; |
| 365 | return -ENXIO; |
| 366 | } |
| 367 | |
| 368 | req->nbytes = nbytes; |
| 369 | req->done = done; |
| 370 | va_start(list, nbytes); |
| 371 | for (i = 0; i < nbytes; ++i) |
| 372 | req->data[i] = va_arg(list, int); |
| 373 | va_end(list); |
| 374 | req->reply_expected = 1; |
| 375 | return cuda_write(req); |
| 376 | } |
| 377 | |
| 378 | static int |
| 379 | cuda_write(struct adb_request *req) |
| 380 | { |
| 381 | unsigned long flags; |
| 382 | |
| 383 | if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) { |
| 384 | req->complete = 1; |
| 385 | return -EINVAL; |
| 386 | } |
| 387 | req->next = NULL; |
| 388 | req->sent = 0; |
| 389 | req->complete = 0; |
| 390 | req->reply_len = 0; |
| 391 | |
| 392 | spin_lock_irqsave(&cuda_lock, flags); |
| 393 | if (current_req != 0) { |
| 394 | last_req->next = req; |
| 395 | last_req = req; |
| 396 | } else { |
| 397 | current_req = req; |
| 398 | last_req = req; |
| 399 | if (cuda_state == idle) |
| 400 | cuda_start(); |
| 401 | } |
| 402 | spin_unlock_irqrestore(&cuda_lock, flags); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static void |
| 408 | cuda_start(void) |
| 409 | { |
| 410 | struct adb_request *req; |
| 411 | |
| 412 | /* assert cuda_state == idle */ |
| 413 | /* get the packet to send */ |
| 414 | req = current_req; |
| 415 | if (req == 0) |
| 416 | return; |
| 417 | if ((in_8(&via[B]) & TREQ) == 0) |
| 418 | return; /* a byte is coming in from the CUDA */ |
| 419 | |
| 420 | /* set the shift register to shift out and send a byte */ |
| 421 | out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT); |
| 422 | out_8(&via[SR], req->data[0]); |
| 423 | out_8(&via[B], in_8(&via[B]) & ~TIP); |
| 424 | cuda_state = sent_first_byte; |
| 425 | } |
| 426 | |
| 427 | void |
| 428 | cuda_poll(void) |
| 429 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | /* cuda_interrupt only takes a normal lock, we disable |
| 431 | * interrupts here to avoid re-entering and thus deadlocking. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | */ |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 433 | disable_irq(cuda_irq); |
Olof Johansson | 49f19ce | 2006-10-05 20:31:10 -0500 | [diff] [blame] | 434 | cuda_interrupt(0, NULL); |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 435 | enable_irq(cuda_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 439 | cuda_interrupt(int irq, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | int status; |
| 442 | struct adb_request *req = NULL; |
| 443 | unsigned char ibuf[16]; |
| 444 | int ibuf_len = 0; |
| 445 | int complete = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | spin_lock(&cuda_lock); |
| 448 | |
Finn Thain | 0251c38 | 2007-05-01 22:33:00 +0200 | [diff] [blame] | 449 | /* On powermacs, this handler is registered for the VIA IRQ. But it uses |
| 450 | * just the shift register IRQ -- other VIA interrupt sources are disabled. |
| 451 | * On m68k macs, the VIA IRQ sources are dispatched individually. Unless |
| 452 | * we are polling, the shift register IRQ flag has already been cleared. |
| 453 | */ |
| 454 | |
| 455 | #ifdef CONFIG_MAC |
| 456 | if (!arg) |
| 457 | #endif |
| 458 | { |
| 459 | if ((in_8(&via[IFR]) & SR_INT) == 0) { |
| 460 | spin_unlock(&cuda_lock); |
| 461 | return IRQ_NONE; |
| 462 | } else { |
| 463 | out_8(&via[IFR], SR_INT); |
| 464 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | status = (~in_8(&via[B]) & (TIP|TREQ)) | (in_8(&via[ACR]) & SR_OUT); |
| 468 | /* printk("cuda_interrupt: state=%d status=%x\n", cuda_state, status); */ |
| 469 | switch (cuda_state) { |
| 470 | case idle: |
| 471 | /* CUDA has sent us the first byte of data - unsolicited */ |
| 472 | if (status != TREQ) |
| 473 | printk("cuda: state=idle, status=%x\n", status); |
| 474 | (void)in_8(&via[SR]); |
| 475 | out_8(&via[B], in_8(&via[B]) & ~TIP); |
| 476 | cuda_state = reading; |
| 477 | reply_ptr = cuda_rbuf; |
| 478 | reading_reply = 0; |
| 479 | break; |
| 480 | |
| 481 | case awaiting_reply: |
| 482 | /* CUDA has sent us the first byte of data of a reply */ |
| 483 | if (status != TREQ) |
| 484 | printk("cuda: state=awaiting_reply, status=%x\n", status); |
| 485 | (void)in_8(&via[SR]); |
| 486 | out_8(&via[B], in_8(&via[B]) & ~TIP); |
| 487 | cuda_state = reading; |
| 488 | reply_ptr = current_req->reply; |
| 489 | reading_reply = 1; |
| 490 | break; |
| 491 | |
| 492 | case sent_first_byte: |
| 493 | if (status == TREQ + TIP + SR_OUT) { |
| 494 | /* collision */ |
| 495 | out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT); |
| 496 | (void)in_8(&via[SR]); |
| 497 | out_8(&via[B], in_8(&via[B]) | TIP | TACK); |
| 498 | cuda_state = idle; |
| 499 | } else { |
| 500 | /* assert status == TIP + SR_OUT */ |
| 501 | if (status != TIP + SR_OUT) |
| 502 | printk("cuda: state=sent_first_byte status=%x\n", status); |
| 503 | out_8(&via[SR], current_req->data[1]); |
| 504 | out_8(&via[B], in_8(&via[B]) ^ TACK); |
| 505 | data_index = 2; |
| 506 | cuda_state = sending; |
| 507 | } |
| 508 | break; |
| 509 | |
| 510 | case sending: |
| 511 | req = current_req; |
| 512 | if (data_index >= req->nbytes) { |
| 513 | out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT); |
| 514 | (void)in_8(&via[SR]); |
| 515 | out_8(&via[B], in_8(&via[B]) | TACK | TIP); |
| 516 | req->sent = 1; |
| 517 | if (req->reply_expected) { |
| 518 | cuda_state = awaiting_reply; |
| 519 | } else { |
| 520 | current_req = req->next; |
| 521 | complete = 1; |
| 522 | /* not sure about this */ |
| 523 | cuda_state = idle; |
| 524 | cuda_start(); |
| 525 | } |
| 526 | } else { |
| 527 | out_8(&via[SR], req->data[data_index++]); |
| 528 | out_8(&via[B], in_8(&via[B]) ^ TACK); |
| 529 | } |
| 530 | break; |
| 531 | |
| 532 | case reading: |
| 533 | *reply_ptr++ = in_8(&via[SR]); |
| 534 | if (status == TIP) { |
| 535 | /* that's all folks */ |
| 536 | out_8(&via[B], in_8(&via[B]) | TACK | TIP); |
| 537 | cuda_state = read_done; |
| 538 | } else { |
| 539 | /* assert status == TIP | TREQ */ |
| 540 | if (status != TIP + TREQ) |
| 541 | printk("cuda: state=reading status=%x\n", status); |
| 542 | out_8(&via[B], in_8(&via[B]) ^ TACK); |
| 543 | } |
| 544 | break; |
| 545 | |
| 546 | case read_done: |
| 547 | (void)in_8(&via[SR]); |
| 548 | if (reading_reply) { |
| 549 | req = current_req; |
| 550 | req->reply_len = reply_ptr - req->reply; |
| 551 | if (req->data[0] == ADB_PACKET) { |
| 552 | /* Have to adjust the reply from ADB commands */ |
| 553 | if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) { |
| 554 | /* the 0x2 bit indicates no response */ |
| 555 | req->reply_len = 0; |
| 556 | } else { |
| 557 | /* leave just the command and result bytes in the reply */ |
| 558 | req->reply_len -= 2; |
| 559 | memmove(req->reply, req->reply + 2, req->reply_len); |
| 560 | } |
| 561 | } |
| 562 | current_req = req->next; |
| 563 | complete = 1; |
| 564 | } else { |
| 565 | /* This is tricky. We must break the spinlock to call |
| 566 | * cuda_input. However, doing so means we might get |
| 567 | * re-entered from another CPU getting an interrupt |
| 568 | * or calling cuda_poll(). I ended up using the stack |
| 569 | * (it's only for 16 bytes) and moving the actual |
| 570 | * call to cuda_input to outside of the lock. |
| 571 | */ |
| 572 | ibuf_len = reply_ptr - cuda_rbuf; |
| 573 | memcpy(ibuf, cuda_rbuf, ibuf_len); |
| 574 | } |
| 575 | if (status == TREQ) { |
| 576 | out_8(&via[B], in_8(&via[B]) & ~TIP); |
| 577 | cuda_state = reading; |
| 578 | reply_ptr = cuda_rbuf; |
| 579 | reading_reply = 0; |
| 580 | } else { |
| 581 | cuda_state = idle; |
| 582 | cuda_start(); |
| 583 | } |
| 584 | break; |
| 585 | |
| 586 | default: |
| 587 | printk("cuda_interrupt: unknown cuda_state %d?\n", cuda_state); |
| 588 | } |
| 589 | spin_unlock(&cuda_lock); |
| 590 | if (complete && req) { |
| 591 | void (*done)(struct adb_request *) = req->done; |
| 592 | mb(); |
| 593 | req->complete = 1; |
| 594 | /* Here, we assume that if the request has a done member, the |
| 595 | * struct request will survive to setting req->complete to 1 |
| 596 | */ |
| 597 | if (done) |
| 598 | (*done)(req); |
| 599 | } |
| 600 | if (ibuf_len) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 601 | cuda_input(ibuf, ibuf_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | return IRQ_HANDLED; |
| 603 | } |
| 604 | |
| 605 | static void |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 606 | cuda_input(unsigned char *buf, int nb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
| 608 | int i; |
| 609 | |
| 610 | switch (buf[0]) { |
| 611 | case ADB_PACKET: |
| 612 | #ifdef CONFIG_XMON |
| 613 | if (nb == 5 && buf[2] == 0x2c) { |
| 614 | extern int xmon_wants_key, xmon_adb_keycode; |
| 615 | if (xmon_wants_key) { |
| 616 | xmon_adb_keycode = buf[3]; |
| 617 | return; |
| 618 | } |
| 619 | } |
| 620 | #endif /* CONFIG_XMON */ |
| 621 | #ifdef CONFIG_ADB |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 622 | adb_input(buf+2, nb-2, buf[1] & 0x40); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | #endif /* CONFIG_ADB */ |
| 624 | break; |
| 625 | |
| 626 | default: |
| 627 | printk("data from cuda (%d bytes):", nb); |
| 628 | for (i = 0; i < nb; ++i) |
| 629 | printk(" %.2x", buf[i]); |
| 630 | printk("\n"); |
| 631 | } |
| 632 | } |