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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [net/] [sunlance.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/* lance.c: Linux/Sparc/Lance driver */
2
/*
3
        Written 1995, 1996 by Miguel de Icaza
4
  Sources:
5
        The Linux  depca driver
6
        The Linux  lance driver.
7
        The Linux  skeleton driver.
8
        The NetBSD Sparc/Lance driver.
9
        Theo de Raadt (deraadt@openbsd.org)
10
        NCR92C990 Lan Controller manual
11
 
12
1.4:
13
        Added support to run with a ledma on the Sun4m
14
1.5:
15
        Added multiple card detection.
16
 
17
        4/17/97: Burst sizes and tpe selection on sun4m by Christian Dost
18
                 (ecd@pool.informatik.rwth-aachen.de)
19
*/
20
#undef DEBUG_DRIVER
21
static char *version =
22
        "sunlance.c:v1.6 19/Apr/96 Miguel de Icaza (miguel@nuclecu.unam.mx)\n";
23
 
24
static char *lancestr = "LANCE";
25
static char *lancedma = "LANCE DMA";
26
 
27
#include <linux/kernel.h>
28
#include <linux/sched.h>
29
#include <linux/types.h>
30
#include <linux/fcntl.h>
31
#include <linux/interrupt.h>
32
#include <linux/ptrace.h>
33
#include <linux/ioport.h>
34
#include <linux/in.h>
35
#include <linux/malloc.h>
36
#include <linux/string.h>
37
#include <asm/system.h>
38
#include <asm/bitops.h>
39
#include <asm/io.h>
40
#include <asm/dma.h>
41
#include <linux/errno.h>
42
#include <asm/byteorder.h>      /* Used by the checksum routines */
43
 
44
/* Used for the temporal inet entries and routing */
45
#include <linux/socket.h>
46
#include <linux/route.h>
47
 
48
#include <asm/idprom.h>
49
#include <asm/sbus.h>
50
#include <asm/openprom.h>
51
#include <asm/oplib.h>
52
 
53
#include <linux/netdevice.h>
54
#include <linux/etherdevice.h>
55
#include <linux/skbuff.h>
56
 
57
/* Define: 2^4 Tx buffers and 2^4 Rx buffers */
58
#ifndef LANCE_LOG_TX_BUFFERS
59
#define LANCE_LOG_TX_BUFFERS 4
60
#define LANCE_LOG_RX_BUFFERS 4
61
#endif
62
 
63
#define LE_CSR0 0
64
#define LE_CSR1 1
65
#define LE_CSR2 2
66
#define LE_CSR3 3
67
 
68
#define LE_MO_PROM      0x8000  /* Enable promiscuous mode */
69
 
70
#define LE_C0_ERR       0x8000  /* Error: set if BAB, SQE, MISS or ME is set */
71
#define LE_C0_BABL      0x4000  /* BAB:  Babble: tx timeout. */
72
#define LE_C0_CERR      0x2000  /* SQE:  Signal quality error */
73
#define LE_C0_MISS      0x1000  /* MISS: Missed a packet */
74
#define LE_C0_MERR      0x0800  /* ME:   Memory error */
75
#define LE_C0_RINT      0x0400  /* Received interrupt */
76
#define LE_C0_TINT      0x0200  /* Transmitter Interrupt */
77
#define LE_C0_IDON      0x0100  /* IFIN: Init finished. */
78
#define LE_C0_INTR      0x0080  /* Interrupt or error */
79
#define LE_C0_INEA      0x0040  /* Interrupt enable */
80
#define LE_C0_RXON      0x0020  /* Receiver on */
81
#define LE_C0_TXON      0x0010  /* Transmitter on */
82
#define LE_C0_TDMD      0x0008  /* Transmitter demand */
83
#define LE_C0_STOP      0x0004  /* Stop the card */
84
#define LE_C0_STRT      0x0002  /* Start the card */
85
#define LE_C0_INIT      0x0001  /* Init the card */
86
 
87
#define LE_C3_BSWP      0x4     /* SWAP */
88
#define LE_C3_ACON      0x2     /* ALE Control */
89
#define LE_C3_BCON      0x1     /* Byte control */
90
 
91
/* Receive message descriptor 1 */
92
#define LE_R1_OWN       0x80    /* Who owns the entry */
93
#define LE_R1_ERR       0x40    /* Error: if FRA, OFL, CRC or BUF is set */
94
#define LE_R1_FRA       0x20    /* FRA: Frame error */
95
#define LE_R1_OFL       0x10    /* OFL: Frame overflow */
96
#define LE_R1_CRC       0x08    /* CRC error */
97
#define LE_R1_BUF       0x04    /* BUF: Buffer error */
98
#define LE_R1_SOP       0x02    /* Start of packet */
99
#define LE_R1_EOP       0x01    /* End of packet */
100
#define LE_R1_POK       0x03    /* Packet is complete: SOP + EOP */
101
 
102
#define LE_T1_OWN       0x80    /* Lance owns the packet */
103
#define LE_T1_ERR       0x40    /* Error summary */
104
#define LE_T1_EMORE     0x10    /* Error: more than one retry needed */
105
#define LE_T1_EONE      0x08    /* Error: one retry needed */
106
#define LE_T1_EDEF      0x04    /* Error: deferred */
107
#define LE_T1_SOP       0x02    /* Start of packet */
108
#define LE_T1_EOP       0x01    /* End of packet */
109
#define LE_T1_POK       0x03    /* Packet is complete: SOP + EOP */
110
 
111
#define LE_T3_BUF       0x8000  /* Buffer error */
112
#define LE_T3_UFL       0x4000  /* Error underflow */
113
#define LE_T3_LCOL      0x1000  /* Error late collision */
114
#define LE_T3_CLOS      0x0800  /* Error carrier loss */
115
#define LE_T3_RTY       0x0400  /* Error retry */
116
#define LE_T3_TDR       0x03ff  /* Time Domain Reflectometry counter */
117
 
118
#define TX_RING_SIZE                    (1 << (LANCE_LOG_TX_BUFFERS))
119
#define TX_RING_MOD_MASK                (TX_RING_SIZE - 1)
120
#define TX_RING_LEN_BITS                ((LANCE_LOG_TX_BUFFERS) << 29)
121
 
122
#define RX_RING_SIZE                    (1 << (LANCE_LOG_RX_BUFFERS))
123
#define RX_RING_MOD_MASK                (RX_RING_SIZE - 1)
124
#define RX_RING_LEN_BITS                ((LANCE_LOG_RX_BUFFERS) << 29)
125
 
126
#define PKT_BUF_SZ              1544
127
#define RX_BUFF_SIZE            PKT_BUF_SZ
128
#define TX_BUFF_SIZE            PKT_BUF_SZ
129
 
130
struct lance_rx_desc {
131
        unsigned short rmd0;        /* low address of packet */
132
        unsigned char  rmd1_bits;   /* descriptor bits */
133
        unsigned char  rmd1_hadr;   /* high address of packet */
134
        short    length;            /* This length is 2s complement (negative)!
135
                                     * Buffer length
136
                                     */
137
        unsigned short mblength;    /* This is the actual number of bytes received */
138
};
139
 
140
struct lance_tx_desc {
141
        unsigned short tmd0;        /* low address of packet */
142
        unsigned char  tmd1_bits;   /* descriptor bits */
143
        unsigned char  tmd1_hadr;   /* high address of packet */
144
        short length;               /* Length is 2s complement (negative)! */
145
        unsigned short misc;
146
};
147
 
148
/* The LANCE initialization block, described in databook. */
149
/* On the Sparc, this block should be on a DMA region     */
150
struct lance_init_block {
151
        unsigned short mode;            /* Pre-set mode (reg. 15) */
152
        unsigned char phys_addr[6];     /* Physical ethernet address */
153
        unsigned filter[2];             /* Multicast filter. */
154
 
155
        /* Receive and transmit ring base, along with extra bits. */
156
        unsigned short rx_ptr;          /* receive descriptor addr */
157
        unsigned short rx_len;          /* receive len and high addr */
158
        unsigned short tx_ptr;          /* transmit descriptor addr */
159
        unsigned short tx_len;          /* transmit len and high addr */
160
 
161
        /* The Tx and Rx ring entries must aligned on 8-byte boundaries. */
162
        struct lance_rx_desc brx_ring[RX_RING_SIZE];
163
        struct lance_tx_desc btx_ring[TX_RING_SIZE];
164
 
165
        char   rx_buf [RX_RING_SIZE][RX_BUFF_SIZE];
166
        char   tx_buf [TX_RING_SIZE][TX_BUFF_SIZE];
167
};
168
 
169
struct lance_private {
170
        char *name;
171
        volatile struct lance_regs *ll;
172
        volatile struct lance_init_block *init_block;
173
 
174
        int rx_new, tx_new;
175
        int rx_old, tx_old;
176
 
177
        struct enet_statistics stats;
178
        struct Linux_SBus_DMA *ledma; /* if set this points to ledma and arch=4m */
179
 
180
        int tpe;                      /* cable-selection is TPE */
181
        int burst_sizes;              /* ledma SBus burst sizes */
182
};
183
 
184
#define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
185
                        lp->tx_old+TX_RING_MOD_MASK-lp->tx_new:\
186
                        lp->tx_old - lp->tx_new-1)
