2 * QEMU PowerPC 405 embedded processors 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"
25 #include "qapi/error.h"
26 #include "qemu-common.h"
29 #include "hw/ppc/ppc.h"
30 #include "hw/boards.h"
32 #include "hw/char/serial.h"
33 #include "qemu/timer.h"
34 #include "sysemu/sysemu.h"
36 #include "exec/address-spaces.h"
41 //#define DEBUG_SERIAL
46 //#define DEBUG_CLOCKS
47 //#define DEBUG_CLOCKS_LL
49 ram_addr_t
ppc405_set_bootinfo (CPUPPCState
*env
, ppc4xx_bd_info_t
*bd
,
52 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
56 /* We put the bd structure at the top of memory */
57 if (bd
->bi_memsize
>= 0x01000000UL
)
58 bdloc
= 0x01000000UL
- sizeof(struct ppc4xx_bd_info_t
);
60 bdloc
= bd
->bi_memsize
- sizeof(struct ppc4xx_bd_info_t
);
61 stl_be_phys(cs
->as
, bdloc
+ 0x00, bd
->bi_memstart
);
62 stl_be_phys(cs
->as
, bdloc
+ 0x04, bd
->bi_memsize
);
63 stl_be_phys(cs
->as
, bdloc
+ 0x08, bd
->bi_flashstart
);
64 stl_be_phys(cs
->as
, bdloc
+ 0x0C, bd
->bi_flashsize
);
65 stl_be_phys(cs
->as
, bdloc
+ 0x10, bd
->bi_flashoffset
);
66 stl_be_phys(cs
->as
, bdloc
+ 0x14, bd
->bi_sramstart
);
67 stl_be_phys(cs
->as
, bdloc
+ 0x18, bd
->bi_sramsize
);
68 stl_be_phys(cs
->as
, bdloc
+ 0x1C, bd
->bi_bootflags
);
69 stl_be_phys(cs
->as
, bdloc
+ 0x20, bd
->bi_ipaddr
);
70 for (i
= 0; i
< 6; i
++) {
71 stb_phys(cs
->as
, bdloc
+ 0x24 + i
, bd
->bi_enetaddr
[i
]);
73 stw_be_phys(cs
->as
, bdloc
+ 0x2A, bd
->bi_ethspeed
);
74 stl_be_phys(cs
->as
, bdloc
+ 0x2C, bd
->bi_intfreq
);
75 stl_be_phys(cs
->as
, bdloc
+ 0x30, bd
->bi_busfreq
);
76 stl_be_phys(cs
->as
, bdloc
+ 0x34, bd
->bi_baudrate
);
77 for (i
= 0; i
< 4; i
++) {
78 stb_phys(cs
->as
, bdloc
+ 0x38 + i
, bd
->bi_s_version
[i
]);
80 for (i
= 0; i
< 32; i
++) {
81 stb_phys(cs
->as
, bdloc
+ 0x3C + i
, bd
->bi_r_version
[i
]);
83 stl_be_phys(cs
->as
, bdloc
+ 0x5C, bd
->bi_plb_busfreq
);
84 stl_be_phys(cs
->as
, bdloc
+ 0x60, bd
->bi_pci_busfreq
);
85 for (i
= 0; i
< 6; i
++) {
86 stb_phys(cs
->as
, bdloc
+ 0x64 + i
, bd
->bi_pci_enetaddr
[i
]);
89 if (flags
& 0x00000001) {
90 for (i
= 0; i
< 6; i
++)
91 stb_phys(cs
->as
, bdloc
+ n
++, bd
->bi_pci_enetaddr2
[i
]);
93 stl_be_phys(cs
->as
, bdloc
+ n
, bd
->bi_opbfreq
);
95 for (i
= 0; i
< 2; i
++) {
96 stl_be_phys(cs
->as
, bdloc
+ n
, bd
->bi_iic_fast
[i
]);
103 /*****************************************************************************/
104 /* Shared peripherals */
106 /*****************************************************************************/
107 /* Peripheral local bus arbitrer */
114 typedef struct ppc4xx_plb_t ppc4xx_plb_t
;
115 struct ppc4xx_plb_t
{
121 static uint32_t dcr_read_plb (void *opaque
, int dcrn
)
138 /* Avoid gcc warning */
146 static void dcr_write_plb (void *opaque
, int dcrn
, uint32_t val
)
153 /* We don't care about the actual parameters written as
154 * we don't manage any priorities on the bus
156 plb
->acr
= val
& 0xF8000000;
168 static void ppc4xx_plb_reset (void *opaque
)
173 plb
->acr
= 0x00000000;
174 plb
->bear
= 0x00000000;
175 plb
->besr
= 0x00000000;
178 static void ppc4xx_plb_init(CPUPPCState
*env
)
182 plb
= g_malloc0(sizeof(ppc4xx_plb_t
));
183 ppc_dcr_register(env
, PLB0_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
184 ppc_dcr_register(env
, PLB0_BEAR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
185 ppc_dcr_register(env
, PLB0_BESR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
186 qemu_register_reset(ppc4xx_plb_reset
, plb
);
189 /*****************************************************************************/
190 /* PLB to OPB bridge */
197 typedef struct ppc4xx_pob_t ppc4xx_pob_t
;
198 struct ppc4xx_pob_t
{
204 static uint32_t dcr_read_pob (void *opaque
, int dcrn
)
221 /* Avoid gcc warning */
229 static void dcr_write_pob (void *opaque
, int dcrn
, uint32_t val
)
249 static void ppc4xx_pob_reset (void *opaque
)
255 pob
->bear
= 0x00000000;
256 pob
->besr0
= 0x0000000;
257 pob
->besr1
= 0x0000000;
260 static void ppc4xx_pob_init(CPUPPCState
*env
)
264 pob
= g_malloc0(sizeof(ppc4xx_pob_t
));
265 ppc_dcr_register(env
, POB0_BEAR
, pob
, &dcr_read_pob
, &dcr_write_pob
);
266 ppc_dcr_register(env
, POB0_BESR0
, pob
, &dcr_read_pob
, &dcr_write_pob
);
267 ppc_dcr_register(env
, POB0_BESR1
, pob
, &dcr_read_pob
, &dcr_write_pob
);
268 qemu_register_reset(ppc4xx_pob_reset
, pob
);
271 /*****************************************************************************/
273 typedef struct ppc4xx_opba_t ppc4xx_opba_t
;
274 struct ppc4xx_opba_t
{
280 static uint32_t opba_readb (void *opaque
, hwaddr addr
)
286 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
304 static void opba_writeb (void *opaque
,
305 hwaddr addr
, uint32_t value
)
310 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
316 opba
->cr
= value
& 0xF8;
319 opba
->pr
= value
& 0xFF;
326 static uint32_t opba_readw (void *opaque
, hwaddr addr
)
331 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
333 ret
= opba_readb(opaque
, addr
) << 8;
334 ret
|= opba_readb(opaque
, addr
+ 1);
339 static void opba_writew (void *opaque
,
340 hwaddr addr
, uint32_t value
)
343 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
346 opba_writeb(opaque
, addr
, value
>> 8);
347 opba_writeb(opaque
, addr
+ 1, value
);
350 static uint32_t opba_readl (void *opaque
, hwaddr addr
)
355 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
357 ret
= opba_readb(opaque
, addr
) << 24;
358 ret
|= opba_readb(opaque
, addr
+ 1) << 16;
363 static void opba_writel (void *opaque
,
364 hwaddr addr
, uint32_t value
)
367 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
370 opba_writeb(opaque
, addr
, value
>> 24);
371 opba_writeb(opaque
, addr
+ 1, value
>> 16);
374 static const MemoryRegionOps opba_ops
= {
376 .read
= { opba_readb
, opba_readw
, opba_readl
, },
377 .write
= { opba_writeb
, opba_writew
, opba_writel
, },
379 .endianness
= DEVICE_NATIVE_ENDIAN
,
382 static void ppc4xx_opba_reset (void *opaque
)
387 opba
->cr
= 0x00; /* No dynamic priorities - park disabled */
391 static void ppc4xx_opba_init(hwaddr base
)
395 opba
= g_malloc0(sizeof(ppc4xx_opba_t
));
397 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
399 memory_region_init_io(&opba
->io
, NULL
, &opba_ops
, opba
, "opba", 0x002);
400 memory_region_add_subregion(get_system_memory(), base
, &opba
->io
);
401 qemu_register_reset(ppc4xx_opba_reset
, opba
);
404 /*****************************************************************************/
405 /* Code decompression controller */
408 /*****************************************************************************/
409 /* Peripheral controller */
410 typedef struct ppc4xx_ebc_t ppc4xx_ebc_t
;
411 struct ppc4xx_ebc_t
{
422 EBC0_CFGADDR
= 0x012,
423 EBC0_CFGDATA
= 0x013,
426 static uint32_t dcr_read_ebc (void *opaque
, int dcrn
)
438 case 0x00: /* B0CR */
441 case 0x01: /* B1CR */
444 case 0x02: /* B2CR */
447 case 0x03: /* B3CR */
450 case 0x04: /* B4CR */
453 case 0x05: /* B5CR */
456 case 0x06: /* B6CR */
459 case 0x07: /* B7CR */
462 case 0x10: /* B0AP */
465 case 0x11: /* B1AP */
468 case 0x12: /* B2AP */
471 case 0x13: /* B3AP */
474 case 0x14: /* B4AP */
477 case 0x15: /* B5AP */
480 case 0x16: /* B6AP */
483 case 0x17: /* B7AP */
486 case 0x20: /* BEAR */
489 case 0x21: /* BESR0 */
492 case 0x22: /* BESR1 */
511 static void dcr_write_ebc (void *opaque
, int dcrn
, uint32_t val
)
522 case 0x00: /* B0CR */
524 case 0x01: /* B1CR */
526 case 0x02: /* B2CR */
528 case 0x03: /* B3CR */
530 case 0x04: /* B4CR */
532 case 0x05: /* B5CR */
534 case 0x06: /* B6CR */
536 case 0x07: /* B7CR */
538 case 0x10: /* B0AP */
540 case 0x11: /* B1AP */
542 case 0x12: /* B2AP */
544 case 0x13: /* B3AP */
546 case 0x14: /* B4AP */
548 case 0x15: /* B5AP */
550 case 0x16: /* B6AP */
552 case 0x17: /* B7AP */
554 case 0x20: /* BEAR */
556 case 0x21: /* BESR0 */
558 case 0x22: /* BESR1 */
571 static void ebc_reset (void *opaque
)
577 ebc
->addr
= 0x00000000;
578 ebc
->bap
[0] = 0x7F8FFE80;
579 ebc
->bcr
[0] = 0xFFE28000;
580 for (i
= 0; i
< 8; i
++) {
581 ebc
->bap
[i
] = 0x00000000;
582 ebc
->bcr
[i
] = 0x00000000;
584 ebc
->besr0
= 0x00000000;
585 ebc
->besr1
= 0x00000000;
586 ebc
->cfg
= 0x80400000;
589 static void ppc405_ebc_init(CPUPPCState
*env
)
593 ebc
= g_malloc0(sizeof(ppc4xx_ebc_t
));
594 qemu_register_reset(&ebc_reset
, ebc
);
595 ppc_dcr_register(env
, EBC0_CFGADDR
,
596 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
597 ppc_dcr_register(env
, EBC0_CFGDATA
,
598 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
601 /*****************************************************************************/
630 typedef struct ppc405_dma_t ppc405_dma_t
;
631 struct ppc405_dma_t
{
644 static uint32_t dcr_read_dma (void *opaque
, int dcrn
)
649 static void dcr_write_dma (void *opaque
, int dcrn
, uint32_t val
)
653 static void ppc405_dma_reset (void *opaque
)
659 for (i
= 0; i
< 4; i
++) {
660 dma
->cr
[i
] = 0x00000000;
661 dma
->ct
[i
] = 0x00000000;
662 dma
->da
[i
] = 0x00000000;
663 dma
->sa
[i
] = 0x00000000;
664 dma
->sg
[i
] = 0x00000000;
666 dma
->sr
= 0x00000000;
667 dma
->sgc
= 0x00000000;
668 dma
->slp
= 0x7C000000;
669 dma
->pol
= 0x00000000;
672 static void ppc405_dma_init(CPUPPCState
*env
, qemu_irq irqs
[4])
676 dma
= g_malloc0(sizeof(ppc405_dma_t
));
677 memcpy(dma
->irqs
, irqs
, 4 * sizeof(qemu_irq
));
678 qemu_register_reset(&ppc405_dma_reset
, dma
);
679 ppc_dcr_register(env
, DMA0_CR0
,
680 dma
, &dcr_read_dma
, &dcr_write_dma
);
681 ppc_dcr_register(env
, DMA0_CT0
,
682 dma
, &dcr_read_dma
, &dcr_write_dma
);
683 ppc_dcr_register(env
, DMA0_DA0
,
684 dma
, &dcr_read_dma
, &dcr_write_dma
);
685 ppc_dcr_register(env
, DMA0_SA0
,
686 dma
, &dcr_read_dma
, &dcr_write_dma
);
687 ppc_dcr_register(env
, DMA0_SG0
,
688 dma
, &dcr_read_dma
, &dcr_write_dma
);
689 ppc_dcr_register(env
, DMA0_CR1
,
690 dma
, &dcr_read_dma
, &dcr_write_dma
);
691 ppc_dcr_register(env
, DMA0_CT1
,
692 dma
, &dcr_read_dma
, &dcr_write_dma
);
693 ppc_dcr_register(env
, DMA0_DA1
,
694 dma
, &dcr_read_dma
, &dcr_write_dma
);
695 ppc_dcr_register(env
, DMA0_SA1
,
696 dma
, &dcr_read_dma
, &dcr_write_dma
);
697 ppc_dcr_register(env
, DMA0_SG1
,
698 dma
, &dcr_read_dma
, &dcr_write_dma
);
699 ppc_dcr_register(env
, DMA0_CR2
,
700 dma
, &dcr_read_dma
, &dcr_write_dma
);
701 ppc_dcr_register(env
, DMA0_CT2
,
702 dma
, &dcr_read_dma
, &dcr_write_dma
);
703 ppc_dcr_register(env
, DMA0_DA2
,
704 dma
, &dcr_read_dma
, &dcr_write_dma
);
705 ppc_dcr_register(env
, DMA0_SA2
,
706 dma
, &dcr_read_dma
, &dcr_write_dma
);
707 ppc_dcr_register(env
, DMA0_SG2
,
708 dma
, &dcr_read_dma
, &dcr_write_dma
);
709 ppc_dcr_register(env
, DMA0_CR3
,
710 dma
, &dcr_read_dma
, &dcr_write_dma
);
711 ppc_dcr_register(env
, DMA0_CT3
,
712 dma
, &dcr_read_dma
, &dcr_write_dma
);
713 ppc_dcr_register(env
, DMA0_DA3
,
714 dma
, &dcr_read_dma
, &dcr_write_dma
);
715 ppc_dcr_register(env
, DMA0_SA3
,
716 dma
, &dcr_read_dma
, &dcr_write_dma
);
717 ppc_dcr_register(env
, DMA0_SG3
,
718 dma
, &dcr_read_dma
, &dcr_write_dma
);
719 ppc_dcr_register(env
, DMA0_SR
,
720 dma
, &dcr_read_dma
, &dcr_write_dma
);
721 ppc_dcr_register(env
, DMA0_SGC
,
722 dma
, &dcr_read_dma
, &dcr_write_dma
);
723 ppc_dcr_register(env
, DMA0_SLP
,
724 dma
, &dcr_read_dma
, &dcr_write_dma
);
725 ppc_dcr_register(env
, DMA0_POL
,
726 dma
, &dcr_read_dma
, &dcr_write_dma
);
729 /*****************************************************************************/
731 typedef struct ppc405_gpio_t ppc405_gpio_t
;
732 struct ppc405_gpio_t
{
747 static uint32_t ppc405_gpio_readb (void *opaque
, hwaddr addr
)
750 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
756 static void ppc405_gpio_writeb (void *opaque
,
757 hwaddr addr
, uint32_t value
)
760 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
765 static uint32_t ppc405_gpio_readw (void *opaque
, hwaddr addr
)
768 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
774 static void ppc405_gpio_writew (void *opaque
,
775 hwaddr addr
, uint32_t value
)
778 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
783 static uint32_t ppc405_gpio_readl (void *opaque
, hwaddr addr
)
786 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
792 static void ppc405_gpio_writel (void *opaque
,
793 hwaddr addr
, uint32_t value
)
796 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
801 static const MemoryRegionOps ppc405_gpio_ops
= {
803 .read
= { ppc405_gpio_readb
, ppc405_gpio_readw
, ppc405_gpio_readl
, },
804 .write
= { ppc405_gpio_writeb
, ppc405_gpio_writew
, ppc405_gpio_writel
, },
806 .endianness
= DEVICE_NATIVE_ENDIAN
,
809 static void ppc405_gpio_reset (void *opaque
)
813 static void ppc405_gpio_init(hwaddr base
)
817 gpio
= g_malloc0(sizeof(ppc405_gpio_t
));
819 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
821 memory_region_init_io(&gpio
->io
, NULL
, &ppc405_gpio_ops
, gpio
, "pgio", 0x038);
822 memory_region_add_subregion(get_system_memory(), base
, &gpio
->io
);
823 qemu_register_reset(&ppc405_gpio_reset
, gpio
);
826 /*****************************************************************************/
830 OCM0_ISACNTL
= 0x019,
832 OCM0_DSACNTL
= 0x01B,
835 typedef struct ppc405_ocm_t ppc405_ocm_t
;
836 struct ppc405_ocm_t
{
838 MemoryRegion isarc_ram
;
839 MemoryRegion dsarc_ram
;
846 static void ocm_update_mappings (ppc405_ocm_t
*ocm
,
847 uint32_t isarc
, uint32_t isacntl
,
848 uint32_t dsarc
, uint32_t dsacntl
)
851 printf("OCM update ISA %08" PRIx32
" %08" PRIx32
" (%08" PRIx32
852 " %08" PRIx32
") DSA %08" PRIx32
" %08" PRIx32
853 " (%08" PRIx32
" %08" PRIx32
")\n",
854 isarc
, isacntl
, dsarc
, dsacntl
,
855 ocm
->isarc
, ocm
->isacntl
, ocm
->dsarc
, ocm
->dsacntl
);
857 if (ocm
->isarc
!= isarc
||
858 (ocm
->isacntl
& 0x80000000) != (isacntl
& 0x80000000)) {
859 if (ocm
->isacntl
& 0x80000000) {
860 /* Unmap previously assigned memory region */
861 printf("OCM unmap ISA %08" PRIx32
"\n", ocm
->isarc
);
862 memory_region_del_subregion(get_system_memory(), &ocm
->isarc_ram
);
864 if (isacntl
& 0x80000000) {
865 /* Map new instruction memory region */
867 printf("OCM map ISA %08" PRIx32
"\n", isarc
);
869 memory_region_add_subregion(get_system_memory(), isarc
,
873 if (ocm
->dsarc
!= dsarc
||
874 (ocm
->dsacntl
& 0x80000000) != (dsacntl
& 0x80000000)) {
875 if (ocm
->dsacntl
& 0x80000000) {
876 /* Beware not to unmap the region we just mapped */
877 if (!(isacntl
& 0x80000000) || ocm
->dsarc
!= isarc
) {
878 /* Unmap previously assigned memory region */
880 printf("OCM unmap DSA %08" PRIx32
"\n", ocm
->dsarc
);
882 memory_region_del_subregion(get_system_memory(),
886 if (dsacntl
& 0x80000000) {
887 /* Beware not to remap the region we just mapped */
888 if (!(isacntl
& 0x80000000) || dsarc
!= isarc
) {
889 /* Map new data memory region */
891 printf("OCM map DSA %08" PRIx32
"\n", dsarc
);
893 memory_region_add_subregion(get_system_memory(), dsarc
,
900 static uint32_t dcr_read_ocm (void *opaque
, int dcrn
)
927 static void dcr_write_ocm (void *opaque
, int dcrn
, uint32_t val
)
930 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
935 isacntl
= ocm
->isacntl
;
936 dsacntl
= ocm
->dsacntl
;
939 isarc
= val
& 0xFC000000;
942 isacntl
= val
& 0xC0000000;
945 isarc
= val
& 0xFC000000;
948 isacntl
= val
& 0xC0000000;
951 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
954 ocm
->isacntl
= isacntl
;
955 ocm
->dsacntl
= dsacntl
;
958 static void ocm_reset (void *opaque
)
961 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
965 isacntl
= 0x00000000;
967 dsacntl
= 0x00000000;
968 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
971 ocm
->isacntl
= isacntl
;
972 ocm
->dsacntl
= dsacntl
;
975 static void ppc405_ocm_init(CPUPPCState
*env
)
979 ocm
= g_malloc0(sizeof(ppc405_ocm_t
));
980 /* XXX: Size is 4096 or 0x04000000 */
981 memory_region_init_ram(&ocm
->isarc_ram
, NULL
, "ppc405.ocm", 4096,
983 memory_region_init_alias(&ocm
->dsarc_ram
, NULL
, "ppc405.dsarc", &ocm
->isarc_ram
,
985 qemu_register_reset(&ocm_reset
, ocm
);
986 ppc_dcr_register(env
, OCM0_ISARC
,
987 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
988 ppc_dcr_register(env
, OCM0_ISACNTL
,
989 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
990 ppc_dcr_register(env
, OCM0_DSARC
,
991 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
992 ppc_dcr_register(env
, OCM0_DSACNTL
,
993 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
996 /*****************************************************************************/
998 typedef struct ppc4xx_i2c_t ppc4xx_i2c_t
;
999 struct ppc4xx_i2c_t
{
1019 static uint32_t ppc4xx_i2c_readb (void *opaque
, hwaddr addr
)
1025 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1030 // i2c_readbyte(&i2c->mdata);
1070 ret
= i2c
->xtcntlss
;
1073 ret
= i2c
->directcntl
;
1080 printf("%s: addr " TARGET_FMT_plx
" %02" PRIx32
"\n", __func__
, addr
, ret
);
1086 static void ppc4xx_i2c_writeb (void *opaque
,
1087 hwaddr addr
, uint32_t value
)
1092 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1099 // i2c_sendbyte(&i2c->mdata);
1114 i2c
->mdcntl
= value
& 0xDF;
1117 i2c
->sts
&= ~(value
& 0x0A);
1120 i2c
->extsts
&= ~(value
& 0x8F);
1129 i2c
->clkdiv
= value
;
1132 i2c
->intrmsk
= value
;
1135 i2c
->xfrcnt
= value
& 0x77;
1138 i2c
->xtcntlss
= value
;
1141 i2c
->directcntl
= value
& 0x7;
1146 static uint32_t ppc4xx_i2c_readw (void *opaque
, hwaddr addr
)
1151 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1153 ret
= ppc4xx_i2c_readb(opaque
, addr
) << 8;
1154 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 1);
1159 static void ppc4xx_i2c_writew (void *opaque
,
1160 hwaddr addr
, uint32_t value
)
1163 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1166 ppc4xx_i2c_writeb(opaque
, addr
, value
>> 8);
1167 ppc4xx_i2c_writeb(opaque
, addr
+ 1, value
);
1170 static uint32_t ppc4xx_i2c_readl (void *opaque
, hwaddr addr
)
1175 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1177 ret
= ppc4xx_i2c_readb(opaque
, addr
) << 24;
1178 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 1) << 16;
1179 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 2) << 8;
1180 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 3);
1185 static void ppc4xx_i2c_writel (void *opaque
,
1186 hwaddr addr
, uint32_t value
)
1189 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1192 ppc4xx_i2c_writeb(opaque
, addr
, value
>> 24);
1193 ppc4xx_i2c_writeb(opaque
, addr
+ 1, value
>> 16);
1194 ppc4xx_i2c_writeb(opaque
, addr
+ 2, value
>> 8);
1195 ppc4xx_i2c_writeb(opaque
, addr
+ 3, value
);
1198 static const MemoryRegionOps i2c_ops
= {
1200 .read
= { ppc4xx_i2c_readb
, ppc4xx_i2c_readw
, ppc4xx_i2c_readl
, },
1201 .write
= { ppc4xx_i2c_writeb
, ppc4xx_i2c_writew
, ppc4xx_i2c_writel
, },
1203 .endianness
= DEVICE_NATIVE_ENDIAN
,
1206 static void ppc4xx_i2c_reset (void *opaque
)
1219 i2c
->directcntl
= 0x0F;
1222 static void ppc405_i2c_init(hwaddr base
, qemu_irq irq
)
1226 i2c
= g_malloc0(sizeof(ppc4xx_i2c_t
));
1229 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1231 memory_region_init_io(&i2c
->iomem
, NULL
, &i2c_ops
, i2c
, "i2c", 0x011);
1232 memory_region_add_subregion(get_system_memory(), base
, &i2c
->iomem
);
1233 qemu_register_reset(ppc4xx_i2c_reset
, i2c
);
1236 /*****************************************************************************/
1237 /* General purpose timers */
1238 typedef struct ppc4xx_gpt_t ppc4xx_gpt_t
;
1239 struct ppc4xx_gpt_t
{
1254 static uint32_t ppc4xx_gpt_readb (void *opaque
, hwaddr addr
)
1257 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1259 /* XXX: generate a bus fault */
1263 static void ppc4xx_gpt_writeb (void *opaque
,
1264 hwaddr addr
, uint32_t value
)
1267 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1270 /* XXX: generate a bus fault */
1273 static uint32_t ppc4xx_gpt_readw (void *opaque
, hwaddr addr
)
1276 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1278 /* XXX: generate a bus fault */
1282 static void ppc4xx_gpt_writew (void *opaque
,
1283 hwaddr addr
, uint32_t value
)
1286 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1289 /* XXX: generate a bus fault */
1292 static int ppc4xx_gpt_compare (ppc4xx_gpt_t
*gpt
, int n
)
1298 static void ppc4xx_gpt_set_output (ppc4xx_gpt_t
*gpt
, int n
, int level
)
1303 static void ppc4xx_gpt_set_outputs (ppc4xx_gpt_t
*gpt
)
1309 for (i
= 0; i
< 5; i
++) {
1310 if (gpt
->oe
& mask
) {
1311 /* Output is enabled */
1312 if (ppc4xx_gpt_compare(gpt
, i
)) {
1313 /* Comparison is OK */
1314 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
);
1316 /* Comparison is KO */
1317 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
? 0 : 1);
1324 static void ppc4xx_gpt_set_irqs (ppc4xx_gpt_t
*gpt
)
1330 for (i
= 0; i
< 5; i
++) {
1331 if (gpt
->is
& gpt
->im
& mask
)
1332 qemu_irq_raise(gpt
->irqs
[i
]);
1334 qemu_irq_lower(gpt
->irqs
[i
]);
1339 static void ppc4xx_gpt_compute_timer (ppc4xx_gpt_t
*gpt
)
1344 static uint32_t ppc4xx_gpt_readl (void *opaque
, hwaddr addr
)
1351 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1356 /* Time base counter */
1357 ret
= muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + gpt
->tb_offset
,
1358 gpt
->tb_freq
, NANOSECONDS_PER_SECOND
);
1369 /* Interrupt mask */
1374 /* Interrupt status */
1378 /* Interrupt enable */
1383 idx
= (addr
- 0x80) >> 2;
1384 ret
= gpt
->comp
[idx
];
1388 idx
= (addr
- 0xC0) >> 2;
1389 ret
= gpt
->mask
[idx
];
1399 static void ppc4xx_gpt_writel (void *opaque
,
1400 hwaddr addr
, uint32_t value
)
1406 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1412 /* Time base counter */
1413 gpt
->tb_offset
= muldiv64(value
, NANOSECONDS_PER_SECOND
, gpt
->tb_freq
)
1414 - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1415 ppc4xx_gpt_compute_timer(gpt
);
1419 gpt
->oe
= value
& 0xF8000000;
1420 ppc4xx_gpt_set_outputs(gpt
);
1424 gpt
->ol
= value
& 0xF8000000;
1425 ppc4xx_gpt_set_outputs(gpt
);
1428 /* Interrupt mask */
1429 gpt
->im
= value
& 0x0000F800;
1432 /* Interrupt status set */
1433 gpt
->is
|= value
& 0x0000F800;
1434 ppc4xx_gpt_set_irqs(gpt
);
1437 /* Interrupt status clear */
1438 gpt
->is
&= ~(value
& 0x0000F800);
1439 ppc4xx_gpt_set_irqs(gpt
);
1442 /* Interrupt enable */
1443 gpt
->ie
= value
& 0x0000F800;
1444 ppc4xx_gpt_set_irqs(gpt
);
1448 idx
= (addr
- 0x80) >> 2;
1449 gpt
->comp
[idx
] = value
& 0xF8000000;
1450 ppc4xx_gpt_compute_timer(gpt
);
1454 idx
= (addr
- 0xC0) >> 2;
1455 gpt
->mask
[idx
] = value
& 0xF8000000;
1456 ppc4xx_gpt_compute_timer(gpt
);
1461 static const MemoryRegionOps gpt_ops
= {
1463 .read
= { ppc4xx_gpt_readb
, ppc4xx_gpt_readw
, ppc4xx_gpt_readl
, },
1464 .write
= { ppc4xx_gpt_writeb
, ppc4xx_gpt_writew
, ppc4xx_gpt_writel
, },
1466 .endianness
= DEVICE_NATIVE_ENDIAN
,
1469 static void ppc4xx_gpt_cb (void *opaque
)
1474 ppc4xx_gpt_set_irqs(gpt
);
1475 ppc4xx_gpt_set_outputs(gpt
);
1476 ppc4xx_gpt_compute_timer(gpt
);
1479 static void ppc4xx_gpt_reset (void *opaque
)
1485 timer_del(gpt
->timer
);
1486 gpt
->oe
= 0x00000000;
1487 gpt
->ol
= 0x00000000;
1488 gpt
->im
= 0x00000000;
1489 gpt
->is
= 0x00000000;
1490 gpt
->ie
= 0x00000000;
1491 for (i
= 0; i
< 5; i
++) {
1492 gpt
->comp
[i
] = 0x00000000;
1493 gpt
->mask
[i
] = 0x00000000;
1497 static void ppc4xx_gpt_init(hwaddr base
, qemu_irq irqs
[5])
1502 gpt
= g_malloc0(sizeof(ppc4xx_gpt_t
));
1503 for (i
= 0; i
< 5; i
++) {
1504 gpt
->irqs
[i
] = irqs
[i
];
1506 gpt
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &ppc4xx_gpt_cb
, gpt
);
1508 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1510 memory_region_init_io(&gpt
->iomem
, NULL
, &gpt_ops
, gpt
, "gpt", 0x0d4);
1511 memory_region_add_subregion(get_system_memory(), base
, &gpt
->iomem
);
1512 qemu_register_reset(ppc4xx_gpt_reset
, gpt
);
1515 /*****************************************************************************/
1521 MAL0_TXCASR
= 0x184,
1522 MAL0_TXCARR
= 0x185,
1523 MAL0_TXEOBISR
= 0x186,
1524 MAL0_TXDEIR
= 0x187,
1525 MAL0_RXCASR
= 0x190,
1526 MAL0_RXCARR
= 0x191,
1527 MAL0_RXEOBISR
= 0x192,
1528 MAL0_RXDEIR
= 0x193,
1529 MAL0_TXCTP0R
= 0x1A0,
1530 MAL0_TXCTP1R
= 0x1A1,
1531 MAL0_TXCTP2R
= 0x1A2,
1532 MAL0_TXCTP3R
= 0x1A3,
1533 MAL0_RXCTP0R
= 0x1C0,
1534 MAL0_RXCTP1R
= 0x1C1,
1539 typedef struct ppc40x_mal_t ppc40x_mal_t
;
1540 struct ppc40x_mal_t
{
1558 static void ppc40x_mal_reset (void *opaque
);
1560 static uint32_t dcr_read_mal (void *opaque
, int dcrn
)
1583 ret
= mal
->txeobisr
;
1595 ret
= mal
->rxeobisr
;
1601 ret
= mal
->txctpr
[0];
1604 ret
= mal
->txctpr
[1];
1607 ret
= mal
->txctpr
[2];
1610 ret
= mal
->txctpr
[3];
1613 ret
= mal
->rxctpr
[0];
1616 ret
= mal
->rxctpr
[1];
1632 static void dcr_write_mal (void *opaque
, int dcrn
, uint32_t val
)
1640 if (val
& 0x80000000)
1641 ppc40x_mal_reset(mal
);
1642 mal
->cfg
= val
& 0x00FFC087;
1649 mal
->ier
= val
& 0x0000001F;
1652 mal
->txcasr
= val
& 0xF0000000;
1655 mal
->txcarr
= val
& 0xF0000000;
1659 mal
->txeobisr
&= ~val
;
1663 mal
->txdeir
&= ~val
;
1666 mal
->rxcasr
= val
& 0xC0000000;
1669 mal
->rxcarr
= val
& 0xC0000000;
1673 mal
->rxeobisr
&= ~val
;
1677 mal
->rxdeir
&= ~val
;
1691 mal
->txctpr
[idx
] = val
;
1699 mal
->rxctpr
[idx
] = val
;
1703 goto update_rx_size
;
1707 mal
->rcbs
[idx
] = val
& 0x000000FF;
1712 static void ppc40x_mal_reset (void *opaque
)
1717 mal
->cfg
= 0x0007C000;
1718 mal
->esr
= 0x00000000;
1719 mal
->ier
= 0x00000000;
1720 mal
->rxcasr
= 0x00000000;
1721 mal
->rxdeir
= 0x00000000;
1722 mal
->rxeobisr
= 0x00000000;
1723 mal
->txcasr
= 0x00000000;
1724 mal
->txdeir
= 0x00000000;
1725 mal
->txeobisr
= 0x00000000;
1728 static void ppc405_mal_init(CPUPPCState
*env
, qemu_irq irqs
[4])
1733 mal
= g_malloc0(sizeof(ppc40x_mal_t
));
1734 for (i
= 0; i
< 4; i
++)
1735 mal
->irqs
[i
] = irqs
[i
];
1736 qemu_register_reset(&ppc40x_mal_reset
, mal
);
1737 ppc_dcr_register(env
, MAL0_CFG
,
1738 mal
, &dcr_read_mal
, &dcr_write_mal
);
1739 ppc_dcr_register(env
, MAL0_ESR
,
1740 mal
, &dcr_read_mal
, &dcr_write_mal
);
1741 ppc_dcr_register(env
, MAL0_IER
,
1742 mal
, &dcr_read_mal
, &dcr_write_mal
);
1743 ppc_dcr_register(env
, MAL0_TXCASR
,
1744 mal
, &dcr_read_mal
, &dcr_write_mal
);
1745 ppc_dcr_register(env
, MAL0_TXCARR
,
1746 mal
, &dcr_read_mal
, &dcr_write_mal
);
1747 ppc_dcr_register(env
, MAL0_TXEOBISR
,
1748 mal
, &dcr_read_mal
, &dcr_write_mal
);
1749 ppc_dcr_register(env
, MAL0_TXDEIR
,
1750 mal
, &dcr_read_mal
, &dcr_write_mal
);
1751 ppc_dcr_register(env
, MAL0_RXCASR
,
1752 mal
, &dcr_read_mal
, &dcr_write_mal
);
1753 ppc_dcr_register(env
, MAL0_RXCARR
,
1754 mal
, &dcr_read_mal
, &dcr_write_mal
);
1755 ppc_dcr_register(env
, MAL0_RXEOBISR
,
1756 mal
, &dcr_read_mal
, &dcr_write_mal
);
1757 ppc_dcr_register(env
, MAL0_RXDEIR
,
1758 mal
, &dcr_read_mal
, &dcr_write_mal
);
1759 ppc_dcr_register(env
, MAL0_TXCTP0R
,
1760 mal
, &dcr_read_mal
, &dcr_write_mal
);
1761 ppc_dcr_register(env
, MAL0_TXCTP1R
,
1762 mal
, &dcr_read_mal
, &dcr_write_mal
);
1763 ppc_dcr_register(env
, MAL0_TXCTP2R
,
1764 mal
, &dcr_read_mal
, &dcr_write_mal
);
1765 ppc_dcr_register(env
, MAL0_TXCTP3R
,
1766 mal
, &dcr_read_mal
, &dcr_write_mal
);
1767 ppc_dcr_register(env
, MAL0_RXCTP0R
,
1768 mal
, &dcr_read_mal
, &dcr_write_mal
);
1769 ppc_dcr_register(env
, MAL0_RXCTP1R
,
1770 mal
, &dcr_read_mal
, &dcr_write_mal
);
1771 ppc_dcr_register(env
, MAL0_RCBS0
,
1772 mal
, &dcr_read_mal
, &dcr_write_mal
);
1773 ppc_dcr_register(env
, MAL0_RCBS1
,
1774 mal
, &dcr_read_mal
, &dcr_write_mal
);
1777 /*****************************************************************************/
1779 void ppc40x_core_reset(PowerPCCPU
*cpu
)
1781 CPUPPCState
*env
= &cpu
->env
;
1784 printf("Reset PowerPC core\n");
1785 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_RESET
);
1786 dbsr
= env
->spr
[SPR_40x_DBSR
];
1787 dbsr
&= ~0x00000300;
1789 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1792 void ppc40x_chip_reset(PowerPCCPU
*cpu
)
1794 CPUPPCState
*env
= &cpu
->env
;
1797 printf("Reset PowerPC chip\n");
1798 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_RESET
);
1799 /* XXX: TODO reset all internal peripherals */
1800 dbsr
= env
->spr
[SPR_40x_DBSR
];
1801 dbsr
&= ~0x00000300;
1803 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1806 void ppc40x_system_reset(PowerPCCPU
*cpu
)
1808 printf("Reset PowerPC system\n");
1809 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
1812 void store_40x_dbcr0 (CPUPPCState
*env
, uint32_t val
)
1814 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1816 switch ((val
>> 28) & 0x3) {
1822 ppc40x_core_reset(cpu
);
1826 ppc40x_chip_reset(cpu
);
1830 ppc40x_system_reset(cpu
);
1835 /*****************************************************************************/
1838 PPC405CR_CPC0_PLLMR
= 0x0B0,
1839 PPC405CR_CPC0_CR0
= 0x0B1,
1840 PPC405CR_CPC0_CR1
= 0x0B2,
1841 PPC405CR_CPC0_PSR
= 0x0B4,
1842 PPC405CR_CPC0_JTAGID
= 0x0B5,
1843 PPC405CR_CPC0_ER
= 0x0B9,
1844 PPC405CR_CPC0_FR
= 0x0BA,
1845 PPC405CR_CPC0_SR
= 0x0BB,
1849 PPC405CR_CPU_CLK
= 0,
1850 PPC405CR_TMR_CLK
= 1,
1851 PPC405CR_PLB_CLK
= 2,
1852 PPC405CR_SDRAM_CLK
= 3,
1853 PPC405CR_OPB_CLK
= 4,
1854 PPC405CR_EXT_CLK
= 5,
1855 PPC405CR_UART_CLK
= 6,
1856 PPC405CR_CLK_NB
= 7,
1859 typedef struct ppc405cr_cpc_t ppc405cr_cpc_t
;
1860 struct ppc405cr_cpc_t
{
1861 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
1872 static void ppc405cr_clk_setup (ppc405cr_cpc_t
*cpc
)
1874 uint64_t VCO_out
, PLL_out
;
1875 uint32_t CPU_clk
, TMR_clk
, SDRAM_clk
, PLB_clk
, OPB_clk
, EXT_clk
, UART_clk
;
1878 D0
= ((cpc
->pllmr
>> 26) & 0x3) + 1; /* CBDV */
1879 if (cpc
->pllmr
& 0x80000000) {
1880 D1
= (((cpc
->pllmr
>> 20) - 1) & 0xF) + 1; /* FBDV */
1881 D2
= 8 - ((cpc
->pllmr
>> 16) & 0x7); /* FWDVA */
1883 VCO_out
= (uint64_t)cpc
->sysclk
* M
;
1884 if (VCO_out
< 400000000 || VCO_out
> 800000000) {
1885 /* PLL cannot lock */
1886 cpc
->pllmr
&= ~0x80000000;
1889 PLL_out
= VCO_out
/ D2
;
1894 PLL_out
= (uint64_t)cpc
->sysclk
* M
;
1897 if (cpc
->cr1
& 0x00800000)
1898 TMR_clk
= cpc
->sysclk
; /* Should have a separate clock */
1901 PLB_clk
= CPU_clk
/ D0
;
1902 SDRAM_clk
= PLB_clk
;
1903 D0
= ((cpc
->pllmr
>> 10) & 0x3) + 1;
1904 OPB_clk
= PLB_clk
/ D0
;
1905 D0
= ((cpc
->pllmr
>> 24) & 0x3) + 2;
1906 EXT_clk
= PLB_clk
/ D0
;
1907 D0
= ((cpc
->cr0
>> 1) & 0x1F) + 1;
1908 UART_clk
= CPU_clk
/ D0
;
1909 /* Setup CPU clocks */
1910 clk_setup(&cpc
->clk_setup
[PPC405CR_CPU_CLK
], CPU_clk
);
1911 /* Setup time-base clock */
1912 clk_setup(&cpc
->clk_setup
[PPC405CR_TMR_CLK
], TMR_clk
);
1913 /* Setup PLB clock */
1914 clk_setup(&cpc
->clk_setup
[PPC405CR_PLB_CLK
], PLB_clk
);
1915 /* Setup SDRAM clock */
1916 clk_setup(&cpc
->clk_setup
[PPC405CR_SDRAM_CLK
], SDRAM_clk
);
1917 /* Setup OPB clock */
1918 clk_setup(&cpc
->clk_setup
[PPC405CR_OPB_CLK
], OPB_clk
);
1919 /* Setup external clock */
1920 clk_setup(&cpc
->clk_setup
[PPC405CR_EXT_CLK
], EXT_clk
);
1921 /* Setup UART clock */
1922 clk_setup(&cpc
->clk_setup
[PPC405CR_UART_CLK
], UART_clk
);
1925 static uint32_t dcr_read_crcpc (void *opaque
, int dcrn
)
1927 ppc405cr_cpc_t
*cpc
;
1932 case PPC405CR_CPC0_PLLMR
:
1935 case PPC405CR_CPC0_CR0
:
1938 case PPC405CR_CPC0_CR1
:
1941 case PPC405CR_CPC0_PSR
:
1944 case PPC405CR_CPC0_JTAGID
:
1947 case PPC405CR_CPC0_ER
:
1950 case PPC405CR_CPC0_FR
:
1953 case PPC405CR_CPC0_SR
:
1954 ret
= ~(cpc
->er
| cpc
->fr
) & 0xFFFF0000;
1957 /* Avoid gcc warning */
1965 static void dcr_write_crcpc (void *opaque
, int dcrn
, uint32_t val
)
1967 ppc405cr_cpc_t
*cpc
;
1971 case PPC405CR_CPC0_PLLMR
:
1972 cpc
->pllmr
= val
& 0xFFF77C3F;
1974 case PPC405CR_CPC0_CR0
:
1975 cpc
->cr0
= val
& 0x0FFFFFFE;
1977 case PPC405CR_CPC0_CR1
:
1978 cpc
->cr1
= val
& 0x00800000;
1980 case PPC405CR_CPC0_PSR
:
1983 case PPC405CR_CPC0_JTAGID
:
1986 case PPC405CR_CPC0_ER
:
1987 cpc
->er
= val
& 0xBFFC0000;
1989 case PPC405CR_CPC0_FR
:
1990 cpc
->fr
= val
& 0xBFFC0000;
1992 case PPC405CR_CPC0_SR
:
1998 static void ppc405cr_cpc_reset (void *opaque
)
2000 ppc405cr_cpc_t
*cpc
;
2004 /* Compute PLLMR value from PSR settings */
2005 cpc
->pllmr
= 0x80000000;
2007 switch ((cpc
->psr
>> 30) & 3) {
2010 cpc
->pllmr
&= ~0x80000000;
2014 cpc
->pllmr
|= 5 << 16;
2018 cpc
->pllmr
|= 4 << 16;
2022 cpc
->pllmr
|= 2 << 16;
2026 D
= (cpc
->psr
>> 28) & 3;
2027 cpc
->pllmr
|= (D
+ 1) << 20;
2029 D
= (cpc
->psr
>> 25) & 7;
2044 D
= (cpc
->psr
>> 23) & 3;
2045 cpc
->pllmr
|= D
<< 26;
2047 D
= (cpc
->psr
>> 21) & 3;
2048 cpc
->pllmr
|= D
<< 10;
2050 D
= (cpc
->psr
>> 17) & 3;
2051 cpc
->pllmr
|= D
<< 24;
2052 cpc
->cr0
= 0x0000003C;
2053 cpc
->cr1
= 0x2B0D8800;
2054 cpc
->er
= 0x00000000;
2055 cpc
->fr
= 0x00000000;
2056 ppc405cr_clk_setup(cpc
);
2059 static void ppc405cr_clk_init (ppc405cr_cpc_t
*cpc
)
2063 /* XXX: this should be read from IO pins */
2064 cpc
->psr
= 0x00000000; /* 8 bits ROM */
2066 D
= 0x2; /* Divide by 4 */
2067 cpc
->psr
|= D
<< 30;
2069 D
= 0x1; /* Divide by 2 */
2070 cpc
->psr
|= D
<< 28;
2072 D
= 0x1; /* Divide by 2 */
2073 cpc
->psr
|= D
<< 23;
2075 D
= 0x5; /* M = 16 */
2076 cpc
->psr
|= D
<< 25;
2078 D
= 0x1; /* Divide by 2 */
2079 cpc
->psr
|= D
<< 21;
2081 D
= 0x2; /* Divide by 4 */
2082 cpc
->psr
|= D
<< 17;
2085 static void ppc405cr_cpc_init (CPUPPCState
*env
, clk_setup_t clk_setup
[7],
2088 ppc405cr_cpc_t
*cpc
;
2090 cpc
= g_malloc0(sizeof(ppc405cr_cpc_t
));
2091 memcpy(cpc
->clk_setup
, clk_setup
,
2092 PPC405CR_CLK_NB
* sizeof(clk_setup_t
));
2093 cpc
->sysclk
= sysclk
;
2094 cpc
->jtagid
= 0x42051049;
2095 ppc_dcr_register(env
, PPC405CR_CPC0_PSR
, cpc
,
2096 &dcr_read_crcpc
, &dcr_write_crcpc
);
2097 ppc_dcr_register(env
, PPC405CR_CPC0_CR0
, cpc
,
2098 &dcr_read_crcpc
, &dcr_write_crcpc
);
2099 ppc_dcr_register(env
, PPC405CR_CPC0_CR1
, cpc
,
2100 &dcr_read_crcpc
, &dcr_write_crcpc
);
2101 ppc_dcr_register(env
, PPC405CR_CPC0_JTAGID
, cpc
,
2102 &dcr_read_crcpc
, &dcr_write_crcpc
);
2103 ppc_dcr_register(env
, PPC405CR_CPC0_PLLMR
, cpc
,
2104 &dcr_read_crcpc
, &dcr_write_crcpc
);
2105 ppc_dcr_register(env
, PPC405CR_CPC0_ER
, cpc
,
2106 &dcr_read_crcpc
, &dcr_write_crcpc
);
2107 ppc_dcr_register(env
, PPC405CR_CPC0_FR
, cpc
,
2108 &dcr_read_crcpc
, &dcr_write_crcpc
);
2109 ppc_dcr_register(env
, PPC405CR_CPC0_SR
, cpc
,
2110 &dcr_read_crcpc
, &dcr_write_crcpc
);
2111 ppc405cr_clk_init(cpc
);
2112 qemu_register_reset(ppc405cr_cpc_reset
, cpc
);
2115 CPUPPCState
*ppc405cr_init(MemoryRegion
*address_space_mem
,
2116 MemoryRegion ram_memories
[4],
2117 hwaddr ram_bases
[4],
2118 hwaddr ram_sizes
[4],
2119 uint32_t sysclk
, qemu_irq
**picp
,
2122 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
2123 qemu_irq dma_irqs
[4];
2126 qemu_irq
*pic
, *irqs
;
2128 memset(clk_setup
, 0, sizeof(clk_setup
));
2129 cpu
= ppc4xx_init("405cr", &clk_setup
[PPC405CR_CPU_CLK
],
2130 &clk_setup
[PPC405CR_TMR_CLK
], sysclk
);
2132 /* Memory mapped devices registers */
2134 ppc4xx_plb_init(env
);
2135 /* PLB to OPB bridge */
2136 ppc4xx_pob_init(env
);
2138 ppc4xx_opba_init(0xef600600);
2139 /* Universal interrupt controller */
2140 irqs
= g_malloc0(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2141 irqs
[PPCUIC_OUTPUT_INT
] =
2142 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2143 irqs
[PPCUIC_OUTPUT_CINT
] =
2144 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2145 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2147 /* SDRAM controller */
2148 ppc4xx_sdram_init(env
, pic
[14], 1, ram_memories
,
2149 ram_bases
, ram_sizes
, do_init
);
2150 /* External bus controller */
2151 ppc405_ebc_init(env
);
2152 /* DMA controller */
2153 dma_irqs
[0] = pic
[26];
2154 dma_irqs
[1] = pic
[25];
2155 dma_irqs
[2] = pic
[24];
2156 dma_irqs
[3] = pic
[23];
2157 ppc405_dma_init(env
, dma_irqs
);
2159 if (serial_hds
[0] != NULL
) {
2160 serial_mm_init(address_space_mem
, 0xef600300, 0, pic
[0],
2161 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[0],
2164 if (serial_hds
[1] != NULL
) {
2165 serial_mm_init(address_space_mem
, 0xef600400, 0, pic
[1],
2166 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[1],
2169 /* IIC controller */
2170 ppc405_i2c_init(0xef600500, pic
[2]);
2172 ppc405_gpio_init(0xef600700);
2174 ppc405cr_cpc_init(env
, clk_setup
, sysclk
);
2179 /*****************************************************************************/
2183 PPC405EP_CPC0_PLLMR0
= 0x0F0,
2184 PPC405EP_CPC0_BOOT
= 0x0F1,
2185 PPC405EP_CPC0_EPCTL
= 0x0F3,
2186 PPC405EP_CPC0_PLLMR1
= 0x0F4,
2187 PPC405EP_CPC0_UCR
= 0x0F5,
2188 PPC405EP_CPC0_SRR
= 0x0F6,
2189 PPC405EP_CPC0_JTAGID
= 0x0F7,
2190 PPC405EP_CPC0_PCI
= 0x0F9,
2192 PPC405EP_CPC0_ER
= xxx
,
2193 PPC405EP_CPC0_FR
= xxx
,
2194 PPC405EP_CPC0_SR
= xxx
,
2199 PPC405EP_CPU_CLK
= 0,
2200 PPC405EP_PLB_CLK
= 1,
2201 PPC405EP_OPB_CLK
= 2,
2202 PPC405EP_EBC_CLK
= 3,
2203 PPC405EP_MAL_CLK
= 4,
2204 PPC405EP_PCI_CLK
= 5,
2205 PPC405EP_UART0_CLK
= 6,
2206 PPC405EP_UART1_CLK
= 7,
2207 PPC405EP_CLK_NB
= 8,
2210 typedef struct ppc405ep_cpc_t ppc405ep_cpc_t
;
2211 struct ppc405ep_cpc_t
{
2213 clk_setup_t clk_setup
[PPC405EP_CLK_NB
];
2221 /* Clock and power management */
2227 static void ppc405ep_compute_clocks (ppc405ep_cpc_t
*cpc
)
2229 uint32_t CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
;
2230 uint32_t UART0_clk
, UART1_clk
;
2231 uint64_t VCO_out
, PLL_out
;
2235 if ((cpc
->pllmr
[1] & 0x80000000) && !(cpc
->pllmr
[1] & 0x40000000)) {
2236 M
= (((cpc
->pllmr
[1] >> 20) - 1) & 0xF) + 1; /* FBMUL */
2237 #ifdef DEBUG_CLOCKS_LL
2238 printf("FBMUL %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 20) & 0xF, M
);
2240 D
= 8 - ((cpc
->pllmr
[1] >> 16) & 0x7); /* FWDA */
2241 #ifdef DEBUG_CLOCKS_LL
2242 printf("FWDA %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 16) & 0x7, D
);
2244 VCO_out
= (uint64_t)cpc
->sysclk
* M
* D
;
2245 if (VCO_out
< 500000000UL || VCO_out
> 1000000000UL) {
2246 /* Error - unlock the PLL */
2247 printf("VCO out of range %" PRIu64
"\n", VCO_out
);
2249 cpc
->pllmr
[1] &= ~0x80000000;
2253 PLL_out
= VCO_out
/ D
;
2254 /* Pretend the PLL is locked */
2255 cpc
->boot
|= 0x00000001;
2260 PLL_out
= cpc
->sysclk
;
2261 if (cpc
->pllmr
[1] & 0x40000000) {
2262 /* Pretend the PLL is not locked */
2263 cpc
->boot
&= ~0x00000001;
2266 /* Now, compute all other clocks */
2267 D
= ((cpc
->pllmr
[0] >> 20) & 0x3) + 1; /* CCDV */
2268 #ifdef DEBUG_CLOCKS_LL
2269 printf("CCDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 20) & 0x3, D
);
2271 CPU_clk
= PLL_out
/ D
;
2272 D
= ((cpc
->pllmr
[0] >> 16) & 0x3) + 1; /* CBDV */
2273 #ifdef DEBUG_CLOCKS_LL
2274 printf("CBDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 16) & 0x3, D
);
2276 PLB_clk
= CPU_clk
/ D
;
2277 D
= ((cpc
->pllmr
[0] >> 12) & 0x3) + 1; /* OPDV */
2278 #ifdef DEBUG_CLOCKS_LL
2279 printf("OPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 12) & 0x3, D
);
2281 OPB_clk
= PLB_clk
/ D
;
2282 D
= ((cpc
->pllmr
[0] >> 8) & 0x3) + 2; /* EPDV */
2283 #ifdef DEBUG_CLOCKS_LL
2284 printf("EPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 8) & 0x3, D
);
2286 EBC_clk
= PLB_clk
/ D
;
2287 D
= ((cpc
->pllmr
[0] >> 4) & 0x3) + 1; /* MPDV */
2288 #ifdef DEBUG_CLOCKS_LL
2289 printf("MPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 4) & 0x3, D
);
2291 MAL_clk
= PLB_clk
/ D
;
2292 D
= (cpc
->pllmr
[0] & 0x3) + 1; /* PPDV */
2293 #ifdef DEBUG_CLOCKS_LL
2294 printf("PPDV %01" PRIx32
" %d\n", cpc
->pllmr
[0] & 0x3, D
);
2296 PCI_clk
= PLB_clk
/ D
;
2297 D
= ((cpc
->ucr
- 1) & 0x7F) + 1; /* U0DIV */
2298 #ifdef DEBUG_CLOCKS_LL
2299 printf("U0DIV %01" PRIx32
" %d\n", cpc
->ucr
& 0x7F, D
);
2301 UART0_clk
= PLL_out
/ D
;
2302 D
= (((cpc
->ucr
>> 8) - 1) & 0x7F) + 1; /* U1DIV */
2303 #ifdef DEBUG_CLOCKS_LL
2304 printf("U1DIV %01" PRIx32
" %d\n", (cpc
->ucr
>> 8) & 0x7F, D
);
2306 UART1_clk
= PLL_out
/ D
;
2308 printf("Setup PPC405EP clocks - sysclk %" PRIu32
" VCO %" PRIu64
2309 " PLL out %" PRIu64
" Hz\n", cpc
->sysclk
, VCO_out
, PLL_out
);
2310 printf("CPU %" PRIu32
" PLB %" PRIu32
" OPB %" PRIu32
" EBC %" PRIu32
2311 " MAL %" PRIu32
" PCI %" PRIu32
" UART0 %" PRIu32
2312 " UART1 %" PRIu32
"\n",
2313 CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
,
2314 UART0_clk
, UART1_clk
);
2316 /* Setup CPU clocks */
2317 clk_setup(&cpc
->clk_setup
[PPC405EP_CPU_CLK
], CPU_clk
);
2318 /* Setup PLB clock */
2319 clk_setup(&cpc
->clk_setup
[PPC405EP_PLB_CLK
], PLB_clk
);
2320 /* Setup OPB clock */
2321 clk_setup(&cpc
->clk_setup
[PPC405EP_OPB_CLK
], OPB_clk
);
2322 /* Setup external clock */
2323 clk_setup(&cpc
->clk_setup
[PPC405EP_EBC_CLK
], EBC_clk
);
2324 /* Setup MAL clock */
2325 clk_setup(&cpc
->clk_setup
[PPC405EP_MAL_CLK
], MAL_clk
);
2326 /* Setup PCI clock */
2327 clk_setup(&cpc
->clk_setup
[PPC405EP_PCI_CLK
], PCI_clk
);
2328 /* Setup UART0 clock */
2329 clk_setup(&cpc
->clk_setup
[PPC405EP_UART0_CLK
], UART0_clk
);
2330 /* Setup UART1 clock */
2331 clk_setup(&cpc
->clk_setup
[PPC405EP_UART1_CLK
], UART1_clk
);
2334 static uint32_t dcr_read_epcpc (void *opaque
, int dcrn
)
2336 ppc405ep_cpc_t
*cpc
;
2341 case PPC405EP_CPC0_BOOT
:
2344 case PPC405EP_CPC0_EPCTL
:
2347 case PPC405EP_CPC0_PLLMR0
:
2348 ret
= cpc
->pllmr
[0];
2350 case PPC405EP_CPC0_PLLMR1
:
2351 ret
= cpc
->pllmr
[1];
2353 case PPC405EP_CPC0_UCR
:
2356 case PPC405EP_CPC0_SRR
:
2359 case PPC405EP_CPC0_JTAGID
:
2362 case PPC405EP_CPC0_PCI
:
2366 /* Avoid gcc warning */
2374 static void dcr_write_epcpc (void *opaque
, int dcrn
, uint32_t val
)
2376 ppc405ep_cpc_t
*cpc
;
2380 case PPC405EP_CPC0_BOOT
:
2381 /* Read-only register */
2383 case PPC405EP_CPC0_EPCTL
:
2384 /* Don't care for now */
2385 cpc
->epctl
= val
& 0xC00000F3;
2387 case PPC405EP_CPC0_PLLMR0
:
2388 cpc
->pllmr
[0] = val
& 0x00633333;
2389 ppc405ep_compute_clocks(cpc
);
2391 case PPC405EP_CPC0_PLLMR1
:
2392 cpc
->pllmr
[1] = val
& 0xC0F73FFF;
2393 ppc405ep_compute_clocks(cpc
);
2395 case PPC405EP_CPC0_UCR
:
2396 /* UART control - don't care for now */
2397 cpc
->ucr
= val
& 0x003F7F7F;
2399 case PPC405EP_CPC0_SRR
:
2402 case PPC405EP_CPC0_JTAGID
:
2405 case PPC405EP_CPC0_PCI
:
2411 static void ppc405ep_cpc_reset (void *opaque
)
2413 ppc405ep_cpc_t
*cpc
= opaque
;
2415 cpc
->boot
= 0x00000010; /* Boot from PCI - IIC EEPROM disabled */
2416 cpc
->epctl
= 0x00000000;
2417 cpc
->pllmr
[0] = 0x00011010;
2418 cpc
->pllmr
[1] = 0x40000000;
2419 cpc
->ucr
= 0x00000000;
2420 cpc
->srr
= 0x00040000;
2421 cpc
->pci
= 0x00000000;
2422 cpc
->er
= 0x00000000;
2423 cpc
->fr
= 0x00000000;
2424 cpc
->sr
= 0x00000000;
2425 ppc405ep_compute_clocks(cpc
);
2428 /* XXX: sysclk should be between 25 and 100 MHz */
2429 static void ppc405ep_cpc_init (CPUPPCState
*env
, clk_setup_t clk_setup
[8],
2432 ppc405ep_cpc_t
*cpc
;
2434 cpc
= g_malloc0(sizeof(ppc405ep_cpc_t
));
2435 memcpy(cpc
->clk_setup
, clk_setup
,
2436 PPC405EP_CLK_NB
* sizeof(clk_setup_t
));
2437 cpc
->jtagid
= 0x20267049;
2438 cpc
->sysclk
= sysclk
;
2439 qemu_register_reset(&ppc405ep_cpc_reset
, cpc
);
2440 ppc_dcr_register(env
, PPC405EP_CPC0_BOOT
, cpc
,
2441 &dcr_read_epcpc
, &dcr_write_epcpc
);
2442 ppc_dcr_register(env
, PPC405EP_CPC0_EPCTL
, cpc
,
2443 &dcr_read_epcpc
, &dcr_write_epcpc
);
2444 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR0
, cpc
,
2445 &dcr_read_epcpc
, &dcr_write_epcpc
);
2446 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR1
, cpc
,
2447 &dcr_read_epcpc
, &dcr_write_epcpc
);
2448 ppc_dcr_register(env
, PPC405EP_CPC0_UCR
, cpc
,
2449 &dcr_read_epcpc
, &dcr_write_epcpc
);
2450 ppc_dcr_register(env
, PPC405EP_CPC0_SRR
, cpc
,
2451 &dcr_read_epcpc
, &dcr_write_epcpc
);
2452 ppc_dcr_register(env
, PPC405EP_CPC0_JTAGID
, cpc
,
2453 &dcr_read_epcpc
, &dcr_write_epcpc
);
2454 ppc_dcr_register(env
, PPC405EP_CPC0_PCI
, cpc
,
2455 &dcr_read_epcpc
, &dcr_write_epcpc
);
2457 ppc_dcr_register(env
, PPC405EP_CPC0_ER
, cpc
,
2458 &dcr_read_epcpc
, &dcr_write_epcpc
);
2459 ppc_dcr_register(env
, PPC405EP_CPC0_FR
, cpc
,
2460 &dcr_read_epcpc
, &dcr_write_epcpc
);
2461 ppc_dcr_register(env
, PPC405EP_CPC0_SR
, cpc
,
2462 &dcr_read_epcpc
, &dcr_write_epcpc
);
2466 CPUPPCState
*ppc405ep_init(MemoryRegion
*address_space_mem
,
2467 MemoryRegion ram_memories
[2],
2468 hwaddr ram_bases
[2],
2469 hwaddr ram_sizes
[2],
2470 uint32_t sysclk
, qemu_irq
**picp
,
2473 clk_setup_t clk_setup
[PPC405EP_CLK_NB
], tlb_clk_setup
;
2474 qemu_irq dma_irqs
[4], gpt_irqs
[5], mal_irqs
[4];
2477 qemu_irq
*pic
, *irqs
;
2479 memset(clk_setup
, 0, sizeof(clk_setup
));
2481 cpu
= ppc4xx_init("405ep", &clk_setup
[PPC405EP_CPU_CLK
],
2482 &tlb_clk_setup
, sysclk
);
2484 clk_setup
[PPC405EP_CPU_CLK
].cb
= tlb_clk_setup
.cb
;
2485 clk_setup
[PPC405EP_CPU_CLK
].opaque
= tlb_clk_setup
.opaque
;
2486 /* Internal devices init */
2487 /* Memory mapped devices registers */
2489 ppc4xx_plb_init(env
);
2490 /* PLB to OPB bridge */
2491 ppc4xx_pob_init(env
);
2493 ppc4xx_opba_init(0xef600600);
2494 /* Initialize timers */
2495 ppc_booke_timers_init(cpu
, sysclk
, 0);
2496 /* Universal interrupt controller */
2497 irqs
= g_malloc0(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2498 irqs
[PPCUIC_OUTPUT_INT
] =
2499 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2500 irqs
[PPCUIC_OUTPUT_CINT
] =
2501 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2502 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2504 /* SDRAM controller */
2505 /* XXX 405EP has no ECC interrupt */
2506 ppc4xx_sdram_init(env
, pic
[17], 2, ram_memories
,
2507 ram_bases
, ram_sizes
, do_init
);
2508 /* External bus controller */
2509 ppc405_ebc_init(env
);
2510 /* DMA controller */
2511 dma_irqs
[0] = pic
[5];
2512 dma_irqs
[1] = pic
[6];
2513 dma_irqs
[2] = pic
[7];
2514 dma_irqs
[3] = pic
[8];
2515 ppc405_dma_init(env
, dma_irqs
);
2516 /* IIC controller */
2517 ppc405_i2c_init(0xef600500, pic
[2]);
2519 ppc405_gpio_init(0xef600700);
2521 if (serial_hds
[0] != NULL
) {
2522 serial_mm_init(address_space_mem
, 0xef600300, 0, pic
[0],
2523 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[0],
2526 if (serial_hds
[1] != NULL
) {
2527 serial_mm_init(address_space_mem
, 0xef600400, 0, pic
[1],
2528 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[1],
2532 ppc405_ocm_init(env
);
2534 gpt_irqs
[0] = pic
[19];
2535 gpt_irqs
[1] = pic
[20];
2536 gpt_irqs
[2] = pic
[21];
2537 gpt_irqs
[3] = pic
[22];
2538 gpt_irqs
[4] = pic
[23];
2539 ppc4xx_gpt_init(0xef600000, gpt_irqs
);
2541 /* Uses pic[3], pic[16], pic[18] */
2543 mal_irqs
[0] = pic
[11];
2544 mal_irqs
[1] = pic
[12];
2545 mal_irqs
[2] = pic
[13];
2546 mal_irqs
[3] = pic
[14];
2547 ppc405_mal_init(env
, mal_irqs
);
2549 /* Uses pic[9], pic[15], pic[17] */
2551 ppc405ep_cpc_init(env
, clk_setup
, sysclk
);