Subversion Repositories Kolibri OS

Rev

Rev 1823 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1554 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2004-2010. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  RTL8169 driver for KolibriOS                                   ;;
7
;;                                                                 ;;
8
;;  Copyright 2007 mike.dld,                                       ;;
9
;;   mike.dld@gmail.com                                            ;;
10
;;                                                                 ;;
11
;;  Version 0.1  11 February 2007                                  ;;
12
;;  Version 0.2  3 August 2010 - port to net branch by hidnplayr   ;;
1823 hidnplayr 13
;;  Version 0.3  31 Januari 2011 - bugfixes by hidnplayr           ;;
1554 hidnplayr 14
;;                                                                 ;;
15
;;  References:                                                    ;;
16
;;    r8169.c - linux driver (etherboot project)                   ;;
17
;;                                                                 ;;
18
;;          GNU GENERAL PUBLIC LICENSE                             ;;
19
;;             Version 2, June 1991                                ;;
20
;;                                                                 ;;
21
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22
 
23
format MS COFF
24
 
2387 hidnplayr 25
        API_VERSION             equ 0x01000100
26
        DRIVER_VERSION          equ 5
1554 hidnplayr 27
 
2387 hidnplayr 28
        MAX_DEVICES             equ 16
1554 hidnplayr 29
 
2387 hidnplayr 30
        DEBUG                   equ 1
31
        __DEBUG__               equ 1
32
        __DEBUG_LEVEL__         equ 1
1554 hidnplayr 33
 
2387 hidnplayr 34
        NUM_TX_DESC             equ 4
35
        NUM_RX_DESC             equ 4
1554 hidnplayr 36
 
37
include 'proc32.inc'
38
include 'imports.inc'
39
include 'fdo.inc'
40
include 'netdrv.inc'
41
 
42
public START
43
public service_proc
44
public version
45
 
46
 
2387 hidnplayr 47
        REG_MAC0               equ 0x0 ; Ethernet hardware address
48
        REG_MAR0               equ 0x8 ; Multicast filter
49
        REG_TxDescStartAddr    equ 0x20
50
        REG_TxHDescStartAddr   equ 0x28
51
        REG_FLASH              equ 0x30
52
        REG_ERSR               equ 0x36
53
        REG_ChipCmd            equ 0x37
54
        REG_TxPoll             equ 0x38
55
        REG_IntrMask           equ 0x3C
56
        REG_IntrStatus         equ 0x3E
57
        REG_TxConfig           equ 0x40
58
        REG_RxConfig           equ 0x44
59
        REG_RxMissed           equ 0x4C
60
        REG_Cfg9346            equ 0x50
61
        REG_Config0            equ 0x51
62
        REG_Config1            equ 0x52
63
        REG_Config2            equ 0x53
64
        REG_Config3            equ 0x54
65
        REG_Config4            equ 0x55
66
        REG_Config5            equ 0x56
67
        REG_MultiIntr          equ 0x5C
68
        REG_PHYAR              equ 0x60
69
        REG_TBICSR             equ 0x64
70
        REG_TBI_ANAR           equ 0x68
71
        REG_TBI_LPAR           equ 0x6A
72
        REG_PHYstatus          equ 0x6C
73
        REG_RxMaxSize          equ 0xDA
74
        REG_CPlusCmd           equ 0xE0
75
        REG_RxDescStartAddr    equ 0xE4
76
        REG_ETThReg            equ 0xEC
77
        REG_FuncEvent          equ 0xF0
78
        REG_FuncEventMask      equ 0xF4
79
        REG_FuncPresetState    equ 0xF8
80
        REG_FuncForceEvent     equ 0xFC
1554 hidnplayr 81
 
2387 hidnplayr 82
        ; InterruptStatusBits
83
        ISB_SYSErr             equ 0x8000
84
        ISB_PCSTimeout         equ 0x4000
85
        ISB_SWInt              equ 0x0100
86
        ISB_TxDescUnavail      equ 0x80
87
        ISB_RxFIFOOver         equ 0x40
88
        ISB_LinkChg            equ 0x20
89
        ISB_RxOverflow         equ 0x10
90
        ISB_TxErr              equ 0x08
91
        ISB_TxOK               equ 0x04
92
        ISB_RxErr              equ 0x02
93
        ISB_RxOK               equ 0x01
1554 hidnplayr 94
 
2387 hidnplayr 95
        ; RxStatusDesc
96
        SD_RxRES               equ 0x00200000
97
        SD_RxCRC               equ 0x00080000
98
        SD_RxRUNT              equ 0x00100000
99
        SD_RxRWT               equ 0x00400000
1554 hidnplayr 100
 
2387 hidnplayr 101
        ; ChipCmdBits
102
        CMD_Reset              equ 0x10
103
        CMD_RxEnb              equ 0x08
104
        CMD_TxEnb              equ 0x04
105
        CMD_RxBufEmpty         equ 0x01
1554 hidnplayr 106
 
2387 hidnplayr 107
        ; Cfg9346Bits
108
        CFG_9346_Lock          equ 0x00
109
        CFG_9346_Unlock        equ 0xC0
1554 hidnplayr 110
 
2387 hidnplayr 111
        ; rx_mode_bits
112
        RXM_AcceptErr          equ 0x20
113
        RXM_AcceptRunt         equ 0x10
114
        RXM_AcceptBroadcast    equ 0x08
115
        RXM_AcceptMulticast    equ 0x04
116
        RXM_AcceptMyPhys       equ 0x02
117
        RXM_AcceptAllPhys      equ 0x01
1554 hidnplayr 118
 
2387 hidnplayr 119
        ; RxConfigBits
120
        RXC_FIFOShift          equ 13
121
        RXC_DMAShift           equ 8
1554 hidnplayr 122
 
2387 hidnplayr 123
        ; TxConfigBits
124
        TXC_InterFrameGapShift equ 24
125
        TXC_DMAShift           equ 8    ; DMA burst value (0-7) is shift this many bits
1554 hidnplayr 126
 
2387 hidnplayr 127
        ; PHYstatus
128
        PHYS_TBI_Enable        equ 0x80
129
        PHYS_TxFlowCtrl        equ 0x40
130
        PHYS_RxFlowCtrl        equ 0x20
131
        PHYS_1000bpsF          equ 0x10
132
        PHYS_100bps            equ 0x08
133
        PHYS_10bps             equ 0x04
134
        PHYS_LinkStatus        equ 0x02
135
        PHYS_FullDup           equ 0x01
1554 hidnplayr 136
 
2387 hidnplayr 137
        ; GIGABIT_PHY_registers
138
        PHY_CTRL_REG           equ 0
139
        PHY_STAT_REG           equ 1
140
        PHY_AUTO_NEGO_REG      equ 4
141
        PHY_1000_CTRL_REG      equ 9
1554 hidnplayr 142
 
2387 hidnplayr 143
        ; GIGABIT_PHY_REG_BIT
144
        PHY_Restart_Auto_Nego  equ 0x0200
145
        PHY_Enable_Auto_Nego   equ 0x1000
1554 hidnplayr 146
 
2387 hidnplayr 147
        ; PHY_STAT_REG = 1;
148
        PHY_Auto_Neco_Comp     equ 0x0020
1554 hidnplayr 149
 
2387 hidnplayr 150
        ; PHY_AUTO_NEGO_REG = 4;
151
        PHY_Cap_10_Half        equ 0x0020
152
        PHY_Cap_10_Full        equ 0x0040
153
        PHY_Cap_100_Half       equ 0x0080
154
        PHY_Cap_100_Full       equ 0x0100
1554 hidnplayr 155
 
2387 hidnplayr 156
        ; PHY_1000_CTRL_REG = 9;
