2 * Digital Semiconductor DECchip 2114x PCI Fast Ethernet LAN Controller.
6 * handle more error conditions;
7 * tidy setup packet mess;
8 * push initialisation back to attach;
12 #include "../port/lib.h"
17 #include "../port/error.h"
18 #include "../port/netif.h"
23 #define debug if(DEBUG)print
30 #define Rbsz ROUNDUP(sizeof(Etherpkt)+4, 4)
32 enum { /* CRS0 - Bus Mode */
33 Swr = 0x00000001, /* Software Reset */
34 Bar = 0x00000002, /* Bus Arbitration */
35 Dsl = 0x0000007C, /* Descriptor Skip Length (field) */
36 Ble = 0x00000080, /* Big/Little Endian */
37 Pbl = 0x00003F00, /* Programmable Burst Length (field) */
38 Cal = 0x0000C000, /* Cache Alignment (field) */
39 Cal8 = 0x00004000, /* 8 longword boundary alignment */
40 Cal16 = 0x00008000, /* 16 longword boundary alignment */
41 Cal32 = 0x0000C000, /* 32 longword boundary alignment */
42 Tap = 0x000E0000, /* Transmit Automatic Polling (field) */
43 Dbo = 0x00100000, /* Descriptor Byte Ordering Mode */
44 Rml = 0x00200000, /* Read Multiple */
47 enum { /* CSR[57] - Status and Interrupt Enable */
48 Ti = 0x00000001, /* Transmit Interrupt */
49 Tps = 0x00000002, /* Transmit Process Stopped */
50 Tu = 0x00000004, /* Transmit buffer Unavailable */
51 Tjt = 0x00000008, /* Transmit Jabber Timeout */
52 Unf = 0x00000020, /* transmit UNderFlow */
53 Ri = 0x00000040, /* Receive Interrupt */
54 Ru = 0x00000080, /* Receive buffer Unavailable */
55 Rps = 0x00000100, /* Receive Process Stopped */
56 Rwt = 0x00000200, /* Receive Watchdog Timeout */
57 Eti = 0x00000400, /* Early Transmit Interrupt */
58 Gte = 0x00000800, /* General purpose Timer Expired */
59 Fbe = 0x00002000, /* Fatal Bus Error */
60 Ais = 0x00008000, /* Abnormal Interrupt Summary */
61 Nis = 0x00010000, /* Normal Interrupt Summary */
62 Rs = 0x000E0000, /* Receive process State (field) */
63 Ts = 0x00700000, /* Transmit process State (field) */
64 Eb = 0x03800000, /* Error bits */
67 enum { /* CSR6 - Operating Mode */
68 Hp = 0x00000001, /* Hash/Perfect receive filtering mode */
69 Sr = 0x00000002, /* Start/stop Receive */
70 Ho = 0x00000004, /* Hash-Only filtering mode */
71 Pb = 0x00000008, /* Pass Bad frames */
72 If = 0x00000010, /* Inverse Filtering */
73 Sb = 0x00000020, /* Start/stop Backoff counter */
74 Pr = 0x00000040, /* Promiscuous Mode */
75 Pm = 0x00000080, /* Pass all Multicast */
76 Fd = 0x00000200, /* Full Duplex mode */
77 Om = 0x00000C00, /* Operating Mode (field) */
78 Fc = 0x00001000, /* Force Collision */
79 St = 0x00002000, /* Start/stop Transmission Command */
80 Tr = 0x0000C000, /* ThReshold control bits (field) */
85 Ca = 0x00020000, /* CApture effect enable */
86 Ps = 0x00040000, /* Port Select */
87 Hbd = 0x00080000, /* HeartBeat Disable */
88 Imm = 0x00100000, /* IMMediate mode */
89 Sf = 0x00200000, /* Store and Forward */
90 Ttm = 0x00400000, /* Transmit Threshold Mode */
91 Pcs = 0x00800000, /* PCS function */
92 Scr = 0x01000000, /* SCRambler mode */
93 Mbo = 0x02000000, /* Must Be One */
94 Ra = 0x40000000, /* Receive All */
95 Sc = 0x80000000, /* Special Capture effect enable */
97 TrMODE = Tr512, /* default transmission threshold */
100 enum { /* CSR9 - ROM and MII Management */
101 Scs = 0x00000001, /* serial ROM chip select */
102 Sclk = 0x00000002, /* serial ROM clock */
103 Sdi = 0x00000004, /* serial ROM data in */
104 Sdo = 0x00000008, /* serial ROM data out */
105 Ss = 0x00000800, /* serial ROM select */
106 Wr = 0x00002000, /* write */
107 Rd = 0x00004000, /* read */
109 Mdc = 0x00010000, /* MII management clock */
110 Mdo = 0x00020000, /* MII management write data */
111 Mii = 0x00040000, /* MII management operation mode (W) */
112 Mdi = 0x00080000, /* MII management data in */
115 enum { /* CSR12 - General-Purpose Port */
116 Gpc = 0x00000100, /* General Purpose Control */
127 Of = 0x00000001, /* Rx: OverFlow */
128 Ce = 0x00000002, /* Rx: CRC Error */
129 Db = 0x00000004, /* Rx: Dribbling Bit */
130 Re = 0x00000008, /* Rx: Report on MII Error */
131 Rw = 0x00000010, /* Rx: Receive Watchdog */
132 Ft = 0x00000020, /* Rx: Frame Type */
133 Cs = 0x00000040, /* Rx: Collision Seen */
134 Tl = 0x00000080, /* Rx: Frame too Long */
135 Ls = 0x00000100, /* Rx: Last deScriptor */
136 Fs = 0x00000200, /* Rx: First deScriptor */
137 Mf = 0x00000400, /* Rx: Multicast Frame */
138 Rf = 0x00000800, /* Rx: Runt Frame */
139 Dt = 0x00003000, /* Rx: Data Type (field) */
140 De = 0x00004000, /* Rx: Descriptor Error */
141 Fl = 0x3FFF0000, /* Rx: Frame Length (field) */
142 Ff = 0x40000000, /* Rx: Filtering Fail */
144 Def = 0x00000001, /* Tx: DEFerred */
145 Uf = 0x00000002, /* Tx: UnderFlow error */
146 Lf = 0x00000004, /* Tx: Link Fail report */
147 Cc = 0x00000078, /* Tx: Collision Count (field) */
148 Hf = 0x00000080, /* Tx: Heartbeat Fail */
149 Ec = 0x00000100, /* Tx: Excessive Collisions */
150 Lc = 0x00000200, /* Tx: Late Collision */
151 Nc = 0x00000400, /* Tx: No Carrier */
152 Lo = 0x00000800, /* Tx: LOss of carrier */
153 To = 0x00004000, /* Tx: Transmission jabber timeOut */
155 Es = 0x00008000, /* [RT]x: Error Summary */
156 Own = 0x80000000, /* [RT]x: OWN bit */
160 Bs1 = 0x000007FF, /* [RT]x: Buffer 1 Size */
161 Bs2 = 0x003FF800, /* [RT]x: Buffer 2 Size */
163 Ch = 0x01000000, /* [RT]x: second address CHained */
164 Er = 0x02000000, /* [RT]x: End of Ring */
166 Ft0 = 0x00400000, /* Tx: Filtering Type 0 */
167 Dpd = 0x00800000, /* Tx: Disabled PaDding */
168 Ac = 0x04000000, /* Tx: Add CRC disable */
169 Set = 0x08000000, /* Tx: SETup packet */
170 Ft1 = 0x10000000, /* Tx: Filtering Type 1 */
171 Fseg = 0x20000000, /* Tx: First SEGment */
172 Lseg = 0x40000000, /* Tx: Last SEGment */
173 Ic = 0x80000000, /* Tx: Interrupt on Completion */
176 enum { /* PHY registers */
177 Bmcr = 0, /* Basic Mode Control */
178 Bmsr = 1, /* Basic Mode Status */
179 Phyidr1 = 2, /* PHY Identifier #1 */
180 Phyidr2 = 3, /* PHY Identifier #2 */
181 Anar = 4, /* Auto-Negotiation Advertisment */
182 Anlpar = 5, /* Auto-Negotiation Link Partner Ability */
183 Aner = 6, /* Auto-Negotiation Expansion */
186 enum { /* Variants */
187 Tulip0 = (0x0009<<16)|0x1011,
188 Tulip3 = (0x0019<<16)|0x1011,
189 Pnic = (0x0002<<16)|0x11AD,
190 Pnic2 = (0xC115<<16)|0x11AD,
193 typedef struct Ctlr Ctlr;
194 typedef struct Ctlr {
199 int id; /* (pcidev->did<<16)|pcidev->vid */
202 int sromsz; /* address size in bits */
203 uchar* sromea; /* MAC address */
205 int sct; /* selected connection type */
206 int k; /* info block count */
207 uchar* infoblock[16];
208 int sctk; /* sct block index */
209 int curk; /* current block index */
212 int phy[32]; /* logical to physical map */
213 int phyreset; /* reset bitmap */
218 uchar fd; /* option */
219 int medium; /* option */
221 int csr6; /* CSR6 - operating mode */
222 int mask; /* CSR[57] - interrupt mask */
227 Des* rdr; /* receive descriptor ring */
228 int nrdr; /* size of rdr */
229 int rdrx; /* index into rdr */
232 Des* tdr; /* transmit descriptor ring */
233 int ntdr; /* size of tdr */
234 int tdrh; /* host index into tdr */
235 int tdri; /* interface index into tdr */
236 int ntq; /* descriptors active */
240 ulong of; /* receive statistics */
251 ulong uf; /* transmit statistics */
264 static Ctlr* ctlrhead;
265 static Ctlr* ctlrtail;
267 #define csr32r(c, r) (inl((c)->port+((r)*8)))
268 #define csr32w(c, r, l) (outl((c)->port+((r)*8), (ulong)(l)))
271 promiscuous(void* arg, int on)
275 ctlr = ((Ether*)arg)->ctlr;
281 csr32w(ctlr, 6, ctlr->csr6);
282 iunlock(&ctlr->lock);
292 if(!(ctlr->csr6 & Sr)){
294 csr32w(ctlr, 6, ctlr->csr6);
296 iunlock(&ctlr->lock);
300 ifstat(Ether* ether, void* a, long n, ulong offset)
308 ether->crcs = ctlr->ce;
309 ether->frames = ctlr->rf+ctlr->cs;
310 ether->buffs = ctlr->de+ctlr->tl;
311 ether->overflows = ctlr->of;
317 l = snprint(p, READSTR, "Overflow: %lud\n", ctlr->of);
318 l += snprint(p+l, READSTR-l, "Ru: %lud\n", ctlr->ru);
319 l += snprint(p+l, READSTR-l, "Rps: %lud\n", ctlr->rps);
320 l += snprint(p+l, READSTR-l, "Rwt: %lud\n", ctlr->rwt);
321 l += snprint(p+l, READSTR-l, "Tps: %lud\n", ctlr->tps);
322 l += snprint(p+l, READSTR-l, "Tu: %lud\n", ctlr->tu);
323 l += snprint(p+l, READSTR-l, "Tjt: %lud\n", ctlr->tjt);
324 l += snprint(p+l, READSTR-l, "Unf: %lud\n", ctlr->unf);
325 l += snprint(p+l, READSTR-l, "CRC Error: %lud\n", ctlr->ce);
326 l += snprint(p+l, READSTR-l, "Collision Seen: %lud\n", ctlr->cs);
327 l += snprint(p+l, READSTR-l, "Frame Too Long: %lud\n", ctlr->tl);
328 l += snprint(p+l, READSTR-l, "Runt Frame: %lud\n", ctlr->rf);
329 l += snprint(p+l, READSTR-l, "Descriptor Error: %lud\n", ctlr->de);
330 l += snprint(p+l, READSTR-l, "Underflow Error: %lud\n", ctlr->uf);
331 l += snprint(p+l, READSTR-l, "Excessive Collisions: %lud\n", ctlr->ec);
332 l += snprint(p+l, READSTR-l, "Late Collision: %lud\n", ctlr->lc);
333 l += snprint(p+l, READSTR-l, "No Carrier: %lud\n", ctlr->nc);
334 l += snprint(p+l, READSTR-l, "Loss of Carrier: %lud\n", ctlr->lo);
335 l += snprint(p+l, READSTR-l, "Transmit Jabber Timeout: %lud\n",
337 l += snprint(p+l, READSTR-l, "csr6: %luX %uX\n", csr32r(ctlr, 6),
339 snprint(p+l, READSTR-l, "ntqmax: %d\n", ctlr->ntqmax);
342 len = readstr(offset, buf, n, p);
350 l = snprint(p, READSTR, "srom:");
351 for(i = 0; i < (1<<(ctlr->sromsz)*sizeof(ushort)); i++){
352 if(i && ((i & 0x0F) == 0))
353 l += snprint(p+l, READSTR-l, "\n ");
354 l += snprint(p+l, READSTR-l, " %2.2uX", ctlr->srom[i]);
357 snprint(p+l, READSTR-l, "\n");
358 len += readstr(offset, buf, n, p);
365 txstart(Ether* ether)
373 while(ctlr->ntq < (ctlr->ntdr-1)){
377 control = Ic|Set|BLEN(bp);
380 bp = qget(ether->oq);
383 control = Ic|Lseg|Fseg|BLEN(bp);
386 ctlr->tdr[PREV(ctlr->tdrh, ctlr->ntdr)].control &= ~Ic;
387 des = &ctlr->tdr[ctlr->tdrh];
389 des->addr = PCIWADDR(bp->rp);
390 des->control |= control;
395 ctlr->tdrh = NEXT(ctlr->tdrh, ctlr->ntdr);
398 if(ctlr->ntq > ctlr->ntqmax)
399 ctlr->ntqmax = ctlr->ntq;
403 transmit(Ether* ether)
410 iunlock(&ctlr->tlock);
414 interrupt(Ureg*, void* arg)
425 while((status = csr32r(ctlr, 5)) & (Nis|Ais)){
427 * Acknowledge the interrupts and mask-out
428 * the ones that are implicitly handled.
430 csr32w(ctlr, 5, status);
431 status &= (ctlr->mask & ~(Nis|Ti));
446 status &= ~(Ais|Rwt|Rps|Ru|Tjt|Tu|Tps);
453 des = &ctlr->rdr[ctlr->rdrx];
454 while(!(des->status & Own)){
455 if(des->status & Es){
469 else if(bp = iallocb(Rbsz)){
470 len = ((des->status & Fl)>>16)-4;
471 des->bp->wp = des->bp->rp+len;
472 etheriq(ether, des->bp, 1);
474 des->addr = PCIWADDR(bp->rp);
478 des->control |= Rbsz;
482 ctlr->rdrx = NEXT(ctlr->rdrx, ctlr->nrdr);
483 des = &ctlr->rdr[ctlr->rdrx];
489 * Check the transmit side:
490 * check for Transmit Underflow and Adjust
491 * the threshold upwards;
492 * free any transmitted buffers and try to
498 csr32w(ctlr, 6, ctlr->csr6 & ~St);
499 switch(ctlr->csr6 & Tr){
514 ctlr->csr6 = (ctlr->csr6 & ~Tr)|len;
515 csr32w(ctlr, 6, ctlr->csr6);
516 iunlock(&ctlr->lock);
517 csr32w(ctlr, 5, Tps);
518 status &= ~(Unf|Tps);
523 des = &ctlr->tdr[ctlr->tdri];
524 if(des->status & Own)
527 if(des->status & Es){
547 ctlr->tdri = NEXT(ctlr->tdri, ctlr->ntdr);
550 iunlock(&ctlr->tlock);
553 * Anything left not catered for?
556 panic("#l%d: status %8.8uX\n", ether->ctlrno, status);
561 ctlrinit(Ether* ether)
567 uchar bi[Eaddrlen*2];
572 * Allocate and initialise the receive ring;
573 * allocate and initialise the transmit ring;
574 * unmask interrupts and start the transmit side;
575 * create and post a setup packet to initialise
576 * the physical ethernet address.
578 ctlr->rdr = xspanalloc(ctlr->nrdr*sizeof(Des), 8*sizeof(ulong), 0);
579 for(des = ctlr->rdr; des < &ctlr->rdr[ctlr->nrdr]; des++){
580 des->bp = iallocb(Rbsz);
582 panic("can't allocate ethernet receive ring\n");
585 des->addr = PCIWADDR(des->bp->rp);
587 ctlr->rdr[ctlr->nrdr-1].control |= Er;
589 csr32w(ctlr, 3, PCIWADDR(ctlr->rdr));
591 ctlr->tdr = xspanalloc(ctlr->ntdr*sizeof(Des), 8*sizeof(ulong), 0);
592 ctlr->tdr[ctlr->ntdr-1].control |= Er;
595 csr32w(ctlr, 4, PCIWADDR(ctlr->tdr));
598 * Clear any bits in the Status Register (CSR5) as
599 * the PNIC has a different reset value from a true 2114x.
601 ctlr->mask = Nis|Ais|Fbe|Rwt|Rps|Ru|Ri|Unf|Tjt|Tps|Ti;
602 csr32w(ctlr, 5, ctlr->mask);
603 csr32w(ctlr, 7, ctlr->mask);
605 csr32w(ctlr, 6, ctlr->csr6);
607 for(i = 0; i < Eaddrlen/2; i++){
608 bi[i*4] = ether->ea[i*2];
609 bi[i*4+1] = ether->ea[i*2+1];
610 bi[i*4+2] = ether->ea[i*2+1];
611 bi[i*4+3] = ether->ea[i*2];
613 bp = iallocb(Eaddrlen*2*16);
615 panic("can't allocate ethernet setup buffer\n");
616 memset(bp->rp, 0xFF, sizeof(bi));
617 for(i = sizeof(bi); i < sizeof(bi)*16; i += sizeof(bi))
618 memmove(bp->rp+i, bi, sizeof(bi));
619 bp->wp += sizeof(bi)*16;
622 ether->oq = qopen(256*1024, Qmsg, 0, 0);
627 csr9w(Ctlr* ctlr, int data)
629 csr32w(ctlr, 9, data);
634 miimdi(Ctlr* ctlr, int n)
639 * Read n bits from the MII Management Register.
642 for(i = n-1; i >= 0; i--){
643 if(csr32r(ctlr, 9) & Mdi)
645 csr9w(ctlr, Mii|Mdc);
654 miimdo(Ctlr* ctlr, int bits, int n)
659 * Write n bits to the MII Management Register.
661 for(i = n-1; i >= 0; i--){
667 csr9w(ctlr, mdo|Mdc);
673 miir(Ctlr* ctlr, int phyad, int regad)
677 if(ctlr->id == Pnic){
679 csr32w(ctlr, 20, 0x60020000|(phyad<<23)|(regad<<18));
682 data = csr32r(ctlr, 20);
683 }while((data & 0x80000000) && --i);
687 return data & 0xFFFF;
695 miimdo(ctlr, 0xFFFFFFFF, 32);
696 miimdo(ctlr, 0x1800|(phyad<<5)|regad, 14);
697 data = miimdi(ctlr, 18);
702 return data & 0xFFFF;
706 miiw(Ctlr* ctlr, int phyad, int regad, int data)
710 * ST+OP+PHYAD+REGAD+TA + 16 data bits;
713 miimdo(ctlr, 0xFFFFFFFF, 32);
715 data |= (0x05<<(5+5+2+16))|(phyad<<(5+2+16))|(regad<<(2+16))|(0x02<<16);
716 miimdo(ctlr, data, 32);
722 sromr(Ctlr* ctlr, int r)
724 int i, op, data, size;
726 if(ctlr->id == Pnic){
728 csr32w(ctlr, 19, 0x600|r);
731 data = csr32r(ctlr, 19);
732 }while((data & 0x80000000) && --i);
734 if(ctlr->sromsz == 0)
737 return csr32r(ctlr, 9) & 0xFFFF;
741 * This sequence for reading a 16-bit register 'r'
742 * in the EEPROM is taken straight from Section
743 * 7.4 of the 21140 Hardware Reference Manual.
747 csr9w(ctlr, Rd|Ss|Scs);
748 csr9w(ctlr, Rd|Ss|Sclk|Scs);
752 for(i = 3-1; i >= 0; i--){
753 data = Rd|Ss|(((op>>i) & 0x01)<<2)|Scs;
755 csr9w(ctlr, data|Sclk);
760 * First time through must work out the EEPROM size.
762 if((size = ctlr->sromsz) == 0)
765 for(size = size-1; size >= 0; size--){
766 data = Rd|Ss|(((r>>size) & 0x01)<<2)|Scs;
768 csr9w(ctlr, data|Sclk);
771 if(!(csr32r(ctlr, 9) & Sdo))
776 for(i = 16-1; i >= 0; i--){
777 csr9w(ctlr, Rd|Ss|Sclk|Scs);
778 if(csr32r(ctlr, 9) & Sdo)
780 csr9w(ctlr, Rd|Ss|Scs);
785 if(ctlr->sromsz == 0){
786 ctlr->sromsz = 8-size;
790 return data & 0xFFFF;
794 softreset(Ctlr* ctlr)
797 * Soft-reset the controller and initialise bus mode.
798 * Delay should be >= 50 PCI cycles (2×S @ 25MHz).
800 csr32w(ctlr, 0, Swr);
802 csr32w(ctlr, 0, Rml|Cal16|Dbo);
807 type5block(Ctlr* ctlr, uchar* block)
812 * Reset or GPR sequence. Reset should be once only,
813 * before the GPR sequence.
814 * Note 'block' is not a pointer to the block head but
815 * a pointer to the data in the block starting at the
816 * reset length value so type5block can be used for the
817 * sequences contained in type 1 and type 3 blocks.
818 * The SROM docs state the 21140 type 5 block is the
819 * same as that for the 21143, but the two controllers
820 * use different registers and sequence-element lengths
821 * so the 21140 code here is a guess for a real type 5
825 if(ctlr->id != Tulip3){
826 for(i = 0; i < len; i++){
827 csr32w(ctlr, 12, *block);
833 for(i = 0; i < len; i++){
834 csr15 = *block++<<16;
835 csr15 |= *block++<<24;
836 csr32w(ctlr, 15, csr15);
837 debug("%8.8uX ", csr15);
843 typephylink(Ctlr* ctlr, uchar*)
845 int an, bmcr, bmsr, csr6, x;
849 * auto-negotiataion enabled but not complete;
850 * no valid link established.
852 bmcr = miir(ctlr, ctlr->curphyad, Bmcr);
853 miir(ctlr, ctlr->curphyad, Bmsr);
854 bmsr = miir(ctlr, ctlr->curphyad, Bmsr);
855 debug("bmcr 0x%2.2uX bmsr 0x%2.2uX\n", bmcr, bmsr);
856 if(((bmcr & 0x1000) && !(bmsr & 0x0020)) || !(bmsr & 0x0004))
860 an = miir(ctlr, ctlr->curphyad, Anar);
861 an &= miir(ctlr, ctlr->curphyad, Anlpar) & 0x3E0;
862 debug("an 0x%2.uX 0x%2.2uX 0x%2.2uX\n",
863 miir(ctlr, ctlr->curphyad, Anar),
864 miir(ctlr, ctlr->curphyad, Anlpar),
878 else if((bmcr & 0x2100) == 0x2100)
880 else if(bmcr & 0x2000){
882 * If FD capable, force it if necessary.
884 if((bmsr & 0x4000) && ctlr->fd){
885 miiw(ctlr, ctlr->curphyad, Bmcr, 0x2100);
891 else if(bmcr & 0x0100)
896 csr6 = Sc|Mbo|Hbd|Ps|Ca|Sb|TrMODE;
901 debug("csr6 0x%8.8uX 0x%8.8uX 0x%8.8luX\n",
902 csr6, ctlr->csr6, csr32r(ctlr, 6));
903 if(csr6 != ctlr->csr6){
905 csr32w(ctlr, 6, csr6);
912 typephymode(Ctlr* ctlr, uchar* block, int wait)
915 int len, mc, nway, phyx, timeo;
920 len = (block[0] & ~0x80)+1;
921 for(i = 0; i < len; i++)
922 debug("%2.2uX ", block[i]);
928 else if(block[1] == 3)
934 * Snarf the media capabilities, nway advertisment,
935 * FDX and TTM bitmaps.
937 p = &block[5+len*block[3]+len*block[4+len*block[3]]];
943 ctlr->fdx |= *p++<<8;
946 debug("mc %4.4uX nway %4.4uX fdx %4.4uX ttm %4.4uX\n",
947 mc, nway, ctlr->fdx, ctlr->ttm);
951 ctlr->curphyad = ctlr->phy[phyx];
953 ctlr->csr6 = 0;//Sc|Mbo|Hbd|Ps|Ca|Sb|TrMODE;
954 //csr32w(ctlr, 6, ctlr->csr6);
955 if(typephylink(ctlr, block))
958 if(!(ctlr->phyreset & (1<<phyx))){
959 debug("reset seq: len %d: ", block[3]);
961 type5block(ctlr, &ctlr->type5block[2]);
963 type5block(ctlr, &block[4+len*block[3]]);
965 ctlr->phyreset |= (1<<phyx);
971 debug("gpr seq: len %d: ", block[3]);
972 type5block(ctlr, &block[3]);
975 ctlr->csr6 = 0;//Sc|Mbo|Hbd|Ps|Ca|Sb|TrMODE;
976 //csr32w(ctlr, 6, ctlr->csr6);
977 if(typephylink(ctlr, block))
981 * Turn off auto-negotiation, set the auto-negotiation
982 * advertisment register then start the auto-negotiation
985 miiw(ctlr, ctlr->curphyad, Bmcr, 0);
986 miiw(ctlr, ctlr->curphyad, Anar, nway|1);
987 miiw(ctlr, ctlr->curphyad, Bmcr, 0x1000);
992 for(timeo = 0; timeo < 30; timeo++){
993 if(typephylink(ctlr, block))
1002 typesymmode(Ctlr *ctlr, uchar *block, int wait)
1004 uint gpmode, gpdata, command;
1007 gpmode = block[3] | ((uint) block[4] << 8);
1008 gpdata = block[5] | ((uint) block[6] << 8);
1009 command = (block[7] | ((uint) block[8] << 8)) & 0x71;
1010 if (command & 0x8000) {
1011 print("ether2114x.c: FIXME: handle type 4 mode blocks where cmd.active_invalid != 0\n");
1014 csr32w(ctlr, 15, gpmode);
1015 csr32w(ctlr, 15, gpdata);
1016 ctlr->csr6 = (command & 0x71) << 18;
1017 csr32w(ctlr, 6, ctlr->csr6);
1022 type2mode(Ctlr* ctlr, uchar* block, int)
1025 int csr6, csr13, csr14, csr15, gpc, gpd;
1027 csr6 = Sc|Mbo|Ca|Sb|TrMODE;
1028 debug("type2mode: medium 0x%2.2uX\n", block[2]);
1031 * Don't attempt full-duplex
1032 * unless explicitly requested.
1034 if((block[2] & 0x3F) == 0x04){ /* 10BASE-TFD */
1041 * Operating mode programming values from the datasheet
1042 * unless media specific data is explicitly given.
1045 if(block[2] & 0x40){
1046 csr13 = (block[4]<<8)|block[3];
1047 csr14 = (block[6]<<8)|block[5];
1048 csr15 = (block[8]<<8)|block[7];
1051 else switch(block[2] & 0x3F){
1054 case 0x00: /* 10BASE-T */
1059 case 0x01: /* 10BASE-2 */
1064 case 0x02: /* 10BASE-5 (AUI) */
1069 case 0x04: /* 10BASE-TFD */
1080 csr32w(ctlr, 13, 0);
1081 csr32w(ctlr, 14, csr14);
1082 csr32w(ctlr, 15, gpc|csr15);
1084 csr32w(ctlr, 15, gpd|csr15);
1085 csr32w(ctlr, 13, csr13);
1088 csr32w(ctlr, 6, ctlr->csr6);
1090 debug("type2mode: csr13 %8.8uX csr14 %8.8uX csr15 %8.8uX\n",
1091 csr13, csr14, csr15);
1092 debug("type2mode: gpc %8.8uX gpd %8.8uX csr6 %8.8uX\n",
1099 type0link(Ctlr* ctlr, uchar* block)
1101 int m, polarity, sense;
1103 m = (block[3]<<8)|block[2];
1104 sense = 1<<((m & 0x000E)>>1);
1110 return (csr32r(ctlr, 12) & sense)^polarity;
1114 type0mode(Ctlr* ctlr, uchar* block, int wait)
1118 csr6 = Sc|Mbo|Hbd|Ca|Sb|TrMODE;
1119 debug("type0: medium 0x%uX, fd %d: 0x%2.2uX 0x%2.2uX 0x%2.2uX 0x%2.2uX\n",
1120 ctlr->medium, ctlr->fd, block[0], block[1], block[2], block[3]);
1125 case 0x04: /* 10BASE-TFD */
1126 case 0x05: /* 100BASE-TXFD */
1127 case 0x08: /* 100BASE-FXFD */
1129 * Don't attempt full-duplex
1130 * unless explicitly requested.
1138 m = (block[3]<<8)|block[2];
1148 csr32w(ctlr, 12, block[1]);
1150 csr32w(ctlr, 6, csr6);
1156 for(timeo = 0; timeo < 30; timeo++){
1157 if(type0link(ctlr, block))
1166 mediaxx(Ether* ether, int wait)
1172 block = ctlr->infoblock[ctlr->curk];
1173 if(block[0] & 0x80){
1178 if(ctlr->medium >= 0 && block[2] != ctlr->medium)
1180 /* need this test? */ if(ctlr->sct != 0x0800 && (ctlr->sct & 0x3F) != block[2])
1182 if(type0mode(ctlr, block+2, wait))
1186 if(typephymode(ctlr, block, wait))
1190 debug("type2: medium %d block[2] %d\n",
1191 ctlr->medium, block[2]);
1192 if(ctlr->medium >= 0 && ((block[2] & 0x3F) != ctlr->medium))
1194 if(type2mode(ctlr, block, wait))
1198 if(typephymode(ctlr, block, wait))
1202 debug("type4: medium %d block[2] %d\n",
1203 ctlr->medium, block[2]);
1204 if(ctlr->medium >= 0 && ((block[2] & 0x3F) != ctlr->medium))
1206 if(typesymmode(ctlr, block, wait))
1212 if(ctlr->medium >= 0 && block[0] != ctlr->medium)
1214 /* need this test? */if(ctlr->sct != 0x0800 && (ctlr->sct & 0x3F) != block[0])
1216 if(type0mode(ctlr, block, wait))
1221 if(!(ctlr->csr6 & Ps) || (ctlr->csr6 & Ttm))
1230 media(Ether* ether, int wait)
1236 for(k = 0; k < ctlr->k; k++){
1237 mbps = mediaxx(ether, wait);
1241 ctlr->curk = ctlr->k-1;
1249 static char* mediatable[9] = {
1250 "10BASE-T", /* TP */
1251 "10BASE-2", /* BNC */
1252 "10BASE-5", /* AUI */
1261 static uchar en1207[] = { /* Accton EN1207-COMBO */
1262 0x00, 0x00, 0xE8, /* [0] vendor ethernet code */
1263 0x00, /* [3] spare */
1265 0x00, 0x08, /* [4] connection (LSB+MSB = 0x0800) */
1266 0x1F, /* [6] general purpose control */
1267 2, /* [7] block count */
1269 0x00, /* [8] media code (10BASE-TX) */
1270 0x0B, /* [9] general purpose port data */
1271 0x9E, 0x00, /* [10] command (LSB+MSB = 0x009E) */
1273 0x03, /* [8] media code (100BASE-TX) */
1274 0x1B, /* [9] general purpose port data */
1275 0x6D, 0x00, /* [10] command (LSB+MSB = 0x006D) */
1277 /* There is 10BASE-2 as well, but... */
1280 static uchar ana6910fx[] = { /* Adaptec (Cogent) ANA-6910FX */
1281 0x00, 0x00, 0x92, /* [0] vendor ethernet code */
1282 0x00, /* [3] spare */
1284 0x00, 0x08, /* [4] connection (LSB+MSB = 0x0800) */
1285 0x3F, /* [6] general purpose control */
1286 1, /* [7] block count */
1288 0x07, /* [8] media code (100BASE-FX) */
1289 0x03, /* [9] general purpose port data */
1290 0x2D, 0x00 /* [10] command (LSB+MSB = 0x000D) */
1293 static uchar smc9332[] = { /* SMC 9332 */
1294 0x00, 0x00, 0xC0, /* [0] vendor ethernet code */
1295 0x00, /* [3] spare */
1297 0x00, 0x08, /* [4] connection (LSB+MSB = 0x0800) */
1298 0x1F, /* [6] general purpose control */
1299 2, /* [7] block count */
1301 0x00, /* [8] media code (10BASE-TX) */
1302 0x00, /* [9] general purpose port data */
1303 0x9E, 0x00, /* [10] command (LSB+MSB = 0x009E) */
1305 0x03, /* [8] media code (100BASE-TX) */
1306 0x09, /* [9] general purpose port data */
1307 0x6D, 0x00, /* [10] command (LSB+MSB = 0x006D) */
1310 static uchar* leaf21140[] = {
1311 en1207, /* Accton EN1207-COMBO */
1312 ana6910fx, /* Adaptec (Cogent) ANA-6910FX */
1313 smc9332, /* SMC 9332 */
1318 * Copied to ctlr->srom at offset 20.
1320 static uchar leafpnic[] = {
1321 0x00, 0x00, 0x00, 0x00, /* MAC address */
1323 0x00, /* controller 0 device number */
1324 0x1E, 0x00, /* controller 0 info leaf offset */
1325 0x00, /* reserved */
1326 0x00, 0x08, /* selected connection type */
1327 0x00, /* general purpose control */
1328 0x01, /* block count */
1330 0x8C, /* format indicator and count */
1331 0x01, /* block type */
1332 0x00, /* PHY number */
1333 0x00, /* GPR sequence length */
1334 0x00, /* reset sequence length */
1335 0x00, 0x78, /* media capabilities */
1336 0xE0, 0x01, /* Nway advertisment */
1337 0x00, 0x50, /* FDX bitmap */
1338 0x00, 0x18, /* TTM bitmap */
1344 int i, k, oui, phy, x;
1348 * This is a partial decoding of the SROM format described in
1349 * 'Digital Semiconductor 21X4 Serial ROM Format, Version 4.05,
1350 * 2-Mar-98'. Only the 2114[03] are handled, support for other
1351 * controllers can be added as needed.
1352 * Do a dummy read first to get the size and allocate ctlr->srom.
1355 if(ctlr->srom == nil)
1356 ctlr->srom = malloc((1<<ctlr->sromsz)*sizeof(ushort));
1357 for(i = 0; i < (1<<ctlr->sromsz); i++){
1359 ctlr->srom[2*i] = x;
1360 ctlr->srom[2*i+1] = x>>8;
1364 * There are 2 SROM layouts:
1365 * e.g. Digital EtherWORKS station address at offset 20;
1366 * this complies with the 21140A SROM
1367 * application note from Digital;
1368 * e.g. SMC9332 station address at offset 0 followed by
1369 * 2 additional bytes, repeated at offset
1370 * 6; the 8 bytes are also repeated in
1371 * reverse order at offset 8.
1372 * To check which it is, read the SROM and check for the repeating
1373 * patterns of the non-compliant cards; if that fails use the one at
1376 ctlr->sromea = ctlr->srom;
1377 for(i = 0; i < 8; i++){
1379 if(x != ctlr->srom[15-i] || x != ctlr->srom[16+i]){
1380 ctlr->sromea = &ctlr->srom[20];
1386 * Fake up the SROM for the PNIC.
1387 * It looks like a 21140 with a PHY.
1388 * The MAC address is byte-swapped in the orginal SROM data.
1390 if(ctlr->id == Pnic){
1391 memmove(&ctlr->srom[20], leafpnic, sizeof(leafpnic));
1392 for(i = 0; i < Eaddrlen; i += 2){
1393 ctlr->srom[20+i] = ctlr->srom[i+1];
1394 ctlr->srom[20+i+1] = ctlr->srom[i];
1399 * Next, try to find the info leaf in the SROM for media detection.
1400 * If it's a non-conforming card try to match the vendor ethernet code
1401 * and point p at a fake info leaf with compact 21140 entries.
1403 if(ctlr->sromea == ctlr->srom){
1405 for(i = 0; leaf21140[i] != nil; i++){
1406 if(memcmp(leaf21140[i], ctlr->sromea, 3) == 0){
1407 p = &leaf21140[i][4];
1415 p = &ctlr->srom[(ctlr->srom[28]<<8)|ctlr->srom[27]];
1418 * Set up the info needed for later media detection.
1419 * For the 21140, set the general-purpose mask in CSR12.
1420 * The info block entries are stored in order of increasing
1421 * precedence, so detection will work backwards through the
1422 * stored indexes into ctlr->srom.
1423 * If an entry is found which matches the selected connection
1424 * type, save the index. Otherwise, start at the last entry.
1425 * If any MII entries are found (type 1 and 3 blocks), scan
1430 ctlr->sct |= *p++<<8;
1431 if(ctlr->id != Tulip3){
1432 csr32w(ctlr, 12, Gpc|*p++);
1436 if(ctlr->k >= nelem(ctlr->infoblock))
1437 ctlr->k = nelem(ctlr->infoblock)-1;
1438 ctlr->sctk = ctlr->k-1;
1440 for(k = 0; k < ctlr->k; k++){
1441 ctlr->infoblock[k] = p;
1443 * The RAMIX PMC665 has a badly-coded SROM,
1444 * hence the test for 21143 and type 3.
1446 if((*p & 0x80) || (ctlr->id == Tulip3 && *(p+1) == 3)){
1448 if(*(p+1) == 1 || *(p+1) == 3)
1451 ctlr->type5block = p;
1452 p += (*p & ~0x80)+1;
1455 debug("type0: 0x%2.2uX 0x%2.2uX 0x%2.2uX 0x%2.2uX\n",
1456 p[0], p[1], p[2], p[3]);
1457 if(ctlr->sct != 0x0800 && *p == (ctlr->sct & 0xFF))
1462 ctlr->curk = ctlr->sctk;
1463 debug("sct 0x%uX medium 0x%uX k %d curk %d phy %d\n",
1464 ctlr->sct, ctlr->medium, ctlr->k, ctlr->curk, phy);
1468 for(k = 0; k < nelem(ctlr->phy); k++){
1469 if((oui = miir(ctlr, k, 2)) == -1 || oui == 0)
1472 oui = (oui & 0x3FF)<<6;
1473 oui |= miir(ctlr, k, 3)>>10;
1475 debug("phy%d: index %d oui %uX reg1 %uX\n",
1476 x, k, oui, miir(ctlr, k, 1));
1497 while(p = pcimatch(p, 0, 0)){
1498 if(p->ccrb != 0x02 || p->ccru != 0)
1500 switch((p->did<<16)|p->vid){
1504 case Tulip3: /* 21143 */
1508 x = pcicfgr32(p, 0x40);
1510 pcicfgw32(p, 0x40, x);
1513 case Pnic: /* PNIC */
1514 case Pnic2: /* PNIC-II */
1515 case Tulip0: /* 21140 */
1520 * bar[0] is the I/O port register address and
1521 * bar[1] is the memory-mapped register address.
1523 ctlr = malloc(sizeof(Ctlr));
1524 ctlr->port = p->mem[0].bar & ~0x01;
1526 ctlr->id = (p->did<<16)|p->vid;
1528 if(ioalloc(ctlr->port, p->mem[0].size, 0, "dec2114x") < 0){
1529 print("dec2114x: port 0x%uX in use\n", ctlr->port);
1535 * Some cards (e.g. ANA-6910FX) seem to need the Ps bit
1536 * set or they don't always work right after a hardware
1539 csr32w(ctlr, 6, Mbo|Ps);
1552 case Pnic: /* PNIC */
1554 * Turn off the jabber timer.
1556 csr32w(ctlr, 15, 0x00000001);
1561 ctlrtail->next = ctlr;
1574 static int scandone;
1582 * Any adapter matches if no ether->port is supplied,
1583 * otherwise the ports must match.
1585 for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
1588 if(ether->port == 0 || ether->port == ctlr->port){
1597 ether->port = ctlr->port;
1598 // ether->irq = ctlr->pcidev->intl;
1599 ether->irq = 2; /* arrrrrgh */
1600 ether->tbdf = ctlr->pcidev->tbdf;
1603 * Check if the adapter's station address is to be overridden.
1604 * If not, read it from the EEPROM and set in ether->ea prior to
1605 * loading the station address in the hardware.
1607 memset(ea, 0, Eaddrlen);
1608 if(memcmp(ea, ether->ea, Eaddrlen) == 0)
1609 memmove(ether->ea, ctlr->sromea, Eaddrlen);
1612 * Look for a medium override in case there's no autonegotiation
1613 * (no MII) or the autonegotiation fails.
1615 for(i = 0; i < ether->nopt; i++){
1616 if(cistrcmp(ether->opt[i], "FD") == 0){
1620 for(x = 0; x < nelem(mediatable); x++){
1621 debug("compare <%s> <%s>\n", mediatable[x],
1623 if(cistrcmp(mediatable[x], ether->opt[i]))
1627 switch(ctlr->medium){
1632 case 0x04: /* 10BASE-TFD */
1633 case 0x05: /* 100BASE-TXFD */
1634 case 0x08: /* 100BASE-FXFD */
1642 ether->mbps = media(ether, 1);
1645 * Initialise descriptor rings, ethernet address.
1649 pcisetbme(ctlr->pcidev);
1653 * Linkage to the generic ethernet driver.
1655 ether->attach = attach;
1656 ether->transmit = transmit;
1657 ether->interrupt = interrupt;
1658 ether->ifstat = ifstat;
1661 ether->promiscuous = promiscuous;
1667 ether2114xlink(void)
1669 addethercard("21140", reset);
1670 addethercard("2114x", reset);