1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "qemu/osdep.h"
21#include "qemu/log.h"
22#include "qemu/module.h"
23#include "hw/i2c/i2c.h"
24#include "hw/irq.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"
30
31#define TYPE_OMAP_I2C "omap_i2c"
32#define OMAP_I2C(obj) OBJECT_CHECK(OMAPI2CState, (obj), TYPE_OMAP_I2C)
33
34typedef struct OMAPI2CState {
35 SysBusDevice parent_obj;
36
37 MemoryRegion iomem;
38 qemu_irq irq;
39 qemu_irq drq[2];
40 I2CBus *bus;
41
42 uint8_t revision;
43 void *iclk;
44 void *fclk;
45
46 uint8_t mask;
47 uint16_t stat;
48 uint16_t dma;
49 uint16_t count;
50 int count_cur;
51 uint32_t fifo;
52 int rxlen;
53 int txlen;
54 uint16_t control;
55 uint16_t addr[2];
56 uint8_t divider;
57 uint8_t times[2];
58 uint16_t test;
59} OMAPI2CState;
60
61#define OMAP2_INTR_REV 0x34
62#define OMAP2_GC_REV 0x34
63
64static void omap_i2c_interrupts_update(OMAPI2CState *s)
65{
66 qemu_set_irq(s->irq, s->stat & s->mask);
67 if ((s->dma >> 15) & 1)
68 qemu_set_irq(s->drq[0], (s->stat >> 3) & 1);
69 if ((s->dma >> 7) & 1)
70 qemu_set_irq(s->drq[1], (s->stat >> 4) & 1);
71}
72
73static void omap_i2c_fifo_run(OMAPI2CState *s)
74{
75 int ack = 1;
76
77 if (!i2c_bus_busy(s->bus))
78 return;
79
80 if ((s->control >> 2) & 1) {
81 if ((s->control >> 1) & 1) {
82 i2c_end_transfer(s->bus);
83 s->control &= ~(1 << 1);
84 s->count_cur = s->count;
85 s->txlen = 0;
86 } else if ((s->control >> 9) & 1) {
87 while (ack && s->txlen)
88 ack = (i2c_send(s->bus,
89 (s->fifo >> ((-- s->txlen) << 3)) &
90 0xff) >= 0);
91 s->stat |= 1 << 4;
92 } else {
93 while (s->rxlen < 4)
94 s->fifo |= i2c_recv(s->bus) << ((s->rxlen ++) << 3);
95 s->stat |= 1 << 3;
96 }
97 } else {
98 if ((s->control >> 9) & 1) {
99 while (ack && s->count_cur && s->txlen) {
100 ack = (i2c_send(s->bus,
101 (s->fifo >> ((-- s->txlen) << 3)) &
102 0xff) >= 0);
103 s->count_cur --;
104 }
105 if (ack && s->count_cur)
106 s->stat |= 1 << 4;
107 else
108 s->stat &= ~(1 << 4);
109 if (!s->count_cur) {
110 s->stat |= 1 << 2;
111 s->control &= ~(1 << 10);
112 }
113 } else {
114 while (s->count_cur && s->rxlen < 4) {
115 s->fifo |= i2c_recv(s->bus) << ((s->rxlen ++) << 3);
116 s->count_cur --;
117 }
118 if (s->rxlen)
119 s->stat |= 1 << 3;
120 else
121 s->stat &= ~(1 << 3);
122 }
123 if (!s->count_cur) {
124 if ((s->control >> 1) & 1) {
125 i2c_end_transfer(s->bus);
126 s->control &= ~(1 << 1);
127 s->count_cur = s->count;
128 s->txlen = 0;
129 } else {
130 s->stat |= 1 << 2;
131 s->control &= ~(1 << 10);
132 }
133 }
134 }
135
136 s->stat |= (!ack) << 1;
137 if (!ack)
138 s->control &= ~(1 << 1);
139}
140
141static void omap_i2c_reset(DeviceState *dev)
142{
143 OMAPI2CState *s = OMAP_I2C(dev);
144
145 s->mask = 0;
146 s->stat = 0;
147 s->dma = 0;
148 s->count = 0;
149 s->count_cur = 0;
150 s->fifo = 0;
151 s->rxlen = 0;
152 s->txlen = 0;
153 s->control = 0;
154 s->addr[0] = 0;
155 s->addr[1] = 0;
156 s->divider = 0;
157 s->times[0] = 0;
158 s->times[1] = 0;
159 s->test = 0;
160}
161
162static uint32_t omap_i2c_read(void *opaque, hwaddr addr)
163{
164 OMAPI2CState *s = opaque;
165 int offset = addr & OMAP_MPUI_REG_MASK;
166 uint16_t ret;
167
168 switch (offset) {
169 case 0x00:
170 return s->revision;
171
172 case 0x04:
173 return s->mask;
174
175 case 0x08:
176 return s->stat | (i2c_bus_busy(s->bus) << 12);
177
178 case 0x0c:
179 if (s->revision >= OMAP2_INTR_REV)
180 break;
181 ret = ctz32(s->stat & s->mask);
182 if (ret != 32) {
183 s->stat ^= 1 << ret;
184 ret++;
185 } else {
186 ret = 0;
187 }
188 omap_i2c_interrupts_update(s);
189 return ret;
190
191 case 0x10:
192 return (s->control >> 15) & 1;
193
194 case 0x14:
195 return s->dma;
196
197 case 0x18:
198 return s->count_cur;
199
200 case 0x1c:
201 ret = 0;
202 if (s->control & (1 << 14)) {
203 ret |= ((s->fifo >> 0) & 0xff) << 8;
204 ret |= ((s->fifo >> 8) & 0xff) << 0;
205 } else {
206 ret |= ((s->fifo >> 8) & 0xff) << 8;
207 ret |= ((s->fifo >> 0) & 0xff) << 0;
208 }
209 if (s->rxlen == 1) {
210 s->stat |= 1 << 15;
211 s->rxlen = 0;
212 } else if (s->rxlen > 1) {
213 if (s->rxlen > 2)
214 s->fifo >>= 16;
215 s->rxlen -= 2;
216 } else {
217
218 }
219 if (!s->rxlen) {
220 s->stat &= ~(1 << 3);
221 if (((s->control >> 10) & 1) &&
222 ((~s->control >> 9) & 1)) {
223 s->stat |= 1 << 2;
224 s->control &= ~(1 << 10);
225 }
226 }
227 s->stat &= ~(1 << 11);
228 omap_i2c_fifo_run(s);
229 omap_i2c_interrupts_update(s);
230 return ret;
231
232 case 0x20:
233 return 0;
234
235 case 0x24:
236 return s->control;
237
238 case 0x28:
239 return s->addr[0];
240
241 case 0x2c:
242 return s->addr[1];
243
244 case 0x30:
245 return s->divider;
246
247 case 0x34:
248 return s->times[0];
249
250 case 0x38:
251 return s->times[1];
252
253 case 0x3c:
254 if (s->test & (1 << 15)) {
255 s->test ^= 0xa;
256 return s->test;
257 } else
258 return s->test & ~0x300f;
259 }
260
261 OMAP_BAD_REG(addr);
262 return 0;
263}
264
265static void omap_i2c_write(void *opaque, hwaddr addr,
266 uint32_t value)
267{
268 OMAPI2CState *s = opaque;
269 int offset = addr & OMAP_MPUI_REG_MASK;
270 int nack;
271
272 switch (offset) {
273 case 0x00:
274 case 0x0c:
275 case 0x10:
276 OMAP_RO_REG(addr);
277 return;
278
279 case 0x04:
280 s->mask = value & (s->revision < OMAP2_GC_REV ? 0x1f : 0x3f);
281 break;
282
283 case 0x08:
284 if (s->revision < OMAP2_INTR_REV) {
285 OMAP_RO_REG(addr);
286 return;
287 }
288
289
290 s->stat &= ~(value & 0x27);
291 omap_i2c_interrupts_update(s);
292 break;
293
294 case 0x14:
295 s->dma = value & 0x8080;
296 if (value & (1 << 15))
297 s->mask &= ~(1 << 3);
298 if (value & (1 << 7))
299 s->mask &= ~(1 << 4);
300 break;
301
302 case 0x18:
303 s->count = value;
304 break;
305
306 case 0x1c:
307 if (s->txlen > 2) {
308
309 break;
310 }
311 s->fifo <<= 16;
312 s->txlen += 2;
313 if (s->control & (1 << 14)) {
314 s->fifo |= ((value >> 8) & 0xff) << 8;
315 s->fifo |= ((value >> 0) & 0xff) << 0;
316 } else {
317 s->fifo |= ((value >> 0) & 0xff) << 8;
318 s->fifo |= ((value >> 8) & 0xff) << 0;
319 }
320 s->stat &= ~(1 << 10);
321 if (s->txlen > 2)
322 s->stat &= ~(1 << 4);
323 omap_i2c_fifo_run(s);
324 omap_i2c_interrupts_update(s);
325 break;
326
327 case 0x20:
328 if (s->revision < OMAP2_INTR_REV) {
329 OMAP_BAD_REG(addr);
330 return;
331 }
332
333 if (value & 2) {
334 omap_i2c_reset(DEVICE(s));
335 }
336 break;
337
338 case 0x24:
339 s->control = value & 0xcf87;
340 if (~value & (1 << 15)) {
341 if (s->revision < OMAP2_INTR_REV) {
342 omap_i2c_reset(DEVICE(s));
343 }
344 break;
345 }
346 if ((value & (1 << 15)) && !(value & (1 << 10))) {
347 qemu_log_mask(LOG_UNIMP, "%s: I^2C slave mode not supported\n",
348 __func__);
349 break;
350 }
351 if ((value & (1 << 15)) && value & (1 << 8)) {
352 qemu_log_mask(LOG_UNIMP,
353 "%s: 10-bit addressing mode not supported\n",
354 __func__);
355 break;
356 }
357 if ((value & (1 << 15)) && value & (1 << 0)) {
358 nack = !!i2c_start_transfer(s->bus, s->addr[1],
359 (~value >> 9) & 1);
360 s->stat |= nack << 1;
361 s->control &= ~(1 << 0);
362 s->fifo = 0;
363 if (nack)
364 s->control &= ~(1 << 1);
365 else {
366 s->count_cur = s->count;
367 omap_i2c_fifo_run(s);
368 }
369 omap_i2c_interrupts_update(s);
370 }
371 break;
372
373 case 0x28:
374 s->addr[0] = value & 0x3ff;
375 break;
376
377 case 0x2c:
378 s->addr[1] = value & 0x3ff;
379 break;
380
381 case 0x30:
382 s->divider = value;
383 break;
384
385 case 0x34:
386 s->times[0] = value;
387 break;
388
389 case 0x38:
390 s->times[1] = value;
391 break;
392
393 case 0x3c:
394 s->test = value & 0xf80f;
395 if (value & (1 << 11))
396 if (s->revision >= OMAP2_INTR_REV) {
397 s->stat |= 0x3f;
398 omap_i2c_interrupts_update(s);
399 }
400 if (value & (1 << 15)) {
401 qemu_log_mask(LOG_UNIMP,
402 "%s: System Test not supported\n", __func__);
403 }
404 break;
405
406 default:
407 OMAP_BAD_REG(addr);
408 return;
409 }
410}
411
412static void omap_i2c_writeb(void *opaque, hwaddr addr,
413 uint32_t value)
414{
415 OMAPI2CState *s = opaque;
416 int offset = addr & OMAP_MPUI_REG_MASK;
417
418 switch (offset) {
419 case 0x1c:
420 if (s->txlen > 2) {
421
422 break;
423 }
424 s->fifo <<= 8;
425 s->txlen += 1;
426 s->fifo |= value & 0xff;
427 s->stat &= ~(1 << 10);
428 if (s->txlen > 2)
429 s->stat &= ~(1 << 4);
430 omap_i2c_fifo_run(s);
431 omap_i2c_interrupts_update(s);
432 break;
433
434 default:
435 OMAP_BAD_REG(addr);
436 return;
437 }
438}
439
440static uint64_t omap_i2c_readfn(void *opaque, hwaddr addr,
441 unsigned size)
442{
443 switch (size) {
444 case 2:
445 return omap_i2c_read(opaque, addr);
446 default:
447 return omap_badwidth_read16(opaque, addr);
448 }
449}
450
451static void omap_i2c_writefn(void *opaque, hwaddr addr,
452 uint64_t value, unsigned size)
453{
454 switch (size) {
455 case 1:
456
457 omap_i2c_writeb(opaque, addr, value);
458 break;
459 case 2:
460 omap_i2c_write(opaque, addr, value);
461 break;
462 default:
463 omap_badwidth_write16(opaque, addr, value);
464 break;
465 }
466}
467
468static const MemoryRegionOps omap_i2c_ops = {
469 .read = omap_i2c_readfn,
470 .write = omap_i2c_writefn,
471 .valid.min_access_size = 1,
472 .valid.max_access_size = 4,
473 .endianness = DEVICE_NATIVE_ENDIAN,
474};
475
476static void omap_i2c_init(Object *obj)
477{
478 DeviceState *dev = DEVICE(obj);
479 OMAPI2CState *s = OMAP_I2C(obj);
480 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
481
482 sysbus_init_irq(sbd, &s->irq);
483 sysbus_init_irq(sbd, &s->drq[0]);
484 sysbus_init_irq(sbd, &s->drq[1]);
485 sysbus_init_mmio(sbd, &s->iomem);
486 s->bus = i2c_init_bus(dev, NULL);
487}
488
489static void omap_i2c_realize(DeviceState *dev, Error **errp)
490{
491 OMAPI2CState *s = OMAP_I2C(dev);
492
493 memory_region_init_io(&s->iomem, OBJECT(dev), &omap_i2c_ops, s, "omap.i2c",
494 (s->revision < OMAP2_INTR_REV) ? 0x800 : 0x1000);
495
496 if (!s->fclk) {
497 error_setg(errp, "omap_i2c: fclk not connected");
498 return;
499 }
500 if (s->revision >= OMAP2_INTR_REV && !s->iclk) {
501
502 error_setg(errp, "omap_i2c: iclk not connected");
503 return;
504 }
505}
506
507static Property omap_i2c_properties[] = {
508 DEFINE_PROP_UINT8("revision", OMAPI2CState, revision, 0),
509 DEFINE_PROP_PTR("iclk", OMAPI2CState, iclk),
510 DEFINE_PROP_PTR("fclk", OMAPI2CState, fclk),
511 DEFINE_PROP_END_OF_LIST(),
512};
513
514static void omap_i2c_class_init(ObjectClass *klass, void *data)
515{
516 DeviceClass *dc = DEVICE_CLASS(klass);
517
518 dc->props = omap_i2c_properties;
519 dc->reset = omap_i2c_reset;
520
521 dc->user_creatable = false;
522 dc->realize = omap_i2c_realize;
523}
524
525static const TypeInfo omap_i2c_info = {
526 .name = TYPE_OMAP_I2C,
527 .parent = TYPE_SYS_BUS_DEVICE,
528 .instance_size = sizeof(OMAPI2CState),
529 .instance_init = omap_i2c_init,
530 .class_init = omap_i2c_class_init,
531};
532
533static void omap_i2c_register_types(void)
534{
535 type_register_static(&omap_i2c_info);
536}
537
538I2CBus *omap_i2c_bus(DeviceState *omap_i2c)
539{
540 OMAPI2CState *s = OMAP_I2C(omap_i2c);
541 return s->bus;
542}
543
544type_init(omap_i2c_register_types)
545