157
        PHY_Cap_1000_Full      equ 0x0200
158
        PHY_Cap_1000_Half      equ 0x0100
1554 hidnplayr 159
 
2387 hidnplayr 160
        PHY_Cap_PAUSE          equ 0x0400
161
        PHY_Cap_ASYM_PAUSE     equ 0x0800
1554 hidnplayr 162
 
2387 hidnplayr 163
        PHY_Cap_Null           equ 0x0
1554 hidnplayr 164
 
2387 hidnplayr 165
        ; _MediaType
166
        MT_10_Half             equ 0x01
167
        MT_10_Full             equ 0x02
168
        MT_100_Half            equ 0x04
169
        MT_100_Full            equ 0x08
170
        MT_1000_Full           equ 0x10
1554 hidnplayr 171
 
2387 hidnplayr 172
        ; _TBICSRBit
173
        TBI_LinkOK             equ 0x02000000
1554 hidnplayr 174
 
2387 hidnplayr 175
        ; _DescStatusBit
176
        DSB_OWNbit             equ 0x80000000
177
        DSB_EORbit             equ 0x40000000
178
        DSB_FSbit              equ 0x20000000
179
        DSB_LSbit              equ 0x10000000
1554 hidnplayr 180
 
2387 hidnplayr 181
        RX_BUF_SIZE             equ 1536    ; Rx Buffer size
1554 hidnplayr 182
 
183
 
2387 hidnplayr 184
ETH_ALEN               equ 6
185
ETH_HLEN               equ (2 * ETH_ALEN + 2)
186
ETH_ZLEN               equ 60 ; 60 + 4bytes auto payload for
187
                                      ; mininmum 64bytes frame length
1554 hidnplayr 188
 
189
; MAC address length
2387 hidnplayr 190
MAC_ADDR_LEN        equ 6
1554 hidnplayr 191
 
192
; max supported gigabit ethernet frame size -- must be at least (dev->mtu+14+4)
193
MAX_ETH_FRAME_SIZE  equ 1536
194
 
2387 hidnplayr 195
TX_FIFO_THRESH      equ 256     ; In bytes
1554 hidnplayr 196
 
2387 hidnplayr 197
RX_FIFO_THRESH      equ 7       ; 7 means NO threshold, Rx buffer level before first PCI xfer
198
RX_DMA_BURST        equ 7       ; Maximum PCI burst, '6' is 1024
199
TX_DMA_BURST        equ 7       ; Maximum PCI burst, '6' is 1024
200
ETTh                equ 0x3F    ; 0x3F means NO threshold
1554 hidnplayr 201
 
2387 hidnplayr 202
EarlyTxThld         equ 0x3F    ; 0x3F means NO early transmit
203
RxPacketMaxSize     equ 0x0800  ; Maximum size supported is 16K-1
204
InterFrameGap       equ 0x03    ; 3 means InterFrameGap = the shortest one
1554 hidnplayr 205
 
2387 hidnplayr 206
HZ                  equ 1000
1554 hidnplayr 207
 
208
RTL_MIN_IO_SIZE     equ 0x80
2387 hidnplayr 209
TX_TIMEOUT          equ (6*HZ)
1554 hidnplayr 210
 
211
TIMER_EXPIRE_TIME equ 100
212
 
2387 hidnplayr 213
ETH_HDR_LEN         equ 14
214
DEFAULT_MTU         equ 1500
1554 hidnplayr 215
DEFAULT_RX_BUF_LEN  equ 1536
216
 
217
 
218
;#ifdef JUMBO_FRAME_SUPPORT
219
;#define MAX_JUMBO_FRAME_MTU    ( 10000 )
220
;#define MAX_RX_SKBDATA_SIZE    ( MAX_JUMBO_FRAME_MTU + ETH_HDR_LEN )
221
;#else
222
MAX_RX_SKBDATA_SIZE equ 1600
223
;#endif                         //end #ifdef JUMBO_FRAME_SUPPORT
224
 
2387 hidnplayr 225
MCFG_METHOD_01       equ 0x01
226
MCFG_METHOD_02       equ 0x02
227
MCFG_METHOD_03       equ 0x03
228
MCFG_METHOD_04       equ 0x04
229
MCFG_METHOD_05       equ 0x05
230
MCFG_METHOD_11       equ 0x0b
231
MCFG_METHOD_12       equ 0x0c
232
MCFG_METHOD_13       equ 0x0d
233
MCFG_METHOD_14       equ 0x0e
234
MCFG_METHOD_15       equ 0x0f
1554 hidnplayr 235
 
2387 hidnplayr 236
PCFG_METHOD_1       equ 0x01    ; PHY Reg 0x03 bit0-3 == 0x0000
237
PCFG_METHOD_2       equ 0x02    ; PHY Reg 0x03 bit0-3 == 0x0001
238
PCFG_METHOD_3       equ 0x03    ; PHY Reg 0x03 bit0-3 == 0x0002
1554 hidnplayr 239
 
240
virtual at 0
241
  tx_desc:
242
  .status    dd ?
243
  .vlan_tag  dd ?
1557 hidnplayr 244
  .buf_addr  dq ?
1554 hidnplayr 245
  .size = $
2387 hidnplayr 246
  rb    (NUM_TX_DESC-1)*tx_desc.size
247
  .buf_soft_addr        dd ?
1554 hidnplayr 248
end virtual
249
 
250
virtual at 0
251
  rx_desc:
252
  .status    dd ?
253
  .vlan_tag  dd ?
1557 hidnplayr 254
  .buf_addr  dq ?
1554 hidnplayr 255
  .size = $
2387 hidnplayr 256
  rb    (NUM_RX_DESC-1)*rx_desc.size
257
  .buf_soft_addr        dd ?
1554 hidnplayr 258
end virtual
259
 
260
virtual at ebx
261
 
2387 hidnplayr 262
        device:
1554 hidnplayr 263
 
2387 hidnplayr 264
        ETH_DEVICE
1554 hidnplayr 265
 
2387 hidnplayr 266
        .io_addr        dd ?
267
        .pci_bus        db ?
268
        .pci_dev        db ?
269
        .irq_line       db ?
1554 hidnplayr 270
 
2387 hidnplayr 271
        tpc:
272
        .mmio_addr      dd ? ; memory map physical address
273
        .chipset        dd ?
274
        .pcfg           dd ?
275
        .mcfg           dd ?
276
        .cur_rx         dd ? ; Index into the Rx descriptor buffer of next Rx pkt
277
        .cur_tx         dd ? ; Index into the Tx descriptor buffer of next Rx pkt
278
        .TxDescArrays   dd ? ; Index of Tx Descriptor buffer
279
        .RxDescArrays   dd ? ; Index of Rx Descriptor buffer
280
        .TxDescArray    dd ? ; Index of 256-alignment Tx Descriptor buffer
281
        .RxDescArray    dd ? ; Index of 256-alignment Rx Descriptor buffer
1554 hidnplayr 282
 
2387 hidnplayr 283
        rb 256-(($ - device) and 255)              ;        align 256
284
        tx_ring rb NUM_TX_DESC * tx_desc.size * 2
1554 hidnplayr 285
 
2387 hidnplayr 286
        rb 256-(($ - device) and 255)              ;        align 256
287
        rx_ring rb NUM_RX_DESC * rx_desc.size * 2
1554 hidnplayr 288
 
2387 hidnplayr 289
        device_size = $ - device
1554 hidnplayr 290
 
291
end virtual
292
 
