diff --git a/target/ast10x0/board/src/lib.rs b/target/ast10x0/board/src/lib.rs index dd7445df6..a32a63df0 100644 --- a/target/ast10x0/board/src/lib.rs +++ b/target/ast10x0/board/src/lib.rs @@ -98,23 +98,25 @@ impl Ast10x0Board { /// - Not thread-safe; caller must ensure no concurrent SCU or I2C accesses. pub unsafe fn init(&self) -> Result<(), I2cError> { // Unlock SCU once before the sequence of writes (aspeed-rust pattern) - let scu = unsafe { ScuRegisters::new_global_unlocked() }; + let scu = unsafe { ScuRegisters::new_global() }; - // Apply pinctrl groups - for group in self.descriptor.pinctrl_groups { - scu.apply_pinctrl_group(group); - } + scu.unlock_and_lock_after(|scu| { + // Apply pinctrl groups + for group in self.descriptor.pinctrl_groups { + scu.apply_pinctrl_group(group); + } - // Enable I2C clock (Group 0, bit 2) - scu.ungate_clock_mask(ClockRegisterHalf::Lower, 1 << 2); + // Enable I2C clock (Group 0, bit 2) + scu.ungate_clock_mask(ClockRegisterHalf::Lower, 1 << 2); - // Assert I2C reset (Upper half, bit 2) - scu.assert_reset_mask(ScuRegisterHalf::Upper, 1 << 2); - delay_us(1000); + // Assert I2C reset (Upper half, bit 2) + scu.assert_reset_mask(ScuRegisterHalf::Upper, 1 << 2); + delay_us(1000); - // Deassert I2C reset - scu.deassert_reset_mask(ScuRegisterHalf::Upper, 1 << 2); - delay_us(1000); + // Deassert I2C reset + scu.deassert_reset_mask(ScuRegisterHalf::Upper, 1 << 2); + delay_us(1000); + }); // Configure I2C global registers (clock dividers, etc.) unsafe { ast10x0_peripherals::i2c::init_i2c_global() }; diff --git a/target/ast10x0/board/src/spim_wiring.rs b/target/ast10x0/board/src/spim_wiring.rs index 051c86090..df206e6e5 100644 --- a/target/ast10x0/board/src/spim_wiring.rs +++ b/target/ast10x0/board/src/spim_wiring.rs @@ -96,6 +96,24 @@ impl From for SpimWiringError { } } +/// Route the SGPIOM clock, load, data-out and data-in signals to their pins. +fn enable_sgpiom_pins() { + // SAFETY: SCU access is coordinated by the board layer. + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| { + scu.regs().scu41c().modify(|_, w| { + w.enbl_sgpiomaster_ckfn_pin() + .set_bit() + .enbl_sgpiomaster_ldfn_pin() + .set_bit() + .enbl_sgpiomaster_dofn_pin() + .set_bit() + .enbl_sgpiomaster_difn_pin() + .set_bit() + }); + }); +} + /// Apply the default SCU pinctrl group for a SPI monitor instance. /// /// The instance numbering follows the hardware SPIPF blocks: `Spim0` uses @@ -126,17 +144,7 @@ pub fn apply_spim_external_mux(instance: SpiMonitorInstance, mux: ScuExtMuxSelec }; let gpio = unsafe { &*device::Gpio::ptr() }; - let scu = unsafe { &*device::Scu::ptr() }; - scu.scu41c().modify(|_, w| { - w.enbl_sgpiomaster_ckfn_pin() - .set_bit() - .enbl_sgpiomaster_ldfn_pin() - .set_bit() - .enbl_sgpiomaster_dofn_pin() - .set_bit() - .enbl_sgpiomaster_difn_pin() - .set_bit() - }); + enable_sgpiom_pins(); match gpio_group { ExternalMuxGpioGroup::Abcd => { gpio.gpio000() @@ -290,17 +298,7 @@ pub fn set_bmc_resets(asserted: bool) -> bool { const BMC_SRST_MASK: u32 = 1 << 8; const BMC_EXTRST_MASK: u32 = 1 << 9; - let scu = unsafe { &*device::Scu::ptr() }; - scu.scu41c().modify(|_, w| { - w.enbl_sgpiomaster_ckfn_pin() - .set_bit() - .enbl_sgpiomaster_ldfn_pin() - .set_bit() - .enbl_sgpiomaster_dofn_pin() - .set_bit() - .enbl_sgpiomaster_difn_pin() - .set_bit() - }); + enable_sgpiom_pins(); let sgpio = unsafe { &*device::Sgpiom::ptr() }; sgpio.gpio554().modify(|_, w| unsafe { diff --git a/target/ast10x0/entry.rs b/target/ast10x0/entry.rs index a668bce77..38a2ba988 100644 --- a/target/ast10x0/entry.rs +++ b/target/ast10x0/entry.rs @@ -85,6 +85,10 @@ unsafe fn pre_init() { // SAFETY: SCU is unlocked above; pre-init has exclusive ownership. unsafe { init_cache() }; + + // Re-lock the SCU. Later users unlock it only for the duration of their + // writes, via `ScuRegisters::unlock_and_lock_after`. + scu.scu000().write(|w| unsafe { w.bits(0) }); } #[unsafe(no_mangle)] diff --git a/target/ast10x0/peripherals/hace/device.rs b/target/ast10x0/peripherals/hace/device.rs index 8f854cd69..44539b4f5 100644 --- a/target/ast10x0/peripherals/hace/device.rs +++ b/target/ast10x0/peripherals/hace/device.rs @@ -110,13 +110,16 @@ impl HaceDevice { // clock-stop bit and enable the HACE YCLK before touching any HACE registers. // SAFETY: SCU is a singleton; this is called under the same single-instance // contract as HaceRegisters::new_global — the caller guarantees exclusivity. - let scu = unsafe { ScuRegisters::new_global_unlocked() }; - scu.ungate_clock_mask(ClockRegisterHalf::Lower, 1 << 13); - // SCU040 bit 4 = HACE reset. Write bit 4 to SCU044 to deassert reset and - // bring the HACE out of reset before accessing any HACE registers. Without - // this, the AHB bus hangs on the first HACE register read when the ROM - // bootloader (UART/fwspick mode) leaves HACE in reset. - scu.deassert_reset_mask(ScuRegisterHalf::Lower, 1 << 4); + let scu = unsafe { ScuRegisters::new_global() }; + + scu.unlock_and_lock_after(|scu| { + scu.ungate_clock_mask(ClockRegisterHalf::Lower, 1 << 13); + // SCU040 bit 4 = HACE reset. Write bit 4 to SCU044 to deassert reset and + // bring the HACE out of reset before accessing any HACE registers. Without + // this, the AHB bus hangs on the first HACE register read when the ROM + // bootloader (UART/fwspick mode) leaves HACE in reset. + scu.deassert_reset_mask(ScuRegisterHalf::Lower, 1 << 4); + }); // SAFETY: Caller coordinates singleton access. let regs = unsafe { HaceRegisters::new_global() }; diff --git a/target/ast10x0/peripherals/i3c/registers.rs b/target/ast10x0/peripherals/i3c/registers.rs index f8913871c..d3ef0769c 100644 --- a/target/ast10x0/peripherals/i3c/registers.rs +++ b/target/ast10x0/peripherals/i3c/registers.rs @@ -18,6 +18,7 @@ use core::marker::PhantomData; use super::constants::MAX_BUSES; +use crate::scu::ScuRegisters; // ----------------------------------------------------------------------------- // Per-bus register dispatch (private) @@ -194,57 +195,61 @@ impl I3cRegisters { // SCU: per-bus reset and clock // ------------------------------------------------------------------------- + /// Run `f` on the SCU register block with write protection lifted. + /// + /// SCU reset and clock registers are write-protected. The lock is taken + /// only around `f` (see [`ScuRegisters::unlock_and_lock_after`]), so writes + /// made outside it are silently dropped. + #[inline] + fn with_scu_unlocked(&self, f: impl FnOnce(&ast1060_pac::scu::RegisterBlock) -> R) -> R { + // SAFETY: the SCU is the global singleton; this façade's contract + // already serializes SCU access with the owning thread. + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| f(scu.regs())) + } + /// SCU050: assert this bus's controller reset. pub(crate) fn core_reset_assert(&self) { - match self.bus { - 0 => self - .scu() - .scu050() - .modify(|_, w| w.rst_i3c0ctrl().set_bit()), - 1 => self - .scu() - .scu050() - .modify(|_, w| w.rst_i3c1ctrl().set_bit()), - 2 => self - .scu() - .scu050() - .modify(|_, w| w.rst_i3c2ctrl().set_bit()), - _ => self - .scu() - .scu050() - .modify(|_, w| w.rst_i3c3ctrl().set_bit()), - }; + self.with_scu_unlocked(|scu| match self.bus { + 0 => scu.scu050().modify(|_, w| w.rst_i3c0ctrl().set_bit()), + 1 => scu.scu050().modify(|_, w| w.rst_i3c1ctrl().set_bit()), + 2 => scu.scu050().modify(|_, w| w.rst_i3c2ctrl().set_bit()), + _ => scu.scu050().modify(|_, w| w.rst_i3c3ctrl().set_bit()), + }); } /// SCU054: deassert this bus's controller reset (write-1-to-clear). pub(crate) fn core_reset_deassert(&self) { let mask = 1u32 << (8 + u32::from(self.bus)); - self.scu() - .scu054() - .modify(|_, w| unsafe { w.scu050sys_rst_ctrl_clear_reg2().bits(mask) }); + self.with_scu_unlocked(|scu| { + scu.scu054() + .modify(|_, w| unsafe { w.scu050sys_rst_ctrl_clear_reg2().bits(mask) }); + }); } /// SCU050: assert the shared I3C register/DMA reset. #[allow(dead_code)] pub(crate) fn global_reset_assert(&self) { - self.scu() - .scu050() - .modify(|_, w| w.rst_i3cregdmactrl().set_bit()); + self.with_scu_unlocked(|scu| { + scu.scu050().modify(|_, w| w.rst_i3cregdmactrl().set_bit()); + }); } /// SCU054: deassert the shared I3C register/DMA reset (write-1-to-clear). pub(crate) fn global_reset_deassert(&self, mask: u32) { - self.scu() - .scu054() - .modify(|_, w| unsafe { w.scu050sys_rst_ctrl_clear_reg2().bits(mask) }); + self.with_scu_unlocked(|scu| { + scu.scu054() + .modify(|_, w| unsafe { w.scu050sys_rst_ctrl_clear_reg2().bits(mask) }); + }); } /// SCU094: ungate this bus's clock (write-1-to-clear stop bit). pub(crate) fn clock_on(&self) { let mask = 1u32 << (8 + u32::from(self.bus)); - self.scu() - .scu094() - .modify(|_, w| unsafe { w.scu090clk_stop_ctrl_clear_reg_set2().bits(mask) }); + self.with_scu_unlocked(|scu| { + scu.scu094() + .modify(|_, w| unsafe { w.scu090clk_stop_ctrl_clear_reg_set2().bits(mask) }); + }); } /// SCU004: hardware revision ID. diff --git a/target/ast10x0/peripherals/scu/cache.rs b/target/ast10x0/peripherals/scu/cache.rs index 0e8a20f5d..19bebb3a1 100644 --- a/target/ast10x0/peripherals/scu/cache.rs +++ b/target/ast10x0/peripherals/scu/cache.rs @@ -18,14 +18,16 @@ impl ScuRegisters { #[inline] pub fn dcache_invd_all(&self) { const DCACHE_CLEAN: u32 = 1 << 1; - // SAFETY: SCU MMIO read-modify-write; DSB only orders memory accesses. - unsafe { - let ctrl = self.regs().scua58().read().bits(); - self.regs().scua58().write(|w| w.bits(ctrl & !DCACHE_CLEAN)); - core::arch::asm!("dsb sy", options(nostack, preserves_flags)); - self.regs().scua58().write(|w| w.bits(ctrl | DCACHE_CLEAN)); - core::arch::asm!("dsb sy", options(nostack, preserves_flags)); - } + self.unlock_and_lock_after(|scu| { + // SAFETY: SCU MMIO read-modify-write; DSB only orders memory accesses. + unsafe { + let ctrl = scu.regs().scua58().read().bits(); + scu.regs().scua58().write(|w| w.bits(ctrl & !DCACHE_CLEAN)); + core::arch::asm!("dsb sy", options(nostack, preserves_flags)); + scu.regs().scua58().write(|w| w.bits(ctrl | DCACHE_CLEAN)); + core::arch::asm!("dsb sy", options(nostack, preserves_flags)); + } + }); } } diff --git a/target/ast10x0/peripherals/scu/clock.rs b/target/ast10x0/peripherals/scu/clock.rs index e258112f6..4867933cc 100644 --- a/target/ast10x0/peripherals/scu/clock.rs +++ b/target/ast10x0/peripherals/scu/clock.rs @@ -9,30 +9,32 @@ use super::types::ClockRegisterHalf; impl ScuRegisters { /// Gate clocks by setting bits in the selected clock-stop register half. /// - /// Caller must call `unlock_write_protection()` before this. + /// Unlocks the SCU for the duration of the write; see + /// [`ScuRegisters::unlock_and_lock_after`]. pub fn gate_clock_mask(&self, half: ClockRegisterHalf, mask: u32) { - match half { + self.unlock_and_lock_after(|scu| match half { ClockRegisterHalf::Lower => { - self.regs().scu080().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu080().write(|w| unsafe { w.bits(mask) }); } ClockRegisterHalf::Upper => { - self.regs().scu090().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu090().write(|w| unsafe { w.bits(mask) }); } - } + }); } /// Ungate clocks by clearing bits in the selected clock-stop register half. /// - /// Caller must call `unlock_write_protection()` before this. + /// Unlocks the SCU for the duration of the write; see + /// [`ScuRegisters::unlock_and_lock_after`]. pub fn ungate_clock_mask(&self, half: ClockRegisterHalf, mask: u32) { - match half { + self.unlock_and_lock_after(|scu| match half { ClockRegisterHalf::Lower => { - self.regs().scu084().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu084().write(|w| unsafe { w.bits(mask) }); } ClockRegisterHalf::Upper => { - self.regs().scu094().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu094().write(|w| unsafe { w.bits(mask) }); } - } + }); } /// Read the selected clock-stop register half. diff --git a/target/ast10x0/peripherals/scu/pinctrl.rs b/target/ast10x0/peripherals/scu/pinctrl.rs index 0bcd3765a..8d0f006de 100644 --- a/target/ast10x0/peripherals/scu/pinctrl.rs +++ b/target/ast10x0/peripherals/scu/pinctrl.rs @@ -1097,26 +1097,28 @@ impl ScuRegisters { /// # } /// ``` pub fn apply_pinctrl_group(&self, pins: &[PinctrlPin]) { - let regs = self.regs(); - for pin in pins { - match pin.offset { - 0x410 => modify_reg!(regs.scu410(), pin.bit, pin.clear), - 0x414 => modify_reg!(regs.scu414(), pin.bit, pin.clear), - 0x418 => modify_reg!(regs.scu418(), pin.bit, pin.clear), - 0x41C => modify_reg!(regs.scu41c(), pin.bit, pin.clear), - 0x430 => modify_reg!(regs.scu430(), pin.bit, pin.clear), - 0x434 => modify_reg!(regs.scu434(), pin.bit, pin.clear), - 0x4B0 => modify_reg!(regs.scu4b0(), pin.bit, pin.clear), - 0x4B4 => modify_reg!(regs.scu4b4(), pin.bit, pin.clear), - 0x4B8 => modify_reg!(regs.scu4b8(), pin.bit, pin.clear), - 0x4BC => modify_reg!(regs.scu4bc(), pin.bit, pin.clear), - 0x690 => modify_reg!(regs.scu690(), pin.bit, pin.clear), - 0x694 => modify_reg!(regs.scu694(), pin.bit, pin.clear), - 0x698 => modify_reg!(regs.scu698(), pin.bit, pin.clear), - 0x69C => modify_reg!(regs.scu69c(), pin.bit, pin.clear), - 0x6B0 => modify_reg!(regs.scu6b0(), pin.bit, pin.clear), - _ => {} // Unknown offset, silently ignore + self.unlock_and_lock_after(|scu| { + let regs = scu.regs(); + for pin in pins { + match pin.offset { + 0x410 => modify_reg!(regs.scu410(), pin.bit, pin.clear), + 0x414 => modify_reg!(regs.scu414(), pin.bit, pin.clear), + 0x418 => modify_reg!(regs.scu418(), pin.bit, pin.clear), + 0x41C => modify_reg!(regs.scu41c(), pin.bit, pin.clear), + 0x430 => modify_reg!(regs.scu430(), pin.bit, pin.clear), + 0x434 => modify_reg!(regs.scu434(), pin.bit, pin.clear), + 0x4B0 => modify_reg!(regs.scu4b0(), pin.bit, pin.clear), + 0x4B4 => modify_reg!(regs.scu4b4(), pin.bit, pin.clear), + 0x4B8 => modify_reg!(regs.scu4b8(), pin.bit, pin.clear), + 0x4BC => modify_reg!(regs.scu4bc(), pin.bit, pin.clear), + 0x690 => modify_reg!(regs.scu690(), pin.bit, pin.clear), + 0x694 => modify_reg!(regs.scu694(), pin.bit, pin.clear), + 0x698 => modify_reg!(regs.scu698(), pin.bit, pin.clear), + 0x69C => modify_reg!(regs.scu69c(), pin.bit, pin.clear), + 0x6B0 => modify_reg!(regs.scu6b0(), pin.bit, pin.clear), + _ => {} // Unknown offset, silently ignore + } } - } + }); } } diff --git a/target/ast10x0/peripherals/scu/registers.rs b/target/ast10x0/peripherals/scu/registers.rs index b5e6a8b04..87a10d02c 100644 --- a/target/ast10x0/peripherals/scu/registers.rs +++ b/target/ast10x0/peripherals/scu/registers.rs @@ -5,9 +5,14 @@ use ast1060_pac as device; use core::marker::PhantomData; +use core::sync::atomic::{AtomicU32, Ordering}; const SCU_UNLOCK_KEY: u32 = 0x1688_A8A8; +/// Nesting depth of [`ScuRegisters::unlock_and_lock_after`]; the SCU is +/// unlocked while this is non-zero. +static UNLOCK_DEPTH: AtomicU32 = AtomicU32::new(0); + /// Safe wrapper around the AST10x0 SCU register block. pub struct ScuRegisters { base: *const device::scu::RegisterBlock, @@ -37,26 +42,11 @@ impl ScuRegisters { /// /// # Safety /// Caller must ensure access to the singleton SCU is coordinated. - pub(crate) const unsafe fn new_global() -> Self { + pub const unsafe fn new_global() -> Self { // SAFETY: Caller upholds the singleton access contract. unsafe { Self::new(device::Scu::ptr()) } } - /// Create a register accessor for the global SCU instance, with write - /// protection immediately unlocked. - /// - /// Follows the aspeed-rust pattern: unlock once, then perform all - /// register writes in sequence without re-locking between operations. - /// - /// # Safety - /// Caller must ensure access to the singleton SCU is coordinated. - pub unsafe fn new_global_unlocked() -> Self { - // SAFETY: Caller upholds the singleton access contract. - let scu = unsafe { Self::new_global() }; - scu.unlock_write_protection(); - scu - } - #[inline] pub fn regs(&self) -> &device::scu::RegisterBlock { // SAFETY: Constructor guarantees a valid SCU register block pointer. @@ -64,13 +54,51 @@ impl ScuRegisters { } /// Unlock SCU write-protected registers for subsequent write operations. - /// - /// Call this once before a sequence of SCU writes, following the aspeed-rust - /// pattern of a single unlock per batch of register operations. #[inline] - pub(crate) fn unlock_write_protection(&self) { + fn unlock_write_protection(&self) { self.regs() .scu000() .write(|w| unsafe { w.bits(SCU_UNLOCK_KEY) }); } + + /// Lock SCU write-protected registers. + #[inline] + fn lock_write_protection(&self) { + self.regs().scu000().write(|w| unsafe { w.bits(0) }); + } + + /// Run `f` with the SCU write-protected registers unlocked, then re-lock. + /// + /// Calls may be nested: only the outermost call unlocks and only the + /// outermost call re-locks, so a setter that takes the lock itself can be + /// invoked from inside another `unlock_and_lock_after` closure without + /// re-locking the SCU under the caller. + /// + /// The SCU is not re-locked if `f` panics. + /// + /// # Example + /// ```rust,no_run + /// # use ast10x0_peripherals::scu::ScuRegisters; + /// // SAFETY: example only; the caller owns the singleton SCU. + /// let scu = unsafe { ScuRegisters::new_global() }; + /// scu.unlock_and_lock_after(|scu| { + /// let mut bits = scu.regs().scu0f0().read().bits(); + /// bits &= !0xF; + /// scu.regs().scu0f0().write(|w| unsafe { w.bits(bits) }); + /// }); + /// ``` + #[inline] + pub fn unlock_and_lock_after(&self, f: F) -> R + where + F: FnOnce(&ScuRegisters) -> R, + { + if UNLOCK_DEPTH.fetch_add(1, Ordering::AcqRel) == 0 { + self.unlock_write_protection(); + } + let result = f(self); + if UNLOCK_DEPTH.fetch_sub(1, Ordering::AcqRel) == 1 { + self.lock_write_protection(); + } + result + } } diff --git a/target/ast10x0/peripherals/scu/reset.rs b/target/ast10x0/peripherals/scu/reset.rs index 73dc09afc..db5c4a9ee 100644 --- a/target/ast10x0/peripherals/scu/reset.rs +++ b/target/ast10x0/peripherals/scu/reset.rs @@ -9,30 +9,32 @@ use super::types::ScuRegisterHalf; impl ScuRegisters { /// Assert reset bits in the selected reset-control register half. /// - /// Caller must call `unlock_write_protection()` before this. + /// Unlocks the SCU for the duration of the write; see + /// [`ScuRegisters::unlock_and_lock_after`]. pub fn assert_reset_mask(&self, half: ScuRegisterHalf, mask: u32) { - match half { + self.unlock_and_lock_after(|scu| match half { ScuRegisterHalf::Lower => { - self.regs().scu040().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu040().write(|w| unsafe { w.bits(mask) }); } ScuRegisterHalf::Upper => { - self.regs().scu050().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu050().write(|w| unsafe { w.bits(mask) }); } - } + }); } /// Deassert reset bits in the selected reset-control register half. /// - /// Caller must call `unlock_write_protection()` before this. + /// Unlocks the SCU for the duration of the write; see + /// [`ScuRegisters::unlock_and_lock_after`]. pub fn deassert_reset_mask(&self, half: ScuRegisterHalf, mask: u32) { - match half { + self.unlock_and_lock_after(|scu| match half { ScuRegisterHalf::Lower => { - self.regs().scu044().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu044().write(|w| unsafe { w.bits(mask) }); } ScuRegisterHalf::Upper => { - self.regs().scu054().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu054().write(|w| unsafe { w.bits(mask) }); } - } + }); } /// Read the selected reset-control register half. diff --git a/target/ast10x0/peripherals/scu/routing.rs b/target/ast10x0/peripherals/scu/routing.rs index b99d1bd7b..dd1782fa0 100644 --- a/target/ast10x0/peripherals/scu/routing.rs +++ b/target/ast10x0/peripherals/scu/routing.rs @@ -186,50 +186,53 @@ impl ScuRegisters { instance: SpiMonitorInstance, passthrough: SpiMonitorPassthrough, ) { - self.unlock_write_protection(); - let enable = passthrough.is_enabled(); - - self.regs().scu0f0().modify(|_, w| match instance { - SpiMonitorInstance::Spim0 => w.enbl_passthrough_of_spipf1().bit(enable), - SpiMonitorInstance::Spim1 => w.enbl_passthrough_of_spipf2().bit(enable), - SpiMonitorInstance::Spim2 => w.enbl_passthrough_of_spipf3().bit(enable), - SpiMonitorInstance::Spim3 => w.enbl_passthrough_of_spipf4().bit(enable), + self.unlock_and_lock_after(|scu| { + let enable = passthrough.is_enabled(); + + scu.regs().scu0f0().modify(|_, w| match instance { + SpiMonitorInstance::Spim0 => w.enbl_passthrough_of_spipf1().bit(enable), + SpiMonitorInstance::Spim1 => w.enbl_passthrough_of_spipf2().bit(enable), + SpiMonitorInstance::Spim2 => w.enbl_passthrough_of_spipf3().bit(enable), + SpiMonitorInstance::Spim3 => w.enbl_passthrough_of_spipf4().bit(enable), + }); }); } /// Enable or disable the SCU-side SPI monitor filter path. Uses SCU0F0[11:8]. pub fn set_spim_filter(&self, instance: SpiMonitorInstance, enable: bool) { - self.unlock_write_protection(); - self.regs().scu0f0().modify(|_, w| match instance { - SpiMonitorInstance::Spim0 => w.enbl_filter_of_spipf1().bit(enable), - SpiMonitorInstance::Spim1 => w.enbl_filter_of_spipf2().bit(enable), - SpiMonitorInstance::Spim2 => w.enbl_filter_of_spipf3().bit(enable), - SpiMonitorInstance::Spim3 => w.enbl_filter_of_spipf4().bit(enable), + self.unlock_and_lock_after(|scu| { + scu.regs().scu0f0().modify(|_, w| match instance { + SpiMonitorInstance::Spim0 => w.enbl_filter_of_spipf1().bit(enable), + SpiMonitorInstance::Spim1 => w.enbl_filter_of_spipf2().bit(enable), + SpiMonitorInstance::Spim2 => w.enbl_filter_of_spipf3().bit(enable), + SpiMonitorInstance::Spim3 => w.enbl_filter_of_spipf4().bit(enable), + }); }); } /// Disable the internal pull-down on the monitor CS output pin. pub fn disable_spim_cs_internal_pull_down(&self, instance: SpiMonitorInstance) { - self.unlock_write_protection(); - match instance { - SpiMonitorInstance::Spim0 => { - self.regs() - .scu610() - .modify(|r, w| unsafe { w.bits(r.bits() | (1 << 6)) }); - } - SpiMonitorInstance::Spim1 => { - self.regs() - .scu610() - .modify(|r, w| unsafe { w.bits(r.bits() | (1 << 20)) }); - } - // SPIM3's corresponding pin cannot have its pull-down disabled. - SpiMonitorInstance::Spim2 => {} - SpiMonitorInstance::Spim3 => { - self.regs() - .scu614() - .modify(|r, w| unsafe { w.bits(r.bits() | (1 << 16)) }); + self.unlock_and_lock_after(|scu| { + match instance { + SpiMonitorInstance::Spim0 => { + scu.regs() + .scu610() + .modify(|r, w| unsafe { w.bits(r.bits() | (1 << 6)) }); + } + SpiMonitorInstance::Spim1 => { + scu.regs() + .scu610() + .modify(|r, w| unsafe { w.bits(r.bits() | (1 << 20)) }); + } + // SPIM3's corresponding pin cannot have its pull-down disabled. + SpiMonitorInstance::Spim2 => {} + SpiMonitorInstance::Spim3 => { + scu.regs() + .scu614() + .modify(|r, w| unsafe { w.bits(r.bits() | (1 << 16)) }); + } } - } + }); } /// Route an internal SPI master through the selected SPI monitor path. @@ -238,15 +241,16 @@ impl ScuRegisters { instance: SpiMonitorInstance, source: SpiMonitorSource, ) { - self.unlock_write_protection(); - self.regs() - .scu0f0() - .modify(|_, w| unsafe { w.select_int_spimaster_connection().bits(instance as u8 + 1) }); - - let select_spi2 = matches!(source, SpiMonitorSource::Spi2); - self.regs() - .scu0f0() - .modify(|_, w| w.int_spimaster_sel().bit(select_spi2)); + self.unlock_and_lock_after(|scu| { + scu.regs().scu0f0().modify(|_, w| unsafe { + w.select_int_spimaster_connection().bits(instance as u8 + 1) + }); + + let select_spi2 = matches!(source, SpiMonitorSource::Spi2); + scu.regs() + .scu0f0() + .modify(|_, w| w.int_spimaster_sel().bit(select_spi2)); + }); } /// Select the internal SPI master source and SPIM output path. @@ -258,105 +262,108 @@ impl ScuRegisters { return Err(super::types::ScuError::InvalidMuxSelection); } - self.unlock_write_protection(); - let source_bit = if matches!(source, SpiMonitorSource::Spi2) { - 0x8 - } else { - 0 - }; - let mut bits = self.regs().scu0f0().read().bits(); - bits = (bits & !0xF) | source_bit | u32::from(output_select); - self.regs().scu0f0().write(|w| unsafe { w.bits(bits) }); - Ok(()) + self.unlock_and_lock_after(|scu| { + let source_bit = if matches!(source, SpiMonitorSource::Spi2) { + 0x8 + } else { + 0 + }; + let mut bits = scu.regs().scu0f0().read().bits(); + bits = (bits & !0xF) | source_bit | u32::from(output_select); + scu.regs().scu0f0().write(|w| unsafe { w.bits(bits) }); + Ok(()) + }) } /// Disable any internal SPI-master detour route. pub fn clear_spim_internal_master_route(&self) { - self.unlock_write_protection(); - let mut bits = self.regs().scu0f0().read().bits(); - bits &= !0xF; - self.regs().scu0f0().write(|w| unsafe { w.bits(bits) }); + self.unlock_and_lock_after(|scu| { + let mut bits = scu.regs().scu0f0().read().bits(); + bits &= !0xF; + scu.regs().scu0f0().write(|w| unsafe { w.bits(bits) }); + }); } /// Apply AST1060 SPIM proprietary pin setup before a transaction. pub fn spim_proprietary_pre_config(&self) -> Option { - self.unlock_write_protection(); - - let scu0f0 = self.regs().scu0f0().read().bits(); - pw_log::debug!("SPIM pre-config: SCU0F0=0x{:08x}", scu0f0 as u32); - let op_idx = match ast1060_spim_op_idx(scu0f0) { - Some(idx) => idx, - None => return None, - }; - pw_log::debug!( - "SPIM pre-config: active SPIM index {}, op index {}", - ((scu0f0 & 0x7) - 1) as u32, - op_idx as u32 - ); - - let clk = AST1060_SPIM_CLK_GPIO[op_idx]; - let cs = AST1060_SPIM_CS_GPIO[op_idx]; - let mut clk_gpio_ori_val = [0u32; 4]; - // Change the paired SPIM CLKOUT pin to GPIO mode. - self.set_spim_pin_function(clk, false); - - // Save its GPIO direction bit, then configure it as an input. - clk_gpio_ori_val[op_idx] = gpio_direction(clk.gpio_group) & clk.gpio_bit_mask; - gpio_set_direction(clk.gpio_group, clk.gpio_bit_mask, false); - - // Drive the paired SPIM CSOUT GPIO high and configure it as output. - gpio_set_output(cs.gpio_group, cs.gpio_bit_mask, true); - - // Change the paired SPIM CSOUT pin to GPIO mode. - self.set_spim_pin_function(cs, false); - - pw_log::debug!( - "SPIM pre-config: op index {}, saved CLK direction mask 0x{:08x}", - op_idx as u32, - clk_gpio_ori_val[op_idx] as u32 - ); - Some(SpimGpioOriVal { clk_gpio_ori_val }) + self.unlock_and_lock_after(|scu| { + let scu0f0 = scu.regs().scu0f0().read().bits(); + pw_log::debug!("SPIM pre-config: SCU0F0=0x{:08x}", scu0f0 as u32); + let op_idx = match ast1060_spim_op_idx(scu0f0) { + Some(idx) => idx, + None => return None, + }; + pw_log::debug!( + "SPIM pre-config: active SPIM index {}, op index {}", + ((scu0f0 & 0x7) - 1) as u32, + op_idx as u32 + ); + + let clk = AST1060_SPIM_CLK_GPIO[op_idx]; + let cs = AST1060_SPIM_CS_GPIO[op_idx]; + let mut clk_gpio_ori_val = [0u32; 4]; + // Change the paired SPIM CLKOUT pin to GPIO mode. + scu.set_spim_pin_function(clk, false); + + // Save its GPIO direction bit, then configure it as an input. + clk_gpio_ori_val[op_idx] = gpio_direction(clk.gpio_group) & clk.gpio_bit_mask; + gpio_set_direction(clk.gpio_group, clk.gpio_bit_mask, false); + + // Drive the paired SPIM CSOUT GPIO high and configure it as output. + gpio_set_output(cs.gpio_group, cs.gpio_bit_mask, true); + + // Change the paired SPIM CSOUT pin to GPIO mode. + scu.set_spim_pin_function(cs, false); + + pw_log::debug!( + "SPIM pre-config: op index {}, saved CLK direction mask 0x{:08x}", + op_idx as u32, + clk_gpio_ori_val[op_idx] as u32 + ); + Some(SpimGpioOriVal { clk_gpio_ori_val }) + }) } /// Restore AST1060 SPIM proprietary pin state after a transaction. pub fn spim_proprietary_post_config(&self, gpio_ori_val: SpimGpioOriVal) { - self.unlock_write_protection(); - - let scu0f0 = self.regs().scu0f0().read().bits(); - let op_idx = match ast1060_spim_op_idx(scu0f0) { - Some(idx) => idx, - None => return, - }; - let clk = AST1060_SPIM_CLK_GPIO[op_idx]; - let cs = AST1060_SPIM_CS_GPIO[op_idx]; - pw_log::debug!( - "SPIM post-config: SCU0F0=0x{:08x}, op index {}, saved CLK direction mask 0x{:08x}", - scu0f0 as u32, - op_idx as u32, - gpio_ori_val.clk_gpio_ori_val[op_idx] as u32 - ); - - // Restore the paired CLKOUT GPIO direction bit. - let was_output = gpio_ori_val.clk_gpio_ori_val[op_idx] != 0; - gpio_set_direction(clk.gpio_group, clk.gpio_bit_mask, was_output); - - // Return paired CLKOUT and CSOUT pins to SPIM mode. - self.set_spim_pin_function(clk, true); - self.set_spim_pin_function(cs, true); - - pw_log::debug!("SPIM post-config: restore complete"); + self.unlock_and_lock_after(|scu| { + let scu0f0 = scu.regs().scu0f0().read().bits(); + let op_idx = match ast1060_spim_op_idx(scu0f0) { + Some(idx) => idx, + None => return, + }; + let clk = AST1060_SPIM_CLK_GPIO[op_idx]; + let cs = AST1060_SPIM_CS_GPIO[op_idx]; + pw_log::debug!( + "SPIM post-config: SCU0F0=0x{:08x}, op index {}, saved CLK direction mask 0x{:08x}", + scu0f0 as u32, + op_idx as u32, + gpio_ori_val.clk_gpio_ori_val[op_idx] as u32 + ); + + // Restore the paired CLKOUT GPIO direction bit. + let was_output = gpio_ori_val.clk_gpio_ori_val[op_idx] != 0; + gpio_set_direction(clk.gpio_group, clk.gpio_bit_mask, was_output); + + // Return paired CLKOUT and CSOUT pins to SPIM mode. + self.set_spim_pin_function(clk, true); + self.set_spim_pin_function(cs, true); + + pw_log::debug!("SPIM post-config: restore complete"); + }); } /// Select the external mux signal for a SPI monitor instance. Uses SCU0F0[15:12]. pub fn set_spim_ext_mux(&self, instance: SpiMonitorInstance, mux: ScuExtMuxSelect) { - self.unlock_write_protection(); - let bit = mux.as_bool(); - - self.regs().scu0f0().modify(|_, w| match instance { - SpiMonitorInstance::Spim0 => w.ext_mux_select_sig_of_spipf1().bit(bit), - SpiMonitorInstance::Spim1 => w.ext_mux_select_sig_of_spipf2().bit(bit), - SpiMonitorInstance::Spim2 => w.ext_mux_select_sig_of_spipf3().bit(bit), - SpiMonitorInstance::Spim3 => w.ext_mux_select_sig_of_spipf4().bit(bit), + self.unlock_and_lock_after(|scu| { + let bit = mux.as_bool(); + + scu.regs().scu0f0().modify(|_, w| match instance { + SpiMonitorInstance::Spim0 => w.ext_mux_select_sig_of_spipf1().bit(bit), + SpiMonitorInstance::Spim1 => w.ext_mux_select_sig_of_spipf2().bit(bit), + SpiMonitorInstance::Spim2 => w.ext_mux_select_sig_of_spipf3().bit(bit), + SpiMonitorInstance::Spim3 => w.ext_mux_select_sig_of_spipf4().bit(bit), + }); }); } @@ -380,29 +387,28 @@ impl ScuRegisters { /// Enable or disable the SCU-controlled MISO multi-function pin for a SPI /// monitor instance. pub fn set_spim_miso_multi_func(&self, instance: SpiMonitorInstance, enable: bool) { - self.unlock_write_protection(); - match instance { + self.unlock_and_lock_after(|scu| match instance { SpiMonitorInstance::Spim0 => { - self.regs() + scu.regs() .scu690() .modify(|_, w| w.enbl_qspimonitor1misoin_fn_pin().bit(enable)); } SpiMonitorInstance::Spim1 => { - self.regs() + scu.regs() .scu690() .modify(|_, w| w.enbl_qspimonitor2misoin_fn_pin().bit(enable)); } SpiMonitorInstance::Spim2 => { - self.regs() + scu.regs() .scu690() .modify(|_, w| w.enbl_qspimonitor3misoin_fn_pin().bit(enable)); } SpiMonitorInstance::Spim3 => { - self.regs() + scu.regs() .scu694() .modify(|_, w| w.enbl_qspimonitor4misoin_fn_pin().bit(enable)); } - } + }) } /// Read the raw SPI-monitor routing control register image. diff --git a/target/ast10x0/peripherals/smc/spi/spi_transaction.rs b/target/ast10x0/peripherals/smc/spi/spi_transaction.rs index fb3e93e83..4f737e453 100644 --- a/target/ast10x0/peripherals/smc/spi/spi_transaction.rs +++ b/target/ast10x0/peripherals/smc/spi/spi_transaction.rs @@ -38,17 +38,22 @@ impl<'a> SpiTransaction<'a> { }; if let Some(spim) = spim { - let scu = unsafe { ScuRegisters::new_global_unlocked() }; - scu.validate_spim_instance(spim) - .map_err(|_| SmcError::HardwareError)?; + let scu = unsafe { ScuRegisters::new_global() }; - if cs.master_idx() != 0 { - scu.set_spim_internal_mux(Self::spim_source(&cs)?, spim as u8 + 1) + scu.unlock_and_lock_after(|scu| { + scu.validate_spim_instance(spim) .map_err(|_| SmcError::HardwareError)?; - mux_state.internal_mux_active = true; - } - mux_state.proprietary_state = scu.spim_proprietary_pre_config(); + if cs.master_idx() != 0 { + scu.set_spim_internal_mux(Self::spim_source(&cs)?, spim as u8 + 1) + .map_err(|_| SmcError::HardwareError)?; + mux_state.internal_mux_active = true; + } + + mux_state.proprietary_state = scu.spim_proprietary_pre_config(); + + Ok(()) + })?; } Ok(Self { @@ -141,15 +146,16 @@ impl<'a> SpiTransaction<'a> { fn finish_restore(&mut self) -> Result<(), SmcError> { if let Some(state) = self.previous_mux.take() { if state.spim_present { - let scu = unsafe { ScuRegisters::new_global_unlocked() }; - - if let Some(proprietary_state) = state.proprietary_state { - scu.spim_proprietary_post_config(proprietary_state); - } - - if state.internal_mux_active { - scu.clear_spim_internal_master_route(); - } + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| { + if let Some(proprietary_state) = state.proprietary_state { + scu.spim_proprietary_post_config(proprietary_state); + } + + if state.internal_mux_active { + scu.clear_spim_internal_master_route(); + } + }) } } @@ -169,15 +175,16 @@ impl Drop for SpiTransaction<'_> { fn drop(&mut self) { if let Some(state) = self.previous_mux.take() { if state.spim_present { - let scu = unsafe { ScuRegisters::new_global_unlocked() }; - - if let Some(proprietary_state) = state.proprietary_state { - scu.spim_proprietary_post_config(proprietary_state); - } - - if state.internal_mux_active { - scu.clear_spim_internal_master_route(); - } + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| { + if let Some(proprietary_state) = state.proprietary_state { + scu.spim_proprietary_post_config(proprietary_state); + } + + if state.internal_mux_active { + scu.clear_spim_internal_master_route(); + } + }) } } } diff --git a/target/ast10x0/tests/flash/target.rs b/target/ast10x0/tests/flash/target.rs index 39871e6e3..75f3b1241 100644 --- a/target/ast10x0/tests/flash/target.rs +++ b/target/ast10x0/tests/flash/target.rs @@ -199,7 +199,7 @@ fn run_with_backup(flash: &mut impl Flash, program_page: usize) -> Result<(), &' fn run_flash_test() -> Result<(), &'static str> { // SAFETY: kernel main() runs once, single-threaded, with exclusive // hardware ownership. - let scu = unsafe { ScuRegisters::new_global_unlocked() }; + let scu = unsafe { ScuRegisters::new_global() }; scu.apply_pinctrl_group(PINCTRL_FMC_QUAD); // SAFETY: this image has no other user of the FMC, and this runs once. diff --git a/target/ast10x0/tests/flash_service/target.rs b/target/ast10x0/tests/flash_service/target.rs index e7cfabc3a..f90039c90 100644 --- a/target/ast10x0/tests/flash_service/target.rs +++ b/target/ast10x0/tests/flash_service/target.rs @@ -26,7 +26,7 @@ impl TargetInterface for Target { // shared pinctrl registers). // SAFETY: kernel main() runs once, single-threaded, with exclusive // hardware ownership. - let scu = unsafe { ScuRegisters::new_global_unlocked() }; + let scu = unsafe { ScuRegisters::new_global() }; scu.apply_pinctrl_group(PINCTRL_FMC_QUAD); codegen::start(); diff --git a/target/ast10x0/tests/peripherals/gpio/gpio_irq/target.rs b/target/ast10x0/tests/peripherals/gpio/gpio_irq/target.rs index cb8b234f4..cc4b20398 100644 --- a/target/ast10x0/tests/peripherals/gpio/gpio_irq/target.rs +++ b/target/ast10x0/tests/peripherals/gpio/gpio_irq/target.rs @@ -20,8 +20,10 @@ impl TargetInterface for Target { fn main() -> ! { // SAFETY: single call at boot with exclusive access to SCU global registers. unsafe { - let scu = ScuRegisters::new_global_unlocked(); - scu.apply_pinctrl_group(pinctrl::PINCTRL_GPIOA0); + let scu = ScuRegisters::new_global(); + scu.unlock_and_lock_after(|scu| { + scu.apply_pinctrl_group(pinctrl::PINCTRL_GPIOA0); + }); } codegen::start(); diff --git a/target/ast10x0/tests/peripherals/gpio/gpio_smoke/target.rs b/target/ast10x0/tests/peripherals/gpio/gpio_smoke/target.rs index 15e9192b8..2b5a86985 100644 --- a/target/ast10x0/tests/peripherals/gpio/gpio_smoke/target.rs +++ b/target/ast10x0/tests/peripherals/gpio/gpio_smoke/target.rs @@ -17,14 +17,14 @@ pub struct Target {} fn run_gpioa_test() -> bool { // SAFETY: this kernel-only test is the sole owner of the SCU and GPIO peripherals. - let gpioa = unsafe { - let scu = ScuRegisters::new_global_unlocked(); + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| { scu.apply_pinctrl_group(pinctrl::PINCTRL_GPIOA0); scu.apply_pinctrl_group(pinctrl::PINCTRL_GPIOA1); scu.apply_pinctrl_group(pinctrl::PINCTRL_GPIOA3); scu.apply_pinctrl_group(pinctrl::PINCTRL_GPIOA4); - gpioa::GPIOA::new_global().split() - }; + }); + let gpioa = unsafe { gpioa::GPIOA::new_global().split() }; pw_log::info!("=== AST10x0 GPIOA smoke test ==="); // AST1060 does not provide internal pull-up or pull-down control for these diff --git a/target/ast10x0/tests/peripherals/gpio/gpioj_loopback/target.rs b/target/ast10x0/tests/peripherals/gpio/gpioj_loopback/target.rs index e1e286d6b..7837a591e 100644 --- a/target/ast10x0/tests/peripherals/gpio/gpioj_loopback/target.rs +++ b/target/ast10x0/tests/peripherals/gpio/gpioj_loopback/target.rs @@ -18,14 +18,15 @@ pub struct Target {} const GPIO_LEVEL_DELAY_CYCLES: u32 = 200_000_000; fn test_gpio_loopback() -> bool { - // The loopback requires a jumper between the fixture board's exposed - // GPIOJ4 pin and GPIOJ5 pin. - let gpioj = unsafe { - let scu = ScuRegisters::new_global_unlocked(); + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| { scu.apply_pinctrl_group(pinctrl::PINCTRL_GPIOJ4); scu.apply_pinctrl_group(pinctrl::PINCTRL_GPIOJ5); - gpioj::GPIOJ::new_global().split() - }; + }); + + // The loopback requires a jumper between the fixture board's exposed + // GPIOJ4 pin and GPIOJ5 pin. + let gpioj = unsafe { gpioj::GPIOJ::new_global().split() }; pw_log::info!("--- GPIOJ loopback test ---"); let mut output = gpioj.pj4.into_push_pull_output(); diff --git a/target/ast10x0/tests/peripherals/i2c/i2c_slave_rx_burst/master_target.rs b/target/ast10x0/tests/peripherals/i2c/i2c_slave_rx_burst/master_target.rs index 4dffc6c62..46f69cfef 100644 --- a/target/ast10x0/tests/peripherals/i2c/i2c_slave_rx_burst/master_target.rs +++ b/target/ast10x0/tests/peripherals/i2c/i2c_slave_rx_burst/master_target.rs @@ -21,19 +21,21 @@ impl TargetInterface for Target { // global resource and must not be mapped or touched by userspace. // SAFETY: kernel main() runs once with exclusive hardware ownership. unsafe { - let scu = ast10x0_peripherals::scu::ScuRegisters::new_global_unlocked(); - scu.ungate_clock_mask(ast10x0_peripherals::scu::ClockRegisterHalf::Lower, 1 << 2); - scu.assert_reset_mask(ast10x0_peripherals::scu::ScuRegisterHalf::Upper, 1 << 2); - // brief settle delay (spin) - for _ in 0..10_000u32 { - core::hint::spin_loop(); - } - scu.deassert_reset_mask(ast10x0_peripherals::scu::ScuRegisterHalf::Upper, 1 << 2); - for _ in 0..10_000u32 { - core::hint::spin_loop(); - } - scu.apply_pinctrl_group(pinctrl::PINCTRL_I2C2); - i2c::init_i2c_global(); + let scu = ast10x0_peripherals::scu::ScuRegisters::new_global(); + scu.unlock_and_lock_after(|scu| { + scu.ungate_clock_mask(ast10x0_peripherals::scu::ClockRegisterHalf::Lower, 1 << 2); + scu.assert_reset_mask(ast10x0_peripherals::scu::ScuRegisterHalf::Upper, 1 << 2); + // brief settle delay (spin) + for _ in 0..10_000u32 { + core::hint::spin_loop(); + } + scu.deassert_reset_mask(ast10x0_peripherals::scu::ScuRegisterHalf::Upper, 1 << 2); + for _ in 0..10_000u32 { + core::hint::spin_loop(); + } + scu.apply_pinctrl_group(pinctrl::PINCTRL_I2C2); + i2c::init_i2c_global(); + }) } codegen::start(); diff --git a/target/ast10x0/tests/peripherals/i2c/i2c_slave_rx_burst/target.rs b/target/ast10x0/tests/peripherals/i2c/i2c_slave_rx_burst/target.rs index 70bfd16b4..c7f97180f 100644 --- a/target/ast10x0/tests/peripherals/i2c/i2c_slave_rx_burst/target.rs +++ b/target/ast10x0/tests/peripherals/i2c/i2c_slave_rx_burst/target.rs @@ -21,19 +21,22 @@ impl TargetInterface for Target { // global resource and must not be mapped or touched by userspace. // SAFETY: kernel main() runs once with exclusive hardware ownership. unsafe { - let scu = ast10x0_peripherals::scu::ScuRegisters::new_global_unlocked(); - scu.ungate_clock_mask(ast10x0_peripherals::scu::ClockRegisterHalf::Lower, 1 << 2); - scu.assert_reset_mask(ast10x0_peripherals::scu::ScuRegisterHalf::Upper, 1 << 2); - // brief settle delay (spin) - for _ in 0..10_000u32 { - core::hint::spin_loop(); - } - scu.deassert_reset_mask(ast10x0_peripherals::scu::ScuRegisterHalf::Upper, 1 << 2); - for _ in 0..10_000u32 { - core::hint::spin_loop(); - } - scu.apply_pinctrl_group(pinctrl::PINCTRL_I2C2); - i2c::init_i2c_global(); + let scu = ast10x0_peripherals::scu::ScuRegisters::new_global(); + + scu.unlock_and_lock_after(|scu| { + scu.ungate_clock_mask(ast10x0_peripherals::scu::ClockRegisterHalf::Lower, 1 << 2); + scu.assert_reset_mask(ast10x0_peripherals::scu::ScuRegisterHalf::Upper, 1 << 2); + // brief settle delay (spin) + for _ in 0..10_000u32 { + core::hint::spin_loop(); + } + scu.deassert_reset_mask(ast10x0_peripherals::scu::ScuRegisterHalf::Upper, 1 << 2); + for _ in 0..10_000u32 { + core::hint::spin_loop(); + } + scu.apply_pinctrl_group(pinctrl::PINCTRL_I2C2); + i2c::init_i2c_global(); + }); } codegen::start(); diff --git a/target/ast10x0/tests/peripherals/i2c/i2c_slave_rx_ipc/target.rs b/target/ast10x0/tests/peripherals/i2c/i2c_slave_rx_ipc/target.rs index e4c1e5cef..8dacaa8a7 100644 --- a/target/ast10x0/tests/peripherals/i2c/i2c_slave_rx_ipc/target.rs +++ b/target/ast10x0/tests/peripherals/i2c/i2c_slave_rx_ipc/target.rs @@ -22,18 +22,20 @@ impl TargetInterface for Target { // global resource and must not be mapped or touched by userspace. // SAFETY: kernel main() runs once with exclusive hardware ownership. unsafe { - let scu = ast10x0_peripherals::scu::ScuRegisters::new_global_unlocked(); - scu.ungate_clock_mask(ast10x0_peripherals::scu::ClockRegisterHalf::Lower, 1 << 2); - scu.assert_reset_mask(ast10x0_peripherals::scu::ScuRegisterHalf::Upper, 1 << 2); - // brief settle delay (spin) - for _ in 0..10_000u32 { - core::hint::spin_loop(); - } - scu.deassert_reset_mask(ast10x0_peripherals::scu::ScuRegisterHalf::Upper, 1 << 2); - for _ in 0..10_000u32 { - core::hint::spin_loop(); - } - scu.apply_pinctrl_group(pinctrl::PINCTRL_I2C2); + let scu = ast10x0_peripherals::scu::ScuRegisters::new_global(); + scu.unlock_and_lock_after(|scu| { + scu.ungate_clock_mask(ast10x0_peripherals::scu::ClockRegisterHalf::Lower, 1 << 2); + scu.assert_reset_mask(ast10x0_peripherals::scu::ScuRegisterHalf::Upper, 1 << 2); + // brief settle delay (spin) + for _ in 0..10_000u32 { + core::hint::spin_loop(); + } + scu.deassert_reset_mask(ast10x0_peripherals::scu::ScuRegisterHalf::Upper, 1 << 2); + for _ in 0..10_000u32 { + core::hint::spin_loop(); + } + scu.apply_pinctrl_group(pinctrl::PINCTRL_I2C2); + }); i2c::init_i2c_global(); } diff --git a/target/ast10x0/tests/peripherals/sgpiom/sgpiom_irq/target.rs b/target/ast10x0/tests/peripherals/sgpiom/sgpiom_irq/target.rs index 7a491562e..f5c091b02 100644 --- a/target/ast10x0/tests/peripherals/sgpiom/sgpiom_irq/target.rs +++ b/target/ast10x0/tests/peripherals/sgpiom/sgpiom_irq/target.rs @@ -28,10 +28,10 @@ impl TargetInterface for Target { fn main() -> ! { // SAFETY: single call at boot with exclusive access to SCU global regs. - unsafe { - let scu = ScuRegisters::new_global_unlocked(); + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| { scu.apply_pinctrl_group(pinctrl::PINCTRL_SGPIOM); - } + }); codegen::start(); loop { diff --git a/target/ast10x0/tests/smc/dma_irq/target.rs b/target/ast10x0/tests/smc/dma_irq/target.rs index 04472d6df..6ed3c6cf5 100644 --- a/target/ast10x0/tests/smc/dma_irq/target.rs +++ b/target/ast10x0/tests/smc/dma_irq/target.rs @@ -111,8 +111,8 @@ fn poll_dma_irq_completion(cs: &mut Cs<'_>) -> Poll> { } fn run_dma_read_irq_test() -> Result<(), SmcError> { - let scu = unsafe { ScuRegisters::new_global_unlocked() }; - scu.apply_pinctrl_group(PINCTRL_FMC_QUAD); + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| scu.apply_pinctrl_group(PINCTRL_FMC_QUAD)); let mut controller = unsafe { FmcUninit::::new()? }.init()?; diff --git a/target/ast10x0/tests/smc/read/target.rs b/target/ast10x0/tests/smc/read/target.rs index fbc0e9758..a08c4c7c0 100644 --- a/target/ast10x0/tests/smc/read/target.rs +++ b/target/ast10x0/tests/smc/read/target.rs @@ -61,8 +61,10 @@ static mut SMC_DMA_BUF: [u8; 256] = [0u8; 256]; fn run_smc_read_test() -> Result<(), SmcError> { // --- 1. Init --- // TODO:: set pinctrl in board/src/lib.rs - let scu = unsafe { ScuRegisters::new_global_unlocked() }; - scu.apply_pinctrl_group(PINCTRL_FMC_QUAD); + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| { + scu.apply_pinctrl_group(PINCTRL_FMC_QUAD); + }); pw_log::info!("=== AST10x0 smc read test ==="); let mut controller = unsafe { FmcUninit::::new()? }.init()?; diff --git a/target/ast10x0/tests/smc/read/target_spi1.rs b/target/ast10x0/tests/smc/read/target_spi1.rs index 1ec3bb6d3..cb043ec47 100644 --- a/target/ast10x0/tests/smc/read/target_spi1.rs +++ b/target/ast10x0/tests/smc/read/target_spi1.rs @@ -50,22 +50,24 @@ pub struct Target {} /// path as the active master. fn config_spi1_master_controller() -> Result<(), SmcError> { const SPIM: SpiMonitorInstance = SpiMonitorInstance::Spim0; - let scu = unsafe { ScuRegisters::new_global_unlocked() }; + let scu = unsafe { ScuRegisters::new_global() }; - if !enable_flash_power(&scu) || !release_spi_flash_resets() || !set_bmc_resets(true) { - return Err(SmcError::HardwareError); - } - - apply_spim_pinctrl(&scu, SPIM); - scu.apply_pinctrl_group(PINCTRL_SPI1_QUAD); - scu.disable_spim_cs_internal_pull_down(SPIM); - scu.set_spim_passthrough(SPIM, SpiMonitorPassthrough::Enabled); - apply_spim_external_mux(SPIM, ScuExtMuxSelect::Mux1); - scu.set_spim_internal_mux(SpiMonitorSource::Spi1, SPIM as u8 + 1) - .map_err(|_| SmcError::HardwareError)?; + scu.unlock_and_lock_after(|scu| { + if !enable_flash_power(scu) || !release_spi_flash_resets() || !set_bmc_resets(true) { + return Err(SmcError::HardwareError); + } - pw_log::info!("SCU pinmux and SPIM routing configured for SPI1 staging flash"); - Ok(()) + apply_spim_pinctrl(&scu, SPIM); + scu.apply_pinctrl_group(PINCTRL_SPI1_QUAD); + scu.disable_spim_cs_internal_pull_down(SPIM); + scu.set_spim_passthrough(SPIM, SpiMonitorPassthrough::Enabled); + apply_spim_external_mux(SPIM, ScuExtMuxSelect::Mux1); + scu.set_spim_internal_mux(SpiMonitorSource::Spi1, SPIM as u8 + 1) + .map_err(|_| SmcError::HardwareError)?; + + pw_log::info!("SCU pinmux and SPIM routing configured for SPI1 staging flash"); + Ok(()) + }) } fn run_spi1_read_test() -> Result<(), SmcError> { diff --git a/target/ast10x0/tests/smc/read/target_spi2.rs b/target/ast10x0/tests/smc/read/target_spi2.rs index e3407062b..6d1a68e57 100644 --- a/target/ast10x0/tests/smc/read/target_spi2.rs +++ b/target/ast10x0/tests/smc/read/target_spi2.rs @@ -34,26 +34,28 @@ const SPI_FLASH_CONFIG: FlashConfig = FlashConfig { pub struct Target {} fn config_spi2_master_controller() -> Result<(), SmcError> { - let scu = unsafe { ScuRegisters::new_global_unlocked() }; - scu.apply_pinctrl_group(PINCTRL_SPIM3_DEFAULT); - scu.apply_pinctrl_group(PINCTRL_SPIM4_DEFAULT); - scu.apply_pinctrl_group(PINCTRL_SPI2_QUAD); - if !enable_flash_power(&scu) { - pw_log::info!("GPIOL2/GPIOL3 flash power readback failed"); - return Err(SmcError::HardwareError); - } - apply_spim_external_mux(SpiMonitorInstance::Spim2, ScuExtMuxSelect::Mux1); + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| { + scu.apply_pinctrl_group(PINCTRL_SPIM3_DEFAULT); + scu.apply_pinctrl_group(PINCTRL_SPIM4_DEFAULT); + scu.apply_pinctrl_group(PINCTRL_SPI2_QUAD); + if !enable_flash_power(scu) { + pw_log::info!("GPIOL2/GPIOL3 flash power readback failed"); + return Err(SmcError::HardwareError); + } + apply_spim_external_mux(SpiMonitorInstance::Spim2, ScuExtMuxSelect::Mux1); - // Configure the mux for the SPI master controller path. - scu.set_spim_internal_master_route(SpiMonitorInstance::Spim2, SpiMonitorSource::Spi2); - scu.set_spim_passthrough(SpiMonitorInstance::Spim2, SpiMonitorPassthrough::Enabled); - scu.set_spim_ext_mux(SpiMonitorInstance::Spim2, ScuExtMuxSelect::Mux1); - pw_log::info!("SCU pinmux and SPIM routing configured for SPI2 monitoring"); + // Configure the mux for the SPI master controller path. + scu.set_spim_internal_master_route(SpiMonitorInstance::Spim2, SpiMonitorSource::Spi2); + scu.set_spim_passthrough(SpiMonitorInstance::Spim2, SpiMonitorPassthrough::Enabled); + scu.set_spim_ext_mux(SpiMonitorInstance::Spim2, ScuExtMuxSelect::Mux1); + pw_log::info!("SCU pinmux and SPIM routing configured for SPI2 monitoring"); - for _ in 0..1_000_000 { - core::hint::spin_loop(); - } - Ok(()) + for _ in 0..1_000_000 { + core::hint::spin_loop(); + } + Ok(()) + }) } fn run_spi2_read_test() -> Result<(), SmcError> { diff --git a/target/ast10x0/tests/smc/sfdp/target.rs b/target/ast10x0/tests/smc/sfdp/target.rs index 4a6a6112d..9a0dda060 100644 --- a/target/ast10x0/tests/smc/sfdp/target.rs +++ b/target/ast10x0/tests/smc/sfdp/target.rs @@ -51,8 +51,10 @@ impl SmcInstance for FmcInstance { pub struct Target {} fn run_smc_fmc_cs1_sfdp_test() -> Result<(), SmcError> { - let scu = unsafe { ScuRegisters::new_global_unlocked() }; - scu.apply_pinctrl_group(PINCTRL_FMC_QUAD); + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| { + scu.apply_pinctrl_group(PINCTRL_FMC_QUAD); + }); pw_log::info!("=== AST10x0 SMC FMC CS1 SFDP config test ==="); // Both CS are driven on the EVB; capacity for each is an OUTPUT of init(). diff --git a/target/ast10x0/tests/smc/write/target.rs b/target/ast10x0/tests/smc/write/target.rs index 6578a964e..8b9c0aedb 100644 --- a/target/ast10x0/tests/smc/write/target.rs +++ b/target/ast10x0/tests/smc/write/target.rs @@ -76,8 +76,10 @@ fn restore_sector(flash: &mut SpiNorFlash<'_>, original: &[u8]) -> Result<(), Sm } fn run_smc_fmc_cs1_write_test() -> Result<(), SmcError> { - let scu = unsafe { ScuRegisters::new_global_unlocked() }; - scu.apply_pinctrl_group(PINCTRL_FMC_QUAD); + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| { + scu.apply_pinctrl_group(PINCTRL_FMC_QUAD); + }); pw_log::info!("=== AST10x0 SMC FMC CS1 write test ==="); let mut fmc = unsafe { FmcUninit::::new()? }.init()?; diff --git a/target/ast10x0/tests/spimonitor/setup_all_spim.rs b/target/ast10x0/tests/spimonitor/setup_all_spim.rs index 0ac4b1eae..4ab6809f9 100644 --- a/target/ast10x0/tests/spimonitor/setup_all_spim.rs +++ b/target/ast10x0/tests/spimonitor/setup_all_spim.rs @@ -315,8 +315,10 @@ fn monitor_spim_violations( csin_levels as u32, bmc_csin_low_us as u32 ); - let scu = unsafe { ScuRegisters::new_global_unlocked() }; - recover_bmc_path(&scu, spim1, spim2); + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| { + recover_bmc_path(scu, spim1, spim2); + }); log_indices[0] = spim1.regs().read_log_idx_reg(); log_indices[1] = spim2.regs().read_log_idx_reg(); bmc_csin_low_us = 0; @@ -327,7 +329,7 @@ fn monitor_spim_violations( poll_count = poll_count.wrapping_add(1); if ENABLE_RUNTIME_DEBUG_LOGS && poll_count % 100 == 0 { - let scu = unsafe { ScuRegisters::new_global_unlocked() }; + let scu = unsafe { ScuRegisters::new_global() }; let mux = match spim_external_mux_state(Spim1Config::INSTANCE) { Some(ScuExtMuxSelect::Mux0) => 0u32, Some(ScuExtMuxSelect::Mux1) => 1u32, @@ -451,7 +453,7 @@ fn production_policy() -> SpiMonitorPolicy { } fn setup_all_spim() -> Result<(), test_common::TestError> { - let scu = unsafe { ScuRegisters::new_global_unlocked() }; + let scu = unsafe { ScuRegisters::new_global() }; let policy = production_policy(); //let spim4_policy = spim4_test_policy(); diff --git a/target/ast10x0/tests/spimonitor/test_common.rs b/target/ast10x0/tests/spimonitor/test_common.rs index 928c6b5ec..904021a1a 100644 --- a/target/ast10x0/tests/spimonitor/test_common.rs +++ b/target/ast10x0/tests/spimonitor/test_common.rs @@ -312,8 +312,9 @@ fn test_policy_locking(configured: ConfiguredSpiMonitor) -> Result<(), TestError pub fn run() -> Result<(), TestError> { pw_log::info!("=== AST10x0 external SPI monitor configuration test ==="); - let scu = unsafe { ScuRegisters::new_global_unlocked() }; - configure_wiring::(&scu)?; + let scu = unsafe { ScuRegisters::new_global() }; + + scu.unlock_and_lock_after(|scu| configure_wiring::(scu))?; let configured = initialize_monitor::(log_buffer())?; test_passthrough_control(&configured)?; test_command_policy(&configured)?;