2 * Luminary Micro Stellaris preipherals
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"
19 typedef const struct {
28 enum {OLED_I2C, OLED_SSI} oled;
29 } stellaris_board_info;
31 /* General purpose timer module. */
33 /* Multiplication factor to convert from GPTM timer ticks to qemu timer
35 static int stellaris_clock_scale;
37 typedef struct gptm_state {
46 uint32_t match_prescale[2];
49 struct gptm_state *opaque[2];
52 /* The timers have an alternate output used to trigger the ADC. */
57 static void gptm_update_irq(gptm_state *s)
60 level = (s->state & s->mask) != 0;
61 qemu_set_irq(s->irq, level);
64 static void gptm_stop(gptm_state *s, int n)
66 qemu_del_timer(s->timer[n]);
69 static void gptm_reload(gptm_state *s, int n, int reset)
73 tick = qemu_get_clock(vm_clock);
78 /* 32-bit CountDown. */
80 count = s->load[0] | (s->load[1] << 16);
81 tick += (int64_t)count * stellaris_clock_scale;
82 } else if (s->config == 1) {
83 /* 32-bit RTC. 1Hz tick. */
84 tick += ticks_per_sec;
85 } else if (s->mode[n] == 0xa) {
86 /* PWM mode. Not implemented. */
88 cpu_abort(cpu_single_env, "TODO: 16-bit timer mode 0x%x\n",
92 qemu_mod_timer(s->timer[n], tick);
95 static void gptm_tick(void *opaque)
97 gptm_state **p = (gptm_state **)opaque;
103 if (s->config == 0) {
105 if ((s->control & 0x20)) {
106 /* Output trigger. */
107 qemu_irq_raise(s->trigger);
108 qemu_irq_lower(s->trigger);
110 if (s->mode[0] & 1) {
115 gptm_reload(s, 0, 0);
117 } else if (s->config == 1) {
121 match = s->match[0] | (s->match[1] << 16);
127 gptm_reload(s, 0, 0);
128 } else if (s->mode[n] == 0xa) {
129 /* PWM mode. Not implemented. */
131 cpu_abort(cpu_single_env, "TODO: 16-bit timer mode 0x%x\n",
137 static uint32_t gptm_read(void *opaque, target_phys_addr_t offset)
139 gptm_state *s = (gptm_state *)opaque;
145 case 0x04: /* TAMR */
147 case 0x08: /* TBMR */
156 return s->state & s->mask;
159 case 0x28: /* TAILR */
160 return s->load[0] | ((s->config < 4) ? (s->load[1] << 16) : 0);
161 case 0x2c: /* TBILR */
163 case 0x30: /* TAMARCHR */
164 return s->match[0] | ((s->config < 4) ? (s->match[1] << 16) : 0);
165 case 0x34: /* TBMATCHR */
167 case 0x38: /* TAPR */
168 return s->prescale[0];
169 case 0x3c: /* TBPR */
170 return s->prescale[1];
171 case 0x40: /* TAPMR */
172 return s->match_prescale[0];
173 case 0x44: /* TBPMR */
174 return s->match_prescale[1];
179 cpu_abort(cpu_single_env, "TODO: Timer value read\n");
181 cpu_abort(cpu_single_env, "gptm_read: Bad offset 0x%x\n", (int)offset);
186 static void gptm_write(void *opaque, target_phys_addr_t offset, uint32_t value)
188 gptm_state *s = (gptm_state *)opaque;
192 /* The timers should be disabled before changing the configuration.
193 We take advantage of this and defer everything until the timer
199 case 0x04: /* TAMR */
202 case 0x08: /* TBMR */
208 /* TODO: Implement pause. */
209 if ((oldval ^ value) & 1) {
211 gptm_reload(s, 0, 1);
216 if (((oldval ^ value) & 0x100) && s->config >= 4) {
218 gptm_reload(s, 1, 1);
225 s->mask = value & 0x77;
231 case 0x28: /* TAILR */
232 s->load[0] = value & 0xffff;
234 s->load[1] = value >> 16;
237 case 0x2c: /* TBILR */
238 s->load[1] = value & 0xffff;
240 case 0x30: /* TAMARCHR */
241 s->match[0] = value & 0xffff;
243 s->match[1] = value >> 16;
246 case 0x34: /* TBMATCHR */
247 s->match[1] = value >> 16;
249 case 0x38: /* TAPR */
250 s->prescale[0] = value;
252 case 0x3c: /* TBPR */
253 s->prescale[1] = value;
255 case 0x40: /* TAPMR */
256 s->match_prescale[0] = value;
258 case 0x44: /* TBPMR */
259 s->match_prescale[0] = value;
262 cpu_abort(cpu_single_env, "gptm_write: Bad offset 0x%x\n", (int)offset);
267 static CPUReadMemoryFunc *gptm_readfn[] = {
273 static CPUWriteMemoryFunc *gptm_writefn[] = {
279 static void stellaris_gptm_init(uint32_t base, qemu_irq irq, qemu_irq trigger)
284 s = (gptm_state *)qemu_mallocz(sizeof(gptm_state));
287 s->trigger = trigger;
288 s->opaque[0] = s->opaque[1] = s;
290 iomemtype = cpu_register_io_memory(0, gptm_readfn,
292 cpu_register_physical_memory(base, 0x00001000, iomemtype);
293 s->timer[0] = qemu_new_timer(vm_clock, gptm_tick, &s->opaque[0]);
294 s->timer[1] = qemu_new_timer(vm_clock, gptm_tick, &s->opaque[1]);
295 /* ??? Save/restore. */
299 /* System controller. */
315 stellaris_board_info *board;
318 static void ssys_update(ssys_state *s)
320 qemu_set_irq(s->irq, (s->int_status & s->int_mask) != 0);
323 static uint32_t pllcfg_sandstorm[16] = {
325 0x1ae0, /* 1.8432 Mhz */
327 0xd573, /* 2.4576 Mhz */
328 0x37a6, /* 3.57954 Mhz */
329 0x1ae2, /* 3.6864 Mhz */
331 0x98bc, /* 4.906 Mhz */
332 0x935b, /* 4.9152 Mhz */
334 0x4dee, /* 5.12 Mhz */
336 0x75db, /* 6.144 Mhz */
337 0x1ae6, /* 7.3728 Mhz */
339 0x585b /* 8.192 Mhz */
342 static uint32_t pllcfg_fury[16] = {
344 0x1b20, /* 1.8432 Mhz */
346 0xf42b, /* 2.4576 Mhz */
347 0x37e3, /* 3.57954 Mhz */
348 0x1b21, /* 3.6864 Mhz */
350 0x98ee, /* 4.906 Mhz */
351 0xd5b4, /* 4.9152 Mhz */
353 0x4e27, /* 5.12 Mhz */
355 0xec1c, /* 6.144 Mhz */
356 0x1b23, /* 7.3728 Mhz */
358 0xb11c /* 8.192 Mhz */
361 static uint32_t ssys_read(void *opaque, target_phys_addr_t offset)
363 ssys_state *s = (ssys_state *)opaque;
367 case 0x000: /* DID0 */
368 return s->board->did0;
369 case 0x004: /* DID1 */
370 return s->board->did1;
371 case 0x008: /* DC0 */
372 return s->board->dc0;
373 case 0x010: /* DC1 */
374 return s->board->dc1;
375 case 0x014: /* DC2 */
376 return s->board->dc2;
377 case 0x018: /* DC3 */
378 return s->board->dc3;
379 case 0x01c: /* DC4 */
380 return s->board->dc4;
381 case 0x030: /* PBORCTL */
383 case 0x034: /* LDOPCTL */
385 case 0x040: /* SRCR0 */
387 case 0x044: /* SRCR1 */
389 case 0x048: /* SRCR2 */
391 case 0x050: /* RIS */
392 return s->int_status;
393 case 0x054: /* IMC */
395 case 0x058: /* MISC */
396 return s->int_status & s->int_mask;
397 case 0x05c: /* RESC */
399 case 0x060: /* RCC */
401 case 0x064: /* PLLCFG */
404 xtal = (s->rcc >> 6) & 0xf;
405 if (s->board->did0 & (1 << 16)) {
406 return pllcfg_fury[xtal];
408 return pllcfg_sandstorm[xtal];
411 case 0x100: /* RCGC0 */
413 case 0x104: /* RCGC1 */
415 case 0x108: /* RCGC2 */
417 case 0x110: /* SCGC0 */
419 case 0x114: /* SCGC1 */
421 case 0x118: /* SCGC2 */
423 case 0x120: /* DCGC0 */
425 case 0x124: /* DCGC1 */
427 case 0x128: /* DCGC2 */
429 case 0x150: /* CLKVCLR */
431 case 0x160: /* LDOARST */
434 cpu_abort(cpu_single_env, "gptm_read: Bad offset 0x%x\n", (int)offset);
439 static void ssys_write(void *opaque, target_phys_addr_t offset, uint32_t value)
441 ssys_state *s = (ssys_state *)opaque;
445 case 0x030: /* PBORCTL */
446 s->pborctl = value & 0xffff;
448 case 0x034: /* LDOPCTL */
449 s->ldopctl = value & 0x1f;
451 case 0x040: /* SRCR0 */
452 case 0x044: /* SRCR1 */
453 case 0x048: /* SRCR2 */
454 fprintf(stderr, "Peripheral reset not implemented\n");
456 case 0x054: /* IMC */
457 s->int_mask = value & 0x7f;
459 case 0x058: /* MISC */
460 s->int_status &= ~value;
462 case 0x05c: /* RESC */
463 s->resc = value & 0x3f;
465 case 0x060: /* RCC */
466 if ((s->rcc & (1 << 13)) != 0 && (value & (1 << 13)) == 0) {
468 s->int_status |= (1 << 6);
471 stellaris_clock_scale = 5 * (((s->rcc >> 23) & 0xf) + 1);
473 case 0x100: /* RCGC0 */
476 case 0x104: /* RCGC1 */
479 case 0x108: /* RCGC2 */
482 case 0x110: /* SCGC0 */
485 case 0x114: /* SCGC1 */
488 case 0x118: /* SCGC2 */
491 case 0x120: /* DCGC0 */
494 case 0x124: /* DCGC1 */
497 case 0x128: /* DCGC2 */
500 case 0x150: /* CLKVCLR */
503 case 0x160: /* LDOARST */
507 cpu_abort(cpu_single_env, "gptm_write: Bad offset 0x%x\n", (int)offset);
512 static CPUReadMemoryFunc *ssys_readfn[] = {
518 static CPUWriteMemoryFunc *ssys_writefn[] = {
524 static void ssys_reset(void *opaque)
526 ssys_state *s = (ssys_state *)opaque;
535 static void stellaris_sys_init(uint32_t base, qemu_irq irq,
536 stellaris_board_info * board)
541 s = (ssys_state *)qemu_mallocz(sizeof(ssys_state));
546 iomemtype = cpu_register_io_memory(0, ssys_readfn,
548 cpu_register_physical_memory(base, 0x00001000, iomemtype);
550 /* ??? Save/restore. */
554 /* I2C controller. */
567 } stellaris_i2c_state;
569 #define STELLARIS_I2C_MCS_BUSY 0x01
570 #define STELLARIS_I2C_MCS_ERROR 0x02
571 #define STELLARIS_I2C_MCS_ADRACK 0x04
572 #define STELLARIS_I2C_MCS_DATACK 0x08
573 #define STELLARIS_I2C_MCS_ARBLST 0x10
574 #define STELLARIS_I2C_MCS_IDLE 0x20
575 #define STELLARIS_I2C_MCS_BUSBSY 0x40
577 static uint32_t stellaris_i2c_read(void *opaque, target_phys_addr_t offset)
579 stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;
586 /* We don't emulate timing, so the controller is never busy. */
587 return s->mcs | STELLARIS_I2C_MCS_IDLE;
590 case 0x0c: /* MTPR */
592 case 0x10: /* MIMR */
594 case 0x14: /* MRIS */
596 case 0x18: /* MMIS */
597 return s->mris & s->mimr;
601 cpu_abort(cpu_single_env, "strllaris_i2c_read: Bad offset 0x%x\n",
607 static void stellaris_i2c_update(stellaris_i2c_state *s)
611 level = (s->mris & s->mimr) != 0;
612 qemu_set_irq(s->irq, level);
615 static void stellaris_i2c_write(void *opaque, target_phys_addr_t offset,
618 stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;
623 s->msa = value & 0xff;
626 if ((s->mcr & 0x10) == 0) {
627 /* Disabled. Do nothing. */
630 /* Grab the bus if this is starting a transfer. */
631 if ((value & 2) && (s->mcs & STELLARIS_I2C_MCS_BUSBSY) == 0) {
632 if (i2c_start_transfer(s->bus, s->msa >> 1, s->msa & 1)) {
633 s->mcs |= STELLARIS_I2C_MCS_ARBLST;
635 s->mcs &= ~STELLARIS_I2C_MCS_ARBLST;
636 s->mcs |= STELLARIS_I2C_MCS_BUSBSY;
639 /* If we don't have the bus then indicate an error. */
640 if (!i2c_bus_busy(s->bus)
641 || (s->mcs & STELLARIS_I2C_MCS_BUSBSY) == 0) {
642 s->mcs |= STELLARIS_I2C_MCS_ERROR;
645 s->mcs &= ~STELLARIS_I2C_MCS_ERROR;
647 /* Transfer a byte. */
648 /* TODO: Handle errors. */
651 s->mdr = i2c_recv(s->bus) & 0xff;
654 i2c_send(s->bus, s->mdr);
656 /* Raise an interrupt. */
660 /* Finish transfer. */
661 i2c_end_transfer(s->bus);
662 s->mcs &= ~STELLARIS_I2C_MCS_BUSBSY;
666 s->mdr = value & 0xff;
668 case 0x0c: /* MTPR */
669 s->mtpr = value & 0xff;
671 case 0x10: /* MIMR */
674 case 0x1c: /* MICR */
679 cpu_abort(cpu_single_env,
680 "stellaris_i2c_write: Loopback not implemented\n");
682 cpu_abort(cpu_single_env,
683 "stellaris_i2c_write: Slave mode not implemented\n");
684 s->mcr = value & 0x31;
687 cpu_abort(cpu_single_env, "stellaris_i2c_write: Bad offset 0x%x\n",
690 stellaris_i2c_update(s);
693 static void stellaris_i2c_reset(stellaris_i2c_state *s)
695 if (s->mcs & STELLARIS_I2C_MCS_BUSBSY)
696 i2c_end_transfer(s->bus);
705 stellaris_i2c_update(s);
708 static CPUReadMemoryFunc *stellaris_i2c_readfn[] = {
714 static CPUWriteMemoryFunc *stellaris_i2c_writefn[] = {
720 static void stellaris_i2c_init(uint32_t base, qemu_irq irq, i2c_bus *bus)
722 stellaris_i2c_state *s;
725 s = (stellaris_i2c_state *)qemu_mallocz(sizeof(stellaris_i2c_state));
730 iomemtype = cpu_register_io_memory(0, stellaris_i2c_readfn,
731 stellaris_i2c_writefn, s);
732 cpu_register_physical_memory(base, 0x00001000, iomemtype);
733 /* ??? For now we only implement the master interface. */
734 stellaris_i2c_reset(s);
737 /* Analogue to Digital Converter. This is only partially implemented,
738 enough for applications that use a combined ADC and timer tick. */
740 #define STELLARIS_ADC_EM_CONTROLLER 0
741 #define STELLARIS_ADC_EM_COMP 1
742 #define STELLARIS_ADC_EM_EXTERNAL 4
743 #define STELLARIS_ADC_EM_TIMER 5
744 #define STELLARIS_ADC_EM_PWM0 6
745 #define STELLARIS_ADC_EM_PWM1 7
746 #define STELLARIS_ADC_EM_PWM2 8
748 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
749 #define STELLARIS_ADC_FIFO_FULL 0x1000
769 } stellaris_adc_state;
771 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state *s, int n)
775 tail = s->fifo[n].state & 0xf;
776 if (s->fifo[n].state & STELLARIS_ADC_FIFO_EMPTY) {
779 s->fifo[n].state = (s->fifo[n].state & ~0xf) | ((tail + 1) & 0xf);
780 s->fifo[n].state &= ~STELLARIS_ADC_FIFO_FULL;
781 if (tail + 1 == ((s->fifo[n].state >> 4) & 0xf))
782 s->fifo[n].state |= STELLARIS_ADC_FIFO_EMPTY;
784 return s->fifo[n].data[tail];
787 static void stellaris_adc_fifo_write(stellaris_adc_state *s, int n,
792 head = (s->fifo[n].state >> 4) & 0xf;
793 if (s->fifo[n].state & STELLARIS_ADC_FIFO_FULL) {
797 s->fifo[n].data[head] = value;
798 head = (head + 1) & 0xf;
799 s->fifo[n].state &= ~STELLARIS_ADC_FIFO_EMPTY;
800 s->fifo[n].state = (s->fifo[n].state & ~0xf0) | (head << 4);
801 if ((s->fifo[n].state & 0xf) == head)
802 s->fifo[n].state |= STELLARIS_ADC_FIFO_FULL;
805 static void stellaris_adc_update(stellaris_adc_state *s)
809 level = (s->ris & s->im) != 0;
810 qemu_set_irq(s->irq, level);
813 static void stellaris_adc_trigger(void *opaque, int irq, int level)
815 stellaris_adc_state *s = (stellaris_adc_state *)opaque;
816 /* Some applications use the ADC as a random number source, so introduce
817 some variation into the signal. */
818 static uint32_t noise = 0;
820 if ((s->actss & 1) == 0) {
824 noise = noise * 314159 + 1;
825 /* ??? actual inputs not implemented. Return an arbitrary value. */
826 stellaris_adc_fifo_write(s, 0, 0x200 + ((noise >> 16) & 7));
828 stellaris_adc_update(s);
831 static void stellaris_adc_reset(stellaris_adc_state *s)
835 for (n = 0; n < 4; n++) {
838 s->fifo[n].state = STELLARIS_ADC_FIFO_EMPTY;
842 static uint32_t stellaris_adc_read(void *opaque, target_phys_addr_t offset)
844 stellaris_adc_state *s = (stellaris_adc_state *)opaque;
846 /* TODO: Implement this. */
848 if (offset >= 0x40 && offset < 0xc0) {
850 n = (offset - 0x40) >> 5;
851 switch (offset & 0x1f) {
852 case 0x00: /* SSMUX */
854 case 0x04: /* SSCTL */
856 case 0x08: /* SSFIFO */
857 return stellaris_adc_fifo_read(s, n);
858 case 0x0c: /* SSFSTAT */
859 return s->fifo[n].state;
865 case 0x00: /* ACTSS */
872 return s->ris & s->im;
873 case 0x10: /* OSTAT */
875 case 0x14: /* EMUX */
877 case 0x18: /* USTAT */
879 case 0x20: /* SSPRI */
884 cpu_abort(cpu_single_env, "strllaris_adc_read: Bad offset 0x%x\n",
890 static void stellaris_adc_write(void *opaque, target_phys_addr_t offset,
893 stellaris_adc_state *s = (stellaris_adc_state *)opaque;
895 /* TODO: Implement this. */
897 if (offset >= 0x40 && offset < 0xc0) {
899 n = (offset - 0x40) >> 5;
900 switch (offset & 0x1f) {
901 case 0x00: /* SSMUX */
902 s->ssmux[n] = value & 0x33333333;
904 case 0x04: /* SSCTL */
906 cpu_abort(cpu_single_env, "ADC: Unimplemented sequence %x\n",
916 case 0x00: /* ACTSS */
917 s->actss = value & 0xf;
919 cpu_abort(cpu_single_env,
920 "Not implemented: ADC sequencers 1-3\n");
929 case 0x10: /* OSTAT */
932 case 0x14: /* EMUX */
935 case 0x18: /* USTAT */
938 case 0x20: /* SSPRI */
941 case 0x28: /* PSSI */
942 cpu_abort(cpu_single_env, "Not implemented: ADC sample initiate\n");
948 cpu_abort(cpu_single_env, "stellaris_adc_write: Bad offset 0x%x\n",
951 stellaris_adc_update(s);
954 static CPUReadMemoryFunc *stellaris_adc_readfn[] = {
960 static CPUWriteMemoryFunc *stellaris_adc_writefn[] = {
966 static qemu_irq stellaris_adc_init(uint32_t base, qemu_irq irq)
968 stellaris_adc_state *s;
972 s = (stellaris_adc_state *)qemu_mallocz(sizeof(stellaris_adc_state));
976 iomemtype = cpu_register_io_memory(0, stellaris_adc_readfn,
977 stellaris_adc_writefn, s);
978 cpu_register_physical_memory(base, 0x00001000, iomemtype);
979 stellaris_adc_reset(s);
980 qi = qemu_allocate_irqs(stellaris_adc_trigger, s, 1);
985 static stellaris_board_info stellaris_boards[] = {
989 0x001f001f, /* dc0 */
999 0x00ff007f, /* dc0 */
1008 static void stellaris_init(const char *kernel_filename, const char *cpu_model,
1009 DisplayState *ds, stellaris_board_info *board)
1011 static const int uart_irq[] = {5, 6, 33, 34};
1012 static const int timer_irq[] = {19, 21, 23, 35};
1013 static const uint32_t gpio_addr[7] =
1014 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1015 0x40024000, 0x40025000, 0x40026000};
1016 static const int gpio_irq[7] = {0, 1, 2, 3, 4, 30, 31};
1019 qemu_irq *gpio_in[5];
1020 qemu_irq *gpio_out[5];
1027 flash_size = ((board->dc0 & 0xffff) + 1) << 1;
1028 sram_size = (board->dc0 >> 18) + 1;
1029 pic = armv7m_init(flash_size, sram_size, kernel_filename, cpu_model);
1031 if (board->dc1 & (1 << 16)) {
1032 adc = stellaris_adc_init(0x40038000, pic[14]);
1036 for (i = 0; i < 4; i++) {
1037 if (board->dc2 & (0x10000 << i)) {
1038 stellaris_gptm_init(0x40030000 + i * 0x1000,
1039 pic[timer_irq[i]], adc);
1043 stellaris_sys_init(0x400fe000, pic[28], board);
1045 for (i = 0; i < 7; i++) {
1046 if (board->dc4 & (1 << i)) {
1047 gpio_in[i] = pl061_init(gpio_addr[i], pic[gpio_irq[i]],
1052 if (board->dc2 & (1 << 12)) {
1053 i2c = i2c_init_bus();
1054 stellaris_i2c_init(0x40020000, pic[8], i2c);
1055 if (board->oled == OLED_I2C) {
1056 ssd0303_init(ds, i2c, 0x3d);
1060 for (i = 0; i < 4; i++) {
1061 if (board->dc2 & (1 << i)) {
1062 pl011_init(0x4000c000 + i * 0x1000, pic[uart_irq[i]],
1063 serial_hds[i], PL011_LUMINARY);
1066 if (board->dc2 & (1 << 4)) {
1067 if (board->oled == OLED_SSI) {
1069 /* FIXME: Implement chip select for OLED/MMC. */
1070 oled = ssd0323_init(ds, &gpio_out[2][7]);
1071 pl022_init(0x40008000, pic[7], ssd0323_xfer_ssi, oled);
1073 pl022_init(0x40008000, pic[7], NULL, NULL);
1078 /* FIXME: Figure out how to generate these from stellaris_boards. */
1079 static void lm3s811evb_init(int ram_size, int vga_ram_size,
1080 const char *boot_device, DisplayState *ds,
1081 const char *kernel_filename, const char *kernel_cmdline,
1082 const char *initrd_filename, const char *cpu_model)
1084 stellaris_init(kernel_filename, cpu_model, ds, &stellaris_boards[0]);
1087 static void lm3s6965evb_init(int ram_size, int vga_ram_size,
1088 const char *boot_device, DisplayState *ds,
1089 const char *kernel_filename, const char *kernel_cmdline,
1090 const char *initrd_filename, const char *cpu_model)
1092 stellaris_init(kernel_filename, cpu_model, ds, &stellaris_boards[1]);
1095 QEMUMachine lm3s811evb_machine = {
1097 "Stellaris LM3S811EVB",
1101 QEMUMachine lm3s6965evb_machine = {
1103 "Stellaris LM3S6965EVB",