293
intr_mask = ISB_LinkChg or ISB_RxOverflow or ISB_RxFIFOOver or ISB_TxErr or ISB_TxOK or ISB_RxErr or ISB_RxOK
294
rx_config = (RX_FIFO_THRESH shl RXC_FIFOShift) or (RX_DMA_BURST shl RXC_DMAShift) or 0x0000000E
295
 
296
 
2387 hidnplayr 297
macro   udelay msec {
1554 hidnplayr 298
 
2387 hidnplayr 299
        push    esi
300
        mov     esi, msec
301
        call    Sleep
302
        pop     esi
1554 hidnplayr 303
 
304
}
305
 
2387 hidnplayr 306
macro   WRITE_GMII_REG  RegAddr, value {
1554 hidnplayr 307
 
2387 hidnplayr 308
        set_io  REG_PHYAR
309
        if      value eq ax
310
        and     eax, 0x0000ffff
311
        or      eax, 0x80000000 + (RegAddr shl 16)
312
        else
313
        mov     eax, 0x80000000 + (RegAddr shl 16) + value
314
        end if
315
        out     dx, eax
1554 hidnplayr 316
 
2387 hidnplayr 317
        call    PHY_WAIT
1554 hidnplayr 318
}
319
 
2387 hidnplayr 320
macro   READ_GMII_REG  RegAddr {
1554 hidnplayr 321
 
2387 hidnplayr 322
local   .error, .done
1554 hidnplayr 323
 
2387 hidnplayr 324
        set_io  REG_PHYAR
325
        mov     eax, RegAddr shl 16
326
        out     dx, eax
1554 hidnplayr 327
 
2387 hidnplayr 328
        call    PHY_WAIT
329
        jz      .error
1554 hidnplayr 330
 
2387 hidnplayr 331
        in      eax, dx
332
        and     eax, 0xFFFF
333
        jmp     .done
1554 hidnplayr 334
 
335
  .error:
2387 hidnplayr 336
        or      eax, -1
1554 hidnplayr 337
  .done:
338
}
339
 
340
align 4
2387 hidnplayr 341
PHY_WAIT:       ; io addr must already be set to REG_PHYAR
1554 hidnplayr 342
 
2387 hidnplayr 343
        udelay  1        ;;;1000
1554 hidnplayr 344
 
2387 hidnplayr 345
        push    ecx
346
        mov     ecx, 2000
347
        ; Check if the RTL8169 has completed writing/reading to the specified MII register
1554 hidnplayr 348
    @@:
2387 hidnplayr 349
        in      eax, dx
350
        test    eax, 0x80000000
351
        jz      .exit
352
        udelay  1        ;;;100
353
        loop    @b
1554 hidnplayr 354
  .exit:
2387 hidnplayr 355
        pop     ecx
356
        ret
1554 hidnplayr 357
 
358
 
359
 
360
section '.flat' code readable align 16
361
 
362
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
363
;;                        ;;
364
;; proc START             ;;
365
;;                        ;;
366
;; (standard driver proc) ;;
367
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
368
 
369
align 4
370
proc START stdcall, state:dword
371
 
2387 hidnplayr 372
        cmp [state], 1
373
        jne .exit
1554 hidnplayr 374
 
375
  .entry:
376
 
2387 hidnplayr 377
        DEBUGF  2,"Loading rtl8169 driver\n"
378
        stdcall RegService, my_service, service_proc
379
        ret
1554 hidnplayr 380
 
381
  .fail:
382
  .exit:
2387 hidnplayr 383
        xor eax, eax
384
        ret
1554 hidnplayr 385
 
386
endp
387
 
388
 
389
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
390
;;                        ;;
391
;; proc SERVICE_PROC      ;;
392
;;                        ;;
393
;; (standard driver proc) ;;
394
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
395
 
396
align 4
397
proc service_proc stdcall, ioctl:dword
398
 
2387 hidnplayr 399
        mov     edx, [ioctl]
400
        mov     eax, [IOCTL.io_code]
1554 hidnplayr 401
 
402
;------------------------------------------------------
403
 
2387 hidnplayr 404
        cmp     eax, 0 ;SRV_GETVERSION
405
        jne     @F
1554 hidnplayr 406
 
2387 hidnplayr 407
        cmp     [IOCTL.out_size], 4
408
        jl      .fail
409
        mov     eax, [IOCTL.output]
410
        mov     [eax], dword API_VERSION
1554 hidnplayr 411
 
2387 hidnplayr 412
        xor     eax, eax
413
        ret
1554 hidnplayr 414
 
415
;------------------------------------------------------
416
  @@:
2387 hidnplayr 417
        cmp     eax, 1 ;SRV_HOOK
418
        jne     .fail
1554 hidnplayr 419
 
2387 hidnplayr 420
        cmp     [IOCTL.inp_size], 3                     ; Data input must be at least 3 bytes
421
        jl      .fail
1554 hidnplayr 422
 
2387 hidnplayr 423
        mov     eax, [IOCTL.input]
424
        cmp     byte [eax], 1                           ; 1 means device number and bus number (pci) are given
425
        jne     .fail                                   ; other types arent supported for this card yet
1554 hidnplayr 426
 
427
; check if the device is already listed
428
 
2387 hidnplayr 429
        mov     esi, device_list
430
        mov     ecx, [devices]
431
        test    ecx, ecx
432
        jz      .firstdevice
1554 hidnplayr 433
 
434
;        mov     eax, [IOCTL.input]                     ; get the pci bus and device numbers
2387 hidnplayr 435
        mov     ax , [eax+1]                            ;
1554 hidnplayr 436
  .nextdevice:
2387 hidnplayr 437
        mov     ebx, [esi]
438
        cmp     ax , word [device.pci_bus]              ; compare with pci and device num in device list (notice the usage of word instead of byte)
439
        je      .find_devicenum                         ; Device is already loaded, let's find it's device number
440
        add     esi, 4
441
        loop    .nextdevice
1554 hidnplayr 442
 
443
 
444
; This device doesnt have its own eth_device structure yet, lets create one
445
  .firstdevice:
2387 hidnplayr 446
        cmp     [devices], MAX_DEVICES                  ; First check if the driver can handle one more card
447
        jge     .fail
1554 hidnplayr 448
 
2387 hidnplayr 449
        allocate_and_clear ebx, device_size, .fail      ; Allocate memory to put the device structure in
1554 hidnplayr 450
 
451
; Fill in the direct call addresses into the struct
452
 
2387 hidnplayr 453
        mov     [device.reset], reset
454
        mov     [device.transmit], transmit
455
        mov     [device.get_MAC], read_mac
456
        mov     [device.set_MAC], write_mac
457
        mov     [device.unload], unload
458
        mov     [device.name], my_service
1554 hidnplayr 459
 
460
; save the pci bus and device numbers
461
 
2387 hidnplayr 462
        mov     eax, [IOCTL.input]
463
        mov     cl , [eax+1]
464
        mov     [device.pci_bus], cl
465
        mov     cl , [eax+2]
466
        mov     [device.pci_dev], cl
1554 hidnplayr 467
 
468
; Now, it's time to find the base io addres of the PCI device
469
 
2387 hidnplayr 470
        find_io [device.pci_bus], [device.pci_dev], [device.io_addr]
471
        mov     eax, [device.io_addr]
472
        mov     [tpc.mmio_addr], eax
1554 hidnplayr 473
 
474
; We've found the io address, find IRQ now
475
 
2387 hidnplayr 476
        find_irq [device.pci_bus], [device.pci_dev], [device.irq_line]
1554 hidnplayr 477
 
2387 hidnplayr 478
        DEBUGF  2,"Hooking into device, dev:%x, bus:%x, irq:%x, addr:%x\n",\
479
        [device.pci_dev]:1,[device.pci_bus]:1,[device.irq_line]:1,[device.io_addr]:8
