Subversion Repositories Kolibri OS

Rev

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

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