altos/telelco-v2.0: A bit fancier with the drag-mode LED show
[fw/altos] / src / stmf0 / ao_flash_stm.c
1 /*
2  * Copyright © 2013 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 /* 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 #define ao_flash_wait_bsy() do { while (stm_flash.sr & (1 << STM_FLASH_SR_BSY)); } while (0)
54
55 static void __attribute__ ((section(".ramtext"),noinline))
56 _ao_flash_erase_page(uint32_t *page)
57 {
58         stm_flash.cr |= (1 << STM_FLASH_CR_PER);
59
60         stm_flash.ar = (uintptr_t) page;
61
62         stm_flash.cr |= (1 << STM_FLASH_CR_STRT);
63
64         ao_flash_wait_bsy();
65
66         stm_flash.cr &= ~(1 << STM_FLASH_CR_PER);
67 }
68
69 static uint32_t
70 stm_flash_page_size(void)
71 {
72         uint16_t        dev_id = stm_dev_id();
73
74         switch (dev_id) {
75         case 0x440:     /* stm32f05x */
76         case 0x444:     /* stm32f03x */
77         case 0x445:     /* stm32f04x */
78                 return 1024;
79         case 0x442:     /* stm32f09x */
80         case 0x448:     /* stm32f07x */
81                 return 2048;
82         }
83         ao_panic(AO_PANIC_FLASH);
84         return 0;
85 }
86
87 void
88 ao_flash_erase_page(uint32_t *page)
89 {
90         /* Erase the whole page at the start. This assumes we'll be flashing things
91          * in memory order
92          */
93
94         if ((uintptr_t) page & (stm_flash_page_size() - 1))
95                 return;
96
97         ao_arch_block_interrupts();
98         ao_flash_unlock();
99
100         _ao_flash_erase_page(page);
101
102         ao_flash_lock();
103         ao_arch_release_interrupts();
104 }
105
106 static void __attribute__ ((section(".ramtext"), noinline))
107 _ao_flash_page(uint16_t *dst, uint16_t *src)
108 {
109         uint8_t         i;
110
111         stm_flash.cr |= (1 << STM_FLASH_CR_PG);
112
113         for (i = 0; i < 128; i++) {
114                 *dst++ = *src++;
115                 ao_flash_wait_bsy();
116         }
117
118         stm_flash.cr &= ~(1 << STM_FLASH_CR_PG);
119 }
120
121 void
122 ao_flash_page(uint32_t *page, uint32_t *src)
123 {
124         ao_flash_erase_page(page);
125
126         ao_arch_block_interrupts();
127         ao_flash_unlock();
128
129         _ao_flash_page((uint16_t *) page, (uint16_t *) src);
130
131         ao_flash_lock();
132         ao_arch_release_interrupts();
133 }