1554 hidnplayr 480
 
481
; Ok, the eth_device structure is ready, let's probe the device
482
; Because initialization fires IRQ, IRQ handler must be aware of this device
2387 hidnplayr 483
        mov     eax, [devices]                                          ; Add the device structure to our device list
484
        mov     [device_list+4*eax], ebx                                ; (IRQ handler uses this list to find device)
485
        inc     [devices]                                               ;
1554 hidnplayr 486
 
2387 hidnplayr 487
        call    probe                                                   ; this function will output in eax
488
        test    eax, eax
489
        jnz     .err2                                                   ; If an error occured, exit
1554 hidnplayr 490
 
491
 
2387 hidnplayr 492
        mov     [device.type], NET_TYPE_ETH
493
        call    NetRegDev
1554 hidnplayr 494
 
2387 hidnplayr 495
        cmp     eax, -1
496
        je      .destroy
1554 hidnplayr 497
 
2387 hidnplayr 498
        ret
1554 hidnplayr 499
 
500
; If the device was already loaded, find the device number and return it in eax
501
 
502
  .find_devicenum:
2387 hidnplayr 503
        DEBUGF  2,"Trying to find device number of already registered device\n"
504
        mov     ebx, eax
505
        call    NetPtrToNum                                             ; This kernel procedure converts a pointer to device struct in ebx
506
                                                                        ; into a device number in edi
507
        mov     eax, edi                                                ; Application wants it in eax instead
508
        DEBUGF  2,"Kernel says: %u\n", eax
509
        ret
1554 hidnplayr 510
 
511
; If an error occured, remove all allocated data and exit (returning -1 in eax)
512
 
513
  .destroy:
2387 hidnplayr 514
        ; todo: reset device into virgin state
1554 hidnplayr 515
 
516
  .err2:
2387 hidnplayr 517
        dec     [devices]
1554 hidnplayr 518
  .err:
2387 hidnplayr 519
        DEBUGF  2,"removing device structure\n"
520
        stdcall KernelFree, ebx
1554 hidnplayr 521
 
522
 
523
  .fail:
2387 hidnplayr 524
        or      eax, -1
525
        ret
1554 hidnplayr 526
 
527
;------------------------------------------------------
528
endp
529
 
530
 
531
align 4
532
unload:
533
 
2387 hidnplayr 534
        ret
1554 hidnplayr 535
 
536
 
537
align 4
538
init_board:
539
 
2387 hidnplayr 540
        DEBUGF  1,"init_board\n"
1554 hidnplayr 541
 
2387 hidnplayr 542
        make_bus_master [device.pci_bus], [device.pci_dev]
1554 hidnplayr 543
 
2387 hidnplayr 544
        ; Soft reset the chip
545
        set_io  0
546
        set_io  REG_ChipCmd
547
        mov     al, CMD_Reset
548
        out     dx, al
1554 hidnplayr 549
 
2387 hidnplayr 550
        ; Check that the chip has finished the reset
551
        mov     ecx, 1000
552
        set_io  REG_ChipCmd
553
    @@: in      al, dx
554
        test    al, CMD_Reset
555
        jz      @f
556
        udelay  10
557
        loop    @b
1554 hidnplayr 558
    @@:
2387 hidnplayr 559
        ; identify config method
560
        set_io  REG_TxConfig
561
        in      eax, dx
562
        and     eax, 0x7c800000
563
        DEBUGF  1,"init_board: TxConfig & 0x7c800000 = 0x%x\n", eax
564
        mov     esi, mac_info-8
565
    @@: add     esi, 8
566
        mov     ecx, eax
567
        and     ecx, [esi]
568
        cmp     ecx, [esi]
569
        jne     @b
570
        mov     eax, [esi+4]
571
        mov     [tpc.mcfg], eax
1554 hidnplayr 572
 
2387 hidnplayr 573
        mov     [tpc.pcfg], PCFG_METHOD_3
574
        READ_GMII_REG 3
575
        and     al, 0x0f
576
        or      al, al
577
        jnz     @f
578
        mov     [tpc.pcfg], PCFG_METHOD_1
579
        jmp     .pconf
580
    @@: dec     al
581
        jnz     .pconf
582
        mov     [tpc.pcfg], PCFG_METHOD_2
1554 hidnplayr 583
  .pconf:
584
 
2387 hidnplayr 585
        ; identify chip attached to board
586
        mov     ecx, 10
587
        mov     eax, [tpc.mcfg]
588
    @@: dec     ecx
589
        js      @f
590
        cmp     eax, [rtl_chip_info+ecx*8]
591
        jne     @b
592
        mov     [tpc.chipset], ecx
593
        jmp     .match
1554 hidnplayr 594
    @@:
2387 hidnplayr 595
        ; if unknown chip, assume array element #0, original RTL-8169 in this case
596
        DEBUGF  1,"init_board: PCI device: unknown chip version, assuming RTL-8169\n"
597
        set_io  REG_TxConfig
598
        in      eax, dx
599
        DEBUGF  1,"init_board: PCI device: TxConfig = 0x%x\n", eax
1554 hidnplayr 600
 
2387 hidnplayr 601
        mov     [tpc.chipset],  0
1554 hidnplayr 602
 
2387 hidnplayr 603
        xor     eax, eax
604
        inc     eax
605
        ret
1554 hidnplayr 606
 
607
  .match:
2387 hidnplayr 608
        DEBUGF  1,"init_board: chipset=%u\n", ecx
609
        xor     eax,eax
610
        ret
1554 hidnplayr 611
 
612
 
613
 
614
;***************************************************************************
615
;   Function
616
;      probe
617
;   Description
618
;      Searches for an ethernet card, enables it and clears the rx buffer
619
;      If a card was found, it enables the ethernet -> TCPIP link
620
;   Destroyed registers
621
;      eax, ebx, ecx, edx
622
;
623
;***************************************************************************
624
align 4
625
probe:
626
 
2387 hidnplayr 627
        DEBUGF  1,"probe\n"
1554 hidnplayr 628
 
2387 hidnplayr 629
        call    init_board
1554 hidnplayr 630
 
2387 hidnplayr 631
        call    read_mac
1554 hidnplayr 632
 
2387 hidnplayr 633
        call    PHY_config
1554 hidnplayr 634
 
635
;       DEBUGF  1,"K :   Set MAC Reg C+CR Offset 0x82h = 0x01h\n"
2387 hidnplayr 636
        set_io  0
637
        set_io  0x82
638
        mov     al, 0x01
639
        out     dx, al
640
        cmp     [tpc.mcfg], MCFG_METHOD_03
641
        jae     @f
1554 hidnplayr 642
;       DEBUGF  1,"K :   Set PCI Latency=0x40\n"
643
;       stdcall pci_write_config_byte,PCI_LATENCY_TIMER,0x40
644
   @@:
2387 hidnplayr 645
        cmp     [tpc.mcfg], MCFG_METHOD_02
646
        jne     @f
1554 hidnplayr 647
;       DEBUGF  1,"K :   Set MAC Reg C+CR Offset 0x82h = 0x01h\n"
2387 hidnplayr 648
        set_io  0x82
649
        mov     al, 0x01
650
        out     dx, al
1554 hidnplayr 651
;       DEBUGF  1,"K :   Set PHY Reg 0x0bh = 0x00h\n"
2387 hidnplayr 652
        WRITE_GMII_REG 0x0b, 0x0000      ; w 0x0b 15 0 0
1554 hidnplayr 653
    @@:
2387 hidnplayr 654
        ; if TBI is not enabled
655
        set_io  0
656
        set_io  REG_PHYstatus
657
        in      al, dx
