qemu/hw/char/lm32_juart.c
<<
>>
Prefs
   1/*
   2 *  LatticeMico32 JTAG UART model.
   3 *
   4 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
   5 *
   6 * This library is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU Lesser General Public
   8 * License as published by the Free Software Foundation; either
   9 * version 2 of the License, or (at your option) any later version.
  10 *
  11 * This library is distributed in the hope that it will be useful,
  12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14 * Lesser General Public License for more details.
  15 *
  16 * You should have received a copy of the GNU Lesser General Public
  17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18 */
  19
  20#include "qemu/osdep.h"
  21#include "hw/hw.h"
  22#include "hw/sysbus.h"
  23#include "qemu/module.h"
  24#include "trace.h"
  25#include "chardev/char-fe.h"
  26
  27#include "hw/char/lm32_juart.h"
  28
  29enum {
  30    LM32_JUART_MIN_SAVE_VERSION = 0,
  31    LM32_JUART_CURRENT_SAVE_VERSION = 0,
  32    LM32_JUART_MAX_SAVE_VERSION = 0,
  33};
  34
  35enum {
  36    JTX_FULL = (1<<8),
  37};
  38
  39enum {
  40    JRX_FULL = (1<<8),
  41};
  42
  43#define LM32_JUART(obj) OBJECT_CHECK(LM32JuartState, (obj), TYPE_LM32_JUART)
  44
  45struct LM32JuartState {
  46    SysBusDevice parent_obj;
  47
  48    CharBackend chr;
  49
  50    uint32_t jtx;
  51    uint32_t jrx;
  52};
  53typedef struct LM32JuartState LM32JuartState;
  54
  55uint32_t lm32_juart_get_jtx(DeviceState *d)
  56{
  57    LM32JuartState *s = LM32_JUART(d);
  58
  59    trace_lm32_juart_get_jtx(s->jtx);
  60    return s->jtx;
  61}
  62
  63uint32_t lm32_juart_get_jrx(DeviceState *d)
  64{
  65    LM32JuartState *s = LM32_JUART(d);
  66
  67    trace_lm32_juart_get_jrx(s->jrx);
  68    return s->jrx;
  69}
  70
  71void lm32_juart_set_jtx(DeviceState *d, uint32_t jtx)
  72{
  73    LM32JuartState *s = LM32_JUART(d);
  74    unsigned char ch = jtx & 0xff;
  75
  76    trace_lm32_juart_set_jtx(s->jtx);
  77
  78    s->jtx = jtx;
  79    /* XXX this blocks entire thread. Rewrite to use
  80     * qemu_chr_fe_write and background I/O callbacks */
  81    qemu_chr_fe_write_all(&s->chr, &ch, 1);
  82}
  83
  84void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx)
  85{
  86    LM32JuartState *s = LM32_JUART(d);
  87
  88    trace_lm32_juart_set_jrx(s->jrx);
  89    s->jrx &= ~JRX_FULL;
  90}
  91
  92static void juart_rx(void *opaque, const uint8_t *buf, int size)
  93{
  94    LM32JuartState *s = opaque;
  95
  96    s->jrx = *buf | JRX_FULL;
  97}
  98
  99static int juart_can_rx(void *opaque)
 100{
 101    LM32JuartState *s = opaque;
 102
 103    return !(s->jrx & JRX_FULL);
 104}
 105
 106static void juart_event(void *opaque, int event)
 107{
 108}
 109
 110static void juart_reset(DeviceState *d)
 111{
 112    LM32JuartState *s = LM32_JUART(d);
 113
 114    s->jtx = 0;
 115    s->jrx = 0;
 116}
 117
 118static void lm32_juart_realize(DeviceState *dev, Error **errp)
 119{
 120    LM32JuartState *s = LM32_JUART(dev);
 121
 122    qemu_chr_fe_set_handlers(&s->chr, juart_can_rx, juart_rx,
 123                             juart_event, NULL, s, NULL, true);
 124}
 125
 126static const VMStateDescription vmstate_lm32_juart = {
 127    .name = "lm32-juart",
 128    .version_id = 1,
 129    .minimum_version_id = 1,
 130    .fields = (VMStateField[]) {
 131        VMSTATE_UINT32(jtx, LM32JuartState),
 132        VMSTATE_UINT32(jrx, LM32JuartState),
 133        VMSTATE_END_OF_LIST()
 134    }
 135};
 136
 137static Property lm32_juart_properties[] = {
 138    DEFINE_PROP_CHR("chardev", LM32JuartState, chr),
 139    DEFINE_PROP_END_OF_LIST(),
 140};
 141
 142static void lm32_juart_class_init(ObjectClass *klass, void *data)
 143{
 144    DeviceClass *dc = DEVICE_CLASS(klass);
 145
 146    dc->reset = juart_reset;
 147    dc->vmsd = &vmstate_lm32_juart;
 148    dc->props = lm32_juart_properties;
 149    dc->realize = lm32_juart_realize;
 150}
 151
 152static const TypeInfo lm32_juart_info = {
 153    .name          = TYPE_LM32_JUART,
 154    .parent        = TYPE_SYS_BUS_DEVICE,
 155    .instance_size = sizeof(LM32JuartState),
 156    .class_init    = lm32_juart_class_init,
 157};
 158
 159static void lm32_juart_register_types(void)
 160{
 161    type_register_static(&lm32_juart_info);
 162}
 163
 164type_init(lm32_juart_register_types)
 165