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. */
345 stellaris_board_info
*board
;
348 static void ssys_update(ssys_state
*s
)
350 qemu_set_irq(s
->irq
, (s
->int_status
& s
->int_mask
) != 0);
353 static uint32_t pllcfg_sandstorm
[16] = {
355 0x1ae0, /* 1.8432 Mhz */
357 0xd573, /* 2.4576 Mhz */
358 0x37a6, /* 3.57954 Mhz */
359 0x1ae2, /* 3.6864 Mhz */
361 0x98bc, /* 4.906 Mhz */
362 0x935b, /* 4.9152 Mhz */
364 0x4dee, /* 5.12 Mhz */
366 0x75db, /* 6.144 Mhz */
367 0x1ae6, /* 7.3728 Mhz */
369 0x585b /* 8.192 Mhz */
372 static uint32_t pllcfg_fury
[16] = {
374 0x1b20, /* 1.8432 Mhz */
376 0xf42b, /* 2.4576 Mhz */
377 0x37e3, /* 3.57954 Mhz */
378 0x1b21, /* 3.6864 Mhz */
380 0x98ee, /* 4.906 Mhz */
381 0xd5b4, /* 4.9152 Mhz */
383 0x4e27, /* 5.12 Mhz */
385 0xec1c, /* 6.144 Mhz */
386 0x1b23, /* 7.3728 Mhz */
388 0xb11c /* 8.192 Mhz */
391 #define DID0_VER_MASK 0x70000000
392 #define DID0_VER_0 0x00000000
393 #define DID0_VER_1 0x10000000
395 #define DID0_CLASS_MASK 0x00FF0000
396 #define DID0_CLASS_SANDSTORM 0x00000000
397 #define DID0_CLASS_FURY 0x00010000
399 static int ssys_board_class(const ssys_state
*s
)
401 uint32_t did0
= s
->board
->did0
;
402 switch (did0
& DID0_VER_MASK
) {
404 return DID0_CLASS_SANDSTORM
;
406 switch (did0
& DID0_CLASS_MASK
) {
407 case DID0_CLASS_SANDSTORM
:
408 case DID0_CLASS_FURY
:
409 return did0
& DID0_CLASS_MASK
;
411 /* for unknown classes, fall through */
413 hw_error("ssys_board_class: Unknown class 0x%08x\n", did0
);
417 static uint32_t ssys_read(void *opaque
, target_phys_addr_t offset
)
419 ssys_state
*s
= (ssys_state
*)opaque
;
422 case 0x000: /* DID0 */
423 return s
->board
->did0
;
424 case 0x004: /* DID1 */
425 return s
->board
->did1
;
426 case 0x008: /* DC0 */
427 return s
->board
->dc0
;
428 case 0x010: /* DC1 */
429 return s
->board
->dc1
;
430 case 0x014: /* DC2 */
431 return s
->board
->dc2
;
432 case 0x018: /* DC3 */
433 return s
->board
->dc3
;
434 case 0x01c: /* DC4 */
435 return s
->board
->dc4
;
436 case 0x030: /* PBORCTL */
438 case 0x034: /* LDOPCTL */
440 case 0x040: /* SRCR0 */
442 case 0x044: /* SRCR1 */
444 case 0x048: /* SRCR2 */
446 case 0x050: /* RIS */
447 return s
->int_status
;
448 case 0x054: /* IMC */
450 case 0x058: /* MISC */
451 return s
->int_status
& s
->int_mask
;
452 case 0x05c: /* RESC */
454 case 0x060: /* RCC */
456 case 0x064: /* PLLCFG */
459 xtal
= (s
->rcc
>> 6) & 0xf;
460 switch (ssys_board_class(s
)) {
461 case DID0_CLASS_FURY
:
462 return pllcfg_fury
[xtal
];
463 case DID0_CLASS_SANDSTORM
:
464 return pllcfg_sandstorm
[xtal
];
466 hw_error("ssys_read: Unhandled class for PLLCFG read.\n");
470 case 0x070: /* RCC2 */
472 case 0x100: /* RCGC0 */
474 case 0x104: /* RCGC1 */
476 case 0x108: /* RCGC2 */
478 case 0x110: /* SCGC0 */
480 case 0x114: /* SCGC1 */
482 case 0x118: /* SCGC2 */
484 case 0x120: /* DCGC0 */
486 case 0x124: /* DCGC1 */
488 case 0x128: /* DCGC2 */
490 case 0x150: /* CLKVCLR */
492 case 0x160: /* LDOARST */
494 case 0x1e0: /* USER0 */
496 case 0x1e4: /* USER1 */
499 hw_error("ssys_read: Bad offset 0x%x\n", (int)offset
);
504 static bool ssys_use_rcc2(ssys_state
*s
)
506 return (s
->rcc2
>> 31) & 0x1;
510 * Caculate the sys. clock period in ms.
512 static void ssys_calculate_system_clock(ssys_state
*s
)
514 if (ssys_use_rcc2(s
)) {
515 system_clock_scale
= 5 * (((s
->rcc2
>> 23) & 0x3f) + 1);
517 system_clock_scale
= 5 * (((s
->rcc
>> 23) & 0xf) + 1);
521 static void ssys_write(void *opaque
, target_phys_addr_t offset
, uint32_t value
)
523 ssys_state
*s
= (ssys_state
*)opaque
;
526 case 0x030: /* PBORCTL */
527 s
->pborctl
= value
& 0xffff;
529 case 0x034: /* LDOPCTL */
530 s
->ldopctl
= value
& 0x1f;
532 case 0x040: /* SRCR0 */
533 case 0x044: /* SRCR1 */
534 case 0x048: /* SRCR2 */
535 fprintf(stderr
, "Peripheral reset not implemented\n");
537 case 0x054: /* IMC */
538 s
->int_mask
= value
& 0x7f;
540 case 0x058: /* MISC */
541 s
->int_status
&= ~value
;
543 case 0x05c: /* RESC */
544 s
->resc
= value
& 0x3f;
546 case 0x060: /* RCC */
547 if ((s
->rcc
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
549 s
->int_status
|= (1 << 6);
552 ssys_calculate_system_clock(s
);
554 case 0x070: /* RCC2 */
555 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
559 if ((s
->rcc2
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
561 s
->int_status
|= (1 << 6);
564 ssys_calculate_system_clock(s
);
566 case 0x100: /* RCGC0 */
569 case 0x104: /* RCGC1 */
572 case 0x108: /* RCGC2 */
575 case 0x110: /* SCGC0 */
578 case 0x114: /* SCGC1 */
581 case 0x118: /* SCGC2 */
584 case 0x120: /* DCGC0 */
587 case 0x124: /* DCGC1 */
590 case 0x128: /* DCGC2 */
593 case 0x150: /* CLKVCLR */
596 case 0x160: /* LDOARST */
600 hw_error("ssys_write: Bad offset 0x%x\n", (int)offset
);
605 static CPUReadMemoryFunc
* const ssys_readfn
[] = {
611 static CPUWriteMemoryFunc
* const ssys_writefn
[] = {
617 static void ssys_reset(void *opaque
)
619 ssys_state
*s
= (ssys_state
*)opaque
;
624 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
627 s
->rcc2
= 0x07802810;
634 static int stellaris_sys_post_load(void *opaque
, int version_id
)
636 ssys_state
*s
= opaque
;
638 ssys_calculate_system_clock(s
);
643 static const VMStateDescription vmstate_stellaris_sys
= {
644 .name
= "stellaris_sys",
646 .minimum_version_id
= 1,
647 .minimum_version_id_old
= 1,
648 .post_load
= stellaris_sys_post_load
,
649 .fields
= (VMStateField
[]) {
650 VMSTATE_UINT32(pborctl
, ssys_state
),
651 VMSTATE_UINT32(ldopctl
, ssys_state
),
652 VMSTATE_UINT32(int_mask
, ssys_state
),
653 VMSTATE_UINT32(int_status
, ssys_state
),
654 VMSTATE_UINT32(resc
, ssys_state
),
655 VMSTATE_UINT32(rcc
, ssys_state
),
656 VMSTATE_UINT32_V(rcc2
, ssys_state
, 2),
657 VMSTATE_UINT32_ARRAY(rcgc
, ssys_state
, 3),
658 VMSTATE_UINT32_ARRAY(scgc
, ssys_state
, 3),
659 VMSTATE_UINT32_ARRAY(dcgc
, ssys_state
, 3),
660 VMSTATE_UINT32(clkvclr
, ssys_state
),
661 VMSTATE_UINT32(ldoarst
, ssys_state
),
662 VMSTATE_END_OF_LIST()
666 static int stellaris_sys_init(uint32_t base
, qemu_irq irq
,
667 stellaris_board_info
* board
,
673 s
= (ssys_state
*)g_malloc0(sizeof(ssys_state
));
676 /* Most devices come preprogrammed with a MAC address in the user data. */
677 s
->user0
= macaddr
[0] | (macaddr
[1] << 8) | (macaddr
[2] << 16);
678 s
->user1
= macaddr
[3] | (macaddr
[4] << 8) | (macaddr
[5] << 16);
680 iomemtype
= cpu_register_io_memory(ssys_readfn
,
682 DEVICE_NATIVE_ENDIAN
);
683 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
685 vmstate_register(NULL
, -1, &vmstate_stellaris_sys
, s
);
690 /* I2C controller. */
703 } stellaris_i2c_state
;
705 #define STELLARIS_I2C_MCS_BUSY 0x01
706 #define STELLARIS_I2C_MCS_ERROR 0x02
707 #define STELLARIS_I2C_MCS_ADRACK 0x04
708 #define STELLARIS_I2C_MCS_DATACK 0x08
709 #define STELLARIS_I2C_MCS_ARBLST 0x10
710 #define STELLARIS_I2C_MCS_IDLE 0x20
711 #define STELLARIS_I2C_MCS_BUSBSY 0x40
713 static uint32_t stellaris_i2c_read(void *opaque
, target_phys_addr_t offset
)
715 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
721 /* We don't emulate timing, so the controller is never busy. */
722 return s
->mcs
| STELLARIS_I2C_MCS_IDLE
;
725 case 0x0c: /* MTPR */
727 case 0x10: /* MIMR */
729 case 0x14: /* MRIS */
731 case 0x18: /* MMIS */
732 return s
->mris
& s
->mimr
;
736 hw_error("strllaris_i2c_read: Bad offset 0x%x\n", (int)offset
);
741 static void stellaris_i2c_update(stellaris_i2c_state
*s
)
745 level
= (s
->mris
& s
->mimr
) != 0;
746 qemu_set_irq(s
->irq
, level
);
749 static void stellaris_i2c_write(void *opaque
, target_phys_addr_t offset
,
752 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
756 s
->msa
= value
& 0xff;
759 if ((s
->mcr
& 0x10) == 0) {
760 /* Disabled. Do nothing. */
763 /* Grab the bus if this is starting a transfer. */
764 if ((value
& 2) && (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
765 if (i2c_start_transfer(s
->bus
, s
->msa
>> 1, s
->msa
& 1)) {
766 s
->mcs
|= STELLARIS_I2C_MCS_ARBLST
;
768 s
->mcs
&= ~STELLARIS_I2C_MCS_ARBLST
;
769 s
->mcs
|= STELLARIS_I2C_MCS_BUSBSY
;
772 /* If we don't have the bus then indicate an error. */
773 if (!i2c_bus_busy(s
->bus
)
774 || (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
775 s
->mcs
|= STELLARIS_I2C_MCS_ERROR
;
778 s
->mcs
&= ~STELLARIS_I2C_MCS_ERROR
;
780 /* Transfer a byte. */
781 /* TODO: Handle errors. */
784 s
->mdr
= i2c_recv(s
->bus
) & 0xff;
787 i2c_send(s
->bus
, s
->mdr
);
789 /* Raise an interrupt. */
793 /* Finish transfer. */
794 i2c_end_transfer(s
->bus
);
795 s
->mcs
&= ~STELLARIS_I2C_MCS_BUSBSY
;
799 s
->mdr
= value
& 0xff;
801 case 0x0c: /* MTPR */
802 s
->mtpr
= value
& 0xff;
804 case 0x10: /* MIMR */
807 case 0x1c: /* MICR */
813 "stellaris_i2c_write: Loopback not implemented\n");
816 "stellaris_i2c_write: Slave mode not implemented\n");
817 s
->mcr
= value
& 0x31;
820 hw_error("stellaris_i2c_write: Bad offset 0x%x\n",
823 stellaris_i2c_update(s
);
826 static void stellaris_i2c_reset(stellaris_i2c_state
*s
)
828 if (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
)
829 i2c_end_transfer(s
->bus
);
838 stellaris_i2c_update(s
);
841 static CPUReadMemoryFunc
* const stellaris_i2c_readfn
[] = {
847 static CPUWriteMemoryFunc
* const stellaris_i2c_writefn
[] = {
853 static const VMStateDescription vmstate_stellaris_i2c
= {
854 .name
= "stellaris_i2c",
856 .minimum_version_id
= 1,
857 .minimum_version_id_old
= 1,
858 .fields
= (VMStateField
[]) {
859 VMSTATE_UINT32(msa
, stellaris_i2c_state
),
860 VMSTATE_UINT32(mcs
, stellaris_i2c_state
),
861 VMSTATE_UINT32(mdr
, stellaris_i2c_state
),
862 VMSTATE_UINT32(mtpr
, stellaris_i2c_state
),
863 VMSTATE_UINT32(mimr
, stellaris_i2c_state
),
864 VMSTATE_UINT32(mris
, stellaris_i2c_state
),
865 VMSTATE_UINT32(mcr
, stellaris_i2c_state
),
866 VMSTATE_END_OF_LIST()
870 static int stellaris_i2c_init(SysBusDevice
* dev
)
872 stellaris_i2c_state
*s
= FROM_SYSBUS(stellaris_i2c_state
, dev
);
876 sysbus_init_irq(dev
, &s
->irq
);
877 bus
= i2c_init_bus(&dev
->qdev
, "i2c");
880 iomemtype
= cpu_register_io_memory(stellaris_i2c_readfn
,
881 stellaris_i2c_writefn
, s
,
882 DEVICE_NATIVE_ENDIAN
);
883 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
884 /* ??? For now we only implement the master interface. */
885 stellaris_i2c_reset(s
);
886 vmstate_register(&dev
->qdev
, -1, &vmstate_stellaris_i2c
, s
);
890 /* Analogue to Digital Converter. This is only partially implemented,
891 enough for applications that use a combined ADC and timer tick. */
893 #define STELLARIS_ADC_EM_CONTROLLER 0
894 #define STELLARIS_ADC_EM_COMP 1
895 #define STELLARIS_ADC_EM_EXTERNAL 4
896 #define STELLARIS_ADC_EM_TIMER 5
897 #define STELLARIS_ADC_EM_PWM0 6
898 #define STELLARIS_ADC_EM_PWM1 7
899 #define STELLARIS_ADC_EM_PWM2 8
901 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
902 #define STELLARIS_ADC_FIFO_FULL 0x1000
923 } stellaris_adc_state
;
925 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state
*s
, int n
)
929 tail
= s
->fifo
[n
].state
& 0xf;
930 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_EMPTY
) {
933 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf) | ((tail
+ 1) & 0xf);
934 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_FULL
;
935 if (tail
+ 1 == ((s
->fifo
[n
].state
>> 4) & 0xf))
936 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_EMPTY
;
938 return s
->fifo
[n
].data
[tail
];
941 static void stellaris_adc_fifo_write(stellaris_adc_state
*s
, int n
,
946 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
947 FIFO fir each sequencer. */
948 head
= (s
->fifo
[n
].state
>> 4) & 0xf;
949 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_FULL
) {
953 s
->fifo
[n
].data
[head
] = value
;
954 head
= (head
+ 1) & 0xf;
955 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_EMPTY
;
956 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf0) | (head
<< 4);
957 if ((s
->fifo
[n
].state
& 0xf) == head
)
958 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_FULL
;
961 static void stellaris_adc_update(stellaris_adc_state
*s
)
966 for (n
= 0; n
< 4; n
++) {
967 level
= (s
->ris
& s
->im
& (1 << n
)) != 0;
968 qemu_set_irq(s
->irq
[n
], level
);
972 static void stellaris_adc_trigger(void *opaque
, int irq
, int level
)
974 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
977 for (n
= 0; n
< 4; n
++) {
978 if ((s
->actss
& (1 << n
)) == 0) {
982 if (((s
->emux
>> (n
* 4)) & 0xff) != 5) {
986 /* Some applications use the ADC as a random number source, so introduce
987 some variation into the signal. */
988 s
->noise
= s
->noise
* 314159 + 1;
989 /* ??? actual inputs not implemented. Return an arbitrary value. */
990 stellaris_adc_fifo_write(s
, n
, 0x200 + ((s
->noise
>> 16) & 7));
992 stellaris_adc_update(s
);
996 static void stellaris_adc_reset(stellaris_adc_state
*s
)
1000 for (n
= 0; n
< 4; n
++) {
1003 s
->fifo
[n
].state
= STELLARIS_ADC_FIFO_EMPTY
;
1007 static uint32_t stellaris_adc_read(void *opaque
, target_phys_addr_t offset
)
1009 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1011 /* TODO: Implement this. */
1012 if (offset
>= 0x40 && offset
< 0xc0) {
1014 n
= (offset
- 0x40) >> 5;
1015 switch (offset
& 0x1f) {
1016 case 0x00: /* SSMUX */
1018 case 0x04: /* SSCTL */
1020 case 0x08: /* SSFIFO */
1021 return stellaris_adc_fifo_read(s
, n
);
1022 case 0x0c: /* SSFSTAT */
1023 return s
->fifo
[n
].state
;
1029 case 0x00: /* ACTSS */
1031 case 0x04: /* RIS */
1035 case 0x0c: /* ISC */
1036 return s
->ris
& s
->im
;
1037 case 0x10: /* OSTAT */
1039 case 0x14: /* EMUX */
1041 case 0x18: /* USTAT */
1043 case 0x20: /* SSPRI */
1045 case 0x30: /* SAC */
1048 hw_error("strllaris_adc_read: Bad offset 0x%x\n",
1054 static void stellaris_adc_write(void *opaque
, target_phys_addr_t offset
,
1057 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1059 /* TODO: Implement this. */
1060 if (offset
>= 0x40 && offset
< 0xc0) {
1062 n
= (offset
- 0x40) >> 5;
1063 switch (offset
& 0x1f) {
1064 case 0x00: /* SSMUX */
1065 s
->ssmux
[n
] = value
& 0x33333333;
1067 case 0x04: /* SSCTL */
1069 hw_error("ADC: Unimplemented sequence %x\n",
1072 s
->ssctl
[n
] = value
;
1079 case 0x00: /* ACTSS */
1080 s
->actss
= value
& 0xf;
1085 case 0x0c: /* ISC */
1088 case 0x10: /* OSTAT */
1091 case 0x14: /* EMUX */
1094 case 0x18: /* USTAT */
1097 case 0x20: /* SSPRI */
1100 case 0x28: /* PSSI */
1101 hw_error("Not implemented: ADC sample initiate\n");
1103 case 0x30: /* SAC */
1107 hw_error("stellaris_adc_write: Bad offset 0x%x\n", (int)offset
);
1109 stellaris_adc_update(s
);
1112 static CPUReadMemoryFunc
* const stellaris_adc_readfn
[] = {
1118 static CPUWriteMemoryFunc
* const stellaris_adc_writefn
[] = {
1119 stellaris_adc_write
,
1120 stellaris_adc_write
,
1124 static const VMStateDescription vmstate_stellaris_adc
= {
1125 .name
= "stellaris_adc",
1127 .minimum_version_id
= 1,
1128 .minimum_version_id_old
= 1,
1129 .fields
= (VMStateField
[]) {
1130 VMSTATE_UINT32(actss
, stellaris_adc_state
),
1131 VMSTATE_UINT32(ris
, stellaris_adc_state
),
1132 VMSTATE_UINT32(im
, stellaris_adc_state
),
1133 VMSTATE_UINT32(emux
, stellaris_adc_state
),
1134 VMSTATE_UINT32(ostat
, stellaris_adc_state
),
1135 VMSTATE_UINT32(ustat
, stellaris_adc_state
),
1136 VMSTATE_UINT32(sspri
, stellaris_adc_state
),
1137 VMSTATE_UINT32(sac
, stellaris_adc_state
),
1138 VMSTATE_UINT32(fifo
[0].state
, stellaris_adc_state
),
1139 VMSTATE_UINT32_ARRAY(fifo
[0].data
, stellaris_adc_state
, 16),
1140 VMSTATE_UINT32(ssmux
[0], stellaris_adc_state
),
1141 VMSTATE_UINT32(ssctl
[0], stellaris_adc_state
),
1142 VMSTATE_UINT32(fifo
[1].state
, stellaris_adc_state
),
1143 VMSTATE_UINT32_ARRAY(fifo
[1].data
, stellaris_adc_state
, 16),
1144 VMSTATE_UINT32(ssmux
[1], stellaris_adc_state
),
1145 VMSTATE_UINT32(ssctl
[1], stellaris_adc_state
),
1146 VMSTATE_UINT32(fifo
[2].state
, stellaris_adc_state
),
1147 VMSTATE_UINT32_ARRAY(fifo
[2].data
, stellaris_adc_state
, 16),
1148 VMSTATE_UINT32(ssmux
[2], stellaris_adc_state
),
1149 VMSTATE_UINT32(ssctl
[2], stellaris_adc_state
),
1150 VMSTATE_UINT32(fifo
[3].state
, stellaris_adc_state
),
1151 VMSTATE_UINT32_ARRAY(fifo
[3].data
, stellaris_adc_state
, 16),
1152 VMSTATE_UINT32(ssmux
[3], stellaris_adc_state
),
1153 VMSTATE_UINT32(ssctl
[3], stellaris_adc_state
),
1154 VMSTATE_UINT32(noise
, stellaris_adc_state
),
1155 VMSTATE_END_OF_LIST()
1159 static int stellaris_adc_init(SysBusDevice
*dev
)
1161 stellaris_adc_state
*s
= FROM_SYSBUS(stellaris_adc_state
, dev
);
1165 for (n
= 0; n
< 4; n
++) {
1166 sysbus_init_irq(dev
, &s
->irq
[n
]);
1169 iomemtype
= cpu_register_io_memory(stellaris_adc_readfn
,
1170 stellaris_adc_writefn
, s
,
1171 DEVICE_NATIVE_ENDIAN
);
1172 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
1173 stellaris_adc_reset(s
);
1174 qdev_init_gpio_in(&dev
->qdev
, stellaris_adc_trigger
, 1);
1175 vmstate_register(&dev
->qdev
, -1, &vmstate_stellaris_adc
, s
);
1179 /* Some boards have both an OLED controller and SD card connected to
1180 the same SSI port, with the SD card chip select connected to a
1181 GPIO pin. Technically the OLED chip select is connected to the SSI
1182 Fss pin. We do not bother emulating that as both devices should
1183 never be selected simultaneously, and our OLED controller ignores stray
1184 0xff commands that occur when deselecting the SD card. */
1191 } stellaris_ssi_bus_state
;
1193 static void stellaris_ssi_bus_select(void *opaque
, int irq
, int level
)
1195 stellaris_ssi_bus_state
*s
= (stellaris_ssi_bus_state
*)opaque
;
1197 s
->current_dev
= level
;
1200 static uint32_t stellaris_ssi_bus_transfer(SSISlave
*dev
, uint32_t val
)
1202 stellaris_ssi_bus_state
*s
= FROM_SSI_SLAVE(stellaris_ssi_bus_state
, dev
);
1204 return ssi_transfer(s
->bus
[s
->current_dev
], val
);
1207 static const VMStateDescription vmstate_stellaris_ssi_bus
= {
1208 .name
= "stellaris_ssi_bus",
1210 .minimum_version_id
= 1,
1211 .minimum_version_id_old
= 1,
1212 .fields
= (VMStateField
[]) {
1213 VMSTATE_INT32(current_dev
, stellaris_ssi_bus_state
),
1214 VMSTATE_END_OF_LIST()
1218 static int stellaris_ssi_bus_init(SSISlave
*dev
)
1220 stellaris_ssi_bus_state
*s
= FROM_SSI_SLAVE(stellaris_ssi_bus_state
, dev
);
1222 s
->bus
[0] = ssi_create_bus(&dev
->qdev
, "ssi0");
1223 s
->bus
[1] = ssi_create_bus(&dev
->qdev
, "ssi1");
1224 qdev_init_gpio_in(&dev
->qdev
, stellaris_ssi_bus_select
, 1);
1226 vmstate_register(&dev
->qdev
, -1, &vmstate_stellaris_ssi_bus
, s
);
1231 static stellaris_board_info stellaris_boards
[] = {
1235 0x001f001f, /* dc0 */
1245 0x00ff007f, /* dc0 */
1250 BP_OLED_SSI
| BP_GAMEPAD
1254 static void stellaris_init(const char *kernel_filename
, const char *cpu_model
,
1255 stellaris_board_info
*board
)
1257 static const int uart_irq
[] = {5, 6, 33, 34};
1258 static const int timer_irq
[] = {19, 21, 23, 35};
1259 static const uint32_t gpio_addr
[7] =
1260 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1261 0x40024000, 0x40025000, 0x40026000};
1262 static const int gpio_irq
[7] = {0, 1, 2, 3, 4, 30, 31};
1264 MemoryRegion
*address_space_mem
= get_system_memory();
1266 DeviceState
*gpio_dev
[7];
1267 qemu_irq gpio_in
[7][8];
1268 qemu_irq gpio_out
[7][8];
1277 flash_size
= ((board
->dc0
& 0xffff) + 1) << 1;
1278 sram_size
= (board
->dc0
>> 18) + 1;
1279 pic
= armv7m_init(address_space_mem
,
1280 flash_size
, sram_size
, kernel_filename
, cpu_model
);
1282 if (board
->dc1
& (1 << 16)) {
1283 dev
= sysbus_create_varargs("stellaris-adc", 0x40038000,
1284 pic
[14], pic
[15], pic
[16], pic
[17], NULL
);
1285 adc
= qdev_get_gpio_in(dev
, 0);
1289 for (i
= 0; i
< 4; i
++) {
1290 if (board
->dc2
& (0x10000 << i
)) {
1291 dev
= sysbus_create_simple("stellaris-gptm",
1292 0x40030000 + i
* 0x1000,
1294 /* TODO: This is incorrect, but we get away with it because
1295 the ADC output is only ever pulsed. */
1296 qdev_connect_gpio_out(dev
, 0, adc
);
1300 stellaris_sys_init(0x400fe000, pic
[28], board
, nd_table
[0].macaddr
.a
);
1302 for (i
= 0; i
< 7; i
++) {
1303 if (board
->dc4
& (1 << i
)) {
1304 gpio_dev
[i
] = sysbus_create_simple("pl061_luminary", gpio_addr
[i
],
1306 for (j
= 0; j
< 8; j
++) {
1307 gpio_in
[i
][j
] = qdev_get_gpio_in(gpio_dev
[i
], j
);
1308 gpio_out
[i
][j
] = NULL
;
1313 if (board
->dc2
& (1 << 12)) {
1314 dev
= sysbus_create_simple("stellaris-i2c", 0x40020000, pic
[8]);
1315 i2c
= (i2c_bus
*)qdev_get_child_bus(dev
, "i2c");
1316 if (board
->peripherals
& BP_OLED_I2C
) {
1317 i2c_create_slave(i2c
, "ssd0303", 0x3d);
1321 for (i
= 0; i
< 4; i
++) {
1322 if (board
->dc2
& (1 << i
)) {
1323 sysbus_create_simple("pl011_luminary", 0x4000c000 + i
* 0x1000,
1327 if (board
->dc2
& (1 << 4)) {
1328 dev
= sysbus_create_simple("pl022", 0x40008000, pic
[7]);
1329 if (board
->peripherals
& BP_OLED_SSI
) {
1333 bus
= qdev_get_child_bus(dev
, "ssi");
1334 mux
= ssi_create_slave(bus
, "evb6965-ssi");
1335 gpio_out
[GPIO_D
][0] = qdev_get_gpio_in(mux
, 0);
1337 bus
= qdev_get_child_bus(mux
, "ssi0");
1338 ssi_create_slave(bus
, "ssi-sd");
1340 bus
= qdev_get_child_bus(mux
, "ssi1");
1341 dev
= ssi_create_slave(bus
, "ssd0323");
1342 gpio_out
[GPIO_C
][7] = qdev_get_gpio_in(dev
, 0);
1344 /* Make sure the select pin is high. */
1345 qemu_irq_raise(gpio_out
[GPIO_D
][0]);
1348 if (board
->dc4
& (1 << 28)) {
1351 qemu_check_nic_model(&nd_table
[0], "stellaris");
1353 enet
= qdev_create(NULL
, "stellaris_enet");
1354 qdev_set_nic_properties(enet
, &nd_table
[0]);
1355 qdev_init_nofail(enet
);
1356 sysbus_mmio_map(sysbus_from_qdev(enet
), 0, 0x40048000);
1357 sysbus_connect_irq(sysbus_from_qdev(enet
), 0, pic
[42]);
1359 if (board
->peripherals
& BP_GAMEPAD
) {
1360 qemu_irq gpad_irq
[5];
1361 static const int gpad_keycode
[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1363 gpad_irq
[0] = qemu_irq_invert(gpio_in
[GPIO_E
][0]); /* up */
1364 gpad_irq
[1] = qemu_irq_invert(gpio_in
[GPIO_E
][1]); /* down */
1365 gpad_irq
[2] = qemu_irq_invert(gpio_in
[GPIO_E
][2]); /* left */
1366 gpad_irq
[3] = qemu_irq_invert(gpio_in
[GPIO_E
][3]); /* right */
1367 gpad_irq
[4] = qemu_irq_invert(gpio_in
[GPIO_F
][1]); /* select */
1369 stellaris_gamepad_init(5, gpad_irq
, gpad_keycode
);
1371 for (i
= 0; i
< 7; i
++) {
1372 if (board
->dc4
& (1 << i
)) {
1373 for (j
= 0; j
< 8; j
++) {
1374 if (gpio_out
[i
][j
]) {
1375 qdev_connect_gpio_out(gpio_dev
[i
], j
, gpio_out
[i
][j
]);
1382 /* FIXME: Figure out how to generate these from stellaris_boards. */
1383 static void lm3s811evb_init(ram_addr_t ram_size
,
1384 const char *boot_device
,
1385 const char *kernel_filename
, const char *kernel_cmdline
,
1386 const char *initrd_filename
, const char *cpu_model
)
1388 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[0]);
1391 static void lm3s6965evb_init(ram_addr_t ram_size
,
1392 const char *boot_device
,
1393 const char *kernel_filename
, const char *kernel_cmdline
,
1394 const char *initrd_filename
, const char *cpu_model
)
1396 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[1]);
1399 static QEMUMachine lm3s811evb_machine
= {
1400 .name
= "lm3s811evb",
1401 .desc
= "Stellaris LM3S811EVB",
1402 .init
= lm3s811evb_init
,
1405 static QEMUMachine lm3s6965evb_machine
= {
1406 .name
= "lm3s6965evb",
1407 .desc
= "Stellaris LM3S6965EVB",
1408 .init
= lm3s6965evb_init
,
1411 static void stellaris_machine_init(void)
1413 qemu_register_machine(&lm3s811evb_machine
);
1414 qemu_register_machine(&lm3s6965evb_machine
);
1417 machine_init(stellaris_machine_init
);
1419 static SSISlaveInfo stellaris_ssi_bus_info
= {
1420 .qdev
.name
= "evb6965-ssi",
1421 .qdev
.size
= sizeof(stellaris_ssi_bus_state
),
1422 .init
= stellaris_ssi_bus_init
,
1423 .transfer
= stellaris_ssi_bus_transfer
1426 static void stellaris_register_devices(void)
1428 sysbus_register_dev("stellaris-i2c", sizeof(stellaris_i2c_state
),
1429 stellaris_i2c_init
);
1430 sysbus_register_dev("stellaris-gptm", sizeof(gptm_state
),
1431 stellaris_gptm_init
);
1432 sysbus_register_dev("stellaris-adc", sizeof(stellaris_adc_state
),
1433 stellaris_adc_init
);
1434 ssi_register_slave(&stellaris_ssi_bus_info
);
1437 device_init(stellaris_register_devices
)