187
 
188
/* On the sparc, the lance control ports are memory mapped */
189
struct lance_regs {
190
        unsigned short rdp;                     /* register data port */
191
        unsigned short rap;                     /* register address port */
192
};
193
 
194
int sparc_lance_debug = 2;
195
 
196
/* The Lance uses 24 bit addresses */
197
/* On the Sun4c the DVMA will provide the remaining bytes for us */
198
/* On the Sun4m we have to instruct the ledma to provide them    */
199
#define LANCE_ADDR(x) ((int)(x) & ~0xff000000)
200
 
201
/* Load the CSR registers */
202
static void load_csrs (struct lance_private *lp)
203
{
204
        volatile struct lance_regs *ll = lp->ll;
205
        volatile struct lance_init_block *ib = lp->init_block;
206
        int leptr;
207
 
208
        leptr = LANCE_ADDR (ib);
209
        ll->rap = LE_CSR1;
210
        ll->rdp = (leptr & 0xFFFF);
211
        ll->rap = LE_CSR2;
212
        ll->rdp = leptr >> 16;
213
        ll->rap = LE_CSR3;
214
        ll->rdp = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
215
 
216
        /* Point back to csr0 */
217
        ll->rap = LE_CSR0;
218
}
219
 
220
#define ZERO 0
221
 
