2 * Copyright © 2017 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.
16 #include <ao_storage.h>
18 uint32_t ao_storage_block;
19 ao_pos_t ao_storage_total;
20 uint16_t ao_storage_unit;
22 /* Note that the HSI clock must be running for this code to work.
23 * Also, special care must be taken with the linker to ensure that the
24 * functions marked 'ramtext' land in ram and not rom. An example of that
25 * can be found in altos-loader.ld
29 ao_flash_is_locked(void)
31 return (stm_flash.cr & (1 << STM_FLASH_CR_LOCK)) != 0;
37 if (!ao_flash_is_locked())
40 /* Unlock FLASH_CR register */
41 stm_flash.keyr = STM_FLASH_KEYR_KEY1;
42 stm_flash.keyr = STM_FLASH_KEYR_KEY2;
43 if (ao_flash_is_locked())
44 ao_panic(AO_PANIC_FLASH);
50 stm_flash.cr |= (1 << STM_FLASH_CR_LOCK);
54 stm_flash_page_size(void)
56 uint16_t dev_id = stm_dev_id();
59 case 0x440: /* stm32f05x */
60 case 0x444: /* stm32f03x */
61 case 0x445: /* stm32f04x */
63 case 0x442: /* stm32f09x */
64 case 0x448: /* stm32f07x */
67 ao_panic(AO_PANIC_FLASH);
71 #define ao_flash_wait_bsy() do { while (stm_flash.sr & (1 << STM_FLASH_SR_BSY)); } while (0)
73 static void __attribute__ ((section(".sdata2.flash"), noinline))
74 _ao_flash_erase_page(uint16_t *page)
76 stm_flash.cr |= (1 << STM_FLASH_CR_PER);
78 stm_flash.ar = (uintptr_t) page;
80 stm_flash.cr |= (1 << STM_FLASH_CR_STRT);
84 stm_flash.cr &= ~(1 << STM_FLASH_CR_PER);
87 #define _ao_flash_addr(pos) ((uint16_t *) (void *) ((uint8_t *) __flash__ + (pos)))
89 static void __attribute__ ((section(".sdata2.flash"), noinline))
90 _ao_flash_byte(uint32_t pos, uint8_t b)
93 uint16_t *a = _ao_flash_addr(pos & ~1);
96 v = (*a & 0xff) | (b << 8);
98 v = (*a & 0xff00) | b;
103 static void __attribute__ ((section(".sdata2.flash"), noinline))
104 _ao_flash_write(uint32_t pos, void *sv, uint16_t len)
110 stm_flash.cr |= (1 << STM_FLASH_CR_PG);
113 _ao_flash_byte(pos++, *s++);
116 f16 = _ao_flash_addr(pos);
118 v = s[0] | (s[1] << 8);
126 _ao_flash_byte(pos, *s++);
128 stm_flash.cr &= ~(1 << STM_FLASH_CR_PG);
132 ao_storage_is_erased(uint32_t pos)
134 uint16_t *flash = _ao_flash_addr(pos);
135 uint32_t i = ao_storage_block >> 1;
138 if (*flash++ != 0xffff)
144 ao_storage_erase(uint32_t pos)
146 if (ao_storage_is_erased(pos))
149 ao_arch_block_interrupts();
152 _ao_flash_erase_page(_ao_flash_addr(pos));
155 ao_arch_release_interrupts();
160 ao_storage_device_write(uint32_t pos, void *v, uint16_t len)
165 ao_arch_block_interrupts();
168 _ao_flash_write(pos, v, len);
171 ao_arch_release_interrupts();
176 ao_storage_device_read(uint32_t pos, void *d, uint16_t len)
178 if (pos >= ao_storage_total || pos + len > ao_storage_total)
180 memcpy(d, _ao_flash_addr(pos), len);
185 ao_storage_flush(void)
190 ao_storage_setup(void)
192 ao_storage_block = stm_flash_page_size();
193 ao_storage_total = ((uint8_t *) __flash_end__) - ((uint8_t *) __flash__);
194 ao_storage_unit = ao_storage_total;
198 ao_storage_device_info(void)
200 printf ("Using internal flash, page %d bytes, total %d bytes\n",
201 ao_storage_block, ao_storage_total);
205 ao_storage_device_init(void)