2019-11-21 07:14:09 +08:00
|
|
|
//! Quad-SPI Flash Controller
|
|
|
|
|
2019-11-28 10:02:51 +08:00
|
|
|
use core::marker::PhantomData;
|
2019-11-21 07:14:09 +08:00
|
|
|
use crate::regs::{RegisterW, RegisterRW};
|
|
|
|
use super::slcr;
|
|
|
|
use super::clocks::CpuClocks;
|
|
|
|
|
|
|
|
pub mod regs;
|
|
|
|
|
2019-11-23 08:59:24 +08:00
|
|
|
const FLASH_BAUD_RATE: u32 = 50_000_000;
|
|
|
|
|
2019-11-28 10:02:51 +08:00
|
|
|
pub struct LinearAddressing;
|
|
|
|
|
2019-11-21 07:14:09 +08:00
|
|
|
/// Flash Interface Driver
|
2019-11-23 08:59:24 +08:00
|
|
|
///
|
|
|
|
/// For 2x Spansion S25FL128SAGMFIR01
|
2019-11-28 10:02:51 +08:00
|
|
|
pub struct Flash<MODE> {
|
2019-11-21 07:14:09 +08:00
|
|
|
regs: &'static mut regs::RegisterBlock,
|
2019-11-28 10:02:51 +08:00
|
|
|
_mode: PhantomData<MODE>,
|
2019-11-21 07:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-11-28 10:02:51 +08:00
|
|
|
impl Flash<()> {
|
2019-11-21 07:14:09 +08:00
|
|
|
pub fn new(clock: u32) -> Self {
|
|
|
|
Self::enable_clocks(clock);
|
|
|
|
Self::setup_signals();
|
|
|
|
Self::reset();
|
|
|
|
|
|
|
|
let regs = regs::RegisterBlock::qspi();
|
2019-11-28 10:02:51 +08:00
|
|
|
let mut flash = Flash { regs, _mode: PhantomData };
|
2019-11-23 08:59:24 +08:00
|
|
|
flash.configure((FLASH_BAUD_RATE - 1 + clock) / FLASH_BAUD_RATE);
|
2019-11-21 07:14:09 +08:00
|
|
|
flash
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enable_clocks(clock: u32) {
|
|
|
|
let io_pll = CpuClocks::get().io;
|
|
|
|
let divisor = ((clock - 1 + io_pll) / clock)
|
|
|
|
.max(1).min(63) as u8;
|
|
|
|
|
|
|
|
slcr::RegisterBlock::unlocked(|slcr| {
|
|
|
|
slcr.lqspi_clk_ctrl.write(
|
|
|
|
slcr::LqspiClkCtrl::zeroed()
|
|
|
|
.src_sel(slcr::PllSource::IoPll)
|
|
|
|
.divisor(divisor)
|
|
|
|
.clkact(true)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup_signals() {
|
2019-11-23 08:59:24 +08:00
|
|
|
slcr::RegisterBlock::unlocked(|slcr| {
|
|
|
|
// 1. Configure MIO pin 1 for chip select 0 output.
|
|
|
|
slcr.mio_pin_01.write(
|
|
|
|
slcr::MioPin01::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
.pullup(true)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Configure MIO pins 2 through 5 for I/O.
|
|
|
|
slcr.mio_pin_02.write(
|
|
|
|
slcr::MioPin02::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
);
|
|
|
|
slcr.mio_pin_03.write(
|
|
|
|
slcr::MioPin03::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
);
|
|
|
|
slcr.mio_pin_04.write(
|
|
|
|
slcr::MioPin04::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
);
|
|
|
|
slcr.mio_pin_05.write(
|
|
|
|
slcr::MioPin05::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
);
|
|
|
|
|
|
|
|
// 3. Configure MIO pin 6 for serial clock 0 output.
|
|
|
|
slcr.mio_pin_06.write(
|
|
|
|
slcr::MioPin06::zeroed()
|
|
|
|
.l0_sel(true)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: optional 2nd chip setup
|
|
|
|
});
|
2019-11-21 07:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn reset() {
|
|
|
|
slcr::RegisterBlock::unlocked(|slcr| {
|
|
|
|
slcr.lqspi_rst_ctrl.write(
|
|
|
|
slcr::LqspiRstCtrl::zeroed()
|
|
|
|
.ref_rst(true)
|
|
|
|
.cpu1x_rst(true)
|
|
|
|
);
|
|
|
|
slcr.lqspi_rst_ctrl.write(
|
|
|
|
slcr::LqspiRstCtrl::zeroed()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-23 08:59:24 +08:00
|
|
|
fn configure(&mut self, divider: u32) {
|
|
|
|
// for a baud_rate_div=1 LPBK_DLY_ADJ would be required
|
|
|
|
let mut baud_rate_div = 2u32;
|
|
|
|
while baud_rate_div < 7 && 2u32.pow(1 + baud_rate_div) < divider {
|
|
|
|
baud_rate_div += 1;
|
|
|
|
}
|
|
|
|
|
2019-11-28 10:02:51 +08:00
|
|
|
self.regs.config.write(regs::Config::zeroed()
|
2019-11-23 08:59:24 +08:00
|
|
|
.baud_rate_div(baud_rate_div as u8)
|
2019-11-21 07:14:09 +08:00
|
|
|
.mode_sel(true)
|
|
|
|
.leg_flsh(true)
|
|
|
|
.fifo_width(0b11)
|
|
|
|
);
|
|
|
|
}
|
2019-11-23 08:59:24 +08:00
|
|
|
|
2019-11-28 10:02:51 +08:00
|
|
|
pub fn linear_addressing_mode(self) -> Flash<LinearAddressing> {
|
|
|
|
// Set manual start enable to auto mode.
|
|
|
|
// Assert the chip select.
|
|
|
|
self.regs.config.modify(|_, w| w
|
|
|
|
.man_start_en(false)
|
|
|
|
.pcs(false)
|
|
|
|
);
|
|
|
|
|
|
|
|
self.regs.lqspi_cfg.write(regs::LqspiCfg::zeroed()
|
2019-11-23 08:59:24 +08:00
|
|
|
.inst_code(0x3)
|
|
|
|
.u_page(false)
|
|
|
|
.sep_bus(false)
|
|
|
|
.two_mem(false)
|
|
|
|
.lq_mode(true)
|
|
|
|
);
|
2019-11-28 10:02:51 +08:00
|
|
|
|
|
|
|
self.regs.enable.modify(|_, w| w.spi_en(true));
|
|
|
|
|
|
|
|
Flash {
|
|
|
|
regs: self.regs,
|
|
|
|
_mode: PhantomData,
|
|
|
|
}
|
2019-11-23 08:59:24 +08:00
|
|
|
}
|
2019-11-28 10:02:51 +08:00
|
|
|
}
|
2019-11-23 08:59:24 +08:00
|
|
|
|
2019-11-28 10:02:51 +08:00
|
|
|
impl Flash<LinearAddressing> {
|
2019-11-23 08:59:24 +08:00
|
|
|
pub fn ptr<T>(&mut self) -> *mut T {
|
|
|
|
0xFC00_0000 as *mut _
|
|
|
|
}
|
2019-11-21 07:14:09 +08:00
|
|
|
}
|