1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <stdarg.h>
28#include <linux/types.h>
29#include <linux/errno.h>
30#include <linux/kernel.h>
31#include <linux/delay.h>
32#include <linux/adb.h>
33#include <linux/interrupt.h>
34#include <linux/init.h>
35#include <asm/macintosh.h>
36#include <asm/macints.h>
37#include <asm/mac_via.h>
38
39static volatile unsigned char *via;
40
41
42#define RS 0x200
43#define B 0
44#define A RS
45#define DIRB (2*RS)
46#define DIRA (3*RS)
47#define T1CL (4*RS)
48#define T1CH (5*RS)
49#define T1LL (6*RS)
50#define T1LH (7*RS)
51#define T2CL (8*RS)
52#define T2CH (9*RS)
53#define SR (10*RS)
54#define ACR (11*RS)
55#define PCR (12*RS)
56#define IFR (13*RS)
57#define IER (14*RS)
58#define ANH (15*RS)
59
60
61#define CTLR_IRQ 0x08
62#define ST_MASK 0x30
63
64
65#define SR_CTRL 0x1c
66#define SR_EXT 0x0c
67#define SR_OUT 0x10
68
69
70#define IER_SET 0x80
71#define IER_CLR 0
72#define SR_INT 0x04
73
74
75#define ST_CMD 0x00
76#define ST_EVEN 0x10
77#define ST_ODD 0x20
78#define ST_IDLE 0x30
79
80static int macii_init_via(void);
81static void macii_start(void);
82static irqreturn_t macii_interrupt(int irq, void *arg);
83static void macii_queue_poll(void);
84
85static int macii_probe(void);
86static int macii_init(void);
87static int macii_send_request(struct adb_request *req, int sync);
88static int macii_write(struct adb_request *req);
89static int macii_autopoll(int devs);
90static void macii_poll(void);
91static int macii_reset_bus(void);
92
93struct adb_driver via_macii_driver = {
94 "Mac II",
95 macii_probe,
96 macii_init,
97 macii_send_request,
98 macii_autopoll,
99 macii_poll,
100 macii_reset_bus
101};
102
103static enum macii_state {
104 idle,
105 sending,
106 reading,
107 read_done,
108} macii_state;
109
110static struct adb_request *current_req;
111static struct adb_request *last_req;
112static unsigned char reply_buf[16];
113static unsigned char *reply_ptr;
114static int reading_reply;
115static int data_index;
116static int reply_len;
117static int status;
118static int last_status;
119static int srq_asserted;
120static int command_byte;
121static int autopoll_devs;
122
123
124static int request_is_queued(struct adb_request *req) {
125 struct adb_request *cur;
126 unsigned long flags;
127 local_irq_save(flags);
128 cur = current_req;
129 while (cur) {
130 if (cur == req) {
131 local_irq_restore(flags);
132 return 1;
133 }
134 cur = cur->next;
135 }
136 local_irq_restore(flags);
137 return 0;
138}
139
140
141static int macii_probe(void)
142{
143 if (macintosh_config->adb_type != MAC_ADB_II) return -ENODEV;
144
145 via = via1;
146
147 printk("adb: Mac II ADB Driver v1.0 for Unified ADB\n");
148 return 0;
149}
150
151
152int macii_init(void)
153{
154 unsigned long flags;
155 int err;
156
157 local_irq_save(flags);
158
159 err = macii_init_via();
160 if (err) goto out;
161
162 err = request_irq(IRQ_MAC_ADB, macii_interrupt, 0, "ADB",
163 macii_interrupt);
164 if (err) goto out;
165
166 macii_state = idle;
167out:
168 local_irq_restore(flags);
169 return err;
170}
171
172
173static int macii_init_via(void)
174{
175 unsigned char x;
176
177
178 via[DIRB] = (via[DIRB] | ST_EVEN | ST_ODD) & ~CTLR_IRQ;
179
180
181 via[B] |= ST_IDLE;
182 last_status = via[B] & (ST_MASK|CTLR_IRQ);
183
184
185 via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;
186
187
188 x = via[SR];
189
190 return 0;
191}
192
193
194static void macii_queue_poll(void)
195{
196
197
198
199
200
201
202
203
204 int device_mask;
205 int next_device;
206 static struct adb_request req;
207
208 if (!autopoll_devs) return;
209
210 device_mask = (1 << (((command_byte & 0xF0) >> 4) + 1)) - 1;
211 if (autopoll_devs & ~device_mask)
212 next_device = ffs(autopoll_devs & ~device_mask) - 1;
213 else
214 next_device = ffs(autopoll_devs) - 1;
215
216 BUG_ON(request_is_queued(&req));
217
218 adb_request(&req, NULL, ADBREQ_NOSEND, 1,
219 ADB_READREG(next_device, 0));
220
221 req.sent = 0;
222 req.complete = 0;
223 req.reply_len = 0;
224 req.next = current_req;
225
226 if (current_req != NULL) {
227 current_req = &req;
228 } else {
229 current_req = &req;
230 last_req = &req;
231 }
232}
233
234
235static int macii_send_request(struct adb_request *req, int sync)
236{
237 int err;
238 unsigned long flags;
239
240 BUG_ON(request_is_queued(req));
241
242 local_irq_save(flags);
243 err = macii_write(req);
244 local_irq_restore(flags);
245
246 if (!err && sync) {
247 while (!req->complete) {
248 macii_poll();
249 }
250 BUG_ON(request_is_queued(req));
251 }
252
253 return err;
254}
255
256
257static int macii_write(struct adb_request *req)
258{
259 if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) {
260 req->complete = 1;
261 return -EINVAL;
262 }
263
264 req->next = NULL;
265 req->sent = 0;
266 req->complete = 0;
267 req->reply_len = 0;
268
269 if (current_req != NULL) {
270 last_req->next = req;
271 last_req = req;
272 } else {
273 current_req = req;
274 last_req = req;
275 if (macii_state == idle) macii_start();
276 }
277 return 0;
278}
279
280
281static int macii_autopoll(int devs)
282{
283 static struct adb_request req;
284 unsigned long flags;
285 int err = 0;
286
287
288 autopoll_devs = devs & 0xFFFE;
289
290 if (!autopoll_devs) return 0;
291
292 local_irq_save(flags);
293
294 if (current_req == NULL) {
295
296
297
298 adb_request(&req, NULL, ADBREQ_NOSEND, 1,
299 ADB_READREG(ffs(autopoll_devs) - 1, 0));
300 err = macii_write(&req);
301 }
302
303 local_irq_restore(flags);
304 return err;
305}
306
307static inline int need_autopoll(void) {
308
309
310
311 if ((command_byte & 0x0F) == 0x0C &&
312 ((1 << ((command_byte & 0xF0) >> 4)) & autopoll_devs))
313 return 0;
314 return 1;
315}
316
317
318static void macii_poll(void)
319{
320 disable_irq(IRQ_MAC_ADB);
321 macii_interrupt(0, NULL);
322 enable_irq(IRQ_MAC_ADB);
323}
324
325
326static int macii_reset_bus(void)
327{
328 static struct adb_request req;
329
330 if (request_is_queued(&req))
331 return 0;
332
333
334 adb_request(&req, NULL, 0, 1, ADB_BUSRESET);
335
336
337 udelay(3000);
338
339 return 0;
340}
341
342
343static void macii_start(void)
344{
345 struct adb_request *req;
346
347 req = current_req;
348
349 BUG_ON(req == NULL);
350
351 BUG_ON(macii_state != idle);
352
353
354
355
356
357
358
359 command_byte = req->data[1];
360
361 via[ACR] |= SR_OUT;
362
363 via[SR] = req->data[1];
364
365 via[B] = (via[B] & ~ST_MASK) | ST_CMD;
366
367 macii_state = sending;
368 data_index = 2;
369}
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387static irqreturn_t macii_interrupt(int irq, void *arg)
388{
389 int x;
390 static int entered;
391 struct adb_request *req;
392
393 if (!arg) {
394
395 if (via[IFR] & SR_INT)
396 via[IFR] = SR_INT;
397 else
398 return IRQ_NONE;
399 }
400
401 BUG_ON(entered++);
402
403 last_status = status;
404 status = via[B] & (ST_MASK|CTLR_IRQ);
405
406 switch (macii_state) {
407 case idle:
408 if (reading_reply) {
409 reply_ptr = current_req->reply;
410 } else {
411 BUG_ON(current_req != NULL);
412 reply_ptr = reply_buf;
413 }
414
415 x = via[SR];
416
417 if ((status & CTLR_IRQ) && (x == 0xFF)) {
418
419
420
421 reply_len = 0;
422 srq_asserted = 0;
423 macii_state = read_done;
424 } else {
425 macii_state = reading;
426 *reply_ptr = x;
427 reply_len = 1;
428 }
429
430
431 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
432 break;
433
434 case sending:
435 req = current_req;
436 if (data_index >= req->nbytes) {
437 req->sent = 1;
438 macii_state = idle;
439
440 if (req->reply_expected) {
441 reading_reply = 1;
442 } else {
443 req->complete = 1;
444 current_req = req->next;
445 if (req->done) (*req->done)(req);
446
447 if (current_req)
448 macii_start();
449 else
450 if (need_autopoll())
451 macii_autopoll(autopoll_devs);
452 }
453
454 if (macii_state == idle) {
455
456 via[ACR] &= ~SR_OUT;
457 x = via[SR];
458
459 via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
460 }
461 } else {
462 via[SR] = req->data[data_index++];
463
464 if ( (via[B] & ST_MASK) == ST_CMD ) {
465
466 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
467 } else {
468
469 via[B] ^= ST_MASK;
470 }
471 }
472 break;
473
474 case reading:
475 x = via[SR];
476 BUG_ON((status & ST_MASK) == ST_CMD ||
477 (status & ST_MASK) == ST_IDLE);
478
479
480
481
482
483
484
485
486
487
488
489 srq_asserted = 0;
490 if (!(status & CTLR_IRQ)) {
491 if (x == 0xFF) {
492 if (!(last_status & CTLR_IRQ)) {
493 macii_state = read_done;
494 reply_len = 0;
495 srq_asserted = 1;
496 }
497 } else if (x == 0x00) {
498 macii_state = read_done;
499 if (!(last_status & CTLR_IRQ))
500 srq_asserted = 1;
501 }
502 }
503
504 if (macii_state == reading) {
505 BUG_ON(reply_len > 15);
506 reply_ptr++;
507 *reply_ptr = x;
508 reply_len++;
509 }
510
511
512 via[B] ^= ST_MASK;
513 break;
514
515 case read_done:
516 x = via[SR];
517
518 if (reading_reply) {
519 reading_reply = 0;
520 req = current_req;
521 req->reply_len = reply_len;
522 req->complete = 1;
523 current_req = req->next;
524 if (req->done) (*req->done)(req);
525 } else if (reply_len && autopoll_devs)
526 adb_input(reply_buf, reply_len, 0);
527
528 macii_state = idle;
529
530
531 if (srq_asserted)
532 macii_queue_poll();
533
534 if (current_req)
535 macii_start();
536 else
537 if (need_autopoll())
538 macii_autopoll(autopoll_devs);
539
540 if (macii_state == idle)
541 via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
542 break;
543
544 default:
545 break;
546 }
547
548 entered--;
549 return IRQ_HANDLED;
550}
551