Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: Copyright 2023-2024 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 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 | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 10 | use core::{ |
| 11 | marker::PhantomData, |
| 12 | ops::{Deref, DerefMut}, |
| 13 | }; |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 14 | |
| 15 | use spin::mutex::Mutex; |
| 16 | |
| 17 | use arm_gic::GICDRegisters; |
| 18 | use arm_pl011::PL011Registers; |
| 19 | use arm_sp805::SP805Registers; |
| 20 | |
| 21 | /// UART0 - PL011 |
| 22 | pub struct UART0 { |
| 23 | _marker: PhantomData<*const ()>, |
| 24 | } |
| 25 | |
| 26 | impl UART0 { |
Imre Kis | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 27 | pub const PTR: *mut PL011Registers = 0x1c09_0000 as *mut _; |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | impl Deref for UART0 { |
| 31 | type Target = PL011Registers; |
| 32 | |
| 33 | #[inline(always)] |
| 34 | fn deref(&self) -> &Self::Target { |
| 35 | unsafe { &*Self::PTR } |
| 36 | } |
| 37 | } |
| 38 | |
Imre Kis | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 39 | impl DerefMut for UART0 { |
| 40 | #[inline(always)] |
| 41 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 42 | unsafe { &mut *Self::PTR } |
| 43 | } |
| 44 | } |
| 45 | |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 46 | unsafe impl Send for UART0 {} |
| 47 | |
| 48 | /// UART1 - PL011 |
| 49 | pub struct UART1 { |
| 50 | _marker: PhantomData<*const ()>, |
| 51 | } |
| 52 | |
| 53 | impl UART1 { |
Imre Kis | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 54 | pub const PTR: *mut PL011Registers = 0x1c0a_0000 as *mut _; |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | impl Deref for UART1 { |
| 58 | type Target = PL011Registers; |
| 59 | |
| 60 | #[inline(always)] |
| 61 | fn deref(&self) -> &Self::Target { |
| 62 | unsafe { &*Self::PTR } |
| 63 | } |
| 64 | } |
| 65 | |
Imre Kis | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 66 | impl DerefMut for UART1 { |
| 67 | #[inline(always)] |
| 68 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 69 | unsafe { &mut *Self::PTR } |
| 70 | } |
| 71 | } |
| 72 | |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 73 | unsafe impl Send for UART1 {} |
| 74 | |
| 75 | /// UART2 - PL011 |
| 76 | pub struct UART2 { |
| 77 | _marker: PhantomData<*const ()>, |
| 78 | } |
| 79 | |
| 80 | impl UART2 { |
Imre Kis | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 81 | pub const PTR: *mut PL011Registers = 0x1c0b_0000 as *mut _; |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | impl Deref for UART2 { |
| 85 | type Target = PL011Registers; |
| 86 | |
| 87 | #[inline(always)] |
| 88 | fn deref(&self) -> &Self::Target { |
| 89 | unsafe { &*Self::PTR } |
| 90 | } |
| 91 | } |
| 92 | |
Imre Kis | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 93 | impl DerefMut for UART2 { |
| 94 | #[inline(always)] |
| 95 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 96 | unsafe { &mut *Self::PTR } |
| 97 | } |
| 98 | } |
| 99 | |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 100 | unsafe impl Send for UART2 {} |
| 101 | |
| 102 | /// UART3 - PL011 |
| 103 | pub struct UART3 { |
| 104 | _marker: PhantomData<*const ()>, |
| 105 | } |
| 106 | |
| 107 | impl UART3 { |
Imre Kis | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 108 | pub const PTR: *mut PL011Registers = 0x1c0c_0000 as *mut _; |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | impl Deref for UART3 { |
| 112 | type Target = PL011Registers; |
| 113 | |
| 114 | #[inline(always)] |
| 115 | fn deref(&self) -> &Self::Target { |
| 116 | unsafe { &*Self::PTR } |
| 117 | } |
| 118 | } |
| 119 | |
Imre Kis | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 120 | impl DerefMut for UART3 { |
| 121 | #[inline(always)] |
| 122 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 123 | unsafe { &mut *Self::PTR } |
| 124 | } |
| 125 | } |
| 126 | |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 127 | unsafe impl Send for UART3 {} |
| 128 | |
| 129 | /// Watchdog - SP805 |
| 130 | #[allow(clippy::upper_case_acronyms)] |
| 131 | pub struct WATCHDOG { |
| 132 | _marker: PhantomData<*const ()>, |
| 133 | } |
| 134 | |
| 135 | impl WATCHDOG { |
Imre Kis | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 136 | pub const PTR: *mut SP805Registers = 0x1c0f_0000 as *mut _; |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | impl Deref for WATCHDOG { |
| 140 | type Target = SP805Registers; |
| 141 | |
| 142 | #[inline(always)] |
| 143 | fn deref(&self) -> &Self::Target { |
| 144 | unsafe { &*Self::PTR } |
| 145 | } |
| 146 | } |
| 147 | |
Imre Kis | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 148 | impl DerefMut for WATCHDOG { |
| 149 | #[inline(always)] |
| 150 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 151 | unsafe { &mut *Self::PTR } |
| 152 | } |
| 153 | } |
| 154 | |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 155 | unsafe impl Send for WATCHDOG {} |
| 156 | |
| 157 | /// GIC Distributor |
| 158 | #[allow(clippy::upper_case_acronyms)] |
| 159 | pub struct GICD { |
| 160 | _marker: PhantomData<*const ()>, |
| 161 | } |
| 162 | |
| 163 | impl GICD { |
Imre Kis | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 164 | pub const PTR: *mut GICDRegisters = 0x2f00_0000 as *mut _; |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | impl Deref for GICD { |
| 168 | type Target = GICDRegisters; |
| 169 | |
| 170 | #[inline(always)] |
| 171 | fn deref(&self) -> &Self::Target { |
| 172 | unsafe { &*Self::PTR } |
| 173 | } |
| 174 | } |
| 175 | |
Imre Kis | 7c36bde | 2024-09-26 11:20:06 +0200 | [diff] [blame^] | 176 | impl DerefMut for GICD { |
| 177 | #[inline(always)] |
| 178 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 179 | unsafe { &mut *Self::PTR } |
| 180 | } |
| 181 | } |
| 182 | |
Imre Kis | 9c084c0 | 2024-08-14 15:53:45 +0200 | [diff] [blame] | 183 | unsafe impl Send for GICD {} |
| 184 | |
| 185 | static PERIPHERALS_TAKEN: Mutex<bool> = Mutex::new(false); |
| 186 | |
| 187 | /// FVP peripherals |
| 188 | #[allow(non_snake_case)] |
| 189 | pub struct Peripherals { |
| 190 | pub UART0: UART0, |
| 191 | pub UART1: UART1, |
| 192 | pub UART2: UART2, |
| 193 | pub UART3: UART3, |
| 194 | pub WATCHDOG: WATCHDOG, |
| 195 | pub GICD: GICD, |
| 196 | } |
| 197 | |
| 198 | impl Peripherals { |
| 199 | /// Take the peripherals once |
| 200 | pub fn take() -> Option<Self> { |
| 201 | if !*PERIPHERALS_TAKEN.lock() { |
| 202 | Some(unsafe { Self::steal() }) |
| 203 | } else { |
| 204 | None |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /// Unsafe version of take() |
| 209 | /// |
| 210 | /// # Safety |
| 211 | /// The caller has to ensure that each peripheral is only used once. |
| 212 | pub unsafe fn steal() -> Self { |
| 213 | *PERIPHERALS_TAKEN.lock() = true; |
| 214 | |
| 215 | Peripherals { |
| 216 | UART0: UART0 { |
| 217 | _marker: PhantomData, |
| 218 | }, |
| 219 | UART1: UART1 { |
| 220 | _marker: PhantomData, |
| 221 | }, |
| 222 | UART2: UART2 { |
| 223 | _marker: PhantomData, |
| 224 | }, |
| 225 | UART3: UART3 { |
| 226 | _marker: PhantomData, |
| 227 | }, |
| 228 | WATCHDOG: WATCHDOG { |
| 229 | _marker: PhantomData, |
| 230 | }, |
| 231 | GICD: GICD { |
| 232 | _marker: PhantomData, |
| 233 | }, |
| 234 | } |
| 235 | } |
| 236 | } |