52
52
#define MICROPY_HW_QSPI_CS_HIGH_CYCLES 2 // nCS stays high for 2 cycles
53
53
#endif
54
54
55
+ #ifndef MICROPY_HW_QSPI_MPU_REGION_SIZE
56
+ #define MICROPY_HW_QSPI_MPU_REGION_SIZE ((1 << (MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2 - 3)) >> 20)
57
+ #endif
58
+
55
59
#if (MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2 - 3 - 1 ) >= 24
56
60
#define QSPI_CMD 0xec
57
61
#define QSPI_ADSIZE 3
@@ -74,11 +78,37 @@ static inline void qspi_mpu_enable_mapped(void) {
74
78
// for the memory-mapped region, so 3 MPU regions are used to disable access
75
79
// to everything except the valid address space, using holes in the bottom
76
80
// of the regions and nesting them.
77
- // At the moment this is hard-coded to 2MiB of QSPI address space.
81
+ // Note: Disabling a subregion (by setting its corresponding SRD bit to 1)
82
+ // means another region overlapping the disabled range matches instead. If no
83
+ // other enabled region overlaps the disabled subregion, and the access is
84
+ // unprivileged or the background region is disabled, the MPU issues a fault.
78
85
uint32_t irq_state = mpu_config_start ();
86
+ #if MICROPY_HW_QSPI_MPU_REGION_SIZE > 128
87
+ mpu_config_region (MPU_REGION_QSPI1 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0xFF , MPU_REGION_SIZE_256MB ));
88
+ #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 64
89
+ mpu_config_region (MPU_REGION_QSPI1 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x0F , MPU_REGION_SIZE_256MB ));
90
+ #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 32
91
+ mpu_config_region (MPU_REGION_QSPI1 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x03 , MPU_REGION_SIZE_256MB ));
92
+ #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 16
93
+ mpu_config_region (MPU_REGION_QSPI1 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x01 , MPU_REGION_SIZE_256MB ));
94
+ #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 8
95
+ mpu_config_region (MPU_REGION_QSPI1 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x01 , MPU_REGION_SIZE_256MB ));
96
+ mpu_config_region (MPU_REGION_QSPI2 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x0F , MPU_REGION_SIZE_32MB ));
97
+ #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 4
79
98
mpu_config_region (MPU_REGION_QSPI1 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x01 , MPU_REGION_SIZE_256MB ));
80
- mpu_config_region (MPU_REGION_QSPI2 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x0f , MPU_REGION_SIZE_32MB ));
99
+ mpu_config_region (MPU_REGION_QSPI2 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x03 , MPU_REGION_SIZE_32MB ));
100
+ #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 2
101
+ mpu_config_region (MPU_REGION_QSPI1 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x01 , MPU_REGION_SIZE_256MB ));
102
+ mpu_config_region (MPU_REGION_QSPI2 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x01 , MPU_REGION_SIZE_32MB ));
103
+ #elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 1
104
+ mpu_config_region (MPU_REGION_QSPI1 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x01 , MPU_REGION_SIZE_256MB ));
105
+ mpu_config_region (MPU_REGION_QSPI2 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x0F , MPU_REGION_SIZE_32MB ));
81
106
mpu_config_region (MPU_REGION_QSPI3 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x01 , MPU_REGION_SIZE_16MB ));
107
+ #else
108
+ mpu_config_region (MPU_REGION_QSPI1 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x01 , MPU_REGION_SIZE_256MB ));
109
+ mpu_config_region (MPU_REGION_QSPI2 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x01 , MPU_REGION_SIZE_32MB ));
110
+ mpu_config_region (MPU_REGION_QSPI3 , QSPI_MAP_ADDR , MPU_CONFIG_DISABLE (0x03 , MPU_REGION_SIZE_4MB ));
111
+ #endif
82
112
mpu_config_end (irq_state );
83
113
}
84
114
0 commit comments