Subversion Repositories Kolibri OS

Rev

Rev 387 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 387 Rev 444
-
 
1
$Revision: 431 $
-
 
2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
 
3
;;                                                              ;;
-
 
4
;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
-
 
5
;; Distributed under terms of the GNU General Public License    ;;
-
 
6
;;                                                              ;;
-
 
7
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
 
8
 
1
;***************************************************************************
9
;***************************************************************************
2
;
10
;
3
;  PCI CODE FOLLOWS
11
;  PCI CODE FOLLOWS
4
;
12
;
5
;  the following functions provide access to the PCI interface.
13
;  the following functions provide access to the PCI interface.
6
;  These functions are used by scan_bus, and also some ethernet drivers
14
;  These functions are used by scan_bus, and also some ethernet drivers
7
;
15
;
8
;***************************************************************************
16
;***************************************************************************
9
 
17
 
10
; PCI Bus defines
18
; PCI Bus defines
11
PCI_HEADER_TYPE 	    equ     0x0e  ;8 bit
19
PCI_HEADER_TYPE 	    equ     0x0e  ;8 bit
12
PCI_BASE_ADDRESS_0	    equ     0x10  ;32 bit
20
PCI_BASE_ADDRESS_0	    equ     0x10  ;32 bit
13
PCI_BASE_ADDRESS_5	    equ     0x24  ;32 bits
21
PCI_BASE_ADDRESS_5	    equ     0x24  ;32 bits
14
PCI_BASE_ADDRESS_SPACE_IO   equ     0x01
22
PCI_BASE_ADDRESS_SPACE_IO   equ     0x01
15
PCI_VENDOR_ID		    equ     0x00  ;16 bit
23
PCI_VENDOR_ID		    equ     0x00  ;16 bit
16
PCI_BASE_ADDRESS_IO_MASK    equ     0xFFFFFFFC
24
PCI_BASE_ADDRESS_IO_MASK    equ     0xFFFFFFFC
17
 
25
 
18
;***************************************************************************
26
;***************************************************************************
19
;   Function
27
;   Function
20
;      config_cmd
28
;      config_cmd
21
;
29
;
22
;   Description
30
;   Description
23
;       creates a command dword  for use with the PCI bus
31
;       creates a command dword  for use with the PCI bus
24
;       bus # in ebx
32
;       bus # in ebx
25
;      devfn in ecx
33
;      devfn in ecx
26
;       where in edx
34
;       where in edx
27
;
35
;
28
;      command dword returned in eax
36
;      command dword returned in eax
29
;       Only eax destroyed
37
;       Only eax destroyed
30
;***************************************************************************
38
;***************************************************************************
31
config_cmd:
39
config_cmd:
32
    push    ecx
40
    push    ecx
33
    mov     eax, ebx
41
    mov     eax, ebx
34
    shl     eax, 16
42
    shl     eax, 16
35
    or	    eax, 0x80000000
43
    or	    eax, 0x80000000
36
    shl     ecx, 8
44
    shl     ecx, 8
37
    or	    eax, ecx
45
    or	    eax, ecx
38
    pop     ecx
46
    pop     ecx
39
    or	    eax, edx
47
    or	    eax, edx
40
    and     eax, 0xFFFFFFFC
48
    and     eax, 0xFFFFFFFC
41
    ret
49
    ret
42
 
50
 
43
;***************************************************************************
51
;***************************************************************************
44
;   Function
52
;   Function
45
;      pcibios_read_config_byte
53
;      pcibios_read_config_byte
46
;
54
;
47
;   Description
55
;   Description
48
;       reads a byte from the PCI config space
56
;       reads a byte from the PCI config space
49
;       bus # in ebx
57
;       bus # in ebx
50
;      devfn in ecx
58
;      devfn in ecx
51
;       where in edx ( ls 16 bits significant )
59
;       where in edx ( ls 16 bits significant )
52
;
60
;
53
;      byte returned in al ( rest of eax zero )
61
;      byte returned in al ( rest of eax zero )
54
;       Only eax/edx destroyed
62
;       Only eax/edx destroyed
55
;***************************************************************************
63
;***************************************************************************
56
pcibios_read_config_byte:
64
pcibios_read_config_byte:
57
    call    config_cmd
65
    call    config_cmd
