altos: Remove 8051 address space specifiers
[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(".ramtext"),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(".ramtext"), noinline)) _ao_flash_byte(uint32_t pos, uint8_t b)
90 {
91         uint16_t        v;
92         uint16_t        *a = _ao_flash_addr(pos & ~1);
93
94         if (pos & 1)
95                 v = (*a & 0xff) | (b << 8);
96         else
97                 v = (*a & 0xff00) | b;
98         *a = v;
99         ao_flash_wait_bsy();
100 }
101
102 static void __attribute__ ((section(".ramtext"), noinline))
103 _ao_flash_write(uint32_t pos, void *sv, uint16_t len)
104 {
105         uint8_t         *s = sv;
106         uint16_t        *f16;
107         uint16_t        v;
108
109         stm_flash.cr |= (1 << STM_FLASH_CR_PG);
110
111         if (pos & 1) {
112                 _ao_flash_byte(pos++, *s++);
113                 len--;
114         }
115         f16 = _ao_flash_addr(pos);
116         while(len > 1) {
117                 v = s[0] | (s[1] << 8);
118                 s += 2;
119                 *f16++ = v;
120                 ao_flash_wait_bsy();
121                 len -= 2;
122                 pos += 2;
123         }
124         if (len)
125                 _ao_flash_byte(pos, *s++);
126
127         stm_flash.cr &= ~(1 << STM_FLASH_CR_PG);
128 }
129
130 uint8_t
131 ao_storage_erase(uint32_t pos)
132 {
133         ao_arch_block_interrupts();
134         ao_flash_unlock();
135
136         _ao_flash_erase_page(_ao_flash_addr(pos));
137
138         ao_flash_lock();
139         ao_arch_release_interrupts();
140         return 1;
141 }
142
143 uint8_t
144 ao_storage_device_write(uint32_t pos, void *v, uint16_t len)
145 {
146         if (len == 0)
147                 return 1;
148
149         ao_arch_block_interrupts();
150         ao_flash_unlock();
151
152         _ao_flash_write(pos, v, len);
153
154         ao_flash_lock();
155         ao_arch_release_interrupts();
156         return 1;
157 }
158
159 uint8_t
160 ao_storage_device_read(uint32_t pos, void *d, uint16_t len) 
161 {
162         if (pos >= ao_storage_total || pos + len > ao_storage_total)
163                 return 0;
164         memcpy(d, _ao_flash_addr(pos), len);
165         return 1;
166 }
167
168 void
169 ao_storage_flush(void) 
170 {
171 }
172
173 void
174 ao_storage_setup(void)
175 {
176         ao_storage_block = stm_flash_page_size();
177         ao_storage_total = ((uint8_t *) __flash_end__) - ((uint8_t *) __flash__);
178         ao_storage_unit = ao_storage_total;
179 }
180
181 void
182 ao_storage_device_info(void) 
183 {
184         printf ("Using internal flash, page %d bytes, total %d bytes\n",
185                 ao_storage_block, ao_storage_total);
186 }
187
188 void
189 ao_storage_device_init(void)
190 {
191 }