master
c 195 lines 5.22 KB
Raw
1 /*
2 * QTest I2C driver
3 *
4 * Copyright (c) 2012 Andreas Färber
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
9 #include "qemu/osdep.h"
10 #include "i2c.h"
11
12
13 #include "../libqtest.h"
14
15 enum OMAPI2CRegisters {
16 OMAP_I2C_REV = 0x00,
17 OMAP_I2C_STAT = 0x08,
18 OMAP_I2C_CNT = 0x18,
19 OMAP_I2C_DATA = 0x1c,
20 OMAP_I2C_CON = 0x24,
21 OMAP_I2C_SA = 0x2c,
22 };
23
24 enum OMAPI2CSTATBits {
25 OMAP_I2C_STAT_NACK = 1 << 1,
26 OMAP_I2C_STAT_ARDY = 1 << 2,
27 OMAP_I2C_STAT_RRDY = 1 << 3,
28 OMAP_I2C_STAT_XRDY = 1 << 4,
29 OMAP_I2C_STAT_ROVR = 1 << 11,
30 OMAP_I2C_STAT_SBD = 1 << 15,
31 };
32
33 enum OMAPI2CCONBits {
34 OMAP_I2C_CON_STT = 1 << 0,
35 OMAP_I2C_CON_STP = 1 << 1,
36 OMAP_I2C_CON_TRX = 1 << 9,
37 OMAP_I2C_CON_MST = 1 << 10,
38 OMAP_I2C_CON_BE = 1 << 14,
39 OMAP_I2C_CON_I2C_EN = 1 << 15,
40 };
41
42
43 static void omap_i2c_set_slave_addr(OMAPI2C *s, uint8_t addr)
44 {
45 uint16_t data = addr;
46
47 qtest_writew(s->parent.qts, s->addr + OMAP_I2C_SA, data);
48 data = qtest_readw(s->parent.qts, s->addr + OMAP_I2C_SA);
49 g_assert_cmphex(data, ==, addr);
50 }
51
52 static void omap_i2c_send(I2CAdapter *i2c, uint8_t addr,
53 const uint8_t *buf, uint16_t len)
54 {
55 OMAPI2C *s = container_of(i2c, OMAPI2C, parent);
56 uint16_t data;
57
58 omap_i2c_set_slave_addr(s, addr);
59
60 data = len;
61 qtest_writew(i2c->qts, s->addr + OMAP_I2C_CNT, data);
62
63 data = OMAP_I2C_CON_I2C_EN |
64 OMAP_I2C_CON_TRX |
65 OMAP_I2C_CON_MST |
66 OMAP_I2C_CON_STT |
67 OMAP_I2C_CON_STP;
68 qtest_writew(i2c->qts, s->addr + OMAP_I2C_CON, data);
69 data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CON);
70 g_assert((data & OMAP_I2C_CON_STP) != 0);
71
72 data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
73 g_assert((data & OMAP_I2C_STAT_NACK) == 0);
74
75 while (len > 1) {
76 data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
77 g_assert((data & OMAP_I2C_STAT_XRDY) != 0);
78
79 data = buf[0] | ((uint16_t)buf[1] << 8);
80 qtest_writew(i2c->qts, s->addr + OMAP_I2C_DATA, data);
81 buf = (uint8_t *)buf + 2;
82 len -= 2;
83 }
84 if (len == 1) {
85 data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
86 g_assert((data & OMAP_I2C_STAT_XRDY) != 0);
87
88 data = buf[0];
89 qtest_writew(i2c->qts, s->addr + OMAP_I2C_DATA, data);
90 }
91
92 data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CON);
93 g_assert((data & OMAP_I2C_CON_STP) == 0);
94 }
95
96 static void omap_i2c_recv(I2CAdapter *i2c, uint8_t addr,
97 uint8_t *buf, uint16_t len)
98 {
99 OMAPI2C *s = container_of(i2c, OMAPI2C, parent);
100 uint16_t data, stat;
101 uint16_t orig_len = len;
102
103 omap_i2c_set_slave_addr(s, addr);
104
105 data = len;
106 qtest_writew(i2c->qts, s->addr + OMAP_I2C_CNT, data);
107
108 data = OMAP_I2C_CON_I2C_EN |
109 OMAP_I2C_CON_MST |
110 OMAP_I2C_CON_STT |
111 OMAP_I2C_CON_STP;
112 qtest_writew(i2c->qts, s->addr + OMAP_I2C_CON, data);
113
114 data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
115 g_assert((data & OMAP_I2C_STAT_NACK) == 0);
116
117 while (len > 0) {
118 data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CON);
119 if (len <= 4) {
120 g_assert((data & OMAP_I2C_CON_STP) == 0);
121
122 data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CNT);
123 g_assert_cmpuint(data, ==, orig_len);
124 } else {
125 g_assert((data & OMAP_I2C_CON_STP) != 0);
126
127 data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CNT);
128 g_assert_cmpuint(data, ==, len - 4);
129 }
130
131 data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
132 g_assert((data & OMAP_I2C_STAT_RRDY) != 0);
133 g_assert((data & OMAP_I2C_STAT_ROVR) == 0);
134
135 data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_DATA);
136
137 stat = qtest_readw(i2c->qts, s->addr + OMAP_I2C_STAT);
138
139 if (unlikely(len == 1)) {
140 g_assert((stat & OMAP_I2C_STAT_SBD) != 0);
141
142 buf[0] = data & 0xff;
143 buf++;
144 len--;
145 } else {
146 buf[0] = data & 0xff;
147 buf[1] = data >> 8;
148 buf += 2;
149 len -= 2;
150 }
151 }
152
153 data = qtest_readw(i2c->qts, s->addr + OMAP_I2C_CON);
154 g_assert((data & OMAP_I2C_CON_STP) == 0);
155 }
156
157 static void *omap_i2c_get_driver(void *obj, const char *interface)
158 {
159 OMAPI2C *s = obj;
160 if (!g_strcmp0(interface, "i2c-bus")) {
161 return &s->parent;
162 }
163 fprintf(stderr, "%s not present in omap_i2c\n", interface);
164 g_assert_not_reached();
165 }
166
167 static void omap_i2c_start_hw(QOSGraphObject *object)
168 {
169 OMAPI2C *s = (OMAPI2C *) object;
170 uint16_t data;
171
172 /* verify the mmio address by looking for a known signature */
173 data = qtest_readw(s->parent.qts, s->addr + OMAP_I2C_REV);
174 g_assert_cmphex(data, ==, 0x34);
175 }
176
177 void omap_i2c_init(OMAPI2C *s, QTestState *qts, uint64_t addr)
178 {
179 s->addr = addr;
180
181 s->obj.get_driver = omap_i2c_get_driver;
182 s->obj.start_hw = omap_i2c_start_hw;
183
184 s->parent.send = omap_i2c_send;
185 s->parent.recv = omap_i2c_recv;
186 s->parent.qts = qts;
187 }
188
189 static void omap_i2c_register_nodes(void)
190 {
191 qos_node_create_driver("omap_i2c", NULL);
192 qos_node_produces("omap_i2c", "i2c-bus");
193 }
194
195 libqos_init(omap_i2c_register_nodes);