aboutsummaryrefslogtreecommitdiff
path: root/hw/watchdog/allwinner-wdt.c
blob: 6205765efec7c172016fd87fbd1079e704da31a9 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * Allwinner Watchdog emulation
 *
 * Copyright (C) 2023 Strahinja Jankovic <strahinja.p.jankovic@gmail.com>
 *
 *  This file is derived from Allwinner RTC,
 *  by Niek Linnenbank.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "qemu/osdep.h"
#include "qemu/log.h"
#include "qemu/units.h"
#include "qemu/module.h"
#include "trace.h"
#include "hw/sysbus.h"
#include "hw/registerfields.h"
#include "hw/watchdog/allwinner-wdt.h"
#include "sysemu/watchdog.h"
#include "migration/vmstate.h"

/* WDT registers */
enum {
    REG_IRQ_EN = 0,     /* Watchdog interrupt enable */
    REG_IRQ_STA,        /* Watchdog interrupt status */
    REG_CTRL,           /* Watchdog control register */
    REG_CFG,            /* Watchdog configuration register */
    REG_MODE,           /* Watchdog mode register */
};

/* Universal WDT register flags */
#define WDT_RESTART_MASK    (1 << 0)
#define WDT_EN_MASK         (1 << 0)

/* sun4i specific WDT register flags */
#define RST_EN_SUN4I_MASK       (1 << 1)
#define INTV_VALUE_SUN4I_SHIFT  (3)
#define INTV_VALUE_SUN4I_MASK   (0xfu << INTV_VALUE_SUN4I_SHIFT)

/* sun6i specific WDT register flags */
#define RST_EN_SUN6I_MASK       (1 << 0)
#define KEY_FIELD_SUN6I_SHIFT   (1)
#define KEY_FIELD_SUN6I_MASK    (0xfffu << KEY_FIELD_SUN6I_SHIFT)
#define KEY_FIELD_SUN6I         (0xA57u)
#define INTV_VALUE_SUN6I_SHIFT  (4)
#define INTV_VALUE_SUN6I_MASK   (0xfu << INTV_VALUE_SUN6I_SHIFT)

/* Map of INTV_VALUE to 0.5s units. */
static const uint8_t allwinner_wdt_count_map[] = {
    1,
    2,
    4,
    6,
    8,
    10,
    12,
    16,
    20,
    24,
    28,
    32
};

/* WDT sun4i register map (offset to name) */
const uint8_t allwinner_wdt_sun4i_regmap[] = {
    [0x0000] = REG_CTRL,
    [0x0004] = REG_MODE,
};

/* WDT sun6i register map (offset to name) */
const uint8_t allwinner_wdt_sun6i_regmap[] = {
    [0x0000] = REG_IRQ_EN,
    [0x0004] = REG_IRQ_STA,
    [0x0010] = REG_CTRL,
    [0x0014] = REG_CFG,
    [0x0018] = REG_MODE,
};

static bool allwinner_wdt_sun4i_read(AwWdtState *s, uint32_t offset)
{
    /* no sun4i specific registers currently implemented */
    return false;
}

static bool allwinner_wdt_sun4i_write(AwWdtState *s, uint32_t offset,
                                      uint32_t data)
{
    /* no sun4i specific registers currently implemented */
    return false;
}

static bool allwinner_wdt_sun4i_can_reset_system(AwWdtState *s)
{
    if (s->regs[REG_MODE] & RST_EN_SUN4I_MASK) {
        return true;
    } else {
        return false;
    }
}

static bool allwinner_wdt_sun4i_is_key_valid(AwWdtState *s, uint32_t val)
{
    /* sun4i has no key */
    return true;
}

static uint8_t allwinner_wdt_sun4i_get_intv_value(AwWdtState *s)
{
    return ((s->regs[REG_MODE] & INTV_VALUE_SUN4I_MASK) >>
            INTV_VALUE_SUN4I_SHIFT);
}

static bool allwinner_wdt_sun6i_read(AwWdtState *s, uint32_t offset)
{
    const AwWdtClass *c = AW_WDT_GET_CLASS(s);

    switch (c->regmap[offset]) {
    case REG_IRQ_EN:
    case REG_IRQ_STA:
    case REG_CFG:
        return true;
    default:
        break;
    }
    return false;
}

static bool allwinner_wdt_sun6i_write(AwWdtState *s, uint32_t offset,
                                      uint32_t data)
{
    const AwWdtClass *c = AW_WDT_GET_CLASS(s);

    switch (c->regmap[offset]) {
    case REG_IRQ_EN:
    case REG_IRQ_STA:
    case REG_CFG:
        return true;
    default:
        break;
    }
    return false;
}