222
/* Setup the Lance Rx and Tx rings */
223
/* Sets dev->tbusy */
224
static void lance_init_ring (struct device *dev)
225
{
226
        struct lance_private *lp = (struct lance_private *) dev->priv;
227
        volatile struct lance_init_block *ib = lp->init_block;
228
        int leptr;
229
        int i;
230
 
231
        /* Lock out other processes while setting up hardware */
232
        dev->tbusy = 1;
233
        lp->rx_new = lp->tx_new = 0;
234
        lp->rx_old = lp->tx_old = 0;
235
 
236
        ib->mode = 0;
237
 
238
        /* Copy the ethernet address to the lance init block
239
         * Note that on the sparc you need to swap the ethernet address.
240
         */
241
        ib->phys_addr [0] = dev->dev_addr [1];
242
        ib->phys_addr [1] = dev->dev_addr [0];
243
        ib->phys_addr [2] = dev->dev_addr [3];
244
        ib->phys_addr [3] = dev->dev_addr [2];
245
        ib->phys_addr [4] = dev->dev_addr [5];
246
        ib->phys_addr [5] = dev->dev_addr [4];
247
 
248
        if (ZERO)
249
                printk ("TX rings:\n");
250
 
251
        /* Setup the Tx ring entries */
252
        for (i = 0; i <= TX_RING_SIZE; i++) {
253
                leptr = LANCE_ADDR(&ib->tx_buf[i][0]);
254
                ib->btx_ring [i].tmd0      = leptr;
255
                ib->btx_ring [i].tmd1_hadr = leptr >> 16;
256
                ib->btx_ring [i].tmd1_bits = 0;
257
                ib->btx_ring [i].length    = 0xf000; /* The ones required by tmd2 */
258
                ib->btx_ring [i].misc      = 0;
259
                if (i < 3)
260
                        if (ZERO) printk ("%d: 0x%8.8x\n", i, leptr);
261
        }
262
 
263
        /* Setup the Rx ring entries */
264
        if (ZERO)
265
                printk ("RX rings:\n");
266
        for (i = 0; i < RX_RING_SIZE; i++) {
267
                leptr = LANCE_ADDR(&ib->rx_buf[i][0]);
268
 
269
                ib->brx_ring [i].rmd0      = leptr;
270
                ib->brx_ring [i].rmd1_hadr = leptr >> 16;
271
                ib->brx_ring [i].rmd1_bits = LE_R1_OWN;
272
                ib->brx_ring [i].length    = -RX_BUFF_SIZE | 0xf000;
273
                ib->brx_ring [i].mblength  = 0;
274
                if (i < 3 && ZERO)
275
                        printk ("%d: 0x%8.8x\n", i, leptr);
276
        }
277
 
278
        /* Setup the initialization block */
279
 
280
        /* Setup rx descriptor pointer */
281
        leptr = LANCE_ADDR(&ib->brx_ring);
282
        ib->rx_len = (LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16);
283
        ib->rx_ptr = leptr;
284
        if (ZERO)
285
                printk ("RX ptr: %8.8x\n", leptr);
286
 
287
        /* Setup tx descriptor pointer */
288
        leptr = LANCE_ADDR(&ib->btx_ring);
289
        ib->tx_len = (LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16);
290
        ib->tx_ptr = leptr;
291
        if (ZERO)
292
                printk ("TX ptr: %8.8x\n", leptr);
293
 
294
        /* Clear the multicast filter */
295
        ib->filter [0] = 0;
296
        ib->filter [1] = 0;
297
}
298
 
