use crate::Steal;
use crate::qspi::Window;
const XIP_MAINTENANCE_BASE: u32 = 0x1800_0000;
const XIP_ADDRESS_SPACE_SIZE: u32 = 0x0400_0000;
const XIP_CACHE_SIZE: u32 = 16 * 1024;
const XIP_CACHE_LINE_SIZE: u32 = 8;
const OP_INVALIDATE_BY_SET_WAY: u32 = 0;
const OP_CLEAN_BY_SET_WAY: u32 = 1;
pub struct Xip {
xip_ctrl: rp2350_pac::XIP_CTRL,
}
impl Xip {
#[inline]
pub fn init(xip_ctrl: rp2350_pac::XIP_CTRL) -> Self {
Self { xip_ctrl }
}
#[inline]
pub unsafe fn steal() -> Steal<Self> {
Steal(Self {
xip_ctrl: unsafe { rp2350_pac::XIP_CTRL::steal() },
})
}
#[inline]
pub unsafe fn regs(&self) -> &rp2350_pac::xip_ctrl::RegisterBlock {
&self.xip_ctrl
}
#[inline]
pub fn release(self) -> rp2350_pac::XIP_CTRL {
self.xip_ctrl
}
#[inline]
pub fn enable_cache(&self) {
self.xip_ctrl.ctrl.set_en_secure();
self.xip_ctrl.ctrl.set_en_nonsecure();
}
#[inline]
pub fn disable_cache(&self) {
self.xip_ctrl.ctrl.clr_en_secure();
self.xip_ctrl.ctrl.clr_en_nonsecure();
}
#[inline]
pub fn cache_enabled(&self) -> bool {
self.xip_ctrl.ctrl.en_secure()
}
#[inline]
pub fn power_down_cache(&self) {
self.xip_ctrl.ctrl.set_power_down();
}
#[inline]
pub fn cache_powered_down(&self) -> bool {
self.xip_ctrl.ctrl.power_down()
}
#[inline]
pub fn set_writable(&self, window: Window, enable: bool) {
match window {
Window::M0 => {
if enable {
self.xip_ctrl.ctrl.set_writable_m0();
} else {
self.xip_ctrl.ctrl.clr_writable_m0();
}
}
Window::M1 => {
if enable {
self.xip_ctrl.ctrl.set_writable_m1();
} else {
self.xip_ctrl.ctrl.clr_writable_m1();
}
}
}
}
#[inline]
pub fn writable(&self, window: Window) -> bool {
match window {
Window::M0 => self.xip_ctrl.ctrl.writable_m0(),
Window::M1 => self.xip_ctrl.ctrl.writable_m1(),
}
}
#[inline]
pub fn set_split_ways(&self, enable: bool) {
if enable {
self.xip_ctrl.ctrl.set_split_ways();
} else {
self.xip_ctrl.ctrl.clr_split_ways();
}
}
pub fn flush_cache(&self) {
self.maintain(
XIP_ADDRESS_SPACE_SIZE - XIP_CACHE_SIZE,
XIP_CACHE_SIZE,
OP_INVALIDATE_BY_SET_WAY,
);
}
pub fn clean_cache(&self) {
self.maintain(
XIP_ADDRESS_SPACE_SIZE - XIP_CACHE_SIZE,
XIP_CACHE_SIZE,
OP_CLEAN_BY_SET_WAY,
);
}
fn maintain(&self, start: u32, size: u32, op: u32) {
let end = start + size;
let mut offset = start;
while offset < end {
unsafe {
core::ptr::write_volatile((XIP_MAINTENANCE_BASE + offset + op) as *mut u8, 0);
}
offset += XIP_CACHE_LINE_SIZE;
}
unsafe {
core::arch::asm!("dsb sy", "isb sy", options(nomem, nostack, preserves_flags));
}
}
#[inline]
pub fn start_stream(&self, addr: u32, count: u32) {
self.xip_ctrl.stream_addr.write_stream_addr(addr);
self.xip_ctrl.stream_ctr.write_stream_ctr(count);
}
#[inline]
pub fn stop_stream(&self) {
self.xip_ctrl.stream_ctr.write_stream_ctr(0);
}
#[inline]
pub fn stream_fifo_empty(&self) -> bool {
self.xip_ctrl.stat.fifo_empty()
}
#[inline]
pub fn stream_fifo_full(&self) -> bool {
self.xip_ctrl.stat.fifo_full()
}
#[inline]
pub fn read_stream(&self) -> u32 {
self.xip_ctrl.stream_fifo.stream_fifo()
}
#[inline]
pub fn stream_aux_address(&self) -> u32 {
rp2350_pac::XIP_AUX::PTR as u32
}
}