Skip to content

Commit af4ac7b

Browse files
committed
ports/rp2: Re-init PSRAM on CPU freq change.
Signed-off-by: Phil Howard <[email protected]>
1 parent 45b462e commit af4ac7b

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

ports/rp2/modmachine.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "mp_usbd.h"
3232
#include "modmachine.h"
3333
#include "uart.h"
34+
#include "rp2_psram.h"
3435
#include "clocks_extra.h"
3536
#include "hardware/pll.h"
3637
#include "hardware/structs/rosc.h"
@@ -115,6 +116,9 @@ static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
115116
setup_default_uart();
116117
mp_uart_init();
117118
#endif
119+
#if defined(MICROPY_HW_PSRAM_CS_PIN) && MICROPY_HW_ENABLE_PSRAM
120+
psram_init(MICROPY_HW_PSRAM_CS_PIN);
121+
#endif
118122
}
119123

120124
static void mp_machine_idle(void) {

ports/rp2/rp2_psram.c

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "hardware/structs/ioqspi.h"
22
#include "hardware/structs/qmi.h"
33
#include "hardware/structs/xip_ctrl.h"
4+
#include "hardware/clocks.h"
45
#include "hardware/sync.h"
56
#include "rp2_psram.h"
67

@@ -11,11 +12,13 @@ void __no_inline_not_in_flash_func(psram_set_qmi_timing)() {
1112
;
1213
}
1314

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+
}
1922

2023
// Force a read through XIP to ensure the timing is applied
2124
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) {
123126
;
124127
}
125128

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+
}
149152

150153
// Set PSRAM commands and formats
151154
qmi_hw->m[1].rfmt =
@@ -175,6 +178,5 @@ size_t __no_inline_not_in_flash_func(psram_init)(uint cs_pin) {
175178
// Enable writes to PSRAM
176179
hw_set_bits(&xip_ctrl_hw->ctrl, XIP_CTRL_WRITABLE_M1_BITS);
177180

178-
// TODO: Detect PSRAM ID and size
179181
return psram_size;
180182
}

0 commit comments

Comments
 (0)