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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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