Filter:   InfoImg
download fa311.c
Language: C
License: GPL
Copyright: copyright 1999-2001 by Donald Becker. (C) 2001 Inprimis Technologies, Inc.
LOC: 316
Project Info
Etherboot
Server: SourceForge
Type: cvs
...erboot‑5.1\src\drivers\net\
   3c509.c
   3c509.h
   3c515.c
   3c595.c
   3c595.h
   3c90x.c
   cs89x0.c
   cs89x0.h
   davicom.c
   depca.c
   e1000.c
   e1000_hw.h
   eepro.c
   eepro100.c
   epic100.c
   epic100.h
   fa311.c
   hfa384x.h
   lance.c
   natsemi.c
   ns8390.c
   ns8390.h
   p80211hdr.h
   pcnet32.c
   prism2.c
   prism2_pci.c
   prism2_plx.c
   rtl8139.c
   sis900.c
   sis900.h
   sk_g16.c
   sk_g16.h
   skel.c
   smc9000.c
   smc9000.h
   sundance.c
   tg3.c
   tg3.h
   tlan.c
   tlan.h
   tulip.c
   via-rhine.c
   w89c840.c
   wlan_compat.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#ifdef ALLMULTI
#error multicast support is not yet implemented
#endif
/*
        Driver for the National Semiconductor DP83810 Ethernet controller.
        
        Portions Copyright (C) 2001 Inprimis Technologies, Inc.
        http://www.inprimis.com/
        
        This driver is based (heavily) on the Linux driver for this chip 
        which is copyright 1999-2001 by Donald Becker.

        This software has no warranties expressed or implied for any
        purpose.

        This software may be used and distributed according to the terms of
        the GNU General Public License (GPL), incorporated herein by reference.
        Drivers based on or derived from this code fall under the GPL and must
        retain the authorship, copyright and license notice.  This file is not
        a complete program and may only be used when the entire operating
        system is licensed under the GPL.  License for under other terms may be
        available.  Contact the original author for details.

        The original author may be reached as becker@scyld.com, or at
        Scyld Computing Corporation
        410 Severn Ave., Suite 210
        Annapolis MD 21403
*/


typedef unsigned char  u8;
typedef   signed char  s8;
typedef unsigned short u16;
typedef   signed short s16;
typedef unsigned int   u32;
typedef   signed int   s32;

#include "etherboot.h"
#include "nic.h"
#include "pci.h"

#define virt_to_le32desc(addr)  cpu_to_le32(virt_to_bus(addr))
#define le32desc_to_virt(addr)  bus_to_virt(le32_to_cpu(addr))

#define TX_RING_SIZE 1
#define RX_RING_SIZE 4
#define TIME_OUT     1000000
#define PKT_BUF_SZ   1536

/* Offsets to the device registers. */
enum register_offsets {
    ChipCmd=0x00, ChipConfig=0x04, EECtrl=0x08, PCIBusCfg=0x0C,
    IntrStatus=0x10, IntrMask=0x14, IntrEnable=0x18,
    TxRingPtr=0x20, TxConfig=0x24,
    RxRingPtr=0x30, RxConfig=0x34,
    WOLCmd=0x40, PauseCmd=0x44, RxFilterAddr=0x48, RxFilterData=0x4C,
    BootRomAddr=0x50, BootRomData=0x54, StatsCtrl=0x5C, StatsData=0x60,
    RxPktErrs=0x60, RxMissed=0x68, RxCRCErrs=0x64,
};

/* Bit in ChipCmd. */
enum ChipCmdBits {
    ChipReset=0x100, RxReset=0x20, TxReset=0x10, RxOff=0x08, RxOn=0x04,
    TxOff=0x02, TxOn=0x01,
};

/* Bits in the interrupt status/mask registers. */
enum intr_status_bits {
    IntrRxDone=0x0001, IntrRxIntr=0x0002, IntrRxErr=0x0004, IntrRxEarly=0x0008,
    IntrRxIdle=0x0010, IntrRxOverrun=0x0020,
    IntrTxDone=0x0040, IntrTxIntr=0x0080, IntrTxErr=0x0100,
    IntrTxIdle=0x0200, IntrTxUnderrun=0x0400,
    StatsMax=0x0800, LinkChange=0x4000,	WOLPkt=0x2000,
    RxResetDone=0x1000000, TxResetDone=0x2000000,
    IntrPCIErr=0x00f00000, IntrNormalSummary=0x0251, IntrAbnormalSummary=0xED20,
};