658
        test    al, PHYS_TBI_Enable
659
        jz      .tbi_dis
660
        READ_GMII_REG PHY_AUTO_NEGO_REG
1554 hidnplayr 661
 
2387 hidnplayr 662
        ; enable 10/100 Full/Half Mode, leave PHY_AUTO_NEGO_REG bit4:0 unchanged
663
        and     eax, 0x0C1F
664
        or      eax, PHY_Cap_10_Half or PHY_Cap_10_Full or PHY_Cap_100_Half or PHY_Cap_100_Full
665
        WRITE_GMII_REG PHY_AUTO_NEGO_REG, ax
1554 hidnplayr 666
 
2387 hidnplayr 667
        ; enable 1000 Full Mode
668
        WRITE_GMII_REG PHY_1000_CTRL_REG, PHY_Cap_1000_Full or PHY_Cap_1000_Half ; rtl8168
1554 hidnplayr 669
 
2387 hidnplayr 670
        ; Enable auto-negotiation and restart auto-nigotiation
671
        WRITE_GMII_REG PHY_CTRL_REG, PHY_Enable_Auto_Nego or PHY_Restart_Auto_Nego
1554 hidnplayr 672
 
2387 hidnplayr 673
        udelay  100
674
        mov     ecx, 10000
675
        ; wait for auto-negotiation process
676
    @@: dec     ecx
677
        jz      @f
678
        set_io  0
679
        READ_GMII_REG PHY_STAT_REG
680
        udelay  100
681
        test    eax, PHY_Auto_Neco_Comp
682
        jz      @b
683
        set_io  REG_PHYstatus
684
        in      al, dx
685
        jmp     @f
1554 hidnplayr 686
  .tbi_dis:
2387 hidnplayr 687
        udelay  100
1554 hidnplayr 688
    @@:
689
 
690
 
691
;***************************************************************************
692
;   Function
693
;      rt8169_reset
694
;   Description
695
;      Place the chip (ie, the ethernet card) into a virgin state
696
;   Destroyed registers
697
;      eax, ebx, ecx, edx
698
;
699
;***************************************************************************
700
align 4
701
reset:
702
 
2387 hidnplayr 703
        DEBUGF  1,"reset\n"
1554 hidnplayr 704
 
2387 hidnplayr 705
        lea     eax, [tx_ring]
706
        mov     [tpc.TxDescArrays], eax
707
        mov     [tpc.TxDescArray], eax
1554 hidnplayr 708
 
2387 hidnplayr 709
        lea     eax, [rx_ring]
710
        mov     [tpc.RxDescArrays], eax
711
        mov     [tpc.RxDescArray], eax
1554 hidnplayr 712
 
2387 hidnplayr 713
        call    init_ring
714
        call    hw_start
1554 hidnplayr 715
 
1558 hidnplayr 716
; clear packet/byte counters
717
 
2387 hidnplayr 718
        xor     eax, eax
719
        lea     edi, [device.bytes_tx]
720
        mov     ecx, 6
721
        rep     stosd
1558 hidnplayr 722
 
2387 hidnplayr 723
        mov     [device.mtu], 1500
1554 hidnplayr 724
 
2387 hidnplayr 725
        xor     eax, eax
726
        ret
1554 hidnplayr 727
 
728
 
729
 
730
 
731
 
732
align 4
733
PHY_config:
734
 
2387 hidnplayr 735
        DEBUGF  1,"hw_PHY_config: priv.mcfg=%d, priv.pcfg=%d\n",[tpc.mcfg],[tpc.pcfg]
1554 hidnplayr 736
 
2387 hidnplayr 737
        cmp     [tpc.mcfg], MCFG_METHOD_04
738
        jne     .not_4
739
        set_io  0
1554 hidnplayr 740
;       WRITE_GMII_REG 0x1F, 0x0001
741
;       WRITE_GMII_REG 0x1b, 0x841e
742
;       WRITE_GMII_REG 0x0e, 0x7bfb
743
;       WRITE_GMII_REG 0x09, 0x273a
2387 hidnplayr 744
        WRITE_GMII_REG 0x1F, 0x0002
745
        WRITE_GMII_REG 0x01, 0x90D0
746
        WRITE_GMII_REG 0x1F, 0x0000
747
        jmp     .exit
1554 hidnplayr 748
  .not_4:
2387 hidnplayr 749
        cmp     [tpc.mcfg], MCFG_METHOD_02
750
        je      @f
751
        cmp     [tpc.mcfg], MCFG_METHOD_03
752
        jne     .not_2_or_3
1557 hidnplayr 753
    @@:
2387 hidnplayr 754
        set_io  0
755
        WRITE_GMII_REG 0x1F, 0x0001
756
        WRITE_GMII_REG 0x15, 0x1000
757
        WRITE_GMII_REG 0x18, 0x65C7
758
        WRITE_GMII_REG 0x04, 0x0000
759
        WRITE_GMII_REG 0x03, 0x00A1
760
        WRITE_GMII_REG 0x02, 0x0008
761
        WRITE_GMII_REG 0x01, 0x1020
762
        WRITE_GMII_REG 0x00, 0x1000
763
        WRITE_GMII_REG 0x04, 0x0800
764
        WRITE_GMII_REG 0x04, 0x0000
765
        WRITE_GMII_REG 0x04, 0x7000
766
        WRITE_GMII_REG 0x03, 0xFF41
767
        WRITE_GMII_REG 0x02, 0xDE60
768
        WRITE_GMII_REG 0x01, 0x0140
769
        WRITE_GMII_REG 0x00, 0x0077
770
        WRITE_GMII_REG 0x04, 0x7800
771
        WRITE_GMII_REG 0x04, 0x7000
772
        WRITE_GMII_REG 0x04, 0xA000
773
        WRITE_GMII_REG 0x03, 0xDF01
774
        WRITE_GMII_REG 0x02, 0xDF20
775
        WRITE_GMII_REG 0x01, 0xFF95
776
        WRITE_GMII_REG 0x00, 0xFA00
777
        WRITE_GMII_REG 0x04, 0xA800
778
        WRITE_GMII_REG 0x04, 0xA000
779
        WRITE_GMII_REG 0x04, 0xB000
780
        WRITE_GMII_REG 0x03, 0xFF41
781
        WRITE_GMII_REG 0x02, 0xDE20
782
        WRITE_GMII_REG 0x01, 0x0140
783
        WRITE_GMII_REG 0x00, 0x00BB
784
        WRITE_GMII_REG 0x04, 0xB800
785
        WRITE_GMII_REG 0x04, 0xB000
786
        WRITE_GMII_REG 0x04, 0xF000
787
        WRITE_GMII_REG 0x03, 0xDF01
788
        WRITE_GMII_REG 0x02, 0xDF20
789
        WRITE_GMII_REG 0x01, 0xFF95
790
        WRITE_GMII_REG 0x00, 0xBF00
791
        WRITE_GMII_REG 0x04, 0xF800
792
        WRITE_GMII_REG 0x04, 0xF000
793
        WRITE_GMII_REG 0x04, 0x0000
794
        WRITE_GMII_REG 0x1F, 0x0000
795
        WRITE_GMII_REG 0x0B, 0x0000
796
        jmp     .exit
1554 hidnplayr 797
  .not_2_or_3:
2387 hidnplayr 798
        DEBUGF  1,"tpc.mcfg=%d, discard hw PHY config\n", [tpc.mcfg]
1554 hidnplayr 799
  .exit:
2387 hidnplayr 800
        ret
1554 hidnplayr 801
 
802
 
803
 
804
align 4
805
set_rx_mode:
806
 
2387 hidnplayr 807
        DEBUGF  1,"set_rx_mode\n"
