2 * Copyright © 2011 Anthony Towns <aj@erisian.com.au>
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.
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.
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.
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)
27 #define ENDOFCODE (0x51f0 + 1500)
28 #define NUM_PAGES ((0x8000-ENDOFCODE)/1024)
29 #define SIZE (1024*NUM_PAGES)
30 #define LOCN (0x8000 - SIZE)
33 #error "Too few pages"
37 #error "Pages aren't aligned properly"
40 __xdata __at(LOCN) uint8_t ao_intflash[SIZE];
42 /* Total bytes of available storage */
43 __xdata uint32_t ao_storage_total = sizeof(ao_intflash);
45 /* Block size - device is erased in these units. */
46 __xdata uint32_t ao_storage_block = 1024;
48 /* Byte offset of config block. Will be ao_storage_block bytes long */
49 __xdata uint32_t ao_storage_config = sizeof(ao_intflash);
51 /* Storage unit size - device reads and writes must be within blocks of this size. */
52 __xdata uint16_t ao_storage_unit = 1024;
54 __xdata static uint8_t ao_intflash_dma_done;
55 static uint8_t ao_intflash_dma;
58 * The internal flash chip is arranged in 1kB sectors; the
59 * chip cannot erase in units smaller than that.
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
67 * Erase the specified sector
70 ao_storage_erase(uint32_t pos) __reentrant
74 if (pos >= ao_storage_total || pos + ao_storage_block > ao_storage_total)
77 addr = ((uint16_t)(ao_intflash + pos) >> 1);
79 while (FCTL & FCTL_BUSY)
82 FWT = 0x1F; // 21000 * f / 16e9 ; f = system freq = 24MHz
84 FADDRL = addr & ~0xFF00;
88 orl _FCTL, #FCTL_ERASE; ; FCTL |= FCTL_ERASE
89 nop; ; Required, see datasheet.
92 while (FCTL & FCTL_BUSY)
103 word_aligned_write(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
107 addr = ((uint16_t)(ao_intflash + pos) >> 1);
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);
116 while (FCTL & FCTL_BUSY)
119 FWT = 0x1F; // 21000 * f / 16e9 ; f = system freq = 24MHz
122 FADDRL = addr & ~0xFF00;
124 ao_dma_start(ao_intflash_dma);
128 orl _FCTL, #FCTL_WRITE; ; FCTL |= FCTL_WRITE
131 __critical while (!ao_intflash_dma_done)
132 ao_sleep(&ao_intflash_dma_done);
134 while (FCTL & (FCTL_BUSY | FCTL_SWBSY))
139 ao_storage_device_write(uint32_t pos, __xdata void *v, uint16_t len) __reentrant
141 static __xdata uint8_t b[2];
142 __xdata uint8_t *d = v;
145 if (pos >= ao_storage_total || pos + len > ao_storage_total)
153 word_aligned_write(pos-1, b, 2);
161 word_aligned_write(pos, d, len);
166 word_aligned_write(pos+len, b, 2);
176 ao_storage_device_read(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
178 if (pos >= ao_storage_total || pos + len > ao_storage_total)
180 memcpy(d, ao_intflash+pos, len);
185 ao_storage_flush(void) __reentrant
190 ao_storage_setup(void)
195 ao_storage_device_info(void) __reentrant
197 printf ("Using internal flash, starting at 0x%04x\n", LOCN);
201 ao_storage_device_init(void)
203 ao_intflash_dma = ao_dma_alloc(&ao_intflash_dma_done);