299
static int init_restart_lance (struct lance_private *lp)
300
{
301
        volatile struct lance_regs *ll = lp->ll;
302
        int i;
303
 
304
        if (lp->ledma) {
305
                struct sparc_dma_registers *dregs = lp->ledma->regs;
306
                unsigned long creg;
307
 
308
                while (dregs->cond_reg & DMA_FIFO_ISDRAIN) /* E-Cache draining */
309
                        barrier();
310
 
311
                creg = dregs->cond_reg;
312
                if (lp->burst_sizes & DMA_BURST32)
313
                        creg |= DMA_E_BURST8;
314
                else
315
                        creg &= ~DMA_E_BURST8;
316
 
317
                creg |= (DMA_DSBL_RD_DRN | DMA_DSBL_WR_INV | DMA_FIFO_INV);
318
 
319
                if (lp->tpe)
320
                        creg |= DMA_EN_ENETAUI;
321
                else
322
                        creg &= ~DMA_EN_ENETAUI;
323
                udelay(20);
324
                dregs->cond_reg = creg;
325
                udelay(200);
326
        }
327
 
328
        ll->rap = LE_CSR0;
329
        ll->rdp = LE_C0_INIT;
330
 
331
        /* Wait for the lance to complete initialization */
332
        for (i = 0; (i < 100) && !(ll->rdp & (LE_C0_ERR | LE_C0_IDON)); i++)
333
                barrier();
334
        if ((i == 100) || (ll->rdp & LE_C0_ERR)) {
335
                printk ("LANCE unopened after %d ticks, csr0=%4.4x.\n", i, ll->rdp);
336
                if (lp->ledma)
337
                        printk ("dcsr=%8.8x\n",
338
                                (unsigned int) lp->ledma->regs->cond_reg);
339
                return -1;
340
        }
341
 
342
        /* Clear IDON by writing a "1", enable interrupts and start lance */
343
        ll->rdp = LE_C0_IDON;
344
        ll->rdp = LE_C0_INEA | LE_C0_STRT;
345
 
346
        if (lp->ledma)
347
                lp->ledma->regs->cond_reg |= DMA_INT_ENAB;
348
 
349
        return 0;
350
}
351
 
352
static int lance_rx (struct device *dev)
353
{
354
        struct lance_private *lp = (struct lance_private *) dev->priv;
355
        volatile struct lance_init_block *ib = lp->init_block;
356
        volatile struct lance_regs *ll = lp->ll;
357
        volatile struct lance_rx_desc *rd;
358
        unsigned char bits;
359
 
360
#ifdef TEST_HITS
361
        printk ("[");
362
        for (i = 0; i < RX_RING_SIZE; i++) {
363
                if (i == lp->rx_new)
364
                        printk ("%s",
365
                                ib->brx_ring [i].rmd1_bits & LE_R1_OWN ? "_" : "X");
366
                else
367
                        printk ("%s",
368
                                ib->brx_ring [i].rmd1_bits & LE_R1_OWN ? "." : "1");
369
        }
370
        printk ("]");
371
#endif
372
 
373
        ll->rdp = LE_C0_RINT|LE_C0_INEA;
374
        for (rd = &ib->brx_ring [lp->rx_new];
375
             !((bits = rd->rmd1_bits) & LE_R1_OWN);
376
             rd = &ib->brx_ring [lp->rx_new]) {
377
                int pkt_len;
378
                struct sk_buff *skb;
379
 
380
                /* We got an incomplete frame? */
381
                if ((bits & LE_R1_POK) != LE_R1_POK) {
382
                        lp->stats.rx_over_errors++;
383
                        lp->stats.rx_errors++;
384
                        continue;
385
                } else if (bits & LE_R1_ERR) {
386
                        /* Count only the end frame as a tx error, not the beginning */
387
                        if (bits & LE_R1_BUF) lp->stats.rx_fifo_errors++;
388
                        if (bits & LE_R1_CRC) lp->stats.rx_crc_errors++;
389
                        if (bits & LE_R1_OFL) lp->stats.rx_over_errors++;
390
                        if (bits & LE_R1_FRA) lp->stats.rx_frame_errors++;
391
                        if (bits & LE_R1_EOP) lp->stats.rx_errors++;
392
                } else {
393
                        pkt_len = rd->mblength;
394
                        skb = dev_alloc_skb (pkt_len+2);
395
                        if (skb == NULL) {
396
                                printk ("%s: Memory squeeze, deferring packet.\n",
397
                                        dev->name);
398
                                lp->stats.rx_dropped++;
399
                                rd->mblength = 0;
400
                                rd->rmd1_bits = LE_R1_OWN;
401
                                lp->rx_new = (lp->rx_new + 1) & RX_RING_MOD_MASK;
402
                                return 0;
403
                        }
404
 
405
                        skb->dev = dev;
406
                        skb_reserve (skb, 2);               /* 16 byte align */
407
                        skb_put (skb, pkt_len);             /* make room */
408
                        eth_copy_and_sum(skb,
409
                                         (unsigned char *)&(ib->rx_buf [lp->rx_new][0]),
410
                                         pkt_len, 0);
411
                        skb->protocol = eth_type_trans (skb,dev);
412
                        netif_rx (skb);
413
                        lp->stats.rx_packets++;
414
                }
415
 
416
                /* Return the packet to the pool */
417
                rd->mblength = 0;
418
                rd->rmd1_bits = LE_R1_OWN;
419
                lp->rx_new = (lp->rx_new + 1) & RX_RING_MOD_MASK;
420
        }
421
        return 0;
422
}
423
 
