Comply to new api.
[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         ASSERT(offset == 0);
75         ASSERT(size == blk->blk_size);
76
77         memcpy_P(buf, (const void *)(uint16_t)(idx * blk->blk_size), size);
78         return blk->blk_size;
79 }
80
81 static size_t avr_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
82 {
83         ASSERT(offset == 0);
84         ASSERT(size == blk->blk_size);
85
86         uint32_t page_addr = idx * blk->blk_size;
87         uint32_t addr = idx * blk->blk_size;
88         const uint8_t *buf = (const uint8_t *)_buf;
89
90         /* Wait while the SPM instruction is busy. */
91         boot_spm_busy_wait();
92
93         /* Fill the temporary buffer of the AVR */
94         while (size)
95         {
96                 uint16_t data = ((*buf + 1) << 8) | *buf;
97                 ATOMIC(boot_page_fill(addr, data));
98
99                 buf += 2;
100                 size -= 2;
101                 addr += 2;
102         }
103
104         wdt_reset();
105
106         /* Page erase */
107         ATOMIC(boot_page_erase(page_addr));
108
109         /* Wait until the memory is erased. */
110         boot_spm_busy_wait();
111
112         /* Store buffer in flash page. */
113         ATOMIC(boot_page_write(page_addr));
114
115         /* Wait while the SPM instruction is busy. */
116         boot_spm_busy_wait();
117
118         /*
119         * Reenable RWW-section again. We need this if we want to jump back
120         * to the application after bootloading.
121         */
122         ATOMIC(boot_rww_enable());
123
124         return blk->blk_size;
125 }
126
127 static int avr_flash_dummy(UNUSED_ARG(struct KBlock, *blk))
128 {
129         return 0;
130 }
131
132 static const KBlockVTable flash_avr_buffered_vt =
133 {
134         .readDirect = avr_flash_readDirect,
135         .writeDirect = avr_flash_writeDirect,
136
137         .readBuf = kblock_swReadBuf,
138         .writeBuf = kblock_swWriteBuf,
139         .load = kblock_swLoad,
140         .store = kblock_swStore,
141
142         .error = avr_flash_dummy,
143         .clearerr = (kblock_clearerr_t)avr_flash_dummy,
144 };
145
146 static const KBlockVTable flash_avr_unbuffered_vt =
147 {
148         .readDirect = avr_flash_readDirect,
149         .writeDirect = avr_flash_writeDirect,
150
151         .error = avr_flash_dummy,
152         .clearerr = (kblock_clearerr_t)avr_flash_dummy,
153 };
154
155 static uint8_t flash_buf[SPM_PAGESIZE];
156
157 static void common_init(Flash *fls)
158 {
159         memset(fls, 0, sizeof(*fls));
160         DB(fls->blk.priv.type = KBT_FLASH);
161
162         fls->blk.blk_size = SPM_PAGESIZE;
163         fls->blk.blk_cnt =  (FLASHEND + 1) / SPM_PAGESIZE;
164 }
165
166
167 void flash_hw_init(Flash *fls, int flags)
168 {
169         common_init(fls);
170         fls->blk.priv.vt = &flash_avr_buffered_vt;
171         fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE | flags;
172         fls->blk.priv.buf = flash_buf;
173 }
174
175 void flash_hw_initUnbuffered(Flash *fls, int flags)
176 {
177         common_init(fls);
178         fls->blk.priv.vt = &flash_avr_unbuffered_vt;
179         fls->blk.priv.flags |= flags;
180 }
181
182