2 * Luminary Micro Stellaris peripherals
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "hw/sysbus.h"
13 #include "hw/ssi/ssi.h"
14 #include "hw/arm/arm.h"
15 #include "hw/devices.h"
16 #include "qemu/timer.h"
17 #include "hw/i2c/i2c.h"
19 #include "hw/boards.h"
21 #include "exec/address-spaces.h"
22 #include "sysemu/sysemu.h"
23 #include "hw/arm/armv7m.h"
24 #include "hw/char/pl011.h"
25 #include "hw/misc/unimp.h"
36 #define BP_OLED_I2C 0x01
37 #define BP_OLED_SSI 0x02
38 #define BP_GAMEPAD 0x04
40 #define NUM_IRQ_LINES 64
42 typedef const struct {
52 } stellaris_board_info
;
54 /* General purpose timer module. */
56 #define TYPE_STELLARIS_GPTM "stellaris-gptm"
57 #define STELLARIS_GPTM(obj) \
58 OBJECT_CHECK(gptm_state, (obj), TYPE_STELLARIS_GPTM)
60 typedef struct gptm_state
{
61 SysBusDevice parent_obj
;
72 uint32_t match_prescale
[2];
75 struct gptm_state
*opaque
[2];
77 /* The timers have an alternate output used to trigger the ADC. */
82 static void gptm_update_irq(gptm_state
*s
)
85 level
= (s
->state
& s
->mask
) != 0;
86 qemu_set_irq(s
->irq
, level
);
89 static void gptm_stop(gptm_state
*s
, int n
)
91 timer_del(s
->timer
[n
]);
94 static void gptm_reload(gptm_state
*s
, int n
, int reset
)
98 tick
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
102 if (s
->config
== 0) {
103 /* 32-bit CountDown. */
105 count
= s
->load
[0] | (s
->load
[1] << 16);
106 tick
+= (int64_t)count
* system_clock_scale
;
107 } else if (s
->config
== 1) {
108 /* 32-bit RTC. 1Hz tick. */
109 tick
+= NANOSECONDS_PER_SECOND
;
110 } else if (s
->mode
[n
] == 0xa) {
111 /* PWM mode. Not implemented. */
113 qemu_log_mask(LOG_UNIMP
,
114 "GPTM: 16-bit timer mode unimplemented: 0x%x\n",
119 timer_mod(s
->timer
[n
], tick
);
122 static void gptm_tick(void *opaque
)
124 gptm_state
**p
= (gptm_state
**)opaque
;
130 if (s
->config
== 0) {
132 if ((s
->control
& 0x20)) {
133 /* Output trigger. */
134 qemu_irq_pulse(s
->trigger
);
136 if (s
->mode
[0] & 1) {
141 gptm_reload(s
, 0, 0);
143 } else if (s
->config
== 1) {
147 match
= s
->match
[0] | (s
->match
[1] << 16);
153 gptm_reload(s
, 0, 0);
154 } else if (s
->mode
[n
] == 0xa) {
155 /* PWM mode. Not implemented. */
157 qemu_log_mask(LOG_UNIMP
,
158 "GPTM: 16-bit timer mode unimplemented: 0x%x\n",
164 static uint64_t gptm_read(void *opaque
, hwaddr offset
,
167 gptm_state
*s
= (gptm_state
*)opaque
;
172 case 0x04: /* TAMR */
174 case 0x08: /* TBMR */
183 return s
->state
& s
->mask
;
186 case 0x28: /* TAILR */
187 return s
->load
[0] | ((s
->config
< 4) ? (s
->load
[1] << 16) : 0);
188 case 0x2c: /* TBILR */
190 case 0x30: /* TAMARCHR */
191 return s
->match
[0] | ((s
->config
< 4) ? (s
->match
[1] << 16) : 0);
192 case 0x34: /* TBMATCHR */
194 case 0x38: /* TAPR */
195 return s
->prescale
[0];
196 case 0x3c: /* TBPR */
197 return s
->prescale
[1];
198 case 0x40: /* TAPMR */
199 return s
->match_prescale
[0];
200 case 0x44: /* TBPMR */
201 return s
->match_prescale
[1];
203 if (s
->config
== 1) {
206 qemu_log_mask(LOG_UNIMP
,
207 "GPTM: read of TAR but timer read not supported\n");
210 qemu_log_mask(LOG_UNIMP
,
211 "GPTM: read of TBR but timer read not supported\n");
214 qemu_log_mask(LOG_GUEST_ERROR
,
215 "GPTM: read at bad offset 0x02%" HWADDR_PRIx
"\n",
221 static void gptm_write(void *opaque
, hwaddr offset
,
222 uint64_t value
, unsigned size
)
224 gptm_state
*s
= (gptm_state
*)opaque
;
227 /* The timers should be disabled before changing the configuration.
228 We take advantage of this and defer everything until the timer
234 case 0x04: /* TAMR */
237 case 0x08: /* TBMR */
243 /* TODO: Implement pause. */
244 if ((oldval
^ value
) & 1) {
246 gptm_reload(s
, 0, 1);
251 if (((oldval
^ value
) & 0x100) && s
->config
>= 4) {
253 gptm_reload(s
, 1, 1);
260 s
->mask
= value
& 0x77;
266 case 0x28: /* TAILR */
267 s
->load
[0] = value
& 0xffff;
269 s
->load
[1] = value
>> 16;
272 case 0x2c: /* TBILR */
273 s
->load
[1] = value
& 0xffff;
275 case 0x30: /* TAMARCHR */
276 s
->match
[0] = value
& 0xffff;
278 s
->match
[1] = value
>> 16;
281 case 0x34: /* TBMATCHR */
282 s
->match
[1] = value
>> 16;
284 case 0x38: /* TAPR */
285 s
->prescale
[0] = value
;
287 case 0x3c: /* TBPR */
288 s
->prescale
[1] = value
;
290 case 0x40: /* TAPMR */
291 s
->match_prescale
[0] = value
;
293 case 0x44: /* TBPMR */
294 s
->match_prescale
[0] = value
;
297 qemu_log_mask(LOG_GUEST_ERROR
,
298 "GPTM: write at bad offset 0x02%" HWADDR_PRIx
"\n",
304 static const MemoryRegionOps gptm_ops
= {
307 .endianness
= DEVICE_NATIVE_ENDIAN
,
310 static const VMStateDescription vmstate_stellaris_gptm
= {
311 .name
= "stellaris_gptm",
313 .minimum_version_id
= 1,
314 .fields
= (VMStateField
[]) {
315 VMSTATE_UINT32(config
, gptm_state
),
316 VMSTATE_UINT32_ARRAY(mode
, gptm_state
, 2),
317 VMSTATE_UINT32(control
, gptm_state
),
318 VMSTATE_UINT32(state
, gptm_state
),
319 VMSTATE_UINT32(mask
, gptm_state
),
321 VMSTATE_UINT32_ARRAY(load
, gptm_state
, 2),
322 VMSTATE_UINT32_ARRAY(match
, gptm_state
, 2),
323 VMSTATE_UINT32_ARRAY(prescale
, gptm_state
, 2),
324 VMSTATE_UINT32_ARRAY(match_prescale
, gptm_state
, 2),
325 VMSTATE_UINT32(rtc
, gptm_state
),
326 VMSTATE_INT64_ARRAY(tick
, gptm_state
, 2),
327 VMSTATE_TIMER_PTR_ARRAY(timer
, gptm_state
, 2),
328 VMSTATE_END_OF_LIST()
332 static void stellaris_gptm_init(Object
*obj
)
334 DeviceState
*dev
= DEVICE(obj
);
335 gptm_state
*s
= STELLARIS_GPTM(obj
);
336 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
338 sysbus_init_irq(sbd
, &s
->irq
);
339 qdev_init_gpio_out(dev
, &s
->trigger
, 1);
341 memory_region_init_io(&s
->iomem
, obj
, &gptm_ops
, s
,
343 sysbus_init_mmio(sbd
, &s
->iomem
);
345 s
->opaque
[0] = s
->opaque
[1] = s
;
346 s
->timer
[0] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[0]);
347 s
->timer
[1] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[1]);
351 /* System controller. */
370 stellaris_board_info
*board
;
373 static void ssys_update(ssys_state
*s
)
375 qemu_set_irq(s
->irq
, (s
->int_status
& s
->int_mask
) != 0);
378 static uint32_t pllcfg_sandstorm
[16] = {
380 0x1ae0, /* 1.8432 Mhz */
382 0xd573, /* 2.4576 Mhz */
383 0x37a6, /* 3.57954 Mhz */
384 0x1ae2, /* 3.6864 Mhz */
386 0x98bc, /* 4.906 Mhz */
387 0x935b, /* 4.9152 Mhz */
389 0x4dee, /* 5.12 Mhz */
391 0x75db, /* 6.144 Mhz */
392 0x1ae6, /* 7.3728 Mhz */
394 0x585b /* 8.192 Mhz */
397 static uint32_t pllcfg_fury
[16] = {
399 0x1b20, /* 1.8432 Mhz */
401 0xf42b, /* 2.4576 Mhz */
402 0x37e3, /* 3.57954 Mhz */
403 0x1b21, /* 3.6864 Mhz */
405 0x98ee, /* 4.906 Mhz */
406 0xd5b4, /* 4.9152 Mhz */
408 0x4e27, /* 5.12 Mhz */
410 0xec1c, /* 6.144 Mhz */
411 0x1b23, /* 7.3728 Mhz */
413 0xb11c /* 8.192 Mhz */
416 #define DID0_VER_MASK 0x70000000
417 #define DID0_VER_0 0x00000000
418 #define DID0_VER_1 0x10000000
420 #define DID0_CLASS_MASK 0x00FF0000
421 #define DID0_CLASS_SANDSTORM 0x00000000
422 #define DID0_CLASS_FURY 0x00010000
424 static int ssys_board_class(const ssys_state
*s
)
426 uint32_t did0
= s
->board
->did0
;
427 switch (did0
& DID0_VER_MASK
) {
429 return DID0_CLASS_SANDSTORM
;
431 switch (did0
& DID0_CLASS_MASK
) {
432 case DID0_CLASS_SANDSTORM
:
433 case DID0_CLASS_FURY
:
434 return did0
& DID0_CLASS_MASK
;
436 /* for unknown classes, fall through */
438 /* This can only happen if the hardwired constant did0 value
439 * in this board's stellaris_board_info struct is wrong.
441 g_assert_not_reached();
445 static uint64_t ssys_read(void *opaque
, hwaddr offset
,
448 ssys_state
*s
= (ssys_state
*)opaque
;
451 case 0x000: /* DID0 */
452 return s
->board
->did0
;
453 case 0x004: /* DID1 */
454 return s
->board
->did1
;
455 case 0x008: /* DC0 */
456 return s
->board
->dc0
;
457 case 0x010: /* DC1 */
458 return s
->board
->dc1
;
459 case 0x014: /* DC2 */
460 return s
->board
->dc2
;
461 case 0x018: /* DC3 */
462 return s
->board
->dc3
;
463 case 0x01c: /* DC4 */
464 return s
->board
->dc4
;
465 case 0x030: /* PBORCTL */
467 case 0x034: /* LDOPCTL */
469 case 0x040: /* SRCR0 */
471 case 0x044: /* SRCR1 */
473 case 0x048: /* SRCR2 */
475 case 0x050: /* RIS */
476 return s
->int_status
;
477 case 0x054: /* IMC */
479 case 0x058: /* MISC */
480 return s
->int_status
& s
->int_mask
;
481 case 0x05c: /* RESC */
483 case 0x060: /* RCC */
485 case 0x064: /* PLLCFG */
488 xtal
= (s
->rcc
>> 6) & 0xf;
489 switch (ssys_board_class(s
)) {
490 case DID0_CLASS_FURY
:
491 return pllcfg_fury
[xtal
];
492 case DID0_CLASS_SANDSTORM
:
493 return pllcfg_sandstorm
[xtal
];
495 g_assert_not_reached();
498 case 0x070: /* RCC2 */
500 case 0x100: /* RCGC0 */
502 case 0x104: /* RCGC1 */
504 case 0x108: /* RCGC2 */
506 case 0x110: /* SCGC0 */
508 case 0x114: /* SCGC1 */
510 case 0x118: /* SCGC2 */
512 case 0x120: /* DCGC0 */
514 case 0x124: /* DCGC1 */
516 case 0x128: /* DCGC2 */
518 case 0x150: /* CLKVCLR */
520 case 0x160: /* LDOARST */
522 case 0x1e0: /* USER0 */
524 case 0x1e4: /* USER1 */
527 qemu_log_mask(LOG_GUEST_ERROR
,
528 "SSYS: read at bad offset 0x%x\n", (int)offset
);
533 static bool ssys_use_rcc2(ssys_state
*s
)
535 return (s
->rcc2
>> 31) & 0x1;
539 * Caculate the sys. clock period in ms.
541 static void ssys_calculate_system_clock(ssys_state
*s
)
543 if (ssys_use_rcc2(s
)) {
544 system_clock_scale
= 5 * (((s
->rcc2
>> 23) & 0x3f) + 1);
546 system_clock_scale
= 5 * (((s
->rcc
>> 23) & 0xf) + 1);
550 static void ssys_write(void *opaque
, hwaddr offset
,
551 uint64_t value
, unsigned size
)
553 ssys_state
*s
= (ssys_state
*)opaque
;
556 case 0x030: /* PBORCTL */
557 s
->pborctl
= value
& 0xffff;
559 case 0x034: /* LDOPCTL */
560 s
->ldopctl
= value
& 0x1f;
562 case 0x040: /* SRCR0 */
563 case 0x044: /* SRCR1 */
564 case 0x048: /* SRCR2 */
565 qemu_log_mask(LOG_UNIMP
, "Peripheral reset not implemented\n");
567 case 0x054: /* IMC */
568 s
->int_mask
= value
& 0x7f;
570 case 0x058: /* MISC */
571 s
->int_status
&= ~value
;
573 case 0x05c: /* RESC */
574 s
->resc
= value
& 0x3f;
576 case 0x060: /* RCC */
577 if ((s
->rcc
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
579 s
->int_status
|= (1 << 6);
582 ssys_calculate_system_clock(s
);
584 case 0x070: /* RCC2 */
585 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
589 if ((s
->rcc2
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
591 s
->int_status
|= (1 << 6);
594 ssys_calculate_system_clock(s
);
596 case 0x100: /* RCGC0 */
599 case 0x104: /* RCGC1 */
602 case 0x108: /* RCGC2 */
605 case 0x110: /* SCGC0 */
608 case 0x114: /* SCGC1 */
611 case 0x118: /* SCGC2 */
614 case 0x120: /* DCGC0 */
617 case 0x124: /* DCGC1 */
620 case 0x128: /* DCGC2 */
623 case 0x150: /* CLKVCLR */
626 case 0x160: /* LDOARST */
630 qemu_log_mask(LOG_GUEST_ERROR
,
631 "SSYS: write at bad offset 0x%x\n", (int)offset
);
636 static const MemoryRegionOps ssys_ops
= {
639 .endianness
= DEVICE_NATIVE_ENDIAN
,
642 static void ssys_reset(void *opaque
)
644 ssys_state
*s
= (ssys_state
*)opaque
;
649 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
652 s
->rcc2
= 0x07802810;
657 ssys_calculate_system_clock(s
);
660 static int stellaris_sys_post_load(void *opaque
, int version_id
)
662 ssys_state
*s
= opaque
;
664 ssys_calculate_system_clock(s
);
669 static const VMStateDescription vmstate_stellaris_sys
= {
670 .name
= "stellaris_sys",
672 .minimum_version_id
= 1,
673 .post_load
= stellaris_sys_post_load
,
674 .fields
= (VMStateField
[]) {
675 VMSTATE_UINT32(pborctl
, ssys_state
),
676 VMSTATE_UINT32(ldopctl
, ssys_state
),
677 VMSTATE_UINT32(int_mask
, ssys_state
),
678 VMSTATE_UINT32(int_status
, ssys_state
),
679 VMSTATE_UINT32(resc
, ssys_state
),
680 VMSTATE_UINT32(rcc
, ssys_state
),
681 VMSTATE_UINT32_V(rcc2
, ssys_state
, 2),
682 VMSTATE_UINT32_ARRAY(rcgc
, ssys_state
, 3),
683 VMSTATE_UINT32_ARRAY(scgc
, ssys_state
, 3),
684 VMSTATE_UINT32_ARRAY(dcgc
, ssys_state
, 3),
685 VMSTATE_UINT32(clkvclr
, ssys_state
),
686 VMSTATE_UINT32(ldoarst
, ssys_state
),
687 VMSTATE_END_OF_LIST()
691 static int stellaris_sys_init(uint32_t base
, qemu_irq irq
,
692 stellaris_board_info
* board
,
697 s
= g_new0(ssys_state
, 1);
700 /* Most devices come preprogrammed with a MAC address in the user data. */
701 s
->user0
= macaddr
[0] | (macaddr
[1] << 8) | (macaddr
[2] << 16);
702 s
->user1
= macaddr
[3] | (macaddr
[4] << 8) | (macaddr
[5] << 16);
704 memory_region_init_io(&s
->iomem
, NULL
, &ssys_ops
, s
, "ssys", 0x00001000);
705 memory_region_add_subregion(get_system_memory(), base
, &s
->iomem
);
707 vmstate_register(NULL
, -1, &vmstate_stellaris_sys
, s
);
712 /* I2C controller. */
714 #define TYPE_STELLARIS_I2C "stellaris-i2c"
715 #define STELLARIS_I2C(obj) \
716 OBJECT_CHECK(stellaris_i2c_state, (obj), TYPE_STELLARIS_I2C)
719 SysBusDevice parent_obj
;
731 } stellaris_i2c_state
;
733 #define STELLARIS_I2C_MCS_BUSY 0x01
734 #define STELLARIS_I2C_MCS_ERROR 0x02
735 #define STELLARIS_I2C_MCS_ADRACK 0x04
736 #define STELLARIS_I2C_MCS_DATACK 0x08
737 #define STELLARIS_I2C_MCS_ARBLST 0x10
738 #define STELLARIS_I2C_MCS_IDLE 0x20
739 #define STELLARIS_I2C_MCS_BUSBSY 0x40
741 static uint64_t stellaris_i2c_read(void *opaque
, hwaddr offset
,
744 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
750 /* We don't emulate timing, so the controller is never busy. */
751 return s
->mcs
| STELLARIS_I2C_MCS_IDLE
;
754 case 0x0c: /* MTPR */
756 case 0x10: /* MIMR */
758 case 0x14: /* MRIS */
760 case 0x18: /* MMIS */
761 return s
->mris
& s
->mimr
;
765 qemu_log_mask(LOG_GUEST_ERROR
,
766 "stellaris_i2c: read at bad offset 0x%x\n", (int)offset
);
771 static void stellaris_i2c_update(stellaris_i2c_state
*s
)
775 level
= (s
->mris
& s
->mimr
) != 0;
776 qemu_set_irq(s
->irq
, level
);
779 static void stellaris_i2c_write(void *opaque
, hwaddr offset
,
780 uint64_t value
, unsigned size
)
782 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
786 s
->msa
= value
& 0xff;
789 if ((s
->mcr
& 0x10) == 0) {
790 /* Disabled. Do nothing. */
793 /* Grab the bus if this is starting a transfer. */
794 if ((value
& 2) && (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
795 if (i2c_start_transfer(s
->bus
, s
->msa
>> 1, s
->msa
& 1)) {
796 s
->mcs
|= STELLARIS_I2C_MCS_ARBLST
;
798 s
->mcs
&= ~STELLARIS_I2C_MCS_ARBLST
;
799 s
->mcs
|= STELLARIS_I2C_MCS_BUSBSY
;
802 /* If we don't have the bus then indicate an error. */
803 if (!i2c_bus_busy(s
->bus
)
804 || (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
805 s
->mcs
|= STELLARIS_I2C_MCS_ERROR
;
808 s
->mcs
&= ~STELLARIS_I2C_MCS_ERROR
;
810 /* Transfer a byte. */
811 /* TODO: Handle errors. */
814 s
->mdr
= i2c_recv(s
->bus
) & 0xff;
817 i2c_send(s
->bus
, s
->mdr
);
819 /* Raise an interrupt. */
823 /* Finish transfer. */
824 i2c_end_transfer(s
->bus
);
825 s
->mcs
&= ~STELLARIS_I2C_MCS_BUSBSY
;
829 s
->mdr
= value
& 0xff;
831 case 0x0c: /* MTPR */
832 s
->mtpr
= value
& 0xff;
834 case 0x10: /* MIMR */
837 case 0x1c: /* MICR */
842 qemu_log_mask(LOG_UNIMP
,
843 "stellaris_i2c: Loopback not implemented\n");
846 qemu_log_mask(LOG_UNIMP
,
847 "stellaris_i2c: Slave mode not implemented\n");
849 s
->mcr
= value
& 0x31;
852 qemu_log_mask(LOG_GUEST_ERROR
,
853 "stellaris_i2c: write at bad offset 0x%x\n", (int)offset
);
855 stellaris_i2c_update(s
);
858 static void stellaris_i2c_reset(stellaris_i2c_state
*s
)
860 if (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
)
861 i2c_end_transfer(s
->bus
);
870 stellaris_i2c_update(s
);
873 static const MemoryRegionOps stellaris_i2c_ops
= {
874 .read
= stellaris_i2c_read
,
875 .write
= stellaris_i2c_write
,
876 .endianness
= DEVICE_NATIVE_ENDIAN
,
879 static const VMStateDescription vmstate_stellaris_i2c
= {
880 .name
= "stellaris_i2c",
882 .minimum_version_id
= 1,
883 .fields
= (VMStateField
[]) {
884 VMSTATE_UINT32(msa
, stellaris_i2c_state
),
885 VMSTATE_UINT32(mcs
, stellaris_i2c_state
),
886 VMSTATE_UINT32(mdr
, stellaris_i2c_state
),
887 VMSTATE_UINT32(mtpr
, stellaris_i2c_state
),
888 VMSTATE_UINT32(mimr
, stellaris_i2c_state
),
889 VMSTATE_UINT32(mris
, stellaris_i2c_state
),
890 VMSTATE_UINT32(mcr
, stellaris_i2c_state
),
891 VMSTATE_END_OF_LIST()
895 static void stellaris_i2c_init(Object
*obj
)
897 DeviceState
*dev
= DEVICE(obj
);
898 stellaris_i2c_state
*s
= STELLARIS_I2C(obj
);
899 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
902 sysbus_init_irq(sbd
, &s
->irq
);
903 bus
= i2c_init_bus(dev
, "i2c");
906 memory_region_init_io(&s
->iomem
, obj
, &stellaris_i2c_ops
, s
,
908 sysbus_init_mmio(sbd
, &s
->iomem
);
909 /* ??? For now we only implement the master interface. */
910 stellaris_i2c_reset(s
);
913 /* Analogue to Digital Converter. This is only partially implemented,
914 enough for applications that use a combined ADC and timer tick. */
916 #define STELLARIS_ADC_EM_CONTROLLER 0
917 #define STELLARIS_ADC_EM_COMP 1
918 #define STELLARIS_ADC_EM_EXTERNAL 4
919 #define STELLARIS_ADC_EM_TIMER 5
920 #define STELLARIS_ADC_EM_PWM0 6
921 #define STELLARIS_ADC_EM_PWM1 7
922 #define STELLARIS_ADC_EM_PWM2 8
924 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
925 #define STELLARIS_ADC_FIFO_FULL 0x1000
927 #define TYPE_STELLARIS_ADC "stellaris-adc"
928 #define STELLARIS_ADC(obj) \
929 OBJECT_CHECK(stellaris_adc_state, (obj), TYPE_STELLARIS_ADC)
931 typedef struct StellarisADCState
{
932 SysBusDevice parent_obj
;
951 } stellaris_adc_state
;
953 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state
*s
, int n
)
957 tail
= s
->fifo
[n
].state
& 0xf;
958 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_EMPTY
) {
961 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf) | ((tail
+ 1) & 0xf);
962 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_FULL
;
963 if (tail
+ 1 == ((s
->fifo
[n
].state
>> 4) & 0xf))
964 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_EMPTY
;
966 return s
->fifo
[n
].data
[tail
];
969 static void stellaris_adc_fifo_write(stellaris_adc_state
*s
, int n
,
974 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
975 FIFO fir each sequencer. */
976 head
= (s
->fifo
[n
].state
>> 4) & 0xf;
977 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_FULL
) {
981 s
->fifo
[n
].data
[head
] = value
;
982 head
= (head
+ 1) & 0xf;
983 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_EMPTY
;
984 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf0) | (head
<< 4);
985 if ((s
->fifo
[n
].state
& 0xf) == head
)
986 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_FULL
;
989 static void stellaris_adc_update(stellaris_adc_state
*s
)
994 for (n
= 0; n
< 4; n
++) {
995 level
= (s
->ris
& s
->im
& (1 << n
)) != 0;
996 qemu_set_irq(s
->irq
[n
], level
);
1000 static void stellaris_adc_trigger(void *opaque
, int irq
, int level
)
1002 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1005 for (n
= 0; n
< 4; n
++) {
1006 if ((s
->actss
& (1 << n
)) == 0) {
1010 if (((s
->emux
>> (n
* 4)) & 0xff) != 5) {
1014 /* Some applications use the ADC as a random number source, so introduce
1015 some variation into the signal. */
1016 s
->noise
= s
->noise
* 314159 + 1;
1017 /* ??? actual inputs not implemented. Return an arbitrary value. */
1018 stellaris_adc_fifo_write(s
, n
, 0x200 + ((s
->noise
>> 16) & 7));
1020 stellaris_adc_update(s
);
1024 static void stellaris_adc_reset(stellaris_adc_state
*s
)
1028 for (n
= 0; n
< 4; n
++) {
1031 s
->fifo
[n
].state
= STELLARIS_ADC_FIFO_EMPTY
;
1035 static uint64_t stellaris_adc_read(void *opaque
, hwaddr offset
,
1038 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1040 /* TODO: Implement this. */
1041 if (offset
>= 0x40 && offset
< 0xc0) {
1043 n
= (offset
- 0x40) >> 5;
1044 switch (offset
& 0x1f) {
1045 case 0x00: /* SSMUX */
1047 case 0x04: /* SSCTL */
1049 case 0x08: /* SSFIFO */
1050 return stellaris_adc_fifo_read(s
, n
);
1051 case 0x0c: /* SSFSTAT */
1052 return s
->fifo
[n
].state
;
1058 case 0x00: /* ACTSS */
1060 case 0x04: /* RIS */
1064 case 0x0c: /* ISC */
1065 return s
->ris
& s
->im
;
1066 case 0x10: /* OSTAT */
1068 case 0x14: /* EMUX */
1070 case 0x18: /* USTAT */
1072 case 0x20: /* SSPRI */
1074 case 0x30: /* SAC */
1077 qemu_log_mask(LOG_GUEST_ERROR
,
1078 "stellaris_adc: read at bad offset 0x%x\n", (int)offset
);
1083 static void stellaris_adc_write(void *opaque
, hwaddr offset
,
1084 uint64_t value
, unsigned size
)
1086 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1088 /* TODO: Implement this. */
1089 if (offset
>= 0x40 && offset
< 0xc0) {
1091 n
= (offset
- 0x40) >> 5;
1092 switch (offset
& 0x1f) {
1093 case 0x00: /* SSMUX */
1094 s
->ssmux
[n
] = value
& 0x33333333;
1096 case 0x04: /* SSCTL */
1098 qemu_log_mask(LOG_UNIMP
,
1099 "ADC: Unimplemented sequence %" PRIx64
"\n",
1102 s
->ssctl
[n
] = value
;
1109 case 0x00: /* ACTSS */
1110 s
->actss
= value
& 0xf;
1115 case 0x0c: /* ISC */
1118 case 0x10: /* OSTAT */
1121 case 0x14: /* EMUX */
1124 case 0x18: /* USTAT */
1127 case 0x20: /* SSPRI */
1130 case 0x28: /* PSSI */
1131 qemu_log_mask(LOG_UNIMP
, "ADC: sample initiate unimplemented\n");
1133 case 0x30: /* SAC */
1137 qemu_log_mask(LOG_GUEST_ERROR
,
1138 "stellaris_adc: write at bad offset 0x%x\n", (int)offset
);
1140 stellaris_adc_update(s
);
1143 static const MemoryRegionOps stellaris_adc_ops
= {
1144 .read
= stellaris_adc_read
,
1145 .write
= stellaris_adc_write
,
1146 .endianness
= DEVICE_NATIVE_ENDIAN
,
1149 static const VMStateDescription vmstate_stellaris_adc
= {
1150 .name
= "stellaris_adc",
1152 .minimum_version_id
= 1,
1153 .fields
= (VMStateField
[]) {
1154 VMSTATE_UINT32(actss
, stellaris_adc_state
),
1155 VMSTATE_UINT32(ris
, stellaris_adc_state
),
1156 VMSTATE_UINT32(im
, stellaris_adc_state
),
1157 VMSTATE_UINT32(emux
, stellaris_adc_state
),
1158 VMSTATE_UINT32(ostat
, stellaris_adc_state
),
1159 VMSTATE_UINT32(ustat
, stellaris_adc_state
),
1160 VMSTATE_UINT32(sspri
, stellaris_adc_state
),
1161 VMSTATE_UINT32(sac
, stellaris_adc_state
),
1162 VMSTATE_UINT32(fifo
[0].state
, stellaris_adc_state
),
1163 VMSTATE_UINT32_ARRAY(fifo
[0].data
, stellaris_adc_state
, 16),
1164 VMSTATE_UINT32(ssmux
[0], stellaris_adc_state
),
1165 VMSTATE_UINT32(ssctl
[0], stellaris_adc_state
),
1166 VMSTATE_UINT32(fifo
[1].state
, stellaris_adc_state
),
1167 VMSTATE_UINT32_ARRAY(fifo
[1].data
, stellaris_adc_state
, 16),
1168 VMSTATE_UINT32(ssmux
[1], stellaris_adc_state
),
1169 VMSTATE_UINT32(ssctl
[1], stellaris_adc_state
),
1170 VMSTATE_UINT32(fifo
[2].state
, stellaris_adc_state
),
1171 VMSTATE_UINT32_ARRAY(fifo
[2].data
, stellaris_adc_state
, 16),
1172 VMSTATE_UINT32(ssmux
[2], stellaris_adc_state
),
1173 VMSTATE_UINT32(ssctl
[2], stellaris_adc_state
),
1174 VMSTATE_UINT32(fifo
[3].state
, stellaris_adc_state
),
1175 VMSTATE_UINT32_ARRAY(fifo
[3].data
, stellaris_adc_state
, 16),
1176 VMSTATE_UINT32(ssmux
[3], stellaris_adc_state
),
1177 VMSTATE_UINT32(ssctl
[3], stellaris_adc_state
),
1178 VMSTATE_UINT32(noise
, stellaris_adc_state
),
1179 VMSTATE_END_OF_LIST()
1183 static void stellaris_adc_init(Object
*obj
)
1185 DeviceState
*dev
= DEVICE(obj
);
1186 stellaris_adc_state
*s
= STELLARIS_ADC(obj
);
1187 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
1190 for (n
= 0; n
< 4; n
++) {
1191 sysbus_init_irq(sbd
, &s
->irq
[n
]);
1194 memory_region_init_io(&s
->iomem
, obj
, &stellaris_adc_ops
, s
,
1196 sysbus_init_mmio(sbd
, &s
->iomem
);
1197 stellaris_adc_reset(s
);
1198 qdev_init_gpio_in(dev
, stellaris_adc_trigger
, 1);
1202 void do_sys_reset(void *opaque
, int n
, int level
)
1205 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
1210 static stellaris_board_info stellaris_boards
[] = {
1214 0x001f001f, /* dc0 */
1224 0x00ff007f, /* dc0 */
1229 BP_OLED_SSI
| BP_GAMEPAD
1233 static void stellaris_init(MachineState
*ms
, stellaris_board_info
*board
)
1235 static const int uart_irq
[] = {5, 6, 33, 34};
1236 static const int timer_irq
[] = {19, 21, 23, 35};
1237 static const uint32_t gpio_addr
[7] =
1238 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1239 0x40024000, 0x40025000, 0x40026000};
1240 static const int gpio_irq
[7] = {0, 1, 2, 3, 4, 30, 31};
1242 /* Memory map of SoC devices, from
1243 * Stellaris LM3S6965 Microcontroller Data Sheet (rev I)
1244 * http://www.ti.com/lit/ds/symlink/lm3s6965.pdf
1246 * 40000000 wdtimer (unimplemented)
1247 * 40002000 i2c (unimplemented)
1257 * 40021000 i2c (unimplemented)
1261 * 40028000 PWM (unimplemented)
1262 * 4002c000 QEI (unimplemented)
1263 * 4002d000 QEI (unimplemented)
1269 * 4003c000 analogue comparator (unimplemented)
1271 * 400fc000 hibernation module (unimplemented)
1272 * 400fd000 flash memory control (unimplemented)
1273 * 400fe000 system control
1276 DeviceState
*gpio_dev
[7], *nvic
;
1277 qemu_irq gpio_in
[7][8];
1278 qemu_irq gpio_out
[7][8];
1287 MemoryRegion
*sram
= g_new(MemoryRegion
, 1);
1288 MemoryRegion
*flash
= g_new(MemoryRegion
, 1);
1289 MemoryRegion
*system_memory
= get_system_memory();
1291 flash_size
= (((board
->dc0
& 0xffff) + 1) << 1) * 1024;
1292 sram_size
= ((board
->dc0
>> 18) + 1) * 1024;
1294 /* Flash programming is done via the SCU, so pretend it is ROM. */
1295 memory_region_init_ram(flash
, NULL
, "stellaris.flash", flash_size
,
1297 memory_region_set_readonly(flash
, true);
1298 memory_region_add_subregion(system_memory
, 0, flash
);
1300 memory_region_init_ram(sram
, NULL
, "stellaris.sram", sram_size
,
1302 memory_region_add_subregion(system_memory
, 0x20000000, sram
);
1304 nvic
= qdev_create(NULL
, TYPE_ARMV7M
);
1305 qdev_prop_set_uint32(nvic
, "num-irq", NUM_IRQ_LINES
);
1306 qdev_prop_set_string(nvic
, "cpu-type", ms
->cpu_type
);
1307 qdev_prop_set_bit(nvic
, "enable-bitband", true);
1308 object_property_set_link(OBJECT(nvic
), OBJECT(get_system_memory()),
1309 "memory", &error_abort
);
1310 /* This will exit with an error if the user passed us a bad cpu_type */
1311 qdev_init_nofail(nvic
);
1313 qdev_connect_gpio_out_named(nvic
, "SYSRESETREQ", 0,
1314 qemu_allocate_irq(&do_sys_reset
, NULL
, 0));
1316 if (board
->dc1
& (1 << 16)) {
1317 dev
= sysbus_create_varargs(TYPE_STELLARIS_ADC
, 0x40038000,
1318 qdev_get_gpio_in(nvic
, 14),
1319 qdev_get_gpio_in(nvic
, 15),
1320 qdev_get_gpio_in(nvic
, 16),
1321 qdev_get_gpio_in(nvic
, 17),
1323 adc
= qdev_get_gpio_in(dev
, 0);
1327 for (i
= 0; i
< 4; i
++) {
1328 if (board
->dc2
& (0x10000 << i
)) {
1329 dev
= sysbus_create_simple(TYPE_STELLARIS_GPTM
,
1330 0x40030000 + i
* 0x1000,
1331 qdev_get_gpio_in(nvic
, timer_irq
[i
]));
1332 /* TODO: This is incorrect, but we get away with it because
1333 the ADC output is only ever pulsed. */
1334 qdev_connect_gpio_out(dev
, 0, adc
);
1338 stellaris_sys_init(0x400fe000, qdev_get_gpio_in(nvic
, 28),
1339 board
, nd_table
[0].macaddr
.a
);
1341 for (i
= 0; i
< 7; i
++) {
1342 if (board
->dc4
& (1 << i
)) {
1343 gpio_dev
[i
] = sysbus_create_simple("pl061_luminary", gpio_addr
[i
],
1344 qdev_get_gpio_in(nvic
,
1346 for (j
= 0; j
< 8; j
++) {
1347 gpio_in
[i
][j
] = qdev_get_gpio_in(gpio_dev
[i
], j
);
1348 gpio_out
[i
][j
] = NULL
;
1353 if (board
->dc2
& (1 << 12)) {
1354 dev
= sysbus_create_simple(TYPE_STELLARIS_I2C
, 0x40020000,
1355 qdev_get_gpio_in(nvic
, 8));
1356 i2c
= (I2CBus
*)qdev_get_child_bus(dev
, "i2c");
1357 if (board
->peripherals
& BP_OLED_I2C
) {
1358 i2c_create_slave(i2c
, "ssd0303", 0x3d);
1362 for (i
= 0; i
< 4; i
++) {
1363 if (board
->dc2
& (1 << i
)) {
1364 pl011_luminary_create(0x4000c000 + i
* 0x1000,
1365 qdev_get_gpio_in(nvic
, uart_irq
[i
]),
1369 if (board
->dc2
& (1 << 4)) {
1370 dev
= sysbus_create_simple("pl022", 0x40008000,
1371 qdev_get_gpio_in(nvic
, 7));
1372 if (board
->peripherals
& BP_OLED_SSI
) {
1375 DeviceState
*ssddev
;
1377 /* Some boards have both an OLED controller and SD card connected to
1378 * the same SSI port, with the SD card chip select connected to a
1379 * GPIO pin. Technically the OLED chip select is connected to the
1380 * SSI Fss pin. We do not bother emulating that as both devices
1381 * should never be selected simultaneously, and our OLED controller
1382 * ignores stray 0xff commands that occur when deselecting the SD
1385 bus
= qdev_get_child_bus(dev
, "ssi");
1387 sddev
= ssi_create_slave(bus
, "ssi-sd");
1388 ssddev
= ssi_create_slave(bus
, "ssd0323");
1389 gpio_out
[GPIO_D
][0] = qemu_irq_split(
1390 qdev_get_gpio_in_named(sddev
, SSI_GPIO_CS
, 0),
1391 qdev_get_gpio_in_named(ssddev
, SSI_GPIO_CS
, 0));
1392 gpio_out
[GPIO_C
][7] = qdev_get_gpio_in(ssddev
, 0);
1394 /* Make sure the select pin is high. */
1395 qemu_irq_raise(gpio_out
[GPIO_D
][0]);
1398 if (board
->dc4
& (1 << 28)) {
1401 qemu_check_nic_model(&nd_table
[0], "stellaris");
1403 enet
= qdev_create(NULL
, "stellaris_enet");
1404 qdev_set_nic_properties(enet
, &nd_table
[0]);
1405 qdev_init_nofail(enet
);
1406 sysbus_mmio_map(SYS_BUS_DEVICE(enet
), 0, 0x40048000);
1407 sysbus_connect_irq(SYS_BUS_DEVICE(enet
), 0, qdev_get_gpio_in(nvic
, 42));
1409 if (board
->peripherals
& BP_GAMEPAD
) {
1410 qemu_irq gpad_irq
[5];
1411 static const int gpad_keycode
[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1413 gpad_irq
[0] = qemu_irq_invert(gpio_in
[GPIO_E
][0]); /* up */
1414 gpad_irq
[1] = qemu_irq_invert(gpio_in
[GPIO_E
][1]); /* down */
1415 gpad_irq
[2] = qemu_irq_invert(gpio_in
[GPIO_E
][2]); /* left */
1416 gpad_irq
[3] = qemu_irq_invert(gpio_in
[GPIO_E
][3]); /* right */
1417 gpad_irq
[4] = qemu_irq_invert(gpio_in
[GPIO_F
][1]); /* select */
1419 stellaris_gamepad_init(5, gpad_irq
, gpad_keycode
);
1421 for (i
= 0; i
< 7; i
++) {
1422 if (board
->dc4
& (1 << i
)) {
1423 for (j
= 0; j
< 8; j
++) {
1424 if (gpio_out
[i
][j
]) {
1425 qdev_connect_gpio_out(gpio_dev
[i
], j
, gpio_out
[i
][j
]);
1431 /* Add dummy regions for the devices we don't implement yet,
1432 * so guest accesses don't cause unlogged crashes.
1434 create_unimplemented_device("wdtimer", 0x40000000, 0x1000);
1435 create_unimplemented_device("i2c-0", 0x40002000, 0x1000);
1436 create_unimplemented_device("i2c-2", 0x40021000, 0x1000);
1437 create_unimplemented_device("PWM", 0x40028000, 0x1000);
1438 create_unimplemented_device("QEI-0", 0x4002c000, 0x1000);
1439 create_unimplemented_device("QEI-1", 0x4002d000, 0x1000);
1440 create_unimplemented_device("analogue-comparator", 0x4003c000, 0x1000);
1441 create_unimplemented_device("hibernation", 0x400fc000, 0x1000);
1442 create_unimplemented_device("flash-control", 0x400fd000, 0x1000);
1444 armv7m_load_kernel(ARM_CPU(first_cpu
), ms
->kernel_filename
, flash_size
);
1447 /* FIXME: Figure out how to generate these from stellaris_boards. */
1448 static void lm3s811evb_init(MachineState
*machine
)
1450 stellaris_init(machine
, &stellaris_boards
[0]);
1453 static void lm3s6965evb_init(MachineState
*machine
)
1455 stellaris_init(machine
, &stellaris_boards
[1]);
1458 static void lm3s811evb_class_init(ObjectClass
*oc
, void *data
)
1460 MachineClass
*mc
= MACHINE_CLASS(oc
);
1462 mc
->desc
= "Stellaris LM3S811EVB";
1463 mc
->init
= lm3s811evb_init
;
1464 mc
->ignore_memory_transaction_failures
= true;
1465 mc
->default_cpu_type
= ARM_CPU_TYPE_NAME("cortex-m3");
1468 static const TypeInfo lm3s811evb_type
= {
1469 .name
= MACHINE_TYPE_NAME("lm3s811evb"),
1470 .parent
= TYPE_MACHINE
,
1471 .class_init
= lm3s811evb_class_init
,
1474 static void lm3s6965evb_class_init(ObjectClass
*oc
, void *data
)
1476 MachineClass
*mc
= MACHINE_CLASS(oc
);
1478 mc
->desc
= "Stellaris LM3S6965EVB";
1479 mc
->init
= lm3s6965evb_init
;
1480 mc
->ignore_memory_transaction_failures
= true;
1481 mc
->default_cpu_type
= ARM_CPU_TYPE_NAME("cortex-m3");
1484 static const TypeInfo lm3s6965evb_type
= {
1485 .name
= MACHINE_TYPE_NAME("lm3s6965evb"),
1486 .parent
= TYPE_MACHINE
,
1487 .class_init
= lm3s6965evb_class_init
,
1490 static void stellaris_machine_init(void)
1492 type_register_static(&lm3s811evb_type
);
1493 type_register_static(&lm3s6965evb_type
);
1496 type_init(stellaris_machine_init
)
1498 static void stellaris_i2c_class_init(ObjectClass
*klass
, void *data
)
1500 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1502 dc
->vmsd
= &vmstate_stellaris_i2c
;
1505 static const TypeInfo stellaris_i2c_info
= {
1506 .name
= TYPE_STELLARIS_I2C
,
1507 .parent
= TYPE_SYS_BUS_DEVICE
,
1508 .instance_size
= sizeof(stellaris_i2c_state
),
1509 .instance_init
= stellaris_i2c_init
,
1510 .class_init
= stellaris_i2c_class_init
,
1513 static void stellaris_gptm_class_init(ObjectClass
*klass
, void *data
)
1515 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1517 dc
->vmsd
= &vmstate_stellaris_gptm
;
1520 static const TypeInfo stellaris_gptm_info
= {
1521 .name
= TYPE_STELLARIS_GPTM
,
1522 .parent
= TYPE_SYS_BUS_DEVICE
,
1523 .instance_size
= sizeof(gptm_state
),
1524 .instance_init
= stellaris_gptm_init
,
1525 .class_init
= stellaris_gptm_class_init
,
1528 static void stellaris_adc_class_init(ObjectClass
*klass
, void *data
)
1530 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1532 dc
->vmsd
= &vmstate_stellaris_adc
;
1535 static const TypeInfo stellaris_adc_info
= {
1536 .name
= TYPE_STELLARIS_ADC
,
1537 .parent
= TYPE_SYS_BUS_DEVICE
,
1538 .instance_size
= sizeof(stellaris_adc_state
),
1539 .instance_init
= stellaris_adc_init
,
1540 .class_init
= stellaris_adc_class_init
,
1543 static void stellaris_register_types(void)
1545 type_register_static(&stellaris_i2c_info
);
1546 type_register_static(&stellaris_gptm_info
);
1547 type_register_static(&stellaris_adc_info
);
1550 type_init(stellaris_register_types
)