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/>.
45 #define OMAP2_INTR_REV 0x34
46 #define OMAP2_GC_REV 0x34
48 static void omap_i2c_interrupts_update(struct omap_i2c_s
*s
)
50 qemu_set_irq(s
->irq
, s
->stat
& s
->mask
);
51 if ((s
->dma
>> 15) & 1) /* RDMA_EN */
52 qemu_set_irq(s
->drq
[0], (s
->stat
>> 3) & 1); /* RRDY */
53 if ((s
->dma
>> 7) & 1) /* XDMA_EN */
54 qemu_set_irq(s
->drq
[1], (s
->stat
>> 4) & 1); /* XRDY */
57 static void omap_i2c_fifo_run(struct omap_i2c_s
*s
)
61 if (!i2c_bus_busy(s
->bus
))
64 if ((s
->control
>> 2) & 1) { /* RM */
65 if ((s
->control
>> 1) & 1) { /* STP */
66 i2c_end_transfer(s
->bus
);
67 s
->control
&= ~(1 << 1); /* STP */
68 s
->count_cur
= s
->count
;
70 } else if ((s
->control
>> 9) & 1) { /* TRX */
71 while (ack
&& s
->txlen
)
72 ack
= (i2c_send(s
->bus
,
73 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
75 s
->stat
|= 1 << 4; /* XRDY */
78 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
79 s
->stat
|= 1 << 3; /* RRDY */
82 if ((s
->control
>> 9) & 1) { /* TRX */
83 while (ack
&& s
->count_cur
&& s
->txlen
) {
84 ack
= (i2c_send(s
->bus
,
85 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
89 if (ack
&& s
->count_cur
)
90 s
->stat
|= 1 << 4; /* XRDY */
92 s
->stat
&= ~(1 << 4); /* XRDY */
94 s
->stat
|= 1 << 2; /* ARDY */
95 s
->control
&= ~(1 << 10); /* MST */
98 while (s
->count_cur
&& s
->rxlen
< 4) {
99 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
103 s
->stat
|= 1 << 3; /* RRDY */
105 s
->stat
&= ~(1 << 3); /* RRDY */
108 if ((s
->control
>> 1) & 1) { /* STP */
109 i2c_end_transfer(s
->bus
);
110 s
->control
&= ~(1 << 1); /* STP */
111 s
->count_cur
= s
->count
;
114 s
->stat
|= 1 << 2; /* ARDY */
115 s
->control
&= ~(1 << 10); /* MST */
120 s
->stat
|= (!ack
) << 1; /* NACK */
122 s
->control
&= ~(1 << 1); /* STP */
125 void omap_i2c_reset(struct omap_i2c_s
*s
)
144 static uint32_t omap_i2c_read(void *opaque
, target_phys_addr_t addr
)
146 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) opaque
;
147 int offset
= addr
& OMAP_MPUI_REG_MASK
;
151 case 0x00: /* I2C_REV */
152 return s
->revision
; /* REV */
154 case 0x04: /* I2C_IE */
157 case 0x08: /* I2C_STAT */
158 return s
->stat
| (i2c_bus_busy(s
->bus
) << 12);
160 case 0x0c: /* I2C_IV */
161 if (s
->revision
>= OMAP2_INTR_REV
)
163 ret
= ffs(s
->stat
& s
->mask
);
165 s
->stat
^= 1 << (ret
- 1);
166 omap_i2c_interrupts_update(s
);
169 case 0x10: /* I2C_SYSS */
170 return (s
->control
>> 15) & 1; /* I2C_EN */
172 case 0x14: /* I2C_BUF */
175 case 0x18: /* I2C_CNT */
176 return s
->count_cur
; /* DCOUNT */
178 case 0x1c: /* I2C_DATA */
180 if (s
->control
& (1 << 14)) { /* BE */
181 ret
|= ((s
->fifo
>> 0) & 0xff) << 8;
182 ret
|= ((s
->fifo
>> 8) & 0xff) << 0;
184 ret
|= ((s
->fifo
>> 8) & 0xff) << 8;
185 ret
|= ((s
->fifo
>> 0) & 0xff) << 0;
188 s
->stat
|= 1 << 15; /* SBD */
190 } else if (s
->rxlen
> 1) {
195 /* XXX: remote access (qualifier) error - what's that? */
198 s
->stat
&= ~(1 << 3); /* RRDY */
199 if (((s
->control
>> 10) & 1) && /* MST */
200 ((~s
->control
>> 9) & 1)) { /* TRX */
201 s
->stat
|= 1 << 2; /* ARDY */
202 s
->control
&= ~(1 << 10); /* MST */
205 s
->stat
&= ~(1 << 11); /* ROVR */
206 omap_i2c_fifo_run(s
);
207 omap_i2c_interrupts_update(s
);
210 case 0x20: /* I2C_SYSC */
213 case 0x24: /* I2C_CON */
216 case 0x28: /* I2C_OA */
219 case 0x2c: /* I2C_SA */
222 case 0x30: /* I2C_PSC */
225 case 0x34: /* I2C_SCLL */
228 case 0x38: /* I2C_SCLH */
231 case 0x3c: /* I2C_SYSTEST */
232 if (s
->test
& (1 << 15)) { /* ST_EN */
236 return s
->test
& ~0x300f;
243 static void omap_i2c_write(void *opaque
, target_phys_addr_t addr
,
246 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) opaque
;
247 int offset
= addr
& OMAP_MPUI_REG_MASK
;
251 case 0x00: /* I2C_REV */
252 case 0x0c: /* I2C_IV */
253 case 0x10: /* I2C_SYSS */
257 case 0x04: /* I2C_IE */
258 s
->mask
= value
& (s
->revision
< OMAP2_GC_REV
? 0x1f : 0x3f);
261 case 0x08: /* I2C_STAT */
262 if (s
->revision
< OMAP2_INTR_REV
) {
267 /* RRDY and XRDY are reset by hardware. (in all versions???) */
268 s
->stat
&= ~(value
& 0x27);
269 omap_i2c_interrupts_update(s
);
272 case 0x14: /* I2C_BUF */
273 s
->dma
= value
& 0x8080;
274 if (value
& (1 << 15)) /* RDMA_EN */
275 s
->mask
&= ~(1 << 3); /* RRDY_IE */
276 if (value
& (1 << 7)) /* XDMA_EN */
277 s
->mask
&= ~(1 << 4); /* XRDY_IE */
280 case 0x18: /* I2C_CNT */
281 s
->count
= value
; /* DCOUNT */
284 case 0x1c: /* I2C_DATA */
286 /* XXX: remote access (qualifier) error - what's that? */
291 if (s
->control
& (1 << 14)) { /* BE */
292 s
->fifo
|= ((value
>> 8) & 0xff) << 8;
293 s
->fifo
|= ((value
>> 0) & 0xff) << 0;
295 s
->fifo
|= ((value
>> 0) & 0xff) << 8;
296 s
->fifo
|= ((value
>> 8) & 0xff) << 0;
298 s
->stat
&= ~(1 << 10); /* XUDF */
300 s
->stat
&= ~(1 << 4); /* XRDY */
301 omap_i2c_fifo_run(s
);
302 omap_i2c_interrupts_update(s
);
305 case 0x20: /* I2C_SYSC */
306 if (s
->revision
< OMAP2_INTR_REV
) {
315 case 0x24: /* I2C_CON */
316 s
->control
= value
& 0xcf87;
317 if (~value
& (1 << 15)) { /* I2C_EN */
318 if (s
->revision
< OMAP2_INTR_REV
)
322 if ((value
& (1 << 15)) && !(value
& (1 << 10))) { /* MST */
323 fprintf(stderr
, "%s: I^2C slave mode not supported\n",
327 if ((value
& (1 << 15)) && value
& (1 << 8)) { /* XA */
328 fprintf(stderr
, "%s: 10-bit addressing mode not supported\n",
332 if ((value
& (1 << 15)) && value
& (1 << 0)) { /* STT */
333 nack
= !!i2c_start_transfer(s
->bus
, s
->addr
[1], /* SA */
334 (~value
>> 9) & 1); /* TRX */
335 s
->stat
|= nack
<< 1; /* NACK */
336 s
->control
&= ~(1 << 0); /* STT */
339 s
->control
&= ~(1 << 1); /* STP */
341 s
->count_cur
= s
->count
;
342 omap_i2c_fifo_run(s
);
344 omap_i2c_interrupts_update(s
);
348 case 0x28: /* I2C_OA */
349 s
->addr
[0] = value
& 0x3ff;
352 case 0x2c: /* I2C_SA */
353 s
->addr
[1] = value
& 0x3ff;
356 case 0x30: /* I2C_PSC */
360 case 0x34: /* I2C_SCLL */
364 case 0x38: /* I2C_SCLH */
368 case 0x3c: /* I2C_SYSTEST */
369 s
->test
= value
& 0xf80f;
370 if (value
& (1 << 11)) /* SBB */
371 if (s
->revision
>= OMAP2_INTR_REV
) {
373 omap_i2c_interrupts_update(s
);
375 if (value
& (1 << 15)) /* ST_EN */
376 fprintf(stderr
, "%s: System Test not supported\n", __FUNCTION__
);
385 static void omap_i2c_writeb(void *opaque
, target_phys_addr_t addr
,
388 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) opaque
;
389 int offset
= addr
& OMAP_MPUI_REG_MASK
;
392 case 0x1c: /* I2C_DATA */
394 /* XXX: remote access (qualifier) error - what's that? */
399 s
->fifo
|= value
& 0xff;
400 s
->stat
&= ~(1 << 10); /* XUDF */
402 s
->stat
&= ~(1 << 4); /* XRDY */
403 omap_i2c_fifo_run(s
);
404 omap_i2c_interrupts_update(s
);
413 static const MemoryRegionOps omap_i2c_ops
= {
416 omap_badwidth_read16
,
418 omap_badwidth_read16
,
421 omap_i2c_writeb
, /* Only the last fifo write can be 8 bit. */
423 omap_badwidth_write16
,
426 .endianness
= DEVICE_NATIVE_ENDIAN
,
429 struct omap_i2c_s
*omap_i2c_init(MemoryRegion
*sysmem
,
430 target_phys_addr_t base
,
435 struct omap_i2c_s
*s
= (struct omap_i2c_s
*)
436 g_malloc0(sizeof(struct omap_i2c_s
));
438 /* TODO: set a value greater or equal to real hardware */
443 s
->bus
= i2c_init_bus(NULL
, "i2c");
446 memory_region_init_io(&s
->iomem
, &omap_i2c_ops
, s
, "omap.i2c", 0x800);
447 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
452 struct omap_i2c_s
*omap2_i2c_init(struct omap_target_agent_s
*ta
,
453 qemu_irq irq
, qemu_irq
*dma
, omap_clk fclk
, omap_clk iclk
)
455 struct omap_i2c_s
*s
= (struct omap_i2c_s
*)
456 g_malloc0(sizeof(struct omap_i2c_s
));
462 s
->bus
= i2c_init_bus(NULL
, "i2c");
465 memory_region_init_io(&s
->iomem
, &omap_i2c_ops
, s
, "omap2.i2c",
466 omap_l4_region_size(ta
, 0));
467 omap_l4_attach(ta
, 0, &s
->iomem
);
472 i2c_bus
*omap_i2c_bus(struct omap_i2c_s
*s
)