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