1 /*
2  * Copyright 2021 Basalte bv
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT   nxp_imx_flexspi_s27ks0641
8 
9 #include <zephyr/logging/log.h>
10 #include <zephyr/sys/util.h>
11 
12 #include "memc_mcux_flexspi.h"
13 
14 
15 /*
16  * NOTE: If CONFIG_FLASH_MCUX_FLEXSPI_XIP is selected, Any external functions
17  * called while interacting with the flexspi MUST be relocated to SRAM or ITCM
18  * at runtime, so that the chip does not access the flexspi to read program
19  * instructions while it is being written to
20  */
21 #if defined(CONFIG_FLASH_MCUX_FLEXSPI_XIP) && (CONFIG_MEMC_LOG_LEVEL > 0)
22 #warning "Enabling memc driver logging and XIP mode simultaneously can cause \
23 	read-while-write hazards. This configuration is not recommended."
24 #endif
25 
26 LOG_MODULE_REGISTER(memc_flexspi_s27ks0641, CONFIG_MEMC_LOG_LEVEL);
27 
28 enum {
29 	READ_DATA,
30 	WRITE_DATA,
31 	READ_REG,
32 	WRITE_REG,
33 };
34 
35 struct memc_flexspi_s27ks0641_config {
36 	flexspi_port_t port;
37 	flexspi_device_config_t config;
38 };
39 
40 /* Device variables used in critical sections should be in this structure */
41 struct memc_flexspi_s27ks0641_data {
42 	const struct device *controller;
43 };
44 
45 static const uint32_t memc_flexspi_s27ks0641_lut[][4] = {
46 	/* Read Data */
47 	[READ_DATA] = {
48 		FLEXSPI_LUT_SEQ(kFLEXSPI_Command_DDR,             kFLEXSPI_8PAD, 0xA0,
49 				kFLEXSPI_Command_RADDR_DDR,       kFLEXSPI_8PAD, 0x18),
50 		FLEXSPI_LUT_SEQ(kFLEXSPI_Command_CADDR_DDR,       kFLEXSPI_8PAD, 0x10,
51 				kFLEXSPI_Command_DUMMY_RWDS_DDR,  kFLEXSPI_8PAD, 0x06),
52 		FLEXSPI_LUT_SEQ(kFLEXSPI_Command_READ_DDR,        kFLEXSPI_8PAD, 0x04,
53 				kFLEXSPI_Command_STOP,            kFLEXSPI_1PAD, 0x00),
54 	},
55 
56 	/* Write Data */
57 	[WRITE_DATA] = {
58 		FLEXSPI_LUT_SEQ(kFLEXSPI_Command_DDR,             kFLEXSPI_8PAD, 0x20,
59 				kFLEXSPI_Command_RADDR_DDR,       kFLEXSPI_8PAD, 0x18),
60 		FLEXSPI_LUT_SEQ(kFLEXSPI_Command_CADDR_DDR,       kFLEXSPI_8PAD, 0x10,
61 				kFLEXSPI_Command_DUMMY_RWDS_DDR,  kFLEXSPI_8PAD, 0x06),
62 		FLEXSPI_LUT_SEQ(kFLEXSPI_Command_WRITE_DDR,       kFLEXSPI_8PAD, 0x04,
63 				kFLEXSPI_Command_STOP,            kFLEXSPI_1PAD, 0x00),
64 	},
65 
66 	/* Read Register */
67 	[READ_REG] = {
68 		FLEXSPI_LUT_SEQ(kFLEXSPI_Command_DDR,             kFLEXSPI_8PAD, 0xE0,
69 				kFLEXSPI_Command_RADDR_DDR,       kFLEXSPI_8PAD, 0x18),
70 		FLEXSPI_LUT_SEQ(kFLEXSPI_Command_CADDR_DDR,       kFLEXSPI_8PAD, 0x10,
71 				kFLEXSPI_Command_DUMMY_RWDS_DDR,  kFLEXSPI_8PAD, 0x06),
72 		FLEXSPI_LUT_SEQ(kFLEXSPI_Command_READ_DDR,        kFLEXSPI_8PAD, 0x04,
73 				kFLEXSPI_Command_STOP,            kFLEXSPI_1PAD, 0x00),
74 	},
75 
76 	/* Write Register */
77 	[WRITE_REG] = {
78 		FLEXSPI_LUT_SEQ(kFLEXSPI_Command_DDR,             kFLEXSPI_8PAD, 0x60,
79 				kFLEXSPI_Command_RADDR_DDR,       kFLEXSPI_8PAD, 0x18),
80 		FLEXSPI_LUT_SEQ(kFLEXSPI_Command_CADDR_DDR,       kFLEXSPI_8PAD, 0x10,
81 				kFLEXSPI_Command_DUMMY_RWDS_DDR,  kFLEXSPI_8PAD, 0x06),
82 		FLEXSPI_LUT_SEQ(kFLEXSPI_Command_WRITE_DDR,       kFLEXSPI_8PAD, 0x04,
83 				kFLEXSPI_Command_STOP,            kFLEXSPI_1PAD, 0x00),
84 	},
85 };
86 
memc_flexspi_s27ks0641_get_vendor_id(const struct device * dev,uint16_t * vendor_id)87 static int memc_flexspi_s27ks0641_get_vendor_id(const struct device *dev,
88 						uint16_t *vendor_id)
89 {
90 	const struct memc_flexspi_s27ks0641_config *config = dev->config;
91 	struct memc_flexspi_s27ks0641_data *data = dev->data;
92 	uint32_t buffer = 0;
93 	int ret;
94 
95 	flexspi_transfer_t transfer = {
96 		.deviceAddress = 0,
97 		.port = config->port,
98 		.cmdType = kFLEXSPI_Read,
99 		.SeqNumber = 1,
100 		.seqIndex = READ_REG,
101 		.data = &buffer,
102 		.dataSize = 4,
103 	};
104 
105 	LOG_DBG("Reading id");
106 
107 	ret = memc_flexspi_transfer(data->controller, &transfer);
108 	*vendor_id = buffer & 0xffff;
109 
110 	return ret;
111 }
112 
memc_flexspi_s27ks0641_init(const struct device * dev)113 static int memc_flexspi_s27ks0641_init(const struct device *dev)
114 {
115 	const struct memc_flexspi_s27ks0641_config *config = dev->config;
116 	struct memc_flexspi_s27ks0641_data *data = dev->data;
117 	uint16_t vendor_id;
118 
119 	if (!device_is_ready(data->controller)) {
120 		LOG_ERR("Controller device not ready");
121 		return -ENODEV;
122 	}
123 
124 	if (memc_flexspi_set_device_config(data->controller, &config->config,
125 					   config->port)) {
126 		LOG_ERR("Could not set device configuration");
127 		return -EINVAL;
128 	}
129 
130 	if (memc_flexspi_update_lut(data->controller, 0,
131 				    (const uint32_t *) memc_flexspi_s27ks0641_lut,
132 				    sizeof(memc_flexspi_s27ks0641_lut) / 4)) {
133 		LOG_ERR("Could not update lut");
134 		return -EINVAL;
135 	}
136 
137 	memc_flexspi_reset(data->controller);
138 
139 	if (memc_flexspi_s27ks0641_get_vendor_id(dev, &vendor_id)) {
140 		LOG_ERR("Could not read vendor id");
141 		return -EIO;
142 	}
143 	LOG_DBG("Vendor id: 0x%0x", vendor_id);
144 
145 	return 0;
146 }
147 
148 #define CONCAT3(x, y, z) x ## y ## z
149 
150 #define CS_INTERVAL_UNIT(unit) \
151 	CONCAT3(kFLEXSPI_CsIntervalUnit, unit, SckCycle)
152 
153 #define AHB_WRITE_WAIT_UNIT(unit) \
154 	CONCAT3(kFLEXSPI_AhbWriteWaitUnit, unit, AhbCycle)
155 
156 #define MEMC_FLEXSPI_DEVICE_CONFIG(n)					\
157 	{								\
158 		.flexspiRootClk = MHZ(332),				\
159 		.isSck2Enabled = false,					\
160 		.flashSize = DT_INST_PROP(n, size) / 8 / KB(1),		\
161 		.CSIntervalUnit =					\
162 			CS_INTERVAL_UNIT(				\
163 				DT_INST_PROP(n, cs_interval_unit)),	\
164 		.CSInterval = DT_INST_PROP(n, cs_interval),		\
165 		.CSHoldTime = DT_INST_PROP(n, cs_hold_time),		\
166 		.CSSetupTime = DT_INST_PROP(n, cs_setup_time),		\
167 		.dataValidTime = DT_INST_PROP(n, data_valid_time),	\
168 		.columnspace = DT_INST_PROP(n, column_space),		\
169 		.enableWordAddress = DT_INST_PROP(n, word_addressable),	\
170 		.AWRSeqIndex = WRITE_DATA,				\
171 		.AWRSeqNumber = 1,					\
172 		.ARDSeqIndex = READ_DATA,				\
173 		.ARDSeqNumber = 1,					\
174 		.AHBWriteWaitUnit =					\
175 			AHB_WRITE_WAIT_UNIT(				\
176 				DT_INST_PROP(n, ahb_write_wait_unit)),	\
177 		.AHBWriteWaitInterval =					\
178 			DT_INST_PROP(n, ahb_write_wait_interval),	\
179 		.enableWriteMask = true,				\
180 	}								\
181 
182 #define MEMC_FLEXSPI_S27KS0641(n)				  \
183 	static const struct memc_flexspi_s27ks0641_config	  \
184 		memc_flexspi_s27ks0641_config_##n = {		  \
185 		.port = DT_INST_REG_ADDR(n),			  \
186 		.config = MEMC_FLEXSPI_DEVICE_CONFIG(n),	  \
187 	};							  \
188 								  \
189 	static struct memc_flexspi_s27ks0641_data		  \
190 		memc_flexspi_s27ks0641_data_##n = {		  \
191 		.controller = DEVICE_DT_GET(DT_INST_BUS(n)),	  \
192 	};							  \
193 								  \
194 	DEVICE_DT_INST_DEFINE(n,				  \
195 			      memc_flexspi_s27ks0641_init,	  \
196 			      NULL,				  \
197 			      &memc_flexspi_s27ks0641_data_##n,  \
198 			      &memc_flexspi_s27ks0641_config_##n,  \
199 			      POST_KERNEL,			  \
200 			      CONFIG_MEMC_INIT_PRIORITY, \
201 			      NULL);
202 
203 DT_INST_FOREACH_STATUS_OKAY(MEMC_FLEXSPI_S27KS0641)
204