1554 hidnplayr 808
 
2387 hidnplayr 809
        ; IFF_ALLMULTI
810
        ; Too many to filter perfectly -- accept all multicasts
811
        set_io  0
812
        set_io  REG_RxConfig
813
        in      eax, dx
814
        mov     ecx, [tpc.chipset]
815
        and     eax, [rtl_chip_info + ecx * 8 + 4] ; RxConfigMask
816
        or      eax, rx_config or (RXM_AcceptBroadcast or RXM_AcceptMulticast or RXM_AcceptMyPhys)
817
        out     dx, eax
1554 hidnplayr 818
 
2387 hidnplayr 819
        ; Multicast hash filter
820
        set_io  REG_MAR0 + 0
821
        or      eax, -1
822
        out     dx, eax
823
        set_io  REG_MAR0 + 4
824
        out     dx, eax
1554 hidnplayr 825
 
2387 hidnplayr 826
        ret
1554 hidnplayr 827
 
828
 
829
align 4
830
init_ring:
831
 
2387 hidnplayr 832
        DEBUGF  1,"init_ring\n"
1554 hidnplayr 833
 
2387 hidnplayr 834
        xor     eax, eax
835
        mov     [tpc.cur_rx], eax
836
        mov     [tpc.cur_tx], eax
1554 hidnplayr 837
 
2387 hidnplayr 838
        lea     edi, [tx_ring]
839
        mov     ecx, (NUM_TX_DESC * tx_desc.size) / 4
840
        rep     stosd
1554 hidnplayr 841
 
2387 hidnplayr 842
        lea     edi, [rx_ring]
843
        mov     ecx, (NUM_RX_DESC * rx_desc.size) / 4
844
        rep     stosd
1554 hidnplayr 845
 
2387 hidnplayr 846
        mov     edi, [tpc.RxDescArray]
847
        mov     ecx, NUM_RX_DESC
1557 hidnplayr 848
  .loop:
2387 hidnplayr 849
        push    ecx
850
        stdcall KernelAlloc, RX_BUF_SIZE
851
        mov     [edi + rx_desc.buf_soft_addr], eax
852
        call    GetPgAddr
853
        mov     dword [edi + rx_desc.buf_addr], eax
854
        mov     [edi + rx_desc.status], DSB_OWNbit or RX_BUF_SIZE
855
        add     edi, rx_desc.size
856
        pop     ecx
857
        loop    .loop
858
        or      [edi - rx_desc.size + rx_desc.status], DSB_EORbit
1554 hidnplayr 859
 
2387 hidnplayr 860
        ret
1554 hidnplayr 861
 
862
 
863
align 4
864
hw_start:
865
 
2387 hidnplayr 866
        DEBUGF  1,"hw_start\n"
1554 hidnplayr 867
 
1556 hidnplayr 868
; attach int handler
2387 hidnplayr 869
        movzx   eax, [device.irq_line]
870
        DEBUGF  1,"Attaching int handler to irq %x\n", eax:1
871
        stdcall AttachIntHandler, eax, int_handler, dword 0
1556 hidnplayr 872
 
2387 hidnplayr 873
        ; Soft reset the chip
874
        set_io  0
875
        set_io  REG_ChipCmd
876
        mov     al, CMD_Reset
877
        out     dx, al
1554 hidnplayr 878
 
2387 hidnplayr 879
        ; Check that the chip has finished the reset
880
        mov     ecx, 1000
881
        set_io  REG_ChipCmd
882
    @@: in      al, dx
883
        test    al, CMD_Reset
884
        jz      @f
885
        udelay  10
886
        loop    @b
1554 hidnplayr 887
    @@:
888
 
2387 hidnplayr 889
        set_io  REG_Cfg9346
890
        mov     al, CFG_9346_Unlock
891
        out     dx, al
1554 hidnplayr 892
 
2387 hidnplayr 893
        set_io  REG_ChipCmd
894
        mov     al, CMD_TxEnb or CMD_RxEnb
895
        out     dx, al
1554 hidnplayr 896
 
2387 hidnplayr 897
        set_io  REG_ETThReg
898
        mov     al, ETTh
899
        out     dx, al
1554 hidnplayr 900
 
2387 hidnplayr 901
        ; For gigabit rtl8169
902
        set_io  REG_RxMaxSize
903
        mov     ax, RxPacketMaxSize
904
        out     dx, ax
1554 hidnplayr 905
 
2387 hidnplayr 906
        ; Set Rx Config register
907
        set_io  REG_RxConfig
908
        in      ax, dx
909
        mov     ecx, [tpc.chipset]
910
        and     eax, [rtl_chip_info + ecx * 8 + 4] ; RxConfigMask
911
        or      eax, rx_config
912
        out     dx, eax
1554 hidnplayr 913
 
2387 hidnplayr 914
        ; Set DMA burst size and Interframe Gap Time
915
        set_io  REG_TxConfig
916
        mov     eax, (TX_DMA_BURST shl TXC_DMAShift) or (InterFrameGap shl TXC_InterFrameGapShift)
917
        out     dx, eax
1554 hidnplayr 918
 
2387 hidnplayr 919
        set_io  REG_CPlusCmd
920
        in      ax, dx
921
        out     dx, ax
1554 hidnplayr 922
 
2387 hidnplayr 923
        in      ax, dx
924
        or      ax, 1 shl 3
925
        cmp     [tpc.mcfg], MCFG_METHOD_02
926
        jne     @f
927
        cmp     [tpc.mcfg], MCFG_METHOD_03
928
        jne     @f
929
        or      ax,1 shl 14
930
        DEBUGF  1,"Set MAC Reg C+CR Offset 0xE0: bit-3 and bit-14\n"
931
        jmp     .set
1554 hidnplayr 932
    @@:
2387 hidnplayr 933
        DEBUGF  1,"Set MAC Reg C+CR Offset 0xE0: bit-3\n"
1554 hidnplayr 934
  .set:
2387 hidnplayr 935
        set_io  REG_CPlusCmd
936
        out     dx, ax
1554 hidnplayr 937
 
2387 hidnplayr 938
        set_io  0xE2
1554 hidnplayr 939
;        mov     ax, 0x1517
940
;        out     dx, ax
941
;        mov     ax, 0x152a
942
;        out     dx, ax
943
;        mov     ax, 0x282a
944
;        out     dx, ax
2387 hidnplayr 945
        xor     ax, ax
946
        out     dx, ax
1554 hidnplayr 947
 
2387 hidnplayr 948
        xor     eax, eax
949
        mov     [tpc.cur_rx], eax
950
        lea     eax, [tx_ring]
951
        GetRealAddr
952
        set_io  REG_TxDescStartAddr
953
        out     dx, eax
1554 hidnplayr 954
 
2387 hidnplayr 955
        lea     eax, [rx_ring]
956
        GetRealAddr
957
        set_io  REG_RxDescStartAddr
958
        out     dx, eax
1554 hidnplayr 959
 
2387 hidnplayr 960
        set_io  REG_Cfg9346
961
        mov     al, CFG_9346_Lock
962
        out     dx, al
1554 hidnplayr 963
 
2387 hidnplayr 964
        udelay  10
1554 hidnplayr 965
 
2387 hidnplayr 966
        xor     eax, eax
967
        set_io  REG_RxMissed
968
        out     dx, eax
1554 hidnplayr 969
 
2387 hidnplayr 970
        call    set_rx_mode
1554 hidnplayr 971
 
2387 hidnplayr 972
        set_io  0
973
        ; no early-rx interrupts
974
        set_io  REG_MultiIntr
975
        in      ax, dx
976
        and     ax, 0xF000
977
        out     dx, ax
