recognize more flash chip types

1. System works with 1.8V VDD I/O now, needs a new chip ID for
flash

2. Add direct c-translated version of init_clock_asic() for reference
testing.
This commit is contained in:
bunnie 2024-10-01 14:39:40 +08:00
parent 66c3656d03
commit cb466c24a3

View file

@ -140,6 +140,7 @@ pub fn early_init() {
// Now setup the clocks for real
// Safety: this can only be called in the early_init boot context
let perclk = unsafe { init_clock_asic(800_000_000) };
// let perclk = unsafe { init_clock_asic_c(800_000_000, 0) };
crate::println!("Perclk is {} Hz", perclk);
// Configure the UDMA UART. This UART's settings will be used as the initial console UART.
@ -147,10 +148,10 @@ pub fn early_init() {
// on the cramium-hal crate to be functional.
// Set up the IO mux to map UART_A0:
// UART_RX_A[0] = PA3
// UART_TX_A[0] = PA4
// UART_RX_A[1] = PD13
// UART_RX_A[1] = PD14
// UART_RX_A[0] = PA3 app
// UART_TX_A[0] = PA4 app
// UART_RX_A[1] = PD13 console
// UART_RX_A[1] = PD14 console
let mut 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);
@ -393,7 +394,7 @@ pub fn early_init() {
11,
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive4mA),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
None,
None,
@ -407,7 +408,7 @@ pub fn early_init() {
Some(IoxFunction::AF1),
None,
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
Some(IoxEnable::Disable),
None,
None,
);
@ -420,7 +421,7 @@ pub fn early_init() {
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
Some(IoxEnable::Disable),
None,
None,
);
@ -432,7 +433,7 @@ pub fn early_init() {
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
Some(IoxEnable::Disable),
None,
None,
);
@ -507,7 +508,8 @@ pub fn early_init() {
crate::println!("flash ID: {:x}", flash_id);
crate::println!("ram ID: {:x}", ram_id);
// density 18, memory type 20, mfg ID C2 ==> MX25L128833F
assert!(flash_id & 0xFF_FF_FF == 0x1820C2);
// density 38, memory type 25, mfg ID C2 ==> MX25U12832F
assert!(flash_id & 0xFF_FF_FF == 0x1820C2 || flash_id & 0xFF_FF_FF == 0x38_25_C2);
// KGD 5D, mfg ID 9D; remainder of bits are part of the EID
assert!(ram_id & 0xFF_FF == 0x5D9D);
@ -534,7 +536,8 @@ pub fn early_init() {
crate::println!("QPI flash ID: {:x}", flash_id);
crate::println!("QPI ram ID: {:x}", ram_id);
// density 18, memory type 20, mfg ID C2 ==> MX25L128833F
assert!(flash_id & 0xFF_FF_FF == 0x1820C2);
// density 38, memory type 25, mfg ID C2 ==> MX25U12832F
assert!(flash_id & 0xFF_FF_FF == 0x1820C2 || flash_id & 0xFF_FF_FF == 0x38_25_C2);
// KGD 5D, mfg ID 9D; remainder of bits are part of the EID
assert!(ram_id & 0xFF_FF == 0x5D9D);
@ -786,6 +789,7 @@ pub fn early_init() {
}
}
/*
let aoc_base = utralib::CSR::new(0x4006_0000 as *mut u32);
unsafe {
for i in 0..0x50 / 4 {
@ -796,6 +800,7 @@ pub fn early_init() {
aoc_base.base().add(4).write_volatile(pmu_cr & !1);
crate::println!("pmu_cr upd: {:x}", aoc_base.base().add(4).read_volatile());
}
*/
udma_uart.write("Press any key to continue...".as_bytes());
getc();
udma_uart.write(b"\n\rBooting!\n\r");
@ -1361,3 +1366,168 @@ pub extern "C" fn trap_handler(
unsafe { mie::set_mext() };
unsafe { _resume_context(0x61008000u32) }; // this is the scratch page used in the assembly routine above
}
#[allow(dead_code)]
pub fn log_2(mut value: u32) -> u32 {
let mut result = 0;
// Shift right until we find the position of the highest set bit
while value > 1 {
value >>= 1;
result += 1;
}
result
}
#[allow(dead_code)]
/// Direct translation of the C code
pub unsafe fn init_clock_asic_c(freq_hz: u32, duty_sram: u32) -> u32 {
use utra::sysctrl;
let daric_cgu = sysctrl::HW_SYSCTRL_BASE as *mut u32;
const UNIT_MHZ: u32 = 1000u32 * 1000u32;
const PFD_F_MHZ: u32 = 16;
const FREQ_0: u32 = 16u32 * 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
];
if (0 == (daric_cgu.add(sysctrl::SFR_IPCPLLMN.offset()).read_volatile() & 0x0001F000))
|| (0 == (daric_cgu.add(sysctrl::SFR_IPCPLLMN.offset()).read_volatile() & 0x00000fff))
{
// for SIM, avoid div by 0 if unconfigurated
// , default VCO 48MHz / 48 * 1200 = 1.2GHz
// TODO magic numbers
daric_cgu
.add(sysctrl::SFR_IPCPLLMN.offset())
.write_volatile(((M << 12) & 0x0001F000) | ((1200) & 0x00000fff));
daric_cgu.add(sysctrl::SFR_IPCPLLF.offset()).write_volatile(0); // ??
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
daric_cgu.add(sysctrl::SFR_IPCARIPFLOW.offset()).write_volatile(0x32); // commit
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
// TODO select int/ext osc/xtal
daric_cgu.add(sysctrl::SFR_CGUSEL1.offset()).write_volatile(1); // 0: RC, 1: XTAL
daric_cgu.add(sysctrl::SFR_CGUFSCR.offset()).write_volatile(FREQ_OSC_MHZ); // external crystal is 48MHz
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
daric_cgu.add(sysctrl::SFR_CGUSET.offset()).write_volatile(0x32);
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
if freq_hz < 1000000 {
daric_cgu.add(sysctrl::SFR_IPCOSC.offset()).write_volatile(freq_hz);
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
daric_cgu.add(sysctrl::SFR_IPCARIPFLOW.offset()).write_volatile(0x32); // commit, must write 32
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
// switch to OSC
daric_cgu.add(sysctrl::SFR_CGUSEL0.offset()).write_volatile(0); // clktop sel, 0:clksys, 1:clkpll0
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
daric_cgu.add(sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); // commit
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
if freq_hz < 1000000 {
} else {
let f16_mhz_log2: usize = log_2(freq_hz / FREQ_0) as usize;
// PD PLL
daric_cgu
.add(sysctrl::SFR_IPCLPEN.offset())
.write_volatile(daric_cgu.add(sysctrl::SFR_IPCLPEN.offset()).read_volatile() | 0x02);
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
daric_cgu.add(sysctrl::SFR_IPCARIPFLOW.offset()).write_volatile(0x32); // commit, must write 32
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
// delay
for _ in 0..1024 {
unsafe { core::arch::asm!("nop") };
}
let n_fxp24: u64 = (((freq_hz as u64) << 24u64) * TBL_MUL[f16_mhz_log2] as u64
+ PFD_F_MHZ as u64 * UNIT_MHZ as u64 / 2u64)
/ (PFD_F_MHZ as u64 * UNIT_MHZ as u64); // rounded
let n_frac: u32 = (n_fxp24 & 0x00ffffff) as u32;
daric_cgu
.add(sysctrl::SFR_IPCPLLMN.offset())
.write_volatile(((M << 12) & 0x0001F000) | ((n_fxp24 >> 24) & 0x00000fff) as u32); // 0x1F598; // ??
daric_cgu
.add(sysctrl::SFR_IPCPLLF.offset())
.write_volatile(n_frac | if 0 == n_frac { 0 } else { 1 << 24 }); // ??
daric_cgu.add(sysctrl::SFR_IPCPLLQ.offset()).write_volatile(TBL_Q[f16_mhz_log2] as u32); // ?? TODO select DIV for VCO freq
// VCO bias CPP bias CPI bias
// 1 2 3
//DARIC_IPC->ipc = (3 << 6) | (5 << 3) | (5);
daric_cgu.add(sysctrl::SFR_IPCCR.offset()).write_volatile((1 << 6) | (2 << 3) | (3));
// daric_cgu.add(sysctrl::SFR_IPCCR.offset()).write_volatile((3 << 6) | (5 << 3) | (5));
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
daric_cgu.add(sysctrl::SFR_IPCARIPFLOW.offset()).write_volatile(0x32); // commit, must write 32
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
daric_cgu
.add(sysctrl::SFR_IPCLPEN.offset())
.write_volatile(daric_cgu.add(sysctrl::SFR_IPCLPEN.offset()).read_volatile() & !0x02);
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
daric_cgu.add(sysctrl::SFR_IPCARIPFLOW.offset()).write_volatile(0x32); // commit, must write 32
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
// delay
for _ in 0..1024 {
unsafe { core::arch::asm!("nop") };
}
//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));
crate::println!("PLL switchover");
// TODO wait/poll lock status?
daric_cgu.add(sysctrl::SFR_CGUSEL0.offset()).write_volatile(1); // clktop sel, 0:clksys, 1:clkpll0
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
daric_cgu.add(sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); // commit
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
crate::println!("PLL switchover done");
// 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);
}
daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0.offset()).write_volatile(0x7fff); // CPU
if 0 == duty_sram {
daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1.offset()).write_volatile(0x3f7f); // aclk
} else {
daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1.offset()).write_volatile(duty_sram);
}
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
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
daric_cgu.add(utra::sysctrl::SFR_CGUSET.offset()).write_volatile(0x32); // commit
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
// UDMACORE->CFG_CG = 0xffffffff; //everything on
// core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
100_000_000 // bodge for now
}