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
31 //#define DEBUG_UNASSIGNED
36 # define LOG_UIC(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
38 # define LOG_UIC(...) do { } while (0)
41 /*****************************************************************************/
42 /* Generic PowerPC 4xx processor instanciation */
43 CPUState
*ppc4xx_init (const char *cpu_model
,
44 clk_setup_t
*cpu_clk
, clk_setup_t
*tb_clk
,
50 env
= cpu_init(cpu_model
);
52 fprintf(stderr
, "Unable to find PowerPC %s CPU definition\n",
56 cpu_clk
->cb
= NULL
; /* We don't care about CPU clock frequency changes */
57 cpu_clk
->opaque
= env
;
58 /* Set time-base frequency to sysclk */
59 tb_clk
->cb
= ppc_emb_timers_init(env
, sysclk
);
61 ppc_dcr_init(env
, NULL
, NULL
);
62 /* Register qemu callbacks */
63 qemu_register_reset((QEMUResetHandler
*)&cpu_reset
, env
);
68 /*****************************************************************************/
69 /* "Universal" Interrupt controller */
83 #define UIC_MAX_IRQ 32
84 typedef struct ppcuic_t ppcuic_t
;
88 uint32_t level
; /* Remembers the state of level-triggered interrupts. */
89 uint32_t uicsr
; /* Status register */
90 uint32_t uicer
; /* Enable register */
91 uint32_t uiccr
; /* Critical register */
92 uint32_t uicpr
; /* Polarity register */
93 uint32_t uictr
; /* Triggering register */
94 uint32_t uicvcr
; /* Vector configuration register */
99 static void ppcuic_trigger_irq (ppcuic_t
*uic
)
102 int start
, end
, inc
, i
;
104 /* Trigger interrupt if any is pending */
105 ir
= uic
->uicsr
& uic
->uicer
& (~uic
->uiccr
);
106 cr
= uic
->uicsr
& uic
->uicer
& uic
->uiccr
;
107 LOG_UIC("%s: uicsr %08" PRIx32
" uicer %08" PRIx32
108 " uiccr %08" PRIx32
"\n"
109 " %08" PRIx32
" ir %08" PRIx32
" cr %08" PRIx32
"\n",
110 __func__
, uic
->uicsr
, uic
->uicer
, uic
->uiccr
,
111 uic
->uicsr
& uic
->uicer
, ir
, cr
);
112 if (ir
!= 0x0000000) {
113 LOG_UIC("Raise UIC interrupt\n");
114 qemu_irq_raise(uic
->irqs
[PPCUIC_OUTPUT_INT
]);
116 LOG_UIC("Lower UIC interrupt\n");
117 qemu_irq_lower(uic
->irqs
[PPCUIC_OUTPUT_INT
]);
119 /* Trigger critical interrupt if any is pending and update vector */
120 if (cr
!= 0x0000000) {
121 qemu_irq_raise(uic
->irqs
[PPCUIC_OUTPUT_CINT
]);
122 if (uic
->use_vectors
) {
123 /* Compute critical IRQ vector */
124 if (uic
->uicvcr
& 1) {
133 uic
->uicvr
= uic
->uicvcr
& 0xFFFFFFFC;
134 for (i
= start
; i
<= end
; i
+= inc
) {
136 uic
->uicvr
+= (i
- start
) * 512 * inc
;
141 LOG_UIC("Raise UIC critical interrupt - "
142 "vector %08" PRIx32
"\n", uic
->uicvr
);
144 LOG_UIC("Lower UIC critical interrupt\n");
145 qemu_irq_lower(uic
->irqs
[PPCUIC_OUTPUT_CINT
]);
146 uic
->uicvr
= 0x00000000;
150 static void ppcuic_set_irq (void *opaque
, int irq_num
, int level
)
156 mask
= 1 << (31-irq_num
);
157 LOG_UIC("%s: irq %d level %d uicsr %08" PRIx32
158 " mask %08" PRIx32
" => %08" PRIx32
" %08" PRIx32
"\n",
159 __func__
, irq_num
, level
,
160 uic
->uicsr
, mask
, uic
->uicsr
& mask
, level
<< irq_num
);
161 if (irq_num
< 0 || irq_num
> 31)
165 /* Update status register */
166 if (uic
->uictr
& mask
) {
167 /* Edge sensitive interrupt */
171 /* Level sensitive interrupt */
180 LOG_UIC("%s: irq %d level %d sr %" PRIx32
" => "
181 "%08" PRIx32
"\n", __func__
, irq_num
, level
, uic
->uicsr
, sr
);
182 if (sr
!= uic
->uicsr
)
183 ppcuic_trigger_irq(uic
);
186 static uint32_t dcr_read_uic (void *opaque
, int dcrn
)
192 dcrn
-= uic
->dcr_base
;
211 ret
= uic
->uicsr
& uic
->uicer
;
214 if (!uic
->use_vectors
)
219 if (!uic
->use_vectors
)
232 static void dcr_write_uic (void *opaque
, int dcrn
, uint32_t val
)
237 dcrn
-= uic
->dcr_base
;
238 LOG_UIC("%s: dcr %d val 0x%x\n", __func__
, dcrn
, val
);
242 uic
->uicsr
|= uic
->level
;
243 ppcuic_trigger_irq(uic
);
247 ppcuic_trigger_irq(uic
);
251 ppcuic_trigger_irq(uic
);
255 ppcuic_trigger_irq(uic
);
262 ppcuic_trigger_irq(uic
);
269 uic
->uicvcr
= val
& 0xFFFFFFFD;
270 ppcuic_trigger_irq(uic
);
275 static void ppcuic_reset (void *opaque
)
280 uic
->uiccr
= 0x00000000;
281 uic
->uicer
= 0x00000000;
282 uic
->uicpr
= 0x00000000;
283 uic
->uicsr
= 0x00000000;
284 uic
->uictr
= 0x00000000;
285 if (uic
->use_vectors
) {
286 uic
->uicvcr
= 0x00000000;
287 uic
->uicvr
= 0x0000000;
291 qemu_irq
*ppcuic_init (CPUState
*env
, qemu_irq
*irqs
,
292 uint32_t dcr_base
, int has_ssr
, int has_vr
)
297 uic
= qemu_mallocz(sizeof(ppcuic_t
));
298 uic
->dcr_base
= dcr_base
;
301 uic
->use_vectors
= 1;
302 for (i
= 0; i
< DCR_UICMAX
; i
++) {
303 ppc_dcr_register(env
, dcr_base
+ i
, uic
,
304 &dcr_read_uic
, &dcr_write_uic
);
306 qemu_register_reset(ppcuic_reset
, uic
);
308 return qemu_allocate_irqs(&ppcuic_set_irq
, uic
, UIC_MAX_IRQ
);
311 /*****************************************************************************/
312 /* SDRAM controller */
313 typedef struct ppc4xx_sdram_t ppc4xx_sdram_t
;
314 struct ppc4xx_sdram_t
{
317 target_phys_addr_t ram_bases
[4];
318 target_phys_addr_t ram_sizes
[4];
334 SDRAM0_CFGADDR
= 0x010,
335 SDRAM0_CFGDATA
= 0x011,
338 /* XXX: TOFIX: some patches have made this code become inconsistent:
339 * there are type inconsistencies, mixing target_phys_addr_t, target_ulong
342 static uint32_t sdram_bcr (target_phys_addr_t ram_base
,
343 target_phys_addr_t ram_size
)
348 case (4 * 1024 * 1024):
351 case (8 * 1024 * 1024):
354 case (16 * 1024 * 1024):
357 case (32 * 1024 * 1024):
360 case (64 * 1024 * 1024):
363 case (128 * 1024 * 1024):
366 case (256 * 1024 * 1024):
370 printf("%s: invalid RAM size " TARGET_FMT_plx
"\n", __func__
,
374 bcr
|= ram_base
& 0xFF800000;
380 static inline target_phys_addr_t
sdram_base(uint32_t bcr
)
382 return bcr
& 0xFF800000;
385 static target_ulong
sdram_size (uint32_t bcr
)
390 sh
= (bcr
>> 17) & 0x7;
394 size
= (4 * 1024 * 1024) << sh
;
399 static void sdram_set_bcr (uint32_t *bcrp
, uint32_t bcr
, int enabled
)
401 if (*bcrp
& 0x00000001) {
404 printf("%s: unmap RAM area " TARGET_FMT_plx
" " TARGET_FMT_lx
"\n",
405 __func__
, sdram_base(*bcrp
), sdram_size(*bcrp
));
407 cpu_register_physical_memory(sdram_base(*bcrp
), sdram_size(*bcrp
),
410 *bcrp
= bcr
& 0xFFDEE001;
411 if (enabled
&& (bcr
& 0x00000001)) {
413 printf("%s: Map RAM area " TARGET_FMT_plx
" " TARGET_FMT_lx
"\n",
414 __func__
, sdram_base(bcr
), sdram_size(bcr
));
416 cpu_register_physical_memory(sdram_base(bcr
), sdram_size(bcr
),
417 sdram_base(bcr
) | IO_MEM_RAM
);
421 static void sdram_map_bcr (ppc4xx_sdram_t
*sdram
)
425 for (i
= 0; i
< sdram
->nbanks
; i
++) {
426 if (sdram
->ram_sizes
[i
] != 0) {
427 sdram_set_bcr(&sdram
->bcr
[i
],
428 sdram_bcr(sdram
->ram_bases
[i
], sdram
->ram_sizes
[i
]),
431 sdram_set_bcr(&sdram
->bcr
[i
], 0x00000000, 0);
436 static void sdram_unmap_bcr (ppc4xx_sdram_t
*sdram
)
440 for (i
= 0; i
< sdram
->nbanks
; i
++) {
442 printf("%s: Unmap RAM area " TARGET_FMT_plx
" " TARGET_FMT_lx
"\n",
443 __func__
, sdram_base(sdram
->bcr
[i
]), sdram_size(sdram
->bcr
[i
]));
445 cpu_register_physical_memory(sdram_base(sdram
->bcr
[i
]),
446 sdram_size(sdram
->bcr
[i
]),
451 static uint32_t dcr_read_sdram (void *opaque
, int dcrn
)
453 ppc4xx_sdram_t
*sdram
;
462 switch (sdram
->addr
) {
463 case 0x00: /* SDRAM_BESR0 */
466 case 0x08: /* SDRAM_BESR1 */
469 case 0x10: /* SDRAM_BEAR */
472 case 0x20: /* SDRAM_CFG */
475 case 0x24: /* SDRAM_STATUS */
478 case 0x30: /* SDRAM_RTR */
481 case 0x34: /* SDRAM_PMIT */
484 case 0x40: /* SDRAM_B0CR */
487 case 0x44: /* SDRAM_B1CR */
490 case 0x48: /* SDRAM_B2CR */
493 case 0x4C: /* SDRAM_B3CR */
496 case 0x80: /* SDRAM_TR */
499 case 0x94: /* SDRAM_ECCCFG */
502 case 0x98: /* SDRAM_ECCESR */
511 /* Avoid gcc warning */
519 static void dcr_write_sdram (void *opaque
, int dcrn
, uint32_t val
)
521 ppc4xx_sdram_t
*sdram
;
529 switch (sdram
->addr
) {
530 case 0x00: /* SDRAM_BESR0 */
531 sdram
->besr0
&= ~val
;
533 case 0x08: /* SDRAM_BESR1 */
534 sdram
->besr1
&= ~val
;
536 case 0x10: /* SDRAM_BEAR */
539 case 0x20: /* SDRAM_CFG */
541 if (!(sdram
->cfg
& 0x80000000) && (val
& 0x80000000)) {
543 printf("%s: enable SDRAM controller\n", __func__
);
545 /* validate all RAM mappings */
546 sdram_map_bcr(sdram
);
547 sdram
->status
&= ~0x80000000;
548 } else if ((sdram
->cfg
& 0x80000000) && !(val
& 0x80000000)) {
550 printf("%s: disable SDRAM controller\n", __func__
);
552 /* invalidate all RAM mappings */
553 sdram_unmap_bcr(sdram
);
554 sdram
->status
|= 0x80000000;
556 if (!(sdram
->cfg
& 0x40000000) && (val
& 0x40000000))
557 sdram
->status
|= 0x40000000;
558 else if ((sdram
->cfg
& 0x40000000) && !(val
& 0x40000000))
559 sdram
->status
&= ~0x40000000;
562 case 0x24: /* SDRAM_STATUS */
563 /* Read-only register */
565 case 0x30: /* SDRAM_RTR */
566 sdram
->rtr
= val
& 0x3FF80000;
568 case 0x34: /* SDRAM_PMIT */
569 sdram
->pmit
= (val
& 0xF8000000) | 0x07C00000;
571 case 0x40: /* SDRAM_B0CR */
572 sdram_set_bcr(&sdram
->bcr
[0], val
, sdram
->cfg
& 0x80000000);
574 case 0x44: /* SDRAM_B1CR */
575 sdram_set_bcr(&sdram
->bcr
[1], val
, sdram
->cfg
& 0x80000000);
577 case 0x48: /* SDRAM_B2CR */
578 sdram_set_bcr(&sdram
->bcr
[2], val
, sdram
->cfg
& 0x80000000);
580 case 0x4C: /* SDRAM_B3CR */
581 sdram_set_bcr(&sdram
->bcr
[3], val
, sdram
->cfg
& 0x80000000);
583 case 0x80: /* SDRAM_TR */
584 sdram
->tr
= val
& 0x018FC01F;
586 case 0x94: /* SDRAM_ECCCFG */
587 sdram
->ecccfg
= val
& 0x00F00000;
589 case 0x98: /* SDRAM_ECCESR */
591 if (sdram
->eccesr
== 0 && val
!= 0)
592 qemu_irq_raise(sdram
->irq
);
593 else if (sdram
->eccesr
!= 0 && val
== 0)
594 qemu_irq_lower(sdram
->irq
);
604 static void sdram_reset (void *opaque
)
606 ppc4xx_sdram_t
*sdram
;
609 sdram
->addr
= 0x00000000;
610 sdram
->bear
= 0x00000000;
611 sdram
->besr0
= 0x00000000; /* No error */
612 sdram
->besr1
= 0x00000000; /* No error */
613 sdram
->cfg
= 0x00000000;
614 sdram
->ecccfg
= 0x00000000; /* No ECC */
615 sdram
->eccesr
= 0x00000000; /* No error */
616 sdram
->pmit
= 0x07C00000;
617 sdram
->rtr
= 0x05F00000;
618 sdram
->tr
= 0x00854009;
619 /* We pre-initialize RAM banks */
620 sdram
->status
= 0x00000000;
621 sdram
->cfg
= 0x00800000;
624 void ppc4xx_sdram_init (CPUState
*env
, qemu_irq irq
, int nbanks
,
625 target_phys_addr_t
*ram_bases
,
626 target_phys_addr_t
*ram_sizes
,
629 ppc4xx_sdram_t
*sdram
;
631 sdram
= qemu_mallocz(sizeof(ppc4xx_sdram_t
));
633 sdram
->nbanks
= nbanks
;
634 memset(sdram
->ram_bases
, 0, 4 * sizeof(target_phys_addr_t
));
635 memcpy(sdram
->ram_bases
, ram_bases
,
636 nbanks
* sizeof(target_phys_addr_t
));
637 memset(sdram
->ram_sizes
, 0, 4 * sizeof(target_phys_addr_t
));
638 memcpy(sdram
->ram_sizes
, ram_sizes
,
639 nbanks
* sizeof(target_phys_addr_t
));
640 qemu_register_reset(&sdram_reset
, sdram
);
641 ppc_dcr_register(env
, SDRAM0_CFGADDR
,
642 sdram
, &dcr_read_sdram
, &dcr_write_sdram
);
643 ppc_dcr_register(env
, SDRAM0_CFGDATA
,
644 sdram
, &dcr_read_sdram
, &dcr_write_sdram
);
646 sdram_map_bcr(sdram
);
649 /* Fill in consecutive SDRAM banks with 'ram_size' bytes of memory.
651 * sdram_bank_sizes[] must be 0-terminated.
653 * The 4xx SDRAM controller supports a small number of banks, and each bank
654 * must be one of a small set of sizes. The number of banks and the supported
655 * sizes varies by SoC. */
656 ram_addr_t
ppc4xx_sdram_adjust(ram_addr_t ram_size
, int nr_banks
,
657 target_phys_addr_t ram_bases
[],
658 target_phys_addr_t ram_sizes
[],
659 const unsigned int sdram_bank_sizes
[])
661 ram_addr_t size_left
= ram_size
;
665 for (i
= 0; i
< nr_banks
; i
++) {
666 for (j
= 0; sdram_bank_sizes
[j
] != 0; j
++) {
667 unsigned int bank_size
= sdram_bank_sizes
[j
];
669 if (bank_size
<= size_left
) {
671 snprintf(name
, sizeof(name
), "ppc4xx.sdram%d", i
);
672 ram_bases
[i
] = qemu_ram_alloc(NULL
, name
, bank_size
);
673 ram_sizes
[i
] = bank_size
;
674 size_left
-= bank_size
;
680 /* No need to use the remaining banks. */
685 ram_size
-= size_left
;
687 printf("Truncating memory to %d MiB to fit SDRAM controller limits.\n",
688 (int)(ram_size
>> 20));