blob: b23e7d1a03d9f6b9ad4b9f418680dc0cd6121ae3 [file] [log] [blame]
Imre Kiscd007ad2024-08-14 15:53:11 +02001// SPDX-FileCopyrightText: Copyright 2023-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Arm Watchdog Module (SP805) driver
5//!
6//! Driver implementation for the [SP805 watchdog module](https://developer.arm.com/documentation/ddi0270/latest/).
7
8#![no_std]
9
Imre Kise05daa42024-09-26 11:20:06 +020010use core::{
11 ops::{Deref, DerefMut},
12 ptr::addr_of_mut,
13};
Imre Kiscd007ad2024-08-14 15:53:11 +020014
15use bitflags::bitflags;
Imre Kiscd007ad2024-08-14 15:53:11 +020016
17bitflags! {
18 /// Control register
19 #[repr(transparent)]
20 #[derive(Copy, Clone)]
21 struct ControlRegister : u32 {
22 /// Enable Watchdog module reset output
23 const RESEN = 1 << 1;
24 /// Break error
25 const INTEN = 1 << 0;
26 }
27
28 /// Raw Interrupt Status Register
29 #[repr(transparent)]
30 #[derive(Copy, Clone)]
31 struct RawInterruptStatusRegister : u32 {
32 /// Raw interrupt status from the counter
33 const WDOGRIS = 1 << 0;
34 }
35
36 /// Masked Interrupt Status Register
37 struct MaskedInterruptStatusRegister : u32 {
38 /// Enabled interrupt status from the counter
39 const WDOGMIS = 1 << 0;
40 }
41}
42
43/// SP805 register map
44#[repr(C, align(4))]
45pub struct SP805Registers {
Imre Kise05daa42024-09-26 11:20:06 +020046 wdog_load: u32, // 0x000 Load Register
47 wdog_value: u32, // 0x004 Value Register
48 wdog_control: u32, // 0x008 Control register
49 wdog_intclr: u32, // 0x00c Interrupt Clear Register
50 wdog_ris: u32, // 0x010 Raw Interrupt Status Register
51 wdog_mis: u32, // 0x014 Masked Interrupt Status Register
Imre Kiscd007ad2024-08-14 15:53:11 +020052 reserved_18: [u32; 762], // 0x018 - 0xbfc
Imre Kise05daa42024-09-26 11:20:06 +020053 wdog_lock: u32, // 0xc00 Lock Register
Imre Kiscd007ad2024-08-14 15:53:11 +020054 reserved_c04: [u32; 191], // 0xc04 - 0xefc
Imre Kise05daa42024-09-26 11:20:06 +020055 wdog_itcr: u32, // 0xf00 Integration Test Control Register,
56 wdog_itop: u32, // 0xf04 Integration Test Output Set
Imre Kiscd007ad2024-08-14 15:53:11 +020057 reserved_f08: [u32; 54], // 0xf08 - 0xfdc
Imre Kise05daa42024-09-26 11:20:06 +020058 wdog_periph_id0: u32, // 0xfe0 Peripheral Identification Register 0
59 wdog_periph_id1: u32, // 0xfe4 Peripheral Identification Register 1
60 wdog_periph_id2: u32, // 0xfe8 Peripheral Identification Register 2
61 wdog_periph_id3: u32, // 0xfec Peripheral Identification Register 3
62 wdog_pcell_id0: u32, // 0xff0 PrimeCell Identification Register 0
63 wdog_pcell_id1: u32, // 0xff4 PrimeCell Identification Register 1
64 wdog_pcell_id2: u32, // 0xff8 PrimeCell Identification Register 2
65 wdog_pcell_id3: u32, // 0xffc PrimeCell Identification Register 3
Imre Kiscd007ad2024-08-14 15:53:11 +020066}
67
68struct WatchdogUnlockGuard<'a, R>
69where
Imre Kise05daa42024-09-26 11:20:06 +020070 R: DerefMut<Target = SP805Registers>,
Imre Kiscd007ad2024-08-14 15:53:11 +020071{
Imre Kise05daa42024-09-26 11:20:06 +020072 regs: &'a mut R,
Imre Kiscd007ad2024-08-14 15:53:11 +020073}
74
75impl<'a, R> WatchdogUnlockGuard<'a, R>
76where
Imre Kise05daa42024-09-26 11:20:06 +020077 R: DerefMut<Target = SP805Registers>,
Imre Kiscd007ad2024-08-14 15:53:11 +020078{
79 const LOCK: u32 = 0x00000001;
80 const UNLOCK: u32 = 0x1ACCE551;
81
Imre Kise05daa42024-09-26 11:20:06 +020082 pub fn new(regs: &'a mut R) -> Self {
Imre Kiscd007ad2024-08-14 15:53:11 +020083 unsafe {
Imre Kise05daa42024-09-26 11:20:06 +020084 addr_of_mut!(regs.wdog_lock).write_volatile(Self::UNLOCK);
Imre Kiscd007ad2024-08-14 15:53:11 +020085 }
86 Self { regs }
87 }
88}
89
90impl<'a, R> Deref for WatchdogUnlockGuard<'a, R>
91where
Imre Kise05daa42024-09-26 11:20:06 +020092 R: DerefMut<Target = SP805Registers>,
Imre Kiscd007ad2024-08-14 15:53:11 +020093{
94 type Target = R;
95
96 fn deref(&self) -> &Self::Target {
97 self.regs
98 }
99}
100
Imre Kise05daa42024-09-26 11:20:06 +0200101impl<'a, R> DerefMut for WatchdogUnlockGuard<'a, R>
102where
103 R: DerefMut<Target = SP805Registers>,
104{
105 fn deref_mut(&mut self) -> &mut Self::Target {
106 self.regs
107 }
108}
109
Imre Kiscd007ad2024-08-14 15:53:11 +0200110impl<'a, R> Drop for WatchdogUnlockGuard<'a, R>
111where
Imre Kise05daa42024-09-26 11:20:06 +0200112 R: DerefMut<Target = SP805Registers>,
Imre Kiscd007ad2024-08-14 15:53:11 +0200113{
114 fn drop(&mut self) {
115 unsafe {
Imre Kise05daa42024-09-26 11:20:06 +0200116 addr_of_mut!(self.regs.wdog_lock).write_volatile(Self::LOCK);
Imre Kiscd007ad2024-08-14 15:53:11 +0200117 }
118 }
119}
120
121/// SP805 Watchdog implementation
122pub struct Watchdog<R>
123where
124 R: Deref<Target = SP805Registers>,
125{
126 regs: R,
127 load_value: u32,
128}
129
130impl<R> Watchdog<R>
131where
Imre Kise05daa42024-09-26 11:20:06 +0200132 R: DerefMut<Target = SP805Registers>,
Imre Kiscd007ad2024-08-14 15:53:11 +0200133{
134 /// Create new watchdog instance
135 pub fn new(regs: R, load_value: u32) -> Self {
136 Self { regs, load_value }
137 }
138
139 /// Enable watchdog
Imre Kise05daa42024-09-26 11:20:06 +0200140 pub fn enable(&mut self) {
141 let load_value = self.load_value;
142 let mut regs = self.unlock();
Imre Kiscd007ad2024-08-14 15:53:11 +0200143
144 unsafe {
Imre Kise05daa42024-09-26 11:20:06 +0200145 addr_of_mut!(regs.wdog_load).write_volatile(load_value);
146 addr_of_mut!(regs.wdog_intclr).write_volatile(1);
147 addr_of_mut!(regs.wdog_control)
148 .write_volatile((ControlRegister::INTEN | ControlRegister::RESEN).bits());
Imre Kiscd007ad2024-08-14 15:53:11 +0200149 }
150 }
151
152 /// Disable watchdog
Imre Kise05daa42024-09-26 11:20:06 +0200153 pub fn disable(&mut self) {
Imre Kiscd007ad2024-08-14 15:53:11 +0200154 unsafe {
Imre Kise05daa42024-09-26 11:20:06 +0200155 addr_of_mut!(self.unlock().wdog_control)
156 .write_volatile(ControlRegister::empty().bits());
Imre Kiscd007ad2024-08-14 15:53:11 +0200157 }
158 }
159
160 /// Update watchdog
Imre Kise05daa42024-09-26 11:20:06 +0200161 pub fn update(&mut self) {
162 let load_value = self.load_value;
163
Imre Kiscd007ad2024-08-14 15:53:11 +0200164 unsafe {
Imre Kise05daa42024-09-26 11:20:06 +0200165 addr_of_mut!(self.unlock().wdog_load).write_volatile(load_value);
Imre Kiscd007ad2024-08-14 15:53:11 +0200166 }
167 }
168
Imre Kise05daa42024-09-26 11:20:06 +0200169 fn unlock(&mut self) -> WatchdogUnlockGuard<R> {
170 WatchdogUnlockGuard::new(&mut self.regs)
Imre Kiscd007ad2024-08-14 15:53:11 +0200171 }
172}