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"
32 SysBusDevice parent_obj
;
58 #define OMAP2_INTR_REV 0x34
59 #define OMAP2_GC_REV 0x34
61 static void omap_i2c_interrupts_update(OMAPI2CState
*s
)
63 qemu_set_irq(s
->irq
, s
->stat
& s
->mask
);
64 if ((s
->dma
>> 15) & 1) /* RDMA_EN */
65 qemu_set_irq(s
->drq
[0], (s
->stat
>> 3) & 1); /* RRDY */
66 if ((s
->dma
>> 7) & 1) /* XDMA_EN */
67 qemu_set_irq(s
->drq
[1], (s
->stat
>> 4) & 1); /* XRDY */
70 static void omap_i2c_fifo_run(OMAPI2CState
*s
)
74 if (!i2c_bus_busy(s
->bus
))
77 if ((s
->control
>> 2) & 1) { /* RM */
78 if ((s
->control
>> 1) & 1) { /* STP */
79 i2c_end_transfer(s
->bus
);
80 s
->control
&= ~(1 << 1); /* STP */
81 s
->count_cur
= s
->count
;
83 } else if ((s
->control
>> 9) & 1) { /* TRX */
84 while (ack
&& s
->txlen
)
85 ack
= (i2c_send(s
->bus
,
86 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
88 s
->stat
|= 1 << 4; /* XRDY */
91 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
92 s
->stat
|= 1 << 3; /* RRDY */
95 if ((s
->control
>> 9) & 1) { /* TRX */
96 while (ack
&& s
->count_cur
&& s
->txlen
) {
97 ack
= (i2c_send(s
->bus
,
98 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
102 if (ack
&& s
->count_cur
)
103 s
->stat
|= 1 << 4; /* XRDY */
105 s
->stat
&= ~(1 << 4); /* XRDY */
107 s
->stat
|= 1 << 2; /* ARDY */
108 s
->control
&= ~(1 << 10); /* MST */
111 while (s
->count_cur
&& s
->rxlen
< 4) {
112 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
116 s
->stat
|= 1 << 3; /* RRDY */
118 s
->stat
&= ~(1 << 3); /* RRDY */
121 if ((s
->control
>> 1) & 1) { /* STP */
122 i2c_end_transfer(s
->bus
);
123 s
->control
&= ~(1 << 1); /* STP */
124 s
->count_cur
= s
->count
;
127 s
->stat
|= 1 << 2; /* ARDY */
128 s
->control
&= ~(1 << 10); /* MST */
133 s
->stat
|= (!ack
) << 1; /* NACK */
135 s
->control
&= ~(1 << 1); /* STP */
138 static void omap_i2c_reset(DeviceState
*dev
)
140 OMAPI2CState
*s
= OMAP_I2C(dev
);
159 static uint32_t omap_i2c_read(void *opaque
, hwaddr addr
)
161 OMAPI2CState
*s
= opaque
;
162 int offset
= addr
& OMAP_MPUI_REG_MASK
;
166 case 0x00: /* I2C_REV */
167 return s
->revision
; /* REV */
169 case 0x04: /* I2C_IE */
172 case 0x08: /* I2C_STAT */
173 return s
->stat
| (i2c_bus_busy(s
->bus
) << 12);
175 case 0x0c: /* I2C_IV */
176 if (s
->revision
>= OMAP2_INTR_REV
)
178 ret
= ctz32(s
->stat
& s
->mask
);
185 omap_i2c_interrupts_update(s
);
188 case 0x10: /* I2C_SYSS */
189 return (s
->control
>> 15) & 1; /* I2C_EN */
191 case 0x14: /* I2C_BUF */
194 case 0x18: /* I2C_CNT */
195 return s
->count_cur
; /* DCOUNT */
197 case 0x1c: /* I2C_DATA */
199 if (s
->control
& (1 << 14)) { /* BE */
200 ret
|= ((s
->fifo
>> 0) & 0xff) << 8;
201 ret
|= ((s
->fifo
>> 8) & 0xff) << 0;
203 ret
|= ((s
->fifo
>> 8) & 0xff) << 8;
204 ret
|= ((s
->fifo
>> 0) & 0xff) << 0;
207 s
->stat
|= 1 << 15; /* SBD */
209 } else if (s
->rxlen
> 1) {
214 /* XXX: remote access (qualifier) error - what's that? */
217 s
->stat
&= ~(1 << 3); /* RRDY */
218 if (((s
->control
>> 10) & 1) && /* MST */
219 ((~s
->control
>> 9) & 1)) { /* TRX */
220 s
->stat
|= 1 << 2; /* ARDY */
221 s
->control
&= ~(1 << 10); /* MST */
224 s
->stat
&= ~(1 << 11); /* ROVR */
225 omap_i2c_fifo_run(s
);
226 omap_i2c_interrupts_update(s
);
229 case 0x20: /* I2C_SYSC */
232 case 0x24: /* I2C_CON */
235 case 0x28: /* I2C_OA */
238 case 0x2c: /* I2C_SA */
241 case 0x30: /* I2C_PSC */
244 case 0x34: /* I2C_SCLL */
247 case 0x38: /* I2C_SCLH */
250 case 0x3c: /* I2C_SYSTEST */
251 if (s
->test
& (1 << 15)) { /* ST_EN */
255 return s
->test
& ~0x300f;
262 static void omap_i2c_write(void *opaque
, hwaddr addr
,
265 OMAPI2CState
*s
= opaque
;
266 int offset
= addr
& OMAP_MPUI_REG_MASK
;
270 case 0x00: /* I2C_REV */
271 case 0x0c: /* I2C_IV */
272 case 0x10: /* I2C_SYSS */
276 case 0x04: /* I2C_IE */
277 s
->mask
= value
& (s
->revision
< OMAP2_GC_REV
? 0x1f : 0x3f);
280 case 0x08: /* I2C_STAT */
281 if (s
->revision
< OMAP2_INTR_REV
) {
286 /* RRDY and XRDY are reset by hardware. (in all versions???) */
287 s
->stat
&= ~(value
& 0x27);
288 omap_i2c_interrupts_update(s
);
291 case 0x14: /* I2C_BUF */
292 s
->dma
= value
& 0x8080;
293 if (value
& (1 << 15)) /* RDMA_EN */
294 s
->mask
&= ~(1 << 3); /* RRDY_IE */
295 if (value
& (1 << 7)) /* XDMA_EN */
296 s
->mask
&= ~(1 << 4); /* XRDY_IE */
299 case 0x18: /* I2C_CNT */
300 s
->count
= value
; /* DCOUNT */
303 case 0x1c: /* I2C_DATA */
305 /* XXX: remote access (qualifier) error - what's that? */
310 if (s
->control
& (1 << 14)) { /* BE */
311 s
->fifo
|= ((value
>> 8) & 0xff) << 8;
312 s
->fifo
|= ((value
>> 0) & 0xff) << 0;
314 s
->fifo
|= ((value
>> 0) & 0xff) << 8;
315 s
->fifo
|= ((value
>> 8) & 0xff) << 0;
317 s
->stat
&= ~(1 << 10); /* XUDF */
319 s
->stat
&= ~(1 << 4); /* XRDY */
320 omap_i2c_fifo_run(s
);
321 omap_i2c_interrupts_update(s
);
324 case 0x20: /* I2C_SYSC */
325 if (s
->revision
< OMAP2_INTR_REV
) {
331 omap_i2c_reset(DEVICE(s
));
335 case 0x24: /* I2C_CON */
336 s
->control
= value
& 0xcf87;
337 if (~value
& (1 << 15)) { /* I2C_EN */
338 if (s
->revision
< OMAP2_INTR_REV
) {
339 omap_i2c_reset(DEVICE(s
));
343 if ((value
& (1 << 15)) && !(value
& (1 << 10))) { /* MST */
344 qemu_log_mask(LOG_UNIMP
, "%s: I^2C slave mode not supported\n",
348 if ((value
& (1 << 15)) && value
& (1 << 8)) { /* XA */
349 qemu_log_mask(LOG_UNIMP
,
350 "%s: 10-bit addressing mode not supported\n",
354 if ((value
& (1 << 15)) && value
& (1 << 0)) { /* STT */
355 nack
= !!i2c_start_transfer(s
->bus
, s
->addr
[1], /* SA */
356 (~value
>> 9) & 1); /* TRX */
357 s
->stat
|= nack
<< 1; /* NACK */
358 s
->control
&= ~(1 << 0); /* STT */
361 s
->control
&= ~(1 << 1); /* STP */
363 s
->count_cur
= s
->count
;
364 omap_i2c_fifo_run(s
);
366 omap_i2c_interrupts_update(s
);
370 case 0x28: /* I2C_OA */
371 s
->addr
[0] = value
& 0x3ff;
374 case 0x2c: /* I2C_SA */
375 s
->addr
[1] = value
& 0x3ff;
378 case 0x30: /* I2C_PSC */
382 case 0x34: /* I2C_SCLL */
386 case 0x38: /* I2C_SCLH */
390 case 0x3c: /* I2C_SYSTEST */
391 s
->test
= value
& 0xf80f;
392 if (value
& (1 << 11)) /* SBB */
393 if (s
->revision
>= OMAP2_INTR_REV
) {
395 omap_i2c_interrupts_update(s
);
397 if (value
& (1 << 15)) { /* ST_EN */
398 qemu_log_mask(LOG_UNIMP
,
399 "%s: System Test not supported\n", __func__
);
409 static void omap_i2c_writeb(void *opaque
, hwaddr addr
,
412 OMAPI2CState
*s
= opaque
;
413 int offset
= addr
& OMAP_MPUI_REG_MASK
;
416 case 0x1c: /* I2C_DATA */
418 /* XXX: remote access (qualifier) error - what's that? */
423 s
->fifo
|= value
& 0xff;
424 s
->stat
&= ~(1 << 10); /* XUDF */
426 s
->stat
&= ~(1 << 4); /* XRDY */
427 omap_i2c_fifo_run(s
);
428 omap_i2c_interrupts_update(s
);
437 static uint64_t omap_i2c_readfn(void *opaque
, hwaddr addr
,
442 return omap_i2c_read(opaque
, addr
);
444 return omap_badwidth_read16(opaque
, addr
);
448 static void omap_i2c_writefn(void *opaque
, hwaddr addr
,
449 uint64_t value
, unsigned size
)
453 /* Only the last fifo write can be 8 bit. */
454 omap_i2c_writeb(opaque
, addr
, value
);
457 omap_i2c_write(opaque
, addr
, value
);
460 omap_badwidth_write16(opaque
, addr
, value
);
465 static const MemoryRegionOps omap_i2c_ops
= {
466 .read
= omap_i2c_readfn
,
467 .write
= omap_i2c_writefn
,
468 .valid
.min_access_size
= 1,
469 .valid
.max_access_size
= 4,
470 .endianness
= DEVICE_NATIVE_ENDIAN
,
473 static void omap_i2c_init(Object
*obj
)
475 DeviceState
*dev
= DEVICE(obj
);
476 OMAPI2CState
*s
= OMAP_I2C(obj
);
477 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
479 sysbus_init_irq(sbd
, &s
->irq
);
480 sysbus_init_irq(sbd
, &s
->drq
[0]);
481 sysbus_init_irq(sbd
, &s
->drq
[1]);
482 sysbus_init_mmio(sbd
, &s
->iomem
);
483 s
->bus
= i2c_init_bus(dev
, NULL
);
486 static void omap_i2c_realize(DeviceState
*dev
, Error
**errp
)
488 OMAPI2CState
*s
= OMAP_I2C(dev
);
490 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &omap_i2c_ops
, s
, "omap.i2c",
491 (s
->revision
< OMAP2_INTR_REV
) ? 0x800 : 0x1000);
494 error_setg(errp
, "omap_i2c: fclk not connected");
497 if (s
->revision
>= OMAP2_INTR_REV
&& !s
->iclk
) {
498 /* Note that OMAP1 doesn't have a separate interface clock */
499 error_setg(errp
, "omap_i2c: iclk not connected");
504 void omap_i2c_set_iclk(OMAPI2CState
*i2c
, omap_clk clk
)
509 void omap_i2c_set_fclk(OMAPI2CState
*i2c
, omap_clk clk
)
514 static Property omap_i2c_properties
[] = {
515 DEFINE_PROP_UINT8("revision", OMAPI2CState
, revision
, 0),
516 DEFINE_PROP_END_OF_LIST(),
519 static void omap_i2c_class_init(ObjectClass
*klass
, void *data
)
521 DeviceClass
*dc
= DEVICE_CLASS(klass
);
523 dc
->props
= omap_i2c_properties
;
524 dc
->reset
= omap_i2c_reset
;
525 /* Reason: pointer properties "iclk", "fclk" */
526 dc
->user_creatable
= false;
527 dc
->realize
= omap_i2c_realize
;
530 static const TypeInfo omap_i2c_info
= {
531 .name
= TYPE_OMAP_I2C
,
532 .parent
= TYPE_SYS_BUS_DEVICE
,
533 .instance_size
= sizeof(OMAPI2CState
),
534 .instance_init
= omap_i2c_init
,
535 .class_init
= omap_i2c_class_init
,
538 static void omap_i2c_register_types(void)
540 type_register_static(&omap_i2c_info
);
543 I2CBus
*omap_i2c_bus(DeviceState
*omap_i2c
)
545 OMAPI2CState
*s
= OMAP_I2C(omap_i2c
);
549 type_init(omap_i2c_register_types
)