Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
1731 clevermous 1
; Stub of videodriver for Intel videocards.
2
; (c) CleverMouse
3
 
4
; When the start procedure gots control,
5
; it tries to detect preferred resolution,
6
; sets the detected resolution assuming 32-bpp VESA mode and exits
7
; (without registering a service).
8
; Detection can be overloaded with compile-time settings
9
; use_predefined_mode/predefined_width/predefined_height.
10
 
11
; set predefined resolution here
12
use_predefined_mode = 0;1
13
predefined_width = 0;1366
14
predefined_height = 0;768
15
 
16
; standard driver stuff
17
format MS COFF
18
 
19
DEBUG = 1
20
 
21
include 'proc32.inc'
22
include 'imports.inc'
23
 
24
public START
25
public version
26
 
27
section '.flat' code readable align 16
28
; the start procedure (see the description above)
29
START:
30
; 1. Detect device. Abort if not found.
31
	push	esi
32
	call	DetectDevice
33
	test	esi, esi
34
	jz	.return0
35
; 2. Detect optimal mode unless the mode is given explicitly. Abort if failed.
36
if use_predefined_mode = 0
37
	call	DetectMode
38
end if
39
	cmp	[width], 0
40
	jz	.return0_cleanup
41
; 3. Set the detected mode.
42
	call	SetMode
43
; 4. Cleanup and return.
44
.return0_cleanup:
45
	stdcall	FreeKernelSpace, esi
46
.return0:
47
	pop	esi
48
	xor	eax, eax
49
	ret	4
50
 
51
; check that there is Intel videocard
52
; if so, map MMIO registers and set internal variables
53
; esi points to MMIO block; NULL means no device
54
DetectDevice:
55
; 1. Sanity check: check that we are dealing with Intel videocard.
56
; Integrated video device for Intel is always at PCI:0:2:0.
57
	xor	esi, esi	; initialize return value to NULL
58
; 1a. Get PCI VendorID and DeviceID.
59
	push	esi
60
	push	10h
61
	push	esi
62
	call	PciRead32
63
; 1b. loword(eax) = ax = VendorID, hiword(eax) = DeviceID.
64
; Test whether we have Intel chipset.
65
	cmp	ax, 8086h
66
	jnz	.return
67
; 1c. Say hi including DeviceID.
68
	shr	eax, 10h
69
	push	edi
70
	pusha
71
	mov	edi, pciid_text
72
	call	WriteWord
73
	mov	esi, hellomsg
74
	call	SysMsgBoardStr
75
	popa
76
; 1d. Test whether we know this DeviceID.
77
; If this is the case, remember the position of the device in line of Intel cards;
78
; this knowledge will be useful later.
79
; Tested on devices with id: 8086:0046, partially 8086:2A02.
80
	mov	ecx, pciids_num
81
	mov	edi, pciids
82
	repnz	scasw
83
	pop	edi
84
	jnz	.return_unknown_pciid
85
	sub	ecx, pciids_num - 1
86
	neg	ecx
87
	mov	[deviceType], ecx
88
; 1e. Continue saying hi with positive intonation.
89
	pusha
90
	mov	esi, knownmsg
91
	call	SysMsgBoardStr
92
	popa
93
; 2. Prepare MMIO region to control the card.
94
; 2a. Read MMIO physical address from PCI config space.
95
	push	10h
96
	push	10h
97
	push	esi
98
	call	PciRead32
99
; 2b. Mask out PCI region type, lower 4 bits.
100
	and	al, not 0xF
101
; 2c. Create virtual mapping of the physical memory.
102
	push	1Bh
103
	push	100000h
104
	push	eax
105
	call	MapIoMem
106
; 3. Return.
107
	xchg	esi, eax
108
.return:
109
	ret
110
; 1f. If we do not know DeviceID, continue saying hi with negative intonation.
111
.return_unknown_pciid:
112
	pusha
113
	mov	esi, unknownmsg
114
	call	SysMsgBoardStr
115
	popa
116
	ret
117
 
118
; Convert word in ax to hexadecimal text in edi, advance edi.
119
WriteWord:
120
; 1. Convert high byte.
121
	push	eax
122
	mov	al, ah
123
	call	WriteByte
124
	pop	eax
