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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/* cs89x0.c: A Crystal Semiconductor CS89[02]0 driver for linux. */
2
/*
3
        Written 1996 by Russell Nelson, with reference to skeleton.c
4
        written 1993-1994 by Donald Becker.
5
 
6
        This software may be used and distributed according to the terms
7
        of the GNU Public License, incorporated herein by reference.
8
 
9
        The author may be reached at nelson@crynwr.com, Crynwr
10
        Software, 11 Grant St., Potsdam, NY 13676
11
 
12
  Changelog:
13
 
14
  Mike Cruse        : mcruse@cti-ltd.com
15
                    : Changes for Linux 2.0 compatibility.
16
                    : Added dev_id parameter in net_interrupt(),
17
                    : request_irq() and free_irq(). Just NULL for now.
18
 
19
  Mike Cruse        : Added MOD_INC_USE_COUNT and MOD_DEC_USE_COUNT macros
20
                    : in net_open() and net_close() so kerneld would know
21
                    : that the module is in use and wouldn't eject the
22
                    : driver prematurely.
23
 
24
  Mike Cruse        : Rewrote init_module() and cleanup_module using 8390.c
25
                    : as an example. Disabled autoprobing in init_module(),
26
                    : not a good thing to do to other devices while Linux
27
                    : is running from all accounts.
28
 
29
  Alan Cox          : Removed 1.2 support, added 2.1 extra counters.
30
*/
31
 
32
static char *version =
33
"cs89x0.c:v1.02 11/26/96 Russell Nelson <nelson@crynwr.com>\n"
34
"etherc.c:v1.00 19/02/99 Ben Dooks <ben@fluff.org>\n";
35
 
36
/* ======================= configure the driver here ======================= */
37
 
38
/* use 0 for production, 1 for verification, >2 for debug */
39
#ifndef NET_DEBUG
40
#define NET_DEBUG 1
41
#endif
42
 
43
/* ======================= end of configuration ======================= */
44
 
45
 
46
/* Always include 'config.h' first in case the user wants to turn on
47
   or override something. */
48
#ifdef MODULE
49
#include <linux/module.h>
50
#include <linux/version.h>
51
#else
52
#define MOD_INC_USE_COUNT
53
#define MOD_DEC_USE_COUNT
54
#endif
55
 
56
#define PRINTK(x) printk x
57
 
58
/*
59
  Sources:
60
 
61
        Crynwr packet driver epktisa.
62
 
63
        Crystal Semiconductor data sheets.
64
 
65
*/
66
 
67
#include <linux/kernel.h>
68
#include <linux/sched.h>
69
#include <linux/types.h>
70
#include <linux/fcntl.h>
71
#include <linux/interrupt.h>
72
#include <linux/ptrace.h>
73
#include <linux/ioport.h>
74
#include <linux/in.h>
75
#include <linux/malloc.h>
76
#include <linux/string.h>
77
#include <asm/system.h>
78
#include <asm/bitops.h>
79
#include <asm/io.h>
80
#include <linux/errno.h>
81
#if KERNEL_VERSION >= 0x020100
82
#include <linux/init.h>
83
#else
84
#define __initfunc(x) x
85
#endif
86
 
87
#include <asm/delay.h>
88
#include <asm/ecard.h>
89
 
90
#include <linux/netdevice.h>
91
#include <linux/etherdevice.h>
92
#include <linux/skbuff.h>
93
#include "etherc.h"
94
 
95
/* First, a few definitions that the brave might change. */
96
/* A zero-terminated list of I/O addresses to be probed. */
97
 
98
static const card_ids cids[] = { {0x1f,0x2401}, { 0xffff, 0xffff} };
99
 
100
static unsigned int net_debug = NET_DEBUG;
101
 
102
/* The number of low I/O ports used by the ethercard. */
103
#define NETCARD_IO_EXTENT       16
104
 
105
/* Information that need to be kept for each board. */
106
struct net_local {
107
        struct enet_statistics stats;
108
        struct expansion_card *ec;
109
        int chip_type;          /* one of: CS8900, CS8920, CS8920M */
110
        char chip_revision;     /* revision letter of the chip ('A'...) */
111
        int send_cmd;           /* the propercommand used to send a packet. */
112
        int auto_neg_cnf;
113
        int adapter_cnf;
114
        int isa_config;
115
        int irq_map;
116
        int rx_mode;
117
        int curr_rx_cfg;
118
        int linectl;
119
        int send_underrun;      /* keep track of how many underruns in a row we get */
120
        struct sk_buff *skb;
121
};
122
 
123
/* Index to functions, as function prototypes. */
124
 
125
extern int etherc_probe(struct device *dev);
126
 
127
static int cs89x0_probe1(struct device *dev, int ioaddr,
128
                         struct expansion_card *ec);
129
static int net_open(struct device *dev);
130
static int      net_send_packet(struct sk_buff *skb, struct device *dev);
131
static void net_interrupt(int irq, void *dev_id, struct pt_regs *regs);
132
static void set_multicast_list(struct device *dev);
133
static void net_rx(struct device *dev);
134
static int net_close(struct device *dev);
135
static struct enet_statistics *net_get_stats(struct device *dev);
136
static void reset_chip(struct device *dev);
137
static int get_eeprom_data(struct device *dev, int off, int len, int *buffer);
138
static int get_eeprom_cksum(int off, int len, int *buffer);
139
static int set_mac_address(struct device *dev, void *addr);
140
 
141
 
142
/* Example routines you must write ;->. */
143
#define tx_done(dev) 1
144
 
145
 
146
/* Check for a network adaptor of this type, and return '0' iff one exists.
147
   If dev->base_addr == 0, probe all likely locations.
148
   If dev->base_addr == 1, always return failure.
149
   If dev->base_addr == 2, allocate space for the device and return success
150
   (detachable devices only).
151
   */
152
 
