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"
27 #include "hw/boards.h"
29 #include "exec/address-spaces.h"
35 # define LOG_UIC(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
37 # define LOG_UIC(...) do { } while (0)
40 static void ppc4xx_reset(void *opaque
)
42 PowerPCCPU
*cpu
= opaque
;
47 /*****************************************************************************/
48 /* Generic PowerPC 4xx processor instantiation */
49 PowerPCCPU
*ppc4xx_init(const char *cpu_model
,
50 clk_setup_t
*cpu_clk
, clk_setup_t
*tb_clk
,
57 cpu
= cpu_ppc_init(cpu_model
);
59 fprintf(stderr
, "Unable to find PowerPC %s CPU definition\n",
65 cpu_clk
->cb
= NULL
; /* We don't care about CPU clock frequency changes */
66 cpu_clk
->opaque
= env
;
67 /* Set time-base frequency to sysclk */
68 tb_clk
->cb
= ppc_40x_timers_init(env
, sysclk
, PPC_INTERRUPT_PIT
);
70 ppc_dcr_init(env
, NULL
, NULL
);
71 /* Register qemu callbacks */
72 qemu_register_reset(ppc4xx_reset
, cpu
);
77 /*****************************************************************************/
78 /* "Universal" Interrupt controller */
92 #define UIC_MAX_IRQ 32
93 typedef struct ppcuic_t ppcuic_t
;
97 uint32_t level
; /* Remembers the state of level-triggered interrupts. */
98 uint32_t uicsr
; /* Status register */
99 uint32_t uicer
; /* Enable register */
100 uint32_t uiccr
; /* Critical register */
101 uint32_t uicpr
; /* Polarity register */
102 uint32_t uictr
; /* Triggering register */
103 uint32_t uicvcr
; /* Vector configuration register */
108 static void ppcuic_trigger_irq (ppcuic_t
*uic
)
111 int start
, end
, inc
, i
;
113 /* Trigger interrupt if any is pending */
114 ir
= uic
->uicsr
& uic
->uicer
& (~uic
->uiccr
);
115 cr
= uic
->uicsr
& uic
->uicer
& uic
->uiccr
;
116 LOG_UIC("%s: uicsr %08" PRIx32
" uicer %08" PRIx32
117 " uiccr %08" PRIx32
"\n"
118 " %08" PRIx32
" ir %08" PRIx32
" cr %08" PRIx32
"\n",
119 __func__
, uic
->uicsr
, uic
->uicer
, uic
->uiccr
,
120 uic
->uicsr
& uic
->uicer
, ir
, cr
);
121 if (ir
!= 0x0000000) {
122 LOG_UIC("Raise UIC interrupt\n");
123 qemu_irq_raise(uic
->irqs
[PPCUIC_OUTPUT_INT
]);
125 LOG_UIC("Lower UIC interrupt\n");
126 qemu_irq_lower(uic
->irqs
[PPCUIC_OUTPUT_INT
]);
128 /* Trigger critical interrupt if any is pending and update vector */
129 if (cr
!= 0x0000000) {
130 qemu_irq_raise(uic
->irqs
[PPCUIC_OUTPUT_CINT
]);
131 if (uic
->use_vectors
) {
132 /* Compute critical IRQ vector */
133 if (uic
->uicvcr
& 1) {
142 uic
->uicvr
= uic
->uicvcr
& 0xFFFFFFFC;
143 for (i
= start
; i
<= end
; i
+= inc
) {
145 uic
->uicvr
+= (i
- start
) * 512 * inc
;
150 LOG_UIC("Raise UIC critical interrupt - "
151 "vector %08" PRIx32
"\n", uic
->uicvr
);
153 LOG_UIC("Lower UIC critical interrupt\n");
154 qemu_irq_lower(uic
->irqs
[PPCUIC_OUTPUT_CINT
]);
155 uic
->uicvr
= 0x00000000;
159 static void ppcuic_set_irq (void *opaque
, int irq_num
, int level
)
165 mask
= 1U << (31-irq_num
);
166 LOG_UIC("%s: irq %d level %d uicsr %08" PRIx32
167 " mask %08" PRIx32
" => %08" PRIx32
" %08" PRIx32
"\n",
168 __func__
, irq_num
, level
,
169 uic
->uicsr
, mask
, uic
->uicsr
& mask
, level
<< irq_num
);
170 if (irq_num
< 0 || irq_num
> 31)
174 /* Update status register */
175 if (uic
->uictr
& mask
) {
176 /* Edge sensitive interrupt */
180 /* Level sensitive interrupt */
189 LOG_UIC("%s: irq %d level %d sr %" PRIx32
" => "
190 "%08" PRIx32
"\n", __func__
, irq_num
, level
, uic
->uicsr
, sr
);
191 if (sr
!= uic
->uicsr
)
192 ppcuic_trigger_irq(uic
);
195 static uint32_t dcr_read_uic (void *opaque
, int dcrn
)
201 dcrn
-= uic
->dcr_base
;
220 ret
= uic
->uicsr
& uic
->uicer
;
223 if (!uic
->use_vectors
)
228 if (!uic
->use_vectors
)
241 static void dcr_write_uic (void *opaque
, int dcrn
, uint32_t val
)
246 dcrn
-= uic
->dcr_base
;
247 LOG_UIC("%s: dcr %d val 0x%x\n", __func__
, dcrn
, val
);
251 uic
->uicsr
|= uic
->level
;
252 ppcuic_trigger_irq(uic
);
256 ppcuic_trigger_irq(uic
);
260 ppcuic_trigger_irq(uic
);
264 ppcuic_trigger_irq(uic
);
271 ppcuic_trigger_irq(uic
);
278 uic
->uicvcr
= val
& 0xFFFFFFFD;
279 ppcuic_trigger_irq(uic
);
284 static void ppcuic_reset (void *opaque
)
289 uic
->uiccr
= 0x00000000;
290 uic
->uicer
= 0x00000000;
291 uic
->uicpr
= 0x00000000;
292 uic
->uicsr
= 0x00000000;
293 uic
->uictr
= 0x00000000;
294 if (uic
->use_vectors
) {
295 uic
->uicvcr
= 0x00000000;
296 uic
->uicvr
= 0x0000000;
300 qemu_irq
*ppcuic_init (CPUPPCState
*env
, qemu_irq
*irqs
,
301 uint32_t dcr_base
, int has_ssr
, int has_vr
)
306 uic
= g_malloc0(sizeof(ppcuic_t
));
307 uic
->dcr_base
= dcr_base
;
310 uic
->use_vectors
= 1;
311 for (i
= 0; i
< DCR_UICMAX
; i
++) {
312 ppc_dcr_register(env
, dcr_base
+ i
, uic
,
313 &dcr_read_uic
, &dcr_write_uic
);
315 qemu_register_reset(ppcuic_reset
, uic
);
317 return qemu_allocate_irqs(&ppcuic_set_irq
, uic
, UIC_MAX_IRQ
);
320 /*****************************************************************************/
321 /* SDRAM controller */
322 typedef struct ppc4xx_sdram_t ppc4xx_sdram_t
;
323 struct ppc4xx_sdram_t
{
326 MemoryRegion containers
[4]; /* used for clipping */
327 MemoryRegion
*ram_memories
;
345 SDRAM0_CFGADDR
= 0x010,
346 SDRAM0_CFGDATA
= 0x011,
349 /* XXX: TOFIX: some patches have made this code become inconsistent:
350 * there are type inconsistencies, mixing hwaddr, target_ulong
353 static uint32_t sdram_bcr (hwaddr ram_base
,
359 case (4 * 1024 * 1024):
362 case (8 * 1024 * 1024):
365 case (16 * 1024 * 1024):
368 case (32 * 1024 * 1024):
371 case (64 * 1024 * 1024):
374 case (128 * 1024 * 1024):
377 case (256 * 1024 * 1024):
381 printf("%s: invalid RAM size " TARGET_FMT_plx
"\n", __func__
,
385 bcr
|= ram_base
& 0xFF800000;
391 static inline hwaddr
sdram_base(uint32_t bcr
)
393 return bcr
& 0xFF800000;
396 static target_ulong
sdram_size (uint32_t bcr
)
401 sh
= (bcr
>> 17) & 0x7;
405 size
= (4 * 1024 * 1024) << sh
;
410 static void sdram_set_bcr(ppc4xx_sdram_t
*sdram
,
411 uint32_t *bcrp
, uint32_t bcr
, int enabled
)
413 unsigned n
= bcrp
- sdram
->bcr
;
415 if (*bcrp
& 0x00000001) {
418 printf("%s: unmap RAM area " TARGET_FMT_plx
" " TARGET_FMT_lx
"\n",
419 __func__
, sdram_base(*bcrp
), sdram_size(*bcrp
));
421 memory_region_del_subregion(get_system_memory(),
422 &sdram
->containers
[n
]);
423 memory_region_del_subregion(&sdram
->containers
[n
],
424 &sdram
->ram_memories
[n
]);
425 object_unparent(OBJECT(&sdram
->containers
[n
]));
427 *bcrp
= bcr
& 0xFFDEE001;
428 if (enabled
&& (bcr
& 0x00000001)) {
430 printf("%s: Map RAM area " TARGET_FMT_plx
" " TARGET_FMT_lx
"\n",
431 __func__
, sdram_base(bcr
), sdram_size(bcr
));
433 memory_region_init(&sdram
->containers
[n
], NULL
, "sdram-containers",
435 memory_region_add_subregion(&sdram
->containers
[n
], 0,
436 &sdram
->ram_memories
[n
]);
437 memory_region_add_subregion(get_system_memory(),
439 &sdram
->containers
[n
]);
443 static void sdram_map_bcr (ppc4xx_sdram_t
*sdram
)
447 for (i
= 0; i
< sdram
->nbanks
; i
++) {
448 if (sdram
->ram_sizes
[i
] != 0) {
451 sdram_bcr(sdram
->ram_bases
[i
], sdram
->ram_sizes
[i
]),
454 sdram_set_bcr(sdram
, &sdram
->bcr
[i
], 0x00000000, 0);
459 static void sdram_unmap_bcr (ppc4xx_sdram_t
*sdram
)
463 for (i
= 0; i
< sdram
->nbanks
; i
++) {
465 printf("%s: Unmap RAM area " TARGET_FMT_plx
" " TARGET_FMT_lx
"\n",
466 __func__
, sdram_base(sdram
->bcr
[i
]), sdram_size(sdram
->bcr
[i
]));
468 memory_region_del_subregion(get_system_memory(),
469 &sdram
->ram_memories
[i
]);
473 static uint32_t dcr_read_sdram (void *opaque
, int dcrn
)
475 ppc4xx_sdram_t
*sdram
;
484 switch (sdram
->addr
) {
485 case 0x00: /* SDRAM_BESR0 */
488 case 0x08: /* SDRAM_BESR1 */
491 case 0x10: /* SDRAM_BEAR */
494 case 0x20: /* SDRAM_CFG */
497 case 0x24: /* SDRAM_STATUS */
500 case 0x30: /* SDRAM_RTR */
503 case 0x34: /* SDRAM_PMIT */
506 case 0x40: /* SDRAM_B0CR */
509 case 0x44: /* SDRAM_B1CR */
512 case 0x48: /* SDRAM_B2CR */
515 case 0x4C: /* SDRAM_B3CR */
518 case 0x80: /* SDRAM_TR */
521 case 0x94: /* SDRAM_ECCCFG */
524 case 0x98: /* SDRAM_ECCESR */
533 /* Avoid gcc warning */
541 static void dcr_write_sdram (void *opaque
, int dcrn
, uint32_t val
)
543 ppc4xx_sdram_t
*sdram
;
551 switch (sdram
->addr
) {
552 case 0x00: /* SDRAM_BESR0 */
553 sdram
->besr0
&= ~val
;
555 case 0x08: /* SDRAM_BESR1 */
556 sdram
->besr1
&= ~val
;
558 case 0x10: /* SDRAM_BEAR */
561 case 0x20: /* SDRAM_CFG */
563 if (!(sdram
->cfg
& 0x80000000) && (val
& 0x80000000)) {
565 printf("%s: enable SDRAM controller\n", __func__
);
567 /* validate all RAM mappings */
568 sdram_map_bcr(sdram
);
569 sdram
->status
&= ~0x80000000;
570 } else if ((sdram
->cfg
& 0x80000000) && !(val
& 0x80000000)) {
572 printf("%s: disable SDRAM controller\n", __func__
);
574 /* invalidate all RAM mappings */
575 sdram_unmap_bcr(sdram
);
576 sdram
->status
|= 0x80000000;
578 if (!(sdram
->cfg
& 0x40000000) && (val
& 0x40000000))
579 sdram
->status
|= 0x40000000;
580 else if ((sdram
->cfg
& 0x40000000) && !(val
& 0x40000000))
581 sdram
->status
&= ~0x40000000;
584 case 0x24: /* SDRAM_STATUS */
585 /* Read-only register */
587 case 0x30: /* SDRAM_RTR */
588 sdram
->rtr
= val
& 0x3FF80000;
590 case 0x34: /* SDRAM_PMIT */
591 sdram
->pmit
= (val
& 0xF8000000) | 0x07C00000;
593 case 0x40: /* SDRAM_B0CR */
594 sdram_set_bcr(sdram
, &sdram
->bcr
[0], val
, sdram
->cfg
& 0x80000000);
596 case 0x44: /* SDRAM_B1CR */
597 sdram_set_bcr(sdram
, &sdram
->bcr
[1], val
, sdram
->cfg
& 0x80000000);
599 case 0x48: /* SDRAM_B2CR */
600 sdram_set_bcr(sdram
, &sdram
->bcr
[2], val
, sdram
->cfg
& 0x80000000);
602 case 0x4C: /* SDRAM_B3CR */
603 sdram_set_bcr(sdram
, &sdram
->bcr
[3], val
, sdram
->cfg
& 0x80000000);
605 case 0x80: /* SDRAM_TR */
606 sdram
->tr
= val
& 0x018FC01F;
608 case 0x94: /* SDRAM_ECCCFG */
609 sdram
->ecccfg
= val
& 0x00F00000;
611 case 0x98: /* SDRAM_ECCESR */
613 if (sdram
->eccesr
== 0 && val
!= 0)
614 qemu_irq_raise(sdram
->irq
);
615 else if (sdram
->eccesr
!= 0 && val
== 0)
616 qemu_irq_lower(sdram
->irq
);
626 static void sdram_reset (void *opaque
)
628 ppc4xx_sdram_t
*sdram
;
631 sdram
->addr
= 0x00000000;
632 sdram
->bear
= 0x00000000;
633 sdram
->besr0
= 0x00000000; /* No error */
634 sdram
->besr1
= 0x00000000; /* No error */
635 sdram
->cfg
= 0x00000000;
636 sdram
->ecccfg
= 0x00000000; /* No ECC */
637 sdram
->eccesr
= 0x00000000; /* No error */
638 sdram
->pmit
= 0x07C00000;
639 sdram
->rtr
= 0x05F00000;
640 sdram
->tr
= 0x00854009;
641 /* We pre-initialize RAM banks */
642 sdram
->status
= 0x00000000;
643 sdram
->cfg
= 0x00800000;
646 void ppc4xx_sdram_init (CPUPPCState
*env
, qemu_irq irq
, int nbanks
,
647 MemoryRegion
*ram_memories
,
652 ppc4xx_sdram_t
*sdram
;
654 sdram
= g_malloc0(sizeof(ppc4xx_sdram_t
));
656 sdram
->nbanks
= nbanks
;
657 sdram
->ram_memories
= ram_memories
;
658 memset(sdram
->ram_bases
, 0, 4 * sizeof(hwaddr
));
659 memcpy(sdram
->ram_bases
, ram_bases
,
660 nbanks
* sizeof(hwaddr
));
661 memset(sdram
->ram_sizes
, 0, 4 * sizeof(hwaddr
));
662 memcpy(sdram
->ram_sizes
, ram_sizes
,
663 nbanks
* sizeof(hwaddr
));
664 qemu_register_reset(&sdram_reset
, sdram
);
665 ppc_dcr_register(env
, SDRAM0_CFGADDR
,
666 sdram
, &dcr_read_sdram
, &dcr_write_sdram
);
667 ppc_dcr_register(env
, SDRAM0_CFGDATA
,
668 sdram
, &dcr_read_sdram
, &dcr_write_sdram
);
670 sdram_map_bcr(sdram
);
673 /* Fill in consecutive SDRAM banks with 'ram_size' bytes of memory.
675 * sdram_bank_sizes[] must be 0-terminated.
677 * The 4xx SDRAM controller supports a small number of banks, and each bank
678 * must be one of a small set of sizes. The number of banks and the supported
679 * sizes varies by SoC. */
680 ram_addr_t
ppc4xx_sdram_adjust(ram_addr_t ram_size
, int nr_banks
,
681 MemoryRegion ram_memories
[],
684 const unsigned int sdram_bank_sizes
[])
686 MemoryRegion
*ram
= g_malloc0(sizeof(*ram
));
687 ram_addr_t size_left
= ram_size
;
689 unsigned int bank_size
;
693 for (i
= 0; i
< nr_banks
; i
++) {
694 for (j
= 0; sdram_bank_sizes
[j
] != 0; j
++) {
695 bank_size
= sdram_bank_sizes
[j
];
696 if (bank_size
<= size_left
) {
697 size_left
-= bank_size
;
701 /* No need to use the remaining banks. */
706 ram_size
-= size_left
;
708 printf("Truncating memory to %d MiB to fit SDRAM controller limits.\n",
709 (int)(ram_size
>> 20));
712 memory_region_allocate_system_memory(ram
, NULL
, "ppc4xx.sdram", ram_size
);
714 size_left
= ram_size
;
715 for (i
= 0; i
< nr_banks
&& size_left
; i
++) {
716 for (j
= 0; sdram_bank_sizes
[j
] != 0; j
++) {
717 bank_size
= sdram_bank_sizes
[j
];
719 if (bank_size
<= size_left
) {
721 snprintf(name
, sizeof(name
), "ppc4xx.sdram%d", i
);
722 memory_region_init_alias(&ram_memories
[i
], NULL
, name
, ram
,
725 ram_sizes
[i
] = bank_size
;
727 size_left
-= bank_size
;