2 * TI OMAP on-chip I2C controller. Only "new I2C" mode supported.
4 * Copyright (C) 2007 Andrzej Zaborowski <balrog@zabor.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License 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/>.
19 #include "qemu/osdep.h"
21 #include "hw/i2c/i2c.h"
22 #include "hw/arm/omap.h"
23 #include "hw/sysbus.h"
24 #include "qemu/error-report.h"
26 #define TYPE_OMAP_I2C "omap_i2c"
27 #define OMAP_I2C(obj) OBJECT_CHECK(OMAPI2CState, (obj), TYPE_OMAP_I2C)
29 typedef struct OMAPI2CState
{
30 SysBusDevice parent_obj
;
56 #define OMAP2_INTR_REV 0x34
57 #define OMAP2_GC_REV 0x34
59 static void omap_i2c_interrupts_update(OMAPI2CState
*s
)
61 qemu_set_irq(s
->irq
, s
->stat
& s
->mask
);
62 if ((s
->dma
>> 15) & 1) /* RDMA_EN */
63 qemu_set_irq(s
->drq
[0], (s
->stat
>> 3) & 1); /* RRDY */
64 if ((s
->dma
>> 7) & 1) /* XDMA_EN */
65 qemu_set_irq(s
->drq
[1], (s
->stat
>> 4) & 1); /* XRDY */
68 static void omap_i2c_fifo_run(OMAPI2CState
*s
)
72 if (!i2c_bus_busy(s
->bus
))
75 if ((s
->control
>> 2) & 1) { /* RM */
76 if ((s
->control
>> 1) & 1) { /* STP */
77 i2c_end_transfer(s
->bus
);
78 s
->control
&= ~(1 << 1); /* STP */
79 s
->count_cur
= s
->count
;
81 } else if ((s
->control
>> 9) & 1) { /* TRX */
82 while (ack
&& s
->txlen
)
83 ack
= (i2c_send(s
->bus
,
84 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
86 s
->stat
|= 1 << 4; /* XRDY */
89 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
90 s
->stat
|= 1 << 3; /* RRDY */
93 if ((s
->control
>> 9) & 1) { /* TRX */
94 while (ack
&& s
->count_cur
&& s
->txlen
) {
95 ack
= (i2c_send(s
->bus
,
96 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
100 if (ack
&& s
->count_cur
)
101 s
->stat
|= 1 << 4; /* XRDY */
103 s
->stat
&= ~(1 << 4); /* XRDY */
105 s
->stat
|= 1 << 2; /* ARDY */
106 s
->control
&= ~(1 << 10); /* MST */
109 while (s
->count_cur
&& s
->rxlen
< 4) {
110 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
114 s
->stat
|= 1 << 3; /* RRDY */
116 s
->stat
&= ~(1 << 3); /* RRDY */
119 if ((s
->control
>> 1) & 1) { /* STP */
120 i2c_end_transfer(s
->bus
);
121 s
->control
&= ~(1 << 1); /* STP */
122 s
->count_cur
= s
->count
;
125 s
->stat
|= 1 << 2; /* ARDY */
126 s
->control
&= ~(1 << 10); /* MST */
131 s
->stat
|= (!ack
) << 1; /* NACK */
133 s
->control
&= ~(1 << 1); /* STP */
136 static void omap_i2c_reset(DeviceState
*dev
)
138 OMAPI2CState
*s
= OMAP_I2C(dev
);
157 static uint32_t omap_i2c_read(void *opaque
, hwaddr addr
)
159 OMAPI2CState
*s
= opaque
;
160 int offset
= addr
& OMAP_MPUI_REG_MASK
;
164 case 0x00: /* I2C_REV */
165 return s
->revision
; /* REV */
167 case 0x04: /* I2C_IE */
170 case 0x08: /* I2C_STAT */
171 return s
->stat
| (i2c_bus_busy(s
->bus
) << 12);
173 case 0x0c: /* I2C_IV */
174 if (s
->revision
>= OMAP2_INTR_REV
)
176 ret
= ctz32(s
->stat
& s
->mask
);
183 omap_i2c_interrupts_update(s
);
186 case 0x10: /* I2C_SYSS */
187 return (s
->control
>> 15) & 1; /* I2C_EN */
189 case 0x14: /* I2C_BUF */
192 case 0x18: /* I2C_CNT */
193 return s
->count_cur
; /* DCOUNT */
195 case 0x1c: /* I2C_DATA */
197 if (s
->control
& (1 << 14)) { /* BE */
198 ret
|= ((s
->fifo
>> 0) & 0xff) << 8;
199 ret
|= ((s
->fifo
>> 8) & 0xff) << 0;
201 ret
|= ((s
->fifo
>> 8) & 0xff) << 8;
202 ret
|= ((s
->fifo
>> 0) & 0xff) << 0;
205 s
->stat
|= 1 << 15; /* SBD */
207 } else if (s
->rxlen
> 1) {
212 /* XXX: remote access (qualifier) error - what's that? */
215 s
->stat
&= ~(1 << 3); /* RRDY */
216 if (((s
->control
>> 10) & 1) && /* MST */
217 ((~s
->control
>> 9) & 1)) { /* TRX */
218 s
->stat
|= 1 << 2; /* ARDY */
219 s
->control
&= ~(1 << 10); /* MST */
222 s
->stat
&= ~(1 << 11); /* ROVR */
223 omap_i2c_fifo_run(s
);
224 omap_i2c_interrupts_update(s
);
227 case 0x20: /* I2C_SYSC */
230 case 0x24: /* I2C_CON */
233 case 0x28: /* I2C_OA */
236 case 0x2c: /* I2C_SA */
239 case 0x30: /* I2C_PSC */
242 case 0x34: /* I2C_SCLL */
245 case 0x38: /* I2C_SCLH */
248 case 0x3c: /* I2C_SYSTEST */
249 if (s
->test
& (1 << 15)) { /* ST_EN */
253 return s
->test
& ~0x300f;
260 static void omap_i2c_write(void *opaque
, hwaddr addr
,
263 OMAPI2CState
*s
= opaque
;
264 int offset
= addr
& OMAP_MPUI_REG_MASK
;
268 case 0x00: /* I2C_REV */
269 case 0x0c: /* I2C_IV */
270 case 0x10: /* I2C_SYSS */
274 case 0x04: /* I2C_IE */
275 s
->mask
= value
& (s
->revision
< OMAP2_GC_REV
? 0x1f : 0x3f);
278 case 0x08: /* I2C_STAT */
279 if (s
->revision
< OMAP2_INTR_REV
) {
284 /* RRDY and XRDY are reset by hardware. (in all versions???) */
285 s
->stat
&= ~(value
& 0x27);
286 omap_i2c_interrupts_update(s
);
289 case 0x14: /* I2C_BUF */
290 s
->dma
= value
& 0x8080;
291 if (value
& (1 << 15)) /* RDMA_EN */
292 s
->mask
&= ~(1 << 3); /* RRDY_IE */
293 if (value
& (1 << 7)) /* XDMA_EN */
294 s
->mask
&= ~(1 << 4); /* XRDY_IE */
297 case 0x18: /* I2C_CNT */
298 s
->count
= value
; /* DCOUNT */
301 case 0x1c: /* I2C_DATA */
303 /* XXX: remote access (qualifier) error - what's that? */
308 if (s
->control
& (1 << 14)) { /* BE */
309 s
->fifo
|= ((value
>> 8) & 0xff) << 8;
310 s
->fifo
|= ((value
>> 0) & 0xff) << 0;
312 s
->fifo
|= ((value
>> 0) & 0xff) << 8;
313 s
->fifo
|= ((value
>> 8) & 0xff) << 0;
315 s
->stat
&= ~(1 << 10); /* XUDF */
317 s
->stat
&= ~(1 << 4); /* XRDY */
318 omap_i2c_fifo_run(s
);
319 omap_i2c_interrupts_update(s
);
322 case 0x20: /* I2C_SYSC */
323 if (s
->revision
< OMAP2_INTR_REV
) {
329 omap_i2c_reset(DEVICE(s
));
333 case 0x24: /* I2C_CON */
334 s
->control
= value
& 0xcf87;
335 if (~value
& (1 << 15)) { /* I2C_EN */
336 if (s
->revision
< OMAP2_INTR_REV
) {
337 omap_i2c_reset(DEVICE(s
));
341 if ((value
& (1 << 15)) && !(value
& (1 << 10))) { /* MST */
342 fprintf(stderr
, "%s: I^2C slave mode not supported\n",
346 if ((value
& (1 << 15)) && value
& (1 << 8)) { /* XA */
347 fprintf(stderr
, "%s: 10-bit addressing mode not supported\n",
351 if ((value
& (1 << 15)) && value
& (1 << 0)) { /* STT */
352 nack
= !!i2c_start_transfer(s
->bus
, s
->addr
[1], /* SA */
353 (~value
>> 9) & 1); /* TRX */
354 s
->stat
|= nack
<< 1; /* NACK */
355 s
->control
&= ~(1 << 0); /* STT */
358 s
->control
&= ~(1 << 1); /* STP */
360 s
->count_cur
= s
->count
;
361 omap_i2c_fifo_run(s
);
363 omap_i2c_interrupts_update(s
);
367 case 0x28: /* I2C_OA */
368 s
->addr
[0] = value
& 0x3ff;
371 case 0x2c: /* I2C_SA */
372 s
->addr
[1] = value
& 0x3ff;
375 case 0x30: /* I2C_PSC */
379 case 0x34: /* I2C_SCLL */
383 case 0x38: /* I2C_SCLH */
387 case 0x3c: /* I2C_SYSTEST */
388 s
->test
= value
& 0xf80f;
389 if (value
& (1 << 11)) /* SBB */
390 if (s
->revision
>= OMAP2_INTR_REV
) {
392 omap_i2c_interrupts_update(s
);
394 if (value
& (1 << 15)) /* ST_EN */
395 fprintf(stderr
, "%s: System Test not supported\n", __FUNCTION__
);
404 static void omap_i2c_writeb(void *opaque
, hwaddr addr
,
407 OMAPI2CState
*s
= opaque
;
408 int offset
= addr
& OMAP_MPUI_REG_MASK
;
411 case 0x1c: /* I2C_DATA */
413 /* XXX: remote access (qualifier) error - what's that? */
418 s
->fifo
|= value
& 0xff;
419 s
->stat
&= ~(1 << 10); /* XUDF */
421 s
->stat
&= ~(1 << 4); /* XRDY */
422 omap_i2c_fifo_run(s
);
423 omap_i2c_interrupts_update(s
);
432 static const MemoryRegionOps omap_i2c_ops
= {
435 omap_badwidth_read16
,
437 omap_badwidth_read16
,
440 omap_i2c_writeb
, /* Only the last fifo write can be 8 bit. */
442 omap_badwidth_write16
,
445 .endianness
= DEVICE_NATIVE_ENDIAN
,
448 static int omap_i2c_init(SysBusDevice
*sbd
)
450 DeviceState
*dev
= DEVICE(sbd
);
451 OMAPI2CState
*s
= OMAP_I2C(dev
);
454 error_report("omap_i2c: fclk not connected");
457 if (s
->revision
>= OMAP2_INTR_REV
&& !s
->iclk
) {
458 /* Note that OMAP1 doesn't have a separate interface clock */
459 error_report("omap_i2c: iclk not connected");
463 sysbus_init_irq(sbd
, &s
->irq
);
464 sysbus_init_irq(sbd
, &s
->drq
[0]);
465 sysbus_init_irq(sbd
, &s
->drq
[1]);
466 memory_region_init_io(&s
->iomem
, OBJECT(s
), &omap_i2c_ops
, s
, "omap.i2c",
467 (s
->revision
< OMAP2_INTR_REV
) ? 0x800 : 0x1000);
468 sysbus_init_mmio(sbd
, &s
->iomem
);
469 s
->bus
= i2c_init_bus(dev
, NULL
);
473 static Property omap_i2c_properties
[] = {
474 DEFINE_PROP_UINT8("revision", OMAPI2CState
, revision
, 0),
475 DEFINE_PROP_PTR("iclk", OMAPI2CState
, iclk
),
476 DEFINE_PROP_PTR("fclk", OMAPI2CState
, fclk
),
477 DEFINE_PROP_END_OF_LIST(),
480 static void omap_i2c_class_init(ObjectClass
*klass
, void *data
)
482 DeviceClass
*dc
= DEVICE_CLASS(klass
);
483 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
484 k
->init
= omap_i2c_init
;
485 dc
->props
= omap_i2c_properties
;
486 dc
->reset
= omap_i2c_reset
;
487 /* Reason: pointer properties "iclk", "fclk" */
488 dc
->cannot_instantiate_with_device_add_yet
= true;
491 static const TypeInfo omap_i2c_info
= {
492 .name
= TYPE_OMAP_I2C
,
493 .parent
= TYPE_SYS_BUS_DEVICE
,
494 .instance_size
= sizeof(OMAPI2CState
),
495 .class_init
= omap_i2c_class_init
,
498 static void omap_i2c_register_types(void)
500 type_register_static(&omap_i2c_info
);
503 I2CBus
*omap_i2c_bus(DeviceState
*omap_i2c
)
505 OMAPI2CState
*s
= OMAP_I2C(omap_i2c
);
509 type_init(omap_i2c_register_types
)