2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
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.
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.
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.
22 ao_flash_pecr_is_locked(void)
24 return (stm_flash.pecr & (1 << STM_FLASH_PECR_PELOCK)) != 0;
28 ao_flash_pgr_is_locked(void)
30 return (stm_flash.pecr & (1 << STM_FLASH_PECR_PRGLOCK)) != 0;
34 ao_flash_pecr_unlock(void)
36 if (!ao_flash_pecr_is_locked())
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);
47 ao_flash_pgr_unlock(void)
49 if (!ao_flash_pgr_is_locked())
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);
62 stm_flash.pecr |= (1 << STM_FLASH_PECR_OPTLOCK) | (1 << STM_FLASH_PECR_PRGLOCK) | (1 << STM_FLASH_PECR_PELOCK);
66 ao_flash_wait_bsy(void)
68 while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
72 static void __attribute__ ((section(".ramtext"),noinline))
73 _ao_flash_erase_page(uint32_t *page)
75 stm_flash.pecr |= (1 << STM_FLASH_PECR_ERASE) | (1 << STM_FLASH_PECR_PROG);
79 while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
84 ao_flash_erase_page(uint32_t *page)
86 ao_arch_block_interrupts();
87 ao_flash_pecr_unlock();
88 ao_flash_pgr_unlock();
90 _ao_flash_erase_page(page);
93 ao_arch_release_interrupts();
96 static void __attribute__ ((section(".ramtext"), noinline))
97 _ao_flash_half_page(uint32_t *dst, uint32_t *src)
101 stm_flash.pecr |= (1 << STM_FLASH_PECR_FPRG);
102 stm_flash.pecr |= (1 << STM_FLASH_PECR_PROG);
104 while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
107 for (i = 0; i < 32; i++) {
111 while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
116 ao_flash_page(uint32_t *page, uint32_t *src)
120 ao_flash_erase_page(page);
122 ao_arch_block_interrupts();
123 ao_flash_pecr_unlock();
124 ao_flash_pgr_unlock();
125 for (h = 0; h < 2; h++) {
126 _ao_flash_half_page(page, src);
131 ao_arch_release_interrupts();