From 3c5240401f7748dfacde602289f9d6736256af21 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 10 Aug 2025 02:03:10 +0800 Subject: [PATCH] fix loader for NTO --- loader/src/platform/cramium/cramium.rs | 638 ++++++++++++++----------- 1 file changed, 363 insertions(+), 275 deletions(-) diff --git a/loader/src/platform/cramium/cramium.rs b/loader/src/platform/cramium/cramium.rs index 09231c229..64359951d 100644 --- a/loader/src/platform/cramium/cramium.rs +++ b/loader/src/platform/cramium/cramium.rs @@ -10,6 +10,7 @@ use cramium_hal::udma; #[cfg(any(feature = "board-baosec", feature = "board-baosor"))] use cramium_hal::{axp2101::Axp2101, udma::GlobalConfig}; use utralib::generated::*; +use utralib::utra::sysctrl; #[cfg(feature = "qr")] use crate::platform::cramium::{homography, qr}; @@ -63,6 +64,8 @@ static ALLOCATOR: linked_list_allocator::LockedHeap = linked_list_allocator::Loc break into the loader updater less able to get at any root keys? */ +pub const SYSTEM_CLOCK_FREQUENCY: u32 = 800_000_000; + pub const RAM_SIZE: usize = utralib::generated::HW_SRAM_MEM_LEN; pub const RAM_BASE: usize = utralib::generated::HW_SRAM_MEM; pub const FLASH_BASE: usize = utralib::generated::HW_RERAM_MEM; @@ -90,56 +93,133 @@ pub fn delay(quantum: usize) { #[cfg(all(feature = "cramium-soc", not(feature = "verilator-only")))] pub fn early_init() -> u32 { - // TODO: pull in the EMA settings from nto-tests - // *((volatile uint32_t*)0x40014000) = 0x3; //sramcfg.cach:ema[2:0]=0x4 (default for 0.8V), 0x3 for 0.9V - // *((volatile uint32_t*)0x40014014) = 0x1; //sramcfg.vexram:ema[2:0]=0x4 (default for 0.8V), 0x1 for 0.9V + let daric_cgu = sysctrl::HW_SYSCTRL_BASE as *mut u32; - // Set up the initial clocks. This is done as a "poke array" into a table of addresses. - // Why? because this is actually how it's done for the chip verification code. We can - // make this nicer and more abstract with register meanings down the road, if necessary, - // but for now this actually makes it easier to maintain, because we can visually compare the - // register settings directly againt what the designers are using in validation. - // - // Not all design changes have a rhyme or reason at this stage -- sometimes "it just works, - // don't futz with it" is actually the answer that goes to production. - use utralib::utra::sysctrl; + let ao_sysctrl = utra::ao_sysctrl::HW_AO_SYSCTRL_BASE as *mut u32; + unsafe { + // this turns off the VDD85D (doesn't work) + // TODO: debug switching regulator power-on + // ao_sysctrl.add(utra::ao_sysctrl::SFR_PMUCSR.offset()).write_volatile(0x6c); + + // this sets VDD85D to 0.90V + ao_sysctrl.add(utra::ao_sysctrl::SFR_PMUTRM0CSR.offset()).write_volatile(0x0842_1FF1); // 0x0842_1080 default + daric_cgu.add(utra::sysctrl::SFR_IPCARIPFLOW.offset()).write_volatile(0x57); + } + + let uart = crate::debug::Uart {}; + + for _ in 0..100 { + uart.putc('a' as u32 as u8); + } unsafe { - // this is MANDATORY for any chip stability in real silicon, as the initial - // clocks are too unstable to do anything otherwise. However, for the simulation - // environment, this can (should?) be dropped - let daric_cgu = sysctrl::HW_SYSCTRL_BASE as *mut u32; - daric_cgu.add(sysctrl::SFR_CGUSEL1.offset()).write_volatile(1); // 0: RC, 1: XTAL - daric_cgu.add(sysctrl::SFR_CGUFSCR.offset()).write_volatile(48); // external crystal is 48MHz - - daric_cgu.add(sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); - + // this block is mandatory in all cases to get clocks set into some consistent, expected mode + { + // conservative dividers + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0.offset()).write_volatile(0x7f7f); + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1.offset()).write_volatile(0x7f7f); + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2.offset()).write_volatile(0x3f7f); + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_3.offset()).write_volatile(0x1f3f); + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_4.offset()).write_volatile(0x0f1f); + // ungate all clocks + daric_cgu.add(utra::sysctrl::SFR_ACLKGR.offset()).write_volatile(0xFF); + daric_cgu.add(utra::sysctrl::SFR_HCLKGR.offset()).write_volatile(0xFF); + daric_cgu.add(utra::sysctrl::SFR_ICLKGR.offset()).write_volatile(0xFF); + daric_cgu.add(utra::sysctrl::SFR_PCLKGR.offset()).write_volatile(0xFF); + // commit clocks + daric_cgu.add(utra::sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); + } + // enable DUART let duart = utra::duart::HW_DUART_BASE as *mut u32; duart.add(utra::duart::SFR_CR.offset()).write_volatile(0); - duart.add(utra::duart::SFR_ETUC.offset()).write_volatile(24); + // based on ringosc trimming as measured by oscope. this will get precise after we set the PLL. + duart.add(utra::duart::SFR_ETUC.offset()).write_volatile(34); duart.add(utra::duart::SFR_CR.offset()).write_volatile(1); } - // this block is mandatory in all cases to get clocks set into some consistent, expected mode - unsafe { - let daric_cgu = sysctrl::HW_SYSCTRL_BASE as *mut u32; - // conservative dividers - daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0.offset()).write_volatile(0x7f7f); - daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1.offset()).write_volatile(0x7f7f); - daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2.offset()).write_volatile(0x3f7f); - daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_3.offset()).write_volatile(0x1f3f); - daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_4.offset()).write_volatile(0x0f1f); - // ungate all clocks - daric_cgu.add(utra::sysctrl::SFR_ACLKGR.offset()).write_volatile(0xFF); - daric_cgu.add(utra::sysctrl::SFR_HCLKGR.offset()).write_volatile(0xFF); - daric_cgu.add(utra::sysctrl::SFR_ICLKGR.offset()).write_volatile(0xFF); - daric_cgu.add(utra::sysctrl::SFR_PCLKGR.offset()).write_volatile(0xFF); - // commit clocks - daric_cgu.add(utra::sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); + // sram0 requires 1 wait state for writes + let mut sramtrm = CSR::new(utra::coresub_sramtrm::HW_CORESUB_SRAMTRM_BASE as *mut u32); + sramtrm.wo(utra::coresub_sramtrm::SFR_SRAM0, 0x8); + sramtrm.wo(utra::coresub_sramtrm::SFR_SRAM1, 0x8); - let duart = utra::duart::HW_DUART_BASE as *mut u32; - // enable DUART - duart.add(utra::duart::SFR_CR.offset()).write_volatile(1); + // #[cfg(feature = "v0p9")] // always targeting 0.9v timings + { + /* + logic [15:0] trm_ram32kx72 ; assign trm_ram32kx72 = trmdat[0 ]; localparam t_trm IV_trm_ram32kx72 = IV_sram_sp_uhde_inst_sram0; + logic [15:0] trm_ram8kx72 ; assign trm_ram8kx72 = trmdat[1 ]; localparam t_trm IV_trm_ram8kx72 = IV_sram_sp_hde_inst_sram1; + logic [15:0] trm_rf1kx72 ; assign trm_rf1kx72 = trmdat[2 ]; localparam t_trm IV_trm_rf1kx72 = IV_rf_sp_hde_inst_cache; + logic [15:0] trm_rf256x27 ; assign trm_rf256x27 = trmdat[3 ]; localparam t_trm IV_trm_rf256x27 = IV_rf_sp_hde_inst_cache; + logic [15:0] trm_rf512x39 ; assign trm_rf512x39 = trmdat[4 ]; localparam t_trm IV_trm_rf512x39 = IV_rf_sp_hde_inst_cache; + logic [15:0] trm_rf128x31 ; assign trm_rf128x31 = trmdat[5 ]; localparam t_trm IV_trm_rf128x31 = IV_rf_sp_hde_inst_cache; + logic [15:0] trm_dtcm8kx36 ; assign trm_dtcm8kx36 = trmdat[6 ]; localparam t_trm IV_trm_dtcm8kx36 = IV_sram_sp_hde_inst_tcm; + logic [15:0] trm_itcm32kx18 ; assign trm_itcm32kx18 = trmdat[7 ]; localparam t_trm IV_trm_itcm32kx18 = IV_sram_sp_hde_inst_tcm; + logic [15:0] trm_ifram32kx36 ; assign trm_ifram32kx36 = trmdat[8 ]; localparam t_trm IV_trm_ifram32kx36 = IV_sram_sp_uhde_inst; + logic [15:0] trm_sce_sceram_10k ; assign trm_sce_sceram_10k = trmdat[9 ]; localparam t_trm IV_trm_sce_sceram_10k = IV_sram_sp_hde_inst; + logic [15:0] trm_sce_hashram_3k ; assign trm_sce_hashram_3k = trmdat[10]; localparam t_trm IV_trm_sce_hashram_3k = IV_rf_sp_hde_inst; + logic [15:0] trm_sce_aesram_1k ; assign trm_sce_aesram_1k = trmdat[11]; localparam t_trm IV_trm_sce_aesram_1k = IV_rf_sp_hde_inst; + logic [15:0] trm_sce_pkeram_4k ; assign trm_sce_pkeram_4k = trmdat[12]; localparam t_trm IV_trm_sce_pkeram_4k = IV_rf_sp_hde_inst; + logic [15:0] trm_sce_aluram_3k ; assign trm_sce_aluram_3k = trmdat[13]; localparam t_trm IV_trm_sce_aluram_3k = IV_rf_sp_hde_inst; + logic [15:0] trm_sce_mimmdpram ; assign trm_sce_mimmdpram = trmdat[14]; localparam t_trm IV_trm_sce_mimmdpram = IV_rf_2p_hdc_inst; + logic [15:0] trm_rdram1kx32 ; assign trm_rdram1kx32 = trmdat[15]; localparam t_trm IV_trm_rdram1kx32 = IV_rf_2p_hdc_inst_vex; + logic [15:0] trm_rdram512x64 ; assign trm_rdram512x64 = trmdat[16]; localparam t_trm IV_trm_rdram512x64 = IV_rf_2p_hdc_inst_vex; + logic [15:0] trm_rdram128x22 ; assign trm_rdram128x22 = trmdat[17]; localparam t_trm IV_trm_rdram128x22 = IV_rf_2p_hdc_inst_vex; + logic [15:0] trm_rdram32x16 ; assign trm_rdram32x16 = trmdat[18]; localparam t_trm IV_trm_rdram32x16 = IV_rf_2p_hdc_inst_vex; + logic [15:0] trm_bioram1kx32 ; assign trm_bioram1kx32 = trmdat[19]; localparam t_trm IV_trm_bioram1kx32 = IV_rf_sp_hde_inst_cache; + logic [15:0] trm_tx_fifo128x32 ; assign trm_tx_fifo128x32 = trmdat[20]; localparam t_trm IV_trm_tx_fifo128x32 = IV_rf_2p_hdc_inst; + logic [15:0] trm_rx_fifo128x32 ; assign trm_rx_fifo128x32 = trmdat[21]; localparam t_trm IV_trm_rx_fifo128x32 = IV_rf_2p_hdc_inst; + logic [15:0] trm_fifo32x19 ; assign trm_fifo32x19 = trmdat[22]; localparam t_trm IV_trm_fifo32x19 = IV_rf_2p_hdc_inst; + logic [15:0] trm_udcmem_share ; assign trm_udcmem_share = trmdat[23]; localparam t_trm IV_trm_udcmem_share = IV_rf_2p_hdc_inst; + logic [15:0] trm_udcmem_odb ; assign trm_udcmem_odb = trmdat[24]; localparam t_trm IV_trm_udcmem_odb = IV_rf_2p_hdc_inst; + logic [15:0] trm_udcmem_256x64 ; assign trm_udcmem_256x64 = trmdat[25]; localparam t_trm IV_trm_udcmem_256x64 = IV_rf_2p_hdc_inst; + logic [15:0] trm_acram2kx64 ; assign trm_acram2kx64 = trmdat[26]; localparam t_trm IV_trm_acram2kx64 = IV_sram_sp_uhde_inst_sram0; + logic [15:0] trm_aoram1kx36 ; assign trm_aoram1kx36 = trmdat[27]; localparam t_trm IV_trm_aoram1kx36 = IV_sram_sp_hde_inst; + + */ + crate::println!("setting 0.9v sramtrm"); + let mut sramtrm = CSR::new(utra::coresub_sramtrm::HW_CORESUB_SRAMTRM_BASE as *mut u32); + sramtrm.wo(utra::coresub_sramtrm::SFR_CACHE, 0x3); + sramtrm.wo(utra::coresub_sramtrm::SFR_ITCM, 0x3); + sramtrm.wo(utra::coresub_sramtrm::SFR_DTCM, 0x3); + sramtrm.wo(utra::coresub_sramtrm::SFR_VEXRAM, 0x1); + + let mut rbist = CSR::new(utra::rbist_wrp::HW_RBIST_WRP_BASE as *mut u32); + // bio 0.9v settings + rbist.wo(utra::rbist_wrp::SFRCR_TRM, (19 << 16) | 0b011_000_01_0_0_000_0_00); + rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a); + + // vex 0.9v settings + for i in 0..4 { + rbist.wo(utra::rbist_wrp::SFRCR_TRM, ((15 + i) << 16) | 0b001_010_00_0_0_000_0_00); + rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a); + } + + // sram 0.9v settings + rbist.wo(utra::rbist_wrp::SFRCR_TRM, (0 << 16) | 0b011_000_01_0_1_000_0_00); + rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a); + rbist.wo(utra::rbist_wrp::SFRCR_TRM, (1 << 16) | 0b011_000_00_0_0_000_0_00); + rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a); + crate::println!("setting other 0.9v trims"); + + // tcm 0.9v + rbist.wo(utra::rbist_wrp::SFRCR_TRM, (6 << 16) | 0b011_000_00_0_0_000_0_00); + rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a); + rbist.wo(utra::rbist_wrp::SFRCR_TRM, (7 << 16) | 0b011_000_00_0_0_000_0_00); + rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a); + + // ifram 0.9v + rbist.wo(utra::rbist_wrp::SFRCR_TRM, (8 << 16) | 0b010_000_00_0_1_000_1_01); + rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a); + + // sce 0.9V + rbist.wo(utra::rbist_wrp::SFRCR_TRM, (9 << 16) | 0b011_000_00_0_0_000_0_00); + rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a); + for i in 0..4 { + rbist.wo(utra::rbist_wrp::SFRCR_TRM, ((10 + i) << 16) | 0b011_000_01_0_1_000_0_00); + rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a); + } + rbist.wo(utra::rbist_wrp::SFRCR_TRM, (14 << 16) | 0b001_010_00_0_0_000_0_00); + rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a); } + // unsafe, direct-writes to address offsets are used here instead of the UTRA abstraction // because there are some quirks in the early boot path that make the system more stable // if all register accesses are in-lined. @@ -161,55 +241,11 @@ pub fn early_init() -> u32 { } } } - #[cfg(feature = "sram-margin")] - unsafe { - // set SRAM delay to max - opens up timing margin as much a possible, supposedly? - let sram_ctl = utra::coresub_sramtrm::HW_CORESUB_SRAMTRM_BASE as *mut u32; - let waitcycles = 3; - sram_ctl.add(utra::coresub_sramtrm::SFR_SRAM0.offset()).write_volatile( - (sram_ctl.add(utra::coresub_sramtrm::SFR_SRAM0.offset()).read_volatile() & !0x18) - | ((waitcycles << 3) & 0x18), - ); - sram_ctl.add(utra::coresub_sramtrm::SFR_SRAM1.offset()).write_volatile( - (sram_ctl.add(utra::coresub_sramtrm::SFR_SRAM1.offset()).read_volatile() & !0x18) - | ((waitcycles << 3) & 0x18), - ); - } - // SoC emulator board parameters (deals with MMCM instead of PLL) - // Remove this once we feel confident we're sticking with SoC hardware. - /* - unsafe { - let poke_array: [(u32, u32, bool); 9] = [ - (0x40040030, 0x0001, true), // cgusel1 - (0x40040010, 0x0001, true), // cgusel0 - (0x40040010, 0x0001, true), // cgusel0 - (0x40040014, 0x007f, true), // fdfclk - (0x40040018, 0x007f, true), // fdaclk - (0x4004001c, 0x007f, true), // fdhclk - (0x40040020, 0x007f, true), // fdiclk - (0x40040024, 0x007f, true), // fdpclk - (0x400400a0, 0x4040, false), // pllmn FPGA - ]; - for &(addr, dat, is_u32) in poke_array.iter() { - let rbk = if is_u32 { - (addr as *mut u32).write_volatile(dat); - (addr as *const u32).read_volatile() - } else { - (addr as *mut u16).write_volatile(dat as u16); - (addr as *const u16).read_volatile() as u32 - }; - if dat != rbk { - crate::println!("{:08x}(w) != {:08x}(r)", dat, rbk); - } else { - crate::println!("{:08x} ok", dat); - } - } - } */ // Now setup the clocks for real // Safety: this can only be called in the early_init boot context #[cfg(not(feature = "simulation-only"))] - let perclk = unsafe { init_clock_asic(800_000_000) }; + let perclk = unsafe { init_clock_asic(SYSTEM_CLOCK_FREQUENCY) }; #[cfg(feature = "simulation-only")] let perclk = 100_000_000; crate::println!("Perclk is {} Hz", perclk); @@ -221,29 +257,28 @@ pub fn early_init() -> u32 { // Set up the IO mux to map UART_A0: // UART_RX_A[0] = PA3 app // UART_TX_A[0] = PA4 app - // UART_RX_A[1] = PD13 console - // UART_RX_A[1] = PD14 console + // UART_RX_B[2] = PB13 console + // UART_RX_B[2] = PB14 console #[allow(unused_mut)] // some configs require mut let mut iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); - iox.set_alternate_function(IoxPort::PD, 13, IoxFunction::AF1); - iox.set_alternate_function(IoxPort::PD, 14, IoxFunction::AF1); + iox.set_alternate_function(IoxPort::PB, 13, IoxFunction::AF1); + iox.set_alternate_function(IoxPort::PB, 14, IoxFunction::AF1); // rx as input, with pull-up - iox.set_gpio_dir(IoxPort::PD, 13, IoxDir::Input); - iox.set_gpio_pullup(IoxPort::PD, 13, IoxEnable::Enable); + iox.set_gpio_dir(IoxPort::PB, 13, IoxDir::Input); + iox.set_gpio_pullup(IoxPort::PB, 13, IoxEnable::Enable); // tx as output - iox.set_gpio_dir(IoxPort::PD, 14, IoxDir::Output); - + iox.set_gpio_dir(IoxPort::PB, 14, IoxDir::Output); // Set up the UDMA_UART block to the correct baud rate and enable status #[allow(unused_mut)] // some configs require mut let mut udma_global = GlobalConfig::new(); - udma_global.clock_on(PeriphId::Uart1); + udma_global.clock_on(PeriphId::Uart2); udma_global.map_event( - PeriphId::Uart1, + PeriphId::Uart2, PeriphEventType::Uart(EventUartOffset::Rx), EventChannel::Channel0, ); udma_global.map_event( - PeriphId::Uart1, + PeriphId::Uart2, PeriphEventType::Uart(EventUartOffset::Tx), EventChannel::Channel1, ); @@ -257,7 +292,7 @@ pub fn early_init() -> u32 { let uart_buf_addr = loader::UART_IFRAM_ADDR; let mut udma_uart = unsafe { // safety: this is safe to call, because we set up clock and events prior to calling new. - udma::Uart::get_handle(utra::udma_uart_1::HW_UDMA_UART_1_BASE, uart_buf_addr, uart_buf_addr) + udma::Uart::get_handle(utra::udma_uart_2::HW_UDMA_UART_2_BASE, uart_buf_addr, uart_buf_addr) }; crate::println!("Baud freq is {} Hz, baudrate is {}", freq, baudrate); udma_uart.set_baud(baudrate, freq); @@ -268,11 +303,6 @@ pub fn early_init() -> u32 { clock_tests(&mut udma_uart); } - // do a PL230/PIO test. Toggles PB15 (PIO0) with an LFSR sequence. - // let mut pl230 = xous_pl230::Pl230::new(); - // xous_pl230::pl230_tests::units::basic_tests(&mut pl230); - // xous_pl230::pl230_tests::units::pio_test(&mut pl230); - // Setup some global control registers that will allow the TRNG to operate once the kernel is // booted. This is done so the kernel doesn't have to exclusively have rights to the SCE global // registers just for this purpose. @@ -320,18 +350,6 @@ pub fn early_init() -> u32 { rcurst.wo(utra::sysctrl::SFR_RCURST0, 0x55AA); rcurst.wo(utra::sysctrl::SFR_RCURST1, 0x55AA); - /* - unsafe { - let ifram0 = HW_IFRAM0_MEM as *mut u32; - for i in 0..HW_IFRAM0_MEM_LEN / size_of::() { - ifram0.add(i).write_volatile(0); - } - let ifram1 = HW_IFRAM1_MEM as *mut u32; - for i in 0..HW_IFRAM1_MEM_LEN / size_of::() { - ifram1.add(i).write_volatile(0); - } - }*/ - delay(500); } }; @@ -339,13 +357,21 @@ pub fn early_init() -> u32 { if let Some(mut pmic) = pmic { // disable CCM, force PWM on for DCDC2 - required for rail stability pmic.set_pwm_mode(&mut i2c, cramium_hal::axp2101::WhichDcDc::Dcdc2, true).unwrap(); + pmic.set_dcdc(&mut i2c, Some((0.9, false)), cramium_hal::axp2101::WhichDcDc::Dcdc2).unwrap(); + // turns on the camera DCDC. TODO: make this turn on only when camera is active. + pmic.set_ldo(&mut i2c, Some(2.85), cramium_hal::axp2101::WhichLdo::Bldo2).unwrap(); + pmic.set_dcdc(&mut i2c, Some((1.8, false)), cramium_hal::axp2101::WhichDcDc::Dcdc5).unwrap(); + + // TODO: cleanup these legacy comments + /* pmic.set_ldo(&mut i2c, Some(2.7), cramium_hal::axp2101::WhichLdo::Aldo2).unwrap(); pmic.set_dcdc(&mut i2c, Some((1.8, false)), cramium_hal::axp2101::WhichDcDc::Dcdc4).unwrap(); pmic.set_dcdc(&mut i2c, Some((1.8, false)), cramium_hal::axp2101::WhichDcDc::Dcdc5).unwrap(); // apply proposed baosec settings pmic.debug(&mut i2c); + */ // This debug print creates a lot of extra code... // crate::println!("AXP2101 configure: {:?}", pmic); @@ -399,8 +425,55 @@ pub fn early_init() -> u32 { } } - // Turn off SE0 on USB - let _se0 = cramium_hal::board::setup_usb_pins(&iox); + // TODO: move this into the camera routine. For now, just free-run the clock; burns a lot of power. + // Also, the clock is at 12.5MHz, we really want this at 25MHz but maybe it's just not possible? + crate::println!("Setup PWM"); + { + use cramium_api::*; + use cramium_hal::iox::Iox; + let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); + iox.setup_pin(IoxPort::PF, 9, Some(IoxDir::Input), Some(IoxFunction::Gpio), None, None, None, None); + iox.setup_pin( + IoxPort::PA, + 0, + Some(IoxDir::Output), + Some(IoxFunction::Gpio), + None, + None, + Some(IoxEnable::Enable), + Some(IoxDriveStrength::Drive2mA), + ); + for _ in 0..8 { + iox.set_gpio_pin(IoxPort::PA, 0, IoxValue::Low); + iox.set_gpio_pin(IoxPort::PA, 0, IoxValue::High); + } + + iox.setup_pin( + IoxPort::PA, + 0, + Some(IoxDir::Output), + Some(IoxFunction::AF3), + None, + None, + Some(IoxEnable::Enable), + Some(IoxDriveStrength::Drive2mA), + ); + let mut timer = CSR::new(utra::pwm::HW_PWM_BASE as *mut u32); + timer.wo(utra::pwm::REG_CH_EN, 1); + timer.rmwf(utra::pwm::REG_TIM0_CFG_R_TIMER0_SAW, 0); + timer.rmwf(utra::pwm::REG_TIM0_CH0_TH_R_TIMER0_CH0_TH, 0); + timer.rmwf(utra::pwm::REG_TIM0_CH0_TH_R_TIMER0_CH0_MODE, 3); + let pwm = utra::pwm::HW_PWM_BASE as *mut u32; + unsafe { pwm.add(2).write_volatile(1 << 16) }; + timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 1); + crate::println!("PWM running on PA0?"); + for i in 0..12 { + crate::println!("0x{:2x}: 0x{:08x}", i, unsafe { pwm.add(i).read_volatile() }) + } + crate::println!("0x{:2x}: 0x{:08x}", 65, unsafe { pwm.add(65).read_volatile() }); + crate::println!(""); + } + #[cfg(feature = "board-bringup")] let iox_loop = iox.clone(); @@ -1455,96 +1528,144 @@ pub fn platform_tests() { test_duart(); } // returns the actual per_clk #[cfg(not(feature = "simulation-only"))] -unsafe fn init_clock_asic(freq_hz: u32) -> u32 { +pub unsafe fn init_clock_asic(freq_hz: u32) -> u32 { use utra::sysctrl; let daric_cgu = sysctrl::HW_SYSCTRL_BASE as *mut u32; - /* - Code notes from RTL: - assign pll_m = ipc_pllmn[16:12]; - assign pll_n = ipc_pllmn[11: 0]; - assign pll_f = ipc_pllf[23: 0]; - assign pll_fen = ipc_pllf[24]; - assign pll_q00 = ipc_pllq[ 2: 0]; - assign pll_q10 = ipc_pllq[ 6: 4]; - assign pll_q01 = ipc_pllq[10: 8]; - assign pll_q11 = ipc_pllq[14:12]; + let mut cgu = CSR::new(daric_cgu); - Clko0 = Fvco / (pllq[ 2:0] + 1) / (pllq[ 6:4] + 1) - Clko1 = Fvco / (pllq[10:8] + 1) / (pllq[14:12] + 1) - Fvco target is 2GHz (1-3GHz range) + const UNIT_MHZ: u32 = 1000 * 1000; + const PFD_F_MHZ: u32 = 16; + const FREQ_0: u32 = 16 * UNIT_MHZ; + const FREQ_OSC_MHZ: u32 = 48; // Actually 48MHz + const M: u32 = FREQ_OSC_MHZ / PFD_F_MHZ; // - 1; // OSC input was 24, replace with 48 - .gvco_bias ( pll_bias[7:6] ), - .cpp_bias ( pll_bias[5:3] ), - .cpi_bias ( pll_bias[2:0] ), - */ - // Derive VCO frequency from legal, even dividers that get us close to our target frequency - const TARGET_VCO_HZ: u32 = 1_600_000_000; // 1.6GHz - let final_div: u32 = TARGET_VCO_HZ / freq_hz; - // fclk_div has to be a power of 2 - let fclk_div = - if (1 << final_div.ilog2()) != final_div { 1 << (final_div.ilog2() + 1) } else { final_div }; - let vco_actual: u32 = fclk_div * freq_hz; - if vco_actual < 1_000_000_000 || vco_actual > 3_000_000_000 { - crate::println!("Warning: VCO out of range: {}", vco_actual); - } - const TARGET_PERCLK_HZ: u32 = 100_000_000; // 100 MHz - let perclk_np_div: u32 = vco_actual / TARGET_PERCLK_HZ; - let perclk_div = if (1 << perclk_np_div.ilog2()) != perclk_np_div { - 1 << (perclk_np_div.ilog2() + 1) - } else { - perclk_np_div + const TBL_Q: [u16; 7] = [ + // keep later DIV even number as possible + 0x7777, // 16-32 MHz + 0x7737, // 32-64 + 0x3733, // 64-128 + 0x3313, // 128-256 + 0x3311, // 256-512 // keep ~ 100MHz + 0x3301, // 512-1024 + 0x3301, // 1024-1600 + ]; + const TBL_MUL: [u32; 7] = [ + 64, // 16-32 MHz + 32, // 32-64 + 16, // 64-128 + 8, // 128-256 + 4, // 256-512 + 2, // 512-1024 + 2, // 1024-1600 + ]; + + // this block might belong at the top, in particular, configuring the dividers prevents stuff + // from being overclocked when the PLL comes live; but for now we are debugging other stuff + let perclk = { + // Hits a 16:8:4:2:1 ratio on fclk:aclk:hclk:iclk:pclk + // Resulting in 800:400:200:100:50 MHz assuming 800MHz fclk + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0.offset()).write_volatile(0x3fff); // fclk + + // Hits a 8:8:4:2:1 ratio on fclk:aclk:hclk:iclk:pclk + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1.offset()).write_volatile(0x3f7f); // aclk + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2.offset()).write_volatile(0x1f3f); // hclk + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_3.offset()).write_volatile(0x0f1f); // iclk + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_4.offset()).write_volatile(0x070f); // pclk + // TODO: derive this from the actual perclk value, based on the values in the lookup table below... + let perclk = if freq_hz > 400_000_000 { + daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x07_ff_ff); + freq_hz / 8 + } else { + daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x03_ff_ff); + freq_hz / 4 + }; + + /* + perclk fields: min-cycle-lp | min-cycle | fd-lp | fd + clkper fd + 0xff : Fperclk = Fclktop/2 + 0x7f: Fperclk = Fclktop/4 + 0x3f : Fperclk = Fclktop/8 + 0x1f : Fperclk = Fclktop/16 + 0x0f : Fperclk = Fclktop/32 + 0x07 : Fperclk = Fclktop/64 + 0x03: Fperclk = Fclktop/128 + 0x01: Fperclk = Fclktop/256 + + min cycle of clktop, F means frequency + Fperclk Max = Fperclk/(min cycle+1)*2 + */ + + // turn off gates + daric_cgu.add(utra::sysctrl::SFR_ACLKGR.offset()).write_volatile(0x2f); + daric_cgu.add(utra::sysctrl::SFR_HCLKGR.offset()).write_volatile(0xff); + daric_cgu.add(utra::sysctrl::SFR_ICLKGR.offset()).write_volatile(0x8f); + daric_cgu.add(utra::sysctrl::SFR_PCLKGR.offset()).write_volatile(0xff); + crate::println!("bef gates set"); + for _ in 0..100 { + crate::print!("*"); + } + crate::println!("."); + // commit dividers + daric_cgu.add(utra::sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); + crate::println!("gates set"); + for _ in 0..100 { + crate::print!("-"); + } + crate::println!("."); + perclk }; - let ilog2_fdiv = fclk_div.ilog2(); - let ilog2_pdiv = perclk_div.ilog2(); - let pll_q0_0 = (1 << (ilog2_fdiv / 2)) - 1; - let pll_q1_0 = (1 << (ilog2_fdiv / 2 + ilog2_fdiv % 2)) - 1; - let pll_q0_1 = (1 << (ilog2_pdiv / 2)) - 1; - let pll_q1_1 = (1 << (ilog2_pdiv / 2 + ilog2_pdiv % 2)) - 1; - if pll_q0_0 > 7 || pll_q0_1 > 7 || pll_q1_0 > 7 || pll_q1_1 > 7 { - crate::println!( - "Warning: PLLQ out of range: 0_0:{} 1_0:{} 0_1:{} 1_1:{}", - pll_q0_0, - pll_q1_0, - pll_q0_1, - pll_q1_1 - ); + + for _ in 0..100 { + crate::print!("1"); } - // this is the pllq value - let pllq = (pll_q0_0 & 7) | ((pll_q1_0 & 7) << 4) | ((pll_q0_1 & 7) << 8) | ((pll_q1_1 & 7) << 12); + crate::println!("."); - // now, program the VCO to get to as close to vco_actual - const FREF_HZ: u32 = 48_000_000; - // adjust m so that PFD runs between 4-16MHz (target 8MHz) - const PREDIV_M: u32 = 6; - let fref_hz = FREF_HZ / PREDIV_M; - assert!(fref_hz == 8_000_000); + cgu.wo(sysctrl::SFR_CGUSEL1, 1); + cgu.wo(sysctrl::SFR_CGUFSCR, FREQ_OSC_MHZ); + cgu.wo(sysctrl::SFR_CGUSET, 0x32); - let ni = vco_actual / fref_hz; - if ni >= 4096 || ni < 8 { - crate::println!("Warning: ni out of range: {}", ni); + let duart = utra::duart::HW_DUART_BASE as *mut u32; + duart.add(utra::duart::SFR_CR.offset()).write_volatile(0); + // set the ETUC now that we're on the xosc. + duart.add(utra::duart::SFR_ETUC.offset()).write_volatile(FREQ_OSC_MHZ); + duart.add(utra::duart::SFR_CR.offset()).write_volatile(1); + + for _ in 0..100 { + crate::print!("2"); } - let pllmn = (PREDIV_M << 12) | ni & 0xFFF; // m is set to PREDIV_M, lower 12 bits is nf - let frac_n = ((vco_actual as f32 / fref_hz as f32) - ni as f32).max(0 as f32); - let pllf: u32 = (frac_n * ((1 << 24) as f32)) as u32; - if pllf >= 1 << 24 { - crate::println!("Warning nf out of range: 0x{:x}", pllf); + crate::println!("."); + + if freq_hz < 1000000 { + cgu.wo(sysctrl::SFR_IPCOSC, freq_hz); + cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); } - let n_frac = if pllf != 0 { pllf | 1 << 24 } else { 0 }; // set the frac enable bit if needed - - crate::println!("pllq: 0x{:x}, pllmn: 0x{:x}, n_frac: 0x{:x}", pllq, pllmn, n_frac); - // switch to OSC - daric_cgu.add(sysctrl::SFR_CGUSEL0.offset()).write_volatile(0); // clktop sel, 0:clksys, 1:clkpll0 - daric_cgu.add(sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); // commit + // clktop sel, 0:clksys, 1:clkpll0 + cgu.wo(sysctrl::SFR_CGUSEL0, 0); + // commit + cgu.wo(sysctrl::SFR_CGUSET, 0x32); - if 0 == freq_hz { - // do nothing + for _ in 0..100 { + crate::print!("3"); + } + crate::println!("."); + + if freq_hz < 1000000 { } else { - // powerdown PLL - daric_cgu - .add(sysctrl::SFR_IPCLPEN.offset()) - .write_volatile(daric_cgu.add(sysctrl::SFR_IPCLPEN.offset()).read_volatile() | 0x02); - daric_cgu.add(sysctrl::SFR_IPCARIPFLOW.offset()).write_volatile(0x32); // commit, must write 32 + let n_fxp24: u64; // fixed point + let f16mhz_log2: u32 = (freq_hz / FREQ_0).ilog2(); + + for _ in 0..100 { + crate::print!("4"); + } + crate::println!("."); + + // PD PLL + cgu.wo(sysctrl::SFR_IPCLPEN, cgu.r(sysctrl::SFR_IPCLPEN) | 0x2); + // commit, must write 32 + cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); + // __DSB(); // delay for _ in 0..1024 { @@ -1552,32 +1673,29 @@ unsafe fn init_clock_asic(freq_hz: u32) -> u32 { } crate::println!("PLL delay 1"); - daric_cgu.add(sysctrl::SFR_IPCPLLMN.offset()).write_volatile(pllmn); // 0x1F598; - daric_cgu.add(sysctrl::SFR_IPCPLLF.offset()).write_volatile(n_frac); // 0x2812 - daric_cgu.add(sysctrl::SFR_IPCPLLQ.offset()).write_volatile(pllq); // 0x2401 TODO select DIV for VCO freq + n_fxp24 = (((freq_hz as u64) << 24) * TBL_MUL[f16mhz_log2 as usize] as u64 + + PFD_F_MHZ as u64 * UNIT_MHZ as u64 / 2) + / (PFD_F_MHZ as u64 * UNIT_MHZ as u64); // rounded + let n_frac: u32 = (n_fxp24 & 0x00ffffff) as u32; + cgu.wo(sysctrl::SFR_IPCPLLMN, ((M << 12) & 0x0001F000) | (((n_fxp24 >> 24) as u32) & 0x00000fff)); + // DARIC_IPC->pll_f = n_frac | ((0 == n_frac) ? 0 : (1UL << 24)); + cgu.wo(sysctrl::SFR_IPCPLLF, n_frac | if 0 == n_frac { 0 } else { 1u32 << 24 }); + // DARIC_IPC->pll_q = TBL_Q[f16MHzLog2]; // ?? TODO select DIV for VCO freq + cgu.wo(sysctrl::SFR_IPCPLLQ, TBL_Q[f16mhz_log2 as usize] as u32); // VCO bias CPP bias CPI bias // 1 2 3 - // DARIC_IPC->ipc = (3 << 6) | (5 << 3) | (5); - daric_cgu.add(sysctrl::SFR_IPCCR.offset()).write_volatile((1 << 6) | (2 << 3) | (3)); - // daric_cgu.add(sysctrl::SFR_IPCCR.offset()).write_volatile((3 << 6) | (5 << 3) | (5)); - daric_cgu.add(sysctrl::SFR_IPCARIPFLOW.offset()).write_volatile(0x32); // commit, must write 32 + //DARIC_IPC->ipc = (3 << 6) | (5 << 3) | (5); + // DARIC_IPC->ipc = (1 << 6) | (2 << 3) | (3); + cgu.wo(sysctrl::SFR_IPCCR, (1 << 6) | (2 << 3) | (3)); + // commit + cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); - // ensure the correct oscillator is selected here. It's put after the PLL is configured - // to ensure the PLL doesn't get edges while in an invalid configuration. - daric_cgu.add(sysctrl::SFR_CGUSEL1.offset()).write_volatile(1); // 0: RC, 1: XTAL - daric_cgu.add(sysctrl::SFR_CGUFSCR.offset()).write_volatile(48); // external crystal is 48MHz - daric_cgu.add(sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); + cgu.wo(sysctrl::SFR_IPCLPEN, cgu.r(sysctrl::SFR_IPCLPEN) & !0x2); - if freq_hz < 1_000_000 { - daric_cgu.add(sysctrl::SFR_IPCOSC.offset()).write_volatile(freq_hz); - daric_cgu.add(sysctrl::SFR_IPCARIPFLOW.offset()).write_volatile(0x32); // commit, must write 32 - } - - daric_cgu - .add(sysctrl::SFR_IPCLPEN.offset()) - .write_volatile(daric_cgu.add(sysctrl::SFR_IPCLPEN.offset()).read_volatile() & !0x02); - daric_cgu.add(sysctrl::SFR_IPCARIPFLOW.offset()).write_volatile(0x32); // commit, must write 32 + // commit + cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); + // __DSB(); // delay for _ in 0..1024 { @@ -1585,68 +1703,38 @@ unsafe fn init_clock_asic(freq_hz: u32) -> u32 { } crate::println!("PLL delay 2"); - daric_cgu.add(sysctrl::SFR_CGUSEL0.offset()).write_volatile(1); // clktop sel, 0:clksys, 1:clkpll0 - daric_cgu.add(sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); // commit - - for _ in 0..1024 { - unsafe { core::arch::asm!("nop") }; - } - crate::println!("PLL delay 3"); - - crate::println!("fsvalid: {}", daric_cgu.add(sysctrl::SFR_CGUFSVLD.offset()).read_volatile()); - let _cgufsfreq0 = daric_cgu.add(sysctrl::SFR_CGUFSSR_FSFREQ0.offset()).read_volatile(); - let _cgufsfreq1 = daric_cgu.add(sysctrl::SFR_CGUFSSR_FSFREQ1.offset()).read_volatile(); - let _cgufsfreq2 = daric_cgu.add(sysctrl::SFR_CGUFSSR_FSFREQ2.offset()).read_volatile(); - let _cgufsfreq3 = daric_cgu.add(sysctrl::SFR_CGUFSSR_FSFREQ3.offset()).read_volatile(); - crate::println!( - "Internal osc: {} -> {} MHz ({} MHz)", - _cgufsfreq0, - fsfreq_to_hz(_cgufsfreq0), - fsfreq_to_hz_32(_cgufsfreq0) - ); - crate::println!( - "XTAL: {} -> {} MHz ({} MHz)", - _cgufsfreq1, - fsfreq_to_hz(_cgufsfreq1), - fsfreq_to_hz_32(_cgufsfreq1) - ); - crate::println!( - "pll output 0: {} -> {} MHz ({} MHz)", - _cgufsfreq2, - fsfreq_to_hz(_cgufsfreq2), - fsfreq_to_hz_32(_cgufsfreq2) - ); - crate::println!( - "pll output 1: {} -> {} MHz ({} MHz)", - _cgufsfreq3, - fsfreq_to_hz(_cgufsfreq3), - fsfreq_to_hz_32(_cgufsfreq3) - ); - - // Hits a 16:8:4:2:1 ratio on fclk:aclk:hclk:iclk:pclk - // Resulting in 800:400:200:100:50 MHz assuming 800MHz fclk - #[cfg(feature = "fast-fclk")] - daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0.offset()).write_volatile(0x7fff); // fclk - - // Hits a 8:8:4:2:1 ratio on fclk:aclk:hclk:iclk:pclk - // Resulting in 400:400:200:100:50 MHz assuming 800MHz fclk - #[cfg(not(feature = "fast-fclk"))] - daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0.offset()).write_volatile(0x7f7f); // fclk - daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1.offset()).write_volatile(0x3f7f); // aclk - daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2.offset()).write_volatile(0x1f3f); // hclk - daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_3.offset()).write_volatile(0x0f1f); // iclk - daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_4.offset()).write_volatile(0x070f); // pclk - - #[cfg(not(feature = "cramium-mpw"))] - // perclk divider - set to divide by 8 off of an 800Mhz base. Only found on NTO. - daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x03_ff_ff); - - // commit dividers - daric_cgu.add(utra::sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); + // TODO wait/poll lock status? + // DARIC_CGU->cgusel0 = 1; // clktop sel, 0:clksys, 1:clkpll0 + cgu.wo(sysctrl::SFR_CGUSEL0, 1); + // __DSB(); + // DARIC_CGU->cguset = 0x32; // commit + cgu.wo(sysctrl::SFR_CGUSET, 0x32); + crate::println!("clocks set"); } - crate::println!("PLL configured to {} MHz", freq_hz / 1_000_000); + crate::println!( + "mn {:x}, q{:x}", + (0x400400a0 as *const u32).read_volatile(), + (0x400400a8 as *const u32).read_volatile() + ); - vco_actual / perclk_div + crate::println!("fsvalid: {}", daric_cgu.add(sysctrl::SFR_CGUFSVLD.offset()).read_volatile()); + let clk_desc: [(&'static str, u32, usize); 8] = [ + ("fclk", 16, 0x40 / size_of::()), + ("pke", 0, 0x40 / size_of::()), + ("ao", 16, 0x44 / size_of::()), + ("aoram", 0, 0x44 / size_of::()), + ("osc", 16, 0x48 / size_of::()), + ("xtal", 0, 0x48 / size_of::()), + ("pll0", 16, 0x4c / size_of::()), + ("pll1", 0, 0x4c / size_of::()), + ]; + for (name, shift, offset) in clk_desc { + let fsfreq = (daric_cgu.add(offset).read_volatile() >> shift) & 0xffff; + crate::println!("{}: {} MHz", name, fsfreq); + } + + crate::println!("PLL configured to {} MHz; perclk {}", freq_hz / 1_000_000, perclk); + perclk } #[allow(dead_code)]