Imre Kis | 5566163 | 2025-03-14 15:25:40 +0100 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: Copyright 2023-2025 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 2 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 | |
| 4 | //! # Peripheral Access Crate fro Arm Fixed Virtual Platform |
| 5 | //! |
| 6 | //! The crate provides access to the peripherals of [Arm Fixed Virtual Platform](https://developer.arm.com/Tools%20and%20Software/Fixed%20Virtual%20Platforms). |
| 7 | |
| 8 | #![no_std] |
| 9 | |
Imre Kis | 5566163 | 2025-03-14 15:25:40 +0100 | [diff] [blame] | 10 | // Re-export peripheral drivers and common safe-mmio types |
| 11 | pub use arm_gic; |
| 12 | pub use arm_pl011_uart; |
| 13 | pub use arm_sp805; |
| 14 | pub use safe_mmio::{PhysicalInstance, UniqueMmioPointer}; |
Tomás González | 52cb4a6 | 2025-03-19 18:41:00 +0000 | [diff] [blame^] | 15 | pub mod power_controller; |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 16 | |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 17 | use arm_gic::GICDRegisters; |
Balint Dobszay | 4292ed4 | 2025-01-09 13:52:32 +0100 | [diff] [blame] | 18 | use arm_pl011_uart::PL011Registers; |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 19 | use arm_sp805::SP805Registers; |
Imre Kis | 5566163 | 2025-03-14 15:25:40 +0100 | [diff] [blame] | 20 | use core::fmt::Debug; |
Tomás González | 52cb4a6 | 2025-03-19 18:41:00 +0000 | [diff] [blame^] | 21 | use power_controller::FvpPowerControllerRegisters; |
Andrew Walbran | 1a289d1 | 2025-02-11 14:55:39 +0000 | [diff] [blame] | 22 | use spin::mutex::Mutex; |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 23 | |
| 24 | static PERIPHERALS_TAKEN: Mutex<bool> = Mutex::new(false); |
| 25 | |
| 26 | /// FVP peripherals |
Andrew Walbran | 1a289d1 | 2025-02-11 14:55:39 +0000 | [diff] [blame] | 27 | #[derive(Debug)] |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 28 | pub struct Peripherals { |
Andrew Walbran | 1a289d1 | 2025-02-11 14:55:39 +0000 | [diff] [blame] | 29 | pub uart0: PhysicalInstance<PL011Registers>, |
| 30 | pub uart1: PhysicalInstance<PL011Registers>, |
| 31 | pub uart2: PhysicalInstance<PL011Registers>, |
| 32 | pub uart3: PhysicalInstance<PL011Registers>, |
| 33 | pub watchdog: PhysicalInstance<SP805Registers>, |
| 34 | pub gicd: PhysicalInstance<GICDRegisters>, |
Tomás González | 52cb4a6 | 2025-03-19 18:41:00 +0000 | [diff] [blame^] | 35 | pub power_controller: PhysicalInstance<FvpPowerControllerRegisters>, |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | impl Peripherals { |
| 39 | /// Take the peripherals once |
| 40 | pub fn take() -> Option<Self> { |
| 41 | if !*PERIPHERALS_TAKEN.lock() { |
Imre Kis | 75a4354 | 2024-10-02 14:11:25 +0200 | [diff] [blame] | 42 | // SAFETY: PERIPHERALS_TAKEN ensures that this is only called once. |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 43 | Some(unsafe { Self::steal() }) |
| 44 | } else { |
| 45 | None |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Unsafe version of take() |
| 50 | /// |
| 51 | /// # Safety |
Andrew Walbran | 1a289d1 | 2025-02-11 14:55:39 +0000 | [diff] [blame] | 52 | /// |
| 53 | /// The caller must ensure that each peripheral is only used once. |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 54 | pub unsafe fn steal() -> Self { |
| 55 | *PERIPHERALS_TAKEN.lock() = true; |
| 56 | |
| 57 | Peripherals { |
Andrew Walbran | 1a289d1 | 2025-02-11 14:55:39 +0000 | [diff] [blame] | 58 | uart0: PhysicalInstance::new(0x1c09_0000), |
| 59 | uart1: PhysicalInstance::new(0x1c0a_0000), |
| 60 | uart2: PhysicalInstance::new(0x1c0b_0000), |
| 61 | uart3: PhysicalInstance::new(0x1c0c_0000), |
| 62 | watchdog: PhysicalInstance::new(0x1c0f_0000), |
Tomás González | 52cb4a6 | 2025-03-19 18:41:00 +0000 | [diff] [blame^] | 63 | power_controller: PhysicalInstance::new(0x1c10_0000), |
Andrew Walbran | 1a289d1 | 2025-02-11 14:55:39 +0000 | [diff] [blame] | 64 | gicd: PhysicalInstance::new(0x2f00_0000), |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |