Refactor to use new protocol module and sipo.
[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 struct FlashHardware
60 {
61         uint8_t status;
62 };
63
64 static bool flash_wait(struct KBlock *blk)
65 {
66         Flash *fls = FLASH_CAST(blk);
67         ticks_t start = timer_clock();
68         while (true)
69         {
70                 cpu_relax();
71                 if (!(EMB_FLASH->SR & FLASH_FLAG_BSY))
72                         break;
73
74                 if (EMB_FLASH->SR & FLASH_FLAG_PGERR)
75                 {
76                         fls->hw->status |= FLASH_NOT_ERASED;
77                         LOG_ERR("flash not erased..\n");
78                         return false;
79                 }
80
81                 if (EMB_FLASH->SR & FLASH_FLAG_WRPRTERR)
82                 {
83                         fls->hw->status |= FLASH_WR_PROTECT;
84                         LOG_ERR("wr protect..\n");
85                         return false;
86                 }
87
88                 if (timer_clock() - start > ms_to_ticks(CONFIG_FLASH_WR_TIMEOUT))
89                 {
90                         fls->hw->status |= FLASH_WR_TIMEOUT;
91                         LOG_ERR("Timeout..\n");
92                         return false;
93                 }
94         }
95
96         return true;
97 }
98
99 static bool stm32_erasePage(struct KBlock *blk, uint32_t page_add)
100 {
101
102         EMB_FLASH->CR |= CR_PER_SET;
103         EMB_FLASH->AR = page_add;
104         EMB_FLASH->CR |= CR_STRT_SET;
105
106         if (!flash_wait(blk))
107                 return false;
108
109         EMB_FLASH->CR &= CR_PER_RESET;
110
111         return true;
112 }
113
114 #if 0
115 // not used for now
116 static bool stm32_eraseAll(struct KBlock *blk)
117 {
118         EMB_FLASH->CR |= CR_MER_SET;
119         EMB_FLASH->CR |= CR_STRT_SET;
120
121         if (!flash_wait(blk))
122                 return false;
123
124         EMB_FLASH->CR &= CR_MER_RESET;
125
126         return true;
127 }
128 #endif
129
130 static int stm32_flash_error(struct KBlock *blk)
131 {
132         Flash *fls = FLASH_CAST(blk);
133         return fls->hw->status;
134 }
135
136 static void stm32_flash_clearerror(struct KBlock *blk)
137 {
138         Flash *fls = FLASH_CAST(blk);
139         fls->hw->status = 0;
140 }
141
142 static size_t stm32_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
143 {
144         memcpy(buf, (void *)(idx * blk->blk_size + offset), size);
145         return size;
146 }
147
148
149 INLINE bool stm32_writeWord(struct KBlock *blk, uint32_t addr, uint16_t data)
150 {
151         ASSERT(!(addr % 2));
152
153         EMB_FLASH->CR |= CR_PG_SET;
154
155         *(reg16_t *)addr = data;
156
157         if (!flash_wait(blk))
158                 return false;
159
160         EMB_FLASH->CR &= CR_PG_RESET;
161
162         return true;
163 }
164
165 static size_t stm32_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
166 {
167         ASSERT(offset == 0);
168         ASSERT(size == blk->blk_size);
169
170         if (!stm32_erasePage(blk, (idx * blk->blk_size)))
171                 return 0;
172
173         uint32_t addr = idx * blk->blk_size;
174         const uint8_t *buf = (const uint8_t *)_buf;
175
176         while (size)
177         {
178                 uint16_t data = (*(buf + 1) << 8) | *buf;
179                 if (!stm32_writeWord(blk, addr, data))
180                         return 0;
181
182                 buf += 2;
183                 size -= 2;
184                 addr += 2;
185         }
186
187         return blk->blk_size;
188 }
189
190 static const KBlockVTable flash_stm32_buffered_vt =
191 {
192         .readDirect = stm32_flash_readDirect,
193         .writeDirect = stm32_flash_writeDirect,
194
195         .readBuf = kblock_swReadBuf,
196         .writeBuf = kblock_swWriteBuf,
197         .load = kblock_swLoad,
198         .store = kblock_swStore,
199
200         .close = kblock_swClose,
201
202         .error = stm32_flash_error,
203         .clearerr = stm32_flash_clearerror,
204 };
205
206 static const KBlockVTable flash_stm32_unbuffered_vt =
207 {
208         .readDirect = stm32_flash_readDirect,
209         .writeDirect = stm32_flash_writeDirect,
210
211         .close = kblock_swClose,
212
213         .error = stm32_flash_error,
214         .clearerr = stm32_flash_clearerror,
215 };
216
217 static struct FlashHardware flash_stm32_hw;
218 static uint8_t flash_buf[FLASH_PAGE_SIZE];
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_stm32_hw;
226
227         fls->blk.blk_size = FLASH_PAGE_SIZE;
228         fls->blk.blk_cnt = (F_SIZE * 1024) / FLASH_PAGE_SIZE;
229
230         /* Unlock flash memory for the FPEC Access */
231         EMB_FLASH->KEYR = FLASH_KEY1;
232         EMB_FLASH->KEYR = FLASH_KEY2;
233 }
234
235
236 void flash_hw_init(Flash *fls, UNUSED_ARG(int, flags))
237 {
238         common_init(fls);
239         fls->blk.priv.vt = &flash_stm32_buffered_vt;
240         fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
241         fls->blk.priv.buf = flash_buf;
242
243         /* Load the first block in the cache */
244         void *flash_start = 0x0;
245         memcpy(fls->blk.priv.buf, flash_start, fls->blk.blk_size);
246 }
247
248 void flash_hw_initUnbuffered(Flash *fls, UNUSED_ARG(int, flags))
249 {
250         common_init(fls);
251         fls->blk.priv.vt = &flash_stm32_unbuffered_vt;
252 }