Skip to main content

strat9_bus_drivers/
sun50i_de2.rs

1use crate::{BusChild, BusDriver, BusError, PowerState, mmio::MmioRegion};
2use alloc::{string::String, vec::Vec};
3
4const COMPATIBLE: &[&str] = &["allwinner,sun50i-a64-de2"];
5
6pub struct Sun50iDe2 {
7    regs: MmioRegion,
8    power_state: PowerState,
9    sram_claimed: bool,
10    children: Vec<BusChild>,
11}
12
13impl Sun50iDe2 {
14    /// Creates a new instance.
15    pub fn new() -> Self {
16        Self {
17            regs: MmioRegion::new(),
18            power_state: PowerState::Off,
19            sram_claimed: false,
20            children: Vec::new(),
21        }
22    }
23
24    /// Performs the claim sram operation.
25    pub fn claim_sram(&mut self) -> Result<(), BusError> {
26        self.sram_claimed = true;
27        Ok(())
28    }
29
30    /// Performs the release sram operation.
31    pub fn release_sram(&mut self) {
32        self.sram_claimed = false;
33    }
34
35    /// Performs the add child operation.
36    pub fn add_child(&mut self, child: BusChild) {
37        self.children.push(child);
38    }
39}
40
41impl BusDriver for Sun50iDe2 {
42    /// Performs the name operation.
43    fn name(&self) -> &str {
44        "sun50i-de2"
45    }
46
47    /// Performs the compatible operation.
48    fn compatible(&self) -> &[&str] {
49        COMPATIBLE
50    }
51
52    /// Performs the init operation.
53    fn init(&mut self, base: usize) -> Result<(), BusError> {
54        self.regs.init(base, 0x1000);
55        self.claim_sram()?;
56        self.power_state = PowerState::On;
57        Ok(())
58    }
59
60    /// Performs the shutdown operation.
61    fn shutdown(&mut self) -> Result<(), BusError> {
62        self.release_sram();
63        self.power_state = PowerState::Off;
64        Ok(())
65    }
66
67    /// Reads reg.
68    fn read_reg(&self, offset: usize) -> Result<u32, BusError> {
69        if !self.regs.is_valid() {
70            return Err(BusError::InitFailed);
71        }
72        Ok(self.regs.read32(offset))
73    }
74
75    /// Writes reg.
76    fn write_reg(&mut self, offset: usize, value: u32) -> Result<(), BusError> {
77        if !self.regs.is_valid() {
78            return Err(BusError::InitFailed);
79        }
80        self.regs.write32(offset, value);
81        Ok(())
82    }
83
84    /// Performs the children operation.
85    fn children(&self) -> Vec<BusChild> {
86        self.children.clone()
87    }
88}