Merge branch 'master' of git://git.gag.com/fw/altos
[fw/altos] / src / stm / ao_eeprom_stm.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
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 <ao_storage.h>
20
21 /* Total bytes of available storage */
22 ao_pos_t        ao_storage_total = 4096;
23
24 /* Block size - device is erased in these units. */
25 ao_pos_t        ao_storage_block = 1024;
26
27 /* Byte offset of config block. Will be ao_storage_block bytes long */
28 ao_pos_t        ao_storage_config = 0;
29
30 /* Storage unit size - device reads and writes must be within blocks of this size. */
31 uint16_t        ao_storage_unit = 1024;
32
33 /* Location of eeprom in address space */
34 #define stm_eeprom      ((uint8_t *) 0x08080000)
35
36 /*
37  * The internal flash chip is arranged in 8 byte sectors; the
38  * chip cannot erase in units smaller than that.
39  *
40  * Writing happens in units of 2 bytes and
41  * can only change bits from 1 to 0. So, you can rewrite
42  * the same contents, or append to an existing page easily enough
43  */
44
45 /*
46  * Erase the specified sector
47  */
48 uint8_t
49 ao_storage_erase(ao_pos_t pos) __reentrant
50 {
51         /* Not necessary */
52         return 1;
53 }
54
55 static void
56 ao_intflash_unlock(void)
57 {
58         /* Unlock Data EEPROM and FLASH_PECR register */
59         stm_flash.pekeyr = STM_FLASH_PEKEYR_PEKEY1;
60         stm_flash.pekeyr = STM_FLASH_PEKEYR_PEKEY2;
61
62         /* Configure the FTDW bit (FLASH_PECR[8]) to execute
63          * word write, whatever the previous value of the word
64          * being written to
65          */
66         stm_flash.pecr = ((0 << STM_FLASH_PECR_OBL_LAUNCH) |
67                           (0 << STM_FLASH_PECR_ERRIE) |
68                           (0 << STM_FLASH_PECR_EOPIE) |
69                           (0 << STM_FLASH_PECR_FPRG) |
70                           (0 << STM_FLASH_PECR_ERASE) |
71                           (0 << STM_FLASH_PECR_FTDW) |
72                           (1 << STM_FLASH_PECR_DATA) |
73                           (0 << STM_FLASH_PECR_PROG) |
74                           (0 << STM_FLASH_PECR_OPTLOCK) |
75                           (0 << STM_FLASH_PECR_PRGLOCK) |
76                           (0 << STM_FLASH_PECR_PELOCK));
77 }
78
79 static void
80 ao_intflash_lock(void)
81 {
82         stm_flash.pecr |= (1 << STM_FLASH_PECR_PELOCK);
83 }
84
85 static void
86 ao_intflash_write32(uint16_t pos, uint32_t w)
87 {
88         uint32_t        *addr;
89
90         addr = (uint32_t *) (stm_eeprom + pos);
91
92         /* Write a word to a valid address in the data EEPROM */
93         *addr = w;
94
95         /* Wait for the flash unit to go idle */
96         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
97                 ;
98 }
99
100 static void
101 ao_intflash_write8(uint16_t pos, uint8_t d)
102 {
103         uint32_t        w, *addr, mask;
104         uint8_t         shift;
105         
106         addr = (uint32_t *) (stm_eeprom + (pos & ~3));
107
108         /* Compute word to be written */
109         shift = (pos & 3) << 3;
110         mask = 0xff << shift;
111         w = (*addr & ~mask) | (d << shift);
112
113         ao_intflash_write32(pos & ~3, w);
114 }
115
116 static uint8_t
117 ao_intflash_read(uint16_t pos)
118 {
119         return stm_eeprom[pos];
120 }
121
122 /*
123  * Write to flash
124  */
125
126 uint8_t
127 ao_storage_device_write(ao_pos_t pos32, __xdata void *v, uint16_t len) __reentrant
128 {
129         uint16_t pos = pos32;
130         __xdata uint8_t *d = v;
131
132         if (pos >= ao_storage_total || pos + len > ao_storage_total)
133                 return 0;
134
135         ao_intflash_unlock();
136         while (len) {
137                 if ((pos & 3) == 0 && len >= 4) {
138                         uint32_t        w;
139
140                         w = d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);
141                         ao_intflash_write32(pos, w);
142                         pos += 4;
143                         d += 4;
144                         len -= 4;
145                 } else {
146                         ao_intflash_write8(pos, *d);
147                         pos += 1;
148                         d += 1;
149                         len -= 1;
150                 }
151         }
152         ao_intflash_lock();
153
154         return 1;
155 }
156
157 /*
158  * Read from flash
159  */
160 uint8_t
161 ao_storage_device_read(ao_pos_t pos, __xdata void *v, uint16_t len) __reentrant
162 {
163         uint8_t *d = v;
164         
165         if (pos >= ao_storage_total || pos + len > ao_storage_total)
166                 return 0;
167         while (len--)
168                 *d++ = ao_intflash_read(pos++);
169         return 1;
170 }
171
172 void
173 ao_storage_flush(void) __reentrant
174 {
175 }
176
177 void
178 ao_storage_setup(void)
179 {
180 }
181
182 void
183 ao_storage_device_info(void) __reentrant
184 {
185         printf ("Using internal flash\n");
186 }
187
188 void
189 ao_storage_device_init(void)
190 {
191 }