mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
kernel: split precursor and sama5d2 platform modules
This commit is contained in:
parent
2ecd91b3c5
commit
38a02f9dbb
11 changed files with 670 additions and 18 deletions
243
kernel/src/platform/atsama5d2/aic.rs
Normal file
243
kernel/src/platform/atsama5d2/aic.rs
Normal file
|
|
@ -0,0 +1,243 @@
|
|||
use core::convert::TryFrom;
|
||||
|
||||
use crate::arch::irq::_irq_handler_rust;
|
||||
use crate::mem::MemoryManager;
|
||||
|
||||
use atsama5d27::{
|
||||
aic::{Aic, InterruptEntry, IrqPendingInfo, SourceKind},
|
||||
pmc::PeripheralId,
|
||||
};
|
||||
use utralib::{HW_AIC_BASE, HW_SAIC_BASE};
|
||||
use xous_kernel::{arch::irq::IrqNumber, MemoryFlags, MemoryType, PID};
|
||||
|
||||
pub const AIC_KERNEL_ADDR: usize = 0xffca_0000;
|
||||
pub static mut AIC_KERNEL: Option<AicKernel> = None;
|
||||
|
||||
pub const SAIC_KERNEL_ADDR: usize = 0xffcb_0000; // Secure version of AIC
|
||||
pub static mut SAIC_KERNEL: Option<AicKernel> = None;
|
||||
|
||||
unsafe extern "C" fn spurious_interrupt_handler() {
|
||||
core::arch::asm!("bkpt");
|
||||
}
|
||||
|
||||
pub struct AicKernel {
|
||||
base_addr: usize,
|
||||
pub inner: Option<Aic>,
|
||||
}
|
||||
|
||||
impl AicKernel {
|
||||
pub fn new(addr: usize) -> AicKernel {
|
||||
AicKernel {
|
||||
base_addr: addr,
|
||||
inner: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(&mut self) {
|
||||
let mut aic = Aic::with_alt_base_addr(self.base_addr as u32);
|
||||
aic.init();
|
||||
aic.set_spurious_handler_fn_ptr(
|
||||
spurious_interrupt_handler as unsafe extern "C" fn() as usize,
|
||||
);
|
||||
self.inner = Some(aic);
|
||||
}
|
||||
|
||||
pub fn enable_interrupt(&mut self, kind: SourceKind, id: PeripheralId) {
|
||||
if let Some(aic) = &mut self.inner {
|
||||
let handler = InterruptEntry {
|
||||
peripheral_id: id,
|
||||
vector_fn_ptr: _irq_handler_rust as unsafe extern "C" fn() as usize,
|
||||
kind,
|
||||
priority: 0,
|
||||
};
|
||||
aic.set_interrupt_handler(handler);
|
||||
} else {
|
||||
panic!("AIC is not initialized")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn disable_interrupt(&mut self, id: PeripheralId) {
|
||||
if let Some(aic) = &mut self.inner {
|
||||
aic.set_interrupt_enabled(id, false);
|
||||
} else {
|
||||
panic!("AIC is not initialized")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_pending_irqs(&self) -> IrqPendingInfo {
|
||||
if let Some(aic) = &self.inner {
|
||||
aic.get_pending_irqs()
|
||||
} else {
|
||||
panic!("AIC is not initialized")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn interrupt_completed(&mut self) {
|
||||
if let Some(aic) = &mut self.inner {
|
||||
aic.interrupt_completed()
|
||||
} else {
|
||||
panic!("AIC is not initialized")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
MemoryManager::with_mut(|memory_manager| {
|
||||
memory_manager
|
||||
.map_range(
|
||||
HW_AIC_BASE as *mut u8,
|
||||
(AIC_KERNEL_ADDR & !4095) as *mut u8,
|
||||
0x4000, // 16K
|
||||
PID::new(1).unwrap(),
|
||||
MemoryFlags::R | MemoryFlags::W | MemoryFlags::DEV,
|
||||
MemoryType::Default,
|
||||
)
|
||||
.expect("unable to map PMC_KERNEL")
|
||||
});
|
||||
MemoryManager::with_mut(|memory_manager| {
|
||||
memory_manager
|
||||
.map_range(
|
||||
HW_SAIC_BASE as *mut u8,
|
||||
(SAIC_KERNEL_ADDR & !4095) as *mut u8,
|
||||
0x4000, // 16K
|
||||
PID::new(1).unwrap(),
|
||||
MemoryFlags::R | MemoryFlags::W | MemoryFlags::DEV,
|
||||
MemoryType::Default,
|
||||
)
|
||||
.expect("unable to map PMC_KERNEL")
|
||||
});
|
||||
|
||||
let mut aic_kernel = AicKernel::new(AIC_KERNEL_ADDR);
|
||||
aic_kernel.init();
|
||||
|
||||
unsafe {
|
||||
AIC_KERNEL = Some(aic_kernel);
|
||||
}
|
||||
|
||||
let mut saic_kernel = AicKernel::new(SAIC_KERNEL_ADDR);
|
||||
saic_kernel.init();
|
||||
|
||||
unsafe {
|
||||
SAIC_KERNEL = Some(saic_kernel);
|
||||
}
|
||||
}
|
||||
|
||||
fn peripheral_id_to_irq_no(id: PeripheralId) -> Option<IrqNumber> {
|
||||
klog!("Pending IRQ: {:?}", id);
|
||||
|
||||
match id {
|
||||
PeripheralId::Pit => Some(IrqNumber::PeriodicIntervalTimer),
|
||||
|
||||
PeripheralId::Uart0 => Some(IrqNumber::Uart0),
|
||||
PeripheralId::Uart1 => Some(IrqNumber::Uart1),
|
||||
PeripheralId::Uart2 => Some(IrqNumber::Uart2),
|
||||
PeripheralId::Uart3 => Some(IrqNumber::Uart3),
|
||||
PeripheralId::Uart4 => Some(IrqNumber::Uart4),
|
||||
|
||||
PeripheralId::Pioa => Some(IrqNumber::Pioa),
|
||||
PeripheralId::Piob => Some(IrqNumber::Piob),
|
||||
PeripheralId::Pioc => Some(IrqNumber::Pioc),
|
||||
PeripheralId::Piod => Some(IrqNumber::Piod),
|
||||
|
||||
PeripheralId::Isi => Some(IrqNumber::Isi),
|
||||
PeripheralId::Lcdc => Some(IrqNumber::Lcdc),
|
||||
|
||||
PeripheralId::Uhphs => Some(IrqNumber::Uhphs),
|
||||
PeripheralId::Udphs => Some(IrqNumber::Udphs),
|
||||
|
||||
PeripheralId::Tc0 => Some(IrqNumber::Tc0),
|
||||
PeripheralId::Tc1 => Some(IrqNumber::Tc1),
|
||||
|
||||
_ => return None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_irq_enabled(irq_no: IrqNumber, enabled: bool) {
|
||||
let (sama5d2_irq_no, source_kind) = match irq_no {
|
||||
IrqNumber::PeriodicIntervalTimer => (PeripheralId::Pit, SourceKind::LevelSensitive),
|
||||
|
||||
IrqNumber::Uart0 => (PeripheralId::Uart0, SourceKind::LevelSensitive),
|
||||
IrqNumber::Uart1 => (PeripheralId::Uart1, SourceKind::LevelSensitive),
|
||||
IrqNumber::Uart2 => (PeripheralId::Uart2, SourceKind::LevelSensitive),
|
||||
IrqNumber::Uart3 => (PeripheralId::Uart3, SourceKind::LevelSensitive),
|
||||
IrqNumber::Uart4 => (PeripheralId::Uart3, SourceKind::LevelSensitive),
|
||||
|
||||
IrqNumber::Pioa => (PeripheralId::Pioa, SourceKind::LevelSensitive),
|
||||
IrqNumber::Piob => (PeripheralId::Piob, SourceKind::LevelSensitive),
|
||||
IrqNumber::Pioc => (PeripheralId::Pioc, SourceKind::LevelSensitive),
|
||||
IrqNumber::Piod => (PeripheralId::Piod, SourceKind::LevelSensitive),
|
||||
|
||||
IrqNumber::Isi => (PeripheralId::Isi, SourceKind::LevelSensitive),
|
||||
IrqNumber::Lcdc => (PeripheralId::Lcdc, SourceKind::LevelSensitive),
|
||||
|
||||
IrqNumber::Uhphs => (PeripheralId::Uhphs, SourceKind::LevelSensitive),
|
||||
IrqNumber::Udphs => (PeripheralId::Udphs, SourceKind::LevelSensitive),
|
||||
|
||||
IrqNumber::Tc0 => (PeripheralId::Tc0, SourceKind::LevelSensitive),
|
||||
IrqNumber::Tc1 => (PeripheralId::Tc1, SourceKind::LevelSensitive),
|
||||
};
|
||||
|
||||
unsafe {
|
||||
let aic = AIC_KERNEL.as_mut().expect("AIC is not initialized");
|
||||
|
||||
if enabled {
|
||||
aic.enable_interrupt(source_kind, sama5d2_irq_no);
|
||||
} else {
|
||||
aic.disable_interrupt(sama5d2_irq_no);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_pending_irqs() -> usize {
|
||||
let pending_irqs = unsafe {
|
||||
let aic = AIC_KERNEL.as_mut().expect("AIC is not initialized");
|
||||
aic.get_pending_irqs()
|
||||
};
|
||||
|
||||
let mut pending_mask = 0;
|
||||
for i in 0..32_u32 {
|
||||
if pending_irqs.irqs_0_31 & (1 << i) != 0 {
|
||||
if let Ok(id) = PeripheralId::try_from(i as u8) {
|
||||
if let Some(irq_no) = peripheral_id_to_irq_no(id) {
|
||||
pending_mask |= 1 << irq_no as usize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in 32..63_u32 {
|
||||
if pending_irqs.irqs_32_63 & (1 << (i - 32)) != 0 {
|
||||
if let Ok(id) = PeripheralId::try_from(i as u8) {
|
||||
if let Some(irq_no) = peripheral_id_to_irq_no(id) {
|
||||
pending_mask |= 1 << irq_no as usize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in 64..95_u32 {
|
||||
if pending_irqs.irqs_64_95 & (1 << (i - 64)) != 0 {
|
||||
if let Ok(id) = PeripheralId::try_from(i as u8) {
|
||||
if let Some(irq_no) = peripheral_id_to_irq_no(id) {
|
||||
pending_mask |= 1 << irq_no as usize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in 96..127_u32 {
|
||||
if pending_irqs.irqs_96_127 & (1 << (i - 96)) != 0 {
|
||||
if let Ok(id) = PeripheralId::try_from(i as u8) {
|
||||
if let Some(irq_no) = peripheral_id_to_irq_no(id) {
|
||||
pending_mask |= 1 << irq_no as usize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pending_mask
|
||||
}
|
||||
|
||||
pub fn acknowledge_irq() {
|
||||
unsafe {
|
||||
let aic = AIC_KERNEL.as_mut().expect("AIC is not initialized");
|
||||
aic.interrupt_completed();
|
||||
};
|
||||
}
|
||||
28
kernel/src/platform/atsama5d2/mod.rs
Normal file
28
kernel/src/platform/atsama5d2/mod.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// SPDX-FileCopyrightText: 2022 Foundation Devices, Inc. <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
pub mod aic;
|
||||
pub mod pmc;
|
||||
pub mod rand;
|
||||
pub mod uart;
|
||||
pub mod pio;
|
||||
|
||||
/// atsama5d2 specific initialization.
|
||||
pub fn init() {
|
||||
// The order of these init calls is important,
|
||||
// don't rearrange them if you don't know what you're doing!
|
||||
|
||||
self::pmc::init();
|
||||
self::pmc::enable_tc0();
|
||||
self::pmc::enable_pio();
|
||||
self::pmc::enable_aic();
|
||||
|
||||
self::pio::init();
|
||||
self::pio::init_lcd_pins();
|
||||
|
||||
self::pmc::enable_lcdc();
|
||||
|
||||
self::aic::init();
|
||||
self::rand::init();
|
||||
self::uart::init();
|
||||
}
|
||||
46
kernel/src/platform/atsama5d2/pio.rs
Normal file
46
kernel/src/platform/atsama5d2/pio.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
// SPDX-FileCopyrightText: 2022 Foundation Devices, Inc. <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::mem::MemoryManager;
|
||||
|
||||
use atsama5d27::pio::{PioA, PioB, PioC, PioD, Func, PioPort, Direction};
|
||||
use utralib::HW_PIO_BASE;
|
||||
use xous_kernel::{MemoryFlags, MemoryType, PID};
|
||||
|
||||
/// The manually chosen virtual address has to be in the top 4MiB as it is the
|
||||
/// only page shared among all processes.
|
||||
///
|
||||
/// See https://github.com/betrusted-io/xous-core/blob/master/docs/memory.md
|
||||
pub const PIO_KERNEL_ADDR: usize = 0xffcd_0000;
|
||||
|
||||
/// Initialize PIO driver.
|
||||
///
|
||||
/// Needed so that the kernel can manage peripherals.
|
||||
pub fn init() {
|
||||
MemoryManager::with_mut(|memory_manager| {
|
||||
memory_manager
|
||||
.map_range(
|
||||
HW_PIO_BASE as *mut u8,
|
||||
(PIO_KERNEL_ADDR & !4095) as *mut u8,
|
||||
0x4000, // 16K
|
||||
PID::new(1).unwrap(),
|
||||
MemoryFlags::R | MemoryFlags::W | MemoryFlags::DEV,
|
||||
MemoryType::Default,
|
||||
)
|
||||
.expect("unable to map PIO to kernel")
|
||||
});
|
||||
}
|
||||
|
||||
/// *NOTE*: this assumes that PIO memory mapping was initialized beforehand.
|
||||
pub fn init_lcd_pins() {
|
||||
let addr = PIO_KERNEL_ADDR as u32;
|
||||
|
||||
PioA::configure_pins_by_mask(Some(addr), 1 << 10, Func::Gpio, Direction::Output);
|
||||
PioA::clear_all(Some(addr));
|
||||
|
||||
PioB::configure_pins_by_mask(Some(addr), 0xe7e7e000, Func::A, None);
|
||||
PioB::configure_pins_by_mask(Some(addr), 0x2, Func::A, None);
|
||||
PioB::clear_all(Some(addr));
|
||||
PioC::configure_pins_by_mask(Some(addr), 0x1ff, Func::A, None);
|
||||
PioD::configure_pins_by_mask(Some(addr), 0x30, Func::A, None);
|
||||
}
|
||||
121
kernel/src/platform/atsama5d2/pmc.rs
Normal file
121
kernel/src/platform/atsama5d2/pmc.rs
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
// SPDX-FileCopyrightText: 2022 Foundation Devices, Inc. <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::mem::MemoryManager;
|
||||
|
||||
use atsama5d27::pmc::{Pmc, PeripheralId};
|
||||
use utralib::HW_PMC_BASE;
|
||||
use xous_kernel::{MemoryFlags, MemoryType, PID};
|
||||
|
||||
/// The manually chosen virtual address has to be in the top 4MiB as it is the
|
||||
/// only page shared among all processes.
|
||||
///
|
||||
/// See https://github.com/betrusted-io/xous-core/blob/master/docs/memory.md
|
||||
pub const PMC_KERNEL_ADDR: usize = 0xffcc_0000;
|
||||
pub static mut PMC_KERNEL: Option<PmcKernel> = None;
|
||||
|
||||
pub struct PmcKernel {
|
||||
base_addr: usize,
|
||||
pub inner: Option<Pmc>,
|
||||
}
|
||||
|
||||
impl PmcKernel {
|
||||
pub fn new(addr: usize) -> PmcKernel {
|
||||
PmcKernel {
|
||||
base_addr: addr,
|
||||
inner: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(&mut self) {
|
||||
self.inner = Some(Pmc::with_alt_base_addr(self.base_addr as u32));
|
||||
}
|
||||
|
||||
pub fn enable_peripheral_clock(&mut self, periph: PeripheralId) {
|
||||
if let Some(pmc) = &mut self.inner {
|
||||
return pmc.enable_peripheral_clock(periph);
|
||||
}
|
||||
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize PMC driver.
|
||||
///
|
||||
/// Needed so that the kernel can manage peripherals.
|
||||
pub fn init() {
|
||||
MemoryManager::with_mut(|memory_manager| {
|
||||
memory_manager
|
||||
.map_range(
|
||||
HW_PMC_BASE as *mut u8,
|
||||
(PMC_KERNEL_ADDR & !4095) as *mut u8,
|
||||
0x4000, // 16K
|
||||
PID::new(1).unwrap(),
|
||||
MemoryFlags::R | MemoryFlags::W | MemoryFlags::DEV,
|
||||
MemoryType::Default,
|
||||
)
|
||||
.expect("unable to map PMC_KERNEL")
|
||||
});
|
||||
|
||||
let mut pmc_kernel = PmcKernel::new(PMC_KERNEL_ADDR);
|
||||
pmc_kernel.init();
|
||||
|
||||
unsafe {
|
||||
PMC_KERNEL = Some(pmc_kernel);
|
||||
}
|
||||
}
|
||||
|
||||
/// Enables `TRNG` peripheral to make it usable.
|
||||
pub fn enable_trng() {
|
||||
unsafe {
|
||||
PMC_KERNEL
|
||||
.as_mut()
|
||||
.expect("PMC_KERNEL driver not initialized")
|
||||
.enable_peripheral_clock(PeripheralId::Trng)
|
||||
}
|
||||
}
|
||||
|
||||
/// Enables `AIC` peripheral to make it usable.
|
||||
pub fn enable_aic() {
|
||||
unsafe {
|
||||
PMC_KERNEL
|
||||
.as_mut()
|
||||
.expect("PMC_KERNEL driver not initialized")
|
||||
.enable_peripheral_clock(PeripheralId::Aic)
|
||||
}
|
||||
}
|
||||
|
||||
/// Enables `TC0` peripheral to make it usable.
|
||||
pub fn enable_tc0() {
|
||||
unsafe {
|
||||
PMC_KERNEL
|
||||
.as_mut()
|
||||
.expect("PMC_KERNEL driver not initialized")
|
||||
.enable_peripheral_clock(PeripheralId::Tc0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Enables `LCDC` peripheral to make it usable.
|
||||
pub fn enable_lcdc() {
|
||||
unsafe {
|
||||
PMC_KERNEL
|
||||
.as_mut()
|
||||
.expect("PMC_KERNEL driver not initialized")
|
||||
.enable_peripheral_clock(PeripheralId::Lcdc)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Enables `PIOx` peripherals to make them usable.
|
||||
pub fn enable_pio() {
|
||||
unsafe {
|
||||
let pmc = PMC_KERNEL
|
||||
.as_mut()
|
||||
.expect("PMC_KERNEL driver not initialized");
|
||||
|
||||
pmc.enable_peripheral_clock(PeripheralId::Pioa);
|
||||
pmc.enable_peripheral_clock(PeripheralId::Piob);
|
||||
pmc.enable_peripheral_clock(PeripheralId::Pioc);
|
||||
pmc.enable_peripheral_clock(PeripheralId::Piod);
|
||||
}
|
||||
}
|
||||
90
kernel/src/platform/atsama5d2/rand.rs
Normal file
90
kernel/src/platform/atsama5d2/rand.rs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
// SPDX-FileCopyrightText: 2022 Foundation Devices, Inc. <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::mem::MemoryManager;
|
||||
|
||||
use atsama5d27::trng::{Enabled, StatefulTrng, Trng};
|
||||
use utralib::HW_TRNG_BASE;
|
||||
use xous_kernel::{MemoryFlags, MemoryType, PID};
|
||||
|
||||
/// The manually chosen virtual address has to be in the top 4MiB as it is the
|
||||
/// only page shared among all processes.
|
||||
///
|
||||
/// See https://github.com/betrusted-io/xous-core/blob/master/docs/memory.md
|
||||
pub const TRNG_KERNEL_ADDR: usize = 0xffce_0000;
|
||||
pub static mut TRNG_KERNEL: Option<TrngKernel> = None;
|
||||
|
||||
pub struct TrngKernel {
|
||||
base_addr: usize,
|
||||
pub inner: Option<StatefulTrng<Enabled>>,
|
||||
}
|
||||
|
||||
impl TrngKernel {
|
||||
pub fn new(addr: usize) -> TrngKernel {
|
||||
TrngKernel {
|
||||
base_addr: addr,
|
||||
inner: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(&mut self) {
|
||||
self.inner = Some(Trng::with_alt_base_addr(self.base_addr as u32).enable());
|
||||
}
|
||||
|
||||
pub fn get_u32(&mut self) -> u32 {
|
||||
if let Some(trng) = &self.inner {
|
||||
return trng.read_u32();
|
||||
}
|
||||
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize TRNG driver.
|
||||
///
|
||||
/// Needed so that the kernel can allocate names.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// If `pmc::init()` hasn't called prior to calling this function.
|
||||
pub fn init() {
|
||||
// PMC driver should be initialized before.
|
||||
super::pmc::enable_trng();
|
||||
|
||||
// Map the TRNG so that we can allocate names
|
||||
// hardware guarantees that:
|
||||
// - TRNG will automatically power on
|
||||
// - Both TRNGs are enabled, with conservative defaults
|
||||
// - Kernel FIFO will fill with TRNGs such that at least the next 512 calls to get_u32() will succeed without delay
|
||||
// - The kernel will start a TRNG server
|
||||
// - All further security decisions and policies are 100% delegated to this new server.
|
||||
MemoryManager::with_mut(|memory_manager| {
|
||||
memory_manager
|
||||
.map_range(
|
||||
HW_TRNG_BASE as *mut u8,
|
||||
(TRNG_KERNEL_ADDR & !4095) as *mut u8,
|
||||
0x4000, // 16K
|
||||
PID::new(1).unwrap(),
|
||||
MemoryFlags::R | MemoryFlags::W | MemoryFlags::DEV,
|
||||
MemoryType::Default,
|
||||
)
|
||||
.expect("unable to map TRNG_KERNEL")
|
||||
});
|
||||
|
||||
let mut trng_kernel = TrngKernel::new(TRNG_KERNEL_ADDR);
|
||||
trng_kernel.init();
|
||||
|
||||
unsafe {
|
||||
TRNG_KERNEL = Some(trng_kernel);
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieve random `u32`.
|
||||
pub fn get_u32() -> u32 {
|
||||
unsafe {
|
||||
TRNG_KERNEL
|
||||
.as_mut()
|
||||
.expect("TRNG_KERNEL driver not initialized")
|
||||
.get_u32()
|
||||
}
|
||||
}
|
||||
102
kernel/src/platform/atsama5d2/uart.rs
Normal file
102
kernel/src/platform/atsama5d2/uart.rs
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
// SPDX-FileCopyrightText: 2022 Foundation Devices, Inc. <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::{
|
||||
debug::shell::process_characters,
|
||||
io::{SerialRead, SerialWrite},
|
||||
mem::MemoryManager,
|
||||
PID,
|
||||
};
|
||||
use atsama5d27::uart::{Uart as UartHw, Uart1, UART_BASE_ADDRESS};
|
||||
use xous_kernel::{MemoryFlags, MemoryType, arch::irq::IrqNumber};
|
||||
|
||||
const UART_NUMBER: usize = 1;
|
||||
type UartType = UartHw<Uart1>; // Make sure this matches the UART_NUMBER above
|
||||
const UART_IRQ_NUM: IrqNumber = IrqNumber::Uart1;
|
||||
|
||||
pub const HW_UART_BASE: u32 = UART_BASE_ADDRESS[UART_NUMBER];
|
||||
|
||||
/// UART virtual address.
|
||||
///
|
||||
/// See https://github.com/betrusted-io/xous-core/blob/master/docs/memory.md
|
||||
pub const UART_ADDR: usize = 0xffcf_0000;
|
||||
|
||||
/// UART instance.
|
||||
///
|
||||
/// Initialized by [`init`].
|
||||
pub static mut UART: Option<Uart> = None;
|
||||
|
||||
/// UART peripheral driver.
|
||||
pub struct Uart {
|
||||
uart_csr: UartType,
|
||||
callback: fn(&mut Self),
|
||||
}
|
||||
|
||||
impl Uart {
|
||||
pub fn new(addr: usize, callback: fn(&mut Self)) -> Uart {
|
||||
Uart {
|
||||
uart_csr: UartHw::with_alt_base_addr(addr as u32),
|
||||
callback,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(&mut self) {
|
||||
// no-op, it should be already initialized by the 2nd stage bootloader
|
||||
}
|
||||
|
||||
pub fn irq(_irq_number: usize, arg: *mut usize) {
|
||||
let uart = unsafe { &mut *(arg as *mut Uart) };
|
||||
(uart.callback)(uart);
|
||||
}
|
||||
|
||||
pub fn enable_rx_irq(&mut self) {
|
||||
self.uart_csr.set_rx_interrupt(true);
|
||||
self.uart_csr.set_rx(true);
|
||||
}
|
||||
}
|
||||
|
||||
impl SerialWrite for Uart {
|
||||
fn putc(&mut self, c: u8) {
|
||||
self.uart_csr.write_byte(c);
|
||||
}
|
||||
}
|
||||
|
||||
impl SerialRead for Uart {
|
||||
fn getc(&mut self) -> Option<u8> {
|
||||
self.uart_csr.getc_nonblocking()
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize UART driver and debug shell.
|
||||
pub fn init() {
|
||||
// Map the UART peripheral.
|
||||
MemoryManager::with_mut(|memory_manager| {
|
||||
memory_manager
|
||||
.map_range(
|
||||
HW_UART_BASE as *mut u8,
|
||||
(UART_ADDR & !4095) as *mut u8,
|
||||
0x4000, // 16K
|
||||
PID::new(1).unwrap(),
|
||||
MemoryFlags::R | MemoryFlags::W | MemoryFlags::DEV,
|
||||
MemoryType::Default,
|
||||
)
|
||||
.expect("unable to map serial port")
|
||||
});
|
||||
|
||||
let mut uart = Uart::new(UART_ADDR, process_characters);
|
||||
uart.init();
|
||||
|
||||
unsafe {
|
||||
UART = Some(uart);
|
||||
crate::debug::shell::init(UART.as_mut().unwrap());
|
||||
|
||||
// Claim UART interrupt
|
||||
klog!("Claiming IRQ {:?} via syscall...", UART_IRQ_NUM);
|
||||
xous_kernel::claim_interrupt(
|
||||
UART_IRQ_NUM as usize,
|
||||
Uart::irq,
|
||||
(UART.as_mut().unwrap() as *mut Uart) as *mut usize,
|
||||
).expect("Couldn't claim debug interrupts");
|
||||
(UART.as_mut().unwrap() as &mut Uart).enable_rx_irq();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +1,20 @@
|
|||
// SPDX-FileCopyrightText: 2022 Foundation Devices, Inc. <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#[cfg(any(feature="precursor", feature="renode"))]
|
||||
#[cfg(any(feature = "precursor", feature = "renode"))]
|
||||
pub mod precursor;
|
||||
|
||||
#[cfg(any(feature = "atsama5d27"))]
|
||||
pub mod atsama5d2;
|
||||
|
||||
pub mod rand;
|
||||
|
||||
/// Platform specific initialization.
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
pub fn init() {
|
||||
#[cfg(any(feature="precursor", feature="renode"))]
|
||||
#[cfg(any(feature = "precursor", feature = "renode"))]
|
||||
self::precursor::init();
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "atsama5d27"))]
|
||||
self::atsama5d2::init();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
// SPDX-FileCopyrightText: 2022 Foundation Devices, Inc. <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
pub mod rand;
|
||||
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
|
||||
pub mod uart;
|
||||
pub mod rand;
|
||||
|
||||
/// Precursor specific initialization.
|
||||
pub fn init() {
|
||||
self::rand::init();
|
||||
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
|
||||
self::uart::init();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,19 +25,25 @@ impl TrngKernel {
|
|||
}
|
||||
|
||||
pub fn init(&mut self) {
|
||||
if false { // raw random path - left in for debugging urandom, can strip out later
|
||||
if false {
|
||||
// raw random path - left in for debugging urandom, can strip out later
|
||||
while self.trng_kernel_csr.rf(utra::trng_kernel::STATUS_AVAIL) == 0 {}
|
||||
// discard the first entry, as it is always 0x0000_0000
|
||||
// this is because the read register is a pipeline stage behind the FIFO
|
||||
// once the pipeline has been filled, there is no need to prime it again.
|
||||
self.trng_kernel_csr.rf(utra::trng_kernel::DATA_DATA);
|
||||
} else { // urandom path (recommended)
|
||||
} else {
|
||||
// urandom path (recommended)
|
||||
// simulations show this isn't strictly necessary, but I prefer to have it
|
||||
// just in case a subtle bug in the reset logic leaves something deterministic
|
||||
// in the connecting logic: the simulation coverage stops at the edge of the TRNG block.
|
||||
for _ in 0..4 {
|
||||
// wait until the urandom port is initialized
|
||||
while self.trng_kernel_csr.rf(utra::trng_kernel::URANDOM_VALID_URANDOM_VALID) == 0 {}
|
||||
while self
|
||||
.trng_kernel_csr
|
||||
.rf(utra::trng_kernel::URANDOM_VALID_URANDOM_VALID)
|
||||
== 0
|
||||
{}
|
||||
// pull a dummy piece of data
|
||||
self.trng_kernel_csr.rf(utra::trng_kernel::URANDOM_URANDOM);
|
||||
}
|
||||
|
|
@ -45,18 +51,24 @@ impl TrngKernel {
|
|||
}
|
||||
|
||||
pub fn get_u32(&mut self) -> u32 {
|
||||
if false { // raw random path
|
||||
if false {
|
||||
// raw random path
|
||||
while self.trng_kernel_csr.rf(utra::trng_kernel::STATUS_AVAIL) == 0 {}
|
||||
self.trng_kernel_csr.rf(utra::trng_kernel::DATA_DATA)
|
||||
} else { // urandom path (recommended)
|
||||
while self.trng_kernel_csr.rf(utra::trng_kernel::URANDOM_VALID_URANDOM_VALID) == 0 {}
|
||||
} else {
|
||||
// urandom path (recommended)
|
||||
while self
|
||||
.trng_kernel_csr
|
||||
.rf(utra::trng_kernel::URANDOM_VALID_URANDOM_VALID)
|
||||
== 0
|
||||
{}
|
||||
self.trng_kernel_csr.rf(utra::trng_kernel::URANDOM_URANDOM)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize TRNG driver.
|
||||
///
|
||||
///
|
||||
/// Needed so that the kernel can allocate names.
|
||||
pub fn init() {
|
||||
// Map the TRNG so that we can allocate names
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::{
|
||||
io::{SerialWrite, SerialRead},
|
||||
mem::MemoryManager,
|
||||
debug::shell::process_characters,
|
||||
io::{SerialRead, SerialWrite},
|
||||
mem::MemoryManager,
|
||||
PID,
|
||||
};
|
||||
use utralib::generated::*;
|
||||
|
|
@ -17,7 +17,7 @@ use xous_kernel::{MemoryFlags, MemoryType};
|
|||
pub const UART_ADDR: usize = 0xffcf_0000;
|
||||
|
||||
/// UART instance.
|
||||
///
|
||||
///
|
||||
/// Initialized by [`init`].
|
||||
pub static mut UART: Option<Uart> = None;
|
||||
|
||||
|
|
@ -98,6 +98,7 @@ pub fn init() {
|
|||
utra::uart::UART_IRQ,
|
||||
Uart::irq,
|
||||
(UART.as_mut().unwrap() as *mut Uart) as *mut usize,
|
||||
).expect("Couldn't claim debug interrupt");
|
||||
)
|
||||
.expect("Couldn't claim debug interrupt");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,10 @@ pub fn get_u32() -> u32 {
|
|||
// hosted rand code is coupled with arch code.
|
||||
#[cfg(any(windows, unix))]
|
||||
let rand = crate::arch::rand::get_u32();
|
||||
#[cfg(any(feature="precursor", feature="renode"))]
|
||||
#[cfg(any(feature = "precursor", feature = "renode"))]
|
||||
let rand = crate::platform::precursor::rand::get_u32();
|
||||
#[cfg(any(feature = "atsama5d27"))]
|
||||
let rand = crate::platform::atsama5d2::rand::get_u32();
|
||||
|
||||
rand
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue