[3] | 1 | ; File name : HStatus.asm |
---|
| 2 | ; Project name : IDE BIOS |
---|
| 3 | ; Created date : 15.12.2009 |
---|
[10] | 4 | ; Last update : 25.5.2010 |
---|
[3] | 5 | ; Author : Tomi Tilli |
---|
| 6 | ; Description : IDE Status Register polling functions. |
---|
| 7 | |
---|
| 8 | ; Section containing code |
---|
| 9 | SECTION .text |
---|
| 10 | |
---|
| 11 | ;-------------------------------------------------------------------- |
---|
| 12 | ; Waits Hard Disk IRQ when not transferring data. |
---|
| 13 | ; If interrupts are disabled, RDY flag is polled. |
---|
| 14 | ; |
---|
| 15 | ; HStatus_WaitIrqOrRdy |
---|
| 16 | ; Parameters: |
---|
| 17 | ; DS:BX: Ptr to DPT |
---|
| 18 | ; Returns: |
---|
| 19 | ; AH: BIOS Error code |
---|
| 20 | ; CF: 0 if wait succesfull |
---|
| 21 | ; 1 if any error |
---|
| 22 | ; Corrupts registers: |
---|
| 23 | ; AL, CX, DX |
---|
| 24 | ;-------------------------------------------------------------------- |
---|
| 25 | ALIGN JUMP_ALIGN |
---|
| 26 | HStatus_WaitIrqOrRdy: |
---|
| 27 | test BYTE [di+DPT.bDrvCtrl], FLG_IDE_CTRL_nIEN |
---|
[10] | 28 | jnz SHORT .PollRdySinceIrqsAreDisabled |
---|
| 29 | jmp HIRQ_WaitIRQ |
---|
| 30 | ALIGN JUMP_ALIGN |
---|
| 31 | .PollRdySinceIrqsAreDisabled: |
---|
[3] | 32 | call HStatus_ReadAndIgnoreAlternateStatus |
---|
| 33 | mov cl, B_TIMEOUT_DRQ ; Load DRQ (not RDY) timeout |
---|
| 34 | jmp SHORT HStatus_WaitRdy ; Jump to poll RDY |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | ;-------------------------------------------------------------------- |
---|
| 38 | ; Reads Alternate Status Register and ignores result. |
---|
| 39 | ; Alternate Status Register is read to prevent polling host from |
---|
| 40 | ; reading status before it is valid. |
---|
| 41 | ; |
---|
| 42 | ; HStatus_ReadAndIgnoreAlternateStatus |
---|
| 43 | ; Parameters: |
---|
| 44 | ; DS:BX: Ptr to DPT |
---|
| 45 | ; Returns: |
---|
| 46 | ; Nothing |
---|
| 47 | ; Corrupts registers: |
---|
| 48 | ; AL, CX, DX |
---|
| 49 | ;-------------------------------------------------------------------- |
---|
| 50 | ALIGN JUMP_ALIGN |
---|
| 51 | HStatus_ReadAndIgnoreAlternateStatus: |
---|
| 52 | mov cx, bx ; Backup BX |
---|
| 53 | eMOVZX bx, BYTE [bx+DPT.bIdeOff] ; CS:BX now points to IDEVARS |
---|
| 54 | mov dx, [cs:bx+IDEVARS.wPortCtrl] ; DX = Control Block base port |
---|
| 55 | add dx, BYTE REGR_IDEC_AST ; DX = Alternate Status Register address |
---|
| 56 | in al, dx ; Read Alternate Status Register |
---|
| 57 | mov bx, cx ; Restore BX |
---|
| 58 | ret |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | ;-------------------------------------------------------------------- |
---|
| 62 | ; Waits until Hard Disk is ready to transfer data. |
---|
| 63 | ; |
---|
| 64 | ; HStatus_WaitIrqOrDrq |
---|
| 65 | ; Parameters: |
---|
| 66 | ; DS:BX: Ptr to DPT (in RAMVARS segment) |
---|
| 67 | ; Returns: |
---|
| 68 | ; AH: BIOS Error code |
---|
| 69 | ; CF: 0 if wait succesfull |
---|
| 70 | ; 1 if any error |
---|
| 71 | ; Corrupts registers: |
---|
| 72 | ; AL |
---|
| 73 | ;-------------------------------------------------------------------- |
---|
| 74 | ALIGN JUMP_ALIGN |
---|
| 75 | HStatus_WaitIrqOrDrq: |
---|
| 76 | push dx |
---|
| 77 | push cx |
---|
| 78 | |
---|
| 79 | ; Check if interrupts are enabled |
---|
| 80 | test BYTE [bx+DPT.bDrvCtrl], FLG_IDE_CTRL_nIEN |
---|
| 81 | jnz SHORT .PollDRQ ; Poll DRQ if IRQ disabled |
---|
| 82 | call HIRQ_WaitIRQ ; Wait for IRQ |
---|
| 83 | jmp SHORT .Return |
---|
| 84 | |
---|
| 85 | ALIGN JUMP_ALIGN |
---|
| 86 | .PollDRQ: |
---|
| 87 | call HStatus_ReadAndIgnoreAlternateStatus |
---|
| 88 | call HStatus_WaitDrqDefTime |
---|
| 89 | ALIGN JUMP_ALIGN |
---|
| 90 | .Return: |
---|
| 91 | pop cx |
---|
| 92 | pop dx |
---|
| 93 | ret |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | ;-------------------------------------------------------------------- |
---|
| 97 | ; Waits until busy flag is cleared from selected Hard Disk drive. |
---|
| 98 | ; |
---|
| 99 | ; HStatus_WaitBsyDefTime Uses default timeout |
---|
| 100 | ; HStatus_WaitBsy Uses user defined timeout |
---|
| 101 | ; HStatus_WaitBsyBase Uses user base port address and timeout |
---|
| 102 | ; Parameters: |
---|
| 103 | ; CL: Timeout value in system timer ticks (not HStatus_WaitBsyDefTime) |
---|
| 104 | ; DX: IDE Base port address (HUtil_WaitBsyBase only) |
---|
| 105 | ; DS: Segment to RAMVARS |
---|
| 106 | ; Returns: |
---|
| 107 | ; AH: BIOS Error code |
---|
| 108 | ; DX: IDE Status Register Address |
---|
| 109 | ; CF: 0 if wait succesfull |
---|
| 110 | ; 1 if any error |
---|
| 111 | ; Corrupts registers: |
---|
| 112 | ; AL, CX |
---|
| 113 | ;-------------------------------------------------------------------- |
---|
| 114 | ALIGN JUMP_ALIGN |
---|
| 115 | HStatus_WaitBsyDefTime: |
---|
| 116 | mov cl, B_TIMEOUT_BSY ; Load timeout value |
---|
| 117 | ALIGN JUMP_ALIGN |
---|
| 118 | HStatus_WaitBsy: |
---|
| 119 | mov dx, [RAMVARS.wIdeBase] ; Load offset to base port |
---|
| 120 | ALIGN JUMP_ALIGN |
---|
| 121 | HStatus_WaitBsyBase: |
---|
| 122 | add dx, BYTE REGR_IDE_ST ; Add offset to status reg |
---|
| 123 | jmp SHORT HStatus_PollBsy ; Wait until not busy |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | ;-------------------------------------------------------------------- |
---|
| 127 | ; Waits until Hard Disk is ready to accept commands. |
---|
| 128 | ; |
---|
| 129 | ; HStatus_WaitRdyDefTime Uses default timeout |
---|
| 130 | ; HStatus_WaitRdy Uses user defined timeout |
---|
| 131 | ; HStatus_WaitRdyBase Uses user base port address and timeout |
---|
| 132 | ; Parameters: |
---|
| 133 | ; CL: Timeout value in system timer ticks (not HStatus_WaitRdyDefTime) |
---|
| 134 | ; DX: IDE Base port address (HStatus_WaitRdyBase only) |
---|
| 135 | ; DS: Segment to RAMVARS |
---|
| 136 | ; Returns: |
---|
| 137 | ; AH: BIOS Error code |
---|
| 138 | ; DX: IDE Status Register Address |
---|
| 139 | ; CF: 0 if wait succesfull |
---|
| 140 | ; 1 if any error |
---|
| 141 | ; Corrupts registers: |
---|
| 142 | ; AL, CX |
---|
| 143 | ;-------------------------------------------------------------------- |
---|
| 144 | ALIGN JUMP_ALIGN |
---|
| 145 | HStatus_WaitRdyDefTime: |
---|
| 146 | mov cl, B_TIMEOUT_RDY ; Load timeout value |
---|
| 147 | ALIGN JUMP_ALIGN |
---|
| 148 | HStatus_WaitRdy: |
---|
| 149 | mov dx, [RAMVARS.wIdeBase] ; Load offset to base port |
---|
| 150 | ALIGN JUMP_ALIGN |
---|
| 151 | HStatus_WaitRdyBase: |
---|
| 152 | add dx, BYTE REGR_IDE_ST ; Add offset to status reg |
---|
| 153 | mov ah, FLG_IDE_ST_DRDY ; Flag to poll |
---|
| 154 | jmp SHORT HStatus_PollBsyAndFlg ; Wait until flag set |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | ;-------------------------------------------------------------------- |
---|
| 158 | ; Waits until Hard Disk is ready to transfer data. |
---|
| 159 | ; Note! This function polls DRQ even if interrupts are enabled! |
---|
| 160 | ; |
---|
| 161 | ; HStatus_WaitDrqDefTime Uses default timeout |
---|
| 162 | ; HStatus_WaitDrq Uses user defined timeout |
---|
| 163 | ; HStatus_WaitDrqBase Uses user base port address and timeout |
---|
| 164 | ; Parameters: |
---|
| 165 | ; CL: Timeout value in system timer ticks (not HStatus_WaitDrqDefTime) |
---|
| 166 | ; DX: IDE Base port address (HStatus_WaitDrqBase only) |
---|
| 167 | ; DS: Segment to RAMVARS |
---|
| 168 | ; Returns: |
---|
| 169 | ; AH: BIOS Error code |
---|
| 170 | ; DX: IDE Status Register Address |
---|
| 171 | ; CF: 0 if wait succesfull |
---|
| 172 | ; 1 if any error |
---|
| 173 | ; Corrupts registers: |
---|
| 174 | ; AL, CX |
---|
| 175 | ;-------------------------------------------------------------------- |
---|
| 176 | ALIGN JUMP_ALIGN |
---|
| 177 | HStatus_WaitDrqDefTime: |
---|
| 178 | mov cl, B_TIMEOUT_DRQ ; Load timeout value |
---|
| 179 | ALIGN JUMP_ALIGN |
---|
| 180 | HStatus_WaitDrq: |
---|
| 181 | mov dx, [RAMVARS.wIdeBase] ; Load offset to base port |
---|
| 182 | ALIGN JUMP_ALIGN |
---|
| 183 | HStatus_WaitDrqBase: |
---|
| 184 | add dx, BYTE REGR_IDE_ST ; Add offset to status reg |
---|
| 185 | mov ah, FLG_IDE_ST_DRQ ; Flag to poll |
---|
| 186 | ; Fall to HStatus_PollBsyAndFlg |
---|
| 187 | |
---|
| 188 | ;-------------------------------------------------------------------- |
---|
| 189 | ; IDE Status register polling. |
---|
| 190 | ; This function first waits until controller is not busy. |
---|
| 191 | ; When not busy, IDE Status Register is polled until wanted |
---|
| 192 | ; flag (HBIT_ST_DRDY or HBIT_ST_DRQ) is set. |
---|
| 193 | ; |
---|
| 194 | ; HStatus_PollBusyAndFlg |
---|
| 195 | ; Parameters: |
---|
| 196 | ; AH: Status Register Flag to poll (until set) when not busy |
---|
| 197 | ; CL: Timeout value in system timer ticks |
---|
| 198 | ; DX: IDE Status Register Address |
---|
| 199 | ; DS: Segment to RAMVARS |
---|
| 200 | ; Returns: |
---|
| 201 | ; AH: BIOS Error code |
---|
| 202 | ; CF: Clear if wait completed successfully (no errors) |
---|
| 203 | ; Set if any error |
---|
| 204 | ; Corrupts registers: |
---|
| 205 | ; AL, CX |
---|
| 206 | ;-------------------------------------------------------------------- |
---|
| 207 | ALIGN JUMP_ALIGN |
---|
| 208 | HStatus_PollBsyAndFlg: |
---|
| 209 | call SoftDelay_InitTimeout ; Initialize timeout counter |
---|
| 210 | ALIGN JUMP_ALIGN |
---|
| 211 | .PollLoop: |
---|
| 212 | in al, dx ; Load IDE Status Register |
---|
| 213 | test al, FLG_IDE_ST_BSY ; Controller busy? |
---|
| 214 | jnz SHORT .UpdateTimeout ; If so, jump to timeout update |
---|
| 215 | test al, ah ; Test secondary flag |
---|
| 216 | jnz SHORT HStatus_PollCompleted ; If set, break loop |
---|
| 217 | ALIGN JUMP_ALIGN |
---|
| 218 | .UpdateTimeout: |
---|
| 219 | call SoftDelay_UpdTimeout ; Update timeout counter |
---|
| 220 | jnc SHORT .PollLoop ; Loop if time left (sets CF on timeout) |
---|
| 221 | mov ah, RET_HD_TIMEOUT ; Load error code for timeout |
---|
| 222 | ret |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | ;-------------------------------------------------------------------- |
---|
| 226 | ; IDE Status register polling. |
---|
| 227 | ; This function waits until controller is not busy. |
---|
| 228 | ; |
---|
| 229 | ; HStatus_PollBsy |
---|
| 230 | ; Parameters: |
---|
| 231 | ; CL: Timeout value in system timer ticks |
---|
| 232 | ; DX: IDE Status Register Address |
---|
| 233 | ; DS: Segment to RAMVARS |
---|
| 234 | ; Returns: |
---|
| 235 | ; AH: BIOS Error code |
---|
| 236 | ; CF: Clear if wait completed successfully (no errors) |
---|
| 237 | ; Set if any error |
---|
| 238 | ; Corrupts registers: |
---|
| 239 | ; AL, CX |
---|
| 240 | ;-------------------------------------------------------------------- |
---|
| 241 | ALIGN JUMP_ALIGN |
---|
| 242 | HStatus_PollBsy: |
---|
| 243 | call SoftDelay_InitTimeout ; Initialize timeout counter |
---|
| 244 | ALIGN JUMP_ALIGN |
---|
| 245 | .PollLoop: |
---|
| 246 | in al, dx ; Load IDE Status Reg |
---|
| 247 | test al, FLG_IDE_ST_BSY ; Controller busy? (clears CF) |
---|
| 248 | jz SHORT HStatus_PollCompleted ; If not, jump to check errors |
---|
| 249 | call SoftDelay_UpdTimeout ; Update timeout counter |
---|
| 250 | jnc SHORT .PollLoop ; Loop if time left (sets CF on timeout) |
---|
| 251 | mov ah, RET_HD_TIMEOUT ; Load error code for timeout |
---|
| 252 | ret |
---|
| 253 | |
---|
| 254 | ALIGN JUMP_ALIGN |
---|
| 255 | HStatus_PollCompleted: |
---|
| 256 | test al, FLG_IDE_ST_DF | FLG_IDE_ST_ERR |
---|
| 257 | jnz SHORT .GetErrorCode ; If errors, jump to get error code |
---|
| 258 | xor ah, ah ; Zero AH and clear CF |
---|
| 259 | ret |
---|
| 260 | .GetErrorCode: |
---|
| 261 | jmp HError_GetErrorCodeForStatusReg |
---|