2 * Luminary Micro Stellaris peripherals
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "hw/sysbus.h"
13 #include "hw/ssi/ssi.h"
14 #include "hw/arm/arm.h"
15 #include "hw/devices.h"
16 #include "qemu/timer.h"
17 #include "hw/i2c/i2c.h"
19 #include "hw/boards.h"
21 #include "exec/address-spaces.h"
22 #include "sysemu/sysemu.h"
23 #include "hw/char/pl011.h"
33 #define BP_OLED_I2C 0x01
34 #define BP_OLED_SSI 0x02
35 #define BP_GAMEPAD 0x04
37 #define NUM_IRQ_LINES 64
39 typedef const struct {
49 } stellaris_board_info;
51 /* General purpose timer module. */
53 #define TYPE_STELLARIS_GPTM "stellaris-gptm"
54 #define STELLARIS_GPTM(obj) \
55 OBJECT_CHECK(gptm_state, (obj), TYPE_STELLARIS_GPTM)
57 typedef struct gptm_state {
58 SysBusDevice parent_obj;
69 uint32_t match_prescale[2];
72 struct gptm_state *opaque[2];
74 /* The timers have an alternate output used to trigger the ADC. */
79 static void gptm_update_irq(gptm_state *s)
82 level = (s->state & s->mask) != 0;
83 qemu_set_irq(s->irq, level);
86 static void gptm_stop(gptm_state *s, int n)
88 timer_del(s->timer[n]);
91 static void gptm_reload(gptm_state *s, int n, int reset)
95 tick = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
100 /* 32-bit CountDown. */
102 count = s->load[0] | (s->load[1] << 16);
103 tick += (int64_t)count * system_clock_scale;
104 } else if (s->config == 1) {
105 /* 32-bit RTC. 1Hz tick. */
106 tick += NANOSECONDS_PER_SECOND;
107 } else if (s->mode[n] == 0xa) {
108 /* PWM mode. Not implemented. */
110 hw_error("TODO: 16-bit timer mode 0x%x\n", s->mode[n]);
113 timer_mod(s->timer[n], tick);
116 static void gptm_tick(void *opaque)
118 gptm_state **p = (gptm_state **)opaque;
124 if (s->config == 0) {
126 if ((s->control & 0x20)) {
127 /* Output trigger. */
128 qemu_irq_pulse(s->trigger);
130 if (s->mode[0] & 1) {
135 gptm_reload(s, 0, 0);
137 } else if (s->config == 1) {
141 match = s->match[0] | (s->match[1] << 16);
147 gptm_reload(s, 0, 0);
148 } else if (s->mode[n] == 0xa) {
149 /* PWM mode. Not implemented. */
151 hw_error("TODO: 16-bit timer mode 0x%x\n", s->mode[n]);
156 static uint64_t gptm_read(void *opaque, hwaddr offset,
159 gptm_state *s = (gptm_state *)opaque;
164 case 0x04: /* TAMR */
166 case 0x08: /* TBMR */
175 return s->state & s->mask;
178 case 0x28: /* TAILR */
179 return s->load[0] | ((s->config < 4) ? (s->load[1] << 16) : 0);
180 case 0x2c: /* TBILR */
182 case 0x30: /* TAMARCHR */
183 return s->match[0] | ((s->config < 4) ? (s->match[1] << 16) : 0);
184 case 0x34: /* TBMATCHR */
186 case 0x38: /* TAPR */
187 return s->prescale[0];
188 case 0x3c: /* TBPR */
189 return s->prescale[1];
190 case 0x40: /* TAPMR */
191 return s->match_prescale[0];
192 case 0x44: /* TBPMR */
193 return s->match_prescale[1];
195 if (s->config == 1) {
198 qemu_log_mask(LOG_UNIMP,
199 "GPTM: read of TAR but timer read not supported");
202 qemu_log_mask(LOG_UNIMP,
203 "GPTM: read of TBR but timer read not supported");
206 qemu_log_mask(LOG_GUEST_ERROR,
207 "GPTM: read at bad offset 0x%x\n", (int)offset);
212 static void gptm_write(void *opaque, hwaddr offset,
213 uint64_t value, unsigned size)
215 gptm_state *s = (gptm_state *)opaque;
218 /* The timers should be disabled before changing the configuration.
219 We take advantage of this and defer everything until the timer
225 case 0x04: /* TAMR */
228 case 0x08: /* TBMR */
234 /* TODO: Implement pause. */
235 if ((oldval ^ value) & 1) {
237 gptm_reload(s, 0, 1);
242 if (((oldval ^ value) & 0x100) && s->config >= 4) {
244 gptm_reload(s, 1, 1);
251 s->mask = value & 0x77;
257 case 0x28: /* TAILR */
258 s->load[0] = value & 0xffff;
260 s->load[1] = value >> 16;
263 case 0x2c: /* TBILR */
264 s->load[1] = value & 0xffff;
266 case 0x30: /* TAMARCHR */
267 s->match[0] = value & 0xffff;
269 s->match[1] = value >> 16;
272 case 0x34: /* TBMATCHR */
273 s->match[1] = value >> 16;
275 case 0x38: /* TAPR */
276 s->prescale[0] = value;
278 case 0x3c: /* TBPR */
279 s->prescale[1] = value;
281 case 0x40: /* TAPMR */
282 s->match_prescale[0] = value;
284 case 0x44: /* TBPMR */
285 s->match_prescale[0] = value;
288 hw_error("gptm_write: Bad offset 0x%x\n", (int)offset);
293 static const MemoryRegionOps gptm_ops = {
296 .endianness = DEVICE_NATIVE_ENDIAN,
299 static const VMStateDescription vmstate_stellaris_gptm = {
300 .name = "stellaris_gptm",
302 .minimum_version_id = 1,
303 .fields = (VMStateField[]) {
304 VMSTATE_UINT32(config, gptm_state),
305 VMSTATE_UINT32_ARRAY(mode, gptm_state, 2),
306 VMSTATE_UINT32(control, gptm_state),
307 VMSTATE_UINT32(state, gptm_state),
308 VMSTATE_UINT32(mask, gptm_state),
310 VMSTATE_UINT32_ARRAY(load, gptm_state, 2),
311 VMSTATE_UINT32_ARRAY(match, gptm_state, 2),
312 VMSTATE_UINT32_ARRAY(prescale, gptm_state, 2),
313 VMSTATE_UINT32_ARRAY(match_prescale, gptm_state, 2),
314 VMSTATE_UINT32(rtc, gptm_state),
315 VMSTATE_INT64_ARRAY(tick, gptm_state, 2),
316 VMSTATE_TIMER_PTR_ARRAY(timer, gptm_state, 2),
317 VMSTATE_END_OF_LIST()
321 static void stellaris_gptm_init(Object *obj)
323 DeviceState *dev = DEVICE(obj);
324 gptm_state *s = STELLARIS_GPTM(obj);
325 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
327 sysbus_init_irq(sbd, &s->irq);
328 qdev_init_gpio_out(dev, &s->trigger, 1);
330 memory_region_init_io(&s->iomem, obj, &gptm_ops, s,
332 sysbus_init_mmio(sbd, &s->iomem);
334 s->opaque[0] = s->opaque[1] = s;
335 s->timer[0] = timer_new_ns(QEMU_CLOCK_VIRTUAL, gptm_tick, &s->opaque[0]);
336 s->timer[1] = timer_new_ns(QEMU_CLOCK_VIRTUAL, gptm_tick, &s->opaque[1]);
340 /* System controller. */
359 stellaris_board_info *board;
362 static void ssys_update(ssys_state *s)
364 qemu_set_irq(s->irq, (s->int_status & s->int_mask) != 0);
367 static uint32_t pllcfg_sandstorm[16] = {
369 0x1ae0, /* 1.8432 Mhz */
371 0xd573, /* 2.4576 Mhz */
372 0x37a6, /* 3.57954 Mhz */
373 0x1ae2, /* 3.6864 Mhz */
375 0x98bc, /* 4.906 Mhz */
376 0x935b, /* 4.9152 Mhz */
378 0x4dee, /* 5.12 Mhz */
380 0x75db, /* 6.144 Mhz */
381 0x1ae6, /* 7.3728 Mhz */
383 0x585b /* 8.192 Mhz */
386 static uint32_t pllcfg_fury[16] = {
388 0x1b20, /* 1.8432 Mhz */
390 0xf42b, /* 2.4576 Mhz */
391 0x37e3, /* 3.57954 Mhz */
392 0x1b21, /* 3.6864 Mhz */
394 0x98ee, /* 4.906 Mhz */
395 0xd5b4, /* 4.9152 Mhz */
397 0x4e27, /* 5.12 Mhz */
399 0xec1c, /* 6.144 Mhz */
400 0x1b23, /* 7.3728 Mhz */
402 0xb11c /* 8.192 Mhz */
405 #define DID0_VER_MASK 0x70000000
406 #define DID0_VER_0 0x00000000
407 #define DID0_VER_1 0x10000000
409 #define DID0_CLASS_MASK 0x00FF0000
410 #define DID0_CLASS_SANDSTORM 0x00000000
411 #define DID0_CLASS_FURY 0x00010000
413 static int ssys_board_class(const ssys_state *s)
415 uint32_t did0 = s->board->did0;
416 switch (did0 & DID0_VER_MASK) {
418 return DID0_CLASS_SANDSTORM;
420 switch (did0 & DID0_CLASS_MASK) {
421 case DID0_CLASS_SANDSTORM:
422 case DID0_CLASS_FURY:
423 return did0 & DID0_CLASS_MASK;
425 /* for unknown classes, fall through */
427 hw_error("ssys_board_class: Unknown class 0x%08x\n", did0);
431 static uint64_t ssys_read(void *opaque, hwaddr offset,
434 ssys_state *s = (ssys_state *)opaque;
437 case 0x000: /* DID0 */
438 return s->board->did0;
439 case 0x004: /* DID1 */
440 return s->board->did1;
441 case 0x008: /* DC0 */
442 return s->board->dc0;
443 case 0x010: /* DC1 */
444 return s->board->dc1;
445 case 0x014: /* DC2 */
446 return s->board->dc2;
447 case 0x018: /* DC3 */
448 return s->board->dc3;
449 case 0x01c: /* DC4 */
450 return s->board->dc4;
451 case 0x030: /* PBORCTL */
453 case 0x034: /* LDOPCTL */
455 case 0x040: /* SRCR0 */
457 case 0x044: /* SRCR1 */
459 case 0x048: /* SRCR2 */
461 case 0x050: /* RIS */
462 return s->int_status;
463 case 0x054: /* IMC */
465 case 0x058: /* MISC */
466 return s->int_status & s->int_mask;
467 case 0x05c: /* RESC */
469 case 0x060: /* RCC */
471 case 0x064: /* PLLCFG */
474 xtal = (s->rcc >> 6) & 0xf;
475 switch (ssys_board_class(s)) {
476 case DID0_CLASS_FURY:
477 return pllcfg_fury[xtal];
478 case DID0_CLASS_SANDSTORM:
479 return pllcfg_sandstorm[xtal];
481 hw_error("ssys_read: Unhandled class for PLLCFG read.\n");
485 case 0x070: /* RCC2 */
487 case 0x100: /* RCGC0 */
489 case 0x104: /* RCGC1 */
491 case 0x108: /* RCGC2 */
493 case 0x110: /* SCGC0 */
495 case 0x114: /* SCGC1 */
497 case 0x118: /* SCGC2 */
499 case 0x120: /* DCGC0 */
501 case 0x124: /* DCGC1 */
503 case 0x128: /* DCGC2 */
505 case 0x150: /* CLKVCLR */
507 case 0x160: /* LDOARST */
509 case 0x1e0: /* USER0 */
511 case 0x1e4: /* USER1 */
514 hw_error("ssys_read: Bad offset 0x%x\n", (int)offset);
519 static bool ssys_use_rcc2(ssys_state *s)
521 return (s->rcc2 >> 31) & 0x1;
525 * Caculate the sys. clock period in ms.
527 static void ssys_calculate_system_clock(ssys_state *s)
529 if (ssys_use_rcc2(s)) {
530 system_clock_scale = 5 * (((s->rcc2 >> 23) & 0x3f) + 1);
532 system_clock_scale = 5 * (((s->rcc >> 23) & 0xf) + 1);
536 static void ssys_write(void *opaque, hwaddr offset,
537 uint64_t value, unsigned size)
539 ssys_state *s = (ssys_state *)opaque;
542 case 0x030: /* PBORCTL */
543 s->pborctl = value & 0xffff;
545 case 0x034: /* LDOPCTL */
546 s->ldopctl = value & 0x1f;
548 case 0x040: /* SRCR0 */
549 case 0x044: /* SRCR1 */
550 case 0x048: /* SRCR2 */
551 fprintf(stderr, "Peripheral reset not implemented\n");
553 case 0x054: /* IMC */
554 s->int_mask = value & 0x7f;
556 case 0x058: /* MISC */
557 s->int_status &= ~value;
559 case 0x05c: /* RESC */
560 s->resc = value & 0x3f;
562 case 0x060: /* RCC */
563 if ((s->rcc & (1 << 13)) != 0 && (value & (1 << 13)) == 0) {
565 s->int_status |= (1 << 6);
568 ssys_calculate_system_clock(s);
570 case 0x070: /* RCC2 */
571 if (ssys_board_class(s) == DID0_CLASS_SANDSTORM) {
575 if ((s->rcc2 & (1 << 13)) != 0 && (value & (1 << 13)) == 0) {
577 s->int_status |= (1 << 6);
580 ssys_calculate_system_clock(s);
582 case 0x100: /* RCGC0 */
585 case 0x104: /* RCGC1 */
588 case 0x108: /* RCGC2 */
591 case 0x110: /* SCGC0 */
594 case 0x114: /* SCGC1 */
597 case 0x118: /* SCGC2 */
600 case 0x120: /* DCGC0 */
603 case 0x124: /* DCGC1 */
606 case 0x128: /* DCGC2 */
609 case 0x150: /* CLKVCLR */
612 case 0x160: /* LDOARST */
616 hw_error("ssys_write: Bad offset 0x%x\n", (int)offset);
621 static const MemoryRegionOps ssys_ops = {
624 .endianness = DEVICE_NATIVE_ENDIAN,
627 static void ssys_reset(void *opaque)
629 ssys_state *s = (ssys_state *)opaque;
634 if (ssys_board_class(s) == DID0_CLASS_SANDSTORM) {
637 s->rcc2 = 0x07802810;
642 ssys_calculate_system_clock(s);
645 static int stellaris_sys_post_load(void *opaque, int version_id)
647 ssys_state *s = opaque;
649 ssys_calculate_system_clock(s);
654 static const VMStateDescription vmstate_stellaris_sys = {
655 .name = "stellaris_sys",
657 .minimum_version_id = 1,
658 .post_load = stellaris_sys_post_load,
659 .fields = (VMStateField[]) {
660 VMSTATE_UINT32(pborctl, ssys_state),
661 VMSTATE_UINT32(ldopctl, ssys_state),
662 VMSTATE_UINT32(int_mask, ssys_state),
663 VMSTATE_UINT32(int_status, ssys_state),
664 VMSTATE_UINT32(resc, ssys_state),
665 VMSTATE_UINT32(rcc, ssys_state),
666 VMSTATE_UINT32_V(rcc2, ssys_state, 2),
667 VMSTATE_UINT32_ARRAY(rcgc, ssys_state, 3),
668 VMSTATE_UINT32_ARRAY(scgc, ssys_state, 3),
669 VMSTATE_UINT32_ARRAY(dcgc, ssys_state, 3),
670 VMSTATE_UINT32(clkvclr, ssys_state),
671 VMSTATE_UINT32(ldoarst, ssys_state),
672 VMSTATE_END_OF_LIST()
676 static int stellaris_sys_init(uint32_t base, qemu_irq irq,
677 stellaris_board_info * board,
682 s = g_new0(ssys_state, 1);
685 /* Most devices come preprogrammed with a MAC address in the user data. */
686 s->user0 = macaddr[0] | (macaddr[1] << 8) | (macaddr[2] << 16);
687 s->user1 = macaddr[3] | (macaddr[4] << 8) | (macaddr[5] << 16);
689 memory_region_init_io(&s->iomem, NULL, &ssys_ops, s, "ssys", 0x00001000);
690 memory_region_add_subregion(get_system_memory(), base, &s->iomem);
692 vmstate_register(NULL, -1, &vmstate_stellaris_sys, s);
697 /* I2C controller. */
699 #define TYPE_STELLARIS_I2C "stellaris-i2c"
700 #define STELLARIS_I2C(obj) \
701 OBJECT_CHECK(stellaris_i2c_state, (obj), TYPE_STELLARIS_I2C)
704 SysBusDevice parent_obj;
716 } stellaris_i2c_state;
718 #define STELLARIS_I2C_MCS_BUSY 0x01
719 #define STELLARIS_I2C_MCS_ERROR 0x02
720 #define STELLARIS_I2C_MCS_ADRACK 0x04
721 #define STELLARIS_I2C_MCS_DATACK 0x08
722 #define STELLARIS_I2C_MCS_ARBLST 0x10
723 #define STELLARIS_I2C_MCS_IDLE 0x20
724 #define STELLARIS_I2C_MCS_BUSBSY 0x40
726 static uint64_t stellaris_i2c_read(void *opaque, hwaddr offset,
729 stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;
735 /* We don't emulate timing, so the controller is never busy. */
736 return s->mcs | STELLARIS_I2C_MCS_IDLE;
739 case 0x0c: /* MTPR */
741 case 0x10: /* MIMR */
743 case 0x14: /* MRIS */
745 case 0x18: /* MMIS */
746 return s->mris & s->mimr;
750 hw_error("strllaris_i2c_read: Bad offset 0x%x\n", (int)offset);
755 static void stellaris_i2c_update(stellaris_i2c_state *s)
759 level = (s->mris & s->mimr) != 0;
760 qemu_set_irq(s->irq, level);
763 static void stellaris_i2c_write(void *opaque, hwaddr offset,
764 uint64_t value, unsigned size)
766 stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;
770 s->msa = value & 0xff;
773 if ((s->mcr & 0x10) == 0) {
774 /* Disabled. Do nothing. */
777 /* Grab the bus if this is starting a transfer. */
778 if ((value & 2) && (s->mcs & STELLARIS_I2C_MCS_BUSBSY) == 0) {
779 if (i2c_start_transfer(s->bus, s->msa >> 1, s->msa & 1)) {
780 s->mcs |= STELLARIS_I2C_MCS_ARBLST;
782 s->mcs &= ~STELLARIS_I2C_MCS_ARBLST;
783 s->mcs |= STELLARIS_I2C_MCS_BUSBSY;
786 /* If we don't have the bus then indicate an error. */
787 if (!i2c_bus_busy(s->bus)
788 || (s->mcs & STELLARIS_I2C_MCS_BUSBSY) == 0) {
789 s->mcs |= STELLARIS_I2C_MCS_ERROR;
792 s->mcs &= ~STELLARIS_I2C_MCS_ERROR;
794 /* Transfer a byte. */
795 /* TODO: Handle errors. */
798 s->mdr = i2c_recv(s->bus) & 0xff;
801 i2c_send(s->bus, s->mdr);
803 /* Raise an interrupt. */
807 /* Finish transfer. */
808 i2c_end_transfer(s->bus);
809 s->mcs &= ~STELLARIS_I2C_MCS_BUSBSY;
813 s->mdr = value & 0xff;
815 case 0x0c: /* MTPR */
816 s->mtpr = value & 0xff;
818 case 0x10: /* MIMR */
821 case 0x1c: /* MICR */
827 "stellaris_i2c_write: Loopback not implemented\n");
830 "stellaris_i2c_write: Slave mode not implemented\n");
831 s->mcr = value & 0x31;
834 hw_error("stellaris_i2c_write: Bad offset 0x%x\n",
837 stellaris_i2c_update(s);
840 static void stellaris_i2c_reset(stellaris_i2c_state *s)
842 if (s->mcs & STELLARIS_I2C_MCS_BUSBSY)
843 i2c_end_transfer(s->bus);
852 stellaris_i2c_update(s);
855 static const MemoryRegionOps stellaris_i2c_ops = {
856 .read = stellaris_i2c_read,
857 .write = stellaris_i2c_write,
858 .endianness = DEVICE_NATIVE_ENDIAN,
861 static const VMStateDescription vmstate_stellaris_i2c = {
862 .name = "stellaris_i2c",
864 .minimum_version_id = 1,
865 .fields = (VMStateField[]) {
866 VMSTATE_UINT32(msa, stellaris_i2c_state),
867 VMSTATE_UINT32(mcs, stellaris_i2c_state),
868 VMSTATE_UINT32(mdr, stellaris_i2c_state),
869 VMSTATE_UINT32(mtpr, stellaris_i2c_state),
870 VMSTATE_UINT32(mimr, stellaris_i2c_state),
871 VMSTATE_UINT32(mris, stellaris_i2c_state),
872 VMSTATE_UINT32(mcr, stellaris_i2c_state),
873 VMSTATE_END_OF_LIST()
877 static void stellaris_i2c_init(Object *obj)
879 DeviceState *dev = DEVICE(obj);
880 stellaris_i2c_state *s = STELLARIS_I2C(obj);
881 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
884 sysbus_init_irq(sbd, &s->irq);
885 bus = i2c_init_bus(dev, "i2c");
888 memory_region_init_io(&s->iomem, obj, &stellaris_i2c_ops, s,
890 sysbus_init_mmio(sbd, &s->iomem);
891 /* ??? For now we only implement the master interface. */
892 stellaris_i2c_reset(s);
895 /* Analogue to Digital Converter. This is only partially implemented,
896 enough for applications that use a combined ADC and timer tick. */
898 #define STELLARIS_ADC_EM_CONTROLLER 0
899 #define STELLARIS_ADC_EM_COMP 1
900 #define STELLARIS_ADC_EM_EXTERNAL 4
901 #define STELLARIS_ADC_EM_TIMER 5
902 #define STELLARIS_ADC_EM_PWM0 6
903 #define STELLARIS_ADC_EM_PWM1 7
904 #define STELLARIS_ADC_EM_PWM2 8
906 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
907 #define STELLARIS_ADC_FIFO_FULL 0x1000
909 #define TYPE_STELLARIS_ADC "stellaris-adc"
910 #define STELLARIS_ADC(obj) \
911 OBJECT_CHECK(stellaris_adc_state, (obj), TYPE_STELLARIS_ADC)
913 typedef struct StellarisADCState {
914 SysBusDevice parent_obj;
933 } stellaris_adc_state;
935 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state *s, int n)
939 tail = s->fifo[n].state & 0xf;
940 if (s->fifo[n].state & STELLARIS_ADC_FIFO_EMPTY) {
943 s->fifo[n].state = (s->fifo[n].state & ~0xf) | ((tail + 1) & 0xf);
944 s->fifo[n].state &= ~STELLARIS_ADC_FIFO_FULL;
945 if (tail + 1 == ((s->fifo[n].state >> 4) & 0xf))
946 s->fifo[n].state |= STELLARIS_ADC_FIFO_EMPTY;
948 return s->fifo[n].data[tail];
951 static void stellaris_adc_fifo_write(stellaris_adc_state *s, int n,
956 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
957 FIFO fir each sequencer. */
958 head = (s->fifo[n].state >> 4) & 0xf;
959 if (s->fifo[n].state & STELLARIS_ADC_FIFO_FULL) {
963 s->fifo[n].data[head] = value;
964 head = (head + 1) & 0xf;
965 s->fifo[n].state &= ~STELLARIS_ADC_FIFO_EMPTY;
966 s->fifo[n].state = (s->fifo[n].state & ~0xf0) | (head << 4);
967 if ((s->fifo[n].state & 0xf) == head)
968 s->fifo[n].state |= STELLARIS_ADC_FIFO_FULL;
971 static void stellaris_adc_update(stellaris_adc_state *s)
976 for (n = 0; n < 4; n++) {
977 level = (s->ris & s->im & (1 << n)) != 0;
978 qemu_set_irq(s->irq[n], level);
982 static void stellaris_adc_trigger(void *opaque, int irq, int level)
984 stellaris_adc_state *s = (stellaris_adc_state *)opaque;
987 for (n = 0; n < 4; n++) {
988 if ((s->actss & (1 << n)) == 0) {
992 if (((s->emux >> (n * 4)) & 0xff) != 5) {
996 /* Some applications use the ADC as a random number source, so introduce
997 some variation into the signal. */
998 s->noise = s->noise * 314159 + 1;
999 /* ??? actual inputs not implemented. Return an arbitrary value. */
1000 stellaris_adc_fifo_write(s, n, 0x200 + ((s->noise >> 16) & 7));
1002 stellaris_adc_update(s);
1006 static void stellaris_adc_reset(stellaris_adc_state *s)
1010 for (n = 0; n < 4; n++) {
1013 s->fifo[n].state = STELLARIS_ADC_FIFO_EMPTY;
1017 static uint64_t stellaris_adc_read(void *opaque, hwaddr offset,
1020 stellaris_adc_state *s = (stellaris_adc_state *)opaque;
1022 /* TODO: Implement this. */
1023 if (offset >= 0x40 && offset < 0xc0) {
1025 n = (offset - 0x40) >> 5;
1026 switch (offset & 0x1f) {
1027 case 0x00: /* SSMUX */
1029 case 0x04: /* SSCTL */
1031 case 0x08: /* SSFIFO */
1032 return stellaris_adc_fifo_read(s, n);
1033 case 0x0c: /* SSFSTAT */
1034 return s->fifo[n].state;
1040 case 0x00: /* ACTSS */
1042 case 0x04: /* RIS */
1046 case 0x0c: /* ISC */
1047 return s->ris & s->im;
1048 case 0x10: /* OSTAT */
1050 case 0x14: /* EMUX */
1052 case 0x18: /* USTAT */
1054 case 0x20: /* SSPRI */
1056 case 0x30: /* SAC */
1059 hw_error("strllaris_adc_read: Bad offset 0x%x\n",
1065 static void stellaris_adc_write(void *opaque, hwaddr offset,
1066 uint64_t value, unsigned size)
1068 stellaris_adc_state *s = (stellaris_adc_state *)opaque;
1070 /* TODO: Implement this. */
1071 if (offset >= 0x40 && offset < 0xc0) {
1073 n = (offset - 0x40) >> 5;
1074 switch (offset & 0x1f) {
1075 case 0x00: /* SSMUX */
1076 s->ssmux[n] = value & 0x33333333;
1078 case 0x04: /* SSCTL */
1080 hw_error("ADC: Unimplemented sequence %" PRIx64 "\n",
1083 s->ssctl[n] = value;
1090 case 0x00: /* ACTSS */
1091 s->actss = value & 0xf;
1096 case 0x0c: /* ISC */
1099 case 0x10: /* OSTAT */
1102 case 0x14: /* EMUX */
1105 case 0x18: /* USTAT */
1108 case 0x20: /* SSPRI */
1111 case 0x28: /* PSSI */
1112 hw_error("Not implemented: ADC sample initiate\n");
1114 case 0x30: /* SAC */
1118 hw_error("stellaris_adc_write: Bad offset 0x%x\n", (int)offset);
1120 stellaris_adc_update(s);
1123 static const MemoryRegionOps stellaris_adc_ops = {
1124 .read = stellaris_adc_read,
1125 .write = stellaris_adc_write,
1126 .endianness = DEVICE_NATIVE_ENDIAN,
1129 static const VMStateDescription vmstate_stellaris_adc = {
1130 .name = "stellaris_adc",
1132 .minimum_version_id = 1,
1133 .fields = (VMStateField[]) {
1134 VMSTATE_UINT32(actss, stellaris_adc_state),
1135 VMSTATE_UINT32(ris, stellaris_adc_state),
1136 VMSTATE_UINT32(im, stellaris_adc_state),
1137 VMSTATE_UINT32(emux, stellaris_adc_state),
1138 VMSTATE_UINT32(ostat, stellaris_adc_state),
1139 VMSTATE_UINT32(ustat, stellaris_adc_state),
1140 VMSTATE_UINT32(sspri, stellaris_adc_state),
1141 VMSTATE_UINT32(sac, stellaris_adc_state),
1142 VMSTATE_UINT32(fifo[0].state, stellaris_adc_state),
1143 VMSTATE_UINT32_ARRAY(fifo[0].data, stellaris_adc_state, 16),
1144 VMSTATE_UINT32(ssmux[0], stellaris_adc_state),
1145 VMSTATE_UINT32(ssctl[0], stellaris_adc_state),
1146 VMSTATE_UINT32(fifo[1].state, stellaris_adc_state),
1147 VMSTATE_UINT32_ARRAY(fifo[1].data, stellaris_adc_state, 16),
1148 VMSTATE_UINT32(ssmux[1], stellaris_adc_state),
1149 VMSTATE_UINT32(ssctl[1], stellaris_adc_state),
1150 VMSTATE_UINT32(fifo[2].state, stellaris_adc_state),
1151 VMSTATE_UINT32_ARRAY(fifo[2].data, stellaris_adc_state, 16),
1152 VMSTATE_UINT32(ssmux[2], stellaris_adc_state),
1153 VMSTATE_UINT32(ssctl[2], stellaris_adc_state),
1154 VMSTATE_UINT32(fifo[3].state, stellaris_adc_state),
1155 VMSTATE_UINT32_ARRAY(fifo[3].data, stellaris_adc_state, 16),
1156 VMSTATE_UINT32(ssmux[3], stellaris_adc_state),
1157 VMSTATE_UINT32(ssctl[3], stellaris_adc_state),
1158 VMSTATE_UINT32(noise, stellaris_adc_state),
1159 VMSTATE_END_OF_LIST()
1163 static void stellaris_adc_init(Object *obj)
1165 DeviceState *dev = DEVICE(obj);
1166 stellaris_adc_state *s = STELLARIS_ADC(obj);
1167 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
1170 for (n = 0; n < 4; n++) {
1171 sysbus_init_irq(sbd, &s->irq[n]);
1174 memory_region_init_io(&s->iomem, obj, &stellaris_adc_ops, s,
1176 sysbus_init_mmio(sbd, &s->iomem);
1177 stellaris_adc_reset(s);
1178 qdev_init_gpio_in(dev, stellaris_adc_trigger, 1);
1182 void do_sys_reset(void *opaque, int n, int level)
1185 qemu_system_reset_request();
1190 static stellaris_board_info stellaris_boards[] = {
1194 0x001f001f, /* dc0 */
1204 0x00ff007f, /* dc0 */
1209 BP_OLED_SSI | BP_GAMEPAD
1213 static void stellaris_init(const char *kernel_filename, const char *cpu_model,
1214 stellaris_board_info *board)
1216 static const int uart_irq[] = {5, 6, 33, 34};
1217 static const int timer_irq[] = {19, 21, 23, 35};
1218 static const uint32_t gpio_addr[7] =
1219 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1220 0x40024000, 0x40025000, 0x40026000};
1221 static const int gpio_irq[7] = {0, 1, 2, 3, 4, 30, 31};
1223 DeviceState *gpio_dev[7], *nvic;
1224 qemu_irq gpio_in[7][8];
1225 qemu_irq gpio_out[7][8];
1234 MemoryRegion *sram = g_new(MemoryRegion, 1);
1235 MemoryRegion *flash = g_new(MemoryRegion, 1);
1236 MemoryRegion *system_memory = get_system_memory();
1238 flash_size = (((board->dc0 & 0xffff) + 1) << 1) * 1024;
1239 sram_size = ((board->dc0 >> 18) + 1) * 1024;
1241 /* Flash programming is done via the SCU, so pretend it is ROM. */
1242 memory_region_init_ram(flash, NULL, "stellaris.flash", flash_size,
1244 vmstate_register_ram_global(flash);
1245 memory_region_set_readonly(flash, true);
1246 memory_region_add_subregion(system_memory, 0, flash);
1248 memory_region_init_ram(sram, NULL, "stellaris.sram", sram_size,
1250 vmstate_register_ram_global(sram);
1251 memory_region_add_subregion(system_memory, 0x20000000, sram);
1253 nvic = armv7m_init(system_memory, flash_size, NUM_IRQ_LINES,
1254 kernel_filename, cpu_model);
1256 qdev_connect_gpio_out_named(nvic, "SYSRESETREQ", 0,
1257 qemu_allocate_irq(&do_sys_reset, NULL, 0));
1259 if (board->dc1 & (1 << 16)) {
1260 dev = sysbus_create_varargs(TYPE_STELLARIS_ADC, 0x40038000,
1261 qdev_get_gpio_in(nvic, 14),
1262 qdev_get_gpio_in(nvic, 15),
1263 qdev_get_gpio_in(nvic, 16),
1264 qdev_get_gpio_in(nvic, 17),
1266 adc = qdev_get_gpio_in(dev, 0);
1270 for (i = 0; i < 4; i++) {
1271 if (board->dc2 & (0x10000 << i)) {
1272 dev = sysbus_create_simple(TYPE_STELLARIS_GPTM,
1273 0x40030000 + i * 0x1000,
1274 qdev_get_gpio_in(nvic, timer_irq[i]));
1275 /* TODO: This is incorrect, but we get away with it because
1276 the ADC output is only ever pulsed. */
1277 qdev_connect_gpio_out(dev, 0, adc);
1281 stellaris_sys_init(0x400fe000, qdev_get_gpio_in(nvic, 28),
1282 board, nd_table[0].macaddr.a);
1284 for (i = 0; i < 7; i++) {
1285 if (board->dc4 & (1 << i)) {
1286 gpio_dev[i] = sysbus_create_simple("pl061_luminary", gpio_addr[i],
1287 qdev_get_gpio_in(nvic,
1289 for (j = 0; j < 8; j++) {
1290 gpio_in[i][j] = qdev_get_gpio_in(gpio_dev[i], j);
1291 gpio_out[i][j] = NULL;
1296 if (board->dc2 & (1 << 12)) {
1297 dev = sysbus_create_simple(TYPE_STELLARIS_I2C, 0x40020000,
1298 qdev_get_gpio_in(nvic, 8));
1299 i2c = (I2CBus *)qdev_get_child_bus(dev, "i2c");
1300 if (board->peripherals & BP_OLED_I2C) {
1301 i2c_create_slave(i2c, "ssd0303", 0x3d);
1305 for (i = 0; i < 4; i++) {
1306 if (board->dc2 & (1 << i)) {
1307 pl011_luminary_create(0x4000c000 + i * 0x1000,
1308 qdev_get_gpio_in(nvic, uart_irq[i]),
1312 if (board->dc2 & (1 << 4)) {
1313 dev = sysbus_create_simple("pl022", 0x40008000,
1314 qdev_get_gpio_in(nvic, 7));
1315 if (board->peripherals & BP_OLED_SSI) {
1318 DeviceState *ssddev;
1320 /* Some boards have both an OLED controller and SD card connected to
1321 * the same SSI port, with the SD card chip select connected to a
1322 * GPIO pin. Technically the OLED chip select is connected to the
1323 * SSI Fss pin. We do not bother emulating that as both devices
1324 * should never be selected simultaneously, and our OLED controller
1325 * ignores stray 0xff commands that occur when deselecting the SD
1328 bus = qdev_get_child_bus(dev, "ssi");
1330 sddev = ssi_create_slave(bus, "ssi-sd");
1331 ssddev = ssi_create_slave(bus, "ssd0323");
1332 gpio_out[GPIO_D][0] = qemu_irq_split(
1333 qdev_get_gpio_in_named(sddev, SSI_GPIO_CS, 0),
1334 qdev_get_gpio_in_named(ssddev, SSI_GPIO_CS, 0));
1335 gpio_out[GPIO_C][7] = qdev_get_gpio_in(ssddev, 0);
1337 /* Make sure the select pin is high. */
1338 qemu_irq_raise(gpio_out[GPIO_D][0]);
1341 if (board->dc4 & (1 << 28)) {
1344 qemu_check_nic_model(&nd_table[0], "stellaris");
1346 enet = qdev_create(NULL, "stellaris_enet");
1347 qdev_set_nic_properties(enet, &nd_table[0]);
1348 qdev_init_nofail(enet);
1349 sysbus_mmio_map(SYS_BUS_DEVICE(enet), 0, 0x40048000);
1350 sysbus_connect_irq(SYS_BUS_DEVICE(enet), 0, qdev_get_gpio_in(nvic, 42));
1352 if (board->peripherals & BP_GAMEPAD) {
1353 qemu_irq gpad_irq[5];
1354 static const int gpad_keycode[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1356 gpad_irq[0] = qemu_irq_invert(gpio_in[GPIO_E][0]); /* up */
1357 gpad_irq[1] = qemu_irq_invert(gpio_in[GPIO_E][1]); /* down */
1358 gpad_irq[2] = qemu_irq_invert(gpio_in[GPIO_E][2]); /* left */
1359 gpad_irq[3] = qemu_irq_invert(gpio_in[GPIO_E][3]); /* right */
1360 gpad_irq[4] = qemu_irq_invert(gpio_in[GPIO_F][1]); /* select */
1362 stellaris_gamepad_init(5, gpad_irq, gpad_keycode);
1364 for (i = 0; i < 7; i++) {
1365 if (board->dc4 & (1 << i)) {
1366 for (j = 0; j < 8; j++) {
1367 if (gpio_out[i][j]) {
1368 qdev_connect_gpio_out(gpio_dev[i], j, gpio_out[i][j]);
1375 /* FIXME: Figure out how to generate these from stellaris_boards. */
1376 static void lm3s811evb_init(MachineState *machine)
1378 const char *cpu_model = machine->cpu_model;
1379 const char *kernel_filename = machine->kernel_filename;
1380 stellaris_init(kernel_filename, cpu_model, &stellaris_boards[0]);
1383 static void lm3s6965evb_init(MachineState *machine)
1385 const char *cpu_model = machine->cpu_model;
1386 const char *kernel_filename = machine->kernel_filename;
1387 stellaris_init(kernel_filename, cpu_model, &stellaris_boards[1]);
1390 static void lm3s811evb_class_init(ObjectClass *oc, void *data)
1392 MachineClass *mc = MACHINE_CLASS(oc);
1394 mc->desc = "Stellaris LM3S811EVB";
1395 mc->init = lm3s811evb_init;
1398 static const TypeInfo lm3s811evb_type = {
1399 .name = MACHINE_TYPE_NAME("lm3s811evb"),
1400 .parent = TYPE_MACHINE,
1401 .class_init = lm3s811evb_class_init,
1404 static void lm3s6965evb_class_init(ObjectClass *oc, void *data)
1406 MachineClass *mc = MACHINE_CLASS(oc);
1408 mc->desc = "Stellaris LM3S6965EVB";
1409 mc->init = lm3s6965evb_init;
1412 static const TypeInfo lm3s6965evb_type = {
1413 .name = MACHINE_TYPE_NAME("lm3s6965evb"),
1414 .parent = TYPE_MACHINE,
1415 .class_init = lm3s6965evb_class_init,
1418 static void stellaris_machine_init(void)
1420 type_register_static(&lm3s811evb_type);
1421 type_register_static(&lm3s6965evb_type);
1424 type_init(stellaris_machine_init)
1426 static void stellaris_i2c_class_init(ObjectClass *klass, void *data)
1428 DeviceClass *dc = DEVICE_CLASS(klass);
1430 dc->vmsd = &vmstate_stellaris_i2c;
1433 static const TypeInfo stellaris_i2c_info = {
1434 .name = TYPE_STELLARIS_I2C,
1435 .parent = TYPE_SYS_BUS_DEVICE,
1436 .instance_size = sizeof(stellaris_i2c_state),
1437 .instance_init = stellaris_i2c_init,
1438 .class_init = stellaris_i2c_class_init,
1441 static void stellaris_gptm_class_init(ObjectClass *klass, void *data)
1443 DeviceClass *dc = DEVICE_CLASS(klass);
1445 dc->vmsd = &vmstate_stellaris_gptm;
1448 static const TypeInfo stellaris_gptm_info = {
1449 .name = TYPE_STELLARIS_GPTM,
1450 .parent = TYPE_SYS_BUS_DEVICE,
1451 .instance_size = sizeof(gptm_state),
1452 .instance_init = stellaris_gptm_init,
1453 .class_init = stellaris_gptm_class_init,
1456 static void stellaris_adc_class_init(ObjectClass *klass, void *data)
1458 DeviceClass *dc = DEVICE_CLASS(klass);
1460 dc->vmsd = &vmstate_stellaris_adc;
1463 static const TypeInfo stellaris_adc_info = {
1464 .name = TYPE_STELLARIS_ADC,
1465 .parent = TYPE_SYS_BUS_DEVICE,
1466 .instance_size = sizeof(stellaris_adc_state),
1467 .instance_init = stellaris_adc_init,
1468 .class_init = stellaris_adc_class_init,
1471 static void stellaris_register_types(void)
1473 type_register_static(&stellaris_i2c_info);
1474 type_register_static(&stellaris_gptm_info);
1475 type_register_static(&stellaris_adc_info);
1478 type_init(stellaris_register_types)