static bool allwinner_wdt_sun6i_can_reset_system(AwWdtState *s)
{
    if (s->regs[REG_CFG] & RST_EN_SUN6I_MASK) {
        return true;
    } else {
        return false;
    }
}

static bool allwinner_wdt_sun6i_is_key_valid(AwWdtState *s, uint32_t val)
{
    uint16_t key = (val & KEY_FIELD_SUN6I_MASK) >> KEY_FIELD_SUN6I_SHIFT;
    return (key == KEY_FIELD_SUN6I);
}

static uint8_t allwinner_wdt_sun6i_get_intv_value(AwWdtState *s)
{
    return ((s->regs[REG_MODE] & INTV_VALUE_SUN6I_MASK) >>
            INTV_VALUE_SUN6I_SHIFT);
}

static void allwinner_wdt_update_timer(AwWdtState *s)
{
    const AwWdtClass *c = AW_WDT_GET_CLASS(s);
    uint8_t count = c->get_intv_value(s);

    ptimer_transaction_begin(s->timer);
    ptimer_stop(s->timer);

    /* Use map to convert. */
    if (count < sizeof(allwinner_wdt_count_map)) {
        ptimer_set_count(s->timer, allwinner_wdt_count_map[count]);
    } else {
        qemu_log_mask(LOG_GUEST_ERROR, "%s: incorrect INTV_VALUE 0x%02x\n",
                __func__, count);
    }

    ptimer_run(s->timer, 1);
    ptimer_transaction_commit(s->timer);

    trace_allwinner_wdt_update_timer(count);
}

static uint64_t allwinner_wdt_read(void *opaque, hwaddr offset,
                                       unsigned size)
{
    AwWdtState *s = AW_WDT(opaque);
    const AwWdtClass *c = AW_WDT_GET_CLASS(s);
    uint64_t r;

    if (offset >= c->regmap_size) {
        qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
                      __func__, (uint32_t)offset);
        return 0;
    }

    switch (c->regmap[offset]) {
    case REG_CTRL:
    case REG_MODE:
        r = s->regs[c->regmap[offset]];
        break;
    default:
        if (!c->read(s, offset)) {
            qemu_log_mask(LOG_UNIMP, "%s: unimplemented register 0x%04x\n",
                            __func__, (uint32_t)offset);
            return 0;
        }
        r = s->regs[c->regmap[offset]];
        break;
    }

    trace_allwinner_wdt_read(offset, r, size);

    return r;
}

static void allwinner_wdt_write(void *opaque, hwaddr offset,
                                   uint64_t val, unsigned size)
{
    AwWdtState *s = AW_WDT(opaque);
    const AwWdtClass *c = AW_WDT_GET_CLASS(s);
    uint32_t old_val;

    if (offset >= c->regmap_size) {
        qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
                      __func__, (uint32_t)offset);
        return;
    }

   trace_allwinner_wdt_write(offset, val, size);

    switch (c->regmap[offset]) {
    case REG_CTRL:
        if (c->is_key_valid(s, val)) {
            if (val & WDT_RESTART_MASK) {
                /* Kick timer */
                allwinner_wdt_update_timer(s);
            }
        }
        break;
    case REG_MODE:
        old_val = s->regs[REG_MODE];
        s->regs[REG_MODE] = (uint32_t)val;

        /* Check for rising edge on WDOG_MODE_EN */
        if ((s->regs[REG_MODE] & ~old_val) & WDT_EN_MASK) {
            allwinner_wdt_update_timer(s);
        }
        break;
    default:
        if (!c->write(s, offset, val)) {
            qemu_log_mask(LOG_UNIMP, "%s: unimplemented register 0x%04x\n",
                          __func__, (uint32_t)offset);
        }
        s->regs[c->regmap[offset]] = (uint32_t)val;
        break;
    }
}

static const MemoryRegionOps allwinner_wdt_ops = {
    .read = allwinner_wdt_read,
    .write = allwinner_wdt_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
    .valid = {
        .min_access_size = 4,
        .max_access_size = 4,
    },
    .impl.min_access_size = 4,
};

static void allwinner_wdt_expired(void *opaque)
{
    AwWdtState *s = AW_WDT(opaque);
    const AwWdtClass *c = AW_WDT_GET_CLASS(s);

    bool enabled = s->regs[REG_MODE] & WDT_EN_MASK;
    bool reset_enabled = c->can_reset_system(s);

    trace_allwinner_wdt_expired(enabled, reset_enabled);

    /* Perform watchdog action if watchdog is enabled and can trigger reset */
    if (enabled && reset_enabled) {
        watchdog_perform_action();
    }
}

