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"
24 #define TYPE_SSI_BUS "SSI"
25 #define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS)
27 static const TypeInfo ssi_bus_info
= {
30 .instance_size
= sizeof(SSIBus
),
33 static void ssi_cs_default(void *opaque
, int n
, int level
)
35 SSISlave
*s
= SSI_SLAVE(opaque
);
39 SSISlaveClass
*ssc
= SSI_SLAVE_GET_CLASS(s
);
47 static uint32_t ssi_transfer_raw_default(SSISlave
*dev
, uint32_t val
)
49 SSISlaveClass
*ssc
= SSI_SLAVE_GET_CLASS(dev
);
51 if ((dev
->cs
&& ssc
->cs_polarity
== SSI_CS_HIGH
) ||
52 (!dev
->cs
&& ssc
->cs_polarity
== SSI_CS_LOW
) ||
53 ssc
->cs_polarity
== SSI_CS_NONE
) {
54 return ssc
->transfer(dev
, val
);
59 static void ssi_slave_realize(DeviceState
*dev
, Error
**errp
)
61 SSISlave
*s
= SSI_SLAVE(dev
);
62 SSISlaveClass
*ssc
= SSI_SLAVE_GET_CLASS(s
);
64 if (ssc
->transfer_raw
== ssi_transfer_raw_default
&&
65 ssc
->cs_polarity
!= SSI_CS_NONE
) {
66 qdev_init_gpio_in_named(dev
, ssi_cs_default
, SSI_GPIO_CS
, 1);
69 ssc
->realize(s
, errp
);
72 static void ssi_slave_class_init(ObjectClass
*klass
, void *data
)
74 SSISlaveClass
*ssc
= SSI_SLAVE_CLASS(klass
);
75 DeviceClass
*dc
= DEVICE_CLASS(klass
);
77 dc
->realize
= ssi_slave_realize
;
78 dc
->bus_type
= TYPE_SSI_BUS
;
79 if (!ssc
->transfer_raw
) {
80 ssc
->transfer_raw
= ssi_transfer_raw_default
;
84 static const TypeInfo ssi_slave_info
= {
85 .name
= TYPE_SSI_SLAVE
,
86 .parent
= TYPE_DEVICE
,
87 .class_init
= ssi_slave_class_init
,
88 .class_size
= sizeof(SSISlaveClass
),
92 DeviceState
*ssi_create_slave_no_init(SSIBus
*bus
, const char *name
)
94 return qdev_create(BUS(bus
), name
);
97 DeviceState
*ssi_create_slave(SSIBus
*bus
, const char *name
)
99 DeviceState
*dev
= ssi_create_slave_no_init(bus
, name
);
101 qdev_init_nofail(dev
);
105 SSIBus
*ssi_create_bus(DeviceState
*parent
, const char *name
)
108 bus
= qbus_create(TYPE_SSI_BUS
, parent
, name
);
112 uint32_t ssi_transfer(SSIBus
*bus
, uint32_t val
)
114 BusState
*b
= BUS(bus
);
119 QTAILQ_FOREACH(kid
, &b
->children
, sibling
) {
120 SSISlave
*slave
= SSI_SLAVE(kid
->child
);
121 ssc
= SSI_SLAVE_GET_CLASS(slave
);
122 r
|= ssc
->transfer_raw(slave
, val
);
128 const VMStateDescription vmstate_ssi_slave
= {
131 .minimum_version_id
= 1,
132 .fields
= (VMStateField
[]) {
133 VMSTATE_BOOL(cs
, SSISlave
),
134 VMSTATE_END_OF_LIST()
138 static void ssi_slave_register_types(void)
140 type_register_static(&ssi_bus_info
);
141 type_register_static(&ssi_slave_info
);
144 type_init(ssi_slave_register_types
)
146 typedef struct SSIAutoConnectArg
{
151 static int ssi_auto_connect_slave(Object
*child
, void *opaque
)
153 SSIAutoConnectArg
*arg
= opaque
;
154 SSISlave
*dev
= (SSISlave
*)object_dynamic_cast(child
, TYPE_SSI_SLAVE
);
161 cs_line
= qdev_get_gpio_in_named(DEVICE(dev
), SSI_GPIO_CS
, 0);
162 qdev_set_parent_bus(DEVICE(dev
), BUS(arg
->bus
));
163 **arg
->cs_linep
= cs_line
;
168 void ssi_auto_connect_slaves(DeviceState
*parent
, qemu_irq
*cs_line
,
171 SSIAutoConnectArg arg
= {
172 .cs_linep
= &cs_line
,
176 object_child_foreach(OBJECT(parent
), ssi_auto_connect_slave
, &arg
);