153
__initfunc(int
154
etherc_probe(struct device *dev))
155
{
156
        unsigned long ioaddr;
157
        struct expansion_card *ec;
158
 
159
        ecard_startfind();
160
 
161
        do {
162
          if ((ec = ecard_find(0, cids)) == NULL)
163
            break;
164
 
165
          ioaddr = ecard_address(ec, ECARD_EASI, 0);
166
          dev->irq = ec->irq;
167
 
168
          if (cs89x0_probe1(dev, ioaddr, ec) == 0)
169
            return 0;
170
        } while(0==0);
171
 
172
        return ENODEV;
173
}
174
 
175
 
176
static int inline
177
readreg(struct device *dev, int portno)
178
{
179
        outw(portno, dev->base_addr + (ADD_PORT << ECARD_SHIFT));
180
        return inw(dev->base_addr + (DATA_PORT << ECARD_SHIFT));
181
}
182
 
183
static void inline
184
writereg(struct device *dev, int portno, int value)
185
{
186
        outw(portno, dev->base_addr + (ADD_PORT << ECARD_SHIFT));
187
        outw(value, dev->base_addr + (DATA_PORT << ECARD_SHIFT));
188
}
189
 
190
 
191
static int inline
192
readword(struct device *dev, int portno)
193
{
194
        return inw(dev->base_addr + (portno << ECARD_SHIFT));
195
}
196
 
197
static void inline
198
writeword(struct device *dev, int portno, int value)
199
{
200
        outw(value, dev->base_addr + (portno << ECARD_SHIFT));
201
}
202
 
203
__initfunc(static int
204
wait_eeprom_ready(struct device *dev))
205
{
206
        int timeout = jiffies;
207
        /* check to see if the EEPROM is ready, a timeout is used -
208
           just in case EEPROM is ready when SI_BUSY in the
209
           PP_SelfST is clear */
210
        while(readreg(dev, PP_SelfST) & SI_BUSY)
211
                if (jiffies - timeout >= 40)
212
                        return -1;
213
        return 0;
214
}
215
 
216
__initfunc(static int
217
get_eeprom_data(struct device *dev, int off, int len, int *buffer))
218
{
219
        int i;
220
 
221
        if (net_debug > 3) printk("EEPROM data from %x for %x:\n",off,len);
222
        for (i = 0; i < len; i++) {
223
                if (wait_eeprom_ready(dev) < 0) return -1;
224
                /* Now send the EEPROM read command and EEPROM location to read */
225
                writereg(dev, PP_EECMD, (off + i) | EEPROM_READ_CMD);
226
                if (wait_eeprom_ready(dev) < 0) return -1;
227
                buffer[i] = readreg(dev, PP_EEData);
228
                if (net_debug > 3) printk("%04x ", buffer[i]);
229
        }
230
        if (net_debug > 3) printk("\n");
231
        return 0;
232
}
233
 
234
__initfunc(static int
235
get_eeprom_cksum(int off, int len, int *buffer))
236
{
237
        int i, cksum;
238
 
239
        cksum = 0;
240
        for (i = 0; i < len; i++)
241
                cksum += buffer[i];
242
        cksum &= 0xffff;
243
        if (cksum == 0)
244
                return 0;
245
        return -1;
246
}
247
 
248
/* This is the real probe routine.  Linux has a history of friendly device
249
   probes on the ISA bus.  A good device probes avoids doing writes, and
250
   verifies that the correct device exists and functions.  */
251
 
252
__initfunc(static int cs89x0_probe1(struct device *dev, int io_addr, struct expansion_card *ec))
253
{
254
        struct net_local *lp;
255
        static unsigned version_printed = 0;
256
        int i;
257
        unsigned rev_type = 0;
258
        int eeprom_buff[CHKSUM_LEN];
259
 
260
        io_addr += (0x400600 >> 2);
261
        if (check_region(io_addr, NETCARD_IO_EXTENT))
262
            return 1;
263
 
264
        /* Initialize the device structure. */
265
        if (dev->priv == NULL) {
266
                dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
267
                memset(dev->priv, 0, sizeof(struct net_local));
268
        }
269
        lp = (struct net_local *)dev->priv;
270
 
271
        ecard_claim(ec);
272
 
273
        /* if they give us an odd I/O address, then do ONE write to
274
           the address port, to get it back to address zero, where we
275
           expect to find the EISA signature word. */
276
        if (io_addr & 1) {
277
                io_addr &= ~1;
278
                if ((inw(io_addr + ADD_PORT) & ADD_MASK) != ADD_SIG)
279
                        return ENODEV;
280
                outw(PP_ChipID, io_addr + ADD_PORT);
281
        }
282
        outw(PP_ChipID, io_addr + ADD_PORT);
283
        if (inw(io_addr + DATA_PORT) != CHIP_EISA_ID_SIG)
284
                return ENODEV;
285
 
286
        /* Fill in the 'dev' fields. */
287
        dev->base_addr = io_addr;
288
//      dev->mem_start = io_addr + (0x600000 >> 1);
289
        dev->mem_start = 0;
290
        dev->irq = ec->irq;
291
        lp->ec = ec;
292
 
293
        ec->irqaddr = ioaddr(io_addr + (0x3ffa00 >> 2));
294
        ec->irqmask = 4;
295
 
296
        /* get the chip type */
297
        rev_type = readreg(dev, PRODUCT_ID_ADD);
298
        lp->chip_type = rev_type &~ REVISON_BITS;
299
        lp->chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
300
 
301
        /* Check the chip type and revision in order to set the correct send command
302
        CS8920 revision C and CS8900 revision F can use the faster send. */
303
        lp->send_cmd = TX_AFTER_381;
304
//      if (lp->chip_type == CS8900 && lp->chip_revision >= 'F')
305
//              lp->send_cmd = TX_NOW;
306
//      if (lp->chip_type != CS8900 && lp->chip_revision >= 'C')
307
//              lp->send_cmd = TX_NOW;
308
 
309
        if (net_debug  &&  version_printed++ == 0)
310
                printk(version);
311
 
312
        printk("%s: cs89%c0%s rev %c found at %#3lx",
313
               dev->name,
314
               lp->chip_type==CS8900?'0':'2',
315
               lp->chip_type==CS8920M?"M":"",
316
               lp->chip_revision,
317
               dev->base_addr);
318
 
319
        reset_chip(dev);
320
 
321
        /* First check to see if an EEPROM is attached*/
322
        if ((readreg(dev, PP_SelfST) & EEPROM_PRESENT) == 0)
323
                printk("\ncs89x0: No EEPROM, relying on command line....\n");
324
        else if (get_eeprom_data(dev, START_EEPROM_DATA,CHKSUM_LEN,eeprom_buff) < 0) {
325
                printk("\ncs89x0: EEPROM read failed, relying on command line.\n");
326
        } else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN,eeprom_buff) < 0) {
327
                printk("\ncs89x0: EEPROM checksum bad, relyong on command line\n");
328
        } else {
329
                /* get transmission control word  but keep the autonegotiation bits */
330
                if (!lp->auto_neg_cnf) lp->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
331
                /* Store adapter configuration */
332
                if (!lp->adapter_cnf) lp->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET/2];