424
static int lance_tx (struct device *dev)
425
{
426
        struct lance_private *lp = (struct lance_private *) dev->priv;
427
        volatile struct lance_init_block *ib = lp->init_block;
428
        volatile struct lance_regs *ll = lp->ll;
429
        volatile struct lance_tx_desc *td;
430
        int i, j;
431
        int status;
432
 
433
        /* csr0 is 2f3 */
434
        ll->rdp = LE_C0_TINT | LE_C0_INEA;
435
        /* csr0 is 73 */
436
        j = lp->tx_old;
437
        for (i = 0; i < TX_RING_SIZE; i++) {
438
                td = &ib->btx_ring [j];
439
 
440
                if (td->tmd1_bits & LE_T1_ERR) {
441
                        status = td->misc;
442
 
443
                        lp->stats.tx_errors++;
444
                        if (status & LE_T3_RTY)  lp->stats.tx_aborted_errors++;
445
                        if (status & LE_T3_CLOS) lp->stats.tx_carrier_errors++;
446
                        if (status & LE_T3_LCOL) lp->stats.tx_window_errors++;
447
 
448
                        /* buffer errors and underflows turn off the transmitter */
449
                        /* Restart the adapter */
450
                        if (status & (LE_T3_BUF|LE_T3_UFL)) {
451
                                lp->stats.tx_fifo_errors++;
452
 
453
                                printk ("%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
454
                                        dev->name);
455
                                /* Stop the lance */
456
                                ll->rap = LE_CSR0;
457
                                ll->rdp = LE_C0_STOP;
458
                                lance_init_ring (dev);
459
                                load_csrs (lp);
460
                                init_restart_lance (lp);
461
                                return 0;
462
                        }
463
                } else if ((td->tmd1_bits & LE_T1_POK) == LE_T1_POK) {
464
                        /*
465
                         * So we don't count the packet more than once.
466
                         */
467
                        td->tmd1_bits &= ~(LE_T1_POK);
468
 
469
                        /* One collision before packet was sent. */
470
                        if (td->tmd1_bits & LE_T1_EONE)
471
                                lp->stats.collisions++;
472
 
473
                        /* More than one collision, be optimistic. */
474
                        if (td->tmd1_bits & LE_T1_EMORE)
475
                                lp->stats.collisions += 2;
476
 
477
                        /* What to set here? */
478
                        if (td->tmd1_bits & LE_T1_EDEF)
479
                                /* EMPTY */ ;
480
 
481
                        lp->stats.tx_packets++;
482
                }
483
 
484
                j = (j + 1) & TX_RING_MOD_MASK;
485
        }
486
        lp->tx_old = (lp->tx_old+1) & TX_RING_MOD_MASK;
487
 
488
        ll->rdp = LE_C0_TINT | LE_C0_INEA;
489
        return 0;
490
}
491
 
492
static void lance_interrupt (int irq, void *dev_id, struct pt_regs *regs)
493
{
494
        struct device *dev;
495
        struct lance_private *lp;
496
        volatile struct lance_regs *ll;
497
        int csr0;
498
 
499
#ifdef OLD_STYLE_IRQ
500
        dev = (struct device *) (irq2dev_map [irq]);
501
#else
502
        dev = (struct device *) dev_id;
503
#endif
504
 
505
        lp = (struct lance_private *) dev->priv;
506
        ll = lp->ll;
507
 
508
        if (lp->ledma) {
509
                if (lp->ledma->regs->cond_reg & DMA_HNDL_ERROR) {
510
                        printk ("%s: should reset my ledma (dmacsr=%8.8x, csr=%4.4x\n",
511
                                dev->name, (unsigned int) lp->ledma->regs->cond_reg,
512
                                ll->rdp);
513
                        printk ("send mail to miguel@nuclecu.unam.mx\n");
514
                }
515
        }
516
        if (dev->interrupt)
517
                printk ("%s: again", dev->name);
518
 
519
        dev->interrupt = 1;
520
 
521
        csr0 = ll->rdp;
522
 
523
        /* Acknowledge all the interrupt sources ASAP */
524
        ll->rdp = csr0 & 0x004f;
525
 
526
        if ((csr0 & LE_C0_ERR)) {
527
                /* Clear the error condition */
528
                ll->rdp = LE_C0_BABL|LE_C0_ERR|LE_C0_MISS|LE_C0_INEA;
529
        }
530
 
531
        if (csr0 & LE_C0_RINT)
532
                lance_rx (dev);
533
 
534
        if (csr0 & LE_C0_TINT)
535
                lance_tx (dev);
536
 
537
        if ((TX_BUFFS_AVAIL >= 0) && dev->tbusy) {
538
                dev->tbusy = 0;
539
                mark_bh (NET_BH);
540
        }
541
        ll->rap = LE_CSR0;
542
        ll->rdp = 0x7940;
543
 
544
        dev->interrupt = 0;
545
}
546
 
