aboutsummaryrefslogtreecommitdiff
path: root/hw/timer/sifive_pwm.c
blob: 4602fc1a616b920d09829d35325785c1e41107f8 (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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * SiFive PWM
 *
 * Copyright (c) 2020 Western Digital
 *
 * Author:  Alistair Francis <alistair.francis@wdc.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "qemu/osdep.h"
#include "trace.h"
#include "hw/irq.h"
#include "hw/timer/sifive_pwm.h"
#include "hw/qdev-properties.h"
#include "hw/registerfields.h"
#include "migration/vmstate.h"
#include "qemu/log.h"
#include "qemu/module.h"

#define HAS_PWM_EN_BITS(cfg) ((cfg & R_CONFIG_ENONESHOT_MASK) || \
                              (cfg & R_CONFIG_ENALWAYS_MASK))

#define PWMCMP_MASK 0xFFFF
#define PWMCOUNT_MASK 0x7FFFFFFF

REG32(CONFIG,                   0x00)
    FIELD(CONFIG, SCALE,            0, 4)
    FIELD(CONFIG, STICKY,           8, 1)
    FIELD(CONFIG, ZEROCMP,          9, 1)
    FIELD(CONFIG, DEGLITCH,         10, 1)
    FIELD(CONFIG, ENALWAYS,         12, 1)
    FIELD(CONFIG, ENONESHOT,        13, 1)
    FIELD(CONFIG, CMP0CENTER,       16, 1)
    FIELD(CONFIG, CMP1CENTER,       17, 1)
    FIELD(CONFIG, CMP2CENTER,       18, 1)
    FIELD(CONFIG, CMP3CENTER,       19, 1)
    FIELD(CONFIG, CMP0GANG,         24, 1)
    FIELD(CONFIG, CMP1GANG,         25, 1)
    FIELD(CONFIG, CMP2GANG,         26, 1)
    FIELD(CONFIG, CMP3GANG,         27, 1)
    FIELD(CONFIG, CMP0IP,           28, 1)
    FIELD(CONFIG, CMP1IP,           29, 1)
    FIELD(CONFIG, CMP2IP,           30, 1)
    FIELD(CONFIG, CMP3IP,           31, 1)
REG32(COUNT,                    0x08)
REG32(PWMS,                     0x10)
REG32(PWMCMP0,                  0x20)
REG32(PWMCMP1,                  0x24)
REG32(PWMCMP2,                  0x28)
REG32(PWMCMP3,                  0x2C)

static inline uint64_t sifive_pwm_ns_to_ticks(SiFivePwmState *s,
                                                uint64_t time)
{
    return muldiv64(time, s->freq_hz, NANOSECONDS_PER_SECOND);
}

static inline uint64_t sifive_pwm_ticks_to_ns(SiFivePwmState *s,
                                                uint64_t ticks)
{
    return muldiv64(ticks, NANOSECONDS_PER_SECOND, s->freq_hz);
}

static inline uint64_t sifive_pwm_compute_scale(SiFivePwmState *s)
{
    return s->pwmcfg & R_CONFIG_SCALE_MASK;
}

