Add internal flash driver for sam3x.
[bertos.git] / bertos / cpu / cortex-m3 / drv / flash_sam3.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Daniele Basile <asterix@develer.com>
34  *
35  * \brief SAM3 Internal flash read/write driver.
36  *
37  *
38  */
39
40 #include "flash_sam3.h"
41
42 #include "cfg/cfg_emb_flash.h"
43 #include <cfg/macros.h>
44
45 // Define log settings for cfg/log.h
46 #define LOG_LEVEL    CONFIG_FLASH_EMB_LOG_LEVEL
47 #define LOG_FORMAT   CONFIG_FLASH_EMB_LOG_FORMAT
48 #include <cfg/log.h>
49
50 #include <cpu/irq.h>
51 #include <cpu/attr.h>
52 #include <cpu/power.h>
53
54 #include <io/kfile.h>
55 #include <io/kblock.h>
56 #include <io/cm3.h>
57
58 #include <drv/timer.h>
59 #include <drv/flash.h>
60
61 #include <string.h>
62
63
64 #define FLASH_MEM_SIZE          0x80000UL ///< Internal flash memory size
65 #define FLASH_PAGE_SIZE_BYTES         256 ///< Size of cpu flash memory page in bytes
66 #define FLASH_BANKS_NUM                 2 ///< Number of flash banks
67 #define FLASH_BASE                    0x0
68
69 struct FlashHardware
70 {
71         uint8_t status;
72 };
73
74
75 /**
76  * Really send the flash write command.
77  *
78  * \note This function has to be placed in RAM because
79  *       executing code from flash while a writing process
80  *       is in progress is forbidden.
81  */
82 RAM_FUNC NOINLINE static void write_page(uint32_t page)
83 {
84         // Send the 'write page' command
85         EEFC0_FCR = EEFC_FCR_FKEY | EFC_FCR_FCMD_EWP | EEFC_FCR_FARG(page);
86
87         // Wait for the end of command
88         while(!(EEFC0_FSR & BV(EEFC_FSR_FRDY)))
89         {
90                 //NOP;
91         }
92 }
93
94
95 /**
96  * Send write command.
97  *
98  * After WR command cpu write bufferd page into flash memory.
99  *
100  */
101 INLINE void flash_sendWRcmd(uint32_t page)
102 {
103         cpu_flags_t flags;
104
105         LOG_INFO("Writing page %ld...\n", page);
106
107         IRQ_SAVE_DISABLE(flags);
108         write_page(page);
109
110         IRQ_RESTORE(flags);
111         LOG_INFO("Done\n");
112 }
113
114 /**
115  * Return true if no error are occurred after flash memory
116  * read or write operation, otherwise return error code.
117  */
118 static bool flash_getStatus(struct KBlock *blk)
119 {
120         Flash *fls = FLASH_CAST(blk);
121         /*
122          * This bit is set to one if an invalid command and/or a bad keywords was/were
123          * written in the Flash Command Register.
124          */
125         if(EEFC0_FSR & BV(EEFC_FSR_FCMDE))
126         {
127                 fls->hw->status |= FLASH_WR_ERR;
128                 LOG_ERR("flash not erased..\n");
129                 return false;
130         }
131
132         /*
133          * This bit is set to one if we programming of at least one locked lock
134          * region.
135          */
136         if(EEFC0_FSR & BV(EEFC_FSR_FLOCKE))
137         {
138                 fls->hw->status |= FLASH_WR_PROTECT;
139                 LOG_ERR("wr protect..\n");
140                 return false;
141         }
142
143         return true;
144 }
145
146 static size_t sam3_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
147 {
148         memcpy(buf, (void *)(idx * blk->blk_size +  FLASH_BASE + offset), size);
149         return size;
150 }
151
152 static size_t sam3_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
153 {
154         ASSERT(offset == 0);
155         ASSERT(size == blk->blk_size);
156
157         uint32_t *addr = (uint32_t *)(idx * blk->blk_size +  FLASH_BASE);
158         const uint8_t *buf = (const uint8_t *)_buf;
159
160         while (size)
161         {
162                 uint32_t data = (*(buf + 3) << 24) |
163                                                 (*(buf + 2) << 16) |
164                                                 (*(buf + 1) << 8)  |
165                                                 *buf;
166                 *addr = data;
167
168                 size -= 4;
169                 buf += 4;
170                 addr++;
171         }
172
173         flash_sendWRcmd(idx);
174
175         if (!flash_getStatus(blk))
176                 return 0;
177
178         return blk->blk_size;
179 }
180
181
182 static int sam3_flash_error(struct KBlock *blk)
183 {
184         Flash *fls = FLASH_CAST(blk);
185         return fls->hw->status;
186 }
187
188 static void sam3_flash_clearerror(struct KBlock *blk)
189 {
190         Flash *fls = FLASH_CAST(blk);
191         fls->hw->status = 0;
192 }
193
194 static const KBlockVTable flash_sam3_buffered_vt =
195 {
196         .readDirect = sam3_flash_readDirect,
197         .writeDirect = sam3_flash_writeDirect,
198
199         .readBuf = kblock_swReadBuf,
200         .writeBuf = kblock_swWriteBuf,
201         .load = kblock_swLoad,
202         .store = kblock_swStore,
203
204         .error = sam3_flash_error,
205         .clearerr = sam3_flash_clearerror,
206 };
207
208 static const KBlockVTable flash_sam3_unbuffered_vt =
209 {
210         .readDirect = sam3_flash_readDirect,
211         .writeDirect = sam3_flash_writeDirect,
212
213         .error = sam3_flash_error,
214         .clearerr = sam3_flash_clearerror,
215 };
216
217 static struct FlashHardware flash_sam3_hw;
218 static uint8_t flash_buf[FLASH_PAGE_SIZE_BYTES];
219
220 static void common_init(Flash *fls)
221 {
222         memset(fls, 0, sizeof(*fls));
223         DB(fls->blk.priv.type = KBT_FLASH);
224
225         fls->hw = &flash_sam3_hw;
226
227         fls->blk.blk_size = FLASH_PAGE_SIZE_BYTES;
228         fls->blk.blk_cnt = FLASH_MEM_SIZE / FLASH_PAGE_SIZE_BYTES;
229 }
230
231 void flash_hw_init(Flash *fls, UNUSED_ARG(int, flags))
232 {
233         common_init(fls);
234         fls->blk.priv.vt = &flash_sam3_buffered_vt;
235         fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
236         fls->blk.priv.buf = flash_buf;
237
238         /* Load the first block in the cache */
239         memcpy(fls->blk.priv.buf, (void *)(FLASH_BASE), fls->blk.blk_size);
240 }
241
242 void flash_hw_initUnbuffered(Flash *fls, UNUSED_ARG(int, flags))
243 {
244         common_init(fls);
245         fls->blk.priv.vt = &flash_sam3_unbuffered_vt;
246 }
247