Increase ethernet irq priority.
[bertos.git] / bertos / cpu / arm / drv / flash_at91.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 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Daniele Basile <asterix@develer.com>
34  *
35  * \brief At91sam7 Internal flash read/write driver.
36  *
37  *
38  */
39
40 #include "flash_at91.h"
41
42 #include "cfg/cfg_emb_flash.h"
43 #include <cfg/macros.h>
44
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
48 #include <cfg/log.h>
49
50 #include <cpu/irq.h>
51 #include <cpu/attr.h>
52 #include <cpu/power.h>
53
54 #include <io/kfile.h>
55 #include <io/kblock.h>
56 #include <io/arm.h>
57
58 #include <drv/timer.h>
59 #include <drv/flash.h>
60
61 #include <string.h>
62
63 struct FlashHardware
64 {
65         uint8_t status;
66 };
67
68
69 /**
70  * Really send the flash write command.
71  *
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.
75  */
76 RAM_FUNC NOINLINE static void write_page(uint32_t page)
77 {
78         reg32_t *fcr, *fsr;
79
80         #if CPU_ARM_AT91SAM7S512 || CPU_ARM_AT91SAM7X512
81         if (page >= (FLASH_MEM_SIZE / FLASH_PAGE_SIZE_BYTES / 2))
82         {
83                 fcr = &MC_FCR1;
84                 fsr = &MC_FSR1;
85                 page &= 0x3FF;
86         }
87         else
88         #endif
89         {
90                 fcr = &MC_FCR;
91                 fsr = &MC_FSR;
92         }
93
94         // Send the 'write page' command
95         *fcr = MC_KEY | MC_FCMD_WP | (MC_PAGEN_MASK & (page << 8));
96
97         // Wait for the end of command
98         while(!(*fsr & BV(MC_FRDY)))
99         {
100                 //NOP;
101         }
102 }
103
104
105 /**
106  * Send write command.
107  *
108  * After WR command cpu write bufferd page into flash memory.
109  *
110  */
111 INLINE void flash_sendWRcmd(uint32_t page)
112 {
113         cpu_flags_t flags;
114
115         LOG_INFO("Writing page %ld...\n", page);
116
117         IRQ_SAVE_DISABLE(flags);
118         write_page(page);
119
120         IRQ_RESTORE(flags);
121         LOG_INFO("Done\n");
122 }
123
124 /**
125  * Return true if no error are occurred after flash memory
126  * read or write operation, otherwise return error code.
127  */
128 static bool flash_getStatus(struct KBlock *blk)
129 {
130         Flash *fls = FLASH_CAST(blk);
131         /*
132          * This bit is set to one if an invalid command and/or a bad keywords was/were
133          * written in the Flash Command Register.
134          */
135         if(MC_FSR & BV(MC_PROGE))
136         {
137                 fls->hw->status |= FLASH_WR_ERR;
138                 LOG_ERR("flash not erased..\n");
139                 return false;
140         }
141
142         /*
143          * This bit is set to one if we programming of at least one locked lock
144          * region.
145          */
146         if(MC_FSR & BV(MC_LOCKE))
147         {
148                 fls->hw->status |= FLASH_WR_PROTECT;
149                 LOG_ERR("wr protect..\n");
150                 return false;
151         }
152         #if CPU_ARM_AT91SAM7X512 || CPU_ARM_AT91SAM7S512
153                 /* Check also EFC1 for larger devices */
154                 if(MC_FSR1 & BV(MC_PROGE))
155                 {
156                         fls->hw->status |= FLASH_WR_ERR;
157                         LOG_ERR("flash not erased..\n");
158                         return false;
159                 }
160
161                 if(MC_FSR1 & BV(MC_LOCKE))
162                 {
163                         fls->hw->status |= FLASH_WR_PROTECT;
164                         LOG_ERR("wr protect..\n");
165                         return false;
166                 }
167
168         #endif
169
170         return true;
171 }
172
173 static size_t at91_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
174 {
175         memcpy(buf, (void *)(idx * blk->blk_size +  FLASH_BASE + offset), size);
176         return size;
177 }
178
179 static size_t at91_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
180 {
181         ASSERT(offset == 0);
182         ASSERT(size == blk->blk_size);
183
184         uint32_t *addr = (uint32_t *)(idx * blk->blk_size +  FLASH_BASE);
185         const uint8_t *buf = (const uint8_t *)_buf;
186
187         while (size)
188         {
189                 uint32_t data = (*(buf + 3) << 24) |
190                                                 (*(buf + 2) << 16) |
191                                                 (*(buf + 1) << 8)  |
192                                                 *buf;
193                 *addr = data;
194
195                 size -= 4;
196                 buf += 4;
197                 addr++;
198         }
199
200         flash_sendWRcmd(idx);
201
202         if (!flash_getStatus(blk))
203                 return 0;
204
205         return blk->blk_size;
206 }
207
208
209 static int at91_flash_error(struct KBlock *blk)
210 {
211         Flash *fls = FLASH_CAST(blk);
212         return fls->hw->status;
213 }
214
215 static void at91_flash_clearerror(struct KBlock *blk)
216 {
217         Flash *fls = FLASH_CAST(blk);
218         fls->hw->status = 0;
219 }
220
221 static const KBlockVTable flash_at91_buffered_vt =
222 {
223         .readDirect = at91_flash_readDirect,
224         .writeDirect = at91_flash_writeDirect,
225
226         .readBuf = kblock_swReadBuf,
227         .writeBuf = kblock_swWriteBuf,
228         .load = kblock_swLoad,
229         .store = kblock_swStore,
230
231         .error = at91_flash_error,
232         .clearerr = at91_flash_clearerror,
233 };
234
235 static const KBlockVTable flash_at91_unbuffered_vt =
236 {
237         .readDirect = at91_flash_readDirect,
238         .writeDirect = at91_flash_writeDirect,
239
240         .error = at91_flash_error,
241         .clearerr = at91_flash_clearerror,
242 };
243
244 static struct FlashHardware flash_at91_hw;
245 static uint8_t flash_buf[FLASH_PAGE_SIZE_BYTES];
246
247 static void common_init(Flash *fls)
248 {
249         memset(fls, 0, sizeof(*fls));
250         DB(fls->blk.priv.type = KBT_FLASH);
251
252         fls->hw = &flash_at91_hw;
253
254         fls->blk.blk_size = FLASH_PAGE_SIZE_BYTES;
255         fls->blk.blk_cnt = FLASH_MEM_SIZE / FLASH_PAGE_SIZE_BYTES;
256 }
257
258 void flash_hw_init(Flash *fls, UNUSED_ARG(int, flags))
259 {
260         common_init(fls);
261         fls->blk.priv.vt = &flash_at91_buffered_vt;
262         fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
263         fls->blk.priv.buf = flash_buf;
264
265         /* Load the first block in the cache */
266         memcpy(fls->blk.priv.buf, (void *)(FLASH_BASE), fls->blk.blk_size);
267 }
268
269 void flash_hw_initUnbuffered(Flash *fls, UNUSED_ARG(int, flags))
270 {
271         common_init(fls);
272         fls->blk.priv.vt = &flash_at91_unbuffered_vt;
273 }
274