/* Bits in the RxMode register. */
enum rx_mode_bits {
    AcceptErr=0x20, AcceptRunt=0x10, AcceptBroadcast=0xC0000000,
    AcceptMulticast=0x00200000, AcceptAllMulticast=0x20000000,
    AcceptAllPhys=0x10000000, AcceptMyPhys=0x08000000,
};

/* Bits in network_desc.status */
enum desc_status_bits {
    DescOwn=0x80000000, DescMore=0x40000000, DescIntr=0x20000000,
    DescNoCRC=0x10000000,
    DescPktOK=0x08000000, RxTooLong=0x00400000,
};

/* The Rx and Tx buffer descriptors. */
struct netdev_desc {
    u32 next_desc;
    s32 cmd_status;
    u32 addr;
};

static struct FA311_DEV {
    unsigned int    ioaddr;
    unsigned short  vendor;
    unsigned short  device;
    unsigned int    cur_rx;
    unsigned int    cur_tx;
    unsigned int    rx_buf_sz;
    volatile struct netdev_desc *rx_head_desc;
    volatile struct netdev_desc rx_ring[RX_RING_SIZE] __attribute__ ((aligned (4)));
    volatile struct netdev_desc tx_ring[TX_RING_SIZE] __attribute__ ((aligned (4)));
} fa311_dev;

static int  eeprom_read(long ioaddr, int location);
static void init_ring(struct FA311_DEV *dev);
static void fa311_reset(struct nic *nic);
static int  fa311_poll(struct nic *nic);
static void fa311_transmit(struct nic *nic, const char *d, unsigned int t, unsigned int s, const char *p);
static void fa311_disable(struct dev *dev);

static char rx_packet[PKT_BUF_SZ * RX_RING_SIZE] __attribute__ ((aligned (4)));
static char tx_packet[PKT_BUF_SZ * TX_RING_SIZE] __attribute__ ((aligned (4)));

static int fa311_probe(struct dev *ndev, struct pci_device *pci)
{
struct nic *nic = (struct nic *)ndev;
int            prev_eedata;
int            i;
int            duplex;
int            tx_config;
int            rx_config;
unsigned char  macaddr[6];
unsigned char  mactest;
struct FA311_DEV* dev = &fa311_dev;
unsigned long mmio_start, mmio_len;
	
    memset(dev, 0, sizeof(*dev));
    dev->vendor = pci->vendor;
    dev->device = pci->dev_id;
    mmio_start = pci_bar_start(pci, PCI_BASE_ADDRESS_1);
    mmio_len   = pci_bar_start(pci, PCI_BASE_ADDRESS_1);
    /* FIXME this is an abuse of dev->ioaddr... */
    dev->ioaddr = ioremap(mmio_start, mmio_len);

    /* Work around the dropped serial bit. */
    prev_eedata = eeprom_read(dev->ioaddr, 6);
    for (i = 0; i < 3; i++) {
        int eedata = eeprom_read(dev->ioaddr, i + 7);
        macaddr[i*2] = (eedata << 1) + (prev_eedata >> 15);
        macaddr[i*2+1] = eedata >> 7;
        prev_eedata = eedata;
    }
    mactest = 0;
    for (i = 0; i < 6; i++)
        mactest |= macaddr[i];
    if (mactest == 0)
        return (0);
    for (i = 0; i < 6; i++)
        nic->node_addr[i] = macaddr[i];
    printf("%! ", nic->node_addr);

    adjust_pci_device(pci);

    fa311_reset(nic);

    ndev->disable = fa311_disable;
    nic->poll     = fa311_poll;
    nic->transmit = fa311_transmit;

    init_ring(dev);

    writel(virt_to_bus(dev->rx_ring), dev->ioaddr + RxRingPtr);
    writel(virt_to_bus(dev->tx_ring), dev->ioaddr + TxRingPtr);

    for (i = 0; i < 6; i += 2)
    {
        writel(i, dev->ioaddr + RxFilterAddr);
        writew(macaddr[i] + (macaddr[i+1] << 8),
               dev->ioaddr + RxFilterData);
    }

    /* Initialize other registers. */
    /* Configure for standard, in-spec Ethernet. */
    if (readl(dev->ioaddr + ChipConfig) & 0x20000000)
    {    /* Full duplex */
        tx_config = 0xD0801002;
        rx_config = 0x10000020;
    }
    else
    {
        tx_config = 0x10801002;
        rx_config = 0x0020;
    }
    writel(tx_config, dev->ioaddr + TxConfig);
    writel(rx_config, dev->ioaddr + RxConfig);

    duplex = readl(dev->ioaddr + ChipConfig) & 0x20000000 ? 1 : 0;
    if (duplex) {
        rx_config |= 0x10000000;
        tx_config |= 0xC0000000;
    } else {
        rx_config &= ~0x10000000;
        tx_config &= ~0xC0000000;
    }
    writew(tx_config, dev->ioaddr + TxConfig);
    writew(rx_config, dev->ioaddr + RxConfig);

    writel(AcceptBroadcast | AcceptAllMulticast | AcceptMyPhys, 
           dev->ioaddr + RxFilterAddr);

    writel(RxOn | TxOn, dev->ioaddr + ChipCmd);
    writel(4, dev->ioaddr + StatsCtrl);              /* Clear Stats */
    return 1;	

}

