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 "hw/i2c/i2c.h"
21 #include "hw/arm/omap.h"
22 #include "hw/sysbus.h"
24 #define TYPE_OMAP_I2C "omap_i2c"
25 #define OMAP_I2C(obj) OBJECT_CHECK(OMAPI2CState, (obj), TYPE_OMAP_I2C)
27 typedef struct OMAPI2CState
{
28 SysBusDevice parent_obj
;
54 #define OMAP2_INTR_REV 0x34
55 #define OMAP2_GC_REV 0x34
57 static void omap_i2c_interrupts_update(OMAPI2CState
*s
)
59 qemu_set_irq(s
->irq
, s
->stat
& s
->mask
);
60 if ((s
->dma
>> 15) & 1) /* RDMA_EN */
61 qemu_set_irq(s
->drq
[0], (s
->stat
>> 3) & 1); /* RRDY */
62 if ((s
->dma
>> 7) & 1) /* XDMA_EN */
63 qemu_set_irq(s
->drq
[1], (s
->stat
>> 4) & 1); /* XRDY */
66 static void omap_i2c_fifo_run(OMAPI2CState
*s
)
70 if (!i2c_bus_busy(s
->bus
))
73 if ((s
->control
>> 2) & 1) { /* RM */
74 if ((s
->control
>> 1) & 1) { /* STP */
75 i2c_end_transfer(s
->bus
);
76 s
->control
&= ~(1 << 1); /* STP */
77 s
->count_cur
= s
->count
;
79 } else if ((s
->control
>> 9) & 1) { /* TRX */
80 while (ack
&& s
->txlen
)
81 ack
= (i2c_send(s
->bus
,
82 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
84 s
->stat
|= 1 << 4; /* XRDY */
87 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
88 s
->stat
|= 1 << 3; /* RRDY */
91 if ((s
->control
>> 9) & 1) { /* TRX */
92 while (ack
&& s
->count_cur
&& s
->txlen
) {
93 ack
= (i2c_send(s
->bus
,
94 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
98 if (ack
&& s
->count_cur
)
99 s
->stat
|= 1 << 4; /* XRDY */
101 s
->stat
&= ~(1 << 4); /* XRDY */
103 s
->stat
|= 1 << 2; /* ARDY */
104 s
->control
&= ~(1 << 10); /* MST */
107 while (s
->count_cur
&& s
->rxlen
< 4) {
108 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
112 s
->stat
|= 1 << 3; /* RRDY */
114 s
->stat
&= ~(1 << 3); /* RRDY */
117 if ((s
->control
>> 1) & 1) { /* STP */
118 i2c_end_transfer(s
->bus
);
119 s
->control
&= ~(1 << 1); /* STP */
120 s
->count_cur
= s
->count
;
123 s
->stat
|= 1 << 2; /* ARDY */
124 s
->control
&= ~(1 << 10); /* MST */
129 s
->stat
|= (!ack
) << 1; /* NACK */
131 s
->control
&= ~(1 << 1); /* STP */
134 static void omap_i2c_reset(DeviceState
*dev
)
136 OMAPI2CState
*s
= OMAP_I2C(dev
);
155 static uint32_t omap_i2c_read(void *opaque
, hwaddr addr
)
157 OMAPI2CState
*s
= opaque
;
158 int offset
= addr
& OMAP_MPUI_REG_MASK
;
162 case 0x00: /* I2C_REV */
163 return s
->revision
; /* REV */
165 case 0x04: /* I2C_IE */
168 case 0x08: /* I2C_STAT */
169 return s
->stat
| (i2c_bus_busy(s
->bus
) << 12);
171 case 0x0c: /* I2C_IV */
172 if (s
->revision
>= OMAP2_INTR_REV
)
174 ret
= ctz32(s
->stat
& s
->mask
);
181 omap_i2c_interrupts_update(s
);
184 case 0x10: /* I2C_SYSS */
185 return (s
->control
>> 15) & 1; /* I2C_EN */
187 case 0x14: /* I2C_BUF */
190 case 0x18: /* I2C_CNT */
191 return s
->count_cur
; /* DCOUNT */
193 case 0x1c: /* I2C_DATA */
195 if (s
->control
& (1 << 14)) { /* BE */
196 ret
|= ((s
->fifo
>> 0) & 0xff) << 8;
197 ret
|= ((s
->fifo
>> 8) & 0xff) << 0;
199 ret
|= ((s
->fifo
>> 8) & 0xff) << 8;
200 ret
|= ((s
->fifo
>> 0) & 0xff) << 0;
203 s
->stat
|= 1 << 15; /* SBD */
205 } else if (s
->rxlen
> 1) {
210 /* XXX: remote access (qualifier) error - what's that? */
213 s
->stat
&= ~(1 << 3); /* RRDY */
214 if (((s
->control
>> 10) & 1) && /* MST */
215 ((~s
->control
>> 9) & 1)) { /* TRX */
216 s
->stat
|= 1 << 2; /* ARDY */
217 s
->control
&= ~(1 << 10); /* MST */
220 s
->stat
&= ~(1 << 11); /* ROVR */
221 omap_i2c_fifo_run(s
);
222 omap_i2c_interrupts_update(s
);
225 case 0x20: /* I2C_SYSC */
228 case 0x24: /* I2C_CON */
231 case 0x28: /* I2C_OA */
234 case 0x2c: /* I2C_SA */
237 case 0x30: /* I2C_PSC */
240 case 0x34: /* I2C_SCLL */
243 case 0x38: /* I2C_SCLH */
246 case 0x3c: /* I2C_SYSTEST */
247 if (s
->test
& (1 << 15)) { /* ST_EN */
251 return s
->test
& ~0x300f;
258 static void omap_i2c_write(void *opaque
, hwaddr addr
,
261 OMAPI2CState
*s
= opaque
;
262 int offset
= addr
& OMAP_MPUI_REG_MASK
;
266 case 0x00: /* I2C_REV */
267 case 0x0c: /* I2C_IV */
268 case 0x10: /* I2C_SYSS */
272 case 0x04: /* I2C_IE */
273 s
->mask
= value
& (s
->revision
< OMAP2_GC_REV
? 0x1f : 0x3f);
276 case 0x08: /* I2C_STAT */
277 if (s
->revision
< OMAP2_INTR_REV
) {
282 /* RRDY and XRDY are reset by hardware. (in all versions???) */
283 s
->stat
&= ~(value
& 0x27);
284 omap_i2c_interrupts_update(s
);
287 case 0x14: /* I2C_BUF */
288 s
->dma
= value
& 0x8080;
289 if (value
& (1 << 15)) /* RDMA_EN */
290 s
->mask
&= ~(1 << 3); /* RRDY_IE */
291 if (value
& (1 << 7)) /* XDMA_EN */
292 s
->mask
&= ~(1 << 4); /* XRDY_IE */
295 case 0x18: /* I2C_CNT */
296 s
->count
= value
; /* DCOUNT */
299 case 0x1c: /* I2C_DATA */
301 /* XXX: remote access (qualifier) error - what's that? */
306 if (s
->control
& (1 << 14)) { /* BE */
307 s
->fifo
|= ((value
>> 8) & 0xff) << 8;
308 s
->fifo
|= ((value
>> 0) & 0xff) << 0;
310 s
->fifo
|= ((value
>> 0) & 0xff) << 8;
311 s
->fifo
|= ((value
>> 8) & 0xff) << 0;
313 s
->stat
&= ~(1 << 10); /* XUDF */
315 s
->stat
&= ~(1 << 4); /* XRDY */
316 omap_i2c_fifo_run(s
);
317 omap_i2c_interrupts_update(s
);
320 case 0x20: /* I2C_SYSC */
321 if (s
->revision
< OMAP2_INTR_REV
) {
327 omap_i2c_reset(DEVICE(s
));
331 case 0x24: /* I2C_CON */
332 s
->control
= value
& 0xcf87;
333 if (~value
& (1 << 15)) { /* I2C_EN */
334 if (s
->revision
< OMAP2_INTR_REV
) {
335 omap_i2c_reset(DEVICE(s
));
339 if ((value
& (1 << 15)) && !(value
& (1 << 10))) { /* MST */
340 fprintf(stderr
, "%s: I^2C slave mode not supported\n",
344 if ((value
& (1 << 15)) && value
& (1 << 8)) { /* XA */
345 fprintf(stderr
, "%s: 10-bit addressing mode not supported\n",
349 if ((value
& (1 << 15)) && value
& (1 << 0)) { /* STT */
350 nack
= !!i2c_start_transfer(s
->bus
, s
->addr
[1], /* SA */
351 (~value
>> 9) & 1); /* TRX */
352 s
->stat
|= nack
<< 1; /* NACK */
353 s
->control
&= ~(1 << 0); /* STT */
356 s
->control
&= ~(1 << 1); /* STP */
358 s
->count_cur
= s
->count
;
359 omap_i2c_fifo_run(s
);
361 omap_i2c_interrupts_update(s
);
365 case 0x28: /* I2C_OA */
366 s
->addr
[0] = value
& 0x3ff;
369 case 0x2c: /* I2C_SA */
370 s
->addr
[1] = value
& 0x3ff;
373 case 0x30: /* I2C_PSC */
377 case 0x34: /* I2C_SCLL */
381 case 0x38: /* I2C_SCLH */
385 case 0x3c: /* I2C_SYSTEST */
386 s
->test
= value
& 0xf80f;
387 if (value
& (1 << 11)) /* SBB */
388 if (s
->revision
>= OMAP2_INTR_REV
) {
390 omap_i2c_interrupts_update(s
);
392 if (value
& (1 << 15)) /* ST_EN */
393 fprintf(stderr
, "%s: System Test not supported\n", __FUNCTION__
);
402 static void omap_i2c_writeb(void *opaque
, hwaddr addr
,
405 OMAPI2CState
*s
= opaque
;
406 int offset
= addr
& OMAP_MPUI_REG_MASK
;
409 case 0x1c: /* I2C_DATA */
411 /* XXX: remote access (qualifier) error - what's that? */
416 s
->fifo
|= value
& 0xff;
417 s
->stat
&= ~(1 << 10); /* XUDF */
419 s
->stat
&= ~(1 << 4); /* XRDY */
420 omap_i2c_fifo_run(s
);
421 omap_i2c_interrupts_update(s
);
430 static const MemoryRegionOps omap_i2c_ops
= {
433 omap_badwidth_read16
,
435 omap_badwidth_read16
,
438 omap_i2c_writeb
, /* Only the last fifo write can be 8 bit. */
440 omap_badwidth_write16
,
443 .endianness
= DEVICE_NATIVE_ENDIAN
,
446 static int omap_i2c_init(SysBusDevice
*sbd
)
448 DeviceState
*dev
= DEVICE(sbd
);
449 OMAPI2CState
*s
= OMAP_I2C(dev
);
452 hw_error("omap_i2c: fclk not connected\n");
454 if (s
->revision
>= OMAP2_INTR_REV
&& !s
->iclk
) {
455 /* Note that OMAP1 doesn't have a separate interface clock */
456 hw_error("omap_i2c: iclk not connected\n");
458 sysbus_init_irq(sbd
, &s
->irq
);
459 sysbus_init_irq(sbd
, &s
->drq
[0]);
460 sysbus_init_irq(sbd
, &s
->drq
[1]);
461 memory_region_init_io(&s
->iomem
, OBJECT(s
), &omap_i2c_ops
, s
, "omap.i2c",
462 (s
->revision
< OMAP2_INTR_REV
) ? 0x800 : 0x1000);
463 sysbus_init_mmio(sbd
, &s
->iomem
);
464 s
->bus
= i2c_init_bus(dev
, NULL
);
468 static Property omap_i2c_properties
[] = {
469 DEFINE_PROP_UINT8("revision", OMAPI2CState
, revision
, 0),
470 DEFINE_PROP_PTR("iclk", OMAPI2CState
, iclk
),
471 DEFINE_PROP_PTR("fclk", OMAPI2CState
, fclk
),
472 DEFINE_PROP_END_OF_LIST(),
475 static void omap_i2c_class_init(ObjectClass
*klass
, void *data
)
477 DeviceClass
*dc
= DEVICE_CLASS(klass
);
478 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
479 k
->init
= omap_i2c_init
;
480 dc
->props
= omap_i2c_properties
;
481 dc
->reset
= omap_i2c_reset
;
482 /* Reason: pointer properties "iclk", "fclk" */
483 dc
->cannot_instantiate_with_device_add_yet
= true;
486 static const TypeInfo omap_i2c_info
= {
487 .name
= TYPE_OMAP_I2C
,
488 .parent
= TYPE_SYS_BUS_DEVICE
,
489 .instance_size
= sizeof(OMAPI2CState
),
490 .class_init
= omap_i2c_class_init
,
493 static void omap_i2c_register_types(void)
495 type_register_static(&omap_i2c_info
);
498 I2CBus
*omap_i2c_bus(DeviceState
*omap_i2c
)
500 OMAPI2CState
*s
= OMAP_I2C(omap_i2c
);
504 type_init(omap_i2c_register_types
)