333
                /* Store ISA configuration */
334
                lp->isa_config = eeprom_buff[ISA_CNF_OFFSET/2];
335
                /* store the initial memory base address */
336
                dev->mem_start = eeprom_buff[PACKET_PAGE_OFFSET/2] << 8;
337
                for (i = 0; i < ETH_ALEN/2; i++) {
338
                        dev->dev_addr[i*2] = eeprom_buff[i];
339
                        dev->dev_addr[i*2+1] = eeprom_buff[i] >> 8;
340
                }
341
        }
342
 
343
 
344
        printk(" media %s%s%s",
345
               (lp->adapter_cnf & A_CNF_10B_T)?"RJ-45,":"",
346
               (lp->adapter_cnf & A_CNF_AUI)?"AUI,":"",
347
               (lp->adapter_cnf & A_CNF_10B_2)?"BNC,":"");
348
 
349
        lp->irq_map = 0xffff;
350
 
351
        /* If this is a CS8900 then no pnp soft */
352
        if (lp->chip_type != CS8900 &&
353
            /* Check if the ISA IRQ has been set  */
354
                (i = readreg(dev, PP_CS8920_ISAINT) & 0xff,
355
                 (i != 0 && i < CS8920_NO_INTS))) {
356
                if (!dev->irq)
357
                        dev->irq = i;
358
        } else {
359
                i = lp->isa_config & INT_NO_MASK;
360
                if (lp->chip_type == CS8900) {
361
                        /* the table that follows is dependent upon how you wired up your cs8900
362
                         * in your system.  The table is the same as the cs8900 engineering demo
363
                         * board.  irq_map also depends on the contents of the table.  Also see
364
                         * write_irq, which is the reverse mapping of the table below. */
365
                        switch(i) {
366
                        case 0: i = 10; break;
367
                        case 1: i = 11; break;
368
                        case 2: i = 12; break;
369
                        case 3: i =  5; break;
370
                        default: printk("\ncs89x0: bug: isa_config is %d\n", i);
371
                        }
372
                        lp->irq_map = CS8900_IRQ_MAP; /* fixed IRQ map for CS8900 */
373
                } else {
374
                        int irq_map_buff[IRQ_MAP_LEN/2];
375
 
376
                        if (get_eeprom_data(dev, IRQ_MAP_EEPROM_DATA,
377
                                            IRQ_MAP_LEN/2,
378
                                            irq_map_buff) >= 0) {
379
                                if ((irq_map_buff[0] & 0xff) == PNP_IRQ_FRMT)
380
                                        lp->irq_map = (irq_map_buff[0]>>8) | (irq_map_buff[1] << 8);
381
                        }
382
                }
383
                if (!dev->irq)
384
                        dev->irq = i;
385
        }
386
 
387
        printk(" IRQ %d", dev->irq);
388
 
389
 
390
        /* print the ethernet address. */
391
        for (i = 0; i < ETH_ALEN; i++)
392
                printk(" %2.2x", dev->dev_addr[i]);
393
 
394
        /* Grab the region so we can find another board if autoIRQ fails. */
395
        request_region(io_addr, NETCARD_IO_EXTENT,"cs89x0");
396
 
397
        dev->open               = net_open;
398
        dev->stop               = net_close;
399
        dev->hard_start_xmit = net_send_packet;
400
        dev->get_stats  = net_get_stats;
401
        dev->set_multicast_list = &set_multicast_list;
402
        dev->set_mac_address = &set_mac_address;
403
 
404
        /* Fill in the fields of the device structure with ethernet values. */
405
        ether_setup(dev);
406
 
407
        printk("\n");
408
        return 0;
409
}
410
 
411
 
412
 
413
__initfunc(void
414
reset_chip(struct device *dev))
415
{
416
        struct net_local *lp = (struct net_local *)dev->priv;
417
        int ioaddr = dev->base_addr;
418
        int reset_start_time;
419
 
420
        writereg(dev, PP_SelfCTL, readreg(dev, PP_SelfCTL) | POWER_ON_RESET);
421
 
422
        /* wait 30 ms */
423
        current->state = TASK_INTERRUPTIBLE;
424
        current->timeout = jiffies + 30*HZ/1000;
425
        schedule();
426
 
427
        if (lp->chip_type != CS8900) {
428
                /* Hardware problem requires PNP registers to be reconfigured after a reset */
429
                outw(PP_CS8920_ISAINT, ioaddr + ADD_PORT);
430
                outb(dev->irq, ioaddr + DATA_PORT);
431
                outb(0,      ioaddr + DATA_PORT + 1);
432
 
433
                outw(PP_CS8920_ISAMemB, ioaddr + ADD_PORT);
434
                outb((dev->mem_start >> 8) & 0xff, ioaddr + DATA_PORT);
435
                outb((dev->mem_start >> 24) & 0xff,   ioaddr + DATA_PORT + 1);
436
        }
437
        /* Wait until the chip is reset */
438
        reset_start_time = jiffies;
439
        while( (readreg(dev, PP_SelfST) & INIT_DONE) == 0 && jiffies - reset_start_time < 2)
440
                ;
441
}
442
 
