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"
41 //#define DEBUG_CLOCKS_LL
43 ram_addr_t
ppc405_set_bootinfo (CPUState
*env
, ppc4xx_bd_info_t
*bd
,
49 /* We put the bd structure at the top of memory */
50 if (bd
->bi_memsize
>= 0x01000000UL
)
51 bdloc
= 0x01000000UL
- sizeof(struct ppc4xx_bd_info_t
);
53 bdloc
= bd
->bi_memsize
- sizeof(struct ppc4xx_bd_info_t
);
54 stl_phys(bdloc
+ 0x00, bd
->bi_memstart
);
55 stl_phys(bdloc
+ 0x04, bd
->bi_memsize
);
56 stl_phys(bdloc
+ 0x08, bd
->bi_flashstart
);
57 stl_phys(bdloc
+ 0x0C, bd
->bi_flashsize
);
58 stl_phys(bdloc
+ 0x10, bd
->bi_flashoffset
);
59 stl_phys(bdloc
+ 0x14, bd
->bi_sramstart
);
60 stl_phys(bdloc
+ 0x18, bd
->bi_sramsize
);
61 stl_phys(bdloc
+ 0x1C, bd
->bi_bootflags
);
62 stl_phys(bdloc
+ 0x20, bd
->bi_ipaddr
);
63 for (i
= 0; i
< 6; i
++)
64 stb_phys(bdloc
+ 0x24 + i
, bd
->bi_enetaddr
[i
]);
65 stw_phys(bdloc
+ 0x2A, bd
->bi_ethspeed
);
66 stl_phys(bdloc
+ 0x2C, bd
->bi_intfreq
);
67 stl_phys(bdloc
+ 0x30, bd
->bi_busfreq
);
68 stl_phys(bdloc
+ 0x34, bd
->bi_baudrate
);
69 for (i
= 0; i
< 4; i
++)
70 stb_phys(bdloc
+ 0x38 + i
, bd
->bi_s_version
[i
]);
71 for (i
= 0; i
< 32; i
++)
72 stb_phys(bdloc
+ 0x3C + i
, bd
->bi_s_version
[i
]);
73 stl_phys(bdloc
+ 0x5C, bd
->bi_plb_busfreq
);
74 stl_phys(bdloc
+ 0x60, bd
->bi_pci_busfreq
);
75 for (i
= 0; i
< 6; i
++)
76 stb_phys(bdloc
+ 0x64 + i
, bd
->bi_pci_enetaddr
[i
]);
78 if (flags
& 0x00000001) {
79 for (i
= 0; i
< 6; i
++)
80 stb_phys(bdloc
+ n
++, bd
->bi_pci_enetaddr2
[i
]);
82 stl_phys(bdloc
+ n
, bd
->bi_opbfreq
);
84 for (i
= 0; i
< 2; i
++) {
85 stl_phys(bdloc
+ n
, bd
->bi_iic_fast
[i
]);
92 /*****************************************************************************/
93 /* Shared peripherals */
95 /*****************************************************************************/
96 /* Peripheral local bus arbitrer */
103 typedef struct ppc4xx_plb_t ppc4xx_plb_t
;
104 struct ppc4xx_plb_t
{
110 static uint32_t dcr_read_plb (void *opaque
, int dcrn
)
127 /* Avoid gcc warning */
135 static void dcr_write_plb (void *opaque
, int dcrn
, uint32_t val
)
142 /* We don't care about the actual parameters written as
143 * we don't manage any priorities on the bus
145 plb
->acr
= val
& 0xF8000000;
157 static void ppc4xx_plb_reset (void *opaque
)
162 plb
->acr
= 0x00000000;
163 plb
->bear
= 0x00000000;
164 plb
->besr
= 0x00000000;
167 static void ppc4xx_plb_init(CPUState
*env
)
171 plb
= qemu_mallocz(sizeof(ppc4xx_plb_t
));
172 ppc_dcr_register(env
, PLB0_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
173 ppc_dcr_register(env
, PLB0_BEAR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
174 ppc_dcr_register(env
, PLB0_BESR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
175 qemu_register_reset(ppc4xx_plb_reset
, plb
);
178 /*****************************************************************************/
179 /* PLB to OPB bridge */
186 typedef struct ppc4xx_pob_t ppc4xx_pob_t
;
187 struct ppc4xx_pob_t
{
192 static uint32_t dcr_read_pob (void *opaque
, int dcrn
)
204 ret
= pob
->besr
[dcrn
- POB0_BESR0
];
207 /* Avoid gcc warning */
215 static void dcr_write_pob (void *opaque
, int dcrn
, uint32_t val
)
227 pob
->besr
[dcrn
- POB0_BESR0
] &= ~val
;
232 static void ppc4xx_pob_reset (void *opaque
)
238 pob
->bear
= 0x00000000;
239 pob
->besr
[0] = 0x0000000;
240 pob
->besr
[1] = 0x0000000;
243 static void ppc4xx_pob_init(CPUState
*env
)
247 pob
= qemu_mallocz(sizeof(ppc4xx_pob_t
));
248 ppc_dcr_register(env
, POB0_BEAR
, pob
, &dcr_read_pob
, &dcr_write_pob
);
249 ppc_dcr_register(env
, POB0_BESR0
, pob
, &dcr_read_pob
, &dcr_write_pob
);
250 ppc_dcr_register(env
, POB0_BESR1
, pob
, &dcr_read_pob
, &dcr_write_pob
);
251 qemu_register_reset(ppc4xx_pob_reset
, pob
);
254 /*****************************************************************************/
256 typedef struct ppc4xx_opba_t ppc4xx_opba_t
;
257 struct ppc4xx_opba_t
{
262 static uint32_t opba_readb (void *opaque
, target_phys_addr_t addr
)
268 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
286 static void opba_writeb (void *opaque
,
287 target_phys_addr_t addr
, uint32_t value
)
292 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
298 opba
->cr
= value
& 0xF8;
301 opba
->pr
= value
& 0xFF;
308 static uint32_t opba_readw (void *opaque
, target_phys_addr_t addr
)
313 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
315 ret
= opba_readb(opaque
, addr
) << 8;
316 ret
|= opba_readb(opaque
, addr
+ 1);
321 static void opba_writew (void *opaque
,
322 target_phys_addr_t addr
, uint32_t value
)
325 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
328 opba_writeb(opaque
, addr
, value
>> 8);
329 opba_writeb(opaque
, addr
+ 1, value
);
332 static uint32_t opba_readl (void *opaque
, target_phys_addr_t addr
)
337 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
339 ret
= opba_readb(opaque
, addr
) << 24;
340 ret
|= opba_readb(opaque
, addr
+ 1) << 16;
345 static void opba_writel (void *opaque
,
346 target_phys_addr_t addr
, uint32_t value
)
349 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
352 opba_writeb(opaque
, addr
, value
>> 24);
353 opba_writeb(opaque
, addr
+ 1, value
>> 16);
356 static CPUReadMemoryFunc
* const opba_read
[] = {
362 static CPUWriteMemoryFunc
* const opba_write
[] = {
368 static void ppc4xx_opba_reset (void *opaque
)
373 opba
->cr
= 0x00; /* No dynamic priorities - park disabled */
377 static void ppc4xx_opba_init(target_phys_addr_t base
)
382 opba
= qemu_mallocz(sizeof(ppc4xx_opba_t
));
384 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
386 io
= cpu_register_io_memory(opba_read
, opba_write
, opba
);
387 cpu_register_physical_memory(base
, 0x002, io
);
388 qemu_register_reset(ppc4xx_opba_reset
, opba
);
391 /*****************************************************************************/
392 /* Code decompression controller */
395 /*****************************************************************************/
396 /* Peripheral controller */
397 typedef struct ppc4xx_ebc_t ppc4xx_ebc_t
;
398 struct ppc4xx_ebc_t
{
409 EBC0_CFGADDR
= 0x012,
410 EBC0_CFGDATA
= 0x013,
413 static uint32_t dcr_read_ebc (void *opaque
, int dcrn
)
425 case 0x00: /* B0CR */
428 case 0x01: /* B1CR */
431 case 0x02: /* B2CR */
434 case 0x03: /* B3CR */
437 case 0x04: /* B4CR */
440 case 0x05: /* B5CR */
443 case 0x06: /* B6CR */
446 case 0x07: /* B7CR */
449 case 0x10: /* B0AP */
452 case 0x11: /* B1AP */
455 case 0x12: /* B2AP */
458 case 0x13: /* B3AP */
461 case 0x14: /* B4AP */
464 case 0x15: /* B5AP */
467 case 0x16: /* B6AP */
470 case 0x17: /* B7AP */
473 case 0x20: /* BEAR */
476 case 0x21: /* BESR0 */
479 case 0x22: /* BESR1 */
498 static void dcr_write_ebc (void *opaque
, int dcrn
, uint32_t val
)
509 case 0x00: /* B0CR */
511 case 0x01: /* B1CR */
513 case 0x02: /* B2CR */
515 case 0x03: /* B3CR */
517 case 0x04: /* B4CR */
519 case 0x05: /* B5CR */
521 case 0x06: /* B6CR */
523 case 0x07: /* B7CR */
525 case 0x10: /* B0AP */
527 case 0x11: /* B1AP */
529 case 0x12: /* B2AP */
531 case 0x13: /* B3AP */
533 case 0x14: /* B4AP */
535 case 0x15: /* B5AP */
537 case 0x16: /* B6AP */
539 case 0x17: /* B7AP */
541 case 0x20: /* BEAR */
543 case 0x21: /* BESR0 */
545 case 0x22: /* BESR1 */
558 static void ebc_reset (void *opaque
)
564 ebc
->addr
= 0x00000000;
565 ebc
->bap
[0] = 0x7F8FFE80;
566 ebc
->bcr
[0] = 0xFFE28000;
567 for (i
= 0; i
< 8; i
++) {
568 ebc
->bap
[i
] = 0x00000000;
569 ebc
->bcr
[i
] = 0x00000000;
571 ebc
->besr0
= 0x00000000;
572 ebc
->besr1
= 0x00000000;
573 ebc
->cfg
= 0x80400000;
576 static void ppc405_ebc_init(CPUState
*env
)
580 ebc
= qemu_mallocz(sizeof(ppc4xx_ebc_t
));
581 qemu_register_reset(&ebc_reset
, ebc
);
582 ppc_dcr_register(env
, EBC0_CFGADDR
,
583 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
584 ppc_dcr_register(env
, EBC0_CFGDATA
,
585 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
588 /*****************************************************************************/
617 typedef struct ppc405_dma_t ppc405_dma_t
;
618 struct ppc405_dma_t
{
631 static uint32_t dcr_read_dma (void *opaque
, int dcrn
)
636 static void dcr_write_dma (void *opaque
, int dcrn
, uint32_t val
)
640 static void ppc405_dma_reset (void *opaque
)
646 for (i
= 0; i
< 4; i
++) {
647 dma
->cr
[i
] = 0x00000000;
648 dma
->ct
[i
] = 0x00000000;
649 dma
->da
[i
] = 0x00000000;
650 dma
->sa
[i
] = 0x00000000;
651 dma
->sg
[i
] = 0x00000000;
653 dma
->sr
= 0x00000000;
654 dma
->sgc
= 0x00000000;
655 dma
->slp
= 0x7C000000;
656 dma
->pol
= 0x00000000;
659 static void ppc405_dma_init(CPUState
*env
, qemu_irq irqs
[4])
663 dma
= qemu_mallocz(sizeof(ppc405_dma_t
));
664 memcpy(dma
->irqs
, irqs
, 4 * sizeof(qemu_irq
));
665 qemu_register_reset(&ppc405_dma_reset
, dma
);
666 ppc_dcr_register(env
, DMA0_CR0
,
667 dma
, &dcr_read_dma
, &dcr_write_dma
);
668 ppc_dcr_register(env
, DMA0_CT0
,
669 dma
, &dcr_read_dma
, &dcr_write_dma
);
670 ppc_dcr_register(env
, DMA0_DA0
,
671 dma
, &dcr_read_dma
, &dcr_write_dma
);
672 ppc_dcr_register(env
, DMA0_SA0
,
673 dma
, &dcr_read_dma
, &dcr_write_dma
);
674 ppc_dcr_register(env
, DMA0_SG0
,
675 dma
, &dcr_read_dma
, &dcr_write_dma
);
676 ppc_dcr_register(env
, DMA0_CR1
,
677 dma
, &dcr_read_dma
, &dcr_write_dma
);
678 ppc_dcr_register(env
, DMA0_CT1
,
679 dma
, &dcr_read_dma
, &dcr_write_dma
);
680 ppc_dcr_register(env
, DMA0_DA1
,
681 dma
, &dcr_read_dma
, &dcr_write_dma
);
682 ppc_dcr_register(env
, DMA0_SA1
,
683 dma
, &dcr_read_dma
, &dcr_write_dma
);
684 ppc_dcr_register(env
, DMA0_SG1
,
685 dma
, &dcr_read_dma
, &dcr_write_dma
);
686 ppc_dcr_register(env
, DMA0_CR2
,
687 dma
, &dcr_read_dma
, &dcr_write_dma
);
688 ppc_dcr_register(env
, DMA0_CT2
,
689 dma
, &dcr_read_dma
, &dcr_write_dma
);
690 ppc_dcr_register(env
, DMA0_DA2
,
691 dma
, &dcr_read_dma
, &dcr_write_dma
);
692 ppc_dcr_register(env
, DMA0_SA2
,
693 dma
, &dcr_read_dma
, &dcr_write_dma
);
694 ppc_dcr_register(env
, DMA0_SG2
,
695 dma
, &dcr_read_dma
, &dcr_write_dma
);
696 ppc_dcr_register(env
, DMA0_CR3
,
697 dma
, &dcr_read_dma
, &dcr_write_dma
);
698 ppc_dcr_register(env
, DMA0_CT3
,
699 dma
, &dcr_read_dma
, &dcr_write_dma
);
700 ppc_dcr_register(env
, DMA0_DA3
,
701 dma
, &dcr_read_dma
, &dcr_write_dma
);
702 ppc_dcr_register(env
, DMA0_SA3
,
703 dma
, &dcr_read_dma
, &dcr_write_dma
);
704 ppc_dcr_register(env
, DMA0_SG3
,
705 dma
, &dcr_read_dma
, &dcr_write_dma
);
706 ppc_dcr_register(env
, DMA0_SR
,
707 dma
, &dcr_read_dma
, &dcr_write_dma
);
708 ppc_dcr_register(env
, DMA0_SGC
,
709 dma
, &dcr_read_dma
, &dcr_write_dma
);
710 ppc_dcr_register(env
, DMA0_SLP
,
711 dma
, &dcr_read_dma
, &dcr_write_dma
);
712 ppc_dcr_register(env
, DMA0_POL
,
713 dma
, &dcr_read_dma
, &dcr_write_dma
);
716 /*****************************************************************************/
718 typedef struct ppc405_gpio_t ppc405_gpio_t
;
719 struct ppc405_gpio_t
{
733 static uint32_t ppc405_gpio_readb (void *opaque
, target_phys_addr_t addr
)
736 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
742 static void ppc405_gpio_writeb (void *opaque
,
743 target_phys_addr_t addr
, uint32_t value
)
746 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
751 static uint32_t ppc405_gpio_readw (void *opaque
, target_phys_addr_t addr
)
754 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
760 static void ppc405_gpio_writew (void *opaque
,
761 target_phys_addr_t addr
, uint32_t value
)
764 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
769 static uint32_t ppc405_gpio_readl (void *opaque
, target_phys_addr_t addr
)
772 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
778 static void ppc405_gpio_writel (void *opaque
,
779 target_phys_addr_t addr
, uint32_t value
)
782 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
787 static CPUReadMemoryFunc
* const ppc405_gpio_read
[] = {
793 static CPUWriteMemoryFunc
* const ppc405_gpio_write
[] = {
799 static void ppc405_gpio_reset (void *opaque
)
803 static void ppc405_gpio_init(target_phys_addr_t base
)
808 gpio
= qemu_mallocz(sizeof(ppc405_gpio_t
));
810 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
812 io
= cpu_register_io_memory(ppc405_gpio_read
, ppc405_gpio_write
, gpio
);
813 cpu_register_physical_memory(base
, 0x038, io
);
814 qemu_register_reset(&ppc405_gpio_reset
, gpio
);
817 /*****************************************************************************/
821 OCM0_ISACNTL
= 0x019,
823 OCM0_DSACNTL
= 0x01B,
826 typedef struct ppc405_ocm_t ppc405_ocm_t
;
827 struct ppc405_ocm_t
{
835 static void ocm_update_mappings (ppc405_ocm_t
*ocm
,
836 uint32_t isarc
, uint32_t isacntl
,
837 uint32_t dsarc
, uint32_t dsacntl
)
840 printf("OCM update ISA %08" PRIx32
" %08" PRIx32
" (%08" PRIx32
841 " %08" PRIx32
") DSA %08" PRIx32
" %08" PRIx32
842 " (%08" PRIx32
" %08" PRIx32
")\n",
843 isarc
, isacntl
, dsarc
, dsacntl
,
844 ocm
->isarc
, ocm
->isacntl
, ocm
->dsarc
, ocm
->dsacntl
);
846 if (ocm
->isarc
!= isarc
||
847 (ocm
->isacntl
& 0x80000000) != (isacntl
& 0x80000000)) {
848 if (ocm
->isacntl
& 0x80000000) {
849 /* Unmap previously assigned memory region */
850 printf("OCM unmap ISA %08" PRIx32
"\n", ocm
->isarc
);
851 cpu_register_physical_memory(ocm
->isarc
, 0x04000000,
854 if (isacntl
& 0x80000000) {
855 /* Map new instruction memory region */
857 printf("OCM map ISA %08" PRIx32
"\n", isarc
);
859 cpu_register_physical_memory(isarc
, 0x04000000,
860 ocm
->offset
| IO_MEM_RAM
);
863 if (ocm
->dsarc
!= dsarc
||
864 (ocm
->dsacntl
& 0x80000000) != (dsacntl
& 0x80000000)) {
865 if (ocm
->dsacntl
& 0x80000000) {
866 /* Beware not to unmap the region we just mapped */
867 if (!(isacntl
& 0x80000000) || ocm
->dsarc
!= isarc
) {
868 /* Unmap previously assigned memory region */
870 printf("OCM unmap DSA %08" PRIx32
"\n", ocm
->dsarc
);
872 cpu_register_physical_memory(ocm
->dsarc
, 0x04000000,
876 if (dsacntl
& 0x80000000) {
877 /* Beware not to remap the region we just mapped */
878 if (!(isacntl
& 0x80000000) || dsarc
!= isarc
) {
879 /* Map new data memory region */
881 printf("OCM map DSA %08" PRIx32
"\n", dsarc
);
883 cpu_register_physical_memory(dsarc
, 0x04000000,
884 ocm
->offset
| IO_MEM_RAM
);
890 static uint32_t dcr_read_ocm (void *opaque
, int dcrn
)
917 static void dcr_write_ocm (void *opaque
, int dcrn
, uint32_t val
)
920 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
925 isacntl
= ocm
->isacntl
;
926 dsacntl
= ocm
->dsacntl
;
929 isarc
= val
& 0xFC000000;
932 isacntl
= val
& 0xC0000000;
935 isarc
= val
& 0xFC000000;
938 isacntl
= val
& 0xC0000000;
941 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
944 ocm
->isacntl
= isacntl
;
945 ocm
->dsacntl
= dsacntl
;
948 static void ocm_reset (void *opaque
)
951 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
955 isacntl
= 0x00000000;
957 dsacntl
= 0x00000000;
958 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
961 ocm
->isacntl
= isacntl
;
962 ocm
->dsacntl
= dsacntl
;
965 static void ppc405_ocm_init(CPUState
*env
)
969 ocm
= qemu_mallocz(sizeof(ppc405_ocm_t
));
970 ocm
->offset
= qemu_ram_alloc(NULL
, "ppc405.ocm", 4096);
971 qemu_register_reset(&ocm_reset
, ocm
);
972 ppc_dcr_register(env
, OCM0_ISARC
,
973 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
974 ppc_dcr_register(env
, OCM0_ISACNTL
,
975 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
976 ppc_dcr_register(env
, OCM0_DSARC
,
977 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
978 ppc_dcr_register(env
, OCM0_DSACNTL
,
979 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
982 /*****************************************************************************/
984 typedef struct ppc4xx_i2c_t ppc4xx_i2c_t
;
985 struct ppc4xx_i2c_t
{
1004 static uint32_t ppc4xx_i2c_readb (void *opaque
, target_phys_addr_t addr
)
1010 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1015 // i2c_readbyte(&i2c->mdata);
1055 ret
= i2c
->xtcntlss
;
1058 ret
= i2c
->directcntl
;
1065 printf("%s: addr " TARGET_FMT_plx
" %02" PRIx32
"\n", __func__
, addr
, ret
);
1071 static void ppc4xx_i2c_writeb (void *opaque
,
1072 target_phys_addr_t addr
, uint32_t value
)
1077 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1084 // i2c_sendbyte(&i2c->mdata);
1099 i2c
->mdcntl
= value
& 0xDF;
1102 i2c
->sts
&= ~(value
& 0x0A);
1105 i2c
->extsts
&= ~(value
& 0x8F);
1114 i2c
->clkdiv
= value
;
1117 i2c
->intrmsk
= value
;
1120 i2c
->xfrcnt
= value
& 0x77;
1123 i2c
->xtcntlss
= value
;
1126 i2c
->directcntl
= value
& 0x7;
1131 static uint32_t ppc4xx_i2c_readw (void *opaque
, target_phys_addr_t addr
)
1136 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1138 ret
= ppc4xx_i2c_readb(opaque
, addr
) << 8;
1139 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 1);
1144 static void ppc4xx_i2c_writew (void *opaque
,
1145 target_phys_addr_t addr
, uint32_t value
)
1148 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1151 ppc4xx_i2c_writeb(opaque
, addr
, value
>> 8);
1152 ppc4xx_i2c_writeb(opaque
, addr
+ 1, value
);
1155 static uint32_t ppc4xx_i2c_readl (void *opaque
, target_phys_addr_t addr
)
1160 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1162 ret
= ppc4xx_i2c_readb(opaque
, addr
) << 24;
1163 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 1) << 16;
1164 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 2) << 8;
1165 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 3);
1170 static void ppc4xx_i2c_writel (void *opaque
,
1171 target_phys_addr_t addr
, uint32_t value
)
1174 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1177 ppc4xx_i2c_writeb(opaque
, addr
, value
>> 24);
1178 ppc4xx_i2c_writeb(opaque
, addr
+ 1, value
>> 16);
1179 ppc4xx_i2c_writeb(opaque
, addr
+ 2, value
>> 8);
1180 ppc4xx_i2c_writeb(opaque
, addr
+ 3, value
);
1183 static CPUReadMemoryFunc
* const i2c_read
[] = {
1189 static CPUWriteMemoryFunc
* const i2c_write
[] = {
1195 static void ppc4xx_i2c_reset (void *opaque
)
1208 i2c
->directcntl
= 0x0F;
1211 static void ppc405_i2c_init(target_phys_addr_t base
, qemu_irq irq
)
1216 i2c
= qemu_mallocz(sizeof(ppc4xx_i2c_t
));
1219 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1221 io
= cpu_register_io_memory(i2c_read
, i2c_write
, i2c
);
1222 cpu_register_physical_memory(base
, 0x011, io
);
1223 qemu_register_reset(ppc4xx_i2c_reset
, i2c
);
1226 /*****************************************************************************/
1227 /* General purpose timers */
1228 typedef struct ppc4xx_gpt_t ppc4xx_gpt_t
;
1229 struct ppc4xx_gpt_t
{
1232 struct QEMUTimer
*timer
;
1243 static uint32_t ppc4xx_gpt_readb (void *opaque
, target_phys_addr_t addr
)
1246 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1248 /* XXX: generate a bus fault */
1252 static void ppc4xx_gpt_writeb (void *opaque
,
1253 target_phys_addr_t addr
, uint32_t value
)
1256 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1259 /* XXX: generate a bus fault */
1262 static uint32_t ppc4xx_gpt_readw (void *opaque
, target_phys_addr_t addr
)
1265 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1267 /* XXX: generate a bus fault */
1271 static void ppc4xx_gpt_writew (void *opaque
,
1272 target_phys_addr_t addr
, uint32_t value
)
1275 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1278 /* XXX: generate a bus fault */
1281 static int ppc4xx_gpt_compare (ppc4xx_gpt_t
*gpt
, int n
)
1287 static void ppc4xx_gpt_set_output (ppc4xx_gpt_t
*gpt
, int n
, int level
)
1292 static void ppc4xx_gpt_set_outputs (ppc4xx_gpt_t
*gpt
)
1298 for (i
= 0; i
< 5; i
++) {
1299 if (gpt
->oe
& mask
) {
1300 /* Output is enabled */
1301 if (ppc4xx_gpt_compare(gpt
, i
)) {
1302 /* Comparison is OK */
1303 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
);
1305 /* Comparison is KO */
1306 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
? 0 : 1);
1313 static void ppc4xx_gpt_set_irqs (ppc4xx_gpt_t
*gpt
)
1319 for (i
= 0; i
< 5; i
++) {
1320 if (gpt
->is
& gpt
->im
& mask
)
1321 qemu_irq_raise(gpt
->irqs
[i
]);
1323 qemu_irq_lower(gpt
->irqs
[i
]);
1328 static void ppc4xx_gpt_compute_timer (ppc4xx_gpt_t
*gpt
)
1333 static uint32_t ppc4xx_gpt_readl (void *opaque
, target_phys_addr_t addr
)
1340 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1345 /* Time base counter */
1346 ret
= muldiv64(qemu_get_clock(vm_clock
) + gpt
->tb_offset
,
1347 gpt
->tb_freq
, get_ticks_per_sec());
1358 /* Interrupt mask */
1363 /* Interrupt status */
1367 /* Interrupt enable */
1372 idx
= (addr
- 0x80) >> 2;
1373 ret
= gpt
->comp
[idx
];
1377 idx
= (addr
- 0xC0) >> 2;
1378 ret
= gpt
->mask
[idx
];
1388 static void ppc4xx_gpt_writel (void *opaque
,
1389 target_phys_addr_t addr
, uint32_t value
)
1395 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1401 /* Time base counter */
1402 gpt
->tb_offset
= muldiv64(value
, get_ticks_per_sec(), gpt
->tb_freq
)
1403 - qemu_get_clock(vm_clock
);
1404 ppc4xx_gpt_compute_timer(gpt
);
1408 gpt
->oe
= value
& 0xF8000000;
1409 ppc4xx_gpt_set_outputs(gpt
);
1413 gpt
->ol
= value
& 0xF8000000;
1414 ppc4xx_gpt_set_outputs(gpt
);
1417 /* Interrupt mask */
1418 gpt
->im
= value
& 0x0000F800;
1421 /* Interrupt status set */
1422 gpt
->is
|= value
& 0x0000F800;
1423 ppc4xx_gpt_set_irqs(gpt
);
1426 /* Interrupt status clear */
1427 gpt
->is
&= ~(value
& 0x0000F800);
1428 ppc4xx_gpt_set_irqs(gpt
);
1431 /* Interrupt enable */
1432 gpt
->ie
= value
& 0x0000F800;
1433 ppc4xx_gpt_set_irqs(gpt
);
1437 idx
= (addr
- 0x80) >> 2;
1438 gpt
->comp
[idx
] = value
& 0xF8000000;
1439 ppc4xx_gpt_compute_timer(gpt
);
1443 idx
= (addr
- 0xC0) >> 2;
1444 gpt
->mask
[idx
] = value
& 0xF8000000;
1445 ppc4xx_gpt_compute_timer(gpt
);
1450 static CPUReadMemoryFunc
* const gpt_read
[] = {
1456 static CPUWriteMemoryFunc
* const gpt_write
[] = {
1462 static void ppc4xx_gpt_cb (void *opaque
)
1467 ppc4xx_gpt_set_irqs(gpt
);
1468 ppc4xx_gpt_set_outputs(gpt
);
1469 ppc4xx_gpt_compute_timer(gpt
);
1472 static void ppc4xx_gpt_reset (void *opaque
)
1478 qemu_del_timer(gpt
->timer
);
1479 gpt
->oe
= 0x00000000;
1480 gpt
->ol
= 0x00000000;
1481 gpt
->im
= 0x00000000;
1482 gpt
->is
= 0x00000000;
1483 gpt
->ie
= 0x00000000;
1484 for (i
= 0; i
< 5; i
++) {
1485 gpt
->comp
[i
] = 0x00000000;
1486 gpt
->mask
[i
] = 0x00000000;
1490 static void ppc4xx_gpt_init(target_phys_addr_t base
, qemu_irq irqs
[5])
1496 gpt
= qemu_mallocz(sizeof(ppc4xx_gpt_t
));
1497 for (i
= 0; i
< 5; i
++) {
1498 gpt
->irqs
[i
] = irqs
[i
];
1500 gpt
->timer
= qemu_new_timer(vm_clock
, &ppc4xx_gpt_cb
, gpt
);
1502 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1504 io
= cpu_register_io_memory(gpt_read
, gpt_write
, gpt
);
1505 cpu_register_physical_memory(base
, 0x0d4, io
);
1506 qemu_register_reset(ppc4xx_gpt_reset
, gpt
);
1509 /*****************************************************************************/
1515 MAL0_TXCASR
= 0x184,
1516 MAL0_TXCARR
= 0x185,
1517 MAL0_TXEOBISR
= 0x186,
1518 MAL0_TXDEIR
= 0x187,
1519 MAL0_RXCASR
= 0x190,
1520 MAL0_RXCARR
= 0x191,
1521 MAL0_RXEOBISR
= 0x192,
1522 MAL0_RXDEIR
= 0x193,
1523 MAL0_TXCTP0R
= 0x1A0,
1524 MAL0_TXCTP1R
= 0x1A1,
1525 MAL0_TXCTP2R
= 0x1A2,
1526 MAL0_TXCTP3R
= 0x1A3,
1527 MAL0_RXCTP0R
= 0x1C0,
1528 MAL0_RXCTP1R
= 0x1C1,
1533 typedef struct ppc40x_mal_t ppc40x_mal_t
;
1534 struct ppc40x_mal_t
{
1552 static void ppc40x_mal_reset (void *opaque
);
1554 static uint32_t dcr_read_mal (void *opaque
, int dcrn
)
1577 ret
= mal
->txeobisr
;
1589 ret
= mal
->rxeobisr
;
1595 ret
= mal
->txctpr
[0];
1598 ret
= mal
->txctpr
[1];
1601 ret
= mal
->txctpr
[2];
1604 ret
= mal
->txctpr
[3];
1607 ret
= mal
->rxctpr
[0];
1610 ret
= mal
->rxctpr
[1];
1626 static void dcr_write_mal (void *opaque
, int dcrn
, uint32_t val
)
1634 if (val
& 0x80000000)
1635 ppc40x_mal_reset(mal
);
1636 mal
->cfg
= val
& 0x00FFC087;
1643 mal
->ier
= val
& 0x0000001F;
1646 mal
->txcasr
= val
& 0xF0000000;
1649 mal
->txcarr
= val
& 0xF0000000;
1653 mal
->txeobisr
&= ~val
;
1657 mal
->txdeir
&= ~val
;
1660 mal
->rxcasr
= val
& 0xC0000000;
1663 mal
->rxcarr
= val
& 0xC0000000;
1667 mal
->rxeobisr
&= ~val
;
1671 mal
->rxdeir
&= ~val
;
1685 mal
->txctpr
[idx
] = val
;
1693 mal
->rxctpr
[idx
] = val
;
1697 goto update_rx_size
;
1701 mal
->rcbs
[idx
] = val
& 0x000000FF;
1706 static void ppc40x_mal_reset (void *opaque
)
1711 mal
->cfg
= 0x0007C000;
1712 mal
->esr
= 0x00000000;
1713 mal
->ier
= 0x00000000;
1714 mal
->rxcasr
= 0x00000000;
1715 mal
->rxdeir
= 0x00000000;
1716 mal
->rxeobisr
= 0x00000000;
1717 mal
->txcasr
= 0x00000000;
1718 mal
->txdeir
= 0x00000000;
1719 mal
->txeobisr
= 0x00000000;
1722 static void ppc405_mal_init(CPUState
*env
, qemu_irq irqs
[4])
1727 mal
= qemu_mallocz(sizeof(ppc40x_mal_t
));
1728 for (i
= 0; i
< 4; i
++)
1729 mal
->irqs
[i
] = irqs
[i
];
1730 qemu_register_reset(&ppc40x_mal_reset
, mal
);
1731 ppc_dcr_register(env
, MAL0_CFG
,
1732 mal
, &dcr_read_mal
, &dcr_write_mal
);
1733 ppc_dcr_register(env
, MAL0_ESR
,
1734 mal
, &dcr_read_mal
, &dcr_write_mal
);
1735 ppc_dcr_register(env
, MAL0_IER
,
1736 mal
, &dcr_read_mal
, &dcr_write_mal
);
1737 ppc_dcr_register(env
, MAL0_TXCASR
,
1738 mal
, &dcr_read_mal
, &dcr_write_mal
);
1739 ppc_dcr_register(env
, MAL0_TXCARR
,
1740 mal
, &dcr_read_mal
, &dcr_write_mal
);
1741 ppc_dcr_register(env
, MAL0_TXEOBISR
,
1742 mal
, &dcr_read_mal
, &dcr_write_mal
);
1743 ppc_dcr_register(env
, MAL0_TXDEIR
,
1744 mal
, &dcr_read_mal
, &dcr_write_mal
);
1745 ppc_dcr_register(env
, MAL0_RXCASR
,
1746 mal
, &dcr_read_mal
, &dcr_write_mal
);
1747 ppc_dcr_register(env
, MAL0_RXCARR
,
1748 mal
, &dcr_read_mal
, &dcr_write_mal
);
1749 ppc_dcr_register(env
, MAL0_RXEOBISR
,
1750 mal
, &dcr_read_mal
, &dcr_write_mal
);
1751 ppc_dcr_register(env
, MAL0_RXDEIR
,
1752 mal
, &dcr_read_mal
, &dcr_write_mal
);
1753 ppc_dcr_register(env
, MAL0_TXCTP0R
,
1754 mal
, &dcr_read_mal
, &dcr_write_mal
);
1755 ppc_dcr_register(env
, MAL0_TXCTP1R
,
1756 mal
, &dcr_read_mal
, &dcr_write_mal
);
1757 ppc_dcr_register(env
, MAL0_TXCTP2R
,
1758 mal
, &dcr_read_mal
, &dcr_write_mal
);
1759 ppc_dcr_register(env
, MAL0_TXCTP3R
,
1760 mal
, &dcr_read_mal
, &dcr_write_mal
);
1761 ppc_dcr_register(env
, MAL0_RXCTP0R
,
1762 mal
, &dcr_read_mal
, &dcr_write_mal
);
1763 ppc_dcr_register(env
, MAL0_RXCTP1R
,
1764 mal
, &dcr_read_mal
, &dcr_write_mal
);
1765 ppc_dcr_register(env
, MAL0_RCBS0
,
1766 mal
, &dcr_read_mal
, &dcr_write_mal
);
1767 ppc_dcr_register(env
, MAL0_RCBS1
,
1768 mal
, &dcr_read_mal
, &dcr_write_mal
);
1771 /*****************************************************************************/
1773 void ppc40x_core_reset (CPUState
*env
)
1777 printf("Reset PowerPC core\n");
1778 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1783 qemu_system_reset_request();
1785 dbsr
= env
->spr
[SPR_40x_DBSR
];
1786 dbsr
&= ~0x00000300;
1788 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1791 void ppc40x_chip_reset (CPUState
*env
)
1795 printf("Reset PowerPC chip\n");
1796 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1801 qemu_system_reset_request();
1803 /* XXX: TODO reset all internal peripherals */
1804 dbsr
= env
->spr
[SPR_40x_DBSR
];
1805 dbsr
&= ~0x00000300;
1807 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1810 void ppc40x_system_reset (CPUState
*env
)
1812 printf("Reset PowerPC system\n");
1813 qemu_system_reset_request();
1816 void store_40x_dbcr0 (CPUState
*env
, uint32_t val
)
1818 switch ((val
>> 28) & 0x3) {
1824 ppc40x_core_reset(env
);
1828 ppc40x_chip_reset(env
);
1832 ppc40x_system_reset(env
);
1837 /*****************************************************************************/
1840 PPC405CR_CPC0_PLLMR
= 0x0B0,
1841 PPC405CR_CPC0_CR0
= 0x0B1,
1842 PPC405CR_CPC0_CR1
= 0x0B2,
1843 PPC405CR_CPC0_PSR
= 0x0B4,
1844 PPC405CR_CPC0_JTAGID
= 0x0B5,
1845 PPC405CR_CPC0_ER
= 0x0B9,
1846 PPC405CR_CPC0_FR
= 0x0BA,
1847 PPC405CR_CPC0_SR
= 0x0BB,
1851 PPC405CR_CPU_CLK
= 0,
1852 PPC405CR_TMR_CLK
= 1,
1853 PPC405CR_PLB_CLK
= 2,
1854 PPC405CR_SDRAM_CLK
= 3,
1855 PPC405CR_OPB_CLK
= 4,
1856 PPC405CR_EXT_CLK
= 5,
1857 PPC405CR_UART_CLK
= 6,
1858 PPC405CR_CLK_NB
= 7,
1861 typedef struct ppc405cr_cpc_t ppc405cr_cpc_t
;
1862 struct ppc405cr_cpc_t
{
1863 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
1874 static void ppc405cr_clk_setup (ppc405cr_cpc_t
*cpc
)
1876 uint64_t VCO_out
, PLL_out
;
1877 uint32_t CPU_clk
, TMR_clk
, SDRAM_clk
, PLB_clk
, OPB_clk
, EXT_clk
, UART_clk
;
1880 D0
= ((cpc
->pllmr
>> 26) & 0x3) + 1; /* CBDV */
1881 if (cpc
->pllmr
& 0x80000000) {
1882 D1
= (((cpc
->pllmr
>> 20) - 1) & 0xF) + 1; /* FBDV */
1883 D2
= 8 - ((cpc
->pllmr
>> 16) & 0x7); /* FWDVA */
1885 VCO_out
= cpc
->sysclk
* M
;
1886 if (VCO_out
< 400000000 || VCO_out
> 800000000) {
1887 /* PLL cannot lock */
1888 cpc
->pllmr
&= ~0x80000000;
1891 PLL_out
= VCO_out
/ D2
;
1896 PLL_out
= cpc
->sysclk
* M
;
1899 if (cpc
->cr1
& 0x00800000)
1900 TMR_clk
= cpc
->sysclk
; /* Should have a separate clock */
1903 PLB_clk
= CPU_clk
/ D0
;
1904 SDRAM_clk
= PLB_clk
;
1905 D0
= ((cpc
->pllmr
>> 10) & 0x3) + 1;
1906 OPB_clk
= PLB_clk
/ D0
;
1907 D0
= ((cpc
->pllmr
>> 24) & 0x3) + 2;
1908 EXT_clk
= PLB_clk
/ D0
;
1909 D0
= ((cpc
->cr0
>> 1) & 0x1F) + 1;
1910 UART_clk
= CPU_clk
/ D0
;
1911 /* Setup CPU clocks */
1912 clk_setup(&cpc
->clk_setup
[PPC405CR_CPU_CLK
], CPU_clk
);
1913 /* Setup time-base clock */
1914 clk_setup(&cpc
->clk_setup
[PPC405CR_TMR_CLK
], TMR_clk
);
1915 /* Setup PLB clock */
1916 clk_setup(&cpc
->clk_setup
[PPC405CR_PLB_CLK
], PLB_clk
);
1917 /* Setup SDRAM clock */
1918 clk_setup(&cpc
->clk_setup
[PPC405CR_SDRAM_CLK
], SDRAM_clk
);
1919 /* Setup OPB clock */
1920 clk_setup(&cpc
->clk_setup
[PPC405CR_OPB_CLK
], OPB_clk
);
1921 /* Setup external clock */
1922 clk_setup(&cpc
->clk_setup
[PPC405CR_EXT_CLK
], EXT_clk
);
1923 /* Setup UART clock */
1924 clk_setup(&cpc
->clk_setup
[PPC405CR_UART_CLK
], UART_clk
);
1927 static uint32_t dcr_read_crcpc (void *opaque
, int dcrn
)
1929 ppc405cr_cpc_t
*cpc
;
1934 case PPC405CR_CPC0_PLLMR
:
1937 case PPC405CR_CPC0_CR0
:
1940 case PPC405CR_CPC0_CR1
:
1943 case PPC405CR_CPC0_PSR
:
1946 case PPC405CR_CPC0_JTAGID
:
1949 case PPC405CR_CPC0_ER
:
1952 case PPC405CR_CPC0_FR
:
1955 case PPC405CR_CPC0_SR
:
1956 ret
= ~(cpc
->er
| cpc
->fr
) & 0xFFFF0000;
1959 /* Avoid gcc warning */
1967 static void dcr_write_crcpc (void *opaque
, int dcrn
, uint32_t val
)
1969 ppc405cr_cpc_t
*cpc
;
1973 case PPC405CR_CPC0_PLLMR
:
1974 cpc
->pllmr
= val
& 0xFFF77C3F;
1976 case PPC405CR_CPC0_CR0
:
1977 cpc
->cr0
= val
& 0x0FFFFFFE;
1979 case PPC405CR_CPC0_CR1
:
1980 cpc
->cr1
= val
& 0x00800000;
1982 case PPC405CR_CPC0_PSR
:
1985 case PPC405CR_CPC0_JTAGID
:
1988 case PPC405CR_CPC0_ER
:
1989 cpc
->er
= val
& 0xBFFC0000;
1991 case PPC405CR_CPC0_FR
:
1992 cpc
->fr
= val
& 0xBFFC0000;
1994 case PPC405CR_CPC0_SR
:
2000 static void ppc405cr_cpc_reset (void *opaque
)
2002 ppc405cr_cpc_t
*cpc
;
2006 /* Compute PLLMR value from PSR settings */
2007 cpc
->pllmr
= 0x80000000;
2009 switch ((cpc
->psr
>> 30) & 3) {
2012 cpc
->pllmr
&= ~0x80000000;
2016 cpc
->pllmr
|= 5 << 16;
2020 cpc
->pllmr
|= 4 << 16;
2024 cpc
->pllmr
|= 2 << 16;
2028 D
= (cpc
->psr
>> 28) & 3;
2029 cpc
->pllmr
|= (D
+ 1) << 20;
2031 D
= (cpc
->psr
>> 25) & 7;
2046 D
= (cpc
->psr
>> 23) & 3;
2047 cpc
->pllmr
|= D
<< 26;
2049 D
= (cpc
->psr
>> 21) & 3;
2050 cpc
->pllmr
|= D
<< 10;
2052 D
= (cpc
->psr
>> 17) & 3;
2053 cpc
->pllmr
|= D
<< 24;
2054 cpc
->cr0
= 0x0000003C;
2055 cpc
->cr1
= 0x2B0D8800;
2056 cpc
->er
= 0x00000000;
2057 cpc
->fr
= 0x00000000;
2058 ppc405cr_clk_setup(cpc
);
2061 static void ppc405cr_clk_init (ppc405cr_cpc_t
*cpc
)
2065 /* XXX: this should be read from IO pins */
2066 cpc
->psr
= 0x00000000; /* 8 bits ROM */
2068 D
= 0x2; /* Divide by 4 */
2069 cpc
->psr
|= D
<< 30;
2071 D
= 0x1; /* Divide by 2 */
2072 cpc
->psr
|= D
<< 28;
2074 D
= 0x1; /* Divide by 2 */
2075 cpc
->psr
|= D
<< 23;
2077 D
= 0x5; /* M = 16 */
2078 cpc
->psr
|= D
<< 25;
2080 D
= 0x1; /* Divide by 2 */
2081 cpc
->psr
|= D
<< 21;
2083 D
= 0x2; /* Divide by 4 */
2084 cpc
->psr
|= D
<< 17;
2087 static void ppc405cr_cpc_init (CPUState
*env
, clk_setup_t clk_setup
[7],
2090 ppc405cr_cpc_t
*cpc
;
2092 cpc
= qemu_mallocz(sizeof(ppc405cr_cpc_t
));
2093 memcpy(cpc
->clk_setup
, clk_setup
,
2094 PPC405CR_CLK_NB
* sizeof(clk_setup_t
));
2095 cpc
->sysclk
= sysclk
;
2096 cpc
->jtagid
= 0x42051049;
2097 ppc_dcr_register(env
, PPC405CR_CPC0_PSR
, cpc
,
2098 &dcr_read_crcpc
, &dcr_write_crcpc
);
2099 ppc_dcr_register(env
, PPC405CR_CPC0_CR0
, cpc
,
2100 &dcr_read_crcpc
, &dcr_write_crcpc
);
2101 ppc_dcr_register(env
, PPC405CR_CPC0_CR1
, cpc
,
2102 &dcr_read_crcpc
, &dcr_write_crcpc
);
2103 ppc_dcr_register(env
, PPC405CR_CPC0_JTAGID
, cpc
,
2104 &dcr_read_crcpc
, &dcr_write_crcpc
);
2105 ppc_dcr_register(env
, PPC405CR_CPC0_PLLMR
, cpc
,
2106 &dcr_read_crcpc
, &dcr_write_crcpc
);
2107 ppc_dcr_register(env
, PPC405CR_CPC0_ER
, cpc
,
2108 &dcr_read_crcpc
, &dcr_write_crcpc
);
2109 ppc_dcr_register(env
, PPC405CR_CPC0_FR
, cpc
,
2110 &dcr_read_crcpc
, &dcr_write_crcpc
);
2111 ppc_dcr_register(env
, PPC405CR_CPC0_SR
, cpc
,
2112 &dcr_read_crcpc
, &dcr_write_crcpc
);
2113 ppc405cr_clk_init(cpc
);
2114 qemu_register_reset(ppc405cr_cpc_reset
, cpc
);
2117 CPUState
*ppc405cr_init (target_phys_addr_t ram_bases
[4],
2118 target_phys_addr_t ram_sizes
[4],
2119 uint32_t sysclk
, qemu_irq
**picp
,
2122 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
2123 qemu_irq dma_irqs
[4];
2125 qemu_irq
*pic
, *irqs
;
2127 memset(clk_setup
, 0, sizeof(clk_setup
));
2128 env
= ppc4xx_init("405cr", &clk_setup
[PPC405CR_CPU_CLK
],
2129 &clk_setup
[PPC405CR_TMR_CLK
], sysclk
);
2130 /* Memory mapped devices registers */
2132 ppc4xx_plb_init(env
);
2133 /* PLB to OPB bridge */
2134 ppc4xx_pob_init(env
);
2136 ppc4xx_opba_init(0xef600600);
2137 /* Universal interrupt controller */
2138 irqs
= qemu_mallocz(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2139 irqs
[PPCUIC_OUTPUT_INT
] =
2140 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2141 irqs
[PPCUIC_OUTPUT_CINT
] =
2142 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2143 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2145 /* SDRAM controller */
2146 ppc4xx_sdram_init(env
, pic
[14], 1, ram_bases
, ram_sizes
, do_init
);
2147 /* External bus controller */
2148 ppc405_ebc_init(env
);
2149 /* DMA controller */
2150 dma_irqs
[0] = pic
[26];
2151 dma_irqs
[1] = pic
[25];
2152 dma_irqs
[2] = pic
[24];
2153 dma_irqs
[3] = pic
[23];
2154 ppc405_dma_init(env
, dma_irqs
);
2156 if (serial_hds
[0] != NULL
) {
2157 serial_mm_init(0xef600300, 0, pic
[0], PPC_SERIAL_MM_BAUDBASE
,
2158 serial_hds
[0], 1, 1);
2160 if (serial_hds
[1] != NULL
) {
2161 serial_mm_init(0xef600400, 0, pic
[1], PPC_SERIAL_MM_BAUDBASE
,
2162 serial_hds
[1], 1, 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 (CPUState
*env
, clk_setup_t clk_setup
[8],
2427 ppc405ep_cpc_t
*cpc
;
2429 cpc
= qemu_mallocz(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 CPUState
*ppc405ep_init (target_phys_addr_t ram_bases
[2],
2462 target_phys_addr_t ram_sizes
[2],
2463 uint32_t sysclk
, qemu_irq
**picp
,
2466 clk_setup_t clk_setup
[PPC405EP_CLK_NB
], tlb_clk_setup
;
2467 qemu_irq dma_irqs
[4], gpt_irqs
[5], mal_irqs
[4];
2469 qemu_irq
*pic
, *irqs
;
2471 memset(clk_setup
, 0, sizeof(clk_setup
));
2473 env
= ppc4xx_init("405ep", &clk_setup
[PPC405EP_CPU_CLK
],
2474 &tlb_clk_setup
, sysclk
);
2475 clk_setup
[PPC405EP_CPU_CLK
].cb
= tlb_clk_setup
.cb
;
2476 clk_setup
[PPC405EP_CPU_CLK
].opaque
= tlb_clk_setup
.opaque
;
2477 /* Internal devices init */
2478 /* Memory mapped devices registers */
2480 ppc4xx_plb_init(env
);
2481 /* PLB to OPB bridge */
2482 ppc4xx_pob_init(env
);
2484 ppc4xx_opba_init(0xef600600);
2485 /* Universal interrupt controller */
2486 irqs
= qemu_mallocz(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2487 irqs
[PPCUIC_OUTPUT_INT
] =
2488 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2489 irqs
[PPCUIC_OUTPUT_CINT
] =
2490 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2491 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2493 /* SDRAM controller */
2494 /* XXX 405EP has no ECC interrupt */
2495 ppc4xx_sdram_init(env
, pic
[17], 2, ram_bases
, ram_sizes
, do_init
);
2496 /* External bus controller */
2497 ppc405_ebc_init(env
);
2498 /* DMA controller */
2499 dma_irqs
[0] = pic
[5];
2500 dma_irqs
[1] = pic
[6];
2501 dma_irqs
[2] = pic
[7];
2502 dma_irqs
[3] = pic
[8];
2503 ppc405_dma_init(env
, dma_irqs
);
2504 /* IIC controller */
2505 ppc405_i2c_init(0xef600500, pic
[2]);
2507 ppc405_gpio_init(0xef600700);
2509 if (serial_hds
[0] != NULL
) {
2510 serial_mm_init(0xef600300, 0, pic
[0], PPC_SERIAL_MM_BAUDBASE
,
2511 serial_hds
[0], 1, 1);
2513 if (serial_hds
[1] != NULL
) {
2514 serial_mm_init(0xef600400, 0, pic
[1], PPC_SERIAL_MM_BAUDBASE
,
2515 serial_hds
[1], 1, 1);
2518 ppc405_ocm_init(env
);
2520 gpt_irqs
[0] = pic
[19];
2521 gpt_irqs
[1] = pic
[20];
2522 gpt_irqs
[2] = pic
[21];
2523 gpt_irqs
[3] = pic
[22];
2524 gpt_irqs
[4] = pic
[23];
2525 ppc4xx_gpt_init(0xef600000, gpt_irqs
);
2527 /* Uses pic[3], pic[16], pic[18] */
2529 mal_irqs
[0] = pic
[11];
2530 mal_irqs
[1] = pic
[12];
2531 mal_irqs
[2] = pic
[13];
2532 mal_irqs
[3] = pic
[14];
2533 ppc405_mal_init(env
, mal_irqs
);
2535 /* Uses pic[9], pic[15], pic[17] */
2537 ppc405ep_cpc_init(env
, clk_setup
, sysclk
);