static void fa311_reset(struct nic *nic __unused)
{
u32 chip_config;
struct FA311_DEV* dev = &fa311_dev;

    /* Reset the chip to erase previous misconfiguration. */
    outl(ChipReset, dev->ioaddr + ChipCmd);

    if ((readl(dev->ioaddr + ChipConfig) & 0xe000) != 0xe000)
    {
        chip_config = readl(dev->ioaddr + ChipConfig);
    }
}

static int fa311_poll(struct nic *nic)
{
s32 desc_status;
int to;
int entry;
int retcode;
struct FA311_DEV* dev = &fa311_dev;

    retcode = 0;
    entry = dev->cur_rx;
    to = TIME_OUT;
    while (to != 0)
    {
        desc_status = dev->rx_ring[entry].cmd_status;
        if ((desc_status & DescOwn) != 0)
            break;
        else
            --to;
    }
    if (to != 0)
    {
        readl(dev->ioaddr + IntrStatus);         /* clear interrrupt bits */
        /* driver owns the next entry it's a new packet. Send it up. */
        if ((desc_status & (DescMore|DescPktOK|RxTooLong)) == DescPktOK)
        {
            nic->packetlen = (desc_status & 0x0fff) - 4;    /* Omit CRC size. */
            memcpy(nic->packet, (char*)(dev->rx_ring[entry].addr), nic->packetlen);
            retcode = 1;
        }
        /* Give the descriptor back to the chip */
        dev->rx_ring[entry].cmd_status = cpu_to_le32(dev->rx_buf_sz);
        dev->cur_rx++;
        if (dev->cur_rx >= RX_RING_SIZE)
            dev->cur_rx = 0;
        dev->rx_head_desc = virt_to_le32desc(&dev->rx_ring[dev->cur_rx]);
    }
    /* Restart Rx engine if stopped. */
    writel(RxOn, dev->ioaddr + ChipCmd);
    return retcode;
}

static void fa311_transmit(struct nic *nic, const char *destaddr, unsigned int type, unsigned int len, const char *data)
{
unsigned short nstype;
s32            desc_status;
int            to;
int            entry;
char*          txp;
unsigned char* s;
struct FA311_DEV* dev = &fa311_dev;

    /* Calculate the next Tx descriptor entry. */
    entry = dev->cur_tx;
    txp = (char*)(dev->tx_ring[entry].addr);

    memcpy(txp, destaddr, ETH_ALEN);
    memcpy(txp + ETH_ALEN, nic->node_addr, ETH_ALEN);
    nstype = htons(type);
    memcpy(txp + 12, (char*)&nstype, 2);
    memcpy(txp + ETH_HLEN, data, len);
    len += ETH_HLEN;
    /* pad frame */
    if (len <  ETH_ZLEN)
    {
        s = (unsigned char*)(txp+len);
        while (s < (unsigned char*)(txp+ETH_ZLEN))
            *s++ = 0;
        len = ETH_ZLEN;
    }
    dev->tx_ring[entry].cmd_status = cpu_to_le32(DescOwn | len);
    dev->cur_tx++;
    if (dev->cur_tx >= TX_RING_SIZE)
        dev->cur_tx = 0;

    /* Wake the potentially-idle transmit channel. */
    writel(TxOn, dev->ioaddr + ChipCmd);

    /* wait for tranmission to complete */
    to = TIME_OUT;
    while (to != 0)
    {
        desc_status = dev->tx_ring[entry].cmd_status;
        if ((desc_status & DescOwn) == 0)
            break;
        else
            --to;
    }

    readl(dev->ioaddr + IntrStatus);         /* clear interrrupt bits */
    return;
}

