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
;
350 s
->timer
[0] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[0]);
351 s
->timer
[1] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[1]);
355 /* System controller. */
374 stellaris_board_info
*board
;
377 static void ssys_update(ssys_state
*s
)
379 qemu_set_irq(s
->irq
, (s
->int_status
& s
->int_mask
) != 0);
382 static uint32_t pllcfg_sandstorm
[16] = {
384 0x1ae0, /* 1.8432 Mhz */
386 0xd573, /* 2.4576 Mhz */
387 0x37a6, /* 3.57954 Mhz */
388 0x1ae2, /* 3.6864 Mhz */
390 0x98bc, /* 4.906 Mhz */
391 0x935b, /* 4.9152 Mhz */
393 0x4dee, /* 5.12 Mhz */
395 0x75db, /* 6.144 Mhz */
396 0x1ae6, /* 7.3728 Mhz */
398 0x585b /* 8.192 Mhz */
401 static uint32_t pllcfg_fury
[16] = {
403 0x1b20, /* 1.8432 Mhz */
405 0xf42b, /* 2.4576 Mhz */
406 0x37e3, /* 3.57954 Mhz */
407 0x1b21, /* 3.6864 Mhz */
409 0x98ee, /* 4.906 Mhz */
410 0xd5b4, /* 4.9152 Mhz */
412 0x4e27, /* 5.12 Mhz */
414 0xec1c, /* 6.144 Mhz */
415 0x1b23, /* 7.3728 Mhz */
417 0xb11c /* 8.192 Mhz */
420 #define DID0_VER_MASK 0x70000000
421 #define DID0_VER_0 0x00000000
422 #define DID0_VER_1 0x10000000
424 #define DID0_CLASS_MASK 0x00FF0000
425 #define DID0_CLASS_SANDSTORM 0x00000000
426 #define DID0_CLASS_FURY 0x00010000
428 static int ssys_board_class(const ssys_state
*s
)
430 uint32_t did0
= s
->board
->did0
;
431 switch (did0
& DID0_VER_MASK
) {
433 return DID0_CLASS_SANDSTORM
;
435 switch (did0
& DID0_CLASS_MASK
) {
436 case DID0_CLASS_SANDSTORM
:
437 case DID0_CLASS_FURY
:
438 return did0
& DID0_CLASS_MASK
;
440 /* for unknown classes, fall through */
442 /* This can only happen if the hardwired constant did0 value
443 * in this board's stellaris_board_info struct is wrong.
445 g_assert_not_reached();
449 static uint64_t ssys_read(void *opaque
, hwaddr offset
,
452 ssys_state
*s
= (ssys_state
*)opaque
;
455 case 0x000: /* DID0 */
456 return s
->board
->did0
;
457 case 0x004: /* DID1 */
458 return s
->board
->did1
;
459 case 0x008: /* DC0 */
460 return s
->board
->dc0
;
461 case 0x010: /* DC1 */
462 return s
->board
->dc1
;
463 case 0x014: /* DC2 */
464 return s
->board
->dc2
;
465 case 0x018: /* DC3 */
466 return s
->board
->dc3
;
467 case 0x01c: /* DC4 */
468 return s
->board
->dc4
;
469 case 0x030: /* PBORCTL */
471 case 0x034: /* LDOPCTL */
473 case 0x040: /* SRCR0 */
475 case 0x044: /* SRCR1 */
477 case 0x048: /* SRCR2 */
479 case 0x050: /* RIS */
480 return s
->int_status
;
481 case 0x054: /* IMC */
483 case 0x058: /* MISC */
484 return s
->int_status
& s
->int_mask
;
485 case 0x05c: /* RESC */
487 case 0x060: /* RCC */
489 case 0x064: /* PLLCFG */
492 xtal
= (s
->rcc
>> 6) & 0xf;
493 switch (ssys_board_class(s
)) {
494 case DID0_CLASS_FURY
:
495 return pllcfg_fury
[xtal
];
496 case DID0_CLASS_SANDSTORM
:
497 return pllcfg_sandstorm
[xtal
];
499 g_assert_not_reached();
502 case 0x070: /* RCC2 */
504 case 0x100: /* RCGC0 */
506 case 0x104: /* RCGC1 */
508 case 0x108: /* RCGC2 */
510 case 0x110: /* SCGC0 */
512 case 0x114: /* SCGC1 */
514 case 0x118: /* SCGC2 */
516 case 0x120: /* DCGC0 */
518 case 0x124: /* DCGC1 */
520 case 0x128: /* DCGC2 */
522 case 0x150: /* CLKVCLR */
524 case 0x160: /* LDOARST */
526 case 0x1e0: /* USER0 */
528 case 0x1e4: /* USER1 */
531 qemu_log_mask(LOG_GUEST_ERROR
,
532 "SSYS: read at bad offset 0x%x\n", (int)offset
);
537 static bool ssys_use_rcc2(ssys_state
*s
)
539 return (s
->rcc2
>> 31) & 0x1;
543 * Caculate the sys. clock period in ms.
545 static void ssys_calculate_system_clock(ssys_state
*s
)
547 if (ssys_use_rcc2(s
)) {
548 system_clock_scale
= 5 * (((s
->rcc2
>> 23) & 0x3f) + 1);
550 system_clock_scale
= 5 * (((s
->rcc
>> 23) & 0xf) + 1);
554 static void ssys_write(void *opaque
, hwaddr offset
,
555 uint64_t value
, unsigned size
)
557 ssys_state
*s
= (ssys_state
*)opaque
;
560 case 0x030: /* PBORCTL */
561 s
->pborctl
= value
& 0xffff;
563 case 0x034: /* LDOPCTL */
564 s
->ldopctl
= value
& 0x1f;
566 case 0x040: /* SRCR0 */
567 case 0x044: /* SRCR1 */
568 case 0x048: /* SRCR2 */
569 qemu_log_mask(LOG_UNIMP
, "Peripheral reset not implemented\n");
571 case 0x054: /* IMC */
572 s
->int_mask
= value
& 0x7f;
574 case 0x058: /* MISC */
575 s
->int_status
&= ~value
;
577 case 0x05c: /* RESC */
578 s
->resc
= value
& 0x3f;
580 case 0x060: /* RCC */
581 if ((s
->rcc
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
583 s
->int_status
|= (1 << 6);
586 ssys_calculate_system_clock(s
);
588 case 0x070: /* RCC2 */
589 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
593 if ((s
->rcc2
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
595 s
->int_status
|= (1 << 6);
598 ssys_calculate_system_clock(s
);
600 case 0x100: /* RCGC0 */
603 case 0x104: /* RCGC1 */
606 case 0x108: /* RCGC2 */
609 case 0x110: /* SCGC0 */
612 case 0x114: /* SCGC1 */
615 case 0x118: /* SCGC2 */
618 case 0x120: /* DCGC0 */
621 case 0x124: /* DCGC1 */
624 case 0x128: /* DCGC2 */
627 case 0x150: /* CLKVCLR */
630 case 0x160: /* LDOARST */
634 qemu_log_mask(LOG_GUEST_ERROR
,
635 "SSYS: write at bad offset 0x%x\n", (int)offset
);
640 static const MemoryRegionOps ssys_ops
= {
643 .endianness
= DEVICE_NATIVE_ENDIAN
,
646 static void ssys_reset(void *opaque
)
648 ssys_state
*s
= (ssys_state
*)opaque
;
653 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
656 s
->rcc2
= 0x07802810;
661 ssys_calculate_system_clock(s
);
664 static int stellaris_sys_post_load(void *opaque
, int version_id
)
666 ssys_state
*s
= opaque
;
668 ssys_calculate_system_clock(s
);
673 static const VMStateDescription vmstate_stellaris_sys
= {
674 .name
= "stellaris_sys",
676 .minimum_version_id
= 1,
677 .post_load
= stellaris_sys_post_load
,
678 .fields
= (VMStateField
[]) {
679 VMSTATE_UINT32(pborctl
, ssys_state
),
680 VMSTATE_UINT32(ldopctl
, ssys_state
),
681 VMSTATE_UINT32(int_mask
, ssys_state
),
682 VMSTATE_UINT32(int_status
, ssys_state
),
683 VMSTATE_UINT32(resc
, ssys_state
),
684 VMSTATE_UINT32(rcc
, ssys_state
),
685 VMSTATE_UINT32_V(rcc2
, ssys_state
, 2),
686 VMSTATE_UINT32_ARRAY(rcgc
, ssys_state
, 3),
687 VMSTATE_UINT32_ARRAY(scgc
, ssys_state
, 3),
688 VMSTATE_UINT32_ARRAY(dcgc
, ssys_state
, 3),
689 VMSTATE_UINT32(clkvclr
, ssys_state
),
690 VMSTATE_UINT32(ldoarst
, ssys_state
),
691 VMSTATE_END_OF_LIST()
695 static int stellaris_sys_init(uint32_t base
, qemu_irq irq
,
696 stellaris_board_info
* board
,
701 s
= g_new0(ssys_state
, 1);
704 /* Most devices come preprogrammed with a MAC address in the user data. */
705 s
->user0
= macaddr
[0] | (macaddr
[1] << 8) | (macaddr
[2] << 16);
706 s
->user1
= macaddr
[3] | (macaddr
[4] << 8) | (macaddr
[5] << 16);
708 memory_region_init_io(&s
->iomem
, NULL
, &ssys_ops
, s
, "ssys", 0x00001000);
709 memory_region_add_subregion(get_system_memory(), base
, &s
->iomem
);
711 vmstate_register(NULL
, -1, &vmstate_stellaris_sys
, s
);
716 /* I2C controller. */
718 #define TYPE_STELLARIS_I2C "stellaris-i2c"
719 #define STELLARIS_I2C(obj) \
720 OBJECT_CHECK(stellaris_i2c_state, (obj), TYPE_STELLARIS_I2C)
723 SysBusDevice parent_obj
;
735 } stellaris_i2c_state
;
737 #define STELLARIS_I2C_MCS_BUSY 0x01
738 #define STELLARIS_I2C_MCS_ERROR 0x02
739 #define STELLARIS_I2C_MCS_ADRACK 0x04
740 #define STELLARIS_I2C_MCS_DATACK 0x08
741 #define STELLARIS_I2C_MCS_ARBLST 0x10
742 #define STELLARIS_I2C_MCS_IDLE 0x20
743 #define STELLARIS_I2C_MCS_BUSBSY 0x40
745 static uint64_t stellaris_i2c_read(void *opaque
, hwaddr offset
,
748 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
754 /* We don't emulate timing, so the controller is never busy. */
755 return s
->mcs
| STELLARIS_I2C_MCS_IDLE
;
758 case 0x0c: /* MTPR */
760 case 0x10: /* MIMR */
762 case 0x14: /* MRIS */
764 case 0x18: /* MMIS */
765 return s
->mris
& s
->mimr
;
769 qemu_log_mask(LOG_GUEST_ERROR
,
770 "stellaris_i2c: read at bad offset 0x%x\n", (int)offset
);
775 static void stellaris_i2c_update(stellaris_i2c_state
*s
)
779 level
= (s
->mris
& s
->mimr
) != 0;
780 qemu_set_irq(s
->irq
, level
);
783 static void stellaris_i2c_write(void *opaque
, hwaddr offset
,
784 uint64_t value
, unsigned size
)
786 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
790 s
->msa
= value
& 0xff;
793 if ((s
->mcr
& 0x10) == 0) {
794 /* Disabled. Do nothing. */
797 /* Grab the bus if this is starting a transfer. */
798 if ((value
& 2) && (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
799 if (i2c_start_transfer(s
->bus
, s
->msa
>> 1, s
->msa
& 1)) {
800 s
->mcs
|= STELLARIS_I2C_MCS_ARBLST
;
802 s
->mcs
&= ~STELLARIS_I2C_MCS_ARBLST
;
803 s
->mcs
|= STELLARIS_I2C_MCS_BUSBSY
;
806 /* If we don't have the bus then indicate an error. */
807 if (!i2c_bus_busy(s
->bus
)
808 || (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
809 s
->mcs
|= STELLARIS_I2C_MCS_ERROR
;
812 s
->mcs
&= ~STELLARIS_I2C_MCS_ERROR
;
814 /* Transfer a byte. */
815 /* TODO: Handle errors. */
818 s
->mdr
= i2c_recv(s
->bus
);
821 i2c_send(s
->bus
, s
->mdr
);
823 /* Raise an interrupt. */
827 /* Finish transfer. */
828 i2c_end_transfer(s
->bus
);
829 s
->mcs
&= ~STELLARIS_I2C_MCS_BUSBSY
;
833 s
->mdr
= value
& 0xff;
835 case 0x0c: /* MTPR */
836 s
->mtpr
= value
& 0xff;
838 case 0x10: /* MIMR */
841 case 0x1c: /* MICR */
846 qemu_log_mask(LOG_UNIMP
,
847 "stellaris_i2c: Loopback not implemented\n");
850 qemu_log_mask(LOG_UNIMP
,
851 "stellaris_i2c: Slave mode not implemented\n");
853 s
->mcr
= value
& 0x31;
856 qemu_log_mask(LOG_GUEST_ERROR
,
857 "stellaris_i2c: write at bad offset 0x%x\n", (int)offset
);
859 stellaris_i2c_update(s
);
862 static void stellaris_i2c_reset(stellaris_i2c_state
*s
)
864 if (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
)
865 i2c_end_transfer(s
->bus
);
874 stellaris_i2c_update(s
);
877 static const MemoryRegionOps stellaris_i2c_ops
= {
878 .read
= stellaris_i2c_read
,
879 .write
= stellaris_i2c_write
,
880 .endianness
= DEVICE_NATIVE_ENDIAN
,
883 static const VMStateDescription vmstate_stellaris_i2c
= {
884 .name
= "stellaris_i2c",
886 .minimum_version_id
= 1,
887 .fields
= (VMStateField
[]) {
888 VMSTATE_UINT32(msa
, stellaris_i2c_state
),
889 VMSTATE_UINT32(mcs
, stellaris_i2c_state
),
890 VMSTATE_UINT32(mdr
, stellaris_i2c_state
),
891 VMSTATE_UINT32(mtpr
, stellaris_i2c_state
),
892 VMSTATE_UINT32(mimr
, stellaris_i2c_state
),
893 VMSTATE_UINT32(mris
, stellaris_i2c_state
),
894 VMSTATE_UINT32(mcr
, stellaris_i2c_state
),
895 VMSTATE_END_OF_LIST()
899 static void stellaris_i2c_init(Object
*obj
)
901 DeviceState
*dev
= DEVICE(obj
);
902 stellaris_i2c_state
*s
= STELLARIS_I2C(obj
);
903 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
906 sysbus_init_irq(sbd
, &s
->irq
);
907 bus
= i2c_init_bus(dev
, "i2c");
910 memory_region_init_io(&s
->iomem
, obj
, &stellaris_i2c_ops
, s
,
912 sysbus_init_mmio(sbd
, &s
->iomem
);
913 /* ??? For now we only implement the master interface. */
914 stellaris_i2c_reset(s
);
917 /* Analogue to Digital Converter. This is only partially implemented,
918 enough for applications that use a combined ADC and timer tick. */
920 #define STELLARIS_ADC_EM_CONTROLLER 0
921 #define STELLARIS_ADC_EM_COMP 1
922 #define STELLARIS_ADC_EM_EXTERNAL 4
923 #define STELLARIS_ADC_EM_TIMER 5
924 #define STELLARIS_ADC_EM_PWM0 6
925 #define STELLARIS_ADC_EM_PWM1 7
926 #define STELLARIS_ADC_EM_PWM2 8
928 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
929 #define STELLARIS_ADC_FIFO_FULL 0x1000
931 #define TYPE_STELLARIS_ADC "stellaris-adc"
932 #define STELLARIS_ADC(obj) \
933 OBJECT_CHECK(stellaris_adc_state, (obj), TYPE_STELLARIS_ADC)
935 typedef struct StellarisADCState
{
936 SysBusDevice parent_obj
;
955 } stellaris_adc_state
;
957 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state
*s
, int n
)
961 tail
= s
->fifo
[n
].state
& 0xf;
962 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_EMPTY
) {
965 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf) | ((tail
+ 1) & 0xf);
966 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_FULL
;
967 if (tail
+ 1 == ((s
->fifo
[n
].state
>> 4) & 0xf))
968 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_EMPTY
;
970 return s
->fifo
[n
].data
[tail
];
973 static void stellaris_adc_fifo_write(stellaris_adc_state
*s
, int n
,
978 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
979 FIFO fir each sequencer. */
980 head
= (s
->fifo
[n
].state
>> 4) & 0xf;
981 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_FULL
) {
985 s
->fifo
[n
].data
[head
] = value
;
986 head
= (head
+ 1) & 0xf;
987 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_EMPTY
;
988 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf0) | (head
<< 4);
989 if ((s
->fifo
[n
].state
& 0xf) == head
)
990 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_FULL
;
993 static void stellaris_adc_update(stellaris_adc_state
*s
)
998 for (n
= 0; n
< 4; n
++) {
999 level
= (s
->ris
& s
->im
& (1 << n
)) != 0;
1000 qemu_set_irq(s
->irq
[n
], level
);
1004 static void stellaris_adc_trigger(void *opaque
, int irq
, int level
)
1006 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1009 for (n
= 0; n
< 4; n
++) {
1010 if ((s
->actss
& (1 << n
)) == 0) {
1014 if (((s
->emux
>> (n
* 4)) & 0xff) != 5) {
1018 /* Some applications use the ADC as a random number source, so introduce
1019 some variation into the signal. */
1020 s
->noise
= s
->noise
* 314159 + 1;
1021 /* ??? actual inputs not implemented. Return an arbitrary value. */
1022 stellaris_adc_fifo_write(s
, n
, 0x200 + ((s
->noise
>> 16) & 7));
1024 stellaris_adc_update(s
);
1028 static void stellaris_adc_reset(stellaris_adc_state
*s
)
1032 for (n
= 0; n
< 4; n
++) {
1035 s
->fifo
[n
].state
= STELLARIS_ADC_FIFO_EMPTY
;
1039 static uint64_t stellaris_adc_read(void *opaque
, hwaddr offset
,
1042 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1044 /* TODO: Implement this. */
1045 if (offset
>= 0x40 && offset
< 0xc0) {
1047 n
= (offset
- 0x40) >> 5;
1048 switch (offset
& 0x1f) {
1049 case 0x00: /* SSMUX */
1051 case 0x04: /* SSCTL */
1053 case 0x08: /* SSFIFO */
1054 return stellaris_adc_fifo_read(s
, n
);
1055 case 0x0c: /* SSFSTAT */
1056 return s
->fifo
[n
].state
;
1062 case 0x00: /* ACTSS */
1064 case 0x04: /* RIS */
1068 case 0x0c: /* ISC */
1069 return s
->ris
& s
->im
;
1070 case 0x10: /* OSTAT */
1072 case 0x14: /* EMUX */
1074 case 0x18: /* USTAT */
1076 case 0x20: /* SSPRI */
1078 case 0x30: /* SAC */
1081 qemu_log_mask(LOG_GUEST_ERROR
,
1082 "stellaris_adc: read at bad offset 0x%x\n", (int)offset
);
1087 static void stellaris_adc_write(void *opaque
, hwaddr offset
,
1088 uint64_t value
, unsigned size
)
1090 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1092 /* TODO: Implement this. */
1093 if (offset
>= 0x40 && offset
< 0xc0) {
1095 n
= (offset
- 0x40) >> 5;
1096 switch (offset
& 0x1f) {
1097 case 0x00: /* SSMUX */
1098 s
->ssmux
[n
] = value
& 0x33333333;
1100 case 0x04: /* SSCTL */
1102 qemu_log_mask(LOG_UNIMP
,
1103 "ADC: Unimplemented sequence %" PRIx64
"\n",
1106 s
->ssctl
[n
] = value
;
1113 case 0x00: /* ACTSS */
1114 s
->actss
= value
& 0xf;
1119 case 0x0c: /* ISC */
1122 case 0x10: /* OSTAT */
1125 case 0x14: /* EMUX */
1128 case 0x18: /* USTAT */
1131 case 0x20: /* SSPRI */
1134 case 0x28: /* PSSI */
1135 qemu_log_mask(LOG_UNIMP
, "ADC: sample initiate unimplemented\n");
1137 case 0x30: /* SAC */
1141 qemu_log_mask(LOG_GUEST_ERROR
,
1142 "stellaris_adc: write at bad offset 0x%x\n", (int)offset
);
1144 stellaris_adc_update(s
);
1147 static const MemoryRegionOps stellaris_adc_ops
= {
1148 .read
= stellaris_adc_read
,
1149 .write
= stellaris_adc_write
,
1150 .endianness
= DEVICE_NATIVE_ENDIAN
,
1153 static const VMStateDescription vmstate_stellaris_adc
= {
1154 .name
= "stellaris_adc",
1156 .minimum_version_id
= 1,
1157 .fields
= (VMStateField
[]) {
1158 VMSTATE_UINT32(actss
, stellaris_adc_state
),
1159 VMSTATE_UINT32(ris
, stellaris_adc_state
),
1160 VMSTATE_UINT32(im
, stellaris_adc_state
),
1161 VMSTATE_UINT32(emux
, stellaris_adc_state
),
1162 VMSTATE_UINT32(ostat
, stellaris_adc_state
),
1163 VMSTATE_UINT32(ustat
, stellaris_adc_state
),
1164 VMSTATE_UINT32(sspri
, stellaris_adc_state
),
1165 VMSTATE_UINT32(sac
, stellaris_adc_state
),
1166 VMSTATE_UINT32(fifo
[0].state
, stellaris_adc_state
),
1167 VMSTATE_UINT32_ARRAY(fifo
[0].data
, stellaris_adc_state
, 16),
1168 VMSTATE_UINT32(ssmux
[0], stellaris_adc_state
),
1169 VMSTATE_UINT32(ssctl
[0], stellaris_adc_state
),
1170 VMSTATE_UINT32(fifo
[1].state
, stellaris_adc_state
),
1171 VMSTATE_UINT32_ARRAY(fifo
[1].data
, stellaris_adc_state
, 16),
1172 VMSTATE_UINT32(ssmux
[1], stellaris_adc_state
),
1173 VMSTATE_UINT32(ssctl
[1], stellaris_adc_state
),
1174 VMSTATE_UINT32(fifo
[2].state
, stellaris_adc_state
),
1175 VMSTATE_UINT32_ARRAY(fifo
[2].data
, stellaris_adc_state
, 16),
1176 VMSTATE_UINT32(ssmux
[2], stellaris_adc_state
),
1177 VMSTATE_UINT32(ssctl
[2], stellaris_adc_state
),
1178 VMSTATE_UINT32(fifo
[3].state
, stellaris_adc_state
),
1179 VMSTATE_UINT32_ARRAY(fifo
[3].data
, stellaris_adc_state
, 16),
1180 VMSTATE_UINT32(ssmux
[3], stellaris_adc_state
),
1181 VMSTATE_UINT32(ssctl
[3], stellaris_adc_state
),
1182 VMSTATE_UINT32(noise
, stellaris_adc_state
),
1183 VMSTATE_END_OF_LIST()
1187 static void stellaris_adc_init(Object
*obj
)
1189 DeviceState
*dev
= DEVICE(obj
);
1190 stellaris_adc_state
*s
= STELLARIS_ADC(obj
);
1191 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
1194 for (n
= 0; n
< 4; n
++) {
1195 sysbus_init_irq(sbd
, &s
->irq
[n
]);
1198 memory_region_init_io(&s
->iomem
, obj
, &stellaris_adc_ops
, s
,
1200 sysbus_init_mmio(sbd
, &s
->iomem
);
1201 stellaris_adc_reset(s
);
1202 qdev_init_gpio_in(dev
, stellaris_adc_trigger
, 1);
1206 void do_sys_reset(void *opaque
, int n
, int level
)
1209 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
1214 static stellaris_board_info stellaris_boards
[] = {
1218 0x001f001f, /* dc0 */
1228 0x00ff007f, /* dc0 */
1233 BP_OLED_SSI
| BP_GAMEPAD
1237 static void stellaris_init(MachineState
*ms
, stellaris_board_info
*board
)
1239 static const int uart_irq
[] = {5, 6, 33, 34};
1240 static const int timer_irq
[] = {19, 21, 23, 35};
1241 static const uint32_t gpio_addr
[7] =
1242 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1243 0x40024000, 0x40025000, 0x40026000};
1244 static const int gpio_irq
[7] = {0, 1, 2, 3, 4, 30, 31};
1246 /* Memory map of SoC devices, from
1247 * Stellaris LM3S6965 Microcontroller Data Sheet (rev I)
1248 * http://www.ti.com/lit/ds/symlink/lm3s6965.pdf
1251 * 40002000 i2c (unimplemented)
1261 * 40021000 i2c (unimplemented)
1265 * 40028000 PWM (unimplemented)
1266 * 4002c000 QEI (unimplemented)
1267 * 4002d000 QEI (unimplemented)
1273 * 4003c000 analogue comparator (unimplemented)
1275 * 400fc000 hibernation module (unimplemented)
1276 * 400fd000 flash memory control (unimplemented)
1277 * 400fe000 system control
1280 DeviceState
*gpio_dev
[7], *nvic
;
1281 qemu_irq gpio_in
[7][8];
1282 qemu_irq gpio_out
[7][8];
1291 MemoryRegion
*sram
= g_new(MemoryRegion
, 1);
1292 MemoryRegion
*flash
= g_new(MemoryRegion
, 1);
1293 MemoryRegion
*system_memory
= get_system_memory();
1295 flash_size
= (((board
->dc0
& 0xffff) + 1) << 1) * 1024;
1296 sram_size
= ((board
->dc0
>> 18) + 1) * 1024;
1298 /* Flash programming is done via the SCU, so pretend it is ROM. */
1299 memory_region_init_ram(flash
, NULL
, "stellaris.flash", flash_size
,
1301 memory_region_set_readonly(flash
, true);
1302 memory_region_add_subregion(system_memory
, 0, flash
);
1304 memory_region_init_ram(sram
, NULL
, "stellaris.sram", sram_size
,
1306 memory_region_add_subregion(system_memory
, 0x20000000, sram
);
1308 nvic
= qdev_create(NULL
, TYPE_ARMV7M
);
1309 qdev_prop_set_uint32(nvic
, "num-irq", NUM_IRQ_LINES
);
1310 qdev_prop_set_string(nvic
, "cpu-type", ms
->cpu_type
);
1311 qdev_prop_set_bit(nvic
, "enable-bitband", true);
1312 object_property_set_link(OBJECT(nvic
), OBJECT(get_system_memory()),
1313 "memory", &error_abort
);
1314 /* This will exit with an error if the user passed us a bad cpu_type */
1315 qdev_init_nofail(nvic
);
1317 qdev_connect_gpio_out_named(nvic
, "SYSRESETREQ", 0,
1318 qemu_allocate_irq(&do_sys_reset
, NULL
, 0));
1320 if (board
->dc1
& (1 << 16)) {
1321 dev
= sysbus_create_varargs(TYPE_STELLARIS_ADC
, 0x40038000,
1322 qdev_get_gpio_in(nvic
, 14),
1323 qdev_get_gpio_in(nvic
, 15),
1324 qdev_get_gpio_in(nvic
, 16),
1325 qdev_get_gpio_in(nvic
, 17),
1327 adc
= qdev_get_gpio_in(dev
, 0);
1331 for (i
= 0; i
< 4; i
++) {
1332 if (board
->dc2
& (0x10000 << i
)) {
1333 dev
= sysbus_create_simple(TYPE_STELLARIS_GPTM
,
1334 0x40030000 + i
* 0x1000,
1335 qdev_get_gpio_in(nvic
, timer_irq
[i
]));
1336 /* TODO: This is incorrect, but we get away with it because
1337 the ADC output is only ever pulsed. */
1338 qdev_connect_gpio_out(dev
, 0, adc
);
1342 stellaris_sys_init(0x400fe000, qdev_get_gpio_in(nvic
, 28),
1343 board
, nd_table
[0].macaddr
.a
);
1346 if (board
->dc1
& (1 << 3)) { /* watchdog present */
1347 dev
= qdev_create(NULL
, TYPE_LUMINARY_WATCHDOG
);
1349 /* system_clock_scale is valid now */
1350 uint32_t mainclk
= NANOSECONDS_PER_SECOND
/ system_clock_scale
;
1351 qdev_prop_set_uint32(dev
, "wdogclk-frq", mainclk
);
1353 qdev_init_nofail(dev
);
1354 sysbus_mmio_map(SYS_BUS_DEVICE(dev
),
1357 sysbus_connect_irq(SYS_BUS_DEVICE(dev
),
1359 qdev_get_gpio_in(nvic
, 18));
1363 for (i
= 0; i
< 7; i
++) {
1364 if (board
->dc4
& (1 << i
)) {
1365 gpio_dev
[i
] = sysbus_create_simple("pl061_luminary", gpio_addr
[i
],
1366 qdev_get_gpio_in(nvic
,
1368 for (j
= 0; j
< 8; j
++) {
1369 gpio_in
[i
][j
] = qdev_get_gpio_in(gpio_dev
[i
], j
);
1370 gpio_out
[i
][j
] = NULL
;
1375 if (board
->dc2
& (1 << 12)) {
1376 dev
= sysbus_create_simple(TYPE_STELLARIS_I2C
, 0x40020000,
1377 qdev_get_gpio_in(nvic
, 8));
1378 i2c
= (I2CBus
*)qdev_get_child_bus(dev
, "i2c");
1379 if (board
->peripherals
& BP_OLED_I2C
) {
1380 i2c_create_slave(i2c
, "ssd0303", 0x3d);
1384 for (i
= 0; i
< 4; i
++) {
1385 if (board
->dc2
& (1 << i
)) {
1386 pl011_luminary_create(0x4000c000 + i
* 0x1000,
1387 qdev_get_gpio_in(nvic
, uart_irq
[i
]),
1391 if (board
->dc2
& (1 << 4)) {
1392 dev
= sysbus_create_simple("pl022", 0x40008000,
1393 qdev_get_gpio_in(nvic
, 7));
1394 if (board
->peripherals
& BP_OLED_SSI
) {
1397 DeviceState
*ssddev
;
1399 /* Some boards have both an OLED controller and SD card connected to
1400 * the same SSI port, with the SD card chip select connected to a
1401 * GPIO pin. Technically the OLED chip select is connected to the
1402 * SSI Fss pin. We do not bother emulating that as both devices
1403 * should never be selected simultaneously, and our OLED controller
1404 * ignores stray 0xff commands that occur when deselecting the SD
1407 bus
= qdev_get_child_bus(dev
, "ssi");
1409 sddev
= ssi_create_slave(bus
, "ssi-sd");
1410 ssddev
= ssi_create_slave(bus
, "ssd0323");
1411 gpio_out
[GPIO_D
][0] = qemu_irq_split(
1412 qdev_get_gpio_in_named(sddev
, SSI_GPIO_CS
, 0),
1413 qdev_get_gpio_in_named(ssddev
, SSI_GPIO_CS
, 0));
1414 gpio_out
[GPIO_C
][7] = qdev_get_gpio_in(ssddev
, 0);
1416 /* Make sure the select pin is high. */
1417 qemu_irq_raise(gpio_out
[GPIO_D
][0]);
1420 if (board
->dc4
& (1 << 28)) {
1423 qemu_check_nic_model(&nd_table
[0], "stellaris");
1425 enet
= qdev_create(NULL
, "stellaris_enet");
1426 qdev_set_nic_properties(enet
, &nd_table
[0]);
1427 qdev_init_nofail(enet
);
1428 sysbus_mmio_map(SYS_BUS_DEVICE(enet
), 0, 0x40048000);
1429 sysbus_connect_irq(SYS_BUS_DEVICE(enet
), 0, qdev_get_gpio_in(nvic
, 42));
1431 if (board
->peripherals
& BP_GAMEPAD
) {
1432 qemu_irq gpad_irq
[5];
1433 static const int gpad_keycode
[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1435 gpad_irq
[0] = qemu_irq_invert(gpio_in
[GPIO_E
][0]); /* up */
1436 gpad_irq
[1] = qemu_irq_invert(gpio_in
[GPIO_E
][1]); /* down */
1437 gpad_irq
[2] = qemu_irq_invert(gpio_in
[GPIO_E
][2]); /* left */
1438 gpad_irq
[3] = qemu_irq_invert(gpio_in
[GPIO_E
][3]); /* right */
1439 gpad_irq
[4] = qemu_irq_invert(gpio_in
[GPIO_F
][1]); /* select */
1441 stellaris_gamepad_init(5, gpad_irq
, gpad_keycode
);
1443 for (i
= 0; i
< 7; i
++) {
1444 if (board
->dc4
& (1 << i
)) {
1445 for (j
= 0; j
< 8; j
++) {
1446 if (gpio_out
[i
][j
]) {
1447 qdev_connect_gpio_out(gpio_dev
[i
], j
, gpio_out
[i
][j
]);
1453 /* Add dummy regions for the devices we don't implement yet,
1454 * so guest accesses don't cause unlogged crashes.
1456 create_unimplemented_device("i2c-0", 0x40002000, 0x1000);
1457 create_unimplemented_device("i2c-2", 0x40021000, 0x1000);
1458 create_unimplemented_device("PWM", 0x40028000, 0x1000);
1459 create_unimplemented_device("QEI-0", 0x4002c000, 0x1000);
1460 create_unimplemented_device("QEI-1", 0x4002d000, 0x1000);
1461 create_unimplemented_device("analogue-comparator", 0x4003c000, 0x1000);
1462 create_unimplemented_device("hibernation", 0x400fc000, 0x1000);
1463 create_unimplemented_device("flash-control", 0x400fd000, 0x1000);
1465 armv7m_load_kernel(ARM_CPU(first_cpu
), ms
->kernel_filename
, flash_size
);
1468 /* FIXME: Figure out how to generate these from stellaris_boards. */
1469 static void lm3s811evb_init(MachineState
*machine
)
1471 stellaris_init(machine
, &stellaris_boards
[0]);
1474 static void lm3s6965evb_init(MachineState
*machine
)
1476 stellaris_init(machine
, &stellaris_boards
[1]);
1479 static void lm3s811evb_class_init(ObjectClass
*oc
, void *data
)
1481 MachineClass
*mc
= MACHINE_CLASS(oc
);
1483 mc
->desc
= "Stellaris LM3S811EVB";
1484 mc
->init
= lm3s811evb_init
;
1485 mc
->ignore_memory_transaction_failures
= true;
1486 mc
->default_cpu_type
= ARM_CPU_TYPE_NAME("cortex-m3");
1489 static const TypeInfo lm3s811evb_type
= {
1490 .name
= MACHINE_TYPE_NAME("lm3s811evb"),
1491 .parent
= TYPE_MACHINE
,
1492 .class_init
= lm3s811evb_class_init
,
1495 static void lm3s6965evb_class_init(ObjectClass
*oc
, void *data
)
1497 MachineClass
*mc
= MACHINE_CLASS(oc
);
1499 mc
->desc
= "Stellaris LM3S6965EVB";
1500 mc
->init
= lm3s6965evb_init
;
1501 mc
->ignore_memory_transaction_failures
= true;
1502 mc
->default_cpu_type
= ARM_CPU_TYPE_NAME("cortex-m3");
1505 static const TypeInfo lm3s6965evb_type
= {
1506 .name
= MACHINE_TYPE_NAME("lm3s6965evb"),
1507 .parent
= TYPE_MACHINE
,
1508 .class_init
= lm3s6965evb_class_init
,
1511 static void stellaris_machine_init(void)
1513 type_register_static(&lm3s811evb_type
);
1514 type_register_static(&lm3s6965evb_type
);
1517 type_init(stellaris_machine_init
)
1519 static void stellaris_i2c_class_init(ObjectClass
*klass
, void *data
)
1521 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1523 dc
->vmsd
= &vmstate_stellaris_i2c
;
1526 static const TypeInfo stellaris_i2c_info
= {
1527 .name
= TYPE_STELLARIS_I2C
,
1528 .parent
= TYPE_SYS_BUS_DEVICE
,
1529 .instance_size
= sizeof(stellaris_i2c_state
),
1530 .instance_init
= stellaris_i2c_init
,
1531 .class_init
= stellaris_i2c_class_init
,
1534 static void stellaris_gptm_class_init(ObjectClass
*klass
, void *data
)
1536 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1538 dc
->vmsd
= &vmstate_stellaris_gptm
;
1541 static const TypeInfo stellaris_gptm_info
= {
1542 .name
= TYPE_STELLARIS_GPTM
,
1543 .parent
= TYPE_SYS_BUS_DEVICE
,
1544 .instance_size
= sizeof(gptm_state
),
1545 .instance_init
= stellaris_gptm_init
,
1546 .class_init
= stellaris_gptm_class_init
,
1549 static void stellaris_adc_class_init(ObjectClass
*klass
, void *data
)
1551 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1553 dc
->vmsd
= &vmstate_stellaris_adc
;
1556 static const TypeInfo stellaris_adc_info
= {
1557 .name
= TYPE_STELLARIS_ADC
,
1558 .parent
= TYPE_SYS_BUS_DEVICE
,
1559 .instance_size
= sizeof(stellaris_adc_state
),
1560 .instance_init
= stellaris_adc_init
,
1561 .class_init
= stellaris_adc_class_init
,
1564 static void stellaris_register_types(void)
1566 type_register_static(&stellaris_i2c_info
);
1567 type_register_static(&stellaris_gptm_info
);
1568 type_register_static(&stellaris_adc_info
);
1571 type_init(stellaris_register_types
)