2 * Luminary Micro Stellaris peripherals
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
14 #include "qemu-timer.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(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 void gptm_save(QEMUFile
*f
, void *opaque
)
285 gptm_state
*s
= (gptm_state
*)opaque
;
287 qemu_put_be32(f
, s
->config
);
288 qemu_put_be32(f
, s
->mode
[0]);
289 qemu_put_be32(f
, s
->mode
[1]);
290 qemu_put_be32(f
, s
->control
);
291 qemu_put_be32(f
, s
->state
);
292 qemu_put_be32(f
, s
->mask
);
293 qemu_put_be32(f
, s
->mode
[0]);
294 qemu_put_be32(f
, s
->mode
[0]);
295 qemu_put_be32(f
, s
->load
[0]);
296 qemu_put_be32(f
, s
->load
[1]);
297 qemu_put_be32(f
, s
->match
[0]);
298 qemu_put_be32(f
, s
->match
[1]);
299 qemu_put_be32(f
, s
->prescale
[0]);
300 qemu_put_be32(f
, s
->prescale
[1]);
301 qemu_put_be32(f
, s
->match_prescale
[0]);
302 qemu_put_be32(f
, s
->match_prescale
[1]);
303 qemu_put_be32(f
, s
->rtc
);
304 qemu_put_be64(f
, s
->tick
[0]);
305 qemu_put_be64(f
, s
->tick
[1]);
306 qemu_put_timer(f
, s
->timer
[0]);
307 qemu_put_timer(f
, s
->timer
[1]);
310 static int gptm_load(QEMUFile
*f
, void *opaque
, int version_id
)
312 gptm_state
*s
= (gptm_state
*)opaque
;
317 s
->config
= qemu_get_be32(f
);
318 s
->mode
[0] = qemu_get_be32(f
);
319 s
->mode
[1] = qemu_get_be32(f
);
320 s
->control
= qemu_get_be32(f
);
321 s
->state
= qemu_get_be32(f
);
322 s
->mask
= qemu_get_be32(f
);
323 s
->mode
[0] = qemu_get_be32(f
);
324 s
->mode
[0] = qemu_get_be32(f
);
325 s
->load
[0] = qemu_get_be32(f
);
326 s
->load
[1] = qemu_get_be32(f
);
327 s
->match
[0] = qemu_get_be32(f
);
328 s
->match
[1] = qemu_get_be32(f
);
329 s
->prescale
[0] = qemu_get_be32(f
);
330 s
->prescale
[1] = qemu_get_be32(f
);
331 s
->match_prescale
[0] = qemu_get_be32(f
);
332 s
->match_prescale
[1] = qemu_get_be32(f
);
333 s
->rtc
= qemu_get_be32(f
);
334 s
->tick
[0] = qemu_get_be64(f
);
335 s
->tick
[1] = qemu_get_be64(f
);
336 qemu_get_timer(f
, s
->timer
[0]);
337 qemu_get_timer(f
, s
->timer
[1]);
342 static int stellaris_gptm_init(SysBusDevice
*dev
)
345 gptm_state
*s
= FROM_SYSBUS(gptm_state
, dev
);
347 sysbus_init_irq(dev
, &s
->irq
);
348 qdev_init_gpio_out(&dev
->qdev
, &s
->trigger
, 1);
350 iomemtype
= cpu_register_io_memory(gptm_readfn
,
352 DEVICE_NATIVE_ENDIAN
);
353 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
355 s
->opaque
[0] = s
->opaque
[1] = s
;
356 s
->timer
[0] = qemu_new_timer(vm_clock
, gptm_tick
, &s
->opaque
[0]);
357 s
->timer
[1] = qemu_new_timer(vm_clock
, gptm_tick
, &s
->opaque
[1]);
358 register_savevm(&dev
->qdev
, "stellaris_gptm", -1, 1,
359 gptm_save
, gptm_load
, s
);
364 /* System controller. */
381 stellaris_board_info
*board
;
384 static void ssys_update(ssys_state
*s
)
386 qemu_set_irq(s
->irq
, (s
->int_status
& s
->int_mask
) != 0);
389 static uint32_t pllcfg_sandstorm
[16] = {
391 0x1ae0, /* 1.8432 Mhz */
393 0xd573, /* 2.4576 Mhz */
394 0x37a6, /* 3.57954 Mhz */
395 0x1ae2, /* 3.6864 Mhz */
397 0x98bc, /* 4.906 Mhz */
398 0x935b, /* 4.9152 Mhz */
400 0x4dee, /* 5.12 Mhz */
402 0x75db, /* 6.144 Mhz */
403 0x1ae6, /* 7.3728 Mhz */
405 0x585b /* 8.192 Mhz */
408 static uint32_t pllcfg_fury
[16] = {
410 0x1b20, /* 1.8432 Mhz */
412 0xf42b, /* 2.4576 Mhz */
413 0x37e3, /* 3.57954 Mhz */
414 0x1b21, /* 3.6864 Mhz */
416 0x98ee, /* 4.906 Mhz */
417 0xd5b4, /* 4.9152 Mhz */
419 0x4e27, /* 5.12 Mhz */
421 0xec1c, /* 6.144 Mhz */
422 0x1b23, /* 7.3728 Mhz */
424 0xb11c /* 8.192 Mhz */
427 static uint32_t ssys_read(void *opaque
, target_phys_addr_t offset
)
429 ssys_state
*s
= (ssys_state
*)opaque
;
432 case 0x000: /* DID0 */
433 return s
->board
->did0
;
434 case 0x004: /* DID1 */
435 return s
->board
->did1
;
436 case 0x008: /* DC0 */
437 return s
->board
->dc0
;
438 case 0x010: /* DC1 */
439 return s
->board
->dc1
;
440 case 0x014: /* DC2 */
441 return s
->board
->dc2
;
442 case 0x018: /* DC3 */
443 return s
->board
->dc3
;
444 case 0x01c: /* DC4 */
445 return s
->board
->dc4
;
446 case 0x030: /* PBORCTL */
448 case 0x034: /* LDOPCTL */
450 case 0x040: /* SRCR0 */
452 case 0x044: /* SRCR1 */
454 case 0x048: /* SRCR2 */
456 case 0x050: /* RIS */
457 return s
->int_status
;
458 case 0x054: /* IMC */
460 case 0x058: /* MISC */
461 return s
->int_status
& s
->int_mask
;
462 case 0x05c: /* RESC */
464 case 0x060: /* RCC */
466 case 0x064: /* PLLCFG */
469 xtal
= (s
->rcc
>> 6) & 0xf;
470 if (s
->board
->did0
& (1 << 16)) {
471 return pllcfg_fury
[xtal
];
473 return pllcfg_sandstorm
[xtal
];
476 case 0x100: /* RCGC0 */
478 case 0x104: /* RCGC1 */
480 case 0x108: /* RCGC2 */
482 case 0x110: /* SCGC0 */
484 case 0x114: /* SCGC1 */
486 case 0x118: /* SCGC2 */
488 case 0x120: /* DCGC0 */
490 case 0x124: /* DCGC1 */
492 case 0x128: /* DCGC2 */
494 case 0x150: /* CLKVCLR */
496 case 0x160: /* LDOARST */
498 case 0x1e0: /* USER0 */
500 case 0x1e4: /* USER1 */
503 hw_error("ssys_read: Bad offset 0x%x\n", (int)offset
);
508 static void ssys_calculate_system_clock(ssys_state
*s
)
510 system_clock_scale
= 5 * (((s
->rcc
>> 23) & 0xf) + 1);
513 static void ssys_write(void *opaque
, target_phys_addr_t offset
, uint32_t value
)
515 ssys_state
*s
= (ssys_state
*)opaque
;
518 case 0x030: /* PBORCTL */
519 s
->pborctl
= value
& 0xffff;
521 case 0x034: /* LDOPCTL */
522 s
->ldopctl
= value
& 0x1f;
524 case 0x040: /* SRCR0 */
525 case 0x044: /* SRCR1 */
526 case 0x048: /* SRCR2 */
527 fprintf(stderr
, "Peripheral reset not implemented\n");
529 case 0x054: /* IMC */
530 s
->int_mask
= value
& 0x7f;
532 case 0x058: /* MISC */
533 s
->int_status
&= ~value
;
535 case 0x05c: /* RESC */
536 s
->resc
= value
& 0x3f;
538 case 0x060: /* RCC */
539 if ((s
->rcc
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
541 s
->int_status
|= (1 << 6);
544 ssys_calculate_system_clock(s
);
546 case 0x100: /* RCGC0 */
549 case 0x104: /* RCGC1 */
552 case 0x108: /* RCGC2 */
555 case 0x110: /* SCGC0 */
558 case 0x114: /* SCGC1 */
561 case 0x118: /* SCGC2 */
564 case 0x120: /* DCGC0 */
567 case 0x124: /* DCGC1 */
570 case 0x128: /* DCGC2 */
573 case 0x150: /* CLKVCLR */
576 case 0x160: /* LDOARST */
580 hw_error("ssys_write: Bad offset 0x%x\n", (int)offset
);
585 static CPUReadMemoryFunc
* const ssys_readfn
[] = {
591 static CPUWriteMemoryFunc
* const ssys_writefn
[] = {
597 static void ssys_reset(void *opaque
)
599 ssys_state
*s
= (ssys_state
*)opaque
;
608 static void ssys_save(QEMUFile
*f
, void *opaque
)
610 ssys_state
*s
= (ssys_state
*)opaque
;
612 qemu_put_be32(f
, s
->pborctl
);
613 qemu_put_be32(f
, s
->ldopctl
);
614 qemu_put_be32(f
, s
->int_mask
);
615 qemu_put_be32(f
, s
->int_status
);
616 qemu_put_be32(f
, s
->resc
);
617 qemu_put_be32(f
, s
->rcc
);
618 qemu_put_be32(f
, s
->rcgc
[0]);
619 qemu_put_be32(f
, s
->rcgc
[1]);
620 qemu_put_be32(f
, s
->rcgc
[2]);
621 qemu_put_be32(f
, s
->scgc
[0]);
622 qemu_put_be32(f
, s
->scgc
[1]);
623 qemu_put_be32(f
, s
->scgc
[2]);
624 qemu_put_be32(f
, s
->dcgc
[0]);
625 qemu_put_be32(f
, s
->dcgc
[1]);
626 qemu_put_be32(f
, s
->dcgc
[2]);
627 qemu_put_be32(f
, s
->clkvclr
);
628 qemu_put_be32(f
, s
->ldoarst
);
631 static int ssys_load(QEMUFile
*f
, void *opaque
, int version_id
)
633 ssys_state
*s
= (ssys_state
*)opaque
;
638 s
->pborctl
= qemu_get_be32(f
);
639 s
->ldopctl
= qemu_get_be32(f
);
640 s
->int_mask
= qemu_get_be32(f
);
641 s
->int_status
= qemu_get_be32(f
);
642 s
->resc
= qemu_get_be32(f
);
643 s
->rcc
= qemu_get_be32(f
);
644 s
->rcgc
[0] = qemu_get_be32(f
);
645 s
->rcgc
[1] = qemu_get_be32(f
);
646 s
->rcgc
[2] = qemu_get_be32(f
);
647 s
->scgc
[0] = qemu_get_be32(f
);
648 s
->scgc
[1] = qemu_get_be32(f
);
649 s
->scgc
[2] = qemu_get_be32(f
);
650 s
->dcgc
[0] = qemu_get_be32(f
);
651 s
->dcgc
[1] = qemu_get_be32(f
);
652 s
->dcgc
[2] = qemu_get_be32(f
);
653 s
->clkvclr
= qemu_get_be32(f
);
654 s
->ldoarst
= qemu_get_be32(f
);
655 ssys_calculate_system_clock(s
);
660 static int stellaris_sys_init(uint32_t base
, qemu_irq irq
,
661 stellaris_board_info
* board
,
667 s
= (ssys_state
*)qemu_mallocz(sizeof(ssys_state
));
670 /* Most devices come preprogrammed with a MAC address in the user data. */
671 s
->user0
= macaddr
[0] | (macaddr
[1] << 8) | (macaddr
[2] << 16);
672 s
->user1
= macaddr
[3] | (macaddr
[4] << 8) | (macaddr
[5] << 16);
674 iomemtype
= cpu_register_io_memory(ssys_readfn
,
676 DEVICE_NATIVE_ENDIAN
);
677 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
679 register_savevm(NULL
, "stellaris_sys", -1, 1, ssys_save
, ssys_load
, s
);
684 /* I2C controller. */
697 } stellaris_i2c_state
;
699 #define STELLARIS_I2C_MCS_BUSY 0x01
700 #define STELLARIS_I2C_MCS_ERROR 0x02
701 #define STELLARIS_I2C_MCS_ADRACK 0x04
702 #define STELLARIS_I2C_MCS_DATACK 0x08
703 #define STELLARIS_I2C_MCS_ARBLST 0x10
704 #define STELLARIS_I2C_MCS_IDLE 0x20
705 #define STELLARIS_I2C_MCS_BUSBSY 0x40
707 static uint32_t stellaris_i2c_read(void *opaque
, target_phys_addr_t offset
)
709 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
715 /* We don't emulate timing, so the controller is never busy. */
716 return s
->mcs
| STELLARIS_I2C_MCS_IDLE
;
719 case 0x0c: /* MTPR */
721 case 0x10: /* MIMR */
723 case 0x14: /* MRIS */
725 case 0x18: /* MMIS */
726 return s
->mris
& s
->mimr
;
730 hw_error("strllaris_i2c_read: Bad offset 0x%x\n", (int)offset
);
735 static void stellaris_i2c_update(stellaris_i2c_state
*s
)
739 level
= (s
->mris
& s
->mimr
) != 0;
740 qemu_set_irq(s
->irq
, level
);
743 static void stellaris_i2c_write(void *opaque
, target_phys_addr_t offset
,
746 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
750 s
->msa
= value
& 0xff;
753 if ((s
->mcr
& 0x10) == 0) {
754 /* Disabled. Do nothing. */
757 /* Grab the bus if this is starting a transfer. */
758 if ((value
& 2) && (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
759 if (i2c_start_transfer(s
->bus
, s
->msa
>> 1, s
->msa
& 1)) {
760 s
->mcs
|= STELLARIS_I2C_MCS_ARBLST
;
762 s
->mcs
&= ~STELLARIS_I2C_MCS_ARBLST
;
763 s
->mcs
|= STELLARIS_I2C_MCS_BUSBSY
;
766 /* If we don't have the bus then indicate an error. */
767 if (!i2c_bus_busy(s
->bus
)
768 || (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
769 s
->mcs
|= STELLARIS_I2C_MCS_ERROR
;
772 s
->mcs
&= ~STELLARIS_I2C_MCS_ERROR
;
774 /* Transfer a byte. */
775 /* TODO: Handle errors. */
778 s
->mdr
= i2c_recv(s
->bus
) & 0xff;
781 i2c_send(s
->bus
, s
->mdr
);
783 /* Raise an interrupt. */
787 /* Finish transfer. */
788 i2c_end_transfer(s
->bus
);
789 s
->mcs
&= ~STELLARIS_I2C_MCS_BUSBSY
;
793 s
->mdr
= value
& 0xff;
795 case 0x0c: /* MTPR */
796 s
->mtpr
= value
& 0xff;
798 case 0x10: /* MIMR */
801 case 0x1c: /* MICR */
807 "stellaris_i2c_write: Loopback not implemented\n");
810 "stellaris_i2c_write: Slave mode not implemented\n");
811 s
->mcr
= value
& 0x31;
814 hw_error("stellaris_i2c_write: Bad offset 0x%x\n",
817 stellaris_i2c_update(s
);
820 static void stellaris_i2c_reset(stellaris_i2c_state
*s
)
822 if (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
)
823 i2c_end_transfer(s
->bus
);
832 stellaris_i2c_update(s
);
835 static CPUReadMemoryFunc
* const stellaris_i2c_readfn
[] = {
841 static CPUWriteMemoryFunc
* const stellaris_i2c_writefn
[] = {
847 static void stellaris_i2c_save(QEMUFile
*f
, void *opaque
)
849 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
851 qemu_put_be32(f
, s
->msa
);
852 qemu_put_be32(f
, s
->mcs
);
853 qemu_put_be32(f
, s
->mdr
);
854 qemu_put_be32(f
, s
->mtpr
);
855 qemu_put_be32(f
, s
->mimr
);
856 qemu_put_be32(f
, s
->mris
);
857 qemu_put_be32(f
, s
->mcr
);
860 static int stellaris_i2c_load(QEMUFile
*f
, void *opaque
, int version_id
)
862 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
867 s
->msa
= qemu_get_be32(f
);
868 s
->mcs
= qemu_get_be32(f
);
869 s
->mdr
= qemu_get_be32(f
);
870 s
->mtpr
= qemu_get_be32(f
);
871 s
->mimr
= qemu_get_be32(f
);
872 s
->mris
= qemu_get_be32(f
);
873 s
->mcr
= qemu_get_be32(f
);
878 static int stellaris_i2c_init(SysBusDevice
* dev
)
880 stellaris_i2c_state
*s
= FROM_SYSBUS(stellaris_i2c_state
, dev
);
884 sysbus_init_irq(dev
, &s
->irq
);
885 bus
= i2c_init_bus(&dev
->qdev
, "i2c");
888 iomemtype
= cpu_register_io_memory(stellaris_i2c_readfn
,
889 stellaris_i2c_writefn
, s
,
890 DEVICE_NATIVE_ENDIAN
);
891 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
892 /* ??? For now we only implement the master interface. */
893 stellaris_i2c_reset(s
);
894 register_savevm(&dev
->qdev
, "stellaris_i2c", -1, 1,
895 stellaris_i2c_save
, stellaris_i2c_load
, s
);
899 /* Analogue to Digital Converter. This is only partially implemented,
900 enough for applications that use a combined ADC and timer tick. */
902 #define STELLARIS_ADC_EM_CONTROLLER 0
903 #define STELLARIS_ADC_EM_COMP 1
904 #define STELLARIS_ADC_EM_EXTERNAL 4
905 #define STELLARIS_ADC_EM_TIMER 5
906 #define STELLARIS_ADC_EM_PWM0 6
907 #define STELLARIS_ADC_EM_PWM1 7
908 #define STELLARIS_ADC_EM_PWM2 8
910 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
911 #define STELLARIS_ADC_FIFO_FULL 0x1000
932 } stellaris_adc_state
;
934 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state
*s
, int n
)
938 tail
= s
->fifo
[n
].state
& 0xf;
939 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_EMPTY
) {
942 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf) | ((tail
+ 1) & 0xf);
943 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_FULL
;
944 if (tail
+ 1 == ((s
->fifo
[n
].state
>> 4) & 0xf))
945 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_EMPTY
;
947 return s
->fifo
[n
].data
[tail
];
950 static void stellaris_adc_fifo_write(stellaris_adc_state
*s
, int n
,
955 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
956 FIFO fir each sequencer. */
957 head
= (s
->fifo
[n
].state
>> 4) & 0xf;
958 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_FULL
) {
962 s
->fifo
[n
].data
[head
] = value
;
963 head
= (head
+ 1) & 0xf;
964 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_EMPTY
;
965 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf0) | (head
<< 4);
966 if ((s
->fifo
[n
].state
& 0xf) == head
)
967 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_FULL
;
970 static void stellaris_adc_update(stellaris_adc_state
*s
)
975 for (n
= 0; n
< 4; n
++) {
976 level
= (s
->ris
& s
->im
& (1 << n
)) != 0;
977 qemu_set_irq(s
->irq
[n
], level
);
981 static void stellaris_adc_trigger(void *opaque
, int irq
, int level
)
983 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
986 for (n
= 0; n
< 4; n
++) {
987 if ((s
->actss
& (1 << n
)) == 0) {
991 if (((s
->emux
>> (n
* 4)) & 0xff) != 5) {
995 /* Some applications use the ADC as a random number source, so introduce
996 some variation into the signal. */
997 s
->noise
= s
->noise
* 314159 + 1;
998 /* ??? actual inputs not implemented. Return an arbitrary value. */
999 stellaris_adc_fifo_write(s
, n
, 0x200 + ((s
->noise
>> 16) & 7));
1001 stellaris_adc_update(s
);
1005 static void stellaris_adc_reset(stellaris_adc_state
*s
)
1009 for (n
= 0; n
< 4; n
++) {
1012 s
->fifo
[n
].state
= STELLARIS_ADC_FIFO_EMPTY
;
1016 static uint32_t stellaris_adc_read(void *opaque
, target_phys_addr_t 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
, target_phys_addr_t offset
,
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 %x\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 CPUReadMemoryFunc
* const stellaris_adc_readfn
[] = {
1127 static CPUWriteMemoryFunc
* const stellaris_adc_writefn
[] = {
1128 stellaris_adc_write
,
1129 stellaris_adc_write
,
1133 static void stellaris_adc_save(QEMUFile
*f
, void *opaque
)
1135 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1139 qemu_put_be32(f
, s
->actss
);
1140 qemu_put_be32(f
, s
->ris
);
1141 qemu_put_be32(f
, s
->im
);
1142 qemu_put_be32(f
, s
->emux
);
1143 qemu_put_be32(f
, s
->ostat
);
1144 qemu_put_be32(f
, s
->ustat
);
1145 qemu_put_be32(f
, s
->sspri
);
1146 qemu_put_be32(f
, s
->sac
);
1147 for (i
= 0; i
< 4; i
++) {
1148 qemu_put_be32(f
, s
->fifo
[i
].state
);
1149 for (j
= 0; j
< 16; j
++) {
1150 qemu_put_be32(f
, s
->fifo
[i
].data
[j
]);
1152 qemu_put_be32(f
, s
->ssmux
[i
]);
1153 qemu_put_be32(f
, s
->ssctl
[i
]);
1155 qemu_put_be32(f
, s
->noise
);
1158 static int stellaris_adc_load(QEMUFile
*f
, void *opaque
, int version_id
)
1160 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1164 if (version_id
!= 1)
1167 s
->actss
= qemu_get_be32(f
);
1168 s
->ris
= qemu_get_be32(f
);
1169 s
->im
= qemu_get_be32(f
);
1170 s
->emux
= qemu_get_be32(f
);
1171 s
->ostat
= qemu_get_be32(f
);
1172 s
->ustat
= qemu_get_be32(f
);
1173 s
->sspri
= qemu_get_be32(f
);
1174 s
->sac
= qemu_get_be32(f
);
1175 for (i
= 0; i
< 4; i
++) {
1176 s
->fifo
[i
].state
= qemu_get_be32(f
);
1177 for (j
= 0; j
< 16; j
++) {
1178 s
->fifo
[i
].data
[j
] = qemu_get_be32(f
);
1180 s
->ssmux
[i
] = qemu_get_be32(f
);
1181 s
->ssctl
[i
] = qemu_get_be32(f
);
1183 s
->noise
= qemu_get_be32(f
);
1188 static int stellaris_adc_init(SysBusDevice
*dev
)
1190 stellaris_adc_state
*s
= FROM_SYSBUS(stellaris_adc_state
, dev
);
1194 for (n
= 0; n
< 4; n
++) {
1195 sysbus_init_irq(dev
, &s
->irq
[n
]);
1198 iomemtype
= cpu_register_io_memory(stellaris_adc_readfn
,
1199 stellaris_adc_writefn
, s
,
1200 DEVICE_NATIVE_ENDIAN
);
1201 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
1202 stellaris_adc_reset(s
);
1203 qdev_init_gpio_in(&dev
->qdev
, stellaris_adc_trigger
, 1);
1204 register_savevm(&dev
->qdev
, "stellaris_adc", -1, 1,
1205 stellaris_adc_save
, stellaris_adc_load
, s
);
1209 /* Some boards have both an OLED controller and SD card connected to
1210 the same SSI port, with the SD card chip select connected to a
1211 GPIO pin. Technically the OLED chip select is connected to the SSI
1212 Fss pin. We do not bother emulating that as both devices should
1213 never be selected simultaneously, and our OLED controller ignores stray
1214 0xff commands that occur when deselecting the SD card. */
1221 } stellaris_ssi_bus_state
;
1223 static void stellaris_ssi_bus_select(void *opaque
, int irq
, int level
)
1225 stellaris_ssi_bus_state
*s
= (stellaris_ssi_bus_state
*)opaque
;
1227 s
->current_dev
= level
;
1230 static uint32_t stellaris_ssi_bus_transfer(SSISlave
*dev
, uint32_t val
)
1232 stellaris_ssi_bus_state
*s
= FROM_SSI_SLAVE(stellaris_ssi_bus_state
, dev
);
1234 return ssi_transfer(s
->bus
[s
->current_dev
], val
);
1237 static void stellaris_ssi_bus_save(QEMUFile
*f
, void *opaque
)
1239 stellaris_ssi_bus_state
*s
= (stellaris_ssi_bus_state
*)opaque
;
1241 qemu_put_be32(f
, s
->current_dev
);
1244 static int stellaris_ssi_bus_load(QEMUFile
*f
, void *opaque
, int version_id
)
1246 stellaris_ssi_bus_state
*s
= (stellaris_ssi_bus_state
*)opaque
;
1248 if (version_id
!= 1)
1251 s
->current_dev
= qemu_get_be32(f
);
1256 static int stellaris_ssi_bus_init(SSISlave
*dev
)
1258 stellaris_ssi_bus_state
*s
= FROM_SSI_SLAVE(stellaris_ssi_bus_state
, dev
);
1260 s
->bus
[0] = ssi_create_bus(&dev
->qdev
, "ssi0");
1261 s
->bus
[1] = ssi_create_bus(&dev
->qdev
, "ssi1");
1262 qdev_init_gpio_in(&dev
->qdev
, stellaris_ssi_bus_select
, 1);
1264 register_savevm(&dev
->qdev
, "stellaris_ssi_bus", -1, 1,
1265 stellaris_ssi_bus_save
, stellaris_ssi_bus_load
, s
);
1270 static stellaris_board_info stellaris_boards
[] = {
1274 0x001f001f, /* dc0 */
1284 0x00ff007f, /* dc0 */
1289 BP_OLED_SSI
| BP_GAMEPAD
1293 static void stellaris_init(const char *kernel_filename
, const char *cpu_model
,
1294 stellaris_board_info
*board
)
1296 static const int uart_irq
[] = {5, 6, 33, 34};
1297 static const int timer_irq
[] = {19, 21, 23, 35};
1298 static const uint32_t gpio_addr
[7] =
1299 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1300 0x40024000, 0x40025000, 0x40026000};
1301 static const int gpio_irq
[7] = {0, 1, 2, 3, 4, 30, 31};
1304 DeviceState
*gpio_dev
[7];
1305 qemu_irq gpio_in
[7][8];
1306 qemu_irq gpio_out
[7][8];
1315 flash_size
= ((board
->dc0
& 0xffff) + 1) << 1;
1316 sram_size
= (board
->dc0
>> 18) + 1;
1317 pic
= armv7m_init(flash_size
, sram_size
, kernel_filename
, cpu_model
);
1319 if (board
->dc1
& (1 << 16)) {
1320 dev
= sysbus_create_varargs("stellaris-adc", 0x40038000,
1321 pic
[14], pic
[15], pic
[16], pic
[17], NULL
);
1322 adc
= qdev_get_gpio_in(dev
, 0);
1326 for (i
= 0; i
< 4; i
++) {
1327 if (board
->dc2
& (0x10000 << i
)) {
1328 dev
= sysbus_create_simple("stellaris-gptm",
1329 0x40030000 + i
* 0x1000,
1331 /* TODO: This is incorrect, but we get away with it because
1332 the ADC output is only ever pulsed. */
1333 qdev_connect_gpio_out(dev
, 0, adc
);
1337 stellaris_sys_init(0x400fe000, pic
[28], board
, nd_table
[0].macaddr
);
1339 for (i
= 0; i
< 7; i
++) {
1340 if (board
->dc4
& (1 << i
)) {
1341 gpio_dev
[i
] = sysbus_create_simple("pl061", gpio_addr
[i
],
1343 for (j
= 0; j
< 8; j
++) {
1344 gpio_in
[i
][j
] = qdev_get_gpio_in(gpio_dev
[i
], j
);
1345 gpio_out
[i
][j
] = NULL
;
1350 if (board
->dc2
& (1 << 12)) {
1351 dev
= sysbus_create_simple("stellaris-i2c", 0x40020000, pic
[8]);
1352 i2c
= (i2c_bus
*)qdev_get_child_bus(dev
, "i2c");
1353 if (board
->peripherals
& BP_OLED_I2C
) {
1354 i2c_create_slave(i2c
, "ssd0303", 0x3d);
1358 for (i
= 0; i
< 4; i
++) {
1359 if (board
->dc2
& (1 << i
)) {
1360 sysbus_create_simple("pl011_luminary", 0x4000c000 + i
* 0x1000,
1364 if (board
->dc2
& (1 << 4)) {
1365 dev
= sysbus_create_simple("pl022", 0x40008000, pic
[7]);
1366 if (board
->peripherals
& BP_OLED_SSI
) {
1370 bus
= qdev_get_child_bus(dev
, "ssi");
1371 mux
= ssi_create_slave(bus
, "evb6965-ssi");
1372 gpio_out
[GPIO_D
][0] = qdev_get_gpio_in(mux
, 0);
1374 bus
= qdev_get_child_bus(mux
, "ssi0");
1375 ssi_create_slave(bus
, "ssi-sd");
1377 bus
= qdev_get_child_bus(mux
, "ssi1");
1378 dev
= ssi_create_slave(bus
, "ssd0323");
1379 gpio_out
[GPIO_C
][7] = qdev_get_gpio_in(dev
, 0);
1381 /* Make sure the select pin is high. */
1382 qemu_irq_raise(gpio_out
[GPIO_D
][0]);
1385 if (board
->dc4
& (1 << 28)) {
1388 qemu_check_nic_model(&nd_table
[0], "stellaris");
1390 enet
= qdev_create(NULL
, "stellaris_enet");
1391 qdev_set_nic_properties(enet
, &nd_table
[0]);
1392 qdev_init_nofail(enet
);
1393 sysbus_mmio_map(sysbus_from_qdev(enet
), 0, 0x40048000);
1394 sysbus_connect_irq(sysbus_from_qdev(enet
), 0, pic
[42]);
1396 if (board
->peripherals
& BP_GAMEPAD
) {
1397 qemu_irq gpad_irq
[5];
1398 static const int gpad_keycode
[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1400 gpad_irq
[0] = qemu_irq_invert(gpio_in
[GPIO_E
][0]); /* up */
1401 gpad_irq
[1] = qemu_irq_invert(gpio_in
[GPIO_E
][1]); /* down */
1402 gpad_irq
[2] = qemu_irq_invert(gpio_in
[GPIO_E
][2]); /* left */
1403 gpad_irq
[3] = qemu_irq_invert(gpio_in
[GPIO_E
][3]); /* right */
1404 gpad_irq
[4] = qemu_irq_invert(gpio_in
[GPIO_F
][1]); /* select */
1406 stellaris_gamepad_init(5, gpad_irq
, gpad_keycode
);
1408 for (i
= 0; i
< 7; i
++) {
1409 if (board
->dc4
& (1 << i
)) {
1410 for (j
= 0; j
< 8; j
++) {
1411 if (gpio_out
[i
][j
]) {
1412 qdev_connect_gpio_out(gpio_dev
[i
], j
, gpio_out
[i
][j
]);
1419 /* FIXME: Figure out how to generate these from stellaris_boards. */
1420 static void lm3s811evb_init(ram_addr_t ram_size
,
1421 const char *boot_device
,
1422 const char *kernel_filename
, const char *kernel_cmdline
,
1423 const char *initrd_filename
, const char *cpu_model
)
1425 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[0]);
1428 static void lm3s6965evb_init(ram_addr_t ram_size
,
1429 const char *boot_device
,
1430 const char *kernel_filename
, const char *kernel_cmdline
,
1431 const char *initrd_filename
, const char *cpu_model
)
1433 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[1]);
1436 static QEMUMachine lm3s811evb_machine
= {
1437 .name
= "lm3s811evb",
1438 .desc
= "Stellaris LM3S811EVB",
1439 .init
= lm3s811evb_init
,
1442 static QEMUMachine lm3s6965evb_machine
= {
1443 .name
= "lm3s6965evb",
1444 .desc
= "Stellaris LM3S6965EVB",
1445 .init
= lm3s6965evb_init
,
1448 static void stellaris_machine_init(void)
1450 qemu_register_machine(&lm3s811evb_machine
);
1451 qemu_register_machine(&lm3s6965evb_machine
);
1454 machine_init(stellaris_machine_init
);
1456 static SSISlaveInfo stellaris_ssi_bus_info
= {
1457 .qdev
.name
= "evb6965-ssi",
1458 .qdev
.size
= sizeof(stellaris_ssi_bus_state
),
1459 .init
= stellaris_ssi_bus_init
,
1460 .transfer
= stellaris_ssi_bus_transfer
1463 static void stellaris_register_devices(void)
1465 sysbus_register_dev("stellaris-i2c", sizeof(stellaris_i2c_state
),
1466 stellaris_i2c_init
);
1467 sysbus_register_dev("stellaris-gptm", sizeof(gptm_state
),
1468 stellaris_gptm_init
);
1469 sysbus_register_dev("stellaris-adc", sizeof(stellaris_adc_state
),
1470 stellaris_adc_init
);
1471 ssi_register_slave(&stellaris_ssi_bus_info
);
1474 device_init(stellaris_register_devices
)