1 // Copyright 2024, Linaro Limited 2 // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org> 3 // SPDX-License-Identifier: GPL-2.0-or-later 4 // 5 // PL011 QEMU Device Model 6 // 7 // This library implements a device model for the PrimeCell® UART (PL011) 8 // device in QEMU. 9 // 10 #![doc = include_str!("../README.md")] 11 //! # Library crate 12 //! 13 //! See [`PL011State`](crate::device::PL011State) for the device model type and 14 //! the [`registers`] module for register types. 15 16 #![deny( 17 clippy::correctness, 18 clippy::suspicious, 19 clippy::complexity, 20 clippy::perf, 21 clippy::cargo, 22 clippy::nursery, 23 clippy::style 24 )] 25 #![allow(clippy::upper_case_acronyms)] 26 #![allow(clippy::result_unit_err)] 27 28 extern crate bilge; 29 extern crate bilge_impl; 30 extern crate qemu_api; 31 32 use qemu_api::c_str; 33 34 pub mod device; 35 pub mod device_class; 36 pub mod memory_ops; 37 38 pub const TYPE_PL011: &::std::ffi::CStr = c_str!("pl011"); 39 pub const TYPE_PL011_LUMINARY: &::std::ffi::CStr = c_str!("pl011_luminary"); 40 41 /// Offset of each register from the base memory address of the device. 42 /// 43 /// # Source 44 /// ARM DDI 0183G, Table 3-1 p.3-3 45 #[doc(alias = "offset")] 46 #[allow(non_camel_case_types)] 47 #[repr(u64)] 48 #[derive(Debug)] 49 pub enum RegisterOffset { 50 /// Data Register 51 /// 52 /// A write to this register initiates the actual data transmission 53 #[doc(alias = "UARTDR")] 54 DR = 0x000, 55 /// Receive Status Register or Error Clear Register 56 #[doc(alias = "UARTRSR")] 57 #[doc(alias = "UARTECR")] 58 RSR = 0x004, 59 /// Flag Register 60 /// 61 /// A read of this register shows if transmission is complete 62 #[doc(alias = "UARTFR")] 63 FR = 0x018, 64 /// Fractional Baud Rate Register 65 /// 66 /// responsible for baud rate speed 67 #[doc(alias = "UARTFBRD")] 68 FBRD = 0x028, 69 /// `IrDA` Low-Power Counter Register 70 #[doc(alias = "UARTILPR")] 71 ILPR = 0x020, 72 /// Integer Baud Rate Register 73 /// 74 /// Responsible for baud rate speed 75 #[doc(alias = "UARTIBRD")] 76 IBRD = 0x024, 77 /// line control register (data frame format) 78 #[doc(alias = "UARTLCR_H")] 79 LCR_H = 0x02C, 80 /// Toggle UART, transmission or reception 81 #[doc(alias = "UARTCR")] 82 CR = 0x030, 83 /// Interrupt FIFO Level Select Register 84 #[doc(alias = "UARTIFLS")] 85 FLS = 0x034, 86 /// Interrupt Mask Set/Clear Register 87 #[doc(alias = "UARTIMSC")] 88 IMSC = 0x038, 89 /// Raw Interrupt Status Register 90 #[doc(alias = "UARTRIS")] 91 RIS = 0x03C, 92 /// Masked Interrupt Status Register 93 #[doc(alias = "UARTMIS")] 94 MIS = 0x040, 95 /// Interrupt Clear Register 96 #[doc(alias = "UARTICR")] 97 ICR = 0x044, 98 /// DMA control Register 99 #[doc(alias = "UARTDMACR")] 100 DMACR = 0x048, 101 ///// Reserved, offsets `0x04C` to `0x07C`. 102 //Reserved = 0x04C, 103 } 104 105 impl core::convert::TryFrom<u64> for RegisterOffset { 106 type Error = u64; 107 108 fn try_from(value: u64) -> Result<Self, Self::Error> { 109 macro_rules! case { 110 ($($discriminant:ident),*$(,)*) => { 111 /* check that matching on all macro arguments compiles, which means we are not 112 * missing any enum value; if the type definition ever changes this will stop 113 * compiling. 114 */ 115 const fn _assert_exhaustive(val: RegisterOffset) { 116 match val { 117 $(RegisterOffset::$discriminant => (),)* 118 } 119 } 120 121 match value { 122 $(x if x == Self::$discriminant as u64 => Ok(Self::$discriminant),)* 123 _ => Err(value), 124 } 125 } 126 } 127 case! { DR, RSR, FR, FBRD, ILPR, IBRD, LCR_H, CR, FLS, IMSC, RIS, MIS, ICR, DMACR } 128 } 129 } 130 131 pub mod registers { 132 //! Device registers exposed as typed structs which are backed by arbitrary 133 //! integer bitmaps. [`Data`], [`Control`], [`LineControl`], etc. 134 //! 135 //! All PL011 registers are essentially 32-bit wide, but are typed here as 136 //! bitmaps with only the necessary width. That is, if a struct bitmap 137 //! in this module is for example 16 bits long, it should be conceived 138 //! as a 32-bit register where the unmentioned higher bits are always 139 //! unused thus treated as zero when read or written. 140 use bilge::prelude::*; 141 142 // TODO: FIFO Mode has different semantics 143 /// Data Register, `UARTDR` 144 /// 145 /// The `UARTDR` register is the data register. 146 /// 147 /// For words to be transmitted: 148 /// 149 /// - if the FIFOs are enabled, data written to this location is pushed onto 150 /// the transmit 151 /// FIFO 152 /// - if the FIFOs are not enabled, data is stored in the transmitter 153 /// holding register (the 154 /// bottom word of the transmit FIFO). 155 /// 156 /// The write operation initiates transmission from the UART. The data is 157 /// prefixed with a start bit, appended with the appropriate parity bit 158 /// (if parity is enabled), and a stop bit. The resultant word is then 159 /// transmitted. 160 /// 161 /// For received words: 162 /// 163 /// - if the FIFOs are enabled, the data byte and the 4-bit status (break, 164 /// frame, parity, 165 /// and overrun) is pushed onto the 12-bit wide receive FIFO 166 /// - if the FIFOs are not enabled, the data byte and status are stored in 167 /// the receiving 168 /// holding register (the bottom word of the receive FIFO). 169 /// 170 /// The received data byte is read by performing reads from the `UARTDR` 171 /// register along with the corresponding status information. The status 172 /// information can also be read by a read of the `UARTRSR/UARTECR` 173 /// register. 174 /// 175 /// # Note 176 /// 177 /// You must disable the UART before any of the control registers are 178 /// reprogrammed. When the UART is disabled in the middle of 179 /// transmission or reception, it completes the current character before 180 /// stopping. 181 /// 182 /// # Source 183 /// ARM DDI 0183G 3.3.1 Data Register, UARTDR 184 #[bitsize(16)] 185 #[derive(Clone, Copy, DebugBits, FromBits)] 186 #[doc(alias = "UARTDR")] 187 pub struct Data { 188 _reserved: u4, 189 pub data: u8, 190 pub framing_error: bool, 191 pub parity_error: bool, 192 pub break_error: bool, 193 pub overrun_error: bool, 194 } 195 196 // TODO: FIFO Mode has different semantics 197 /// Receive Status Register / Error Clear Register, `UARTRSR/UARTECR` 198 /// 199 /// The UARTRSR/UARTECR register is the receive status register/error clear 200 /// register. Receive status can also be read from the `UARTRSR` 201 /// register. If the status is read from this register, then the status 202 /// information for break, framing and parity corresponds to the 203 /// data character read from the [Data register](Data), `UARTDR` prior to 204 /// reading the UARTRSR register. The status information for overrun is 205 /// set immediately when an overrun condition occurs. 206 /// 207 /// 208 /// # Note 209 /// The received data character must be read first from the [Data 210 /// Register](Data), `UARTDR` before reading the error status associated 211 /// with that data character from the `UARTRSR` register. This read 212 /// sequence cannot be reversed, because the `UARTRSR` register is 213 /// updated only when a read occurs from the `UARTDR` register. However, 214 /// the status information can also be obtained by reading the `UARTDR` 215 /// register 216 /// 217 /// # Source 218 /// ARM DDI 0183G 3.3.2 Receive Status Register/Error Clear Register, 219 /// UARTRSR/UARTECR 220 #[bitsize(8)] 221 #[derive(Clone, Copy, DebugBits, FromBits)] 222 pub struct ReceiveStatusErrorClear { 223 pub framing_error: bool, 224 pub parity_error: bool, 225 pub break_error: bool, 226 pub overrun_error: bool, 227 _reserved_unpredictable: u4, 228 } 229 230 impl ReceiveStatusErrorClear { 231 pub fn reset(&mut self) { 232 // All the bits are cleared to 0 on reset. 233 *self = Self::default(); 234 } 235 } 236 237 impl Default for ReceiveStatusErrorClear { 238 fn default() -> Self { 239 0.into() 240 } 241 } 242 243 #[bitsize(16)] 244 #[derive(Clone, Copy, DebugBits, FromBits)] 245 /// Flag Register, `UARTFR` 246 #[doc(alias = "UARTFR")] 247 pub struct Flags { 248 /// CTS Clear to send. This bit is the complement of the UART clear to 249 /// send, `nUARTCTS`, modem status input. That is, the bit is 1 250 /// when `nUARTCTS` is LOW. 251 pub clear_to_send: bool, 252 /// DSR Data set ready. This bit is the complement of the UART data set 253 /// ready, `nUARTDSR`, modem status input. That is, the bit is 1 when 254 /// `nUARTDSR` is LOW. 255 pub data_set_ready: bool, 256 /// DCD Data carrier detect. This bit is the complement of the UART data 257 /// carrier detect, `nUARTDCD`, modem status input. That is, the bit is 258 /// 1 when `nUARTDCD` is LOW. 259 pub data_carrier_detect: bool, 260 /// BUSY UART busy. If this bit is set to 1, the UART is busy 261 /// transmitting data. This bit remains set until the complete 262 /// byte, including all the stop bits, has been sent from the 263 /// shift register. This bit is set as soon as the transmit FIFO 264 /// becomes non-empty, regardless of whether the UART is enabled 265 /// or not. 266 pub busy: bool, 267 /// RXFE Receive FIFO empty. The meaning of this bit depends on the 268 /// state of the FEN bit in the UARTLCR_H register. If the FIFO 269 /// is disabled, this bit is set when the receive holding 270 /// register is empty. If the FIFO is enabled, the RXFE bit is 271 /// set when the receive FIFO is empty. 272 pub receive_fifo_empty: bool, 273 /// TXFF Transmit FIFO full. The meaning of this bit depends on the 274 /// state of the FEN bit in the UARTLCR_H register. If the FIFO 275 /// is disabled, this bit is set when the transmit holding 276 /// register is full. If the FIFO is enabled, the TXFF bit is 277 /// set when the transmit FIFO is full. 278 pub transmit_fifo_full: bool, 279 /// RXFF Receive FIFO full. The meaning of this bit depends on the state 280 /// of the FEN bit in the UARTLCR_H register. If the FIFO is 281 /// disabled, this bit is set when the receive holding register 282 /// is full. If the FIFO is enabled, the RXFF bit is set when 283 /// the receive FIFO is full. 284 pub receive_fifo_full: bool, 285 /// Transmit FIFO empty. The meaning of this bit depends on the state of 286 /// the FEN bit in the [Line Control register](LineControl), 287 /// `UARTLCR_H`. If the FIFO is disabled, this bit is set when the 288 /// transmit holding register is empty. If the FIFO is enabled, 289 /// the TXFE bit is set when the transmit FIFO is empty. This 290 /// bit does not indicate if there is data in the transmit shift 291 /// register. 292 pub transmit_fifo_empty: bool, 293 /// `RI`, is `true` when `nUARTRI` is `LOW`. 294 pub ring_indicator: bool, 295 _reserved_zero_no_modify: u7, 296 } 297 298 impl Flags { 299 pub fn reset(&mut self) { 300 *self = Self::default(); 301 } 302 } 303 304 impl Default for Flags { 305 fn default() -> Self { 306 let mut ret: Self = 0.into(); 307 // After reset TXFF, RXFF, and BUSY are 0, and TXFE and RXFE are 1 308 ret.set_receive_fifo_empty(true); 309 ret.set_transmit_fifo_empty(true); 310 ret 311 } 312 } 313 314 #[bitsize(16)] 315 #[derive(Clone, Copy, DebugBits, FromBits)] 316 /// Line Control Register, `UARTLCR_H` 317 #[doc(alias = "UARTLCR_H")] 318 pub struct LineControl { 319 /// BRK Send break. 320 /// 321 /// If this bit is set to `1`, a low-level is continually output on the 322 /// `UARTTXD` output, after completing transmission of the 323 /// current character. For the proper execution of the break command, 324 /// the software must set this bit for at least two complete 325 /// frames. For normal use, this bit must be cleared to `0`. 326 pub send_break: bool, 327 /// 1 PEN Parity enable: 328 /// 329 /// - 0 = parity is disabled and no parity bit added to the data frame 330 /// - 1 = parity checking and generation is enabled. 331 /// 332 /// See Table 3-11 on page 3-14 for the parity truth table. 333 pub parity_enabled: bool, 334 /// EPS Even parity select. Controls the type of parity the UART uses 335 /// during transmission and reception: 336 /// - 0 = odd parity. The UART generates or checks for an odd number of 337 /// 1s in the data and parity bits. 338 /// - 1 = even parity. The UART generates or checks for an even number 339 /// of 1s in the data and parity bits. 340 /// This bit has no effect when the `PEN` bit disables parity checking 341 /// and generation. See Table 3-11 on page 3-14 for the parity 342 /// truth table. 343 pub parity: Parity, 344 /// 3 STP2 Two stop bits select. If this bit is set to 1, two stop bits 345 /// are transmitted at the end of the frame. The receive 346 /// logic does not check for two stop bits being received. 347 pub two_stops_bits: bool, 348 /// FEN Enable FIFOs: 349 /// 0 = FIFOs are disabled (character mode) that is, the FIFOs become 350 /// 1-byte-deep holding registers 1 = transmit and receive FIFO 351 /// buffers are enabled (FIFO mode). 352 pub fifos_enabled: Mode, 353 /// WLEN Word length. These bits indicate the number of data bits 354 /// transmitted or received in a frame as follows: b11 = 8 bits 355 /// b10 = 7 bits 356 /// b01 = 6 bits 357 /// b00 = 5 bits. 358 pub word_length: WordLength, 359 /// 7 SPS Stick parity select. 360 /// 0 = stick parity is disabled 361 /// 1 = either: 362 /// • if the EPS bit is 0 then the parity bit is transmitted and checked 363 /// as a 1 • if the EPS bit is 1 then the parity bit is 364 /// transmitted and checked as a 0. This bit has no effect when 365 /// the PEN bit disables parity checking and generation. See Table 3-11 366 /// on page 3-14 for the parity truth table. 367 pub sticky_parity: bool, 368 /// 15:8 - Reserved, do not modify, read as zero. 369 _reserved_zero_no_modify: u8, 370 } 371 372 impl LineControl { 373 pub fn reset(&mut self) { 374 // All the bits are cleared to 0 when reset. 375 *self = 0.into(); 376 } 377 } 378 379 impl Default for LineControl { 380 fn default() -> Self { 381 0.into() 382 } 383 } 384 385 #[bitsize(1)] 386 #[derive(Clone, Copy, Debug, Eq, FromBits, PartialEq)] 387 /// `EPS` "Even parity select", field of [Line Control 388 /// register](LineControl). 389 pub enum Parity { 390 /// - 0 = odd parity. The UART generates or checks for an odd number of 391 /// 1s in the data and parity bits. 392 Odd = 0, 393 /// - 1 = even parity. The UART generates or checks for an even number 394 /// of 1s in the data and parity bits. 395 Even = 1, 396 } 397 398 #[bitsize(1)] 399 #[derive(Clone, Copy, Debug, Eq, FromBits, PartialEq)] 400 /// `FEN` "Enable FIFOs" or Device mode, field of [Line Control 401 /// register](LineControl). 402 pub enum Mode { 403 /// 0 = FIFOs are disabled (character mode) that is, the FIFOs become 404 /// 1-byte-deep holding registers 405 Character = 0, 406 /// 1 = transmit and receive FIFO buffers are enabled (FIFO mode). 407 FIFO = 1, 408 } 409 410 impl From<Mode> for bool { 411 fn from(val: Mode) -> Self { 412 matches!(val, Mode::FIFO) 413 } 414 } 415 416 #[bitsize(2)] 417 #[derive(Clone, Copy, Debug, Eq, FromBits, PartialEq)] 418 /// `WLEN` Word length, field of [Line Control register](LineControl). 419 /// 420 /// These bits indicate the number of data bits transmitted or received in a 421 /// frame as follows: 422 pub enum WordLength { 423 /// b11 = 8 bits 424 _8Bits = 0b11, 425 /// b10 = 7 bits 426 _7Bits = 0b10, 427 /// b01 = 6 bits 428 _6Bits = 0b01, 429 /// b00 = 5 bits. 430 _5Bits = 0b00, 431 } 432 433 /// Control Register, `UARTCR` 434 /// 435 /// The `UARTCR` register is the control register. All the bits are cleared 436 /// to `0` on reset except for bits `9` and `8` that are set to `1`. 437 /// 438 /// # Source 439 /// ARM DDI 0183G, 3.3.8 Control Register, `UARTCR`, Table 3-12 440 #[bitsize(16)] 441 #[doc(alias = "UARTCR")] 442 #[derive(Clone, Copy, DebugBits, FromBits)] 443 pub struct Control { 444 /// `UARTEN` UART enable: 0 = UART is disabled. If the UART is disabled 445 /// in the middle of transmission or reception, it completes the current 446 /// character before stopping. 1 = the UART is enabled. Data 447 /// transmission and reception occurs for either UART signals or SIR 448 /// signals depending on the setting of the SIREN bit. 449 pub enable_uart: bool, 450 /// `SIREN` `SIR` enable: 0 = IrDA SIR ENDEC is disabled. `nSIROUT` 451 /// remains LOW (no light pulse generated), and signal transitions on 452 /// SIRIN have no effect. 1 = IrDA SIR ENDEC is enabled. Data is 453 /// transmitted and received on nSIROUT and SIRIN. UARTTXD remains HIGH, 454 /// in the marking state. Signal transitions on UARTRXD or modem status 455 /// inputs have no effect. This bit has no effect if the UARTEN bit 456 /// disables the UART. 457 pub enable_sir: bool, 458 /// `SIRLP` SIR low-power IrDA mode. This bit selects the IrDA encoding 459 /// mode. If this bit is cleared to 0, low-level bits are transmitted as 460 /// an active high pulse with a width of 3/ 16th of the bit period. If 461 /// this bit is set to 1, low-level bits are transmitted with a pulse 462 /// width that is 3 times the period of the IrLPBaud16 input signal, 463 /// regardless of the selected bit rate. Setting this bit uses less 464 /// power, but might reduce transmission distances. 465 pub sir_lowpower_irda_mode: u1, 466 /// Reserved, do not modify, read as zero. 467 _reserved_zero_no_modify: u4, 468 /// `LBE` Loopback enable. If this bit is set to 1 and the SIREN bit is 469 /// set to 1 and the SIRTEST bit in the Test Control register, UARTTCR 470 /// on page 4-5 is set to 1, then the nSIROUT path is inverted, and fed 471 /// through to the SIRIN path. The SIRTEST bit in the test register must 472 /// be set to 1 to override the normal half-duplex SIR operation. This 473 /// must be the requirement for accessing the test registers during 474 /// normal operation, and SIRTEST must be cleared to 0 when loopback 475 /// testing is finished. This feature reduces the amount of external 476 /// coupling required during system test. If this bit is set to 1, and 477 /// the SIRTEST bit is set to 0, the UARTTXD path is fed through to the 478 /// UARTRXD path. In either SIR mode or UART mode, when this bit is set, 479 /// the modem outputs are also fed through to the modem inputs. This bit 480 /// is cleared to 0 on reset, to disable loopback. 481 pub enable_loopback: bool, 482 /// `TXE` Transmit enable. If this bit is set to 1, the transmit section 483 /// of the UART is enabled. Data transmission occurs for either UART 484 /// signals, or SIR signals depending on the setting of the SIREN bit. 485 /// When the UART is disabled in the middle of transmission, it 486 /// completes the current character before stopping. 487 pub enable_transmit: bool, 488 /// `RXE` Receive enable. If this bit is set to 1, the receive section 489 /// of the UART is enabled. Data reception occurs for either UART 490 /// signals or SIR signals depending on the setting of the SIREN bit. 491 /// When the UART is disabled in the middle of reception, it completes 492 /// the current character before stopping. 493 pub enable_receive: bool, 494 /// `DTR` Data transmit ready. This bit is the complement of the UART 495 /// data transmit ready, `nUARTDTR`, modem status output. That is, when 496 /// the bit is programmed to a 1 then `nUARTDTR` is LOW. 497 pub data_transmit_ready: bool, 498 /// `RTS` Request to send. This bit is the complement of the UART 499 /// request to send, `nUARTRTS`, modem status output. That is, when the 500 /// bit is programmed to a 1 then `nUARTRTS` is LOW. 501 pub request_to_send: bool, 502 /// `Out1` This bit is the complement of the UART Out1 (`nUARTOut1`) 503 /// modem status output. That is, when the bit is programmed to a 1 the 504 /// output is 0. For DTE this can be used as Data Carrier Detect (DCD). 505 pub out_1: bool, 506 /// `Out2` This bit is the complement of the UART Out2 (`nUARTOut2`) 507 /// modem status output. That is, when the bit is programmed to a 1, the 508 /// output is 0. For DTE this can be used as Ring Indicator (RI). 509 pub out_2: bool, 510 /// `RTSEn` RTS hardware flow control enable. If this bit is set to 1, 511 /// RTS hardware flow control is enabled. Data is only requested when 512 /// there is space in the receive FIFO for it to be received. 513 pub rts_hardware_flow_control_enable: bool, 514 /// `CTSEn` CTS hardware flow control enable. If this bit is set to 1, 515 /// CTS hardware flow control is enabled. Data is only transmitted when 516 /// the `nUARTCTS` signal is asserted. 517 pub cts_hardware_flow_control_enable: bool, 518 } 519 520 impl Control { 521 pub fn reset(&mut self) { 522 *self = 0.into(); 523 self.set_enable_receive(true); 524 self.set_enable_transmit(true); 525 } 526 } 527 528 impl Default for Control { 529 fn default() -> Self { 530 let mut ret: Self = 0.into(); 531 ret.reset(); 532 ret 533 } 534 } 535 536 /// Interrupt status bits in UARTRIS, UARTMIS, UARTIMSC 537 pub const INT_OE: u32 = 1 << 10; 538 pub const INT_BE: u32 = 1 << 9; 539 pub const INT_PE: u32 = 1 << 8; 540 pub const INT_FE: u32 = 1 << 7; 541 pub const INT_RT: u32 = 1 << 6; 542 pub const INT_TX: u32 = 1 << 5; 543 pub const INT_RX: u32 = 1 << 4; 544 pub const INT_DSR: u32 = 1 << 3; 545 pub const INT_DCD: u32 = 1 << 2; 546 pub const INT_CTS: u32 = 1 << 1; 547 pub const INT_RI: u32 = 1 << 0; 548 pub const INT_E: u32 = INT_OE | INT_BE | INT_PE | INT_FE; 549 pub const INT_MS: u32 = INT_RI | INT_DSR | INT_DCD | INT_CTS; 550 551 #[repr(u32)] 552 pub enum Interrupt { 553 OE = 1 << 10, 554 BE = 1 << 9, 555 PE = 1 << 8, 556 FE = 1 << 7, 557 RT = 1 << 6, 558 TX = 1 << 5, 559 RX = 1 << 4, 560 DSR = 1 << 3, 561 DCD = 1 << 2, 562 CTS = 1 << 1, 563 RI = 1 << 0, 564 } 565 566 impl Interrupt { 567 pub const E: u32 = INT_OE | INT_BE | INT_PE | INT_FE; 568 pub const MS: u32 = INT_RI | INT_DSR | INT_DCD | INT_CTS; 569 } 570 } 571 572 // TODO: You must disable the UART before any of the control registers are 573 // reprogrammed. When the UART is disabled in the middle of transmission or 574 // reception, it completes the current character before stopping 575