mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
add baremetal RRAM API
this works in baremetal environment. Needs to be modified/augmented for xous...
This commit is contained in:
parent
da623ccd01
commit
4fbae4969b
1 changed files with 139 additions and 0 deletions
139
libs/cramium-hal/src/rram.rs
Normal file
139
libs/cramium-hal/src/rram.rs
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
use utralib::{CSR, utra};
|
||||
|
||||
pub struct Reram {
|
||||
csr: CSR<u32>,
|
||||
array: &'static mut [u32],
|
||||
}
|
||||
|
||||
/// Matches the alignment requirement of the RRC write buffer
|
||||
const ALIGNMENT: usize = 32;
|
||||
#[repr(align(4))]
|
||||
struct AlignedBuffer([u8; ALIGNMENT]);
|
||||
impl AlignedBuffer {
|
||||
pub fn as_slice_u32(&self) -> &[u32] {
|
||||
// safety: this is safe because the #repr(align) ensures that our alignment is correct,
|
||||
// and the length of the internal data structure is set correctly by design. Furthermore,
|
||||
// all values in both the source and destination transmutation are representable and valid.
|
||||
unsafe { core::slice::from_raw_parts(self.0.as_ptr() as *const u32, self.0.len() / 4) }
|
||||
}
|
||||
}
|
||||
|
||||
/// This is the code that enables the security modes. Must be written into
|
||||
/// RRCR on every update - kind of dangerous design, because it is too easy
|
||||
/// to overlook setting this in a compound register.
|
||||
const SECURITY_MODE: u32 = 0b1111_1100_0000_0000;
|
||||
const RRC_LOAD_BUFFER: u32 = 0x5200;
|
||||
const RRC_WRITE_BUFFER: u32 = 0x9528;
|
||||
const RRC_CR_NORMAL: u32 = 0;
|
||||
#[allow(dead_code)]
|
||||
const RRC_CR_POWERDOWN: u32 = 1;
|
||||
#[allow(dead_code)]
|
||||
const RRC_CR_WRITE_DATA: u32 = 0;
|
||||
const RRC_CR_WRITE_CMD: u32 = 2;
|
||||
|
||||
impl Reram {
|
||||
pub fn new() -> Self {
|
||||
Reram {
|
||||
csr: CSR::new(utra::rrc::HW_RRC_BASE as *mut u32),
|
||||
array: unsafe {
|
||||
core::slice::from_raw_parts_mut(
|
||||
utralib::HW_RERAM_MEM as *mut u32,
|
||||
utralib::HW_RERAM_MEM_LEN / core::mem::size_of::<u32>(),
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_slice(&self) -> &[u32] { self.array }
|
||||
|
||||
/// This is a crappy "unsafe" initial version that requires the write
|
||||
/// destination address to be aligned to a 256-bit boundary, and the data
|
||||
/// to be exactly 256 bits long.
|
||||
pub unsafe fn write_u32_aligned(&mut self, addr: usize, data: &[u32]) {
|
||||
assert!(addr % 0x20 == 0, "unaligned destination address!");
|
||||
assert!(data.len() % 8 == 0, "unaligned source data!");
|
||||
// crate::print!("@ {:x} > ", addr);
|
||||
for (outer, d) in data.chunks_exact(8).enumerate() {
|
||||
// write the data to the buffer
|
||||
for (inner, &datum) in d.iter().enumerate() {
|
||||
// crate::print!(" {:x}", datum);
|
||||
self.array
|
||||
.as_mut_ptr()
|
||||
.add(addr / core::mem::size_of::<u32>() + outer * 8 + inner)
|
||||
.write_volatile(datum);
|
||||
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
|
||||
}
|
||||
// crate::println!("");
|
||||
|
||||
self.csr.rmwf(utra::rrc::SFR_RRCCR_SFR_RRCCR, RRC_CR_WRITE_CMD | SECURITY_MODE);
|
||||
self.array
|
||||
.as_mut_ptr()
|
||||
.add(addr / core::mem::size_of::<u32>() + outer * 8)
|
||||
.write_volatile(RRC_LOAD_BUFFER);
|
||||
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
|
||||
self.array
|
||||
.as_mut_ptr()
|
||||
.add(addr / core::mem::size_of::<u32>() + outer * 8)
|
||||
.write_volatile(RRC_WRITE_BUFFER);
|
||||
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
|
||||
self.csr.rmwf(utra::rrc::SFR_RRCCR_SFR_RRCCR, RRC_CR_NORMAL | SECURITY_MODE);
|
||||
}
|
||||
crate::cache_flush();
|
||||
}
|
||||
|
||||
/// This is a general unaligned write primitive for the RRAM that can handle any length
|
||||
/// slice and alignment of data.
|
||||
pub fn write_slice(&mut self, offset: usize, data: &[u8]) {
|
||||
let mut buffer = AlignedBuffer([0u8; ALIGNMENT]);
|
||||
|
||||
// ragged start
|
||||
let start_len = ALIGNMENT - (offset % ALIGNMENT);
|
||||
if start_len != 0 {
|
||||
let start_offset = offset & !(ALIGNMENT - 1);
|
||||
let dest_slice = unsafe {
|
||||
core::slice::from_raw_parts(
|
||||
(start_offset + utralib::HW_RERAM_MEM) as *const u8,
|
||||
buffer.0.len(),
|
||||
)
|
||||
};
|
||||
// populate from old data first
|
||||
buffer.0.copy_from_slice(&dest_slice);
|
||||
buffer.0[offset % ALIGNMENT..].copy_from_slice(&data[..start_len]);
|
||||
// safe because alignment and buffer sizes are guaranteed
|
||||
unsafe {
|
||||
self.write_u32_aligned(start_offset, buffer.as_slice_u32());
|
||||
}
|
||||
}
|
||||
|
||||
// aligned middle & end
|
||||
let mut cur_offset = offset + start_len;
|
||||
if data.len() - start_len > 0 {
|
||||
for chunk in data[start_len..].chunks(buffer.0.len()) {
|
||||
// full chunk
|
||||
if chunk.len() == buffer.0.len() {
|
||||
buffer.0.copy_from_slice(&chunk);
|
||||
// safe because alignment and buffer sizes are guaranteed
|
||||
unsafe {
|
||||
self.write_u32_aligned(cur_offset, &buffer.as_slice_u32());
|
||||
}
|
||||
} else {
|
||||
let dest_slice = unsafe {
|
||||
core::slice::from_raw_parts(
|
||||
(cur_offset + utralib::HW_RERAM_MEM) as *const u8,
|
||||
buffer.0.len(),
|
||||
)
|
||||
};
|
||||
// read in the destination full contents
|
||||
buffer.0.copy_from_slice(&dest_slice);
|
||||
// now overwrite the "ragged end"
|
||||
buffer.0[..chunk.len()].copy_from_slice(&chunk);
|
||||
// safe because alignment and buffer sizes are guaranteed
|
||||
unsafe {
|
||||
self.write_u32_aligned(cur_offset, &buffer.as_slice_u32());
|
||||
}
|
||||
}
|
||||
cur_offset += chunk.len();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue