OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [macintosh/] [via-macii.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * Device driver for the via ADB on (many) Mac II-class machines
3
 *
4
 * Based on the original ADB keyboard handler Copyright (c) 1997 Alan Cox
5
 * Also derived from code Copyright (C) 1996 Paul Mackerras.
6
 *
7
 * With various updates provided over the years by Michael Schmitz,
8
 * Guideo Koerber and others.
9
 *
10
 * Rewrite for Unified ADB by Joshua M. Thompson (funaho@jurai.org)
11
 *
12
 * 1999-08-02 (jmt) - Initial rewrite for Unified ADB.
13
 * 2000-03-29 Tony Mantler <tonym@mac.linux-m68k.org>
14
 *                              - Big overhaul, should actually work now.
15
 */
16
 
17
#include <stdarg.h>
18
#include <linux/types.h>
19
#include <linux/errno.h>
20
#include <linux/kernel.h>
21
#include <linux/delay.h>
22
#include <linux/sched.h>
23
#include <linux/adb.h>
24
#include <linux/init.h>
25
#include <asm/macintosh.h>
26
#include <asm/macints.h>
27
#include <asm/machw.h>
28
#include <asm/mac_via.h>
29
#include <asm/io.h>
30
#include <asm/system.h>
31
 
32
static volatile unsigned char *via;
33
 
34
/* VIA registers - spaced 0x200 bytes apart */
35
#define RS              0x200           /* skip between registers */
36
#define B               0                /* B-side data */
37
#define A               RS              /* A-side data */
38
#define DIRB            (2*RS)          /* B-side direction (1=output) */
39
#define DIRA            (3*RS)          /* A-side direction (1=output) */
40
#define T1CL            (4*RS)          /* Timer 1 ctr/latch (low 8 bits) */
41
#define T1CH            (5*RS)          /* Timer 1 counter (high 8 bits) */
42
#define T1LL            (6*RS)          /* Timer 1 latch (low 8 bits) */
43
#define T1LH            (7*RS)          /* Timer 1 latch (high 8 bits) */
44
#define T2CL            (8*RS)          /* Timer 2 ctr/latch (low 8 bits) */
45
#define T2CH            (9*RS)          /* Timer 2 counter (high 8 bits) */
46
#define SR              (10*RS)         /* Shift register */
47
#define ACR             (11*RS)         /* Auxiliary control register */
48
#define PCR             (12*RS)         /* Peripheral control register */
49
#define IFR             (13*RS)         /* Interrupt flag register */
50
#define IER             (14*RS)         /* Interrupt enable register */
51
#define ANH             (15*RS)         /* A-side data, no handshake */
52
 
53
/* Bits in B data register: all active low */
54
#define TREQ            0x08            /* Transfer request (input) */
55
#define TACK            0x10            /* Transfer acknowledge (output) */
56
#define TIP             0x20            /* Transfer in progress (output) */
57
#define ST_MASK         0x30            /* mask for selecting ADB state bits */
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
#define SR_DATA         0x08            /* Shift register data */
69
#define SR_CLOCK        0x10            /* Shift register clock */
70
 
71
/* ADB transaction states according to GMHW */
72
#define ST_CMD          0x00            /* ADB state: command byte */
73
#define ST_EVEN         0x10            /* ADB state: even data byte */
74
#define ST_ODD          0x20            /* ADB state: odd data byte */
75
#define ST_IDLE         0x30            /* ADB state: idle, nothing to send */
76
 
77
static int  macii_init_via(void);
78
static void macii_start(void);
79
static void macii_interrupt(int irq, void *arg, struct pt_regs *regs);
80
static void macii_retransmit(int);
81
static void macii_queue_poll(void);
82
 
83
static int macii_probe(void);
84
static int macii_init(void);
85
static int macii_send_request(struct adb_request *req, int sync);
86
static int macii_write(struct adb_request *req);
87
static int macii_autopoll(int devs);
88
static void macii_poll(void);
89
static int macii_reset_bus(void);
90
 
91
struct adb_driver via_macii_driver = {
92
        "Mac II",
93
        macii_probe,
94
        macii_init,
95
        macii_send_request,
96
        macii_autopoll,
97
        macii_poll,
98
        macii_reset_bus
99
};
100
 
101
static enum macii_state {
102
        idle,
103
        sending,
104
        reading,
105
        read_done,
106
        awaiting_reply
107
} macii_state;
108
 
109
static int need_poll    = 0;
110
static int command_byte = 0;
111
static int last_reply   = 0;
112
static int last_active  = 0;
113
 
114
static struct adb_request *current_req;
115
static struct adb_request *last_req;
116
static struct adb_request *retry_req;
117
static unsigned char reply_buf[16];
118
static unsigned char *reply_ptr;
119
static int reply_len;
120
static int reading_reply;
121
static int data_index;
122
static int first_byte;
123
static int prefix_len;
124
static int status = ST_IDLE|TREQ;
125
static int last_status;
126
static int driver_running = 0;
127
 
128
/* debug level 10 required for ADB logging (should be && debug_adb, ideally) */
129
 
130
/* Check for MacII style ADB */
131
static int macii_probe(void)
132
{
133
        if (macintosh_config->adb_type != MAC_ADB_II) return -ENODEV;
134
 
135
        via = via1;
136
 
137
        printk("adb: Mac II ADB Driver v1.0 for Unified ADB\n");
138
        return 0;
139
}
140
 
141
/* Initialize the driver */
142
int macii_init(void)
143
{
144
        unsigned long flags;
145
        int err;
146
 
147
        save_flags(flags);
148
        cli();
149
 
150
        err = macii_init_via();
151
        if (err) return err;
152
 
153
        err = request_irq(IRQ_MAC_ADB, macii_interrupt, IRQ_FLG_LOCK, "ADB",
154
                    macii_interrupt);
155
        if (err) return err;
156
 
157
        macii_state = idle;
158
        restore_flags(flags);
159
        return 0;
160
}
161
 
162
/* initialize the hardware */
163
static int macii_init_via(void)
164
{
165
        unsigned char x;
166
 
167
        /* Set the lines up. We want TREQ as input TACK|TIP as output */
168
        via[DIRB] = (via[DIRB] | TACK | TIP) & ~TREQ;
169
 
170
        /* Set up state: idle */
171
        via[B] |= ST_IDLE;
172
        last_status = via[B] & (ST_MASK|TREQ);
173
 
174
        /* Shift register on input */
175
        via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;
176
 
177
        /* Wipe any pending data and int */
178
        x = via[SR];
179
 
180
        return 0;
181
}
182
 
183
/* Send an ADB poll (Talk Register 0 command, tagged on the front of the request queue) */
184
static void macii_queue_poll(void)
185
{
186
        static int device = 0;
187
        static int in_poll=0;
188
        static struct adb_request req;
189
        unsigned long flags;
190
 
191
        if (in_poll) printk("macii_queue_poll: double poll!\n");
192
 
193
        in_poll++;
194
        if (++device > 15) device = 1;
195
 
196
        adb_request(&req, NULL, ADBREQ_REPLY|ADBREQ_NOSEND, 1,
197
                    ADB_READREG(device, 0));
198
 
199
        save_flags(flags);
200
        cli();
201
 
202
        req.next = current_req;
203
        current_req = &req;
204
 
205
        restore_flags(flags);
206
        macii_start();
207
        in_poll--;
208
}
209
 
210
/* Send an ADB retransmit (Talk, appended to the request queue) */
211
static void macii_retransmit(int device)
212
{
213
        static int in_retransmit = 0;
214
        static struct adb_request rt;
215
        unsigned long flags;
216
 
217
        if (in_retransmit) printk("macii_retransmit: double retransmit!\n");
218
 
219
        in_retransmit++;
220
 
221
        adb_request(&rt, NULL, ADBREQ_REPLY|ADBREQ_NOSEND, 1,
222
                    ADB_READREG(device, 0));
223
 
224
        save_flags(flags);
225
        cli();
226
 
227
        if (current_req != NULL) {
228
                last_req->next = &rt;
229
                last_req = &rt;
230
        } else {
231
                current_req = &rt;
232
                last_req = &rt;
233
        }
234
 
235
        if (macii_state == idle) macii_start();
236
 
237
        restore_flags(flags);
238
        in_retransmit--;
239
}
240
 
241
/* Send an ADB request; if sync, poll out the reply 'till it's done */
242
static int macii_send_request(struct adb_request *req, int sync)
243
{
244
        int i;
245
 
246
        i = macii_write(req);
247
        if (i) return i;
248
 
249
        if (sync) {
250
                while (!req->complete) macii_poll();
251
        }
252
        return 0;
253
}
254
 
255
/* Send an ADB request */
256
static int macii_write(struct adb_request *req)
257
{
258
        unsigned long flags;
259
 
260
        if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) {
261
                req->complete = 1;
262
                return -EINVAL;
263
        }
264
 
265
        req->next = 0;
266
        req->sent = 0;
267
        req->complete = 0;
268
        req->reply_len = 0;
269
 
270
        save_flags(flags); cli();
271
 
272
        if (current_req != NULL) {
273
                last_req->next = req;
274
                last_req = req;
275
        } else {
276
                current_req = req;
277
                last_req = req;
278
                if (macii_state == idle) macii_start();
279
        }
280
 
281
        restore_flags(flags);
282
        return 0;
283
}
284
 
