2 * Copyright © 2019 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 /* Erase rows are four pages */
25 samd21_nvmctrl_row_size(void)
27 return samd21_nvmctrl_page_size() * 4;
30 /* size of a lock region. That's just total flash size / 16 */
32 samd21_nvmctrl_lock_region(void)
34 return samd21_flash_size() >> 4;
37 /* Find the bit index of an address within the lock word */
39 ao_flash_lock_region_bit(void *addr)
41 uint32_t lock_region = samd21_nvmctrl_lock_region();
42 uintptr_t a = (uintptr_t) addr;
53 ao_flash_is_locked(void *addr)
55 return (samd21_nvmctrl.lock >> ao_flash_lock_region_bit(addr)) & 1;
58 /* Execute a single flash operation, waiting for it to complete. This
59 * bit of code must be in ram
61 static void __attribute__ ((section(".sdata2.flash"), noinline))
62 _ao_flash_execute(uint16_t cmd)
64 while ((samd21_nvmctrl.intflag & (1 << SAMD21_NVMCTRL_INTFLAG_READY)) == 0)
66 samd21_nvmctrl.ctrla = ((cmd << SAMD21_NVMCTRL_CTRLA_CMD) |
67 (SAMD21_NVMCTRL_CTRLA_CMDEX_KEY << SAMD21_NVMCTRL_CTRLA_CMDEX));
68 while ((samd21_nvmctrl.intflag & (1 << SAMD21_NVMCTRL_INTFLAG_READY)) == 0)
70 samd21_nvmctrl.intflag = ((1 << SAMD21_NVMCTRL_INTFLAG_READY) |
71 (1 << SAMD21_NVMCTRL_INTFLAG_ERROR));
74 /* Set the address of the next flash operation */
76 _ao_flash_set_addr(void *addr)
78 while ((samd21_nvmctrl.intflag & (1 << SAMD21_NVMCTRL_INTFLAG_READY)) == 0)
80 samd21_nvmctrl.addr = ((uint32_t) addr) >> 1;
81 while ((samd21_nvmctrl.intflag & (1 << SAMD21_NVMCTRL_INTFLAG_READY)) == 0)
85 /* Unlock a region of flash */
87 _ao_flash_unlock(void *addr)
89 if (!ao_flash_is_locked(addr))
92 _ao_flash_set_addr(addr);
93 _ao_flash_execute(SAMD21_NVMCTRL_CTRLA_CMD_UR);
96 /* Erase a row of flash */
98 _ao_flash_erase_row(void *row)
100 _ao_flash_unlock(row);
101 _ao_flash_set_addr(row);
102 _ao_flash_execute(SAMD21_NVMCTRL_CTRLA_CMD_ER);
106 ao_flash_erase_page(uint32_t *page)
108 uint8_t *row = (uint8_t *) page;
109 uint32_t row_size = samd21_nvmctrl_row_size();
110 uint32_t rows = (row_size + 255) / 256;
112 if ((uintptr_t) page & (row_size - 1))
115 ao_arch_block_interrupts();
117 if (((uintptr_t) row & (row_size - 1)) == 0) {
119 _ao_flash_erase_row(row);
124 ao_arch_release_interrupts();
128 ao_flash_page(uint32_t *page, uint32_t *src)
130 uint32_t page_shift = samd21_nvmctrl_page_shift();
131 uint32_t pages = 256 >> page_shift;
133 uint32_t per_page = 1 << (page_shift - 2);
135 ao_flash_erase_page(page);
137 ao_arch_block_interrupts();
140 /* Clear write buffer */
141 _ao_flash_execute(SAMD21_NVMCTRL_CTRLA_CMD_PBC);
142 _ao_flash_set_addr(page);
143 for (i = 0; i < per_page; i++)
145 _ao_flash_execute(SAMD21_NVMCTRL_CTRLA_CMD_WP);
148 ao_arch_release_interrupts();