hw/misc/zynq_slcr: Avoid #DIV/0! error
[qemu/ar7.git] / hw / misc / bcm2835_mphi.c
blob0428e10ba5cc4fc3b9dcbf86a0a151d09db9330a
1 /*
2 * BCM2835 SOC MPHI emulation
4 * Very basic emulation, only providing the FIQ interrupt needed to
5 * allow the dwc-otg USB host controller driver in the Raspbian kernel
6 * to function.
8 * Copyright (c) 2020 Paul Zimmerman <pauldzim@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "hw/misc/bcm2835_mphi.h"
24 #include "migration/vmstate.h"
25 #include "qemu/error-report.h"
26 #include "qemu/log.h"
27 #include "qemu/main-loop.h"
29 static inline void mphi_raise_irq(BCM2835MphiState *s)
31 qemu_set_irq(s->irq, 1);
34 static inline void mphi_lower_irq(BCM2835MphiState *s)
36 qemu_set_irq(s->irq, 0);
39 static uint64_t mphi_reg_read(void *ptr, hwaddr addr, unsigned size)
41 BCM2835MphiState *s = ptr;
42 uint32_t val = 0;
44 switch (addr) {
45 case 0x28: /* outdda */
46 val = s->outdda;
47 break;
48 case 0x2c: /* outddb */
49 val = s->outddb;
50 break;
51 case 0x4c: /* ctrl */
52 val = s->ctrl;
53 val |= 1 << 17;
54 break;
55 case 0x50: /* intstat */
56 val = s->intstat;
57 break;
58 case 0x1f0: /* swirq_set */
59 val = s->swirq;
60 break;
61 case 0x1f4: /* swirq_clr */
62 val = s->swirq;
63 break;
64 default:
65 qemu_log_mask(LOG_UNIMP, "read from unknown register");
66 break;
69 return val;
72 static void mphi_reg_write(void *ptr, hwaddr addr, uint64_t val, unsigned size)
74 BCM2835MphiState *s = ptr;
75 int do_irq = 0;
77 switch (addr) {
78 case 0x28: /* outdda */
79 s->outdda = val;
80 break;
81 case 0x2c: /* outddb */
82 s->outddb = val;
83 if (val & (1 << 29)) {
84 do_irq = 1;
86 break;
87 case 0x4c: /* ctrl */
88 s->ctrl = val;
89 if (val & (1 << 16)) {
90 do_irq = -1;
92 break;
93 case 0x50: /* intstat */
94 s->intstat = val;
95 if (val & ((1 << 16) | (1 << 29))) {
96 do_irq = -1;
98 break;
99 case 0x1f0: /* swirq_set */
100 s->swirq |= val;
101 do_irq = 1;
102 break;
103 case 0x1f4: /* swirq_clr */
104 s->swirq &= ~val;
105 do_irq = -1;
106 break;
107 default:
108 qemu_log_mask(LOG_UNIMP, "write to unknown register");
109 return;
112 if (do_irq > 0) {
113 mphi_raise_irq(s);
114 } else if (do_irq < 0) {
115 mphi_lower_irq(s);
119 static const MemoryRegionOps mphi_mmio_ops = {
120 .read = mphi_reg_read,
121 .write = mphi_reg_write,
122 .impl.min_access_size = 4,
123 .impl.max_access_size = 4,
124 .endianness = DEVICE_LITTLE_ENDIAN,
127 static void mphi_reset(DeviceState *dev)
129 BCM2835MphiState *s = BCM2835_MPHI(dev);
131 s->outdda = 0;
132 s->outddb = 0;
133 s->ctrl = 0;
134 s->intstat = 0;
135 s->swirq = 0;
138 static void mphi_realize(DeviceState *dev, Error **errp)
140 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
141 BCM2835MphiState *s = BCM2835_MPHI(dev);
143 sysbus_init_irq(sbd, &s->irq);
146 static void mphi_init(Object *obj)
148 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
149 BCM2835MphiState *s = BCM2835_MPHI(obj);
151 memory_region_init_io(&s->iomem, obj, &mphi_mmio_ops, s, "mphi", MPHI_MMIO_SIZE);
152 sysbus_init_mmio(sbd, &s->iomem);
155 const VMStateDescription vmstate_mphi_state = {
156 .name = "mphi",
157 .version_id = 1,
158 .minimum_version_id = 1,
159 .fields = (VMStateField[]) {
160 VMSTATE_UINT32(outdda, BCM2835MphiState),
161 VMSTATE_UINT32(outddb, BCM2835MphiState),
162 VMSTATE_UINT32(ctrl, BCM2835MphiState),
163 VMSTATE_UINT32(intstat, BCM2835MphiState),
164 VMSTATE_UINT32(swirq, BCM2835MphiState),
165 VMSTATE_END_OF_LIST()
169 static void mphi_class_init(ObjectClass *klass, void *data)
171 DeviceClass *dc = DEVICE_CLASS(klass);
173 dc->realize = mphi_realize;
174 dc->reset = mphi_reset;
175 dc->vmsd = &vmstate_mphi_state;
178 static const TypeInfo bcm2835_mphi_type_info = {
179 .name = TYPE_BCM2835_MPHI,
180 .parent = TYPE_SYS_BUS_DEVICE,
181 .instance_size = sizeof(BCM2835MphiState),
182 .instance_init = mphi_init,
183 .class_init = mphi_class_init,
186 static void bcm2835_mphi_register_types(void)
188 type_register_static(&bcm2835_mphi_type_info);
191 type_init(bcm2835_mphi_register_types)