rtl8019.c

00001 #include "rtl8019.h"
00002 #include "delay.h"
00003 #include "debug.h"
00004 #include "avr/pgmspace.h"
00005 #include "rtlregs.h"
00006 
00007 #ifndef cbi
00008 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
00009 #endif
00010 #ifndef sbi
00011 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
00012 #endif
00013 
00014 #define outp(val, port) do { (port) = (val); } while(0)
00015 #define inp(port) (port)
00016 
00017 /*****************************************************************************
00018 *  Module Name:       Realtek 8019AS Driver
00019 *
00020 *  Created By:        Louis Beaudoin (www.embedded-creations.com)
00021 *
00022 *  Original Release:  September 21, 2002
00023 *
00024 *  Module Description:
00025 *  Provides functions to initialize the Realtek 8019AS, and send and retreive
00026 *  packets
00027 *
00028 *  November 15, 2002 - Louis Beaudoin
00029 *    processRTL8019Interrupt() - bit mask mistake fixed
00030 *
00031 *  September 30, 2002 - Louis Beaudoin
00032 *    Receive functions modified to handle errors encountered when receiving a
00033 *      fast data stream.  Functions now manually retreive data instead of
00034 *      using the send packet command.  Interface improved by checking for
00035 *      overruns and data in the buffer internally.
00036 *    Corrected the overrun function - overrun flag was not reset after overrun
00037 *    Added support for the Imagecraft Compiler
00038 *    Added support to communicate with the NIC using general I/O ports
00039 *
00040 *****************************************************************************/
00041 
00042 
00043 /*****************************************************************************
00044 *  writeRTL( RTL_ADDRESS, RTL_DATA )
00045 *  Args:        1. unsigned char RTL_ADDRESS - register offset of RTL register
00046 *               2. unsigned char RTL_DATA - data to write to register
00047 *  Created By:  Louis Beaudoin
00048 *  Date:        September 21, 2002
00049 *  Description: Writes byte to RTL8019 register.
00050 *
00051 *  Notes - If using the External SRAM Interface, performs a write to
00052 *            address MEMORY_MAPPED_RTL8019_OFFSET + (RTL_ADDRESS<<8)
00053 *            The address is sent in the non-multiplxed upper address port so
00054 *            no latch is required.
00055 *
00056 *          If using general I/O ports, the data port is left in the input
00057 *            state with pullups enabled
00058 *
00059 *****************************************************************************/
00060 #if MEMORY_MAPPED_NIC == 1
00061 /*#define writeRTL(RTL_ADDRESS,RTL_DATA) do{ *(volatile unsigned char *) \
00062                              (MEMORY_MAPPED_RTL8019_OFFSET \
00063                              + (((unsigned char)(RTL_ADDRESS)) << 8)) = \
00064                              (unsigned char)(RTL_DATA); } while(0)*/
00065 #define writeRTL nic_write
00066 #else
00067 
00068 
00069 void writeRTL(unsigned char address, unsigned char data)
00070 {
00071     // put the address and data in the port registers - data port is output
00072     outp( address, RTL8019_ADDRESS_PORT );
00073     outp( 0xFF, RTL8019_DATA_DDR );
00074     outp( data, RTL8019_DATA_PORT );
00075     
00076         // toggle write pin
00077     RTL8019_CLEAR_WRITE;
00078     RTL8019_SET_WRITE;
00079     
00080         // set data port back to input with pullups enabled
00081     outp( 0x00, RTL8019_DATA_DDR );
00082     outp( 0xFF, RTL8019_DATA_PORT );
00083 }
00084 
00085 
00086 
00087 #endif
00088 
00089 /*****************************************************************************
00090 *  readRTL(RTL_ADDRESS)
00091 *  Args:        unsigned char RTL_ADDRESS - register offset of RTL register
00092 *  Created By:  Louis Beaudoin
00093 *  Date:        September 21, 2002
00094 *  Description: Reads byte from RTL8019 register
00095 *
00096 *  Notes - If using the External SRAM Interface, performs a read from
00097 *            address MEMORY_MAPPED_RTL8019_OFFSET + (RTL_ADDRESS<<8)
00098 *            The address is sent in the non-multiplxed upper address port so
00099 *            no latch is required.
00100 *
00101 *          If using general I/O ports, the data port is assumed to already be
00102 *            an input, and is left as an input port when done
00103 *
00104 *****************************************************************************/
00105 #if MEMORY_MAPPED_NIC == 1
00106 /*#define readRTL(RTL_ADDRESS) (*(volatile unsigned char *) \
00107                        (MEMORY_MAPPED_RTL8019_OFFSET \
00108                        + (((unsigned char)(RTL_ADDRESS)) << 8)) )*/
00109 #define readRTL nic_read
00110 #else
00111 
00112 unsigned char readRTL(unsigned char address)
00113 {
00114    unsigned char byte;
00115    
00116    // drive the read address
00117    outp( address, RTL8019_ADDRESS_PORT );
00118     
00119    //nop();
00120    
00121    // assert read
00122    RTL8019_CLEAR_READ;
00123    nop();
00124    
00125    // read in the data
00126    byte = inp( RTL8019_DATA_PIN );
00127 
00128    // negate read
00129    RTL8019_SET_READ;
00130 
00131    return byte;
00132 }
00133 
00134 #endif
00135 
00136 
00137 
00138 /*****************************************************************************
00139 *  RTL8019setupPorts(void);
00140 *
00141 *  Created By:  Louis Beaudoin
00142 *  Date:        September 21, 2002
00143 *  Description: Sets up the ports used for communication with the RTL8019 NIC
00144 *                 (data bus, address bus, read, write, and reset)
00145 *****************************************************************************/
00146 void RTL8019setupPorts(void)
00147 {
00148 volatile unsigned char *base = (unsigned char *)0x8300;
00149 
00150 #if MEMORY_MAPPED_NIC == 1
00151         // enable external SRAM interface - no wait states
00152         outp(inp(MCUCR) | (1<<SRE), MCUCR);
00153 
00154 #else
00155 
00156     // make the address port output
00157     outp( 0xFF, RTL8019_ADDRESS_DDR );
00158     
00159     // make the data port input with pull-ups
00160     outp( 0xFF, RTL8019_DATA_PORT );
00161 
00162         // make the control port read and write pins outputs and asserted
00163         //outp( inp(RTL8019_CONTROL_DDR) | (1<<RTL8019_CONTROL_READPIN) |
00164         //          (1<<RTL8019_CONTROL_WRITEPIN), RTL8019_CONTROL_DDR );
00165         sbi( RTL8019_CONTROL_DDR, RTL8019_CONTROL_READPIN );
00166         sbi( RTL8019_CONTROL_DDR, RTL8019_CONTROL_WRITEPIN );
00167                   
00168         //outp( inp(RTL8019_CONTROL_PORT) | (1<<RTL8019_CONTROL_READPIN) |
00169         //          (1<<RTL8019_CONTROL_WRITEPIN), RTL8019_CONTROL_PORT );
00170         sbi( RTL8019_CONTROL_PORT, RTL8019_CONTROL_READPIN );
00171         sbi( RTL8019_CONTROL_PORT, RTL8019_CONTROL_WRITEPIN );
00172 
00173 #endif
00174 
00175         // enable output pin for Resetting the RTL8019
00176         sbi( RTL8019_RESET_DDR, RTL8019_RESET_PIN );
00177         
00178         
00179         
00180 
00181 
00182 }
00183 
00184 
00185 
00186 /*****************************************************************************
00187 *  HARD_RESET_RTL8019()
00188 *
00189 *  Created By:  Louis Beaudoin
00190 *  Date:        September 21, 2002
00191 *  Description: Simply toggles the pin that resets the NIC
00192 *****************************************************************************/
00193 /*#define HARD_RESET_RTL8019() do{ sbi(RTL8019_RESET_PORT, RTL8019_RESET_PIN); \
00194                                 Delay_10ms(1); \
00195                                 cbi(RTL8019_RESET_PORT, RTL8019_RESET_PIN);} \
00196                                 while(0)*/
00197 
00198 
00199 
00200 /*****************************************************************************
00201 *  overrun(void);
00202 *
00203 *  Created By:  Louis Beaudoin
00204 *  Date:        September 21, 2002
00205 *  Description: "Canned" receive buffer overrun function originally from
00206 *                 a National Semiconductor appnote
00207 *  Notes:       This function must be called before retreiving packets from
00208 *                 the NIC if there is a buffer overrun
00209 *****************************************************************************/
00210 void overrun(void);
00211 
00212 
00213 
00214 
00215 //******************************************************************
00216 //*     REALTEK CONTROL REGISTER OFFSETS
00217 //*   All offsets in Page 0 unless otherwise specified
00218 //*       All functions accessing CR must leave CR in page 0 upon exit
00219 //******************************************************************
00220 #define CR                      0x00
00221 #define PSTART          0x01
00222 #define PAR0            0x01    // Page 1
00223 #define CR9346          0x01    // Page 3
00224 #define PSTOP           0x02
00225 #define BNRY            0x03
00226 #define TSR                     0x04
00227 #define TPSR            0x04
00228 #define TBCR0           0x05
00229 #define NCR                     0x05
00230 #define TBCR1           0x06
00231 #define ISR                     0x07
00232 #define CURR            0x07   // Page 1
00233 #define RSAR0           0x08
00234 #define CRDA0           0x08
00235 #define RSAR1           0x09
00236 #define CRDA1           0x09
00237 #define RBCR0           0x0A
00238 #define RBCR1           0x0B
00239 #define RSR                     0x0C
00240 #define RCR                     0x0C
00241 #define TCR                     0x0D
00242 #define CNTR0           0x0D
00243 #define DCR                     0x0E
00244 #define CNTR1           0x0E
00245 #define IMR                     0x0F
00246 #define CNTR2           0x0F
00247 #define RDMAPORT        0x10
00248 #define RSTPORT         0x18
00249 
00250 
00251 /*****************************************************************************
00252 *
00253 * RTL ISR Register Bits
00254 *
00255 *****************************************************************************/
00256 #define ISR_RST 7
00257 #define ISR_OVW 4
00258 #define ISR_PRX 0
00259 #define ISR_RDC 6
00260 #define ISR_PTX 1
00261 
00262 
00263 /*****************************************************************************
00264 *
00265 *  RTL Register Initialization Values
00266 *
00267 *****************************************************************************/
00268 // RCR : accept broadcast packets and packets destined to this MAC
00269 //         drop short frames and receive errors
00270 #define RCR_INIT                0x04
00271 
00272 // TCR : default transmit operation - CRC is generated
00273 #define TCR_INIT                0x00
00274 
00275 // DCR : allows send packet to be used for packet retreival
00276 //         FIFO threshold: 8-bits (works)
00277 //         8-bit transfer mode
00278 #define DCR_INIT                0x58
00279 
00280 // IMR : interrupt enabled for receive and overrun events
00281 #define IMR_INIT                0x11
00282 
00283 // buffer boundaries - transmit has 6 256-byte pages
00284 //   receive has 26 256-byte pages
00285 //   entire available packet buffer space is allocated
00286 #define TXSTART_INIT    0x40
00287 #define RXSTART_INIT    0x46
00288 #define RXSTOP_INIT     0x60
00289 
00290 
00291 
00292 void RTL8019beginPacketSend(unsigned int packetLength)
00293 {
00294 
00295   volatile unsigned char *base = (unsigned char *)0x8300;
00296         unsigned int sendPacketLength;
00297         sendPacketLength = (packetLength>=ETHERNET_MIN_PACKET_LENGTH) ?
00298                          packetLength : ETHERNET_MIN_PACKET_LENGTH ;
00299         
00300         //start the NIC
00301         writeRTL(CR,0x22);
00302         
00303         // still transmitting a packet - wait for it to finish
00304         while( readRTL(CR) & 0x04 );
00305 
00306         //load beginning page for transmit buffer
00307         writeRTL(TPSR,TXSTART_INIT);
00308         
00309         //set start address for remote DMA operation
00310         writeRTL(RSAR0,0x00);
00311         writeRTL(RSAR1,0x40);
00312         
00313         //clear the packet stored interrupt
00314         writeRTL(ISR,(1<<ISR_PTX));
00315 
00316         //load data byte count for remote DMA
00317         writeRTL(RBCR0, (unsigned char)(packetLength));
00318         writeRTL(RBCR1, (unsigned char)(packetLength>>8));
00319 
00320         writeRTL(TBCR0, (unsigned char)(sendPacketLength));
00321         writeRTL(TBCR1, (unsigned char)((sendPacketLength)>>8));
00322         
00323         //do remote write operation
00324         writeRTL(CR,0x12);
00325 }
00326 
00327 
00328 
00329 void RTL8019sendPacketData(unsigned char * localBuffer, unsigned int length)
00330 {
00331         unsigned int i;
00332         volatile unsigned char *base = (unsigned char *)0x8300;
00333         for(i=0;i<length;i++)
00334                 writeRTL(RDMAPORT, localBuffer[i]);
00335 }
00336 
00337 
00338 
00339 void RTL8019endPacketSend(void)
00340 {
00341   volatile unsigned char *base = (unsigned char *)0x8300;
00342         //send the contents of the transmit buffer onto the network
00343         writeRTL(CR,0x24);
00344         
00345         // clear the remote DMA interrupt
00346         writeRTL(ISR, (1<<ISR_RDC));
00347 }
00348 
00349 
00350 
00351 
00352 // pointers to locations in the RTL8019 receive buffer
00353 static unsigned char nextPage;
00354 static unsigned int currentRetreiveAddress;
00355 
00356 // location of items in the RTL8019's page header
00357 #define  enetpacketstatus     0x00
00358 #define  nextblock_ptr        0x01
00359 #define  enetpacketLenL           0x02
00360 #define  enetpacketLenH           0x03
00361 
00362 
00363 
00364 unsigned int RTL8019beginPacketRetreive(void)
00365 {
00366   volatile unsigned char *base = (unsigned char *)0x8300;
00367         unsigned char i;
00368         unsigned char bnry;
00369         
00370         unsigned char pageheader[4];
00371         unsigned int rxlen;
00372         
00373         // check for and handle an overflow
00374         processRTL8019Interrupt();
00375         
00376         // read CURR from page 1
00377         writeRTL(CR,0x62);
00378         i = readRTL(CURR);
00379         
00380         // return to page 0
00381         writeRTL(CR,0x22);
00382         
00383         // read the boundary register - pointing to the beginning of the packet
00384         bnry = readRTL(BNRY) ;
00385 
00386         /*      debug_print(PSTR("bnry: "));
00387                 debug_print8(bnry);*/
00388 
00389         /*      debug_print(PSTR("RXSTOP_INIT: "));
00390         debug_print8(RXSTOP_INIT);
00391         debug_print(PSTR("RXSTART_INIT: "));
00392         debug_print8(RXSTART_INIT);*/
00393         // return if there is no packet in the buffer
00394         if( bnry == i ) {
00395           return 0;
00396         }
00397         
00398 
00399         // clear the packet received interrupt flag
00400         writeRTL(ISR, (1<<ISR_PRX));
00401         
00402         
00403         // the boundary pointer is invalid, reset the contents of the buffer and exit
00404         if( (bnry >= RXSTOP_INIT) || (bnry < RXSTART_INIT) )
00405         {
00406                 writeRTL(BNRY, RXSTART_INIT);
00407                 writeRTL(CR, 0x62);
00408                 writeRTL(CURR, RXSTART_INIT);
00409                 writeRTL(CR, 0x22);
00410                 return 0;
00411         }
00412 
00413         // initiate DMA to transfer the RTL8019 packet header
00414     writeRTL(RBCR0, 4);
00415     writeRTL(RBCR1, 0);
00416     writeRTL(RSAR0, 0);
00417     writeRTL(RSAR1, bnry);
00418     writeRTL(CR, 0x0A);
00419     /*          debug_print(PSTR("Page header: "));*/
00420 
00421         for(i=0;i<4;i++) {
00422           pageheader[i] = readRTL(RDMAPORT);
00423           /*      debug_print8(pageheader[i]);*/
00424         }
00425         
00426         // end the DMA operation
00427     writeRTL(CR, 0x22);
00428     for(i = 0; i <= 20; i++) {
00429       if(readRTL(ISR) & 1<<6) {
00430         break;
00431       }
00432     }
00433     writeRTL(ISR, 1<<6);
00434 
00435         
00436         
00437         rxlen = (pageheader[enetpacketLenH]<<8) + pageheader[enetpacketLenL];
00438         nextPage = pageheader[nextblock_ptr] ;
00439         
00440         currentRetreiveAddress = (bnry<<8) + 4;
00441 
00442         /*      debug_print(PSTR("nextPage: "));
00443                 debug_print8(nextPage);*/
00444         
00445         // if the nextPage pointer is invalid, the packet is not ready yet - exit
00446         if( (nextPage >= RXSTOP_INIT) || (nextPage < RXSTART_INIT) ) {
00447           /*      UDR0 = '0';*/
00448           return 0;
00449         }
00450     
00451     return rxlen-4;
00452 }
00453 
00454 
00455 void RTL8019retreivePacketData(unsigned char * localBuffer, unsigned int length)
00456 {
00457         unsigned int i;
00458         volatile unsigned char *base = (unsigned char *)0x8300;
00459         // initiate DMA to transfer the data
00460     writeRTL(RBCR0, (unsigned char)length);
00461     writeRTL(RBCR1, (unsigned char)(length>>8));
00462     writeRTL(RSAR0, (unsigned char)currentRetreiveAddress);
00463     writeRTL(RSAR1, (unsigned char)(currentRetreiveAddress>>8));
00464     writeRTL(CR, 0x0A);
00465         for(i=0;i<length;i++)
00466                 localBuffer[i] = readRTL(RDMAPORT);
00467 
00468         // end the DMA operation
00469     writeRTL(CR, 0x22);
00470     for(i = 0; i <= 20; i++)
00471         if(readRTL(ISR) & 1<<6)
00472             break;
00473     writeRTL(ISR, 1<<6);
00474     
00475     currentRetreiveAddress += length;
00476     if( currentRetreiveAddress >= 0x6000 )
00477         currentRetreiveAddress = currentRetreiveAddress - (0x6000-0x4600) ;
00478 }
00479 
00480 
00481 
00482 void RTL8019endPacketRetreive(void)
00483 {
00484   volatile unsigned char *base = (unsigned char *)0x8300;
00485         unsigned char i;
00486 
00487         // end the DMA operation
00488     writeRTL(CR, 0x22);
00489     for(i = 0; i <= 20; i++)
00490         if(readRTL(ISR) & 1<<6)
00491             break;
00492     writeRTL(ISR, 1<<6);
00493 
00494         // set the boundary register to point to the start of the next packet
00495     writeRTL(BNRY, nextPage);
00496 }
00497 
00498 
00499 void overrun(void)
00500 {
00501   volatile unsigned char *base = (unsigned char *)0x8300;
00502         unsigned char data_L, resend;
00503 
00504         data_L = readRTL(CR);
00505         writeRTL(CR, 0x21);
00506         Delay_1ms(2);
00507         writeRTL(RBCR0, 0x00);
00508         writeRTL(RBCR1, 0x00);
00509         if(!(data_L & 0x04))
00510                 resend = 0;
00511         else if(data_L & 0x04)
00512         {
00513                 data_L = readRTL(ISR);
00514                 if((data_L & 0x02) || (data_L & 0x08))
00515                 resend = 0;
00516             else
00517                 resend = 1;
00518         }
00519         
00520         writeRTL(TCR, 0x02);
00521         writeRTL(CR, 0x22);
00522         writeRTL(BNRY, RXSTART_INIT);
00523         writeRTL(CR, 0x62);
00524         writeRTL(CURR, RXSTART_INIT);
00525         writeRTL(CR, 0x22);
00526         writeRTL(ISR, 0x10);
00527         writeRTL(TCR, TCR_INIT);
00528         
00529         writeRTL(ISR, 0xFF);
00530 }
00531 
00532 
00533 
00534 
00535 /*!
00536  * \brief Size of a single ring buffer page.
00537  */
00538 #define NIC_PAGE_SIZE   0x100
00539 
00540 /*!
00541  * \brief First ring buffer page address.
00542  */
00543 #define NIC_START_PAGE  0x40
00544 
00545 /*!
00546  * \brief Last ring buffer page address plus 1.
00547  */
00548 #define NIC_STOP_PAGE   0x60
00549 
00550 /*!
00551  * \brief Number of pages in a single transmit buffer.
00552  *
00553  * This should be at least the MTU size.
00554  */
00555 #define NIC_TX_PAGES    6
00556 
00557 /*!
00558  * \brief Number of transmit buffers.
00559  */
00560 #define NIC_TX_BUFFERS  2
00561 
00562 /*!
00563  * \brief Controller memory layout:
00564  *
00565  * 0x4000 - 0x4bff  3k bytes transmit buffer
00566  * 0x4c00 - 0x5fff  5k bytes receive buffer
00567  */
00568 #define NIC_FIRST_TX_PAGE   NIC_START_PAGE
00569 #define NIC_FIRST_RX_PAGE   (NIC_FIRST_TX_PAGE + NIC_TX_PAGES * NIC_TX_BUFFERS)
00570 
00571 /*!
00572  * \brief Standard sizing information
00573  */
00574 #define TX_PAGES 12         /* Allow for 2 back-to-back frames */
00575 
00576 static unsigned char mac[6] = {0x00,0x06,0x98,0x01,0x02,0x29};
00577 void Delay(long nops)
00578 {
00579     volatile long i;
00580 
00581     for(i = 0; i < nops; i++)
00582 #ifdef __IMAGECRAFT__
00583         asm("nop\n");
00584 #else
00585         asm volatile("nop\n\t"::);
00586 #endif
00587 }
00588 
00589 static int NicReset(void)
00590 {
00591 volatile unsigned char *base = (unsigned char *)0x8300;
00592     unsigned char i;
00593     unsigned char j;
00594 
00595     for(j = 0; j < 20; j++) {
00596         debug_print(PSTR("SW-Reset..."));
00597         i = nic_read(NIC_RESET);
00598         Delay(500);
00599         nic_write(NIC_RESET, i);
00600         for(i = 0; i < 20; i++) {
00601             Delay(5000);
00602 
00603             /*
00604              * ID detection added for version 1.1 boards.
00605              */
00606             if((nic_read(NIC_PG0_ISR) & NIC_ISR_RST) != 0 &&
00607                nic_read(NIC_PG0_RBCR0) == 0x50 &&
00608                nic_read(NIC_PG0_RBCR1) == 0x70) {
00609                 debug_print(PSTR("OK\r\n"));
00610                 return 0;
00611             }
00612         }
00613         debug_print(PSTR("failed\r\n\x07"));
00614 
00615         /*
00616          * Toggle the hardware reset line. Since Ethernut version 1.3 the
00617          * hardware reset pin of the nic is no longer connected to bit 4
00618          * on port E, but wired to the board reset line.
00619          */
00620         if(j == 10) {
00621             debug_print(PSTR("Ethernut 1.1 HW-Reset\r\n"));
00622             sbi(DDRE, 4);
00623             sbi(PORTE, 4);
00624             Delay(100000);
00625             cbi(PORTE, 4);
00626             Delay(250000);
00627         }
00628     }
00629     return -1;
00630 }
00631 
00632 void initRTL8019(void)
00633 {
00634   unsigned char i, rb;
00635   volatile unsigned char *base = (unsigned char *)0x8300;
00636   
00637   RTL8019setupPorts();
00638 
00639   /*#define nic_write writeRTL
00640     #define nic_read readRTL*/
00641       /*
00642      * Disable NIC interrupts.
00643      */
00644     cbi(EIMSK, INT5);
00645 
00646     /*    if(NicReset(base))
00647           return -1;*/
00648 #if 0
00649     /*
00650      * Mask all interrupts and clear any interrupt status flag to set the
00651      * INT pin back to low.
00652      */
00653     nic_write(NIC_PG0_IMR, 0);
00654     nic_write(NIC_PG0_ISR, 0xff);
00655 
00656     /*
00657      * During reset the nic loaded its initial configuration from an
00658      * external eeprom. On the ethernut board we do not have any
00659      * configuration eeprom, but simply tied the eeprom data line to
00660      * high level. So we have to clear some bits in the configuration
00661      * register. Switch to register page 3.
00662      */
00663     nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0 | NIC_CR_PS1);
00664 
00665     /*
00666      * The nic configuration registers are write protected unless both
00667      * EEM bits are set to 1.
00668      */
00669     nic_write(NIC_PG3_EECR, NIC_EECR_EEM0 | NIC_EECR_EEM1);
00670 
00671     /*
00672      * Disable sleep and power down.
00673      */
00674     nic_write(NIC_PG3_CONFIG3, 0);
00675 
00676     /*
00677      * Network media had been set to 10Base2 by the virtual EEPROM and
00678      * will be set now to auto detect. This will initiate a link test.
00679      * We don't force 10BaseT, because this would disable the link test.
00680      */
00681     nic_write(NIC_PG3_CONFIG2, NIC_CONFIG2_BSELB);
00682 
00683     /*
00684      * Reenable write protection of the nic configuration registers
00685      * and wait for link test to complete.
00686      */
00687     nic_write(NIC_PG3_EECR, 0);
00688     /*    NutSleep(WAIT500);*/
00689     Delay_10ms(50);
00690 
00691     /*
00692      * Switch to register page 0 and set data configuration register
00693      * to byte-wide DMA transfers, normal operation (no loopback),
00694      * send command not executed and 8 byte fifo threshold.
00695      */
00696     nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2);
00697     nic_write(NIC_PG0_DCR, NIC_DCR_LS | NIC_DCR_FT1);
00698 
00699     /*
00700      * Clear remote dma byte count register.
00701      */
00702     nic_write(NIC_PG0_RBCR0, 0);
00703     nic_write(NIC_PG0_RBCR1, 0);
00704 
00705     /*
00706      * Temporarily set receiver to monitor mode and transmitter to
00707      * internal loopback mode. Incoming packets will not be stored
00708      * in the nic ring buffer and no data will be send to the network.
00709      */
00710     nic_write(NIC_PG0_RCR, NIC_RCR_MON);
00711     nic_write(NIC_PG0_TCR, NIC_TCR_LB0);
00712 
00713     /*
00714      * Configure the nic's ring buffer page layout.
00715      * NIC_PG0_BNRY: Last page read.
00716      * NIC_PG0_PSTART: First page of receiver buffer.
00717      * NIC_PG0_PSTOP: Last page of receiver buffer.
00718      */
00719     nic_write(NIC_PG0_TPSR, NIC_FIRST_TX_PAGE);
00720     nic_write(NIC_PG0_BNRY, NIC_STOP_PAGE - 1);
00721     nic_write(NIC_PG0_PSTART, NIC_FIRST_RX_PAGE);
00722     nic_write(NIC_PG0_PSTOP, NIC_STOP_PAGE);
00723 
00724     /*
00725      * Once again clear interrupt status register.
00726      */
00727     nic_write(NIC_PG0_ISR, 0xff);
00728 
00729     /*
00730      * Switch to register page 1 and copy our MAC address into the nic.
00731      * We are still in stop mode.
00732      */
00733     nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0);
00734     for(i = 0; i < 6; i++)
00735         nic_write(NIC_PG1_PAR0 + i, mac[i]);
00736 
00737     /*
00738      * Clear multicast filter bits to disable all packets.
00739      */
00740     for(i = 0; i < 8; i++)
00741         nic_write(NIC_PG1_MAR0 + i, 0);
00742 
00743     /*
00744      * Set current page pointer to one page after the boundary pointer.
00745      */
00746     nic_write(NIC_PG1_CURR, NIC_START_PAGE + TX_PAGES);
00747 
00748     /*
00749      * Switch back to register page 0, remaining in stop mode.
00750      */
00751     nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2);
00752 
00753     /*
00754      * Take receiver out of monitor mode and enable it for accepting
00755      * broadcasts.
00756      */
00757     nic_write(NIC_PG0_RCR, NIC_RCR_AB);
00758 
00759     /*
00760      * Clear all interrupt status flags and enable interrupts.
00761      */
00762     nic_write(NIC_PG0_ISR, 0xff);
00763     nic_write(NIC_PG0_IMR, NIC_IMR_PRXE | NIC_IMR_PTXE | NIC_IMR_RXEE |
00764                            NIC_IMR_TXEE | NIC_IMR_OVWE);
00765 
00766     /*
00767      * Fire up the nic by clearing the stop bit and setting the start bit.
00768      * To activate the local receive dma we must also take the nic out of
00769      * the local loopback mode.
00770      */
00771     nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
00772     nic_write(NIC_PG0_TCR, 0);
00773 
00774     /*    NutSleep(WAIT500);*/
00775     Delay_10ms(50);
00776 
00777 
00778 #endif /* 0 */
00779 
00780     NicReset();
00781     
00782     debug_print(PSTR("Init controller..."));
00783     nic_write(NIC_PG0_IMR, 0);
00784     nic_write(NIC_PG0_ISR, 0xff);
00785     nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0 | NIC_CR_PS1);
00786     nic_write(NIC_PG3_EECR, NIC_EECR_EEM0 | NIC_EECR_EEM1);
00787     nic_write(NIC_PG3_CONFIG3, 0);
00788     nic_write(NIC_PG3_CONFIG2, NIC_CONFIG2_BSELB);
00789     nic_write(NIC_PG3_EECR, 0);
00790     /*    Delay(50000);*/
00791     Delay_10ms(200);
00792     nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2);
00793     nic_write(NIC_PG0_DCR, NIC_DCR_LS | NIC_DCR_FT1);
00794     nic_write(NIC_PG0_RBCR0, 0);
00795     nic_write(NIC_PG0_RBCR1, 0);
00796     nic_write(NIC_PG0_RCR, NIC_RCR_MON);
00797     nic_write(NIC_PG0_TCR, NIC_TCR_LB0);
00798     nic_write(NIC_PG0_TPSR, NIC_FIRST_TX_PAGE);
00799     nic_write(NIC_PG0_BNRY, NIC_STOP_PAGE - 1);
00800     nic_write(NIC_PG0_PSTART, NIC_FIRST_RX_PAGE);
00801     nic_write(NIC_PG0_PSTOP, NIC_STOP_PAGE);
00802     nic_write(NIC_PG0_ISR, 0xff);
00803     nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0);
00804     for(i = 0; i < 6; i++)
00805         nic_write(NIC_PG1_PAR0 + i, mac[i]);
00806     for(i = 0; i < 8; i++)
00807         nic_write(NIC_PG1_MAR0 + i, 0);
00808     nic_write(NIC_PG1_CURR, NIC_START_PAGE + TX_PAGES);
00809     nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2);
00810     nic_write(NIC_PG0_RCR, NIC_RCR_AB);
00811     nic_write(NIC_PG0_ISR, 0xff);
00812     nic_write(NIC_PG0_IMR, 0);
00813     nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
00814     nic_write(NIC_PG0_TCR, 0);
00815     /*    Delay(1000000)*/
00816     Delay_10ms(200);
00817 
00818 
00819         nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2 | NIC_CR_PS0 | NIC_CR_PS1);
00820     rb = nic_read(NIC_PG3_CONFIG0);
00821     debug_print8(rb);
00822     switch(rb & 0xC0) {
00823     case 0x00:
00824         debug_print(PSTR("RTL8019AS "));
00825         if(rb & 0x08)
00826             debug_print(PSTR("jumper mode: "));
00827         if(rb & 0x20)
00828             debug_print(PSTR("AUI "));
00829         if(rb & 0x10)
00830             debug_print(PSTR("PNP "));
00831         break;
00832     case 0xC0:
00833         debug_print(PSTR("RTL8019 "));
00834         if(rb & 0x08)
00835             debug_print(PSTR("jumper mode: "));
00836         break;
00837     default:
00838         debug_print(PSTR("Unknown chip "));
00839         debug_print8(rb);
00840         break;
00841     }
00842     if(rb & 0x04)
00843         debug_print(PSTR("BNC\x07 "));
00844     if(rb & 0x03)
00845         debug_print(PSTR("Failed\x07 "));
00846 
00847     /*    rb = nic_read(NIC_PG3_CONFIG1);
00848           debug_print8(rb);*/
00849     /*    NutPrintFormat(0, "IRQ%u ", (rb >> 4) & 7);*/
00850     /*    debug_print("IRQ ");
00851           debug_print8((rb >> 4) & 7);*/
00852 
00853     rb = nic_read(NIC_PG3_CONFIG2);
00854     debug_print8(rb);
00855     switch(rb & 0xC0) {
00856     case 0x00:
00857         debug_print(PSTR("Auto "));
00858         break;
00859     case 0x40:
00860         debug_print(PSTR("10BaseT "));
00861         break;
00862     case 0x80:
00863         debug_print(PSTR("10Base5 "));
00864         break;
00865     case 0xC0:
00866         debug_print(PSTR("10Base2 "));
00867         break;
00868     }
00869 
00870 
00871     return;
00872     
00873   /*  HARD_RESET_RTL8019();*/
00874 
00875   // do soft reset
00876   writeRTL( ISR, readRTL(ISR) ) ;
00877   Delay_10ms(5);
00878   
00879   writeRTL(CR,0x21);       // stop the NIC, abort DMA, page 0
00880   Delay_1ms(2);               // make sure nothing is coming in or going out
00881   writeRTL(DCR, DCR_INIT);    // 0x58
00882   writeRTL(RBCR0,0x00);
00883   writeRTL(RBCR1,0x00);
00884   writeRTL(RCR,0x04);
00885   writeRTL(TPSR, TXSTART_INIT);
00886   writeRTL(TCR,0x02);
00887   writeRTL(PSTART, RXSTART_INIT);
00888   writeRTL(BNRY, RXSTART_INIT);
00889   writeRTL(PSTOP, RXSTOP_INIT);
00890   writeRTL(CR, 0x61);
00891   Delay_1ms(2);
00892   writeRTL(CURR, RXSTART_INIT);
00893   
00894   writeRTL(PAR0+0, MYMAC_0);
00895   writeRTL(PAR0+1, MYMAC_1);
00896   writeRTL(PAR0+2, MYMAC_2);
00897   writeRTL(PAR0+3, MYMAC_3);
00898   writeRTL(PAR0+4, MYMAC_4);
00899   writeRTL(PAR0+5, MYMAC_5);
00900           
00901   writeRTL(CR,0x21);
00902   writeRTL(DCR, DCR_INIT);
00903   writeRTL(CR,0x22);
00904   writeRTL(ISR,0xFF);
00905   writeRTL(IMR, IMR_INIT);
00906   writeRTL(TCR, TCR_INIT);
00907         
00908   writeRTL(CR, 0x22);   // start the NIC
00909 }
00910 
00911 
00912 void processRTL8019Interrupt(void)
00913 {
00914   volatile unsigned char *base = (unsigned char *)0x8300;
00915   unsigned char byte = readRTL(ISR);
00916         
00917   if( byte & (1<<ISR_OVW) )
00918     overrun();
00919 
00920 }
00921 
00922 /*
00923   unsigned char RTL8019ReceiveEmpty(void)
00924   {
00925   unsigned char temp;
00926 
00927   // read CURR from page 1
00928   writeRTL(CR,0x62);
00929   temp = readRTL(CURR);
00930         
00931   // return to page 0
00932   writeRTL(CR,0x22);
00933         
00934   return ( readRTL(BNRY) == temp );
00935         
00936   }*/

Generated on Mon Apr 11 14:23:36 2011 for Contiki 2.5 by  doxygen 1.6.1