Add embedded flash module for stm32. First refactor of emb flash using kblock.
[bertos.git] / bertos / cpu / cortex-m3 / drv / flash_stm32.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 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief STM32F103xx internal flash memory driver.
34  *
35  * \author Daniele Basile <asterix@develer.com>
36  */
37
38 #include "flash_stm32.h"
39
40 #include "cfg/cfg_emb_flash.h"
41
42 // Define log settings for cfg/log.h
43 #define LOG_LEVEL    CONFIG_FLASH_EMB_LOG_LEVEL
44 #define LOG_FORMAT   CONFIG_FLASH_EMB_LOG_FORMAT
45 #include <cfg/log.h>
46
47 #include <drv/timer.h>
48 #include <drv/flash.h>
49
50 #include <cpu/power.h>
51 #include <cpu/detect.h>
52
53 #include <io/stm32.h>
54
55 #include <string.h>
56
57 #define EMB_FLASH                ((struct stm32_flash*)FLASH_R_BASE)
58
59 #if CPU_CM3_STM32F103RB
60         #define EMB_FLASH_PAGE_SIZE   1024
61 #else
62         #error Unknown CPU
63 #endif
64
65
66 struct FlashHardware
67 {
68         uint8_t status;
69 };
70
71 INLINE bool flash_wait(struct KBlock *blk)
72 {
73         Flash *fls = FLASH_CAST(blk);
74         ticks_t start = timer_clock();
75         while (true)
76         {
77                 if (!(EMB_FLASH->SR & FLASH_FLAG_BSY))
78                         break;
79
80                 if (EMB_FLASH->SR & FLASH_FLAG_PGERR)
81                 {
82                         fls->hw->status |= FLASH_NOT_ERASED;
83                         LOG_ERR("flash not erased..\n");
84                         return false;
85                 }
86
87                 if (EMB_FLASH->SR & FLASH_FLAG_WRPRTERR)
88                 {
89                         fls->hw->status |= FLASH_WR_PROTECT;
90                         LOG_ERR("wr protect..\n");
91                         return false;
92                 }
93
94                 if (timer_clock() - start > ms_to_ticks(CONFIG_FLASH_WR_TIMEOUT))
95                 {
96                         fls->hw->status |= FLASH_WR_TIMEOUT;
97                         LOG_ERR("Timeout..\n");
98                         return false;
99                 }
100
101                 cpu_relax();
102         }
103
104         return true;
105 }
106
107 static bool stm32_erasePage(struct KBlock *blk, uint32_t page_add)
108 {
109
110         EMB_FLASH->CR |= CR_PER_SET;
111         EMB_FLASH->AR = page_add;
112         EMB_FLASH->CR |= CR_STRT_SET;
113
114         if (!flash_wait(blk))
115                 return false;
116
117         EMB_FLASH->CR &= CR_PER_RESET;
118
119         return true;
120 }
121
122 static bool stm32_eraseAll(struct KBlock *blk)
123 {
124         EMB_FLASH->CR |= CR_MER_SET;
125         EMB_FLASH->CR |= CR_STRT_SET;
126
127         if (!flash_wait(blk))
128                 return false;
129
130         EMB_FLASH->CR &= CR_MER_RESET;
131
132         return true;
133 }
134
135 static int stm32_flash_error(struct KBlock *blk)
136 {
137         Flash *fls = FLASH_CAST(blk);
138         return fls->hw->status;
139 }
140
141 static void stm32_flash_clearerror(struct KBlock *blk)
142 {
143         Flash *fls = FLASH_CAST(blk);
144         fls->hw->status = 0;
145 }
146
147 static size_t stm32_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
148 {
149         memcpy(buf, (void *)(idx * blk->blk_size + offset), size);
150
151         return size;
152 }
153
154
155 INLINE bool stm32_writeWord(struct KBlock *blk, uint32_t addr, uint16_t data)
156 {
157         ASSERT(!(addr % 2));
158
159         EMB_FLASH->CR |= CR_PG_SET;
160
161         *(reg16_t *)addr = data;
162
163         if (!flash_wait(blk))
164                 return false;
165
166         EMB_FLASH->CR &= CR_PG_RESET;
167
168         return true;
169 }
170
171 static size_t stm32_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
172 {
173         if (!stm32_erasePage(blk, (idx * blk->blk_size)))
174                 return 0;
175
176         uint32_t addr = idx * blk->blk_size + offset;
177         const uint8_t *buf = (const uint8_t *)_buf;
178         size_t count = 0;
179
180         if (addr % 2)
181         {
182                 if (!stm32_writeWord(blk, addr - 1, (buf[0] << 8) | 0xFF))
183                         return count;
184
185                 buf++;
186                 size--;
187                 count++;
188                 addr++;
189         }
190
191         ASSERT(!(addr % 2));
192         while (size)
193         {
194                 uint16_t data;
195                 size_t len;
196                 if (size == 1)
197                 {
198                         data = 0xFF00 | *buf;
199                         len = 1;
200                 }
201                 else
202                 {
203                         data = ((*buf + 1) << 8) | *buf;
204                         len = 2;
205                 }
206
207                 if (!stm32_writeWord(blk, addr, data))
208                         return count;
209
210                 buf += len;
211                 size -= len;
212                 count += len;
213                 addr += len;
214         }
215         return count;
216 }
217
218 static const KBlockVTable flash_stm32_buffered_vt =
219 {
220         .readDirect = stm32_flash_readDirect,
221         .writeDirect = stm32_flash_writeDirect,
222
223         .readBuf = kblock_swReadBuf,
224         .writeBuf = kblock_swWriteBuf,
225         .load = kblock_swLoad,
226         .store = kblock_swStore,
227
228         .error = stm32_flash_error,
229         .clearerr = stm32_flash_clearerror,
230 };
231
232
233
234 static const KBlockVTable flash_stm32_unbuffered_vt =
235 {
236         .readDirect = stm32_flash_readDirect,
237         .writeDirect = stm32_flash_writeDirect,
238
239         .error = stm32_flash_error,
240         .clearerr = stm32_flash_clearerror,
241 };
242
243 static struct FlashHardware flash_stm32_hw;
244 static uint8_t flash_buf[EMB_FLASH_PAGE_SIZE];
245
246 static void common_init(Flash *fls)
247 {
248         memset(fls, 0, sizeof(*fls));
249         DB(fls->blk.priv.type = KBT_FLASH);
250
251         EMB_FLASH->CR = 0;
252
253         fls->hw = &flash_stm32_hw;
254
255         fls->blk.blk_size = EMB_FLASH_PAGE_SIZE;
256         fls->blk.blk_cnt = (F_SIZE * 1024) / EMB_FLASH_PAGE_SIZE;
257
258         /* Unlock flash memory for the FPEC Access */
259         EMB_FLASH->KEYR = FLASH_KEY1;
260         EMB_FLASH->KEYR = FLASH_KEY2;
261 }
262
263
264 void flash_hw_init(Flash *fls)
265 {
266         common_init(fls);
267         fls->blk.priv.vt = &flash_stm32_buffered_vt;
268         fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
269         fls->blk.priv.buf = flash_buf;
270 }
271
272 void flash_hw_initUnbuffered(Flash *fls)
273 {
274         common_init(fls);
275         fls->blk.priv.vt = &flash_stm32_unbuffered_vt;
276 }