2 * Luminary Micro Stellaris peripherals
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
12 #include "primecell.h"
14 #include "qemu-timer.h"
29 #define BP_OLED_I2C 0x01
30 #define BP_OLED_SSI 0x02
31 #define BP_GAMEPAD 0x04
33 typedef const struct {
43 } stellaris_board_info
;
45 /* General purpose timer module. */
47 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
+= ticks_per_sec
;
94 } else if (s
->mode
[n
] == 0xa) {
95 /* PWM mode. Not implemented. */
97 cpu_abort(cpu_single_env
, "TODO: 16-bit timer mode 0x%x\n",
101 qemu_mod_timer(s
->timer
[n
], tick
);
104 static void gptm_tick(void *opaque
)
106 gptm_state
**p
= (gptm_state
**)opaque
;
112 if (s
->config
== 0) {
114 if ((s
->control
& 0x20)) {
115 /* Output trigger. */
116 qemu_irq_raise(s
->trigger
);
117 qemu_irq_lower(s
->trigger
);
119 if (s
->mode
[0] & 1) {
124 gptm_reload(s
, 0, 0);
126 } else if (s
->config
== 1) {
130 match
= s
->match
[0] | (s
->match
[1] << 16);
136 gptm_reload(s
, 0, 0);
137 } else if (s
->mode
[n
] == 0xa) {
138 /* PWM mode. Not implemented. */
140 cpu_abort(cpu_single_env
, "TODO: 16-bit timer mode 0x%x\n",
146 static uint32_t gptm_read(void *opaque
, target_phys_addr_t offset
)
148 gptm_state
*s
= (gptm_state
*)opaque
;
153 case 0x04: /* TAMR */
155 case 0x08: /* TBMR */
164 return s
->state
& s
->mask
;
167 case 0x28: /* TAILR */
168 return s
->load
[0] | ((s
->config
< 4) ? (s
->load
[1] << 16) : 0);
169 case 0x2c: /* TBILR */
171 case 0x30: /* TAMARCHR */
172 return s
->match
[0] | ((s
->config
< 4) ? (s
->match
[1] << 16) : 0);
173 case 0x34: /* TBMATCHR */
175 case 0x38: /* TAPR */
176 return s
->prescale
[0];
177 case 0x3c: /* TBPR */
178 return s
->prescale
[1];
179 case 0x40: /* TAPMR */
180 return s
->match_prescale
[0];
181 case 0x44: /* TBPMR */
182 return s
->match_prescale
[1];
187 cpu_abort(cpu_single_env
, "TODO: Timer value read\n");
189 cpu_abort(cpu_single_env
, "gptm_read: Bad offset 0x%x\n", (int)offset
);
194 static void gptm_write(void *opaque
, target_phys_addr_t offset
, uint32_t value
)
196 gptm_state
*s
= (gptm_state
*)opaque
;
199 /* The timers should be disabled before changing the configuration.
200 We take advantage of this and defer everything until the timer
206 case 0x04: /* TAMR */
209 case 0x08: /* TBMR */
215 /* TODO: Implement pause. */
216 if ((oldval
^ value
) & 1) {
218 gptm_reload(s
, 0, 1);
223 if (((oldval
^ value
) & 0x100) && s
->config
>= 4) {
225 gptm_reload(s
, 1, 1);
232 s
->mask
= value
& 0x77;
238 case 0x28: /* TAILR */
239 s
->load
[0] = value
& 0xffff;
241 s
->load
[1] = value
>> 16;
244 case 0x2c: /* TBILR */
245 s
->load
[1] = value
& 0xffff;
247 case 0x30: /* TAMARCHR */
248 s
->match
[0] = value
& 0xffff;
250 s
->match
[1] = value
>> 16;
253 case 0x34: /* TBMATCHR */
254 s
->match
[1] = value
>> 16;
256 case 0x38: /* TAPR */
257 s
->prescale
[0] = value
;
259 case 0x3c: /* TBPR */
260 s
->prescale
[1] = value
;
262 case 0x40: /* TAPMR */
263 s
->match_prescale
[0] = value
;
265 case 0x44: /* TBPMR */
266 s
->match_prescale
[0] = value
;
269 cpu_abort(cpu_single_env
, "gptm_write: Bad offset 0x%x\n", (int)offset
);
274 static CPUReadMemoryFunc
*gptm_readfn
[] = {
280 static CPUWriteMemoryFunc
*gptm_writefn
[] = {
286 static void gptm_save(QEMUFile
*f
, void *opaque
)
288 gptm_state
*s
= (gptm_state
*)opaque
;
290 qemu_put_be32(f
, s
->config
);
291 qemu_put_be32(f
, s
->mode
[0]);
292 qemu_put_be32(f
, s
->mode
[1]);
293 qemu_put_be32(f
, s
->control
);
294 qemu_put_be32(f
, s
->state
);
295 qemu_put_be32(f
, s
->mask
);
296 qemu_put_be32(f
, s
->mode
[0]);
297 qemu_put_be32(f
, s
->mode
[0]);
298 qemu_put_be32(f
, s
->load
[0]);
299 qemu_put_be32(f
, s
->load
[1]);
300 qemu_put_be32(f
, s
->match
[0]);
301 qemu_put_be32(f
, s
->match
[1]);
302 qemu_put_be32(f
, s
->prescale
[0]);
303 qemu_put_be32(f
, s
->prescale
[1]);
304 qemu_put_be32(f
, s
->match_prescale
[0]);
305 qemu_put_be32(f
, s
->match_prescale
[1]);
306 qemu_put_be32(f
, s
->rtc
);
307 qemu_put_be64(f
, s
->tick
[0]);
308 qemu_put_be64(f
, s
->tick
[1]);
309 qemu_put_timer(f
, s
->timer
[0]);
310 qemu_put_timer(f
, s
->timer
[1]);
313 static int gptm_load(QEMUFile
*f
, void *opaque
, int version_id
)
315 gptm_state
*s
= (gptm_state
*)opaque
;
320 s
->config
= qemu_get_be32(f
);
321 s
->mode
[0] = qemu_get_be32(f
);
322 s
->mode
[1] = qemu_get_be32(f
);
323 s
->control
= qemu_get_be32(f
);
324 s
->state
= qemu_get_be32(f
);
325 s
->mask
= qemu_get_be32(f
);
326 s
->mode
[0] = qemu_get_be32(f
);
327 s
->mode
[0] = qemu_get_be32(f
);
328 s
->load
[0] = qemu_get_be32(f
);
329 s
->load
[1] = qemu_get_be32(f
);
330 s
->match
[0] = qemu_get_be32(f
);
331 s
->match
[1] = qemu_get_be32(f
);
332 s
->prescale
[0] = qemu_get_be32(f
);
333 s
->prescale
[1] = qemu_get_be32(f
);
334 s
->match_prescale
[0] = qemu_get_be32(f
);
335 s
->match_prescale
[1] = qemu_get_be32(f
);
336 s
->rtc
= qemu_get_be32(f
);
337 s
->tick
[0] = qemu_get_be64(f
);
338 s
->tick
[1] = qemu_get_be64(f
);
339 qemu_get_timer(f
, s
->timer
[0]);
340 qemu_get_timer(f
, s
->timer
[1]);
345 static void stellaris_gptm_init(uint32_t base
, qemu_irq irq
, qemu_irq trigger
)
350 s
= (gptm_state
*)qemu_mallocz(sizeof(gptm_state
));
352 s
->trigger
= trigger
;
353 s
->opaque
[0] = s
->opaque
[1] = s
;
355 iomemtype
= cpu_register_io_memory(0, gptm_readfn
,
357 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
358 s
->timer
[0] = qemu_new_timer(vm_clock
, gptm_tick
, &s
->opaque
[0]);
359 s
->timer
[1] = qemu_new_timer(vm_clock
, gptm_tick
, &s
->opaque
[1]);
360 register_savevm("stellaris_gptm", -1, 1, 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 cpu_abort(cpu_single_env
, "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 cpu_abort(cpu_single_env
, "ssys_write: Bad offset 0x%x\n", (int)offset
);
585 static CPUReadMemoryFunc
*ssys_readfn
[] = {
591 static CPUWriteMemoryFunc
*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 void 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(0, ssys_readfn
,
676 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
678 register_savevm("stellaris_sys", -1, 1, ssys_save
, ssys_load
, s
);
682 /* I2C controller. */
694 } stellaris_i2c_state
;
696 #define STELLARIS_I2C_MCS_BUSY 0x01
697 #define STELLARIS_I2C_MCS_ERROR 0x02
698 #define STELLARIS_I2C_MCS_ADRACK 0x04
699 #define STELLARIS_I2C_MCS_DATACK 0x08
700 #define STELLARIS_I2C_MCS_ARBLST 0x10
701 #define STELLARIS_I2C_MCS_IDLE 0x20
702 #define STELLARIS_I2C_MCS_BUSBSY 0x40
704 static uint32_t stellaris_i2c_read(void *opaque
, target_phys_addr_t offset
)
706 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
712 /* We don't emulate timing, so the controller is never busy. */
713 return s
->mcs
| STELLARIS_I2C_MCS_IDLE
;
716 case 0x0c: /* MTPR */
718 case 0x10: /* MIMR */
720 case 0x14: /* MRIS */
722 case 0x18: /* MMIS */
723 return s
->mris
& s
->mimr
;
727 cpu_abort(cpu_single_env
, "strllaris_i2c_read: Bad offset 0x%x\n",
733 static void stellaris_i2c_update(stellaris_i2c_state
*s
)
737 level
= (s
->mris
& s
->mimr
) != 0;
738 qemu_set_irq(s
->irq
, level
);
741 static void stellaris_i2c_write(void *opaque
, target_phys_addr_t offset
,
744 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
748 s
->msa
= value
& 0xff;
751 if ((s
->mcr
& 0x10) == 0) {
752 /* Disabled. Do nothing. */
755 /* Grab the bus if this is starting a transfer. */
756 if ((value
& 2) && (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
757 if (i2c_start_transfer(s
->bus
, s
->msa
>> 1, s
->msa
& 1)) {
758 s
->mcs
|= STELLARIS_I2C_MCS_ARBLST
;
760 s
->mcs
&= ~STELLARIS_I2C_MCS_ARBLST
;
761 s
->mcs
|= STELLARIS_I2C_MCS_BUSBSY
;
764 /* If we don't have the bus then indicate an error. */
765 if (!i2c_bus_busy(s
->bus
)
766 || (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
767 s
->mcs
|= STELLARIS_I2C_MCS_ERROR
;
770 s
->mcs
&= ~STELLARIS_I2C_MCS_ERROR
;
772 /* Transfer a byte. */
773 /* TODO: Handle errors. */
776 s
->mdr
= i2c_recv(s
->bus
) & 0xff;
779 i2c_send(s
->bus
, s
->mdr
);
781 /* Raise an interrupt. */
785 /* Finish transfer. */
786 i2c_end_transfer(s
->bus
);
787 s
->mcs
&= ~STELLARIS_I2C_MCS_BUSBSY
;
791 s
->mdr
= value
& 0xff;
793 case 0x0c: /* MTPR */
794 s
->mtpr
= value
& 0xff;
796 case 0x10: /* MIMR */
799 case 0x1c: /* MICR */
804 cpu_abort(cpu_single_env
,
805 "stellaris_i2c_write: Loopback not implemented\n");
807 cpu_abort(cpu_single_env
,
808 "stellaris_i2c_write: Slave mode not implemented\n");
809 s
->mcr
= value
& 0x31;
812 cpu_abort(cpu_single_env
, "stellaris_i2c_write: Bad offset 0x%x\n",
815 stellaris_i2c_update(s
);
818 static void stellaris_i2c_reset(stellaris_i2c_state
*s
)
820 if (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
)
821 i2c_end_transfer(s
->bus
);
830 stellaris_i2c_update(s
);
833 static CPUReadMemoryFunc
*stellaris_i2c_readfn
[] = {
839 static CPUWriteMemoryFunc
*stellaris_i2c_writefn
[] = {
845 static void stellaris_i2c_save(QEMUFile
*f
, void *opaque
)
847 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
849 qemu_put_be32(f
, s
->msa
);
850 qemu_put_be32(f
, s
->mcs
);
851 qemu_put_be32(f
, s
->mdr
);
852 qemu_put_be32(f
, s
->mtpr
);
853 qemu_put_be32(f
, s
->mimr
);
854 qemu_put_be32(f
, s
->mris
);
855 qemu_put_be32(f
, s
->mcr
);
858 static int stellaris_i2c_load(QEMUFile
*f
, void *opaque
, int version_id
)
860 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
865 s
->msa
= qemu_get_be32(f
);
866 s
->mcs
= qemu_get_be32(f
);
867 s
->mdr
= qemu_get_be32(f
);
868 s
->mtpr
= qemu_get_be32(f
);
869 s
->mimr
= qemu_get_be32(f
);
870 s
->mris
= qemu_get_be32(f
);
871 s
->mcr
= qemu_get_be32(f
);
876 static void stellaris_i2c_init(uint32_t base
, qemu_irq irq
, i2c_bus
*bus
)
878 stellaris_i2c_state
*s
;
881 s
= (stellaris_i2c_state
*)qemu_mallocz(sizeof(stellaris_i2c_state
));
885 iomemtype
= cpu_register_io_memory(0, stellaris_i2c_readfn
,
886 stellaris_i2c_writefn
, s
);
887 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
888 /* ??? For now we only implement the master interface. */
889 stellaris_i2c_reset(s
);
890 register_savevm("stellaris_i2c", -1, 1,
891 stellaris_i2c_save
, stellaris_i2c_load
, s
);
894 /* Analogue to Digital Converter. This is only partially implemented,
895 enough for applications that use a combined ADC and timer tick. */
897 #define STELLARIS_ADC_EM_CONTROLLER 0
898 #define STELLARIS_ADC_EM_COMP 1
899 #define STELLARIS_ADC_EM_EXTERNAL 4
900 #define STELLARIS_ADC_EM_TIMER 5
901 #define STELLARIS_ADC_EM_PWM0 6
902 #define STELLARIS_ADC_EM_PWM1 7
903 #define STELLARIS_ADC_EM_PWM2 8
905 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
906 #define STELLARIS_ADC_FIFO_FULL 0x1000
926 } stellaris_adc_state
;
928 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state
*s
, int n
)
932 tail
= s
->fifo
[n
].state
& 0xf;
933 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_EMPTY
) {
936 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf) | ((tail
+ 1) & 0xf);
937 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_FULL
;
938 if (tail
+ 1 == ((s
->fifo
[n
].state
>> 4) & 0xf))
939 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_EMPTY
;
941 return s
->fifo
[n
].data
[tail
];
944 static void stellaris_adc_fifo_write(stellaris_adc_state
*s
, int n
,
949 head
= (s
->fifo
[n
].state
>> 4) & 0xf;
950 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_FULL
) {
954 s
->fifo
[n
].data
[head
] = value
;
955 head
= (head
+ 1) & 0xf;
956 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_EMPTY
;
957 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf0) | (head
<< 4);
958 if ((s
->fifo
[n
].state
& 0xf) == head
)
959 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_FULL
;
962 static void stellaris_adc_update(stellaris_adc_state
*s
)
966 level
= (s
->ris
& s
->im
) != 0;
967 qemu_set_irq(s
->irq
, level
);
970 static void stellaris_adc_trigger(void *opaque
, int irq
, int level
)
972 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
974 if ((s
->actss
& 1) == 0) {
978 /* Some applications use the ADC as a random number source, so introduce
979 some variation into the signal. */
980 s
->noise
= s
->noise
* 314159 + 1;
981 /* ??? actual inputs not implemented. Return an arbitrary value. */
982 stellaris_adc_fifo_write(s
, 0, 0x200 + ((s
->noise
>> 16) & 7));
984 stellaris_adc_update(s
);
987 static void stellaris_adc_reset(stellaris_adc_state
*s
)
991 for (n
= 0; n
< 4; n
++) {
994 s
->fifo
[n
].state
= STELLARIS_ADC_FIFO_EMPTY
;
998 static uint32_t stellaris_adc_read(void *opaque
, target_phys_addr_t offset
)
1000 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1002 /* TODO: Implement this. */
1003 if (offset
>= 0x40 && offset
< 0xc0) {
1005 n
= (offset
- 0x40) >> 5;
1006 switch (offset
& 0x1f) {
1007 case 0x00: /* SSMUX */
1009 case 0x04: /* SSCTL */
1011 case 0x08: /* SSFIFO */
1012 return stellaris_adc_fifo_read(s
, n
);
1013 case 0x0c: /* SSFSTAT */
1014 return s
->fifo
[n
].state
;
1020 case 0x00: /* ACTSS */
1022 case 0x04: /* RIS */
1026 case 0x0c: /* ISC */
1027 return s
->ris
& s
->im
;
1028 case 0x10: /* OSTAT */
1030 case 0x14: /* EMUX */
1032 case 0x18: /* USTAT */
1034 case 0x20: /* SSPRI */
1036 case 0x30: /* SAC */
1039 cpu_abort(cpu_single_env
, "strllaris_adc_read: Bad offset 0x%x\n",
1045 static void stellaris_adc_write(void *opaque
, target_phys_addr_t offset
,
1048 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1050 /* TODO: Implement this. */
1051 if (offset
>= 0x40 && offset
< 0xc0) {
1053 n
= (offset
- 0x40) >> 5;
1054 switch (offset
& 0x1f) {
1055 case 0x00: /* SSMUX */
1056 s
->ssmux
[n
] = value
& 0x33333333;
1058 case 0x04: /* SSCTL */
1060 cpu_abort(cpu_single_env
, "ADC: Unimplemented sequence %x\n",
1063 s
->ssctl
[n
] = value
;
1070 case 0x00: /* ACTSS */
1071 s
->actss
= value
& 0xf;
1073 cpu_abort(cpu_single_env
,
1074 "Not implemented: ADC sequencers 1-3\n");
1080 case 0x0c: /* ISC */
1083 case 0x10: /* OSTAT */
1086 case 0x14: /* EMUX */
1089 case 0x18: /* USTAT */
1092 case 0x20: /* SSPRI */
1095 case 0x28: /* PSSI */
1096 cpu_abort(cpu_single_env
, "Not implemented: ADC sample initiate\n");
1098 case 0x30: /* SAC */
1102 cpu_abort(cpu_single_env
, "stellaris_adc_write: Bad offset 0x%x\n",
1105 stellaris_adc_update(s
);
1108 static CPUReadMemoryFunc
*stellaris_adc_readfn
[] = {
1114 static CPUWriteMemoryFunc
*stellaris_adc_writefn
[] = {
1115 stellaris_adc_write
,
1116 stellaris_adc_write
,
1120 static void stellaris_adc_save(QEMUFile
*f
, void *opaque
)
1122 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1126 qemu_put_be32(f
, s
->actss
);
1127 qemu_put_be32(f
, s
->ris
);
1128 qemu_put_be32(f
, s
->im
);
1129 qemu_put_be32(f
, s
->emux
);
1130 qemu_put_be32(f
, s
->ostat
);
1131 qemu_put_be32(f
, s
->ustat
);
1132 qemu_put_be32(f
, s
->sspri
);
1133 qemu_put_be32(f
, s
->sac
);
1134 for (i
= 0; i
< 4; i
++) {
1135 qemu_put_be32(f
, s
->fifo
[i
].state
);
1136 for (j
= 0; j
< 16; j
++) {
1137 qemu_put_be32(f
, s
->fifo
[i
].data
[j
]);
1139 qemu_put_be32(f
, s
->ssmux
[i
]);
1140 qemu_put_be32(f
, s
->ssctl
[i
]);
1142 qemu_put_be32(f
, s
->noise
);
1145 static int stellaris_adc_load(QEMUFile
*f
, void *opaque
, int version_id
)
1147 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1151 if (version_id
!= 1)
1154 s
->actss
= qemu_get_be32(f
);
1155 s
->ris
= qemu_get_be32(f
);
1156 s
->im
= qemu_get_be32(f
);
1157 s
->emux
= qemu_get_be32(f
);
1158 s
->ostat
= qemu_get_be32(f
);
1159 s
->ustat
= qemu_get_be32(f
);
1160 s
->sspri
= qemu_get_be32(f
);
1161 s
->sac
= qemu_get_be32(f
);
1162 for (i
= 0; i
< 4; i
++) {
1163 s
->fifo
[i
].state
= qemu_get_be32(f
);
1164 for (j
= 0; j
< 16; j
++) {
1165 s
->fifo
[i
].data
[j
] = qemu_get_be32(f
);
1167 s
->ssmux
[i
] = qemu_get_be32(f
);
1168 s
->ssctl
[i
] = qemu_get_be32(f
);
1170 s
->noise
= qemu_get_be32(f
);
1175 static qemu_irq
stellaris_adc_init(uint32_t base
, qemu_irq irq
)
1177 stellaris_adc_state
*s
;
1181 s
= (stellaris_adc_state
*)qemu_mallocz(sizeof(stellaris_adc_state
));
1184 iomemtype
= cpu_register_io_memory(0, stellaris_adc_readfn
,
1185 stellaris_adc_writefn
, s
);
1186 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
1187 stellaris_adc_reset(s
);
1188 qi
= qemu_allocate_irqs(stellaris_adc_trigger
, s
, 1);
1189 register_savevm("stellaris_adc", -1, 1,
1190 stellaris_adc_save
, stellaris_adc_load
, s
);
1194 /* Some boards have both an OLED controller and SD card connected to
1195 the same SSI port, with the SD card chip select connected to a
1196 GPIO pin. Technically the OLED chip select is connected to the SSI
1197 Fss pin. We do not bother emulating that as both devices should
1198 never be selected simultaneously, and our OLED controller ignores stray
1199 0xff commands that occur when deselecting the SD card. */
1202 ssi_xfer_cb xfer_cb
[2];
1206 } stellaris_ssi_bus_state
;
1208 static void stellaris_ssi_bus_select(void *opaque
, int irq
, int level
)
1210 stellaris_ssi_bus_state
*s
= (stellaris_ssi_bus_state
*)opaque
;
1212 s
->current_dev
= level
;
1215 static int stellaris_ssi_bus_xfer(void *opaque
, int val
)
1217 stellaris_ssi_bus_state
*s
= (stellaris_ssi_bus_state
*)opaque
;
1219 return s
->xfer_cb
[s
->current_dev
](s
->opaque
[s
->current_dev
], val
);
1222 static void stellaris_ssi_bus_save(QEMUFile
*f
, void *opaque
)
1224 stellaris_ssi_bus_state
*s
= (stellaris_ssi_bus_state
*)opaque
;
1226 qemu_put_be32(f
, s
->current_dev
);
1229 static int stellaris_ssi_bus_load(QEMUFile
*f
, void *opaque
, int version_id
)
1231 stellaris_ssi_bus_state
*s
= (stellaris_ssi_bus_state
*)opaque
;
1233 if (version_id
!= 1)
1236 s
->current_dev
= qemu_get_be32(f
);
1241 static void *stellaris_ssi_bus_init(qemu_irq
*irqp
,
1242 ssi_xfer_cb cb0
, void *opaque0
,
1243 ssi_xfer_cb cb1
, void *opaque1
)
1246 stellaris_ssi_bus_state
*s
;
1248 s
= (stellaris_ssi_bus_state
*)qemu_mallocz(sizeof(stellaris_ssi_bus_state
));
1249 s
->xfer_cb
[0] = cb0
;
1250 s
->opaque
[0] = opaque0
;
1251 s
->xfer_cb
[1] = cb1
;
1252 s
->opaque
[1] = opaque1
;
1253 qi
= qemu_allocate_irqs(stellaris_ssi_bus_select
, s
, 1);
1255 register_savevm("stellaris_ssi_bus", -1, 1,
1256 stellaris_ssi_bus_save
, stellaris_ssi_bus_load
, s
);
1261 static stellaris_board_info stellaris_boards
[] = {
1265 0x001f001f, /* dc0 */
1275 0x00ff007f, /* dc0 */
1280 BP_OLED_SSI
| BP_GAMEPAD
1284 static void stellaris_init(const char *kernel_filename
, const char *cpu_model
,
1285 stellaris_board_info
*board
)
1287 static const int uart_irq
[] = {5, 6, 33, 34};
1288 static const int timer_irq
[] = {19, 21, 23, 35};
1289 static const uint32_t gpio_addr
[7] =
1290 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1291 0x40024000, 0x40025000, 0x40026000};
1292 static const int gpio_irq
[7] = {0, 1, 2, 3, 4, 30, 31};
1295 qemu_irq
*gpio_in
[7];
1296 qemu_irq
*gpio_out
[7];
1303 flash_size
= ((board
->dc0
& 0xffff) + 1) << 1;
1304 sram_size
= (board
->dc0
>> 18) + 1;
1305 pic
= armv7m_init(flash_size
, sram_size
, kernel_filename
, cpu_model
);
1307 if (board
->dc1
& (1 << 16)) {
1308 adc
= stellaris_adc_init(0x40038000, pic
[14]);
1312 for (i
= 0; i
< 4; i
++) {
1313 if (board
->dc2
& (0x10000 << i
)) {
1314 stellaris_gptm_init(0x40030000 + i
* 0x1000,
1315 pic
[timer_irq
[i
]], adc
);
1319 stellaris_sys_init(0x400fe000, pic
[28], board
, nd_table
[0].macaddr
);
1321 for (i
= 0; i
< 7; i
++) {
1322 if (board
->dc4
& (1 << i
)) {
1323 gpio_in
[i
] = pl061_init(gpio_addr
[i
], pic
[gpio_irq
[i
]],
1328 if (board
->dc2
& (1 << 12)) {
1329 i2c
= i2c_init_bus();
1330 stellaris_i2c_init(0x40020000, pic
[8], i2c
);
1331 if (board
->peripherals
& BP_OLED_I2C
) {
1332 ssd0303_init(i2c
, 0x3d);
1336 for (i
= 0; i
< 4; i
++) {
1337 if (board
->dc2
& (1 << i
)) {
1338 pl011_init(0x4000c000 + i
* 0x1000, pic
[uart_irq
[i
]],
1339 serial_hds
[i
], PL011_LUMINARY
);
1342 if (board
->dc2
& (1 << 4)) {
1343 if (board
->peripherals
& BP_OLED_SSI
) {
1349 oled
= ssd0323_init(&gpio_out
[GPIO_C
][7]);
1350 index
= drive_get_index(IF_SD
, 0, 0);
1351 sd
= ssi_sd_init(drives_table
[index
].bdrv
);
1353 ssi_bus
= stellaris_ssi_bus_init(&gpio_out
[GPIO_D
][0],
1355 ssd0323_xfer_ssi
, oled
);
1357 pl022_init(0x40008000, pic
[7], stellaris_ssi_bus_xfer
, ssi_bus
);
1358 /* Make sure the select pin is high. */
1359 qemu_irq_raise(gpio_out
[GPIO_D
][0]);
1361 pl022_init(0x40008000, pic
[7], NULL
, NULL
);
1364 if (board
->dc4
& (1 << 28))
1365 stellaris_enet_init(&nd_table
[0], 0x40048000, pic
[42]);
1366 if (board
->peripherals
& BP_GAMEPAD
) {
1367 qemu_irq gpad_irq
[5];
1368 static const int gpad_keycode
[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1370 gpad_irq
[0] = qemu_irq_invert(gpio_in
[GPIO_E
][0]); /* up */
1371 gpad_irq
[1] = qemu_irq_invert(gpio_in
[GPIO_E
][1]); /* down */
1372 gpad_irq
[2] = qemu_irq_invert(gpio_in
[GPIO_E
][2]); /* left */
1373 gpad_irq
[3] = qemu_irq_invert(gpio_in
[GPIO_E
][3]); /* right */
1374 gpad_irq
[4] = qemu_irq_invert(gpio_in
[GPIO_F
][1]); /* select */
1376 stellaris_gamepad_init(5, gpad_irq
, gpad_keycode
);
1380 /* FIXME: Figure out how to generate these from stellaris_boards. */
1381 static void lm3s811evb_init(ram_addr_t ram_size
, int vga_ram_size
,
1382 const char *boot_device
,
1383 const char *kernel_filename
, const char *kernel_cmdline
,
1384 const char *initrd_filename
, const char *cpu_model
)
1386 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[0]);
1389 static void lm3s6965evb_init(ram_addr_t ram_size
, int vga_ram_size
,
1390 const char *boot_device
,
1391 const char *kernel_filename
, const char *kernel_cmdline
,
1392 const char *initrd_filename
, const char *cpu_model
)
1394 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[1]);
1397 QEMUMachine lm3s811evb_machine
= {
1398 .name
= "lm3s811evb",
1399 .desc
= "Stellaris LM3S811EVB",
1400 .init
= lm3s811evb_init
,
1401 .ram_require
= (64 * 1024 + 8 * 1024) | RAMSIZE_FIXED
,
1404 QEMUMachine lm3s6965evb_machine
= {
1405 .name
= "lm3s6965evb",
1406 .desc
= "Stellaris LM3S6965EVB",
1407 .init
= lm3s6965evb_init
,
1408 .ram_require
= (256 * 1024 + 64 * 1024) | RAMSIZE_FIXED
,