WRPLL firmware

satman main & si549: add WRPLL select_recovered_clock
satman main & si549: add helper si549 & interrupt setup
si549: add tag collector to process gtx & main tags
si549: add frequency counter to set BASE_ADPLL
si549: add set_adpll for main & helper PLL
si549: add main & helper PLL
IRQ & si549: add handler for gtx & main tags irq
IRQ: add docs for IRQ id
morgan 2024-01-04 15:46:59 +08:00
parent 05f80b579f
commit ec506fd0ce
3 changed files with 354 additions and 9 deletions

View File

@ -284,3 +284,310 @@ pub fn main_setup(timer: &mut GlobalTimer) -> Result<(), &'static str> {
info!("Main Si549 started");
Ok(())
}
#[cfg(has_wrpll)]
pub mod wrpll {
use libboard_zynq::gic;
use libcortex_a9::mutex::Mutex;
use super::*;
const IRQ_ID: [u8; 2] = [61, 62];
const TIMER_WIDTH: u32 = 24;
const COUNTER_DIV: u32 = 2;
const ADPLL_MAX: i32 = (950.0 / 0.0001164) as i32;
static BASE_ADPLL: Mutex<i32> = Mutex::new(0);
static H_INTEGRATOR: Mutex<i32> = Mutex::new(0);
static M_INTEGRATOR: Mutex<i32> = Mutex::new(0);
#[derive(Clone, Copy)]
pub enum IRQ {
GTXTag,
MainTag,
}
mod tag_collector {
use super::*;
const BEATING_PERIOD: i32 = 0x4000;
const BEATING_HALFPERIOD: i32 = 0x2000;
// for helper PLL
static LAST_GTX_TAG: Mutex<u32> = Mutex::new(0);
static FIRST_GTX_INTERRUPT: Mutex<bool> = Mutex::new(false);
static PERIOD_DET_READY: Mutex<bool> = Mutex::new(false);
// for main PLL
static GTX_TAG: Mutex<u32> = Mutex::new(0);
static GTX_TAG_READY: Mutex<bool> = Mutex::new(false);
static MAIN_TAG: Mutex<u32> = Mutex::new(0);
static MAIN_TAG_READY: Mutex<bool> = Mutex::new(false);
pub fn reset() {
clear_period_det_ready();
clear_phase_det_ready();
*LAST_GTX_TAG.lock() = 0;
*FIRST_GTX_INTERRUPT.lock() = true;
*GTX_TAG.lock() = 0;
*MAIN_TAG.lock() = 0;
}
pub fn clear_period_det_ready() {
*PERIOD_DET_READY.lock() = false;
}
pub fn clear_phase_det_ready() {
*GTX_TAG_READY.lock() = false;
*MAIN_TAG_READY.lock() = false;
}
pub fn collect_tags(interrupt: IRQ) {
match interrupt {
IRQ::GTXTag => {
let mut gtx_tag_lock = GTX_TAG.lock();
if !(*FIRST_GTX_INTERRUPT.lock()) {
*LAST_GTX_TAG.lock() = *gtx_tag_lock;
}
*gtx_tag_lock = unsafe { csr::wrpll::gtx_tag_read() };
*FIRST_GTX_INTERRUPT.lock() = false;
*GTX_TAG_READY.lock() = true;
}
IRQ::MainTag => {
*MAIN_TAG.lock() = unsafe { csr::wrpll::main_tag_read() };
*MAIN_TAG_READY.lock() = true;
}
}
}
pub fn period_det_ready() -> bool {
*PERIOD_DET_READY.lock()
}
pub fn phase_det_ready() -> bool {
*GTX_TAG_READY.lock() && *MAIN_TAG_READY.lock()
}
pub fn get_period_error() -> i32 {
// overflowing_sub can calculate the correct beating period when GTX_TAG roll over
BEATING_PERIOD - ((*GTX_TAG.lock()).overflowing_sub(*LAST_GTX_TAG.lock()).0) as i32
}
pub fn get_phase_error() -> i32 {
// unwrap tag difference
let mut phase_error = (*MAIN_TAG.lock())
.overflowing_sub(*GTX_TAG.lock())
.0
.rem_euclid(BEATING_PERIOD as u32) as i32;
// mapping tags from [0, 2π] -> [-π, π]
if phase_error > BEATING_HALFPERIOD {
phase_error -= BEATING_PERIOD
}
phase_error
}
}
pub fn helper_setup(timer: &mut GlobalTimer) -> Result<(), &'static str> {
unsafe {
csr::wrpll::helper_reset_write(1);
csr::helper_dcxo::bitbang_enable_write(1);
csr::helper_dcxo::i2c_address_write(ADDRESS);
}
#[cfg(rtio_frequency = "125.0")]
let (h_hsdiv, h_lsdiv, h_fbdiv) = (0x058, 0, 0x0481458C94D); // 125Mhz*16383/16384
setup(i2c::DCXO::Helper, h_hsdiv, h_lsdiv, h_fbdiv, timer)?;
// Si549 maximum settling time for large frequency change.
timer.delay_us(40_000);
unsafe {
csr::wrpll::helper_reset_write(0);
csr::helper_dcxo::bitbang_enable_write(0);
}
info!("Helper Si549 started");
Ok(())
}
pub fn interrupt_setup(interrupt_controller: &mut gic::InterruptController) {
for id in IRQ_ID.iter() {
// setup shared peripheral interrupts (SPI)
interrupt_controller.enable(
gic::InterruptId(*id),
gic::CPUCore::Core0,
gic::InterruptSensitivity::Edge,
0,
);
}
}
fn set_irq(en: bool) {
let val = if en { 1 } else { 0 };
unsafe {
csr::wrpll::gtx_tag_ev_enable_write(val);
csr::wrpll::main_tag_ev_enable_write(val);
}
}
/// set adpll using gateware i2c
/// Note: disable main/helper i2c bitbang before using this function
fn set_adpll(dcxo: i2c::DCXO, adpll: i32) -> Result<(), &'static str> {
if adpll.abs() > ADPLL_MAX {
return Err("adpll is too large");
}
match dcxo {
i2c::DCXO::Main => unsafe {
if csr::main_dcxo::bitbang_enable_read() == 1 {
return Err("Main si549 bitbang mode is active when using gateware i2c");
}
while csr::main_dcxo::adpll_busy_read() == 1 {}
csr::main_dcxo::i2c_address_write(ADDRESS);
csr::main_dcxo::adpll_write(adpll as u32);
csr::main_dcxo::adpll_stb_write(1);
csr::main_dcxo::adpll_stb_write(0);
if csr::main_dcxo::nack_read() == 1 {
return Err("Main si549 failed to ack adpll write");
}
},
#[cfg(has_wrpll)]
i2c::DCXO::Helper => unsafe {
if csr::helper_dcxo::bitbang_enable_read() == 1 {
return Err("Helper si549 bitbang mode is active when using gateware i2c");
}
while csr::helper_dcxo::adpll_busy_read() == 1 {}
csr::helper_dcxo::i2c_address_write(ADDRESS);
csr::helper_dcxo::adpll_write(adpll as u32);
csr::helper_dcxo::adpll_stb_write(1);
csr::helper_dcxo::adpll_stb_write(0);
if csr::helper_dcxo::nack_read() == 1 {
return Err("Helper si549 failed to ack adpll write");
}
},
};
Ok(())
}
fn set_base_adpll(timer: &mut GlobalTimer) -> Result<(), &'static str> {
let count2adpll =
|error: i32| (((error) as f64 * 1e6) / (0.0001164 * (1 << (TIMER_WIDTH - COUNTER_DIV)) as f64)) as i32;
let (gtx_count, main_count, _helper_count) = get_freq_counts(timer);
let mut base_adpll_lock = BASE_ADPLL.lock();
*base_adpll_lock = count2adpll(gtx_count as i32 - main_count as i32);
set_adpll(i2c::DCXO::Main, *base_adpll_lock)?;
set_adpll(i2c::DCXO::Helper, *base_adpll_lock)?;
Ok(())
}
fn get_freq_counts(timer: &mut GlobalTimer) -> (u32, u32, u32) {
unsafe {
csr::wrpll::frequency_counter_update_en_write(1);
timer.delay_us(150_000); // 8ns << TIMER_WIDTH
csr::wrpll::frequency_counter_update_en_write(0);
let gtx = csr::wrpll::frequency_counter_counter_gtx0_rtio_rx_read();
let main = csr::wrpll::frequency_counter_counter_sys_read();
let helper = csr::wrpll::frequency_counter_counter_helper_read();
(gtx, main, helper)
}
}
fn reset_plls() -> Result<(), &'static str> {
*H_INTEGRATOR.lock() = 0;
*M_INTEGRATOR.lock() = 0;
set_adpll(i2c::DCXO::Main, 0)?;
set_adpll(i2c::DCXO::Helper, 0)?;
Ok(())
}
fn clear_pending(interrupt: IRQ) {
match interrupt {
IRQ::GTXTag => unsafe { csr::wrpll::gtx_tag_ev_pending_write(1) },
IRQ::MainTag => unsafe { csr::wrpll::main_tag_ev_pending_write(1) },
}
}
pub fn interrupt_handler(interrupt: IRQ) {
tag_collector::collect_tags(interrupt);
if tag_collector::period_det_ready() {
helper_pll().expect("failed to run helper DCXO PLL");
tag_collector::clear_period_det_ready();
}
if tag_collector::phase_det_ready() {
main_pll().expect("failed to run main DCXO PLL");
tag_collector::clear_phase_det_ready();
}
clear_pending(interrupt);
}
pub fn helper_pll() -> Result<(), &'static str> {
const H_KP: i32 = 2;
const H_KI: f32 = 0.5;
let period_err = tag_collector::get_period_error();
let mut integrator_lock = H_INTEGRATOR.lock();
*integrator_lock += (period_err as f32 * H_KI) as i32;
let mut h_adpll = *BASE_ADPLL.lock() + period_err * H_KP + *integrator_lock;
h_adpll = h_adpll.clamp(-ADPLL_MAX, ADPLL_MAX);
set_adpll(i2c::DCXO::Helper, h_adpll)?;
Ok(())
}
pub fn main_pll() -> Result<(), &'static str> {
const M_KP: i32 = 12;
const M_KI: i32 = 2;
let phase_err = tag_collector::get_phase_error();
let mut integrator_lock = M_INTEGRATOR.lock();
*integrator_lock += phase_err * M_KI;
let mut m_adpll = *BASE_ADPLL.lock() + phase_err * M_KP + *integrator_lock;
m_adpll = m_adpll.clamp(-ADPLL_MAX, ADPLL_MAX);
set_adpll(i2c::DCXO::Main, m_adpll)?;
Ok(())
}
pub fn select_recovered_clock(rc: bool, timer: &mut GlobalTimer) {
set_irq(false);
if rc {
tag_collector::reset();
reset_plls().expect("failed to reset main and helper PLL");
info!("warming up GTX CDR...");
// gtx need a couple seconds for freq counter to read it properly
timer.delay_us(20_000_000);
set_base_adpll(timer).expect("failed to set base adpll");
// clear gateware pending flag
clear_pending(IRQ::GTXTag);
clear_pending(IRQ::MainTag);
set_irq(true);
info!("WRPLL interrupt enabled");
}
}
}

