4ecdc7a526534644994a8fbb6a1ffbd8772bb4af
[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 uint8_t
132 ao_storage_device_erase(uint32_t pos)
133 {
134         ao_arch_block_interrupts();
135         ao_flash_unlock();
136
137         _ao_flash_erase_page(_ao_flash_addr(pos));
138
139         ao_flash_lock();
140         ao_arch_release_interrupts();
141         return 1;
142 }
143
144 uint8_t
145 ao_storage_device_write(uint32_t pos, void *v, uint16_t len)
146 {
147         if (len == 0)
148                 return 1;
149
150         ao_arch_block_interrupts();
151         ao_flash_unlock();
152
153         _ao_flash_write(pos, v, len);
154
155         ao_flash_lock();
156         ao_arch_release_interrupts();
157         return 1;
158 }
159
160 uint8_t
161 ao_storage_device_read(uint32_t pos, void *d, uint16_t len) 
162 {
163         if (pos >= ao_storage_total || pos + len > ao_storage_total)
164                 return 0;
165         memcpy(d, _ao_flash_addr(pos), len);
166         return 1;
167 }
168
169 void
170 ao_storage_flush(void) 
171 {
172 }
173
174 void
175 ao_storage_setup(void)
176 {
177         ao_storage_block = stm_flash_page_size();
178         ao_storage_total = ((uint8_t *) __flash_end__) - ((uint8_t *) __flash__);
179         ao_storage_unit = ao_storage_total;
180 }
181
182 void
183 ao_storage_device_info(void) 
184 {
185         printf ("Using internal flash, page %d bytes, total %d bytes\n",
186                 ao_storage_block, ao_storage_total);
187 }
188
189 void
190 ao_storage_device_init(void)
191 {
192 }