1554 hidnplayr 978
 
2387 hidnplayr 979
        ; set interrupt mask
980
        set_io  REG_IntrMask
981
        mov     ax, intr_mask
982
        out     dx, ax
1554 hidnplayr 983
 
2387 hidnplayr 984
        xor     eax, eax
985
        ret
1554 hidnplayr 986
 
987
 
988
align 4
989
read_mac:
990
 
2387 hidnplayr 991
        set_io  0
992
        set_io  REG_MAC0
993
        xor     ecx, ecx
994
        lea     edi, [device.mac]
995
        mov     ecx, MAC_ADDR_LEN
1554 hidnplayr 996
 
2387 hidnplayr 997
        ; Get MAC address. FIXME: read EEPROM
998
    @@: in      al, dx
999
        stosb
1000
        inc     edx
1001
        loop    @r
1554 hidnplayr 1002
 
2387 hidnplayr 1003
        DEBUGF  1,"MAC = %x-%x-%x-%x-%x-%x\n",[device.mac+0]:2,[device.mac+1]:2,[device.mac+2]:2,[device.mac+3]:2,[device.mac+4]:2,[device.mac+5]:2
1554 hidnplayr 1004
 
2387 hidnplayr 1005
        ret
1554 hidnplayr 1006
 
1007
align 4
1008
write_mac:
1009
 
2387 hidnplayr 1010
        ret     6
1554 hidnplayr 1011
 
1012
 
1013
 
1014
 
1015
 
1016
;***************************************************************************
1017
;   Function
1018
;      transmit
1019
;   Description
1020
;      Transmits a packet of data via the ethernet card
1021
;
1022
;   Destroyed registers
1023
;      eax, edx, esi, edi
1024
;
1025
;***************************************************************************
1026
align 4
1027
transmit:
1028
 
2387 hidnplayr 1029
        DEBUGF  1,"Transmitting packet, buffer:%x, size:%u\n", [esp+4], [esp+8]
1030
        mov     eax, [esp+4]
1031
        DEBUGF  1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
