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, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 #define OMAP2_INTR_REV 0x34
47 #define OMAP2_GC_REV 0x34
49 static void omap_i2c_interrupts_update(struct omap_i2c_s
*s
)
51 qemu_set_irq(s
->irq
, s
->stat
& s
->mask
);
52 if ((s
->dma
>> 15) & 1) /* RDMA_EN */
53 qemu_set_irq(s
->drq
[0], (s
->stat
>> 3) & 1); /* RRDY */
54 if ((s
->dma
>> 7) & 1) /* XDMA_EN */
55 qemu_set_irq(s
->drq
[1], (s
->stat
>> 4) & 1); /* XRDY */
58 /* These are only stubs now. */
59 static void omap_i2c_event(i2c_slave
*i2c
, enum i2c_event event
)
61 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) i2c
;
63 if ((~s
->control
>> 15) & 1) /* I2C_EN */
69 s
->stat
|= 1 << 9; /* AAS */
72 s
->stat
|= 1 << 2; /* ARDY */
75 s
->stat
|= 1 << 1; /* NACK */
79 omap_i2c_interrupts_update(s
);
82 static int omap_i2c_rx(i2c_slave
*i2c
)
84 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) i2c
;
87 if ((~s
->control
>> 15) & 1) /* I2C_EN */
91 ret
= s
->fifo
>> ((-- s
->txlen
) << 3) & 0xff;
93 s
->stat
|= 1 << 10; /* XUDF */
94 s
->stat
|= 1 << 4; /* XRDY */
96 omap_i2c_interrupts_update(s
);
100 static int omap_i2c_tx(i2c_slave
*i2c
, uint8_t data
)
102 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) i2c
;
104 if ((~s
->control
>> 15) & 1) /* I2C_EN */
108 s
->fifo
|= data
<< ((s
->rxlen
++) << 3);
110 s
->stat
|= 1 << 11; /* ROVR */
111 s
->stat
|= 1 << 3; /* RRDY */
113 omap_i2c_interrupts_update(s
);
117 static void omap_i2c_fifo_run(struct omap_i2c_s
*s
)
121 if (!i2c_bus_busy(s
->bus
))
124 if ((s
->control
>> 2) & 1) { /* RM */
125 if ((s
->control
>> 1) & 1) { /* STP */
126 i2c_end_transfer(s
->bus
);
127 s
->control
&= ~(1 << 1); /* STP */
128 s
->count_cur
= s
->count
;
130 } else if ((s
->control
>> 9) & 1) { /* TRX */
131 while (ack
&& s
->txlen
)
132 ack
= (i2c_send(s
->bus
,
133 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
135 s
->stat
|= 1 << 4; /* XRDY */
138 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
139 s
->stat
|= 1 << 3; /* RRDY */
142 if ((s
->control
>> 9) & 1) { /* TRX */
143 while (ack
&& s
->count_cur
&& s
->txlen
) {
144 ack
= (i2c_send(s
->bus
,
145 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
149 if (ack
&& s
->count_cur
)
150 s
->stat
|= 1 << 4; /* XRDY */
152 s
->stat
&= ~(1 << 4); /* XRDY */
154 s
->stat
|= 1 << 2; /* ARDY */
155 s
->control
&= ~(1 << 10); /* MST */
158 while (s
->count_cur
&& s
->rxlen
< 4) {
159 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
163 s
->stat
|= 1 << 3; /* RRDY */
165 s
->stat
&= ~(1 << 3); /* RRDY */
168 if ((s
->control
>> 1) & 1) { /* STP */
169 i2c_end_transfer(s
->bus
);
170 s
->control
&= ~(1 << 1); /* STP */
171 s
->count_cur
= s
->count
;
174 s
->stat
|= 1 << 2; /* ARDY */
175 s
->control
&= ~(1 << 10); /* MST */
180 s
->stat
|= (!ack
) << 1; /* NACK */
182 s
->control
&= ~(1 << 1); /* STP */
185 void omap_i2c_reset(struct omap_i2c_s
*s
)
204 static uint32_t omap_i2c_read(void *opaque
, target_phys_addr_t addr
)
206 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) opaque
;
207 int offset
= addr
& OMAP_MPUI_REG_MASK
;
211 case 0x00: /* I2C_REV */
212 return s
->revision
; /* REV */
214 case 0x04: /* I2C_IE */
217 case 0x08: /* I2C_STAT */
218 return s
->stat
| (i2c_bus_busy(s
->bus
) << 12);
220 case 0x0c: /* I2C_IV */
221 if (s
->revision
>= OMAP2_INTR_REV
)
223 ret
= ffs(s
->stat
& s
->mask
);
225 s
->stat
^= 1 << (ret
- 1);
226 omap_i2c_interrupts_update(s
);
229 case 0x10: /* I2C_SYSS */
230 return (s
->control
>> 15) & 1; /* I2C_EN */
232 case 0x14: /* I2C_BUF */
235 case 0x18: /* I2C_CNT */
236 return s
->count_cur
; /* DCOUNT */
238 case 0x1c: /* I2C_DATA */
240 if (s
->control
& (1 << 14)) { /* BE */
241 ret
|= ((s
->fifo
>> 0) & 0xff) << 8;
242 ret
|= ((s
->fifo
>> 8) & 0xff) << 0;
244 ret
|= ((s
->fifo
>> 8) & 0xff) << 8;
245 ret
|= ((s
->fifo
>> 0) & 0xff) << 0;
248 s
->stat
|= 1 << 15; /* SBD */
250 } else if (s
->rxlen
> 1) {
255 /* XXX: remote access (qualifier) error - what's that? */;
257 s
->stat
&= ~(1 << 3); /* RRDY */
258 if (((s
->control
>> 10) & 1) && /* MST */
259 ((~s
->control
>> 9) & 1)) { /* TRX */
260 s
->stat
|= 1 << 2; /* ARDY */
261 s
->control
&= ~(1 << 10); /* MST */
264 s
->stat
&= ~(1 << 11); /* ROVR */
265 omap_i2c_fifo_run(s
);
266 omap_i2c_interrupts_update(s
);
269 case 0x20: /* I2C_SYSC */
272 case 0x24: /* I2C_CON */
275 case 0x28: /* I2C_OA */
278 case 0x2c: /* I2C_SA */
281 case 0x30: /* I2C_PSC */
284 case 0x34: /* I2C_SCLL */
287 case 0x38: /* I2C_SCLH */
290 case 0x3c: /* I2C_SYSTEST */
291 if (s
->test
& (1 << 15)) { /* ST_EN */
295 return s
->test
& ~0x300f;
302 static void omap_i2c_write(void *opaque
, target_phys_addr_t addr
,
305 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) opaque
;
306 int offset
= addr
& OMAP_MPUI_REG_MASK
;
310 case 0x00: /* I2C_REV */
311 case 0x0c: /* I2C_IV */
312 case 0x10: /* I2C_SYSS */
316 case 0x04: /* I2C_IE */
317 s
->mask
= value
& (s
->revision
< OMAP2_GC_REV
? 0x1f : 0x3f);
320 case 0x08: /* I2C_STAT */
321 if (s
->revision
< OMAP2_INTR_REV
) {
326 /* RRDY and XRDY are reset by hardware. (in all versions???) */
327 s
->stat
&= ~(value
& 0x27);
328 omap_i2c_interrupts_update(s
);
331 case 0x14: /* I2C_BUF */
332 s
->dma
= value
& 0x8080;
333 if (value
& (1 << 15)) /* RDMA_EN */
334 s
->mask
&= ~(1 << 3); /* RRDY_IE */
335 if (value
& (1 << 7)) /* XDMA_EN */
336 s
->mask
&= ~(1 << 4); /* XRDY_IE */
339 case 0x18: /* I2C_CNT */
340 s
->count
= value
; /* DCOUNT */
343 case 0x1c: /* I2C_DATA */
345 /* XXX: remote access (qualifier) error - what's that? */
350 if (s
->control
& (1 << 14)) { /* BE */
351 s
->fifo
|= ((value
>> 8) & 0xff) << 8;
352 s
->fifo
|= ((value
>> 0) & 0xff) << 0;
354 s
->fifo
|= ((value
>> 0) & 0xff) << 8;
355 s
->fifo
|= ((value
>> 8) & 0xff) << 0;
357 s
->stat
&= ~(1 << 10); /* XUDF */
359 s
->stat
&= ~(1 << 4); /* XRDY */
360 omap_i2c_fifo_run(s
);
361 omap_i2c_interrupts_update(s
);
364 case 0x20: /* I2C_SYSC */
365 if (s
->revision
< OMAP2_INTR_REV
) {
374 case 0x24: /* I2C_CON */
375 s
->control
= value
& 0xcf87;
376 if (~value
& (1 << 15)) { /* I2C_EN */
377 if (s
->revision
< OMAP2_INTR_REV
)
381 if ((value
& (1 << 15)) && !(value
& (1 << 10))) { /* MST */
382 fprintf(stderr
, "%s: I^2C slave mode not supported\n",
386 if ((value
& (1 << 15)) && value
& (1 << 8)) { /* XA */
387 fprintf(stderr
, "%s: 10-bit addressing mode not supported\n",
391 if ((value
& (1 << 15)) && value
& (1 << 0)) { /* STT */
392 nack
= !!i2c_start_transfer(s
->bus
, s
->addr
[1], /* SA */
393 (~value
>> 9) & 1); /* TRX */
394 s
->stat
|= nack
<< 1; /* NACK */
395 s
->control
&= ~(1 << 0); /* STT */
398 s
->control
&= ~(1 << 1); /* STP */
400 s
->count_cur
= s
->count
;
401 omap_i2c_fifo_run(s
);
403 omap_i2c_interrupts_update(s
);
407 case 0x28: /* I2C_OA */
408 s
->addr
[0] = value
& 0x3ff;
409 i2c_set_slave_address(&s
->slave
, value
& 0x7f);
412 case 0x2c: /* I2C_SA */
413 s
->addr
[1] = value
& 0x3ff;
416 case 0x30: /* I2C_PSC */
420 case 0x34: /* I2C_SCLL */
424 case 0x38: /* I2C_SCLH */
428 case 0x3c: /* I2C_SYSTEST */
429 s
->test
= value
& 0xf80f;
430 if (value
& (1 << 11)) /* SBB */
431 if (s
->revision
>= OMAP2_INTR_REV
) {
433 omap_i2c_interrupts_update(s
);
435 if (value
& (1 << 15)) /* ST_EN */
436 fprintf(stderr
, "%s: System Test not supported\n", __FUNCTION__
);
445 static void omap_i2c_writeb(void *opaque
, target_phys_addr_t addr
,
448 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) opaque
;
449 int offset
= addr
& OMAP_MPUI_REG_MASK
;
452 case 0x1c: /* I2C_DATA */
454 /* XXX: remote access (qualifier) error - what's that? */
459 s
->fifo
|= value
& 0xff;
460 s
->stat
&= ~(1 << 10); /* XUDF */
462 s
->stat
&= ~(1 << 4); /* XRDY */
463 omap_i2c_fifo_run(s
);
464 omap_i2c_interrupts_update(s
);
473 static CPUReadMemoryFunc
*omap_i2c_readfn
[] = {
474 omap_badwidth_read16
,
476 omap_badwidth_read16
,
479 static CPUWriteMemoryFunc
*omap_i2c_writefn
[] = {
480 omap_i2c_writeb
, /* Only the last fifo write can be 8 bit. */
482 omap_badwidth_write16
,
485 struct omap_i2c_s
*omap_i2c_init(target_phys_addr_t base
,
486 qemu_irq irq
, qemu_irq
*dma
, omap_clk clk
)
489 struct omap_i2c_s
*s
= (struct omap_i2c_s
*)
490 qemu_mallocz(sizeof(struct omap_i2c_s
));
492 /* TODO: set a value greater or equal to real hardware */
497 s
->slave
.event
= omap_i2c_event
;
498 s
->slave
.recv
= omap_i2c_rx
;
499 s
->slave
.send
= omap_i2c_tx
;
500 s
->bus
= i2c_init_bus();
503 iomemtype
= cpu_register_io_memory(0, omap_i2c_readfn
,
504 omap_i2c_writefn
, s
);
505 cpu_register_physical_memory(base
, 0x800, iomemtype
);
510 struct omap_i2c_s
*omap2_i2c_init(struct omap_target_agent_s
*ta
,
511 qemu_irq irq
, qemu_irq
*dma
, omap_clk fclk
, omap_clk iclk
)
514 struct omap_i2c_s
*s
= (struct omap_i2c_s
*)
515 qemu_mallocz(sizeof(struct omap_i2c_s
));
521 s
->slave
.event
= omap_i2c_event
;
522 s
->slave
.recv
= omap_i2c_rx
;
523 s
->slave
.send
= omap_i2c_tx
;
524 s
->bus
= i2c_init_bus();
527 iomemtype
= l4_register_io_memory(0, omap_i2c_readfn
,
528 omap_i2c_writefn
, s
);
529 omap_l4_attach(ta
, 0, iomemtype
);
534 i2c_bus
*omap_i2c_bus(struct omap_i2c_s
*s
)