2 * Intel 82557 Fast Ethernet PCI Bus LAN Controller
3 * as found on the Intel EtherExpress PRO/100B. This chip is full
4 * of smarts, unfortunately none of them are in the right place.
6 * the PCI scanning code could be made common to other adapters;
7 * PCI code needs rewritten to handle byte, word, dword accesses
8 * and using the devno as a bus+dev+function triplet.
20 Nrfd = 4, /* receive frame area */
22 NullPointer = 0xFFFFFFFF, /* 82557 NULL pointer */
26 Status = 0x00, /* byte or word (word includes Ack) */
27 Ack = 0x01, /* byte */
28 CommandR = 0x02, /* byte or word (word includes Interrupt) */
29 Interrupt = 0x03, /* byte */
30 Pointer = 0x04, /* dword */
31 Port = 0x08, /* dword */
32 Fcr = 0x0C, /* Flash control register */
33 Ecr = 0x0E, /* EEPROM control register */
34 Mcr = 0x10, /* MDI control register */
40 RUnoresources = 0x0008,
42 RUrbd = 0x0020, /* bit */
43 RUstatus = 0x003F, /* mask */
48 CUstatus = 0x00C0, /* mask */
50 StatSWI = 0x0400, /* SoftWare generated Interrupt */
51 StatMDI = 0x0800, /* MDI r/w done */
52 StatRNR = 0x1000, /* Receive unit Not Ready */
53 StatCNA = 0x2000, /* Command unit Not Active (Active->Idle) */
54 StatFR = 0x4000, /* Finished Receiving */
55 StatCX = 0x8000, /* Command eXecuted */
56 StatTNO = 0x8000, /* Transmit NOT OK */
59 enum { /* Command (byte) */
63 LoadDCA = 0x40, /* Load Dump Counters Address */
64 DumpSC = 0x50, /* Dump Statistical Counters */
65 LoadCUB = 0x60, /* Load CU Base */
66 ResetSA = 0x70, /* Dump and Reset Statistical Counters */
71 LoadHDS = 0x05, /* Load Header Data Size */
72 LoadRUB = 0x06, /* Load RU Base */
73 RBDresume = 0x07, /* Resume frame reception */
76 enum { /* Interrupt (byte) */
77 InterruptM = 0x01, /* interrupt Mask */
78 InterruptSI = 0x02, /* Software generated Interrupt */
82 EEsk = 0x01, /* serial clock */
83 EEcs = 0x02, /* chip select */
84 EEdi = 0x04, /* serial data in */
85 EEdo = 0x08, /* serial data out */
87 EEstart = 0x04, /* start bit */
88 EEread = 0x02, /* read opcode */
92 MDIread = 0x08000000, /* read opcode */
93 MDIwrite = 0x04000000, /* write opcode */
94 MDIready = 0x10000000, /* ready bit */
95 MDIie = 0x20000000, /* interrupt enable */
109 RfdCollision = 0x00000001,
110 RfdIA = 0x00000002, /* IA match */
111 RfdRxerr = 0x00000010, /* PHY character error */
112 RfdType = 0x00000020, /* Type frame */
113 RfdRunt = 0x00000080,
114 RfdOverrun = 0x00000100,
115 RfdBuffer = 0x00000200,
116 RfdAlignment = 0x00000400,
119 RfdOK = 0x00002000, /* frame received OK */
120 RfdC = 0x00008000, /* reception Complete */
121 RfdSF = 0x00080000, /* Simplified or Flexible (1) Rfd */
122 RfdH = 0x00100000, /* Header RFD */
124 RfdI = 0x20000000, /* Interrupt after completion */
125 RfdS = 0x40000000, /* Suspend after completion */
126 RfdEL = 0x80000000, /* End of List */
137 uchar data[24]; /* CbIAS + CbConfigure */
140 typedef struct TxCB {
149 enum { /* action command */
150 CbOK = 0x00002000, /* DMA completed OK */
151 CbC = 0x00008000, /* execution Complete */
154 CbIAS = 0x00010000, /* Indvidual Address Setup */
155 CbConfigure = 0x00020000,
156 CbMAS = 0x00030000, /* Multicast Address Setup */
157 CbTransmit = 0x00040000,
159 CbDiagnose = 0x00070000,
160 CbCommand = 0x00070000, /* mask */
162 CbSF = 0x00080000, /* CbTransmit */
164 CbI = 0x20000000, /* Interrupt after completion */
165 CbS = 0x40000000, /* Suspend after completion */
166 CbEL = 0x80000000, /* End of List */
169 enum { /* CbTransmit count */
173 typedef struct Ctlr Ctlr;
174 typedef struct Ctlr {
180 int eepromsz; /* address size in bits */
186 uchar configdata[24];
197 static Ctlr* ctlrhead;
198 static Ctlr* ctlrtail;
200 static uchar configdata[24] = {
201 0x16, /* byte count */
202 0x44, /* Rx/Tx FIFO limit */
203 0x00, /* adaptive IFS */
205 0x04, /* Rx DMA maximum byte count */
206 0x84, /* Tx DMA maximum byte count */
207 0x33, /* late SCB, CNA interrupts */
208 0x01, /* discard short Rx frames */
212 0x2E, /* normal operation, NSAI */
213 0x00, /* linear priority */
214 0x60, /* inter-frame spacing */
217 0x48, /* promiscuous mode off */
220 0xF2, /* transmit padding enable */
221 0x80, /* full duplex pin enable */
222 0x3F, /* no Multi IA */
223 0x05, /* no Multi Cast ALL */
226 #define csr8r(c, r) (inb((c)->port+(r)))
227 #define csr16r(c, r) (ins((c)->port+(r)))
228 #define csr32r(c, r) (inl((c)->port+(r)))
229 #define csr8w(c, r, b) (outb((c)->port+(r), (int)(b)))
230 #define csr16w(c, r, w) (outs((c)->port+(r), (ushort)(w)))
231 #define csr32w(c, r, l) (outl((c)->port+(r), (ulong)(l)))
236 if(ctlr->cbqhead == 0){
242 csr32w(ctlr, Pointer, PADDR(ctlr->cbqhead->rp));
243 while(csr8r(ctlr, CommandR))
245 csr8w(ctlr, CommandR, CUstart);
249 action(Ctlr* ctlr, Block* bp)
257 ctlr->cbqtail->next = bp;
258 cb = (Cb*)ctlr->cbqtail->rp;
259 cb->link = PADDR(bp->rp);
260 cb->command &= ~CbEL;
266 if(ctlr->cbqbusy == 0)
277 status = csr16r(ctlr, Status);
278 if((status & RUstatus) == RUidle){
279 csr32w(ctlr, Pointer, PADDR(&ctlr->rfd[ctlr->rfdx]));
280 while(csr8r(ctlr, CommandR))
282 csr8w(ctlr, CommandR, RUstart);
287 configure(void* arg, int promiscuous)
293 ctlr = ((Ether*)arg)->ctlr;
295 bp = allocb(sizeof(Cb));
297 bp->wp += sizeof(Cb);
299 cb->command = CbConfigure;
300 cb->link = NullPointer;
301 memmove(cb->data, ctlr->configdata, sizeof(ctlr->configdata));
303 cb->data[15] |= 0x01;
308 transmit(Ether* ether)
314 for(tb = ðer->tb[ether->ti]; tb->owner == Interface; tb = ðer->tb[ether->ti]){
315 bp = allocb(tb->len+sizeof(TxCB));
316 txcb = (TxCB*)bp->wp;
317 bp->wp += sizeof(TxCB);
319 txcb->command = CbTransmit;
320 txcb->link = NullPointer;
321 txcb->tbd = NullPointer;
322 txcb->count = CbEOF|tb->len;
326 memmove(bp->wp, tb->pkt, tb->len);
327 memmove(bp->wp+Eaddrlen, ether->ea, Eaddrlen);
330 action(ether->ctlr, bp);
333 ether->ti = NEXT(ether->ti, ether->ntb);
338 interrupt(Ureg*, void* arg)
351 status = csr16r(ctlr, Status);
352 csr8w(ctlr, Ack, (status>>8) & 0xFF);
354 if((status & (StatCX|StatFR|StatCNA|StatRNR)) == 0)
358 rfd = &ctlr->rfd[ctlr->rfdx];
359 while(rfd->field & RfdC){
360 rb = ðer->rb[ether->ri];
361 if(rb->owner == Interface){
363 rb->len = rfd->count & 0x3FFF;
364 memmove(rb->pkt, rfd->d, rfd->count & 0x3FFF);
365 ether->ri = NEXT(ether->ri, ether->nrb);
369 * Reinitialise the frame for reception and bump
370 * the receive frame processing index;
371 * bump the sentinel index, mark the new sentinel
372 * and clear the old sentinel suspend bit;
373 * set bp and rfd for the next receive frame to
378 ctlr->rfdx = NEXT(ctlr->rfdx, Nrfd);
380 rfd = &ctlr->rfd[ctlr->rfdl];
381 ctlr->rfdl = NEXT(ctlr->rfdl, Nrfd);
382 ctlr->rfd[ctlr->rfdl].field |= RfdS;
385 rfd = &ctlr->rfd[ctlr->rfdx];
390 if(status & StatRNR){
391 while(csr8r(ctlr, CommandR))
393 csr8w(ctlr, CommandR, RUresume);
398 if(status & StatCNA){
399 while(bp = ctlr->cbqhead){
400 if((((Cb*)bp->rp)->command & CbC) == 0)
402 ctlr->cbqhead = bp->next;
410 if(status & (StatCX|StatFR|StatCNA|StatRNR|StatMDI|StatSWI))
411 panic("%s#%d: status %uX", ctlr->type, ctlr->ctlrno, status);
423 for(i = Nrfd-1; i >= 0; i--){
429 rfd->rbd = NullPointer;
431 rfd->size = sizeof(Etherpkt);
433 ctlr->rfd[Nrfd-1].link = PADDR(&ctlr->rfd[0]);
436 ctlr->rfd[0].field |= RfdS;
439 memmove(ctlr->configdata, configdata, sizeof(configdata));
443 miir(Ctlr* ctlr, int phyadd, int regadd)
447 csr32w(ctlr, Mcr, MDIread|(phyadd<<21)|(regadd<<16));
449 for(timo = 64; timo; timo--){
450 mcr = csr32r(ctlr, Mcr);
463 miiw(Ctlr* ctlr, int phyadd, int regadd, int data)
467 csr32w(ctlr, Mcr, MDIwrite|(phyadd<<21)|(regadd<<16)|(data & 0xFFFF));
469 for(timo = 64; timo; timo--){
470 mcr = csr32r(ctlr, Mcr);
483 hy93c46r(Ctlr* ctlr, int r)
485 int data, i, op, size;
488 * Hyundai HY93C46 or equivalent serial EEPROM.
489 * This sequence for reading a 16-bit register 'r'
490 * in the EEPROM is taken straight from Section
491 * 3.3.4.2 of the Intel 82557 User's Guide.
494 csr16w(ctlr, Ecr, EEcs);
496 for(i = 2; i >= 0; i--){
497 data = (((op>>i) & 0x01)<<2)|EEcs;
498 csr16w(ctlr, Ecr, data);
499 csr16w(ctlr, Ecr, data|EEsk);
501 csr16w(ctlr, Ecr, data);
506 * First time through must work out the EEPROM size.
508 if((size = ctlr->eepromsz) == 0)
511 for(size = size-1; size >= 0; size--){
512 data = (((r>>size) & 0x01)<<2)|EEcs;
513 csr16w(ctlr, Ecr, data);
514 csr16w(ctlr, Ecr, data|EEsk);
516 csr16w(ctlr, Ecr, data);
518 if(!(csr16r(ctlr, Ecr) & EEdo))
523 for(i = 15; i >= 0; i--){
524 csr16w(ctlr, Ecr, EEcs|EEsk);
526 if(csr16r(ctlr, Ecr) & EEdo)
528 csr16w(ctlr, Ecr, EEcs);
532 csr16w(ctlr, Ecr, 0);
534 if(ctlr->eepromsz == 0){
535 ctlr->eepromsz = 8-size;
536 ctlr->eeprom = malloc((1<<ctlr->eepromsz)*sizeof(ushort));
550 while(p = pcimatch(p, 0x8086, 0)){
554 case 0x1031: /* Intel 82562EM */
555 case 0x103B: /* Intel 82562EM */
556 case 0x103C: /* Intel 82562EM */
557 case 0x1050: /* Intel 82562EZ */
558 case 0x1039: /* Intel 82801BD PRO/100 VE */
559 case 0x103A: /* Intel 82562 PRO/100 VE */
560 case 0x1064: /* Intel 82562 PRO/100 VE */
561 case 0x2449: /* Intel 82562ET */
562 case 0x27DC: /* Intel 82801G PRO/100 VE */
563 case 0x1209: /* Intel 82559ER */
564 case 0x1229: /* Intel 8255[789] */
565 case 0x1030: /* Intel 82559 InBusiness 10/100 */
570 * bar[0] is the memory-mapped register address (4KB),
571 * bar[1] is the I/O port register address (32 bytes) and
572 * bar[2] is for the flash ROM (1MB).
574 ctlr = malloc(sizeof(Ctlr));
575 ctlr->port = p->mem[1].bar & ~0x01;
579 ctlrtail->next = ctlr;
595 csr32w(ctlr, Port, 0);
598 while(csr8r(ctlr, CommandR))
607 for(i = 0; i < 32; i++){
608 if((oui = miir(ctlr, i, 2)) == -1 || oui == 0 || oui == 0xFFFF)
611 x = miir(ctlr, i, 3);
613 //print("phy%d: oui %uX reg1 %uX\n", i, oui, miir(ctlr, i, 1));
616 ctlr->eeprom[6] = 0x07<<8;
617 else if(oui == 0x80017){
619 ctlr->eeprom[6] = 0x0A<<8;
621 ctlr->eeprom[6] = 0x04<<8;
629 i82557reset(Ether* ether)
631 int anar, anlpar, bmcr, bmsr, force, i, phyaddr, x;
643 * Any adapter matches if no ether->port is supplied,
644 * otherwise the ports must match.
646 for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
649 if(ether->port == 0 || ether->port == ctlr->port){
658 * Initialise the Ctlr structure.
659 * Perform a software reset after which need to ensure busmastering
660 * is still enabled. The EtherExpress PRO/100B appears to leave
661 * the PCI configuration alone (see the 'To do' list above) so punt
663 * Load the RUB and CUB registers for linear addressing (0).
666 ether->port = ctlr->port;
667 ether->irq = ctlr->pcidev->intl;
668 ether->tbdf = ctlr->pcidev->tbdf;
669 ctlr->ctlrno = ether->ctlrno;
670 ctlr->type = ether->type;
672 csr32w(ctlr, Port, 0);
675 while(csr8r(ctlr, CommandR))
677 csr32w(ctlr, Pointer, 0);
678 csr8w(ctlr, CommandR, LoadRUB);
679 while(csr8r(ctlr, CommandR))
681 csr8w(ctlr, CommandR, LoadCUB);
684 * Initialise the action and receive frame areas.
690 * Do a dummy read first to get the size
691 * and allocate ctlr->eeprom.
695 for(i = 0; i < (1<<ctlr->eepromsz); i++){
696 x = hy93c46r(ctlr, i);
701 print("#l%d: EEPROM checksum - 0x%4.4uX\n", ether->ctlrno, sum);
704 * Eeprom[6] indicates whether there is a PHY and whether
705 * it's not 10Mb-only, in which case use the given PHY address
706 * to set any PHY specific options and determine the speed.
707 * Unfortunately, sometimes the EEPROM is blank except for
708 * the ether address and checksum; in this case look at the
709 * controller type and if it's am 82558 or 82559 it has an
710 * embedded PHY so scan for that.
711 * If no PHY, assume 82503 (serial) operation.
713 if((ctlr->eeprom[6] & 0x1F00) && !(ctlr->eeprom[6] & 0x8000))
714 phyaddr = ctlr->eeprom[6] & 0x00FF;
716 switch(ctlr->pcidev->rid){
717 case 0x01: /* 82557 A-step */
718 case 0x02: /* 82557 B-step */
719 case 0x03: /* 82557 C-step */
723 case 0x04: /* 82558 A-step */
724 case 0x05: /* 82558 B-step */
725 case 0x06: /* 82559 A-step */
726 case 0x07: /* 82559 B-step */
727 case 0x08: /* 82559 C-step */
728 case 0x09: /* 82559ER A-step */
729 phyaddr = scanphy(ctlr);
734 * Resolve the highest common ability of the two
735 * link partners. In descending order:
736 * 0x0100 100BASE-TX Full Duplex
739 * 0x0040 10BASE-T Full Duplex
742 anar = miir(ctlr, phyaddr, 0x04);
743 anlpar = miir(ctlr, phyaddr, 0x05) & 0x03E0;
751 switch((ctlr->eeprom[6]>>8) & 0x001F){
753 case 0x04: /* DP83840 */
754 case 0x0A: /* DP83840A */
756 * The DP83840[A] requires some tweaking for
757 * reliable operation.
758 * The manual says bit 10 should be unconditionally
759 * set although it supposedly only affects full-duplex
760 * operation (an & 0x0140).
762 x = miir(ctlr, phyaddr, 0x17) & ~0x0520;
764 for(i = 0; i < ether->nopt; i++){
765 if(cistrcmp(ether->opt[i], "congestioncontrol"))
770 miiw(ctlr, phyaddr, 0x17, x);
773 * If the link partner can't autonegotiate, determine
774 * the speed from elsewhere.
777 miir(ctlr, phyaddr, 0x01);
778 bmsr = miir(ctlr, phyaddr, 0x01);
779 x = miir(ctlr, phyaddr, 0x19);
780 if((bmsr & 0x0004) && !(x & 0x0040))
785 case 0x07: /* Intel 82555 */
787 * Auto-negotiation may fail if the other end is
788 * a DP83840A and the cable is short.
790 bmsr = miir(ctlr, phyaddr, 0x01);
791 if((miir(ctlr, phyaddr, 0) & 0x1000) && !(bmsr & 0x0020)){
792 miiw(ctlr, phyaddr, 0x1A, 0x2010);
793 x = miir(ctlr, phyaddr, 0);
794 miiw(ctlr, phyaddr, 0, 0x0200|x);
795 for(i = 0; i < 3000; i++){
797 if(miir(ctlr, phyaddr, 0x01) & 0x0020)
800 miiw(ctlr, phyaddr, 0x1A, 0x2000);
802 anar = miir(ctlr, phyaddr, 0x04);
803 anlpar = miir(ctlr, phyaddr, 0x05) & 0x03E0;
815 * Force speed and duplex if no auto-negotiation.
819 for(i = 0; i < ether->nopt; i++){
820 if(cistrcmp(ether->opt[i], "fullduplex") == 0){
823 ctlr->configdata[19] |= 0x40;
825 else if(cistrcmp(ether->opt[i], "speed") == 0){
827 x = strtol(ðer->opt[i][6], 0, 0);
837 miiw(ctlr, phyaddr, 0x00, bmcr);
840 ctlr->configdata[8] = 1;
841 ctlr->configdata[15] &= ~0x80;
844 ctlr->configdata[8] = 0;
845 ctlr->configdata[15] |= 0x80;
849 * Load the chip configuration
854 * Check if the adapter's station address is to be overridden.
855 * If not, read it from the EEPROM and set in ether->ea prior to loading
856 * the station address with the Individual Address Setup command.
858 memset(ea, 0, Eaddrlen);
859 if(memcmp(ea, ether->ea, Eaddrlen) == 0){
860 for(i = 0; i < Eaddrlen/2; i++){
862 ether->ea[2*i] = x & 0xFF;
863 ether->ea[2*i+1] = (x>>8) & 0xFF;
867 bp = allocb(sizeof(Cb));
869 bp->wp += sizeof(Cb);
872 cb->link = NullPointer;
873 memmove(cb->data, ether->ea, Eaddrlen);
877 * Linkage to the generic ethernet driver.
879 ether->attach = attach;
880 ether->transmit = transmit;
881 ether->interrupt = interrupt;
882 ether->detach = detach;