58
    push    dx
66
    push    dx
59
    mov     dx, 0xCF8
67
    mov     dx, 0xCF8
60
    out     dx, eax
68
    out     dx, eax
61
    pop     dx
69
    pop     dx
62
 
70
 
63
    xor     eax, eax
71
    xor     eax, eax
64
    and     dx, 0x03
72
    and     dx, 0x03
65
    add     dx, 0xCFC
73
    add     dx, 0xCFC
66
;   and     dx, 0xFFC
74
;   and     dx, 0xFFC
67
    in	    al, dx
75
    in	    al, dx
68
    ret
76
    ret
69
 
77
 
70
;***************************************************************************
78
;***************************************************************************
71
;   Function
79
;   Function
72
;      pcibios_read_config_word
80
;      pcibios_read_config_word
73
;
81
;
74
;   Description
82
;   Description
75
;       reads a word from the PCI config space
83
;       reads a word from the PCI config space
76
;       bus # in ebx
84
;       bus # in ebx
77
;      devfn in ecx
85
;      devfn in ecx
78
;       where in edx ( ls 16 bits significant )
86
;       where in edx ( ls 16 bits significant )
79
;
87
;
80
;      word returned in ax ( rest of eax zero )
88
;      word returned in ax ( rest of eax zero )
81
;       Only eax/edx destroyed
89
;       Only eax/edx destroyed
82
;***************************************************************************
90
;***************************************************************************
83
pcibios_read_config_word:
91
pcibios_read_config_word:
84
    call    config_cmd
92
    call    config_cmd
85
    push    dx
93
    push    dx
86
    mov     dx, 0xCF8
94
    mov     dx, 0xCF8
87
    out     dx, eax
95
    out     dx, eax
88
    pop     dx
96
    pop     dx
89
 
97
 
90
    xor     eax, eax
98
    xor     eax, eax
91
    and     dx, 0x02
99
    and     dx, 0x02
92
    add     dx, 0xCFC
100
    add     dx, 0xCFC
93
;   and     dx, 0xFFC
101
;   and     dx, 0xFFC
94
    in	    ax, dx
102
    in	    ax, dx
95
    ret
103
    ret
96
 
104
 
97
;***************************************************************************
105
;***************************************************************************
98
;   Function
106
;   Function
99
;      pcibios_read_config_dword
107
;      pcibios_read_config_dword
100
;
108
;
101
;   Description
109
;   Description
102
;       reads a dword from the PCI config space
110
;       reads a dword from the PCI config space
103
;       bus # in ebx
111
;       bus # in ebx
104
;      devfn in ecx
112
;      devfn in ecx
105
;       where in edx ( ls 16 bits significant )
113
;       where in edx ( ls 16 bits significant )
106
;
114
;
107
;      dword returned in eax
115
;      dword returned in eax
108
;       Only eax/edx destroyed
116
;       Only eax/edx destroyed
109
;***************************************************************************
117
;***************************************************************************
110
pcibios_read_config_dword:
118
pcibios_read_config_dword:
111
    push    edx
119
    push    edx
112
    call    config_cmd
120
    call    config_cmd
113
    push    dx
121
    push    dx
114
    mov     dx, 0xCF8
122
    mov     dx, 0xCF8
115
    out     dx, eax
123
    out     dx, eax
116
    pop     dx
124
    pop     dx
117
    xor     eax, eax
125
    xor     eax, eax
118
    mov     dx, 0xCFC
126
    mov     dx, 0xCFC
119
    in	    eax, dx
127
    in	    eax, dx
120
    pop     edx
128
    pop     edx
121
    ret
129
    ret
122
 
130
 
123
;***************************************************************************
131
;***************************************************************************
124
;   Function
132
;   Function
125
;      pcibios_write_config_byte
133
;      pcibios_write_config_byte
126
;
134
;
127
;   Description
135
;   Description
128
;       write a byte in al to the PCI config space
136
;       write a byte in al to the PCI config space
129
;       bus # in ebx
137
;       bus # in ebx
130
;      devfn in ecx
138
;      devfn in ecx
131
;       where in edx ( ls 16 bits significant )
139
;       where in edx ( ls 16 bits significant )
132
;
140
;
133
;       Only eax/edx destroyed
141
;       Only eax/edx destroyed
134
;***************************************************************************
142
;***************************************************************************
135
pcibios_write_config_byte:
143
pcibios_write_config_byte:
136
    push    ax
