hmp-commands: add sync-profile
[qemu/ar7.git] / tests / libqos / i2c-imx.c
blob0945f2ecdc3e82cf27ea72711432a976e420c16c
1 /*
2 * QTest i.MX I2C driver
4 * Copyright (c) 2013 Jean-Christophe Dubois
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "libqos/i2c.h"
24 #include "libqtest.h"
26 #include "hw/i2c/imx_i2c.h"
28 enum IMXI2CDirection {
29 IMX_I2C_READ,
30 IMX_I2C_WRITE,
33 typedef struct IMXI2C {
34 I2CAdapter parent;
36 uint64_t addr;
37 } IMXI2C;
40 static void imx_i2c_set_slave_addr(IMXI2C *s, uint8_t addr,
41 enum IMXI2CDirection direction)
43 qtest_writeb(s->parent.qts, s->addr + I2DR_ADDR,
44 (addr << 1) | (direction == IMX_I2C_READ ? 1 : 0));
47 static void imx_i2c_send(I2CAdapter *i2c, uint8_t addr,
48 const uint8_t *buf, uint16_t len)
50 IMXI2C *s = (IMXI2C *)i2c;
51 uint8_t data;
52 uint8_t status;
53 uint16_t size = 0;
55 if (!len) {
56 return;
59 /* set the bus for write */
60 data = I2CR_IEN |
61 I2CR_IIEN |
62 I2CR_MSTA |
63 I2CR_MTX |
64 I2CR_TXAK;
66 qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
67 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
68 g_assert((status & I2SR_IBB) != 0);
70 /* set the slave address */
71 imx_i2c_set_slave_addr(s, addr, IMX_I2C_WRITE);
72 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
73 g_assert((status & I2SR_IIF) != 0);
74 g_assert((status & I2SR_RXAK) == 0);
76 /* ack the interrupt */
77 qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
78 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
79 g_assert((status & I2SR_IIF) == 0);
81 while (size < len) {
82 /* check we are still busy */
83 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
84 g_assert((status & I2SR_IBB) != 0);
86 /* write the data */
87 qtest_writeb(i2c->qts, s->addr + I2DR_ADDR, buf[size]);
88 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
89 g_assert((status & I2SR_IIF) != 0);
90 g_assert((status & I2SR_RXAK) == 0);
92 /* ack the interrupt */
93 qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
94 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
95 g_assert((status & I2SR_IIF) == 0);
97 size++;
100 /* release the bus */
101 data &= ~(I2CR_MSTA | I2CR_MTX);
102 qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
103 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
104 g_assert((status & I2SR_IBB) == 0);
107 static void imx_i2c_recv(I2CAdapter *i2c, uint8_t addr,
108 uint8_t *buf, uint16_t len)
110 IMXI2C *s = (IMXI2C *)i2c;
111 uint8_t data;
112 uint8_t status;
113 uint16_t size = 0;
115 if (!len) {
116 return;
119 /* set the bus for write */
120 data = I2CR_IEN |
121 I2CR_IIEN |
122 I2CR_MSTA |
123 I2CR_MTX |
124 I2CR_TXAK;
126 qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
127 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
128 g_assert((status & I2SR_IBB) != 0);
130 /* set the slave address */
131 imx_i2c_set_slave_addr(s, addr, IMX_I2C_READ);
132 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
133 g_assert((status & I2SR_IIF) != 0);
134 g_assert((status & I2SR_RXAK) == 0);
136 /* ack the interrupt */
137 qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
138 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
139 g_assert((status & I2SR_IIF) == 0);
141 /* set the bus for read */
142 data &= ~I2CR_MTX;
143 /* if only one byte don't ack */
144 if (len != 1) {
145 data &= ~I2CR_TXAK;
147 qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
148 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
149 g_assert((status & I2SR_IBB) != 0);
151 /* dummy read */
152 qtest_readb(i2c->qts, s->addr + I2DR_ADDR);
153 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
154 g_assert((status & I2SR_IIF) != 0);
156 /* ack the interrupt */
157 qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
158 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
159 g_assert((status & I2SR_IIF) == 0);
161 while (size < len) {
162 /* check we are still busy */
163 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
164 g_assert((status & I2SR_IBB) != 0);
166 if (size == (len - 1)) {
167 /* stop the read transaction */
168 data &= ~(I2CR_MSTA | I2CR_MTX);
169 } else {
170 /* ack the data read */
171 data |= I2CR_TXAK;
173 qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
175 /* read the data */
176 buf[size] = qtest_readb(i2c->qts, s->addr + I2DR_ADDR);
178 if (size != (len - 1)) {
179 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
180 g_assert((status & I2SR_IIF) != 0);
182 /* ack the interrupt */
183 qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
186 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
187 g_assert((status & I2SR_IIF) == 0);
189 size++;
192 status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
193 g_assert((status & I2SR_IBB) == 0);
196 I2CAdapter *imx_i2c_create(QTestState *qts, uint64_t addr)
198 IMXI2C *s = g_malloc0(sizeof(*s));
199 I2CAdapter *i2c = (I2CAdapter *)s;
201 s->addr = addr;
203 i2c->send = imx_i2c_send;
204 i2c->recv = imx_i2c_recv;
205 i2c->qts = qts;
207 return i2c;