2 * QEMU PowerPC 4xx embedded processors shared devices emulation
4 * Copyright (c) 2007 Jocelyn Mayer
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
25 #include "qemu/units.h"
28 #include "hw/ppc/ppc.h"
29 #include "hw/ppc/ppc4xx.h"
30 #include "hw/boards.h"
32 #include "exec/address-spaces.h"
33 #include "qemu/error-report.h"
38 # define LOG_UIC(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
40 # define LOG_UIC(...) do { } while (0)
43 static void ppc4xx_reset(void *opaque)
45 PowerPCCPU *cpu = opaque;
50 /*****************************************************************************/
51 /* Generic PowerPC 4xx processor instantiation */
52 PowerPCCPU *ppc4xx_init(const char *cpu_type,
53 clk_setup_t *cpu_clk, clk_setup_t *tb_clk,
60 cpu = POWERPC_CPU(cpu_create(cpu_type));
63 cpu_clk->cb = NULL; /* We don't care about CPU clock frequency changes */
64 cpu_clk->opaque = env;
65 /* Set time-base frequency to sysclk */
66 tb_clk->cb = ppc_40x_timers_init(env, sysclk, PPC_INTERRUPT_PIT);
68 ppc_dcr_init(env, NULL, NULL);
69 /* Register qemu callbacks */
70 qemu_register_reset(ppc4xx_reset, cpu);
75 /*****************************************************************************/
76 /* "Universal" Interrupt controller */
90 #define UIC_MAX_IRQ 32
91 typedef struct ppcuic_t ppcuic_t;
95 uint32_t level; /* Remembers the state of level-triggered interrupts. */
96 uint32_t uicsr; /* Status register */
97 uint32_t uicer; /* Enable register */
98 uint32_t uiccr; /* Critical register */
99 uint32_t uicpr; /* Polarity register */
100 uint32_t uictr; /* Triggering register */
101 uint32_t uicvcr; /* Vector configuration register */
106 static void ppcuic_trigger_irq (ppcuic_t *uic)
109 int start, end, inc, i;
111 /* Trigger interrupt if any is pending */
112 ir = uic->uicsr & uic->uicer & (~uic->uiccr);
113 cr = uic->uicsr & uic->uicer & uic->uiccr;
114 LOG_UIC("%s: uicsr %08" PRIx32 " uicer %08" PRIx32
115 " uiccr %08" PRIx32 "\n"
116 " %08" PRIx32 " ir %08" PRIx32 " cr %08" PRIx32 "\n",
117 __func__, uic->uicsr, uic->uicer, uic->uiccr,
118 uic->uicsr & uic->uicer, ir, cr);
119 if (ir != 0x0000000) {
120 LOG_UIC("Raise UIC interrupt\n");
121 qemu_irq_raise(uic->irqs[PPCUIC_OUTPUT_INT]);
123 LOG_UIC("Lower UIC interrupt\n");
124 qemu_irq_lower(uic->irqs[PPCUIC_OUTPUT_INT]);
126 /* Trigger critical interrupt if any is pending and update vector */
127 if (cr != 0x0000000) {
128 qemu_irq_raise(uic->irqs[PPCUIC_OUTPUT_CINT]);
129 if (uic->use_vectors) {
130 /* Compute critical IRQ vector */
131 if (uic->uicvcr & 1) {
140 uic->uicvr = uic->uicvcr & 0xFFFFFFFC;
141 for (i = start; i <= end; i += inc) {
143 uic->uicvr += (i - start) * 512 * inc;
148 LOG_UIC("Raise UIC critical interrupt - "
149 "vector %08" PRIx32 "\n", uic->uicvr);
151 LOG_UIC("Lower UIC critical interrupt\n");
152 qemu_irq_lower(uic->irqs[PPCUIC_OUTPUT_CINT]);
153 uic->uicvr = 0x00000000;
157 static void ppcuic_set_irq (void *opaque, int irq_num, int level)
163 mask = 1U << (31-irq_num);
164 LOG_UIC("%s: irq %d level %d uicsr %08" PRIx32
165 " mask %08" PRIx32 " => %08" PRIx32 " %08" PRIx32 "\n",
166 __func__, irq_num, level,
167 uic->uicsr, mask, uic->uicsr & mask, level << irq_num);
168 if (irq_num < 0 || irq_num > 31)
172 /* Update status register */
173 if (uic->uictr & mask) {
174 /* Edge sensitive interrupt */
178 /* Level sensitive interrupt */
187 LOG_UIC("%s: irq %d level %d sr %" PRIx32 " => "
188 "%08" PRIx32 "\n", __func__, irq_num, level, uic->uicsr, sr);
189 if (sr != uic->uicsr)
190 ppcuic_trigger_irq(uic);
193 static uint32_t dcr_read_uic (void *opaque, int dcrn)
199 dcrn -= uic->dcr_base;
218 ret = uic->uicsr & uic->uicer;
221 if (!uic->use_vectors)
226 if (!uic->use_vectors)
239 static void dcr_write_uic (void *opaque, int dcrn, uint32_t val)
244 dcrn -= uic->dcr_base;
245 LOG_UIC("%s: dcr %d val 0x%x\n", __func__, dcrn, val);
249 uic->uicsr |= uic->level;
250 ppcuic_trigger_irq(uic);
254 ppcuic_trigger_irq(uic);
258 ppcuic_trigger_irq(uic);
262 ppcuic_trigger_irq(uic);
269 ppcuic_trigger_irq(uic);
276 uic->uicvcr = val & 0xFFFFFFFD;
277 ppcuic_trigger_irq(uic);
282 static void ppcuic_reset (void *opaque)
287 uic->uiccr = 0x00000000;
288 uic->uicer = 0x00000000;
289 uic->uicpr = 0x00000000;
290 uic->uicsr = 0x00000000;
291 uic->uictr = 0x00000000;
292 if (uic->use_vectors) {
293 uic->uicvcr = 0x00000000;
294 uic->uicvr = 0x0000000;
298 qemu_irq *ppcuic_init (CPUPPCState *env, qemu_irq *irqs,
299 uint32_t dcr_base, int has_ssr, int has_vr)
304 uic = g_malloc0(sizeof(ppcuic_t));
305 uic->dcr_base = dcr_base;
308 uic->use_vectors = 1;
309 for (i = 0; i < DCR_UICMAX; i++) {
310 ppc_dcr_register(env, dcr_base + i, uic,
311 &dcr_read_uic, &dcr_write_uic);
313 qemu_register_reset(ppcuic_reset, uic);
315 return qemu_allocate_irqs(&ppcuic_set_irq, uic, UIC_MAX_IRQ);
318 /*****************************************************************************/
319 /* SDRAM controller */
320 typedef struct ppc4xx_sdram_t ppc4xx_sdram_t;
321 struct ppc4xx_sdram_t {
324 MemoryRegion containers[4]; /* used for clipping */
325 MemoryRegion *ram_memories;
343 SDRAM0_CFGADDR = 0x010,
344 SDRAM0_CFGDATA = 0x011,
347 /* XXX: TOFIX: some patches have made this code become inconsistent:
348 * there are type inconsistencies, mixing hwaddr, target_ulong
351 static uint32_t sdram_bcr (hwaddr ram_base,
379 printf("%s: invalid RAM size " TARGET_FMT_plx "\n", __func__,
383 bcr |= ram_base & 0xFF800000;
389 static inline hwaddr sdram_base(uint32_t bcr)
391 return bcr & 0xFF800000;
394 static target_ulong sdram_size (uint32_t bcr)
399 sh = (bcr >> 17) & 0x7;
403 size = (4 * MiB) << sh;
408 static void sdram_set_bcr(ppc4xx_sdram_t *sdram,
409 uint32_t *bcrp, uint32_t bcr, int enabled)
411 unsigned n = bcrp - sdram->bcr;
413 if (*bcrp & 0x00000001) {
416 printf("%s: unmap RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
417 __func__, sdram_base(*bcrp), sdram_size(*bcrp));
419 memory_region_del_subregion(get_system_memory(),
420 &sdram->containers[n]);
421 memory_region_del_subregion(&sdram->containers[n],
422 &sdram->ram_memories[n]);
423 object_unparent(OBJECT(&sdram->containers[n]));
425 *bcrp = bcr & 0xFFDEE001;
426 if (enabled && (bcr & 0x00000001)) {
428 printf("%s: Map RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
429 __func__, sdram_base(bcr), sdram_size(bcr));
431 memory_region_init(&sdram->containers[n], NULL, "sdram-containers",
433 memory_region_add_subregion(&sdram->containers[n], 0,
434 &sdram->ram_memories[n]);
435 memory_region_add_subregion(get_system_memory(),
437 &sdram->containers[n]);
441 static void sdram_map_bcr (ppc4xx_sdram_t *sdram)
445 for (i = 0; i < sdram->nbanks; i++) {
446 if (sdram->ram_sizes[i] != 0) {
449 sdram_bcr(sdram->ram_bases[i], sdram->ram_sizes[i]),
452 sdram_set_bcr(sdram, &sdram->bcr[i], 0x00000000, 0);
457 static void sdram_unmap_bcr (ppc4xx_sdram_t *sdram)
461 for (i = 0; i < sdram->nbanks; i++) {
463 printf("%s: Unmap RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
464 __func__, sdram_base(sdram->bcr[i]), sdram_size(sdram->bcr[i]));
466 memory_region_del_subregion(get_system_memory(),
467 &sdram->ram_memories[i]);
471 static uint32_t dcr_read_sdram (void *opaque, int dcrn)
473 ppc4xx_sdram_t *sdram;
482 switch (sdram->addr) {
483 case 0x00: /* SDRAM_BESR0 */
486 case 0x08: /* SDRAM_BESR1 */
489 case 0x10: /* SDRAM_BEAR */
492 case 0x20: /* SDRAM_CFG */
495 case 0x24: /* SDRAM_STATUS */
498 case 0x30: /* SDRAM_RTR */
501 case 0x34: /* SDRAM_PMIT */
504 case 0x40: /* SDRAM_B0CR */
507 case 0x44: /* SDRAM_B1CR */
510 case 0x48: /* SDRAM_B2CR */
513 case 0x4C: /* SDRAM_B3CR */
516 case 0x80: /* SDRAM_TR */
519 case 0x94: /* SDRAM_ECCCFG */
522 case 0x98: /* SDRAM_ECCESR */
531 /* Avoid gcc warning */
539 static void dcr_write_sdram (void *opaque, int dcrn, uint32_t val)
541 ppc4xx_sdram_t *sdram;
549 switch (sdram->addr) {
550 case 0x00: /* SDRAM_BESR0 */
551 sdram->besr0 &= ~val;
553 case 0x08: /* SDRAM_BESR1 */
554 sdram->besr1 &= ~val;
556 case 0x10: /* SDRAM_BEAR */
559 case 0x20: /* SDRAM_CFG */
561 if (!(sdram->cfg & 0x80000000) && (val & 0x80000000)) {
563 printf("%s: enable SDRAM controller\n", __func__);
565 /* validate all RAM mappings */
566 sdram_map_bcr(sdram);
567 sdram->status &= ~0x80000000;
568 } else if ((sdram->cfg & 0x80000000) && !(val & 0x80000000)) {
570 printf("%s: disable SDRAM controller\n", __func__);
572 /* invalidate all RAM mappings */
573 sdram_unmap_bcr(sdram);
574 sdram->status |= 0x80000000;
576 if (!(sdram->cfg & 0x40000000) && (val & 0x40000000))
577 sdram->status |= 0x40000000;
578 else if ((sdram->cfg & 0x40000000) && !(val & 0x40000000))
579 sdram->status &= ~0x40000000;
582 case 0x24: /* SDRAM_STATUS */
583 /* Read-only register */
585 case 0x30: /* SDRAM_RTR */
586 sdram->rtr = val & 0x3FF80000;
588 case 0x34: /* SDRAM_PMIT */
589 sdram->pmit = (val & 0xF8000000) | 0x07C00000;
591 case 0x40: /* SDRAM_B0CR */
592 sdram_set_bcr(sdram, &sdram->bcr[0], val, sdram->cfg & 0x80000000);
594 case 0x44: /* SDRAM_B1CR */
595 sdram_set_bcr(sdram, &sdram->bcr[1], val, sdram->cfg & 0x80000000);
597 case 0x48: /* SDRAM_B2CR */
598 sdram_set_bcr(sdram, &sdram->bcr[2], val, sdram->cfg & 0x80000000);
600 case 0x4C: /* SDRAM_B3CR */
601 sdram_set_bcr(sdram, &sdram->bcr[3], val, sdram->cfg & 0x80000000);
603 case 0x80: /* SDRAM_TR */
604 sdram->tr = val & 0x018FC01F;
606 case 0x94: /* SDRAM_ECCCFG */
607 sdram->ecccfg = val & 0x00F00000;
609 case 0x98: /* SDRAM_ECCESR */
611 if (sdram->eccesr == 0 && val != 0)
612 qemu_irq_raise(sdram->irq);
613 else if (sdram->eccesr != 0 && val == 0)
614 qemu_irq_lower(sdram->irq);
624 static void sdram_reset (void *opaque)
626 ppc4xx_sdram_t *sdram;
629 sdram->addr = 0x00000000;
630 sdram->bear = 0x00000000;
631 sdram->besr0 = 0x00000000; /* No error */
632 sdram->besr1 = 0x00000000; /* No error */
633 sdram->cfg = 0x00000000;
634 sdram->ecccfg = 0x00000000; /* No ECC */
635 sdram->eccesr = 0x00000000; /* No error */
636 sdram->pmit = 0x07C00000;
637 sdram->rtr = 0x05F00000;
638 sdram->tr = 0x00854009;
639 /* We pre-initialize RAM banks */
640 sdram->status = 0x00000000;
641 sdram->cfg = 0x00800000;
644 void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
645 MemoryRegion *ram_memories,
650 ppc4xx_sdram_t *sdram;
652 sdram = g_malloc0(sizeof(ppc4xx_sdram_t));
654 sdram->nbanks = nbanks;
655 sdram->ram_memories = ram_memories;
656 memset(sdram->ram_bases, 0, 4 * sizeof(hwaddr));
657 memcpy(sdram->ram_bases, ram_bases,
658 nbanks * sizeof(hwaddr));
659 memset(sdram->ram_sizes, 0, 4 * sizeof(hwaddr));
660 memcpy(sdram->ram_sizes, ram_sizes,
661 nbanks * sizeof(hwaddr));
662 qemu_register_reset(&sdram_reset, sdram);
663 ppc_dcr_register(env, SDRAM0_CFGADDR,
664 sdram, &dcr_read_sdram, &dcr_write_sdram);
665 ppc_dcr_register(env, SDRAM0_CFGDATA,
666 sdram, &dcr_read_sdram, &dcr_write_sdram);
668 sdram_map_bcr(sdram);
671 /* Fill in consecutive SDRAM banks with 'ram_size' bytes of memory.
673 * sdram_bank_sizes[] must be 0-terminated.
675 * The 4xx SDRAM controller supports a small number of banks, and each bank
676 * must be one of a small set of sizes. The number of banks and the supported
677 * sizes varies by SoC. */
678 ram_addr_t ppc4xx_sdram_adjust(ram_addr_t ram_size, int nr_banks,
679 MemoryRegion ram_memories[],
682 const unsigned int sdram_bank_sizes[])
684 MemoryRegion *ram = g_malloc0(sizeof(*ram));
685 ram_addr_t size_left = ram_size;
687 unsigned int bank_size;
691 for (i = 0; i < nr_banks; i++) {
692 for (j = 0; sdram_bank_sizes[j] != 0; j++) {
693 bank_size = sdram_bank_sizes[j];
694 if (bank_size <= size_left) {
695 size_left -= bank_size;
699 /* No need to use the remaining banks. */
704 ram_size -= size_left;
706 error_report("Truncating memory to %" PRId64 " MiB to fit SDRAM"
707 " controller limits", ram_size / MiB);
710 memory_region_allocate_system_memory(ram, NULL, "ppc4xx.sdram", ram_size);
712 size_left = ram_size;
713 for (i = 0; i < nr_banks && size_left; i++) {
714 for (j = 0; sdram_bank_sizes[j] != 0; j++) {
715 bank_size = sdram_bank_sizes[j];
717 if (bank_size <= size_left) {
719 snprintf(name, sizeof(name), "ppc4xx.sdram%d", i);
720 memory_region_init_alias(&ram_memories[i], NULL, name, ram,
723 ram_sizes[i] = bank_size;
725 size_left -= bank_size;
734 /*****************************************************************************/
743 MAL0_TXEOBISR = 0x186,
747 MAL0_RXEOBISR = 0x192,
749 MAL0_TXCTP0R = 0x1A0,
750 MAL0_RXCTP0R = 0x1C0,
755 typedef struct ppc4xx_mal_t ppc4xx_mal_t;
756 struct ppc4xx_mal_t {
776 static void ppc4xx_mal_reset(void *opaque)
781 mal->cfg = 0x0007C000;
782 mal->esr = 0x00000000;
783 mal->ier = 0x00000000;
784 mal->rxcasr = 0x00000000;
785 mal->rxdeir = 0x00000000;
786 mal->rxeobisr = 0x00000000;
787 mal->txcasr = 0x00000000;
788 mal->txdeir = 0x00000000;
789 mal->txeobisr = 0x00000000;
792 static uint32_t dcr_read_mal(void *opaque, int dcrn)
836 if (dcrn >= MAL0_TXCTP0R && dcrn < MAL0_TXCTP0R + mal->txcnum) {
837 ret = mal->txctpr[dcrn - MAL0_TXCTP0R];
839 if (dcrn >= MAL0_RXCTP0R && dcrn < MAL0_RXCTP0R + mal->rxcnum) {
840 ret = mal->rxctpr[dcrn - MAL0_RXCTP0R];
842 if (dcrn >= MAL0_RCBS0 && dcrn < MAL0_RCBS0 + mal->rxcnum) {
843 ret = mal->rcbs[dcrn - MAL0_RCBS0];
849 static void dcr_write_mal(void *opaque, int dcrn, uint32_t val)
856 if (val & 0x80000000) {
857 ppc4xx_mal_reset(mal);
859 mal->cfg = val & 0x00FFC087;
866 mal->ier = val & 0x0000001F;
869 mal->txcasr = val & 0xF0000000;
872 mal->txcarr = val & 0xF0000000;
876 mal->txeobisr &= ~val;
883 mal->rxcasr = val & 0xC0000000;
886 mal->rxcarr = val & 0xC0000000;
890 mal->rxeobisr &= ~val;
897 if (dcrn >= MAL0_TXCTP0R && dcrn < MAL0_TXCTP0R + mal->txcnum) {
898 mal->txctpr[dcrn - MAL0_TXCTP0R] = val;
900 if (dcrn >= MAL0_RXCTP0R && dcrn < MAL0_RXCTP0R + mal->rxcnum) {
901 mal->rxctpr[dcrn - MAL0_RXCTP0R] = val;
903 if (dcrn >= MAL0_RCBS0 && dcrn < MAL0_RCBS0 + mal->rxcnum) {
904 mal->rcbs[dcrn - MAL0_RCBS0] = val & 0x000000FF;
908 void ppc4xx_mal_init(CPUPPCState *env, uint8_t txcnum, uint8_t rxcnum,
914 assert(txcnum <= 32 && rxcnum <= 32);
915 mal = g_malloc0(sizeof(*mal));
916 mal->txcnum = txcnum;
917 mal->rxcnum = rxcnum;
918 mal->txctpr = g_new0(uint32_t, txcnum);
919 mal->rxctpr = g_new0(uint32_t, rxcnum);
920 mal->rcbs = g_new0(uint32_t, rxcnum);
921 for (i = 0; i < 4; i++) {
922 mal->irqs[i] = irqs[i];
924 qemu_register_reset(&ppc4xx_mal_reset, mal);
925 ppc_dcr_register(env, MAL0_CFG,
926 mal, &dcr_read_mal, &dcr_write_mal);
927 ppc_dcr_register(env, MAL0_ESR,
928 mal, &dcr_read_mal, &dcr_write_mal);
929 ppc_dcr_register(env, MAL0_IER,
930 mal, &dcr_read_mal, &dcr_write_mal);
931 ppc_dcr_register(env, MAL0_TXCASR,
932 mal, &dcr_read_mal, &dcr_write_mal);
933 ppc_dcr_register(env, MAL0_TXCARR,
934 mal, &dcr_read_mal, &dcr_write_mal);
935 ppc_dcr_register(env, MAL0_TXEOBISR,
936 mal, &dcr_read_mal, &dcr_write_mal);
937 ppc_dcr_register(env, MAL0_TXDEIR,
938 mal, &dcr_read_mal, &dcr_write_mal);
939 ppc_dcr_register(env, MAL0_RXCASR,
940 mal, &dcr_read_mal, &dcr_write_mal);
941 ppc_dcr_register(env, MAL0_RXCARR,
942 mal, &dcr_read_mal, &dcr_write_mal);
943 ppc_dcr_register(env, MAL0_RXEOBISR,
944 mal, &dcr_read_mal, &dcr_write_mal);
945 ppc_dcr_register(env, MAL0_RXDEIR,
946 mal, &dcr_read_mal, &dcr_write_mal);
947 for (i = 0; i < txcnum; i++) {
948 ppc_dcr_register(env, MAL0_TXCTP0R + i,
949 mal, &dcr_read_mal, &dcr_write_mal);
951 for (i = 0; i < rxcnum; i++) {
952 ppc_dcr_register(env, MAL0_RXCTP0R + i,
953 mal, &dcr_read_mal, &dcr_write_mal);
955 for (i = 0; i < rxcnum; i++) {
956 ppc_dcr_register(env, MAL0_RCBS0 + i,
957 mal, &dcr_read_mal, &dcr_write_mal);