2018-03-01 09:22:52 +08:00
|
|
|
use board::{csr, clock};
|
|
|
|
use core::slice;
|
|
|
|
use byteorder::{ByteOrder, BigEndian};
|
2018-03-01 00:46:16 +08:00
|
|
|
|
2018-03-01 09:22:52 +08:00
|
|
|
const CCLK_BIT: u8 = 1 << 0;
|
|
|
|
const DIN_BIT: u8 = 1 << 1;
|
|
|
|
const DONE_BIT: u8 = 1 << 2;
|
|
|
|
const INIT_B_BIT: u8 = 1 << 3;
|
|
|
|
const PROGRAM_B_BIT: u8 = 1 << 4;
|
2018-03-01 00:46:16 +08:00
|
|
|
|
2018-03-01 09:22:52 +08:00
|
|
|
const GATEWARE: *mut u8 = csr::CONFIG_SLAVE_FPGA_GATEWARE as *mut u8;
|
2018-03-01 00:46:16 +08:00
|
|
|
|
2018-03-01 09:22:52 +08:00
|
|
|
unsafe fn shift_u8(data: u8) {
|
|
|
|
for i in 0..8 {
|
|
|
|
let mut bits: u8 = PROGRAM_B_BIT;
|
|
|
|
if data & (0x80 >> i) != 0 {
|
|
|
|
bits |= DIN_BIT;
|
2018-03-01 00:46:16 +08:00
|
|
|
}
|
2018-03-01 09:22:52 +08:00
|
|
|
// Without delays, this is about 6 MHz CCLK which is fine.
|
|
|
|
csr::slave_fpga_cfg::out_write(bits);
|
|
|
|
// clock::spin_us(1);
|
|
|
|
csr::slave_fpga_cfg::out_write(bits | CCLK_BIT);
|
|
|
|
// clock::spin_us(1);
|
2018-03-01 00:46:16 +08:00
|
|
|
}
|
2018-03-01 09:22:52 +08:00
|
|
|
}
|
2018-03-01 00:46:16 +08:00
|
|
|
|
2018-03-01 09:22:52 +08:00
|
|
|
pub fn load() -> Result<(), &'static str> {
|
|
|
|
info!("Loading slave FPGA gateware...");
|
2018-03-01 00:46:16 +08:00
|
|
|
|
2018-03-01 09:22:52 +08:00
|
|
|
let header = unsafe { slice::from_raw_parts(GATEWARE, 8) };
|
|
|
|
let magic = BigEndian::read_u32(&header[0..]);
|
|
|
|
let length = BigEndian::read_u32(&header[4..]) as usize;
|
2018-03-01 00:46:16 +08:00
|
|
|
|
2018-03-01 09:22:52 +08:00
|
|
|
if magic != 0x53415231 { // "SAR1"
|
|
|
|
return Err("Slave FPGA gateware magic not found");
|
|
|
|
} else if length > 0x220000 {
|
|
|
|
return Err("Slave FPGA gateware too large (corrupted?)");
|
|
|
|
}
|
|
|
|
info!("Slave FPGA gateware length: 0x{:06x}", length);
|
2018-03-01 00:46:16 +08:00
|
|
|
|
2018-03-01 09:22:52 +08:00
|
|
|
unsafe {
|
|
|
|
csr::slave_fpga_cfg::oe_write(CCLK_BIT | DIN_BIT | PROGRAM_B_BIT);
|
2018-03-01 00:46:16 +08:00
|
|
|
|
2018-03-01 09:22:52 +08:00
|
|
|
csr::slave_fpga_cfg::out_write(0);
|
2018-03-01 17:28:01 +08:00
|
|
|
clock::spin_us(1_000); // TPROGRAM=250ns min, be_generous
|
|
|
|
if csr::slave_fpga_cfg::in_read() & INIT_B_BIT != 0 {
|
|
|
|
return Err("Slave FPGA did not react to PROGRAM.");
|
|
|
|
}
|
2018-03-01 09:22:52 +08:00
|
|
|
csr::slave_fpga_cfg::out_write(PROGRAM_B_BIT);
|
|
|
|
clock::spin_us(5_000); // TPL=5ms max
|
|
|
|
if csr::slave_fpga_cfg::in_read() & INIT_B_BIT == 0 {
|
2018-03-01 17:28:01 +08:00
|
|
|
return Err("Slave FPGA did finish INITialization.");
|
2018-03-01 09:22:52 +08:00
|
|
|
}
|
2018-03-01 00:46:16 +08:00
|
|
|
|
2018-03-01 09:22:52 +08:00
|
|
|
for i in slice::from_raw_parts(GATEWARE.offset(8), length) {
|
|
|
|
shift_u8(*i);
|
|
|
|
if csr::slave_fpga_cfg::in_read() & INIT_B_BIT == 0 {
|
|
|
|
return Err("Slave FPGA error: INIT_B went low.");
|
2018-03-01 00:46:16 +08:00
|
|
|
}
|
2018-03-01 09:22:52 +08:00
|
|
|
}
|
2018-03-01 00:46:16 +08:00
|
|
|
|
2018-03-01 09:22:52 +08:00
|
|
|
let t = clock::get_ms();
|
|
|
|
while csr::slave_fpga_cfg::in_read() & DONE_BIT == 0 {
|
|
|
|
if clock::get_ms() > t + 100 {
|
|
|
|
error!("Slave FPGA not DONE after loading");
|
|
|
|
error!("Corrupt gateware? Slave FPGA in slave serial mode?");
|
|
|
|
return Err("Slave FPGA not DONE");
|
2018-03-01 00:46:16 +08:00
|
|
|
}
|
2018-03-01 09:22:52 +08:00
|
|
|
shift_u8(0xff);
|
2018-03-01 00:46:16 +08:00
|
|
|
}
|
2018-03-01 09:22:52 +08:00
|
|
|
shift_u8(0xff); // "Compensate for Special Startup Conditions"
|
|
|
|
csr::slave_fpga_cfg::out_write(PROGRAM_B_BIT);
|
2018-03-01 00:46:16 +08:00
|
|
|
}
|
|
|
|
|
2018-03-01 09:22:52 +08:00
|
|
|
Ok(())
|
2018-03-01 00:46:16 +08:00
|
|
|
}
|