static void sifive_pwm_set_alarms(SiFivePwmState *s)
{
    uint64_t now_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);

    if (HAS_PWM_EN_BITS(s->pwmcfg)) {
        /*
         * Subtract ticks from number of ticks when the timer was zero
         * and mask to the register width.
         */
        uint64_t pwmcount = (sifive_pwm_ns_to_ticks(s, now_ns) -
                             s->tick_offset) & PWMCOUNT_MASK;
        uint64_t scale = sifive_pwm_compute_scale(s);
        /* PWMs only contains PWMCMP_MASK bits starting at scale */
        uint64_t pwms = (pwmcount & (PWMCMP_MASK << scale)) >> scale;

        for (int i = 0; i < SIFIVE_PWM_CHANS; i++) {
            uint64_t pwmcmp = s->pwmcmp[i] & PWMCMP_MASK;
            uint64_t pwmcmp_ticks = pwmcmp << scale;

            /*
             * Per circuit diagram and spec, both cases raises corresponding
             * IP bit one clock cycle after time expires.
             */
            if (pwmcmp > pwms) {
                uint64_t offset = pwmcmp_ticks - pwmcount + 1;
                uint64_t when_to_fire = now_ns +
                                          sifive_pwm_ticks_to_ns(s, offset);

                trace_sifive_pwm_set_alarm(when_to_fire, now_ns);
                timer_mod(&s->timer[i], when_to_fire);
            } else {
                /* Schedule interrupt for next cycle */
                trace_sifive_pwm_set_alarm(now_ns + 1, now_ns);
                timer_mod(&s->timer[i], now_ns + 1);
            }

        }
    } else {
        /*
         * If timer incrementing disabled, just do pwms > pwmcmp check since
         * a write may have happened to PWMs.
         */
        uint64_t pwmcount = (s->tick_offset) & PWMCOUNT_MASK;
        uint64_t scale = sifive_pwm_compute_scale(s);
        uint64_t pwms = (pwmcount & (PWMCMP_MASK << scale)) >> scale;

        for (int i = 0; i < SIFIVE_PWM_CHANS; i++) {
            uint64_t pwmcmp = s->pwmcmp[i] & PWMCMP_MASK;

            if (pwms >= pwmcmp) {
                trace_sifive_pwm_set_alarm(now_ns + 1, now_ns);
                timer_mod(&s->timer[i], now_ns + 1);
            } else {
                /* Effectively disable timer by scheduling far in future. */
                trace_sifive_pwm_set_alarm(0xFFFFFFFFFFFFFF, now_ns);
                timer_mod(&s->timer[i], 0xFFFFFFFFFFFFFF);
            }
        }
    }
}

static void sifive_pwm_interrupt(SiFivePwmState *s, int num)
{
    uint64_t now = sifive_pwm_ns_to_ticks(s,
                                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
    bool was_incrementing = HAS_PWM_EN_BITS(s->pwmcfg);

    trace_sifive_pwm_interrupt(num);

    s->pwmcfg |= R_CONFIG_CMP0IP_MASK << num;
    qemu_irq_raise(s->irqs[num]);

    /*
     * If the zerocmp is set and pwmcmp0 raised the interrupt
     * reset the zero ticks.
     */
    if ((s->pwmcfg & R_CONFIG_ZEROCMP_MASK) && (num == 0)) {
        /* If reset signal conditions, disable ENONESHOT. */
        s->pwmcfg &= ~R_CONFIG_ENONESHOT_MASK;

        if (was_incrementing) {
            /* If incrementing, time in ticks is when pwmcount is zero */
            s->tick_offset = now;
        } else {
            /* If not incrementing, pwmcount = 0 */
            s->tick_offset = 0;
        }
    }

    /*
     * If carryout bit set, which we discern via looking for overflow,
     * also reset ENONESHOT.
     */
    if (was_incrementing &&
        ((now & PWMCOUNT_MASK) < (s->tick_offset & PWMCOUNT_MASK))) {
        s->pwmcfg &= ~R_CONFIG_ENONESHOT_MASK;
    }

    /* Schedule or disable interrupts */
    sifive_pwm_set_alarms(s);

    /* If was enabled, and now not enabled, switch tick rep */
    if (was_incrementing && !HAS_PWM_EN_BITS(s->pwmcfg)) {
        s->tick_offset = (now - s->tick_offset) & PWMCOUNT_MASK;
    }
}

static void sifive_pwm_interrupt_0(void *opaque)
{
    SiFivePwmState *s = opaque;

    sifive_pwm_interrupt(s, 0);
}

static void sifive_pwm_interrupt_1(void *opaque)
{
    SiFivePwmState *s = opaque;

    sifive_pwm_interrupt(s, 1);
}

static void sifive_pwm_interrupt_2(void *opaque)
{
    SiFivePwmState *s = opaque;

    sifive_pwm_interrupt(s, 2);
}

static void sifive_pwm_interrupt_3(void *opaque)
{
    SiFivePwmState *s = opaque;

    sifive_pwm_interrupt(s, 3);
}

static uint64_t sifive_pwm_read(void *opaque, hwaddr addr,
                                  unsigned int size)
{
    SiFivePwmState *s = opaque;
    uint64_t cur_time, scale;
    uint64_t now = sifive_pwm_ns_to_ticks(s,
                                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));

    trace_sifive_pwm_read(addr);

    switch (addr) {
    case A_CONFIG:
        return s->pwmcfg;
    case A_COUNT:
        cur_time = s->tick_offset;

        if (HAS_PWM_EN_BITS(s->pwmcfg)) {
            cur_time = now - cur_time;
        }

        /*
         * Return the value in the counter with bit 31 always 0
         * This is allowed to wrap around so we don't need to check that.
         */
        return cur_time & PWMCOUNT_MASK;
    case A_PWMS:
        cur_time = s->tick_offset;
        scale = sifive_pwm_compute_scale(s);

        if (HAS_PWM_EN_BITS(s->pwmcfg)) {
            cur_time = now - cur_time;
        }

        return ((cur_time & PWMCOUNT_MASK) >> scale) & PWMCMP_MASK;
    case A_PWMCMP0:
        return s->pwmcmp[0] & PWMCMP_MASK;
    case A_PWMCMP1:
        return s->pwmcmp[1] & PWMCMP_MASK;
    case A_PWMCMP2:
        return s->pwmcmp[2] & PWMCMP_MASK;
    case A_PWMCMP3:
        return s->pwmcmp[3] & PWMCMP_MASK;
    default:
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
        return 0;
    }

    return 0;
}

