add pl230 module

this will hopefully eventually be generic across any platform
that uses the pl230 dma module, which I have seen... on a couple
of platforms at least.
This commit is contained in:
bunnie 2023-07-17 18:26:46 +08:00
parent b64e8ee9e2
commit bdf785f7c7
5 changed files with 324 additions and 0 deletions

View file

@ -0,0 +1,24 @@
[package]
name = "xous-pl230"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4.14"
utralib = {version = "0.1.20", optional = true, default-features = false }
bitfield = "0.13.2"
[target.'cfg(target_os = "xous")'.dependencies]
xous = "0.9.47"
[features]
cramium-soc = ["utralib/cramium-soc"]
cramium-fpga = ["utralib/cramium-fpga"]
precursor = []
hosted = []
renode = []
tests = []
default = ["tests"]

View file

@ -0,0 +1,107 @@
use utralib::generated::*;
pub struct Uart {
// pub base: *mut u32,
}
#[allow(dead_code)]
impl Uart {
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 putc(&self, c: u8) {
let base = utra::duart::HW_DUART_BASE as *mut u32;
let mut uart = CSR::new(base);
while uart.r(utra::duart::SFR_SR) != 0 {}
uart.wo(utra::duart::SFR_TXD, c as u32);
}
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(())
}
}
#[macro_use]
#[cfg(all(not(test), feature = "debug-print"))]
pub mod debug_print_hardware {
#[macro_export]
macro_rules! print
{
($($args:tt)+) => ({
use core::fmt::Write;
let _ = write!(crate::debug::Uart {}, $($args)+);
});
}
}
#[macro_use]
#[cfg(all(not(test), not(feature = "debug-print")))]
mod debug_print_hardware {
#[macro_export]
#[allow(unused_variables)]
macro_rules! print {
($($args:tt)+) => ({
()
});
}
}
#[macro_use]
#[cfg(test)]
mod debug_print_hardware {
#[macro_export]
#[allow(unused_variables)]
macro_rules! print {
($($args:tt)+) => ({
std::print!($($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)+)
});
}

106
libs/xous-pl230/src/lib.rs Normal file
View file

@ -0,0 +1,106 @@
#![cfg_attr(target_os = "none", no_std)]
#[cfg(not(any(target_os="xous")))]
mod debug;
#[cfg(feature="tests")]
pub mod pl230_tests;
use utralib::*;
use bitfield::bitfield;
#[repr(C, align(16))]
#[derive(Default)]
struct ChannelControl {
pub src_end_ptr: u32,
pub dst_end_ptr: u32,
pub control: u32,
pub reserved: u32,
}
#[repr(C, align(256))]
#[derive(Default)]
struct ControlChannels {
pub channels: [ChannelControl; 8],
}
pub struct Pl230 {
pub csr: CSR<u32>,
}
impl Pl230 {
#[cfg(not(any(target_os="xous")))]
pub fn new() -> Self {
Pl230 {
csr: CSR::new(utralib::HW_PL230_BASE as *mut u32),
}
}
#[cfg(target_os="xous")]
pub fn new() -> Self {
// Note: this requires a memory region window to be manually specified in create-image
// so that the loader maps the pages for the PIO block. This is because the PIO block is
// an IP block that is created *outside* of the normal LiteX ecosystem. Specifically look in
// xtask/src/builder.rs for a "--extra-svd" argument that refers to precursors/pio.svd.
let csr = xous::syscall::map_memory(
xous::MemoryAddress::new(utralib::HW_PL230_BASE),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.unwrap();
Pl230 {
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
}
}
}
#[repr(u32)]
pub enum DmaWidth {
Byte = 0b00,
HalfWord = 0b01,
Word = 0b10,
NoInc = 0b11,
}
#[repr(u32)]
pub enum ArbitrateAfter {
XferEach = 0b0000,
Xfer2 = 0b0001,
Xfer4 = 0b0010,
Xfer8 = 0b0011,
Xfer16 = 0b0100,
Xfer32 = 0b0101,
Xfer64 = 0b0110,
Xfer128 = 0b0111,
Xfer256 = 0b1000,
Xfer512 = 0b1001,
Xfer1024 = 0b1010,
}
#[repr(u32)]
pub enum DmaCycleControl {
Stop = 0b000,
Basic = 0b001,
AutoRequest = 0b010,
PingPong = 0b011,
MemoryScatterGatherPrimary = 0b100,
MemoryScatterGatherAlt = 0b101,
PeripheralScatterGatherPrimary = 0b110,
PeripheralScatterGatherAlt = 0b111,
}
bitfield! {
#[derive(Copy, Clone)]
pub struct DmaChanControl(u32);
impl Debug;
pub cycle_ctrl, set_cycle_ctrl: 2, 0;
pub next_useburst, set_next_useburst: 3;
pub n_minus_1, set_n_minus_1: 13, 4;
pub r_power, set_r_power: 17, 14;
pub src_prot_ctrl, set_src_prot_ctrl: 20, 18;
pub dst_prot_ctrl, set_dst_prot_ctrl: 23, 21;
pub src_size, set_src_size: 25, 24;
pub src_inc, set_src_inc: 27, 26;
pub dst_size, set_dst_size: 29, 28;
pub dst_inc, set_dst_inc: 31, 30;
}

View file

@ -0,0 +1,17 @@
pub mod units;
pub fn report_api(desc: &str, d: u32) {
#[cfg(not(any(target_os="xous")))]
{
use core::fmt::Write;
let mut uart = crate::debug::Uart{};
writeln!(uart, "pl230: [{}] 0x{:x}", desc, d).ok();
}
#[cfg(target_os="xous")]
log::info!("pl230: [{}] 0x{:x}", desc, d);
}
pub fn pl230_tests() {
let mut pl230 = crate::Pl230::new();
units::basic_tests(&mut pl230);
}

View file

@ -0,0 +1,70 @@
use crate::*;
use super::report_api;
use utralib::*;
/// used to generate some test vectors
pub fn lfsr_next(state: u32) -> u32 {
let bit = ((state >> 31) ^
(state >> 21) ^
(state >> 1) ^
(state >> 0)) & 1;
(state << 1) + bit
}
pub fn basic_tests (pl230: &mut Pl230) {
report_api("channels", pl230.csr.rf(utra::pl230::STATUS_CHNLS_MINUS1) + 1);
//report_api("id0", pl230.csr.r(utra::pl230::PERIPH_ID_0));
//report_api("id1", pl230.csr.r(utra::pl230::PERIPH_ID_1));
//report_api("id2", pl230.csr.r(utra::pl230::PERIPH_ID_2));
let mut cc_struct = ControlChannels::default();
// read the status register
report_api("status", pl230.csr.r(utra::pl230::STATUS));
pl230.csr.wfo(utra::pl230::CFG_MASTER_ENABLE, 1); // enable
report_api("status after enable", pl230.csr.r(utra::pl230::STATUS));
// setup the PL230 to do a simple transfer between two memory regions
let mut region_a = [0u32; 16];
let region_b = [0u32; 16];
let mut state = 0x1111_1111;
for d in region_a.iter_mut() {
*d = state;
state = lfsr_next(state);
}
cc_struct.channels[0].dst_end_ptr = unsafe{region_b.as_ptr().add(region_b.len() - 1)} as u32;
cc_struct.channels[0].src_end_ptr = unsafe{region_a.as_ptr().add(region_a.len() - 1)} as u32;
let mut cc = DmaChanControl(0);
cc.set_src_size(DmaWidth::Word as u32);
cc.set_src_inc(DmaWidth::Word as u32);
cc.set_dst_size(DmaWidth::Word as u32);
cc.set_dst_inc(DmaWidth::Word as u32);
cc.set_r_power(ArbitrateAfter::Xfer8 as u32);
cc.set_n_minus_1(region_a.len() as u32 - 1);
cc.set_cycle_ctrl(DmaCycleControl::AutoRequest as u32);
cc_struct.channels[0].control = cc.0;
pl230.csr.wo(utra::pl230::CTRLBASEPTR, cc_struct.channels.as_ptr() as u32);
pl230.csr.wo(utra::pl230::CHNLREQMASKSET, 1);
pl230.csr.wo(utra::pl230::CHNLENABLESET, 1);
report_api("baseptr", cc_struct.channels.as_ptr() as u32);
report_api("baseptr[0]", unsafe{cc_struct.channels.as_ptr().read()}.src_end_ptr);
report_api("baseptr[1]", unsafe{cc_struct.channels.as_ptr().read()}.dst_end_ptr);
report_api("baseptr[2]", unsafe{cc_struct.channels.as_ptr().read()}.control);
report_api("baseptr[3]", unsafe{cc_struct.channels.as_ptr().read()}.reserved);
report_api("baseptr reg", pl230.csr.r(utra::pl230::CTRLBASEPTR));
// this should kick off the DMA
pl230.csr.wo(utra::pl230::CHNLSWREQUEST, 1);
let mut timeout = 0;
while (DmaChanControl(cc_struct.channels[0].control).cycle_ctrl() != 0) && timeout < 16 {
report_api("dma progress ", cc_struct.channels[0].control);
timeout += 1;
}
for d in region_b {
report_api("b: ", d);
}
}