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 typedef const struct {
42 } stellaris_board_info;
44 /* General purpose timer module. */
46 #define TYPE_STELLARIS_GPTM "stellaris-gptm"
47 #define STELLARIS_GPTM(obj) \
48 OBJECT_CHECK(gptm_state, (obj), TYPE_STELLARIS_GPTM)
50 typedef struct gptm_state {
51 SysBusDevice parent_obj;
62 uint32_t match_prescale[2];
65 struct gptm_state *opaque[2];
67 /* The timers have an alternate output used to trigger the ADC. */
72 static void gptm_update_irq(gptm_state *s)
75 level = (s->state & s->mask) != 0;
76 qemu_set_irq(s->irq, level);
79 static void gptm_stop(gptm_state *s, int n)
81 timer_del(s->timer[n]);
84 static void gptm_reload(gptm_state *s, int n, int reset)
88 tick = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
93 /* 32-bit CountDown. */
95 count = s->load[0] | (s->load[1] << 16);
96 tick += (int64_t)count * system_clock_scale;
97 } else if (s->config == 1) {
98 /* 32-bit RTC. 1Hz tick. */
99 tick += get_ticks_per_sec();
100 } else if (s->mode[n] == 0xa) {
101 /* PWM mode. Not implemented. */
103 hw_error("TODO: 16-bit timer mode 0x%x\n", s->mode[n]);
106 timer_mod(s->timer[n], tick);
109 static void gptm_tick(void *opaque)
111 gptm_state **p = (gptm_state **)opaque;
117 if (s->config == 0) {
119 if ((s->control & 0x20)) {
120 /* Output trigger. */
121 qemu_irq_pulse(s->trigger);
123 if (s->mode[0] & 1) {
128 gptm_reload(s, 0, 0);
130 } else if (s->config == 1) {
134 match = s->match[0] | (s->match[1] << 16);
140 gptm_reload(s, 0, 0);
141 } else if (s->mode[n] == 0xa) {
142 /* PWM mode. Not implemented. */
144 hw_error("TODO: 16-bit timer mode 0x%x\n", s->mode[n]);
149 static uint64_t gptm_read(void *opaque, hwaddr offset,
152 gptm_state *s = (gptm_state *)opaque;
157 case 0x04: /* TAMR */
159 case 0x08: /* TBMR */
168 return s->state & s->mask;
171 case 0x28: /* TAILR */
172 return s->load[0] | ((s->config < 4) ? (s->load[1] << 16) : 0);
173 case 0x2c: /* TBILR */
175 case 0x30: /* TAMARCHR */
176 return s->match[0] | ((s->config < 4) ? (s->match[1] << 16) : 0);
177 case 0x34: /* TBMATCHR */
179 case 0x38: /* TAPR */
180 return s->prescale[0];
181 case 0x3c: /* TBPR */
182 return s->prescale[1];
183 case 0x40: /* TAPMR */
184 return s->match_prescale[0];
185 case 0x44: /* TBPMR */
186 return s->match_prescale[1];
188 if (s->config == 1) {
191 qemu_log_mask(LOG_UNIMP,
192 "GPTM: read of TAR but timer read not supported");
195 qemu_log_mask(LOG_UNIMP,
196 "GPTM: read of TBR but timer read not supported");
199 qemu_log_mask(LOG_GUEST_ERROR,
200 "GPTM: read at bad offset 0x%x\n", (int)offset);
205 static void gptm_write(void *opaque, hwaddr offset,
206 uint64_t value, unsigned size)
208 gptm_state *s = (gptm_state *)opaque;
211 /* The timers should be disabled before changing the configuration.
212 We take advantage of this and defer everything until the timer
218 case 0x04: /* TAMR */
221 case 0x08: /* TBMR */
227 /* TODO: Implement pause. */
228 if ((oldval ^ value) & 1) {
230 gptm_reload(s, 0, 1);
235 if (((oldval ^ value) & 0x100) && s->config >= 4) {
237 gptm_reload(s, 1, 1);
244 s->mask = value & 0x77;
250 case 0x28: /* TAILR */
251 s->load[0] = value & 0xffff;
253 s->load[1] = value >> 16;
256 case 0x2c: /* TBILR */
257 s->load[1] = value & 0xffff;
259 case 0x30: /* TAMARCHR */
260 s->match[0] = value & 0xffff;
262 s->match[1] = value >> 16;
265 case 0x34: /* TBMATCHR */
266 s->match[1] = value >> 16;
268 case 0x38: /* TAPR */
269 s->prescale[0] = value;
271 case 0x3c: /* TBPR */
272 s->prescale[1] = value;
274 case 0x40: /* TAPMR */
275 s->match_prescale[0] = value;
277 case 0x44: /* TBPMR */
278 s->match_prescale[0] = value;
281 hw_error("gptm_write: Bad offset 0x%x\n", (int)offset);
286 static const MemoryRegionOps gptm_ops = {
289 .endianness = DEVICE_NATIVE_ENDIAN,
292 static const VMStateDescription vmstate_stellaris_gptm = {
293 .name = "stellaris_gptm",
295 .minimum_version_id = 1,
296 .fields = (VMStateField[]) {
297 VMSTATE_UINT32(config, gptm_state),
298 VMSTATE_UINT32_ARRAY(mode, gptm_state, 2),
299 VMSTATE_UINT32(control, gptm_state),
300 VMSTATE_UINT32(state, gptm_state),
301 VMSTATE_UINT32(mask, gptm_state),
303 VMSTATE_UINT32_ARRAY(load, gptm_state, 2),
304 VMSTATE_UINT32_ARRAY(match, gptm_state, 2),
305 VMSTATE_UINT32_ARRAY(prescale, gptm_state, 2),
306 VMSTATE_UINT32_ARRAY(match_prescale, gptm_state, 2),
307 VMSTATE_UINT32(rtc, gptm_state),
308 VMSTATE_INT64_ARRAY(tick, gptm_state, 2),
309 VMSTATE_TIMER_ARRAY(timer, gptm_state, 2),
310 VMSTATE_END_OF_LIST()
314 static int stellaris_gptm_init(SysBusDevice *sbd)
316 DeviceState *dev = DEVICE(sbd);
317 gptm_state *s = STELLARIS_GPTM(dev);
319 sysbus_init_irq(sbd, &s->irq);
320 qdev_init_gpio_out(dev, &s->trigger, 1);
322 memory_region_init_io(&s->iomem, OBJECT(s), &gptm_ops, s,
324 sysbus_init_mmio(sbd, &s->iomem);
326 s->opaque[0] = s->opaque[1] = s;
327 s->timer[0] = timer_new_ns(QEMU_CLOCK_VIRTUAL, gptm_tick, &s->opaque[0]);
328 s->timer[1] = timer_new_ns(QEMU_CLOCK_VIRTUAL, gptm_tick, &s->opaque[1]);
329 vmstate_register(dev, -1, &vmstate_stellaris_gptm, s);
334 /* System controller. */
353 stellaris_board_info *board;
356 static void ssys_update(ssys_state *s)
358 qemu_set_irq(s->irq, (s->int_status & s->int_mask) != 0);
361 static uint32_t pllcfg_sandstorm[16] = {
363 0x1ae0, /* 1.8432 Mhz */
365 0xd573, /* 2.4576 Mhz */
366 0x37a6, /* 3.57954 Mhz */
367 0x1ae2, /* 3.6864 Mhz */
369 0x98bc, /* 4.906 Mhz */
370 0x935b, /* 4.9152 Mhz */
372 0x4dee, /* 5.12 Mhz */
374 0x75db, /* 6.144 Mhz */
375 0x1ae6, /* 7.3728 Mhz */
377 0x585b /* 8.192 Mhz */
380 static uint32_t pllcfg_fury[16] = {
382 0x1b20, /* 1.8432 Mhz */
384 0xf42b, /* 2.4576 Mhz */
385 0x37e3, /* 3.57954 Mhz */
386 0x1b21, /* 3.6864 Mhz */
388 0x98ee, /* 4.906 Mhz */
389 0xd5b4, /* 4.9152 Mhz */
391 0x4e27, /* 5.12 Mhz */
393 0xec1c, /* 6.144 Mhz */
394 0x1b23, /* 7.3728 Mhz */
396 0xb11c /* 8.192 Mhz */
399 #define DID0_VER_MASK 0x70000000
400 #define DID0_VER_0 0x00000000
401 #define DID0_VER_1 0x10000000
403 #define DID0_CLASS_MASK 0x00FF0000
404 #define DID0_CLASS_SANDSTORM 0x00000000
405 #define DID0_CLASS_FURY 0x00010000
407 static int ssys_board_class(const ssys_state *s)
409 uint32_t did0 = s->board->did0;
410 switch (did0 & DID0_VER_MASK) {
412 return DID0_CLASS_SANDSTORM;
414 switch (did0 & DID0_CLASS_MASK) {
415 case DID0_CLASS_SANDSTORM:
416 case DID0_CLASS_FURY:
417 return did0 & DID0_CLASS_MASK;
419 /* for unknown classes, fall through */
421 hw_error("ssys_board_class: Unknown class 0x%08x\n", did0);
425 static uint64_t ssys_read(void *opaque, hwaddr offset,
428 ssys_state *s = (ssys_state *)opaque;
431 case 0x000: /* DID0 */
432 return s->board->did0;
433 case 0x004: /* DID1 */
434 return s->board->did1;
435 case 0x008: /* DC0 */
436 return s->board->dc0;
437 case 0x010: /* DC1 */
438 return s->board->dc1;
439 case 0x014: /* DC2 */
440 return s->board->dc2;
441 case 0x018: /* DC3 */
442 return s->board->dc3;
443 case 0x01c: /* DC4 */
444 return s->board->dc4;
445 case 0x030: /* PBORCTL */
447 case 0x034: /* LDOPCTL */
449 case 0x040: /* SRCR0 */
451 case 0x044: /* SRCR1 */
453 case 0x048: /* SRCR2 */
455 case 0x050: /* RIS */
456 return s->int_status;
457 case 0x054: /* IMC */
459 case 0x058: /* MISC */
460 return s->int_status & s->int_mask;
461 case 0x05c: /* RESC */
463 case 0x060: /* RCC */
465 case 0x064: /* PLLCFG */
468 xtal = (s->rcc >> 6) & 0xf;
469 switch (ssys_board_class(s)) {
470 case DID0_CLASS_FURY:
471 return pllcfg_fury[xtal];
472 case DID0_CLASS_SANDSTORM:
473 return pllcfg_sandstorm[xtal];
475 hw_error("ssys_read: Unhandled class for PLLCFG read.\n");
479 case 0x070: /* RCC2 */
481 case 0x100: /* RCGC0 */
483 case 0x104: /* RCGC1 */
485 case 0x108: /* RCGC2 */
487 case 0x110: /* SCGC0 */
489 case 0x114: /* SCGC1 */
491 case 0x118: /* SCGC2 */
493 case 0x120: /* DCGC0 */
495 case 0x124: /* DCGC1 */
497 case 0x128: /* DCGC2 */
499 case 0x150: /* CLKVCLR */
501 case 0x160: /* LDOARST */
503 case 0x1e0: /* USER0 */
505 case 0x1e4: /* USER1 */
508 hw_error("ssys_read: Bad offset 0x%x\n", (int)offset);
513 static bool ssys_use_rcc2(ssys_state *s)
515 return (s->rcc2 >> 31) & 0x1;
519 * Caculate the sys. clock period in ms.
521 static void ssys_calculate_system_clock(ssys_state *s)
523 if (ssys_use_rcc2(s)) {
524 system_clock_scale = 5 * (((s->rcc2 >> 23) & 0x3f) + 1);
526 system_clock_scale = 5 * (((s->rcc >> 23) & 0xf) + 1);
530 static void ssys_write(void *opaque, hwaddr offset,
531 uint64_t value, unsigned size)
533 ssys_state *s = (ssys_state *)opaque;
536 case 0x030: /* PBORCTL */
537 s->pborctl = value & 0xffff;
539 case 0x034: /* LDOPCTL */
540 s->ldopctl = value & 0x1f;
542 case 0x040: /* SRCR0 */
543 case 0x044: /* SRCR1 */
544 case 0x048: /* SRCR2 */
545 fprintf(stderr, "Peripheral reset not implemented\n");
547 case 0x054: /* IMC */
548 s->int_mask = value & 0x7f;
550 case 0x058: /* MISC */
551 s->int_status &= ~value;
553 case 0x05c: /* RESC */
554 s->resc = value & 0x3f;
556 case 0x060: /* RCC */
557 if ((s->rcc & (1 << 13)) != 0 && (value & (1 << 13)) == 0) {
559 s->int_status |= (1 << 6);
562 ssys_calculate_system_clock(s);
564 case 0x070: /* RCC2 */
565 if (ssys_board_class(s) == DID0_CLASS_SANDSTORM) {
569 if ((s->rcc2 & (1 << 13)) != 0 && (value & (1 << 13)) == 0) {
571 s->int_status |= (1 << 6);
574 ssys_calculate_system_clock(s);
576 case 0x100: /* RCGC0 */
579 case 0x104: /* RCGC1 */
582 case 0x108: /* RCGC2 */
585 case 0x110: /* SCGC0 */
588 case 0x114: /* SCGC1 */
591 case 0x118: /* SCGC2 */
594 case 0x120: /* DCGC0 */
597 case 0x124: /* DCGC1 */
600 case 0x128: /* DCGC2 */
603 case 0x150: /* CLKVCLR */
606 case 0x160: /* LDOARST */
610 hw_error("ssys_write: Bad offset 0x%x\n", (int)offset);
615 static const MemoryRegionOps ssys_ops = {
618 .endianness = DEVICE_NATIVE_ENDIAN,
621 static void ssys_reset(void *opaque)
623 ssys_state *s = (ssys_state *)opaque;
628 if (ssys_board_class(s) == DID0_CLASS_SANDSTORM) {
631 s->rcc2 = 0x07802810;
636 ssys_calculate_system_clock(s);
639 static int stellaris_sys_post_load(void *opaque, int version_id)
641 ssys_state *s = opaque;
643 ssys_calculate_system_clock(s);
648 static const VMStateDescription vmstate_stellaris_sys = {
649 .name = "stellaris_sys",
651 .minimum_version_id = 1,
652 .post_load = stellaris_sys_post_load,
653 .fields = (VMStateField[]) {
654 VMSTATE_UINT32(pborctl, ssys_state),
655 VMSTATE_UINT32(ldopctl, ssys_state),
656 VMSTATE_UINT32(int_mask, ssys_state),
657 VMSTATE_UINT32(int_status, ssys_state),
658 VMSTATE_UINT32(resc, ssys_state),
659 VMSTATE_UINT32(rcc, ssys_state),
660 VMSTATE_UINT32_V(rcc2, ssys_state, 2),
661 VMSTATE_UINT32_ARRAY(rcgc, ssys_state, 3),
662 VMSTATE_UINT32_ARRAY(scgc, ssys_state, 3),
663 VMSTATE_UINT32_ARRAY(dcgc, ssys_state, 3),
664 VMSTATE_UINT32(clkvclr, ssys_state),
665 VMSTATE_UINT32(ldoarst, ssys_state),
666 VMSTATE_END_OF_LIST()
670 static int stellaris_sys_init(uint32_t base, qemu_irq irq,
671 stellaris_board_info * board,
676 s = (ssys_state *)g_malloc0(sizeof(ssys_state));
679 /* Most devices come preprogrammed with a MAC address in the user data. */
680 s->user0 = macaddr[0] | (macaddr[1] << 8) | (macaddr[2] << 16);
681 s->user1 = macaddr[3] | (macaddr[4] << 8) | (macaddr[5] << 16);
683 memory_region_init_io(&s->iomem, NULL, &ssys_ops, s, "ssys", 0x00001000);
684 memory_region_add_subregion(get_system_memory(), base, &s->iomem);
686 vmstate_register(NULL, -1, &vmstate_stellaris_sys, s);
691 /* I2C controller. */
693 #define TYPE_STELLARIS_I2C "stellaris-i2c"
694 #define STELLARIS_I2C(obj) \
695 OBJECT_CHECK(stellaris_i2c_state, (obj), TYPE_STELLARIS_I2C)
698 SysBusDevice parent_obj;
710 } stellaris_i2c_state;
712 #define STELLARIS_I2C_MCS_BUSY 0x01
713 #define STELLARIS_I2C_MCS_ERROR 0x02
714 #define STELLARIS_I2C_MCS_ADRACK 0x04
715 #define STELLARIS_I2C_MCS_DATACK 0x08
716 #define STELLARIS_I2C_MCS_ARBLST 0x10
717 #define STELLARIS_I2C_MCS_IDLE 0x20
718 #define STELLARIS_I2C_MCS_BUSBSY 0x40
720 static uint64_t stellaris_i2c_read(void *opaque, hwaddr offset,
723 stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;
729 /* We don't emulate timing, so the controller is never busy. */
730 return s->mcs | STELLARIS_I2C_MCS_IDLE;
733 case 0x0c: /* MTPR */
735 case 0x10: /* MIMR */
737 case 0x14: /* MRIS */
739 case 0x18: /* MMIS */
740 return s->mris & s->mimr;
744 hw_error("strllaris_i2c_read: Bad offset 0x%x\n", (int)offset);
749 static void stellaris_i2c_update(stellaris_i2c_state *s)
753 level = (s->mris & s->mimr) != 0;
754 qemu_set_irq(s->irq, level);
757 static void stellaris_i2c_write(void *opaque, hwaddr offset,
758 uint64_t value, unsigned size)
760 stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;
764 s->msa = value & 0xff;
767 if ((s->mcr & 0x10) == 0) {
768 /* Disabled. Do nothing. */
771 /* Grab the bus if this is starting a transfer. */
772 if ((value & 2) && (s->mcs & STELLARIS_I2C_MCS_BUSBSY) == 0) {
773 if (i2c_start_transfer(s->bus, s->msa >> 1, s->msa & 1)) {
774 s->mcs |= STELLARIS_I2C_MCS_ARBLST;
776 s->mcs &= ~STELLARIS_I2C_MCS_ARBLST;
777 s->mcs |= STELLARIS_I2C_MCS_BUSBSY;
780 /* If we don't have the bus then indicate an error. */
781 if (!i2c_bus_busy(s->bus)
782 || (s->mcs & STELLARIS_I2C_MCS_BUSBSY) == 0) {
783 s->mcs |= STELLARIS_I2C_MCS_ERROR;
786 s->mcs &= ~STELLARIS_I2C_MCS_ERROR;
788 /* Transfer a byte. */
789 /* TODO: Handle errors. */
792 s->mdr = i2c_recv(s->bus) & 0xff;
795 i2c_send(s->bus, s->mdr);
797 /* Raise an interrupt. */
801 /* Finish transfer. */
802 i2c_end_transfer(s->bus);
803 s->mcs &= ~STELLARIS_I2C_MCS_BUSBSY;
807 s->mdr = value & 0xff;
809 case 0x0c: /* MTPR */
810 s->mtpr = value & 0xff;
812 case 0x10: /* MIMR */
815 case 0x1c: /* MICR */
821 "stellaris_i2c_write: Loopback not implemented\n");
824 "stellaris_i2c_write: Slave mode not implemented\n");
825 s->mcr = value & 0x31;
828 hw_error("stellaris_i2c_write: Bad offset 0x%x\n",
831 stellaris_i2c_update(s);
834 static void stellaris_i2c_reset(stellaris_i2c_state *s)
836 if (s->mcs & STELLARIS_I2C_MCS_BUSBSY)
837 i2c_end_transfer(s->bus);
846 stellaris_i2c_update(s);
849 static const MemoryRegionOps stellaris_i2c_ops = {
850 .read = stellaris_i2c_read,
851 .write = stellaris_i2c_write,
852 .endianness = DEVICE_NATIVE_ENDIAN,
855 static const VMStateDescription vmstate_stellaris_i2c = {
856 .name = "stellaris_i2c",
858 .minimum_version_id = 1,
859 .fields = (VMStateField[]) {
860 VMSTATE_UINT32(msa, stellaris_i2c_state),
861 VMSTATE_UINT32(mcs, stellaris_i2c_state),
862 VMSTATE_UINT32(mdr, stellaris_i2c_state),
863 VMSTATE_UINT32(mtpr, stellaris_i2c_state),
864 VMSTATE_UINT32(mimr, stellaris_i2c_state),
865 VMSTATE_UINT32(mris, stellaris_i2c_state),
866 VMSTATE_UINT32(mcr, stellaris_i2c_state),
867 VMSTATE_END_OF_LIST()
871 static int stellaris_i2c_init(SysBusDevice *sbd)
873 DeviceState *dev = DEVICE(sbd);
874 stellaris_i2c_state *s = STELLARIS_I2C(dev);
877 sysbus_init_irq(sbd, &s->irq);
878 bus = i2c_init_bus(dev, "i2c");
881 memory_region_init_io(&s->iomem, OBJECT(s), &stellaris_i2c_ops, s,
883 sysbus_init_mmio(sbd, &s->iomem);
884 /* ??? For now we only implement the master interface. */
885 stellaris_i2c_reset(s);
886 vmstate_register(dev, -1, &vmstate_stellaris_i2c, s);
890 /* Analogue to Digital Converter. This is only partially implemented,
891 enough for applications that use a combined ADC and timer tick. */
893 #define STELLARIS_ADC_EM_CONTROLLER 0
894 #define STELLARIS_ADC_EM_COMP 1
895 #define STELLARIS_ADC_EM_EXTERNAL 4
896 #define STELLARIS_ADC_EM_TIMER 5
897 #define STELLARIS_ADC_EM_PWM0 6
898 #define STELLARIS_ADC_EM_PWM1 7
899 #define STELLARIS_ADC_EM_PWM2 8
901 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
902 #define STELLARIS_ADC_FIFO_FULL 0x1000
904 #define TYPE_STELLARIS_ADC "stellaris-adc"
905 #define STELLARIS_ADC(obj) \
906 OBJECT_CHECK(stellaris_adc_state, (obj), TYPE_STELLARIS_ADC)
908 typedef struct StellarisADCState {
909 SysBusDevice parent_obj;
928 } stellaris_adc_state;
930 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state *s, int n)
934 tail = s->fifo[n].state & 0xf;
935 if (s->fifo[n].state & STELLARIS_ADC_FIFO_EMPTY) {
938 s->fifo[n].state = (s->fifo[n].state & ~0xf) | ((tail + 1) & 0xf);
939 s->fifo[n].state &= ~STELLARIS_ADC_FIFO_FULL;
940 if (tail + 1 == ((s->fifo[n].state >> 4) & 0xf))
941 s->fifo[n].state |= STELLARIS_ADC_FIFO_EMPTY;
943 return s->fifo[n].data[tail];
946 static void stellaris_adc_fifo_write(stellaris_adc_state *s, int n,
951 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
952 FIFO fir each sequencer. */
953 head = (s->fifo[n].state >> 4) & 0xf;
954 if (s->fifo[n].state & STELLARIS_ADC_FIFO_FULL) {
958 s->fifo[n].data[head] = value;
959 head = (head + 1) & 0xf;
960 s->fifo[n].state &= ~STELLARIS_ADC_FIFO_EMPTY;
961 s->fifo[n].state = (s->fifo[n].state & ~0xf0) | (head << 4);
962 if ((s->fifo[n].state & 0xf) == head)
963 s->fifo[n].state |= STELLARIS_ADC_FIFO_FULL;
966 static void stellaris_adc_update(stellaris_adc_state *s)
971 for (n = 0; n < 4; n++) {
972 level = (s->ris & s->im & (1 << n)) != 0;
973 qemu_set_irq(s->irq[n], level);
977 static void stellaris_adc_trigger(void *opaque, int irq, int level)
979 stellaris_adc_state *s = (stellaris_adc_state *)opaque;
982 for (n = 0; n < 4; n++) {
983 if ((s->actss & (1 << n)) == 0) {
987 if (((s->emux >> (n * 4)) & 0xff) != 5) {
991 /* Some applications use the ADC as a random number source, so introduce
992 some variation into the signal. */
993 s->noise = s->noise * 314159 + 1;
994 /* ??? actual inputs not implemented. Return an arbitrary value. */
995 stellaris_adc_fifo_write(s, n, 0x200 + ((s->noise >> 16) & 7));
997 stellaris_adc_update(s);
1001 static void stellaris_adc_reset(stellaris_adc_state *s)
1005 for (n = 0; n < 4; n++) {
1008 s->fifo[n].state = STELLARIS_ADC_FIFO_EMPTY;
1012 static uint64_t stellaris_adc_read(void *opaque, hwaddr offset,
1015 stellaris_adc_state *s = (stellaris_adc_state *)opaque;
1017 /* TODO: Implement this. */
1018 if (offset >= 0x40 && offset < 0xc0) {
1020 n = (offset - 0x40) >> 5;
1021 switch (offset & 0x1f) {
1022 case 0x00: /* SSMUX */
1024 case 0x04: /* SSCTL */
1026 case 0x08: /* SSFIFO */
1027 return stellaris_adc_fifo_read(s, n);
1028 case 0x0c: /* SSFSTAT */
1029 return s->fifo[n].state;
1035 case 0x00: /* ACTSS */
1037 case 0x04: /* RIS */
1041 case 0x0c: /* ISC */
1042 return s->ris & s->im;
1043 case 0x10: /* OSTAT */
1045 case 0x14: /* EMUX */
1047 case 0x18: /* USTAT */
1049 case 0x20: /* SSPRI */
1051 case 0x30: /* SAC */
1054 hw_error("strllaris_adc_read: Bad offset 0x%x\n",
1060 static void stellaris_adc_write(void *opaque, hwaddr offset,
1061 uint64_t value, unsigned size)
1063 stellaris_adc_state *s = (stellaris_adc_state *)opaque;
1065 /* TODO: Implement this. */
1066 if (offset >= 0x40 && offset < 0xc0) {
1068 n = (offset - 0x40) >> 5;
1069 switch (offset & 0x1f) {
1070 case 0x00: /* SSMUX */
1071 s->ssmux[n] = value & 0x33333333;
1073 case 0x04: /* SSCTL */
1075 hw_error("ADC: Unimplemented sequence %" PRIx64 "\n",
1078 s->ssctl[n] = value;
1085 case 0x00: /* ACTSS */
1086 s->actss = value & 0xf;
1091 case 0x0c: /* ISC */
1094 case 0x10: /* OSTAT */
1097 case 0x14: /* EMUX */
1100 case 0x18: /* USTAT */
1103 case 0x20: /* SSPRI */
1106 case 0x28: /* PSSI */
1107 hw_error("Not implemented: ADC sample initiate\n");
1109 case 0x30: /* SAC */
1113 hw_error("stellaris_adc_write: Bad offset 0x%x\n", (int)offset);
1115 stellaris_adc_update(s);
1118 static const MemoryRegionOps stellaris_adc_ops = {
1119 .read = stellaris_adc_read,
1120 .write = stellaris_adc_write,
1121 .endianness = DEVICE_NATIVE_ENDIAN,
1124 static const VMStateDescription vmstate_stellaris_adc = {
1125 .name = "stellaris_adc",
1127 .minimum_version_id = 1,
1128 .fields = (VMStateField[]) {
1129 VMSTATE_UINT32(actss, stellaris_adc_state),
1130 VMSTATE_UINT32(ris, stellaris_adc_state),
1131 VMSTATE_UINT32(im, stellaris_adc_state),
1132 VMSTATE_UINT32(emux, stellaris_adc_state),
1133 VMSTATE_UINT32(ostat, stellaris_adc_state),
1134 VMSTATE_UINT32(ustat, stellaris_adc_state),
1135 VMSTATE_UINT32(sspri, stellaris_adc_state),
1136 VMSTATE_UINT32(sac, stellaris_adc_state),
1137 VMSTATE_UINT32(fifo[0].state, stellaris_adc_state),
1138 VMSTATE_UINT32_ARRAY(fifo[0].data, stellaris_adc_state, 16),
1139 VMSTATE_UINT32(ssmux[0], stellaris_adc_state),
1140 VMSTATE_UINT32(ssctl[0], stellaris_adc_state),
1141 VMSTATE_UINT32(fifo[1].state, stellaris_adc_state),
1142 VMSTATE_UINT32_ARRAY(fifo[1].data, stellaris_adc_state, 16),
1143 VMSTATE_UINT32(ssmux[1], stellaris_adc_state),
1144 VMSTATE_UINT32(ssctl[1], stellaris_adc_state),
1145 VMSTATE_UINT32(fifo[2].state, stellaris_adc_state),
1146 VMSTATE_UINT32_ARRAY(fifo[2].data, stellaris_adc_state, 16),
1147 VMSTATE_UINT32(ssmux[2], stellaris_adc_state),
1148 VMSTATE_UINT32(ssctl[2], stellaris_adc_state),
1149 VMSTATE_UINT32(fifo[3].state, stellaris_adc_state),
1150 VMSTATE_UINT32_ARRAY(fifo[3].data, stellaris_adc_state, 16),
1151 VMSTATE_UINT32(ssmux[3], stellaris_adc_state),
1152 VMSTATE_UINT32(ssctl[3], stellaris_adc_state),
1153 VMSTATE_UINT32(noise, stellaris_adc_state),
1154 VMSTATE_END_OF_LIST()
1158 static int stellaris_adc_init(SysBusDevice *sbd)
1160 DeviceState *dev = DEVICE(sbd);
1161 stellaris_adc_state *s = STELLARIS_ADC(dev);
1164 for (n = 0; n < 4; n++) {
1165 sysbus_init_irq(sbd, &s->irq[n]);
1168 memory_region_init_io(&s->iomem, OBJECT(s), &stellaris_adc_ops, s,
1170 sysbus_init_mmio(sbd, &s->iomem);
1171 stellaris_adc_reset(s);
1172 qdev_init_gpio_in(dev, stellaris_adc_trigger, 1);
1173 vmstate_register(dev, -1, &vmstate_stellaris_adc, s);
1178 static stellaris_board_info stellaris_boards[] = {
1182 0x001f001f, /* dc0 */
1192 0x00ff007f, /* dc0 */
1197 BP_OLED_SSI | BP_GAMEPAD
1201 static void stellaris_init(const char *kernel_filename, const char *cpu_model,
1202 stellaris_board_info *board)
1204 static const int uart_irq[] = {5, 6, 33, 34};
1205 static const int timer_irq[] = {19, 21, 23, 35};
1206 static const uint32_t gpio_addr[7] =
1207 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1208 0x40024000, 0x40025000, 0x40026000};
1209 static const int gpio_irq[7] = {0, 1, 2, 3, 4, 30, 31};
1211 MemoryRegion *address_space_mem = get_system_memory();
1213 DeviceState *gpio_dev[7];
1214 qemu_irq gpio_in[7][8];
1215 qemu_irq gpio_out[7][8];
1224 flash_size = ((board->dc0 & 0xffff) + 1) << 1;
1225 sram_size = (board->dc0 >> 18) + 1;
1226 pic = armv7m_init(address_space_mem,
1227 flash_size, sram_size, kernel_filename, cpu_model);
1229 if (board->dc1 & (1 << 16)) {
1230 dev = sysbus_create_varargs(TYPE_STELLARIS_ADC, 0x40038000,
1231 pic[14], pic[15], pic[16], pic[17], NULL);
1232 adc = qdev_get_gpio_in(dev, 0);
1236 for (i = 0; i < 4; i++) {
1237 if (board->dc2 & (0x10000 << i)) {
1238 dev = sysbus_create_simple(TYPE_STELLARIS_GPTM,
1239 0x40030000 + i * 0x1000,
1241 /* TODO: This is incorrect, but we get away with it because
1242 the ADC output is only ever pulsed. */
1243 qdev_connect_gpio_out(dev, 0, adc);
1247 stellaris_sys_init(0x400fe000, pic[28], board, nd_table[0].macaddr.a);
1249 for (i = 0; i < 7; i++) {
1250 if (board->dc4 & (1 << i)) {
1251 gpio_dev[i] = sysbus_create_simple("pl061_luminary", gpio_addr[i],
1253 for (j = 0; j < 8; j++) {
1254 gpio_in[i][j] = qdev_get_gpio_in(gpio_dev[i], j);
1255 gpio_out[i][j] = NULL;
1260 if (board->dc2 & (1 << 12)) {
1261 dev = sysbus_create_simple(TYPE_STELLARIS_I2C, 0x40020000, pic[8]);
1262 i2c = (I2CBus *)qdev_get_child_bus(dev, "i2c");
1263 if (board->peripherals & BP_OLED_I2C) {
1264 i2c_create_slave(i2c, "ssd0303", 0x3d);
1268 for (i = 0; i < 4; i++) {
1269 if (board->dc2 & (1 << i)) {
1270 sysbus_create_simple("pl011_luminary", 0x4000c000 + i * 0x1000,
1274 if (board->dc2 & (1 << 4)) {
1275 dev = sysbus_create_simple("pl022", 0x40008000, pic[7]);
1276 if (board->peripherals & BP_OLED_SSI) {
1279 DeviceState *ssddev;
1281 /* Some boards have both an OLED controller and SD card connected to
1282 * the same SSI port, with the SD card chip select connected to a
1283 * GPIO pin. Technically the OLED chip select is connected to the
1284 * SSI Fss pin. We do not bother emulating that as both devices
1285 * should never be selected simultaneously, and our OLED controller
1286 * ignores stray 0xff commands that occur when deselecting the SD
1289 bus = qdev_get_child_bus(dev, "ssi");
1291 sddev = ssi_create_slave(bus, "ssi-sd");
1292 ssddev = ssi_create_slave(bus, "ssd0323");
1293 gpio_out[GPIO_D][0] = qemu_irq_split(
1294 qdev_get_gpio_in_named(sddev, SSI_GPIO_CS, 0),
1295 qdev_get_gpio_in_named(ssddev, SSI_GPIO_CS, 0));
1296 gpio_out[GPIO_C][7] = qdev_get_gpio_in(ssddev, 0);
1298 /* Make sure the select pin is high. */
1299 qemu_irq_raise(gpio_out[GPIO_D][0]);
1302 if (board->dc4 & (1 << 28)) {
1305 qemu_check_nic_model(&nd_table[0], "stellaris");
1307 enet = qdev_create(NULL, "stellaris_enet");
1308 qdev_set_nic_properties(enet, &nd_table[0]);
1309 qdev_init_nofail(enet);
1310 sysbus_mmio_map(SYS_BUS_DEVICE(enet), 0, 0x40048000);
1311 sysbus_connect_irq(SYS_BUS_DEVICE(enet), 0, pic[42]);
1313 if (board->peripherals & BP_GAMEPAD) {
1314 qemu_irq gpad_irq[5];
1315 static const int gpad_keycode[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1317 gpad_irq[0] = qemu_irq_invert(gpio_in[GPIO_E][0]); /* up */
1318 gpad_irq[1] = qemu_irq_invert(gpio_in[GPIO_E][1]); /* down */
1319 gpad_irq[2] = qemu_irq_invert(gpio_in[GPIO_E][2]); /* left */
1320 gpad_irq[3] = qemu_irq_invert(gpio_in[GPIO_E][3]); /* right */
1321 gpad_irq[4] = qemu_irq_invert(gpio_in[GPIO_F][1]); /* select */
1323 stellaris_gamepad_init(5, gpad_irq, gpad_keycode);
1325 for (i = 0; i < 7; i++) {
1326 if (board->dc4 & (1 << i)) {
1327 for (j = 0; j < 8; j++) {
1328 if (gpio_out[i][j]) {
1329 qdev_connect_gpio_out(gpio_dev[i], j, gpio_out[i][j]);
1336 /* FIXME: Figure out how to generate these from stellaris_boards. */
1337 static void lm3s811evb_init(MachineState *machine)
1339 const char *cpu_model = machine->cpu_model;
1340 const char *kernel_filename = machine->kernel_filename;
1341 stellaris_init(kernel_filename, cpu_model, &stellaris_boards[0]);
1344 static void lm3s6965evb_init(MachineState *machine)
1346 const char *cpu_model = machine->cpu_model;
1347 const char *kernel_filename = machine->kernel_filename;
1348 stellaris_init(kernel_filename, cpu_model, &stellaris_boards[1]);
1351 static QEMUMachine lm3s811evb_machine = {
1352 .name = "lm3s811evb",
1353 .desc = "Stellaris LM3S811EVB",
1354 .init = lm3s811evb_init,
1357 static QEMUMachine lm3s6965evb_machine = {
1358 .name = "lm3s6965evb",
1359 .desc = "Stellaris LM3S6965EVB",
1360 .init = lm3s6965evb_init,
1363 static void stellaris_machine_init(void)
1365 qemu_register_machine(&lm3s811evb_machine);
1366 qemu_register_machine(&lm3s6965evb_machine);
1369 machine_init(stellaris_machine_init);
1371 static void stellaris_i2c_class_init(ObjectClass *klass, void *data)
1373 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
1375 sdc->init = stellaris_i2c_init;
1378 static const TypeInfo stellaris_i2c_info = {
1379 .name = TYPE_STELLARIS_I2C,
1380 .parent = TYPE_SYS_BUS_DEVICE,
1381 .instance_size = sizeof(stellaris_i2c_state),
1382 .class_init = stellaris_i2c_class_init,
1385 static void stellaris_gptm_class_init(ObjectClass *klass, void *data)
1387 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
1389 sdc->init = stellaris_gptm_init;
1392 static const TypeInfo stellaris_gptm_info = {
1393 .name = TYPE_STELLARIS_GPTM,
1394 .parent = TYPE_SYS_BUS_DEVICE,
1395 .instance_size = sizeof(gptm_state),
1396 .class_init = stellaris_gptm_class_init,
1399 static void stellaris_adc_class_init(ObjectClass *klass, void *data)
1401 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
1403 sdc->init = stellaris_adc_init;
1406 static const TypeInfo stellaris_adc_info = {
1407 .name = TYPE_STELLARIS_ADC,
1408 .parent = TYPE_SYS_BUS_DEVICE,
1409 .instance_size = sizeof(stellaris_adc_state),
1410 .class_init = stellaris_adc_class_init,
1413 static void stellaris_register_types(void)
1415 type_register_static(&stellaris_i2c_info);
1416 type_register_static(&stellaris_gptm_info);
1417 type_register_static(&stellaris_adc_info);
1420 type_init(stellaris_register_types)