1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @brief Header containing QSPI device interface specific declarations for the
9  * Zephyr OS layer of the Wi-Fi driver.
10  */
11 
12 #ifndef __QSPI_IF_H__
13 #define __QSPI_IF_H__
14 
15 #include <zephyr/kernel.h>
16 #include <zephyr/drivers/gpio.h>
17 #ifdef CONFIG_NRF70_ON_QSPI
18 #include <nrfx_qspi.h>
19 #endif
20 
21 #define RPU_WAKEUP_NOW BIT(0) /* WAKEUP RPU - RW */
22 #define RPU_AWAKE_BIT BIT(1) /* RPU AWAKE FROM SLEEP - RO */
23 #define RPU_READY_BIT BIT(2) /* RPU IS READY - RO*/
24 
25 struct qspi_config {
26 #ifdef CONFIG_NRF70_ON_QSPI
27 	nrf_qspi_addrmode_t addrmode;
28 	nrf_qspi_readoc_t readoc;
29 	nrf_qspi_writeoc_t writeoc;
30 	nrf_qspi_frequency_t sckfreq;
31 #endif
32 	unsigned char RDC4IO;
33 	bool easydma;
34 	bool single_op;
35 	bool quad_spi;
36 	bool encryption;
37 	bool CMD_CNONCE;
38 	bool enc_enabled;
39 	struct k_sem lock;
40 	unsigned int addrmask;
41 	unsigned char qspi_slave_latency;
42 #if defined(CONFIG_NRF70_ON_QSPI) && (NRF_QSPI_HAS_XIP_ENC || NRF_QSPI_HAS_DMA_ENC)
43 	nrf_qspi_encryption_t p_cfg;
44 #endif /*CONFIG_NRF70_ON_QSPI && (NRF_QSPI_HAS_XIP_ENC || NRF_QSPI_HAS_DMA_ENC)*/
45 	int test_hlread;
46 	char *test_name;
47 	int test_start;
48 	int test_end;
49 	int test_iterations;
50 	int test_timediff_read;
51 	int test_timediff_write;
52 	int test_status;
53 	int test_iteration;
54 };
55 struct qspi_dev {
56 	int (*deinit)(void);
57 	void *config;
58 	int (*init)(struct qspi_config *config);
59 	int (*write)(unsigned int addr, const void *data, int len);
60 	int (*read)(unsigned int addr, void *data, int len);
61 	int (*hl_read)(unsigned int addr, void *data, int len);
62 	void (*hard_reset)(void);
63 };
64 
65 int qspi_cmd_wakeup_rpu(const struct device *dev, uint8_t data);
66 
67 int qspi_init(struct qspi_config *config);
68 
69 int qspi_write(unsigned int addr, const void *data, int len);
70 
71 int qspi_read(unsigned int addr, void *data, int len);
72 
73 int qspi_hl_read(unsigned int addr, void *data, int len);
74 
75 int qspi_deinit(void);
76 
77 void gpio_free_irq(int pin, struct gpio_callback *button_cb_data);
78 
79 int gpio_request_irq(int pin, struct gpio_callback *button_cb_data, void (*irq_handler)());
80 
81 struct qspi_config *qspi_defconfig(void);
82 
83 struct qspi_dev *qspi_dev(void);
84 struct qspi_config *qspi_get_config(void);
85 
86 int qspi_cmd_sleep_rpu(const struct device *dev);
87 
88 void hard_reset(void);
89 void get_sleep_stats(uint32_t addr, uint32_t *buff, uint32_t wrd_len);
90 
91 extern struct device qspi_perip;
92 
93 int qspi_validate_rpu_wake_writecmd(const struct device *dev);
94 int qspi_cmd_wakeup_rpu(const struct device *dev, uint8_t data);
95 int qspi_wait_while_rpu_awake(const struct device *dev);
96 
97 int qspi_RDSR1(const struct device *dev, uint8_t *rdsr1);
98 int qspi_RDSR2(const struct device *dev, uint8_t *rdsr2);
99 int qspi_WRSR2(const struct device *dev, const uint8_t wrsr2);
100 
101 #ifdef CONFIG_NRF_WIFI_LOW_POWER
102 int func_rpu_sleep(void);
103 int func_rpu_wake(void);
104 int func_rpu_sleep_status(void);
105 #endif /* CONFIG_NRF_WIFI_LOW_POWER */
106 
107 #define QSPI_KEY_LEN_BYTES 16
108 
109 /*! \brief Enable encryption
110  *
111  *  \param key Pointer to the 128-bit key
112  *  \return 0 on success, negative errno code on failure.
113  */
114 int qspi_enable_encryption(uint8_t *key);
115 
116 #endif /* __QSPI_IF_H__ */
117