144
    push    ax
137
    call    config_cmd
145
    call    config_cmd
138
    push    dx
146
    push    dx
139
    mov     dx, 0xCF8
147
    mov     dx, 0xCF8
140
    out     dx, eax
148
    out     dx, eax
141
    pop     dx
149
    pop     dx
142
    pop     ax
150
    pop     ax
143
 
151
 
144
    and     dx, 0x03
152
    and     dx, 0x03
145
    add     dx, 0xCFC
153
    add     dx, 0xCFC
146
    out     dx, al
154
    out     dx, al
147
    ret
155
    ret
148
 
156
 
149
;***************************************************************************
157
;***************************************************************************
150
;   Function
158
;   Function
151
;      pcibios_write_config_word
159
;      pcibios_write_config_word
152
;
160
;
153
;   Description
161
;   Description
154
;       write a word in ax to the PCI config space
162
;       write a word in ax to the PCI config space
155
;       bus # in ebx
163
;       bus # in ebx
156
;      devfn in ecx
164
;      devfn in ecx
157
;       where in edx ( ls 16 bits significant )
165
;       where in edx ( ls 16 bits significant )
158
;
166
;
159
;       Only eax/edx destroyed
167
;       Only eax/edx destroyed
160
;***************************************************************************
168
;***************************************************************************
161
pcibios_write_config_word:
169
pcibios_write_config_word:
162
    push    ax
170
    push    ax
163
    call    config_cmd
171
    call    config_cmd
164
    push    dx
172
    push    dx
165
    mov     dx, 0xCF8
173
    mov     dx, 0xCF8
166
    out     dx, eax
174
    out     dx, eax
167
    pop     dx
175
    pop     dx
168
    pop     ax
176
    pop     ax
169
 
177
 
170
    and     dx, 0x02
178
    and     dx, 0x02
171
    add     dx, 0xCFC
179
    add     dx, 0xCFC
172
    out     dx, ax
180
    out     dx, ax
173
    ret
181
    ret
174
 
182
 
175
;***************************************************************************
183
;***************************************************************************
176
;   Function
184
;   Function
177
;      delay_us
185
;      delay_us
178
;
186
;
179
;   Description
187
;   Description
180
;       delays for 30 to 60 us
188
;       delays for 30 to 60 us
181
;
189
;
182
;        I would prefer this routine to be able to delay for
190
;        I would prefer this routine to be able to delay for
183
;       a selectable number of microseconds, but this works for now.
191
;       a selectable number of microseconds, but this works for now.
184
;
192
;
185
;       If you know a better way to do 2us delay, pleae tell me!
193
;       If you know a better way to do 2us delay, pleae tell me!
186
;***************************************************************************
194
;***************************************************************************
187
delay_us:
195
delay_us:
188
    push    eax
196
    push    eax
189
    push    ecx
197
    push    ecx
190
 
198
 
191
    mov     ecx,2
199
    mov     ecx,2
192
 
200
 
193
    in	    al,0x61
201
    in	    al,0x61
194
    and     al,0x10
202
    and     al,0x10
195
    mov     ah,al
203
    mov     ah,al
196
    cld
204
    cld
197
 
205
 
198
dcnt1:
206
dcnt1:
199
    in	    al,0x61
207
    in	    al,0x61
200
    and     al,0x10
208
    and     al,0x10
201
    cmp     al,ah
209
    cmp     al,ah
202
    jz	    dcnt1
210
    jz	    dcnt1
203
 
211
 
204
    mov     ah,al
212
    mov     ah,al
205
    loop    dcnt1
213
    loop    dcnt1
206
 
214
 
207
    pop     ecx
215
    pop     ecx
208
    pop     eax
216
    pop     eax
209
 
217
 
210
    ret
218
    ret
211
 
219
 
