4 * Copyright (c) 2012 Andreas Färber
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.
9 #include "qemu/osdep.h"
10 #include "libqos/i2c.h"
14 #include "qemu/bswap.h"
17 enum OMAPI2CRegisters {
26 enum OMAPI2CSTATBits {
27 OMAP_I2C_STAT_NACK = 1 << 1,
28 OMAP_I2C_STAT_ARDY = 1 << 2,
29 OMAP_I2C_STAT_RRDY = 1 << 3,
30 OMAP_I2C_STAT_XRDY = 1 << 4,
31 OMAP_I2C_STAT_ROVR = 1 << 11,
32 OMAP_I2C_STAT_SBD = 1 << 15,
36 OMAP_I2C_CON_STT = 1 << 0,
37 OMAP_I2C_CON_STP = 1 << 1,
38 OMAP_I2C_CON_TRX = 1 << 9,
39 OMAP_I2C_CON_MST = 1 << 10,
40 OMAP_I2C_CON_BE = 1 << 14,
41 OMAP_I2C_CON_I2C_EN = 1 << 15,
44 typedef struct OMAPI2C {
51 static void omap_i2c_set_slave_addr(OMAPI2C *s, uint8_t addr)
55 writew(s->addr + OMAP_I2C_SA, data);
56 data = readw(s->addr + OMAP_I2C_SA);
57 g_assert_cmphex(data, ==, addr);
60 static void omap_i2c_send(I2CAdapter *i2c, uint8_t addr,
61 const uint8_t *buf, uint16_t len)
63 OMAPI2C *s = (OMAPI2C *)i2c;
66 omap_i2c_set_slave_addr(s, addr);
69 writew(s->addr + OMAP_I2C_CNT, data);
71 data = OMAP_I2C_CON_I2C_EN |
76 writew(s->addr + OMAP_I2C_CON, data);
77 data = readw(s->addr + OMAP_I2C_CON);
78 g_assert((data & OMAP_I2C_CON_STP) != 0);
80 data = readw(s->addr + OMAP_I2C_STAT);
81 g_assert((data & OMAP_I2C_STAT_NACK) == 0);
84 data = readw(s->addr + OMAP_I2C_STAT);
85 g_assert((data & OMAP_I2C_STAT_XRDY) != 0);
87 data = buf[0] | ((uint16_t)buf[1] << 8);
88 writew(s->addr + OMAP_I2C_DATA, data);
89 buf = (uint8_t *)buf + 2;
93 data = readw(s->addr + OMAP_I2C_STAT);
94 g_assert((data & OMAP_I2C_STAT_XRDY) != 0);
97 writew(s->addr + OMAP_I2C_DATA, data);
100 data = readw(s->addr + OMAP_I2C_CON);
101 g_assert((data & OMAP_I2C_CON_STP) == 0);
104 static void omap_i2c_recv(I2CAdapter *i2c, uint8_t addr,
105 uint8_t *buf, uint16_t len)
107 OMAPI2C *s = (OMAPI2C *)i2c;
110 omap_i2c_set_slave_addr(s, addr);
113 writew(s->addr + OMAP_I2C_CNT, data);
115 data = OMAP_I2C_CON_I2C_EN |
119 writew(s->addr + OMAP_I2C_CON, data);
120 data = readw(s->addr + OMAP_I2C_CON);
121 g_assert((data & OMAP_I2C_CON_STP) == 0);
123 data = readw(s->addr + OMAP_I2C_STAT);
124 g_assert((data & OMAP_I2C_STAT_NACK) == 0);
126 data = readw(s->addr + OMAP_I2C_CNT);
127 g_assert_cmpuint(data, ==, len);
130 data = readw(s->addr + OMAP_I2C_STAT);
131 g_assert((data & OMAP_I2C_STAT_RRDY) != 0);
132 g_assert((data & OMAP_I2C_STAT_ROVR) == 0);
134 data = readw(s->addr + OMAP_I2C_DATA);
136 stat = readw(s->addr + OMAP_I2C_STAT);
138 if (unlikely(len == 1)) {
139 g_assert((stat & OMAP_I2C_STAT_SBD) != 0);
141 buf[0] = data & 0xff;
145 buf[0] = data & 0xff;
152 data = readw(s->addr + OMAP_I2C_CON);
153 g_assert((data & OMAP_I2C_CON_STP) == 0);
156 I2CAdapter *omap_i2c_create(uint64_t addr)
158 OMAPI2C *s = g_malloc0(sizeof(*s));
159 I2CAdapter *i2c = (I2CAdapter *)s;
164 i2c->send = omap_i2c_send;
165 i2c->recv = omap_i2c_recv;
167 /* verify the mmio address by looking for a known signature */
168 data = readw(addr + OMAP_I2C_REV);
169 g_assert_cmphex(data, ==, 0x34);