547
struct device *last_dev = 0;
548
 
549
static int lance_open (struct device *dev)
550
{
551
        struct lance_private *lp = (struct lance_private *)dev->priv;
552
        volatile struct lance_regs *ll = lp->ll;
553
        int status = 0;
554
 
555
        last_dev = dev;
556
 
557
        if (request_irq (dev->irq, &lance_interrupt, 0, lancestr, (void *) dev)) {
558
                printk ("Lance: Can't get irq %d\n", dev->irq);
559
                return -EAGAIN;
560
        }
561
 
562
        /* Stop the Lance */
563
        ll->rap = LE_CSR0;
564
        ll->rdp = LE_C0_STOP;
565
 
566
#ifdef OLD_STYLE_IRQ
567
        irq2dev_map [dev->irq] = dev;
568
#endif
569
 
570
        /* On the 4m, setup the ledma to provide the upper bits for buffers */
571
        if (lp->ledma)
572
                lp->ledma->regs->dma_test = ((unsigned int) lp->init_block) & 0xff000000;
573
 
574
        lance_init_ring (dev);
575
        load_csrs (lp);
576
 
577
        dev->tbusy = 0;
578
        dev->interrupt = 0;
579
        dev->start = 1;
580
 
581
        status = init_restart_lance (lp);
582
#if 0
583
        /* To emulate SunOS, we add a route to the local network */
584
        rt_add (RTF_UP,
585
                dev->pa_addr & ip_get_mask (dev->pa_addr),
586
                ip_get_mask (dev->pa_addr),
587
                0, dev, dev->mtu, 0, 0);
588
#endif
589
        return status;
590
}
591
 
592
static int lance_close (struct device *dev)
593
{
594
        struct lance_private *lp = (struct lance_private *) dev->priv;
595
        volatile struct lance_regs *ll = lp->ll;
596
 
597
        dev->start = 0;
598
        dev->tbusy = 1;
599
 
600
        /* Stop the card */
601
        ll->rap = LE_CSR0;
602
        ll->rdp = LE_C0_STOP;
603
 
604
        free_irq (dev->irq, NULL);
605
#ifdef OLD_STYLE_IRQ
606
        irq2dev_map [dev->irq] = NULL;
607
#endif
608
 
609
        return 0;
610
}
611
 
612
static inline int lance_reset (struct device *dev)
613
{
614
        struct lance_private *lp = (struct lance_private *)dev->priv;
615
        volatile struct lance_regs *ll = lp->ll;
616
        int status;
617
 
618
        /* Stop the lance */
619
        ll->rap = LE_CSR0;
620
        ll->rdp = LE_C0_STOP;
621
 
622
        /* On the 4m, reset the dma too */
623
        if (lp->ledma) {
624
                printk ("resetting ledma\n");
625
                lp->ledma->regs->cond_reg |= DMA_RST_ENET;
626
                udelay (200);
627
                lp->ledma->regs->cond_reg &= ~DMA_RST_ENET;
628
                lp->ledma->regs->dma_test = ((unsigned int) lp->init_block) & 0xff000000;
629
        }
630
        lance_init_ring (dev);
631
        load_csrs (lp);
632
        dev->trans_start = jiffies;
633
        dev->interrupt = 0;
634
        dev->start = 1;
635
        dev->tbusy = 0;
636
        status = init_restart_lance (lp);
637
#ifdef DEBUG_DRIVER
638
        printk ("Lance restart=%d\n", status);
639
#endif
640
        return status;
641
}
642
 
