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_is_locked(void)
24 return (stm_flash.cr & (1 << STM_FLASH_CR_LOCK)) != 0;
30 if (!ao_flash_is_locked())
33 /* Unlock FLASH_CR register */
34 stm_flash.keyr = STM_FLASH_KEYR_KEY1;
35 stm_flash.keyr = STM_FLASH_KEYR_KEY2;
36 if (ao_flash_is_locked())
37 ao_panic(AO_PANIC_FLASH);
43 stm_flash.cr |= (1 << STM_FLASH_CR_LOCK);
47 ao_flash_wait_bsy(void)
49 while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
53 static void __attribute__ ((section(".ramtext"),noinline))
54 _ao_flash_erase_page(uint32_t *page)
56 stm_flash.cr |= (1 << STM_FLASH_CR_PER);
58 stm_flash.ar = (uintptr_t) page;
60 stm_flash.cr |= (1 << STM_FLASH_CR_STRT);
64 stm_flash.cr &= ~(1 << STM_FLASH_CR_PER);
68 stm_flash_page_size(void)
70 uint16_t dev_id = stm_dev_id();
73 case 0x440: /* stm32f05x */
74 case 0x444: /* stm32f03x */
75 case 0x445: /* stm32f04x */
77 case 0x442: /* stm32f09x */
78 case 0x448: /* stm32f07x */
81 ao_panic(AO_PANIC_FLASH);
86 ao_flash_erase_page(uint32_t *page)
88 /* Erase the whole page at the start. This assumes we'll be flashing things
92 if ((uintptr_t) page & (stm_flash_page_size() - 1))
95 ao_arch_block_interrupts();
98 _ao_flash_erase_page(page);
101 ao_arch_release_interrupts();
104 static void __attribute__ ((section(".ramtext"), noinline))
105 _ao_flash_page(uint16_t *dst, uint16_t *src)
109 stm_flash.cr |= (1 << STM_FLASH_CR_PG);
111 for (i = 0; i < 128; i++) {
116 stm_flash.cr &= ~(1 << STM_FLASH_CR_PG);
120 ao_flash_page(uint32_t *page, uint32_t *src)
122 ao_flash_erase_page(page);
124 ao_arch_block_interrupts();
127 _ao_flash_page((uint16_t *) page, (uint16_t *) src);
130 ao_arch_release_interrupts();