mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
update APIs to use the central clocking implementation
This commit is contained in:
parent
11b506bdc4
commit
a3f9a9942d
4 changed files with 48 additions and 751 deletions
|
|
@ -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::<u32>()),
|
||||
("pke", 0, 0x40 / size_of::<u32>()),
|
||||
("ao", 16, 0x44 / size_of::<u32>()),
|
||||
("aoram", 0, 0x44 / size_of::<u32>()),
|
||||
("osc", 16, 0x48 / size_of::<u32>()),
|
||||
("xtal", 0, 0x48 / size_of::<u32>()),
|
||||
("pll0", 16, 0x4c / size_of::<u32>()),
|
||||
("pll1", 0, 0x4c / size_of::<u32>()),
|
||||
];
|
||||
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<T: IoSetup + IoGpio>(board_type: &BoardTypeCoding, iox: &T) -> Option<KeyPress> {
|
||||
// check key press state. Depends on the board type
|
||||
match board_type {
|
||||
|
|
|
|||
|
|
@ -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::<u32>()),
|
||||
("pke", 0, 0x40 / size_of::<u32>()),
|
||||
("ao", 16, 0x44 / size_of::<u32>()),
|
||||
("aoram", 0, 0x44 / size_of::<u32>()),
|
||||
("osc", 16, 0x48 / size_of::<u32>()),
|
||||
("xtal", 0, 0x48 / size_of::<u32>()),
|
||||
("pll0", 16, 0x4c / size_of::<u32>()),
|
||||
("pll1", 0, 0x4c / size_of::<u32>()),
|
||||
];
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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<T: IoSetup + IoGpio>(
|
|||
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::<u32>()),
|
||||
("pke", 0, 0x40 / size_of::<u32>()),
|
||||
("ao", 16, 0x44 / size_of::<u32>()),
|
||||
("aoram", 0, 0x44 / size_of::<u32>()),
|
||||
("osc", 16, 0x48 / size_of::<u32>()),
|
||||
("xtal", 0, 0x48 / size_of::<u32>()),
|
||||
("pll0", 16, 0x4c / size_of::<u32>()),
|
||||
("pll1", 0, 0x4c / size_of::<u32>()),
|
||||
];
|
||||
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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue