diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index 21d68ca06..1bc007baa 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -225,6 +225,10 @@ impl Repl { #[cfg(feature = "nto-bio")] "bio" => { + use cramium_hal::cache_flush; + + use crate::platform::delay; + const BIO_TESTS: usize = // get_id 1 @@ -276,15 +280,66 @@ impl Repl { } // map the BIO ports to GPIO pins - let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); - iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); - iox.set_gpio_pullup(cramium_api::iox::IoxPort::PB, 2, cramium_api::iox::IoxEnable::Enable); - iox.set_gpio_pullup(cramium_api::iox::IoxPort::PB, 3, cramium_api::iox::IoxEnable::Enable); + // let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); + // iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); + + crate::println!("Resetting block"); + let mut bio_ss = BioSharedState::new(); + bio_ss.init(); + + crate::println!("ldst trial"); + bio_ss.load_code(ldst_code(), 0, BioCore::Core3); + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x888); + for _ in 0..2 { + debug_bio(&bio_ss); + } + crate::println!("ldst trial end"); + + let mut bio_ss = BioSharedState::new(); + // stop all the machines, so that code can be loaded + bio_ss.bio.wo(utra::bio_bdma::SFR_EXTCLOCK, 0); + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); + bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x0); + bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV1, 0x0); + bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV2, 0x0); + bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV3, 0x0); + // bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV1, 0xFFF0_FFFF); + // bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV2, 0xFFF0_FFFF); + // bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV3, 0xFFF0_FFFF); + crate::println!( + "rxflevel0: {}", + bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0) + ); + bio_ss.load_code(simple_test_code(), 0, BioCore::Core3); + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x888); + for i in 0..12 { + debug_bio(&bio_ss); + bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, i); + crate::println!( + "f0:{} f1:{} f2:{} f3:{}", + bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0), + bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1), + bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL2), + bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL3) + ); + crate::println!( + "{:x}, {:x}", + bio_ss.bio.rf(utra::bio_bdma::SFR_RXF1_FDOUT), + bio_ss.bio.rf(utra::bio_bdma::SFR_RXF2_FDOUT), + ); + } + + bio_ss.init(); + passing_tests += bio_tests::arith::stack_test(); + + crate::println!("early abort"); + self.abort_cmd(); + return; passing_tests += bio_tests::units::hello_multiverse(); passing_tests += bio_tests::units::hello_world(); - passing_tests += bio_tests::arith::stack_test(); + bio_ss.init(); // safety: this is safe only if the target supports multiplication passing_tests += unsafe { bio_tests::arith::mac_test() }; // 1 @@ -319,6 +374,102 @@ impl Repl { // Final report crate::println!("\n--- BIO Tests Complete: {}/{} passed. ---\n", passing_tests, BIO_TESTS); } + "usb" => { + crate::println!("USB basic test..."); + let csr = cramium_hal::usb::compat::AtomicCsr::new( + cramium_hal::usb::utra::CORIGINE_USB_BASE as *mut u32, + ); + let irq_csr = cramium_hal::usb::compat::AtomicCsr::new( + utralib::utra::irqarray1::HW_IRQARRAY1_BASE as *mut u32, + ); + crate::println!("inspect USB region..."); + let usbregs = 0x50202400 as *const u32; + for i in 0..32 { + crate::println!("{:x}, {:08x}", i, unsafe { + usbregs + .add(cramium_hal::usb::utra::CORIGINE_DEV_OFFSET / size_of::() + i) + .read_volatile() + }); + } + // safety: this is safe because we are in machine mode, and vaddr/paddr always pairs up + crate::println!("Getting pointer..."); + let mut usb = unsafe { + cramium_hal::usb::driver::CorigineUsb::new( + cramium_hal::board::CRG_UDC_MEMBASE, + csr.clone(), + irq_csr.clone(), + ) + }; + crate::println!("Reset"); + usb.reset(); + let mut idle_timer = 0; + let mut vbus_on = false; + let mut vbus_on_count = 0; + let mut in_u0 = false; + let mut last_sc = 0; + loop { + let next_sc = csr.r(cramium_hal::usb::utra::PORTSC); + if last_sc != next_sc { + last_sc = next_sc; + crate::println!("**** SC update {:x?}", cramium_hal::usb::driver::PortSc(next_sc)); + /* + if cramium_hal::usb::driver::PortSc(next_sc).pr() { + crate::println!(" >>reset<<"); + usb.start(); + in_u0 = false; + vbus_on_count = 0; + } + */ + } + let event = usb.udc_handle_interrupt(); + if event == cramium_hal::usb::driver::CrgEvent::None { + idle_timer += 1; + } else { + // crate::println!("*Event {:?} at {}", event, idle_timer); + idle_timer = 0; + } + + if !vbus_on && vbus_on_count == 4 { + crate::println!("*Vbus on"); + usb.reset(); + usb.init(); + usb.start(); + vbus_on = true; + in_u0 = false; + + let irq1 = irq_csr.r(utralib::utra::irqarray1::EV_PENDING); + crate::println!( + "irq1: {:x}, status: {:x}", + irq1, + csr.r(cramium_hal::usb::utra::USBSTS) + ); + irq_csr.wo(utralib::utra::irqarray1::EV_PENDING, irq1); + // restore this to go on to boot + // break; + } else if usb.pp() && !vbus_on { + vbus_on_count += 1; + crate::println!("*Vbus_on_count: {}", vbus_on_count); + // mdelay(100); + } else if !usb.pp() && vbus_on { + crate::println!("*Vbus off"); + usb.stop(); + usb.reset(); + vbus_on_count = 0; + vbus_on = false; + in_u0 = false; + } else if in_u0 && vbus_on { + // usb.udc_handle_interrupt(); + // TODO + } else if usb.ccs() && vbus_on { + // usb.print_status(usb.csr.r(cramium_hal::usb::utra::PORTSC)); + crate::println!("*Enter U0"); + in_u0 = true; + let irq1 = irq_csr.r(utralib::utra::irqarray1::EV_PENDING); + // usb.print_status(csr.r(cramium_hal::usb::utra::PORTSC)); + irq_csr.wo(utralib::utra::irqarray1::EV_PENDING, irq1); + } + } + } "echo" => { for word in args { crate::print!("{} ", word); @@ -342,4 +493,41 @@ impl Repl { self.do_cmd = false; self.cmdline.clear(); } + + fn abort_cmd(&mut self) { + self.do_cmd = false; + self.cmdline.clear(); + } +} + +#[rustfmt::skip] +bio_code!(simple_test_code, SIMPLE_TEST_START, SIMPLE_TEST_END, + "li sp, 0x800", + "10:", + "mv t0, x31", + "mv t1, x16", + "sw t1, 0(sp)", + "lw t2, 0(sp)", + "addi t2, t2, 0x200", + "mv x17, t2", + "mv x18, t0", + // "mv x20, x0", + "j 10b" +); + +#[rustfmt::skip] +bio_code!(ldst_code, LDST_START, LDST_END, + "sw x0, 0x20(x0)", + "10:", + "j 10b" +); + +fn debug_bio(bio_ss: &BioSharedState) { + crate::println!( + "c0:{:04x} c1:{:04x} c2:{:04x} c3:{:04x}", + bio_ss.bio.r(utra::bio_bdma::SFR_DBG0), + bio_ss.bio.r(utra::bio_bdma::SFR_DBG1), + bio_ss.bio.r(utra::bio_bdma::SFR_DBG2), + bio_ss.bio.r(utra::bio_bdma::SFR_DBG3), + ); } diff --git a/libs/xous-bio-bdma/src/lib.rs b/libs/xous-bio-bdma/src/lib.rs index 680990453..8040e6ad5 100644 --- a/libs/xous-bio-bdma/src/lib.rs +++ b/libs/xous-bio-bdma/src/lib.rs @@ -165,7 +165,85 @@ impl BioSharedState { } } + pub fn init(&mut self) { + self.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); + for imem in self.imem_slice.iter_mut() { + // jump to current location + imem.fill(0xA001_A001); + } + for (i, imem) in self.imem_slice.iter().enumerate() { + for (j, &d) in imem.iter().enumerate() { + if d != 0xA001_A001 { + crate::println!("imem{}[{:x}]: {:x}", i, j, d); + } + } + } + + // This may not scale to e.g. shared memory model but trying to see its impact + let fifo_slice = unsafe { + [ + core::slice::from_raw_parts_mut( + utralib::generated::HW_BIO_FIFO0_MEM as *mut u32, + HW_BIO_FIFO0_MEM_LEN / size_of::(), + ), + core::slice::from_raw_parts_mut( + utralib::generated::HW_BIO_FIFO1_MEM as *mut u32, + HW_BIO_FIFO1_MEM_LEN / size_of::(), + ), + core::slice::from_raw_parts_mut( + utralib::generated::HW_BIO_FIFO2_MEM as *mut u32, + HW_BIO_FIFO2_MEM_LEN / size_of::(), + ), + core::slice::from_raw_parts_mut( + utralib::generated::HW_BIO_FIFO3_MEM as *mut u32, + HW_BIO_FIFO3_MEM_LEN / size_of::(), + ), + ] + }; + for (i, fifo) in fifo_slice.iter().enumerate() { + crate::println!("fifo{}: {:x}", i, fifo[0]); + } + + // set clocking mode to 3 + self.bio.wfo(utra::bio_bdma::SFR_CONFIG_CLOCKING_MODE, 3); + self.bio.wo(utra::bio_bdma::SFR_EXTCLOCK, 0); + self.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x0_0000); + self.bio.wo(utra::bio_bdma::SFR_QDIV1, 0x0_0000); + self.bio.wo(utra::bio_bdma::SFR_QDIV2, 0x0_0000); + self.bio.wo(utra::bio_bdma::SFR_QDIV3, 0x0_0000); + self.bio.wo(utra::bio_bdma::SFR_CTRL, 0xFFF); + self.bio.wfo(utra::bio_bdma::SFR_FIFO_CLR_SFR_FIFO_CLR, 0xf); + for _ in 0..8 { + let _ = self.bio.r(utra::bio_bdma::SFR_RXF0); + let _ = self.bio.r(utra::bio_bdma::SFR_RXF1); + let _ = self.bio.r(utra::bio_bdma::SFR_RXF2); + let _ = self.bio.r(utra::bio_bdma::SFR_RXF3); + } + self.bio.wfo(utra::bio_bdma::SFR_FIFO_CLR_SFR_FIFO_CLR, 0xf); + self.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); + crate::println!( + "{:04x} {:04x} {:04x} {:04x}", + self.bio.r(utra::bio_bdma::SFR_DBG0), + self.bio.r(utra::bio_bdma::SFR_DBG1), + self.bio.r(utra::bio_bdma::SFR_DBG2), + self.bio.r(utra::bio_bdma::SFR_DBG3), + ); + + /* + crate::println!("ldst trial"); + self.bio.wo(utra::bio_bdma::SFR_CTRL, 0x000); + self.load_code(ldst_code(), 0, BioCore::Core0); + self.load_code(ldst_code(), 0, BioCore::Core1); + self.load_code(ldst_code(), 0, BioCore::Core2); + self.load_code(ldst_code(), 0, BioCore::Core3); + self.bio.wo(utra::bio_bdma::SFR_CTRL, 0xFFF); + crate::println!("ldst trial end"); + self.bio.wo(utra::bio_bdma::SFR_CTRL, 0x000); + */ + } + pub fn load_code(&mut self, prog: &[u8], offset_bytes: usize, core: BioCore) { + crate::println!("load code from {:x}", prog.as_ptr() as usize); let offset = offset_bytes / core::mem::size_of::(); for (i, chunk) in prog.chunks(4).enumerate() { if chunk.len() == 4 { @@ -180,6 +258,13 @@ impl BioSharedState { self.imem_slice[core as usize][i + offset] = ragged_word; } } + match self.verify_code(&prog, offset_bytes, core) { + Err(BioError::CodeCheck(offset)) => { + crate::println!("Code verification error at {:x}", offset) + } + _ => (), + } + cache_flush(); } pub fn verify_code(&mut self, prog: &[u8], offset_bytes: usize, core: BioCore) -> Result<(), BioError> { @@ -249,3 +334,19 @@ macro_rules! bio_code { ); }; } + +#[inline(always)] +pub(crate) fn cache_flush() { + unsafe { + // cache flush + #[rustfmt::skip] + core::arch::asm!( + "fence.i", + ".word 0x500F", + "nop", + "nop", + "nop", + "nop", + ); + } +}