xref: /qemu/include/hw/ssi/ssi.h (revision 912a9efd6bf4d808b238e17d26de2e4bb9bc4743)
190d37239SPaul Brook /* QEMU Synchronous Serial Interface support.  */
290d37239SPaul Brook 
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.
1190d37239SPaul Brook  */
1290d37239SPaul Brook 
1390d37239SPaul Brook #ifndef QEMU_SSI_H
1490d37239SPaul Brook #define QEMU_SSI_H
1590d37239SPaul Brook 
16a27bd6c7SMarkus Armbruster #include "hw/qdev-core.h"
17db1015e9SEduardo Habkost #include "qom/object.h"
1890d37239SPaul Brook 
198fd06719SAlistair Francis typedef enum SSICSMode SSICSMode;
2090d37239SPaul Brook 
21ec7e429bSPhilippe Mathieu-Daudé #define TYPE_SSI_PERIPHERAL "ssi-peripheral"
22ec7e429bSPhilippe Mathieu-Daudé OBJECT_DECLARE_TYPE(SSIPeripheral, SSIPeripheralClass,
23ec7e429bSPhilippe Mathieu-Daudé                     SSI_PERIPHERAL)
24cd6c4cf2SAnthony Liguori 
25de77914eSPeter Crosthwaite #define SSI_GPIO_CS "ssi-gpio-cs"
26de77914eSPeter Crosthwaite 
278fd06719SAlistair Francis enum SSICSMode {
2866530953SPeter A. G. Crosthwaite     SSI_CS_NONE = 0,
2966530953SPeter A. G. Crosthwaite     SSI_CS_LOW,
3066530953SPeter A. G. Crosthwaite     SSI_CS_HIGH,
318fd06719SAlistair Francis };
3266530953SPeter A. G. Crosthwaite 
33ec7e429bSPhilippe Mathieu-Daudé /* Peripherals.  */
34ec7e429bSPhilippe Mathieu-Daudé struct SSIPeripheralClass {
35cd6c4cf2SAnthony Liguori     DeviceClass parent_class;
36cd6c4cf2SAnthony Liguori 
37ec7e429bSPhilippe Mathieu-Daudé     void (*realize)(SSIPeripheral *dev, Error **errp);
3866530953SPeter A. G. Crosthwaite 
3966530953SPeter A. G. Crosthwaite     /* if you have standard or no CS behaviour, just override transfer.
4066530953SPeter A. G. Crosthwaite      * This is called when the device cs is active (true by default).
4166530953SPeter A. G. Crosthwaite      */
42ec7e429bSPhilippe Mathieu-Daudé     uint32_t (*transfer)(SSIPeripheral *dev, uint32_t val);
4366530953SPeter A. G. Crosthwaite     /* called when the CS line changes. Optional, devices only need to implement
4466530953SPeter A. G. Crosthwaite      * this if they have side effects associated with the cs line (beyond
4566530953SPeter A. G. Crosthwaite      * tristating the txrx lines).
4666530953SPeter A. G. Crosthwaite      */
47ec7e429bSPhilippe Mathieu-Daudé     int (*set_cs)(SSIPeripheral *dev, bool select);
4866530953SPeter A. G. Crosthwaite     /* define whether or not CS exists and is active low/high */
4966530953SPeter A. G. Crosthwaite     SSICSMode cs_polarity;
5066530953SPeter A. G. Crosthwaite 
5166530953SPeter A. G. Crosthwaite     /* if you have non-standard CS behaviour override this to take control
5266530953SPeter A. G. Crosthwaite      * of the CS behaviour at the device level. transfer, set_cs, and
5366530953SPeter A. G. Crosthwaite      * cs_polarity are unused if this is overwritten. Transfer_raw will
5466530953SPeter A. G. Crosthwaite      * always be called for the device for every txrx access to the parent bus
5566530953SPeter A. G. Crosthwaite      */
56ec7e429bSPhilippe Mathieu-Daudé     uint32_t (*transfer_raw)(SSIPeripheral *dev, uint32_t val);
578fd06719SAlistair Francis };
5890d37239SPaul Brook 
59ec7e429bSPhilippe Mathieu-Daudé struct SSIPeripheral {
601f760d5fSPeter Crosthwaite     DeviceState parent_obj;
6166530953SPeter A. G. Crosthwaite 
62db96605aSAlex Bennée     /* cache the class */
63db96605aSAlex Bennée     SSIPeripheralClass *spc;
64db96605aSAlex Bennée 
6566530953SPeter A. G. Crosthwaite     /* Chip select state */
6666530953SPeter A. G. Crosthwaite     bool cs;
67243975c0SCédric Le Goater 
68243975c0SCédric Le Goater     /* Chip select index */
69243975c0SCédric Le Goater     uint8_t cs_index;
7090d37239SPaul Brook };
7190d37239SPaul Brook 
72ec7e429bSPhilippe Mathieu-Daudé extern const VMStateDescription vmstate_ssi_peripheral;
7366530953SPeter A. G. Crosthwaite 
74ec7e429bSPhilippe Mathieu-Daudé #define VMSTATE_SSI_PERIPHERAL(_field, _state) {                     \
7566530953SPeter A. G. Crosthwaite     .name       = (stringify(_field)),                               \
76ec7e429bSPhilippe Mathieu-Daudé     .size       = sizeof(SSIPeripheral),                             \
77ec7e429bSPhilippe Mathieu-Daudé     .vmsd       = &vmstate_ssi_peripheral,                           \
7866530953SPeter A. G. Crosthwaite     .flags      = VMS_STRUCT,                                        \
79ec7e429bSPhilippe Mathieu-Daudé     .offset     = vmstate_offset_value(_state, _field, SSIPeripheral), \
8066530953SPeter A. G. Crosthwaite }
8166530953SPeter A. G. Crosthwaite 
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);
10990d37239SPaul Brook 
11090d37239SPaul Brook /* Master interface.  */
11102e2da45SPaul Brook SSIBus *ssi_create_bus(DeviceState *parent, const char *name);
11290d37239SPaul Brook 
11390d37239SPaul Brook uint32_t ssi_transfer(SSIBus *bus, uint32_t val);
11490d37239SPaul Brook 
115*8a211fa3SCédric Le Goater DeviceState *ssi_get_cs(SSIBus *bus, uint8_t cs_index);
116*8a211fa3SCédric Le Goater 
11790d37239SPaul Brook #endif
118