summaryrefslogtreecommitdiff
path: root/components/drivers/watchdog/watchdog-i6300esb.c
blob: 6d4867ee5d62b374970443a9469ed32a0247abc4 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-26     GuEe-GUI     first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "wdt.i6300esb"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#define I6300ESB_REG_BAR        0

/* PCI configuration registers */
#define ESB_CONFIG_PCI_REG      0x60    /* Config register */
#define ESB_LOCK_PCI_REG        0x68    /* WDT lock register */

/* Memory mapped registers */
#define ESB_TIMER1_REG          0x00    /* Timer1 value after each reset */
#define ESB_TIMER2_REG          0x04    /* Timer2 value after each reset */
#define ESB_GINTSR_REG          0x08    /* General Interrupt Status Reg */
#define ESB_RELOAD_REG          0x0c    /* Reload register */

/* Lock register bits */
#define ESB_WDT_FUNC            (0x01 << 2) /* Watchdog functionality */
#define ESB_WDT_ENABLE          (0x01 << 1) /* Enable WDT */
#define ESB_WDT_LOCK            (0x01 << 0) /* Lock (nowayout) */

/* Config register bits */
#define ESB_WDT_REBOOT          (0x01 << 5) /* Enable reboot on timeout */
#define ESB_WDT_FREQ            (0x01 << 2) /* Decrement frequency */
#define ESB_WDT_INTTYPE         (0x03 << 0) /* Interrupt type on timer1 timeout */

/* Reload register bits */
#define ESB_WDT_TIMEOUT         (0x01 << 9) /* Watchdog timed out */
#define ESB_WDT_RELOAD          (0x01 << 8) /* prevent timeout */

/* Magic constants */
#define ESB_UNLOCK1             0x80    /* Step 1 to unlock reset registers */
#define ESB_UNLOCK2             0x86    /* Step 2 to unlock reset registers */

/* 30 sec default heartbeat (1 < heartbeat < 2*1023) */
#define ESB_HEARTBEAT_MIN       1
#define ESB_HEARTBEAT_MAX       2046
#define ESB_HEARTBEAT_DEFAULT   30

struct i6300esb_wdt
{
    rt_watchdog_t parent;

    void *regs;
    rt_uint32_t timeout;
    struct rt_pci_device *pdev;
};

#define raw_to_i6300esb_wdt(raw) rt_container_of(raw, struct i6300esb_wdt, parent)

/*
 * Prepare for reloading the timer by unlocking the proper registers.
 * This is performed by first writing 0x80 followed by 0x86 to the
 * reload register. After this the appropriate registers can be written
 * to once before they need to be unlocked again.
 */
rt_inline void i6300esb_wdt_unlock_registers(struct i6300esb_wdt *esb)
{
    HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_UNLOCK1;
    HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_UNLOCK2;
}

static rt_uint32_t i6300esb_timer_start(struct i6300esb_wdt *esb)
{
    i6300esb_wdt_unlock_registers(esb);
    HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_WDT_RELOAD;

    rt_pci_write_config_u8(esb->pdev, ESB_LOCK_PCI_REG, ESB_WDT_ENABLE);

    return RT_EOK;
}

static rt_uint32_t i6300esb_timer_stop(struct i6300esb_wdt *esb)
{
    rt_uint8_t val;

    /* First, reset timers as suggested by the docs */
    i6300esb_wdt_unlock_registers(esb);
    HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_WDT_RELOAD;

    /* Then disable the WDT */
    rt_pci_write_config_u8(esb->pdev, ESB_LOCK_PCI_REG, 0x0);
    rt_pci_read_config_u8(esb->pdev, ESB_LOCK_PCI_REG, &val);

    /* Returns 0 if the timer was disabled, non-zero otherwise */
    return val & ESB_WDT_ENABLE;
}

static rt_err_t esb_timer_keepalive(struct i6300esb_wdt *esb)
{
    i6300esb_wdt_unlock_registers(esb);
    HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_WDT_RELOAD;

    return RT_EOK;
}

static rt_err_t i6300esb_timer_set_heartbeat(struct i6300esb_wdt *esb, rt_uint32_t time)
{
    rt_uint32_t val;

    if ((time < ESB_HEARTBEAT_MIN) || (time > ESB_HEARTBEAT_MAX))
    {
        return -RT_EINVAL;
    }

    /*
     * We shift by 9, so if we are passed a value of 1 sec,
     * val will be 1 << 9 = 512, then write that to two
     * timers => 2 * 512 = 1024 (which is decremented at 1KHz)
     */
    val = time << 9;

    /* Write timer 1 */
    i6300esb_wdt_unlock_registers(esb);
    HWREG32(esb->regs + ESB_TIMER1_REG) = val;

    /* Write timer 2 */
    i6300esb_wdt_unlock_registers(esb);
    HWREG32(esb->regs + ESB_TIMER2_REG) = val;

    /* Reload */
    i6300esb_wdt_unlock_registers(esb);
    HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_WDT_RELOAD;

    esb->timeout = time;

    return RT_EOK;
}

