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/>.
20 #include "qemu/osdep.h"
22 #include "qemu/module.h"
23 #include "hw/i2c/i2c.h"
25 #include "hw/qdev-properties.h"
26 #include "hw/arm/omap.h"
27 #include "hw/sysbus.h"
28 #include "qemu/error-report.h"
29 #include "qapi/error.h"
31 #define TYPE_OMAP_I2C "omap_i2c"
32 #define OMAP_I2C(obj) OBJECT_CHECK(OMAPI2CState, (obj), TYPE_OMAP_I2C)
34 typedef struct OMAPI2CState
{
35 SysBusDevice parent_obj
;
61 #define OMAP2_INTR_REV 0x34
62 #define OMAP2_GC_REV 0x34
64 static void omap_i2c_interrupts_update(OMAPI2CState
*s
)
66 qemu_set_irq(s
->irq
, s
->stat
& s
->mask
);
67 if ((s
->dma
>> 15) & 1) /* RDMA_EN */
68 qemu_set_irq(s
->drq
[0], (s
->stat
>> 3) & 1); /* RRDY */
69 if ((s
->dma
>> 7) & 1) /* XDMA_EN */
70 qemu_set_irq(s
->drq
[1], (s
->stat
>> 4) & 1); /* XRDY */
73 static void omap_i2c_fifo_run(OMAPI2CState
*s
)
77 if (!i2c_bus_busy(s
->bus
))
80 if ((s
->control
>> 2) & 1) { /* RM */
81 if ((s
->control
>> 1) & 1) { /* STP */
82 i2c_end_transfer(s
->bus
);
83 s
->control
&= ~(1 << 1); /* STP */
84 s
->count_cur
= s
->count
;
86 } else if ((s
->control
>> 9) & 1) { /* TRX */
87 while (ack
&& s
->txlen
)
88 ack
= (i2c_send(s
->bus
,
89 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
91 s
->stat
|= 1 << 4; /* XRDY */
94 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
95 s
->stat
|= 1 << 3; /* RRDY */
98 if ((s
->control
>> 9) & 1) { /* TRX */
99 while (ack
&& s
->count_cur
&& s
->txlen
) {
100 ack
= (i2c_send(s
->bus
,
101 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
105 if (ack
&& s
->count_cur
)
106 s
->stat
|= 1 << 4; /* XRDY */
108 s
->stat
&= ~(1 << 4); /* XRDY */
110 s
->stat
|= 1 << 2; /* ARDY */
111 s
->control
&= ~(1 << 10); /* MST */
114 while (s
->count_cur
&& s
->rxlen
< 4) {
115 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
119 s
->stat
|= 1 << 3; /* RRDY */
121 s
->stat
&= ~(1 << 3); /* RRDY */
124 if ((s
->control
>> 1) & 1) { /* STP */
125 i2c_end_transfer(s
->bus
);
126 s
->control
&= ~(1 << 1); /* STP */
127 s
->count_cur
= s
->count
;
130 s
->stat
|= 1 << 2; /* ARDY */
131 s
->control
&= ~(1 << 10); /* MST */
136 s
->stat
|= (!ack
) << 1; /* NACK */
138 s
->control
&= ~(1 << 1); /* STP */
141 static void omap_i2c_reset(DeviceState
*dev
)
143 OMAPI2CState
*s
= OMAP_I2C(dev
);
162 static uint32_t omap_i2c_read(void *opaque
, hwaddr addr
)
164 OMAPI2CState
*s
= opaque
;
165 int offset
= addr
& OMAP_MPUI_REG_MASK
;
169 case 0x00: /* I2C_REV */
170 return s
->revision
; /* REV */
172 case 0x04: /* I2C_IE */
175 case 0x08: /* I2C_STAT */
176 return s
->stat
| (i2c_bus_busy(s
->bus
) << 12);
178 case 0x0c: /* I2C_IV */
179 if (s
->revision
>= OMAP2_INTR_REV
)
181 ret
= ctz32(s
->stat
& s
->mask
);
188 omap_i2c_interrupts_update(s
);
191 case 0x10: /* I2C_SYSS */
192 return (s
->control
>> 15) & 1; /* I2C_EN */
194 case 0x14: /* I2C_BUF */
197 case 0x18: /* I2C_CNT */
198 return s
->count_cur
; /* DCOUNT */
200 case 0x1c: /* I2C_DATA */
202 if (s
->control
& (1 << 14)) { /* BE */
203 ret
|= ((s
->fifo
>> 0) & 0xff) << 8;
204 ret
|= ((s
->fifo
>> 8) & 0xff) << 0;
206 ret
|= ((s
->fifo
>> 8) & 0xff) << 8;
207 ret
|= ((s
->fifo
>> 0) & 0xff) << 0;
210 s
->stat
|= 1 << 15; /* SBD */
212 } else if (s
->rxlen
> 1) {
217 /* XXX: remote access (qualifier) error - what's that? */
220 s
->stat
&= ~(1 << 3); /* RRDY */
221 if (((s
->control
>> 10) & 1) && /* MST */
222 ((~s
->control
>> 9) & 1)) { /* TRX */
223 s
->stat
|= 1 << 2; /* ARDY */
224 s
->control
&= ~(1 << 10); /* MST */
227 s
->stat
&= ~(1 << 11); /* ROVR */
228 omap_i2c_fifo_run(s
);
229 omap_i2c_interrupts_update(s
);
232 case 0x20: /* I2C_SYSC */
235 case 0x24: /* I2C_CON */
238 case 0x28: /* I2C_OA */
241 case 0x2c: /* I2C_SA */
244 case 0x30: /* I2C_PSC */
247 case 0x34: /* I2C_SCLL */
250 case 0x38: /* I2C_SCLH */
253 case 0x3c: /* I2C_SYSTEST */
254 if (s
->test
& (1 << 15)) { /* ST_EN */
258 return s
->test
& ~0x300f;
265 static void omap_i2c_write(void *opaque
, hwaddr addr
,
268 OMAPI2CState
*s
= opaque
;
269 int offset
= addr
& OMAP_MPUI_REG_MASK
;
273 case 0x00: /* I2C_REV */
274 case 0x0c: /* I2C_IV */
275 case 0x10: /* I2C_SYSS */
279 case 0x04: /* I2C_IE */
280 s
->mask
= value
& (s
->revision
< OMAP2_GC_REV
? 0x1f : 0x3f);
283 case 0x08: /* I2C_STAT */
284 if (s
->revision
< OMAP2_INTR_REV
) {
289 /* RRDY and XRDY are reset by hardware. (in all versions???) */
290 s
->stat
&= ~(value
& 0x27);
291 omap_i2c_interrupts_update(s
);
294 case 0x14: /* I2C_BUF */
295 s
->dma
= value
& 0x8080;
296 if (value
& (1 << 15)) /* RDMA_EN */
297 s
->mask
&= ~(1 << 3); /* RRDY_IE */
298 if (value
& (1 << 7)) /* XDMA_EN */
299 s
->mask
&= ~(1 << 4); /* XRDY_IE */
302 case 0x18: /* I2C_CNT */
303 s
->count
= value
; /* DCOUNT */
306 case 0x1c: /* I2C_DATA */
308 /* XXX: remote access (qualifier) error - what's that? */
313 if (s
->control
& (1 << 14)) { /* BE */
314 s
->fifo
|= ((value
>> 8) & 0xff) << 8;
315 s
->fifo
|= ((value
>> 0) & 0xff) << 0;
317 s
->fifo
|= ((value
>> 0) & 0xff) << 8;
318 s
->fifo
|= ((value
>> 8) & 0xff) << 0;
320 s
->stat
&= ~(1 << 10); /* XUDF */
322 s
->stat
&= ~(1 << 4); /* XRDY */
323 omap_i2c_fifo_run(s
);
324 omap_i2c_interrupts_update(s
);
327 case 0x20: /* I2C_SYSC */
328 if (s
->revision
< OMAP2_INTR_REV
) {
334 omap_i2c_reset(DEVICE(s
));
338 case 0x24: /* I2C_CON */
339 s
->control
= value
& 0xcf87;
340 if (~value
& (1 << 15)) { /* I2C_EN */
341 if (s
->revision
< OMAP2_INTR_REV
) {
342 omap_i2c_reset(DEVICE(s
));
346 if ((value
& (1 << 15)) && !(value
& (1 << 10))) { /* MST */
347 qemu_log_mask(LOG_UNIMP
, "%s: I^2C slave mode not supported\n",
351 if ((value
& (1 << 15)) && value
& (1 << 8)) { /* XA */
352 qemu_log_mask(LOG_UNIMP
,
353 "%s: 10-bit addressing mode not supported\n",
357 if ((value
& (1 << 15)) && value
& (1 << 0)) { /* STT */
358 nack
= !!i2c_start_transfer(s
->bus
, s
->addr
[1], /* SA */
359 (~value
>> 9) & 1); /* TRX */
360 s
->stat
|= nack
<< 1; /* NACK */
361 s
->control
&= ~(1 << 0); /* STT */
364 s
->control
&= ~(1 << 1); /* STP */
366 s
->count_cur
= s
->count
;
367 omap_i2c_fifo_run(s
);
369 omap_i2c_interrupts_update(s
);
373 case 0x28: /* I2C_OA */
374 s
->addr
[0] = value
& 0x3ff;
377 case 0x2c: /* I2C_SA */
378 s
->addr
[1] = value
& 0x3ff;
381 case 0x30: /* I2C_PSC */
385 case 0x34: /* I2C_SCLL */
389 case 0x38: /* I2C_SCLH */
393 case 0x3c: /* I2C_SYSTEST */
394 s
->test
= value
& 0xf80f;
395 if (value
& (1 << 11)) /* SBB */
396 if (s
->revision
>= OMAP2_INTR_REV
) {
398 omap_i2c_interrupts_update(s
);
400 if (value
& (1 << 15)) { /* ST_EN */
401 qemu_log_mask(LOG_UNIMP
,
402 "%s: System Test not supported\n", __func__
);
412 static void omap_i2c_writeb(void *opaque
, hwaddr addr
,
415 OMAPI2CState
*s
= opaque
;
416 int offset
= addr
& OMAP_MPUI_REG_MASK
;
419 case 0x1c: /* I2C_DATA */
421 /* XXX: remote access (qualifier) error - what's that? */
426 s
->fifo
|= value
& 0xff;
427 s
->stat
&= ~(1 << 10); /* XUDF */
429 s
->stat
&= ~(1 << 4); /* XRDY */
430 omap_i2c_fifo_run(s
);
431 omap_i2c_interrupts_update(s
);
440 static uint64_t omap_i2c_readfn(void *opaque
, hwaddr addr
,
445 return omap_i2c_read(opaque
, addr
);
447 return omap_badwidth_read16(opaque
, addr
);
451 static void omap_i2c_writefn(void *opaque
, hwaddr addr
,
452 uint64_t value
, unsigned size
)
456 /* Only the last fifo write can be 8 bit. */
457 omap_i2c_writeb(opaque
, addr
, value
);
460 omap_i2c_write(opaque
, addr
, value
);
463 omap_badwidth_write16(opaque
, addr
, value
);
468 static const MemoryRegionOps omap_i2c_ops
= {
469 .read
= omap_i2c_readfn
,
470 .write
= omap_i2c_writefn
,
471 .valid
.min_access_size
= 1,
472 .valid
.max_access_size
= 4,
473 .endianness
= DEVICE_NATIVE_ENDIAN
,
476 static void omap_i2c_init(Object
*obj
)
478 DeviceState
*dev
= DEVICE(obj
);
479 OMAPI2CState
*s
= OMAP_I2C(obj
);
480 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
482 sysbus_init_irq(sbd
, &s
->irq
);
483 sysbus_init_irq(sbd
, &s
->drq
[0]);
484 sysbus_init_irq(sbd
, &s
->drq
[1]);
485 sysbus_init_mmio(sbd
, &s
->iomem
);
486 s
->bus
= i2c_init_bus(dev
, NULL
);
489 static void omap_i2c_realize(DeviceState
*dev
, Error
**errp
)
491 OMAPI2CState
*s
= OMAP_I2C(dev
);
493 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &omap_i2c_ops
, s
, "omap.i2c",
494 (s
->revision
< OMAP2_INTR_REV
) ? 0x800 : 0x1000);
497 error_setg(errp
, "omap_i2c: fclk not connected");
500 if (s
->revision
>= OMAP2_INTR_REV
&& !s
->iclk
) {
501 /* Note that OMAP1 doesn't have a separate interface clock */
502 error_setg(errp
, "omap_i2c: iclk not connected");
507 static Property omap_i2c_properties
[] = {
508 DEFINE_PROP_UINT8("revision", OMAPI2CState
, revision
, 0),
509 DEFINE_PROP_PTR("iclk", OMAPI2CState
, iclk
),
510 DEFINE_PROP_PTR("fclk", OMAPI2CState
, fclk
),
511 DEFINE_PROP_END_OF_LIST(),
514 static void omap_i2c_class_init(ObjectClass
*klass
, void *data
)
516 DeviceClass
*dc
= DEVICE_CLASS(klass
);
518 dc
->props
= omap_i2c_properties
;
519 dc
->reset
= omap_i2c_reset
;
520 /* Reason: pointer properties "iclk", "fclk" */
521 dc
->user_creatable
= false;
522 dc
->realize
= omap_i2c_realize
;
525 static const TypeInfo omap_i2c_info
= {
526 .name
= TYPE_OMAP_I2C
,
527 .parent
= TYPE_SYS_BUS_DEVICE
,
528 .instance_size
= sizeof(OMAPI2CState
),
529 .instance_init
= omap_i2c_init
,
530 .class_init
= omap_i2c_class_init
,
533 static void omap_i2c_register_types(void)
535 type_register_static(&omap_i2c_info
);
538 I2CBus
*omap_i2c_bus(DeviceState
*omap_i2c
)
540 OMAPI2CState
*s
= OMAP_I2C(omap_i2c
);
544 type_init(omap_i2c_register_types
)