1 /******************************************************************************
2  *
3  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4  * Analog Devices, Inc.),
5  * Copyright (C) 2023-2024 Analog Devices, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************************/
20 
21 /* **** Includes **** */
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include "mxc_device.h"
26 #include "mxc_errors.h"
27 #include "mxc_assert.h"
28 #include "mxc_sys.h"
29 #include "wdt_revb.h"
30 
31 /* **** Functions **** */
32 
MXC_WDT_RevB_Init(mxc_wdt_revb_regs_t * wdt,mxc_wdt_revb_cfg_t * cfg)33 int MXC_WDT_RevB_Init(mxc_wdt_revb_regs_t *wdt, mxc_wdt_revb_cfg_t *cfg)
34 {
35     if (wdt == NULL || cfg == NULL) {
36         return E_NULL_PTR;
37     }
38 
39     if (cfg->mode & MXC_WDT_REVB_WINDOWED) {
40         wdt->ctrl |= MXC_F_WDT_REVB_CTRL_WIN_EN;
41     } else {
42         wdt->ctrl &= ~(MXC_F_WDT_REVB_CTRL_WIN_EN);
43     }
44 
45     return E_NO_ERROR;
46 }
47 
MXC_WDT_RevB_SetIntPeriod(mxc_wdt_revb_regs_t * wdt,mxc_wdt_revb_cfg_t * cfg)48 void MXC_WDT_RevB_SetIntPeriod(mxc_wdt_revb_regs_t *wdt, mxc_wdt_revb_cfg_t *cfg)
49 {
50     MXC_SETFIELD(wdt->ctrl, MXC_F_WDT_REVB_CTRL_INT_LATE_VAL, cfg->upperIntPeriod);
51 
52     if (cfg->mode & MXC_WDT_REVB_WINDOWED) {
53         MXC_SETFIELD(wdt->ctrl, MXC_F_WDT_REVB_CTRL_INT_EARLY_VAL,
54                      (cfg->lowerIntPeriod << MXC_F_WDT_REVB_CTRL_INT_EARLY_VAL_POS));
55     }
56 }
57 
MXC_WDT_RevB_SetResetPeriod(mxc_wdt_revb_regs_t * wdt,mxc_wdt_revb_cfg_t * cfg)58 void MXC_WDT_RevB_SetResetPeriod(mxc_wdt_revb_regs_t *wdt, mxc_wdt_revb_cfg_t *cfg)
59 {
60     MXC_SETFIELD(wdt->ctrl, MXC_F_WDT_REVB_CTRL_RST_LATE_VAL,
61                  (cfg->upperResetPeriod << MXC_F_WDT_REVB_CTRL_RST_LATE_VAL_POS));
62 
63     if (cfg->mode & MXC_WDT_REVB_WINDOWED) {
64         MXC_SETFIELD(wdt->ctrl, MXC_F_WDT_REVB_CTRL_RST_EARLY_VAL,
65                      (cfg->lowerResetPeriod << MXC_F_WDT_REVB_CTRL_RST_EARLY_VAL_POS));
66     }
67 }
68 
MXC_WDT_RevB_Enable(mxc_wdt_revb_regs_t * wdt)69 void MXC_WDT_RevB_Enable(mxc_wdt_revb_regs_t *wdt)
70 {
71     wdt->rst = 0xFE; // Feed seuqence chips
72     wdt->rst = 0xED;
73     wdt->ctrl |= MXC_F_WDT_REVB_CTRL_EN; // Direct write chips
74 }
75 
MXC_WDT_RevB_Disable(mxc_wdt_revb_regs_t * wdt)76 void MXC_WDT_RevB_Disable(mxc_wdt_revb_regs_t *wdt)
77 {
78     wdt->rst = 0xDE; // Feed sequence chips
79     wdt->rst = 0xAD;
80     wdt->ctrl &= ~(MXC_F_WDT_REVB_CTRL_EN); // Direct write chips
81 }
82 
MXC_WDT_RevB_EnableInt(mxc_wdt_revb_regs_t * wdt,mxc_wdt_revb_en_t enable)83 void MXC_WDT_RevB_EnableInt(mxc_wdt_revb_regs_t *wdt, mxc_wdt_revb_en_t enable)
84 {
85     if (enable) {
86         wdt->ctrl |= MXC_F_WDT_REVB_CTRL_WDT_INT_EN;
87     } else {
88         wdt->ctrl &= ~(MXC_F_WDT_REVB_CTRL_WDT_INT_EN);
89     }
90 }
91 
MXC_WDT_RevB_EnableReset(mxc_wdt_revb_regs_t * wdt,mxc_wdt_revb_en_t enable)92 void MXC_WDT_RevB_EnableReset(mxc_wdt_revb_regs_t *wdt, mxc_wdt_revb_en_t enable)
93 {
94     if (enable) {
95         wdt->ctrl |= MXC_F_WDT_REVB_CTRL_WDT_RST_EN;
96     } else {
97         wdt->ctrl &= ~(MXC_F_WDT_REVB_CTRL_WDT_RST_EN);
98     }
99 }
100 
MXC_WDT_RevB_ResetTimer(mxc_wdt_revb_regs_t * wdt)101 void MXC_WDT_RevB_ResetTimer(mxc_wdt_revb_regs_t *wdt)
102 {
103     wdt->rst = 0x00A5;
104     wdt->rst = 0x005A;
105 }
106 
MXC_WDT_RevB_GetResetFlag(mxc_wdt_revb_regs_t * wdt)107 int MXC_WDT_RevB_GetResetFlag(mxc_wdt_revb_regs_t *wdt)
108 {
109     return (wdt->ctrl & (MXC_F_WDT_REVB_CTRL_RST_LATE | MXC_F_WDT_REVB_CTRL_RST_EARLY));
110 }
111 
MXC_WDT_RevB_ClearResetFlag(mxc_wdt_revb_regs_t * wdt)112 void MXC_WDT_RevB_ClearResetFlag(mxc_wdt_revb_regs_t *wdt)
113 {
114     wdt->ctrl &= ~(MXC_F_WDT_REVB_CTRL_RST_LATE | MXC_F_WDT_REVB_CTRL_RST_EARLY);
115 }
116 
MXC_WDT_RevB_GetIntFlag(mxc_wdt_revb_regs_t * wdt)117 int MXC_WDT_RevB_GetIntFlag(mxc_wdt_revb_regs_t *wdt)
118 {
119     return !!(wdt->ctrl & (MXC_F_WDT_REVB_CTRL_INT_LATE | MXC_F_WDT_REVB_CTRL_INT_EARLY));
120 }
121 
MXC_WDT_RevB_ClearIntFlag(mxc_wdt_revb_regs_t * wdt)122 void MXC_WDT_RevB_ClearIntFlag(mxc_wdt_revb_regs_t *wdt)
123 {
124     wdt->ctrl &= ~(MXC_F_WDT_REVB_CTRL_INT_LATE | MXC_F_WDT_REVB_CTRL_INT_EARLY);
125 }
126 
MXC_WDT_RevB_SetClockSource(mxc_wdt_revb_regs_t * wdt,int clock_source)127 void MXC_WDT_RevB_SetClockSource(mxc_wdt_revb_regs_t *wdt, int clock_source)
128 {
129     const uint8_t clock_source_num = 8; // Max number of clock sources for Rev B WDT
130     (void)clock_source_num;
131 
132     MXC_ASSERT((clock_source < clock_source_num) && (clock_source >= 0));
133     MXC_SETFIELD(wdt->clksel, MXC_F_WDT_REVB_CLKSEL_SOURCE,
134                  (clock_source << MXC_F_WDT_REVB_CLKSEL_SOURCE_POS));
135 }
136