xref: /DragonOS/kernel/src/arch/x86_64/driver/rtc.rs (revision 28fe4ad2a0b0d8b5abf1f0cb402b1c3204b42242)
1da152319SLoGin use core::any::Any;
2da152319SLoGin 
3da152319SLoGin use alloc::{
4da152319SLoGin     string::{String, ToString},
5da152319SLoGin     sync::{Arc, Weak},
6da152319SLoGin };
72eab6dd7S曾俊 use log::error;
8da152319SLoGin use system_error::SystemError;
9da152319SLoGin use unified_init::macros::unified_init;
10da152319SLoGin 
11da152319SLoGin use crate::{
12da152319SLoGin     arch::{io::PortIOArch, CurrentIrqArch, CurrentPortIOArch},
13da152319SLoGin     driver::{
14da152319SLoGin         base::{
15da152319SLoGin             class::Class,
16da152319SLoGin             device::{
17da152319SLoGin                 bus::Bus, device_manager, driver::Driver, Device, DeviceCommonData, DeviceState,
18da152319SLoGin                 DeviceType, IdTable,
19da152319SLoGin             },
20da152319SLoGin             kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState},
21da152319SLoGin             kset::KSet,
22da152319SLoGin             platform::platform_device::{platform_device_manager, PlatformDevice},
23da152319SLoGin         },
24da152319SLoGin         rtc::{RtcClassOps, RtcDevice, RtcTime},
25da152319SLoGin     },
26da152319SLoGin     exception::InterruptArch,
27da152319SLoGin     filesystem::kernfs::KernFSInode,
28da152319SLoGin     init::initcall::INITCALL_DEVICE,
29da152319SLoGin     libs::{
30da152319SLoGin         mutex::Mutex,
31da152319SLoGin         rwlock::{RwLockReadGuard, RwLockWriteGuard},
32da152319SLoGin         spinlock::{SpinLock, SpinLockGuard},
33da152319SLoGin     },
34da152319SLoGin };
35da152319SLoGin 
36da152319SLoGin #[derive(Debug)]
37da152319SLoGin #[cast_to([sync] Device, PlatformDevice, RtcDevice)]
38da152319SLoGin struct CmosRtcDevice {
39da152319SLoGin     inner: SpinLock<InnerCmosRtc>,
40da152319SLoGin     locked_kobjstate: LockedKObjectState,
41da152319SLoGin     ops_mutex: Mutex<()>,
42da152319SLoGin }
43da152319SLoGin 
44da152319SLoGin impl CmosRtcDevice {
45da152319SLoGin     const NAME: &str = "rtc_cmos";
new() -> Arc<Self>46da152319SLoGin     pub fn new() -> Arc<Self> {
47da152319SLoGin         let r = CmosRtcDevice {
48da152319SLoGin             inner: SpinLock::new(InnerCmosRtc {
49da152319SLoGin                 device_common: DeviceCommonData::default(),
50da152319SLoGin                 kobject_common: KObjectCommonData::default(),
51da152319SLoGin                 device_state: DeviceState::NotInitialized,
52da152319SLoGin             }),
53da152319SLoGin             locked_kobjstate: LockedKObjectState::new(None),
54da152319SLoGin             ops_mutex: Mutex::new(()),
55da152319SLoGin         };
56da152319SLoGin 
57da152319SLoGin         r.inner().device_common.can_match = true;
58da152319SLoGin 
59da152319SLoGin         Arc::new(r)
60da152319SLoGin     }
61da152319SLoGin 
inner(&self) -> SpinLockGuard<InnerCmosRtc>62da152319SLoGin     fn inner(&self) -> SpinLockGuard<InnerCmosRtc> {
63da152319SLoGin         self.inner.lock()
64da152319SLoGin     }
65da152319SLoGin 
66da152319SLoGin     ///置位0x70的第7位,禁止不可屏蔽中断
67da152319SLoGin     #[inline]
read_cmos(&self, addr: u8) -> u868da152319SLoGin     fn read_cmos(&self, addr: u8) -> u8 {
69da152319SLoGin         unsafe {
70da152319SLoGin             CurrentPortIOArch::out8(0x70, 0x80 | addr);
71da152319SLoGin             return CurrentPortIOArch::in8(0x71);
72da152319SLoGin         }
73da152319SLoGin     }
74da152319SLoGin }
75da152319SLoGin 
76da152319SLoGin #[derive(Debug)]
77da152319SLoGin struct InnerCmosRtc {
78da152319SLoGin     device_common: DeviceCommonData,
79da152319SLoGin     kobject_common: KObjectCommonData,
80da152319SLoGin 
81da152319SLoGin     device_state: DeviceState,
82da152319SLoGin }
83da152319SLoGin 
84da152319SLoGin impl RtcDevice for CmosRtcDevice {
class_ops(&self) -> &'static dyn RtcClassOps85da152319SLoGin     fn class_ops(&self) -> &'static dyn RtcClassOps {
86da152319SLoGin         &CmosRtcClassOps
87da152319SLoGin     }
88da152319SLoGin }
89da152319SLoGin 
90da152319SLoGin impl PlatformDevice for CmosRtcDevice {
pdev_name(&self) -> &str91da152319SLoGin     fn pdev_name(&self) -> &str {
92da152319SLoGin         Self::NAME
93da152319SLoGin     }
94da152319SLoGin 
set_pdev_id(&self, _id: i32)95da152319SLoGin     fn set_pdev_id(&self, _id: i32) {
96da152319SLoGin         todo!()
97da152319SLoGin     }
98da152319SLoGin 
set_pdev_id_auto(&self, _id_auto: bool)99da152319SLoGin     fn set_pdev_id_auto(&self, _id_auto: bool) {
100da152319SLoGin         todo!()
101da152319SLoGin     }
102da152319SLoGin 
is_initialized(&self) -> bool103da152319SLoGin     fn is_initialized(&self) -> bool {
104da152319SLoGin         self.inner().device_state == DeviceState::Initialized
105da152319SLoGin     }
106da152319SLoGin 
set_state(&self, set_state: DeviceState)107da152319SLoGin     fn set_state(&self, set_state: DeviceState) {
108da152319SLoGin         self.inner().device_state = set_state;
109da152319SLoGin     }
110da152319SLoGin }
111da152319SLoGin 
112da152319SLoGin impl Device for CmosRtcDevice {
dev_type(&self) -> DeviceType113da152319SLoGin     fn dev_type(&self) -> DeviceType {
114da152319SLoGin         DeviceType::Rtc
115da152319SLoGin     }
116da152319SLoGin 
id_table(&self) -> IdTable117da152319SLoGin     fn id_table(&self) -> IdTable {
118da152319SLoGin         IdTable::new(Self::NAME.to_string(), None)
119da152319SLoGin     }
120da152319SLoGin 
set_bus(&self, bus: Option<Weak<dyn Bus>>)121da152319SLoGin     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
122da152319SLoGin         self.inner().device_common.bus = bus;
123da152319SLoGin     }
124da152319SLoGin 
set_class(&self, class: Option<Weak<dyn Class>>)125da152319SLoGin     fn set_class(&self, class: Option<Weak<dyn Class>>) {
126da152319SLoGin         self.inner().device_common.class = class;
127da152319SLoGin     }
128da152319SLoGin 
class(&self) -> Option<Arc<dyn Class>>129da152319SLoGin     fn class(&self) -> Option<Arc<dyn Class>> {
130da152319SLoGin         self.inner()
131da152319SLoGin             .device_common
132da152319SLoGin             .get_class_weak_or_clear()
133da152319SLoGin             .and_then(|c| c.upgrade())
134da152319SLoGin     }
135da152319SLoGin 
driver(&self) -> Option<Arc<dyn Driver>>136da152319SLoGin     fn driver(&self) -> Option<Arc<dyn Driver>> {
137da152319SLoGin         self.inner()
138da152319SLoGin             .device_common
139da152319SLoGin             .get_driver_weak_or_clear()
140da152319SLoGin             .and_then(|d| d.upgrade())
141da152319SLoGin     }
142da152319SLoGin 
set_driver(&self, driver: Option<Weak<dyn Driver>>)143da152319SLoGin     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
144da152319SLoGin         self.inner().device_common.driver = driver;
145da152319SLoGin     }
146da152319SLoGin 
is_dead(&self) -> bool147da152319SLoGin     fn is_dead(&self) -> bool {
148da152319SLoGin         self.inner().device_common.dead
149da152319SLoGin     }
150da152319SLoGin 
can_match(&self) -> bool151da152319SLoGin     fn can_match(&self) -> bool {
152da152319SLoGin         self.inner().device_common.can_match
153da152319SLoGin     }
154da152319SLoGin 
set_can_match(&self, can_match: bool)155da152319SLoGin     fn set_can_match(&self, can_match: bool) {
156da152319SLoGin         self.inner().device_common.can_match = can_match;
157da152319SLoGin     }
158da152319SLoGin 
state_synced(&self) -> bool159da152319SLoGin     fn state_synced(&self) -> bool {
160da152319SLoGin         true
161da152319SLoGin     }
162da152319SLoGin 
bus(&self) -> Option<Weak<dyn Bus>>163da152319SLoGin     fn bus(&self) -> Option<Weak<dyn Bus>> {
164da152319SLoGin         self.inner().device_common.get_bus_weak_or_clear()
165da152319SLoGin     }
166*28fe4ad2S黄铭涛 
dev_parent(&self) -> Option<Weak<dyn Device>>167*28fe4ad2S黄铭涛     fn dev_parent(&self) -> Option<Weak<dyn Device>> {
168*28fe4ad2S黄铭涛         self.inner().device_common.get_parent_weak_or_clear()
169*28fe4ad2S黄铭涛     }
170*28fe4ad2S黄铭涛 
set_dev_parent(&self, parent: Option<Weak<dyn Device>>)171*28fe4ad2S黄铭涛     fn set_dev_parent(&self, parent: Option<Weak<dyn Device>>) {
172*28fe4ad2S黄铭涛         self.inner().device_common.parent = parent;
173*28fe4ad2S黄铭涛     }
174da152319SLoGin }
175da152319SLoGin 
176da152319SLoGin impl KObject for CmosRtcDevice {
as_any_ref(&self) -> &dyn Any177da152319SLoGin     fn as_any_ref(&self) -> &dyn Any {
178da152319SLoGin         self
179da152319SLoGin     }
180da152319SLoGin 
set_inode(&self, inode: Option<Arc<KernFSInode>>)181da152319SLoGin     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
182da152319SLoGin         self.inner().kobject_common.kern_inode = inode;
183da152319SLoGin     }
184da152319SLoGin 
inode(&self) -> Option<Arc<KernFSInode>>185da152319SLoGin     fn inode(&self) -> Option<Arc<KernFSInode>> {
186da152319SLoGin         self.inner().kobject_common.kern_inode.clone()
187da152319SLoGin     }
188da152319SLoGin 
parent(&self) -> Option<Weak<dyn KObject>>189da152319SLoGin     fn parent(&self) -> Option<Weak<dyn KObject>> {
190da152319SLoGin         self.inner().kobject_common.get_parent_or_clear_weak()
191da152319SLoGin     }
192da152319SLoGin 
set_parent(&self, parent: Option<Weak<dyn KObject>>)193da152319SLoGin     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
194da152319SLoGin         self.inner().kobject_common.parent = parent;
195da152319SLoGin     }
196da152319SLoGin 
kset(&self) -> Option<Arc<KSet>>197da152319SLoGin     fn kset(&self) -> Option<Arc<KSet>> {
198da152319SLoGin         self.inner().kobject_common.kset.clone()
199da152319SLoGin     }
200da152319SLoGin 
set_kset(&self, kset: Option<Arc<KSet>>)201da152319SLoGin     fn set_kset(&self, kset: Option<Arc<KSet>>) {
202da152319SLoGin         self.inner().kobject_common.kset = kset;
203da152319SLoGin     }
204da152319SLoGin 
kobj_type(&self) -> Option<&'static dyn KObjType>205da152319SLoGin     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
206da152319SLoGin         self.inner().kobject_common.kobj_type
207da152319SLoGin     }
208da152319SLoGin 
set_kobj_type(&self, ktype: Option<&'static dyn KObjType>)209da152319SLoGin     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
210da152319SLoGin         self.inner().kobject_common.kobj_type = ktype;
211da152319SLoGin     }
212da152319SLoGin 
name(&self) -> String213da152319SLoGin     fn name(&self) -> String {
214da152319SLoGin         Self::NAME.to_string()
215da152319SLoGin     }
216da152319SLoGin 
set_name(&self, _name: String)217da152319SLoGin     fn set_name(&self, _name: String) {
218da152319SLoGin         // Do nothing
219da152319SLoGin     }
220da152319SLoGin 
kobj_state(&self) -> RwLockReadGuard<KObjectState>221da152319SLoGin     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
222da152319SLoGin         self.locked_kobjstate.read()
223da152319SLoGin     }
224da152319SLoGin 
kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>225da152319SLoGin     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
226da152319SLoGin         self.locked_kobjstate.write()
227da152319SLoGin     }
228da152319SLoGin 
set_kobj_state(&self, state: KObjectState)229da152319SLoGin     fn set_kobj_state(&self, state: KObjectState) {
230da152319SLoGin         *self.locked_kobjstate.write() = state;
231da152319SLoGin     }
232da152319SLoGin }
233da152319SLoGin 
234da152319SLoGin #[derive(Debug)]
235da152319SLoGin pub struct CmosRtcClassOps;
236da152319SLoGin 
237da152319SLoGin impl RtcClassOps for CmosRtcClassOps {
read_time(&self, dev: &Arc<dyn RtcDevice>) -> Result<RtcTime, SystemError>238da152319SLoGin     fn read_time(&self, dev: &Arc<dyn RtcDevice>) -> Result<RtcTime, SystemError> {
239da152319SLoGin         // 检查是否为cmos rtc
240da152319SLoGin         let dev = dev
241da152319SLoGin             .as_any_ref()
242da152319SLoGin             .downcast_ref::<CmosRtcDevice>()
243da152319SLoGin             .ok_or(SystemError::EINVAL)?;
244da152319SLoGin 
245da152319SLoGin         let _guard = dev.ops_mutex.lock();
246da152319SLoGin 
247da152319SLoGin         // 为防止中断请求打断该过程,需要先关中断
248da152319SLoGin         let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
249da152319SLoGin         //0x0B
250da152319SLoGin         let status_register_b: u8 = dev.read_cmos(0x0B); // 读取状态寄存器B
251da152319SLoGin         let is_24h: bool = (status_register_b & 0x02) != 0;
252da152319SLoGin         // 判断是否启用24小时模式
253da152319SLoGin         let is_binary: bool = (status_register_b & 0x04) != 0; // 判断是否为二进制码
254da152319SLoGin         let mut res = RtcTime::default();
255da152319SLoGin 
256da152319SLoGin         loop {
257da152319SLoGin             res.year = dev.read_cmos(CMOSTimeSelector::Year as u8) as i32;
258da152319SLoGin             res.month = dev.read_cmos(CMOSTimeSelector::Month as u8) as i32;
259da152319SLoGin             res.mday = dev.read_cmos(CMOSTimeSelector::Day as u8) as i32;
260da152319SLoGin             res.hour = dev.read_cmos(CMOSTimeSelector::Hour as u8) as i32;
261da152319SLoGin             res.minute = dev.read_cmos(CMOSTimeSelector::Minute as u8) as i32;
262da152319SLoGin             res.second = dev.read_cmos(CMOSTimeSelector::Second as u8) as i32;
263da152319SLoGin 
264da152319SLoGin             if res.second == dev.read_cmos(CMOSTimeSelector::Second as u8) as i32 {
265da152319SLoGin                 break;
266da152319SLoGin             } // 若读取时间过程中时间发生跳变则重新读取
267da152319SLoGin         }
268da152319SLoGin 
269da152319SLoGin         unsafe {
270da152319SLoGin             CurrentPortIOArch::out8(0x70, 0x00);
271da152319SLoGin         }
272da152319SLoGin 
273da152319SLoGin         if !is_binary
274da152319SLoGin         // 把BCD转为二进制
275da152319SLoGin         {
276da152319SLoGin             res.second = (res.second & 0xf) + (res.second >> 4) * 10;
277da152319SLoGin             res.minute = (res.minute & 0xf) + (res.minute >> 4) * 10;
278da152319SLoGin             res.hour = ((res.hour & 0xf) + ((res.hour & 0x70) >> 4) * 10) | (res.hour & 0x80);
279da152319SLoGin             res.mday = (res.mday & 0xf) + ((res.mday / 16) * 10);
280da152319SLoGin             res.month = (res.month & 0xf) + (res.month >> 4) * 10;
281da152319SLoGin             res.year = (res.year & 0xf) + (res.year >> 4) * 10;
282da152319SLoGin         }
283da152319SLoGin         res.year += 100;
284da152319SLoGin 
285da152319SLoGin         if (!is_24h) && (res.hour & 0x80) != 0 {
286da152319SLoGin             res.hour = ((res.hour & 0x7f) + 12) % 24;
287da152319SLoGin         } // 将十二小时制转为24小时
288da152319SLoGin 
289da152319SLoGin         res.month -= 1;
290da152319SLoGin 
291da152319SLoGin         drop(irq_guard);
292da152319SLoGin 
293da152319SLoGin         return Ok(res);
294da152319SLoGin     }
295da152319SLoGin 
set_time(&self, _dev: &Arc<dyn RtcDevice>, _time: &RtcTime) -> Result<(), SystemError>296da152319SLoGin     fn set_time(&self, _dev: &Arc<dyn RtcDevice>, _time: &RtcTime) -> Result<(), SystemError> {
2972eab6dd7S曾俊         error!("set_time is not implemented for CmosRtcClassOps");
298da152319SLoGin         Err(SystemError::ENOSYS)
299da152319SLoGin     }
300da152319SLoGin }
301da152319SLoGin 
302da152319SLoGin /// used in the form of u8
303da152319SLoGin #[repr(u8)]
304da152319SLoGin enum CMOSTimeSelector {
305da152319SLoGin     Second = 0x00,
306da152319SLoGin     Minute = 0x02,
307da152319SLoGin     Hour = 0x04,
308da152319SLoGin     Day = 0x07,
309da152319SLoGin     Month = 0x08,
310da152319SLoGin     Year = 0x09,
311da152319SLoGin }
312da152319SLoGin 
313da152319SLoGin #[unified_init(INITCALL_DEVICE)]
cmos_rtc_device_init() -> Result<(), SystemError>314da152319SLoGin pub fn cmos_rtc_device_init() -> Result<(), SystemError> {
315da152319SLoGin     let device = CmosRtcDevice::new();
316da152319SLoGin     device_manager().device_default_initialize(&(device.clone() as Arc<dyn Device>));
317da152319SLoGin     platform_device_manager().device_add(device)?;
318da152319SLoGin 
319da152319SLoGin     return Ok(());
320da152319SLoGin }
321