xref: /openbmc/qemu/include/hw/ssi/ssi.h (revision b21f12d6b68a55bc11e86e81d802d84288ec903b)
18fd06719SAlistair Francis /* QEMU Synchronous Serial Interface support.  */
28fd06719SAlistair Francis 
39ce89a22SPhilippe Mathieu-Daudé /*
49ce89a22SPhilippe Mathieu-Daudé  * In principle SSI is a point-point interface.  As such the qemu
5ec7e429bSPhilippe Mathieu-Daudé  * implementation has a single peripheral on a "bus".
6ec7e429bSPhilippe Mathieu-Daudé  * However it is fairly common for boards to have multiple peripherals
79ce89a22SPhilippe Mathieu-Daudé  * connected to a single master, and select devices with an external
89ce89a22SPhilippe Mathieu-Daudé  * chip select.  This is implemented in qemu by having an explicit mux device.
9ec7e429bSPhilippe Mathieu-Daudé  * It is assumed that master and peripheral are both using the same transfer
109ce89a22SPhilippe Mathieu-Daudé  * width.
118fd06719SAlistair Francis  */
128fd06719SAlistair Francis 
138fd06719SAlistair Francis #ifndef QEMU_SSI_H
148fd06719SAlistair Francis #define QEMU_SSI_H
158fd06719SAlistair Francis 
16a27bd6c7SMarkus Armbruster #include "hw/qdev-core.h"
17db1015e9SEduardo Habkost #include "qom/object.h"
188fd06719SAlistair Francis 
198fd06719SAlistair Francis typedef enum SSICSMode SSICSMode;
208fd06719SAlistair Francis 
21ec7e429bSPhilippe Mathieu-Daudé #define TYPE_SSI_PERIPHERAL "ssi-peripheral"
22ec7e429bSPhilippe Mathieu-Daudé OBJECT_DECLARE_TYPE(SSIPeripheral, SSIPeripheralClass,
23ec7e429bSPhilippe Mathieu-Daudé                     SSI_PERIPHERAL)
248fd06719SAlistair Francis 
258fd06719SAlistair Francis #define SSI_GPIO_CS "ssi-gpio-cs"
268fd06719SAlistair Francis 
278fd06719SAlistair Francis enum SSICSMode {
288fd06719SAlistair Francis     SSI_CS_NONE = 0,
298fd06719SAlistair Francis     SSI_CS_LOW,
308fd06719SAlistair Francis     SSI_CS_HIGH,
318fd06719SAlistair Francis };
328fd06719SAlistair Francis 
33ec7e429bSPhilippe Mathieu-Daudé /* Peripherals.  */
34ec7e429bSPhilippe Mathieu-Daudé struct SSIPeripheralClass {
358fd06719SAlistair Francis     DeviceClass parent_class;
368fd06719SAlistair Francis 
37ec7e429bSPhilippe Mathieu-Daudé     void (*realize)(SSIPeripheral *dev, Error **errp);
388fd06719SAlistair Francis 
398fd06719SAlistair Francis     /* if you have standard or no CS behaviour, just override transfer.
408fd06719SAlistair Francis      * This is called when the device cs is active (true by default).
418fd06719SAlistair Francis      */
42ec7e429bSPhilippe Mathieu-Daudé     uint32_t (*transfer)(SSIPeripheral *dev, uint32_t val);
438fd06719SAlistair Francis     /* called when the CS line changes. Optional, devices only need to implement
448fd06719SAlistair Francis      * this if they have side effects associated with the cs line (beyond
458fd06719SAlistair Francis      * tristating the txrx lines).
468fd06719SAlistair Francis      */
47ec7e429bSPhilippe Mathieu-Daudé     int (*set_cs)(SSIPeripheral *dev, bool select);
488fd06719SAlistair Francis     /* define whether or not CS exists and is active low/high */
498fd06719SAlistair Francis     SSICSMode cs_polarity;
508fd06719SAlistair Francis 
518fd06719SAlistair Francis     /* if you have non-standard CS behaviour override this to take control
528fd06719SAlistair Francis      * of the CS behaviour at the device level. transfer, set_cs, and
538fd06719SAlistair Francis      * cs_polarity are unused if this is overwritten. Transfer_raw will
548fd06719SAlistair Francis      * always be called for the device for every txrx access to the parent bus
558fd06719SAlistair Francis      */
56ec7e429bSPhilippe Mathieu-Daudé     uint32_t (*transfer_raw)(SSIPeripheral *dev, uint32_t val);
578fd06719SAlistair Francis };
588fd06719SAlistair Francis 
59ec7e429bSPhilippe Mathieu-Daudé struct SSIPeripheral {
608fd06719SAlistair Francis     DeviceState parent_obj;
618fd06719SAlistair Francis 
62db96605aSAlex Bennée     /* cache the class */
63db96605aSAlex Bennée     SSIPeripheralClass *spc;
64db96605aSAlex Bennée 
658fd06719SAlistair Francis     /* Chip select state */
668fd06719SAlistair Francis     bool cs;
67c2f4d7a2SCédric Le Goater 
68c2f4d7a2SCédric Le Goater     /* Chip select index */
69c2f4d7a2SCédric Le Goater     uint8_t cs_index;
708fd06719SAlistair Francis };
718fd06719SAlistair Francis 
72ec7e429bSPhilippe Mathieu-Daudé extern const VMStateDescription vmstate_ssi_peripheral;
738fd06719SAlistair Francis 
74ec7e429bSPhilippe Mathieu-Daudé #define VMSTATE_SSI_PERIPHERAL(_field, _state) {                     \
758fd06719SAlistair Francis     .name       = (stringify(_field)),                               \
76ec7e429bSPhilippe Mathieu-Daudé     .size       = sizeof(SSIPeripheral),                             \
77ec7e429bSPhilippe Mathieu-Daudé     .vmsd       = &vmstate_ssi_peripheral,                           \
788fd06719SAlistair Francis     .flags      = VMS_STRUCT,                                        \
79ec7e429bSPhilippe Mathieu-Daudé     .offset     = vmstate_offset_value(_state, _field, SSIPeripheral), \
808fd06719SAlistair Francis }
818fd06719SAlistair Francis 
82ec7e429bSPhilippe Mathieu-Daudé DeviceState *ssi_create_peripheral(SSIBus *bus, const char *name);
83581e109dSPeter Maydell /**
84ec7e429bSPhilippe Mathieu-Daudé  * ssi_realize_and_unref: realize and unref an SSI peripheral
85ec7e429bSPhilippe Mathieu-Daudé  * @dev: SSI peripheral to realize
86581e109dSPeter Maydell  * @bus: SSI bus to put it on
87581e109dSPeter Maydell  * @errp: error pointer
88581e109dSPeter Maydell  *
89581e109dSPeter Maydell  * Call 'realize' on @dev, put it on the specified @bus, and drop the
90581e109dSPeter Maydell  * reference to it. Errors are reported via @errp and by returning
91581e109dSPeter Maydell  * false.
92581e109dSPeter Maydell  *
93581e109dSPeter Maydell  * This function is useful if you have created @dev via qdev_new()
94581e109dSPeter Maydell  * (which takes a reference to the device it returns to you), so that
95581e109dSPeter Maydell  * you can set properties on it before realizing it. If you don't need
96ec7e429bSPhilippe Mathieu-Daudé  * to set properties then ssi_create_peripheral() is probably better (as it
97581e109dSPeter Maydell  * does the create, init and realize in one step).
98581e109dSPeter Maydell  *
99ec7e429bSPhilippe Mathieu-Daudé  * If you are embedding the SSI peripheral into another QOM device and
100581e109dSPeter Maydell  * initialized it via some variant on object_initialize_child() then
101581e109dSPeter Maydell  * do not use this function, because that family of functions arrange
102581e109dSPeter Maydell  * for the only reference to the child device to be held by the parent
103581e109dSPeter Maydell  * via the child<> property, and so the reference-count-drop done here
104581e109dSPeter Maydell  * would be incorrect.  (Instead you would want ssi_realize(), which
105581e109dSPeter Maydell  * doesn't currently exist but would be trivial to create if we had
106581e109dSPeter Maydell  * any code that wanted it.)
107581e109dSPeter Maydell  */
108581e109dSPeter Maydell bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bus, Error **errp);
1098fd06719SAlistair Francis 
1108fd06719SAlistair Francis /* Master interface.  */
1118fd06719SAlistair Francis SSIBus *ssi_create_bus(DeviceState *parent, const char *name);
1128fd06719SAlistair Francis 
1138fd06719SAlistair Francis uint32_t ssi_transfer(SSIBus *bus, uint32_t val);
1148fd06719SAlistair Francis 
115*b21f12d6SCédric Le Goater DeviceState *ssi_get_cs(SSIBus *bus, uint8_t cs_index);
116*b21f12d6SCédric Le Goater 
1178fd06719SAlistair Francis #endif
118