125
; 2. Convert low byte.
126
; Fall through to WriteByte; ret from WriteByte is ret from WriteWord too.
127
 
128
; Convert byte in al to hexadecimal text in edi, advance edi.
129
WriteByte:
130
; 1. Convert high nibble.
131
	push	eax
132
	shr	al, 4
133
	call	WriteNibble
134
	pop	eax
135
; 2. Convert low nibble.
136
	and	al, 0xF
137
; Fall through to WriteNibble; ret from WriteNibble is ret from WriteByte too.
138
 
139
; Convert nibble in al to hexadecimal text in edi, advance edi.
140
WriteNibble:
141
; Obvious, isn't it?
142
	cmp	al, 10
143
	sbb	al, 69h
144
	das
145
	stosb
146
	ret
147
 
148
if use_predefined_mode = 0
149
; detect resolution of the flat panel
150
DetectMode:
151
	push	esi edi
152
; 1. Get the location of block of GMBUS* registers.
153
; Starting with Ironlake, GMBUS* registers were moved.
154
	add	esi, 5100h
155
	cmp	[deviceType], ironlake_start
156
	jb	@f
157
	add	esi, 0xC0000
158
@@:
159
; 2. Initialize GMBUS engine.
160
	mov	edi, edid
161
	mov	ecx, 0x10000
162
@@:
163
	test	byte [esi+8+1], 80h
164
	loopnz	@b
165
	jnz	.fail
166
	mov	dword [esi], 3
167
	test	byte [esi+8+1], 4
168
	jz	.noreset
169
	call	ResetGMBus
170
	jnz	.fail
171
.noreset:
172
; 3. Send read command.
173
	and	dword [esi+20h], 0
174
	mov	dword [esi+4], 4E8000A1h
175
; 4. Wait for data, writing to the buffer as data arrive.
176
.getdata:
177
	mov	ecx, 0x10000
178
@@:
179
	test	byte [esi+8+1], 8
180
	loopz	@b
181
	test	byte [esi+8+1], 4
182
	jz	.dataok
183
	call	ResetGMBus
184
	jmp	.fail
185
.dataok:
186
	mov	eax, [esi+0Ch]
187
	stosd
188
	cmp	edi, edid+80h
189
	jb	.getdata
190
; 5. Wait for bus idle.
191
	mov	ecx, 0x10000
192
@@:
193
	test	byte [esi+8+1], 2
194
	loopnz	@b
195
; 6. We got EDID; dump it if DEBUG.
196
if DEBUG
197
	pusha
198
	xor	ecx, ecx
199
	mov	esi, edid
200
	mov	edi, edid_text
201
.dumploop:
202
	lodsb
203
	call	WriteByte
204
	mov	al, ' '
205
	stosb
206
	inc	cl
207
	test	cl, 15
208
	jnz	@f
209
	mov	byte [edi-1], 13
210
	mov	al, 10
211
	stosb
212
@@:
213
	test	cl, cl
214
	jns	.dumploop
215
	mov	esi, edidmsg
216
	call	SysMsgBoardStr
217
	popa
218
end if
219
; 7. Test whether EDID is good.
220
; 7a. Signature: 00 FF FF FF FF FF FF 00.
221
	mov	esi, edid
222
	cmp	dword [esi], 0xFFFFFF00
223
	jnz	.fail
224
	cmp	dword [esi+4], 0x00FFFFFF
225
	jnz	.fail
226
; 7b. Checksum must be zero.
227
	xor	edx, edx
228
	mov	ecx, 80h
229
@@:
230
	lodsb
231
	add	dl, al
232
	loop	@b
233
	jnz	.fail
234
; 8. Get width and height from EDID.
235
	xor	eax, eax
236
	mov	ah, [esi-80h+3Ah]
237
	shr	ah, 4
238
	mov	al, [esi-80h+38h]
239
	mov	[width], eax
240
	mov	ah, [esi-80h+3Dh]
241
	shr	ah, 4
242
	mov	al, [esi-80h+3Bh]
243
	mov	[height], eax
244
; 9. Return.
245
.fail:
246
	pop	edi esi
247
	ret
248
 
249
; reset bus, clear all errors
250
ResetGMBus:
251
; look into the PRM
252
	mov	dword [esi+4], 80000000h
253
	mov	dword [esi+4], 0
