1 /**
2  * @file flc.h
3  * @brief      Flash Controler driver.
4  * @details    This driver can be used to operate on the embedded flash memory.
5  */
6 /* ****************************************************************************
7  * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
23  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Except as contained in this notice, the name of Maxim Integrated
28  * Products, Inc. shall not be used except as stated in the Maxim Integrated
29  * Products, Inc. Branding Policy.
30  *
31  * The mere transfer of this software does not imply any licenses
32  * of trade secrets, proprietary technology, copyrights, patents,
33  * trademarks, maskwork rights, or any other form of intellectual
34  * property whatsoever. Maxim Integrated Products, Inc. retains all
35  * ownership rights.
36  *
37  * $Date: 2019-06-05 16:53:29 -0500 (Wed, 05 Jun 2019) $
38  * $Revision: 43696 $
39  *
40  *************************************************************************** */
41 
42 /* **** Includes **** */
43 #include <string.h>
44 #include "mxc_config.h"
45 #include "mxc_sys.h"
46 #include "flc.h"
47 #include "flc_regs.h"
48 
49 
50 /* **** Definitions **** */
51 
52 /* **** Globals **** */
53 
54 /* **** Functions **** */
55 
56 // *****************************************************************************
57 #if defined (__ICCARM__)
58 #pragma section=".flashprog"
59 #endif
60 #if defined ( __GNUC__ )
61 __attribute__ ((section(".flashprog")))
62 #endif
prepare_flc(void)63 static int prepare_flc(void)
64 {
65     // Set flash clock divider to generate a 1MHz clock from the APB clock
66     MXC_FLC->clkdiv = SystemCoreClock / 1000000;
67 
68     /* Check if the flash controller is busy */
69     if (FLC_Busy()) {
70         return E_BUSY;
71     }
72 
73     /* Clear stale errors */
74     if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
75         MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
76     }
77 
78     /* Unlock flash */
79     MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_UNLOCK) | MXC_S_FLC_CN_UNLOCK_UNLOCKED;
80 
81     return E_NO_ERROR;
82 }
83 
84 // *****************************************************************************
85 #if defined (__ICCARM__)
86 // IAR memory section declaration for the in-system flash programming functions to be loaded in RAM.
87 #pragma section=".flashprog"
88 #endif
89 #if defined ( __GNUC__ )
90 __attribute__ ((section(".flashprog")))
91 #endif
FLC_Init(const sys_cfg_flc_t * sys_cfg)92 int FLC_Init(const sys_cfg_flc_t *sys_cfg)
93 {
94   SYS_FLC_Init(sys_cfg);
95 
96   return E_NO_ERROR;
97 }
98 
99 // *****************************************************************************
100 #if defined (__ICCARM__)
101 // IAR memory section declaration for the in-system flash programming functions to be loaded in RAM.
102 #pragma section=".flashprog"
103 #endif
104 #if defined ( __GNUC__ )
105 __attribute__ ((section(".flashprog")))
106 #endif
FLC_Busy(void)107 int FLC_Busy(void)
108 {
109     return (MXC_FLC->cn & (MXC_F_FLC_CN_WR | MXC_F_FLC_CN_ME | MXC_F_FLC_CN_PGE));
110 }
111 
112 // *****************************************************************************
113 #if defined (__ICCARM__)
114 #pragma section=".flashprog"
115 #endif
116 #if defined ( __GNUC__ )
117 __attribute__ ((section(".flashprog")))
118 #endif
FLC_MassErase(void)119 int FLC_MassErase(void)
120 {
121     int err;
122 
123     if ((err = prepare_flc()) != E_NO_ERROR)
124         return err;
125 
126     /* Write mass erase code */
127     MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_ERASE_CODE) | MXC_S_FLC_CN_ERASE_CODE_ERASEALL;
128 
129     /* Issue mass erase command */
130     MXC_FLC->cn |= MXC_F_FLC_CN_ME;
131 
132     /* Wait until flash operation is complete */
133     while (FLC_Busy());
134     /* Lock flash */
135     MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
136 
137     /* Check access violations */
138     if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
139         MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
140         return E_BAD_STATE;
141     }
142 
143     SYS_Flash_Operation();
144 
145     return E_NO_ERROR;
146 }
147 
148 // *****************************************************************************
149 #if defined (__ICCARM__)
150 #pragma section=".flashprog"
151 #endif
152 #if defined ( __GNUC__ )
153 __attribute__ ((section(".flashprog")))
154 #endif
FLC_PageErase(uint32_t address)155 int FLC_PageErase(uint32_t address)
156 {
157     int err;
158 
159     if ((err = prepare_flc()) != E_NO_ERROR)
160         return err;
161 
162     // Align address on page boundary
163     address = address - (address % MXC_FLASH_PAGE_SIZE);
164 
165     /* Write page erase code */
166     MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_ERASE_CODE) | MXC_S_FLC_CN_ERASE_CODE_ERASEPAGE;
167     /* Issue page erase command */
168     MXC_FLC->addr = address;
169     MXC_FLC->cn |= MXC_F_FLC_CN_PGE;
170 
171     /* Wait until flash operation is complete */
172     while (FLC_Busy());
173 
174     /* Lock flash */
175     MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
176 
177     /* Check access violations */
178     if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
179         MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
180         return E_BAD_STATE;
181     }
182 
183     SYS_Flash_Operation();
184 
185     return E_NO_ERROR;
186 }
187 
188 // *****************************************************************************
189 #if defined (__ICCARM__)
190 #pragma section=".flashprog"
191 #endif
192 #if defined ( __GNUC__ )
193 __attribute__ ((section(".flashprog")))
194 #endif
FLC_Erase(uint32_t start,uint32_t end)195 int FLC_Erase(uint32_t start, uint32_t end)
196 {
197     int retval;
198     uint32_t addr;
199 
200     // Align start and end on page boundaries
201     start = start - (start % MXC_FLASH_PAGE_SIZE);
202     end = end - (end % MXC_FLASH_PAGE_SIZE);
203 
204     for (addr = start; addr <= end; addr += MXC_FLASH_PAGE_SIZE) {
205         retval = FLC_PageErase(addr);
206         if (retval != E_NO_ERROR)  {
207             return retval;
208         }
209     }
210 
211     return E_NO_ERROR;
212 }
213 
214 // *****************************************************************************
215 #if defined (__ICCARM__)
216 #pragma section=".flashprog"
217 #endif
218 #if defined ( __GNUC__ )
219 __attribute__ ((section(".flashprog")))
220 #endif
FLC_BufferErase(uint32_t start,uint32_t end,uint8_t * buffer,unsigned length)221 int FLC_BufferErase(uint32_t start, uint32_t end, uint8_t *buffer, unsigned length)
222 {
223     int retval;
224     uint32_t start_align, start_len, end_align, end_len;
225 
226     // Align start and end on page boundaries, calculate length of data to buffer
227     start_align = start - (start % MXC_FLASH_PAGE_SIZE);
228     start_len = (start % MXC_FLASH_PAGE_SIZE);
229     end_align = end - (end % MXC_FLASH_PAGE_SIZE);
230     end_len = ((MXC_FLASH_PAGE_SIZE - (end % MXC_FLASH_PAGE_SIZE)) % MXC_FLASH_PAGE_SIZE);
231 
232     // Make sure the length of buffer is sufficient
233     if ((length < start_len) || (length < end_len)) {
234         return E_BAD_PARAM;
235     }
236 
237 
238     // Start and end address are in the same page
239     if (start_align == end_align) {
240         if (length < (start_len + end_len)) {
241             return E_BAD_PARAM;
242         }
243 
244         // Buffer first page data and last page data, erase and write
245         memcpy(buffer, (void*)start_align, start_len);
246         memcpy(&buffer[start_len], (void*)end, end_len);
247         retval = FLC_PageErase(start_align);
248         if (retval != E_NO_ERROR) {
249             return retval;
250         }
251 
252         retval = FLC_Write(start_align, start_len, buffer);
253         if (retval != E_NO_ERROR) {
254             return retval;
255         }
256         retval = FLC_Write(end, end_len, &buffer[start_len]);
257         if (retval != E_NO_ERROR) {
258             return retval;
259         }
260 
261         return E_NO_ERROR;
262     }
263 
264     // Buffer, erase, and write the data in the first page
265     memcpy(buffer, (void*)start_align, start_len);
266     retval = FLC_PageErase(start_align);
267     if (retval != E_NO_ERROR) {
268         return retval;
269     }
270 
271     retval = FLC_Write(start_align, start_len, buffer);
272     if (retval != E_NO_ERROR) {
273         return retval;
274     }
275 
276     // Buffer, erase, and write the data in the last page
277     memcpy(buffer, (void*)end, end_len);
278     retval = FLC_PageErase(end_align);
279     if (retval != E_NO_ERROR) {
280         return retval;
281     }
282 
283     retval = FLC_Write(end, end_len, buffer);
284     if (retval != E_NO_ERROR) {
285         return retval;
286     }
287 
288     // Erase the remaining pages
289     if (start_align != end_align) {
290         return FLC_Erase((start_align + MXC_FLASH_PAGE_SIZE), (end_align - MXC_FLASH_PAGE_SIZE));
291     }
292 
293     return E_NO_ERROR;
294 }
295 
296 // *****************************************************************************
297 #if defined (__ICCARM__)
298 #pragma section=".flashprog"
299 #endif
300 #if defined ( __GNUC__ )
301 __attribute__ ((section(".flashprog")))
302 #endif
FLC_Write32(uint32_t address,uint32_t data)303 int FLC_Write32(uint32_t address, uint32_t data)
304 {
305     int err;
306 
307     // Address checked if it is byte addressable
308     if (address & 0x3) {
309         return E_BAD_PARAM;
310     }
311 
312     if ((err = prepare_flc()) != E_NO_ERROR)
313         return err;
314 
315     // write in 32-bit units
316     MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
317     MXC_FLC->cn &= ~MXC_F_FLC_CN_BRST;
318 
319     // write the data
320     MXC_FLC->addr = address;
321     MXC_FLC->data[0] = data;
322     MXC_FLC->cn |= MXC_F_FLC_CN_WR;
323 
324 
325     /* Wait until flash operation is complete */
326     while (FLC_Busy()) {}
327 
328     /* Lock flash */
329     MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
330 
331     /* Check access violations */
332     if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
333         MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
334         return E_BAD_STATE;
335     }
336 
337     SYS_Flash_Operation();
338 
339     return E_NO_ERROR;
340 }
341 
342 // *****************************************************************************
343 #if defined (__ICCARM__)
344 #pragma section=".flashprog"
345 #endif
346 #if defined ( __GNUC__ )
347 __attribute__ ((section(".flashprog")))
348 #endif
FLC_Write128(uint32_t address,uint32_t * data)349 int FLC_Write128(uint32_t address, uint32_t *data)
350 {
351     int err;
352 
353     // Address checked if it is word addressable
354     if (address & 0xF) {
355         return E_BAD_PARAM;
356     }
357 
358     if ((err = prepare_flc()) != E_NO_ERROR)
359         return err;
360 
361     // write 128-bits
362     MXC_FLC->cn &= ~MXC_F_FLC_CN_WDTH;
363 
364     // write the data
365     MXC_FLC->addr = address;
366     memcpy((void*)&MXC_FLC->data[0], data, 16);
367     MXC_FLC->cn |= MXC_F_FLC_CN_WR;
368 
369     /* Wait until flash operation is complete */
370     while (FLC_Busy());
371 
372     /* Lock flash */
373     MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
374 
375     /* Check access violations */
376     if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
377         MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
378         return E_BAD_STATE;
379     }
380 
381     SYS_Flash_Operation();
382 
383     return E_NO_ERROR;
384 }
385 
386 // *****************************************************************************
387 #if defined (__ICCARM__)
388 #pragma section=".flashprog"
389 #endif
390 #if defined ( __GNUC__ )
391 __attribute__ ((section(".flashprog")))
392 #endif
FLC_Write(uint32_t address,uint32_t length,uint8_t * buffer)393 int FLC_Write(uint32_t address, uint32_t length, uint8_t *buffer)
394 {
395     int err;
396     uint32_t bytes_written;
397     uint8_t current_data[4];
398 
399     if ((err = prepare_flc()) != E_NO_ERROR)
400         return err;
401 
402     // write in 32-bit units until we are 128-bit aligned
403     MXC_FLC->cn &= ~MXC_F_FLC_CN_BRST;
404     MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
405 
406     // Align the address and read/write if we have to
407     if (address & 0x3) {
408 
409         // Figure out how many bytes we have to write to round up the address
410         bytes_written = 4 - (address & 0x3);
411 
412         // Save the data currently in the flash
413         memcpy(current_data, (void*)(address & (~0x3)), 4);
414 
415         // Modify current_data to insert the data from buffer
416         memcpy(&current_data[4-bytes_written], buffer, bytes_written);
417 
418         // Write the modified data
419         MXC_FLC->addr = address - (address % 4);
420         memcpy((void*)&MXC_FLC->data[0], &current_data, 4);
421         MXC_FLC->cn |= MXC_F_FLC_CN_WR;
422 
423         /* Wait until flash operation is complete */
424         while (FLC_Busy());
425 
426         address += bytes_written;
427         length -= bytes_written;
428         buffer += bytes_written;
429     }
430 
431     while ( (length >= 4) && ((address & 0xF) != 0) ) {
432         MXC_FLC->addr = address;
433         memcpy((void*)&MXC_FLC->data[0], buffer, 4);
434         MXC_FLC->cn |= MXC_F_FLC_CN_WR;
435 
436         /* Wait until flash operation is complete */
437         while (FLC_Busy());
438 
439         address += 4;
440         length -= 4;
441         buffer += 4;
442     }
443 
444     if (length >= 16) {
445 
446         // write in 128-bit bursts while we can
447         MXC_FLC->cn &= ~MXC_F_FLC_CN_WDTH;
448 
449         while (length >= 16) {
450             MXC_FLC->addr = address;
451             memcpy((void*)&MXC_FLC->data[0], buffer, 16);
452             MXC_FLC->cn |= MXC_F_FLC_CN_WR;
453 
454             /* Wait until flash operation is complete */
455             while (FLC_Busy());
456 
457             address += 16;
458             length -= 16;
459             buffer += 16;
460     }
461 
462         // Return to 32-bit writes.
463         MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
464     }
465 
466         while (length >= 4) {
467             MXC_FLC->addr = address;
468             memcpy((void*)&MXC_FLC->data[0], buffer, 4);
469             MXC_FLC->cn |= MXC_F_FLC_CN_WR;
470 
471             /* Wait until flash operation is complete */
472             while (FLC_Busy());
473 
474             address += 4;
475             length -= 4;
476             buffer += 4;
477         }
478 
479     if (length > 0) {
480         // Save the data currently in the flash
481         memcpy(current_data, (void*)(address), 4);
482 
483         // Modify current_data to insert the data from buffer
484         memcpy(current_data, buffer, length);
485 
486         MXC_FLC->addr = address;
487         memcpy((void*)&MXC_FLC->data[0], current_data, 4);
488         MXC_FLC->cn |= MXC_F_FLC_CN_WR;
489 
490         /* Wait until flash operation is complete */
491         while (FLC_Busy());
492     }
493 
494     /* Lock flash */
495     MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
496 
497     /* Check access violations */
498     if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
499         MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
500         return E_BAD_STATE;
501     }
502 
503     SYS_Flash_Operation();
504 
505     return E_NO_ERROR;
506 }
507 
FLC_EnableInt(uint32_t mask)508 int FLC_EnableInt(uint32_t mask)
509 {
510   uint32_t tmp;
511 
512   mask &= (MXC_F_FLC_INTR_DONEIE |  MXC_F_FLC_INTR_AFIE);
513   if (!mask) {
514     /* No bits set? Wasn't something we can enable. */
515     return E_BAD_PARAM;
516   }
517 
518   /* Careful with access_fail bit, as it is W0C */
519   tmp = MXC_FLC->intr | MXC_F_FLC_INTR_AF;
520   /* Don't lose done flag */
521   tmp &= ~(MXC_F_FLC_INTR_DONE);
522   /* Apply enables and write back */
523   MXC_FLC->intr = (tmp | mask);
524 
525   return E_NO_ERROR;
526 }
527 
FLC_DisableInt(uint32_t mask)528 int FLC_DisableInt(uint32_t mask)
529 {
530   uint32_t tmp;
531 
532   mask &= (MXC_F_FLC_INTR_DONEIE |  MXC_F_FLC_INTR_AFIE);
533   if (!mask) {
534     /* No bits set? Wasn't something we can disable. */
535     return E_BAD_PARAM;
536   }
537 
538   /* Careful with access_fail bit, as it is W0C */
539   tmp = MXC_FLC->intr | MXC_F_FLC_INTR_AF;
540   /* Don't lose done flag */
541   tmp &= ~(MXC_F_FLC_INTR_DONE);
542   /* Apply disables and write back */
543   MXC_FLC->intr = (tmp & ~mask);
544 
545   return E_NO_ERROR;
546 }
547 
FLC_GetFlags(void)548 int FLC_GetFlags(void)
549 {
550   return (MXC_FLC->intr & (MXC_F_FLC_INTR_DONE | MXC_F_FLC_INTR_AF));
551 }
552 
FLC_ClearFlags(uint32_t mask)553 int FLC_ClearFlags(uint32_t mask)
554 {
555   mask &= (MXC_F_FLC_INTR_DONE | MXC_F_FLC_INTR_AF);
556   if (!mask) {
557     /* No bits set? Wasn't something we can clear. */
558     return E_BAD_PARAM;
559   }
560 
561   // Both bits are write zero clear
562   MXC_FLC->intr ^= mask;
563 
564   return E_NO_ERROR;
565 }
566 
FLC_UnlockInfoBlock()567 int FLC_UnlockInfoBlock()
568 {
569     MXC_FLC->acntl = 0x3a7f5ca3;
570     MXC_FLC->acntl = 0xa1e34f20;
571     MXC_FLC->acntl = 0x9608b2c1;
572     return E_NO_ERROR;
573 }
574 
FLC_LockInfoBlock()575 int FLC_LockInfoBlock()
576 {
577     MXC_FLC->acntl = 0xDEADBEEF;
578     return E_NO_ERROR;
579 }
580