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"
27 #include "hw/ppc/ppc.h"
28 #include "hw/ppc/ppc4xx.h"
29 #include "hw/boards.h"
31 #include "exec/address-spaces.h"
37 # define LOG_UIC(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
39 # define LOG_UIC(...) do { } while (0)
42 static void ppc4xx_reset(void *opaque)
44 PowerPCCPU *cpu = opaque;
49 /*****************************************************************************/
50 /* Generic PowerPC 4xx processor instantiation */
51 PowerPCCPU *ppc4xx_init(const char *cpu_type,
52 clk_setup_t *cpu_clk, clk_setup_t *tb_clk,
59 cpu = POWERPC_CPU(cpu_create(cpu_type));
62 cpu_clk->cb = NULL; /* We don't care about CPU clock frequency changes */
63 cpu_clk->opaque = env;
64 /* Set time-base frequency to sysclk */
65 tb_clk->cb = ppc_40x_timers_init(env, sysclk, PPC_INTERRUPT_PIT);
67 ppc_dcr_init(env, NULL, NULL);
68 /* Register qemu callbacks */
69 qemu_register_reset(ppc4xx_reset, cpu);
74 /*****************************************************************************/
75 /* "Universal" Interrupt controller */
89 #define UIC_MAX_IRQ 32
90 typedef struct ppcuic_t ppcuic_t;
94 uint32_t level; /* Remembers the state of level-triggered interrupts. */
95 uint32_t uicsr; /* Status register */
96 uint32_t uicer; /* Enable register */
97 uint32_t uiccr; /* Critical register */
98 uint32_t uicpr; /* Polarity register */
99 uint32_t uictr; /* Triggering register */
100 uint32_t uicvcr; /* Vector configuration register */
105 static void ppcuic_trigger_irq (ppcuic_t *uic)
108 int start, end, inc, i;
110 /* Trigger interrupt if any is pending */
111 ir = uic->uicsr & uic->uicer & (~uic->uiccr);
112 cr = uic->uicsr & uic->uicer & uic->uiccr;
113 LOG_UIC("%s: uicsr %08" PRIx32 " uicer %08" PRIx32
114 " uiccr %08" PRIx32 "\n"
115 " %08" PRIx32 " ir %08" PRIx32 " cr %08" PRIx32 "\n",
116 __func__, uic->uicsr, uic->uicer, uic->uiccr,
117 uic->uicsr & uic->uicer, ir, cr);
118 if (ir != 0x0000000) {
119 LOG_UIC("Raise UIC interrupt\n");
120 qemu_irq_raise(uic->irqs[PPCUIC_OUTPUT_INT]);
122 LOG_UIC("Lower UIC interrupt\n");
123 qemu_irq_lower(uic->irqs[PPCUIC_OUTPUT_INT]);
125 /* Trigger critical interrupt if any is pending and update vector */
126 if (cr != 0x0000000) {
127 qemu_irq_raise(uic->irqs[PPCUIC_OUTPUT_CINT]);
128 if (uic->use_vectors) {
129 /* Compute critical IRQ vector */
130 if (uic->uicvcr & 1) {
139 uic->uicvr = uic->uicvcr & 0xFFFFFFFC;
140 for (i = start; i <= end; i += inc) {
142 uic->uicvr += (i - start) * 512 * inc;
147 LOG_UIC("Raise UIC critical interrupt - "
148 "vector %08" PRIx32 "\n", uic->uicvr);
150 LOG_UIC("Lower UIC critical interrupt\n");
151 qemu_irq_lower(uic->irqs[PPCUIC_OUTPUT_CINT]);
152 uic->uicvr = 0x00000000;
156 static void ppcuic_set_irq (void *opaque, int irq_num, int level)
162 mask = 1U << (31-irq_num);
163 LOG_UIC("%s: irq %d level %d uicsr %08" PRIx32
164 " mask %08" PRIx32 " => %08" PRIx32 " %08" PRIx32 "\n",
165 __func__, irq_num, level,
166 uic->uicsr, mask, uic->uicsr & mask, level << irq_num);
167 if (irq_num < 0 || irq_num > 31)
171 /* Update status register */
172 if (uic->uictr & mask) {
173 /* Edge sensitive interrupt */
177 /* Level sensitive interrupt */
186 LOG_UIC("%s: irq %d level %d sr %" PRIx32 " => "
187 "%08" PRIx32 "\n", __func__, irq_num, level, uic->uicsr, sr);
188 if (sr != uic->uicsr)
189 ppcuic_trigger_irq(uic);
192 static uint32_t dcr_read_uic (void *opaque, int dcrn)
198 dcrn -= uic->dcr_base;
217 ret = uic->uicsr & uic->uicer;
220 if (!uic->use_vectors)
225 if (!uic->use_vectors)
238 static void dcr_write_uic (void *opaque, int dcrn, uint32_t val)
243 dcrn -= uic->dcr_base;
244 LOG_UIC("%s: dcr %d val 0x%x\n", __func__, dcrn, val);
248 uic->uicsr |= uic->level;
249 ppcuic_trigger_irq(uic);
253 ppcuic_trigger_irq(uic);
257 ppcuic_trigger_irq(uic);
261 ppcuic_trigger_irq(uic);
268 ppcuic_trigger_irq(uic);
275 uic->uicvcr = val & 0xFFFFFFFD;
276 ppcuic_trigger_irq(uic);
281 static void ppcuic_reset (void *opaque)
286 uic->uiccr = 0x00000000;
287 uic->uicer = 0x00000000;
288 uic->uicpr = 0x00000000;
289 uic->uicsr = 0x00000000;
290 uic->uictr = 0x00000000;
291 if (uic->use_vectors) {
292 uic->uicvcr = 0x00000000;
293 uic->uicvr = 0x0000000;
297 qemu_irq *ppcuic_init (CPUPPCState *env, qemu_irq *irqs,
298 uint32_t dcr_base, int has_ssr, int has_vr)
303 uic = g_malloc0(sizeof(ppcuic_t));
304 uic->dcr_base = dcr_base;
307 uic->use_vectors = 1;
308 for (i = 0; i < DCR_UICMAX; i++) {
309 ppc_dcr_register(env, dcr_base + i, uic,
310 &dcr_read_uic, &dcr_write_uic);
312 qemu_register_reset(ppcuic_reset, uic);
314 return qemu_allocate_irqs(&ppcuic_set_irq, uic, UIC_MAX_IRQ);
317 /*****************************************************************************/
318 /* SDRAM controller */
319 typedef struct ppc4xx_sdram_t ppc4xx_sdram_t;
320 struct ppc4xx_sdram_t {
323 MemoryRegion containers[4]; /* used for clipping */
324 MemoryRegion *ram_memories;
342 SDRAM0_CFGADDR = 0x010,
343 SDRAM0_CFGDATA = 0x011,
346 /* XXX: TOFIX: some patches have made this code become inconsistent:
347 * there are type inconsistencies, mixing hwaddr, target_ulong
350 static uint32_t sdram_bcr (hwaddr ram_base,
356 case (4 * 1024 * 1024):
359 case (8 * 1024 * 1024):
362 case (16 * 1024 * 1024):
365 case (32 * 1024 * 1024):
368 case (64 * 1024 * 1024):
371 case (128 * 1024 * 1024):
374 case (256 * 1024 * 1024):
378 printf("%s: invalid RAM size " TARGET_FMT_plx "\n", __func__,
382 bcr |= ram_base & 0xFF800000;
388 static inline hwaddr sdram_base(uint32_t bcr)
390 return bcr & 0xFF800000;
393 static target_ulong sdram_size (uint32_t bcr)
398 sh = (bcr >> 17) & 0x7;
402 size = (4 * 1024 * 1024) << sh;
407 static void sdram_set_bcr(ppc4xx_sdram_t *sdram,
408 uint32_t *bcrp, uint32_t bcr, int enabled)
410 unsigned n = bcrp - sdram->bcr;
412 if (*bcrp & 0x00000001) {
415 printf("%s: unmap RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
416 __func__, sdram_base(*bcrp), sdram_size(*bcrp));
418 memory_region_del_subregion(get_system_memory(),
419 &sdram->containers[n]);
420 memory_region_del_subregion(&sdram->containers[n],
421 &sdram->ram_memories[n]);
422 object_unparent(OBJECT(&sdram->containers[n]));
424 *bcrp = bcr & 0xFFDEE001;
425 if (enabled && (bcr & 0x00000001)) {
427 printf("%s: Map RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
428 __func__, sdram_base(bcr), sdram_size(bcr));
430 memory_region_init(&sdram->containers[n], NULL, "sdram-containers",
432 memory_region_add_subregion(&sdram->containers[n], 0,
433 &sdram->ram_memories[n]);
434 memory_region_add_subregion(get_system_memory(),
436 &sdram->containers[n]);
440 static void sdram_map_bcr (ppc4xx_sdram_t *sdram)
444 for (i = 0; i < sdram->nbanks; i++) {
445 if (sdram->ram_sizes[i] != 0) {
448 sdram_bcr(sdram->ram_bases[i], sdram->ram_sizes[i]),
451 sdram_set_bcr(sdram, &sdram->bcr[i], 0x00000000, 0);
456 static void sdram_unmap_bcr (ppc4xx_sdram_t *sdram)
460 for (i = 0; i < sdram->nbanks; i++) {
462 printf("%s: Unmap RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
463 __func__, sdram_base(sdram->bcr[i]), sdram_size(sdram->bcr[i]));
465 memory_region_del_subregion(get_system_memory(),
466 &sdram->ram_memories[i]);
470 static uint32_t dcr_read_sdram (void *opaque, int dcrn)
472 ppc4xx_sdram_t *sdram;
481 switch (sdram->addr) {
482 case 0x00: /* SDRAM_BESR0 */
485 case 0x08: /* SDRAM_BESR1 */
488 case 0x10: /* SDRAM_BEAR */
491 case 0x20: /* SDRAM_CFG */
494 case 0x24: /* SDRAM_STATUS */
497 case 0x30: /* SDRAM_RTR */
500 case 0x34: /* SDRAM_PMIT */
503 case 0x40: /* SDRAM_B0CR */
506 case 0x44: /* SDRAM_B1CR */
509 case 0x48: /* SDRAM_B2CR */
512 case 0x4C: /* SDRAM_B3CR */
515 case 0x80: /* SDRAM_TR */
518 case 0x94: /* SDRAM_ECCCFG */
521 case 0x98: /* SDRAM_ECCESR */
530 /* Avoid gcc warning */
538 static void dcr_write_sdram (void *opaque, int dcrn, uint32_t val)
540 ppc4xx_sdram_t *sdram;
548 switch (sdram->addr) {
549 case 0x00: /* SDRAM_BESR0 */
550 sdram->besr0 &= ~val;
552 case 0x08: /* SDRAM_BESR1 */
553 sdram->besr1 &= ~val;
555 case 0x10: /* SDRAM_BEAR */
558 case 0x20: /* SDRAM_CFG */
560 if (!(sdram->cfg & 0x80000000) && (val & 0x80000000)) {
562 printf("%s: enable SDRAM controller\n", __func__);
564 /* validate all RAM mappings */
565 sdram_map_bcr(sdram);
566 sdram->status &= ~0x80000000;
567 } else if ((sdram->cfg & 0x80000000) && !(val & 0x80000000)) {
569 printf("%s: disable SDRAM controller\n", __func__);
571 /* invalidate all RAM mappings */
572 sdram_unmap_bcr(sdram);
573 sdram->status |= 0x80000000;
575 if (!(sdram->cfg & 0x40000000) && (val & 0x40000000))
576 sdram->status |= 0x40000000;
577 else if ((sdram->cfg & 0x40000000) && !(val & 0x40000000))
578 sdram->status &= ~0x40000000;
581 case 0x24: /* SDRAM_STATUS */
582 /* Read-only register */
584 case 0x30: /* SDRAM_RTR */
585 sdram->rtr = val & 0x3FF80000;
587 case 0x34: /* SDRAM_PMIT */
588 sdram->pmit = (val & 0xF8000000) | 0x07C00000;
590 case 0x40: /* SDRAM_B0CR */
591 sdram_set_bcr(sdram, &sdram->bcr[0], val, sdram->cfg & 0x80000000);
593 case 0x44: /* SDRAM_B1CR */
594 sdram_set_bcr(sdram, &sdram->bcr[1], val, sdram->cfg & 0x80000000);
596 case 0x48: /* SDRAM_B2CR */
597 sdram_set_bcr(sdram, &sdram->bcr[2], val, sdram->cfg & 0x80000000);
599 case 0x4C: /* SDRAM_B3CR */
600 sdram_set_bcr(sdram, &sdram->bcr[3], val, sdram->cfg & 0x80000000);
602 case 0x80: /* SDRAM_TR */
603 sdram->tr = val & 0x018FC01F;
605 case 0x94: /* SDRAM_ECCCFG */
606 sdram->ecccfg = val & 0x00F00000;
608 case 0x98: /* SDRAM_ECCESR */
610 if (sdram->eccesr == 0 && val != 0)
611 qemu_irq_raise(sdram->irq);
612 else if (sdram->eccesr != 0 && val == 0)
613 qemu_irq_lower(sdram->irq);
623 static void sdram_reset (void *opaque)
625 ppc4xx_sdram_t *sdram;
628 sdram->addr = 0x00000000;
629 sdram->bear = 0x00000000;
630 sdram->besr0 = 0x00000000; /* No error */
631 sdram->besr1 = 0x00000000; /* No error */
632 sdram->cfg = 0x00000000;
633 sdram->ecccfg = 0x00000000; /* No ECC */
634 sdram->eccesr = 0x00000000; /* No error */
635 sdram->pmit = 0x07C00000;
636 sdram->rtr = 0x05F00000;
637 sdram->tr = 0x00854009;
638 /* We pre-initialize RAM banks */
639 sdram->status = 0x00000000;
640 sdram->cfg = 0x00800000;
643 void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
644 MemoryRegion *ram_memories,
649 ppc4xx_sdram_t *sdram;
651 sdram = g_malloc0(sizeof(ppc4xx_sdram_t));
653 sdram->nbanks = nbanks;
654 sdram->ram_memories = ram_memories;
655 memset(sdram->ram_bases, 0, 4 * sizeof(hwaddr));
656 memcpy(sdram->ram_bases, ram_bases,
657 nbanks * sizeof(hwaddr));
658 memset(sdram->ram_sizes, 0, 4 * sizeof(hwaddr));
659 memcpy(sdram->ram_sizes, ram_sizes,
660 nbanks * sizeof(hwaddr));
661 qemu_register_reset(&sdram_reset, sdram);
662 ppc_dcr_register(env, SDRAM0_CFGADDR,
663 sdram, &dcr_read_sdram, &dcr_write_sdram);
664 ppc_dcr_register(env, SDRAM0_CFGDATA,
665 sdram, &dcr_read_sdram, &dcr_write_sdram);
667 sdram_map_bcr(sdram);
670 /* Fill in consecutive SDRAM banks with 'ram_size' bytes of memory.
672 * sdram_bank_sizes[] must be 0-terminated.
674 * The 4xx SDRAM controller supports a small number of banks, and each bank
675 * must be one of a small set of sizes. The number of banks and the supported
676 * sizes varies by SoC. */
677 ram_addr_t ppc4xx_sdram_adjust(ram_addr_t ram_size, int nr_banks,
678 MemoryRegion ram_memories[],
681 const unsigned int sdram_bank_sizes[])
683 MemoryRegion *ram = g_malloc0(sizeof(*ram));
684 ram_addr_t size_left = ram_size;
686 unsigned int bank_size;
690 for (i = 0; i < nr_banks; i++) {
691 for (j = 0; sdram_bank_sizes[j] != 0; j++) {
692 bank_size = sdram_bank_sizes[j];
693 if (bank_size <= size_left) {
694 size_left -= bank_size;
698 /* No need to use the remaining banks. */
703 ram_size -= size_left;
705 printf("Truncating memory to %d MiB to fit SDRAM controller limits.\n",
706 (int)(ram_size >> 20));
709 memory_region_allocate_system_memory(ram, NULL, "ppc4xx.sdram", ram_size);
711 size_left = ram_size;
712 for (i = 0; i < nr_banks && size_left; i++) {
713 for (j = 0; sdram_bank_sizes[j] != 0; j++) {
714 bank_size = sdram_bank_sizes[j];
716 if (bank_size <= size_left) {
718 snprintf(name, sizeof(name), "ppc4xx.sdram%d", i);
719 memory_region_init_alias(&ram_memories[i], NULL, name, ram,
722 ram_sizes[i] = bank_size;
724 size_left -= bank_size;
733 /*****************************************************************************/
742 MAL0_TXEOBISR = 0x186,
746 MAL0_RXEOBISR = 0x192,
748 MAL0_TXCTP0R = 0x1A0,
749 MAL0_RXCTP0R = 0x1C0,
754 typedef struct ppc4xx_mal_t ppc4xx_mal_t;
755 struct ppc4xx_mal_t {
775 static void ppc4xx_mal_reset(void *opaque)
780 mal->cfg = 0x0007C000;
781 mal->esr = 0x00000000;
782 mal->ier = 0x00000000;
783 mal->rxcasr = 0x00000000;
784 mal->rxdeir = 0x00000000;
785 mal->rxeobisr = 0x00000000;
786 mal->txcasr = 0x00000000;
787 mal->txdeir = 0x00000000;
788 mal->txeobisr = 0x00000000;
791 static uint32_t dcr_read_mal(void *opaque, int dcrn)
835 if (dcrn >= MAL0_TXCTP0R && dcrn < MAL0_TXCTP0R + mal->txcnum) {
836 ret = mal->txctpr[dcrn - MAL0_TXCTP0R];
838 if (dcrn >= MAL0_RXCTP0R && dcrn < MAL0_RXCTP0R + mal->rxcnum) {
839 ret = mal->rxctpr[dcrn - MAL0_RXCTP0R];
841 if (dcrn >= MAL0_RCBS0 && dcrn < MAL0_RCBS0 + mal->rxcnum) {
842 ret = mal->rcbs[dcrn - MAL0_RCBS0];
848 static void dcr_write_mal(void *opaque, int dcrn, uint32_t val)
855 if (val & 0x80000000) {
856 ppc4xx_mal_reset(mal);
858 mal->cfg = val & 0x00FFC087;
865 mal->ier = val & 0x0000001F;
868 mal->txcasr = val & 0xF0000000;
871 mal->txcarr = val & 0xF0000000;
875 mal->txeobisr &= ~val;
882 mal->rxcasr = val & 0xC0000000;
885 mal->rxcarr = val & 0xC0000000;
889 mal->rxeobisr &= ~val;
896 if (dcrn >= MAL0_TXCTP0R && dcrn < MAL0_TXCTP0R + mal->txcnum) {
897 mal->txctpr[dcrn - MAL0_TXCTP0R] = val;
899 if (dcrn >= MAL0_RXCTP0R && dcrn < MAL0_RXCTP0R + mal->rxcnum) {
900 mal->rxctpr[dcrn - MAL0_RXCTP0R] = val;
902 if (dcrn >= MAL0_RCBS0 && dcrn < MAL0_RCBS0 + mal->rxcnum) {
903 mal->rcbs[dcrn - MAL0_RCBS0] = val & 0x000000FF;
907 void ppc4xx_mal_init(CPUPPCState *env, uint8_t txcnum, uint8_t rxcnum,
913 assert(txcnum <= 32 && rxcnum <= 32);
914 mal = g_malloc0(sizeof(*mal));
915 mal->txcnum = txcnum;
916 mal->rxcnum = rxcnum;
917 mal->txctpr = g_new0(uint32_t, txcnum);
918 mal->rxctpr = g_new0(uint32_t, rxcnum);
919 mal->rcbs = g_new0(uint32_t, rxcnum);
920 for (i = 0; i < 4; i++) {
921 mal->irqs[i] = irqs[i];
923 qemu_register_reset(&ppc4xx_mal_reset, mal);
924 ppc_dcr_register(env, MAL0_CFG,
925 mal, &dcr_read_mal, &dcr_write_mal);
926 ppc_dcr_register(env, MAL0_ESR,
927 mal, &dcr_read_mal, &dcr_write_mal);
928 ppc_dcr_register(env, MAL0_IER,
929 mal, &dcr_read_mal, &dcr_write_mal);
930 ppc_dcr_register(env, MAL0_TXCASR,
931 mal, &dcr_read_mal, &dcr_write_mal);
932 ppc_dcr_register(env, MAL0_TXCARR,
933 mal, &dcr_read_mal, &dcr_write_mal);
934 ppc_dcr_register(env, MAL0_TXEOBISR,
935 mal, &dcr_read_mal, &dcr_write_mal);
936 ppc_dcr_register(env, MAL0_TXDEIR,
937 mal, &dcr_read_mal, &dcr_write_mal);
938 ppc_dcr_register(env, MAL0_RXCASR,
939 mal, &dcr_read_mal, &dcr_write_mal);
940 ppc_dcr_register(env, MAL0_RXCARR,
941 mal, &dcr_read_mal, &dcr_write_mal);
942 ppc_dcr_register(env, MAL0_RXEOBISR,
943 mal, &dcr_read_mal, &dcr_write_mal);
944 ppc_dcr_register(env, MAL0_RXDEIR,
945 mal, &dcr_read_mal, &dcr_write_mal);
946 for (i = 0; i < txcnum; i++) {
947 ppc_dcr_register(env, MAL0_TXCTP0R + i,
948 mal, &dcr_read_mal, &dcr_write_mal);
950 for (i = 0; i < rxcnum; i++) {
951 ppc_dcr_register(env, MAL0_RXCTP0R + i,
952 mal, &dcr_read_mal, &dcr_write_mal);
954 for (i = 0; i < rxcnum; i++) {
955 ppc_dcr_register(env, MAL0_RCBS0 + i,
956 mal, &dcr_read_mal, &dcr_write_mal);