643
static int lance_start_xmit (struct sk_buff *skb, struct device *dev)
644
{
645
        struct lance_private *lp = (struct lance_private *)dev->priv;
646
        volatile struct lance_regs *ll = lp->ll;
647
        volatile struct lance_init_block *ib = lp->init_block;
648
        volatile unsigned long flush;
649
        int entry, skblen, len;
650
        int status = 0;
651
        static int outs;
652
 
653
        /* Transmitter timeout, serious problems */
654
        if (dev->tbusy) {
655
                int tickssofar = jiffies - dev->trans_start;
656
 
657
                if (tickssofar < 100) {
658
                        status = -1;
659
                } else {
660
                        printk ("%s: transmit timed out, status %04x, resetting\n",
661
                                dev->name, ll->rdp);
662
                        lance_reset (dev);
663
                }
664
                return status;
665
        }
666
 
667
        if (skb == NULL) {
668
                dev_tint (dev);
669
                printk ("skb is NULL\n");
670
                return 0;
671
        }
672
 
673
        if (skb->len <= 0) {
674
                printk ("skb len is %ld\n", skb->len);
675
                return 0;
676
        }
677
        /* Block a timer-based transmit from overlapping. */
678
#ifdef OLD_METHOD
679
        dev->tbusy = 1;
680
#else
681
        if (set_bit (0, (void *) &dev->tbusy) != 0) {
682
                printk ("Transmitter access conflict.\n");
683
                return -1;
684
        }
685
#endif
686
        skblen = skb->len;
687
 
688
        if (!TX_BUFFS_AVAIL)
689
                return -1;
690
 
691
#ifdef DEBUG_DRIVER
692
        /* dump the packet */
693
        {
694
                int i;
695
 
696
                for (i = 0; i < 64; i++) {
697
                        if ((i % 16) == 0)
698
                                printk ("\n");
699
                        printk ("%2.2x ", skb->data [i]);
700
                }
701
        }
702
#endif
703
        len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
704
        entry = lp->tx_new & TX_RING_MOD_MASK;
705
        ib->btx_ring [entry].length = (-len) | 0xf000;
706
        ib->btx_ring [entry].misc = 0;
707
 
708
        memcpy ((char *)&ib->tx_buf [entry][0], skb->data, skblen);
709
 
710
        /* Clear the slack of the packet, do I need this? */
711
        if (len != skblen)
712
                memset ((char *) &ib->tx_buf [entry][skblen], 0, len - skblen);
713
 
714
        /* Now, give the packet to the lance */
715
        ib->btx_ring [entry].tmd1_bits = (LE_T1_POK|LE_T1_OWN);
716
        lp->tx_new = (lp->tx_new+1) & TX_RING_MOD_MASK;
717
 
718
        outs++;
719
        /* Kick the lance: transmit now */
720
        ll->rdp = LE_C0_INEA | LE_C0_TDMD;
721
        dev->trans_start = jiffies;
722
        dev_kfree_skb (skb, FREE_WRITE);
723
 
724
        if (TX_BUFFS_AVAIL)
725
                dev->tbusy = 0;
726
 
727
        /* Read back CSR to invalidate the E-Cache.
728
         * This is needed, because DMA_DSBL_WR_INV is set. */
729
        if (lp->ledma)
730
                flush = ll->rdp;
731
 
732
        return status;
733
}
734
 
735
static struct enet_statistics *lance_get_stats (struct device *dev)
736
{
737
        struct lance_private *lp = (struct lance_private *) dev->priv;
738
 
739
        return &lp->stats;
740
}
741
 
742
static void lance_set_multicast (struct device *dev)
743
{
744
#ifdef NOT_YET
745
        struct lance_private *lp = (struct lance_private *) dev->priv;
746
        volatile struct lance_init_block *ib = lp->init_block;
747
        volatile struct lance_regs *ll = lp->ll;
748
 
749
        ll->rap = LE_CSR0;
750
        ll->rdp = LE_C0_STOP;
751
        lance_init_ring (dev);
752
        ib->mode |= LE_MO_PROM;
753
        lance_init_ring (dev);
754
        load_csrs (lp);
755
        init_restart_lance (lp);
756
        dev->tbusy = 0;
757
#endif
758
}
759
 
760
int sparc_lance_init (struct device *dev, struct linux_sbus_device *sdev,
761
                      struct Linux_SBus_DMA *ledma,
762
                      struct linux_sbus_device *lebuffer)
