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

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

Changes to Configurator v2:

  • Auto Configure now detects XT-IDE rev 1 and rev 2 correctly.
File size: 7.3 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_DetectIdeDeviceFromPortDX
25;   Parameters:
26;       DX:     IDE Base Port
27;       DS:DI:  Ptr to ROMVARS
28;   Returns:
29;       AL:     Device Type
30;       CF:     Clear if IDE Device found
31;               Set if IDE Device not found
32;   Corrupts registers:
33;       AH, BX, CX
34;--------------------------------------------------------------------
35IdeAutodetect_DetectIdeDeviceFromPortDX:
36    cmp     dx, FIRST_MEMORY_SEGMENT_ADDRESS
37    jb      SHORT .DetectPortMappedDevices
38
39    ; Try to detect JR-IDE/ISA (only if MODULE_8BIT_IDE_ADVANCED is present)
40    test    WORD [di+ROMVARS.wFlags], FLG_ROMVARS_MODULE_8BIT_IDE_ADVANCED
41    jz      SHORT .SkipRestOfDetection
42
43    push    ds
44    mov     ds, dx
45    cli                         ; Disable Interrupts
46    mov     ah, [JRIDE_COMMAND_BLOCK_REGISTER_WINDOW_OFFSET + STATUS_REGISTER_in]
47    mov     al, [JRIDE_CONTROL_BLOCK_REGISTER_WINDOW_OFFSET + ALTERNATE_STATUS_REGISTER_in]
48    sti                         ; Enable Interrupts
49    pop     ds
50    call    CompareIdeStatusRegistersFromALandAH
51    mov     al, DEVICE_8BIT_JRIDE_ISA
52    ret
53.DetectPortMappedDevices:
54
55
56    ; Try to detect Standard 16- and 32-bit IDE Devices
57    mov     bh, DEVICE_16BIT_ATA        ; Assume 16-bit ISA slot for AT builds
58    call    Buffers_IsXTbuildLoaded
59    eCMOVE  bh, DEVICE_8BIT_ATA         ; Assume 8-bit ISA slot for XT builds
60    mov     bl, STATUS_REGISTER_in
61    mov     cx, STANDARD_CONTROL_BLOCK_OFFSET + ALTERNATE_STATUS_REGISTER_in
62    call    DetectIdeDeviceFromPortDXwithStatusRegOffsetsInBLandCX
63    mov     al, bh
64    jnc     SHORT .IdeDeviceFound
65
66
67    ; Detect 8-bit devices only if MODULE_8BIT_IDE is available
68    test    BYTE [di+ROMVARS.wFlags], FLG_ROMVARS_MODULE_8BIT_IDE
69    jz      SHORT .SkipRestOfDetection
70
71    ; Try to detect XT-CF
72    mov     bl, STATUS_REGISTER_in << 1
73    mov     cx, (XTIDE_CONTROL_BLOCK_OFFSET + ALTERNATE_STATUS_REGISTER_in) << 1
74    call    DetectIdeDeviceFromPortDXwithStatusRegOffsetsInBLandCX
75    mov     al, DEVICE_8BIT_XTCF_PIO8
76    jnc     SHORT .IdeDeviceFound
77
78    ; Try to detect 8-bit XT-IDE rev 1 or rev 2.
79    ; Note that A0<->A3 address swaps Status Register and Alternative
80    ; Status Register addresses. That is why we need another step
81    ; to check is this XT-IDE rev 1 or rev 2.
82    shr     cx, 1
83    call    DetectIdeDeviceFromPortDXwithStatusRegOffsetsInBLandCX
84    jc      SHORT .SkipRestOfDetection  ; No XT-IDE rev 1 or rev 2 found
85
86    ; Now we can be sure that we have XT-IDE rev 1 or rev 2.
87    ; Rev 2 swaps address lines A0 and A3 thus LBA Low Register
88    ; moves from offset 3h to offset Ah. There is no Register at
89    ; offset Ah so if we can write to it and read back, then we
90    ; must have XT-IDE rev 2 or modded rev 1.
91    push    dx
92    add     dx, BYTE 0Ah                ; LBA Low Register for XT-IDE rev 2
93    mov     al, DEVICE_8BIT_XTIDE_REV2  ; Our test byte
94    out     dx, al                      ; Output our test byte
95    JMP_DELAY
96    in      al, dx                      ; Read back
97    pop     dx
98    cmp     al, DEVICE_8BIT_XTIDE_REV2
99    je      SHORT .IdeDeviceFound
100    mov     al, DEVICE_8BIT_XTIDE_REV1  ; We must have rev 1
101    clc
102.IdeDeviceFound:
103    ret
104.SkipRestOfDetection:
105    stc
106    ret
107
108
109;--------------------------------------------------------------------
110; DetectIdeDeviceFromPortDXwithStatusRegOffsetsInBLandCX
111;   Parameters:
112;       BL:     Offset to IDE Status Register
113;       CX:     Offset to Alternative Status Register
114;       DX:     IDE Base Port
115;   Returns:
116;       CF:     Clear if IDE Device found
117;               Set if IDE Device not found
118;   Corrupts registers:
119;       AX
120;--------------------------------------------------------------------
121DetectIdeDeviceFromPortDXwithStatusRegOffsetsInBLandCX:
122    ; Read Status and Alternative Status Registers
123    push    cx
124    push    dx
125
126    add     cx, dx              ; CX = Address to Alternative Status Register
127    add     dl, bl              ; DX = Address to Status Register
128    cli                         ; Disable Interrupts
129    in      al, dx              ; Read Status Register
130    mov     ah, al
131    mov     dx, cx
132    in      al, dx              ; Read Alternative Status Register
133    sti                         ; Enable Interrupts
134
135    pop     dx
136    pop     cx
137    ; Fall to CompareIdeStatusRegistersFromALandAH
138
139
140;--------------------------------------------------------------------
141; CompareIdeStatusRegistersFromALandAH
142;   Parameters:
143;       AL:     Possible IDE Status Register contents
144;       AH:     Possible IDE Alternative Status Register contents
145;   Returns:
146;       CF:     Clear if valid Status Register Contents
147;               Set if not possible IDE Status Registers
148;   Corrupts registers:
149;       Nothing
150;--------------------------------------------------------------------
151CompareIdeStatusRegistersFromALandAH:
152    ; Status Register now in AH and Alternative Status Register in AL.
153    ; They must be the same if base port was in use by IDE device.
154    cmp     al, ah
155    jne     SHORT .InvalidStatusRegister
156
157    ; Bytes were the same but it is possible they were both FFh, for 
158    ; example. We must make sure bit are what is expected from valid
159    ; IDE Status Register.
160    test    al, FLG_STATUS_BSY | FLG_STATUS_DF | FLG_STATUS_DRQ | FLG_STATUS_ERR
161    jnz     SHORT .InvalidStatusRegister    ; Busy or Errors cannot be set
162    test    al, FLG_STATUS_DRDY
163    jz      SHORT .InvalidStatusRegister    ; Device needs to be ready
164    ret                                     ; Return with CF cleared
165
166.InvalidStatusRegister:
167AllPortsAlreadyDetected:
168    stc
169    ret
170
171
172;--------------------------------------------------------------------
173; IdeAutodetect_IncrementDXtoNextIdeBasePort
174;   Parameters:
175;       DX:     Previous IDE Base Port
176;   Returns:
177;       DX:     Next IDE Base Port
178;       ZF:     Set if no more Base Ports (DX was last base port on entry)
179;               Clear if new base port returned in DX
180;   Corrupts registers:
181;       AX
182;--------------------------------------------------------------------
183ALIGN JUMP_ALIGN
184IdeAutodetect_IncrementDXtoNextIdeBasePort:
185    cmp     dx, [cs:.wLastIdePort]
186    je      SHORT .AllPortsAlreadyDetected
187
188    push    si
189    mov     si, .rgwIdeBasePorts
190.CompareNextIdeBasePort:
191    cmp     [cs:si], dx
192    lea     si, [si+2]  ; Increment SI and preserve FLAGS
193    jne     SHORT .CompareNextIdeBasePort
194
195    mov     dx, [cs:si]         ; Get next port
196    test    dx, dx              ; Clear ZF
197    pop     si
198.AllPortsAlreadyDetected:
199    ret
200
201
202    ; All ports used in autodetection. Ports can be in any order.
203ALIGN WORD_ALIGN
204.rgwIdeBasePorts:
205    dw      IDE_PORT_TO_START_DETECTION     ; Must be first
206    ; Standard IDE
207    dw      DEVICE_ATA_PRIMARY_PORT
208    dw      DEVICE_ATA_SECONDARY_PORT
209    dw      DEVICE_ATA_TERTIARY_PORT
210    dw      DEVICE_ATA_QUATERNARY_PORT
211    ; 8-bit Devices
212    dw      200h
213    dw      220h
214    dw      240h
215    dw      260h
216    dw      280h
217    dw      2A0h
218    dw      2C0h
219    dw      2E0h
220    dw      300h
221    dw      320h
222    dw      340h
223    dw      360h
224    dw      380h
225    dw      3A0h
226    dw      3C0h
227    dw      3E0h
228    ; JR-IDE/ISA (Memory Segment Addresses)
229    dw      0C000h
230    dw      0C400h
231    dw      0C800h
232    dw      0CC00h
233    dw      0D000h
234    dw      0D400h
235    dw      0D800h
236.wLastIdePort:
237    dw      0DC00h
Note: See TracBrowser for help on using the repository browser.