From af04da72f5f9a4de44fb40873b97002896ac69d2 Mon Sep 17 00:00:00 2001 From: Andrew Leech <> Date: Fri, 26 Jan 2024 15:26:29 +1100 Subject: [PATCH] stm32/qspi: Use the flash size configured in spiflash init. Signed-off-by: Andrew Leech --- drivers/bus/qspi.h | 3 +- drivers/bus/softqspi.c | 2 +- drivers/memory/spiflash.c | 10 +- ports/stm32/boards/STM32F769DISC/board_init.c | 3 +- ports/stm32/qspi.c | 173 +++++++++++++----- ports/stm32/qspi.h | 2 +- 6 files changed, 138 insertions(+), 55 deletions(-) diff --git a/drivers/bus/qspi.h b/drivers/bus/qspi.h index 669ff544a44a4..160cc63d4a42c 100644 --- a/drivers/bus/qspi.h +++ b/drivers/bus/qspi.h @@ -37,6 +37,7 @@ enum { MP_QSPI_IOCTL_DEINIT, MP_QSPI_IOCTL_BUS_ACQUIRE, MP_QSPI_IOCTL_BUS_RELEASE, + MP_QSPI_IOCTL_FLASH_SIZE, }; enum qspi_tranfer_mode { @@ -45,7 +46,7 @@ enum qspi_tranfer_mode { }; typedef struct _mp_qspi_proto_t { - int (*ioctl)(void *self, uint32_t cmd); + int (*ioctl)(void *self, uint32_t cmd, uint32_t arg); int (*write_cmd_data)(void *self, uint8_t cmd, size_t len, uint32_t data); int (*write_cmd_addr_data)(void *self, uint8_t cmd, uint32_t addr, size_t len, const uint8_t *src); int (*read_cmd)(void *self, uint8_t cmd, size_t len, uint32_t *dest); diff --git a/drivers/bus/softqspi.c b/drivers/bus/softqspi.c index e418a97e819b8..aa484d7e55138 100644 --- a/drivers/bus/softqspi.c +++ b/drivers/bus/softqspi.c @@ -56,7 +56,7 @@ STATIC void nibble_write(mp_soft_qspi_obj_t *self, uint8_t v) { mp_hal_pin_write(self->io3, (v >> 3) & 1); } -STATIC int mp_soft_qspi_ioctl(void *self_in, uint32_t cmd) { +STATIC int mp_soft_qspi_ioctl(void *self_in, uint32_t cmd, uint32_t arg) { mp_soft_qspi_obj_t *self = (mp_soft_qspi_obj_t*)self_in; switch (cmd) { diff --git a/drivers/memory/spiflash.c b/drivers/memory/spiflash.c index 4e6dd6c2ce62e..76b282b86bf8d 100644 --- a/drivers/memory/spiflash.c +++ b/drivers/memory/spiflash.c @@ -80,14 +80,14 @@ static external_flash_device generic_config = GENERIC; STATIC void mp_spiflash_acquire_bus(mp_spiflash_t *self) { const mp_spiflash_config_t *c = self->config; if (c->bus_kind == MP_SPIFLASH_BUS_QSPI) { - c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_ACQUIRE); + c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_ACQUIRE, 0); } } STATIC void mp_spiflash_release_bus(mp_spiflash_t *self) { const mp_spiflash_config_t *c = self->config; if (c->bus_kind == MP_SPIFLASH_BUS_QSPI) { - c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_RELEASE); + c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_RELEASE, 0); } } @@ -198,7 +198,7 @@ int mp_spiflash_init(mp_spiflash_t *self) { mp_hal_pin_output(self->config->bus.u_spi.cs); self->config->bus.u_spi.proto->ioctl(self->config->bus.u_spi.data, MP_SPI_IOCTL_INIT); } else { - self->config->bus.u_qspi.proto->ioctl(self->config->bus.u_qspi.data, MP_QSPI_IOCTL_INIT); + self->config->bus.u_qspi.proto->ioctl(self->config->bus.u_qspi.data, MP_QSPI_IOCTL_INIT, 0); } mp_spiflash_acquire_bus(self); @@ -327,6 +327,10 @@ int mp_spiflash_init(mp_spiflash_t *self) { } } + if (self->config->bus_kind == MP_SPIFLASH_BUS_QSPI) { + self->config->bus.u_qspi.proto->ioctl(self->config->bus.u_qspi.data, MP_QSPI_IOCTL_FLASH_SIZE, self->device->total_size); + } + mp_spiflash_release_bus(self); return ret; } diff --git a/ports/stm32/boards/STM32F769DISC/board_init.c b/ports/stm32/boards/STM32F769DISC/board_init.c index e89d187fece7a..5d7f1baa76e04 100644 --- a/ports/stm32/boards/STM32F769DISC/board_init.c +++ b/ports/stm32/boards/STM32F769DISC/board_init.c @@ -1,5 +1,6 @@ #include "storage.h" #include "qspi.h" +#include "mpconfigboard.h" // This configuration is needed for mboot to be able to write to the external QSPI flash @@ -21,6 +22,6 @@ spi_bdev_t spi_bdev; // This init function is needed to memory map the QSPI flash early in the boot process void board_early_init(void) { - qspi_init(); + qspi_init(MICROPY_HW_BDEV_SPIFLASH_SIZE_BYTES); qspi_memory_map(); } diff --git a/ports/stm32/qspi.c b/ports/stm32/qspi.c index cc88c63246734..a405ac15e525d 100644 --- a/ports/stm32/qspi.c +++ b/ports/stm32/qspi.c @@ -32,7 +32,7 @@ #include "qspi.h" #include "pin_static_af.h" -#if defined(MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2) +#if MICROPY_HW_ENABLE_QSPI || defined(MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2) #define QSPI_MAP_ADDR (0x90000000) @@ -52,17 +52,18 @@ #define MICROPY_HW_QSPI_CS_HIGH_CYCLES 2 // nCS stays high for 2 cycles #endif -#ifndef MICROPY_HW_QSPI_MPU_REGION_SIZE -#define MICROPY_HW_QSPI_MPU_REGION_SIZE ((1 << (MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2 - 3)) >> 20) +#ifndef MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2 #endif -#if (MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2 - 3 - 1) >= 24 -#define QSPI_CMD 0xec -#define QSPI_ADSIZE 3 -#else -#define QSPI_CMD 0xeb -#define QSPI_ADSIZE 2 -#endif +// Fast Read command in 32bit and 24bit addressing. +#define QSPI_FAST_READ_A4_CMD 0xec +#define QSPI_FAST_READ_A3_CMD 0xeb + +// this formula computes the log2 of "m" +#define BITS_TO_LOG2(m) ((m) - 1) / (((m) - 1) % 255 + 1) / 255 % 255 * 8 + 7 - 86 / (((m) - 1) % 255 + 12) + +#define MBytes (1024 * 1024) +static size_t qspi_memory_size_bytes = 0; static inline void qspi_mpu_disable_all(void) { // Configure MPU to disable access to entire QSPI region, to prevent CPU @@ -72,7 +73,48 @@ static inline void qspi_mpu_disable_all(void) { mpu_config_end(irq_state); } -static inline void qspi_mpu_enable_mapped(void) { +#if 1 + +static inline void qspi_mpu_enable_mapped() { + // Configure MPU to allow access to only the valid part of external SPI flash. + // The memory accesses to the mapped QSPI are faster if the MPU is not used + // unprivileged or the background region is disabled, the MPU issues a fault. + uint32_t irq_state = mpu_config_start(); + + if (qspi_memory_size_bytes > (128 * MBytes)) { + mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0xFF, MPU_REGION_SIZE_256MB)); + } else if (qspi_memory_size_bytes > (64 * MBytes)) { + mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x0F, MPU_REGION_SIZE_256MB)); + } else if (qspi_memory_size_bytes > (32 * MBytes)) { + mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x03, MPU_REGION_SIZE_256MB)); + } else if (qspi_memory_size_bytes > (16 * MBytes)) { + mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB)); + } else if (qspi_memory_size_bytes > (8 * MBytes)) { + mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB)); + mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x0F, MPU_REGION_SIZE_32MB)); + } else if (qspi_memory_size_bytes > (4 * MBytes)) { + mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB)); + mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x03, MPU_REGION_SIZE_32MB)); + } else if (qspi_memory_size_bytes > (2 * MBytes)) { + mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB)); + mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_32MB)); + } else if (qspi_memory_size_bytes > (1 * MBytes)) { + mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB)); + mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x0F, MPU_REGION_SIZE_32MB)); + mpu_config_region(MPU_REGION_QSPI3, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_16MB)); + } else { + mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB)); + mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_32MB)); + mpu_config_region(MPU_REGION_QSPI3, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x03, MPU_REGION_SIZE_4MB)); + } + mpu_config_end(irq_state); +} + +#else + +// This variant of the function is harder to read, but 76 bytes smaller. + +static inline void qspi_mpu_enable_mapped() { // Configure MPU to allow access to only the valid part of external SPI flash. // The memory accesses to the mapped QSPI are faster if the MPU is not used // for the memory-mapped region, so 3 MPU regions are used to disable access @@ -83,36 +125,61 @@ static inline void qspi_mpu_enable_mapped(void) { // other enabled region overlaps the disabled subregion, and the access is // unprivileged or the background region is disabled, the MPU issues a fault. uint32_t irq_state = mpu_config_start(); - #if MICROPY_HW_QSPI_MPU_REGION_SIZE > 128 - mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0xFF, MPU_REGION_SIZE_256MB)); - #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 64 - mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x0F, MPU_REGION_SIZE_256MB)); - #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 32 - mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x03, MPU_REGION_SIZE_256MB)); - #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 16 - mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB)); - #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 8 - mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB)); - mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x0F, MPU_REGION_SIZE_32MB)); - #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 4 - mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB)); - mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x03, MPU_REGION_SIZE_32MB)); - #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 2 - mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB)); - mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_32MB)); - #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 1 - mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB)); - mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x0F, MPU_REGION_SIZE_32MB)); - mpu_config_region(MPU_REGION_QSPI3, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_16MB)); - #else - mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB)); - mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_32MB)); - mpu_config_region(MPU_REGION_QSPI3, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x03, MPU_REGION_SIZE_4MB)); - #endif + + static const uint8_t region_definitions[][7] = { + // Each row per MB region total size, specfying region srd and size for MPU_REGION_QSPI1, 2 and 3. + {128, 0xFF, MPU_REGION_SIZE_256MB, 0, 0, 0, 0}, + { 64, 0x0F, MPU_REGION_SIZE_256MB, 0, 0, 0, 0}, + { 32, 0x03, MPU_REGION_SIZE_256MB, 0, 0, 0, 0}, + { 16, 0x01, MPU_REGION_SIZE_256MB, 0, 0, 0, 0}, + { 8, 0x01, MPU_REGION_SIZE_256MB, + 0x0F, MPU_REGION_SIZE_32MB, 0, 0}, + { 4, 0x01, MPU_REGION_SIZE_256MB, + 0x03, MPU_REGION_SIZE_32MB, 0, 0}, + { 2, 0x01, MPU_REGION_SIZE_256MB, + 0x01, MPU_REGION_SIZE_32MB, 0, 0}, + { 1, 0x01, MPU_REGION_SIZE_256MB, + 0x0F, MPU_REGION_SIZE_32MB, + 0x01, MPU_REGION_SIZE_16MB}, + { 0, 0x01, MPU_REGION_SIZE_256MB, + 0x01, MPU_REGION_SIZE_32MB, + 0x03, MPU_REGION_SIZE_4MB}, + }; + size_t qspi_memory_size_mbytes = qspi_memory_size_bytes / 1024 / 1024; + + for (uint8_t i = 0; i < 9; ++i) { + if (qspi_memory_size_mbytes > region_definitions[i][0]) { + uint32_t attr_size_1 = MPU_CONFIG_DISABLE(region_definitions[i][1], region_definitions[i][2]); + mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, attr_size_1); + if (region_definitions[i][3] > 0) { + uint32_t attr_size_2 = MPU_CONFIG_DISABLE(region_definitions[i][3], region_definitions[i][4]); + mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, attr_size_2); + } + if (region_definitions[i][5] > 0) { + uint32_t attr_size_3 = MPU_CONFIG_DISABLE(region_definitions[i][5], region_definitions[i][6]); + mpu_config_region(MPU_REGION_QSPI3, QSPI_MAP_ADDR, attr_size_3); + } + break; + } + } mpu_config_end(irq_state); } -void qspi_init(void) { +#endif + +void qspi_set_memory_size(size_t memory_size_bytes) { + qspi_memory_size_bytes = memory_size_bytes; + size_t QSPIFLASH_SIZE_BITS_LOG2 = BITS_TO_LOG2(qspi_memory_size_bytes * 8); + QUADSPI->DCR = + (QSPIFLASH_SIZE_BITS_LOG2 - 3 - 1) << QUADSPI_DCR_FSIZE_Pos + | (MICROPY_HW_QSPI_CS_HIGH_CYCLES - 1) << QUADSPI_DCR_CSHT_Pos + | 0 << QUADSPI_DCR_CKMODE_Pos // CLK idles at low state + ; +} + +void qspi_init(size_t memory_size_bytes) { + qspi_memory_size_bytes = memory_size_bytes; + qspi_mpu_disable_all(); // Configure pins @@ -143,15 +210,20 @@ void qspi_init(void) { | 1 << QUADSPI_CR_EN_Pos // enable the peripheral ; - QUADSPI->DCR = - (MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2 - 3 - 1) << QUADSPI_DCR_FSIZE_Pos - | (MICROPY_HW_QSPI_CS_HIGH_CYCLES - 1) << QUADSPI_DCR_CSHT_Pos - | 0 << QUADSPI_DCR_CKMODE_Pos // CLK idles at low state - ; + if (qspi_memory_size_bytes) { + qspi_set_memory_size(qspi_memory_size_bytes); + } } -void qspi_memory_map(void) { +void qspi_memory_map() { // Enable memory-mapped mode + uint8_t cmd = QSPI_FAST_READ_A3_CMD; + uint8_t adsize = 2; + if (qspi_memory_size_bytes > (16 * MBytes)) { + // Flash chips over 16MB require 32bit addressing. + cmd = QSPI_FAST_READ_A4_CMD; + adsize = 3; + } QUADSPI->ABR = 0; // disable continuous read mode @@ -163,20 +235,20 @@ void qspi_memory_map(void) { | 4 << QUADSPI_CCR_DCYC_Pos // 4 dummy cycles | 0 << QUADSPI_CCR_ABSIZE_Pos // 8-bit alternate byte | 3 << QUADSPI_CCR_ABMODE_Pos // alternate byte on 4 lines - | QSPI_ADSIZE << QUADSPI_CCR_ADSIZE_Pos + | adsize << QUADSPI_CCR_ADSIZE_Pos | 3 << QUADSPI_CCR_ADMODE_Pos // address on 4 lines | 1 << QUADSPI_CCR_IMODE_Pos // instruction on 1 line - | QSPI_CMD << QUADSPI_CCR_INSTRUCTION_Pos + | cmd << QUADSPI_CCR_INSTRUCTION_Pos ; qspi_mpu_enable_mapped(); } -STATIC int qspi_ioctl(void *self_in, uint32_t cmd) { +STATIC int qspi_ioctl(void *self_in, uint32_t cmd, uint32_t arg) { (void)self_in; switch (cmd) { case MP_QSPI_IOCTL_INIT: - qspi_init(); + qspi_init(0); break; case MP_QSPI_IOCTL_BUS_ACQUIRE: // Disable memory-mapped region during bus access @@ -192,6 +264,11 @@ STATIC int qspi_ioctl(void *self_in, uint32_t cmd) { // Switch to memory-map mode when bus is idle qspi_memory_map(); break; + case MP_QSPI_IOCTL_FLASH_SIZE: + if (arg > 0) { + qspi_set_memory_size(arg); + } + return qspi_memory_size_bytes; } return 0; // success } diff --git a/ports/stm32/qspi.h b/ports/stm32/qspi.h index c774b12582be6..e1b9c8402d46e 100644 --- a/ports/stm32/qspi.h +++ b/ports/stm32/qspi.h @@ -30,7 +30,7 @@ extern const mp_qspi_proto_t qspi_proto; -void qspi_init(void); +void qspi_init(size_t memory_size_bytes); void qspi_memory_map(void); #endif // MICROPY_INCLUDED_STM32_QSPI_H