443
 
444
static void
445
control_dc_dc(struct device *dev, int on_not_off)
446
{
447
        struct net_local *lp = (struct net_local *)dev->priv;
448
        unsigned int selfcontrol;
449
        int timenow = jiffies;
450
        /* control the DC to DC convertor in the SelfControl register.  */
451
 
452
        selfcontrol = HCB1_ENBL; /* Enable the HCB1 bit as an output */
453
        if (((lp->adapter_cnf & A_CNF_DC_DC_POLARITY) != 0) ^ on_not_off)
454
                selfcontrol |= HCB1;
455
        else
456
                selfcontrol &= ~HCB1;
457
        writereg(dev, PP_SelfCTL, selfcontrol);
458
 
459
        /* Wait for the DC/DC converter to power up - 500ms */
460
        while (jiffies - timenow < 100)
461
                ;
462
 
463
}
464
 
465
static int
466
detect_tp(struct device *dev)
467
{
468
        struct net_local *lp = (struct net_local *)dev->priv;
469
        int timenow = jiffies;
470
 
471
        if (net_debug > 1) printk("%s: Attempting TP\n", dev->name);
472
 
473
        /* If connected to another full duplex capable 10-Base-T card the link pulses
474
           seem to be lost when the auto detect bit in the LineCTL is set.
475
           To overcome this the auto detect bit will be cleared whilst testing the
476
           10-Base-T interface.  This would not be necessary for the sparrow chip but
477
           is simpler to do it anyway. */
478
        writereg(dev, PP_LineCTL, lp->linectl &~ AUI_ONLY);
479
        control_dc_dc(dev, 0);
480
 
481
        /* Delay for the hardware to work out if the TP cable is present - 150ms */
482
        for (timenow = jiffies; jiffies - timenow < 15; )
483
                ;
484
        if ((readreg(dev, PP_LineST) & LINK_OK) == 0)
485
                return 0;
486
 
487
        if (lp->chip_type != CS8900) {
488
 
489
                writereg(dev, PP_AutoNegCTL, lp->auto_neg_cnf & AUTO_NEG_MASK);
490
 
491
                if ((lp->auto_neg_cnf & AUTO_NEG_BITS) == AUTO_NEG_ENABLE) {
492
                        printk("%s: negotiating duplex...\n",dev->name);
493
                        while (readreg(dev, PP_AutoNegST) & AUTO_NEG_BUSY) {
494
                                if (jiffies - timenow > 4000) {
495
                                        printk("**** Full / half duplex auto-negotiation timed out ****\n");
496
                                        break;
497
                                }
498
                        }
499
                }
500
                if (readreg(dev, PP_AutoNegST) & FDX_ACTIVE)
501
                        printk("%s: using full duplex\n", dev->name);
502
                else
503
                        printk("%s: using half duplex\n", dev->name);
504
        }
505
 
506
        return A_CNF_MEDIA_10B_T;
507
}
508
 
509
/* send a test packet - return true if carrier bits are ok */
510
static int
511
send_test_pkt(struct device *dev)
512
{
513
        int ioaddr = dev->base_addr;
514
        char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
515
                                 0, 46, /* A 46 in network order */
516
                                 0, 0, /* DSAP=0 & SSAP=0 fields */
517
                                 0xf3, 0 /* Control (Test Req + P bit set) */ };
518
        long timenow = jiffies;
519
 
520
        writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_TX_ON);
521
 
522
        memcpy(test_packet,          dev->dev_addr, ETH_ALEN);
523
        memcpy(test_packet+ETH_ALEN, dev->dev_addr, ETH_ALEN);
524
 
525
        outw(TX_AFTER_ALL, ioaddr + (TX_CMD_PORT << ECARD_SHIFT));
526
        outw(ETH_ZLEN, ioaddr + (TX_LEN_PORT << ECARD_SHIFT));
527
 
528
        /* Test to see if the chip has allocated memory for the packet */
529
        while (jiffies - timenow < 5)
530
                if (readreg(dev, PP_BusST) & READY_FOR_TX_NOW)
531
                        break;
532
        if (jiffies - timenow >= 5)
533
                return 0;        /* this shouldn't happen */
534
 
535
        /* Write the contents of the packet */
536
        if (dev->mem_start) {
537
                memcpy((void *)dev->mem_start + PP_TxFrame, test_packet, ETH_ZLEN);
538
        } else {
539
                outsw(ioaddr + (TX_FRAME_PORT << ECARD_SHIFT),
540
                      test_packet,(ETH_ZLEN+1) >>1);
541
        }
542
 
543
        if (net_debug > 1) printk("Sending test packet ");
544
        /* wait a couple of jiffies for packet to be received */
545
        for (timenow = jiffies; jiffies - timenow < 3; )
546
                ;
