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 "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "sysemu/reset.h"
30 #include "hw/ppc/ppc.h"
31 #include "hw/ppc/ppc4xx.h"
32 #include "hw/intc/ppc-uic.h"
33 #include "hw/qdev-properties.h"
35 #include "exec/address-spaces.h"
36 #include "qemu/error-report.h"
37 #include "qapi/error.h"
42 # define LOG_UIC(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
44 # define LOG_UIC(...) do { } while (0)
47 static void ppc4xx_reset(void *opaque
)
49 PowerPCCPU
*cpu
= opaque
;
54 /*****************************************************************************/
55 /* Generic PowerPC 4xx processor instantiation */
56 PowerPCCPU
*ppc4xx_init(const char *cpu_type
,
57 clk_setup_t
*cpu_clk
, clk_setup_t
*tb_clk
,
64 cpu
= POWERPC_CPU(cpu_create(cpu_type
));
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 /* SDRAM controller */
81 typedef struct ppc4xx_sdram_t ppc4xx_sdram_t
;
82 struct ppc4xx_sdram_t
{
85 MemoryRegion containers
[4]; /* used for clipping */
86 MemoryRegion
*ram_memories
;
104 SDRAM0_CFGADDR
= 0x010,
105 SDRAM0_CFGDATA
= 0x011,
108 /* XXX: TOFIX: some patches have made this code become inconsistent:
109 * there are type inconsistencies, mixing hwaddr, target_ulong
112 static uint32_t sdram_bcr (hwaddr ram_base
,
140 printf("%s: invalid RAM size " TARGET_FMT_plx
"\n", __func__
,
144 bcr
|= ram_base
& 0xFF800000;
150 static inline hwaddr
sdram_base(uint32_t bcr
)
152 return bcr
& 0xFF800000;
155 static target_ulong
sdram_size (uint32_t bcr
)
160 sh
= (bcr
>> 17) & 0x7;
164 size
= (4 * MiB
) << sh
;
169 static void sdram_set_bcr(ppc4xx_sdram_t
*sdram
, int i
,
170 uint32_t bcr
, int enabled
)
172 if (sdram
->bcr
[i
] & 0x00000001) {
175 printf("%s: unmap RAM area " TARGET_FMT_plx
" " TARGET_FMT_lx
"\n",
176 __func__
, sdram_base(sdram
->bcr
[i
]), sdram_size(sdram
->bcr
[i
]));
178 memory_region_del_subregion(get_system_memory(),
179 &sdram
->containers
[i
]);
180 memory_region_del_subregion(&sdram
->containers
[i
],
181 &sdram
->ram_memories
[i
]);
182 object_unparent(OBJECT(&sdram
->containers
[i
]));
184 sdram
->bcr
[i
] = bcr
& 0xFFDEE001;
185 if (enabled
&& (bcr
& 0x00000001)) {
187 printf("%s: Map RAM area " TARGET_FMT_plx
" " TARGET_FMT_lx
"\n",
188 __func__
, sdram_base(bcr
), sdram_size(bcr
));
190 memory_region_init(&sdram
->containers
[i
], NULL
, "sdram-containers",
192 memory_region_add_subregion(&sdram
->containers
[i
], 0,
193 &sdram
->ram_memories
[i
]);
194 memory_region_add_subregion(get_system_memory(),
196 &sdram
->containers
[i
]);
200 static void sdram_map_bcr (ppc4xx_sdram_t
*sdram
)
204 for (i
= 0; i
< sdram
->nbanks
; i
++) {
205 if (sdram
->ram_sizes
[i
] != 0) {
206 sdram_set_bcr(sdram
, i
, sdram_bcr(sdram
->ram_bases
[i
],
207 sdram
->ram_sizes
[i
]), 1);
209 sdram_set_bcr(sdram
, i
, 0x00000000, 0);
214 static void sdram_unmap_bcr (ppc4xx_sdram_t
*sdram
)
218 for (i
= 0; i
< sdram
->nbanks
; i
++) {
220 printf("%s: Unmap RAM area " TARGET_FMT_plx
" " TARGET_FMT_lx
"\n",
221 __func__
, sdram_base(sdram
->bcr
[i
]), sdram_size(sdram
->bcr
[i
]));
223 memory_region_del_subregion(get_system_memory(),
224 &sdram
->ram_memories
[i
]);
228 static uint32_t dcr_read_sdram (void *opaque
, int dcrn
)
230 ppc4xx_sdram_t
*sdram
;
239 switch (sdram
->addr
) {
240 case 0x00: /* SDRAM_BESR0 */
243 case 0x08: /* SDRAM_BESR1 */
246 case 0x10: /* SDRAM_BEAR */
249 case 0x20: /* SDRAM_CFG */
252 case 0x24: /* SDRAM_STATUS */
255 case 0x30: /* SDRAM_RTR */
258 case 0x34: /* SDRAM_PMIT */
261 case 0x40: /* SDRAM_B0CR */
264 case 0x44: /* SDRAM_B1CR */
267 case 0x48: /* SDRAM_B2CR */
270 case 0x4C: /* SDRAM_B3CR */
273 case 0x80: /* SDRAM_TR */
276 case 0x94: /* SDRAM_ECCCFG */
279 case 0x98: /* SDRAM_ECCESR */
288 /* Avoid gcc warning */
296 static void dcr_write_sdram (void *opaque
, int dcrn
, uint32_t val
)
298 ppc4xx_sdram_t
*sdram
;
306 switch (sdram
->addr
) {
307 case 0x00: /* SDRAM_BESR0 */
308 sdram
->besr0
&= ~val
;
310 case 0x08: /* SDRAM_BESR1 */
311 sdram
->besr1
&= ~val
;
313 case 0x10: /* SDRAM_BEAR */
316 case 0x20: /* SDRAM_CFG */
318 if (!(sdram
->cfg
& 0x80000000) && (val
& 0x80000000)) {
320 printf("%s: enable SDRAM controller\n", __func__
);
322 /* validate all RAM mappings */
323 sdram_map_bcr(sdram
);
324 sdram
->status
&= ~0x80000000;
325 } else if ((sdram
->cfg
& 0x80000000) && !(val
& 0x80000000)) {
327 printf("%s: disable SDRAM controller\n", __func__
);
329 /* invalidate all RAM mappings */
330 sdram_unmap_bcr(sdram
);
331 sdram
->status
|= 0x80000000;
333 if (!(sdram
->cfg
& 0x40000000) && (val
& 0x40000000))
334 sdram
->status
|= 0x40000000;
335 else if ((sdram
->cfg
& 0x40000000) && !(val
& 0x40000000))
336 sdram
->status
&= ~0x40000000;
339 case 0x24: /* SDRAM_STATUS */
340 /* Read-only register */
342 case 0x30: /* SDRAM_RTR */
343 sdram
->rtr
= val
& 0x3FF80000;
345 case 0x34: /* SDRAM_PMIT */
346 sdram
->pmit
= (val
& 0xF8000000) | 0x07C00000;
348 case 0x40: /* SDRAM_B0CR */
349 sdram_set_bcr(sdram
, 0, val
, sdram
->cfg
& 0x80000000);
351 case 0x44: /* SDRAM_B1CR */
352 sdram_set_bcr(sdram
, 1, val
, sdram
->cfg
& 0x80000000);
354 case 0x48: /* SDRAM_B2CR */
355 sdram_set_bcr(sdram
, 2, val
, sdram
->cfg
& 0x80000000);
357 case 0x4C: /* SDRAM_B3CR */
358 sdram_set_bcr(sdram
, 3, val
, sdram
->cfg
& 0x80000000);
360 case 0x80: /* SDRAM_TR */
361 sdram
->tr
= val
& 0x018FC01F;
363 case 0x94: /* SDRAM_ECCCFG */
364 sdram
->ecccfg
= val
& 0x00F00000;
366 case 0x98: /* SDRAM_ECCESR */
368 if (sdram
->eccesr
== 0 && val
!= 0)
369 qemu_irq_raise(sdram
->irq
);
370 else if (sdram
->eccesr
!= 0 && val
== 0)
371 qemu_irq_lower(sdram
->irq
);
381 static void sdram_reset (void *opaque
)
383 ppc4xx_sdram_t
*sdram
;
386 sdram
->addr
= 0x00000000;
387 sdram
->bear
= 0x00000000;
388 sdram
->besr0
= 0x00000000; /* No error */
389 sdram
->besr1
= 0x00000000; /* No error */
390 sdram
->cfg
= 0x00000000;
391 sdram
->ecccfg
= 0x00000000; /* No ECC */
392 sdram
->eccesr
= 0x00000000; /* No error */
393 sdram
->pmit
= 0x07C00000;
394 sdram
->rtr
= 0x05F00000;
395 sdram
->tr
= 0x00854009;
396 /* We pre-initialize RAM banks */
397 sdram
->status
= 0x00000000;
398 sdram
->cfg
= 0x00800000;
401 void ppc4xx_sdram_init (CPUPPCState
*env
, qemu_irq irq
, int nbanks
,
402 MemoryRegion
*ram_memories
,
407 ppc4xx_sdram_t
*sdram
;
409 sdram
= g_malloc0(sizeof(ppc4xx_sdram_t
));
411 sdram
->nbanks
= nbanks
;
412 sdram
->ram_memories
= ram_memories
;
413 memset(sdram
->ram_bases
, 0, 4 * sizeof(hwaddr
));
414 memcpy(sdram
->ram_bases
, ram_bases
,
415 nbanks
* sizeof(hwaddr
));
416 memset(sdram
->ram_sizes
, 0, 4 * sizeof(hwaddr
));
417 memcpy(sdram
->ram_sizes
, ram_sizes
,
418 nbanks
* sizeof(hwaddr
));
419 qemu_register_reset(&sdram_reset
, sdram
);
420 ppc_dcr_register(env
, SDRAM0_CFGADDR
,
421 sdram
, &dcr_read_sdram
, &dcr_write_sdram
);
422 ppc_dcr_register(env
, SDRAM0_CFGDATA
,
423 sdram
, &dcr_read_sdram
, &dcr_write_sdram
);
425 sdram_map_bcr(sdram
);
429 * Split RAM between SDRAM banks.
431 * sdram_bank_sizes[] must be in descending order, that is sizes[i] > sizes[i+1]
432 * and must be 0-terminated.
434 * The 4xx SDRAM controller supports a small number of banks, and each bank
435 * must be one of a small set of sizes. The number of banks and the supported
436 * sizes varies by SoC.
438 void ppc4xx_sdram_banks(MemoryRegion
*ram
, int nr_banks
,
439 MemoryRegion ram_memories
[],
440 hwaddr ram_bases
[], hwaddr ram_sizes
[],
441 const ram_addr_t sdram_bank_sizes
[])
443 ram_addr_t size_left
= memory_region_size(ram
);
445 ram_addr_t bank_size
;
449 for (i
= 0; i
< nr_banks
; i
++) {
450 for (j
= 0; sdram_bank_sizes
[j
] != 0; j
++) {
451 bank_size
= sdram_bank_sizes
[j
];
452 if (bank_size
<= size_left
) {
456 ram_sizes
[i
] = bank_size
;
458 size_left
-= bank_size
;
459 snprintf(name
, sizeof(name
), "ppc4xx.sdram%d", i
);
460 memory_region_init_alias(&ram_memories
[i
], NULL
, name
, ram
,
461 ram_bases
[i
], ram_sizes
[i
]);
466 /* No need to use the remaining banks. */
472 ram_addr_t used_size
= memory_region_size(ram
) - size_left
;
473 GString
*s
= g_string_new(NULL
);
475 for (i
= 0; sdram_bank_sizes
[i
]; i
++) {
476 g_string_append_printf(s
, "%" PRIi64
"%s",
477 sdram_bank_sizes
[i
] / MiB
,
478 sdram_bank_sizes
[i
+ 1] ? ", " : "");
480 error_report("at most %d bank%s of %s MiB each supported",
481 nr_banks
, nr_banks
== 1 ? "" : "s", s
->str
);
482 error_printf("Possible valid RAM size: %" PRIi64
" MiB \n",
483 used_size
? used_size
/ MiB
: sdram_bank_sizes
[i
- 1] / MiB
);
485 g_string_free(s
, true);
490 /*****************************************************************************/
499 MAL0_TXEOBISR
= 0x186,
503 MAL0_RXEOBISR
= 0x192,
505 MAL0_TXCTP0R
= 0x1A0,
506 MAL0_RXCTP0R
= 0x1C0,
511 typedef struct ppc4xx_mal_t ppc4xx_mal_t
;
512 struct ppc4xx_mal_t
{
532 static void ppc4xx_mal_reset(void *opaque
)
537 mal
->cfg
= 0x0007C000;
538 mal
->esr
= 0x00000000;
539 mal
->ier
= 0x00000000;
540 mal
->rxcasr
= 0x00000000;
541 mal
->rxdeir
= 0x00000000;
542 mal
->rxeobisr
= 0x00000000;
543 mal
->txcasr
= 0x00000000;
544 mal
->txdeir
= 0x00000000;
545 mal
->txeobisr
= 0x00000000;
548 static uint32_t dcr_read_mal(void *opaque
, int dcrn
)
592 if (dcrn
>= MAL0_TXCTP0R
&& dcrn
< MAL0_TXCTP0R
+ mal
->txcnum
) {
593 ret
= mal
->txctpr
[dcrn
- MAL0_TXCTP0R
];
595 if (dcrn
>= MAL0_RXCTP0R
&& dcrn
< MAL0_RXCTP0R
+ mal
->rxcnum
) {
596 ret
= mal
->rxctpr
[dcrn
- MAL0_RXCTP0R
];
598 if (dcrn
>= MAL0_RCBS0
&& dcrn
< MAL0_RCBS0
+ mal
->rxcnum
) {
599 ret
= mal
->rcbs
[dcrn
- MAL0_RCBS0
];
605 static void dcr_write_mal(void *opaque
, int dcrn
, uint32_t val
)
612 if (val
& 0x80000000) {
613 ppc4xx_mal_reset(mal
);
615 mal
->cfg
= val
& 0x00FFC087;
622 mal
->ier
= val
& 0x0000001F;
625 mal
->txcasr
= val
& 0xF0000000;
628 mal
->txcarr
= val
& 0xF0000000;
632 mal
->txeobisr
&= ~val
;
639 mal
->rxcasr
= val
& 0xC0000000;
642 mal
->rxcarr
= val
& 0xC0000000;
646 mal
->rxeobisr
&= ~val
;
653 if (dcrn
>= MAL0_TXCTP0R
&& dcrn
< MAL0_TXCTP0R
+ mal
->txcnum
) {
654 mal
->txctpr
[dcrn
- MAL0_TXCTP0R
] = val
;
656 if (dcrn
>= MAL0_RXCTP0R
&& dcrn
< MAL0_RXCTP0R
+ mal
->rxcnum
) {
657 mal
->rxctpr
[dcrn
- MAL0_RXCTP0R
] = val
;
659 if (dcrn
>= MAL0_RCBS0
&& dcrn
< MAL0_RCBS0
+ mal
->rxcnum
) {
660 mal
->rcbs
[dcrn
- MAL0_RCBS0
] = val
& 0x000000FF;
664 void ppc4xx_mal_init(CPUPPCState
*env
, uint8_t txcnum
, uint8_t rxcnum
,
670 assert(txcnum
<= 32 && rxcnum
<= 32);
671 mal
= g_malloc0(sizeof(*mal
));
672 mal
->txcnum
= txcnum
;
673 mal
->rxcnum
= rxcnum
;
674 mal
->txctpr
= g_new0(uint32_t, txcnum
);
675 mal
->rxctpr
= g_new0(uint32_t, rxcnum
);
676 mal
->rcbs
= g_new0(uint32_t, rxcnum
);
677 for (i
= 0; i
< 4; i
++) {
678 mal
->irqs
[i
] = irqs
[i
];
680 qemu_register_reset(&ppc4xx_mal_reset
, mal
);
681 ppc_dcr_register(env
, MAL0_CFG
,
682 mal
, &dcr_read_mal
, &dcr_write_mal
);
683 ppc_dcr_register(env
, MAL0_ESR
,
684 mal
, &dcr_read_mal
, &dcr_write_mal
);
685 ppc_dcr_register(env
, MAL0_IER
,
686 mal
, &dcr_read_mal
, &dcr_write_mal
);
687 ppc_dcr_register(env
, MAL0_TXCASR
,
688 mal
, &dcr_read_mal
, &dcr_write_mal
);
689 ppc_dcr_register(env
, MAL0_TXCARR
,
690 mal
, &dcr_read_mal
, &dcr_write_mal
);
691 ppc_dcr_register(env
, MAL0_TXEOBISR
,
692 mal
, &dcr_read_mal
, &dcr_write_mal
);
693 ppc_dcr_register(env
, MAL0_TXDEIR
,
694 mal
, &dcr_read_mal
, &dcr_write_mal
);
695 ppc_dcr_register(env
, MAL0_RXCASR
,
696 mal
, &dcr_read_mal
, &dcr_write_mal
);
697 ppc_dcr_register(env
, MAL0_RXCARR
,
698 mal
, &dcr_read_mal
, &dcr_write_mal
);
699 ppc_dcr_register(env
, MAL0_RXEOBISR
,
700 mal
, &dcr_read_mal
, &dcr_write_mal
);
701 ppc_dcr_register(env
, MAL0_RXDEIR
,
702 mal
, &dcr_read_mal
, &dcr_write_mal
);
703 for (i
= 0; i
< txcnum
; i
++) {
704 ppc_dcr_register(env
, MAL0_TXCTP0R
+ i
,
705 mal
, &dcr_read_mal
, &dcr_write_mal
);
707 for (i
= 0; i
< rxcnum
; i
++) {
708 ppc_dcr_register(env
, MAL0_RXCTP0R
+ i
,
709 mal
, &dcr_read_mal
, &dcr_write_mal
);
711 for (i
= 0; i
< rxcnum
; i
++) {
712 ppc_dcr_register(env
, MAL0_RCBS0
+ i
,
713 mal
, &dcr_read_mal
, &dcr_write_mal
);