Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
444 serge 1
$Revision: 431 $
261 hidnplayr 2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3
;;                                                                 ;;
444 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
302 hidnplayr 357
	mov	eax, rtl8139_rx_buff
358
	add	edx, RTL8139_REG_RBSTART - RTL8139_REG_IMR
359
	out	dx, eax
261 hidnplayr 360
; Indicate that we have successfully reset the card
302 hidnplayr 361
	mov	eax, [pci_data]
362
	mov	[eth_status], eax
363
	ret
261 hidnplayr 364
 
365
;***************************************************************************
366
;   Function
367
;      rtl8139_read_eeprom
368
;   Description
369
;      reads eeprom type 93c46 and 93c56
370
;   Parameters
371
;      al - word to be read (6bit in case of 93c46 and 8bit otherwise)
372
;   Return value
373
;      ax - word read in
374
;   Destroyed register(s)
375
;      eax, cx, ebx, edx
376
;
377
;***************************************************************************
378
rtl8139_read_eeprom:
302 hidnplayr 379
	movzx	ebx, al
380
	mov	edx, [io_addr]
381
	add	edx, RTL8139_REG_RXCONFIG
382
	in	al, dx
383
	test	al, (1 shl RTL8139_BIT_9356SEL)
384
	jz	.type_93c46
261 hidnplayr 385
;       and     bl, 01111111b ; don't care first bit
302 hidnplayr 386
	or	bx, EE_93C56_READ_CMD ; it contains start bit
387
	mov	cx, EE_93C56_CMD_LENGTH-1 ; cmd_loop counter
388
	jmp	.read_eeprom
261 hidnplayr 389
.type_93c46:
302 hidnplayr 390
	and	bl, 00111111b
391
	or	bx, EE_93C46_READ_CMD ; it contains start bit
392
	mov	cx, EE_93C46_CMD_LENGTH-1 ; cmd_loop counter
261 hidnplayr 393
.read_eeprom:
302 hidnplayr 394
	add	edx, RTL8139_REG_9346CR - RTL8139_REG_RXCONFIG_0
261 hidnplayr 395
;       mov     al, (1 shl RTL8139_BIT_93C46_EEM1)
396
;       out     dx, al
302 hidnplayr 397
	mov	al, (1 shl RTL8139_BIT_93C46_EEM1) \
398
		    or (1 shl RTL8139_BIT_93C46_EECS) ; wake up the eeprom
399
	out	dx, al
261 hidnplayr 400
.cmd_loop:
302 hidnplayr 401
	mov	al, (1 shl RTL8139_BIT_93C46_EEM1) or (1 shl RTL8139_BIT_93C46_EECS)
402
	bt	bx, cx
403
	jnc	.zero_bit
404
	or	al, (1 shl RTL8139_BIT_93C46_EEDI)
261 hidnplayr 405
.zero_bit:
302 hidnplayr 406
	out	dx, al
261 hidnplayr 407
;       push    eax
408
;       in      eax, dx ; eeprom delay
409
;       pop     eax
302 hidnplayr 410
	or	al, (1 shl RTL8139_BIT_93C46_EESK)
411
	out	dx, al
261 hidnplayr 412
;       in      eax, dx ; eeprom delay
302 hidnplayr 413
	dec	cx
414
	jns	.cmd_loop
261 hidnplayr 415
;       in      eax, dx ; eeprom delay
302 hidnplayr 416
	mov	al, (1 shl RTL8139_BIT_93C46_EEM1) or (1 shl RTL8139_BIT_93C46_EECS)
417
	out	dx, al
418
	mov	cl, 0xf
261 hidnplayr 419
.read_loop:
302 hidnplayr 420
	shl	ebx, 1
421
	mov	al, (1 shl RTL8139_BIT_93C46_EEM1) \
422
		    or (1 shl RTL8139_BIT_93C46_EECS) \
423
		    or (1 shl RTL8139_BIT_93C46_EESK)
424
	out	dx, al
261 hidnplayr 425
;       in      eax, dx ; eeprom delay
302 hidnplayr 426
	in	al, dx
427
	and	al, (1 shl RTL8139_BIT_93C46_EEDO)
428
	jz	.dont_set
429
	inc	ebx