static void sifive_pwm_write(void *opaque, hwaddr addr,
                               uint64_t val64, unsigned int size)
{
    SiFivePwmState *s = opaque;
    uint32_t value = val64;
    uint64_t new_offset, scale;
    uint64_t now = sifive_pwm_ns_to_ticks(s,
                                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));

    trace_sifive_pwm_write(value, addr);

    switch (addr) {
    case A_CONFIG:
        if (value & (R_CONFIG_CMP0CENTER_MASK | R_CONFIG_CMP1CENTER_MASK |
                     R_CONFIG_CMP2CENTER_MASK | R_CONFIG_CMP3CENTER_MASK)) {
            qemu_log_mask(LOG_UNIMP, "%s: CMPxCENTER is not supported\n",
                          __func__);
        }

        if (value & (R_CONFIG_CMP0GANG_MASK | R_CONFIG_CMP1GANG_MASK |
                     R_CONFIG_CMP2GANG_MASK | R_CONFIG_CMP3GANG_MASK)) {
            qemu_log_mask(LOG_UNIMP, "%s: CMPxGANG is not supported\n",
                          __func__);
        }

        if (value & (R_CONFIG_CMP0IP_MASK | R_CONFIG_CMP1IP_MASK |
                     R_CONFIG_CMP2IP_MASK | R_CONFIG_CMP3IP_MASK)) {
            qemu_log_mask(LOG_UNIMP, "%s: CMPxIP is not supported\n",
                          __func__);
        }

        if (!(value & R_CONFIG_CMP0IP_MASK)) {
            qemu_irq_lower(s->irqs[0]);
        }

        if (!(value & R_CONFIG_CMP1IP_MASK)) {
            qemu_irq_lower(s->irqs[1]);
        }

        if (!(value & R_CONFIG_CMP2IP_MASK)) {
            qemu_irq_lower(s->irqs[2]);
        }

        if (!(value & R_CONFIG_CMP3IP_MASK)) {
            qemu_irq_lower(s->irqs[3]);
        }

        /*
         * If this write enables the timer increment
         * set the time when pwmcount was zero to be cur_time - pwmcount.
         * If this write disables the timer increment
         * convert back from pwmcount to the time in ticks
         * when pwmcount was zero.
         */
        if ((!HAS_PWM_EN_BITS(s->pwmcfg) && HAS_PWM_EN_BITS(value)) ||
            (HAS_PWM_EN_BITS(s->pwmcfg) && !HAS_PWM_EN_BITS(value))) {
            s->tick_offset = (now - s->tick_offset) & PWMCOUNT_MASK;
        }

        s->pwmcfg = value;
        break;
    case A_COUNT:
        /* The guest changed the counter, updated the offset value. */
        new_offset = value;

        if (HAS_PWM_EN_BITS(s->pwmcfg)) {
            new_offset = now - new_offset;
        }

        s->tick_offset = new_offset;
        break;
    case A_PWMS:
        scale = sifive_pwm_compute_scale(s);
        new_offset = (((value & PWMCMP_MASK) << scale) & PWMCOUNT_MASK);

        if (HAS_PWM_EN_BITS(s->pwmcfg)) {
            new_offset = now - new_offset;
        }

        s->tick_offset = new_offset;
        break;
    case A_PWMCMP0:
        s->pwmcmp[0] = value & PWMCMP_MASK;
        break;
    case A_PWMCMP1:
        s->pwmcmp[1] = value & PWMCMP_MASK;
        break;
    case A_PWMCMP2:
        s->pwmcmp[2] = value & PWMCMP_MASK;
        break;
    case A_PWMCMP3:
        s->pwmcmp[3] = value & PWMCMP_MASK;
        break;
    default:
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
    }

    /* Update the alarms to reflect possible updated values */
    sifive_pwm_set_alarms(s);
}

