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