Skip to main content

strat9_bus_drivers/
simple_pm_bus.rs

1use crate::{BusChild, BusDriver, BusError, PowerState, mmio::MmioRegion};
2use alloc::{string::String, vec::Vec};
3
4const COMPATIBLE: &[&str] = &[
5    "simple-pm-bus",
6    "simple-bus",
7    "simple-mfd",
8    "isa",
9    "arm,amba-bus",
10];
11
12pub struct SimplePmBus {
13    regs: MmioRegion,
14    power_state: PowerState,
15    children: Vec<BusChild>,
16    num_clocks: u32,
17}
18
19impl SimplePmBus {
20    /// Creates a new instance.
21    pub fn new() -> Self {
22        Self {
23            regs: MmioRegion::new(),
24            power_state: PowerState::Off,
25            children: Vec::new(),
26            num_clocks: 0,
27        }
28    }
29
30    /// Sets num clocks.
31    pub fn set_num_clocks(&mut self, n: u32) {
32        self.num_clocks = n;
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 SimplePmBus {
42    /// Performs the name operation.
43    fn name(&self) -> &str {
44        "simple-pm-bus"
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.power_state = PowerState::On;
56        Ok(())
57    }
58
59    /// Performs the shutdown operation.
60    fn shutdown(&mut self) -> Result<(), BusError> {
61        self.power_state = PowerState::Off;
62        Ok(())
63    }
64
65    /// Performs the suspend operation.
66    fn suspend(&mut self) -> Result<(), BusError> {
67        self.power_state = PowerState::Suspended;
68        Ok(())
69    }
70
71    /// Performs the resume operation.
72    fn resume(&mut self) -> Result<(), BusError> {
73        self.power_state = PowerState::On;
74        Ok(())
75    }
76
77    /// Reads reg.
78    fn read_reg(&self, offset: usize) -> Result<u32, BusError> {
79        if !self.regs.is_valid() {
80            return Err(BusError::InitFailed);
81        }
82        Ok(self.regs.read32(offset))
83    }
84
85    /// Writes reg.
86    fn write_reg(&mut self, offset: usize, value: u32) -> Result<(), BusError> {
87        if !self.regs.is_valid() {
88            return Err(BusError::InitFailed);
89        }
90        self.regs.write32(offset, value);
91        Ok(())
92    }
93
94    /// Performs the children operation.
95    fn children(&self) -> Vec<BusChild> {
96        self.children.clone()
97    }
98}