2 * QEMU Synchronous Serial Interface support
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
9 * This code is licensed under the GNU GPL v2.
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.
15 #include "qemu/osdep.h"
16 #include "hw/ssi/ssi.h"
17 #include "migration/vmstate.h"
18 #include "qemu/module.h"
19 #include "qapi/error.h"
25 #define TYPE_SSI_BUS "SSI"
26 #define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS)
28 static const TypeInfo ssi_bus_info
= {
31 .instance_size
= sizeof(SSIBus
),
34 static void ssi_cs_default(void *opaque
, int n
, int level
)
36 SSISlave
*s
= SSI_SLAVE(opaque
);
40 SSISlaveClass
*ssc
= SSI_SLAVE_GET_CLASS(s
);
48 static uint32_t ssi_transfer_raw_default(SSISlave
*dev
, uint32_t val
)
50 SSISlaveClass
*ssc
= SSI_SLAVE_GET_CLASS(dev
);
52 if ((dev
->cs
&& ssc
->cs_polarity
== SSI_CS_HIGH
) ||
53 (!dev
->cs
&& ssc
->cs_polarity
== SSI_CS_LOW
) ||
54 ssc
->cs_polarity
== SSI_CS_NONE
) {
55 return ssc
->transfer(dev
, val
);
60 static void ssi_slave_realize(DeviceState
*dev
, Error
**errp
)
62 SSISlave
*s
= SSI_SLAVE(dev
);
63 SSISlaveClass
*ssc
= SSI_SLAVE_GET_CLASS(s
);
65 if (ssc
->transfer_raw
== ssi_transfer_raw_default
&&
66 ssc
->cs_polarity
!= SSI_CS_NONE
) {
67 qdev_init_gpio_in_named(dev
, ssi_cs_default
, SSI_GPIO_CS
, 1);
70 ssc
->realize(s
, errp
);
73 static void ssi_slave_class_init(ObjectClass
*klass
, void *data
)
75 SSISlaveClass
*ssc
= SSI_SLAVE_CLASS(klass
);
76 DeviceClass
*dc
= DEVICE_CLASS(klass
);
78 dc
->realize
= ssi_slave_realize
;
79 dc
->bus_type
= TYPE_SSI_BUS
;
80 if (!ssc
->transfer_raw
) {
81 ssc
->transfer_raw
= ssi_transfer_raw_default
;
85 static const TypeInfo ssi_slave_info
= {
86 .name
= TYPE_SSI_SLAVE
,
87 .parent
= TYPE_DEVICE
,
88 .class_init
= ssi_slave_class_init
,
89 .class_size
= sizeof(SSISlaveClass
),
93 DeviceState
*ssi_create_slave(SSIBus
*bus
, const char *name
)
95 DeviceState
*dev
= qdev_new(name
);
97 qdev_realize_and_unref(dev
, &bus
->parent_obj
, &error_fatal
);
101 SSIBus
*ssi_create_bus(DeviceState
*parent
, const char *name
)
104 bus
= qbus_create(TYPE_SSI_BUS
, parent
, name
);
108 uint32_t ssi_transfer(SSIBus
*bus
, uint32_t val
)
110 BusState
*b
= BUS(bus
);
115 QTAILQ_FOREACH(kid
, &b
->children
, sibling
) {
116 SSISlave
*slave
= SSI_SLAVE(kid
->child
);
117 ssc
= SSI_SLAVE_GET_CLASS(slave
);
118 r
|= ssc
->transfer_raw(slave
, val
);
124 const VMStateDescription vmstate_ssi_slave
= {
127 .minimum_version_id
= 1,
128 .fields
= (VMStateField
[]) {
129 VMSTATE_BOOL(cs
, SSISlave
),
130 VMSTATE_END_OF_LIST()
134 static void ssi_slave_register_types(void)
136 type_register_static(&ssi_bus_info
);
137 type_register_static(&ssi_slave_info
);
140 type_init(ssi_slave_register_types
)