2 * Copyright © 2020 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_pecr_is_locked(void)
25 return (stm_flash.pecr & (1 << STM_FLASH_PECR_PE_LOCK)) != 0;
29 ao_flash_pgr_is_locked(void)
31 return (stm_flash.pecr & (1 << STM_FLASH_PECR_PRG_LOCK)) != 0;
35 ao_flash_pecr_unlock(void)
37 if (!ao_flash_pecr_is_locked())
40 /* Unlock Data EEPROM and FLASH_PECR register */
41 stm_flash.pekeyr = STM_FLASH_PEKEYR_PEKEY1;
42 stm_flash.pekeyr = STM_FLASH_PEKEYR_PEKEY2;
43 if (ao_flash_pecr_is_locked())
44 ao_panic(AO_PANIC_FLASH);
48 ao_flash_pgr_unlock(void)
50 if (!ao_flash_pgr_is_locked())
53 /* Unlock program memory */
54 stm_flash.prgkeyr = STM_FLASH_PRGKEYR_PRGKEY1;
55 stm_flash.prgkeyr = STM_FLASH_PRGKEYR_PRGKEY2;
56 if (ao_flash_pgr_is_locked())
57 ao_panic(AO_PANIC_FLASH);
63 stm_flash.pecr |= (1 << STM_FLASH_PECR_OPT_LOCK) | (1 << STM_FLASH_PECR_PRG_LOCK) | (1 << STM_FLASH_PECR_PE_LOCK);
67 ao_flash_wait_bsy(void)
69 while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
73 static void __attribute__ ((section(".sdata2.flash"), noinline))
74 _ao_flash_erase_page(uint32_t *page)
76 stm_flash.pecr |= (1 << STM_FLASH_PECR_ERASE) | (1 << STM_FLASH_PECR_PROG);
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();
97 static void __attribute__ ((section(".sdata2.flash"), noinline))
98 _ao_flash_half_page(uint32_t *dst, uint32_t *src)
102 stm_flash.pecr |= (1 << STM_FLASH_PECR_FPRG);
103 stm_flash.pecr |= (1 << STM_FLASH_PECR_PROG);
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();
136 ao_storage_setup(void)
140 static ao_pos_t write_pos;
141 static uint8_t write_pending;
148 ao_storage_flush(void)
151 ao_flash_pecr_unlock();
152 ao_flash_pgr_unlock();
153 __storage[write_pos] = write_buf.u32;
159 /* Read data within a storage unit */
161 ao_storage_device_read(ao_pos_t pos, void *buf, uint16_t len)
163 memcpy(buf, ((uint8_t *) __storage) + pos, len);
168 flash_write_select(ao_pos_t pos)
170 /* Flush any pending writes to another word */
172 if (pos == write_pos)
174 __storage[write_pos] = write_buf.u32;
180 /* Write data within a storage unit */
182 ao_storage_device_write(ao_pos_t pos, void *buf, uint16_t len)
186 ao_flash_pecr_unlock();
187 ao_flash_pgr_unlock();
189 ao_pos_t this_pos = pos >> 2;
191 flash_write_select(this_pos);
193 /* Update write buffer with new contents */
194 int this_word = 4 - (pos & 3);
197 memcpy(&write_buf.u8[pos & 3], b8, this_word);
202 /* If we filled the buffer, flush it out */
203 if ((pos & 3) == 0) {
204 __storage[write_pos] = write_buf.u32;
213 /* Erase device from pos through pos + ao_storage_block */
215 ao_storage_device_erase(uint32_t pos)
217 ao_flash_erase_page(__storage + pos);
221 /* Initialize low-level device bits */
223 ao_storage_device_init(void)
227 /* Print out information about flash chips */
229 ao_storage_device_info(void)
231 printf("Detected chips 1 size %d\n", ao_storage_total);