diff options
Diffstat (limited to 'lib/chibios-contrib/ext/mcux-sdk/components/flash/mflash/mimxrt1064/mflash_drv.c')
-rw-r--r-- | lib/chibios-contrib/ext/mcux-sdk/components/flash/mflash/mimxrt1064/mflash_drv.c | 544 |
1 files changed, 544 insertions, 0 deletions
diff --git a/lib/chibios-contrib/ext/mcux-sdk/components/flash/mflash/mimxrt1064/mflash_drv.c b/lib/chibios-contrib/ext/mcux-sdk/components/flash/mflash/mimxrt1064/mflash_drv.c new file mode 100644 index 000000000..f0e14255e --- /dev/null +++ b/lib/chibios-contrib/ext/mcux-sdk/components/flash/mflash/mimxrt1064/mflash_drv.c | |||
@@ -0,0 +1,544 @@ | |||
1 | /* | ||
2 | * Copyright 2018-2020 NXP | ||
3 | * | ||
4 | * SPDX-License-Identifier: BSD-3-Clause | ||
5 | */ | ||
6 | |||
7 | #include <stdbool.h> | ||
8 | |||
9 | #include "mflash_drv.h" | ||
10 | |||
11 | #include "fsl_flexspi.h" | ||
12 | #include "fsl_cache.h" | ||
13 | #include "pin_mux.h" | ||
14 | |||
15 | #define NOR_CMD_LUT_SEQ_IDX_READ_NORMAL 2 | ||
16 | #define NOR_CMD_LUT_SEQ_IDX_READ_FAST 1 | ||
17 | #define NOR_CMD_LUT_SEQ_IDX_READ_FAST_QUAD 0 // set it to index0 to align with xip settings | ||
18 | #define NOR_CMD_LUT_SEQ_IDX_READSTATUS 3 | ||
19 | #define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE 4 | ||
20 | #define NOR_CMD_LUT_SEQ_IDX_ERASESECTOR 5 | ||
21 | #define NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_SINGLE 6 | ||
22 | #define NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_QUAD 7 | ||
23 | #define NOR_CMD_LUT_SEQ_IDX_READID 8 | ||
24 | #define NOR_CMD_LUT_SEQ_IDX_WRITESTATUSREG 9 | ||
25 | #define NOR_CMD_LUT_SEQ_IDX_ENTERQPI 10 | ||
26 | #define NOR_CMD_LUT_SEQ_IDX_EXITQPI 11 | ||
27 | #define NOR_CMD_LUT_SEQ_IDX_READSTATUSREG 12 | ||
28 | |||
29 | #define CUSTOM_LUT_LENGTH 60 | ||
30 | #define FLASH_BUSY_STATUS_POL 1 | ||
31 | #define FLASH_BUSY_STATUS_OFFSET 0 | ||
32 | |||
33 | #define FLASH_SIZE 0x4000 | ||
34 | |||
35 | flexspi_device_config_t deviceconfig = { | ||
36 | .flexspiRootClk = 100000000, | ||
37 | .flashSize = FLASH_SIZE, | ||
38 | .CSIntervalUnit = kFLEXSPI_CsIntervalUnit1SckCycle, | ||
39 | .CSInterval = 2, | ||
40 | .CSHoldTime = 3, | ||
41 | .CSSetupTime = 3, | ||
42 | .dataValidTime = 0, | ||
43 | .columnspace = 0, | ||
44 | .enableWordAddress = 0, | ||
45 | .AWRSeqIndex = 0, | ||
46 | .AWRSeqNumber = 0, | ||
47 | .ARDSeqIndex = NOR_CMD_LUT_SEQ_IDX_READ_FAST_QUAD, | ||
48 | .ARDSeqNumber = 1, | ||
49 | .AHBWriteWaitUnit = kFLEXSPI_AhbWriteWaitUnit2AhbCycle, | ||
50 | .AHBWriteWaitInterval = 0, | ||
51 | }; | ||
52 | |||
53 | static uint32_t customLUT[CUSTOM_LUT_LENGTH] = { | ||
54 | /* Normal read mode -SDR */ | ||
55 | [4 * NOR_CMD_LUT_SEQ_IDX_READ_NORMAL] = | ||
56 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x03, kFLEXSPI_Command_RADDR_SDR, kFLEXSPI_1PAD, 0x18), | ||
57 | [4 * NOR_CMD_LUT_SEQ_IDX_READ_NORMAL + 1] = | ||
58 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_READ_SDR, kFLEXSPI_1PAD, 0x04, kFLEXSPI_Command_STOP, kFLEXSPI_1PAD, 0), | ||
59 | |||
60 | /* Fast read mode - SDR */ | ||
61 | [4 * NOR_CMD_LUT_SEQ_IDX_READ_FAST] = | ||
62 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x0B, kFLEXSPI_Command_RADDR_SDR, kFLEXSPI_1PAD, 0x18), | ||
63 | [4 * NOR_CMD_LUT_SEQ_IDX_READ_FAST + 1] = FLEXSPI_LUT_SEQ( | ||
64 | kFLEXSPI_Command_DUMMY_SDR, kFLEXSPI_1PAD, 0x08, kFLEXSPI_Command_READ_SDR, kFLEXSPI_1PAD, 0x04), | ||
65 | |||
66 | /* Fast read quad mode - SDR */ | ||
67 | [4 * NOR_CMD_LUT_SEQ_IDX_READ_FAST_QUAD] = | ||
68 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0xEB, kFLEXSPI_Command_RADDR_SDR, kFLEXSPI_4PAD, 0x18), | ||
69 | [4 * NOR_CMD_LUT_SEQ_IDX_READ_FAST_QUAD + 1] = FLEXSPI_LUT_SEQ( | ||
70 | kFLEXSPI_Command_DUMMY_SDR, kFLEXSPI_4PAD, 0x06, kFLEXSPI_Command_READ_SDR, kFLEXSPI_4PAD, 0x04), | ||
71 | |||
72 | /* Read extend parameters */ | ||
73 | [4 * NOR_CMD_LUT_SEQ_IDX_READSTATUS] = | ||
74 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x81, kFLEXSPI_Command_READ_SDR, kFLEXSPI_1PAD, 0x04), | ||
75 | |||
76 | /* Write Enable */ | ||
77 | [4 * NOR_CMD_LUT_SEQ_IDX_WRITEENABLE] = | ||
78 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x06, kFLEXSPI_Command_STOP, kFLEXSPI_1PAD, 0), | ||
79 | |||
80 | /* Erase Sector */ | ||
81 | [4 * NOR_CMD_LUT_SEQ_IDX_ERASESECTOR] = | ||
82 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x20, kFLEXSPI_Command_RADDR_SDR, kFLEXSPI_1PAD, 0x18), | ||
83 | |||
84 | /* Page Program - single mode */ | ||
85 | [4 * NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_SINGLE] = | ||
86 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x02, kFLEXSPI_Command_RADDR_SDR, kFLEXSPI_1PAD, 0x18), | ||
87 | [4 * NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_SINGLE + 1] = | ||
88 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_WRITE_SDR, kFLEXSPI_1PAD, 0x04, kFLEXSPI_Command_STOP, kFLEXSPI_1PAD, 0), | ||
89 | |||
90 | /* Page Program - quad mode */ | ||
91 | [4 * NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_QUAD] = | ||
92 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x32, kFLEXSPI_Command_RADDR_SDR, kFLEXSPI_1PAD, 0x18), | ||
93 | [4 * NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_QUAD + 1] = | ||
94 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_WRITE_SDR, kFLEXSPI_4PAD, 0x04, kFLEXSPI_Command_STOP, kFLEXSPI_1PAD, 0), | ||
95 | |||
96 | /* Read ID */ | ||
97 | [4 * NOR_CMD_LUT_SEQ_IDX_READID] = | ||
98 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0xAB, kFLEXSPI_Command_DUMMY_SDR, kFLEXSPI_1PAD, 0x18), | ||
99 | [4 * NOR_CMD_LUT_SEQ_IDX_READID + 1] = | ||
100 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_READ_SDR, kFLEXSPI_1PAD, 0x04, kFLEXSPI_Command_STOP, kFLEXSPI_1PAD, 0), | ||
101 | |||
102 | /* Enable Quad mode */ | ||
103 | [4 * NOR_CMD_LUT_SEQ_IDX_WRITESTATUSREG] = | ||
104 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x01, kFLEXSPI_Command_WRITE_SDR, kFLEXSPI_1PAD, 0x04), | ||
105 | |||
106 | /* Enter QPI mode */ | ||
107 | [4 * NOR_CMD_LUT_SEQ_IDX_ENTERQPI] = | ||
108 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x35, kFLEXSPI_Command_STOP, kFLEXSPI_1PAD, 0), | ||
109 | |||
110 | /* Exit QPI mode */ | ||
111 | [4 * NOR_CMD_LUT_SEQ_IDX_EXITQPI] = | ||
112 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_4PAD, 0xF5, kFLEXSPI_Command_STOP, kFLEXSPI_1PAD, 0), | ||
113 | |||
114 | /* Read status register */ | ||
115 | [4 * NOR_CMD_LUT_SEQ_IDX_READSTATUSREG] = | ||
116 | FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x05, kFLEXSPI_Command_READ_SDR, kFLEXSPI_1PAD, 0x04), | ||
117 | }; | ||
118 | |||
119 | static status_t flexspi_nor_wait_bus_busy(FLEXSPI_Type *base) | ||
120 | { | ||
121 | /* Wait status ready. */ | ||
122 | bool isBusy; | ||
123 | uint32_t readValue; | ||
124 | status_t status; | ||
125 | flexspi_transfer_t flashXfer; | ||
126 | |||
127 | flashXfer.deviceAddress = 0; | ||
128 | flashXfer.port = kFLEXSPI_PortA1; | ||
129 | flashXfer.cmdType = kFLEXSPI_Read; | ||
130 | flashXfer.SeqNumber = 1; | ||
131 | flashXfer.seqIndex = NOR_CMD_LUT_SEQ_IDX_READSTATUSREG; | ||
132 | flashXfer.data = &readValue; | ||
133 | flashXfer.dataSize = 1; | ||
134 | |||
135 | do | ||
136 | { | ||
137 | status = FLEXSPI_TransferBlocking(base, &flashXfer); | ||
138 | |||
139 | if (status != kStatus_Success) | ||
140 | { | ||
141 | return status; | ||
142 | } | ||
143 | if (FLASH_BUSY_STATUS_POL) | ||
144 | { | ||
145 | if (readValue & (1U << FLASH_BUSY_STATUS_OFFSET)) | ||
146 | { | ||
147 | isBusy = true; | ||
148 | } | ||
149 | else | ||
150 | { | ||
151 | isBusy = false; | ||
152 | } | ||
153 | } | ||
154 | else | ||
155 | { | ||
156 | if (readValue & (1U << FLASH_BUSY_STATUS_OFFSET)) | ||
157 | { | ||
158 | isBusy = false; | ||
159 | } | ||
160 | else | ||
161 | { | ||
162 | isBusy = true; | ||
163 | } | ||
164 | } | ||
165 | |||
166 | } while (isBusy); | ||
167 | |||
168 | return status; | ||
169 | } | ||
170 | |||
171 | static status_t flexspi_nor_write_enable(FLEXSPI_Type *base, uint32_t address) | ||
172 | { | ||
173 | flexspi_transfer_t flashXfer; | ||
174 | status_t status; | ||
175 | |||
176 | /* Write neable */ | ||
177 | flashXfer.deviceAddress = address; | ||
178 | flashXfer.port = kFLEXSPI_PortA1; | ||
179 | flashXfer.cmdType = kFLEXSPI_Command; | ||
180 | flashXfer.SeqNumber = 1; | ||
181 | flashXfer.seqIndex = NOR_CMD_LUT_SEQ_IDX_WRITEENABLE; | ||
182 | |||
183 | status = FLEXSPI_TransferBlocking(base, &flashXfer); | ||
184 | |||
185 | return status; | ||
186 | } | ||
187 | |||
188 | status_t flexspi_nor_enable_quad_mode(FLEXSPI_Type *base) | ||
189 | { | ||
190 | flexspi_transfer_t flashXfer; | ||
191 | status_t status; | ||
192 | uint32_t writeValue = 0x40; | ||
193 | |||
194 | /* Write neable */ | ||
195 | status = flexspi_nor_write_enable(base, 0); | ||
196 | |||
197 | if (status != kStatus_Success) | ||
198 | { | ||
199 | return status; | ||
200 | } | ||
201 | |||
202 | /* Enable quad mode. */ | ||
203 | flashXfer.deviceAddress = 0; | ||
204 | flashXfer.port = kFLEXSPI_PortA1; | ||
205 | flashXfer.cmdType = kFLEXSPI_Write; | ||
206 | flashXfer.SeqNumber = 1; | ||
207 | flashXfer.seqIndex = NOR_CMD_LUT_SEQ_IDX_WRITESTATUSREG; | ||
208 | flashXfer.data = &writeValue; | ||
209 | flashXfer.dataSize = 1; | ||
210 | |||
211 | status = FLEXSPI_TransferBlocking(base, &flashXfer); | ||
212 | if (status != kStatus_Success) | ||
213 | { | ||
214 | return status; | ||
215 | } | ||
216 | |||
217 | status = flexspi_nor_wait_bus_busy(base); | ||
218 | |||
219 | return status; | ||
220 | } | ||
221 | |||
222 | static status_t flexspi_nor_flash_sector_erase(FLEXSPI_Type *base, uint32_t address) | ||
223 | { | ||
224 | status_t status; | ||
225 | flexspi_transfer_t flashXfer; | ||
226 | |||
227 | /* Write enable */ | ||
228 | status = flexspi_nor_write_enable(base, address); | ||
229 | |||
230 | if (status != kStatus_Success) | ||
231 | { | ||
232 | return status; | ||
233 | } | ||
234 | |||
235 | flashXfer.deviceAddress = address; | ||
236 | flashXfer.port = kFLEXSPI_PortA1; | ||
237 | flashXfer.cmdType = kFLEXSPI_Command; | ||
238 | flashXfer.SeqNumber = 1; | ||
239 | flashXfer.seqIndex = NOR_CMD_LUT_SEQ_IDX_ERASESECTOR; | ||
240 | status = FLEXSPI_TransferBlocking(base, &flashXfer); | ||
241 | |||
242 | if (status != kStatus_Success) | ||
243 | { | ||
244 | return status; | ||
245 | } | ||
246 | |||
247 | status = flexspi_nor_wait_bus_busy(base); | ||
248 | |||
249 | return status; | ||
250 | } | ||
251 | |||
252 | static status_t flexspi_nor_flash_page_program(FLEXSPI_Type *base, uint32_t address, const uint32_t *src) | ||
253 | { | ||
254 | status_t status; | ||
255 | flexspi_transfer_t flashXfer; | ||
256 | |||
257 | /* Write enable */ | ||
258 | status = flexspi_nor_write_enable(base, address); | ||
259 | |||
260 | if (status != kStatus_Success) | ||
261 | { | ||
262 | return status; | ||
263 | } | ||
264 | |||
265 | /* Prepare page program command */ | ||
266 | flashXfer.deviceAddress = address; | ||
267 | flashXfer.port = kFLEXSPI_PortA1; | ||
268 | flashXfer.cmdType = kFLEXSPI_Write; | ||
269 | flashXfer.SeqNumber = 1; | ||
270 | flashXfer.seqIndex = NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_QUAD; | ||
271 | flashXfer.data = (uint32_t *)src; | ||
272 | flashXfer.dataSize = MFLASH_PAGE_SIZE; | ||
273 | status = FLEXSPI_TransferBlocking(base, &flashXfer); | ||
274 | |||
275 | if (status != kStatus_Success) | ||
276 | { | ||
277 | return status; | ||
278 | } | ||
279 | |||
280 | status = flexspi_nor_wait_bus_busy(base); | ||
281 | |||
282 | return status; | ||
283 | } | ||
284 | |||
285 | static status_t flexspi_nor_read_data(FLEXSPI_Type *base, uint32_t startAddress, uint32_t *buffer, uint32_t length) | ||
286 | { | ||
287 | status_t status; | ||
288 | flexspi_transfer_t flashXfer; | ||
289 | uint32_t readAddress = startAddress; | ||
290 | |||
291 | /* Read page. */ | ||
292 | flashXfer.deviceAddress = readAddress; | ||
293 | flashXfer.port = kFLEXSPI_PortA1; | ||
294 | flashXfer.cmdType = kFLEXSPI_Read; | ||
295 | flashXfer.SeqNumber = 1; | ||
296 | flashXfer.seqIndex = NOR_CMD_LUT_SEQ_IDX_READ_FAST_QUAD; | ||
297 | flashXfer.data = buffer; | ||
298 | flashXfer.dataSize = length; | ||
299 | |||
300 | status = FLEXSPI_TransferBlocking(base, &flashXfer); | ||
301 | |||
302 | return status; | ||
303 | } | ||
304 | |||
305 | /* Initialize flash peripheral, | ||
306 | * cannot be invoked directly, requires calling wrapper in non XIP memory */ | ||
307 | static int32_t mflash_drv_init_internal(void) | ||
308 | { | ||
309 | /* NOTE: Multithread access is not supported for SRAM target. | ||
310 | * XIP target MUST be protected by disabling global interrupts | ||
311 | * since all ISR (and API that is used inside) is placed at XIP. | ||
312 | * It is necessary to place at least "mflash_drv.o", "fsl_flexspi.o" to SRAM */ | ||
313 | /* disable interrupts when running from XIP */ | ||
314 | uint32_t primask = __get_PRIMASK(); | ||
315 | |||
316 | __asm("cpsid i"); | ||
317 | |||
318 | flexspi_config_t config; | ||
319 | status_t status; | ||
320 | |||
321 | /* Get FLEXSPI default settings and configure the flexspi. */ | ||
322 | FLEXSPI_GetDefaultConfig(&config); | ||
323 | |||
324 | /* Set AHB buffer size for reading data through AHB bus. */ | ||
325 | config.ahbConfig.enableAHBPrefetch = true; | ||
326 | config.ahbConfig.enableAHBBufferable = true; | ||
327 | config.ahbConfig.enableAHBCachable = true; | ||
328 | config.rxSampleClock = kFLEXSPI_ReadSampleClkLoopbackFromDqsPad; | ||
329 | FLEXSPI_Init(MFLASH_FLEXSPI, &config); | ||
330 | |||
331 | /* AHB Read Address option bit. This option bit is intend to remove AHB burst start address alignment limitation */ | ||
332 | MFLASH_FLEXSPI->AHBCR |= FLEXSPI_AHBCR_READADDROPT_MASK; | ||
333 | |||
334 | /* Configure flash settings according to serial flash feature. */ | ||
335 | FLEXSPI_SetFlashConfig(MFLASH_FLEXSPI, &deviceconfig, kFLEXSPI_PortA1); | ||
336 | |||
337 | /* Update LUT table. */ | ||
338 | FLEXSPI_UpdateLUT(MFLASH_FLEXSPI, 0, customLUT, CUSTOM_LUT_LENGTH); | ||
339 | |||
340 | /* Enter quad mode. */ | ||
341 | status = flexspi_nor_enable_quad_mode(MFLASH_FLEXSPI); | ||
342 | |||
343 | if (primask == 0) | ||
344 | { | ||
345 | __asm("cpsie i"); | ||
346 | } | ||
347 | |||
348 | return status; | ||
349 | } | ||
350 | |||
351 | /* API - initialize 'mflash' */ | ||
352 | int32_t mflash_drv_init(void) | ||
353 | { | ||
354 | /* Necessary to have double wrapper call in non_xip memory */ | ||
355 | return mflash_drv_init_internal(); | ||
356 | } | ||
357 | |||
358 | /* Internal - erase single sector */ | ||
359 | static int32_t mflash_drv_sector_erase_internal(uint32_t sector_addr) | ||
360 | { | ||
361 | status_t status; | ||
362 | uint32_t primask = __get_PRIMASK(); | ||
363 | |||
364 | __asm("cpsid i"); | ||
365 | |||
366 | status = flexspi_nor_flash_sector_erase(MFLASH_FLEXSPI, sector_addr); | ||
367 | |||
368 | /* Do software reset. */ | ||
369 | FLEXSPI_SoftwareReset(MFLASH_FLEXSPI); | ||
370 | |||
371 | DCACHE_InvalidateByRange(MFLASH_BASE_ADDRESS + sector_addr, MFLASH_SECTOR_SIZE); | ||
372 | |||
373 | if (primask == 0) | ||
374 | { | ||
375 | __asm("cpsie i"); | ||
376 | } | ||
377 | |||
378 | /* Flush pipeline to allow pending interrupts take place | ||
379 | * before starting next loop */ | ||
380 | __ISB(); | ||
381 | |||
382 | return status; | ||
383 | } | ||
384 | |||
385 | /* Calling wrapper for 'mflash_drv_sector_erase_internal'. | ||
386 | * Erase one sector starting at 'sector_addr' - must be sector aligned. | ||
387 | */ | ||
388 | int32_t mflash_drv_sector_erase(uint32_t sector_addr) | ||
389 | { | ||
390 | if (0 == mflash_drv_is_sector_aligned(sector_addr)) | ||
391 | return kStatus_InvalidArgument; | ||
392 | |||
393 | return mflash_drv_sector_erase_internal(sector_addr); | ||
394 | } | ||
395 | |||
396 | /* Internal - write single page */ | ||
397 | static int32_t mflash_drv_page_program_internal(uint32_t page_addr, uint32_t *data) | ||
398 | { | ||
399 | uint32_t primask = __get_PRIMASK(); | ||
400 | |||
401 | __asm("cpsid i"); | ||
402 | |||
403 | status_t status; | ||
404 | status = flexspi_nor_flash_page_program(MFLASH_FLEXSPI, page_addr, data); | ||
405 | |||
406 | /* Do software reset. */ | ||
407 | FLEXSPI_SoftwareReset(MFLASH_FLEXSPI); | ||
408 | |||
409 | DCACHE_InvalidateByRange(MFLASH_BASE_ADDRESS + page_addr, MFLASH_PAGE_SIZE); | ||
410 | |||
411 | if (primask == 0) | ||
412 | { | ||
413 | __asm("cpsie i"); | ||
414 | } | ||
415 | |||
416 | /* Flush pipeline to allow pending interrupts take place | ||
417 | * before starting next loop */ | ||
418 | __ISB(); | ||
419 | |||
420 | return status; | ||
421 | } | ||
422 | |||
423 | /* Calling wrapper for 'mflash_drv_page_program_internal'. | ||
424 | * Write 'data' to 'page_addr' - must be page aligned. | ||
425 | * NOTE: Don't try to store constant data that are located in XIP !! | ||
426 | */ | ||
427 | int32_t mflash_drv_page_program(uint32_t page_addr, uint32_t *data) | ||
428 | { | ||
429 | if (0 == mflash_drv_is_page_aligned(page_addr)) | ||
430 | return kStatus_InvalidArgument; | ||
431 | |||
432 | return mflash_drv_page_program_internal(page_addr, data); | ||
433 | } | ||
434 | |||
435 | /* Internal - read data */ | ||
436 | static int32_t mflash_drv_read_internal(uint32_t addr, uint32_t *buffer, uint32_t len) | ||
437 | { | ||
438 | uint32_t primask = __get_PRIMASK(); | ||
439 | |||
440 | __asm("cpsid i"); | ||
441 | |||
442 | status_t status; | ||
443 | status = flexspi_nor_read_data(MFLASH_FLEXSPI, addr, buffer, len); | ||
444 | |||
445 | /* Do software reset. */ | ||
446 | FLEXSPI_SoftwareReset(MFLASH_FLEXSPI); | ||
447 | |||
448 | if (primask == 0) | ||
449 | { | ||
450 | __asm("cpsie i"); | ||
451 | } | ||
452 | |||
453 | /* Flush pipeline to allow pending interrupts take place | ||
454 | * before starting next loop */ | ||
455 | __ISB(); | ||
456 | |||
457 | return status; | ||
458 | } | ||
459 | |||
460 | /* Calling wrapper for 'mflash_drv_read_internal'. */ | ||
461 | int32_t mflash_drv_read(uint32_t addr, uint32_t *buffer, uint32_t len) | ||
462 | { | ||
463 | /* Check alignment */ | ||
464 | if (((uint32_t)buffer % 4) || (len % 4)) | ||
465 | return kStatus_InvalidArgument; | ||
466 | |||
467 | return mflash_drv_read_internal(addr, buffer, len); | ||
468 | } | ||
469 | |||
470 | /* Returns pointer (AHB address) to memory area where the specified region of FLASH is mapped, NULL on failure (could | ||
471 | * not map continuous block) */ | ||
472 | void *mflash_drv_phys2log(uint32_t addr, uint32_t len) | ||
473 | { | ||
474 | /* take FLEXSPI remapping into account */ | ||
475 | uint32_t remap_offset = IOMUXC_GPR->GPR32 & 0xFFFFF000; | ||
476 | uint32_t remap_start = IOMUXC_GPR->GPR30 & 0xFFFFF000; | ||
477 | uint32_t remap_end = IOMUXC_GPR->GPR31 & 0xFFFFF000; | ||
478 | |||
479 | /* calculate the bus address where the requested FLASH region is expected to be available */ | ||
480 | uint32_t bus_addr = addr + MFLASH_BASE_ADDRESS; | ||
481 | |||
482 | if (remap_offset == 0 || (remap_end <= remap_start)) | ||
483 | { | ||
484 | /* remapping is not active */ | ||
485 | return (void *)bus_addr; | ||
486 | } | ||
487 | |||
488 | if ((remap_start >= bus_addr + len) || (remap_end <= bus_addr)) | ||
489 | { | ||
490 | /* remapping window does not collide with bus addresses normally assigned for requested range of FLASH */ | ||
491 | return (void *)bus_addr; | ||
492 | } | ||
493 | |||
494 | if ((remap_start + remap_offset <= bus_addr) && (remap_end + remap_offset >= bus_addr + len)) | ||
495 | { | ||
496 | /* remapping window coveres the whole requested range of FLASH, return address adjusted by negative offset */ | ||
497 | return (void *)(bus_addr - remap_offset); | ||
498 | } | ||
499 | |||
500 | /* the bus address region normally assigned for requested range of FLASH is partially or completely shadowed by | ||
501 | * remapping, fail */ | ||
502 | return NULL; | ||
503 | } | ||
504 | |||
505 | /* Returns address of physical memory where the area accessible by given pointer is actually stored, UINT32_MAX on | ||
506 | * failure (could not map as continuous block) */ | ||
507 | uint32_t mflash_drv_log2phys(void *ptr, uint32_t len) | ||
508 | { | ||
509 | /* take FLEXSPI remapping into account */ | ||
510 | uint32_t remap_offset = IOMUXC_GPR->GPR32 & 0xFFFFF000; | ||
511 | uint32_t remap_start = IOMUXC_GPR->GPR30 & 0xFFFFF000; | ||
512 | uint32_t remap_end = IOMUXC_GPR->GPR31 & 0xFFFFF000; | ||
513 | |||
514 | /* calculate the bus address where the requested FLASH region is expected to be available */ | ||
515 | uint32_t bus_addr = (uint32_t)ptr; | ||
516 | |||
517 | if (bus_addr < MFLASH_BASE_ADDRESS) | ||
518 | { | ||
519 | /* the pointer points outside of the flash memory area */ | ||
520 | return UINT32_MAX; | ||
521 | } | ||
522 | |||
523 | if (remap_offset == 0 || (remap_end <= remap_start)) | ||
524 | { | ||
525 | /* remapping is not active */ | ||
526 | return (bus_addr - MFLASH_BASE_ADDRESS); | ||
527 | } | ||
528 | |||
529 | if ((remap_start >= bus_addr + len) || (remap_end <= bus_addr)) | ||
530 | { | ||
531 | /* remapping window does not affect the requested memory area */ | ||
532 | return (bus_addr - MFLASH_BASE_ADDRESS); | ||
533 | } | ||
534 | |||
535 | if ((remap_start <= bus_addr) && (remap_end >= bus_addr + len)) | ||
536 | { | ||
537 | /* remapping window coveres the whole address range, return address adjusted by offset */ | ||
538 | return (bus_addr + remap_offset - MFLASH_BASE_ADDRESS); | ||
539 | } | ||
540 | |||
541 | /* the bus address region partially collides with the remapping window, hence the range is not mapped to continuous | ||
542 | * block in the FLASH, fail */ | ||
543 | return UINT32_MAX; | ||
544 | } | ||