763
{
764
        static unsigned version_printed = 0;
765
        int    i;
766
        struct lance_private *lp;
767
        volatile struct lance_regs *ll;
768
 
769
        if (dev == NULL) {
770
                dev = init_etherdev (0, sizeof (struct lance_private));
771
        } else {
772
                dev->priv = kmalloc (sizeof (struct lance_private), GFP_KERNEL);
773
                if (dev->priv == NULL)
774
                        return -ENOMEM;
775
        }
776
        if (sparc_lance_debug && version_printed++ == 0)
777
                printk (version);
778
 
779
        printk ("%s: LANCE ", dev->name);
780
        /* Fill the dev fields */
781
        dev->base_addr = (long) sdev;
782
 
783
        /* Copy the IDPROM ethernet address to the device structure, later we
784
         * will copy the address in the device structure to the lance initialization
785
         * block
786
         */
787
        for (i = 0; i < 6; i++)
788
                printk ("%2.2x%c",
789
                        dev->dev_addr [i] = idprom->id_eaddr [i], i == 5 ? ' ': ':');
790
        printk("\n");
791
 
792
        /* Get the IO region */
793
        prom_apply_sbus_ranges (&sdev->reg_addrs [0], sdev->num_registers);
794
        ll = sparc_alloc_io (sdev->reg_addrs [0].phys_addr, 0,
795
                             sizeof (struct lance_regs), lancestr,
796
                             sdev->reg_addrs[0].which_io, 0x0);
797
 
798
        /* Make certain the data structures used by the LANCE are aligned. */
799
        dev->priv = (void *)(((int)dev->priv + 7) & ~7);
800
        lp = (struct lance_private *) dev->priv;
801
        memset ((char *)dev->priv, 0, sizeof (struct lance_private));
802
 
803
        if (lebuffer){
804
                prom_apply_sbus_ranges (&sdev->reg_addrs [0], sdev->num_registers);
805
                lp->init_block = (void *)
806
                        sparc_alloc_io (lebuffer->reg_addrs [0].phys_addr, 0,
807
                                sizeof (struct lance_init_block), "lebuffer",
808
                                lebuffer->reg_addrs [0].which_io, 0);
809
        } else {
810
                lp->init_block = (void *)
811
                        sparc_dvma_malloc (sizeof (struct lance_init_block),
812
                                   lancedma);
813
        }
814
 
815
        lp->ll = ll;
816
        lp->name = lancestr;
817
        lp->ledma = ledma;
818
 
819
        lp->burst_sizes = 0;
820
        if (lp->ledma) {
821
                char cable_prop[4];
822
                unsigned int sbmask;
823
 
824
                /* Find burst-size property for ledma */
825
                lp->burst_sizes = prom_getintdefault(ledma->SBus_dev->prom_node,
826
                                                     "burst-sizes", 0);
827
 
828
                /* ledma may be capable of fast bursts, but sbus may not. */
829
                sbmask = prom_getintdefault(ledma->SBus_dev->my_bus->prom_node,
830
                                            "burst-sizes", DMA_BURSTBITS);
831
                lp->burst_sizes &= sbmask;
832
 
833
                /* Get the cable-selection property */
834
                prom_getstring(ledma->SBus_dev->prom_node, "cable-selection",
835
                               cable_prop, sizeof(cable_prop));
836
                if (!strcmp(cable_prop, "aui"))
837
                        lp->tpe = 0;
838
                else
839
                        lp->tpe = 1;
840
 
841
                /* Reset ledma */
842
                lp->ledma->regs->cond_reg |= DMA_RST_ENET;
843
                udelay (200);
844
                lp->ledma->regs->cond_reg &= ~DMA_RST_ENET;
845
        }
846
 
847
        /* This should never happen. */
848
        if ((int)(lp->init_block->brx_ring) & 0x07) {
849
                printk(" **ERROR** LANCE Rx and Tx rings not on even boundary.\n");
850
                return ENODEV;
851
        }
852
 
853
        dev->open = &lance_open;
854
        dev->stop = &lance_close;
855
        dev->hard_start_xmit = &lance_start_xmit;
856
        dev->get_stats = &lance_get_stats;
857
        dev->set_multicast_list = &lance_set_multicast;
858
 
859
        dev->irq = (unsigned char) sdev->irqs [0].pri;
860
        dev->dma = 0;
861
        ether_setup (dev);
862
 
863
        return 0;
864
}
865
 
866
/* On 4m, find the associated dma for the lance chip */
867
static struct Linux_SBus_DMA *
868
find_ledma (struct linux_sbus_device *dev)
869
{
870
        struct Linux_SBus_DMA *p;
871
 
872
        for (p = dma_chain; p; p = p->next)
873
                if (p->SBus_dev == dev)
874
                        return p;
875
        return 0;
876
}
877
 
878
/* Find all the lance cards on the system and initialize them */
879
int sparc_lance_probe (struct device *dev)
880
{
881
        struct linux_sbus *bus;
882
        struct linux_sbus_device *sdev = 0;
883
        struct Linux_SBus_DMA *ledma = 0;
884
        int cards = 0, v;
885
 
886
        for_each_sbus (bus) {
887
                for_each_sbusdev (sdev, bus) {
888
                        if (cards) dev = NULL;
889
                        if (strcmp (sdev->prom_name, "le") == 0) {
890
                                cards++;
891
                                if ((v = sparc_lance_init(dev, sdev, ledma,0)))
892
                                        return v;
893
                        }
894
                        if (strcmp (sdev->prom_name, "ledma") == 0) {
895
                                cards++;
896
                                ledma = find_ledma (sdev);
897
                                sdev = sdev->child;
898
                                if ((v = sparc_lance_init(dev, sdev, ledma,0)))
899
                                        return v;
900
                                break;
901
                        }
902
                        if (strcmp (sdev->prom_name, "lebuffer") == 0){
903
                                struct linux_sbus_device *le = sdev->child;
904
                                cards++;
905
                                if ((v = sparc_lance_init(dev, le, ledma,sdev)))
906
                                        return v;
907
                                break;
908
                        }
909
                } /* for each sbusdev */
910
        } /* for each sbus */
911
        if (!cards)
912
                return ENODEV;
913
        return 0;
914
}
915
 
916
/*
917
 * Local variables:
918
 *  version-control: t
919
 *  kept-new-versions: 5
920
 * End:
921
 */

powered by: WebSVN 2.1.0

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