POK
/home/jaouen/pok_official/pok/trunk/libpok/drivers/rtl8029.h
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Mon May 18 18:44:51 2009 
00015  */
00016 
00017 #ifdef POK_NEEDS_RTL8029
00018 # ifndef __POK_RTL8029_H__
00019 #  define __POK_RTL8029_H__
00020 
00021 #  include <libc/stdio.h>
00022 #  include <libc/string.h>
00023 #  include <arch/x86/pci.h>
00024 #  include <arch/x86/ioports.h>
00025 #  include <middleware/port.h>
00026 
00027 #  define ETH_DATA_MAXLEN       1500
00028 #  define ETH_DATA_MINLEN       46
00029 #  define ETH_MAC_LEN           6
00030 
00031 #  define RECV_BUF_SZ           20
00032 
00033 // Ethernet header
00034 typedef struct
00035 {
00036   char           dst[ETH_MAC_LEN];
00037   char           src[ETH_MAC_LEN];
00038   unsigned short ethertype;
00039 } __attribute__ ((__packed__)) eth_hdr_t;
00040 
00041 typedef struct
00042 {
00043   unsigned short src;
00044   unsigned short dst;
00045   unsigned short len;
00046   unsigned short chk; // not used
00047 } __attribute__ ((__packed__)) udp_hdr_t;
00048 
00049 #  define NET_DATA_MAXLEN       (ETH_DATA_MAXLEN - sizeof (udp_hdr_t))
00050 #  define NET_DATA_MINLEN       (ETH_DATA_MINLEN - sizeof (udp_hdr_t))
00051 
00052 typedef struct
00053 {
00054   eth_hdr_t     eth;
00055   udp_hdr_t     udp;
00056   char          data[NET_DATA_MAXLEN];
00057 } __attribute__ ((__packed__)) pok_packet_t;
00058 
00059 typedef struct
00060 {
00061   uint32_t      len;
00062   uint32_t      off;
00063   char          data[RECV_BUF_SZ];
00064 } __attribute__ ((__packed__)) pok_queue_t;
00065 
00066 typedef struct
00067 {
00068   s_pci_device  pci;
00069   unsigned int  addr;
00070   char          mac[6];
00071   pok_queue_t   recv_buf[20]; /* TODO: random constant */
00072 } s_ne2000_dev;
00073 
00074 typedef struct
00075 {
00076   unsigned char       status;
00077   unsigned char       next;
00078   unsigned short      size;
00079 } __attribute__ ((packed)) s_ne2000_header;
00080 
00081 /* CR: Command Register */
00082 #  define NE2000_CR     0x00     /* Register address */
00083 #  define NE2000_CR_STP (1 << 0) /* This bit is the STOP command. When it is
00084                                     set, no packets will be received or
00085                                     transmitted. POWER UP=1. */
00086 #  define NE2000_CR_STA (1 << 1) /* The STA bit controls nothing. It only
00087                                     reflects the value written to this bit.
00088                                     POWER UP=0. */
00089 #  define NE2000_CR_TXP (1 << 2) /* This bit must be set to transmit a packet.
00090                                     It is internally reset either after the
00091                                     transmission is completed or aborted. */
00092 #  define NE2000_CR_RD0 (1 << 3) /* See RD2-0 in the datasheet */
00093 #  define NE2000_CR_RD1 (1 << 4) /* See RD2-0 in the datasheet */
00094 #  define NE2000_CR_RD2 (1 << 5) /* See RD2-0 in the datasheet */
00095 #  define NE2000_CR_PS0 (1 << 6) /* See PS0 in the datasheet */
00096 #  define NE2000_CR_PS1 (1 << 7) /* See PS1 in the datasheet */
00097 
00098 /* ISR: Interrupt Status Register */
00099 #  define NE2000_ISR    0x07     /* Register address */
00100 
00101 #  define NE2000_ISR_PRX        (1 << 0) /* This bit indicates packet received with no
00102                                     errors. */
00103 #  define NE2000_ISR_PTX        (1 << 1) /* This bit indicates packet transmitted with
00104                                     no errors. */
00105 #  define NE2000_ISR_RXE        (1 << 2) /* This bit is set when a packet received
00106                                     with one or more of the following errors:
00107                                       - CRC error
00108                                       - Frame alignment error
00109                                       - Missed packet */
00110 #  define NE2000_ISR_TXE        (1 << 3) /* Transmit error bit is set when a packet
00111                                     transmission is aborted due to excessive
00112                                     collisions. */
00113 #  define NE2000_ISR_OVW        (1 << 4) /* This bit is set when the receive buffer
00114                                     has been exhausted. */
00115 #  define NE2000_ISR_CNT        (1 << 5) /* Set when MSB of one or more of the network
00116                                     tally counters has been set. */
00117 #  define NE2000_ISR_RDC        (1 << 6) /* Set when remote DMA operation has been
00118                                     completed. */
00119 #  define NE2000_ISR_RST (1 << 7) /* This bit is set when NIC enters reset state
00120                                     and is cleared when a start command is
00121                                     issued to the CR. It is also set when
00122                                     receive buffer overflows and is cleared
00123                                     when one or more packets have been read from
00124                                     the buffer. */
00125 
00126 /* DCR: Data Configuration Register */
00127 #  define NE2000_DCR    0x0E     /* Register address */
00128 #  define NE2000_DCR_WTS        (1 << 0) /* Word Transfer Select
00129                                       0: byte-wide DMA transfer
00130                                       1: word-wide DMA transfer */
00131 #  define NE2000_DCR_BOS        (1 << 1) /* Byte Order Select
00132                                       0: MS byte placed on MD15-8 and LS byte on MD7-0. (32xxx,80x86)
00133                                       1: MS byte placed on MD7-0 and LS byte on MD15-8. (680x0) */
00134 #  define NE2000_DCR_LAS        (1 << 2) /* This bit must be set to zero. NIC only supports dual 16-bit DMA
00135                                     mode. POWER UP =1 */
00136 #  define NE2000_DCR_LS (1 << 3) /* Loopback Select
00137                                       0: Loopback mode selected. Bits 1 and 2 of the TCR must also be
00138                                       programmed for Loopback operation.
00139                                       1: Normal Operation */
00140 #  define NE2000_DCR_ARM        (1 << 4) /* Auto-initialize Remote
00141                                       0: Send Packet Command not executed.
00142                                       1: Send Packet Command executed.
00143                                     This bit is set when the receive buffer */
00144 #  define NE2000_DCR_FT0        (1 << 5) /* FIFO threshold select bit 0. */
00145 #  define NE2000_DCR_FT1        (1 << 6) /* FIFO threshold select bit 1. */
00146 
00147 /* RCR: Receive Configuration Register */
00148 #  define NE2000_RCR    0x0C     /* Register address */
00149 #  define NE2000_RCR_AB (1 << 2) /* If AB=1, packets with broadcast destination
00150                                     address are accepted.
00151                                     If AB=0, packets with broadcast destination
00152                                     address are rejected.*/
00153 
00154 /* RSR: Receive Status Register */
00155 #  define NE2000_RSR    0x0C     /* Register address */
00156 #  define NE2000_RSR_PRX        (1 << 0) /* This bit indicates packet received with
00157                                     no errors.*/
00158 
00159 /* IMR: Interrupt Mask Register */
00160 #  define NE2000_IMR    0x0F     /* Register address */
00161 
00162 /* PSTART: Page Start Register */
00163 #  define NE2000_PSTART 0x01     /* Register address */
00164 
00165 /* PSTOP: Page Stop Register */
00166 #  define NE2000_PSTOP  0x02     /* Register address */
00167 
00168 /* BNRY: Boundary Register */
00169 #  define NE2000_BNRY   0x03     /* Register address */
00170 
00171 /* TPSR: Transmit Page Start Register */
00172 #  define NE2000_TPSR   0x04     /* Register address */
00173 
00174 /* TCR: Transmit Configuration Register */
00175 #  define NE2000_TCR    0x0D     /* Register address */
00176 
00177 /* CURR: Current Page Register */
00178 #  define NE2000_CURR   0x07     /* Register address */
00179 
00180 /* TBCR0,1: Transmit Byte Count Registers */
00181 #  define NE2000_TBCR0  0x05     /* Register address */
00182 #  define NE2000_TBCR1  0x06     /* Register address */
00183 
00184 
00185 /* RSAR0,1: Remote Start Address Registers */
00186 #  define NE2000_RSAR0  0x08     /* Register address */
00187 #  define NE2000_RSAR1  0x09     /* Register address */
00188 
00189 /* RBCR0,1: Remote Byte Count Registers */
00190 #  define NE2000_RBCR0  0x0A     /* Register address */
00191 #  define NE2000_RBCR1  0x0B     /* Register address */
00192 
00193 /* PAR0-5: Physical Address Registers */
00194 #  define NE2000_PAR0   0x01     /* Register address */
00195 #  define NE2000_PAR1   0x02     /* Register address */
00196 #  define NE2000_PAR2   0x03     /* Register address */
00197 #  define NE2000_PAR3   0x04     /* Register address */
00198 #  define NE2000_PAR4   0x05     /* Register address */
00199 #  define NE2000_PAR5   0x06     /* Register address */
00200 
00201 
00202 /* MAR0-7: Multicast Address Register */
00203 #  define NE2000_MAR    0x08     /* Register address */
00204 
00205 /* Remote DMA Port */
00206 #  define NE2000_DMA_PORT 0x10   /* Register address */
00207 
00208 
00209 #  define NE2000_MEMSZ  128     // device memory size in 256 bytes block
00210 #  define NE2000_TXBUF  64      // tx buffer offset in 256 bytes block
00211 #  define NE2000_TXBUFSZ 8      // tx buffer size in 256 bytes block
00212 #  define NE2000_RXBUF  (NE2000_TXBUF + NE2000_TXBUFSZ) // rx buf offset
00213                                                         // in 256 bytes block
00214 #  define NE2000_RXBUFSZ (NE2000_MEMSZ - NE2000_TXBUFSZ) // rx buf size
00215                                                         // in 256 bytes block
00216 #  define outb(a,b) pok_outb((b), (a))
00217 
00218 # endif /* !__POK_RTL8029_H__ */
00219 #endif /* POK_NEEDS_RTL8029 */