]> Joshua Wise's Git repositories - netwatch.git/blob - net/3c90x.c
Merge branch 'master' of nyus:/storage/git/netwatch
[netwatch.git] / net / 3c90x.c
1 /*
2  * 3c90x.c -- This file implements the 3c90x driver for etherboot.  Written
3  * by Greg Beeley, Greg.Beeley@LightSys.org.  Modified by Steve Smith,
4  * Steve.Smith@Juno.Com. Alignment bug fix Neil Newell (nn@icenoir.net).
5  *
6  * This program Copyright (C) 1999 LightSys Technology Services, Inc.
7  * Portions Copyright (C) 1999 Steve Smith
8  *
9  * This program may be re-distributed in source or binary form, modified,
10  * sold, or copied for any purpose, provided that the above copyright message
11  * and this text are included with all source copies or derivative works, and
12  * provided that the above copyright message and this text are included in the
13  * documentation of any binary-only distributions.  This program is distributed
14  * WITHOUT ANY WARRANTY, without even the warranty of FITNESS FOR A PARTICULAR
15  * PURPOSE or MERCHANTABILITY.  Please read the associated documentation
16  * "3c90x.txt" before compiling and using this driver.
17  *
18  * --------
19  *
20  * Program written with the assistance of the 3com documentation for
21  * the 3c905B-TX card, as well as with some assistance from the 3c59x
22  * driver Donald Becker wrote for the Linux kernel, and with some assistance
23  * from the remainder of the Etherboot distribution.
24  *
25  * REVISION HISTORY:
26  *
27  * v0.10        1-26-1998       GRB     Initial implementation.
28  * v0.90        1-27-1998       GRB     System works.
29  * v1.00pre1    2-11-1998       GRB     Got prom boot issue fixed.
30  * v2.0         9-24-1999       SCS     Modified for 3c905 (from 3c905b code)
31  *                                      Re-wrote poll and transmit for
32  *                                      better error recovery and heavy
33  *                                      network traffic operation
34  * v2.01    5-26-2003 NN Fixed driver alignment issue which
35  *                  caused system lockups if driver structures
36  *                  not 8-byte aligned.
37  *
38  */
39
40 #include "etherboot-compat.h"
41 #include "net.h"
42 #include <timer.h>
43 #include <io.h>
44 #include <pci.h>
45 #include <pci-bother.h>
46 #include <minilib.h>
47 #include <output.h>
48 #include <paging.h>
49
50 #define XCVR_MAGIC      (0x5A00)
51 /** any single transmission fails after 16 collisions or other errors
52  ** this is the number of times to retry the transmission -- this should
53  ** be plenty
54  **/
55 #define XMIT_RETRIES    5
56
57 /*** Register definitions for the 3c905 ***/
58 enum Registers
59     {
60     regPowerMgmtCtrl_w = 0x7c,        /** 905B Revision Only                 **/
61     regUpMaxBurst_w = 0x7a,           /** 905B Revision Only                 **/
62     regDnMaxBurst_w = 0x78,           /** 905B Revision Only                 **/
63     regDebugControl_w = 0x74,         /** 905B Revision Only                 **/
64     regDebugData_l = 0x70,            /** 905B Revision Only                 **/
65     regRealTimeCnt_l = 0x40,          /** Universal                          **/
66     regUpBurstThresh_b = 0x3e,        /** 905B Revision Only                 **/
67     regUpPoll_b = 0x3d,               /** 905B Revision Only                 **/
68     regUpPriorityThresh_b = 0x3c,     /** 905B Revision Only                 **/
69     regUpListPtr_l = 0x38,            /** Universal                          **/
70     regCountdown_w = 0x36,            /** Universal                          **/
71     regFreeTimer_w = 0x34,            /** Universal                          **/
72     regUpPktStatus_l = 0x30,          /** Universal with Exception, pg 130   **/
73     regTxFreeThresh_b = 0x2f,         /** 90X Revision Only                  **/
74     regDnPoll_b = 0x2d,               /** 905B Revision Only                 **/
75     regDnPriorityThresh_b = 0x2c,     /** 905B Revision Only                 **/
76     regDnBurstThresh_b = 0x2a,        /** 905B Revision Only                 **/
77     regDnListPtr_l = 0x24,            /** Universal with Exception, pg 107   **/
78     regDmaCtrl_l = 0x20,              /** Universal with Exception, pg 106   **/
79                                       /**                                    **/
80     regIntStatusAuto_w = 0x1e,        /** 905B Revision Only                 **/
81     regTxStatus_b = 0x1b,             /** Universal with Exception, pg 113   **/
82     regTimer_b = 0x1a,                /** Universal                          **/
83     regTxPktId_b = 0x18,              /** 905B Revision Only                 **/
84     regCommandIntStatus_w = 0x0e,     /** Universal (Command Variations)     **/
85     };
86
87 /** following are windowed registers **/
88 enum Registers7
89     {
90     regPowerMgmtEvent_7_w = 0x0c,     /** 905B Revision Only                 **/
91     regVlanEtherType_7_w = 0x04,      /** 905B Revision Only                 **/
92     regVlanMask_7_w = 0x00,           /** 905B Revision Only                 **/
93     };
94
95 enum Registers6
96     {
97     regBytesXmittedOk_6_w = 0x0c,     /** Universal                          **/
98     regBytesRcvdOk_6_w = 0x0a,        /** Universal                          **/
99     regUpperFramesOk_6_b = 0x09,      /** Universal                          **/
100     regFramesDeferred_6_b = 0x08,     /** Universal                          **/
101     regFramesRecdOk_6_b = 0x07,       /** Universal with Exceptions, pg 142  **/
102     regFramesXmittedOk_6_b = 0x06,    /** Universal                          **/
103     regRxOverruns_6_b = 0x05,         /** Universal                          **/
104     regLateCollisions_6_b = 0x04,     /** Universal                          **/
105     regSingleCollisions_6_b = 0x03,   /** Universal                          **/
106     regMultipleCollisions_6_b = 0x02, /** Universal                          **/
107     regSqeErrors_6_b = 0x01,          /** Universal                          **/
108     regCarrierLost_6_b = 0x00,        /** Universal                          **/
109     };
110
111 enum Registers5
112     {
113     regIndicationEnable_5_w = 0x0c,   /** Universal                          **/
114     regInterruptEnable_5_w = 0x0a,    /** Universal                          **/
115     regTxReclaimThresh_5_b = 0x09,    /** 905B Revision Only                 **/
116     regRxFilter_5_b = 0x08,           /** Universal                          **/
117     regRxEarlyThresh_5_w = 0x06,      /** Universal                          **/
118     regTxStartThresh_5_w = 0x00,      /** Universal                          **/
119     };
120
121 enum Registers4
122     {
123     regUpperBytesOk_4_b = 0x0d,       /** Universal                          **/
124     regBadSSD_4_b = 0x0c,             /** Universal                          **/
125     regMediaStatus_4_w = 0x0a,        /** Universal with Exceptions, pg 201  **/
126     regPhysicalMgmt_4_w = 0x08,       /** Universal                          **/
127     regNetworkDiagnostic_4_w = 0x06,  /** Universal with Exceptions, pg 203  **/
128     regFifoDiagnostic_4_w = 0x04,     /** Universal with Exceptions, pg 196  **/
129     regVcoDiagnostic_4_w = 0x02,      /** Undocumented?                      **/
130     };
131
132 enum Registers3
133     {
134     regTxFree_3_w = 0x0c,             /** Universal                          **/
135     regRxFree_3_w = 0x0a,             /** Universal with Exceptions, pg 125  **/
136     regResetMediaOptions_3_w = 0x08,  /** Media Options on B Revision,       **/
137                                       /** Reset Options on Non-B Revision    **/
138     regMacControl_3_w = 0x06,         /** Universal with Exceptions, pg 199  **/
139     regMaxPktSize_3_w = 0x04,         /** 905B Revision Only                 **/
140     regInternalConfig_3_l = 0x00,     /** Universal, different bit           **/
141                                       /** definitions, pg 59                 **/
142     };
143
144 enum Registers2
145     {
146     regResetOptions_2_w = 0x0c,       /** 905B Revision Only                 **/
147     regStationMask_2_3w = 0x06,       /** Universal with Exceptions, pg 127  **/
148     regStationAddress_2_3w = 0x00,    /** Universal with Exceptions, pg 127  **/
149     };
150
151 enum Registers1
152     {
153     regRxStatus_1_w = 0x0a,           /** 90X Revision Only, Pg 126          **/
154     };
155
156 enum Registers0
157     {
158     regEepromData_0_w = 0x0c,         /** Universal                          **/
159     regEepromCommand_0_w = 0x0a,      /** Universal                          **/
160     regBiosRomData_0_b = 0x08,        /** 905B Revision Only                 **/
161     regBiosRomAddr_0_l = 0x04,        /** 905B Revision Only                 **/
162     };
163
164
165 /*** The names for the eight register windows ***/
166 enum Windows
167     {
168     winPowerVlan7 = 0x07,
169     winStatistics6 = 0x06,
170     winTxRxControl5 = 0x05,
171     winDiagnostics4 = 0x04,
172     winTxRxOptions3 = 0x03,
173     winAddressing2 = 0x02,
174     winUnused1 = 0x01,
175     winEepromBios0 = 0x00,
176     };
177
178
179 /*** Command definitions for the 3c90X ***/
180 enum Commands
181     {
182     cmdGlobalReset = 0x00,             /** Universal with Exceptions, pg 151 **/
183     cmdSelectRegisterWindow = 0x01,    /** Universal                         **/
184     cmdEnableDcConverter = 0x02,       /**                                   **/
185     cmdRxDisable = 0x03,               /**                                   **/
186     cmdRxEnable = 0x04,                /** Universal                         **/
187     cmdRxReset = 0x05,                 /** Universal                         **/
188     cmdStallCtl = 0x06,                /** Universal                         **/
189     cmdTxEnable = 0x09,                /** Universal                         **/
190     cmdTxDisable = 0x0A,               /**                                   **/
191     cmdTxReset = 0x0B,                 /** Universal                         **/
192     cmdRequestInterrupt = 0x0C,        /**                                   **/
193     cmdAcknowledgeInterrupt = 0x0D,    /** Universal                         **/
194     cmdSetInterruptEnable = 0x0E,      /** Universal                         **/
195     cmdSetIndicationEnable = 0x0F,     /** Universal                         **/
196     cmdSetRxFilter = 0x10,             /** Universal                         **/
197     cmdSetRxEarlyThresh = 0x11,        /**                                   **/
198     cmdSetTxStartThresh = 0x13,        /**                                   **/
199     cmdStatisticsEnable = 0x15,        /**                                   **/
200     cmdStatisticsDisable = 0x16,       /**                                   **/
201     cmdDisableDcConverter = 0x17,      /**                                   **/
202     cmdSetTxReclaimThresh = 0x18,      /**                                   **/
203     cmdSetHashFilterBit = 0x19,        /**                                   **/
204     };
205
206
207 /*** Values for int status register bitmask **/
208 #define INT_INTERRUPTLATCH      (1<<0)
209 #define INT_HOSTERROR           (1<<1)
210 #define INT_TXCOMPLETE          (1<<2)
211 #define INT_RXCOMPLETE          (1<<4)
212 #define INT_RXEARLY             (1<<5)
213 #define INT_INTREQUESTED        (1<<6)
214 #define INT_UPDATESTATS         (1<<7)
215 #define INT_LINKEVENT           (1<<8)
216 #define INT_DNCOMPLETE          (1<<9)
217 #define INT_UPCOMPLETE          (1<<10)
218 #define INT_CMDINPROGRESS       (1<<12)
219 #define INT_WINDOWNUMBER        (7<<13)
220
221
222 /*** TX descriptor ***/
223 typedef struct
224     {
225     unsigned int        DnNextPtr;
226     unsigned int        FrameStartHeader;
227     unsigned int        DataAddr;
228     unsigned int        DataLength;
229     }
230     TXD __attribute__ ((aligned(8))); /* 64-bit aligned for bus mastering */
231
232 /*** RX descriptor ***/
233 typedef struct
234     {
235     unsigned int        UpNextPtr;
236     unsigned int        UpPktStatus;
237     unsigned int        DataAddr;
238     unsigned int        DataLength;
239     }
240     RXD __attribute__ ((aligned(8))); /* 64-bit aligned for bus mastering */
241
242 /*** Global variables ***/
243 static struct
244     {
245     unsigned int        is3c556;
246     unsigned char       isBrev;
247     unsigned char       CurrentWindow;
248     unsigned int        IOAddr;
249     unsigned char       HWAddr[ETH_ALEN];
250     TXD                 TransmitDPD;
251     RXD                 ReceiveUPD;
252     }
253     INF_3C90X;
254 static struct nic nic;
255
256 #define _outl(v,a) outl((a),(v))
257 #define _outw(v,a) outw((a),(v))
258 #define _outb(v,a) outb((a),(v))
259
260 static int _issue_command(int ioaddr, int cmd, int param)
261 {
262         outw(ioaddr + regCommandIntStatus_w, (cmd << 11) | param);
263
264         while (inw(ioaddr + regCommandIntStatus_w) & INT_CMDINPROGRESS)
265                 ;
266
267         return 0;
268 }
269
270
271 /*** a3c90x_internal_SetWindow: selects a register window set.
272  ***/
273 static int _set_window(int ioaddr, int window)
274 {
275         if (INF_3C90X.CurrentWindow == window)
276                 return 0;
277
278         _issue_command(ioaddr, cmdSelectRegisterWindow, window);
279         INF_3C90X.CurrentWindow = window;
280
281         return 0;
282 }
283
284
285 /*** a3c90x_internal_ReadEeprom - read data from the serial eeprom.
286  ***/
287 static unsigned short
288 a3c90x_internal_ReadEeprom(int ioaddr, int address)
289 {
290         unsigned short val;
291
292         /** Select correct window **/
293         _set_window(INF_3C90X.IOAddr, winEepromBios0);
294
295         /** Make sure the eeprom isn't busy **/
296         do
297         {
298                 int i;
299                 for (i = 0; i < 165; i++)
300                         inb(0x80);      /* wait 165 usec */
301         }
302         while(0x8000 & inw(ioaddr + regEepromCommand_0_w));
303
304         /** Read the value. **/
305         if (INF_3C90X.is3c556)
306                 _outw(address + (0x230), ioaddr + regEepromCommand_0_w);
307         else
308                 _outw(address + 0x80, ioaddr + regEepromCommand_0_w);
309
310         do
311         {
312                 int i;
313                 for (i = 0; i < 165; i++)
314                         inb(0x80);      /* wait 165 usec */
315         }
316         while(0x8000 & inw(ioaddr + regEepromCommand_0_w));
317         val = inw(ioaddr + regEepromData_0_w);
318         
319         return val;
320 }
321
322
323 #ifdef  CFG_3C90X_BOOTROM_FIX
324 /*** a3c90x_internal_WriteEepromWord - write a physical word of
325  *** data to the onboard serial eeprom (not the BIOS prom, but the
326  *** nvram in the card that stores, among other things, the MAC
327  *** address).
328  ***/
329 static int
330 a3c90x_internal_WriteEepromWord(int ioaddr, int address, unsigned short value)
331     {
332         /** Select register window **/
333         _set_window(ioaddr, winEepromBios0);
334
335         /** Verify Eeprom not busy **/
336         while((1<<15) & inw(ioaddr + regEepromCommand_0_w));
337
338         /** Issue WriteEnable, and wait for completion. **/
339         _outw(0x30, ioaddr + regEepromCommand_0_w);
340         while((1<<15) & inw(ioaddr + regEepromCommand_0_w));
341
342         /** Issue EraseRegister, and wait for completion. **/
343         _outw(address + ((0x03)<<6), ioaddr + regEepromCommand_0_w);
344         while((1<<15) & inw(ioaddr + regEepromCommand_0_w));
345
346         /** Send the new data to the eeprom, and wait for completion. **/
347         _outw(value, ioaddr + regEepromData_0_w);
348         _outw(0x30, ioaddr + regEepromCommand_0_w);
349         while((1<<15) & inw(ioaddr + regEepromCommand_0_w));
350
351         /** Burn the new data into the eeprom, and wait for completion. **/
352         _outw(address + ((0x01)<<6), ioaddr + regEepromCommand_0_w);
353         while((1<<15) & inw(ioaddr + regEepromCommand_0_w));
354
355     return 0;
356     }
357 #endif
358
359 #ifdef  CFG_3C90X_BOOTROM_FIX
360 /*** a3c90x_internal_WriteEeprom - write data to the serial eeprom,
361  *** and re-compute the eeprom checksum.
362  ***/
363 static int
364 a3c90x_internal_WriteEeprom(int ioaddr, int address, unsigned short value)
365     {
366     int cksum = 0,v;
367     int i;
368     int maxAddress, cksumAddress;
369
370         if (INF_3C90X.isBrev)
371             {
372             maxAddress=0x1f;
373             cksumAddress=0x20;
374             }
375         else
376             {
377             maxAddress=0x16;
378             cksumAddress=0x17;
379             }
380
381         /** Write the value. **/
382         if (a3c90x_internal_WriteEepromWord(ioaddr, address, value) == -1)
383             return -1;
384
385         /** Recompute the checksum. **/
386         for(i=0;i<=maxAddress;i++)
387             {
388             v = a3c90x_internal_ReadEeprom(ioaddr, i);
389             cksum ^= (v & 0xFF);
390             cksum ^= ((v>>8) & 0xFF);
391             }
392         /** Write the checksum to the location in the eeprom **/
393         if (a3c90x_internal_WriteEepromWord(ioaddr, cksumAddress, cksum) == -1)
394             return -1;
395
396     return 0;
397     }
398 #endif
399
400 /*** a3c90x_reset: exported function that resets the card to its default
401  *** state.  This is so the Linux driver can re-set the card up the way
402  *** it wants to.  If CFG_3C90X_PRESERVE_XCVR is defined, then the reset will
403  *** not alter the selected transceiver that we used to download the boot
404  *** image.
405  ***/
406 static void a3c90x_reset(void)
407     {
408 #ifdef  CFG_3C90X_PRESERVE_XCVR
409     int cfg;
410     /** Read the current InternalConfig value. **/
411     _set_window(INF_3C90X.IOAddr, winTxRxOptions3);
412     cfg = inl(INF_3C90X.IOAddr + regInternalConfig_3_l);
413 #endif
414
415     /** Send the reset command to the card **/
416     outputf("3c90x: issuing RESET");
417     _issue_command(INF_3C90X.IOAddr, cmdGlobalReset, 0);
418
419     /** global reset command resets station mask, non-B revision cards
420      ** require explicit reset of values
421      **/
422     _set_window(INF_3C90X.IOAddr, winAddressing2);
423     _outw(0, INF_3C90X.IOAddr + regStationMask_2_3w+0);
424     _outw(0, INF_3C90X.IOAddr + regStationMask_2_3w+2);
425     _outw(0, INF_3C90X.IOAddr + regStationMask_2_3w+4);
426
427 #ifdef  CFG_3C90X_PRESERVE_XCVR
428     /** Re-set the original InternalConfig value from before reset **/
429     _set_window(INF_3C90X.IOAddr, winTxRxOptions3);
430     _outl(cfg, INF_3C90X.IOAddr + regInternalConfig_3_l);
431
432     /** enable DC converter for 10-Base-T **/
433     if ((cfg&0x0300) == 0x0300)
434         {
435         _issue_command(INF_3C90X.IOAddr, cmdEnableDcConverter, 0);
436         }
437 #endif
438
439     /** Issue transmit reset, wait for command completion **/
440     _issue_command(INF_3C90X.IOAddr, cmdTxReset, 0);
441     if (! INF_3C90X.isBrev)
442         _outb(0x01, INF_3C90X.IOAddr + regTxFreeThresh_b);
443     _issue_command(INF_3C90X.IOAddr, cmdTxEnable, 0);
444
445     /**
446      ** reset of the receiver on B-revision cards re-negotiates the link
447      ** takes several seconds (a computer eternity)
448      **/
449     if (INF_3C90X.isBrev)
450         _issue_command(INF_3C90X.IOAddr, cmdRxReset, 0x04);
451     else
452         _issue_command(INF_3C90X.IOAddr, cmdRxReset, 0x00);
453     while (inw(INF_3C90X.IOAddr + regCommandIntStatus_w) & INT_CMDINPROGRESS)
454        ;
455     _issue_command(INF_3C90X.IOAddr, cmdRxEnable, 0);
456
457     _issue_command(INF_3C90X.IOAddr, cmdSetInterruptEnable, 0);
458     /** enable rxComplete and txComplete **/
459     _issue_command(INF_3C90X.IOAddr, cmdSetIndicationEnable, 0x0014);
460     /** acknowledge any pending status flags **/
461     _issue_command(INF_3C90X.IOAddr, cmdAcknowledgeInterrupt, 0x661);
462
463     return;
464     }
465
466
467
468 /*** a3c90x_transmit: exported function that transmits a packet.  Does not
469  *** return any particular status.  Parameters are:
470  *** dest_addr[6] - destination address, ethernet;
471  *** proto - protocol type (ARP, IP, etc);
472  *** size - size of the non-header part of the packet that needs transmitted;
473  *** pkt - the pointer to the packet data itself.
474  ***/
475 static void
476 a3c90x_transmit(unsigned int size, const char *pkt)
477 {
478         unsigned char status;
479         unsigned int i, retries;
480         static unsigned int stillwaiting = 0;
481
482         if (stillwaiting)
483         {
484                 while (!(inw(INF_3C90X.IOAddr + regCommandIntStatus_w) & INT_TXCOMPLETE) && oneshot_running())
485                         ;
486                 if (!(inw(INF_3C90X.IOAddr + regCommandIntStatus_w) & INT_TXCOMPLETE))
487                 {
488                         outputf("3c90x: tx timeout? txstat %02x", inb(INF_3C90X.IOAddr + regTxStatus_b));
489                         outputf("3c90x: Gen sts %04x", inw(INF_3C90X.IOAddr + regCommandIntStatus_w));
490                 }
491                 status = inb(INF_3C90X.IOAddr + regTxStatus_b);
492                 outb(INF_3C90X.IOAddr + regTxStatus_b, 0x00);
493                 stillwaiting = 0;
494         }
495
496         for (retries=0; retries < XMIT_RETRIES; retries++)
497         {
498                 if (retries != 0)
499                         outputf("3c90x: retrying packet send (%d)", retries);
500                 
501                 _issue_command(INF_3C90X.IOAddr, cmdStallCtl, 2 /* Stall download */);
502
503                 /** Setup the DPD (download descriptor) **/
504                 INF_3C90X.TransmitDPD.DnNextPtr = 0;
505                 /** set notification for transmission completion (bit 15) **/
506                 INF_3C90X.TransmitDPD.FrameStartHeader = (size) | 0x8000;
507                 INF_3C90X.TransmitDPD.DataAddr = v2p((void*)pkt);
508                 INF_3C90X.TransmitDPD.DataLength = size + (1<<31);
509
510                 /** Send the packet **/
511                 outl(INF_3C90X.IOAddr + regDnListPtr_l, v2p(&(INF_3C90X.TransmitDPD)));
512                 _issue_command(INF_3C90X.IOAddr, cmdStallCtl, 3 /* Unstall download */);
513                 
514                 oneshot_start_ms(10);
515                 while((inl(INF_3C90X.IOAddr + regDnListPtr_l) != 0) && oneshot_running())
516                         ;
517                 if (!oneshot_running())
518                 {
519                         outputf("3c90x: Download engine pointer timeout");
520                         continue;
521                 }
522
523                 oneshot_start_ms(10);
524                 stillwaiting = 1;
525                 break;
526                 
527 #if 0           
528                 /** successful completion (sans "interrupt Requested" bit) **/
529                 if ((status & 0xbf) == 0x80)
530                         return;
531
532                 outputf("3c90x: Status (%hhX)", status);
533                 /** check error codes **/
534                 if (status & 0x02)
535                 {
536                         outputf("3c90x: Tx Reclaim Error (%hhX)", status);
537                         a3c90x_reset();
538                 } else if (status & 0x04) {
539                         outputf("3c90x: Tx Status Overflow (%hhX)", status);
540                         for (i=0; i<32; i++)
541                                 _outb(0x00, INF_3C90X.IOAddr + regTxStatus_b);
542                         /** must re-enable after max collisions before re-issuing tx **/
543                         _issue_command(INF_3C90X.IOAddr, cmdTxEnable, 0);
544                 } else if (status & 0x08) {
545                         outputf("3c90x: Tx Max Collisions (%hhX)", status);
546                         /** must re-enable after max collisions before re-issuing tx **/
547                         _issue_command(INF_3C90X.IOAddr, cmdTxEnable, 0);
548                 } else if (status & 0x10) {
549                         outputf("3c90x: Tx Underrun (%hhX)", status);
550                         a3c90x_reset();
551                 } else if (status & 0x20) {
552                         outputf("3c90x: Tx Jabber (%hhX)", status);
553                         a3c90x_reset();
554                 } else if ((status & 0x80) != 0x80) {
555                         outputf("3c90x: Internal Error - Incomplete Transmission (%hhX)", status);
556                         a3c90x_reset();
557                 }
558 #endif
559         }
560
561         /** failed after RETRY attempts **/
562         outputf("3c90x: Failed to send after %d retries", retries);
563         return;
564 }
565
566
567
568 /*** a3c90x_poll: exported routine that waits for a certain length of time
569  *** for a packet, and if it sees none, returns 0.  This routine should
570  *** copy the packet to nic->packet if it gets a packet and set the size
571  *** in nic->packetlen.  Return 1 if a packet was found.
572  ***/
573 static int
574 a3c90x_poll(struct nic *nic, int retrieve)
575     {
576     int i, errcode;
577
578     if (!(inw(INF_3C90X.IOAddr + regCommandIntStatus_w)&0x0010))
579         {
580         return 0;
581         }
582
583     if ( ! retrieve ) return 1;
584
585     /** we don't need to acknowledge rxComplete -- the upload engine
586      ** does it for us.
587      **/
588
589     /** Build the up-load descriptor **/
590     INF_3C90X.ReceiveUPD.UpNextPtr = 0;
591     INF_3C90X.ReceiveUPD.UpPktStatus = 0;
592     INF_3C90X.ReceiveUPD.DataAddr = v2p(nic->packet);
593     INF_3C90X.ReceiveUPD.DataLength = 1536 + (1<<31);
594
595     /** Submit the upload descriptor to the NIC **/
596     _outl(v2p(&(INF_3C90X.ReceiveUPD)),
597          INF_3C90X.IOAddr + regUpListPtr_l);
598
599     /** Wait for upload completion (upComplete(15) or upError (14)) **/
600     for(i=0;i<40000;i++);
601     while((INF_3C90X.ReceiveUPD.UpPktStatus & ((1<<14) | (1<<15))) == 0)
602         for(i=0;i<40000;i++);
603
604     /** Check for Error (else we have good packet) **/
605     if (INF_3C90X.ReceiveUPD.UpPktStatus & (1<<14))
606         {
607         errcode = INF_3C90X.ReceiveUPD.UpPktStatus;
608         if (errcode & (1<<16))
609             outputf("3C90X: Rx Overrun (%hX)",errcode>>16);
610         else if (errcode & (1<<17))
611             outputf("3C90X: Runt Frame (%hX)",errcode>>16);
612         else if (errcode & (1<<18))
613             outputf("3C90X: Alignment Error (%hX)",errcode>>16);
614         else if (errcode & (1<<19))
615             outputf("3C90X: CRC Error (%hX)",errcode>>16);
616         else if (errcode & (1<<20))
617             outputf("3C90X: Oversized Frame (%hX)",errcode>>16);
618         else
619             outputf("3C90X: Packet error (%hX)",errcode>>16);
620         return 0;
621         }
622
623     /** Ok, got packet.  Set length in nic->packetlen. **/
624     nic->packetlen = (INF_3C90X.ReceiveUPD.UpPktStatus & 0x1FFF);
625
626     return 1;
627     }
628
629
630
631 /*** a3c90x_disable: exported routine to disable the card.  What's this for?
632  *** the eepro100.c driver didn't have one, so I just left this one empty too.
633  *** Ideas anyone?
634  *** Must turn off receiver at least so stray packets will not corrupt memory
635  *** [Ken]
636  ***/
637 void a3c90x_disable(struct dev *dev)
638 {
639         /* reset and disable merge */
640         a3c90x_reset();
641         /* Disable the receiver and transmitter. */
642         _outw(cmdRxDisable, INF_3C90X.IOAddr + regCommandIntStatus_w);
643         _outw(cmdTxDisable, INF_3C90X.IOAddr + regCommandIntStatus_w);
644 }
645
646
647 /*** a3c90x_probe: exported routine to probe for the 3c905 card and perform
648  *** initialization.  If this routine is called, the pci functions did find the
649  *** card.  We just have to init it here.
650  ***/
651 static int a3c90x_probe(struct pci_dev * pci, void * data)
652 {
653     INF_3C90X.is3c556 = (pci->did == 0x6055);
654  
655     int i, c;
656     unsigned short eeprom[0x100];
657     unsigned int cfg;
658     unsigned int mopt;
659     unsigned int mstat;
660     unsigned short linktype;
661 #define HWADDR_OFFSET   10
662
663     unsigned long ioaddr = 0;
664     for (i = 0; i < 6; i++) {
665         if (pci->bars[i].type == PCI_BAR_IO) {
666             ioaddr = pci->bars[i].addr;
667             break;
668         }
669     }
670
671     if (ioaddr == 0)
672     {
673         outputf("3c90x: Unable to find I/O address");
674         return 0;
675     }
676     
677     /* Power it on */
678     pci_write16(pci->bus, pci->dev, pci->fn, 0xE0,
679         pci_read16(pci->bus, pci->dev, pci->fn, 0xE0) & ~0x3);
680     
681     outputf("3c90x: Picked I/O address %04x", ioaddr);
682     pci_bother_add(pci);
683     nic.ioaddr = ioaddr & ~3;
684     nic.irqno = 0;
685
686     INF_3C90X.IOAddr = ioaddr;
687     INF_3C90X.CurrentWindow = 255;
688     switch (a3c90x_internal_ReadEeprom(INF_3C90X.IOAddr, 0x03))
689         {
690         case 0x9000: /** 10 Base TPO             **/
691         case 0x9001: /** 10/100 T4               **/
692         case 0x9050: /** 10/100 TPO              **/
693         case 0x9051: /** 10 Base Combo           **/
694                 INF_3C90X.isBrev = 0;
695                 break;
696
697         case 0x9004: /** 10 Base TPO             **/
698         case 0x9005: /** 10 Base Combo           **/
699         case 0x9006: /** 10 Base TPO and Base2   **/
700         case 0x900A: /** 10 Base FL              **/
701         case 0x9055: /** 10/100 TPO              **/
702         case 0x9056: /** 10/100 T4               **/
703         case 0x905A: /** 10 Base FX              **/
704         default:
705                 INF_3C90X.isBrev = 1;
706                 break;
707         }
708
709     /** Load the EEPROM contents **/
710     if (INF_3C90X.isBrev)
711         {
712         for(i=0;i<=/*0x20*/0x7F;i++)
713             {
714             eeprom[i] = a3c90x_internal_ReadEeprom(INF_3C90X.IOAddr, i);
715             }
716
717 #ifdef  CFG_3C90X_BOOTROM_FIX
718         /** Set xcvrSelect in InternalConfig in eeprom. **/
719         /* only necessary for 3c905b revision cards with boot PROM bug!!! */
720         a3c90x_internal_WriteEeprom(INF_3C90X.IOAddr, 0x13, 0x0160);
721 #endif
722
723 #ifdef  CFG_3C90X_XCVR
724         if (CFG_3C90X_XCVR == 255)
725             {
726             /** Clear the LanWorks register **/
727             a3c90x_internal_WriteEeprom(INF_3C90X.IOAddr, 0x16, 0);
728             }
729         else
730             {
731             /** Set the selected permanent-xcvrSelect in the
732              ** LanWorks register
733              **/
734             a3c90x_internal_WriteEeprom(INF_3C90X.IOAddr, 0x16,
735                             XCVR_MAGIC + ((CFG_3C90X_XCVR) & 0x000F));
736             }
737 #endif
738         }
739     else
740         {
741         for(i=0;i<=/*0x17*/0x7F;i++)
742             {
743             eeprom[i] = a3c90x_internal_ReadEeprom(INF_3C90X.IOAddr, i);
744             }
745         }
746
747     /** Print identification message **/
748 #ifdef  CFG_3C90X_BOOTROM_FIX
749     if (INF_3C90X.isBrev)
750         {
751         outputf("NOTE: 3c905b bootrom fix enabled; has side "
752            "effects.  See 3c90x.txt for info.");
753         }
754 #endif
755
756     /** Retrieve the Hardware address and print it on the screen. **/
757     INF_3C90X.HWAddr[0] = eeprom[HWADDR_OFFSET + 0]>>8;
758     INF_3C90X.HWAddr[1] = eeprom[HWADDR_OFFSET + 0]&0xFF;
759     INF_3C90X.HWAddr[2] = eeprom[HWADDR_OFFSET + 1]>>8;
760     INF_3C90X.HWAddr[3] = eeprom[HWADDR_OFFSET + 1]&0xFF;
761     INF_3C90X.HWAddr[4] = eeprom[HWADDR_OFFSET + 2]>>8;
762     INF_3C90X.HWAddr[5] = eeprom[HWADDR_OFFSET + 2]&0xFF;
763     outputf("MAC Address = %02x:%02x:%02x:%02x:%02x:%02x",
764         INF_3C90X.HWAddr[0],
765         INF_3C90X.HWAddr[1],
766         INF_3C90X.HWAddr[2],
767         INF_3C90X.HWAddr[3],
768         INF_3C90X.HWAddr[4],
769         INF_3C90X.HWAddr[5]);
770
771     /** 3C556: Invert MII power **/
772     if (INF_3C90X.is3c556) {
773         unsigned int tmp;
774         _set_window(INF_3C90X.IOAddr, winAddressing2);
775         tmp = inw(INF_3C90X.IOAddr + regResetOptions_2_w);
776         tmp |= 0x4000;
777         _outw(tmp, INF_3C90X.IOAddr + regResetOptions_2_w);
778     }
779
780     /* Test if the link is good, if not continue */
781     _set_window(INF_3C90X.IOAddr, winDiagnostics4);
782     mstat = inw(INF_3C90X.IOAddr + regMediaStatus_4_w);
783     if((mstat & (1<<11)) == 0) {
784         outputf("Valid link not established");
785         return 0;
786     }
787
788     /** Program the MAC address into the station address registers **/
789     _set_window(INF_3C90X.IOAddr, winAddressing2);
790     _outw(htons(eeprom[HWADDR_OFFSET + 0]), INF_3C90X.IOAddr + regStationAddress_2_3w);
791     _outw(htons(eeprom[HWADDR_OFFSET + 1]), INF_3C90X.IOAddr + regStationAddress_2_3w+2);
792     _outw(htons(eeprom[HWADDR_OFFSET + 2]), INF_3C90X.IOAddr + regStationAddress_2_3w+4);
793     _outw(0, INF_3C90X.IOAddr + regStationMask_2_3w+0);
794     _outw(0, INF_3C90X.IOAddr + regStationMask_2_3w+2);
795     _outw(0, INF_3C90X.IOAddr + regStationMask_2_3w+4);
796
797     /** Fill in our entry in the etherboot arp table **/
798 /* XXX ? for lwip? 
799     for(i=0;i<ETH_ALEN;i++)
800         nic.node_addr[i] = (eeprom[HWADDR_OFFSET + i/2] >> (8*((i&1)^1))) & 0xff;
801 */
802
803     /** Read the media options register, print a message and set default
804      ** xcvr.
805      **
806      ** Uses Media Option command on B revision, Reset Option on non-B
807      ** revision cards -- same register address
808      **/
809     _set_window(INF_3C90X.IOAddr, winTxRxOptions3);
810     mopt = inw(INF_3C90X.IOAddr + regResetMediaOptions_3_w);
811
812     /** mask out VCO bit that is defined as 10baseFL bit on B-rev cards **/
813     if (! INF_3C90X.isBrev)
814         {
815         mopt &= 0x7F;
816         }
817
818     outputf("Connectors present: ");
819     c = 0;
820     linktype = 0x0008;
821     if (mopt & 0x01)
822         {
823         outputf("%s100Base-T4",(c++)?", ":"");
824         linktype = 0x0006;
825         }
826     if (mopt & 0x04)
827         {
828         outputf("%s100Base-FX",(c++)?", ":"");
829         linktype = 0x0005;
830         }
831     if (mopt & 0x10)
832         {
833         outputf("%s10Base-2",(c++)?", ":"");
834         linktype = 0x0003;
835         }
836     if (mopt & 0x20)
837         {
838         outputf("%sAUI",(c++)?", ":"");
839         linktype = 0x0001;
840         }
841     if (mopt & 0x40)
842         {
843         outputf("%sMII",(c++)?", ":"");
844         linktype = 0x0006;
845         }
846     if ((mopt & 0xA) == 0xA)
847         {
848         outputf("%s10Base-T / 100Base-TX",(c++)?", ":"");
849         linktype = 0x0008;
850         }
851     else if ((mopt & 0xA) == 0x2)
852         {
853         outputf("%s100Base-TX",(c++)?", ":"");
854         linktype = 0x0008;
855         }
856     else if ((mopt & 0xA) == 0x8)
857         {
858         outputf("%s10Base-T",(c++)?", ":"");
859         linktype = 0x0008;
860         }
861     outputf(".");
862
863     /** Determine transceiver type to use, depending on value stored in
864      ** eeprom 0x16
865      **/
866     if (INF_3C90X.isBrev)
867         {
868         if ((eeprom[0x16] & 0xFF00) == XCVR_MAGIC)
869             {
870             /** User-defined **/
871             linktype = eeprom[0x16] & 0x000F;
872             }
873         }
874     else
875         {
876 #ifdef  CFG_3C90X_XCVR
877             if (CFG_3C90X_XCVR != 255)
878                 linktype = CFG_3C90X_XCVR;
879 #endif  /* CFG_3C90X_XCVR */
880
881             /** I don't know what MII MAC only mode is!!! **/
882             if (linktype == 0x0009)
883                 {
884                 if (INF_3C90X.isBrev)
885                         outputf("WARNING: MII External MAC Mode only supported on B-revision "
886                                "cards!!!!\nFalling Back to MII Mode\n");
887                 linktype = 0x0006;
888                 }
889         }
890
891     /** enable DC converter for 10-Base-T **/
892     if (linktype == 0x0003)
893         {
894         _issue_command(INF_3C90X.IOAddr, cmdEnableDcConverter, 0);
895         }
896
897     /** Set the link to the type we just determined. **/
898     _set_window(INF_3C90X.IOAddr, winTxRxOptions3);
899     cfg = inl(INF_3C90X.IOAddr + regInternalConfig_3_l);
900     cfg &= ~(0xF<<20);
901     cfg |= (linktype<<20);
902     _outl(cfg, INF_3C90X.IOAddr + regInternalConfig_3_l);
903
904     /** Now that we set the xcvr type, reset the Tx and Rx, re-enable. **/
905     _issue_command(INF_3C90X.IOAddr, cmdTxReset, 0);
906     if (!INF_3C90X.isBrev)
907         _outb(0x01, INF_3C90X.IOAddr + regTxFreeThresh_b);
908
909     _issue_command(INF_3C90X.IOAddr, cmdTxEnable, 0);
910
911     /**
912      ** reset of the receiver on B-revision cards re-negotiates the link
913      ** takes several seconds (a computer eternity)
914      **/
915     if (INF_3C90X.isBrev)
916         _issue_command(INF_3C90X.IOAddr, cmdRxReset, 0x04);
917     else
918         _issue_command(INF_3C90X.IOAddr, cmdRxReset, 0x00);
919
920     /** Set the RX filter = receive only individual pkts & multicast & bcast. **/
921     _issue_command(INF_3C90X.IOAddr, cmdSetRxFilter, 0x01 + 0x02 + 0x04);
922     _issue_command(INF_3C90X.IOAddr, cmdRxEnable, 0);
923
924
925     /**
926      ** set Indication and Interrupt flags , acknowledge any IRQ's
927      **/
928     _issue_command(INF_3C90X.IOAddr, cmdSetInterruptEnable, 0);
929     _issue_command(INF_3C90X.IOAddr, cmdSetIndicationEnable, 0x0014);
930     _issue_command(INF_3C90X.IOAddr, cmdAcknowledgeInterrupt, 0x661);
931
932     /* * Set our exported functions **/
933     nic.poll     = a3c90x_poll;
934     nic.transmit = a3c90x_transmit;
935     memcpy(nic.hwaddr, INF_3C90X.HWAddr, 6);
936     eth_register(&nic);
937
938     return 1;
939 }
940
941 static struct pci_id a3c90x_nics[] = {
942 /* Original 90x revisions: */
943 PCI_ROM(0x10b7, 0x6055, "3c556",         "3C556"),              /* Huricane */
944 PCI_ROM(0x10b7, 0x9000, "3c905-tpo",     "3Com900-TPO"),        /* 10 Base TPO */
945 PCI_ROM(0x10b7, 0x9001, "3c905-t4",      "3Com900-Combo"),      /* 10/100 T4 */
946 PCI_ROM(0x10b7, 0x9050, "3c905-tpo100",  "3Com905-TX"),         /* 100 Base TX / 10/100 TPO */
947 PCI_ROM(0x10b7, 0x9051, "3c905-combo",   "3Com905-T4"),         /* 100 Base T4 / 10 Base Combo */
948 /* Newer 90xB revisions: */
949 PCI_ROM(0x10b7, 0x9004, "3c905b-tpo",    "3Com900B-TPO"),       /* 10 Base TPO */
950 PCI_ROM(0x10b7, 0x9005, "3c905b-combo",  "3Com900B-Combo"),     /* 10 Base Combo */
951 PCI_ROM(0x10b7, 0x9006, "3c905b-tpb2",   "3Com900B-2/T"),       /* 10 Base TP and Base2 */
952 PCI_ROM(0x10b7, 0x900a, "3c905b-fl",     "3Com900B-FL"),        /* 10 Base FL */
953 PCI_ROM(0x10b7, 0x9055, "3c905b-tpo100", "3Com905B-TX"),        /* 10/100 TPO */
954 PCI_ROM(0x10b7, 0x9056, "3c905b-t4",     "3Com905B-T4"),        /* 10/100 T4 */
955 PCI_ROM(0x10b7, 0x9058, "3c905b-9058",   "3Com905B-9058"),      /* Cyclone 10/100/BNC */
956 PCI_ROM(0x10b7, 0x905a, "3c905b-fx",     "3Com905B-FL"),        /* 100 Base FX / 10 Base FX */
957 /* Newer 90xC revision: */
958 PCI_ROM(0x10b7, 0x9200, "3c905c-tpo",    "3Com905C-TXM"),       /* 10/100 TPO (3C905C-TXM) */
959 PCI_ROM(0x10b7, 0x9202, "3c920b-emb-ati", "3c920B-EMB-WNM (ATI Radeon 9100 IGP)"),      /* 3c920B-EMB-WNM (ATI Radeon 9100 IGP) */
960 PCI_ROM(0x10b7, 0x9210, "3c920b-emb-wnm","3Com20B-EMB WNM"),
961 PCI_ROM(0x10b7, 0x9800, "3c980",         "3Com980-Cyclone"),    /* Cyclone */
962 PCI_ROM(0x10b7, 0x9805, "3c9805",        "3Com9805"),           /* Dual Port Server Cyclone */
963 PCI_ROM(0x10b7, 0x7646, "3csoho100-tx",  "3CSOHO100-TX"),       /* Hurricane */
964 PCI_ROM(0x10b7, 0x4500, "3c450",         "3Com450 HomePNA Tornado"),
965 PCI_ROM(0x10b7, 0x1201, "3c982a",        "3Com982A"),
966 PCI_ROM(0x10b7, 0x1202, "3c982b",        "3Com982B"),
967 };
968
969 struct pci_driver a3c90x_driver = {
970         .name     = "3C90X",
971         .probe    = a3c90x_probe,
972         .ids      = a3c90x_nics,
973         .id_count = sizeof(a3c90x_nics)/sizeof(a3c90x_nics[0]),
974 };
This page took 0.122929 seconds and 4 git commands to generate.