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