Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
1159 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2004-2008. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;; Realtek 8139 driver for KolibriOS                               ;;
7
;;                                                                 ;;
8
;;    Written by hidnplayr@kolibrios.org                           ;;
9
;;                                                                 ;;
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
 
371
	call	EthRegDev
372
	cmp	eax, -1
373
	je	.destroy
374
 
375
	ret
376
 
377
; If the device was already loaded, find the device number and return it in eax
378
 
379
  .find_devicenum:
1472 hidnplayr 380
	DEBUGF	2,"Trying to find device number of already registered device\n"
1159 hidnplayr 381
	mov	ebx, eax
382
	call	EthStruc2Dev						; This kernel procedure converts a pointer to device struct in ebx
383
									; into a device number in edi
384
	mov	eax, edi						; Application wants it in eax instead
1472 hidnplayr 385
	DEBUGF	2,"Kernel says: %u\n", eax
1159 hidnplayr 386
	ret
387
 
388
; If an error occured, remove all allocated data and exit (returning -1 in eax)
389
 
390
  .destroy:
391
	; todo: reset device into virgin state
392
 
393
  .err:
1472 hidnplayr 394
	stdcall KernelFree, dword [device.rx_buffer]
395
	stdcall KernelFree, dword [device.tx_buffer]
1159 hidnplayr 396
	stdcall KernelFree, ebx
397
 
398
 
399
  .fail:
400
	or	eax, -1
401
	ret
402
 
403
;------------------------------------------------------
404
endp
405
 
406
 
407
;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
408
;;                                                                        ;;
409
;;        Actual Hardware dependent code starts here                      ;;
410
;;                                                                        ;;
411
;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
412
 
413
align 4
414
unload:
415
	; TODO: (in this particular order)
416
	;
417
	; - Stop the device
418
	; - Detach int handler
419
	; - Remove device from local list (RTL8139_LIST)
420
	; - call unregister function in kernel
421
	; - Remove all allocated structures and buffers the card used
422
 
423
	or	eax,-1
424
 
425
ret
426
 
427
 
428
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429
;;
430
;;  probe: enables the device (if it really is RTL8139)
431
;;
432
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
433
 
434
align 4
435
probe:
436
	DEBUGF	2,"Probing rtl8139 device: "
437
 
1472 hidnplayr 438
	make_bus_master [device.pci_bus], [device.pci_dev]
1159 hidnplayr 439
 
440
; get chip version
441
 
1472 hidnplayr 442
	set_io	0
443
	set_io	REG_TXCONFIG + 2
1159 hidnplayr 444
	in	ax , dx
445
	shr	ah , 2
446
	shr	ax , 6
447
	and	al , 01111111b
448
	mov	ecx, HW_VER_ARRAY_SIZE-1
449
  .chip_ver_loop:
450
	cmp	al , [hw_ver_array+ecx]
451
	je	.chip_ver_found
452
	dec	ecx
453
	jns	.chip_ver_loop
454
	xor	cl , cl ; default RTL8139
455
  .chip_ver_found:
1472 hidnplayr 456
	mov	[device.hw_ver_id], cl
1159 hidnplayr 457
 
1178 hidnplayr 458
	shl	ecx, 2
1472 hidnplayr 459
	add	ecx, crosslist
1178 hidnplayr 460
	mov	ecx, [ecx]
1472 hidnplayr 461
	mov	[device.name], ecx
1178 hidnplayr 462
 
1472 hidnplayr 463
	DEBUGF	2,"Chip version: %s\n",ecx
1178 hidnplayr 464
 
1159 hidnplayr 465
; wake up the chip
466
 
1472 hidnplayr 467
	set_io	0
468
	set_io	REG_HLTCLK
1159 hidnplayr 469
	mov	al , 'R' ; run the clock
470
	out	dx , al
471
 
472
; unlock config and BMCR registers
473
 
1472 hidnplayr 474
	set_io	REG_9346CR
