2019-06-14 15:26:30 +08:00
|
|
|
use board_misoc::config;
|
|
|
|
use board_artiq::si5324;
|
2020-04-06 22:34:05 +08:00
|
|
|
use board_misoc::{csr, clock};
|
2019-06-14 15:26:30 +08:00
|
|
|
|
2021-10-28 16:21:51 +08:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
#[allow(non_camel_case_types)]
|
2019-06-14 15:26:30 +08:00
|
|
|
pub enum RtioClock {
|
2021-10-28 16:21:51 +08:00
|
|
|
Default,
|
|
|
|
Int_125,
|
|
|
|
Int_100,
|
|
|
|
Ext0_Bypass,
|
|
|
|
Ext0_Synth0_10to125,
|
2023-04-13 11:06:53 +08:00
|
|
|
Ext0_Synth0_80to125,
|
2021-10-28 16:21:51 +08:00
|
|
|
Ext0_Synth0_100to125,
|
|
|
|
Ext0_Synth0_125to125,
|
2019-06-14 15:26:30 +08:00
|
|
|
}
|
|
|
|
|
2021-10-28 16:21:51 +08:00
|
|
|
#[allow(unreachable_code)]
|
2019-06-14 15:26:30 +08:00
|
|
|
fn get_rtio_clock_cfg() -> RtioClock {
|
2021-10-28 16:21:51 +08:00
|
|
|
config::read_str("rtio_clock", |result| {
|
|
|
|
let res = match result {
|
|
|
|
Ok("int_125") => RtioClock::Int_125,
|
|
|
|
Ok("int_100") => RtioClock::Int_100,
|
|
|
|
Ok("ext0_bypass") => RtioClock::Ext0_Bypass,
|
|
|
|
Ok("ext0_bypass_125") => RtioClock::Ext0_Bypass,
|
|
|
|
Ok("ext0_bypass_100") => RtioClock::Ext0_Bypass,
|
|
|
|
Ok("ext0_synth0_10to125") => RtioClock::Ext0_Synth0_10to125,
|
2023-04-13 11:06:53 +08:00
|
|
|
Ok("ext0_synth0_80to125") => RtioClock::Ext0_Synth0_80to125,
|
2021-10-28 16:21:51 +08:00
|
|
|
Ok("ext0_synth0_100to125") => RtioClock::Ext0_Synth0_100to125,
|
|
|
|
Ok("ext0_synth0_125to125") => RtioClock::Ext0_Synth0_125to125,
|
|
|
|
Ok("i") => {
|
|
|
|
warn!("Using legacy rtio_clock setting ('i'). Falling back to default. This will be deprecated.");
|
|
|
|
RtioClock::Default
|
2019-06-14 15:26:30 +08:00
|
|
|
},
|
2021-10-28 16:21:51 +08:00
|
|
|
Ok("e") => {
|
|
|
|
warn!("Using legacy rtio_clock setting ('e'). This will be deprecated.");
|
|
|
|
RtioClock::Ext0_Bypass
|
2019-06-14 15:26:30 +08:00
|
|
|
},
|
|
|
|
_ => {
|
2021-10-28 16:21:51 +08:00
|
|
|
warn!("rtio_clock setting not recognised. Falling back to default.");
|
|
|
|
RtioClock::Default
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if res == RtioClock::Default {
|
|
|
|
#[cfg(any(si5324_ext_ref, ext_ref_frequency))]
|
|
|
|
warn!("si5324_ext_ref and ext_ref_frequency compile-time options are deprecated. Please use the rtio_clock coreconfig settings instead.");
|
|
|
|
#[cfg(all(rtio_frequency = "125.0", si5324_ext_ref, ext_ref_frequency = "10.0"))]
|
|
|
|
return RtioClock::Ext0_Synth0_10to125;
|
2023-04-13 11:06:53 +08:00
|
|
|
#[cfg(all(rtio_frequency = "125.0", si5324_ext_ref, ext_ref_frequency = "80.0"))]
|
|
|
|
return RtioClock::Ext0_Synth0_80to125;
|
2021-10-28 16:21:51 +08:00
|
|
|
#[cfg(all(rtio_frequency = "125.0", si5324_ext_ref, ext_ref_frequency = "100.0"))]
|
|
|
|
return RtioClock::Ext0_Synth0_100to125;
|
|
|
|
#[cfg(all(rtio_frequency = "125.0", si5324_ext_ref, ext_ref_frequency = "125.0"))]
|
|
|
|
return RtioClock::Ext0_Synth0_125to125;
|
|
|
|
#[cfg(all(rtio_frequency = "125.0", not(si5324_ext_ref)))]
|
|
|
|
return RtioClock::Int_125;
|
2022-10-28 17:31:34 +08:00
|
|
|
#[cfg(all(rtio_frequency = "100.0", not(si5324_ext_ref), not(soc_platform = "kasli")))]
|
2021-10-28 16:21:51 +08:00
|
|
|
return RtioClock::Int_100;
|
|
|
|
//in case nothing is set
|
|
|
|
return RtioClock::Int_125;
|
2019-06-14 15:26:30 +08:00
|
|
|
}
|
2021-10-28 16:21:51 +08:00
|
|
|
res
|
|
|
|
})
|
|
|
|
|
2019-06-14 15:26:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(has_rtio_crg)]
|
|
|
|
pub mod crg {
|
|
|
|
use board_misoc::{clock, csr};
|
|
|
|
|
|
|
|
pub fn check() -> bool {
|
|
|
|
unsafe { csr::rtio_crg::pll_locked_read() != 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init() -> bool {
|
2021-10-28 16:21:51 +08:00
|
|
|
info!("Using internal RTIO clock");
|
2019-06-14 15:26:30 +08:00
|
|
|
unsafe {
|
|
|
|
csr::rtio_crg::pll_reset_write(0);
|
|
|
|
}
|
|
|
|
clock::spin_us(150);
|
|
|
|
return check()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(has_rtio_crg))]
|
|
|
|
pub mod crg {
|
|
|
|
pub fn check() -> bool { true }
|
|
|
|
}
|
|
|
|
|
2022-12-04 12:00:48 +08:00
|
|
|
// Si5324 input to select for locking to an external clock (as opposed to
|
|
|
|
// a recovered link clock in DRTIO satellites, which is handled elsewhere).
|
2023-01-12 13:01:08 +08:00
|
|
|
#[cfg(all(soc_platform = "kasli", hw_rev = "v2.0"))]
|
2022-12-04 12:00:48 +08:00
|
|
|
const SI5324_EXT_INPUT: si5324::Input = si5324::Input::Ckin1;
|
2023-01-12 13:01:08 +08:00
|
|
|
#[cfg(all(soc_platform = "kasli", not(hw_rev = "v2.0")))]
|
2022-12-04 12:00:48 +08:00
|
|
|
const SI5324_EXT_INPUT: si5324::Input = si5324::Input::Ckin2;
|
2023-01-12 13:01:08 +08:00
|
|
|
#[cfg(all(soc_platform = "kc705"))]
|
2022-12-04 12:00:48 +08:00
|
|
|
const SI5324_EXT_INPUT: si5324::Input = si5324::Input::Ckin2;
|
|
|
|
|
2023-01-12 13:01:08 +08:00
|
|
|
fn setup_si5324_pll(cfg: RtioClock) {
|
2022-12-17 10:06:22 +08:00
|
|
|
let (si5324_settings, si5324_ref_input) = match cfg {
|
2021-10-28 16:21:51 +08:00
|
|
|
RtioClock::Ext0_Synth0_10to125 => { // 125 MHz output from 10 MHz CLKINx reference, 504 Hz BW
|
|
|
|
info!("using 10MHz reference to make 125MHz RTIO clock with PLL");
|
2022-12-17 10:06:22 +08:00
|
|
|
(
|
|
|
|
si5324::FrequencySettings {
|
|
|
|
n1_hs : 10,
|
|
|
|
nc1_ls : 4,
|
|
|
|
n2_hs : 10,
|
|
|
|
n2_ls : 300,
|
|
|
|
n31 : 6,
|
|
|
|
n32 : 6,
|
|
|
|
bwsel : 4,
|
2022-12-17 10:09:20 +08:00
|
|
|
crystal_as_ckin2: false
|
2022-12-17 10:06:22 +08:00
|
|
|
},
|
|
|
|
SI5324_EXT_INPUT
|
|
|
|
)
|
2021-10-28 16:21:51 +08:00
|
|
|
},
|
2023-04-13 11:06:53 +08:00
|
|
|
RtioClock::Ext0_Synth0_80to125 => { // 125 MHz output from 80 MHz CLKINx reference, 611 Hz BW
|
|
|
|
info!("using 80MHz reference to make 125MHz RTIO clock with PLL");
|
|
|
|
(
|
|
|
|
si5324::FrequencySettings {
|
|
|
|
n1_hs : 4,
|
|
|
|
nc1_ls : 10,
|
|
|
|
n2_hs : 10,
|
|
|
|
n2_ls : 250,
|
|
|
|
n31 : 40,
|
|
|
|
n32 : 40,
|
|
|
|
bwsel : 4,
|
|
|
|
crystal_as_ckin2: false
|
|
|
|
},
|
|
|
|
SI5324_EXT_INPUT
|
|
|
|
)
|
|
|
|
},
|
2021-10-28 16:21:51 +08:00
|
|
|
RtioClock::Ext0_Synth0_100to125 => { // 125MHz output, from 100MHz CLKINx reference, 586 Hz loop bandwidth
|
2022-12-02 09:37:56 +08:00
|
|
|
info!("using 100MHz reference to make 125MHz RTIO clock with PLL");
|
2022-12-17 10:06:22 +08:00
|
|
|
(
|
|
|
|
si5324::FrequencySettings {
|
|
|
|
n1_hs : 10,
|
|
|
|
nc1_ls : 4,
|
|
|
|
n2_hs : 10,
|
|
|
|
n2_ls : 260,
|
|
|
|
n31 : 52,
|
|
|
|
n32 : 52,
|
|
|
|
bwsel : 4,
|
2022-12-17 10:09:20 +08:00
|
|
|
crystal_as_ckin2: false
|
2022-12-17 10:06:22 +08:00
|
|
|
},
|
|
|
|
SI5324_EXT_INPUT
|
|
|
|
)
|
2021-10-28 16:21:51 +08:00
|
|
|
},
|
|
|
|
RtioClock::Ext0_Synth0_125to125 => { // 125MHz output, from 125MHz CLKINx reference, 606 Hz loop bandwidth
|
2022-12-02 09:37:56 +08:00
|
|
|
info!("using 125MHz reference to make 125MHz RTIO clock with PLL");
|
2022-12-17 10:06:22 +08:00
|
|
|
(
|
|
|
|
si5324::FrequencySettings {
|
|
|
|
n1_hs : 5,
|
|
|
|
nc1_ls : 8,
|
|
|
|
n2_hs : 7,
|
|
|
|
n2_ls : 360,
|
|
|
|
n31 : 63,
|
|
|
|
n32 : 63,
|
|
|
|
bwsel : 4,
|
2022-12-17 10:09:20 +08:00
|
|
|
crystal_as_ckin2: false
|
2022-12-17 10:06:22 +08:00
|
|
|
},
|
|
|
|
SI5324_EXT_INPUT
|
|
|
|
)
|
2021-10-28 16:21:51 +08:00
|
|
|
},
|
2022-12-17 10:06:22 +08:00
|
|
|
RtioClock::Int_100 => { // 100MHz output, from crystal
|
2021-10-28 16:21:51 +08:00
|
|
|
info!("using internal 100MHz RTIO clock");
|
2022-12-17 10:06:22 +08:00
|
|
|
(
|
|
|
|
si5324::FrequencySettings {
|
|
|
|
n1_hs : 9,
|
|
|
|
nc1_ls : 6,
|
|
|
|
n2_hs : 10,
|
|
|
|
n2_ls : 33732,
|
|
|
|
n31 : 7139,
|
|
|
|
n32 : 7139,
|
|
|
|
bwsel : 3,
|
2022-12-17 10:09:20 +08:00
|
|
|
crystal_as_ckin2: true
|
2022-12-17 10:06:22 +08:00
|
|
|
},
|
|
|
|
si5324::Input::Ckin2
|
|
|
|
)
|
2021-10-28 16:21:51 +08:00
|
|
|
},
|
2021-10-28 16:34:22 +08:00
|
|
|
RtioClock::Int_125 => { // 125MHz output, from crystal, 7 Hz
|
|
|
|
info!("using internal 125MHz RTIO clock");
|
2022-12-17 10:06:22 +08:00
|
|
|
(
|
|
|
|
si5324::FrequencySettings {
|
|
|
|
n1_hs : 10,
|
|
|
|
nc1_ls : 4,
|
|
|
|
n2_hs : 10,
|
|
|
|
n2_ls : 19972,
|
|
|
|
n31 : 4565,
|
|
|
|
n32 : 4565,
|
|
|
|
bwsel : 4,
|
2022-12-17 10:09:20 +08:00
|
|
|
crystal_as_ckin2: true
|
2022-12-17 10:06:22 +08:00
|
|
|
},
|
|
|
|
si5324::Input::Ckin2
|
|
|
|
)
|
|
|
|
},
|
2021-10-28 16:34:22 +08:00
|
|
|
_ => { // 125MHz output like above, default (if chosen option is not supported)
|
|
|
|
warn!("rtio_clock setting '{:?}' is not supported. Falling back to default internal 125MHz RTIO clock.", cfg);
|
2022-12-17 10:06:22 +08:00
|
|
|
(
|
|
|
|
si5324::FrequencySettings {
|
|
|
|
n1_hs : 10,
|
|
|
|
nc1_ls : 4,
|
|
|
|
n2_hs : 10,
|
|
|
|
n2_ls : 19972,
|
|
|
|
n31 : 4565,
|
|
|
|
n32 : 4565,
|
|
|
|
bwsel : 4,
|
2022-12-17 10:09:20 +08:00
|
|
|
crystal_as_ckin2: true
|
2022-12-17 10:06:22 +08:00
|
|
|
},
|
|
|
|
si5324::Input::Ckin2
|
|
|
|
)
|
2021-10-28 16:21:51 +08:00
|
|
|
}
|
2019-06-14 15:26:30 +08:00
|
|
|
};
|
2022-12-17 10:06:22 +08:00
|
|
|
si5324::setup(&si5324_settings, si5324_ref_input).expect("cannot initialize Si5324");
|
2019-06-14 15:26:30 +08:00
|
|
|
}
|
|
|
|
|
2022-10-28 17:31:34 +08:00
|
|
|
fn setup_si5324(clock_cfg: RtioClock) {
|
2022-12-17 15:39:54 +08:00
|
|
|
let switched = unsafe {
|
|
|
|
csr::crg::switch_done_read()
|
|
|
|
};
|
|
|
|
if switched == 1 {
|
|
|
|
info!("Clocking has already been set up.");
|
|
|
|
return;
|
2022-10-28 17:31:34 +08:00
|
|
|
}
|
|
|
|
match clock_cfg {
|
|
|
|
RtioClock::Ext0_Bypass => {
|
|
|
|
info!("using external RTIO clock with PLL bypass");
|
2023-01-12 12:58:19 +08:00
|
|
|
si5324::bypass(SI5324_EXT_INPUT).expect("cannot bypass Si5324")
|
2022-10-28 17:31:34 +08:00
|
|
|
},
|
2023-01-12 13:01:08 +08:00
|
|
|
_ => setup_si5324_pll(clock_cfg),
|
2022-10-28 17:31:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// switch sysclk source to si5324
|
2022-12-17 15:39:54 +08:00
|
|
|
#[cfg(not(has_drtio))]
|
2019-06-14 15:48:05 +08:00
|
|
|
{
|
2022-12-17 15:39:54 +08:00
|
|
|
info!("Switching sys clock, rebooting...");
|
|
|
|
// delay for clean UART log, wait until UART FIFO is empty
|
|
|
|
clock::spin_us(1300);
|
2022-10-28 17:31:34 +08:00
|
|
|
unsafe {
|
|
|
|
csr::crg::clock_sel_write(1);
|
2022-12-17 15:39:54 +08:00
|
|
|
loop {}
|
2019-06-14 15:48:05 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-28 17:31:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn init() {
|
|
|
|
let clock_cfg = get_rtio_clock_cfg();
|
|
|
|
setup_si5324(clock_cfg);
|
2019-06-14 15:26:30 +08:00
|
|
|
|
|
|
|
#[cfg(has_drtio)]
|
2020-04-10 17:23:17 +08:00
|
|
|
{
|
2022-12-17 15:39:54 +08:00
|
|
|
let switched = unsafe {
|
|
|
|
csr::crg::switch_done_read()
|
|
|
|
};
|
|
|
|
if switched == 0 {
|
|
|
|
info!("Switching sys clock, rebooting...");
|
2023-11-03 10:24:54 +08:00
|
|
|
clock::spin_us(3000); // delay for clean UART log
|
2022-12-17 15:39:54 +08:00
|
|
|
unsafe {
|
|
|
|
// clock switch and reboot will begin after TX is initialized
|
|
|
|
// and TX will be initialized after this
|
2023-08-28 12:50:46 +08:00
|
|
|
csr::gt_drtio::stable_clkin_write(1);
|
2022-12-17 15:39:54 +08:00
|
|
|
}
|
|
|
|
loop {}
|
2020-04-10 17:23:17 +08:00
|
|
|
}
|
2022-12-17 15:39:54 +08:00
|
|
|
else {
|
|
|
|
// enable TX after the reboot, with stable clock
|
|
|
|
unsafe {
|
2023-08-28 12:50:46 +08:00
|
|
|
csr::gt_drtio::txenable_write(0xffffffffu32 as _);
|
2023-09-05 16:58:28 +08:00
|
|
|
|
|
|
|
#[cfg(has_drtio_eem)]
|
|
|
|
csr::eem_transceiver::txenable_write(0xffffffffu32 as _);
|
2022-12-17 15:39:54 +08:00
|
|
|
}
|
2020-04-10 17:23:17 +08:00
|
|
|
}
|
2020-04-06 22:34:05 +08:00
|
|
|
}
|
2019-06-14 15:26:30 +08:00
|
|
|
|
2022-12-17 15:39:54 +08:00
|
|
|
|
2019-06-14 15:26:30 +08:00
|
|
|
#[cfg(has_rtio_crg)]
|
|
|
|
{
|
2019-06-24 23:33:13 +08:00
|
|
|
let result = crg::init();
|
2019-06-14 15:29:16 +08:00
|
|
|
if !result {
|
|
|
|
error!("RTIO clock failed");
|
2019-06-14 15:26:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|