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>
63 /* Embedded flash programming defines. */
64 #define IAP_ADDRESS 0x7ffffff1
66 typedef enum IapCommands
68 PREPARE_SECTOR_FOR_WRITE = 50,
69 COPY_RAM_TO_FLASH = 51,
71 BLANK_CHECK_SECTOR = 53,
79 #define FLASH_MEM_SIZE (504 * 1024L)
80 #define FLASH_PAGE_SIZE_BYTES 4096
81 #define FLASH_PAGE_4K_CNT 14
105 typedef void (*iap_callback_t)(IapCmd *, IapRes *);
107 iap_callback_t iap = (iap_callback_t)IAP_ADDRESS;
109 static size_t sector_size(uint32_t page)
122 static size_t sector_addr(uint32_t page)
127 return (page - 8) * 32768 + 4096 * 8;
129 return (page - 22) * 4096 + 32768 * 14 + 4096 * 8;
136 static uint32_t addr_to_sector(size_t addr)
140 else if (addr < 4096 * 8 + 32768L * 14)
141 return ((addr - 4096 * 8) / 32768) + 8;
142 else if (addr < 4096 * 8 + 32768L * 14 + 4096 * 6)
143 return ((addr - 4096 * 8 - 32768L * 14) / 4096) + 22;
149 static uint32_t addr_to_pageaddr(size_t addr)
153 else if (addr < 4096 * 8 + 32768L * 14)
154 return (addr - 4096 * 8) % 32768;
155 else if (addr < 4096 * 8 + 32768L * 14 + 4096 * 6)
156 return (addr - 4096 * 8 - 32768L * 14) % 4096;
162 static size_t lpc2_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
165 ASSERT(size == blk->blk_size);
167 ASSERT(sector_size(idx) <= FLASH_PAGE_SIZE_BYTES);
169 memcpy(buf, (void *)(idx * blk->blk_size), size);
173 static size_t lpc2_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
176 ASSERT(size == blk->blk_size);
177 ASSERT(sector_size(idx) <= FLASH_PAGE_SIZE_BYTES);
179 Flash *fls = FLASH_CAST(blk);
180 const uint8_t *buf = (const uint8_t *)_buf;
183 //Compute page address of current page.
184 uint32_t addr = sector_addr(idx);
186 LOG_INFO("Writing page %ld...\n", idx);
188 IRQ_SAVE_DISABLE(flags);
192 cmd.cmd = PREPARE_SECTOR_FOR_WRITE;
193 cmd.param[0] = cmd.param[1] = idx;
195 if (res.status != CMD_SUCCESS)
197 LOG_ERR("%ld\n", res.status);
198 fls->hw->status |= FLASH_WR_ERR;
202 cmd.cmd = ERASE_SECTOR;
203 cmd.param[0] = cmd.param[1] = idx;
204 cmd.param[2] = CPU_FREQ / 1000;
206 if (res.status != CMD_SUCCESS)
208 LOG_ERR("%ld\n", res.status);
209 fls->hw->status |= FLASH_WR_ERR;
215 LOG_INFO("Writing page %ld, addr %ld, size %d\n", idx, addr, size);
216 cmd.cmd = PREPARE_SECTOR_FOR_WRITE;
217 cmd.param[0] = cmd.param[1] = idx;
219 if (res.status != CMD_SUCCESS)
221 LOG_ERR("%ld\n", res.status);
222 fls->hw->status |= FLASH_WR_ERR;
226 cmd.cmd = COPY_RAM_TO_FLASH;
228 cmd.param[1] = (uint32_t)buf;
230 cmd.param[3] = CPU_FREQ / 1000;
232 if (res.status != CMD_SUCCESS)
234 LOG_ERR("%ld\n", res.status);
235 fls->hw->status |= FLASH_WR_ERR;
241 buf += 4096 / sizeof(uint32_t);
247 return blk->blk_size;
250 static int lpc2_flash_error(struct KBlock *blk)
252 Flash *fls = FLASH_CAST(blk);
253 return fls->hw->status;
256 static void lpc2_flash_clearerror(struct KBlock *blk)
258 Flash *fls = FLASH_CAST(blk);
262 static const KBlockVTable flash_lpc2_buffered_vt =
264 .readDirect = lpc2_flash_readDirect,
265 .writeDirect = lpc2_flash_writeDirect,
267 .readBuf = kblock_swReadBuf,
268 .writeBuf = kblock_swWriteBuf,
269 .load = kblock_swLoad,
270 .store = kblock_swStore,
272 .close = kblock_swClose,
274 .error = lpc2_flash_error,
275 .clearerr = lpc2_flash_clearerror,
278 static const KBlockVTable flash_lpc2_unbuffered_vt =
280 .readDirect = lpc2_flash_readDirect,
281 .writeDirect = lpc2_flash_writeDirect,
283 .close = kblock_swClose,
285 .error = lpc2_flash_error,
286 .clearerr = lpc2_flash_clearerror,
289 static struct FlashHardware flash_lpc2_hw;
290 static uint8_t flash_buf[FLASH_PAGE_SIZE_BYTES];
292 static void common_init(Flash *fls)
294 memset(fls, 0, sizeof(*fls));
295 DB(fls->blk.priv.type = KBT_FLASH);
297 fls->hw = &flash_lpc2_hw;
299 fls->blk.blk_size = FLASH_PAGE_SIZE_BYTES;
300 fls->blk.blk_cnt = 28;
303 void flash_hw_init(Flash *fls)
306 fls->blk.priv.vt = &flash_lpc2_buffered_vt;
307 fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
308 fls->blk.priv.buf = flash_buf;
310 /* Load the first block in the cache */
311 void *flash_start = 0x0;
312 memcpy(fls->blk.priv.buf, flash_start, fls->blk.blk_size);
314 kprintf("page[%d]\n", sector_addr(22));
317 void flash_hw_initUnbuffered(Flash *fls)
320 fls->blk.priv.vt = &flash_lpc2_unbuffered_vt;