b3ef6a6273ac1bf8f88da1542965c002baf0bc70
[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_stm.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 }
43
44 static void
45 ao_flash_pgr_unlock(void)
46 {
47         if (!ao_flash_pgr_is_locked())
48                 return;
49
50         /* Unlock program memory */
51         stm_flash.prgkeyr = STM_FLASH_PRGKEYR_PRGKEY1;
52         stm_flash.prgkeyr = STM_FLASH_PRGKEYR_PRGKEY2;
53 }
54
55 static void
56 ao_flash_wait_bsy(void)
57 {
58         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
59                 ;
60 }
61
62 static void
63 ao_flash_erase_page(uint32_t *page)
64 {
65         ao_flash_pecr_unlock();
66         ao_flash_pgr_unlock();
67
68         stm_flash.pecr |= (1 << STM_FLASH_PECR_ERASE);
69         stm_flash.pecr |= (1 << STM_FLASH_PECR_PROG);
70         
71         ao_flash_wait_bsy();
72
73         *page = 0x00000000;
74 }
75
76 static void __attribute__ ((section(".text.ram"), noinline))
77         _ao_flash_half_page(uint32_t *dst, uint32_t *src)
78 {
79         uint8_t         i;
80
81         stm_flash.pecr |= (1 << STM_FLASH_PECR_FPRG);
82         stm_flash.pecr |= (1 << STM_FLASH_PECR_PROG);
83         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
84                 ;
85         
86         for (i = 0; i < 32; i++)
87                 *dst++ = *src++;
88 }
89
90 void
91 ao_flash_page(uint32_t *page, uint32_t *src)
92 {
93         uint8_t         h;
94
95         ao_flash_erase_page(page);
96         for (h = 0; h < 2; h++) {
97                 ao_flash_pecr_unlock();
98                 ao_flash_pgr_unlock();
99                 _ao_flash_half_page(page, src);
100                 page += 32;
101                 src += 32;
102         }
103 }