1 /*
2  * Copyright (c) 2020 Piotr Mienkowski
3  * Copyright (c) 2020 Linaro Limited
4  * Copyright (c) 2022 Georgij Cernysiov
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define DT_DRV_COMPAT st_stm32_qspi_nor
10 
11 #include <errno.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/toolchain.h>
14 #include <zephyr/arch/common/ffs.h>
15 #include <zephyr/sys/__assert.h>
16 #include <zephyr/sys/util.h>
17 #include <soc.h>
18 #include <string.h>
19 #include <zephyr/drivers/pinctrl.h>
20 #include <zephyr/drivers/clock_control/stm32_clock_control.h>
21 #include <zephyr/drivers/clock_control.h>
22 #include <zephyr/drivers/flash.h>
23 #include <zephyr/drivers/dma.h>
24 #include <zephyr/drivers/dma/dma_stm32.h>
25 #include <zephyr/drivers/gpio.h>
26 
27 #if DT_INST_NODE_HAS_PROP(0, spi_bus_width) && \
28 	DT_INST_PROP(0, spi_bus_width) == 4
29 #define STM32_QSPI_USE_QUAD_IO 1
30 #else
31 #define STM32_QSPI_USE_QUAD_IO 0
32 #endif
33 
34 /* Get the base address of the flash from the DTS node */
35 #define STM32_QSPI_BASE_ADDRESS DT_INST_REG_ADDR(0)
36 
37 #define STM32_QSPI_RESET_GPIO DT_INST_NODE_HAS_PROP(0, reset_gpios)
38 #define STM32_QSPI_RESET_CMD  DT_INST_PROP(0, reset_cmd)
39 
40 #include <stm32_ll_dma.h>
41 
42 #include "spi_nor.h"
43 #include "jesd216.h"
44 
45 #include <zephyr/logging/log.h>
46 #include <zephyr/irq.h>
47 LOG_MODULE_REGISTER(flash_stm32_qspi, CONFIG_FLASH_LOG_LEVEL);
48 
49 #define STM32_QSPI_FIFO_THRESHOLD         8
50 #define STM32_QSPI_CLOCK_PRESCALER_MAX  255
51 
52 #define STM32_QSPI_UNKNOWN_MODE (0xFF)
53 
54 #define STM32_QSPI_USE_DMA DT_NODE_HAS_PROP(DT_INST_PARENT(0), dmas)
55 
56 #if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_qspi_nor)
57 
58 /* In dual-flash mode, total size is twice the size of one flash component */
59 #define STM32_QSPI_DOUBLE_FLASH	DT_PROP(DT_NODELABEL(quadspi), dual_flash)
60 
61 #if STM32_QSPI_USE_DMA
62 static const uint32_t table_m_size[] = {
63 	LL_DMA_MDATAALIGN_BYTE,
64 	LL_DMA_MDATAALIGN_HALFWORD,
65 	LL_DMA_MDATAALIGN_WORD,
66 };
67 
68 static const uint32_t table_p_size[] = {
69 	LL_DMA_PDATAALIGN_BYTE,
70 	LL_DMA_PDATAALIGN_HALFWORD,
71 	LL_DMA_PDATAALIGN_WORD,
72 };
73 
74 /* Lookup table to set dma priority from the DTS */
75 static const uint32_t table_priority[] = {
76 	DMA_PRIORITY_LOW,
77 	DMA_PRIORITY_MEDIUM,
78 	DMA_PRIORITY_HIGH,
79 	DMA_PRIORITY_VERY_HIGH,
80 };
81 #endif /* STM32_QSPI_USE_DMA */
82 
83 typedef void (*irq_config_func_t)(const struct device *dev);
84 
85 struct stream {
86 	DMA_TypeDef *reg;
87 	const struct device *dev;
88 	uint32_t channel;
89 	struct dma_config cfg;
90 };
91 
92 struct flash_stm32_qspi_config {
93 	QUADSPI_TypeDef *regs;
94 	struct stm32_pclken pclken;
95 	irq_config_func_t irq_config;
96 	size_t flash_size;
97 	uint32_t max_frequency;
98 	const struct pinctrl_dev_config *pcfg;
99 #if STM32_QSPI_RESET_GPIO
100 	const struct gpio_dt_spec reset;
101 #endif
102 #if DT_NODE_HAS_PROP(DT_INST(0, st_stm32_qspi_nor), jedec_id)
103 	uint8_t jedec_id[DT_INST_PROP_LEN(0, jedec_id)];
104 #endif /* jedec_id */
105 };
106 
107 struct flash_stm32_qspi_data {
108 	QSPI_HandleTypeDef hqspi;
109 	struct k_sem sem;
110 	struct k_sem sync;
111 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
112 	struct flash_pages_layout layout;
113 #endif
114 	struct jesd216_erase_type erase_types[JESD216_NUM_ERASE_TYPES];
115 	/* Number of bytes per page */
116 	uint16_t page_size;
117 	enum jesd216_dw15_qer_type qer_type;
118 	enum jesd216_mode_type mode;
119 	int cmd_status;
120 	struct stream dma;
121 	uint8_t qspi_write_cmd;
122 	uint8_t qspi_read_cmd;
123 	uint8_t qspi_read_cmd_latency;
124 	/*
125 	 * If set addressed operations should use 32-bit rather than
126 	 * 24-bit addresses.
127 	 */
128 	bool flag_access_32bit: 1;
129 };
130 
131 static const QSPI_CommandTypeDef cmd_write_en = {
132 	.Instruction = SPI_NOR_CMD_WREN,
133 	.InstructionMode = QSPI_INSTRUCTION_1_LINE
134 };
135 
qspi_lock_thread(const struct device * dev)136 static inline void qspi_lock_thread(const struct device *dev)
137 {
138 	struct flash_stm32_qspi_data *dev_data = dev->data;
139 
140 	k_sem_take(&dev_data->sem, K_FOREVER);
141 }
142 
qspi_unlock_thread(const struct device * dev)143 static inline void qspi_unlock_thread(const struct device *dev)
144 {
145 	struct flash_stm32_qspi_data *dev_data = dev->data;
146 
147 	k_sem_give(&dev_data->sem);
148 }
149 
qspi_set_address_size(const struct device * dev,QSPI_CommandTypeDef * cmd)150 static inline void qspi_set_address_size(const struct device *dev,
151 					 QSPI_CommandTypeDef *cmd)
152 {
153 	struct flash_stm32_qspi_data *dev_data = dev->data;
154 
155 	if (dev_data->flag_access_32bit) {
156 		cmd->AddressSize = QSPI_ADDRESS_32_BITS;
157 		return;
158 	}
159 
160 	cmd->AddressSize = QSPI_ADDRESS_24_BITS;
161 }
162 
qspi_prepare_quad_read(const struct device * dev,QSPI_CommandTypeDef * cmd)163 static inline int qspi_prepare_quad_read(const struct device *dev,
164 					  QSPI_CommandTypeDef *cmd)
165 {
166 	struct flash_stm32_qspi_data *dev_data = dev->data;
167 
168 	__ASSERT_NO_MSG(dev_data->mode == JESD216_MODE_114 ||
169 			dev_data->mode == JESD216_MODE_144);
170 
171 	cmd->Instruction = dev_data->qspi_read_cmd;
172 	cmd->AddressMode = ((dev_data->mode == JESD216_MODE_114)
173 				? QSPI_ADDRESS_1_LINE
174 				: QSPI_ADDRESS_4_LINES);
175 	cmd->DataMode = QSPI_DATA_4_LINES;
176 	cmd->DummyCycles = dev_data->qspi_read_cmd_latency;
177 
178 	return 0;
179 }
180 
qspi_prepare_quad_program(const struct device * dev,QSPI_CommandTypeDef * cmd)181 static inline int qspi_prepare_quad_program(const struct device *dev,
182 					     QSPI_CommandTypeDef *cmd)
183 {
184 	struct flash_stm32_qspi_data *dev_data = dev->data;
185 
186 	__ASSERT_NO_MSG(dev_data->qspi_write_cmd == SPI_NOR_CMD_PP_1_1_4 ||
187 			dev_data->qspi_write_cmd == SPI_NOR_CMD_PP_1_4_4);
188 
189 	cmd->Instruction = dev_data->qspi_write_cmd;
190 #if defined(CONFIG_USE_MICROCHIP_QSPI_FLASH_WITH_STM32)
191 	/* Microchip qspi-NOR flash, does not follow the standard rules */
192 	if (cmd->Instruction == SPI_NOR_CMD_PP_1_1_4) {
193 		cmd->AddressMode = QSPI_ADDRESS_4_LINES;
194 	}
195 #else
196 	cmd->AddressMode = ((cmd->Instruction == SPI_NOR_CMD_PP_1_1_4)
197 				? QSPI_ADDRESS_1_LINE
198 				: QSPI_ADDRESS_4_LINES);
199 #endif /* CONFIG_USE_MICROCHIP_QSPI_FLASH_WITH_STM32 */
200 	cmd->DataMode = QSPI_DATA_4_LINES;
201 	cmd->DummyCycles = 0;
202 
203 	return 0;
204 }
205 
206 /*
207  * Send a command over QSPI bus.
208  */
qspi_send_cmd(const struct device * dev,const QSPI_CommandTypeDef * cmd)209 static int qspi_send_cmd(const struct device *dev, const QSPI_CommandTypeDef *cmd)
210 {
211 	const struct flash_stm32_qspi_config *dev_cfg = dev->config;
212 	struct flash_stm32_qspi_data *dev_data = dev->data;
213 	HAL_StatusTypeDef hal_ret;
214 
215 	ARG_UNUSED(dev_cfg);
216 
217 	LOG_DBG("Instruction 0x%x", cmd->Instruction);
218 
219 	dev_data->cmd_status = 0;
220 
221 	hal_ret = HAL_QSPI_Command_IT(&dev_data->hqspi, (QSPI_CommandTypeDef *)cmd);
222 	if (hal_ret != HAL_OK) {
223 		LOG_ERR("%d: Failed to send QSPI instruction", hal_ret);
224 		return -EIO;
225 	}
226 	LOG_DBG("CCR 0x%x", dev_cfg->regs->CCR);
227 
228 	k_sem_take(&dev_data->sync, K_FOREVER);
229 
230 	return dev_data->cmd_status;
231 }
232 
233 /*
234  * Perform a read access over QSPI bus.
235  */
qspi_read_access(const struct device * dev,QSPI_CommandTypeDef * cmd,uint8_t * data,size_t size)236 static int qspi_read_access(const struct device *dev, QSPI_CommandTypeDef *cmd,
237 			    uint8_t *data, size_t size)
238 {
239 	const struct flash_stm32_qspi_config *dev_cfg = dev->config;
240 	struct flash_stm32_qspi_data *dev_data = dev->data;
241 	HAL_StatusTypeDef hal_ret;
242 
243 	ARG_UNUSED(dev_cfg);
244 
245 	cmd->NbData = size;
246 
247 	dev_data->cmd_status = 0;
248 
249 	hal_ret = HAL_QSPI_Command_IT(&dev_data->hqspi, cmd);
250 	if (hal_ret != HAL_OK) {
251 		LOG_ERR("%d: Failed to send QSPI instruction", hal_ret);
252 		return -EIO;
253 	}
254 
255 #if STM32_QSPI_USE_DMA
256 	hal_ret = HAL_QSPI_Receive_DMA(&dev_data->hqspi, data);
257 #else
258 	hal_ret = HAL_QSPI_Receive_IT(&dev_data->hqspi, data);
259 #endif
260 	if (hal_ret != HAL_OK) {
261 		LOG_ERR("%d: Failed to read data", hal_ret);
262 		return -EIO;
263 	}
264 
265 	k_sem_take(&dev_data->sync, K_FOREVER);
266 
267 	return dev_data->cmd_status;
268 }
269 
270 /*
271  * Perform a write access over QSPI bus.
272  */
qspi_write_access(const struct device * dev,QSPI_CommandTypeDef * cmd,const uint8_t * data,size_t size)273 static int qspi_write_access(const struct device *dev, QSPI_CommandTypeDef *cmd,
274 			     const uint8_t *data, size_t size)
275 {
276 	const struct flash_stm32_qspi_config *dev_cfg = dev->config;
277 	struct flash_stm32_qspi_data *dev_data = dev->data;
278 	HAL_StatusTypeDef hal_ret;
279 
280 	ARG_UNUSED(dev_cfg);
281 
282 	LOG_DBG("Instruction 0x%x", cmd->Instruction);
283 
284 	cmd->NbData = size;
285 
286 	dev_data->cmd_status = 0;
287 
288 	hal_ret = HAL_QSPI_Command_IT(&dev_data->hqspi, cmd);
289 	if (hal_ret != HAL_OK) {
290 		LOG_ERR("%d: Failed to send QSPI instruction", hal_ret);
291 		return -EIO;
292 	}
293 
294 #if STM32_QSPI_USE_DMA
295 	hal_ret = HAL_QSPI_Transmit_DMA(&dev_data->hqspi, (uint8_t *)data);
296 #else
297 	hal_ret = HAL_QSPI_Transmit_IT(&dev_data->hqspi, (uint8_t *)data);
298 #endif
299 	if (hal_ret != HAL_OK) {
300 		LOG_ERR("%d: Failed to read data", hal_ret);
301 		return -EIO;
302 	}
303 	LOG_DBG("CCR 0x%x", dev_cfg->regs->CCR);
304 
305 	k_sem_take(&dev_data->sync, K_FOREVER);
306 
307 	return dev_data->cmd_status;
308 }
309 
310 #if defined(CONFIG_FLASH_JESD216_API)
311 /*
312  * Read Serial Flash ID :
313  * perform a read access over SPI bus for read Identification (DataMode is already set)
314  * and compare to the jedec-id from the DTYS table exists
315  */
qspi_read_jedec_id(const struct device * dev,uint8_t * id)316 static int qspi_read_jedec_id(const struct device *dev, uint8_t *id)
317 {
318 	struct flash_stm32_qspi_data *dev_data = dev->data;
319 	uint8_t data[JESD216_READ_ID_LEN];
320 
321 	QSPI_CommandTypeDef cmd = {
322 		.Instruction = JESD216_CMD_READ_ID,
323 		.AddressSize = QSPI_ADDRESS_NONE,
324 		.DummyCycles = 8,
325 		.InstructionMode = QSPI_INSTRUCTION_1_LINE,
326 		.AddressMode = QSPI_ADDRESS_1_LINE,
327 		.DataMode = QSPI_DATA_1_LINE,
328 		.NbData = JESD216_READ_ID_LEN,
329 	};
330 
331 	HAL_StatusTypeDef hal_ret;
332 
333 	hal_ret = HAL_QSPI_Command_IT(&dev_data->hqspi, &cmd);
334 
335 	if (hal_ret != HAL_OK) {
336 		LOG_ERR("%d: Failed to send OSPI instruction", hal_ret);
337 		return -EIO;
338 	}
339 
340 	hal_ret = HAL_QSPI_Receive(&dev_data->hqspi, data, HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
341 	if (hal_ret != HAL_OK) {
342 		LOG_ERR("%d: Failed to read data", hal_ret);
343 		return -EIO;
344 	}
345 
346 	LOG_DBG("Read JESD216-ID");
347 
348 	dev_data->cmd_status = 0;
349 	memcpy(id, data, JESD216_READ_ID_LEN);
350 
351 	return 0;
352 }
353 #endif /* CONFIG_FLASH_JESD216_API */
354 
qspi_write_unprotect(const struct device * dev)355 static int qspi_write_unprotect(const struct device *dev)
356 {
357 	int ret = 0;
358 	QSPI_CommandTypeDef cmd_unprotect = {
359 			.Instruction = SPI_NOR_CMD_ULBPR,
360 			.InstructionMode = QSPI_INSTRUCTION_1_LINE,
361 	};
362 
363 	if (IS_ENABLED(DT_INST_PROP(0, requires_ulbpr))) {
364 		ret = qspi_send_cmd(dev, &cmd_write_en);
365 
366 		if (ret != 0) {
367 			return ret;
368 		}
369 
370 		ret = qspi_send_cmd(dev, &cmd_unprotect);
371 	}
372 
373 	return ret;
374 }
375 
376 /*
377  * Read Serial Flash Discovery Parameter
378  */
qspi_read_sfdp(const struct device * dev,off_t addr,void * data,size_t size)379 static int qspi_read_sfdp(const struct device *dev, off_t addr, void *data,
380 			  size_t size)
381 {
382 	struct flash_stm32_qspi_data *dev_data = dev->data;
383 	HAL_StatusTypeDef hal_ret;
384 
385 	__ASSERT(data != NULL, "null destination");
386 
387 	LOG_INF("Reading SFDP");
388 
389 	QSPI_CommandTypeDef cmd = {
390 		.Instruction = JESD216_CMD_READ_SFDP,
391 		.Address = addr,
392 		.AddressSize = QSPI_ADDRESS_24_BITS,
393 		.DummyCycles = 8,
394 		.InstructionMode = QSPI_INSTRUCTION_1_LINE,
395 		.AddressMode = QSPI_ADDRESS_1_LINE,
396 		.DataMode = QSPI_DATA_1_LINE,
397 		.NbData = size,
398 	};
399 
400 	hal_ret = HAL_QSPI_Command(&dev_data->hqspi, &cmd,
401 				   HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
402 	if (hal_ret != HAL_OK) {
403 		LOG_ERR("%d: Failed to send SFDP instruction", hal_ret);
404 		return -EIO;
405 	}
406 
407 	hal_ret = HAL_QSPI_Receive(&dev_data->hqspi, (uint8_t *)data,
408 				   HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
409 	if (hal_ret != HAL_OK) {
410 		LOG_ERR("%d: Failed to read SFDP", hal_ret);
411 		return -EIO;
412 	}
413 
414 	dev_data->cmd_status = 0;
415 
416 	return 0;
417 }
418 
qspi_address_is_valid(const struct device * dev,off_t addr,size_t size)419 static bool qspi_address_is_valid(const struct device *dev, off_t addr,
420 				  size_t size)
421 {
422 	const struct flash_stm32_qspi_config *dev_cfg = dev->config;
423 	size_t flash_size = dev_cfg->flash_size;
424 
425 	return (addr >= 0) && ((uint64_t)addr + (uint64_t)size <= flash_size);
426 }
427 
428 #ifdef CONFIG_STM32_MEMMAP
429 /* Must be called inside qspi_lock_thread(). */
stm32_qspi_set_memory_mapped(const struct device * dev)430 static int stm32_qspi_set_memory_mapped(const struct device *dev)
431 {
432 	int ret;
433 	HAL_StatusTypeDef hal_ret;
434 	struct flash_stm32_qspi_data *dev_data = dev->data;
435 
436 	QSPI_CommandTypeDef cmd = {
437 		.Instruction = SPI_NOR_CMD_READ,
438 		.Address = 0,
439 		.InstructionMode = QSPI_INSTRUCTION_1_LINE,
440 		.AddressMode = QSPI_ADDRESS_1_LINE,
441 		.DataMode = QSPI_DATA_1_LINE,
442 	};
443 
444 	qspi_set_address_size(dev, &cmd);
445 	if (IS_ENABLED(STM32_QSPI_USE_QUAD_IO)) {
446 		ret = qspi_prepare_quad_read(dev, &cmd);
447 		if (ret < 0) {
448 			return ret;
449 		}
450 	}
451 
452 	QSPI_MemoryMappedTypeDef mem_mapped = {
453 		.TimeOutActivation = QSPI_TIMEOUT_COUNTER_DISABLE,
454 	};
455 
456 	hal_ret = HAL_QSPI_MemoryMapped(&dev_data->hqspi, &cmd, &mem_mapped);
457 	if (hal_ret != 0) {
458 		LOG_ERR("%d: Failed to enable memory mapped", hal_ret);
459 		return -EIO;
460 	}
461 
462 	LOG_DBG("MemoryMap mode enabled");
463 	return 0;
464 }
465 
stm32_qspi_is_memory_mapped(const struct device * dev)466 static bool stm32_qspi_is_memory_mapped(const struct device *dev)
467 {
468 	struct flash_stm32_qspi_data *dev_data = dev->data;
469 
470 	return READ_BIT(dev_data->hqspi.Instance->CCR, QUADSPI_CCR_FMODE) == QUADSPI_CCR_FMODE;
471 }
472 
stm32_qspi_abort(const struct device * dev)473 static int stm32_qspi_abort(const struct device *dev)
474 {
475 	struct flash_stm32_qspi_data *dev_data = dev->data;
476 	HAL_StatusTypeDef hal_ret;
477 
478 	hal_ret = HAL_QSPI_Abort(&dev_data->hqspi);
479 	if (hal_ret != HAL_OK) {
480 		LOG_ERR("%d: QSPI abort failed", hal_ret);
481 		return -EIO;
482 	}
483 
484 	return 0;
485 }
486 #endif
487 
flash_stm32_qspi_read(const struct device * dev,off_t addr,void * data,size_t size)488 static int flash_stm32_qspi_read(const struct device *dev, off_t addr,
489 				 void *data, size_t size)
490 {
491 	int ret;
492 
493 	if (!qspi_address_is_valid(dev, addr, size)) {
494 		LOG_DBG("Error: address or size exceeds expected values: "
495 			"addr 0x%lx, size %zu", (long)addr, size);
496 		return -EINVAL;
497 	}
498 
499 	/* read non-zero size */
500 	if (size == 0) {
501 		return 0;
502 	}
503 
504 #ifdef CONFIG_STM32_MEMMAP
505 	qspi_lock_thread(dev);
506 
507 	/* Do reads through memory-mapping instead of indirect */
508 	if (!stm32_qspi_is_memory_mapped(dev)) {
509 		ret = stm32_qspi_set_memory_mapped(dev);
510 		if (ret != 0) {
511 			LOG_ERR("READ: failed to set memory mapped");
512 			goto end;
513 		}
514 	}
515 
516 	__ASSERT_NO_MSG(stm32_qspi_is_memory_mapped(dev));
517 
518 	uintptr_t mmap_addr = STM32_QSPI_BASE_ADDRESS + addr;
519 
520 	LOG_DBG("Memory-mapped read from 0x%08lx, len %zu", mmap_addr, size);
521 	memcpy(data, (void *)mmap_addr, size);
522 	ret = 0;
523 	goto end;
524 #else
525 	QSPI_CommandTypeDef cmd = {
526 		.Instruction = SPI_NOR_CMD_READ,
527 		.Address = addr,
528 		.InstructionMode = QSPI_INSTRUCTION_1_LINE,
529 		.AddressMode = QSPI_ADDRESS_1_LINE,
530 		.DataMode = QSPI_DATA_1_LINE,
531 	};
532 
533 	qspi_set_address_size(dev, &cmd);
534 	if (IS_ENABLED(STM32_QSPI_USE_QUAD_IO)) {
535 		ret = qspi_prepare_quad_read(dev, &cmd);
536 		if (ret < 0) {
537 			return ret;
538 		}
539 	}
540 
541 	qspi_lock_thread(dev);
542 
543 	ret = qspi_read_access(dev, &cmd, data, size);
544 	goto end;
545 #endif
546 
547 end:
548 	qspi_unlock_thread(dev);
549 
550 	return ret;
551 }
552 
qspi_wait_until_ready(const struct device * dev)553 static int qspi_wait_until_ready(const struct device *dev)
554 {
555 	uint8_t reg;
556 	int ret;
557 
558 	QSPI_CommandTypeDef cmd = {
559 		.Instruction = SPI_NOR_CMD_RDSR,
560 		.InstructionMode = QSPI_INSTRUCTION_1_LINE,
561 		.DataMode = QSPI_DATA_1_LINE,
562 	};
563 
564 	do {
565 		ret = qspi_read_access(dev, &cmd, &reg, sizeof(reg));
566 	} while (!ret && (reg & SPI_NOR_WIP_BIT));
567 
568 	return ret;
569 }
570 
flash_stm32_qspi_write(const struct device * dev,off_t addr,const void * data,size_t size)571 static int flash_stm32_qspi_write(const struct device *dev, off_t addr,
572 				  const void *data, size_t size)
573 {
574 	int ret = 0;
575 
576 	if (!qspi_address_is_valid(dev, addr, size)) {
577 		LOG_DBG("Error: address or size exceeds expected values: "
578 			"addr 0x%lx, size %zu", (long)addr, size);
579 		return -EINVAL;
580 	}
581 
582 	/* write non-zero size */
583 	if (size == 0) {
584 		return 0;
585 	}
586 
587 	QSPI_CommandTypeDef cmd_pp = {
588 		.Instruction = SPI_NOR_CMD_PP,
589 		.InstructionMode = QSPI_INSTRUCTION_1_LINE,
590 		.AddressMode = QSPI_ADDRESS_1_LINE,
591 		.DataMode = QSPI_DATA_1_LINE,
592 	};
593 
594 	qspi_set_address_size(dev, &cmd_pp);
595 	if (IS_ENABLED(STM32_QSPI_USE_QUAD_IO)) {
596 		ret = qspi_prepare_quad_program(dev, &cmd_pp);
597 		if (ret < 0) {
598 			return ret;
599 		}
600 	}
601 
602 	qspi_lock_thread(dev);
603 
604 #ifdef CONFIG_STM32_MEMMAP
605 	if (stm32_qspi_is_memory_mapped(dev)) {
606 		/* Abort ongoing transfer to force CS high/BUSY deasserted */
607 		ret = stm32_qspi_abort(dev);
608 		if (ret != 0) {
609 			LOG_ERR("Failed to abort memory-mapped access before write");
610 			goto end;
611 		}
612 	}
613 #endif
614 
615 	while (size > 0) {
616 		size_t to_write = size;
617 
618 		/* Don't write more than a page. */
619 		if (to_write >= SPI_NOR_PAGE_SIZE) {
620 			to_write = SPI_NOR_PAGE_SIZE;
621 		}
622 
623 		/* Don't write across a page boundary */
624 		if (((addr + to_write - 1U) / SPI_NOR_PAGE_SIZE)
625 		    != (addr / SPI_NOR_PAGE_SIZE)) {
626 			to_write = SPI_NOR_PAGE_SIZE -
627 						(addr % SPI_NOR_PAGE_SIZE);
628 		}
629 
630 		ret = qspi_send_cmd(dev, &cmd_write_en);
631 		if (ret != 0) {
632 			break;
633 		}
634 
635 		cmd_pp.Address = addr;
636 		ret = qspi_write_access(dev, &cmd_pp, data, to_write);
637 		if (ret != 0) {
638 			break;
639 		}
640 
641 		size -= to_write;
642 		data = (const uint8_t *)data + to_write;
643 		addr += to_write;
644 
645 		ret = qspi_wait_until_ready(dev);
646 		if (ret != 0) {
647 			break;
648 		}
649 	}
650 	goto end;
651 
652 end:
653 	qspi_unlock_thread(dev);
654 
655 	return ret;
656 }
657 
flash_stm32_qspi_erase(const struct device * dev,off_t addr,size_t size)658 static int flash_stm32_qspi_erase(const struct device *dev, off_t addr,
659 				  size_t size)
660 {
661 	const struct flash_stm32_qspi_config *dev_cfg = dev->config;
662 	struct flash_stm32_qspi_data *dev_data = dev->data;
663 	int ret = 0;
664 
665 	if (!qspi_address_is_valid(dev, addr, size)) {
666 		LOG_DBG("Error: address or size exceeds expected values: "
667 			"addr 0x%lx, size %zu", (long)addr, size);
668 		return -EINVAL;
669 	}
670 
671 	/* erase non-zero size */
672 	if (size == 0) {
673 		return 0;
674 	}
675 
676 	QSPI_CommandTypeDef cmd_erase = {
677 		.Instruction = 0,
678 		.InstructionMode = QSPI_INSTRUCTION_1_LINE,
679 		.AddressMode = QSPI_ADDRESS_1_LINE,
680 	};
681 
682 	qspi_set_address_size(dev, &cmd_erase);
683 	qspi_lock_thread(dev);
684 
685 #ifdef CONFIG_STM32_MEMMAP
686 	if (stm32_qspi_is_memory_mapped(dev)) {
687 		/* Abort ongoing transfer to force CS high/BUSY deasserted */
688 		ret = stm32_qspi_abort(dev);
689 		if (ret != 0) {
690 			LOG_ERR("Failed to abort memory-mapped access before erase");
691 			goto end;
692 		}
693 	}
694 #endif
695 
696 	while ((size > 0) && (ret == 0)) {
697 		cmd_erase.Address = addr;
698 		qspi_send_cmd(dev, &cmd_write_en);
699 
700 		if (size == dev_cfg->flash_size) {
701 			/* chip erase */
702 			cmd_erase.Instruction = SPI_NOR_CMD_CE;
703 			cmd_erase.AddressMode = QSPI_ADDRESS_NONE;
704 			qspi_send_cmd(dev, &cmd_erase);
705 			size -= dev_cfg->flash_size;
706 		} else {
707 			const struct jesd216_erase_type *erase_types =
708 							dev_data->erase_types;
709 			const struct jesd216_erase_type *bet = NULL;
710 
711 			for (uint8_t ei = 0;
712 				ei < JESD216_NUM_ERASE_TYPES; ++ei) {
713 				const struct jesd216_erase_type *etp =
714 							&erase_types[ei];
715 
716 				if ((etp->exp != 0)
717 				    && SPI_NOR_IS_ALIGNED(addr, etp->exp)
718 				    && SPI_NOR_IS_ALIGNED(size, etp->exp)
719 				    && ((bet == NULL)
720 					|| (etp->exp > bet->exp))) {
721 					bet = etp;
722 					cmd_erase.Instruction = bet->cmd;
723 				}
724 			}
725 			if (bet != NULL) {
726 				qspi_send_cmd(dev, &cmd_erase);
727 				addr += BIT(bet->exp);
728 				size -= BIT(bet->exp);
729 			} else {
730 				LOG_ERR("Can't erase %zu at 0x%lx",
731 					size, (long)addr);
732 				ret = -EINVAL;
733 			}
734 		}
735 		qspi_wait_until_ready(dev);
736 	}
737 	goto end;
738 
739 end:
740 	qspi_unlock_thread(dev);
741 
742 	return ret;
743 }
744 
745 static const struct flash_parameters flash_stm32_qspi_parameters = {
746 	.write_block_size = 1,
747 	.erase_value = 0xff
748 };
749 
750 static const struct flash_parameters *
flash_stm32_qspi_get_parameters(const struct device * dev)751 flash_stm32_qspi_get_parameters(const struct device *dev)
752 {
753 	ARG_UNUSED(dev);
754 
755 	return &flash_stm32_qspi_parameters;
756 }
757 
flash_stm32_qspi_get_size(const struct device * dev,uint64_t * size)758 static int flash_stm32_qspi_get_size(const struct device *dev, uint64_t *size)
759 {
760 	const struct flash_stm32_qspi_config *dev_cfg = dev->config;
761 
762 	*size = (uint64_t)dev_cfg->flash_size;
763 
764 	return 0;
765 }
766 
flash_stm32_qspi_isr(const struct device * dev)767 static void flash_stm32_qspi_isr(const struct device *dev)
768 {
769 	struct flash_stm32_qspi_data *dev_data = dev->data;
770 
771 	HAL_QSPI_IRQHandler(&dev_data->hqspi);
772 }
773 
774 /* This function is executed in the interrupt context */
775 #if STM32_QSPI_USE_DMA
qspi_dma_callback(const struct device * dev,void * arg,uint32_t channel,int status)776 static void qspi_dma_callback(const struct device *dev, void *arg,
777 			 uint32_t channel, int status)
778 {
779 	DMA_HandleTypeDef *hdma = arg;
780 
781 	ARG_UNUSED(dev);
782 
783 	if (status < 0) {
784 		LOG_ERR("DMA callback error with channel %d.", channel);
785 
786 	}
787 
788 	HAL_DMA_IRQHandler(hdma);
789 }
790 #endif
791 
HAL_DMA_Abort(DMA_HandleTypeDef * hdma)792 __weak HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
793 {
794 	return HAL_OK;
795 }
796 
HAL_DMA_Abort_IT(DMA_HandleTypeDef * hdma)797 __weak HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
798 {
799 	return HAL_OK;
800 }
801 
802 /*
803  * Transfer Error callback.
804  */
HAL_QSPI_ErrorCallback(QSPI_HandleTypeDef * hqspi)805 void HAL_QSPI_ErrorCallback(QSPI_HandleTypeDef *hqspi)
806 {
807 	struct flash_stm32_qspi_data *dev_data =
808 		CONTAINER_OF(hqspi, struct flash_stm32_qspi_data, hqspi);
809 
810 	LOG_DBG("Enter");
811 
812 	dev_data->cmd_status = -EIO;
813 
814 	k_sem_give(&dev_data->sync);
815 }
816 
817 /*
818  * Command completed callback.
819  */
HAL_QSPI_CmdCpltCallback(QSPI_HandleTypeDef * hqspi)820 void HAL_QSPI_CmdCpltCallback(QSPI_HandleTypeDef *hqspi)
821 {
822 	struct flash_stm32_qspi_data *dev_data =
823 		CONTAINER_OF(hqspi, struct flash_stm32_qspi_data, hqspi);
824 
825 	k_sem_give(&dev_data->sync);
826 }
827 
828 /*
829  * Rx Transfer completed callback.
830  */
HAL_QSPI_RxCpltCallback(QSPI_HandleTypeDef * hqspi)831 void HAL_QSPI_RxCpltCallback(QSPI_HandleTypeDef *hqspi)
832 {
833 	struct flash_stm32_qspi_data *dev_data =
834 		CONTAINER_OF(hqspi, struct flash_stm32_qspi_data, hqspi);
835 
836 	k_sem_give(&dev_data->sync);
837 }
838 
839 /*
840  * Tx Transfer completed callback.
841  */
HAL_QSPI_TxCpltCallback(QSPI_HandleTypeDef * hqspi)842 void HAL_QSPI_TxCpltCallback(QSPI_HandleTypeDef *hqspi)
843 {
844 	struct flash_stm32_qspi_data *dev_data =
845 		CONTAINER_OF(hqspi, struct flash_stm32_qspi_data, hqspi);
846 
847 	k_sem_give(&dev_data->sync);
848 }
849 
850 /*
851  * Status Match callback.
852  */
HAL_QSPI_StatusMatchCallback(QSPI_HandleTypeDef * hqspi)853 void HAL_QSPI_StatusMatchCallback(QSPI_HandleTypeDef *hqspi)
854 {
855 	struct flash_stm32_qspi_data *dev_data =
856 		CONTAINER_OF(hqspi, struct flash_stm32_qspi_data, hqspi);
857 
858 	k_sem_give(&dev_data->sync);
859 }
860 
861 /*
862  * Timeout callback.
863  */
HAL_QSPI_TimeOutCallback(QSPI_HandleTypeDef * hqspi)864 void HAL_QSPI_TimeOutCallback(QSPI_HandleTypeDef *hqspi)
865 {
866 	struct flash_stm32_qspi_data *dev_data =
867 		CONTAINER_OF(hqspi, struct flash_stm32_qspi_data, hqspi);
868 
869 	LOG_DBG("Enter");
870 
871 	dev_data->cmd_status = -EIO;
872 
873 	k_sem_give(&dev_data->sync);
874 }
875 
876 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
flash_stm32_qspi_pages_layout(const struct device * dev,const struct flash_pages_layout ** layout,size_t * layout_size)877 static void flash_stm32_qspi_pages_layout(const struct device *dev,
878 				const struct flash_pages_layout **layout,
879 				size_t *layout_size)
880 {
881 	struct flash_stm32_qspi_data *dev_data = dev->data;
882 
883 	*layout = &dev_data->layout;
884 	*layout_size = 1;
885 }
886 #endif
887 
888 static DEVICE_API(flash, flash_stm32_qspi_driver_api) = {
889 	.read = flash_stm32_qspi_read,
890 	.write = flash_stm32_qspi_write,
891 	.erase = flash_stm32_qspi_erase,
892 	.get_parameters = flash_stm32_qspi_get_parameters,
893 	.get_size = flash_stm32_qspi_get_size,
894 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
895 	.page_layout = flash_stm32_qspi_pages_layout,
896 #endif
897 #if defined(CONFIG_FLASH_JESD216_API)
898 	.sfdp_read = qspi_read_sfdp,
899 	.read_jedec_id = qspi_read_jedec_id,
900 #endif /* CONFIG_FLASH_JESD216_API */
901 };
902 
903 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
setup_pages_layout(const struct device * dev)904 static int setup_pages_layout(const struct device *dev)
905 {
906 	const struct flash_stm32_qspi_config *dev_cfg = dev->config;
907 	struct flash_stm32_qspi_data *data = dev->data;
908 	const size_t flash_size = dev_cfg->flash_size;
909 	uint32_t layout_page_size = data->page_size;
910 	uint8_t exp = 0;
911 	int rv = 0;
912 
913 	/* Find the smallest erase size. */
914 	for (size_t i = 0; i < ARRAY_SIZE(data->erase_types); ++i) {
915 		const struct jesd216_erase_type *etp = &data->erase_types[i];
916 
917 		if ((etp->cmd != 0)
918 		    && ((exp == 0) || (etp->exp < exp))) {
919 			exp = etp->exp;
920 		}
921 	}
922 
923 	if (exp == 0) {
924 		return -ENOTSUP;
925 	}
926 
927 	uint32_t erase_size = BIT(exp) << STM32_QSPI_DOUBLE_FLASH;
928 
929 	/* We need layout page size to be compatible with erase size */
930 	if ((layout_page_size % erase_size) != 0) {
931 		LOG_DBG("layout page %u not compatible with erase size %u",
932 			layout_page_size, erase_size);
933 		LOG_DBG("erase size will be used as layout page size");
934 		layout_page_size = erase_size;
935 	}
936 
937 	/* Warn but accept layout page sizes that leave inaccessible
938 	 * space.
939 	 */
940 	if ((flash_size % layout_page_size) != 0) {
941 		LOG_INF("layout page %u wastes space with device size %zu",
942 			layout_page_size, flash_size);
943 	}
944 
945 	data->layout.pages_size = layout_page_size;
946 	data->layout.pages_count = flash_size / layout_page_size;
947 	LOG_DBG("layout %u x %u By pages", data->layout.pages_count,
948 					   data->layout.pages_size);
949 
950 	return rv;
951 }
952 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
953 
qspi_program_addr_4b(const struct device * dev,bool write_enable)954 static int qspi_program_addr_4b(const struct device *dev, bool write_enable)
955 {
956 	int ret;
957 
958 	/* Send write enable command, if required */
959 	if (write_enable) {
960 		ret = qspi_send_cmd(dev, &cmd_write_en);
961 		if (ret != 0) {
962 			return ret;
963 		}
964 	}
965 
966 	/* Program the flash memory to use 4 bytes addressing */
967 	QSPI_CommandTypeDef cmd = {
968 		.Instruction = SPI_NOR_CMD_4BA,
969 		.InstructionMode = QSPI_INSTRUCTION_1_LINE,
970 	};
971 
972 	/*
973 	 * No need to Read control register afterwards to verify if 4byte addressing mode
974 	 * is enabled as the effect of the command is immediate
975 	 * and the SPI_NOR_CMD_RDCR is vendor-specific :
976 	 * SPI_NOR_4BYTE_BIT is BIT 5 for Macronix and 0 for Micron or Windbond
977 	 * Moreover bit value meaning is also vendor-specific
978 	 */
979 
980 	return qspi_send_cmd(dev, &cmd);
981 }
982 
qspi_read_status_register(const struct device * dev,uint8_t reg_num,uint8_t * reg)983 static int qspi_read_status_register(const struct device *dev, uint8_t reg_num, uint8_t *reg)
984 {
985 	QSPI_CommandTypeDef cmd = {
986 		.InstructionMode = QSPI_INSTRUCTION_1_LINE,
987 		.DataMode = QSPI_DATA_1_LINE,
988 	};
989 
990 	switch (reg_num) {
991 	case 1U:
992 		cmd.Instruction = SPI_NOR_CMD_RDSR;
993 		break;
994 	case 2U:
995 		cmd.Instruction = SPI_NOR_CMD_RDSR2;
996 		break;
997 	case 3U:
998 		cmd.Instruction = SPI_NOR_CMD_RDSR3;
999 		break;
1000 	default:
1001 		return -EINVAL;
1002 	}
1003 
1004 	return qspi_read_access(dev, &cmd, reg, sizeof(*reg));
1005 }
1006 
qspi_write_status_register(const struct device * dev,uint8_t reg_num,uint8_t reg)1007 static int qspi_write_status_register(const struct device *dev, uint8_t reg_num, uint8_t reg)
1008 {
1009 	struct flash_stm32_qspi_data *dev_data = dev->data;
1010 	size_t size;
1011 	uint8_t regs[4] = { 0 };
1012 	uint8_t *regs_p;
1013 	int ret;
1014 
1015 	QSPI_CommandTypeDef cmd = {
1016 		.Instruction = SPI_NOR_CMD_WRSR,
1017 		.InstructionMode = QSPI_INSTRUCTION_1_LINE,
1018 		.DataMode = QSPI_DATA_1_LINE,
1019 	};
1020 
1021 	if (reg_num == 1) {
1022 		size = 1U;
1023 		regs[0] = reg;
1024 		regs_p = &regs[0];
1025 		/* 1 byte write clears SR2, write SR2 as well */
1026 		if (dev_data->qer_type == JESD216_DW15_QER_S2B1v1) {
1027 			ret = qspi_read_status_register(dev, 2, &regs[1]);
1028 			if (ret < 0) {
1029 				return ret;
1030 			}
1031 			size = 2U;
1032 		}
1033 	} else if (reg_num == 2) {
1034 		cmd.Instruction = SPI_NOR_CMD_WRSR2;
1035 		size = 1U;
1036 		regs[1] = reg;
1037 		regs_p = &regs[1];
1038 		/* if SR2 write needs SR1 */
1039 		if ((dev_data->qer_type == JESD216_DW15_QER_VAL_S2B1v1) ||
1040 		    (dev_data->qer_type == JESD216_DW15_QER_VAL_S2B1v4) ||
1041 		    (dev_data->qer_type == JESD216_DW15_QER_VAL_S2B1v5)) {
1042 			ret = qspi_read_status_register(dev, 1, &regs[0]);
1043 			if (ret < 0) {
1044 				return ret;
1045 			}
1046 			cmd.Instruction = SPI_NOR_CMD_WRSR;
1047 			size = 2U;
1048 			regs_p = &regs[0];
1049 		}
1050 	} else if (reg_num == 3) {
1051 		cmd.Instruction = SPI_NOR_CMD_WRSR3;
1052 		size = 1U;
1053 		regs[2] = reg;
1054 		regs_p = &regs[2];
1055 	} else {
1056 		return -EINVAL;
1057 	}
1058 
1059 	return qspi_write_access(dev, &cmd, regs_p, size);
1060 }
1061 
qspi_write_enable(const struct device * dev)1062 static int qspi_write_enable(const struct device *dev)
1063 {
1064 	uint8_t reg;
1065 	int ret;
1066 
1067 	ret = qspi_send_cmd(dev, &cmd_write_en);
1068 	if (ret) {
1069 		return ret;
1070 	}
1071 
1072 	do {
1073 		ret = qspi_read_status_register(dev, 1U, &reg);
1074 	} while (!ret && !(reg & SPI_NOR_WEL_BIT));
1075 
1076 	return ret;
1077 }
1078 
qspi_program_quad_io(const struct device * dev)1079 static int qspi_program_quad_io(const struct device *dev)
1080 {
1081 	struct flash_stm32_qspi_data *data = dev->data;
1082 	uint8_t qe_reg_num;
1083 	uint8_t qe_bit;
1084 	uint8_t reg;
1085 	int ret;
1086 
1087 	switch (data->qer_type) {
1088 	case JESD216_DW15_QER_NONE:
1089 		/* no QE bit, device detects reads based on opcode */
1090 		return 0;
1091 	case JESD216_DW15_QER_S1B6:
1092 		qe_reg_num = 1U;
1093 		qe_bit = BIT(6U);
1094 		break;
1095 	case JESD216_DW15_QER_S2B7:
1096 		qe_reg_num = 2U;
1097 		qe_bit = BIT(7U);
1098 		break;
1099 	case JESD216_DW15_QER_S2B1v1:
1100 		__fallthrough;
1101 	case JESD216_DW15_QER_S2B1v4:
1102 		__fallthrough;
1103 	case JESD216_DW15_QER_S2B1v5:
1104 		__fallthrough;
1105 	case JESD216_DW15_QER_S2B1v6:
1106 		qe_reg_num = 2U;
1107 		qe_bit = BIT(1U);
1108 		break;
1109 	default:
1110 		return -ENOTSUP;
1111 	}
1112 
1113 	ret = qspi_read_status_register(dev, qe_reg_num, &reg);
1114 	if (ret < 0) {
1115 		return ret;
1116 	}
1117 
1118 	/* exit early if QE bit is already set */
1119 	if ((reg & qe_bit) != 0U) {
1120 		return 0;
1121 	}
1122 
1123 	reg |= qe_bit;
1124 
1125 	ret = qspi_write_enable(dev);
1126 	if (ret < 0) {
1127 		return ret;
1128 	}
1129 
1130 	ret = qspi_write_status_register(dev, qe_reg_num, reg);
1131 	if (ret < 0) {
1132 		return ret;
1133 	}
1134 
1135 	ret = qspi_wait_until_ready(dev);
1136 	if (ret < 0) {
1137 		return ret;
1138 	}
1139 
1140 	/* validate that QE bit is set */
1141 	ret = qspi_read_status_register(dev, qe_reg_num, &reg);
1142 	if (ret < 0) {
1143 		return ret;
1144 	}
1145 
1146 	if ((reg & qe_bit) == 0U) {
1147 		LOG_ERR("Status Register %u [0x%02x] not set", qe_reg_num, reg);
1148 		return -EIO;
1149 	}
1150 
1151 	return ret;
1152 }
1153 
spi_nor_process_bfp(const struct device * dev,const struct jesd216_param_header * php,const struct jesd216_bfp * bfp)1154 static int spi_nor_process_bfp(const struct device *dev,
1155 			       const struct jesd216_param_header *php,
1156 			       const struct jesd216_bfp *bfp)
1157 {
1158 	const struct flash_stm32_qspi_config *dev_cfg = dev->config;
1159 	struct flash_stm32_qspi_data *data = dev->data;
1160 	struct jesd216_erase_type *etp = data->erase_types;
1161 	uint8_t addr_mode;
1162 	const size_t flash_size = (jesd216_bfp_density(bfp) / 8U) << STM32_QSPI_DOUBLE_FLASH;
1163 	int rc;
1164 
1165 	if (flash_size != dev_cfg->flash_size) {
1166 		LOG_ERR("Unexpected flash size: %u", flash_size);
1167 	}
1168 
1169 	LOG_INF("%s: %u MiBy flash", dev->name, (uint32_t)(flash_size >> 20));
1170 
1171 	/* Copy over the erase types, preserving their order.  (The
1172 	 * Sector Map Parameter table references them by index.)
1173 	 */
1174 	memset(data->erase_types, 0, sizeof(data->erase_types));
1175 	for (uint8_t ti = 1; ti <= ARRAY_SIZE(data->erase_types); ++ti) {
1176 		if (jesd216_bfp_erase(bfp, ti, etp) == 0) {
1177 			LOG_DBG("Erase %u with %02x",
1178 					(uint32_t)BIT(etp->exp), etp->cmd);
1179 		}
1180 		++etp;
1181 	}
1182 
1183 	data->page_size = jesd216_bfp_page_size(php, bfp);
1184 
1185 	LOG_DBG("Page size %u bytes", data->page_size);
1186 	LOG_DBG("Flash size %u bytes", flash_size);
1187 
1188 	addr_mode = jesd216_bfp_addrbytes(bfp);
1189 	if (addr_mode == JESD216_SFDP_BFP_DW1_ADDRBYTES_VAL_3B4B) {
1190 		struct jesd216_bfp_dw16 dw16;
1191 
1192 		if (jesd216_bfp_decode_dw16(php, bfp, &dw16) == 0) {
1193 			/*
1194 			 * According to JESD216, the bit0 of dw16.enter_4ba
1195 			 * portion of flash description register 16 indicates
1196 			 * if it is enough to use 0xB7 instruction without
1197 			 * write enable to switch to 4 bytes addressing mode.
1198 			 * If bit 1 is set, a write enable is needed.
1199 			 */
1200 			if (dw16.enter_4ba & 0x3) {
1201 				rc = qspi_program_addr_4b(dev, dw16.enter_4ba & 2);
1202 				if (rc == 0) {
1203 					data->flag_access_32bit = true;
1204 					LOG_INF("Flash - address mode: 4B");
1205 				} else {
1206 					LOG_ERR("Unable to enter 4B mode: %d\n", rc);
1207 					return rc;
1208 				}
1209 			}
1210 		}
1211 	}
1212 	if (addr_mode == JESD216_SFDP_BFP_DW1_ADDRBYTES_VAL_4B) {
1213 		data->flag_access_32bit = true;
1214 		LOG_INF("Flash - address mode: 4B");
1215 	}
1216 
1217 	/*
1218 	 * Only check if the 1-4-4 (i.e. 4READ) or 1-1-4 (QREAD)
1219 	 * is supported - other modes are not.
1220 	 */
1221 	if (IS_ENABLED(STM32_QSPI_USE_QUAD_IO)) {
1222 		const enum jesd216_mode_type supported_modes[] = { JESD216_MODE_114,
1223 								   JESD216_MODE_144 };
1224 		struct jesd216_bfp_dw15 dw15;
1225 		struct jesd216_instr res;
1226 
1227 		/* reset active mode */
1228 		data->mode = STM32_QSPI_UNKNOWN_MODE;
1229 
1230 		/* query supported read modes, begin from the slowest */
1231 		for (size_t i = 0; i < ARRAY_SIZE(supported_modes); ++i) {
1232 			rc = jesd216_bfp_read_support(php, bfp, supported_modes[i], &res);
1233 			if (rc >= 0) {
1234 				LOG_INF("Quad read mode %d instr [0x%x] supported",
1235 					supported_modes[i], res.instr);
1236 
1237 				data->mode = supported_modes[i];
1238 				data->qspi_read_cmd = res.instr;
1239 				data->qspi_read_cmd_latency = res.wait_states;
1240 
1241 				if (res.mode_clocks) {
1242 					data->qspi_read_cmd_latency += res.mode_clocks;
1243 				}
1244 			}
1245 		}
1246 
1247 		/* don't continue when there is no supported mode */
1248 		if (data->mode == STM32_QSPI_UNKNOWN_MODE) {
1249 			LOG_ERR("No supported flash read mode found");
1250 			return -ENOTSUP;
1251 		}
1252 
1253 		LOG_INF("Quad read mode %d instr [0x%x] will be used", data->mode, res.instr);
1254 
1255 		/* try to decode QE requirement type */
1256 		rc = jesd216_bfp_decode_dw15(php, bfp, &dw15);
1257 		if (rc < 0) {
1258 			/* will use QER from DTS or default (refer to device data) */
1259 			LOG_WRN("Unable to decode QE requirement [DW15]: %d", rc);
1260 		} else {
1261 			/* bypass DTS QER value */
1262 			data->qer_type = dw15.qer;
1263 		}
1264 
1265 		LOG_INF("QE requirement mode: %x", data->qer_type);
1266 
1267 		/* enable QE */
1268 		rc = qspi_program_quad_io(dev);
1269 		if (rc < 0) {
1270 			LOG_ERR("Failed to enable Quad mode: %d", rc);
1271 			return rc;
1272 		}
1273 
1274 		LOG_INF("Quad mode enabled");
1275 	}
1276 
1277 	return 0;
1278 }
1279 
1280 #if STM32_QSPI_RESET_GPIO
flash_stm32_qspi_gpio_reset(const struct device * dev)1281 static void flash_stm32_qspi_gpio_reset(const struct device *dev)
1282 {
1283 	const struct flash_stm32_qspi_config *dev_cfg = dev->config;
1284 
1285 	/* Generate RESETn pulse for the flash memory */
1286 	gpio_pin_configure_dt(&dev_cfg->reset, GPIO_OUTPUT_ACTIVE);
1287 	k_msleep(DT_INST_PROP(0, reset_gpios_duration));
1288 	gpio_pin_set_dt(&dev_cfg->reset, 0);
1289 }
1290 #endif
1291 
1292 #if STM32_QSPI_RESET_CMD
flash_stm32_qspi_send_reset(const struct device * dev)1293 static int flash_stm32_qspi_send_reset(const struct device *dev)
1294 {
1295 	QSPI_CommandTypeDef cmd = {
1296 		.Instruction = SPI_NOR_CMD_RESET_EN,
1297 		.InstructionMode = QSPI_INSTRUCTION_1_LINE,
1298 	};
1299 	int ret;
1300 
1301 	ret = qspi_send_cmd(dev, &cmd);
1302 	if (ret != 0) {
1303 		LOG_ERR("%d: Failed to send RESET_EN", ret);
1304 		return ret;
1305 	}
1306 
1307 	cmd.Instruction = SPI_NOR_CMD_RESET_MEM;
1308 	ret = qspi_send_cmd(dev, &cmd);
1309 	if (ret != 0) {
1310 		LOG_ERR("%d: Failed to send RESET_MEM", ret);
1311 		return ret;
1312 	}
1313 
1314 	LOG_DBG("Send Reset command");
1315 
1316 	return 0;
1317 }
1318 #endif
1319 
flash_stm32_qspi_init(const struct device * dev)1320 static int flash_stm32_qspi_init(const struct device *dev)
1321 {
1322 	const struct flash_stm32_qspi_config *dev_cfg = dev->config;
1323 	struct flash_stm32_qspi_data *dev_data = dev->data;
1324 	uint32_t ahb_clock_freq;
1325 	uint32_t prescaler = 0;
1326 	int ret;
1327 
1328 	/* Signals configuration */
1329 	ret = pinctrl_apply_state(dev_cfg->pcfg, PINCTRL_STATE_DEFAULT);
1330 	if (ret < 0) {
1331 		LOG_ERR("QSPI pinctrl setup failed (%d)", ret);
1332 		return ret;
1333 	}
1334 
1335 #if STM32_QSPI_RESET_GPIO
1336 	flash_stm32_qspi_gpio_reset(dev);
1337 #endif
1338 #if STM32_QSPI_USE_DMA
1339 	/*
1340 	 * DMA configuration
1341 	 * Due to use of QSPI HAL API in current driver,
1342 	 * both HAL and Zephyr DMA drivers should be configured.
1343 	 * The required configuration for Zephyr DMA driver should only provide
1344 	 * the minimum information to inform the DMA slot will be in used and
1345 	 * how to route callbacks.
1346 	 */
1347 	struct dma_config dma_cfg = dev_data->dma.cfg;
1348 	static DMA_HandleTypeDef hdma;
1349 
1350 	if (!device_is_ready(dev_data->dma.dev)) {
1351 		LOG_ERR("%s device not ready", dev_data->dma.dev->name);
1352 		return -ENODEV;
1353 	}
1354 
1355 	/* Proceed to the minimum Zephyr DMA driver init */
1356 	dma_cfg.user_data = &hdma;
1357 	/* HACK: This field is used to inform driver that it is overridden */
1358 	dma_cfg.linked_channel = STM32_DMA_HAL_OVERRIDE;
1359 	ret = dma_config(dev_data->dma.dev, dev_data->dma.channel, &dma_cfg);
1360 	if (ret != 0) {
1361 		return ret;
1362 	}
1363 
1364 	/* Proceed to the HAL DMA driver init */
1365 	if (dma_cfg.source_data_size != dma_cfg.dest_data_size) {
1366 		LOG_ERR("Source and destination data sizes not aligned");
1367 		return -EINVAL;
1368 	}
1369 
1370 	int index = find_lsb_set(dma_cfg.source_data_size) - 1;
1371 
1372 	hdma.Init.PeriphDataAlignment = table_p_size[index];
1373 	hdma.Init.MemDataAlignment = table_m_size[index];
1374 	hdma.Init.PeriphInc = DMA_PINC_DISABLE;
1375 	hdma.Init.MemInc = DMA_MINC_ENABLE;
1376 	hdma.Init.Mode = DMA_NORMAL;
1377 	hdma.Init.Priority = table_priority[dma_cfg.channel_priority];
1378 #ifdef CONFIG_DMA_STM32_V1
1379 	/* TODO: Not tested in this configuration */
1380 	hdma.Init.Channel = dma_cfg.dma_slot;
1381 	hdma.Instance = __LL_DMA_GET_STREAM_INSTANCE(dev_data->dma.reg,
1382 						     dev_data->dma.channel);
1383 #else
1384 	hdma.Init.Request = dma_cfg.dma_slot;
1385 #ifdef CONFIG_DMAMUX_STM32
1386 	/* HAL expects a valid DMA channel (not a DMAMUX channel) */
1387 	hdma.Instance = __LL_DMA_GET_CHANNEL_INSTANCE(dev_data->dma.reg,
1388 						      dev_data->dma.channel);
1389 #else
1390 	hdma.Instance = __LL_DMA_GET_CHANNEL_INSTANCE(dev_data->dma.reg,
1391 						      dev_data->dma.channel-1);
1392 #endif
1393 #endif /* CONFIG_DMA_STM32_V1 */
1394 
1395 	/* Initialize DMA HAL */
1396 	__HAL_LINKDMA(&dev_data->hqspi, hdma, hdma);
1397 	HAL_DMA_Init(&hdma);
1398 
1399 #endif /* STM32_QSPI_USE_DMA */
1400 
1401 	/* Clock configuration */
1402 	if (clock_control_on(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
1403 			     (clock_control_subsys_t) &dev_cfg->pclken) != 0) {
1404 		LOG_DBG("Could not enable QSPI clock");
1405 		return -EIO;
1406 	}
1407 
1408 	if (clock_control_get_rate(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
1409 			(clock_control_subsys_t) &dev_cfg->pclken,
1410 			&ahb_clock_freq) < 0) {
1411 		LOG_DBG("Failed to get AHB clock frequency");
1412 		return -EIO;
1413 	}
1414 
1415 	for (; prescaler <= STM32_QSPI_CLOCK_PRESCALER_MAX; prescaler++) {
1416 		uint32_t clk = ahb_clock_freq / (prescaler + 1);
1417 
1418 		if (clk <= dev_cfg->max_frequency) {
1419 			break;
1420 		}
1421 	}
1422 	__ASSERT_NO_MSG(prescaler <= STM32_QSPI_CLOCK_PRESCALER_MAX);
1423 	/* Initialize QSPI HAL */
1424 	dev_data->hqspi.Init.ClockPrescaler = prescaler;
1425 	/* Give a bit position from 0 to 31 to the HAL init minus 1 for the DCR1 reg */
1426 	dev_data->hqspi.Init.FlashSize = find_lsb_set(dev_cfg->flash_size) - 2;
1427 #if DT_PROP(DT_NODELABEL(quadspi), dual_flash) && defined(QUADSPI_CR_DFM)
1428 	/*
1429 	 * When the DTS has <dual-flash>, it means Dual Flash Mode
1430 	 * Even in DUAL flash config, the SDFP is read from one single quad-NOR
1431 	 * else the magic nb is wrong (0x46465353)
1432 	 * That means that the Dual Flash config is set after the SFDP sequence
1433 	 */
1434 	dev_data->hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_HALFCYCLE;
1435 	dev_data->hqspi.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_3_CYCLE;
1436 	dev_data->hqspi.Init.DualFlash = QSPI_DUALFLASH_DISABLE;
1437 	/* Set Dual Flash Mode only on MemoryMapped */
1438 	dev_data->hqspi.Init.FlashID = QSPI_FLASH_ID_1;
1439 #endif /* dual_flash */
1440 
1441 	HAL_QSPI_Init(&dev_data->hqspi);
1442 
1443 #if DT_NODE_HAS_PROP(DT_NODELABEL(quadspi), flash_id) && \
1444 	defined(QUADSPI_CR_FSEL)
1445 	/*
1446 	 * Some stm32 mcu with quadspi (like stm32l47x or stm32l48x)
1447 	 * does not support Dual-Flash Mode
1448 	 */
1449 	uint8_t qspi_flash_id = DT_PROP(DT_NODELABEL(quadspi), flash_id);
1450 
1451 	HAL_QSPI_SetFlashID(&dev_data->hqspi,
1452 			    (qspi_flash_id - 1) << QUADSPI_CR_FSEL_Pos);
1453 #endif
1454 	/* Initialize semaphores */
1455 	k_sem_init(&dev_data->sem, 1, 1);
1456 	k_sem_init(&dev_data->sync, 0, 1);
1457 
1458 	/* Run IRQ init */
1459 	dev_cfg->irq_config(dev);
1460 
1461 #if STM32_QSPI_RESET_CMD
1462 	flash_stm32_qspi_send_reset(dev);
1463 	k_busy_wait(DT_INST_PROP(0, reset_cmd_wait));
1464 #endif
1465 
1466 	/* Run NOR init */
1467 	const uint8_t decl_nph = 2;
1468 	union {
1469 		/* We only process BFP so use one parameter block */
1470 		uint8_t raw[JESD216_SFDP_SIZE(decl_nph)];
1471 		struct jesd216_sfdp_header sfdp;
1472 	} u;
1473 	const struct jesd216_sfdp_header *hp = &u.sfdp;
1474 
1475 	ret = qspi_read_sfdp(dev, 0, u.raw, sizeof(u.raw));
1476 	if (ret != 0) {
1477 		LOG_ERR("SFDP read failed: %d", ret);
1478 		return ret;
1479 	}
1480 
1481 	uint32_t magic = jesd216_sfdp_magic(hp);
1482 
1483 	if (magic != JESD216_SFDP_MAGIC) {
1484 		LOG_ERR("SFDP magic %08x invalid", magic);
1485 		return -EINVAL;
1486 	}
1487 
1488 	LOG_INF("%s: SFDP v %u.%u AP %x with %u PH", dev->name,
1489 		hp->rev_major, hp->rev_minor, hp->access, 1 + hp->nph);
1490 
1491 	const struct jesd216_param_header *php = hp->phdr;
1492 	const struct jesd216_param_header *phpe = php +
1493 						     MIN(decl_nph, 1 + hp->nph);
1494 
1495 	while (php != phpe) {
1496 		uint16_t id = jesd216_param_id(php);
1497 
1498 		LOG_INF("PH%u: %04x rev %u.%u: %u DW @ %x",
1499 			(php - hp->phdr), id, php->rev_major, php->rev_minor,
1500 			php->len_dw, jesd216_param_addr(php));
1501 
1502 		if (id == JESD216_SFDP_PARAM_ID_BFP) {
1503 			union {
1504 				uint32_t dw[20];
1505 				struct jesd216_bfp bfp;
1506 			} u2;
1507 			const struct jesd216_bfp *bfp = &u2.bfp;
1508 
1509 			ret = qspi_read_sfdp(dev, jesd216_param_addr(php),
1510 					     (uint8_t *)u2.dw,
1511 					     MIN(sizeof(uint32_t) * php->len_dw, sizeof(u2.dw)));
1512 			if (ret == 0) {
1513 				ret = spi_nor_process_bfp(dev, php, bfp);
1514 			}
1515 
1516 			if (ret != 0) {
1517 				LOG_ERR("SFDP BFP failed: %d", ret);
1518 				break;
1519 			}
1520 		}
1521 		++php;
1522 	}
1523 
1524 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
1525 	ret = setup_pages_layout(dev);
1526 	if (ret != 0) {
1527 		LOG_ERR("layout setup failed: %d", ret);
1528 		return -ENODEV;
1529 	}
1530 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
1531 
1532 	ret = qspi_write_unprotect(dev);
1533 	if (ret != 0) {
1534 		LOG_ERR("write unprotect failed: %d", ret);
1535 		return -ENODEV;
1536 	}
1537 	LOG_DBG("Write Un-protected");
1538 
1539 #ifdef CONFIG_STM32_MEMMAP
1540 #if DT_PROP(DT_NODELABEL(quadspi), dual_flash) && defined(QUADSPI_CR_DFM)
1541 	/*
1542 	 * When the DTS has dual_flash, it means Dual Flash Mode for Memory MAPPED
1543 	 * Force Dual Flash mode now, after the SFDP sequence which is reading
1544 	 * one quad-NOR only
1545 	 */
1546 	MODIFY_REG(dev_data->hqspi.Instance->CR, (QUADSPI_CR_DFM), QSPI_DUALFLASH_ENABLE);
1547 	LOG_DBG("Dual Flash Mode");
1548 #endif /* dual_flash */
1549 
1550 	ret = stm32_qspi_set_memory_mapped(dev);
1551 	if (ret != 0) {
1552 		LOG_ERR("Failed to enable memory-mapped mode: %d", ret);
1553 		return ret;
1554 	}
1555 	LOG_INF("Memory-mapped NOR quad-flash at 0x%lx (0x%x bytes)",
1556 		(long)(STM32_QSPI_BASE_ADDRESS),
1557 		dev_cfg->flash_size);
1558 #else
1559 	LOG_INF("NOR quad-flash at 0x%lx (0x%x bytes)",
1560 		(long)(STM32_QSPI_BASE_ADDRESS),
1561 		dev_cfg->flash_size);
1562 #endif
1563 
1564 	return 0;
1565 }
1566 
1567 #define DMA_CHANNEL_CONFIG(node, dir)					\
1568 		DT_DMAS_CELL_BY_NAME(node, dir, channel_config)
1569 
1570 #define QSPI_DMA_CHANNEL_INIT(node, dir)				\
1571 	.dev = DEVICE_DT_GET(DT_DMAS_CTLR(node)),			\
1572 	.channel = DT_DMAS_CELL_BY_NAME(node, dir, channel),		\
1573 	.reg = (DMA_TypeDef *)DT_REG_ADDR(				\
1574 				   DT_PHANDLE_BY_NAME(node, dmas, dir)),\
1575 	.cfg = {							\
1576 		.dma_slot = DT_DMAS_CELL_BY_NAME(node, dir, slot),	\
1577 		.source_data_size = STM32_DMA_CONFIG_PERIPHERAL_DATA_SIZE( \
1578 					DMA_CHANNEL_CONFIG(node, dir)), \
1579 		.dest_data_size = STM32_DMA_CONFIG_MEMORY_DATA_SIZE(    \
1580 					DMA_CHANNEL_CONFIG(node, dir)), \
1581 		.channel_priority = STM32_DMA_CONFIG_PRIORITY(		\
1582 					DMA_CHANNEL_CONFIG(node, dir)), \
1583 		.dma_callback = qspi_dma_callback,			\
1584 	},								\
1585 
1586 #define QSPI_DMA_CHANNEL(node, dir)					\
1587 	.dma = {							\
1588 		COND_CODE_1(DT_DMAS_HAS_NAME(node, dir),		\
1589 			(QSPI_DMA_CHANNEL_INIT(node, dir)),		\
1590 			(NULL))						\
1591 		},
1592 
1593 #define QSPI_FLASH_MODULE(drv_id, flash_id) 				\
1594 		(DT_DRV_INST(drv_id), qspi_nor_flash_##flash_id)
1595 
1596 static void flash_stm32_qspi_irq_config_func(const struct device *dev);
1597 
1598 #define DT_WRITEOC_PROP_OR(inst, default_value)                                              \
1599 	COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, writeoc),                                    \
1600 		    (_CONCAT(SPI_NOR_CMD_, DT_STRING_TOKEN(DT_DRV_INST(inst), writeoc))),    \
1601 		    ((default_value)))
1602 
1603 #define DT_QER_PROP_OR(inst, default_value)                                                  \
1604 	COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, quad_enable_requirements),                   \
1605 		    (_CONCAT(JESD216_DW15_QER_VAL_,                                          \
1606 			     DT_STRING_TOKEN(DT_DRV_INST(inst), quad_enable_requirements))), \
1607 		    ((default_value)))
1608 
1609 #define STM32_QSPI_NODE DT_INST_PARENT(0)
1610 
1611 PINCTRL_DT_DEFINE(STM32_QSPI_NODE);
1612 
1613 static const struct flash_stm32_qspi_config flash_stm32_qspi_cfg = {
1614 	.regs = (QUADSPI_TypeDef *)DT_REG_ADDR(STM32_QSPI_NODE),
1615 	.pclken = {
1616 		.enr = DT_CLOCKS_CELL(STM32_QSPI_NODE, bits),
1617 		.bus = DT_CLOCKS_CELL(STM32_QSPI_NODE, bus)
1618 	},
1619 	.irq_config = flash_stm32_qspi_irq_config_func,
1620 	.flash_size = DT_INST_REG_ADDR_BY_IDX(0, 1) << STM32_QSPI_DOUBLE_FLASH,
1621 	.max_frequency = DT_INST_PROP(0, qspi_max_frequency),
1622 	.pcfg = PINCTRL_DT_DEV_CONFIG_GET(STM32_QSPI_NODE),
1623 #if STM32_QSPI_RESET_GPIO
1624 	.reset = GPIO_DT_SPEC_INST_GET(0, reset_gpios),
1625 #endif
1626 #if DT_NODE_HAS_PROP(DT_INST(0, st_stm32_qspi_nor), jedec_id)
1627 	.jedec_id = DT_INST_PROP(0, jedec_id),
1628 #endif /* jedec_id */
1629 };
1630 
1631 static struct flash_stm32_qspi_data flash_stm32_qspi_dev_data = {
1632 	.hqspi = {
1633 		.Instance = (QUADSPI_TypeDef *)DT_REG_ADDR(STM32_QSPI_NODE),
1634 		.Init = {
1635 			.FifoThreshold = STM32_QSPI_FIFO_THRESHOLD,
1636 			.SampleShifting = QSPI_SAMPLE_SHIFTING_NONE,
1637 			.ChipSelectHighTime = QSPI_CS_HIGH_TIME_1_CYCLE,
1638 			.ClockMode = QSPI_CLOCK_MODE_0,
1639 			},
1640 	},
1641 	.qer_type = DT_QER_PROP_OR(0, JESD216_DW15_QER_VAL_S1B6),
1642 	.qspi_write_cmd = DT_WRITEOC_PROP_OR(0, SPI_NOR_CMD_PP_1_4_4),
1643 	QSPI_DMA_CHANNEL(STM32_QSPI_NODE, tx_rx)
1644 };
1645 
1646 DEVICE_DT_INST_DEFINE(0, &flash_stm32_qspi_init, NULL,
1647 		      &flash_stm32_qspi_dev_data, &flash_stm32_qspi_cfg,
1648 		      POST_KERNEL, CONFIG_FLASH_INIT_PRIORITY,
1649 		      &flash_stm32_qspi_driver_api);
1650 
flash_stm32_qspi_irq_config_func(const struct device * dev)1651 static void flash_stm32_qspi_irq_config_func(const struct device *dev)
1652 {
1653 	IRQ_CONNECT(DT_IRQN(STM32_QSPI_NODE), DT_IRQ(STM32_QSPI_NODE, priority),
1654 		    flash_stm32_qspi_isr, DEVICE_DT_INST_GET(0), 0);
1655 	irq_enable(DT_IRQN(STM32_QSPI_NODE));
1656 }
1657 
1658 #endif
1659