Subversion Repositories Kolibri OS

Rev

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

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