cleanup
[pyfrprog.git] / pkernel / flash.c
1 #include "flash.h"
2 #include "mb91465k.h"
3
4 void FLASH_PrepareWriteHalfWordMode()
5 {
6         /*      Set FLASH Access Mode via BootROM Routine       */
7         /*      For details refer to the Hardware Manual or Data Sheet */
8 #pragma asm
9         ST              RP,@-R15
10         STM0    (R4,R5)
11         STM1    (R12)
12         LDI             #0x01,R4        ; Set FLASH to 16Bit read/write Mode
13         LDI             #0x04,R5        ; Go 4 times through delay loop (64MHz CLKB)
14         LDI             #0xBF60,R12
15         CALL    @R12
16         LDM1    (R12)
17         LDM0    (R4,R5)
18         LD              @R15+,RP
19 #pragma endasm
20         
21         /*      Set the FLASH Interface to Write Timing */
22         /*      For details refer to the Hardware Manual or Data Sheet */
23         /*      Setting shown here is for CLKB = 64MHz  */
24         FMWT_ATD = 1;
25         FMWT_WEXH = 0;
26         FMWT_WTC = 8;
27 }
28
29 void FLASH_PrepareReadMode()
30 {
31         /*      Set FLASH Access Mode via BootROM Routine       */
32         /*      For details refer to the Hardware Manual or Data Sheet */
33 #pragma asm
34         ST              RP,@-R15
35         STM0    (R4,R5)
36         STM1    (R12)
37         LDI             #0x00,R4        ; Set FLASH to 32Bit read/write Mode
38         LDI             #0x04,R5        ; Go 4 times through delay loop (64MHz CLKB)
39         LDI             #0xBF60,R12
40         CALL    @R12
41         LDM1    (R12)
42         LDM0    (R4,R5)
43         LD              @R15+,RP
44 #pragma endasm
45         
46         /*      Set the FLASH Interface to Read Timing  */
47         /*      For details refer to the Hardware Manual or Data Sheet */
48         /*      Setting shown here is for CLKB = 64MHz  */
49         FMWT_ATD = 1;
50         FMWT_EQ = 3;
51         FMWT_WTC = 4;
52 }               
53                 
54 unsigned char FLASH_ChipErase(void)
55 {
56         unsigned char flag = 0;
57
58         /*Set FLASH access mode to 16Bit Write Mode*/
59         FLASH_PrepareWriteHalfWordMode();
60
61         /*Start FLASH Sector Erase Sequence*/
62         *hseq_1 = 0x00AA;
63         *hseq_2 = 0x0055;
64         *hseq_1 = 0x0080;
65         *hseq_1 = 0x00AA;
66         *hseq_2 = 0x0055;
67         *hseq_1 = 0x0010;
68
69         /*Wait for the Auto Algorithm to finish*/
70         while( flag == 0 ) {
71                 /* Feed Hardware Watchdog */
72                 HWWD_CL = 0;
73
74                 if(*hseq_1 & DPOLL) {
75                         flag = 1;
76                 }
77                 if(*hseq_1 & TLOVER) {
78                         if(*hseq_1 & DPOLL) {
79                                 flag = 1;
80                         }
81                         else {
82                                 /*Reset FLASH (keep in mind 16Bit access to FLASH)*/
83                                 *hseq_1 = 0x00F0; // Keep in Mind (16Bit access)
84
85                                 flag = 2;
86                         }
87                 }
88         }
89
90         /*Set FLASH access mode to 32Bit Read Mode*/
91         FLASH_PrepareReadMode();
92
93         return flag;
94 }
95         
96 unsigned char FLASH_SectorErase(unsigned int secadr)
97 {
98         unsigned char flag = 0;
99         volatile unsigned int value = 0;
100         
101         /*      Set FLASH access mode to 16Bit Write Mode       */
102         FLASH_PrepareWriteHalfWordMode();
103                                         
104         secadr |= 0x0003;
105         
106         /*      Start FLASH Sector Erase Sequence       */
107         *hseq_1 = 0x00AA;
108         *hseq_2 = 0x0055;
109         *hseq_1 = 0x0080;
110         *hseq_1 = 0x00AA;
111         *hseq_2 = 0x0055;
112         *(unsigned  short int *)secadr = 0x0030;
113
114         /*      Wait for the Auto Algorithm to start    */
115         while( !( *(unsigned  short int *)secadr & SETIMR ) )
116         {
117                 /*      Feed the Hardware Watchdog      */
118                 HWWD_CL = 0;
119                 
120                 /*      Check for Pending Interrupts    */
121                 if( FLASH_CheckPendingInterrupt() )
122                 {               
123                         /*      Wait for Sector Erase Suspend   */
124                         FLASH_SuspendSectorErase(secadr);
125                         
126                 /*      Set FLASH access mode to 32Bit Read Mode        */
127                 FLASH_PrepareReadMode();
128                         
129                         /*      Keep on checking for pending Interrupts */
130                         while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
131                         
132                         /*      Set FLASH access mode to 16Bit Write Mode       */
133                 FLASH_PrepareWriteHalfWordMode();
134                                                 
135                         /*      Sector Erase Resume     */
136                         FLASH_ResumeSectorErase(secadr);
137                 }               
138         }
139
140         /*      Wait for the Auto Algorithm to finish   */
141         while( flag == 0 )
142         {
143                 /* Feed Hardware Watchdog */
144                 HWWD_CL = 0;
145
146                 /*      Check for Pending Interrupts    */
147                 if( FLASH_CheckPendingInterrupt() )
148                 {               
149                         /*      Sector Erase Suspend    */
150                         FLASH_SuspendSectorErase(secadr);
151                         
152                 /*      Set FLASH access mode to 32Bit Read Mode        */
153                 FLASH_PrepareReadMode();
154                                         
155                         /*      Keep on checking for pending Interrupts */
156                         while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
157                         
158                         /*      Set FLASH access mode to 16Bit Write Mode       */
159                 FLASH_PrepareWriteHalfWordMode();
160                                                 
161                         /*      Sector Erase Resume     */
162                         FLASH_ResumeSectorErase(secadr);
163                 }               
164                 
165                         
166                 /*      Check the Hardware Sequence Flags       */                              
167                 if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
168                 {
169                         flag = 1;
170                 }
171                 if( ( *(unsigned short int *)secadr /* value */ & TLOVER ) )
172                 {
173                         if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
174                         {
175                                 flag = 1;
176                         }
177                         else
178                         {
179                                 /*      Reset FLASH     */
180                                 FLASH_ReadReset();
181                                 
182                                 flag = 2;
183                         }
184                 }
185         }
186         /*      Set FLASH access mode to 32Bit Read Mode        */
187         FLASH_PrepareReadMode();
188                 
189         return flag;
190 }
191
192 unsigned char FLASH_SectorBlankCheck(unsigned int secaddr, unsigned int size)
193 {
194         unsigned int count;
195         unsigned char empty_flag = 0;
196         unsigned int addr = secaddr;
197                 
198         /*      Clear FIXE bit to see FLASH memory content instead of fixed reset vector        */
199         FMCS_FIXE = 0;
200         
201         for(count = 0; count < size; count ++)
202         {
203                 /*      Clear Hardware Watchdog */
204                 HWWD_CL = 0;
205                 if( *(unsigned int *)addr != 0xFFFFFFFF ) empty_flag = 1;
206                 addr += 4;
207         }
208         
209         /*      Set FIXE bit to see fixed reset vector  */
210         FMCS_FIXE = 1;
211
212         if( empty_flag != 0 )
213         {
214                 return 2;
215         }
216         
217         return 1;
218 }
219
220 unsigned char FLASH_WriteHalfWord(unsigned int adr, unsigned short int data)
221 {
222         unsigned char flag = 0;
223
224         /*      Set FLASH access mode to 16Bit Write Mode       */
225         FLASH_PrepareWriteHalfWordMode();
226         
227         /*      Start Write FLASH Sequence      */
228         *hseq_1 = 0x00AA;
229         *hseq_2 = 0x0055;
230         *hseq_1 = 0x00A0;
231         *((volatile unsigned short int *)adr) = data;
232         
233         /*      Wait for the Auto Algorithm to finish   */
234         while( flag == 0 )
235         {
236                 /* Feed Hardware Watchdog */
237                 HWWD_CL = 0;
238                 
239                 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
240                 {
241                         flag = 1;
242                 }
243                 if( ( *(volatile unsigned short int *)adr & TLOVER ) == TLOVER )
244                 {
245                         if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
246                         {
247                                 flag = 1;
248                         }
249                         else
250                         {
251                                 /*      Reset FLASH (keep in mind 16Bit access to FLASH)        */
252                                 *hseq_1 = 0x00F0;       // Keep in Mind (16Bit access)
253                                 
254                                 flag = 2;
255                         }
256                 }
257         }
258
259         /*      Set FLASH access mode to 32Bit Read Mode        */
260         FLASH_PrepareReadMode();
261         
262         return flag;
263 }
264
265
266 unsigned char FLASH_ReadReset()
267 {
268         *hseq_1 = 0x00F0;
269         
270         return 1;
271 }
272
273
274 unsigned char FLASH_SuspendSectorErase(unsigned int secaddr)
275 {               
276         /* Write Sector Erase Suspend Command   */
277         *(volatile unsigned short int *)secaddr = 0x00B0;
278
279         /*      Wait for the FLASH macro to suspend sector erase        */              
280         while(!(*(unsigned short int *)secaddr /* value */ & DPOLL) && (*(unsigned short int *)secaddr /* value */ & SETIMR))
281         {
282                 HWWD_CL=0;
283         }
284         
285         return 1;
286 }
287
288 unsigned char FLASH_ResumeSectorErase(unsigned int secaddr)
289 {
290         /*      Write the Sector Erase Resume Command   */
291         *(volatile unsigned short int *)secaddr = 0x0030;
292         
293         /*      Wait for the FLASH Macro to resume sector erase */              
294         while((*(unsigned short int *)secaddr /*value */ & DPOLL) && !(*(unsigned short int *)secaddr /*value */ & SETIMR))
295         {
296                 HWWD_CL=0;
297         }
298         
299         return 1;               
300 }
301
302 unsigned char FLASH_CheckPendingInterrupt()
303 {
304         /*      Poll for Pending Interrupts which are needed here       */
305         if(TMCSR0_UF) return 1; /* in this sample the only interrupt source supervised is Reload Timer 0        */
306         
307         /*      and return 1 when an Interrupt is pending       */
308         return 0;
309 }