ao_intflash: Avoid overwriting code
[fw/altos] / src / ao_intflash.c
1 /*
2  * Copyright © 2011  Anthony Towns <aj@erisian.com.au>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19 #include "cc1111.h"
20
21 #define FCTL_BUSY               (1 << 7)
22 #define FCTL_SWBSY              (1 << 6)
23 #define FCTL_CONTRD_ENABLE      (1 << 4)
24 #define FCTL_WRITE              (1 << 1)
25 #define FCTL_ERASE              (1 << 0)
26
27 #define ENDOFCODE  (CODESIZE)
28 #define NUM_PAGES ((0x8000-ENDOFCODE)/1024)
29 #define SIZE      (1024*NUM_PAGES)
30 #define LOCN      (0x8000 - SIZE)
31
32 #if NUM_PAGES < 1
33 #error "Too few pages"
34 #endif
35
36 #if LOCN % 1024 != 0
37 #error "Pages aren't aligned properly"
38 #endif
39
40 __xdata __at(LOCN) uint8_t ao_intflash[SIZE];
41
42 /* Total bytes of available storage */
43 __xdata uint32_t        ao_storage_total = sizeof(ao_intflash);
44
45 /* Block size - device is erased in these units. */
46 __xdata uint32_t        ao_storage_block = 1024;
47
48 /* Byte offset of config block. Will be ao_storage_block bytes long */
49 __xdata uint32_t        ao_storage_config = sizeof(ao_intflash);
50
51 /* Storage unit size - device reads and writes must be within blocks of this size. */
52 __xdata uint16_t        ao_storage_unit = 1024;
53
54 __xdata static uint8_t  ao_intflash_dma_done;
55 static uint8_t ao_intflash_dma;
56
57 /*
58  * The internal flash chip is arranged in 1kB sectors; the
59  * chip cannot erase in units smaller than that.
60  *
61  * Writing happens in units of 2 bytes and
62  * can only change bits from 1 to 0. So, you can rewrite
63  * the same contents, or append to an existing page easily enough
64  */
65
66 /*
67  * Erase the specified sector
68  */
69 uint8_t
70 ao_storage_erase(uint32_t pos) __reentrant
71 {
72         uint16_t addr;
73
74         if (pos >= ao_storage_total || pos + ao_storage_block > ao_storage_total)
75                 return 0;
76
77         addr = ((uint16_t)(ao_intflash + pos) >> 1);
78
79         while (FCTL & FCTL_BUSY)
80                 ;
81
82         FWT = 0x1F; // 21000 * f / 16e9 ; f = system freq = 24MHz
83         FADDRH = addr >> 8;
84         FADDRL = addr & ~0xFF00;
85
86         _asm
87         .even
88         orl _FCTL, #FCTL_ERASE;         ; FCTL |=  FCTL_ERASE
89         nop;                            ; Required, see datasheet.
90         _endasm;
91
92         while (FCTL & FCTL_BUSY)
93                 ;
94
95         return 1;
96 }
97
98 /*
99  * Write to flash
100  */
101
102 static void
103 word_aligned_write(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
104 {
105         uint16_t addr;
106
107         addr = ((uint16_t)(ao_intflash + pos) >> 1);
108
109         ao_dma_set_transfer(ao_intflash_dma, d, &X_FWDATA,
110                 DMA_LEN_HIGH_VLEN_LEN | len,
111                 DMA_CFG0_WORDSIZE_8 | DMA_CFG0_TMODE_SINGLE |
112                         DMA_CFG0_TRIGGER_FLASH,
113                 DMA_CFG1_SRCINC_1 | DMA_CFG1_DESTINC_0 |
114                         DMA_CFG1_IRQMASK | DMA_CFG1_PRIORITY_HIGH);
115
116         while (FCTL & FCTL_BUSY)
117                 ;
118
119         FWT = 0x1F; // 21000 * f / 16e9 ; f = system freq = 24MHz
120
121         FADDRH = addr >> 8;
122         FADDRL = addr & ~0xFF00;
123
124         ao_dma_start(ao_intflash_dma);
125
126         _asm
127         .even
128         orl _FCTL, #FCTL_WRITE;         ; FCTL |=  FCTL_WRITE
129         _endasm;
130
131         __critical while (!ao_intflash_dma_done)
132                 ao_sleep(&ao_intflash_dma_done);
133
134         while (FCTL & (FCTL_BUSY | FCTL_SWBSY))
135                 ;
136 }
137
138 uint8_t
139 ao_storage_device_write(uint32_t pos, __xdata void *v, uint16_t len) __reentrant
140 {
141         static __xdata uint8_t b[2];
142         __xdata uint8_t *d = v;
143         uint8_t oddlen;
144
145         if (pos >= ao_storage_total || pos + len > ao_storage_total)
146                 return 0;
147         if (len == 0)
148                 return 1;
149
150         if (pos & 1) {
151                 b[0] = ~0;
152                 b[1] = d[0];
153                 word_aligned_write(pos-1, b, 2);
154                 pos++;
155                 len--;
156                 d++;
157         }
158         oddlen = len & 1;
159         len &= ~1;
160         if (len > 0) {
161                 word_aligned_write(pos, d, len);
162         }
163         if (oddlen) {
164                 b[0] = d[len];
165                 b[1] = ~0;
166                 word_aligned_write(pos+len, b, 2);
167         }
168
169         return 1;
170 }
171
172 /*
173  * Read from flash
174  */
175 uint8_t
176 ao_storage_device_read(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
177 {
178         if (pos >= ao_storage_total || pos + len > ao_storage_total)
179                 return 0;
180         memcpy(d, ao_intflash+pos, len);
181         return 1;
182 }
183
184 void
185 ao_storage_flush(void) __reentrant
186 {
187 }
188
189 void
190 ao_storage_setup(void)
191 {
192 }
193
194 void
195 ao_storage_device_info(void) __reentrant
196 {
197         printf ("Using internal flash, starting at 0x%04x\n", LOCN);
198 }
199
200 void
201 ao_storage_device_init(void)
202 {
203         ao_intflash_dma = ao_dma_alloc(&ao_intflash_dma_done);
204 }