261 hidnplayr 430
.dont_set:
302 hidnplayr 431
	mov	al, (1 shl RTL8139_BIT_93C46_EEM1) \
432
		    or (1 shl RTL8139_BIT_93C46_EECS)
433
	out	dx, al
261 hidnplayr 434
;       in      eax, dx ; eeprom delay
302 hidnplayr 435
	dec	cl
436
	jns	.read_loop
437
	xor	al, al
438
	out	dx, al
439
	mov	ax, bx
440
	ret
261 hidnplayr 441
 
442
;***************************************************************************
443
;   Function
444
;      rtl8139_transmit
445
;   Description
446
;      Transmits a packet of data via the ethernet card
447
;         Pointer to 48 bit destination address in edi
448
;         Type of packet in bx
373 mikedld 449
;         Size of packet in ecx
450
;         Pointer to packet data in esi
261 hidnplayr 451
;   Destroyed registers
452
;      eax, edx, esi, edi
453
;   ToDo
454
;      for waiting of timeout the rtl8139 internal timer
455
;      should be used
456
;
457
;***************************************************************************
458
rtl8139_transmit:
302 hidnplayr 459
	cmp	ecx, MAX_ETH_FRAME_SIZE
460
	jg	.finish ; packet is too long
461
	push	ecx
261 hidnplayr 462
; check descriptor
302 hidnplayr 463
	mov	ecx, [curr_tx_desc]
464
	mov	edx, [io_addr]
465
	lea	edx, [edx+ecx*4+RTL8139_REG_TSD0]
466
	push	edx ebx
467
	in	ax, dx
323 hidnplayr 468
	test    ax, 0x1fff ; or no size given
469
      jz      .send_packet
470
      and	ax, (1 shl RTL8139_BIT_TOK) or (1 shl RTL8139_BIT_OWN)
302 hidnplayr 471
	cmp	ax, (1 shl RTL8139_BIT_TOK) or (1 shl RTL8139_BIT_OWN)
472
	jz	.send_packet
261 hidnplayr 473
; wait for timeout
302 hidnplayr 474
	mov	ebx, RTL8139_TX_TIMEOUT
475
	mov	eax, 0x5 ; delay x/100 secs
476
	int	0x40
477
	in	ax, dx
478
	and	ax, (1 shl RTL8139_BIT_TOK) or (1 shl RTL8139_BIT_OWN)
479
	cmp	ax, (1 shl RTL8139_BIT_TOK) or (1 shl RTL8139_BIT_OWN)
480
	jz	.send_packet
261 hidnplayr 481
; chip hung, reset it
302 hidnplayr 482
	call	rtl8139_reset
261 hidnplayr 483
; reset the card
484
.send_packet:
485
; calculate tx_buffer address
302 hidnplayr 486
	pop	ebx
487
	push	esi
488
	mov	eax, MAX_ETH_FRAME_SIZE
489
	mul	dword [curr_tx_desc]
490
	mov	esi, edi
491
	lea	edi, [rtl8139_tx_buff+eax]
492
	mov	eax, edi
493
	cld
261 hidnplayr 494
; copy destination address
302 hidnplayr 495
	movsd
496
	movsw
261 hidnplayr 497
; copy source address
302 hidnplayr 498
	mov	esi, node_addr
499
	movsd
500
	movsw
261 hidnplayr 501
; copy packet type
302 hidnplayr 502
	mov	[edi], bx
503
	add	edi, 2
261 hidnplayr 504
; copy the packet data
302 hidnplayr 505
	pop	esi edx ecx
506
	push	ecx
507
	shr	ecx, 2
508
	rep	movsd
509
	pop	ecx
510
	push	ecx
511
	and	ecx, 3
512
	rep	movsb
261 hidnplayr 513
; set address
302 hidnplayr 514
	add	edx, RTL8139_REG_TSAD0 - RTL8139_REG_TSD0
515
	out	dx, eax
261 hidnplayr 516
; set size and early threshold
302 hidnplayr 517
	pop	eax ; pick up the size
518
	add	eax, ETH_HLEN
519
	cmp	eax, ETH_ZLEN
520
	jnc	.no_pad
521
	mov	eax, ETH_ZLEN
