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/boot.h"
15 #include "qemu/timer.h"
16 #include "hw/i2c/i2c.h"
18 #include "hw/boards.h"
20 #include "exec/address-spaces.h"
21 #include "sysemu/runstate.h"
22 #include "sysemu/sysemu.h"
23 #include "hw/arm/armv7m.h"
24 #include "hw/char/pl011.h"
25 #include "hw/input/gamepad.h"
27 #include "hw/watchdog/cmsdk-apb-watchdog.h"
28 #include "migration/vmstate.h"
29 #include "hw/misc/unimp.h"
40 #define BP_OLED_I2C 0x01
41 #define BP_OLED_SSI 0x02
42 #define BP_GAMEPAD 0x04
44 #define NUM_IRQ_LINES 64
46 typedef const struct {
56 } stellaris_board_info
;
58 /* General purpose timer module. */
60 #define TYPE_STELLARIS_GPTM "stellaris-gptm"
61 #define STELLARIS_GPTM(obj) \
62 OBJECT_CHECK(gptm_state, (obj), TYPE_STELLARIS_GPTM)
64 typedef struct gptm_state
{
65 SysBusDevice parent_obj
;
76 uint32_t match_prescale
[2];
79 struct gptm_state
*opaque
[2];
81 /* The timers have an alternate output used to trigger the ADC. */
86 static void gptm_update_irq(gptm_state
*s
)
89 level
= (s
->state
& s
->mask
) != 0;
90 qemu_set_irq(s
->irq
, level
);
93 static void gptm_stop(gptm_state
*s
, int n
)
95 timer_del(s
->timer
[n
]);
98 static void gptm_reload(gptm_state
*s
, int n
, int reset
)
102 tick
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
106 if (s
->config
== 0) {
107 /* 32-bit CountDown. */
109 count
= s
->load
[0] | (s
->load
[1] << 16);
110 tick
+= (int64_t)count
* system_clock_scale
;
111 } else if (s
->config
== 1) {
112 /* 32-bit RTC. 1Hz tick. */
113 tick
+= NANOSECONDS_PER_SECOND
;
114 } else if (s
->mode
[n
] == 0xa) {
115 /* PWM mode. Not implemented. */
117 qemu_log_mask(LOG_UNIMP
,
118 "GPTM: 16-bit timer mode unimplemented: 0x%x\n",
123 timer_mod(s
->timer
[n
], tick
);
126 static void gptm_tick(void *opaque
)
128 gptm_state
**p
= (gptm_state
**)opaque
;
134 if (s
->config
== 0) {
136 if ((s
->control
& 0x20)) {
137 /* Output trigger. */
138 qemu_irq_pulse(s
->trigger
);
140 if (s
->mode
[0] & 1) {
145 gptm_reload(s
, 0, 0);
147 } else if (s
->config
== 1) {
151 match
= s
->match
[0] | (s
->match
[1] << 16);
157 gptm_reload(s
, 0, 0);
158 } else if (s
->mode
[n
] == 0xa) {
159 /* PWM mode. Not implemented. */
161 qemu_log_mask(LOG_UNIMP
,
162 "GPTM: 16-bit timer mode unimplemented: 0x%x\n",
168 static uint64_t gptm_read(void *opaque
, hwaddr offset
,
171 gptm_state
*s
= (gptm_state
*)opaque
;
176 case 0x04: /* TAMR */
178 case 0x08: /* TBMR */
187 return s
->state
& s
->mask
;
190 case 0x28: /* TAILR */
191 return s
->load
[0] | ((s
->config
< 4) ? (s
->load
[1] << 16) : 0);
192 case 0x2c: /* TBILR */
194 case 0x30: /* TAMARCHR */
195 return s
->match
[0] | ((s
->config
< 4) ? (s
->match
[1] << 16) : 0);
196 case 0x34: /* TBMATCHR */
198 case 0x38: /* TAPR */
199 return s
->prescale
[0];
200 case 0x3c: /* TBPR */
201 return s
->prescale
[1];
202 case 0x40: /* TAPMR */
203 return s
->match_prescale
[0];
204 case 0x44: /* TBPMR */
205 return s
->match_prescale
[1];
207 if (s
->config
== 1) {
210 qemu_log_mask(LOG_UNIMP
,
211 "GPTM: read of TAR but timer read not supported\n");
214 qemu_log_mask(LOG_UNIMP
,
215 "GPTM: read of TBR but timer read not supported\n");
218 qemu_log_mask(LOG_GUEST_ERROR
,
219 "GPTM: read at bad offset 0x02%" HWADDR_PRIx
"\n",
225 static void gptm_write(void *opaque
, hwaddr offset
,
226 uint64_t value
, unsigned size
)
228 gptm_state
*s
= (gptm_state
*)opaque
;
231 /* The timers should be disabled before changing the configuration.
232 We take advantage of this and defer everything until the timer
238 case 0x04: /* TAMR */
241 case 0x08: /* TBMR */
247 /* TODO: Implement pause. */
248 if ((oldval
^ value
) & 1) {
250 gptm_reload(s
, 0, 1);
255 if (((oldval
^ value
) & 0x100) && s
->config
>= 4) {
257 gptm_reload(s
, 1, 1);
264 s
->mask
= value
& 0x77;
270 case 0x28: /* TAILR */
271 s
->load
[0] = value
& 0xffff;
273 s
->load
[1] = value
>> 16;
276 case 0x2c: /* TBILR */
277 s
->load
[1] = value
& 0xffff;
279 case 0x30: /* TAMARCHR */
280 s
->match
[0] = value
& 0xffff;
282 s
->match
[1] = value
>> 16;
285 case 0x34: /* TBMATCHR */
286 s
->match
[1] = value
>> 16;
288 case 0x38: /* TAPR */
289 s
->prescale
[0] = value
;
291 case 0x3c: /* TBPR */
292 s
->prescale
[1] = value
;
294 case 0x40: /* TAPMR */
295 s
->match_prescale
[0] = value
;
297 case 0x44: /* TBPMR */
298 s
->match_prescale
[0] = value
;
301 qemu_log_mask(LOG_GUEST_ERROR
,
302 "GPTM: write at bad offset 0x02%" HWADDR_PRIx
"\n",
308 static const MemoryRegionOps gptm_ops
= {
311 .endianness
= DEVICE_NATIVE_ENDIAN
,
314 static const VMStateDescription vmstate_stellaris_gptm
= {
315 .name
= "stellaris_gptm",
317 .minimum_version_id
= 1,
318 .fields
= (VMStateField
[]) {
319 VMSTATE_UINT32(config
, gptm_state
),
320 VMSTATE_UINT32_ARRAY(mode
, gptm_state
, 2),
321 VMSTATE_UINT32(control
, gptm_state
),
322 VMSTATE_UINT32(state
, gptm_state
),
323 VMSTATE_UINT32(mask
, gptm_state
),
325 VMSTATE_UINT32_ARRAY(load
, gptm_state
, 2),
326 VMSTATE_UINT32_ARRAY(match
, gptm_state
, 2),
327 VMSTATE_UINT32_ARRAY(prescale
, gptm_state
, 2),
328 VMSTATE_UINT32_ARRAY(match_prescale
, gptm_state
, 2),
329 VMSTATE_UINT32(rtc
, gptm_state
),
330 VMSTATE_INT64_ARRAY(tick
, gptm_state
, 2),
331 VMSTATE_TIMER_PTR_ARRAY(timer
, gptm_state
, 2),
332 VMSTATE_END_OF_LIST()
336 static void stellaris_gptm_init(Object
*obj
)
338 DeviceState
*dev
= DEVICE(obj
);
339 gptm_state
*s
= STELLARIS_GPTM(obj
);
340 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
342 sysbus_init_irq(sbd
, &s
->irq
);
343 qdev_init_gpio_out(dev
, &s
->trigger
, 1);
345 memory_region_init_io(&s
->iomem
, obj
, &gptm_ops
, s
,
347 sysbus_init_mmio(sbd
, &s
->iomem
);
349 s
->opaque
[0] = s
->opaque
[1] = s
;
352 static void stellaris_gptm_realize(DeviceState
*dev
, Error
**errp
)
354 gptm_state
*s
= STELLARIS_GPTM(dev
);
355 s
->timer
[0] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[0]);
356 s
->timer
[1] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[1]);
359 /* System controller. */
378 stellaris_board_info
*board
;
381 static void ssys_update(ssys_state
*s
)
383 qemu_set_irq(s
->irq
, (s
->int_status
& s
->int_mask
) != 0);
386 static uint32_t pllcfg_sandstorm
[16] = {
388 0x1ae0, /* 1.8432 Mhz */
390 0xd573, /* 2.4576 Mhz */
391 0x37a6, /* 3.57954 Mhz */
392 0x1ae2, /* 3.6864 Mhz */
394 0x98bc, /* 4.906 Mhz */
395 0x935b, /* 4.9152 Mhz */
397 0x4dee, /* 5.12 Mhz */
399 0x75db, /* 6.144 Mhz */
400 0x1ae6, /* 7.3728 Mhz */
402 0x585b /* 8.192 Mhz */
405 static uint32_t pllcfg_fury
[16] = {
407 0x1b20, /* 1.8432 Mhz */
409 0xf42b, /* 2.4576 Mhz */
410 0x37e3, /* 3.57954 Mhz */
411 0x1b21, /* 3.6864 Mhz */
413 0x98ee, /* 4.906 Mhz */
414 0xd5b4, /* 4.9152 Mhz */
416 0x4e27, /* 5.12 Mhz */
418 0xec1c, /* 6.144 Mhz */
419 0x1b23, /* 7.3728 Mhz */
421 0xb11c /* 8.192 Mhz */
424 #define DID0_VER_MASK 0x70000000
425 #define DID0_VER_0 0x00000000
426 #define DID0_VER_1 0x10000000
428 #define DID0_CLASS_MASK 0x00FF0000
429 #define DID0_CLASS_SANDSTORM 0x00000000
430 #define DID0_CLASS_FURY 0x00010000
432 static int ssys_board_class(const ssys_state
*s
)
434 uint32_t did0
= s
->board
->did0
;
435 switch (did0
& DID0_VER_MASK
) {
437 return DID0_CLASS_SANDSTORM
;
439 switch (did0
& DID0_CLASS_MASK
) {
440 case DID0_CLASS_SANDSTORM
:
441 case DID0_CLASS_FURY
:
442 return did0
& DID0_CLASS_MASK
;
444 /* for unknown classes, fall through */
446 /* This can only happen if the hardwired constant did0 value
447 * in this board's stellaris_board_info struct is wrong.
449 g_assert_not_reached();
453 static uint64_t ssys_read(void *opaque
, hwaddr offset
,
456 ssys_state
*s
= (ssys_state
*)opaque
;
459 case 0x000: /* DID0 */
460 return s
->board
->did0
;
461 case 0x004: /* DID1 */
462 return s
->board
->did1
;
463 case 0x008: /* DC0 */
464 return s
->board
->dc0
;
465 case 0x010: /* DC1 */
466 return s
->board
->dc1
;
467 case 0x014: /* DC2 */
468 return s
->board
->dc2
;
469 case 0x018: /* DC3 */
470 return s
->board
->dc3
;
471 case 0x01c: /* DC4 */
472 return s
->board
->dc4
;
473 case 0x030: /* PBORCTL */
475 case 0x034: /* LDOPCTL */
477 case 0x040: /* SRCR0 */
479 case 0x044: /* SRCR1 */
481 case 0x048: /* SRCR2 */
483 case 0x050: /* RIS */
484 return s
->int_status
;
485 case 0x054: /* IMC */
487 case 0x058: /* MISC */
488 return s
->int_status
& s
->int_mask
;
489 case 0x05c: /* RESC */
491 case 0x060: /* RCC */
493 case 0x064: /* PLLCFG */
496 xtal
= (s
->rcc
>> 6) & 0xf;
497 switch (ssys_board_class(s
)) {
498 case DID0_CLASS_FURY
:
499 return pllcfg_fury
[xtal
];
500 case DID0_CLASS_SANDSTORM
:
501 return pllcfg_sandstorm
[xtal
];
503 g_assert_not_reached();
506 case 0x070: /* RCC2 */
508 case 0x100: /* RCGC0 */
510 case 0x104: /* RCGC1 */
512 case 0x108: /* RCGC2 */
514 case 0x110: /* SCGC0 */
516 case 0x114: /* SCGC1 */
518 case 0x118: /* SCGC2 */
520 case 0x120: /* DCGC0 */
522 case 0x124: /* DCGC1 */
524 case 0x128: /* DCGC2 */
526 case 0x150: /* CLKVCLR */
528 case 0x160: /* LDOARST */
530 case 0x1e0: /* USER0 */
532 case 0x1e4: /* USER1 */
535 qemu_log_mask(LOG_GUEST_ERROR
,
536 "SSYS: read at bad offset 0x%x\n", (int)offset
);
541 static bool ssys_use_rcc2(ssys_state
*s
)
543 return (s
->rcc2
>> 31) & 0x1;
547 * Caculate the sys. clock period in ms.
549 static void ssys_calculate_system_clock(ssys_state
*s
)
551 if (ssys_use_rcc2(s
)) {
552 system_clock_scale
= 5 * (((s
->rcc2
>> 23) & 0x3f) + 1);
554 system_clock_scale
= 5 * (((s
->rcc
>> 23) & 0xf) + 1);
558 static void ssys_write(void *opaque
, hwaddr offset
,
559 uint64_t value
, unsigned size
)
561 ssys_state
*s
= (ssys_state
*)opaque
;
564 case 0x030: /* PBORCTL */
565 s
->pborctl
= value
& 0xffff;
567 case 0x034: /* LDOPCTL */
568 s
->ldopctl
= value
& 0x1f;
570 case 0x040: /* SRCR0 */
571 case 0x044: /* SRCR1 */
572 case 0x048: /* SRCR2 */
573 qemu_log_mask(LOG_UNIMP
, "Peripheral reset not implemented\n");
575 case 0x054: /* IMC */
576 s
->int_mask
= value
& 0x7f;
578 case 0x058: /* MISC */
579 s
->int_status
&= ~value
;
581 case 0x05c: /* RESC */
582 s
->resc
= value
& 0x3f;
584 case 0x060: /* RCC */
585 if ((s
->rcc
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
587 s
->int_status
|= (1 << 6);
590 ssys_calculate_system_clock(s
);
592 case 0x070: /* RCC2 */
593 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
597 if ((s
->rcc2
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
599 s
->int_status
|= (1 << 6);
602 ssys_calculate_system_clock(s
);
604 case 0x100: /* RCGC0 */
607 case 0x104: /* RCGC1 */
610 case 0x108: /* RCGC2 */
613 case 0x110: /* SCGC0 */
616 case 0x114: /* SCGC1 */
619 case 0x118: /* SCGC2 */
622 case 0x120: /* DCGC0 */
625 case 0x124: /* DCGC1 */
628 case 0x128: /* DCGC2 */
631 case 0x150: /* CLKVCLR */
634 case 0x160: /* LDOARST */
638 qemu_log_mask(LOG_GUEST_ERROR
,
639 "SSYS: write at bad offset 0x%x\n", (int)offset
);
644 static const MemoryRegionOps ssys_ops
= {
647 .endianness
= DEVICE_NATIVE_ENDIAN
,
650 static void ssys_reset(void *opaque
)
652 ssys_state
*s
= (ssys_state
*)opaque
;
657 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
660 s
->rcc2
= 0x07802810;
665 ssys_calculate_system_clock(s
);
668 static int stellaris_sys_post_load(void *opaque
, int version_id
)
670 ssys_state
*s
= opaque
;
672 ssys_calculate_system_clock(s
);
677 static const VMStateDescription vmstate_stellaris_sys
= {
678 .name
= "stellaris_sys",
680 .minimum_version_id
= 1,
681 .post_load
= stellaris_sys_post_load
,
682 .fields
= (VMStateField
[]) {
683 VMSTATE_UINT32(pborctl
, ssys_state
),
684 VMSTATE_UINT32(ldopctl
, ssys_state
),
685 VMSTATE_UINT32(int_mask
, ssys_state
),
686 VMSTATE_UINT32(int_status
, ssys_state
),
687 VMSTATE_UINT32(resc
, ssys_state
),
688 VMSTATE_UINT32(rcc
, ssys_state
),
689 VMSTATE_UINT32_V(rcc2
, ssys_state
, 2),
690 VMSTATE_UINT32_ARRAY(rcgc
, ssys_state
, 3),
691 VMSTATE_UINT32_ARRAY(scgc
, ssys_state
, 3),
692 VMSTATE_UINT32_ARRAY(dcgc
, ssys_state
, 3),
693 VMSTATE_UINT32(clkvclr
, ssys_state
),
694 VMSTATE_UINT32(ldoarst
, ssys_state
),
695 VMSTATE_END_OF_LIST()
699 static int stellaris_sys_init(uint32_t base
, qemu_irq irq
,
700 stellaris_board_info
* board
,
705 s
= g_new0(ssys_state
, 1);
708 /* Most devices come preprogrammed with a MAC address in the user data. */
709 s
->user0
= macaddr
[0] | (macaddr
[1] << 8) | (macaddr
[2] << 16);
710 s
->user1
= macaddr
[3] | (macaddr
[4] << 8) | (macaddr
[5] << 16);
712 memory_region_init_io(&s
->iomem
, NULL
, &ssys_ops
, s
, "ssys", 0x00001000);
713 memory_region_add_subregion(get_system_memory(), base
, &s
->iomem
);
715 vmstate_register(NULL
, VMSTATE_INSTANCE_ID_ANY
, &vmstate_stellaris_sys
, s
);
720 /* I2C controller. */
722 #define TYPE_STELLARIS_I2C "stellaris-i2c"
723 #define STELLARIS_I2C(obj) \
724 OBJECT_CHECK(stellaris_i2c_state, (obj), TYPE_STELLARIS_I2C)
727 SysBusDevice parent_obj
;
739 } stellaris_i2c_state
;
741 #define STELLARIS_I2C_MCS_BUSY 0x01
742 #define STELLARIS_I2C_MCS_ERROR 0x02
743 #define STELLARIS_I2C_MCS_ADRACK 0x04
744 #define STELLARIS_I2C_MCS_DATACK 0x08
745 #define STELLARIS_I2C_MCS_ARBLST 0x10
746 #define STELLARIS_I2C_MCS_IDLE 0x20
747 #define STELLARIS_I2C_MCS_BUSBSY 0x40
749 static uint64_t stellaris_i2c_read(void *opaque
, hwaddr offset
,
752 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
758 /* We don't emulate timing, so the controller is never busy. */
759 return s
->mcs
| STELLARIS_I2C_MCS_IDLE
;
762 case 0x0c: /* MTPR */
764 case 0x10: /* MIMR */
766 case 0x14: /* MRIS */
768 case 0x18: /* MMIS */
769 return s
->mris
& s
->mimr
;
773 qemu_log_mask(LOG_GUEST_ERROR
,
774 "stellaris_i2c: read at bad offset 0x%x\n", (int)offset
);
779 static void stellaris_i2c_update(stellaris_i2c_state
*s
)
783 level
= (s
->mris
& s
->mimr
) != 0;
784 qemu_set_irq(s
->irq
, level
);
787 static void stellaris_i2c_write(void *opaque
, hwaddr offset
,
788 uint64_t value
, unsigned size
)
790 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
794 s
->msa
= value
& 0xff;
797 if ((s
->mcr
& 0x10) == 0) {
798 /* Disabled. Do nothing. */
801 /* Grab the bus if this is starting a transfer. */
802 if ((value
& 2) && (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
803 if (i2c_start_transfer(s
->bus
, s
->msa
>> 1, s
->msa
& 1)) {
804 s
->mcs
|= STELLARIS_I2C_MCS_ARBLST
;
806 s
->mcs
&= ~STELLARIS_I2C_MCS_ARBLST
;
807 s
->mcs
|= STELLARIS_I2C_MCS_BUSBSY
;
810 /* If we don't have the bus then indicate an error. */
811 if (!i2c_bus_busy(s
->bus
)
812 || (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
813 s
->mcs
|= STELLARIS_I2C_MCS_ERROR
;
816 s
->mcs
&= ~STELLARIS_I2C_MCS_ERROR
;
818 /* Transfer a byte. */
819 /* TODO: Handle errors. */
822 s
->mdr
= i2c_recv(s
->bus
);
825 i2c_send(s
->bus
, s
->mdr
);
827 /* Raise an interrupt. */
831 /* Finish transfer. */
832 i2c_end_transfer(s
->bus
);
833 s
->mcs
&= ~STELLARIS_I2C_MCS_BUSBSY
;
837 s
->mdr
= value
& 0xff;
839 case 0x0c: /* MTPR */
840 s
->mtpr
= value
& 0xff;
842 case 0x10: /* MIMR */
845 case 0x1c: /* MICR */
850 qemu_log_mask(LOG_UNIMP
,
851 "stellaris_i2c: Loopback not implemented\n");
854 qemu_log_mask(LOG_UNIMP
,
855 "stellaris_i2c: Slave mode not implemented\n");
857 s
->mcr
= value
& 0x31;
860 qemu_log_mask(LOG_GUEST_ERROR
,
861 "stellaris_i2c: write at bad offset 0x%x\n", (int)offset
);
863 stellaris_i2c_update(s
);
866 static void stellaris_i2c_reset(stellaris_i2c_state
*s
)
868 if (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
)
869 i2c_end_transfer(s
->bus
);
878 stellaris_i2c_update(s
);
881 static const MemoryRegionOps stellaris_i2c_ops
= {
882 .read
= stellaris_i2c_read
,
883 .write
= stellaris_i2c_write
,
884 .endianness
= DEVICE_NATIVE_ENDIAN
,
887 static const VMStateDescription vmstate_stellaris_i2c
= {
888 .name
= "stellaris_i2c",
890 .minimum_version_id
= 1,
891 .fields
= (VMStateField
[]) {
892 VMSTATE_UINT32(msa
, stellaris_i2c_state
),
893 VMSTATE_UINT32(mcs
, stellaris_i2c_state
),
894 VMSTATE_UINT32(mdr
, stellaris_i2c_state
),
895 VMSTATE_UINT32(mtpr
, stellaris_i2c_state
),
896 VMSTATE_UINT32(mimr
, stellaris_i2c_state
),
897 VMSTATE_UINT32(mris
, stellaris_i2c_state
),
898 VMSTATE_UINT32(mcr
, stellaris_i2c_state
),
899 VMSTATE_END_OF_LIST()
903 static void stellaris_i2c_init(Object
*obj
)
905 DeviceState
*dev
= DEVICE(obj
);
906 stellaris_i2c_state
*s
= STELLARIS_I2C(obj
);
907 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
910 sysbus_init_irq(sbd
, &s
->irq
);
911 bus
= i2c_init_bus(dev
, "i2c");
914 memory_region_init_io(&s
->iomem
, obj
, &stellaris_i2c_ops
, s
,
916 sysbus_init_mmio(sbd
, &s
->iomem
);
917 /* ??? For now we only implement the master interface. */
918 stellaris_i2c_reset(s
);
921 /* Analogue to Digital Converter. This is only partially implemented,
922 enough for applications that use a combined ADC and timer tick. */
924 #define STELLARIS_ADC_EM_CONTROLLER 0
925 #define STELLARIS_ADC_EM_COMP 1
926 #define STELLARIS_ADC_EM_EXTERNAL 4
927 #define STELLARIS_ADC_EM_TIMER 5
928 #define STELLARIS_ADC_EM_PWM0 6
929 #define STELLARIS_ADC_EM_PWM1 7
930 #define STELLARIS_ADC_EM_PWM2 8
932 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
933 #define STELLARIS_ADC_FIFO_FULL 0x1000
935 #define TYPE_STELLARIS_ADC "stellaris-adc"
936 #define STELLARIS_ADC(obj) \
937 OBJECT_CHECK(stellaris_adc_state, (obj), TYPE_STELLARIS_ADC)
939 typedef struct StellarisADCState
{
940 SysBusDevice parent_obj
;
959 } stellaris_adc_state
;
961 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state
*s
, int n
)
965 tail
= s
->fifo
[n
].state
& 0xf;
966 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_EMPTY
) {
969 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf) | ((tail
+ 1) & 0xf);
970 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_FULL
;
971 if (tail
+ 1 == ((s
->fifo
[n
].state
>> 4) & 0xf))
972 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_EMPTY
;
974 return s
->fifo
[n
].data
[tail
];
977 static void stellaris_adc_fifo_write(stellaris_adc_state
*s
, int n
,
982 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
983 FIFO fir each sequencer. */
984 head
= (s
->fifo
[n
].state
>> 4) & 0xf;
985 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_FULL
) {
989 s
->fifo
[n
].data
[head
] = value
;
990 head
= (head
+ 1) & 0xf;
991 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_EMPTY
;
992 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf0) | (head
<< 4);
993 if ((s
->fifo
[n
].state
& 0xf) == head
)
994 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_FULL
;
997 static void stellaris_adc_update(stellaris_adc_state
*s
)
1002 for (n
= 0; n
< 4; n
++) {
1003 level
= (s
->ris
& s
->im
& (1 << n
)) != 0;
1004 qemu_set_irq(s
->irq
[n
], level
);
1008 static void stellaris_adc_trigger(void *opaque
, int irq
, int level
)
1010 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1013 for (n
= 0; n
< 4; n
++) {
1014 if ((s
->actss
& (1 << n
)) == 0) {
1018 if (((s
->emux
>> (n
* 4)) & 0xff) != 5) {
1022 /* Some applications use the ADC as a random number source, so introduce
1023 some variation into the signal. */
1024 s
->noise
= s
->noise
* 314159 + 1;
1025 /* ??? actual inputs not implemented. Return an arbitrary value. */
1026 stellaris_adc_fifo_write(s
, n
, 0x200 + ((s
->noise
>> 16) & 7));
1028 stellaris_adc_update(s
);
1032 static void stellaris_adc_reset(stellaris_adc_state
*s
)
1036 for (n
= 0; n
< 4; n
++) {
1039 s
->fifo
[n
].state
= STELLARIS_ADC_FIFO_EMPTY
;
1043 static uint64_t stellaris_adc_read(void *opaque
, hwaddr offset
,
1046 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1048 /* TODO: Implement this. */
1049 if (offset
>= 0x40 && offset
< 0xc0) {
1051 n
= (offset
- 0x40) >> 5;
1052 switch (offset
& 0x1f) {
1053 case 0x00: /* SSMUX */
1055 case 0x04: /* SSCTL */
1057 case 0x08: /* SSFIFO */
1058 return stellaris_adc_fifo_read(s
, n
);
1059 case 0x0c: /* SSFSTAT */
1060 return s
->fifo
[n
].state
;
1066 case 0x00: /* ACTSS */
1068 case 0x04: /* RIS */
1072 case 0x0c: /* ISC */
1073 return s
->ris
& s
->im
;
1074 case 0x10: /* OSTAT */
1076 case 0x14: /* EMUX */
1078 case 0x18: /* USTAT */
1080 case 0x20: /* SSPRI */
1082 case 0x30: /* SAC */
1085 qemu_log_mask(LOG_GUEST_ERROR
,
1086 "stellaris_adc: read at bad offset 0x%x\n", (int)offset
);
1091 static void stellaris_adc_write(void *opaque
, hwaddr offset
,
1092 uint64_t value
, unsigned size
)
1094 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1096 /* TODO: Implement this. */
1097 if (offset
>= 0x40 && offset
< 0xc0) {
1099 n
= (offset
- 0x40) >> 5;
1100 switch (offset
& 0x1f) {
1101 case 0x00: /* SSMUX */
1102 s
->ssmux
[n
] = value
& 0x33333333;
1104 case 0x04: /* SSCTL */
1106 qemu_log_mask(LOG_UNIMP
,
1107 "ADC: Unimplemented sequence %" PRIx64
"\n",
1110 s
->ssctl
[n
] = value
;
1117 case 0x00: /* ACTSS */
1118 s
->actss
= value
& 0xf;
1123 case 0x0c: /* ISC */
1126 case 0x10: /* OSTAT */
1129 case 0x14: /* EMUX */
1132 case 0x18: /* USTAT */
1135 case 0x20: /* SSPRI */
1138 case 0x28: /* PSSI */
1139 qemu_log_mask(LOG_UNIMP
, "ADC: sample initiate unimplemented\n");
1141 case 0x30: /* SAC */
1145 qemu_log_mask(LOG_GUEST_ERROR
,
1146 "stellaris_adc: write at bad offset 0x%x\n", (int)offset
);
1148 stellaris_adc_update(s
);
1151 static const MemoryRegionOps stellaris_adc_ops
= {
1152 .read
= stellaris_adc_read
,
1153 .write
= stellaris_adc_write
,
1154 .endianness
= DEVICE_NATIVE_ENDIAN
,
1157 static const VMStateDescription vmstate_stellaris_adc
= {
1158 .name
= "stellaris_adc",
1160 .minimum_version_id
= 1,
1161 .fields
= (VMStateField
[]) {
1162 VMSTATE_UINT32(actss
, stellaris_adc_state
),
1163 VMSTATE_UINT32(ris
, stellaris_adc_state
),
1164 VMSTATE_UINT32(im
, stellaris_adc_state
),
1165 VMSTATE_UINT32(emux
, stellaris_adc_state
),
1166 VMSTATE_UINT32(ostat
, stellaris_adc_state
),
1167 VMSTATE_UINT32(ustat
, stellaris_adc_state
),
1168 VMSTATE_UINT32(sspri
, stellaris_adc_state
),
1169 VMSTATE_UINT32(sac
, stellaris_adc_state
),
1170 VMSTATE_UINT32(fifo
[0].state
, stellaris_adc_state
),
1171 VMSTATE_UINT32_ARRAY(fifo
[0].data
, stellaris_adc_state
, 16),
1172 VMSTATE_UINT32(ssmux
[0], stellaris_adc_state
),
1173 VMSTATE_UINT32(ssctl
[0], stellaris_adc_state
),
1174 VMSTATE_UINT32(fifo
[1].state
, stellaris_adc_state
),
1175 VMSTATE_UINT32_ARRAY(fifo
[1].data
, stellaris_adc_state
, 16),
1176 VMSTATE_UINT32(ssmux
[1], stellaris_adc_state
),
1177 VMSTATE_UINT32(ssctl
[1], stellaris_adc_state
),
1178 VMSTATE_UINT32(fifo
[2].state
, stellaris_adc_state
),
1179 VMSTATE_UINT32_ARRAY(fifo
[2].data
, stellaris_adc_state
, 16),
1180 VMSTATE_UINT32(ssmux
[2], stellaris_adc_state
),
1181 VMSTATE_UINT32(ssctl
[2], stellaris_adc_state
),
1182 VMSTATE_UINT32(fifo
[3].state
, stellaris_adc_state
),
1183 VMSTATE_UINT32_ARRAY(fifo
[3].data
, stellaris_adc_state
, 16),
1184 VMSTATE_UINT32(ssmux
[3], stellaris_adc_state
),
1185 VMSTATE_UINT32(ssctl
[3], stellaris_adc_state
),
1186 VMSTATE_UINT32(noise
, stellaris_adc_state
),
1187 VMSTATE_END_OF_LIST()
1191 static void stellaris_adc_init(Object
*obj
)
1193 DeviceState
*dev
= DEVICE(obj
);
1194 stellaris_adc_state
*s
= STELLARIS_ADC(obj
);
1195 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
1198 for (n
= 0; n
< 4; n
++) {
1199 sysbus_init_irq(sbd
, &s
->irq
[n
]);
1202 memory_region_init_io(&s
->iomem
, obj
, &stellaris_adc_ops
, s
,
1204 sysbus_init_mmio(sbd
, &s
->iomem
);
1205 stellaris_adc_reset(s
);
1206 qdev_init_gpio_in(dev
, stellaris_adc_trigger
, 1);
1210 void do_sys_reset(void *opaque
, int n
, int level
)
1213 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
1218 static stellaris_board_info stellaris_boards
[] = {
1222 0x001f001f, /* dc0 */
1232 0x00ff007f, /* dc0 */
1237 BP_OLED_SSI
| BP_GAMEPAD
1241 static void stellaris_init(MachineState
*ms
, stellaris_board_info
*board
)
1243 static const int uart_irq
[] = {5, 6, 33, 34};
1244 static const int timer_irq
[] = {19, 21, 23, 35};
1245 static const uint32_t gpio_addr
[7] =
1246 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1247 0x40024000, 0x40025000, 0x40026000};
1248 static const int gpio_irq
[7] = {0, 1, 2, 3, 4, 30, 31};
1250 /* Memory map of SoC devices, from
1251 * Stellaris LM3S6965 Microcontroller Data Sheet (rev I)
1252 * http://www.ti.com/lit/ds/symlink/lm3s6965.pdf
1255 * 40002000 i2c (unimplemented)
1265 * 40021000 i2c (unimplemented)
1269 * 40028000 PWM (unimplemented)
1270 * 4002c000 QEI (unimplemented)
1271 * 4002d000 QEI (unimplemented)
1277 * 4003c000 analogue comparator (unimplemented)
1279 * 400fc000 hibernation module (unimplemented)
1280 * 400fd000 flash memory control (unimplemented)
1281 * 400fe000 system control
1284 DeviceState
*gpio_dev
[7], *nvic
;
1285 qemu_irq gpio_in
[7][8];
1286 qemu_irq gpio_out
[7][8];
1295 MemoryRegion
*sram
= g_new(MemoryRegion
, 1);
1296 MemoryRegion
*flash
= g_new(MemoryRegion
, 1);
1297 MemoryRegion
*system_memory
= get_system_memory();
1299 flash_size
= (((board
->dc0
& 0xffff) + 1) << 1) * 1024;
1300 sram_size
= ((board
->dc0
>> 18) + 1) * 1024;
1302 /* Flash programming is done via the SCU, so pretend it is ROM. */
1303 memory_region_init_rom(flash
, NULL
, "stellaris.flash", flash_size
,
1305 memory_region_add_subregion(system_memory
, 0, flash
);
1307 memory_region_init_ram(sram
, NULL
, "stellaris.sram", sram_size
,
1309 memory_region_add_subregion(system_memory
, 0x20000000, sram
);
1311 nvic
= qdev_new(TYPE_ARMV7M
);
1312 qdev_prop_set_uint32(nvic
, "num-irq", NUM_IRQ_LINES
);
1313 qdev_prop_set_string(nvic
, "cpu-type", ms
->cpu_type
);
1314 qdev_prop_set_bit(nvic
, "enable-bitband", true);
1315 object_property_set_link(OBJECT(nvic
), "memory",
1316 OBJECT(get_system_memory()), &error_abort
);
1317 /* This will exit with an error if the user passed us a bad cpu_type */
1318 sysbus_realize_and_unref(SYS_BUS_DEVICE(nvic
), &error_fatal
);
1320 qdev_connect_gpio_out_named(nvic
, "SYSRESETREQ", 0,
1321 qemu_allocate_irq(&do_sys_reset
, NULL
, 0));
1323 if (board
->dc1
& (1 << 16)) {
1324 dev
= sysbus_create_varargs(TYPE_STELLARIS_ADC
, 0x40038000,
1325 qdev_get_gpio_in(nvic
, 14),
1326 qdev_get_gpio_in(nvic
, 15),
1327 qdev_get_gpio_in(nvic
, 16),
1328 qdev_get_gpio_in(nvic
, 17),
1330 adc
= qdev_get_gpio_in(dev
, 0);
1334 for (i
= 0; i
< 4; i
++) {
1335 if (board
->dc2
& (0x10000 << i
)) {
1336 dev
= sysbus_create_simple(TYPE_STELLARIS_GPTM
,
1337 0x40030000 + i
* 0x1000,
1338 qdev_get_gpio_in(nvic
, timer_irq
[i
]));
1339 /* TODO: This is incorrect, but we get away with it because
1340 the ADC output is only ever pulsed. */
1341 qdev_connect_gpio_out(dev
, 0, adc
);
1345 stellaris_sys_init(0x400fe000, qdev_get_gpio_in(nvic
, 28),
1346 board
, nd_table
[0].macaddr
.a
);
1349 if (board
->dc1
& (1 << 3)) { /* watchdog present */
1350 dev
= qdev_new(TYPE_LUMINARY_WATCHDOG
);
1352 /* system_clock_scale is valid now */
1353 uint32_t mainclk
= NANOSECONDS_PER_SECOND
/ system_clock_scale
;
1354 qdev_prop_set_uint32(dev
, "wdogclk-frq", mainclk
);
1356 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev
), &error_fatal
);
1357 sysbus_mmio_map(SYS_BUS_DEVICE(dev
),
1360 sysbus_connect_irq(SYS_BUS_DEVICE(dev
),
1362 qdev_get_gpio_in(nvic
, 18));
1366 for (i
= 0; i
< 7; i
++) {
1367 if (board
->dc4
& (1 << i
)) {
1368 gpio_dev
[i
] = sysbus_create_simple("pl061_luminary", gpio_addr
[i
],
1369 qdev_get_gpio_in(nvic
,
1371 for (j
= 0; j
< 8; j
++) {
1372 gpio_in
[i
][j
] = qdev_get_gpio_in(gpio_dev
[i
], j
);
1373 gpio_out
[i
][j
] = NULL
;
1378 if (board
->dc2
& (1 << 12)) {
1379 dev
= sysbus_create_simple(TYPE_STELLARIS_I2C
, 0x40020000,
1380 qdev_get_gpio_in(nvic
, 8));
1381 i2c
= (I2CBus
*)qdev_get_child_bus(dev
, "i2c");
1382 if (board
->peripherals
& BP_OLED_I2C
) {
1383 i2c_slave_create_simple(i2c
, "ssd0303", 0x3d);
1387 for (i
= 0; i
< 4; i
++) {
1388 if (board
->dc2
& (1 << i
)) {
1389 pl011_luminary_create(0x4000c000 + i
* 0x1000,
1390 qdev_get_gpio_in(nvic
, uart_irq
[i
]),
1394 if (board
->dc2
& (1 << 4)) {
1395 dev
= sysbus_create_simple("pl022", 0x40008000,
1396 qdev_get_gpio_in(nvic
, 7));
1397 if (board
->peripherals
& BP_OLED_SSI
) {
1400 DeviceState
*ssddev
;
1402 /* Some boards have both an OLED controller and SD card connected to
1403 * the same SSI port, with the SD card chip select connected to a
1404 * GPIO pin. Technically the OLED chip select is connected to the
1405 * SSI Fss pin. We do not bother emulating that as both devices
1406 * should never be selected simultaneously, and our OLED controller
1407 * ignores stray 0xff commands that occur when deselecting the SD
1410 bus
= qdev_get_child_bus(dev
, "ssi");
1412 sddev
= ssi_create_slave(bus
, "ssi-sd");
1413 ssddev
= ssi_create_slave(bus
, "ssd0323");
1414 gpio_out
[GPIO_D
][0] = qemu_irq_split(
1415 qdev_get_gpio_in_named(sddev
, SSI_GPIO_CS
, 0),
1416 qdev_get_gpio_in_named(ssddev
, SSI_GPIO_CS
, 0));
1417 gpio_out
[GPIO_C
][7] = qdev_get_gpio_in(ssddev
, 0);
1419 /* Make sure the select pin is high. */
1420 qemu_irq_raise(gpio_out
[GPIO_D
][0]);
1423 if (board
->dc4
& (1 << 28)) {
1426 qemu_check_nic_model(&nd_table
[0], "stellaris");
1428 enet
= qdev_new("stellaris_enet");
1429 qdev_set_nic_properties(enet
, &nd_table
[0]);
1430 sysbus_realize_and_unref(SYS_BUS_DEVICE(enet
), &error_fatal
);
1431 sysbus_mmio_map(SYS_BUS_DEVICE(enet
), 0, 0x40048000);
1432 sysbus_connect_irq(SYS_BUS_DEVICE(enet
), 0, qdev_get_gpio_in(nvic
, 42));
1434 if (board
->peripherals
& BP_GAMEPAD
) {
1435 qemu_irq gpad_irq
[5];
1436 static const int gpad_keycode
[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1438 gpad_irq
[0] = qemu_irq_invert(gpio_in
[GPIO_E
][0]); /* up */
1439 gpad_irq
[1] = qemu_irq_invert(gpio_in
[GPIO_E
][1]); /* down */
1440 gpad_irq
[2] = qemu_irq_invert(gpio_in
[GPIO_E
][2]); /* left */
1441 gpad_irq
[3] = qemu_irq_invert(gpio_in
[GPIO_E
][3]); /* right */
1442 gpad_irq
[4] = qemu_irq_invert(gpio_in
[GPIO_F
][1]); /* select */
1444 stellaris_gamepad_init(5, gpad_irq
, gpad_keycode
);
1446 for (i
= 0; i
< 7; i
++) {
1447 if (board
->dc4
& (1 << i
)) {
1448 for (j
= 0; j
< 8; j
++) {
1449 if (gpio_out
[i
][j
]) {
1450 qdev_connect_gpio_out(gpio_dev
[i
], j
, gpio_out
[i
][j
]);
1456 /* Add dummy regions for the devices we don't implement yet,
1457 * so guest accesses don't cause unlogged crashes.
1459 create_unimplemented_device("i2c-0", 0x40002000, 0x1000);
1460 create_unimplemented_device("i2c-2", 0x40021000, 0x1000);
1461 create_unimplemented_device("PWM", 0x40028000, 0x1000);
1462 create_unimplemented_device("QEI-0", 0x4002c000, 0x1000);
1463 create_unimplemented_device("QEI-1", 0x4002d000, 0x1000);
1464 create_unimplemented_device("analogue-comparator", 0x4003c000, 0x1000);
1465 create_unimplemented_device("hibernation", 0x400fc000, 0x1000);
1466 create_unimplemented_device("flash-control", 0x400fd000, 0x1000);
1468 armv7m_load_kernel(ARM_CPU(first_cpu
), ms
->kernel_filename
, flash_size
);
1471 /* FIXME: Figure out how to generate these from stellaris_boards. */
1472 static void lm3s811evb_init(MachineState
*machine
)
1474 stellaris_init(machine
, &stellaris_boards
[0]);
1477 static void lm3s6965evb_init(MachineState
*machine
)
1479 stellaris_init(machine
, &stellaris_boards
[1]);
1482 static void lm3s811evb_class_init(ObjectClass
*oc
, void *data
)
1484 MachineClass
*mc
= MACHINE_CLASS(oc
);
1486 mc
->desc
= "Stellaris LM3S811EVB";
1487 mc
->init
= lm3s811evb_init
;
1488 mc
->ignore_memory_transaction_failures
= true;
1489 mc
->default_cpu_type
= ARM_CPU_TYPE_NAME("cortex-m3");
1492 static const TypeInfo lm3s811evb_type
= {
1493 .name
= MACHINE_TYPE_NAME("lm3s811evb"),
1494 .parent
= TYPE_MACHINE
,
1495 .class_init
= lm3s811evb_class_init
,
1498 static void lm3s6965evb_class_init(ObjectClass
*oc
, void *data
)
1500 MachineClass
*mc
= MACHINE_CLASS(oc
);
1502 mc
->desc
= "Stellaris LM3S6965EVB";
1503 mc
->init
= lm3s6965evb_init
;
1504 mc
->ignore_memory_transaction_failures
= true;
1505 mc
->default_cpu_type
= ARM_CPU_TYPE_NAME("cortex-m3");
1508 static const TypeInfo lm3s6965evb_type
= {
1509 .name
= MACHINE_TYPE_NAME("lm3s6965evb"),
1510 .parent
= TYPE_MACHINE
,
1511 .class_init
= lm3s6965evb_class_init
,
1514 static void stellaris_machine_init(void)
1516 type_register_static(&lm3s811evb_type
);
1517 type_register_static(&lm3s6965evb_type
);
1520 type_init(stellaris_machine_init
)
1522 static void stellaris_i2c_class_init(ObjectClass
*klass
, void *data
)
1524 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1526 dc
->vmsd
= &vmstate_stellaris_i2c
;
1529 static const TypeInfo stellaris_i2c_info
= {
1530 .name
= TYPE_STELLARIS_I2C
,
1531 .parent
= TYPE_SYS_BUS_DEVICE
,
1532 .instance_size
= sizeof(stellaris_i2c_state
),
1533 .instance_init
= stellaris_i2c_init
,
1534 .class_init
= stellaris_i2c_class_init
,
1537 static void stellaris_gptm_class_init(ObjectClass
*klass
, void *data
)
1539 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1541 dc
->vmsd
= &vmstate_stellaris_gptm
;
1542 dc
->realize
= stellaris_gptm_realize
;
1545 static const TypeInfo stellaris_gptm_info
= {
1546 .name
= TYPE_STELLARIS_GPTM
,
1547 .parent
= TYPE_SYS_BUS_DEVICE
,
1548 .instance_size
= sizeof(gptm_state
),
1549 .instance_init
= stellaris_gptm_init
,
1550 .class_init
= stellaris_gptm_class_init
,
1553 static void stellaris_adc_class_init(ObjectClass
*klass
, void *data
)
1555 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1557 dc
->vmsd
= &vmstate_stellaris_adc
;
1560 static const TypeInfo stellaris_adc_info
= {
1561 .name
= TYPE_STELLARIS_ADC
,
1562 .parent
= TYPE_SYS_BUS_DEVICE
,
1563 .instance_size
= sizeof(stellaris_adc_state
),
1564 .instance_init
= stellaris_adc_init
,
1565 .class_init
= stellaris_adc_class_init
,
1568 static void stellaris_register_types(void)
1570 type_register_static(&stellaris_i2c_info
);
1571 type_register_static(&stellaris_gptm_info
);
1572 type_register_static(&stellaris_adc_info
);
1575 type_init(stellaris_register_types
)