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