Move flash related flags to the flash driver; refactor accordingly.
[bertos.git] / bertos / cpu / avr / drv / flash_avr.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 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Self programming routines.
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  * \author Daniele Basile <asterix@develer.com>
37  *
38  * This module implements a kfile-like access for Atmel avr internal flash.
39  * Internal flash writing access is controlled by BOOTSZ fuses, check
40  * datasheet for details.
41  */
42
43 #include "flash_avr.h"
44
45 #include "cfg/cfg_emb_flash.h"
46
47 #include <cfg/macros.h> // MIN()
48 #include <cfg/compiler.h>
49 #include <cfg/debug.h>
50 #include <cpu/irq.h>
51
52 // Define log settings for cfg/log.h
53 #define LOG_LEVEL    CONFIG_FLASH_EMB_LOG_LEVEL
54 #define LOG_FORMAT   CONFIG_FLASH_EMB_LOG_FORMAT
55 #include <cfg/log.h>
56
57 #include <drv/wdt.h>
58 #include <drv/flash.h>
59
60 #include <io/kfile.h>
61 #include <io/kfile_block.h>
62 #include <io/kblock.h>
63
64 #include <avr/io.h>
65 #include <avr/boot.h>
66 #include <avr/pgmspace.h>
67
68 #include <string.h>
69
70 struct FlashHardware;
71
72 static size_t avr_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
73 {
74         memcpy_P(buf, (const void *)(uint16_t)(idx * blk->blk_size + offset), size);
75         return blk->blk_size;
76 }
77
78 static size_t avr_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
79 {
80         ASSERT(offset == 0);
81         ASSERT(size == blk->blk_size);
82
83         uint32_t page_addr = idx * blk->blk_size;
84         uint32_t addr = idx * blk->blk_size;
85         const uint8_t *buf = (const uint8_t *)_buf;
86
87         /* Wait while the SPM instruction is busy. */
88         boot_spm_busy_wait();
89
90         /* Fill the temporary buffer of the AVR */
91         while (size)
92         {
93                 uint16_t data = ((*buf + 1) << 8) | *buf;
94                 ATOMIC(boot_page_fill(addr, data));
95
96                 buf += 2;
97                 size -= 2;
98                 addr += 2;
99         }
100
101         wdt_reset();
102
103         /* Page erase */
104         ATOMIC(boot_page_erase(page_addr));
105
106         /* Wait until the memory is erased. */
107         boot_spm_busy_wait();
108
109         /* Store buffer in flash page. */
110         ATOMIC(boot_page_write(page_addr));
111
112         /* Wait while the SPM instruction is busy. */
113         boot_spm_busy_wait();
114
115         /*
116         * Reenable RWW-section again. We need this if we want to jump back
117         * to the application after bootloading.
118         */
119         ATOMIC(boot_rww_enable());
120
121         return blk->blk_size;
122 }
123
124 static int avr_flash_dummy(UNUSED_ARG(struct KBlock, *blk))
125 {
126         return 0;
127 }
128
129 static const KBlockVTable flash_avr_buffered_vt =
130 {
131         .readDirect = avr_flash_readDirect,
132         .writeDirect = avr_flash_writeDirect,
133
134         .readBuf = kblock_swReadBuf,
135         .writeBuf = kblock_swWriteBuf,
136         .load = kblock_swLoad,
137         .store = kblock_swStore,
138
139         .error = avr_flash_dummy,
140         .clearerr = (kblock_clearerr_t)avr_flash_dummy,
141 };
142
143 static const KBlockVTable flash_avr_unbuffered_vt =
144 {
145         .readDirect = avr_flash_readDirect,
146         .writeDirect = avr_flash_writeDirect,
147
148         .error = avr_flash_dummy,
149         .clearerr = (kblock_clearerr_t)avr_flash_dummy,
150 };
151
152 static uint8_t flash_buf[SPM_PAGESIZE];
153
154 static void common_init(Flash *fls)
155 {
156         memset(fls, 0, sizeof(*fls));
157         DB(fls->blk.priv.type = KBT_FLASH);
158
159         fls->blk.blk_size = SPM_PAGESIZE;
160         fls->blk.blk_cnt =  (FLASHEND + 1) / SPM_PAGESIZE;
161 }
162
163
164 void flash_hw_init(Flash *fls, UNUSED_ARG(int, flags))
165 {
166         common_init(fls);
167         fls->blk.priv.vt = &flash_avr_buffered_vt;
168         fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
169         fls->blk.priv.buf = flash_buf;
170 }
171
172 void flash_hw_initUnbuffered(Flash *fls, UNUSED_ARG(int, flags))
173 {
174         common_init(fls);
175         fls->blk.priv.vt = &flash_avr_unbuffered_vt;
176 }
177
178