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 vmstate_register_ram_global(&ocm
->isarc_ram
);
984 memory_region_init_alias(&ocm
->dsarc_ram
, NULL
, "ppc405.dsarc", &ocm
->isarc_ram
,
986 qemu_register_reset(&ocm_reset
, ocm
);
987 ppc_dcr_register(env
, OCM0_ISARC
,
988 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
989 ppc_dcr_register(env
, OCM0_ISACNTL
,
990 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
991 ppc_dcr_register(env
, OCM0_DSARC
,
992 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
993 ppc_dcr_register(env
, OCM0_DSACNTL
,
994 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
997 /*****************************************************************************/
999 typedef struct ppc4xx_i2c_t ppc4xx_i2c_t
;
1000 struct ppc4xx_i2c_t
{
1020 static uint32_t ppc4xx_i2c_readb (void *opaque
, hwaddr addr
)
1026 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1031 // i2c_readbyte(&i2c->mdata);
1071 ret
= i2c
->xtcntlss
;
1074 ret
= i2c
->directcntl
;
1081 printf("%s: addr " TARGET_FMT_plx
" %02" PRIx32
"\n", __func__
, addr
, ret
);
1087 static void ppc4xx_i2c_writeb (void *opaque
,
1088 hwaddr addr
, uint32_t value
)
1093 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1100 // i2c_sendbyte(&i2c->mdata);
1115 i2c
->mdcntl
= value
& 0xDF;
1118 i2c
->sts
&= ~(value
& 0x0A);
1121 i2c
->extsts
&= ~(value
& 0x8F);
1130 i2c
->clkdiv
= value
;
1133 i2c
->intrmsk
= value
;
1136 i2c
->xfrcnt
= value
& 0x77;
1139 i2c
->xtcntlss
= value
;
1142 i2c
->directcntl
= value
& 0x7;
1147 static uint32_t ppc4xx_i2c_readw (void *opaque
, hwaddr addr
)
1152 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1154 ret
= ppc4xx_i2c_readb(opaque
, addr
) << 8;
1155 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 1);
1160 static void ppc4xx_i2c_writew (void *opaque
,
1161 hwaddr addr
, uint32_t value
)
1164 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1167 ppc4xx_i2c_writeb(opaque
, addr
, value
>> 8);
1168 ppc4xx_i2c_writeb(opaque
, addr
+ 1, value
);
1171 static uint32_t ppc4xx_i2c_readl (void *opaque
, hwaddr addr
)
1176 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1178 ret
= ppc4xx_i2c_readb(opaque
, addr
) << 24;
1179 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 1) << 16;
1180 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 2) << 8;
1181 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 3);
1186 static void ppc4xx_i2c_writel (void *opaque
,
1187 hwaddr addr
, uint32_t value
)
1190 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1193 ppc4xx_i2c_writeb(opaque
, addr
, value
>> 24);
1194 ppc4xx_i2c_writeb(opaque
, addr
+ 1, value
>> 16);
1195 ppc4xx_i2c_writeb(opaque
, addr
+ 2, value
>> 8);
1196 ppc4xx_i2c_writeb(opaque
, addr
+ 3, value
);
1199 static const MemoryRegionOps i2c_ops
= {
1201 .read
= { ppc4xx_i2c_readb
, ppc4xx_i2c_readw
, ppc4xx_i2c_readl
, },
1202 .write
= { ppc4xx_i2c_writeb
, ppc4xx_i2c_writew
, ppc4xx_i2c_writel
, },
1204 .endianness
= DEVICE_NATIVE_ENDIAN
,
1207 static void ppc4xx_i2c_reset (void *opaque
)
1220 i2c
->directcntl
= 0x0F;
1223 static void ppc405_i2c_init(hwaddr base
, qemu_irq irq
)
1227 i2c
= g_malloc0(sizeof(ppc4xx_i2c_t
));
1230 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1232 memory_region_init_io(&i2c
->iomem
, NULL
, &i2c_ops
, i2c
, "i2c", 0x011);
1233 memory_region_add_subregion(get_system_memory(), base
, &i2c
->iomem
);
1234 qemu_register_reset(ppc4xx_i2c_reset
, i2c
);
1237 /*****************************************************************************/
1238 /* General purpose timers */
1239 typedef struct ppc4xx_gpt_t ppc4xx_gpt_t
;
1240 struct ppc4xx_gpt_t
{
1255 static uint32_t ppc4xx_gpt_readb (void *opaque
, hwaddr addr
)
1258 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1260 /* XXX: generate a bus fault */
1264 static void ppc4xx_gpt_writeb (void *opaque
,
1265 hwaddr addr
, uint32_t value
)
1268 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1271 /* XXX: generate a bus fault */
1274 static uint32_t ppc4xx_gpt_readw (void *opaque
, hwaddr addr
)
1277 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1279 /* XXX: generate a bus fault */
1283 static void ppc4xx_gpt_writew (void *opaque
,
1284 hwaddr addr
, uint32_t value
)
1287 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1290 /* XXX: generate a bus fault */
1293 static int ppc4xx_gpt_compare (ppc4xx_gpt_t
*gpt
, int n
)
1299 static void ppc4xx_gpt_set_output (ppc4xx_gpt_t
*gpt
, int n
, int level
)
1304 static void ppc4xx_gpt_set_outputs (ppc4xx_gpt_t
*gpt
)
1310 for (i
= 0; i
< 5; i
++) {
1311 if (gpt
->oe
& mask
) {
1312 /* Output is enabled */
1313 if (ppc4xx_gpt_compare(gpt
, i
)) {
1314 /* Comparison is OK */
1315 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
);
1317 /* Comparison is KO */
1318 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
? 0 : 1);
1325 static void ppc4xx_gpt_set_irqs (ppc4xx_gpt_t
*gpt
)
1331 for (i
= 0; i
< 5; i
++) {
1332 if (gpt
->is
& gpt
->im
& mask
)
1333 qemu_irq_raise(gpt
->irqs
[i
]);
1335 qemu_irq_lower(gpt
->irqs
[i
]);
1340 static void ppc4xx_gpt_compute_timer (ppc4xx_gpt_t
*gpt
)
1345 static uint32_t ppc4xx_gpt_readl (void *opaque
, hwaddr addr
)
1352 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1357 /* Time base counter */
1358 ret
= muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + gpt
->tb_offset
,
1359 gpt
->tb_freq
, NANOSECONDS_PER_SECOND
);
1370 /* Interrupt mask */
1375 /* Interrupt status */
1379 /* Interrupt enable */
1384 idx
= (addr
- 0x80) >> 2;
1385 ret
= gpt
->comp
[idx
];
1389 idx
= (addr
- 0xC0) >> 2;
1390 ret
= gpt
->mask
[idx
];
1400 static void ppc4xx_gpt_writel (void *opaque
,
1401 hwaddr addr
, uint32_t value
)
1407 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1413 /* Time base counter */
1414 gpt
->tb_offset
= muldiv64(value
, NANOSECONDS_PER_SECOND
, gpt
->tb_freq
)
1415 - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1416 ppc4xx_gpt_compute_timer(gpt
);
1420 gpt
->oe
= value
& 0xF8000000;
1421 ppc4xx_gpt_set_outputs(gpt
);
1425 gpt
->ol
= value
& 0xF8000000;
1426 ppc4xx_gpt_set_outputs(gpt
);
1429 /* Interrupt mask */
1430 gpt
->im
= value
& 0x0000F800;
1433 /* Interrupt status set */
1434 gpt
->is
|= value
& 0x0000F800;
1435 ppc4xx_gpt_set_irqs(gpt
);
1438 /* Interrupt status clear */
1439 gpt
->is
&= ~(value
& 0x0000F800);
1440 ppc4xx_gpt_set_irqs(gpt
);
1443 /* Interrupt enable */
1444 gpt
->ie
= value
& 0x0000F800;
1445 ppc4xx_gpt_set_irqs(gpt
);
1449 idx
= (addr
- 0x80) >> 2;
1450 gpt
->comp
[idx
] = value
& 0xF8000000;
1451 ppc4xx_gpt_compute_timer(gpt
);
1455 idx
= (addr
- 0xC0) >> 2;
1456 gpt
->mask
[idx
] = value
& 0xF8000000;
1457 ppc4xx_gpt_compute_timer(gpt
);
1462 static const MemoryRegionOps gpt_ops
= {
1464 .read
= { ppc4xx_gpt_readb
, ppc4xx_gpt_readw
, ppc4xx_gpt_readl
, },
1465 .write
= { ppc4xx_gpt_writeb
, ppc4xx_gpt_writew
, ppc4xx_gpt_writel
, },
1467 .endianness
= DEVICE_NATIVE_ENDIAN
,
1470 static void ppc4xx_gpt_cb (void *opaque
)
1475 ppc4xx_gpt_set_irqs(gpt
);
1476 ppc4xx_gpt_set_outputs(gpt
);
1477 ppc4xx_gpt_compute_timer(gpt
);
1480 static void ppc4xx_gpt_reset (void *opaque
)
1486 timer_del(gpt
->timer
);
1487 gpt
->oe
= 0x00000000;
1488 gpt
->ol
= 0x00000000;
1489 gpt
->im
= 0x00000000;
1490 gpt
->is
= 0x00000000;
1491 gpt
->ie
= 0x00000000;
1492 for (i
= 0; i
< 5; i
++) {
1493 gpt
->comp
[i
] = 0x00000000;
1494 gpt
->mask
[i
] = 0x00000000;
1498 static void ppc4xx_gpt_init(hwaddr base
, qemu_irq irqs
[5])
1503 gpt
= g_malloc0(sizeof(ppc4xx_gpt_t
));
1504 for (i
= 0; i
< 5; i
++) {
1505 gpt
->irqs
[i
] = irqs
[i
];
1507 gpt
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &ppc4xx_gpt_cb
, gpt
);
1509 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1511 memory_region_init_io(&gpt
->iomem
, NULL
, &gpt_ops
, gpt
, "gpt", 0x0d4);
1512 memory_region_add_subregion(get_system_memory(), base
, &gpt
->iomem
);
1513 qemu_register_reset(ppc4xx_gpt_reset
, gpt
);
1516 /*****************************************************************************/
1522 MAL0_TXCASR
= 0x184,
1523 MAL0_TXCARR
= 0x185,
1524 MAL0_TXEOBISR
= 0x186,
1525 MAL0_TXDEIR
= 0x187,
1526 MAL0_RXCASR
= 0x190,
1527 MAL0_RXCARR
= 0x191,
1528 MAL0_RXEOBISR
= 0x192,
1529 MAL0_RXDEIR
= 0x193,
1530 MAL0_TXCTP0R
= 0x1A0,
1531 MAL0_TXCTP1R
= 0x1A1,
1532 MAL0_TXCTP2R
= 0x1A2,
1533 MAL0_TXCTP3R
= 0x1A3,
1534 MAL0_RXCTP0R
= 0x1C0,
1535 MAL0_RXCTP1R
= 0x1C1,
1540 typedef struct ppc40x_mal_t ppc40x_mal_t
;
1541 struct ppc40x_mal_t
{
1559 static void ppc40x_mal_reset (void *opaque
);
1561 static uint32_t dcr_read_mal (void *opaque
, int dcrn
)
1584 ret
= mal
->txeobisr
;
1596 ret
= mal
->rxeobisr
;
1602 ret
= mal
->txctpr
[0];
1605 ret
= mal
->txctpr
[1];
1608 ret
= mal
->txctpr
[2];
1611 ret
= mal
->txctpr
[3];
1614 ret
= mal
->rxctpr
[0];
1617 ret
= mal
->rxctpr
[1];
1633 static void dcr_write_mal (void *opaque
, int dcrn
, uint32_t val
)
1641 if (val
& 0x80000000)
1642 ppc40x_mal_reset(mal
);
1643 mal
->cfg
= val
& 0x00FFC087;
1650 mal
->ier
= val
& 0x0000001F;
1653 mal
->txcasr
= val
& 0xF0000000;
1656 mal
->txcarr
= val
& 0xF0000000;
1660 mal
->txeobisr
&= ~val
;
1664 mal
->txdeir
&= ~val
;
1667 mal
->rxcasr
= val
& 0xC0000000;
1670 mal
->rxcarr
= val
& 0xC0000000;
1674 mal
->rxeobisr
&= ~val
;
1678 mal
->rxdeir
&= ~val
;
1692 mal
->txctpr
[idx
] = val
;
1700 mal
->rxctpr
[idx
] = val
;
1704 goto update_rx_size
;
1708 mal
->rcbs
[idx
] = val
& 0x000000FF;
1713 static void ppc40x_mal_reset (void *opaque
)
1718 mal
->cfg
= 0x0007C000;
1719 mal
->esr
= 0x00000000;
1720 mal
->ier
= 0x00000000;
1721 mal
->rxcasr
= 0x00000000;
1722 mal
->rxdeir
= 0x00000000;
1723 mal
->rxeobisr
= 0x00000000;
1724 mal
->txcasr
= 0x00000000;
1725 mal
->txdeir
= 0x00000000;
1726 mal
->txeobisr
= 0x00000000;
1729 static void ppc405_mal_init(CPUPPCState
*env
, qemu_irq irqs
[4])
1734 mal
= g_malloc0(sizeof(ppc40x_mal_t
));
1735 for (i
= 0; i
< 4; i
++)
1736 mal
->irqs
[i
] = irqs
[i
];
1737 qemu_register_reset(&ppc40x_mal_reset
, mal
);
1738 ppc_dcr_register(env
, MAL0_CFG
,
1739 mal
, &dcr_read_mal
, &dcr_write_mal
);
1740 ppc_dcr_register(env
, MAL0_ESR
,
1741 mal
, &dcr_read_mal
, &dcr_write_mal
);
1742 ppc_dcr_register(env
, MAL0_IER
,
1743 mal
, &dcr_read_mal
, &dcr_write_mal
);
1744 ppc_dcr_register(env
, MAL0_TXCASR
,
1745 mal
, &dcr_read_mal
, &dcr_write_mal
);
1746 ppc_dcr_register(env
, MAL0_TXCARR
,
1747 mal
, &dcr_read_mal
, &dcr_write_mal
);
1748 ppc_dcr_register(env
, MAL0_TXEOBISR
,
1749 mal
, &dcr_read_mal
, &dcr_write_mal
);
1750 ppc_dcr_register(env
, MAL0_TXDEIR
,
1751 mal
, &dcr_read_mal
, &dcr_write_mal
);
1752 ppc_dcr_register(env
, MAL0_RXCASR
,
1753 mal
, &dcr_read_mal
, &dcr_write_mal
);
1754 ppc_dcr_register(env
, MAL0_RXCARR
,
1755 mal
, &dcr_read_mal
, &dcr_write_mal
);
1756 ppc_dcr_register(env
, MAL0_RXEOBISR
,
1757 mal
, &dcr_read_mal
, &dcr_write_mal
);
1758 ppc_dcr_register(env
, MAL0_RXDEIR
,
1759 mal
, &dcr_read_mal
, &dcr_write_mal
);
1760 ppc_dcr_register(env
, MAL0_TXCTP0R
,
1761 mal
, &dcr_read_mal
, &dcr_write_mal
);
1762 ppc_dcr_register(env
, MAL0_TXCTP1R
,
1763 mal
, &dcr_read_mal
, &dcr_write_mal
);
1764 ppc_dcr_register(env
, MAL0_TXCTP2R
,
1765 mal
, &dcr_read_mal
, &dcr_write_mal
);
1766 ppc_dcr_register(env
, MAL0_TXCTP3R
,
1767 mal
, &dcr_read_mal
, &dcr_write_mal
);
1768 ppc_dcr_register(env
, MAL0_RXCTP0R
,
1769 mal
, &dcr_read_mal
, &dcr_write_mal
);
1770 ppc_dcr_register(env
, MAL0_RXCTP1R
,
1771 mal
, &dcr_read_mal
, &dcr_write_mal
);
1772 ppc_dcr_register(env
, MAL0_RCBS0
,
1773 mal
, &dcr_read_mal
, &dcr_write_mal
);
1774 ppc_dcr_register(env
, MAL0_RCBS1
,
1775 mal
, &dcr_read_mal
, &dcr_write_mal
);
1778 /*****************************************************************************/
1780 void ppc40x_core_reset(PowerPCCPU
*cpu
)
1782 CPUPPCState
*env
= &cpu
->env
;
1785 printf("Reset PowerPC core\n");
1786 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_RESET
);
1787 dbsr
= env
->spr
[SPR_40x_DBSR
];
1788 dbsr
&= ~0x00000300;
1790 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1793 void ppc40x_chip_reset(PowerPCCPU
*cpu
)
1795 CPUPPCState
*env
= &cpu
->env
;
1798 printf("Reset PowerPC chip\n");
1799 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_RESET
);
1800 /* XXX: TODO reset all internal peripherals */
1801 dbsr
= env
->spr
[SPR_40x_DBSR
];
1802 dbsr
&= ~0x00000300;
1804 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1807 void ppc40x_system_reset(PowerPCCPU
*cpu
)
1809 printf("Reset PowerPC system\n");
1810 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
1813 void store_40x_dbcr0 (CPUPPCState
*env
, uint32_t val
)
1815 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1817 switch ((val
>> 28) & 0x3) {
1823 ppc40x_core_reset(cpu
);
1827 ppc40x_chip_reset(cpu
);
1831 ppc40x_system_reset(cpu
);
1836 /*****************************************************************************/
1839 PPC405CR_CPC0_PLLMR
= 0x0B0,
1840 PPC405CR_CPC0_CR0
= 0x0B1,
1841 PPC405CR_CPC0_CR1
= 0x0B2,
1842 PPC405CR_CPC0_PSR
= 0x0B4,
1843 PPC405CR_CPC0_JTAGID
= 0x0B5,
1844 PPC405CR_CPC0_ER
= 0x0B9,
1845 PPC405CR_CPC0_FR
= 0x0BA,
1846 PPC405CR_CPC0_SR
= 0x0BB,
1850 PPC405CR_CPU_CLK
= 0,
1851 PPC405CR_TMR_CLK
= 1,
1852 PPC405CR_PLB_CLK
= 2,
1853 PPC405CR_SDRAM_CLK
= 3,
1854 PPC405CR_OPB_CLK
= 4,
1855 PPC405CR_EXT_CLK
= 5,
1856 PPC405CR_UART_CLK
= 6,
1857 PPC405CR_CLK_NB
= 7,
1860 typedef struct ppc405cr_cpc_t ppc405cr_cpc_t
;
1861 struct ppc405cr_cpc_t
{
1862 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
1873 static void ppc405cr_clk_setup (ppc405cr_cpc_t
*cpc
)
1875 uint64_t VCO_out
, PLL_out
;
1876 uint32_t CPU_clk
, TMR_clk
, SDRAM_clk
, PLB_clk
, OPB_clk
, EXT_clk
, UART_clk
;
1879 D0
= ((cpc
->pllmr
>> 26) & 0x3) + 1; /* CBDV */
1880 if (cpc
->pllmr
& 0x80000000) {
1881 D1
= (((cpc
->pllmr
>> 20) - 1) & 0xF) + 1; /* FBDV */
1882 D2
= 8 - ((cpc
->pllmr
>> 16) & 0x7); /* FWDVA */
1884 VCO_out
= (uint64_t)cpc
->sysclk
* M
;
1885 if (VCO_out
< 400000000 || VCO_out
> 800000000) {
1886 /* PLL cannot lock */
1887 cpc
->pllmr
&= ~0x80000000;
1890 PLL_out
= VCO_out
/ D2
;
1895 PLL_out
= (uint64_t)cpc
->sysclk
* M
;
1898 if (cpc
->cr1
& 0x00800000)
1899 TMR_clk
= cpc
->sysclk
; /* Should have a separate clock */
1902 PLB_clk
= CPU_clk
/ D0
;
1903 SDRAM_clk
= PLB_clk
;
1904 D0
= ((cpc
->pllmr
>> 10) & 0x3) + 1;
1905 OPB_clk
= PLB_clk
/ D0
;
1906 D0
= ((cpc
->pllmr
>> 24) & 0x3) + 2;
1907 EXT_clk
= PLB_clk
/ D0
;
1908 D0
= ((cpc
->cr0
>> 1) & 0x1F) + 1;
1909 UART_clk
= CPU_clk
/ D0
;
1910 /* Setup CPU clocks */
1911 clk_setup(&cpc
->clk_setup
[PPC405CR_CPU_CLK
], CPU_clk
);
1912 /* Setup time-base clock */
1913 clk_setup(&cpc
->clk_setup
[PPC405CR_TMR_CLK
], TMR_clk
);
1914 /* Setup PLB clock */
1915 clk_setup(&cpc
->clk_setup
[PPC405CR_PLB_CLK
], PLB_clk
);
1916 /* Setup SDRAM clock */
1917 clk_setup(&cpc
->clk_setup
[PPC405CR_SDRAM_CLK
], SDRAM_clk
);
1918 /* Setup OPB clock */
1919 clk_setup(&cpc
->clk_setup
[PPC405CR_OPB_CLK
], OPB_clk
);
1920 /* Setup external clock */
1921 clk_setup(&cpc
->clk_setup
[PPC405CR_EXT_CLK
], EXT_clk
);
1922 /* Setup UART clock */
1923 clk_setup(&cpc
->clk_setup
[PPC405CR_UART_CLK
], UART_clk
);
1926 static uint32_t dcr_read_crcpc (void *opaque
, int dcrn
)
1928 ppc405cr_cpc_t
*cpc
;
1933 case PPC405CR_CPC0_PLLMR
:
1936 case PPC405CR_CPC0_CR0
:
1939 case PPC405CR_CPC0_CR1
:
1942 case PPC405CR_CPC0_PSR
:
1945 case PPC405CR_CPC0_JTAGID
:
1948 case PPC405CR_CPC0_ER
:
1951 case PPC405CR_CPC0_FR
:
1954 case PPC405CR_CPC0_SR
:
1955 ret
= ~(cpc
->er
| cpc
->fr
) & 0xFFFF0000;
1958 /* Avoid gcc warning */
1966 static void dcr_write_crcpc (void *opaque
, int dcrn
, uint32_t val
)
1968 ppc405cr_cpc_t
*cpc
;
1972 case PPC405CR_CPC0_PLLMR
:
1973 cpc
->pllmr
= val
& 0xFFF77C3F;
1975 case PPC405CR_CPC0_CR0
:
1976 cpc
->cr0
= val
& 0x0FFFFFFE;
1978 case PPC405CR_CPC0_CR1
:
1979 cpc
->cr1
= val
& 0x00800000;
1981 case PPC405CR_CPC0_PSR
:
1984 case PPC405CR_CPC0_JTAGID
:
1987 case PPC405CR_CPC0_ER
:
1988 cpc
->er
= val
& 0xBFFC0000;
1990 case PPC405CR_CPC0_FR
:
1991 cpc
->fr
= val
& 0xBFFC0000;
1993 case PPC405CR_CPC0_SR
:
1999 static void ppc405cr_cpc_reset (void *opaque
)
2001 ppc405cr_cpc_t
*cpc
;
2005 /* Compute PLLMR value from PSR settings */
2006 cpc
->pllmr
= 0x80000000;
2008 switch ((cpc
->psr
>> 30) & 3) {
2011 cpc
->pllmr
&= ~0x80000000;
2015 cpc
->pllmr
|= 5 << 16;
2019 cpc
->pllmr
|= 4 << 16;
2023 cpc
->pllmr
|= 2 << 16;
2027 D
= (cpc
->psr
>> 28) & 3;
2028 cpc
->pllmr
|= (D
+ 1) << 20;
2030 D
= (cpc
->psr
>> 25) & 7;
2045 D
= (cpc
->psr
>> 23) & 3;
2046 cpc
->pllmr
|= D
<< 26;
2048 D
= (cpc
->psr
>> 21) & 3;
2049 cpc
->pllmr
|= D
<< 10;
2051 D
= (cpc
->psr
>> 17) & 3;
2052 cpc
->pllmr
|= D
<< 24;
2053 cpc
->cr0
= 0x0000003C;
2054 cpc
->cr1
= 0x2B0D8800;
2055 cpc
->er
= 0x00000000;
2056 cpc
->fr
= 0x00000000;
2057 ppc405cr_clk_setup(cpc
);
2060 static void ppc405cr_clk_init (ppc405cr_cpc_t
*cpc
)
2064 /* XXX: this should be read from IO pins */
2065 cpc
->psr
= 0x00000000; /* 8 bits ROM */
2067 D
= 0x2; /* Divide by 4 */
2068 cpc
->psr
|= D
<< 30;
2070 D
= 0x1; /* Divide by 2 */
2071 cpc
->psr
|= D
<< 28;
2073 D
= 0x1; /* Divide by 2 */
2074 cpc
->psr
|= D
<< 23;
2076 D
= 0x5; /* M = 16 */
2077 cpc
->psr
|= D
<< 25;
2079 D
= 0x1; /* Divide by 2 */
2080 cpc
->psr
|= D
<< 21;
2082 D
= 0x2; /* Divide by 4 */
2083 cpc
->psr
|= D
<< 17;
2086 static void ppc405cr_cpc_init (CPUPPCState
*env
, clk_setup_t clk_setup
[7],
2089 ppc405cr_cpc_t
*cpc
;
2091 cpc
= g_malloc0(sizeof(ppc405cr_cpc_t
));
2092 memcpy(cpc
->clk_setup
, clk_setup
,
2093 PPC405CR_CLK_NB
* sizeof(clk_setup_t
));
2094 cpc
->sysclk
= sysclk
;
2095 cpc
->jtagid
= 0x42051049;
2096 ppc_dcr_register(env
, PPC405CR_CPC0_PSR
, cpc
,
2097 &dcr_read_crcpc
, &dcr_write_crcpc
);
2098 ppc_dcr_register(env
, PPC405CR_CPC0_CR0
, cpc
,
2099 &dcr_read_crcpc
, &dcr_write_crcpc
);
2100 ppc_dcr_register(env
, PPC405CR_CPC0_CR1
, cpc
,
2101 &dcr_read_crcpc
, &dcr_write_crcpc
);
2102 ppc_dcr_register(env
, PPC405CR_CPC0_JTAGID
, cpc
,
2103 &dcr_read_crcpc
, &dcr_write_crcpc
);
2104 ppc_dcr_register(env
, PPC405CR_CPC0_PLLMR
, cpc
,
2105 &dcr_read_crcpc
, &dcr_write_crcpc
);
2106 ppc_dcr_register(env
, PPC405CR_CPC0_ER
, cpc
,
2107 &dcr_read_crcpc
, &dcr_write_crcpc
);
2108 ppc_dcr_register(env
, PPC405CR_CPC0_FR
, cpc
,
2109 &dcr_read_crcpc
, &dcr_write_crcpc
);
2110 ppc_dcr_register(env
, PPC405CR_CPC0_SR
, cpc
,
2111 &dcr_read_crcpc
, &dcr_write_crcpc
);
2112 ppc405cr_clk_init(cpc
);
2113 qemu_register_reset(ppc405cr_cpc_reset
, cpc
);
2116 CPUPPCState
*ppc405cr_init(MemoryRegion
*address_space_mem
,
2117 MemoryRegion ram_memories
[4],
2118 hwaddr ram_bases
[4],
2119 hwaddr ram_sizes
[4],
2120 uint32_t sysclk
, qemu_irq
**picp
,
2123 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
2124 qemu_irq dma_irqs
[4];
2127 qemu_irq
*pic
, *irqs
;
2129 memset(clk_setup
, 0, sizeof(clk_setup
));
2130 cpu
= ppc4xx_init("405cr", &clk_setup
[PPC405CR_CPU_CLK
],
2131 &clk_setup
[PPC405CR_TMR_CLK
], sysclk
);
2133 /* Memory mapped devices registers */
2135 ppc4xx_plb_init(env
);
2136 /* PLB to OPB bridge */
2137 ppc4xx_pob_init(env
);
2139 ppc4xx_opba_init(0xef600600);
2140 /* Universal interrupt controller */
2141 irqs
= g_malloc0(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2142 irqs
[PPCUIC_OUTPUT_INT
] =
2143 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2144 irqs
[PPCUIC_OUTPUT_CINT
] =
2145 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2146 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2148 /* SDRAM controller */
2149 ppc4xx_sdram_init(env
, pic
[14], 1, ram_memories
,
2150 ram_bases
, ram_sizes
, do_init
);
2151 /* External bus controller */
2152 ppc405_ebc_init(env
);
2153 /* DMA controller */
2154 dma_irqs
[0] = pic
[26];
2155 dma_irqs
[1] = pic
[25];
2156 dma_irqs
[2] = pic
[24];
2157 dma_irqs
[3] = pic
[23];
2158 ppc405_dma_init(env
, dma_irqs
);
2160 if (serial_hds
[0] != NULL
) {
2161 serial_mm_init(address_space_mem
, 0xef600300, 0, pic
[0],
2162 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[0],
2165 if (serial_hds
[1] != NULL
) {
2166 serial_mm_init(address_space_mem
, 0xef600400, 0, pic
[1],
2167 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[1],
2170 /* IIC controller */
2171 ppc405_i2c_init(0xef600500, pic
[2]);
2173 ppc405_gpio_init(0xef600700);
2175 ppc405cr_cpc_init(env
, clk_setup
, sysclk
);
2180 /*****************************************************************************/
2184 PPC405EP_CPC0_PLLMR0
= 0x0F0,
2185 PPC405EP_CPC0_BOOT
= 0x0F1,
2186 PPC405EP_CPC0_EPCTL
= 0x0F3,
2187 PPC405EP_CPC0_PLLMR1
= 0x0F4,
2188 PPC405EP_CPC0_UCR
= 0x0F5,
2189 PPC405EP_CPC0_SRR
= 0x0F6,
2190 PPC405EP_CPC0_JTAGID
= 0x0F7,
2191 PPC405EP_CPC0_PCI
= 0x0F9,
2193 PPC405EP_CPC0_ER
= xxx
,
2194 PPC405EP_CPC0_FR
= xxx
,
2195 PPC405EP_CPC0_SR
= xxx
,
2200 PPC405EP_CPU_CLK
= 0,
2201 PPC405EP_PLB_CLK
= 1,
2202 PPC405EP_OPB_CLK
= 2,
2203 PPC405EP_EBC_CLK
= 3,
2204 PPC405EP_MAL_CLK
= 4,
2205 PPC405EP_PCI_CLK
= 5,
2206 PPC405EP_UART0_CLK
= 6,
2207 PPC405EP_UART1_CLK
= 7,
2208 PPC405EP_CLK_NB
= 8,
2211 typedef struct ppc405ep_cpc_t ppc405ep_cpc_t
;
2212 struct ppc405ep_cpc_t
{
2214 clk_setup_t clk_setup
[PPC405EP_CLK_NB
];
2222 /* Clock and power management */
2228 static void ppc405ep_compute_clocks (ppc405ep_cpc_t
*cpc
)
2230 uint32_t CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
;
2231 uint32_t UART0_clk
, UART1_clk
;
2232 uint64_t VCO_out
, PLL_out
;
2236 if ((cpc
->pllmr
[1] & 0x80000000) && !(cpc
->pllmr
[1] & 0x40000000)) {
2237 M
= (((cpc
->pllmr
[1] >> 20) - 1) & 0xF) + 1; /* FBMUL */
2238 #ifdef DEBUG_CLOCKS_LL
2239 printf("FBMUL %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 20) & 0xF, M
);
2241 D
= 8 - ((cpc
->pllmr
[1] >> 16) & 0x7); /* FWDA */
2242 #ifdef DEBUG_CLOCKS_LL
2243 printf("FWDA %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 16) & 0x7, D
);
2245 VCO_out
= (uint64_t)cpc
->sysclk
* M
* D
;
2246 if (VCO_out
< 500000000UL || VCO_out
> 1000000000UL) {
2247 /* Error - unlock the PLL */
2248 printf("VCO out of range %" PRIu64
"\n", VCO_out
);
2250 cpc
->pllmr
[1] &= ~0x80000000;
2254 PLL_out
= VCO_out
/ D
;
2255 /* Pretend the PLL is locked */
2256 cpc
->boot
|= 0x00000001;
2261 PLL_out
= cpc
->sysclk
;
2262 if (cpc
->pllmr
[1] & 0x40000000) {
2263 /* Pretend the PLL is not locked */
2264 cpc
->boot
&= ~0x00000001;
2267 /* Now, compute all other clocks */
2268 D
= ((cpc
->pllmr
[0] >> 20) & 0x3) + 1; /* CCDV */
2269 #ifdef DEBUG_CLOCKS_LL
2270 printf("CCDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 20) & 0x3, D
);
2272 CPU_clk
= PLL_out
/ D
;
2273 D
= ((cpc
->pllmr
[0] >> 16) & 0x3) + 1; /* CBDV */
2274 #ifdef DEBUG_CLOCKS_LL
2275 printf("CBDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 16) & 0x3, D
);
2277 PLB_clk
= CPU_clk
/ D
;
2278 D
= ((cpc
->pllmr
[0] >> 12) & 0x3) + 1; /* OPDV */
2279 #ifdef DEBUG_CLOCKS_LL
2280 printf("OPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 12) & 0x3, D
);
2282 OPB_clk
= PLB_clk
/ D
;
2283 D
= ((cpc
->pllmr
[0] >> 8) & 0x3) + 2; /* EPDV */
2284 #ifdef DEBUG_CLOCKS_LL
2285 printf("EPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 8) & 0x3, D
);
2287 EBC_clk
= PLB_clk
/ D
;
2288 D
= ((cpc
->pllmr
[0] >> 4) & 0x3) + 1; /* MPDV */
2289 #ifdef DEBUG_CLOCKS_LL
2290 printf("MPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 4) & 0x3, D
);
2292 MAL_clk
= PLB_clk
/ D
;
2293 D
= (cpc
->pllmr
[0] & 0x3) + 1; /* PPDV */
2294 #ifdef DEBUG_CLOCKS_LL
2295 printf("PPDV %01" PRIx32
" %d\n", cpc
->pllmr
[0] & 0x3, D
);
2297 PCI_clk
= PLB_clk
/ D
;
2298 D
= ((cpc
->ucr
- 1) & 0x7F) + 1; /* U0DIV */
2299 #ifdef DEBUG_CLOCKS_LL
2300 printf("U0DIV %01" PRIx32
" %d\n", cpc
->ucr
& 0x7F, D
);
2302 UART0_clk
= PLL_out
/ D
;
2303 D
= (((cpc
->ucr
>> 8) - 1) & 0x7F) + 1; /* U1DIV */
2304 #ifdef DEBUG_CLOCKS_LL
2305 printf("U1DIV %01" PRIx32
" %d\n", (cpc
->ucr
>> 8) & 0x7F, D
);
2307 UART1_clk
= PLL_out
/ D
;
2309 printf("Setup PPC405EP clocks - sysclk %" PRIu32
" VCO %" PRIu64
2310 " PLL out %" PRIu64
" Hz\n", cpc
->sysclk
, VCO_out
, PLL_out
);
2311 printf("CPU %" PRIu32
" PLB %" PRIu32
" OPB %" PRIu32
" EBC %" PRIu32
2312 " MAL %" PRIu32
" PCI %" PRIu32
" UART0 %" PRIu32
2313 " UART1 %" PRIu32
"\n",
2314 CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
,
2315 UART0_clk
, UART1_clk
);
2317 /* Setup CPU clocks */
2318 clk_setup(&cpc
->clk_setup
[PPC405EP_CPU_CLK
], CPU_clk
);
2319 /* Setup PLB clock */
2320 clk_setup(&cpc
->clk_setup
[PPC405EP_PLB_CLK
], PLB_clk
);
2321 /* Setup OPB clock */
2322 clk_setup(&cpc
->clk_setup
[PPC405EP_OPB_CLK
], OPB_clk
);
2323 /* Setup external clock */
2324 clk_setup(&cpc
->clk_setup
[PPC405EP_EBC_CLK
], EBC_clk
);
2325 /* Setup MAL clock */
2326 clk_setup(&cpc
->clk_setup
[PPC405EP_MAL_CLK
], MAL_clk
);
2327 /* Setup PCI clock */
2328 clk_setup(&cpc
->clk_setup
[PPC405EP_PCI_CLK
], PCI_clk
);
2329 /* Setup UART0 clock */
2330 clk_setup(&cpc
->clk_setup
[PPC405EP_UART0_CLK
], UART0_clk
);
2331 /* Setup UART1 clock */
2332 clk_setup(&cpc
->clk_setup
[PPC405EP_UART1_CLK
], UART1_clk
);
2335 static uint32_t dcr_read_epcpc (void *opaque
, int dcrn
)
2337 ppc405ep_cpc_t
*cpc
;
2342 case PPC405EP_CPC0_BOOT
:
2345 case PPC405EP_CPC0_EPCTL
:
2348 case PPC405EP_CPC0_PLLMR0
:
2349 ret
= cpc
->pllmr
[0];
2351 case PPC405EP_CPC0_PLLMR1
:
2352 ret
= cpc
->pllmr
[1];
2354 case PPC405EP_CPC0_UCR
:
2357 case PPC405EP_CPC0_SRR
:
2360 case PPC405EP_CPC0_JTAGID
:
2363 case PPC405EP_CPC0_PCI
:
2367 /* Avoid gcc warning */
2375 static void dcr_write_epcpc (void *opaque
, int dcrn
, uint32_t val
)
2377 ppc405ep_cpc_t
*cpc
;
2381 case PPC405EP_CPC0_BOOT
:
2382 /* Read-only register */
2384 case PPC405EP_CPC0_EPCTL
:
2385 /* Don't care for now */
2386 cpc
->epctl
= val
& 0xC00000F3;
2388 case PPC405EP_CPC0_PLLMR0
:
2389 cpc
->pllmr
[0] = val
& 0x00633333;
2390 ppc405ep_compute_clocks(cpc
);
2392 case PPC405EP_CPC0_PLLMR1
:
2393 cpc
->pllmr
[1] = val
& 0xC0F73FFF;
2394 ppc405ep_compute_clocks(cpc
);
2396 case PPC405EP_CPC0_UCR
:
2397 /* UART control - don't care for now */
2398 cpc
->ucr
= val
& 0x003F7F7F;
2400 case PPC405EP_CPC0_SRR
:
2403 case PPC405EP_CPC0_JTAGID
:
2406 case PPC405EP_CPC0_PCI
:
2412 static void ppc405ep_cpc_reset (void *opaque
)
2414 ppc405ep_cpc_t
*cpc
= opaque
;
2416 cpc
->boot
= 0x00000010; /* Boot from PCI - IIC EEPROM disabled */
2417 cpc
->epctl
= 0x00000000;
2418 cpc
->pllmr
[0] = 0x00011010;
2419 cpc
->pllmr
[1] = 0x40000000;
2420 cpc
->ucr
= 0x00000000;
2421 cpc
->srr
= 0x00040000;
2422 cpc
->pci
= 0x00000000;
2423 cpc
->er
= 0x00000000;
2424 cpc
->fr
= 0x00000000;
2425 cpc
->sr
= 0x00000000;
2426 ppc405ep_compute_clocks(cpc
);
2429 /* XXX: sysclk should be between 25 and 100 MHz */
2430 static void ppc405ep_cpc_init (CPUPPCState
*env
, clk_setup_t clk_setup
[8],
2433 ppc405ep_cpc_t
*cpc
;
2435 cpc
= g_malloc0(sizeof(ppc405ep_cpc_t
));
2436 memcpy(cpc
->clk_setup
, clk_setup
,
2437 PPC405EP_CLK_NB
* sizeof(clk_setup_t
));
2438 cpc
->jtagid
= 0x20267049;
2439 cpc
->sysclk
= sysclk
;
2440 qemu_register_reset(&ppc405ep_cpc_reset
, cpc
);
2441 ppc_dcr_register(env
, PPC405EP_CPC0_BOOT
, cpc
,
2442 &dcr_read_epcpc
, &dcr_write_epcpc
);
2443 ppc_dcr_register(env
, PPC405EP_CPC0_EPCTL
, cpc
,
2444 &dcr_read_epcpc
, &dcr_write_epcpc
);
2445 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR0
, cpc
,
2446 &dcr_read_epcpc
, &dcr_write_epcpc
);
2447 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR1
, cpc
,
2448 &dcr_read_epcpc
, &dcr_write_epcpc
);
2449 ppc_dcr_register(env
, PPC405EP_CPC0_UCR
, cpc
,
2450 &dcr_read_epcpc
, &dcr_write_epcpc
);
2451 ppc_dcr_register(env
, PPC405EP_CPC0_SRR
, cpc
,
2452 &dcr_read_epcpc
, &dcr_write_epcpc
);
2453 ppc_dcr_register(env
, PPC405EP_CPC0_JTAGID
, cpc
,
2454 &dcr_read_epcpc
, &dcr_write_epcpc
);
2455 ppc_dcr_register(env
, PPC405EP_CPC0_PCI
, cpc
,
2456 &dcr_read_epcpc
, &dcr_write_epcpc
);
2458 ppc_dcr_register(env
, PPC405EP_CPC0_ER
, cpc
,
2459 &dcr_read_epcpc
, &dcr_write_epcpc
);
2460 ppc_dcr_register(env
, PPC405EP_CPC0_FR
, cpc
,
2461 &dcr_read_epcpc
, &dcr_write_epcpc
);
2462 ppc_dcr_register(env
, PPC405EP_CPC0_SR
, cpc
,
2463 &dcr_read_epcpc
, &dcr_write_epcpc
);
2467 CPUPPCState
*ppc405ep_init(MemoryRegion
*address_space_mem
,
2468 MemoryRegion ram_memories
[2],
2469 hwaddr ram_bases
[2],
2470 hwaddr ram_sizes
[2],
2471 uint32_t sysclk
, qemu_irq
**picp
,
2474 clk_setup_t clk_setup
[PPC405EP_CLK_NB
], tlb_clk_setup
;
2475 qemu_irq dma_irqs
[4], gpt_irqs
[5], mal_irqs
[4];
2478 qemu_irq
*pic
, *irqs
;
2480 memset(clk_setup
, 0, sizeof(clk_setup
));
2482 cpu
= ppc4xx_init("405ep", &clk_setup
[PPC405EP_CPU_CLK
],
2483 &tlb_clk_setup
, sysclk
);
2485 clk_setup
[PPC405EP_CPU_CLK
].cb
= tlb_clk_setup
.cb
;
2486 clk_setup
[PPC405EP_CPU_CLK
].opaque
= tlb_clk_setup
.opaque
;
2487 /* Internal devices init */
2488 /* Memory mapped devices registers */
2490 ppc4xx_plb_init(env
);
2491 /* PLB to OPB bridge */
2492 ppc4xx_pob_init(env
);
2494 ppc4xx_opba_init(0xef600600);
2495 /* Initialize timers */
2496 ppc_booke_timers_init(cpu
, sysclk
, 0);
2497 /* Universal interrupt controller */
2498 irqs
= g_malloc0(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2499 irqs
[PPCUIC_OUTPUT_INT
] =
2500 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2501 irqs
[PPCUIC_OUTPUT_CINT
] =
2502 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2503 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2505 /* SDRAM controller */
2506 /* XXX 405EP has no ECC interrupt */
2507 ppc4xx_sdram_init(env
, pic
[17], 2, ram_memories
,
2508 ram_bases
, ram_sizes
, do_init
);
2509 /* External bus controller */
2510 ppc405_ebc_init(env
);
2511 /* DMA controller */
2512 dma_irqs
[0] = pic
[5];
2513 dma_irqs
[1] = pic
[6];
2514 dma_irqs
[2] = pic
[7];
2515 dma_irqs
[3] = pic
[8];
2516 ppc405_dma_init(env
, dma_irqs
);
2517 /* IIC controller */
2518 ppc405_i2c_init(0xef600500, pic
[2]);
2520 ppc405_gpio_init(0xef600700);
2522 if (serial_hds
[0] != NULL
) {
2523 serial_mm_init(address_space_mem
, 0xef600300, 0, pic
[0],
2524 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[0],
2527 if (serial_hds
[1] != NULL
) {
2528 serial_mm_init(address_space_mem
, 0xef600400, 0, pic
[1],
2529 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[1],
2533 ppc405_ocm_init(env
);
2535 gpt_irqs
[0] = pic
[19];
2536 gpt_irqs
[1] = pic
[20];
2537 gpt_irqs
[2] = pic
[21];
2538 gpt_irqs
[3] = pic
[22];
2539 gpt_irqs
[4] = pic
[23];
2540 ppc4xx_gpt_init(0xef600000, gpt_irqs
);
2542 /* Uses pic[3], pic[16], pic[18] */
2544 mal_irqs
[0] = pic
[11];
2545 mal_irqs
[1] = pic
[12];
2546 mal_irqs
[2] = pic
[13];
2547 mal_irqs
[3] = pic
[14];
2548 ppc405_mal_init(env
, mal_irqs
);
2550 /* Uses pic[9], pic[15], pic[17] */
2552 ppc405ep_cpc_init(env
, clk_setup
, sysclk
);