137fdb2f5SManos Pitsidianakis // Copyright 2024, Linaro Limited 237fdb2f5SManos Pitsidianakis // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org> 337fdb2f5SManos Pitsidianakis // SPDX-License-Identifier: GPL-2.0-or-later 437fdb2f5SManos Pitsidianakis // 537fdb2f5SManos Pitsidianakis // PL011 QEMU Device Model 637fdb2f5SManos Pitsidianakis // 737fdb2f5SManos Pitsidianakis // This library implements a device model for the PrimeCell® UART (PL011) 837fdb2f5SManos Pitsidianakis // device in QEMU. 937fdb2f5SManos Pitsidianakis // 1037fdb2f5SManos Pitsidianakis #![doc = include_str!("../README.md")] 1137fdb2f5SManos Pitsidianakis //! # Library crate 1237fdb2f5SManos Pitsidianakis //! 1337fdb2f5SManos Pitsidianakis //! See [`PL011State`](crate::device::PL011State) for the device model type and 1437fdb2f5SManos Pitsidianakis //! the [`registers`] module for register types. 1537fdb2f5SManos Pitsidianakis 1637fdb2f5SManos Pitsidianakis #![deny( 1737fdb2f5SManos Pitsidianakis rustdoc::broken_intra_doc_links, 1837fdb2f5SManos Pitsidianakis rustdoc::redundant_explicit_links, 1937fdb2f5SManos Pitsidianakis clippy::correctness, 2037fdb2f5SManos Pitsidianakis clippy::suspicious, 2137fdb2f5SManos Pitsidianakis clippy::complexity, 2237fdb2f5SManos Pitsidianakis clippy::perf, 2337fdb2f5SManos Pitsidianakis clippy::cargo, 2437fdb2f5SManos Pitsidianakis clippy::nursery, 2537fdb2f5SManos Pitsidianakis clippy::style, 2637fdb2f5SManos Pitsidianakis // restriction group 2737fdb2f5SManos Pitsidianakis clippy::dbg_macro, 2837fdb2f5SManos Pitsidianakis clippy::as_underscore, 2937fdb2f5SManos Pitsidianakis clippy::assertions_on_result_states, 3037fdb2f5SManos Pitsidianakis // pedantic group 3137fdb2f5SManos Pitsidianakis clippy::doc_markdown, 3237fdb2f5SManos Pitsidianakis clippy::borrow_as_ptr, 3337fdb2f5SManos Pitsidianakis clippy::cast_lossless, 3437fdb2f5SManos Pitsidianakis clippy::option_if_let_else, 3537fdb2f5SManos Pitsidianakis clippy::missing_const_for_fn, 3637fdb2f5SManos Pitsidianakis clippy::cognitive_complexity, 3737fdb2f5SManos Pitsidianakis clippy::missing_safety_doc, 3837fdb2f5SManos Pitsidianakis )] 3993243319SManos Pitsidianakis #![allow(clippy::result_unit_err)] 4037fdb2f5SManos Pitsidianakis 4137fdb2f5SManos Pitsidianakis extern crate bilge; 4237fdb2f5SManos Pitsidianakis extern crate bilge_impl; 4337fdb2f5SManos Pitsidianakis extern crate qemu_api; 4437fdb2f5SManos Pitsidianakis 45*718e255fSPaolo Bonzini use qemu_api::c_str; 46*718e255fSPaolo Bonzini 4737fdb2f5SManos Pitsidianakis pub mod device; 4837fdb2f5SManos Pitsidianakis pub mod device_class; 4937fdb2f5SManos Pitsidianakis pub mod memory_ops; 5037fdb2f5SManos Pitsidianakis 51*718e255fSPaolo Bonzini pub const TYPE_PL011: &::std::ffi::CStr = c_str!("pl011"); 52*718e255fSPaolo Bonzini pub const TYPE_PL011_LUMINARY: &::std::ffi::CStr = c_str!("pl011_luminary"); 5337fdb2f5SManos Pitsidianakis 5437fdb2f5SManos Pitsidianakis /// Offset of each register from the base memory address of the device. 5537fdb2f5SManos Pitsidianakis /// 5637fdb2f5SManos Pitsidianakis /// # Source 5737fdb2f5SManos Pitsidianakis /// ARM DDI 0183G, Table 3-1 p.3-3 5837fdb2f5SManos Pitsidianakis #[doc(alias = "offset")] 5937fdb2f5SManos Pitsidianakis #[allow(non_camel_case_types)] 6037fdb2f5SManos Pitsidianakis #[repr(u64)] 6137fdb2f5SManos Pitsidianakis #[derive(Debug)] 6237fdb2f5SManos Pitsidianakis pub enum RegisterOffset { 6337fdb2f5SManos Pitsidianakis /// Data Register 6437fdb2f5SManos Pitsidianakis /// 6537fdb2f5SManos Pitsidianakis /// A write to this register initiates the actual data transmission 6637fdb2f5SManos Pitsidianakis #[doc(alias = "UARTDR")] 6737fdb2f5SManos Pitsidianakis DR = 0x000, 6837fdb2f5SManos Pitsidianakis /// Receive Status Register or Error Clear Register 6937fdb2f5SManos Pitsidianakis #[doc(alias = "UARTRSR")] 7037fdb2f5SManos Pitsidianakis #[doc(alias = "UARTECR")] 7137fdb2f5SManos Pitsidianakis RSR = 0x004, 7237fdb2f5SManos Pitsidianakis /// Flag Register 7337fdb2f5SManos Pitsidianakis /// 7437fdb2f5SManos Pitsidianakis /// A read of this register shows if transmission is complete 7537fdb2f5SManos Pitsidianakis #[doc(alias = "UARTFR")] 7637fdb2f5SManos Pitsidianakis FR = 0x018, 7737fdb2f5SManos Pitsidianakis /// Fractional Baud Rate Register 7837fdb2f5SManos Pitsidianakis /// 7937fdb2f5SManos Pitsidianakis /// responsible for baud rate speed 8037fdb2f5SManos Pitsidianakis #[doc(alias = "UARTFBRD")] 8137fdb2f5SManos Pitsidianakis FBRD = 0x028, 8237fdb2f5SManos Pitsidianakis /// `IrDA` Low-Power Counter Register 8337fdb2f5SManos Pitsidianakis #[doc(alias = "UARTILPR")] 8437fdb2f5SManos Pitsidianakis ILPR = 0x020, 8537fdb2f5SManos Pitsidianakis /// Integer Baud Rate Register 8637fdb2f5SManos Pitsidianakis /// 8737fdb2f5SManos Pitsidianakis /// Responsible for baud rate speed 8837fdb2f5SManos Pitsidianakis #[doc(alias = "UARTIBRD")] 8937fdb2f5SManos Pitsidianakis IBRD = 0x024, 9037fdb2f5SManos Pitsidianakis /// line control register (data frame format) 9137fdb2f5SManos Pitsidianakis #[doc(alias = "UARTLCR_H")] 9237fdb2f5SManos Pitsidianakis LCR_H = 0x02C, 9337fdb2f5SManos Pitsidianakis /// Toggle UART, transmission or reception 9437fdb2f5SManos Pitsidianakis #[doc(alias = "UARTCR")] 9537fdb2f5SManos Pitsidianakis CR = 0x030, 9637fdb2f5SManos Pitsidianakis /// Interrupt FIFO Level Select Register 9737fdb2f5SManos Pitsidianakis #[doc(alias = "UARTIFLS")] 9837fdb2f5SManos Pitsidianakis FLS = 0x034, 9937fdb2f5SManos Pitsidianakis /// Interrupt Mask Set/Clear Register 10037fdb2f5SManos Pitsidianakis #[doc(alias = "UARTIMSC")] 10137fdb2f5SManos Pitsidianakis IMSC = 0x038, 10237fdb2f5SManos Pitsidianakis /// Raw Interrupt Status Register 10337fdb2f5SManos Pitsidianakis #[doc(alias = "UARTRIS")] 10437fdb2f5SManos Pitsidianakis RIS = 0x03C, 10537fdb2f5SManos Pitsidianakis /// Masked Interrupt Status Register 10637fdb2f5SManos Pitsidianakis #[doc(alias = "UARTMIS")] 10737fdb2f5SManos Pitsidianakis MIS = 0x040, 10837fdb2f5SManos Pitsidianakis /// Interrupt Clear Register 10937fdb2f5SManos Pitsidianakis #[doc(alias = "UARTICR")] 11037fdb2f5SManos Pitsidianakis ICR = 0x044, 11137fdb2f5SManos Pitsidianakis /// DMA control Register 11237fdb2f5SManos Pitsidianakis #[doc(alias = "UARTDMACR")] 11337fdb2f5SManos Pitsidianakis DMACR = 0x048, 11437fdb2f5SManos Pitsidianakis ///// Reserved, offsets `0x04C` to `0x07C`. 11537fdb2f5SManos Pitsidianakis //Reserved = 0x04C, 11637fdb2f5SManos Pitsidianakis } 11737fdb2f5SManos Pitsidianakis 11837fdb2f5SManos Pitsidianakis impl core::convert::TryFrom<u64> for RegisterOffset { 11937fdb2f5SManos Pitsidianakis type Error = u64; 12037fdb2f5SManos Pitsidianakis try_from(value: u64) -> Result<Self, Self::Error>12137fdb2f5SManos Pitsidianakis fn try_from(value: u64) -> Result<Self, Self::Error> { 12237fdb2f5SManos Pitsidianakis macro_rules! case { 12337fdb2f5SManos Pitsidianakis ($($discriminant:ident),*$(,)*) => { 12437fdb2f5SManos Pitsidianakis /* check that matching on all macro arguments compiles, which means we are not 12537fdb2f5SManos Pitsidianakis * missing any enum value; if the type definition ever changes this will stop 12637fdb2f5SManos Pitsidianakis * compiling. 12737fdb2f5SManos Pitsidianakis */ 12837fdb2f5SManos Pitsidianakis const fn _assert_exhaustive(val: RegisterOffset) { 12937fdb2f5SManos Pitsidianakis match val { 13037fdb2f5SManos Pitsidianakis $(RegisterOffset::$discriminant => (),)* 13137fdb2f5SManos Pitsidianakis } 13237fdb2f5SManos Pitsidianakis } 13337fdb2f5SManos Pitsidianakis 13437fdb2f5SManos Pitsidianakis match value { 13537fdb2f5SManos Pitsidianakis $(x if x == Self::$discriminant as u64 => Ok(Self::$discriminant),)* 13637fdb2f5SManos Pitsidianakis _ => Err(value), 13737fdb2f5SManos Pitsidianakis } 13837fdb2f5SManos Pitsidianakis } 13937fdb2f5SManos Pitsidianakis } 14037fdb2f5SManos Pitsidianakis case! { DR, RSR, FR, FBRD, ILPR, IBRD, LCR_H, CR, FLS, IMSC, RIS, MIS, ICR, DMACR } 14137fdb2f5SManos Pitsidianakis } 14237fdb2f5SManos Pitsidianakis } 14337fdb2f5SManos Pitsidianakis 14437fdb2f5SManos Pitsidianakis pub mod registers { 14537fdb2f5SManos Pitsidianakis //! Device registers exposed as typed structs which are backed by arbitrary 14637fdb2f5SManos Pitsidianakis //! integer bitmaps. [`Data`], [`Control`], [`LineControl`], etc. 14737fdb2f5SManos Pitsidianakis //! 14837fdb2f5SManos Pitsidianakis //! All PL011 registers are essentially 32-bit wide, but are typed here as 14937fdb2f5SManos Pitsidianakis //! bitmaps with only the necessary width. That is, if a struct bitmap 15037fdb2f5SManos Pitsidianakis //! in this module is for example 16 bits long, it should be conceived 15137fdb2f5SManos Pitsidianakis //! as a 32-bit register where the unmentioned higher bits are always 15237fdb2f5SManos Pitsidianakis //! unused thus treated as zero when read or written. 15337fdb2f5SManos Pitsidianakis use bilge::prelude::*; 15437fdb2f5SManos Pitsidianakis 15537fdb2f5SManos Pitsidianakis // TODO: FIFO Mode has different semantics 15637fdb2f5SManos Pitsidianakis /// Data Register, `UARTDR` 15737fdb2f5SManos Pitsidianakis /// 15837fdb2f5SManos Pitsidianakis /// The `UARTDR` register is the data register. 15937fdb2f5SManos Pitsidianakis /// 16037fdb2f5SManos Pitsidianakis /// For words to be transmitted: 16137fdb2f5SManos Pitsidianakis /// 16237fdb2f5SManos Pitsidianakis /// - if the FIFOs are enabled, data written to this location is pushed onto 16337fdb2f5SManos Pitsidianakis /// the transmit 16437fdb2f5SManos Pitsidianakis /// FIFO 16537fdb2f5SManos Pitsidianakis /// - if the FIFOs are not enabled, data is stored in the transmitter 16637fdb2f5SManos Pitsidianakis /// holding register (the 16737fdb2f5SManos Pitsidianakis /// bottom word of the transmit FIFO). 16837fdb2f5SManos Pitsidianakis /// 16937fdb2f5SManos Pitsidianakis /// The write operation initiates transmission from the UART. The data is 17037fdb2f5SManos Pitsidianakis /// prefixed with a start bit, appended with the appropriate parity bit 17137fdb2f5SManos Pitsidianakis /// (if parity is enabled), and a stop bit. The resultant word is then 17237fdb2f5SManos Pitsidianakis /// transmitted. 17337fdb2f5SManos Pitsidianakis /// 17437fdb2f5SManos Pitsidianakis /// For received words: 17537fdb2f5SManos Pitsidianakis /// 17637fdb2f5SManos Pitsidianakis /// - if the FIFOs are enabled, the data byte and the 4-bit status (break, 17737fdb2f5SManos Pitsidianakis /// frame, parity, 17837fdb2f5SManos Pitsidianakis /// and overrun) is pushed onto the 12-bit wide receive FIFO 17937fdb2f5SManos Pitsidianakis /// - if the FIFOs are not enabled, the data byte and status are stored in 18037fdb2f5SManos Pitsidianakis /// the receiving 18137fdb2f5SManos Pitsidianakis /// holding register (the bottom word of the receive FIFO). 18237fdb2f5SManos Pitsidianakis /// 18337fdb2f5SManos Pitsidianakis /// The received data byte is read by performing reads from the `UARTDR` 18437fdb2f5SManos Pitsidianakis /// register along with the corresponding status information. The status 18537fdb2f5SManos Pitsidianakis /// information can also be read by a read of the `UARTRSR/UARTECR` 18637fdb2f5SManos Pitsidianakis /// register. 18737fdb2f5SManos Pitsidianakis /// 18837fdb2f5SManos Pitsidianakis /// # Note 18937fdb2f5SManos Pitsidianakis /// 19037fdb2f5SManos Pitsidianakis /// You must disable the UART before any of the control registers are 19137fdb2f5SManos Pitsidianakis /// reprogrammed. When the UART is disabled in the middle of 19237fdb2f5SManos Pitsidianakis /// transmission or reception, it completes the current character before 19337fdb2f5SManos Pitsidianakis /// stopping. 19437fdb2f5SManos Pitsidianakis /// 19537fdb2f5SManos Pitsidianakis /// # Source 19637fdb2f5SManos Pitsidianakis /// ARM DDI 0183G 3.3.1 Data Register, UARTDR 19737fdb2f5SManos Pitsidianakis #[bitsize(16)] 19837fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, DebugBits, FromBits)] 19937fdb2f5SManos Pitsidianakis #[doc(alias = "UARTDR")] 20037fdb2f5SManos Pitsidianakis pub struct Data { 20137fdb2f5SManos Pitsidianakis _reserved: u4, 20237fdb2f5SManos Pitsidianakis pub data: u8, 20337fdb2f5SManos Pitsidianakis pub framing_error: bool, 20437fdb2f5SManos Pitsidianakis pub parity_error: bool, 20537fdb2f5SManos Pitsidianakis pub break_error: bool, 20637fdb2f5SManos Pitsidianakis pub overrun_error: bool, 20737fdb2f5SManos Pitsidianakis } 20837fdb2f5SManos Pitsidianakis 20937fdb2f5SManos Pitsidianakis // TODO: FIFO Mode has different semantics 21037fdb2f5SManos Pitsidianakis /// Receive Status Register / Error Clear Register, `UARTRSR/UARTECR` 21137fdb2f5SManos Pitsidianakis /// 21237fdb2f5SManos Pitsidianakis /// The UARTRSR/UARTECR register is the receive status register/error clear 21337fdb2f5SManos Pitsidianakis /// register. Receive status can also be read from the `UARTRSR` 21437fdb2f5SManos Pitsidianakis /// register. If the status is read from this register, then the status 21537fdb2f5SManos Pitsidianakis /// information for break, framing and parity corresponds to the 21637fdb2f5SManos Pitsidianakis /// data character read from the [Data register](Data), `UARTDR` prior to 21737fdb2f5SManos Pitsidianakis /// reading the UARTRSR register. The status information for overrun is 21837fdb2f5SManos Pitsidianakis /// set immediately when an overrun condition occurs. 21937fdb2f5SManos Pitsidianakis /// 22037fdb2f5SManos Pitsidianakis /// 22137fdb2f5SManos Pitsidianakis /// # Note 22237fdb2f5SManos Pitsidianakis /// The received data character must be read first from the [Data 22337fdb2f5SManos Pitsidianakis /// Register](Data), `UARTDR` before reading the error status associated 22437fdb2f5SManos Pitsidianakis /// with that data character from the `UARTRSR` register. This read 22537fdb2f5SManos Pitsidianakis /// sequence cannot be reversed, because the `UARTRSR` register is 22637fdb2f5SManos Pitsidianakis /// updated only when a read occurs from the `UARTDR` register. However, 22737fdb2f5SManos Pitsidianakis /// the status information can also be obtained by reading the `UARTDR` 22837fdb2f5SManos Pitsidianakis /// register 22937fdb2f5SManos Pitsidianakis /// 23037fdb2f5SManos Pitsidianakis /// # Source 23137fdb2f5SManos Pitsidianakis /// ARM DDI 0183G 3.3.2 Receive Status Register/Error Clear Register, 23237fdb2f5SManos Pitsidianakis /// UARTRSR/UARTECR 23337fdb2f5SManos Pitsidianakis #[bitsize(8)] 23437fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, DebugBits, FromBits)] 23537fdb2f5SManos Pitsidianakis pub struct ReceiveStatusErrorClear { 23637fdb2f5SManos Pitsidianakis pub framing_error: bool, 23737fdb2f5SManos Pitsidianakis pub parity_error: bool, 23837fdb2f5SManos Pitsidianakis pub break_error: bool, 23937fdb2f5SManos Pitsidianakis pub overrun_error: bool, 24037fdb2f5SManos Pitsidianakis _reserved_unpredictable: u4, 24137fdb2f5SManos Pitsidianakis } 24237fdb2f5SManos Pitsidianakis 24337fdb2f5SManos Pitsidianakis impl ReceiveStatusErrorClear { reset(&mut self)24437fdb2f5SManos Pitsidianakis pub fn reset(&mut self) { 24537fdb2f5SManos Pitsidianakis // All the bits are cleared to 0 on reset. 24637fdb2f5SManos Pitsidianakis *self = 0.into(); 24737fdb2f5SManos Pitsidianakis } 24837fdb2f5SManos Pitsidianakis } 24937fdb2f5SManos Pitsidianakis 25037fdb2f5SManos Pitsidianakis impl Default for ReceiveStatusErrorClear { default() -> Self25137fdb2f5SManos Pitsidianakis fn default() -> Self { 25237fdb2f5SManos Pitsidianakis 0.into() 25337fdb2f5SManos Pitsidianakis } 25437fdb2f5SManos Pitsidianakis } 25537fdb2f5SManos Pitsidianakis 25637fdb2f5SManos Pitsidianakis #[bitsize(16)] 25737fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, DebugBits, FromBits)] 25837fdb2f5SManos Pitsidianakis /// Flag Register, `UARTFR` 25937fdb2f5SManos Pitsidianakis #[doc(alias = "UARTFR")] 26037fdb2f5SManos Pitsidianakis pub struct Flags { 26137fdb2f5SManos Pitsidianakis /// CTS Clear to send. This bit is the complement of the UART clear to 26237fdb2f5SManos Pitsidianakis /// send, `nUARTCTS`, modem status input. That is, the bit is 1 26337fdb2f5SManos Pitsidianakis /// when `nUARTCTS` is LOW. 26437fdb2f5SManos Pitsidianakis pub clear_to_send: bool, 26537fdb2f5SManos Pitsidianakis /// DSR Data set ready. This bit is the complement of the UART data set 26637fdb2f5SManos Pitsidianakis /// ready, `nUARTDSR`, modem status input. That is, the bit is 1 when 26737fdb2f5SManos Pitsidianakis /// `nUARTDSR` is LOW. 26837fdb2f5SManos Pitsidianakis pub data_set_ready: bool, 26937fdb2f5SManos Pitsidianakis /// DCD Data carrier detect. This bit is the complement of the UART data 27037fdb2f5SManos Pitsidianakis /// carrier detect, `nUARTDCD`, modem status input. That is, the bit is 27137fdb2f5SManos Pitsidianakis /// 1 when `nUARTDCD` is LOW. 27237fdb2f5SManos Pitsidianakis pub data_carrier_detect: bool, 27337fdb2f5SManos Pitsidianakis /// BUSY UART busy. If this bit is set to 1, the UART is busy 27437fdb2f5SManos Pitsidianakis /// transmitting data. This bit remains set until the complete 27537fdb2f5SManos Pitsidianakis /// byte, including all the stop bits, has been sent from the 27637fdb2f5SManos Pitsidianakis /// shift register. This bit is set as soon as the transmit FIFO 27737fdb2f5SManos Pitsidianakis /// becomes non-empty, regardless of whether the UART is enabled 27837fdb2f5SManos Pitsidianakis /// or not. 27937fdb2f5SManos Pitsidianakis pub busy: bool, 28037fdb2f5SManos Pitsidianakis /// RXFE Receive FIFO empty. The meaning of this bit depends on the 28137fdb2f5SManos Pitsidianakis /// state of the FEN bit in the UARTLCR_H register. If the FIFO 28237fdb2f5SManos Pitsidianakis /// is disabled, this bit is set when the receive holding 28337fdb2f5SManos Pitsidianakis /// register is empty. If the FIFO is enabled, the RXFE bit is 28437fdb2f5SManos Pitsidianakis /// set when the receive FIFO is empty. 28537fdb2f5SManos Pitsidianakis pub receive_fifo_empty: bool, 28637fdb2f5SManos Pitsidianakis /// TXFF Transmit FIFO full. The meaning of this bit depends on the 28737fdb2f5SManos Pitsidianakis /// state of the FEN bit in the UARTLCR_H register. If the FIFO 28837fdb2f5SManos Pitsidianakis /// is disabled, this bit is set when the transmit holding 28937fdb2f5SManos Pitsidianakis /// register is full. If the FIFO is enabled, the TXFF bit is 29037fdb2f5SManos Pitsidianakis /// set when the transmit FIFO is full. 29137fdb2f5SManos Pitsidianakis pub transmit_fifo_full: bool, 29237fdb2f5SManos Pitsidianakis /// RXFF Receive FIFO full. The meaning of this bit depends on the state 29337fdb2f5SManos Pitsidianakis /// of the FEN bit in the UARTLCR_H register. If the FIFO is 29437fdb2f5SManos Pitsidianakis /// disabled, this bit is set when the receive holding register 29537fdb2f5SManos Pitsidianakis /// is full. If the FIFO is enabled, the RXFF bit is set when 29637fdb2f5SManos Pitsidianakis /// the receive FIFO is full. 29737fdb2f5SManos Pitsidianakis pub receive_fifo_full: bool, 29837fdb2f5SManos Pitsidianakis /// Transmit FIFO empty. The meaning of this bit depends on the state of 29937fdb2f5SManos Pitsidianakis /// the FEN bit in the [Line Control register](LineControl), 30037fdb2f5SManos Pitsidianakis /// `UARTLCR_H`. If the FIFO is disabled, this bit is set when the 30137fdb2f5SManos Pitsidianakis /// transmit holding register is empty. If the FIFO is enabled, 30237fdb2f5SManos Pitsidianakis /// the TXFE bit is set when the transmit FIFO is empty. This 30337fdb2f5SManos Pitsidianakis /// bit does not indicate if there is data in the transmit shift 30437fdb2f5SManos Pitsidianakis /// register. 30537fdb2f5SManos Pitsidianakis pub transmit_fifo_empty: bool, 30637fdb2f5SManos Pitsidianakis /// `RI`, is `true` when `nUARTRI` is `LOW`. 30737fdb2f5SManos Pitsidianakis pub ring_indicator: bool, 30837fdb2f5SManos Pitsidianakis _reserved_zero_no_modify: u7, 30937fdb2f5SManos Pitsidianakis } 31037fdb2f5SManos Pitsidianakis 31137fdb2f5SManos Pitsidianakis impl Flags { reset(&mut self)31237fdb2f5SManos Pitsidianakis pub fn reset(&mut self) { 31337fdb2f5SManos Pitsidianakis // After reset TXFF, RXFF, and BUSY are 0, and TXFE and RXFE are 1 31437fdb2f5SManos Pitsidianakis self.set_receive_fifo_full(false); 31537fdb2f5SManos Pitsidianakis self.set_transmit_fifo_full(false); 31637fdb2f5SManos Pitsidianakis self.set_busy(false); 31737fdb2f5SManos Pitsidianakis self.set_receive_fifo_empty(true); 31837fdb2f5SManos Pitsidianakis self.set_transmit_fifo_empty(true); 31937fdb2f5SManos Pitsidianakis } 32037fdb2f5SManos Pitsidianakis } 32137fdb2f5SManos Pitsidianakis 32237fdb2f5SManos Pitsidianakis impl Default for Flags { default() -> Self32337fdb2f5SManos Pitsidianakis fn default() -> Self { 32437fdb2f5SManos Pitsidianakis let mut ret: Self = 0.into(); 32537fdb2f5SManos Pitsidianakis ret.reset(); 32637fdb2f5SManos Pitsidianakis ret 32737fdb2f5SManos Pitsidianakis } 32837fdb2f5SManos Pitsidianakis } 32937fdb2f5SManos Pitsidianakis 33037fdb2f5SManos Pitsidianakis #[bitsize(16)] 33137fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, DebugBits, FromBits)] 33237fdb2f5SManos Pitsidianakis /// Line Control Register, `UARTLCR_H` 33337fdb2f5SManos Pitsidianakis #[doc(alias = "UARTLCR_H")] 33437fdb2f5SManos Pitsidianakis pub struct LineControl { 33537fdb2f5SManos Pitsidianakis /// 15:8 - Reserved, do not modify, read as zero. 33637fdb2f5SManos Pitsidianakis _reserved_zero_no_modify: u8, 33737fdb2f5SManos Pitsidianakis /// 7 SPS Stick parity select. 33837fdb2f5SManos Pitsidianakis /// 0 = stick parity is disabled 33937fdb2f5SManos Pitsidianakis /// 1 = either: 34037fdb2f5SManos Pitsidianakis /// • if the EPS bit is 0 then the parity bit is transmitted and checked 34137fdb2f5SManos Pitsidianakis /// as a 1 • if the EPS bit is 1 then the parity bit is 34237fdb2f5SManos Pitsidianakis /// transmitted and checked as a 0. This bit has no effect when 34337fdb2f5SManos Pitsidianakis /// the PEN bit disables parity checking and generation. See Table 3-11 34437fdb2f5SManos Pitsidianakis /// on page 3-14 for the parity truth table. 34537fdb2f5SManos Pitsidianakis pub sticky_parity: bool, 34637fdb2f5SManos Pitsidianakis /// WLEN Word length. These bits indicate the number of data bits 34737fdb2f5SManos Pitsidianakis /// transmitted or received in a frame as follows: b11 = 8 bits 34837fdb2f5SManos Pitsidianakis /// b10 = 7 bits 34937fdb2f5SManos Pitsidianakis /// b01 = 6 bits 35037fdb2f5SManos Pitsidianakis /// b00 = 5 bits. 35137fdb2f5SManos Pitsidianakis pub word_length: WordLength, 35237fdb2f5SManos Pitsidianakis /// FEN Enable FIFOs: 35337fdb2f5SManos Pitsidianakis /// 0 = FIFOs are disabled (character mode) that is, the FIFOs become 35437fdb2f5SManos Pitsidianakis /// 1-byte-deep holding registers 1 = transmit and receive FIFO 35537fdb2f5SManos Pitsidianakis /// buffers are enabled (FIFO mode). 35637fdb2f5SManos Pitsidianakis pub fifos_enabled: Mode, 35737fdb2f5SManos Pitsidianakis /// 3 STP2 Two stop bits select. If this bit is set to 1, two stop bits 35837fdb2f5SManos Pitsidianakis /// are transmitted at the end of the frame. The receive 35937fdb2f5SManos Pitsidianakis /// logic does not check for two stop bits being received. 36037fdb2f5SManos Pitsidianakis pub two_stops_bits: bool, 36137fdb2f5SManos Pitsidianakis /// EPS Even parity select. Controls the type of parity the UART uses 36237fdb2f5SManos Pitsidianakis /// during transmission and reception: 36337fdb2f5SManos Pitsidianakis /// - 0 = odd parity. The UART generates or checks for an odd number of 36437fdb2f5SManos Pitsidianakis /// 1s in the data and parity bits. 36537fdb2f5SManos Pitsidianakis /// - 1 = even parity. The UART generates or checks for an even number 36637fdb2f5SManos Pitsidianakis /// of 1s in the data and parity bits. 36737fdb2f5SManos Pitsidianakis /// This bit has no effect when the `PEN` bit disables parity checking 36837fdb2f5SManos Pitsidianakis /// and generation. See Table 3-11 on page 3-14 for the parity 36937fdb2f5SManos Pitsidianakis /// truth table. 37037fdb2f5SManos Pitsidianakis pub parity: Parity, 37137fdb2f5SManos Pitsidianakis /// 1 PEN Parity enable: 37237fdb2f5SManos Pitsidianakis /// 37337fdb2f5SManos Pitsidianakis /// - 0 = parity is disabled and no parity bit added to the data frame 37437fdb2f5SManos Pitsidianakis /// - 1 = parity checking and generation is enabled. 37537fdb2f5SManos Pitsidianakis /// 37637fdb2f5SManos Pitsidianakis /// See Table 3-11 on page 3-14 for the parity truth table. 37737fdb2f5SManos Pitsidianakis pub parity_enabled: bool, 37837fdb2f5SManos Pitsidianakis /// BRK Send break. 37937fdb2f5SManos Pitsidianakis /// 38037fdb2f5SManos Pitsidianakis /// If this bit is set to `1`, a low-level is continually output on the 38137fdb2f5SManos Pitsidianakis /// `UARTTXD` output, after completing transmission of the 38237fdb2f5SManos Pitsidianakis /// current character. For the proper execution of the break command, 38337fdb2f5SManos Pitsidianakis /// the software must set this bit for at least two complete 38437fdb2f5SManos Pitsidianakis /// frames. For normal use, this bit must be cleared to `0`. 38537fdb2f5SManos Pitsidianakis pub send_break: bool, 38637fdb2f5SManos Pitsidianakis } 38737fdb2f5SManos Pitsidianakis 38837fdb2f5SManos Pitsidianakis impl LineControl { reset(&mut self)38937fdb2f5SManos Pitsidianakis pub fn reset(&mut self) { 39037fdb2f5SManos Pitsidianakis // All the bits are cleared to 0 when reset. 39137fdb2f5SManos Pitsidianakis *self = 0.into(); 39237fdb2f5SManos Pitsidianakis } 39337fdb2f5SManos Pitsidianakis } 39437fdb2f5SManos Pitsidianakis 39537fdb2f5SManos Pitsidianakis impl Default for LineControl { default() -> Self39637fdb2f5SManos Pitsidianakis fn default() -> Self { 39737fdb2f5SManos Pitsidianakis 0.into() 39837fdb2f5SManos Pitsidianakis } 39937fdb2f5SManos Pitsidianakis } 40037fdb2f5SManos Pitsidianakis 40137fdb2f5SManos Pitsidianakis #[bitsize(1)] 40237fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, Debug, Eq, FromBits, PartialEq)] 40337fdb2f5SManos Pitsidianakis /// `EPS` "Even parity select", field of [Line Control 40437fdb2f5SManos Pitsidianakis /// register](LineControl). 40537fdb2f5SManos Pitsidianakis pub enum Parity { 40637fdb2f5SManos Pitsidianakis /// - 0 = odd parity. The UART generates or checks for an odd number of 40737fdb2f5SManos Pitsidianakis /// 1s in the data and parity bits. 40837fdb2f5SManos Pitsidianakis Odd = 0, 40937fdb2f5SManos Pitsidianakis /// - 1 = even parity. The UART generates or checks for an even number 41037fdb2f5SManos Pitsidianakis /// of 1s in the data and parity bits. 41137fdb2f5SManos Pitsidianakis Even = 1, 41237fdb2f5SManos Pitsidianakis } 41337fdb2f5SManos Pitsidianakis 41437fdb2f5SManos Pitsidianakis #[bitsize(1)] 41537fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, Debug, Eq, FromBits, PartialEq)] 41637fdb2f5SManos Pitsidianakis /// `FEN` "Enable FIFOs" or Device mode, field of [Line Control 41737fdb2f5SManos Pitsidianakis /// register](LineControl). 41837fdb2f5SManos Pitsidianakis pub enum Mode { 41937fdb2f5SManos Pitsidianakis /// 0 = FIFOs are disabled (character mode) that is, the FIFOs become 42037fdb2f5SManos Pitsidianakis /// 1-byte-deep holding registers 42137fdb2f5SManos Pitsidianakis Character = 0, 42237fdb2f5SManos Pitsidianakis /// 1 = transmit and receive FIFO buffers are enabled (FIFO mode). 42337fdb2f5SManos Pitsidianakis FIFO = 1, 42437fdb2f5SManos Pitsidianakis } 42537fdb2f5SManos Pitsidianakis 42637fdb2f5SManos Pitsidianakis impl From<Mode> for bool { from(val: Mode) -> Self42737fdb2f5SManos Pitsidianakis fn from(val: Mode) -> Self { 42837fdb2f5SManos Pitsidianakis matches!(val, Mode::FIFO) 42937fdb2f5SManos Pitsidianakis } 43037fdb2f5SManos Pitsidianakis } 43137fdb2f5SManos Pitsidianakis 43237fdb2f5SManos Pitsidianakis #[bitsize(2)] 43337fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, Debug, Eq, FromBits, PartialEq)] 43437fdb2f5SManos Pitsidianakis /// `WLEN` Word length, field of [Line Control register](LineControl). 43537fdb2f5SManos Pitsidianakis /// 43637fdb2f5SManos Pitsidianakis /// These bits indicate the number of data bits transmitted or received in a 43737fdb2f5SManos Pitsidianakis /// frame as follows: 43837fdb2f5SManos Pitsidianakis pub enum WordLength { 43937fdb2f5SManos Pitsidianakis /// b11 = 8 bits 44037fdb2f5SManos Pitsidianakis _8Bits = 0b11, 44137fdb2f5SManos Pitsidianakis /// b10 = 7 bits 44237fdb2f5SManos Pitsidianakis _7Bits = 0b10, 44337fdb2f5SManos Pitsidianakis /// b01 = 6 bits 44437fdb2f5SManos Pitsidianakis _6Bits = 0b01, 44537fdb2f5SManos Pitsidianakis /// b00 = 5 bits. 44637fdb2f5SManos Pitsidianakis _5Bits = 0b00, 44737fdb2f5SManos Pitsidianakis } 44837fdb2f5SManos Pitsidianakis 44937fdb2f5SManos Pitsidianakis /// Control Register, `UARTCR` 45037fdb2f5SManos Pitsidianakis /// 45137fdb2f5SManos Pitsidianakis /// The `UARTCR` register is the control register. All the bits are cleared 45237fdb2f5SManos Pitsidianakis /// to `0` on reset except for bits `9` and `8` that are set to `1`. 45337fdb2f5SManos Pitsidianakis /// 45437fdb2f5SManos Pitsidianakis /// # Source 45537fdb2f5SManos Pitsidianakis /// ARM DDI 0183G, 3.3.8 Control Register, `UARTCR`, Table 3-12 45637fdb2f5SManos Pitsidianakis #[bitsize(16)] 45737fdb2f5SManos Pitsidianakis #[doc(alias = "UARTCR")] 45837fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, DebugBits, FromBits)] 45937fdb2f5SManos Pitsidianakis pub struct Control { 46037fdb2f5SManos Pitsidianakis /// `UARTEN` UART enable: 0 = UART is disabled. If the UART is disabled 46137fdb2f5SManos Pitsidianakis /// in the middle of transmission or reception, it completes the current 46237fdb2f5SManos Pitsidianakis /// character before stopping. 1 = the UART is enabled. Data 46337fdb2f5SManos Pitsidianakis /// transmission and reception occurs for either UART signals or SIR 46437fdb2f5SManos Pitsidianakis /// signals depending on the setting of the SIREN bit. 46537fdb2f5SManos Pitsidianakis pub enable_uart: bool, 46637fdb2f5SManos Pitsidianakis /// `SIREN` `SIR` enable: 0 = IrDA SIR ENDEC is disabled. `nSIROUT` 46737fdb2f5SManos Pitsidianakis /// remains LOW (no light pulse generated), and signal transitions on 46837fdb2f5SManos Pitsidianakis /// SIRIN have no effect. 1 = IrDA SIR ENDEC is enabled. Data is 46937fdb2f5SManos Pitsidianakis /// transmitted and received on nSIROUT and SIRIN. UARTTXD remains HIGH, 47037fdb2f5SManos Pitsidianakis /// in the marking state. Signal transitions on UARTRXD or modem status 47137fdb2f5SManos Pitsidianakis /// inputs have no effect. This bit has no effect if the UARTEN bit 47237fdb2f5SManos Pitsidianakis /// disables the UART. 47337fdb2f5SManos Pitsidianakis pub enable_sir: bool, 47437fdb2f5SManos Pitsidianakis /// `SIRLP` SIR low-power IrDA mode. This bit selects the IrDA encoding 47537fdb2f5SManos Pitsidianakis /// mode. If this bit is cleared to 0, low-level bits are transmitted as 47637fdb2f5SManos Pitsidianakis /// an active high pulse with a width of 3/ 16th of the bit period. If 47737fdb2f5SManos Pitsidianakis /// this bit is set to 1, low-level bits are transmitted with a pulse 47837fdb2f5SManos Pitsidianakis /// width that is 3 times the period of the IrLPBaud16 input signal, 47937fdb2f5SManos Pitsidianakis /// regardless of the selected bit rate. Setting this bit uses less 48037fdb2f5SManos Pitsidianakis /// power, but might reduce transmission distances. 48137fdb2f5SManos Pitsidianakis pub sir_lowpower_irda_mode: u1, 48237fdb2f5SManos Pitsidianakis /// Reserved, do not modify, read as zero. 48337fdb2f5SManos Pitsidianakis _reserved_zero_no_modify: u4, 48437fdb2f5SManos Pitsidianakis /// `LBE` Loopback enable. If this bit is set to 1 and the SIREN bit is 48537fdb2f5SManos Pitsidianakis /// set to 1 and the SIRTEST bit in the Test Control register, UARTTCR 48637fdb2f5SManos Pitsidianakis /// on page 4-5 is set to 1, then the nSIROUT path is inverted, and fed 48737fdb2f5SManos Pitsidianakis /// through to the SIRIN path. The SIRTEST bit in the test register must 48837fdb2f5SManos Pitsidianakis /// be set to 1 to override the normal half-duplex SIR operation. This 48937fdb2f5SManos Pitsidianakis /// must be the requirement for accessing the test registers during 49037fdb2f5SManos Pitsidianakis /// normal operation, and SIRTEST must be cleared to 0 when loopback 49137fdb2f5SManos Pitsidianakis /// testing is finished. This feature reduces the amount of external 49237fdb2f5SManos Pitsidianakis /// coupling required during system test. If this bit is set to 1, and 49337fdb2f5SManos Pitsidianakis /// the SIRTEST bit is set to 0, the UARTTXD path is fed through to the 49437fdb2f5SManos Pitsidianakis /// UARTRXD path. In either SIR mode or UART mode, when this bit is set, 49537fdb2f5SManos Pitsidianakis /// the modem outputs are also fed through to the modem inputs. This bit 49637fdb2f5SManos Pitsidianakis /// is cleared to 0 on reset, to disable loopback. 49737fdb2f5SManos Pitsidianakis pub enable_loopback: bool, 49837fdb2f5SManos Pitsidianakis /// `TXE` Transmit enable. If this bit is set to 1, the transmit section 49937fdb2f5SManos Pitsidianakis /// of the UART is enabled. Data transmission occurs for either UART 50037fdb2f5SManos Pitsidianakis /// signals, or SIR signals depending on the setting of the SIREN bit. 50137fdb2f5SManos Pitsidianakis /// When the UART is disabled in the middle of transmission, it 50237fdb2f5SManos Pitsidianakis /// completes the current character before stopping. 50337fdb2f5SManos Pitsidianakis pub enable_transmit: bool, 50437fdb2f5SManos Pitsidianakis /// `RXE` Receive enable. If this bit is set to 1, the receive section 50537fdb2f5SManos Pitsidianakis /// of the UART is enabled. Data reception occurs for either UART 50637fdb2f5SManos Pitsidianakis /// signals or SIR signals depending on the setting of the SIREN bit. 50737fdb2f5SManos Pitsidianakis /// When the UART is disabled in the middle of reception, it completes 50837fdb2f5SManos Pitsidianakis /// the current character before stopping. 50937fdb2f5SManos Pitsidianakis pub enable_receive: bool, 51037fdb2f5SManos Pitsidianakis /// `DTR` Data transmit ready. This bit is the complement of the UART 51137fdb2f5SManos Pitsidianakis /// data transmit ready, `nUARTDTR`, modem status output. That is, when 51237fdb2f5SManos Pitsidianakis /// the bit is programmed to a 1 then `nUARTDTR` is LOW. 51337fdb2f5SManos Pitsidianakis pub data_transmit_ready: bool, 51437fdb2f5SManos Pitsidianakis /// `RTS` Request to send. This bit is the complement of the UART 51537fdb2f5SManos Pitsidianakis /// request to send, `nUARTRTS`, modem status output. That is, when the 51637fdb2f5SManos Pitsidianakis /// bit is programmed to a 1 then `nUARTRTS` is LOW. 51737fdb2f5SManos Pitsidianakis pub request_to_send: bool, 51837fdb2f5SManos Pitsidianakis /// `Out1` This bit is the complement of the UART Out1 (`nUARTOut1`) 51937fdb2f5SManos Pitsidianakis /// modem status output. That is, when the bit is programmed to a 1 the 52037fdb2f5SManos Pitsidianakis /// output is 0. For DTE this can be used as Data Carrier Detect (DCD). 52137fdb2f5SManos Pitsidianakis pub out_1: bool, 52237fdb2f5SManos Pitsidianakis /// `Out2` This bit is the complement of the UART Out2 (`nUARTOut2`) 52337fdb2f5SManos Pitsidianakis /// modem status output. That is, when the bit is programmed to a 1, the 52437fdb2f5SManos Pitsidianakis /// output is 0. For DTE this can be used as Ring Indicator (RI). 52537fdb2f5SManos Pitsidianakis pub out_2: bool, 52637fdb2f5SManos Pitsidianakis /// `RTSEn` RTS hardware flow control enable. If this bit is set to 1, 52737fdb2f5SManos Pitsidianakis /// RTS hardware flow control is enabled. Data is only requested when 52837fdb2f5SManos Pitsidianakis /// there is space in the receive FIFO for it to be received. 52937fdb2f5SManos Pitsidianakis pub rts_hardware_flow_control_enable: bool, 53037fdb2f5SManos Pitsidianakis /// `CTSEn` CTS hardware flow control enable. If this bit is set to 1, 53137fdb2f5SManos Pitsidianakis /// CTS hardware flow control is enabled. Data is only transmitted when 53237fdb2f5SManos Pitsidianakis /// the `nUARTCTS` signal is asserted. 53337fdb2f5SManos Pitsidianakis pub cts_hardware_flow_control_enable: bool, 53437fdb2f5SManos Pitsidianakis } 53537fdb2f5SManos Pitsidianakis 53637fdb2f5SManos Pitsidianakis impl Control { reset(&mut self)53737fdb2f5SManos Pitsidianakis pub fn reset(&mut self) { 53837fdb2f5SManos Pitsidianakis *self = 0.into(); 53937fdb2f5SManos Pitsidianakis self.set_enable_receive(true); 54037fdb2f5SManos Pitsidianakis self.set_enable_transmit(true); 54137fdb2f5SManos Pitsidianakis } 54237fdb2f5SManos Pitsidianakis } 54337fdb2f5SManos Pitsidianakis 54437fdb2f5SManos Pitsidianakis impl Default for Control { default() -> Self54537fdb2f5SManos Pitsidianakis fn default() -> Self { 54637fdb2f5SManos Pitsidianakis let mut ret: Self = 0.into(); 54737fdb2f5SManos Pitsidianakis ret.reset(); 54837fdb2f5SManos Pitsidianakis ret 54937fdb2f5SManos Pitsidianakis } 55037fdb2f5SManos Pitsidianakis } 55137fdb2f5SManos Pitsidianakis 55237fdb2f5SManos Pitsidianakis /// Interrupt status bits in UARTRIS, UARTMIS, UARTIMSC 55337fdb2f5SManos Pitsidianakis pub const INT_OE: u32 = 1 << 10; 55437fdb2f5SManos Pitsidianakis pub const INT_BE: u32 = 1 << 9; 55537fdb2f5SManos Pitsidianakis pub const INT_PE: u32 = 1 << 8; 55637fdb2f5SManos Pitsidianakis pub const INT_FE: u32 = 1 << 7; 55737fdb2f5SManos Pitsidianakis pub const INT_RT: u32 = 1 << 6; 55837fdb2f5SManos Pitsidianakis pub const INT_TX: u32 = 1 << 5; 55937fdb2f5SManos Pitsidianakis pub const INT_RX: u32 = 1 << 4; 56037fdb2f5SManos Pitsidianakis pub const INT_DSR: u32 = 1 << 3; 56137fdb2f5SManos Pitsidianakis pub const INT_DCD: u32 = 1 << 2; 56237fdb2f5SManos Pitsidianakis pub const INT_CTS: u32 = 1 << 1; 56337fdb2f5SManos Pitsidianakis pub const INT_RI: u32 = 1 << 0; 56437fdb2f5SManos Pitsidianakis pub const INT_E: u32 = INT_OE | INT_BE | INT_PE | INT_FE; 56537fdb2f5SManos Pitsidianakis pub const INT_MS: u32 = INT_RI | INT_DSR | INT_DCD | INT_CTS; 56637fdb2f5SManos Pitsidianakis 56737fdb2f5SManos Pitsidianakis #[repr(u32)] 56837fdb2f5SManos Pitsidianakis pub enum Interrupt { 56937fdb2f5SManos Pitsidianakis OE = 1 << 10, 57037fdb2f5SManos Pitsidianakis BE = 1 << 9, 57137fdb2f5SManos Pitsidianakis PE = 1 << 8, 57237fdb2f5SManos Pitsidianakis FE = 1 << 7, 57337fdb2f5SManos Pitsidianakis RT = 1 << 6, 57437fdb2f5SManos Pitsidianakis TX = 1 << 5, 57537fdb2f5SManos Pitsidianakis RX = 1 << 4, 57637fdb2f5SManos Pitsidianakis DSR = 1 << 3, 57737fdb2f5SManos Pitsidianakis DCD = 1 << 2, 57837fdb2f5SManos Pitsidianakis CTS = 1 << 1, 57937fdb2f5SManos Pitsidianakis RI = 1 << 0, 58037fdb2f5SManos Pitsidianakis } 58137fdb2f5SManos Pitsidianakis 58237fdb2f5SManos Pitsidianakis impl Interrupt { 58337fdb2f5SManos Pitsidianakis pub const E: u32 = INT_OE | INT_BE | INT_PE | INT_FE; 58437fdb2f5SManos Pitsidianakis pub const MS: u32 = INT_RI | INT_DSR | INT_DCD | INT_CTS; 58537fdb2f5SManos Pitsidianakis } 58637fdb2f5SManos Pitsidianakis } 58737fdb2f5SManos Pitsidianakis 58837fdb2f5SManos Pitsidianakis // TODO: You must disable the UART before any of the control registers are 58937fdb2f5SManos Pitsidianakis // reprogrammed. When the UART is disabled in the middle of transmission or 59037fdb2f5SManos Pitsidianakis // reception, it completes the current character before stopping 591