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