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_r_version
[i
]);
74 stl_phys(bdloc
+ 0x5C, bd
->bi_plb_busfreq
);
75 stl_phys(bdloc
+ 0x60, bd
->bi_pci_busfreq
);
76 for (i
= 0; i
< 6; i
++)
77 stb_phys(bdloc
+ 0x64 + i
, bd
->bi_pci_enetaddr
[i
]);
79 if (flags
& 0x00000001) {
80 for (i
= 0; i
< 6; i
++)
81 stb_phys(bdloc
+ n
++, bd
->bi_pci_enetaddr2
[i
]);
83 stl_phys(bdloc
+ n
, bd
->bi_opbfreq
);
85 for (i
= 0; i
< 2; i
++) {
86 stl_phys(bdloc
+ n
, bd
->bi_iic_fast
[i
]);
93 /*****************************************************************************/
94 /* Shared peripherals */
96 /*****************************************************************************/
97 /* Peripheral local bus arbitrer */
104 typedef struct ppc4xx_plb_t ppc4xx_plb_t
;
105 struct ppc4xx_plb_t
{
111 static uint32_t dcr_read_plb (void *opaque
, int dcrn
)
128 /* Avoid gcc warning */
136 static void dcr_write_plb (void *opaque
, int dcrn
, uint32_t val
)
143 /* We don't care about the actual parameters written as
144 * we don't manage any priorities on the bus
146 plb
->acr
= val
& 0xF8000000;
158 static void ppc4xx_plb_reset (void *opaque
)
163 plb
->acr
= 0x00000000;
164 plb
->bear
= 0x00000000;
165 plb
->besr
= 0x00000000;
168 static void ppc4xx_plb_init(CPUState
*env
)
172 plb
= qemu_mallocz(sizeof(ppc4xx_plb_t
));
173 ppc_dcr_register(env
, PLB0_ACR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
174 ppc_dcr_register(env
, PLB0_BEAR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
175 ppc_dcr_register(env
, PLB0_BESR
, plb
, &dcr_read_plb
, &dcr_write_plb
);
176 qemu_register_reset(ppc4xx_plb_reset
, plb
);
179 /*****************************************************************************/
180 /* PLB to OPB bridge */
187 typedef struct ppc4xx_pob_t ppc4xx_pob_t
;
188 struct ppc4xx_pob_t
{
193 static uint32_t dcr_read_pob (void *opaque
, int dcrn
)
205 ret
= pob
->besr
[dcrn
- POB0_BESR0
];
208 /* Avoid gcc warning */
216 static void dcr_write_pob (void *opaque
, int dcrn
, uint32_t val
)
228 pob
->besr
[dcrn
- POB0_BESR0
] &= ~val
;
233 static void ppc4xx_pob_reset (void *opaque
)
239 pob
->bear
= 0x00000000;
240 pob
->besr
[0] = 0x0000000;
241 pob
->besr
[1] = 0x0000000;
244 static void ppc4xx_pob_init(CPUState
*env
)
248 pob
= qemu_mallocz(sizeof(ppc4xx_pob_t
));
249 ppc_dcr_register(env
, POB0_BEAR
, pob
, &dcr_read_pob
, &dcr_write_pob
);
250 ppc_dcr_register(env
, POB0_BESR0
, pob
, &dcr_read_pob
, &dcr_write_pob
);
251 ppc_dcr_register(env
, POB0_BESR1
, pob
, &dcr_read_pob
, &dcr_write_pob
);
252 qemu_register_reset(ppc4xx_pob_reset
, pob
);
255 /*****************************************************************************/
257 typedef struct ppc4xx_opba_t ppc4xx_opba_t
;
258 struct ppc4xx_opba_t
{
263 static uint32_t opba_readb (void *opaque
, target_phys_addr_t addr
)
269 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
287 static void opba_writeb (void *opaque
,
288 target_phys_addr_t addr
, uint32_t value
)
293 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
299 opba
->cr
= value
& 0xF8;
302 opba
->pr
= value
& 0xFF;
309 static uint32_t opba_readw (void *opaque
, target_phys_addr_t addr
)
314 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
316 ret
= opba_readb(opaque
, addr
) << 8;
317 ret
|= opba_readb(opaque
, addr
+ 1);
322 static void opba_writew (void *opaque
,
323 target_phys_addr_t addr
, uint32_t value
)
326 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
329 opba_writeb(opaque
, addr
, value
>> 8);
330 opba_writeb(opaque
, addr
+ 1, value
);
333 static uint32_t opba_readl (void *opaque
, target_phys_addr_t addr
)
338 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
340 ret
= opba_readb(opaque
, addr
) << 24;
341 ret
|= opba_readb(opaque
, addr
+ 1) << 16;
346 static void opba_writel (void *opaque
,
347 target_phys_addr_t addr
, uint32_t value
)
350 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
353 opba_writeb(opaque
, addr
, value
>> 24);
354 opba_writeb(opaque
, addr
+ 1, value
>> 16);
357 static CPUReadMemoryFunc
* const opba_read
[] = {
363 static CPUWriteMemoryFunc
* const opba_write
[] = {
369 static void ppc4xx_opba_reset (void *opaque
)
374 opba
->cr
= 0x00; /* No dynamic priorities - park disabled */
378 static void ppc4xx_opba_init(target_phys_addr_t base
)
383 opba
= qemu_mallocz(sizeof(ppc4xx_opba_t
));
385 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
387 io
= cpu_register_io_memory(opba_read
, opba_write
, opba
,
388 DEVICE_NATIVE_ENDIAN
);
389 cpu_register_physical_memory(base
, 0x002, io
);
390 qemu_register_reset(ppc4xx_opba_reset
, opba
);
393 /*****************************************************************************/
394 /* Code decompression controller */
397 /*****************************************************************************/
398 /* Peripheral controller */
399 typedef struct ppc4xx_ebc_t ppc4xx_ebc_t
;
400 struct ppc4xx_ebc_t
{
411 EBC0_CFGADDR
= 0x012,
412 EBC0_CFGDATA
= 0x013,
415 static uint32_t dcr_read_ebc (void *opaque
, int dcrn
)
427 case 0x00: /* B0CR */
430 case 0x01: /* B1CR */
433 case 0x02: /* B2CR */
436 case 0x03: /* B3CR */
439 case 0x04: /* B4CR */
442 case 0x05: /* B5CR */
445 case 0x06: /* B6CR */
448 case 0x07: /* B7CR */
451 case 0x10: /* B0AP */
454 case 0x11: /* B1AP */
457 case 0x12: /* B2AP */
460 case 0x13: /* B3AP */
463 case 0x14: /* B4AP */
466 case 0x15: /* B5AP */
469 case 0x16: /* B6AP */
472 case 0x17: /* B7AP */
475 case 0x20: /* BEAR */
478 case 0x21: /* BESR0 */
481 case 0x22: /* BESR1 */
500 static void dcr_write_ebc (void *opaque
, int dcrn
, uint32_t val
)
511 case 0x00: /* B0CR */
513 case 0x01: /* B1CR */
515 case 0x02: /* B2CR */
517 case 0x03: /* B3CR */
519 case 0x04: /* B4CR */
521 case 0x05: /* B5CR */
523 case 0x06: /* B6CR */
525 case 0x07: /* B7CR */
527 case 0x10: /* B0AP */
529 case 0x11: /* B1AP */
531 case 0x12: /* B2AP */
533 case 0x13: /* B3AP */
535 case 0x14: /* B4AP */
537 case 0x15: /* B5AP */
539 case 0x16: /* B6AP */
541 case 0x17: /* B7AP */
543 case 0x20: /* BEAR */
545 case 0x21: /* BESR0 */
547 case 0x22: /* BESR1 */
560 static void ebc_reset (void *opaque
)
566 ebc
->addr
= 0x00000000;
567 ebc
->bap
[0] = 0x7F8FFE80;
568 ebc
->bcr
[0] = 0xFFE28000;
569 for (i
= 0; i
< 8; i
++) {
570 ebc
->bap
[i
] = 0x00000000;
571 ebc
->bcr
[i
] = 0x00000000;
573 ebc
->besr0
= 0x00000000;
574 ebc
->besr1
= 0x00000000;
575 ebc
->cfg
= 0x80400000;
578 static void ppc405_ebc_init(CPUState
*env
)
582 ebc
= qemu_mallocz(sizeof(ppc4xx_ebc_t
));
583 qemu_register_reset(&ebc_reset
, ebc
);
584 ppc_dcr_register(env
, EBC0_CFGADDR
,
585 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
586 ppc_dcr_register(env
, EBC0_CFGDATA
,
587 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
590 /*****************************************************************************/
619 typedef struct ppc405_dma_t ppc405_dma_t
;
620 struct ppc405_dma_t
{
633 static uint32_t dcr_read_dma (void *opaque
, int dcrn
)
638 static void dcr_write_dma (void *opaque
, int dcrn
, uint32_t val
)
642 static void ppc405_dma_reset (void *opaque
)
648 for (i
= 0; i
< 4; i
++) {
649 dma
->cr
[i
] = 0x00000000;
650 dma
->ct
[i
] = 0x00000000;
651 dma
->da
[i
] = 0x00000000;
652 dma
->sa
[i
] = 0x00000000;
653 dma
->sg
[i
] = 0x00000000;
655 dma
->sr
= 0x00000000;
656 dma
->sgc
= 0x00000000;
657 dma
->slp
= 0x7C000000;
658 dma
->pol
= 0x00000000;
661 static void ppc405_dma_init(CPUState
*env
, qemu_irq irqs
[4])
665 dma
= qemu_mallocz(sizeof(ppc405_dma_t
));
666 memcpy(dma
->irqs
, irqs
, 4 * sizeof(qemu_irq
));
667 qemu_register_reset(&ppc405_dma_reset
, dma
);
668 ppc_dcr_register(env
, DMA0_CR0
,
669 dma
, &dcr_read_dma
, &dcr_write_dma
);
670 ppc_dcr_register(env
, DMA0_CT0
,
671 dma
, &dcr_read_dma
, &dcr_write_dma
);
672 ppc_dcr_register(env
, DMA0_DA0
,
673 dma
, &dcr_read_dma
, &dcr_write_dma
);
674 ppc_dcr_register(env
, DMA0_SA0
,
675 dma
, &dcr_read_dma
, &dcr_write_dma
);
676 ppc_dcr_register(env
, DMA0_SG0
,
677 dma
, &dcr_read_dma
, &dcr_write_dma
);
678 ppc_dcr_register(env
, DMA0_CR1
,
679 dma
, &dcr_read_dma
, &dcr_write_dma
);
680 ppc_dcr_register(env
, DMA0_CT1
,
681 dma
, &dcr_read_dma
, &dcr_write_dma
);
682 ppc_dcr_register(env
, DMA0_DA1
,
683 dma
, &dcr_read_dma
, &dcr_write_dma
);
684 ppc_dcr_register(env
, DMA0_SA1
,
685 dma
, &dcr_read_dma
, &dcr_write_dma
);
686 ppc_dcr_register(env
, DMA0_SG1
,
687 dma
, &dcr_read_dma
, &dcr_write_dma
);
688 ppc_dcr_register(env
, DMA0_CR2
,
689 dma
, &dcr_read_dma
, &dcr_write_dma
);
690 ppc_dcr_register(env
, DMA0_CT2
,
691 dma
, &dcr_read_dma
, &dcr_write_dma
);
692 ppc_dcr_register(env
, DMA0_DA2
,
693 dma
, &dcr_read_dma
, &dcr_write_dma
);
694 ppc_dcr_register(env
, DMA0_SA2
,
695 dma
, &dcr_read_dma
, &dcr_write_dma
);
696 ppc_dcr_register(env
, DMA0_SG2
,
697 dma
, &dcr_read_dma
, &dcr_write_dma
);
698 ppc_dcr_register(env
, DMA0_CR3
,
699 dma
, &dcr_read_dma
, &dcr_write_dma
);
700 ppc_dcr_register(env
, DMA0_CT3
,
701 dma
, &dcr_read_dma
, &dcr_write_dma
);
702 ppc_dcr_register(env
, DMA0_DA3
,
703 dma
, &dcr_read_dma
, &dcr_write_dma
);
704 ppc_dcr_register(env
, DMA0_SA3
,
705 dma
, &dcr_read_dma
, &dcr_write_dma
);
706 ppc_dcr_register(env
, DMA0_SG3
,
707 dma
, &dcr_read_dma
, &dcr_write_dma
);
708 ppc_dcr_register(env
, DMA0_SR
,
709 dma
, &dcr_read_dma
, &dcr_write_dma
);
710 ppc_dcr_register(env
, DMA0_SGC
,
711 dma
, &dcr_read_dma
, &dcr_write_dma
);
712 ppc_dcr_register(env
, DMA0_SLP
,
713 dma
, &dcr_read_dma
, &dcr_write_dma
);
714 ppc_dcr_register(env
, DMA0_POL
,
715 dma
, &dcr_read_dma
, &dcr_write_dma
);
718 /*****************************************************************************/
720 typedef struct ppc405_gpio_t ppc405_gpio_t
;
721 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 CPUReadMemoryFunc
* const ppc405_gpio_read
[] = {
795 static CPUWriteMemoryFunc
* const ppc405_gpio_write
[] = {
801 static void ppc405_gpio_reset (void *opaque
)
805 static void ppc405_gpio_init(target_phys_addr_t base
)
810 gpio
= qemu_mallocz(sizeof(ppc405_gpio_t
));
812 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
814 io
= cpu_register_io_memory(ppc405_gpio_read
, ppc405_gpio_write
, gpio
,
815 DEVICE_NATIVE_ENDIAN
);
816 cpu_register_physical_memory(base
, 0x038, io
);
817 qemu_register_reset(&ppc405_gpio_reset
, gpio
);
820 /*****************************************************************************/
824 OCM0_ISACNTL
= 0x019,
826 OCM0_DSACNTL
= 0x01B,
829 typedef struct ppc405_ocm_t ppc405_ocm_t
;
830 struct ppc405_ocm_t
{
838 static void ocm_update_mappings (ppc405_ocm_t
*ocm
,
839 uint32_t isarc
, uint32_t isacntl
,
840 uint32_t dsarc
, uint32_t dsacntl
)
843 printf("OCM update ISA %08" PRIx32
" %08" PRIx32
" (%08" PRIx32
844 " %08" PRIx32
") DSA %08" PRIx32
" %08" PRIx32
845 " (%08" PRIx32
" %08" PRIx32
")\n",
846 isarc
, isacntl
, dsarc
, dsacntl
,
847 ocm
->isarc
, ocm
->isacntl
, ocm
->dsarc
, ocm
->dsacntl
);
849 if (ocm
->isarc
!= isarc
||
850 (ocm
->isacntl
& 0x80000000) != (isacntl
& 0x80000000)) {
851 if (ocm
->isacntl
& 0x80000000) {
852 /* Unmap previously assigned memory region */
853 printf("OCM unmap ISA %08" PRIx32
"\n", ocm
->isarc
);
854 cpu_register_physical_memory(ocm
->isarc
, 0x04000000,
857 if (isacntl
& 0x80000000) {
858 /* Map new instruction memory region */
860 printf("OCM map ISA %08" PRIx32
"\n", isarc
);
862 cpu_register_physical_memory(isarc
, 0x04000000,
863 ocm
->offset
| IO_MEM_RAM
);
866 if (ocm
->dsarc
!= dsarc
||
867 (ocm
->dsacntl
& 0x80000000) != (dsacntl
& 0x80000000)) {
868 if (ocm
->dsacntl
& 0x80000000) {
869 /* Beware not to unmap the region we just mapped */
870 if (!(isacntl
& 0x80000000) || ocm
->dsarc
!= isarc
) {
871 /* Unmap previously assigned memory region */
873 printf("OCM unmap DSA %08" PRIx32
"\n", ocm
->dsarc
);
875 cpu_register_physical_memory(ocm
->dsarc
, 0x04000000,
879 if (dsacntl
& 0x80000000) {
880 /* Beware not to remap the region we just mapped */
881 if (!(isacntl
& 0x80000000) || dsarc
!= isarc
) {
882 /* Map new data memory region */
884 printf("OCM map DSA %08" PRIx32
"\n", dsarc
);
886 cpu_register_physical_memory(dsarc
, 0x04000000,
887 ocm
->offset
| IO_MEM_RAM
);
893 static uint32_t dcr_read_ocm (void *opaque
, int dcrn
)
920 static void dcr_write_ocm (void *opaque
, int dcrn
, uint32_t val
)
923 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
928 isacntl
= ocm
->isacntl
;
929 dsacntl
= ocm
->dsacntl
;
932 isarc
= val
& 0xFC000000;
935 isacntl
= val
& 0xC0000000;
938 isarc
= val
& 0xFC000000;
941 isacntl
= val
& 0xC0000000;
944 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
947 ocm
->isacntl
= isacntl
;
948 ocm
->dsacntl
= dsacntl
;
951 static void ocm_reset (void *opaque
)
954 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
958 isacntl
= 0x00000000;
960 dsacntl
= 0x00000000;
961 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
964 ocm
->isacntl
= isacntl
;
965 ocm
->dsacntl
= dsacntl
;
968 static void ppc405_ocm_init(CPUState
*env
)
972 ocm
= qemu_mallocz(sizeof(ppc405_ocm_t
));
973 ocm
->offset
= qemu_ram_alloc(NULL
, "ppc405.ocm", 4096);
974 qemu_register_reset(&ocm_reset
, ocm
);
975 ppc_dcr_register(env
, OCM0_ISARC
,
976 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
977 ppc_dcr_register(env
, OCM0_ISACNTL
,
978 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
979 ppc_dcr_register(env
, OCM0_DSARC
,
980 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
981 ppc_dcr_register(env
, OCM0_DSACNTL
,
982 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
985 /*****************************************************************************/
987 typedef struct ppc4xx_i2c_t ppc4xx_i2c_t
;
988 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 CPUReadMemoryFunc
* const i2c_read
[] = {
1192 static CPUWriteMemoryFunc
* const i2c_write
[] = {
1198 static void ppc4xx_i2c_reset (void *opaque
)
1211 i2c
->directcntl
= 0x0F;
1214 static void ppc405_i2c_init(target_phys_addr_t base
, qemu_irq irq
)
1219 i2c
= qemu_mallocz(sizeof(ppc4xx_i2c_t
));
1222 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1224 io
= cpu_register_io_memory(i2c_read
, i2c_write
, i2c
,
1225 DEVICE_NATIVE_ENDIAN
);
1226 cpu_register_physical_memory(base
, 0x011, io
);
1227 qemu_register_reset(ppc4xx_i2c_reset
, i2c
);
1230 /*****************************************************************************/
1231 /* General purpose timers */
1232 typedef struct ppc4xx_gpt_t ppc4xx_gpt_t
;
1233 struct ppc4xx_gpt_t
{
1236 struct QEMUTimer
*timer
;
1247 static uint32_t ppc4xx_gpt_readb (void *opaque
, target_phys_addr_t addr
)
1250 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1252 /* XXX: generate a bus fault */
1256 static void ppc4xx_gpt_writeb (void *opaque
,
1257 target_phys_addr_t addr
, uint32_t value
)
1260 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1263 /* XXX: generate a bus fault */
1266 static uint32_t ppc4xx_gpt_readw (void *opaque
, target_phys_addr_t addr
)
1269 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1271 /* XXX: generate a bus fault */
1275 static void ppc4xx_gpt_writew (void *opaque
,
1276 target_phys_addr_t addr
, uint32_t value
)
1279 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1282 /* XXX: generate a bus fault */
1285 static int ppc4xx_gpt_compare (ppc4xx_gpt_t
*gpt
, int n
)
1291 static void ppc4xx_gpt_set_output (ppc4xx_gpt_t
*gpt
, int n
, int level
)
1296 static void ppc4xx_gpt_set_outputs (ppc4xx_gpt_t
*gpt
)
1302 for (i
= 0; i
< 5; i
++) {
1303 if (gpt
->oe
& mask
) {
1304 /* Output is enabled */
1305 if (ppc4xx_gpt_compare(gpt
, i
)) {
1306 /* Comparison is OK */
1307 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
);
1309 /* Comparison is KO */
1310 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
? 0 : 1);
1317 static void ppc4xx_gpt_set_irqs (ppc4xx_gpt_t
*gpt
)
1323 for (i
= 0; i
< 5; i
++) {
1324 if (gpt
->is
& gpt
->im
& mask
)
1325 qemu_irq_raise(gpt
->irqs
[i
]);
1327 qemu_irq_lower(gpt
->irqs
[i
]);
1332 static void ppc4xx_gpt_compute_timer (ppc4xx_gpt_t
*gpt
)
1337 static uint32_t ppc4xx_gpt_readl (void *opaque
, target_phys_addr_t addr
)
1344 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1349 /* Time base counter */
1350 ret
= muldiv64(qemu_get_clock_ns(vm_clock
) + gpt
->tb_offset
,
1351 gpt
->tb_freq
, get_ticks_per_sec());
1362 /* Interrupt mask */
1367 /* Interrupt status */
1371 /* Interrupt enable */
1376 idx
= (addr
- 0x80) >> 2;
1377 ret
= gpt
->comp
[idx
];
1381 idx
= (addr
- 0xC0) >> 2;
1382 ret
= gpt
->mask
[idx
];
1392 static void ppc4xx_gpt_writel (void *opaque
,
1393 target_phys_addr_t addr
, uint32_t value
)
1399 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1405 /* Time base counter */
1406 gpt
->tb_offset
= muldiv64(value
, get_ticks_per_sec(), gpt
->tb_freq
)
1407 - qemu_get_clock_ns(vm_clock
);
1408 ppc4xx_gpt_compute_timer(gpt
);
1412 gpt
->oe
= value
& 0xF8000000;
1413 ppc4xx_gpt_set_outputs(gpt
);
1417 gpt
->ol
= value
& 0xF8000000;
1418 ppc4xx_gpt_set_outputs(gpt
);
1421 /* Interrupt mask */
1422 gpt
->im
= value
& 0x0000F800;
1425 /* Interrupt status set */
1426 gpt
->is
|= value
& 0x0000F800;
1427 ppc4xx_gpt_set_irqs(gpt
);
1430 /* Interrupt status clear */
1431 gpt
->is
&= ~(value
& 0x0000F800);
1432 ppc4xx_gpt_set_irqs(gpt
);
1435 /* Interrupt enable */
1436 gpt
->ie
= value
& 0x0000F800;
1437 ppc4xx_gpt_set_irqs(gpt
);
1441 idx
= (addr
- 0x80) >> 2;
1442 gpt
->comp
[idx
] = value
& 0xF8000000;
1443 ppc4xx_gpt_compute_timer(gpt
);
1447 idx
= (addr
- 0xC0) >> 2;
1448 gpt
->mask
[idx
] = value
& 0xF8000000;
1449 ppc4xx_gpt_compute_timer(gpt
);
1454 static CPUReadMemoryFunc
* const gpt_read
[] = {
1460 static CPUWriteMemoryFunc
* const gpt_write
[] = {
1466 static void ppc4xx_gpt_cb (void *opaque
)
1471 ppc4xx_gpt_set_irqs(gpt
);
1472 ppc4xx_gpt_set_outputs(gpt
);
1473 ppc4xx_gpt_compute_timer(gpt
);
1476 static void ppc4xx_gpt_reset (void *opaque
)
1482 qemu_del_timer(gpt
->timer
);
1483 gpt
->oe
= 0x00000000;
1484 gpt
->ol
= 0x00000000;
1485 gpt
->im
= 0x00000000;
1486 gpt
->is
= 0x00000000;
1487 gpt
->ie
= 0x00000000;
1488 for (i
= 0; i
< 5; i
++) {
1489 gpt
->comp
[i
] = 0x00000000;
1490 gpt
->mask
[i
] = 0x00000000;
1494 static void ppc4xx_gpt_init(target_phys_addr_t base
, qemu_irq irqs
[5])
1500 gpt
= qemu_mallocz(sizeof(ppc4xx_gpt_t
));
1501 for (i
= 0; i
< 5; i
++) {
1502 gpt
->irqs
[i
] = irqs
[i
];
1504 gpt
->timer
= qemu_new_timer_ns(vm_clock
, &ppc4xx_gpt_cb
, gpt
);
1506 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1508 io
= cpu_register_io_memory(gpt_read
, gpt_write
, gpt
, DEVICE_NATIVE_ENDIAN
);
1509 cpu_register_physical_memory(base
, 0x0d4, io
);
1510 qemu_register_reset(ppc4xx_gpt_reset
, gpt
);
1513 /*****************************************************************************/
1519 MAL0_TXCASR
= 0x184,
1520 MAL0_TXCARR
= 0x185,
1521 MAL0_TXEOBISR
= 0x186,
1522 MAL0_TXDEIR
= 0x187,
1523 MAL0_RXCASR
= 0x190,
1524 MAL0_RXCARR
= 0x191,
1525 MAL0_RXEOBISR
= 0x192,
1526 MAL0_RXDEIR
= 0x193,
1527 MAL0_TXCTP0R
= 0x1A0,
1528 MAL0_TXCTP1R
= 0x1A1,
1529 MAL0_TXCTP2R
= 0x1A2,
1530 MAL0_TXCTP3R
= 0x1A3,
1531 MAL0_RXCTP0R
= 0x1C0,
1532 MAL0_RXCTP1R
= 0x1C1,
1537 typedef struct ppc40x_mal_t ppc40x_mal_t
;
1538 struct ppc40x_mal_t
{
1556 static void ppc40x_mal_reset (void *opaque
);
1558 static uint32_t dcr_read_mal (void *opaque
, int dcrn
)
1581 ret
= mal
->txeobisr
;
1593 ret
= mal
->rxeobisr
;
1599 ret
= mal
->txctpr
[0];
1602 ret
= mal
->txctpr
[1];
1605 ret
= mal
->txctpr
[2];
1608 ret
= mal
->txctpr
[3];
1611 ret
= mal
->rxctpr
[0];
1614 ret
= mal
->rxctpr
[1];
1630 static void dcr_write_mal (void *opaque
, int dcrn
, uint32_t val
)
1638 if (val
& 0x80000000)
1639 ppc40x_mal_reset(mal
);
1640 mal
->cfg
= val
& 0x00FFC087;
1647 mal
->ier
= val
& 0x0000001F;
1650 mal
->txcasr
= val
& 0xF0000000;
1653 mal
->txcarr
= val
& 0xF0000000;
1657 mal
->txeobisr
&= ~val
;
1661 mal
->txdeir
&= ~val
;
1664 mal
->rxcasr
= val
& 0xC0000000;
1667 mal
->rxcarr
= val
& 0xC0000000;
1671 mal
->rxeobisr
&= ~val
;
1675 mal
->rxdeir
&= ~val
;
1689 mal
->txctpr
[idx
] = val
;
1697 mal
->rxctpr
[idx
] = val
;
1701 goto update_rx_size
;
1705 mal
->rcbs
[idx
] = val
& 0x000000FF;
1710 static void ppc40x_mal_reset (void *opaque
)
1715 mal
->cfg
= 0x0007C000;
1716 mal
->esr
= 0x00000000;
1717 mal
->ier
= 0x00000000;
1718 mal
->rxcasr
= 0x00000000;
1719 mal
->rxdeir
= 0x00000000;
1720 mal
->rxeobisr
= 0x00000000;
1721 mal
->txcasr
= 0x00000000;
1722 mal
->txdeir
= 0x00000000;
1723 mal
->txeobisr
= 0x00000000;
1726 static void ppc405_mal_init(CPUState
*env
, qemu_irq irqs
[4])
1731 mal
= qemu_mallocz(sizeof(ppc40x_mal_t
));
1732 for (i
= 0; i
< 4; i
++)
1733 mal
->irqs
[i
] = irqs
[i
];
1734 qemu_register_reset(&ppc40x_mal_reset
, mal
);
1735 ppc_dcr_register(env
, MAL0_CFG
,
1736 mal
, &dcr_read_mal
, &dcr_write_mal
);
1737 ppc_dcr_register(env
, MAL0_ESR
,
1738 mal
, &dcr_read_mal
, &dcr_write_mal
);
1739 ppc_dcr_register(env
, MAL0_IER
,
1740 mal
, &dcr_read_mal
, &dcr_write_mal
);
1741 ppc_dcr_register(env
, MAL0_TXCASR
,
1742 mal
, &dcr_read_mal
, &dcr_write_mal
);
1743 ppc_dcr_register(env
, MAL0_TXCARR
,
1744 mal
, &dcr_read_mal
, &dcr_write_mal
);
1745 ppc_dcr_register(env
, MAL0_TXEOBISR
,
1746 mal
, &dcr_read_mal
, &dcr_write_mal
);
1747 ppc_dcr_register(env
, MAL0_TXDEIR
,
1748 mal
, &dcr_read_mal
, &dcr_write_mal
);
1749 ppc_dcr_register(env
, MAL0_RXCASR
,
1750 mal
, &dcr_read_mal
, &dcr_write_mal
);
1751 ppc_dcr_register(env
, MAL0_RXCARR
,
1752 mal
, &dcr_read_mal
, &dcr_write_mal
);
1753 ppc_dcr_register(env
, MAL0_RXEOBISR
,
1754 mal
, &dcr_read_mal
, &dcr_write_mal
);
1755 ppc_dcr_register(env
, MAL0_RXDEIR
,
1756 mal
, &dcr_read_mal
, &dcr_write_mal
);
1757 ppc_dcr_register(env
, MAL0_TXCTP0R
,
1758 mal
, &dcr_read_mal
, &dcr_write_mal
);
1759 ppc_dcr_register(env
, MAL0_TXCTP1R
,
1760 mal
, &dcr_read_mal
, &dcr_write_mal
);
1761 ppc_dcr_register(env
, MAL0_TXCTP2R
,
1762 mal
, &dcr_read_mal
, &dcr_write_mal
);
1763 ppc_dcr_register(env
, MAL0_TXCTP3R
,
1764 mal
, &dcr_read_mal
, &dcr_write_mal
);
1765 ppc_dcr_register(env
, MAL0_RXCTP0R
,
1766 mal
, &dcr_read_mal
, &dcr_write_mal
);
1767 ppc_dcr_register(env
, MAL0_RXCTP1R
,
1768 mal
, &dcr_read_mal
, &dcr_write_mal
);
1769 ppc_dcr_register(env
, MAL0_RCBS0
,
1770 mal
, &dcr_read_mal
, &dcr_write_mal
);
1771 ppc_dcr_register(env
, MAL0_RCBS1
,
1772 mal
, &dcr_read_mal
, &dcr_write_mal
);
1775 /*****************************************************************************/
1777 void ppc40x_core_reset (CPUState
*env
)
1781 printf("Reset PowerPC core\n");
1782 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1787 qemu_system_reset_request();
1789 dbsr
= env
->spr
[SPR_40x_DBSR
];
1790 dbsr
&= ~0x00000300;
1792 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1795 void ppc40x_chip_reset (CPUState
*env
)
1799 printf("Reset PowerPC chip\n");
1800 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1805 qemu_system_reset_request();
1807 /* XXX: TODO reset all internal peripherals */
1808 dbsr
= env
->spr
[SPR_40x_DBSR
];
1809 dbsr
&= ~0x00000300;
1811 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1814 void ppc40x_system_reset (CPUState
*env
)
1816 printf("Reset PowerPC system\n");
1817 qemu_system_reset_request();
1820 void store_40x_dbcr0 (CPUState
*env
, uint32_t val
)
1822 switch ((val
>> 28) & 0x3) {
1828 ppc40x_core_reset(env
);
1832 ppc40x_chip_reset(env
);
1836 ppc40x_system_reset(env
);
1841 /*****************************************************************************/
1844 PPC405CR_CPC0_PLLMR
= 0x0B0,
1845 PPC405CR_CPC0_CR0
= 0x0B1,
1846 PPC405CR_CPC0_CR1
= 0x0B2,
1847 PPC405CR_CPC0_PSR
= 0x0B4,
1848 PPC405CR_CPC0_JTAGID
= 0x0B5,
1849 PPC405CR_CPC0_ER
= 0x0B9,
1850 PPC405CR_CPC0_FR
= 0x0BA,
1851 PPC405CR_CPC0_SR
= 0x0BB,
1855 PPC405CR_CPU_CLK
= 0,
1856 PPC405CR_TMR_CLK
= 1,
1857 PPC405CR_PLB_CLK
= 2,
1858 PPC405CR_SDRAM_CLK
= 3,
1859 PPC405CR_OPB_CLK
= 4,
1860 PPC405CR_EXT_CLK
= 5,
1861 PPC405CR_UART_CLK
= 6,
1862 PPC405CR_CLK_NB
= 7,
1865 typedef struct ppc405cr_cpc_t ppc405cr_cpc_t
;
1866 struct ppc405cr_cpc_t
{
1867 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
1878 static void ppc405cr_clk_setup (ppc405cr_cpc_t
*cpc
)
1880 uint64_t VCO_out
, PLL_out
;
1881 uint32_t CPU_clk
, TMR_clk
, SDRAM_clk
, PLB_clk
, OPB_clk
, EXT_clk
, UART_clk
;
1884 D0
= ((cpc
->pllmr
>> 26) & 0x3) + 1; /* CBDV */
1885 if (cpc
->pllmr
& 0x80000000) {
1886 D1
= (((cpc
->pllmr
>> 20) - 1) & 0xF) + 1; /* FBDV */
1887 D2
= 8 - ((cpc
->pllmr
>> 16) & 0x7); /* FWDVA */
1889 VCO_out
= cpc
->sysclk
* M
;
1890 if (VCO_out
< 400000000 || VCO_out
> 800000000) {
1891 /* PLL cannot lock */
1892 cpc
->pllmr
&= ~0x80000000;
1895 PLL_out
= VCO_out
/ D2
;
1900 PLL_out
= cpc
->sysclk
* M
;
1903 if (cpc
->cr1
& 0x00800000)
1904 TMR_clk
= cpc
->sysclk
; /* Should have a separate clock */
1907 PLB_clk
= CPU_clk
/ D0
;
1908 SDRAM_clk
= PLB_clk
;
1909 D0
= ((cpc
->pllmr
>> 10) & 0x3) + 1;
1910 OPB_clk
= PLB_clk
/ D0
;
1911 D0
= ((cpc
->pllmr
>> 24) & 0x3) + 2;
1912 EXT_clk
= PLB_clk
/ D0
;
1913 D0
= ((cpc
->cr0
>> 1) & 0x1F) + 1;
1914 UART_clk
= CPU_clk
/ D0
;
1915 /* Setup CPU clocks */
1916 clk_setup(&cpc
->clk_setup
[PPC405CR_CPU_CLK
], CPU_clk
);
1917 /* Setup time-base clock */
1918 clk_setup(&cpc
->clk_setup
[PPC405CR_TMR_CLK
], TMR_clk
);
1919 /* Setup PLB clock */
1920 clk_setup(&cpc
->clk_setup
[PPC405CR_PLB_CLK
], PLB_clk
);
1921 /* Setup SDRAM clock */
1922 clk_setup(&cpc
->clk_setup
[PPC405CR_SDRAM_CLK
], SDRAM_clk
);
1923 /* Setup OPB clock */
1924 clk_setup(&cpc
->clk_setup
[PPC405CR_OPB_CLK
], OPB_clk
);
1925 /* Setup external clock */
1926 clk_setup(&cpc
->clk_setup
[PPC405CR_EXT_CLK
], EXT_clk
);
1927 /* Setup UART clock */
1928 clk_setup(&cpc
->clk_setup
[PPC405CR_UART_CLK
], UART_clk
);
1931 static uint32_t dcr_read_crcpc (void *opaque
, int dcrn
)
1933 ppc405cr_cpc_t
*cpc
;
1938 case PPC405CR_CPC0_PLLMR
:
1941 case PPC405CR_CPC0_CR0
:
1944 case PPC405CR_CPC0_CR1
:
1947 case PPC405CR_CPC0_PSR
:
1950 case PPC405CR_CPC0_JTAGID
:
1953 case PPC405CR_CPC0_ER
:
1956 case PPC405CR_CPC0_FR
:
1959 case PPC405CR_CPC0_SR
:
1960 ret
= ~(cpc
->er
| cpc
->fr
) & 0xFFFF0000;
1963 /* Avoid gcc warning */
1971 static void dcr_write_crcpc (void *opaque
, int dcrn
, uint32_t val
)
1973 ppc405cr_cpc_t
*cpc
;
1977 case PPC405CR_CPC0_PLLMR
:
1978 cpc
->pllmr
= val
& 0xFFF77C3F;
1980 case PPC405CR_CPC0_CR0
:
1981 cpc
->cr0
= val
& 0x0FFFFFFE;
1983 case PPC405CR_CPC0_CR1
:
1984 cpc
->cr1
= val
& 0x00800000;
1986 case PPC405CR_CPC0_PSR
:
1989 case PPC405CR_CPC0_JTAGID
:
1992 case PPC405CR_CPC0_ER
:
1993 cpc
->er
= val
& 0xBFFC0000;
1995 case PPC405CR_CPC0_FR
:
1996 cpc
->fr
= val
& 0xBFFC0000;
1998 case PPC405CR_CPC0_SR
:
2004 static void ppc405cr_cpc_reset (void *opaque
)
2006 ppc405cr_cpc_t
*cpc
;
2010 /* Compute PLLMR value from PSR settings */
2011 cpc
->pllmr
= 0x80000000;
2013 switch ((cpc
->psr
>> 30) & 3) {
2016 cpc
->pllmr
&= ~0x80000000;
2020 cpc
->pllmr
|= 5 << 16;
2024 cpc
->pllmr
|= 4 << 16;
2028 cpc
->pllmr
|= 2 << 16;
2032 D
= (cpc
->psr
>> 28) & 3;
2033 cpc
->pllmr
|= (D
+ 1) << 20;
2035 D
= (cpc
->psr
>> 25) & 7;
2050 D
= (cpc
->psr
>> 23) & 3;
2051 cpc
->pllmr
|= D
<< 26;
2053 D
= (cpc
->psr
>> 21) & 3;
2054 cpc
->pllmr
|= D
<< 10;
2056 D
= (cpc
->psr
>> 17) & 3;
2057 cpc
->pllmr
|= D
<< 24;
2058 cpc
->cr0
= 0x0000003C;
2059 cpc
->cr1
= 0x2B0D8800;
2060 cpc
->er
= 0x00000000;
2061 cpc
->fr
= 0x00000000;
2062 ppc405cr_clk_setup(cpc
);
2065 static void ppc405cr_clk_init (ppc405cr_cpc_t
*cpc
)
2069 /* XXX: this should be read from IO pins */
2070 cpc
->psr
= 0x00000000; /* 8 bits ROM */
2072 D
= 0x2; /* Divide by 4 */
2073 cpc
->psr
|= D
<< 30;
2075 D
= 0x1; /* Divide by 2 */
2076 cpc
->psr
|= D
<< 28;
2078 D
= 0x1; /* Divide by 2 */
2079 cpc
->psr
|= D
<< 23;
2081 D
= 0x5; /* M = 16 */
2082 cpc
->psr
|= D
<< 25;
2084 D
= 0x1; /* Divide by 2 */
2085 cpc
->psr
|= D
<< 21;
2087 D
= 0x2; /* Divide by 4 */
2088 cpc
->psr
|= D
<< 17;
2091 static void ppc405cr_cpc_init (CPUState
*env
, clk_setup_t clk_setup
[7],
2094 ppc405cr_cpc_t
*cpc
;
2096 cpc
= qemu_mallocz(sizeof(ppc405cr_cpc_t
));
2097 memcpy(cpc
->clk_setup
, clk_setup
,
2098 PPC405CR_CLK_NB
* sizeof(clk_setup_t
));
2099 cpc
->sysclk
= sysclk
;
2100 cpc
->jtagid
= 0x42051049;
2101 ppc_dcr_register(env
, PPC405CR_CPC0_PSR
, cpc
,
2102 &dcr_read_crcpc
, &dcr_write_crcpc
);
2103 ppc_dcr_register(env
, PPC405CR_CPC0_CR0
, cpc
,
2104 &dcr_read_crcpc
, &dcr_write_crcpc
);
2105 ppc_dcr_register(env
, PPC405CR_CPC0_CR1
, cpc
,
2106 &dcr_read_crcpc
, &dcr_write_crcpc
);
2107 ppc_dcr_register(env
, PPC405CR_CPC0_JTAGID
, cpc
,
2108 &dcr_read_crcpc
, &dcr_write_crcpc
);
2109 ppc_dcr_register(env
, PPC405CR_CPC0_PLLMR
, cpc
,
2110 &dcr_read_crcpc
, &dcr_write_crcpc
);
2111 ppc_dcr_register(env
, PPC405CR_CPC0_ER
, cpc
,
2112 &dcr_read_crcpc
, &dcr_write_crcpc
);
2113 ppc_dcr_register(env
, PPC405CR_CPC0_FR
, cpc
,
2114 &dcr_read_crcpc
, &dcr_write_crcpc
);
2115 ppc_dcr_register(env
, PPC405CR_CPC0_SR
, cpc
,
2116 &dcr_read_crcpc
, &dcr_write_crcpc
);
2117 ppc405cr_clk_init(cpc
);
2118 qemu_register_reset(ppc405cr_cpc_reset
, cpc
);
2121 CPUState
*ppc405cr_init (target_phys_addr_t ram_bases
[4],
2122 target_phys_addr_t ram_sizes
[4],
2123 uint32_t sysclk
, qemu_irq
**picp
,
2126 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
2127 qemu_irq dma_irqs
[4];
2129 qemu_irq
*pic
, *irqs
;
2131 memset(clk_setup
, 0, sizeof(clk_setup
));
2132 env
= ppc4xx_init("405cr", &clk_setup
[PPC405CR_CPU_CLK
],
2133 &clk_setup
[PPC405CR_TMR_CLK
], sysclk
);
2134 /* Memory mapped devices registers */
2136 ppc4xx_plb_init(env
);
2137 /* PLB to OPB bridge */
2138 ppc4xx_pob_init(env
);
2140 ppc4xx_opba_init(0xef600600);
2141 /* Universal interrupt controller */
2142 irqs
= qemu_mallocz(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2143 irqs
[PPCUIC_OUTPUT_INT
] =
2144 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2145 irqs
[PPCUIC_OUTPUT_CINT
] =
2146 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2147 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2149 /* SDRAM controller */
2150 ppc4xx_sdram_init(env
, pic
[14], 1, ram_bases
, ram_sizes
, do_init
);
2151 /* External bus controller */
2152 ppc405_ebc_init(env
);
2153 /* DMA controller */
2154 dma_irqs
[0] = pic
[26];
2155 dma_irqs
[1] = pic
[25];
2156 dma_irqs
[2] = pic
[24];
2157 dma_irqs
[3] = pic
[23];
2158 ppc405_dma_init(env
, dma_irqs
);
2160 if (serial_hds
[0] != NULL
) {
2161 serial_mm_init(0xef600300, 0, pic
[0], PPC_SERIAL_MM_BAUDBASE
,
2162 serial_hds
[0], 1, 1);
2164 if (serial_hds
[1] != NULL
) {
2165 serial_mm_init(0xef600400, 0, pic
[1], PPC_SERIAL_MM_BAUDBASE
,
2166 serial_hds
[1], 1, 1);
2168 /* IIC controller */
2169 ppc405_i2c_init(0xef600500, pic
[2]);
2171 ppc405_gpio_init(0xef600700);
2173 ppc405cr_cpc_init(env
, clk_setup
, sysclk
);
2178 /*****************************************************************************/
2182 PPC405EP_CPC0_PLLMR0
= 0x0F0,
2183 PPC405EP_CPC0_BOOT
= 0x0F1,
2184 PPC405EP_CPC0_EPCTL
= 0x0F3,
2185 PPC405EP_CPC0_PLLMR1
= 0x0F4,
2186 PPC405EP_CPC0_UCR
= 0x0F5,
2187 PPC405EP_CPC0_SRR
= 0x0F6,
2188 PPC405EP_CPC0_JTAGID
= 0x0F7,
2189 PPC405EP_CPC0_PCI
= 0x0F9,
2191 PPC405EP_CPC0_ER
= xxx
,
2192 PPC405EP_CPC0_FR
= xxx
,
2193 PPC405EP_CPC0_SR
= xxx
,
2198 PPC405EP_CPU_CLK
= 0,
2199 PPC405EP_PLB_CLK
= 1,
2200 PPC405EP_OPB_CLK
= 2,
2201 PPC405EP_EBC_CLK
= 3,
2202 PPC405EP_MAL_CLK
= 4,
2203 PPC405EP_PCI_CLK
= 5,
2204 PPC405EP_UART0_CLK
= 6,
2205 PPC405EP_UART1_CLK
= 7,
2206 PPC405EP_CLK_NB
= 8,
2209 typedef struct ppc405ep_cpc_t ppc405ep_cpc_t
;
2210 struct ppc405ep_cpc_t
{
2212 clk_setup_t clk_setup
[PPC405EP_CLK_NB
];
2220 /* Clock and power management */
2226 static void ppc405ep_compute_clocks (ppc405ep_cpc_t
*cpc
)
2228 uint32_t CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
;
2229 uint32_t UART0_clk
, UART1_clk
;
2230 uint64_t VCO_out
, PLL_out
;
2234 if ((cpc
->pllmr
[1] & 0x80000000) && !(cpc
->pllmr
[1] & 0x40000000)) {
2235 M
= (((cpc
->pllmr
[1] >> 20) - 1) & 0xF) + 1; /* FBMUL */
2236 #ifdef DEBUG_CLOCKS_LL
2237 printf("FBMUL %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 20) & 0xF, M
);
2239 D
= 8 - ((cpc
->pllmr
[1] >> 16) & 0x7); /* FWDA */
2240 #ifdef DEBUG_CLOCKS_LL
2241 printf("FWDA %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 16) & 0x7, D
);
2243 VCO_out
= cpc
->sysclk
* M
* D
;
2244 if (VCO_out
< 500000000UL || VCO_out
> 1000000000UL) {
2245 /* Error - unlock the PLL */
2246 printf("VCO out of range %" PRIu64
"\n", VCO_out
);
2248 cpc
->pllmr
[1] &= ~0x80000000;
2252 PLL_out
= VCO_out
/ D
;
2253 /* Pretend the PLL is locked */
2254 cpc
->boot
|= 0x00000001;
2259 PLL_out
= cpc
->sysclk
;
2260 if (cpc
->pllmr
[1] & 0x40000000) {
2261 /* Pretend the PLL is not locked */
2262 cpc
->boot
&= ~0x00000001;
2265 /* Now, compute all other clocks */
2266 D
= ((cpc
->pllmr
[0] >> 20) & 0x3) + 1; /* CCDV */
2267 #ifdef DEBUG_CLOCKS_LL
2268 printf("CCDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 20) & 0x3, D
);
2270 CPU_clk
= PLL_out
/ D
;
2271 D
= ((cpc
->pllmr
[0] >> 16) & 0x3) + 1; /* CBDV */
2272 #ifdef DEBUG_CLOCKS_LL
2273 printf("CBDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 16) & 0x3, D
);
2275 PLB_clk
= CPU_clk
/ D
;
2276 D
= ((cpc
->pllmr
[0] >> 12) & 0x3) + 1; /* OPDV */
2277 #ifdef DEBUG_CLOCKS_LL
2278 printf("OPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 12) & 0x3, D
);
2280 OPB_clk
= PLB_clk
/ D
;
2281 D
= ((cpc
->pllmr
[0] >> 8) & 0x3) + 2; /* EPDV */
2282 #ifdef DEBUG_CLOCKS_LL
2283 printf("EPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 8) & 0x3, D
);
2285 EBC_clk
= PLB_clk
/ D
;
2286 D
= ((cpc
->pllmr
[0] >> 4) & 0x3) + 1; /* MPDV */
2287 #ifdef DEBUG_CLOCKS_LL
2288 printf("MPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 4) & 0x3, D
);
2290 MAL_clk
= PLB_clk
/ D
;
2291 D
= (cpc
->pllmr
[0] & 0x3) + 1; /* PPDV */
2292 #ifdef DEBUG_CLOCKS_LL
2293 printf("PPDV %01" PRIx32
" %d\n", cpc
->pllmr
[0] & 0x3, D
);
2295 PCI_clk
= PLB_clk
/ D
;
2296 D
= ((cpc
->ucr
- 1) & 0x7F) + 1; /* U0DIV */
2297 #ifdef DEBUG_CLOCKS_LL
2298 printf("U0DIV %01" PRIx32
" %d\n", cpc
->ucr
& 0x7F, D
);
2300 UART0_clk
= PLL_out
/ D
;
2301 D
= (((cpc
->ucr
>> 8) - 1) & 0x7F) + 1; /* U1DIV */
2302 #ifdef DEBUG_CLOCKS_LL
2303 printf("U1DIV %01" PRIx32
" %d\n", (cpc
->ucr
>> 8) & 0x7F, D
);
2305 UART1_clk
= PLL_out
/ D
;
2307 printf("Setup PPC405EP clocks - sysclk %" PRIu32
" VCO %" PRIu64
2308 " PLL out %" PRIu64
" Hz\n", cpc
->sysclk
, VCO_out
, PLL_out
);
2309 printf("CPU %" PRIu32
" PLB %" PRIu32
" OPB %" PRIu32
" EBC %" PRIu32
2310 " MAL %" PRIu32
" PCI %" PRIu32
" UART0 %" PRIu32
2311 " UART1 %" PRIu32
"\n",
2312 CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
,
2313 UART0_clk
, UART1_clk
);
2315 /* Setup CPU clocks */
2316 clk_setup(&cpc
->clk_setup
[PPC405EP_CPU_CLK
], CPU_clk
);
2317 /* Setup PLB clock */
2318 clk_setup(&cpc
->clk_setup
[PPC405EP_PLB_CLK
], PLB_clk
);
2319 /* Setup OPB clock */
2320 clk_setup(&cpc
->clk_setup
[PPC405EP_OPB_CLK
], OPB_clk
);
2321 /* Setup external clock */
2322 clk_setup(&cpc
->clk_setup
[PPC405EP_EBC_CLK
], EBC_clk
);
2323 /* Setup MAL clock */
2324 clk_setup(&cpc
->clk_setup
[PPC405EP_MAL_CLK
], MAL_clk
);
2325 /* Setup PCI clock */
2326 clk_setup(&cpc
->clk_setup
[PPC405EP_PCI_CLK
], PCI_clk
);
2327 /* Setup UART0 clock */
2328 clk_setup(&cpc
->clk_setup
[PPC405EP_UART0_CLK
], UART0_clk
);
2329 /* Setup UART1 clock */
2330 clk_setup(&cpc
->clk_setup
[PPC405EP_UART1_CLK
], UART1_clk
);
2333 static uint32_t dcr_read_epcpc (void *opaque
, int dcrn
)
2335 ppc405ep_cpc_t
*cpc
;
2340 case PPC405EP_CPC0_BOOT
:
2343 case PPC405EP_CPC0_EPCTL
:
2346 case PPC405EP_CPC0_PLLMR0
:
2347 ret
= cpc
->pllmr
[0];
2349 case PPC405EP_CPC0_PLLMR1
:
2350 ret
= cpc
->pllmr
[1];
2352 case PPC405EP_CPC0_UCR
:
2355 case PPC405EP_CPC0_SRR
:
2358 case PPC405EP_CPC0_JTAGID
:
2361 case PPC405EP_CPC0_PCI
:
2365 /* Avoid gcc warning */
2373 static void dcr_write_epcpc (void *opaque
, int dcrn
, uint32_t val
)
2375 ppc405ep_cpc_t
*cpc
;
2379 case PPC405EP_CPC0_BOOT
:
2380 /* Read-only register */
2382 case PPC405EP_CPC0_EPCTL
:
2383 /* Don't care for now */
2384 cpc
->epctl
= val
& 0xC00000F3;
2386 case PPC405EP_CPC0_PLLMR0
:
2387 cpc
->pllmr
[0] = val
& 0x00633333;
2388 ppc405ep_compute_clocks(cpc
);
2390 case PPC405EP_CPC0_PLLMR1
:
2391 cpc
->pllmr
[1] = val
& 0xC0F73FFF;
2392 ppc405ep_compute_clocks(cpc
);
2394 case PPC405EP_CPC0_UCR
:
2395 /* UART control - don't care for now */
2396 cpc
->ucr
= val
& 0x003F7F7F;
2398 case PPC405EP_CPC0_SRR
:
2401 case PPC405EP_CPC0_JTAGID
:
2404 case PPC405EP_CPC0_PCI
:
2410 static void ppc405ep_cpc_reset (void *opaque
)
2412 ppc405ep_cpc_t
*cpc
= opaque
;
2414 cpc
->boot
= 0x00000010; /* Boot from PCI - IIC EEPROM disabled */
2415 cpc
->epctl
= 0x00000000;
2416 cpc
->pllmr
[0] = 0x00011010;
2417 cpc
->pllmr
[1] = 0x40000000;
2418 cpc
->ucr
= 0x00000000;
2419 cpc
->srr
= 0x00040000;
2420 cpc
->pci
= 0x00000000;
2421 cpc
->er
= 0x00000000;
2422 cpc
->fr
= 0x00000000;
2423 cpc
->sr
= 0x00000000;
2424 ppc405ep_compute_clocks(cpc
);
2427 /* XXX: sysclk should be between 25 and 100 MHz */
2428 static void ppc405ep_cpc_init (CPUState
*env
, clk_setup_t clk_setup
[8],
2431 ppc405ep_cpc_t
*cpc
;
2433 cpc
= qemu_mallocz(sizeof(ppc405ep_cpc_t
));
2434 memcpy(cpc
->clk_setup
, clk_setup
,
2435 PPC405EP_CLK_NB
* sizeof(clk_setup_t
));
2436 cpc
->jtagid
= 0x20267049;
2437 cpc
->sysclk
= sysclk
;
2438 qemu_register_reset(&ppc405ep_cpc_reset
, cpc
);
2439 ppc_dcr_register(env
, PPC405EP_CPC0_BOOT
, cpc
,
2440 &dcr_read_epcpc
, &dcr_write_epcpc
);
2441 ppc_dcr_register(env
, PPC405EP_CPC0_EPCTL
, cpc
,
2442 &dcr_read_epcpc
, &dcr_write_epcpc
);
2443 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR0
, cpc
,
2444 &dcr_read_epcpc
, &dcr_write_epcpc
);
2445 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR1
, cpc
,
2446 &dcr_read_epcpc
, &dcr_write_epcpc
);
2447 ppc_dcr_register(env
, PPC405EP_CPC0_UCR
, cpc
,
2448 &dcr_read_epcpc
, &dcr_write_epcpc
);
2449 ppc_dcr_register(env
, PPC405EP_CPC0_SRR
, cpc
,
2450 &dcr_read_epcpc
, &dcr_write_epcpc
);
2451 ppc_dcr_register(env
, PPC405EP_CPC0_JTAGID
, cpc
,
2452 &dcr_read_epcpc
, &dcr_write_epcpc
);
2453 ppc_dcr_register(env
, PPC405EP_CPC0_PCI
, cpc
,
2454 &dcr_read_epcpc
, &dcr_write_epcpc
);
2456 ppc_dcr_register(env
, PPC405EP_CPC0_ER
, cpc
,
2457 &dcr_read_epcpc
, &dcr_write_epcpc
);
2458 ppc_dcr_register(env
, PPC405EP_CPC0_FR
, cpc
,
2459 &dcr_read_epcpc
, &dcr_write_epcpc
);
2460 ppc_dcr_register(env
, PPC405EP_CPC0_SR
, cpc
,
2461 &dcr_read_epcpc
, &dcr_write_epcpc
);
2465 CPUState
*ppc405ep_init (target_phys_addr_t ram_bases
[2],
2466 target_phys_addr_t ram_sizes
[2],
2467 uint32_t sysclk
, qemu_irq
**picp
,
2470 clk_setup_t clk_setup
[PPC405EP_CLK_NB
], tlb_clk_setup
;
2471 qemu_irq dma_irqs
[4], gpt_irqs
[5], mal_irqs
[4];
2473 qemu_irq
*pic
, *irqs
;
2475 memset(clk_setup
, 0, sizeof(clk_setup
));
2477 env
= ppc4xx_init("405ep", &clk_setup
[PPC405EP_CPU_CLK
],
2478 &tlb_clk_setup
, sysclk
);
2479 clk_setup
[PPC405EP_CPU_CLK
].cb
= tlb_clk_setup
.cb
;
2480 clk_setup
[PPC405EP_CPU_CLK
].opaque
= tlb_clk_setup
.opaque
;
2481 /* Internal devices init */
2482 /* Memory mapped devices registers */
2484 ppc4xx_plb_init(env
);
2485 /* PLB to OPB bridge */
2486 ppc4xx_pob_init(env
);
2488 ppc4xx_opba_init(0xef600600);
2489 /* Universal interrupt controller */
2490 irqs
= qemu_mallocz(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2491 irqs
[PPCUIC_OUTPUT_INT
] =
2492 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2493 irqs
[PPCUIC_OUTPUT_CINT
] =
2494 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2495 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2497 /* SDRAM controller */
2498 /* XXX 405EP has no ECC interrupt */
2499 ppc4xx_sdram_init(env
, pic
[17], 2, ram_bases
, ram_sizes
, do_init
);
2500 /* External bus controller */
2501 ppc405_ebc_init(env
);
2502 /* DMA controller */
2503 dma_irqs
[0] = pic
[5];
2504 dma_irqs
[1] = pic
[6];
2505 dma_irqs
[2] = pic
[7];
2506 dma_irqs
[3] = pic
[8];
2507 ppc405_dma_init(env
, dma_irqs
);
2508 /* IIC controller */
2509 ppc405_i2c_init(0xef600500, pic
[2]);
2511 ppc405_gpio_init(0xef600700);
2513 if (serial_hds
[0] != NULL
) {
2514 serial_mm_init(0xef600300, 0, pic
[0], PPC_SERIAL_MM_BAUDBASE
,
2515 serial_hds
[0], 1, 1);
2517 if (serial_hds
[1] != NULL
) {
2518 serial_mm_init(0xef600400, 0, pic
[1], PPC_SERIAL_MM_BAUDBASE
,
2519 serial_hds
[1], 1, 1);
2522 ppc405_ocm_init(env
);
2524 gpt_irqs
[0] = pic
[19];
2525 gpt_irqs
[1] = pic
[20];
2526 gpt_irqs
[2] = pic
[21];
2527 gpt_irqs
[3] = pic
[22];
2528 gpt_irqs
[4] = pic
[23];
2529 ppc4xx_gpt_init(0xef600000, gpt_irqs
);
2531 /* Uses pic[3], pic[16], pic[18] */
2533 mal_irqs
[0] = pic
[11];
2534 mal_irqs
[1] = pic
[12];
2535 mal_irqs
[2] = pic
[13];
2536 mal_irqs
[3] = pic
[14];
2537 ppc405_mal_init(env
, mal_irqs
);
2539 /* Uses pic[9], pic[15], pic[17] */
2541 ppc405ep_cpc_init(env
, clk_setup
, sysclk
);