Subversion Repositories Kolibri OS

Rev

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

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