Exclude from nightlytest.
[bertos.git] / bertos / cpu / arm / drv / flash_lpc2.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  * \author Francesco Sacchi <batt@develer.com>
34  * \author Daniele Basile <asterix@develer.com>
35  *
36  * \brief NPX lpc23xx embedded flash read/write driver.
37  *
38  * notest: arm
39  */
40
41 #include "flash_lpc2.h"
42 #include "cfg/cfg_emb_flash.h"
43
44 // Define log settings for cfg/log.h
45 #define LOG_LEVEL    CONFIG_FLASH_EMB_LOG_LEVEL
46 #define LOG_FORMAT   CONFIG_FLASH_EMB_LOG_FORMAT
47 #include <cfg/log.h>
48 #include <cfg/macros.h>
49
50 #include <cpu/irq.h>
51 #include <cpu/attr.h>
52 #include <cpu/power.h>
53 #include <cpu/types.h>
54
55 #include <io/kblock.h>
56 #include <io/arm.h>
57
58 #include <drv/timer.h>
59 #include <drv/flash.h>
60
61 #include <string.h>
62
63 #define CMD_SUCCESS 0
64
65 struct FlashHardware
66 {
67         uint8_t status;
68 };
69
70 typedef struct IapCmd
71 {
72         uint32_t cmd;
73         uint32_t param[4];
74 } IapCmd;
75
76 typedef struct IapRes
77 {
78         uint32_t status;
79         uint32_t res[2];
80 } IapRes;
81
82 typedef void (*iap_callback_t)(IapCmd *, IapRes *);
83
84 iap_callback_t iap = (iap_callback_t)IAP_ADDRESS;
85
86 static size_t sector_size(uint32_t page)
87 {
88         if (page < 8)
89                 return 4096;
90         else if (page < 22)
91                 return 32768;
92         else if (page < 28)
93                 return 4096;
94
95         ASSERT(0);
96         return 0;
97 }
98
99 static size_t sector_addr(uint32_t page)
100 {
101         if (page < 8)
102                 return page * 4096;
103         else if (page < 22)
104                 return (page - 8) * 32768 + 4096 * 8;
105         else if (page < 28)
106                 return (page - 22) * 4096 + 32768 * 14 + 4096 * 8;
107
108         ASSERT(0);
109         return 0;
110 }
111
112
113 static uint32_t addr_to_sector(size_t addr)
114 {
115         if (addr < 4096 * 8)
116                 return addr / 4096;
117         else if (addr < 4096 * 8 + 32768L * 14)
118                 return ((addr - 4096 * 8) / 32768) + 8;
119         else if (addr < 4096 * 8 + 32768L * 14 + 4096 * 6)
120                 return ((addr - 4096 * 8 - 32768L * 14) / 4096) + 22;
121
122         ASSERT(0);
123         return 0;
124 }
125
126 static uint32_t addr_to_pageaddr(size_t addr)
127 {
128         if (addr < 4096 * 8)
129                 return addr % 4096;
130         else if (addr < 4096 * 8 + 32768L * 14)
131                 return (addr - 4096 * 8) % 32768;
132         else if (addr < 4096 * 8 + 32768L * 14 + 4096 * 6)
133                 return (addr - 4096 * 8 - 32768L * 14) % 4096;
134
135         ASSERT(0);
136         return 0;
137 }
138
139 static size_t lpc2_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
140 {
141         ASSERT(offset == 0);
142         ASSERT(size == blk->blk_size);
143
144         ASSERT(sector_size(idx) <= FLASH_PAGE_SIZE_BYTES);
145
146         memcpy(buf, (void *)(idx * blk->blk_size), size);
147         return size;
148 }
149
150 static size_t lpc2_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
151 {
152         ASSERT(offset == 0);
153         ASSERT(size == blk->blk_size);
154         ASSERT(sector_size(idx) <= FLASH_PAGE_SIZE_BYTES);
155
156         Flash *fls = FLASH_CAST(blk);
157         const uint8_t *buf = (const uint8_t *)_buf;
158         cpu_flags_t flags;
159
160         //Compute page address of current page.
161         uint32_t addr = sector_addr(idx);
162
163         LOG_INFO("Writing page %ld...\n", idx);
164
165         IRQ_SAVE_DISABLE(flags);
166
167         IapCmd cmd;
168         IapRes res;
169         cmd.cmd = PREPARE_SECTOR_FOR_WRITE;
170         cmd.param[0] = cmd.param[1] = idx;
171         iap(&cmd, &res);
172         if (res.status != CMD_SUCCESS)
173         {
174                 LOG_ERR("%ld\n", res.status);
175                 fls->hw->status |= FLASH_WR_ERR;
176                 return 0;
177         }
178
179         cmd.cmd = ERASE_SECTOR;
180         cmd.param[0] = cmd.param[1] = idx;
181         cmd.param[2] = CPU_FREQ / 1000;
182         iap(&cmd, &res);
183         if (res.status != CMD_SUCCESS)
184         {
185                 LOG_ERR("%ld\n", res.status);
186                 fls->hw->status |= FLASH_WR_ERR;
187                 return 0;
188         }
189
190         while (size)
191         {
192                 LOG_INFO("Writing page %ld, addr %ld, size %d\n", idx, addr, size);
193                 cmd.cmd = PREPARE_SECTOR_FOR_WRITE;
194                 cmd.param[0] = cmd.param[1] = idx;
195                 iap(&cmd, &res);
196                 if (res.status != CMD_SUCCESS)
197                 {
198                         LOG_ERR("%ld\n", res.status);
199                         fls->hw->status |= FLASH_WR_ERR;
200                         return 0;
201                 }
202
203                 cmd.cmd = COPY_RAM_TO_FLASH;
204                 cmd.param[0] = addr;
205                 cmd.param[1] = (uint32_t)buf;
206                 cmd.param[2] = 4096;
207                 cmd.param[3] = CPU_FREQ / 1000;
208                 iap(&cmd, &res);
209                 if (res.status != CMD_SUCCESS)
210                 {
211                         LOG_ERR("%ld\n", res.status);
212                         fls->hw->status |= FLASH_WR_ERR;
213                         return 0;
214                 }
215
216                 size -= 4096;
217                 addr += 4096;
218                 buf += 4096 / sizeof(uint32_t);
219         }
220
221         IRQ_RESTORE(flags);
222         LOG_INFO("Done\n");
223
224         return blk->blk_size;
225 }
226
227 static int lpc2_flash_error(struct KBlock *blk)
228 {
229         Flash *fls = FLASH_CAST(blk);
230         return fls->hw->status;
231 }
232
233 static void lpc2_flash_clearerror(struct KBlock *blk)
234 {
235         Flash *fls = FLASH_CAST(blk);
236         fls->hw->status = 0;
237 }
238
239 static const KBlockVTable flash_lpc2_buffered_vt =
240 {
241         .readDirect = lpc2_flash_readDirect,
242         .writeDirect = lpc2_flash_writeDirect,
243
244         .readBuf = kblock_swReadBuf,
245         .writeBuf = kblock_swWriteBuf,
246         .load = kblock_swLoad,
247         .store = kblock_swStore,
248
249         .close = kblock_swClose,
250
251         .error = lpc2_flash_error,
252         .clearerr = lpc2_flash_clearerror,
253 };
254
255 static const KBlockVTable flash_lpc2_unbuffered_vt =
256 {
257         .readDirect = lpc2_flash_readDirect,
258         .writeDirect = lpc2_flash_writeDirect,
259
260         .close = kblock_swClose,
261
262         .error = lpc2_flash_error,
263         .clearerr = lpc2_flash_clearerror,
264 };
265
266 static struct FlashHardware flash_lpc2_hw;
267 static uint8_t flash_buf[FLASH_PAGE_SIZE_BYTES];
268
269 static void common_init(Flash *fls)
270 {
271         memset(fls, 0, sizeof(*fls));
272         DB(fls->blk.priv.type = KBT_FLASH);
273
274         fls->hw = &flash_lpc2_hw;
275
276         fls->blk.blk_size = FLASH_PAGE_SIZE_BYTES;
277         fls->blk.blk_cnt = 28;
278 }
279
280 void flash_hw_init(Flash *fls)
281 {
282         common_init(fls);
283         fls->blk.priv.vt = &flash_lpc2_buffered_vt;
284         fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
285         fls->blk.priv.buf = flash_buf;
286
287         /* Load the first block in the cache */
288         void *flash_start = 0x0;
289         memcpy(fls->blk.priv.buf, flash_start, fls->blk.blk_size);
290
291         kprintf("page[%d]\n", sector_addr(22));
292 }
293
294 void flash_hw_initUnbuffered(Flash *fls)
295 {
296         common_init(fls);
297         fls->blk.priv.vt = &flash_lpc2_unbuffered_vt;
298 }