285
/* Start auto-polling */
286
static int macii_autopoll(int devs)
287
{
288
        /* Just ping a random default address */
289
        if (!(current_req || retry_req))
290
                macii_retransmit( (last_active < 16 && last_active > 0) ? last_active : 3);
291
        return 0;
292
}
293
 
294
/* Prod the chip without interrupts */
295
static void macii_poll(void)
296
{
297
        unsigned long flags;
298
 
299
        save_flags(flags); cli();
300
        if (via[IFR] & SR_INT) macii_interrupt(0, 0, 0);
301
        restore_flags(flags);
302
}
303
 
304
/* Reset the bus */
305
static int macii_reset_bus(void)
306
{
307
        static struct adb_request req;
308
 
309
        /* Command = 0, Address = ignored */
310
        adb_request(&req, NULL, 0, 1, ADB_BUSRESET);
311
 
312
        return 0;
313
}
314
 
315
/* Start sending ADB packet */
316
static void macii_start(void)
317
{
318
        unsigned long flags;
319
        struct adb_request *req;
320
 
321
        req = current_req;
322
        if (!req) return;
323
 
324
        /* assert macii_state == idle */
325
        if (macii_state != idle) {
326
                printk("macii_start: called while driver busy (%p %x %x)!\n",
327
                        req, macii_state, (uint) via1[B] & (ST_MASK|TREQ));
328
                return;
329
        }
330
 
331
        save_flags(flags); cli();
332
 
333
        /*
334
         * IRQ signaled ?? (means ADB controller wants to send, or might
335
         * be end of packet if we were reading)
336
         */
337
#if 0 /* FIXME: This is broke broke broke, for some reason */
338
        if ((via[B] & TREQ) == 0) {
339
                printk("macii_start: weird poll stuff. huh?\n");
340
                /*
341
                 *      FIXME - we need to restart this on a timer
342
                 *      or a collision at boot hangs us.
343
                 *      Never set macii_state to idle here, or macii_start
344
                 *      won't be called again from send_request!
345
                 *      (need to re-check other cases ...)
346
                 */
347
                /*
348
                 * if the interrupt handler set the need_poll
349
                 * flag, it's hopefully a SRQ poll or re-Talk
350
                 * so we try to send here anyway
351
                 */
352
                if (!need_poll) {
353
                        if (console_loglevel == 10)
354
                                printk("macii_start: device busy - retry %p state %d status %x!\n",
355
                                        req, macii_state,
356
                                        (uint) via[B] & (ST_MASK|TREQ));
357
                        retry_req = req;
358
                        /* set ADB status here ? */
359
                        restore_flags(flags);
360
                        return;
361
                } else {
362
                        need_poll = 0;
363
                }
364
        }
365
#endif
366
        /*
367
         * Another retry pending? (sanity check)
368
         */
369
        if (retry_req) {
370
                retry_req = NULL;
371
        }
372
 
373
        /* Now send it. Be careful though, that first byte of the request */
374
        /* is actually ADB_PACKET; the real data begins at index 1!       */
375
 
376
        /* store command byte */
377
        command_byte = req->data[1];
378
        /* Output mode */
379
        via[ACR] |= SR_OUT;
380
        /* Load data */
381
        via[SR] = req->data[1];
382
        /* set ADB state to 'command' */
383
        via[B] = (via[B] & ~ST_MASK) | ST_CMD;
384
 
385
        macii_state = sending;
386
        data_index = 2;
387
 
388
        restore_flags(flags);
389
}
390
 
