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 2011 Develer S.r.l. (http://www.develer.com/)
33 * \author Daniele Basile <asterix@develer.com>
35 * \brief SAM3 Internal flash read/write driver.
40 #include "flash_sam3.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>
71 * Really send the flash write command.
73 * \note This function has to be placed in RAM because
74 * executing code from flash while a writing process
75 * is in progress is forbidden.
77 RAM_FUNC NOINLINE static void write_page_bank0(uint32_t page)
79 // Send the 'write page' command
80 EEFC0_FCR = EEFC_FCR_FKEY | EFC_FCR_FCMD_EWP | EEFC_FCR_FARG(page);
82 // Wait for the end of command
83 while(!(EEFC0_FSR & BV(EEFC_FSR_FRDY)))
89 #if FLASH_BANKS_NUM > 1
90 RAM_FUNC NOINLINE static void write_page_bank1(uint32_t page)
92 // Send the 'write page' command
93 EEFC1_FCR = EEFC_FCR_FKEY | EFC_FCR_FCMD_EWP | EEFC_FCR_FARG(page);
95 // Wait for the end of command
96 while(!(EEFC1_FSR & BV(EEFC_FSR_FRDY)))
105 * Send write command.
107 * After WR command cpu write bufferd page into flash memory.
110 INLINE void flash_sendWRcmd(uint32_t page)
114 LOG_INFO("Writing page %ld...\n", page);
116 if (page > FLASH_PAGES_FOR_BANK)
118 page = page - FLASH_PAGES_FOR_BANK;
119 IRQ_SAVE_DISABLE(flags);
120 write_page_bank1(page);
125 IRQ_SAVE_DISABLE(flags);
126 write_page_bank0(page);
134 * Return true if no error are occurred after flash memory
135 * read or write operation, otherwise return error code.
137 static bool flash_getStatus(struct KBlock *blk)
139 Flash *fls = FLASH_CAST(blk);
141 * This bit is set to one if an invalid command and/or a bad keywords was/were
142 * written in the Flash Command Register.
144 if(EEFC0_FSR & BV(EEFC_FSR_FCMDE))
146 fls->hw->status |= FLASH_WR_ERR;
147 LOG_ERR("flash not erased..\n");
152 * This bit is set to one if we programming of at least one locked lock
155 if(EEFC0_FSR & BV(EEFC_FSR_FLOCKE))
157 fls->hw->status |= FLASH_WR_PROTECT;
158 LOG_ERR("wr protect..\n");
166 static size_t sam3_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
168 memcpy(buf, (void *)(idx * blk->blk_size + FLASH_BASE + offset), size);
172 static size_t sam3_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
175 ASSERT(size == blk->blk_size);
177 uint32_t *addr = (uint32_t *)(idx * blk->blk_size + FLASH_BASE);
178 const uint8_t *buf = (const uint8_t *)_buf;
182 uint32_t data = (*(buf + 3) << 24) |
193 flash_sendWRcmd(idx);
195 if (!flash_getStatus(blk))
198 return blk->blk_size;
202 static int sam3_flash_error(struct KBlock *blk)
204 Flash *fls = FLASH_CAST(blk);
205 return fls->hw->status;
208 static void sam3_flash_clearerror(struct KBlock *blk)
210 Flash *fls = FLASH_CAST(blk);
214 static const KBlockVTable flash_sam3_buffered_vt =
216 .readDirect = sam3_flash_readDirect,
217 .writeDirect = sam3_flash_writeDirect,
219 .readBuf = kblock_swReadBuf,
220 .writeBuf = kblock_swWriteBuf,
221 .load = kblock_swLoad,
222 .store = kblock_swStore,
224 .error = sam3_flash_error,
225 .clearerr = sam3_flash_clearerror,
228 static const KBlockVTable flash_sam3_unbuffered_vt =
230 .readDirect = sam3_flash_readDirect,
231 .writeDirect = sam3_flash_writeDirect,
233 .error = sam3_flash_error,
234 .clearerr = sam3_flash_clearerror,
237 static struct FlashHardware flash_sam3_hw;
238 static uint8_t flash_buf[FLASH_PAGE_SIZE_BYTES];
240 static void common_init(Flash *fls)
242 memset(fls, 0, sizeof(*fls));
243 DB(fls->blk.priv.type = KBT_FLASH);
245 fls->hw = &flash_sam3_hw;
247 fls->blk.blk_size = FLASH_PAGE_SIZE_BYTES;
248 fls->blk.blk_cnt = FLASH_MEM_SIZE / FLASH_PAGE_SIZE_BYTES;
251 void flash_hw_init(Flash *fls, UNUSED_ARG(int, flags))
254 fls->blk.priv.vt = &flash_sam3_buffered_vt;
255 fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
256 fls->blk.priv.buf = flash_buf;
258 /* Load the first block in the cache */
259 memcpy(fls->blk.priv.buf, (void *)(FLASH_BASE), fls->blk.blk_size);
262 void flash_hw_initUnbuffered(Flash *fls, UNUSED_ARG(int, flags))
265 fls->blk.priv.vt = &flash_sam3_unbuffered_vt;