diff --git a/bao1x-boot/boot1/src/main.rs b/bao1x-boot/boot1/src/main.rs index bd8f3311b..71bca99ce 100644 --- a/bao1x-boot/boot1/src/main.rs +++ b/bao1x-boot/boot1/src/main.rs @@ -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(); } diff --git a/bao1x-boot/boot1/src/platform/bao1x/irq.rs b/bao1x-boot/boot1/src/platform/bao1x/irq.rs index cf88ac153..173bf0db0 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/irq.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/irq.rs @@ -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)] diff --git a/bao1x-boot/boot1/src/platform/bao1x/usb/glue.rs b/bao1x-boot/boot1/src/platform/bao1x/usb/glue.rs index 7b9d96ea3..1a9d6c413 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/usb/glue.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/usb/glue.rs @@ -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 {