547
        if ((readreg(dev, PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
548
                if (net_debug > 1) printk("succeeded\n");
549
                return 1;
550
        }
551
        if (net_debug > 1) printk("failed\n");
552
        return 0;
553
}
554
 
555
 
556
static int
557
detect_aui(struct device *dev)
558
{
559
        struct net_local *lp = (struct net_local *)dev->priv;
560
 
561
        if (net_debug > 1) printk("%s: Attempting AUI\n", dev->name);
562
        control_dc_dc(dev, 0);
563
 
564
        writereg(dev, PP_LineCTL, (lp->linectl &~ AUTO_AUI_10BASET) | AUI_ONLY);
565
 
566
        if (send_test_pkt(dev))
567
                return A_CNF_MEDIA_AUI;
568
        else
569
                return 0;
570
}
571
 
572
static int
573
detect_bnc(struct device *dev)
574
{
575
        struct net_local *lp = (struct net_local *)dev->priv;
576
 
577
        if (net_debug > 1) printk("%s: Attempting BNC\n", dev->name);
578
        control_dc_dc(dev, 1);
579
 
580
        writereg(dev, PP_LineCTL, (lp->linectl &~ AUTO_AUI_10BASET) | AUI_ONLY);
581
 
582
        if (send_test_pkt(dev))
583
                return A_CNF_MEDIA_10B_2;
584
        else
585
                return 0;
586
}
587
 
588
 
589
static void
590
write_irq(struct device *dev, int chip_type, int irq)
591
{
592
        int i;
593
 
594
        // we always write 0, as that is what the card is hardwired to
595
 
596
        if (chip_type == CS8900) {
597
                switch(irq) {
598
                case 10: i = 0; break;
599
                case 11: i = 1; break;
600
                case 12: i = 2; break;
601
                case 5: i =  3; break;
602
                default: i = 3; break;
603
                }
604
                writereg(dev, PP_CS8900_ISAINT, 0x0);
605
        } else {
606
                writereg(dev, PP_CS8920_ISAINT, 0x0);
607
        }
608
}
609
 
610
/* Open/initialize the board.  This is called (in the current kernel)
611
   sometime after booting when the 'ifconfig' program is run.
612
 
613
   This routine should set everything up anew at each open, even
614
   registers that "should" only need to be set once at boot, so that
615
   there is non-reboot way to recover if something goes wrong.
616
   */
617
static int
618
net_open(struct device *dev)
619
{
620
        struct net_local *lp = (struct net_local *)dev->priv;
621
        int result = 0;
622
        int i;
623
 
624
        if (dev->irq < 2) {
625
                /* Allow interrupts to be generated by the chip */
626
                writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
627
                for (i = 2; i < CS8920_NO_INTS; i++) if ((1 << dev->irq) & lp->irq_map) {
628
                        if (request_irq (i, NULL, 0, "cs8920", dev) != -EBUSY) {
629
                                write_irq(dev, lp->chip_type, i);
630
                                writereg(dev, PP_BufCFG, GENERATE_SW_INTERRUPT);
631
                                if (request_irq (dev->irq = i, &net_interrupt, 0, "cs89x0", dev) == 0)
632
                                        break;
633
                        }
634
                }
635
 
636
 
637
                if (i >= CS8920_NO_INTS) {
638
                        writereg(dev, PP_BusCTL, 0);     /* disable interrupts. */
639
                        return -EAGAIN;
640
                }
641
        } else {
642
#if 0
643
                if (((1 << dev->irq) & lp->irq_map) == 0) {
644
                        printk("%s: IRQ %d is not in our map of allowable IRQs, which is %x\n",
645
                               dev->name, dev->irq, lp->irq_map);
646
                        return -EAGAIN;
647
                }
648
#endif
649
                writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
650
                write_irq(dev, lp->chip_type, dev->irq);
651
                if (request_irq(dev->irq, &net_interrupt, 0, "cs89x0", dev)) {
652
                        return -EAGAIN;
653
                }
654
        }
655
 
656
        /* set the Ethernet address */
657
        for (i=0; i < ETH_ALEN/2; i++)
658
                writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
659
 
660
        /* while we're testing the interface, leave interrupts disabled */
661
        writereg(dev, PP_BusCTL, MEMORY_ON);
662
 
663
        /* Set the LineCTL quintuplet based on adapter configuration read from EEPROM */
664
        if ((lp->adapter_cnf & A_CNF_EXTND_10B_2) && (lp->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
665
                lp->linectl = LOW_RX_SQUELCH;
666
        else
667
                lp->linectl = 0;
668
 
669
        /* check to make sure that they have the "right" hardware available */
670
        switch(lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
671
        case A_CNF_MEDIA_10B_T: result = lp->adapter_cnf & A_CNF_10B_T; break;
672
        case A_CNF_MEDIA_AUI:   result = lp->adapter_cnf & A_CNF_AUI; break;
673
        case A_CNF_MEDIA_10B_2: result = lp->adapter_cnf & A_CNF_10B_2; break;
674
        default: result = lp->adapter_cnf & (A_CNF_10B_T | A_CNF_AUI | A_CNF_10B_2);
675
        }
676
        if (!result) {
677
                printk("%s: EEPROM is configured for unavailable media\n", dev->name);
678
        release_irq:
679
                writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) & ~(SERIAL_TX_ON | SERIAL_RX_ON));
680
                free_irq(dev->irq, dev);
681
                return -EAGAIN;
682
        }
683
 
684
        /* set the hardware to the configured choice */
685
        switch(lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
686
        case A_CNF_MEDIA_10B_T:
687
                result = detect_tp(dev);
688
                if (!result) printk("%s: 10Base-T (RJ-45) has no cable\n", dev->name);
689
                if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
690
                        result = A_CNF_MEDIA_10B_T; /* Yes! I don't care if I see a link pulse */
691
                break;
692
        case A_CNF_MEDIA_AUI:
693
                result = detect_aui(dev);
694
                if (!result) printk("%s: 10Base-5 (AUI) has no cable\n", dev->name);
695
                if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
696
                        result = A_CNF_MEDIA_AUI; /* Yes! I don't care if I see a carrrier */
697
                break;
698
        case A_CNF_MEDIA_10B_2:
699
                result = detect_bnc(dev);
700
                if (!result) printk("%s: 10Base-2 (BNC) has no cable\n", dev->name);
701
                if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
702
                        result = A_CNF_MEDIA_10B_2; /* Yes! I don't care if I can xmit a packet */
703
                break;
704
        case A_CNF_MEDIA_AUTO:
705
                writereg(dev, PP_LineCTL, lp->linectl | AUTO_AUI_10BASET);
706
                if (lp->adapter_cnf & A_CNF_10B_T)
707
                        if ((result = detect_tp(dev)) != 0)
708
                                break;
709
                if (lp->adapter_cnf & A_CNF_AUI)
710
                        if ((result = detect_aui(dev)) != 0)
711
                                break;
712
                if (lp->adapter_cnf & A_CNF_10B_2)
713
                        if ((result = detect_bnc(dev)) != 0)
714
                                break;
715
                printk("%s: no media detected\n", dev->name);
716
                goto release_irq;
717
        }
718
        switch(result) {
719
        case 0: printk("%s: no network cable attached to configured media\n", dev->name);
720
                goto release_irq;
721
        case A_CNF_MEDIA_10B_T: printk("%s: using 10Base-T (RJ-45)\n", dev->name);break;
722
        case A_CNF_MEDIA_AUI:   printk("%s: using 10Base-5 (AUI)\n", dev->name);break;
723
        case A_CNF_MEDIA_10B_2: printk("%s: using 10Base-2 (BNC)\n", dev->name);break;
724
        default: printk("%s: unexpected result was %x\n", dev->name, result); goto release_irq;
725
        }
726
 
727
        /* Turn on both receive and transmit operations */
728
        writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
729
 
730
        /* Receive only error free packets addressed to this card */
731
        lp->rx_mode = 0;
732
        writereg(dev, PP_RxCTL, DEF_RX_ACCEPT);
733
 
734
        lp->curr_rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL;
735
        if (lp->isa_config & STREAM_TRANSFER)
736
                lp->curr_rx_cfg |= RX_STREAM_ENBL;
737
 
738
        writereg(dev, PP_RxCFG, lp->curr_rx_cfg);
739
 
740
        writereg(dev, PP_TxCFG, TX_LOST_CRS_ENBL | TX_SQE_ERROR_ENBL | TX_OK_ENBL |
741
               TX_LATE_COL_ENBL | TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
742
 
743
        writereg(dev, PP_BufCFG, READY_FOR_TX_ENBL | RX_MISS_COUNT_OVRFLOW_ENBL |
744
                 TX_COL_COUNT_OVRFLOW_ENBL | TX_UNDERRUN_ENBL);
745
 
746
        /* now that we've got our act together, enable everything */
747
        writereg(dev, PP_BusCTL, ENABLE_IRQ
748
                 );
749
        dev->tbusy = 0;
750
        dev->interrupt = 0;
751
        dev->start = 1;
752
        MOD_INC_USE_COUNT;
753
        return 0;
754
}
755
 
756
static int
757
net_send_packet(struct sk_buff *skb, struct device *dev)
758
{
759
        if (dev->tbusy) {
760
                /* If we get here, some higher level has decided we are broken.
761
                   There should really be a "kick me" function call instead. */
762
                int tickssofar = jiffies - dev->trans_start;
763
                if (tickssofar < 5)
764
                        return 1;
765
                if (net_debug > 0) printk("%s: transmit timed out, %s?\n", dev->name,
766
                           tx_done(dev) ? "IRQ conflict" : "network cable problem");
767
                /* Try to restart the adaptor. */
768
                dev->tbusy=0;
769
                dev->trans_start = jiffies;
770
        }
771
 
772
        /* Block a timer-based transmit from overlapping.  This could better be
773
           done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
774
        if (set_bit(0, (void*)&dev->tbusy) != 0)
775
                printk("%s: Transmitter access conflict.\n", dev->name);
776
        else {
777
                struct net_local *lp = (struct net_local *)dev->priv;
778
                unsigned int ioaddr = dev->base_addr;
779
                unsigned long flags;
780
 
781
                if (net_debug > 3)printk("%s: sent %d byte packet of type %x\n", dev->name, skb->len, (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
782
 
783
                /* keep the upload from being interrupted, since we
784
                   ask the chip to start transmitting before the
785
                   whole packet has been completely uploaded. */
786
                save_flags(flags);
787
                cli();
788
 
789
                /* initiate a transmit sequence */
790
                outw(lp->send_cmd, ioaddr + TX_CMD_PORT);
791
                outw(skb->len, ioaddr + TX_LEN_PORT);
792
 
793
                /* Test to see if the chip has allocated memory for the packet */
794
                if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) {
795
                        /* Gasp!  It hasn't.  But that shouldn't happen since
796
                           we're waiting for TxOk, so return 1 and requeue this packet. */
797
                        restore_flags(flags);
798
                        return 1;
799
                }
800
 
801
                /* Write the contents of the packet */
802
                outsw(ioaddr + TX_FRAME_PORT,skb->data,(skb->len+1) >>1);
803
 
804
                restore_flags(flags);
805
                dev->trans_start = jiffies;
806
        }
807
        dev_kfree_skb (skb, FREE_WRITE);
808
 
809
        return 0;
810
}
811
 
