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
28 #include "qemu-timer.h"
31 #include "exec-memory.h"
42 //#define DEBUG_CLOCKS_LL
44 ram_addr_t
ppc405_set_bootinfo (CPUPPCState
*env
, ppc4xx_bd_info_t
*bd
,
50 /* We put the bd structure at the top of memory */
51 if (bd
->bi_memsize
>= 0x01000000UL
)
52 bdloc
= 0x01000000UL
- sizeof(struct ppc4xx_bd_info_t
);
54 bdloc
= bd
->bi_memsize
- sizeof(struct ppc4xx_bd_info_t
);
55 stl_be_phys(bdloc
+ 0x00, bd
->bi_memstart
);
56 stl_be_phys(bdloc
+ 0x04, bd
->bi_memsize
);
57 stl_be_phys(bdloc
+ 0x08, bd
->bi_flashstart
);
58 stl_be_phys(bdloc
+ 0x0C, bd
->bi_flashsize
);
59 stl_be_phys(bdloc
+ 0x10, bd
->bi_flashoffset
);
60 stl_be_phys(bdloc
+ 0x14, bd
->bi_sramstart
);
61 stl_be_phys(bdloc
+ 0x18, bd
->bi_sramsize
);
62 stl_be_phys(bdloc
+ 0x1C, bd
->bi_bootflags
);
63 stl_be_phys(bdloc
+ 0x20, bd
->bi_ipaddr
);
64 for (i
= 0; i
< 6; i
++) {
65 stb_phys(bdloc
+ 0x24 + i
, bd
->bi_enetaddr
[i
]);
67 stw_be_phys(bdloc
+ 0x2A, bd
->bi_ethspeed
);
68 stl_be_phys(bdloc
+ 0x2C, bd
->bi_intfreq
);
69 stl_be_phys(bdloc
+ 0x30, bd
->bi_busfreq
);
70 stl_be_phys(bdloc
+ 0x34, bd
->bi_baudrate
);
71 for (i
= 0; i
< 4; i
++) {
72 stb_phys(bdloc
+ 0x38 + i
, bd
->bi_s_version
[i
]);
74 for (i
= 0; i
< 32; i
++) {
75 stb_phys(bdloc
+ 0x3C + i
, bd
->bi_r_version
[i
]);
77 stl_be_phys(bdloc
+ 0x5C, bd
->bi_plb_busfreq
);
78 stl_be_phys(bdloc
+ 0x60, bd
->bi_pci_busfreq
);
79 for (i
= 0; i
< 6; i
++) {
80 stb_phys(bdloc
+ 0x64 + i
, bd
->bi_pci_enetaddr
[i
]);
83 if (flags
& 0x00000001) {
84 for (i
= 0; i
< 6; i
++)
85 stb_phys(bdloc
+ n
++, bd
->bi_pci_enetaddr2
[i
]);
87 stl_be_phys(bdloc
+ n
, bd
->bi_opbfreq
);
89 for (i
= 0; i
< 2; i
++) {
90 stl_be_phys(bdloc
+ n
, bd
->bi_iic_fast
[i
]);
97 /*****************************************************************************/
98 /* Shared peripherals */
100 /*****************************************************************************/
101 /* Peripheral local bus arbitrer */
108 typedef struct ppc4xx_plb_t ppc4xx_plb_t
;
109 struct ppc4xx_plb_t
{
115 static uint32_t dcr_read_plb (void *opaque
, int dcrn
)
132 /* Avoid gcc warning */
140 static void dcr_write_plb (void *opaque
, int dcrn
, uint32_t val
)
147 /* We don't care about the actual parameters written as
148 * we don't manage any priorities on the bus
150 plb
->acr
= val
& 0xF8000000;
162 static void ppc4xx_plb_reset (void *opaque
)
167 plb
->acr
= 0x00000000;
168 plb
->bear
= 0x00000000;
169 plb
->besr
= 0x00000000;
172 static void ppc4xx_plb_init(CPUPPCState
*env
)
176 plb
= g_malloc0(sizeof(ppc4xx_plb_t
));
177 ppc_dcr_register(env
, PLB0_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
178 ppc_dcr_register(env
, PLB0_BEAR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
179 ppc_dcr_register(env
, PLB0_BESR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
180 qemu_register_reset(ppc4xx_plb_reset
, plb
);
183 /*****************************************************************************/
184 /* PLB to OPB bridge */
191 typedef struct ppc4xx_pob_t ppc4xx_pob_t
;
192 struct ppc4xx_pob_t
{
197 static uint32_t dcr_read_pob (void *opaque
, int dcrn
)
209 ret
= pob
->besr
[dcrn
- POB0_BESR0
];
212 /* Avoid gcc warning */
220 static void dcr_write_pob (void *opaque
, int dcrn
, uint32_t val
)
232 pob
->besr
[dcrn
- POB0_BESR0
] &= ~val
;
237 static void ppc4xx_pob_reset (void *opaque
)
243 pob
->bear
= 0x00000000;
244 pob
->besr
[0] = 0x0000000;
245 pob
->besr
[1] = 0x0000000;
248 static void ppc4xx_pob_init(CPUPPCState
*env
)
252 pob
= g_malloc0(sizeof(ppc4xx_pob_t
));
253 ppc_dcr_register(env
, POB0_BEAR
, pob
, &dcr_read_pob
, &dcr_write_pob
);
254 ppc_dcr_register(env
, POB0_BESR0
, pob
, &dcr_read_pob
, &dcr_write_pob
);
255 ppc_dcr_register(env
, POB0_BESR1
, pob
, &dcr_read_pob
, &dcr_write_pob
);
256 qemu_register_reset(ppc4xx_pob_reset
, pob
);
259 /*****************************************************************************/
261 typedef struct ppc4xx_opba_t ppc4xx_opba_t
;
262 struct ppc4xx_opba_t
{
268 static uint32_t opba_readb (void *opaque
, target_phys_addr_t addr
)
274 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
292 static void opba_writeb (void *opaque
,
293 target_phys_addr_t addr
, uint32_t value
)
298 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
304 opba
->cr
= value
& 0xF8;
307 opba
->pr
= value
& 0xFF;
314 static uint32_t opba_readw (void *opaque
, target_phys_addr_t addr
)
319 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
321 ret
= opba_readb(opaque
, addr
) << 8;
322 ret
|= opba_readb(opaque
, addr
+ 1);
327 static void opba_writew (void *opaque
,
328 target_phys_addr_t addr
, uint32_t value
)
331 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
334 opba_writeb(opaque
, addr
, value
>> 8);
335 opba_writeb(opaque
, addr
+ 1, value
);
338 static uint32_t opba_readl (void *opaque
, target_phys_addr_t addr
)
343 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
345 ret
= opba_readb(opaque
, addr
) << 24;
346 ret
|= opba_readb(opaque
, addr
+ 1) << 16;
351 static void opba_writel (void *opaque
,
352 target_phys_addr_t addr
, uint32_t value
)
355 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
358 opba_writeb(opaque
, addr
, value
>> 24);
359 opba_writeb(opaque
, addr
+ 1, value
>> 16);
362 static const MemoryRegionOps opba_ops
= {
364 .read
= { opba_readb
, opba_readw
, opba_readl
, },
365 .write
= { opba_writeb
, opba_writew
, opba_writel
, },
367 .endianness
= DEVICE_NATIVE_ENDIAN
,
370 static void ppc4xx_opba_reset (void *opaque
)
375 opba
->cr
= 0x00; /* No dynamic priorities - park disabled */
379 static void ppc4xx_opba_init(target_phys_addr_t base
)
383 opba
= g_malloc0(sizeof(ppc4xx_opba_t
));
385 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
387 memory_region_init_io(&opba
->io
, &opba_ops
, opba
, "opba", 0x002);
388 memory_region_add_subregion(get_system_memory(), base
, &opba
->io
);
389 qemu_register_reset(ppc4xx_opba_reset
, opba
);
392 /*****************************************************************************/
393 /* Code decompression controller */
396 /*****************************************************************************/
397 /* Peripheral controller */
398 typedef struct ppc4xx_ebc_t ppc4xx_ebc_t
;
399 struct ppc4xx_ebc_t
{
410 EBC0_CFGADDR
= 0x012,
411 EBC0_CFGDATA
= 0x013,
414 static uint32_t dcr_read_ebc (void *opaque
, int dcrn
)
426 case 0x00: /* B0CR */
429 case 0x01: /* B1CR */
432 case 0x02: /* B2CR */
435 case 0x03: /* B3CR */
438 case 0x04: /* B4CR */
441 case 0x05: /* B5CR */
444 case 0x06: /* B6CR */
447 case 0x07: /* B7CR */
450 case 0x10: /* B0AP */
453 case 0x11: /* B1AP */
456 case 0x12: /* B2AP */
459 case 0x13: /* B3AP */
462 case 0x14: /* B4AP */
465 case 0x15: /* B5AP */
468 case 0x16: /* B6AP */
471 case 0x17: /* B7AP */
474 case 0x20: /* BEAR */
477 case 0x21: /* BESR0 */
480 case 0x22: /* BESR1 */
499 static void dcr_write_ebc (void *opaque
, int dcrn
, uint32_t val
)
510 case 0x00: /* B0CR */
512 case 0x01: /* B1CR */
514 case 0x02: /* B2CR */
516 case 0x03: /* B3CR */
518 case 0x04: /* B4CR */
520 case 0x05: /* B5CR */
522 case 0x06: /* B6CR */
524 case 0x07: /* B7CR */
526 case 0x10: /* B0AP */
528 case 0x11: /* B1AP */
530 case 0x12: /* B2AP */
532 case 0x13: /* B3AP */
534 case 0x14: /* B4AP */
536 case 0x15: /* B5AP */
538 case 0x16: /* B6AP */
540 case 0x17: /* B7AP */
542 case 0x20: /* BEAR */
544 case 0x21: /* BESR0 */
546 case 0x22: /* BESR1 */
559 static void ebc_reset (void *opaque
)
565 ebc
->addr
= 0x00000000;
566 ebc
->bap
[0] = 0x7F8FFE80;
567 ebc
->bcr
[0] = 0xFFE28000;
568 for (i
= 0; i
< 8; i
++) {
569 ebc
->bap
[i
] = 0x00000000;
570 ebc
->bcr
[i
] = 0x00000000;
572 ebc
->besr0
= 0x00000000;
573 ebc
->besr1
= 0x00000000;
574 ebc
->cfg
= 0x80400000;
577 static void ppc405_ebc_init(CPUPPCState
*env
)
581 ebc
= g_malloc0(sizeof(ppc4xx_ebc_t
));
582 qemu_register_reset(&ebc_reset
, ebc
);
583 ppc_dcr_register(env
, EBC0_CFGADDR
,
584 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
585 ppc_dcr_register(env
, EBC0_CFGDATA
,
586 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
589 /*****************************************************************************/
618 typedef struct ppc405_dma_t ppc405_dma_t
;
619 struct ppc405_dma_t
{
632 static uint32_t dcr_read_dma (void *opaque
, int dcrn
)
637 static void dcr_write_dma (void *opaque
, int dcrn
, uint32_t val
)
641 static void ppc405_dma_reset (void *opaque
)
647 for (i
= 0; i
< 4; i
++) {
648 dma
->cr
[i
] = 0x00000000;
649 dma
->ct
[i
] = 0x00000000;
650 dma
->da
[i
] = 0x00000000;
651 dma
->sa
[i
] = 0x00000000;
652 dma
->sg
[i
] = 0x00000000;
654 dma
->sr
= 0x00000000;
655 dma
->sgc
= 0x00000000;
656 dma
->slp
= 0x7C000000;
657 dma
->pol
= 0x00000000;
660 static void ppc405_dma_init(CPUPPCState
*env
, qemu_irq irqs
[4])
664 dma
= g_malloc0(sizeof(ppc405_dma_t
));
665 memcpy(dma
->irqs
, irqs
, 4 * sizeof(qemu_irq
));
666 qemu_register_reset(&ppc405_dma_reset
, dma
);
667 ppc_dcr_register(env
, DMA0_CR0
,
668 dma
, &dcr_read_dma
, &dcr_write_dma
);
669 ppc_dcr_register(env
, DMA0_CT0
,
670 dma
, &dcr_read_dma
, &dcr_write_dma
);
671 ppc_dcr_register(env
, DMA0_DA0
,
672 dma
, &dcr_read_dma
, &dcr_write_dma
);
673 ppc_dcr_register(env
, DMA0_SA0
,
674 dma
, &dcr_read_dma
, &dcr_write_dma
);
675 ppc_dcr_register(env
, DMA0_SG0
,
676 dma
, &dcr_read_dma
, &dcr_write_dma
);
677 ppc_dcr_register(env
, DMA0_CR1
,
678 dma
, &dcr_read_dma
, &dcr_write_dma
);
679 ppc_dcr_register(env
, DMA0_CT1
,
680 dma
, &dcr_read_dma
, &dcr_write_dma
);
681 ppc_dcr_register(env
, DMA0_DA1
,
682 dma
, &dcr_read_dma
, &dcr_write_dma
);
683 ppc_dcr_register(env
, DMA0_SA1
,
684 dma
, &dcr_read_dma
, &dcr_write_dma
);
685 ppc_dcr_register(env
, DMA0_SG1
,
686 dma
, &dcr_read_dma
, &dcr_write_dma
);
687 ppc_dcr_register(env
, DMA0_CR2
,
688 dma
, &dcr_read_dma
, &dcr_write_dma
);
689 ppc_dcr_register(env
, DMA0_CT2
,
690 dma
, &dcr_read_dma
, &dcr_write_dma
);
691 ppc_dcr_register(env
, DMA0_DA2
,
692 dma
, &dcr_read_dma
, &dcr_write_dma
);
693 ppc_dcr_register(env
, DMA0_SA2
,
694 dma
, &dcr_read_dma
, &dcr_write_dma
);
695 ppc_dcr_register(env
, DMA0_SG2
,
696 dma
, &dcr_read_dma
, &dcr_write_dma
);
697 ppc_dcr_register(env
, DMA0_CR3
,
698 dma
, &dcr_read_dma
, &dcr_write_dma
);
699 ppc_dcr_register(env
, DMA0_CT3
,
700 dma
, &dcr_read_dma
, &dcr_write_dma
);
701 ppc_dcr_register(env
, DMA0_DA3
,
702 dma
, &dcr_read_dma
, &dcr_write_dma
);
703 ppc_dcr_register(env
, DMA0_SA3
,
704 dma
, &dcr_read_dma
, &dcr_write_dma
);
705 ppc_dcr_register(env
, DMA0_SG3
,
706 dma
, &dcr_read_dma
, &dcr_write_dma
);
707 ppc_dcr_register(env
, DMA0_SR
,
708 dma
, &dcr_read_dma
, &dcr_write_dma
);
709 ppc_dcr_register(env
, DMA0_SGC
,
710 dma
, &dcr_read_dma
, &dcr_write_dma
);
711 ppc_dcr_register(env
, DMA0_SLP
,
712 dma
, &dcr_read_dma
, &dcr_write_dma
);
713 ppc_dcr_register(env
, DMA0_POL
,
714 dma
, &dcr_read_dma
, &dcr_write_dma
);
717 /*****************************************************************************/
719 typedef struct ppc405_gpio_t ppc405_gpio_t
;
720 struct ppc405_gpio_t
{
735 static uint32_t ppc405_gpio_readb (void *opaque
, target_phys_addr_t addr
)
738 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
744 static void ppc405_gpio_writeb (void *opaque
,
745 target_phys_addr_t addr
, uint32_t value
)
748 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
753 static uint32_t ppc405_gpio_readw (void *opaque
, target_phys_addr_t addr
)
756 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
762 static void ppc405_gpio_writew (void *opaque
,
763 target_phys_addr_t addr
, uint32_t value
)
766 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
771 static uint32_t ppc405_gpio_readl (void *opaque
, target_phys_addr_t addr
)
774 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
780 static void ppc405_gpio_writel (void *opaque
,
781 target_phys_addr_t addr
, uint32_t value
)
784 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
789 static const MemoryRegionOps ppc405_gpio_ops
= {
791 .read
= { ppc405_gpio_readb
, ppc405_gpio_readw
, ppc405_gpio_readl
, },
792 .write
= { ppc405_gpio_writeb
, ppc405_gpio_writew
, ppc405_gpio_writel
, },
794 .endianness
= DEVICE_NATIVE_ENDIAN
,
797 static void ppc405_gpio_reset (void *opaque
)
801 static void ppc405_gpio_init(target_phys_addr_t base
)
805 gpio
= g_malloc0(sizeof(ppc405_gpio_t
));
807 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
809 memory_region_init_io(&gpio
->io
, &ppc405_gpio_ops
, gpio
, "pgio", 0x038);
810 memory_region_add_subregion(get_system_memory(), base
, &gpio
->io
);
811 qemu_register_reset(&ppc405_gpio_reset
, gpio
);
814 /*****************************************************************************/
818 OCM0_ISACNTL
= 0x019,
820 OCM0_DSACNTL
= 0x01B,
823 typedef struct ppc405_ocm_t ppc405_ocm_t
;
824 struct ppc405_ocm_t
{
826 MemoryRegion isarc_ram
;
827 MemoryRegion dsarc_ram
;
834 static void ocm_update_mappings (ppc405_ocm_t
*ocm
,
835 uint32_t isarc
, uint32_t isacntl
,
836 uint32_t dsarc
, uint32_t dsacntl
)
839 printf("OCM update ISA %08" PRIx32
" %08" PRIx32
" (%08" PRIx32
840 " %08" PRIx32
") DSA %08" PRIx32
" %08" PRIx32
841 " (%08" PRIx32
" %08" PRIx32
")\n",
842 isarc
, isacntl
, dsarc
, dsacntl
,
843 ocm
->isarc
, ocm
->isacntl
, ocm
->dsarc
, ocm
->dsacntl
);
845 if (ocm
->isarc
!= isarc
||
846 (ocm
->isacntl
& 0x80000000) != (isacntl
& 0x80000000)) {
847 if (ocm
->isacntl
& 0x80000000) {
848 /* Unmap previously assigned memory region */
849 printf("OCM unmap ISA %08" PRIx32
"\n", ocm
->isarc
);
850 memory_region_del_subregion(get_system_memory(), &ocm
->isarc_ram
);
852 if (isacntl
& 0x80000000) {
853 /* Map new instruction memory region */
855 printf("OCM map ISA %08" PRIx32
"\n", isarc
);
857 memory_region_add_subregion(get_system_memory(), isarc
,
861 if (ocm
->dsarc
!= dsarc
||
862 (ocm
->dsacntl
& 0x80000000) != (dsacntl
& 0x80000000)) {
863 if (ocm
->dsacntl
& 0x80000000) {
864 /* Beware not to unmap the region we just mapped */
865 if (!(isacntl
& 0x80000000) || ocm
->dsarc
!= isarc
) {
866 /* Unmap previously assigned memory region */
868 printf("OCM unmap DSA %08" PRIx32
"\n", ocm
->dsarc
);
870 memory_region_del_subregion(get_system_memory(),
874 if (dsacntl
& 0x80000000) {
875 /* Beware not to remap the region we just mapped */
876 if (!(isacntl
& 0x80000000) || dsarc
!= isarc
) {
877 /* Map new data memory region */
879 printf("OCM map DSA %08" PRIx32
"\n", dsarc
);
881 memory_region_add_subregion(get_system_memory(), dsarc
,
888 static uint32_t dcr_read_ocm (void *opaque
, int dcrn
)
915 static void dcr_write_ocm (void *opaque
, int dcrn
, uint32_t val
)
918 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
923 isacntl
= ocm
->isacntl
;
924 dsacntl
= ocm
->dsacntl
;
927 isarc
= val
& 0xFC000000;
930 isacntl
= val
& 0xC0000000;
933 isarc
= val
& 0xFC000000;
936 isacntl
= val
& 0xC0000000;
939 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
942 ocm
->isacntl
= isacntl
;
943 ocm
->dsacntl
= dsacntl
;
946 static void ocm_reset (void *opaque
)
949 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
953 isacntl
= 0x00000000;
955 dsacntl
= 0x00000000;
956 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
959 ocm
->isacntl
= isacntl
;
960 ocm
->dsacntl
= dsacntl
;
963 static void ppc405_ocm_init(CPUPPCState
*env
)
967 ocm
= g_malloc0(sizeof(ppc405_ocm_t
));
968 /* XXX: Size is 4096 or 0x04000000 */
969 memory_region_init_ram(&ocm
->isarc_ram
, "ppc405.ocm", 4096);
970 vmstate_register_ram_global(&ocm
->isarc_ram
);
971 memory_region_init_alias(&ocm
->dsarc_ram
, "ppc405.dsarc", &ocm
->isarc_ram
,
973 qemu_register_reset(&ocm_reset
, ocm
);
974 ppc_dcr_register(env
, OCM0_ISARC
,
975 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
976 ppc_dcr_register(env
, OCM0_ISACNTL
,
977 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
978 ppc_dcr_register(env
, OCM0_DSARC
,
979 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
980 ppc_dcr_register(env
, OCM0_DSACNTL
,
981 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
984 /*****************************************************************************/
986 typedef struct ppc4xx_i2c_t ppc4xx_i2c_t
;
987 struct ppc4xx_i2c_t
{
1007 static uint32_t ppc4xx_i2c_readb (void *opaque
, target_phys_addr_t addr
)
1013 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1018 // i2c_readbyte(&i2c->mdata);
1058 ret
= i2c
->xtcntlss
;
1061 ret
= i2c
->directcntl
;
1068 printf("%s: addr " TARGET_FMT_plx
" %02" PRIx32
"\n", __func__
, addr
, ret
);
1074 static void ppc4xx_i2c_writeb (void *opaque
,
1075 target_phys_addr_t addr
, uint32_t value
)
1080 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1087 // i2c_sendbyte(&i2c->mdata);
1102 i2c
->mdcntl
= value
& 0xDF;
1105 i2c
->sts
&= ~(value
& 0x0A);
1108 i2c
->extsts
&= ~(value
& 0x8F);
1117 i2c
->clkdiv
= value
;
1120 i2c
->intrmsk
= value
;
1123 i2c
->xfrcnt
= value
& 0x77;
1126 i2c
->xtcntlss
= value
;
1129 i2c
->directcntl
= value
& 0x7;
1134 static uint32_t ppc4xx_i2c_readw (void *opaque
, target_phys_addr_t addr
)
1139 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1141 ret
= ppc4xx_i2c_readb(opaque
, addr
) << 8;
1142 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 1);
1147 static void ppc4xx_i2c_writew (void *opaque
,
1148 target_phys_addr_t addr
, uint32_t value
)
1151 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1154 ppc4xx_i2c_writeb(opaque
, addr
, value
>> 8);
1155 ppc4xx_i2c_writeb(opaque
, addr
+ 1, value
);
1158 static uint32_t ppc4xx_i2c_readl (void *opaque
, target_phys_addr_t addr
)
1163 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1165 ret
= ppc4xx_i2c_readb(opaque
, addr
) << 24;
1166 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 1) << 16;
1167 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 2) << 8;
1168 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 3);
1173 static void ppc4xx_i2c_writel (void *opaque
,
1174 target_phys_addr_t addr
, uint32_t value
)
1177 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1180 ppc4xx_i2c_writeb(opaque
, addr
, value
>> 24);
1181 ppc4xx_i2c_writeb(opaque
, addr
+ 1, value
>> 16);
1182 ppc4xx_i2c_writeb(opaque
, addr
+ 2, value
>> 8);
1183 ppc4xx_i2c_writeb(opaque
, addr
+ 3, value
);
1186 static const MemoryRegionOps i2c_ops
= {
1188 .read
= { ppc4xx_i2c_readb
, ppc4xx_i2c_readw
, ppc4xx_i2c_readl
, },
1189 .write
= { ppc4xx_i2c_writeb
, ppc4xx_i2c_writew
, ppc4xx_i2c_writel
, },
1191 .endianness
= DEVICE_NATIVE_ENDIAN
,
1194 static void ppc4xx_i2c_reset (void *opaque
)
1207 i2c
->directcntl
= 0x0F;
1210 static void ppc405_i2c_init(target_phys_addr_t base
, qemu_irq irq
)
1214 i2c
= g_malloc0(sizeof(ppc4xx_i2c_t
));
1217 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1219 memory_region_init_io(&i2c
->iomem
, &i2c_ops
, i2c
, "i2c", 0x011);
1220 memory_region_add_subregion(get_system_memory(), base
, &i2c
->iomem
);
1221 qemu_register_reset(ppc4xx_i2c_reset
, i2c
);
1224 /*****************************************************************************/
1225 /* General purpose timers */
1226 typedef struct ppc4xx_gpt_t ppc4xx_gpt_t
;
1227 struct ppc4xx_gpt_t
{
1231 struct QEMUTimer
*timer
;
1242 static uint32_t ppc4xx_gpt_readb (void *opaque
, target_phys_addr_t addr
)
1245 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1247 /* XXX: generate a bus fault */
1251 static void ppc4xx_gpt_writeb (void *opaque
,
1252 target_phys_addr_t addr
, uint32_t value
)
1255 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1258 /* XXX: generate a bus fault */
1261 static uint32_t ppc4xx_gpt_readw (void *opaque
, target_phys_addr_t addr
)
1264 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1266 /* XXX: generate a bus fault */
1270 static void ppc4xx_gpt_writew (void *opaque
,
1271 target_phys_addr_t addr
, uint32_t value
)
1274 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1277 /* XXX: generate a bus fault */
1280 static int ppc4xx_gpt_compare (ppc4xx_gpt_t
*gpt
, int n
)
1286 static void ppc4xx_gpt_set_output (ppc4xx_gpt_t
*gpt
, int n
, int level
)
1291 static void ppc4xx_gpt_set_outputs (ppc4xx_gpt_t
*gpt
)
1297 for (i
= 0; i
< 5; i
++) {
1298 if (gpt
->oe
& mask
) {
1299 /* Output is enabled */
1300 if (ppc4xx_gpt_compare(gpt
, i
)) {
1301 /* Comparison is OK */
1302 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
);
1304 /* Comparison is KO */
1305 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
? 0 : 1);
1312 static void ppc4xx_gpt_set_irqs (ppc4xx_gpt_t
*gpt
)
1318 for (i
= 0; i
< 5; i
++) {
1319 if (gpt
->is
& gpt
->im
& mask
)
1320 qemu_irq_raise(gpt
->irqs
[i
]);
1322 qemu_irq_lower(gpt
->irqs
[i
]);
1327 static void ppc4xx_gpt_compute_timer (ppc4xx_gpt_t
*gpt
)
1332 static uint32_t ppc4xx_gpt_readl (void *opaque
, target_phys_addr_t addr
)
1339 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1344 /* Time base counter */
1345 ret
= muldiv64(qemu_get_clock_ns(vm_clock
) + gpt
->tb_offset
,
1346 gpt
->tb_freq
, get_ticks_per_sec());
1357 /* Interrupt mask */
1362 /* Interrupt status */
1366 /* Interrupt enable */
1371 idx
= (addr
- 0x80) >> 2;
1372 ret
= gpt
->comp
[idx
];
1376 idx
= (addr
- 0xC0) >> 2;
1377 ret
= gpt
->mask
[idx
];
1387 static void ppc4xx_gpt_writel (void *opaque
,
1388 target_phys_addr_t addr
, uint32_t value
)
1394 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1400 /* Time base counter */
1401 gpt
->tb_offset
= muldiv64(value
, get_ticks_per_sec(), gpt
->tb_freq
)
1402 - qemu_get_clock_ns(vm_clock
);
1403 ppc4xx_gpt_compute_timer(gpt
);
1407 gpt
->oe
= value
& 0xF8000000;
1408 ppc4xx_gpt_set_outputs(gpt
);
1412 gpt
->ol
= value
& 0xF8000000;
1413 ppc4xx_gpt_set_outputs(gpt
);
1416 /* Interrupt mask */
1417 gpt
->im
= value
& 0x0000F800;
1420 /* Interrupt status set */
1421 gpt
->is
|= value
& 0x0000F800;
1422 ppc4xx_gpt_set_irqs(gpt
);
1425 /* Interrupt status clear */
1426 gpt
->is
&= ~(value
& 0x0000F800);
1427 ppc4xx_gpt_set_irqs(gpt
);
1430 /* Interrupt enable */
1431 gpt
->ie
= value
& 0x0000F800;
1432 ppc4xx_gpt_set_irqs(gpt
);
1436 idx
= (addr
- 0x80) >> 2;
1437 gpt
->comp
[idx
] = value
& 0xF8000000;
1438 ppc4xx_gpt_compute_timer(gpt
);
1442 idx
= (addr
- 0xC0) >> 2;
1443 gpt
->mask
[idx
] = value
& 0xF8000000;
1444 ppc4xx_gpt_compute_timer(gpt
);
1449 static const MemoryRegionOps gpt_ops
= {
1451 .read
= { ppc4xx_gpt_readb
, ppc4xx_gpt_readw
, ppc4xx_gpt_readl
, },
1452 .write
= { ppc4xx_gpt_writeb
, ppc4xx_gpt_writew
, ppc4xx_gpt_writel
, },
1454 .endianness
= DEVICE_NATIVE_ENDIAN
,
1457 static void ppc4xx_gpt_cb (void *opaque
)
1462 ppc4xx_gpt_set_irqs(gpt
);
1463 ppc4xx_gpt_set_outputs(gpt
);
1464 ppc4xx_gpt_compute_timer(gpt
);
1467 static void ppc4xx_gpt_reset (void *opaque
)
1473 qemu_del_timer(gpt
->timer
);
1474 gpt
->oe
= 0x00000000;
1475 gpt
->ol
= 0x00000000;
1476 gpt
->im
= 0x00000000;
1477 gpt
->is
= 0x00000000;
1478 gpt
->ie
= 0x00000000;
1479 for (i
= 0; i
< 5; i
++) {
1480 gpt
->comp
[i
] = 0x00000000;
1481 gpt
->mask
[i
] = 0x00000000;
1485 static void ppc4xx_gpt_init(target_phys_addr_t base
, qemu_irq irqs
[5])
1490 gpt
= g_malloc0(sizeof(ppc4xx_gpt_t
));
1491 for (i
= 0; i
< 5; i
++) {
1492 gpt
->irqs
[i
] = irqs
[i
];
1494 gpt
->timer
= qemu_new_timer_ns(vm_clock
, &ppc4xx_gpt_cb
, gpt
);
1496 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1498 memory_region_init_io(&gpt
->iomem
, &gpt_ops
, gpt
, "gpt", 0x0d4);
1499 memory_region_add_subregion(get_system_memory(), base
, &gpt
->iomem
);
1500 qemu_register_reset(ppc4xx_gpt_reset
, gpt
);
1503 /*****************************************************************************/
1509 MAL0_TXCASR
= 0x184,
1510 MAL0_TXCARR
= 0x185,
1511 MAL0_TXEOBISR
= 0x186,
1512 MAL0_TXDEIR
= 0x187,
1513 MAL0_RXCASR
= 0x190,
1514 MAL0_RXCARR
= 0x191,
1515 MAL0_RXEOBISR
= 0x192,
1516 MAL0_RXDEIR
= 0x193,
1517 MAL0_TXCTP0R
= 0x1A0,
1518 MAL0_TXCTP1R
= 0x1A1,
1519 MAL0_TXCTP2R
= 0x1A2,
1520 MAL0_TXCTP3R
= 0x1A3,
1521 MAL0_RXCTP0R
= 0x1C0,
1522 MAL0_RXCTP1R
= 0x1C1,
1527 typedef struct ppc40x_mal_t ppc40x_mal_t
;
1528 struct ppc40x_mal_t
{
1546 static void ppc40x_mal_reset (void *opaque
);
1548 static uint32_t dcr_read_mal (void *opaque
, int dcrn
)
1571 ret
= mal
->txeobisr
;
1583 ret
= mal
->rxeobisr
;
1589 ret
= mal
->txctpr
[0];
1592 ret
= mal
->txctpr
[1];
1595 ret
= mal
->txctpr
[2];
1598 ret
= mal
->txctpr
[3];
1601 ret
= mal
->rxctpr
[0];
1604 ret
= mal
->rxctpr
[1];
1620 static void dcr_write_mal (void *opaque
, int dcrn
, uint32_t val
)
1628 if (val
& 0x80000000)
1629 ppc40x_mal_reset(mal
);
1630 mal
->cfg
= val
& 0x00FFC087;
1637 mal
->ier
= val
& 0x0000001F;
1640 mal
->txcasr
= val
& 0xF0000000;
1643 mal
->txcarr
= val
& 0xF0000000;
1647 mal
->txeobisr
&= ~val
;
1651 mal
->txdeir
&= ~val
;
1654 mal
->rxcasr
= val
& 0xC0000000;
1657 mal
->rxcarr
= val
& 0xC0000000;
1661 mal
->rxeobisr
&= ~val
;
1665 mal
->rxdeir
&= ~val
;
1679 mal
->txctpr
[idx
] = val
;
1687 mal
->rxctpr
[idx
] = val
;
1691 goto update_rx_size
;
1695 mal
->rcbs
[idx
] = val
& 0x000000FF;
1700 static void ppc40x_mal_reset (void *opaque
)
1705 mal
->cfg
= 0x0007C000;
1706 mal
->esr
= 0x00000000;
1707 mal
->ier
= 0x00000000;
1708 mal
->rxcasr
= 0x00000000;
1709 mal
->rxdeir
= 0x00000000;
1710 mal
->rxeobisr
= 0x00000000;
1711 mal
->txcasr
= 0x00000000;
1712 mal
->txdeir
= 0x00000000;
1713 mal
->txeobisr
= 0x00000000;
1716 static void ppc405_mal_init(CPUPPCState
*env
, qemu_irq irqs
[4])
1721 mal
= g_malloc0(sizeof(ppc40x_mal_t
));
1722 for (i
= 0; i
< 4; i
++)
1723 mal
->irqs
[i
] = irqs
[i
];
1724 qemu_register_reset(&ppc40x_mal_reset
, mal
);
1725 ppc_dcr_register(env
, MAL0_CFG
,
1726 mal
, &dcr_read_mal
, &dcr_write_mal
);
1727 ppc_dcr_register(env
, MAL0_ESR
,
1728 mal
, &dcr_read_mal
, &dcr_write_mal
);
1729 ppc_dcr_register(env
, MAL0_IER
,
1730 mal
, &dcr_read_mal
, &dcr_write_mal
);
1731 ppc_dcr_register(env
, MAL0_TXCASR
,
1732 mal
, &dcr_read_mal
, &dcr_write_mal
);
1733 ppc_dcr_register(env
, MAL0_TXCARR
,
1734 mal
, &dcr_read_mal
, &dcr_write_mal
);
1735 ppc_dcr_register(env
, MAL0_TXEOBISR
,
1736 mal
, &dcr_read_mal
, &dcr_write_mal
);
1737 ppc_dcr_register(env
, MAL0_TXDEIR
,
1738 mal
, &dcr_read_mal
, &dcr_write_mal
);
1739 ppc_dcr_register(env
, MAL0_RXCASR
,
1740 mal
, &dcr_read_mal
, &dcr_write_mal
);
1741 ppc_dcr_register(env
, MAL0_RXCARR
,
1742 mal
, &dcr_read_mal
, &dcr_write_mal
);
1743 ppc_dcr_register(env
, MAL0_RXEOBISR
,
1744 mal
, &dcr_read_mal
, &dcr_write_mal
);
1745 ppc_dcr_register(env
, MAL0_RXDEIR
,
1746 mal
, &dcr_read_mal
, &dcr_write_mal
);
1747 ppc_dcr_register(env
, MAL0_TXCTP0R
,
1748 mal
, &dcr_read_mal
, &dcr_write_mal
);
1749 ppc_dcr_register(env
, MAL0_TXCTP1R
,
1750 mal
, &dcr_read_mal
, &dcr_write_mal
);
1751 ppc_dcr_register(env
, MAL0_TXCTP2R
,
1752 mal
, &dcr_read_mal
, &dcr_write_mal
);
1753 ppc_dcr_register(env
, MAL0_TXCTP3R
,
1754 mal
, &dcr_read_mal
, &dcr_write_mal
);
1755 ppc_dcr_register(env
, MAL0_RXCTP0R
,
1756 mal
, &dcr_read_mal
, &dcr_write_mal
);
1757 ppc_dcr_register(env
, MAL0_RXCTP1R
,
1758 mal
, &dcr_read_mal
, &dcr_write_mal
);
1759 ppc_dcr_register(env
, MAL0_RCBS0
,
1760 mal
, &dcr_read_mal
, &dcr_write_mal
);
1761 ppc_dcr_register(env
, MAL0_RCBS1
,
1762 mal
, &dcr_read_mal
, &dcr_write_mal
);
1765 /*****************************************************************************/
1767 void ppc40x_core_reset (CPUPPCState
*env
)
1771 printf("Reset PowerPC core\n");
1772 cpu_interrupt(env
, CPU_INTERRUPT_RESET
);
1773 dbsr
= env
->spr
[SPR_40x_DBSR
];
1774 dbsr
&= ~0x00000300;
1776 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1779 void ppc40x_chip_reset (CPUPPCState
*env
)
1783 printf("Reset PowerPC chip\n");
1784 cpu_interrupt(env
, CPU_INTERRUPT_RESET
);
1785 /* XXX: TODO reset all internal peripherals */
1786 dbsr
= env
->spr
[SPR_40x_DBSR
];
1787 dbsr
&= ~0x00000300;
1789 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1792 void ppc40x_system_reset (CPUPPCState
*env
)
1794 printf("Reset PowerPC system\n");
1795 qemu_system_reset_request();
1798 void store_40x_dbcr0 (CPUPPCState
*env
, uint32_t val
)
1800 switch ((val
>> 28) & 0x3) {
1806 ppc40x_core_reset(env
);
1810 ppc40x_chip_reset(env
);
1814 ppc40x_system_reset(env
);
1819 /*****************************************************************************/
1822 PPC405CR_CPC0_PLLMR
= 0x0B0,
1823 PPC405CR_CPC0_CR0
= 0x0B1,
1824 PPC405CR_CPC0_CR1
= 0x0B2,
1825 PPC405CR_CPC0_PSR
= 0x0B4,
1826 PPC405CR_CPC0_JTAGID
= 0x0B5,
1827 PPC405CR_CPC0_ER
= 0x0B9,
1828 PPC405CR_CPC0_FR
= 0x0BA,
1829 PPC405CR_CPC0_SR
= 0x0BB,
1833 PPC405CR_CPU_CLK
= 0,
1834 PPC405CR_TMR_CLK
= 1,
1835 PPC405CR_PLB_CLK
= 2,
1836 PPC405CR_SDRAM_CLK
= 3,
1837 PPC405CR_OPB_CLK
= 4,
1838 PPC405CR_EXT_CLK
= 5,
1839 PPC405CR_UART_CLK
= 6,
1840 PPC405CR_CLK_NB
= 7,
1843 typedef struct ppc405cr_cpc_t ppc405cr_cpc_t
;
1844 struct ppc405cr_cpc_t
{
1845 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
1856 static void ppc405cr_clk_setup (ppc405cr_cpc_t
*cpc
)
1858 uint64_t VCO_out
, PLL_out
;
1859 uint32_t CPU_clk
, TMR_clk
, SDRAM_clk
, PLB_clk
, OPB_clk
, EXT_clk
, UART_clk
;
1862 D0
= ((cpc
->pllmr
>> 26) & 0x3) + 1; /* CBDV */
1863 if (cpc
->pllmr
& 0x80000000) {
1864 D1
= (((cpc
->pllmr
>> 20) - 1) & 0xF) + 1; /* FBDV */
1865 D2
= 8 - ((cpc
->pllmr
>> 16) & 0x7); /* FWDVA */
1867 VCO_out
= cpc
->sysclk
* M
;
1868 if (VCO_out
< 400000000 || VCO_out
> 800000000) {
1869 /* PLL cannot lock */
1870 cpc
->pllmr
&= ~0x80000000;
1873 PLL_out
= VCO_out
/ D2
;
1878 PLL_out
= cpc
->sysclk
* M
;
1881 if (cpc
->cr1
& 0x00800000)
1882 TMR_clk
= cpc
->sysclk
; /* Should have a separate clock */
1885 PLB_clk
= CPU_clk
/ D0
;
1886 SDRAM_clk
= PLB_clk
;
1887 D0
= ((cpc
->pllmr
>> 10) & 0x3) + 1;
1888 OPB_clk
= PLB_clk
/ D0
;
1889 D0
= ((cpc
->pllmr
>> 24) & 0x3) + 2;
1890 EXT_clk
= PLB_clk
/ D0
;
1891 D0
= ((cpc
->cr0
>> 1) & 0x1F) + 1;
1892 UART_clk
= CPU_clk
/ D0
;
1893 /* Setup CPU clocks */
1894 clk_setup(&cpc
->clk_setup
[PPC405CR_CPU_CLK
], CPU_clk
);
1895 /* Setup time-base clock */
1896 clk_setup(&cpc
->clk_setup
[PPC405CR_TMR_CLK
], TMR_clk
);
1897 /* Setup PLB clock */
1898 clk_setup(&cpc
->clk_setup
[PPC405CR_PLB_CLK
], PLB_clk
);
1899 /* Setup SDRAM clock */
1900 clk_setup(&cpc
->clk_setup
[PPC405CR_SDRAM_CLK
], SDRAM_clk
);
1901 /* Setup OPB clock */
1902 clk_setup(&cpc
->clk_setup
[PPC405CR_OPB_CLK
], OPB_clk
);
1903 /* Setup external clock */
1904 clk_setup(&cpc
->clk_setup
[PPC405CR_EXT_CLK
], EXT_clk
);
1905 /* Setup UART clock */
1906 clk_setup(&cpc
->clk_setup
[PPC405CR_UART_CLK
], UART_clk
);
1909 static uint32_t dcr_read_crcpc (void *opaque
, int dcrn
)
1911 ppc405cr_cpc_t
*cpc
;
1916 case PPC405CR_CPC0_PLLMR
:
1919 case PPC405CR_CPC0_CR0
:
1922 case PPC405CR_CPC0_CR1
:
1925 case PPC405CR_CPC0_PSR
:
1928 case PPC405CR_CPC0_JTAGID
:
1931 case PPC405CR_CPC0_ER
:
1934 case PPC405CR_CPC0_FR
:
1937 case PPC405CR_CPC0_SR
:
1938 ret
= ~(cpc
->er
| cpc
->fr
) & 0xFFFF0000;
1941 /* Avoid gcc warning */
1949 static void dcr_write_crcpc (void *opaque
, int dcrn
, uint32_t val
)
1951 ppc405cr_cpc_t
*cpc
;
1955 case PPC405CR_CPC0_PLLMR
:
1956 cpc
->pllmr
= val
& 0xFFF77C3F;
1958 case PPC405CR_CPC0_CR0
:
1959 cpc
->cr0
= val
& 0x0FFFFFFE;
1961 case PPC405CR_CPC0_CR1
:
1962 cpc
->cr1
= val
& 0x00800000;
1964 case PPC405CR_CPC0_PSR
:
1967 case PPC405CR_CPC0_JTAGID
:
1970 case PPC405CR_CPC0_ER
:
1971 cpc
->er
= val
& 0xBFFC0000;
1973 case PPC405CR_CPC0_FR
:
1974 cpc
->fr
= val
& 0xBFFC0000;
1976 case PPC405CR_CPC0_SR
:
1982 static void ppc405cr_cpc_reset (void *opaque
)
1984 ppc405cr_cpc_t
*cpc
;
1988 /* Compute PLLMR value from PSR settings */
1989 cpc
->pllmr
= 0x80000000;
1991 switch ((cpc
->psr
>> 30) & 3) {
1994 cpc
->pllmr
&= ~0x80000000;
1998 cpc
->pllmr
|= 5 << 16;
2002 cpc
->pllmr
|= 4 << 16;
2006 cpc
->pllmr
|= 2 << 16;
2010 D
= (cpc
->psr
>> 28) & 3;
2011 cpc
->pllmr
|= (D
+ 1) << 20;
2013 D
= (cpc
->psr
>> 25) & 7;
2028 D
= (cpc
->psr
>> 23) & 3;
2029 cpc
->pllmr
|= D
<< 26;
2031 D
= (cpc
->psr
>> 21) & 3;
2032 cpc
->pllmr
|= D
<< 10;
2034 D
= (cpc
->psr
>> 17) & 3;
2035 cpc
->pllmr
|= D
<< 24;
2036 cpc
->cr0
= 0x0000003C;
2037 cpc
->cr1
= 0x2B0D8800;
2038 cpc
->er
= 0x00000000;
2039 cpc
->fr
= 0x00000000;
2040 ppc405cr_clk_setup(cpc
);
2043 static void ppc405cr_clk_init (ppc405cr_cpc_t
*cpc
)
2047 /* XXX: this should be read from IO pins */
2048 cpc
->psr
= 0x00000000; /* 8 bits ROM */
2050 D
= 0x2; /* Divide by 4 */
2051 cpc
->psr
|= D
<< 30;
2053 D
= 0x1; /* Divide by 2 */
2054 cpc
->psr
|= D
<< 28;
2056 D
= 0x1; /* Divide by 2 */
2057 cpc
->psr
|= D
<< 23;
2059 D
= 0x5; /* M = 16 */
2060 cpc
->psr
|= D
<< 25;
2062 D
= 0x1; /* Divide by 2 */
2063 cpc
->psr
|= D
<< 21;
2065 D
= 0x2; /* Divide by 4 */
2066 cpc
->psr
|= D
<< 17;
2069 static void ppc405cr_cpc_init (CPUPPCState
*env
, clk_setup_t clk_setup
[7],
2072 ppc405cr_cpc_t
*cpc
;
2074 cpc
= g_malloc0(sizeof(ppc405cr_cpc_t
));
2075 memcpy(cpc
->clk_setup
, clk_setup
,
2076 PPC405CR_CLK_NB
* sizeof(clk_setup_t
));
2077 cpc
->sysclk
= sysclk
;
2078 cpc
->jtagid
= 0x42051049;
2079 ppc_dcr_register(env
, PPC405CR_CPC0_PSR
, cpc
,
2080 &dcr_read_crcpc
, &dcr_write_crcpc
);
2081 ppc_dcr_register(env
, PPC405CR_CPC0_CR0
, cpc
,
2082 &dcr_read_crcpc
, &dcr_write_crcpc
);
2083 ppc_dcr_register(env
, PPC405CR_CPC0_CR1
, cpc
,
2084 &dcr_read_crcpc
, &dcr_write_crcpc
);
2085 ppc_dcr_register(env
, PPC405CR_CPC0_JTAGID
, cpc
,
2086 &dcr_read_crcpc
, &dcr_write_crcpc
);
2087 ppc_dcr_register(env
, PPC405CR_CPC0_PLLMR
, cpc
,
2088 &dcr_read_crcpc
, &dcr_write_crcpc
);
2089 ppc_dcr_register(env
, PPC405CR_CPC0_ER
, cpc
,
2090 &dcr_read_crcpc
, &dcr_write_crcpc
);
2091 ppc_dcr_register(env
, PPC405CR_CPC0_FR
, cpc
,
2092 &dcr_read_crcpc
, &dcr_write_crcpc
);
2093 ppc_dcr_register(env
, PPC405CR_CPC0_SR
, cpc
,
2094 &dcr_read_crcpc
, &dcr_write_crcpc
);
2095 ppc405cr_clk_init(cpc
);
2096 qemu_register_reset(ppc405cr_cpc_reset
, cpc
);
2099 CPUPPCState
*ppc405cr_init(MemoryRegion
*address_space_mem
,
2100 MemoryRegion ram_memories
[4],
2101 target_phys_addr_t ram_bases
[4],
2102 target_phys_addr_t ram_sizes
[4],
2103 uint32_t sysclk
, qemu_irq
**picp
,
2106 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
2107 qemu_irq dma_irqs
[4];
2109 qemu_irq
*pic
, *irqs
;
2111 memset(clk_setup
, 0, sizeof(clk_setup
));
2112 env
= ppc4xx_init("405cr", &clk_setup
[PPC405CR_CPU_CLK
],
2113 &clk_setup
[PPC405CR_TMR_CLK
], sysclk
);
2114 /* Memory mapped devices registers */
2116 ppc4xx_plb_init(env
);
2117 /* PLB to OPB bridge */
2118 ppc4xx_pob_init(env
);
2120 ppc4xx_opba_init(0xef600600);
2121 /* Universal interrupt controller */
2122 irqs
= g_malloc0(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2123 irqs
[PPCUIC_OUTPUT_INT
] =
2124 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2125 irqs
[PPCUIC_OUTPUT_CINT
] =
2126 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2127 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2129 /* SDRAM controller */
2130 ppc4xx_sdram_init(env
, pic
[14], 1, ram_memories
,
2131 ram_bases
, ram_sizes
, do_init
);
2132 /* External bus controller */
2133 ppc405_ebc_init(env
);
2134 /* DMA controller */
2135 dma_irqs
[0] = pic
[26];
2136 dma_irqs
[1] = pic
[25];
2137 dma_irqs
[2] = pic
[24];
2138 dma_irqs
[3] = pic
[23];
2139 ppc405_dma_init(env
, dma_irqs
);
2141 if (serial_hds
[0] != NULL
) {
2142 serial_mm_init(address_space_mem
, 0xef600300, 0, pic
[0],
2143 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[0],
2146 if (serial_hds
[1] != NULL
) {
2147 serial_mm_init(address_space_mem
, 0xef600400, 0, pic
[1],
2148 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[1],
2151 /* IIC controller */
2152 ppc405_i2c_init(0xef600500, pic
[2]);
2154 ppc405_gpio_init(0xef600700);
2156 ppc405cr_cpc_init(env
, clk_setup
, sysclk
);
2161 /*****************************************************************************/
2165 PPC405EP_CPC0_PLLMR0
= 0x0F0,
2166 PPC405EP_CPC0_BOOT
= 0x0F1,
2167 PPC405EP_CPC0_EPCTL
= 0x0F3,
2168 PPC405EP_CPC0_PLLMR1
= 0x0F4,
2169 PPC405EP_CPC0_UCR
= 0x0F5,
2170 PPC405EP_CPC0_SRR
= 0x0F6,
2171 PPC405EP_CPC0_JTAGID
= 0x0F7,
2172 PPC405EP_CPC0_PCI
= 0x0F9,
2174 PPC405EP_CPC0_ER
= xxx
,
2175 PPC405EP_CPC0_FR
= xxx
,
2176 PPC405EP_CPC0_SR
= xxx
,
2181 PPC405EP_CPU_CLK
= 0,
2182 PPC405EP_PLB_CLK
= 1,
2183 PPC405EP_OPB_CLK
= 2,
2184 PPC405EP_EBC_CLK
= 3,
2185 PPC405EP_MAL_CLK
= 4,
2186 PPC405EP_PCI_CLK
= 5,
2187 PPC405EP_UART0_CLK
= 6,
2188 PPC405EP_UART1_CLK
= 7,
2189 PPC405EP_CLK_NB
= 8,
2192 typedef struct ppc405ep_cpc_t ppc405ep_cpc_t
;
2193 struct ppc405ep_cpc_t
{
2195 clk_setup_t clk_setup
[PPC405EP_CLK_NB
];
2203 /* Clock and power management */
2209 static void ppc405ep_compute_clocks (ppc405ep_cpc_t
*cpc
)
2211 uint32_t CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
;
2212 uint32_t UART0_clk
, UART1_clk
;
2213 uint64_t VCO_out
, PLL_out
;
2217 if ((cpc
->pllmr
[1] & 0x80000000) && !(cpc
->pllmr
[1] & 0x40000000)) {
2218 M
= (((cpc
->pllmr
[1] >> 20) - 1) & 0xF) + 1; /* FBMUL */
2219 #ifdef DEBUG_CLOCKS_LL
2220 printf("FBMUL %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 20) & 0xF, M
);
2222 D
= 8 - ((cpc
->pllmr
[1] >> 16) & 0x7); /* FWDA */
2223 #ifdef DEBUG_CLOCKS_LL
2224 printf("FWDA %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 16) & 0x7, D
);
2226 VCO_out
= cpc
->sysclk
* M
* D
;
2227 if (VCO_out
< 500000000UL || VCO_out
> 1000000000UL) {
2228 /* Error - unlock the PLL */
2229 printf("VCO out of range %" PRIu64
"\n", VCO_out
);
2231 cpc
->pllmr
[1] &= ~0x80000000;
2235 PLL_out
= VCO_out
/ D
;
2236 /* Pretend the PLL is locked */
2237 cpc
->boot
|= 0x00000001;
2242 PLL_out
= cpc
->sysclk
;
2243 if (cpc
->pllmr
[1] & 0x40000000) {
2244 /* Pretend the PLL is not locked */
2245 cpc
->boot
&= ~0x00000001;
2248 /* Now, compute all other clocks */
2249 D
= ((cpc
->pllmr
[0] >> 20) & 0x3) + 1; /* CCDV */
2250 #ifdef DEBUG_CLOCKS_LL
2251 printf("CCDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 20) & 0x3, D
);
2253 CPU_clk
= PLL_out
/ D
;
2254 D
= ((cpc
->pllmr
[0] >> 16) & 0x3) + 1; /* CBDV */
2255 #ifdef DEBUG_CLOCKS_LL
2256 printf("CBDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 16) & 0x3, D
);
2258 PLB_clk
= CPU_clk
/ D
;
2259 D
= ((cpc
->pllmr
[0] >> 12) & 0x3) + 1; /* OPDV */
2260 #ifdef DEBUG_CLOCKS_LL
2261 printf("OPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 12) & 0x3, D
);
2263 OPB_clk
= PLB_clk
/ D
;
2264 D
= ((cpc
->pllmr
[0] >> 8) & 0x3) + 2; /* EPDV */
2265 #ifdef DEBUG_CLOCKS_LL
2266 printf("EPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 8) & 0x3, D
);
2268 EBC_clk
= PLB_clk
/ D
;
2269 D
= ((cpc
->pllmr
[0] >> 4) & 0x3) + 1; /* MPDV */
2270 #ifdef DEBUG_CLOCKS_LL
2271 printf("MPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 4) & 0x3, D
);
2273 MAL_clk
= PLB_clk
/ D
;
2274 D
= (cpc
->pllmr
[0] & 0x3) + 1; /* PPDV */
2275 #ifdef DEBUG_CLOCKS_LL
2276 printf("PPDV %01" PRIx32
" %d\n", cpc
->pllmr
[0] & 0x3, D
);
2278 PCI_clk
= PLB_clk
/ D
;
2279 D
= ((cpc
->ucr
- 1) & 0x7F) + 1; /* U0DIV */
2280 #ifdef DEBUG_CLOCKS_LL
2281 printf("U0DIV %01" PRIx32
" %d\n", cpc
->ucr
& 0x7F, D
);
2283 UART0_clk
= PLL_out
/ D
;
2284 D
= (((cpc
->ucr
>> 8) - 1) & 0x7F) + 1; /* U1DIV */
2285 #ifdef DEBUG_CLOCKS_LL
2286 printf("U1DIV %01" PRIx32
" %d\n", (cpc
->ucr
>> 8) & 0x7F, D
);
2288 UART1_clk
= PLL_out
/ D
;
2290 printf("Setup PPC405EP clocks - sysclk %" PRIu32
" VCO %" PRIu64
2291 " PLL out %" PRIu64
" Hz\n", cpc
->sysclk
, VCO_out
, PLL_out
);
2292 printf("CPU %" PRIu32
" PLB %" PRIu32
" OPB %" PRIu32
" EBC %" PRIu32
2293 " MAL %" PRIu32
" PCI %" PRIu32
" UART0 %" PRIu32
2294 " UART1 %" PRIu32
"\n",
2295 CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
,
2296 UART0_clk
, UART1_clk
);
2298 /* Setup CPU clocks */
2299 clk_setup(&cpc
->clk_setup
[PPC405EP_CPU_CLK
], CPU_clk
);
2300 /* Setup PLB clock */
2301 clk_setup(&cpc
->clk_setup
[PPC405EP_PLB_CLK
], PLB_clk
);
2302 /* Setup OPB clock */
2303 clk_setup(&cpc
->clk_setup
[PPC405EP_OPB_CLK
], OPB_clk
);
2304 /* Setup external clock */
2305 clk_setup(&cpc
->clk_setup
[PPC405EP_EBC_CLK
], EBC_clk
);
2306 /* Setup MAL clock */
2307 clk_setup(&cpc
->clk_setup
[PPC405EP_MAL_CLK
], MAL_clk
);
2308 /* Setup PCI clock */
2309 clk_setup(&cpc
->clk_setup
[PPC405EP_PCI_CLK
], PCI_clk
);
2310 /* Setup UART0 clock */
2311 clk_setup(&cpc
->clk_setup
[PPC405EP_UART0_CLK
], UART0_clk
);
2312 /* Setup UART1 clock */
2313 clk_setup(&cpc
->clk_setup
[PPC405EP_UART1_CLK
], UART1_clk
);
2316 static uint32_t dcr_read_epcpc (void *opaque
, int dcrn
)
2318 ppc405ep_cpc_t
*cpc
;
2323 case PPC405EP_CPC0_BOOT
:
2326 case PPC405EP_CPC0_EPCTL
:
2329 case PPC405EP_CPC0_PLLMR0
:
2330 ret
= cpc
->pllmr
[0];
2332 case PPC405EP_CPC0_PLLMR1
:
2333 ret
= cpc
->pllmr
[1];
2335 case PPC405EP_CPC0_UCR
:
2338 case PPC405EP_CPC0_SRR
:
2341 case PPC405EP_CPC0_JTAGID
:
2344 case PPC405EP_CPC0_PCI
:
2348 /* Avoid gcc warning */
2356 static void dcr_write_epcpc (void *opaque
, int dcrn
, uint32_t val
)
2358 ppc405ep_cpc_t
*cpc
;
2362 case PPC405EP_CPC0_BOOT
:
2363 /* Read-only register */
2365 case PPC405EP_CPC0_EPCTL
:
2366 /* Don't care for now */
2367 cpc
->epctl
= val
& 0xC00000F3;
2369 case PPC405EP_CPC0_PLLMR0
:
2370 cpc
->pllmr
[0] = val
& 0x00633333;
2371 ppc405ep_compute_clocks(cpc
);
2373 case PPC405EP_CPC0_PLLMR1
:
2374 cpc
->pllmr
[1] = val
& 0xC0F73FFF;
2375 ppc405ep_compute_clocks(cpc
);
2377 case PPC405EP_CPC0_UCR
:
2378 /* UART control - don't care for now */
2379 cpc
->ucr
= val
& 0x003F7F7F;
2381 case PPC405EP_CPC0_SRR
:
2384 case PPC405EP_CPC0_JTAGID
:
2387 case PPC405EP_CPC0_PCI
:
2393 static void ppc405ep_cpc_reset (void *opaque
)
2395 ppc405ep_cpc_t
*cpc
= opaque
;
2397 cpc
->boot
= 0x00000010; /* Boot from PCI - IIC EEPROM disabled */
2398 cpc
->epctl
= 0x00000000;
2399 cpc
->pllmr
[0] = 0x00011010;
2400 cpc
->pllmr
[1] = 0x40000000;
2401 cpc
->ucr
= 0x00000000;
2402 cpc
->srr
= 0x00040000;
2403 cpc
->pci
= 0x00000000;
2404 cpc
->er
= 0x00000000;
2405 cpc
->fr
= 0x00000000;
2406 cpc
->sr
= 0x00000000;
2407 ppc405ep_compute_clocks(cpc
);
2410 /* XXX: sysclk should be between 25 and 100 MHz */
2411 static void ppc405ep_cpc_init (CPUPPCState
*env
, clk_setup_t clk_setup
[8],
2414 ppc405ep_cpc_t
*cpc
;
2416 cpc
= g_malloc0(sizeof(ppc405ep_cpc_t
));
2417 memcpy(cpc
->clk_setup
, clk_setup
,
2418 PPC405EP_CLK_NB
* sizeof(clk_setup_t
));
2419 cpc
->jtagid
= 0x20267049;
2420 cpc
->sysclk
= sysclk
;
2421 qemu_register_reset(&ppc405ep_cpc_reset
, cpc
);
2422 ppc_dcr_register(env
, PPC405EP_CPC0_BOOT
, cpc
,
2423 &dcr_read_epcpc
, &dcr_write_epcpc
);
2424 ppc_dcr_register(env
, PPC405EP_CPC0_EPCTL
, cpc
,
2425 &dcr_read_epcpc
, &dcr_write_epcpc
);
2426 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR0
, cpc
,
2427 &dcr_read_epcpc
, &dcr_write_epcpc
);
2428 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR1
, cpc
,
2429 &dcr_read_epcpc
, &dcr_write_epcpc
);
2430 ppc_dcr_register(env
, PPC405EP_CPC0_UCR
, cpc
,
2431 &dcr_read_epcpc
, &dcr_write_epcpc
);
2432 ppc_dcr_register(env
, PPC405EP_CPC0_SRR
, cpc
,
2433 &dcr_read_epcpc
, &dcr_write_epcpc
);
2434 ppc_dcr_register(env
, PPC405EP_CPC0_JTAGID
, cpc
,
2435 &dcr_read_epcpc
, &dcr_write_epcpc
);
2436 ppc_dcr_register(env
, PPC405EP_CPC0_PCI
, cpc
,
2437 &dcr_read_epcpc
, &dcr_write_epcpc
);
2439 ppc_dcr_register(env
, PPC405EP_CPC0_ER
, cpc
,
2440 &dcr_read_epcpc
, &dcr_write_epcpc
);
2441 ppc_dcr_register(env
, PPC405EP_CPC0_FR
, cpc
,
2442 &dcr_read_epcpc
, &dcr_write_epcpc
);
2443 ppc_dcr_register(env
, PPC405EP_CPC0_SR
, cpc
,
2444 &dcr_read_epcpc
, &dcr_write_epcpc
);
2448 CPUPPCState
*ppc405ep_init(MemoryRegion
*address_space_mem
,
2449 MemoryRegion ram_memories
[2],
2450 target_phys_addr_t ram_bases
[2],
2451 target_phys_addr_t ram_sizes
[2],
2452 uint32_t sysclk
, qemu_irq
**picp
,
2455 clk_setup_t clk_setup
[PPC405EP_CLK_NB
], tlb_clk_setup
;
2456 qemu_irq dma_irqs
[4], gpt_irqs
[5], mal_irqs
[4];
2458 qemu_irq
*pic
, *irqs
;
2460 memset(clk_setup
, 0, sizeof(clk_setup
));
2462 env
= ppc4xx_init("405ep", &clk_setup
[PPC405EP_CPU_CLK
],
2463 &tlb_clk_setup
, sysclk
);
2464 clk_setup
[PPC405EP_CPU_CLK
].cb
= tlb_clk_setup
.cb
;
2465 clk_setup
[PPC405EP_CPU_CLK
].opaque
= tlb_clk_setup
.opaque
;
2466 /* Internal devices init */
2467 /* Memory mapped devices registers */
2469 ppc4xx_plb_init(env
);
2470 /* PLB to OPB bridge */
2471 ppc4xx_pob_init(env
);
2473 ppc4xx_opba_init(0xef600600);
2474 /* Initialize timers */
2475 ppc_booke_timers_init(env
, sysclk
, 0);
2476 /* Universal interrupt controller */
2477 irqs
= g_malloc0(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2478 irqs
[PPCUIC_OUTPUT_INT
] =
2479 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2480 irqs
[PPCUIC_OUTPUT_CINT
] =
2481 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2482 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2484 /* SDRAM controller */
2485 /* XXX 405EP has no ECC interrupt */
2486 ppc4xx_sdram_init(env
, pic
[17], 2, ram_memories
,
2487 ram_bases
, ram_sizes
, do_init
);
2488 /* External bus controller */
2489 ppc405_ebc_init(env
);
2490 /* DMA controller */
2491 dma_irqs
[0] = pic
[5];
2492 dma_irqs
[1] = pic
[6];
2493 dma_irqs
[2] = pic
[7];
2494 dma_irqs
[3] = pic
[8];
2495 ppc405_dma_init(env
, dma_irqs
);
2496 /* IIC controller */
2497 ppc405_i2c_init(0xef600500, pic
[2]);
2499 ppc405_gpio_init(0xef600700);
2501 if (serial_hds
[0] != NULL
) {
2502 serial_mm_init(address_space_mem
, 0xef600300, 0, pic
[0],
2503 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[0],
2506 if (serial_hds
[1] != NULL
) {
2507 serial_mm_init(address_space_mem
, 0xef600400, 0, pic
[1],
2508 PPC_SERIAL_MM_BAUDBASE
, serial_hds
[1],
2512 ppc405_ocm_init(env
);
2514 gpt_irqs
[0] = pic
[19];
2515 gpt_irqs
[1] = pic
[20];
2516 gpt_irqs
[2] = pic
[21];
2517 gpt_irqs
[3] = pic
[22];
2518 gpt_irqs
[4] = pic
[23];
2519 ppc4xx_gpt_init(0xef600000, gpt_irqs
);
2521 /* Uses pic[3], pic[16], pic[18] */
2523 mal_irqs
[0] = pic
[11];
2524 mal_irqs
[1] = pic
[12];
2525 mal_irqs
[2] = pic
[13];
2526 mal_irqs
[3] = pic
[14];
2527 ppc405_mal_init(env
, mal_irqs
);
2529 /* Uses pic[9], pic[15], pic[17] */
2531 ppc405ep_cpc_init(env
, clk_setup
, sysclk
);