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"
25 #include "qapi/error.h"
27 #define TYPE_OMAP_I2C "omap_i2c"
28 #define OMAP_I2C(obj) OBJECT_CHECK(OMAPI2CState, (obj), TYPE_OMAP_I2C)
30 typedef struct OMAPI2CState
{
31 SysBusDevice parent_obj
;
57 #define OMAP2_INTR_REV 0x34
58 #define OMAP2_GC_REV 0x34
60 static void omap_i2c_interrupts_update(OMAPI2CState
*s
)
62 qemu_set_irq(s
->irq
, s
->stat
& s
->mask
);
63 if ((s
->dma
>> 15) & 1) /* RDMA_EN */
64 qemu_set_irq(s
->drq
[0], (s
->stat
>> 3) & 1); /* RRDY */
65 if ((s
->dma
>> 7) & 1) /* XDMA_EN */
66 qemu_set_irq(s
->drq
[1], (s
->stat
>> 4) & 1); /* XRDY */
69 static void omap_i2c_fifo_run(OMAPI2CState
*s
)
73 if (!i2c_bus_busy(s
->bus
))
76 if ((s
->control
>> 2) & 1) { /* RM */
77 if ((s
->control
>> 1) & 1) { /* STP */
78 i2c_end_transfer(s
->bus
);
79 s
->control
&= ~(1 << 1); /* STP */
80 s
->count_cur
= s
->count
;
82 } else if ((s
->control
>> 9) & 1) { /* TRX */
83 while (ack
&& s
->txlen
)
84 ack
= (i2c_send(s
->bus
,
85 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
87 s
->stat
|= 1 << 4; /* XRDY */
90 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
91 s
->stat
|= 1 << 3; /* RRDY */
94 if ((s
->control
>> 9) & 1) { /* TRX */
95 while (ack
&& s
->count_cur
&& s
->txlen
) {
96 ack
= (i2c_send(s
->bus
,
97 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
101 if (ack
&& s
->count_cur
)
102 s
->stat
|= 1 << 4; /* XRDY */
104 s
->stat
&= ~(1 << 4); /* XRDY */
106 s
->stat
|= 1 << 2; /* ARDY */
107 s
->control
&= ~(1 << 10); /* MST */
110 while (s
->count_cur
&& s
->rxlen
< 4) {
111 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
115 s
->stat
|= 1 << 3; /* RRDY */
117 s
->stat
&= ~(1 << 3); /* RRDY */
120 if ((s
->control
>> 1) & 1) { /* STP */
121 i2c_end_transfer(s
->bus
);
122 s
->control
&= ~(1 << 1); /* STP */
123 s
->count_cur
= s
->count
;
126 s
->stat
|= 1 << 2; /* ARDY */
127 s
->control
&= ~(1 << 10); /* MST */
132 s
->stat
|= (!ack
) << 1; /* NACK */
134 s
->control
&= ~(1 << 1); /* STP */
137 static void omap_i2c_reset(DeviceState
*dev
)
139 OMAPI2CState
*s
= OMAP_I2C(dev
);
158 static uint32_t omap_i2c_read(void *opaque
, hwaddr addr
)
160 OMAPI2CState
*s
= opaque
;
161 int offset
= addr
& OMAP_MPUI_REG_MASK
;
165 case 0x00: /* I2C_REV */
166 return s
->revision
; /* REV */
168 case 0x04: /* I2C_IE */
171 case 0x08: /* I2C_STAT */
172 return s
->stat
| (i2c_bus_busy(s
->bus
) << 12);
174 case 0x0c: /* I2C_IV */
175 if (s
->revision
>= OMAP2_INTR_REV
)
177 ret
= ctz32(s
->stat
& s
->mask
);
184 omap_i2c_interrupts_update(s
);
187 case 0x10: /* I2C_SYSS */
188 return (s
->control
>> 15) & 1; /* I2C_EN */
190 case 0x14: /* I2C_BUF */
193 case 0x18: /* I2C_CNT */
194 return s
->count_cur
; /* DCOUNT */
196 case 0x1c: /* I2C_DATA */
198 if (s
->control
& (1 << 14)) { /* BE */
199 ret
|= ((s
->fifo
>> 0) & 0xff) << 8;
200 ret
|= ((s
->fifo
>> 8) & 0xff) << 0;
202 ret
|= ((s
->fifo
>> 8) & 0xff) << 8;
203 ret
|= ((s
->fifo
>> 0) & 0xff) << 0;
206 s
->stat
|= 1 << 15; /* SBD */
208 } else if (s
->rxlen
> 1) {
213 /* XXX: remote access (qualifier) error - what's that? */
216 s
->stat
&= ~(1 << 3); /* RRDY */
217 if (((s
->control
>> 10) & 1) && /* MST */
218 ((~s
->control
>> 9) & 1)) { /* TRX */
219 s
->stat
|= 1 << 2; /* ARDY */
220 s
->control
&= ~(1 << 10); /* MST */
223 s
->stat
&= ~(1 << 11); /* ROVR */
224 omap_i2c_fifo_run(s
);
225 omap_i2c_interrupts_update(s
);
228 case 0x20: /* I2C_SYSC */
231 case 0x24: /* I2C_CON */
234 case 0x28: /* I2C_OA */
237 case 0x2c: /* I2C_SA */
240 case 0x30: /* I2C_PSC */
243 case 0x34: /* I2C_SCLL */
246 case 0x38: /* I2C_SCLH */
249 case 0x3c: /* I2C_SYSTEST */
250 if (s
->test
& (1 << 15)) { /* ST_EN */
254 return s
->test
& ~0x300f;
261 static void omap_i2c_write(void *opaque
, hwaddr addr
,
264 OMAPI2CState
*s
= opaque
;
265 int offset
= addr
& OMAP_MPUI_REG_MASK
;
269 case 0x00: /* I2C_REV */
270 case 0x0c: /* I2C_IV */
271 case 0x10: /* I2C_SYSS */
275 case 0x04: /* I2C_IE */
276 s
->mask
= value
& (s
->revision
< OMAP2_GC_REV
? 0x1f : 0x3f);
279 case 0x08: /* I2C_STAT */
280 if (s
->revision
< OMAP2_INTR_REV
) {
285 /* RRDY and XRDY are reset by hardware. (in all versions???) */
286 s
->stat
&= ~(value
& 0x27);
287 omap_i2c_interrupts_update(s
);
290 case 0x14: /* I2C_BUF */
291 s
->dma
= value
& 0x8080;
292 if (value
& (1 << 15)) /* RDMA_EN */
293 s
->mask
&= ~(1 << 3); /* RRDY_IE */
294 if (value
& (1 << 7)) /* XDMA_EN */
295 s
->mask
&= ~(1 << 4); /* XRDY_IE */
298 case 0x18: /* I2C_CNT */
299 s
->count
= value
; /* DCOUNT */
302 case 0x1c: /* I2C_DATA */
304 /* XXX: remote access (qualifier) error - what's that? */
309 if (s
->control
& (1 << 14)) { /* BE */
310 s
->fifo
|= ((value
>> 8) & 0xff) << 8;
311 s
->fifo
|= ((value
>> 0) & 0xff) << 0;
313 s
->fifo
|= ((value
>> 0) & 0xff) << 8;
314 s
->fifo
|= ((value
>> 8) & 0xff) << 0;
316 s
->stat
&= ~(1 << 10); /* XUDF */
318 s
->stat
&= ~(1 << 4); /* XRDY */
319 omap_i2c_fifo_run(s
);
320 omap_i2c_interrupts_update(s
);
323 case 0x20: /* I2C_SYSC */
324 if (s
->revision
< OMAP2_INTR_REV
) {
330 omap_i2c_reset(DEVICE(s
));
334 case 0x24: /* I2C_CON */
335 s
->control
= value
& 0xcf87;
336 if (~value
& (1 << 15)) { /* I2C_EN */
337 if (s
->revision
< OMAP2_INTR_REV
) {
338 omap_i2c_reset(DEVICE(s
));
342 if ((value
& (1 << 15)) && !(value
& (1 << 10))) { /* MST */
343 fprintf(stderr
, "%s: I^2C slave mode not supported\n",
347 if ((value
& (1 << 15)) && value
& (1 << 8)) { /* XA */
348 fprintf(stderr
, "%s: 10-bit addressing mode not supported\n",
352 if ((value
& (1 << 15)) && value
& (1 << 0)) { /* STT */
353 nack
= !!i2c_start_transfer(s
->bus
, s
->addr
[1], /* SA */
354 (~value
>> 9) & 1); /* TRX */
355 s
->stat
|= nack
<< 1; /* NACK */
356 s
->control
&= ~(1 << 0); /* STT */
359 s
->control
&= ~(1 << 1); /* STP */
361 s
->count_cur
= s
->count
;
362 omap_i2c_fifo_run(s
);
364 omap_i2c_interrupts_update(s
);
368 case 0x28: /* I2C_OA */
369 s
->addr
[0] = value
& 0x3ff;
372 case 0x2c: /* I2C_SA */
373 s
->addr
[1] = value
& 0x3ff;
376 case 0x30: /* I2C_PSC */
380 case 0x34: /* I2C_SCLL */
384 case 0x38: /* I2C_SCLH */
388 case 0x3c: /* I2C_SYSTEST */
389 s
->test
= value
& 0xf80f;
390 if (value
& (1 << 11)) /* SBB */
391 if (s
->revision
>= OMAP2_INTR_REV
) {
393 omap_i2c_interrupts_update(s
);
395 if (value
& (1 << 15)) /* ST_EN */
396 fprintf(stderr
, "%s: System Test not supported\n", __FUNCTION__
);
405 static void omap_i2c_writeb(void *opaque
, hwaddr addr
,
408 OMAPI2CState
*s
= opaque
;
409 int offset
= addr
& OMAP_MPUI_REG_MASK
;
412 case 0x1c: /* I2C_DATA */
414 /* XXX: remote access (qualifier) error - what's that? */
419 s
->fifo
|= value
& 0xff;
420 s
->stat
&= ~(1 << 10); /* XUDF */
422 s
->stat
&= ~(1 << 4); /* XRDY */
423 omap_i2c_fifo_run(s
);
424 omap_i2c_interrupts_update(s
);
433 static const MemoryRegionOps omap_i2c_ops
= {
436 omap_badwidth_read16
,
438 omap_badwidth_read16
,
441 omap_i2c_writeb
, /* Only the last fifo write can be 8 bit. */
443 omap_badwidth_write16
,
446 .endianness
= DEVICE_NATIVE_ENDIAN
,
449 static void omap_i2c_init(Object
*obj
)
451 DeviceState
*dev
= DEVICE(obj
);
452 OMAPI2CState
*s
= OMAP_I2C(obj
);
453 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
455 sysbus_init_irq(sbd
, &s
->irq
);
456 sysbus_init_irq(sbd
, &s
->drq
[0]);
457 sysbus_init_irq(sbd
, &s
->drq
[1]);
458 sysbus_init_mmio(sbd
, &s
->iomem
);
459 s
->bus
= i2c_init_bus(dev
, NULL
);
462 static void omap_i2c_realize(DeviceState
*dev
, Error
**errp
)
464 OMAPI2CState
*s
= OMAP_I2C(dev
);
466 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &omap_i2c_ops
, s
, "omap.i2c",
467 (s
->revision
< OMAP2_INTR_REV
) ? 0x800 : 0x1000);
470 error_setg(errp
, "omap_i2c: fclk not connected");
473 if (s
->revision
>= OMAP2_INTR_REV
&& !s
->iclk
) {
474 /* Note that OMAP1 doesn't have a separate interface clock */
475 error_setg(errp
, "omap_i2c: iclk not connected");
480 static Property omap_i2c_properties
[] = {
481 DEFINE_PROP_UINT8("revision", OMAPI2CState
, revision
, 0),
482 DEFINE_PROP_PTR("iclk", OMAPI2CState
, iclk
),
483 DEFINE_PROP_PTR("fclk", OMAPI2CState
, fclk
),
484 DEFINE_PROP_END_OF_LIST(),
487 static void omap_i2c_class_init(ObjectClass
*klass
, void *data
)
489 DeviceClass
*dc
= DEVICE_CLASS(klass
);
491 dc
->props
= omap_i2c_properties
;
492 dc
->reset
= omap_i2c_reset
;
493 /* Reason: pointer properties "iclk", "fclk" */
494 dc
->user_creatable
= false;
495 dc
->realize
= omap_i2c_realize
;
498 static const TypeInfo omap_i2c_info
= {
499 .name
= TYPE_OMAP_I2C
,
500 .parent
= TYPE_SYS_BUS_DEVICE
,
501 .instance_size
= sizeof(OMAPI2CState
),
502 .instance_init
= omap_i2c_init
,
503 .class_init
= omap_i2c_class_init
,
506 static void omap_i2c_register_types(void)
508 type_register_static(&omap_i2c_info
);
511 I2CBus
*omap_i2c_bus(DeviceState
*omap_i2c
)
513 OMAPI2CState
*s
= OMAP_I2C(omap_i2c
);
517 type_init(omap_i2c_register_types
)