static void fa311_disable(struct dev *ndev)
{
struct nic *nic = (struct nic *)ndev;
struct FA311_DEV* dev = &fa311_dev;

    /* merge reset and disable */
    fa311_reset(nic); 

    /* Stop the chip's Tx and Rx processes. */
    writel(RxOff | TxOff, dev->ioaddr + ChipCmd);

    /* Unmap our access to the device */
    iounmap(dev->ioaddr);
}


/* Read the EEPROM and MII Management Data I/O (MDIO) interfaces.
   The EEPROM code is for the common 93c06/46 EEPROMs with 6 bit addresses. */

/* Delay between EEPROM clock transitions.
   No extra delay is needed with 33Mhz PCI, but future 66Mhz access may need
   a delay.  Note that pre-2.0.34 kernels had a cache-alignment bug that
   made udelay() unreliable.
   The old method of using an ISA access as a delay, __SLOW_DOWN_IO__, is
   depricated.
*/
#define eeprom_delay(ee_addr)	inl(ee_addr)

enum EEPROM_Ctrl_Bits {
	EE_ShiftClk=0x04, EE_DataIn=0x01, EE_ChipSelect=0x08, EE_DataOut=0x02,
};
#define EE_Write0 (EE_ChipSelect)
#define EE_Write1 (EE_ChipSelect | EE_DataIn)

/* The EEPROM commands include the alway-set leading bit. */
enum EEPROM_Cmds {
	EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
};


static int eeprom_read(long addr, int location)
{
	int i;
	int retval = 0;
	int ee_addr = addr + EECtrl;
	int read_cmd = location | EE_ReadCmd;
	writel(EE_Write0, ee_addr);

	/* Shift the read command bits out. */
	for (i = 10; i >= 0; i--) {
		short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
		writel(dataval, ee_addr);
		eeprom_delay(ee_addr);
		writel(dataval | EE_ShiftClk, ee_addr);
		eeprom_delay(ee_addr);
	}
	writel(EE_ChipSelect, ee_addr);
	eeprom_delay(ee_addr);

	for (i = 0; i < 16; i++) {
		writel(EE_ChipSelect | EE_ShiftClk, ee_addr);
		eeprom_delay(ee_addr);
		retval |= (readl(ee_addr) & EE_DataOut) ? 1 << i : 0;
		writel(EE_ChipSelect, ee_addr);
		eeprom_delay(ee_addr);
	}

	/* Terminate the EEPROM access. */
	writel(EE_Write0, ee_addr);
	writel(0, ee_addr);
	return retval;
}

/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
static void init_ring(struct FA311_DEV *dev)
{
	int i;

	dev->cur_rx = 0;
    dev->cur_tx = 0;

	dev->rx_buf_sz = PKT_BUF_SZ;
	dev->rx_head_desc = virt_to_le32desc(&dev->rx_ring[0]);

	/* Initialize all Rx descriptors. */
	for (i = 0; i < RX_RING_SIZE; i++) {
		dev->rx_ring[i].next_desc = virt_to_le32desc(&dev->rx_ring[i+1]);
		dev->rx_ring[i].cmd_status = DescOwn;
	}
	/* Mark the last entry as wrapping the ring. */
	dev->rx_ring[i-1].next_desc = virt_to_le32desc(&dev->rx_ring[0]);

	/* Fill in the Rx buffers.  Handle allocation failure gracefully. */
	for (i = 0; i < RX_RING_SIZE; i++) {
		dev->rx_ring[i].addr = virt_to_le32desc(&rx_packet[PKT_BUF_SZ * i]);
	    dev->rx_ring[i].cmd_status = cpu_to_le32(dev->rx_buf_sz);
	}

	for (i = 0; i < TX_RING_SIZE; i++) {
		dev->tx_ring[i].next_desc = virt_to_le32desc(&dev->tx_ring[i+1]);
		dev->tx_ring[i].cmd_status = 0;
	}
	dev->tx_ring[i-1].next_desc = virt_to_le32desc(&dev->tx_ring[0]);

	for (i = 0; i < TX_RING_SIZE; i++)
		dev->tx_ring[i].addr = virt_to_le32desc(&tx_packet[PKT_BUF_SZ * i]);
	return;
}

static struct pci_id fa311_nics[] = {
PCI_ROM(0x100b, 0x0020, "fa311too", "DP83815"),
};

static struct pci_driver fa311_driver __pci_driver = {
	.type     = NIC_DRIVER,
	.name     = "FA311",
	.probe    = fa311_probe,
	.ids      = fa311_nics,
	.id_count = sizeof(fa311_nics)/sizeof(fa311_nics[0]),
	.class    = 0,
};