391
/*
392
 * The notorious ADB interrupt handler - does all of the protocol handling,
393
 * except for starting new send operations. Relies heavily on the ADB
394
 * controller sending and receiving data, thereby generating SR interrupts
395
 * for us. This means there has to be always activity on the ADB bus, otherwise
396
 * the whole process dies and has to be re-kicked by sending TALK requests ...
397
 * CUDA-based Macs seem to solve this with the autopoll option, for MacII-type
398
 * ADB the problem isn't solved yet (retransmit of the latest active TALK seems
399
 * a good choice; either on timeout or on a timer interrupt).
400
 *
401
 * The basic ADB state machine was left unchanged from the original MacII code
402
 * by Alan Cox, which was based on the CUDA driver for PowerMac.
403
 * The syntax of the ADB status lines seems to be totally different on MacII,
404
 * though. MacII uses the states Command -> Even -> Odd -> Even ->...-> Idle for
405
 * sending, and Idle -> Even -> Odd -> Even ->...-> Idle for receiving. Start
406
 * and end of a receive packet are signaled by asserting /IRQ on the interrupt
407
 * line. Timeouts are signaled by a sequence of 4 0xFF, with /IRQ asserted on
408
 * every other byte. SRQ is probably signaled by 3 or more 0xFF tacked on the
409
 * end of a packet. (Thanks to Guido Koerber for eavesdropping on the ADB
410
 * protocol with a logic analyzer!!)
411
 *
412
 * Note: As of 21/10/97, the MacII ADB part works including timeout detection
413
 * and retransmit (Talk to the last active device).
414
 */