812
/* The typical workload of the driver:
813
   Handle the network interface interrupts. */
814
static void net_interrupt(int irq, void *dev_id, struct pt_regs * regs)
815
{
816
        struct device *dev = dev_id;
817
        struct net_local *lp;
818
        int ioaddr, status;
819
 
820
        if (dev == NULL) {
821
                printk ("net_interrupt(): irq %d for unknown device.\n", irq);
822
                return;
823
        }
824
        if (dev->interrupt)
825
                printk("%s: Re-entering the interrupt handler.\n", dev->name);
826
        dev->interrupt = 1;
827
 
828
        ioaddr = dev->base_addr;
829
        lp = (struct net_local *)dev->priv;
830
 
831
        /* we MUST read all the events out of the ISQ, otherwise we'll never
832
           get interrupted again.  As a consequence, we can't have any limit
833
           on the number of times we loop in the interrupt handler.  The
834
           hardware guarantees that eventually we'll run out of events.  Of
835
           course, if you're on a slow machine, and packets are arriving
836
           faster than you can read them off, you're screwed.  Hasta la
837
           vista, baby!  */
838
        while ((status = readword(dev, ISQ_PORT))) {
839
                if (net_debug > 4)printk("%s: event=%04x\n", dev->name, status);
840
                switch(status & ISQ_EVENT_MASK) {
841
                case ISQ_RECEIVER_EVENT:
842
                        /* Got a packet(s). */
843
                        net_rx(dev);
844
                        break;
845
                case ISQ_TRANSMITTER_EVENT:
846
                        lp->stats.tx_packets++;
847
                        dev->tbusy = 0;
848
                        mark_bh(NET_BH);        /* Inform upper layers. */
849
                        if ((status & TX_OK) == 0) lp->stats.tx_errors++;
850
                        if (status & TX_LOST_CRS) lp->stats.tx_carrier_errors++;
851
                        if (status & TX_SQE_ERROR) lp->stats.tx_heartbeat_errors++;
852
                        if (status & TX_LATE_COL) lp->stats.tx_window_errors++;
853
                        if (status & TX_16_COL) lp->stats.tx_aborted_errors++;
854
                        break;
855
                case ISQ_BUFFER_EVENT:
856
                        if (status & READY_FOR_TX) {
857
                                /* we tried to transmit a packet earlier,
858
                                   but inexplicably ran out of buffers.
859
                                   That shouldn't happen since we only ever
860
                                   load one packet.  Shrug.  Do the right
861
                                   thing anyway. */
862
                                dev->tbusy = 0;
863
                                mark_bh(NET_BH);        /* Inform upper layers. */
864
                        }
865
                        if (status & TX_UNDERRUN) {
866
                                if (net_debug > 0) printk("%s: transmit underrun\n", dev->name);
867
                                lp->send_underrun++;
868
                                if (lp->send_underrun == 3) lp->send_cmd = TX_AFTER_381;
869
                                else if (lp->send_underrun == 6) lp->send_cmd = TX_AFTER_ALL;
870
                        }
871
                        break;
872
                case ISQ_RX_MISS_EVENT:
873
                        lp->stats.rx_missed_errors += (status >>6);
874
                        break;
875
                case ISQ_TX_COL_EVENT:
876
                        lp->stats.collisions += (status >>6);
877
                        break;
878
                }
879
        }
880
        dev->interrupt = 0;
881
        return;
882
}
883
 
