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 "hw/sysbus.h"
12 #include "hw/arm/arm.h"
13 #include "hw/devices.h"
14 #include "qemu/timer.h"
15 #include "hw/i2c/i2c.h"
17 #include "hw/boards.h"
18 #include "exec/address-spaces.h"
28 #define BP_OLED_I2C 0x01
29 #define BP_OLED_SSI 0x02
30 #define BP_GAMEPAD 0x04
32 #define NUM_IRQ_LINES 64
34 typedef const struct {
44 } stellaris_board_info;
46 /* General purpose timer module. */
48 #define TYPE_STELLARIS_GPTM "stellaris-gptm"
49 #define STELLARIS_GPTM(obj) \
50 OBJECT_CHECK(gptm_state, (obj), TYPE_STELLARIS_GPTM)
52 typedef struct gptm_state {
53 SysBusDevice parent_obj;
64 uint32_t match_prescale[2];
67 struct gptm_state *opaque[2];
69 /* The timers have an alternate output used to trigger the ADC. */
74 static void gptm_update_irq(gptm_state *s)
77 level = (s->state & s->mask) != 0;
78 qemu_set_irq(s->irq, level);
81 static void gptm_stop(gptm_state *s, int n)
83 timer_del(s->timer[n]);
86 static void gptm_reload(gptm_state *s, int n, int reset)
90 tick = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
95 /* 32-bit CountDown. */
97 count = s->load[0] | (s->load[1] << 16);
98 tick += (int64_t)count * system_clock_scale;
99 } else if (s->config == 1) {
100 /* 32-bit RTC. 1Hz tick. */
101 tick += get_ticks_per_sec();
102 } else if (s->mode[n] == 0xa) {
103 /* PWM mode. Not implemented. */
105 hw_error("TODO: 16-bit timer mode 0x%x\n", s->mode[n]);
108 timer_mod(s->timer[n], tick);
111 static void gptm_tick(void *opaque)
113 gptm_state **p = (gptm_state **)opaque;
119 if (s->config == 0) {
121 if ((s->control & 0x20)) {
122 /* Output trigger. */
123 qemu_irq_pulse(s->trigger);
125 if (s->mode[0] & 1) {
130 gptm_reload(s, 0, 0);
132 } else if (s->config == 1) {
136 match = s->match[0] | (s->match[1] << 16);
142 gptm_reload(s, 0, 0);
143 } else if (s->mode[n] == 0xa) {
144 /* PWM mode. Not implemented. */
146 hw_error("TODO: 16-bit timer mode 0x%x\n", s->mode[n]);
151 static uint64_t gptm_read(void *opaque, hwaddr offset,
154 gptm_state *s = (gptm_state *)opaque;
159 case 0x04: /* TAMR */
161 case 0x08: /* TBMR */
170 return s->state & s->mask;
173 case 0x28: /* TAILR */
174 return s->load[0] | ((s->config < 4) ? (s->load[1] << 16) : 0);
175 case 0x2c: /* TBILR */
177 case 0x30: /* TAMARCHR */
178 return s->match[0] | ((s->config < 4) ? (s->match[1] << 16) : 0);
179 case 0x34: /* TBMATCHR */
181 case 0x38: /* TAPR */
182 return s->prescale[0];
183 case 0x3c: /* TBPR */
184 return s->prescale[1];
185 case 0x40: /* TAPMR */
186 return s->match_prescale[0];
187 case 0x44: /* TBPMR */
188 return s->match_prescale[1];
190 if (s->config == 1) {
193 qemu_log_mask(LOG_UNIMP,
194 "GPTM: read of TAR but timer read not supported");
197 qemu_log_mask(LOG_UNIMP,
198 "GPTM: read of TBR but timer read not supported");
201 qemu_log_mask(LOG_GUEST_ERROR,
202 "GPTM: read at bad offset 0x%x\n", (int)offset);
207 static void gptm_write(void *opaque, hwaddr offset,
208 uint64_t value, unsigned size)
210 gptm_state *s = (gptm_state *)opaque;
213 /* The timers should be disabled before changing the configuration.
214 We take advantage of this and defer everything until the timer
220 case 0x04: /* TAMR */
223 case 0x08: /* TBMR */
229 /* TODO: Implement pause. */
230 if ((oldval ^ value) & 1) {
232 gptm_reload(s, 0, 1);
237 if (((oldval ^ value) & 0x100) && s->config >= 4) {
239 gptm_reload(s, 1, 1);
246 s->mask = value & 0x77;
252 case 0x28: /* TAILR */
253 s->load[0] = value & 0xffff;
255 s->load[1] = value >> 16;
258 case 0x2c: /* TBILR */
259 s->load[1] = value & 0xffff;
261 case 0x30: /* TAMARCHR */
262 s->match[0] = value & 0xffff;
264 s->match[1] = value >> 16;
267 case 0x34: /* TBMATCHR */
268 s->match[1] = value >> 16;
270 case 0x38: /* TAPR */
271 s->prescale[0] = value;
273 case 0x3c: /* TBPR */
274 s->prescale[1] = value;
276 case 0x40: /* TAPMR */
277 s->match_prescale[0] = value;
279 case 0x44: /* TBPMR */
280 s->match_prescale[0] = value;
283 hw_error("gptm_write: Bad offset 0x%x\n", (int)offset);
288 static const MemoryRegionOps gptm_ops = {
291 .endianness = DEVICE_NATIVE_ENDIAN,
294 static const VMStateDescription vmstate_stellaris_gptm = {
295 .name = "stellaris_gptm",
297 .minimum_version_id = 1,
298 .fields = (VMStateField[]) {
299 VMSTATE_UINT32(config, gptm_state),
300 VMSTATE_UINT32_ARRAY(mode, gptm_state, 2),
301 VMSTATE_UINT32(control, gptm_state),
302 VMSTATE_UINT32(state, gptm_state),
303 VMSTATE_UINT32(mask, gptm_state),
305 VMSTATE_UINT32_ARRAY(load, gptm_state, 2),
306 VMSTATE_UINT32_ARRAY(match, gptm_state, 2),
307 VMSTATE_UINT32_ARRAY(prescale, gptm_state, 2),
308 VMSTATE_UINT32_ARRAY(match_prescale, gptm_state, 2),
309 VMSTATE_UINT32(rtc, gptm_state),
310 VMSTATE_INT64_ARRAY(tick, gptm_state, 2),
311 VMSTATE_TIMER_PTR_ARRAY(timer, gptm_state, 2),
312 VMSTATE_END_OF_LIST()
316 static int stellaris_gptm_init(SysBusDevice *sbd)
318 DeviceState *dev = DEVICE(sbd);
319 gptm_state *s = STELLARIS_GPTM(dev);
321 sysbus_init_irq(sbd, &s->irq);
322 qdev_init_gpio_out(dev, &s->trigger, 1);
324 memory_region_init_io(&s->iomem, OBJECT(s), &gptm_ops, s,
326 sysbus_init_mmio(sbd, &s->iomem);
328 s->opaque[0] = s->opaque[1] = s;
329 s->timer[0] = timer_new_ns(QEMU_CLOCK_VIRTUAL, gptm_tick, &s->opaque[0]);
330 s->timer[1] = timer_new_ns(QEMU_CLOCK_VIRTUAL, gptm_tick, &s->opaque[1]);
331 vmstate_register(dev, -1, &vmstate_stellaris_gptm, s);
336 /* System controller. */
355 stellaris_board_info *board;
358 static void ssys_update(ssys_state *s)
360 qemu_set_irq(s->irq, (s->int_status & s->int_mask) != 0);
363 static uint32_t pllcfg_sandstorm[16] = {
365 0x1ae0, /* 1.8432 Mhz */
367 0xd573, /* 2.4576 Mhz */
368 0x37a6, /* 3.57954 Mhz */
369 0x1ae2, /* 3.6864 Mhz */
371 0x98bc, /* 4.906 Mhz */
372 0x935b, /* 4.9152 Mhz */
374 0x4dee, /* 5.12 Mhz */
376 0x75db, /* 6.144 Mhz */
377 0x1ae6, /* 7.3728 Mhz */
379 0x585b /* 8.192 Mhz */
382 static uint32_t pllcfg_fury[16] = {
384 0x1b20, /* 1.8432 Mhz */
386 0xf42b, /* 2.4576 Mhz */
387 0x37e3, /* 3.57954 Mhz */
388 0x1b21, /* 3.6864 Mhz */
390 0x98ee, /* 4.906 Mhz */
391 0xd5b4, /* 4.9152 Mhz */
393 0x4e27, /* 5.12 Mhz */
395 0xec1c, /* 6.144 Mhz */
396 0x1b23, /* 7.3728 Mhz */
398 0xb11c /* 8.192 Mhz */
401 #define DID0_VER_MASK 0x70000000
402 #define DID0_VER_0 0x00000000
403 #define DID0_VER_1 0x10000000
405 #define DID0_CLASS_MASK 0x00FF0000
406 #define DID0_CLASS_SANDSTORM 0x00000000
407 #define DID0_CLASS_FURY 0x00010000
409 static int ssys_board_class(const ssys_state *s)
411 uint32_t did0 = s->board->did0;
412 switch (did0 & DID0_VER_MASK) {
414 return DID0_CLASS_SANDSTORM;
416 switch (did0 & DID0_CLASS_MASK) {
417 case DID0_CLASS_SANDSTORM:
418 case DID0_CLASS_FURY:
419 return did0 & DID0_CLASS_MASK;
421 /* for unknown classes, fall through */
423 hw_error("ssys_board_class: Unknown class 0x%08x\n", did0);
427 static uint64_t ssys_read(void *opaque, hwaddr offset,
430 ssys_state *s = (ssys_state *)opaque;
433 case 0x000: /* DID0 */
434 return s->board->did0;
435 case 0x004: /* DID1 */
436 return s->board->did1;
437 case 0x008: /* DC0 */
438 return s->board->dc0;
439 case 0x010: /* DC1 */
440 return s->board->dc1;
441 case 0x014: /* DC2 */
442 return s->board->dc2;
443 case 0x018: /* DC3 */
444 return s->board->dc3;
445 case 0x01c: /* DC4 */
446 return s->board->dc4;
447 case 0x030: /* PBORCTL */
449 case 0x034: /* LDOPCTL */
451 case 0x040: /* SRCR0 */
453 case 0x044: /* SRCR1 */
455 case 0x048: /* SRCR2 */
457 case 0x050: /* RIS */
458 return s->int_status;
459 case 0x054: /* IMC */
461 case 0x058: /* MISC */
462 return s->int_status & s->int_mask;
463 case 0x05c: /* RESC */
465 case 0x060: /* RCC */
467 case 0x064: /* PLLCFG */
470 xtal = (s->rcc >> 6) & 0xf;
471 switch (ssys_board_class(s)) {
472 case DID0_CLASS_FURY:
473 return pllcfg_fury[xtal];
474 case DID0_CLASS_SANDSTORM:
475 return pllcfg_sandstorm[xtal];
477 hw_error("ssys_read: Unhandled class for PLLCFG read.\n");
481 case 0x070: /* RCC2 */
483 case 0x100: /* RCGC0 */
485 case 0x104: /* RCGC1 */
487 case 0x108: /* RCGC2 */
489 case 0x110: /* SCGC0 */
491 case 0x114: /* SCGC1 */
493 case 0x118: /* SCGC2 */
495 case 0x120: /* DCGC0 */
497 case 0x124: /* DCGC1 */
499 case 0x128: /* DCGC2 */
501 case 0x150: /* CLKVCLR */
503 case 0x160: /* LDOARST */
505 case 0x1e0: /* USER0 */
507 case 0x1e4: /* USER1 */
510 hw_error("ssys_read: Bad offset 0x%x\n", (int)offset);
515 static bool ssys_use_rcc2(ssys_state *s)
517 return (s->rcc2 >> 31) & 0x1;
521 * Caculate the sys. clock period in ms.
523 static void ssys_calculate_system_clock(ssys_state *s)
525 if (ssys_use_rcc2(s)) {
526 system_clock_scale = 5 * (((s->rcc2 >> 23) & 0x3f) + 1);
528 system_clock_scale = 5 * (((s->rcc >> 23) & 0xf) + 1);
532 static void ssys_write(void *opaque, hwaddr offset,
533 uint64_t value, unsigned size)
535 ssys_state *s = (ssys_state *)opaque;
538 case 0x030: /* PBORCTL */
539 s->pborctl = value & 0xffff;
541 case 0x034: /* LDOPCTL */
542 s->ldopctl = value & 0x1f;
544 case 0x040: /* SRCR0 */
545 case 0x044: /* SRCR1 */
546 case 0x048: /* SRCR2 */
547 fprintf(stderr, "Peripheral reset not implemented\n");
549 case 0x054: /* IMC */
550 s->int_mask = value & 0x7f;
552 case 0x058: /* MISC */
553 s->int_status &= ~value;
555 case 0x05c: /* RESC */
556 s->resc = value & 0x3f;
558 case 0x060: /* RCC */
559 if ((s->rcc & (1 << 13)) != 0 && (value & (1 << 13)) == 0) {
561 s->int_status |= (1 << 6);
564 ssys_calculate_system_clock(s);
566 case 0x070: /* RCC2 */
567 if (ssys_board_class(s) == DID0_CLASS_SANDSTORM) {
571 if ((s->rcc2 & (1 << 13)) != 0 && (value & (1 << 13)) == 0) {
573 s->int_status |= (1 << 6);
576 ssys_calculate_system_clock(s);
578 case 0x100: /* RCGC0 */
581 case 0x104: /* RCGC1 */
584 case 0x108: /* RCGC2 */
587 case 0x110: /* SCGC0 */
590 case 0x114: /* SCGC1 */
593 case 0x118: /* SCGC2 */
596 case 0x120: /* DCGC0 */
599 case 0x124: /* DCGC1 */
602 case 0x128: /* DCGC2 */
605 case 0x150: /* CLKVCLR */
608 case 0x160: /* LDOARST */
612 hw_error("ssys_write: Bad offset 0x%x\n", (int)offset);
617 static const MemoryRegionOps ssys_ops = {
620 .endianness = DEVICE_NATIVE_ENDIAN,
623 static void ssys_reset(void *opaque)
625 ssys_state *s = (ssys_state *)opaque;
630 if (ssys_board_class(s) == DID0_CLASS_SANDSTORM) {
633 s->rcc2 = 0x07802810;
638 ssys_calculate_system_clock(s);
641 static int stellaris_sys_post_load(void *opaque, int version_id)
643 ssys_state *s = opaque;
645 ssys_calculate_system_clock(s);
650 static const VMStateDescription vmstate_stellaris_sys = {
651 .name = "stellaris_sys",
653 .minimum_version_id = 1,
654 .post_load = stellaris_sys_post_load,
655 .fields = (VMStateField[]) {
656 VMSTATE_UINT32(pborctl, ssys_state),
657 VMSTATE_UINT32(ldopctl, ssys_state),
658 VMSTATE_UINT32(int_mask, ssys_state),
659 VMSTATE_UINT32(int_status, ssys_state),
660 VMSTATE_UINT32(resc, ssys_state),
661 VMSTATE_UINT32(rcc, ssys_state),
662 VMSTATE_UINT32_V(rcc2, ssys_state, 2),
663 VMSTATE_UINT32_ARRAY(rcgc, ssys_state, 3),
664 VMSTATE_UINT32_ARRAY(scgc, ssys_state, 3),
665 VMSTATE_UINT32_ARRAY(dcgc, ssys_state, 3),
666 VMSTATE_UINT32(clkvclr, ssys_state),
667 VMSTATE_UINT32(ldoarst, ssys_state),
668 VMSTATE_END_OF_LIST()
672 static int stellaris_sys_init(uint32_t base, qemu_irq irq,
673 stellaris_board_info * board,
678 s = g_new0(ssys_state, 1);
681 /* Most devices come preprogrammed with a MAC address in the user data. */
682 s->user0 = macaddr[0] | (macaddr[1] << 8) | (macaddr[2] << 16);
683 s->user1 = macaddr[3] | (macaddr[4] << 8) | (macaddr[5] << 16);
685 memory_region_init_io(&s->iomem, NULL, &ssys_ops, s, "ssys", 0x00001000);
686 memory_region_add_subregion(get_system_memory(), base, &s->iomem);
688 vmstate_register(NULL, -1, &vmstate_stellaris_sys, s);
693 /* I2C controller. */
695 #define TYPE_STELLARIS_I2C "stellaris-i2c"
696 #define STELLARIS_I2C(obj) \
697 OBJECT_CHECK(stellaris_i2c_state, (obj), TYPE_STELLARIS_I2C)
700 SysBusDevice parent_obj;
712 } stellaris_i2c_state;
714 #define STELLARIS_I2C_MCS_BUSY 0x01
715 #define STELLARIS_I2C_MCS_ERROR 0x02
716 #define STELLARIS_I2C_MCS_ADRACK 0x04
717 #define STELLARIS_I2C_MCS_DATACK 0x08
718 #define STELLARIS_I2C_MCS_ARBLST 0x10
719 #define STELLARIS_I2C_MCS_IDLE 0x20
720 #define STELLARIS_I2C_MCS_BUSBSY 0x40
722 static uint64_t stellaris_i2c_read(void *opaque, hwaddr offset,
725 stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;
731 /* We don't emulate timing, so the controller is never busy. */
732 return s->mcs | STELLARIS_I2C_MCS_IDLE;
735 case 0x0c: /* MTPR */
737 case 0x10: /* MIMR */
739 case 0x14: /* MRIS */
741 case 0x18: /* MMIS */
742 return s->mris & s->mimr;
746 hw_error("strllaris_i2c_read: Bad offset 0x%x\n", (int)offset);
751 static void stellaris_i2c_update(stellaris_i2c_state *s)
755 level = (s->mris & s->mimr) != 0;
756 qemu_set_irq(s->irq, level);
759 static void stellaris_i2c_write(void *opaque, hwaddr offset,
760 uint64_t value, unsigned size)
762 stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;
766 s->msa = value & 0xff;
769 if ((s->mcr & 0x10) == 0) {
770 /* Disabled. Do nothing. */
773 /* Grab the bus if this is starting a transfer. */
774 if ((value & 2) && (s->mcs & STELLARIS_I2C_MCS_BUSBSY) == 0) {
775 if (i2c_start_transfer(s->bus, s->msa >> 1, s->msa & 1)) {
776 s->mcs |= STELLARIS_I2C_MCS_ARBLST;
778 s->mcs &= ~STELLARIS_I2C_MCS_ARBLST;
779 s->mcs |= STELLARIS_I2C_MCS_BUSBSY;
782 /* If we don't have the bus then indicate an error. */
783 if (!i2c_bus_busy(s->bus)
784 || (s->mcs & STELLARIS_I2C_MCS_BUSBSY) == 0) {
785 s->mcs |= STELLARIS_I2C_MCS_ERROR;
788 s->mcs &= ~STELLARIS_I2C_MCS_ERROR;
790 /* Transfer a byte. */
791 /* TODO: Handle errors. */
794 s->mdr = i2c_recv(s->bus) & 0xff;
797 i2c_send(s->bus, s->mdr);
799 /* Raise an interrupt. */
803 /* Finish transfer. */
804 i2c_end_transfer(s->bus);
805 s->mcs &= ~STELLARIS_I2C_MCS_BUSBSY;
809 s->mdr = value & 0xff;
811 case 0x0c: /* MTPR */
812 s->mtpr = value & 0xff;
814 case 0x10: /* MIMR */
817 case 0x1c: /* MICR */
823 "stellaris_i2c_write: Loopback not implemented\n");
826 "stellaris_i2c_write: Slave mode not implemented\n");
827 s->mcr = value & 0x31;
830 hw_error("stellaris_i2c_write: Bad offset 0x%x\n",
833 stellaris_i2c_update(s);
836 static void stellaris_i2c_reset(stellaris_i2c_state *s)
838 if (s->mcs & STELLARIS_I2C_MCS_BUSBSY)
839 i2c_end_transfer(s->bus);
848 stellaris_i2c_update(s);
851 static const MemoryRegionOps stellaris_i2c_ops = {
852 .read = stellaris_i2c_read,
853 .write = stellaris_i2c_write,
854 .endianness = DEVICE_NATIVE_ENDIAN,
857 static const VMStateDescription vmstate_stellaris_i2c = {
858 .name = "stellaris_i2c",
860 .minimum_version_id = 1,
861 .fields = (VMStateField[]) {
862 VMSTATE_UINT32(msa, stellaris_i2c_state),
863 VMSTATE_UINT32(mcs, stellaris_i2c_state),
864 VMSTATE_UINT32(mdr, stellaris_i2c_state),
865 VMSTATE_UINT32(mtpr, stellaris_i2c_state),
866 VMSTATE_UINT32(mimr, stellaris_i2c_state),
867 VMSTATE_UINT32(mris, stellaris_i2c_state),
868 VMSTATE_UINT32(mcr, stellaris_i2c_state),
869 VMSTATE_END_OF_LIST()
873 static int stellaris_i2c_init(SysBusDevice *sbd)
875 DeviceState *dev = DEVICE(sbd);
876 stellaris_i2c_state *s = STELLARIS_I2C(dev);
879 sysbus_init_irq(sbd, &s->irq);
880 bus = i2c_init_bus(dev, "i2c");
883 memory_region_init_io(&s->iomem, OBJECT(s), &stellaris_i2c_ops, s,
885 sysbus_init_mmio(sbd, &s->iomem);
886 /* ??? For now we only implement the master interface. */
887 stellaris_i2c_reset(s);
888 vmstate_register(dev, -1, &vmstate_stellaris_i2c, s);
892 /* Analogue to Digital Converter. This is only partially implemented,
893 enough for applications that use a combined ADC and timer tick. */
895 #define STELLARIS_ADC_EM_CONTROLLER 0
896 #define STELLARIS_ADC_EM_COMP 1
897 #define STELLARIS_ADC_EM_EXTERNAL 4
898 #define STELLARIS_ADC_EM_TIMER 5
899 #define STELLARIS_ADC_EM_PWM0 6
900 #define STELLARIS_ADC_EM_PWM1 7
901 #define STELLARIS_ADC_EM_PWM2 8
903 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
904 #define STELLARIS_ADC_FIFO_FULL 0x1000
906 #define TYPE_STELLARIS_ADC "stellaris-adc"
907 #define STELLARIS_ADC(obj) \
908 OBJECT_CHECK(stellaris_adc_state, (obj), TYPE_STELLARIS_ADC)
910 typedef struct StellarisADCState {
911 SysBusDevice parent_obj;
930 } stellaris_adc_state;
932 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state *s, int n)
936 tail = s->fifo[n].state & 0xf;
937 if (s->fifo[n].state & STELLARIS_ADC_FIFO_EMPTY) {
940 s->fifo[n].state = (s->fifo[n].state & ~0xf) | ((tail + 1) & 0xf);
941 s->fifo[n].state &= ~STELLARIS_ADC_FIFO_FULL;
942 if (tail + 1 == ((s->fifo[n].state >> 4) & 0xf))
943 s->fifo[n].state |= STELLARIS_ADC_FIFO_EMPTY;
945 return s->fifo[n].data[tail];
948 static void stellaris_adc_fifo_write(stellaris_adc_state *s, int n,
953 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
954 FIFO fir each sequencer. */
955 head = (s->fifo[n].state >> 4) & 0xf;
956 if (s->fifo[n].state & STELLARIS_ADC_FIFO_FULL) {
960 s->fifo[n].data[head] = value;
961 head = (head + 1) & 0xf;
962 s->fifo[n].state &= ~STELLARIS_ADC_FIFO_EMPTY;
963 s->fifo[n].state = (s->fifo[n].state & ~0xf0) | (head << 4);
964 if ((s->fifo[n].state & 0xf) == head)
965 s->fifo[n].state |= STELLARIS_ADC_FIFO_FULL;
968 static void stellaris_adc_update(stellaris_adc_state *s)
973 for (n = 0; n < 4; n++) {
974 level = (s->ris & s->im & (1 << n)) != 0;
975 qemu_set_irq(s->irq[n], level);
979 static void stellaris_adc_trigger(void *opaque, int irq, int level)
981 stellaris_adc_state *s = (stellaris_adc_state *)opaque;
984 for (n = 0; n < 4; n++) {
985 if ((s->actss & (1 << n)) == 0) {
989 if (((s->emux >> (n * 4)) & 0xff) != 5) {
993 /* Some applications use the ADC as a random number source, so introduce
994 some variation into the signal. */
995 s->noise = s->noise * 314159 + 1;
996 /* ??? actual inputs not implemented. Return an arbitrary value. */
997 stellaris_adc_fifo_write(s, n, 0x200 + ((s->noise >> 16) & 7));
999 stellaris_adc_update(s);
1003 static void stellaris_adc_reset(stellaris_adc_state *s)
1007 for (n = 0; n < 4; n++) {
1010 s->fifo[n].state = STELLARIS_ADC_FIFO_EMPTY;
1014 static uint64_t stellaris_adc_read(void *opaque, hwaddr offset,
1017 stellaris_adc_state *s = (stellaris_adc_state *)opaque;
1019 /* TODO: Implement this. */
1020 if (offset >= 0x40 && offset < 0xc0) {
1022 n = (offset - 0x40) >> 5;
1023 switch (offset & 0x1f) {
1024 case 0x00: /* SSMUX */
1026 case 0x04: /* SSCTL */
1028 case 0x08: /* SSFIFO */
1029 return stellaris_adc_fifo_read(s, n);
1030 case 0x0c: /* SSFSTAT */
1031 return s->fifo[n].state;
1037 case 0x00: /* ACTSS */
1039 case 0x04: /* RIS */
1043 case 0x0c: /* ISC */
1044 return s->ris & s->im;
1045 case 0x10: /* OSTAT */
1047 case 0x14: /* EMUX */
1049 case 0x18: /* USTAT */
1051 case 0x20: /* SSPRI */
1053 case 0x30: /* SAC */
1056 hw_error("strllaris_adc_read: Bad offset 0x%x\n",
1062 static void stellaris_adc_write(void *opaque, hwaddr offset,
1063 uint64_t value, unsigned size)
1065 stellaris_adc_state *s = (stellaris_adc_state *)opaque;
1067 /* TODO: Implement this. */
1068 if (offset >= 0x40 && offset < 0xc0) {
1070 n = (offset - 0x40) >> 5;
1071 switch (offset & 0x1f) {
1072 case 0x00: /* SSMUX */
1073 s->ssmux[n] = value & 0x33333333;
1075 case 0x04: /* SSCTL */
1077 hw_error("ADC: Unimplemented sequence %" PRIx64 "\n",
1080 s->ssctl[n] = value;
1087 case 0x00: /* ACTSS */
1088 s->actss = value & 0xf;
1093 case 0x0c: /* ISC */
1096 case 0x10: /* OSTAT */
1099 case 0x14: /* EMUX */
1102 case 0x18: /* USTAT */
1105 case 0x20: /* SSPRI */
1108 case 0x28: /* PSSI */
1109 hw_error("Not implemented: ADC sample initiate\n");
1111 case 0x30: /* SAC */
1115 hw_error("stellaris_adc_write: Bad offset 0x%x\n", (int)offset);
1117 stellaris_adc_update(s);
1120 static const MemoryRegionOps stellaris_adc_ops = {
1121 .read = stellaris_adc_read,
1122 .write = stellaris_adc_write,
1123 .endianness = DEVICE_NATIVE_ENDIAN,
1126 static const VMStateDescription vmstate_stellaris_adc = {
1127 .name = "stellaris_adc",
1129 .minimum_version_id = 1,
1130 .fields = (VMStateField[]) {
1131 VMSTATE_UINT32(actss, stellaris_adc_state),
1132 VMSTATE_UINT32(ris, stellaris_adc_state),
1133 VMSTATE_UINT32(im, stellaris_adc_state),
1134 VMSTATE_UINT32(emux, stellaris_adc_state),
1135 VMSTATE_UINT32(ostat, stellaris_adc_state),
1136 VMSTATE_UINT32(ustat, stellaris_adc_state),
1137 VMSTATE_UINT32(sspri, stellaris_adc_state),
1138 VMSTATE_UINT32(sac, stellaris_adc_state),
1139 VMSTATE_UINT32(fifo[0].state, stellaris_adc_state),
1140 VMSTATE_UINT32_ARRAY(fifo[0].data, stellaris_adc_state, 16),
1141 VMSTATE_UINT32(ssmux[0], stellaris_adc_state),
1142 VMSTATE_UINT32(ssctl[0], stellaris_adc_state),
1143 VMSTATE_UINT32(fifo[1].state, stellaris_adc_state),
1144 VMSTATE_UINT32_ARRAY(fifo[1].data, stellaris_adc_state, 16),
1145 VMSTATE_UINT32(ssmux[1], stellaris_adc_state),
1146 VMSTATE_UINT32(ssctl[1], stellaris_adc_state),
1147 VMSTATE_UINT32(fifo[2].state, stellaris_adc_state),
1148 VMSTATE_UINT32_ARRAY(fifo[2].data, stellaris_adc_state, 16),
1149 VMSTATE_UINT32(ssmux[2], stellaris_adc_state),
1150 VMSTATE_UINT32(ssctl[2], stellaris_adc_state),
1151 VMSTATE_UINT32(fifo[3].state, stellaris_adc_state),
1152 VMSTATE_UINT32_ARRAY(fifo[3].data, stellaris_adc_state, 16),
1153 VMSTATE_UINT32(ssmux[3], stellaris_adc_state),
1154 VMSTATE_UINT32(ssctl[3], stellaris_adc_state),
1155 VMSTATE_UINT32(noise, stellaris_adc_state),
1156 VMSTATE_END_OF_LIST()
1160 static int stellaris_adc_init(SysBusDevice *sbd)
1162 DeviceState *dev = DEVICE(sbd);
1163 stellaris_adc_state *s = STELLARIS_ADC(dev);
1166 for (n = 0; n < 4; n++) {
1167 sysbus_init_irq(sbd, &s->irq[n]);
1170 memory_region_init_io(&s->iomem, OBJECT(s), &stellaris_adc_ops, s,
1172 sysbus_init_mmio(sbd, &s->iomem);
1173 stellaris_adc_reset(s);
1174 qdev_init_gpio_in(dev, stellaris_adc_trigger, 1);
1175 vmstate_register(dev, -1, &vmstate_stellaris_adc, s);
1180 static stellaris_board_info stellaris_boards[] = {
1184 0x001f001f, /* dc0 */
1194 0x00ff007f, /* dc0 */
1199 BP_OLED_SSI | BP_GAMEPAD
1203 static void stellaris_init(const char *kernel_filename, const char *cpu_model,
1204 stellaris_board_info *board)
1206 static const int uart_irq[] = {5, 6, 33, 34};
1207 static const int timer_irq[] = {19, 21, 23, 35};
1208 static const uint32_t gpio_addr[7] =
1209 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1210 0x40024000, 0x40025000, 0x40026000};
1211 static const int gpio_irq[7] = {0, 1, 2, 3, 4, 30, 31};
1214 DeviceState *gpio_dev[7];
1215 qemu_irq gpio_in[7][8];
1216 qemu_irq gpio_out[7][8];
1225 MemoryRegion *sram = g_new(MemoryRegion, 1);
1226 MemoryRegion *flash = g_new(MemoryRegion, 1);
1227 MemoryRegion *system_memory = get_system_memory();
1229 flash_size = (((board->dc0 & 0xffff) + 1) << 1) * 1024;
1230 sram_size = ((board->dc0 >> 18) + 1) * 1024;
1232 /* Flash programming is done via the SCU, so pretend it is ROM. */
1233 memory_region_init_ram(flash, NULL, "stellaris.flash", flash_size,
1235 vmstate_register_ram_global(flash);
1236 memory_region_set_readonly(flash, true);
1237 memory_region_add_subregion(system_memory, 0, flash);
1239 memory_region_init_ram(sram, NULL, "stellaris.sram", sram_size,
1241 vmstate_register_ram_global(sram);
1242 memory_region_add_subregion(system_memory, 0x20000000, sram);
1244 pic = armv7m_init(system_memory, flash_size, NUM_IRQ_LINES,
1245 kernel_filename, cpu_model);
1247 if (board->dc1 & (1 << 16)) {
1248 dev = sysbus_create_varargs(TYPE_STELLARIS_ADC, 0x40038000,
1249 pic[14], pic[15], pic[16], pic[17], NULL);
1250 adc = qdev_get_gpio_in(dev, 0);
1254 for (i = 0; i < 4; i++) {
1255 if (board->dc2 & (0x10000 << i)) {
1256 dev = sysbus_create_simple(TYPE_STELLARIS_GPTM,
1257 0x40030000 + i * 0x1000,
1259 /* TODO: This is incorrect, but we get away with it because
1260 the ADC output is only ever pulsed. */
1261 qdev_connect_gpio_out(dev, 0, adc);
1265 stellaris_sys_init(0x400fe000, pic[28], board, nd_table[0].macaddr.a);
1267 for (i = 0; i < 7; i++) {
1268 if (board->dc4 & (1 << i)) {
1269 gpio_dev[i] = sysbus_create_simple("pl061_luminary", gpio_addr[i],
1271 for (j = 0; j < 8; j++) {
1272 gpio_in[i][j] = qdev_get_gpio_in(gpio_dev[i], j);
1273 gpio_out[i][j] = NULL;
1278 if (board->dc2 & (1 << 12)) {
1279 dev = sysbus_create_simple(TYPE_STELLARIS_I2C, 0x40020000, pic[8]);
1280 i2c = (I2CBus *)qdev_get_child_bus(dev, "i2c");
1281 if (board->peripherals & BP_OLED_I2C) {
1282 i2c_create_slave(i2c, "ssd0303", 0x3d);
1286 for (i = 0; i < 4; i++) {
1287 if (board->dc2 & (1 << i)) {
1288 sysbus_create_simple("pl011_luminary", 0x4000c000 + i * 0x1000,
1292 if (board->dc2 & (1 << 4)) {
1293 dev = sysbus_create_simple("pl022", 0x40008000, pic[7]);
1294 if (board->peripherals & BP_OLED_SSI) {
1297 DeviceState *ssddev;
1299 /* Some boards have both an OLED controller and SD card connected to
1300 * the same SSI port, with the SD card chip select connected to a
1301 * GPIO pin. Technically the OLED chip select is connected to the
1302 * SSI Fss pin. We do not bother emulating that as both devices
1303 * should never be selected simultaneously, and our OLED controller
1304 * ignores stray 0xff commands that occur when deselecting the SD
1307 bus = qdev_get_child_bus(dev, "ssi");
1309 sddev = ssi_create_slave(bus, "ssi-sd");
1310 ssddev = ssi_create_slave(bus, "ssd0323");
1311 gpio_out[GPIO_D][0] = qemu_irq_split(
1312 qdev_get_gpio_in_named(sddev, SSI_GPIO_CS, 0),
1313 qdev_get_gpio_in_named(ssddev, SSI_GPIO_CS, 0));
1314 gpio_out[GPIO_C][7] = qdev_get_gpio_in(ssddev, 0);
1316 /* Make sure the select pin is high. */
1317 qemu_irq_raise(gpio_out[GPIO_D][0]);
1320 if (board->dc4 & (1 << 28)) {
1323 qemu_check_nic_model(&nd_table[0], "stellaris");
1325 enet = qdev_create(NULL, "stellaris_enet");
1326 qdev_set_nic_properties(enet, &nd_table[0]);
1327 qdev_init_nofail(enet);
1328 sysbus_mmio_map(SYS_BUS_DEVICE(enet), 0, 0x40048000);
1329 sysbus_connect_irq(SYS_BUS_DEVICE(enet), 0, pic[42]);
1331 if (board->peripherals & BP_GAMEPAD) {
1332 qemu_irq gpad_irq[5];
1333 static const int gpad_keycode[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1335 gpad_irq[0] = qemu_irq_invert(gpio_in[GPIO_E][0]); /* up */
1336 gpad_irq[1] = qemu_irq_invert(gpio_in[GPIO_E][1]); /* down */
1337 gpad_irq[2] = qemu_irq_invert(gpio_in[GPIO_E][2]); /* left */
1338 gpad_irq[3] = qemu_irq_invert(gpio_in[GPIO_E][3]); /* right */
1339 gpad_irq[4] = qemu_irq_invert(gpio_in[GPIO_F][1]); /* select */
1341 stellaris_gamepad_init(5, gpad_irq, gpad_keycode);
1343 for (i = 0; i < 7; i++) {
1344 if (board->dc4 & (1 << i)) {
1345 for (j = 0; j < 8; j++) {
1346 if (gpio_out[i][j]) {
1347 qdev_connect_gpio_out(gpio_dev[i], j, gpio_out[i][j]);
1354 /* FIXME: Figure out how to generate these from stellaris_boards. */
1355 static void lm3s811evb_init(MachineState *machine)
1357 const char *cpu_model = machine->cpu_model;
1358 const char *kernel_filename = machine->kernel_filename;
1359 stellaris_init(kernel_filename, cpu_model, &stellaris_boards[0]);
1362 static void lm3s6965evb_init(MachineState *machine)
1364 const char *cpu_model = machine->cpu_model;
1365 const char *kernel_filename = machine->kernel_filename;
1366 stellaris_init(kernel_filename, cpu_model, &stellaris_boards[1]);
1369 static void lm3s811evb_class_init(ObjectClass *oc, void *data)
1371 MachineClass *mc = MACHINE_CLASS(oc);
1373 mc->desc = "Stellaris LM3S811EVB";
1374 mc->init = lm3s811evb_init;
1377 static const TypeInfo lm3s811evb_type = {
1378 .name = MACHINE_TYPE_NAME("lm3s811evb"),
1379 .parent = TYPE_MACHINE,
1380 .class_init = lm3s811evb_class_init,
1383 static void lm3s6965evb_class_init(ObjectClass *oc, void *data)
1385 MachineClass *mc = MACHINE_CLASS(oc);
1387 mc->desc = "Stellaris LM3S6965EVB";
1388 mc->init = lm3s6965evb_init;
1391 static const TypeInfo lm3s6965evb_type = {
1392 .name = MACHINE_TYPE_NAME("lm3s6965evb"),
1393 .parent = TYPE_MACHINE,
1394 .class_init = lm3s6965evb_class_init,
1397 static void stellaris_machine_init(void)
1399 type_register_static(&lm3s811evb_type);
1400 type_register_static(&lm3s6965evb_type);
1403 machine_init(stellaris_machine_init)
1405 static void stellaris_i2c_class_init(ObjectClass *klass, void *data)
1407 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
1409 sdc->init = stellaris_i2c_init;
1412 static const TypeInfo stellaris_i2c_info = {
1413 .name = TYPE_STELLARIS_I2C,
1414 .parent = TYPE_SYS_BUS_DEVICE,
1415 .instance_size = sizeof(stellaris_i2c_state),
1416 .class_init = stellaris_i2c_class_init,
1419 static void stellaris_gptm_class_init(ObjectClass *klass, void *data)
1421 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
1423 sdc->init = stellaris_gptm_init;
1426 static const TypeInfo stellaris_gptm_info = {
1427 .name = TYPE_STELLARIS_GPTM,
1428 .parent = TYPE_SYS_BUS_DEVICE,
1429 .instance_size = sizeof(gptm_state),
1430 .class_init = stellaris_gptm_class_init,
1433 static void stellaris_adc_class_init(ObjectClass *klass, void *data)
1435 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
1437 sdc->init = stellaris_adc_init;
1440 static const TypeInfo stellaris_adc_info = {
1441 .name = TYPE_STELLARIS_ADC,
1442 .parent = TYPE_SYS_BUS_DEVICE,
1443 .instance_size = sizeof(stellaris_adc_state),
1444 .class_init = stellaris_adc_class_init,
1447 static void stellaris_register_types(void)
1449 type_register_static(&stellaris_i2c_info);
1450 type_register_static(&stellaris_gptm_info);
1451 type_register_static(&stellaris_adc_info);
1454 type_init(stellaris_register_types)