summaryrefslogtreecommitdiff
path: root/bsp/nxp/imx/imx6ull-smart/drivers/drv_wdt.c
blob: 763b6526daff95d1d3b26b7c7affbcc48964d4e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author         Notes
 * 2021-07-28     songchao           first version
 */

#include <rtconfig.h>

#ifdef RT_USING_WDT
#include <rtthread.h>
#include <rtdbg.h>
#include "drv_wdt.h"
#include "fsl_wdog.h"
#include "imx6ull.h"

enum
{
#ifdef RT_USING_WDT1
    DEV_WDT1,
#endif

#ifdef RT_USING_WDT2
    DEV_WDT2,
#endif

#ifdef RT_USING_WDT3
    DEV_WDT3,
#endif

    DEV_MAX,
};

#ifdef RT_USING_WDT1
static wdog_config_t WDOG_1_config =
{
    .timeoutValue = 0xffu,
    .enablePowerDown = false,
    .softwareResetExtension = false,
    .softwareAssertion = true,
    .softwareResetSignal = true,
    .enableWdog = true,
    .workMode =
    {
        .enableWait = false,
        .enableStop = false,
        .enableDebug = false,
    },
    .enableInterrupt = false,
    .interruptTimeValue = 0x04u,
};
#endif

#ifdef RT_USING_WDT2
static wdog_config_t WDOG_2_config =
{
    .timeoutValue = 0xffu,
    .enablePowerDown = false,
    .softwareResetExtension = false,
    .softwareAssertion = true,
    .softwareResetSignal = true,
    .enableWdog = true,
    .workMode =
    {
        .enableWait = false,
        .enableStop = false,
        .enableDebug = false,
    },
    .enableInterrupt = false,
    .interruptTimeValue = 0x04u,
};
#endif

#ifdef RT_USING_WDT3
static wdog_config_t WDOG_3_config =
{
    .timeoutValue = 0xffu,
    .enablePowerDown = false,
    .softwareResetExtension = false,
    .softwareAssertion = true,
    .softwareResetSignal = true,
    .enableWdog = true,
    .workMode =
    {
        .enableWait = false,
        .enableStop = false,
        .enableDebug = false,
    },
    .enableInterrupt = false,
    .interruptTimeValue = 0x04u,
};
#endif

static rt_watchdog_t imx6ull_watchdog[DEV_MAX] =
{
#ifdef RT_USING_WDT1
    {
        .name = "wdt1",
        .paddr = IMX6ULL_WATCHDOG1_BASE,
        .config = &WDOG_1_config,
    },
#endif

#ifdef RT_USING_WDT2
    {
        .name = "wdt2",
        .paddr = IMX6ULL_WATCHDOG2_BASE,
        .config = &WDOG_2_config,
    },
#endif

#ifdef RT_USING_WDT3
    {
        .name = "wdt3",
        .paddr = IMX6ULL_WATCHDOG3_BASE,
        .config = &WDOG_3_config,
    },
#endif

};

static rt_err_t imx6ull_wdog_init(rt_watchdog_t *wdt)
{
    WDOG_Type *base = RT_NULL;
    base = (WDOG_Type *)wdt->vaddr;
    WDOG_Init(base, wdt->config);
    WDOG_Disable(base);
    return RT_EOK;
}

static rt_err_t imx6ull_wdog_control(rt_watchdog_t *wdt, int cmd, void *args)
{
    RT_ASSERT(wdt != NULL);

    WDOG_Type *base = RT_NULL;
    base = (WDOG_Type *)wdt->vaddr;

    switch(cmd)
    {
        case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
        {
            *(uint16_t *)args = (base->WCR >> 8)  / 2;
        }
        break;
        case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
        {
            RT_ASSERT(*(uint16_t *)args != 0);
            WDOG_SetTimeoutValue(base, (*(uint16_t *)args) * 2);
            WDOG_Disable(base);
        }
        break;
        case RT_DEVICE_CTRL_WDT_KEEPALIVE:
        {
            WDOG_Refresh(base);
        }
        break;
        case RT_DEVICE_CTRL_WDT_START:
        {
            WDOG_Enable(base);
        }
        break;
        case RT_DEVICE_CTRL_WDT_STOP:
        {
            WDOG_Disable(base);
        }
        break;
        default:
            return RT_EINVAL;
    }

    return RT_EOK;
}

static struct rt_watchdog_ops imx6ull_wdog_ops =
{
    .init = imx6ull_wdog_init,
    .control = imx6ull_wdog_control,
};

int rt_hw_wdt_init(void)
{
    rt_err_t ret = RT_EOK;
    for(int idx = 0; idx < GET_ARRAY_NUM(imx6ull_watchdog); idx++)
    {
        imx6ull_watchdog[idx].ops = &imx6ull_wdog_ops;
        imx6ull_watchdog[idx].vaddr = platform_get_periph_vaddr(imx6ull_watchdog[idx].paddr);
        ret = rt_hw_watchdog_register(&imx6ull_watchdog[idx], imx6ull_watchdog[idx].name,
                                         RT_DEVICE_FLAG_DEACTIVATE, RT_NULL);
        if (ret != RT_EOK)
        {
            LOG_E("rt device register failed %d\n", ret);
        }
    }

    return ret;
}

INIT_DEVICE_EXPORT(rt_hw_wdt_init);

#endif