source: xtideuniversalbios/trunk/XTIDE_Universal_BIOS_Configurator_v2/Src/IdeAutodetect.asm @ 502

Last change on this file since 502 was 502, checked in by aitotat@…, 11 years ago

Changes to Configurator v2:

  • XT-CF port can be selected again.
  • Auto Configure should now detect Sound Blaster 16 Tertiary and Quaternary IDE.
  • Auto Configure now properly sets Slave Drive variables.
File size: 8.9 KB
Line 
1; Project name  :   XTIDE Universal BIOS Configurator v2
2; Description   :   Functions to detect ports and devices.
3
4;
5; XTIDE Universal BIOS and Associated Tools
6; Copyright (C) 2009-2010 by Tomi Tilli, 2011-2012 by XTIDE Universal BIOS Team.
7;
8; This program is free software; you can redistribute it and/or modify
9; it under the terms of the GNU General Public License as published by
10; the Free Software Foundation; either version 2 of the License, or
11; (at your option) any later version.
12;
13; This program is distributed in the hope that it will be useful,
14; but WITHOUT ANY WARRANTY; without even the implied warranty of
15; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16; GNU General Public License for more details.
17; Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18;
19
20; Section containing code
21SECTION .text
22
23;--------------------------------------------------------------------
24; IdeAutodetect_DetectIdeDeviceFromPortDXAndReturnControlBlockInCX
25;   Parameters:
26;       DX:     IDE Base Port
27;       DS:DI:  Ptr to ROMVARS
28;   Returns:
29;       AL:     Device Type
30;       CX:     Control Block Base port (detected since there is no
31;               standard address for Tetriary and Quaternary IDE controllers)
32;       CF:     Clear if IDE Device found
33;               Set if IDE Device not found
34;   Corrupts registers:
35;       AH, BX
36;--------------------------------------------------------------------
37IdeAutodetect_DetectIdeDeviceFromPortDXAndReturnControlBlockInCX:
38    cmp     dx, FIRST_MEMORY_SEGMENT_ADDRESS
39    jb      SHORT .DetectPortMappedDevices
40
41    ; *** Try to detect JR-IDE/ISA (only if MODULE_8BIT_IDE_ADVANCED is present) ***
42    test    WORD [di+ROMVARS.wFlags], FLG_ROMVARS_MODULE_8BIT_IDE_ADVANCED
43    jz      SHORT .SkipRestOfDetection
44
45    push    ds
46    mov     ds, dx
47    cli                                 ; Disable Interrupts
48    mov     ah, [JRIDE_COMMAND_BLOCK_REGISTER_WINDOW_OFFSET + STATUS_REGISTER_in]
49    mov     al, [JRIDE_CONTROL_BLOCK_REGISTER_WINDOW_OFFSET + ALTERNATE_STATUS_REGISTER_in]
50    sti                                 ; Enable Interrupts
51    pop     ds
52    call    CompareIdeStatusRegistersFromALandAH
53    mov     al, DEVICE_8BIT_JRIDE_ISA   ; Assume CF was cleared
54    ret                                 ; No need to return Control Block Port
55.DetectPortMappedDevices:
56
57
58    ; *** Try to detect Standard 16- and 32-bit IDE Devices ***
59    mov     bh, DEVICE_16BIT_ATA        ; Assume 16-bit ISA slot for AT builds
60    call    Buffers_IsXTbuildLoaded
61    eCMOVE  bh, DEVICE_8BIT_ATA         ; Assume 8-bit ISA slot for XT builds
62
63    mov     bl, STATUS_REGISTER_in
64    mov     cx, STANDARD_CONTROL_BLOCK_OFFSET + ALTERNATE_STATUS_REGISTER_in
65.RedetectTertiaryOrQuaternaryWithDifferentAlternativeStatusRegisterPort:
66    call    DetectIdeDeviceFromPortDXwithStatusRegOffsetsInBLandCX
67    mov     al, bh
68    jnc     SHORT .IdeDeviceFound
69
70    ; 16- or 32-bit IDE Device was not found but we may have used wrong Control Block port if we were trying
71    ; to detect Tertiary or Quaternary IDE controllers. Control Block port location is not standardized. For
72    ; example Promise FloppyMAX has Control Block at STANDARD_CONTROL_BLOCK_OFFSET but Sound Blaster 16 (CT2290)
73    ; use DEVICE_ATA_SECONDARY_PORTCTRL for Tertiary and Quaternary even though only Secondary should use that.
74    cmp     cx, STANDARD_CONTROL_BLOCK_OFFSET + ALTERNATE_STATUS_REGISTER_in
75    jne     SHORT .AlreadyTriedAlternativeControlBlock
76    mov     cx, DEVICE_ATA_SECONDARY_PORTCTRL + ALTERNATE_STATUS_REGISTER_in
77    sub     cx, dx                      ; Offset to add to DX
78    cmp     dx, DEVICE_ATA_TERTIARY_PORT
79    je      SHORT .RedetectTertiaryOrQuaternaryWithDifferentAlternativeStatusRegisterPort
80    cmp     dx, DEVICE_ATA_QUATERNARY_PORT
81    je      SHORT .RedetectTertiaryOrQuaternaryWithDifferentAlternativeStatusRegisterPort
82.AlreadyTriedAlternativeControlBlock:
83
84
85    ; Detect 8-bit devices only if MODULE_8BIT_IDE is available
86    test    BYTE [di+ROMVARS.wFlags], FLG_ROMVARS_MODULE_8BIT_IDE
87    jz      SHORT .SkipRestOfDetection
88
89    ; *** Try to detect XT-CF ***
90    mov     bl, STATUS_REGISTER_in << 1
91    mov     cx, (XTIDE_CONTROL_BLOCK_OFFSET + ALTERNATE_STATUS_REGISTER_in) << 1
92    call    DetectIdeDeviceFromPortDXwithStatusRegOffsetsInBLandCX
93    rcl     ax, 1                       ; Store CF
94    shr     cx, 1                       ; XTIDE_CONTROL_BLOCK_OFFSET + ALTERNATE_STATUS_REGISTER_in
95    rcr     ax, 1                       ; Restore CF
96    mov     al, DEVICE_8BIT_XTCF_PIO8
97    jnc     SHORT .IdeDeviceFound
98
99
100    ; *** Try to detect 8-bit XT-IDE rev 1 or rev 2 ***
101    ; Note that A0<->A3 address swaps Status Register and Alternative
102    ; Status Register addresses. That is why we need another step
103    ; to check is this XT-IDE rev 1 or rev 2.
104    call    DetectIdeDeviceFromPortDXwithStatusRegOffsetsInBLandCX
105    jc      SHORT .SkipRestOfDetection  ; No XT-IDE rev 1 or rev 2 found
106
107    ; Now we can be sure that we have XT-IDE rev 1 or rev 2.
108    ; Rev 2 swaps address lines A0 and A3 thus LBA Low Register
109    ; moves from offset 3h to offset Ah. There is no Register at
110    ; offset Ah so if we can write to it and read back, then we
111    ; must have XT-IDE rev 2 or modded rev 1.
112    push    dx
113    add     dx, BYTE 0Ah                ; LBA Low Register for XT-IDE rev 2
114    mov     al, DEVICE_8BIT_XTIDE_REV2  ; Our test byte
115    out     dx, al                      ; Output our test byte
116    JMP_DELAY
117    in      al, dx                      ; Read back
118    pop     dx
119    cmp     al, DEVICE_8BIT_XTIDE_REV2
120    je      SHORT .IdeDeviceFound
121    mov     al, DEVICE_8BIT_XTIDE_REV1  ; We must have rev 1
122.IdeDeviceFound:
123    sub     cl, ALTERNATE_STATUS_REGISTER_in    ; Clear CF
124    add     cx, dx                              ; CX = Control Block address
125    ret
126.SkipRestOfDetection:
127    stc
128    ret
129
130
131;--------------------------------------------------------------------
132; DetectIdeDeviceFromPortDXwithStatusRegOffsetsInBLandCX
133;   Parameters:
134;       BL:     Offset to IDE Status Register
135;       CX:     Offset to Alternative Status Register
136;       DX:     IDE Base Port
137;   Returns:
138;       CF:     Clear if IDE Device found
139;               Set if IDE Device not found
140;   Corrupts registers:
141;       AX
142;--------------------------------------------------------------------
143DetectIdeDeviceFromPortDXwithStatusRegOffsetsInBLandCX:
144    ; Read Status and Alternative Status Registers
145    push    cx
146    push    dx
147
148    add     cx, dx              ; CX = Address to Alternative Status Register
149    add     dl, bl              ; DX = Address to Status Register
150    cli                         ; Disable Interrupts
151    in      al, dx              ; Read Status Register
152    mov     ah, al
153    mov     dx, cx
154    in      al, dx              ; Read Alternative Status Register
155    sti                         ; Enable Interrupts
156
157    pop     dx
158    pop     cx
159    ; Fall to CompareIdeStatusRegistersFromALandAH
160
161
162;--------------------------------------------------------------------
163; CompareIdeStatusRegistersFromALandAH
164;   Parameters:
165;       AL:     Possible IDE Status Register contents
166;       AH:     Possible IDE Alternative Status Register contents
167;   Returns:
168;       CF:     Clear if valid Status Register Contents
169;               Set if not possible IDE Status Registers
170;   Corrupts registers:
171;       Nothing
172;--------------------------------------------------------------------
173CompareIdeStatusRegistersFromALandAH:
174    ; Status Register now in AH and Alternative Status Register in AL.
175    ; They must be the same if base port was in use by IDE device.
176    cmp     al, ah
177    jne     SHORT .InvalidStatusRegister
178
179    ; Bytes were the same but it is possible they were both FFh, for 
180    ; example. We must make sure bits are what is expected from valid
181    ; IDE Status Register. So far all drives I've tested return 50h
182    ; (FLG_STATUS_DRDY and FLG_STATUS_DSC set) but I don't want to assume
183    ; just yet that all drives report 50h.
184    test    al, FLG_STATUS_BSY | FLG_STATUS_DF | FLG_STATUS_DRQ | FLG_STATUS_ERR
185    jnz     SHORT .InvalidStatusRegister    ; Busy or Errors cannot be set
186    test    al, FLG_STATUS_DRDY
187    jz      SHORT .InvalidStatusRegister    ; Device needs to be ready
188    ret                                     ; Return with CF cleared
189
190.InvalidStatusRegister:
191AllPortsAlreadyDetected:
192    stc
193    ret
194
195
196;--------------------------------------------------------------------
197; IdeAutodetect_IncrementDXtoNextIdeBasePort
198;   Parameters:
199;       DX:     Previous IDE Base Port
200;   Returns:
201;       DX:     Next IDE Base Port
202;       ZF:     Set if no more Base Ports (DX was last base port on entry)
203;               Clear if new base port returned in DX
204;   Corrupts registers:
205;       AX
206;--------------------------------------------------------------------
207ALIGN JUMP_ALIGN
208IdeAutodetect_IncrementDXtoNextIdeBasePort:
209    cmp     dx, [cs:.wLastIdePort]
210    je      SHORT .AllPortsAlreadyDetected
211
212    push    si
213    mov     si, .rgwIdeBasePorts
214.CompareNextIdeBasePort:
215    cmp     [cs:si], dx
216    lea     si, [si+2]  ; Increment SI and preserve FLAGS
217    jne     SHORT .CompareNextIdeBasePort
218
219    mov     dx, [cs:si]         ; Get next port
220    test    dx, dx              ; Clear ZF
221    pop     si
222.AllPortsAlreadyDetected:
223    ret
224
225
226    ; All ports used in autodetection. Ports can be in any order.
227ALIGN WORD_ALIGN
228.rgwIdeBasePorts:
229    dw      IDE_PORT_TO_START_DETECTION     ; Must be first
230    ; Standard IDE
231    dw      DEVICE_ATA_PRIMARY_PORT
232    dw      DEVICE_ATA_SECONDARY_PORT
233    dw      DEVICE_ATA_TERTIARY_PORT
234    dw      DEVICE_ATA_QUATERNARY_PORT
235    ; 8-bit Devices
236    dw      200h
237    dw      220h
238    dw      240h
239    dw      260h
240    dw      280h
241    dw      2A0h
242    dw      2C0h
243    dw      2E0h
244    dw      300h
245    dw      320h
246    dw      340h
247    dw      360h
248    dw      380h
249    dw      3A0h
250    dw      3C0h
251    dw      3E0h
252    ; JR-IDE/ISA (Memory Segment Addresses)
253    dw      0C000h
254    dw      0C400h
255    dw      0C800h
256    dw      0CC00h
257    dw      0D000h
258    dw      0D400h
259    dw      0D800h
260.wLastIdePort:
261    dw      0DC00h
Note: See TracBrowser for help on using the repository browser.