xref: /qemu/tests/qtest/libqos/i2c-imx.c (revision f1dfd507325f25a73d777db5f5d9b3fcac89287b)
17f398627SJean-Christophe Dubois /*
27f398627SJean-Christophe Dubois  * QTest i.MX I2C driver
37f398627SJean-Christophe Dubois  *
47f398627SJean-Christophe Dubois  * Copyright (c) 2013 Jean-Christophe Dubois
57f398627SJean-Christophe Dubois  *
67f398627SJean-Christophe Dubois  *  This program is free software; you can redistribute it and/or modify it
77f398627SJean-Christophe Dubois  *  under the terms of the GNU General Public License as published by the
87f398627SJean-Christophe Dubois  *  Free Software Foundation; either version 2 of the License, or
97f398627SJean-Christophe Dubois  *  (at your option) any later version.
107f398627SJean-Christophe Dubois  *
117f398627SJean-Christophe Dubois  *  This program is distributed in the hope that it will be useful, but WITHOUT
127f398627SJean-Christophe Dubois  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
137f398627SJean-Christophe Dubois  *  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
147f398627SJean-Christophe Dubois  *  for more details.
157f398627SJean-Christophe Dubois  *
167f398627SJean-Christophe Dubois  *  You should have received a copy of the GNU General Public License along
177f398627SJean-Christophe Dubois  *  with this program; if not, see <http://www.gnu.org/licenses/>.
187f398627SJean-Christophe Dubois  */
197f398627SJean-Christophe Dubois 
20681c28a3SPeter Maydell #include "qemu/osdep.h"
217f398627SJean-Christophe Dubois #include "libqos/i2c.h"
227f398627SJean-Christophe Dubois 
237f398627SJean-Christophe Dubois 
247f398627SJean-Christophe Dubois #include "libqtest.h"
257f398627SJean-Christophe Dubois 
267f398627SJean-Christophe Dubois #include "hw/i2c/imx_i2c.h"
277f398627SJean-Christophe Dubois 
287f398627SJean-Christophe Dubois enum IMXI2CDirection {
297f398627SJean-Christophe Dubois     IMX_I2C_READ,
307f398627SJean-Christophe Dubois     IMX_I2C_WRITE,
317f398627SJean-Christophe Dubois };
327f398627SJean-Christophe Dubois 
337f398627SJean-Christophe Dubois typedef struct IMXI2C {
347f398627SJean-Christophe Dubois     I2CAdapter parent;
357f398627SJean-Christophe Dubois 
367f398627SJean-Christophe Dubois     uint64_t addr;
377f398627SJean-Christophe Dubois } IMXI2C;
387f398627SJean-Christophe Dubois 
397f398627SJean-Christophe Dubois 
407f398627SJean-Christophe Dubois static void imx_i2c_set_slave_addr(IMXI2C *s, uint8_t addr,
417f398627SJean-Christophe Dubois                                    enum IMXI2CDirection direction)
427f398627SJean-Christophe Dubois {
43*f1dfd507SEric Blake     qtest_writeb(s->parent.qts, s->addr + I2DR_ADDR,
44*f1dfd507SEric Blake                  (addr << 1) | (direction == IMX_I2C_READ ? 1 : 0));
457f398627SJean-Christophe Dubois }
467f398627SJean-Christophe Dubois 
477f398627SJean-Christophe Dubois static void imx_i2c_send(I2CAdapter *i2c, uint8_t addr,
487f398627SJean-Christophe Dubois                          const uint8_t *buf, uint16_t len)
497f398627SJean-Christophe Dubois {
507f398627SJean-Christophe Dubois     IMXI2C *s = (IMXI2C *)i2c;
517f398627SJean-Christophe Dubois     uint8_t data;
527f398627SJean-Christophe Dubois     uint8_t status;
537f398627SJean-Christophe Dubois     uint16_t size = 0;
547f398627SJean-Christophe Dubois 
557f398627SJean-Christophe Dubois     if (!len) {
567f398627SJean-Christophe Dubois         return;
577f398627SJean-Christophe Dubois     }
587f398627SJean-Christophe Dubois 
597f398627SJean-Christophe Dubois     /* set the bus for write */
607f398627SJean-Christophe Dubois     data = I2CR_IEN |
617f398627SJean-Christophe Dubois            I2CR_IIEN |
627f398627SJean-Christophe Dubois            I2CR_MSTA |
637f398627SJean-Christophe Dubois            I2CR_MTX |
647f398627SJean-Christophe Dubois            I2CR_TXAK;
657f398627SJean-Christophe Dubois 
66*f1dfd507SEric Blake     qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
67*f1dfd507SEric Blake     status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
687f398627SJean-Christophe Dubois     g_assert((status & I2SR_IBB) != 0);
697f398627SJean-Christophe Dubois 
707f398627SJean-Christophe Dubois     /* set the slave address */
717f398627SJean-Christophe Dubois     imx_i2c_set_slave_addr(s, addr, IMX_I2C_WRITE);
72*f1dfd507SEric Blake     status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
737f398627SJean-Christophe Dubois     g_assert((status & I2SR_IIF) != 0);
747f398627SJean-Christophe Dubois     g_assert((status & I2SR_RXAK) == 0);
757f398627SJean-Christophe Dubois 
767f398627SJean-Christophe Dubois     /* ack the interrupt */
77*f1dfd507SEric Blake     qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
78*f1dfd507SEric Blake     status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
797f398627SJean-Christophe Dubois     g_assert((status & I2SR_IIF) == 0);
807f398627SJean-Christophe Dubois 
817f398627SJean-Christophe Dubois     while (size < len) {
827f398627SJean-Christophe Dubois         /* check we are still busy */
83*f1dfd507SEric Blake         status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
847f398627SJean-Christophe Dubois         g_assert((status & I2SR_IBB) != 0);
857f398627SJean-Christophe Dubois 
867f398627SJean-Christophe Dubois         /* write the data */
87*f1dfd507SEric Blake         qtest_writeb(i2c->qts, s->addr + I2DR_ADDR, buf[size]);
88*f1dfd507SEric Blake         status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
897f398627SJean-Christophe Dubois         g_assert((status & I2SR_IIF) != 0);
907f398627SJean-Christophe Dubois         g_assert((status & I2SR_RXAK) == 0);
917f398627SJean-Christophe Dubois 
927f398627SJean-Christophe Dubois         /* ack the interrupt */
93*f1dfd507SEric Blake         qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
94*f1dfd507SEric Blake         status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
957f398627SJean-Christophe Dubois         g_assert((status & I2SR_IIF) == 0);
967f398627SJean-Christophe Dubois 
977f398627SJean-Christophe Dubois         size++;
987f398627SJean-Christophe Dubois     }
997f398627SJean-Christophe Dubois 
1007f398627SJean-Christophe Dubois     /* release the bus */
1017f398627SJean-Christophe Dubois     data &= ~(I2CR_MSTA | I2CR_MTX);
102*f1dfd507SEric Blake     qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
103*f1dfd507SEric Blake     status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
1047f398627SJean-Christophe Dubois     g_assert((status & I2SR_IBB) == 0);
1057f398627SJean-Christophe Dubois }
1067f398627SJean-Christophe Dubois 
1077f398627SJean-Christophe Dubois static void imx_i2c_recv(I2CAdapter *i2c, uint8_t addr,
1087f398627SJean-Christophe Dubois                          uint8_t *buf, uint16_t len)
1097f398627SJean-Christophe Dubois {
1107f398627SJean-Christophe Dubois     IMXI2C *s = (IMXI2C *)i2c;
1117f398627SJean-Christophe Dubois     uint8_t data;
1127f398627SJean-Christophe Dubois     uint8_t status;
1137f398627SJean-Christophe Dubois     uint16_t size = 0;
1147f398627SJean-Christophe Dubois 
1157f398627SJean-Christophe Dubois     if (!len) {
1167f398627SJean-Christophe Dubois         return;
1177f398627SJean-Christophe Dubois     }
1187f398627SJean-Christophe Dubois 
1197f398627SJean-Christophe Dubois     /* set the bus for write */
1207f398627SJean-Christophe Dubois     data = I2CR_IEN |
1217f398627SJean-Christophe Dubois            I2CR_IIEN |
1227f398627SJean-Christophe Dubois            I2CR_MSTA |
1237f398627SJean-Christophe Dubois            I2CR_MTX |
1247f398627SJean-Christophe Dubois            I2CR_TXAK;
1257f398627SJean-Christophe Dubois 
126*f1dfd507SEric Blake     qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
127*f1dfd507SEric Blake     status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
1287f398627SJean-Christophe Dubois     g_assert((status & I2SR_IBB) != 0);
1297f398627SJean-Christophe Dubois 
1307f398627SJean-Christophe Dubois     /* set the slave address */
1317f398627SJean-Christophe Dubois     imx_i2c_set_slave_addr(s, addr, IMX_I2C_READ);
132*f1dfd507SEric Blake     status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
1337f398627SJean-Christophe Dubois     g_assert((status & I2SR_IIF) != 0);
1347f398627SJean-Christophe Dubois     g_assert((status & I2SR_RXAK) == 0);
1357f398627SJean-Christophe Dubois 
1367f398627SJean-Christophe Dubois     /* ack the interrupt */
137*f1dfd507SEric Blake     qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
138*f1dfd507SEric Blake     status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
1397f398627SJean-Christophe Dubois     g_assert((status & I2SR_IIF) == 0);
1407f398627SJean-Christophe Dubois 
1417f398627SJean-Christophe Dubois     /* set the bus for read */
1427f398627SJean-Christophe Dubois     data &= ~I2CR_MTX;
1437f398627SJean-Christophe Dubois     /* if only one byte don't ack */
1447f398627SJean-Christophe Dubois     if (len != 1) {
1457f398627SJean-Christophe Dubois         data &= ~I2CR_TXAK;
1467f398627SJean-Christophe Dubois     }
147*f1dfd507SEric Blake     qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
148*f1dfd507SEric Blake     status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
1497f398627SJean-Christophe Dubois     g_assert((status & I2SR_IBB) != 0);
1507f398627SJean-Christophe Dubois 
1517f398627SJean-Christophe Dubois     /* dummy read */
152*f1dfd507SEric Blake     qtest_readb(i2c->qts, s->addr + I2DR_ADDR);
153*f1dfd507SEric Blake     status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
1547f398627SJean-Christophe Dubois     g_assert((status & I2SR_IIF) != 0);
1557f398627SJean-Christophe Dubois 
1567f398627SJean-Christophe Dubois     /* ack the interrupt */
157*f1dfd507SEric Blake     qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
158*f1dfd507SEric Blake     status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
1597f398627SJean-Christophe Dubois     g_assert((status & I2SR_IIF) == 0);
1607f398627SJean-Christophe Dubois 
1617f398627SJean-Christophe Dubois     while (size < len) {
1627f398627SJean-Christophe Dubois         /* check we are still busy */
163*f1dfd507SEric Blake         status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
1647f398627SJean-Christophe Dubois         g_assert((status & I2SR_IBB) != 0);
1657f398627SJean-Christophe Dubois 
1667f398627SJean-Christophe Dubois         if (size == (len - 1)) {
1677f398627SJean-Christophe Dubois             /* stop the read transaction */
1687f398627SJean-Christophe Dubois             data &= ~(I2CR_MSTA | I2CR_MTX);
1697f398627SJean-Christophe Dubois         } else {
1707f398627SJean-Christophe Dubois             /* ack the data read */
1717f398627SJean-Christophe Dubois             data |= I2CR_TXAK;
1727f398627SJean-Christophe Dubois         }
173*f1dfd507SEric Blake         qtest_writeb(i2c->qts, s->addr + I2CR_ADDR, data);
1747f398627SJean-Christophe Dubois 
1757f398627SJean-Christophe Dubois         /* read the data */
176*f1dfd507SEric Blake         buf[size] = qtest_readb(i2c->qts, s->addr + I2DR_ADDR);
1777f398627SJean-Christophe Dubois 
1787f398627SJean-Christophe Dubois         if (size != (len - 1)) {
179*f1dfd507SEric Blake             status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
1807f398627SJean-Christophe Dubois             g_assert((status & I2SR_IIF) != 0);
1817f398627SJean-Christophe Dubois 
1827f398627SJean-Christophe Dubois             /* ack the interrupt */
183*f1dfd507SEric Blake             qtest_writeb(i2c->qts, s->addr + I2SR_ADDR, 0);
1847f398627SJean-Christophe Dubois         }
1857f398627SJean-Christophe Dubois 
186*f1dfd507SEric Blake         status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
1877f398627SJean-Christophe Dubois         g_assert((status & I2SR_IIF) == 0);
1887f398627SJean-Christophe Dubois 
1897f398627SJean-Christophe Dubois         size++;
1907f398627SJean-Christophe Dubois     }
1917f398627SJean-Christophe Dubois 
192*f1dfd507SEric Blake     status = qtest_readb(i2c->qts, s->addr + I2SR_ADDR);
1937f398627SJean-Christophe Dubois     g_assert((status & I2SR_IBB) == 0);
1947f398627SJean-Christophe Dubois }
1957f398627SJean-Christophe Dubois 
196*f1dfd507SEric Blake I2CAdapter *imx_i2c_create(QTestState *qts, uint64_t addr)
1977f398627SJean-Christophe Dubois {
1987f398627SJean-Christophe Dubois     IMXI2C *s = g_malloc0(sizeof(*s));
1997f398627SJean-Christophe Dubois     I2CAdapter *i2c = (I2CAdapter *)s;
2007f398627SJean-Christophe Dubois 
2017f398627SJean-Christophe Dubois     s->addr = addr;
2027f398627SJean-Christophe Dubois 
2037f398627SJean-Christophe Dubois     i2c->send = imx_i2c_send;
2047f398627SJean-Christophe Dubois     i2c->recv = imx_i2c_recv;
205*f1dfd507SEric Blake     i2c->qts = qts;
2067f398627SJean-Christophe Dubois 
2077f398627SJean-Christophe Dubois     return i2c;
2087f398627SJean-Christophe Dubois }
209