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