source: xtideuniversalbios/trunk/XTIDE_Universal_BIOS_Configurator_v2/Src/FlashSST.asm @ 620

Last change on this file since 620 was 620, checked in by krille_n_, 2 years ago

Changes:

  • A huge thank you to Jayeson Lee-Steere for adding SST39SF0x0 flash ROM programming support to the configurator (XTIDECFG.COM). This means that there is no longer a need to use a separate program for flashing the Lo-Tech boards and other devices using these flash ROMs.
File size: 6.5 KB
RevLine 
[620]1; Project name  :   XTIDE Universal BIOS Configurator v2
2; Description   :   Functions for flashing SST flash devices.
3
4;
5; Created by Jayeson Lee-Steere
6; Hereby placed into the public domain.
7;
8
9; Section containing code
10SECTION .text
11
12;--------------------------------------------------------------------
13; FlashSst_WithFlashvarsInDSSI
14;   Parameters:
15;       DS:BX:  Ptr to FLASHVARS
16;   Returns:
17;       Updated FLASHVARS in DS:BX
18;   Corrupts registers:
19;       AX, DX, DI
20;--------------------------------------------------------------------
21ALIGN JUMP_ALIGN
22FlashSst_WithFlashvarsInDSBX:
23    push    ds
24    push    es
25    push    bx
26    push    cx
27    push    si
28    push    bp
29    mov     bp, bx                  ; Flashvars now in SS:BP.
30
31    mov     BYTE [bp+FLASHVARS.flashResult], FLASH_RESULT.DeviceNotDetected
32    call    DetectSstDevice
33    jc      SHORT .ExitOnError
34
35    call    CalibrateSstTimeout
36   
37    mov     BYTE [bp+FLASHVARS.flashResult], FLASH_RESULT.PollingTimeoutError
38    mov     cx, [bp+FLASHVARS.wPagesToFlash]
39    lds     si, [bp+FLASHVARS.fpNextSourcePage]
40    les     di, [bp+FLASHVARS.fpNextDestinationPage]
41%ifdef CLD_NEEDED
42    cld
43%endif
44
45ALIGN JUMP_ALIGN
46.NextPage:
47    ; See if this page needs updating.
48    push    si
49    push    di
50    push    cx
51    mov     cx, [bp+FLASHVARS.wEepromPageSize]
52    mov     bx, cx
53    repe cmpsb
54    pop     cx
55    pop     di
56    pop     si
57    jnz     SHORT .FlashThisPage
58    add     si, bx
59    add     di, bx
60    jmp     SHORT .ContinueLoop
61
62.FlashThisPage:
63    call    EraseSstPage
64    jc      SHORT .ExitOnError
65    call    WriteSstPage
66    jc      SHORT .ExitOnError
67.ContinueLoop:
68    loop    .NextPage
69
70    ; The write process has already confirmed the results one byte at a time.
71    ; Here we do an additional verify check just in case there was some 
72    ; kind of oddity with pages / addresses.
73    mov     BYTE [bp+FLASHVARS.flashResult], FLASH_RESULT.DataVerifyError
74    mov     ax, [bp+FLASHVARS.wPagesToFlash]
75    mov     cl, SST_PAGE_SIZE_SHIFT
76    shl     ax, cl
77    mov     cx, ax
78    lds     si, [bp+FLASHVARS.fpNextSourcePage]
79    les     di, [bp+FLASHVARS.fpNextDestinationPage]
80    repe cmpsb
81    jnz     SHORT .ExitOnError
82
83%ifndef CHECK_FOR_UNUSED_ENTRYPOINTS
84%if FLASH_RESULT.success = 0    ; Just in case this should ever change
85    mov     [bp+FLASHVARS.flashResult], cl
86%else
87    mov     BYTE [bp+FLASHVARS.flashResult], FLASH_RESULT.success
88%endif
89%endif
90ALIGN JUMP_ALIGN
91.ExitOnError:
92    pop     bp
93    pop     si
94    pop     cx
95    pop     bx
96    pop     es
97    pop     ds
98    ret
99
100;--------------------------------------------------------------------
101; DetectSstDevice
102;   Parameters:
103;       SS:BP:  Ptr to FLASHVARS
104;   Returns:
105;       CF: Clear if supported SST device found
106;           Set if supported SST device not found
107;   Corrupts registers:
108;       AX, DI, ES
109;--------------------------------------------------------------------
110ALIGN JUMP_ALIGN
111DetectSstDevice:
112    les     di, [bp+FLASHVARS.fpNextDestinationPage]
113
114    cli
115    mov     BYTE [es:05555h], 0AAh  ; Enter software ID sequence.
116    mov     BYTE [es:02AAAh], 055h
117    mov     BYTE [es:05555h], 090h
118    mov     al, [es:di]             ; Extra reads to be sure device
119    mov     al, [es:di]             ; has time to respond.
120    mov     al, [es:di]
121    mov     ah, [es:di]             ; Vendor ID in AH.
122    mov     al, [es:di + 1]         ; Device ID in AL.
123    mov     BYTE [es:05555h], 0F0h  ; Exit software ID.
124    sti
125
126    cmp     al, 0B4h
127    jb      SHORT .NotValidDevice
128    cmp     al, 0B7h
129    ja      SHORT .NotValidDevice
130    cmp     ah, 0BFh
131    jne     SHORT .NotValidDevice
132    ret
133
134.NotValidDevice:
135    stc
136    ret
137   
138;--------------------------------------------------------------------
139; CalibrateSstTimeout
140;   Parameters:
141;       SS:BP:  Ptr to FLASHVARS
142;   Returns:
143;       FLASHVARS.wTimeoutCounter
144;   Corrupts registers:
145;       AX, BX, CX, SI, DI, DS, ES
146;--------------------------------------------------------------------
147ALIGN JUMP_ALIGN
148CalibrateSstTimeout:
149    LOAD_BDA_SEGMENT_TO ds, ax
150    les     di, [bp+FLASHVARS.fpNextDestinationPage]
151    xor     cx, cx
152    mov     si, cx
153    mov     di, cx
154    mov     al, [es:di]
155    not     al                          ; Forces poll to fail.
156
157    mov     bx, [BDA.dwTimerTicks]      ; Read low word only.
158    inc     bx
159.WaitForFirstIncrement:
160    cmp     bx, [BDA.dwTimerTicks]
161    jnz     SHORT .WaitForFirstIncrement
162
163    inc     bx
164
165.WaitForSecondIncrement:
166    inc     ch                          ; cx now 0x0100
167.PollLoop:                              ; Identical to poll loop used 
168    cmp     [es:di], al                 ; during programming
169    jz      SHORT .PollComplete         ; Will never branch in this case
170    loop    .PollLoop
171.PollComplete:
172    add     si, 1                       ; number of poll loops completed
173    jc      SHORT .countOverflow
174    cmp     bx, [BDA.dwTimerTicks]
175    jnz     SHORT .WaitForSecondIncrement
176
177.CalComplete:
178    ; SI ~= number of polling loops in 215us.
179    mov     [bp+FLASHVARS.wTimeoutCounter], si
180    ret
181       
182.countOverflow:
183    ; Clamp on overflow, although it should not be possible on
184    ; real hardware. In principle SI could overflow on a very
185    ; fast CPU. However the SST device is on a slow bus. Even
186    ; running at the min read cycle time of fastest version of
187    ; the device, SI can not overflow.
188    dec     si
189    jmp     SHORT .CalComplete
190
191;--------------------------------------------------------------------
192; EraseSstPage
193;   Parameters:
194;       ES:DI:  Destination ptr.
195;   Returns:
196;       CF:     Set on error.
197;   Corrupts registers:
198;       AX
199;--------------------------------------------------------------------
200ALIGN JUMP_ALIGN
201EraseSstPage:
202    push    cx
203
204    mov     BYTE [es:05555h], 0AAh  ; Sector erase sequence.
205    mov     BYTE [es:02AAAh], 055h
206    mov     BYTE [es:05555h], 080h
207    mov     BYTE [es:05555h], 0AAh
208    mov     BYTE [es:02AAAh], 055h
209    mov     BYTE [es:di], 030h
210
211    mov     ax, 1163                ; 1163 x ~215us = 250ms = 10x datasheet max
212.TimeoutOuterLoop:
213    mov     cx, [bp+FLASHVARS.wTimeoutCounter]
214.TimeoutInnerLoop:
215    cmp     BYTE [es:di], 0FFh      ; Will return 0FFh when erase complete.
216    jz      SHORT .Exit
217    loop    .TimeoutInnerLoop
218    dec     ax
219    jnz     SHORT .TimeoutOuterLoop
220    stc                             ; Timed out.
221.Exit:
222    pop     cx
223    ret
224
225;--------------------------------------------------------------------
226; WriteSstPage
227;   Parameters:
228;       DS:SI:  Source ptr.
229;       ES:DI:  Destination ptr.
230;   Returns:
231;       SI, DI: Each advanced forward 1 page.
232;       CF:     Set on error.
233;   Corrupts registers:
234;       AL, BX, DX
235;--------------------------------------------------------------------
236ALIGN JUMP_ALIGN
237WriteSstPage:
238    push    cx
239
240    mov     bx, [bp+FLASHVARS.wTimeoutCounter]
241    mov     dx, [bp+FLASHVARS.wEepromPageSize]
242    cli
243
244.NextByte:
245    lodsb
246    mov     BYTE [es:05555h], 0AAh  ; Byte program sequence.
247    mov     BYTE [es:02AAAh], 055h
248    mov     BYTE [es:05555h], 0A0h
249    mov     [es:di], al
250
251    mov     cx, bx
252.WaitLoop:
253    cmp     [es:di], al             ; Device won't return actual data until 
254    jz      SHORT .ByteFinished     ; write complete. Timeout ~215us, or 
255    loop    .WaitLoop               ; ~10x 20us max program time from datasheet.
256
257    stc                             ; Write timeout.
258    jmp     SHORT .Exit
259
260.ByteFinished:
261    inc     di
262    dec     dx
263    jnz     SHORT .NextByte
264    clc
265.Exit:
266    sti
267    pop     cx
268    ret
Note: See TracBrowser for help on using the repository browser.