1159 hidnplayr 475
	mov	al , (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
476
	out	dx , al
477
 
478
; enable power management
479
 
1472 hidnplayr 480
	set_io	REG_CONFIG1
1159 hidnplayr 481
	in	al , dx
1472 hidnplayr 482
	cmp	[device.hw_ver_id], IDX_RTL8139B
1159 hidnplayr 483
	jl	.old_chip
484
 
485
; set LWAKE pin to active high (default value).
486
; it is for Wake-On-LAN functionality of some motherboards.
487
; this signal is used to inform the motherboard to execute a wake-up process.
488
; only at newer chips.
489
 
490
	or	al , (1 shl BIT_PMEn)
491
	and	al , not (1 shl BIT_LWACT)
492
	out	dx , al
1472 hidnplayr 493
 
494
	set_io	REG_CONFIG4
1159 hidnplayr 495
	in	al , dx
496
	and	al , not (1 shl BIT_LWPTN)
497
	out	dx , al
1472 hidnplayr 498
 
1159 hidnplayr 499
	jmp	.finish_wake_up
500
  .old_chip:
501
 
502
; wake up older chips
503
 
504
	and	al , not ((1 shl BIT_SLEEP) or (1 shl BIT_PWRDWN))
505
	out	dx , al
506
  .finish_wake_up:
507
 
508
; lock config and BMCR registers
509
 
510
	xor	al , al
1472 hidnplayr 511
	set_io	0
512
	set_io	REG_9346CR
1159 hidnplayr 513
	out	dx , al
514
	DEBUGF	2,"done!\n"
515
 
516
 
517
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
518
;;
519
;;   reset: Set up all registers and descriptors, clear some values
520
;;
521
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
522
 
523
reset:
524
	DEBUGF	2,"Resetting rtl8139: "
525
 
526
; attach int handler
527
 
1472 hidnplayr 528
	movzx	eax, [device.irq_line]
1159 hidnplayr 529
	DEBUGF	1,"Attaching int handler to irq %x, ",eax:1
530
	stdcall AttachIntHandler, eax, int_handler, dword 0
531
	test	eax, eax
532
	jnz	@f
533
	DEBUGF	1,"\nCould not attach int handler!\n"
534
;        or      eax, -1
535
;        ret
536
  @@:
537
 
538
; reset chip
539
 
540
	DEBUGF	1,"Resetting chip\n"
1472 hidnplayr 541
	set_io	0
542
	set_io	REG_COMMAND
1159 hidnplayr 543
	mov	al , 1 shl BIT_RST
544
	out	dx , al
545
	mov	cx , 1000		; wait no longer for the reset
546
  .wait_for_reset:
547
	in	al , dx
548
	test	al , 1 shl BIT_RST
549
	jz	.reset_completed	; RST remains 1 during reset
550
	dec	cx
551
	jns	.wait_for_reset
552
  .reset_completed:
553
 
554
; unlock config and BMCR registers
555
 
1472 hidnplayr 556
	set_io	REG_9346CR
1159 hidnplayr 557
	mov	al , (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
558
	out	dx , al
559
 
560
; initialize multicast registers (no filtering)
561
 
562
	mov	eax, 0xffffffff
1472 hidnplayr 563
	set_io	REG_MAR0
1159 hidnplayr 564
	out	dx , eax
1472 hidnplayr 565
	set_io	REG_MAR4
1159 hidnplayr 566
	out	dx , eax
567
 
568
; enable Rx/Tx
569
 
570
	mov	al , (1 shl BIT_RE) or (1 shl BIT_TE)
1472 hidnplayr 571
	set_io	REG_COMMAND
1159 hidnplayr 572
	out	dx , al
573
 
574
; 32k Rxbuffer, unlimited dma burst, no wrapping, no rx threshold
575
; accept broadcast packets, accept physical match packets
576
 
577
	mov	ax , RX_CONFIG
1472 hidnplayr 578
	set_io	REG_RXCONFIG
1159 hidnplayr 579
	out	dx , ax
580
 
1472 hidnplayr 581
 
1159 hidnplayr 582
; 1024 bytes DMA burst, total retries = 16 + 8 * 16 = 144
583
 
584
	mov	eax , (TX_MXDMA shl BIT_TX_MXDMA) or (TXRR shl BIT_TXRR) or BIT_IFG1 or BIT_IFG0
1472 hidnplayr 585
	set_io	REG_TXCONFIG
1159 hidnplayr 586
	out	dx , eax
587
 
588
; enable auto negotiation
589
 
1472 hidnplayr 590
	set_io	REG_BMCR
1159 hidnplayr 591
	in	ax , dx
592
	or	ax , (1 shl BIT_ANE)
593
	out	dx , ax
594
 
595
; set auto negotiation advertisement
596
 
1472 hidnplayr 597
	set_io	REG_ANAR
1159 hidnplayr 598
	in	ax , dx
599
	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)
600
	out	dx , ax
601
 
602
; lock config and BMCR registers
603
 
604
	xor	eax, eax
1472 hidnplayr 605
	set_io	REG_9346CR
1159 hidnplayr 606
	out	dx , al
607
 
608
; init RX/TX pointers
609
 
1472 hidnplayr 610
	mov	[device.rx_data_offset], eax
611
	mov	[device.curr_tx_desc], al
1159 hidnplayr 612
 
1171 hidnplayr 613
; clear packet/byte counters
614
 
1472 hidnplayr 615
	lea	edi, [device.bytes_tx]
1171 hidnplayr 616
	mov	ecx, 6
617
	rep	stosd
618
 
1159 hidnplayr 619
; clear missing packet counter
620
 
1472 hidnplayr 621
	set_io	REG_MPC
1159 hidnplayr 622
	out	dx , eax
623
 
624
; Set up the 4 Txbuffer descriptors
625
 
1472 hidnplayr 626
	set_io	REG_TSAD0
627
	mov	eax, [device.tx_buffer]
1159 hidnplayr 628
	mov	ecx, 4
629
  .loop:
630
	push	eax
631
	call	GetPgAddr
1472 hidnplayr 632
	DEBUGF	1,"Desc: %x ", eax
1159 hidnplayr 633
	out	dx , eax
634
	add	dx , 4
635
	pop	eax
636
	add	eax, TX_BUF_SIZE
637
	loop	.loop
638
 
1472 hidnplayr 639
; set RxBuffer address, init RX buffer offset
1159 hidnplayr 640
 
1472 hidnplayr 641
	mov	eax, [device.rx_buffer]
1159 hidnplayr 642
	call	GetPgAddr
1472 hidnplayr 643
	set_io	0
644
	set_io	REG_RBSTART
1159 hidnplayr 645
	out	dx , eax
646
 
647
; enable interrupts
648
 
649
	mov	eax, INTERRUPT_MASK
1472 hidnplayr 650
	set_io	REG_IMR
1159 hidnplayr 651
	out	dx , ax
652
 
653
; Read MAC address
654
 
655
	call	read_mac
656
 
657
; Indicate that we have successfully reset the card
658
 
659
	DEBUGF	2,"Done!\n"
660
	xor	eax, eax
661
 
662
	ret
663
 
664
 
665
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
666
;;                                         ;;
667
;; Transmit                                ;;
668
;;                                         ;;
1254 hidnplayr 669
;; In: buffer pointer in [esp+4]           ;;
670
;;     size of buffer in [esp+8]           ;;
1159 hidnplayr 671
;;     pointer to device structure in ebx  ;;
672
;;                                         ;;
673
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
674
 
675
align 4
676
transmit:
1254 hidnplayr 677
	DEBUGF	1,"Transmitting packet, buffer:%x, size:%u\n",[esp+4],[esp+8]
678
	mov	eax, [esp+4]
1159 hidnplayr 679
	DEBUGF	1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
680
	[eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
681
	[eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
682
	[eax+13]:2,[eax+12]:2
683
 
1254 hidnplayr 684
	cmp	dword [esp+8], MAX_ETH_FRAME_SIZE
1159 hidnplayr 685
	jg	.finish 			; packet is too long
1254 hidnplayr 686
	cmp	dword [esp+8], 60
1159 hidnplayr 687
	jl	.finish 			; packet is too short
688
 
689
; check descriptor
1472 hidnplayr 690
	DEBUGF	1,"Checking descriptor, "
691
	movzx	ecx, [device.curr_tx_desc]
692
	mov	edx, [device.io_addr]
1159 hidnplayr 693
	lea	edx, [edx+ecx*4+REG_TSD0]
694
	in	ax, dx
695
	test	ax, 0x1fff ; or no size given
696
	jz	.send_packet
697
	and	ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
698
	cmp	ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
699
	jz	.send_packet
700
; wait for timeout
1472 hidnplayr 701
	DEBUGF	1,"Waiting for timeout, "
1159 hidnplayr 702
 
703
	push	edx ebx 			 ; TODO : rtl8139 internal timer should be used instead
704
	stdcall Sleep, TX_TIMEOUT		 ; ? What registers does this destroy ?
705
	pop	ebx edx
706
 
707
	in	ax, dx
708
	and	ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
709
	cmp	ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
710
	jz	.send_packet			 ; if chip hung, reset it
711
	push	dx
712
	call	reset				 ; reset the card
713
	pop	dx
714
.send_packet:
1472 hidnplayr 715
	DEBUGF	1,"Sending packet, "
1159 hidnplayr 716
 
717
	push	edx
1472 hidnplayr 718
	movzx	eax, [device.curr_tx_desc]   ; calculate the current tx_buffer address
1159 hidnplayr 719
	mov	edx, TX_BUF_SIZE ;MAX_ETH_FRAME_SIZE          ;
720
	mul	edx				 ;
1472 hidnplayr 721
	mov	edi, [device.tx_buffer]      ;
1159 hidnplayr 722
	add	edi, eax			 ; Store it in edi
723
	pop	edx
724
 
1254 hidnplayr 725
	mov	esi, [esp+4]			 ; Copy data to that address
726
	mov	ecx, [esp+8]			 ;
1159 hidnplayr 727
	shr	ecx, 2				 ;
728
	rep	movsd				 ;
1254 hidnplayr 729
	mov	ecx, [esp+8]			 ;
1159 hidnplayr 730
	and	ecx, 3				 ;
731
	rep	movsb				 ;
732
 
1472 hidnplayr 733
	inc	[device.packets_tx]	     ;
1254 hidnplayr 734
	mov	eax, [esp+8]			 ; Get packet size in eax
1159 hidnplayr 735
 
1472 hidnplayr 736
	add	dword [device.bytes_tx], eax
737
	adc	dword [device.bytes_tx + 4], 0
1159 hidnplayr 738
 
739
;        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)
740
	out	dx , eax			 ;
741
 
742
; get next descriptor 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, ...
1472 hidnplayr 743
	inc	[device.curr_tx_desc]
744
	and	[device.curr_tx_desc], 3
1159 hidnplayr 745
 
1472 hidnplayr 746
	DEBUGF	1," - Packet Sent! "
1159 hidnplayr 747
.finish:
1472 hidnplayr 748
	DEBUGF	1," - Done!\n"
1159 hidnplayr 749
	ret
750
 
751
 
752
 
753
 
754
 
755
;;;;;;;;;;;;;;;;;;;;;;;
756
;;                   ;;
757
;; Interrupt handler ;;
758
;;                   ;;
759
;;;;;;;;;;;;;;;;;;;;;;;
760
 
761
align 4
762
int_handler:
763
 
764
	DEBUGF	1,"IRQ %x ",eax:2		    ; no, you cant replace 'eax:2' with 'al', this must be a bug in FDO
765
 
766
; find pointer of device wich made IRQ occur
767
 
768
	mov	esi, RTL8139_LIST
769
	mov	ecx, [RTL8139_DEV]
1486 hidnplayr 770
	test	ecx, ecx
771
	jz	.fail
1159 hidnplayr 772
.nextdevice:
773
	mov	ebx, dword [esi]
774
 
1472 hidnplayr 775
	set_io	0
776
	set_io	REG_ISR
1159 hidnplayr 777
	in	ax , dx
778
	out	dx , ax 			    ; send it back to ACK
779
 
780
	add	esi, 4
781
 
782
	test	ax , ax
783
	jnz	.got_it
784
 
1486 hidnplayr 785
	dec	ecx
786
	jnz	.nextdevice
1159 hidnplayr 787
 
788
	ret					    ; If no device was found, abort (The irq was probably for a device, not registered to this driver)
789
 
790
  .got_it:
791
 
792
; looks like we've found it!
793
 
794
; Lets found out why the irq occured then..
795
 
796
;----------------------------------------------------
797
; Received packet ok?
798
 
799
	test	ax, ISR_ROK
800
	jz	@f
801
	push	ax
802
 
803
  .receive:
1472 hidnplayr 804
	set_io	0
805
	set_io	REG_COMMAND
806
	in	al , dx
1159 hidnplayr 807
	test	al , BUFE			    ; test if RX buffer is empty
808
	jnz	.finish 			    ;
809
 
1472 hidnplayr 810
	DEBUGF	1,"RX: "
1159 hidnplayr 811
 
1472 hidnplayr 812
	mov	eax, [device.rx_buffer]
813
	add	eax, [device.rx_data_offset]
1159 hidnplayr 814
	test	byte [eax], (1 shl BIT_ROK)	    ; check if packet is ok
815
	jz	.reset_rx
1472 hidnplayr 816
 
817
; packet is ok, copy it
1159 hidnplayr 818
	movzx	ecx, word [eax+2]		    ; packet length
1472 hidnplayr 819
 
820
; Update stats
821
	add	dword [device.bytes_rx], ecx
822
	adc	dword [device.bytes_rx + 4], 0
823
	inc	dword [device.packets_rx]
824
 
1159 hidnplayr 825
	sub	ecx, 4				    ; don't copy CRC
1472 hidnplayr 826
 
1159 hidnplayr 827
	DEBUGF	1,"Received %u bytes\n", ecx
828
 
829
	push	ebx eax ecx
830
	stdcall KernelAlloc, ecx		    ; Allocate a buffer to put packet into
831
	pop	ecx
832
	test	eax, eax			    ; Test if we allocated succesfully
1472 hidnplayr 833
	jz	.abort
1159 hidnplayr 834
 
1472 hidnplayr 835
 
836
	mov	edi, eax			    ; Where we will copy too
837
 
838
	mov	esi, [esp]			    ; The buffer we will copy from
1159 hidnplayr 839
	add	esi, 4				    ; Dont copy CRC
840
 
841
	push	dword .abort			    ; Kernel will return to this address after EthReceiver
842
	push	ecx edi 			    ; Save buffer pointer and size, to pass to kernel
843
 
1472 hidnplayr 844
  .copy:
845
	shr	ecx, 1
846
	jnc	.nb
847
	movsb
848
  .nb:
849
	shr	ecx, 1
850
	jnc	.nw
851
	movsw
852
  .nw:
853
	jz	.nd
854
	rep	movsd
855
  .nd:
1159 hidnplayr 856
 
857
	jmp	EthReceiver			    ; Send it to kernel
858
 
1472 hidnplayr 859
 
1159 hidnplayr 860
  .abort:
861
	pop	eax ebx
862
						    ; update eth_data_start_offset
863
	movzx	eax, word [eax+2]		    ; packet length
1472 hidnplayr 864
	add	eax, [device.rx_data_offset]
1159 hidnplayr 865
	add	eax, 4+3			    ; packet header is 4 bytes long + dword alignment
866
	and	eax, not 3			    ; dword alignment
1472 hidnplayr 867
 
1159 hidnplayr 868
	cmp	eax, RX_BUFFER_SIZE
869
	jl	.no_wrap
1472 hidnplayr 870
	DEBUGF	2,"Wrapping"
1159 hidnplayr 871
	sub	eax, RX_BUFFER_SIZE
872
  .no_wrap:
1472 hidnplayr 873
	mov	[device.rx_data_offset], eax
874
	DEBUGF	1,"New RX ptr: %d ", eax
1159 hidnplayr 875
 
1472 hidnplayr 876
	set_io	0
877
	set_io	REG_CAPR			    ; update 'Current Address of Packet Read register'
1159 hidnplayr 878
	sub	eax, 0x10			    ; value 0x10 is a constant for CAPR
879
	out	dx , ax
880
 
881
	jmp	.receive			    ; check for multiple packets
882
 
883
  .reset_rx:
884
	test	byte [eax], (1 shl BIT_CRC)
885
	jz	.no_crc_error
886
	DEBUGF	2,"\nCRC error!\n"
887
 
888
  .no_crc_error:
889
	test	byte [eax], (1 shl BIT_FAE)
890
	jz	.no_fae_error
891
	DEBUGF	1,"\nFrame alignment error!\n"
892
 
893
  .no_fae_error:
894
	DEBUGF	1,"Reset RX\n"
895
	in	al , dx 			    ; read command register
896
	push	ax
897
 
898
	and	al , not (1 shl BIT_RE) 	    ; Clear the RE bit
899
	out	dx , al
900
 
901
	pop	ax
902
	out	dx , al 			    ; write original command back
903
 
904
	add	edx, REG_RXCONFIG - REG_COMMAND     ; Restore RX configuration
905
	mov	ax , RX_CONFIG
906
	out	dx , ax
907
 
908
  .finish:
909
	pop	ax
910
 
911
;----------------------------------------------------
912
; Transmit error ?
913
 
914
  @@:
915
	test	ax, ISR_TER
916
	jz	@f
917
 
918
	push	ax
1472 hidnplayr 919
	cmp	[device.curr_tx_desc], 4
1159 hidnplayr 920
	jz	.notxd
921
 
1472 hidnplayr 922
	set_io	0
923
	movzx	ecx, [device.curr_tx_desc]
1159 hidnplayr 924
	lea	edx, [edx+ecx*4+REG_TSD0]
925
	in	eax, dx
926
 
927
  .notxd:
928
	test	eax, TSR_TUN
929
	jz	.nobun
1472 hidnplayr 930
	DEBUGF	2, "TX: FIFO Buffer underrun!\n"
1159 hidnplayr 931
 
932
  .nobun:
933
	test	eax, TSR_OWC
934
	jz	.noowc
1472 hidnplayr 935
	DEBUGF	2, "TX: OWC!\n"
1159 hidnplayr 936
 
937
  .noowc:
938
	test	eax, TSR_TABT
939
	jz	.notabt
1472 hidnplayr 940
	DEBUGF	2, "TX: TABT!\n"
1159 hidnplayr 941
 
942
  .notabt:
943
	test	eax, TSR_CRS
944
	jz	.nocsl
1472 hidnplayr 945
	DEBUGF	2, "TX: Carrier Sense Lost!\n"
1159 hidnplayr 946
 
947
  .nocsl:
948
;                test    eax, TSR_OWN or TSR_TOK
949
;                jz      .nofd
950
;                DEBUGF  1, "TX: Transmit OK (desc: %u)\n", ecx
951
;
952
;               .nofd:
953
	pop	ax
954
 
955
;----------------------------------------------------
956
; Transmit ok ?
957
 
958
  @@:
959
	test	ax, ISR_TOK
960
	jz	@f
961
 
1472 hidnplayr 962
	DEBUGF	1, "TX: Transmit OK (desc: %u)\n", [device.curr_tx_desc]:1
1159 hidnplayr 963
 
964
;----------------------------------------------------
965
; Rx buffer overflow ?
966
 
967
  @@:
968
	test	ax, ISR_RXOVW
969
	jz	@f
970
 
971
	push	ax
1472 hidnplayr 972
	DEBUGF	2,"RX-buffer overflow!\n"
1159 hidnplayr 973
 
1472 hidnplayr 974
	mov	edx, [device.io_addr]
1159 hidnplayr 975
	add	edx, REG_ISR
976
	mov	ax , ISR_FIFOOVW or ISR_RXOVW
977
	out	dx , ax
978
	pop	ax
979
 
980
;----------------------------------------------------
981
; Packet underrun? ?
982
 
983
 
984
  @@:
985
	test	ax, ISR_PUN
986
	jz	@f
987
 
1472 hidnplayr 988
	DEBUGF	2,"Packet underrun!\n"
1159 hidnplayr 989
 
990
;----------------------------------------------------
991
; Receive FIFO overflow ?
992
 
993
  @@:
994
	test	ax, ISR_FIFOOVW
995
	jz	@f
996
 
997
	push	ax
998
	DEBUGF	2,"RX fifo overflox!\n"
999
 
1472 hidnplayr 1000
	mov	edx, [device.io_addr]
1159 hidnplayr 1001
	add	edx, REG_ISR
1002
	mov	ax , ISR_FIFOOVW or ISR_RXOVW
1003
	out	dx , ax
1004
	pop	ax
1005
 
1006
;----------------------------------------------------
1007
; Something about Cable changed ?
1008
 
1009
  @@:
1010
	test	ax, ISR_LENCHG
1011
	jz	.fail
1012
 
1013
	DEBUGF	2,"Cable changed!\n"
1014
	call	cable
1015
 
1016
; If none of the above events happened, just exit clearing int
1017
 
1018
  .fail:
1019
 
1472 hidnplayr 1020
	DEBUGF	1,"\n"
1159 hidnplayr 1021
	ret
1022
 
1023
 
1024
 
1025
 
1026
;;;;;;;;;;;;;;;;;;;;;;;;;
1027
;;                     ;;
1028
;; Update Cable status ;;
1029
;;                     ;;
1030
;;;;;;;;;;;;;;;;;;;;;;;;;
1031
 
1032
align 4
1033
cable:
1034
	DEBUGF	1,"Checking Cable status: "
1035
 
1472 hidnplayr 1036
	mov	edx, dword [device.io_addr]
1159 hidnplayr 1037
	add	edx, REG_MSR
1038
	in	al , dx
1039
 
1040
;        test    al , 1 SHL 2     ; 0 = link ok 1 = link fail
1041
;        jnz     .notconnected
1042
 
1043
;        test    al , 1 SHL 3     ; 0 = 100 Mbps 1 = 10 Mbps
1044
;        jnz     .10mbps
1045
 
1046
	shr	al, 2
1047
	and	al, 3
1048
 
1472 hidnplayr 1049
	mov	byte [device.mode+3], al
1159 hidnplayr 1050
	DEBUGF	1,"Done!\n"
1051
ret
1052
 
1053
 
1054
 
1055
;;;;;;;;;;;;;;;;;;;;;;;
1056
;;                   ;;
1057
;; Write MAC address ;;
1058
;;                   ;;
1059
;;;;;;;;;;;;;;;;;;;;;;;
1060
 
1061
align 4
1062
write_mac:	; in: mac pushed onto stack (as 3 words)
1063
 
1472 hidnplayr 1064
	DEBUGF	2,"Writing MAC: "
1159 hidnplayr 1065
 
1066
; disable all in command registers
1067
 
1472 hidnplayr 1068
	set_io	0
1069
	set_io	REG_9346CR
1159 hidnplayr 1070
	xor	eax, eax
1071
	out	dx , al
1072
 
1472 hidnplayr 1073
	set_io	REG_IMR
1159 hidnplayr 1074
	xor	eax, eax
1075
	out	dx , ax
1076
 
1472 hidnplayr 1077
	set_io	REG_ISR
1159 hidnplayr 1078
	mov	eax, -1
1079
	out	dx , ax
1080
 
1081
; enable writing
1082
 
1472 hidnplayr 1083
	set_io	REG_9346CR
1159 hidnplayr 1084
	mov	eax, REG_9346CR_WE
1085
	out	dx , al
1086
 
1087
 ; write the mac ...
1088
 
1472 hidnplayr 1089
	set_io	REG_IDR0
1159 hidnplayr 1090
	pop	eax
1091
	out	dx , eax
1092
 
1472 hidnplayr 1093
	set_io	REG_IDR0+4
1159 hidnplayr 1094
	xor	eax, eax
1095
	pop	ax
1096
	out	dx , eax
1097
 
1098
; disable writing
1099
 
1472 hidnplayr 1100
	set_io	REG_9346CR
1159 hidnplayr 1101
	xor	eax, eax
1102
	out	dx , al
1103
 
1472 hidnplayr 1104
	DEBUGF	2,"ok!\n"
1159 hidnplayr 1105
 
1106
; Notice this procedure does not ret, but continues to read_mac instead.
1107
 
1108
 
1109
;;;;;;;;;;;;;;;;;;;;;;
1110
;;                  ;;
1111
;; Read MAC address ;;
1112
;;                  ;;
1113
;;;;;;;;;;;;;;;;;;;;;;
1114
 
1115
read_mac:
1472 hidnplayr 1116
	DEBUGF	2,"Reading MAC: "
1159 hidnplayr 1117
 
1472 hidnplayr 1118
	set_io	0
1119
	lea	edi, [device.mac]
1177 clevermous 1120
	in	eax, dx
1159 hidnplayr 1121
	stosd
1177 clevermous 1122
	add	edx, 4
1173 clevermous 1123
	in	ax, dx
1159 hidnplayr 1124
	stosw
1125
 
1472 hidnplayr 1126
	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 1127
 
1159 hidnplayr 1128
	ret
1129
 
1130
 
1131
 
1132
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1133
;;                                                                      ;;
1134
;; Read eeprom (type 93c46 and 93c56)                                   ;;
1135
;;                                                                      ;;
1136
;; In: word to be read in al (6bit in case of 93c46 and 8bit otherwise) ;;
1137
;;     pointer to device structure in ebx                               ;;
1138
;;                                                                      ;;
1139
;; OUT: word read in ax                                                 ;;
1140
;;                                                                      ;;
1141
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1142
 
1143
align 4
1144
read_eeprom:
1145
	DEBUGF	2,"Reading eeprom, "
1146
 
1472 hidnplayr 1147
	set_io	0
1159 hidnplayr 1148
	push	ebx
1149
	movzx	ebx, al
1472 hidnplayr 1150
	set_io	REG_RXCONFIG
1159 hidnplayr 1151
	in	al, dx
1152
	test	al, (1 shl BIT_9356SEL)
1153
	jz	.type_93c46
1154
;       and     bl, 01111111b ; don't care first bit
1155
	or	bx, EE_93C56_READ_CMD		; it contains start bit
1156
	mov	cx, EE_93C56_CMD_LENGTH-1	; cmd_loop counter
1157
	jmp	.read_eeprom
1158
.type_93c46:
1159
	and	bl, 00111111b
1160
	or	bx, EE_93C46_READ_CMD		; it contains start bit
1161
	mov	cx, EE_93C46_CMD_LENGTH-1	; cmd_loop counter
1162
.read_eeprom:
1472 hidnplayr 1163
	set_io	REG_9346CR
1159 hidnplayr 1164
;       mov     al, (1 shl BIT_93C46_EEM1)
1165
;       out     dx, al
1166
	mov	al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS) ; wake up the eeprom
1167
	out	dx, al
1168
.cmd_loop:
1169
	mov	al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
1170
	bt	bx, cx
1171
	jnc	.zero_bit
1172
	or	al, (1 shl BIT_93C46_EEDI)
1173
.zero_bit:
1174
	out	dx, al
1175
;       push    eax
1176
;       in      eax, dx ; eeprom delay
1177
;       pop     eax
1178
	or	al, (1 shl BIT_93C46_EESK)
1179
	out	dx, al
1180
;       in      eax, dx ; eeprom delay
1181
	dec	cx
1182
	jns	.cmd_loop
1183
;       in      eax, dx ; eeprom delay
1184
	mov	al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
1185
	out	dx, al
1186
	mov	cl, 0xf
1187
.read_loop:
1188
	shl	ebx, 1
1189
	mov	al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS) or (1 shl BIT_93C46_EESK)
