1
1
#include "hardware/structs/ioqspi.h"
2
2
#include "hardware/structs/qmi.h"
3
3
#include "hardware/structs/xip_ctrl.h"
4
+ #include "hardware/clocks.h"
4
5
#include "hardware/sync.h"
5
6
#include "rp2_psram.h"
6
7
@@ -11,11 +12,13 @@ void __no_inline_not_in_flash_func(psram_set_qmi_timing)() {
11
12
;
12
13
}
13
14
14
- // For > 133 MHz
15
- qmi_hw -> m [0 ].timing = 0x40000202 ;
16
-
17
- // For <= 133 MHz
18
- // qmi_hw->m[0].timing = 0x40000101;
15
+ if (clock_get_hz (clk_sys ) > 133000000 ) {
16
+ // For > 133 MHz
17
+ qmi_hw -> m [0 ].timing = 0x40000202 ;
18
+ } else {
19
+ // For <= 133 MHz
20
+ qmi_hw -> m [0 ].timing = 0x40000101 ;
21
+ }
19
22
20
23
// Force a read through XIP to ensure the timing is applied
21
24
volatile uint32_t * ptr = (volatile uint32_t * )0x14000000 ;
@@ -123,29 +126,29 @@ size_t __no_inline_not_in_flash_func(psram_init)(uint cs_pin) {
123
126
;
124
127
}
125
128
126
- # if 0
127
- // Set PSRAM timing for APS6404:
128
- // - Max select assumes a sys clock speed >= 240MHz
129
- // - Min deselect assumes a sys clock speed <= 305MHz
130
- // - Clkdiv of 2 is OK up to 266MHz.
131
- qmi_hw -> m [1 ].timing = 1 << QMI_M1_TIMING_COOLDOWN_LSB |
132
- QMI_M1_TIMING_PAGEBREAK_VALUE_1024 << QMI_M1_TIMING_PAGEBREAK_LSB |
133
- 30 << QMI_M1_TIMING_MAX_SELECT_LSB |
134
- 5 << QMI_M1_TIMING_MIN_DESELECT_LSB |
135
- 3 << QMI_M1_TIMING_RXDELAY_LSB |
136
- 2 << QMI_M1_TIMING_CLKDIV_LSB ;
137
- # else
138
- // Set PSRAM timing for APS6404:
139
- // - Max select assumes a sys clock speed >= 120MHz
140
- // - Min deselect assumes a sys clock speed <= 138MHz
141
- // - Clkdiv of 1 is OK up to 133MHz.
142
- qmi_hw -> m [1 ].timing = 1 << QMI_M1_TIMING_COOLDOWN_LSB |
143
- QMI_M1_TIMING_PAGEBREAK_VALUE_1024 << QMI_M1_TIMING_PAGEBREAK_LSB |
144
- 15 << QMI_M1_TIMING_MAX_SELECT_LSB |
145
- 2 << QMI_M1_TIMING_MIN_DESELECT_LSB |
146
- 2 << QMI_M1_TIMING_RXDELAY_LSB |
147
- 1 << QMI_M1_TIMING_CLKDIV_LSB ;
148
- #endif
129
+ if ( clock_get_hz ( clk_sys ) >= 120000000 ) {
130
+ // Set PSRAM timing for APS6404:
131
+ // - Max select assumes a sys clock speed >= 120MHz
132
+ // - Min deselect assumes a sys clock speed <= 305MHz
133
+ // - Clkdiv of 2 is OK up to 266MHz.
134
+ qmi_hw -> m [1 ].timing = 1 << QMI_M1_TIMING_COOLDOWN_LSB |
135
+ QMI_M1_TIMING_PAGEBREAK_VALUE_1024 << QMI_M1_TIMING_PAGEBREAK_LSB |
136
+ 15 << QMI_M1_TIMING_MAX_SELECT_LSB |
137
+ 5 << QMI_M1_TIMING_MIN_DESELECT_LSB |
138
+ 3 << QMI_M1_TIMING_RXDELAY_LSB |
139
+ 2 << QMI_M1_TIMING_CLKDIV_LSB ;
140
+ } else {
141
+ // Set PSRAM timing for APS6404:
142
+ // - Max select assumes a sys clock speed >= 120MHz
143
+ // - Min deselect assumes a sys clock speed <= 138MHz
144
+ // - Clkdiv of 1 is OK up to 133MHz.
145
+ qmi_hw -> m [1 ].timing = 1 << QMI_M1_TIMING_COOLDOWN_LSB |
146
+ QMI_M1_TIMING_PAGEBREAK_VALUE_1024 << QMI_M1_TIMING_PAGEBREAK_LSB |
147
+ 15 << QMI_M1_TIMING_MAX_SELECT_LSB |
148
+ 2 << QMI_M1_TIMING_MIN_DESELECT_LSB |
149
+ 2 << QMI_M1_TIMING_RXDELAY_LSB |
150
+ 1 << QMI_M1_TIMING_CLKDIV_LSB ;
151
+ }
149
152
150
153
// Set PSRAM commands and formats
151
154
qmi_hw -> m [1 ].rfmt =
@@ -175,6 +178,5 @@ size_t __no_inline_not_in_flash_func(psram_init)(uint cs_pin) {
175
178
// Enable writes to PSRAM
176
179
hw_set_bits (& xip_ctrl_hw -> ctrl , XIP_CTRL_WRITABLE_M1_BITS );
177
180
178
- // TODO: Detect PSRAM ID and size
179
181
return psram_size ;
180
182
}
0 commit comments