261 hidnplayr 522
.no_pad:
302 hidnplayr 523
	or	eax, (RTL8139_ERTXTH shl RTL8139_BIT_ERTXTH)
524
	add	edx, RTL8139_REG_TSD0 - RTL8139_REG_TSAD0
525
	out	dx, eax
261 hidnplayr 526
; get next descriptor 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, ...
302 hidnplayr 527
	inc	dword [curr_tx_desc]
528
	and	dword [curr_tx_desc], 3
261 hidnplayr 529
.finish:
302 hidnplayr 530
	ret
261 hidnplayr 531
 
532
;***************************************************************************
533
; Function
534
;    rtl8139_poll
535
;
536
; Description
537
;    Polls the ethernet card for a received packet
538
;    Received data, if any, ends up in Ether_buffer
539
; Destroyed register(s)
540
;    eax, edx, ecx
541
;
542
;***************************************************************************
543
rtl8139_poll:
302 hidnplayr 544
	mov	word [eth_rx_data_len], 0
545
	mov	edx, [io_addr]
546
	add	edx, RTL8139_REG_COMMAND
547
	in	al, dx
548
	test	al, (1 shl RTL8139_BIT_BUFE)
549
	jnz	.finish
261 hidnplayr 550
; new packet received copy it from rx_buffer into Ether_buffer
302 hidnplayr 551
	mov	eax, rtl8139_rx_buff
552
	add	eax, [rtl8139_rx_buff_offset]
261 hidnplayr 553
; check if packet is ok
302 hidnplayr 554
	test	byte [eax], (1 shl RTL8139_BIT_ROK)
555
	jz	.reset_rx
261 hidnplayr 556
; packet is ok copy it into the Ether_buffer
302 hidnplayr 557
	movzx	ecx, word [eax+2] ; packet length
558
	sub	ecx, 4 ; don't copy CRC
559
	mov	word [eth_rx_data_len], cx
560
	push	ecx
561
	shr	ecx, 2 ; first copy dword-wise
562
	lea	esi, [eax+4] ; don't copy the packet header
563
	mov	edi, Ether_buffer
564
	cld
565
	rep	movsd ; copy the dwords
566
	pop	ecx
567
	and	ecx, 3
568
	rep	movsb ; copy the rest bytes
261 hidnplayr 569
; update rtl8139_rx_buff_offset
302 hidnplayr 570
	movzx	eax, word [eax+2] ; packet length
571
	add	eax, [rtl8139_rx_buff_offset]
572
	add	eax, 4+3 ; packet header is 4 bytes long + dword alignment
573
	and	eax, not 3 ; dword alignment
574
	cmp	eax, RTL8139_RX_BUFFER_SIZE
575
	jl	.no_wrap
576
	sub	eax, RTL8139_RX_BUFFER_SIZE
261 hidnplayr 577
.no_wrap:
302 hidnplayr 578
	mov	[rtl8139_rx_buff_offset], eax
261 hidnplayr 579
; update CAPR register
302 hidnplayr 580
	sub	eax, 0x10 ; value 0x10 is a constant for CAPR
581
	add	edx, RTL8139_REG_CAPR - RTL8139_REG_COMMAND
582
	out	dx, ax
261 hidnplayr 583
.finish:
584
; clear active interrupt sources
302 hidnplayr 585
	mov	edx, [io_addr]
586
	add	edx, RTL8139_REG_ISR
587
	in	ax, dx
588
	out	dx, ax
589
	ret
261 hidnplayr 590
.reset_rx:
302 hidnplayr 591
	in	al, dx ; read command register
592
	push	eax
593
	and	al, not (1 shl RTL8139_BIT_RE)
594
	out	dx, al
595
	pop	eax
596
	out	dx, al
597
	add	edx, RTL8139_REG_RXCONFIG - RTL8139_REG_COMMAND
598
	mov	ax, RTL8139_RX_CONFIG
599
	out	dx, ax
600
	ret
601
 
602
rtl8139_cable:
603
	pusha
604
	mov	edx, [io_addr]
605
	add	edx, 0x58
606
	in	al,dx
607
	test	al,1 SHL 2
608
	jnz	.notconnected
609
	popa
610
	xor	al,al
611
	inc	al
612
	ret
613
       .notconnected:
614
	popa
615
	xor	al,al
616
	ret