152da9a59SGnoCiYeH use core::{fmt::Debug, sync::atomic::AtomicU32}; 252da9a59SGnoCiYeH 352da9a59SGnoCiYeH use alloc::sync::Arc; 452da9a59SGnoCiYeH use system_error::SystemError; 552da9a59SGnoCiYeH 652da9a59SGnoCiYeH use crate::{driver::base::device::device_number::DeviceNumber, mm::VirtAddr}; 752da9a59SGnoCiYeH 852da9a59SGnoCiYeH use self::serial8250::serial8250_manager; 952da9a59SGnoCiYeH 1052da9a59SGnoCiYeH pub mod serial8250; 1152da9a59SGnoCiYeH 12*bd70d2d1SLoGin #[allow(dead_code)] 1352da9a59SGnoCiYeH pub trait UartDriver: Debug + Send + Sync { 1452da9a59SGnoCiYeH fn device_number(&self) -> DeviceNumber; 1552da9a59SGnoCiYeH 1652da9a59SGnoCiYeH /// 获取最大的设备数量 1752da9a59SGnoCiYeH fn max_devs_num(&self) -> i32; 1852da9a59SGnoCiYeH 1952da9a59SGnoCiYeH // todo: 获取指向console的指针(在我们系统里面,将来可能是改进后的Textui Window) 2052da9a59SGnoCiYeH } 2152da9a59SGnoCiYeH 2252da9a59SGnoCiYeH /// 串口端口应当实现的trait 2352da9a59SGnoCiYeH /// 2452da9a59SGnoCiYeH /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/serial_core.h#428 25*bd70d2d1SLoGin #[allow(dead_code)] 2652da9a59SGnoCiYeH pub trait UartPort { 2752da9a59SGnoCiYeH fn iobase(&self) -> Option<usize> { 2852da9a59SGnoCiYeH None 2952da9a59SGnoCiYeH } 3052da9a59SGnoCiYeH fn membase(&self) -> Option<VirtAddr> { 3152da9a59SGnoCiYeH None 3252da9a59SGnoCiYeH } 3352da9a59SGnoCiYeH fn serial_in(&self, offset: u32) -> u32; 3452da9a59SGnoCiYeH fn serial_out(&self, offset: u32, value: u32); 3552da9a59SGnoCiYeH fn divisor(&self, baud: BaudRate) -> (u32, DivisorFraction); 3652da9a59SGnoCiYeH fn set_divisor(&self, baud: BaudRate) -> Result<(), SystemError>; 3752da9a59SGnoCiYeH fn baud_rate(&self) -> Option<BaudRate>; 3852da9a59SGnoCiYeH fn startup(&self) -> Result<(), SystemError>; 3952da9a59SGnoCiYeH fn shutdown(&self); 4052da9a59SGnoCiYeH fn handle_irq(&self) -> Result<(), SystemError>; 4152da9a59SGnoCiYeH } 4252da9a59SGnoCiYeH 4352da9a59SGnoCiYeH int_like!(BaudRate, AtomicBaudRate, u32, AtomicU32); 4452da9a59SGnoCiYeH int_like!(DivisorFraction, u32); 4552da9a59SGnoCiYeH 4652da9a59SGnoCiYeH #[inline(always)] 4752da9a59SGnoCiYeH #[allow(dead_code)] 4852da9a59SGnoCiYeH pub(super) fn uart_manager() -> &'static UartManager { 4952da9a59SGnoCiYeH &UartManager 5052da9a59SGnoCiYeH } 5152da9a59SGnoCiYeH 5252da9a59SGnoCiYeH #[derive(Debug)] 5352da9a59SGnoCiYeH pub(super) struct UartManager; 5452da9a59SGnoCiYeH 5552da9a59SGnoCiYeH impl UartManager { 5652da9a59SGnoCiYeH /// todo: 把uart设备注册到tty层 5752da9a59SGnoCiYeH /// 5852da9a59SGnoCiYeH /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/tty/serial/serial_core.c?fi=uart_register_driver#2720 5952da9a59SGnoCiYeH #[allow(dead_code)] 6052da9a59SGnoCiYeH pub fn register_driver(&self, _driver: &Arc<dyn UartDriver>) -> Result<(), SystemError> { 6152da9a59SGnoCiYeH return Ok(()); 6252da9a59SGnoCiYeH } 6352da9a59SGnoCiYeH } 6452da9a59SGnoCiYeH 6552da9a59SGnoCiYeH pub fn serial_early_init() -> Result<(), SystemError> { 6652da9a59SGnoCiYeH serial8250_manager().early_init()?; 6752da9a59SGnoCiYeH return Ok(()); 6852da9a59SGnoCiYeH } 6952da9a59SGnoCiYeH 7052da9a59SGnoCiYeH pub(super) fn serial_init() -> Result<(), SystemError> { 7152da9a59SGnoCiYeH serial8250_manager().init()?; 7252da9a59SGnoCiYeH return Ok(()); 7352da9a59SGnoCiYeH } 74