212
;***************************************************************************
220
;***************************************************************************
213
;   Function
221
;   Function
214
;      scan_bus
222
;      scan_bus
215
;
223
;
216
;   Description
224
;   Description
217
;       Scans the PCI bus for a supported device
225
;       Scans the PCI bus for a supported device
218
;        If a supported device is found, the drvr_ variables are initialised
226
;        If a supported device is found, the drvr_ variables are initialised
219
;       to that drivers functions ( as defined in the PCICards table)
227
;       to that drivers functions ( as defined in the PCICards table)
220
;
228
;
221
;        io_addr   holds card I/O space. 32 bit, but only LS 16 bits valid
229
;        io_addr   holds card I/O space. 32 bit, but only LS 16 bits valid
222
;        pci_data  holds the PCI vendor + device code
230
;        pci_data  holds the PCI vendor + device code
223
;       pci_dev   holds PCI bus dev #
231
;       pci_dev   holds PCI bus dev #
224
;       pci_bus   holds PCI bus #
232
;       pci_bus   holds PCI bus #
225
;
233
;
226
;        io_addr will be zero if no card found
234
;        io_addr will be zero if no card found
227
;
235
;
228
;***************************************************************************
236
;***************************************************************************
229
scan_bus:
237
scan_bus:
230
    xor     eax, eax
238
    xor     eax, eax
231
    mov     [hdrtype], al
239
    mov     [hdrtype], al
232
    mov     [pci_data], eax
240
    mov     [pci_data], eax
233
 
241
 
234
    xor     ebx, ebx	     ; ebx = bus# 0 .. 255
242
    xor     ebx, ebx	     ; ebx = bus# 0 .. 255
235
 
243
 
236
sb_bus_loop:
244
sb_bus_loop:
237
    xor     ecx, ecx	     ; ecx = devfn# 0 .. 254  ( not 255? )
245
    xor     ecx, ecx	     ; ecx = devfn# 0 .. 254  ( not 255? )
238
 
246
 
239
sb_devf_loop:
247
sb_devf_loop:
240
    mov     eax, ecx
248
    mov     eax, ecx
241
    and     eax, 0x07
249
    and     eax, 0x07
242
 
250
 
243
    cmp     eax, 0
251
    cmp     eax, 0
244
    jne     sb_001
252
    jne     sb_001
245
 
253
 
246
    mov     edx, PCI_HEADER_TYPE
254
    mov     edx, PCI_HEADER_TYPE
247
    call    pcibios_read_config_byte
255
    call    pcibios_read_config_byte
248
    mov     [hdrtype], al
256
    mov     [hdrtype], al
249
    jmp     sb_002
257
    jmp     sb_002
250
 
258
 
251
sb_001:
259
sb_001:
252
    mov     al, [hdrtype]
260
    mov     al, [hdrtype]
253
    and     al, 0x80
261
    and     al, 0x80
254
    cmp     al, 0x80
262
    cmp     al, 0x80
255
    jne     sb_inc_devf
263
    jne     sb_inc_devf
256
 
264
 
257
sb_002:
265
sb_002:
258
    mov     edx, PCI_VENDOR_ID
266
    mov     edx, PCI_VENDOR_ID
259
    call    pcibios_read_config_dword
267
    call    pcibios_read_config_dword
260
    mov     [vendor_device], eax
268
    mov     [vendor_device], eax
261
    cmp     eax, 0xffffffff
269
    cmp     eax, 0xffffffff
262
    je	    sb_empty
270
    je	    sb_empty
263
    cmp     eax, 0
271
    cmp     eax, 0
264
    jne     sb_check_vendor
272
    jne     sb_check_vendor
265
 
273
 
266
sb_empty:
274
sb_empty:
267
    mov     [hdrtype], byte 0
275
    mov     [hdrtype], byte 0
268
    jmp     sb_inc_devf
276
    jmp     sb_inc_devf
269
 
277
 
270
sb_check_vendor:
278
sb_check_vendor:
271
    ; iterate though PCICards until end or match found
279
    ; iterate though PCICards until end or match found
272
    mov     esi, PCICards
280
    mov     esi, PCICards
273
 
281
 
274
sb_check:
282
sb_check:
275
    cmp     [esi], dword 0
283
    cmp     [esi], dword 0
276
    je	    sb_inc_devf 	       ; Quit if at last entry
284
    je	    sb_inc_devf 	       ; Quit if at last entry
277
    cmp     eax, [esi]
285
    cmp     eax, [esi]
278
    je	    sb_got_card
286
    je	    sb_got_card
279
    add     esi, PCICARDS_ENTRY_SIZE
287
    add     esi, PCICARDS_ENTRY_SIZE
280
    jmp     sb_check
288
    jmp     sb_check
281
 
289
 
282
sb_got_card:
290
sb_got_card:
283
    ; indicate that we have found the card
291
    ; indicate that we have found the card
284
    mov     [pci_data], eax
292
    mov     [pci_data], eax
285
    mov     [pci_dev], ecx
293
    mov     [pci_dev], ecx
286
    mov     [pci_bus], ebx
294
    mov     [pci_bus], ebx
287
 
295
 
288
    ; Define the driver functions
296
    ; Define the driver functions
289
    push    eax
297
    push    eax
290
    mov     eax, [esi+4]
298
    mov     eax, [esi+4]
291
    mov     [drvr_probe], eax
299
    mov     [drvr_probe], eax
292
    mov     eax, [esi+8]
300
    mov     eax, [esi+8]
293
    mov     [drvr_reset], eax
301
    mov     [drvr_reset], eax
294
    mov     eax, [esi+12]
302
    mov     eax, [esi+12]
295
    mov     [drvr_poll], eax
303
    mov     [drvr_poll], eax
296
    mov     eax, [esi+16]
304
    mov     eax, [esi+16]
297
    mov     [drvr_transmit], eax
305
    mov     [drvr_transmit], eax
298
    mov     eax, [esi+20]
306
    mov     eax, [esi+20]
299
    mov     [drvr_cable], eax
307
    mov     [drvr_cable], eax
300
    pop     eax
308
    pop     eax
301
 
309
 
302
    mov     edx, PCI_BASE_ADDRESS_0
310
    mov     edx, PCI_BASE_ADDRESS_0
303
 
311
 
304
sb_reg_check:
312
sb_reg_check:
305
    call    pcibios_read_config_dword
313
    call    pcibios_read_config_dword
306
    mov     [io_addr], eax
314
    mov     [io_addr], eax
307
    and     eax, PCI_BASE_ADDRESS_IO_MASK
315
    and     eax, PCI_BASE_ADDRESS_IO_MASK
308
    cmp     eax, 0
316
    cmp     eax, 0
309
    je	    sb_inc_reg
317
    je	    sb_inc_reg
310
    mov     eax, [io_addr]
318
    mov     eax, [io_addr]
311
    and     eax, PCI_BASE_ADDRESS_SPACE_IO
319
    and     eax, PCI_BASE_ADDRESS_SPACE_IO
312
    cmp     eax, 0
320
    cmp     eax, 0
313
    je	    sb_inc_reg
321
    je	    sb_inc_reg
314
 
322
 
315
    mov     eax, [io_addr]
323
    mov     eax, [io_addr]
316
    and     eax, PCI_BASE_ADDRESS_IO_MASK
324
    and     eax, PCI_BASE_ADDRESS_IO_MASK
317
    mov     [io_addr], eax
325
    mov     [io_addr], eax
318
 
326
 
319
sb_exit1:
327
sb_exit1:
320
    ret
328
    ret
321
 
329
 
322
sb_inc_reg:
330
sb_inc_reg:
323
    add     edx, 4
331
    add     edx, 4
324
    cmp     edx, PCI_BASE_ADDRESS_5
332
    cmp     edx, PCI_BASE_ADDRESS_5
325
    jbe     sb_reg_check
333
    jbe     sb_reg_check
326
 
334
 
327
sb_inc_devf:
335
sb_inc_devf:
328
    inc     ecx
336
    inc     ecx
329
    cmp     ecx, 255
337
    cmp     ecx, 255
330
    jb	    sb_devf_loop
338
    jb	    sb_devf_loop
331
    inc     ebx
339
    inc     ebx
332
    cmp     ebx, 256
340
    cmp     ebx, 256
333
    jb	    sb_bus_loop
341
    jb	    sb_bus_loop
334
 
342
 
335
    ; We get here if we didn't find our card
343
    ; We get here if we didn't find our card
336
    ; set io_addr to 0 as an indication
344
    ; set io_addr to 0 as an indication
337
    xor     eax, eax
345
    xor     eax, eax
338
    mov     [io_addr], eax
346
    mov     [io_addr], eax
339
 
347
 
340
sb_exit2:
348
sb_exit2:
341
    ret
349
    ret