Skip to main content

esp_hal/psram/
esp32s31.rs

1//! PSRAM driver for ESP32-S31.
2
3use super::{EXTMEM_ORIGIN, PsramSize};
4use crate::{
5    clock::ll::{ClockTree, PsramFunctionClockConfig, PsramInstance},
6    peripherals::HP_SYS_CLKRST,
7};
8
9mod oct_hex;
10
11#[cfg(all(idle_frequency_scaling, psram_idle_low_speed_switch))]
12pub(crate) use oct_hex::low_speed;
13
14/// PSRAM configuration.
15#[derive(Copy, Clone, Debug, Default, PartialEq)]
16#[cfg_attr(feature = "defmt", derive(defmt::Format))]
17#[instability::unstable]
18pub struct PsramConfig {
19    /// Size of PSRAM to map. Default: `AutoDetect` via MR2 density.
20    pub size: PsramSize,
21
22    /// PSRAM timing parameters. Default: 200 MHz. 250 MHz needs the MPLL at 500 MHz.
23    pub timing: PsramTimingParams,
24    // TODO: ECC enable.
25    // The MSPI0 controller has a ECC engine.
26    // pub ecc: bool, // or any other enum.
27}
28
29/// PSRAM timing parameters.
30///
31/// These values should be tuned together with the source clock frequency. The source clock
32/// frequency must be an integer multiple of the PSRAM frequency.
33#[derive(Copy, Clone, Debug, PartialEq)]
34#[cfg_attr(feature = "defmt", derive(defmt::Format))]
35pub struct PsramTimingParams {
36    /// PSRAM clock source.
37    pub clock_source: PsramFunctionClockConfig,
38
39    /// Bus clock frequency in MHz. Must divide the source clock evenly.
40    pub clock: u32,
41
42    /// MR0.read_latency field value (cycles = 2 * value + 6).
43    pub mr0_rl: u8,
44
45    /// MR4.wr_latency field value.
46    pub mr4_wl: u8,
47
48    /// Reads dummy length in bits for sync data reads (cache path).
49    ///
50    /// For `N` dummy bits, configure `reg_dummy_bits` to `N - 1`.
51    pub rd_dummy_bits: u8,
52
53    /// Writes dummy length in bits for sync data writes (cache path).
54    ///
55    /// For `N` dummy bits, configure `reg_dummy_bits` to `N - 1`.
56    pub wr_dummy_bits: u8,
57
58    /// Register-read dummy length for direct command path (MSPI3).
59    pub reg_dummy_bits: u32,
60}
61
62impl Default for PsramTimingParams {
63    fn default() -> Self {
64        Self::MHZ_200
65    }
66}
67
68impl PsramTimingParams {
69    /// Preset for 20 MHz clock speed.
70    pub const MHZ_20: Self = Self {
71        clock_source: PsramFunctionClockConfig::Mpll,
72        clock: 20,
73        mr0_rl: 2,
74        mr4_wl: 2,
75        rd_dummy_bits: 17,
76        wr_dummy_bits: 7,
77        reg_dummy_bits: 8,
78    };
79
80    /// Preset for 80 MHz clock speed.
81    pub const MHZ_80: Self = Self {
82        clock_source: PsramFunctionClockConfig::Mpll,
83        clock: 80,
84        mr0_rl: 2,
85        mr4_wl: 2,
86        rd_dummy_bits: 17,
87        wr_dummy_bits: 7,
88        reg_dummy_bits: 8,
89    };
90
91    /// Preset for 125 MHz clock speed.
92    pub const MHZ_125: Self = Self {
93        clock_source: PsramFunctionClockConfig::Mpll,
94        clock: 125,
95        mr0_rl: 2,
96        mr4_wl: 2,
97        rd_dummy_bits: 17,
98        wr_dummy_bits: 7,
99        reg_dummy_bits: 8,
100    };
101
102    /// Preset for 200 MHz clock speed.
103    pub const MHZ_200: Self = Self {
104        clock_source: PsramFunctionClockConfig::Mpll,
105        clock: 200,
106        mr0_rl: 4,
107        mr4_wl: 1,
108        rd_dummy_bits: 25,
109        wr_dummy_bits: 11,
110        reg_dummy_bits: 12,
111    };
112
113    /// Preset for 250 MHz clock speed.
114    pub const MHZ_250: Self = Self {
115        clock_source: PsramFunctionClockConfig::Mpll,
116        clock: 250,
117        mr0_rl: 6,
118        mr4_wl: 3,
119        rd_dummy_bits: 33,
120        wr_dummy_bits: 15,
121        reg_dummy_bits: 16,
122    };
123}
124
125/// Initializes PSRAM.
126#[crate::ram]
127pub(crate) fn init_psram(config: &mut PsramConfig) -> bool {
128    // Module clock + clock source
129    enable_psram_mspi();
130    reset_psram_mspi();
131
132    ClockTree::with(|clocks| {
133        PsramInstance::Psram.configure_function_clock(clocks, config.timing.clock_source);
134        PsramInstance::Psram.request_function_clock(clocks);
135    });
136
137    // Controller + PHY pad bring-up.
138    if !oct_hex::set_bus_clock(config.timing.clock) {
139        return false;
140    }
141    oct_hex::enable_dll();
142    oct_hex::psram_pad_init(false); // required for DDR strobe latch
143    oct_hex::set_cs_timing();
144
145    // SoC MR init (via MSPI3 SPI direct)
146    oct_hex::init_mr_registers(&config.timing, false);
147
148    if config.size.is_auto() {
149        config.size = PsramSize::Size(oct_hex::psram_detect_size(&config.timing));
150    }
151
152    // basic AXI configuration here
153    oct_hex::configure_psram_mspi(&config.timing, false);
154    oct_hex::mmu_map_psram(config.size.get());
155
156    true
157}
158
159pub(crate) fn map_psram(config: PsramConfig) -> core::ops::Range<usize> {
160    let start = EXTMEM_ORIGIN;
161    start..start + config.size.get()
162}
163
164fn enable_psram_mspi() {
165    HP_SYS_CLKRST::regs()
166        .psram_ctrl0()
167        .modify(|_, w| w.sys_clk_en().set_bit());
168}
169
170fn reset_psram_mspi() {
171    HP_SYS_CLKRST::regs().psram_ctrl0().modify(|_, w| {
172        w.axi_rst_en().set_bit();
173        w.apb_rst_en().set_bit()
174    });
175    HP_SYS_CLKRST::regs().psram_ctrl0().modify(|_, w| {
176        w.axi_rst_en().clear_bit();
177        w.apb_rst_en().clear_bit()
178    });
179}