254
	mov	ecx, 0x10000
255
@@:
256
	test	byte [esi+8+1], 2
257
	loopnz	@b
258
	ret
259
end if
260
 
261
; set resolution [width]*[height]
262
SetMode:
263
; 1. Program the registers of videocard.
264
; look into the PRM
265
	cli
1750 clevermous 266
;	or	byte [esi+7000Ah], 0Ch	; PIPEACONF: disable Display+Cursor Planes
267
;	or	byte [esi+7100Ah], 0Ch	; PIPEBCONF: disable Display+Cursor Planes
268
	xor	eax, eax
269
	xor	edx, edx
270
	cmp	[deviceType], i965_start
271
	jb	@f
272
	mov	dl, 9Ch - 84h
273
@@:
274
;	or	byte [esi+71403h], 80h	; VGACNTRL: VGA Display Disable
275
	and	byte [esi+70080h], not 27h	; CURACNTR: disable cursor A
276
	mov	dword [esi+70084h], eax	; CURABASE: force write to CURA* regs
277
	and	byte [esi+700C0h], not 27h	; CURBCNTR: disable cursor B
278
	mov	dword [esi+700C4h], eax	; CURBBASE: force write to CURB* regs
279
	and	byte [esi+70183h], not 80h	; DSPACNTR: disable Primary A Plane
280
	mov	dword [esi+edx+70184h], eax	; DSPALINOFF/DSPASURF: force write to DSPA* regs
281
	and	byte [esi+71183h], not 80h	; DSPBCNTR: disable Primary B Plane
282
	mov	dword [esi+edx+71184h], eax	; DSPBLINOFF/DSPBSURF: force write to DSPB* regs
283
if 1
284
	cmp	[deviceType], ironlake_start
285
	jae	.disable_pipes
286
	mov	edx, 10000h
287
	or	byte [esi+70024h], 2	; PIPEASTAT: clear VBLANK status
288
	or	byte [esi+71024h], 2	; PIPEBSTAT: clear VBLANK status
289
.wait_vblank_preironlake1:
290
	mov	ecx, 1000h
291
	loop	$
292
	test	byte [esi+7000Bh], 80h		; PIPEACONF: pipe A active?
293
	jz	@f
294
	test	byte [esi+70024h], 2		; PIPEASTAT: got VBLANK?
295
	jz	.wait_vblank_preironlake2
296
@@:
297
	test	byte [esi+7100Bh], 80h		; PIPEBCONF: pipe B active?
298
	jz	.disable_pipes
299
	test	byte [esi+71024h], 2		; PIPEBSTAT: got VBLANK?
300
	jnz	.disable_pipes
301
.wait_vblank_preironlake2:
302
	dec	edx
303
	jnz	.wait_vblank_preironlake1
304
	jmp	.not_disabled
305
.disable_pipes:
306
end if
1731 clevermous 307
	and	byte [esi+7000Bh], not 80h	; PIPEACONF: disable pipe
308
	and	byte [esi+7100Bh], not 80h	; PIPEBCONF: disable pipe
1750 clevermous 309
if 1
1731 clevermous 310
	mov	edx, 10000h
311
@@:
312
	mov	ecx, 1000h
313
	loop	$
314
	test	byte [esi+7000Bh], 40h	; PIPEACONF: wait until pipe disabled
315
	jz	@f
316
	dec	edx
317
	jnz	@b
318
.not_disabled:
319
	sti
320
	jmp	.return
321
@@:
322
	test	byte [esi+7100Bh], 40h	; PIPEBCONF: wait until pipe disabled
323
	jz	@f
324
	mov	ecx, 1000h
325
	loop	$
326
	dec	edx
327
	jnz	@b
328
	jmp	.not_disabled
329
@@:
1750 clevermous 330
else
331
; alternative way of waiting for pipe stop, works too
332
	mov	edx, 1000h
333
.dis1:
334
	push	dword [esi+71000h]
335
	push	dword [esi+70000h]
336
	mov	ecx, 10000h
337
	loop	$
338
	pop	eax
339
	xor	eax, [esi+70000h]
340
	and	eax, 1FFFh
341
	pop	eax
342
	jnz	.notdis1
343
	xor	eax, [esi+71000h]
344
	and	eax, 1FFFh
345
	jz	.disabled
