Merge branch 'master-fixes' into stm-flash-fixes
[fw/altos] / src / stm / 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_pecr_is_locked(void)
23 {
24         return (stm_flash.pecr & (1 << STM_FLASH_PECR_PELOCK)) != 0;
25 }
26
27 static uint8_t
28 ao_flash_pgr_is_locked(void)
29 {
30         return (stm_flash.pecr & (1 << STM_FLASH_PECR_PRGLOCK)) != 0;
31 }
32
33 static void
34 ao_flash_pecr_unlock(void)
35 {
36         if (!ao_flash_pecr_is_locked())
37                 return;
38
39         /* Unlock Data EEPROM and FLASH_PECR register */
40         stm_flash.pekeyr = STM_FLASH_PEKEYR_PEKEY1;
41         stm_flash.pekeyr = STM_FLASH_PEKEYR_PEKEY2;
42         if (ao_flash_pecr_is_locked())
43                 ao_panic(AO_PANIC_FLASH);
44 }
45
46 static void
47 ao_flash_pgr_unlock(void)
48 {
49         if (!ao_flash_pgr_is_locked())
50                 return;
51
52         /* Unlock program memory */
53         stm_flash.prgkeyr = STM_FLASH_PRGKEYR_PRGKEY1;
54         stm_flash.prgkeyr = STM_FLASH_PRGKEYR_PRGKEY2;
55         if (ao_flash_pgr_is_locked())
56                 ao_panic(AO_PANIC_FLASH);
57 }
58
59 static void
60 ao_flash_lock(void)
61 {
62         stm_flash.pecr |= (1 << STM_FLASH_PECR_OPTLOCK) | (1 << STM_FLASH_PECR_PRGLOCK) | (1 << STM_FLASH_PECR_PELOCK);
63 }
64
65 static void
66 ao_flash_wait_bsy(void)
67 {
68         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
69                 ;
70 }
71
72 static void __attribute__ ((section(".text.ram"),noinline))
73 _ao_flash_erase_page(uint32_t *page)
74 {
75         stm_flash.pecr |= (1 << STM_FLASH_PECR_ERASE) | (1 << STM_FLASH_PECR_PROG);
76         
77         *page = 0x00000000;
78
79         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
80                 ;
81 }
82
83 void
84 ao_flash_erase_page(uint32_t *page)
85 {
86         ao_flash_pecr_unlock();
87         ao_flash_pgr_unlock();
88
89         _ao_flash_erase_page(page);
90
91         ao_flash_lock();
92 }
93
94 static void __attribute__ ((section(".text.ram"), noinline))
95 _ao_flash_half_page(uint32_t *dst, uint32_t *src)
96 {
97         uint8_t         i;
98
99         stm_flash.pecr |= (1 << STM_FLASH_PECR_FPRG);
100         stm_flash.pecr |= (1 << STM_FLASH_PECR_PROG);
101         
102         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
103                 ;
104
105         for (i = 0; i < 32; i++) {
106                 *dst++ = *src++;
107         }
108
109         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
110                 ;
111 }
112
113 void
114 ao_flash_page(uint32_t *page, uint32_t *src)
115 {
116         uint8_t         h;
117
118         ao_flash_erase_page(page);
119         ao_flash_pecr_unlock();
120         ao_flash_pgr_unlock();
121         for (h = 0; h < 2; h++) {
122                 _ao_flash_half_page(page, src);
123                 page += 32;
124                 src += 32;
125         }
126         ao_flash_lock();
127 }