1190
	out	dx, al
1191
;       in      eax, dx ; eeprom delay
1192
	in	al, dx
1193
	and	al, (1 shl BIT_93C46_EEDO)
1194
	jz	.dont_set
1195
	inc	ebx
1196
.dont_set:
1197
	mov	al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
1198
	out	dx, al
1199
;       in      eax, dx ; eeprom delay
1200
	dec	cl
1201
	jns	.read_loop
1202
	xor	al, al
1203
	out	dx, al
1204
	mov	ax, bx
1205
	pop	ebx
1206
 
1207
	ret
1208
 
1209
 
1210
; End of code
1211
 
1212
align 4 					; Place all initialised data here
1213
 
1214
RTL8139_DEV   dd 0
1215
version       dd (5 shl 16) or (API_VERSION and 0xFFFF)
1216
my_service    db 'RTL8139',0			; max 16 chars include zero
1217
 
1178 hidnplayr 1218
device_1      db 'Realtek 8139',0
1219
device_2      db 'Realtek 8139A',0
1220
device_3      db 'Realtek 8139B',0
1221
device_4      db 'Realtek 8139C',0
1222
device_5      db 'Realtek 8100',0
1223
device_6      db 'Realtek 8139D',0
1224
device_7      db 'Realtek 8139CP',0
1225
device_8      db 'Realtek 8101',0
1226
 
1472 hidnplayr 1227
crosslist     dd device_1
1228
	      dd device_2
1229
	      dd device_3
1230
	      dd device_4
1231
	      dd device_5
1232
	      dd device_6
1233
	      dd device_7
1234
	      dd device_8
1178 hidnplayr 1235
 
1159 hidnplayr 1236
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
1237
	      db VER_RTL8139A
1238
	      db VER_RTL8139B
1239
	      db VER_RTL8139C
1240
	      db VER_RTL8100
1241
	      db VER_RTL8139D
1242
	      db VER_RTL8139CP
1243
	      db VER_RTL8101
1244
 
1245
HW_VER_ARRAY_SIZE = $-hw_ver_array
1246
 
1247
include_debug_strings				; All data wich FDO uses will be included here
1248
 
1249
section '.data' data readable writable align 16 ; place all uninitialized data place here
1250
 
1251
RTL8139_LIST rd MAX_RTL8139			; This list contains all pointers to device structures the driver is handling
1252