4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \author Francesco Sacchi <batt@develer.com>
34 * \author Daniele Basile <asterix@develer.com>
36 * \brief NPX lpc23xx embedded flash read/write driver.
41 #include "flash_lpc2.h"
42 #include "cfg/cfg_emb_flash.h"
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
48 #include <cfg/macros.h>
52 #include <cpu/power.h>
53 #include <cpu/types.h>
55 #include <io/kblock.h>
58 #include <drv/timer.h>
59 #include <drv/flash.h>
61 #include <struct/bitarray.h>
65 /* Embedded flash programming defines. */
66 #define IAP_ADDRESS 0x7ffffff1
68 typedef enum IapCommands
70 PREPARE_SECTOR_FOR_WRITE = 50,
71 COPY_RAM_TO_FLASH = 51,
73 BLANK_CHECK_SECTOR = 53,
81 #define FLASH_MEM_SIZE (504 * 1024L)
82 #define FLASH_PAGE_SIZE_BYTES 4096
83 #define FLASH_REAL_PAGE_CNT 28
95 #define FLASH_PAGE_CNT FLASH_MEM_SIZE / FLASH_PAGE_SIZE_BYTES
97 BITARRAY_ALLOC(page_dirty, FLASH_PAGE_CNT);
98 static BitArray lpc2_bitx;
100 uint8_t erase_group[] = {
102 4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
103 4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
105 4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
106 4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
108 32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
109 32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
111 32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
112 32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
114 32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
115 32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
117 32768 / FLASH_PAGE_SIZE_BYTES, 32768 / FLASH_PAGE_SIZE_BYTES,
118 4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
120 4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
121 4096 / FLASH_PAGE_SIZE_BYTES, 4096 / FLASH_PAGE_SIZE_BYTES,
124 typedef struct IapCmd
130 typedef struct IapRes
136 typedef void (*iap_callback_t)(IapCmd *, IapRes *);
138 iap_callback_t iap = (iap_callback_t)IAP_ADDRESS;
140 static size_t sector_size(uint32_t page)
153 static size_t sector_addr(uint32_t page)
158 return (page - 8) * 32768 + 4096 * 8;
160 return (page - 22) * 4096 + 32768 * 14 + 4096 * 8;
167 static uint32_t addr_to_sector(size_t addr)
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;
180 static size_t lpc2_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
182 memcpy(buf, (void *)(idx * blk->blk_size + offset), size);
186 static size_t lpc2_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
189 ASSERT(FLASH_PAGE_SIZE_BYTES == size);
191 Flash *fls = FLASH_CAST(blk);
192 if (!(fls->blk.priv.flags & KB_WRITE_ONCE))
193 ASSERT(sector_size(idx) <= FLASH_PAGE_SIZE_BYTES);
195 const uint8_t *buf = (const uint8_t *)_buf;
198 //Compute page address of current page.
199 uint32_t addr = idx * blk->blk_size;
200 uint32_t sector = addr_to_sector(addr);
201 // Compute the first page index in the sector to manage the status
202 int idx_sector = sector_addr(sector) / blk->blk_size;
204 LOG_INFO("Writing page[%ld]sector[%ld]idx[%d]\n", idx, sector, idx_sector);
205 IRQ_SAVE_DISABLE(flags);
209 cmd.cmd = PREPARE_SECTOR_FOR_WRITE;
210 cmd.param[0] = cmd.param[1] = sector;
213 if (res.status != CMD_SUCCESS)
216 if ((fls->blk.priv.flags & KB_WRITE_ONCE) &&
217 bitarray_isRangeFull(&lpc2_bitx, idx_sector, erase_group[sector]))
219 kputs("blocchi pieni\n");
225 if ((fls->blk.priv.flags & KB_WRITE_ONCE) &&
226 bitarray_isRangeEmpty(&lpc2_bitx, idx_sector, erase_group[sector]))
229 if (!(fls->blk.priv.flags & KB_WRITE_ONCE))
234 cmd.cmd = ERASE_SECTOR;
235 cmd.param[0] = cmd.param[1] = sector;
236 cmd.param[2] = CPU_FREQ / 1000;
239 if (res.status != CMD_SUCCESS)
243 LOG_INFO("Writing page [%ld], addr [%ld] in sector[%ld]\n", idx, addr, sector);
244 cmd.cmd = PREPARE_SECTOR_FOR_WRITE;
245 cmd.param[0] = cmd.param[1] = sector;
248 if (res.status != CMD_SUCCESS)
251 if (fls->blk.priv.flags & KB_WRITE_ONCE)
253 if (bitarray_test(&lpc2_bitx, idx))
259 bitarray_set(&lpc2_bitx, idx);
262 cmd.cmd = COPY_RAM_TO_FLASH;
264 cmd.param[1] = (uint32_t)buf;
265 cmd.param[2] = FLASH_PAGE_SIZE_BYTES;
266 cmd.param[3] = CPU_FREQ / 1000;
269 if (res.status != CMD_SUCCESS)
275 return blk->blk_size;
278 LOG_ERR("%ld\n", res.status);
279 fls->hw->status |= FLASH_WR_ERR;
283 static int lpc2_flash_close(UNUSED_ARG(struct KBlock, *blk))
285 memset(page_dirty, 0, sizeof(page_dirty));
290 static int lpc2_flash_error(struct KBlock *blk)
292 Flash *fls = FLASH_CAST(blk);
293 return fls->hw->status;
296 static void lpc2_flash_clearerror(struct KBlock *blk)
298 Flash *fls = FLASH_CAST(blk);
302 static const KBlockVTable flash_lpc2_buffered_vt =
304 .readDirect = lpc2_flash_readDirect,
305 .writeDirect = lpc2_flash_writeDirect,
307 .readBuf = kblock_swReadBuf,
308 .writeBuf = kblock_swWriteBuf,
309 .load = kblock_swLoad,
310 .store = kblock_swStore,
312 .close = lpc2_flash_close,
314 .error = lpc2_flash_error,
315 .clearerr = lpc2_flash_clearerror,
318 static const KBlockVTable flash_lpc2_unbuffered_vt =
320 .readDirect = lpc2_flash_readDirect,
321 .writeDirect = lpc2_flash_writeDirect,
323 .close = lpc2_flash_close,
325 .error = lpc2_flash_error,
326 .clearerr = lpc2_flash_clearerror,
329 static struct FlashHardware flash_lpc2_hw;
330 static uint8_t flash_buf[FLASH_PAGE_SIZE_BYTES];
332 static void common_init(Flash *fls)
334 memset(fls, 0, sizeof(*fls));
335 DB(fls->blk.priv.type = KBT_FLASH);
337 fls->hw = &flash_lpc2_hw;
339 fls->blk.blk_size = FLASH_PAGE_SIZE_BYTES;
340 fls->blk.blk_cnt = FLASH_MEM_SIZE / FLASH_PAGE_SIZE_BYTES;
342 bitarray_init(&lpc2_bitx, FLASH_PAGE_CNT, page_dirty, sizeof(page_dirty));
345 void flash_hw_init(Flash *fls, int flags)
348 fls->blk.priv.vt = &flash_lpc2_buffered_vt;
349 fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE | flags;
350 fls->blk.priv.buf = flash_buf;
352 /* Load the first block in the cache */
353 void *flash_start = 0x0;
354 memcpy(fls->blk.priv.buf, flash_start, fls->blk.blk_size);
357 void flash_hw_initUnbuffered(Flash *fls, int flags)
360 fls->blk.priv.vt = &flash_lpc2_unbuffered_vt;
361 fls->blk.priv.flags |= flags;