Skip to content

Commit f854433

Browse files
committed
mimxrt/flexspi_nor_c: Set the flash CLK frequency on boot.
The flash clock frequency may have been set to a different value by a bootloader. Set the frequency according to the configured value. Actual MHz values set depending on the configuration: Configured Set 30 60 50 60 60 60 75 80 80 80 100 96 133 120 166 160 Code sample provided by CircuitPython. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent 12caa68 commit f854433

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

ports/mimxrt/flash.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
void flash_init(void) {
3030
// Upload the custom flash configuration
3131
// And fix the entry for PAGEPROGRAM_QUAD
32-
flexspi_nor_update_lut();
32+
// Update the flash CLK
33+
flexspi_nor_update_lut_clk();
3334

3435
// Configure FLEXSPI IP FIFO access.
3536
BOARD_FLEX_SPI->MCR0 &= ~(FLEXSPI_MCR0_ARDFEN_MASK);

ports/mimxrt/hal/flexspi_hyper_flash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "fsl_clock.h"
1010
#include "flexspi_hyper_flash.h"
1111

12-
void flexspi_nor_update_lut(void) {
12+
void flexspi_nor_update_lut_clk(void) {
1313
}
1414

1515
// Copy of a few (pseudo-)functions from fsl_clock.h, which were nor reliably

ports/mimxrt/hal/flexspi_nor_flash.c

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
#include "flexspi_nor_flash.h"
3939
#include "flexspi_flash_config.h"
4040

41+
bool flash_busy_status_pol = 0;
42+
bool flash_busy_status_offset = 0;
43+
4144
uint32_t LUT_pageprogram_quad[4] = {
4245
// 10 Page Program - quad mode
4346
FLEXSPI_LUT_SEQ(CMD_SDR, FLEXSPI_1PAD, 0x32, RADDR_SDR, FLEXSPI_1PAD, 24),
@@ -54,16 +57,65 @@ uint32_t LUT_write_status[4] = {
5457
FLEXSPI_LUT_SEQ(0, 0, 0, 0, 0, 0), // Filler
5558
};
5659

57-
void flexspi_nor_update_lut(void) {
60+
#if !defined(MIMXRT117x_SERIES)
61+
static uint16_t freq_table_mhz[] = {
62+
60, // Entry 0 to 2 are out of range
63+
60, // So it is set to 60
64+
60, // as lowest possible MHz value
65+
60, // -> 60 MHz
66+
75, // -> 80 MHz
67+
80, // -> 80 MHz
68+
100, // -> 96 Mhz
69+
133, // -> 120 MHz
70+
166 // -> 160 MHz
71+
};
72+
#endif
73+
74+
__attribute__((section(".ram_functions"))) void flexspi_nor_update_lut_clk(void) {
75+
// Create a local copy of the LookupTable. Modify the entry for WRITESTATUSREG
76+
// Add an entry for PAGEPROGRAM_QUAD.
5877
uint32_t lookuptable_copy[64];
5978
memcpy(lookuptable_copy, (const uint32_t *)&qspiflash_config.memConfig.lookupTable, 64 * sizeof(uint32_t));
60-
// write WRITESTATUSREG code to entry 10
79+
// write local WRITESTATUSREG code to index 4
6180
memcpy(&lookuptable_copy[NOR_CMD_LUT_SEQ_IDX_WRITESTATUSREG * 4],
6281
LUT_write_status, 4 * sizeof(uint32_t));
63-
// write PAGEPROGRAM_QUAD code to entry 10
82+
// write local PAGEPROGRAM_QUAD code to index 10
6483
memcpy(&lookuptable_copy[NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_QUAD * 4],
6584
LUT_pageprogram_quad, 4 * sizeof(uint32_t));
85+
// Update the LookupTable.
6686
FLEXSPI_UpdateLUT(BOARD_FLEX_SPI, 0, lookuptable_copy, 64);
87+
88+
#if !defined(MIMXRT117x_SERIES)
89+
// We pre-calculate the divider here as long as the freq_table_mhz is accessible.
90+
// The PFD is set below to 480 MHz.
91+
uint32_t freq_divider = (480 + freq_table_mhz[MICROPY_HW_FLASH_CLK] / 2) / freq_table_mhz[MICROPY_HW_FLASH_CLK] - 1;
92+
93+
__DSB();
94+
__ISB();
95+
__disable_irq();
96+
SCB_DisableDCache();
97+
while (!FLEXSPI_GetBusIdleStatus(BOARD_FLEX_SPI)) {
98+
}
99+
FLEXSPI_Enable(BOARD_FLEX_SPI, false);
100+
101+
// Disable FlexSPI clock
102+
CCM->CCGR6 &= ~CCM_CCGR6_CG5_MASK;
103+
// Changing the clock is OK now.
104+
// The PFD is 480 * 18 / PFD0_FRAC. We do / 18 which outputs 480 MHz.
105+
CCM_ANALOG->PFD_480 = (CCM_ANALOG->PFD_480 & ~CCM_ANALOG_PFD_480_TOG_PFD0_FRAC_MASK) | CCM_ANALOG_PFD_480_TOG_PFD0_FRAC(18);
106+
// This divides down the 480 Mhz by PODF + 1. So e.g. 480 / (3 + 1) = 120 MHz, 480 / (4 + 1) = 96 MHz.
107+
CCM->CSCMR1 = (CCM->CSCMR1 & ~CCM_CSCMR1_FLEXSPI_PODF_MASK) | CCM_CSCMR1_FLEXSPI_PODF(freq_divider);
108+
// Re-enable FlexSPI
109+
CCM->CCGR6 |= CCM_CCGR6_CG5_MASK;
110+
111+
FLEXSPI_Enable(BOARD_FLEX_SPI, true);
112+
FLEXSPI_SoftwareReset(BOARD_FLEX_SPI);
113+
while (!FLEXSPI_GetBusIdleStatus(BOARD_FLEX_SPI)) {
114+
}
115+
116+
SCB_EnableDCache();
117+
__enable_irq();
118+
#endif
67119
}
68120

69121
void flexspi_nor_reset(FLEXSPI_Type *base) __attribute__((section(".ram_functions")));

ports/mimxrt/hal/flexspi_nor_flash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extern flexspi_nor_config_t qspiflash_config;
4646

4747
status_t flexspi_nor_get_vendor_id(FLEXSPI_Type *base, uint8_t *vendorId);
4848
status_t flexspi_nor_init(void);
49-
void flexspi_nor_update_lut(void);
49+
void flexspi_nor_update_lut_clk(void);
5050
status_t flexspi_nor_enable_quad_mode(FLEXSPI_Type *base);
5151
status_t flexspi_nor_flash_erase_sector(FLEXSPI_Type *base, uint32_t address);
5252
status_t flexspi_nor_flash_erase_block(FLEXSPI_Type *base, uint32_t address);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy