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