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; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
23 ao_flash_is_locked(void)
25 return (stm_flash.cr & (1 << STM_FLASH_CR_LOCK)) != 0;
31 if (!ao_flash_is_locked())
34 /* Unlock FLASH_CR register */
35 stm_flash.keyr = STM_FLASH_KEYR_KEY1;
36 stm_flash.keyr = STM_FLASH_KEYR_KEY2;
37 if (ao_flash_is_locked())
38 ao_panic(AO_PANIC_FLASH);
44 stm_flash.cr |= (1 << STM_FLASH_CR_LOCK);
48 ao_flash_wait_bsy(void)
50 while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
54 static void __attribute__ ((section(".ramtext"),noinline))
55 _ao_flash_erase_page(uint32_t *page)
57 stm_flash.cr |= (1 << STM_FLASH_CR_PER);
59 stm_flash.ar = (uintptr_t) page;
61 stm_flash.cr |= (1 << STM_FLASH_CR_STRT);
65 stm_flash.cr &= ~(1 << STM_FLASH_CR_PER);
69 stm_flash_page_size(void)
71 uint16_t dev_id = stm_dev_id();
74 case 0x440: /* stm32f05x */
75 case 0x444: /* stm32f03x */
76 case 0x445: /* stm32f04x */
78 case 0x442: /* stm32f09x */
79 case 0x448: /* stm32f07x */
82 ao_panic(AO_PANIC_FLASH);
87 ao_flash_erase_page(uint32_t *page)
89 /* Erase the whole page at the start. This assumes we'll be flashing things
93 if ((uintptr_t) page & (stm_flash_page_size() - 1))
96 ao_arch_block_interrupts();
99 _ao_flash_erase_page(page);
102 ao_arch_release_interrupts();
105 static void __attribute__ ((section(".ramtext"), noinline))
106 _ao_flash_page(uint16_t *dst, uint16_t *src)
110 stm_flash.cr |= (1 << STM_FLASH_CR_PG);
112 for (i = 0; i < 128; i++) {
117 stm_flash.cr &= ~(1 << STM_FLASH_CR_PG);
121 ao_flash_page(uint32_t *page, uint32_t *src)
123 ao_flash_erase_page(page);
125 ao_arch_block_interrupts();
128 _ao_flash_page((uint16_t *) page, (uint16_t *) src);
131 ao_arch_release_interrupts();