aboutsummaryrefslogtreecommitdiff
path: root/hw/misc/bcm2835_mphi.c
blob: 0428e10ba5cc4fc3b9dcbf86a0a151d09db9330a (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
/*
 * BCM2835 SOC MPHI emulation
 *
 * Very basic emulation, only providing the FIQ interrupt needed to
 * allow the dwc-otg USB host controller driver in the Raspbian kernel
 * to function.
 *
 * Copyright (c) 2020 Paul Zimmerman <pauldzim@gmail.com>
 *
 * 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.
 */

#include "qemu/osdep.h"
#include "qapi/error.h"
#include "hw/misc/bcm2835_mphi.h"
#include "migration/vmstate.h"
#include "qemu/error-report.h"
#include "qemu/log.h"
#include "qemu/main-loop.h"

static inline void mphi_raise_irq(BCM2835MphiState *s)
{
    qemu_set_irq(s->irq, 1);
}

static inline void mphi_lower_irq(BCM2835MphiState *s)
{
    qemu_set_irq(s->irq, 0);
}

static uint64_t mphi_reg_read(void *ptr, hwaddr addr, unsigned size)
{
    BCM2835MphiState *s = ptr;
    uint32_t val = 0;

    switch (addr) {
    case 0x28:  /* outdda */
        val = s->outdda;
        break;
    case 0x2c:  /* outddb */
        val = s->outddb;
        break;
    case 0x4c:  /* ctrl */
        val = s->ctrl;
        val |= 1 << 17;
        break;
    case 0x50:  /* intstat */
        val = s->intstat;
        break;
    case 0x1f0: /* swirq_set */
        val = s->swirq;
        break;
    case 0x1f4: /* swirq_clr */
        val = s->swirq;
        break;
    default:
        qemu_log_mask(LOG_UNIMP, "read from unknown register");
        break;
    }

    return val;
}

static void mphi_reg_write(void *ptr, hwaddr addr, uint64_t val, unsigned size)
{
    BCM2835MphiState *s = ptr;
    int do_irq = 0;

    switch (addr) {
    case 0x28:  /* outdda */
        s->outdda = val;
        break;
    case 0x2c:  /* outddb */
        s->outddb = val;
        if (val & (1 << 29)) {
            do_irq = 1;
        }
        break;
    case 0x4c:  /* ctrl */
        s->ctrl = val;
        if (val & (1 << 16)) {
            do_irq = -1;
        }
        break;
    case 0x50:  /* intstat */
        s->intstat = val;
        if (val & ((1 << 16) | (1 << 29))) {
            do_irq = -1;
        }
        break;
    case 0x1f0: /* swirq_set */
        s->swirq |= val;
        do_irq = 1;
        break;
    case 0x1f4: /* swirq_clr */
        s->swirq &= ~val;
        do_irq = -1;
        break;
    default:
        qemu_log_mask(LOG_UNIMP, "write to unknown register");
        return;
    }

    if (do_irq > 0) {
        mphi_raise_irq(s);
    } else if (do_irq < 0) {
        mphi_lower_irq(s);
    }
}

static const MemoryRegionOps mphi_mmio_ops = {
    .read = mphi_reg_read,
    .write = mphi_reg_write,
    .impl.min_access_size = 4,
    .impl.max_access_size = 4,
    .endianness = DEVICE_LITTLE_ENDIAN,
};

static void mphi_reset(DeviceState *dev)
{
    BCM2835MphiState *s = BCM2835_MPHI(dev);

    s->outdda = 0;
    s->outddb = 0;
    s->ctrl = 0;
    s->intstat = 0;
    s->swirq = 0;
}

static void mphi_realize(DeviceState *dev, Error **errp)
{
    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
    BCM2835MphiState *s = BCM2835_MPHI(dev);

    sysbus_init_irq(sbd, &s->irq);
}

static void mphi_init(Object *obj)
{
    SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
    BCM2835MphiState *s = BCM2835_MPHI(obj);

    memory_region_init_io(&s->iomem, obj, &mphi_mmio_ops, s, "mphi", MPHI_MMIO_SIZE);
    sysbus_init_mmio(sbd, &s->iomem);
}

const VMStateDescription vmstate_mphi_state = {
    .name = "mphi",
    .version_id = 1,
    .minimum_version_id = 1,
    .fields = (VMStateField[]) {
        VMSTATE_UINT32(outdda, BCM2835MphiState),
        VMSTATE_UINT32(outddb, BCM2835MphiState),
        VMSTATE_UINT32(ctrl, BCM2835MphiState),
        VMSTATE_UINT32(intstat, BCM2835MphiState),
        VMSTATE_UINT32(swirq, BCM2835MphiState),
        VMSTATE_END_OF_LIST()
    }
};

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

    dc->realize = mphi_realize;
    dc->reset = mphi_reset;
    dc->vmsd = &vmstate_mphi_state;
}

static const TypeInfo bcm2835_mphi_type_info = {
    .name          = TYPE_BCM2835_MPHI,
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(BCM2835MphiState),
    .instance_init = mphi_init,
    .class_init    = mphi_class_init,
};

static void bcm2835_mphi_register_types(void)
{
    type_register_static(&bcm2835_mphi_type_info);
}

type_init(bcm2835_mphi_register_types)