add cleanup for USB stack

have to shut it down gracefully or the next stage won't run.
This commit is contained in:
bunnie 2025-10-01 23:32:21 +08:00
parent b86664d279
commit 444ae53a11
3 changed files with 34 additions and 0 deletions

View file

@ -245,6 +245,10 @@ pub unsafe extern "C" fn rust_entry() -> ! {
marquee(sh1107, "Booting...");
}
// stop the USB subsystem so it can be re-init'd by the next stage.
// without this, USB init will hang later on.
glue::shutdown();
// when we get to this point, there's only two options...
crate::secboot::boot_or_die();
}

View file

@ -34,6 +34,26 @@ pub fn enable_irq(irq_no: usize) {
unsafe { mie::set_mext() };
}
pub fn disable_all_irqs() {
// Note that the vexriscv "IRQ Mask" register is inverse-logic --
// that is, setting a bit in the "mask" register unmasks (i.e. enables) it.
mim::write(0);
unsafe { mie::clear_mext() };
unsafe { mstatus::clear_mie() };
// redo delegations
unsafe {
#[rustfmt::skip]
core::arch::asm!(
"li t0, 0xffffffff",
"csrw mideleg, t0",
"csrw medeleg, t0",
// Re-install the machine mode trap handler
"la t0, abort",
"csrw mtvec, t0",
);
}
}
#[export_name = "_start_trap"]
// #[repr(align(4))] // can't do this yet.
#[inline(never)]

View file

@ -51,6 +51,16 @@ pub fn setup() -> (UsbDeviceState, u32) {
}
}
pub fn shutdown() {
unsafe {
if let Some(ref mut usb_ref) = crate::platform::bao1x::usb::USB {
let usb = &mut *core::ptr::addr_of_mut!(*usb_ref);
crate::irq::disable_all_irqs();
usb.stop();
}
}
}
pub fn usb_status() -> (UsbDeviceState, u32) {
unsafe {
if let Some(ref mut usb_ref) = crate::platform::bao1x::usb::USB {