blob: 185ab2197dd2320504152e16acbd5d7aef9780ca [file] [log] [blame]
Imre Kis55661632025-03-14 15:25:40 +01001// SPDX-FileCopyrightText: Copyright 2023-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>
Imre Kis9c084c02024-08-14 15:53:45 +02002// 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 Kis55661632025-03-14 15:25:40 +010010// Re-export peripheral drivers and common safe-mmio types
11pub use arm_gic;
12pub use arm_pl011_uart;
13pub use arm_sp805;
14pub use safe_mmio::{PhysicalInstance, UniqueMmioPointer};
Imre Kis9c084c02024-08-14 15:53:45 +020015
Imre Kis9c084c02024-08-14 15:53:45 +020016use arm_gic::GICDRegisters;
Balint Dobszay4292ed42025-01-09 13:52:32 +010017use arm_pl011_uart::PL011Registers;
Imre Kis9c084c02024-08-14 15:53:45 +020018use arm_sp805::SP805Registers;
Imre Kis55661632025-03-14 15:25:40 +010019use core::fmt::Debug;
Andrew Walbran1a289d12025-02-11 14:55:39 +000020use spin::mutex::Mutex;
Imre Kis9c084c02024-08-14 15:53:45 +020021
22static PERIPHERALS_TAKEN: Mutex<bool> = Mutex::new(false);
23
24/// FVP peripherals
Andrew Walbran1a289d12025-02-11 14:55:39 +000025#[derive(Debug)]
Imre Kis9c084c02024-08-14 15:53:45 +020026pub struct Peripherals {
Andrew Walbran1a289d12025-02-11 14:55:39 +000027 pub uart0: PhysicalInstance<PL011Registers>,
28 pub uart1: PhysicalInstance<PL011Registers>,
29 pub uart2: PhysicalInstance<PL011Registers>,
30 pub uart3: PhysicalInstance<PL011Registers>,
31 pub watchdog: PhysicalInstance<SP805Registers>,
32 pub gicd: PhysicalInstance<GICDRegisters>,
Imre Kis9c084c02024-08-14 15:53:45 +020033}
34
35impl Peripherals {
36 /// Take the peripherals once
37 pub fn take() -> Option<Self> {
38 if !*PERIPHERALS_TAKEN.lock() {
Imre Kis75a43542024-10-02 14:11:25 +020039 // SAFETY: PERIPHERALS_TAKEN ensures that this is only called once.
Imre Kis9c084c02024-08-14 15:53:45 +020040 Some(unsafe { Self::steal() })
41 } else {
42 None
43 }
44 }
45
46 /// Unsafe version of take()
47 ///
48 /// # Safety
Andrew Walbran1a289d12025-02-11 14:55:39 +000049 ///
50 /// The caller must ensure that each peripheral is only used once.
Imre Kis9c084c02024-08-14 15:53:45 +020051 pub unsafe fn steal() -> Self {
52 *PERIPHERALS_TAKEN.lock() = true;
53
54 Peripherals {
Andrew Walbran1a289d12025-02-11 14:55:39 +000055 uart0: PhysicalInstance::new(0x1c09_0000),
56 uart1: PhysicalInstance::new(0x1c0a_0000),
57 uart2: PhysicalInstance::new(0x1c0b_0000),
58 uart3: PhysicalInstance::new(0x1c0c_0000),
59 watchdog: PhysicalInstance::new(0x1c0f_0000),
60 gicd: PhysicalInstance::new(0x2f00_0000),
Imre Kis9c084c02024-08-14 15:53:45 +020061 }
62 }
63}