Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
425 victor 1
$Revision: 431 $
431 serge 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
;;  PCI32.INC                                                   ;;
9
;;                                                              ;;
10
;;  32 bit PCI driver code                                      ;;
11
;;                                                              ;;
12
;;  Version 0.2  December 21st, 2002                            ;;
13
;;                                                              ;;
14
;;  Author: Victor Prodan, victorprodan@yahoo.com               ;;
15
;;    Credits:                                                  ;;
16
;;          Ralf Brown                                          ;;
17
;;          Mike Hibbett, mikeh@oceanfree.net                   ;;
18
;;                                                              ;;
19
;;  See file COPYING for details                                ;;
20
;;                                                              ;;
21
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1 ha 22
 
23
 
24
;***************************************************************************
25
;   Function
26
;      pci_api:
27
;
28
;   Description
29
;       entry point for system PCI calls
30
;***************************************************************************
31
 
32
align 4
33
 
34
pci_api:
35
 
36
        cmp  [pci_access_enabled],1
37
        jne  no_pci_access_for_applications
38
 
39
        or al,al
40
        jnz pci_fn_1
41
        ; PCI function 0: get pci version (AH.AL)
42
        movzx eax,word [0x2F0000+0x9022]
43
        ret
44
 
45
pci_fn_1:
46
        cmp al,1
47
        jnz pci_fn_2
48
 
49
        ; PCI function 1: get last bus in AL
50
        mov al,[0x2F0000+0x9021]
51
        ret
52
 
53
pci_fn_2:
54
        cmp al,2
55
        jne pci_fn_3
56
        ; PCI function 2: get pci access mechanism
57
        mov al,[0x2F0000+0x9020]
58
        ret
59
pci_fn_3:
60
 
61
        cmp al,4
62
        jz pci_read_reg   ;byte
63
        cmp al,5
64
        jz pci_read_reg   ;word
65
        cmp al,6
66
        jz pci_read_reg   ;dword
67
 
68
        cmp al,8
69
        jz pci_write_reg  ;byte
70
        cmp al,9
71
        jz pci_write_reg  ;word
72
        cmp al,10
73
        jz pci_write_reg  ;dword
74
 
75
      no_pci_access_for_applications:
76
 
77
        mov eax,-1
78
 
79
        ret
80
 
81
;***************************************************************************
82
;   Function
83
;      pci_make_config_cmd
84
;
85
;   Description
86
;       creates a command dword  for use with the PCI bus
87
;       bus # in ah
88
;       device+func in bh (dddddfff)
89
;       register in bl
90
;
91
;      command dword returned in eax ( 10000000 bbbbbbbb dddddfff rrrrrr00 )
92
;***************************************************************************
93
 
94
align 4
95
 
96
pci_make_config_cmd:
97
    shl     eax,8          ; move bus to bits 16-23
98
    mov     ax,bx          ; combine all
99
    and     eax,0xffffff
100
    or      eax,0x80000000
101
    ret
102
 
103
;***************************************************************************
104
;   Function
105
;      pci_read_reg:
106
;
107
;   Description
108
;       read a register from the PCI config space into EAX/AX/AL
109
;       IN: ah=bus,device+func=bh,register address=bl
110
;           number of bytes to read (1,2,4) coded into AL, bits 0-1
111
;***************************************************************************
112
 
113
align 4
114
 
115
pci_read_reg:
116
        cmp     byte [0x2F0000+0x9020],2 ;what mechanism will we use?
117
        je      pci_read_reg_2
118
 
119
                ; mechanism 1
120
        push    esi   ; save register size into ESI
121
        mov     esi,eax
122
        and     esi,3
123
 
124
        call    pci_make_config_cmd
125
        mov     ebx,eax
126
                ; get current state
127
        mov     dx,0xcf8
128
        in      eax, dx
129
        push    eax
130
                ; set up addressing to config data
131
        mov     eax,ebx
132
        and     al,0xfc ; make address dword-aligned
133
        out     dx,eax
134
                ; get requested DWORD of config data
135
        mov     dl,0xfc
136
        and     bl,3
137
        or      dl,bl    ; add to port address first 2 bits of register address
138
 
139
        or      esi,esi
140
        jz      pci_read_byte1
141
        cmp     esi,1
142
        jz      pci_read_word1
143
        cmp     esi,2
144
        jz      pci_read_dword1
145
        jmp     pci_fin_read1
146
 
147
pci_read_byte1:
148
        in      al,dx
149
        jmp pci_fin_read1
150
pci_read_word1:
151
        in      ax,dx
152
        jmp pci_fin_read1
153
pci_read_dword1:
154
        in      eax,dx
155
        jmp     pci_fin_read1
156
pci_fin_read1:
157
                ; restore configuration control
158
        xchg    eax,[esp]
159
        mov     dx,0xcf8
160
        out     dx,eax
161
 
162
        pop     eax
163
        pop     esi
164
        ret
165
pci_read_reg_2:
166
 
167
        test    bh,128  ;mech#2 only supports 16 devices per bus
168
        jnz     pci_read_reg_err
169
 
170
        push esi   ; save register size into ESI
171
        mov esi,eax
172
        and esi,3
173
 
174
        push    eax
175
                ;store current state of config space
176
        mov     dx,0xcf8
177
        in      al,dx
178
        mov     ah,al
179
        mov     dl,0xfa
