epic100.c

Go to the documentation of this file.
00001 
00002 /* epic100.c: A SMC 83c170 EPIC/100 fast ethernet driver for Etherboot */
00003 
00004 FILE_LICENCE ( GPL2_OR_LATER );
00005 
00006 /* 05/06/2003   timlegge        Fixed relocation and implemented Multicast */
00007 #define LINUX_OUT_MACROS
00008 
00009 #include "etherboot.h"
00010 #include <gpxe/pci.h>
00011 #include <gpxe/ethernet.h>
00012 #include "nic.h"
00013 #include "console.h"
00014 #include "epic100.h"
00015 
00016 /* Condensed operations for readability */
00017 #define virt_to_le32desc(addr)  cpu_to_le32(virt_to_bus(addr))
00018 #define le32desc_to_virt(addr)  bus_to_virt(le32_to_cpu(addr))
00019 
00020 #define TX_RING_SIZE    2       /* use at least 2 buffers for TX */
00021 #define RX_RING_SIZE    2
00022 
00023 #define PKT_BUF_SZ      1536    /* Size of each temporary Tx/Rx buffer.*/
00024 
00025 /*
00026 #define DEBUG_RX
00027 #define DEBUG_TX
00028 #define DEBUG_EEPROM
00029 */
00030 
00031 #define EPIC_DEBUG 0    /* debug level */
00032 
00033 /* The EPIC100 Rx and Tx buffer descriptors. */
00034 struct epic_rx_desc {
00035     unsigned long status;
00036     unsigned long bufaddr;
00037     unsigned long buflength;
00038     unsigned long next;
00039 };
00040 /* description of the tx descriptors control bits commonly used */
00041 #define TD_STDFLAGS     TD_LASTDESC
00042 
00043 struct epic_tx_desc {
00044     unsigned long status;
00045     unsigned long bufaddr;
00046     unsigned long buflength;
00047     unsigned long  next;
00048 };
00049 
00050 #define delay(nanosec)   do { int _i = 3; while (--_i > 0) \
00051                                      { __SLOW_DOWN_IO; }} while (0)
00052 
00053 static void     epic100_open(void);
00054 static void     epic100_init_ring(void);
00055 static void     epic100_disable(struct nic *nic);
00056 static int      epic100_poll(struct nic *nic, int retrieve);
00057 static void     epic100_transmit(struct nic *nic, const char *destaddr,
00058                                  unsigned int type, unsigned int len, const char *data);
00059 #ifdef  DEBUG_EEPROM
00060 static int      read_eeprom(int location);
00061 #endif
00062 static int      mii_read(int phy_id, int location);
00063 static void     epic100_irq(struct nic *nic, irq_action_t action);
00064 
00065 static struct nic_operations epic100_operations;
00066 
00067 static int      ioaddr;
00068 
00069 static int      command;
00070 static int      intstat;
00071 static int      intmask;
00072 static int      genctl ;
00073 static int      eectl  ;
00074 static int      test   ;
00075 static int      mmctl  ;
00076 static int      mmdata ;
00077 static int      lan0   ;
00078 static int      mc0    ;
00079 static int      rxcon  ;
00080 static int      txcon  ;
00081 static int      prcdar ;
00082 static int      ptcdar ;
00083 static int      eththr ;
00084 
00085 static unsigned int     cur_rx, cur_tx;         /* The next free ring entry */
00086 #ifdef  DEBUG_EEPROM
00087 static unsigned short   eeprom[64];
00088 #endif
00089 static signed char      phys[4];                /* MII device addresses. */
00090 struct {
00091         struct epic_rx_desc     rx_ring[RX_RING_SIZE]
00092         __attribute__ ((aligned(4)));
00093         struct epic_tx_desc     tx_ring[TX_RING_SIZE]
00094         __attribute__ ((aligned(4)));
00095         unsigned char           rx_packet[PKT_BUF_SZ * RX_RING_SIZE];
00096         unsigned char           tx_packet[PKT_BUF_SZ * TX_RING_SIZE];
00097 } epic100_bufs __shared;
00098 #define rx_ring epic100_bufs.rx_ring
00099 #define tx_ring epic100_bufs.tx_ring
00100 #define rx_packet epic100_bufs.rx_packet
00101 #define tx_packet epic100_bufs.tx_packet
00102 
00103 /***********************************************************************/
00104 /*                    Externally visible functions                     */
00105 /***********************************************************************/
00106 
00107 
00108 static int
00109 epic100_probe ( struct nic *nic, struct pci_device *pci ) {
00110 
00111     int i;
00112     unsigned short* ap;
00113     unsigned int phy, phy_idx;
00114 
00115     if (pci->ioaddr == 0)
00116         return 0;
00117 
00118     /* Ideally we would detect all network cards in slot order.  That would
00119        be best done a central PCI probe dispatch, which wouldn't work
00120        well with the current structure.  So instead we detect just the
00121        Epic cards in slot order. */
00122 
00123     ioaddr = pci->ioaddr;
00124 
00125     nic->irqno  = 0;
00126     nic->ioaddr = pci->ioaddr & ~3;
00127 
00128     /* compute all used static epic100 registers address */
00129     command = ioaddr + COMMAND;         /* Control Register */
00130     intstat = ioaddr + INTSTAT;         /* Interrupt Status */
00131     intmask = ioaddr + INTMASK;         /* Interrupt Mask */
00132     genctl  = ioaddr + GENCTL;          /* General Control */
00133     eectl   = ioaddr + EECTL;           /* EEPROM Control  */
00134     test    = ioaddr + TEST;            /* Test register (clocks) */
00135     mmctl   = ioaddr + MMCTL;           /* MII Management Interface Control */
00136     mmdata  = ioaddr + MMDATA;          /* MII Management Interface Data */
00137     lan0    = ioaddr + LAN0;            /* MAC address. (0x40-0x48) */
00138     mc0     = ioaddr + MC0;             /* Multicast Control */
00139     rxcon   = ioaddr + RXCON;           /* Receive Control */
00140     txcon   = ioaddr + TXCON;           /* Transmit Control */
00141     prcdar  = ioaddr + PRCDAR;          /* PCI Receive Current Descr Address */
00142     ptcdar  = ioaddr + PTCDAR;          /* PCI Transmit Current Descr Address */
00143     eththr  = ioaddr + ETHTHR;          /* Early Transmit Threshold */
00144 
00145     /* Reset the chip & bring it out of low-power mode. */
00146     outl(GC_SOFT_RESET, genctl);
00147 
00148     /* Disable ALL interrupts by setting the interrupt mask. */
00149     outl(INTR_DISABLE, intmask);
00150 
00151     /*
00152      * set the internal clocks:
00153      * Application Note 7.15 says:
00154      *    In order to set the CLOCK TEST bit in the TEST register,
00155      *    perform the following:
00156      *
00157      *        Write 0x0008 to the test register at least sixteen
00158      *        consecutive times.
00159      *
00160      * The CLOCK TEST bit is Write-Only. Writing it several times
00161      * consecutively insures a successful write to the bit...
00162      */
00163 
00164     for (i = 0; i < 16; i++) {
00165         outl(0x00000008, test);
00166     }
00167 
00168 #ifdef  DEBUG_EEPROM
00169 {
00170     unsigned short sum = 0;
00171     unsigned short value;
00172     for (i = 0; i < 64; i++) {
00173         value = read_eeprom(i);
00174         eeprom[i] = value;
00175         sum += value;
00176     }
00177 }
00178 
00179 #if     (EPIC_DEBUG > 1)
00180     printf("EEPROM contents\n");
00181     for (i = 0; i < 64; i++) {
00182         printf(" %hhX%s", eeprom[i], i % 16 == 15 ? "\n" : "");
00183     }
00184 #endif
00185 #endif
00186 
00187     /* This could also be read from the EEPROM. */
00188     ap = (unsigned short*)nic->node_addr;
00189     for (i = 0; i < 3; i++)
00190         *ap++ = inw(lan0 + i*4);
00191 
00192     DBG ( " I/O %4.4x %s ", ioaddr, eth_ntoa ( nic->node_addr ) );
00193 
00194     /* Find the connected MII xcvrs. */
00195     for (phy = 0, phy_idx = 0; phy < 32 && phy_idx < sizeof(phys); phy++) {
00196         int mii_status = mii_read(phy, 0);
00197 
00198         if (mii_status != 0xffff  && mii_status != 0x0000) {
00199             phys[phy_idx++] = phy;
00200 #if     (EPIC_DEBUG > 1)
00201             printf("MII transceiver found at address %d.\n", phy);
00202 #endif
00203         }
00204     }
00205     if (phy_idx == 0) {
00206 #if     (EPIC_DEBUG > 1)
00207         printf("***WARNING***: No MII transceiver found!\n");
00208 #endif
00209         /* Use the known PHY address of the EPII. */
00210         phys[0] = 3;
00211     }
00212 
00213     epic100_open();
00214     nic->nic_op = &epic100_operations;
00215 
00216     return 1;
00217 }
00218 
00219 static void set_rx_mode(void)
00220 {
00221         unsigned char mc_filter[8];
00222         int i;
00223         memset(mc_filter, 0xff, sizeof(mc_filter));
00224         outl(0x0C, rxcon);
00225         for(i = 0; i < 4; i++)
00226                 outw(((unsigned short *)mc_filter)[i], mc0 + i*4);
00227         return;
00228 }
00229         
00230    static void
00231 epic100_open(void)
00232 {
00233     int mii_reg5;
00234     int full_duplex = 0;
00235     unsigned long tmp;
00236 
00237     epic100_init_ring();
00238 
00239     /* Pull the chip out of low-power mode, and set for PCI read multiple. */
00240     outl(GC_RX_FIFO_THR_64 | GC_MRC_READ_MULT | GC_ONE_COPY, genctl);
00241 
00242     outl(TX_FIFO_THRESH, eththr);
00243 
00244     tmp = TC_EARLY_TX_ENABLE | TX_SLOT_TIME;
00245 
00246     mii_reg5 = mii_read(phys[0], 5);
00247     if (mii_reg5 != 0xffff && (mii_reg5 & 0x0100)) {
00248         full_duplex = 1;
00249         printf(" full-duplex mode");
00250         tmp |= TC_LM_FULL_DPX;
00251     } else
00252         tmp |= TC_LM_NORMAL;
00253 
00254     outl(tmp, txcon);
00255 
00256     /* Give adress of RX and TX ring to the chip */
00257     outl(virt_to_le32desc(&rx_ring), prcdar);
00258     outl(virt_to_le32desc(&tx_ring), ptcdar);
00259 
00260     /* Start the chip's Rx process: receive unicast and broadcast */
00261     set_rx_mode();
00262     outl(CR_START_RX | CR_QUEUE_RX, command);
00263 
00264     putchar('\n');
00265 }
00266 
00267 /* Initialize the Rx and Tx rings. */
00268     static void
00269 epic100_init_ring(void)
00270 {
00271     int i;
00272 
00273     cur_rx = cur_tx = 0;
00274 
00275     for (i = 0; i < RX_RING_SIZE; i++) {
00276         rx_ring[i].status    = cpu_to_le32(RRING_OWN);  /* Owned by Epic chip */
00277         rx_ring[i].buflength = cpu_to_le32(PKT_BUF_SZ);
00278         rx_ring[i].bufaddr   = virt_to_bus(&rx_packet[i * PKT_BUF_SZ]);
00279         rx_ring[i].next      = virt_to_le32desc(&rx_ring[i + 1]) ;
00280     }
00281     /* Mark the last entry as wrapping the ring. */
00282     rx_ring[i-1].next = virt_to_le32desc(&rx_ring[0]);
00283 
00284     /*
00285      *The Tx buffer descriptor is filled in as needed,
00286      * but we do need to clear the ownership bit.
00287      */
00288 
00289     for (i = 0; i < TX_RING_SIZE; i++) {
00290         tx_ring[i].status  = 0x0000;                    /* Owned by CPU */
00291         tx_ring[i].buflength = 0x0000 | cpu_to_le32(TD_STDFLAGS << 16);
00292         tx_ring[i].bufaddr = virt_to_bus(&tx_packet[i * PKT_BUF_SZ]);
00293         tx_ring[i].next    = virt_to_le32desc(&tx_ring[i + 1]);
00294     }
00295         tx_ring[i-1].next    = virt_to_le32desc(&tx_ring[0]);
00296 }
00297 
00298 /* function: epic100_transmit
00299  * This transmits a packet.
00300  *
00301  * Arguments: char d[6]:          destination ethernet address.
00302  *            unsigned short t:   ethernet protocol type.
00303  *            unsigned short s:   size of the data-part of the packet.
00304  *            char *p:            the data for the packet.
00305  * returns:   void.
00306  */
00307     static void
00308 epic100_transmit(struct nic *nic, const char *destaddr, unsigned int type,
00309                  unsigned int len, const char *data)
00310 {
00311     unsigned short nstype;
00312     unsigned char *txp;
00313     int entry;
00314     unsigned long ct;
00315 
00316     /* Calculate the next Tx descriptor entry. */
00317     entry = cur_tx % TX_RING_SIZE;
00318 
00319     if ((tx_ring[entry].status & TRING_OWN) == TRING_OWN) {
00320         printf("eth_transmit: Unable to transmit. status=%4.4lx. Resetting...\n",
00321                tx_ring[entry].status);
00322 
00323         epic100_open();
00324         return;
00325     }
00326 
00327     txp = tx_packet + (entry * PKT_BUF_SZ);
00328 
00329     memcpy(txp, destaddr, ETH_ALEN);
00330     memcpy(txp + ETH_ALEN, nic->node_addr, ETH_ALEN);
00331     nstype = htons(type);
00332     memcpy(txp + 12, (char*)&nstype, 2);
00333     memcpy(txp + ETH_HLEN, data, len);
00334 
00335     len += ETH_HLEN;
00336         len &= 0x0FFF;
00337         while(len < ETH_ZLEN)
00338                 txp[len++] = '\0';
00339     /*
00340      * Caution: the write order is important here,
00341      * set the base address with the "ownership"
00342      * bits last.
00343      */
00344    
00345     tx_ring[entry].buflength |= cpu_to_le32(len);
00346     tx_ring[entry].status = cpu_to_le32(len << 16) |
00347             cpu_to_le32(TRING_OWN);     /* Pass ownership to the chip. */
00348 
00349     cur_tx++;
00350 
00351     /* Trigger an immediate transmit demand. */
00352     outl(CR_QUEUE_TX, command);
00353 
00354     ct = currticks();
00355     /* timeout 10 ms for transmit */
00356     while ((le32_to_cpu(tx_ring[entry].status) & (TRING_OWN)) &&
00357                 ct + 10*1000 < currticks())
00358         /* Wait */;
00359 
00360     if ((le32_to_cpu(tx_ring[entry].status) & TRING_OWN) != 0)
00361         printf("Oops, transmitter timeout, status=%4.4lX\n",
00362             tx_ring[entry].status);
00363 }
00364 
00365 /* function: epic100_poll / eth_poll
00366  * This receives a packet from the network.
00367  *
00368  * Arguments: none
00369  *
00370  * returns:   1 if a packet was received.
00371  *            0 if no pacet was received.
00372  * side effects:
00373  *            returns the packet in the array nic->packet.
00374  *            returns the length of the packet in nic->packetlen.
00375  */
00376 
00377     static int
00378 epic100_poll(struct nic *nic, int retrieve)
00379 {
00380     int entry;
00381     int retcode;
00382     int status;
00383     entry = cur_rx % RX_RING_SIZE;
00384 
00385     if ((rx_ring[entry].status & cpu_to_le32(RRING_OWN)) == RRING_OWN)
00386         return (0);
00387 
00388     if ( ! retrieve ) return 1;
00389 
00390     status = le32_to_cpu(rx_ring[entry].status);
00391     /* We own the next entry, it's a new packet. Send it up. */
00392 
00393 #if     (EPIC_DEBUG > 4)
00394     printf("epic_poll: entry %d status %hX\n", entry, status);
00395 #endif
00396 
00397     cur_rx++;
00398     if (status & 0x2000) {
00399         printf("epic_poll: Giant packet\n");
00400         retcode = 0;
00401     } else if (status & 0x0006) {
00402         /* Rx Frame errors are counted in hardware. */
00403         printf("epic_poll: Frame received with errors\n");
00404         retcode = 0;
00405     } else {
00406         /* Omit the four octet CRC from the length. */
00407         nic->packetlen = le32_to_cpu((rx_ring[entry].buflength))- 4;
00408         memcpy(nic->packet, &rx_packet[entry * PKT_BUF_SZ], nic->packetlen);
00409         retcode = 1;
00410     }
00411 
00412     /* Clear all error sources. */
00413     outl(status & INTR_CLEARERRS, intstat);
00414 
00415     /* Give the descriptor back to the chip */
00416     rx_ring[entry].status = RRING_OWN;
00417 
00418     /* Restart Receiver */
00419     outl(CR_START_RX | CR_QUEUE_RX, command); 
00420 
00421     return retcode;
00422 }
00423 
00424 
00425 static void epic100_disable ( struct nic *nic __unused ) {
00426         /* Soft reset the chip. */
00427         outl(GC_SOFT_RESET, genctl);
00428 }
00429 
00430 static void epic100_irq(struct nic *nic __unused, irq_action_t action __unused)
00431 {
00432   switch ( action ) {
00433   case DISABLE :
00434     break;
00435   case ENABLE :
00436     break;
00437   case FORCE :
00438     break;
00439   }
00440 }
00441 
00442 #ifdef  DEBUG_EEPROM
00443 /* Serial EEPROM section. */
00444 
00445 /*  EEPROM_Ctrl bits. */
00446 #define EE_SHIFT_CLK    0x04    /* EEPROM shift clock. */
00447 #define EE_CS           0x02    /* EEPROM chip select. */
00448 #define EE_DATA_WRITE   0x08    /* EEPROM chip data in. */
00449 #define EE_WRITE_0      0x01
00450 #define EE_WRITE_1      0x09
00451 #define EE_DATA_READ    0x10    /* EEPROM chip data out. */
00452 #define EE_ENB          (0x0001 | EE_CS)
00453 
00454 /* The EEPROM commands include the alway-set leading bit. */
00455 #define EE_WRITE_CMD    (5 << 6)
00456 #define EE_READ_CMD     (6 << 6)
00457 #define EE_ERASE_CMD    (7 << 6)
00458 
00459 #define eeprom_delay(n) delay(n)
00460 
00461     static int
00462 read_eeprom(int location)
00463 {
00464     int i;
00465     int retval = 0;
00466     int read_cmd = location | EE_READ_CMD;
00467 
00468     outl(EE_ENB & ~EE_CS, eectl);
00469     outl(EE_ENB, eectl);
00470 
00471     /* Shift the read command bits out. */
00472     for (i = 10; i >= 0; i--) {
00473         short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
00474         outl(EE_ENB | dataval, eectl);
00475         eeprom_delay(100);
00476         outl(EE_ENB | dataval | EE_SHIFT_CLK, eectl);
00477         eeprom_delay(150);
00478         outl(EE_ENB | dataval, eectl);  /* Finish EEPROM a clock tick. */
00479         eeprom_delay(250);
00480     }
00481     outl(EE_ENB, eectl);
00482 
00483     for (i = 16; i > 0; i--) {
00484         outl(EE_ENB | EE_SHIFT_CLK, eectl);
00485         eeprom_delay(100);
00486         retval = (retval << 1) | ((inl(eectl) & EE_DATA_READ) ? 1 : 0);
00487         outl(EE_ENB, eectl);
00488         eeprom_delay(100);
00489     }
00490 
00491     /* Terminate the EEPROM access. */
00492     outl(EE_ENB & ~EE_CS, eectl);
00493     return retval;
00494 }
00495 #endif
00496 
00497 
00498 #define MII_READOP      1
00499 #define MII_WRITEOP     2
00500 
00501     static int
00502 mii_read(int phy_id, int location)
00503 {
00504     int i;
00505 
00506     outl((phy_id << 9) | (location << 4) | MII_READOP, mmctl);
00507     /* Typical operation takes < 50 ticks. */
00508 
00509     for (i = 4000; i > 0; i--)
00510         if ((inl(mmctl) & MII_READOP) == 0)
00511             break;
00512     return inw(mmdata);
00513 }
00514 
00515 static struct nic_operations epic100_operations = {
00516         .connect        = dummy_connect,
00517         .poll           = epic100_poll,
00518         .transmit       = epic100_transmit,
00519         .irq            = epic100_irq,
00520 
00521 };
00522 
00523 static struct pci_device_id epic100_nics[] = {
00524 PCI_ROM(0x10b8, 0x0005, "epic100",    "SMC EtherPowerII", 0),           /* SMC 83c170 EPIC/100 */
00525 PCI_ROM(0x10b8, 0x0006, "smc-83c175", "SMC EPIC/C 83c175", 0),
00526 };
00527 
00528 PCI_DRIVER ( epic100_driver, epic100_nics, PCI_NO_CLASS );
00529 
00530 DRIVER ( "EPIC100", nic_driver, pci_driver, epic100_driver,
00531          epic100_probe, epic100_disable );
00532 
00533 /*
00534  * Local variables:
00535  *  c-basic-offset: 8
00536  *  c-indent-level: 8
00537  *  tab-width: 8
00538  * End:
00539  */

Generated on Tue Apr 6 20:00:59 2010 for gPXE by  doxygen 1.5.7.1