1 /*
2  * Copyright (c) 2017 BayLibre, SAS
3  * Copyright (c) 2019 Linaro Limited
4  * Copyright (c) 2020 Andreas Sandberg
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/logging/log.h>
10 LOG_MODULE_REGISTER(flash_stm32generic, CONFIG_FLASH_LOG_LEVEL);
11 
12 #include <zephyr/kernel.h>
13 #include <zephyr/device.h>
14 #include <string.h>
15 #include <zephyr/drivers/flash.h>
16 #include <zephyr/init.h>
17 #include <zephyr/sys/barrier.h>
18 #include <soc.h>
19 
20 #include "flash_stm32.h"
21 
22 #if FLASH_STM32_WRITE_BLOCK_SIZE == 8
23 typedef uint64_t flash_prg_t;
24 #elif FLASH_STM32_WRITE_BLOCK_SIZE == 4
25 typedef uint32_t flash_prg_t;
26 #elif FLASH_STM32_WRITE_BLOCK_SIZE == 2
27 typedef uint16_t flash_prg_t;
28 #elif FLASH_STM32_WRITE_BLOCK_SIZE == 1
29 typedef uint8_t flash_prg_t;
30 #else
31 #error Unknown write block size
32 #endif
33 
34 #if defined(FLASH_CR_PER)
35 #define FLASH_ERASED_VALUE ((flash_prg_t)-1)
36 #elif defined(FLASH_PECR_ERASE)
37 #define FLASH_ERASED_VALUE 0
38 #else
39 #error Unknown erase value
40 #endif
41 
get_page(off_t offset)42 static unsigned int get_page(off_t offset)
43 {
44 	return offset / FLASH_PAGE_SIZE;
45 }
46 
47 #if defined(FLASH_CR_PER)
is_flash_locked(FLASH_TypeDef * regs)48 static int is_flash_locked(FLASH_TypeDef *regs)
49 {
50 	return !!(regs->CR & FLASH_CR_LOCK);
51 }
52 
write_enable(FLASH_TypeDef * regs)53 static void write_enable(FLASH_TypeDef *regs)
54 {
55 	regs->CR |= FLASH_CR_PG;
56 }
57 
write_disable(FLASH_TypeDef * regs)58 static void write_disable(FLASH_TypeDef *regs)
59 {
60 	regs->CR &= (~FLASH_CR_PG);
61 }
62 
erase_page_begin(FLASH_TypeDef * regs,unsigned int page)63 static void erase_page_begin(FLASH_TypeDef *regs, unsigned int page)
64 {
65 	/* Set the PER bit and select the page you wish to erase */
66 	regs->CR |= FLASH_CR_PER;
67 	regs->AR = FLASH_STM32_BASE_ADDRESS + page * FLASH_PAGE_SIZE;
68 
69 	barrier_dsync_fence_full();
70 
71 	/* Set the STRT bit */
72 	regs->CR |= FLASH_CR_STRT;
73 }
74 
erase_page_end(FLASH_TypeDef * regs)75 static void erase_page_end(FLASH_TypeDef *regs)
76 {
77 	regs->CR &= ~FLASH_CR_PER;
78 }
79 
80 #else
81 
is_flash_locked(FLASH_TypeDef * regs)82 static int is_flash_locked(FLASH_TypeDef *regs)
83 {
84 	return !!(regs->PECR & FLASH_PECR_PRGLOCK);
85 }
86 
write_enable(FLASH_TypeDef * regs)87 static void write_enable(FLASH_TypeDef *regs)
88 {
89 	/* Only used for half-page programming on L1x */
90 #if !defined(CONFIG_SOC_SERIES_STM32L1X)
91 	regs->PECR |= FLASH_PECR_PROG;
92 #endif
93 }
94 
write_disable(FLASH_TypeDef * regs)95 static void write_disable(FLASH_TypeDef *regs)
96 {
97 	/* Clear the PG bit */
98 	regs->PECR &= ~FLASH_PECR_PROG;
99 }
100 
erase_page_begin(FLASH_TypeDef * regs,unsigned int page)101 static void erase_page_begin(FLASH_TypeDef *regs, unsigned int page)
102 {
103 	volatile flash_prg_t *page_base = (flash_prg_t *)(
104 		FLASH_STM32_BASE_ADDRESS + page * FLASH_PAGE_SIZE);
105 	/* Enable programming in erase mode. An erase is triggered by
106 	 * writing 0 to the first word of a page.
107 	 */
108 	regs->PECR |= FLASH_PECR_ERASE;
109 	regs->PECR |= FLASH_PECR_PROG;
110 
111 	barrier_dsync_fence_full();
112 
113 	*page_base = 0;
114 }
115 
erase_page_end(FLASH_TypeDef * regs)116 static void erase_page_end(FLASH_TypeDef *regs)
117 {
118 	/* Disable programming */
119 	regs->PECR &= ~FLASH_PECR_PROG;
120 	regs->PECR &= ~FLASH_PECR_ERASE;
121 }
122 #endif
123 
write_value(const struct device * dev,off_t offset,flash_prg_t val)124 static int write_value(const struct device *dev, off_t offset,
125 		       flash_prg_t val)
126 {
127 	volatile flash_prg_t *flash = (flash_prg_t *)(
128 		offset + FLASH_STM32_BASE_ADDRESS);
129 	FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
130 	int rc;
131 
132 	/* if the control register is locked, do not fail silently */
133 	if (is_flash_locked(regs)) {
134 		LOG_ERR("Flash is locked");
135 		return -EIO;
136 	}
137 
138 	/* Check that no Flash main memory operation is ongoing */
139 	rc = flash_stm32_wait_flash_idle(dev);
140 	if (rc < 0) {
141 		return rc;
142 	}
143 
144 	/* Check if this half word is erased */
145 	if (*flash != FLASH_ERASED_VALUE) {
146 		LOG_ERR("Flash location not erased");
147 		return -EIO;
148 	}
149 
150 	/* Enable writing */
151 	write_enable(regs);
152 
153 	/* Make sure the register write has taken effect */
154 	barrier_dsync_fence_full();
155 
156 	/* Perform the data write operation at the desired memory address */
157 	*flash = val;
158 
159 	/* Wait until the BSY bit is cleared */
160 	rc = flash_stm32_wait_flash_idle(dev);
161 
162 	/* Disable writing */
163 	write_disable(regs);
164 
165 	return rc;
166 }
167 
flash_stm32_block_erase_loop(const struct device * dev,unsigned int offset,unsigned int len)168 int flash_stm32_block_erase_loop(const struct device *dev,
169 				 unsigned int offset,
170 				 unsigned int len)
171 {
172 	FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
173 	int i, rc = 0;
174 
175 	/* if the control register is locked, do not fail silently */
176 	if (is_flash_locked(regs)) {
177 		LOG_ERR("Flash is locked");
178 		return -EIO;
179 	}
180 
181 	/* Check that no Flash memory operation is ongoing */
182 	rc = flash_stm32_wait_flash_idle(dev);
183 	if (rc < 0) {
184 		return rc;
185 	}
186 
187 	for (i = get_page(offset); i <= get_page(offset + len - 1); ++i) {
188 		erase_page_begin(regs, i);
189 		barrier_dsync_fence_full();
190 		rc = flash_stm32_wait_flash_idle(dev);
191 		erase_page_end(regs);
192 
193 		if (rc < 0) {
194 			break;
195 		}
196 	}
197 
198 	return rc;
199 }
200 
flash_stm32_write_range(const struct device * dev,unsigned int offset,const void * data,unsigned int len)201 int flash_stm32_write_range(const struct device *dev, unsigned int offset,
202 			    const void *data, unsigned int len)
203 {
204 	int i, rc = 0;
205 	flash_prg_t value;
206 
207 	for (i = 0; i < len / sizeof(flash_prg_t); i++) {
208 		memcpy(&value,
209 		       (const uint8_t *)data + i * sizeof(flash_prg_t),
210 		       sizeof(flash_prg_t));
211 		rc = write_value(dev, offset + i * sizeof(flash_prg_t), value);
212 		if (rc < 0) {
213 			return rc;
214 		}
215 	}
216 
217 	return rc;
218 }
219 
flash_stm32_page_layout(const struct device * dev,const struct flash_pages_layout ** layout,size_t * layout_size)220 void flash_stm32_page_layout(const struct device *dev,
221 			     const struct flash_pages_layout **layout,
222 			     size_t *layout_size)
223 {
224 	static struct flash_pages_layout flash_layout = {
225 		.pages_count = 0,
226 		.pages_size = 0,
227 	};
228 
229 	ARG_UNUSED(dev);
230 
231 	if (flash_layout.pages_count == 0) {
232 #if defined(CONFIG_SOC_SERIES_STM32F3X)
233 		flash_layout.pages_count =
234 			DT_REG_SIZE(DT_INST(0, soc_nv_flash)) / FLASH_PAGE_SIZE;
235 #else
236 		flash_layout.pages_count = (CONFIG_FLASH_SIZE * 1024) /
237 			FLASH_PAGE_SIZE;
238 #endif
239 		flash_layout.pages_size = FLASH_PAGE_SIZE;
240 	}
241 
242 	*layout = &flash_layout;
243 	*layout_size = 1;
244 }
245