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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 target_phys_addr_t base
;
48 #define OMAP2_INTR_REV 0x34
49 #define OMAP2_GC_REV 0x34
51 static void omap_i2c_interrupts_update(struct omap_i2c_s
*s
)
53 qemu_set_irq(s
->irq
, s
->stat
& s
->mask
);
54 if ((s
->dma
>> 15) & 1) /* RDMA_EN */
55 qemu_set_irq(s
->drq
[0], (s
->stat
>> 3) & 1); /* RRDY */
56 if ((s
->dma
>> 7) & 1) /* XDMA_EN */
57 qemu_set_irq(s
->drq
[1], (s
->stat
>> 4) & 1); /* XRDY */
60 /* These are only stubs now. */
61 static void omap_i2c_event(i2c_slave
*i2c
, enum i2c_event event
)
63 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) i2c
;
65 if ((~s
->control
>> 15) & 1) /* I2C_EN */
71 s
->stat
|= 1 << 9; /* AAS */
74 s
->stat
|= 1 << 2; /* ARDY */
77 s
->stat
|= 1 << 1; /* NACK */
81 omap_i2c_interrupts_update(s
);
84 static int omap_i2c_rx(i2c_slave
*i2c
)
86 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) i2c
;
89 if ((~s
->control
>> 15) & 1) /* I2C_EN */
93 ret
= s
->fifo
>> ((-- s
->txlen
) << 3) & 0xff;
95 s
->stat
|= 1 << 10; /* XUDF */
96 s
->stat
|= 1 << 4; /* XRDY */
98 omap_i2c_interrupts_update(s
);
102 static int omap_i2c_tx(i2c_slave
*i2c
, uint8_t data
)
104 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) i2c
;
106 if ((~s
->control
>> 15) & 1) /* I2C_EN */
110 s
->fifo
|= data
<< ((s
->rxlen
++) << 3);
112 s
->stat
|= 1 << 11; /* ROVR */
113 s
->stat
|= 1 << 3; /* RRDY */
115 omap_i2c_interrupts_update(s
);
119 static void omap_i2c_fifo_run(struct omap_i2c_s
*s
)
123 if (!i2c_bus_busy(s
->bus
))
126 if ((s
->control
>> 2) & 1) { /* RM */
127 if ((s
->control
>> 1) & 1) { /* STP */
128 i2c_end_transfer(s
->bus
);
129 s
->control
&= ~(1 << 1); /* STP */
130 s
->count_cur
= s
->count
;
132 } else if ((s
->control
>> 9) & 1) { /* TRX */
133 while (ack
&& s
->txlen
)
134 ack
= (i2c_send(s
->bus
,
135 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
137 s
->stat
|= 1 << 4; /* XRDY */
140 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
141 s
->stat
|= 1 << 3; /* RRDY */
144 if ((s
->control
>> 9) & 1) { /* TRX */
145 while (ack
&& s
->count_cur
&& s
->txlen
) {
146 ack
= (i2c_send(s
->bus
,
147 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
151 if (ack
&& s
->count_cur
)
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 */
166 if ((s
->control
>> 1) & 1) { /* STP */
167 i2c_end_transfer(s
->bus
);
168 s
->control
&= ~(1 << 1); /* STP */
169 s
->count_cur
= s
->count
;
172 s
->stat
|= 1 << 2; /* ARDY */
173 s
->control
&= ~(1 << 10); /* MST */
178 s
->stat
|= (!ack
) << 1; /* NACK */
180 s
->control
&= ~(1 << 1); /* STP */
183 void omap_i2c_reset(struct omap_i2c_s
*s
)
202 static uint32_t omap_i2c_read(void *opaque
, target_phys_addr_t addr
)
204 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) opaque
;
205 int offset
= addr
& OMAP_MPUI_REG_MASK
;
209 case 0x00: /* I2C_REV */
210 return s
->revision
; /* REV */
212 case 0x04: /* I2C_IE */
215 case 0x08: /* I2C_STAT */
216 return s
->stat
| (i2c_bus_busy(s
->bus
) << 12);
218 case 0x0c: /* I2C_IV */
219 if (s
->revision
>= OMAP2_INTR_REV
)
221 ret
= ffs(s
->stat
& s
->mask
);
223 s
->stat
^= 1 << (ret
- 1);
224 omap_i2c_interrupts_update(s
);
227 case 0x10: /* I2C_SYSS */
228 return (s
->control
>> 15) & 1; /* I2C_EN */
230 case 0x14: /* I2C_BUF */
233 case 0x18: /* I2C_CNT */
234 return s
->count_cur
; /* DCOUNT */
236 case 0x1c: /* I2C_DATA */
238 if (s
->control
& (1 << 14)) { /* BE */
239 ret
|= ((s
->fifo
>> 0) & 0xff) << 8;
240 ret
|= ((s
->fifo
>> 8) & 0xff) << 0;
242 ret
|= ((s
->fifo
>> 8) & 0xff) << 8;
243 ret
|= ((s
->fifo
>> 0) & 0xff) << 0;
246 s
->stat
|= 1 << 15; /* SBD */
248 } else if (s
->rxlen
> 1) {
253 /* XXX: remote access (qualifier) error - what's that? */;
255 s
->stat
&= ~(1 << 3); /* RRDY */
256 if (((s
->control
>> 10) & 1) && /* MST */
257 ((~s
->control
>> 9) & 1)) { /* TRX */
258 s
->stat
|= 1 << 2; /* ARDY */
259 s
->control
&= ~(1 << 10); /* MST */
262 s
->stat
&= ~(1 << 11); /* ROVR */
263 omap_i2c_fifo_run(s
);
264 omap_i2c_interrupts_update(s
);
267 case 0x20: /* I2C_SYSC */
270 case 0x24: /* I2C_CON */
273 case 0x28: /* I2C_OA */
276 case 0x2c: /* I2C_SA */
279 case 0x30: /* I2C_PSC */
282 case 0x34: /* I2C_SCLL */
285 case 0x38: /* I2C_SCLH */
288 case 0x3c: /* I2C_SYSTEST */
289 if (s
->test
& (1 << 15)) { /* ST_EN */
293 return s
->test
& ~0x300f;
300 static void omap_i2c_write(void *opaque
, target_phys_addr_t addr
,
303 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) opaque
;
304 int offset
= addr
& OMAP_MPUI_REG_MASK
;
308 case 0x00: /* I2C_REV */
309 case 0x0c: /* I2C_IV */
310 case 0x10: /* I2C_SYSS */
314 case 0x04: /* I2C_IE */
315 s
->mask
= value
& (s
->revision
< OMAP2_GC_REV
? 0x1f : 0x3f);
318 case 0x08: /* I2C_STAT */
319 if (s
->revision
< OMAP2_INTR_REV
) {
324 s
->stat
&= ~(value
& 0x3f);
325 omap_i2c_interrupts_update(s
);
328 case 0x14: /* I2C_BUF */
329 s
->dma
= value
& 0x8080;
330 if (value
& (1 << 15)) /* RDMA_EN */
331 s
->mask
&= ~(1 << 3); /* RRDY_IE */
332 if (value
& (1 << 7)) /* XDMA_EN */
333 s
->mask
&= ~(1 << 4); /* XRDY_IE */
336 case 0x18: /* I2C_CNT */
337 s
->count
= value
; /* DCOUNT */
340 case 0x1c: /* I2C_DATA */
342 /* XXX: remote access (qualifier) error - what's that? */
347 if (s
->control
& (1 << 14)) { /* BE */
348 s
->fifo
|= ((value
>> 8) & 0xff) << 8;
349 s
->fifo
|= ((value
>> 0) & 0xff) << 0;
351 s
->fifo
|= ((value
>> 0) & 0xff) << 8;
352 s
->fifo
|= ((value
>> 8) & 0xff) << 0;
354 s
->stat
&= ~(1 << 10); /* XUDF */
356 s
->stat
&= ~(1 << 4); /* XRDY */
357 omap_i2c_fifo_run(s
);
358 omap_i2c_interrupts_update(s
);
361 case 0x20: /* I2C_SYSC */
362 if (s
->revision
< OMAP2_INTR_REV
) {
371 case 0x24: /* I2C_CON */
372 s
->control
= value
& 0xcf87;
373 if (~value
& (1 << 15)) { /* I2C_EN */
374 if (s
->revision
< OMAP2_INTR_REV
)
378 if ((value
& (1 << 15)) && !(value
& (1 << 10))) { /* MST */
379 printf("%s: I^2C slave mode not supported\n", __FUNCTION__
);
382 if ((value
& (1 << 15)) && value
& (1 << 8)) { /* XA */
383 printf("%s: 10-bit addressing mode not supported\n", __FUNCTION__
);
386 if ((value
& (1 << 15)) && value
& (1 << 0)) { /* STT */
387 nack
= !!i2c_start_transfer(s
->bus
, s
->addr
[1], /* SA */
388 (~value
>> 9) & 1); /* TRX */
389 s
->stat
|= nack
<< 1; /* NACK */
390 s
->control
&= ~(1 << 0); /* STT */
392 s
->control
&= ~(1 << 1); /* STP */
394 s
->count_cur
= s
->count
;
395 omap_i2c_fifo_run(s
);
397 omap_i2c_interrupts_update(s
);
401 case 0x28: /* I2C_OA */
402 s
->addr
[0] = value
& 0x3ff;
403 i2c_set_slave_address(&s
->slave
, value
& 0x7f);
406 case 0x2c: /* I2C_SA */
407 s
->addr
[1] = value
& 0x3ff;
410 case 0x30: /* I2C_PSC */
414 case 0x34: /* I2C_SCLL */
418 case 0x38: /* I2C_SCLH */
422 case 0x3c: /* I2C_SYSTEST */
423 s
->test
= value
& 0xf80f;
424 if (value
& (1 << 11)) /* SBB */
425 if (s
->revision
>= OMAP2_INTR_REV
) {
427 omap_i2c_interrupts_update(s
);
429 if (value
& (1 << 15)) /* ST_EN */
430 printf("%s: System Test not supported\n", __FUNCTION__
);
439 static void omap_i2c_writeb(void *opaque
, target_phys_addr_t addr
,
442 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) opaque
;
443 int offset
= addr
& OMAP_MPUI_REG_MASK
;
446 case 0x1c: /* I2C_DATA */
448 /* XXX: remote access (qualifier) error - what's that? */
453 s
->fifo
|= value
& 0xff;
454 s
->stat
&= ~(1 << 10); /* XUDF */
456 s
->stat
&= ~(1 << 4); /* XRDY */
457 omap_i2c_fifo_run(s
);
458 omap_i2c_interrupts_update(s
);
467 static CPUReadMemoryFunc
*omap_i2c_readfn
[] = {
468 omap_badwidth_read16
,
470 omap_badwidth_read16
,
473 static CPUWriteMemoryFunc
*omap_i2c_writefn
[] = {
474 omap_i2c_writeb
, /* Only the last fifo write can be 8 bit. */
476 omap_badwidth_write16
,
479 struct omap_i2c_s
*omap_i2c_init(target_phys_addr_t base
,
480 qemu_irq irq
, qemu_irq
*dma
, omap_clk clk
)
483 struct omap_i2c_s
*s
= (struct omap_i2c_s
*)
484 qemu_mallocz(sizeof(struct omap_i2c_s
));
486 /* TODO: set a value greater or equal to real hardware */
492 s
->slave
.event
= omap_i2c_event
;
493 s
->slave
.recv
= omap_i2c_rx
;
494 s
->slave
.send
= omap_i2c_tx
;
495 s
->bus
= i2c_init_bus();
498 iomemtype
= cpu_register_io_memory(0, omap_i2c_readfn
,
499 omap_i2c_writefn
, s
);
500 cpu_register_physical_memory(s
->base
, 0x800, iomemtype
);
505 struct omap_i2c_s
*omap2_i2c_init(struct omap_target_agent_s
*ta
,
506 qemu_irq irq
, qemu_irq
*dma
, omap_clk fclk
, omap_clk iclk
)
509 struct omap_i2c_s
*s
= (struct omap_i2c_s
*)
510 qemu_mallocz(sizeof(struct omap_i2c_s
));
516 s
->slave
.event
= omap_i2c_event
;
517 s
->slave
.recv
= omap_i2c_rx
;
518 s
->slave
.send
= omap_i2c_tx
;
519 s
->bus
= i2c_init_bus();
522 iomemtype
= cpu_register_io_memory(0, omap_i2c_readfn
,
523 omap_i2c_writefn
, s
);
524 s
->base
= omap_l4_attach(ta
, 0, iomemtype
);
529 i2c_bus
*omap_i2c_bus(struct omap_i2c_s
*s
)