static void sifive_pwm_reset(DeviceState *dev)
{
    SiFivePwmState *s = SIFIVE_PWM(dev);
    uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);

    s->pwmcfg = 0x00000000;
    s->pwmcmp[0] = 0x00000000;
    s->pwmcmp[1] = 0x00000000;
    s->pwmcmp[2] = 0x00000000;
    s->pwmcmp[3] = 0x00000000;

    s->tick_offset = sifive_pwm_ns_to_ticks(s, now);
}

static const MemoryRegionOps sifive_pwm_ops = {
    .read = sifive_pwm_read,
    .write = sifive_pwm_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
};

static const VMStateDescription vmstate_sifive_pwm = {
    .name = TYPE_SIFIVE_PWM,
    .version_id = 1,
    .minimum_version_id = 1,
    .fields = (const VMStateField[]) {
        VMSTATE_TIMER_ARRAY(timer, SiFivePwmState, 4),
        VMSTATE_UINT64(tick_offset, SiFivePwmState),
        VMSTATE_UINT32(pwmcfg, SiFivePwmState),
        VMSTATE_UINT32_ARRAY(pwmcmp, SiFivePwmState, 4),
        VMSTATE_END_OF_LIST()
    }
};

static Property sifive_pwm_properties[] = {
    /* 0.5Ghz per spec after FSBL */
    DEFINE_PROP_UINT64("clock-frequency", struct SiFivePwmState,
                       freq_hz, 500000000ULL),
    DEFINE_PROP_END_OF_LIST(),
};

static void sifive_pwm_init(Object *obj)
{
    SiFivePwmState *s = SIFIVE_PWM(obj);
    int i;

    for (i = 0; i < SIFIVE_PWM_IRQS; i++) {
        sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irqs[i]);
    }

    memory_region_init_io(&s->mmio, obj, &sifive_pwm_ops, s,
                          TYPE_SIFIVE_PWM, 0x100);
    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
}

static void sifive_pwm_realize(DeviceState *dev, Error **errp)
{
    SiFivePwmState *s = SIFIVE_PWM(dev);

    timer_init_ns(&s->timer[0], QEMU_CLOCK_VIRTUAL,
                  sifive_pwm_interrupt_0, s);

    timer_init_ns(&s->timer[1], QEMU_CLOCK_VIRTUAL,
                  sifive_pwm_interrupt_1, s);

    timer_init_ns(&s->timer[2], QEMU_CLOCK_VIRTUAL,
                  sifive_pwm_interrupt_2, s);

    timer_init_ns(&s->timer[3], QEMU_CLOCK_VIRTUAL,
                  sifive_pwm_interrupt_3, s);
}

static void sifive_pwm_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);

    device_class_set_legacy_reset(dc, sifive_pwm_reset);
    device_class_set_props(dc, sifive_pwm_properties);
    dc->vmsd = &vmstate_sifive_pwm;
    dc->realize = sifive_pwm_realize;
}

static const TypeInfo sifive_pwm_info = {
    .name          = TYPE_SIFIVE_PWM,
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(SiFivePwmState),
    .instance_init = sifive_pwm_init,
    .class_init    = sifive_pwm_class_init,
};

static void sifive_pwm_register_types(void)
{
    type_register_static(&sifive_pwm_info);
}

type_init(sifive_pwm_register_types)