[620] | 1 | ; Project name : XTIDE Universal BIOS Configurator v2 |
---|
| 2 | ; Description : Functions for flashing SST flash devices. |
---|
| 3 | |
---|
| 4 | ; |
---|
[621] | 5 | ; XTIDE Universal BIOS and Associated Tools |
---|
| 6 | ; Copyright (C) 2009-2010 by Tomi Tilli, 2011-2021 by XTIDE Universal BIOS Team. |
---|
[620] | 7 | ; |
---|
[621] | 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 | ; |
---|
[620] | 19 | |
---|
| 20 | ; Section containing code |
---|
| 21 | SECTION .text |
---|
| 22 | |
---|
| 23 | ;-------------------------------------------------------------------- |
---|
| 24 | ; FlashSst_WithFlashvarsInDSSI |
---|
| 25 | ; Parameters: |
---|
| 26 | ; DS:BX: Ptr to FLASHVARS |
---|
| 27 | ; Returns: |
---|
| 28 | ; Updated FLASHVARS in DS:BX |
---|
| 29 | ; Corrupts registers: |
---|
| 30 | ; AX, DX, DI |
---|
| 31 | ;-------------------------------------------------------------------- |
---|
| 32 | ALIGN JUMP_ALIGN |
---|
| 33 | FlashSst_WithFlashvarsInDSBX: |
---|
| 34 | push ds |
---|
| 35 | push es |
---|
| 36 | push bx |
---|
| 37 | push cx |
---|
| 38 | push si |
---|
| 39 | push bp |
---|
[621] | 40 | mov bp, bx ; Flashvars now in SS:BP (Assumes SS=DS) |
---|
[620] | 41 | |
---|
| 42 | mov BYTE [bp+FLASHVARS.flashResult], FLASH_RESULT.DeviceNotDetected |
---|
| 43 | call DetectSstDevice |
---|
| 44 | jc SHORT .ExitOnError |
---|
| 45 | |
---|
| 46 | call CalibrateSstTimeout |
---|
[621] | 47 | |
---|
[620] | 48 | mov BYTE [bp+FLASHVARS.flashResult], FLASH_RESULT.PollingTimeoutError |
---|
| 49 | mov cx, [bp+FLASHVARS.wPagesToFlash] |
---|
[621] | 50 | mov dx, [bp+FLASHVARS.wEepromPageSize] |
---|
| 51 | les di, [bp+FLASHVARS.fpNextSourcePage] |
---|
| 52 | lds si, [bp+FLASHVARS.fpNextDestinationPage] |
---|
[620] | 53 | %ifdef CLD_NEEDED |
---|
| 54 | cld |
---|
| 55 | %endif |
---|
| 56 | |
---|
| 57 | ALIGN JUMP_ALIGN |
---|
| 58 | .NextPage: |
---|
| 59 | ; See if this page needs updating. |
---|
| 60 | push si |
---|
| 61 | push di |
---|
| 62 | push cx |
---|
[621] | 63 | mov cx, dx |
---|
[620] | 64 | repe cmpsb |
---|
| 65 | pop cx |
---|
| 66 | pop di |
---|
| 67 | pop si |
---|
[621] | 68 | jne SHORT .FlashThisPage |
---|
| 69 | add si, dx |
---|
| 70 | add di, dx |
---|
[620] | 71 | jmp SHORT .ContinueLoop |
---|
| 72 | |
---|
| 73 | .FlashThisPage: |
---|
| 74 | call EraseSstPage |
---|
| 75 | jc SHORT .ExitOnError |
---|
| 76 | call WriteSstPage |
---|
| 77 | jc SHORT .ExitOnError |
---|
| 78 | .ContinueLoop: |
---|
| 79 | loop .NextPage |
---|
| 80 | |
---|
| 81 | ; The write process has already confirmed the results one byte at a time. |
---|
[621] | 82 | ; Here we do an additional verify check just in case there was some |
---|
[620] | 83 | ; kind of oddity with pages / addresses. |
---|
| 84 | mov BYTE [bp+FLASHVARS.flashResult], FLASH_RESULT.DataVerifyError |
---|
[621] | 85 | %ifndef USE_186 |
---|
[620] | 86 | mov ax, [bp+FLASHVARS.wPagesToFlash] |
---|
[621] | 87 | mov cl, SST_PAGE_SIZE_SHIFT - 1 ; -1 because we compare WORDs (verifying 64 KB won't work otherwise) |
---|
[620] | 88 | shl ax, cl |
---|
[621] | 89 | xchg cx, ax |
---|
| 90 | %else |
---|
| 91 | mov cx, [bp+FLASHVARS.wPagesToFlash] |
---|
| 92 | shl cx, SST_PAGE_SIZE_SHIFT - 1 |
---|
| 93 | %endif |
---|
[620] | 94 | lds si, [bp+FLASHVARS.fpNextSourcePage] |
---|
| 95 | les di, [bp+FLASHVARS.fpNextDestinationPage] |
---|
[621] | 96 | repe cmpsw |
---|
| 97 | jne SHORT .ExitOnError |
---|
[620] | 98 | |
---|
| 99 | %ifndef CHECK_FOR_UNUSED_ENTRYPOINTS |
---|
| 100 | %if FLASH_RESULT.success = 0 ; Just in case this should ever change |
---|
| 101 | mov [bp+FLASHVARS.flashResult], cl |
---|
| 102 | %else |
---|
| 103 | mov BYTE [bp+FLASHVARS.flashResult], FLASH_RESULT.success |
---|
| 104 | %endif |
---|
| 105 | %endif |
---|
| 106 | ALIGN JUMP_ALIGN |
---|
| 107 | .ExitOnError: |
---|
| 108 | pop bp |
---|
| 109 | pop si |
---|
| 110 | pop cx |
---|
| 111 | pop bx |
---|
| 112 | pop es |
---|
| 113 | pop ds |
---|
| 114 | ret |
---|
| 115 | |
---|
| 116 | ;-------------------------------------------------------------------- |
---|
| 117 | ; DetectSstDevice |
---|
| 118 | ; Parameters: |
---|
| 119 | ; SS:BP: Ptr to FLASHVARS |
---|
| 120 | ; Returns: |
---|
| 121 | ; CF: Clear if supported SST device found |
---|
| 122 | ; Set if supported SST device not found |
---|
| 123 | ; Corrupts registers: |
---|
[621] | 124 | ; AX, BX, SI, DS |
---|
[620] | 125 | ;-------------------------------------------------------------------- |
---|
| 126 | ALIGN JUMP_ALIGN |
---|
| 127 | DetectSstDevice: |
---|
[621] | 128 | lds si, [bp+FLASHVARS.fpNextDestinationPage] |
---|
| 129 | mov bx, 5555h |
---|
[620] | 130 | |
---|
| 131 | cli |
---|
[621] | 132 | mov BYTE [bx], 0AAh ; Enter software ID sequence. |
---|
| 133 | shr bx, 1 ; BX=2AAAh, CF=1 |
---|
| 134 | mov BYTE [bx], 55h |
---|
| 135 | eRCL_IM bx, 1 ; BX=5555h |
---|
| 136 | mov BYTE [bx], 90h |
---|
| 137 | mov al, [si] ; Extra reads to be sure device |
---|
| 138 | mov al, [si] ; has time to respond. |
---|
| 139 | mov al, [si] |
---|
| 140 | mov ah, [si] ; Vendor ID in AH. |
---|
| 141 | mov al, [si+1] ; Device ID in AL. |
---|
| 142 | mov BYTE [bx], 0F0h ; Exit software ID. |
---|
[620] | 143 | sti |
---|
| 144 | |
---|
[621] | 145 | cmp ax, 0BFB4h |
---|
[620] | 146 | jb SHORT .NotValidDevice |
---|
[621] | 147 | cmp ax, 0BFB7h+1 |
---|
| 148 | cmc |
---|
| 149 | .NotValidDevice: |
---|
[620] | 150 | ret |
---|
| 151 | |
---|
| 152 | ;-------------------------------------------------------------------- |
---|
| 153 | ; CalibrateSstTimeout |
---|
| 154 | ; Parameters: |
---|
| 155 | ; SS:BP: Ptr to FLASHVARS |
---|
| 156 | ; Returns: |
---|
| 157 | ; FLASHVARS.wTimeoutCounter |
---|
| 158 | ; Corrupts registers: |
---|
| 159 | ; AX, BX, CX, SI, DI, DS, ES |
---|
| 160 | ;-------------------------------------------------------------------- |
---|
| 161 | ALIGN JUMP_ALIGN |
---|
| 162 | CalibrateSstTimeout: |
---|
[621] | 163 | LOAD_BDA_SEGMENT_TO es, cx, ! |
---|
| 164 | mov ds, [bp+FLASHVARS.fpNextDestinationPage+2] |
---|
| 165 | mov bx, BDA.dwTimerTicks |
---|
[620] | 166 | mov si, cx |
---|
| 167 | mov di, cx |
---|
[621] | 168 | mov al, [di] |
---|
| 169 | inc ax ; Forces poll to fail |
---|
[620] | 170 | |
---|
[621] | 171 | mov ah, [es:bx] ; Read low byte only |
---|
| 172 | inc ah |
---|
[620] | 173 | .WaitForFirstIncrement: |
---|
[621] | 174 | cmp ah, [es:bx] |
---|
| 175 | jne SHORT .WaitForFirstIncrement |
---|
[620] | 176 | |
---|
[621] | 177 | inc ah |
---|
[620] | 178 | |
---|
| 179 | .WaitForSecondIncrement: |
---|
[621] | 180 | inc ch ; CX now 0x0100 |
---|
| 181 | .PollLoop: ; Identical to poll loop used |
---|
| 182 | cmp [di], al ; during programming |
---|
| 183 | loopne .PollLoop ; Will never be equal in this case |
---|
| 184 | inc si ; Number of poll loops completed |
---|
| 185 | jz SHORT .CountOverflow |
---|
| 186 | cmp ah, [es:bx] |
---|
| 187 | jne SHORT .WaitForSecondIncrement |
---|
| 188 | SKIP1B al |
---|
| 189 | .CountOverflow: |
---|
[620] | 190 | ; Clamp on overflow, although it should not be possible on |
---|
| 191 | ; real hardware. In principle SI could overflow on a very |
---|
| 192 | ; fast CPU. However the SST device is on a slow bus. Even |
---|
| 193 | ; running at the min read cycle time of fastest version of |
---|
| 194 | ; the device, SI can not overflow. |
---|
| 195 | dec si |
---|
| 196 | |
---|
[621] | 197 | ; SI ~= number of polling loops in 215us. |
---|
| 198 | mov [bp+FLASHVARS.wTimeoutCounter], si |
---|
| 199 | ret |
---|
| 200 | |
---|
[620] | 201 | ;-------------------------------------------------------------------- |
---|
| 202 | ; EraseSstPage |
---|
| 203 | ; Parameters: |
---|
[621] | 204 | ; DS:SI: Destination ptr |
---|
[620] | 205 | ; Returns: |
---|
| 206 | ; CF: Set on error. |
---|
| 207 | ; Corrupts registers: |
---|
[621] | 208 | ; AX, BX |
---|
[620] | 209 | ;-------------------------------------------------------------------- |
---|
| 210 | ALIGN JUMP_ALIGN |
---|
| 211 | EraseSstPage: |
---|
| 212 | push cx |
---|
| 213 | |
---|
[621] | 214 | mov bx, 5555h |
---|
| 215 | mov ax, 2AAAh |
---|
[620] | 216 | |
---|
[621] | 217 | ; Sector erase sequence. |
---|
| 218 | mov [bx], al ; [5555h] <- AAh |
---|
| 219 | xchg bx, ax |
---|
| 220 | mov [bx], al ; [2AAAh] <- 55h |
---|
| 221 | xchg bx, ax |
---|
| 222 | mov BYTE [bx], 80h ; [5555h] <- 80h |
---|
| 223 | mov [bx], al ; [5555h] <- AAh |
---|
| 224 | xchg bx, ax |
---|
| 225 | mov [bx], al ; [2AAAh] <- 55h |
---|
| 226 | mov BYTE [si], 30h |
---|
| 227 | |
---|
| 228 | or bl, al ; BL = 0FFh |
---|
[620] | 229 | mov ax, 1163 ; 1163 x ~215us = 250ms = 10x datasheet max |
---|
| 230 | .TimeoutOuterLoop: |
---|
| 231 | mov cx, [bp+FLASHVARS.wTimeoutCounter] |
---|
| 232 | .TimeoutInnerLoop: |
---|
[621] | 233 | cmp [si], bl ; Will return 0FFh when erase complete |
---|
| 234 | loopne .TimeoutInnerLoop |
---|
| 235 | je SHORT .Return |
---|
[620] | 236 | dec ax |
---|
| 237 | jnz SHORT .TimeoutOuterLoop |
---|
[621] | 238 | ; Timed out (CF=1) |
---|
| 239 | .Return: |
---|
[620] | 240 | pop cx |
---|
| 241 | ret |
---|
| 242 | |
---|
| 243 | ;-------------------------------------------------------------------- |
---|
| 244 | ; WriteSstPage |
---|
| 245 | ; Parameters: |
---|
[621] | 246 | ; DX: EEPROM page size |
---|
| 247 | ; DS:SI: Destination ptr |
---|
| 248 | ; ES:DI: Source ptr |
---|
[620] | 249 | ; Returns: |
---|
| 250 | ; SI, DI: Each advanced forward 1 page. |
---|
| 251 | ; CF: Set on error. |
---|
| 252 | ; Corrupts registers: |
---|
[621] | 253 | ; AL, BX |
---|
[620] | 254 | ;-------------------------------------------------------------------- |
---|
| 255 | ALIGN JUMP_ALIGN |
---|
| 256 | WriteSstPage: |
---|
| 257 | push cx |
---|
[621] | 258 | push dx |
---|
[620] | 259 | |
---|
| 260 | mov bx, [bp+FLASHVARS.wTimeoutCounter] |
---|
[621] | 261 | xchg si, di |
---|
[620] | 262 | cli |
---|
| 263 | |
---|
| 264 | .NextByte: |
---|
[621] | 265 | es lodsb ; Read byte from ES:SI |
---|
| 266 | mov BYTE [5555h], 0AAh ; Byte program sequence. |
---|
| 267 | mov BYTE [2AAAh], 55h |
---|
| 268 | mov BYTE [5555h], 0A0h |
---|
| 269 | mov [di], al ; Write byte to DS:DI |
---|
[620] | 270 | |
---|
| 271 | mov cx, bx |
---|
| 272 | .WaitLoop: |
---|
[621] | 273 | cmp [di], al ; Device won't return actual data until write complete. |
---|
| 274 | loopne .WaitLoop ; Timeout ~215us, or ~10x 20us max program time from datasheet. |
---|
| 275 | jne SHORT .WriteTimeout |
---|
[620] | 276 | |
---|
| 277 | inc di |
---|
| 278 | dec dx |
---|
| 279 | jnz SHORT .NextByte |
---|
[621] | 280 | SKIP1B al |
---|
| 281 | .WriteTimeout: |
---|
| 282 | stc |
---|
[620] | 283 | sti |
---|
[621] | 284 | xchg si, di |
---|
| 285 | pop dx |
---|
[620] | 286 | pop cx |
---|
| 287 | ret |
---|