Subversion Repositories Kolibri OS

Rev

Rev 330 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;;  RTL8139.INC                                                    ;;
  4. ;;                                                                 ;;
  5. ;;  Ethernet driver for Menuet OS                                  ;;
  6. ;;                                                                 ;;
  7. ;;  Version 0.2  11 August 2003                                    ;;
  8. ;;                                                                 ;;
  9. ;;  Driver for chips of RealTek 8139 family                        ;;
  10. ;;  References:                                                    ;;
  11. ;;    www.realtek.com.hw - data sheets                             ;;
  12. ;;    rtl8139.c - linux driver                                     ;;
  13. ;;    8139too.c - linux driver                                     ;;
  14. ;;    ethernet driver template by Mike Hibbett                     ;;
  15. ;;                                                                 ;;
  16. ;;  The copyright statement is                                     ;;
  17. ;;                                                                 ;;
  18. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  19. ;;             Version 2, June 1991                                ;;
  20. ;;                                                                 ;;
  21. ;;  Copyright 2003 Endre Kozma,                                    ;;
  22. ;;   endre.kozma@axelero.hu                                        ;;
  23. ;;                                                                 ;;
  24. ;;  See file COPYING for details                                   ;;
  25. ;;                                                                 ;;
  26. ;;  10.01.2007 Bugfix for l8139_transmit from Paolo Franchetti     ;;
  27. ;;                                                                 ;;
  28. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  29.         ETH_ALEN               equ 6
  30.         ETH_HLEN               equ (2 * ETH_ALEN + 2)
  31.         ETH_ZLEN               equ 60 ; 60 + 4bytes auto payload for
  32.                                       ; mininmum 64bytes frame length
  33.  
  34.         PCI_REG_COMMAND        equ 0x04 ; command register
  35.         PCI_BIT_PIO            equ 0 ; bit0: io space control
  36.         PCI_BIT_MMIO           equ 1 ; bit1: memory space control
  37.         PCI_BIT_MASTER         equ 2 ; bit2: device acts as a PCI master
  38.  
  39.         RTL8139_REG_MAR0       equ 0x08 ; multicast filter register 0
  40.         RTL8139_REG_MAR4       equ 0x0c ; multicast filter register 4
  41.         RTL8139_REG_TSD0       equ 0x10 ; transmit status of descriptor
  42.         RTL8139_REG_TSAD0      equ 0x20 ; transmit start address of descriptor
  43.         RTL8139_REG_RBSTART    equ 0x30 ; RxBuffer start address
  44.         RTL8139_REG_COMMAND    equ 0x37 ; command register
  45.         RTL8139_REG_CAPR       equ 0x38 ; current address of packet read
  46.         RTL8139_REG_IMR        equ 0x3c ; interrupt mask register
  47.         RTL8139_REG_ISR        equ 0x3e ; interrupt status register
  48.         RTL8139_REG_TXCONFIG   equ 0x40 ; transmit configuration register
  49.         RTL8139_REG_TXCONFIG_0 equ 0x40 ; transmit configuration register 0
  50.         RTL8139_REG_TXCONFIG_1 equ 0x41 ; transmit configuration register 1
  51.         RTL8139_REG_TXCONFIG_2 equ 0x42 ; transmit configuration register 2
  52.         RTL8139_REG_TXCONFIG_3 equ 0x43 ; transmit configuration register 3
  53.         RTL8139_REG_RXCONFIG   equ 0x44 ; receive configuration register 0
  54.         RTL8139_REG_RXCONFIG_0 equ 0x44 ; receive configuration register 0
  55.         RTL8139_REG_RXCONFIG_1 equ 0x45 ; receive configuration register 1
  56.         RTL8139_REG_RXCONFIG_2 equ 0x46 ; receive configuration register 2
  57.         RTL8139_REG_RXCONFIG_3 equ 0x47 ; receive configuration register 3
  58.         RTL8139_REG_MPC        equ 0x4c ; missed packet counter
  59.         RTL8139_REG_9346CR     equ 0x50 ; serial eeprom 93C46 command register
  60.         RTL8139_REG_CONFIG1    equ 0x52 ; configuration register 1
  61.         RTL8139_REG_CONFIG4    equ 0x5a ; configuration register 4
  62.         RTL8139_REG_HLTCLK     equ 0x5b ; undocumented halt clock register
  63.         RTL8139_REG_BMCR       equ 0x62 ; basic mode control register
  64.         RTL8139_REG_ANAR       equ 0x66 ; auto negotiation advertisement register
  65.  
  66. ; 5.1 packet header
  67.         RTL8139_BIT_RUNT       equ 4 ; total packet length < 64 bytes
  68.         RTL8139_BIT_LONG       equ 3 ; total packet length > 4k
  69.         RTL8139_BIT_CRC        equ 2 ; crc error occured
  70.         RTL8139_BIT_FAE        equ 1 ; frame alignment error occured
  71.         RTL8139_BIT_ROK        equ 0 ; received packet is ok
  72. ; 5.4 command register
  73.         RTL8139_BIT_RST        equ 4 ; reset bit
  74.         RTL8139_BIT_RE         equ 3 ; receiver enabled
  75.         RTL8139_BIT_TE         equ 2 ; transmitter enabled
  76.         RTL8139_BIT_BUFE       equ 0 ; rx buffer is empty, no packet stored
  77. ; 5.6 interrupt status register
  78.         RTL8139_BIT_ISR_TOK    equ 2 ; transmit ok
  79.         RTL8139_BIT_ISR_RER    equ 1 ; receive error interrupt
  80.         RTL8139_BIT_ISR_ROK    equ 0 ; receive ok
  81. ; 5.7 transmit configyration register
  82.         RTL8139_BIT_TX_MXDMA   equ 8 ; Max DMA burst size per Tx DMA burst
  83.         RTL8139_BIT_TXRR       equ 4 ; Tx Retry count 16+(TXRR*16)
  84. ; 5.8 receive configuration register
  85.         RTL8139_BIT_RXFTH      equ 13 ; Rx fifo threshold
  86.         RTL8139_BIT_RBLEN      equ 11 ; Ring buffer length indicator
  87.         RTL8139_BIT_RX_MXDMA   equ 8 ; Max DMA burst size per Rx DMA burst
  88.         RTL8139_BIT_NOWRAP     equ 7 ; transfered data wrapping
  89.         RTL8139_BIT_9356SEL    equ 6 ; eeprom selector 9346/9356
  90.         RTL8139_BIT_AER        equ 5 ; accept error packets
  91.         RTL8139_BIT_AR         equ 4 ; accept runt packets
  92.         RTL8139_BIT_AB         equ 3 ; accept broadcast packets
  93.         RTL8139_BIT_AM         equ 2 ; accept multicast packets
  94.         RTL8139_BIT_APM        equ 1 ; accept physical match packets
  95.         RTL8139_BIT_AAP        equ 0 ; accept all packets
  96. ; 5.9 93C46/93C56 command register
  97.         RTL8139_BIT_93C46_EEM1 equ 7 ; RTL8139 eeprom operating mode1
  98.         RTL8139_BIT_93C46_EEM0 equ 6 ; RTL8139 eeprom operating mode0
  99.         RTL8139_BIT_93C46_EECS equ 3 ; chip select
  100.         RTL8139_BIT_93C46_EESK equ 2 ; serial data clock
  101.         RTL8139_BIT_93C46_EEDI equ 1 ; serial data input
  102.         RTL8139_BIT_93C46_EEDO equ 0 ; serial data output
  103. ; 5.11 configuration register 1
  104.         RTL8139_BIT_LWACT      equ 4 ; see RTL8139_REG_CONFIG1
  105.         RTL8139_BIT_SLEEP      equ 1 ; sleep bit at older chips
  106.         RTL8139_BIT_PWRDWN     equ 0 ; power down bit at older chips
  107.         RTL8139_BIT_PMEn       equ 0 ; power management enabled
  108. ; 5.14 configuration register 4
  109.         RTL8139_BIT_LWPTN      equ 2 ; see RTL8139_REG_CONFIG4
  110. ; 6.2 transmit status register
  111.         RTL8139_BIT_ERTXTH     equ 16 ; early TX threshold
  112.         RTL8139_BIT_TOK        equ 15 ; transmit ok
  113.         RTL8139_BIT_OWN        equ 13 ; tx DMA operation is completed
  114. ; 6.18 basic mode control register
  115.         RTL8139_BIT_ANE        equ 12 ; auto negotiation enable
  116. ; 6.20 auto negotiation advertisement register
  117.         RTL8139_BIT_TXFD       equ 8 ; 100base-T full duplex
  118.         RTL8139_BIT_TX         equ 7 ; 100base-T
  119.         RTL8139_BIT_10FD       equ 6 ; 10base-T full duplex
  120.         RTL8139_BIT_10         equ 5 ; 10base-T
  121.         RTL8139_BIT_SELECTOR   equ 0 ; binary encoded selector CSMA/CD=00001
  122. ; RX/TX buffer size
  123.         RTL8139_RBLEN          equ 0 ; 0==8K 1==16k 2==32k 3==64k
  124.         RTL8139_RX_BUFFER_SIZE equ (8192 shl RTL8139_RBLEN)
  125.         MAX_ETH_FRAME_SIZE     equ 1516 ; exactly 1514 wthout CRC
  126.         RTL8139_NUM_TX_DESC    equ 4
  127.         RTL8139_TX_BUFFER_SIZE equ (MAX_ETH_FRAME_SIZE * RTL8139_NUM_TX_DESC)
  128.         RTL8139_TXRR           equ 8 ; total retries = 16+(TXRR*16)
  129.         RTL8139_TX_MXDMA       equ 6 ; 0==16 1==32 2==64 3==128
  130.                                      ; 4==256 5==512 6==1024 7==2048
  131.         RTL8139_ERTXTH         equ 8 ; in unit of 32 bytes e.g:(8*32)=256
  132.         RTL8139_RX_MXDMA       equ 7 ; 0==16 1==32 2==64 3==128
  133.                                      ; 4==256 5==512 6==1024 7==unlimited
  134.         RTL8139_RXFTH          equ 7 ; 0==16 1==32 2==64 3==128
  135.                                      ; 4==256 5==512 6==1024 7==no threshold
  136.         RTL8139_RX_CONFIG      equ ((RTL8139_RBLEN shl RTL8139_BIT_RBLEN) \
  137.                                     or (RTL8139_RX_MXDMA shl RTL8139_BIT_RX_MXDMA) \
  138.                                     or (1 shl RTL8139_BIT_NOWRAP) \
  139.                                     or (RTL8139_RXFTH shl RTL8139_BIT_RXFTH) \
  140.                                     or (1 shl RTL8139_BIT_AB) or (1 shl RTL8139_BIT_APM) \
  141.                                     or (1 shl RTL8139_BIT_AER) or (1 shl RTL8139_BIT_AR) \
  142.                                     or (1 shl RTL8139_BIT_AM))
  143.         RTL8139_TX_TIMEOUT     equ 30 ; 300 milliseconds timeout
  144.  
  145.         EE_93C46_REG_ETH_ID    equ 7 ; MAC offset
  146.         EE_93C46_READ_CMD      equ (6 shl 6) ; 110b + 6bit address
  147.         EE_93C56_READ_CMD      equ (6 shl 8) ; 110b + 8bit address
  148.         EE_93C46_CMD_LENGTH    equ 9 ; start bit + cmd + 6bit address
  149.         EE_93C56_CMD_LENGTH    equ 11 ; start bit + cmd + 8bit ddress
  150.  
  151.         VER_RTL8139            equ 1100000b
  152.         VER_RTL8139A           equ 1110000b
  153. ;       VER_RTL8139AG          equ 1110100b
  154.         VER_RTL8139B           equ 1111000b
  155.         VER_RTL8130            equ VER_RTL8139B
  156.         VER_RTL8139C           equ 1110100b
  157.         VER_RTL8100            equ 1111010b
  158.         VER_RTL8100B           equ 1110101b
  159.         VER_RTL8139D           equ VER_RTL8100B
  160.         VER_RTL8139CP          equ 1110110b
  161.         VER_RTL8101            equ 1110111b
  162.  
  163.         IDX_RTL8139            equ 0
  164.         IDX_RTL8139A           equ 1
  165.         IDX_RTL8139B           equ 2
  166.         IDX_RTL8139C           equ 3
  167.         IDX_RTL8100            equ 4
  168.         IDX_RTL8139D           equ 5
  169.         IDX_RTL8139D           equ 6
  170.         IDX_RTL8101            equ 7
  171.  
  172.  
  173. ; These two must be 4 byte aligned ( which they are )
  174. rtl8139_rx_buff     equ     eth_data_start
  175. rtl8139_tx_buff     equ     rtl8139_rx_buff + (RTL8139_RX_BUFFER_SIZE + MAX_ETH_FRAME_SIZE)
  176.  
  177. uglobal
  178.         align   4
  179. rtl8139_rx_buff_offset: dd 0
  180. curr_tx_desc dd 0
  181. endg
  182.  
  183. iglobal
  184. hw_ver_array: db VER_RTL8139, VER_RTL8139A, VER_RTL8139B, VER_RTL8139C
  185.               db VER_RTL8100, VER_RTL8139D, VER_RTL8139CP, VER_RTL8101
  186. HW_VER_ARRAY_SIZE = $-hw_ver_array
  187. endg
  188.  
  189. uglobal
  190. hw_ver_id: db 0
  191. endg
  192.  
  193. ;***************************************************************************
  194. ;   Function
  195. ;      rtl8139_probe
  196. ;   Description
  197. ;      Searches for an ethernet card, enables it and clears the rx buffer
  198. ;      If a card was found, it enables the ethernet -> TCPIP link
  199. ;   Destroyed registers
  200. ;      eax, ebx, ecx, edx
  201. ;
  202. ;***************************************************************************
  203. rtl8139_probe:
  204. ; enable the device
  205.         mov     al, 2
  206.         mov     ah, [pci_bus]
  207.         mov     bh, [pci_dev]
  208.         mov     bl, PCI_REG_COMMAND
  209.         call    pci_read_reg
  210.         mov     cx, ax
  211.         or      cl, (1 shl PCI_BIT_MASTER) or (1 shl PCI_BIT_PIO)
  212.         and     cl, not (1 shl PCI_BIT_MMIO)
  213.         mov     al, 2
  214.         mov     ah, [pci_bus]
  215.         mov     bh, [pci_dev]
  216.         mov     bl, PCI_REG_COMMAND
  217.         call    pci_write_reg
  218. ; get chip version
  219.         mov     edx, [io_addr]
  220.         add     edx, RTL8139_REG_TXCONFIG_2
  221.         in      ax, dx
  222.         shr     ah, 2
  223.         shr     ax, 6
  224.         and     al, 01111111b
  225.         mov     ecx, HW_VER_ARRAY_SIZE-1
  226. .chip_ver_loop:
  227.         cmp     al, [hw_ver_array+ecx]
  228.         je      .chip_ver_found
  229.         dec     ecx
  230.         jns     .chip_ver_loop
  231.         xor     cl, cl ; default RTL8139
  232. .chip_ver_found:
  233.         mov     [hw_ver_id], cl
  234. ; wake up the chip
  235.         mov     edx, [io_addr]
  236.         add     edx, RTL8139_REG_HLTCLK
  237.         mov     al, 'R' ; run the clock
  238.         out     dx, al
  239. ; unlock config and BMCR registers
  240.         add     edx, RTL8139_REG_9346CR - RTL8139_REG_HLTCLK
  241.         mov     al, (1 shl RTL8139_BIT_93C46_EEM1) or (1 shl RTL8139_BIT_93C46_EEM0)
  242.         out     dx, al
  243. ; enable power management
  244.         add     edx, RTL8139_REG_CONFIG1 - RTL8139_REG_9346CR
  245.         in      al, dx
  246.         cmp     byte [hw_ver_id], IDX_RTL8139B
  247.         jl      .old_chip
  248. ; set LWAKE pin to active high (default value).
  249. ; it is for Wake-On-LAN functionality of some motherboards.
  250. ; this signal is used to inform the motherboard to execute a wake-up process.
  251. ; only at newer chips.
  252.         or      al, (1 shl RTL8139_BIT_PMEn)
  253.         and     al, not (1 shl RTL8139_BIT_LWACT)
  254.         out     dx, al
  255.         add     edx, RTL8139_REG_CONFIG4 - RTL8139_REG_CONFIG1
  256.         in      al, dx
  257.         and     al, not (1 shl RTL8139_BIT_LWPTN)
  258.         out     dx, al
  259.         jmp     .finish_wake_up
  260. .old_chip:
  261. ; wake up older chips
  262.         and     al, not ((1 shl RTL8139_BIT_SLEEP) or (1 shl RTL8139_BIT_PWRDWN))
  263.         out     dx, al
  264. .finish_wake_up:
  265. ; lock config and BMCR registers
  266.         xor     al, al
  267.         mov     edx, [io_addr]
  268.         add     edx, RTL8139_REG_9346CR
  269.         out     dx, al
  270. ;***************************************************************************
  271. ;   Function
  272. ;      rt8139_reset
  273. ;   Description
  274. ;      Place the chip (ie, the ethernet card) into a virgin state
  275. ;   Destroyed registers
  276. ;      eax, ebx, ecx, edx
  277. ;
  278. ;***************************************************************************
  279. rtl8139_reset:
  280.         mov     edx, [io_addr]
  281.         add     edx, RTL8139_REG_COMMAND
  282.         mov     al, 1 shl RTL8139_BIT_RST
  283.         out     dx, al
  284.         mov     cx, 1000 ; wait no longer for the reset
  285. .wait_for_reset:
  286.         in      al, dx
  287.         test    al, 1 shl RTL8139_BIT_RST
  288.         jz      .reset_completed ; RST remains 1 during reset
  289.         dec     cx
  290.         jns     .wait_for_reset
  291. .reset_completed:
  292. ; get MAC (hardware address)
  293.         mov     ecx, 2
  294. .mac_read_loop:
  295.         lea     eax, [EE_93C46_REG_ETH_ID+ecx]
  296.         push    ecx
  297.         call    rtl8139_read_eeprom
  298.         pop     ecx
  299.         mov     [node_addr+ecx*2], ax
  300.         dec     ecx
  301.         jns     .mac_read_loop
  302. ; unlock config and BMCR registers
  303.         mov     edx, [io_addr]
  304.         add     edx, RTL8139_REG_9346CR
  305.         mov     al, (1 shl RTL8139_BIT_93C46_EEM1) or (1 shl RTL8139_BIT_93C46_EEM0)
  306.         out     dx, al
  307. ; initialize multicast registers (no filtering)
  308.         mov     eax, 0xffffffff
  309.         add     edx, RTL8139_REG_MAR0 - RTL8139_REG_9346CR
  310.         out     dx, eax
  311.         add     edx, RTL8139_REG_MAR4 - RTL8139_REG_MAR0
  312.         out     dx, eax
  313. ; enable Rx/Tx
  314.         mov     al, (1 shl RTL8139_BIT_RE) or (1 shl RTL8139_BIT_TE)
  315.         add     edx, RTL8139_REG_COMMAND - RTL8139_REG_MAR4
  316.         out     dx, al
  317. ; 32k Rxbuffer, unlimited dma burst, no wrapping, no rx threshold
  318. ; accept broadcast packets, accept physical match packets
  319.         mov     ax, RTL8139_RX_CONFIG
  320.         add     edx, RTL8139_REG_RXCONFIG - RTL8139_REG_COMMAND
  321.         out     dx, ax
  322. ; 1024 bytes DMA burst, total retries = 16 + 8 * 16 = 144
  323.         mov     ax, (RTL8139_TX_MXDMA shl RTL8139_BIT_TX_MXDMA) \
  324.                     or (RTL8139_TXRR shl RTL8139_BIT_TXRR)
  325.         add     edx, RTL8139_REG_TXCONFIG - RTL8139_REG_RXCONFIG
  326.         out     dx, ax
  327. ; enable auto negotiation
  328.         add     edx, RTL8139_REG_BMCR - RTL8139_REG_TXCONFIG
  329.         in      ax, dx
  330.         or      ax, (1 shl RTL8139_BIT_ANE)
  331.         out     dx, ax
  332. ; set auto negotiation advertisement
  333.         add     edx, RTL8139_REG_ANAR - RTL8139_REG_BMCR
  334.         in      ax, dx
  335.         or      ax, (1 shl RTL8139_BIT_SELECTOR) or (1 shl RTL8139_BIT_10) \
  336.                     or (1 shl RTL8139_BIT_10FD) or (1 shl RTL8139_BIT_TX) \
  337.                     or (1 shl RTL8139_BIT_TXFD)
  338.         out     dx, ax
  339. ; lock config and BMCR registers
  340.         xor     eax, eax
  341.         add     edx, RTL8139_REG_9346CR - RTL8139_REG_ANAR
  342.         out     dx, al
  343. ; init RX/TX pointers
  344.         mov     [rtl8139_rx_buff_offset], eax
  345.         mov     [curr_tx_desc], eax
  346. ; clear missing packet counter
  347.         add     edx, RTL8139_REG_MPC - RTL8139_REG_9346CR
  348.         out     dx, eax
  349. ; disable all interrupts
  350.         add     edx, RTL8139_REG_IMR - RTL8139_REG_MPC
  351.         out     dx, ax
  352. ; set RxBuffer address, init RX buffer offset, init TX ring
  353.         mov     eax, rtl8139_rx_buff
  354.         add     edx, RTL8139_REG_RBSTART - RTL8139_REG_IMR
  355.         out     dx, eax
  356. ; Indicate that we have successfully reset the card
  357.         mov     eax, [pci_data]
  358.         mov     [eth_status], eax
  359.         ret
  360.  
  361. ;***************************************************************************
  362. ;   Function
  363. ;      rtl8139_read_eeprom
  364. ;   Description
  365. ;      reads eeprom type 93c46 and 93c56
  366. ;   Parameters
  367. ;      al - word to be read (6bit in case of 93c46 and 8bit otherwise)
  368. ;   Return value
  369. ;      ax - word read in
  370. ;   Destroyed register(s)
  371. ;      eax, cx, ebx, edx
  372. ;
  373. ;***************************************************************************
  374. rtl8139_read_eeprom:
  375.         movzx   ebx, al
  376.         mov     edx, [io_addr]
  377.         add     edx, RTL8139_REG_RXCONFIG
  378.         in      al, dx
  379.         test    al, (1 shl RTL8139_BIT_9356SEL)
  380.         jz      .type_93c46
  381. ;       and     bl, 01111111b ; don't care first bit
  382.         or      bx, EE_93C56_READ_CMD ; it contains start bit
  383.         mov     cx, EE_93C56_CMD_LENGTH-1 ; cmd_loop counter
  384.         jmp     .read_eeprom
  385. .type_93c46:
  386.         and     bl, 00111111b
  387.         or      bx, EE_93C46_READ_CMD ; it contains start bit
  388.         mov     cx, EE_93C46_CMD_LENGTH-1 ; cmd_loop counter
  389. .read_eeprom:
  390.         add     edx, RTL8139_REG_9346CR - RTL8139_REG_RXCONFIG_0
  391. ;       mov     al, (1 shl RTL8139_BIT_93C46_EEM1)
  392. ;       out     dx, al
  393.         mov     al, (1 shl RTL8139_BIT_93C46_EEM1) \
  394.                     or (1 shl RTL8139_BIT_93C46_EECS) ; wake up the eeprom
  395.         out     dx, al
  396. .cmd_loop:
  397.         mov     al, (1 shl RTL8139_BIT_93C46_EEM1) or (1 shl RTL8139_BIT_93C46_EECS)
  398.         bt      bx, cx
  399.         jnc     .zero_bit
  400.         or      al, (1 shl RTL8139_BIT_93C46_EEDI)
  401. .zero_bit:
  402.         out     dx, al
  403. ;       push    eax
  404. ;       in      eax, dx ; eeprom delay
  405. ;       pop     eax
  406.         or      al, (1 shl RTL8139_BIT_93C46_EESK)
  407.         out     dx, al
  408. ;       in      eax, dx ; eeprom delay
  409.         dec     cx
  410.         jns     .cmd_loop
  411. ;       in      eax, dx ; eeprom delay
  412.         mov     al, (1 shl RTL8139_BIT_93C46_EEM1) or (1 shl RTL8139_BIT_93C46_EECS)
  413.         out     dx, al
  414.         mov     cl, 0xf
  415. .read_loop:
  416.         shl     ebx, 1
  417.         mov     al, (1 shl RTL8139_BIT_93C46_EEM1) \
  418.                     or (1 shl RTL8139_BIT_93C46_EECS) \
  419.                     or (1 shl RTL8139_BIT_93C46_EESK)
  420.         out     dx, al
  421. ;       in      eax, dx ; eeprom delay
  422.         in      al, dx
  423.         and     al, (1 shl RTL8139_BIT_93C46_EEDO)
  424.         jz      .dont_set
  425.         inc     ebx
  426. .dont_set:
  427.         mov     al, (1 shl RTL8139_BIT_93C46_EEM1) \
  428.                     or (1 shl RTL8139_BIT_93C46_EECS)
  429.         out     dx, al
  430. ;       in      eax, dx ; eeprom delay
  431.         dec     cl
  432.         jns     .read_loop
  433.         xor     al, al
  434.         out     dx, al
  435.         mov     ax, bx
  436.         ret
  437.  
  438. ;***************************************************************************
  439. ;   Function
  440. ;      rtl8139_transmit
  441. ;   Description
  442. ;      Transmits a packet of data via the ethernet card
  443. ;         Pointer to 48 bit destination address in edi
  444. ;         Type of packet in bx
  445. ;         Size of packet in ecx
  446. ;         Pointer to packet data in esi
  447. ;   Destroyed registers
  448. ;      eax, edx, esi, edi
  449. ;   ToDo
  450. ;      for waiting of timeout the rtl8139 internal timer
  451. ;      should be used
  452. ;
  453. ;***************************************************************************
  454. rtl8139_transmit:
  455.         cmp     ecx, MAX_ETH_FRAME_SIZE
  456.         jg      .finish ; packet is too long
  457.         push    ecx
  458. ; check descriptor
  459.         mov     ecx, [curr_tx_desc]
  460.         mov     edx, [io_addr]
  461.         lea     edx, [edx+ecx*4+RTL8139_REG_TSD0]
  462.         push    edx ebx
  463.         in      ax, dx
  464.         test    ax, 0x1fff ; or no size given
  465.       jz      .send_packet
  466.       and       ax, (1 shl RTL8139_BIT_TOK) or (1 shl RTL8139_BIT_OWN)
  467.         cmp     ax, (1 shl RTL8139_BIT_TOK) or (1 shl RTL8139_BIT_OWN)
  468.         jz      .send_packet
  469. ; wait for timeout
  470.         mov     ebx, RTL8139_TX_TIMEOUT
  471.         mov     eax, 0x5 ; delay x/100 secs
  472.         int     0x40
  473.         in      ax, dx
  474.         and     ax, (1 shl RTL8139_BIT_TOK) or (1 shl RTL8139_BIT_OWN)
  475.         cmp     ax, (1 shl RTL8139_BIT_TOK) or (1 shl RTL8139_BIT_OWN)
  476.         jz      .send_packet
  477. ; chip hung, reset it
  478.         call    rtl8139_reset
  479. ; reset the card
  480. .send_packet:
  481. ; calculate tx_buffer address
  482.         pop     ebx
  483.         push    esi
  484.         mov     eax, MAX_ETH_FRAME_SIZE
  485.         mul     dword [curr_tx_desc]
  486.         mov     esi, edi
  487.         lea     edi, [rtl8139_tx_buff+eax]
  488.         mov     eax, edi
  489.         cld
  490. ; copy destination address
  491.         movsd
  492.         movsw
  493. ; copy source address
  494.         mov     esi, node_addr
  495.         movsd
  496.         movsw
  497. ; copy packet type
  498.         mov     [edi], bx
  499.         add     edi, 2
  500. ; copy the packet data
  501.         pop     esi edx ecx
  502.         push    ecx
  503.         shr     ecx, 2
  504.         rep     movsd
  505.         pop     ecx
  506.         push    ecx
  507.         and     ecx, 3
  508.         rep     movsb
  509. ; set address
  510.         add     edx, RTL8139_REG_TSAD0 - RTL8139_REG_TSD0
  511.         out     dx, eax
  512. ; set size and early threshold
  513.         pop     eax ; pick up the size
  514.         add     eax, ETH_HLEN
  515.         cmp     eax, ETH_ZLEN
  516.         jnc     .no_pad
  517.         mov     eax, ETH_ZLEN
  518. .no_pad:
  519.         or      eax, (RTL8139_ERTXTH shl RTL8139_BIT_ERTXTH)
  520.         add     edx, RTL8139_REG_TSD0 - RTL8139_REG_TSAD0
  521.         out     dx, eax
  522. ; get next descriptor 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, ...
  523.         inc     dword [curr_tx_desc]
  524.         and     dword [curr_tx_desc], 3
  525. .finish:
  526.         ret
  527.  
  528. ;***************************************************************************
  529. ; Function
  530. ;    rtl8139_poll
  531. ;
  532. ; Description
  533. ;    Polls the ethernet card for a received packet
  534. ;    Received data, if any, ends up in Ether_buffer
  535. ; Destroyed register(s)
  536. ;    eax, edx, ecx
  537. ;
  538. ;***************************************************************************
  539. rtl8139_poll:
  540.         mov     word [eth_rx_data_len], 0
  541.         mov     edx, [io_addr]
  542.         add     edx, RTL8139_REG_COMMAND
  543.         in      al, dx
  544.         test    al, (1 shl RTL8139_BIT_BUFE)
  545.         jnz     .finish
  546. ; new packet received copy it from rx_buffer into Ether_buffer
  547.         mov     eax, rtl8139_rx_buff
  548.         add     eax, [rtl8139_rx_buff_offset]
  549. ; check if packet is ok
  550.         test    byte [eax], (1 shl RTL8139_BIT_ROK)
  551.         jz      .reset_rx
  552. ; packet is ok copy it into the Ether_buffer
  553.         movzx   ecx, word [eax+2] ; packet length
  554.         sub     ecx, 4 ; don't copy CRC
  555.         mov     word [eth_rx_data_len], cx
  556.         push    ecx
  557.         shr     ecx, 2 ; first copy dword-wise
  558.         lea     esi, [eax+4] ; don't copy the packet header
  559.         mov     edi, Ether_buffer
  560.         cld
  561.         rep     movsd ; copy the dwords
  562.         pop     ecx
  563.         and     ecx, 3
  564.         rep     movsb ; copy the rest bytes
  565. ; update rtl8139_rx_buff_offset
  566.         movzx   eax, word [eax+2] ; packet length
  567.         add     eax, [rtl8139_rx_buff_offset]
  568.         add     eax, 4+3 ; packet header is 4 bytes long + dword alignment
  569.         and     eax, not 3 ; dword alignment
  570.         cmp     eax, RTL8139_RX_BUFFER_SIZE
  571.         jl      .no_wrap
  572.         sub     eax, RTL8139_RX_BUFFER_SIZE
  573. .no_wrap:
  574.         mov     [rtl8139_rx_buff_offset], eax
  575. ; update CAPR register
  576.         sub     eax, 0x10 ; value 0x10 is a constant for CAPR
  577.         add     edx, RTL8139_REG_CAPR - RTL8139_REG_COMMAND
  578.         out     dx, ax
  579. .finish:
  580. ; clear active interrupt sources
  581.         mov     edx, [io_addr]
  582.         add     edx, RTL8139_REG_ISR
  583.         in      ax, dx
  584.         out     dx, ax
  585.         ret
  586. .reset_rx:
  587.         in      al, dx ; read command register
  588.         push    eax
  589.         and     al, not (1 shl RTL8139_BIT_RE)
  590.         out     dx, al
  591.         pop     eax
  592.         out     dx, al
  593.         add     edx, RTL8139_REG_RXCONFIG - RTL8139_REG_COMMAND
  594.         mov     ax, RTL8139_RX_CONFIG
  595.         out     dx, ax
  596.         ret
  597.  
  598. rtl8139_cable:
  599.         pusha
  600.         mov     edx, [io_addr]
  601.         add     edx, 0x58
  602.         in      al,dx
  603.         test    al,1 SHL 2
  604.         jnz     .notconnected
  605.         popa
  606.         xor     al,al
  607.         inc     al
  608.         ret
  609.        .notconnected:
  610.         popa
  611.         xor     al,al
  612.         ret
  613.