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 "hw/sysbus.h"
12 #include "hw/arm/arm.h"
13 #include "hw/devices.h"
14 #include "qemu/timer.h"
15 #include "hw/i2c/i2c.h"
17 #include "hw/boards.h"
18 #include "exec/address-spaces.h"
19 #include "sysemu/sysemu.h"
29 #define BP_OLED_I2C 0x01
30 #define BP_OLED_SSI 0x02
31 #define BP_GAMEPAD 0x04
33 #define NUM_IRQ_LINES 64
35 typedef const struct {
45 } stellaris_board_info
;
47 /* General purpose timer module. */
49 #define TYPE_STELLARIS_GPTM "stellaris-gptm"
50 #define STELLARIS_GPTM(obj) \
51 OBJECT_CHECK(gptm_state, (obj), TYPE_STELLARIS_GPTM)
53 typedef struct gptm_state
{
54 SysBusDevice parent_obj
;
65 uint32_t match_prescale
[2];
68 struct gptm_state
*opaque
[2];
70 /* The timers have an alternate output used to trigger the ADC. */
75 static void gptm_update_irq(gptm_state
*s
)
78 level
= (s
->state
& s
->mask
) != 0;
79 qemu_set_irq(s
->irq
, level
);
82 static void gptm_stop(gptm_state
*s
, int n
)
84 timer_del(s
->timer
[n
]);
87 static void gptm_reload(gptm_state
*s
, int n
, int reset
)
91 tick
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
96 /* 32-bit CountDown. */
98 count
= s
->load
[0] | (s
->load
[1] << 16);
99 tick
+= (int64_t)count
* system_clock_scale
;
100 } else if (s
->config
== 1) {
101 /* 32-bit RTC. 1Hz tick. */
102 tick
+= get_ticks_per_sec();
103 } else if (s
->mode
[n
] == 0xa) {
104 /* PWM mode. Not implemented. */
106 hw_error("TODO: 16-bit timer mode 0x%x\n", s
->mode
[n
]);
109 timer_mod(s
->timer
[n
], tick
);
112 static void gptm_tick(void *opaque
)
114 gptm_state
**p
= (gptm_state
**)opaque
;
120 if (s
->config
== 0) {
122 if ((s
->control
& 0x20)) {
123 /* Output trigger. */
124 qemu_irq_pulse(s
->trigger
);
126 if (s
->mode
[0] & 1) {
131 gptm_reload(s
, 0, 0);
133 } else if (s
->config
== 1) {
137 match
= s
->match
[0] | (s
->match
[1] << 16);
143 gptm_reload(s
, 0, 0);
144 } else if (s
->mode
[n
] == 0xa) {
145 /* PWM mode. Not implemented. */
147 hw_error("TODO: 16-bit timer mode 0x%x\n", s
->mode
[n
]);
152 static uint64_t gptm_read(void *opaque
, hwaddr offset
,
155 gptm_state
*s
= (gptm_state
*)opaque
;
160 case 0x04: /* TAMR */
162 case 0x08: /* TBMR */
171 return s
->state
& s
->mask
;
174 case 0x28: /* TAILR */
175 return s
->load
[0] | ((s
->config
< 4) ? (s
->load
[1] << 16) : 0);
176 case 0x2c: /* TBILR */
178 case 0x30: /* TAMARCHR */
179 return s
->match
[0] | ((s
->config
< 4) ? (s
->match
[1] << 16) : 0);
180 case 0x34: /* TBMATCHR */
182 case 0x38: /* TAPR */
183 return s
->prescale
[0];
184 case 0x3c: /* TBPR */
185 return s
->prescale
[1];
186 case 0x40: /* TAPMR */
187 return s
->match_prescale
[0];
188 case 0x44: /* TBPMR */
189 return s
->match_prescale
[1];
191 if (s
->config
== 1) {
194 qemu_log_mask(LOG_UNIMP
,
195 "GPTM: read of TAR but timer read not supported");
198 qemu_log_mask(LOG_UNIMP
,
199 "GPTM: read of TBR but timer read not supported");
202 qemu_log_mask(LOG_GUEST_ERROR
,
203 "GPTM: read at bad offset 0x%x\n", (int)offset
);
208 static void gptm_write(void *opaque
, hwaddr offset
,
209 uint64_t value
, unsigned size
)
211 gptm_state
*s
= (gptm_state
*)opaque
;
214 /* The timers should be disabled before changing the configuration.
215 We take advantage of this and defer everything until the timer
221 case 0x04: /* TAMR */
224 case 0x08: /* TBMR */
230 /* TODO: Implement pause. */
231 if ((oldval
^ value
) & 1) {
233 gptm_reload(s
, 0, 1);
238 if (((oldval
^ value
) & 0x100) && s
->config
>= 4) {
240 gptm_reload(s
, 1, 1);
247 s
->mask
= value
& 0x77;
253 case 0x28: /* TAILR */
254 s
->load
[0] = value
& 0xffff;
256 s
->load
[1] = value
>> 16;
259 case 0x2c: /* TBILR */
260 s
->load
[1] = value
& 0xffff;
262 case 0x30: /* TAMARCHR */
263 s
->match
[0] = value
& 0xffff;
265 s
->match
[1] = value
>> 16;
268 case 0x34: /* TBMATCHR */
269 s
->match
[1] = value
>> 16;
271 case 0x38: /* TAPR */
272 s
->prescale
[0] = value
;
274 case 0x3c: /* TBPR */
275 s
->prescale
[1] = value
;
277 case 0x40: /* TAPMR */
278 s
->match_prescale
[0] = value
;
280 case 0x44: /* TBPMR */
281 s
->match_prescale
[0] = value
;
284 hw_error("gptm_write: Bad offset 0x%x\n", (int)offset
);
289 static const MemoryRegionOps gptm_ops
= {
292 .endianness
= DEVICE_NATIVE_ENDIAN
,
295 static const VMStateDescription vmstate_stellaris_gptm
= {
296 .name
= "stellaris_gptm",
298 .minimum_version_id
= 1,
299 .fields
= (VMStateField
[]) {
300 VMSTATE_UINT32(config
, gptm_state
),
301 VMSTATE_UINT32_ARRAY(mode
, gptm_state
, 2),
302 VMSTATE_UINT32(control
, gptm_state
),
303 VMSTATE_UINT32(state
, gptm_state
),
304 VMSTATE_UINT32(mask
, gptm_state
),
306 VMSTATE_UINT32_ARRAY(load
, gptm_state
, 2),
307 VMSTATE_UINT32_ARRAY(match
, gptm_state
, 2),
308 VMSTATE_UINT32_ARRAY(prescale
, gptm_state
, 2),
309 VMSTATE_UINT32_ARRAY(match_prescale
, gptm_state
, 2),
310 VMSTATE_UINT32(rtc
, gptm_state
),
311 VMSTATE_INT64_ARRAY(tick
, gptm_state
, 2),
312 VMSTATE_TIMER_PTR_ARRAY(timer
, gptm_state
, 2),
313 VMSTATE_END_OF_LIST()
317 static int stellaris_gptm_init(SysBusDevice
*sbd
)
319 DeviceState
*dev
= DEVICE(sbd
);
320 gptm_state
*s
= STELLARIS_GPTM(dev
);
322 sysbus_init_irq(sbd
, &s
->irq
);
323 qdev_init_gpio_out(dev
, &s
->trigger
, 1);
325 memory_region_init_io(&s
->iomem
, OBJECT(s
), &gptm_ops
, s
,
327 sysbus_init_mmio(sbd
, &s
->iomem
);
329 s
->opaque
[0] = s
->opaque
[1] = s
;
330 s
->timer
[0] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[0]);
331 s
->timer
[1] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[1]);
332 vmstate_register(dev
, -1, &vmstate_stellaris_gptm
, s
);
337 /* System controller. */
356 stellaris_board_info
*board
;
359 static void ssys_update(ssys_state
*s
)
361 qemu_set_irq(s
->irq
, (s
->int_status
& s
->int_mask
) != 0);
364 static uint32_t pllcfg_sandstorm
[16] = {
366 0x1ae0, /* 1.8432 Mhz */
368 0xd573, /* 2.4576 Mhz */
369 0x37a6, /* 3.57954 Mhz */
370 0x1ae2, /* 3.6864 Mhz */
372 0x98bc, /* 4.906 Mhz */
373 0x935b, /* 4.9152 Mhz */
375 0x4dee, /* 5.12 Mhz */
377 0x75db, /* 6.144 Mhz */
378 0x1ae6, /* 7.3728 Mhz */
380 0x585b /* 8.192 Mhz */
383 static uint32_t pllcfg_fury
[16] = {
385 0x1b20, /* 1.8432 Mhz */
387 0xf42b, /* 2.4576 Mhz */
388 0x37e3, /* 3.57954 Mhz */
389 0x1b21, /* 3.6864 Mhz */
391 0x98ee, /* 4.906 Mhz */
392 0xd5b4, /* 4.9152 Mhz */
394 0x4e27, /* 5.12 Mhz */
396 0xec1c, /* 6.144 Mhz */
397 0x1b23, /* 7.3728 Mhz */
399 0xb11c /* 8.192 Mhz */
402 #define DID0_VER_MASK 0x70000000
403 #define DID0_VER_0 0x00000000
404 #define DID0_VER_1 0x10000000
406 #define DID0_CLASS_MASK 0x00FF0000
407 #define DID0_CLASS_SANDSTORM 0x00000000
408 #define DID0_CLASS_FURY 0x00010000
410 static int ssys_board_class(const ssys_state
*s
)
412 uint32_t did0
= s
->board
->did0
;
413 switch (did0
& DID0_VER_MASK
) {
415 return DID0_CLASS_SANDSTORM
;
417 switch (did0
& DID0_CLASS_MASK
) {
418 case DID0_CLASS_SANDSTORM
:
419 case DID0_CLASS_FURY
:
420 return did0
& DID0_CLASS_MASK
;
422 /* for unknown classes, fall through */
424 hw_error("ssys_board_class: Unknown class 0x%08x\n", did0
);
428 static uint64_t ssys_read(void *opaque
, hwaddr offset
,
431 ssys_state
*s
= (ssys_state
*)opaque
;
434 case 0x000: /* DID0 */
435 return s
->board
->did0
;
436 case 0x004: /* DID1 */
437 return s
->board
->did1
;
438 case 0x008: /* DC0 */
439 return s
->board
->dc0
;
440 case 0x010: /* DC1 */
441 return s
->board
->dc1
;
442 case 0x014: /* DC2 */
443 return s
->board
->dc2
;
444 case 0x018: /* DC3 */
445 return s
->board
->dc3
;
446 case 0x01c: /* DC4 */
447 return s
->board
->dc4
;
448 case 0x030: /* PBORCTL */
450 case 0x034: /* LDOPCTL */
452 case 0x040: /* SRCR0 */
454 case 0x044: /* SRCR1 */
456 case 0x048: /* SRCR2 */
458 case 0x050: /* RIS */
459 return s
->int_status
;
460 case 0x054: /* IMC */
462 case 0x058: /* MISC */
463 return s
->int_status
& s
->int_mask
;
464 case 0x05c: /* RESC */
466 case 0x060: /* RCC */
468 case 0x064: /* PLLCFG */
471 xtal
= (s
->rcc
>> 6) & 0xf;
472 switch (ssys_board_class(s
)) {
473 case DID0_CLASS_FURY
:
474 return pllcfg_fury
[xtal
];
475 case DID0_CLASS_SANDSTORM
:
476 return pllcfg_sandstorm
[xtal
];
478 hw_error("ssys_read: Unhandled class for PLLCFG read.\n");
482 case 0x070: /* RCC2 */
484 case 0x100: /* RCGC0 */
486 case 0x104: /* RCGC1 */
488 case 0x108: /* RCGC2 */
490 case 0x110: /* SCGC0 */
492 case 0x114: /* SCGC1 */
494 case 0x118: /* SCGC2 */
496 case 0x120: /* DCGC0 */
498 case 0x124: /* DCGC1 */
500 case 0x128: /* DCGC2 */
502 case 0x150: /* CLKVCLR */
504 case 0x160: /* LDOARST */
506 case 0x1e0: /* USER0 */
508 case 0x1e4: /* USER1 */
511 hw_error("ssys_read: Bad offset 0x%x\n", (int)offset
);
516 static bool ssys_use_rcc2(ssys_state
*s
)
518 return (s
->rcc2
>> 31) & 0x1;
522 * Caculate the sys. clock period in ms.
524 static void ssys_calculate_system_clock(ssys_state
*s
)
526 if (ssys_use_rcc2(s
)) {
527 system_clock_scale
= 5 * (((s
->rcc2
>> 23) & 0x3f) + 1);
529 system_clock_scale
= 5 * (((s
->rcc
>> 23) & 0xf) + 1);
533 static void ssys_write(void *opaque
, hwaddr offset
,
534 uint64_t value
, unsigned size
)
536 ssys_state
*s
= (ssys_state
*)opaque
;
539 case 0x030: /* PBORCTL */
540 s
->pborctl
= value
& 0xffff;
542 case 0x034: /* LDOPCTL */
543 s
->ldopctl
= value
& 0x1f;
545 case 0x040: /* SRCR0 */
546 case 0x044: /* SRCR1 */
547 case 0x048: /* SRCR2 */
548 fprintf(stderr
, "Peripheral reset not implemented\n");
550 case 0x054: /* IMC */
551 s
->int_mask
= value
& 0x7f;
553 case 0x058: /* MISC */
554 s
->int_status
&= ~value
;
556 case 0x05c: /* RESC */
557 s
->resc
= value
& 0x3f;
559 case 0x060: /* RCC */
560 if ((s
->rcc
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
562 s
->int_status
|= (1 << 6);
565 ssys_calculate_system_clock(s
);
567 case 0x070: /* RCC2 */
568 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
572 if ((s
->rcc2
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
574 s
->int_status
|= (1 << 6);
577 ssys_calculate_system_clock(s
);
579 case 0x100: /* RCGC0 */
582 case 0x104: /* RCGC1 */
585 case 0x108: /* RCGC2 */
588 case 0x110: /* SCGC0 */
591 case 0x114: /* SCGC1 */
594 case 0x118: /* SCGC2 */
597 case 0x120: /* DCGC0 */
600 case 0x124: /* DCGC1 */
603 case 0x128: /* DCGC2 */
606 case 0x150: /* CLKVCLR */
609 case 0x160: /* LDOARST */
613 hw_error("ssys_write: Bad offset 0x%x\n", (int)offset
);
618 static const MemoryRegionOps ssys_ops
= {
621 .endianness
= DEVICE_NATIVE_ENDIAN
,
624 static void ssys_reset(void *opaque
)
626 ssys_state
*s
= (ssys_state
*)opaque
;
631 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
634 s
->rcc2
= 0x07802810;
639 ssys_calculate_system_clock(s
);
642 static int stellaris_sys_post_load(void *opaque
, int version_id
)
644 ssys_state
*s
= opaque
;
646 ssys_calculate_system_clock(s
);
651 static const VMStateDescription vmstate_stellaris_sys
= {
652 .name
= "stellaris_sys",
654 .minimum_version_id
= 1,
655 .post_load
= stellaris_sys_post_load
,
656 .fields
= (VMStateField
[]) {
657 VMSTATE_UINT32(pborctl
, ssys_state
),
658 VMSTATE_UINT32(ldopctl
, ssys_state
),
659 VMSTATE_UINT32(int_mask
, ssys_state
),
660 VMSTATE_UINT32(int_status
, ssys_state
),
661 VMSTATE_UINT32(resc
, ssys_state
),
662 VMSTATE_UINT32(rcc
, ssys_state
),
663 VMSTATE_UINT32_V(rcc2
, ssys_state
, 2),
664 VMSTATE_UINT32_ARRAY(rcgc
, ssys_state
, 3),
665 VMSTATE_UINT32_ARRAY(scgc
, ssys_state
, 3),
666 VMSTATE_UINT32_ARRAY(dcgc
, ssys_state
, 3),
667 VMSTATE_UINT32(clkvclr
, ssys_state
),
668 VMSTATE_UINT32(ldoarst
, ssys_state
),
669 VMSTATE_END_OF_LIST()
673 static int stellaris_sys_init(uint32_t base
, qemu_irq irq
,
674 stellaris_board_info
* board
,
679 s
= g_new0(ssys_state
, 1);
682 /* Most devices come preprogrammed with a MAC address in the user data. */
683 s
->user0
= macaddr
[0] | (macaddr
[1] << 8) | (macaddr
[2] << 16);
684 s
->user1
= macaddr
[3] | (macaddr
[4] << 8) | (macaddr
[5] << 16);
686 memory_region_init_io(&s
->iomem
, NULL
, &ssys_ops
, s
, "ssys", 0x00001000);
687 memory_region_add_subregion(get_system_memory(), base
, &s
->iomem
);
689 vmstate_register(NULL
, -1, &vmstate_stellaris_sys
, s
);
694 /* I2C controller. */
696 #define TYPE_STELLARIS_I2C "stellaris-i2c"
697 #define STELLARIS_I2C(obj) \
698 OBJECT_CHECK(stellaris_i2c_state, (obj), TYPE_STELLARIS_I2C)
701 SysBusDevice parent_obj
;
713 } stellaris_i2c_state
;
715 #define STELLARIS_I2C_MCS_BUSY 0x01
716 #define STELLARIS_I2C_MCS_ERROR 0x02
717 #define STELLARIS_I2C_MCS_ADRACK 0x04
718 #define STELLARIS_I2C_MCS_DATACK 0x08
719 #define STELLARIS_I2C_MCS_ARBLST 0x10
720 #define STELLARIS_I2C_MCS_IDLE 0x20
721 #define STELLARIS_I2C_MCS_BUSBSY 0x40
723 static uint64_t stellaris_i2c_read(void *opaque
, hwaddr offset
,
726 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
732 /* We don't emulate timing, so the controller is never busy. */
733 return s
->mcs
| STELLARIS_I2C_MCS_IDLE
;
736 case 0x0c: /* MTPR */
738 case 0x10: /* MIMR */
740 case 0x14: /* MRIS */
742 case 0x18: /* MMIS */
743 return s
->mris
& s
->mimr
;
747 hw_error("strllaris_i2c_read: Bad offset 0x%x\n", (int)offset
);
752 static void stellaris_i2c_update(stellaris_i2c_state
*s
)
756 level
= (s
->mris
& s
->mimr
) != 0;
757 qemu_set_irq(s
->irq
, level
);
760 static void stellaris_i2c_write(void *opaque
, hwaddr offset
,
761 uint64_t value
, unsigned size
)
763 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
767 s
->msa
= value
& 0xff;
770 if ((s
->mcr
& 0x10) == 0) {
771 /* Disabled. Do nothing. */
774 /* Grab the bus if this is starting a transfer. */
775 if ((value
& 2) && (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
776 if (i2c_start_transfer(s
->bus
, s
->msa
>> 1, s
->msa
& 1)) {
777 s
->mcs
|= STELLARIS_I2C_MCS_ARBLST
;
779 s
->mcs
&= ~STELLARIS_I2C_MCS_ARBLST
;
780 s
->mcs
|= STELLARIS_I2C_MCS_BUSBSY
;
783 /* If we don't have the bus then indicate an error. */
784 if (!i2c_bus_busy(s
->bus
)
785 || (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
786 s
->mcs
|= STELLARIS_I2C_MCS_ERROR
;
789 s
->mcs
&= ~STELLARIS_I2C_MCS_ERROR
;
791 /* Transfer a byte. */
792 /* TODO: Handle errors. */
795 s
->mdr
= i2c_recv(s
->bus
) & 0xff;
798 i2c_send(s
->bus
, s
->mdr
);
800 /* Raise an interrupt. */
804 /* Finish transfer. */
805 i2c_end_transfer(s
->bus
);
806 s
->mcs
&= ~STELLARIS_I2C_MCS_BUSBSY
;
810 s
->mdr
= value
& 0xff;
812 case 0x0c: /* MTPR */
813 s
->mtpr
= value
& 0xff;
815 case 0x10: /* MIMR */
818 case 0x1c: /* MICR */
824 "stellaris_i2c_write: Loopback not implemented\n");
827 "stellaris_i2c_write: Slave mode not implemented\n");
828 s
->mcr
= value
& 0x31;
831 hw_error("stellaris_i2c_write: Bad offset 0x%x\n",
834 stellaris_i2c_update(s
);
837 static void stellaris_i2c_reset(stellaris_i2c_state
*s
)
839 if (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
)
840 i2c_end_transfer(s
->bus
);
849 stellaris_i2c_update(s
);
852 static const MemoryRegionOps stellaris_i2c_ops
= {
853 .read
= stellaris_i2c_read
,
854 .write
= stellaris_i2c_write
,
855 .endianness
= DEVICE_NATIVE_ENDIAN
,
858 static const VMStateDescription vmstate_stellaris_i2c
= {
859 .name
= "stellaris_i2c",
861 .minimum_version_id
= 1,
862 .fields
= (VMStateField
[]) {
863 VMSTATE_UINT32(msa
, stellaris_i2c_state
),
864 VMSTATE_UINT32(mcs
, stellaris_i2c_state
),
865 VMSTATE_UINT32(mdr
, stellaris_i2c_state
),
866 VMSTATE_UINT32(mtpr
, stellaris_i2c_state
),
867 VMSTATE_UINT32(mimr
, stellaris_i2c_state
),
868 VMSTATE_UINT32(mris
, stellaris_i2c_state
),
869 VMSTATE_UINT32(mcr
, stellaris_i2c_state
),
870 VMSTATE_END_OF_LIST()
874 static int stellaris_i2c_init(SysBusDevice
*sbd
)
876 DeviceState
*dev
= DEVICE(sbd
);
877 stellaris_i2c_state
*s
= STELLARIS_I2C(dev
);
880 sysbus_init_irq(sbd
, &s
->irq
);
881 bus
= i2c_init_bus(dev
, "i2c");
884 memory_region_init_io(&s
->iomem
, OBJECT(s
), &stellaris_i2c_ops
, s
,
886 sysbus_init_mmio(sbd
, &s
->iomem
);
887 /* ??? For now we only implement the master interface. */
888 stellaris_i2c_reset(s
);
889 vmstate_register(dev
, -1, &vmstate_stellaris_i2c
, s
);
893 /* Analogue to Digital Converter. This is only partially implemented,
894 enough for applications that use a combined ADC and timer tick. */
896 #define STELLARIS_ADC_EM_CONTROLLER 0
897 #define STELLARIS_ADC_EM_COMP 1
898 #define STELLARIS_ADC_EM_EXTERNAL 4
899 #define STELLARIS_ADC_EM_TIMER 5
900 #define STELLARIS_ADC_EM_PWM0 6
901 #define STELLARIS_ADC_EM_PWM1 7
902 #define STELLARIS_ADC_EM_PWM2 8
904 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
905 #define STELLARIS_ADC_FIFO_FULL 0x1000
907 #define TYPE_STELLARIS_ADC "stellaris-adc"
908 #define STELLARIS_ADC(obj) \
909 OBJECT_CHECK(stellaris_adc_state, (obj), TYPE_STELLARIS_ADC)
911 typedef struct StellarisADCState
{
912 SysBusDevice parent_obj
;
931 } stellaris_adc_state
;
933 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state
*s
, int n
)
937 tail
= s
->fifo
[n
].state
& 0xf;
938 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_EMPTY
) {
941 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf) | ((tail
+ 1) & 0xf);
942 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_FULL
;
943 if (tail
+ 1 == ((s
->fifo
[n
].state
>> 4) & 0xf))
944 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_EMPTY
;
946 return s
->fifo
[n
].data
[tail
];
949 static void stellaris_adc_fifo_write(stellaris_adc_state
*s
, int n
,
954 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
955 FIFO fir each sequencer. */
956 head
= (s
->fifo
[n
].state
>> 4) & 0xf;
957 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_FULL
) {
961 s
->fifo
[n
].data
[head
] = value
;
962 head
= (head
+ 1) & 0xf;
963 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_EMPTY
;
964 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf0) | (head
<< 4);
965 if ((s
->fifo
[n
].state
& 0xf) == head
)
966 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_FULL
;
969 static void stellaris_adc_update(stellaris_adc_state
*s
)
974 for (n
= 0; n
< 4; n
++) {
975 level
= (s
->ris
& s
->im
& (1 << n
)) != 0;
976 qemu_set_irq(s
->irq
[n
], level
);
980 static void stellaris_adc_trigger(void *opaque
, int irq
, int level
)
982 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
985 for (n
= 0; n
< 4; n
++) {
986 if ((s
->actss
& (1 << n
)) == 0) {
990 if (((s
->emux
>> (n
* 4)) & 0xff) != 5) {
994 /* Some applications use the ADC as a random number source, so introduce
995 some variation into the signal. */
996 s
->noise
= s
->noise
* 314159 + 1;
997 /* ??? actual inputs not implemented. Return an arbitrary value. */
998 stellaris_adc_fifo_write(s
, n
, 0x200 + ((s
->noise
>> 16) & 7));
1000 stellaris_adc_update(s
);
1004 static void stellaris_adc_reset(stellaris_adc_state
*s
)
1008 for (n
= 0; n
< 4; n
++) {
1011 s
->fifo
[n
].state
= STELLARIS_ADC_FIFO_EMPTY
;
1015 static uint64_t stellaris_adc_read(void *opaque
, hwaddr offset
,
1018 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1020 /* TODO: Implement this. */
1021 if (offset
>= 0x40 && offset
< 0xc0) {
1023 n
= (offset
- 0x40) >> 5;
1024 switch (offset
& 0x1f) {
1025 case 0x00: /* SSMUX */
1027 case 0x04: /* SSCTL */
1029 case 0x08: /* SSFIFO */
1030 return stellaris_adc_fifo_read(s
, n
);
1031 case 0x0c: /* SSFSTAT */
1032 return s
->fifo
[n
].state
;
1038 case 0x00: /* ACTSS */
1040 case 0x04: /* RIS */
1044 case 0x0c: /* ISC */
1045 return s
->ris
& s
->im
;
1046 case 0x10: /* OSTAT */
1048 case 0x14: /* EMUX */
1050 case 0x18: /* USTAT */
1052 case 0x20: /* SSPRI */
1054 case 0x30: /* SAC */
1057 hw_error("strllaris_adc_read: Bad offset 0x%x\n",
1063 static void stellaris_adc_write(void *opaque
, hwaddr offset
,
1064 uint64_t value
, unsigned size
)
1066 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1068 /* TODO: Implement this. */
1069 if (offset
>= 0x40 && offset
< 0xc0) {
1071 n
= (offset
- 0x40) >> 5;
1072 switch (offset
& 0x1f) {
1073 case 0x00: /* SSMUX */
1074 s
->ssmux
[n
] = value
& 0x33333333;
1076 case 0x04: /* SSCTL */
1078 hw_error("ADC: Unimplemented sequence %" PRIx64
"\n",
1081 s
->ssctl
[n
] = value
;
1088 case 0x00: /* ACTSS */
1089 s
->actss
= value
& 0xf;
1094 case 0x0c: /* ISC */
1097 case 0x10: /* OSTAT */
1100 case 0x14: /* EMUX */
1103 case 0x18: /* USTAT */
1106 case 0x20: /* SSPRI */
1109 case 0x28: /* PSSI */
1110 hw_error("Not implemented: ADC sample initiate\n");
1112 case 0x30: /* SAC */
1116 hw_error("stellaris_adc_write: Bad offset 0x%x\n", (int)offset
);
1118 stellaris_adc_update(s
);
1121 static const MemoryRegionOps stellaris_adc_ops
= {
1122 .read
= stellaris_adc_read
,
1123 .write
= stellaris_adc_write
,
1124 .endianness
= DEVICE_NATIVE_ENDIAN
,
1127 static const VMStateDescription vmstate_stellaris_adc
= {
1128 .name
= "stellaris_adc",
1130 .minimum_version_id
= 1,
1131 .fields
= (VMStateField
[]) {
1132 VMSTATE_UINT32(actss
, stellaris_adc_state
),
1133 VMSTATE_UINT32(ris
, stellaris_adc_state
),
1134 VMSTATE_UINT32(im
, stellaris_adc_state
),
1135 VMSTATE_UINT32(emux
, stellaris_adc_state
),
1136 VMSTATE_UINT32(ostat
, stellaris_adc_state
),
1137 VMSTATE_UINT32(ustat
, stellaris_adc_state
),
1138 VMSTATE_UINT32(sspri
, stellaris_adc_state
),
1139 VMSTATE_UINT32(sac
, stellaris_adc_state
),
1140 VMSTATE_UINT32(fifo
[0].state
, stellaris_adc_state
),
1141 VMSTATE_UINT32_ARRAY(fifo
[0].data
, stellaris_adc_state
, 16),
1142 VMSTATE_UINT32(ssmux
[0], stellaris_adc_state
),
1143 VMSTATE_UINT32(ssctl
[0], stellaris_adc_state
),
1144 VMSTATE_UINT32(fifo
[1].state
, stellaris_adc_state
),
1145 VMSTATE_UINT32_ARRAY(fifo
[1].data
, stellaris_adc_state
, 16),
1146 VMSTATE_UINT32(ssmux
[1], stellaris_adc_state
),
1147 VMSTATE_UINT32(ssctl
[1], stellaris_adc_state
),
1148 VMSTATE_UINT32(fifo
[2].state
, stellaris_adc_state
),
1149 VMSTATE_UINT32_ARRAY(fifo
[2].data
, stellaris_adc_state
, 16),
1150 VMSTATE_UINT32(ssmux
[2], stellaris_adc_state
),
1151 VMSTATE_UINT32(ssctl
[2], stellaris_adc_state
),
1152 VMSTATE_UINT32(fifo
[3].state
, stellaris_adc_state
),
1153 VMSTATE_UINT32_ARRAY(fifo
[3].data
, stellaris_adc_state
, 16),
1154 VMSTATE_UINT32(ssmux
[3], stellaris_adc_state
),
1155 VMSTATE_UINT32(ssctl
[3], stellaris_adc_state
),
1156 VMSTATE_UINT32(noise
, stellaris_adc_state
),
1157 VMSTATE_END_OF_LIST()
1161 static int stellaris_adc_init(SysBusDevice
*sbd
)
1163 DeviceState
*dev
= DEVICE(sbd
);
1164 stellaris_adc_state
*s
= STELLARIS_ADC(dev
);
1167 for (n
= 0; n
< 4; n
++) {
1168 sysbus_init_irq(sbd
, &s
->irq
[n
]);
1171 memory_region_init_io(&s
->iomem
, OBJECT(s
), &stellaris_adc_ops
, s
,
1173 sysbus_init_mmio(sbd
, &s
->iomem
);
1174 stellaris_adc_reset(s
);
1175 qdev_init_gpio_in(dev
, stellaris_adc_trigger
, 1);
1176 vmstate_register(dev
, -1, &vmstate_stellaris_adc
, s
);
1181 void do_sys_reset(void *opaque
, int n
, int level
)
1184 qemu_system_reset_request();
1189 static stellaris_board_info stellaris_boards
[] = {
1193 0x001f001f, /* dc0 */
1203 0x00ff007f, /* dc0 */
1208 BP_OLED_SSI
| BP_GAMEPAD
1212 static void stellaris_init(const char *kernel_filename
, const char *cpu_model
,
1213 stellaris_board_info
*board
)
1215 static const int uart_irq
[] = {5, 6, 33, 34};
1216 static const int timer_irq
[] = {19, 21, 23, 35};
1217 static const uint32_t gpio_addr
[7] =
1218 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1219 0x40024000, 0x40025000, 0x40026000};
1220 static const int gpio_irq
[7] = {0, 1, 2, 3, 4, 30, 31};
1222 DeviceState
*gpio_dev
[7], *nvic
;
1223 qemu_irq gpio_in
[7][8];
1224 qemu_irq gpio_out
[7][8];
1233 MemoryRegion
*sram
= g_new(MemoryRegion
, 1);
1234 MemoryRegion
*flash
= g_new(MemoryRegion
, 1);
1235 MemoryRegion
*system_memory
= get_system_memory();
1237 flash_size
= (((board
->dc0
& 0xffff) + 1) << 1) * 1024;
1238 sram_size
= ((board
->dc0
>> 18) + 1) * 1024;
1240 /* Flash programming is done via the SCU, so pretend it is ROM. */
1241 memory_region_init_ram(flash
, NULL
, "stellaris.flash", flash_size
,
1243 vmstate_register_ram_global(flash
);
1244 memory_region_set_readonly(flash
, true);
1245 memory_region_add_subregion(system_memory
, 0, flash
);
1247 memory_region_init_ram(sram
, NULL
, "stellaris.sram", sram_size
,
1249 vmstate_register_ram_global(sram
);
1250 memory_region_add_subregion(system_memory
, 0x20000000, sram
);
1252 nvic
= armv7m_init(system_memory
, flash_size
, NUM_IRQ_LINES
,
1253 kernel_filename
, cpu_model
);
1255 qdev_connect_gpio_out_named(nvic
, "SYSRESETREQ", 0,
1256 qemu_allocate_irq(&do_sys_reset
, NULL
, 0));
1258 if (board
->dc1
& (1 << 16)) {
1259 dev
= sysbus_create_varargs(TYPE_STELLARIS_ADC
, 0x40038000,
1260 qdev_get_gpio_in(nvic
, 14),
1261 qdev_get_gpio_in(nvic
, 15),
1262 qdev_get_gpio_in(nvic
, 16),
1263 qdev_get_gpio_in(nvic
, 17),
1265 adc
= qdev_get_gpio_in(dev
, 0);
1269 for (i
= 0; i
< 4; i
++) {
1270 if (board
->dc2
& (0x10000 << i
)) {
1271 dev
= sysbus_create_simple(TYPE_STELLARIS_GPTM
,
1272 0x40030000 + i
* 0x1000,
1273 qdev_get_gpio_in(nvic
, timer_irq
[i
]));
1274 /* TODO: This is incorrect, but we get away with it because
1275 the ADC output is only ever pulsed. */
1276 qdev_connect_gpio_out(dev
, 0, adc
);
1280 stellaris_sys_init(0x400fe000, qdev_get_gpio_in(nvic
, 28),
1281 board
, nd_table
[0].macaddr
.a
);
1283 for (i
= 0; i
< 7; i
++) {
1284 if (board
->dc4
& (1 << i
)) {
1285 gpio_dev
[i
] = sysbus_create_simple("pl061_luminary", gpio_addr
[i
],
1286 qdev_get_gpio_in(nvic
,
1288 for (j
= 0; j
< 8; j
++) {
1289 gpio_in
[i
][j
] = qdev_get_gpio_in(gpio_dev
[i
], j
);
1290 gpio_out
[i
][j
] = NULL
;
1295 if (board
->dc2
& (1 << 12)) {
1296 dev
= sysbus_create_simple(TYPE_STELLARIS_I2C
, 0x40020000,
1297 qdev_get_gpio_in(nvic
, 8));
1298 i2c
= (I2CBus
*)qdev_get_child_bus(dev
, "i2c");
1299 if (board
->peripherals
& BP_OLED_I2C
) {
1300 i2c_create_slave(i2c
, "ssd0303", 0x3d);
1304 for (i
= 0; i
< 4; i
++) {
1305 if (board
->dc2
& (1 << i
)) {
1306 sysbus_create_simple("pl011_luminary", 0x4000c000 + i
* 0x1000,
1307 qdev_get_gpio_in(nvic
, uart_irq
[i
]));
1310 if (board
->dc2
& (1 << 4)) {
1311 dev
= sysbus_create_simple("pl022", 0x40008000,
1312 qdev_get_gpio_in(nvic
, 7));
1313 if (board
->peripherals
& BP_OLED_SSI
) {
1316 DeviceState
*ssddev
;
1318 /* Some boards have both an OLED controller and SD card connected to
1319 * the same SSI port, with the SD card chip select connected to a
1320 * GPIO pin. Technically the OLED chip select is connected to the
1321 * SSI Fss pin. We do not bother emulating that as both devices
1322 * should never be selected simultaneously, and our OLED controller
1323 * ignores stray 0xff commands that occur when deselecting the SD
1326 bus
= qdev_get_child_bus(dev
, "ssi");
1328 sddev
= ssi_create_slave(bus
, "ssi-sd");
1329 ssddev
= ssi_create_slave(bus
, "ssd0323");
1330 gpio_out
[GPIO_D
][0] = qemu_irq_split(
1331 qdev_get_gpio_in_named(sddev
, SSI_GPIO_CS
, 0),
1332 qdev_get_gpio_in_named(ssddev
, SSI_GPIO_CS
, 0));
1333 gpio_out
[GPIO_C
][7] = qdev_get_gpio_in(ssddev
, 0);
1335 /* Make sure the select pin is high. */
1336 qemu_irq_raise(gpio_out
[GPIO_D
][0]);
1339 if (board
->dc4
& (1 << 28)) {
1342 qemu_check_nic_model(&nd_table
[0], "stellaris");
1344 enet
= qdev_create(NULL
, "stellaris_enet");
1345 qdev_set_nic_properties(enet
, &nd_table
[0]);
1346 qdev_init_nofail(enet
);
1347 sysbus_mmio_map(SYS_BUS_DEVICE(enet
), 0, 0x40048000);
1348 sysbus_connect_irq(SYS_BUS_DEVICE(enet
), 0, qdev_get_gpio_in(nvic
, 42));
1350 if (board
->peripherals
& BP_GAMEPAD
) {
1351 qemu_irq gpad_irq
[5];
1352 static const int gpad_keycode
[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1354 gpad_irq
[0] = qemu_irq_invert(gpio_in
[GPIO_E
][0]); /* up */
1355 gpad_irq
[1] = qemu_irq_invert(gpio_in
[GPIO_E
][1]); /* down */
1356 gpad_irq
[2] = qemu_irq_invert(gpio_in
[GPIO_E
][2]); /* left */
1357 gpad_irq
[3] = qemu_irq_invert(gpio_in
[GPIO_E
][3]); /* right */
1358 gpad_irq
[4] = qemu_irq_invert(gpio_in
[GPIO_F
][1]); /* select */
1360 stellaris_gamepad_init(5, gpad_irq
, gpad_keycode
);
1362 for (i
= 0; i
< 7; i
++) {
1363 if (board
->dc4
& (1 << i
)) {
1364 for (j
= 0; j
< 8; j
++) {
1365 if (gpio_out
[i
][j
]) {
1366 qdev_connect_gpio_out(gpio_dev
[i
], j
, gpio_out
[i
][j
]);
1373 /* FIXME: Figure out how to generate these from stellaris_boards. */
1374 static void lm3s811evb_init(MachineState
*machine
)
1376 const char *cpu_model
= machine
->cpu_model
;
1377 const char *kernel_filename
= machine
->kernel_filename
;
1378 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[0]);
1381 static void lm3s6965evb_init(MachineState
*machine
)
1383 const char *cpu_model
= machine
->cpu_model
;
1384 const char *kernel_filename
= machine
->kernel_filename
;
1385 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[1]);
1388 static void lm3s811evb_class_init(ObjectClass
*oc
, void *data
)
1390 MachineClass
*mc
= MACHINE_CLASS(oc
);
1392 mc
->desc
= "Stellaris LM3S811EVB";
1393 mc
->init
= lm3s811evb_init
;
1396 static const TypeInfo lm3s811evb_type
= {
1397 .name
= MACHINE_TYPE_NAME("lm3s811evb"),
1398 .parent
= TYPE_MACHINE
,
1399 .class_init
= lm3s811evb_class_init
,
1402 static void lm3s6965evb_class_init(ObjectClass
*oc
, void *data
)
1404 MachineClass
*mc
= MACHINE_CLASS(oc
);
1406 mc
->desc
= "Stellaris LM3S6965EVB";
1407 mc
->init
= lm3s6965evb_init
;
1410 static const TypeInfo lm3s6965evb_type
= {
1411 .name
= MACHINE_TYPE_NAME("lm3s6965evb"),
1412 .parent
= TYPE_MACHINE
,
1413 .class_init
= lm3s6965evb_class_init
,
1416 static void stellaris_machine_init(void)
1418 type_register_static(&lm3s811evb_type
);
1419 type_register_static(&lm3s6965evb_type
);
1422 machine_init(stellaris_machine_init
)
1424 static void stellaris_i2c_class_init(ObjectClass
*klass
, void *data
)
1426 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
1428 sdc
->init
= stellaris_i2c_init
;
1431 static const TypeInfo stellaris_i2c_info
= {
1432 .name
= TYPE_STELLARIS_I2C
,
1433 .parent
= TYPE_SYS_BUS_DEVICE
,
1434 .instance_size
= sizeof(stellaris_i2c_state
),
1435 .class_init
= stellaris_i2c_class_init
,
1438 static void stellaris_gptm_class_init(ObjectClass
*klass
, void *data
)
1440 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
1442 sdc
->init
= stellaris_gptm_init
;
1445 static const TypeInfo stellaris_gptm_info
= {
1446 .name
= TYPE_STELLARIS_GPTM
,
1447 .parent
= TYPE_SYS_BUS_DEVICE
,
1448 .instance_size
= sizeof(gptm_state
),
1449 .class_init
= stellaris_gptm_class_init
,
1452 static void stellaris_adc_class_init(ObjectClass
*klass
, void *data
)
1454 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
1456 sdc
->init
= stellaris_adc_init
;
1459 static const TypeInfo stellaris_adc_info
= {
1460 .name
= TYPE_STELLARIS_ADC
,
1461 .parent
= TYPE_SYS_BUS_DEVICE
,
1462 .instance_size
= sizeof(stellaris_adc_state
),
1463 .class_init
= stellaris_adc_class_init
,
1466 static void stellaris_register_types(void)
1468 type_register_static(&stellaris_i2c_info
);
1469 type_register_static(&stellaris_gptm_info
);
1470 type_register_static(&stellaris_adc_info
);
1473 type_init(stellaris_register_types
)