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 2009 Develer S.r.l. (http://www.develer.com/)
33 * \author Daniele Basile <asterix@develer.com>
35 * \brief At91sam7 Internal flash read/write driver.
40 #include "flash_at91.h"
42 #include "cfg/cfg_emb_flash.h"
43 #include <cfg/macros.h>
45 // Define log settings for cfg/log.h
46 #define LOG_LEVEL CONFIG_FLASH_EMB_LOG_LEVEL
47 #define LOG_FORMAT CONFIG_FLASH_EMB_LOG_FORMAT
52 #include <cpu/power.h>
55 #include <io/kblock.h>
58 #include <drv/timer.h>
59 #include <drv/flash.h>
70 * Really send the flash write command.
72 * \note This function has to be placed in RAM because
73 * executing code from flash while a writing process
74 * is in progress is forbidden.
76 RAM_FUNC NOINLINE static void write_page(uint32_t page)
78 // Send the 'write page' command
79 MC_FCR = MC_KEY | MC_FCMD_WP | (MC_PAGEN_MASK & (page << 8));
81 // Wait for the end of command
82 while(!(MC_FSR & BV(MC_FRDY)))
92 * After WR command cpu write bufferd page into flash memory.
95 INLINE void flash_sendWRcmd(uint32_t page)
99 LOG_INFO("Writing page %ld...\n", page);
101 IRQ_SAVE_DISABLE(flags);
109 * Return true if no error are occurred after flash memory
110 * read or write operation, otherwise return error code.
112 static bool flash_getStatus(struct KBlock *blk)
114 Flash *fls = FLASH_CAST(blk);
116 * This bit is set to one if an invalid command and/or a bad keywords was/were
117 * written in the Flash Command Register.
119 if(MC_FSR & BV(MC_PROGE))
121 fls->hw->status |= FLASH_WR_ERR;
122 LOG_ERR("flash not erased..\n");
127 * This bit is set to one if we programming of at least one locked lock
130 if(MC_FSR & BV(MC_LOCKE))
132 fls->hw->status |= FLASH_WR_PROTECT;
133 LOG_ERR("wr protect..\n");
140 static size_t at91_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
142 memcpy(buf, (void *)(idx * blk->blk_size + FLASH_BASE + offset), size);
146 static size_t at91_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
149 ASSERT(size == blk->blk_size);
151 uint32_t *addr = (uint32_t *)(idx * blk->blk_size + FLASH_BASE);
152 const uint8_t *buf = (const uint8_t *)_buf;
156 uint32_t data = (*(buf + 3) << 24) |
167 flash_sendWRcmd(idx);
169 if (!flash_getStatus(blk))
172 return blk->blk_size;
176 static int at91_flash_error(struct KBlock *blk)
178 Flash *fls = FLASH_CAST(blk);
179 return fls->hw->status;
182 static void at91_flash_clearerror(struct KBlock *blk)
184 Flash *fls = FLASH_CAST(blk);
188 static const KBlockVTable flash_at91_buffered_vt =
190 .readDirect = at91_flash_readDirect,
191 .writeDirect = at91_flash_writeDirect,
193 .readBuf = kblock_swReadBuf,
194 .writeBuf = kblock_swWriteBuf,
195 .load = kblock_swLoad,
196 .store = kblock_swStore,
198 .error = at91_flash_error,
199 .clearerr = at91_flash_clearerror,
202 static const KBlockVTable flash_at91_unbuffered_vt =
204 .readDirect = at91_flash_readDirect,
205 .writeDirect = at91_flash_writeDirect,
207 .error = at91_flash_error,
208 .clearerr = at91_flash_clearerror,
211 static struct FlashHardware flash_at91_hw;
212 static uint8_t flash_buf[FLASH_PAGE_SIZE_BYTES];
214 static void common_init(Flash *fls)
216 memset(fls, 0, sizeof(*fls));
217 DB(fls->blk.priv.type = KBT_FLASH);
219 fls->hw = &flash_at91_hw;
221 fls->blk.blk_size = FLASH_PAGE_SIZE_BYTES;
222 fls->blk.blk_cnt = FLASH_MEM_SIZE / FLASH_PAGE_SIZE_BYTES;
225 void flash_hw_init(Flash *fls, UNUSED_ARG(int, flags))
228 fls->blk.priv.vt = &flash_at91_buffered_vt;
229 fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
230 fls->blk.priv.buf = flash_buf;
232 /* Load the first block in the cache */
233 memcpy(fls->blk.priv.buf, (void *)(FLASH_BASE), fls->blk.blk_size);
236 void flash_hw_initUnbuffered(Flash *fls, UNUSED_ARG(int, flags))
239 fls->blk.priv.vt = &flash_at91_unbuffered_vt;