1032
        [eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
1033
        [eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
1034
        [eax+13]:2,[eax+12]:2
1554 hidnplayr 1035
 
2387 hidnplayr 1036
        cmp     dword [esp+8], MAX_ETH_FRAME_SIZE
1037
        ja      .fail
1554 hidnplayr 1038
 
1039
;----------------------------------
1040
; Find currentTX descriptor address
1041
 
2387 hidnplayr 1042
        mov     eax, tx_desc.size
1043
        mul     [tpc.cur_tx]
1044
        lea     esi, [eax + tx_ring]
1554 hidnplayr 1045
 
2387 hidnplayr 1046
        DEBUGF  1,"Using TX desc: %x\n", esi
1823 hidnplayr 1047
 
1554 hidnplayr 1048
;---------------------------
1049
; Program the packet pointer
1050
 
2387 hidnplayr 1051
        mov     eax, [esp + 4]
1052
        mov     [esi + tx_desc.buf_soft_addr], eax
1053
        GetRealAddr
1054
        mov     dword [esi + tx_desc.buf_addr], eax
1554 hidnplayr 1055
 
1056
;------------------------
1057
; Program the packet size
1058
 
2387 hidnplayr 1059
        mov     eax, [esp + 8]
1060
    @@: or      eax, DSB_OWNbit or DSB_FSbit or DSB_LSbit
1061
        cmp     [tpc.cur_tx], NUM_TX_DESC - 1
1062
        jne     @f
1063
        or      eax, DSB_EORbit
1064
    @@: mov     [esi + tx_desc.status], eax
1554 hidnplayr 1065
 
1558 hidnplayr 1066
;-----------------------------------------
1067
; Set the polling bit (start transmission)
1554 hidnplayr 1068
 
2387 hidnplayr 1069
        set_io  0
1070
        set_io  REG_TxPoll
1071
        mov     al, 0x40     ; set polling bit
1072
        out     dx, al
1554 hidnplayr 1073
 
1074
;-----------------------
1075
; Update TX descriptor
1076
 
2387 hidnplayr 1077
        inc     [tpc.cur_tx]
1078
        and     [tpc.cur_tx], NUM_TX_DESC - 1
1558 hidnplayr 1079
 
1080
;-------------
1081
; Update stats
1082
 
2387 hidnplayr 1083
        inc     [device.packets_tx]
1084
        mov     eax, [esp+8]
1085
        add     dword [device.bytes_tx], eax
1086
        adc     dword [device.bytes_tx + 4], 0
1558 hidnplayr 1087
 
2387 hidnplayr 1088
        xor     eax, eax
1089
        ret     8
1554 hidnplayr 1090
 
1091
  .fail:
2387 hidnplayr 1092
        DEBUGF  1,"transmit failed\n"
1093
        or      eax, -1
1094
        stdcall KernelFree, [esp+4]
1095
        ret     8
1554 hidnplayr 1096
 
1097
 
1098
;;;DSB_OWNbit
1099
 
1100
 
1101
;;;;;;;;;;;;;;;;;;;;;;;
1102
;;                   ;;
1103
;; Interrupt handler ;;
1104
;;                   ;;
1105
;;;;;;;;;;;;;;;;;;;;;;;
1106
 
1107
align 4
1108
int_handler:
1109
 
2387 hidnplayr 1110
        DEBUGF  1,"IRQ %x ",eax:2
1554 hidnplayr 1111
 
1112
; find pointer of device wich made IRQ occur
1113
 
2387 hidnplayr 1114
        mov     ecx, [devices]
1115
        test    ecx, ecx
1116
        jz      .fail
1117
        mov     esi, device_list
1554 hidnplayr 1118
  .nextdevice:
2387 hidnplayr 1119
        mov     ebx, dword [esi]
1554 hidnplayr 1120
 
2387 hidnplayr 1121
        set_io  0
1122
        set_io  REG_IntrStatus
1123
        in      ax, dx
1554 hidnplayr 1124
 
2387 hidnplayr 1125
        test    ax, ax
1126
        jnz     .got_it
1554 hidnplayr 1127
 
1128
  .continue:
2387 hidnplayr 1129
        add     esi, 4
1130
        dec     ecx
1131
        jnz     .nextdevice
1554 hidnplayr 1132
 
2387 hidnplayr 1133
        ret                                             ; If no device was found, abort (The irq was probably for a device, not registered to this driver)
1554 hidnplayr 1134
 
1135
  .got_it:
2387 hidnplayr 1136
        DEBUGF  1,"IntrStatus = 0x%x\n",ax
1554 hidnplayr 1137
 
2387 hidnplayr 1138
        cmp     ax, 0xFFFF      ; if so, hardware is no longer present
1139
        je      .fail
1554 hidnplayr 1140
 
1141
;--------
1142
; Receive
1143
 
2387 hidnplayr 1144
        test    ax, ISB_RxOK
1145
        jz      .no_rx
1554 hidnplayr 1146
 
2387 hidnplayr 1147
        push    ax
1148
        push    ebx
1554 hidnplayr 1149
 
1559 hidnplayr 1150
  .check_more:
2387 hidnplayr 1151
        pop     ebx
1152
        DEBUGF  1,"ebx = 0x%x\n", ebx
1153
        mov     eax, rx_desc.size
1154
        mul     [tpc.cur_rx]
1155
        lea     esi, [eax + rx_ring]
1554 hidnplayr 1156
 
2387 hidnplayr 1157
        DEBUGF  1,"RxDesc.status = 0x%x\n", [esi + rx_desc.status]
1554 hidnplayr 1158
 
2387 hidnplayr 1159
        mov     eax, [esi + rx_desc.status]
1160
        test    eax, DSB_OWNbit ;;;
1161
        jnz     .rx_return
1554 hidnplayr 1162
 
2387 hidnplayr 1163
        DEBUGF  1,"tpc.cur_rx = %u\n", [tpc.cur_rx]
1554 hidnplayr 1164
 
2387 hidnplayr 1165
        test    eax, SD_RxRES
1166
        jnz     .rx_return      ;;;;; RX error!
1554 hidnplayr 1167
 
2387 hidnplayr 1168
        push    ebx
1169
        push    .check_more
1170
        and     eax, 0x00001FFF
1171
        add     eax, -4                         ; we dont need CRC
1172
        push    eax
1173
        DEBUGF  1,"data length = %u\n", ax
1554 hidnplayr 1174
 
1558 hidnplayr 1175
;-------------
1176
; Update stats
1559 hidnplayr 1177
 
2387 hidnplayr 1178
        add     dword [device.bytes_rx], eax
1179
        adc     dword [device.bytes_rx + 4], 0
1180
        inc     dword [device.packets_rx]
1558 hidnplayr 1181
 
2387 hidnplayr 1182
        push    [esi + rx_desc.buf_soft_addr]
1554 hidnplayr 1183
 
1557 hidnplayr 1184
;----------------------
1185
; Allocate a new buffer
1186
 
2387 hidnplayr 1187
        stdcall KernelAlloc, RX_BUF_SIZE
1188
        mov     [esi + rx_desc.buf_soft_addr], eax
1189
        GetRealAddr
1190
        mov     dword [esi + rx_desc.buf_addr], eax
1557 hidnplayr 1191
 
1554 hidnplayr 1192
;---------------
1193
; re set OWN bit
1194
 
2387 hidnplayr 1195
        mov     eax, DSB_OWNbit or RX_BUF_SIZE
1196
        cmp     [tpc.cur_rx], NUM_RX_DESC - 1
1197
        jne     @f
1198
        or      eax, DSB_EORbit
1199
    @@: mov     [esi + rx_desc.status], eax
1554 hidnplayr 1200
 
1201
;--------------
1202
; Update rx ptr
1203
 
2387 hidnplayr 1204
        inc     [tpc.cur_rx]
1205
        and     [tpc.cur_rx], NUM_RX_DESC - 1
1554 hidnplayr 1206
 
2387 hidnplayr 1207
        jmp     EthReceiver
1554 hidnplayr 1208
  .rx_return:
1209
 
2387 hidnplayr 1210
        pop     ax
1554 hidnplayr 1211
  .no_rx:
1212
 
1213
;---------
1214
; Transmit
1215
 
2387 hidnplayr 1216
        test    ax, ISB_TxOK
1217
        jz      .no_tx
1218
        push    ax
1554 hidnplayr 1219
 
2387 hidnplayr 1220
        DEBUGF  1,"TX ok!\n"
1554 hidnplayr 1221
 
2387 hidnplayr 1222
        mov     ecx, NUM_TX_DESC
1223
        lea     esi, [tx_ring]
1558 hidnplayr 1224
  .txloop:
2387 hidnplayr 1225
        cmp     [esi+tx_desc.buf_soft_addr], 0
1226
        jz      .maybenext
1558 hidnplayr 1227
 
2387 hidnplayr 1228
        test    [esi+tx_desc.status], DSB_OWNbit
1229
        jnz     .maybenext
1558 hidnplayr 1230
 
2387 hidnplayr 1231
        push    ecx
1232
        DEBUGF  1,"Freeing up TX desc: %x\n", esi
1233
        stdcall KernelFree, [esi+tx_desc.buf_soft_addr]
1234
        pop     ecx
1235
        and     [esi+tx_desc.buf_soft_addr], 0
1558 hidnplayr 1236
 
1237
  .maybenext:
2387 hidnplayr 1238
        add     esi, tx_desc.size
1239
        dec     ecx
1240
        jnz     .txloop
1558 hidnplayr 1241
 
2387 hidnplayr 1242
        pop     ax
1554 hidnplayr 1243
  .no_tx:
1244
 
1245
;-------
1246
; Finish
1247
 
2387 hidnplayr 1248
        set_io  0
1249
        set_io  REG_IntrStatus
1250
        out     dx, ax                  ; ACK all interrupts
1554 hidnplayr 1251
 
1252
  .fail:
2387 hidnplayr 1253
        ret
1554 hidnplayr 1254
 
1255
 
1256
 
1257
 
1258
 
1259
 
1260
 
1261
 
1262
 
1263
; End of code
2387 hidnplayr 1264
align 4                                         ; Place all initialised data here
1554 hidnplayr 1265
 
1266
devices       dd 0
1267
version       dd (DRIVER_VERSION shl 16) or (API_VERSION and 0xFFFF)
2387 hidnplayr 1268
my_service    db 'RTL8169',0                    ; max 16 chars include zero
1554 hidnplayr 1269
 
2387 hidnplayr 1270
include_debug_strings                           ; All data wich FDO uses will be included here
1554 hidnplayr 1271
 
1272
rtl_chip_info dd \
1273
  MCFG_METHOD_01, 0xff7e1880, \ ; RTL8169
1274
  MCFG_METHOD_02, 0xff7e1880, \ ; RTL8169s/8110s
1275
  MCFG_METHOD_03, 0xff7e1880, \ ; RTL8169s/8110s
1276
  MCFG_METHOD_04, 0xff7e1880, \ ; RTL8169sb/8110sb
1277
  MCFG_METHOD_05, 0xff7e1880, \ ; RTL8169sc/8110sc
1278
  MCFG_METHOD_11, 0xff7e1880, \ ; RTL8168b/8111b   // PCI-E
1279
  MCFG_METHOD_12, 0xff7e1880, \ ; RTL8168b/8111b   // PCI-E
1280
  MCFG_METHOD_13, 0xff7e1880, \ ; RTL8101e         // PCI-E 8139
1281
  MCFG_METHOD_14, 0xff7e1880, \ ; RTL8100e         // PCI-E 8139
2387 hidnplayr 1282
  MCFG_METHOD_15, 0xff7e1880    ; RTL8100e         // PCI-E 8139
1554 hidnplayr 1283
 
1284
mac_info dd \
1285
  0x38800000, MCFG_METHOD_15, \
1286
  0x38000000, MCFG_METHOD_12, \
1287
  0x34000000, MCFG_METHOD_13, \
1288
  0x30800000, MCFG_METHOD_14, \
1289
  0x30000000, MCFG_METHOD_11, \
1290
  0x18000000, MCFG_METHOD_05, \
1291
  0x10000000, MCFG_METHOD_04, \
1292
  0x04000000, MCFG_METHOD_03, \
1293
  0x00800000, MCFG_METHOD_02, \
2387 hidnplayr 1294
  0x00000000, MCFG_METHOD_01    ; catch-all
1554 hidnplayr 1295
 
2387 hidnplayr 1296
name_01         db "RTL8169", 0
1297
name_02_03      db "RTL8169s/8110s", 0
1298
name_04         db "RTL8169sb/8110sb", 0
1299
name_05         db "RTL8169sc/8110sc", 0
1300
name_11_12      db "RTL8168b/8111b", 0  ; PCI-E
1301
name_13         db "RTL8101e", 0        ; PCI-E 8139
1302
name_14_15      db "RTL8100e", 0        ; PCI-E 8139
1558 hidnplayr 1303
 
1304
 
1554 hidnplayr 1305
section '.data' data readable writable align 16 ; place all uninitialized data place here
1306
 
2387 hidnplayr 1307
device_list rd MAX_DEVICES                     ; This list contains all pointers to device structures the driver is handling
1554 hidnplayr 1308