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/>.
44 #define OMAP2_INTR_REV 0x34
45 #define OMAP2_GC_REV 0x34
47 static void omap_i2c_interrupts_update(struct omap_i2c_s
*s
)
49 qemu_set_irq(s
->irq
, s
->stat
& s
->mask
);
50 if ((s
->dma
>> 15) & 1) /* RDMA_EN */
51 qemu_set_irq(s
->drq
[0], (s
->stat
>> 3) & 1); /* RRDY */
52 if ((s
->dma
>> 7) & 1) /* XDMA_EN */
53 qemu_set_irq(s
->drq
[1], (s
->stat
>> 4) & 1); /* XRDY */
56 static void omap_i2c_fifo_run(struct omap_i2c_s
*s
)
60 if (!i2c_bus_busy(s
->bus
))
63 if ((s
->control
>> 2) & 1) { /* RM */
64 if ((s
->control
>> 1) & 1) { /* STP */
65 i2c_end_transfer(s
->bus
);
66 s
->control
&= ~(1 << 1); /* STP */
67 s
->count_cur
= s
->count
;
69 } else if ((s
->control
>> 9) & 1) { /* TRX */
70 while (ack
&& s
->txlen
)
71 ack
= (i2c_send(s
->bus
,
72 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
74 s
->stat
|= 1 << 4; /* XRDY */
77 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
78 s
->stat
|= 1 << 3; /* RRDY */
81 if ((s
->control
>> 9) & 1) { /* TRX */
82 while (ack
&& s
->count_cur
&& s
->txlen
) {
83 ack
= (i2c_send(s
->bus
,
84 (s
->fifo
>> ((-- s
->txlen
) << 3)) &
88 if (ack
&& s
->count_cur
)
89 s
->stat
|= 1 << 4; /* XRDY */
91 s
->stat
&= ~(1 << 4); /* XRDY */
93 s
->stat
|= 1 << 2; /* ARDY */
94 s
->control
&= ~(1 << 10); /* MST */
97 while (s
->count_cur
&& s
->rxlen
< 4) {
98 s
->fifo
|= i2c_recv(s
->bus
) << ((s
->rxlen
++) << 3);
102 s
->stat
|= 1 << 3; /* RRDY */
104 s
->stat
&= ~(1 << 3); /* RRDY */
107 if ((s
->control
>> 1) & 1) { /* STP */
108 i2c_end_transfer(s
->bus
);
109 s
->control
&= ~(1 << 1); /* STP */
110 s
->count_cur
= s
->count
;
113 s
->stat
|= 1 << 2; /* ARDY */
114 s
->control
&= ~(1 << 10); /* MST */
119 s
->stat
|= (!ack
) << 1; /* NACK */
121 s
->control
&= ~(1 << 1); /* STP */
124 void omap_i2c_reset(struct omap_i2c_s
*s
)
143 static uint32_t omap_i2c_read(void *opaque
, target_phys_addr_t addr
)
145 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) opaque
;
146 int offset
= addr
& OMAP_MPUI_REG_MASK
;
150 case 0x00: /* I2C_REV */
151 return s
->revision
; /* REV */
153 case 0x04: /* I2C_IE */
156 case 0x08: /* I2C_STAT */
157 return s
->stat
| (i2c_bus_busy(s
->bus
) << 12);
159 case 0x0c: /* I2C_IV */
160 if (s
->revision
>= OMAP2_INTR_REV
)
162 ret
= ffs(s
->stat
& s
->mask
);
164 s
->stat
^= 1 << (ret
- 1);
165 omap_i2c_interrupts_update(s
);
168 case 0x10: /* I2C_SYSS */
169 return (s
->control
>> 15) & 1; /* I2C_EN */
171 case 0x14: /* I2C_BUF */
174 case 0x18: /* I2C_CNT */
175 return s
->count_cur
; /* DCOUNT */
177 case 0x1c: /* I2C_DATA */
179 if (s
->control
& (1 << 14)) { /* BE */
180 ret
|= ((s
->fifo
>> 0) & 0xff) << 8;
181 ret
|= ((s
->fifo
>> 8) & 0xff) << 0;
183 ret
|= ((s
->fifo
>> 8) & 0xff) << 8;
184 ret
|= ((s
->fifo
>> 0) & 0xff) << 0;
187 s
->stat
|= 1 << 15; /* SBD */
189 } else if (s
->rxlen
> 1) {
194 /* XXX: remote access (qualifier) error - what's that? */
197 s
->stat
&= ~(1 << 3); /* RRDY */
198 if (((s
->control
>> 10) & 1) && /* MST */
199 ((~s
->control
>> 9) & 1)) { /* TRX */
200 s
->stat
|= 1 << 2; /* ARDY */
201 s
->control
&= ~(1 << 10); /* MST */
204 s
->stat
&= ~(1 << 11); /* ROVR */
205 omap_i2c_fifo_run(s
);
206 omap_i2c_interrupts_update(s
);
209 case 0x20: /* I2C_SYSC */
212 case 0x24: /* I2C_CON */
215 case 0x28: /* I2C_OA */
218 case 0x2c: /* I2C_SA */
221 case 0x30: /* I2C_PSC */
224 case 0x34: /* I2C_SCLL */
227 case 0x38: /* I2C_SCLH */
230 case 0x3c: /* I2C_SYSTEST */
231 if (s
->test
& (1 << 15)) { /* ST_EN */
235 return s
->test
& ~0x300f;
242 static void omap_i2c_write(void *opaque
, target_phys_addr_t addr
,
245 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) opaque
;
246 int offset
= addr
& OMAP_MPUI_REG_MASK
;
250 case 0x00: /* I2C_REV */
251 case 0x0c: /* I2C_IV */
252 case 0x10: /* I2C_SYSS */
256 case 0x04: /* I2C_IE */
257 s
->mask
= value
& (s
->revision
< OMAP2_GC_REV
? 0x1f : 0x3f);
260 case 0x08: /* I2C_STAT */
261 if (s
->revision
< OMAP2_INTR_REV
) {
266 /* RRDY and XRDY are reset by hardware. (in all versions???) */
267 s
->stat
&= ~(value
& 0x27);
268 omap_i2c_interrupts_update(s
);
271 case 0x14: /* I2C_BUF */
272 s
->dma
= value
& 0x8080;
273 if (value
& (1 << 15)) /* RDMA_EN */
274 s
->mask
&= ~(1 << 3); /* RRDY_IE */
275 if (value
& (1 << 7)) /* XDMA_EN */
276 s
->mask
&= ~(1 << 4); /* XRDY_IE */
279 case 0x18: /* I2C_CNT */
280 s
->count
= value
; /* DCOUNT */
283 case 0x1c: /* I2C_DATA */
285 /* XXX: remote access (qualifier) error - what's that? */
290 if (s
->control
& (1 << 14)) { /* BE */
291 s
->fifo
|= ((value
>> 8) & 0xff) << 8;
292 s
->fifo
|= ((value
>> 0) & 0xff) << 0;
294 s
->fifo
|= ((value
>> 0) & 0xff) << 8;
295 s
->fifo
|= ((value
>> 8) & 0xff) << 0;
297 s
->stat
&= ~(1 << 10); /* XUDF */
299 s
->stat
&= ~(1 << 4); /* XRDY */
300 omap_i2c_fifo_run(s
);
301 omap_i2c_interrupts_update(s
);
304 case 0x20: /* I2C_SYSC */
305 if (s
->revision
< OMAP2_INTR_REV
) {
314 case 0x24: /* I2C_CON */
315 s
->control
= value
& 0xcf87;
316 if (~value
& (1 << 15)) { /* I2C_EN */
317 if (s
->revision
< OMAP2_INTR_REV
)
321 if ((value
& (1 << 15)) && !(value
& (1 << 10))) { /* MST */
322 fprintf(stderr
, "%s: I^2C slave mode not supported\n",
326 if ((value
& (1 << 15)) && value
& (1 << 8)) { /* XA */
327 fprintf(stderr
, "%s: 10-bit addressing mode not supported\n",
331 if ((value
& (1 << 15)) && value
& (1 << 0)) { /* STT */
332 nack
= !!i2c_start_transfer(s
->bus
, s
->addr
[1], /* SA */
333 (~value
>> 9) & 1); /* TRX */
334 s
->stat
|= nack
<< 1; /* NACK */
335 s
->control
&= ~(1 << 0); /* STT */
338 s
->control
&= ~(1 << 1); /* STP */
340 s
->count_cur
= s
->count
;
341 omap_i2c_fifo_run(s
);
343 omap_i2c_interrupts_update(s
);
347 case 0x28: /* I2C_OA */
348 s
->addr
[0] = value
& 0x3ff;
351 case 0x2c: /* I2C_SA */
352 s
->addr
[1] = value
& 0x3ff;
355 case 0x30: /* I2C_PSC */
359 case 0x34: /* I2C_SCLL */
363 case 0x38: /* I2C_SCLH */
367 case 0x3c: /* I2C_SYSTEST */
368 s
->test
= value
& 0xf80f;
369 if (value
& (1 << 11)) /* SBB */
370 if (s
->revision
>= OMAP2_INTR_REV
) {
372 omap_i2c_interrupts_update(s
);
374 if (value
& (1 << 15)) /* ST_EN */
375 fprintf(stderr
, "%s: System Test not supported\n", __FUNCTION__
);
384 static void omap_i2c_writeb(void *opaque
, target_phys_addr_t addr
,
387 struct omap_i2c_s
*s
= (struct omap_i2c_s
*) opaque
;
388 int offset
= addr
& OMAP_MPUI_REG_MASK
;
391 case 0x1c: /* I2C_DATA */
393 /* XXX: remote access (qualifier) error - what's that? */
398 s
->fifo
|= value
& 0xff;
399 s
->stat
&= ~(1 << 10); /* XUDF */
401 s
->stat
&= ~(1 << 4); /* XRDY */
402 omap_i2c_fifo_run(s
);
403 omap_i2c_interrupts_update(s
);
412 static CPUReadMemoryFunc
* const omap_i2c_readfn
[] = {
413 omap_badwidth_read16
,
415 omap_badwidth_read16
,
418 static CPUWriteMemoryFunc
* const omap_i2c_writefn
[] = {
419 omap_i2c_writeb
, /* Only the last fifo write can be 8 bit. */
421 omap_badwidth_write16
,
424 struct omap_i2c_s
*omap_i2c_init(target_phys_addr_t base
,
425 qemu_irq irq
, qemu_irq
*dma
, omap_clk clk
)
428 struct omap_i2c_s
*s
= (struct omap_i2c_s
*)
429 g_malloc0(sizeof(struct omap_i2c_s
));
431 /* TODO: set a value greater or equal to real hardware */
436 s
->bus
= i2c_init_bus(NULL
, "i2c");
439 iomemtype
= cpu_register_io_memory(omap_i2c_readfn
,
440 omap_i2c_writefn
, s
, DEVICE_NATIVE_ENDIAN
);
441 cpu_register_physical_memory(base
, 0x800, iomemtype
);
446 struct omap_i2c_s
*omap2_i2c_init(struct omap_target_agent_s
*ta
,
447 qemu_irq irq
, qemu_irq
*dma
, omap_clk fclk
, omap_clk iclk
)
450 struct omap_i2c_s
*s
= (struct omap_i2c_s
*)
451 g_malloc0(sizeof(struct omap_i2c_s
));
457 s
->bus
= i2c_init_bus(NULL
, "i2c");
460 iomemtype
= l4_register_io_memory(omap_i2c_readfn
,
461 omap_i2c_writefn
, s
);
462 omap_l4_attach(ta
, 0, iomemtype
);
467 i2c_bus
*omap_i2c_bus(struct omap_i2c_s
*s
)