]> git.proxmox.com Git - qemu.git/blob - tests/libqos/i2c-omap.c
hw/pci-host/versatile.c: Update autodetect to detect newer kernels
[qemu.git] / tests / libqos / i2c-omap.c
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 "libqos/i2c.h"
10
11 #include <glib.h>
12 #include <string.h>
13
14 #include "qemu/osdep.h"
15 #include "qemu/bswap.h"
16 #include "libqtest.h"
17
18 enum OMAPI2CRegisters {
19 OMAP_I2C_REV = 0x00,
20 OMAP_I2C_STAT = 0x08,
21 OMAP_I2C_CNT = 0x18,
22 OMAP_I2C_DATA = 0x1c,
23 OMAP_I2C_CON = 0x24,
24 OMAP_I2C_SA = 0x2c,
25 };
26
27 enum OMAPI2CSTATBits {
28 OMAP_I2C_STAT_NACK = 1 << 1,
29 OMAP_I2C_STAT_ARDY = 1 << 2,
30 OMAP_I2C_STAT_RRDY = 1 << 3,
31 OMAP_I2C_STAT_XRDY = 1 << 4,
32 OMAP_I2C_STAT_ROVR = 1 << 11,
33 OMAP_I2C_STAT_SBD = 1 << 15,
34 };
35
36 enum OMAPI2CCONBits {
37 OMAP_I2C_CON_STT = 1 << 0,
38 OMAP_I2C_CON_STP = 1 << 1,
39 OMAP_I2C_CON_TRX = 1 << 9,
40 OMAP_I2C_CON_MST = 1 << 10,
41 OMAP_I2C_CON_BE = 1 << 14,
42 OMAP_I2C_CON_I2C_EN = 1 << 15,
43 };
44
45 typedef struct OMAPI2C {
46 I2CAdapter parent;
47
48 uint64_t addr;
49 } OMAPI2C;
50
51
52 static void omap_i2c_set_slave_addr(OMAPI2C *s, uint8_t addr)
53 {
54 uint16_t data = addr;
55
56 writew(s->addr + OMAP_I2C_SA, data);
57 data = readw(s->addr + OMAP_I2C_SA);
58 g_assert_cmphex(data, ==, addr);
59 }
60
61 static void omap_i2c_send(I2CAdapter *i2c, uint8_t addr,
62 const uint8_t *buf, uint16_t len)
63 {
64 OMAPI2C *s = (OMAPI2C *)i2c;
65 uint16_t data;
66
67 omap_i2c_set_slave_addr(s, addr);
68
69 data = len;
70 writew(s->addr + OMAP_I2C_CNT, data);
71
72 data = OMAP_I2C_CON_I2C_EN |
73 OMAP_I2C_CON_TRX |
74 OMAP_I2C_CON_MST |
75 OMAP_I2C_CON_STT |
76 OMAP_I2C_CON_STP;
77 writew(s->addr + OMAP_I2C_CON, data);
78 data = readw(s->addr + OMAP_I2C_CON);
79 g_assert((data & OMAP_I2C_CON_STP) != 0);
80
81 data = readw(s->addr + OMAP_I2C_STAT);
82 g_assert((data & OMAP_I2C_STAT_NACK) == 0);
83
84 while (len > 1) {
85 data = readw(s->addr + OMAP_I2C_STAT);
86 g_assert((data & OMAP_I2C_STAT_XRDY) != 0);
87
88 data = buf[0] | ((uint16_t)buf[1] << 8);
89 writew(s->addr + OMAP_I2C_DATA, data);
90 buf = (uint8_t *)buf + 2;
91 len -= 2;
92 }
93 if (len == 1) {
94 data = readw(s->addr + OMAP_I2C_STAT);
95 g_assert((data & OMAP_I2C_STAT_XRDY) != 0);
96
97 data = buf[0];
98 writew(s->addr + OMAP_I2C_DATA, data);
99 }
100
101 data = readw(s->addr + OMAP_I2C_CON);
102 g_assert((data & OMAP_I2C_CON_STP) == 0);
103 }
104
105 static void omap_i2c_recv(I2CAdapter *i2c, uint8_t addr,
106 uint8_t *buf, uint16_t len)
107 {
108 OMAPI2C *s = (OMAPI2C *)i2c;
109 uint16_t data, stat;
110
111 omap_i2c_set_slave_addr(s, addr);
112
113 data = len;
114 writew(s->addr + OMAP_I2C_CNT, data);
115
116 data = OMAP_I2C_CON_I2C_EN |
117 OMAP_I2C_CON_MST |
118 OMAP_I2C_CON_STT |
119 OMAP_I2C_CON_STP;
120 writew(s->addr + OMAP_I2C_CON, data);
121 data = readw(s->addr + OMAP_I2C_CON);
122 g_assert((data & OMAP_I2C_CON_STP) == 0);
123
124 data = readw(s->addr + OMAP_I2C_STAT);
125 g_assert((data & OMAP_I2C_STAT_NACK) == 0);
126
127 data = readw(s->addr + OMAP_I2C_CNT);
128 g_assert_cmpuint(data, ==, len);
129
130 while (len > 0) {
131 data = readw(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 = readw(s->addr + OMAP_I2C_DATA);
136
137 stat = readw(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 = readw(s->addr + OMAP_I2C_CON);
154 g_assert((data & OMAP_I2C_CON_STP) == 0);
155 }
156
157 I2CAdapter *omap_i2c_create(uint64_t addr)
158 {
159 OMAPI2C *s = g_malloc0(sizeof(*s));
160 I2CAdapter *i2c = (I2CAdapter *)s;
161 uint16_t data;
162
163 s->addr = addr;
164
165 i2c->send = omap_i2c_send;
166 i2c->recv = omap_i2c_recv;
167
168 /* verify the mmio address by looking for a known signature */
169 data = readw(addr + OMAP_I2C_REV);
170 g_assert_cmphex(data, ==, 0x34);
171
172 return i2c;
173 }