Subversion Repositories Kolibri OS

Rev

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