[150] | 1 | ; Project name : XTIDE Universal BIOS |
---|
| 2 | ; Description : IDE Device Command functions. |
---|
| 3 | |
---|
[376] | 4 | ; |
---|
[445] | 5 | ; XTIDE Universal BIOS and Associated Tools |
---|
[376] | 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. |
---|
[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 | ;-------------------------------------------------------------------- |
---|
| 24 | ; IdeCommand_IdentifyDeviceToBufferInESSIwithDriveSelectByteInBH |
---|
| 25 | ; Parameters: |
---|
| 26 | ; BH: Drive Select byte for Drive and Head Select Register |
---|
[473] | 27 | ; DX: Autodetected port for XT-CF |
---|
[150] | 28 | ; DS: Segment to RAMVARS |
---|
| 29 | ; ES:SI: Ptr to buffer to receive 512-byte IDE Information |
---|
| 30 | ; CS:BP: Ptr to IDEVARS |
---|
| 31 | ; Returns: |
---|
| 32 | ; AH: INT 13h Error Code |
---|
| 33 | ; CF: Cleared if success, Set if error |
---|
| 34 | ; Corrupts registers: |
---|
[443] | 35 | ; AL, BX, CX, DX, SI, DI, ES |
---|
[150] | 36 | ;-------------------------------------------------------------------- |
---|
[400] | 37 | IdeCommand_IdentifyDeviceToBufferInESSIwithDriveSelectByteInBH: |
---|
[150] | 38 | ; Create fake DPT to be able to use Device.asm functions |
---|
| 39 | call FindDPT_ForNewDriveToDSDI |
---|
[158] | 40 | eMOVZX ax, bh |
---|
[150] | 41 | mov [di+DPT.wFlags], ax |
---|
[473] | 42 | call CreateDPT_StoreIdevarsOffsetAndBasePortFromCSBPtoDPTinDSDI |
---|
| 43 | call IdeDPT_StoreDeviceTypeToDPTinDSDIfromIdevarsInCSBP |
---|
[365] | 44 | mov BYTE [di+DPT_ATA.bBlockSize], 1 ; Block = 1 sector |
---|
[150] | 45 | |
---|
[443] | 46 | ; Wait until drive motors have reached full speed |
---|
[473] | 47 | cmp bp, BYTE ROMVARS.ideVars0 ; First controller? |
---|
[150] | 48 | jne SHORT .SkipLongWaitSinceDriveIsNotPrimaryMaster |
---|
[473] | 49 | test bh, FLG_DRVNHEAD_DRV ; Wait already done for Master |
---|
[150] | 50 | jnz SHORT .SkipLongWaitSinceDriveIsNotPrimaryMaster |
---|
[445] | 51 | call AHDh_WaitUntilDriveMotorHasReachedFullSpeed |
---|
[150] | 52 | .SkipLongWaitSinceDriveIsNotPrimaryMaster: |
---|
| 53 | |
---|
| 54 | ; Create IDEPACK without INTPACK |
---|
| 55 | push bp |
---|
| 56 | call Idepack_FakeToSSBP |
---|
| 57 | |
---|
[493] | 58 | %ifdef MODULE_8BIT_IDE_ADVANCED |
---|
[480] | 59 | ; Enable 8-bit PIO mode for 8-bit ATA and XT-CF |
---|
| 60 | push si |
---|
| 61 | call AH9h_Enable8bitModeForDevice8bitAta |
---|
[473] | 62 | xor al, al ; XTCF_8BIT_PIO_MODE |
---|
[480] | 63 | call AH9h_SetModeFromALtoXTCF |
---|
[443] | 64 | pop si |
---|
[493] | 65 | %endif ; MODULE_8BIT_IDE_ADVANCED |
---|
[437] | 66 | |
---|
[150] | 67 | ; Prepare to output Identify Device command |
---|
| 68 | mov dl, 1 ; Sector count (required by IdeTransfer.asm) |
---|
| 69 | mov al, COMMAND_IDENTIFY_DEVICE |
---|
[411] | 70 | mov bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRQ, FLG_STATUS_DRQ) |
---|
[150] | 71 | call Idepack_StoreNonExtParametersAndIssueCommandFromAL |
---|
| 72 | |
---|
| 73 | ; Clean stack and return |
---|
[439] | 74 | .FailedToSet8bitMode: |
---|
[443] | 75 | lea sp, [bp+SIZE_OF_IDEPACK_WITHOUT_INTPACK] ; This assumes BP hasn't changed between Idepack_FakeToSSBP and here |
---|
[150] | 76 | pop bp |
---|
| 77 | ret |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | ;-------------------------------------------------------------------- |
---|
| 81 | ; IdeCommand_OutputWithParameters |
---|
| 82 | ; Parameters: |
---|
| 83 | ; BH: System timer ticks for timeout |
---|
| 84 | ; BL: IDE Status Register bit to poll after command |
---|
| 85 | ; ES:SI: Ptr to buffer (for data transfer commands) |
---|
| 86 | ; DS:DI: Ptr to DPT (in RAMVARS segment) |
---|
| 87 | ; SS:BP: Ptr to IDEPACK |
---|
| 88 | ; Returns: |
---|
| 89 | ; AH: INT 13h Error Code |
---|
[249] | 90 | ; CX: Number of successfully transferred sectors (for transfer commands) |
---|
[150] | 91 | ; CF: Cleared if success, Set if error |
---|
| 92 | ; Corrupts registers: |
---|
[249] | 93 | ; AL, BX, (CX), DX, (ES:SI for data transfer commands) |
---|
[150] | 94 | ;-------------------------------------------------------------------- |
---|
| 95 | ALIGN JUMP_ALIGN |
---|
[400] | 96 | IdeCommand_OutputWithParameters: |
---|
[158] | 97 | push bx ; Store status register bits to poll |
---|
[150] | 98 | |
---|
| 99 | ; Select Master or Slave drive and output head number or LBA28 top bits |
---|
[400] | 100 | call IdeCommand_SelectDrive |
---|
[150] | 101 | jc SHORT .DriveNotReady |
---|
| 102 | |
---|
| 103 | ; Output Device Control Byte to enable or disable interrupts |
---|
| 104 | mov al, [bp+IDEPACK.bDeviceControl] |
---|
[400] | 105 | %ifdef MODULE_IRQ |
---|
[158] | 106 | test al, FLG_DEVCONTROL_nIEN ; Interrupts disabled? |
---|
[152] | 107 | jnz SHORT .DoNotSetInterruptInServiceFlag |
---|
[158] | 108 | |
---|
| 109 | ; Clear Task Flag and set Interrupt In-Service Flag |
---|
| 110 | or BYTE [di+DPT.bFlagsHigh], FLGH_DPT_INTERRUPT_IN_SERVICE |
---|
[152] | 111 | push ds |
---|
[158] | 112 | LOAD_BDA_SEGMENT_TO ds, dx, ! ; Also zero DX |
---|
| 113 | mov [BDA.bHDTaskFlg], dl |
---|
[152] | 114 | pop ds |
---|
| 115 | .DoNotSetInterruptInServiceFlag: |
---|
[266] | 116 | %endif |
---|
[267] | 117 | OUTPUT_AL_TO_IDE_CONTROL_BLOCK_REGISTER DEVICE_CONTROL_REGISTER_out |
---|
[150] | 118 | |
---|
| 119 | ; Output Feature Number |
---|
| 120 | mov al, [bp+IDEPACK.bFeatures] |
---|
[267] | 121 | OUTPUT_AL_TO_IDE_REGISTER FEATURES_REGISTER_out |
---|
[150] | 122 | |
---|
| 123 | ; Output Sector Address High (only used by LBA48) |
---|
[285] | 124 | %ifdef MODULE_EBIOS |
---|
[294] | 125 | eMOVZX ax, [bp+IDEPACK.bLbaLowExt] ; Zero sector count |
---|
[150] | 126 | mov cx, [bp+IDEPACK.wLbaMiddleAndHighExt] |
---|
[400] | 127 | call OutputSectorCountAndAddress |
---|
[285] | 128 | %endif |
---|
[150] | 129 | |
---|
| 130 | ; Output Sector Address Low |
---|
| 131 | mov ax, [bp+IDEPACK.wSectorCountAndLbaLow] |
---|
| 132 | mov cx, [bp+IDEPACK.wLbaMiddleAndHigh] |
---|
[400] | 133 | call OutputSectorCountAndAddress |
---|
[150] | 134 | |
---|
| 135 | ; Output command |
---|
| 136 | mov al, [bp+IDEPACK.bCommand] |
---|
[267] | 137 | OUTPUT_AL_TO_IDE_REGISTER COMMAND_REGISTER_out |
---|
[150] | 138 | |
---|
| 139 | ; Wait until command completed |
---|
[400] | 140 | pop bx ; Pop status and timeout for polling |
---|
| 141 | cmp bl, FLG_STATUS_DRQ ; Data transfer started? |
---|
| 142 | jne SHORT .WaitUntilNonTransferCommandCompletes |
---|
[493] | 143 | %ifdef MODULE_8BIT_IDE_ADVANCED |
---|
[480] | 144 | cmp BYTE [di+DPT_ATA.bDevice], DEVICE_8BIT_XTCF_MEMMAP |
---|
| 145 | jae SHORT JrIdeTransfer_StartWithCommandInAL ; DEVICE_8BIT_XTCF_MEMMAP or DEVICE_8BIT_JRIDE_ISA |
---|
| 146 | %endif |
---|
[474] | 147 | jmp IdeTransfer_StartWithCommandInAL |
---|
[400] | 148 | |
---|
| 149 | .WaitUntilNonTransferCommandCompletes: |
---|
| 150 | %ifdef MODULE_IRQ |
---|
[150] | 151 | test BYTE [bp+IDEPACK.bDeviceControl], FLG_DEVCONTROL_nIEN |
---|
[400] | 152 | jz SHORT .PollStatusFlagInsteadOfWaitIrq |
---|
| 153 | jmp IdeWait_IRQorStatusFlagInBLwithTimeoutInBH |
---|
| 154 | .PollStatusFlagInsteadOfWaitIrq: |
---|
| 155 | %endif |
---|
| 156 | jmp IdeWait_PollStatusFlagInBLwithTimeoutInBH |
---|
[150] | 157 | |
---|
| 158 | .DriveNotReady: |
---|
| 159 | pop bx ; Clean stack |
---|
| 160 | ret |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | ;-------------------------------------------------------------------- |
---|
| 164 | ; IdeCommand_SelectDrive |
---|
| 165 | ; Parameters: |
---|
| 166 | ; DS:DI: Ptr to DPT (in RAMVARS segment) |
---|
| 167 | ; SS:BP: Ptr to IDEPACK |
---|
| 168 | ; Returns: |
---|
| 169 | ; AH: INT 13h Error Code |
---|
| 170 | ; CF: Cleared if success, Set if error |
---|
| 171 | ; Corrupts registers: |
---|
| 172 | ; AL, BX, CX, DX |
---|
| 173 | ;-------------------------------------------------------------------- |
---|
| 174 | ALIGN JUMP_ALIGN |
---|
[400] | 175 | IdeCommand_SelectDrive: |
---|
[444] | 176 | ; We use different timeout value when detecting drives. |
---|
| 177 | ; This prevents unnecessary long delays when drive is not present. |
---|
[473] | 178 | mov cx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRDY, FLG_STATUS_DRDY) |
---|
[444] | 179 | cmp WORD [RAMVARS.wDrvDetectSignature], RAMVARS_DRV_DETECT_SIGNATURE |
---|
[473] | 180 | eCMOVE ch, TIMEOUT_SELECT_DRIVE_DURING_DRIVE_DETECTION |
---|
[408] | 181 | |
---|
[150] | 182 | ; Select Master or Slave Drive |
---|
| 183 | mov al, [bp+IDEPACK.bDrvAndHead] |
---|
[267] | 184 | OUTPUT_AL_TO_IDE_REGISTER DRIVE_AND_HEAD_SELECT_REGISTER |
---|
[473] | 185 | mov bx, cx |
---|
[400] | 186 | call IdeWait_PollStatusFlagInBLwithTimeoutInBH |
---|
[150] | 187 | |
---|
[281] | 188 | ; Ignore errors from IDE Error Register (set by previous command) |
---|
[285] | 189 | cmp ah, RET_HD_TIMEOUT |
---|
| 190 | je SHORT .FailedToSelectDrive |
---|
| 191 | xor ax, ax ; Always success unless timeout |
---|
| 192 | ret |
---|
| 193 | .FailedToSelectDrive: |
---|
[281] | 194 | stc |
---|
[279] | 195 | ret |
---|
[150] | 196 | |
---|
[279] | 197 | |
---|
[150] | 198 | ;-------------------------------------------------------------------- |
---|
| 199 | ; OutputSectorCountAndAddress |
---|
| 200 | ; Parameters: |
---|
| 201 | ; AH: LBA low bits (Sector Number) |
---|
| 202 | ; AL: Sector Count |
---|
| 203 | ; CL: LBA middle bits (Cylinder Number low) |
---|
| 204 | ; CH: LBA high bits (Cylinder Number high) |
---|
| 205 | ; DS:DI: Ptr to DPT (in RAMVARS segment) |
---|
| 206 | ; Returns: |
---|
| 207 | ; Nothing |
---|
| 208 | ; Corrupts registers: |
---|
| 209 | ; AL, BX, DX |
---|
| 210 | ;-------------------------------------------------------------------- |
---|
| 211 | ALIGN JUMP_ALIGN |
---|
[400] | 212 | OutputSectorCountAndAddress: |
---|
[267] | 213 | OUTPUT_AL_TO_IDE_REGISTER SECTOR_COUNT_REGISTER |
---|
[150] | 214 | |
---|
| 215 | mov al, ah |
---|
[267] | 216 | OUTPUT_AL_TO_IDE_REGISTER LBA_LOW_REGISTER |
---|
[150] | 217 | |
---|
| 218 | mov al, cl |
---|
[267] | 219 | OUTPUT_AL_TO_IDE_REGISTER LBA_MIDDLE_REGISTER |
---|
[150] | 220 | |
---|
| 221 | mov al, ch |
---|
[473] | 222 | OUTPUT_AL_TO_IDE_REGISTER LBA_HIGH_REGISTER |
---|
| 223 | ret |
---|