Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions target/ast10x0/board/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
Expand Down
42 changes: 20 additions & 22 deletions target/ast10x0/board/src/spim_wiring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ impl From<SpiMonitorError> 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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions target/ast10x0/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
17 changes: 10 additions & 7 deletions target/ast10x0/peripherals/hace/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@ impl<Y: FnMut(u32)> HaceDevice<Y> {
// 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() };
Expand Down
65 changes: 35 additions & 30 deletions target/ast10x0/peripherals/i3c/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use core::marker::PhantomData;

use super::constants::MAX_BUSES;
use crate::scu::ScuRegisters;

// -----------------------------------------------------------------------------
// Per-bus register dispatch (private)
Expand Down Expand Up @@ -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<R>(&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.
Expand Down
18 changes: 10 additions & 8 deletions target/ast10x0/peripherals/scu/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
}
}

Expand Down
22 changes: 12 additions & 10 deletions target/ast10x0/peripherals/scu/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 22 additions & 20 deletions target/ast10x0/peripherals/scu/pinctrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
});
}
}
Loading
Loading