Guest User

PrettyOS - rtl8139.c

a guest
Jul 31st, 2011
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.61 KB | None | 0 0
  1. /*
  2. *  license and disclaimer for the use of this source code as per statement below
  3. *  Lizenz und Haftungsausschluss für die Verwendung dieses Sourcecodes siehe unten
  4. */
  5.  
  6. #include "rtl8139.h"
  7. #include "util.h"
  8. #include "timer.h"
  9. #include "paging.h"
  10. #include "video/console.h"
  11. #include "kheap.h"
  12.  
  13. /// Information: The eeprom code for the RTL8139 (rtl8139_eeprom.h/c) has been deleted in rev. 1002. Check out this revision to access it.
  14.  
  15. #define RTL8139_RX_BUFFER_SIZE 0x2000 // 8 KiB
  16. #define RTL8139_TX_BUFFER_SIZE 4096
  17.  
  18.  
  19. void rtl8139_handler(registers_t* data, pciDev_t* device)
  20. {
  21.     network_adapter_t* adapter = device->data;
  22.     RTL8139_networkAdapter_t* rAdapter = adapter->data;
  23.  
  24.     #ifdef _NETWORK_DIAGNOSIS_
  25.     textColor(HEADLINE);
  26.     printf("\n--------------------------------------------------------------------------------");
  27.     #endif
  28.  
  29.     // read bytes 003Eh bis 003Fh, Interrupt Status Register
  30.     volatile uint16_t val = *((uint16_t*)(adapter->MMIO_base + RTL8139_INTRSTATUS));
  31.     #ifdef _NETWORK_DIAGNOSIS_
  32.     textColor(YELLOW);
  33.     printf("\nRTL8139 Interrupt Status: %yh, ", val);
  34.     textColor(ERROR);
  35.     if      (val & RTL8139_INT_RX_OK)           { textColor(SUCCESS); puts("Receive OK"); }
  36.     else if (val & RTL8139_INT_RX_ERR)          { puts("Receive Error"); }
  37.     else if (val & RTL8139_INT_TX_OK)           { textColor(SUCCESS); puts("Transmit OK"); }
  38.     else if (val & RTL8139_INT_TX_ERR)          { puts("Transmit Error"); }
  39.     else if (val & RTL8139_INT_RXBUF_OVERFLOW)  { puts("Rx Buffer Overflow");}
  40.     else if (val & RTL8139_INT_RXFIFO_UNDERRUN) { puts("Packet Underrun / Link change");}
  41.     else if (val & RTL8139_INT_RXFIFO_OVERFLOW) { puts("Rx FIFO Overflow");}
  42.     else if (val & RTL8139_INT_CABLE)           { puts("Cable Length Change");}
  43.     else if (val & RTL8139_INT_TIMEOUT)         { puts("Time Out");}
  44.     else if (val & RTL8139_INT_PCIERR)          { puts("System Error");}
  45.     textColor(TEXT);
  46.     #endif
  47.  
  48.     if (val & RTL8139_INT_RX_OK)
  49.     {
  50.     uint32_t length = (rAdapter->RxBuffer[rAdapter->RxBufferPointer+3] << 8) + rAdapter->RxBuffer[rAdapter->RxBufferPointer+2]; // Little Endian.
  51.  
  52.     // Display RTL8139 specific data
  53.     #ifdef _NETWORK_DATA_
  54.     textColor(HEADLINE);
  55.     printf("\nFlags: ");
  56.     textColor(TEXT);
  57.     for (uint8_t i = 0; i < 2; i++)
  58.     {
  59.         printf("%yh ", rAdapter->RxBuffer[rAdapter->RxBufferPointer+i]);
  60.     }
  61.     #endif
  62.  
  63.     // Inform network interface about the packet
  64.     network_receivedPacket(adapter, &rAdapter->RxBuffer[rAdapter->RxBufferPointer]+4, length - 4); // Strip CRC from packet.
  65.  
  66.     *((uint16_t*)(adapter->MMIO_base + RTL8139_INTRSTATUS)) = val; // reset interrupts by writing 1 to the bits of offset 003Eh to 003Fh, Interrupt Status Register
  67.  
  68.     // Increase RxBufferPointer
  69.     rAdapter->RxBufferPointer += length + 4;
  70.     rAdapter->RxBufferPointer = alignUp(rAdapter->RxBufferPointer, 4); // packets are DWORD aligned
  71.     rAdapter->RxBufferPointer %= RTL8139_RX_BUFFER_SIZE; // handle wrap-around
  72.  
  73.     // set read pointer
  74.     *((uint16_t*)(adapter->MMIO_base + RTL8139_RXBUFTAIL)) = rAdapter->RxBufferPointer - 0x10; // 0x10 = 16
  75.  
  76.     #ifdef _NETWORK_DIAGNOSIS_
  77.     printf("RXBUFTAIL: %u", *((uint16_t*)(adapter->MMIO_base + RTL8139_RXBUFTAIL)));
  78.     #endif
  79.     }
  80.     else
  81.         *((uint16_t*)(adapter->MMIO_base + RTL8139_INTRSTATUS)) = val; // reset interrupts by writing 1 to the bits of offset 003Eh to 003Fh, Interrupt Status Register
  82. }
  83.  
  84.  
  85. void install_RTL8139(network_adapter_t* adapter)
  86. {
  87.     RTL8139_networkAdapter_t* rAdapter = malloc(sizeof(RTL8139_networkAdapter_t), 0, "RTL8139");
  88.     rAdapter->device = adapter;
  89.     adapter->data = rAdapter;
  90.  
  91.     rAdapter->RxBuffer = malloc(RTL8139_RX_BUFFER_SIZE, 4, "RTL8139-RxBuf");
  92.     rAdapter->RxBufferPointer = 0;
  93.     memset(rAdapter->RxBuffer, 0, RTL8139_RX_BUFFER_SIZE); // clear receiving buffer
  94.  
  95.     rAdapter->TxBuffer = malloc(RTL8139_TX_BUFFER_SIZE, 4, "RTL8139-TxBuf");
  96.     rAdapter->TxBufferPhys = paging_getPhysAddr(rAdapter->TxBuffer);
  97.     rAdapter->TxBufferIndex = 0;
  98.  
  99.     /*
  100.     http://wiki.osdev.org/RTL8139
  101.  
  102.     Turning on the RTL8139:
  103.     Send 0x00 to the CONFIG_1 register (0x52) to set the LWAKE + LWPTN to active high.
  104.     this should essentially "power on" the device.
  105.  
  106.     Software Reset:
  107.     Sending 0x10 to the Command register (0x37) will send the RTL8139 into a software reset.
  108.     Once that byte is sent, the RST bit must be checked to make sure that the chip has finished the reset.
  109.     If the RST bit is 1, then the reset is still in operation.
  110.  
  111.     Init Receive Buffer:
  112.     Send the chip a memory location to use as its receive buffer start location.
  113.     One way to do it, would be to define a buffer variable
  114.     and send that variables memory location to the RBSTART register (0x30).
  115.     */
  116.  
  117.     kdebug(3, "\nRTL8139 MMIO: %Xh", adapter->MMIO_base);
  118.     adapter->MMIO_base = paging_acquirePciMemory((uint32_t)adapter->MMIO_base, 1);
  119.  
  120.   #ifdef _NETWORK_DIAGNOSIS_
  121.     printf("\nMMIO base mapped to virt. addr. %Xh", adapter->MMIO_base);
  122.   #endif
  123.  
  124.     // "power on" the card
  125.     *((uint8_t*)(adapter->MMIO_base + RTL8139_CONFIG1)) = 0x00;
  126.  
  127.     // carry out reset of network card: set bit 4 at offset 0x37 (1 Byte)
  128.     *((uint8_t*)(adapter->MMIO_base + RTL8139_CHIPCMD)) = RTL8139_CMD_RESET;
  129.  
  130.     // wait for the reset of the "reset flag"
  131.     uint32_t k=0;
  132.     while (true)
  133.     {
  134.         delay(10000);
  135.         if (!(*((volatile uint8_t*)(adapter->MMIO_base + RTL8139_CHIPCMD)) & RTL8139_CMD_RESET))
  136.         {
  137.             kdebug(3, "\nwaiting successful (%d).\n", k);
  138.             break;
  139.         }
  140.         k++;
  141.         if (k > 100)
  142.         {
  143.             printf("\nWaiting not successful! Finished by timeout.\n");
  144.             break;
  145.         }
  146.     }
  147.  
  148.     // now we set the RE and TE bits from the "Command Register" to Enable Receiving and Transmission
  149.     // activate transmitter and receiver: Set bit 2 (TE) and 3 (RE) in control register 0x37 (1 byte).
  150.     *((uint8_t*)(adapter->MMIO_base + RTL8139_CHIPCMD)) = RTL8139_CMD_RX_ENABLE | RTL8139_CMD_TX_ENABLE;
  151.  
  152.     // set TCR (transmit configuration register, 0x40, 4 byte)
  153.     *((uint32_t*)(adapter->MMIO_base + RTL8139_TXCONFIG)) = 0x03000700;       // TCR
  154.  
  155.     // set RCR (receive configuration register, RTL8139_RXCONFIG, 4 byte)
  156.     /*
  157.     bit4 AR  - Accept Runt
  158.     bit3 AB  - Accept Broadcast:      Accept broadcast packets sent to mac FF:FF:FF:FF:FF:FF
  159.     bit2 AM  - Accept Multicast:      Accept multicast packets.
  160.     bit1 APM - Accept Physical Match: Accept packets send to NIC's MAC address.
  161.     bit0 AAP - Accept All Packets
  162.     */
  163.  
  164.     // bit 12:11 defines the size of the Rx ring buffer length
  165.     // 00b:  8K + 16 byte       01b: 16K + 16 byte
  166.     // 10b: 32K + 16 byte       11b: 64K + 16 byte
  167.  
  168.     *((uint32_t*)(adapter->MMIO_base + RTL8139_RXCONFIG)) = 0x0000071A; // 11100011010  // RCR
  169.  
  170.     // physical address of the receive buffer has to be written to RBSTART (0x30, 4 byte)
  171.     *((uint32_t*)(adapter->MMIO_base + RTL8139_RXBUF)) = paging_getPhysAddr(rAdapter->RxBuffer);
  172.  
  173.     // set interrupt mask
  174.     *((uint16_t*)(adapter->MMIO_base + RTL8139_INTRMASK)) = 0xFFFF; // all interrupts
  175.  
  176.     for (uint8_t i = 0; i < 6; i++)
  177.     {
  178.         adapter->MAC[i] =  *(uint8_t*)(adapter->MMIO_base + RTL8139_IDR0 + i);
  179.     }
  180. }
  181.  
  182. /*
  183. The process of transmitting a packet with RTL8139:
  184. 1: copy the packet to a physically continuous buffer in memory.
  185. 2: Write the descriptor which is functioning
  186.   (1). Fill in Start Address(physical address) of this buffer.
  187.   (2). Fill in Transmit Status: the size of this packet, the early transmit threshold, Clear OWN bit in TSD (this starts the PCI operation).
  188. 3: As the number of data moved to FIFO meet early transmit threshold, the chip start to move data from FIFO to line..
  189. 4: When the whole packet is moved to FIFO, the OWN bit is set to 1.
  190. 5: When the whole packet is moved to line, the TOK(in TSD) is set to 1.
  191. 6: If TOK(IMR) is set to 1 and TOK(ISR) is set then a interrupt is triggered.
  192. 7: Interrupt service routine called, driver should clear TOK(ISR) State Diagram: (TOK,OWN)
  193. */
  194. bool rtl8139_send(network_adapter_t* adapter, uint8_t* data, size_t length)
  195. {
  196.     RTL8139_networkAdapter_t* rAdapter = adapter->data;
  197.  
  198.     memcpy(rAdapter->TxBuffer, data, length); // tx buffer
  199.     if(length < 60) // Fill buffer to a minimal length of 60
  200.     {
  201.         memset(rAdapter->TxBuffer+length, 0, 60-length);
  202.         length = 60;
  203.     }
  204.  
  205.   #ifdef _NETWORK_DIAGNOSIS_
  206.     printf("\n\n>>> Transmission starts <<<\nPhysical Address of Tx Buffer = %Xh\n", rAdapter->TxBufferPhys);
  207.   #endif
  208.  
  209.     // set address and size of the Tx buffer
  210.     // reset OWN bit in TASD (REG_TRANSMIT_STATUS) starting transmit
  211.     // set transmit FIFO threshhold to 48*32 = 1536 bytes to avoid tx underrun
  212.     *((uint32_t*)(adapter->MMIO_base + RTL8139_TXADDR0   + 4 * rAdapter->TxBufferIndex)) = rAdapter->TxBufferPhys;
  213.     *((uint32_t*)(adapter->MMIO_base + RTL8139_TXSTATUS0 + 4 * rAdapter->TxBufferIndex)) = length | (48 << 16);
  214.  
  215.     rAdapter->TxBufferIndex++;
  216.     rAdapter->TxBufferIndex %= 4;
  217.   #ifdef _NETWORK_DIAGNOSIS_
  218.     textColor(LIGHT_BLUE);
  219.     printf("\n>> Packet sent. <<");
  220.   #endif
  221.     return true;
  222. }
  223.  
  224. /*
  225. * Copyright (c) 2009-2011 The PrettyOS Project. All rights reserved.
  226. *
  227. * http://www.c-plusplus.de/forum/viewforum-var-f-is-62.html
  228. *
  229. * Redistribution and use in source and binary forms, with or without modification,
  230. * are permitted provided that the following conditions are met:
  231. *
  232. * 1. Redistributions of source code must retain the above copyright notice,
  233. *    this list of conditions and the following disclaimer.
  234. *
  235. * 2. Redistributions in binary form must reproduce the above copyright
  236. *    notice, this list of conditions and the following disclaimer in the
  237. *    documentation and/or other materials provided with the distribution.
  238. *
  239. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  240. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  241. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  242. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
  243. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  244. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  245. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  246. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  247. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  248. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  249. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  250. */
Advertisement
Add Comment
Please, Sign In to add comment