pc: acpi: consolidate CPU hotplug AML
[qemu/ar7.git] / tests / libqos / i2c-imx.c
blob51c3468f97f470f7032f69f709b6bae841458608
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"
23 #include <glib.h>
25 #include "libqtest.h"
27 #include "hw/i2c/imx_i2c.h"
29 enum IMXI2CDirection {
30 IMX_I2C_READ,
31 IMX_I2C_WRITE,
34 typedef struct IMXI2C {
35 I2CAdapter parent;
37 uint64_t addr;
38 } IMXI2C;
41 static void imx_i2c_set_slave_addr(IMXI2C *s, uint8_t addr,
42 enum IMXI2CDirection direction)
44 writeb(s->addr + I2DR_ADDR, (addr << 1) |
45 (direction == IMX_I2C_READ ? 1 : 0));
48 static void imx_i2c_send(I2CAdapter *i2c, uint8_t addr,
49 const uint8_t *buf, uint16_t len)
51 IMXI2C *s = (IMXI2C *)i2c;
52 uint8_t data;
53 uint8_t status;
54 uint16_t size = 0;
56 if (!len) {
57 return;
60 /* set the bus for write */
61 data = I2CR_IEN |
62 I2CR_IIEN |
63 I2CR_MSTA |
64 I2CR_MTX |
65 I2CR_TXAK;
67 writeb(s->addr + I2CR_ADDR, data);
68 status = readb(s->addr + I2SR_ADDR);
69 g_assert((status & I2SR_IBB) != 0);
71 /* set the slave address */
72 imx_i2c_set_slave_addr(s, addr, IMX_I2C_WRITE);
73 status = readb(s->addr + I2SR_ADDR);
74 g_assert((status & I2SR_IIF) != 0);
75 g_assert((status & I2SR_RXAK) == 0);
77 /* ack the interrupt */
78 writeb(s->addr + I2SR_ADDR, 0);
79 status = readb(s->addr + I2SR_ADDR);
80 g_assert((status & I2SR_IIF) == 0);
82 while (size < len) {
83 /* check we are still busy */
84 status = readb(s->addr + I2SR_ADDR);
85 g_assert((status & I2SR_IBB) != 0);
87 /* write the data */
88 writeb(s->addr + I2DR_ADDR, buf[size]);
89 status = readb(s->addr + I2SR_ADDR);
90 g_assert((status & I2SR_IIF) != 0);
91 g_assert((status & I2SR_RXAK) == 0);
93 /* ack the interrupt */
94 writeb(s->addr + I2SR_ADDR, 0);
95 status = readb(s->addr + I2SR_ADDR);
96 g_assert((status & I2SR_IIF) == 0);
98 size++;
101 /* release the bus */
102 data &= ~(I2CR_MSTA | I2CR_MTX);
103 writeb(s->addr + I2CR_ADDR, data);
104 status = readb(s->addr + I2SR_ADDR);
105 g_assert((status & I2SR_IBB) == 0);
108 static void imx_i2c_recv(I2CAdapter *i2c, uint8_t addr,
109 uint8_t *buf, uint16_t len)
111 IMXI2C *s = (IMXI2C *)i2c;
112 uint8_t data;
113 uint8_t status;
114 uint16_t size = 0;
116 if (!len) {
117 return;
120 /* set the bus for write */
121 data = I2CR_IEN |
122 I2CR_IIEN |
123 I2CR_MSTA |
124 I2CR_MTX |
125 I2CR_TXAK;
127 writeb(s->addr + I2CR_ADDR, data);
128 status = readb(s->addr + I2SR_ADDR);
129 g_assert((status & I2SR_IBB) != 0);
131 /* set the slave address */
132 imx_i2c_set_slave_addr(s, addr, IMX_I2C_READ);
133 status = readb(s->addr + I2SR_ADDR);
134 g_assert((status & I2SR_IIF) != 0);
135 g_assert((status & I2SR_RXAK) == 0);
137 /* ack the interrupt */
138 writeb(s->addr + I2SR_ADDR, 0);
139 status = readb(s->addr + I2SR_ADDR);
140 g_assert((status & I2SR_IIF) == 0);
142 /* set the bus for read */
143 data &= ~I2CR_MTX;
144 /* if only one byte don't ack */
145 if (len != 1) {
146 data &= ~I2CR_TXAK;
148 writeb(s->addr + I2CR_ADDR, data);
149 status = readb(s->addr + I2SR_ADDR);
150 g_assert((status & I2SR_IBB) != 0);
152 /* dummy read */
153 readb(s->addr + I2DR_ADDR);
154 status = readb(s->addr + I2SR_ADDR);
155 g_assert((status & I2SR_IIF) != 0);
157 /* ack the interrupt */
158 writeb(s->addr + I2SR_ADDR, 0);
159 status = readb(s->addr + I2SR_ADDR);
160 g_assert((status & I2SR_IIF) == 0);
162 while (size < len) {
163 /* check we are still busy */
164 status = readb(s->addr + I2SR_ADDR);
165 g_assert((status & I2SR_IBB) != 0);
167 if (size == (len - 1)) {
168 /* stop the read transaction */
169 data &= ~(I2CR_MSTA | I2CR_MTX);
170 } else {
171 /* ack the data read */
172 data |= I2CR_TXAK;
174 writeb(s->addr + I2CR_ADDR, data);
176 /* read the data */
177 buf[size] = readb(s->addr + I2DR_ADDR);
179 if (size != (len - 1)) {
180 status = readb(s->addr + I2SR_ADDR);
181 g_assert((status & I2SR_IIF) != 0);
183 /* ack the interrupt */
184 writeb(s->addr + I2SR_ADDR, 0);
187 status = readb(s->addr + I2SR_ADDR);
188 g_assert((status & I2SR_IIF) == 0);
190 size++;
193 status = readb(s->addr + I2SR_ADDR);
194 g_assert((status & I2SR_IBB) == 0);
197 I2CAdapter *imx_i2c_create(uint64_t addr)
199 IMXI2C *s = g_malloc0(sizeof(*s));
200 I2CAdapter *i2c = (I2CAdapter *)s;
202 s->addr = addr;
204 i2c->send = imx_i2c_send;
205 i2c->recv = imx_i2c_recv;
207 return i2c;