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"
31 //#define DEBUG_UNASSIGNED
36 # define LOG_UIC(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
38 # define LOG_UIC(...) do { } while (0)
41 static void ppc4xx_reset(void *opaque
)
43 PowerPCCPU
*cpu
= opaque
;
48 /*****************************************************************************/
49 /* Generic PowerPC 4xx processor instantiation */
50 PowerPCCPU
*ppc4xx_init(const char *cpu_model
,
51 clk_setup_t
*cpu_clk
, clk_setup_t
*tb_clk
,
58 cpu
= cpu_ppc_init(cpu_model
);
60 fprintf(stderr
, "Unable to find PowerPC %s CPU definition\n",
66 cpu_clk
->cb
= NULL
; /* We don't care about CPU clock frequency changes */
67 cpu_clk
->opaque
= env
;
68 /* Set time-base frequency to sysclk */
69 tb_clk
->cb
= ppc_40x_timers_init(env
, sysclk
, PPC_INTERRUPT_PIT
);
71 ppc_dcr_init(env
, NULL
, NULL
);
72 /* Register qemu callbacks */
73 qemu_register_reset(ppc4xx_reset
, cpu
);
78 /*****************************************************************************/
79 /* "Universal" Interrupt controller */
93 #define UIC_MAX_IRQ 32
94 typedef struct ppcuic_t ppcuic_t
;
98 uint32_t level
; /* Remembers the state of level-triggered interrupts. */
99 uint32_t uicsr
; /* Status register */
100 uint32_t uicer
; /* Enable register */
101 uint32_t uiccr
; /* Critical register */
102 uint32_t uicpr
; /* Polarity register */
103 uint32_t uictr
; /* Triggering register */
104 uint32_t uicvcr
; /* Vector configuration register */
109 static void ppcuic_trigger_irq (ppcuic_t
*uic
)
112 int start
, end
, inc
, i
;
114 /* Trigger interrupt if any is pending */
115 ir
= uic
->uicsr
& uic
->uicer
& (~uic
->uiccr
);
116 cr
= uic
->uicsr
& uic
->uicer
& uic
->uiccr
;
117 LOG_UIC("%s: uicsr %08" PRIx32
" uicer %08" PRIx32
118 " uiccr %08" PRIx32
"\n"
119 " %08" PRIx32
" ir %08" PRIx32
" cr %08" PRIx32
"\n",
120 __func__
, uic
->uicsr
, uic
->uicer
, uic
->uiccr
,
121 uic
->uicsr
& uic
->uicer
, ir
, cr
);
122 if (ir
!= 0x0000000) {
123 LOG_UIC("Raise UIC interrupt\n");
124 qemu_irq_raise(uic
->irqs
[PPCUIC_OUTPUT_INT
]);
126 LOG_UIC("Lower UIC interrupt\n");
127 qemu_irq_lower(uic
->irqs
[PPCUIC_OUTPUT_INT
]);
129 /* Trigger critical interrupt if any is pending and update vector */
130 if (cr
!= 0x0000000) {
131 qemu_irq_raise(uic
->irqs
[PPCUIC_OUTPUT_CINT
]);
132 if (uic
->use_vectors
) {
133 /* Compute critical IRQ vector */
134 if (uic
->uicvcr
& 1) {
143 uic
->uicvr
= uic
->uicvcr
& 0xFFFFFFFC;
144 for (i
= start
; i
<= end
; i
+= inc
) {
146 uic
->uicvr
+= (i
- start
) * 512 * inc
;
151 LOG_UIC("Raise UIC critical interrupt - "
152 "vector %08" PRIx32
"\n", uic
->uicvr
);
154 LOG_UIC("Lower UIC critical interrupt\n");
155 qemu_irq_lower(uic
->irqs
[PPCUIC_OUTPUT_CINT
]);
156 uic
->uicvr
= 0x00000000;
160 static void ppcuic_set_irq (void *opaque
, int irq_num
, int level
)
166 mask
= 1 << (31-irq_num
);
167 LOG_UIC("%s: irq %d level %d uicsr %08" PRIx32
168 " mask %08" PRIx32
" => %08" PRIx32
" %08" PRIx32
"\n",
169 __func__
, irq_num
, level
,
170 uic
->uicsr
, mask
, uic
->uicsr
& mask
, level
<< irq_num
);
171 if (irq_num
< 0 || irq_num
> 31)
175 /* Update status register */
176 if (uic
->uictr
& mask
) {
177 /* Edge sensitive interrupt */
181 /* Level sensitive interrupt */
190 LOG_UIC("%s: irq %d level %d sr %" PRIx32
" => "
191 "%08" PRIx32
"\n", __func__
, irq_num
, level
, uic
->uicsr
, sr
);
192 if (sr
!= uic
->uicsr
)
193 ppcuic_trigger_irq(uic
);
196 static uint32_t dcr_read_uic (void *opaque
, int dcrn
)
202 dcrn
-= uic
->dcr_base
;
221 ret
= uic
->uicsr
& uic
->uicer
;
224 if (!uic
->use_vectors
)
229 if (!uic
->use_vectors
)
242 static void dcr_write_uic (void *opaque
, int dcrn
, uint32_t val
)
247 dcrn
-= uic
->dcr_base
;
248 LOG_UIC("%s: dcr %d val 0x%x\n", __func__
, dcrn
, val
);
252 uic
->uicsr
|= uic
->level
;
253 ppcuic_trigger_irq(uic
);
257 ppcuic_trigger_irq(uic
);
261 ppcuic_trigger_irq(uic
);
265 ppcuic_trigger_irq(uic
);
272 ppcuic_trigger_irq(uic
);
279 uic
->uicvcr
= val
& 0xFFFFFFFD;
280 ppcuic_trigger_irq(uic
);
285 static void ppcuic_reset (void *opaque
)
290 uic
->uiccr
= 0x00000000;
291 uic
->uicer
= 0x00000000;
292 uic
->uicpr
= 0x00000000;
293 uic
->uicsr
= 0x00000000;
294 uic
->uictr
= 0x00000000;
295 if (uic
->use_vectors
) {
296 uic
->uicvcr
= 0x00000000;
297 uic
->uicvr
= 0x0000000;
301 qemu_irq
*ppcuic_init (CPUPPCState
*env
, qemu_irq
*irqs
,
302 uint32_t dcr_base
, int has_ssr
, int has_vr
)
307 uic
= g_malloc0(sizeof(ppcuic_t
));
308 uic
->dcr_base
= dcr_base
;
311 uic
->use_vectors
= 1;
312 for (i
= 0; i
< DCR_UICMAX
; i
++) {
313 ppc_dcr_register(env
, dcr_base
+ i
, uic
,
314 &dcr_read_uic
, &dcr_write_uic
);
316 qemu_register_reset(ppcuic_reset
, uic
);
318 return qemu_allocate_irqs(&ppcuic_set_irq
, uic
, UIC_MAX_IRQ
);
321 /*****************************************************************************/
322 /* SDRAM controller */
323 typedef struct ppc4xx_sdram_t ppc4xx_sdram_t
;
324 struct ppc4xx_sdram_t
{
327 MemoryRegion containers
[4]; /* used for clipping */
328 MemoryRegion
*ram_memories
;
346 SDRAM0_CFGADDR
= 0x010,
347 SDRAM0_CFGDATA
= 0x011,
350 /* XXX: TOFIX: some patches have made this code become inconsistent:
351 * there are type inconsistencies, mixing hwaddr, target_ulong
354 static uint32_t sdram_bcr (hwaddr ram_base
,
360 case (4 * 1024 * 1024):
363 case (8 * 1024 * 1024):
366 case (16 * 1024 * 1024):
369 case (32 * 1024 * 1024):
372 case (64 * 1024 * 1024):
375 case (128 * 1024 * 1024):
378 case (256 * 1024 * 1024):
382 printf("%s: invalid RAM size " TARGET_FMT_plx
"\n", __func__
,
386 bcr
|= ram_base
& 0xFF800000;
392 static inline hwaddr
sdram_base(uint32_t bcr
)
394 return bcr
& 0xFF800000;
397 static target_ulong
sdram_size (uint32_t bcr
)
402 sh
= (bcr
>> 17) & 0x7;
406 size
= (4 * 1024 * 1024) << sh
;
411 static void sdram_set_bcr(ppc4xx_sdram_t
*sdram
,
412 uint32_t *bcrp
, uint32_t bcr
, int enabled
)
414 unsigned n
= bcrp
- sdram
->bcr
;
416 if (*bcrp
& 0x00000001) {
419 printf("%s: unmap RAM area " TARGET_FMT_plx
" " TARGET_FMT_lx
"\n",
420 __func__
, sdram_base(*bcrp
), sdram_size(*bcrp
));
422 memory_region_del_subregion(get_system_memory(),
423 &sdram
->containers
[n
]);
424 memory_region_del_subregion(&sdram
->containers
[n
],
425 &sdram
->ram_memories
[n
]);
426 memory_region_destroy(&sdram
->containers
[n
]);
428 *bcrp
= bcr
& 0xFFDEE001;
429 if (enabled
&& (bcr
& 0x00000001)) {
431 printf("%s: Map RAM area " TARGET_FMT_plx
" " TARGET_FMT_lx
"\n",
432 __func__
, sdram_base(bcr
), sdram_size(bcr
));
434 memory_region_init(&sdram
->containers
[n
], "sdram-containers",
436 memory_region_add_subregion(&sdram
->containers
[n
], 0,
437 &sdram
->ram_memories
[n
]);
438 memory_region_add_subregion(get_system_memory(),
440 &sdram
->containers
[n
]);
444 static void sdram_map_bcr (ppc4xx_sdram_t
*sdram
)
448 for (i
= 0; i
< sdram
->nbanks
; i
++) {
449 if (sdram
->ram_sizes
[i
] != 0) {
452 sdram_bcr(sdram
->ram_bases
[i
], sdram
->ram_sizes
[i
]),
455 sdram_set_bcr(sdram
, &sdram
->bcr
[i
], 0x00000000, 0);
460 static void sdram_unmap_bcr (ppc4xx_sdram_t
*sdram
)
464 for (i
= 0; i
< sdram
->nbanks
; i
++) {
466 printf("%s: Unmap RAM area " TARGET_FMT_plx
" " TARGET_FMT_lx
"\n",
467 __func__
, sdram_base(sdram
->bcr
[i
]), sdram_size(sdram
->bcr
[i
]));
469 memory_region_del_subregion(get_system_memory(),
470 &sdram
->ram_memories
[i
]);
474 static uint32_t dcr_read_sdram (void *opaque
, int dcrn
)
476 ppc4xx_sdram_t
*sdram
;
485 switch (sdram
->addr
) {
486 case 0x00: /* SDRAM_BESR0 */
489 case 0x08: /* SDRAM_BESR1 */
492 case 0x10: /* SDRAM_BEAR */
495 case 0x20: /* SDRAM_CFG */
498 case 0x24: /* SDRAM_STATUS */
501 case 0x30: /* SDRAM_RTR */
504 case 0x34: /* SDRAM_PMIT */
507 case 0x40: /* SDRAM_B0CR */
510 case 0x44: /* SDRAM_B1CR */
513 case 0x48: /* SDRAM_B2CR */
516 case 0x4C: /* SDRAM_B3CR */
519 case 0x80: /* SDRAM_TR */
522 case 0x94: /* SDRAM_ECCCFG */
525 case 0x98: /* SDRAM_ECCESR */
534 /* Avoid gcc warning */
542 static void dcr_write_sdram (void *opaque
, int dcrn
, uint32_t val
)
544 ppc4xx_sdram_t
*sdram
;
552 switch (sdram
->addr
) {
553 case 0x00: /* SDRAM_BESR0 */
554 sdram
->besr0
&= ~val
;
556 case 0x08: /* SDRAM_BESR1 */
557 sdram
->besr1
&= ~val
;
559 case 0x10: /* SDRAM_BEAR */
562 case 0x20: /* SDRAM_CFG */
564 if (!(sdram
->cfg
& 0x80000000) && (val
& 0x80000000)) {
566 printf("%s: enable SDRAM controller\n", __func__
);
568 /* validate all RAM mappings */
569 sdram_map_bcr(sdram
);
570 sdram
->status
&= ~0x80000000;
571 } else if ((sdram
->cfg
& 0x80000000) && !(val
& 0x80000000)) {
573 printf("%s: disable SDRAM controller\n", __func__
);
575 /* invalidate all RAM mappings */
576 sdram_unmap_bcr(sdram
);
577 sdram
->status
|= 0x80000000;
579 if (!(sdram
->cfg
& 0x40000000) && (val
& 0x40000000))
580 sdram
->status
|= 0x40000000;
581 else if ((sdram
->cfg
& 0x40000000) && !(val
& 0x40000000))
582 sdram
->status
&= ~0x40000000;
585 case 0x24: /* SDRAM_STATUS */
586 /* Read-only register */
588 case 0x30: /* SDRAM_RTR */
589 sdram
->rtr
= val
& 0x3FF80000;
591 case 0x34: /* SDRAM_PMIT */
592 sdram
->pmit
= (val
& 0xF8000000) | 0x07C00000;
594 case 0x40: /* SDRAM_B0CR */
595 sdram_set_bcr(sdram
, &sdram
->bcr
[0], val
, sdram
->cfg
& 0x80000000);
597 case 0x44: /* SDRAM_B1CR */
598 sdram_set_bcr(sdram
, &sdram
->bcr
[1], val
, sdram
->cfg
& 0x80000000);
600 case 0x48: /* SDRAM_B2CR */
601 sdram_set_bcr(sdram
, &sdram
->bcr
[2], val
, sdram
->cfg
& 0x80000000);
603 case 0x4C: /* SDRAM_B3CR */
604 sdram_set_bcr(sdram
, &sdram
->bcr
[3], val
, sdram
->cfg
& 0x80000000);
606 case 0x80: /* SDRAM_TR */
607 sdram
->tr
= val
& 0x018FC01F;
609 case 0x94: /* SDRAM_ECCCFG */
610 sdram
->ecccfg
= val
& 0x00F00000;
612 case 0x98: /* SDRAM_ECCESR */
614 if (sdram
->eccesr
== 0 && val
!= 0)
615 qemu_irq_raise(sdram
->irq
);
616 else if (sdram
->eccesr
!= 0 && val
== 0)
617 qemu_irq_lower(sdram
->irq
);
627 static void sdram_reset (void *opaque
)
629 ppc4xx_sdram_t
*sdram
;
632 sdram
->addr
= 0x00000000;
633 sdram
->bear
= 0x00000000;
634 sdram
->besr0
= 0x00000000; /* No error */
635 sdram
->besr1
= 0x00000000; /* No error */
636 sdram
->cfg
= 0x00000000;
637 sdram
->ecccfg
= 0x00000000; /* No ECC */
638 sdram
->eccesr
= 0x00000000; /* No error */
639 sdram
->pmit
= 0x07C00000;
640 sdram
->rtr
= 0x05F00000;
641 sdram
->tr
= 0x00854009;
642 /* We pre-initialize RAM banks */
643 sdram
->status
= 0x00000000;
644 sdram
->cfg
= 0x00800000;
647 void ppc4xx_sdram_init (CPUPPCState
*env
, qemu_irq irq
, int nbanks
,
648 MemoryRegion
*ram_memories
,
653 ppc4xx_sdram_t
*sdram
;
655 sdram
= g_malloc0(sizeof(ppc4xx_sdram_t
));
657 sdram
->nbanks
= nbanks
;
658 sdram
->ram_memories
= ram_memories
;
659 memset(sdram
->ram_bases
, 0, 4 * sizeof(hwaddr
));
660 memcpy(sdram
->ram_bases
, ram_bases
,
661 nbanks
* sizeof(hwaddr
));
662 memset(sdram
->ram_sizes
, 0, 4 * sizeof(hwaddr
));
663 memcpy(sdram
->ram_sizes
, ram_sizes
,
664 nbanks
* sizeof(hwaddr
));
665 qemu_register_reset(&sdram_reset
, sdram
);
666 ppc_dcr_register(env
, SDRAM0_CFGADDR
,
667 sdram
, &dcr_read_sdram
, &dcr_write_sdram
);
668 ppc_dcr_register(env
, SDRAM0_CFGDATA
,
669 sdram
, &dcr_read_sdram
, &dcr_write_sdram
);
671 sdram_map_bcr(sdram
);
674 /* Fill in consecutive SDRAM banks with 'ram_size' bytes of memory.
676 * sdram_bank_sizes[] must be 0-terminated.
678 * The 4xx SDRAM controller supports a small number of banks, and each bank
679 * must be one of a small set of sizes. The number of banks and the supported
680 * sizes varies by SoC. */
681 ram_addr_t
ppc4xx_sdram_adjust(ram_addr_t ram_size
, int nr_banks
,
682 MemoryRegion ram_memories
[],
685 const unsigned int sdram_bank_sizes
[])
687 ram_addr_t size_left
= ram_size
;
692 for (i
= 0; i
< nr_banks
; i
++) {
693 for (j
= 0; sdram_bank_sizes
[j
] != 0; j
++) {
694 unsigned int bank_size
= sdram_bank_sizes
[j
];
696 if (bank_size
<= size_left
) {
698 snprintf(name
, sizeof(name
), "ppc4xx.sdram%d", i
);
699 memory_region_init_ram(&ram_memories
[i
], name
, bank_size
);
700 vmstate_register_ram_global(&ram_memories
[i
]);
702 ram_sizes
[i
] = bank_size
;
704 size_left
-= bank_size
;
710 /* No need to use the remaining banks. */
715 ram_size
-= size_left
;
717 printf("Truncating memory to %d MiB to fit SDRAM controller limits.\n",
718 (int)(ram_size
>> 20));