Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  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
  266.         or      byte [esi+7000Ah], 0Ch  ; PIPEACONF: disable Display+Cursor Planes
  267.         or      byte [esi+7100Ah], 0Ch  ; PIPEBCONF: disable Display+Cursor Planes
  268.         and     byte [esi+7000Bh], not 80h      ; PIPEACONF: disable pipe
  269.         and     byte [esi+7100Bh], not 80h      ; PIPEBCONF: disable pipe
  270. ;       or      byte [esi+71403h], 80h  ; VGACNTRL: VGA Display Disable
  271.         mov     edx, 10000h
  272. @@:
  273.         mov     ecx, 1000h
  274.         loop    $
  275.         test    byte [esi+7000Bh], 40h  ; PIPEACONF: wait until pipe disabled
  276.         jz      @f
  277.         dec     edx
  278.         jnz     @b
  279. .not_disabled:
  280.         sti
  281.         jmp     .return
  282. @@:
  283.         test    byte [esi+7100Bh], 40h  ; PIPEBCONF: wait until pipe disabled
  284.         jz      @f
  285.         mov     ecx, 1000h
  286.         loop    $
  287.         dec     edx
  288.         jnz     @b
  289.         jmp     .not_disabled
  290. @@:
  291.         lea     eax, [esi+61183h]
  292.         cmp     [deviceType], ironlake_start
  293.         jb      @f
  294.         add     eax, 0xE0000 - 0x60000
  295. @@:
  296.         lea     edx, [esi+60000h]
  297.         test    byte [eax], 40h
  298.         jz      @f
  299.         add     edx, 1000h
  300. @@:
  301.         mov     eax, [width]
  302.         dec     eax
  303.         shl     eax, 16
  304.         mov     ax, word [height]
  305.         dec     eax
  306.         mov     dword [edx+1Ch], eax    ; PIPEASRC: set source image size
  307.         ror     eax, 16
  308.         mov     dword [edx+10190h], eax ; for old cards
  309.         mov     ecx, [width]
  310.         add     ecx, 15
  311.         and     ecx, not 15
  312.         shl     ecx, 2
  313.         mov     dword [edx+10188h], ecx ; DSPASTRIDE: set scanline length
  314.         and     byte [esi+61233h], not 80h      ; PFIT_CONTROL: disable panel fitting
  315.         or      byte [edx+10183h], 80h          ; DSPACNTR: enable Display Plane A
  316.         or      byte [edx+1000Bh], 80h          ; PIPEACONF: enable pipe
  317.         and     byte [edx+1000Ah], not 0Ch      ; PIPEACONF: enable Display+Cursor Planes
  318.         sti
  319. ; 2. Notify the kernel that resolution has changed.
  320.         call    GetDisplay
  321.         mov     edx, [width]
  322.         mov     dword [eax+8], edx
  323.         mov     edx, [height]
  324.         mov     dword [eax+0Ch], edx
  325.         mov     [eax+18h], ecx
  326.         mov     eax, [width]
  327.         dec     eax
  328.         dec     edx
  329.         call    SetScreen
  330. .return:
  331.         ret
  332.  
  333. align 4
  334. hellomsg        db      'Intel videocard detected, PciId=8086:'
  335. pciid_text      db      '0000'
  336.                 db      ', which is ', 0
  337. knownmsg        db      'known',13,10,0
  338. unknownmsg      db      'unknown',13,10,0
  339.  
  340. if DEBUG
  341. edidmsg         db      'EDID successfully read:',13,10
  342. edid_text       rb      8*(16*3+1)
  343.                 db      0
  344. end if
  345.  
  346. version:
  347.         dd      0x50005
  348.  
  349. width   dd      predefined_width
  350. height  dd      predefined_height
  351.  
  352. pciids:
  353.         dw      0x2582  ; i915g
  354.         dw      0x258a  ; i915g
  355.         dw      0x2592  ; i915gm
  356.         dw      0x2772  ; i945g
  357.         dw      0x27a2  ; i945gm
  358.         dw      0x27ae  ; i945gm
  359.         dw      0x2972  ; i965g
  360.         dw      0x2982  ; i965g
  361.         dw      0x2992  ; i965g
  362.         dw      0x29a2  ; i965g
  363.         dw      0x29b2  ; g33
  364.         dw      0x29c2  ; g33
  365.         dw      0x29d2  ; g33
  366.         dw      0x2a02  ; i965gm
  367.         dw      0x2a12  ; i965gm
  368.         dw      0x2a42  ; gm45
  369.         dw      0x2e02  ; g45
  370.         dw      0x2e12  ; g45
  371.         dw      0x2e22  ; g45
  372.         dw      0x2e32  ; g45
  373.         dw      0x2e42  ; g45
  374.         dw      0x2e92  ; g45
  375.         dw      0xa001  ; pineview
  376.         dw      0xa011  ; pineview
  377. ironlake_start = ($ - pciids) / 2
  378.         dw      0x0042  ; ironlake_d
  379.         dw      0x0046  ; ironlake_m
  380.         dw      0x0102  ; sandybridge_d
  381.         dw      0x0112  ; sandybridge_d
  382.         dw      0x0122  ; sandybridge_d
  383.         dw      0x0106  ; sandybridge_m
  384.         dw      0x0116  ; sandybridge_m
  385.         dw      0x0126  ; sandybridge_m
  386.         dw      0x010A  ; sandybridge_d
  387. pciids_num = ($ - pciids) / 2
  388.  
  389. align 4
  390. deviceType      dd      ?
  391. edid    rb      0x80
  392.