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