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