From 785826ca23d4cbeb83756418632641c652925804 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 6 Dec 2024 03:32:49 +0800 Subject: [PATCH] modifications to the HAL for DAR-699 --- libs/cramium-hal/Cargo.toml | 1 + libs/cramium-hal/src/udma/i2c.rs | 89 ++++++++++++++++++++++++++------ libs/cramium-hal/src/udma/mod.rs | 18 +++++++ 3 files changed, 93 insertions(+), 15 deletions(-) diff --git a/libs/cramium-hal/Cargo.toml b/libs/cramium-hal/Cargo.toml index 37ec79007..155759cfd 100644 --- a/libs/cramium-hal/Cargo.toml +++ b/libs/cramium-hal/Cargo.toml @@ -39,6 +39,7 @@ board-baosec = [ board-baosor = ["camera-ov2640", "axp2101"] # Precursor form factor board-dabao = [] # Dev board form factor verilator-only = [] +mpw = [] udma-stress-test = [ ] # For RTL simulation testing - aggravates a key corner case diff --git a/libs/cramium-hal/src/udma/i2c.rs b/libs/cramium-hal/src/udma/i2c.rs index 607ac0553..5074f2304 100644 --- a/libs/cramium-hal/src/udma/i2c.rs +++ b/libs/cramium-hal/src/udma/i2c.rs @@ -6,6 +6,15 @@ use crate::udma::{Bank, Udma}; const TIMEOUT_ITERS: usize = 1_000_000; +// MPW had this register: +// pub const REG_SETUP: crate::Register = crate::Register::new(13, 0x1); +// pub const REG_SETUP_R_DO_RST: crate::Field = crate::Field::new(1, 0, REG_SETUP); +// It is gone in NTO and we use the UDMA system reset instead +#[cfg(feature = "mpw")] +const SETUP_OFFSET: usize = 13; +#[cfg(feature = "mpw")] +const DO_RST_MASK: usize = 1; + #[derive(Copy, Clone)] #[repr(u8)] pub enum I2cCmd { @@ -75,9 +84,12 @@ const MAX_I2C_TXLEN: usize = 512; const MAX_I2C_RXLEN: usize = 512; const MAX_I2C_CMDLEN: usize = 512; -pub struct I2c { +pub struct I2c<'a> { csr: CSR, - _channel: I2cChannel, + #[allow(dead_code)] // used in NTO + udma_global: &'a dyn UdmaGlobalConfig, + #[allow(dead_code)] // used in NTO + channel: I2cChannel, divider: u16, perclk_freq: u32, pub ifram: IframRange, @@ -91,20 +103,25 @@ pub struct I2c { pending: I2cPending, } -impl Udma for I2c { +impl Udma for I2c<'_> { fn csr_mut(&mut self) -> &mut CSR { &mut self.csr } fn csr(&self) -> &CSR { &self.csr } } -impl I2c { +impl<'a> I2c<'a> { /// Safety: called only after global clock for I2C channel is enabled. /// It is also unsafe to `Drop` because you have to cleanup the clock manually. #[cfg(feature = "std")] - pub unsafe fn new(channel: I2cChannel, i2c_freq: u32, perclk_freq: u32) -> Option { + pub unsafe fn new( + channel: I2cChannel, + i2c_freq: u32, + perclk_freq: u32, + udma_global: &'a dyn UdmaGlobalConfig, + ) -> Option { // one page is the minimum size we can request if let Some(ifram) = IframRange::request(4096, None) { - Some(I2c::new_with_ifram(channel, i2c_freq, perclk_freq, ifram)) + Some(I2c::new_with_ifram(channel, i2c_freq, perclk_freq, ifram, udma_global)) } else { None } @@ -115,6 +132,7 @@ impl I2c { i2c_freq: u32, perclk_freq: u32, ifram: IframRange, + udma_global: &'a dyn UdmaGlobalConfig, ) -> Self { // divide-by-4 is an empirical observation let divider: u16 = ((((perclk_freq / 2) / i2c_freq) / 4).min(u16::MAX as u32)) as u16; @@ -134,18 +152,33 @@ impl I2c { ) .expect("couldn't map i2c port"); #[cfg(target_os = "xous")] + #[allow(unused_mut)] // because it is used when `mpw` feature is selected let mut csr = CSR::new(csr_range.as_mut_ptr() as *mut u32); #[cfg(not(target_os = "xous"))] + #[allow(unused_mut)] // because it is used when `mpw` feature is selected let mut csr = CSR::new(base_addr as *mut u32); - // reset the block - csr.wfo(utra::udma_i2c_0::REG_SETUP_R_DO_RST, 1); - csr.wo(utra::udma_i2c_0::REG_SETUP, 0); + // reset the block, if MPW. If NTO, this is handled by udma global + #[cfg(feature = "mpw")] + unsafe { + csr.base().add(SETUP_OFFSET).write_volatile(DO_RST_MASK as u32); + csr.base().add(SETUP_OFFSET).write_volatile(0); + } + #[cfg(not(feature = "mpw"))] + { + udma_global.reset(match channel { + I2cChannel::Channel0 => PeriphId::I2c0, + I2cChannel::Channel1 => PeriphId::I2c1, + I2cChannel::Channel2 => PeriphId::I2c2, + I2cChannel::Channel3 => PeriphId::I2c3, + }); + } // one page is the minimum size we can request let ifram_base = ifram.virt_range.as_ptr() as usize; let ifram_base_phys = ifram.phys_range.as_ptr() as usize; let mut i2c = I2c { csr, - _channel: channel, + udma_global, + channel, ifram, divider, cmd_buf: unsafe { core::slice::from_raw_parts_mut(ifram_base as *mut u32, MAX_I2C_CMDLEN) }, @@ -221,8 +254,22 @@ impl I2c { self.udma_reset(Bank::Custom); self.udma_reset(Bank::Tx); self.udma_reset(Bank::Rx); - self.csr.wfo(utra::udma_i2c_0::REG_SETUP_R_DO_RST, 1); - self.csr.wo(utra::udma_i2c_0::REG_SETUP, 0); + // reset the block, if MPW. If NTO, this needs to be handled by the upper level code with a + // reset to udma_global + #[cfg(feature = "mpw")] + unsafe { + self.csr.base().add(SETUP_OFFSET).write_volatile(DO_RST_MASK as u32); + self.csr.base().add(SETUP_OFFSET).write_volatile(0); + } + #[cfg(not(feature = "mpw"))] + { + self.udma_global.reset(match self.channel { + I2cChannel::Channel0 => PeriphId::I2c0, + I2cChannel::Channel1 => PeriphId::I2c1, + I2cChannel::Channel2 => PeriphId::I2c2, + I2cChannel::Channel3 => PeriphId::I2c3, + }); + } self.send_cmd_list(&[I2cCmd::Config(self.divider)]); self.pending.take(); @@ -297,8 +344,20 @@ impl I2c { // block has to be reset on every start transaction due to a... bug? programming error? // where the Rx length is mismatched from the actual length of Rx data expected because // it seems the Rx buffer pointer increments even during Tx events. - self.csr.wfo(utra::udma_i2c_0::REG_SETUP_R_DO_RST, 1); - self.csr.wo(utra::udma_i2c_0::REG_SETUP, 0); + #[cfg(feature = "mpw")] + unsafe { + self.csr.base().add(SETUP_OFFSET).write_volatile(DO_RST_MASK as u32); + self.csr.base().add(SETUP_OFFSET).write_volatile(0); + } + #[cfg(not(feature = "mpw"))] + { + self.udma_global.reset(match self.channel { + I2cChannel::Channel0 => PeriphId::I2c0, + I2cChannel::Channel1 => PeriphId::I2c1, + I2cChannel::Channel2 => PeriphId::I2c2, + I2cChannel::Channel3 => PeriphId::I2c3, + }); + } // into the pre-allocated Tx buf self.new_tranaction(); @@ -328,7 +387,7 @@ impl I2c { } } -impl I2cApi for I2c { +impl I2cApi for I2c<'_> { fn i2c_read( &mut self, dev: u8, diff --git a/libs/cramium-hal/src/udma/mod.rs b/libs/cramium-hal/src/udma/mod.rs index 19da2b93b..4f1d0fe30 100644 --- a/libs/cramium-hal/src/udma/mod.rs +++ b/libs/cramium-hal/src/udma/mod.rs @@ -28,6 +28,7 @@ use crate::SharedCsr; enum GlobalReg { ClockGate = 0, EventIn = 1, + Reset = 2, } impl Into for GlobalReg { fn into(self) -> usize { self as usize } @@ -241,6 +242,7 @@ pub trait UdmaGlobalConfig { event_type: PeriphEventType, to_channel: EventChannel, ); + fn reset(&self, peripheral: PeriphId); } #[repr(u32)] @@ -319,6 +321,20 @@ impl GlobalConfig { // Safety: only safe when used in the context of UDMA registers. unsafe { self.csr.base().add(GlobalReg::EventIn.into()).read_volatile() } } + + pub fn reset(&self, peripheral: PeriphId) { + unsafe { + // assert reset + self.csr.base().add(GlobalReg::Reset.into()).write_volatile(peripheral.into()); + // a few nops for the reset to propagate + core::arch::asm!( + "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", + "nop", "nop", "nop", + ); + // de-assert reset + self.csr.base().add(GlobalReg::Reset.into()).write_volatile(0); + } + } } impl UdmaGlobalConfig for GlobalConfig { @@ -338,6 +354,8 @@ impl UdmaGlobalConfig for GlobalConfig { ) { self.map_event(peripheral, event_type, to_channel); } + + fn reset(&self, peripheral: PeriphId) { self.reset(peripheral); } } // --------------------------------- DMA channel ------------------------------------ pub(crate) const CFG_EN: u32 = 0b01_0000; // start a transfer