1f42b92163dc1082613bd3d0861f8993f13e2fe1
[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 <struct/bitarray.h>
62
63 #include <string.h>
64
65 /* Embedded flash programming defines. */
66 #define IAP_ADDRESS 0x7ffffff1
67
68 typedef enum IapCommands
69 {
70         PREPARE_SECTOR_FOR_WRITE = 50,
71         COPY_RAM_TO_FLASH = 51,
72         ERASE_SECTOR = 52,
73         BLANK_CHECK_SECTOR = 53,
74         READ_PART_ID = 54,
75         READ_BOOT_VER = 55,
76         COMPARE = 56,
77         REINVOKE_ISP = 57,
78 } IapCommands;
79
80 #if CPU_ARM_LPC2378
81         #define FLASH_MEM_SIZE         (504 * 1024L)
82         #define FLASH_PAGE_SIZE_BYTES          4096
83         #define FLASH_REAL_PAGE_CNT              28
84 #else
85         #error Unknown CPU
86 #endif
87
88 #define CMD_SUCCESS 0
89
90 struct FlashHardware
91 {
92         uint8_t status;
93 };
94
95 #define FLASH_PAGE_CNT  FLASH_MEM_SIZE / FLASH_PAGE_SIZE_BYTES
96
97 ALLOC_BITARRAY(page_dirty, FLASH_PAGE_CNT);
98 static BitArray lpc2_bitx;
99
100 uint8_t erase_group[] = {
101
102         4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
103         4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
104
105         4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
106         4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
107
108         32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
109         32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
110
111         32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
112         32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
113
114         32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
115         32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
116
117         32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
118         4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
119
120         4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
121         4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
122 };
123
124 typedef struct IapCmd
125 {
126         uint32_t cmd;
127         uint32_t param[4];
128 } IapCmd;
129
130 typedef struct IapRes
131 {
132         uint32_t status;
133         uint32_t res[2];
134 } IapRes;
135
136 typedef void (*iap_callback_t)(IapCmd *, IapRes *);
137
138 iap_callback_t iap = (iap_callback_t)IAP_ADDRESS;
139
140 static size_t sector_size(uint32_t page)
141 {
142         if (page < 8)
143                 return 4096;
144         else if (page < 22)
145                 return 32768;
146         else if (page < 28)
147                 return 4096;
148
149         ASSERT(0);
150         return 0;
151 }
152
153 static size_t sector_addr(uint32_t page)
154 {
155         if (page < 8)
156                 return page * 4096;
157         else if (page < 22)
158                 return (page - 8) * 32768 + 4096 * 8;
159         else if (page < 28)
160                 return (page - 22) * 4096 + 32768 * 14 + 4096 * 8;
161
162         ASSERT(0);
163         return 0;
164 }
165
166
167 static uint32_t addr_to_sector(size_t addr)
168 {
169         if (addr < 4096 * 8)
170                 return addr / 4096;
171         else if (addr < 4096 * 8 + 32768L * 14)
172                 return ((addr - 4096 * 8) / 32768) + 8;
173         else if (addr < 4096 * 8 + 32768L * 14 + 4096 * 6)
174                 return ((addr - 4096 * 8 - 32768L * 14) / 4096) + 22;
175
176         ASSERT(0);
177         return 0;
178 }
179
180 static size_t lpc2_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
181 {
182         ASSERT(offset == 0);
183         ASSERT(size == blk->blk_size);
184
185         memcpy(buf, (void *)(idx * blk->blk_size), size);
186         return size;
187 }
188
189 static size_t lpc2_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
190 {
191         ASSERT(offset == 0);
192         ASSERT(FLASH_PAGE_SIZE_BYTES == size);
193
194         Flash *fls = FLASH_CAST(blk);
195         if (!(fls->blk.priv.flags & KB_WRITE_ONCE))
196                 ASSERT(sector_size(idx) <= FLASH_PAGE_SIZE_BYTES);
197
198         const uint8_t *buf = (const uint8_t *)_buf;
199         cpu_flags_t flags;
200
201         //Compute page address of current page.
202         uint32_t addr = idx * blk->blk_size;
203         uint32_t sector = addr_to_sector(addr);
204         // Compute the first page index in the sector to manage the status
205         int idx_sector = sector_addr(sector) /  blk->blk_size;
206
207         LOG_INFO("Writing page[%ld]sector[%ld]idx[%d]\n", idx, sector, idx_sector);
208         IRQ_SAVE_DISABLE(flags);
209
210         IapCmd cmd;
211         IapRes res;
212         cmd.cmd = PREPARE_SECTOR_FOR_WRITE;
213         cmd.param[0] = cmd.param[1] = sector;
214         iap(&cmd, &res);
215
216         if (res.status != CMD_SUCCESS)
217                 goto flash_error;
218
219         if ((fls->blk.priv.flags & KB_WRITE_ONCE) &&
220                         bitarray_blockFull(&lpc2_bitx, idx_sector, erase_group[sector]))
221         {
222                 kputs("blocchi pieni\n");
223                 ASSERT(0);
224                 goto flash_error;
225         }
226
227         bool erase = false;
228         if ((fls->blk.priv.flags & KB_WRITE_ONCE) &&
229                         bitarray_blockEmpty(&lpc2_bitx, idx_sector, erase_group[sector]))
230                 erase = true;
231
232         if (!(fls->blk.priv.flags & KB_WRITE_ONCE))
233                 erase = true;
234
235         if (erase)
236         {
237                 cmd.cmd = ERASE_SECTOR;
238                 cmd.param[0] = cmd.param[1] = sector;
239                 cmd.param[2] = CPU_FREQ / 1000;
240                 iap(&cmd, &res);
241
242                 if (res.status != CMD_SUCCESS)
243                         goto flash_error;
244         }
245
246         LOG_INFO("Writing page [%ld], addr [%ld] in sector[%ld]\n", idx, addr, sector);
247         cmd.cmd = PREPARE_SECTOR_FOR_WRITE;
248         cmd.param[0] = cmd.param[1] = sector;
249         iap(&cmd, &res);
250
251         if (res.status != CMD_SUCCESS)
252                 goto flash_error;
253
254         if (fls->blk.priv.flags & KB_WRITE_ONCE)
255         {
256                 if (bitarray_check(&lpc2_bitx, idx))
257                 {
258                         ASSERT(0);
259                         goto flash_error;
260                 }
261                 else
262                         bitarray_set(&lpc2_bitx, idx);
263         }
264
265         cmd.cmd = COPY_RAM_TO_FLASH;
266         cmd.param[0] = addr;
267         cmd.param[1] = (uint32_t)buf;
268         cmd.param[2] = FLASH_PAGE_SIZE_BYTES;
269         cmd.param[3] = CPU_FREQ / 1000;
270         iap(&cmd, &res);
271
272         if (res.status != CMD_SUCCESS)
273                 goto flash_error;
274
275         IRQ_RESTORE(flags);
276         LOG_INFO("Done\n");
277
278         return blk->blk_size;
279
280 flash_error:
281         LOG_ERR("%ld\n", res.status);
282         fls->hw->status |= FLASH_WR_ERR;
283         return 0;
284 }
285
286 static int lpc2_flash_close(UNUSED_ARG(struct KBlock, *blk))
287 {
288         memset(page_dirty, 0, sizeof(page_dirty));
289         return 0;
290 }
291
292
293 static int lpc2_flash_error(struct KBlock *blk)
294 {
295         Flash *fls = FLASH_CAST(blk);
296         return fls->hw->status;
297 }
298
299 static void lpc2_flash_clearerror(struct KBlock *blk)
300 {
301         Flash *fls = FLASH_CAST(blk);
302         fls->hw->status = 0;
303 }
304
305 static const KBlockVTable flash_lpc2_buffered_vt =
306 {
307         .readDirect = lpc2_flash_readDirect,
308         .writeDirect = lpc2_flash_writeDirect,
309
310         .readBuf = kblock_swReadBuf,
311         .writeBuf = kblock_swWriteBuf,
312         .load = kblock_swLoad,
313         .store = kblock_swStore,
314
315         .close = lpc2_flash_close,
316
317         .error = lpc2_flash_error,
318         .clearerr = lpc2_flash_clearerror,
319 };
320
321 static const KBlockVTable flash_lpc2_unbuffered_vt =
322 {
323         .readDirect = lpc2_flash_readDirect,
324         .writeDirect = lpc2_flash_writeDirect,
325
326         .close = lpc2_flash_close,
327
328         .error = lpc2_flash_error,
329         .clearerr = lpc2_flash_clearerror,
330 };
331
332 static struct FlashHardware flash_lpc2_hw;
333 static uint8_t flash_buf[FLASH_PAGE_SIZE_BYTES];
334
335 static void common_init(Flash *fls)
336 {
337         memset(fls, 0, sizeof(*fls));
338         DB(fls->blk.priv.type = KBT_FLASH);
339
340         fls->hw = &flash_lpc2_hw;
341
342         fls->blk.blk_size = FLASH_PAGE_SIZE_BYTES;
343         fls->blk.blk_cnt = FLASH_MEM_SIZE / FLASH_PAGE_SIZE_BYTES;
344
345         init_bitarray(&lpc2_bitx, FLASH_PAGE_CNT, page_dirty, sizeof(page_dirty));
346 }
347
348 void flash_hw_init(Flash *fls, int flags)
349 {
350         common_init(fls);
351         fls->blk.priv.vt = &flash_lpc2_buffered_vt;
352         fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE | flags;
353         fls->blk.priv.buf = flash_buf;
354
355         /* Load the first block in the cache */
356         void *flash_start = 0x0;
357         memcpy(fls->blk.priv.buf, flash_start, fls->blk.blk_size);
358 }
359
360 void flash_hw_initUnbuffered(Flash *fls, int flags)
361 {
362         common_init(fls);
363         fls->blk.priv.vt = &flash_lpc2_unbuffered_vt;
364         fls->blk.priv.flags |= flags;
365 }