static rt_err_t i6300esb_wdt_init(rt_watchdog_t *wdt)
{
    return RT_EOK;
}

static rt_err_t i6300esb_wdt_control(rt_watchdog_t *wdt, int cmd, void *args)
{
    rt_err_t err = RT_EOK;
    struct i6300esb_wdt *esb = raw_to_i6300esb_wdt(wdt);

    switch (cmd)
    {
    case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
        *(rt_uint32_t *)args = esb->timeout;
        break;

    case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
        if (args == RT_NULL)
        {
            err = -RT_EINVAL;
            break;
        }
        err = i6300esb_timer_set_heartbeat(esb, *(rt_uint32_t *)args);
        break;

    case RT_DEVICE_CTRL_WDT_KEEPALIVE:
        err = esb_timer_keepalive(esb);
        break;

    case RT_DEVICE_CTRL_WDT_START:
        err = i6300esb_timer_start(esb);
        break;

    case RT_DEVICE_CTRL_WDT_STOP:
        err = i6300esb_timer_stop(esb);
        break;

    default:
        err = -RT_EINVAL;
    }

    return err;
}

static const struct rt_watchdog_ops i6300esb_wdt_ops =
{
    .init = i6300esb_wdt_init,
    .control = i6300esb_wdt_control,
};

static rt_err_t i6300esb_wdt_probe(struct rt_pci_device *pdev)
{
    rt_err_t err;
    rt_uint8_t val1;
    rt_uint16_t val2;
    const char *dev_name;
    struct i6300esb_wdt *esb = rt_calloc(1, sizeof(*esb));

    if (!esb)
    {
        return -RT_ENOMEM;
    }

    esb->regs = rt_pci_iomap(pdev, I6300ESB_REG_BAR);

    if (!esb->regs)
    {
        err = -RT_EIO;

        goto _fail;
    }

    /*
     * Config register:
     * Bit    5 : 0 = Enable WDT_OUTPUT
     * Bit    2 : 0 = set the timer frequency to the PCI clock
     * divided by 2^15 (approx 1KHz).
     * Bits 1:0 : 11 = WDT_INT_TYPE Disabled.
     * The watchdog has two timers, it can be setup so that the expiry of timer1
     * results in an interrupt and the expiry of timer2 results in a reboot.
     * We set it to not generate any interrupts as there is not much
     * we can do with it right now.
     */
    rt_pci_write_config_u16(pdev, ESB_CONFIG_PCI_REG, 0x0003);

    /* Check that the WDT isn't already locked */
    rt_pci_read_config_u8(pdev, ESB_LOCK_PCI_REG, &val1);
    if (val1 & ESB_WDT_LOCK)
    {
        LOG_W("Nowayout already set");
    }

    /* Set the timer to watchdog mode and disable it for now */
    rt_pci_write_config_u8(pdev, ESB_LOCK_PCI_REG, 0x00);

    /* Check if the watchdog was previously triggered */
    i6300esb_wdt_unlock_registers(esb);
    val2 = HWREG16(esb->regs + ESB_RELOAD_REG);
    if (val2 & ESB_WDT_TIMEOUT)
    {
        LOG_D("Card previously reset the CPU");
    }

    /* Reset WDT_TIMEOUT flag and timers */
    i6300esb_wdt_unlock_registers(esb);
    HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_WDT_TIMEOUT | ESB_WDT_RELOAD;

    /* And set the correct timeout value */
    i6300esb_timer_set_heartbeat(esb, ESB_HEARTBEAT_DEFAULT);

    pdev->parent.user_data = esb;

    esb->pdev = pdev;
    esb->parent.ops = &i6300esb_wdt_ops;

    rt_dm_dev_set_name_auto(&esb->parent.parent, "wdt");
    dev_name = rt_dm_dev_get_name(&esb->parent.parent);
    rt_hw_watchdog_register(&esb->parent, dev_name, 0, esb);

    return RT_EOK;

_fail:
    if (esb->regs)
    {
        rt_iounmap(esb->regs);
    }

    rt_free(esb);

    return err;
}

static rt_err_t i6300esb_wdt_remove(struct rt_pci_device *pdev)
{
    struct i6300esb_wdt *esb = pdev->parent.user_data;

    i6300esb_timer_stop(esb);

    rt_device_unregister(&esb->parent.parent);

    rt_iounmap(esb->regs);
    rt_free(esb);

    return RT_EOK;
}

static const struct rt_pci_device_id i6300esb_wdt_pci_ids[] =
{
    { RT_PCI_DEVICE_ID(PCI_VENDOR_ID_INTEL, 0x25ab), },
    { /* sentinel */ }
};

static struct rt_pci_driver i6300esb_wdt_driver =
{
    .name = "i6300esb-wdt",

    .ids = i6300esb_wdt_pci_ids,
    .probe = i6300esb_wdt_probe,
    .remove = i6300esb_wdt_remove,
};
RT_PCI_DRIVER_EXPORT(i6300esb_wdt_driver);