Subversion Repositories Kolibri OS

Rev

Rev 1177 | 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
1178 hidnplayr 365
	mov	dword [ebx+device.name], my_service
1159 hidnplayr 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
 
1178 hidnplayr 558
	shl	ecx, 2
559
	add	ecx, name_crosslist
560
	mov	ecx, [ecx]
561
	mov	dword [ebx+device.name], ecx
562
 
563
	DEBUGF	1,"Chip version: %s\n",ecx
564
 
1159 hidnplayr 565
; wake up the chip
566
 
567
	mov	edx, [ebx+device.io_addr]
568
	add	edx, REG_HLTCLK
569
	mov	al , 'R' ; run the clock
570
	out	dx , al
571
 
572
; unlock config and BMCR registers
573
 
574
	add	edx, REG_9346CR - REG_HLTCLK
575
	mov	al , (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
576
	out	dx , al
577
 
578
; enable power management
579
 
580
	add	edx, REG_CONFIG1 - REG_9346CR
581
	in	al , dx
582
	cmp	byte [ebx+device.hw_ver_id], IDX_RTL8139B
583
	jl	.old_chip
584
 
585
; set LWAKE pin to active high (default value).
586
; it is for Wake-On-LAN functionality of some motherboards.
587
; this signal is used to inform the motherboard to execute a wake-up process.
588
; only at newer chips.
589
 
590
	or	al , (1 shl BIT_PMEn)
591
	and	al , not (1 shl BIT_LWACT)
592
	out	dx , al
593
	add	edx, REG_CONFIG4 - REG_CONFIG1
594
	in	al , dx
595
	and	al , not (1 shl BIT_LWPTN)
596
	out	dx , al
597
	jmp	.finish_wake_up
598
  .old_chip:
599
 
600
; wake up older chips
601
 
602
	and	al , not ((1 shl BIT_SLEEP) or (1 shl BIT_PWRDWN))
603
	out	dx , al
604
  .finish_wake_up:
605
 
606
; lock config and BMCR registers
607
 
608
	xor	al , al
609
	mov	edx, [ebx+device.io_addr]
610
	add	edx, REG_9346CR
611
	out	dx , al
612
	DEBUGF	2,"done!\n"
613
 
614
 
615
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
616
;;
617
;;   reset: Set up all registers and descriptors, clear some values
618
;;
619
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
620
 
621
reset:
622
	DEBUGF	2,"Resetting rtl8139: "
623
 
624
; attach int handler
625
 
626
	movzx	eax, [ebx+device.irq_line]
627
	DEBUGF	1,"Attaching int handler to irq %x, ",eax:1
628
	stdcall AttachIntHandler, eax, int_handler, dword 0
629
	test	eax, eax
630
	jnz	@f
631
	DEBUGF	1,"\nCould not attach int handler!\n"
632
;        or      eax, -1
633
;        ret
634
  @@:
635
 
636
; reset chip
637
 
638
	DEBUGF	1,"Resetting chip\n"
639
	mov	edx, [ebx+device.io_addr]
640
	add	edx, REG_COMMAND
641
	mov	al , 1 shl BIT_RST
642
	out	dx , al
643
	mov	cx , 1000		; wait no longer for the reset
644
  .wait_for_reset:
645
	in	al , dx
646
	test	al , 1 shl BIT_RST
647
	jz	.reset_completed	; RST remains 1 during reset
648
	dec	cx
649
	jns	.wait_for_reset
650
  .reset_completed:
651
 
652
; unlock config and BMCR registers
653
 
654
	mov	edx, [ebx+device.io_addr]
655
	add	edx, REG_9346CR
656
	mov	al , (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
657
	out	dx , al
658
 
659
; initialize multicast registers (no filtering)
660
 
661
	mov	eax, 0xffffffff
662
	add	edx, REG_MAR0 - REG_9346CR
663
	out	dx , eax
664
	add	edx, REG_MAR4 - REG_MAR0
665
	out	dx , eax
666
 
667
; enable Rx/Tx
668
 
669
	mov	al , (1 shl BIT_RE) or (1 shl BIT_TE)
670
	add	edx, REG_COMMAND - REG_MAR4
671
	out	dx , al
672
 
673
; 32k Rxbuffer, unlimited dma burst, no wrapping, no rx threshold
674
; accept broadcast packets, accept physical match packets
675
 
676
	mov	ax , RX_CONFIG
677
	add	edx, REG_RXCONFIG - REG_COMMAND
678
	out	dx , ax
679
 
680
; 1024 bytes DMA burst, total retries = 16 + 8 * 16 = 144
681
 
682
	mov	eax , (TX_MXDMA shl BIT_TX_MXDMA) or (TXRR shl BIT_TXRR) or BIT_IFG1 or BIT_IFG0
683
	add	edx, REG_TXCONFIG - REG_RXCONFIG
684
	out	dx , eax
685
 
686
; enable auto negotiation
687
 
688
	add	edx, REG_BMCR - REG_TXCONFIG
689
	in	ax , dx
690
	or	ax , (1 shl BIT_ANE)
691
	out	dx , ax
692
 
693
; set auto negotiation advertisement
694
 
695
	add	edx, REG_ANAR - REG_BMCR
696
	in	ax , dx
697
	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)
698
	out	dx , ax
699
 
700
; lock config and BMCR registers
701
 
702
	xor	eax, eax
703
	add	edx, REG_9346CR - REG_ANAR
704
	out	dx , al
705
 
706
; init RX/TX pointers
707
 
708
	mov	[ebx+device.rx_data_offset], eax
709
	mov	[ebx+device.curr_tx_desc], al
710
 
1171 hidnplayr 711
; clear packet/byte counters
712
 
713
	lea	edi, [ebx+device.bytes_tx] ; TODO: check if destroying edi, ecx doesnt harm anything
714
	mov	ecx, 6
715
	rep	stosd
716
 
1159 hidnplayr 717
; clear missing packet counter
718
 
719
	add	edx, REG_MPC - REG_9346CR
720
	out	dx , eax
721
 
722
; Set up the 4 Txbuffer descriptors
723
 
724
	add	edx, REG_TSAD0 - REG_MPC
725
	mov	eax, [ebx+device.tx_buffer]
726
	mov	ecx, 4
727
  .loop:
728
	push	eax
729
	call	GetPgAddr
730
	DEBUGF	2,"Desc: %x ", eax
731
	out	dx , eax
732
	add	dx , 4
733
	pop	eax
734
	add	eax, TX_BUF_SIZE
735
	loop	.loop
736
 
737
; set RxBuffer address, init RX buffer offset, init TX ring
738
 
739
	mov	eax, [ebx+device.rx_buffer]
740
	call	GetPgAddr
741
	mov	edx, [ebx+device.io_addr]
742
	add	edx, REG_RBSTART
743
	out	dx , eax
744
 
745
; enable interrupts
746
 
747
	mov	eax, INTERRUPT_MASK
748
	add	edx, REG_IMR - REG_RBSTART
749
	out	dx , ax
750
 
751
; Read MAC address
752
 
753
	call	read_mac
754
 
755
; Indicate that we have successfully reset the card
756
 
757
	DEBUGF	2,"Done!\n"
758
	xor	eax, eax
759
 
760
	ret
761
 
762
 
763
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
764
;;                                         ;;
765
;; Transmit                                ;;
766
;;                                         ;;
767
;; In: buffer pointer in [esp]             ;;
768
;;     size of buffer in [esp+4]           ;;
769
;;     pointer to device structure in ebx  ;;
770
;;                                         ;;
771
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
772
 
773
align 4
774
transmit:
775
	DEBUGF	1,"Transmitting packet, buffer:%x, size:%u\n",[esp],[esp+4]
776
	mov	eax, [esp]
777
	DEBUGF	1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
778
	[eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
779
	[eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
780
	[eax+13]:2,[eax+12]:2
781
 
782
	cmp	dword [esp+4], MAX_ETH_FRAME_SIZE
783
	jg	.finish 			; packet is too long
784
	cmp	dword [esp+4], 60
785
	jl	.finish 			; packet is too short
786
 
787
; check descriptor
788
;        DEBUGF  1,"Checking descriptor, "
789
	movzx	ecx, [ebx+device.curr_tx_desc]
790
	mov	edx, [ebx+device.io_addr]
791
	lea	edx, [edx+ecx*4+REG_TSD0]
792
	in	ax, dx
793
	test	ax, 0x1fff ; or no size given
794
	jz	.send_packet
795
	and	ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
796
	cmp	ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
797
	jz	.send_packet
798
; wait for timeout
799
;        DEBUGF  1,"Waiting for timeout, "
800
 
801
	push	edx ebx 			 ; TODO : rtl8139 internal timer should be used instead
802
	stdcall Sleep, TX_TIMEOUT		 ; ? What registers does this destroy ?
803
	pop	ebx edx
804
 
805
	in	ax, dx
806
	and	ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
807
	cmp	ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
808
	jz	.send_packet			 ; if chip hung, reset it
809
	push	dx
810
	call	reset				 ; reset the card
811
	pop	dx
812
.send_packet:
813
;        DEBUGF  1,"Sending packet, "
814
 
815
	push	edx
816
	movzx	eax, [ebx+device.curr_tx_desc]	 ; calculate the current tx_buffer address
817
	mov	edx, TX_BUF_SIZE ;MAX_ETH_FRAME_SIZE          ;
818
	mul	edx				 ;
819
	mov	edi, [ebx+device.tx_buffer]	 ;
820
	add	edi, eax			 ; Store it in edi
821
	pop	edx
822
 
823
	mov	esi, [esp]			 ; Copy data to that address
824
	mov	ecx, [esp+4]			 ;
825
	shr	ecx, 2				 ;
826
	rep	movsd				 ;
827
	mov	ecx, [esp+4]			 ;
828
	and	ecx, 3				 ;
829
	rep	movsb				 ;
830
 
831
	inc	[ebx+device.packets_tx] 	 ;
832
	mov	eax, [esp+4]			 ; Get packet size in eax
833
 
1174 hidnplayr 834
	add	dword [ebx + device.bytes_tx], eax
835
	adc	dword [ebx + device.bytes_tx + 4], 0
1159 hidnplayr 836
 
837
;        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)
838
	out	dx , eax			 ;
839
 
840
; get next descriptor 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, ...
841
	inc	[ebx+device.curr_tx_desc]
842
	and	[ebx+device.curr_tx_desc], 3
843
 
844
	DEBUGF	2," - Packet Sent! "
845
.finish:
846
	DEBUGF	2," - Done!\n"
847
	call	KernelFree
848
	add	esp, 4 ; pop (balance stack)
849
 
850
	ret
851
 
852
 
853
 
854
 
855
 
856
;;;;;;;;;;;;;;;;;;;;;;;
857
;;                   ;;
858
;; Interrupt handler ;;
859
;;                   ;;
860
;;;;;;;;;;;;;;;;;;;;;;;
861
 
862
align 4
863
int_handler:
864
 
865
	DEBUGF	1,"IRQ %x ",eax:2		    ; no, you cant replace 'eax:2' with 'al', this must be a bug in FDO
866
 
867
; find pointer of device wich made IRQ occur
868
 
869
	mov	esi, RTL8139_LIST
870
	mov	ecx, [RTL8139_DEV]
871
.nextdevice:
872
	mov	ebx, dword [esi]
873
 
874
	mov	edx, dword [ebx+device.io_addr]     ; get IRQ reason
875
	add	edx, REG_ISR
876
	in	ax , dx
877
	out	dx , ax 			    ; send it back to ACK
878
 
879
	add	esi, 4
880
 
881
	test	ax , ax
882
	jnz	.got_it
883
 
884
	loop	.nextdevice
885
 
886
	ret					    ; If no device was found, abort (The irq was probably for a device, not registered to this driver)
887
 
888
  .got_it:
889
 
890
; looks like we've found it!
891
 
892
; Lets found out why the irq occured then..
893
 
894
;----------------------------------------------------
895
; Received packet ok?
896
 
897
	test	ax, ISR_ROK
898
	jz	@f
899
	push	ax
900
 
901
  .receive:
902
	mov	edx, dword [ebx+device.io_addr]     ; get IRQ reason
903
	add	edx, REG_COMMAND		    ;
904
	in	al , dx 			    ;
905
	test	al , BUFE			    ; test if RX buffer is empty
906
	jnz	.finish 			    ;
907
 
908
	DEBUGF	2,"RX: "
909
 
910
	mov	eax, dword [ebx+device.rx_buffer]
911
	add	eax, dword [ebx+device.rx_data_offset]
912
	test	byte [eax], (1 shl BIT_ROK)	    ; check if packet is ok
913
	jz	.reset_rx
914
						    ; packet is ok, copy it
915
	movzx	ecx, word [eax+2]		    ; packet length
1174 hidnplayr 916
	add	dword [ebx + device.bytes_rx], ecx  ; Update stats
917
	adc	dword [ebx + device.bytes_rx + 4], 0
1159 hidnplayr 918
	inc	dword [ebx + device.packets_rx]     ;
919
	sub	ecx, 4				    ; don't copy CRC
920
	DEBUGF	1,"Received %u bytes\n", ecx
921
 
922
	push	ebx eax ecx
923
	stdcall KernelAlloc, ecx		    ; Allocate a buffer to put packet into
924
	pop	ecx
925
	test	eax, eax			    ; Test if we allocated succesfully
926
	jz	.abort				    ;
927
 
928
	mov	edi, eax			    ; Set up registers to copy the packet
929
	mov	esi, [esp]			    ;
930
	add	esi, 4				    ; Dont copy CRC
931
 
932
	push	dword .abort			    ; Kernel will return to this address after EthReceiver
933
	push	ecx edi 			    ; Save buffer pointer and size, to pass to kernel
934
 
935
	shr	ecx, 2
936
	cld
937
	rep	movsd				    ; copy the dwords
938
	mov	ecx, [esp+4]
939
	and	ecx, 3
940
	rep	movsb				    ; copy the rest bytes
941
 
942
	jmp	EthReceiver			    ; Send it to kernel
943
 
944
  .abort:
945
	pop	eax ebx
946
						    ; update eth_data_start_offset
947
	movzx	eax, word [eax+2]		    ; packet length
948
	add	eax, [ebx+device.rx_data_offset]
949
	add	eax, 4+3			    ; packet header is 4 bytes long + dword alignment
950
	and	eax, not 3			    ; dword alignment
951
	cmp	eax, RX_BUFFER_SIZE
952
	jl	.no_wrap
953
	sub	eax, RX_BUFFER_SIZE
954
  .no_wrap:
955
	mov	[ebx+device.rx_data_offset], eax
956
	DEBUGF	1,"New RX ptr: %u", eax
957
 
958
	mov	edx, dword [ebx+device.io_addr]
959
	add	edx, REG_CAPR			    ; update 'Current Address of Packet Read register'
960
	sub	eax, 0x10			    ; value 0x10 is a constant for CAPR
961
	out	dx , ax
962
 
963
	jmp	.receive			    ; check for multiple packets
964
 
965
  .reset_rx:
966
	test	byte [eax], (1 shl BIT_CRC)
967
	jz	.no_crc_error
968
	DEBUGF	2,"\nCRC error!\n"
969
 
970
  .no_crc_error:
971
	test	byte [eax], (1 shl BIT_FAE)
972
	jz	.no_fae_error
973
	DEBUGF	1,"\nFrame alignment error!\n"
974
 
975
  .no_fae_error:
976
	DEBUGF	1,"Reset RX\n"
977
	in	al , dx 			    ; read command register
978
	push	ax
979
 
980
	and	al , not (1 shl BIT_RE) 	    ; Clear the RE bit
981
	out	dx , al
982
 
983
	pop	ax
984
	out	dx , al 			    ; write original command back
985
 
986
	add	edx, REG_RXCONFIG - REG_COMMAND     ; Restore RX configuration
987
	mov	ax , RX_CONFIG
988
	out	dx , ax
989
 
990
  .finish:
991
	pop	ax
992
 
993
;----------------------------------------------------
994
; Transmit error ?
995
 
996
  @@:
997
	test	ax, ISR_TER
998
	jz	@f
999
 
1000
	push	ax
1001
	cmp	[ebx+device.curr_tx_desc], 4
1002
	jz	.notxd
1003
 
1004
	mov	edx, [ebx+device.io_addr]
1005
	movzx	ecx, [ebx+device.curr_tx_desc]
1006
	lea	edx, [edx+ecx*4+REG_TSD0]
1007
	in	eax, dx
1008
 
1009
  .notxd:
1010
	test	eax, TSR_TUN
1011
	jz	.nobun
1012
	DEBUGF	1, "TX: FIFO Buffer underrun!\n"
1013
 
1014
  .nobun:
1015
	test	eax, TSR_OWC
1016
	jz	.noowc
1017
	DEBUGF	1, "TX: OWC!\n"
1018
 
1019
  .noowc:
1020
	test	eax, TSR_TABT
1021
	jz	.notabt
1022
	DEBUGF	1, "TX: TABT!\n"
1023
 
1024
  .notabt:
1025
	test	eax, TSR_CRS
1026
	jz	.nocsl
1027
	DEBUGF	1, "TX: Carrier Sense Lost!\n"
1028
 
1029
  .nocsl:
1030
;                test    eax, TSR_OWN or TSR_TOK
1031
;                jz      .nofd
1032
;                DEBUGF  1, "TX: Transmit OK (desc: %u)\n", ecx
1033
;
1034
;               .nofd:
1035
	pop	ax
1036
 
1037
;----------------------------------------------------
1038
; Transmit ok ?
1039
 
1040
  @@:
1041
	test	ax, ISR_TOK
1042
	jz	@f
1043
 
1044
	DEBUGF	1, "TX: Transmit OK (desc: %u)\n", [ebx+device.curr_tx_desc]:1
1045
 
1046
;----------------------------------------------------
1047
; Rx buffer overflow ?
1048
 
1049
  @@:
1050
	test	ax, ISR_RXOVW
1051
	jz	@f
1052
 
1053
	push	ax
1054
	DEBUGF	1,"RX-buffer overflow!\n"
1055
 
1056
	mov	edx, [ebx+device.io_addr]
1057
	add	edx, REG_ISR
1058
	mov	ax , ISR_FIFOOVW or ISR_RXOVW
1059
	out	dx , ax
1060
	pop	ax
1061
 
1062
;----------------------------------------------------
1063
; Packet underrun? ?
1064
 
1065
 
1066
  @@:
1067
	test	ax, ISR_PUN
1068
	jz	@f
1069
 
1070
	DEBUGF	1,"Packet underrun!\n"
1071
 
1072
;----------------------------------------------------
1073
; Receive FIFO overflow ?
1074
 
1075
  @@:
1076
	test	ax, ISR_FIFOOVW
1077
	jz	@f
1078
 
1079
	push	ax
1080
	DEBUGF	2,"RX fifo overflox!\n"
1081
 
1082
	mov	edx, [ebx+device.io_addr]
1083
	add	edx, REG_ISR
1084
	mov	ax , ISR_FIFOOVW or ISR_RXOVW
1085
	out	dx , ax
1086
	pop	ax
1087
 
1088
;----------------------------------------------------
1089
; Something about Cable changed ?
1090
 
1091
  @@:
1092
	test	ax, ISR_LENCHG
1093
	jz	.fail
1094
 
1095
	DEBUGF	2,"Cable changed!\n"
1096
	call	cable
1097
 
1098
; If none of the above events happened, just exit clearing int
1099
 
1100
  .fail:
1101
 
1102
	DEBUGF	2,"\n"
1103
	ret
1104
 
1105
 
1106
 
1107
 
1108
;;;;;;;;;;;;;;;;;;;;;;;;;
1109
;;                     ;;
1110
;; Update Cable status ;;
1111
;;                     ;;
1112
;;;;;;;;;;;;;;;;;;;;;;;;;
1113
 
1114
align 4
1115
cable:
1116
	DEBUGF	1,"Checking Cable status: "
1117
 
1118
	mov	edx, dword [ebx+device.io_addr]
1119
	add	edx, REG_MSR
1120
	in	al , dx
1121
 
1122
;        test    al , 1 SHL 2     ; 0 = link ok 1 = link fail
1123
;        jnz     .notconnected
1124
 
1125
;        test    al , 1 SHL 3     ; 0 = 100 Mbps 1 = 10 Mbps
1126
;        jnz     .10mbps
1127
 
1128
	shr	al, 2
1129
	and	al, 3
1130
 
1131
	mov	byte [ebx+device.mode+3], al
1132
	DEBUGF	1,"Done!\n"
1133
ret
1134
 
1135
 
1136
 
1137
;;;;;;;;;;;;;;;;;;;;;;;
1138
;;                   ;;
1139
;; Write MAC address ;;
1140
;;                   ;;
1141
;;;;;;;;;;;;;;;;;;;;;;;
1142
 
1143
align 4
1144
write_mac:	; in: mac pushed onto stack (as 3 words)
1145
 
1146
	DEBUGF	1,"Writing MAC: "
1147
 
1148
; disable all in command registers
1149
 
1150
	mov	edx, [ebx+device.io_addr]
1151
	add	edx, REG_9346CR
1152
	xor	eax, eax
1153
	out	dx , al
1154
 
1155
	add	edx, REG_IMR - REG_9346CR
1156
	xor	eax, eax
1157
	out	dx , ax
1158
 
1159
	add	edx, REG_ISR - REG_IMR
1160
	mov	eax, -1
1161
	out	dx , ax
1162
 
1163
; enable writing
1164
 
1165
 
1166
	add	edx, REG_9346CR - REG_ISR
1167
	mov	eax, REG_9346CR_WE
1168
	out	dx , al
1169
 
1170
 ; write the mac ...
1171
 
1172
	add	edx, REG_IDR0 - REG_9346CR
1173
	pop	eax
1174
	out	dx , eax
1175
 
1176
	add	edx, 4
1177
	xor	eax, eax
1178
	pop	ax
1179
	out	dx , eax
1180
 
1181
; disable writing
1182
 
1183
	add	edx, REG_9346CR -REG_IDR0
1184
	xor	eax, eax
1185
	out	dx , al
1186
 
1187
	DEBUGF	1,"ok!\n"
1188
 
1189
; Notice this procedure does not ret, but continues to read_mac instead.
1190
 
1191
 
1192
;;;;;;;;;;;;;;;;;;;;;;
1193
;;                  ;;
1194
;; Read MAC address ;;
1195
;;                  ;;
1196
;;;;;;;;;;;;;;;;;;;;;;
1197
 
1198
read_mac:
1199
	DEBUGF	1,"Reading MAC: "
1200
 
1201
	mov	edx, [ebx + device.io_addr]
1202
	lea	edi, [ebx + device.mac]
1177 clevermous 1203
	in	eax, dx
1159 hidnplayr 1204
	stosd
1177 clevermous 1205
	add	edx, 4
1173 clevermous 1206
	in	ax, dx
1159 hidnplayr 1207
	stosw
1208
 
1173 clevermous 1209
	DEBUGF	1,"%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
1210
 
1159 hidnplayr 1211
	ret
1212
 
1213
 
1214
 
1215
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1216
;;                                                                      ;;
1217
;; Read eeprom (type 93c46 and 93c56)                                   ;;
1218
;;                                                                      ;;
1219
;; In: word to be read in al (6bit in case of 93c46 and 8bit otherwise) ;;
1220
;;     pointer to device structure in ebx                               ;;
1221
;;                                                                      ;;
1222
;; OUT: word read in ax                                                 ;;
1223
;;                                                                      ;;
1224
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1225
 
1226
align 4
1227
read_eeprom:
1228
	DEBUGF	2,"Reading eeprom, "
1229
 
1230
	mov	edx, [ebx+device.io_addr]
1231
	push	ebx
1232
	movzx	ebx, al
1233
	add	edx, REG_RXCONFIG
1234
	in	al, dx
1235
	test	al, (1 shl BIT_9356SEL)
1236
	jz	.type_93c46
1237
;       and     bl, 01111111b ; don't care first bit
1238
	or	bx, EE_93C56_READ_CMD		; it contains start bit
1239
	mov	cx, EE_93C56_CMD_LENGTH-1	; cmd_loop counter
1240
	jmp	.read_eeprom
1241
.type_93c46:
1242
	and	bl, 00111111b
1243
	or	bx, EE_93C46_READ_CMD		; it contains start bit
1244
	mov	cx, EE_93C46_CMD_LENGTH-1	; cmd_loop counter
1245
.read_eeprom:
1246
	add	edx, REG_9346CR - REG_RXCONFIG
1247
;       mov     al, (1 shl BIT_93C46_EEM1)
1248
;       out     dx, al
1249
	mov	al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS) ; wake up the eeprom
1250
	out	dx, al
1251
.cmd_loop:
1252
	mov	al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
1253
	bt	bx, cx
1254
	jnc	.zero_bit
1255
	or	al, (1 shl BIT_93C46_EEDI)
1256
.zero_bit:
1257
	out	dx, al
1258
;       push    eax
1259
;       in      eax, dx ; eeprom delay
1260
;       pop     eax
1261
	or	al, (1 shl BIT_93C46_EESK)
1262
	out	dx, al
1263
;       in      eax, dx ; eeprom delay
1264
	dec	cx
1265
	jns	.cmd_loop
1266
;       in      eax, dx ; eeprom delay
1267
	mov	al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
1268
	out	dx, al
1269
	mov	cl, 0xf
1270
.read_loop:
1271
	shl	ebx, 1
1272
	mov	al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS) or (1 shl BIT_93C46_EESK)
