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"
34 #define BP_OLED_I2C 0x01
35 #define BP_OLED_SSI 0x02
36 #define BP_GAMEPAD 0x04
38 #define NUM_IRQ_LINES 64
40 typedef const struct {
50 } stellaris_board_info
;
52 /* General purpose timer module. */
54 #define TYPE_STELLARIS_GPTM "stellaris-gptm"
55 #define STELLARIS_GPTM(obj) \
56 OBJECT_CHECK(gptm_state, (obj), TYPE_STELLARIS_GPTM)
58 typedef struct gptm_state
{
59 SysBusDevice parent_obj
;
70 uint32_t match_prescale
[2];
73 struct gptm_state
*opaque
[2];
75 /* The timers have an alternate output used to trigger the ADC. */
80 static void gptm_update_irq(gptm_state
*s
)
83 level
= (s
->state
& s
->mask
) != 0;
84 qemu_set_irq(s
->irq
, level
);
87 static void gptm_stop(gptm_state
*s
, int n
)
89 timer_del(s
->timer
[n
]);
92 static void gptm_reload(gptm_state
*s
, int n
, int reset
)
96 tick
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
100 if (s
->config
== 0) {
101 /* 32-bit CountDown. */
103 count
= s
->load
[0] | (s
->load
[1] << 16);
104 tick
+= (int64_t)count
* system_clock_scale
;
105 } else if (s
->config
== 1) {
106 /* 32-bit RTC. 1Hz tick. */
107 tick
+= NANOSECONDS_PER_SECOND
;
108 } else if (s
->mode
[n
] == 0xa) {
109 /* PWM mode. Not implemented. */
111 qemu_log_mask(LOG_UNIMP
,
112 "GPTM: 16-bit timer mode unimplemented: 0x%x\n",
117 timer_mod(s
->timer
[n
], tick
);
120 static void gptm_tick(void *opaque
)
122 gptm_state
**p
= (gptm_state
**)opaque
;
128 if (s
->config
== 0) {
130 if ((s
->control
& 0x20)) {
131 /* Output trigger. */
132 qemu_irq_pulse(s
->trigger
);
134 if (s
->mode
[0] & 1) {
139 gptm_reload(s
, 0, 0);
141 } else if (s
->config
== 1) {
145 match
= s
->match
[0] | (s
->match
[1] << 16);
151 gptm_reload(s
, 0, 0);
152 } else if (s
->mode
[n
] == 0xa) {
153 /* PWM mode. Not implemented. */
155 qemu_log_mask(LOG_UNIMP
,
156 "GPTM: 16-bit timer mode unimplemented: 0x%x\n",
162 static uint64_t gptm_read(void *opaque
, hwaddr offset
,
165 gptm_state
*s
= (gptm_state
*)opaque
;
170 case 0x04: /* TAMR */
172 case 0x08: /* TBMR */
181 return s
->state
& s
->mask
;
184 case 0x28: /* TAILR */
185 return s
->load
[0] | ((s
->config
< 4) ? (s
->load
[1] << 16) : 0);
186 case 0x2c: /* TBILR */
188 case 0x30: /* TAMARCHR */
189 return s
->match
[0] | ((s
->config
< 4) ? (s
->match
[1] << 16) : 0);
190 case 0x34: /* TBMATCHR */
192 case 0x38: /* TAPR */
193 return s
->prescale
[0];
194 case 0x3c: /* TBPR */
195 return s
->prescale
[1];
196 case 0x40: /* TAPMR */
197 return s
->match_prescale
[0];
198 case 0x44: /* TBPMR */
199 return s
->match_prescale
[1];
201 if (s
->config
== 1) {
204 qemu_log_mask(LOG_UNIMP
,
205 "GPTM: read of TAR but timer read not supported");
208 qemu_log_mask(LOG_UNIMP
,
209 "GPTM: read of TBR but timer read not supported");
212 qemu_log_mask(LOG_GUEST_ERROR
,
213 "GPTM: read at bad offset 0x%x\n", (int)offset
);
218 static void gptm_write(void *opaque
, hwaddr offset
,
219 uint64_t value
, unsigned size
)
221 gptm_state
*s
= (gptm_state
*)opaque
;
224 /* The timers should be disabled before changing the configuration.
225 We take advantage of this and defer everything until the timer
231 case 0x04: /* TAMR */
234 case 0x08: /* TBMR */
240 /* TODO: Implement pause. */
241 if ((oldval
^ value
) & 1) {
243 gptm_reload(s
, 0, 1);
248 if (((oldval
^ value
) & 0x100) && s
->config
>= 4) {
250 gptm_reload(s
, 1, 1);
257 s
->mask
= value
& 0x77;
263 case 0x28: /* TAILR */
264 s
->load
[0] = value
& 0xffff;
266 s
->load
[1] = value
>> 16;
269 case 0x2c: /* TBILR */
270 s
->load
[1] = value
& 0xffff;
272 case 0x30: /* TAMARCHR */
273 s
->match
[0] = value
& 0xffff;
275 s
->match
[1] = value
>> 16;
278 case 0x34: /* TBMATCHR */
279 s
->match
[1] = value
>> 16;
281 case 0x38: /* TAPR */
282 s
->prescale
[0] = value
;
284 case 0x3c: /* TBPR */
285 s
->prescale
[1] = value
;
287 case 0x40: /* TAPMR */
288 s
->match_prescale
[0] = value
;
290 case 0x44: /* TBPMR */
291 s
->match_prescale
[0] = value
;
294 qemu_log_mask(LOG_GUEST_ERROR
,
295 "GPTM: read at bad offset 0x%x\n", (int)offset
);
300 static const MemoryRegionOps gptm_ops
= {
303 .endianness
= DEVICE_NATIVE_ENDIAN
,
306 static const VMStateDescription vmstate_stellaris_gptm
= {
307 .name
= "stellaris_gptm",
309 .minimum_version_id
= 1,
310 .fields
= (VMStateField
[]) {
311 VMSTATE_UINT32(config
, gptm_state
),
312 VMSTATE_UINT32_ARRAY(mode
, gptm_state
, 2),
313 VMSTATE_UINT32(control
, gptm_state
),
314 VMSTATE_UINT32(state
, gptm_state
),
315 VMSTATE_UINT32(mask
, gptm_state
),
317 VMSTATE_UINT32_ARRAY(load
, gptm_state
, 2),
318 VMSTATE_UINT32_ARRAY(match
, gptm_state
, 2),
319 VMSTATE_UINT32_ARRAY(prescale
, gptm_state
, 2),
320 VMSTATE_UINT32_ARRAY(match_prescale
, gptm_state
, 2),
321 VMSTATE_UINT32(rtc
, gptm_state
),
322 VMSTATE_INT64_ARRAY(tick
, gptm_state
, 2),
323 VMSTATE_TIMER_PTR_ARRAY(timer
, gptm_state
, 2),
324 VMSTATE_END_OF_LIST()
328 static void stellaris_gptm_init(Object
*obj
)
330 DeviceState
*dev
= DEVICE(obj
);
331 gptm_state
*s
= STELLARIS_GPTM(obj
);
332 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
334 sysbus_init_irq(sbd
, &s
->irq
);
335 qdev_init_gpio_out(dev
, &s
->trigger
, 1);
337 memory_region_init_io(&s
->iomem
, obj
, &gptm_ops
, s
,
339 sysbus_init_mmio(sbd
, &s
->iomem
);
341 s
->opaque
[0] = s
->opaque
[1] = s
;
342 s
->timer
[0] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[0]);
343 s
->timer
[1] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[1]);
347 /* System controller. */
366 stellaris_board_info
*board
;
369 static void ssys_update(ssys_state
*s
)
371 qemu_set_irq(s
->irq
, (s
->int_status
& s
->int_mask
) != 0);
374 static uint32_t pllcfg_sandstorm
[16] = {
376 0x1ae0, /* 1.8432 Mhz */
378 0xd573, /* 2.4576 Mhz */
379 0x37a6, /* 3.57954 Mhz */
380 0x1ae2, /* 3.6864 Mhz */
382 0x98bc, /* 4.906 Mhz */
383 0x935b, /* 4.9152 Mhz */
385 0x4dee, /* 5.12 Mhz */
387 0x75db, /* 6.144 Mhz */
388 0x1ae6, /* 7.3728 Mhz */
390 0x585b /* 8.192 Mhz */
393 static uint32_t pllcfg_fury
[16] = {
395 0x1b20, /* 1.8432 Mhz */
397 0xf42b, /* 2.4576 Mhz */
398 0x37e3, /* 3.57954 Mhz */
399 0x1b21, /* 3.6864 Mhz */
401 0x98ee, /* 4.906 Mhz */
402 0xd5b4, /* 4.9152 Mhz */
404 0x4e27, /* 5.12 Mhz */
406 0xec1c, /* 6.144 Mhz */
407 0x1b23, /* 7.3728 Mhz */
409 0xb11c /* 8.192 Mhz */
412 #define DID0_VER_MASK 0x70000000
413 #define DID0_VER_0 0x00000000
414 #define DID0_VER_1 0x10000000
416 #define DID0_CLASS_MASK 0x00FF0000
417 #define DID0_CLASS_SANDSTORM 0x00000000
418 #define DID0_CLASS_FURY 0x00010000
420 static int ssys_board_class(const ssys_state
*s
)
422 uint32_t did0
= s
->board
->did0
;
423 switch (did0
& DID0_VER_MASK
) {
425 return DID0_CLASS_SANDSTORM
;
427 switch (did0
& DID0_CLASS_MASK
) {
428 case DID0_CLASS_SANDSTORM
:
429 case DID0_CLASS_FURY
:
430 return did0
& DID0_CLASS_MASK
;
432 /* for unknown classes, fall through */
434 /* This can only happen if the hardwired constant did0 value
435 * in this board's stellaris_board_info struct is wrong.
437 g_assert_not_reached();
441 static uint64_t ssys_read(void *opaque
, hwaddr offset
,
444 ssys_state
*s
= (ssys_state
*)opaque
;
447 case 0x000: /* DID0 */
448 return s
->board
->did0
;
449 case 0x004: /* DID1 */
450 return s
->board
->did1
;
451 case 0x008: /* DC0 */
452 return s
->board
->dc0
;
453 case 0x010: /* DC1 */
454 return s
->board
->dc1
;
455 case 0x014: /* DC2 */
456 return s
->board
->dc2
;
457 case 0x018: /* DC3 */
458 return s
->board
->dc3
;
459 case 0x01c: /* DC4 */
460 return s
->board
->dc4
;
461 case 0x030: /* PBORCTL */
463 case 0x034: /* LDOPCTL */
465 case 0x040: /* SRCR0 */
467 case 0x044: /* SRCR1 */
469 case 0x048: /* SRCR2 */
471 case 0x050: /* RIS */
472 return s
->int_status
;
473 case 0x054: /* IMC */
475 case 0x058: /* MISC */
476 return s
->int_status
& s
->int_mask
;
477 case 0x05c: /* RESC */
479 case 0x060: /* RCC */
481 case 0x064: /* PLLCFG */
484 xtal
= (s
->rcc
>> 6) & 0xf;
485 switch (ssys_board_class(s
)) {
486 case DID0_CLASS_FURY
:
487 return pllcfg_fury
[xtal
];
488 case DID0_CLASS_SANDSTORM
:
489 return pllcfg_sandstorm
[xtal
];
491 g_assert_not_reached();
494 case 0x070: /* RCC2 */
496 case 0x100: /* RCGC0 */
498 case 0x104: /* RCGC1 */
500 case 0x108: /* RCGC2 */
502 case 0x110: /* SCGC0 */
504 case 0x114: /* SCGC1 */
506 case 0x118: /* SCGC2 */
508 case 0x120: /* DCGC0 */
510 case 0x124: /* DCGC1 */
512 case 0x128: /* DCGC2 */
514 case 0x150: /* CLKVCLR */
516 case 0x160: /* LDOARST */
518 case 0x1e0: /* USER0 */
520 case 0x1e4: /* USER1 */
523 qemu_log_mask(LOG_GUEST_ERROR
,
524 "SSYS: read at bad offset 0x%x\n", (int)offset
);
529 static bool ssys_use_rcc2(ssys_state
*s
)
531 return (s
->rcc2
>> 31) & 0x1;
535 * Caculate the sys. clock period in ms.
537 static void ssys_calculate_system_clock(ssys_state
*s
)
539 if (ssys_use_rcc2(s
)) {
540 system_clock_scale
= 5 * (((s
->rcc2
>> 23) & 0x3f) + 1);
542 system_clock_scale
= 5 * (((s
->rcc
>> 23) & 0xf) + 1);
546 static void ssys_write(void *opaque
, hwaddr offset
,
547 uint64_t value
, unsigned size
)
549 ssys_state
*s
= (ssys_state
*)opaque
;
552 case 0x030: /* PBORCTL */
553 s
->pborctl
= value
& 0xffff;
555 case 0x034: /* LDOPCTL */
556 s
->ldopctl
= value
& 0x1f;
558 case 0x040: /* SRCR0 */
559 case 0x044: /* SRCR1 */
560 case 0x048: /* SRCR2 */
561 fprintf(stderr
, "Peripheral reset not implemented\n");
563 case 0x054: /* IMC */
564 s
->int_mask
= value
& 0x7f;
566 case 0x058: /* MISC */
567 s
->int_status
&= ~value
;
569 case 0x05c: /* RESC */
570 s
->resc
= value
& 0x3f;
572 case 0x060: /* RCC */
573 if ((s
->rcc
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
575 s
->int_status
|= (1 << 6);
578 ssys_calculate_system_clock(s
);
580 case 0x070: /* RCC2 */
581 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
585 if ((s
->rcc2
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
587 s
->int_status
|= (1 << 6);
590 ssys_calculate_system_clock(s
);
592 case 0x100: /* RCGC0 */
595 case 0x104: /* RCGC1 */
598 case 0x108: /* RCGC2 */
601 case 0x110: /* SCGC0 */
604 case 0x114: /* SCGC1 */
607 case 0x118: /* SCGC2 */
610 case 0x120: /* DCGC0 */
613 case 0x124: /* DCGC1 */
616 case 0x128: /* DCGC2 */
619 case 0x150: /* CLKVCLR */
622 case 0x160: /* LDOARST */
626 qemu_log_mask(LOG_GUEST_ERROR
,
627 "SSYS: write at bad offset 0x%x\n", (int)offset
);
632 static const MemoryRegionOps ssys_ops
= {
635 .endianness
= DEVICE_NATIVE_ENDIAN
,
638 static void ssys_reset(void *opaque
)
640 ssys_state
*s
= (ssys_state
*)opaque
;
645 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
648 s
->rcc2
= 0x07802810;
653 ssys_calculate_system_clock(s
);
656 static int stellaris_sys_post_load(void *opaque
, int version_id
)
658 ssys_state
*s
= opaque
;
660 ssys_calculate_system_clock(s
);
665 static const VMStateDescription vmstate_stellaris_sys
= {
666 .name
= "stellaris_sys",
668 .minimum_version_id
= 1,
669 .post_load
= stellaris_sys_post_load
,
670 .fields
= (VMStateField
[]) {
671 VMSTATE_UINT32(pborctl
, ssys_state
),
672 VMSTATE_UINT32(ldopctl
, ssys_state
),
673 VMSTATE_UINT32(int_mask
, ssys_state
),
674 VMSTATE_UINT32(int_status
, ssys_state
),
675 VMSTATE_UINT32(resc
, ssys_state
),
676 VMSTATE_UINT32(rcc
, ssys_state
),
677 VMSTATE_UINT32_V(rcc2
, ssys_state
, 2),
678 VMSTATE_UINT32_ARRAY(rcgc
, ssys_state
, 3),
679 VMSTATE_UINT32_ARRAY(scgc
, ssys_state
, 3),
680 VMSTATE_UINT32_ARRAY(dcgc
, ssys_state
, 3),
681 VMSTATE_UINT32(clkvclr
, ssys_state
),
682 VMSTATE_UINT32(ldoarst
, ssys_state
),
683 VMSTATE_END_OF_LIST()
687 static int stellaris_sys_init(uint32_t base
, qemu_irq irq
,
688 stellaris_board_info
* board
,
693 s
= g_new0(ssys_state
, 1);
696 /* Most devices come preprogrammed with a MAC address in the user data. */
697 s
->user0
= macaddr
[0] | (macaddr
[1] << 8) | (macaddr
[2] << 16);
698 s
->user1
= macaddr
[3] | (macaddr
[4] << 8) | (macaddr
[5] << 16);
700 memory_region_init_io(&s
->iomem
, NULL
, &ssys_ops
, s
, "ssys", 0x00001000);
701 memory_region_add_subregion(get_system_memory(), base
, &s
->iomem
);
703 vmstate_register(NULL
, -1, &vmstate_stellaris_sys
, s
);
708 /* I2C controller. */
710 #define TYPE_STELLARIS_I2C "stellaris-i2c"
711 #define STELLARIS_I2C(obj) \
712 OBJECT_CHECK(stellaris_i2c_state, (obj), TYPE_STELLARIS_I2C)
715 SysBusDevice parent_obj
;
727 } stellaris_i2c_state
;
729 #define STELLARIS_I2C_MCS_BUSY 0x01
730 #define STELLARIS_I2C_MCS_ERROR 0x02
731 #define STELLARIS_I2C_MCS_ADRACK 0x04
732 #define STELLARIS_I2C_MCS_DATACK 0x08
733 #define STELLARIS_I2C_MCS_ARBLST 0x10
734 #define STELLARIS_I2C_MCS_IDLE 0x20
735 #define STELLARIS_I2C_MCS_BUSBSY 0x40
737 static uint64_t stellaris_i2c_read(void *opaque
, hwaddr offset
,
740 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
746 /* We don't emulate timing, so the controller is never busy. */
747 return s
->mcs
| STELLARIS_I2C_MCS_IDLE
;
750 case 0x0c: /* MTPR */
752 case 0x10: /* MIMR */
754 case 0x14: /* MRIS */
756 case 0x18: /* MMIS */
757 return s
->mris
& s
->mimr
;
761 qemu_log_mask(LOG_GUEST_ERROR
,
762 "stellaris_i2c: read at bad offset 0x%x\n", (int)offset
);
767 static void stellaris_i2c_update(stellaris_i2c_state
*s
)
771 level
= (s
->mris
& s
->mimr
) != 0;
772 qemu_set_irq(s
->irq
, level
);
775 static void stellaris_i2c_write(void *opaque
, hwaddr offset
,
776 uint64_t value
, unsigned size
)
778 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
782 s
->msa
= value
& 0xff;
785 if ((s
->mcr
& 0x10) == 0) {
786 /* Disabled. Do nothing. */
789 /* Grab the bus if this is starting a transfer. */
790 if ((value
& 2) && (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
791 if (i2c_start_transfer(s
->bus
, s
->msa
>> 1, s
->msa
& 1)) {
792 s
->mcs
|= STELLARIS_I2C_MCS_ARBLST
;
794 s
->mcs
&= ~STELLARIS_I2C_MCS_ARBLST
;
795 s
->mcs
|= STELLARIS_I2C_MCS_BUSBSY
;
798 /* If we don't have the bus then indicate an error. */
799 if (!i2c_bus_busy(s
->bus
)
800 || (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
801 s
->mcs
|= STELLARIS_I2C_MCS_ERROR
;
804 s
->mcs
&= ~STELLARIS_I2C_MCS_ERROR
;
806 /* Transfer a byte. */
807 /* TODO: Handle errors. */
810 s
->mdr
= i2c_recv(s
->bus
) & 0xff;
813 i2c_send(s
->bus
, s
->mdr
);
815 /* Raise an interrupt. */
819 /* Finish transfer. */
820 i2c_end_transfer(s
->bus
);
821 s
->mcs
&= ~STELLARIS_I2C_MCS_BUSBSY
;
825 s
->mdr
= value
& 0xff;
827 case 0x0c: /* MTPR */
828 s
->mtpr
= value
& 0xff;
830 case 0x10: /* MIMR */
833 case 0x1c: /* MICR */
838 qemu_log_mask(LOG_UNIMP
, "stellaris_i2c: Loopback not implemented");
841 qemu_log_mask(LOG_UNIMP
,
842 "stellaris_i2c: Slave mode not implemented");
844 s
->mcr
= value
& 0x31;
847 qemu_log_mask(LOG_GUEST_ERROR
,
848 "stellaris_i2c: write at bad offset 0x%x\n", (int)offset
);
850 stellaris_i2c_update(s
);
853 static void stellaris_i2c_reset(stellaris_i2c_state
*s
)
855 if (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
)
856 i2c_end_transfer(s
->bus
);
865 stellaris_i2c_update(s
);
868 static const MemoryRegionOps stellaris_i2c_ops
= {
869 .read
= stellaris_i2c_read
,
870 .write
= stellaris_i2c_write
,
871 .endianness
= DEVICE_NATIVE_ENDIAN
,
874 static const VMStateDescription vmstate_stellaris_i2c
= {
875 .name
= "stellaris_i2c",
877 .minimum_version_id
= 1,
878 .fields
= (VMStateField
[]) {
879 VMSTATE_UINT32(msa
, stellaris_i2c_state
),
880 VMSTATE_UINT32(mcs
, stellaris_i2c_state
),
881 VMSTATE_UINT32(mdr
, stellaris_i2c_state
),
882 VMSTATE_UINT32(mtpr
, stellaris_i2c_state
),
883 VMSTATE_UINT32(mimr
, stellaris_i2c_state
),
884 VMSTATE_UINT32(mris
, stellaris_i2c_state
),
885 VMSTATE_UINT32(mcr
, stellaris_i2c_state
),
886 VMSTATE_END_OF_LIST()
890 static void stellaris_i2c_init(Object
*obj
)
892 DeviceState
*dev
= DEVICE(obj
);
893 stellaris_i2c_state
*s
= STELLARIS_I2C(obj
);
894 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
897 sysbus_init_irq(sbd
, &s
->irq
);
898 bus
= i2c_init_bus(dev
, "i2c");
901 memory_region_init_io(&s
->iomem
, obj
, &stellaris_i2c_ops
, s
,
903 sysbus_init_mmio(sbd
, &s
->iomem
);
904 /* ??? For now we only implement the master interface. */
905 stellaris_i2c_reset(s
);
908 /* Analogue to Digital Converter. This is only partially implemented,
909 enough for applications that use a combined ADC and timer tick. */
911 #define STELLARIS_ADC_EM_CONTROLLER 0
912 #define STELLARIS_ADC_EM_COMP 1
913 #define STELLARIS_ADC_EM_EXTERNAL 4
914 #define STELLARIS_ADC_EM_TIMER 5
915 #define STELLARIS_ADC_EM_PWM0 6
916 #define STELLARIS_ADC_EM_PWM1 7
917 #define STELLARIS_ADC_EM_PWM2 8
919 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
920 #define STELLARIS_ADC_FIFO_FULL 0x1000
922 #define TYPE_STELLARIS_ADC "stellaris-adc"
923 #define STELLARIS_ADC(obj) \
924 OBJECT_CHECK(stellaris_adc_state, (obj), TYPE_STELLARIS_ADC)
926 typedef struct StellarisADCState
{
927 SysBusDevice parent_obj
;
946 } stellaris_adc_state
;
948 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state
*s
, int n
)
952 tail
= s
->fifo
[n
].state
& 0xf;
953 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_EMPTY
) {
956 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf) | ((tail
+ 1) & 0xf);
957 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_FULL
;
958 if (tail
+ 1 == ((s
->fifo
[n
].state
>> 4) & 0xf))
959 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_EMPTY
;
961 return s
->fifo
[n
].data
[tail
];
964 static void stellaris_adc_fifo_write(stellaris_adc_state
*s
, int n
,
969 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
970 FIFO fir each sequencer. */
971 head
= (s
->fifo
[n
].state
>> 4) & 0xf;
972 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_FULL
) {
976 s
->fifo
[n
].data
[head
] = value
;
977 head
= (head
+ 1) & 0xf;
978 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_EMPTY
;
979 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf0) | (head
<< 4);
980 if ((s
->fifo
[n
].state
& 0xf) == head
)
981 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_FULL
;
984 static void stellaris_adc_update(stellaris_adc_state
*s
)
989 for (n
= 0; n
< 4; n
++) {
990 level
= (s
->ris
& s
->im
& (1 << n
)) != 0;
991 qemu_set_irq(s
->irq
[n
], level
);
995 static void stellaris_adc_trigger(void *opaque
, int irq
, int level
)
997 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1000 for (n
= 0; n
< 4; n
++) {
1001 if ((s
->actss
& (1 << n
)) == 0) {
1005 if (((s
->emux
>> (n
* 4)) & 0xff) != 5) {
1009 /* Some applications use the ADC as a random number source, so introduce
1010 some variation into the signal. */
1011 s
->noise
= s
->noise
* 314159 + 1;
1012 /* ??? actual inputs not implemented. Return an arbitrary value. */
1013 stellaris_adc_fifo_write(s
, n
, 0x200 + ((s
->noise
>> 16) & 7));
1015 stellaris_adc_update(s
);
1019 static void stellaris_adc_reset(stellaris_adc_state
*s
)
1023 for (n
= 0; n
< 4; n
++) {
1026 s
->fifo
[n
].state
= STELLARIS_ADC_FIFO_EMPTY
;
1030 static uint64_t stellaris_adc_read(void *opaque
, hwaddr offset
,
1033 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1035 /* TODO: Implement this. */
1036 if (offset
>= 0x40 && offset
< 0xc0) {
1038 n
= (offset
- 0x40) >> 5;
1039 switch (offset
& 0x1f) {
1040 case 0x00: /* SSMUX */
1042 case 0x04: /* SSCTL */
1044 case 0x08: /* SSFIFO */
1045 return stellaris_adc_fifo_read(s
, n
);
1046 case 0x0c: /* SSFSTAT */
1047 return s
->fifo
[n
].state
;
1053 case 0x00: /* ACTSS */
1055 case 0x04: /* RIS */
1059 case 0x0c: /* ISC */
1060 return s
->ris
& s
->im
;
1061 case 0x10: /* OSTAT */
1063 case 0x14: /* EMUX */
1065 case 0x18: /* USTAT */
1067 case 0x20: /* SSPRI */
1069 case 0x30: /* SAC */
1072 qemu_log_mask(LOG_GUEST_ERROR
,
1073 "stellaris_adc: read at bad offset 0x%x\n", (int)offset
);
1078 static void stellaris_adc_write(void *opaque
, hwaddr offset
,
1079 uint64_t value
, unsigned size
)
1081 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1083 /* TODO: Implement this. */
1084 if (offset
>= 0x40 && offset
< 0xc0) {
1086 n
= (offset
- 0x40) >> 5;
1087 switch (offset
& 0x1f) {
1088 case 0x00: /* SSMUX */
1089 s
->ssmux
[n
] = value
& 0x33333333;
1091 case 0x04: /* SSCTL */
1093 qemu_log_mask(LOG_UNIMP
,
1094 "ADC: Unimplemented sequence %" PRIx64
"\n",
1097 s
->ssctl
[n
] = value
;
1104 case 0x00: /* ACTSS */
1105 s
->actss
= value
& 0xf;
1110 case 0x0c: /* ISC */
1113 case 0x10: /* OSTAT */
1116 case 0x14: /* EMUX */
1119 case 0x18: /* USTAT */
1122 case 0x20: /* SSPRI */
1125 case 0x28: /* PSSI */
1126 qemu_log_mask(LOG_UNIMP
, "ADC: sample initiate unimplemented");
1128 case 0x30: /* SAC */
1132 qemu_log_mask(LOG_GUEST_ERROR
,
1133 "stellaris_adc: write at bad offset 0x%x\n", (int)offset
);
1135 stellaris_adc_update(s
);
1138 static const MemoryRegionOps stellaris_adc_ops
= {
1139 .read
= stellaris_adc_read
,
1140 .write
= stellaris_adc_write
,
1141 .endianness
= DEVICE_NATIVE_ENDIAN
,
1144 static const VMStateDescription vmstate_stellaris_adc
= {
1145 .name
= "stellaris_adc",
1147 .minimum_version_id
= 1,
1148 .fields
= (VMStateField
[]) {
1149 VMSTATE_UINT32(actss
, stellaris_adc_state
),
1150 VMSTATE_UINT32(ris
, stellaris_adc_state
),
1151 VMSTATE_UINT32(im
, stellaris_adc_state
),
1152 VMSTATE_UINT32(emux
, stellaris_adc_state
),
1153 VMSTATE_UINT32(ostat
, stellaris_adc_state
),
1154 VMSTATE_UINT32(ustat
, stellaris_adc_state
),
1155 VMSTATE_UINT32(sspri
, stellaris_adc_state
),
1156 VMSTATE_UINT32(sac
, stellaris_adc_state
),
1157 VMSTATE_UINT32(fifo
[0].state
, stellaris_adc_state
),
1158 VMSTATE_UINT32_ARRAY(fifo
[0].data
, stellaris_adc_state
, 16),
1159 VMSTATE_UINT32(ssmux
[0], stellaris_adc_state
),
1160 VMSTATE_UINT32(ssctl
[0], stellaris_adc_state
),
1161 VMSTATE_UINT32(fifo
[1].state
, stellaris_adc_state
),
1162 VMSTATE_UINT32_ARRAY(fifo
[1].data
, stellaris_adc_state
, 16),
1163 VMSTATE_UINT32(ssmux
[1], stellaris_adc_state
),
1164 VMSTATE_UINT32(ssctl
[1], stellaris_adc_state
),
1165 VMSTATE_UINT32(fifo
[2].state
, stellaris_adc_state
),
1166 VMSTATE_UINT32_ARRAY(fifo
[2].data
, stellaris_adc_state
, 16),
1167 VMSTATE_UINT32(ssmux
[2], stellaris_adc_state
),
1168 VMSTATE_UINT32(ssctl
[2], stellaris_adc_state
),
1169 VMSTATE_UINT32(fifo
[3].state
, stellaris_adc_state
),
1170 VMSTATE_UINT32_ARRAY(fifo
[3].data
, stellaris_adc_state
, 16),
1171 VMSTATE_UINT32(ssmux
[3], stellaris_adc_state
),
1172 VMSTATE_UINT32(ssctl
[3], stellaris_adc_state
),
1173 VMSTATE_UINT32(noise
, stellaris_adc_state
),
1174 VMSTATE_END_OF_LIST()
1178 static void stellaris_adc_init(Object
*obj
)
1180 DeviceState
*dev
= DEVICE(obj
);
1181 stellaris_adc_state
*s
= STELLARIS_ADC(obj
);
1182 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
1185 for (n
= 0; n
< 4; n
++) {
1186 sysbus_init_irq(sbd
, &s
->irq
[n
]);
1189 memory_region_init_io(&s
->iomem
, obj
, &stellaris_adc_ops
, s
,
1191 sysbus_init_mmio(sbd
, &s
->iomem
);
1192 stellaris_adc_reset(s
);
1193 qdev_init_gpio_in(dev
, stellaris_adc_trigger
, 1);
1197 void do_sys_reset(void *opaque
, int n
, int level
)
1200 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
1205 static stellaris_board_info stellaris_boards
[] = {
1209 0x001f001f, /* dc0 */
1219 0x00ff007f, /* dc0 */
1224 BP_OLED_SSI
| BP_GAMEPAD
1228 static void stellaris_init(const char *kernel_filename
, const char *cpu_model
,
1229 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 vmstate_register_ram_global(flash
);
1294 memory_region_set_readonly(flash
, true);
1295 memory_region_add_subregion(system_memory
, 0, flash
);
1297 memory_region_init_ram(sram
, NULL
, "stellaris.sram", sram_size
,
1299 vmstate_register_ram_global(sram
);
1300 memory_region_add_subregion(system_memory
, 0x20000000, sram
);
1302 nvic
= armv7m_init(system_memory
, flash_size
, NUM_IRQ_LINES
,
1303 kernel_filename
, cpu_model
);
1305 qdev_connect_gpio_out_named(nvic
, "SYSRESETREQ", 0,
1306 qemu_allocate_irq(&do_sys_reset
, NULL
, 0));
1308 if (board
->dc1
& (1 << 16)) {
1309 dev
= sysbus_create_varargs(TYPE_STELLARIS_ADC
, 0x40038000,
1310 qdev_get_gpio_in(nvic
, 14),
1311 qdev_get_gpio_in(nvic
, 15),
1312 qdev_get_gpio_in(nvic
, 16),
1313 qdev_get_gpio_in(nvic
, 17),
1315 adc
= qdev_get_gpio_in(dev
, 0);
1319 for (i
= 0; i
< 4; i
++) {
1320 if (board
->dc2
& (0x10000 << i
)) {
1321 dev
= sysbus_create_simple(TYPE_STELLARIS_GPTM
,
1322 0x40030000 + i
* 0x1000,
1323 qdev_get_gpio_in(nvic
, timer_irq
[i
]));
1324 /* TODO: This is incorrect, but we get away with it because
1325 the ADC output is only ever pulsed. */
1326 qdev_connect_gpio_out(dev
, 0, adc
);
1330 stellaris_sys_init(0x400fe000, qdev_get_gpio_in(nvic
, 28),
1331 board
, nd_table
[0].macaddr
.a
);
1333 for (i
= 0; i
< 7; i
++) {
1334 if (board
->dc4
& (1 << i
)) {
1335 gpio_dev
[i
] = sysbus_create_simple("pl061_luminary", gpio_addr
[i
],
1336 qdev_get_gpio_in(nvic
,
1338 for (j
= 0; j
< 8; j
++) {
1339 gpio_in
[i
][j
] = qdev_get_gpio_in(gpio_dev
[i
], j
);
1340 gpio_out
[i
][j
] = NULL
;
1345 if (board
->dc2
& (1 << 12)) {
1346 dev
= sysbus_create_simple(TYPE_STELLARIS_I2C
, 0x40020000,
1347 qdev_get_gpio_in(nvic
, 8));
1348 i2c
= (I2CBus
*)qdev_get_child_bus(dev
, "i2c");
1349 if (board
->peripherals
& BP_OLED_I2C
) {
1350 i2c_create_slave(i2c
, "ssd0303", 0x3d);
1354 for (i
= 0; i
< 4; i
++) {
1355 if (board
->dc2
& (1 << i
)) {
1356 pl011_luminary_create(0x4000c000 + i
* 0x1000,
1357 qdev_get_gpio_in(nvic
, uart_irq
[i
]),
1361 if (board
->dc2
& (1 << 4)) {
1362 dev
= sysbus_create_simple("pl022", 0x40008000,
1363 qdev_get_gpio_in(nvic
, 7));
1364 if (board
->peripherals
& BP_OLED_SSI
) {
1367 DeviceState
*ssddev
;
1369 /* Some boards have both an OLED controller and SD card connected to
1370 * the same SSI port, with the SD card chip select connected to a
1371 * GPIO pin. Technically the OLED chip select is connected to the
1372 * SSI Fss pin. We do not bother emulating that as both devices
1373 * should never be selected simultaneously, and our OLED controller
1374 * ignores stray 0xff commands that occur when deselecting the SD
1377 bus
= qdev_get_child_bus(dev
, "ssi");
1379 sddev
= ssi_create_slave(bus
, "ssi-sd");
1380 ssddev
= ssi_create_slave(bus
, "ssd0323");
1381 gpio_out
[GPIO_D
][0] = qemu_irq_split(
1382 qdev_get_gpio_in_named(sddev
, SSI_GPIO_CS
, 0),
1383 qdev_get_gpio_in_named(ssddev
, SSI_GPIO_CS
, 0));
1384 gpio_out
[GPIO_C
][7] = qdev_get_gpio_in(ssddev
, 0);
1386 /* Make sure the select pin is high. */
1387 qemu_irq_raise(gpio_out
[GPIO_D
][0]);
1390 if (board
->dc4
& (1 << 28)) {
1393 qemu_check_nic_model(&nd_table
[0], "stellaris");
1395 enet
= qdev_create(NULL
, "stellaris_enet");
1396 qdev_set_nic_properties(enet
, &nd_table
[0]);
1397 qdev_init_nofail(enet
);
1398 sysbus_mmio_map(SYS_BUS_DEVICE(enet
), 0, 0x40048000);
1399 sysbus_connect_irq(SYS_BUS_DEVICE(enet
), 0, qdev_get_gpio_in(nvic
, 42));
1401 if (board
->peripherals
& BP_GAMEPAD
) {
1402 qemu_irq gpad_irq
[5];
1403 static const int gpad_keycode
[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1405 gpad_irq
[0] = qemu_irq_invert(gpio_in
[GPIO_E
][0]); /* up */
1406 gpad_irq
[1] = qemu_irq_invert(gpio_in
[GPIO_E
][1]); /* down */
1407 gpad_irq
[2] = qemu_irq_invert(gpio_in
[GPIO_E
][2]); /* left */
1408 gpad_irq
[3] = qemu_irq_invert(gpio_in
[GPIO_E
][3]); /* right */
1409 gpad_irq
[4] = qemu_irq_invert(gpio_in
[GPIO_F
][1]); /* select */
1411 stellaris_gamepad_init(5, gpad_irq
, gpad_keycode
);
1413 for (i
= 0; i
< 7; i
++) {
1414 if (board
->dc4
& (1 << i
)) {
1415 for (j
= 0; j
< 8; j
++) {
1416 if (gpio_out
[i
][j
]) {
1417 qdev_connect_gpio_out(gpio_dev
[i
], j
, gpio_out
[i
][j
]);
1423 /* Add dummy regions for the devices we don't implement yet,
1424 * so guest accesses don't cause unlogged crashes.
1426 create_unimplemented_device("wdtimer", 0x40000000, 0x1000);
1427 create_unimplemented_device("i2c-0", 0x40002000, 0x1000);
1428 create_unimplemented_device("i2c-2", 0x40021000, 0x1000);
1429 create_unimplemented_device("PWM", 0x40028000, 0x1000);
1430 create_unimplemented_device("QEI-0", 0x4002c000, 0x1000);
1431 create_unimplemented_device("QEI-1", 0x4002d000, 0x1000);
1432 create_unimplemented_device("analogue-comparator", 0x4003c000, 0x1000);
1433 create_unimplemented_device("hibernation", 0x400fc000, 0x1000);
1434 create_unimplemented_device("flash-control", 0x400fd000, 0x1000);
1437 /* FIXME: Figure out how to generate these from stellaris_boards. */
1438 static void lm3s811evb_init(MachineState
*machine
)
1440 const char *cpu_model
= machine
->cpu_model
;
1441 const char *kernel_filename
= machine
->kernel_filename
;
1442 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[0]);
1445 static void lm3s6965evb_init(MachineState
*machine
)
1447 const char *cpu_model
= machine
->cpu_model
;
1448 const char *kernel_filename
= machine
->kernel_filename
;
1449 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[1]);
1452 static void lm3s811evb_class_init(ObjectClass
*oc
, void *data
)
1454 MachineClass
*mc
= MACHINE_CLASS(oc
);
1456 mc
->desc
= "Stellaris LM3S811EVB";
1457 mc
->init
= lm3s811evb_init
;
1460 static const TypeInfo lm3s811evb_type
= {
1461 .name
= MACHINE_TYPE_NAME("lm3s811evb"),
1462 .parent
= TYPE_MACHINE
,
1463 .class_init
= lm3s811evb_class_init
,
1466 static void lm3s6965evb_class_init(ObjectClass
*oc
, void *data
)
1468 MachineClass
*mc
= MACHINE_CLASS(oc
);
1470 mc
->desc
= "Stellaris LM3S6965EVB";
1471 mc
->init
= lm3s6965evb_init
;
1474 static const TypeInfo lm3s6965evb_type
= {
1475 .name
= MACHINE_TYPE_NAME("lm3s6965evb"),
1476 .parent
= TYPE_MACHINE
,
1477 .class_init
= lm3s6965evb_class_init
,
1480 static void stellaris_machine_init(void)
1482 type_register_static(&lm3s811evb_type
);
1483 type_register_static(&lm3s6965evb_type
);
1486 type_init(stellaris_machine_init
)
1488 static void stellaris_i2c_class_init(ObjectClass
*klass
, void *data
)
1490 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1492 dc
->vmsd
= &vmstate_stellaris_i2c
;
1495 static const TypeInfo stellaris_i2c_info
= {
1496 .name
= TYPE_STELLARIS_I2C
,
1497 .parent
= TYPE_SYS_BUS_DEVICE
,
1498 .instance_size
= sizeof(stellaris_i2c_state
),
1499 .instance_init
= stellaris_i2c_init
,
1500 .class_init
= stellaris_i2c_class_init
,
1503 static void stellaris_gptm_class_init(ObjectClass
*klass
, void *data
)
1505 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1507 dc
->vmsd
= &vmstate_stellaris_gptm
;
1510 static const TypeInfo stellaris_gptm_info
= {
1511 .name
= TYPE_STELLARIS_GPTM
,
1512 .parent
= TYPE_SYS_BUS_DEVICE
,
1513 .instance_size
= sizeof(gptm_state
),
1514 .instance_init
= stellaris_gptm_init
,
1515 .class_init
= stellaris_gptm_class_init
,
1518 static void stellaris_adc_class_init(ObjectClass
*klass
, void *data
)
1520 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1522 dc
->vmsd
= &vmstate_stellaris_adc
;
1525 static const TypeInfo stellaris_adc_info
= {
1526 .name
= TYPE_STELLARIS_ADC
,
1527 .parent
= TYPE_SYS_BUS_DEVICE
,
1528 .instance_size
= sizeof(stellaris_adc_state
),
1529 .instance_init
= stellaris_adc_init
,
1530 .class_init
= stellaris_adc_class_init
,
1533 static void stellaris_register_types(void)
1535 type_register_static(&stellaris_i2c_info
);
1536 type_register_static(&stellaris_gptm_info
);
1537 type_register_static(&stellaris_adc_info
);
1540 type_init(stellaris_register_types
)