PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
main_flash.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 
42 /******************************************************************************/
52 /*---------------------------------------------------------------------------*/
53 /* include files */
54 /*---------------------------------------------------------------------------*/
55 #include "main_flash.h"
56 
57 #if defined(PDL_PERIPHERAL_MAIN_FLASH_ACTIVE)
58 
64 
71 #define Flash_Read(addr) *(volatile uint16_t*)((uint32_t)(addr))
72 
80 #define Flash_Write(addr, data) *(volatile uint16_t*)((uint32_t)(addr)) = ( uint16_t)(data)
81 
82 #if defined ( __ICCARM__ )
83 __ramfunc
84 #endif
85 static uint8_t MFlash_CheckToggle( uint16_t* pAddr );
86 
87 #if defined ( __ICCARM__ )
88 __ramfunc
89 #endif
90 static void MFlash_ReadResetCmd(uint16_t* pResetSecAddr);
91 
102 #if defined ( __ICCARM__ )
103 __ramfunc
104 #endif
105 static void MFlash_ReadResetCmd(uint16_t* pu16ResetSecAddr)
106 {
107  uint8_t u8Dummy;
108 
109  /* issue read/reset command */
110  Flash_Write(0x0000, 0xF0U) ;
111  u8Dummy = Flash_Read(pu16ResetSecAddr) ;
112  if(u8Dummy) /* avoid warning */
113  ;
114  return ;
115 }
116 
130 #if defined ( __ICCARM__ )
131 __ramfunc
132 #endif
133 uint8_t MFlash_ChipErase(boolean_t bCrRemain)
134 {
135  uint8_t u8Dummy, u8Cnt;
136  uint32_t u32CrData, u32CrAddr;
137  uint16_t u16WriteData;
138  uint8_t u8RetValue = MFLASH_RET_OK;
139  if(bCrRemain > 1)
140  u8RetValue = MFLASH_RET_INVALID_PARA;
141  else
142  {
143  /*
144  * It can not be interrupted when set to 16-bit operation mode,
145  * as code may switch to 32-bit operation before write is completed.
146  */
147  __disable_irq();
148  /* Save CR value */
149  if (bCrRemain == TRUE)
150  {
151  u32CrData = *(uint32_t*)CR_DATA_ADDR;
152  }
153  /* CPU programming mode setting */
154  FM4_FLASH_IF->FASZR = 0x01;
155  u8Dummy = FM4_FLASH_IF->FASZR;
156  if(u8Dummy) ;
157  Flash_Write(MFLASH_CODE1, 0x00AA);
158  Flash_Write(MFLASH_CODE2, 0x0055);
159  Flash_Write(MFLASH_CODE1, 0x0080);
160  Flash_Write(MFLASH_CODE1, 0x00AA);
161  Flash_Write(MFLASH_CODE2, 0x0055);
162  Flash_Write(MFLASH_CODE1, 0x0010);
163 
164  /* if execution result of the automatic algorithm of flash memory is abnormally completed */
165  if( MFlash_CheckToggle((uint16_t*)0) == MFLASH_CHK_TOGG_ABNORMAL)
166  {
167  /* sending the read/reset command to the reset sector */
168  MFlash_ReadResetCmd((uint16_t*)0) ;
169 
170  /* return flash operation abnormally */
171  u8RetValue = MFLASH_RET_ABNORMAL ;
172  }
173 
174  /* restore CR data in Flash */
175  if (bCrRemain == TRUE)
176  {
177  for(u8Cnt=2,u16WriteData=(uint16_t)u32CrData, u32CrAddr = CR_DATA_ADDR;u8Cnt;u8Cnt--)
178  {
179  /* issue write command */
180  Flash_Write(MFLASH_CODE1, 0x00AA) ;
181  Flash_Write(MFLASH_CODE2, 0x0055) ;
182  Flash_Write(MFLASH_CODE1, 0x00A0) ;
183  Flash_Write((uint16_t*)u32CrAddr, u16WriteData);
184 
185  /* execution result of the automatic algorithm of flash memory is abnormally complete or verify error */
186  if(( MFlash_CheckToggle((uint16_t*)u32CrAddr) == MFLASH_CHK_TOGG_ABNORMAL) ||
187  ( Flash_Read((uint16_t*)u32CrAddr) != u16WriteData))
188  {
189  /* issue read/reset command to reset sector */
190  MFlash_ReadResetCmd((uint16_t*)u32CrAddr) ;
191 
192  /* return flash operation abnormally */
193  u8RetValue = MFLASH_RET_ABNORMAL ;
194  }
195  u16WriteData = (uint16_t)(u32CrData>>16);
196  u32CrAddr += 2;
197  }
198  }
199 
200  /* CPU ROM mode setting */
201  FM4_FLASH_IF->FASZR = 2;
202  u8Dummy = FM4_FLASH_IF->FASZR;
203 
204  /* Recover IRQ */
205  __enable_irq();
206  }
207  return u8RetValue;
208 }
209 
223 #if defined ( __ICCARM__ )
224 __ramfunc
225 #endif
226 uint8_t MFlash_SectorErase(uint16_t* pu16SecAddr)
227 {
228  uint8_t Dummy;
229  uint8_t u8Ret = MFLASH_RET_OK;
230  if(pu16SecAddr == (uint16_t*)0 )
231  u8Ret = MFLASH_RET_INVALID_PARA;
232  else
233  {
234  /*
235  * It can not be interrupted when set to 16-bit operation mode,
236  * as code may switch to 32-bit operation before write is completed.
237  */
238  __disable_irq();
239  /* CPU programming mode setting */
240  FM4_FLASH_IF->FASZR = 0x01;
241  Dummy = FM4_FLASH_IF->FASZR;
242  if(Dummy) ;
243  Flash_Write(MFLASH_CODE1, 0x00AA);
244  Flash_Write(MFLASH_CODE2, 0x0055);
245  Flash_Write(MFLASH_CODE1, 0x0080);
246  Flash_Write(MFLASH_CODE1, 0x00AA);
247  Flash_Write(MFLASH_CODE2, 0x0055);
248  Flash_Write(pu16SecAddr, 0x0030);
249 
250  /* if execution result of the automatic algorithm of flash memory is abnormally completed */
251  if( MFlash_CheckToggle(pu16SecAddr) == MFLASH_CHK_TOGG_ABNORMAL)
252  {
253  /* sending the read/reset command to the reset sector */
254  MFlash_ReadResetCmd(pu16SecAddr) ;
255 
256  /* return flash operation abnormally */
257  u8Ret = MFLASH_RET_ABNORMAL ;
258  }
259 
260  /* CPU ROM mode setting */
261  FM4_FLASH_IF->FASZR = 2;
262  Dummy = FM4_FLASH_IF->FASZR;
263  /* Recover IRQ */
264  __enable_irq();
265 
266  }
267  return u8Ret;
268 }
269 
286 #if defined ( __ICCARM__ )
287 __ramfunc
288 #endif
289 uint8_t MFlash_Write(uint16_t* pu16WriteAddr,
290  uint16_t* pu16WriteData,
291  uint32_t u32EvenSize,
292  boolean_t bVerifyAndEccCheck)
293 {
294  uint8_t u8RetValue = MFLASH_RET_OK ;
295  uint32_t u32Cnt;
296  uint8_t u8Dummy;
297  uint16_t* pu16OrgWriteAddr;
298  uint16_t* pu16OrgWriteData;
299  pu16OrgWriteAddr = pu16WriteAddr;
300  pu16OrgWriteData = pu16WriteData;
301 
302  if((pu16WriteAddr == (uint16_t*)0)
303  || ((u32EvenSize%2) != 0))
304  {
305  /* return parameter invalid */
306  u8RetValue = MFLASH_RET_INVALID_PARA ;
307  }
308  else
309  {
310  /*
311  * It can not be interrupted when set to 16-bit operation mode,
312  * as code may switch to 32-bit operation before write is completed.
313  */
314  __disable_irq();
315  /* CPU programming mode setting */
316  FM4_FLASH_IF->FASZR = 1;
317  u8Dummy = FM4_FLASH_IF->FASZR;
318  if(u8Dummy) /* avoid warning */
319  ;
320  for(u32Cnt=u32EvenSize;u32Cnt;u32Cnt--)
321  {
322  /* issue write command */
323  Flash_Write(MFLASH_CODE1, 0x00AA) ;
324  Flash_Write(MFLASH_CODE2, 0x0055) ;
325  Flash_Write(MFLASH_CODE1, 0x00A0) ;
326  Flash_Write(pu16WriteAddr, (uint16_t)*pu16WriteData);
327 
328  /* execution result of the automatic algorithm of flash memory is abnormally complete or verify error */
329  if(( MFlash_CheckToggle(pu16WriteAddr) == MFLASH_CHK_TOGG_ABNORMAL) ||
330  ( Flash_Read(pu16WriteAddr) != *pu16WriteData))
331  {
332  /* issue read/reset command to reset sector */
333  MFlash_ReadResetCmd(pu16WriteAddr) ;
334 
335  /* return flash operation abnormally */
336  u8RetValue = MFLASH_RET_ABNORMAL ;
337  }
338  /* Prepare next h-word write */
339  pu16WriteAddr++;
340  pu16WriteData++;
341  }
342 
343  /* CPU ROM mode setting */
344  FM4_FLASH_IF->FASZR = 2;
345  u8Dummy = FM4_FLASH_IF->FASZR;
346 
347  /* Recover IRQ */
348  __enable_irq();
349 
350  /* Verify the readback data */
351  if (bVerifyAndEccCheck)
352  {
353  for(u32Cnt=u32EvenSize;u32Cnt;u32Cnt--)
354  {
355  if(*pu16OrgWriteAddr == *pu16OrgWriteData)
356  {
357  pu16OrgWriteAddr++;
358  pu16OrgWriteData++;
359  }
360  else
361  {
362  return MFLASH_RET_ABNORMAL;
363  }
364  }
365 
366  /* Check ECC error */
367  if(bFM4_FLASH_IF_FSTR_ERR)
368  {
369  bFM4_FLASH_IF_FSTR_ERR = 0;
370  return MFLASH_RET_ABNORMAL;
371  }
372  }
373  }
374 
375  return u8RetValue ;
376 }
377 
391 #if defined ( __ICCARM__ )
392 __ramfunc
393 #endif
394 static uint8_t MFlash_CheckToggle( uint16_t* pu16Addr )
395 {
396  uint16_t u16SequenceFlag1, u16SequenceFlag2 ; /* hardware sequence flag */
397  uint8_t u8RetValue = MFLASH_CHK_TOGG_NORMAL ;
398 
399  /* set hardware sequence flag */
400  u16SequenceFlag1 = Flash_Read(pu16Addr) ;
401  u16SequenceFlag2 = Flash_Read(pu16Addr) ;
402  /* if automatic algorithm is executing */
403  while((( u16SequenceFlag1 ^ u16SequenceFlag2) & MFLASH_CHK_TOGG_MASK) == MFLASH_CHK_TOGG_MASK)
404  {
405  /* if exceeds the timing limit */
406  if(( u16SequenceFlag1 & MFLASH_CHK_TLOV_MASK) == MFLASH_CHK_TLOV_MASK)
407  {
408  /* set hardware sequence flag */
409  u16SequenceFlag1 = Flash_Read(pu16Addr) ;
410  u16SequenceFlag2 = Flash_Read(pu16Addr) ;
411 
412  /* if automatic algorithm is executing */
413  if((( u16SequenceFlag1 ^ u16SequenceFlag2) & MFLASH_CHK_TOGG_MASK) == MFLASH_CHK_TOGG_MASK)
414  {
415  /* abnormally complete */
416  u8RetValue = MFLASH_CHK_TOGG_ABNORMAL ;
417 
418  break;
419  }
420  }
421 
422  /* set hardware sequence flag */
423  u16SequenceFlag1 = Flash_Read(pu16Addr) ;
424  u16SequenceFlag2 = Flash_Read(pu16Addr) ;
425  }
426 
427  return u8RetValue ;
428 }
429 
431 
432 #endif
433 
434 /******************************************************************************/
435 /* EOF (not truncated) */
436 /******************************************************************************/