Skip to content

Commit eac61b8

Browse files
committed
Uncached address; DMAC design
1 parent fdf1ad6 commit eac61b8

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "k210-hal"
33
version = "0.2.0"
44
authors = ["The RISC-V Team <[email protected]>"]
55
categories = ["embedded", "hardware-support", "no-std"]
6-
description = "HAL for K210 SoC"
6+
description = "Hardware Abstract Layer (HAL) support for K210, dual-core RV64GC SoC"
77
repository = "https://github.com/riscv-rust/k210-hal"
88
keywords = ["riscv", "k210", "hal"]
99
license = "ISC"

src/cache.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//! (TODO) Bypass cache
2+
//!
3+
//! Todo: verify this module!
4+
5+
use core::slice;
6+
7+
/// Convert a buffer or a pointer into ones with uncached address.
8+
///
9+
/// Section 3.4.1, Kendryte K210 Datasheet
10+
pub fn uncached<T: Uncache>(buf: T) -> T {
11+
buf.uncache()
12+
}
13+
14+
/// Uncacheable buffer or pointer.
15+
pub trait Uncache {
16+
/// Convert this buffer or pointer to uncached addressed ones
17+
fn uncache(self) -> Self;
18+
}
19+
20+
impl<T> Uncache for &T {
21+
#[inline]
22+
fn uncache(self) -> Self {
23+
let addr = self as *const T as usize;
24+
assert_addr_cached(addr);
25+
// note(unsafe): safe for source address is safe
26+
unsafe { &*((addr - 0x4000_0000) as *const T) }
27+
}
28+
}
29+
30+
impl<T> Uncache for &mut T {
31+
#[inline]
32+
fn uncache(self) -> Self {
33+
let addr = self as *mut T as usize;
34+
assert_addr_cached(addr);
35+
// note(unsafe): safe for source address is safe
36+
unsafe { &mut *((addr - 0x4000_0000) as *mut T) }
37+
}
38+
}
39+
40+
impl<T> Uncache for &[T] {
41+
#[inline]
42+
fn uncache(self) -> Self {
43+
let addr = self.as_ptr() as usize;
44+
assert_addr_cached(addr);
45+
let new_ptr = (addr - 0x4000_0000) as *const T;
46+
// note(unsafe): source address is safe; passing ownership
47+
unsafe { slice::from_raw_parts(new_ptr, self.len()) }
48+
}
49+
}
50+
51+
impl<T> Uncache for &mut [T] {
52+
#[inline]
53+
fn uncache(self) -> Self {
54+
let addr = self.as_ptr() as usize;
55+
assert_addr_cached(addr);
56+
let new_ptr = (addr - 0x4000_0000) as *mut T;
57+
// note(unsafe): source address is safe; passing ownership
58+
unsafe { slice::from_raw_parts_mut(new_ptr, self.len()) }
59+
}
60+
}
61+
62+
impl<T> Uncache for *const T {
63+
#[inline]
64+
fn uncache(self) -> Self {
65+
assert_addr_cached(self as usize);
66+
(self as usize - 0x4000_0000) as *const T
67+
}
68+
}
69+
70+
impl<T> Uncache for *mut T {
71+
#[inline]
72+
fn uncache(self) -> Self {
73+
assert_addr_cached(self as usize);
74+
(self as usize - 0x4000_0000) as *mut T
75+
}
76+
}
77+
78+
#[inline]
79+
fn assert_addr_cached(addr: usize) {
80+
assert!(addr <= 0x805F_FFFF && addr >= 0x8000_0000);
81+
}

src/dmac.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
11
//! (TODO) Direct Memory Access Controller (DMAC)
2+
use crate::{pac, sysctl};
3+
4+
pub fn dmac_id() -> u64 {
5+
unsafe { (*pac::DMAC::ptr()).id.read().bits() }
6+
}
7+
8+
pub fn dmac_version() -> u64 {
9+
unsafe { (*pac::DMAC::ptr()).compver.read().bits() }
10+
}
11+
12+
pub trait DmacExt {
13+
fn configure(self, /* sysctl ACLK clock */) -> Dmac;
14+
}
15+
16+
impl DmacExt for pac::DMAC {
17+
fn configure(self, /* sysctl ACLK clock */) -> Dmac {
18+
// enable
19+
sysctl::clk_en_peri().modify(|_, w| w.dma_clk_en().set_bit());
20+
// todo: reset
21+
Dmac {} // todo
22+
}
23+
}
24+
25+
pub struct Dmac {
26+
27+
}
28+
29+
// pub struct C0 {
30+
// // todo
31+
// pub async fn poll() {
32+
33+
// }
34+
// }

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub use k210_pac as pac;
99

1010
pub mod aes;
1111
pub mod apu;
12+
pub mod cache;
1213
pub mod clint;
1314
pub mod clock;
1415
pub mod dmac;

0 commit comments

Comments
 (0)