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_model,
52 clk_setup_t *cpu_clk, clk_setup_t *tb_clk,
59 cpu = cpu_ppc_init(cpu_model);
61 fprintf(stderr, "Unable to find PowerPC %s CPU definition\n",
67 cpu_clk->cb = NULL; /* We don't care about CPU clock frequency changes */
68 cpu_clk->opaque = env;
69 /* Set time-base frequency to sysclk */
70 tb_clk->cb = ppc_40x_timers_init(env, sysclk, PPC_INTERRUPT_PIT);
72 ppc_dcr_init(env, NULL, NULL);
73 /* Register qemu callbacks */
74 qemu_register_reset(ppc4xx_reset, cpu);
79 /*****************************************************************************/
80 /* "Universal" Interrupt controller */
94 #define UIC_MAX_IRQ 32
95 typedef struct ppcuic_t ppcuic_t;
99 uint32_t level; /* Remembers the state of level-triggered interrupts. */
100 uint32_t uicsr; /* Status register */
101 uint32_t uicer; /* Enable register */
102 uint32_t uiccr; /* Critical register */
103 uint32_t uicpr; /* Polarity register */
104 uint32_t uictr; /* Triggering register */
105 uint32_t uicvcr; /* Vector configuration register */
110 static void ppcuic_trigger_irq (ppcuic_t *uic)
113 int start, end, inc, i;
115 /* Trigger interrupt if any is pending */
116 ir = uic->uicsr & uic->uicer & (~uic->uiccr);
117 cr = uic->uicsr & uic->uicer & uic->uiccr;
118 LOG_UIC("%s: uicsr %08" PRIx32 " uicer %08" PRIx32
119 " uiccr %08" PRIx32 "\n"
120 " %08" PRIx32 " ir %08" PRIx32 " cr %08" PRIx32 "\n",
121 __func__, uic->uicsr, uic->uicer, uic->uiccr,
122 uic->uicsr & uic->uicer, ir, cr);
123 if (ir != 0x0000000) {
124 LOG_UIC("Raise UIC interrupt\n");
125 qemu_irq_raise(uic->irqs[PPCUIC_OUTPUT_INT]);
127 LOG_UIC("Lower UIC interrupt\n");
128 qemu_irq_lower(uic->irqs[PPCUIC_OUTPUT_INT]);
130 /* Trigger critical interrupt if any is pending and update vector */
131 if (cr != 0x0000000) {
132 qemu_irq_raise(uic->irqs[PPCUIC_OUTPUT_CINT]);
133 if (uic->use_vectors) {
134 /* Compute critical IRQ vector */
135 if (uic->uicvcr & 1) {
144 uic->uicvr = uic->uicvcr & 0xFFFFFFFC;
145 for (i = start; i <= end; i += inc) {
147 uic->uicvr += (i - start) * 512 * inc;
152 LOG_UIC("Raise UIC critical interrupt - "
153 "vector %08" PRIx32 "\n", uic->uicvr);
155 LOG_UIC("Lower UIC critical interrupt\n");
156 qemu_irq_lower(uic->irqs[PPCUIC_OUTPUT_CINT]);
157 uic->uicvr = 0x00000000;
161 static void ppcuic_set_irq (void *opaque, int irq_num, int level)
167 mask = 1U << (31-irq_num);
168 LOG_UIC("%s: irq %d level %d uicsr %08" PRIx32
169 " mask %08" PRIx32 " => %08" PRIx32 " %08" PRIx32 "\n",
170 __func__, irq_num, level,
171 uic->uicsr, mask, uic->uicsr & mask, level << irq_num);
172 if (irq_num < 0 || irq_num > 31)
176 /* Update status register */
177 if (uic->uictr & mask) {
178 /* Edge sensitive interrupt */
182 /* Level sensitive interrupt */
191 LOG_UIC("%s: irq %d level %d sr %" PRIx32 " => "
192 "%08" PRIx32 "\n", __func__, irq_num, level, uic->uicsr, sr);
193 if (sr != uic->uicsr)
194 ppcuic_trigger_irq(uic);
197 static uint32_t dcr_read_uic (void *opaque, int dcrn)
203 dcrn -= uic->dcr_base;
222 ret = uic->uicsr & uic->uicer;
225 if (!uic->use_vectors)
230 if (!uic->use_vectors)
243 static void dcr_write_uic (void *opaque, int dcrn, uint32_t val)
248 dcrn -= uic->dcr_base;
249 LOG_UIC("%s: dcr %d val 0x%x\n", __func__, dcrn, val);
253 uic->uicsr |= uic->level;
254 ppcuic_trigger_irq(uic);
258 ppcuic_trigger_irq(uic);
262 ppcuic_trigger_irq(uic);
266 ppcuic_trigger_irq(uic);
273 ppcuic_trigger_irq(uic);
280 uic->uicvcr = val & 0xFFFFFFFD;
281 ppcuic_trigger_irq(uic);
286 static void ppcuic_reset (void *opaque)
291 uic->uiccr = 0x00000000;
292 uic->uicer = 0x00000000;
293 uic->uicpr = 0x00000000;
294 uic->uicsr = 0x00000000;
295 uic->uictr = 0x00000000;
296 if (uic->use_vectors) {
297 uic->uicvcr = 0x00000000;
298 uic->uicvr = 0x0000000;
302 qemu_irq *ppcuic_init (CPUPPCState *env, qemu_irq *irqs,
303 uint32_t dcr_base, int has_ssr, int has_vr)
308 uic = g_malloc0(sizeof(ppcuic_t));
309 uic->dcr_base = dcr_base;
312 uic->use_vectors = 1;
313 for (i = 0; i < DCR_UICMAX; i++) {
314 ppc_dcr_register(env, dcr_base + i, uic,
315 &dcr_read_uic, &dcr_write_uic);
317 qemu_register_reset(ppcuic_reset, uic);
319 return qemu_allocate_irqs(&ppcuic_set_irq, uic, UIC_MAX_IRQ);
322 /*****************************************************************************/
323 /* SDRAM controller */
324 typedef struct ppc4xx_sdram_t ppc4xx_sdram_t;
325 struct ppc4xx_sdram_t {
328 MemoryRegion containers[4]; /* used for clipping */
329 MemoryRegion *ram_memories;
347 SDRAM0_CFGADDR = 0x010,
348 SDRAM0_CFGDATA = 0x011,
351 /* XXX: TOFIX: some patches have made this code become inconsistent:
352 * there are type inconsistencies, mixing hwaddr, target_ulong
355 static uint32_t sdram_bcr (hwaddr ram_base,
361 case (4 * 1024 * 1024):
364 case (8 * 1024 * 1024):
367 case (16 * 1024 * 1024):
370 case (32 * 1024 * 1024):
373 case (64 * 1024 * 1024):
376 case (128 * 1024 * 1024):
379 case (256 * 1024 * 1024):
383 printf("%s: invalid RAM size " TARGET_FMT_plx "\n", __func__,
387 bcr |= ram_base & 0xFF800000;
393 static inline hwaddr sdram_base(uint32_t bcr)
395 return bcr & 0xFF800000;
398 static target_ulong sdram_size (uint32_t bcr)
403 sh = (bcr >> 17) & 0x7;
407 size = (4 * 1024 * 1024) << sh;
412 static void sdram_set_bcr(ppc4xx_sdram_t *sdram,
413 uint32_t *bcrp, uint32_t bcr, int enabled)
415 unsigned n = bcrp - sdram->bcr;
417 if (*bcrp & 0x00000001) {
420 printf("%s: unmap RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
421 __func__, sdram_base(*bcrp), sdram_size(*bcrp));
423 memory_region_del_subregion(get_system_memory(),
424 &sdram->containers[n]);
425 memory_region_del_subregion(&sdram->containers[n],
426 &sdram->ram_memories[n]);
427 object_unparent(OBJECT(&sdram->containers[n]));
429 *bcrp = bcr & 0xFFDEE001;
430 if (enabled && (bcr & 0x00000001)) {
432 printf("%s: Map RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
433 __func__, sdram_base(bcr), sdram_size(bcr));
435 memory_region_init(&sdram->containers[n], NULL, "sdram-containers",
437 memory_region_add_subregion(&sdram->containers[n], 0,
438 &sdram->ram_memories[n]);
439 memory_region_add_subregion(get_system_memory(),
441 &sdram->containers[n]);
445 static void sdram_map_bcr (ppc4xx_sdram_t *sdram)
449 for (i = 0; i < sdram->nbanks; i++) {
450 if (sdram->ram_sizes[i] != 0) {
453 sdram_bcr(sdram->ram_bases[i], sdram->ram_sizes[i]),
456 sdram_set_bcr(sdram, &sdram->bcr[i], 0x00000000, 0);
461 static void sdram_unmap_bcr (ppc4xx_sdram_t *sdram)
465 for (i = 0; i < sdram->nbanks; i++) {
467 printf("%s: Unmap RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
468 __func__, sdram_base(sdram->bcr[i]), sdram_size(sdram->bcr[i]));
470 memory_region_del_subregion(get_system_memory(),
471 &sdram->ram_memories[i]);
475 static uint32_t dcr_read_sdram (void *opaque, int dcrn)
477 ppc4xx_sdram_t *sdram;
486 switch (sdram->addr) {
487 case 0x00: /* SDRAM_BESR0 */
490 case 0x08: /* SDRAM_BESR1 */
493 case 0x10: /* SDRAM_BEAR */
496 case 0x20: /* SDRAM_CFG */
499 case 0x24: /* SDRAM_STATUS */
502 case 0x30: /* SDRAM_RTR */
505 case 0x34: /* SDRAM_PMIT */
508 case 0x40: /* SDRAM_B0CR */
511 case 0x44: /* SDRAM_B1CR */
514 case 0x48: /* SDRAM_B2CR */
517 case 0x4C: /* SDRAM_B3CR */
520 case 0x80: /* SDRAM_TR */
523 case 0x94: /* SDRAM_ECCCFG */
526 case 0x98: /* SDRAM_ECCESR */
535 /* Avoid gcc warning */
543 static void dcr_write_sdram (void *opaque, int dcrn, uint32_t val)
545 ppc4xx_sdram_t *sdram;
553 switch (sdram->addr) {
554 case 0x00: /* SDRAM_BESR0 */
555 sdram->besr0 &= ~val;
557 case 0x08: /* SDRAM_BESR1 */
558 sdram->besr1 &= ~val;
560 case 0x10: /* SDRAM_BEAR */
563 case 0x20: /* SDRAM_CFG */
565 if (!(sdram->cfg & 0x80000000) && (val & 0x80000000)) {
567 printf("%s: enable SDRAM controller\n", __func__);
569 /* validate all RAM mappings */
570 sdram_map_bcr(sdram);
571 sdram->status &= ~0x80000000;
572 } else if ((sdram->cfg & 0x80000000) && !(val & 0x80000000)) {
574 printf("%s: disable SDRAM controller\n", __func__);
576 /* invalidate all RAM mappings */
577 sdram_unmap_bcr(sdram);
578 sdram->status |= 0x80000000;
580 if (!(sdram->cfg & 0x40000000) && (val & 0x40000000))
581 sdram->status |= 0x40000000;
582 else if ((sdram->cfg & 0x40000000) && !(val & 0x40000000))
583 sdram->status &= ~0x40000000;
586 case 0x24: /* SDRAM_STATUS */
587 /* Read-only register */
589 case 0x30: /* SDRAM_RTR */
590 sdram->rtr = val & 0x3FF80000;
592 case 0x34: /* SDRAM_PMIT */
593 sdram->pmit = (val & 0xF8000000) | 0x07C00000;
595 case 0x40: /* SDRAM_B0CR */
596 sdram_set_bcr(sdram, &sdram->bcr[0], val, sdram->cfg & 0x80000000);
598 case 0x44: /* SDRAM_B1CR */
599 sdram_set_bcr(sdram, &sdram->bcr[1], val, sdram->cfg & 0x80000000);
601 case 0x48: /* SDRAM_B2CR */
602 sdram_set_bcr(sdram, &sdram->bcr[2], val, sdram->cfg & 0x80000000);
604 case 0x4C: /* SDRAM_B3CR */
605 sdram_set_bcr(sdram, &sdram->bcr[3], val, sdram->cfg & 0x80000000);
607 case 0x80: /* SDRAM_TR */
608 sdram->tr = val & 0x018FC01F;
610 case 0x94: /* SDRAM_ECCCFG */
611 sdram->ecccfg = val & 0x00F00000;
613 case 0x98: /* SDRAM_ECCESR */
615 if (sdram->eccesr == 0 && val != 0)
616 qemu_irq_raise(sdram->irq);
617 else if (sdram->eccesr != 0 && val == 0)
618 qemu_irq_lower(sdram->irq);
628 static void sdram_reset (void *opaque)
630 ppc4xx_sdram_t *sdram;
633 sdram->addr = 0x00000000;
634 sdram->bear = 0x00000000;
635 sdram->besr0 = 0x00000000; /* No error */
636 sdram->besr1 = 0x00000000; /* No error */
637 sdram->cfg = 0x00000000;
638 sdram->ecccfg = 0x00000000; /* No ECC */
639 sdram->eccesr = 0x00000000; /* No error */
640 sdram->pmit = 0x07C00000;
641 sdram->rtr = 0x05F00000;
642 sdram->tr = 0x00854009;
643 /* We pre-initialize RAM banks */
644 sdram->status = 0x00000000;
645 sdram->cfg = 0x00800000;
648 void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
649 MemoryRegion *ram_memories,
654 ppc4xx_sdram_t *sdram;
656 sdram = g_malloc0(sizeof(ppc4xx_sdram_t));
658 sdram->nbanks = nbanks;
659 sdram->ram_memories = ram_memories;
660 memset(sdram->ram_bases, 0, 4 * sizeof(hwaddr));
661 memcpy(sdram->ram_bases, ram_bases,
662 nbanks * sizeof(hwaddr));
663 memset(sdram->ram_sizes, 0, 4 * sizeof(hwaddr));
664 memcpy(sdram->ram_sizes, ram_sizes,
665 nbanks * sizeof(hwaddr));
666 qemu_register_reset(&sdram_reset, sdram);
667 ppc_dcr_register(env, SDRAM0_CFGADDR,
668 sdram, &dcr_read_sdram, &dcr_write_sdram);
669 ppc_dcr_register(env, SDRAM0_CFGDATA,
670 sdram, &dcr_read_sdram, &dcr_write_sdram);
672 sdram_map_bcr(sdram);
675 /* Fill in consecutive SDRAM banks with 'ram_size' bytes of memory.
677 * sdram_bank_sizes[] must be 0-terminated.
679 * The 4xx SDRAM controller supports a small number of banks, and each bank
680 * must be one of a small set of sizes. The number of banks and the supported
681 * sizes varies by SoC. */
682 ram_addr_t ppc4xx_sdram_adjust(ram_addr_t ram_size, int nr_banks,
683 MemoryRegion ram_memories[],
686 const unsigned int sdram_bank_sizes[])
688 MemoryRegion *ram = g_malloc0(sizeof(*ram));
689 ram_addr_t size_left = ram_size;
691 unsigned int bank_size;
695 for (i = 0; i < nr_banks; i++) {
696 for (j = 0; sdram_bank_sizes[j] != 0; j++) {
697 bank_size = sdram_bank_sizes[j];
698 if (bank_size <= size_left) {
699 size_left -= bank_size;
703 /* No need to use the remaining banks. */
708 ram_size -= size_left;
710 printf("Truncating memory to %d MiB to fit SDRAM controller limits.\n",
711 (int)(ram_size >> 20));
714 memory_region_allocate_system_memory(ram, NULL, "ppc4xx.sdram", ram_size);
716 size_left = ram_size;
717 for (i = 0; i < nr_banks && size_left; i++) {
718 for (j = 0; sdram_bank_sizes[j] != 0; j++) {
719 bank_size = sdram_bank_sizes[j];
721 if (bank_size <= size_left) {
723 snprintf(name, sizeof(name), "ppc4xx.sdram%d", i);
724 memory_region_init_alias(&ram_memories[i], NULL, name, ram,
727 ram_sizes[i] = bank_size;
729 size_left -= bank_size;