source: xtideuniversalbios/trunk/XTIDE_Universal_BIOS/Src/Device/IDE/IdeCommand.asm@ 630

Last change on this file since 630 was 630, checked in by Krister Nordvall, 8 days ago

Changes:

  • Changed the g_szPCFlashSuccessful string in XTIDECFG to reflect the reality - it turns out ANY key was limited to just ENTER or ESC.
  • Removed the NEC V-specific optimization I added in r602 because NEC's documentation completely fails to mention that the ROL4 instruction also changes the high nibble of AL. Huge thanks to vcfed-member dreNorteR for discovering this and also for suggesting an optimization to the physical address conversion code in IdeTransfer.asm.
  • Made some changes to the OUTPUT_AL_TO_IDE_REGISTER and OUTPUT_AL_TO_IDE_CONTROL_BLOCK_REGISTER macros as an optimization. This saves 15+12 bytes in builds without MODULE_8BIT_IDE (e.g. the PS/2 builds).
  • Other minor optimizations and cleanups.
File size: 11.2 KB
Line 
1; Project name : XTIDE Universal BIOS
2; Description : IDE Device Command functions.
3
4;
5; XTIDE Universal BIOS and Associated Tools
6; Copyright (C) 2009-2010 by Tomi Tilli, 2011-2024 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; IdeCommand_ResetMasterAndSlaveController
25; Parameters:
26; DS:DI: Ptr to DPT (in RAMVARS segment)
27; Returns:
28; AH: INT 13h Error Code
29; CF: Cleared if success, Set if error
30; Corrupts registers:
31; AL, BX, CX, DX
32;--------------------------------------------------------------------
33IdeCommand_ResetMasterAndSlaveController: ; Unused entrypoint OK
34 ; HSR0: Set_SRST
35; Used to be:
36; call AccessDPT_GetDeviceControlByteToAL
37; or al, FLG_DEVCONTROL_SRST | FLG_DEVCONTROL_nIEN ; Set Reset bit
38; Is now:
39 mov al, FLG_DEVCONTROL_SRST | FLG_DEVCONTROL_nIEN
40; ---
41%define IO_SEQUENCE ; Do not modify DX while this is in effect!
42 OUTPUT_AL_TO_IDE_CONTROL_BLOCK_REGISTER DEVICE_CONTROL_REGISTER_out
43 mov ax, HSR0_RESET_WAIT_US
44 call Timer_DelayMicrosecondsFromAX
45
46 ; HSR1: Clear_wait
47; Used to be:
48; call AccessDPT_GetDeviceControlByteToAL
49; or al, FLG_DEVCONTROL_nIEN
50; and al, ~FLG_DEVCONTROL_SRST ; Clear reset bit
51; Is now:
52 mov al, FLG_DEVCONTROL_nIEN
53; ---
54 OUTPUT_AL_TO_IDE_CONTROL_BLOCK_REGISTER DEVICE_CONTROL_REGISTER_out
55%undef IO_SEQUENCE ; DX can be freely modified again.
56 mov ax, HSR1_RESET_WAIT_US
57 call Timer_DelayMicrosecondsFromAX
58
59 ; HSR2: Check_status
60 mov bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_MAXIMUM, FLG_STATUS_BSY)
61 jmp IdeWait_PollStatusFlagInBLwithTimeoutInBH
62
63; *FIXME* AccessDPT_GetDeviceControlByteToAL currently always returns with
64; AL cleared (0) or with only bit 1 set (FLG_DEVCONTROL_nIEN = 2).
65; The commented away instructions above sets FLG_DEVCONTROL_nIEN anyway
66; making the call to AccessDPT_GetDeviceControlByteToAL redundant.
67; I have left this code as is since I don't know if it's a mistake
68; (from all the way back to r150) or if it's coded this way in anticipation
69; of some future changes to AccessDPT_GetDeviceControlByteToAL.
70
71;--------------------------------------------------------------------
72; IdeCommand_IdentifyDeviceToBufferInESSIwithDriveSelectByteInBH
73; Parameters:
74; BH: Drive Select byte for Drive and Head Select Register
75; DX: Autodetected port for XT-CF
76; DS: Segment to RAMVARS
77; ES:SI: Ptr to buffer to receive 512-byte IDE Information
78; CS:BP: Ptr to IDEVARS
79; Returns:
80; AH: INT 13h Error Code
81; CF: Cleared if success, Set if error
82; Corrupts registers:
83; AL, BX, CX, DX, SI, DI, ES
84;--------------------------------------------------------------------
85IdeCommand_IdentifyDeviceToBufferInESSIwithDriveSelectByteInBH: ; Unused entrypoint OK
86 ; Create fake DPT to be able to use Device.asm functions
87 call FindDPT_ForNewDriveToDSDI
88 eMOVZX ax, bh
89 mov [di+DPT.wFlags], ax
90 call CreateDPT_StoreIdevarsOffsetAndBasePortFromCSBPtoDPTinDSDI
91 call IdeDPT_StoreDeviceTypeToDPTinDSDIfromIdevarsInCSBP
92 mov BYTE [di+DPT_ATA.bBlockSize], 1 ; Block = 1 sector
93
94 ; Wait until drive motors have reached full speed
95 cmp bp, BYTE ROMVARS.ideVars0 ; First controller?
96 jne SHORT .SkipLongWaitSinceDriveIsNotPrimaryMaster
97 test bh, FLG_DRVNHEAD_DRV ; Wait already done for Master
98 jnz SHORT .SkipLongWaitSinceDriveIsNotPrimaryMaster
99 mov bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_MOTOR_STARTUP, FLG_STATUS_BSY)
100 call IdeWait_PollStatusFlagInBLwithTimeoutInBH
101.SkipLongWaitSinceDriveIsNotPrimaryMaster:
102
103 ; Create IDEPACK without INTPACK
104 push bp
105 call Idepack_FakeToSSBP
106
107%ifdef MODULE_8BIT_IDE
108 push si
109
110 ; Enable 8-bit PIO for DEVICE_8BIT_ATA (no need to verify device type here)
111 call AH9h_Enable8bitModeForDevice8bitAta
112
113 ; Set XT-CF mode. No need to check here if device is XT-CF or not.
114%ifdef MODULE_8BIT_IDE_ADVANCED
115 call AH1Eh_GetCurrentXTCFmodeToAX ; Reads from DPT_ATA.bDevice that we just stored
116 call AH9h_SetModeFromALtoXTCF ; Enables/disables 8-bit mode when necessary
117%endif ; MODULE_8BIT_IDE_ADVANCED
118 pop si
119%endif ; MODULE_8BIT_IDE
120
121 ; Prepare to output Identify Device command
122 mov dl, 1 ; Sector count (required by IdeTransfer.asm)
123 mov al, COMMAND_IDENTIFY_DEVICE
124 mov bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRQ, FLG_STATUS_DRQ)
125 call Idepack_StoreNonExtParametersAndIssueCommandFromAL
126
127 ; Clean stack and return
128 lea sp, [bp+SIZE_OF_IDEPACK_WITHOUT_INTPACK] ; This assumes BP hasn't changed between Idepack_FakeToSSBP and here
129 pop bp
130 ret
131
132
133;--------------------------------------------------------------------
134; IdeCommand_OutputWithParameters
135; Parameters:
136; BH: System timer ticks for timeout
137; BL: IDE Status Register bit to poll after command
138; ES:SI: Ptr to buffer (for data transfer commands)
139; DS:DI: Ptr to DPT (in RAMVARS segment)
140; SS:BP: Ptr to IDEPACK
141; Returns:
142; AH: INT 13h Error Code
143; CX: Number of successfully transferred sectors (for transfer commands)
144; CF: Cleared if success, Set if error
145; Corrupts registers:
146; AL, BX, (CX), DX, (ES:SI for data transfer commands)
147;--------------------------------------------------------------------
148ALIGN JUMP_ALIGN
149IdeCommand_OutputWithParameters: ; Unused entrypoint OK
150 push bx ; Store status register bits to poll
151
152 ; Select Master or Slave drive and output head number or LBA28 top bits
153 call IdeCommand_SelectDrive
154 jc SHORT .DriveNotReady
155
156 ; Output Device Control Byte to enable or disable interrupts
157 mov al, [bp+IDEPACK.bDeviceControl]
158%ifdef MODULE_IRQ
159 test al, FLG_DEVCONTROL_nIEN ; Interrupts disabled?
160 jnz SHORT .DoNotSetInterruptInServiceFlag
161
162 ; Clear Task Flag and set Interrupt In-Service Flag
163 push ds
164 LOAD_BDA_SEGMENT_TO ds, dx
165 mov BYTE [BDA.bHDTaskFlg], 1 ; Will be adjusted to zero later
166 pop ds
167.DoNotSetInterruptInServiceFlag:
168%endif
169 OUTPUT_AL_TO_IDE_CONTROL_BLOCK_REGISTER DEVICE_CONTROL_REGISTER_out
170
171 ; Output Feature Number
172 mov al, [bp+IDEPACK.bFeatures]
173 OUTPUT_AL_TO_IDE_REGISTER FEATURES_REGISTER_out
174
175 ; Output Sector Address High (only used by LBA48)
176%ifdef MODULE_EBIOS
177 mov ah, [bp+IDEPACK.bLbaLowExt]
178 xor al, al ; Zero sector count
179 mov cx, [bp+IDEPACK.wLbaMiddleAndHighExt]
180 call OutputSectorCountAndAddress
181%endif
182
183 ; Output Sector Address Low
184 mov ax, [bp+IDEPACK.wSectorCountAndLbaLow]
185 mov cx, [bp+IDEPACK.wLbaMiddleAndHigh]
186 call OutputSectorCountAndAddress
187
188 ; Output command
189 mov al, [bp+IDEPACK.bCommand]
190 OUTPUT_AL_TO_IDE_REGISTER COMMAND_REGISTER_out
191
192 ; Wait until command completed
193 pop bx ; Pop status and timeout for polling
194 cmp bl, FLG_STATUS_DRQ ; Data transfer started?
195 jne SHORT .WaitUntilNonTransferCommandCompletes
196%ifdef MODULE_8BIT_IDE_ADVANCED
197 cmp BYTE [di+DPT_ATA.bDevice], DEVICE_8BIT_JRIDE_ISA
198 jae SHORT JrIdeTransfer_StartWithCommandInAL ; DEVICE_8BIT_JRIDE_ISA or DEVICE_8BIT_ADP50L
199%endif
200 jmp IdeTransfer_StartWithCommandInAL
201
202.WaitUntilNonTransferCommandCompletes:
203%ifdef MODULE_IRQ
204 test BYTE [bp+IDEPACK.bDeviceControl], FLG_DEVCONTROL_nIEN
205%ifdef USE_386
206 jnz IdeWait_IRQorStatusFlagInBLwithTimeoutInBH
207%else
208 jz SHORT .PollStatusFlagInsteadOfWaitIrq
209 jmp IdeWait_IRQorStatusFlagInBLwithTimeoutInBH
210.PollStatusFlagInsteadOfWaitIrq:
211%endif
212%endif ; MODULE_IRQ
213 jmp IdeWait_PollStatusFlagInBLwithTimeoutInBH
214
215.DriveNotReady:
216 pop bx ; Clean stack
217 ret
218
219
220;--------------------------------------------------------------------
221; IdeCommand_SelectDrive
222; Parameters:
223; DS:DI: Ptr to DPT (in RAMVARS segment)
224; SS:BP: Ptr to IDEPACK
225; Returns:
226; AH: INT 13h Error Code
227; CF: Cleared if success, Set if error
228; Corrupts registers:
229; AL, BX, CX, DX
230;--------------------------------------------------------------------
231ALIGN JUMP_ALIGN
232IdeCommand_SelectDrive:
233 ; We use different timeout value when detecting drives.
234 ; This prevents unnecessary long delays when drive is not present.
235 mov cx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRDY, FLG_STATUS_DRDY)
236 cmp WORD [RAMVARS.wDrvDetectSignature], RAMVARS_DRV_DETECT_SIGNATURE
237 eCMOVE ch, TIMEOUT_SELECT_DRIVE_DURING_DRIVE_DETECTION
238
239 ; Select Master or Slave Drive
240 mov al, [bp+IDEPACK.bDrvAndHead]
241 OUTPUT_AL_TO_IDE_REGISTER DRIVE_AND_HEAD_SELECT_REGISTER
242 mov bx, cx
243 call IdeWait_PollStatusFlagInBLwithTimeoutInBH
244
245 ; Output again to make sure head bits are set. They were lost if the device
246 ; was busy when we first output drive select (although it shouldn't be busy
247 ; since we have waited error result after previous command). Some low power
248 ; drives (CF cards, 1.8" HDDs etc) have some internal sleep modes that
249 ; might cause trouble? Normal HDDs seem to work fine.
250 ;
251 ; Now commented away since this fix was not necessary. Let's keep it here
252 ; if some drive someday has problems crossing 8GB
253 ;mov al, [bp+IDEPACK.bDrvAndHead]
254 ;OUTPUT_AL_TO_IDE_REGISTER DRIVE_AND_HEAD_SELECT_REGISTER
255
256 ; Ignore errors from IDE Error Register (set by previous command)
257 cmp ah, RET_HD_TIMEOUT
258 je SHORT .FailedToSelectDrive
259 xor ax, ax ; Always success unless timeout
260 ret
261.FailedToSelectDrive:
262 stc
263 ret
264
265
266;--------------------------------------------------------------------
267; OutputSectorCountAndAddress
268; Parameters:
269; AH: LBA low bits (Sector Number)
270; AL: Sector Count
271; CL: LBA middle bits (Cylinder Number low)
272; CH: LBA high bits (Cylinder Number high)
273; DS:DI: Ptr to DPT (in RAMVARS segment)
274; Returns:
275; Nothing
276; Corrupts registers:
277; AL, BX, DX
278;--------------------------------------------------------------------
279ALIGN JUMP_ALIGN
280OutputSectorCountAndAddress:
281%define IO_SEQUENCE ; Do not modify DX while this is in effect!
282 OUTPUT_AL_TO_IDE_REGISTER SECTOR_COUNT_REGISTER
283
284 mov al, ah
285 OUTPUT_AL_TO_IDE_REGISTER LBA_LOW_REGISTER
286
287 mov al, cl
288 OUTPUT_AL_TO_IDE_REGISTER LBA_MIDDLE_REGISTER
289
290 mov al, ch
291 OUTPUT_AL_TO_IDE_REGISTER LBA_HIGH_REGISTER
292%undef IO_SEQUENCE ; DX can be freely modified again.
293 ret
294
295
296;--------------------------------------------------------------------
297; IdeCommand_ReadLBAlowRegisterToAL
298; Returns LBA low register / Sector number register contents.
299; Note that this returns valid value only after transfer command (read/write/verify)
300; has stopped to an error. Do not call this otherwise.
301; Parameters:
302; DS:DI: Ptr to DPT (in RAMVARS segment)
303; Returns:
304; AL: Byte read from the register
305; Corrupts registers:
306; BX, DX
307;--------------------------------------------------------------------
308ALIGN JUMP_ALIGN
309IdeCommand_ReadLBAlowRegisterToAL: ; Unused entrypoint OK
310 ; HOB bit (defined in 48-bit address feature set) should be zero by default
311 ; so we get the correct value for CHS, LBA28 and LBA48 drives and commands
312 INPUT_TO_AL_FROM_IDE_REGISTER LBA_LOW_REGISTER
313 ret
Note: See TracBrowser for help on using the repository browser.