summaryrefslogtreecommitdiff
path: root/bsp/allwinner/libraries/sunxi-hal/hal/source/watchdog/hal_watchdog.c
blob: e70e9e3d4b44fa4a41ec87e764d126f89d9ed781 (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
/*
 * Copyright 2018 Rockchip Electronics Co., Ltd
 *
 */
#include <stdio.h>
#include <hal_osal.h>
#include <barrier.h>
#include "sunxi_hal_common.h"
#include "platform_watchdog.h"

#define writel_wdt  hal_writel
#define readl_wdt   hal_readl

static unsigned long running;
static unsigned long running_saved;

static const int wdt_timeout_map[] =
{
    [1]  = 0x1,  /* 1s  32000cycles*/
    [2]  = 0x2,  /* 2s  64000*/
    [3]  = 0x3,  /* 3s  96000*/
    [4]  = 0x4,  /* 4s  128000*/
    [5]  = 0x5,  /* 5s  160000*/
    [6]  = 0x6,  /* 6s  192000*/
    [8]  = 0x7,  /* 8s  256000*/
    [10] = 0x8,  /* 10s 320000*/
    [12] = 0x9,  /* 12s 384000*/
    [14] = 0xA,  /* 14s 448000*/
    [16] = 0xB,  /* 16s 512000*/
};

int hal_watchdog_is_running(void)
{

    return running_saved;
}

int hal_watchdog_suspend(int timeout)
{

    running_saved = running;

    pr_debug("%s()\n", __func__);
    if (hal_watchdog_is_running()) {
        pr_debug("%s()\n", __func__);
        hal_watchdog_stop(timeout);
    }

    return 0;
}

int hal_watchdog_resume(int timeout)
{

    pr_debug("%s()\n", __func__);
    if (hal_watchdog_is_running()) {
        pr_debug("%s()\n", __func__);
        hal_watchdog_start(timeout);
    }

    return 0;
}
void hal_watchdog_info(void)
{
    struct hal_sunxi_wdt *wdt = (struct hal_sunxi_wdt *)WDT_BASE;
    printf("mode: 0x%x, cfg=0x%x, ctl=0x%x\n",
    (unsigned int)(wdt->mode), (unsigned int)(wdt->cfg), (unsigned int)(wdt->ctl));
}

void hal_watchdog_disable(void)
{
    struct hal_sunxi_wdt *wdt = (struct hal_sunxi_wdt *)WDT_BASE;
    unsigned int wtmode;

    pr_debug("%s()\n", __func__);
    wtmode = readl_wdt(&wdt->mode);
    wtmode &= ~WDT_MODE_EN;
    wtmode |= KEY_FIELD_MAGIC;

    writel_wdt(wtmode, &wdt->mode);
    isb();
    running = 0;
}

void hal_watchdog_reset(int timeout)
{
    int timeout_set = timeout;
    struct hal_sunxi_wdt *wdt = (struct hal_sunxi_wdt *)WDT_BASE;
    unsigned int wtmode;

    pr_debug("%s()\n", __func__);
    hal_watchdog_disable();

    if (timeout > 16)
    {
        timeout_set = 16;
    }

    if (wdt_timeout_map[timeout_set] == 0)
    {
        timeout_set++;
    }

    wtmode = KEY_FIELD_MAGIC | (wdt_timeout_map[timeout_set] << 4) | WDT_MODE_EN;

    writel_wdt(KEY_FIELD_MAGIC | WDT_CFG_RESET, &wdt->cfg);
    writel_wdt(wtmode, &wdt->mode);

    isb();
    writel_wdt(WDT_CTRL_KEY | WDT_CTRL_RESTART, &wdt->ctl);
    running = 1;
}

void hal_watchdog_restart(void)
{
    struct hal_sunxi_wdt *wdt = (struct hal_sunxi_wdt *)WDT_BASE;

    pr_debug("%s()\n", __func__);
    hal_watchdog_disable();

    /* add delay for watchdog disable */
    udelay(50);
    /* Set the watchdog for its shortest interval (.5s) and wait */
    writel_wdt(KEY_FIELD_MAGIC | WDT_CFG_RESET, &wdt->cfg);
    isb();
    writel_wdt(KEY_FIELD_MAGIC | WDT_MODE_EN, &wdt->mode);

    while (1) {};
}

void hal_watchdog_init(void)
{
    pr_debug("%s()\n", __func__);
    running = 0;
    running_saved = 0;
}

void hal_watchdog_stop(int timeout)
{
    pr_debug("%s()\n", __func__);
    hal_watchdog_disable();
}

void hal_watchdog_start(int timeout)
{
    pr_debug("%s()\n", __func__);
    hal_watchdog_reset(timeout);
}

void hal_watchdog_feed(void)
{
    struct hal_sunxi_wdt *wdt = (struct hal_sunxi_wdt *)WDT_BASE;
    pr_debug("%s()\n", __func__);
    writel_wdt(WDT_CTRL_KEY | WDT_CTRL_RESTART, &wdt->ctl);
}