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
25 #include "hw/ppc/ppc.h"
26 #include "hw/ppc/ppc4xx.h"
28 #include "exec/address-spaces.h"
34 # define LOG_UIC(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
36 # define LOG_UIC(...) do { } while (0)
39 static void ppc4xx_reset(void *opaque)
41 PowerPCCPU *cpu = opaque;
46 /*****************************************************************************/
47 /* Generic PowerPC 4xx processor instantiation */
48 PowerPCCPU *ppc4xx_init(const char *cpu_model,
49 clk_setup_t *cpu_clk, clk_setup_t *tb_clk,
56 cpu = cpu_ppc_init(cpu_model);
58 fprintf(stderr, "Unable to find PowerPC %s CPU definition\n",
64 cpu_clk->cb = NULL; /* We don't care about CPU clock frequency changes */
65 cpu_clk->opaque = env;
66 /* Set time-base frequency to sysclk */
67 tb_clk->cb = ppc_40x_timers_init(env, sysclk, PPC_INTERRUPT_PIT);
69 ppc_dcr_init(env, NULL, NULL);
70 /* Register qemu callbacks */
71 qemu_register_reset(ppc4xx_reset, cpu);
76 /*****************************************************************************/
77 /* "Universal" Interrupt controller */
91 #define UIC_MAX_IRQ 32
92 typedef struct ppcuic_t ppcuic_t;
96 uint32_t level; /* Remembers the state of level-triggered interrupts. */
97 uint32_t uicsr; /* Status register */
98 uint32_t uicer; /* Enable register */
99 uint32_t uiccr; /* Critical register */
100 uint32_t uicpr; /* Polarity register */
101 uint32_t uictr; /* Triggering register */
102 uint32_t uicvcr; /* Vector configuration register */
107 static void ppcuic_trigger_irq (ppcuic_t *uic)
110 int start, end, inc, i;
112 /* Trigger interrupt if any is pending */
113 ir = uic->uicsr & uic->uicer & (~uic->uiccr);
114 cr = uic->uicsr & uic->uicer & uic->uiccr;
115 LOG_UIC("%s: uicsr %08" PRIx32 " uicer %08" PRIx32
116 " uiccr %08" PRIx32 "\n"
117 " %08" PRIx32 " ir %08" PRIx32 " cr %08" PRIx32 "\n",
118 __func__, uic->uicsr, uic->uicer, uic->uiccr,
119 uic->uicsr & uic->uicer, ir, cr);
120 if (ir != 0x0000000) {
121 LOG_UIC("Raise UIC interrupt\n");
122 qemu_irq_raise(uic->irqs[PPCUIC_OUTPUT_INT]);
124 LOG_UIC("Lower UIC interrupt\n");
125 qemu_irq_lower(uic->irqs[PPCUIC_OUTPUT_INT]);
127 /* Trigger critical interrupt if any is pending and update vector */
128 if (cr != 0x0000000) {
129 qemu_irq_raise(uic->irqs[PPCUIC_OUTPUT_CINT]);
130 if (uic->use_vectors) {
131 /* Compute critical IRQ vector */
132 if (uic->uicvcr & 1) {
141 uic->uicvr = uic->uicvcr & 0xFFFFFFFC;
142 for (i = start; i <= end; i += inc) {
144 uic->uicvr += (i - start) * 512 * inc;
149 LOG_UIC("Raise UIC critical interrupt - "
150 "vector %08" PRIx32 "\n", uic->uicvr);
152 LOG_UIC("Lower UIC critical interrupt\n");
153 qemu_irq_lower(uic->irqs[PPCUIC_OUTPUT_CINT]);
154 uic->uicvr = 0x00000000;
158 static void ppcuic_set_irq (void *opaque, int irq_num, int level)
164 mask = 1U << (31-irq_num);
165 LOG_UIC("%s: irq %d level %d uicsr %08" PRIx32
166 " mask %08" PRIx32 " => %08" PRIx32 " %08" PRIx32 "\n",
167 __func__, irq_num, level,
168 uic->uicsr, mask, uic->uicsr & mask, level << irq_num);
169 if (irq_num < 0 || irq_num > 31)
173 /* Update status register */
174 if (uic->uictr & mask) {
175 /* Edge sensitive interrupt */
179 /* Level sensitive interrupt */
188 LOG_UIC("%s: irq %d level %d sr %" PRIx32 " => "
189 "%08" PRIx32 "\n", __func__, irq_num, level, uic->uicsr, sr);
190 if (sr != uic->uicsr)
191 ppcuic_trigger_irq(uic);
194 static uint32_t dcr_read_uic (void *opaque, int dcrn)
200 dcrn -= uic->dcr_base;
219 ret = uic->uicsr & uic->uicer;
222 if (!uic->use_vectors)
227 if (!uic->use_vectors)
240 static void dcr_write_uic (void *opaque, int dcrn, uint32_t val)
245 dcrn -= uic->dcr_base;
246 LOG_UIC("%s: dcr %d val 0x%x\n", __func__, dcrn, val);
250 uic->uicsr |= uic->level;
251 ppcuic_trigger_irq(uic);
255 ppcuic_trigger_irq(uic);
259 ppcuic_trigger_irq(uic);
263 ppcuic_trigger_irq(uic);
270 ppcuic_trigger_irq(uic);
277 uic->uicvcr = val & 0xFFFFFFFD;
278 ppcuic_trigger_irq(uic);
283 static void ppcuic_reset (void *opaque)
288 uic->uiccr = 0x00000000;
289 uic->uicer = 0x00000000;
290 uic->uicpr = 0x00000000;
291 uic->uicsr = 0x00000000;
292 uic->uictr = 0x00000000;
293 if (uic->use_vectors) {
294 uic->uicvcr = 0x00000000;
295 uic->uicvr = 0x0000000;
299 qemu_irq *ppcuic_init (CPUPPCState *env, qemu_irq *irqs,
300 uint32_t dcr_base, int has_ssr, int has_vr)
305 uic = g_malloc0(sizeof(ppcuic_t));
306 uic->dcr_base = dcr_base;
309 uic->use_vectors = 1;
310 for (i = 0; i < DCR_UICMAX; i++) {
311 ppc_dcr_register(env, dcr_base + i, uic,
312 &dcr_read_uic, &dcr_write_uic);
314 qemu_register_reset(ppcuic_reset, uic);
316 return qemu_allocate_irqs(&ppcuic_set_irq, uic, UIC_MAX_IRQ);
319 /*****************************************************************************/
320 /* SDRAM controller */
321 typedef struct ppc4xx_sdram_t ppc4xx_sdram_t;
322 struct ppc4xx_sdram_t {
325 MemoryRegion containers[4]; /* used for clipping */
326 MemoryRegion *ram_memories;
344 SDRAM0_CFGADDR = 0x010,
345 SDRAM0_CFGDATA = 0x011,
348 /* XXX: TOFIX: some patches have made this code become inconsistent:
349 * there are type inconsistencies, mixing hwaddr, target_ulong
352 static uint32_t sdram_bcr (hwaddr ram_base,
358 case (4 * 1024 * 1024):
361 case (8 * 1024 * 1024):
364 case (16 * 1024 * 1024):
367 case (32 * 1024 * 1024):
370 case (64 * 1024 * 1024):
373 case (128 * 1024 * 1024):
376 case (256 * 1024 * 1024):
380 printf("%s: invalid RAM size " TARGET_FMT_plx "\n", __func__,
384 bcr |= ram_base & 0xFF800000;
390 static inline hwaddr sdram_base(uint32_t bcr)
392 return bcr & 0xFF800000;
395 static target_ulong sdram_size (uint32_t bcr)
400 sh = (bcr >> 17) & 0x7;
404 size = (4 * 1024 * 1024) << sh;
409 static void sdram_set_bcr(ppc4xx_sdram_t *sdram,
410 uint32_t *bcrp, uint32_t bcr, int enabled)
412 unsigned n = bcrp - sdram->bcr;
414 if (*bcrp & 0x00000001) {
417 printf("%s: unmap RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
418 __func__, sdram_base(*bcrp), sdram_size(*bcrp));
420 memory_region_del_subregion(get_system_memory(),
421 &sdram->containers[n]);
422 memory_region_del_subregion(&sdram->containers[n],
423 &sdram->ram_memories[n]);
424 memory_region_destroy(&sdram->containers[n]);
426 *bcrp = bcr & 0xFFDEE001;
427 if (enabled && (bcr & 0x00000001)) {
429 printf("%s: Map RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
430 __func__, sdram_base(bcr), sdram_size(bcr));
432 memory_region_init(&sdram->containers[n], NULL, "sdram-containers",
434 memory_region_add_subregion(&sdram->containers[n], 0,
435 &sdram->ram_memories[n]);
436 memory_region_add_subregion(get_system_memory(),
438 &sdram->containers[n]);
442 static void sdram_map_bcr (ppc4xx_sdram_t *sdram)
446 for (i = 0; i < sdram->nbanks; i++) {
447 if (sdram->ram_sizes[i] != 0) {
450 sdram_bcr(sdram->ram_bases[i], sdram->ram_sizes[i]),
453 sdram_set_bcr(sdram, &sdram->bcr[i], 0x00000000, 0);
458 static void sdram_unmap_bcr (ppc4xx_sdram_t *sdram)
462 for (i = 0; i < sdram->nbanks; i++) {
464 printf("%s: Unmap RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
465 __func__, sdram_base(sdram->bcr[i]), sdram_size(sdram->bcr[i]));
467 memory_region_del_subregion(get_system_memory(),
468 &sdram->ram_memories[i]);
472 static uint32_t dcr_read_sdram (void *opaque, int dcrn)
474 ppc4xx_sdram_t *sdram;
483 switch (sdram->addr) {
484 case 0x00: /* SDRAM_BESR0 */
487 case 0x08: /* SDRAM_BESR1 */
490 case 0x10: /* SDRAM_BEAR */
493 case 0x20: /* SDRAM_CFG */
496 case 0x24: /* SDRAM_STATUS */
499 case 0x30: /* SDRAM_RTR */
502 case 0x34: /* SDRAM_PMIT */
505 case 0x40: /* SDRAM_B0CR */
508 case 0x44: /* SDRAM_B1CR */
511 case 0x48: /* SDRAM_B2CR */
514 case 0x4C: /* SDRAM_B3CR */
517 case 0x80: /* SDRAM_TR */
520 case 0x94: /* SDRAM_ECCCFG */
523 case 0x98: /* SDRAM_ECCESR */
532 /* Avoid gcc warning */
540 static void dcr_write_sdram (void *opaque, int dcrn, uint32_t val)
542 ppc4xx_sdram_t *sdram;
550 switch (sdram->addr) {
551 case 0x00: /* SDRAM_BESR0 */
552 sdram->besr0 &= ~val;
554 case 0x08: /* SDRAM_BESR1 */
555 sdram->besr1 &= ~val;
557 case 0x10: /* SDRAM_BEAR */
560 case 0x20: /* SDRAM_CFG */
562 if (!(sdram->cfg & 0x80000000) && (val & 0x80000000)) {
564 printf("%s: enable SDRAM controller\n", __func__);
566 /* validate all RAM mappings */
567 sdram_map_bcr(sdram);
568 sdram->status &= ~0x80000000;
569 } else if ((sdram->cfg & 0x80000000) && !(val & 0x80000000)) {
571 printf("%s: disable SDRAM controller\n", __func__);
573 /* invalidate all RAM mappings */
574 sdram_unmap_bcr(sdram);
575 sdram->status |= 0x80000000;
577 if (!(sdram->cfg & 0x40000000) && (val & 0x40000000))
578 sdram->status |= 0x40000000;
579 else if ((sdram->cfg & 0x40000000) && !(val & 0x40000000))
580 sdram->status &= ~0x40000000;
583 case 0x24: /* SDRAM_STATUS */
584 /* Read-only register */
586 case 0x30: /* SDRAM_RTR */
587 sdram->rtr = val & 0x3FF80000;
589 case 0x34: /* SDRAM_PMIT */
590 sdram->pmit = (val & 0xF8000000) | 0x07C00000;
592 case 0x40: /* SDRAM_B0CR */
593 sdram_set_bcr(sdram, &sdram->bcr[0], val, sdram->cfg & 0x80000000);
595 case 0x44: /* SDRAM_B1CR */
596 sdram_set_bcr(sdram, &sdram->bcr[1], val, sdram->cfg & 0x80000000);
598 case 0x48: /* SDRAM_B2CR */
599 sdram_set_bcr(sdram, &sdram->bcr[2], val, sdram->cfg & 0x80000000);
601 case 0x4C: /* SDRAM_B3CR */
602 sdram_set_bcr(sdram, &sdram->bcr[3], val, sdram->cfg & 0x80000000);
604 case 0x80: /* SDRAM_TR */
605 sdram->tr = val & 0x018FC01F;
607 case 0x94: /* SDRAM_ECCCFG */
608 sdram->ecccfg = val & 0x00F00000;
610 case 0x98: /* SDRAM_ECCESR */
612 if (sdram->eccesr == 0 && val != 0)
613 qemu_irq_raise(sdram->irq);
614 else if (sdram->eccesr != 0 && val == 0)
615 qemu_irq_lower(sdram->irq);
625 static void sdram_reset (void *opaque)
627 ppc4xx_sdram_t *sdram;
630 sdram->addr = 0x00000000;
631 sdram->bear = 0x00000000;
632 sdram->besr0 = 0x00000000; /* No error */
633 sdram->besr1 = 0x00000000; /* No error */
634 sdram->cfg = 0x00000000;
635 sdram->ecccfg = 0x00000000; /* No ECC */
636 sdram->eccesr = 0x00000000; /* No error */
637 sdram->pmit = 0x07C00000;
638 sdram->rtr = 0x05F00000;
639 sdram->tr = 0x00854009;
640 /* We pre-initialize RAM banks */
641 sdram->status = 0x00000000;
642 sdram->cfg = 0x00800000;
645 void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
646 MemoryRegion *ram_memories,
651 ppc4xx_sdram_t *sdram;
653 sdram = g_malloc0(sizeof(ppc4xx_sdram_t));
655 sdram->nbanks = nbanks;
656 sdram->ram_memories = ram_memories;
657 memset(sdram->ram_bases, 0, 4 * sizeof(hwaddr));
658 memcpy(sdram->ram_bases, ram_bases,
659 nbanks * sizeof(hwaddr));
660 memset(sdram->ram_sizes, 0, 4 * sizeof(hwaddr));
661 memcpy(sdram->ram_sizes, ram_sizes,
662 nbanks * sizeof(hwaddr));
663 qemu_register_reset(&sdram_reset, sdram);
664 ppc_dcr_register(env, SDRAM0_CFGADDR,
665 sdram, &dcr_read_sdram, &dcr_write_sdram);
666 ppc_dcr_register(env, SDRAM0_CFGDATA,
667 sdram, &dcr_read_sdram, &dcr_write_sdram);
669 sdram_map_bcr(sdram);
672 /* Fill in consecutive SDRAM banks with 'ram_size' bytes of memory.
674 * sdram_bank_sizes[] must be 0-terminated.
676 * The 4xx SDRAM controller supports a small number of banks, and each bank
677 * must be one of a small set of sizes. The number of banks and the supported
678 * sizes varies by SoC. */
679 ram_addr_t ppc4xx_sdram_adjust(ram_addr_t ram_size, int nr_banks,
680 MemoryRegion ram_memories[],
683 const unsigned int sdram_bank_sizes[])
685 ram_addr_t size_left = ram_size;
690 for (i = 0; i < nr_banks; i++) {
691 for (j = 0; sdram_bank_sizes[j] != 0; j++) {
692 unsigned int bank_size = sdram_bank_sizes[j];
694 if (bank_size <= size_left) {
696 snprintf(name, sizeof(name), "ppc4xx.sdram%d", i);
697 memory_region_init_ram(&ram_memories[i], NULL, name, bank_size);
698 vmstate_register_ram_global(&ram_memories[i]);
700 ram_sizes[i] = bank_size;
702 size_left -= bank_size;
708 /* No need to use the remaining banks. */
713 ram_size -= size_left;
715 printf("Truncating memory to %d MiB to fit SDRAM controller limits.\n",
716 (int)(ram_size >> 20));