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 use core::{ 637fdb2f5SManos Pitsidianakis ffi::{c_int, c_uchar, c_uint, c_void, CStr}, 737fdb2f5SManos Pitsidianakis ptr::{addr_of, addr_of_mut, NonNull}, 837fdb2f5SManos Pitsidianakis }; 937fdb2f5SManos Pitsidianakis 1037fdb2f5SManos Pitsidianakis use qemu_api::{ 1137fdb2f5SManos Pitsidianakis bindings::{self, *}, 1237fdb2f5SManos Pitsidianakis definitions::ObjectImpl, 1337fdb2f5SManos Pitsidianakis }; 1437fdb2f5SManos Pitsidianakis 1537fdb2f5SManos Pitsidianakis use crate::{ 1637fdb2f5SManos Pitsidianakis memory_ops::PL011_OPS, 1737fdb2f5SManos Pitsidianakis registers::{self, Interrupt}, 1837fdb2f5SManos Pitsidianakis RegisterOffset, 1937fdb2f5SManos Pitsidianakis }; 2037fdb2f5SManos Pitsidianakis 2137fdb2f5SManos Pitsidianakis static PL011_ID_ARM: [c_uchar; 8] = [0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1]; 2237fdb2f5SManos Pitsidianakis 23*93243319SManos Pitsidianakis /// Integer Baud Rate Divider, `UARTIBRD` 24*93243319SManos Pitsidianakis const IBRD_MASK: u32 = 0x3f; 25*93243319SManos Pitsidianakis 26*93243319SManos Pitsidianakis /// Fractional Baud Rate Divider, `UARTFBRD` 27*93243319SManos Pitsidianakis const FBRD_MASK: u32 = 0xffff; 28*93243319SManos Pitsidianakis 2937fdb2f5SManos Pitsidianakis const DATA_BREAK: u32 = 1 << 10; 3037fdb2f5SManos Pitsidianakis 3137fdb2f5SManos Pitsidianakis /// QEMU sourced constant. 3237fdb2f5SManos Pitsidianakis pub const PL011_FIFO_DEPTH: usize = 16_usize; 3337fdb2f5SManos Pitsidianakis 3437fdb2f5SManos Pitsidianakis #[repr(C)] 3537fdb2f5SManos Pitsidianakis #[derive(Debug, qemu_api_macros::Object)] 3637fdb2f5SManos Pitsidianakis /// PL011 Device Model in QEMU 3737fdb2f5SManos Pitsidianakis pub struct PL011State { 3837fdb2f5SManos Pitsidianakis pub parent_obj: SysBusDevice, 3937fdb2f5SManos Pitsidianakis pub iomem: MemoryRegion, 4037fdb2f5SManos Pitsidianakis #[doc(alias = "fr")] 4137fdb2f5SManos Pitsidianakis pub flags: registers::Flags, 4237fdb2f5SManos Pitsidianakis #[doc(alias = "lcr")] 4337fdb2f5SManos Pitsidianakis pub line_control: registers::LineControl, 4437fdb2f5SManos Pitsidianakis #[doc(alias = "rsr")] 4537fdb2f5SManos Pitsidianakis pub receive_status_error_clear: registers::ReceiveStatusErrorClear, 4637fdb2f5SManos Pitsidianakis #[doc(alias = "cr")] 4737fdb2f5SManos Pitsidianakis pub control: registers::Control, 4837fdb2f5SManos Pitsidianakis pub dmacr: u32, 4937fdb2f5SManos Pitsidianakis pub int_enabled: u32, 5037fdb2f5SManos Pitsidianakis pub int_level: u32, 5137fdb2f5SManos Pitsidianakis pub read_fifo: [u32; PL011_FIFO_DEPTH], 5237fdb2f5SManos Pitsidianakis pub ilpr: u32, 5337fdb2f5SManos Pitsidianakis pub ibrd: u32, 5437fdb2f5SManos Pitsidianakis pub fbrd: u32, 5537fdb2f5SManos Pitsidianakis pub ifl: u32, 5637fdb2f5SManos Pitsidianakis pub read_pos: usize, 5737fdb2f5SManos Pitsidianakis pub read_count: usize, 5837fdb2f5SManos Pitsidianakis pub read_trigger: usize, 5937fdb2f5SManos Pitsidianakis #[doc(alias = "chr")] 6037fdb2f5SManos Pitsidianakis pub char_backend: CharBackend, 6137fdb2f5SManos Pitsidianakis /// QEMU interrupts 6237fdb2f5SManos Pitsidianakis /// 6337fdb2f5SManos Pitsidianakis /// ```text 6437fdb2f5SManos Pitsidianakis /// * sysbus MMIO region 0: device registers 6537fdb2f5SManos Pitsidianakis /// * sysbus IRQ 0: `UARTINTR` (combined interrupt line) 6637fdb2f5SManos Pitsidianakis /// * sysbus IRQ 1: `UARTRXINTR` (receive FIFO interrupt line) 6737fdb2f5SManos Pitsidianakis /// * sysbus IRQ 2: `UARTTXINTR` (transmit FIFO interrupt line) 6837fdb2f5SManos Pitsidianakis /// * sysbus IRQ 3: `UARTRTINTR` (receive timeout interrupt line) 6937fdb2f5SManos Pitsidianakis /// * sysbus IRQ 4: `UARTMSINTR` (momem status interrupt line) 7037fdb2f5SManos Pitsidianakis /// * sysbus IRQ 5: `UARTEINTR` (error interrupt line) 7137fdb2f5SManos Pitsidianakis /// ``` 7237fdb2f5SManos Pitsidianakis #[doc(alias = "irq")] 7337fdb2f5SManos Pitsidianakis pub interrupts: [qemu_irq; 6usize], 7437fdb2f5SManos Pitsidianakis #[doc(alias = "clk")] 7537fdb2f5SManos Pitsidianakis pub clock: NonNull<Clock>, 7637fdb2f5SManos Pitsidianakis #[doc(alias = "migrate_clk")] 7737fdb2f5SManos Pitsidianakis pub migrate_clock: bool, 7837fdb2f5SManos Pitsidianakis } 7937fdb2f5SManos Pitsidianakis 8037fdb2f5SManos Pitsidianakis impl ObjectImpl for PL011State { 8137fdb2f5SManos Pitsidianakis type Class = PL011Class; 8237fdb2f5SManos Pitsidianakis const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self }; 8337fdb2f5SManos Pitsidianakis const TYPE_NAME: &'static CStr = crate::TYPE_PL011; 8437fdb2f5SManos Pitsidianakis const PARENT_TYPE_NAME: Option<&'static CStr> = Some(TYPE_SYS_BUS_DEVICE); 8537fdb2f5SManos Pitsidianakis const ABSTRACT: bool = false; 8637fdb2f5SManos Pitsidianakis const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = Some(pl011_init); 8737fdb2f5SManos Pitsidianakis const INSTANCE_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None; 8837fdb2f5SManos Pitsidianakis const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> = None; 8937fdb2f5SManos Pitsidianakis } 9037fdb2f5SManos Pitsidianakis 9137fdb2f5SManos Pitsidianakis #[repr(C)] 9237fdb2f5SManos Pitsidianakis pub struct PL011Class { 9337fdb2f5SManos Pitsidianakis _inner: [u8; 0], 9437fdb2f5SManos Pitsidianakis } 9537fdb2f5SManos Pitsidianakis 9637fdb2f5SManos Pitsidianakis impl qemu_api::definitions::Class for PL011Class { 9737fdb2f5SManos Pitsidianakis const CLASS_INIT: Option< 9837fdb2f5SManos Pitsidianakis unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut core::ffi::c_void), 9937fdb2f5SManos Pitsidianakis > = Some(crate::device_class::pl011_class_init); 10037fdb2f5SManos Pitsidianakis const CLASS_BASE_INIT: Option< 10137fdb2f5SManos Pitsidianakis unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut core::ffi::c_void), 10237fdb2f5SManos Pitsidianakis > = None; 10337fdb2f5SManos Pitsidianakis } 10437fdb2f5SManos Pitsidianakis 10537fdb2f5SManos Pitsidianakis #[used] 10637fdb2f5SManos Pitsidianakis pub static CLK_NAME: &CStr = c"clk"; 10737fdb2f5SManos Pitsidianakis 10837fdb2f5SManos Pitsidianakis impl PL011State { 10937fdb2f5SManos Pitsidianakis /// Initializes a pre-allocated, unitialized instance of `PL011State`. 11037fdb2f5SManos Pitsidianakis /// 11137fdb2f5SManos Pitsidianakis /// # Safety 11237fdb2f5SManos Pitsidianakis /// 11337fdb2f5SManos Pitsidianakis /// `self` must point to a correctly sized and aligned location for the 11437fdb2f5SManos Pitsidianakis /// `PL011State` type. It must not be called more than once on the same 11537fdb2f5SManos Pitsidianakis /// location/instance. All its fields are expected to hold unitialized 11637fdb2f5SManos Pitsidianakis /// values with the sole exception of `parent_obj`. 11737fdb2f5SManos Pitsidianakis pub unsafe fn init(&mut self) { 11837fdb2f5SManos Pitsidianakis let dev = addr_of_mut!(*self).cast::<DeviceState>(); 11937fdb2f5SManos Pitsidianakis // SAFETY: 12037fdb2f5SManos Pitsidianakis // 12137fdb2f5SManos Pitsidianakis // self and self.iomem are guaranteed to be valid at this point since callers 12237fdb2f5SManos Pitsidianakis // must make sure the `self` reference is valid. 12337fdb2f5SManos Pitsidianakis unsafe { 12437fdb2f5SManos Pitsidianakis memory_region_init_io( 12537fdb2f5SManos Pitsidianakis addr_of_mut!(self.iomem), 12637fdb2f5SManos Pitsidianakis addr_of_mut!(*self).cast::<Object>(), 12737fdb2f5SManos Pitsidianakis &PL011_OPS, 12837fdb2f5SManos Pitsidianakis addr_of_mut!(*self).cast::<c_void>(), 12937fdb2f5SManos Pitsidianakis Self::TYPE_INFO.name, 13037fdb2f5SManos Pitsidianakis 0x1000, 13137fdb2f5SManos Pitsidianakis ); 13237fdb2f5SManos Pitsidianakis let sbd = addr_of_mut!(*self).cast::<SysBusDevice>(); 13337fdb2f5SManos Pitsidianakis sysbus_init_mmio(sbd, addr_of_mut!(self.iomem)); 13437fdb2f5SManos Pitsidianakis for irq in self.interrupts.iter_mut() { 13537fdb2f5SManos Pitsidianakis sysbus_init_irq(sbd, irq); 13637fdb2f5SManos Pitsidianakis } 13737fdb2f5SManos Pitsidianakis } 13837fdb2f5SManos Pitsidianakis // SAFETY: 13937fdb2f5SManos Pitsidianakis // 14037fdb2f5SManos Pitsidianakis // self.clock is not initialized at this point; but since `NonNull<_>` is Copy, 14137fdb2f5SManos Pitsidianakis // we can overwrite the undefined value without side effects. This is 14237fdb2f5SManos Pitsidianakis // safe since all PL011State instances are created by QOM code which 14337fdb2f5SManos Pitsidianakis // calls this function to initialize the fields; therefore no code is 14437fdb2f5SManos Pitsidianakis // able to access an invalid self.clock value. 14537fdb2f5SManos Pitsidianakis unsafe { 14637fdb2f5SManos Pitsidianakis self.clock = NonNull::new(qdev_init_clock_in( 14737fdb2f5SManos Pitsidianakis dev, 14837fdb2f5SManos Pitsidianakis CLK_NAME.as_ptr(), 14937fdb2f5SManos Pitsidianakis None, /* pl011_clock_update */ 15037fdb2f5SManos Pitsidianakis addr_of_mut!(*self).cast::<c_void>(), 15137fdb2f5SManos Pitsidianakis ClockEvent::ClockUpdate.0, 15237fdb2f5SManos Pitsidianakis )) 15337fdb2f5SManos Pitsidianakis .unwrap(); 15437fdb2f5SManos Pitsidianakis } 15537fdb2f5SManos Pitsidianakis } 15637fdb2f5SManos Pitsidianakis 15737fdb2f5SManos Pitsidianakis pub fn read( 15837fdb2f5SManos Pitsidianakis &mut self, 15937fdb2f5SManos Pitsidianakis offset: hwaddr, 16037fdb2f5SManos Pitsidianakis _size: core::ffi::c_uint, 16137fdb2f5SManos Pitsidianakis ) -> std::ops::ControlFlow<u64, u64> { 16237fdb2f5SManos Pitsidianakis use RegisterOffset::*; 16337fdb2f5SManos Pitsidianakis 16437fdb2f5SManos Pitsidianakis std::ops::ControlFlow::Break(match RegisterOffset::try_from(offset) { 16537fdb2f5SManos Pitsidianakis Err(v) if (0x3f8..0x400).contains(&v) => { 16637fdb2f5SManos Pitsidianakis u64::from(PL011_ID_ARM[((offset - 0xfe0) >> 2) as usize]) 16737fdb2f5SManos Pitsidianakis } 16837fdb2f5SManos Pitsidianakis Err(_) => { 16937fdb2f5SManos Pitsidianakis // qemu_log_mask(LOG_GUEST_ERROR, "pl011_read: Bad offset 0x%x\n", (int)offset); 17037fdb2f5SManos Pitsidianakis 0 17137fdb2f5SManos Pitsidianakis } 17237fdb2f5SManos Pitsidianakis Ok(DR) => { 17337fdb2f5SManos Pitsidianakis // s->flags &= ~PL011_FLAG_RXFF; 17437fdb2f5SManos Pitsidianakis self.flags.set_receive_fifo_full(false); 17537fdb2f5SManos Pitsidianakis let c = self.read_fifo[self.read_pos]; 17637fdb2f5SManos Pitsidianakis if self.read_count > 0 { 17737fdb2f5SManos Pitsidianakis self.read_count -= 1; 17837fdb2f5SManos Pitsidianakis self.read_pos = (self.read_pos + 1) & (self.fifo_depth() - 1); 17937fdb2f5SManos Pitsidianakis } 18037fdb2f5SManos Pitsidianakis if self.read_count == 0 { 18137fdb2f5SManos Pitsidianakis // self.flags |= PL011_FLAG_RXFE; 18237fdb2f5SManos Pitsidianakis self.flags.set_receive_fifo_empty(true); 18337fdb2f5SManos Pitsidianakis } 18437fdb2f5SManos Pitsidianakis if self.read_count + 1 == self.read_trigger { 18537fdb2f5SManos Pitsidianakis //self.int_level &= ~ INT_RX; 18637fdb2f5SManos Pitsidianakis self.int_level &= !registers::INT_RX; 18737fdb2f5SManos Pitsidianakis } 18837fdb2f5SManos Pitsidianakis // Update error bits. 18937fdb2f5SManos Pitsidianakis self.receive_status_error_clear = c.to_be_bytes()[3].into(); 19037fdb2f5SManos Pitsidianakis self.update(); 19137fdb2f5SManos Pitsidianakis // Must call qemu_chr_fe_accept_input, so return Continue: 19237fdb2f5SManos Pitsidianakis return std::ops::ControlFlow::Continue(c.into()); 19337fdb2f5SManos Pitsidianakis } 19437fdb2f5SManos Pitsidianakis Ok(RSR) => u8::from(self.receive_status_error_clear).into(), 19537fdb2f5SManos Pitsidianakis Ok(FR) => u16::from(self.flags).into(), 19637fdb2f5SManos Pitsidianakis Ok(FBRD) => self.fbrd.into(), 19737fdb2f5SManos Pitsidianakis Ok(ILPR) => self.ilpr.into(), 19837fdb2f5SManos Pitsidianakis Ok(IBRD) => self.ibrd.into(), 19937fdb2f5SManos Pitsidianakis Ok(LCR_H) => u16::from(self.line_control).into(), 20037fdb2f5SManos Pitsidianakis Ok(CR) => { 20137fdb2f5SManos Pitsidianakis // We exercise our self-control. 20237fdb2f5SManos Pitsidianakis u16::from(self.control).into() 20337fdb2f5SManos Pitsidianakis } 20437fdb2f5SManos Pitsidianakis Ok(FLS) => self.ifl.into(), 20537fdb2f5SManos Pitsidianakis Ok(IMSC) => self.int_enabled.into(), 20637fdb2f5SManos Pitsidianakis Ok(RIS) => self.int_level.into(), 20737fdb2f5SManos Pitsidianakis Ok(MIS) => u64::from(self.int_level & self.int_enabled), 20837fdb2f5SManos Pitsidianakis Ok(ICR) => { 20937fdb2f5SManos Pitsidianakis // "The UARTICR Register is the interrupt clear register and is write-only" 21037fdb2f5SManos Pitsidianakis // Source: ARM DDI 0183G 3.3.13 Interrupt Clear Register, UARTICR 21137fdb2f5SManos Pitsidianakis 0 21237fdb2f5SManos Pitsidianakis } 21337fdb2f5SManos Pitsidianakis Ok(DMACR) => self.dmacr.into(), 21437fdb2f5SManos Pitsidianakis }) 21537fdb2f5SManos Pitsidianakis } 21637fdb2f5SManos Pitsidianakis 21737fdb2f5SManos Pitsidianakis pub fn write(&mut self, offset: hwaddr, value: u64) { 21837fdb2f5SManos Pitsidianakis // eprintln!("write offset {offset} value {value}"); 21937fdb2f5SManos Pitsidianakis use RegisterOffset::*; 22037fdb2f5SManos Pitsidianakis let value: u32 = value as u32; 22137fdb2f5SManos Pitsidianakis match RegisterOffset::try_from(offset) { 22237fdb2f5SManos Pitsidianakis Err(_bad_offset) => { 22337fdb2f5SManos Pitsidianakis eprintln!("write bad offset {offset} value {value}"); 22437fdb2f5SManos Pitsidianakis } 22537fdb2f5SManos Pitsidianakis Ok(DR) => { 22637fdb2f5SManos Pitsidianakis // ??? Check if transmitter is enabled. 22737fdb2f5SManos Pitsidianakis let ch: u8 = value as u8; 22837fdb2f5SManos Pitsidianakis // XXX this blocks entire thread. Rewrite to use 22937fdb2f5SManos Pitsidianakis // qemu_chr_fe_write and background I/O callbacks 23037fdb2f5SManos Pitsidianakis 23137fdb2f5SManos Pitsidianakis // SAFETY: self.char_backend is a valid CharBackend instance after it's been 23237fdb2f5SManos Pitsidianakis // initialized in realize(). 23337fdb2f5SManos Pitsidianakis unsafe { 23437fdb2f5SManos Pitsidianakis qemu_chr_fe_write_all(addr_of_mut!(self.char_backend), &ch, 1); 23537fdb2f5SManos Pitsidianakis } 23637fdb2f5SManos Pitsidianakis self.loopback_tx(value); 23737fdb2f5SManos Pitsidianakis self.int_level |= registers::INT_TX; 23837fdb2f5SManos Pitsidianakis self.update(); 23937fdb2f5SManos Pitsidianakis } 24037fdb2f5SManos Pitsidianakis Ok(RSR) => { 24137fdb2f5SManos Pitsidianakis self.receive_status_error_clear = 0.into(); 24237fdb2f5SManos Pitsidianakis } 24337fdb2f5SManos Pitsidianakis Ok(FR) => { 24437fdb2f5SManos Pitsidianakis // flag writes are ignored 24537fdb2f5SManos Pitsidianakis } 24637fdb2f5SManos Pitsidianakis Ok(ILPR) => { 24737fdb2f5SManos Pitsidianakis self.ilpr = value; 24837fdb2f5SManos Pitsidianakis } 24937fdb2f5SManos Pitsidianakis Ok(IBRD) => { 25037fdb2f5SManos Pitsidianakis self.ibrd = value; 25137fdb2f5SManos Pitsidianakis } 25237fdb2f5SManos Pitsidianakis Ok(FBRD) => { 25337fdb2f5SManos Pitsidianakis self.fbrd = value; 25437fdb2f5SManos Pitsidianakis } 25537fdb2f5SManos Pitsidianakis Ok(LCR_H) => { 25637fdb2f5SManos Pitsidianakis let value = value as u16; 25737fdb2f5SManos Pitsidianakis let new_val: registers::LineControl = value.into(); 25837fdb2f5SManos Pitsidianakis // Reset the FIFO state on FIFO enable or disable 25937fdb2f5SManos Pitsidianakis if bool::from(self.line_control.fifos_enabled()) 26037fdb2f5SManos Pitsidianakis ^ bool::from(new_val.fifos_enabled()) 26137fdb2f5SManos Pitsidianakis { 26237fdb2f5SManos Pitsidianakis self.reset_fifo(); 26337fdb2f5SManos Pitsidianakis } 26437fdb2f5SManos Pitsidianakis if self.line_control.send_break() ^ new_val.send_break() { 26537fdb2f5SManos Pitsidianakis let mut break_enable: c_int = new_val.send_break().into(); 26637fdb2f5SManos Pitsidianakis // SAFETY: self.char_backend is a valid CharBackend instance after it's been 26737fdb2f5SManos Pitsidianakis // initialized in realize(). 26837fdb2f5SManos Pitsidianakis unsafe { 26937fdb2f5SManos Pitsidianakis qemu_chr_fe_ioctl( 27037fdb2f5SManos Pitsidianakis addr_of_mut!(self.char_backend), 27137fdb2f5SManos Pitsidianakis CHR_IOCTL_SERIAL_SET_BREAK as i32, 27237fdb2f5SManos Pitsidianakis addr_of_mut!(break_enable).cast::<c_void>(), 27337fdb2f5SManos Pitsidianakis ); 27437fdb2f5SManos Pitsidianakis } 27537fdb2f5SManos Pitsidianakis self.loopback_break(break_enable > 0); 27637fdb2f5SManos Pitsidianakis } 27737fdb2f5SManos Pitsidianakis self.line_control = new_val; 27837fdb2f5SManos Pitsidianakis self.set_read_trigger(); 27937fdb2f5SManos Pitsidianakis } 28037fdb2f5SManos Pitsidianakis Ok(CR) => { 28137fdb2f5SManos Pitsidianakis // ??? Need to implement the enable bit. 28237fdb2f5SManos Pitsidianakis let value = value as u16; 28337fdb2f5SManos Pitsidianakis self.control = value.into(); 28437fdb2f5SManos Pitsidianakis self.loopback_mdmctrl(); 28537fdb2f5SManos Pitsidianakis } 28637fdb2f5SManos Pitsidianakis Ok(FLS) => { 28737fdb2f5SManos Pitsidianakis self.ifl = value; 28837fdb2f5SManos Pitsidianakis self.set_read_trigger(); 28937fdb2f5SManos Pitsidianakis } 29037fdb2f5SManos Pitsidianakis Ok(IMSC) => { 29137fdb2f5SManos Pitsidianakis self.int_enabled = value; 29237fdb2f5SManos Pitsidianakis self.update(); 29337fdb2f5SManos Pitsidianakis } 29437fdb2f5SManos Pitsidianakis Ok(RIS) => {} 29537fdb2f5SManos Pitsidianakis Ok(MIS) => {} 29637fdb2f5SManos Pitsidianakis Ok(ICR) => { 29737fdb2f5SManos Pitsidianakis self.int_level &= !value; 29837fdb2f5SManos Pitsidianakis self.update(); 29937fdb2f5SManos Pitsidianakis } 30037fdb2f5SManos Pitsidianakis Ok(DMACR) => { 30137fdb2f5SManos Pitsidianakis self.dmacr = value; 30237fdb2f5SManos Pitsidianakis if value & 3 > 0 { 30337fdb2f5SManos Pitsidianakis // qemu_log_mask(LOG_UNIMP, "pl011: DMA not implemented\n"); 30437fdb2f5SManos Pitsidianakis eprintln!("pl011: DMA not implemented"); 30537fdb2f5SManos Pitsidianakis } 30637fdb2f5SManos Pitsidianakis } 30737fdb2f5SManos Pitsidianakis } 30837fdb2f5SManos Pitsidianakis } 30937fdb2f5SManos Pitsidianakis 31037fdb2f5SManos Pitsidianakis #[inline] 31137fdb2f5SManos Pitsidianakis fn loopback_tx(&mut self, value: u32) { 31237fdb2f5SManos Pitsidianakis if !self.loopback_enabled() { 31337fdb2f5SManos Pitsidianakis return; 31437fdb2f5SManos Pitsidianakis } 31537fdb2f5SManos Pitsidianakis 31637fdb2f5SManos Pitsidianakis // Caveat: 31737fdb2f5SManos Pitsidianakis // 31837fdb2f5SManos Pitsidianakis // In real hardware, TX loopback happens at the serial-bit level 31937fdb2f5SManos Pitsidianakis // and then reassembled by the RX logics back into bytes and placed 32037fdb2f5SManos Pitsidianakis // into the RX fifo. That is, loopback happens after TX fifo. 32137fdb2f5SManos Pitsidianakis // 32237fdb2f5SManos Pitsidianakis // Because the real hardware TX fifo is time-drained at the frame 32337fdb2f5SManos Pitsidianakis // rate governed by the configured serial format, some loopback 32437fdb2f5SManos Pitsidianakis // bytes in TX fifo may still be able to get into the RX fifo 32537fdb2f5SManos Pitsidianakis // that could be full at times while being drained at software 32637fdb2f5SManos Pitsidianakis // pace. 32737fdb2f5SManos Pitsidianakis // 32837fdb2f5SManos Pitsidianakis // In such scenario, the RX draining pace is the major factor 32937fdb2f5SManos Pitsidianakis // deciding which loopback bytes get into the RX fifo, unless 33037fdb2f5SManos Pitsidianakis // hardware flow-control is enabled. 33137fdb2f5SManos Pitsidianakis // 33237fdb2f5SManos Pitsidianakis // For simplicity, the above described is not emulated. 33337fdb2f5SManos Pitsidianakis self.put_fifo(value); 33437fdb2f5SManos Pitsidianakis } 33537fdb2f5SManos Pitsidianakis 33637fdb2f5SManos Pitsidianakis fn loopback_mdmctrl(&mut self) { 33737fdb2f5SManos Pitsidianakis if !self.loopback_enabled() { 33837fdb2f5SManos Pitsidianakis return; 33937fdb2f5SManos Pitsidianakis } 34037fdb2f5SManos Pitsidianakis 34137fdb2f5SManos Pitsidianakis /* 34237fdb2f5SManos Pitsidianakis * Loopback software-driven modem control outputs to modem status inputs: 34337fdb2f5SManos Pitsidianakis * FR.RI <= CR.Out2 34437fdb2f5SManos Pitsidianakis * FR.DCD <= CR.Out1 34537fdb2f5SManos Pitsidianakis * FR.CTS <= CR.RTS 34637fdb2f5SManos Pitsidianakis * FR.DSR <= CR.DTR 34737fdb2f5SManos Pitsidianakis * 34837fdb2f5SManos Pitsidianakis * The loopback happens immediately even if this call is triggered 34937fdb2f5SManos Pitsidianakis * by setting only CR.LBE. 35037fdb2f5SManos Pitsidianakis * 35137fdb2f5SManos Pitsidianakis * CTS/RTS updates due to enabled hardware flow controls are not 35237fdb2f5SManos Pitsidianakis * dealt with here. 35337fdb2f5SManos Pitsidianakis */ 35437fdb2f5SManos Pitsidianakis 35537fdb2f5SManos Pitsidianakis //fr = s->flags & ~(PL011_FLAG_RI | PL011_FLAG_DCD | 35637fdb2f5SManos Pitsidianakis // PL011_FLAG_DSR | PL011_FLAG_CTS); 35737fdb2f5SManos Pitsidianakis //fr |= (cr & CR_OUT2) ? PL011_FLAG_RI : 0; 35837fdb2f5SManos Pitsidianakis //fr |= (cr & CR_OUT1) ? PL011_FLAG_DCD : 0; 35937fdb2f5SManos Pitsidianakis //fr |= (cr & CR_RTS) ? PL011_FLAG_CTS : 0; 36037fdb2f5SManos Pitsidianakis //fr |= (cr & CR_DTR) ? PL011_FLAG_DSR : 0; 36137fdb2f5SManos Pitsidianakis // 36237fdb2f5SManos Pitsidianakis self.flags.set_ring_indicator(self.control.out_2()); 36337fdb2f5SManos Pitsidianakis self.flags.set_data_carrier_detect(self.control.out_1()); 36437fdb2f5SManos Pitsidianakis self.flags.set_clear_to_send(self.control.request_to_send()); 36537fdb2f5SManos Pitsidianakis self.flags 36637fdb2f5SManos Pitsidianakis .set_data_set_ready(self.control.data_transmit_ready()); 36737fdb2f5SManos Pitsidianakis 36837fdb2f5SManos Pitsidianakis // Change interrupts based on updated FR 36937fdb2f5SManos Pitsidianakis let mut il = self.int_level; 37037fdb2f5SManos Pitsidianakis 37137fdb2f5SManos Pitsidianakis il &= !Interrupt::MS; 37237fdb2f5SManos Pitsidianakis //il |= (fr & PL011_FLAG_DSR) ? INT_DSR : 0; 37337fdb2f5SManos Pitsidianakis //il |= (fr & PL011_FLAG_DCD) ? INT_DCD : 0; 37437fdb2f5SManos Pitsidianakis //il |= (fr & PL011_FLAG_CTS) ? INT_CTS : 0; 37537fdb2f5SManos Pitsidianakis //il |= (fr & PL011_FLAG_RI) ? INT_RI : 0; 37637fdb2f5SManos Pitsidianakis 37737fdb2f5SManos Pitsidianakis if self.flags.data_set_ready() { 37837fdb2f5SManos Pitsidianakis il |= Interrupt::DSR as u32; 37937fdb2f5SManos Pitsidianakis } 38037fdb2f5SManos Pitsidianakis if self.flags.data_carrier_detect() { 38137fdb2f5SManos Pitsidianakis il |= Interrupt::DCD as u32; 38237fdb2f5SManos Pitsidianakis } 38337fdb2f5SManos Pitsidianakis if self.flags.clear_to_send() { 38437fdb2f5SManos Pitsidianakis il |= Interrupt::CTS as u32; 38537fdb2f5SManos Pitsidianakis } 38637fdb2f5SManos Pitsidianakis if self.flags.ring_indicator() { 38737fdb2f5SManos Pitsidianakis il |= Interrupt::RI as u32; 38837fdb2f5SManos Pitsidianakis } 38937fdb2f5SManos Pitsidianakis self.int_level = il; 39037fdb2f5SManos Pitsidianakis self.update(); 39137fdb2f5SManos Pitsidianakis } 39237fdb2f5SManos Pitsidianakis 39337fdb2f5SManos Pitsidianakis fn loopback_break(&mut self, enable: bool) { 39437fdb2f5SManos Pitsidianakis if enable { 39537fdb2f5SManos Pitsidianakis self.loopback_tx(DATA_BREAK); 39637fdb2f5SManos Pitsidianakis } 39737fdb2f5SManos Pitsidianakis } 39837fdb2f5SManos Pitsidianakis 39937fdb2f5SManos Pitsidianakis fn set_read_trigger(&mut self) { 40037fdb2f5SManos Pitsidianakis self.read_trigger = 1; 40137fdb2f5SManos Pitsidianakis } 40237fdb2f5SManos Pitsidianakis 40337fdb2f5SManos Pitsidianakis pub fn realize(&mut self) { 40437fdb2f5SManos Pitsidianakis // SAFETY: self.char_backend has the correct size and alignment for a 40537fdb2f5SManos Pitsidianakis // CharBackend object, and its callbacks are of the correct types. 40637fdb2f5SManos Pitsidianakis unsafe { 40737fdb2f5SManos Pitsidianakis qemu_chr_fe_set_handlers( 40837fdb2f5SManos Pitsidianakis addr_of_mut!(self.char_backend), 40937fdb2f5SManos Pitsidianakis Some(pl011_can_receive), 41037fdb2f5SManos Pitsidianakis Some(pl011_receive), 41137fdb2f5SManos Pitsidianakis Some(pl011_event), 41237fdb2f5SManos Pitsidianakis None, 41337fdb2f5SManos Pitsidianakis addr_of_mut!(*self).cast::<c_void>(), 41437fdb2f5SManos Pitsidianakis core::ptr::null_mut(), 41537fdb2f5SManos Pitsidianakis true, 41637fdb2f5SManos Pitsidianakis ); 41737fdb2f5SManos Pitsidianakis } 41837fdb2f5SManos Pitsidianakis } 41937fdb2f5SManos Pitsidianakis 42037fdb2f5SManos Pitsidianakis pub fn reset(&mut self) { 42137fdb2f5SManos Pitsidianakis self.line_control.reset(); 42237fdb2f5SManos Pitsidianakis self.receive_status_error_clear.reset(); 42337fdb2f5SManos Pitsidianakis self.dmacr = 0; 42437fdb2f5SManos Pitsidianakis self.int_enabled = 0; 42537fdb2f5SManos Pitsidianakis self.int_level = 0; 42637fdb2f5SManos Pitsidianakis self.ilpr = 0; 42737fdb2f5SManos Pitsidianakis self.ibrd = 0; 42837fdb2f5SManos Pitsidianakis self.fbrd = 0; 42937fdb2f5SManos Pitsidianakis self.read_trigger = 1; 43037fdb2f5SManos Pitsidianakis self.ifl = 0x12; 43137fdb2f5SManos Pitsidianakis self.control.reset(); 43237fdb2f5SManos Pitsidianakis self.flags = 0.into(); 43337fdb2f5SManos Pitsidianakis self.reset_fifo(); 43437fdb2f5SManos Pitsidianakis } 43537fdb2f5SManos Pitsidianakis 43637fdb2f5SManos Pitsidianakis pub fn reset_fifo(&mut self) { 43737fdb2f5SManos Pitsidianakis self.read_count = 0; 43837fdb2f5SManos Pitsidianakis self.read_pos = 0; 43937fdb2f5SManos Pitsidianakis 44037fdb2f5SManos Pitsidianakis /* Reset FIFO flags */ 44137fdb2f5SManos Pitsidianakis self.flags.reset(); 44237fdb2f5SManos Pitsidianakis } 44337fdb2f5SManos Pitsidianakis 44437fdb2f5SManos Pitsidianakis pub fn can_receive(&self) -> bool { 44537fdb2f5SManos Pitsidianakis // trace_pl011_can_receive(s->lcr, s->read_count, r); 44637fdb2f5SManos Pitsidianakis self.read_count < self.fifo_depth() 44737fdb2f5SManos Pitsidianakis } 44837fdb2f5SManos Pitsidianakis 44937fdb2f5SManos Pitsidianakis pub fn event(&mut self, event: QEMUChrEvent) { 45037fdb2f5SManos Pitsidianakis if event == bindings::QEMUChrEvent::CHR_EVENT_BREAK && !self.fifo_enabled() { 45137fdb2f5SManos Pitsidianakis self.put_fifo(DATA_BREAK); 45237fdb2f5SManos Pitsidianakis self.receive_status_error_clear.set_break_error(true); 45337fdb2f5SManos Pitsidianakis } 45437fdb2f5SManos Pitsidianakis } 45537fdb2f5SManos Pitsidianakis 45637fdb2f5SManos Pitsidianakis #[inline] 45737fdb2f5SManos Pitsidianakis pub fn fifo_enabled(&self) -> bool { 45837fdb2f5SManos Pitsidianakis matches!(self.line_control.fifos_enabled(), registers::Mode::FIFO) 45937fdb2f5SManos Pitsidianakis } 46037fdb2f5SManos Pitsidianakis 46137fdb2f5SManos Pitsidianakis #[inline] 46237fdb2f5SManos Pitsidianakis pub fn loopback_enabled(&self) -> bool { 46337fdb2f5SManos Pitsidianakis self.control.enable_loopback() 46437fdb2f5SManos Pitsidianakis } 46537fdb2f5SManos Pitsidianakis 46637fdb2f5SManos Pitsidianakis #[inline] 46737fdb2f5SManos Pitsidianakis pub fn fifo_depth(&self) -> usize { 46837fdb2f5SManos Pitsidianakis // Note: FIFO depth is expected to be power-of-2 46937fdb2f5SManos Pitsidianakis if self.fifo_enabled() { 47037fdb2f5SManos Pitsidianakis return PL011_FIFO_DEPTH; 47137fdb2f5SManos Pitsidianakis } 47237fdb2f5SManos Pitsidianakis 1 47337fdb2f5SManos Pitsidianakis } 47437fdb2f5SManos Pitsidianakis 47537fdb2f5SManos Pitsidianakis pub fn put_fifo(&mut self, value: c_uint) { 47637fdb2f5SManos Pitsidianakis let depth = self.fifo_depth(); 47737fdb2f5SManos Pitsidianakis assert!(depth > 0); 47837fdb2f5SManos Pitsidianakis let slot = (self.read_pos + self.read_count) & (depth - 1); 47937fdb2f5SManos Pitsidianakis self.read_fifo[slot] = value; 48037fdb2f5SManos Pitsidianakis self.read_count += 1; 48137fdb2f5SManos Pitsidianakis // s->flags &= ~PL011_FLAG_RXFE; 48237fdb2f5SManos Pitsidianakis self.flags.set_receive_fifo_empty(false); 48337fdb2f5SManos Pitsidianakis if self.read_count == depth { 48437fdb2f5SManos Pitsidianakis //s->flags |= PL011_FLAG_RXFF; 48537fdb2f5SManos Pitsidianakis self.flags.set_receive_fifo_full(true); 48637fdb2f5SManos Pitsidianakis } 48737fdb2f5SManos Pitsidianakis 48837fdb2f5SManos Pitsidianakis if self.read_count == self.read_trigger { 48937fdb2f5SManos Pitsidianakis self.int_level |= registers::INT_RX; 49037fdb2f5SManos Pitsidianakis self.update(); 49137fdb2f5SManos Pitsidianakis } 49237fdb2f5SManos Pitsidianakis } 49337fdb2f5SManos Pitsidianakis 49437fdb2f5SManos Pitsidianakis pub fn update(&self) { 49537fdb2f5SManos Pitsidianakis let flags = self.int_level & self.int_enabled; 49637fdb2f5SManos Pitsidianakis for (irq, i) in self.interrupts.iter().zip(IRQMASK) { 49737fdb2f5SManos Pitsidianakis // SAFETY: self.interrupts have been initialized in init(). 49837fdb2f5SManos Pitsidianakis unsafe { qemu_set_irq(*irq, i32::from(flags & i != 0)) }; 49937fdb2f5SManos Pitsidianakis } 50037fdb2f5SManos Pitsidianakis } 501*93243319SManos Pitsidianakis 502*93243319SManos Pitsidianakis pub fn post_load(&mut self, _version_id: u32) -> Result<(), ()> { 503*93243319SManos Pitsidianakis /* Sanity-check input state */ 504*93243319SManos Pitsidianakis if self.read_pos >= self.read_fifo.len() || self.read_count > self.read_fifo.len() { 505*93243319SManos Pitsidianakis return Err(()); 506*93243319SManos Pitsidianakis } 507*93243319SManos Pitsidianakis 508*93243319SManos Pitsidianakis if !self.fifo_enabled() && self.read_count > 0 && self.read_pos > 0 { 509*93243319SManos Pitsidianakis // Older versions of PL011 didn't ensure that the single 510*93243319SManos Pitsidianakis // character in the FIFO in FIFO-disabled mode is in 511*93243319SManos Pitsidianakis // element 0 of the array; convert to follow the current 512*93243319SManos Pitsidianakis // code's assumptions. 513*93243319SManos Pitsidianakis self.read_fifo[0] = self.read_fifo[self.read_pos]; 514*93243319SManos Pitsidianakis self.read_pos = 0; 515*93243319SManos Pitsidianakis } 516*93243319SManos Pitsidianakis 517*93243319SManos Pitsidianakis self.ibrd &= IBRD_MASK; 518*93243319SManos Pitsidianakis self.fbrd &= FBRD_MASK; 519*93243319SManos Pitsidianakis 520*93243319SManos Pitsidianakis Ok(()) 521*93243319SManos Pitsidianakis } 52237fdb2f5SManos Pitsidianakis } 52337fdb2f5SManos Pitsidianakis 52437fdb2f5SManos Pitsidianakis /// Which bits in the interrupt status matter for each outbound IRQ line ? 52537fdb2f5SManos Pitsidianakis pub const IRQMASK: [u32; 6] = [ 52637fdb2f5SManos Pitsidianakis /* combined IRQ */ 52737fdb2f5SManos Pitsidianakis Interrupt::E 52837fdb2f5SManos Pitsidianakis | Interrupt::MS 52937fdb2f5SManos Pitsidianakis | Interrupt::RT as u32 53037fdb2f5SManos Pitsidianakis | Interrupt::TX as u32 53137fdb2f5SManos Pitsidianakis | Interrupt::RX as u32, 53237fdb2f5SManos Pitsidianakis Interrupt::RX as u32, 53337fdb2f5SManos Pitsidianakis Interrupt::TX as u32, 53437fdb2f5SManos Pitsidianakis Interrupt::RT as u32, 53537fdb2f5SManos Pitsidianakis Interrupt::MS, 53637fdb2f5SManos Pitsidianakis Interrupt::E, 53737fdb2f5SManos Pitsidianakis ]; 53837fdb2f5SManos Pitsidianakis 53937fdb2f5SManos Pitsidianakis /// # Safety 54037fdb2f5SManos Pitsidianakis /// 54137fdb2f5SManos Pitsidianakis /// We expect the FFI user of this function to pass a valid pointer, that has 54237fdb2f5SManos Pitsidianakis /// the same size as [`PL011State`]. We also expect the device is 54337fdb2f5SManos Pitsidianakis /// readable/writeable from one thread at any time. 54437fdb2f5SManos Pitsidianakis pub unsafe extern "C" fn pl011_can_receive(opaque: *mut c_void) -> c_int { 54537fdb2f5SManos Pitsidianakis unsafe { 54637fdb2f5SManos Pitsidianakis debug_assert!(!opaque.is_null()); 54737fdb2f5SManos Pitsidianakis let state = NonNull::new_unchecked(opaque.cast::<PL011State>()); 54837fdb2f5SManos Pitsidianakis state.as_ref().can_receive().into() 54937fdb2f5SManos Pitsidianakis } 55037fdb2f5SManos Pitsidianakis } 55137fdb2f5SManos Pitsidianakis 55237fdb2f5SManos Pitsidianakis /// # Safety 55337fdb2f5SManos Pitsidianakis /// 55437fdb2f5SManos Pitsidianakis /// We expect the FFI user of this function to pass a valid pointer, that has 55537fdb2f5SManos Pitsidianakis /// the same size as [`PL011State`]. We also expect the device is 55637fdb2f5SManos Pitsidianakis /// readable/writeable from one thread at any time. 55737fdb2f5SManos Pitsidianakis /// 55837fdb2f5SManos Pitsidianakis /// The buffer and size arguments must also be valid. 55937fdb2f5SManos Pitsidianakis pub unsafe extern "C" fn pl011_receive( 56037fdb2f5SManos Pitsidianakis opaque: *mut core::ffi::c_void, 56137fdb2f5SManos Pitsidianakis buf: *const u8, 56237fdb2f5SManos Pitsidianakis size: core::ffi::c_int, 56337fdb2f5SManos Pitsidianakis ) { 56437fdb2f5SManos Pitsidianakis unsafe { 56537fdb2f5SManos Pitsidianakis debug_assert!(!opaque.is_null()); 56637fdb2f5SManos Pitsidianakis let mut state = NonNull::new_unchecked(opaque.cast::<PL011State>()); 56737fdb2f5SManos Pitsidianakis if state.as_ref().loopback_enabled() { 56837fdb2f5SManos Pitsidianakis return; 56937fdb2f5SManos Pitsidianakis } 57037fdb2f5SManos Pitsidianakis if size > 0 { 57137fdb2f5SManos Pitsidianakis debug_assert!(!buf.is_null()); 57237fdb2f5SManos Pitsidianakis state.as_mut().put_fifo(c_uint::from(buf.read_volatile())) 57337fdb2f5SManos Pitsidianakis } 57437fdb2f5SManos Pitsidianakis } 57537fdb2f5SManos Pitsidianakis } 57637fdb2f5SManos Pitsidianakis 57737fdb2f5SManos Pitsidianakis /// # Safety 57837fdb2f5SManos Pitsidianakis /// 57937fdb2f5SManos Pitsidianakis /// We expect the FFI user of this function to pass a valid pointer, that has 58037fdb2f5SManos Pitsidianakis /// the same size as [`PL011State`]. We also expect the device is 58137fdb2f5SManos Pitsidianakis /// readable/writeable from one thread at any time. 58237fdb2f5SManos Pitsidianakis pub unsafe extern "C" fn pl011_event(opaque: *mut core::ffi::c_void, event: QEMUChrEvent) { 58337fdb2f5SManos Pitsidianakis unsafe { 58437fdb2f5SManos Pitsidianakis debug_assert!(!opaque.is_null()); 58537fdb2f5SManos Pitsidianakis let mut state = NonNull::new_unchecked(opaque.cast::<PL011State>()); 58637fdb2f5SManos Pitsidianakis state.as_mut().event(event) 58737fdb2f5SManos Pitsidianakis } 58837fdb2f5SManos Pitsidianakis } 58937fdb2f5SManos Pitsidianakis 59037fdb2f5SManos Pitsidianakis /// # Safety 59137fdb2f5SManos Pitsidianakis /// 59237fdb2f5SManos Pitsidianakis /// We expect the FFI user of this function to pass a valid pointer for `chr`. 59337fdb2f5SManos Pitsidianakis #[no_mangle] 59437fdb2f5SManos Pitsidianakis pub unsafe extern "C" fn pl011_create( 59537fdb2f5SManos Pitsidianakis addr: u64, 59637fdb2f5SManos Pitsidianakis irq: qemu_irq, 59737fdb2f5SManos Pitsidianakis chr: *mut Chardev, 59837fdb2f5SManos Pitsidianakis ) -> *mut DeviceState { 59937fdb2f5SManos Pitsidianakis unsafe { 60037fdb2f5SManos Pitsidianakis let dev: *mut DeviceState = qdev_new(PL011State::TYPE_INFO.name); 60137fdb2f5SManos Pitsidianakis let sysbus: *mut SysBusDevice = dev.cast::<SysBusDevice>(); 60237fdb2f5SManos Pitsidianakis 603f60f3670SPaolo Bonzini qdev_prop_set_chr(dev, c"chardev".as_ptr(), chr); 60437fdb2f5SManos Pitsidianakis sysbus_realize_and_unref(sysbus, addr_of!(error_fatal) as *mut *mut Error); 60537fdb2f5SManos Pitsidianakis sysbus_mmio_map(sysbus, 0, addr); 60637fdb2f5SManos Pitsidianakis sysbus_connect_irq(sysbus, 0, irq); 60737fdb2f5SManos Pitsidianakis dev 60837fdb2f5SManos Pitsidianakis } 60937fdb2f5SManos Pitsidianakis } 61037fdb2f5SManos Pitsidianakis 61137fdb2f5SManos Pitsidianakis /// # Safety 61237fdb2f5SManos Pitsidianakis /// 61337fdb2f5SManos Pitsidianakis /// We expect the FFI user of this function to pass a valid pointer, that has 61437fdb2f5SManos Pitsidianakis /// the same size as [`PL011State`]. We also expect the device is 61537fdb2f5SManos Pitsidianakis /// readable/writeable from one thread at any time. 61637fdb2f5SManos Pitsidianakis pub unsafe extern "C" fn pl011_init(obj: *mut Object) { 61737fdb2f5SManos Pitsidianakis unsafe { 61837fdb2f5SManos Pitsidianakis debug_assert!(!obj.is_null()); 61937fdb2f5SManos Pitsidianakis let mut state = NonNull::new_unchecked(obj.cast::<PL011State>()); 62037fdb2f5SManos Pitsidianakis state.as_mut().init(); 62137fdb2f5SManos Pitsidianakis } 62237fdb2f5SManos Pitsidianakis } 623