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
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "qapi/error.h"
29 #include "hw/ppc/ppc.h"
30 #include "hw/i2c/ppc4xx_i2c.h"
33 #include "hw/char/serial.h"
34 #include "qemu/timer.h"
35 #include "sysemu/reset.h"
36 #include "sysemu/sysemu.h"
37 #include "exec/address-spaces.h"
38 #include "hw/intc/ppc-uic.h"
39 #include "hw/qdev-properties.h"
40 #include "qapi/error.h"
45 //#define DEBUG_SERIAL
48 //#define DEBUG_CLOCKS
49 //#define DEBUG_CLOCKS_LL
51 ram_addr_t
ppc405_set_bootinfo (CPUPPCState
*env
, ppc4xx_bd_info_t
*bd
,
54 CPUState
*cs
= env_cpu(env
);
58 /* We put the bd structure at the top of memory */
59 if (bd
->bi_memsize
>= 0x01000000UL
)
60 bdloc
= 0x01000000UL
- sizeof(struct ppc4xx_bd_info_t
);
62 bdloc
= bd
->bi_memsize
- sizeof(struct ppc4xx_bd_info_t
);
63 stl_be_phys(cs
->as
, bdloc
+ 0x00, bd
->bi_memstart
);
64 stl_be_phys(cs
->as
, bdloc
+ 0x04, bd
->bi_memsize
);
65 stl_be_phys(cs
->as
, bdloc
+ 0x08, bd
->bi_flashstart
);
66 stl_be_phys(cs
->as
, bdloc
+ 0x0C, bd
->bi_flashsize
);
67 stl_be_phys(cs
->as
, bdloc
+ 0x10, bd
->bi_flashoffset
);
68 stl_be_phys(cs
->as
, bdloc
+ 0x14, bd
->bi_sramstart
);
69 stl_be_phys(cs
->as
, bdloc
+ 0x18, bd
->bi_sramsize
);
70 stl_be_phys(cs
->as
, bdloc
+ 0x1C, bd
->bi_bootflags
);
71 stl_be_phys(cs
->as
, bdloc
+ 0x20, bd
->bi_ipaddr
);
72 for (i
= 0; i
< 6; i
++) {
73 stb_phys(cs
->as
, bdloc
+ 0x24 + i
, bd
->bi_enetaddr
[i
]);
75 stw_be_phys(cs
->as
, bdloc
+ 0x2A, bd
->bi_ethspeed
);
76 stl_be_phys(cs
->as
, bdloc
+ 0x2C, bd
->bi_intfreq
);
77 stl_be_phys(cs
->as
, bdloc
+ 0x30, bd
->bi_busfreq
);
78 stl_be_phys(cs
->as
, bdloc
+ 0x34, bd
->bi_baudrate
);
79 for (i
= 0; i
< 4; i
++) {
80 stb_phys(cs
->as
, bdloc
+ 0x38 + i
, bd
->bi_s_version
[i
]);
82 for (i
= 0; i
< 32; i
++) {
83 stb_phys(cs
->as
, bdloc
+ 0x3C + i
, bd
->bi_r_version
[i
]);
85 stl_be_phys(cs
->as
, bdloc
+ 0x5C, bd
->bi_plb_busfreq
);
86 stl_be_phys(cs
->as
, bdloc
+ 0x60, bd
->bi_pci_busfreq
);
87 for (i
= 0; i
< 6; i
++) {
88 stb_phys(cs
->as
, bdloc
+ 0x64 + i
, bd
->bi_pci_enetaddr
[i
]);
91 if (flags
& 0x00000001) {
92 for (i
= 0; i
< 6; i
++)
93 stb_phys(cs
->as
, bdloc
+ n
++, bd
->bi_pci_enetaddr2
[i
]);
95 stl_be_phys(cs
->as
, bdloc
+ n
, bd
->bi_opbfreq
);
97 for (i
= 0; i
< 2; i
++) {
98 stl_be_phys(cs
->as
, bdloc
+ n
, bd
->bi_iic_fast
[i
]);
105 /*****************************************************************************/
106 /* Shared peripherals */
108 /*****************************************************************************/
109 /* Peripheral local bus arbitrer */
119 typedef struct ppc4xx_plb_t ppc4xx_plb_t
;
120 struct ppc4xx_plb_t
{
126 static uint32_t dcr_read_plb (void *opaque
, int dcrn
)
143 /* Avoid gcc warning */
151 static void dcr_write_plb (void *opaque
, int dcrn
, uint32_t val
)
158 /* We don't care about the actual parameters written as
159 * we don't manage any priorities on the bus
161 plb
->acr
= val
& 0xF8000000;
173 static void ppc4xx_plb_reset (void *opaque
)
178 plb
->acr
= 0x00000000;
179 plb
->bear
= 0x00000000;
180 plb
->besr
= 0x00000000;
183 void ppc4xx_plb_init(CPUPPCState
*env
)
187 plb
= g_malloc0(sizeof(ppc4xx_plb_t
));
188 ppc_dcr_register(env
, PLB3A0_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
189 ppc_dcr_register(env
, PLB4A0_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
190 ppc_dcr_register(env
, PLB0_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
191 ppc_dcr_register(env
, PLB0_BEAR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
192 ppc_dcr_register(env
, PLB0_BESR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
193 ppc_dcr_register(env
, PLB4A1_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
194 qemu_register_reset(ppc4xx_plb_reset
, plb
);
197 /*****************************************************************************/
198 /* PLB to OPB bridge */
205 typedef struct ppc4xx_pob_t ppc4xx_pob_t
;
206 struct ppc4xx_pob_t
{
212 static uint32_t dcr_read_pob (void *opaque
, int dcrn
)
229 /* Avoid gcc warning */
237 static void dcr_write_pob (void *opaque
, int dcrn
, uint32_t val
)
257 static void ppc4xx_pob_reset (void *opaque
)
263 pob
->bear
= 0x00000000;
264 pob
->besr0
= 0x0000000;
265 pob
->besr1
= 0x0000000;
268 static void ppc4xx_pob_init(CPUPPCState
*env
)
272 pob
= g_malloc0(sizeof(ppc4xx_pob_t
));
273 ppc_dcr_register(env
, POB0_BEAR
, pob
, &dcr_read_pob
, &dcr_write_pob
);
274 ppc_dcr_register(env
, POB0_BESR0
, pob
, &dcr_read_pob
, &dcr_write_pob
);
275 ppc_dcr_register(env
, POB0_BESR1
, pob
, &dcr_read_pob
, &dcr_write_pob
);
276 qemu_register_reset(ppc4xx_pob_reset
, pob
);
279 /*****************************************************************************/
281 typedef struct ppc4xx_opba_t ppc4xx_opba_t
;
282 struct ppc4xx_opba_t
{
288 static uint64_t opba_readb(void *opaque
, hwaddr addr
, unsigned size
)
294 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
312 static void opba_writeb(void *opaque
, hwaddr addr
, uint64_t value
,
318 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
324 opba
->cr
= value
& 0xF8;
327 opba
->pr
= value
& 0xFF;
333 static const MemoryRegionOps opba_ops
= {
335 .write
= opba_writeb
,
336 .impl
.min_access_size
= 1,
337 .impl
.max_access_size
= 1,
338 .valid
.min_access_size
= 1,
339 .valid
.max_access_size
= 4,
340 .endianness
= DEVICE_BIG_ENDIAN
,
343 static void ppc4xx_opba_reset (void *opaque
)
348 opba
->cr
= 0x00; /* No dynamic priorities - park disabled */
352 static void ppc4xx_opba_init(hwaddr base
)
356 opba
= g_malloc0(sizeof(ppc4xx_opba_t
));
358 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
360 memory_region_init_io(&opba
->io
, NULL
, &opba_ops
, opba
, "opba", 0x002);
361 memory_region_add_subregion(get_system_memory(), base
, &opba
->io
);
362 qemu_register_reset(ppc4xx_opba_reset
, opba
);
365 /*****************************************************************************/
366 /* Code decompression controller */
369 /*****************************************************************************/
370 /* Peripheral controller */
371 typedef struct ppc4xx_ebc_t ppc4xx_ebc_t
;
372 struct ppc4xx_ebc_t
{
383 EBC0_CFGADDR
= 0x012,
384 EBC0_CFGDATA
= 0x013,
387 static uint32_t dcr_read_ebc (void *opaque
, int dcrn
)
399 case 0x00: /* B0CR */
402 case 0x01: /* B1CR */
405 case 0x02: /* B2CR */
408 case 0x03: /* B3CR */
411 case 0x04: /* B4CR */
414 case 0x05: /* B5CR */
417 case 0x06: /* B6CR */
420 case 0x07: /* B7CR */
423 case 0x10: /* B0AP */
426 case 0x11: /* B1AP */
429 case 0x12: /* B2AP */
432 case 0x13: /* B3AP */
435 case 0x14: /* B4AP */
438 case 0x15: /* B5AP */
441 case 0x16: /* B6AP */
444 case 0x17: /* B7AP */
447 case 0x20: /* BEAR */
450 case 0x21: /* BESR0 */
453 case 0x22: /* BESR1 */
472 static void dcr_write_ebc (void *opaque
, int dcrn
, uint32_t val
)
483 case 0x00: /* B0CR */
485 case 0x01: /* B1CR */
487 case 0x02: /* B2CR */
489 case 0x03: /* B3CR */
491 case 0x04: /* B4CR */
493 case 0x05: /* B5CR */
495 case 0x06: /* B6CR */
497 case 0x07: /* B7CR */
499 case 0x10: /* B0AP */
501 case 0x11: /* B1AP */
503 case 0x12: /* B2AP */
505 case 0x13: /* B3AP */
507 case 0x14: /* B4AP */
509 case 0x15: /* B5AP */
511 case 0x16: /* B6AP */
513 case 0x17: /* B7AP */
515 case 0x20: /* BEAR */
517 case 0x21: /* BESR0 */
519 case 0x22: /* BESR1 */
532 static void ebc_reset (void *opaque
)
538 ebc
->addr
= 0x00000000;
539 ebc
->bap
[0] = 0x7F8FFE80;
540 ebc
->bcr
[0] = 0xFFE28000;
541 for (i
= 0; i
< 8; i
++) {
542 ebc
->bap
[i
] = 0x00000000;
543 ebc
->bcr
[i
] = 0x00000000;
545 ebc
->besr0
= 0x00000000;
546 ebc
->besr1
= 0x00000000;
547 ebc
->cfg
= 0x80400000;
550 void ppc405_ebc_init(CPUPPCState
*env
)
554 ebc
= g_malloc0(sizeof(ppc4xx_ebc_t
));
555 qemu_register_reset(&ebc_reset
, ebc
);
556 ppc_dcr_register(env
, EBC0_CFGADDR
,
557 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
558 ppc_dcr_register(env
, EBC0_CFGDATA
,
559 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
562 /*****************************************************************************/
591 typedef struct ppc405_dma_t ppc405_dma_t
;
592 struct ppc405_dma_t
{
605 static uint32_t dcr_read_dma (void *opaque
, int dcrn
)
610 static void dcr_write_dma (void *opaque
, int dcrn
, uint32_t val
)
614 static void ppc405_dma_reset (void *opaque
)
620 for (i
= 0; i
< 4; i
++) {
621 dma
->cr
[i
] = 0x00000000;
622 dma
->ct
[i
] = 0x00000000;
623 dma
->da
[i
] = 0x00000000;
624 dma
->sa
[i
] = 0x00000000;
625 dma
->sg
[i
] = 0x00000000;
627 dma
->sr
= 0x00000000;
628 dma
->sgc
= 0x00000000;
629 dma
->slp
= 0x7C000000;
630 dma
->pol
= 0x00000000;
633 static void ppc405_dma_init(CPUPPCState
*env
, qemu_irq irqs
[4])
637 dma
= g_malloc0(sizeof(ppc405_dma_t
));
638 memcpy(dma
->irqs
, irqs
, 4 * sizeof(qemu_irq
));
639 qemu_register_reset(&ppc405_dma_reset
, dma
);
640 ppc_dcr_register(env
, DMA0_CR0
,
641 dma
, &dcr_read_dma
, &dcr_write_dma
);
642 ppc_dcr_register(env
, DMA0_CT0
,
643 dma
, &dcr_read_dma
, &dcr_write_dma
);
644 ppc_dcr_register(env
, DMA0_DA0
,
645 dma
, &dcr_read_dma
, &dcr_write_dma
);
646 ppc_dcr_register(env
, DMA0_SA0
,
647 dma
, &dcr_read_dma
, &dcr_write_dma
);
648 ppc_dcr_register(env
, DMA0_SG0
,
649 dma
, &dcr_read_dma
, &dcr_write_dma
);
650 ppc_dcr_register(env
, DMA0_CR1
,
651 dma
, &dcr_read_dma
, &dcr_write_dma
);
652 ppc_dcr_register(env
, DMA0_CT1
,
653 dma
, &dcr_read_dma
, &dcr_write_dma
);
654 ppc_dcr_register(env
, DMA0_DA1
,
655 dma
, &dcr_read_dma
, &dcr_write_dma
);
656 ppc_dcr_register(env
, DMA0_SA1
,
657 dma
, &dcr_read_dma
, &dcr_write_dma
);
658 ppc_dcr_register(env
, DMA0_SG1
,
659 dma
, &dcr_read_dma
, &dcr_write_dma
);
660 ppc_dcr_register(env
, DMA0_CR2
,
661 dma
, &dcr_read_dma
, &dcr_write_dma
);
662 ppc_dcr_register(env
, DMA0_CT2
,
663 dma
, &dcr_read_dma
, &dcr_write_dma
);
664 ppc_dcr_register(env
, DMA0_DA2
,
665 dma
, &dcr_read_dma
, &dcr_write_dma
);
666 ppc_dcr_register(env
, DMA0_SA2
,
667 dma
, &dcr_read_dma
, &dcr_write_dma
);
668 ppc_dcr_register(env
, DMA0_SG2
,
669 dma
, &dcr_read_dma
, &dcr_write_dma
);
670 ppc_dcr_register(env
, DMA0_CR3
,
671 dma
, &dcr_read_dma
, &dcr_write_dma
);
672 ppc_dcr_register(env
, DMA0_CT3
,
673 dma
, &dcr_read_dma
, &dcr_write_dma
);
674 ppc_dcr_register(env
, DMA0_DA3
,
675 dma
, &dcr_read_dma
, &dcr_write_dma
);
676 ppc_dcr_register(env
, DMA0_SA3
,
677 dma
, &dcr_read_dma
, &dcr_write_dma
);
678 ppc_dcr_register(env
, DMA0_SG3
,
679 dma
, &dcr_read_dma
, &dcr_write_dma
);
680 ppc_dcr_register(env
, DMA0_SR
,
681 dma
, &dcr_read_dma
, &dcr_write_dma
);
682 ppc_dcr_register(env
, DMA0_SGC
,
683 dma
, &dcr_read_dma
, &dcr_write_dma
);
684 ppc_dcr_register(env
, DMA0_SLP
,
685 dma
, &dcr_read_dma
, &dcr_write_dma
);
686 ppc_dcr_register(env
, DMA0_POL
,
687 dma
, &dcr_read_dma
, &dcr_write_dma
);
690 /*****************************************************************************/
692 typedef struct ppc405_gpio_t ppc405_gpio_t
;
693 struct ppc405_gpio_t
{
708 static uint64_t ppc405_gpio_read(void *opaque
, hwaddr addr
, unsigned size
)
711 printf("%s: addr " TARGET_FMT_plx
" size %d\n", __func__
, addr
, size
);
717 static void ppc405_gpio_write(void *opaque
, hwaddr addr
, uint64_t value
,
721 printf("%s: addr " TARGET_FMT_plx
" size %d val %08" PRIx32
"\n",
722 __func__
, addr
, size
, value
);
726 static const MemoryRegionOps ppc405_gpio_ops
= {
727 .read
= ppc405_gpio_read
,
728 .write
= ppc405_gpio_write
,
729 .endianness
= DEVICE_NATIVE_ENDIAN
,
732 static void ppc405_gpio_reset (void *opaque
)
736 static void ppc405_gpio_init(hwaddr base
)
740 gpio
= g_malloc0(sizeof(ppc405_gpio_t
));
742 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
744 memory_region_init_io(&gpio
->io
, NULL
, &ppc405_gpio_ops
, gpio
, "pgio", 0x038);
745 memory_region_add_subregion(get_system_memory(), base
, &gpio
->io
);
746 qemu_register_reset(&ppc405_gpio_reset
, gpio
);
749 /*****************************************************************************/
753 OCM0_ISACNTL
= 0x019,
755 OCM0_DSACNTL
= 0x01B,
758 typedef struct ppc405_ocm_t ppc405_ocm_t
;
759 struct ppc405_ocm_t
{
761 MemoryRegion isarc_ram
;
762 MemoryRegion dsarc_ram
;
769 static void ocm_update_mappings (ppc405_ocm_t
*ocm
,
770 uint32_t isarc
, uint32_t isacntl
,
771 uint32_t dsarc
, uint32_t dsacntl
)
774 printf("OCM update ISA %08" PRIx32
" %08" PRIx32
" (%08" PRIx32
775 " %08" PRIx32
") DSA %08" PRIx32
" %08" PRIx32
776 " (%08" PRIx32
" %08" PRIx32
")\n",
777 isarc
, isacntl
, dsarc
, dsacntl
,
778 ocm
->isarc
, ocm
->isacntl
, ocm
->dsarc
, ocm
->dsacntl
);
780 if (ocm
->isarc
!= isarc
||
781 (ocm
->isacntl
& 0x80000000) != (isacntl
& 0x80000000)) {
782 if (ocm
->isacntl
& 0x80000000) {
783 /* Unmap previously assigned memory region */
784 printf("OCM unmap ISA %08" PRIx32
"\n", ocm
->isarc
);
785 memory_region_del_subregion(get_system_memory(), &ocm
->isarc_ram
);
787 if (isacntl
& 0x80000000) {
788 /* Map new instruction memory region */
790 printf("OCM map ISA %08" PRIx32
"\n", isarc
);
792 memory_region_add_subregion(get_system_memory(), isarc
,
796 if (ocm
->dsarc
!= dsarc
||
797 (ocm
->dsacntl
& 0x80000000) != (dsacntl
& 0x80000000)) {
798 if (ocm
->dsacntl
& 0x80000000) {
799 /* Beware not to unmap the region we just mapped */
800 if (!(isacntl
& 0x80000000) || ocm
->dsarc
!= isarc
) {
801 /* Unmap previously assigned memory region */
803 printf("OCM unmap DSA %08" PRIx32
"\n", ocm
->dsarc
);
805 memory_region_del_subregion(get_system_memory(),
809 if (dsacntl
& 0x80000000) {
810 /* Beware not to remap the region we just mapped */
811 if (!(isacntl
& 0x80000000) || dsarc
!= isarc
) {
812 /* Map new data memory region */
814 printf("OCM map DSA %08" PRIx32
"\n", dsarc
);
816 memory_region_add_subregion(get_system_memory(), dsarc
,
823 static uint32_t dcr_read_ocm (void *opaque
, int dcrn
)
850 static void dcr_write_ocm (void *opaque
, int dcrn
, uint32_t val
)
853 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
858 isacntl
= ocm
->isacntl
;
859 dsacntl
= ocm
->dsacntl
;
862 isarc
= val
& 0xFC000000;
865 isacntl
= val
& 0xC0000000;
868 isarc
= val
& 0xFC000000;
871 isacntl
= val
& 0xC0000000;
874 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
877 ocm
->isacntl
= isacntl
;
878 ocm
->dsacntl
= dsacntl
;
881 static void ocm_reset (void *opaque
)
884 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
888 isacntl
= 0x00000000;
890 dsacntl
= 0x00000000;
891 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
894 ocm
->isacntl
= isacntl
;
895 ocm
->dsacntl
= dsacntl
;
898 static void ppc405_ocm_init(CPUPPCState
*env
)
902 ocm
= g_malloc0(sizeof(ppc405_ocm_t
));
903 /* XXX: Size is 4096 or 0x04000000 */
904 memory_region_init_ram(&ocm
->isarc_ram
, NULL
, "ppc405.ocm", 4 * KiB
,
906 memory_region_init_alias(&ocm
->dsarc_ram
, NULL
, "ppc405.dsarc",
907 &ocm
->isarc_ram
, 0, 4 * KiB
);
908 qemu_register_reset(&ocm_reset
, ocm
);
909 ppc_dcr_register(env
, OCM0_ISARC
,
910 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
911 ppc_dcr_register(env
, OCM0_ISACNTL
,
912 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
913 ppc_dcr_register(env
, OCM0_DSARC
,
914 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
915 ppc_dcr_register(env
, OCM0_DSACNTL
,
916 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
919 /*****************************************************************************/
920 /* General purpose timers */
921 typedef struct ppc4xx_gpt_t ppc4xx_gpt_t
;
922 struct ppc4xx_gpt_t
{
937 static int ppc4xx_gpt_compare (ppc4xx_gpt_t
*gpt
, int n
)
943 static void ppc4xx_gpt_set_output (ppc4xx_gpt_t
*gpt
, int n
, int level
)
948 static void ppc4xx_gpt_set_outputs (ppc4xx_gpt_t
*gpt
)
954 for (i
= 0; i
< 5; i
++) {
955 if (gpt
->oe
& mask
) {
956 /* Output is enabled */
957 if (ppc4xx_gpt_compare(gpt
, i
)) {
958 /* Comparison is OK */
959 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
);
961 /* Comparison is KO */
962 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
? 0 : 1);
969 static void ppc4xx_gpt_set_irqs (ppc4xx_gpt_t
*gpt
)
975 for (i
= 0; i
< 5; i
++) {
976 if (gpt
->is
& gpt
->im
& mask
)
977 qemu_irq_raise(gpt
->irqs
[i
]);
979 qemu_irq_lower(gpt
->irqs
[i
]);
984 static void ppc4xx_gpt_compute_timer (ppc4xx_gpt_t
*gpt
)
989 static uint64_t ppc4xx_gpt_read(void *opaque
, hwaddr addr
, unsigned size
)
996 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1001 /* Time base counter */
1002 ret
= muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + gpt
->tb_offset
,
1003 gpt
->tb_freq
, NANOSECONDS_PER_SECOND
);
1014 /* Interrupt mask */
1019 /* Interrupt status */
1023 /* Interrupt enable */
1028 idx
= (addr
- 0x80) >> 2;
1029 ret
= gpt
->comp
[idx
];
1033 idx
= (addr
- 0xC0) >> 2;
1034 ret
= gpt
->mask
[idx
];
1044 static void ppc4xx_gpt_write(void *opaque
, hwaddr addr
, uint64_t value
,
1051 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1057 /* Time base counter */
1058 gpt
->tb_offset
= muldiv64(value
, NANOSECONDS_PER_SECOND
, gpt
->tb_freq
)
1059 - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1060 ppc4xx_gpt_compute_timer(gpt
);
1064 gpt
->oe
= value
& 0xF8000000;
1065 ppc4xx_gpt_set_outputs(gpt
);
1069 gpt
->ol
= value
& 0xF8000000;
1070 ppc4xx_gpt_set_outputs(gpt
);
1073 /* Interrupt mask */
1074 gpt
->im
= value
& 0x0000F800;
1077 /* Interrupt status set */
1078 gpt
->is
|= value
& 0x0000F800;
1079 ppc4xx_gpt_set_irqs(gpt
);
1082 /* Interrupt status clear */
1083 gpt
->is
&= ~(value
& 0x0000F800);
1084 ppc4xx_gpt_set_irqs(gpt
);
1087 /* Interrupt enable */
1088 gpt
->ie
= value
& 0x0000F800;
1089 ppc4xx_gpt_set_irqs(gpt
);
1093 idx
= (addr
- 0x80) >> 2;
1094 gpt
->comp
[idx
] = value
& 0xF8000000;
1095 ppc4xx_gpt_compute_timer(gpt
);
1099 idx
= (addr
- 0xC0) >> 2;
1100 gpt
->mask
[idx
] = value
& 0xF8000000;
1101 ppc4xx_gpt_compute_timer(gpt
);
1106 static const MemoryRegionOps gpt_ops
= {
1107 .read
= ppc4xx_gpt_read
,
1108 .write
= ppc4xx_gpt_write
,
1109 .valid
.min_access_size
= 4,
1110 .valid
.max_access_size
= 4,
1111 .endianness
= DEVICE_NATIVE_ENDIAN
,
1114 static void ppc4xx_gpt_cb (void *opaque
)
1119 ppc4xx_gpt_set_irqs(gpt
);
1120 ppc4xx_gpt_set_outputs(gpt
);
1121 ppc4xx_gpt_compute_timer(gpt
);
1124 static void ppc4xx_gpt_reset (void *opaque
)
1130 timer_del(gpt
->timer
);
1131 gpt
->oe
= 0x00000000;
1132 gpt
->ol
= 0x00000000;
1133 gpt
->im
= 0x00000000;
1134 gpt
->is
= 0x00000000;
1135 gpt
->ie
= 0x00000000;
1136 for (i
= 0; i
< 5; i
++) {
1137 gpt
->comp
[i
] = 0x00000000;
1138 gpt
->mask
[i
] = 0x00000000;
1142 static void ppc4xx_gpt_init(hwaddr base
, qemu_irq irqs
[5])
1147 gpt
= g_malloc0(sizeof(ppc4xx_gpt_t
));
1148 for (i
= 0; i
< 5; i
++) {
1149 gpt
->irqs
[i
] = irqs
[i
];
1151 gpt
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &ppc4xx_gpt_cb
, gpt
);
1153 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1155 memory_region_init_io(&gpt
->iomem
, NULL
, &gpt_ops
, gpt
, "gpt", 0x0d4);
1156 memory_region_add_subregion(get_system_memory(), base
, &gpt
->iomem
);
1157 qemu_register_reset(ppc4xx_gpt_reset
, gpt
);
1160 /*****************************************************************************/
1164 PPC405EP_CPC0_PLLMR0
= 0x0F0,
1165 PPC405EP_CPC0_BOOT
= 0x0F1,
1166 PPC405EP_CPC0_EPCTL
= 0x0F3,
1167 PPC405EP_CPC0_PLLMR1
= 0x0F4,
1168 PPC405EP_CPC0_UCR
= 0x0F5,
1169 PPC405EP_CPC0_SRR
= 0x0F6,
1170 PPC405EP_CPC0_JTAGID
= 0x0F7,
1171 PPC405EP_CPC0_PCI
= 0x0F9,
1173 PPC405EP_CPC0_ER
= xxx
,
1174 PPC405EP_CPC0_FR
= xxx
,
1175 PPC405EP_CPC0_SR
= xxx
,
1180 PPC405EP_CPU_CLK
= 0,
1181 PPC405EP_PLB_CLK
= 1,
1182 PPC405EP_OPB_CLK
= 2,
1183 PPC405EP_EBC_CLK
= 3,
1184 PPC405EP_MAL_CLK
= 4,
1185 PPC405EP_PCI_CLK
= 5,
1186 PPC405EP_UART0_CLK
= 6,
1187 PPC405EP_UART1_CLK
= 7,
1188 PPC405EP_CLK_NB
= 8,
1191 typedef struct ppc405ep_cpc_t ppc405ep_cpc_t
;
1192 struct ppc405ep_cpc_t
{
1194 clk_setup_t clk_setup
[PPC405EP_CLK_NB
];
1202 /* Clock and power management */
1208 static void ppc405ep_compute_clocks (ppc405ep_cpc_t
*cpc
)
1210 uint32_t CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
;
1211 uint32_t UART0_clk
, UART1_clk
;
1212 uint64_t VCO_out
, PLL_out
;
1216 if ((cpc
->pllmr
[1] & 0x80000000) && !(cpc
->pllmr
[1] & 0x40000000)) {
1217 M
= (((cpc
->pllmr
[1] >> 20) - 1) & 0xF) + 1; /* FBMUL */
1218 #ifdef DEBUG_CLOCKS_LL
1219 printf("FBMUL %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 20) & 0xF, M
);
1221 D
= 8 - ((cpc
->pllmr
[1] >> 16) & 0x7); /* FWDA */
1222 #ifdef DEBUG_CLOCKS_LL
1223 printf("FWDA %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 16) & 0x7, D
);
1225 VCO_out
= (uint64_t)cpc
->sysclk
* M
* D
;
1226 if (VCO_out
< 500000000UL || VCO_out
> 1000000000UL) {
1227 /* Error - unlock the PLL */
1228 printf("VCO out of range %" PRIu64
"\n", VCO_out
);
1230 cpc
->pllmr
[1] &= ~0x80000000;
1234 PLL_out
= VCO_out
/ D
;
1235 /* Pretend the PLL is locked */
1236 cpc
->boot
|= 0x00000001;
1241 PLL_out
= cpc
->sysclk
;
1242 if (cpc
->pllmr
[1] & 0x40000000) {
1243 /* Pretend the PLL is not locked */
1244 cpc
->boot
&= ~0x00000001;
1247 /* Now, compute all other clocks */
1248 D
= ((cpc
->pllmr
[0] >> 20) & 0x3) + 1; /* CCDV */
1249 #ifdef DEBUG_CLOCKS_LL
1250 printf("CCDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 20) & 0x3, D
);
1252 CPU_clk
= PLL_out
/ D
;
1253 D
= ((cpc
->pllmr
[0] >> 16) & 0x3) + 1; /* CBDV */
1254 #ifdef DEBUG_CLOCKS_LL
1255 printf("CBDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 16) & 0x3, D
);
1257 PLB_clk
= CPU_clk
/ D
;
1258 D
= ((cpc
->pllmr
[0] >> 12) & 0x3) + 1; /* OPDV */
1259 #ifdef DEBUG_CLOCKS_LL
1260 printf("OPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 12) & 0x3, D
);
1262 OPB_clk
= PLB_clk
/ D
;
1263 D
= ((cpc
->pllmr
[0] >> 8) & 0x3) + 2; /* EPDV */
1264 #ifdef DEBUG_CLOCKS_LL
1265 printf("EPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 8) & 0x3, D
);
1267 EBC_clk
= PLB_clk
/ D
;
1268 D
= ((cpc
->pllmr
[0] >> 4) & 0x3) + 1; /* MPDV */
1269 #ifdef DEBUG_CLOCKS_LL
1270 printf("MPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 4) & 0x3, D
);
1272 MAL_clk
= PLB_clk
/ D
;
1273 D
= (cpc
->pllmr
[0] & 0x3) + 1; /* PPDV */
1274 #ifdef DEBUG_CLOCKS_LL
1275 printf("PPDV %01" PRIx32
" %d\n", cpc
->pllmr
[0] & 0x3, D
);
1277 PCI_clk
= PLB_clk
/ D
;
1278 D
= ((cpc
->ucr
- 1) & 0x7F) + 1; /* U0DIV */
1279 #ifdef DEBUG_CLOCKS_LL
1280 printf("U0DIV %01" PRIx32
" %d\n", cpc
->ucr
& 0x7F, D
);
1282 UART0_clk
= PLL_out
/ D
;
1283 D
= (((cpc
->ucr
>> 8) - 1) & 0x7F) + 1; /* U1DIV */
1284 #ifdef DEBUG_CLOCKS_LL
1285 printf("U1DIV %01" PRIx32
" %d\n", (cpc
->ucr
>> 8) & 0x7F, D
);
1287 UART1_clk
= PLL_out
/ D
;
1289 printf("Setup PPC405EP clocks - sysclk %" PRIu32
" VCO %" PRIu64
1290 " PLL out %" PRIu64
" Hz\n", cpc
->sysclk
, VCO_out
, PLL_out
);
1291 printf("CPU %" PRIu32
" PLB %" PRIu32
" OPB %" PRIu32
" EBC %" PRIu32
1292 " MAL %" PRIu32
" PCI %" PRIu32
" UART0 %" PRIu32
1293 " UART1 %" PRIu32
"\n",
1294 CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
,
1295 UART0_clk
, UART1_clk
);
1297 /* Setup CPU clocks */
1298 clk_setup(&cpc
->clk_setup
[PPC405EP_CPU_CLK
], CPU_clk
);
1299 /* Setup PLB clock */
1300 clk_setup(&cpc
->clk_setup
[PPC405EP_PLB_CLK
], PLB_clk
);
1301 /* Setup OPB clock */
1302 clk_setup(&cpc
->clk_setup
[PPC405EP_OPB_CLK
], OPB_clk
);
1303 /* Setup external clock */
1304 clk_setup(&cpc
->clk_setup
[PPC405EP_EBC_CLK
], EBC_clk
);
1305 /* Setup MAL clock */
1306 clk_setup(&cpc
->clk_setup
[PPC405EP_MAL_CLK
], MAL_clk
);
1307 /* Setup PCI clock */
1308 clk_setup(&cpc
->clk_setup
[PPC405EP_PCI_CLK
], PCI_clk
);
1309 /* Setup UART0 clock */
1310 clk_setup(&cpc
->clk_setup
[PPC405EP_UART0_CLK
], UART0_clk
);
1311 /* Setup UART1 clock */
1312 clk_setup(&cpc
->clk_setup
[PPC405EP_UART1_CLK
], UART1_clk
);
1315 static uint32_t dcr_read_epcpc (void *opaque
, int dcrn
)
1317 ppc405ep_cpc_t
*cpc
;
1322 case PPC405EP_CPC0_BOOT
:
1325 case PPC405EP_CPC0_EPCTL
:
1328 case PPC405EP_CPC0_PLLMR0
:
1329 ret
= cpc
->pllmr
[0];
1331 case PPC405EP_CPC0_PLLMR1
:
1332 ret
= cpc
->pllmr
[1];
1334 case PPC405EP_CPC0_UCR
:
1337 case PPC405EP_CPC0_SRR
:
1340 case PPC405EP_CPC0_JTAGID
:
1343 case PPC405EP_CPC0_PCI
:
1347 /* Avoid gcc warning */
1355 static void dcr_write_epcpc (void *opaque
, int dcrn
, uint32_t val
)
1357 ppc405ep_cpc_t
*cpc
;
1361 case PPC405EP_CPC0_BOOT
:
1362 /* Read-only register */
1364 case PPC405EP_CPC0_EPCTL
:
1365 /* Don't care for now */
1366 cpc
->epctl
= val
& 0xC00000F3;
1368 case PPC405EP_CPC0_PLLMR0
:
1369 cpc
->pllmr
[0] = val
& 0x00633333;
1370 ppc405ep_compute_clocks(cpc
);
1372 case PPC405EP_CPC0_PLLMR1
:
1373 cpc
->pllmr
[1] = val
& 0xC0F73FFF;
1374 ppc405ep_compute_clocks(cpc
);
1376 case PPC405EP_CPC0_UCR
:
1377 /* UART control - don't care for now */
1378 cpc
->ucr
= val
& 0x003F7F7F;
1380 case PPC405EP_CPC0_SRR
:
1383 case PPC405EP_CPC0_JTAGID
:
1386 case PPC405EP_CPC0_PCI
:
1392 static void ppc405ep_cpc_reset (void *opaque
)
1394 ppc405ep_cpc_t
*cpc
= opaque
;
1396 cpc
->boot
= 0x00000010; /* Boot from PCI - IIC EEPROM disabled */
1397 cpc
->epctl
= 0x00000000;
1398 cpc
->pllmr
[0] = 0x00011010;
1399 cpc
->pllmr
[1] = 0x40000000;
1400 cpc
->ucr
= 0x00000000;
1401 cpc
->srr
= 0x00040000;
1402 cpc
->pci
= 0x00000000;
1403 cpc
->er
= 0x00000000;
1404 cpc
->fr
= 0x00000000;
1405 cpc
->sr
= 0x00000000;
1406 ppc405ep_compute_clocks(cpc
);
1409 /* XXX: sysclk should be between 25 and 100 MHz */
1410 static void ppc405ep_cpc_init (CPUPPCState
*env
, clk_setup_t clk_setup
[8],
1413 ppc405ep_cpc_t
*cpc
;
1415 cpc
= g_malloc0(sizeof(ppc405ep_cpc_t
));
1416 memcpy(cpc
->clk_setup
, clk_setup
,
1417 PPC405EP_CLK_NB
* sizeof(clk_setup_t
));
1418 cpc
->jtagid
= 0x20267049;
1419 cpc
->sysclk
= sysclk
;
1420 qemu_register_reset(&ppc405ep_cpc_reset
, cpc
);
1421 ppc_dcr_register(env
, PPC405EP_CPC0_BOOT
, cpc
,
1422 &dcr_read_epcpc
, &dcr_write_epcpc
);
1423 ppc_dcr_register(env
, PPC405EP_CPC0_EPCTL
, cpc
,
1424 &dcr_read_epcpc
, &dcr_write_epcpc
);
1425 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR0
, cpc
,
1426 &dcr_read_epcpc
, &dcr_write_epcpc
);
1427 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR1
, cpc
,
1428 &dcr_read_epcpc
, &dcr_write_epcpc
);
1429 ppc_dcr_register(env
, PPC405EP_CPC0_UCR
, cpc
,
1430 &dcr_read_epcpc
, &dcr_write_epcpc
);
1431 ppc_dcr_register(env
, PPC405EP_CPC0_SRR
, cpc
,
1432 &dcr_read_epcpc
, &dcr_write_epcpc
);
1433 ppc_dcr_register(env
, PPC405EP_CPC0_JTAGID
, cpc
,
1434 &dcr_read_epcpc
, &dcr_write_epcpc
);
1435 ppc_dcr_register(env
, PPC405EP_CPC0_PCI
, cpc
,
1436 &dcr_read_epcpc
, &dcr_write_epcpc
);
1438 ppc_dcr_register(env
, PPC405EP_CPC0_ER
, cpc
,
1439 &dcr_read_epcpc
, &dcr_write_epcpc
);
1440 ppc_dcr_register(env
, PPC405EP_CPC0_FR
, cpc
,
1441 &dcr_read_epcpc
, &dcr_write_epcpc
);
1442 ppc_dcr_register(env
, PPC405EP_CPC0_SR
, cpc
,
1443 &dcr_read_epcpc
, &dcr_write_epcpc
);
1447 CPUPPCState
*ppc405ep_init(MemoryRegion
*address_space_mem
,
1448 MemoryRegion ram_memories
[2],
1449 hwaddr ram_bases
[2],
1450 hwaddr ram_sizes
[2],
1451 uint32_t sysclk
, DeviceState
**uicdevp
,
1454 clk_setup_t clk_setup
[PPC405EP_CLK_NB
], tlb_clk_setup
;
1455 qemu_irq dma_irqs
[4], gpt_irqs
[5], mal_irqs
[4];
1458 DeviceState
*uicdev
;
1459 SysBusDevice
*uicsbd
;
1461 memset(clk_setup
, 0, sizeof(clk_setup
));
1463 cpu
= ppc4xx_init(POWERPC_CPU_TYPE_NAME("405ep"),
1464 &clk_setup
[PPC405EP_CPU_CLK
],
1465 &tlb_clk_setup
, sysclk
);
1467 clk_setup
[PPC405EP_CPU_CLK
].cb
= tlb_clk_setup
.cb
;
1468 clk_setup
[PPC405EP_CPU_CLK
].opaque
= tlb_clk_setup
.opaque
;
1469 /* Internal devices init */
1470 /* Memory mapped devices registers */
1472 ppc4xx_plb_init(env
);
1473 /* PLB to OPB bridge */
1474 ppc4xx_pob_init(env
);
1476 ppc4xx_opba_init(0xef600600);
1477 /* Initialize timers */
1478 ppc_booke_timers_init(cpu
, sysclk
, 0);
1479 /* Universal interrupt controller */
1480 uicdev
= qdev_new(TYPE_PPC_UIC
);
1481 uicsbd
= SYS_BUS_DEVICE(uicdev
);
1483 object_property_set_link(OBJECT(uicdev
), "cpu", OBJECT(cpu
),
1485 sysbus_realize_and_unref(uicsbd
, &error_fatal
);
1487 sysbus_connect_irq(uicsbd
, PPCUIC_OUTPUT_INT
,
1488 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
]);
1489 sysbus_connect_irq(uicsbd
, PPCUIC_OUTPUT_CINT
,
1490 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
]);
1494 /* SDRAM controller */
1495 /* XXX 405EP has no ECC interrupt */
1496 ppc4xx_sdram_init(env
, qdev_get_gpio_in(uicdev
, 17), 2, ram_memories
,
1497 ram_bases
, ram_sizes
, do_init
);
1498 /* External bus controller */
1499 ppc405_ebc_init(env
);
1500 /* DMA controller */
1501 dma_irqs
[0] = qdev_get_gpio_in(uicdev
, 5);
1502 dma_irqs
[1] = qdev_get_gpio_in(uicdev
, 6);
1503 dma_irqs
[2] = qdev_get_gpio_in(uicdev
, 7);
1504 dma_irqs
[3] = qdev_get_gpio_in(uicdev
, 8);
1505 ppc405_dma_init(env
, dma_irqs
);
1506 /* IIC controller */
1507 sysbus_create_simple(TYPE_PPC4xx_I2C
, 0xef600500,
1508 qdev_get_gpio_in(uicdev
, 2));
1510 ppc405_gpio_init(0xef600700);
1512 if (serial_hd(0) != NULL
) {
1513 serial_mm_init(address_space_mem
, 0xef600300, 0,
1514 qdev_get_gpio_in(uicdev
, 0),
1515 PPC_SERIAL_MM_BAUDBASE
, serial_hd(0),
1518 if (serial_hd(1) != NULL
) {
1519 serial_mm_init(address_space_mem
, 0xef600400, 0,
1520 qdev_get_gpio_in(uicdev
, 1),
1521 PPC_SERIAL_MM_BAUDBASE
, serial_hd(1),
1525 ppc405_ocm_init(env
);
1527 gpt_irqs
[0] = qdev_get_gpio_in(uicdev
, 19);
1528 gpt_irqs
[1] = qdev_get_gpio_in(uicdev
, 20);
1529 gpt_irqs
[2] = qdev_get_gpio_in(uicdev
, 21);
1530 gpt_irqs
[3] = qdev_get_gpio_in(uicdev
, 22);
1531 gpt_irqs
[4] = qdev_get_gpio_in(uicdev
, 23);
1532 ppc4xx_gpt_init(0xef600000, gpt_irqs
);
1534 /* Uses UIC IRQs 3, 16, 18 */
1536 mal_irqs
[0] = qdev_get_gpio_in(uicdev
, 11);
1537 mal_irqs
[1] = qdev_get_gpio_in(uicdev
, 12);
1538 mal_irqs
[2] = qdev_get_gpio_in(uicdev
, 13);
1539 mal_irqs
[3] = qdev_get_gpio_in(uicdev
, 14);
1540 ppc4xx_mal_init(env
, 4, 2, mal_irqs
);
1542 /* Uses UIC IRQs 9, 15, 17 */
1544 ppc405ep_cpc_init(env
, clk_setup
, sysclk
);