Skip to main content

strat9_bus_drivers/
stm32_etzpc.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23use crate::{
24    BusChild, BusDriver, BusError, PowerState,
25    mmio::MmioRegion,
26    stm32_firewall::{FirewallController, FirewallType},
27};
28use alloc::{string::String, vec::Vec};
29
30const ETZPC_DECPROT: usize = 0x10;
31const ETZPC_HWCFGR: usize = 0x3F0;
32
33const ETZPC_PROT_A7NS: u32 = 0x3;
34
35const HWCFGR_NUM_TZMA_MASK: u32 = 0xFF;
36const HWCFGR_NUM_PER_SEC_MASK: u32 = 0xFF00;
37const HWCFGR_NUM_PER_SEC_SHIFT: u32 = 8;
38const HWCFGR_NUM_AHB_SEC_MASK: u32 = 0xFF0000;
39const HWCFGR_NUM_AHB_SEC_SHIFT: u32 = 16;
40
41const COMPATIBLE: &[&str] = &["st,stm32-etzpc"];
42
43pub struct Stm32Etzpc {
44    regs: MmioRegion,
45    num_per: u32,
46    num_master: u32,
47    power_state: PowerState,
48    children: Vec<BusChild>,
49}
50
51impl Stm32Etzpc {
52    /// Creates a new instance.
53    pub fn new() -> Self {
54        Self {
55            regs: MmioRegion::new(),
56            num_per: 0,
57            num_master: 0,
58            power_state: PowerState::Off,
59            children: Vec::new(),
60        }
61    }
62
63    /// Reads hwcfg.
64    fn read_hwcfg(&mut self) {
65        let hwcfg = self.regs.read32(ETZPC_HWCFGR);
66        self.num_per = (hwcfg & HWCFGR_NUM_PER_SEC_MASK) >> HWCFGR_NUM_PER_SEC_SHIFT;
67        self.num_master = (hwcfg & HWCFGR_NUM_AHB_SEC_MASK) >> HWCFGR_NUM_AHB_SEC_SHIFT;
68    }
69
70    /// Reads decprot.
71    fn read_decprot(&self, id: u32) -> u32 {
72        let reg_idx = id / 16;
73        let bit_offset = (id % 16) * 2;
74        let val = self.regs.read32(ETZPC_DECPROT + (reg_idx as usize) * 4);
75        (val >> bit_offset) & 0x3
76    }
77
78    /// Performs the add child operation.
79    pub fn add_child(&mut self, child: BusChild) {
80        self.children.push(child);
81    }
82}
83
84impl FirewallController for Stm32Etzpc {
85    /// Performs the name operation.
86    fn name(&self) -> &str {
87        "stm32-etzpc"
88    }
89
90    /// Performs the firewall type operation.
91    fn firewall_type(&self) -> FirewallType {
92        FirewallType::Peripheral
93    }
94
95    /// Performs the max entries operation.
96    fn max_entries(&self) -> u32 {
97        self.num_per + self.num_master
98    }
99
100    /// Performs the grant access operation.
101    fn grant_access(&self, firewall_id: u32) -> Result<(), BusError> {
102        if firewall_id >= self.num_per + self.num_master {
103            return Err(BusError::InvalidArgument);
104        }
105
106        let decprot = self.read_decprot(firewall_id);
107        if decprot != ETZPC_PROT_A7NS {
108            return Err(BusError::PermissionDenied);
109        }
110
111        Ok(())
112    }
113
114    /// Performs the release access operation.
115    fn release_access(&self, _firewall_id: u32) -> Result<(), BusError> {
116        Ok(())
117    }
118}
119
120impl BusDriver for Stm32Etzpc {
121    /// Performs the name operation.
122    fn name(&self) -> &str {
123        "stm32-etzpc"
124    }
125
126    /// Exposes the firewall capability to the scheme server.
127    fn as_firewall(&self) -> Option<&dyn FirewallController> {
128        Some(self)
129    }
130
131    /// Performs the compatible operation.
132    fn compatible(&self) -> &[&str] {
133        COMPATIBLE
134    }
135
136    /// Performs the init operation.
137    fn init(&mut self, base: usize) -> Result<(), BusError> {
138        self.regs.init(base, 0x400);
139        self.read_hwcfg();
140        self.power_state = PowerState::On;
141        Ok(())
142    }
143
144    /// Performs the shutdown operation.
145    fn shutdown(&mut self) -> Result<(), BusError> {
146        self.power_state = PowerState::Off;
147        Ok(())
148    }
149
150    /// Reads reg.
151    fn read_reg(&self, offset: usize) -> Result<u32, BusError> {
152        if !self.regs.is_valid() {
153            return Err(BusError::InitFailed);
154        }
155        self.regs.check_user_offset(offset)?;
156        Ok(self.regs.read32(offset))
157    }
158
159    /// Writes reg.
160    fn write_reg(&mut self, offset: usize, value: u32) -> Result<(), BusError> {
161        if !self.regs.is_valid() {
162            return Err(BusError::InitFailed);
163        }
164        self.regs.check_user_offset(offset)?;
165        self.regs.write32(offset, value);
166        Ok(())
167    }
168
169    /// Performs the children operation.
170    fn children(&self) -> Vec<BusChild> {
171        self.children.clone()
172    }
173}