View File

@ -1,5 +1,7 @@
use core::sync::atomic::{AtomicBool, Ordering};
#[cfg(has_wrpll)]
use libboard_artiq::si549;
use libboard_zynq::{gic, mpcore, println, stdio};
use libcortex_a9::{asm, interrupt_handler, notify_spin_lock, regs::MPIDR, spin_lock_yield};
use libregister::RegisterR;
@ -12,16 +14,37 @@ extern "C" {
static CORE1_RESTART: AtomicBool = AtomicBool::new(false);
interrupt_handler!(IRQ, irq, __irq_stack0_start, __irq_stack1_start, {
if MPIDR.read().cpu_id() == 1 {
let mpcore = mpcore::RegisterBlock::mpcore();
let mut gic = gic::InterruptController::gic(mpcore);
let id = gic.get_interrupt_id();
if id.0 == 0 {
gic.end_interrupt(id);
asm::exit_irq();
asm!("b core1_restart");
let mpcore = mpcore::RegisterBlock::mpcore();
let mut gic = gic::InterruptController::gic(mpcore);
let id = gic.get_interrupt_id();
// IRQ ID information at https://docs.xilinx.com/r/en-US/ug585-zynq-7000-SoC-TRM/Software-Generated-Interrupts-SGI?tocId=D777grsNOem_mnEV7glhfg
match MPIDR.read().cpu_id() {
0 => match id.0 {
61 => {
#[cfg(has_wrpll)]
si549::wrpll::interrupt_handler(si549::wrpll::IRQ::GTXTag);
gic.end_interrupt(id);
return;
}
62 => {
#[cfg(has_wrpll)]
si549::wrpll::interrupt_handler(si549::wrpll::IRQ::MainTag);
gic.end_interrupt(id);
return;
}
_ => {}
},
1 => {
if id.0 == 0 {
gic.end_interrupt(id);
asm::exit_irq();
asm!("b core1_restart");
}
}
}
_ => {}
};
stdio::drop_uart();
println!("IRQ");
loop {}

View File

@ -37,6 +37,8 @@ use libboard_artiq::{drtio_routing, drtioaux,
pl::csr};
#[cfg(feature = "target_kasli_soc")]
use libboard_zynq::error_led::ErrorLED;
#[cfg(has_wrpll)]
use libboard_zynq::{gic::InterruptController, mpcore::RegisterBlock};
use libboard_zynq::{i2c::I2c, print, println, time::Milliseconds, timer::GlobalTimer};
use libcortex_a9::{l2c::enable_l2_cache, regs::MPIDR};
use libregister::RegisterR;
@ -739,6 +741,9 @@ pub extern "C" fn main_core0() -> i32 {
let mut timer = GlobalTimer::start();
#[cfg(has_wrpll)]
let mut interrupt_controller = InterruptController::gic(RegisterBlock::mpcore());
let buffer_logger = unsafe { logger::BufferLogger::new(&mut LOG_BUFFER[..]) };
buffer_logger.set_uart_log_level(log::LevelFilter::Info);
buffer_logger.register();
@ -799,6 +804,11 @@ pub extern "C" fn main_core0() -> i32 {
unsafe {
csr::gt_drtio::txenable_write(0xffffffffu32 as _);
}
#[cfg(has_wrpll)]
{
si549::wrpll::helper_setup(&mut timer).expect("cannot initialize helper Si549");
si549::wrpll::interrupt_setup(&mut interrupt_controller);
}
#[cfg(has_drtio_routing)]
let mut repeaters = [repeater::Repeater::default(); csr::DRTIOREP.len()];
@ -841,6 +851,9 @@ pub extern "C" fn main_core0() -> i32 {
si5324::siphaser::calibrate_skew(&mut timer).expect("failed to calibrate skew");
}
#[cfg(has_wrpll)]
si549::wrpll::select_recovered_clock(true, &mut timer);
// Various managers created here, so when link is dropped, all DMA traces
// are cleared out for a clean slate on subsequent connections,
// without a manual intervention.
@ -898,6 +911,8 @@ pub extern "C" fn main_core0() -> i32 {
info!("uplink is down, switching to local oscillator clock");
#[cfg(has_siphaser)]
si5324::siphaser::select_recovered_clock(&mut i2c, false, &mut timer).expect("failed to switch clocks");
#[cfg(has_wrpll)]
si549::wrpll::select_recovered_clock(false, &mut timer);
}
}