1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "hw/hw.h"
22#include "hw/arm/omap.h"
23#include "hw/sysbus.h"
24
25struct omap_gpio_s {
26 qemu_irq irq;
27 qemu_irq handler[16];
28
29 uint16_t inputs;
30 uint16_t outputs;
31 uint16_t dir;
32 uint16_t edge;
33 uint16_t mask;
34 uint16_t ints;
35 uint16_t pins;
36};
37
38#define TYPE_OMAP1_GPIO "omap-gpio"
39#define OMAP1_GPIO(obj) \
40 OBJECT_CHECK(struct omap_gpif_s, (obj), TYPE_OMAP1_GPIO)
41
42struct omap_gpif_s {
43 SysBusDevice parent_obj;
44
45 MemoryRegion iomem;
46 int mpu_model;
47 void *clk;
48 struct omap_gpio_s omap1;
49};
50
51
52static void omap_gpio_set(void *opaque, int line, int level)
53{
54 struct omap_gpio_s *s = &((struct omap_gpif_s *) opaque)->omap1;
55 uint16_t prev = s->inputs;
56
57 if (level)
58 s->inputs |= 1 << line;
59 else
60 s->inputs &= ~(1 << line);
61
62 if (((s->edge & s->inputs & ~prev) | (~s->edge & ~s->inputs & prev)) &
63 (1 << line) & s->dir & ~s->mask) {
64 s->ints |= 1 << line;
65 qemu_irq_raise(s->irq);
66 }
67}
68
69static uint64_t omap_gpio_read(void *opaque, hwaddr addr,
70 unsigned size)
71{
72 struct omap_gpio_s *s = (struct omap_gpio_s *) opaque;
73 int offset = addr & OMAP_MPUI_REG_MASK;
74
75 if (size != 2) {
76 return omap_badwidth_read16(opaque, addr);
77 }
78
79 switch (offset) {
80 case 0x00:
81 return s->inputs & s->pins;
82
83 case 0x04:
84 return s->outputs;
85
86 case 0x08:
87 return s->dir;
88
89 case 0x0c:
90 return s->edge;
91
92 case 0x10:
93 return s->mask;
94
95 case 0x14:
96 return s->ints;
97
98 case 0x18:
99 OMAP_BAD_REG(addr);
100 return s->pins;
101 }
102
103 OMAP_BAD_REG(addr);
104 return 0;
105}
106
107static void omap_gpio_write(void *opaque, hwaddr addr,
108 uint64_t value, unsigned size)
109{
110 struct omap_gpio_s *s = (struct omap_gpio_s *) opaque;
111 int offset = addr & OMAP_MPUI_REG_MASK;
112 uint16_t diff;
113 int ln;
114
115 if (size != 2) {
116 return omap_badwidth_write16(opaque, addr, value);
117 }
118
119 switch (offset) {
120 case 0x00:
121 OMAP_RO_REG(addr);
122 return;
123
124 case 0x04:
125 diff = (s->outputs ^ value) & ~s->dir;
126 s->outputs = value;
127 while ((ln = ffs(diff))) {
128 ln --;
129 if (s->handler[ln])
130 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
131 diff &= ~(1 << ln);
132 }
133 break;
134
135 case 0x08:
136 diff = s->outputs & (s->dir ^ value);
137 s->dir = value;
138
139 value = s->outputs & ~s->dir;
140 while ((ln = ffs(diff))) {
141 ln --;
142 if (s->handler[ln])
143 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
144 diff &= ~(1 << ln);
145 }
146 break;
147
148 case 0x0c:
149 s->edge = value;
150 break;
151
152 case 0x10:
153 s->mask = value;
154 break;
155
156 case 0x14:
157 s->ints &= ~value;
158 if (!s->ints)
159 qemu_irq_lower(s->irq);
160 break;
161
162 case 0x18:
163 OMAP_BAD_REG(addr);
164 s->pins = value;
165 break;
166
167 default:
168 OMAP_BAD_REG(addr);
169 return;
170 }
171}
172
173
174static const MemoryRegionOps omap_gpio_ops = {
175 .read = omap_gpio_read,
176 .write = omap_gpio_write,
177 .endianness = DEVICE_NATIVE_ENDIAN,
178};
179
180static void omap_gpio_reset(struct omap_gpio_s *s)
181{
182 s->inputs = 0;
183 s->outputs = ~0;
184 s->dir = ~0;
185 s->edge = ~0;
186 s->mask = ~0;
187 s->ints = 0;
188 s->pins = ~0;
189}
190
191struct omap2_gpio_s {
192 qemu_irq irq[2];
193 qemu_irq wkup;
194 qemu_irq *handler;
195 MemoryRegion iomem;
196
197 uint8_t revision;
198 uint8_t config[2];
199 uint32_t inputs;
200 uint32_t outputs;
201 uint32_t dir;
202 uint32_t level[2];
203 uint32_t edge[2];
204 uint32_t mask[2];
205 uint32_t wumask;
206 uint32_t ints[2];
207 uint32_t debounce;
208 uint8_t delay;
209};
210
211#define TYPE_OMAP2_GPIO "omap2-gpio"
212#define OMAP2_GPIO(obj) \
213 OBJECT_CHECK(struct omap2_gpif_s, (obj), TYPE_OMAP2_GPIO)
214
215struct omap2_gpif_s {
216 SysBusDevice parent_obj;
217
218 MemoryRegion iomem;
219 int mpu_model;
220 void *iclk;
221 void *fclk[6];
222 int modulecount;
223 struct omap2_gpio_s *modules;
224 qemu_irq *handler;
225 int autoidle;
226 int gpo;
227};
228
229
230static inline void omap2_gpio_module_int_update(struct omap2_gpio_s *s,
231 int line)
232{
233 qemu_set_irq(s->irq[line], s->ints[line] & s->mask[line]);
234}
235
236static void omap2_gpio_module_wake(struct omap2_gpio_s *s, int line)
237{
238 if (!(s->config[0] & (1 << 2)))
239 return;
240 if (!(s->config[0] & (3 << 3)))
241 return;
242 if (!(s->wumask & (1 << line)))
243 return;
244
245 qemu_irq_raise(s->wkup);
246}
247
248static inline void omap2_gpio_module_out_update(struct omap2_gpio_s *s,
249 uint32_t diff)
250{
251 int ln;
252
253 s->outputs ^= diff;
254 diff &= ~s->dir;
255 while ((ln = ffs(diff))) {
256 ln --;
257 qemu_set_irq(s->handler[ln], (s->outputs >> ln) & 1);
258 diff &= ~(1 << ln);
259 }
260}
261
262static void omap2_gpio_module_level_update(struct omap2_gpio_s *s, int line)
263{
264 s->ints[line] |= s->dir &
265 ((s->inputs & s->level[1]) | (~s->inputs & s->level[0]));
266 omap2_gpio_module_int_update(s, line);
267}
268
269static inline void omap2_gpio_module_int(struct omap2_gpio_s *s, int line)
270{
271 s->ints[0] |= 1 << line;
272 omap2_gpio_module_int_update(s, 0);
273 s->ints[1] |= 1 << line;
274 omap2_gpio_module_int_update(s, 1);
275 omap2_gpio_module_wake(s, line);
276}
277
278static void omap2_gpio_set(void *opaque, int line, int level)
279{
280 struct omap2_gpif_s *p = opaque;
281 struct omap2_gpio_s *s = &p->modules[line >> 5];
282
283 line &= 31;
284 if (level) {
285 if (s->dir & (1 << line) & ((~s->inputs & s->edge[0]) | s->level[1]))
286 omap2_gpio_module_int(s, line);
287 s->inputs |= 1 << line;
288 } else {
289 if (s->dir & (1 << line) & ((s->inputs & s->edge[1]) | s->level[0]))
290 omap2_gpio_module_int(s, line);
291 s->inputs &= ~(1 << line);
292 }
293}
294
295static void omap2_gpio_module_reset(struct omap2_gpio_s *s)
296{
297 s->config[0] = 0;
298 s->config[1] = 2;
299 s->ints[0] = 0;
300 s->ints[1] = 0;
301 s->mask[0] = 0;
302 s->mask[1] = 0;
303 s->wumask = 0;
304 s->dir = ~0;
305 s->level[0] = 0;
306 s->level[1] = 0;
307 s->edge[0] = 0;
308 s->edge[1] = 0;
309 s->debounce = 0;
310 s->delay = 0;
311}
312
313static uint32_t omap2_gpio_module_read(void *opaque, hwaddr addr)
314{
315 struct omap2_gpio_s *s = (struct omap2_gpio_s *) opaque;
316
317 switch (addr) {
318 case 0x00:
319 return s->revision;
320
321 case 0x10:
322 return s->config[0];
323
324 case 0x14:
325 return 0x01;
326
327 case 0x18:
328 return s->ints[0];
329
330 case 0x1c:
331 case 0x60:
332 case 0x64:
333 return s->mask[0];
334
335 case 0x20:
336 case 0x80:
337 case 0x84:
338 return s->wumask;
339
340 case 0x28:
341 return s->ints[1];
342
343 case 0x2c:
344 case 0x70:
345 case 0x74:
346 return s->mask[1];
347
348 case 0x30:
349 return s->config[1];
350
351 case 0x34:
352 return s->dir;
353
354 case 0x38:
355 return s->inputs;
356
357 case 0x3c:
358 case 0x90:
359 case 0x94:
360 return s->outputs;
361
362 case 0x40:
363 return s->level[0];
364
365 case 0x44:
366 return s->level[1];
367
368 case 0x48:
369 return s->edge[0];
370
371 case 0x4c:
372 return s->edge[1];
373
374 case 0x50:
375 return s->debounce;
376
377 case 0x54:
378 return s->delay;
379 }
380
381 OMAP_BAD_REG(addr);
382 return 0;
383}
384
385static void omap2_gpio_module_write(void *opaque, hwaddr addr,
386 uint32_t value)
387{
388 struct omap2_gpio_s *s = (struct omap2_gpio_s *) opaque;
389 uint32_t diff;
390 int ln;
391
392 switch (addr) {
393 case 0x00:
394 case 0x14:
395 case 0x38:
396 OMAP_RO_REG(addr);
397 break;
398
399 case 0x10:
400 if (((value >> 3) & 3) == 3)
401 fprintf(stderr, "%s: bad IDLEMODE value\n", __FUNCTION__);
402 if (value & 2)
403 omap2_gpio_module_reset(s);
404 s->config[0] = value & 0x1d;
405 break;
406
407 case 0x18:
408 if (s->ints[0] & value) {
409 s->ints[0] &= ~value;
410 omap2_gpio_module_level_update(s, 0);
411 }
412 break;
413
414 case 0x1c:
415 s->mask[0] = value;
416 omap2_gpio_module_int_update(s, 0);
417 break;
418
419 case 0x20:
420 s->wumask = value;
421 break;
422
423 case 0x28:
424 if (s->ints[1] & value) {
425 s->ints[1] &= ~value;
426 omap2_gpio_module_level_update(s, 1);
427 }
428 break;
429
430 case 0x2c:
431 s->mask[1] = value;
432 omap2_gpio_module_int_update(s, 1);
433 break;
434
435 case 0x30:
436 s->config[1] = value & 7;
437 break;
438
439 case 0x34:
440 diff = s->outputs & (s->dir ^ value);
441 s->dir = value;
442
443 value = s->outputs & ~s->dir;
444 while ((ln = ffs(diff))) {
445 diff &= ~(1 <<-- ln);
446 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
447 }
448
449 omap2_gpio_module_level_update(s, 0);
450 omap2_gpio_module_level_update(s, 1);
451 break;
452
453 case 0x3c:
454 omap2_gpio_module_out_update(s, s->outputs ^ value);
455 break;
456
457 case 0x40:
458 s->level[0] = value;
459 omap2_gpio_module_level_update(s, 0);
460 omap2_gpio_module_level_update(s, 1);
461 break;
462
463 case 0x44:
464 s->level[1] = value;
465 omap2_gpio_module_level_update(s, 0);
466 omap2_gpio_module_level_update(s, 1);
467 break;
468
469 case 0x48:
470 s->edge[0] = value;
471 break;
472
473 case 0x4c:
474 s->edge[1] = value;
475 break;
476
477 case 0x50:
478 s->debounce = value;
479 break;
480
481 case 0x54:
482 s->delay = value;
483 break;
484
485 case 0x60:
486 s->mask[0] &= ~value;
487 omap2_gpio_module_int_update(s, 0);
488 break;
489
490 case 0x64:
491 s->mask[0] |= value;
492 omap2_gpio_module_int_update(s, 0);
493 break;
494
495 case 0x70:
496 s->mask[1] &= ~value;
497 omap2_gpio_module_int_update(s, 1);
498 break;
499
500 case 0x74:
501 s->mask[1] |= value;
502 omap2_gpio_module_int_update(s, 1);
503 break;
504
505 case 0x80:
506 s->wumask &= ~value;
507 break;
508
509 case 0x84:
510 s->wumask |= value;
511 break;
512
513 case 0x90:
514 omap2_gpio_module_out_update(s, s->outputs & value);
515 break;
516
517 case 0x94:
518 omap2_gpio_module_out_update(s, ~s->outputs & value);
519 break;
520
521 default:
522 OMAP_BAD_REG(addr);
523 return;
524 }
525}
526
527static uint32_t omap2_gpio_module_readp(void *opaque, hwaddr addr)
528{
529 return omap2_gpio_module_read(opaque, addr & ~3) >> ((addr & 3) << 3);
530}
531
532static void omap2_gpio_module_writep(void *opaque, hwaddr addr,
533 uint32_t value)
534{
535 uint32_t cur = 0;
536 uint32_t mask = 0xffff;
537
538 switch (addr & ~3) {
539 case 0x00:
540 case 0x14:
541 case 0x38:
542 OMAP_RO_REG(addr);
543 break;
544
545 case 0x10:
546 case 0x1c:
547 case 0x20:
548 case 0x2c:
549 case 0x30:
550 case 0x34:
551 case 0x3c:
552 case 0x40:
553 case 0x44:
554 case 0x48:
555 case 0x4c:
556 case 0x50:
557 case 0x54:
558 cur = omap2_gpio_module_read(opaque, addr & ~3) &
559 ~(mask << ((addr & 3) << 3));
560
561
562 case 0x18:
563 case 0x28:
564 case 0x60:
565 case 0x64:
566 case 0x70:
567 case 0x74:
568 case 0x80:
569 case 0x84:
570 case 0x90:
571 case 0x94:
572 value <<= (addr & 3) << 3;
573 omap2_gpio_module_write(opaque, addr, cur | value);
574 break;
575
576 default:
577 OMAP_BAD_REG(addr);
578 return;
579 }
580}
581
582static const MemoryRegionOps omap2_gpio_module_ops = {
583 .old_mmio = {
584 .read = {
585 omap2_gpio_module_readp,
586 omap2_gpio_module_readp,
587 omap2_gpio_module_read,
588 },
589 .write = {
590 omap2_gpio_module_writep,
591 omap2_gpio_module_writep,
592 omap2_gpio_module_write,
593 },
594 },
595 .endianness = DEVICE_NATIVE_ENDIAN,
596};
597
598static void omap_gpif_reset(DeviceState *dev)
599{
600 struct omap_gpif_s *s = OMAP1_GPIO(dev);
601
602 omap_gpio_reset(&s->omap1);
603}
604
605static void omap2_gpif_reset(DeviceState *dev)
606{
607 struct omap2_gpif_s *s = OMAP2_GPIO(dev);
608 int i;
609
610 for (i = 0; i < s->modulecount; i++) {
611 omap2_gpio_module_reset(&s->modules[i]);
612 }
613 s->autoidle = 0;
614 s->gpo = 0;
615}
616
617static uint64_t omap2_gpif_top_read(void *opaque, hwaddr addr,
618 unsigned size)
619{
620 struct omap2_gpif_s *s = (struct omap2_gpif_s *) opaque;
621
622 switch (addr) {
623 case 0x00:
624 return 0x18;
625
626 case 0x10:
627 return s->autoidle;
628
629 case 0x14:
630 return 0x01;
631
632 case 0x18:
633 return 0x00;
634
635 case 0x40:
636 return s->gpo;
637
638 case 0x50:
639 return 0x00;
640 }
641
642 OMAP_BAD_REG(addr);
643 return 0;
644}
645
646static void omap2_gpif_top_write(void *opaque, hwaddr addr,
647 uint64_t value, unsigned size)
648{
649 struct omap2_gpif_s *s = (struct omap2_gpif_s *) opaque;
650
651 switch (addr) {
652 case 0x00:
653 case 0x14:
654 case 0x18:
655 case 0x50:
656 OMAP_RO_REG(addr);
657 break;
658
659 case 0x10:
660 if (value & (1 << 1))
661 omap2_gpif_reset(DEVICE(s));
662 s->autoidle = value & 1;
663 break;
664
665 case 0x40:
666 s->gpo = value & 1;
667 break;
668
669 default:
670 OMAP_BAD_REG(addr);
671 return;
672 }
673}
674
675static const MemoryRegionOps omap2_gpif_top_ops = {
676 .read = omap2_gpif_top_read,
677 .write = omap2_gpif_top_write,
678 .endianness = DEVICE_NATIVE_ENDIAN,
679};
680
681static int omap_gpio_init(SysBusDevice *sbd)
682{
683 DeviceState *dev = DEVICE(sbd);
684 struct omap_gpif_s *s = OMAP1_GPIO(dev);
685
686 if (!s->clk) {
687 hw_error("omap-gpio: clk not connected\n");
688 }
689 qdev_init_gpio_in(dev, omap_gpio_set, 16);
690 qdev_init_gpio_out(dev, s->omap1.handler, 16);
691 sysbus_init_irq(sbd, &s->omap1.irq);
692 memory_region_init_io(&s->iomem, OBJECT(s), &omap_gpio_ops, &s->omap1,
693 "omap.gpio", 0x1000);
694 sysbus_init_mmio(sbd, &s->iomem);
695 return 0;
696}
697
698static int omap2_gpio_init(SysBusDevice *sbd)
699{
700 DeviceState *dev = DEVICE(sbd);
701 struct omap2_gpif_s *s = OMAP2_GPIO(dev);
702 int i;
703
704 if (!s->iclk) {
705 hw_error("omap2-gpio: iclk not connected\n");
706 }
707 if (s->mpu_model < omap3430) {
708 s->modulecount = (s->mpu_model < omap2430) ? 4 : 5;
709 memory_region_init_io(&s->iomem, OBJECT(s), &omap2_gpif_top_ops, s,
710 "omap2.gpio", 0x1000);
711 sysbus_init_mmio(sbd, &s->iomem);
712 } else {
713 s->modulecount = 6;
714 }
715 s->modules = g_malloc0(s->modulecount * sizeof(struct omap2_gpio_s));
716 s->handler = g_malloc0(s->modulecount * 32 * sizeof(qemu_irq));
717 qdev_init_gpio_in(dev, omap2_gpio_set, s->modulecount * 32);
718 qdev_init_gpio_out(dev, s->handler, s->modulecount * 32);
719 for (i = 0; i < s->modulecount; i++) {
720 struct omap2_gpio_s *m = &s->modules[i];
721 if (!s->fclk[i]) {
722 hw_error("omap2-gpio: fclk%d not connected\n", i);
723 }
724 m->revision = (s->mpu_model < omap3430) ? 0x18 : 0x25;
725 m->handler = &s->handler[i * 32];
726 sysbus_init_irq(sbd, &m->irq[0]);
727 sysbus_init_irq(sbd, &m->irq[1]);
728 sysbus_init_irq(sbd, &m->wkup);
729 memory_region_init_io(&m->iomem, OBJECT(s), &omap2_gpio_module_ops, m,
730 "omap.gpio-module", 0x1000);
731 sysbus_init_mmio(sbd, &m->iomem);
732 }
733 return 0;
734}
735
736
737
738
739
740
741
742
743
744
745
746
747
748static Property omap_gpio_properties[] = {
749 DEFINE_PROP_INT32("mpu_model", struct omap_gpif_s, mpu_model, 0),
750 DEFINE_PROP_PTR("clk", struct omap_gpif_s, clk),
751 DEFINE_PROP_END_OF_LIST(),
752};
753
754static void omap_gpio_class_init(ObjectClass *klass, void *data)
755{
756 DeviceClass *dc = DEVICE_CLASS(klass);
757 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
758
759 k->init = omap_gpio_init;
760 dc->reset = omap_gpif_reset;
761 dc->props = omap_gpio_properties;
762
763 dc->cannot_instantiate_with_device_add_yet = true;
764}
765
766static const TypeInfo omap_gpio_info = {
767 .name = TYPE_OMAP1_GPIO,
768 .parent = TYPE_SYS_BUS_DEVICE,
769 .instance_size = sizeof(struct omap_gpif_s),
770 .class_init = omap_gpio_class_init,
771};
772
773static Property omap2_gpio_properties[] = {
774 DEFINE_PROP_INT32("mpu_model", struct omap2_gpif_s, mpu_model, 0),
775 DEFINE_PROP_PTR("iclk", struct omap2_gpif_s, iclk),
776 DEFINE_PROP_PTR("fclk0", struct omap2_gpif_s, fclk[0]),
777 DEFINE_PROP_PTR("fclk1", struct omap2_gpif_s, fclk[1]),
778 DEFINE_PROP_PTR("fclk2", struct omap2_gpif_s, fclk[2]),
779 DEFINE_PROP_PTR("fclk3", struct omap2_gpif_s, fclk[3]),
780 DEFINE_PROP_PTR("fclk4", struct omap2_gpif_s, fclk[4]),
781 DEFINE_PROP_PTR("fclk5", struct omap2_gpif_s, fclk[5]),
782 DEFINE_PROP_END_OF_LIST(),
783};
784
785static void omap2_gpio_class_init(ObjectClass *klass, void *data)
786{
787 DeviceClass *dc = DEVICE_CLASS(klass);
788 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
789
790 k->init = omap2_gpio_init;
791 dc->reset = omap2_gpif_reset;
792 dc->props = omap2_gpio_properties;
793
794 dc->cannot_instantiate_with_device_add_yet = true;
795}
796
797static const TypeInfo omap2_gpio_info = {
798 .name = TYPE_OMAP2_GPIO,
799 .parent = TYPE_SYS_BUS_DEVICE,
800 .instance_size = sizeof(struct omap2_gpif_s),
801 .class_init = omap2_gpio_class_init,
802};
803
804static void omap_gpio_register_types(void)
805{
806 type_register_static(&omap_gpio_info);
807 type_register_static(&omap2_gpio_info);
808}
809
810type_init(omap_gpio_register_types)
811