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.
21 #define TYPE_SSI_BUS "SSI"
22 #define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS)
24 static const TypeInfo ssi_bus_info
= {
27 .instance_size
= sizeof(SSIBus
),
30 static void ssi_cs_default(void *opaque
, int n
, int level
)
32 SSISlave
*s
= SSI_SLAVE(opaque
);
36 SSISlaveClass
*ssc
= SSI_SLAVE_GET_CLASS(s
);
44 static uint32_t ssi_transfer_raw_default(SSISlave
*dev
, uint32_t val
)
46 SSISlaveClass
*ssc
= SSI_SLAVE_GET_CLASS(dev
);
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
);
56 static int ssi_slave_init(DeviceState
*dev
)
58 SSISlave
*s
= SSI_SLAVE(dev
);
59 SSISlaveClass
*ssc
= SSI_SLAVE_GET_CLASS(s
);
61 if (ssc
->transfer_raw
== ssi_transfer_raw_default
&&
62 ssc
->cs_polarity
!= SSI_CS_NONE
) {
63 qdev_init_gpio_in_named(dev
, ssi_cs_default
, SSI_GPIO_CS
, 1);
69 static void ssi_slave_class_init(ObjectClass
*klass
, void *data
)
71 SSISlaveClass
*ssc
= SSI_SLAVE_CLASS(klass
);
72 DeviceClass
*dc
= DEVICE_CLASS(klass
);
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
;
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
),
89 DeviceState
*ssi_create_slave_no_init(SSIBus
*bus
, const char *name
)
91 return qdev_create(BUS(bus
), name
);
94 DeviceState
*ssi_create_slave(SSIBus
*bus
, const char *name
)
96 DeviceState
*dev
= ssi_create_slave_no_init(bus
, name
);
98 qdev_init_nofail(dev
);
102 SSIBus
*ssi_create_bus(DeviceState
*parent
, const char *name
)
105 bus
= qbus_create(TYPE_SSI_BUS
, parent
, name
);
109 uint32_t ssi_transfer(SSIBus
*bus
, uint32_t val
)
111 BusState
*b
= BUS(bus
);
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
);
125 const VMStateDescription vmstate_ssi_slave
= {
128 .minimum_version_id
= 1,
129 .fields
= (VMStateField
[]) {
130 VMSTATE_BOOL(cs
, SSISlave
),
131 VMSTATE_END_OF_LIST()
135 static void ssi_slave_register_types(void)
137 type_register_static(&ssi_bus_info
);
138 type_register_static(&ssi_slave_info
);
141 type_init(ssi_slave_register_types
)
143 typedef struct SSIAutoConnectArg
{
148 static int ssi_auto_connect_slave(Object
*child
, void *opaque
)
150 SSIAutoConnectArg
*arg
= opaque
;
151 SSISlave
*dev
= (SSISlave
*)object_dynamic_cast(child
, TYPE_SSI_SLAVE
);
158 cs_line
= qdev_get_gpio_in_named(DEVICE(dev
), SSI_GPIO_CS
, 0);
159 qdev_set_parent_bus(DEVICE(dev
), BUS(arg
->bus
));
160 **arg
->cs_linep
= cs_line
;
165 void ssi_auto_connect_slaves(DeviceState
*parent
, qemu_irq
*cs_line
,
168 SSIAutoConnectArg arg
= {
169 .cs_linep
= &cs_line
,
173 object_child_foreach(OBJECT(parent
), ssi_auto_connect_slave
, &arg
);