884
/* We have a good packet(s), get it/them out of the buffers. */
885
static void
886
net_rx(struct device *dev)
887
{
888
        struct net_local *lp = (struct net_local *)dev->priv;
889
        int ioaddr = dev->base_addr;
890
        struct sk_buff *skb;
891
        int status, length;
892
 
893
        status = inw(ioaddr + RX_FRAME_PORT);
894
        length = inw(ioaddr + RX_FRAME_PORT);
895
        if ((status & RX_OK) == 0) {
896
                lp->stats.rx_errors++;
897
                if (status & RX_RUNT) lp->stats.rx_length_errors++;
898
                if (status & RX_EXTRA_DATA) lp->stats.rx_length_errors++;
899
                if (status & RX_CRC_ERROR) if (!(status & (RX_EXTRA_DATA|RX_RUNT)))
900
                        /* per str 172 */
901
                        lp->stats.rx_crc_errors++;
902
                if (status & RX_DRIBBLE) lp->stats.rx_frame_errors++;
903
                return;
904
        }
905
 
906
        /* Malloc up new buffer. */
907
        skb = alloc_skb(length, GFP_ATOMIC);
908
        if (skb == NULL) {
909
                printk("%s: Memory squeeze, dropping packet.\n", dev->name);
910
                lp->stats.rx_dropped++;
911
                return;
912
        }
913
        skb->len = length;
914
        skb->dev = dev;
915
 
916
        skb_reserve(skb, 2);
917
 
918
        insw(ioaddr + RX_FRAME_PORT, skb->data, length >> 1);
919
        if (length & 1)
920
                skb->data[length-1] = inw(ioaddr + RX_FRAME_PORT);
921
 
922
        if (net_debug > 3)printk("%s: received %d byte packet of type %x\n",
923
                                 dev->name, length,
924
                                 (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
925
 
926
        skb->protocol=eth_type_trans(skb,dev);
927
        netif_rx(skb);
928
        lp->stats.rx_packets++;
929
/*RMK   lp->stats.rx_bytes+=skb->len;*/
930
        return;
931
}
932
 
933
/* The inverse routine to net_open(). */
934
static int
935
net_close(struct device *dev)
936
{
937
 
938
        writereg(dev, PP_RxCFG, 0);
939
        writereg(dev, PP_TxCFG, 0);
940
        writereg(dev, PP_BufCFG, 0);
941
        writereg(dev, PP_BusCTL, 0);
942
 
943
        dev->start = 0;
944
 
945
        free_irq(dev->irq, dev);
946
 
947
        /* Update the statistics here. */
948
 
949
        MOD_DEC_USE_COUNT;
950
        return 0;
951
 
952
}
953
 
954
/* Get the current statistics.  This may be called with the card open or
955
   closed. */
956
static struct enet_statistics *
957
net_get_stats(struct device *dev)
958
{
959
        struct net_local *lp = (struct net_local *)dev->priv;
960
 
961
        cli();
962
        /* Update the statistics from the device registers. */
963
        lp->stats.rx_missed_errors += (readreg(dev, PP_RxMiss) >> 6);
964
        lp->stats.collisions += (readreg(dev, PP_TxCol) >> 6);
965
        sti();
966
 
967
        return &lp->stats;
968
}
969
 
970
static void set_multicast_list(struct device *dev)
971
{
972
        struct net_local *lp = (struct net_local *)dev->priv;
973
 
974
        if(dev->flags&IFF_PROMISC)
975
        {
976
                lp->rx_mode = RX_ALL_ACCEPT;
977
        }
978
        else if((dev->flags&IFF_ALLMULTI)||dev->mc_list)
979
        {
980
                /* The multicast-accept list is initialized to accept-all, and we
981
                   rely on higher-level filtering for now. */
982
                lp->rx_mode = RX_MULTCAST_ACCEPT;
983
        }
984
        else
985
                lp->rx_mode = 0;
986
 
987
        writereg(dev, PP_RxCTL, DEF_RX_ACCEPT | lp->rx_mode);
988
 
989
        /* in promiscuous mode, we accept errored packets, so we have to enable interrupts on them also */
990
        writereg(dev, PP_RxCFG, lp->curr_rx_cfg |
991
             (lp->rx_mode == RX_ALL_ACCEPT? (RX_CRC_ERROR_ENBL|RX_RUNT_ENBL|RX_EXTRA_DATA_ENBL) : 0));
992
}
993
 
994
 
995
static int set_mac_address(struct device *dev, void *addr)
996
{
997
        int i;
998
        if (dev->start)
999
                return -EBUSY;
1000
        printk("%s: Setting MAC address to ", dev->name);
1001
        for (i = 0; i < 6; i++)
1002
                printk(" %2.2x", dev->dev_addr[i] = ((unsigned char *)addr)[i]);
1003
        printk(".\n");
1004
        /* set the Ethernet address */
1005
        for (i=0; i < ETH_ALEN/2; i++)
1006
                writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
1007
 
1008
        return 0;
1009
}
1010
 
1011
#ifdef MODULE
1012
 
1013
static char namespace[16] = "";
1014
static struct device dev_cs89x0 = {
1015
        NULL,
1016
        0, 0, 0, 0,
1017
        0, 0,
1018
        0, 0, 0, NULL, NULL };
1019
 
1020
static int io=0;
1021
static int irq=0;
1022
static int debug=0;
1023
static char media[8];
1024
static int duplex=-1;
1025
 
1026
MODULE_PARM(io, "i");
1027
MODULE_PARM(irq, "i");
1028
MODULE_PARM(debug, "i");
1029
MODULE_PARM(media, "s");
1030
MODULE_PARM(duplex, "i");
1031
 
1032
EXPORT_NO_SYMBOLS;
1033
 
1034
/*
1035
* media=t             - specify media type
1036
   or media=2
1037
   or media=aui
1038
   or medai=auto
1039
* duplex=0            - specify forced half/full/autonegotiate duplex
1040
* debug=#             - debug level
1041
 
1042
 
1043
* Default Chip Configuration:
1044
  * DMA Burst = enabled
1045
  * IOCHRDY Enabled = enabled
1046
    * UseSA = enabled
1047
    * CS8900 defaults to half-duplex if not specified on command-line
1048
    * CS8920 defaults to autoneg if not specified on command-line
1049
    * Use reset defaults for other config parameters
1050
 
1051
* Assumptions:
1052
  * media type specified is supported (circuitry is present)
1053
  * if memory address is > 1MB, then required mem decode hw is present
1054
  * if 10B-2, then agent other than driver will enable DC/DC converter
1055
    (hw or software util)
1056
 
1057
 
1058
*/
1059
 
1060
int
1061
init_module(void)
1062
{
1063
        struct net_local *lp;
1064
 
1065
        net_debug = debug;
1066
        dev_cs89x0.name = namespace;
1067
        dev_cs89x0.irq = irq;
1068
        dev_cs89x0.base_addr = io;
1069
        dev_cs89x0.init = etherc_probe;
1070
        dev_cs89x0.priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
1071
        memset(dev_cs89x0.priv, 0, sizeof(struct net_local));
1072
        lp = (struct net_local *)dev_cs89x0.priv;
1073
 
1074
        /* boy, they'd better get these right */
1075
        if (!strcmp(media, "rj45"))
1076
                lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
1077
        else if (!strcmp(media, "aui"))
1078
                lp->adapter_cnf = A_CNF_MEDIA_AUI   | A_CNF_AUI;
1079
        else if (!strcmp(media, "bnc"))
1080
                lp->adapter_cnf = A_CNF_MEDIA_10B_2 | A_CNF_10B_2;
1081
        else
1082
                lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
1083
 
1084
        if (duplex==-1)
1085
                lp->auto_neg_cnf = AUTO_NEG_ENABLE;
1086
 
1087
        if (io == 0)  {
1088
                printk(KERN_NOTICE "cs89x0.c: Module autoprobing not allowed.\n");
1089
                printk(KERN_NOTICE "cs89x0.c: Append io=0xNNN\n");
1090
                return -EPERM;
1091
        }
1092
        if (register_netdev(&dev_cs89x0) != 0) {
1093
                printk(KERN_WARNING "cs89x0.c: No card found at 0x%x\n", io);
1094
                return -ENXIO;
1095
        }
1096
        return 0;
1097
}
1098
 
1099
void
1100
cleanup_module(void)
1101
{
1102
 
1103
#endif
1104
#ifdef MODULE
1105
        outw(0, dev_cs89x0.base_addr + ADD_PORT);
1106
#endif
1107
#ifdef MODULE
1108
 
1109
        if (dev_cs89x0.priv != NULL) {
1110
                /* Free up the private structure, or leak memory :-)  */
1111
                unregister_netdev(&dev_cs89x0);
1112
                kfree(dev_cs89x0.priv);
1113
                dev_cs89x0.priv = NULL; /* gets re-allocated by cs89x0_probe1 */
1114
                /* If we don't do this, we can't re-insmod it later. */
1115
                release_region(dev_cs89x0.base_addr, NETCARD_IO_EXTENT);
1116
        }
1117
}
1118
#endif /* MODULE */
1119
 
1120
/*
1121
 * Local variables:
1122
 *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/include -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -DMODULE -DCONFIG_MODVERSIONS -c cs89x0.c"
1123
 *  version-control: t
1124
 *  kept-new-versions: 5
1125
 *  c-indent-level: 8
1126
 *  tab-width: 8
1127
 * End:
1128
 *
1129
 */
1130
 

powered by: WebSVN 2.1.0

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