2007-11-03 13:50:46 +01:00
|
|
|
/*
|
|
|
|
* TI OMAP on-chip I2C controller. Only "new I2C" mode supported.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Andrzej Zaborowski <balrog@zabor.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2009-01-04 23:05:52 +01:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
2009-07-16 22:47:01 +02:00
|
|
|
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
2007-11-03 13:50:46 +01:00
|
|
|
*/
|
2016-01-26 19:17:28 +01:00
|
|
|
#include "qemu/osdep.h"
|
2018-06-26 18:50:40 +02:00
|
|
|
#include "qemu/log.h"
|
2013-02-04 15:40:22 +01:00
|
|
|
#include "hw/hw.h"
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/i2c/i2c.h"
|
|
|
|
#include "hw/arm/omap.h"
|
2013-02-04 15:40:22 +01:00
|
|
|
#include "hw/sysbus.h"
|
2015-12-17 17:35:10 +01:00
|
|
|
#include "qemu/error-report.h"
|
2016-06-14 16:59:13 +02:00
|
|
|
#include "qapi/error.h"
|
2007-11-03 13:50:46 +01:00
|
|
|
|
2013-07-26 18:24:27 +02:00
|
|
|
#define TYPE_OMAP_I2C "omap_i2c"
|
|
|
|
#define OMAP_I2C(obj) OBJECT_CHECK(OMAPI2CState, (obj), TYPE_OMAP_I2C)
|
2012-03-14 16:37:53 +01:00
|
|
|
|
|
|
|
typedef struct OMAPI2CState {
|
2013-07-26 18:24:27 +02:00
|
|
|
SysBusDevice parent_obj;
|
|
|
|
|
2011-11-25 15:21:37 +01:00
|
|
|
MemoryRegion iomem;
|
2007-11-03 13:50:46 +01:00
|
|
|
qemu_irq irq;
|
|
|
|
qemu_irq drq[2];
|
2013-08-03 00:18:51 +02:00
|
|
|
I2CBus *bus;
|
2007-11-03 13:50:46 +01:00
|
|
|
|
2008-02-10 18:02:23 +01:00
|
|
|
uint8_t revision;
|
2012-03-14 16:37:53 +01:00
|
|
|
void *iclk;
|
|
|
|
void *fclk;
|
|
|
|
|
2007-11-03 13:50:46 +01:00
|
|
|
uint8_t mask;
|
|
|
|
uint16_t stat;
|
|
|
|
uint16_t dma;
|
|
|
|
uint16_t count;
|
|
|
|
int count_cur;
|
|
|
|
uint32_t fifo;
|
|
|
|
int rxlen;
|
|
|
|
int txlen;
|
|
|
|
uint16_t control;
|
|
|
|
uint16_t addr[2];
|
|
|
|
uint8_t divider;
|
|
|
|
uint8_t times[2];
|
|
|
|
uint16_t test;
|
2012-03-14 16:37:53 +01:00
|
|
|
} OMAPI2CState;
|
2007-11-03 13:50:46 +01:00
|
|
|
|
2008-02-10 18:02:23 +01:00
|
|
|
#define OMAP2_INTR_REV 0x34
|
|
|
|
#define OMAP2_GC_REV 0x34
|
|
|
|
|
2012-03-14 16:37:53 +01:00
|
|
|
static void omap_i2c_interrupts_update(OMAPI2CState *s)
|
2007-11-03 13:50:46 +01:00
|
|
|
{
|
|
|
|
qemu_set_irq(s->irq, s->stat & s->mask);
|
|
|
|
if ((s->dma >> 15) & 1) /* RDMA_EN */
|
|
|
|
qemu_set_irq(s->drq[0], (s->stat >> 3) & 1); /* RRDY */
|
|
|
|
if ((s->dma >> 7) & 1) /* XDMA_EN */
|
|
|
|
qemu_set_irq(s->drq[1], (s->stat >> 4) & 1); /* XRDY */
|
|
|
|
}
|
|
|
|
|
2012-03-14 16:37:53 +01:00
|
|
|
static void omap_i2c_fifo_run(OMAPI2CState *s)
|
2007-11-03 13:50:46 +01:00
|
|
|
{
|
|
|
|
int ack = 1;
|
|
|
|
|
|
|
|
if (!i2c_bus_busy(s->bus))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((s->control >> 2) & 1) { /* RM */
|
|
|
|
if ((s->control >> 1) & 1) { /* STP */
|
|
|
|
i2c_end_transfer(s->bus);
|
|
|
|
s->control &= ~(1 << 1); /* STP */
|
|
|
|
s->count_cur = s->count;
|
2008-02-10 18:02:23 +01:00
|
|
|
s->txlen = 0;
|
2007-11-03 13:50:46 +01:00
|
|
|
} else if ((s->control >> 9) & 1) { /* TRX */
|
|
|
|
while (ack && s->txlen)
|
|
|
|
ack = (i2c_send(s->bus,
|
|
|
|
(s->fifo >> ((-- s->txlen) << 3)) &
|
|
|
|
0xff) >= 0);
|
|
|
|
s->stat |= 1 << 4; /* XRDY */
|
|
|
|
} else {
|
|
|
|
while (s->rxlen < 4)
|
|
|
|
s->fifo |= i2c_recv(s->bus) << ((s->rxlen ++) << 3);
|
|
|
|
s->stat |= 1 << 3; /* RRDY */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((s->control >> 9) & 1) { /* TRX */
|
|
|
|
while (ack && s->count_cur && s->txlen) {
|
|
|
|
ack = (i2c_send(s->bus,
|
|
|
|
(s->fifo >> ((-- s->txlen) << 3)) &
|
|
|
|
0xff) >= 0);
|
|
|
|
s->count_cur --;
|
|
|
|
}
|
|
|
|
if (ack && s->count_cur)
|
|
|
|
s->stat |= 1 << 4; /* XRDY */
|
2008-04-14 23:05:22 +02:00
|
|
|
else
|
|
|
|
s->stat &= ~(1 << 4); /* XRDY */
|
2007-11-03 13:50:46 +01:00
|
|
|
if (!s->count_cur) {
|
|
|
|
s->stat |= 1 << 2; /* ARDY */
|
|
|
|
s->control &= ~(1 << 10); /* MST */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (s->count_cur && s->rxlen < 4) {
|
|
|
|
s->fifo |= i2c_recv(s->bus) << ((s->rxlen ++) << 3);
|
|
|
|
s->count_cur --;
|
|
|
|
}
|
|
|
|
if (s->rxlen)
|
|
|
|
s->stat |= 1 << 3; /* RRDY */
|
2008-04-14 23:05:22 +02:00
|
|
|
else
|
|
|
|
s->stat &= ~(1 << 3); /* RRDY */
|
2007-11-03 13:50:46 +01:00
|
|
|
}
|
|
|
|
if (!s->count_cur) {
|
|
|
|
if ((s->control >> 1) & 1) { /* STP */
|
|
|
|
i2c_end_transfer(s->bus);
|
|
|
|
s->control &= ~(1 << 1); /* STP */
|
|
|
|
s->count_cur = s->count;
|
2008-02-10 18:02:23 +01:00
|
|
|
s->txlen = 0;
|
2007-11-03 13:50:46 +01:00
|
|
|
} else {
|
|
|
|
s->stat |= 1 << 2; /* ARDY */
|
|
|
|
s->control &= ~(1 << 10); /* MST */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->stat |= (!ack) << 1; /* NACK */
|
|
|
|
if (!ack)
|
|
|
|
s->control &= ~(1 << 1); /* STP */
|
|
|
|
}
|
|
|
|
|
2012-03-14 16:37:53 +01:00
|
|
|
static void omap_i2c_reset(DeviceState *dev)
|
2007-11-03 13:50:46 +01:00
|
|
|
{
|
2013-07-26 18:24:27 +02:00
|
|
|
OMAPI2CState *s = OMAP_I2C(dev);
|
|
|
|
|
2007-11-03 13:50:46 +01:00
|
|
|
s->mask = 0;
|
|
|
|
s->stat = 0;
|
|
|
|
s->dma = 0;
|
|
|
|
s->count = 0;
|
|
|
|
s->count_cur = 0;
|
|
|
|
s->fifo = 0;
|
|
|
|
s->rxlen = 0;
|
|
|
|
s->txlen = 0;
|
|
|
|
s->control = 0;
|
|
|
|
s->addr[0] = 0;
|
|
|
|
s->addr[1] = 0;
|
|
|
|
s->divider = 0;
|
|
|
|
s->times[0] = 0;
|
|
|
|
s->times[1] = 0;
|
|
|
|
s->test = 0;
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static uint32_t omap_i2c_read(void *opaque, hwaddr addr)
|
2007-11-03 13:50:46 +01:00
|
|
|
{
|
2012-03-14 16:37:53 +01:00
|
|
|
OMAPI2CState *s = opaque;
|
2007-11-04 13:19:22 +01:00
|
|
|
int offset = addr & OMAP_MPUI_REG_MASK;
|
2007-11-03 13:50:46 +01:00
|
|
|
uint16_t ret;
|
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case 0x00: /* I2C_REV */
|
2008-02-10 18:02:23 +01:00
|
|
|
return s->revision; /* REV */
|
2007-11-03 13:50:46 +01:00
|
|
|
|
|
|
|
case 0x04: /* I2C_IE */
|
|
|
|
return s->mask;
|
|
|
|
|
|
|
|
case 0x08: /* I2C_STAT */
|
|
|
|
return s->stat | (i2c_bus_busy(s->bus) << 12);
|
|
|
|
|
|
|
|
case 0x0c: /* I2C_IV */
|
2008-02-10 18:02:23 +01:00
|
|
|
if (s->revision >= OMAP2_INTR_REV)
|
|
|
|
break;
|
2015-03-23 16:29:27 +01:00
|
|
|
ret = ctz32(s->stat & s->mask);
|
|
|
|
if (ret != 32) {
|
|
|
|
s->stat ^= 1 << ret;
|
|
|
|
ret++;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
2007-11-03 13:50:46 +01:00
|
|
|
omap_i2c_interrupts_update(s);
|
|
|
|
return ret;
|
|
|
|
|
2008-02-10 18:02:23 +01:00
|
|
|
case 0x10: /* I2C_SYSS */
|
|
|
|
return (s->control >> 15) & 1; /* I2C_EN */
|
|
|
|
|
2007-11-03 13:50:46 +01:00
|
|
|
case 0x14: /* I2C_BUF */
|
|
|
|
return s->dma;
|
|
|
|
|
|
|
|
case 0x18: /* I2C_CNT */
|
|
|
|
return s->count_cur; /* DCOUNT */
|
|
|
|
|
|
|
|
case 0x1c: /* I2C_DATA */
|
|
|
|
ret = 0;
|
|
|
|
if (s->control & (1 << 14)) { /* BE */
|
|
|
|
ret |= ((s->fifo >> 0) & 0xff) << 8;
|
|
|
|
ret |= ((s->fifo >> 8) & 0xff) << 0;
|
|
|
|
} else {
|
|
|
|
ret |= ((s->fifo >> 8) & 0xff) << 8;
|
|
|
|
ret |= ((s->fifo >> 0) & 0xff) << 0;
|
|
|
|
}
|
|
|
|
if (s->rxlen == 1) {
|
|
|
|
s->stat |= 1 << 15; /* SBD */
|
|
|
|
s->rxlen = 0;
|
|
|
|
} else if (s->rxlen > 1) {
|
|
|
|
if (s->rxlen > 2)
|
|
|
|
s->fifo >>= 16;
|
|
|
|
s->rxlen -= 2;
|
2010-09-18 09:01:48 +02:00
|
|
|
} else {
|
|
|
|
/* XXX: remote access (qualifier) error - what's that? */
|
|
|
|
}
|
2007-11-03 13:50:46 +01:00
|
|
|
if (!s->rxlen) {
|
2008-02-10 18:02:23 +01:00
|
|
|
s->stat &= ~(1 << 3); /* RRDY */
|
2007-11-03 13:50:46 +01:00
|
|
|
if (((s->control >> 10) & 1) && /* MST */
|
|
|
|
((~s->control >> 9) & 1)) { /* TRX */
|
|
|
|
s->stat |= 1 << 2; /* ARDY */
|
|
|
|
s->control &= ~(1 << 10); /* MST */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->stat &= ~(1 << 11); /* ROVR */
|
|
|
|
omap_i2c_fifo_run(s);
|
|
|
|
omap_i2c_interrupts_update(s);
|
|
|
|
return ret;
|
|
|
|
|
2008-02-10 18:02:23 +01:00
|
|
|
case 0x20: /* I2C_SYSC */
|
|
|
|
return 0;
|
|
|
|
|
2007-11-03 13:50:46 +01:00
|
|
|
case 0x24: /* I2C_CON */
|
|
|
|
return s->control;
|
|
|
|
|
|
|
|
case 0x28: /* I2C_OA */
|
|
|
|
return s->addr[0];
|
|
|
|
|
|
|
|
case 0x2c: /* I2C_SA */
|
|
|
|
return s->addr[1];
|
|
|
|
|
|
|
|
case 0x30: /* I2C_PSC */
|
|
|
|
return s->divider;
|
|
|
|
|
|
|
|
case 0x34: /* I2C_SCLL */
|
|
|
|
return s->times[0];
|
|
|
|
|
|
|
|
case 0x38: /* I2C_SCLH */
|
|
|
|
return s->times[1];
|
|
|
|
|
|
|
|
case 0x3c: /* I2C_SYSTEST */
|
|
|
|
if (s->test & (1 << 15)) { /* ST_EN */
|
|
|
|
s->test ^= 0xa;
|
|
|
|
return s->test;
|
|
|
|
} else
|
|
|
|
return s->test & ~0x300f;
|
|
|
|
}
|
|
|
|
|
|
|
|
OMAP_BAD_REG(addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static void omap_i2c_write(void *opaque, hwaddr addr,
|
2007-11-03 13:50:46 +01:00
|
|
|
uint32_t value)
|
|
|
|
{
|
2012-03-14 16:37:53 +01:00
|
|
|
OMAPI2CState *s = opaque;
|
2007-11-04 13:19:22 +01:00
|
|
|
int offset = addr & OMAP_MPUI_REG_MASK;
|
2007-11-03 13:50:46 +01:00
|
|
|
int nack;
|
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case 0x00: /* I2C_REV */
|
|
|
|
case 0x0c: /* I2C_IV */
|
2008-02-10 18:02:23 +01:00
|
|
|
case 0x10: /* I2C_SYSS */
|
|
|
|
OMAP_RO_REG(addr);
|
2007-11-03 13:50:46 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
case 0x04: /* I2C_IE */
|
2008-02-10 18:02:23 +01:00
|
|
|
s->mask = value & (s->revision < OMAP2_GC_REV ? 0x1f : 0x3f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x08: /* I2C_STAT */
|
|
|
|
if (s->revision < OMAP2_INTR_REV) {
|
|
|
|
OMAP_RO_REG(addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-14 23:05:22 +02:00
|
|
|
/* RRDY and XRDY are reset by hardware. (in all versions???) */
|
|
|
|
s->stat &= ~(value & 0x27);
|
2008-02-10 18:02:23 +01:00
|
|
|
omap_i2c_interrupts_update(s);
|
2007-11-03 13:50:46 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x14: /* I2C_BUF */
|
|
|
|
s->dma = value & 0x8080;
|
|
|
|
if (value & (1 << 15)) /* RDMA_EN */
|
|
|
|
s->mask &= ~(1 << 3); /* RRDY_IE */
|
|
|
|
if (value & (1 << 7)) /* XDMA_EN */
|
|
|
|
s->mask &= ~(1 << 4); /* XRDY_IE */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x18: /* I2C_CNT */
|
|
|
|
s->count = value; /* DCOUNT */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x1c: /* I2C_DATA */
|
|
|
|
if (s->txlen > 2) {
|
|
|
|
/* XXX: remote access (qualifier) error - what's that? */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->fifo <<= 16;
|
|
|
|
s->txlen += 2;
|
|
|
|
if (s->control & (1 << 14)) { /* BE */
|
|
|
|
s->fifo |= ((value >> 8) & 0xff) << 8;
|
|
|
|
s->fifo |= ((value >> 0) & 0xff) << 0;
|
|
|
|
} else {
|
|
|
|
s->fifo |= ((value >> 0) & 0xff) << 8;
|
|
|
|
s->fifo |= ((value >> 8) & 0xff) << 0;
|
|
|
|
}
|
|
|
|
s->stat &= ~(1 << 10); /* XUDF */
|
|
|
|
if (s->txlen > 2)
|
|
|
|
s->stat &= ~(1 << 4); /* XRDY */
|
|
|
|
omap_i2c_fifo_run(s);
|
|
|
|
omap_i2c_interrupts_update(s);
|
|
|
|
break;
|
|
|
|
|
2008-02-10 18:02:23 +01:00
|
|
|
case 0x20: /* I2C_SYSC */
|
|
|
|
if (s->revision < OMAP2_INTR_REV) {
|
|
|
|
OMAP_BAD_REG(addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-26 18:24:27 +02:00
|
|
|
if (value & 2) {
|
|
|
|
omap_i2c_reset(DEVICE(s));
|
|
|
|
}
|
2008-02-10 18:02:23 +01:00
|
|
|
break;
|
|
|
|
|
2007-11-03 13:50:46 +01:00
|
|
|
case 0x24: /* I2C_CON */
|
2008-02-10 18:02:23 +01:00
|
|
|
s->control = value & 0xcf87;
|
2007-11-03 13:50:46 +01:00
|
|
|
if (~value & (1 << 15)) { /* I2C_EN */
|
2013-07-26 18:24:27 +02:00
|
|
|
if (s->revision < OMAP2_INTR_REV) {
|
|
|
|
omap_i2c_reset(DEVICE(s));
|
|
|
|
}
|
2007-11-03 13:50:46 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-06-26 18:50:40 +02:00
|
|
|
if ((value & (1 << 15)) && !(value & (1 << 10))) { /* MST */
|
|
|
|
qemu_log_mask(LOG_UNIMP, "%s: I^2C slave mode not supported\n",
|
|
|
|
__func__);
|
2007-11-03 13:50:46 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-06-26 18:50:40 +02:00
|
|
|
if ((value & (1 << 15)) && value & (1 << 8)) { /* XA */
|
|
|
|
qemu_log_mask(LOG_UNIMP,
|
|
|
|
"%s: 10-bit addressing mode not supported\n",
|
|
|
|
__func__);
|
2007-11-03 13:50:46 +01:00
|
|
|
break;
|
|
|
|
}
|
2008-02-10 18:02:23 +01:00
|
|
|
if ((value & (1 << 15)) && value & (1 << 0)) { /* STT */
|
2007-11-03 13:50:46 +01:00
|
|
|
nack = !!i2c_start_transfer(s->bus, s->addr[1], /* SA */
|
|
|
|
(~value >> 9) & 1); /* TRX */
|
|
|
|
s->stat |= nack << 1; /* NACK */
|
|
|
|
s->control &= ~(1 << 0); /* STT */
|
2008-07-21 21:52:54 +02:00
|
|
|
s->fifo = 0;
|
2007-11-03 13:50:46 +01:00
|
|
|
if (nack)
|
|
|
|
s->control &= ~(1 << 1); /* STP */
|
2008-02-10 18:02:23 +01:00
|
|
|
else {
|
|
|
|
s->count_cur = s->count;
|
2007-11-03 13:50:46 +01:00
|
|
|
omap_i2c_fifo_run(s);
|
2008-02-10 18:02:23 +01:00
|
|
|
}
|
2007-11-03 13:50:46 +01:00
|
|
|
omap_i2c_interrupts_update(s);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x28: /* I2C_OA */
|
|
|
|
s->addr[0] = value & 0x3ff;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x2c: /* I2C_SA */
|
|
|
|
s->addr[1] = value & 0x3ff;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x30: /* I2C_PSC */
|
|
|
|
s->divider = value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x34: /* I2C_SCLL */
|
|
|
|
s->times[0] = value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x38: /* I2C_SCLH */
|
|
|
|
s->times[1] = value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x3c: /* I2C_SYSTEST */
|
2008-02-10 18:02:23 +01:00
|
|
|
s->test = value & 0xf80f;
|
|
|
|
if (value & (1 << 11)) /* SBB */
|
|
|
|
if (s->revision >= OMAP2_INTR_REV) {
|
|
|
|
s->stat |= 0x3f;
|
|
|
|
omap_i2c_interrupts_update(s);
|
|
|
|
}
|
2018-06-26 18:50:40 +02:00
|
|
|
if (value & (1 << 15)) { /* ST_EN */
|
|
|
|
qemu_log_mask(LOG_UNIMP,
|
|
|
|
"%s: System Test not supported\n", __func__);
|
|
|
|
}
|
2007-11-03 13:50:46 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
OMAP_BAD_REG(addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static void omap_i2c_writeb(void *opaque, hwaddr addr,
|
2008-02-10 18:02:23 +01:00
|
|
|
uint32_t value)
|
|
|
|
{
|
2012-03-14 16:37:53 +01:00
|
|
|
OMAPI2CState *s = opaque;
|
2008-02-10 18:02:23 +01:00
|
|
|
int offset = addr & OMAP_MPUI_REG_MASK;
|
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case 0x1c: /* I2C_DATA */
|
|
|
|
if (s->txlen > 2) {
|
|
|
|
/* XXX: remote access (qualifier) error - what's that? */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->fifo <<= 8;
|
|
|
|
s->txlen += 1;
|
|
|
|
s->fifo |= value & 0xff;
|
|
|
|
s->stat &= ~(1 << 10); /* XUDF */
|
|
|
|
if (s->txlen > 2)
|
|
|
|
s->stat &= ~(1 << 4); /* XRDY */
|
|
|
|
omap_i2c_fifo_run(s);
|
|
|
|
omap_i2c_interrupts_update(s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
OMAP_BAD_REG(addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 18:46:17 +02:00
|
|
|
static uint64_t omap_i2c_readfn(void *opaque, hwaddr addr,
|
|
|
|
unsigned size)
|
|
|
|
{
|
|
|
|
switch (size) {
|
|
|
|
case 2:
|
|
|
|
return omap_i2c_read(opaque, addr);
|
|
|
|
default:
|
|
|
|
return omap_badwidth_read16(opaque, addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void omap_i2c_writefn(void *opaque, hwaddr addr,
|
|
|
|
uint64_t value, unsigned size)
|
|
|
|
{
|
|
|
|
switch (size) {
|
|
|
|
case 1:
|
|
|
|
/* Only the last fifo write can be 8 bit. */
|
|
|
|
omap_i2c_writeb(opaque, addr, value);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
omap_i2c_write(opaque, addr, value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
omap_badwidth_write16(opaque, addr, value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-25 15:21:37 +01:00
|
|
|
static const MemoryRegionOps omap_i2c_ops = {
|
2017-09-16 18:46:17 +02:00
|
|
|
.read = omap_i2c_readfn,
|
|
|
|
.write = omap_i2c_writefn,
|
|
|
|
.valid.min_access_size = 1,
|
|
|
|
.valid.max_access_size = 4,
|
2011-11-25 15:21:37 +01:00
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2007-11-03 13:50:46 +01:00
|
|
|
};
|
|
|
|
|
2016-06-14 16:59:13 +02:00
|
|
|
static void omap_i2c_init(Object *obj)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
OMAPI2CState *s = OMAP_I2C(obj);
|
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
|
|
|
|
|
|
|
|
sysbus_init_irq(sbd, &s->irq);
|
|
|
|
sysbus_init_irq(sbd, &s->drq[0]);
|
|
|
|
sysbus_init_irq(sbd, &s->drq[1]);
|
|
|
|
sysbus_init_mmio(sbd, &s->iomem);
|
|
|
|
s->bus = i2c_init_bus(dev, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void omap_i2c_realize(DeviceState *dev, Error **errp)
|
2007-11-03 13:50:46 +01:00
|
|
|
{
|
2013-07-26 18:24:27 +02:00
|
|
|
OMAPI2CState *s = OMAP_I2C(dev);
|
2007-11-03 13:50:46 +01:00
|
|
|
|
2016-06-14 16:59:13 +02:00
|
|
|
memory_region_init_io(&s->iomem, OBJECT(dev), &omap_i2c_ops, s, "omap.i2c",
|
|
|
|
(s->revision < OMAP2_INTR_REV) ? 0x800 : 0x1000);
|
|
|
|
|
2012-03-14 16:37:53 +01:00
|
|
|
if (!s->fclk) {
|
2016-06-14 16:59:13 +02:00
|
|
|
error_setg(errp, "omap_i2c: fclk not connected");
|
|
|
|
return;
|
2012-03-14 16:37:53 +01:00
|
|
|
}
|
|
|
|
if (s->revision >= OMAP2_INTR_REV && !s->iclk) {
|
|
|
|
/* Note that OMAP1 doesn't have a separate interface clock */
|
2016-06-14 16:59:13 +02:00
|
|
|
error_setg(errp, "omap_i2c: iclk not connected");
|
|
|
|
return;
|
2012-03-14 16:37:53 +01:00
|
|
|
}
|
2007-11-03 13:50:46 +01:00
|
|
|
}
|
|
|
|
|
2012-03-14 16:37:53 +01:00
|
|
|
static Property omap_i2c_properties[] = {
|
|
|
|
DEFINE_PROP_UINT8("revision", OMAPI2CState, revision, 0),
|
|
|
|
DEFINE_PROP_PTR("iclk", OMAPI2CState, iclk),
|
|
|
|
DEFINE_PROP_PTR("fclk", OMAPI2CState, fclk),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
2008-02-10 18:02:23 +01:00
|
|
|
|
2012-03-14 16:37:53 +01:00
|
|
|
static void omap_i2c_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2016-06-14 16:59:13 +02:00
|
|
|
|
2012-03-14 16:37:53 +01:00
|
|
|
dc->props = omap_i2c_properties;
|
|
|
|
dc->reset = omap_i2c_reset;
|
2013-11-29 10:43:44 +01:00
|
|
|
/* Reason: pointer properties "iclk", "fclk" */
|
2017-05-03 22:35:44 +02:00
|
|
|
dc->user_creatable = false;
|
2016-06-14 16:59:13 +02:00
|
|
|
dc->realize = omap_i2c_realize;
|
2012-03-14 16:37:53 +01:00
|
|
|
}
|
2008-02-10 18:02:23 +01:00
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo omap_i2c_info = {
|
2013-07-26 18:24:27 +02:00
|
|
|
.name = TYPE_OMAP_I2C,
|
2012-03-14 16:37:53 +01:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(OMAPI2CState),
|
2016-06-14 16:59:13 +02:00
|
|
|
.instance_init = omap_i2c_init,
|
2012-03-14 16:37:53 +01:00
|
|
|
.class_init = omap_i2c_class_init,
|
|
|
|
};
|
2008-02-10 18:02:23 +01:00
|
|
|
|
2012-03-14 16:37:53 +01:00
|
|
|
static void omap_i2c_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&omap_i2c_info);
|
2008-02-10 18:02:23 +01:00
|
|
|
}
|
|
|
|
|
2013-08-03 00:18:51 +02:00
|
|
|
I2CBus *omap_i2c_bus(DeviceState *omap_i2c)
|
2007-11-03 13:50:46 +01:00
|
|
|
{
|
2013-07-26 18:24:27 +02:00
|
|
|
OMAPI2CState *s = OMAP_I2C(omap_i2c);
|
2007-11-03 13:50:46 +01:00
|
|
|
return s->bus;
|
|
|
|
}
|
2012-03-14 16:37:53 +01:00
|
|
|
|
|
|
|
type_init(omap_i2c_register_types)
|