[150] | 1 | ; Project name : XTIDE Universal BIOS
|
---|
| 2 | ; Description : IDE Device Command functions.
|
---|
| 3 |
|
---|
[376] | 4 | ;
|
---|
[445] | 5 | ; XTIDE Universal BIOS and Associated Tools
|
---|
[630] | 6 | ; Copyright (C) 2009-2010 by Tomi Tilli, 2011-2024 by XTIDE Universal BIOS Team.
|
---|
[376] | 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.
|
---|
[445] | 12 | ;
|
---|
[376] | 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.
|
---|
[445] | 17 | ; Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
---|
| 18 | ;
|
---|
[376] | 19 |
|
---|
[150] | 20 | ; Section containing code
|
---|
| 21 | SECTION .text
|
---|
| 22 |
|
---|
| 23 | ;--------------------------------------------------------------------
|
---|
[507] | 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 | ;--------------------------------------------------------------------
|
---|
[596] | 33 | IdeCommand_ResetMasterAndSlaveController: ; Unused entrypoint OK
|
---|
[507] | 34 | ; HSR0: Set_SRST
|
---|
[589] | 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 | ; ---
|
---|
[630] | 41 | %define IO_SEQUENCE ; Do not modify DX while this is in effect!
|
---|
[507] | 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
|
---|
[589] | 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 | ; ---
|
---|
[507] | 54 | OUTPUT_AL_TO_IDE_CONTROL_BLOCK_REGISTER DEVICE_CONTROL_REGISTER_out
|
---|
[630] | 55 | %undef IO_SEQUENCE ; DX can be freely modified again.
|
---|
[507] | 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 |
|
---|
[589] | 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.
|
---|
[507] | 70 |
|
---|
| 71 | ;--------------------------------------------------------------------
|
---|
[150] | 72 | ; IdeCommand_IdentifyDeviceToBufferInESSIwithDriveSelectByteInBH
|
---|
| 73 | ; Parameters:
|
---|
| 74 | ; BH: Drive Select byte for Drive and Head Select Register
|
---|
[473] | 75 | ; DX: Autodetected port for XT-CF
|
---|
[150] | 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:
|
---|
[443] | 83 | ; AL, BX, CX, DX, SI, DI, ES
|
---|
[150] | 84 | ;--------------------------------------------------------------------
|
---|
[596] | 85 | IdeCommand_IdentifyDeviceToBufferInESSIwithDriveSelectByteInBH: ; Unused entrypoint OK
|
---|
[150] | 86 | ; Create fake DPT to be able to use Device.asm functions
|
---|
| 87 | call FindDPT_ForNewDriveToDSDI
|
---|
[158] | 88 | eMOVZX ax, bh
|
---|
[150] | 89 | mov [di+DPT.wFlags], ax
|
---|
[473] | 90 | call CreateDPT_StoreIdevarsOffsetAndBasePortFromCSBPtoDPTinDSDI
|
---|
| 91 | call IdeDPT_StoreDeviceTypeToDPTinDSDIfromIdevarsInCSBP
|
---|
[365] | 92 | mov BYTE [di+DPT_ATA.bBlockSize], 1 ; Block = 1 sector
|
---|
[150] | 93 |
|
---|
[443] | 94 | ; Wait until drive motors have reached full speed
|
---|
[473] | 95 | cmp bp, BYTE ROMVARS.ideVars0 ; First controller?
|
---|
[150] | 96 | jne SHORT .SkipLongWaitSinceDriveIsNotPrimaryMaster
|
---|
[473] | 97 | test bh, FLG_DRVNHEAD_DRV ; Wait already done for Master
|
---|
[150] | 98 | jnz SHORT .SkipLongWaitSinceDriveIsNotPrimaryMaster
|
---|
[545] | 99 | mov bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_MOTOR_STARTUP, FLG_STATUS_BSY)
|
---|
[507] | 100 | call IdeWait_PollStatusFlagInBLwithTimeoutInBH
|
---|
[150] | 101 | .SkipLongWaitSinceDriveIsNotPrimaryMaster:
|
---|
| 102 |
|
---|
| 103 | ; Create IDEPACK without INTPACK
|
---|
| 104 | push bp
|
---|
| 105 | call Idepack_FakeToSSBP
|
---|
| 106 |
|
---|
[545] | 107 | %ifdef MODULE_8BIT_IDE
|
---|
[480] | 108 | push si
|
---|
[584] | 109 |
|
---|
| 110 | ; Enable 8-bit PIO for DEVICE_8BIT_ATA (no need to verify device type here)
|
---|
[480] | 111 | call AH9h_Enable8bitModeForDevice8bitAta
|
---|
[584] | 112 |
|
---|
| 113 | ; Set XT-CF mode. No need to check here if device is XT-CF or not.
|
---|
[545] | 114 | %ifdef MODULE_8BIT_IDE_ADVANCED
|
---|
[584] | 115 | call AH1Eh_GetCurrentXTCFmodeToAX ; Reads from DPT_ATA.bDevice that we just stored
|
---|
| 116 | call AH9h_SetModeFromALtoXTCF ; Enables/disables 8-bit mode when necessary
|
---|
[545] | 117 | %endif ; MODULE_8BIT_IDE_ADVANCED
|
---|
[443] | 118 | pop si
|
---|
[545] | 119 | %endif ; MODULE_8BIT_IDE
|
---|
[437] | 120 |
|
---|
[150] | 121 | ; Prepare to output Identify Device command
|
---|
| 122 | mov dl, 1 ; Sector count (required by IdeTransfer.asm)
|
---|
| 123 | mov al, COMMAND_IDENTIFY_DEVICE
|
---|
[411] | 124 | mov bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRQ, FLG_STATUS_DRQ)
|
---|
[150] | 125 | call Idepack_StoreNonExtParametersAndIssueCommandFromAL
|
---|
| 126 |
|
---|
| 127 | ; Clean stack and return
|
---|
[443] | 128 | lea sp, [bp+SIZE_OF_IDEPACK_WITHOUT_INTPACK] ; This assumes BP hasn't changed between Idepack_FakeToSSBP and here
|
---|
[150] | 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
|
---|
[249] | 143 | ; CX: Number of successfully transferred sectors (for transfer commands)
|
---|
[150] | 144 | ; CF: Cleared if success, Set if error
|
---|
| 145 | ; Corrupts registers:
|
---|
[249] | 146 | ; AL, BX, (CX), DX, (ES:SI for data transfer commands)
|
---|
[150] | 147 | ;--------------------------------------------------------------------
|
---|
| 148 | ALIGN JUMP_ALIGN
|
---|
[596] | 149 | IdeCommand_OutputWithParameters: ; Unused entrypoint OK
|
---|
[158] | 150 | push bx ; Store status register bits to poll
|
---|
[150] | 151 |
|
---|
| 152 | ; Select Master or Slave drive and output head number or LBA28 top bits
|
---|
[400] | 153 | call IdeCommand_SelectDrive
|
---|
[150] | 154 | jc SHORT .DriveNotReady
|
---|
| 155 |
|
---|
| 156 | ; Output Device Control Byte to enable or disable interrupts
|
---|
| 157 | mov al, [bp+IDEPACK.bDeviceControl]
|
---|
[400] | 158 | %ifdef MODULE_IRQ
|
---|
[158] | 159 | test al, FLG_DEVCONTROL_nIEN ; Interrupts disabled?
|
---|
[152] | 160 | jnz SHORT .DoNotSetInterruptInServiceFlag
|
---|
[158] | 161 |
|
---|
| 162 | ; Clear Task Flag and set Interrupt In-Service Flag
|
---|
[152] | 163 | push ds
|
---|
[540] | 164 | LOAD_BDA_SEGMENT_TO ds, dx
|
---|
| 165 | mov BYTE [BDA.bHDTaskFlg], 1 ; Will be adjusted to zero later
|
---|
[152] | 166 | pop ds
|
---|
| 167 | .DoNotSetInterruptInServiceFlag:
|
---|
[266] | 168 | %endif
|
---|
[267] | 169 | OUTPUT_AL_TO_IDE_CONTROL_BLOCK_REGISTER DEVICE_CONTROL_REGISTER_out
|
---|
[150] | 170 |
|
---|
| 171 | ; Output Feature Number
|
---|
| 172 | mov al, [bp+IDEPACK.bFeatures]
|
---|
[267] | 173 | OUTPUT_AL_TO_IDE_REGISTER FEATURES_REGISTER_out
|
---|
[150] | 174 |
|
---|
| 175 | ; Output Sector Address High (only used by LBA48)
|
---|
[285] | 176 | %ifdef MODULE_EBIOS
|
---|
[612] | 177 | mov ah, [bp+IDEPACK.bLbaLowExt]
|
---|
| 178 | xor al, al ; Zero sector count
|
---|
[150] | 179 | mov cx, [bp+IDEPACK.wLbaMiddleAndHighExt]
|
---|
[400] | 180 | call OutputSectorCountAndAddress
|
---|
[285] | 181 | %endif
|
---|
[150] | 182 |
|
---|
| 183 | ; Output Sector Address Low
|
---|
| 184 | mov ax, [bp+IDEPACK.wSectorCountAndLbaLow]
|
---|
| 185 | mov cx, [bp+IDEPACK.wLbaMiddleAndHigh]
|
---|
[400] | 186 | call OutputSectorCountAndAddress
|
---|
[150] | 187 |
|
---|
| 188 | ; Output command
|
---|
| 189 | mov al, [bp+IDEPACK.bCommand]
|
---|
[267] | 190 | OUTPUT_AL_TO_IDE_REGISTER COMMAND_REGISTER_out
|
---|
[150] | 191 |
|
---|
| 192 | ; Wait until command completed
|
---|
[400] | 193 | pop bx ; Pop status and timeout for polling
|
---|
| 194 | cmp bl, FLG_STATUS_DRQ ; Data transfer started?
|
---|
| 195 | jne SHORT .WaitUntilNonTransferCommandCompletes
|
---|
[493] | 196 | %ifdef MODULE_8BIT_IDE_ADVANCED
|
---|
[545] | 197 | cmp BYTE [di+DPT_ATA.bDevice], DEVICE_8BIT_JRIDE_ISA
|
---|
| 198 | jae SHORT JrIdeTransfer_StartWithCommandInAL ; DEVICE_8BIT_JRIDE_ISA or DEVICE_8BIT_ADP50L
|
---|
[480] | 199 | %endif
|
---|
[474] | 200 | jmp IdeTransfer_StartWithCommandInAL
|
---|
[400] | 201 |
|
---|
| 202 | .WaitUntilNonTransferCommandCompletes:
|
---|
| 203 | %ifdef MODULE_IRQ
|
---|
[150] | 204 | test BYTE [bp+IDEPACK.bDeviceControl], FLG_DEVCONTROL_nIEN
|
---|
[592] | 205 | %ifdef USE_386
|
---|
| 206 | jnz IdeWait_IRQorStatusFlagInBLwithTimeoutInBH
|
---|
| 207 | %else
|
---|
[400] | 208 | jz SHORT .PollStatusFlagInsteadOfWaitIrq
|
---|
| 209 | jmp IdeWait_IRQorStatusFlagInBLwithTimeoutInBH
|
---|
| 210 | .PollStatusFlagInsteadOfWaitIrq:
|
---|
| 211 | %endif
|
---|
[592] | 212 | %endif ; MODULE_IRQ
|
---|
[400] | 213 | jmp IdeWait_PollStatusFlagInBLwithTimeoutInBH
|
---|
[150] | 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 | ;--------------------------------------------------------------------
|
---|
| 231 | ALIGN JUMP_ALIGN
|
---|
[400] | 232 | IdeCommand_SelectDrive:
|
---|
[444] | 233 | ; We use different timeout value when detecting drives.
|
---|
| 234 | ; This prevents unnecessary long delays when drive is not present.
|
---|
[473] | 235 | mov cx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRDY, FLG_STATUS_DRDY)
|
---|
[444] | 236 | cmp WORD [RAMVARS.wDrvDetectSignature], RAMVARS_DRV_DETECT_SIGNATURE
|
---|
[473] | 237 | eCMOVE ch, TIMEOUT_SELECT_DRIVE_DURING_DRIVE_DETECTION
|
---|
[408] | 238 |
|
---|
[150] | 239 | ; Select Master or Slave Drive
|
---|
| 240 | mov al, [bp+IDEPACK.bDrvAndHead]
|
---|
[267] | 241 | OUTPUT_AL_TO_IDE_REGISTER DRIVE_AND_HEAD_SELECT_REGISTER
|
---|
[473] | 242 | mov bx, cx
|
---|
[400] | 243 | call IdeWait_PollStatusFlagInBLwithTimeoutInBH
|
---|
[150] | 244 |
|
---|
[610] | 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.
|
---|
[611] | 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
|
---|
[610] | 255 |
|
---|
[281] | 256 | ; Ignore errors from IDE Error Register (set by previous command)
|
---|
[285] | 257 | cmp ah, RET_HD_TIMEOUT
|
---|
| 258 | je SHORT .FailedToSelectDrive
|
---|
| 259 | xor ax, ax ; Always success unless timeout
|
---|
| 260 | ret
|
---|
| 261 | .FailedToSelectDrive:
|
---|
[281] | 262 | stc
|
---|
[279] | 263 | ret
|
---|
[150] | 264 |
|
---|
[279] | 265 |
|
---|
[150] | 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 | ;--------------------------------------------------------------------
|
---|
| 279 | ALIGN JUMP_ALIGN
|
---|
[400] | 280 | OutputSectorCountAndAddress:
|
---|
[630] | 281 | %define IO_SEQUENCE ; Do not modify DX while this is in effect!
|
---|
[267] | 282 | OUTPUT_AL_TO_IDE_REGISTER SECTOR_COUNT_REGISTER
|
---|
[150] | 283 |
|
---|
| 284 | mov al, ah
|
---|
[267] | 285 | OUTPUT_AL_TO_IDE_REGISTER LBA_LOW_REGISTER
|
---|
[150] | 286 |
|
---|
| 287 | mov al, cl
|
---|
[267] | 288 | OUTPUT_AL_TO_IDE_REGISTER LBA_MIDDLE_REGISTER
|
---|
[150] | 289 |
|
---|
| 290 | mov al, ch
|
---|
[473] | 291 | OUTPUT_AL_TO_IDE_REGISTER LBA_HIGH_REGISTER
|
---|
[630] | 292 | %undef IO_SEQUENCE ; DX can be freely modified again.
|
---|
[473] | 293 | ret
|
---|
[617] | 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 | ;--------------------------------------------------------------------
|
---|
| 308 | ALIGN JUMP_ALIGN
|
---|
[625] | 309 | IdeCommand_ReadLBAlowRegisterToAL: ; Unused entrypoint OK
|
---|
[617] | 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
|
---|