Skip to main content

ostd/arch/x86/device/
serial.rs

1// SPDX-License-Identifier: MPL-2.0
2//! A port-mapped UART. Copied from uart_16550.
3#![expect(dead_code)]
4
5use vstd::prelude::*;
6use vstd_extra::external::{group_io_port_models, valid_io_port_access};
7
8use crate::{
9    arch::device::io_port::{ReadWriteAccess, WriteOnlyAccess},
10    io::IoPort,
11};
12/// A serial port.
13///
14/// Serial ports are a legacy communications port common on IBM-PC compatible computers.
15/// Ref: <https://wiki.osdev.org/Serial_Ports>
16#[verus_verify]
17pub struct SerialPort {
18    /// Data Register
19    data: IoPort<u8, ReadWriteAccess>,
20    /// Interrupt Enable Register
21    int_en: IoPort<u8, WriteOnlyAccess>,
22    /// First In First Out Control Register
23    fifo_ctrl: IoPort<u8, WriteOnlyAccess>,
24    /// Line control Register
25    line_ctrl: IoPort<u8, WriteOnlyAccess>,
26    /// Modem Control Register
27    modem_ctrl: IoPort<u8, WriteOnlyAccess>,
28    /// Line status Register
29    line_status: IoPort<u8, ReadWriteAccess>,
30    /// Modem Status Register
31    modem_status: IoPort<u8, ReadWriteAccess>,
32}
33
34verus! {
35
36broadcast use group_io_port_models;
37
38impl View for SerialPort {
39    type V = Seq<u16>;
40
41    /// Register addresses in offset order, from data through modem status.
42    closed spec fn view(&self) -> Seq<u16> {
43        seq![
44            self.data@,
45            self.int_en@,
46            self.fifo_ctrl@,
47            self.line_ctrl@,
48            self.modem_ctrl@,
49            self.line_status@,
50            self.modem_status@,
51        ]
52    }
53}
54
55} // verus!
56#[verus_verify]
57impl SerialPort {
58    /// Creates a serial port.
59    ///
60    /// # Safety
61    ///
62    /// The caller must ensure that the base port is a valid serial base port and that it has
63    /// exclusive ownership of the serial ports.
64    #[verus_spec(ret =>
65        requires
66            port <= u16::MAX - 6,
67        ensures
68            ret@ == Seq::new(7, |i: int| (port + i) as u16),
69            ret@.all(|addr: u16| valid_io_port_access::<u8>(addr)),
70    )]
71    pub const unsafe fn new(port: u16) -> Self {
72        // SAFETY: The safety is upheld by the caller.
73        unsafe {
74            Self {
75                data: IoPort::new(port),
76                int_en: IoPort::new(port + 1),
77                fifo_ctrl: IoPort::new(port + 2),
78                line_ctrl: IoPort::new(port + 3),
79                modem_ctrl: IoPort::new(port + 4),
80                line_status: IoPort::new(port + 5),
81                modem_status: IoPort::new(port + 6),
82            }
83        }
84    }
85
86    /// Initializes the serial port.
87    #[verus_spec]
88    pub fn init(&self) {
89        // Disable interrupts
90        self.int_en.write(0x00);
91        // Enable DLAB
92        self.line_ctrl.write(0x80);
93        // Set maximum speed to 38400 bps by configuring DLL and DLM
94        self.data.write(0x03);
95        self.int_en.write(0x00);
96        // Disable DLAB and set data word length to 8 bits
97        self.line_ctrl.write(0x03);
98        // Enable FIFO, clear TX/RX queues and
99        // set interrupt watermark at 14 bytes
100        self.fifo_ctrl.write(0xC7);
101        // Mark data terminal ready, signal request to send
102        // and enable auxiliary output #2 (used as interrupt line for CPU)
103        self.modem_ctrl.write(0x0B);
104        // Enable interrupts
105        self.int_en.write(0x01);
106    }
107
108    /// Sends data to the data port
109    #[inline]
110    #[verus_spec]
111    pub fn send(&self, data: u8) {
112        self.data.write(data);
113    }
114
115    /// Receives data from the data port
116    #[inline]
117    #[verus_spec]
118    pub fn recv(&self) -> u8 {
119        self.data.read()
120    }
121
122    /// Gets line status
123    #[inline]
124    #[verus_spec]
125    pub fn line_status(&self) -> u8 {
126        self.line_status.read()
127    }
128}