1 /*
2  * Copyright (c) 2017 Linaro Limited
3  * Copyright (c) 2017 BayLibre, SAS
4  * Copyright (c) 2019 Centaur Analytics, Inc
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define LOG_DOMAIN flash_stm32l4
10 #define LOG_LEVEL CONFIG_FLASH_LOG_LEVEL
11 #include <zephyr/logging/log.h>
12 LOG_MODULE_REGISTER(LOG_DOMAIN);
13 
14 #include <zephyr/kernel.h>
15 #include <zephyr/device.h>
16 #include <string.h>
17 #include <zephyr/drivers/flash.h>
18 #include <zephyr/sys/barrier.h>
19 #include <zephyr/init.h>
20 #include <soc.h>
21 
22 #include "flash_stm32.h"
23 
24 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && \
25 	!defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx) && \
26 	!defined(STM32L4Q5xx) && !defined(STM32L4P5xx)
27 #define STM32L4X_PAGE_SHIFT	11
28 #else
29 #define STM32L4X_PAGE_SHIFT	12
30 #endif
31 
32 #if defined(FLASH_OPTR_DUALBANK) || defined(FLASH_STM32_DBANK)
33 #define CONTROL_DCACHE
34 #endif
35 
flush_cache(FLASH_TypeDef * regs)36 static inline void flush_cache(FLASH_TypeDef *regs)
37 {
38 	if (regs->ACR & FLASH_ACR_DCEN) {
39 		regs->ACR &= ~FLASH_ACR_DCEN;
40 		/* Datasheet: DCRST: Data cache reset
41 		 * This bit can be written only when the data cache is disabled
42 		 */
43 		regs->ACR |= FLASH_ACR_DCRST;
44 		regs->ACR &= ~FLASH_ACR_DCRST;
45 		regs->ACR |= FLASH_ACR_DCEN;
46 	}
47 
48 	if (regs->ACR & FLASH_ACR_ICEN) {
49 		regs->ACR &= ~FLASH_ACR_ICEN;
50 		/* Datasheet: ICRST: Instruction cache reset :
51 		 * This bit can be written only when the instruction cache
52 		 * is disabled
53 		 */
54 		regs->ACR |= FLASH_ACR_ICRST;
55 		regs->ACR &= ~FLASH_ACR_ICRST;
56 		regs->ACR |= FLASH_ACR_ICEN;
57 	}
58 }
59 
60 /*
61  * STM32L4xx devices can have up to 512 2K pages on two 256x2K pages banks
62  *
63  * STM32L4R/Sxx devices can have up to 512 4K pages on two 256x4K pages banks
64  */
get_page(off_t offset)65 static unsigned int get_page(off_t offset)
66 {
67 	return offset >> STM32L4X_PAGE_SHIFT;
68 }
69 
write_dword(const struct device * dev,off_t offset,uint64_t val)70 static int write_dword(const struct device *dev, off_t offset, uint64_t val)
71 {
72 	volatile uint32_t *flash = (uint32_t *)(offset + FLASH_STM32_BASE_ADDRESS);
73 	FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
74 #ifdef CONTROL_DCACHE
75 	bool dcache_enabled = false;
76 #endif /* CONTROL_DCACHE */
77 	uint32_t tmp;
78 	int rc;
79 
80 	/* if the control register is locked, do not fail silently */
81 	if (regs->CR & FLASH_CR_LOCK) {
82 		return -EIO;
83 	}
84 
85 	/* Check that no Flash main memory operation is ongoing */
86 	rc = flash_stm32_wait_flash_idle(dev);
87 	if (rc < 0) {
88 		return rc;
89 	}
90 
91 	/* Check if this double word is erased and value isn't 0.
92 	 *
93 	 * It is allowed to write only zeros over an already written dword
94 	 * See 3.3.7 in reference manual.
95 	 */
96 	if ((flash[0] != 0xFFFFFFFFUL ||
97 	     flash[1] != 0xFFFFFFFFUL) && val != 0UL) {
98 		LOG_ERR("Word at offs %ld not erased", (long)offset);
99 		return -EIO;
100 	}
101 
102 #ifdef CONTROL_DCACHE
103 	/*
104 	 * Disable the data cache to avoid the silicon errata 2.2.3:
105 	 * "Data cache might be corrupted during Flash memory read-while-write operation"
106 	 */
107 	if (regs->ACR & FLASH_ACR_DCEN) {
108 		dcache_enabled = true;
109 		regs->ACR &= (~FLASH_ACR_DCEN);
110 	}
111 #endif /* CONTROL_DCACHE */
112 
113 	/* Set the PG bit */
114 	regs->CR |= FLASH_CR_PG;
115 
116 	/* Flush the register write */
117 	tmp = regs->CR;
118 
119 	/* Perform the data write operation at the desired memory address */
120 	flash[0] = (uint32_t)val;
121 	flash[1] = (uint32_t)(val >> 32);
122 
123 	/* Wait until the BSY bit is cleared */
124 	rc = flash_stm32_wait_flash_idle(dev);
125 
126 	/* Clear the PG bit */
127 	regs->CR &= (~FLASH_CR_PG);
128 
129 #ifdef CONTROL_DCACHE
130 	/* Reset/enable the data cache if previously enabled */
131 	if (dcache_enabled) {
132 		regs->ACR |= FLASH_ACR_DCRST;
133 		regs->ACR &= (~FLASH_ACR_DCRST);
134 		regs->ACR |= FLASH_ACR_DCEN;
135 	}
136 #endif /* CONTROL_DCACHE */
137 
138 	return rc;
139 }
140 
141 #define SOC_NV_FLASH_SIZE DT_REG_SIZE(DT_INST(0, soc_nv_flash))
142 
erase_page(const struct device * dev,unsigned int page)143 static int erase_page(const struct device *dev, unsigned int page)
144 {
145 	FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
146 	uint32_t tmp;
147 	uint16_t pages_per_bank;
148 	int rc;
149 
150 #if !defined(FLASH_OPTR_DUALBANK) && !defined(FLASH_STM32_DBANK)
151 	/* Single bank device. Each page is of 2KB size */
152 	pages_per_bank = SOC_NV_FLASH_SIZE >> 11;
153 #elif defined(FLASH_OPTR_DUALBANK)
154 	/* L4 series (2K page size) with configurable Dual Bank (default y) */
155 	/* Dual Bank is only option for 1M devices */
156 	if ((regs->OPTR & FLASH_OPTR_DUALBANK) ||
157 	    (SOC_NV_FLASH_SIZE == (1024*1024))) {
158 		/* Dual Bank configuration (nbr pages = flash size / 2 / 2K) */
159 		pages_per_bank = SOC_NV_FLASH_SIZE >> 12;
160 	} else {
161 		/* Single bank configuration. This has not been validated. */
162 		/* Not supported for now. */
163 		return -ENOTSUP;
164 	}
165 #elif defined(FLASH_STM32_DBANK)
166 	/* L4+ series (4K page size) with configurable Dual Bank (default y)*/
167 	if (regs->OPTR & FLASH_STM32_DBANK) {
168 		/* Dual Bank configuration (nbre pags = flash size / 2 / 4K) */
169 		pages_per_bank = SOC_NV_FLASH_SIZE >> 13;
170 	} else {
171 		/* Single bank configuration */
172 		/* Requires 128 bytes data read. This config is not supported */
173 		return -ENOTSUP;
174 	}
175 #endif
176 
177 	/* if the control register is locked, do not fail silently */
178 	if (regs->CR & FLASH_CR_LOCK) {
179 		return -EIO;
180 	}
181 
182 	/* Check that no Flash memory operation is ongoing */
183 	rc = flash_stm32_wait_flash_idle(dev);
184 	if (rc < 0) {
185 		return rc;
186 	}
187 
188 	flush_cache(regs);
189 
190 	/* Set the PER bit and select the page you wish to erase */
191 	regs->CR |= FLASH_CR_PER;
192 #ifdef FLASH_CR_BKER
193 	regs->CR &= ~FLASH_CR_BKER_Msk;
194 	/* Select bank, only for DUALBANK devices */
195 	if (page >= pages_per_bank)
196 		regs->CR |= FLASH_CR_BKER;
197 #endif
198 	regs->CR &= ~FLASH_CR_PNB_Msk;
199 	regs->CR |= ((page % pages_per_bank) << 3);
200 
201 	/* Set the STRT bit */
202 	regs->CR |= FLASH_CR_STRT;
203 
204 	/* flush the register write */
205 	tmp = regs->CR;
206 
207 	/* Wait for the BSY bit */
208 	rc = flash_stm32_wait_flash_idle(dev);
209 
210 	regs->CR &= ~FLASH_CR_PER;
211 
212 	return rc;
213 }
214 
flash_stm32_block_erase_loop(const struct device * dev,unsigned int offset,unsigned int len)215 int flash_stm32_block_erase_loop(const struct device *dev,
216 				 unsigned int offset,
217 				 unsigned int len)
218 {
219 	int i, rc = 0;
220 
221 	i = get_page(offset);
222 	for (; i <= get_page(offset + len - 1) ; ++i) {
223 		rc = erase_page(dev, i);
224 		if (rc < 0) {
225 			break;
226 		}
227 	}
228 
229 	return rc;
230 }
231 
flash_stm32_write_range(const struct device * dev,unsigned int offset,const void * data,unsigned int len)232 int flash_stm32_write_range(const struct device *dev, unsigned int offset,
233 			    const void *data, unsigned int len)
234 {
235 	int i, rc = 0;
236 
237 	for (i = 0; i < len; i += 8, offset += 8U) {
238 		rc = write_dword(dev, offset,
239 				UNALIGNED_GET((const uint64_t *) data + (i >> 3)));
240 		if (rc < 0) {
241 			return rc;
242 		}
243 	}
244 
245 	return rc;
246 }
247 
write_optb(const struct device * dev,uint32_t mask,uint32_t value)248 static __unused int write_optb(const struct device *dev, uint32_t mask,
249 			       uint32_t value)
250 {
251 	FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
252 	int rc;
253 
254 	if (regs->CR & FLASH_CR_OPTLOCK) {
255 		return -EIO;
256 	}
257 
258 	if ((regs->OPTR & mask) == value) {
259 		return 0;
260 	}
261 
262 	rc = flash_stm32_wait_flash_idle(dev);
263 	if (rc < 0) {
264 		return rc;
265 	}
266 
267 	regs->OPTR = (regs->OPTR & ~mask) | value;
268 	regs->CR |= FLASH_CR_OPTSTRT;
269 
270 	/* Make sure previous write is completed. */
271 	barrier_dsync_fence_full();
272 
273 	rc = flash_stm32_wait_flash_idle(dev);
274 	if (rc < 0) {
275 		return rc;
276 	}
277 
278 	return 0;
279 }
280 
281 #if defined(CONFIG_FLASH_STM32_WRITE_PROTECT)
282 
283 /*
284  * Remark for future development implementing Write Protection for the L4 parts:
285  *
286  * STM32L4 allows for 2 write protected memory areas, c.f. FLASH_WEP1AR, FLASH_WRP1BR
287  * which are defined by their start and end pages.
288  *
289  * Other STM32 parts (i.e. F4 series) uses bitmask to select sectors.
290  *
291  * To implement Write Protection for L4 one should thus add a new EX_OP like
292  * FLASH_STM32_EX_OP_SECTOR_WP_RANGED in stm32_flash_api_extensions.h
293  */
294 
295 #endif /* CONFIG_FLASH_STM32_WRITE_PROTECT */
296 
297 #if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION)
flash_stm32_update_rdp(const struct device * dev,bool enable,bool permanent)298 int flash_stm32_update_rdp(const struct device *dev, bool enable,
299 			   bool permanent)
300 {
301 	FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
302 	uint8_t current_level, target_level;
303 
304 	current_level =
305 		(regs->OPTR & FLASH_OPTR_RDP_Msk) >> FLASH_OPTR_RDP_Pos;
306 	target_level = current_level;
307 
308 	/*
309 	 * 0xAA = RDP level 0 (no protection)
310 	 * 0xCC = RDP level 2 (permanent protection)
311 	 * others = RDP level 1 (protection active)
312 	 */
313 	switch (current_level) {
314 	case FLASH_STM32_RDP2:
315 		if (!enable || !permanent) {
316 			LOG_ERR("RDP level 2 is permanent and can't be changed!");
317 			return -ENOTSUP;
318 		}
319 		break;
320 	case FLASH_STM32_RDP0:
321 		if (enable) {
322 			target_level = FLASH_STM32_RDP1;
323 			if (permanent) {
324 #if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION_PERMANENT_ALLOW)
325 				target_level = FLASH_STM32_RDP2;
326 #else
327 				LOG_ERR("Permanent readout protection (RDP "
328 					"level 0 -> 2) not allowed");
329 				return -ENOTSUP;
330 #endif
331 			}
332 		}
333 		break;
334 	default: /* FLASH_STM32_RDP1 */
335 		if (enable && permanent) {
336 #if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION_PERMANENT_ALLOW)
337 			target_level = FLASH_STM32_RDP2;
338 #else
339 			LOG_ERR("Permanent readout protection (RDP "
340 				"level 1 -> 2) not allowed");
341 			return -ENOTSUP;
342 #endif
343 		}
344 		if (!enable) {
345 #if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION_DISABLE_ALLOW)
346 			target_level = FLASH_STM32_RDP0;
347 #else
348 			LOG_ERR("Disabling readout protection (RDP "
349 				"level 1 -> 0) not allowed");
350 			return -EACCES;
351 #endif
352 		}
353 	}
354 
355 	/* Update RDP level if needed */
356 	if (current_level != target_level) {
357 		LOG_INF("RDP changed from 0x%02x to 0x%02x", current_level,
358 			target_level);
359 
360 		write_optb(dev, FLASH_OPTR_RDP_Msk,
361 			   (uint32_t)target_level << FLASH_OPTR_RDP_Pos);
362 	}
363 	return 0;
364 }
365 
flash_stm32_get_rdp(const struct device * dev,bool * enabled,bool * permanent)366 int flash_stm32_get_rdp(const struct device *dev, bool *enabled,
367 			bool *permanent)
368 {
369 	FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
370 	uint8_t current_level;
371 
372 	current_level =
373 		(regs->OPTR & FLASH_OPTR_RDP_Msk) >> FLASH_OPTR_RDP_Pos;
374 
375 	/*
376 	 * 0xAA = RDP level 0 (no protection)
377 	 * 0xCC = RDP level 2 (permanent protection)
378 	 * others = RDP level 1 (protection active)
379 	 */
380 	switch (current_level) {
381 	case FLASH_STM32_RDP2:
382 		*enabled = true;
383 		*permanent = true;
384 		break;
385 	case FLASH_STM32_RDP0:
386 		*enabled = false;
387 		*permanent = false;
388 		break;
389 	default: /* FLASH_STM32_RDP1 */
390 		*enabled = true;
391 		*permanent = false;
392 	}
393 	return 0;
394 }
395 #endif /* CONFIG_FLASH_STM32_READOUT_PROTECTION */
396 
397 
398 
flash_stm32_page_layout(const struct device * dev,const struct flash_pages_layout ** layout,size_t * layout_size)399 void flash_stm32_page_layout(const struct device *dev,
400 			     const struct flash_pages_layout **layout,
401 			     size_t *layout_size)
402 {
403 	static struct flash_pages_layout stm32l4_flash_layout = {
404 		.pages_count = 0,
405 		.pages_size = 0,
406 	};
407 
408 	ARG_UNUSED(dev);
409 
410 	if (stm32l4_flash_layout.pages_count == 0) {
411 		stm32l4_flash_layout.pages_count = FLASH_SIZE / FLASH_PAGE_SIZE;
412 		stm32l4_flash_layout.pages_size = FLASH_PAGE_SIZE;
413 	}
414 
415 	*layout = &stm32l4_flash_layout;
416 	*layout_size = 1;
417 }
418