xref: /openbmc/qemu/include/hw/ssi/ssi.h (revision 60efffa4)
1 /* QEMU Synchronous Serial Interface support.  */
2 
3 /* In principle SSI is a point-point interface.  As such the qemu
4    implementation has a single slave device on a "bus".
5    However it is fairly common for boards to have multiple slaves
6    connected to a single master, and select devices with an external
7    chip select.  This is implemented in qemu by having an explicit mux device.
8    It is assumed that master and slave are both using the same transfer width.
9    */
10 
11 #ifndef QEMU_SSI_H
12 #define QEMU_SSI_H
13 
14 #include "hw/qdev-core.h"
15 #include "qom/object.h"
16 
17 typedef enum SSICSMode SSICSMode;
18 
19 #define TYPE_SSI_SLAVE "ssi-slave"
20 OBJECT_DECLARE_TYPE(SSISlave, SSISlaveClass,
21                     ssi_slave, SSI_SLAVE)
22 
23 #define SSI_GPIO_CS "ssi-gpio-cs"
24 
25 enum SSICSMode {
26     SSI_CS_NONE = 0,
27     SSI_CS_LOW,
28     SSI_CS_HIGH,
29 };
30 
31 /* Slave devices.  */
32 struct SSISlaveClass {
33     DeviceClass parent_class;
34 
35     void (*realize)(SSISlave *dev, Error **errp);
36 
37     /* if you have standard or no CS behaviour, just override transfer.
38      * This is called when the device cs is active (true by default).
39      */
40     uint32_t (*transfer)(SSISlave *dev, uint32_t val);
41     /* called when the CS line changes. Optional, devices only need to implement
42      * this if they have side effects associated with the cs line (beyond
43      * tristating the txrx lines).
44      */
45     int (*set_cs)(SSISlave *dev, bool select);
46     /* define whether or not CS exists and is active low/high */
47     SSICSMode cs_polarity;
48 
49     /* if you have non-standard CS behaviour override this to take control
50      * of the CS behaviour at the device level. transfer, set_cs, and
51      * cs_polarity are unused if this is overwritten. Transfer_raw will
52      * always be called for the device for every txrx access to the parent bus
53      */
54     uint32_t (*transfer_raw)(SSISlave *dev, uint32_t val);
55 };
56 
57 struct SSISlave {
58     DeviceState parent_obj;
59 
60     /* Chip select state */
61     bool cs;
62 };
63 
64 extern const VMStateDescription vmstate_ssi_slave;
65 
66 #define VMSTATE_SSI_SLAVE(_field, _state) {                          \
67     .name       = (stringify(_field)),                               \
68     .size       = sizeof(SSISlave),                                  \
69     .vmsd       = &vmstate_ssi_slave,                                \
70     .flags      = VMS_STRUCT,                                        \
71     .offset     = vmstate_offset_value(_state, _field, SSISlave),    \
72 }
73 
74 DeviceState *ssi_create_slave(SSIBus *bus, const char *name);
75 /**
76  * ssi_realize_and_unref: realize and unref an SSI slave device
77  * @dev: SSI slave device to realize
78  * @bus: SSI bus to put it on
79  * @errp: error pointer
80  *
81  * Call 'realize' on @dev, put it on the specified @bus, and drop the
82  * reference to it. Errors are reported via @errp and by returning
83  * false.
84  *
85  * This function is useful if you have created @dev via qdev_new()
86  * (which takes a reference to the device it returns to you), so that
87  * you can set properties on it before realizing it. If you don't need
88  * to set properties then ssi_create_slave() is probably better (as it
89  * does the create, init and realize in one step).
90  *
91  * If you are embedding the SSI slave into another QOM device and
92  * initialized it via some variant on object_initialize_child() then
93  * do not use this function, because that family of functions arrange
94  * for the only reference to the child device to be held by the parent
95  * via the child<> property, and so the reference-count-drop done here
96  * would be incorrect.  (Instead you would want ssi_realize(), which
97  * doesn't currently exist but would be trivial to create if we had
98  * any code that wanted it.)
99  */
100 bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bus, Error **errp);
101 
102 /* Master interface.  */
103 SSIBus *ssi_create_bus(DeviceState *parent, const char *name);
104 
105 uint32_t ssi_transfer(SSIBus *bus, uint32_t val);
106 
107 #endif
108