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