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 "hw/ppc/ppc.h"
27 #include "hw/char/serial.h"
28 #include "qemu/timer.h"
29 #include "sysemu/sysemu.h"
31 #include "exec/address-spaces.h"
36 //#define DEBUG_SERIAL
41 //#define DEBUG_CLOCKS
42 //#define DEBUG_CLOCKS_LL
44 ram_addr_t
ppc405_set_bootinfo (CPUPPCState
*env
, ppc4xx_bd_info_t
*bd
,
47 CPUState
*cs
= ENV_GET_CPU(env
);
51 /* We put the bd structure at the top of memory */
52 if (bd
->bi_memsize
>= 0x01000000UL
)
53 bdloc
= 0x01000000UL
- sizeof(struct ppc4xx_bd_info_t
);
55 bdloc
= bd
->bi_memsize
- sizeof(struct ppc4xx_bd_info_t
);
56 stl_be_phys(cs
->as
, bdloc
+ 0x00, bd
->bi_memstart
);
57 stl_be_phys(cs
->as
, bdloc
+ 0x04, bd
->bi_memsize
);
58 stl_be_phys(cs
->as
, bdloc
+ 0x08, bd
->bi_flashstart
);
59 stl_be_phys(cs
->as
, bdloc
+ 0x0C, bd
->bi_flashsize
);
60 stl_be_phys(cs
->as
, bdloc
+ 0x10, bd
->bi_flashoffset
);
61 stl_be_phys(cs
->as
, bdloc
+ 0x14, bd
->bi_sramstart
);
62 stl_be_phys(cs
->as
, bdloc
+ 0x18, bd
->bi_sramsize
);
63 stl_be_phys(cs
->as
, bdloc
+ 0x1C, bd
->bi_bootflags
);
64 stl_be_phys(cs
->as
, bdloc
+ 0x20, bd
->bi_ipaddr
);
65 for (i
= 0; i
< 6; i
++) {
66 stb_phys(cs
->as
, bdloc
+ 0x24 + i
, bd
->bi_enetaddr
[i
]);
68 stw_be_phys(cs
->as
, bdloc
+ 0x2A, bd
->bi_ethspeed
);
69 stl_be_phys(cs
->as
, bdloc
+ 0x2C, bd
->bi_intfreq
);
70 stl_be_phys(cs
->as
, bdloc
+ 0x30, bd
->bi_busfreq
);
71 stl_be_phys(cs
->as
, bdloc
+ 0x34, bd
->bi_baudrate
);
72 for (i
= 0; i
< 4; i
++) {
73 stb_phys(cs
->as
, bdloc
+ 0x38 + i
, bd
->bi_s_version
[i
]);
75 for (i
= 0; i
< 32; i
++) {
76 stb_phys(cs
->as
, bdloc
+ 0x3C + i
, bd
->bi_r_version
[i
]);
78 stl_be_phys(cs
->as
, bdloc
+ 0x5C, bd
->bi_plb_busfreq
);
79 stl_be_phys(cs
->as
, bdloc
+ 0x60, bd
->bi_pci_busfreq
);
80 for (i
= 0; i
< 6; i
++) {
81 stb_phys(cs
->as
, bdloc
+ 0x64 + i
, bd
->bi_pci_enetaddr
[i
]);
84 if (flags
& 0x00000001) {
85 for (i
= 0; i
< 6; i
++)
86 stb_phys(cs
->as
, bdloc
+ n
++, bd
->bi_pci_enetaddr2
[i
]);
88 stl_be_phys(cs
->as
, bdloc
+ n
, bd
->bi_opbfreq
);
90 for (i
= 0; i
< 2; i
++) {
91 stl_be_phys(cs
->as
, bdloc
+ n
, bd
->bi_iic_fast
[i
]);
98 /*****************************************************************************/
99 /* Shared peripherals */
101 /*****************************************************************************/
102 /* Peripheral local bus arbitrer */
109 typedef struct ppc4xx_plb_t ppc4xx_plb_t
;
110 struct ppc4xx_plb_t
{
116 static uint32_t dcr_read_plb (void *opaque
, int dcrn
)
133 /* Avoid gcc warning */
141 static void dcr_write_plb (void *opaque
, int dcrn
, uint32_t val
)
148 /* We don't care about the actual parameters written as
149 * we don't manage any priorities on the bus
151 plb
->acr
= val
& 0xF8000000;
163 static void ppc4xx_plb_reset (void *opaque
)
168 plb
->acr
= 0x00000000;
169 plb
->bear
= 0x00000000;
170 plb
->besr
= 0x00000000;
173 static void ppc4xx_plb_init(CPUPPCState
*env
)
177 plb
= g_malloc0(sizeof(ppc4xx_plb_t
));
178 ppc_dcr_register(env
, PLB0_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
179 ppc_dcr_register(env
, PLB0_BEAR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
180 ppc_dcr_register(env
, PLB0_BESR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
181 qemu_register_reset(ppc4xx_plb_reset
, plb
);
184 /*****************************************************************************/
185 /* PLB to OPB bridge */
192 typedef struct ppc4xx_pob_t ppc4xx_pob_t
;
193 struct ppc4xx_pob_t
{
199 static uint32_t dcr_read_pob (void *opaque
, int dcrn
)
216 /* Avoid gcc warning */
224 static void dcr_write_pob (void *opaque
, int dcrn
, uint32_t val
)
244 static void ppc4xx_pob_reset (void *opaque
)
250 pob
->bear
= 0x00000000;
251 pob
->besr0
= 0x0000000;
252 pob
->besr1
= 0x0000000;
255 static void ppc4xx_pob_init(CPUPPCState
*env
)
259 pob
= g_malloc0(sizeof(ppc4xx_pob_t
));
260 ppc_dcr_register(env
, POB0_BEAR
, pob
, &dcr_read_pob
, &dcr_write_pob
);
261 ppc_dcr_register(env
, POB0_BESR0
, pob
, &dcr_read_pob
, &dcr_write_pob
);
262 ppc_dcr_register(env
, POB0_BESR1
, pob
, &dcr_read_pob
, &dcr_write_pob
);
263 qemu_register_reset(ppc4xx_pob_reset
, pob
);
266 /*****************************************************************************/
268 typedef struct ppc4xx_opba_t ppc4xx_opba_t
;
269 struct ppc4xx_opba_t
{
275 static uint32_t opba_readb (void *opaque
, hwaddr addr
)
281 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
299 static void opba_writeb (void *opaque
,
300 hwaddr addr
, uint32_t value
)
305 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
311 opba
->cr
= value
& 0xF8;
314 opba
->pr
= value
& 0xFF;
321 static uint32_t opba_readw (void *opaque
, hwaddr addr
)
326 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
328 ret
= opba_readb(opaque
, addr
) << 8;
329 ret
|= opba_readb(opaque
, addr
+ 1);
334 static void opba_writew (void *opaque
,
335 hwaddr addr
, uint32_t value
)
338 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
341 opba_writeb(opaque
, addr
, value
>> 8);
342 opba_writeb(opaque
, addr
+ 1, value
);
345 static uint32_t opba_readl (void *opaque
, hwaddr addr
)
350 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
352 ret
= opba_readb(opaque
, addr
) << 24;
353 ret
|= opba_readb(opaque
, addr
+ 1) << 16;
358 static void opba_writel (void *opaque
,
359 hwaddr addr
, uint32_t value
)
362 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
365 opba_writeb(opaque
, addr
, value
>> 24);
366 opba_writeb(opaque
, addr
+ 1, value
>> 16);
369 static const MemoryRegionOps opba_ops
= {
371 .read
= { opba_readb
, opba_readw
, opba_readl
, },
372 .write
= { opba_writeb
, opba_writew
, opba_writel
, },
374 .endianness
= DEVICE_NATIVE_ENDIAN
,
377 static void ppc4xx_opba_reset (void *opaque
)
382 opba
->cr
= 0x00; /* No dynamic priorities - park disabled */
386 static void ppc4xx_opba_init(hwaddr base
)
390 opba
= g_malloc0(sizeof(ppc4xx_opba_t
));
392 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
394 memory_region_init_io(&opba
->io
, NULL
, &opba_ops
, opba
, "opba", 0x002);
395 memory_region_add_subregion(get_system_memory(), base
, &opba
->io
);
396 qemu_register_reset(ppc4xx_opba_reset
, opba
);
399 /*****************************************************************************/
400 /* Code decompression controller */
403 /*****************************************************************************/
404 /* Peripheral controller */
405 typedef struct ppc4xx_ebc_t ppc4xx_ebc_t
;
406 struct ppc4xx_ebc_t
{
417 EBC0_CFGADDR
= 0x012,
418 EBC0_CFGDATA
= 0x013,
421 static uint32_t dcr_read_ebc (void *opaque
, int dcrn
)
433 case 0x00: /* B0CR */
436 case 0x01: /* B1CR */
439 case 0x02: /* B2CR */
442 case 0x03: /* B3CR */
445 case 0x04: /* B4CR */
448 case 0x05: /* B5CR */
451 case 0x06: /* B6CR */
454 case 0x07: /* B7CR */
457 case 0x10: /* B0AP */
460 case 0x11: /* B1AP */
463 case 0x12: /* B2AP */
466 case 0x13: /* B3AP */
469 case 0x14: /* B4AP */
472 case 0x15: /* B5AP */
475 case 0x16: /* B6AP */
478 case 0x17: /* B7AP */
481 case 0x20: /* BEAR */
484 case 0x21: /* BESR0 */
487 case 0x22: /* BESR1 */
506 static void dcr_write_ebc (void *opaque
, int dcrn
, uint32_t val
)
517 case 0x00: /* B0CR */
519 case 0x01: /* B1CR */
521 case 0x02: /* B2CR */
523 case 0x03: /* B3CR */
525 case 0x04: /* B4CR */
527 case 0x05: /* B5CR */
529 case 0x06: /* B6CR */
531 case 0x07: /* B7CR */
533 case 0x10: /* B0AP */
535 case 0x11: /* B1AP */
537 case 0x12: /* B2AP */
539 case 0x13: /* B3AP */
541 case 0x14: /* B4AP */
543 case 0x15: /* B5AP */
545 case 0x16: /* B6AP */
547 case 0x17: /* B7AP */
549 case 0x20: /* BEAR */
551 case 0x21: /* BESR0 */
553 case 0x22: /* BESR1 */
566 static void ebc_reset (void *opaque
)
572 ebc
->addr
= 0x00000000;
573 ebc
->bap
[0] = 0x7F8FFE80;
574 ebc
->bcr
[0] = 0xFFE28000;
575 for (i
= 0; i
< 8; i
++) {
576 ebc
->bap
[i
] = 0x00000000;
577 ebc
->bcr
[i
] = 0x00000000;
579 ebc
->besr0
= 0x00000000;
580 ebc
->besr1
= 0x00000000;
581 ebc
->cfg
= 0x80400000;
584 static void ppc405_ebc_init(CPUPPCState
*env
)
588 ebc
= g_malloc0(sizeof(ppc4xx_ebc_t
));
589 qemu_register_reset(&ebc_reset
, ebc
);
590 ppc_dcr_register(env
, EBC0_CFGADDR
,
591 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
592 ppc_dcr_register(env
, EBC0_CFGDATA
,
593 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
596 /*****************************************************************************/
625 typedef struct ppc405_dma_t ppc405_dma_t
;
626 struct ppc405_dma_t
{
639 static uint32_t dcr_read_dma (void *opaque
, int dcrn
)
644 static void dcr_write_dma (void *opaque
, int dcrn
, uint32_t val
)
648 static void ppc405_dma_reset (void *opaque
)
654 for (i
= 0; i
< 4; i
++) {
655 dma
->cr
[i
] = 0x00000000;
656 dma
->ct
[i
] = 0x00000000;
657 dma
->da
[i
] = 0x00000000;
658 dma
->sa
[i
] = 0x00000000;
659 dma
->sg
[i
] = 0x00000000;
661 dma
->sr
= 0x00000000;
662 dma
->sgc
= 0x00000000;
663 dma
->slp
= 0x7C000000;
664 dma
->pol
= 0x00000000;
667 static void ppc405_dma_init(CPUPPCState
*env
, qemu_irq irqs
[4])
671 dma
= g_malloc0(sizeof(ppc405_dma_t
));
672 memcpy(dma
->irqs
, irqs
, 4 * sizeof(qemu_irq
));
673 qemu_register_reset(&ppc405_dma_reset
, dma
);
674 ppc_dcr_register(env
, DMA0_CR0
,
675 dma
, &dcr_read_dma
, &dcr_write_dma
);
676 ppc_dcr_register(env
, DMA0_CT0
,
677 dma
, &dcr_read_dma
, &dcr_write_dma
);
678 ppc_dcr_register(env
, DMA0_DA0
,
679 dma
, &dcr_read_dma
, &dcr_write_dma
);
680 ppc_dcr_register(env
, DMA0_SA0
,
681 dma
, &dcr_read_dma
, &dcr_write_dma
);
682 ppc_dcr_register(env
, DMA0_SG0
,
683 dma
, &dcr_read_dma
, &dcr_write_dma
);
684 ppc_dcr_register(env
, DMA0_CR1
,
685 dma
, &dcr_read_dma
, &dcr_write_dma
);
686 ppc_dcr_register(env
, DMA0_CT1
,
687 dma
, &dcr_read_dma
, &dcr_write_dma
);
688 ppc_dcr_register(env
, DMA0_DA1
,
689 dma
, &dcr_read_dma
, &dcr_write_dma
);
690 ppc_dcr_register(env
, DMA0_SA1
,
691 dma
, &dcr_read_dma
, &dcr_write_dma
);
692 ppc_dcr_register(env
, DMA0_SG1
,
693 dma
, &dcr_read_dma
, &dcr_write_dma
);
694 ppc_dcr_register(env
, DMA0_CR2
,
695 dma
, &dcr_read_dma
, &dcr_write_dma
);
696 ppc_dcr_register(env
, DMA0_CT2
,
697 dma
, &dcr_read_dma
, &dcr_write_dma
);
698 ppc_dcr_register(env
, DMA0_DA2
,
699 dma
, &dcr_read_dma
, &dcr_write_dma
);
700 ppc_dcr_register(env
, DMA0_SA2
,
701 dma
, &dcr_read_dma
, &dcr_write_dma
);
702 ppc_dcr_register(env
, DMA0_SG2
,
703 dma
, &dcr_read_dma
, &dcr_write_dma
);
704 ppc_dcr_register(env
, DMA0_CR3
,
705 dma
, &dcr_read_dma
, &dcr_write_dma
);
706 ppc_dcr_register(env
, DMA0_CT3
,
707 dma
, &dcr_read_dma
, &dcr_write_dma
);
708 ppc_dcr_register(env
, DMA0_DA3
,
709 dma
, &dcr_read_dma
, &dcr_write_dma
);
710 ppc_dcr_register(env
, DMA0_SA3
,
711 dma
, &dcr_read_dma
, &dcr_write_dma
);
712 ppc_dcr_register(env
, DMA0_SG3
,
713 dma
, &dcr_read_dma
, &dcr_write_dma
);
714 ppc_dcr_register(env
, DMA0_SR
,
715 dma
, &dcr_read_dma
, &dcr_write_dma
);
716 ppc_dcr_register(env
, DMA0_SGC
,
717 dma
, &dcr_read_dma
, &dcr_write_dma
);
718 ppc_dcr_register(env
, DMA0_SLP
,
719 dma
, &dcr_read_dma
, &dcr_write_dma
);
720 ppc_dcr_register(env
, DMA0_POL
,
721 dma
, &dcr_read_dma
, &dcr_write_dma
);
724 /*****************************************************************************/
726 typedef struct ppc405_gpio_t ppc405_gpio_t
;
727 struct ppc405_gpio_t
{
742 static uint32_t ppc405_gpio_readb (void *opaque
, hwaddr addr
)
745 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
751 static void ppc405_gpio_writeb (void *opaque
,
752 hwaddr addr
, uint32_t value
)
755 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
760 static uint32_t ppc405_gpio_readw (void *opaque
, hwaddr addr
)
763 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
769 static void ppc405_gpio_writew (void *opaque
,
770 hwaddr addr
, uint32_t value
)
773 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
778 static uint32_t ppc405_gpio_readl (void *opaque
, hwaddr addr
)
781 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
787 static void ppc405_gpio_writel (void *opaque
,
788 hwaddr addr
, uint32_t value
)
791 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
796 static const MemoryRegionOps ppc405_gpio_ops
= {
798 .read
= { ppc405_gpio_readb
, ppc405_gpio_readw
, ppc405_gpio_readl
, },
799 .write
= { ppc405_gpio_writeb
, ppc405_gpio_writew
, ppc405_gpio_writel
, },
801 .endianness
= DEVICE_NATIVE_ENDIAN
,
804 static void ppc405_gpio_reset (void *opaque
)
808 static void ppc405_gpio_init(hwaddr base
)
812 gpio
= g_malloc0(sizeof(ppc405_gpio_t
));
814 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
816 memory_region_init_io(&gpio
->io
, NULL
, &ppc405_gpio_ops
, gpio
, "pgio", 0x038);
817 memory_region_add_subregion(get_system_memory(), base
, &gpio
->io
);
818 qemu_register_reset(&ppc405_gpio_reset
, gpio
);
821 /*****************************************************************************/
825 OCM0_ISACNTL
= 0x019,
827 OCM0_DSACNTL
= 0x01B,
830 typedef struct ppc405_ocm_t ppc405_ocm_t
;
831 struct ppc405_ocm_t
{
833 MemoryRegion isarc_ram
;
834 MemoryRegion dsarc_ram
;
841 static void ocm_update_mappings (ppc405_ocm_t
*ocm
,
842 uint32_t isarc
, uint32_t isacntl
,
843 uint32_t dsarc
, uint32_t dsacntl
)
846 printf("OCM update ISA %08" PRIx32
" %08" PRIx32
" (%08" PRIx32
847 " %08" PRIx32
") DSA %08" PRIx32
" %08" PRIx32
848 " (%08" PRIx32
" %08" PRIx32
")\n",
849 isarc
, isacntl
, dsarc
, dsacntl
,
850 ocm
->isarc
, ocm
->isacntl
, ocm
->dsarc
, ocm
->dsacntl
);
852 if (ocm
->isarc
!= isarc
||
853 (ocm
->isacntl
& 0x80000000) != (isacntl
& 0x80000000)) {
854 if (ocm
->isacntl
& 0x80000000) {
855 /* Unmap previously assigned memory region */
856 printf("OCM unmap ISA %08" PRIx32
"\n", ocm
->isarc
);
857 memory_region_del_subregion(get_system_memory(), &ocm
->isarc_ram
);
859 if (isacntl
& 0x80000000) {
860 /* Map new instruction memory region */
862 printf("OCM map ISA %08" PRIx32
"\n", isarc
);
864 memory_region_add_subregion(get_system_memory(), isarc
,
868 if (ocm
->dsarc
!= dsarc
||
869 (ocm
->dsacntl
& 0x80000000) != (dsacntl
& 0x80000000)) {
870 if (ocm
->dsacntl
& 0x80000000) {
871 /* Beware not to unmap the region we just mapped */
872 if (!(isacntl
& 0x80000000) || ocm
->dsarc
!= isarc
) {
873 /* Unmap previously assigned memory region */
875 printf("OCM unmap DSA %08" PRIx32
"\n", ocm
->dsarc
);
877 memory_region_del_subregion(get_system_memory(),
881 if (dsacntl
& 0x80000000) {
882 /* Beware not to remap the region we just mapped */
883 if (!(isacntl
& 0x80000000) || dsarc
!= isarc
) {
884 /* Map new data memory region */
886 printf("OCM map DSA %08" PRIx32
"\n", dsarc
);
888 memory_region_add_subregion(get_system_memory(), dsarc
,
895 static uint32_t dcr_read_ocm (void *opaque
, int dcrn
)
922 static void dcr_write_ocm (void *opaque
, int dcrn
, uint32_t val
)
925 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
930 isacntl
= ocm
->isacntl
;
931 dsacntl
= ocm
->dsacntl
;
934 isarc
= val
& 0xFC000000;
937 isacntl
= val
& 0xC0000000;
940 isarc
= val
& 0xFC000000;
943 isacntl
= val
& 0xC0000000;
946 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
949 ocm
->isacntl
= isacntl
;
950 ocm
->dsacntl
= dsacntl
;
953 static void ocm_reset (void *opaque
)
956 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
960 isacntl
= 0x00000000;
962 dsacntl
= 0x00000000;
963 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
966 ocm
->isacntl
= isacntl
;
967 ocm
->dsacntl
= dsacntl
;
970 static void ppc405_ocm_init(CPUPPCState
*env
)
974 ocm
= g_malloc0(sizeof(ppc405_ocm_t
));
975 /* XXX: Size is 4096 or 0x04000000 */
976 memory_region_init_ram(&ocm
->isarc_ram
, NULL
, "ppc405.ocm", 4096);
977 vmstate_register_ram_global(&ocm
->isarc_ram
);
978 memory_region_init_alias(&ocm
->dsarc_ram
, NULL
, "ppc405.dsarc", &ocm
->isarc_ram
,
980 qemu_register_reset(&ocm_reset
, ocm
);
981 ppc_dcr_register(env
, OCM0_ISARC
,
982 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
983 ppc_dcr_register(env
, OCM0_ISACNTL
,
984 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
985 ppc_dcr_register(env
, OCM0_DSARC
,
986 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
987 ppc_dcr_register(env
, OCM0_DSACNTL
,
988 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
991 /*****************************************************************************/
993 typedef struct ppc4xx_i2c_t ppc4xx_i2c_t
;
994 struct ppc4xx_i2c_t
{
1014 static uint32_t ppc4xx_i2c_readb (void *opaque
, hwaddr addr
)
1020 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1025 // i2c_readbyte(&i2c->mdata);
1065 ret
= i2c
->xtcntlss
;
1068 ret
= i2c
->directcntl
;
1075 printf("%s: addr " TARGET_FMT_plx
" %02" PRIx32
"\n", __func__
, addr
, ret
);
1081 static void ppc4xx_i2c_writeb (void *opaque
,
1082 hwaddr addr
, uint32_t value
)
1087 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1094 // i2c_sendbyte(&i2c->mdata);
1109 i2c
->mdcntl
= value
& 0xDF;
1112 i2c
->sts
&= ~(value
& 0x0A);
1115 i2c
->extsts
&= ~(value
& 0x8F);
1124 i2c
->clkdiv
= value
;
1127 i2c
->intrmsk
= value
;
1130 i2c
->xfrcnt
= value
& 0x77;
1133 i2c
->xtcntlss
= value
;
1136 i2c
->directcntl
= value
& 0x7;
1141 static uint32_t ppc4xx_i2c_readw (void *opaque
, hwaddr addr
)
1146 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1148 ret
= ppc4xx_i2c_readb(opaque
, addr
) << 8;
1149 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 1);
1154 static void ppc4xx_i2c_writew (void *opaque
,
1155 hwaddr addr
, uint32_t value
)
1158 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1161 ppc4xx_i2c_writeb(opaque
, addr
, value
>> 8);
1162 ppc4xx_i2c_writeb(opaque
, addr
+ 1, value
);
1165 static uint32_t ppc4xx_i2c_readl (void *opaque
, hwaddr addr
)
1170 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1172 ret
= ppc4xx_i2c_readb(opaque
, addr
) << 24;
1173 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 1) << 16;
1174 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 2) << 8;
1175 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 3);
1180 static void ppc4xx_i2c_writel (void *opaque
,
1181 hwaddr addr
, uint32_t value
)
1184 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1187 ppc4xx_i2c_writeb(opaque
, addr
, value
>> 24);
1188 ppc4xx_i2c_writeb(opaque
, addr
+ 1, value
>> 16);
1189 ppc4xx_i2c_writeb(opaque
, addr
+ 2, value
>> 8);
1190 ppc4xx_i2c_writeb(opaque
, addr
+ 3, value
);
1193 static const MemoryRegionOps i2c_ops
= {
1195 .read
= { ppc4xx_i2c_readb
, ppc4xx_i2c_readw
, ppc4xx_i2c_readl
, },
1196 .write
= { ppc4xx_i2c_writeb
, ppc4xx_i2c_writew
, ppc4xx_i2c_writel
, },
1198 .endianness
= DEVICE_NATIVE_ENDIAN
,
1201 static void ppc4xx_i2c_reset (void *opaque
)
1214 i2c
->directcntl
= 0x0F;
1217 static void ppc405_i2c_init(hwaddr base
, qemu_irq irq
)
1221 i2c
= g_malloc0(sizeof(ppc4xx_i2c_t
));
1224 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1226 memory_region_init_io(&i2c
->iomem
, NULL
, &i2c_ops
, i2c
, "i2c", 0x011);
1227 memory_region_add_subregion(get_system_memory(), base
, &i2c
->iomem
);
1228 qemu_register_reset(ppc4xx_i2c_reset
, i2c
);
1231 /*****************************************************************************/
1232 /* General purpose timers */
1233 typedef struct ppc4xx_gpt_t ppc4xx_gpt_t
;
1234 struct ppc4xx_gpt_t
{
1249 static uint32_t ppc4xx_gpt_readb (void *opaque
, hwaddr addr
)
1252 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1254 /* XXX: generate a bus fault */
1258 static void ppc4xx_gpt_writeb (void *opaque
,
1259 hwaddr addr
, uint32_t value
)
1262 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1265 /* XXX: generate a bus fault */
1268 static uint32_t ppc4xx_gpt_readw (void *opaque
, hwaddr addr
)
1271 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1273 /* XXX: generate a bus fault */
1277 static void ppc4xx_gpt_writew (void *opaque
,
1278 hwaddr addr
, uint32_t value
)
1281 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1284 /* XXX: generate a bus fault */
1287 static int ppc4xx_gpt_compare (ppc4xx_gpt_t
*gpt
, int n
)
1293 static void ppc4xx_gpt_set_output (ppc4xx_gpt_t
*gpt
, int n
, int level
)
1298 static void ppc4xx_gpt_set_outputs (ppc4xx_gpt_t
*gpt
)
1304 for (i
= 0; i
< 5; i
++) {
1305 if (gpt
->oe
& mask
) {
1306 /* Output is enabled */
1307 if (ppc4xx_gpt_compare(gpt
, i
)) {
1308 /* Comparison is OK */
1309 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
);
1311 /* Comparison is KO */
1312 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
? 0 : 1);
1319 static void ppc4xx_gpt_set_irqs (ppc4xx_gpt_t
*gpt
)
1325 for (i
= 0; i
< 5; i
++) {
1326 if (gpt
->is
& gpt
->im
& mask
)
1327 qemu_irq_raise(gpt
->irqs
[i
]);
1329 qemu_irq_lower(gpt
->irqs
[i
]);
1334 static void ppc4xx_gpt_compute_timer (ppc4xx_gpt_t
*gpt
)
1339 static uint32_t ppc4xx_gpt_readl (void *opaque
, hwaddr addr
)
1346 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1351 /* Time base counter */
1352 ret
= muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + gpt
->tb_offset
,
1353 gpt
->tb_freq
, get_ticks_per_sec());
1364 /* Interrupt mask */
1369 /* Interrupt status */
1373 /* Interrupt enable */
1378 idx
= (addr
- 0x80) >> 2;
1379 ret
= gpt
->comp
[idx
];
1383 idx
= (addr
- 0xC0) >> 2;
1384 ret
= gpt
->mask
[idx
];
1394 static void ppc4xx_gpt_writel (void *opaque
,
1395 hwaddr addr
, uint32_t value
)
1401 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1407 /* Time base counter */
1408 gpt
->tb_offset
= muldiv64(value
, get_ticks_per_sec(), gpt
->tb_freq
)
1409 - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1410 ppc4xx_gpt_compute_timer(gpt
);
1414 gpt
->oe
= value
& 0xF8000000;
1415 ppc4xx_gpt_set_outputs(gpt
);
1419 gpt
->ol
= value
& 0xF8000000;
1420 ppc4xx_gpt_set_outputs(gpt
);
1423 /* Interrupt mask */
1424 gpt
->im
= value
& 0x0000F800;
1427 /* Interrupt status set */
1428 gpt
->is
|= value
& 0x0000F800;
1429 ppc4xx_gpt_set_irqs(gpt
);
1432 /* Interrupt status clear */
1433 gpt
->is
&= ~(value
& 0x0000F800);
1434 ppc4xx_gpt_set_irqs(gpt
);
1437 /* Interrupt enable */
1438 gpt
->ie
= value
& 0x0000F800;
1439 ppc4xx_gpt_set_irqs(gpt
);
1443 idx
= (addr
- 0x80) >> 2;
1444 gpt
->comp
[idx
] = value
& 0xF8000000;
1445 ppc4xx_gpt_compute_timer(gpt
);
1449 idx
= (addr
- 0xC0) >> 2;
1450 gpt
->mask
[idx
] = value
& 0xF8000000;
1451 ppc4xx_gpt_compute_timer(gpt
);
1456 static const MemoryRegionOps gpt_ops
= {
1458 .read
= { ppc4xx_gpt_readb
, ppc4xx_gpt_readw
, ppc4xx_gpt_readl
, },
1459 .write
= { ppc4xx_gpt_writeb
, ppc4xx_gpt_writew
, ppc4xx_gpt_writel
, },
1461 .endianness
= DEVICE_NATIVE_ENDIAN
,
1464 static void ppc4xx_gpt_cb (void *opaque
)
1469 ppc4xx_gpt_set_irqs(gpt
);
1470 ppc4xx_gpt_set_outputs(gpt
);
1471 ppc4xx_gpt_compute_timer(gpt
);
1474 static void ppc4xx_gpt_reset (void *opaque
)
1480 timer_del(gpt
->timer
);
1481 gpt
->oe
= 0x00000000;
1482 gpt
->ol
= 0x00000000;
1483 gpt
->im
= 0x00000000;
1484 gpt
->is
= 0x00000000;
1485 gpt
->ie
= 0x00000000;
1486 for (i
= 0; i
< 5; i
++) {
1487 gpt
->comp
[i
] = 0x00000000;
1488 gpt
->mask
[i
] = 0x00000000;
1492 static void ppc4xx_gpt_init(hwaddr base
, qemu_irq irqs
[5])
1497 gpt
= g_malloc0(sizeof(ppc4xx_gpt_t
));
1498 for (i
= 0; i
< 5; i
++) {
1499 gpt
->irqs
[i
] = irqs
[i
];
1501 gpt
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &ppc4xx_gpt_cb
, gpt
);
1503 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1505 memory_region_init_io(&gpt
->iomem
, NULL
, &gpt_ops
, gpt
, "gpt", 0x0d4);
1506 memory_region_add_subregion(get_system_memory(), base
, &gpt
->iomem
);
1507 qemu_register_reset(ppc4xx_gpt_reset
, gpt
);
1510 /*****************************************************************************/
1516 MAL0_TXCASR
= 0x184,
1517 MAL0_TXCARR
= 0x185,
1518 MAL0_TXEOBISR
= 0x186,
1519 MAL0_TXDEIR
= 0x187,
1520 MAL0_RXCASR
= 0x190,
1521 MAL0_RXCARR
= 0x191,
1522 MAL0_RXEOBISR
= 0x192,
1523 MAL0_RXDEIR
= 0x193,
1524 MAL0_TXCTP0R
= 0x1A0,
1525 MAL0_TXCTP1R
= 0x1A1,
1526 MAL0_TXCTP2R
= 0x1A2,
1527 MAL0_TXCTP3R
= 0x1A3,
1528 MAL0_RXCTP0R
= 0x1C0,
1529 MAL0_RXCTP1R
= 0x1C1,
1534 typedef struct ppc40x_mal_t ppc40x_mal_t
;
1535 struct ppc40x_mal_t
{
1553 static void ppc40x_mal_reset (void *opaque
);
1555 static uint32_t dcr_read_mal (void *opaque
, int dcrn
)
1578 ret
= mal
->txeobisr
;
1590 ret
= mal
->rxeobisr
;
1596 ret
= mal
->txctpr
[0];
1599 ret
= mal
->txctpr
[1];
1602 ret
= mal
->txctpr
[2];
1605 ret
= mal
->txctpr
[3];
1608 ret
= mal
->rxctpr
[0];
1611 ret
= mal
->rxctpr
[1];
1627 static void dcr_write_mal (void *opaque
, int dcrn
, uint32_t val
)
1635 if (val
& 0x80000000)
1636 ppc40x_mal_reset(mal
);
1637 mal
->cfg
= val
& 0x00FFC087;
1644 mal
->ier
= val
& 0x0000001F;
1647 mal
->txcasr
= val
& 0xF0000000;
1650 mal
->txcarr
= val
& 0xF0000000;
1654 mal
->txeobisr
&= ~val
;
1658 mal
->txdeir
&= ~val
;
1661 mal
->rxcasr
= val
& 0xC0000000;
1664 mal
->rxcarr
= val
& 0xC0000000;
1668 mal
->rxeobisr
&= ~val
;
1672 mal
->rxdeir
&= ~val
;
1686 mal
->txctpr
[idx
] = val
;
1694 mal
->rxctpr
[idx
] = val
;
1698 goto update_rx_size
;
1702 mal
->rcbs
[idx
] = val
& 0x000000FF;
1707 static void ppc40x_mal_reset (void *opaque
)
1712 mal
->cfg
= 0x0007C000;
1713 mal
->esr
= 0x00000000;
1714 mal
->ier
= 0x00000000;
1715 mal
->rxcasr
= 0x00000000;
1716 mal
->rxdeir
= 0x00000000;
1717 mal
->rxeobisr
= 0x00000000;
1718 mal
->txcasr
= 0x00000000;
1719 mal
->txdeir
= 0x00000000;
1720 mal
->txeobisr
= 0x00000000;
1723 static void ppc405_mal_init(CPUPPCState
*env
, qemu_irq irqs
[4])
1728 mal
= g_malloc0(sizeof(ppc40x_mal_t
));
1729 for (i
= 0; i
< 4; i
++)
1730 mal
->irqs
[i
] = irqs
[i
];
1731 qemu_register_reset(&ppc40x_mal_reset
, mal
);
1732 ppc_dcr_register(env
, MAL0_CFG
,
1733 mal
, &dcr_read_mal
, &dcr_write_mal
);
1734 ppc_dcr_register(env
, MAL0_ESR
,
1735 mal
, &dcr_read_mal
, &dcr_write_mal
);
1736 ppc_dcr_register(env
, MAL0_IER
,
1737 mal
, &dcr_read_mal
, &dcr_write_mal
);
1738 ppc_dcr_register(env
, MAL0_TXCASR
,
1739 mal
, &dcr_read_mal
, &dcr_write_mal
);
1740 ppc_dcr_register(env
, MAL0_TXCARR
,
1741 mal
, &dcr_read_mal
, &dcr_write_mal
);
1742 ppc_dcr_register(env
, MAL0_TXEOBISR
,
1743 mal
, &dcr_read_mal
, &dcr_write_mal
);
1744 ppc_dcr_register(env
, MAL0_TXDEIR
,
1745 mal
, &dcr_read_mal
, &dcr_write_mal
);
1746 ppc_dcr_register(env
, MAL0_RXCASR
,
1747 mal
, &dcr_read_mal
, &dcr_write_mal
);
1748 ppc_dcr_register(env
, MAL0_RXCARR
,
1749 mal
, &dcr_read_mal
, &dcr_write_mal
);
1750 ppc_dcr_register(env
, MAL0_RXEOBISR
,
1751 mal
, &dcr_read_mal
, &dcr_write_mal
);
1752 ppc_dcr_register(env
, MAL0_RXDEIR
,
1753 mal
, &dcr_read_mal
, &dcr_write_mal
);
1754 ppc_dcr_register(env
, MAL0_TXCTP0R
,
1755 mal
, &dcr_read_mal
, &dcr_write_mal
);
1756 ppc_dcr_register(env
, MAL0_TXCTP1R
,
1757 mal
, &dcr_read_mal
, &dcr_write_mal
);
1758 ppc_dcr_register(env
, MAL0_TXCTP2R
,
1759 mal
, &dcr_read_mal
, &dcr_write_mal
);
1760 ppc_dcr_register(env
, MAL0_TXCTP3R
,
1761 mal
, &dcr_read_mal
, &dcr_write_mal
);
1762 ppc_dcr_register(env
, MAL0_RXCTP0R
,
1763 mal
, &dcr_read_mal
, &dcr_write_mal
);
1764 ppc_dcr_register(env
, MAL0_RXCTP1R
,
1765 mal
, &dcr_read_mal
, &dcr_write_mal
);
1766 ppc_dcr_register(env
, MAL0_RCBS0
,
1767 mal
, &dcr_read_mal
, &dcr_write_mal
);
1768 ppc_dcr_register(env
, MAL0_RCBS1
,
1769 mal
, &dcr_read_mal
, &dcr_write_mal
);
1772 /*****************************************************************************/
1774 void ppc40x_core_reset(PowerPCCPU
*cpu
)
1776 CPUPPCState
*env
= &cpu
->env
;
1779 printf("Reset PowerPC core\n");
1780 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_RESET
);
1781 dbsr
= env
->spr
[SPR_40x_DBSR
];
1782 dbsr
&= ~0x00000300;
1784 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1787 void ppc40x_chip_reset(PowerPCCPU
*cpu
)
1789 CPUPPCState
*env
= &cpu
->env
;
1792 printf("Reset PowerPC chip\n");
1793 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_RESET
);
1794 /* XXX: TODO reset all internal peripherals */
1795 dbsr
= env
->spr
[SPR_40x_DBSR
];
1796 dbsr
&= ~0x00000300;
1798 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1801 void ppc40x_system_reset(PowerPCCPU
*cpu
)
1803 printf("Reset PowerPC system\n");
1804 qemu_system_reset_request();
1807 void store_40x_dbcr0 (CPUPPCState
*env
, uint32_t val
)
1809 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1811 switch ((val
>> 28) & 0x3) {
1817 ppc40x_core_reset(cpu
);
1821 ppc40x_chip_reset(cpu
);
1825 ppc40x_system_reset(cpu
);
1830 /*****************************************************************************/
1833 PPC405CR_CPC0_PLLMR
= 0x0B0,
1834 PPC405CR_CPC0_CR0
= 0x0B1,
1835 PPC405CR_CPC0_CR1
= 0x0B2,
1836 PPC405CR_CPC0_PSR
= 0x0B4,
1837 PPC405CR_CPC0_JTAGID
= 0x0B5,
1838 PPC405CR_CPC0_ER
= 0x0B9,
1839 PPC405CR_CPC0_FR
= 0x0BA,
1840 PPC405CR_CPC0_SR
= 0x0BB,
1844 PPC405CR_CPU_CLK
= 0,
1845 PPC405CR_TMR_CLK
= 1,
1846 PPC405CR_PLB_CLK
= 2,
1847 PPC405CR_SDRAM_CLK
= 3,
1848 PPC405CR_OPB_CLK
= 4,
1849 PPC405CR_EXT_CLK
= 5,
1850 PPC405CR_UART_CLK
= 6,
1851 PPC405CR_CLK_NB
= 7,
1854 typedef struct ppc405cr_cpc_t ppc405cr_cpc_t
;
1855 struct ppc405cr_cpc_t
{
1856 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
1867 static void ppc405cr_clk_setup (ppc405cr_cpc_t
*cpc
)
1869 uint64_t VCO_out
, PLL_out
;
1870 uint32_t CPU_clk
, TMR_clk
, SDRAM_clk
, PLB_clk
, OPB_clk
, EXT_clk
, UART_clk
;
1873 D0
= ((cpc
->pllmr
>> 26) & 0x3) + 1; /* CBDV */
1874 if (cpc
->pllmr
& 0x80000000) {
1875 D1
= (((cpc
->pllmr
>> 20) - 1) & 0xF) + 1; /* FBDV */
1876 D2
= 8 - ((cpc
->pllmr
>> 16) & 0x7); /* FWDVA */
1878 VCO_out
= cpc
->sysclk
* M
;
1879 if (VCO_out
< 400000000 || VCO_out
> 800000000) {
1880 /* PLL cannot lock */
1881 cpc
->pllmr
&= ~0x80000000;
1884 PLL_out
= VCO_out
/ D2
;
1889 PLL_out
= cpc
->sysclk
* M
;
1892 if (cpc
->cr1
& 0x00800000)
1893 TMR_clk
= cpc
->sysclk
; /* Should have a separate clock */
1896 PLB_clk
= CPU_clk
/ D0
;
1897 SDRAM_clk
= PLB_clk
;
1898 D0
= ((cpc
->pllmr
>> 10) & 0x3) + 1;
1899 OPB_clk
= PLB_clk
/ D0
;
1900 D0
= ((cpc
->pllmr
>> 24) & 0x3) + 2;
1901 EXT_clk
= PLB_clk
/ D0
;
1902 D0
= ((cpc
->cr0
>> 1) & 0x1F) + 1;
1903 UART_clk
= CPU_clk
/ D0
;
1904 /* Setup CPU clocks */
1905 clk_setup(&cpc
->clk_setup
[PPC405CR_CPU_CLK
], CPU_clk
);
1906 /* Setup time-base clock */
1907 clk_setup(&cpc
->clk_setup
[PPC405CR_TMR_CLK
], TMR_clk
);
1908 /* Setup PLB clock */
1909 clk_setup(&cpc
->clk_setup
[PPC405CR_PLB_CLK
], PLB_clk
);
1910 /* Setup SDRAM clock */
1911 clk_setup(&cpc
->clk_setup
[PPC405CR_SDRAM_CLK
], SDRAM_clk
);
1912 /* Setup OPB clock */
1913 clk_setup(&cpc
->clk_setup
[PPC405CR_OPB_CLK
], OPB_clk
);
1914 /* Setup external clock */
1915 clk_setup(&cpc
->clk_setup
[PPC405CR_EXT_CLK
], EXT_clk
);
1916 /* Setup UART clock */
1917 clk_setup(&cpc
->clk_setup
[PPC405CR_UART_CLK
], UART_clk
);
1920 static uint32_t dcr_read_crcpc (void *opaque
, int dcrn
)
1922 ppc405cr_cpc_t
*cpc
;
1927 case PPC405CR_CPC0_PLLMR
:
1930 case PPC405CR_CPC0_CR0
:
1933 case PPC405CR_CPC0_CR1
:
1936 case PPC405CR_CPC0_PSR
:
1939 case PPC405CR_CPC0_JTAGID
:
1942 case PPC405CR_CPC0_ER
:
1945 case PPC405CR_CPC0_FR
:
1948 case PPC405CR_CPC0_SR
:
1949 ret
= ~(cpc
->er
| cpc
->fr
) & 0xFFFF0000;
1952 /* Avoid gcc warning */
1960 static void dcr_write_crcpc (void *opaque
, int dcrn
, uint32_t val
)
1962 ppc405cr_cpc_t
*cpc
;
1966 case PPC405CR_CPC0_PLLMR
:
1967 cpc
->pllmr
= val
& 0xFFF77C3F;
1969 case PPC405CR_CPC0_CR0
:
1970 cpc
->cr0
= val
& 0x0FFFFFFE;
1972 case PPC405CR_CPC0_CR1
:
1973 cpc
->cr1
= val
& 0x00800000;
1975 case PPC405CR_CPC0_PSR
:
1978 case PPC405CR_CPC0_JTAGID
:
1981 case PPC405CR_CPC0_ER
:
1982 cpc
->er
= val
& 0xBFFC0000;
1984 case PPC405CR_CPC0_FR
:
1985 cpc
->fr
= val
& 0xBFFC0000;
1987 case PPC405CR_CPC0_SR
:
1993 static void ppc405cr_cpc_reset (void *opaque
)
1995 ppc405cr_cpc_t
*cpc
;
1999 /* Compute PLLMR value from PSR settings */
2000 cpc
->pllmr
= 0x80000000;
2002 switch ((cpc
->psr
>> 30) & 3) {
2005 cpc
->pllmr
&= ~0x80000000;
2009 cpc
->pllmr
|= 5 << 16;
2013 cpc
->pllmr
|= 4 << 16;
2017 cpc
->pllmr
|= 2 << 16;
2021 D
= (cpc
->psr
>> 28) & 3;
2022 cpc
->pllmr
|= (D
+ 1) << 20;
2024 D
= (cpc
->psr
>> 25) & 7;
2039 D
= (cpc
->psr
>> 23) & 3;
2040 cpc
->pllmr
|= D
<< 26;
2042 D
= (cpc
->psr
>> 21) & 3;
2043 cpc
->pllmr
|= D
<< 10;
2045 D
= (cpc
->psr
>> 17) & 3;
2046 cpc
->pllmr
|= D
<< 24;
2047 cpc
->cr0
= 0x0000003C;
2048 cpc
->cr1
= 0x2B0D8800;
2049 cpc
->er
= 0x00000000;
2050 cpc
->fr
= 0x00000000;
2051 ppc405cr_clk_setup(cpc
);
2054 static void ppc405cr_clk_init (ppc405cr_cpc_t
*cpc
)
2058 /* XXX: this should be read from IO pins */
2059 cpc
->psr
= 0x00000000; /* 8 bits ROM */
2061 D
= 0x2; /* Divide by 4 */
2062 cpc
->psr
|= D
<< 30;
2064 D
= 0x1; /* Divide by 2 */
2065 cpc
->psr
|= D
<< 28;
2067 D
= 0x1; /* Divide by 2 */
2068 cpc
->psr
|= D
<< 23;
2070 D
= 0x5; /* M = 16 */
2071 cpc
->psr
|= D
<< 25;
2073 D
= 0x1; /* Divide by 2 */
2074 cpc
->psr
|= D
<< 21;
2076 D
= 0x2; /* Divide by 4 */
2077 cpc
->psr
|= D
<< 17;
2080 static void ppc405cr_cpc_init (CPUPPCState
*env
, clk_setup_t clk_setup
[7],
2083 ppc405cr_cpc_t
*cpc
;
2085 cpc
= g_malloc0(sizeof(ppc405cr_cpc_t
));
2086 memcpy(cpc
->clk_setup
, clk_setup
,
2087 PPC405CR_CLK_NB
* sizeof(clk_setup_t
));
2088 cpc
->sysclk
= sysclk
;
2089 cpc
->jtagid
= 0x42051049;
2090 ppc_dcr_register(env
, PPC405CR_CPC0_PSR
, cpc
,
2091 &dcr_read_crcpc
, &dcr_write_crcpc
);
2092 ppc_dcr_register(env
, PPC405CR_CPC0_CR0
, cpc
,
2093 &dcr_read_crcpc
, &dcr_write_crcpc
);
2094 ppc_dcr_register(env
, PPC405CR_CPC0_CR1
, cpc
,
2095 &dcr_read_crcpc
, &dcr_write_crcpc
);
2096 ppc_dcr_register(env
, PPC405CR_CPC0_JTAGID
, cpc
,
2097 &dcr_read_crcpc
, &dcr_write_crcpc
);
2098 ppc_dcr_register(env
, PPC405CR_CPC0_PLLMR
, cpc
,
2099 &dcr_read_crcpc
, &dcr_write_crcpc
);
2100 ppc_dcr_register(env
, PPC405CR_CPC0_ER
, cpc
,
2101 &dcr_read_crcpc
, &dcr_write_crcpc
);
2102 ppc_dcr_register(env
, PPC405CR_CPC0_FR
, cpc
,
2103 &dcr_read_crcpc
, &dcr_write_crcpc
);
2104 ppc_dcr_register(env
, PPC405CR_CPC0_SR
, cpc
,
2105 &dcr_read_crcpc
, &dcr_write_crcpc
);
2106 ppc405cr_clk_init(cpc
);
2107 qemu_register_reset(ppc405cr_cpc_reset
, cpc
);
2110 CPUPPCState
*ppc405cr_init(MemoryRegion
*address_space_mem
,
2111 MemoryRegion ram_memories
[4],
2112 hwaddr ram_bases
[4],
2113 hwaddr ram_sizes
[4],
2114 uint32_t sysclk
, qemu_irq
**picp
,
2117 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
2118 qemu_irq dma_irqs
[4];
2121 qemu_irq
*pic
, *irqs
;
2123 memset(clk_setup
, 0, sizeof(clk_setup
));
2124 cpu
= ppc4xx_init("405cr", &clk_setup
[PPC405CR_CPU_CLK
],
2125 &clk_setup
[PPC405CR_TMR_CLK
], sysclk
);
2127 /* Memory mapped devices registers */
2129 ppc4xx_plb_init(env
);
2130 /* PLB to OPB bridge */
2131 ppc4xx_pob_init(env
);
2133 ppc4xx_opba_init(0xef600600);
2134 /* Universal interrupt controller */
2135 irqs
= g_malloc0(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2136 irqs
[PPCUIC_OUTPUT_INT
] =
2137 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2138 irqs
[PPCUIC_OUTPUT_CINT
] =
2139 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2140 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2142 /* SDRAM controller */
2143 ppc4xx_sdram_init(env
, pic
[14], 1, ram_memories
,
2144 ram_bases
, ram_sizes
, do_init
);
2145 /* External bus controller */
2146 ppc405_ebc_init(env
);
2147 /* DMA controller */
2148 dma_irqs
[0] = pic
[26];
2149 dma_irqs
[1] = pic
[25];
2150 dma_irqs
[2] = pic
[24];
2151 dma_irqs
[3] = pic
[23];
2152 ppc405_dma_init(env
, dma_irqs
);
2154 if (serial_hds
[0] != NULL
) {
2155 serial_mm_init(address_space_mem
, 0xef600300, 0, pic
[0],
2156 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[0],
2159 if (serial_hds
[1] != NULL
) {
2160 serial_mm_init(address_space_mem
, 0xef600400, 0, pic
[1],
2161 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[1],
2164 /* IIC controller */
2165 ppc405_i2c_init(0xef600500, pic
[2]);
2167 ppc405_gpio_init(0xef600700);
2169 ppc405cr_cpc_init(env
, clk_setup
, sysclk
);
2174 /*****************************************************************************/
2178 PPC405EP_CPC0_PLLMR0
= 0x0F0,
2179 PPC405EP_CPC0_BOOT
= 0x0F1,
2180 PPC405EP_CPC0_EPCTL
= 0x0F3,
2181 PPC405EP_CPC0_PLLMR1
= 0x0F4,
2182 PPC405EP_CPC0_UCR
= 0x0F5,
2183 PPC405EP_CPC0_SRR
= 0x0F6,
2184 PPC405EP_CPC0_JTAGID
= 0x0F7,
2185 PPC405EP_CPC0_PCI
= 0x0F9,
2187 PPC405EP_CPC0_ER
= xxx
,
2188 PPC405EP_CPC0_FR
= xxx
,
2189 PPC405EP_CPC0_SR
= xxx
,
2194 PPC405EP_CPU_CLK
= 0,
2195 PPC405EP_PLB_CLK
= 1,
2196 PPC405EP_OPB_CLK
= 2,
2197 PPC405EP_EBC_CLK
= 3,
2198 PPC405EP_MAL_CLK
= 4,
2199 PPC405EP_PCI_CLK
= 5,
2200 PPC405EP_UART0_CLK
= 6,
2201 PPC405EP_UART1_CLK
= 7,
2202 PPC405EP_CLK_NB
= 8,
2205 typedef struct ppc405ep_cpc_t ppc405ep_cpc_t
;
2206 struct ppc405ep_cpc_t
{
2208 clk_setup_t clk_setup
[PPC405EP_CLK_NB
];
2216 /* Clock and power management */
2222 static void ppc405ep_compute_clocks (ppc405ep_cpc_t
*cpc
)
2224 uint32_t CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
;
2225 uint32_t UART0_clk
, UART1_clk
;
2226 uint64_t VCO_out
, PLL_out
;
2230 if ((cpc
->pllmr
[1] & 0x80000000) && !(cpc
->pllmr
[1] & 0x40000000)) {
2231 M
= (((cpc
->pllmr
[1] >> 20) - 1) & 0xF) + 1; /* FBMUL */
2232 #ifdef DEBUG_CLOCKS_LL
2233 printf("FBMUL %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 20) & 0xF, M
);
2235 D
= 8 - ((cpc
->pllmr
[1] >> 16) & 0x7); /* FWDA */
2236 #ifdef DEBUG_CLOCKS_LL
2237 printf("FWDA %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 16) & 0x7, D
);
2239 VCO_out
= cpc
->sysclk
* M
* D
;
2240 if (VCO_out
< 500000000UL || VCO_out
> 1000000000UL) {
2241 /* Error - unlock the PLL */
2242 printf("VCO out of range %" PRIu64
"\n", VCO_out
);
2244 cpc
->pllmr
[1] &= ~0x80000000;
2248 PLL_out
= VCO_out
/ D
;
2249 /* Pretend the PLL is locked */
2250 cpc
->boot
|= 0x00000001;
2255 PLL_out
= cpc
->sysclk
;
2256 if (cpc
->pllmr
[1] & 0x40000000) {
2257 /* Pretend the PLL is not locked */
2258 cpc
->boot
&= ~0x00000001;
2261 /* Now, compute all other clocks */
2262 D
= ((cpc
->pllmr
[0] >> 20) & 0x3) + 1; /* CCDV */
2263 #ifdef DEBUG_CLOCKS_LL
2264 printf("CCDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 20) & 0x3, D
);
2266 CPU_clk
= PLL_out
/ D
;
2267 D
= ((cpc
->pllmr
[0] >> 16) & 0x3) + 1; /* CBDV */
2268 #ifdef DEBUG_CLOCKS_LL
2269 printf("CBDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 16) & 0x3, D
);
2271 PLB_clk
= CPU_clk
/ D
;
2272 D
= ((cpc
->pllmr
[0] >> 12) & 0x3) + 1; /* OPDV */
2273 #ifdef DEBUG_CLOCKS_LL
2274 printf("OPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 12) & 0x3, D
);
2276 OPB_clk
= PLB_clk
/ D
;
2277 D
= ((cpc
->pllmr
[0] >> 8) & 0x3) + 2; /* EPDV */
2278 #ifdef DEBUG_CLOCKS_LL
2279 printf("EPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 8) & 0x3, D
);
2281 EBC_clk
= PLB_clk
/ D
;
2282 D
= ((cpc
->pllmr
[0] >> 4) & 0x3) + 1; /* MPDV */
2283 #ifdef DEBUG_CLOCKS_LL
2284 printf("MPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 4) & 0x3, D
);
2286 MAL_clk
= PLB_clk
/ D
;
2287 D
= (cpc
->pllmr
[0] & 0x3) + 1; /* PPDV */
2288 #ifdef DEBUG_CLOCKS_LL
2289 printf("PPDV %01" PRIx32
" %d\n", cpc
->pllmr
[0] & 0x3, D
);
2291 PCI_clk
= PLB_clk
/ D
;
2292 D
= ((cpc
->ucr
- 1) & 0x7F) + 1; /* U0DIV */
2293 #ifdef DEBUG_CLOCKS_LL
2294 printf("U0DIV %01" PRIx32
" %d\n", cpc
->ucr
& 0x7F, D
);
2296 UART0_clk
= PLL_out
/ D
;
2297 D
= (((cpc
->ucr
>> 8) - 1) & 0x7F) + 1; /* U1DIV */
2298 #ifdef DEBUG_CLOCKS_LL
2299 printf("U1DIV %01" PRIx32
" %d\n", (cpc
->ucr
>> 8) & 0x7F, D
);
2301 UART1_clk
= PLL_out
/ D
;
2303 printf("Setup PPC405EP clocks - sysclk %" PRIu32
" VCO %" PRIu64
2304 " PLL out %" PRIu64
" Hz\n", cpc
->sysclk
, VCO_out
, PLL_out
);
2305 printf("CPU %" PRIu32
" PLB %" PRIu32
" OPB %" PRIu32
" EBC %" PRIu32
2306 " MAL %" PRIu32
" PCI %" PRIu32
" UART0 %" PRIu32
2307 " UART1 %" PRIu32
"\n",
2308 CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
,
2309 UART0_clk
, UART1_clk
);
2311 /* Setup CPU clocks */
2312 clk_setup(&cpc
->clk_setup
[PPC405EP_CPU_CLK
], CPU_clk
);
2313 /* Setup PLB clock */
2314 clk_setup(&cpc
->clk_setup
[PPC405EP_PLB_CLK
], PLB_clk
);
2315 /* Setup OPB clock */
2316 clk_setup(&cpc
->clk_setup
[PPC405EP_OPB_CLK
], OPB_clk
);
2317 /* Setup external clock */
2318 clk_setup(&cpc
->clk_setup
[PPC405EP_EBC_CLK
], EBC_clk
);
2319 /* Setup MAL clock */
2320 clk_setup(&cpc
->clk_setup
[PPC405EP_MAL_CLK
], MAL_clk
);
2321 /* Setup PCI clock */
2322 clk_setup(&cpc
->clk_setup
[PPC405EP_PCI_CLK
], PCI_clk
);
2323 /* Setup UART0 clock */
2324 clk_setup(&cpc
->clk_setup
[PPC405EP_UART0_CLK
], UART0_clk
);
2325 /* Setup UART1 clock */
2326 clk_setup(&cpc
->clk_setup
[PPC405EP_UART1_CLK
], UART1_clk
);
2329 static uint32_t dcr_read_epcpc (void *opaque
, int dcrn
)
2331 ppc405ep_cpc_t
*cpc
;
2336 case PPC405EP_CPC0_BOOT
:
2339 case PPC405EP_CPC0_EPCTL
:
2342 case PPC405EP_CPC0_PLLMR0
:
2343 ret
= cpc
->pllmr
[0];
2345 case PPC405EP_CPC0_PLLMR1
:
2346 ret
= cpc
->pllmr
[1];
2348 case PPC405EP_CPC0_UCR
:
2351 case PPC405EP_CPC0_SRR
:
2354 case PPC405EP_CPC0_JTAGID
:
2357 case PPC405EP_CPC0_PCI
:
2361 /* Avoid gcc warning */
2369 static void dcr_write_epcpc (void *opaque
, int dcrn
, uint32_t val
)
2371 ppc405ep_cpc_t
*cpc
;
2375 case PPC405EP_CPC0_BOOT
:
2376 /* Read-only register */
2378 case PPC405EP_CPC0_EPCTL
:
2379 /* Don't care for now */
2380 cpc
->epctl
= val
& 0xC00000F3;
2382 case PPC405EP_CPC0_PLLMR0
:
2383 cpc
->pllmr
[0] = val
& 0x00633333;
2384 ppc405ep_compute_clocks(cpc
);
2386 case PPC405EP_CPC0_PLLMR1
:
2387 cpc
->pllmr
[1] = val
& 0xC0F73FFF;
2388 ppc405ep_compute_clocks(cpc
);
2390 case PPC405EP_CPC0_UCR
:
2391 /* UART control - don't care for now */
2392 cpc
->ucr
= val
& 0x003F7F7F;
2394 case PPC405EP_CPC0_SRR
:
2397 case PPC405EP_CPC0_JTAGID
:
2400 case PPC405EP_CPC0_PCI
:
2406 static void ppc405ep_cpc_reset (void *opaque
)
2408 ppc405ep_cpc_t
*cpc
= opaque
;
2410 cpc
->boot
= 0x00000010; /* Boot from PCI - IIC EEPROM disabled */
2411 cpc
->epctl
= 0x00000000;
2412 cpc
->pllmr
[0] = 0x00011010;
2413 cpc
->pllmr
[1] = 0x40000000;
2414 cpc
->ucr
= 0x00000000;
2415 cpc
->srr
= 0x00040000;
2416 cpc
->pci
= 0x00000000;
2417 cpc
->er
= 0x00000000;
2418 cpc
->fr
= 0x00000000;
2419 cpc
->sr
= 0x00000000;
2420 ppc405ep_compute_clocks(cpc
);
2423 /* XXX: sysclk should be between 25 and 100 MHz */
2424 static void ppc405ep_cpc_init (CPUPPCState
*env
, clk_setup_t clk_setup
[8],
2427 ppc405ep_cpc_t
*cpc
;
2429 cpc
= g_malloc0(sizeof(ppc405ep_cpc_t
));
2430 memcpy(cpc
->clk_setup
, clk_setup
,
2431 PPC405EP_CLK_NB
* sizeof(clk_setup_t
));
2432 cpc
->jtagid
= 0x20267049;
2433 cpc
->sysclk
= sysclk
;
2434 qemu_register_reset(&ppc405ep_cpc_reset
, cpc
);
2435 ppc_dcr_register(env
, PPC405EP_CPC0_BOOT
, cpc
,
2436 &dcr_read_epcpc
, &dcr_write_epcpc
);
2437 ppc_dcr_register(env
, PPC405EP_CPC0_EPCTL
, cpc
,
2438 &dcr_read_epcpc
, &dcr_write_epcpc
);
2439 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR0
, cpc
,
2440 &dcr_read_epcpc
, &dcr_write_epcpc
);
2441 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR1
, cpc
,
2442 &dcr_read_epcpc
, &dcr_write_epcpc
);
2443 ppc_dcr_register(env
, PPC405EP_CPC0_UCR
, cpc
,
2444 &dcr_read_epcpc
, &dcr_write_epcpc
);
2445 ppc_dcr_register(env
, PPC405EP_CPC0_SRR
, cpc
,
2446 &dcr_read_epcpc
, &dcr_write_epcpc
);
2447 ppc_dcr_register(env
, PPC405EP_CPC0_JTAGID
, cpc
,
2448 &dcr_read_epcpc
, &dcr_write_epcpc
);
2449 ppc_dcr_register(env
, PPC405EP_CPC0_PCI
, cpc
,
2450 &dcr_read_epcpc
, &dcr_write_epcpc
);
2452 ppc_dcr_register(env
, PPC405EP_CPC0_ER
, cpc
,
2453 &dcr_read_epcpc
, &dcr_write_epcpc
);
2454 ppc_dcr_register(env
, PPC405EP_CPC0_FR
, cpc
,
2455 &dcr_read_epcpc
, &dcr_write_epcpc
);
2456 ppc_dcr_register(env
, PPC405EP_CPC0_SR
, cpc
,
2457 &dcr_read_epcpc
, &dcr_write_epcpc
);
2461 CPUPPCState
*ppc405ep_init(MemoryRegion
*address_space_mem
,
2462 MemoryRegion ram_memories
[2],
2463 hwaddr ram_bases
[2],
2464 hwaddr ram_sizes
[2],
2465 uint32_t sysclk
, qemu_irq
**picp
,
2468 clk_setup_t clk_setup
[PPC405EP_CLK_NB
], tlb_clk_setup
;
2469 qemu_irq dma_irqs
[4], gpt_irqs
[5], mal_irqs
[4];
2472 qemu_irq
*pic
, *irqs
;
2474 memset(clk_setup
, 0, sizeof(clk_setup
));
2476 cpu
= ppc4xx_init("405ep", &clk_setup
[PPC405EP_CPU_CLK
],
2477 &tlb_clk_setup
, sysclk
);
2479 clk_setup
[PPC405EP_CPU_CLK
].cb
= tlb_clk_setup
.cb
;
2480 clk_setup
[PPC405EP_CPU_CLK
].opaque
= tlb_clk_setup
.opaque
;
2481 /* Internal devices init */
2482 /* Memory mapped devices registers */
2484 ppc4xx_plb_init(env
);
2485 /* PLB to OPB bridge */
2486 ppc4xx_pob_init(env
);
2488 ppc4xx_opba_init(0xef600600);
2489 /* Initialize timers */
2490 ppc_booke_timers_init(cpu
, sysclk
, 0);
2491 /* Universal interrupt controller */
2492 irqs
= g_malloc0(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2493 irqs
[PPCUIC_OUTPUT_INT
] =
2494 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2495 irqs
[PPCUIC_OUTPUT_CINT
] =
2496 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2497 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2499 /* SDRAM controller */
2500 /* XXX 405EP has no ECC interrupt */
2501 ppc4xx_sdram_init(env
, pic
[17], 2, ram_memories
,
2502 ram_bases
, ram_sizes
, do_init
);
2503 /* External bus controller */
2504 ppc405_ebc_init(env
);
2505 /* DMA controller */
2506 dma_irqs
[0] = pic
[5];
2507 dma_irqs
[1] = pic
[6];
2508 dma_irqs
[2] = pic
[7];
2509 dma_irqs
[3] = pic
[8];
2510 ppc405_dma_init(env
, dma_irqs
);
2511 /* IIC controller */
2512 ppc405_i2c_init(0xef600500, pic
[2]);
2514 ppc405_gpio_init(0xef600700);
2516 if (serial_hds
[0] != NULL
) {
2517 serial_mm_init(address_space_mem
, 0xef600300, 0, pic
[0],
2518 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[0],
2521 if (serial_hds
[1] != NULL
) {
2522 serial_mm_init(address_space_mem
, 0xef600400, 0, pic
[1],
2523 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[1],
2527 ppc405_ocm_init(env
);
2529 gpt_irqs
[0] = pic
[19];
2530 gpt_irqs
[1] = pic
[20];
2531 gpt_irqs
[2] = pic
[21];
2532 gpt_irqs
[3] = pic
[22];
2533 gpt_irqs
[4] = pic
[23];
2534 ppc4xx_gpt_init(0xef600000, gpt_irqs
);
2536 /* Uses pic[3], pic[16], pic[18] */
2538 mal_irqs
[0] = pic
[11];
2539 mal_irqs
[1] = pic
[12];
2540 mal_irqs
[2] = pic
[13];
2541 mal_irqs
[3] = pic
[14];
2542 ppc405_mal_init(env
, mal_irqs
);
2544 /* Uses pic[9], pic[15], pic[17] */
2546 ppc405ep_cpc_init(env
, clk_setup
, sysclk
);