1 /* 2 * QEMU Synchronous Serial Interface support 3 * 4 * Copyright (c) 2009 CodeSourcery. 5 * Copyright (c) 2012 Peter A.G. Crosthwaite (peter.crosthwaite@petalogix.com) 6 * Copyright (c) 2012 PetaLogix Pty Ltd. 7 * Written by Paul Brook 8 * 9 * This code is licensed under the GNU GPL v2. 10 * 11 * Contributions after 2012-01-13 are licensed under the terms of the 12 * GNU GPL, version 2 or (at your option) any later version. 13 */ 14 15 #include "qemu/osdep.h" 16 #include "hw/qdev-properties.h" 17 #include "hw/ssi/ssi.h" 18 #include "migration/vmstate.h" 19 #include "qemu/module.h" 20 #include "qapi/error.h" 21 #include "qom/object.h" 22 23 struct SSIBus { 24 BusState parent_obj; 25 }; 26 27 #define TYPE_SSI_BUS "SSI" 28 OBJECT_DECLARE_SIMPLE_TYPE(SSIBus, SSI_BUS) 29 30 static const TypeInfo ssi_bus_info = { 31 .name = TYPE_SSI_BUS, 32 .parent = TYPE_BUS, 33 .instance_size = sizeof(SSIBus), 34 }; 35 36 static void ssi_cs_default(void *opaque, int n, int level) 37 { 38 SSIPeripheral *s = SSI_PERIPHERAL(opaque); 39 bool cs = !!level; 40 assert(n == 0); 41 if (s->cs != cs) { 42 if (s->spc->set_cs) { 43 s->spc->set_cs(s, cs); 44 } 45 } 46 s->cs = cs; 47 } 48 49 static uint32_t ssi_transfer_raw_default(SSIPeripheral *dev, uint32_t val) 50 { 51 SSIPeripheralClass *ssc = dev->spc; 52 53 if ((dev->cs && ssc->cs_polarity == SSI_CS_HIGH) || 54 (!dev->cs && ssc->cs_polarity == SSI_CS_LOW) || 55 ssc->cs_polarity == SSI_CS_NONE) { 56 return ssc->transfer(dev, val); 57 } 58 return 0; 59 } 60 61 static void ssi_peripheral_realize(DeviceState *dev, Error **errp) 62 { 63 SSIPeripheral *s = SSI_PERIPHERAL(dev); 64 SSIPeripheralClass *ssc = SSI_PERIPHERAL_GET_CLASS(s); 65 66 if (ssc->transfer_raw == ssi_transfer_raw_default && 67 ssc->cs_polarity != SSI_CS_NONE) { 68 qdev_init_gpio_in_named(dev, ssi_cs_default, SSI_GPIO_CS, 1); 69 } 70 s->spc = ssc; 71 72 ssc->realize(s, errp); 73 } 74 75 static Property ssi_peripheral_properties[] = { 76 DEFINE_PROP_UINT32("addr", SSIPeripheral, addr, 0), 77 DEFINE_PROP_END_OF_LIST(), 78 }; 79 80 static void ssi_peripheral_class_init(ObjectClass *klass, void *data) 81 { 82 SSIPeripheralClass *ssc = SSI_PERIPHERAL_CLASS(klass); 83 DeviceClass *dc = DEVICE_CLASS(klass); 84 85 dc->realize = ssi_peripheral_realize; 86 dc->bus_type = TYPE_SSI_BUS; 87 if (!ssc->transfer_raw) { 88 ssc->transfer_raw = ssi_transfer_raw_default; 89 } 90 device_class_set_props(dc, ssi_peripheral_properties); 91 } 92 93 static const TypeInfo ssi_peripheral_info = { 94 .name = TYPE_SSI_PERIPHERAL, 95 .parent = TYPE_DEVICE, 96 .class_init = ssi_peripheral_class_init, 97 .class_size = sizeof(SSIPeripheralClass), 98 .abstract = true, 99 }; 100 101 bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bus, Error **errp) 102 { 103 return qdev_realize_and_unref(dev, &bus->parent_obj, errp); 104 } 105 106 DeviceState *ssi_create_peripheral(SSIBus *bus, const char *name) 107 { 108 DeviceState *dev = qdev_new(name); 109 110 ssi_realize_and_unref(dev, bus, &error_fatal); 111 return dev; 112 } 113 114 SSIBus *ssi_create_bus(DeviceState *parent, const char *name) 115 { 116 BusState *bus; 117 bus = qbus_new(TYPE_SSI_BUS, parent, name); 118 return SSI_BUS(bus); 119 } 120 121 uint32_t ssi_transfer(SSIBus *bus, uint32_t val) 122 { 123 BusState *b = BUS(bus); 124 BusChild *kid; 125 uint32_t r = 0; 126 127 QTAILQ_FOREACH(kid, &b->children, sibling) { 128 SSIPeripheral *p = SSI_PERIPHERAL(kid->child); 129 r |= p->spc->transfer_raw(p, val); 130 } 131 132 return r; 133 } 134 135 const VMStateDescription vmstate_ssi_peripheral = { 136 .name = "SSISlave", 137 .version_id = 1, 138 .minimum_version_id = 1, 139 .fields = (VMStateField[]) { 140 VMSTATE_BOOL(cs, SSIPeripheral), 141 VMSTATE_END_OF_LIST() 142 } 143 }; 144 145 static void ssi_peripheral_register_types(void) 146 { 147 type_register_static(&ssi_bus_info); 148 type_register_static(&ssi_peripheral_info); 149 } 150 151 type_init(ssi_peripheral_register_types) 152