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"
22 #define TYPE_SSI_BUS "SSI"
23 #define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS)
25 static const TypeInfo ssi_bus_info
= {
28 .instance_size
= sizeof(SSIBus
),
31 static void ssi_cs_default(void *opaque
, int n
, int level
)
33 SSISlave
*s
= SSI_SLAVE(opaque
);
37 SSISlaveClass
*ssc
= SSI_SLAVE_GET_CLASS(s
);
45 static uint32_t ssi_transfer_raw_default(SSISlave
*dev
, uint32_t val
)
47 SSISlaveClass
*ssc
= SSI_SLAVE_GET_CLASS(dev
);
49 if ((dev
->cs
&& ssc
->cs_polarity
== SSI_CS_HIGH
) ||
50 (!dev
->cs
&& ssc
->cs_polarity
== SSI_CS_LOW
) ||
51 ssc
->cs_polarity
== SSI_CS_NONE
) {
52 return ssc
->transfer(dev
, val
);
57 static void ssi_slave_realize(DeviceState
*dev
, Error
**errp
)
59 SSISlave
*s
= SSI_SLAVE(dev
);
60 SSISlaveClass
*ssc
= SSI_SLAVE_GET_CLASS(s
);
62 if (ssc
->transfer_raw
== ssi_transfer_raw_default
&&
63 ssc
->cs_polarity
!= SSI_CS_NONE
) {
64 qdev_init_gpio_in_named(dev
, ssi_cs_default
, SSI_GPIO_CS
, 1);
67 ssc
->realize(s
, errp
);
70 static void ssi_slave_class_init(ObjectClass
*klass
, void *data
)
72 SSISlaveClass
*ssc
= SSI_SLAVE_CLASS(klass
);
73 DeviceClass
*dc
= DEVICE_CLASS(klass
);
75 dc
->realize
= ssi_slave_realize
;
76 dc
->bus_type
= TYPE_SSI_BUS
;
77 if (!ssc
->transfer_raw
) {
78 ssc
->transfer_raw
= ssi_transfer_raw_default
;
82 static const TypeInfo ssi_slave_info
= {
83 .name
= TYPE_SSI_SLAVE
,
84 .parent
= TYPE_DEVICE
,
85 .class_init
= ssi_slave_class_init
,
86 .class_size
= sizeof(SSISlaveClass
),
90 DeviceState
*ssi_create_slave_no_init(SSIBus
*bus
, const char *name
)
92 return qdev_create(BUS(bus
), name
);
95 DeviceState
*ssi_create_slave(SSIBus
*bus
, const char *name
)
97 DeviceState
*dev
= ssi_create_slave_no_init(bus
, name
);
99 qdev_init_nofail(dev
);
103 SSIBus
*ssi_create_bus(DeviceState
*parent
, const char *name
)
106 bus
= qbus_create(TYPE_SSI_BUS
, parent
, name
);
110 uint32_t ssi_transfer(SSIBus
*bus
, uint32_t val
)
112 BusState
*b
= BUS(bus
);
117 QTAILQ_FOREACH(kid
, &b
->children
, sibling
) {
118 SSISlave
*slave
= SSI_SLAVE(kid
->child
);
119 ssc
= SSI_SLAVE_GET_CLASS(slave
);
120 r
|= ssc
->transfer_raw(slave
, val
);
126 const VMStateDescription vmstate_ssi_slave
= {
129 .minimum_version_id
= 1,
130 .fields
= (VMStateField
[]) {
131 VMSTATE_BOOL(cs
, SSISlave
),
132 VMSTATE_END_OF_LIST()
136 static void ssi_slave_register_types(void)
138 type_register_static(&ssi_bus_info
);
139 type_register_static(&ssi_slave_info
);
142 type_init(ssi_slave_register_types
)
144 typedef struct SSIAutoConnectArg
{
149 static int ssi_auto_connect_slave(Object
*child
, void *opaque
)
151 SSIAutoConnectArg
*arg
= opaque
;
152 SSISlave
*dev
= (SSISlave
*)object_dynamic_cast(child
, TYPE_SSI_SLAVE
);
159 cs_line
= qdev_get_gpio_in_named(DEVICE(dev
), SSI_GPIO_CS
, 0);
160 qdev_set_parent_bus(DEVICE(dev
), BUS(arg
->bus
));
161 **arg
->cs_linep
= cs_line
;
166 void ssi_auto_connect_slaves(DeviceState
*parent
, qemu_irq
*cs_line
,
169 SSIAutoConnectArg arg
= {
170 .cs_linep
= &cs_line
,
174 object_child_foreach(OBJECT(parent
), ssi_auto_connect_slave
, &arg
);