]> git.gag.com Git - fw/altos/blob - src/stmf0/ao_flash_stm.c
Merge branch 'branch-1.6' into debian
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_flash.h>
20
21 static uint8_t
22 ao_flash_is_locked(void)
23 {
24         return (stm_flash.cr & (1 << STM_FLASH_CR_LOCK)) != 0;
25 }
26
27 static void
28 ao_flash_unlock(void)
29 {
30         if (!ao_flash_is_locked())
31                 return;
32
33         /* Unlock FLASH_CR register */
34         stm_flash.keyr = STM_FLASH_KEYR_KEY1;
35         stm_flash.keyr = STM_FLASH_KEYR_KEY2;
36         if (ao_flash_is_locked())
37                 ao_panic(AO_PANIC_FLASH);
38 }
39
40 static void
41 ao_flash_lock(void)
42 {
43         stm_flash.cr |= (1 << STM_FLASH_CR_LOCK);
44 }
45
46 static void
47 ao_flash_wait_bsy(void)
48 {
49         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
50                 ;
51 }
52
53 static void __attribute__ ((section(".ramtext"),noinline))
54 _ao_flash_erase_page(uint32_t *page)
55 {
56         stm_flash.cr |= (1 << STM_FLASH_CR_PER);
57
58         stm_flash.ar = (uintptr_t) page;
59
60         stm_flash.cr |= (1 << STM_FLASH_CR_STRT);
61
62         ao_flash_wait_bsy();
63
64         stm_flash.cr &= ~(1 << STM_FLASH_CR_PER);
65 }
66
67 static uint32_t
68 stm_flash_page_size(void)
69 {
70         uint16_t        dev_id = stm_dev_id();
71
72         switch (dev_id) {
73         case 0x440:     /* stm32f05x */
74         case 0x444:     /* stm32f03x */
75         case 0x445:     /* stm32f04x */
76                 return 1024;
77         case 0x442:     /* stm32f09x */
78         case 0x448:     /* stm32f07x */
79                 return 2048;
80         }
81         ao_panic(AO_PANIC_FLASH);
82         return 0;
83 }
84
85 void
86 ao_flash_erase_page(uint32_t *page)
87 {
88         /* Erase the whole page at the start. This assumes we'll be flashing things
89          * in memory order
90          */
91
92         if ((uintptr_t) page & (stm_flash_page_size() - 1))
93                 return;
94
95         ao_arch_block_interrupts();
96         ao_flash_unlock();
97
98         _ao_flash_erase_page(page);
99
100         ao_flash_lock();
101         ao_arch_release_interrupts();
102 }
103
104 static void __attribute__ ((section(".ramtext"), noinline))
105 _ao_flash_page(uint16_t *dst, uint16_t *src)
106 {
107         uint8_t         i;
108
109         stm_flash.cr |= (1 << STM_FLASH_CR_PG);
110
111         for (i = 0; i < 128; i++) {
112                 *dst++ = *src++;
113                 ao_flash_wait_bsy();
114         }
115
116         stm_flash.cr &= ~(1 << STM_FLASH_CR_PG);
117 }
118
119 void
120 ao_flash_page(uint32_t *page, uint32_t *src)
121 {
122         ao_flash_erase_page(page);
123
124         ao_arch_block_interrupts();
125         ao_flash_unlock();
126
127         _ao_flash_page((uint16_t *) page, (uint16_t *) src);
128
129         ao_flash_lock();
130         ao_arch_release_interrupts();
131 }