altos: Move from newlib-nano to picolibc
[fw/altos] / src / stmf0 / ao_storage_stm.c
1 /*
2  * Copyright © 2017 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
15 #include <ao.h>
16 #include <ao_storage.h>
17
18 uint32_t        ao_storage_block;
19 ao_pos_t        ao_storage_total;
20 uint16_t        ao_storage_unit;
21
22 /* Note that the HSI clock must be running for this code to work.
23  * Also, special care must be taken with the linker to ensure that the
24  * functions marked 'ramtext' land in ram and not rom. An example of that
25  * can be found in altos-loader.ld
26  */
27
28 static uint8_t
29 ao_flash_is_locked(void)
30 {
31         return (stm_flash.cr & (1 << STM_FLASH_CR_LOCK)) != 0;
32 }
33
34 static void
35 ao_flash_unlock(void)
36 {
37         if (!ao_flash_is_locked())
38                 return;
39
40         /* Unlock FLASH_CR register */
41         stm_flash.keyr = STM_FLASH_KEYR_KEY1;
42         stm_flash.keyr = STM_FLASH_KEYR_KEY2;
43         if (ao_flash_is_locked())
44                 ao_panic(AO_PANIC_FLASH);
45 }
46
47 static void
48 ao_flash_lock(void)
49 {
50         stm_flash.cr |= (1 << STM_FLASH_CR_LOCK);
51 }
52
53 static uint32_t
54 stm_flash_page_size(void)
55 {
56         uint16_t        dev_id = stm_dev_id();
57
58         switch (dev_id) {
59         case 0x440:     /* stm32f05x */
60         case 0x444:     /* stm32f03x */
61         case 0x445:     /* stm32f04x */
62                 return 1024;
63         case 0x442:     /* stm32f09x */
64         case 0x448:     /* stm32f07x */
65                 return 2048;
66         }
67         ao_panic(AO_PANIC_FLASH);
68         return 0;
69 }
70
71 #define ao_flash_wait_bsy() do { while (stm_flash.sr & (1 << STM_FLASH_SR_BSY)); } while (0)
72
73 static void __attribute__ ((section(".sdata2.flash"), noinline))
74 _ao_flash_erase_page(uint16_t *page)
75 {
76         stm_flash.cr |= (1 << STM_FLASH_CR_PER);
77
78         stm_flash.ar = (uintptr_t) page;
79
80         stm_flash.cr |= (1 << STM_FLASH_CR_STRT);
81
82         ao_flash_wait_bsy();
83
84         stm_flash.cr &= ~(1 << STM_FLASH_CR_PER);
85 }
86
87 #define _ao_flash_addr(pos)     ((uint16_t *) (void *) ((uint8_t *) __flash__ + (pos)))
88
89 static void __attribute__ ((section(".sdata2.flash"), noinline))
90 _ao_flash_byte(uint32_t pos, uint8_t b)
91 {
92         uint16_t        v;
93         uint16_t        *a = _ao_flash_addr(pos & ~1);
94
95         if (pos & 1)
96                 v = (*a & 0xff) | (b << 8);
97         else
98                 v = (*a & 0xff00) | b;
99         *a = v;
100         ao_flash_wait_bsy();
101 }
102
103 static void __attribute__ ((section(".sdata2.flash"), noinline))
104 _ao_flash_write(uint32_t pos, void *sv, uint16_t len)
105 {
106         uint8_t         *s = sv;
107         uint16_t        *f16;
108         uint16_t        v;
109
110         stm_flash.cr |= (1 << STM_FLASH_CR_PG);
111
112         if (pos & 1) {
113                 _ao_flash_byte(pos++, *s++);
114                 len--;
115         }
116         f16 = _ao_flash_addr(pos);
117         while(len > 1) {
118                 v = s[0] | (s[1] << 8);
119                 s += 2;
120                 *f16++ = v;
121                 ao_flash_wait_bsy();
122                 len -= 2;
123                 pos += 2;
124         }
125         if (len)
126                 _ao_flash_byte(pos, *s++);
127
128         stm_flash.cr &= ~(1 << STM_FLASH_CR_PG);
129 }
130
131 static bool
132 ao_storage_is_erased(uint32_t pos)
133 {
134         uint16_t *flash = _ao_flash_addr(pos);
135         uint32_t i = ao_storage_block >> 1;
136
137         while (i--)
138                 if (*flash++ != 0xffff)
139                         return false;
140         return true;
141 }
142
143 uint8_t
144 ao_storage_erase(uint32_t pos)
145 {
146         if (ao_storage_is_erased(pos))
147                 return 1;
148
149         ao_arch_block_interrupts();
150         ao_flash_unlock();
151
152         _ao_flash_erase_page(_ao_flash_addr(pos));
153
154         ao_flash_lock();
155         ao_arch_release_interrupts();
156         return 1;
157 }
158
159 uint8_t
160 ao_storage_device_write(uint32_t pos, void *v, uint16_t len)
161 {
162         if (len == 0)
163                 return 1;
164
165         ao_arch_block_interrupts();
166         ao_flash_unlock();
167
168         _ao_flash_write(pos, v, len);
169
170         ao_flash_lock();
171         ao_arch_release_interrupts();
172         return 1;
173 }
174
175 uint8_t
176 ao_storage_device_read(uint32_t pos, void *d, uint16_t len) 
177 {
178         if (pos >= ao_storage_total || pos + len > ao_storage_total)
179                 return 0;
180         memcpy(d, _ao_flash_addr(pos), len);
181         return 1;
182 }
183
184 void
185 ao_storage_flush(void) 
186 {
187 }
188
189 void
190 ao_storage_setup(void)
191 {
192         ao_storage_block = stm_flash_page_size();
193         ao_storage_total = ((uint8_t *) __flash_end__) - ((uint8_t *) __flash__);
194         ao_storage_unit = ao_storage_total;
195 }
196
197 void
198 ao_storage_device_info(void) 
199 {
200         printf ("Using internal flash, page %d bytes, total %d bytes\n",
201                 ao_storage_block, ao_storage_total);
202 }
203
204 void
205 ao_storage_device_init(void)
206 {
207 }