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