From 11b506bdc4e008a96c323a51d40dded1c8e9d210 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Dec 2025 15:55:20 +0800 Subject: [PATCH 1/3] build a consolidated clock setting routine This routine pares down the clock setting routine to hopefully a core minimum set of side-effects while allowing the system to boot. It also takes the register bank offsets as arguments, as well as various functions to accomplish different tasks as argument, so that it can be used in different contexts (such as in `std`). Finally, the BIO clock rate can be set to slow or fast. The power cost of the fast setting is about 12mW. If you're not needing the high speed features of the BIO, slow setting is probably what you want. Currently, the system is configured such that after boot1, the BIO is running in slow mode (to save power for battery powered configurations); and then the clock set calls in baremetal and dabao bump this up to the fast speed since these two configurations are assumed to be running off of a USB wire with ample power. --- libs/bao1x-hal/src/clocks.rs | 235 +++++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 libs/bao1x-hal/src/clocks.rs diff --git a/libs/bao1x-hal/src/clocks.rs b/libs/bao1x-hal/src/clocks.rs new file mode 100644 index 000000000..cf3fd0d20 --- /dev/null +++ b/libs/bao1x-hal/src/clocks.rs @@ -0,0 +1,235 @@ +use utralib::*; + +/// Requires: +/// `freq_hz`: target frequency of PLL - generally it is 2x of the CPU clock frequency +/// `daric_cgu`: Pointer to SYSCTL_BASE - for setting clocks +/// `ao_sysctl`: Pointer to AO_SYSCTRL_BASE - for setting regulators according to clocks +/// `duart`: Pointer to DUART_BASE - for setting the DUART's ETUC divider - only set if provided +/// `delay_at_sysfreq(usize, u32)`: Function that can perform a delay of usize milliseconds at u32 hz +/// this is needed to wait while the power regulators adjust. +/// `fast_bio`: when `true`, BIO clocks at 2x CPU clock. This increases baseline power consumption +/// by about 12mW. Not a big deal if the board is plugged in, but significant for battery-powered systems. +/// +/// (deprecated) `udma_ctrl_base`: Pointer to HW_UDMA_CTRL_BASE - for clearing the UDMA clock gates +/// +/// Register offsets are passed in so the routine can also be used in `std` environments, by a manager +/// that already has mapping to the required registers. `duart` is not always available in the manager's +/// space, which is why it's an Option. +/// +/// The coding style is a little awkward in this routine - a number of registers are unpacked +/// using direct offsets and a write_volatile(). This was done to ensure that the compiler is not +/// optimizing out repeated writes. This could be cleaned up to use the UTRA abstractions, but because +/// the code works and it was pretty hard to get it validated, there is an incentive not to touch it. +pub unsafe fn init_clock_asic( + freq_hz: u32, + cgu_base: usize, + ao_sysctl_base: usize, + duart_base: Option, + delay_at_sysfreq: fn(usize, u32), + fast_bio: bool, +) -> u32 { + use utra::sysctrl; + let daric_cgu = cgu_base as *mut u32; + let mut cgu = CSR::new(daric_cgu); + + const UNIT_MHZ: u32 = 1000 * 1000; + const PFD_F_MHZ: u32 = 16; + const FREQ_0: u32 = 16 * UNIT_MHZ; + const M: u32 = bao1x_api::FREQ_OSC_MHZ / PFD_F_MHZ; // - 1; // OSC input was 24, replace with 48 + + 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 + ]; + + // Safest divider settings, assuming no overclocking. + // If overclocking, need to lower hclk:iclk:pclk even futher; the CPU speed can outperform the bus fabric. + // 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 + if fast_bio { + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0.offset()).write_volatile(0x3fff); // fclk + } else { + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0.offset()).write_volatile(0x3f7f); // 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 + + // calculate perclk divider. Target 100MHz. + + // perclk divider - set to divide by 16 off of an 800Mhz base. Only found on bao1x. + // daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x03_ff_ff); + // perclk divider - set to divide by 8 off of an 800Mhz base. Only found on bao1x. + let (min_cycle, fd, perclk) = + if let Some((min_cycle, fd, perclk)) = bao1x_api::clk_to_per(freq_hz / 1_000_000, 100) { + daric_cgu + .add(utra::sysctrl::SFR_CGUFDPER.offset()) + .write_volatile((min_cycle as u32) << 16 | (fd as u32) << 8 | fd as u32); + (min_cycle, fd, perclk * 1_000_000) + } else if freq_hz > 400_000_000 { + daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x07_ff_ff); + (7, 0xff, freq_hz / 8) + } else { + daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x03_ff_ff); + (3, 0xff, 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 + */ + + // configure gates + daric_cgu.add(utra::sysctrl::SFR_ACLKGR.offset()).write_volatile(0x00); // mbox/qfc turned off + daric_cgu.add(utra::sysctrl::SFR_HCLKGR.offset()).write_volatile(0x02); // mdma off, sce on + daric_cgu.add(utra::sysctrl::SFR_ICLKGR.offset()).write_volatile(0x90); // bio/udc enable + daric_cgu.add(utra::sysctrl::SFR_PCLKGR.offset()).write_volatile(0x80); // enable mesh + // commit dividers + daric_cgu.add(utra::sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); + + let mut ao_sysctrl = CSR::new(ao_sysctl_base as *mut u32); + if freq_hz > 700_000_000 { + // crate::println!("setting vdd85 to 0.893v"); + ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08421FF1); + ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM1CSR, 0x2); + cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x57); + delay_at_sysfreq(20, 48_000_000); + } else if freq_hz > 350_000_000 { + // crate::println!("setting vdd85 to 0.81v"); + ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08421290); + ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM1CSR, 0x2); + cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x57); + delay_at_sysfreq(20, 48_000_000); + } else { + // crate::println!("setting vdd85 to 0.72v"); + ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08420420); + ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM1CSR, 0x2); + cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x57); + delay_at_sysfreq(20, 48_000_000); + } + + // 0: RC, 1: XTAL + cgu.wo(sysctrl::SFR_CGUSEL1, 1); + cgu.wo(sysctrl::SFR_CGUFSCR, bao1x_api::FREQ_OSC_MHZ); + cgu.wo(sysctrl::SFR_CGUSET, 0x32); + + if let Some(duart_ptr) = duart_base { + let duart = duart_ptr 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(bao1x_api::FREQ_OSC_MHZ); + duart.add(utra::duart::SFR_CR.offset()).write_volatile(1); + } + + if freq_hz <= 1_000_000 { + cgu.wo(sysctrl::SFR_IPCOSC, freq_hz); + cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); + } + // switch to OSC - now clocking off of external oscillator (glitch hazard!) + // clktop sel, 0:clksys, 1:clkpll0 + cgu.wo(sysctrl::SFR_CGUSEL0, 0); + cgu.wo(sysctrl::SFR_CGUSET, 0x32); + + if freq_hz <= 1_000_000 { + } else { + let n_fxp24: u64; // fixed point + let f16mhz_log2: u32 = (freq_hz / FREQ_0).ilog2(); + + // PD PLL + cgu.wo(sysctrl::SFR_IPCLPEN, cgu.r(sysctrl::SFR_IPCLPEN) | 0x2); + cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); + + for _ in 0..4096 { + bao1x_api::bollard!(4); + } + + 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)); + cgu.wo(sysctrl::SFR_IPCPLLF, n_frac | if 0 == n_frac { 0 } else { 1u32 << 24 }); + cgu.wo(sysctrl::SFR_IPCPLLQ, TBL_Q[f16mhz_log2 as usize] as u32); + // VCO bias CPP bias CPI bias + // 1 2 3 + cgu.wo(sysctrl::SFR_IPCCR, (1 << 6) | (2 << 3) | (3)); + cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); + + cgu.wo(sysctrl::SFR_IPCLPEN, cgu.r(sysctrl::SFR_IPCLPEN) & !0x2); + + cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); + + for _ in 0..4096 { + bao1x_api::bollard!(4); + } + + bao1x_api::bollard!(6); + cgu.wo(sysctrl::SFR_CGUSEL0, 1); + cgu.wo(sysctrl::SFR_CGUSET, 0x32); + bao1x_api::bollard!(6); + } + // glitch_safety: check that we're running on the PLL + #[cfg(not(feature = "kernel"))] + crate::hardening::check_pll(); + + crate::println!( + "mn {:x}, q{:x}", + (0x400400a0 as *const u32).read_volatile(), + (0x400400a8 as *const u32).read_volatile() + ); + 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); + } + // Comment this out - I think we manage the UDMA clocks explicitly per-driver + // let mut udmacore = CSR::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32); + // udmacore.wo(utra::udma_ctrl::REG_CG, 0xFFFF_FFFF); + + crate::println!("Perclk solution: {:x}|{:x} -> {} MHz", min_cycle, fd, perclk / 1_000_000); + crate::println!("PLL configured to {} MHz", freq_hz / 1_000_000); + + // glitch_safety: check that we're running on the PLL + #[cfg(not(feature = "kernel"))] + crate::hardening::check_pll(); + + perclk +} From a3f9a9942d172bf8e7ebff88bb6f5ea01b6f2738 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Dec 2025 15:58:27 +0800 Subject: [PATCH 2/3] update APIs to use the central clocking implementation --- bao1x-boot/boot1/src/platform/bao1x/bao1x.rs | 268 +---------------- baremetal/src/platform/bao1x/bao1x.rs | 285 ++----------------- libs/bao1x-hal/src/lib.rs | 11 +- loader/src/platform/bao1x/bao1x.rs | 235 +-------------- 4 files changed, 48 insertions(+), 751 deletions(-) diff --git a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs index 998cac84c..311c69a69 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs @@ -330,7 +330,18 @@ pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> (bao1x_api::Boa BoardTypeCoding::Oem => SAFE_FCLK_FREQUENCY, BoardTypeCoding::Dabao => bao1x_api::offsets::dabao::DEFAULT_FCLK_FREQUENCY, }; - let perclk = unsafe { init_clock_asic(fclk_freq) }; + let perclk = unsafe { + bao1x_hal::clocks::init_clock_asic( + fclk_freq, + utra::sysctrl::HW_SYSCTRL_BASE, + utralib::HW_AO_SYSCTRL_BASE, + Some(utra::duart::HW_DUART_BASE), + delay_at_sysfreq, + // slow BIO for better power savings. Overridden in baremetal & dabao images for faster + // performance since these are plug-in applications. + false, + ) + }; crate::println!("scratch page: {:x}, heap start: {:x}", SCRATCH_PAGE, HEAP_START); @@ -421,261 +432,6 @@ mod panic_handler { } } -pub unsafe fn init_clock_asic(freq_hz: u32) -> u32 { - use utra::sysctrl; - let daric_cgu = sysctrl::HW_SYSCTRL_BASE as *mut u32; - let mut cgu = CSR::new(daric_cgu); - - const UNIT_MHZ: u32 = 1000 * 1000; - const PFD_F_MHZ: u32 = 16; - const FREQ_0: u32 = 16 * UNIT_MHZ; - const M: u32 = bao1x_api::FREQ_OSC_MHZ / PFD_F_MHZ; // - 1; // OSC input was 24, replace with 48 - - 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 - ]; - - // Safest divider settings, assuming no overclocking. - // If overclocking, need to lower hclk:iclk:pclk even futher; the CPU speed can outperform the bus fabric. - // 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(0x3f7f); // 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 - - // calculate perclk divider. Target 100MHz. - - // perclk divider - set to divide by 16 off of an 800Mhz base. Only found on bao1x. - // daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x03_ff_ff); - // perclk divider - set to divide by 8 off of an 800Mhz base. Only found on bao1x. - // TODO: this only works for two clock settings. Broken @ 600. Need to fix this to instead derive - // what pclk is instead of always reporting 100mhz - let (min_cycle, fd, perclk) = - if let Some((min_cycle, fd, perclk)) = bao1x_api::clk_to_per(freq_hz / 1_000_000, 100) { - daric_cgu - .add(utra::sysctrl::SFR_CGUFDPER.offset()) - .write_volatile((min_cycle as u32) << 16 | (fd as u32) << 8 | fd as u32); - (min_cycle, fd, perclk * 1_000_000) - } else if freq_hz > 400_000_000 { - daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x07_ff_ff); - (7, 0xff, freq_hz / 8) - } else { - daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x03_ff_ff); - (3, 0xff, 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(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 dividers - daric_cgu.add(utra::sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); - - if freq_hz > 700_000_000 { - crate::println!("setting vdd85 to 0.893v"); - let mut ao_sysctrl = CSR::new(utralib::HW_AO_SYSCTRL_BASE as *mut u32); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08421FF1); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM1CSR, 0x2); - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x57); - crate::platform::delay_at_sysfreq(20, 48_000_000); - } else if freq_hz > 350_000_000 { - crate::println!("setting vdd85 to 0.81v"); - let mut ao_sysctrl = CSR::new(utralib::HW_AO_SYSCTRL_BASE as *mut u32); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08421290); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM1CSR, 0x2); - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x57); - crate::platform::delay_at_sysfreq(20, 48_000_000); - } else { - crate::println!("setting vdd85 to 0.72v"); - let mut ao_sysctrl = CSR::new(utralib::HW_AO_SYSCTRL_BASE as *mut u32); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08420420); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM1CSR, 0x2); - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x57); - crate::platform::delay_at_sysfreq(20, 48_000_000); - } - crate::println!("...done"); - - // DARIC_CGU->cgusel1 = 1; // 0: RC, 1: XTAL - cgu.wo(sysctrl::SFR_CGUSEL1, 1); - // DARIC_CGU->cgufscr = FREQ_OSC_MHZ; // external crystal is 48MHz - cgu.wo(sysctrl::SFR_CGUFSCR, bao1x_api::FREQ_OSC_MHZ); - // __DSB(); - // DARIC_CGU->cguset = 0x32UL; - cgu.wo(sysctrl::SFR_CGUSET, 0x32); - // __DSB(); - - 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(bao1x_api::FREQ_OSC_MHZ); - duart.add(utra::duart::SFR_CR.offset()).write_volatile(1); - - if freq_hz <= 1_000_000 { - // DARIC_IPC->osc = freqHz; - cgu.wo(sysctrl::SFR_IPCOSC, freq_hz); - // __DSB(); - // DARIC_IPC->ar = 0x0032; // commit, must write 32 - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); - // __DSB(); - } - // switch to OSC - //DARIC_CGU->cgusel0 = 0; // clktop sel, 0:clksys, 1:clkpll0 - cgu.wo(sysctrl::SFR_CGUSEL0, 0); - // __DSB(); - // DARIC_CGU->cguset = 0x32; // commit - cgu.wo(sysctrl::SFR_CGUSET, 0x32); - //__DSB(); - - if freq_hz <= 1_000_000 { - } else { - let n_fxp24: u64; // fixed point - let f16mhz_log2: u32 = (freq_hz / FREQ_0).ilog2(); - - // PD PLL - // DARIC_IPC->lpen |= 0x02 ; - cgu.wo(sysctrl::SFR_IPCLPEN, cgu.r(sysctrl::SFR_IPCLPEN) | 0x2); - // __DSB(); - // DARIC_IPC->ar = 0x0032; // commit, must write 32 - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); - // __DSB(); - - // delay - // for (uint32_t i = 0; i < 1024; i++){ - // __NOP(); - //} - for _ in 0..1024 { - unsafe { core::arch::asm!("nop") }; - } - crate::println!("PLL delay 1"); - // why is this print needed for the code not to crash? - crate::println!("freq_hz {} log2 {}", freq_hz, f16mhz_log2); - 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; - - // TODO very verbose - //printf ("%s(%4" PRIu32 "MHz) M = %4" PRIu32 ", N = %4" PRIu32 ".%08" PRIu32 ", Q = %2lu, QDiv = - // 0x%04" PRIx16 "\n", __FUNCTION__, freqHz / 1000000, M, (uint32_t)(n_fxp24 >> 24), - // (uint32_t)((uint64_t)(n_fxp24 & 0x00ffffff) * 100000000/(1UL <<24)), TBL_MUL[f16MHzLog2], - // TBL_Q[f16MHzLog2]); DARIC_IPC->pll_mn = ((M << 12) & 0x0001F000) | ((n_fxp24 >> 24) & - // 0x00000fff); // 0x1F598; // ?? - 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_IPC->ipc = (1 << 6) | (2 << 3) | (3); - cgu.wo(sysctrl::SFR_IPCCR, (1 << 6) | (2 << 3) | (3)); - // __DSB(); - // DARIC_IPC->ar = 0x0032; // commit - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); - // __DSB(); - - // DARIC_IPC->lpen &= ~0x02; - cgu.wo(sysctrl::SFR_IPCLPEN, cgu.r(sysctrl::SFR_IPCLPEN) & !0x2); - - //__DSB(); - // DARIC_IPC->ar = 0x0032; // commit - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); - // __DSB(); - - // delay - // for (uint32_t i = 0; i < 1024; i++){ - // __NOP(); - // } - for _ in 0..1024 { - unsafe { core::arch::asm!("nop") }; - } - crate::println!("PLL delay 2"); - - //printf("read reg a0 : %08" PRIx32"\n", *((volatile uint32_t* )0x400400a0)); - //printf("read reg a4 : %04" PRIx16"\n", *((volatile uint16_t* )0x400400a4)); - //printf("read reg a8 : %04" PRIx16"\n", *((volatile uint16_t* )0x400400a8)); - - // 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"); - - // __DSB(); - - // printf (" MN: 0x%05x, F: 0x%06x, Q: 0x%04x\n", - // DARIC_IPC->pll_mn, DARIC_IPC->pll_f, DARIC_IPC->pll_q); - // printf (" LPEN: 0x%01x, OSC: 0x%04x, BIAS: 0x%04x,\n", - // DARIC_IPC->lpen, DARIC_IPC->osc, DARIC_IPC->ipc); - } - crate::println!( - "mn {:x}, q{:x}", - (0x400400a0 as *const u32).read_volatile(), - (0x400400a8 as *const u32).read_volatile() - ); - - 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); - } - // Taken in from latest daric_util.c - let mut udmacore = CSR::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32); - udmacore.wo(utra::udma_ctrl::REG_CG, 0xFFFF_FFFF); - - crate::println!("Perclk solution: {:x}|{:x} -> {} MHz", min_cycle, fd, perclk / 1_000_000); - crate::println!("PLL configured to {} MHz", freq_hz / 1_000_000); - perclk -} - pub fn get_key(board_type: &BoardTypeCoding, iox: &T) -> Option { // check key press state. Depends on the board type match board_type { diff --git a/baremetal/src/platform/bao1x/bao1x.rs b/baremetal/src/platform/bao1x/bao1x.rs index 2505262b7..f99d96b9e 100644 --- a/baremetal/src/platform/bao1x/bao1x.rs +++ b/baremetal/src/platform/bao1x/bao1x.rs @@ -73,8 +73,16 @@ pub fn early_init() { // set the clock let fclk = SYSTEM_CLOCK_FREQUENCY; - let perclk = unsafe { init_clock_asic(fclk) }; - + let perclk = unsafe { + bao1x_hal::clocks::init_clock_asic( + fclk, + utra::sysctrl::HW_SYSCTRL_BASE, + utralib::HW_AO_SYSCTRL_BASE, + Some(utra::duart::HW_DUART_BASE), + delay_at_sysfreq, + true, + ) + }; // setup heap alloc setup_alloc(); @@ -182,268 +190,6 @@ mod panic_handler { } } -// This function supercedes init_clock_asic() and needs to be back-ported -// into xous-core -// TODO: -// - [ ] Case of clocks <= 48MHz: turn off PLL, divide directly from OSC -// - [ ] Derive clock dividers from freq targets, instead of hard-coding them -// - [ ] Maybe improve dividers to optimize hclk/iclk/pclk settings in lower power? -// - [ ] Very maybe consider setting hclk/iclk/pclk in case of overclocking -pub unsafe fn init_clock_asic(freq_hz: u32) -> u32 { - use utra::sysctrl; - let daric_cgu = sysctrl::HW_SYSCTRL_BASE as *mut u32; - let mut cgu = CSR::new(daric_cgu); - - const UNIT_MHZ: u32 = 1000 * 1000; - const PFD_F_MHZ: u32 = 16; - const FREQ_0: u32 = 16 * UNIT_MHZ; - const M: u32 = bao1x_api::FREQ_OSC_MHZ / PFD_F_MHZ; // - 1; // OSC input was 24, replace with 48 - - 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 - ]; - - // Safest divider settings, assuming no overclocking. - // If overclocking, need to lower hclk:iclk:pclk even futher; the CPU speed can outperform the bus fabric. - // 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(0x3f7f); // 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 - - // calculate perclk divider. Target 100MHz. - - // perclk divider - set to divide by 16 off of an 800Mhz base. Only found on bao1x. - // daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x03_ff_ff); - // perclk divider - set to divide by 8 off of an 800Mhz base. Only found on bao1x. - // TODO: this only works for two clock settings. Broken @ 600. Need to fix this to instead derive - // what pclk is instead of always reporting 100mhz - let (min_cycle, fd, perclk) = - if let Some((min_cycle, fd, perclk)) = bao1x_api::clk_to_per(freq_hz / 1_000_000, 100) { - daric_cgu - .add(utra::sysctrl::SFR_CGUFDPER.offset()) - .write_volatile((min_cycle as u32) << 16 | (fd as u32) << 8 | fd as u32); - (min_cycle, fd, perclk * 1_000_000) - } else if freq_hz > 400_000_000 { - daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x07_ff_ff); - (7, 0xff, freq_hz / 8) - } else { - daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x03_ff_ff); - (3, 0xff, 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(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 dividers - daric_cgu.add(utra::sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); - - if freq_hz > 700_000_000 { - crate::println!("setting vdd85 to 0.893v"); - let mut ao_sysctrl = CSR::new(utralib::HW_AO_SYSCTRL_BASE as *mut u32); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08421FF1); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM1CSR, 0x2); - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x57); - crate::platform::delay_at_sysfreq(20, 48_000_000); - } else if freq_hz > 350_000_000 { - crate::println!("setting vdd85 to 0.81v"); - let mut ao_sysctrl = CSR::new(utralib::HW_AO_SYSCTRL_BASE as *mut u32); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08421290); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM1CSR, 0x2); - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x57); - crate::platform::delay_at_sysfreq(20, 48_000_000); - } else { - crate::println!("setting vdd85 to 0.72v"); - let mut ao_sysctrl = CSR::new(utralib::HW_AO_SYSCTRL_BASE as *mut u32); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08420420); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM1CSR, 0x2); - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x57); - crate::platform::delay_at_sysfreq(20, 48_000_000); - } - crate::println!("...done"); - - // DARIC_CGU->cgusel1 = 1; // 0: RC, 1: XTAL - cgu.wo(sysctrl::SFR_CGUSEL1, 1); - // DARIC_CGU->cgufscr = FREQ_OSC_MHZ; // external crystal is 48MHz - cgu.wo(sysctrl::SFR_CGUFSCR, bao1x_api::FREQ_OSC_MHZ); - // __DSB(); - // DARIC_CGU->cguset = 0x32UL; - cgu.wo(sysctrl::SFR_CGUSET, 0x32); - // __DSB(); - - 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(bao1x_api::FREQ_OSC_MHZ); - duart.add(utra::duart::SFR_CR.offset()).write_volatile(1); - - if freq_hz <= 1_000_000 { - // DARIC_IPC->osc = freqHz; - cgu.wo(sysctrl::SFR_IPCOSC, freq_hz); - // __DSB(); - // DARIC_IPC->ar = 0x0032; // commit, must write 32 - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); - // __DSB(); - } - // switch to OSC - //DARIC_CGU->cgusel0 = 0; // clktop sel, 0:clksys, 1:clkpll0 - cgu.wo(sysctrl::SFR_CGUSEL0, 0); - // __DSB(); - // DARIC_CGU->cguset = 0x32; // commit - cgu.wo(sysctrl::SFR_CGUSET, 0x32); - //__DSB(); - - if freq_hz <= 1_000_000 { - } else { - let n_fxp24: u64; // fixed point - let f16mhz_log2: u32 = (freq_hz / FREQ_0).ilog2(); - - // PD PLL - // DARIC_IPC->lpen |= 0x02 ; - cgu.wo(sysctrl::SFR_IPCLPEN, cgu.r(sysctrl::SFR_IPCLPEN) | 0x2); - // __DSB(); - // DARIC_IPC->ar = 0x0032; // commit, must write 32 - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); - // __DSB(); - - // delay - // for (uint32_t i = 0; i < 1024; i++){ - // __NOP(); - //} - for _ in 0..1024 { - unsafe { core::arch::asm!("nop") }; - } - crate::println!("PLL delay 1"); - // why is this print needed for the code not to crash? - crate::println!("freq_hz {} log2 {}", freq_hz, f16mhz_log2); - 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; - - // TODO very verbose - //printf ("%s(%4" PRIu32 "MHz) M = %4" PRIu32 ", N = %4" PRIu32 ".%08" PRIu32 ", Q = %2lu, QDiv = - // 0x%04" PRIx16 "\n", __FUNCTION__, freqHz / 1000000, M, (uint32_t)(n_fxp24 >> 24), - // (uint32_t)((uint64_t)(n_fxp24 & 0x00ffffff) * 100000000/(1UL <<24)), TBL_MUL[f16MHzLog2], - // TBL_Q[f16MHzLog2]); DARIC_IPC->pll_mn = ((M << 12) & 0x0001F000) | ((n_fxp24 >> 24) & - // 0x00000fff); // 0x1F598; // ?? - 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_IPC->ipc = (1 << 6) | (2 << 3) | (3); - cgu.wo(sysctrl::SFR_IPCCR, (1 << 6) | (2 << 3) | (3)); - // __DSB(); - // DARIC_IPC->ar = 0x0032; // commit - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); - // __DSB(); - - // DARIC_IPC->lpen &= ~0x02; - cgu.wo(sysctrl::SFR_IPCLPEN, cgu.r(sysctrl::SFR_IPCLPEN) & !0x2); - - //__DSB(); - // DARIC_IPC->ar = 0x0032; // commit - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); - // __DSB(); - - // delay - // for (uint32_t i = 0; i < 1024; i++){ - // __NOP(); - // } - for _ in 0..1024 { - unsafe { core::arch::asm!("nop") }; - } - crate::println!("PLL delay 2"); - - //printf("read reg a0 : %08" PRIx32"\n", *((volatile uint32_t* )0x400400a0)); - //printf("read reg a4 : %04" PRIx16"\n", *((volatile uint16_t* )0x400400a4)); - //printf("read reg a8 : %04" PRIx16"\n", *((volatile uint16_t* )0x400400a8)); - - // 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"); - - // __DSB(); - - // printf (" MN: 0x%05x, F: 0x%06x, Q: 0x%04x\n", - // DARIC_IPC->pll_mn, DARIC_IPC->pll_f, DARIC_IPC->pll_q); - // printf (" LPEN: 0x%01x, OSC: 0x%04x, BIAS: 0x%04x,\n", - // DARIC_IPC->lpen, DARIC_IPC->osc, DARIC_IPC->ipc); - } - crate::println!( - "mn {:x}, q{:x}", - (0x400400a0 as *const u32).read_volatile(), - (0x400400a8 as *const u32).read_volatile() - ); - - 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); - } - // Taken in from latest daric_util.c - let mut udmacore = CSR::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32); - udmacore.wo(utra::udma_ctrl::REG_CG, 0xFFFF_FFFF); - - crate::println!("Perclk solution: {:x}|{:x} -> {} MHz", min_cycle, fd, perclk / 1_000_000); - crate::println!("PLL configured to {} MHz", freq_hz / 1_000_000); - perclk -} - /// used to generate some test vectors #[allow(dead_code)] pub fn lfsr_next_u32(state: u32) -> u32 { @@ -454,7 +200,16 @@ pub fn lfsr_next_u32(state: u32) -> u32 { pub fn clockset_wrapper(freq: u32) -> u32 { // reset the baud rate on the console UART - let perclk = unsafe { crate::platform::init_clock_asic(freq) }; + let perclk = unsafe { + bao1x_hal::clocks::init_clock_asic( + freq, + utra::sysctrl::HW_SYSCTRL_BASE, + utralib::HW_AO_SYSCTRL_BASE, + Some(utra::duart::HW_DUART_BASE), + delay_at_sysfreq, + true, + ) + }; let uart_buf_addr = crate::platform::UART_IFRAM_ADDR; #[cfg(feature = "bao1x-evb")] let mut udma_uart = unsafe { diff --git a/libs/bao1x-hal/src/lib.rs b/libs/bao1x-hal/src/lib.rs index 6f283e6f5..07e961ada 100644 --- a/libs/bao1x-hal/src/lib.rs +++ b/libs/bao1x-hal/src/lib.rs @@ -33,7 +33,11 @@ pub mod acram; #[cfg(not(feature = "hosted-baosec"))] pub mod buram; #[cfg(not(feature = "hosted-baosec"))] +pub mod clocks; +#[cfg(not(feature = "hosted-baosec"))] pub mod coreuser; +#[cfg(all(not(feature = "hosted-baosec"), not(feature = "kernel")))] +pub mod hardening; #[cfg(all(not(feature = "hosted-baosec"), feature = "std"))] pub mod kpc_aoint; #[cfg(not(feature = "hosted-baosec"))] @@ -42,13 +46,10 @@ pub mod mbox; pub mod rram; #[cfg(not(feature = "hosted-baosec"))] pub mod rtc; -#[cfg(not(feature = "hosted-baosec"))] -pub mod sram_trim; - -#[cfg(all(not(feature = "hosted-baosec"), not(feature = "kernel")))] -pub mod hardening; #[cfg(all(not(feature = "hosted-baosec"), not(feature = "kernel")))] pub mod sigcheck; +#[cfg(not(feature = "hosted-baosec"))] +pub mod sram_trim; #[inline(always)] pub fn cache_flush() { diff --git a/loader/src/platform/bao1x/bao1x.rs b/loader/src/platform/bao1x/bao1x.rs index 425835cb0..321a36343 100644 --- a/loader/src/platform/bao1x/bao1x.rs +++ b/loader/src/platform/bao1x/bao1x.rs @@ -168,7 +168,16 @@ pub fn early_init_hw() -> u32 { let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); // this needs to be set here because alpha-0 boot1 images set it to the wrong frequency - let perclk = unsafe { init_clock_asic(bao1x_api::dabao::DEFAULT_FCLK_FREQUENCY) }; + let perclk = unsafe { + bao1x_hal::clocks::init_clock_asic( + bao1x_api::dabao::DEFAULT_FCLK_FREQUENCY, + utra::sysctrl::HW_SYSCTRL_BASE, + utralib::HW_AO_SYSCTRL_BASE, + Some(utra::duart::HW_DUART_BASE), + delay_at_sysfreq, + true, + ) + }; // fixup the UART baud rate let _udma_uart = setup_console(&bao1x_api::BoardTypeCoding::Dabao, &iox, perclk); bao1x_hal::board::setup_console_pins(&iox); @@ -245,230 +254,6 @@ pub fn setup_console( udma_uart } -// returns the actual per_clk -#[cfg(not(feature = "simulation-only"))] -#[allow(dead_code)] -pub unsafe fn init_clock_asic(freq_hz: u32) -> u32 { - use utra::sysctrl; - let daric_cgu = sysctrl::HW_SYSCTRL_BASE as *mut u32; - let mut cgu = CSR::new(daric_cgu); - - const UNIT_MHZ: u32 = 1000 * 1000; - const PFD_F_MHZ: u32 = 16; - const FREQ_0: u32 = 16 * UNIT_MHZ; - const M: u32 = bao1x_api::FREQ_OSC_MHZ / PFD_F_MHZ; // - 1; // OSC input was 24, replace with 48 - - 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 - ]; - - // Safest divider settings, assuming no overclocking. - // If overclocking, need to lower hclk:iclk:pclk even futher; the CPU speed can outperform the bus fabric. - // 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(0x3f7f); // 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 - - // calculate perclk divider. Target 100MHz. - let (min_cycle, fd, perclk) = - if let Some((min_cycle, fd, perclk)) = bao1x_api::clk_to_per(freq_hz / 1_000_000, 100) { - daric_cgu - .add(utra::sysctrl::SFR_CGUFDPER.offset()) - .write_volatile((min_cycle as u32) << 16 | (fd as u32) << 8 | fd as u32); - (min_cycle, fd, perclk * 1_000_000) - } else if freq_hz > 400_000_000 { - daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x07_ff_ff); - (7, 0xff, freq_hz / 8) - } else { - daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x03_ff_ff); - (3, 0xff, 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(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 dividers - daric_cgu.add(utra::sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); - - // set voltage regulators to 0.893v. This is necessary because lp mode may set it lower. - if freq_hz > 700_000_000 { - crate::println!("setting vdd85 to 0.893v"); - let mut ao_sysctrl = CSR::new(utralib::HW_AO_SYSCTRL_BASE as *mut u32); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08421FF1); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM1CSR, 0x2); - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x57); - crate::platform::delay_at_sysfreq(20, 48_000_000); - } else if freq_hz > 350_000_000 { - crate::println!("setting vdd85 to 0.81v"); - let mut ao_sysctrl = CSR::new(utralib::HW_AO_SYSCTRL_BASE as *mut u32); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08421290); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM1CSR, 0x2); - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x57); - crate::platform::delay_at_sysfreq(20, 48_000_000); - } else { - crate::println!("setting vdd85 to 0.72v"); - let mut ao_sysctrl = CSR::new(utralib::HW_AO_SYSCTRL_BASE as *mut u32); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08420420); - ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM1CSR, 0x2); - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x57); - crate::platform::delay_at_sysfreq(20, 48_000_000); - } - crate::println!("...done"); - - cgu.wo(sysctrl::SFR_CGUSEL1, 1); - cgu.wo(sysctrl::SFR_CGUFSCR, bao1x_api::FREQ_OSC_MHZ); - cgu.wo(sysctrl::SFR_CGUSET, 0x32); - - 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(bao1x_api::FREQ_OSC_MHZ); - duart.add(utra::duart::SFR_CR.offset()).write_volatile(1); - - if freq_hz <= 1_000_000 { - cgu.wo(sysctrl::SFR_IPCOSC, freq_hz); - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); - } - // switch to OSC - // clktop sel, 0:clksys, 1:clkpll0 - cgu.wo(sysctrl::SFR_CGUSEL0, 0); - // commit - cgu.wo(sysctrl::SFR_CGUSET, 0x32); - - if freq_hz <= 1_000_000 { - } else { - 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 { - unsafe { core::arch::asm!("nop") }; - } - crate::println!("PLL delay 1"); - // why is this print needed for the code not to crash? - crate::println!("freq_hz {} log2 {}", freq_hz, f16mhz_log2); - 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_IPC->ipc = (1 << 6) | (2 << 3) | (3); - cgu.wo(sysctrl::SFR_IPCCR, (1 << 6) | (2 << 3) | (3)); - // commit - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); - - cgu.wo(sysctrl::SFR_IPCLPEN, cgu.r(sysctrl::SFR_IPCLPEN) & !0x2); - - // commit - cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32); - // __DSB(); - - // delay - for _ in 0..1024 { - unsafe { core::arch::asm!("nop") }; - } - crate::println!("PLL delay 2"); - - // 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!( - "mn {:x}, q{:x}", - (0x400400a0 as *const u32).read_volatile(), - (0x400400a8 as *const u32).read_volatile() - ); - - 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); - } - // Taken in from latest daric_util.c - let mut udmacore = CSR::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32); - udmacore.wo(utra::udma_ctrl::REG_CG, 0xFFFF_FFFF); - - crate::println!("Perclk solution: {:x}|{:x} -> {} MHz", min_cycle, fd, perclk / 1_000_000); - crate::println!("PLL configured to {} MHz", freq_hz / 1_000_000); - perclk -} - -#[allow(dead_code)] -fn fsfreq_to_hz(fs_freq: u32) -> u32 { (fs_freq * (48_000_000 / 32)) / 1_000_000 } - -#[allow(dead_code)] -fn fsfreq_to_hz_32(fs_freq: u32) -> u32 { (fs_freq * (32_000_000 / 32)) / 1_000_000 } - #[allow(dead_code)] #[cfg(feature = "bao1x")] /// Used mainly for debug breaks. Not used in every configuration. From 41d09786aecac6b80f522d03626cf322a6cc89ba Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Dec 2025 16:54:23 +0800 Subject: [PATCH 3/3] tighten up clock gates in boot0 confirmed that these settings do not affect the glitch sensors. --- bao1x-boot/boot0/src/platform/bao1x/bao1x.rs | 12 ++++++------ libs/bao1x-hal/src/clocks.rs | 3 --- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/bao1x-boot/boot0/src/platform/bao1x/bao1x.rs b/bao1x-boot/boot0/src/platform/bao1x/bao1x.rs index bef13a2a3..f0f92df91 100644 --- a/bao1x-boot/boot0/src/platform/bao1x/bao1x.rs +++ b/bao1x-boot/boot0/src/platform/bao1x/bao1x.rs @@ -362,10 +362,10 @@ pub unsafe fn init_clock_asic_350mhz() -> u32 { let perclk = 175000000; // turn off gates - 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); + daric_cgu.add(utra::sysctrl::SFR_ACLKGR.offset()).write_volatile(0x00); // mbox/qfc turned off + daric_cgu.add(utra::sysctrl::SFR_HCLKGR.offset()).write_volatile(0x02); // mdma off, sce on + daric_cgu.add(utra::sysctrl::SFR_ICLKGR.offset()).write_volatile(0x90); // bio/udc enable + daric_cgu.add(utra::sysctrl::SFR_PCLKGR.offset()).write_volatile(0x80); // enable mesh // commit dividers daric_cgu.add(utra::sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); @@ -427,9 +427,9 @@ pub unsafe fn init_clock_asic_350mhz() -> u32 { cgu.wo(sysctrl::SFR_CGUSET, 0x32); bao1x_api::bollard!(6); - // Taken in from latest daric_util.c + // UDMA subsystem controls its own gates let mut udmacore = CSR::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32); - udmacore.wo(utra::udma_ctrl::REG_CG, 0xFFFF_FFFF); + udmacore.wo(utra::udma_ctrl::REG_CG, 0x0); // glitch_safety: check that we're running on the PLL bao1x_hal::hardening::check_pll(); diff --git a/libs/bao1x-hal/src/clocks.rs b/libs/bao1x-hal/src/clocks.rs index cf3fd0d20..d0e1295ad 100644 --- a/libs/bao1x-hal/src/clocks.rs +++ b/libs/bao1x-hal/src/clocks.rs @@ -220,9 +220,6 @@ pub unsafe fn init_clock_asic( let fsfreq = (daric_cgu.add(offset).read_volatile() >> shift) & 0xffff; crate::println!("{}: {} MHz", name, fsfreq); } - // Comment this out - I think we manage the UDMA clocks explicitly per-driver - // let mut udmacore = CSR::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32); - // udmacore.wo(utra::udma_ctrl::REG_CG, 0xFFFF_FFFF); crate::println!("Perclk solution: {:x}|{:x} -> {} MHz", min_cycle, fd, perclk / 1_000_000); crate::println!("PLL configured to {} MHz", freq_hz / 1_000_000);