add block erase routine

This commit is contained in:
bunnie 2025-05-21 10:20:23 +08:00
parent 19455d1bc0
commit 1810825744
4 changed files with 58 additions and 12 deletions

View file

@ -30,16 +30,11 @@ utralib = { version = "0.1.25", optional = true, default-features = false }
[target.'cfg(any(windows,unix))'.dependencies]
[features]
cramium-soc = [
"utralib/cramium-soc",
"cramium-hal/std",
"loader/cramium-soc",
"cramium-api/std",
]
cramium-soc = ["utralib/cramium-soc", "loader/cramium-soc", "cramium-api/std"]
cramium-fpga = ["utralib/cramium-fpga"]
board-baosec = ["cramium-hal/board-baosec"]
board-baosor = ["cramium-hal/board-baosor"]
board-dabao = ["cramium-hal/board-dabao"]
board-baosec = ["cramium-hal/board-baosec", "cramium-soc"]
board-baosor = ["cramium-hal/board-baosor", "cramium-soc"]
board-dabao = ["cramium-hal/board-dabao", "cramium-soc"]
precursor = ["utralib/precursor", "precursor-hal"]
hosted = ["utralib/hosted"]

View file

@ -21,6 +21,7 @@ pub enum SwapAbi {
StealPage = 5,
ReleaseMemory = 6,
WritePage = 7,
BlockErase = 8,
}
/// SYNC WITH `kernel/src/swap.rs`
impl SwapAbi {
@ -34,6 +35,7 @@ impl SwapAbi {
5 => StealPage,
6 => ReleaseMemory,
7 => WritePage,
8 => BlockErase,
_ => Invalid,
}
}
@ -51,6 +53,7 @@ pub enum Opcode {
/// anyways because the page was read in and mapped into the caller's space at some
/// point in time.
WritePage,
BulkErase,
/// Test messages
#[cfg(feature = "swap-userspace-testing")]
Test0,
@ -98,7 +101,7 @@ impl Swapper {
/// `offset` is transmitted with the address bank mask for MMAP_VIRT attached to avoid
/// the first sector falling foul of the NonZero requirement.
pub fn write_page(&self, offset: usize, page: FlashPage) -> Result<xous::Result, xous::Error> {
pub fn write_page(&self, offset: usize, page: &FlashPage) -> Result<xous::Result, xous::Error> {
if (offset & (cramium_hal::board::SPINOR_ERASE_SIZE as usize - 1)) != 0 {
return Err(xous::Error::BadAddress);
}
@ -116,6 +119,19 @@ impl Swapper {
// but because we care that the call finished before proceeding.
xous::send_message(self.conn, xous::Message::Borrow(msg))
}
pub fn block_erase(&self, offset: usize, len: usize) -> Result<xous::Result, xous::Error> {
if (offset & (cramium_hal::board::SPINOR_BULK_ERASE_SIZE as usize - 1) != 0)
|| len == 0
|| (len & (cramium_hal::board::SPINOR_BULK_ERASE_SIZE as usize - 1)) != 0
{
return Err(xous::Error::BadAddress);
}
xous::send_message(
self.conn,
xous::Message::new_blocking_scalar(Opcode::BulkErase as usize, offset, len, 0, 0),
)
}
}
use core::sync::atomic::{AtomicU32, Ordering};

View file

@ -91,6 +91,8 @@ pub enum KernelOp {
HardOom = 3,
/// Take the requested page and write it to SPI
WriteToFlash = 4,
/// Bulk erase
BulkErase = 5,
}
pub struct PtPage {
@ -637,7 +639,7 @@ fn swap_handler(
writeln!(
DebugUart {},
"Exiting HARD OOM swap free loop: freed {} pages; {} requests rejected, {} wired",
target_pages - pages_to_free - HARD_OOM_RESERVED_PAGES,
target_pages - pages_to_free,
errs,
wired
)
@ -671,6 +673,15 @@ fn swap_handler(
ss.hal.flash_write(buf, offset);
}
}
Some(KernelOp::BulkErase) => {
let offset = a2;
let len = a3;
if !RENODE_TESTING {
#[cfg(feature = "debug-print-swapper")]
writeln!(DebugUart {}, "BE: PA {:x} len {:x}", offset, len).ok();
ss.hal.block_erase(offset, len);
}
}
_ => {
writeln!(DebugUart {}, "Unimplemented or unknown opcode: {}", opcode).ok();
}
@ -793,6 +804,22 @@ fn main() {
))
.expect("couldn't WritePage");
}
Some(Opcode::BulkErase) => {
if let Some(scalar) = msg.body.scalar_message_mut() {
let block = scalar.arg1;
let len = scalar.arg2;
xous::rsyscall(xous::SysCall::SwapOp(
SwapAbi::BlockErase as usize,
msg.sender.pid().unwrap().get() as usize,
block,
len,
0,
0,
0,
))
.expect("couldn't Block Erase region");
}
}
#[cfg(feature = "swap-userspace-testing")]
Some(Opcode::Test0) => {
log::info!("Free mem: {}kiB", get_free_pages() * PAGE_SIZE / 1024);

View file

@ -222,7 +222,11 @@ impl SwapHal {
.chunks_exact(FLASH_PAGE_LEN)
.zip((preserve_start..(preserve_start + FLASH_SECTOR_LEN)).step_by(FLASH_PAGE_LEN))
{
self.flash_spim.borrow_mut().mem_flash_write_page(addr as u32, page.try_into().unwrap());
// only do the write-page if something is not in the erased state
// this allows us to re-use flash_write as a "slow erase" for things smaller than a block
if !page.iter().all(|&b| b == 0xff) {
self.flash_spim.borrow_mut().mem_flash_write_page(addr as u32, page.try_into().unwrap());
}
}
written += replace_end - preserve_to;
}
@ -272,4 +276,8 @@ impl SwapHal {
}
}
}
pub fn block_erase(&mut self, block: usize, len: usize) -> bool {
self.flash_spim.borrow_mut().flash_erase_block(block, len)
}
}