Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2008. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;; Realtek 8139 driver for KolibriOS                               ;;
  7. ;;                                                                 ;;
  8. ;;    Written by hidnplayr@kolibrios.org                           ;;
  9. ;;                                                                 ;;
  10. ;;     0.1 - x march 2009                                          ;;
  11. ;;     0.2 - 8 november 2009                                       ;;
  12. ;;                                                                 ;;
  13. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  14. ;;             Version 2, June 1991                                ;;
  15. ;;                                                                 ;;
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17.  
  18. format MS COFF
  19.  
  20.         API_VERSION             equ 0x01000100
  21.  
  22.         DEBUG                   equ 1
  23.         __DEBUG__               equ 1
  24.         __DEBUG_LEVEL__         equ 1
  25.  
  26. include 'proc32.inc'
  27. include 'imports.inc'
  28. include 'fdo.inc'
  29.  
  30. OS_BASE         equ 0;
  31. new_app_base    equ 0x60400000
  32. PROC_BASE       equ OS_BASE+0x0080000
  33.  
  34. public START
  35. public service_proc
  36. public version
  37.  
  38. struc IOCTL {
  39.       .handle           dd ?
  40.       .io_code          dd ?
  41.       .input            dd ?
  42.       .inp_size         dd ?
  43.       .output           dd ?
  44.       .out_size         dd ?
  45. }
  46.  
  47. virtual at 0
  48.   IOCTL IOCTL
  49. end virtual
  50.  
  51. struc ETH_DEVICE {
  52. ; pointers to procedures
  53.       .unload           dd ?
  54.       .reset            dd ?
  55.       .transmit         dd ?
  56.       .set_MAC          dd ?
  57.       .get_MAC          dd ?
  58.       .set_mode         dd ?
  59.       .get_mode         dd ?
  60. ; status & variables
  61.       .bytes_tx         dq ?
  62.       .bytes_rx         dq ?
  63.       .packets_tx       dd ?
  64.       .packets_rx       dd ?
  65.       .mode             dd ?  ; This dword contains cable status (10mbit/100mbit, full/half duplex, auto negotiation or not,..)
  66.       .name             dd ?
  67.       .mac              dp ?
  68. ; device specific
  69.       .rx_buffer        dd ?
  70.       .tx_buffer        dd ?
  71.       .rx_data_offset   dd ?
  72.       .io_addr          dd ?
  73.       .curr_tx_desc     db ?
  74.       .pci_bus          db ?
  75.       .pci_dev          db ?
  76.       .irq_line         db ?
  77.       .hw_ver_id        db ?
  78.       .size:
  79.  
  80. }
  81.  
  82. virtual at 0
  83.   device ETH_DEVICE
  84. end virtual
  85.  
  86. ; PCI Bus defines
  87.  
  88.         PCI_HEADER_TYPE                 equ     0x0e  ;8 bit
  89.         PCI_BASE_ADDRESS_0              equ     0x10  ;32 bit
  90.         PCI_BASE_ADDRESS_5              equ     0x24  ;32 bits
  91.         PCI_BASE_ADDRESS_SPACE_IO       equ     0x01
  92.         PCI_VENDOR_ID                   equ     0x00  ;16 bit
  93.         PCI_BASE_ADDRESS_IO_MASK        equ     0xFFFFFFFC
  94.  
  95. ; RTL8139 specific defines
  96.  
  97.         MAX_RTL8139             equ 16   ; Max number of devices this driver may handle
  98.         TX_TIMEOUT              equ 30   ; 300 milliseconds timeout
  99.  
  100.         PCI_REG_CMD             equ 0x04 ; command register
  101.         PCI_BIT_PIO             equ 0    ; bit0: io space control
  102.         PCI_BIT_MMIO            equ 1    ; bit1: memory space control
  103.         PCI_BIT_MASTER          equ 2    ; bit2: device acts as a PCI master
  104.  
  105.         REG_IDR0                equ 0x00
  106.         REG_MAR0                equ 0x08 ; multicast filter register 0
  107.         REG_MAR4                equ 0x0c ; multicast filter register 4
  108.         REG_TSD0                equ 0x10 ; transmit status of descriptor
  109.         REG_TSAD0               equ 0x20 ; transmit start address of descriptor
  110.         REG_RBSTART             equ 0x30 ; RxBuffer start address
  111.         REG_COMMAND             equ 0x37 ; command register
  112.         REG_CAPR                equ 0x38 ; current address of packet read (word) R/W
  113.         REG_IMR                 equ 0x3c ; interrupt mask register
  114.         REG_ISR                 equ 0x3e ; interrupt status register
  115.         REG_TXCONFIG            equ 0x40 ; transmit configuration register
  116.         REG_RXCONFIG            equ 0x44 ; receive configuration register 0
  117.         REG_MPC                 equ 0x4c ; missed packet counter
  118.         REG_9346CR              equ 0x50 ; serial eeprom 93C46 command register
  119.         REG_CONFIG1             equ 0x52 ; configuration register 1
  120.         REG_MSR                 equ 0x58
  121.         REG_CONFIG4             equ 0x5a ; configuration register 4
  122.         REG_HLTCLK              equ 0x5b ; undocumented halt clock register
  123.         REG_BMCR                equ 0x62 ; basic mode control register
  124.         REG_ANAR                equ 0x66 ; auto negotiation advertisement register
  125.         REG_9346CR_WE           equ 11b SHL 6
  126.  
  127.         BIT_RUNT                equ 4 ; total packet length < 64 bytes
  128.         BIT_LONG                equ 3 ; total packet length > 4k
  129.         BIT_CRC                 equ 2 ; crc error occured
  130.         BIT_FAE                 equ 1 ; frame alignment error occured
  131.         BIT_ROK                 equ 0 ; received packet is ok
  132.  
  133.         BIT_RST                 equ 4 ; reset bit
  134.         BIT_RE                  equ 3 ; receiver enabled
  135.         BIT_TE                  equ 2 ; transmitter enabled
  136.         BUFE                    equ 1 ; rx buffer is empty, no packet stored
  137.  
  138.         BIT_ISR_TOK             equ 2 ; transmit ok
  139.         BIT_ISR_RER             equ 1 ; receive error interrupt
  140.         BIT_ISR_ROK             equ 0 ; receive ok
  141.  
  142.         BIT_TX_MXDMA            equ 8 ; Max DMA burst size per Tx DMA burst
  143.         BIT_TXRR                equ 4 ; Tx Retry count 16+(TXRR*16)
  144.  
  145.         BIT_RXFTH               equ 13 ; Rx fifo threshold
  146.         BIT_RBLEN               equ 11 ; Ring buffer length indicator
  147.         BIT_RX_MXDMA            equ 8 ; Max DMA burst size per Rx DMA burst
  148.         BIT_NOWRAP              equ 7 ; transfered data wrapping
  149.         BIT_9356SEL             equ 6 ; eeprom selector 9346/9356
  150.         BIT_AER                 equ 5 ; accept error packets
  151.         BIT_AR                  equ 4 ; accept runt packets
  152.         BIT_AB                  equ 3 ; accept broadcast packets
  153.         BIT_AM                  equ 2 ; accept multicast packets
  154.         BIT_APM                 equ 1 ; accept physical match packets
  155.         BIT_AAP                 equ 0 ; accept all packets
  156.  
  157.         BIT_93C46_EEM1          equ 7 ; RTL8139 eeprom operating mode1
  158.         BIT_93C46_EEM0          equ 6 ; RTL8139 eeprom operating mode0
  159.         BIT_93C46_EECS          equ 3 ; chip select
  160.         BIT_93C46_EESK          equ 2 ; serial data clock
  161.         BIT_93C46_EEDI          equ 1 ; serial data input
  162.         BIT_93C46_EEDO          equ 0 ; serial data output
  163.  
  164.         BIT_LWACT               equ 4 ; see REG_CONFIG1
  165.         BIT_SLEEP               equ 1 ; sleep bit at older chips
  166.         BIT_PWRDWN              equ 0 ; power down bit at older chips
  167.         BIT_PMEn                equ 0 ; power management enabled
  168.  
  169.         BIT_LWPTN               equ 2 ; see REG_CONFIG4
  170.  
  171.         BIT_ERTXTH              equ 16 ; early TX threshold
  172.         BIT_TOK                 equ 15 ; transmit ok
  173.         BIT_OWN                 equ 13 ; tx DMA operation is completed
  174.  
  175.         BIT_ANE                 equ 12 ; auto negotiation enable
  176.  
  177.         BIT_TXFD                equ 8 ; 100base-T full duplex
  178.         BIT_TX                  equ 7 ; 100base-T
  179.         BIT_10FD                equ 6 ; 10base-T full duplex
  180.         BIT_10                  equ 5 ; 10base-T
  181.         BIT_SELECTOR            equ 0 ; binary encoded selector CSMA/CD=00001
  182.  
  183.         BIT_IFG1                equ 25
  184.         BIT_IFG0                equ 24
  185.  
  186.         RBLEN                   equ 2 ; Receive buffer size: 0==8K 1==16k 2==32k 3==64k
  187.         TXRR                    equ 8 ; total retries = 16+(TXRR*16)
  188.         TX_MXDMA                equ 6 ; 0=16 1=32 2=64 3=128 4=256 5=512 6=1024 7=2048
  189.         ERTXTH                  equ 8 ; in unit of 32 bytes e.g:(8*32)=256
  190.         RX_MXDMA                equ 7 ; 0=16 1=32 2=64 3=128 4=256 5=512 6=1024 7=unlimited
  191.         RXFTH                   equ 7 ; 0=16 1=32 2=64 3=128 4=256 5=512 6=1024 7=no threshold
  192.  
  193.         RX_CONFIG               equ (RBLEN shl BIT_RBLEN) or \
  194.                                     (RX_MXDMA shl BIT_RX_MXDMA) or \
  195.                                     (1 shl BIT_NOWRAP) or \
  196.                                     (RXFTH shl BIT_RXFTH) or\
  197.                                     (1 shl BIT_AB) or \
  198.                                     (1 shl BIT_APM) or \
  199.                                     (1 shl BIT_AER) or \
  200.                                     (1 shl BIT_AR) or \
  201.                                     (1 shl BIT_AM)
  202.  
  203.         RX_BUFFER_SIZE          equ (8192 shl RBLEN)
  204.         MAX_ETH_FRAME_SIZE      equ 1516 ; exactly 1514 wthout CRC
  205.         NUM_TX_DESC             equ 4
  206.         TX_BUF_SIZE             equ 4096 ; size of one tx buffer (set to 4kb because of KolibriOS's page size)
  207.  
  208.         EE_93C46_REG_ETH_ID     equ 7 ; MAC offset
  209.         EE_93C46_READ_CMD       equ (6 shl 6) ; 110b + 6bit address
  210.         EE_93C56_READ_CMD       equ (6 shl 8) ; 110b + 8bit address
  211.         EE_93C46_CMD_LENGTH     equ 9  ; start bit + cmd + 6bit address
  212.         EE_93C56_CMD_LENGTH     equ 11 ; start bit + cmd + 8bit ddress
  213.  
  214.         VER_RTL8139             equ 1100000b
  215.         VER_RTL8139A            equ 1110000b
  216.         VER_RTL8139AG           equ 1110100b
  217.         VER_RTL8139B            equ 1111000b
  218.         VER_RTL8130             equ VER_RTL8139B
  219.         VER_RTL8139C            equ 1110100b
  220.         VER_RTL8100             equ 1111010b
  221.         VER_RTL8100B            equ 1110101b
  222.         VER_RTL8139D            equ VER_RTL8100B
  223.         VER_RTL8139CP           equ 1110110b
  224.         VER_RTL8101             equ 1110111b
  225.  
  226.         IDX_RTL8139             equ 0
  227.         IDX_RTL8139A            equ 1
  228.         IDX_RTL8139B            equ 2
  229.         IDX_RTL8139C            equ 3
  230.         IDX_RTL8100             equ 4
  231.         IDX_RTL8139D            equ 5
  232.         IDX_RTL8139D            equ 6
  233.         IDX_RTL8101             equ 7
  234.  
  235.         ISR_SERR                equ 1 SHL 15
  236.         ISR_TIMEOUT             equ 1 SHL 14
  237.         ISR_LENCHG              equ 1 SHL 13
  238.         ISR_FIFOOVW             equ 1 SHL 6
  239.         ISR_PUN                 equ 1 SHL 5
  240.         ISR_RXOVW               equ 1 SHL 4
  241.         ISR_TER                 equ 1 SHL 3
  242.         ISR_TOK                 equ 1 SHL 2
  243.         ISR_RER                 equ 1 SHL 1
  244.         ISR_ROK                 equ 1 SHL 0
  245.  
  246.         INTERRUPT_MASK          equ ISR_ROK or \
  247.                                     ISR_RXOVW or \
  248.                                     ISR_PUN or \
  249.                                     ISR_FIFOOVW or \
  250.                                     ISR_LENCHG or \
  251.                                     ISR_TOK or \
  252.                                     ISR_TER
  253.  
  254.         TSR_OWN                 equ 1 SHL 13
  255.         TSR_TUN                 equ 1 SHL 14
  256.         TSR_TOK                 equ 1 SHL 15
  257.  
  258.         TSR_CDH                 equ 1 SHL 28
  259.         TSR_OWC                 equ 1 SHL 29
  260.         TSR_TABT                equ 1 SHL 30
  261.         TSR_CRS                 equ 1 SHL 31
  262.  
  263.  
  264.  
  265. section '.flat' code readable align 16
  266.  
  267. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  268. ;;                        ;;
  269. ;; proc START             ;;
  270. ;;                        ;;
  271. ;; (standard driver proc) ;;
  272. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  273.  
  274. align 4
  275. proc START stdcall, state:dword
  276.  
  277.         cmp [state], 1
  278.         jne .exit
  279.  
  280.   .entry:
  281.  
  282.         DEBUGF 1,"Loading rtl8139 driver\n"
  283.         stdcall RegService, my_service, service_proc
  284.         ret
  285.  
  286.   .fail:
  287.   .exit:
  288.         xor eax, eax
  289.         ret
  290.  
  291. endp
  292.  
  293.  
  294. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  295. ;;                        ;;
  296. ;; proc SERVICE_PROC      ;;
  297. ;;                        ;;
  298. ;; (standard driver proc) ;;
  299. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  300.  
  301. align 4
  302. proc service_proc stdcall, ioctl:dword
  303.  
  304.         mov     edx, [ioctl]
  305.         mov     eax, [edx+IOCTL.io_code]
  306.  
  307. ;------------------------------------------------------
  308.  
  309.         cmp     eax, 0 ;SRV_GETVERSION
  310.         jne     @F
  311.  
  312.         cmp     [edx+IOCTL.out_size], 4
  313.         jl      .fail
  314.         mov     eax, [edx+IOCTL.output]
  315.         mov     [eax], dword API_VERSION
  316.  
  317.         xor     eax, eax
  318.         ret
  319.  
  320. ;------------------------------------------------------
  321.   @@:
  322.         cmp     eax, 1 ;SRV_HOOK
  323.         jne     .fail
  324.  
  325.         cmp     [edx + IOCTL.inp_size], 3               ; Data input must be at least 3 bytes
  326.         jl      .fail
  327.  
  328.         mov     eax, [edx + IOCTL.input]
  329.         cmp     byte [eax], 1                           ; 1 means device number and bus number (pci) are given
  330.         jne     .fail                                   ; other types arent supported for this card yet
  331.  
  332. ; check if the device is already listed
  333.  
  334.         mov     esi, RTL8139_LIST
  335.         mov     ecx, [RTL8139_DEV]
  336.         test    ecx, ecx
  337.         jz      .firstdevice
  338. ;        mov     eax, [edx+IOCTL.input]                  ; get the pci bus and device numbers
  339.         mov     bx , [eax+1]                            ;
  340.   .nextdevice:
  341.         lodsd
  342.         cmp     bx , word [eax + device.pci_bus]        ; compare with pci and device num in RTL8139 list (notice the usage of word instead of byte)
  343.         je      .find_devicenum                         ; Device is already loaded, let's find it's device number
  344.  
  345.         loop    .nextdevice
  346.  
  347. ; This device doesnt have its own eth_device structure yet, lets create one
  348.   .firstdevice:
  349.         cmp     [RTL8139_DEV], MAX_RTL8139              ; First check if the driver can handle one more card
  350.         jge     .fail
  351.  
  352.         push    edx
  353.         stdcall KernelAlloc, dword device.size          ; Allocate the buffer for eth_device structure
  354.         pop     edx
  355.         test    eax, eax
  356.         jz      .fail
  357.         mov     ebx, eax                                ; ebx is always used as a pointer to the structure (in driver, but also in kernel code)
  358.  
  359. ; Fill in the direct call addresses into the struct
  360.  
  361.         mov     dword [ebx+device.reset], reset
  362.         mov     dword [ebx+device.transmit], transmit
  363.         mov     dword [ebx+device.get_MAC], read_mac
  364.         mov     dword [ebx+device.set_MAC], write_mac
  365.         mov     dword [ebx+device.unload], unload
  366.         mov     dword [ebx+device.name], my_service
  367.  
  368. ; save the pci bus and device numbers
  369.  
  370.         mov     eax, [edx+IOCTL.input]
  371.         mov     cl , [eax+1]
  372.         mov     [ebx+device.pci_bus], cl
  373.         mov     cl , [eax+2]
  374.         mov     [ebx+device.pci_dev], cl
  375.  
  376. ; Now, it's time to find the base io addres of the PCI device
  377. ; TODO: implement check if bus and dev exist on this machine
  378.  
  379.         mov     edx, PCI_BASE_ADDRESS_0
  380.   .reg_check:
  381.         movzx   eax, byte [ebx+device.pci_bus]
  382.         movzx   ecx, byte [ebx+device.pci_dev]
  383.  
  384.         push    edx ecx
  385.         stdcall PciRead16, eax ,ecx ,edx
  386.         pop     ecx edx
  387.  
  388.         mov     [ebx+device.io_addr], eax
  389.         and     eax, PCI_BASE_ADDRESS_IO_MASK
  390.         test    eax, eax
  391.         jz      .inc_reg
  392.         mov     eax, [ebx+device.io_addr]
  393.         and     eax, PCI_BASE_ADDRESS_SPACE_IO
  394.         test    eax, eax
  395.         jz      .inc_reg
  396.  
  397.         mov     eax, [ebx+device.io_addr]
  398.         and     eax, PCI_BASE_ADDRESS_IO_MASK
  399.         mov     [ebx+device.io_addr], eax
  400.         jmp     .got_io
  401.  
  402.   .inc_reg:
  403.         add     edx, 4
  404.         cmp     edx, PCI_BASE_ADDRESS_5
  405.         jbe     .reg_check
  406.  
  407.   .got_io:
  408.  
  409. ; We've found the io address, find IRQ now
  410.  
  411.         movzx   eax, byte [ebx+device.pci_bus]
  412.         movzx   ecx, byte [ebx+device.pci_dev]
  413.         push    ebx
  414.         stdcall PciRead8, eax ,ecx ,0x3c                                ; 0x3c is the offset where irq can be found
  415.         pop     ebx
  416.         mov     byte [ebx+device.irq_line], al
  417.  
  418.         DEBUGF  1,"Hooking into device, dev:%x, bus:%x, irq:%x, addr:%x\n",\
  419.         [ebx+device.pci_dev]:1,[ebx+device.pci_bus]:1,[ebx+device.irq_line]:1,[ebx+device.io_addr]:4
  420.  
  421. ; Allocate the Receive buffer
  422.  
  423.         stdcall KernelAlloc, dword (RX_BUFFER_SIZE+MAX_ETH_FRAME_SIZE)
  424.         test    eax, eax
  425.         jz      .err
  426.         mov     [ebx+device.rx_buffer], eax                             ; Save the address to it into the device struct
  427.  
  428. ; Now, Clear the allocated buffer
  429.  
  430.         cld
  431.         mov     edi, eax
  432.         mov     ecx, (RX_BUFFER_SIZE)/4                                 ; divide by 4 because we are going to use DWORD
  433.         xor     eax, eax
  434.         rep     stosd
  435.  
  436. ; Allocate the Transmit Buffer
  437.  
  438.         stdcall KernelAlloc, dword (TX_BUF_SIZE*NUM_TX_DESC)
  439.         test    eax, eax
  440.         jz      .err
  441.         mov     [ebx+device.tx_buffer], eax
  442.  
  443. ; This one needs to be cleared too..
  444.  
  445.         mov     edi, eax
  446.         mov     ecx, (TX_BUF_SIZE*NUM_TX_DESC)/4
  447.         xor     eax, eax
  448.         rep     stosd
  449.  
  450. ; Ok, the eth_device structure is ready, let's probe the device
  451.  
  452.         call    probe                                                   ; this function will output in eax
  453.         test    eax, eax
  454.         jnz     .err                                                    ; If an error occured, exit
  455.  
  456.         mov     eax, [RTL8139_DEV]                                      ; Add the device structure to our device list
  457.         mov     [RTL8139_LIST+4*eax], ebx                               ; (IRQ handler uses this list to find device)
  458.         inc     [RTL8139_DEV]                                           ;
  459.  
  460.  
  461.         call    EthRegDev
  462.         cmp     eax, -1
  463.         je      .destroy
  464.  
  465.         ret
  466.  
  467. ; If the device was already loaded, find the device number and return it in eax
  468.  
  469.   .find_devicenum:
  470.         DEBUGF  1,"Trying to find device number of already registered device\n"
  471.         mov     ebx, eax
  472.         call    EthStruc2Dev                                            ; This kernel procedure converts a pointer to device struct in ebx
  473.                                                                         ; into a device number in edi
  474.         mov     eax, edi                                                ; Application wants it in eax instead
  475.         DEBUGF  1,"Kernel says: %u\n", eax
  476.         ret
  477.  
  478. ; If an error occured, remove all allocated data and exit (returning -1 in eax)
  479.  
  480.   .destroy:
  481.         ; todo: reset device into virgin state
  482.  
  483.   .err:
  484.         stdcall KernelFree, dword [ebx+device.rx_buffer]
  485.         stdcall KernelFree, dword [ebx+device.tx_buffer]
  486.         stdcall KernelFree, ebx
  487.  
  488.  
  489.   .fail:
  490.         or      eax, -1
  491.         ret
  492.  
  493. ;------------------------------------------------------
  494. endp
  495.  
  496.  
  497. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  498. ;;                                                                        ;;
  499. ;;        Actual Hardware dependent code starts here                      ;;
  500. ;;                                                                        ;;
  501. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  502.  
  503. align 4
  504. unload:
  505.         ; TODO: (in this particular order)
  506.         ;
  507.         ; - Stop the device
  508.         ; - Detach int handler
  509.         ; - Remove device from local list (RTL8139_LIST)
  510.         ; - call unregister function in kernel
  511.         ; - Remove all allocated structures and buffers the card used
  512.  
  513.         or      eax,-1
  514.  
  515. ret
  516.  
  517.  
  518. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  519. ;;
  520. ;;  probe: enables the device (if it really is RTL8139)
  521. ;;
  522. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  523.  
  524. align 4
  525. probe:
  526.         DEBUGF  2,"Probing rtl8139 device: "
  527.  
  528. ; enable the device
  529.  
  530.         movzx   eax, byte [ebx+device.pci_bus]
  531.         movzx   ecx, byte [ebx+device.pci_dev]
  532.         stdcall PciRead32, eax ,ecx ,PCI_REG_CMD
  533.  
  534.         mov     cx , ax
  535.         or      cl , (1 shl PCI_BIT_MASTER) or (1 shl PCI_BIT_PIO)
  536.         and     cl , not (1 shl PCI_BIT_MMIO)
  537.         movzx   eax, byte [ebx+device.pci_bus]
  538.         movzx   edx, byte [ebx+device.pci_dev]
  539.         stdcall PciWrite32, eax ,edx ,PCI_REG_CMD, ecx
  540.  
  541. ; get chip version
  542.  
  543.         mov     edx, [ebx+device.io_addr]
  544.         add     edx, REG_TXCONFIG + 2
  545.         in      ax , dx
  546.         shr     ah , 2
  547.         shr     ax , 6
  548.         and     al , 01111111b
  549.         mov     ecx, HW_VER_ARRAY_SIZE-1
  550.   .chip_ver_loop:
  551.         cmp     al , [hw_ver_array+ecx]
  552.         je      .chip_ver_found
  553.         dec     ecx
  554.         jns     .chip_ver_loop
  555.         xor     cl , cl ; default RTL8139
  556.   .chip_ver_found:
  557.         mov     [ebx+device.hw_ver_id], cl
  558.  
  559.         shl     ecx, 2
  560.         add     ecx, name_crosslist
  561.         mov     ecx, [ecx]
  562.         mov     dword [ebx+device.name], ecx
  563.  
  564.         DEBUGF  1,"Chip version: %s\n",ecx
  565.  
  566. ; wake up the chip
  567.  
  568.         mov     edx, [ebx+device.io_addr]
  569.         add     edx, REG_HLTCLK
  570.         mov     al , 'R' ; run the clock
  571.         out     dx , al
  572.  
  573. ; unlock config and BMCR registers
  574.  
  575.         add     edx, REG_9346CR - REG_HLTCLK
  576.         mov     al , (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
  577.         out     dx , al
  578.  
  579. ; enable power management
  580.  
  581.         add     edx, REG_CONFIG1 - REG_9346CR
  582.         in      al , dx
  583.         cmp     byte [ebx+device.hw_ver_id], IDX_RTL8139B
  584.         jl      .old_chip
  585.  
  586. ; set LWAKE pin to active high (default value).
  587. ; it is for Wake-On-LAN functionality of some motherboards.
  588. ; this signal is used to inform the motherboard to execute a wake-up process.
  589. ; only at newer chips.
  590.  
  591.         or      al , (1 shl BIT_PMEn)
  592.         and     al , not (1 shl BIT_LWACT)
  593.         out     dx , al
  594.         add     edx, REG_CONFIG4 - REG_CONFIG1
  595.         in      al , dx
  596.         and     al , not (1 shl BIT_LWPTN)
  597.         out     dx , al
  598.         jmp     .finish_wake_up
  599.   .old_chip:
  600.  
  601. ; wake up older chips
  602.  
  603.         and     al , not ((1 shl BIT_SLEEP) or (1 shl BIT_PWRDWN))
  604.         out     dx , al
  605.   .finish_wake_up:
  606.  
  607. ; lock config and BMCR registers
  608.  
  609.         xor     al , al
  610.         mov     edx, [ebx+device.io_addr]
  611.         add     edx, REG_9346CR
  612.         out     dx , al
  613.         DEBUGF  2,"done!\n"
  614.  
  615.  
  616. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  617. ;;
  618. ;;   reset: Set up all registers and descriptors, clear some values
  619. ;;
  620. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  621.  
  622. reset:
  623.         DEBUGF  2,"Resetting rtl8139: "
  624.  
  625. ; attach int handler
  626.  
  627.         movzx   eax, [ebx+device.irq_line]
  628.         DEBUGF  1,"Attaching int handler to irq %x, ",eax:1
  629.         stdcall AttachIntHandler, eax, int_handler, dword 0
  630.         test    eax, eax
  631.         jnz     @f
  632.         DEBUGF  1,"\nCould not attach int handler!\n"
  633. ;        or      eax, -1
  634. ;        ret
  635.   @@:
  636.  
  637. ; reset chip
  638.  
  639.         DEBUGF  1,"Resetting chip\n"
  640.         mov     edx, [ebx+device.io_addr]
  641.         add     edx, REG_COMMAND
  642.         mov     al , 1 shl BIT_RST
  643.         out     dx , al
  644.         mov     cx , 1000               ; wait no longer for the reset
  645.   .wait_for_reset:
  646.         in      al , dx
  647.         test    al , 1 shl BIT_RST
  648.         jz      .reset_completed        ; RST remains 1 during reset
  649.         dec     cx
  650.         jns     .wait_for_reset
  651.   .reset_completed:
  652.  
  653. ; unlock config and BMCR registers
  654.  
  655.         mov     edx, [ebx+device.io_addr]
  656.         add     edx, REG_9346CR
  657.         mov     al , (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
  658.         out     dx , al
  659.  
  660. ; initialize multicast registers (no filtering)
  661.  
  662.         mov     eax, 0xffffffff
  663.         add     edx, REG_MAR0 - REG_9346CR
  664.         out     dx , eax
  665.         add     edx, REG_MAR4 - REG_MAR0
  666.         out     dx , eax
  667.  
  668. ; enable Rx/Tx
  669.  
  670.         mov     al , (1 shl BIT_RE) or (1 shl BIT_TE)
  671.         add     edx, REG_COMMAND - REG_MAR4
  672.         out     dx , al
  673.  
  674. ; 32k Rxbuffer, unlimited dma burst, no wrapping, no rx threshold
  675. ; accept broadcast packets, accept physical match packets
  676.  
  677.         mov     ax , RX_CONFIG
  678.         add     edx, REG_RXCONFIG - REG_COMMAND
  679.         out     dx , ax
  680.  
  681. ; 1024 bytes DMA burst, total retries = 16 + 8 * 16 = 144
  682.  
  683.         mov     eax , (TX_MXDMA shl BIT_TX_MXDMA) or (TXRR shl BIT_TXRR) or BIT_IFG1 or BIT_IFG0
  684.         add     edx, REG_TXCONFIG - REG_RXCONFIG
  685.         out     dx , eax
  686.  
  687. ; enable auto negotiation
  688.  
  689.         add     edx, REG_BMCR - REG_TXCONFIG
  690.         in      ax , dx
  691.         or      ax , (1 shl BIT_ANE)
  692.         out     dx , ax
  693.  
  694. ; set auto negotiation advertisement
  695.  
  696.         add     edx, REG_ANAR - REG_BMCR
  697.         in      ax , dx
  698.         or      ax , (1 shl BIT_SELECTOR) or (1 shl BIT_10) or (1 shl BIT_10FD) or (1 shl BIT_TX) or (1 shl BIT_TXFD)
  699.         out     dx , ax
  700.  
  701. ; lock config and BMCR registers
  702.  
  703.         xor     eax, eax
  704.         add     edx, REG_9346CR - REG_ANAR
  705.         out     dx , al
  706.  
  707. ; init RX/TX pointers
  708.  
  709.         mov     [ebx+device.rx_data_offset], eax
  710.         mov     [ebx+device.curr_tx_desc], al
  711.  
  712. ; clear packet/byte counters
  713.  
  714.         lea     edi, [ebx+device.bytes_tx] ; TODO: check if destroying edi, ecx doesnt harm anything
  715.         mov     ecx, 6
  716.         rep     stosd
  717.  
  718. ; clear missing packet counter
  719.  
  720.         add     edx, REG_MPC - REG_9346CR
  721.         out     dx , eax
  722.  
  723. ; Set up the 4 Txbuffer descriptors
  724.  
  725.         add     edx, REG_TSAD0 - REG_MPC
  726.         mov     eax, [ebx+device.tx_buffer]
  727.         mov     ecx, 4
  728.   .loop:
  729.         push    eax
  730.         call    GetPgAddr
  731.         DEBUGF  2,"Desc: %x ", eax
  732.         out     dx , eax
  733.         add     dx , 4
  734.         pop     eax
  735.         add     eax, TX_BUF_SIZE
  736.         loop    .loop
  737.  
  738. ; set RxBuffer address, init RX buffer offset, init TX ring
  739.  
  740.         mov     eax, [ebx+device.rx_buffer]
  741.         call    GetPgAddr
  742.         mov     edx, [ebx+device.io_addr]
  743.         add     edx, REG_RBSTART
  744.         out     dx , eax
  745.  
  746. ; enable interrupts
  747.  
  748.         mov     eax, INTERRUPT_MASK
  749.         add     edx, REG_IMR - REG_RBSTART
  750.         out     dx , ax
  751.  
  752. ; Read MAC address
  753.  
  754.         call    read_mac
  755.  
  756. ; Indicate that we have successfully reset the card
  757.  
  758.         DEBUGF  2,"Done!\n"
  759.         xor     eax, eax
  760.  
  761.         ret
  762.  
  763.  
  764. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  765. ;;                                         ;;
  766. ;; Transmit                                ;;
  767. ;;                                         ;;
  768. ;; In: buffer pointer in [esp+4]           ;;
  769. ;;     size of buffer in [esp+8]           ;;
  770. ;;     pointer to device structure in ebx  ;;
  771. ;;                                         ;;
  772. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  773.  
  774. align 4
  775. transmit:
  776.         DEBUGF  1,"Transmitting packet, buffer:%x, size:%u\n",[esp+4],[esp+8]
  777.         mov     eax, [esp+4]
  778.         DEBUGF  1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
  779.         [eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
  780.         [eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
  781.         [eax+13]:2,[eax+12]:2
  782.  
  783.         cmp     dword [esp+8], MAX_ETH_FRAME_SIZE
  784.         jg      .finish                         ; packet is too long
  785.         cmp     dword [esp+8], 60
  786.         jl      .finish                         ; packet is too short
  787.  
  788. ; check descriptor
  789. ;        DEBUGF  1,"Checking descriptor, "
  790.         movzx   ecx, [ebx+device.curr_tx_desc]
  791.         mov     edx, [ebx+device.io_addr]
  792.         lea     edx, [edx+ecx*4+REG_TSD0]
  793.         in      ax, dx
  794.         test    ax, 0x1fff ; or no size given
  795.         jz      .send_packet
  796.         and     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  797.         cmp     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  798.         jz      .send_packet
  799. ; wait for timeout
  800. ;        DEBUGF  1,"Waiting for timeout, "
  801.  
  802.         push    edx ebx                          ; TODO : rtl8139 internal timer should be used instead
  803.         stdcall Sleep, TX_TIMEOUT                ; ? What registers does this destroy ?
  804.         pop     ebx edx
  805.  
  806.         in      ax, dx
  807.         and     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  808.         cmp     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  809.         jz      .send_packet                     ; if chip hung, reset it
  810.         push    dx
  811.         call    reset                            ; reset the card
  812.         pop     dx
  813. .send_packet:
  814. ;        DEBUGF  1,"Sending packet, "
  815.  
  816.         push    edx
  817.         movzx   eax, [ebx+device.curr_tx_desc]   ; calculate the current tx_buffer address
  818.         mov     edx, TX_BUF_SIZE ;MAX_ETH_FRAME_SIZE          ;
  819.         mul     edx                              ;
  820.         mov     edi, [ebx+device.tx_buffer]      ;
  821.         add     edi, eax                         ; Store it in edi
  822.         pop     edx
  823.  
  824.         mov     esi, [esp+4]                     ; Copy data to that address
  825.         mov     ecx, [esp+8]                     ;
  826.         shr     ecx, 2                           ;
  827.         rep     movsd                            ;
  828.         mov     ecx, [esp+8]                     ;
  829.         and     ecx, 3                           ;
  830.         rep     movsb                            ;
  831.  
  832.         inc     [ebx+device.packets_tx]          ;
  833.         mov     eax, [esp+8]                     ; Get packet size in eax
  834.  
  835.         add     dword [ebx + device.bytes_tx], eax
  836.         adc     dword [ebx + device.bytes_tx + 4], 0
  837.  
  838. ;        or      eax, (ERTXTH shl BIT_ERTXTH)     ; Set descriptor size and the early tx treshold into the correct Transmission status register (TSD0, TSD1, TSD2 or TSD3)
  839.         out     dx , eax                         ;
  840.  
  841. ; get next descriptor 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, ...
  842.         inc     [ebx+device.curr_tx_desc]
  843.         and     [ebx+device.curr_tx_desc], 3
  844.  
  845.         DEBUGF  2," - Packet Sent! "
  846. .finish:
  847.         DEBUGF  2," - Done!\n"
  848.         ret
  849.  
  850.  
  851.  
  852.  
  853.  
  854. ;;;;;;;;;;;;;;;;;;;;;;;
  855. ;;                   ;;
  856. ;; Interrupt handler ;;
  857. ;;                   ;;
  858. ;;;;;;;;;;;;;;;;;;;;;;;
  859.  
  860. align 4
  861. int_handler:
  862.  
  863.         DEBUGF  1,"IRQ %x ",eax:2                   ; no, you cant replace 'eax:2' with 'al', this must be a bug in FDO
  864.  
  865. ; find pointer of device wich made IRQ occur
  866.  
  867.         mov     esi, RTL8139_LIST
  868.         mov     ecx, [RTL8139_DEV]
  869. .nextdevice:
  870.         mov     ebx, dword [esi]
  871.  
  872.         mov     edx, dword [ebx+device.io_addr]     ; get IRQ reason
  873.         add     edx, REG_ISR
  874.         in      ax , dx
  875.         out     dx , ax                             ; send it back to ACK
  876.  
  877.         add     esi, 4
  878.  
  879.         test    ax , ax
  880.         jnz     .got_it
  881.  
  882.         loop    .nextdevice
  883.  
  884.         ret                                         ; If no device was found, abort (The irq was probably for a device, not registered to this driver)
  885.  
  886.   .got_it:
  887.  
  888. ; looks like we've found it!
  889.  
  890. ; Lets found out why the irq occured then..
  891.  
  892. ;----------------------------------------------------
  893. ; Received packet ok?
  894.  
  895.         test    ax, ISR_ROK
  896.         jz      @f
  897.         push    ax
  898.  
  899.   .receive:
  900.         mov     edx, dword [ebx+device.io_addr]     ; get IRQ reason
  901.         add     edx, REG_COMMAND                    ;
  902.         in      al , dx                             ;
  903.         test    al , BUFE                           ; test if RX buffer is empty
  904.         jnz     .finish                             ;
  905.  
  906.         DEBUGF  2,"RX: "
  907.  
  908.         mov     eax, dword [ebx+device.rx_buffer]
  909.         add     eax, dword [ebx+device.rx_data_offset]
  910.         test    byte [eax], (1 shl BIT_ROK)         ; check if packet is ok
  911.         jz      .reset_rx
  912.                                                     ; packet is ok, copy it
  913.         movzx   ecx, word [eax+2]                   ; packet length
  914.         add     dword [ebx + device.bytes_rx], ecx  ; Update stats
  915.         adc     dword [ebx + device.bytes_rx + 4], 0
  916.         inc     dword [ebx + device.packets_rx]     ;
  917.         sub     ecx, 4                              ; don't copy CRC
  918.         DEBUGF  1,"Received %u bytes\n", ecx
  919.  
  920.         push    ebx eax ecx
  921.         stdcall KernelAlloc, ecx                    ; Allocate a buffer to put packet into
  922.         pop     ecx
  923.         test    eax, eax                            ; Test if we allocated succesfully
  924.         jz      .abort                              ;
  925.  
  926.         mov     edi, eax                            ; Set up registers to copy the packet
  927.         mov     esi, [esp]                          ;
  928.         add     esi, 4                              ; Dont copy CRC
  929.  
  930.         push    dword .abort                        ; Kernel will return to this address after EthReceiver
  931.         push    ecx edi                             ; Save buffer pointer and size, to pass to kernel
  932.  
  933.         shr     ecx, 2
  934.         cld
  935.         rep     movsd                               ; copy the dwords
  936.         mov     ecx, [esp+4]
  937.         and     ecx, 3
  938.         rep     movsb                               ; copy the rest bytes
  939.  
  940.         jmp     EthReceiver                         ; Send it to kernel
  941.  
  942.   .abort:
  943.         pop     eax ebx
  944.                                                     ; update eth_data_start_offset
  945.         movzx   eax, word [eax+2]                   ; packet length
  946.         add     eax, [ebx+device.rx_data_offset]
  947.         add     eax, 4+3                            ; packet header is 4 bytes long + dword alignment
  948.         and     eax, not 3                          ; dword alignment
  949.         cmp     eax, RX_BUFFER_SIZE
  950.         jl      .no_wrap
  951.         sub     eax, RX_BUFFER_SIZE
  952.   .no_wrap:
  953.         mov     [ebx+device.rx_data_offset], eax
  954.         DEBUGF  1,"New RX ptr: %u", eax
  955.  
  956.         mov     edx, dword [ebx+device.io_addr]
  957.         add     edx, REG_CAPR                       ; update 'Current Address of Packet Read register'
  958.         sub     eax, 0x10                           ; value 0x10 is a constant for CAPR
  959.         out     dx , ax
  960.  
  961.         jmp     .receive                            ; check for multiple packets
  962.  
  963.   .reset_rx:
  964.         test    byte [eax], (1 shl BIT_CRC)
  965.         jz      .no_crc_error
  966.         DEBUGF  2,"\nCRC error!\n"
  967.  
  968.   .no_crc_error:
  969.         test    byte [eax], (1 shl BIT_FAE)
  970.         jz      .no_fae_error
  971.         DEBUGF  1,"\nFrame alignment error!\n"
  972.  
  973.   .no_fae_error:
  974.         DEBUGF  1,"Reset RX\n"
  975.         in      al , dx                             ; read command register
  976.         push    ax
  977.  
  978.         and     al , not (1 shl BIT_RE)             ; Clear the RE bit
  979.         out     dx , al
  980.  
  981.         pop     ax
  982.         out     dx , al                             ; write original command back
  983.  
  984.         add     edx, REG_RXCONFIG - REG_COMMAND     ; Restore RX configuration
  985.         mov     ax , RX_CONFIG
  986.         out     dx , ax
  987.  
  988.   .finish:
  989.         pop     ax
  990.  
  991. ;----------------------------------------------------
  992. ; Transmit error ?
  993.  
  994.   @@:
  995.         test    ax, ISR_TER
  996.         jz      @f
  997.  
  998.         push    ax
  999.         cmp     [ebx+device.curr_tx_desc], 4
  1000.         jz      .notxd
  1001.  
  1002.         mov     edx, [ebx+device.io_addr]
  1003.         movzx   ecx, [ebx+device.curr_tx_desc]
  1004.         lea     edx, [edx+ecx*4+REG_TSD0]
  1005.         in      eax, dx
  1006.  
  1007.   .notxd:
  1008.         test    eax, TSR_TUN
  1009.         jz      .nobun
  1010.         DEBUGF  1, "TX: FIFO Buffer underrun!\n"
  1011.  
  1012.   .nobun:
  1013.         test    eax, TSR_OWC
  1014.         jz      .noowc
  1015.         DEBUGF  1, "TX: OWC!\n"
  1016.  
  1017.   .noowc:
  1018.         test    eax, TSR_TABT
  1019.         jz      .notabt
  1020.         DEBUGF  1, "TX: TABT!\n"
  1021.  
  1022.   .notabt:
  1023.         test    eax, TSR_CRS
  1024.         jz      .nocsl
  1025.         DEBUGF  1, "TX: Carrier Sense Lost!\n"
  1026.  
  1027.   .nocsl:
  1028. ;                test    eax, TSR_OWN or TSR_TOK
  1029. ;                jz      .nofd
  1030. ;                DEBUGF  1, "TX: Transmit OK (desc: %u)\n", ecx
  1031. ;
  1032. ;               .nofd:
  1033.         pop     ax
  1034.  
  1035. ;----------------------------------------------------
  1036. ; Transmit ok ?
  1037.  
  1038.   @@:
  1039.         test    ax, ISR_TOK
  1040.         jz      @f
  1041.  
  1042.         DEBUGF  1, "TX: Transmit OK (desc: %u)\n", [ebx+device.curr_tx_desc]:1
  1043.  
  1044. ;----------------------------------------------------
  1045. ; Rx buffer overflow ?
  1046.  
  1047.   @@:
  1048.         test    ax, ISR_RXOVW
  1049.         jz      @f
  1050.  
  1051.         push    ax
  1052.         DEBUGF  1,"RX-buffer overflow!\n"
  1053.  
  1054.         mov     edx, [ebx+device.io_addr]
  1055.         add     edx, REG_ISR
  1056.         mov     ax , ISR_FIFOOVW or ISR_RXOVW
  1057.         out     dx , ax
  1058.         pop     ax
  1059.  
  1060. ;----------------------------------------------------
  1061. ; Packet underrun? ?
  1062.  
  1063.  
  1064.   @@:
  1065.         test    ax, ISR_PUN
  1066.         jz      @f
  1067.  
  1068.         DEBUGF  1,"Packet underrun!\n"
  1069.  
  1070. ;----------------------------------------------------
  1071. ; Receive FIFO overflow ?
  1072.  
  1073.   @@:
  1074.         test    ax, ISR_FIFOOVW
  1075.         jz      @f
  1076.  
  1077.         push    ax
  1078.         DEBUGF  2,"RX fifo overflox!\n"
  1079.  
  1080.         mov     edx, [ebx+device.io_addr]
  1081.         add     edx, REG_ISR
  1082.         mov     ax , ISR_FIFOOVW or ISR_RXOVW
  1083.         out     dx , ax
  1084.         pop     ax
  1085.  
  1086. ;----------------------------------------------------
  1087. ; Something about Cable changed ?
  1088.  
  1089.   @@:
  1090.         test    ax, ISR_LENCHG
  1091.         jz      .fail
  1092.  
  1093.         DEBUGF  2,"Cable changed!\n"
  1094.         call    cable
  1095.  
  1096. ; If none of the above events happened, just exit clearing int
  1097.  
  1098.   .fail:
  1099.  
  1100.         DEBUGF  2,"\n"
  1101.         ret
  1102.  
  1103.  
  1104.  
  1105.  
  1106. ;;;;;;;;;;;;;;;;;;;;;;;;;
  1107. ;;                     ;;
  1108. ;; Update Cable status ;;
  1109. ;;                     ;;
  1110. ;;;;;;;;;;;;;;;;;;;;;;;;;
  1111.  
  1112. align 4
  1113. cable:
  1114.         DEBUGF  1,"Checking Cable status: "
  1115.  
  1116.         mov     edx, dword [ebx+device.io_addr]
  1117.         add     edx, REG_MSR
  1118.         in      al , dx
  1119.  
  1120. ;        test    al , 1 SHL 2     ; 0 = link ok 1 = link fail
  1121. ;        jnz     .notconnected
  1122.  
  1123. ;        test    al , 1 SHL 3     ; 0 = 100 Mbps 1 = 10 Mbps
  1124. ;        jnz     .10mbps
  1125.  
  1126.         shr     al, 2
  1127.         and     al, 3
  1128.  
  1129.         mov     byte [ebx+device.mode+3], al
  1130.         DEBUGF  1,"Done!\n"
  1131. ret
  1132.  
  1133.  
  1134.  
  1135. ;;;;;;;;;;;;;;;;;;;;;;;
  1136. ;;                   ;;
  1137. ;; Write MAC address ;;
  1138. ;;                   ;;
  1139. ;;;;;;;;;;;;;;;;;;;;;;;
  1140.  
  1141. align 4
  1142. write_mac:      ; in: mac pushed onto stack (as 3 words)
  1143.  
  1144.         DEBUGF  1,"Writing MAC: "
  1145.  
  1146. ; disable all in command registers
  1147.  
  1148.         mov     edx, [ebx+device.io_addr]
  1149.         add     edx, REG_9346CR
  1150.         xor     eax, eax
  1151.         out     dx , al
  1152.  
  1153.         add     edx, REG_IMR - REG_9346CR
  1154.         xor     eax, eax
  1155.         out     dx , ax
  1156.  
  1157.         add     edx, REG_ISR - REG_IMR
  1158.         mov     eax, -1
  1159.         out     dx , ax
  1160.  
  1161. ; enable writing
  1162.  
  1163.  
  1164.         add     edx, REG_9346CR - REG_ISR
  1165.         mov     eax, REG_9346CR_WE
  1166.         out     dx , al
  1167.  
  1168.  ; write the mac ...
  1169.  
  1170.         add     edx, REG_IDR0 - REG_9346CR
  1171.         pop     eax
  1172.         out     dx , eax
  1173.  
  1174.         add     edx, 4
  1175.         xor     eax, eax
  1176.         pop     ax
  1177.         out     dx , eax
  1178.  
  1179. ; disable writing
  1180.  
  1181.         add     edx, REG_9346CR -REG_IDR0
  1182.         xor     eax, eax
  1183.         out     dx , al
  1184.  
  1185.         DEBUGF  1,"ok!\n"
  1186.  
  1187. ; Notice this procedure does not ret, but continues to read_mac instead.
  1188.  
  1189.  
  1190. ;;;;;;;;;;;;;;;;;;;;;;
  1191. ;;                  ;;
  1192. ;; Read MAC address ;;
  1193. ;;                  ;;
  1194. ;;;;;;;;;;;;;;;;;;;;;;
  1195.  
  1196. read_mac:
  1197.         DEBUGF  1,"Reading MAC: "
  1198.  
  1199.         mov     edx, [ebx + device.io_addr]
  1200.         lea     edi, [ebx + device.mac]
  1201.         in      eax, dx
  1202.         stosd
  1203.         add     edx, 4
  1204.         in      ax, dx
  1205.         stosw
  1206.  
  1207.         DEBUGF  1,"%x-%x-%x-%x-%x-%x\n",[edi-6]:2,[edi-5]:2,[edi-4]:2,[edi-3]:2,[edi-2]:2,[edi-1]:2
  1208.  
  1209.         ret
  1210.  
  1211.  
  1212.  
  1213. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1214. ;;                                                                      ;;
  1215. ;; Read eeprom (type 93c46 and 93c56)                                   ;;
  1216. ;;                                                                      ;;
  1217. ;; In: word to be read in al (6bit in case of 93c46 and 8bit otherwise) ;;
  1218. ;;     pointer to device structure in ebx                               ;;
  1219. ;;                                                                      ;;
  1220. ;; OUT: word read in ax                                                 ;;
  1221. ;;                                                                      ;;
  1222. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1223.  
  1224. align 4
  1225. read_eeprom:
  1226.         DEBUGF  2,"Reading eeprom, "
  1227.  
  1228.         mov     edx, [ebx+device.io_addr]
  1229.         push    ebx
  1230.         movzx   ebx, al
  1231.         add     edx, REG_RXCONFIG
  1232.         in      al, dx
  1233.         test    al, (1 shl BIT_9356SEL)
  1234.         jz      .type_93c46
  1235. ;       and     bl, 01111111b ; don't care first bit
  1236.         or      bx, EE_93C56_READ_CMD           ; it contains start bit
  1237.         mov     cx, EE_93C56_CMD_LENGTH-1       ; cmd_loop counter
  1238.         jmp     .read_eeprom
  1239. .type_93c46:
  1240.         and     bl, 00111111b
  1241.         or      bx, EE_93C46_READ_CMD           ; it contains start bit
  1242.         mov     cx, EE_93C46_CMD_LENGTH-1       ; cmd_loop counter
  1243. .read_eeprom:
  1244.         add     edx, REG_9346CR - REG_RXCONFIG
  1245. ;       mov     al, (1 shl BIT_93C46_EEM1)
  1246. ;       out     dx, al
  1247.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS) ; wake up the eeprom
  1248.         out     dx, al
  1249. .cmd_loop:
  1250.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
  1251.         bt      bx, cx
  1252.         jnc     .zero_bit
  1253.         or      al, (1 shl BIT_93C46_EEDI)
  1254. .zero_bit:
  1255.         out     dx, al
  1256. ;       push    eax
  1257. ;       in      eax, dx ; eeprom delay
  1258. ;       pop     eax
  1259.         or      al, (1 shl BIT_93C46_EESK)
  1260.         out     dx, al
  1261. ;       in      eax, dx ; eeprom delay
  1262.         dec     cx
  1263.         jns     .cmd_loop
  1264. ;       in      eax, dx ; eeprom delay
  1265.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
  1266.         out     dx, al
  1267.         mov     cl, 0xf
  1268. .read_loop:
  1269.         shl     ebx, 1
  1270.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS) or (1 shl BIT_93C46_EESK)
  1271.         out     dx, al
  1272. ;       in      eax, dx ; eeprom delay
  1273.         in      al, dx
  1274.         and     al, (1 shl BIT_93C46_EEDO)
  1275.         jz      .dont_set
  1276.         inc     ebx
  1277. .dont_set:
  1278.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
  1279.         out     dx, al
  1280. ;       in      eax, dx ; eeprom delay
  1281.         dec     cl
  1282.         jns     .read_loop
  1283.         xor     al, al
  1284.         out     dx, al
  1285.         mov     ax, bx
  1286.         pop     ebx
  1287.  
  1288.         ret
  1289.  
  1290.  
  1291. ; End of code
  1292.  
  1293. align 4                                         ; Place all initialised data here
  1294.  
  1295. RTL8139_DEV   dd 0
  1296. version       dd (5 shl 16) or (API_VERSION and 0xFFFF)
  1297. my_service    db 'RTL8139',0                    ; max 16 chars include zero
  1298.  
  1299. device_1      db 'Realtek 8139',0
  1300. device_2      db 'Realtek 8139A',0
  1301. device_3      db 'Realtek 8139B',0
  1302. device_4      db 'Realtek 8139C',0
  1303. device_5      db 'Realtek 8100',0
  1304. device_6      db 'Realtek 8139D',0
  1305. device_7      db 'Realtek 8139CP',0
  1306. device_8      db 'Realtek 8101',0
  1307.  
  1308. name_crosslist dd device_1
  1309.                dd device_2
  1310.                dd device_3
  1311.                dd device_4
  1312.                dd device_5
  1313.                dd device_6
  1314.                dd device_7
  1315.                dd device_8
  1316.  
  1317. hw_ver_array  db VER_RTL8139                    ; This array is used by the probe routine to find out wich version of the RTL8139 we are working with
  1318.               db VER_RTL8139A
  1319.               db VER_RTL8139B
  1320.               db VER_RTL8139C
  1321.               db VER_RTL8100
  1322.               db VER_RTL8139D
  1323.               db VER_RTL8139CP
  1324.               db VER_RTL8101
  1325.  
  1326. HW_VER_ARRAY_SIZE = $-hw_ver_array
  1327.  
  1328. include_debug_strings                           ; All data wich FDO uses will be included here
  1329.  
  1330. section '.data' data readable writable align 16 ; place all uninitialized data place here
  1331.  
  1332. RTL8139_LIST rd MAX_RTL8139                     ; This list contains all pointers to device structures the driver is handling
  1333.  
  1334.