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/char/pl011.h"
24 #include "hw/misc/unimp.h"
35 #define BP_OLED_I2C 0x01
36 #define BP_OLED_SSI 0x02
37 #define BP_GAMEPAD 0x04
39 #define NUM_IRQ_LINES 64
41 typedef const struct {
51 } stellaris_board_info
;
53 /* General purpose timer module. */
55 #define TYPE_STELLARIS_GPTM "stellaris-gptm"
56 #define STELLARIS_GPTM(obj) \
57 OBJECT_CHECK(gptm_state, (obj), TYPE_STELLARIS_GPTM)
59 typedef struct gptm_state
{
60 SysBusDevice parent_obj
;
71 uint32_t match_prescale
[2];
74 struct gptm_state
*opaque
[2];
76 /* The timers have an alternate output used to trigger the ADC. */
81 static void gptm_update_irq(gptm_state
*s
)
84 level
= (s
->state
& s
->mask
) != 0;
85 qemu_set_irq(s
->irq
, level
);
88 static void gptm_stop(gptm_state
*s
, int n
)
90 timer_del(s
->timer
[n
]);
93 static void gptm_reload(gptm_state
*s
, int n
, int reset
)
97 tick
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
101 if (s
->config
== 0) {
102 /* 32-bit CountDown. */
104 count
= s
->load
[0] | (s
->load
[1] << 16);
105 tick
+= (int64_t)count
* system_clock_scale
;
106 } else if (s
->config
== 1) {
107 /* 32-bit RTC. 1Hz tick. */
108 tick
+= NANOSECONDS_PER_SECOND
;
109 } else if (s
->mode
[n
] == 0xa) {
110 /* PWM mode. Not implemented. */
112 qemu_log_mask(LOG_UNIMP
,
113 "GPTM: 16-bit timer mode unimplemented: 0x%x\n",
118 timer_mod(s
->timer
[n
], tick
);
121 static void gptm_tick(void *opaque
)
123 gptm_state
**p
= (gptm_state
**)opaque
;
129 if (s
->config
== 0) {
131 if ((s
->control
& 0x20)) {
132 /* Output trigger. */
133 qemu_irq_pulse(s
->trigger
);
135 if (s
->mode
[0] & 1) {
140 gptm_reload(s
, 0, 0);
142 } else if (s
->config
== 1) {
146 match
= s
->match
[0] | (s
->match
[1] << 16);
152 gptm_reload(s
, 0, 0);
153 } else if (s
->mode
[n
] == 0xa) {
154 /* PWM mode. Not implemented. */
156 qemu_log_mask(LOG_UNIMP
,
157 "GPTM: 16-bit timer mode unimplemented: 0x%x\n",
163 static uint64_t gptm_read(void *opaque
, hwaddr offset
,
166 gptm_state
*s
= (gptm_state
*)opaque
;
171 case 0x04: /* TAMR */
173 case 0x08: /* TBMR */
182 return s
->state
& s
->mask
;
185 case 0x28: /* TAILR */
186 return s
->load
[0] | ((s
->config
< 4) ? (s
->load
[1] << 16) : 0);
187 case 0x2c: /* TBILR */
189 case 0x30: /* TAMARCHR */
190 return s
->match
[0] | ((s
->config
< 4) ? (s
->match
[1] << 16) : 0);
191 case 0x34: /* TBMATCHR */
193 case 0x38: /* TAPR */
194 return s
->prescale
[0];
195 case 0x3c: /* TBPR */
196 return s
->prescale
[1];
197 case 0x40: /* TAPMR */
198 return s
->match_prescale
[0];
199 case 0x44: /* TBPMR */
200 return s
->match_prescale
[1];
202 if (s
->config
== 1) {
205 qemu_log_mask(LOG_UNIMP
,
206 "GPTM: read of TAR but timer read not supported");
209 qemu_log_mask(LOG_UNIMP
,
210 "GPTM: read of TBR but timer read not supported");
213 qemu_log_mask(LOG_GUEST_ERROR
,
214 "GPTM: read at bad offset 0x%x\n", (int)offset
);
219 static void gptm_write(void *opaque
, hwaddr offset
,
220 uint64_t value
, unsigned size
)
222 gptm_state
*s
= (gptm_state
*)opaque
;
225 /* The timers should be disabled before changing the configuration.
226 We take advantage of this and defer everything until the timer
232 case 0x04: /* TAMR */
235 case 0x08: /* TBMR */
241 /* TODO: Implement pause. */
242 if ((oldval
^ value
) & 1) {
244 gptm_reload(s
, 0, 1);
249 if (((oldval
^ value
) & 0x100) && s
->config
>= 4) {
251 gptm_reload(s
, 1, 1);
258 s
->mask
= value
& 0x77;
264 case 0x28: /* TAILR */
265 s
->load
[0] = value
& 0xffff;
267 s
->load
[1] = value
>> 16;
270 case 0x2c: /* TBILR */
271 s
->load
[1] = value
& 0xffff;
273 case 0x30: /* TAMARCHR */
274 s
->match
[0] = value
& 0xffff;
276 s
->match
[1] = value
>> 16;
279 case 0x34: /* TBMATCHR */
280 s
->match
[1] = value
>> 16;
282 case 0x38: /* TAPR */
283 s
->prescale
[0] = value
;
285 case 0x3c: /* TBPR */
286 s
->prescale
[1] = value
;
288 case 0x40: /* TAPMR */
289 s
->match_prescale
[0] = value
;
291 case 0x44: /* TBPMR */
292 s
->match_prescale
[0] = value
;
295 qemu_log_mask(LOG_GUEST_ERROR
,
296 "GPTM: read at bad offset 0x%x\n", (int)offset
);
301 static const MemoryRegionOps gptm_ops
= {
304 .endianness
= DEVICE_NATIVE_ENDIAN
,
307 static const VMStateDescription vmstate_stellaris_gptm
= {
308 .name
= "stellaris_gptm",
310 .minimum_version_id
= 1,
311 .fields
= (VMStateField
[]) {
312 VMSTATE_UINT32(config
, gptm_state
),
313 VMSTATE_UINT32_ARRAY(mode
, gptm_state
, 2),
314 VMSTATE_UINT32(control
, gptm_state
),
315 VMSTATE_UINT32(state
, gptm_state
),
316 VMSTATE_UINT32(mask
, gptm_state
),
318 VMSTATE_UINT32_ARRAY(load
, gptm_state
, 2),
319 VMSTATE_UINT32_ARRAY(match
, gptm_state
, 2),
320 VMSTATE_UINT32_ARRAY(prescale
, gptm_state
, 2),
321 VMSTATE_UINT32_ARRAY(match_prescale
, gptm_state
, 2),
322 VMSTATE_UINT32(rtc
, gptm_state
),
323 VMSTATE_INT64_ARRAY(tick
, gptm_state
, 2),
324 VMSTATE_TIMER_PTR_ARRAY(timer
, gptm_state
, 2),
325 VMSTATE_END_OF_LIST()
329 static void stellaris_gptm_init(Object
*obj
)
331 DeviceState
*dev
= DEVICE(obj
);
332 gptm_state
*s
= STELLARIS_GPTM(obj
);
333 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
335 sysbus_init_irq(sbd
, &s
->irq
);
336 qdev_init_gpio_out(dev
, &s
->trigger
, 1);
338 memory_region_init_io(&s
->iomem
, obj
, &gptm_ops
, s
,
340 sysbus_init_mmio(sbd
, &s
->iomem
);
342 s
->opaque
[0] = s
->opaque
[1] = s
;
343 s
->timer
[0] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[0]);
344 s
->timer
[1] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[1]);
348 /* System controller. */
367 stellaris_board_info
*board
;
370 static void ssys_update(ssys_state
*s
)
372 qemu_set_irq(s
->irq
, (s
->int_status
& s
->int_mask
) != 0);
375 static uint32_t pllcfg_sandstorm
[16] = {
377 0x1ae0, /* 1.8432 Mhz */
379 0xd573, /* 2.4576 Mhz */
380 0x37a6, /* 3.57954 Mhz */
381 0x1ae2, /* 3.6864 Mhz */
383 0x98bc, /* 4.906 Mhz */
384 0x935b, /* 4.9152 Mhz */
386 0x4dee, /* 5.12 Mhz */
388 0x75db, /* 6.144 Mhz */
389 0x1ae6, /* 7.3728 Mhz */
391 0x585b /* 8.192 Mhz */
394 static uint32_t pllcfg_fury
[16] = {
396 0x1b20, /* 1.8432 Mhz */
398 0xf42b, /* 2.4576 Mhz */
399 0x37e3, /* 3.57954 Mhz */
400 0x1b21, /* 3.6864 Mhz */
402 0x98ee, /* 4.906 Mhz */
403 0xd5b4, /* 4.9152 Mhz */
405 0x4e27, /* 5.12 Mhz */
407 0xec1c, /* 6.144 Mhz */
408 0x1b23, /* 7.3728 Mhz */
410 0xb11c /* 8.192 Mhz */
413 #define DID0_VER_MASK 0x70000000
414 #define DID0_VER_0 0x00000000
415 #define DID0_VER_1 0x10000000
417 #define DID0_CLASS_MASK 0x00FF0000
418 #define DID0_CLASS_SANDSTORM 0x00000000
419 #define DID0_CLASS_FURY 0x00010000
421 static int ssys_board_class(const ssys_state
*s
)
423 uint32_t did0
= s
->board
->did0
;
424 switch (did0
& DID0_VER_MASK
) {
426 return DID0_CLASS_SANDSTORM
;
428 switch (did0
& DID0_CLASS_MASK
) {
429 case DID0_CLASS_SANDSTORM
:
430 case DID0_CLASS_FURY
:
431 return did0
& DID0_CLASS_MASK
;
433 /* for unknown classes, fall through */
435 /* This can only happen if the hardwired constant did0 value
436 * in this board's stellaris_board_info struct is wrong.
438 g_assert_not_reached();
442 static uint64_t ssys_read(void *opaque
, hwaddr offset
,
445 ssys_state
*s
= (ssys_state
*)opaque
;
448 case 0x000: /* DID0 */
449 return s
->board
->did0
;
450 case 0x004: /* DID1 */
451 return s
->board
->did1
;
452 case 0x008: /* DC0 */
453 return s
->board
->dc0
;
454 case 0x010: /* DC1 */
455 return s
->board
->dc1
;
456 case 0x014: /* DC2 */
457 return s
->board
->dc2
;
458 case 0x018: /* DC3 */
459 return s
->board
->dc3
;
460 case 0x01c: /* DC4 */
461 return s
->board
->dc4
;
462 case 0x030: /* PBORCTL */
464 case 0x034: /* LDOPCTL */
466 case 0x040: /* SRCR0 */
468 case 0x044: /* SRCR1 */
470 case 0x048: /* SRCR2 */
472 case 0x050: /* RIS */
473 return s
->int_status
;
474 case 0x054: /* IMC */
476 case 0x058: /* MISC */
477 return s
->int_status
& s
->int_mask
;
478 case 0x05c: /* RESC */
480 case 0x060: /* RCC */
482 case 0x064: /* PLLCFG */
485 xtal
= (s
->rcc
>> 6) & 0xf;
486 switch (ssys_board_class(s
)) {
487 case DID0_CLASS_FURY
:
488 return pllcfg_fury
[xtal
];
489 case DID0_CLASS_SANDSTORM
:
490 return pllcfg_sandstorm
[xtal
];
492 g_assert_not_reached();
495 case 0x070: /* RCC2 */
497 case 0x100: /* RCGC0 */
499 case 0x104: /* RCGC1 */
501 case 0x108: /* RCGC2 */
503 case 0x110: /* SCGC0 */
505 case 0x114: /* SCGC1 */
507 case 0x118: /* SCGC2 */
509 case 0x120: /* DCGC0 */
511 case 0x124: /* DCGC1 */
513 case 0x128: /* DCGC2 */
515 case 0x150: /* CLKVCLR */
517 case 0x160: /* LDOARST */
519 case 0x1e0: /* USER0 */
521 case 0x1e4: /* USER1 */
524 qemu_log_mask(LOG_GUEST_ERROR
,
525 "SSYS: read at bad offset 0x%x\n", (int)offset
);
530 static bool ssys_use_rcc2(ssys_state
*s
)
532 return (s
->rcc2
>> 31) & 0x1;
536 * Caculate the sys. clock period in ms.
538 static void ssys_calculate_system_clock(ssys_state
*s
)
540 if (ssys_use_rcc2(s
)) {
541 system_clock_scale
= 5 * (((s
->rcc2
>> 23) & 0x3f) + 1);
543 system_clock_scale
= 5 * (((s
->rcc
>> 23) & 0xf) + 1);
547 static void ssys_write(void *opaque
, hwaddr offset
,
548 uint64_t value
, unsigned size
)
550 ssys_state
*s
= (ssys_state
*)opaque
;
553 case 0x030: /* PBORCTL */
554 s
->pborctl
= value
& 0xffff;
556 case 0x034: /* LDOPCTL */
557 s
->ldopctl
= value
& 0x1f;
559 case 0x040: /* SRCR0 */
560 case 0x044: /* SRCR1 */
561 case 0x048: /* SRCR2 */
562 fprintf(stderr
, "Peripheral reset not implemented\n");
564 case 0x054: /* IMC */
565 s
->int_mask
= value
& 0x7f;
567 case 0x058: /* MISC */
568 s
->int_status
&= ~value
;
570 case 0x05c: /* RESC */
571 s
->resc
= value
& 0x3f;
573 case 0x060: /* RCC */
574 if ((s
->rcc
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
576 s
->int_status
|= (1 << 6);
579 ssys_calculate_system_clock(s
);
581 case 0x070: /* RCC2 */
582 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
586 if ((s
->rcc2
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
588 s
->int_status
|= (1 << 6);
591 ssys_calculate_system_clock(s
);
593 case 0x100: /* RCGC0 */
596 case 0x104: /* RCGC1 */
599 case 0x108: /* RCGC2 */
602 case 0x110: /* SCGC0 */
605 case 0x114: /* SCGC1 */
608 case 0x118: /* SCGC2 */
611 case 0x120: /* DCGC0 */
614 case 0x124: /* DCGC1 */
617 case 0x128: /* DCGC2 */
620 case 0x150: /* CLKVCLR */
623 case 0x160: /* LDOARST */
627 qemu_log_mask(LOG_GUEST_ERROR
,
628 "SSYS: write at bad offset 0x%x\n", (int)offset
);
633 static const MemoryRegionOps ssys_ops
= {
636 .endianness
= DEVICE_NATIVE_ENDIAN
,
639 static void ssys_reset(void *opaque
)
641 ssys_state
*s
= (ssys_state
*)opaque
;
646 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
649 s
->rcc2
= 0x07802810;
654 ssys_calculate_system_clock(s
);
657 static int stellaris_sys_post_load(void *opaque
, int version_id
)
659 ssys_state
*s
= opaque
;
661 ssys_calculate_system_clock(s
);
666 static const VMStateDescription vmstate_stellaris_sys
= {
667 .name
= "stellaris_sys",
669 .minimum_version_id
= 1,
670 .post_load
= stellaris_sys_post_load
,
671 .fields
= (VMStateField
[]) {
672 VMSTATE_UINT32(pborctl
, ssys_state
),
673 VMSTATE_UINT32(ldopctl
, ssys_state
),
674 VMSTATE_UINT32(int_mask
, ssys_state
),
675 VMSTATE_UINT32(int_status
, ssys_state
),
676 VMSTATE_UINT32(resc
, ssys_state
),
677 VMSTATE_UINT32(rcc
, ssys_state
),
678 VMSTATE_UINT32_V(rcc2
, ssys_state
, 2),
679 VMSTATE_UINT32_ARRAY(rcgc
, ssys_state
, 3),
680 VMSTATE_UINT32_ARRAY(scgc
, ssys_state
, 3),
681 VMSTATE_UINT32_ARRAY(dcgc
, ssys_state
, 3),
682 VMSTATE_UINT32(clkvclr
, ssys_state
),
683 VMSTATE_UINT32(ldoarst
, ssys_state
),
684 VMSTATE_END_OF_LIST()
688 static int stellaris_sys_init(uint32_t base
, qemu_irq irq
,
689 stellaris_board_info
* board
,
694 s
= g_new0(ssys_state
, 1);
697 /* Most devices come preprogrammed with a MAC address in the user data. */
698 s
->user0
= macaddr
[0] | (macaddr
[1] << 8) | (macaddr
[2] << 16);
699 s
->user1
= macaddr
[3] | (macaddr
[4] << 8) | (macaddr
[5] << 16);
701 memory_region_init_io(&s
->iomem
, NULL
, &ssys_ops
, s
, "ssys", 0x00001000);
702 memory_region_add_subregion(get_system_memory(), base
, &s
->iomem
);
704 vmstate_register(NULL
, -1, &vmstate_stellaris_sys
, s
);
709 /* I2C controller. */
711 #define TYPE_STELLARIS_I2C "stellaris-i2c"
712 #define STELLARIS_I2C(obj) \
713 OBJECT_CHECK(stellaris_i2c_state, (obj), TYPE_STELLARIS_I2C)
716 SysBusDevice parent_obj
;
728 } stellaris_i2c_state
;
730 #define STELLARIS_I2C_MCS_BUSY 0x01
731 #define STELLARIS_I2C_MCS_ERROR 0x02
732 #define STELLARIS_I2C_MCS_ADRACK 0x04
733 #define STELLARIS_I2C_MCS_DATACK 0x08
734 #define STELLARIS_I2C_MCS_ARBLST 0x10
735 #define STELLARIS_I2C_MCS_IDLE 0x20
736 #define STELLARIS_I2C_MCS_BUSBSY 0x40
738 static uint64_t stellaris_i2c_read(void *opaque
, hwaddr offset
,
741 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
747 /* We don't emulate timing, so the controller is never busy. */
748 return s
->mcs
| STELLARIS_I2C_MCS_IDLE
;
751 case 0x0c: /* MTPR */
753 case 0x10: /* MIMR */
755 case 0x14: /* MRIS */
757 case 0x18: /* MMIS */
758 return s
->mris
& s
->mimr
;
762 qemu_log_mask(LOG_GUEST_ERROR
,
763 "stellaris_i2c: read at bad offset 0x%x\n", (int)offset
);
768 static void stellaris_i2c_update(stellaris_i2c_state
*s
)
772 level
= (s
->mris
& s
->mimr
) != 0;
773 qemu_set_irq(s
->irq
, level
);
776 static void stellaris_i2c_write(void *opaque
, hwaddr offset
,
777 uint64_t value
, unsigned size
)
779 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
783 s
->msa
= value
& 0xff;
786 if ((s
->mcr
& 0x10) == 0) {
787 /* Disabled. Do nothing. */
790 /* Grab the bus if this is starting a transfer. */
791 if ((value
& 2) && (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
792 if (i2c_start_transfer(s
->bus
, s
->msa
>> 1, s
->msa
& 1)) {
793 s
->mcs
|= STELLARIS_I2C_MCS_ARBLST
;
795 s
->mcs
&= ~STELLARIS_I2C_MCS_ARBLST
;
796 s
->mcs
|= STELLARIS_I2C_MCS_BUSBSY
;
799 /* If we don't have the bus then indicate an error. */
800 if (!i2c_bus_busy(s
->bus
)
801 || (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
802 s
->mcs
|= STELLARIS_I2C_MCS_ERROR
;
805 s
->mcs
&= ~STELLARIS_I2C_MCS_ERROR
;
807 /* Transfer a byte. */
808 /* TODO: Handle errors. */
811 s
->mdr
= i2c_recv(s
->bus
) & 0xff;
814 i2c_send(s
->bus
, s
->mdr
);
816 /* Raise an interrupt. */
820 /* Finish transfer. */
821 i2c_end_transfer(s
->bus
);
822 s
->mcs
&= ~STELLARIS_I2C_MCS_BUSBSY
;
826 s
->mdr
= value
& 0xff;
828 case 0x0c: /* MTPR */
829 s
->mtpr
= value
& 0xff;
831 case 0x10: /* MIMR */
834 case 0x1c: /* MICR */
839 qemu_log_mask(LOG_UNIMP
, "stellaris_i2c: Loopback not implemented");
842 qemu_log_mask(LOG_UNIMP
,
843 "stellaris_i2c: Slave mode not implemented");
845 s
->mcr
= value
& 0x31;
848 qemu_log_mask(LOG_GUEST_ERROR
,
849 "stellaris_i2c: write at bad offset 0x%x\n", (int)offset
);
851 stellaris_i2c_update(s
);
854 static void stellaris_i2c_reset(stellaris_i2c_state
*s
)
856 if (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
)
857 i2c_end_transfer(s
->bus
);
866 stellaris_i2c_update(s
);
869 static const MemoryRegionOps stellaris_i2c_ops
= {
870 .read
= stellaris_i2c_read
,
871 .write
= stellaris_i2c_write
,
872 .endianness
= DEVICE_NATIVE_ENDIAN
,
875 static const VMStateDescription vmstate_stellaris_i2c
= {
876 .name
= "stellaris_i2c",
878 .minimum_version_id
= 1,
879 .fields
= (VMStateField
[]) {
880 VMSTATE_UINT32(msa
, stellaris_i2c_state
),
881 VMSTATE_UINT32(mcs
, stellaris_i2c_state
),
882 VMSTATE_UINT32(mdr
, stellaris_i2c_state
),
883 VMSTATE_UINT32(mtpr
, stellaris_i2c_state
),
884 VMSTATE_UINT32(mimr
, stellaris_i2c_state
),
885 VMSTATE_UINT32(mris
, stellaris_i2c_state
),
886 VMSTATE_UINT32(mcr
, stellaris_i2c_state
),
887 VMSTATE_END_OF_LIST()
891 static void stellaris_i2c_init(Object
*obj
)
893 DeviceState
*dev
= DEVICE(obj
);
894 stellaris_i2c_state
*s
= STELLARIS_I2C(obj
);
895 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
898 sysbus_init_irq(sbd
, &s
->irq
);
899 bus
= i2c_init_bus(dev
, "i2c");
902 memory_region_init_io(&s
->iomem
, obj
, &stellaris_i2c_ops
, s
,
904 sysbus_init_mmio(sbd
, &s
->iomem
);
905 /* ??? For now we only implement the master interface. */
906 stellaris_i2c_reset(s
);
909 /* Analogue to Digital Converter. This is only partially implemented,
910 enough for applications that use a combined ADC and timer tick. */
912 #define STELLARIS_ADC_EM_CONTROLLER 0
913 #define STELLARIS_ADC_EM_COMP 1
914 #define STELLARIS_ADC_EM_EXTERNAL 4
915 #define STELLARIS_ADC_EM_TIMER 5
916 #define STELLARIS_ADC_EM_PWM0 6
917 #define STELLARIS_ADC_EM_PWM1 7
918 #define STELLARIS_ADC_EM_PWM2 8
920 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
921 #define STELLARIS_ADC_FIFO_FULL 0x1000
923 #define TYPE_STELLARIS_ADC "stellaris-adc"
924 #define STELLARIS_ADC(obj) \
925 OBJECT_CHECK(stellaris_adc_state, (obj), TYPE_STELLARIS_ADC)
927 typedef struct StellarisADCState
{
928 SysBusDevice parent_obj
;
947 } stellaris_adc_state
;
949 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state
*s
, int n
)
953 tail
= s
->fifo
[n
].state
& 0xf;
954 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_EMPTY
) {
957 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf) | ((tail
+ 1) & 0xf);
958 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_FULL
;
959 if (tail
+ 1 == ((s
->fifo
[n
].state
>> 4) & 0xf))
960 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_EMPTY
;
962 return s
->fifo
[n
].data
[tail
];
965 static void stellaris_adc_fifo_write(stellaris_adc_state
*s
, int n
,
970 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
971 FIFO fir each sequencer. */
972 head
= (s
->fifo
[n
].state
>> 4) & 0xf;
973 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_FULL
) {
977 s
->fifo
[n
].data
[head
] = value
;
978 head
= (head
+ 1) & 0xf;
979 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_EMPTY
;
980 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf0) | (head
<< 4);
981 if ((s
->fifo
[n
].state
& 0xf) == head
)
982 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_FULL
;
985 static void stellaris_adc_update(stellaris_adc_state
*s
)
990 for (n
= 0; n
< 4; n
++) {
991 level
= (s
->ris
& s
->im
& (1 << n
)) != 0;
992 qemu_set_irq(s
->irq
[n
], level
);
996 static void stellaris_adc_trigger(void *opaque
, int irq
, int level
)
998 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1001 for (n
= 0; n
< 4; n
++) {
1002 if ((s
->actss
& (1 << n
)) == 0) {
1006 if (((s
->emux
>> (n
* 4)) & 0xff) != 5) {
1010 /* Some applications use the ADC as a random number source, so introduce
1011 some variation into the signal. */
1012 s
->noise
= s
->noise
* 314159 + 1;
1013 /* ??? actual inputs not implemented. Return an arbitrary value. */
1014 stellaris_adc_fifo_write(s
, n
, 0x200 + ((s
->noise
>> 16) & 7));
1016 stellaris_adc_update(s
);
1020 static void stellaris_adc_reset(stellaris_adc_state
*s
)
1024 for (n
= 0; n
< 4; n
++) {
1027 s
->fifo
[n
].state
= STELLARIS_ADC_FIFO_EMPTY
;
1031 static uint64_t stellaris_adc_read(void *opaque
, hwaddr offset
,
1034 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1036 /* TODO: Implement this. */
1037 if (offset
>= 0x40 && offset
< 0xc0) {
1039 n
= (offset
- 0x40) >> 5;
1040 switch (offset
& 0x1f) {
1041 case 0x00: /* SSMUX */
1043 case 0x04: /* SSCTL */
1045 case 0x08: /* SSFIFO */
1046 return stellaris_adc_fifo_read(s
, n
);
1047 case 0x0c: /* SSFSTAT */
1048 return s
->fifo
[n
].state
;
1054 case 0x00: /* ACTSS */
1056 case 0x04: /* RIS */
1060 case 0x0c: /* ISC */
1061 return s
->ris
& s
->im
;
1062 case 0x10: /* OSTAT */
1064 case 0x14: /* EMUX */
1066 case 0x18: /* USTAT */
1068 case 0x20: /* SSPRI */
1070 case 0x30: /* SAC */
1073 qemu_log_mask(LOG_GUEST_ERROR
,
1074 "stellaris_adc: read at bad offset 0x%x\n", (int)offset
);
1079 static void stellaris_adc_write(void *opaque
, hwaddr offset
,
1080 uint64_t value
, unsigned size
)
1082 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1084 /* TODO: Implement this. */
1085 if (offset
>= 0x40 && offset
< 0xc0) {
1087 n
= (offset
- 0x40) >> 5;
1088 switch (offset
& 0x1f) {
1089 case 0x00: /* SSMUX */
1090 s
->ssmux
[n
] = value
& 0x33333333;
1092 case 0x04: /* SSCTL */
1094 qemu_log_mask(LOG_UNIMP
,
1095 "ADC: Unimplemented sequence %" PRIx64
"\n",
1098 s
->ssctl
[n
] = value
;
1105 case 0x00: /* ACTSS */
1106 s
->actss
= value
& 0xf;
1111 case 0x0c: /* ISC */
1114 case 0x10: /* OSTAT */
1117 case 0x14: /* EMUX */
1120 case 0x18: /* USTAT */
1123 case 0x20: /* SSPRI */
1126 case 0x28: /* PSSI */
1127 qemu_log_mask(LOG_UNIMP
, "ADC: sample initiate unimplemented");
1129 case 0x30: /* SAC */
1133 qemu_log_mask(LOG_GUEST_ERROR
,
1134 "stellaris_adc: write at bad offset 0x%x\n", (int)offset
);
1136 stellaris_adc_update(s
);
1139 static const MemoryRegionOps stellaris_adc_ops
= {
1140 .read
= stellaris_adc_read
,
1141 .write
= stellaris_adc_write
,
1142 .endianness
= DEVICE_NATIVE_ENDIAN
,
1145 static const VMStateDescription vmstate_stellaris_adc
= {
1146 .name
= "stellaris_adc",
1148 .minimum_version_id
= 1,
1149 .fields
= (VMStateField
[]) {
1150 VMSTATE_UINT32(actss
, stellaris_adc_state
),
1151 VMSTATE_UINT32(ris
, stellaris_adc_state
),
1152 VMSTATE_UINT32(im
, stellaris_adc_state
),
1153 VMSTATE_UINT32(emux
, stellaris_adc_state
),
1154 VMSTATE_UINT32(ostat
, stellaris_adc_state
),
1155 VMSTATE_UINT32(ustat
, stellaris_adc_state
),
1156 VMSTATE_UINT32(sspri
, stellaris_adc_state
),
1157 VMSTATE_UINT32(sac
, stellaris_adc_state
),
1158 VMSTATE_UINT32(fifo
[0].state
, stellaris_adc_state
),
1159 VMSTATE_UINT32_ARRAY(fifo
[0].data
, stellaris_adc_state
, 16),
1160 VMSTATE_UINT32(ssmux
[0], stellaris_adc_state
),
1161 VMSTATE_UINT32(ssctl
[0], stellaris_adc_state
),
1162 VMSTATE_UINT32(fifo
[1].state
, stellaris_adc_state
),
1163 VMSTATE_UINT32_ARRAY(fifo
[1].data
, stellaris_adc_state
, 16),
1164 VMSTATE_UINT32(ssmux
[1], stellaris_adc_state
),
1165 VMSTATE_UINT32(ssctl
[1], stellaris_adc_state
),
1166 VMSTATE_UINT32(fifo
[2].state
, stellaris_adc_state
),
1167 VMSTATE_UINT32_ARRAY(fifo
[2].data
, stellaris_adc_state
, 16),
1168 VMSTATE_UINT32(ssmux
[2], stellaris_adc_state
),
1169 VMSTATE_UINT32(ssctl
[2], stellaris_adc_state
),
1170 VMSTATE_UINT32(fifo
[3].state
, stellaris_adc_state
),
1171 VMSTATE_UINT32_ARRAY(fifo
[3].data
, stellaris_adc_state
, 16),
1172 VMSTATE_UINT32(ssmux
[3], stellaris_adc_state
),
1173 VMSTATE_UINT32(ssctl
[3], stellaris_adc_state
),
1174 VMSTATE_UINT32(noise
, stellaris_adc_state
),
1175 VMSTATE_END_OF_LIST()
1179 static void stellaris_adc_init(Object
*obj
)
1181 DeviceState
*dev
= DEVICE(obj
);
1182 stellaris_adc_state
*s
= STELLARIS_ADC(obj
);
1183 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
1186 for (n
= 0; n
< 4; n
++) {
1187 sysbus_init_irq(sbd
, &s
->irq
[n
]);
1190 memory_region_init_io(&s
->iomem
, obj
, &stellaris_adc_ops
, s
,
1192 sysbus_init_mmio(sbd
, &s
->iomem
);
1193 stellaris_adc_reset(s
);
1194 qdev_init_gpio_in(dev
, stellaris_adc_trigger
, 1);
1198 void do_sys_reset(void *opaque
, int n
, int level
)
1201 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
1206 static stellaris_board_info stellaris_boards
[] = {
1210 0x001f001f, /* dc0 */
1220 0x00ff007f, /* dc0 */
1225 BP_OLED_SSI
| BP_GAMEPAD
1229 static void stellaris_init(MachineState
*ms
, stellaris_board_info
*board
)
1231 static const int uart_irq
[] = {5, 6, 33, 34};
1232 static const int timer_irq
[] = {19, 21, 23, 35};
1233 static const uint32_t gpio_addr
[7] =
1234 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1235 0x40024000, 0x40025000, 0x40026000};
1236 static const int gpio_irq
[7] = {0, 1, 2, 3, 4, 30, 31};
1238 /* Memory map of SoC devices, from
1239 * Stellaris LM3S6965 Microcontroller Data Sheet (rev I)
1240 * http://www.ti.com/lit/ds/symlink/lm3s6965.pdf
1242 * 40000000 wdtimer (unimplemented)
1243 * 40002000 i2c (unimplemented)
1253 * 40021000 i2c (unimplemented)
1257 * 40028000 PWM (unimplemented)
1258 * 4002c000 QEI (unimplemented)
1259 * 4002d000 QEI (unimplemented)
1265 * 4003c000 analogue comparator (unimplemented)
1267 * 400fc000 hibernation module (unimplemented)
1268 * 400fd000 flash memory control (unimplemented)
1269 * 400fe000 system control
1272 DeviceState
*gpio_dev
[7], *nvic
;
1273 qemu_irq gpio_in
[7][8];
1274 qemu_irq gpio_out
[7][8];
1283 MemoryRegion
*sram
= g_new(MemoryRegion
, 1);
1284 MemoryRegion
*flash
= g_new(MemoryRegion
, 1);
1285 MemoryRegion
*system_memory
= get_system_memory();
1287 flash_size
= (((board
->dc0
& 0xffff) + 1) << 1) * 1024;
1288 sram_size
= ((board
->dc0
>> 18) + 1) * 1024;
1290 /* Flash programming is done via the SCU, so pretend it is ROM. */
1291 memory_region_init_ram(flash
, NULL
, "stellaris.flash", flash_size
,
1293 memory_region_set_readonly(flash
, true);
1294 memory_region_add_subregion(system_memory
, 0, flash
);
1296 memory_region_init_ram(sram
, NULL
, "stellaris.sram", sram_size
,
1298 memory_region_add_subregion(system_memory
, 0x20000000, sram
);
1300 nvic
= armv7m_init(system_memory
, flash_size
, NUM_IRQ_LINES
,
1301 ms
->kernel_filename
, ms
->cpu_type
);
1303 qdev_connect_gpio_out_named(nvic
, "SYSRESETREQ", 0,
1304 qemu_allocate_irq(&do_sys_reset
, NULL
, 0));
1306 if (board
->dc1
& (1 << 16)) {
1307 dev
= sysbus_create_varargs(TYPE_STELLARIS_ADC
, 0x40038000,
1308 qdev_get_gpio_in(nvic
, 14),
1309 qdev_get_gpio_in(nvic
, 15),
1310 qdev_get_gpio_in(nvic
, 16),
1311 qdev_get_gpio_in(nvic
, 17),
1313 adc
= qdev_get_gpio_in(dev
, 0);
1317 for (i
= 0; i
< 4; i
++) {
1318 if (board
->dc2
& (0x10000 << i
)) {
1319 dev
= sysbus_create_simple(TYPE_STELLARIS_GPTM
,
1320 0x40030000 + i
* 0x1000,
1321 qdev_get_gpio_in(nvic
, timer_irq
[i
]));
1322 /* TODO: This is incorrect, but we get away with it because
1323 the ADC output is only ever pulsed. */
1324 qdev_connect_gpio_out(dev
, 0, adc
);
1328 stellaris_sys_init(0x400fe000, qdev_get_gpio_in(nvic
, 28),
1329 board
, nd_table
[0].macaddr
.a
);
1331 for (i
= 0; i
< 7; i
++) {
1332 if (board
->dc4
& (1 << i
)) {
1333 gpio_dev
[i
] = sysbus_create_simple("pl061_luminary", gpio_addr
[i
],
1334 qdev_get_gpio_in(nvic
,
1336 for (j
= 0; j
< 8; j
++) {
1337 gpio_in
[i
][j
] = qdev_get_gpio_in(gpio_dev
[i
], j
);
1338 gpio_out
[i
][j
] = NULL
;
1343 if (board
->dc2
& (1 << 12)) {
1344 dev
= sysbus_create_simple(TYPE_STELLARIS_I2C
, 0x40020000,
1345 qdev_get_gpio_in(nvic
, 8));
1346 i2c
= (I2CBus
*)qdev_get_child_bus(dev
, "i2c");
1347 if (board
->peripherals
& BP_OLED_I2C
) {
1348 i2c_create_slave(i2c
, "ssd0303", 0x3d);
1352 for (i
= 0; i
< 4; i
++) {
1353 if (board
->dc2
& (1 << i
)) {
1354 pl011_luminary_create(0x4000c000 + i
* 0x1000,
1355 qdev_get_gpio_in(nvic
, uart_irq
[i
]),
1359 if (board
->dc2
& (1 << 4)) {
1360 dev
= sysbus_create_simple("pl022", 0x40008000,
1361 qdev_get_gpio_in(nvic
, 7));
1362 if (board
->peripherals
& BP_OLED_SSI
) {
1365 DeviceState
*ssddev
;
1367 /* Some boards have both an OLED controller and SD card connected to
1368 * the same SSI port, with the SD card chip select connected to a
1369 * GPIO pin. Technically the OLED chip select is connected to the
1370 * SSI Fss pin. We do not bother emulating that as both devices
1371 * should never be selected simultaneously, and our OLED controller
1372 * ignores stray 0xff commands that occur when deselecting the SD
1375 bus
= qdev_get_child_bus(dev
, "ssi");
1377 sddev
= ssi_create_slave(bus
, "ssi-sd");
1378 ssddev
= ssi_create_slave(bus
, "ssd0323");
1379 gpio_out
[GPIO_D
][0] = qemu_irq_split(
1380 qdev_get_gpio_in_named(sddev
, SSI_GPIO_CS
, 0),
1381 qdev_get_gpio_in_named(ssddev
, SSI_GPIO_CS
, 0));
1382 gpio_out
[GPIO_C
][7] = qdev_get_gpio_in(ssddev
, 0);
1384 /* Make sure the select pin is high. */
1385 qemu_irq_raise(gpio_out
[GPIO_D
][0]);
1388 if (board
->dc4
& (1 << 28)) {
1391 qemu_check_nic_model(&nd_table
[0], "stellaris");
1393 enet
= qdev_create(NULL
, "stellaris_enet");
1394 qdev_set_nic_properties(enet
, &nd_table
[0]);
1395 qdev_init_nofail(enet
);
1396 sysbus_mmio_map(SYS_BUS_DEVICE(enet
), 0, 0x40048000);
1397 sysbus_connect_irq(SYS_BUS_DEVICE(enet
), 0, qdev_get_gpio_in(nvic
, 42));
1399 if (board
->peripherals
& BP_GAMEPAD
) {
1400 qemu_irq gpad_irq
[5];
1401 static const int gpad_keycode
[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1403 gpad_irq
[0] = qemu_irq_invert(gpio_in
[GPIO_E
][0]); /* up */
1404 gpad_irq
[1] = qemu_irq_invert(gpio_in
[GPIO_E
][1]); /* down */
1405 gpad_irq
[2] = qemu_irq_invert(gpio_in
[GPIO_E
][2]); /* left */
1406 gpad_irq
[3] = qemu_irq_invert(gpio_in
[GPIO_E
][3]); /* right */
1407 gpad_irq
[4] = qemu_irq_invert(gpio_in
[GPIO_F
][1]); /* select */
1409 stellaris_gamepad_init(5, gpad_irq
, gpad_keycode
);
1411 for (i
= 0; i
< 7; i
++) {
1412 if (board
->dc4
& (1 << i
)) {
1413 for (j
= 0; j
< 8; j
++) {
1414 if (gpio_out
[i
][j
]) {
1415 qdev_connect_gpio_out(gpio_dev
[i
], j
, gpio_out
[i
][j
]);
1421 /* Add dummy regions for the devices we don't implement yet,
1422 * so guest accesses don't cause unlogged crashes.
1424 create_unimplemented_device("wdtimer", 0x40000000, 0x1000);
1425 create_unimplemented_device("i2c-0", 0x40002000, 0x1000);
1426 create_unimplemented_device("i2c-2", 0x40021000, 0x1000);
1427 create_unimplemented_device("PWM", 0x40028000, 0x1000);
1428 create_unimplemented_device("QEI-0", 0x4002c000, 0x1000);
1429 create_unimplemented_device("QEI-1", 0x4002d000, 0x1000);
1430 create_unimplemented_device("analogue-comparator", 0x4003c000, 0x1000);
1431 create_unimplemented_device("hibernation", 0x400fc000, 0x1000);
1432 create_unimplemented_device("flash-control", 0x400fd000, 0x1000);
1435 /* FIXME: Figure out how to generate these from stellaris_boards. */
1436 static void lm3s811evb_init(MachineState
*machine
)
1438 stellaris_init(machine
, &stellaris_boards
[0]);
1441 static void lm3s6965evb_init(MachineState
*machine
)
1443 stellaris_init(machine
, &stellaris_boards
[1]);
1446 static void lm3s811evb_class_init(ObjectClass
*oc
, void *data
)
1448 MachineClass
*mc
= MACHINE_CLASS(oc
);
1450 mc
->desc
= "Stellaris LM3S811EVB";
1451 mc
->init
= lm3s811evb_init
;
1452 mc
->ignore_memory_transaction_failures
= true;
1453 mc
->default_cpu_type
= ARM_CPU_TYPE_NAME("cortex-m3");
1456 static const TypeInfo lm3s811evb_type
= {
1457 .name
= MACHINE_TYPE_NAME("lm3s811evb"),
1458 .parent
= TYPE_MACHINE
,
1459 .class_init
= lm3s811evb_class_init
,
1462 static void lm3s6965evb_class_init(ObjectClass
*oc
, void *data
)
1464 MachineClass
*mc
= MACHINE_CLASS(oc
);
1466 mc
->desc
= "Stellaris LM3S6965EVB";
1467 mc
->init
= lm3s6965evb_init
;
1468 mc
->ignore_memory_transaction_failures
= true;
1469 mc
->default_cpu_type
= ARM_CPU_TYPE_NAME("cortex-m3");
1472 static const TypeInfo lm3s6965evb_type
= {
1473 .name
= MACHINE_TYPE_NAME("lm3s6965evb"),
1474 .parent
= TYPE_MACHINE
,
1475 .class_init
= lm3s6965evb_class_init
,
1478 static void stellaris_machine_init(void)
1480 type_register_static(&lm3s811evb_type
);
1481 type_register_static(&lm3s6965evb_type
);
1484 type_init(stellaris_machine_init
)
1486 static void stellaris_i2c_class_init(ObjectClass
*klass
, void *data
)
1488 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1490 dc
->vmsd
= &vmstate_stellaris_i2c
;
1493 static const TypeInfo stellaris_i2c_info
= {
1494 .name
= TYPE_STELLARIS_I2C
,
1495 .parent
= TYPE_SYS_BUS_DEVICE
,
1496 .instance_size
= sizeof(stellaris_i2c_state
),
1497 .instance_init
= stellaris_i2c_init
,
1498 .class_init
= stellaris_i2c_class_init
,
1501 static void stellaris_gptm_class_init(ObjectClass
*klass
, void *data
)
1503 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1505 dc
->vmsd
= &vmstate_stellaris_gptm
;
1508 static const TypeInfo stellaris_gptm_info
= {
1509 .name
= TYPE_STELLARIS_GPTM
,
1510 .parent
= TYPE_SYS_BUS_DEVICE
,
1511 .instance_size
= sizeof(gptm_state
),
1512 .instance_init
= stellaris_gptm_init
,
1513 .class_init
= stellaris_gptm_class_init
,
1516 static void stellaris_adc_class_init(ObjectClass
*klass
, void *data
)
1518 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1520 dc
->vmsd
= &vmstate_stellaris_adc
;
1523 static const TypeInfo stellaris_adc_info
= {
1524 .name
= TYPE_STELLARIS_ADC
,
1525 .parent
= TYPE_SYS_BUS_DEVICE
,
1526 .instance_size
= sizeof(stellaris_adc_state
),
1527 .instance_init
= stellaris_adc_init
,
1528 .class_init
= stellaris_adc_class_init
,
1531 static void stellaris_register_types(void)
1533 type_register_static(&stellaris_i2c_info
);
1534 type_register_static(&stellaris_gptm_info
);
1535 type_register_static(&stellaris_adc_info
);
1538 type_init(stellaris_register_types
)