180
        in      al,dx
181
 
182
        xchg    eax,[esp]
183
                ; out 0xcfa,bus
184
        mov     al,ah
185
        out     dx,al
186
                ; out 0xcf8,0x80
187
        mov     dl,0xf8
188
        mov     al,0x80
189
        out     dx,al
190
                ; compute addr
191
        shr     bh,3 ; func is ignored in mechanism 2
192
        or      bh,0xc0
193
        mov     dx,bx
194
 
195
        or      esi,esi
196
        jz      pci_read_byte2
197
        cmp     esi,1
198
        jz      pci_read_word2
199
        cmp     esi,2
200
        jz      pci_read_dword2
201
        jmp     pci_fin_read2
202
 
203
pci_read_byte2:
204
        in      al,dx
205
        jmp pci_fin_read2
206
pci_read_word2:
207
        in      ax,dx
208
        jmp pci_fin_read2
209
pci_read_dword2:
210
        in      eax,dx
211
;       jmp pci_fin_read2
212
pci_fin_read2:
213
 
214
                ; restore configuration space
215
        xchg    eax,[esp]
216
        mov     dx,0xcfa
217
        out     dx,al
218
        mov     dl,0xf8
219
        mov     al,ah
220
        out     dx,al
221
 
222
        pop     eax
223
        pop     esi
224
        ret
225
 
226
pci_read_reg_err:
227
        xor     eax,eax
228
        dec     eax
229
        ret
230
 
231
 
232
;***************************************************************************
233
;   Function
234
;      pci_write_reg:
235
;
236
;   Description
237
;       write a register from ECX/CX/CL into the PCI config space
238
;       IN: ah=bus,device+func=bh,register address (dword aligned)=bl,
239
;           value to write in ecx
240
;           number of bytes to write (1,2,4) coded into AL, bits 0-1
241
;***************************************************************************
242
 
243
align 4
244
 
245
pci_write_reg:
246
        cmp byte [0x2F0000+0x9020],2 ;what mechanism will we use?
247
        je pci_write_reg_2
248
 
249
                ; mechanism 1
250
        push    esi   ; save register size into ESI
251
        mov     esi,eax
252
        and     esi,3
253
 
254
        call    pci_make_config_cmd
255
        mov     ebx,eax
256
                ; get current state into ecx
257
        mov     dx,0xcf8
258
        in      eax, dx
259
        push    eax
260
                ; set up addressing to config data
261
        mov     eax,ebx
262
        and     al,0xfc ; make address dword-aligned
263
        out     dx,eax
264
                ; write DWORD of config data
265
        mov     dl,0xfc
266
        and     bl,3
267
        or      dl,bl
268
        mov     eax,ecx
269
 
270
        or      esi,esi
271
        jz      pci_write_byte1
272
        cmp     esi,1
273
        jz      pci_write_word1
274
        cmp     esi,2
275
        jz      pci_write_dword1
276
        jmp     pci_fin_write1
277
 
278
pci_write_byte1:
279
        out     dx,al
280
        jmp pci_fin_write1
281
pci_write_word1:
282
        out     dx,ax
283
        jmp pci_fin_write1
284
pci_write_dword1:
285
        out     dx,eax
286
        jmp     pci_fin_write1
287
pci_fin_write1:
288
 
289
                ; restore configuration control
290
        pop     eax
291
        mov     dl,0xf8
292
        out     dx,eax
293
 
294
        xor     eax,eax
295
        pop     esi
296
 
297
        ret
298
pci_write_reg_2:
299
 
300
        test    bh,128  ;mech#2 only supports 16 devices per bus
301
        jnz     pci_write_reg_err
302
 
303
 
304
        push esi   ; save register size into ESI
305
        mov esi,eax
306
        and esi,3
307
 
308
        push    eax
309
                ;store current state of config space
310
        mov     dx,0xcf8
311
        in      al,dx
312
        mov     ah,al
313
        mov     dl,0xfa
314
        in      al,dx
315
        xchg    eax,[esp]
316
                ; out 0xcfa,bus
317
        mov     al,ah
318
        out     dx,al
319
                ; out 0xcf8,0x80
320
        mov     dl,0xf8
321
        mov     al,0x80
322
        out     dx,al
323
                ; compute addr
324
        shr     bh,3 ; func is ignored in mechanism 2
325
        or      bh,0xc0
326
        mov     dx,bx
327
                ; write register
328
        mov     eax,ecx
329
 
330
        or      esi,esi
331
        jz      pci_write_byte2
332
        cmp     esi,1
333
        jz      pci_write_word2
334
        cmp     esi,2
335
        jz      pci_write_dword2
336
        jmp     pci_fin_write2
337
 
338
pci_write_byte2:
339
        out     dx,al
340
        jmp pci_fin_write2
341
pci_write_word2:
342
        out     dx,ax
343
        jmp pci_fin_write2
344
pci_write_dword2:
345
        out     dx,eax
346
        jmp     pci_fin_write2
347
pci_fin_write2:
348
                ; restore configuration space
349
        pop     eax
350
        mov     dx,0xcfa
351
        out     dx,al
352
        mov     dl,0xf8
353
        mov     al,ah
354
        out     dx,al
355
 
356
        xor     eax,eax
357
        pop     esi
358
        ret
359
 
360
pci_write_reg_err:
361
        xor     eax,eax
362
        dec     eax
363
        ret