415
void macii_interrupt(int irq, void *arg, struct pt_regs *regs)
416
{
417
        int x, adbdir;
418
        unsigned long flags;
419
        struct adb_request *req;
420
 
421
        last_status = status;
422
 
423
        /* prevent races due to SCSI enabling ints */
424
        save_flags(flags); cli();
425
 
426
        if (driver_running) {
427
                restore_flags(flags);
428
                return;
429
        }
430
 
431
        driver_running = 1;
432
 
433
        status = via[B] & (ST_MASK|TREQ);
434
        adbdir = via[ACR] & SR_OUT;
435
 
436
        switch (macii_state) {
437
                case idle:
438
                        x = via[SR];
439
                        first_byte = x;
440
                        /* set ADB state = even for first data byte */
441
                        via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
442
 
443
                        reply_buf[0] = first_byte; /* was command_byte?? */
444
                        reply_ptr = reply_buf + 1;
445
                        reply_len = 1;
446
                        prefix_len = 1;
447
                        reading_reply = 0;
448
 
449
                        macii_state = reading;
450
                        break;
451
 
452
                case awaiting_reply:
453
                        /* handshake etc. for II ?? */
454
                        x = via[SR];
455
                        first_byte = x;
456
                        /* set ADB state = even for first data byte */
457
                        via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
458
 
459
                        current_req->reply[0] = first_byte;
460
                        reply_ptr = current_req->reply + 1;
461
                        reply_len = 1;
462
                        prefix_len = 1;
463
                        reading_reply = 1;
464
 
465
                        macii_state = reading;
466
                        break;
467
 
468
                case sending:
469
                        req = current_req;
470
                        if (data_index >= req->nbytes) {
471
                                /* print an error message if a listen command has no data */
472
                                if (((command_byte & 0x0C) == 0x08)
473
                                 /* && (console_loglevel == 10) */
474
                                    && (data_index == 2))
475
                                        printk("MacII ADB: listen command with no data: %x!\n",
476
                                                command_byte);
477
                                /* reset to shift in */
478
                                via[ACR] &= ~SR_OUT;
479
                                x = via[SR];
480
                                /* set ADB state idle - might get SRQ */
481
                                via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
482
 
483
                                req->sent = 1;
484
 
485
                                if (req->reply_expected) {
486
                                        macii_state = awaiting_reply;
487
                                } else {
488
                                        req->complete = 1;
489
                                        current_req = req->next;
490
                                        if (req->done) (*req->done)(req);
491
                                        macii_state = idle;
492
                                        if (current_req || retry_req)
493
                                                macii_start();
494
                                        else
495
                                                macii_retransmit((command_byte & 0xF0) >> 4);
496
                                }
497
                        } else {
498
                                via[SR] = req->data[data_index++];
499
 
500
                                if ( (via[B] & ST_MASK) == ST_CMD ) {
501
                                        /* just sent the command byte, set to EVEN */
502
                                        via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
503
                                } else {
504
                                        /* invert state bits, toggle ODD/EVEN */
505
                                        via[B] ^= ST_MASK;
506
                                }
507
                        }
508
                        break;
509
 
510
                case reading:
511
 
512
                        /* timeout / SRQ handling for II hw */
513
                        if( (first_byte == 0xFF && (reply_len-prefix_len)==2
514
                             && memcmp(reply_ptr-2,"\xFF\xFF",2)==0) ||
515
                            ((reply_len-prefix_len)==3
516
                             && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0))
517
                        {
518
                                /*
519
                                 * possible timeout (in fact, most probably a
520
                                 * timeout, since SRQ can't be signaled without
521
                                 * transfer on the bus).
522
                                 * The last three bytes seen were FF, together
523
                                 * with the starting byte (in case we started
524
                                 * on 'idle' or 'awaiting_reply') this probably
525
                                 * makes four. So this is mostl likely #5!
526
                                 * The timeout signal is a pattern 1 0 1 0 0..
527
                                 * on /INT, meaning we missed it :-(
528
                                 */
529
                                x = via[SR];
530
                                if (x != 0xFF) printk("MacII ADB: mistaken timeout/SRQ!\n");
531
 
532
                                if ((status & TREQ) == (last_status & TREQ)) {
533
                                        /* Not a timeout. Unsolicited SRQ? weird. */
534
                                        /* Terminate the SRQ packet and poll */
535
                                        need_poll = 1;
536
                                }
537
                                /* There's no packet to get, so reply is blank */
538
                                via[B] ^= ST_MASK;
539
                                reply_ptr -= (reply_len-prefix_len);
540
                                reply_len = prefix_len;
541
                                macii_state = read_done;
542
                                break;
543
                        } /* end timeout / SRQ handling for II hw. */
544
 
545
                        if((reply_len-prefix_len)>3
546
                                && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0)
547
                        {
548
                                /* SRQ tacked on data packet */
549
                                /* Terminate the packet (SRQ never ends) */
550
                                x = via[SR];
551
                                macii_state = read_done;
552
                                reply_len -= 3;
553
                                reply_ptr -= 3;
554
                                need_poll = 1;
555
                                /* need to continue; next byte not seen else */
556
                        } else {
557
                                /* Sanity check */
558
                                if (reply_len > 15) reply_len = 0;
559
                                /* read byte */
560
                                x = via[SR];
561
                                *reply_ptr = x;
562
                                reply_ptr++;
563
                                reply_len++;
564
                        }
565
                        /* The usual handshake ... */
566
 
567
                        /*
568
                         * NetBSD hints that the next to last byte
569
                         * is sent with IRQ !!
570
                         * Guido found out it's the last one (0x0),
571
                         * but IRQ should be asserted already.
572
                         * Problem with timeout detection: First
573
                         * transition to /IRQ might be second
574
                         * byte of timeout packet!
575
                         * Timeouts are signaled by 4x FF.
576
                         */
577
                        if (((status & TREQ) == 0) && (x == 0x00)) { /* != 0xFF */
578
                                /* invert state bits, toggle ODD/EVEN */
579
                                via[B] ^= ST_MASK;
580
 
581
                                /* adjust packet length */
582
                                reply_len--;
583
                                reply_ptr--;
584
                                macii_state = read_done;
585
                        } else {
586
                                /* not caught: ST_CMD */
587
                                /* required for re-entry 'reading'! */
588
                                if ((status & ST_MASK) == ST_IDLE) {
589
                                        /* (in)sanity check - set even */
590
                                        via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
591
                                } else {
592
                                        /* invert state bits */
593
                                        via[B] ^= ST_MASK;
594
                                }
595
                        }
596
                        break;
597
 
598
                case read_done:
599
                        x = via[SR];
600
                        if (reading_reply) {
601
                                req = current_req;
602
                                req->reply_len = reply_ptr - req->reply;
603
                                req->complete = 1;
604
                                current_req = req->next;
605
                                if (req->done) (*req->done)(req);
606
                        } else {
607
                                adb_input(reply_buf, reply_ptr - reply_buf,
608
                                          regs, 0);
609
                        }
610
 
611
                        /*
612
                         * remember this device ID; it's the latest we got a
613
                         * reply from!
614
                         */
615
                        last_reply = command_byte;
616
                        last_active = (command_byte & 0xF0) >> 4;
617
 
618
                        /* SRQ seen before, initiate poll now */
619
                        if (need_poll) {
620
                                macii_state = idle;
621
                                macii_queue_poll();
622
                                need_poll = 0;
623
                                break;
624
                        }
625
 
626
                        /* set ADB state to idle */
627
                        via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
628
 
629
                        /* /IRQ seen, so the ADB controller has data for us */
630
                        if ((via[B] & TREQ) != 0) {
631
                                macii_state = reading;
632
 
633
                                reply_buf[0] = command_byte;
634
                                reply_ptr = reply_buf + 1;
635
                                reply_len = 1;
636
                                prefix_len = 1;
637
                                reading_reply = 0;
638
                        } else {
639
                                /* no IRQ, send next packet or wait */
640
                                macii_state = idle;
641
                                if (current_req)
642
                                        macii_start();
643
                                else
644
                                        macii_retransmit(last_active);
645
                        }
646
                        break;
647
 
648
                default:
649
                break;
650
        }
651
        /* reset mutex and interrupts */
652
        driver_running = 0;
653
        restore_flags(flags);
654
}

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.