mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
improve clock divider setting to handle a wide range of clocks
This commit is contained in:
parent
cd58323541
commit
13b048089b
1 changed files with 100 additions and 89 deletions
|
|
@ -308,8 +308,56 @@ mod panic_handler {
|
|||
}
|
||||
}
|
||||
|
||||
/// This takes in the FD input frequency (the frequency to be divided) in MHz
|
||||
/// and the fd value, and returns the resulting divided frequency.
|
||||
/// *not tested*
|
||||
#[allow(dead_code)]
|
||||
pub fn fd_to_clk(fd_in_mhz: u32, fd_val: u32) -> u32 { (fd_in_mhz * (fd_val + 1)) / 256 }
|
||||
|
||||
/// Takes in the FD input frequencyin MHz, and then the desired frequency.
|
||||
/// Returns Some((fd value, deviation in *hz*, not MHz)) if the requirement is satisfiable
|
||||
/// Returns None if the equation is ill-formed.
|
||||
/// *not tested*
|
||||
#[allow(dead_code)]
|
||||
pub fn clk_to_fd(fd_in_mhz: u32, desired_mhz: u32) -> Option<(u32, i32)> {
|
||||
let platonic_fd: u32 = (desired_mhz * 256) / fd_in_mhz;
|
||||
if platonic_fd > 0 {
|
||||
let actual_fd = platonic_fd - 1;
|
||||
let actual_clk = fd_to_clk(fd_in_mhz, actual_fd);
|
||||
Some((actual_fd, desired_mhz as i32 - actual_clk as i32))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes in the top clock in MHz, desired perclk in MHz, and returns a tuple of
|
||||
/// (min cycle, fd, actual freq)
|
||||
/// *tested*
|
||||
pub fn clk_to_per(top_in_mhz: u32, perclk_in_mhz: u32) -> Option<(u8, u8, u32)> {
|
||||
let fd_platonic = ((256 * perclk_in_mhz) / (top_in_mhz / 2)).max(256);
|
||||
if fd_platonic > 0 {
|
||||
let fd = fd_platonic - 1;
|
||||
let min_cycle = (2 * (256 / (fd + 1))).max(1);
|
||||
let min_freq = top_in_mhz / min_cycle;
|
||||
let target_freq = top_in_mhz * (fd + 1) / 512;
|
||||
let actual_freq = target_freq.max(min_freq);
|
||||
if fd < 256 && min_cycle < 256 && min_cycle > 0 {
|
||||
Some(((min_cycle - 1) as u8, fd as u8, actual_freq))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
|
@ -341,83 +389,60 @@ pub unsafe fn init_clock_asic(freq_hz: u32) -> u32 {
|
|||
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(0x3f7f); // fclk
|
||||
// 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
|
||||
|
||||
// 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
|
||||
// perclk divider - set to divide by 16 off of an 800Mhz base. Only found on NTO.
|
||||
// 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 NTO.
|
||||
// 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 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
|
||||
};
|
||||
// calculate perclk divider. Target 100MHz.
|
||||
|
||||
/*
|
||||
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);
|
||||
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
|
||||
// perclk divider - set to divide by 16 off of an 800Mhz base. Only found on NTO.
|
||||
// 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 NTO.
|
||||
// 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)) = 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)
|
||||
};
|
||||
|
||||
/*
|
||||
if (0 == (cgu.r(sysctrl::SFR_IPCPLLMN) & 0x0001F000))
|
||||
|| (0 == (cgu.r(sysctrl::SFR_IPCPLLMN) & 0x00000fff))
|
||||
{
|
||||
// for SIM, avoid div by 0 if unconfigurated
|
||||
// , default VCO 48MHz / 48 * 1200 = 1.2GHz
|
||||
// TODO magic numbers
|
||||
cgu.wo(sysctrl::SFR_IPCPLLMN, ((M << 12) & 0x0001F000) | ((1200) & 0x00000fff));
|
||||
cgu.wo(sysctrl::SFR_IPCPLLF, 0);
|
||||
cgu.wo(sysctrl::SFR_IPCARIPFLOW, 0x32);
|
||||
}
|
||||
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
|
||||
*/
|
||||
for _ in 0..100 {
|
||||
crate::print!("1");
|
||||
}
|
||||
crate::println!(".");
|
||||
|
||||
// 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);
|
||||
|
||||
// TODO select int/ext osc/xtal
|
||||
// DARIC_CGU->cgusel1 = 1; // 0: RC, 1: XTAL
|
||||
|
|
@ -435,12 +460,7 @@ pub unsafe fn init_clock_asic(freq_hz: u32) -> u32 {
|
|||
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");
|
||||
}
|
||||
crate::println!(".");
|
||||
|
||||
if freq_hz < 1000000 {
|
||||
if freq_hz <= 48_000_000 {
|
||||
// DARIC_IPC->osc = freqHz;
|
||||
cgu.wo(sysctrl::SFR_IPCOSC, freq_hz);
|
||||
// __DSB();
|
||||
|
|
@ -456,21 +476,11 @@ pub unsafe fn init_clock_asic(freq_hz: u32) -> u32 {
|
|||
cgu.wo(sysctrl::SFR_CGUSET, 0x32);
|
||||
//__DSB();
|
||||
|
||||
for _ in 0..100 {
|
||||
crate::print!("3");
|
||||
}
|
||||
crate::println!(".");
|
||||
|
||||
if freq_hz < 1000000 {
|
||||
if freq_hz <= 48_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
|
||||
// DARIC_IPC->lpen |= 0x02 ;
|
||||
cgu.wo(sysctrl::SFR_IPCLPEN, cgu.r(sysctrl::SFR_IPCLPEN) | 0x2);
|
||||
|
|
@ -575,7 +585,8 @@ pub unsafe fn init_clock_asic(freq_hz: u32) -> u32 {
|
|||
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!("PLL configured to {} MHz; perclk {}", freq_hz / 1_000_000, perclk);
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue