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