ef2e261909574d8d8715ac850dc615f0a35f9263
[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 static uint8_t
23 ao_flash_is_locked(void)
24 {
25         return (stm_flash.cr & (1 << STM_FLASH_CR_LOCK)) != 0;
26 }
27
28 static void
29 ao_flash_unlock(void)
30 {
31         if (!ao_flash_is_locked())
32                 return;
33
34         /* Unlock FLASH_CR register */
35         stm_flash.keyr = STM_FLASH_KEYR_KEY1;
36         stm_flash.keyr = STM_FLASH_KEYR_KEY2;
37         if (ao_flash_is_locked())
38                 ao_panic(AO_PANIC_FLASH);
39 }
40
41 static void
42 ao_flash_lock(void)
43 {
44         stm_flash.cr |= (1 << STM_FLASH_CR_LOCK);
45 }
46
47 #define ao_flash_wait_bsy() do { while (stm_flash.sr & (1 << STM_FLASH_SR_BSY)); } while (0)
48
49 static void __attribute__ ((section(".ramtext"),noinline))
50 _ao_flash_erase_page(uint32_t *page)
51 {
52         stm_flash.cr |= (1 << STM_FLASH_CR_PER);
53
54         stm_flash.ar = (uintptr_t) page;
55
56         stm_flash.cr |= (1 << STM_FLASH_CR_STRT);
57
58         ao_flash_wait_bsy();
59
60         stm_flash.cr &= ~(1 << STM_FLASH_CR_PER);
61 }
62
63 static uint32_t
64 stm_flash_page_size(void)
65 {
66         uint16_t        dev_id = stm_dev_id();
67
68         switch (dev_id) {
69         case 0x440:     /* stm32f05x */
70         case 0x444:     /* stm32f03x */
71         case 0x445:     /* stm32f04x */
72                 return 1024;
73         case 0x442:     /* stm32f09x */
74         case 0x448:     /* stm32f07x */
75                 return 2048;
76         }
77         ao_panic(AO_PANIC_FLASH);
78         return 0;
79 }
80
81 void
82 ao_flash_erase_page(uint32_t *page)
83 {
84         /* Erase the whole page at the start. This assumes we'll be flashing things
85          * in memory order
86          */
87
88         if ((uintptr_t) page & (stm_flash_page_size() - 1))
89                 return;
90
91         ao_arch_block_interrupts();
92         ao_flash_unlock();
93
94         _ao_flash_erase_page(page);
95
96         ao_flash_lock();
97         ao_arch_release_interrupts();
98 }
99
100 static void __attribute__ ((section(".ramtext"), noinline))
101 _ao_flash_page(uint16_t *dst, uint16_t *src)
102 {
103         uint8_t         i;
104
105         stm_flash.cr |= (1 << STM_FLASH_CR_PG);
106
107         for (i = 0; i < 128; i++) {
108                 *dst++ = *src++;
109                 ao_flash_wait_bsy();
110         }
111
112         stm_flash.cr &= ~(1 << STM_FLASH_CR_PG);
113 }
114
115 void
116 ao_flash_page(uint32_t *page, uint32_t *src)
117 {
118         ao_flash_erase_page(page);
119
120         ao_arch_block_interrupts();
121         ao_flash_unlock();
122
123         _ao_flash_page((uint16_t *) page, (uint16_t *) src);
124
125         ao_flash_lock();
126         ao_arch_release_interrupts();
127 }