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 target_ulong
dcr_read_plb (void *opaque
, int dcrn
)
127 /* Avoid gcc warning */
135 static void dcr_write_plb (void *opaque
, int dcrn
, target_ulong 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 target_ulong
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
, target_ulong 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 target_ulong
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 */
497 static void dcr_write_ebc (void *opaque
, int dcrn
, target_ulong val
)
508 case 0x00: /* B0CR */
510 case 0x01: /* B1CR */
512 case 0x02: /* B2CR */
514 case 0x03: /* B3CR */
516 case 0x04: /* B4CR */
518 case 0x05: /* B5CR */
520 case 0x06: /* B6CR */
522 case 0x07: /* B7CR */
524 case 0x10: /* B0AP */
526 case 0x11: /* B1AP */
528 case 0x12: /* B2AP */
530 case 0x13: /* B3AP */
532 case 0x14: /* B4AP */
534 case 0x15: /* B5AP */
536 case 0x16: /* B6AP */
538 case 0x17: /* B7AP */
540 case 0x20: /* BEAR */
542 case 0x21: /* BESR0 */
544 case 0x22: /* BESR1 */
557 static void ebc_reset (void *opaque
)
563 ebc
->addr
= 0x00000000;
564 ebc
->bap
[0] = 0x7F8FFE80;
565 ebc
->bcr
[0] = 0xFFE28000;
566 for (i
= 0; i
< 8; i
++) {
567 ebc
->bap
[i
] = 0x00000000;
568 ebc
->bcr
[i
] = 0x00000000;
570 ebc
->besr0
= 0x00000000;
571 ebc
->besr1
= 0x00000000;
572 ebc
->cfg
= 0x80400000;
575 static void ppc405_ebc_init(CPUState
*env
)
579 ebc
= qemu_mallocz(sizeof(ppc4xx_ebc_t
));
580 qemu_register_reset(&ebc_reset
, ebc
);
581 ppc_dcr_register(env
, EBC0_CFGADDR
,
582 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
583 ppc_dcr_register(env
, EBC0_CFGDATA
,
584 ebc
, &dcr_read_ebc
, &dcr_write_ebc
);
587 /*****************************************************************************/
616 typedef struct ppc405_dma_t ppc405_dma_t
;
617 struct ppc405_dma_t
{
630 static target_ulong
dcr_read_dma (void *opaque
, int dcrn
)
639 static void dcr_write_dma (void *opaque
, int dcrn
, target_ulong val
)
646 static void ppc405_dma_reset (void *opaque
)
652 for (i
= 0; i
< 4; i
++) {
653 dma
->cr
[i
] = 0x00000000;
654 dma
->ct
[i
] = 0x00000000;
655 dma
->da
[i
] = 0x00000000;
656 dma
->sa
[i
] = 0x00000000;
657 dma
->sg
[i
] = 0x00000000;
659 dma
->sr
= 0x00000000;
660 dma
->sgc
= 0x00000000;
661 dma
->slp
= 0x7C000000;
662 dma
->pol
= 0x00000000;
665 static void ppc405_dma_init(CPUState
*env
, qemu_irq irqs
[4])
669 dma
= qemu_mallocz(sizeof(ppc405_dma_t
));
670 memcpy(dma
->irqs
, irqs
, 4 * sizeof(qemu_irq
));
671 qemu_register_reset(&ppc405_dma_reset
, dma
);
672 ppc_dcr_register(env
, DMA0_CR0
,
673 dma
, &dcr_read_dma
, &dcr_write_dma
);
674 ppc_dcr_register(env
, DMA0_CT0
,
675 dma
, &dcr_read_dma
, &dcr_write_dma
);
676 ppc_dcr_register(env
, DMA0_DA0
,
677 dma
, &dcr_read_dma
, &dcr_write_dma
);
678 ppc_dcr_register(env
, DMA0_SA0
,
679 dma
, &dcr_read_dma
, &dcr_write_dma
);
680 ppc_dcr_register(env
, DMA0_SG0
,
681 dma
, &dcr_read_dma
, &dcr_write_dma
);
682 ppc_dcr_register(env
, DMA0_CR1
,
683 dma
, &dcr_read_dma
, &dcr_write_dma
);
684 ppc_dcr_register(env
, DMA0_CT1
,
685 dma
, &dcr_read_dma
, &dcr_write_dma
);
686 ppc_dcr_register(env
, DMA0_DA1
,
687 dma
, &dcr_read_dma
, &dcr_write_dma
);
688 ppc_dcr_register(env
, DMA0_SA1
,
689 dma
, &dcr_read_dma
, &dcr_write_dma
);
690 ppc_dcr_register(env
, DMA0_SG1
,
691 dma
, &dcr_read_dma
, &dcr_write_dma
);
692 ppc_dcr_register(env
, DMA0_CR2
,
693 dma
, &dcr_read_dma
, &dcr_write_dma
);
694 ppc_dcr_register(env
, DMA0_CT2
,
695 dma
, &dcr_read_dma
, &dcr_write_dma
);
696 ppc_dcr_register(env
, DMA0_DA2
,
697 dma
, &dcr_read_dma
, &dcr_write_dma
);
698 ppc_dcr_register(env
, DMA0_SA2
,
699 dma
, &dcr_read_dma
, &dcr_write_dma
);
700 ppc_dcr_register(env
, DMA0_SG2
,
701 dma
, &dcr_read_dma
, &dcr_write_dma
);
702 ppc_dcr_register(env
, DMA0_CR3
,
703 dma
, &dcr_read_dma
, &dcr_write_dma
);
704 ppc_dcr_register(env
, DMA0_CT3
,
705 dma
, &dcr_read_dma
, &dcr_write_dma
);
706 ppc_dcr_register(env
, DMA0_DA3
,
707 dma
, &dcr_read_dma
, &dcr_write_dma
);
708 ppc_dcr_register(env
, DMA0_SA3
,
709 dma
, &dcr_read_dma
, &dcr_write_dma
);
710 ppc_dcr_register(env
, DMA0_SG3
,
711 dma
, &dcr_read_dma
, &dcr_write_dma
);
712 ppc_dcr_register(env
, DMA0_SR
,
713 dma
, &dcr_read_dma
, &dcr_write_dma
);
714 ppc_dcr_register(env
, DMA0_SGC
,
715 dma
, &dcr_read_dma
, &dcr_write_dma
);
716 ppc_dcr_register(env
, DMA0_SLP
,
717 dma
, &dcr_read_dma
, &dcr_write_dma
);
718 ppc_dcr_register(env
, DMA0_POL
,
719 dma
, &dcr_read_dma
, &dcr_write_dma
);
722 /*****************************************************************************/
724 typedef struct ppc405_gpio_t ppc405_gpio_t
;
725 struct ppc405_gpio_t
{
739 static uint32_t ppc405_gpio_readb (void *opaque
, target_phys_addr_t addr
)
745 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
751 static void ppc405_gpio_writeb (void *opaque
,
752 target_phys_addr_t addr
, uint32_t value
)
758 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
763 static uint32_t ppc405_gpio_readw (void *opaque
, target_phys_addr_t addr
)
769 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
775 static void ppc405_gpio_writew (void *opaque
,
776 target_phys_addr_t addr
, uint32_t value
)
782 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
787 static uint32_t ppc405_gpio_readl (void *opaque
, target_phys_addr_t addr
)
793 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
799 static void ppc405_gpio_writel (void *opaque
,
800 target_phys_addr_t addr
, uint32_t value
)
806 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
811 static CPUReadMemoryFunc
* const ppc405_gpio_read
[] = {
817 static CPUWriteMemoryFunc
* const ppc405_gpio_write
[] = {
823 static void ppc405_gpio_reset (void *opaque
)
830 static void ppc405_gpio_init(target_phys_addr_t base
)
835 gpio
= qemu_mallocz(sizeof(ppc405_gpio_t
));
837 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
839 io
= cpu_register_io_memory(ppc405_gpio_read
, ppc405_gpio_write
, gpio
);
840 cpu_register_physical_memory(base
, 0x038, io
);
841 qemu_register_reset(&ppc405_gpio_reset
, gpio
);
844 /*****************************************************************************/
848 OCM0_ISACNTL
= 0x019,
850 OCM0_DSACNTL
= 0x01B,
853 typedef struct ppc405_ocm_t ppc405_ocm_t
;
854 struct ppc405_ocm_t
{
862 static void ocm_update_mappings (ppc405_ocm_t
*ocm
,
863 uint32_t isarc
, uint32_t isacntl
,
864 uint32_t dsarc
, uint32_t dsacntl
)
867 printf("OCM update ISA %08" PRIx32
" %08" PRIx32
" (%08" PRIx32
868 " %08" PRIx32
") DSA %08" PRIx32
" %08" PRIx32
869 " (%08" PRIx32
" %08" PRIx32
")\n",
870 isarc
, isacntl
, dsarc
, dsacntl
,
871 ocm
->isarc
, ocm
->isacntl
, ocm
->dsarc
, ocm
->dsacntl
);
873 if (ocm
->isarc
!= isarc
||
874 (ocm
->isacntl
& 0x80000000) != (isacntl
& 0x80000000)) {
875 if (ocm
->isacntl
& 0x80000000) {
876 /* Unmap previously assigned memory region */
877 printf("OCM unmap ISA %08" PRIx32
"\n", ocm
->isarc
);
878 cpu_register_physical_memory(ocm
->isarc
, 0x04000000,
881 if (isacntl
& 0x80000000) {
882 /* Map new instruction memory region */
884 printf("OCM map ISA %08" PRIx32
"\n", isarc
);
886 cpu_register_physical_memory(isarc
, 0x04000000,
887 ocm
->offset
| IO_MEM_RAM
);
890 if (ocm
->dsarc
!= dsarc
||
891 (ocm
->dsacntl
& 0x80000000) != (dsacntl
& 0x80000000)) {
892 if (ocm
->dsacntl
& 0x80000000) {
893 /* Beware not to unmap the region we just mapped */
894 if (!(isacntl
& 0x80000000) || ocm
->dsarc
!= isarc
) {
895 /* Unmap previously assigned memory region */
897 printf("OCM unmap DSA %08" PRIx32
"\n", ocm
->dsarc
);
899 cpu_register_physical_memory(ocm
->dsarc
, 0x04000000,
903 if (dsacntl
& 0x80000000) {
904 /* Beware not to remap the region we just mapped */
905 if (!(isacntl
& 0x80000000) || dsarc
!= isarc
) {
906 /* Map new data memory region */
908 printf("OCM map DSA %08" PRIx32
"\n", dsarc
);
910 cpu_register_physical_memory(dsarc
, 0x04000000,
911 ocm
->offset
| IO_MEM_RAM
);
917 static target_ulong
dcr_read_ocm (void *opaque
, int dcrn
)
944 static void dcr_write_ocm (void *opaque
, int dcrn
, target_ulong val
)
947 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
952 isacntl
= ocm
->isacntl
;
953 dsacntl
= ocm
->dsacntl
;
956 isarc
= val
& 0xFC000000;
959 isacntl
= val
& 0xC0000000;
962 isarc
= val
& 0xFC000000;
965 isacntl
= val
& 0xC0000000;
968 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
971 ocm
->isacntl
= isacntl
;
972 ocm
->dsacntl
= dsacntl
;
975 static void ocm_reset (void *opaque
)
978 uint32_t isarc
, dsarc
, isacntl
, dsacntl
;
982 isacntl
= 0x00000000;
984 dsacntl
= 0x00000000;
985 ocm_update_mappings(ocm
, isarc
, isacntl
, dsarc
, dsacntl
);
988 ocm
->isacntl
= isacntl
;
989 ocm
->dsacntl
= dsacntl
;
992 static void ppc405_ocm_init(CPUState
*env
)
996 ocm
= qemu_mallocz(sizeof(ppc405_ocm_t
));
997 ocm
->offset
= qemu_ram_alloc(4096);
998 qemu_register_reset(&ocm_reset
, ocm
);
999 ppc_dcr_register(env
, OCM0_ISARC
,
1000 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
1001 ppc_dcr_register(env
, OCM0_ISACNTL
,
1002 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
1003 ppc_dcr_register(env
, OCM0_DSARC
,
1004 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
1005 ppc_dcr_register(env
, OCM0_DSACNTL
,
1006 ocm
, &dcr_read_ocm
, &dcr_write_ocm
);
1009 /*****************************************************************************/
1010 /* I2C controller */
1011 typedef struct ppc4xx_i2c_t ppc4xx_i2c_t
;
1012 struct ppc4xx_i2c_t
{
1031 static uint32_t ppc4xx_i2c_readb (void *opaque
, target_phys_addr_t addr
)
1037 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1042 // i2c_readbyte(&i2c->mdata);
1082 ret
= i2c
->xtcntlss
;
1085 ret
= i2c
->directcntl
;
1092 printf("%s: addr " TARGET_FMT_plx
" %02" PRIx32
"\n", __func__
, addr
, ret
);
1098 static void ppc4xx_i2c_writeb (void *opaque
,
1099 target_phys_addr_t addr
, uint32_t value
)
1104 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1111 // i2c_sendbyte(&i2c->mdata);
1126 i2c
->mdcntl
= value
& 0xDF;
1129 i2c
->sts
&= ~(value
& 0x0A);
1132 i2c
->extsts
&= ~(value
& 0x8F);
1141 i2c
->clkdiv
= value
;
1144 i2c
->intrmsk
= value
;
1147 i2c
->xfrcnt
= value
& 0x77;
1150 i2c
->xtcntlss
= value
;
1153 i2c
->directcntl
= value
& 0x7;
1158 static uint32_t ppc4xx_i2c_readw (void *opaque
, target_phys_addr_t addr
)
1163 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1165 ret
= ppc4xx_i2c_readb(opaque
, addr
) << 8;
1166 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 1);
1171 static void ppc4xx_i2c_writew (void *opaque
,
1172 target_phys_addr_t addr
, uint32_t value
)
1175 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1178 ppc4xx_i2c_writeb(opaque
, addr
, value
>> 8);
1179 ppc4xx_i2c_writeb(opaque
, addr
+ 1, value
);
1182 static uint32_t ppc4xx_i2c_readl (void *opaque
, target_phys_addr_t addr
)
1187 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1189 ret
= ppc4xx_i2c_readb(opaque
, addr
) << 24;
1190 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 1) << 16;
1191 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 2) << 8;
1192 ret
|= ppc4xx_i2c_readb(opaque
, addr
+ 3);
1197 static void ppc4xx_i2c_writel (void *opaque
,
1198 target_phys_addr_t addr
, uint32_t value
)
1201 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1204 ppc4xx_i2c_writeb(opaque
, addr
, value
>> 24);
1205 ppc4xx_i2c_writeb(opaque
, addr
+ 1, value
>> 16);
1206 ppc4xx_i2c_writeb(opaque
, addr
+ 2, value
>> 8);
1207 ppc4xx_i2c_writeb(opaque
, addr
+ 3, value
);
1210 static CPUReadMemoryFunc
* const i2c_read
[] = {
1216 static CPUWriteMemoryFunc
* const i2c_write
[] = {
1222 static void ppc4xx_i2c_reset (void *opaque
)
1235 i2c
->directcntl
= 0x0F;
1238 static void ppc405_i2c_init(target_phys_addr_t base
, qemu_irq irq
)
1243 i2c
= qemu_mallocz(sizeof(ppc4xx_i2c_t
));
1246 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1248 io
= cpu_register_io_memory(i2c_read
, i2c_write
, i2c
);
1249 cpu_register_physical_memory(base
, 0x011, io
);
1250 qemu_register_reset(ppc4xx_i2c_reset
, i2c
);
1253 /*****************************************************************************/
1254 /* General purpose timers */
1255 typedef struct ppc4xx_gpt_t ppc4xx_gpt_t
;
1256 struct ppc4xx_gpt_t
{
1259 struct QEMUTimer
*timer
;
1270 static uint32_t ppc4xx_gpt_readb (void *opaque
, target_phys_addr_t addr
)
1273 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1275 /* XXX: generate a bus fault */
1279 static void ppc4xx_gpt_writeb (void *opaque
,
1280 target_phys_addr_t addr
, uint32_t value
)
1283 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1286 /* XXX: generate a bus fault */
1289 static uint32_t ppc4xx_gpt_readw (void *opaque
, target_phys_addr_t addr
)
1292 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1294 /* XXX: generate a bus fault */
1298 static void ppc4xx_gpt_writew (void *opaque
,
1299 target_phys_addr_t addr
, uint32_t value
)
1302 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1305 /* XXX: generate a bus fault */
1308 static int ppc4xx_gpt_compare (ppc4xx_gpt_t
*gpt
, int n
)
1314 static void ppc4xx_gpt_set_output (ppc4xx_gpt_t
*gpt
, int n
, int level
)
1319 static void ppc4xx_gpt_set_outputs (ppc4xx_gpt_t
*gpt
)
1325 for (i
= 0; i
< 5; i
++) {
1326 if (gpt
->oe
& mask
) {
1327 /* Output is enabled */
1328 if (ppc4xx_gpt_compare(gpt
, i
)) {
1329 /* Comparison is OK */
1330 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
);
1332 /* Comparison is KO */
1333 ppc4xx_gpt_set_output(gpt
, i
, gpt
->ol
& mask
? 0 : 1);
1340 static void ppc4xx_gpt_set_irqs (ppc4xx_gpt_t
*gpt
)
1346 for (i
= 0; i
< 5; i
++) {
1347 if (gpt
->is
& gpt
->im
& mask
)
1348 qemu_irq_raise(gpt
->irqs
[i
]);
1350 qemu_irq_lower(gpt
->irqs
[i
]);
1355 static void ppc4xx_gpt_compute_timer (ppc4xx_gpt_t
*gpt
)
1360 static uint32_t ppc4xx_gpt_readl (void *opaque
, target_phys_addr_t addr
)
1367 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1372 /* Time base counter */
1373 ret
= muldiv64(qemu_get_clock(vm_clock
) + gpt
->tb_offset
,
1374 gpt
->tb_freq
, get_ticks_per_sec());
1385 /* Interrupt mask */
1390 /* Interrupt status */
1394 /* Interrupt enable */
1399 idx
= (addr
- 0x80) >> 2;
1400 ret
= gpt
->comp
[idx
];
1404 idx
= (addr
- 0xC0) >> 2;
1405 ret
= gpt
->mask
[idx
];
1415 static void ppc4xx_gpt_writel (void *opaque
,
1416 target_phys_addr_t addr
, uint32_t value
)
1422 printf("%s: addr " TARGET_FMT_plx
" val %08" PRIx32
"\n", __func__
, addr
,
1428 /* Time base counter */
1429 gpt
->tb_offset
= muldiv64(value
, get_ticks_per_sec(), gpt
->tb_freq
)
1430 - qemu_get_clock(vm_clock
);
1431 ppc4xx_gpt_compute_timer(gpt
);
1435 gpt
->oe
= value
& 0xF8000000;
1436 ppc4xx_gpt_set_outputs(gpt
);
1440 gpt
->ol
= value
& 0xF8000000;
1441 ppc4xx_gpt_set_outputs(gpt
);
1444 /* Interrupt mask */
1445 gpt
->im
= value
& 0x0000F800;
1448 /* Interrupt status set */
1449 gpt
->is
|= value
& 0x0000F800;
1450 ppc4xx_gpt_set_irqs(gpt
);
1453 /* Interrupt status clear */
1454 gpt
->is
&= ~(value
& 0x0000F800);
1455 ppc4xx_gpt_set_irqs(gpt
);
1458 /* Interrupt enable */
1459 gpt
->ie
= value
& 0x0000F800;
1460 ppc4xx_gpt_set_irqs(gpt
);
1464 idx
= (addr
- 0x80) >> 2;
1465 gpt
->comp
[idx
] = value
& 0xF8000000;
1466 ppc4xx_gpt_compute_timer(gpt
);
1470 idx
= (addr
- 0xC0) >> 2;
1471 gpt
->mask
[idx
] = value
& 0xF8000000;
1472 ppc4xx_gpt_compute_timer(gpt
);
1477 static CPUReadMemoryFunc
* const gpt_read
[] = {
1483 static CPUWriteMemoryFunc
* const gpt_write
[] = {
1489 static void ppc4xx_gpt_cb (void *opaque
)
1494 ppc4xx_gpt_set_irqs(gpt
);
1495 ppc4xx_gpt_set_outputs(gpt
);
1496 ppc4xx_gpt_compute_timer(gpt
);
1499 static void ppc4xx_gpt_reset (void *opaque
)
1505 qemu_del_timer(gpt
->timer
);
1506 gpt
->oe
= 0x00000000;
1507 gpt
->ol
= 0x00000000;
1508 gpt
->im
= 0x00000000;
1509 gpt
->is
= 0x00000000;
1510 gpt
->ie
= 0x00000000;
1511 for (i
= 0; i
< 5; i
++) {
1512 gpt
->comp
[i
] = 0x00000000;
1513 gpt
->mask
[i
] = 0x00000000;
1517 static void ppc4xx_gpt_init(target_phys_addr_t base
, qemu_irq irqs
[5])
1523 gpt
= qemu_mallocz(sizeof(ppc4xx_gpt_t
));
1524 for (i
= 0; i
< 5; i
++) {
1525 gpt
->irqs
[i
] = irqs
[i
];
1527 gpt
->timer
= qemu_new_timer(vm_clock
, &ppc4xx_gpt_cb
, gpt
);
1529 printf("%s: offset " TARGET_FMT_plx
"\n", __func__
, base
);
1531 io
= cpu_register_io_memory(gpt_read
, gpt_write
, gpt
);
1532 cpu_register_physical_memory(base
, 0x0d4, io
);
1533 qemu_register_reset(ppc4xx_gpt_reset
, gpt
);
1536 /*****************************************************************************/
1542 MAL0_TXCASR
= 0x184,
1543 MAL0_TXCARR
= 0x185,
1544 MAL0_TXEOBISR
= 0x186,
1545 MAL0_TXDEIR
= 0x187,
1546 MAL0_RXCASR
= 0x190,
1547 MAL0_RXCARR
= 0x191,
1548 MAL0_RXEOBISR
= 0x192,
1549 MAL0_RXDEIR
= 0x193,
1550 MAL0_TXCTP0R
= 0x1A0,
1551 MAL0_TXCTP1R
= 0x1A1,
1552 MAL0_TXCTP2R
= 0x1A2,
1553 MAL0_TXCTP3R
= 0x1A3,
1554 MAL0_RXCTP0R
= 0x1C0,
1555 MAL0_RXCTP1R
= 0x1C1,
1560 typedef struct ppc40x_mal_t ppc40x_mal_t
;
1561 struct ppc40x_mal_t
{
1579 static void ppc40x_mal_reset (void *opaque
);
1581 static target_ulong
dcr_read_mal (void *opaque
, int dcrn
)
1604 ret
= mal
->txeobisr
;
1616 ret
= mal
->rxeobisr
;
1622 ret
= mal
->txctpr
[0];
1625 ret
= mal
->txctpr
[1];
1628 ret
= mal
->txctpr
[2];
1631 ret
= mal
->txctpr
[3];
1634 ret
= mal
->rxctpr
[0];
1637 ret
= mal
->rxctpr
[1];
1653 static void dcr_write_mal (void *opaque
, int dcrn
, target_ulong val
)
1661 if (val
& 0x80000000)
1662 ppc40x_mal_reset(mal
);
1663 mal
->cfg
= val
& 0x00FFC087;
1670 mal
->ier
= val
& 0x0000001F;
1673 mal
->txcasr
= val
& 0xF0000000;
1676 mal
->txcarr
= val
& 0xF0000000;
1680 mal
->txeobisr
&= ~val
;
1684 mal
->txdeir
&= ~val
;
1687 mal
->rxcasr
= val
& 0xC0000000;
1690 mal
->rxcarr
= val
& 0xC0000000;
1694 mal
->rxeobisr
&= ~val
;
1698 mal
->rxdeir
&= ~val
;
1712 mal
->txctpr
[idx
] = val
;
1720 mal
->rxctpr
[idx
] = val
;
1724 goto update_rx_size
;
1728 mal
->rcbs
[idx
] = val
& 0x000000FF;
1733 static void ppc40x_mal_reset (void *opaque
)
1738 mal
->cfg
= 0x0007C000;
1739 mal
->esr
= 0x00000000;
1740 mal
->ier
= 0x00000000;
1741 mal
->rxcasr
= 0x00000000;
1742 mal
->rxdeir
= 0x00000000;
1743 mal
->rxeobisr
= 0x00000000;
1744 mal
->txcasr
= 0x00000000;
1745 mal
->txdeir
= 0x00000000;
1746 mal
->txeobisr
= 0x00000000;
1749 static void ppc405_mal_init(CPUState
*env
, qemu_irq irqs
[4])
1754 mal
= qemu_mallocz(sizeof(ppc40x_mal_t
));
1755 for (i
= 0; i
< 4; i
++)
1756 mal
->irqs
[i
] = irqs
[i
];
1757 qemu_register_reset(&ppc40x_mal_reset
, mal
);
1758 ppc_dcr_register(env
, MAL0_CFG
,
1759 mal
, &dcr_read_mal
, &dcr_write_mal
);
1760 ppc_dcr_register(env
, MAL0_ESR
,
1761 mal
, &dcr_read_mal
, &dcr_write_mal
);
1762 ppc_dcr_register(env
, MAL0_IER
,
1763 mal
, &dcr_read_mal
, &dcr_write_mal
);
1764 ppc_dcr_register(env
, MAL0_TXCASR
,
1765 mal
, &dcr_read_mal
, &dcr_write_mal
);
1766 ppc_dcr_register(env
, MAL0_TXCARR
,
1767 mal
, &dcr_read_mal
, &dcr_write_mal
);
1768 ppc_dcr_register(env
, MAL0_TXEOBISR
,
1769 mal
, &dcr_read_mal
, &dcr_write_mal
);
1770 ppc_dcr_register(env
, MAL0_TXDEIR
,
1771 mal
, &dcr_read_mal
, &dcr_write_mal
);
1772 ppc_dcr_register(env
, MAL0_RXCASR
,
1773 mal
, &dcr_read_mal
, &dcr_write_mal
);
1774 ppc_dcr_register(env
, MAL0_RXCARR
,
1775 mal
, &dcr_read_mal
, &dcr_write_mal
);
1776 ppc_dcr_register(env
, MAL0_RXEOBISR
,
1777 mal
, &dcr_read_mal
, &dcr_write_mal
);
1778 ppc_dcr_register(env
, MAL0_RXDEIR
,
1779 mal
, &dcr_read_mal
, &dcr_write_mal
);
1780 ppc_dcr_register(env
, MAL0_TXCTP0R
,
1781 mal
, &dcr_read_mal
, &dcr_write_mal
);
1782 ppc_dcr_register(env
, MAL0_TXCTP1R
,
1783 mal
, &dcr_read_mal
, &dcr_write_mal
);
1784 ppc_dcr_register(env
, MAL0_TXCTP2R
,
1785 mal
, &dcr_read_mal
, &dcr_write_mal
);
1786 ppc_dcr_register(env
, MAL0_TXCTP3R
,
1787 mal
, &dcr_read_mal
, &dcr_write_mal
);
1788 ppc_dcr_register(env
, MAL0_RXCTP0R
,
1789 mal
, &dcr_read_mal
, &dcr_write_mal
);
1790 ppc_dcr_register(env
, MAL0_RXCTP1R
,
1791 mal
, &dcr_read_mal
, &dcr_write_mal
);
1792 ppc_dcr_register(env
, MAL0_RCBS0
,
1793 mal
, &dcr_read_mal
, &dcr_write_mal
);
1794 ppc_dcr_register(env
, MAL0_RCBS1
,
1795 mal
, &dcr_read_mal
, &dcr_write_mal
);
1798 /*****************************************************************************/
1800 void ppc40x_core_reset (CPUState
*env
)
1804 printf("Reset PowerPC core\n");
1805 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1810 qemu_system_reset_request();
1812 dbsr
= env
->spr
[SPR_40x_DBSR
];
1813 dbsr
&= ~0x00000300;
1815 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1818 void ppc40x_chip_reset (CPUState
*env
)
1822 printf("Reset PowerPC chip\n");
1823 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1828 qemu_system_reset_request();
1830 /* XXX: TODO reset all internal peripherals */
1831 dbsr
= env
->spr
[SPR_40x_DBSR
];
1832 dbsr
&= ~0x00000300;
1834 env
->spr
[SPR_40x_DBSR
] = dbsr
;
1837 void ppc40x_system_reset (CPUState
*env
)
1839 printf("Reset PowerPC system\n");
1840 qemu_system_reset_request();
1843 void store_40x_dbcr0 (CPUState
*env
, uint32_t val
)
1845 switch ((val
>> 28) & 0x3) {
1851 ppc40x_core_reset(env
);
1855 ppc40x_chip_reset(env
);
1859 ppc40x_system_reset(env
);
1864 /*****************************************************************************/
1867 PPC405CR_CPC0_PLLMR
= 0x0B0,
1868 PPC405CR_CPC0_CR0
= 0x0B1,
1869 PPC405CR_CPC0_CR1
= 0x0B2,
1870 PPC405CR_CPC0_PSR
= 0x0B4,
1871 PPC405CR_CPC0_JTAGID
= 0x0B5,
1872 PPC405CR_CPC0_ER
= 0x0B9,
1873 PPC405CR_CPC0_FR
= 0x0BA,
1874 PPC405CR_CPC0_SR
= 0x0BB,
1878 PPC405CR_CPU_CLK
= 0,
1879 PPC405CR_TMR_CLK
= 1,
1880 PPC405CR_PLB_CLK
= 2,
1881 PPC405CR_SDRAM_CLK
= 3,
1882 PPC405CR_OPB_CLK
= 4,
1883 PPC405CR_EXT_CLK
= 5,
1884 PPC405CR_UART_CLK
= 6,
1885 PPC405CR_CLK_NB
= 7,
1888 typedef struct ppc405cr_cpc_t ppc405cr_cpc_t
;
1889 struct ppc405cr_cpc_t
{
1890 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
1901 static void ppc405cr_clk_setup (ppc405cr_cpc_t
*cpc
)
1903 uint64_t VCO_out
, PLL_out
;
1904 uint32_t CPU_clk
, TMR_clk
, SDRAM_clk
, PLB_clk
, OPB_clk
, EXT_clk
, UART_clk
;
1907 D0
= ((cpc
->pllmr
>> 26) & 0x3) + 1; /* CBDV */
1908 if (cpc
->pllmr
& 0x80000000) {
1909 D1
= (((cpc
->pllmr
>> 20) - 1) & 0xF) + 1; /* FBDV */
1910 D2
= 8 - ((cpc
->pllmr
>> 16) & 0x7); /* FWDVA */
1912 VCO_out
= cpc
->sysclk
* M
;
1913 if (VCO_out
< 400000000 || VCO_out
> 800000000) {
1914 /* PLL cannot lock */
1915 cpc
->pllmr
&= ~0x80000000;
1918 PLL_out
= VCO_out
/ D2
;
1923 PLL_out
= cpc
->sysclk
* M
;
1926 if (cpc
->cr1
& 0x00800000)
1927 TMR_clk
= cpc
->sysclk
; /* Should have a separate clock */
1930 PLB_clk
= CPU_clk
/ D0
;
1931 SDRAM_clk
= PLB_clk
;
1932 D0
= ((cpc
->pllmr
>> 10) & 0x3) + 1;
1933 OPB_clk
= PLB_clk
/ D0
;
1934 D0
= ((cpc
->pllmr
>> 24) & 0x3) + 2;
1935 EXT_clk
= PLB_clk
/ D0
;
1936 D0
= ((cpc
->cr0
>> 1) & 0x1F) + 1;
1937 UART_clk
= CPU_clk
/ D0
;
1938 /* Setup CPU clocks */
1939 clk_setup(&cpc
->clk_setup
[PPC405CR_CPU_CLK
], CPU_clk
);
1940 /* Setup time-base clock */
1941 clk_setup(&cpc
->clk_setup
[PPC405CR_TMR_CLK
], TMR_clk
);
1942 /* Setup PLB clock */
1943 clk_setup(&cpc
->clk_setup
[PPC405CR_PLB_CLK
], PLB_clk
);
1944 /* Setup SDRAM clock */
1945 clk_setup(&cpc
->clk_setup
[PPC405CR_SDRAM_CLK
], SDRAM_clk
);
1946 /* Setup OPB clock */
1947 clk_setup(&cpc
->clk_setup
[PPC405CR_OPB_CLK
], OPB_clk
);
1948 /* Setup external clock */
1949 clk_setup(&cpc
->clk_setup
[PPC405CR_EXT_CLK
], EXT_clk
);
1950 /* Setup UART clock */
1951 clk_setup(&cpc
->clk_setup
[PPC405CR_UART_CLK
], UART_clk
);
1954 static target_ulong
dcr_read_crcpc (void *opaque
, int dcrn
)
1956 ppc405cr_cpc_t
*cpc
;
1961 case PPC405CR_CPC0_PLLMR
:
1964 case PPC405CR_CPC0_CR0
:
1967 case PPC405CR_CPC0_CR1
:
1970 case PPC405CR_CPC0_PSR
:
1973 case PPC405CR_CPC0_JTAGID
:
1976 case PPC405CR_CPC0_ER
:
1979 case PPC405CR_CPC0_FR
:
1982 case PPC405CR_CPC0_SR
:
1983 ret
= ~(cpc
->er
| cpc
->fr
) & 0xFFFF0000;
1986 /* Avoid gcc warning */
1994 static void dcr_write_crcpc (void *opaque
, int dcrn
, target_ulong val
)
1996 ppc405cr_cpc_t
*cpc
;
2000 case PPC405CR_CPC0_PLLMR
:
2001 cpc
->pllmr
= val
& 0xFFF77C3F;
2003 case PPC405CR_CPC0_CR0
:
2004 cpc
->cr0
= val
& 0x0FFFFFFE;
2006 case PPC405CR_CPC0_CR1
:
2007 cpc
->cr1
= val
& 0x00800000;
2009 case PPC405CR_CPC0_PSR
:
2012 case PPC405CR_CPC0_JTAGID
:
2015 case PPC405CR_CPC0_ER
:
2016 cpc
->er
= val
& 0xBFFC0000;
2018 case PPC405CR_CPC0_FR
:
2019 cpc
->fr
= val
& 0xBFFC0000;
2021 case PPC405CR_CPC0_SR
:
2027 static void ppc405cr_cpc_reset (void *opaque
)
2029 ppc405cr_cpc_t
*cpc
;
2033 /* Compute PLLMR value from PSR settings */
2034 cpc
->pllmr
= 0x80000000;
2036 switch ((cpc
->psr
>> 30) & 3) {
2039 cpc
->pllmr
&= ~0x80000000;
2043 cpc
->pllmr
|= 5 << 16;
2047 cpc
->pllmr
|= 4 << 16;
2051 cpc
->pllmr
|= 2 << 16;
2055 D
= (cpc
->psr
>> 28) & 3;
2056 cpc
->pllmr
|= (D
+ 1) << 20;
2058 D
= (cpc
->psr
>> 25) & 7;
2073 D
= (cpc
->psr
>> 23) & 3;
2074 cpc
->pllmr
|= D
<< 26;
2076 D
= (cpc
->psr
>> 21) & 3;
2077 cpc
->pllmr
|= D
<< 10;
2079 D
= (cpc
->psr
>> 17) & 3;
2080 cpc
->pllmr
|= D
<< 24;
2081 cpc
->cr0
= 0x0000003C;
2082 cpc
->cr1
= 0x2B0D8800;
2083 cpc
->er
= 0x00000000;
2084 cpc
->fr
= 0x00000000;
2085 ppc405cr_clk_setup(cpc
);
2088 static void ppc405cr_clk_init (ppc405cr_cpc_t
*cpc
)
2092 /* XXX: this should be read from IO pins */
2093 cpc
->psr
= 0x00000000; /* 8 bits ROM */
2095 D
= 0x2; /* Divide by 4 */
2096 cpc
->psr
|= D
<< 30;
2098 D
= 0x1; /* Divide by 2 */
2099 cpc
->psr
|= D
<< 28;
2101 D
= 0x1; /* Divide by 2 */
2102 cpc
->psr
|= D
<< 23;
2104 D
= 0x5; /* M = 16 */
2105 cpc
->psr
|= D
<< 25;
2107 D
= 0x1; /* Divide by 2 */
2108 cpc
->psr
|= D
<< 21;
2110 D
= 0x2; /* Divide by 4 */
2111 cpc
->psr
|= D
<< 17;
2114 static void ppc405cr_cpc_init (CPUState
*env
, clk_setup_t clk_setup
[7],
2117 ppc405cr_cpc_t
*cpc
;
2119 cpc
= qemu_mallocz(sizeof(ppc405cr_cpc_t
));
2120 memcpy(cpc
->clk_setup
, clk_setup
,
2121 PPC405CR_CLK_NB
* sizeof(clk_setup_t
));
2122 cpc
->sysclk
= sysclk
;
2123 cpc
->jtagid
= 0x42051049;
2124 ppc_dcr_register(env
, PPC405CR_CPC0_PSR
, cpc
,
2125 &dcr_read_crcpc
, &dcr_write_crcpc
);
2126 ppc_dcr_register(env
, PPC405CR_CPC0_CR0
, cpc
,
2127 &dcr_read_crcpc
, &dcr_write_crcpc
);
2128 ppc_dcr_register(env
, PPC405CR_CPC0_CR1
, cpc
,
2129 &dcr_read_crcpc
, &dcr_write_crcpc
);
2130 ppc_dcr_register(env
, PPC405CR_CPC0_JTAGID
, cpc
,
2131 &dcr_read_crcpc
, &dcr_write_crcpc
);
2132 ppc_dcr_register(env
, PPC405CR_CPC0_PLLMR
, cpc
,
2133 &dcr_read_crcpc
, &dcr_write_crcpc
);
2134 ppc_dcr_register(env
, PPC405CR_CPC0_ER
, cpc
,
2135 &dcr_read_crcpc
, &dcr_write_crcpc
);
2136 ppc_dcr_register(env
, PPC405CR_CPC0_FR
, cpc
,
2137 &dcr_read_crcpc
, &dcr_write_crcpc
);
2138 ppc_dcr_register(env
, PPC405CR_CPC0_SR
, cpc
,
2139 &dcr_read_crcpc
, &dcr_write_crcpc
);
2140 ppc405cr_clk_init(cpc
);
2141 qemu_register_reset(ppc405cr_cpc_reset
, cpc
);
2144 CPUState
*ppc405cr_init (target_phys_addr_t ram_bases
[4],
2145 target_phys_addr_t ram_sizes
[4],
2146 uint32_t sysclk
, qemu_irq
**picp
,
2149 clk_setup_t clk_setup
[PPC405CR_CLK_NB
];
2150 qemu_irq dma_irqs
[4];
2152 qemu_irq
*pic
, *irqs
;
2154 memset(clk_setup
, 0, sizeof(clk_setup
));
2155 env
= ppc4xx_init("405cr", &clk_setup
[PPC405CR_CPU_CLK
],
2156 &clk_setup
[PPC405CR_TMR_CLK
], sysclk
);
2157 /* Memory mapped devices registers */
2159 ppc4xx_plb_init(env
);
2160 /* PLB to OPB bridge */
2161 ppc4xx_pob_init(env
);
2163 ppc4xx_opba_init(0xef600600);
2164 /* Universal interrupt controller */
2165 irqs
= qemu_mallocz(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2166 irqs
[PPCUIC_OUTPUT_INT
] =
2167 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2168 irqs
[PPCUIC_OUTPUT_CINT
] =
2169 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2170 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2172 /* SDRAM controller */
2173 ppc4xx_sdram_init(env
, pic
[14], 1, ram_bases
, ram_sizes
, do_init
);
2174 /* External bus controller */
2175 ppc405_ebc_init(env
);
2176 /* DMA controller */
2177 dma_irqs
[0] = pic
[26];
2178 dma_irqs
[1] = pic
[25];
2179 dma_irqs
[2] = pic
[24];
2180 dma_irqs
[3] = pic
[23];
2181 ppc405_dma_init(env
, dma_irqs
);
2183 if (serial_hds
[0] != NULL
) {
2184 serial_mm_init(0xef600300, 0, pic
[0], PPC_SERIAL_MM_BAUDBASE
,
2187 if (serial_hds
[1] != NULL
) {
2188 serial_mm_init(0xef600400, 0, pic
[1], PPC_SERIAL_MM_BAUDBASE
,
2191 /* IIC controller */
2192 ppc405_i2c_init(0xef600500, pic
[2]);
2194 ppc405_gpio_init(0xef600700);
2196 ppc405cr_cpc_init(env
, clk_setup
, sysclk
);
2201 /*****************************************************************************/
2205 PPC405EP_CPC0_PLLMR0
= 0x0F0,
2206 PPC405EP_CPC0_BOOT
= 0x0F1,
2207 PPC405EP_CPC0_EPCTL
= 0x0F3,
2208 PPC405EP_CPC0_PLLMR1
= 0x0F4,
2209 PPC405EP_CPC0_UCR
= 0x0F5,
2210 PPC405EP_CPC0_SRR
= 0x0F6,
2211 PPC405EP_CPC0_JTAGID
= 0x0F7,
2212 PPC405EP_CPC0_PCI
= 0x0F9,
2214 PPC405EP_CPC0_ER
= xxx
,
2215 PPC405EP_CPC0_FR
= xxx
,
2216 PPC405EP_CPC0_SR
= xxx
,
2221 PPC405EP_CPU_CLK
= 0,
2222 PPC405EP_PLB_CLK
= 1,
2223 PPC405EP_OPB_CLK
= 2,
2224 PPC405EP_EBC_CLK
= 3,
2225 PPC405EP_MAL_CLK
= 4,
2226 PPC405EP_PCI_CLK
= 5,
2227 PPC405EP_UART0_CLK
= 6,
2228 PPC405EP_UART1_CLK
= 7,
2229 PPC405EP_CLK_NB
= 8,
2232 typedef struct ppc405ep_cpc_t ppc405ep_cpc_t
;
2233 struct ppc405ep_cpc_t
{
2235 clk_setup_t clk_setup
[PPC405EP_CLK_NB
];
2243 /* Clock and power management */
2249 static void ppc405ep_compute_clocks (ppc405ep_cpc_t
*cpc
)
2251 uint32_t CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
;
2252 uint32_t UART0_clk
, UART1_clk
;
2253 uint64_t VCO_out
, PLL_out
;
2257 if ((cpc
->pllmr
[1] & 0x80000000) && !(cpc
->pllmr
[1] & 0x40000000)) {
2258 M
= (((cpc
->pllmr
[1] >> 20) - 1) & 0xF) + 1; /* FBMUL */
2259 #ifdef DEBUG_CLOCKS_LL
2260 printf("FBMUL %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 20) & 0xF, M
);
2262 D
= 8 - ((cpc
->pllmr
[1] >> 16) & 0x7); /* FWDA */
2263 #ifdef DEBUG_CLOCKS_LL
2264 printf("FWDA %01" PRIx32
" %d\n", (cpc
->pllmr
[1] >> 16) & 0x7, D
);
2266 VCO_out
= cpc
->sysclk
* M
* D
;
2267 if (VCO_out
< 500000000UL || VCO_out
> 1000000000UL) {
2268 /* Error - unlock the PLL */
2269 printf("VCO out of range %" PRIu64
"\n", VCO_out
);
2271 cpc
->pllmr
[1] &= ~0x80000000;
2275 PLL_out
= VCO_out
/ D
;
2276 /* Pretend the PLL is locked */
2277 cpc
->boot
|= 0x00000001;
2282 PLL_out
= cpc
->sysclk
;
2283 if (cpc
->pllmr
[1] & 0x40000000) {
2284 /* Pretend the PLL is not locked */
2285 cpc
->boot
&= ~0x00000001;
2288 /* Now, compute all other clocks */
2289 D
= ((cpc
->pllmr
[0] >> 20) & 0x3) + 1; /* CCDV */
2290 #ifdef DEBUG_CLOCKS_LL
2291 printf("CCDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 20) & 0x3, D
);
2293 CPU_clk
= PLL_out
/ D
;
2294 D
= ((cpc
->pllmr
[0] >> 16) & 0x3) + 1; /* CBDV */
2295 #ifdef DEBUG_CLOCKS_LL
2296 printf("CBDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 16) & 0x3, D
);
2298 PLB_clk
= CPU_clk
/ D
;
2299 D
= ((cpc
->pllmr
[0] >> 12) & 0x3) + 1; /* OPDV */
2300 #ifdef DEBUG_CLOCKS_LL
2301 printf("OPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 12) & 0x3, D
);
2303 OPB_clk
= PLB_clk
/ D
;
2304 D
= ((cpc
->pllmr
[0] >> 8) & 0x3) + 2; /* EPDV */
2305 #ifdef DEBUG_CLOCKS_LL
2306 printf("EPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 8) & 0x3, D
);
2308 EBC_clk
= PLB_clk
/ D
;
2309 D
= ((cpc
->pllmr
[0] >> 4) & 0x3) + 1; /* MPDV */
2310 #ifdef DEBUG_CLOCKS_LL
2311 printf("MPDV %01" PRIx32
" %d\n", (cpc
->pllmr
[0] >> 4) & 0x3, D
);
2313 MAL_clk
= PLB_clk
/ D
;
2314 D
= (cpc
->pllmr
[0] & 0x3) + 1; /* PPDV */
2315 #ifdef DEBUG_CLOCKS_LL
2316 printf("PPDV %01" PRIx32
" %d\n", cpc
->pllmr
[0] & 0x3, D
);
2318 PCI_clk
= PLB_clk
/ D
;
2319 D
= ((cpc
->ucr
- 1) & 0x7F) + 1; /* U0DIV */
2320 #ifdef DEBUG_CLOCKS_LL
2321 printf("U0DIV %01" PRIx32
" %d\n", cpc
->ucr
& 0x7F, D
);
2323 UART0_clk
= PLL_out
/ D
;
2324 D
= (((cpc
->ucr
>> 8) - 1) & 0x7F) + 1; /* U1DIV */
2325 #ifdef DEBUG_CLOCKS_LL
2326 printf("U1DIV %01" PRIx32
" %d\n", (cpc
->ucr
>> 8) & 0x7F, D
);
2328 UART1_clk
= PLL_out
/ D
;
2330 printf("Setup PPC405EP clocks - sysclk %" PRIu32
" VCO %" PRIu64
2331 " PLL out %" PRIu64
" Hz\n", cpc
->sysclk
, VCO_out
, PLL_out
);
2332 printf("CPU %" PRIu32
" PLB %" PRIu32
" OPB %" PRIu32
" EBC %" PRIu32
2333 " MAL %" PRIu32
" PCI %" PRIu32
" UART0 %" PRIu32
2334 " UART1 %" PRIu32
"\n",
2335 CPU_clk
, PLB_clk
, OPB_clk
, EBC_clk
, MAL_clk
, PCI_clk
,
2336 UART0_clk
, UART1_clk
);
2338 /* Setup CPU clocks */
2339 clk_setup(&cpc
->clk_setup
[PPC405EP_CPU_CLK
], CPU_clk
);
2340 /* Setup PLB clock */
2341 clk_setup(&cpc
->clk_setup
[PPC405EP_PLB_CLK
], PLB_clk
);
2342 /* Setup OPB clock */
2343 clk_setup(&cpc
->clk_setup
[PPC405EP_OPB_CLK
], OPB_clk
);
2344 /* Setup external clock */
2345 clk_setup(&cpc
->clk_setup
[PPC405EP_EBC_CLK
], EBC_clk
);
2346 /* Setup MAL clock */
2347 clk_setup(&cpc
->clk_setup
[PPC405EP_MAL_CLK
], MAL_clk
);
2348 /* Setup PCI clock */
2349 clk_setup(&cpc
->clk_setup
[PPC405EP_PCI_CLK
], PCI_clk
);
2350 /* Setup UART0 clock */
2351 clk_setup(&cpc
->clk_setup
[PPC405EP_UART0_CLK
], UART0_clk
);
2352 /* Setup UART1 clock */
2353 clk_setup(&cpc
->clk_setup
[PPC405EP_UART1_CLK
], UART1_clk
);
2356 static target_ulong
dcr_read_epcpc (void *opaque
, int dcrn
)
2358 ppc405ep_cpc_t
*cpc
;
2363 case PPC405EP_CPC0_BOOT
:
2366 case PPC405EP_CPC0_EPCTL
:
2369 case PPC405EP_CPC0_PLLMR0
:
2370 ret
= cpc
->pllmr
[0];
2372 case PPC405EP_CPC0_PLLMR1
:
2373 ret
= cpc
->pllmr
[1];
2375 case PPC405EP_CPC0_UCR
:
2378 case PPC405EP_CPC0_SRR
:
2381 case PPC405EP_CPC0_JTAGID
:
2384 case PPC405EP_CPC0_PCI
:
2388 /* Avoid gcc warning */
2396 static void dcr_write_epcpc (void *opaque
, int dcrn
, target_ulong val
)
2398 ppc405ep_cpc_t
*cpc
;
2402 case PPC405EP_CPC0_BOOT
:
2403 /* Read-only register */
2405 case PPC405EP_CPC0_EPCTL
:
2406 /* Don't care for now */
2407 cpc
->epctl
= val
& 0xC00000F3;
2409 case PPC405EP_CPC0_PLLMR0
:
2410 cpc
->pllmr
[0] = val
& 0x00633333;
2411 ppc405ep_compute_clocks(cpc
);
2413 case PPC405EP_CPC0_PLLMR1
:
2414 cpc
->pllmr
[1] = val
& 0xC0F73FFF;
2415 ppc405ep_compute_clocks(cpc
);
2417 case PPC405EP_CPC0_UCR
:
2418 /* UART control - don't care for now */
2419 cpc
->ucr
= val
& 0x003F7F7F;
2421 case PPC405EP_CPC0_SRR
:
2424 case PPC405EP_CPC0_JTAGID
:
2427 case PPC405EP_CPC0_PCI
:
2433 static void ppc405ep_cpc_reset (void *opaque
)
2435 ppc405ep_cpc_t
*cpc
= opaque
;
2437 cpc
->boot
= 0x00000010; /* Boot from PCI - IIC EEPROM disabled */
2438 cpc
->epctl
= 0x00000000;
2439 cpc
->pllmr
[0] = 0x00011010;
2440 cpc
->pllmr
[1] = 0x40000000;
2441 cpc
->ucr
= 0x00000000;
2442 cpc
->srr
= 0x00040000;
2443 cpc
->pci
= 0x00000000;
2444 cpc
->er
= 0x00000000;
2445 cpc
->fr
= 0x00000000;
2446 cpc
->sr
= 0x00000000;
2447 ppc405ep_compute_clocks(cpc
);
2450 /* XXX: sysclk should be between 25 and 100 MHz */
2451 static void ppc405ep_cpc_init (CPUState
*env
, clk_setup_t clk_setup
[8],
2454 ppc405ep_cpc_t
*cpc
;
2456 cpc
= qemu_mallocz(sizeof(ppc405ep_cpc_t
));
2457 memcpy(cpc
->clk_setup
, clk_setup
,
2458 PPC405EP_CLK_NB
* sizeof(clk_setup_t
));
2459 cpc
->jtagid
= 0x20267049;
2460 cpc
->sysclk
= sysclk
;
2461 qemu_register_reset(&ppc405ep_cpc_reset
, cpc
);
2462 ppc_dcr_register(env
, PPC405EP_CPC0_BOOT
, cpc
,
2463 &dcr_read_epcpc
, &dcr_write_epcpc
);
2464 ppc_dcr_register(env
, PPC405EP_CPC0_EPCTL
, cpc
,
2465 &dcr_read_epcpc
, &dcr_write_epcpc
);
2466 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR0
, cpc
,
2467 &dcr_read_epcpc
, &dcr_write_epcpc
);
2468 ppc_dcr_register(env
, PPC405EP_CPC0_PLLMR1
, cpc
,
2469 &dcr_read_epcpc
, &dcr_write_epcpc
);
2470 ppc_dcr_register(env
, PPC405EP_CPC0_UCR
, cpc
,
2471 &dcr_read_epcpc
, &dcr_write_epcpc
);
2472 ppc_dcr_register(env
, PPC405EP_CPC0_SRR
, cpc
,
2473 &dcr_read_epcpc
, &dcr_write_epcpc
);
2474 ppc_dcr_register(env
, PPC405EP_CPC0_JTAGID
, cpc
,
2475 &dcr_read_epcpc
, &dcr_write_epcpc
);
2476 ppc_dcr_register(env
, PPC405EP_CPC0_PCI
, cpc
,
2477 &dcr_read_epcpc
, &dcr_write_epcpc
);
2479 ppc_dcr_register(env
, PPC405EP_CPC0_ER
, cpc
,
2480 &dcr_read_epcpc
, &dcr_write_epcpc
);
2481 ppc_dcr_register(env
, PPC405EP_CPC0_FR
, cpc
,
2482 &dcr_read_epcpc
, &dcr_write_epcpc
);
2483 ppc_dcr_register(env
, PPC405EP_CPC0_SR
, cpc
,
2484 &dcr_read_epcpc
, &dcr_write_epcpc
);
2488 CPUState
*ppc405ep_init (target_phys_addr_t ram_bases
[2],
2489 target_phys_addr_t ram_sizes
[2],
2490 uint32_t sysclk
, qemu_irq
**picp
,
2493 clk_setup_t clk_setup
[PPC405EP_CLK_NB
], tlb_clk_setup
;
2494 qemu_irq dma_irqs
[4], gpt_irqs
[5], mal_irqs
[4];
2496 qemu_irq
*pic
, *irqs
;
2498 memset(clk_setup
, 0, sizeof(clk_setup
));
2500 env
= ppc4xx_init("405ep", &clk_setup
[PPC405EP_CPU_CLK
],
2501 &tlb_clk_setup
, sysclk
);
2502 clk_setup
[PPC405EP_CPU_CLK
].cb
= tlb_clk_setup
.cb
;
2503 clk_setup
[PPC405EP_CPU_CLK
].opaque
= tlb_clk_setup
.opaque
;
2504 /* Internal devices init */
2505 /* Memory mapped devices registers */
2507 ppc4xx_plb_init(env
);
2508 /* PLB to OPB bridge */
2509 ppc4xx_pob_init(env
);
2511 ppc4xx_opba_init(0xef600600);
2512 /* Universal interrupt controller */
2513 irqs
= qemu_mallocz(sizeof(qemu_irq
) * PPCUIC_OUTPUT_NB
);
2514 irqs
[PPCUIC_OUTPUT_INT
] =
2515 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
2516 irqs
[PPCUIC_OUTPUT_CINT
] =
2517 ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
2518 pic
= ppcuic_init(env
, irqs
, 0x0C0, 0, 1);
2520 /* SDRAM controller */
2521 /* XXX 405EP has no ECC interrupt */
2522 ppc4xx_sdram_init(env
, pic
[17], 2, ram_bases
, ram_sizes
, do_init
);
2523 /* External bus controller */
2524 ppc405_ebc_init(env
);
2525 /* DMA controller */
2526 dma_irqs
[0] = pic
[5];
2527 dma_irqs
[1] = pic
[6];
2528 dma_irqs
[2] = pic
[7];
2529 dma_irqs
[3] = pic
[8];
2530 ppc405_dma_init(env
, dma_irqs
);
2531 /* IIC controller */
2532 ppc405_i2c_init(0xef600500, pic
[2]);
2534 ppc405_gpio_init(0xef600700);
2536 if (serial_hds
[0] != NULL
) {
2537 serial_mm_init(0xef600300, 0, pic
[0], PPC_SERIAL_MM_BAUDBASE
,
2540 if (serial_hds
[1] != NULL
) {
2541 serial_mm_init(0xef600400, 0, pic
[1], PPC_SERIAL_MM_BAUDBASE
,
2545 ppc405_ocm_init(env
);
2547 gpt_irqs
[0] = pic
[19];
2548 gpt_irqs
[1] = pic
[20];
2549 gpt_irqs
[2] = pic
[21];
2550 gpt_irqs
[3] = pic
[22];
2551 gpt_irqs
[4] = pic
[23];
2552 ppc4xx_gpt_init(0xef600000, gpt_irqs
);
2554 /* Uses pic[3], pic[16], pic[18] */
2556 mal_irqs
[0] = pic
[11];
2557 mal_irqs
[1] = pic
[12];
2558 mal_irqs
[2] = pic
[13];
2559 mal_irqs
[3] = pic
[14];
2560 ppc405_mal_init(env
, mal_irqs
);
2562 /* Uses pic[9], pic[15], pic[17] */
2564 ppc405ep_cpc_init(env
, clk_setup
, sysclk
);