1273
	out	dx, al
1274
;       in      eax, dx ; eeprom delay
1275
	in	al, dx
1276
	and	al, (1 shl BIT_93C46_EEDO)
1277
	jz	.dont_set
1278
	inc	ebx
1279
.dont_set:
1280
	mov	al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
1281
	out	dx, al
1282
;       in      eax, dx ; eeprom delay
1283
	dec	cl
1284
	jns	.read_loop
1285
	xor	al, al
1286
	out	dx, al
1287
	mov	ax, bx
1288
	pop	ebx
1289
 
1290
	ret
1291
 
1292
 
1293
; End of code
1294
 
1295
align 4 					; Place all initialised data here
1296
 
1297
RTL8139_DEV   dd 0
1298
version       dd (5 shl 16) or (API_VERSION and 0xFFFF)
1299
my_service    db 'RTL8139',0			; max 16 chars include zero
1300
 
1178 hidnplayr 1301
device_1      db 'Realtek 8139',0
1302
device_2      db 'Realtek 8139A',0
1303
device_3      db 'Realtek 8139B',0
1304
device_4      db 'Realtek 8139C',0
1305
device_5      db 'Realtek 8100',0
1306
device_6      db 'Realtek 8139D',0
1307
device_7      db 'Realtek 8139CP',0
1308
device_8      db 'Realtek 8101',0
1309
 
1310
name_crosslist dd device_1
1311
	       dd device_2
1312
	       dd device_3
1313
	       dd device_4
1314
	       dd device_5
1315
	       dd device_6
1316
	       dd device_7
1317
	       dd device_8
1318
 
1159 hidnplayr 1319
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
1320
	      db VER_RTL8139A
1321
	      db VER_RTL8139B
1322
	      db VER_RTL8139C
1323
	      db VER_RTL8100
1324
	      db VER_RTL8139D
1325
	      db VER_RTL8139CP
1326
	      db VER_RTL8101
1327
 
1328
HW_VER_ARRAY_SIZE = $-hw_ver_array
1329
 
1330
include_debug_strings				; All data wich FDO uses will be included here
1331
 
1332
section '.data' data readable writable align 16 ; place all uninitialized data place here
1333
 
1334
RTL8139_LIST rd MAX_RTL8139			; This list contains all pointers to device structures the driver is handling
1335