2 * Luminary Micro Stellaris peripherals
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
14 #include "qemu-timer.h"
18 #include "exec-memory.h"
28 #define BP_OLED_I2C 0x01
29 #define BP_OLED_SSI 0x02
30 #define BP_GAMEPAD 0x04
32 typedef const struct {
42 } stellaris_board_info
;
44 /* General purpose timer module. */
46 typedef struct gptm_state
{
56 uint32_t match_prescale
[2];
59 struct gptm_state
*opaque
[2];
61 /* The timers have an alternate output used to trigger the ADC. */
66 static void gptm_update_irq(gptm_state
*s
)
69 level
= (s
->state
& s
->mask
) != 0;
70 qemu_set_irq(s
->irq
, level
);
73 static void gptm_stop(gptm_state
*s
, int n
)
75 qemu_del_timer(s
->timer
[n
]);
78 static void gptm_reload(gptm_state
*s
, int n
, int reset
)
82 tick
= qemu_get_clock_ns(vm_clock
);
87 /* 32-bit CountDown. */
89 count
= s
->load
[0] | (s
->load
[1] << 16);
90 tick
+= (int64_t)count
* system_clock_scale
;
91 } else if (s
->config
== 1) {
92 /* 32-bit RTC. 1Hz tick. */
93 tick
+= get_ticks_per_sec();
94 } else if (s
->mode
[n
] == 0xa) {
95 /* PWM mode. Not implemented. */
97 hw_error("TODO: 16-bit timer mode 0x%x\n", s
->mode
[n
]);
100 qemu_mod_timer(s
->timer
[n
], tick
);
103 static void gptm_tick(void *opaque
)
105 gptm_state
**p
= (gptm_state
**)opaque
;
111 if (s
->config
== 0) {
113 if ((s
->control
& 0x20)) {
114 /* Output trigger. */
115 qemu_irq_pulse(s
->trigger
);
117 if (s
->mode
[0] & 1) {
122 gptm_reload(s
, 0, 0);
124 } else if (s
->config
== 1) {
128 match
= s
->match
[0] | (s
->match
[1] << 16);
134 gptm_reload(s
, 0, 0);
135 } else if (s
->mode
[n
] == 0xa) {
136 /* PWM mode. Not implemented. */
138 hw_error("TODO: 16-bit timer mode 0x%x\n", s
->mode
[n
]);
143 static uint32_t gptm_read(void *opaque
, target_phys_addr_t offset
)
145 gptm_state
*s
= (gptm_state
*)opaque
;
150 case 0x04: /* TAMR */
152 case 0x08: /* TBMR */
161 return s
->state
& s
->mask
;
164 case 0x28: /* TAILR */
165 return s
->load
[0] | ((s
->config
< 4) ? (s
->load
[1] << 16) : 0);
166 case 0x2c: /* TBILR */
168 case 0x30: /* TAMARCHR */
169 return s
->match
[0] | ((s
->config
< 4) ? (s
->match
[1] << 16) : 0);
170 case 0x34: /* TBMATCHR */
172 case 0x38: /* TAPR */
173 return s
->prescale
[0];
174 case 0x3c: /* TBPR */
175 return s
->prescale
[1];
176 case 0x40: /* TAPMR */
177 return s
->match_prescale
[0];
178 case 0x44: /* TBPMR */
179 return s
->match_prescale
[1];
184 hw_error("TODO: Timer value read\n");
186 hw_error("gptm_read: Bad offset 0x%x\n", (int)offset
);
191 static void gptm_write(void *opaque
, target_phys_addr_t offset
, uint32_t value
)
193 gptm_state
*s
= (gptm_state
*)opaque
;
196 /* The timers should be disabled before changing the configuration.
197 We take advantage of this and defer everything until the timer
203 case 0x04: /* TAMR */
206 case 0x08: /* TBMR */
212 /* TODO: Implement pause. */
213 if ((oldval
^ value
) & 1) {
215 gptm_reload(s
, 0, 1);
220 if (((oldval
^ value
) & 0x100) && s
->config
>= 4) {
222 gptm_reload(s
, 1, 1);
229 s
->mask
= value
& 0x77;
235 case 0x28: /* TAILR */
236 s
->load
[0] = value
& 0xffff;
238 s
->load
[1] = value
>> 16;
241 case 0x2c: /* TBILR */
242 s
->load
[1] = value
& 0xffff;
244 case 0x30: /* TAMARCHR */
245 s
->match
[0] = value
& 0xffff;
247 s
->match
[1] = value
>> 16;
250 case 0x34: /* TBMATCHR */
251 s
->match
[1] = value
>> 16;
253 case 0x38: /* TAPR */
254 s
->prescale
[0] = value
;
256 case 0x3c: /* TBPR */
257 s
->prescale
[1] = value
;
259 case 0x40: /* TAPMR */
260 s
->match_prescale
[0] = value
;
262 case 0x44: /* TBPMR */
263 s
->match_prescale
[0] = value
;
266 hw_error("gptm_write: Bad offset 0x%x\n", (int)offset
);
271 static CPUReadMemoryFunc
* const gptm_readfn
[] = {
277 static CPUWriteMemoryFunc
* const gptm_writefn
[] = {
283 static const VMStateDescription vmstate_stellaris_gptm
= {
284 .name
= "stellaris_gptm",
286 .minimum_version_id
= 1,
287 .minimum_version_id_old
= 1,
288 .fields
= (VMStateField
[]) {
289 VMSTATE_UINT32(config
, gptm_state
),
290 VMSTATE_UINT32_ARRAY(mode
, gptm_state
, 2),
291 VMSTATE_UINT32(control
, gptm_state
),
292 VMSTATE_UINT32(state
, gptm_state
),
293 VMSTATE_UINT32(mask
, gptm_state
),
295 VMSTATE_UINT32_ARRAY(load
, gptm_state
, 2),
296 VMSTATE_UINT32_ARRAY(match
, gptm_state
, 2),
297 VMSTATE_UINT32_ARRAY(prescale
, gptm_state
, 2),
298 VMSTATE_UINT32_ARRAY(match_prescale
, gptm_state
, 2),
299 VMSTATE_UINT32(rtc
, gptm_state
),
300 VMSTATE_INT64_ARRAY(tick
, gptm_state
, 2),
301 VMSTATE_TIMER_ARRAY(timer
, gptm_state
, 2),
302 VMSTATE_END_OF_LIST()
306 static int stellaris_gptm_init(SysBusDevice
*dev
)
309 gptm_state
*s
= FROM_SYSBUS(gptm_state
, dev
);
311 sysbus_init_irq(dev
, &s
->irq
);
312 qdev_init_gpio_out(&dev
->qdev
, &s
->trigger
, 1);
314 iomemtype
= cpu_register_io_memory(gptm_readfn
,
316 DEVICE_NATIVE_ENDIAN
);
317 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
319 s
->opaque
[0] = s
->opaque
[1] = s
;
320 s
->timer
[0] = qemu_new_timer_ns(vm_clock
, gptm_tick
, &s
->opaque
[0]);
321 s
->timer
[1] = qemu_new_timer_ns(vm_clock
, gptm_tick
, &s
->opaque
[1]);
322 vmstate_register(&dev
->qdev
, -1, &vmstate_stellaris_gptm
, s
);
327 /* System controller. */
346 stellaris_board_info
*board
;
349 static void ssys_update(ssys_state
*s
)
351 qemu_set_irq(s
->irq
, (s
->int_status
& s
->int_mask
) != 0);
354 static uint32_t pllcfg_sandstorm
[16] = {
356 0x1ae0, /* 1.8432 Mhz */
358 0xd573, /* 2.4576 Mhz */
359 0x37a6, /* 3.57954 Mhz */
360 0x1ae2, /* 3.6864 Mhz */
362 0x98bc, /* 4.906 Mhz */
363 0x935b, /* 4.9152 Mhz */
365 0x4dee, /* 5.12 Mhz */
367 0x75db, /* 6.144 Mhz */
368 0x1ae6, /* 7.3728 Mhz */
370 0x585b /* 8.192 Mhz */
373 static uint32_t pllcfg_fury
[16] = {
375 0x1b20, /* 1.8432 Mhz */
377 0xf42b, /* 2.4576 Mhz */
378 0x37e3, /* 3.57954 Mhz */
379 0x1b21, /* 3.6864 Mhz */
381 0x98ee, /* 4.906 Mhz */
382 0xd5b4, /* 4.9152 Mhz */
384 0x4e27, /* 5.12 Mhz */
386 0xec1c, /* 6.144 Mhz */
387 0x1b23, /* 7.3728 Mhz */
389 0xb11c /* 8.192 Mhz */
392 #define DID0_VER_MASK 0x70000000
393 #define DID0_VER_0 0x00000000
394 #define DID0_VER_1 0x10000000
396 #define DID0_CLASS_MASK 0x00FF0000
397 #define DID0_CLASS_SANDSTORM 0x00000000
398 #define DID0_CLASS_FURY 0x00010000
400 static int ssys_board_class(const ssys_state
*s
)
402 uint32_t did0
= s
->board
->did0
;
403 switch (did0
& DID0_VER_MASK
) {
405 return DID0_CLASS_SANDSTORM
;
407 switch (did0
& DID0_CLASS_MASK
) {
408 case DID0_CLASS_SANDSTORM
:
409 case DID0_CLASS_FURY
:
410 return did0
& DID0_CLASS_MASK
;
412 /* for unknown classes, fall through */
414 hw_error("ssys_board_class: Unknown class 0x%08x\n", did0
);
418 static uint64_t ssys_read(void *opaque
, target_phys_addr_t offset
,
421 ssys_state
*s
= (ssys_state
*)opaque
;
424 case 0x000: /* DID0 */
425 return s
->board
->did0
;
426 case 0x004: /* DID1 */
427 return s
->board
->did1
;
428 case 0x008: /* DC0 */
429 return s
->board
->dc0
;
430 case 0x010: /* DC1 */
431 return s
->board
->dc1
;
432 case 0x014: /* DC2 */
433 return s
->board
->dc2
;
434 case 0x018: /* DC3 */
435 return s
->board
->dc3
;
436 case 0x01c: /* DC4 */
437 return s
->board
->dc4
;
438 case 0x030: /* PBORCTL */
440 case 0x034: /* LDOPCTL */
442 case 0x040: /* SRCR0 */
444 case 0x044: /* SRCR1 */
446 case 0x048: /* SRCR2 */
448 case 0x050: /* RIS */
449 return s
->int_status
;
450 case 0x054: /* IMC */
452 case 0x058: /* MISC */
453 return s
->int_status
& s
->int_mask
;
454 case 0x05c: /* RESC */
456 case 0x060: /* RCC */
458 case 0x064: /* PLLCFG */
461 xtal
= (s
->rcc
>> 6) & 0xf;
462 switch (ssys_board_class(s
)) {
463 case DID0_CLASS_FURY
:
464 return pllcfg_fury
[xtal
];
465 case DID0_CLASS_SANDSTORM
:
466 return pllcfg_sandstorm
[xtal
];
468 hw_error("ssys_read: Unhandled class for PLLCFG read.\n");
472 case 0x070: /* RCC2 */
474 case 0x100: /* RCGC0 */
476 case 0x104: /* RCGC1 */
478 case 0x108: /* RCGC2 */
480 case 0x110: /* SCGC0 */
482 case 0x114: /* SCGC1 */
484 case 0x118: /* SCGC2 */
486 case 0x120: /* DCGC0 */
488 case 0x124: /* DCGC1 */
490 case 0x128: /* DCGC2 */
492 case 0x150: /* CLKVCLR */
494 case 0x160: /* LDOARST */
496 case 0x1e0: /* USER0 */
498 case 0x1e4: /* USER1 */
501 hw_error("ssys_read: Bad offset 0x%x\n", (int)offset
);
506 static bool ssys_use_rcc2(ssys_state
*s
)
508 return (s
->rcc2
>> 31) & 0x1;
512 * Caculate the sys. clock period in ms.
514 static void ssys_calculate_system_clock(ssys_state
*s
)
516 if (ssys_use_rcc2(s
)) {
517 system_clock_scale
= 5 * (((s
->rcc2
>> 23) & 0x3f) + 1);
519 system_clock_scale
= 5 * (((s
->rcc
>> 23) & 0xf) + 1);
523 static void ssys_write(void *opaque
, target_phys_addr_t offset
,
524 uint64_t value
, unsigned size
)
526 ssys_state
*s
= (ssys_state
*)opaque
;
529 case 0x030: /* PBORCTL */
530 s
->pborctl
= value
& 0xffff;
532 case 0x034: /* LDOPCTL */
533 s
->ldopctl
= value
& 0x1f;
535 case 0x040: /* SRCR0 */
536 case 0x044: /* SRCR1 */
537 case 0x048: /* SRCR2 */
538 fprintf(stderr
, "Peripheral reset not implemented\n");
540 case 0x054: /* IMC */
541 s
->int_mask
= value
& 0x7f;
543 case 0x058: /* MISC */
544 s
->int_status
&= ~value
;
546 case 0x05c: /* RESC */
547 s
->resc
= value
& 0x3f;
549 case 0x060: /* RCC */
550 if ((s
->rcc
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
552 s
->int_status
|= (1 << 6);
555 ssys_calculate_system_clock(s
);
557 case 0x070: /* RCC2 */
558 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
562 if ((s
->rcc2
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
564 s
->int_status
|= (1 << 6);
567 ssys_calculate_system_clock(s
);
569 case 0x100: /* RCGC0 */
572 case 0x104: /* RCGC1 */
575 case 0x108: /* RCGC2 */
578 case 0x110: /* SCGC0 */
581 case 0x114: /* SCGC1 */
584 case 0x118: /* SCGC2 */
587 case 0x120: /* DCGC0 */
590 case 0x124: /* DCGC1 */
593 case 0x128: /* DCGC2 */
596 case 0x150: /* CLKVCLR */
599 case 0x160: /* LDOARST */
603 hw_error("ssys_write: Bad offset 0x%x\n", (int)offset
);
608 static const MemoryRegionOps ssys_ops
= {
611 .endianness
= DEVICE_NATIVE_ENDIAN
,
614 static void ssys_reset(void *opaque
)
616 ssys_state
*s
= (ssys_state
*)opaque
;
621 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
624 s
->rcc2
= 0x07802810;
631 static int stellaris_sys_post_load(void *opaque
, int version_id
)
633 ssys_state
*s
= opaque
;
635 ssys_calculate_system_clock(s
);
640 static const VMStateDescription vmstate_stellaris_sys
= {
641 .name
= "stellaris_sys",
643 .minimum_version_id
= 1,
644 .minimum_version_id_old
= 1,
645 .post_load
= stellaris_sys_post_load
,
646 .fields
= (VMStateField
[]) {
647 VMSTATE_UINT32(pborctl
, ssys_state
),
648 VMSTATE_UINT32(ldopctl
, ssys_state
),
649 VMSTATE_UINT32(int_mask
, ssys_state
),
650 VMSTATE_UINT32(int_status
, ssys_state
),
651 VMSTATE_UINT32(resc
, ssys_state
),
652 VMSTATE_UINT32(rcc
, ssys_state
),
653 VMSTATE_UINT32_V(rcc2
, ssys_state
, 2),
654 VMSTATE_UINT32_ARRAY(rcgc
, ssys_state
, 3),
655 VMSTATE_UINT32_ARRAY(scgc
, ssys_state
, 3),
656 VMSTATE_UINT32_ARRAY(dcgc
, ssys_state
, 3),
657 VMSTATE_UINT32(clkvclr
, ssys_state
),
658 VMSTATE_UINT32(ldoarst
, ssys_state
),
659 VMSTATE_END_OF_LIST()
663 static int stellaris_sys_init(uint32_t base
, qemu_irq irq
,
664 stellaris_board_info
* board
,
669 s
= (ssys_state
*)g_malloc0(sizeof(ssys_state
));
672 /* Most devices come preprogrammed with a MAC address in the user data. */
673 s
->user0
= macaddr
[0] | (macaddr
[1] << 8) | (macaddr
[2] << 16);
674 s
->user1
= macaddr
[3] | (macaddr
[4] << 8) | (macaddr
[5] << 16);
676 memory_region_init_io(&s
->iomem
, &ssys_ops
, s
, "ssys", 0x00001000);
677 memory_region_add_subregion(get_system_memory(), base
, &s
->iomem
);
679 vmstate_register(NULL
, -1, &vmstate_stellaris_sys
, s
);
684 /* I2C controller. */
698 } stellaris_i2c_state
;
700 #define STELLARIS_I2C_MCS_BUSY 0x01
701 #define STELLARIS_I2C_MCS_ERROR 0x02
702 #define STELLARIS_I2C_MCS_ADRACK 0x04
703 #define STELLARIS_I2C_MCS_DATACK 0x08
704 #define STELLARIS_I2C_MCS_ARBLST 0x10
705 #define STELLARIS_I2C_MCS_IDLE 0x20
706 #define STELLARIS_I2C_MCS_BUSBSY 0x40
708 static uint64_t stellaris_i2c_read(void *opaque
, target_phys_addr_t offset
,
711 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
717 /* We don't emulate timing, so the controller is never busy. */
718 return s
->mcs
| STELLARIS_I2C_MCS_IDLE
;
721 case 0x0c: /* MTPR */
723 case 0x10: /* MIMR */
725 case 0x14: /* MRIS */
727 case 0x18: /* MMIS */
728 return s
->mris
& s
->mimr
;
732 hw_error("strllaris_i2c_read: Bad offset 0x%x\n", (int)offset
);
737 static void stellaris_i2c_update(stellaris_i2c_state
*s
)
741 level
= (s
->mris
& s
->mimr
) != 0;
742 qemu_set_irq(s
->irq
, level
);
745 static void stellaris_i2c_write(void *opaque
, target_phys_addr_t offset
,
746 uint64_t value
, unsigned size
)
748 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
752 s
->msa
= value
& 0xff;
755 if ((s
->mcr
& 0x10) == 0) {
756 /* Disabled. Do nothing. */
759 /* Grab the bus if this is starting a transfer. */
760 if ((value
& 2) && (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
761 if (i2c_start_transfer(s
->bus
, s
->msa
>> 1, s
->msa
& 1)) {
762 s
->mcs
|= STELLARIS_I2C_MCS_ARBLST
;
764 s
->mcs
&= ~STELLARIS_I2C_MCS_ARBLST
;
765 s
->mcs
|= STELLARIS_I2C_MCS_BUSBSY
;
768 /* If we don't have the bus then indicate an error. */
769 if (!i2c_bus_busy(s
->bus
)
770 || (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
771 s
->mcs
|= STELLARIS_I2C_MCS_ERROR
;
774 s
->mcs
&= ~STELLARIS_I2C_MCS_ERROR
;
776 /* Transfer a byte. */
777 /* TODO: Handle errors. */
780 s
->mdr
= i2c_recv(s
->bus
) & 0xff;
783 i2c_send(s
->bus
, s
->mdr
);
785 /* Raise an interrupt. */
789 /* Finish transfer. */
790 i2c_end_transfer(s
->bus
);
791 s
->mcs
&= ~STELLARIS_I2C_MCS_BUSBSY
;
795 s
->mdr
= value
& 0xff;
797 case 0x0c: /* MTPR */
798 s
->mtpr
= value
& 0xff;
800 case 0x10: /* MIMR */
803 case 0x1c: /* MICR */
809 "stellaris_i2c_write: Loopback not implemented\n");
812 "stellaris_i2c_write: Slave mode not implemented\n");
813 s
->mcr
= value
& 0x31;
816 hw_error("stellaris_i2c_write: Bad offset 0x%x\n",
819 stellaris_i2c_update(s
);
822 static void stellaris_i2c_reset(stellaris_i2c_state
*s
)
824 if (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
)
825 i2c_end_transfer(s
->bus
);
834 stellaris_i2c_update(s
);
837 static const MemoryRegionOps stellaris_i2c_ops
= {
838 .read
= stellaris_i2c_read
,
839 .write
= stellaris_i2c_write
,
840 .endianness
= DEVICE_NATIVE_ENDIAN
,
843 static const VMStateDescription vmstate_stellaris_i2c
= {
844 .name
= "stellaris_i2c",
846 .minimum_version_id
= 1,
847 .minimum_version_id_old
= 1,
848 .fields
= (VMStateField
[]) {
849 VMSTATE_UINT32(msa
, stellaris_i2c_state
),
850 VMSTATE_UINT32(mcs
, stellaris_i2c_state
),
851 VMSTATE_UINT32(mdr
, stellaris_i2c_state
),
852 VMSTATE_UINT32(mtpr
, stellaris_i2c_state
),
853 VMSTATE_UINT32(mimr
, stellaris_i2c_state
),
854 VMSTATE_UINT32(mris
, stellaris_i2c_state
),
855 VMSTATE_UINT32(mcr
, stellaris_i2c_state
),
856 VMSTATE_END_OF_LIST()
860 static int stellaris_i2c_init(SysBusDevice
* dev
)
862 stellaris_i2c_state
*s
= FROM_SYSBUS(stellaris_i2c_state
, dev
);
865 sysbus_init_irq(dev
, &s
->irq
);
866 bus
= i2c_init_bus(&dev
->qdev
, "i2c");
869 memory_region_init_io(&s
->iomem
, &stellaris_i2c_ops
, s
,
871 sysbus_init_mmio_region(dev
, &s
->iomem
);
872 /* ??? For now we only implement the master interface. */
873 stellaris_i2c_reset(s
);
874 vmstate_register(&dev
->qdev
, -1, &vmstate_stellaris_i2c
, s
);
878 /* Analogue to Digital Converter. This is only partially implemented,
879 enough for applications that use a combined ADC and timer tick. */
881 #define STELLARIS_ADC_EM_CONTROLLER 0
882 #define STELLARIS_ADC_EM_COMP 1
883 #define STELLARIS_ADC_EM_EXTERNAL 4
884 #define STELLARIS_ADC_EM_TIMER 5
885 #define STELLARIS_ADC_EM_PWM0 6
886 #define STELLARIS_ADC_EM_PWM1 7
887 #define STELLARIS_ADC_EM_PWM2 8
889 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
890 #define STELLARIS_ADC_FIFO_FULL 0x1000
912 } stellaris_adc_state
;
914 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state
*s
, int n
)
918 tail
= s
->fifo
[n
].state
& 0xf;
919 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_EMPTY
) {
922 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf) | ((tail
+ 1) & 0xf);
923 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_FULL
;
924 if (tail
+ 1 == ((s
->fifo
[n
].state
>> 4) & 0xf))
925 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_EMPTY
;
927 return s
->fifo
[n
].data
[tail
];
930 static void stellaris_adc_fifo_write(stellaris_adc_state
*s
, int n
,
935 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
936 FIFO fir each sequencer. */
937 head
= (s
->fifo
[n
].state
>> 4) & 0xf;
938 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_FULL
) {
942 s
->fifo
[n
].data
[head
] = value
;
943 head
= (head
+ 1) & 0xf;
944 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_EMPTY
;
945 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf0) | (head
<< 4);
946 if ((s
->fifo
[n
].state
& 0xf) == head
)
947 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_FULL
;
950 static void stellaris_adc_update(stellaris_adc_state
*s
)
955 for (n
= 0; n
< 4; n
++) {
956 level
= (s
->ris
& s
->im
& (1 << n
)) != 0;
957 qemu_set_irq(s
->irq
[n
], level
);
961 static void stellaris_adc_trigger(void *opaque
, int irq
, int level
)
963 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
966 for (n
= 0; n
< 4; n
++) {
967 if ((s
->actss
& (1 << n
)) == 0) {
971 if (((s
->emux
>> (n
* 4)) & 0xff) != 5) {
975 /* Some applications use the ADC as a random number source, so introduce
976 some variation into the signal. */
977 s
->noise
= s
->noise
* 314159 + 1;
978 /* ??? actual inputs not implemented. Return an arbitrary value. */
979 stellaris_adc_fifo_write(s
, n
, 0x200 + ((s
->noise
>> 16) & 7));
981 stellaris_adc_update(s
);
985 static void stellaris_adc_reset(stellaris_adc_state
*s
)
989 for (n
= 0; n
< 4; n
++) {
992 s
->fifo
[n
].state
= STELLARIS_ADC_FIFO_EMPTY
;
996 static uint64_t stellaris_adc_read(void *opaque
, target_phys_addr_t offset
,
999 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1001 /* TODO: Implement this. */
1002 if (offset
>= 0x40 && offset
< 0xc0) {
1004 n
= (offset
- 0x40) >> 5;
1005 switch (offset
& 0x1f) {
1006 case 0x00: /* SSMUX */
1008 case 0x04: /* SSCTL */
1010 case 0x08: /* SSFIFO */
1011 return stellaris_adc_fifo_read(s
, n
);
1012 case 0x0c: /* SSFSTAT */
1013 return s
->fifo
[n
].state
;
1019 case 0x00: /* ACTSS */
1021 case 0x04: /* RIS */
1025 case 0x0c: /* ISC */
1026 return s
->ris
& s
->im
;
1027 case 0x10: /* OSTAT */
1029 case 0x14: /* EMUX */
1031 case 0x18: /* USTAT */
1033 case 0x20: /* SSPRI */
1035 case 0x30: /* SAC */
1038 hw_error("strllaris_adc_read: Bad offset 0x%x\n",
1044 static void stellaris_adc_write(void *opaque
, target_phys_addr_t offset
,
1045 uint64_t value
, unsigned size
)
1047 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1049 /* TODO: Implement this. */
1050 if (offset
>= 0x40 && offset
< 0xc0) {
1052 n
= (offset
- 0x40) >> 5;
1053 switch (offset
& 0x1f) {
1054 case 0x00: /* SSMUX */
1055 s
->ssmux
[n
] = value
& 0x33333333;
1057 case 0x04: /* SSCTL */
1059 hw_error("ADC: Unimplemented sequence %" PRIx64
"\n",
1062 s
->ssctl
[n
] = value
;
1069 case 0x00: /* ACTSS */
1070 s
->actss
= value
& 0xf;
1075 case 0x0c: /* ISC */
1078 case 0x10: /* OSTAT */
1081 case 0x14: /* EMUX */
1084 case 0x18: /* USTAT */
1087 case 0x20: /* SSPRI */
1090 case 0x28: /* PSSI */
1091 hw_error("Not implemented: ADC sample initiate\n");
1093 case 0x30: /* SAC */
1097 hw_error("stellaris_adc_write: Bad offset 0x%x\n", (int)offset
);
1099 stellaris_adc_update(s
);
1102 static const MemoryRegionOps stellaris_adc_ops
= {
1103 .read
= stellaris_adc_read
,
1104 .write
= stellaris_adc_write
,
1105 .endianness
= DEVICE_NATIVE_ENDIAN
,
1108 static const VMStateDescription vmstate_stellaris_adc
= {
1109 .name
= "stellaris_adc",
1111 .minimum_version_id
= 1,
1112 .minimum_version_id_old
= 1,
1113 .fields
= (VMStateField
[]) {
1114 VMSTATE_UINT32(actss
, stellaris_adc_state
),
1115 VMSTATE_UINT32(ris
, stellaris_adc_state
),
1116 VMSTATE_UINT32(im
, stellaris_adc_state
),
1117 VMSTATE_UINT32(emux
, stellaris_adc_state
),
1118 VMSTATE_UINT32(ostat
, stellaris_adc_state
),
1119 VMSTATE_UINT32(ustat
, stellaris_adc_state
),
1120 VMSTATE_UINT32(sspri
, stellaris_adc_state
),
1121 VMSTATE_UINT32(sac
, stellaris_adc_state
),
1122 VMSTATE_UINT32(fifo
[0].state
, stellaris_adc_state
),
1123 VMSTATE_UINT32_ARRAY(fifo
[0].data
, stellaris_adc_state
, 16),
1124 VMSTATE_UINT32(ssmux
[0], stellaris_adc_state
),
1125 VMSTATE_UINT32(ssctl
[0], stellaris_adc_state
),
1126 VMSTATE_UINT32(fifo
[1].state
, stellaris_adc_state
),
1127 VMSTATE_UINT32_ARRAY(fifo
[1].data
, stellaris_adc_state
, 16),
1128 VMSTATE_UINT32(ssmux
[1], stellaris_adc_state
),
1129 VMSTATE_UINT32(ssctl
[1], stellaris_adc_state
),
1130 VMSTATE_UINT32(fifo
[2].state
, stellaris_adc_state
),
1131 VMSTATE_UINT32_ARRAY(fifo
[2].data
, stellaris_adc_state
, 16),
1132 VMSTATE_UINT32(ssmux
[2], stellaris_adc_state
),
1133 VMSTATE_UINT32(ssctl
[2], stellaris_adc_state
),
1134 VMSTATE_UINT32(fifo
[3].state
, stellaris_adc_state
),
1135 VMSTATE_UINT32_ARRAY(fifo
[3].data
, stellaris_adc_state
, 16),
1136 VMSTATE_UINT32(ssmux
[3], stellaris_adc_state
),
1137 VMSTATE_UINT32(ssctl
[3], stellaris_adc_state
),
1138 VMSTATE_UINT32(noise
, stellaris_adc_state
),
1139 VMSTATE_END_OF_LIST()
1143 static int stellaris_adc_init(SysBusDevice
*dev
)
1145 stellaris_adc_state
*s
= FROM_SYSBUS(stellaris_adc_state
, dev
);
1148 for (n
= 0; n
< 4; n
++) {
1149 sysbus_init_irq(dev
, &s
->irq
[n
]);
1152 memory_region_init_io(&s
->iomem
, &stellaris_adc_ops
, s
,
1154 sysbus_init_mmio_region(dev
, &s
->iomem
);
1155 stellaris_adc_reset(s
);
1156 qdev_init_gpio_in(&dev
->qdev
, stellaris_adc_trigger
, 1);
1157 vmstate_register(&dev
->qdev
, -1, &vmstate_stellaris_adc
, s
);
1161 /* Some boards have both an OLED controller and SD card connected to
1162 the same SSI port, with the SD card chip select connected to a
1163 GPIO pin. Technically the OLED chip select is connected to the SSI
1164 Fss pin. We do not bother emulating that as both devices should
1165 never be selected simultaneously, and our OLED controller ignores stray
1166 0xff commands that occur when deselecting the SD card. */
1173 } stellaris_ssi_bus_state
;
1175 static void stellaris_ssi_bus_select(void *opaque
, int irq
, int level
)
1177 stellaris_ssi_bus_state
*s
= (stellaris_ssi_bus_state
*)opaque
;
1179 s
->current_dev
= level
;
1182 static uint32_t stellaris_ssi_bus_transfer(SSISlave
*dev
, uint32_t val
)
1184 stellaris_ssi_bus_state
*s
= FROM_SSI_SLAVE(stellaris_ssi_bus_state
, dev
);
1186 return ssi_transfer(s
->bus
[s
->current_dev
], val
);
1189 static const VMStateDescription vmstate_stellaris_ssi_bus
= {
1190 .name
= "stellaris_ssi_bus",
1192 .minimum_version_id
= 1,
1193 .minimum_version_id_old
= 1,
1194 .fields
= (VMStateField
[]) {
1195 VMSTATE_INT32(current_dev
, stellaris_ssi_bus_state
),
1196 VMSTATE_END_OF_LIST()
1200 static int stellaris_ssi_bus_init(SSISlave
*dev
)
1202 stellaris_ssi_bus_state
*s
= FROM_SSI_SLAVE(stellaris_ssi_bus_state
, dev
);
1204 s
->bus
[0] = ssi_create_bus(&dev
->qdev
, "ssi0");
1205 s
->bus
[1] = ssi_create_bus(&dev
->qdev
, "ssi1");
1206 qdev_init_gpio_in(&dev
->qdev
, stellaris_ssi_bus_select
, 1);
1208 vmstate_register(&dev
->qdev
, -1, &vmstate_stellaris_ssi_bus
, s
);
1213 static stellaris_board_info stellaris_boards
[] = {
1217 0x001f001f, /* dc0 */
1227 0x00ff007f, /* dc0 */
1232 BP_OLED_SSI
| BP_GAMEPAD
1236 static void stellaris_init(const char *kernel_filename
, const char *cpu_model
,
1237 stellaris_board_info
*board
)
1239 static const int uart_irq
[] = {5, 6, 33, 34};
1240 static const int timer_irq
[] = {19, 21, 23, 35};
1241 static const uint32_t gpio_addr
[7] =
1242 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1243 0x40024000, 0x40025000, 0x40026000};
1244 static const int gpio_irq
[7] = {0, 1, 2, 3, 4, 30, 31};
1246 MemoryRegion
*address_space_mem
= get_system_memory();
1248 DeviceState
*gpio_dev
[7];
1249 qemu_irq gpio_in
[7][8];
1250 qemu_irq gpio_out
[7][8];
1259 flash_size
= ((board
->dc0
& 0xffff) + 1) << 1;
1260 sram_size
= (board
->dc0
>> 18) + 1;
1261 pic
= armv7m_init(address_space_mem
,
1262 flash_size
, sram_size
, kernel_filename
, cpu_model
);
1264 if (board
->dc1
& (1 << 16)) {
1265 dev
= sysbus_create_varargs("stellaris-adc", 0x40038000,
1266 pic
[14], pic
[15], pic
[16], pic
[17], NULL
);
1267 adc
= qdev_get_gpio_in(dev
, 0);
1271 for (i
= 0; i
< 4; i
++) {
1272 if (board
->dc2
& (0x10000 << i
)) {
1273 dev
= sysbus_create_simple("stellaris-gptm",
1274 0x40030000 + i
* 0x1000,
1276 /* TODO: This is incorrect, but we get away with it because
1277 the ADC output is only ever pulsed. */
1278 qdev_connect_gpio_out(dev
, 0, adc
);
1282 stellaris_sys_init(0x400fe000, pic
[28], board
, nd_table
[0].macaddr
.a
);
1284 for (i
= 0; i
< 7; i
++) {
1285 if (board
->dc4
& (1 << i
)) {
1286 gpio_dev
[i
] = sysbus_create_simple("pl061_luminary", gpio_addr
[i
],
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("stellaris-i2c", 0x40020000, pic
[8]);
1297 i2c
= (i2c_bus
*)qdev_get_child_bus(dev
, "i2c");
1298 if (board
->peripherals
& BP_OLED_I2C
) {
1299 i2c_create_slave(i2c
, "ssd0303", 0x3d);
1303 for (i
= 0; i
< 4; i
++) {
1304 if (board
->dc2
& (1 << i
)) {
1305 sysbus_create_simple("pl011_luminary", 0x4000c000 + i
* 0x1000,
1309 if (board
->dc2
& (1 << 4)) {
1310 dev
= sysbus_create_simple("pl022", 0x40008000, pic
[7]);
1311 if (board
->peripherals
& BP_OLED_SSI
) {
1315 bus
= qdev_get_child_bus(dev
, "ssi");
1316 mux
= ssi_create_slave(bus
, "evb6965-ssi");
1317 gpio_out
[GPIO_D
][0] = qdev_get_gpio_in(mux
, 0);
1319 bus
= qdev_get_child_bus(mux
, "ssi0");
1320 ssi_create_slave(bus
, "ssi-sd");
1322 bus
= qdev_get_child_bus(mux
, "ssi1");
1323 dev
= ssi_create_slave(bus
, "ssd0323");
1324 gpio_out
[GPIO_C
][7] = qdev_get_gpio_in(dev
, 0);
1326 /* Make sure the select pin is high. */
1327 qemu_irq_raise(gpio_out
[GPIO_D
][0]);
1330 if (board
->dc4
& (1 << 28)) {
1333 qemu_check_nic_model(&nd_table
[0], "stellaris");
1335 enet
= qdev_create(NULL
, "stellaris_enet");
1336 qdev_set_nic_properties(enet
, &nd_table
[0]);
1337 qdev_init_nofail(enet
);
1338 sysbus_mmio_map(sysbus_from_qdev(enet
), 0, 0x40048000);
1339 sysbus_connect_irq(sysbus_from_qdev(enet
), 0, pic
[42]);
1341 if (board
->peripherals
& BP_GAMEPAD
) {
1342 qemu_irq gpad_irq
[5];
1343 static const int gpad_keycode
[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1345 gpad_irq
[0] = qemu_irq_invert(gpio_in
[GPIO_E
][0]); /* up */
1346 gpad_irq
[1] = qemu_irq_invert(gpio_in
[GPIO_E
][1]); /* down */
1347 gpad_irq
[2] = qemu_irq_invert(gpio_in
[GPIO_E
][2]); /* left */
1348 gpad_irq
[3] = qemu_irq_invert(gpio_in
[GPIO_E
][3]); /* right */
1349 gpad_irq
[4] = qemu_irq_invert(gpio_in
[GPIO_F
][1]); /* select */
1351 stellaris_gamepad_init(5, gpad_irq
, gpad_keycode
);
1353 for (i
= 0; i
< 7; i
++) {
1354 if (board
->dc4
& (1 << i
)) {
1355 for (j
= 0; j
< 8; j
++) {
1356 if (gpio_out
[i
][j
]) {
1357 qdev_connect_gpio_out(gpio_dev
[i
], j
, gpio_out
[i
][j
]);
1364 /* FIXME: Figure out how to generate these from stellaris_boards. */
1365 static void lm3s811evb_init(ram_addr_t ram_size
,
1366 const char *boot_device
,
1367 const char *kernel_filename
, const char *kernel_cmdline
,
1368 const char *initrd_filename
, const char *cpu_model
)
1370 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[0]);
1373 static void lm3s6965evb_init(ram_addr_t ram_size
,
1374 const char *boot_device
,
1375 const char *kernel_filename
, const char *kernel_cmdline
,
1376 const char *initrd_filename
, const char *cpu_model
)
1378 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[1]);
1381 static QEMUMachine lm3s811evb_machine
= {
1382 .name
= "lm3s811evb",
1383 .desc
= "Stellaris LM3S811EVB",
1384 .init
= lm3s811evb_init
,
1387 static QEMUMachine lm3s6965evb_machine
= {
1388 .name
= "lm3s6965evb",
1389 .desc
= "Stellaris LM3S6965EVB",
1390 .init
= lm3s6965evb_init
,
1393 static void stellaris_machine_init(void)
1395 qemu_register_machine(&lm3s811evb_machine
);
1396 qemu_register_machine(&lm3s6965evb_machine
);
1399 machine_init(stellaris_machine_init
);
1401 static SSISlaveInfo stellaris_ssi_bus_info
= {
1402 .qdev
.name
= "evb6965-ssi",
1403 .qdev
.size
= sizeof(stellaris_ssi_bus_state
),
1404 .init
= stellaris_ssi_bus_init
,
1405 .transfer
= stellaris_ssi_bus_transfer
1408 static void stellaris_register_devices(void)
1410 sysbus_register_dev("stellaris-i2c", sizeof(stellaris_i2c_state
),
1411 stellaris_i2c_init
);
1412 sysbus_register_dev("stellaris-gptm", sizeof(gptm_state
),
1413 stellaris_gptm_init
);
1414 sysbus_register_dev("stellaris-adc", sizeof(stellaris_adc_state
),
1415 stellaris_adc_init
);
1416 ssi_register_slave(&stellaris_ssi_bus_info
);
1419 device_init(stellaris_register_devices
)