add baremetal-cramsoc target for chip bringup

This commit is contained in:
bunnie 2025-08-04 16:11:30 +08:00
parent 364efcb67a
commit af64c1cf14
11 changed files with 891 additions and 13 deletions

View file

@ -24,7 +24,14 @@ artybio = [
"xous-bio-bdma/baremetal",
]
artyvexii = ["utralib/artyvexii", "linked_list_allocator"]
cramium-soc = ["utralib/cramium-soc"]
v0p9 = []
cramium-soc = [
"utralib/cramium-soc",
"linked_list_allocator",
"cramium-hal/loader-baosec",
"cramium-api",
"v0p9",
]
verilator-only = []
default = ["artyvexii"]

View file

@ -6,6 +6,7 @@ use crate::platform;
// Note: inline constants are not yet stable in Rust: https://github.com/rust-lang/rust/pull/104087
#[link_section = ".text.init"]
#[export_name = "_start"]
#[cfg(not(feature = "cramium-soc"))]
pub extern "C" fn _start() {
unsafe {
#[rustfmt::skip]
@ -28,6 +29,80 @@ pub extern "C" fn _start() {
}
}
#[link_section = ".text.init"]
#[export_name = "_start"]
#[cfg(feature = "cramium-soc")]
pub extern "C" fn _start() {
unsafe {
#[rustfmt::skip]
asm! (
// Place the stack pointer at the end of RAM
"mv sp, {ram_top}",
// subtract four from sp to make room for a DMA "gutter"
"addi sp, sp, -4",
// twiddle duart
"li t0, 0x40042000",
// setup etuc
"sw x0, 0x4(t0)", // CR is 0
"li t1, 29",
"sw t1, 0xc(t0)",
"li t1, 0x1",
"sw t1, 0x4(t0)", // CR is 1
// print 32 instances of 'Z' (0x5A)
"li t2, 32",
"li t1, 0x5A",
"10:",
"sw t1, 0x0(t0)",
"11:",
"lw t3, 0x8(t0)", // check SR
"bne x0, t3, 11b", // wait for 0
"addi t2, t2, -1",
"bne x0, t2, 10b",
// test if ifram is cleared
"li t0, 0x50000000",
"li t1, 0x50040000",
"60:",
"lw t2, 0(t0)",
// if not 0, jump to clearing routine
"bne x0, t2, 30f",
"addi t0, t0, 4",
// loop if we haven't checked all of ifram
"bltu t0, t1, 60b",
// if we got here, all of ifram was 0, continue with boot
"j 50f",
// clear ifram
"30:",
"sw x0, 0(t0)",
"addi t0, t0, 4",
"bltu t0, t1, 30b",
// clear main ram
"li t0, 0x61000000",
"li t1, 0x61200000",
"20:",
"sw x0, 0(t0)",
"addi t0, t0, 4",
"bltu t0, t1, 20b",
".word 0x500f",
"50:",
// Install a machine mode trap handler
"la t0, abort",
"csrw mtvec, t0",
// Start Rust
"j rust_entry",
ram_top = in(reg) (platform::RAM_BASE + platform::RAM_SIZE),
options(noreturn)
);
}
}
#[link_section = ".text.init"]
#[export_name = "abort"]
/// This is only used in debug mode
@ -35,12 +110,13 @@ pub extern "C" fn abort() -> ! {
unsafe {
#[rustfmt::skip]
asm!(
"li t0, 0xf0000000",
"li t1, 0x00f",
"li t2, 0xf00",
"300:", // abort
"sw t1, 0(t0)",
"sw t2, 0(t0)",
"li t0, 0x40042000",
"li t1, 0x78",
"300:", // abort by printing x (0x78) to duart
"sw t1, 0x0(t0)",
"11:",
"lw t3, 0x8(t0)", // check SR
"bne x0, t3, 11b", // wait for 0
"j 300b",
options(noreturn)
);

View file

@ -12,6 +12,7 @@ use core::cell::RefCell;
use critical_section::Mutex;
use platform::*;
#[allow(unused_imports)]
use utralib::*;
#[cfg(feature = "artybio")]
use xous_bio_bdma::*;
@ -43,9 +44,12 @@ pub fn uart_irq_handler() {
/// This function is safe to call exactly once.
#[export_name = "rust_entry"]
pub unsafe extern "C" fn rust_entry() -> ! {
// turn on a green LED to indicate boot
let mut rgb = CSR::new(utra::rgb::HW_RGB_BASE as *mut u32);
rgb.wfo(utra::rgb::OUT_OUT, 0x002);
#[cfg(any(feature = "artyvexii", feature = "artybio"))]
{
// turn on a green LED to indicate boot
let mut rgb = CSR::new(utra::rgb::HW_RGB_BASE as *mut u32);
rgb.wfo(utra::rgb::OUT_OUT, 0x002);
}
crate::platform::early_init();
crate::println!("\n~~Baremetal up!~~\n");
@ -60,7 +64,9 @@ pub unsafe extern "C" fn rust_entry() -> ! {
// The green LEDs flash whenever the FPGA is configured with the Arty BIO design.
// The RGB LEDs flash when the CPU is running this code.
#[allow(unused_variables)]
let mut count = 0;
#[cfg(any(feature = "artyvexii", feature = "artybio"))]
let mut rgb = CSR::new(utra::rgb::HW_RGB_BASE as *mut u32);
// provide some feedback on the run state of the BIO by peeking at the program counter
@ -81,6 +87,7 @@ pub unsafe extern "C" fn rust_entry() -> ! {
// Animate the LED flashing to indicate repl loop is running
delay(1);
count += 1;
#[cfg(any(feature = "artyvexii", feature = "artybio"))]
rgb.wfo(utra::rgb::OUT_OUT, ((count / 500) as u32) << 6);
}
}

View file

@ -1,2 +1,4 @@
pub mod cramium;
pub use cramium::*;
pub mod debug;
pub mod irq;

View file

@ -1,3 +1,12 @@
use utralib::CSR;
use utralib::utra;
use utralib::utra::sysctrl;
use crate::platform::{
debug::setup_rx,
irq::{enable_irq, irq_setup},
};
#[global_allocator]
static ALLOCATOR: linked_list_allocator::LockedHeap = linked_list_allocator::LockedHeap::empty();
@ -5,5 +14,411 @@ pub const RAM_SIZE: usize = utralib::generated::HW_SRAM_MEM_LEN;
pub const RAM_BASE: usize = utralib::generated::HW_SRAM_MEM;
pub const FLASH_BASE: usize = utralib::generated::HW_RERAM_MEM;
#[cfg(all(feature = "cramium-soc", not(feature = "verilator-only")))]
pub fn early_init() {}
pub const HEAP_START: usize = RAM_BASE + 1024 * 1024; // 0x610F_E000
pub const HEAP_LEN: usize = 1024 * 256;
// scratch page for exceptions located at top of RAM
// NOTE: there is an additional page above this for exception stack
pub const SCRATCH_PAGE: usize = HEAP_START - 8192;
pub const UART_IFRAM_ADDR: usize = cramium_hal::board::UART_DMA_TX_BUF_PHYS;
pub const SYSTEM_CLOCK_FREQUENCY: u32 = 800_000_000;
pub const SYSTEM_TICK_INTERVAL_MS: u32 = 1;
pub fn early_init() {
let mut uart = crate::debug::Uart {};
for _ in 0..100 {
uart.putc('a' as u32 as u8);
}
unsafe {
// this block is mandatory in all cases to get clocks set into some consistent, expected mode
{
let daric_cgu = sysctrl::HW_SYSCTRL_BASE as *mut u32;
// conservative dividers
daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0.offset()).write_volatile(0x7f7f);
daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1.offset()).write_volatile(0x7f7f);
daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2.offset()).write_volatile(0x3f7f);
daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_3.offset()).write_volatile(0x1f3f);
daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_4.offset()).write_volatile(0x0f1f);
// ungate all clocks
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 clocks
daric_cgu.add(utra::sysctrl::SFR_CGUSET.offset()).write_volatile(0x32);
}
// enable DUART
let duart = utra::duart::HW_DUART_BASE as *mut u32;
duart.add(utra::duart::SFR_CR.offset()).write_volatile(0);
duart.add(utra::duart::SFR_ETUC.offset()).write_volatile(24);
duart.add(utra::duart::SFR_CR.offset()).write_volatile(1);
}
// sram0 requires 1 wait state for writes
let mut sramtrm = CSR::new(utra::coresub_sramtrm::HW_CORESUB_SRAMTRM_BASE as *mut u32);
sramtrm.wo(utra::coresub_sramtrm::SFR_SRAM0, 0x8);
sramtrm.wo(utra::coresub_sramtrm::SFR_SRAM1, 0x8);
#[cfg(feature = "v0p9")]
{
/*
logic [15:0] trm_ram32kx72 ; assign trm_ram32kx72 = trmdat[0 ]; localparam t_trm IV_trm_ram32kx72 = IV_sram_sp_uhde_inst_sram0;
logic [15:0] trm_ram8kx72 ; assign trm_ram8kx72 = trmdat[1 ]; localparam t_trm IV_trm_ram8kx72 = IV_sram_sp_hde_inst_sram1;
logic [15:0] trm_rf1kx72 ; assign trm_rf1kx72 = trmdat[2 ]; localparam t_trm IV_trm_rf1kx72 = IV_rf_sp_hde_inst_cache;
logic [15:0] trm_rf256x27 ; assign trm_rf256x27 = trmdat[3 ]; localparam t_trm IV_trm_rf256x27 = IV_rf_sp_hde_inst_cache;
logic [15:0] trm_rf512x39 ; assign trm_rf512x39 = trmdat[4 ]; localparam t_trm IV_trm_rf512x39 = IV_rf_sp_hde_inst_cache;
logic [15:0] trm_rf128x31 ; assign trm_rf128x31 = trmdat[5 ]; localparam t_trm IV_trm_rf128x31 = IV_rf_sp_hde_inst_cache;
logic [15:0] trm_dtcm8kx36 ; assign trm_dtcm8kx36 = trmdat[6 ]; localparam t_trm IV_trm_dtcm8kx36 = IV_sram_sp_hde_inst_tcm;
logic [15:0] trm_itcm32kx18 ; assign trm_itcm32kx18 = trmdat[7 ]; localparam t_trm IV_trm_itcm32kx18 = IV_sram_sp_hde_inst_tcm;
logic [15:0] trm_ifram32kx36 ; assign trm_ifram32kx36 = trmdat[8 ]; localparam t_trm IV_trm_ifram32kx36 = IV_sram_sp_uhde_inst;
logic [15:0] trm_sce_sceram_10k ; assign trm_sce_sceram_10k = trmdat[9 ]; localparam t_trm IV_trm_sce_sceram_10k = IV_sram_sp_hde_inst;
logic [15:0] trm_sce_hashram_3k ; assign trm_sce_hashram_3k = trmdat[10]; localparam t_trm IV_trm_sce_hashram_3k = IV_rf_sp_hde_inst;
logic [15:0] trm_sce_aesram_1k ; assign trm_sce_aesram_1k = trmdat[11]; localparam t_trm IV_trm_sce_aesram_1k = IV_rf_sp_hde_inst;
logic [15:0] trm_sce_pkeram_4k ; assign trm_sce_pkeram_4k = trmdat[12]; localparam t_trm IV_trm_sce_pkeram_4k = IV_rf_sp_hde_inst;
logic [15:0] trm_sce_aluram_3k ; assign trm_sce_aluram_3k = trmdat[13]; localparam t_trm IV_trm_sce_aluram_3k = IV_rf_sp_hde_inst;
logic [15:0] trm_sce_mimmdpram ; assign trm_sce_mimmdpram = trmdat[14]; localparam t_trm IV_trm_sce_mimmdpram = IV_rf_2p_hdc_inst;
logic [15:0] trm_rdram1kx32 ; assign trm_rdram1kx32 = trmdat[15]; localparam t_trm IV_trm_rdram1kx32 = IV_rf_2p_hdc_inst_vex;
logic [15:0] trm_rdram512x64 ; assign trm_rdram512x64 = trmdat[16]; localparam t_trm IV_trm_rdram512x64 = IV_rf_2p_hdc_inst_vex;
logic [15:0] trm_rdram128x22 ; assign trm_rdram128x22 = trmdat[17]; localparam t_trm IV_trm_rdram128x22 = IV_rf_2p_hdc_inst_vex;
logic [15:0] trm_rdram32x16 ; assign trm_rdram32x16 = trmdat[18]; localparam t_trm IV_trm_rdram32x16 = IV_rf_2p_hdc_inst_vex;
logic [15:0] trm_bioram1kx32 ; assign trm_bioram1kx32 = trmdat[19]; localparam t_trm IV_trm_bioram1kx32 = IV_rf_sp_hde_inst_cache;
logic [15:0] trm_tx_fifo128x32 ; assign trm_tx_fifo128x32 = trmdat[20]; localparam t_trm IV_trm_tx_fifo128x32 = IV_rf_2p_hdc_inst;
logic [15:0] trm_rx_fifo128x32 ; assign trm_rx_fifo128x32 = trmdat[21]; localparam t_trm IV_trm_rx_fifo128x32 = IV_rf_2p_hdc_inst;
logic [15:0] trm_fifo32x19 ; assign trm_fifo32x19 = trmdat[22]; localparam t_trm IV_trm_fifo32x19 = IV_rf_2p_hdc_inst;
logic [15:0] trm_udcmem_share ; assign trm_udcmem_share = trmdat[23]; localparam t_trm IV_trm_udcmem_share = IV_rf_2p_hdc_inst;
logic [15:0] trm_udcmem_odb ; assign trm_udcmem_odb = trmdat[24]; localparam t_trm IV_trm_udcmem_odb = IV_rf_2p_hdc_inst;
logic [15:0] trm_udcmem_256x64 ; assign trm_udcmem_256x64 = trmdat[25]; localparam t_trm IV_trm_udcmem_256x64 = IV_rf_2p_hdc_inst;
logic [15:0] trm_acram2kx64 ; assign trm_acram2kx64 = trmdat[26]; localparam t_trm IV_trm_acram2kx64 = IV_sram_sp_uhde_inst_sram0;
logic [15:0] trm_aoram1kx36 ; assign trm_aoram1kx36 = trmdat[27]; localparam t_trm IV_trm_aoram1kx36 = IV_sram_sp_hde_inst;
*/
uart.tiny_write_str("t0\r");
let mut sramtrm = CSR::new(utra::coresub_sramtrm::HW_CORESUB_SRAMTRM_BASE as *mut u32);
sramtrm.wo(utra::coresub_sramtrm::SFR_CACHE, 0x3);
sramtrm.wo(utra::coresub_sramtrm::SFR_ITCM, 0x3);
sramtrm.wo(utra::coresub_sramtrm::SFR_DTCM, 0x3);
sramtrm.wo(utra::coresub_sramtrm::SFR_VEXRAM, 0x1);
let mut rbist = CSR::new(utra::rbist_wrp::HW_RBIST_WRP_BASE as *mut u32);
// bio 0.9v settings
rbist.wo(utra::rbist_wrp::SFRCR_TRM, (19 << 16) | 0b011_000_01_0_0_000_0_00);
rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a);
// vex 0.9v settings
for i in 0..4 {
rbist.wo(utra::rbist_wrp::SFRCR_TRM, ((15 + i) << 16) | 0b001_010_00_0_0_000_0_00);
rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a);
}
// sram 0.9v settings
rbist.wo(utra::rbist_wrp::SFRCR_TRM, (0 << 16) | 0b011_000_01_0_1_000_0_00);
rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a);
rbist.wo(utra::rbist_wrp::SFRCR_TRM, (1 << 16) | 0b011_000_00_0_0_000_0_00);
rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a);
uart.tiny_write_str("t1\r");
// tcm 0.9v
rbist.wo(utra::rbist_wrp::SFRCR_TRM, (6 << 16) | 0b011_000_00_0_0_000_0_00);
rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a);
rbist.wo(utra::rbist_wrp::SFRCR_TRM, (7 << 16) | 0b011_000_00_0_0_000_0_00);
rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a);
// ifram 0.9v
rbist.wo(utra::rbist_wrp::SFRCR_TRM, (8 << 16) | 0b010_000_00_0_1_000_1_01);
rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a);
// sce 0.9V
rbist.wo(utra::rbist_wrp::SFRCR_TRM, (9 << 16) | 0b011_000_00_0_0_000_0_00);
rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a);
for i in 0..4 {
rbist.wo(utra::rbist_wrp::SFRCR_TRM, ((10 + i) << 16) | 0b011_000_01_0_1_000_0_00);
rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a);
}
rbist.wo(utra::rbist_wrp::SFRCR_TRM, (14 << 16) | 0b001_010_00_0_0_000_0_00);
rbist.wo(utra::rbist_wrp::SFRAR_TRM, 0x5a);
}
let perclk = unsafe { init_clock_asic(800_000_000) };
// setup the static data (hard-coded, because no loader) - necessary for heap
// see build message about non-zero data
let static_data = unsafe { core::slice::from_raw_parts_mut(0x6100_0000 as *mut u8, 20) };
static_data.fill(0);
static_data[8] = 1;
// setup other things
setup_rx(perclk);
irq_setup();
enable_irq(utra::irqarray5::IRQARRAY5_IRQ);
setup_alloc();
}
pub fn setup_alloc() {
// Initialize the allocator with heap memory range
crate::println!("Setting up heap @ {:x}-{:x}", HEAP_START, HEAP_START + HEAP_LEN);
crate::println!("Stack @ {:x}-{:x}", HEAP_START + HEAP_LEN, RAM_BASE + RAM_SIZE);
unsafe {
ALLOCATOR.lock().init(HEAP_START as *mut u8, HEAP_LEN);
}
}
/// Delay function that delays a given number of milliseconds.
pub fn delay(ms: usize) {
let mut timer = utralib::CSR::new(utra::timer0::HW_TIMER0_BASE as *mut u32);
timer.wfo(utra::timer0::EV_PENDING_ZERO, 1);
for _ in 0..ms {
while timer.rf(utra::timer0::EV_PENDING_ZERO) == 0 {}
timer.wfo(utra::timer0::EV_PENDING_ZERO, 1);
}
}
mod panic_handler {
use core::panic::PanicInfo;
#[panic_handler]
fn handle_panic(_arg: &PanicInfo) -> ! {
crate::println!("{}", _arg);
loop {}
}
}
// This function supercedes init_clock_asic() and needs to be back-ported
// into xous-core
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 FREQ_OSC_MHZ: u32 = 48; // Actually 48MHz
const M: u32 = 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
];
// 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
// 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.
daric_cgu.add(utra::sysctrl::SFR_CGUFDPER.offset()).write_volatile(0x01_ff_ff);
/*
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(0x2f);
daric_cgu.add(utra::sysctrl::SFR_HCLKGR.offset()).write_volatile(0xff);
daric_cgu.add(utra::sysctrl::SFR_ICLKGR.offset()).write_volatile(0x8f);
daric_cgu.add(utra::sysctrl::SFR_PCLKGR.offset()).write_volatile(0xff);
crate::println!("bef gates set");
// commit dividers
daric_cgu.add(utra::sysctrl::SFR_CGUSET.offset()).write_volatile(0x32);
crate::println!("gates set");
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);
}
// TODO select int/ext osc/xtal
// 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, FREQ_OSC_MHZ);
// __DSB();
// DARIC_CGU->cguset = 0x32UL;
cgu.wo(sysctrl::SFR_CGUSET, 0x32);
// __DSB();
if freq_hz < 1000000 {
// 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 < 1000000 {
} 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");
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 _cgufsfreq0 = daric_cgu.add(sysctrl::SFR_CGUFSSR_FSFREQ0.offset()).read_volatile();
let _cgufsfreq1 = daric_cgu.add(sysctrl::SFR_CGUFSSR_FSFREQ1.offset()).read_volatile();
let _cgufsfreq2 = daric_cgu.add(sysctrl::SFR_CGUFSSR_FSFREQ2.offset()).read_volatile();
let _cgufsfreq3 = daric_cgu.add(sysctrl::SFR_CGUFSSR_FSFREQ3.offset()).read_volatile();
crate::println!(
"Internal osc: {} -> {} MHz ({} MHz)",
_cgufsfreq0,
fsfreq_to_hz(_cgufsfreq0),
fsfreq_to_hz_32(_cgufsfreq0)
);
crate::println!(
"XTAL: {} -> {} MHz ({} MHz)",
_cgufsfreq1,
fsfreq_to_hz(_cgufsfreq1),
fsfreq_to_hz_32(_cgufsfreq1)
);
crate::println!(
"pll output 0: {} -> {} MHz ({} MHz)",
_cgufsfreq2,
fsfreq_to_hz(_cgufsfreq2),
fsfreq_to_hz_32(_cgufsfreq2)
);
crate::println!(
"pll output 1: {} -> {} MHz ({} MHz)",
_cgufsfreq3,
fsfreq_to_hz(_cgufsfreq3),
fsfreq_to_hz_32(_cgufsfreq3)
);
crate::println!("PLL configured to {} MHz", freq_hz / 1_000_000);
100_000_000
}
#[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 }

View file

@ -0,0 +1,145 @@
use cramium_api::*;
use cramium_hal::iox::Iox;
use cramium_hal::{udma, udma::GlobalConfig};
use utralib::generated::*;
/// A trait for serial like drivers which allows reading from a source.
#[allow(dead_code)]
pub trait SerialRead {
/// Read a single byte.
fn getc(&mut self) -> Option<u8>;
}
pub struct Uart {}
#[allow(dead_code)]
impl Uart {
pub fn putc(&self, c: u8) {
let base = utra::duart::HW_DUART_BASE as *mut u32;
let mut uart = CSR::new(base);
if uart.rf(utra::duart::SFR_CR_SFR_CR) == 0 {
uart.wfo(utra::duart::SFR_CR_SFR_CR, 1);
}
while uart.r(utra::duart::SFR_SR) != 0 {}
uart.wo(utra::duart::SFR_TXD, c as u32);
}
fn put_digit(&mut self, d: u8) {
let nyb = d & 0xF;
let c = if nyb < 10 { nyb + 0x30 } else { nyb + 0x61 - 10 };
assert!(c >= 0x30, "conversion failed!");
self.putc(c);
}
pub fn put_hex(&mut self, c: u8) {
self.put_digit(c >> 4);
self.put_digit(c & 0xF);
}
pub fn newline(&mut self) {
self.putc(0xa);
self.putc(0xd);
}
pub fn print_hex_word(&mut self, word: u32) {
for &byte in word.to_be_bytes().iter() {
self.put_hex(byte);
}
}
pub fn tiny_write_str(&mut self, s: &str) {
for c in s.bytes() {
self.putc(c);
}
}
}
use core::fmt::{Error, Write};
impl Write for Uart {
fn write_str(&mut self, s: &str) -> Result<(), Error> {
for c in s.bytes() {
self.putc(c);
}
Ok(())
}
}
impl SerialRead for Uart {
fn getc(&mut self) -> Option<u8> {
let uart_buf_addr = crate::platform::UART_IFRAM_ADDR;
let mut udma_uart = unsafe {
// safety: this is safe to call, because we set up clock and events prior to calling new.
udma::Uart::get_handle(utra::udma_uart_1::HW_UDMA_UART_1_BASE, uart_buf_addr, uart_buf_addr)
};
let mut c = 0u8;
match udma_uart.read_async(&mut c) {
0 => None,
_ => Some(c),
}
}
}
#[macro_use]
pub mod debug_print_hardware {
#[macro_export]
macro_rules! print
{
($($args:tt)+) => ({
use core::fmt::Write;
let _ = write!(crate::debug::Uart {}, $($args)+);
});
}
}
#[macro_export]
macro_rules! println
{
() => ({
$crate::print!("\r\n")
});
($fmt:expr) => ({
$crate::print!(concat!($fmt, "\r\n"))
});
($fmt:expr, $($args:tt)+) => ({
$crate::print!(concat!($fmt, "\r\n"), $($args)+)
});
}
pub fn setup_rx(perclk: u32) {
let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32);
iox.set_alternate_function(IoxPort::PD, 13, IoxFunction::AF1);
iox.set_alternate_function(IoxPort::PD, 14, IoxFunction::AF1);
// rx as input, with pull-up
iox.set_gpio_dir(IoxPort::PD, 13, IoxDir::Input);
iox.set_gpio_pullup(IoxPort::PD, 13, IoxEnable::Enable);
// tx as output
iox.set_gpio_dir(IoxPort::PD, 14, IoxDir::Output);
let udma_global = GlobalConfig::new();
udma_global.clock_on(PeriphId::Uart1);
udma_global.map_event(
PeriphId::Uart1,
PeriphEventType::Uart(EventUartOffset::Rx),
EventChannel::Channel0,
);
udma_global.map_event(
PeriphId::Uart1,
PeriphEventType::Uart(EventUartOffset::Tx),
EventChannel::Channel1,
);
let baudrate: u32 = 115200;
let freq: u32 = perclk / 2;
// the address of the UART buffer is "hard-allocated" at an offset one page from the top of
// IFRAM0. This is a convention that must be respected by the UDMA UART library implementation
// for things to work.
let uart_buf_addr = crate::platform::UART_IFRAM_ADDR;
let mut udma_uart = unsafe {
// safety: this is safe to call, because we set up clock and events prior to calling new.
udma::Uart::get_handle(utra::udma_uart_1::HW_UDMA_UART_1_BASE, uart_buf_addr, uart_buf_addr)
};
udma_uart.set_baud(baudrate, freq);
udma_uart.setup_async_read();
// setup interrupt here
}

View file

@ -0,0 +1,206 @@
use riscv::register::{mcause, mie, mstatus};
use vexriscv::register::vexriscv::{mim, mip};
use crate::*;
pub fn irq_setup() {
unsafe {
#[rustfmt::skip]
core::arch::asm!(
// Set trap handler, which will be called
// on interrupts and cpu faults
"la t0, _start_trap", // this first one forces the nop sled symbol to be generated
"la t0, _start_trap_aligned", // this is the actual target
"csrw mtvec, t0",
);
}
mim::write(0x0); // first make sure everything is disabled, so we aren't OR'ing in garbage
unsafe {
mstatus::set_mie();
}
// must enable external interrupts on the CPU for any of the above to matter
unsafe { mie::set_mext() };
}
pub fn enable_irq(irq_no: usize) {
// Note that the vexriscv "IRQ Mask" register is inverse-logic --
// that is, setting a bit in the "mask" register unmasks (i.e. enables) it.
mim::write(mim::read() | (1 << irq_no));
// must enable external interrupts on the CPU for any of the above to matter
unsafe { mie::set_mext() };
}
#[export_name = "_start_trap"]
// #[repr(align(4))] // can't do this yet.
#[inline(never)]
pub unsafe extern "C" fn _start_trap() -> ! {
loop {
// install a NOP sled before _start_trap() until https://github.com/rust-lang/rust/issues/82232 is stable
core::arch::asm!("nop", "nop", "nop", "nop");
#[export_name = "_start_trap_aligned"]
pub unsafe extern "C" fn _start_trap_aligned() {
#[rustfmt::skip]
core::arch::asm!(
"csrw mscratch, sp",
"li sp, 0x610FE000", // crate::platform::SCRATCH_PAGE - has to be hard-coded
"sw x1, 0*4(sp)",
// Skip SP for now
"sw x3, 2*4(sp)",
"sw x4, 3*4(sp)",
"sw x5, 4*4(sp)",
"sw x6, 5*4(sp)",
"sw x7, 6*4(sp)",
"sw x8, 7*4(sp)",
"sw x9, 8*4(sp)",
"sw x10, 9*4(sp)",
"sw x11, 10*4(sp)",
"sw x12, 11*4(sp)",
"sw x13, 12*4(sp)",
"sw x14, 13*4(sp)",
"sw x15, 14*4(sp)",
"sw x16, 15*4(sp)",
"sw x17, 16*4(sp)",
"sw x18, 17*4(sp)",
"sw x19, 18*4(sp)",
"sw x20, 19*4(sp)",
"sw x21, 20*4(sp)",
"sw x22, 21*4(sp)",
"sw x23, 22*4(sp)",
"sw x24, 23*4(sp)",
"sw x25, 24*4(sp)",
"sw x26, 25*4(sp)",
"sw x27, 26*4(sp)",
"sw x28, 27*4(sp)",
"sw x29, 28*4(sp)",
"sw x30, 29*4(sp)",
"sw x31, 30*4(sp)",
// Save MEPC
"csrr t0, mepc",
"sw t0, 31*4(sp)",
// Save x1, which was used to calculate the offset. Prior to
// calculating, it was stashed at 0x61006000.
//"li t0, 0x61006000",
//"lw t1, 0*4(t0)",
//"sw t1, 0*4(sp)",
// Finally, save SP
"csrr t0, mscratch",
"sw t0, 1*4(sp)",
// Restore a default stack pointer
"li sp, 0x61100000", // heap base, grows down
// Note that registers $a0-$a7 still contain the arguments
"j _start_trap_rust",
// Note to self: trying to assign the scratch and default pages using in(reg) syntax
// clobbers the `a0` register and places the initialization outside of the handler loop
// and there seems to be no way to refer directly to a symbol? the `sym` directive wants
// to refer to an address, not a constant.
);
}
_start_trap_aligned();
core::arch::asm!("nop", "nop", "nop", "nop");
}
}
#[export_name = "_resume_context"]
#[inline(never)]
pub unsafe extern "C" fn _resume_context(registers: u32) -> ! {
#[rustfmt::skip]
core::arch::asm!(
"move sp, {registers}",
"lw x1, 0*4(sp)",
// Skip SP for now
"lw x3, 2*4(sp)",
"lw x4, 3*4(sp)",
"lw x5, 4*4(sp)",
"lw x6, 5*4(sp)",
"lw x7, 6*4(sp)",
"lw x8, 7*4(sp)",
"lw x9, 8*4(sp)",
"lw x10, 9*4(sp)",
"lw x11, 10*4(sp)",
"lw x12, 11*4(sp)",
"lw x13, 12*4(sp)",
"lw x14, 13*4(sp)",
"lw x15, 14*4(sp)",
"lw x16, 15*4(sp)",
"lw x17, 16*4(sp)",
"lw x18, 17*4(sp)",
"lw x19, 18*4(sp)",
"lw x20, 19*4(sp)",
"lw x21, 20*4(sp)",
"lw x22, 21*4(sp)",
"lw x23, 22*4(sp)",
"lw x24, 23*4(sp)",
"lw x25, 24*4(sp)",
"lw x26, 25*4(sp)",
"lw x27, 26*4(sp)",
"lw x28, 27*4(sp)",
"lw x29, 28*4(sp)",
"lw x30, 29*4(sp)",
"lw x31, 30*4(sp)",
// Restore SP
"lw x2, 1*4(sp)",
"mret",
registers = in(reg) registers,
);
loop {}
}
/// Just handles specific traps for testing CPU interactions. Doesn't do anything useful with the traps.
#[export_name = "_start_trap_rust"]
pub extern "C" fn trap_handler(
_a0: usize,
_a1: usize,
_a2: usize,
_a3: usize,
_a4: usize,
_a5: usize,
_a6: usize,
_a7: usize,
) -> ! {
let cause: mcause::Mcause = mcause::read();
// crate::println!("cause {:x}", cause.bits());
// 2 is illegal instruction
if cause.bits() == 2 {
// skip past the illegal instruction, in case that's what we want to do...
unsafe {
#[rustfmt::skip]
core::arch::asm!(
"csrr t0, mepc",
"addi t0, t0, 4",
"csrw mepc, t0",
);
}
// ...but also panic.
panic!("Illegal Instruction");
} else if cause.bits() == 4 {
let epc = riscv::register::mepc::read();
panic!("Load address misaligned @ {:x}", epc);
} else if cause.bits() == 6 {
let epc = riscv::register::mepc::read();
panic!("Store address misaligned @ {:x}", epc);
} else if cause.bits() == 0x8000_000b {
// external machine interrupt
// external interrupt. find out which ones triggered it, and clear the source.
let irqs_pending = mip::read();
if (irqs_pending & (1 << utra::timer0::TIMER0_IRQ)) != 0 {
let ms = SYSTEM_TICK_INTERVAL_MS;
let mut timer0 = CSR::new(utra::timer0::HW_TIMER0_BASE as *mut u32);
timer0.wfo(utra::timer0::EV_PENDING_ZERO, 1);
timer0.wfo(utra::timer0::RELOAD_RELOAD, (SYSTEM_CLOCK_FREQUENCY / 1_000) * ms);
} else if (irqs_pending & (1 << utra::irqarray5::IRQARRAY5_IRQ)) != 0 {
crate::uart_irq_handler();
}
}
// re-enable interrupts
unsafe { mie::set_mext() };
// crate::println!("restoring from {:x}", crate::platform::SCRATCH_PAGE);
unsafe { _resume_context(crate::platform::SCRATCH_PAGE as u32) };
}

View file

@ -1,6 +1,6 @@
MEMORY
{
FLASH : ORIGIN = 0x60000000, LENGTH = 2M
FLASH : ORIGIN = 0x60000000, LENGTH = 4M
RAM : ORIGIN = 0x61000000, LENGTH = 2M
}

View file

@ -1,6 +1,7 @@
use alloc::string::{String, ToString};
use alloc::vec::Vec;
#[allow(unused_imports)]
use utralib::*;
#[cfg(feature = "artybio")]
use xous_bio_bdma::*;
@ -48,6 +49,7 @@ impl Repl {
let cmd = parts.next().unwrap_or("").to_string();
let args: Vec<String> = parts.map(|s| s.to_string()).collect();
match cmd.as_str() {
#[cfg(not(feature = "cramium-soc"))]
"mon" => {
#[cfg(feature = "artybio")]
let bio_ss = BioSharedState::new();

View file

@ -413,6 +413,19 @@ impl Builder {
self
}
/// Configure for baremetal bringup
pub fn target_baremetal_cramsoc(&mut self) -> &mut Builder {
self.target = Some(crate::TARGET_TRIPLE_RISCV32.to_string());
self.target_kernel = Some(crate::TARGET_TRIPLE_RISCV32_KERNEL.to_string());
self.stream = BuildStream::Release;
self.utra_target = "cramium-soc".to_string();
self.run_svd2repl = false;
self.loader = CrateSpec::Local("baremetal".to_string(), LoaderRegion::Ram);
// this is actually a dummy, there is no kernel in baremetal
self.kernel = CrateSpec::Local("xous-kernel".to_string(), LoaderRegion::Ram);
self
}
/// Override the default kernel. For example, to use the kernel from crates.io, specify as
/// "xous-kernel@0.9.9"
#[allow(dead_code)]

View file

@ -647,6 +647,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
builder.target_artyvexii();
}
Some("baremetal-cramsoc") => {
builder.set_baremetal(true);
builder.target_baremetal_cramsoc();
}
Some("baosec") => {
let board = "board-baosec";
// select the board