2 * QEMU model of the Canon DIGIC UART block.
6 * This model is based on reverse engineering efforts
7 * made by CHDK (http://chdk.wikia.com) and
8 * Magic Lantern (http://www.magiclantern.fm) projects
11 * See "Serial terminal" docs here:
12 * http://magiclantern.wikia.com/wiki/Register_Map#Misc_Registers
14 * The QEMU model of the Milkymist UART block by Michael Walle
15 * is used as a template.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
29 #include "qemu/osdep.h"
31 #include "hw/sysbus.h"
32 #include "sysemu/char.h"
35 #include "hw/char/digic-uart.h"
42 static uint64_t digic_uart_read(void *opaque, hwaddr addr,
45 DigicUartState *s = opaque;
52 s->reg_st &= ~(ST_RX_RDY);
61 qemu_log_mask(LOG_UNIMP,
62 "digic-uart: read access to unknown register 0x"
63 TARGET_FMT_plx, addr << 2);
69 static void digic_uart_write(void *opaque, hwaddr addr, uint64_t value,
72 DigicUartState *s = opaque;
73 unsigned char ch = value;
80 /* XXX this blocks entire thread. Rewrite to use
81 * qemu_chr_fe_write and background I/O callbacks */
82 qemu_chr_fe_write_all(&s->chr, &ch, 1);
88 * Ignore write to R_ST.
90 * The point is that this register is actively used
91 * during receiving and transmitting symbols,
92 * but we don't know the function of most of bits.
94 * Ignoring writes to R_ST is only a simplification
95 * of the model. It has no perceptible side effects
96 * for existing guests.
101 qemu_log_mask(LOG_UNIMP,
102 "digic-uart: write access to unknown register 0x"
103 TARGET_FMT_plx, addr << 2);
107 static const MemoryRegionOps uart_mmio_ops = {
108 .read = digic_uart_read,
109 .write = digic_uart_write,
111 .min_access_size = 4,
112 .max_access_size = 4,
114 .endianness = DEVICE_NATIVE_ENDIAN,
117 static int uart_can_rx(void *opaque)
119 DigicUartState *s = opaque;
121 return !(s->reg_st & ST_RX_RDY);
124 static void uart_rx(void *opaque, const uint8_t *buf, int size)
126 DigicUartState *s = opaque;
128 assert(uart_can_rx(opaque));
130 s->reg_st |= ST_RX_RDY;
134 static void uart_event(void *opaque, int event)
138 static void digic_uart_reset(DeviceState *d)
140 DigicUartState *s = DIGIC_UART(d);
143 s->reg_st = ST_TX_RDY;
146 static void digic_uart_realize(DeviceState *dev, Error **errp)
148 DigicUartState *s = DIGIC_UART(dev);
151 qemu_chr_fe_set_handlers(&s->chr, uart_can_rx, uart_rx,
152 uart_event, s, NULL);
156 static void digic_uart_init(Object *obj)
158 DigicUartState *s = DIGIC_UART(obj);
160 memory_region_init_io(&s->regs_region, OBJECT(s), &uart_mmio_ops, s,
161 TYPE_DIGIC_UART, 0x18);
162 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->regs_region);
165 static const VMStateDescription vmstate_digic_uart = {
166 .name = "digic-uart",
168 .minimum_version_id = 1,
169 .fields = (VMStateField[]) {
170 VMSTATE_UINT32(reg_rx, DigicUartState),
171 VMSTATE_UINT32(reg_st, DigicUartState),
172 VMSTATE_END_OF_LIST()
176 static Property digic_uart_properties[] = {
177 DEFINE_PROP_CHR("chardev", DigicUartState, chr),
178 DEFINE_PROP_END_OF_LIST(),
181 static void digic_uart_class_init(ObjectClass *klass, void *data)
183 DeviceClass *dc = DEVICE_CLASS(klass);
185 dc->realize = digic_uart_realize;
186 dc->reset = digic_uart_reset;
187 dc->vmsd = &vmstate_digic_uart;
188 dc->props = digic_uart_properties;
191 static const TypeInfo digic_uart_info = {
192 .name = TYPE_DIGIC_UART,
193 .parent = TYPE_SYS_BUS_DEVICE,
194 .instance_size = sizeof(DigicUartState),
195 .instance_init = digic_uart_init,
196 .class_init = digic_uart_class_init,
199 static void digic_uart_register_types(void)
201 type_register_static(&digic_uart_info);
204 type_init(digic_uart_register_types)