346
.notdis1:
347
	dec	edx
348
	jnz	.dis1
349
.not_disabled:
350
	sti
351
	jmp	.return
352
.disabled:
353
end if
1731 clevermous 354
	lea	eax, [esi+61183h]
355
	cmp	[deviceType], ironlake_start
356
	jb	@f
357
	add	eax, 0xE0000 - 0x60000
358
@@:
359
	lea	edx, [esi+60000h]
360
	test	byte [eax], 40h
361
	jz	@f
362
	add	edx, 1000h
363
@@:
364
	mov	eax, [width]
365
	dec	eax
366
	shl	eax, 16
367
	mov	ax, word [height]
368
	dec	eax
369
	mov	dword [edx+1Ch], eax	; PIPEASRC: set source image size
370
	ror	eax, 16
371
	mov	dword [edx+10190h], eax	; for old cards
372
	mov	ecx, [width]
373
	add	ecx, 15
374
	and	ecx, not 15
375
	shl	ecx, 2
376
	mov	dword [edx+10188h], ecx	; DSPASTRIDE: set scanline length
377
	and	byte [esi+61233h], not 80h	; PFIT_CONTROL: disable panel fitting
1750 clevermous 378
	or	byte [edx+1000Bh], 80h		; PIPEACONF: enable pipe
379
;	and	byte [edx+1000Ah], not 0Ch	; PIPEACONF: enable Display+Cursor Planes
1731 clevermous 380
	or	byte [edx+10183h], 80h		; DSPACNTR: enable Display Plane A
381
	sti
382
; 2. Notify the kernel that resolution has changed.
383
	call	GetDisplay
384
	mov	edx, [width]
385
	mov	dword [eax+8], edx
386
	mov	edx, [height]
387
	mov	dword [eax+0Ch], edx
388
	mov	[eax+18h], ecx
389
	mov	eax, [width]
390
	dec	eax
391
	dec	edx
392
	call	SetScreen
393
.return:
394
	ret
395
 
396
align 4
397
hellomsg	db	'Intel videocard detected, PciId=8086:'
398
pciid_text	db	'0000'
399
		db	', which is ', 0
400
knownmsg	db	'known',13,10,0
401
unknownmsg	db	'unknown',13,10,0
402
 
403
if DEBUG
404
edidmsg		db	'EDID successfully read:',13,10
405
edid_text	rb	8*(16*3+1)
406
		db	0
407
end if
408
 
409
version:
410
	dd	0x50005
411
 
412
width	dd	predefined_width
413
height	dd	predefined_height
414
 
415
pciids:
416
	dw	0x2582	; i915g
417
	dw	0x258a	; i915g
418
	dw	0x2592	; i915gm
419
	dw	0x2772	; i945g
420
	dw	0x27a2	; i945gm
421
	dw	0x27ae	; i945gm
1750 clevermous 422
i965_start = ($ - pciids) / 2
1731 clevermous 423
	dw	0x2972	; i965g
424
	dw	0x2982	; i965g
425
	dw	0x2992	; i965g
426
	dw	0x29a2	; i965g
427
	dw	0x29b2	; g33
428
	dw	0x29c2	; g33
429
	dw	0x29d2	; g33
430
	dw	0x2a02	; i965gm
431
	dw	0x2a12	; i965gm
432
	dw	0x2a42	; gm45
433
	dw	0x2e02	; g45
434
	dw	0x2e12	; g45
435
	dw	0x2e22	; g45
436
	dw	0x2e32	; g45
437
	dw	0x2e42	; g45
438
	dw	0x2e92	; g45
439
	dw	0xa001	; pineview
440
	dw	0xa011	; pineview
441
ironlake_start = ($ - pciids) / 2
442
	dw	0x0042	; ironlake_d
443
	dw	0x0046	; ironlake_m
444
	dw	0x0102	; sandybridge_d
445
	dw	0x0112	; sandybridge_d
446
	dw	0x0122	; sandybridge_d
447
	dw	0x0106	; sandybridge_m
448
	dw	0x0116	; sandybridge_m
449
	dw	0x0126	; sandybridge_m
450
	dw	0x010A	; sandybridge_d
451
pciids_num = ($ - pciids) / 2
452
 
453
align 4
454
deviceType	dd	?
455
edid	rb	0x80