altos/stm32l0: Add casts to reduce -Wconversion warnings
[fw/altos] / src / stm32l0 / ao_flash_stm32l0.c
1 /*
2  * Copyright © 2020 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_flash.h>
21
22 static uint8_t
23 ao_flash_pecr_is_locked(void)
24 {
25         return (stm_flash.pecr & (1 << STM_FLASH_PECR_PE_LOCK)) != 0;
26 }
27
28 static uint8_t
29 ao_flash_pgr_is_locked(void)
30 {
31         return (stm_flash.pecr & (1 << STM_FLASH_PECR_PRG_LOCK)) != 0;
32 }
33
34 static void
35 ao_flash_pecr_unlock(void)
36 {
37         if (!ao_flash_pecr_is_locked())
38                 return;
39
40         /* Unlock Data EEPROM and FLASH_PECR register */
41         stm_flash.pekeyr = STM_FLASH_PEKEYR_PEKEY1;
42         stm_flash.pekeyr = STM_FLASH_PEKEYR_PEKEY2;
43         if (ao_flash_pecr_is_locked())
44                 ao_panic(AO_PANIC_FLASH);
45 }
46
47 static void
48 ao_flash_pgr_unlock(void)
49 {
50         if (!ao_flash_pgr_is_locked())
51                 return;
52
53         /* Unlock program memory */
54         stm_flash.prgkeyr = STM_FLASH_PRGKEYR_PRGKEY1;
55         stm_flash.prgkeyr = STM_FLASH_PRGKEYR_PRGKEY2;
56         if (ao_flash_pgr_is_locked())
57                 ao_panic(AO_PANIC_FLASH);
58 }
59
60 static void
61 ao_flash_lock(void)
62 {
63         stm_flash.pecr |= (1 << STM_FLASH_PECR_OPT_LOCK) | (1 << STM_FLASH_PECR_PRG_LOCK) | (1 << STM_FLASH_PECR_PE_LOCK);
64 }
65
66 static void
67 ao_flash_wait_bsy(void)
68 {
69         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
70                 ;
71 }
72
73 static void __attribute__ ((section(".sdata2.flash"), noinline))
74 _ao_flash_erase_page(uint32_t *page)
75 {
76         stm_flash.pecr |= (1 << STM_FLASH_PECR_ERASE) | (1 << STM_FLASH_PECR_PROG);
77
78         *page = 0x00000000;
79
80         ao_flash_wait_bsy();
81 }
82
83 void
84 ao_flash_erase_page(uint32_t *page)
85 {
86         ao_arch_block_interrupts();
87         ao_flash_pecr_unlock();
88         ao_flash_pgr_unlock();
89
90         _ao_flash_erase_page(page);
91
92         ao_flash_lock();
93         ao_arch_release_interrupts();
94 }
95
96 #if 0
97 static void __attribute__ ((section(".sdata2.flash"), noinline))
98 _ao_flash_half_page(uint32_t *dst, uint32_t *src)
99 {
100         uint8_t         i;
101
102         stm_flash.pecr |= (1 << STM_FLASH_PECR_FPRG);
103         stm_flash.pecr |= (1 << STM_FLASH_PECR_PROG);
104
105         ao_flash_wait_bsy();
106
107         for (i = 0; i < 32; i++) {
108                 *dst++ = *src++;
109         }
110
111         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
112                 ;
113 }
114
115 void
116 ao_flash_page(uint32_t *page, uint32_t *src)
117 {
118         uint8_t         h;
119
120         ao_flash_erase_page(page);
121
122         ao_arch_block_interrupts();
123         ao_flash_pecr_unlock();
124         ao_flash_pgr_unlock();
125         for (h = 0; h < 2; h++) {
126                 _ao_flash_half_page(page, src);
127                 page += 32;
128                 src += 32;
129         }
130         ao_flash_lock();
131         ao_arch_release_interrupts();
132 }
133 #endif
134
135 static ao_pos_t write_pos;
136 static uint8_t write_pending;
137 static union {
138         uint32_t        u32;
139         uint8_t         u8[4];
140 } write_buf;
141
142 void
143 ao_storage_flush(void)
144 {
145         if (write_pending) {
146                 ao_flash_pecr_unlock();
147                 ao_flash_pgr_unlock();
148                 __storage[write_pos] = write_buf.u32;
149                 ao_flash_lock();
150                 write_pending = 0;
151         }
152 }
153
154 /* Read data within a storage unit */
155 uint8_t
156 ao_storage_device_read(ao_pos_t pos, void *buf, uint16_t len)
157 {
158         memcpy(buf, ((uint8_t *) __storage) + pos, len);
159         return 1;
160 }
161
162 static void
163 flash_write_select(ao_pos_t pos)
164 {
165         /* Flush any pending writes to another word */
166         if (write_pending) {
167                 if (pos == write_pos)
168                         return;
169                 __storage[write_pos] = write_buf.u32;
170                 write_pending = 0;
171         }
172         write_pos = pos;
173 }
174
175 /* Write data within a storage unit */
176 uint8_t
177 ao_storage_device_write(ao_pos_t pos, void *buf, uint16_t len)
178 {
179         uint8_t *b8 = buf;
180
181         ao_flash_pecr_unlock();
182         ao_flash_pgr_unlock();
183         while (len) {
184                 ao_pos_t        this_pos = pos >> 2;
185
186                 flash_write_select(this_pos);
187
188                 /* Update write buffer with new contents */
189                 uint16_t this_word = 4 - (pos & 3);
190                 if (this_word > len)
191                         this_word = len;
192                 memcpy(&write_buf.u8[pos & 3], b8, this_word);
193                 pos += this_word;
194                 len -= this_word;
195                 b8 += this_word;
196
197                 /* If we filled the buffer, flush it out */
198                 if ((pos & 3) == 0) {
199                         __storage[write_pos] = write_buf.u32;
200                 } else {
201                         write_pending = 1;
202                 }
203         }
204         ao_flash_lock();
205         return 1;
206 }
207
208 bool
209 ao_storage_device_is_erased(uint32_t pos)
210 {
211         uint8_t *m = ((uint8_t *) __storage) + pos;
212         uint32_t i;
213
214         for (i = 0; i < STM_FLASH_PAGE_SIZE; i++)
215                 if (*m++ != AO_STORAGE_ERASED_BYTE)
216                         return false;
217         return true;
218 }
219
220 /* Erase device from pos through pos + ao_storage_block */
221 uint8_t
222 ao_storage_device_erase(uint32_t pos)
223 {
224         ao_flash_erase_page(__storage + (pos >> 2));
225         return 1;
226 }