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 "qemu/units.h"
26 #include "qapi/error.h"
27 #include "qemu-common.h"
30 #include "hw/ppc/ppc.h"
31 #include "hw/boards.h"
32 #include "hw/i2c/ppc4xx_i2c.h"
34 #include "hw/char/serial.h"
35 #include "qemu/timer.h"
36 #include "sysemu/sysemu.h"
38 #include "exec/address-spaces.h"
43 //#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 */
117 typedef struct ppc4xx_plb_t ppc4xx_plb_t
;
118 struct ppc4xx_plb_t
{
124 static uint32_t dcr_read_plb (void *opaque
, int dcrn
)
141 /* Avoid gcc warning */
149 static void dcr_write_plb (void *opaque
, int dcrn
, uint32_t val
)
156 /* We don't care about the actual parameters written as
157 * we don't manage any priorities on the bus
159 plb
->acr
= val
& 0xF8000000;
171 static void ppc4xx_plb_reset (void *opaque
)
176 plb
->acr
= 0x00000000;
177 plb
->bear
= 0x00000000;
178 plb
->besr
= 0x00000000;
181 void ppc4xx_plb_init(CPUPPCState
*env
)
185 plb
= g_malloc0(sizeof(ppc4xx_plb_t
));
186 ppc_dcr_register(env
, PLB3A0_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
187 ppc_dcr_register(env
, PLB4A0_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
188 ppc_dcr_register(env
, PLB0_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
189 ppc_dcr_register(env
, PLB0_BEAR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
190 ppc_dcr_register(env
, PLB0_BESR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
191 ppc_dcr_register(env
, PLB4A1_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
192 qemu_register_reset(ppc4xx_plb_reset
, plb
);
195 /*****************************************************************************/
196 /* PLB to OPB bridge */
203 typedef struct ppc4xx_pob_t ppc4xx_pob_t
;
204 struct ppc4xx_pob_t
{
210 static uint32_t dcr_read_pob (void *opaque
, int dcrn
)
227 /* Avoid gcc warning */
235 static void dcr_write_pob (void *opaque
, int dcrn
, uint32_t val
)
255 static void ppc4xx_pob_reset (void *opaque
)
261 pob
->bear
= 0x00000000;
262 pob
->besr0
= 0x0000000;
263 pob
->besr1
= 0x0000000;
266 static void ppc4xx_pob_init(CPUPPCState
*env
)
270 pob
= g_malloc0(sizeof(ppc4xx_pob_t
));
271 ppc_dcr_register(env
, POB0_BEAR
, pob
, &dcr_read_pob
, &dcr_write_pob
);
272 ppc_dcr_register(env
, POB0_BESR0
, pob
, &dcr_read_pob
, &dcr_write_pob
);
273 ppc_dcr_register(env
, POB0_BESR1
, pob
, &dcr_read_pob
, &dcr_write_pob
);
274 qemu_register_reset(ppc4xx_pob_reset
, pob
);
277 /*****************************************************************************/
279 typedef struct ppc4xx_opba_t ppc4xx_opba_t
;
280 struct ppc4xx_opba_t
{
286 static uint32_t opba_readb (void *opaque
, hwaddr addr
)
292 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
310 static void opba_writeb (void *opaque
,
311 hwaddr addr
, uint32_t value
)
316 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
322 opba
->cr
= value
& 0xF8;
325 opba
->pr
= value
& 0xFF;
332 static uint32_t opba_readw (void *opaque
, hwaddr addr
)
337 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
339 ret
= opba_readb(opaque
, addr
) << 8;
340 ret
|= opba_readb(opaque
, addr
+ 1);
345 static void opba_writew (void *opaque
,
346 hwaddr addr
, uint32_t value
)
349 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
352 opba_writeb(opaque
, addr
, value
>> 8);
353 opba_writeb(opaque
, addr
+ 1, value
);
356 static uint32_t opba_readl (void *opaque
, hwaddr addr
)
361 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
363 ret
= opba_readb(opaque
, addr
) << 24;
364 ret
|= opba_readb(opaque
, addr
+ 1) << 16;
369 static void opba_writel (void *opaque
,
370 hwaddr addr
, uint32_t value
)
373 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
376 opba_writeb(opaque
, addr
, value
>> 24);
377 opba_writeb(opaque
, addr
+ 1, value
>> 16);
380 static const MemoryRegionOps opba_ops
= {
382 .read
= { opba_readb
, opba_readw
, opba_readl
, },
383 .write
= { opba_writeb
, opba_writew
, opba_writel
, },
385 .endianness
= DEVICE_NATIVE_ENDIAN
,
388 static void ppc4xx_opba_reset (void *opaque
)
393 opba
->cr
= 0x00; /* No dynamic priorities - park disabled */
397 static void ppc4xx_opba_init(hwaddr base
)
401 opba
= g_malloc0(sizeof(ppc4xx_opba_t
));
403 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
405 memory_region_init_io(&opba
->io
, NULL
, &opba_ops
, opba
, "opba", 0x002);
406 memory_region_add_subregion(get_system_memory(), base
, &opba
->io
);
407 qemu_register_reset(ppc4xx_opba_reset
, opba
);
410 /*****************************************************************************/
411 /* Code decompression controller */
414 /*****************************************************************************/
415 /* Peripheral controller */
416 typedef struct ppc4xx_ebc_t ppc4xx_ebc_t
;
417 struct ppc4xx_ebc_t
{
428 EBC0_CFGADDR
= 0x012,
429 EBC0_CFGDATA
= 0x013,
432 static uint32_t dcr_read_ebc (void *opaque
, int dcrn
)
444 case 0x00: /* B0CR */
447 case 0x01: /* B1CR */
450 case 0x02: /* B2CR */
453 case 0x03: /* B3CR */
456 case 0x04: /* B4CR */
459 case 0x05: /* B5CR */
462 case 0x06: /* B6CR */
465 case 0x07: /* B7CR */
468 case 0x10: /* B0AP */
471 case 0x11: /* B1AP */
474 case 0x12: /* B2AP */
477 case 0x13: /* B3AP */
480 case 0x14: /* B4AP */
483 case 0x15: /* B5AP */
486 case 0x16: /* B6AP */
489 case 0x17: /* B7AP */
492 case 0x20: /* BEAR */
495 case 0x21: /* BESR0 */
498 case 0x22: /* BESR1 */
517 static void dcr_write_ebc (void *opaque
, int dcrn
, uint32_t val
)
528 case 0x00: /* B0CR */
530 case 0x01: /* B1CR */
532 case 0x02: /* B2CR */
534 case 0x03: /* B3CR */
536 case 0x04: /* B4CR */
538 case 0x05: /* B5CR */
540 case 0x06: /* B6CR */
542 case 0x07: /* B7CR */
544 case 0x10: /* B0AP */
546 case 0x11: /* B1AP */
548 case 0x12: /* B2AP */
550 case 0x13: /* B3AP */
552 case 0x14: /* B4AP */
554 case 0x15: /* B5AP */
556 case 0x16: /* B6AP */
558 case 0x17: /* B7AP */
560 case 0x20: /* BEAR */
562 case 0x21: /* BESR0 */
564 case 0x22: /* BESR1 */
577 static void ebc_reset (void *opaque
)
583 ebc
->addr
= 0x00000000;
584 ebc
->bap
[0] = 0x7F8FFE80;
585 ebc
->bcr
[0] = 0xFFE28000;
586 for (i
= 0; i
< 8; i
++) {
587 ebc
->bap
[i
] = 0x00000000;
588 ebc
->bcr
[i
] = 0x00000000;
590 ebc
->besr0
= 0x00000000;
591 ebc
->besr1
= 0x00000000;
592 ebc
->cfg
= 0x80400000;
595 void ppc405_ebc_init(CPUPPCState
*env
)
599 ebc
= g_malloc0(sizeof(ppc4xx_ebc_t
));
600 qemu_register_reset(&ebc_reset
, ebc
);
601 ppc_dcr_register(env
, EBC0_CFGADDR
,
602 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
603 ppc_dcr_register(env
, EBC0_CFGDATA
,
604 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
607 /*****************************************************************************/
636 typedef struct ppc405_dma_t ppc405_dma_t
;
637 struct ppc405_dma_t
{
650 static uint32_t dcr_read_dma (void *opaque
, int dcrn
)
655 static void dcr_write_dma (void *opaque
, int dcrn
, uint32_t val
)
659 static void ppc405_dma_reset (void *opaque
)
665 for (i
= 0; i
< 4; i
++) {
666 dma
->cr
[i
] = 0x00000000;
667 dma
->ct
[i
] = 0x00000000;
668 dma
->da
[i
] = 0x00000000;
669 dma
->sa
[i
] = 0x00000000;
670 dma
->sg
[i
] = 0x00000000;
672 dma
->sr
= 0x00000000;
673 dma
->sgc
= 0x00000000;
674 dma
->slp
= 0x7C000000;
675 dma
->pol
= 0x00000000;
678 static void ppc405_dma_init(CPUPPCState
*env
, qemu_irq irqs
[4])
682 dma
= g_malloc0(sizeof(ppc405_dma_t
));
683 memcpy(dma
->irqs
, irqs
, 4 * sizeof(qemu_irq
));
684 qemu_register_reset(&ppc405_dma_reset
, dma
);
685 ppc_dcr_register(env
, DMA0_CR0
,
686 dma
, &dcr_read_dma
, &dcr_write_dma
);
687 ppc_dcr_register(env
, DMA0_CT0
,
688 dma
, &dcr_read_dma
, &dcr_write_dma
);
689 ppc_dcr_register(env
, DMA0_DA0
,
690 dma
, &dcr_read_dma
, &dcr_write_dma
);
691 ppc_dcr_register(env
, DMA0_SA0
,
692 dma
, &dcr_read_dma
, &dcr_write_dma
);
693 ppc_dcr_register(env
, DMA0_SG0
,
694 dma
, &dcr_read_dma
, &dcr_write_dma
);
695 ppc_dcr_register(env
, DMA0_CR1
,
696 dma
, &dcr_read_dma
, &dcr_write_dma
);
697 ppc_dcr_register(env
, DMA0_CT1
,
698 dma
, &dcr_read_dma
, &dcr_write_dma
);
699 ppc_dcr_register(env
, DMA0_DA1
,
700 dma
, &dcr_read_dma
, &dcr_write_dma
);
701 ppc_dcr_register(env
, DMA0_SA1
,
702 dma
, &dcr_read_dma
, &dcr_write_dma
);
703 ppc_dcr_register(env
, DMA0_SG1
,
704 dma
, &dcr_read_dma
, &dcr_write_dma
);
705 ppc_dcr_register(env
, DMA0_CR2
,
706 dma
, &dcr_read_dma
, &dcr_write_dma
);
707 ppc_dcr_register(env
, DMA0_CT2
,
708 dma
, &dcr_read_dma
, &dcr_write_dma
);
709 ppc_dcr_register(env
, DMA0_DA2
,
710 dma
, &dcr_read_dma
, &dcr_write_dma
);
711 ppc_dcr_register(env
, DMA0_SA2
,
712 dma
, &dcr_read_dma
, &dcr_write_dma
);
713 ppc_dcr_register(env
, DMA0_SG2
,
714 dma
, &dcr_read_dma
, &dcr_write_dma
);
715 ppc_dcr_register(env
, DMA0_CR3
,
716 dma
, &dcr_read_dma
, &dcr_write_dma
);
717 ppc_dcr_register(env
, DMA0_CT3
,
718 dma
, &dcr_read_dma
, &dcr_write_dma
);
719 ppc_dcr_register(env
, DMA0_DA3
,
720 dma
, &dcr_read_dma
, &dcr_write_dma
);
721 ppc_dcr_register(env
, DMA0_SA3
,
722 dma
, &dcr_read_dma
, &dcr_write_dma
);
723 ppc_dcr_register(env
, DMA0_SG3
,
724 dma
, &dcr_read_dma
, &dcr_write_dma
);
725 ppc_dcr_register(env
, DMA0_SR
,
726 dma
, &dcr_read_dma
, &dcr_write_dma
);
727 ppc_dcr_register(env
, DMA0_SGC
,
728 dma
, &dcr_read_dma
, &dcr_write_dma
);
729 ppc_dcr_register(env
, DMA0_SLP
,
730 dma
, &dcr_read_dma
, &dcr_write_dma
);
731 ppc_dcr_register(env
, DMA0_POL
,
732 dma
, &dcr_read_dma
, &dcr_write_dma
);
735 /*****************************************************************************/
737 typedef struct ppc405_gpio_t ppc405_gpio_t
;
738 struct ppc405_gpio_t
{
753 static uint32_t ppc405_gpio_readb (void *opaque
, hwaddr addr
)
756 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
762 static void ppc405_gpio_writeb (void *opaque
,
763 hwaddr addr
, uint32_t value
)
766 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
771 static uint32_t ppc405_gpio_readw (void *opaque
, hwaddr addr
)
774 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
780 static void ppc405_gpio_writew (void *opaque
,
781 hwaddr addr
, uint32_t value
)
784 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
789 static uint32_t ppc405_gpio_readl (void *opaque
, hwaddr addr
)
792 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
798 static void ppc405_gpio_writel (void *opaque
,
799 hwaddr addr
, uint32_t value
)
802 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
807 static const MemoryRegionOps ppc405_gpio_ops
= {
809 .read
= { ppc405_gpio_readb
, ppc405_gpio_readw
, ppc405_gpio_readl
, },
810 .write
= { ppc405_gpio_writeb
, ppc405_gpio_writew
, ppc405_gpio_writel
, },
812 .endianness
= DEVICE_NATIVE_ENDIAN
,
815 static void ppc405_gpio_reset (void *opaque
)
819 static void ppc405_gpio_init(hwaddr base
)
823 gpio
= g_malloc0(sizeof(ppc405_gpio_t
));
825 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
827 memory_region_init_io(&gpio
->io
, NULL
, &ppc405_gpio_ops
, gpio
, "pgio", 0x038);
828 memory_region_add_subregion(get_system_memory(), base
, &gpio
->io
);
829 qemu_register_reset(&ppc405_gpio_reset
, gpio
);
832 /*****************************************************************************/
836 OCM0_ISACNTL
= 0x019,
838 OCM0_DSACNTL
= 0x01B,
841 typedef struct ppc405_ocm_t ppc405_ocm_t
;
842 struct ppc405_ocm_t
{
844 MemoryRegion isarc_ram
;
845 MemoryRegion dsarc_ram
;
852 static void ocm_update_mappings (ppc405_ocm_t
*ocm
,
853 uint32_t isarc
, uint32_t isacntl
,
854 uint32_t dsarc
, uint32_t dsacntl
)
857 printf("OCM update ISA %08" PRIx32
" %08" PRIx32
" (%08" PRIx32
858 " %08" PRIx32
") DSA %08" PRIx32
" %08" PRIx32
859 " (%08" PRIx32
" %08" PRIx32
")\n",
860 isarc
, isacntl
, dsarc
, dsacntl
,
861 ocm
->isarc
, ocm
->isacntl
, ocm
->dsarc
, ocm
->dsacntl
);
863 if (ocm
->isarc
!= isarc
||
864 (ocm
->isacntl
& 0x80000000) != (isacntl
& 0x80000000)) {
865 if (ocm
->isacntl
& 0x80000000) {
866 /* Unmap previously assigned memory region */
867 printf("OCM unmap ISA %08" PRIx32
"\n", ocm
->isarc
);
868 memory_region_del_subregion(get_system_memory(), &ocm
->isarc_ram
);
870 if (isacntl
& 0x80000000) {
871 /* Map new instruction memory region */
873 printf("OCM map ISA %08" PRIx32
"\n", isarc
);
875 memory_region_add_subregion(get_system_memory(), isarc
,
879 if (ocm
->dsarc
!= dsarc
||
880 (ocm
->dsacntl
& 0x80000000) != (dsacntl
& 0x80000000)) {
881 if (ocm
->dsacntl
& 0x80000000) {
882 /* Beware not to unmap the region we just mapped */
883 if (!(isacntl
& 0x80000000) || ocm
->dsarc
!= isarc
) {
884 /* Unmap previously assigned memory region */
886 printf("OCM unmap DSA %08" PRIx32
"\n", ocm
->dsarc
);
888 memory_region_del_subregion(get_system_memory(),
892 if (dsacntl
& 0x80000000) {
893 /* Beware not to remap the region we just mapped */
894 if (!(isacntl
& 0x80000000) || dsarc
!= isarc
) {
895 /* Map new data memory region */
897 printf("OCM map DSA %08" PRIx32
"\n", dsarc
);
899 memory_region_add_subregion(get_system_memory(), dsarc
,
906 static uint32_t dcr_read_ocm (void *opaque
, int dcrn
)
933 static void dcr_write_ocm (void *opaque
, int dcrn
, uint32_t val
)
936 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
941 isacntl
= ocm
->isacntl
;
942 dsacntl
= ocm
->dsacntl
;
945 isarc
= val
& 0xFC000000;
948 isacntl
= val
& 0xC0000000;
951 isarc
= val
& 0xFC000000;
954 isacntl
= val
& 0xC0000000;
957 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
960 ocm
->isacntl
= isacntl
;
961 ocm
->dsacntl
= dsacntl
;
964 static void ocm_reset (void *opaque
)
967 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
971 isacntl
= 0x00000000;
973 dsacntl
= 0x00000000;
974 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
977 ocm
->isacntl
= isacntl
;
978 ocm
->dsacntl
= dsacntl
;
981 static void ppc405_ocm_init(CPUPPCState
*env
)
985 ocm
= g_malloc0(sizeof(ppc405_ocm_t
));
986 /* XXX: Size is 4096 or 0x04000000 */
987 memory_region_init_ram(&ocm
->isarc_ram
, NULL
, "ppc405.ocm", 4 * KiB
,
989 memory_region_init_alias(&ocm
->dsarc_ram
, NULL
, "ppc405.dsarc",
990 &ocm
->isarc_ram
, 0, 4 * KiB
);
991 qemu_register_reset(&ocm_reset
, ocm
);
992 ppc_dcr_register(env
, OCM0_ISARC
,
993 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
994 ppc_dcr_register(env
, OCM0_ISACNTL
,
995 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
996 ppc_dcr_register(env
, OCM0_DSARC
,
997 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
998 ppc_dcr_register(env
, OCM0_DSACNTL
,
999 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
1002 /*****************************************************************************/
1003 /* General purpose timers */
1004 typedef struct ppc4xx_gpt_t ppc4xx_gpt_t
;
1005 struct ppc4xx_gpt_t
{
1020 static uint32_t ppc4xx_gpt_readb (void *opaque
, hwaddr addr
)
1023 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1025 /* XXX: generate a bus fault */
1029 static void ppc4xx_gpt_writeb (void *opaque
,
1030 hwaddr addr
, uint32_t value
)
1033 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1036 /* XXX: generate a bus fault */
1039 static uint32_t ppc4xx_gpt_readw (void *opaque
, hwaddr addr
)
1042 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1044 /* XXX: generate a bus fault */
1048 static void ppc4xx_gpt_writew (void *opaque
,
1049 hwaddr addr
, uint32_t value
)
1052 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1055 /* XXX: generate a bus fault */
1058 static int ppc4xx_gpt_compare (ppc4xx_gpt_t
*gpt
, int n
)
1064 static void ppc4xx_gpt_set_output (ppc4xx_gpt_t
*gpt
, int n
, int level
)
1069 static void ppc4xx_gpt_set_outputs (ppc4xx_gpt_t
*gpt
)
1075 for (i
= 0; i
< 5; i
++) {
1076 if (gpt
->oe
& mask
) {
1077 /* Output is enabled */
1078 if (ppc4xx_gpt_compare(gpt
, i
)) {
1079 /* Comparison is OK */
1080 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
);
1082 /* Comparison is KO */
1083 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
? 0 : 1);
1090 static void ppc4xx_gpt_set_irqs (ppc4xx_gpt_t
*gpt
)
1096 for (i
= 0; i
< 5; i
++) {
1097 if (gpt
->is
& gpt
->im
& mask
)
1098 qemu_irq_raise(gpt
->irqs
[i
]);
1100 qemu_irq_lower(gpt
->irqs
[i
]);
1105 static void ppc4xx_gpt_compute_timer (ppc4xx_gpt_t
*gpt
)
1110 static uint32_t ppc4xx_gpt_readl (void *opaque
, hwaddr addr
)
1117 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1122 /* Time base counter */
1123 ret
= muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + gpt
->tb_offset
,
1124 gpt
->tb_freq
, NANOSECONDS_PER_SECOND
);
1135 /* Interrupt mask */
1140 /* Interrupt status */
1144 /* Interrupt enable */
1149 idx
= (addr
- 0x80) >> 2;
1150 ret
= gpt
->comp
[idx
];
1154 idx
= (addr
- 0xC0) >> 2;
1155 ret
= gpt
->mask
[idx
];
1165 static void ppc4xx_gpt_writel (void *opaque
,
1166 hwaddr addr
, uint32_t value
)
1172 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1178 /* Time base counter */
1179 gpt
->tb_offset
= muldiv64(value
, NANOSECONDS_PER_SECOND
, gpt
->tb_freq
)
1180 - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1181 ppc4xx_gpt_compute_timer(gpt
);
1185 gpt
->oe
= value
& 0xF8000000;
1186 ppc4xx_gpt_set_outputs(gpt
);
1190 gpt
->ol
= value
& 0xF8000000;
1191 ppc4xx_gpt_set_outputs(gpt
);
1194 /* Interrupt mask */
1195 gpt
->im
= value
& 0x0000F800;
1198 /* Interrupt status set */
1199 gpt
->is
|= value
& 0x0000F800;
1200 ppc4xx_gpt_set_irqs(gpt
);
1203 /* Interrupt status clear */
1204 gpt
->is
&= ~(value
& 0x0000F800);
1205 ppc4xx_gpt_set_irqs(gpt
);
1208 /* Interrupt enable */
1209 gpt
->ie
= value
& 0x0000F800;
1210 ppc4xx_gpt_set_irqs(gpt
);
1214 idx
= (addr
- 0x80) >> 2;
1215 gpt
->comp
[idx
] = value
& 0xF8000000;
1216 ppc4xx_gpt_compute_timer(gpt
);
1220 idx
= (addr
- 0xC0) >> 2;
1221 gpt
->mask
[idx
] = value
& 0xF8000000;
1222 ppc4xx_gpt_compute_timer(gpt
);
1227 static const MemoryRegionOps gpt_ops
= {
1229 .read
= { ppc4xx_gpt_readb
, ppc4xx_gpt_readw
, ppc4xx_gpt_readl
, },
1230 .write
= { ppc4xx_gpt_writeb
, ppc4xx_gpt_writew
, ppc4xx_gpt_writel
, },
1232 .endianness
= DEVICE_NATIVE_ENDIAN
,
1235 static void ppc4xx_gpt_cb (void *opaque
)
1240 ppc4xx_gpt_set_irqs(gpt
);
1241 ppc4xx_gpt_set_outputs(gpt
);
1242 ppc4xx_gpt_compute_timer(gpt
);
1245 static void ppc4xx_gpt_reset (void *opaque
)
1251 timer_del(gpt
->timer
);
1252 gpt
->oe
= 0x00000000;
1253 gpt
->ol
= 0x00000000;
1254 gpt
->im
= 0x00000000;
1255 gpt
->is
= 0x00000000;
1256 gpt
->ie
= 0x00000000;
1257 for (i
= 0; i
< 5; i
++) {
1258 gpt
->comp
[i
] = 0x00000000;
1259 gpt
->mask
[i
] = 0x00000000;
1263 static void ppc4xx_gpt_init(hwaddr base
, qemu_irq irqs
[5])
1268 gpt
= g_malloc0(sizeof(ppc4xx_gpt_t
));
1269 for (i
= 0; i
< 5; i
++) {
1270 gpt
->irqs
[i
] = irqs
[i
];
1272 gpt
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &ppc4xx_gpt_cb
, gpt
);
1274 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1276 memory_region_init_io(&gpt
->iomem
, NULL
, &gpt_ops
, gpt
, "gpt", 0x0d4);
1277 memory_region_add_subregion(get_system_memory(), base
, &gpt
->iomem
);
1278 qemu_register_reset(ppc4xx_gpt_reset
, gpt
);
1281 /*****************************************************************************/
1283 void ppc40x_core_reset(PowerPCCPU
*cpu
)
1285 CPUPPCState
*env
= &cpu
->env
;
1288 printf("Reset PowerPC core\n");
1289 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_RESET
);
1290 dbsr
= env
->spr
[SPR_40x_DBSR
];
1291 dbsr
&= ~0x00000300;
1293 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1296 void ppc40x_chip_reset(PowerPCCPU
*cpu
)
1298 CPUPPCState
*env
= &cpu
->env
;
1301 printf("Reset PowerPC chip\n");
1302 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_RESET
);
1303 /* XXX: TODO reset all internal peripherals */
1304 dbsr
= env
->spr
[SPR_40x_DBSR
];
1305 dbsr
&= ~0x00000300;
1307 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1310 void ppc40x_system_reset(PowerPCCPU
*cpu
)
1312 printf("Reset PowerPC system\n");
1313 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
1316 void store_40x_dbcr0 (CPUPPCState
*env
, uint32_t val
)
1318 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1320 switch ((val
>> 28) & 0x3) {
1326 ppc40x_core_reset(cpu
);
1330 ppc40x_chip_reset(cpu
);
1334 ppc40x_system_reset(cpu
);
1339 /*****************************************************************************/
1342 PPC405CR_CPC0_PLLMR
= 0x0B0,
1343 PPC405CR_CPC0_CR0
= 0x0B1,
1344 PPC405CR_CPC0_CR1
= 0x0B2,
1345 PPC405CR_CPC0_PSR
= 0x0B4,
1346 PPC405CR_CPC0_JTAGID
= 0x0B5,
1347 PPC405CR_CPC0_ER
= 0x0B9,
1348 PPC405CR_CPC0_FR
= 0x0BA,
1349 PPC405CR_CPC0_SR
= 0x0BB,
1353 PPC405CR_CPU_CLK
= 0,
1354 PPC405CR_TMR_CLK
= 1,
1355 PPC405CR_PLB_CLK
= 2,
1356 PPC405CR_SDRAM_CLK
= 3,
1357 PPC405CR_OPB_CLK
= 4,
1358 PPC405CR_EXT_CLK
= 5,
1359 PPC405CR_UART_CLK
= 6,
1360 PPC405CR_CLK_NB
= 7,
1363 typedef struct ppc405cr_cpc_t ppc405cr_cpc_t
;
1364 struct ppc405cr_cpc_t
{
1365 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
1376 static void ppc405cr_clk_setup (ppc405cr_cpc_t
*cpc
)
1378 uint64_t VCO_out
, PLL_out
;
1379 uint32_t CPU_clk
, TMR_clk
, SDRAM_clk
, PLB_clk
, OPB_clk
, EXT_clk
, UART_clk
;
1382 D0
= ((cpc
->pllmr
>> 26) & 0x3) + 1; /* CBDV */
1383 if (cpc
->pllmr
& 0x80000000) {
1384 D1
= (((cpc
->pllmr
>> 20) - 1) & 0xF) + 1; /* FBDV */
1385 D2
= 8 - ((cpc
->pllmr
>> 16) & 0x7); /* FWDVA */
1387 VCO_out
= (uint64_t)cpc
->sysclk
* M
;
1388 if (VCO_out
< 400000000 || VCO_out
> 800000000) {
1389 /* PLL cannot lock */
1390 cpc
->pllmr
&= ~0x80000000;
1393 PLL_out
= VCO_out
/ D2
;
1398 PLL_out
= (uint64_t)cpc
->sysclk
* M
;
1401 if (cpc
->cr1
& 0x00800000)
1402 TMR_clk
= cpc
->sysclk
; /* Should have a separate clock */
1405 PLB_clk
= CPU_clk
/ D0
;
1406 SDRAM_clk
= PLB_clk
;
1407 D0
= ((cpc
->pllmr
>> 10) & 0x3) + 1;
1408 OPB_clk
= PLB_clk
/ D0
;
1409 D0
= ((cpc
->pllmr
>> 24) & 0x3) + 2;
1410 EXT_clk
= PLB_clk
/ D0
;
1411 D0
= ((cpc
->cr0
>> 1) & 0x1F) + 1;
1412 UART_clk
= CPU_clk
/ D0
;
1413 /* Setup CPU clocks */
1414 clk_setup(&cpc
->clk_setup
[PPC405CR_CPU_CLK
], CPU_clk
);
1415 /* Setup time-base clock */
1416 clk_setup(&cpc
->clk_setup
[PPC405CR_TMR_CLK
], TMR_clk
);
1417 /* Setup PLB clock */
1418 clk_setup(&cpc
->clk_setup
[PPC405CR_PLB_CLK
], PLB_clk
);
1419 /* Setup SDRAM clock */
1420 clk_setup(&cpc
->clk_setup
[PPC405CR_SDRAM_CLK
], SDRAM_clk
);
1421 /* Setup OPB clock */
1422 clk_setup(&cpc
->clk_setup
[PPC405CR_OPB_CLK
], OPB_clk
);
1423 /* Setup external clock */
1424 clk_setup(&cpc
->clk_setup
[PPC405CR_EXT_CLK
], EXT_clk
);
1425 /* Setup UART clock */
1426 clk_setup(&cpc
->clk_setup
[PPC405CR_UART_CLK
], UART_clk
);
1429 static uint32_t dcr_read_crcpc (void *opaque
, int dcrn
)
1431 ppc405cr_cpc_t
*cpc
;
1436 case PPC405CR_CPC0_PLLMR
:
1439 case PPC405CR_CPC0_CR0
:
1442 case PPC405CR_CPC0_CR1
:
1445 case PPC405CR_CPC0_PSR
:
1448 case PPC405CR_CPC0_JTAGID
:
1451 case PPC405CR_CPC0_ER
:
1454 case PPC405CR_CPC0_FR
:
1457 case PPC405CR_CPC0_SR
:
1458 ret
= ~(cpc
->er
| cpc
->fr
) & 0xFFFF0000;
1461 /* Avoid gcc warning */
1469 static void dcr_write_crcpc (void *opaque
, int dcrn
, uint32_t val
)
1471 ppc405cr_cpc_t
*cpc
;
1475 case PPC405CR_CPC0_PLLMR
:
1476 cpc
->pllmr
= val
& 0xFFF77C3F;
1478 case PPC405CR_CPC0_CR0
:
1479 cpc
->cr0
= val
& 0x0FFFFFFE;
1481 case PPC405CR_CPC0_CR1
:
1482 cpc
->cr1
= val
& 0x00800000;
1484 case PPC405CR_CPC0_PSR
:
1487 case PPC405CR_CPC0_JTAGID
:
1490 case PPC405CR_CPC0_ER
:
1491 cpc
->er
= val
& 0xBFFC0000;
1493 case PPC405CR_CPC0_FR
:
1494 cpc
->fr
= val
& 0xBFFC0000;
1496 case PPC405CR_CPC0_SR
:
1502 static void ppc405cr_cpc_reset (void *opaque
)
1504 ppc405cr_cpc_t
*cpc
;
1508 /* Compute PLLMR value from PSR settings */
1509 cpc
->pllmr
= 0x80000000;
1511 switch ((cpc
->psr
>> 30) & 3) {
1514 cpc
->pllmr
&= ~0x80000000;
1518 cpc
->pllmr
|= 5 << 16;
1522 cpc
->pllmr
|= 4 << 16;
1526 cpc
->pllmr
|= 2 << 16;
1530 D
= (cpc
->psr
>> 28) & 3;
1531 cpc
->pllmr
|= (D
+ 1) << 20;
1533 D
= (cpc
->psr
>> 25) & 7;
1548 D
= (cpc
->psr
>> 23) & 3;
1549 cpc
->pllmr
|= D
<< 26;
1551 D
= (cpc
->psr
>> 21) & 3;
1552 cpc
->pllmr
|= D
<< 10;
1554 D
= (cpc
->psr
>> 17) & 3;
1555 cpc
->pllmr
|= D
<< 24;
1556 cpc
->cr0
= 0x0000003C;
1557 cpc
->cr1
= 0x2B0D8800;
1558 cpc
->er
= 0x00000000;
1559 cpc
->fr
= 0x00000000;
1560 ppc405cr_clk_setup(cpc
);
1563 static void ppc405cr_clk_init (ppc405cr_cpc_t
*cpc
)
1567 /* XXX: this should be read from IO pins */
1568 cpc
->psr
= 0x00000000; /* 8 bits ROM */
1570 D
= 0x2; /* Divide by 4 */
1571 cpc
->psr
|= D
<< 30;
1573 D
= 0x1; /* Divide by 2 */
1574 cpc
->psr
|= D
<< 28;
1576 D
= 0x1; /* Divide by 2 */
1577 cpc
->psr
|= D
<< 23;
1579 D
= 0x5; /* M = 16 */
1580 cpc
->psr
|= D
<< 25;
1582 D
= 0x1; /* Divide by 2 */
1583 cpc
->psr
|= D
<< 21;
1585 D
= 0x2; /* Divide by 4 */
1586 cpc
->psr
|= D
<< 17;
1589 static void ppc405cr_cpc_init (CPUPPCState
*env
, clk_setup_t clk_setup
[7],
1592 ppc405cr_cpc_t
*cpc
;
1594 cpc
= g_malloc0(sizeof(ppc405cr_cpc_t
));
1595 memcpy(cpc
->clk_setup
, clk_setup
,
1596 PPC405CR_CLK_NB
* sizeof(clk_setup_t
));
1597 cpc
->sysclk
= sysclk
;
1598 cpc
->jtagid
= 0x42051049;
1599 ppc_dcr_register(env
, PPC405CR_CPC0_PSR
, cpc
,
1600 &dcr_read_crcpc
, &dcr_write_crcpc
);
1601 ppc_dcr_register(env
, PPC405CR_CPC0_CR0
, cpc
,
1602 &dcr_read_crcpc
, &dcr_write_crcpc
);
1603 ppc_dcr_register(env
, PPC405CR_CPC0_CR1
, cpc
,
1604 &dcr_read_crcpc
, &dcr_write_crcpc
);
1605 ppc_dcr_register(env
, PPC405CR_CPC0_JTAGID
, cpc
,
1606 &dcr_read_crcpc
, &dcr_write_crcpc
);
1607 ppc_dcr_register(env
, PPC405CR_CPC0_PLLMR
, cpc
,
1608 &dcr_read_crcpc
, &dcr_write_crcpc
);
1609 ppc_dcr_register(env
, PPC405CR_CPC0_ER
, cpc
,
1610 &dcr_read_crcpc
, &dcr_write_crcpc
);
1611 ppc_dcr_register(env
, PPC405CR_CPC0_FR
, cpc
,
1612 &dcr_read_crcpc
, &dcr_write_crcpc
);
1613 ppc_dcr_register(env
, PPC405CR_CPC0_SR
, cpc
,
1614 &dcr_read_crcpc
, &dcr_write_crcpc
);
1615 ppc405cr_clk_init(cpc
);
1616 qemu_register_reset(ppc405cr_cpc_reset
, cpc
);
1619 CPUPPCState
*ppc405cr_init(MemoryRegion
*address_space_mem
,
1620 MemoryRegion ram_memories
[4],
1621 hwaddr ram_bases
[4],
1622 hwaddr ram_sizes
[4],
1623 uint32_t sysclk
, qemu_irq
**picp
,
1626 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
1627 qemu_irq dma_irqs
[4];
1630 qemu_irq
*pic
, *irqs
;
1632 memset(clk_setup
, 0, sizeof(clk_setup
));
1633 cpu
= ppc4xx_init(POWERPC_CPU_TYPE_NAME("405crc"),
1634 &clk_setup
[PPC405CR_CPU_CLK
],
1635 &clk_setup
[PPC405CR_TMR_CLK
], sysclk
);
1637 /* Memory mapped devices registers */
1639 ppc4xx_plb_init(env
);
1640 /* PLB to OPB bridge */
1641 ppc4xx_pob_init(env
);
1643 ppc4xx_opba_init(0xef600600);
1644 /* Universal interrupt controller */
1645 irqs
= g_malloc0(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
1646 irqs
[PPCUIC_OUTPUT_INT
] =
1647 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
1648 irqs
[PPCUIC_OUTPUT_CINT
] =
1649 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
1650 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
1652 /* SDRAM controller */
1653 ppc4xx_sdram_init(env
, pic
[14], 1, ram_memories
,
1654 ram_bases
, ram_sizes
, do_init
);
1655 /* External bus controller */
1656 ppc405_ebc_init(env
);
1657 /* DMA controller */
1658 dma_irqs
[0] = pic
[26];
1659 dma_irqs
[1] = pic
[25];
1660 dma_irqs
[2] = pic
[24];
1661 dma_irqs
[3] = pic
[23];
1662 ppc405_dma_init(env
, dma_irqs
);
1664 if (serial_hd(0) != NULL
) {
1665 serial_mm_init(address_space_mem
, 0xef600300, 0, pic
[0],
1666 PPC_SERIAL_MM_BAUDBASE
, serial_hd(0),
1669 if (serial_hd(1) != NULL
) {
1670 serial_mm_init(address_space_mem
, 0xef600400, 0, pic
[1],
1671 PPC_SERIAL_MM_BAUDBASE
, serial_hd(1),
1674 /* IIC controller */
1675 sysbus_create_simple(TYPE_PPC4xx_I2C
, 0xef600500, pic
[2]);
1677 ppc405_gpio_init(0xef600700);
1679 ppc405cr_cpc_init(env
, clk_setup
, sysclk
);
1684 /*****************************************************************************/
1688 PPC405EP_CPC0_PLLMR0
= 0x0F0,
1689 PPC405EP_CPC0_BOOT
= 0x0F1,
1690 PPC405EP_CPC0_EPCTL
= 0x0F3,
1691 PPC405EP_CPC0_PLLMR1
= 0x0F4,
1692 PPC405EP_CPC0_UCR
= 0x0F5,
1693 PPC405EP_CPC0_SRR
= 0x0F6,
1694 PPC405EP_CPC0_JTAGID
= 0x0F7,
1695 PPC405EP_CPC0_PCI
= 0x0F9,
1697 PPC405EP_CPC0_ER
= xxx
,
1698 PPC405EP_CPC0_FR
= xxx
,
1699 PPC405EP_CPC0_SR
= xxx
,
1704 PPC405EP_CPU_CLK
= 0,
1705 PPC405EP_PLB_CLK
= 1,
1706 PPC405EP_OPB_CLK
= 2,
1707 PPC405EP_EBC_CLK
= 3,
1708 PPC405EP_MAL_CLK
= 4,
1709 PPC405EP_PCI_CLK
= 5,
1710 PPC405EP_UART0_CLK
= 6,
1711 PPC405EP_UART1_CLK
= 7,
1712 PPC405EP_CLK_NB
= 8,
1715 typedef struct ppc405ep_cpc_t ppc405ep_cpc_t
;
1716 struct ppc405ep_cpc_t
{
1718 clk_setup_t clk_setup
[PPC405EP_CLK_NB
];
1726 /* Clock and power management */
1732 static void ppc405ep_compute_clocks (ppc405ep_cpc_t
*cpc
)
1734 uint32_t CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
;
1735 uint32_t UART0_clk
, UART1_clk
;
1736 uint64_t VCO_out
, PLL_out
;
1740 if ((cpc
->pllmr
[1] & 0x80000000) && !(cpc
->pllmr
[1] & 0x40000000)) {
1741 M
= (((cpc
->pllmr
[1] >> 20) - 1) & 0xF) + 1; /* FBMUL */
1742 #ifdef DEBUG_CLOCKS_LL
1743 printf("FBMUL %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 20) & 0xF, M
);
1745 D
= 8 - ((cpc
->pllmr
[1] >> 16) & 0x7); /* FWDA */
1746 #ifdef DEBUG_CLOCKS_LL
1747 printf("FWDA %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 16) & 0x7, D
);
1749 VCO_out
= (uint64_t)cpc
->sysclk
* M
* D
;
1750 if (VCO_out
< 500000000UL || VCO_out
> 1000000000UL) {
1751 /* Error - unlock the PLL */
1752 printf("VCO out of range %" PRIu64
"\n", VCO_out
);
1754 cpc
->pllmr
[1] &= ~0x80000000;
1758 PLL_out
= VCO_out
/ D
;
1759 /* Pretend the PLL is locked */
1760 cpc
->boot
|= 0x00000001;
1765 PLL_out
= cpc
->sysclk
;
1766 if (cpc
->pllmr
[1] & 0x40000000) {
1767 /* Pretend the PLL is not locked */
1768 cpc
->boot
&= ~0x00000001;
1771 /* Now, compute all other clocks */
1772 D
= ((cpc
->pllmr
[0] >> 20) & 0x3) + 1; /* CCDV */
1773 #ifdef DEBUG_CLOCKS_LL
1774 printf("CCDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 20) & 0x3, D
);
1776 CPU_clk
= PLL_out
/ D
;
1777 D
= ((cpc
->pllmr
[0] >> 16) & 0x3) + 1; /* CBDV */
1778 #ifdef DEBUG_CLOCKS_LL
1779 printf("CBDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 16) & 0x3, D
);
1781 PLB_clk
= CPU_clk
/ D
;
1782 D
= ((cpc
->pllmr
[0] >> 12) & 0x3) + 1; /* OPDV */
1783 #ifdef DEBUG_CLOCKS_LL
1784 printf("OPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 12) & 0x3, D
);
1786 OPB_clk
= PLB_clk
/ D
;
1787 D
= ((cpc
->pllmr
[0] >> 8) & 0x3) + 2; /* EPDV */
1788 #ifdef DEBUG_CLOCKS_LL
1789 printf("EPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 8) & 0x3, D
);
1791 EBC_clk
= PLB_clk
/ D
;
1792 D
= ((cpc
->pllmr
[0] >> 4) & 0x3) + 1; /* MPDV */
1793 #ifdef DEBUG_CLOCKS_LL
1794 printf("MPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 4) & 0x3, D
);
1796 MAL_clk
= PLB_clk
/ D
;
1797 D
= (cpc
->pllmr
[0] & 0x3) + 1; /* PPDV */
1798 #ifdef DEBUG_CLOCKS_LL
1799 printf("PPDV %01" PRIx32
" %d\n", cpc
->pllmr
[0] & 0x3, D
);
1801 PCI_clk
= PLB_clk
/ D
;
1802 D
= ((cpc
->ucr
- 1) & 0x7F) + 1; /* U0DIV */
1803 #ifdef DEBUG_CLOCKS_LL
1804 printf("U0DIV %01" PRIx32
" %d\n", cpc
->ucr
& 0x7F, D
);
1806 UART0_clk
= PLL_out
/ D
;
1807 D
= (((cpc
->ucr
>> 8) - 1) & 0x7F) + 1; /* U1DIV */
1808 #ifdef DEBUG_CLOCKS_LL
1809 printf("U1DIV %01" PRIx32
" %d\n", (cpc
->ucr
>> 8) & 0x7F, D
);
1811 UART1_clk
= PLL_out
/ D
;
1813 printf("Setup PPC405EP clocks - sysclk %" PRIu32
" VCO %" PRIu64
1814 " PLL out %" PRIu64
" Hz\n", cpc
->sysclk
, VCO_out
, PLL_out
);
1815 printf("CPU %" PRIu32
" PLB %" PRIu32
" OPB %" PRIu32
" EBC %" PRIu32
1816 " MAL %" PRIu32
" PCI %" PRIu32
" UART0 %" PRIu32
1817 " UART1 %" PRIu32
"\n",
1818 CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
,
1819 UART0_clk
, UART1_clk
);
1821 /* Setup CPU clocks */
1822 clk_setup(&cpc
->clk_setup
[PPC405EP_CPU_CLK
], CPU_clk
);
1823 /* Setup PLB clock */
1824 clk_setup(&cpc
->clk_setup
[PPC405EP_PLB_CLK
], PLB_clk
);
1825 /* Setup OPB clock */
1826 clk_setup(&cpc
->clk_setup
[PPC405EP_OPB_CLK
], OPB_clk
);
1827 /* Setup external clock */
1828 clk_setup(&cpc
->clk_setup
[PPC405EP_EBC_CLK
], EBC_clk
);
1829 /* Setup MAL clock */
1830 clk_setup(&cpc
->clk_setup
[PPC405EP_MAL_CLK
], MAL_clk
);
1831 /* Setup PCI clock */
1832 clk_setup(&cpc
->clk_setup
[PPC405EP_PCI_CLK
], PCI_clk
);
1833 /* Setup UART0 clock */
1834 clk_setup(&cpc
->clk_setup
[PPC405EP_UART0_CLK
], UART0_clk
);
1835 /* Setup UART1 clock */
1836 clk_setup(&cpc
->clk_setup
[PPC405EP_UART1_CLK
], UART1_clk
);
1839 static uint32_t dcr_read_epcpc (void *opaque
, int dcrn
)
1841 ppc405ep_cpc_t
*cpc
;
1846 case PPC405EP_CPC0_BOOT
:
1849 case PPC405EP_CPC0_EPCTL
:
1852 case PPC405EP_CPC0_PLLMR0
:
1853 ret
= cpc
->pllmr
[0];
1855 case PPC405EP_CPC0_PLLMR1
:
1856 ret
= cpc
->pllmr
[1];
1858 case PPC405EP_CPC0_UCR
:
1861 case PPC405EP_CPC0_SRR
:
1864 case PPC405EP_CPC0_JTAGID
:
1867 case PPC405EP_CPC0_PCI
:
1871 /* Avoid gcc warning */
1879 static void dcr_write_epcpc (void *opaque
, int dcrn
, uint32_t val
)
1881 ppc405ep_cpc_t
*cpc
;
1885 case PPC405EP_CPC0_BOOT
:
1886 /* Read-only register */
1888 case PPC405EP_CPC0_EPCTL
:
1889 /* Don't care for now */
1890 cpc
->epctl
= val
& 0xC00000F3;
1892 case PPC405EP_CPC0_PLLMR0
:
1893 cpc
->pllmr
[0] = val
& 0x00633333;
1894 ppc405ep_compute_clocks(cpc
);
1896 case PPC405EP_CPC0_PLLMR1
:
1897 cpc
->pllmr
[1] = val
& 0xC0F73FFF;
1898 ppc405ep_compute_clocks(cpc
);
1900 case PPC405EP_CPC0_UCR
:
1901 /* UART control - don't care for now */
1902 cpc
->ucr
= val
& 0x003F7F7F;
1904 case PPC405EP_CPC0_SRR
:
1907 case PPC405EP_CPC0_JTAGID
:
1910 case PPC405EP_CPC0_PCI
:
1916 static void ppc405ep_cpc_reset (void *opaque
)
1918 ppc405ep_cpc_t
*cpc
= opaque
;
1920 cpc
->boot
= 0x00000010; /* Boot from PCI - IIC EEPROM disabled */
1921 cpc
->epctl
= 0x00000000;
1922 cpc
->pllmr
[0] = 0x00011010;
1923 cpc
->pllmr
[1] = 0x40000000;
1924 cpc
->ucr
= 0x00000000;
1925 cpc
->srr
= 0x00040000;
1926 cpc
->pci
= 0x00000000;
1927 cpc
->er
= 0x00000000;
1928 cpc
->fr
= 0x00000000;
1929 cpc
->sr
= 0x00000000;
1930 ppc405ep_compute_clocks(cpc
);
1933 /* XXX: sysclk should be between 25 and 100 MHz */
1934 static void ppc405ep_cpc_init (CPUPPCState
*env
, clk_setup_t clk_setup
[8],
1937 ppc405ep_cpc_t
*cpc
;
1939 cpc
= g_malloc0(sizeof(ppc405ep_cpc_t
));
1940 memcpy(cpc
->clk_setup
, clk_setup
,
1941 PPC405EP_CLK_NB
* sizeof(clk_setup_t
));
1942 cpc
->jtagid
= 0x20267049;
1943 cpc
->sysclk
= sysclk
;
1944 qemu_register_reset(&ppc405ep_cpc_reset
, cpc
);
1945 ppc_dcr_register(env
, PPC405EP_CPC0_BOOT
, cpc
,
1946 &dcr_read_epcpc
, &dcr_write_epcpc
);
1947 ppc_dcr_register(env
, PPC405EP_CPC0_EPCTL
, cpc
,
1948 &dcr_read_epcpc
, &dcr_write_epcpc
);
1949 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR0
, cpc
,
1950 &dcr_read_epcpc
, &dcr_write_epcpc
);
1951 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR1
, cpc
,
1952 &dcr_read_epcpc
, &dcr_write_epcpc
);
1953 ppc_dcr_register(env
, PPC405EP_CPC0_UCR
, cpc
,
1954 &dcr_read_epcpc
, &dcr_write_epcpc
);
1955 ppc_dcr_register(env
, PPC405EP_CPC0_SRR
, cpc
,
1956 &dcr_read_epcpc
, &dcr_write_epcpc
);
1957 ppc_dcr_register(env
, PPC405EP_CPC0_JTAGID
, cpc
,
1958 &dcr_read_epcpc
, &dcr_write_epcpc
);
1959 ppc_dcr_register(env
, PPC405EP_CPC0_PCI
, cpc
,
1960 &dcr_read_epcpc
, &dcr_write_epcpc
);
1962 ppc_dcr_register(env
, PPC405EP_CPC0_ER
, cpc
,
1963 &dcr_read_epcpc
, &dcr_write_epcpc
);
1964 ppc_dcr_register(env
, PPC405EP_CPC0_FR
, cpc
,
1965 &dcr_read_epcpc
, &dcr_write_epcpc
);
1966 ppc_dcr_register(env
, PPC405EP_CPC0_SR
, cpc
,
1967 &dcr_read_epcpc
, &dcr_write_epcpc
);
1971 CPUPPCState
*ppc405ep_init(MemoryRegion
*address_space_mem
,
1972 MemoryRegion ram_memories
[2],
1973 hwaddr ram_bases
[2],
1974 hwaddr ram_sizes
[2],
1975 uint32_t sysclk
, qemu_irq
**picp
,
1978 clk_setup_t clk_setup
[PPC405EP_CLK_NB
], tlb_clk_setup
;
1979 qemu_irq dma_irqs
[4], gpt_irqs
[5], mal_irqs
[4];
1982 qemu_irq
*pic
, *irqs
;
1984 memset(clk_setup
, 0, sizeof(clk_setup
));
1986 cpu
= ppc4xx_init(POWERPC_CPU_TYPE_NAME("405ep"),
1987 &clk_setup
[PPC405EP_CPU_CLK
],
1988 &tlb_clk_setup
, sysclk
);
1990 clk_setup
[PPC405EP_CPU_CLK
].cb
= tlb_clk_setup
.cb
;
1991 clk_setup
[PPC405EP_CPU_CLK
].opaque
= tlb_clk_setup
.opaque
;
1992 /* Internal devices init */
1993 /* Memory mapped devices registers */
1995 ppc4xx_plb_init(env
);
1996 /* PLB to OPB bridge */
1997 ppc4xx_pob_init(env
);
1999 ppc4xx_opba_init(0xef600600);
2000 /* Initialize timers */
2001 ppc_booke_timers_init(cpu
, sysclk
, 0);
2002 /* Universal interrupt controller */
2003 irqs
= g_malloc0(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2004 irqs
[PPCUIC_OUTPUT_INT
] =
2005 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2006 irqs
[PPCUIC_OUTPUT_CINT
] =
2007 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2008 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2010 /* SDRAM controller */
2011 /* XXX 405EP has no ECC interrupt */
2012 ppc4xx_sdram_init(env
, pic
[17], 2, ram_memories
,
2013 ram_bases
, ram_sizes
, do_init
);
2014 /* External bus controller */
2015 ppc405_ebc_init(env
);
2016 /* DMA controller */
2017 dma_irqs
[0] = pic
[5];
2018 dma_irqs
[1] = pic
[6];
2019 dma_irqs
[2] = pic
[7];
2020 dma_irqs
[3] = pic
[8];
2021 ppc405_dma_init(env
, dma_irqs
);
2022 /* IIC controller */
2023 sysbus_create_simple(TYPE_PPC4xx_I2C
, 0xef600500, pic
[2]);
2025 ppc405_gpio_init(0xef600700);
2027 if (serial_hd(0) != NULL
) {
2028 serial_mm_init(address_space_mem
, 0xef600300, 0, pic
[0],
2029 PPC_SERIAL_MM_BAUDBASE
, serial_hd(0),
2032 if (serial_hd(1) != NULL
) {
2033 serial_mm_init(address_space_mem
, 0xef600400, 0, pic
[1],
2034 PPC_SERIAL_MM_BAUDBASE
, serial_hd(1),
2038 ppc405_ocm_init(env
);
2040 gpt_irqs
[0] = pic
[19];
2041 gpt_irqs
[1] = pic
[20];
2042 gpt_irqs
[2] = pic
[21];
2043 gpt_irqs
[3] = pic
[22];
2044 gpt_irqs
[4] = pic
[23];
2045 ppc4xx_gpt_init(0xef600000, gpt_irqs
);
2047 /* Uses pic[3], pic[16], pic[18] */
2049 mal_irqs
[0] = pic
[11];
2050 mal_irqs
[1] = pic
[12];
2051 mal_irqs
[2] = pic
[13];
2052 mal_irqs
[3] = pic
[14];
2053 ppc4xx_mal_init(env
, 4, 2, mal_irqs
);
2055 /* Uses pic[9], pic[15], pic[17] */
2057 ppc405ep_cpc_init(env
, clk_setup
, sysclk
);