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/ssi/ssi.h" 17 #include "qemu/module.h" 18 19 struct SSIBus { 20 BusState parent_obj; 21 }; 22 23 #define TYPE_SSI_BUS "SSI" 24 #define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS) 25 26 static const TypeInfo ssi_bus_info = { 27 .name = TYPE_SSI_BUS, 28 .parent = TYPE_BUS, 29 .instance_size = sizeof(SSIBus), 30 }; 31 32 static void ssi_cs_default(void *opaque, int n, int level) 33 { 34 SSISlave *s = SSI_SLAVE(opaque); 35 bool cs = !!level; 36 assert(n == 0); 37 if (s->cs != cs) { 38 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s); 39 if (ssc->set_cs) { 40 ssc->set_cs(s, cs); 41 } 42 } 43 s->cs = cs; 44 } 45 46 static uint32_t ssi_transfer_raw_default(SSISlave *dev, uint32_t val) 47 { 48 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(dev); 49 50 if ((dev->cs && ssc->cs_polarity == SSI_CS_HIGH) || 51 (!dev->cs && ssc->cs_polarity == SSI_CS_LOW) || 52 ssc->cs_polarity == SSI_CS_NONE) { 53 return ssc->transfer(dev, val); 54 } 55 return 0; 56 } 57 58 static void ssi_slave_realize(DeviceState *dev, Error **errp) 59 { 60 SSISlave *s = SSI_SLAVE(dev); 61 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s); 62 63 if (ssc->transfer_raw == ssi_transfer_raw_default && 64 ssc->cs_polarity != SSI_CS_NONE) { 65 qdev_init_gpio_in_named(dev, ssi_cs_default, SSI_GPIO_CS, 1); 66 } 67 68 ssc->realize(s, errp); 69 } 70 71 static void ssi_slave_class_init(ObjectClass *klass, void *data) 72 { 73 SSISlaveClass *ssc = SSI_SLAVE_CLASS(klass); 74 DeviceClass *dc = DEVICE_CLASS(klass); 75 76 dc->realize = ssi_slave_realize; 77 dc->bus_type = TYPE_SSI_BUS; 78 if (!ssc->transfer_raw) { 79 ssc->transfer_raw = ssi_transfer_raw_default; 80 } 81 } 82 83 static const TypeInfo ssi_slave_info = { 84 .name = TYPE_SSI_SLAVE, 85 .parent = TYPE_DEVICE, 86 .class_init = ssi_slave_class_init, 87 .class_size = sizeof(SSISlaveClass), 88 .abstract = true, 89 }; 90 91 DeviceState *ssi_create_slave_no_init(SSIBus *bus, const char *name) 92 { 93 return qdev_create(BUS(bus), name); 94 } 95 96 DeviceState *ssi_create_slave(SSIBus *bus, const char *name) 97 { 98 DeviceState *dev = ssi_create_slave_no_init(bus, name); 99 100 qdev_init_nofail(dev); 101 return dev; 102 } 103 104 SSIBus *ssi_create_bus(DeviceState *parent, const char *name) 105 { 106 BusState *bus; 107 bus = qbus_create(TYPE_SSI_BUS, parent, name); 108 return SSI_BUS(bus); 109 } 110 111 uint32_t ssi_transfer(SSIBus *bus, uint32_t val) 112 { 113 BusState *b = BUS(bus); 114 BusChild *kid; 115 SSISlaveClass *ssc; 116 uint32_t r = 0; 117 118 QTAILQ_FOREACH(kid, &b->children, sibling) { 119 SSISlave *slave = SSI_SLAVE(kid->child); 120 ssc = SSI_SLAVE_GET_CLASS(slave); 121 r |= ssc->transfer_raw(slave, val); 122 } 123 124 return r; 125 } 126 127 const VMStateDescription vmstate_ssi_slave = { 128 .name = "SSISlave", 129 .version_id = 1, 130 .minimum_version_id = 1, 131 .fields = (VMStateField[]) { 132 VMSTATE_BOOL(cs, SSISlave), 133 VMSTATE_END_OF_LIST() 134 } 135 }; 136 137 static void ssi_slave_register_types(void) 138 { 139 type_register_static(&ssi_bus_info); 140 type_register_static(&ssi_slave_info); 141 } 142 143 type_init(ssi_slave_register_types) 144 145 typedef struct SSIAutoConnectArg { 146 qemu_irq **cs_linep; 147 SSIBus *bus; 148 } SSIAutoConnectArg; 149 150 static int ssi_auto_connect_slave(Object *child, void *opaque) 151 { 152 SSIAutoConnectArg *arg = opaque; 153 SSISlave *dev = (SSISlave *)object_dynamic_cast(child, TYPE_SSI_SLAVE); 154 qemu_irq cs_line; 155 156 if (!dev) { 157 return 0; 158 } 159 160 cs_line = qdev_get_gpio_in_named(DEVICE(dev), SSI_GPIO_CS, 0); 161 qdev_set_parent_bus(DEVICE(dev), BUS(arg->bus)); 162 **arg->cs_linep = cs_line; 163 (*arg->cs_linep)++; 164 return 0; 165 } 166 167 void ssi_auto_connect_slaves(DeviceState *parent, qemu_irq *cs_line, 168 SSIBus *bus) 169 { 170 SSIAutoConnectArg arg = { 171 .cs_linep = &cs_line, 172 .bus = bus 173 }; 174 175 object_child_foreach(OBJECT(parent), ssi_auto_connect_slave, &arg); 176 } 177