1 /*
2 * Copyright 2023-2024 NXP
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #ifndef ZEPHYR_DRIVERS_FLASH_NXP_S32_QSPI_H_
8 #define ZEPHYR_DRIVERS_FLASH_NXP_S32_QSPI_H_
9
10 #include "jesd216.h"
11
12 #define QSPI_ERASE_VALUE 0xff
13
14 #define QSPI_IS_ALIGNED(addr, bits) (((addr) & BIT_MASK(bits)) == 0)
15
16 #if defined(CONFIG_FLASH_NXP_S32_QSPI_SFDP_RUNTIME)
17 /* Size of LUT */
18 #define QSPI_SFDP_LUT_SIZE 130U
19 /* Size of init operations */
20 #define QSPI_SFDP_INIT_OP_SIZE 8U
21 #if defined(CONFIG_FLASH_JESD216_API)
22 /* Size of all LUT sequences for JESD216 operations */
23 #define QSPI_JESD216_SEQ_SIZE 8U
24 #endif /* CONFIG_FLASH_JESD216_API */
25 #endif /* CONFIG_FLASH_NXP_S32_QSPI_SFDP_RUNTIME */
26
27 struct nxp_s32_qspi_config {
28 const struct device *controller;
29 struct flash_parameters flash_parameters;
30 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
31 struct flash_pages_layout layout;
32 #endif
33 #if !defined(CONFIG_FLASH_NXP_S32_QSPI_SFDP_RUNTIME)
34 const Qspi_Ip_MemoryConfigType memory_cfg;
35 enum jesd216_dw15_qer_type qer_type;
36 bool quad_mode;
37 #endif
38 };
39
40 struct nxp_s32_qspi_data {
41 uint8_t instance;
42 Qspi_Ip_MemoryConnectionType memory_conn_cfg;
43 uint8_t read_sfdp_lut_idx;
44 #if defined(CONFIG_FLASH_NXP_S32_QSPI_SFDP_RUNTIME)
45 Qspi_Ip_MemoryConfigType memory_cfg;
46 Qspi_Ip_InstrOpType lut_ops[QSPI_SFDP_LUT_SIZE];
47 Qspi_Ip_InitOperationType init_ops[QSPI_SFDP_INIT_OP_SIZE];
48 #endif
49 #if defined(CONFIG_MULTITHREADING)
50 struct k_sem sem;
51 #endif
52 };
53
get_memory_config(const struct device * dev)54 static ALWAYS_INLINE Qspi_Ip_MemoryConfigType *get_memory_config(const struct device *dev)
55 {
56 #if defined(CONFIG_FLASH_NXP_S32_QSPI_SFDP_RUNTIME)
57 return &((struct nxp_s32_qspi_data *)dev->data)->memory_cfg;
58 #else
59 return ((Qspi_Ip_MemoryConfigType *)&((const struct nxp_s32_qspi_config *)dev->config)
60 ->memory_cfg);
61 #endif
62 }
63
nxp_s32_qspi_lock(const struct device * dev)64 static inline void nxp_s32_qspi_lock(const struct device *dev)
65 {
66 #ifdef CONFIG_MULTITHREADING
67 struct nxp_s32_qspi_data *data = dev->data;
68
69 k_sem_take(&data->sem, K_FOREVER);
70 #else
71 ARG_UNUSED(dev);
72 #endif
73 }
74
nxp_s32_qspi_unlock(const struct device * dev)75 static inline void nxp_s32_qspi_unlock(const struct device *dev)
76 {
77 #ifdef CONFIG_MULTITHREADING
78 struct nxp_s32_qspi_data *data = dev->data;
79
80 k_sem_give(&data->sem);
81 #else
82 ARG_UNUSED(dev);
83 #endif
84 }
85
86 /*
87 * This function retrieves the device instance used by the HAL
88 * to access the internal driver state.
89 */
90 uint8_t nxp_s32_qspi_register_device(void);
91
92 int nxp_s32_qspi_wait_until_ready(const struct device *dev);
93
94 int nxp_s32_qspi_read(const struct device *dev, off_t offset, void *dest, size_t size);
95
96 int nxp_s32_qspi_write(const struct device *dev, off_t offset, const void *src, size_t size);
97
98 int nxp_s32_qspi_erase(const struct device *dev, off_t offset, size_t size);
99
100 const struct flash_parameters *nxp_s32_qspi_get_parameters(const struct device *dev);
101
102 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
103 void nxp_s32_qspi_pages_layout(const struct device *dev, const struct flash_pages_layout **layout,
104 size_t *layout_size);
105 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
106
107 #if defined(CONFIG_FLASH_JESD216_API) || !defined(CONFIG_FLASH_NXP_S32_QSPI_SFDP_RUNTIME)
108 int nxp_s32_qspi_read_id(const struct device *dev, uint8_t *id);
109 #endif /* CONFIG_FLASH_JESD216_API || !CONFIG_FLASH_NXP_S32_QSPI_SFDP_RUNTIME */
110
111 #endif /* ZEPHYR_DRIVERS_FLASH_NXP_S32_QSPI_H_ */
112