4 #include "avr/pgmspace.h"
8 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
11 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
14 #define outp(val, port) do { (port) = (val); } while(0)
15 #define inp(port) (port)
60 #if MEMORY_MAPPED_NIC == 1
65 #define writeRTL nic_write
69 void writeRTL(
unsigned char address,
unsigned char data)
72 outp( address, RTL8019_ADDRESS_PORT );
73 outp( 0xFF, RTL8019_DATA_DDR );
74 outp( data, RTL8019_DATA_PORT );
81 outp( 0x00, RTL8019_DATA_DDR );
82 outp( 0xFF, RTL8019_DATA_PORT );
105 #if MEMORY_MAPPED_NIC == 1
109 #define readRTL nic_read
112 unsigned char readRTL(
unsigned char address)
117 outp( address, RTL8019_ADDRESS_PORT );
126 byte = inp( RTL8019_DATA_PIN );
146 void RTL8019setupPorts(
void)
148 volatile unsigned char *base = (
unsigned char *)0x8300;
150 #if MEMORY_MAPPED_NIC == 1
152 outp(inp(MCUCR) | (1<<SRE), MCUCR);
157 outp( 0xFF, RTL8019_ADDRESS_DDR );
160 outp( 0xFF, RTL8019_DATA_PORT );
165 sbi( RTL8019_CONTROL_DDR, RTL8019_CONTROL_READPIN );
166 sbi( RTL8019_CONTROL_DDR, RTL8019_CONTROL_WRITEPIN );
170 sbi( RTL8019_CONTROL_PORT, RTL8019_CONTROL_READPIN );
171 sbi( RTL8019_CONTROL_PORT, RTL8019_CONTROL_WRITEPIN );
176 sbi( RTL8019_RESET_DDR, RTL8019_RESET_PIN );
222 #define PAR0 0x01 // Page 1
223 #define CR9346 0x01 // Page 3
232 #define CURR 0x07 // Page 1
247 #define RDMAPORT 0x10
270 #define RCR_INIT 0x04
273 #define TCR_INIT 0x00
278 #define DCR_INIT 0x58
281 #define IMR_INIT 0x11
286 #define TXSTART_INIT 0x40
287 #define RXSTART_INIT 0x46
288 #define RXSTOP_INIT 0x60
292 void RTL8019beginPacketSend(
unsigned int packetLength)
295 volatile unsigned char *base = (
unsigned char *)0x8300;
296 unsigned int sendPacketLength;
297 sendPacketLength = (packetLength>=ETHERNET_MIN_PACKET_LENGTH) ?
298 packetLength : ETHERNET_MIN_PACKET_LENGTH ;
304 while( readRTL(CR) & 0x04 );
307 writeRTL(TPSR,TXSTART_INIT);
310 writeRTL(RSAR0,0x00);
311 writeRTL(RSAR1,0x40);
314 writeRTL(ISR,(1<<ISR_PTX));
317 writeRTL(RBCR0, (
unsigned char)(packetLength));
318 writeRTL(RBCR1, (
unsigned char)(packetLength>>8));
320 writeRTL(TBCR0, (
unsigned char)(sendPacketLength));
321 writeRTL(TBCR1, (
unsigned char)((sendPacketLength)>>8));
329 void RTL8019sendPacketData(
unsigned char * localBuffer,
unsigned int length)
332 volatile unsigned char *base = (
unsigned char *)0x8300;
333 for(i=0;i<length;i++)
334 writeRTL(RDMAPORT, localBuffer[i]);
339 void RTL8019endPacketSend(
void)
341 volatile unsigned char *base = (
unsigned char *)0x8300;
346 writeRTL(ISR, (1<<ISR_RDC));
353 static unsigned char nextPage;
354 static unsigned int currentRetreiveAddress;
357 #define enetpacketstatus 0x00
358 #define nextblock_ptr 0x01
359 #define enetpacketLenL 0x02
360 #define enetpacketLenH 0x03
364 unsigned int RTL8019beginPacketRetreive(
void)
366 volatile unsigned char *base = (
unsigned char *)0x8300;
370 unsigned char pageheader[4];
374 processRTL8019Interrupt();
384 bnry = readRTL(BNRY) ;
400 writeRTL(ISR, (1<<ISR_PRX));
404 if( (bnry >= RXSTOP_INIT) || (bnry < RXSTART_INIT) )
406 writeRTL(BNRY, RXSTART_INIT);
408 writeRTL(CURR, RXSTART_INIT);
417 writeRTL(RSAR1, bnry);
422 pageheader[i] = readRTL(RDMAPORT);
428 for(i = 0; i <= 20; i++) {
429 if(readRTL(ISR) & 1<<6) {
437 rxlen = (pageheader[enetpacketLenH]<<8) + pageheader[enetpacketLenL];
438 nextPage = pageheader[nextblock_ptr] ;
440 currentRetreiveAddress = (bnry<<8) + 4;
446 if( (nextPage >= RXSTOP_INIT) || (nextPage < RXSTART_INIT) ) {
455 void RTL8019retreivePacketData(
unsigned char * localBuffer,
unsigned int length)
458 volatile unsigned char *base = (
unsigned char *)0x8300;
460 writeRTL(RBCR0, (
unsigned char)length);
461 writeRTL(RBCR1, (
unsigned char)(length>>8));
462 writeRTL(RSAR0, (
unsigned char)currentRetreiveAddress);
463 writeRTL(RSAR1, (
unsigned char)(currentRetreiveAddress>>8));
465 for(i=0;i<length;i++)
466 localBuffer[i] = readRTL(RDMAPORT);
470 for(i = 0; i <= 20; i++)
471 if(readRTL(ISR) & 1<<6)
475 currentRetreiveAddress += length;
476 if( currentRetreiveAddress >= 0x6000 )
477 currentRetreiveAddress = currentRetreiveAddress - (0x6000-0x4600) ;
482 void RTL8019endPacketRetreive(
void)
484 volatile unsigned char *base = (
unsigned char *)0x8300;
489 for(i = 0; i <= 20; i++)
490 if(readRTL(ISR) & 1<<6)
495 writeRTL(BNRY, nextPage);
501 volatile unsigned char *base = (
unsigned char *)0x8300;
502 unsigned char data_L, resend;
504 data_L = readRTL(CR);
507 writeRTL(RBCR0, 0x00);
508 writeRTL(RBCR1, 0x00);
511 else if(data_L & 0x04)
513 data_L = readRTL(ISR);
514 if((data_L & 0x02) || (data_L & 0x08))
522 writeRTL(BNRY, RXSTART_INIT);
524 writeRTL(CURR, RXSTART_INIT);
527 writeRTL(TCR, TCR_INIT);
538 #define NIC_PAGE_SIZE 0x100
543 #define NIC_START_PAGE 0x40
548 #define NIC_STOP_PAGE 0x60
555 #define NIC_TX_PAGES 6
560 #define NIC_TX_BUFFERS 2
568 #define NIC_FIRST_TX_PAGE NIC_START_PAGE
569 #define NIC_FIRST_RX_PAGE (NIC_FIRST_TX_PAGE + NIC_TX_PAGES * NIC_TX_BUFFERS)
576 static unsigned char mac[6] = {0x00,0x06,0x98,0x01,0x02,0x29};
577 void Delay(
long nops)
581 for(i = 0; i < nops; i++)
582 #ifdef __IMAGECRAFT__
585 asm volatile(
"nop\n\t"::);
589 static int NicReset(
void)
591 volatile unsigned char *base = (
unsigned char *)0x8300;
595 for(j = 0; j < 20; j++) {
596 debug_print(PSTR(
"SW-Reset..."));
597 i = nic_read(NIC_RESET);
599 nic_write(NIC_RESET, i);
600 for(i = 0; i < 20; i++) {
606 if((nic_read(NIC_PG0_ISR) & NIC_ISR_RST) != 0 &&
607 nic_read(NIC_PG0_RBCR0) == 0x50 &&
608 nic_read(NIC_PG0_RBCR1) == 0x70) {
609 debug_print(PSTR(
"OK\r\n"));
613 debug_print(PSTR(
"failed\r\n\x07"));
621 debug_print(PSTR(
"Ethernut 1.1 HW-Reset\r\n"));
632 void initRTL8019(
void)
635 volatile unsigned char *base = (
unsigned char *)0x8300;
653 nic_write(NIC_PG0_IMR, 0);
654 nic_write(NIC_PG0_ISR, 0xff);
663 nic_write(
NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0 | NIC_CR_PS1);
669 nic_write(NIC_PG3_EECR, NIC_EECR_EEM0 | NIC_EECR_EEM1);
674 nic_write(NIC_PG3_CONFIG3, 0);
681 nic_write(NIC_PG3_CONFIG2, NIC_CONFIG2_BSELB);
687 nic_write(NIC_PG3_EECR, 0);
696 nic_write(
NIC_CR, NIC_CR_STP | NIC_CR_RD2);
697 nic_write(NIC_PG0_DCR, NIC_DCR_LS | NIC_DCR_FT1);
702 nic_write(NIC_PG0_RBCR0, 0);
703 nic_write(NIC_PG0_RBCR1, 0);
710 nic_write(NIC_PG0_RCR, NIC_RCR_MON);
711 nic_write(NIC_PG0_TCR, NIC_TCR_LB0);
719 nic_write(NIC_PG0_TPSR, NIC_FIRST_TX_PAGE);
720 nic_write(NIC_PG0_BNRY, NIC_STOP_PAGE - 1);
721 nic_write(NIC_PG0_PSTART, NIC_FIRST_RX_PAGE);
722 nic_write(NIC_PG0_PSTOP, NIC_STOP_PAGE);
727 nic_write(NIC_PG0_ISR, 0xff);
733 nic_write(
NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0);
734 for(i = 0; i < 6; i++)
735 nic_write(NIC_PG1_PAR0 + i, mac[i]);
740 for(i = 0; i < 8; i++)
741 nic_write(NIC_PG1_MAR0 + i, 0);
746 nic_write(NIC_PG1_CURR, NIC_START_PAGE + TX_PAGES);
751 nic_write(
NIC_CR, NIC_CR_STP | NIC_CR_RD2);
757 nic_write(NIC_PG0_RCR, NIC_RCR_AB);
762 nic_write(NIC_PG0_ISR, 0xff);
763 nic_write(NIC_PG0_IMR, NIC_IMR_PRXE | NIC_IMR_PTXE | NIC_IMR_RXEE |
764 NIC_IMR_TXEE | NIC_IMR_OVWE);
771 nic_write(
NIC_CR, NIC_CR_STA | NIC_CR_RD2);
772 nic_write(NIC_PG0_TCR, 0);
782 debug_print(PSTR(
"Init controller..."));
783 nic_write(NIC_PG0_IMR, 0);
784 nic_write(NIC_PG0_ISR, 0xff);
785 nic_write(
NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0 | NIC_CR_PS1);
786 nic_write(NIC_PG3_EECR, NIC_EECR_EEM0 | NIC_EECR_EEM1);
787 nic_write(NIC_PG3_CONFIG3, 0);
788 nic_write(NIC_PG3_CONFIG2, NIC_CONFIG2_BSELB);
789 nic_write(NIC_PG3_EECR, 0);
792 nic_write(
NIC_CR, NIC_CR_STP | NIC_CR_RD2);
793 nic_write(NIC_PG0_DCR, NIC_DCR_LS | NIC_DCR_FT1);
794 nic_write(NIC_PG0_RBCR0, 0);
795 nic_write(NIC_PG0_RBCR1, 0);
796 nic_write(NIC_PG0_RCR, NIC_RCR_MON);
797 nic_write(NIC_PG0_TCR, NIC_TCR_LB0);
798 nic_write(NIC_PG0_TPSR, NIC_FIRST_TX_PAGE);
799 nic_write(NIC_PG0_BNRY, NIC_STOP_PAGE - 1);
800 nic_write(NIC_PG0_PSTART, NIC_FIRST_RX_PAGE);
801 nic_write(NIC_PG0_PSTOP, NIC_STOP_PAGE);
802 nic_write(NIC_PG0_ISR, 0xff);
803 nic_write(
NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0);
804 for(i = 0; i < 6; i++)
805 nic_write(NIC_PG1_PAR0 + i, mac[i]);
806 for(i = 0; i < 8; i++)
807 nic_write(NIC_PG1_MAR0 + i, 0);
808 nic_write(NIC_PG1_CURR, NIC_START_PAGE + TX_PAGES);
809 nic_write(
NIC_CR, NIC_CR_STP | NIC_CR_RD2);
810 nic_write(NIC_PG0_RCR, NIC_RCR_AB);
811 nic_write(NIC_PG0_ISR, 0xff);
812 nic_write(NIC_PG0_IMR, 0);
813 nic_write(
NIC_CR, NIC_CR_STA | NIC_CR_RD2);
814 nic_write(NIC_PG0_TCR, 0);
819 nic_write(
NIC_CR, NIC_CR_STA | NIC_CR_RD2 | NIC_CR_PS0 | NIC_CR_PS1);
820 rb = nic_read(NIC_PG3_CONFIG0);
824 debug_print(PSTR(
"RTL8019AS "));
826 debug_print(PSTR(
"jumper mode: "));
828 debug_print(PSTR(
"AUI "));
830 debug_print(PSTR(
"PNP "));
833 debug_print(PSTR(
"RTL8019 "));
835 debug_print(PSTR(
"jumper mode: "));
838 debug_print(PSTR(
"Unknown chip "));
843 debug_print(PSTR(
"BNC\x07 "));
845 debug_print(PSTR(
"Failed\x07 "));
853 rb = nic_read(NIC_PG3_CONFIG2);
857 debug_print(PSTR(
"Auto "));
860 debug_print(PSTR(
"10BaseT "));
863 debug_print(PSTR(
"10Base5 "));
866 debug_print(PSTR(
"10Base2 "));
876 writeRTL( ISR, readRTL(ISR) ) ;
881 writeRTL(DCR, DCR_INIT);
882 writeRTL(RBCR0,0x00);
883 writeRTL(RBCR1,0x00);
885 writeRTL(TPSR, TXSTART_INIT);
887 writeRTL(PSTART, RXSTART_INIT);
888 writeRTL(BNRY, RXSTART_INIT);
889 writeRTL(PSTOP, RXSTOP_INIT);
892 writeRTL(CURR, RXSTART_INIT);
894 writeRTL(PAR0+0, MYMAC_0);
895 writeRTL(PAR0+1, MYMAC_1);
896 writeRTL(PAR0+2, MYMAC_2);
897 writeRTL(PAR0+3, MYMAC_3);
898 writeRTL(PAR0+4, MYMAC_4);
899 writeRTL(PAR0+5, MYMAC_5);
902 writeRTL(DCR, DCR_INIT);
905 writeRTL(IMR, IMR_INIT);
906 writeRTL(TCR, TCR_INIT);
912 void processRTL8019Interrupt(
void)
914 volatile unsigned char *base = (
unsigned char *)0x8300;
915 unsigned char byte = readRTL(ISR);
917 if( byte & (1<<ISR_OVW) )