static void allwinner_wdt_reset_enter(Object *obj, ResetType type)
{
    AwWdtState *s = AW_WDT(obj);

    trace_allwinner_wdt_reset_enter();

    /* Clear registers */
    memset(s->regs, 0, sizeof(s->regs));
}

static const VMStateDescription allwinner_wdt_vmstate = {
    .name = "allwinner-wdt",
    .version_id = 1,
    .minimum_version_id = 1,
    .fields = (VMStateField[]) {
        VMSTATE_PTIMER(timer, AwWdtState),
        VMSTATE_UINT32_ARRAY(regs, AwWdtState, AW_WDT_REGS_NUM),
        VMSTATE_END_OF_LIST()
    }
};

static void allwinner_wdt_init(Object *obj)
{
    SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
    AwWdtState *s = AW_WDT(obj);
    const AwWdtClass *c = AW_WDT_GET_CLASS(s);

    /* Memory mapping */
    memory_region_init_io(&s->iomem, OBJECT(s), &allwinner_wdt_ops, s,
                          TYPE_AW_WDT, c->regmap_size * 4);
    sysbus_init_mmio(sbd, &s->iomem);
}

static void allwinner_wdt_realize(DeviceState *dev, Error **errp)
{
    AwWdtState *s = AW_WDT(dev);

    s->timer = ptimer_init(allwinner_wdt_expired, s,
                           PTIMER_POLICY_NO_IMMEDIATE_TRIGGER |
                           PTIMER_POLICY_NO_IMMEDIATE_RELOAD |
                           PTIMER_POLICY_NO_COUNTER_ROUND_DOWN);

    ptimer_transaction_begin(s->timer);
    /* Set to 2Hz (0.5s period); other periods are multiples of 0.5s. */
    ptimer_set_freq(s->timer, 2);
    ptimer_set_limit(s->timer, 0xff, 1);
    ptimer_transaction_commit(s->timer);
}

static void allwinner_wdt_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    ResettableClass *rc = RESETTABLE_CLASS(klass);

    rc->phases.enter = allwinner_wdt_reset_enter;
    dc->realize = allwinner_wdt_realize;
    dc->vmsd = &allwinner_wdt_vmstate;
}

static void allwinner_wdt_sun4i_class_init(ObjectClass *klass, void *data)
{
    AwWdtClass *awc = AW_WDT_CLASS(klass);

    awc->regmap = allwinner_wdt_sun4i_regmap;
    awc->regmap_size = sizeof(allwinner_wdt_sun4i_regmap);
    awc->read = allwinner_wdt_sun4i_read;
    awc->write = allwinner_wdt_sun4i_write;
    awc->can_reset_system = allwinner_wdt_sun4i_can_reset_system;
    awc->is_key_valid = allwinner_wdt_sun4i_is_key_valid;
    awc->get_intv_value = allwinner_wdt_sun4i_get_intv_value;
}

static void allwinner_wdt_sun6i_class_init(ObjectClass *klass, void *data)
{
    AwWdtClass *awc = AW_WDT_CLASS(klass);

    awc->regmap = allwinner_wdt_sun6i_regmap;
    awc->regmap_size = sizeof(allwinner_wdt_sun6i_regmap);
    awc->read = allwinner_wdt_sun6i_read;
    awc->write = allwinner_wdt_sun6i_write;
    awc->can_reset_system = allwinner_wdt_sun6i_can_reset_system;
    awc->is_key_valid = allwinner_wdt_sun6i_is_key_valid;
    awc->get_intv_value = allwinner_wdt_sun6i_get_intv_value;
}

static const TypeInfo allwinner_wdt_info = {
    .name          = TYPE_AW_WDT,
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_init = allwinner_wdt_init,
    .instance_size = sizeof(AwWdtState),
    .class_init    = allwinner_wdt_class_init,
    .class_size    = sizeof(AwWdtClass),
    .abstract      = true,
};

static const TypeInfo allwinner_wdt_sun4i_info = {
    .name          = TYPE_AW_WDT_SUN4I,
    .parent        = TYPE_AW_WDT,
    .class_init    = allwinner_wdt_sun4i_class_init,
};

static const TypeInfo allwinner_wdt_sun6i_info = {
    .name          = TYPE_AW_WDT_SUN6I,
    .parent        = TYPE_AW_WDT,
    .class_init    = allwinner_wdt_sun6i_class_init,
};

static void allwinner_wdt_register(void)
{
    type_register_static(&allwinner_wdt_info);
    type_register_static(&allwinner_wdt_sun4i_info);
    type_register_static(&allwinner_wdt_sun6i_info);
}

type_init(allwinner_wdt_register)