2 * QEMU aCube Sam460ex board emulation
4 * Copyright (c) 2012 François Revol
5 * Copyright (c) 2016-2019 BALATON Zoltan
7 * This file is derived from hw/ppc440_bamboo.c,
8 * the copyright for that material belongs to the original owners.
10 * This work is licensed under the GNU GPL license version 2 or later.
14 #include "qemu/osdep.h"
15 #include "qemu/units.h"
16 #include "qemu-common.h"
17 #include "qemu/error-report.h"
18 #include "qapi/error.h"
20 #include "hw/boards.h"
21 #include "sysemu/kvm.h"
23 #include "sysemu/device_tree.h"
24 #include "sysemu/block-backend.h"
25 #include "hw/loader.h"
27 #include "exec/address-spaces.h"
28 #include "exec/memory.h"
31 #include "hw/block/flash.h"
32 #include "sysemu/sysemu.h"
33 #include "sysemu/qtest.h"
34 #include "hw/sysbus.h"
35 #include "hw/char/serial.h"
36 #include "hw/i2c/ppc4xx_i2c.h"
37 #include "hw/i2c/smbus.h"
38 #include "hw/usb/hcd-ehci.h"
39 #include "hw/ppc/fdt.h"
43 #define BINARY_DEVICE_TREE_FILE "canyonlands.dtb"
44 #define UBOOT_FILENAME "u-boot-sam460-20100605.bin"
45 /* to extract the official U-Boot bin from the updater: */
46 /* dd bs=1 skip=$(($(stat -c '%s' updater/updater-460) - 0x80000)) \
47 if=updater/updater-460 of=u-boot-sam460-20100605.bin */
49 /* from Sam460 U-Boot include/configs/Sam460ex.h */
50 #define FLASH_BASE 0xfff00000
51 #define FLASH_BASE_H 0x4
52 #define FLASH_SIZE (1 * MiB)
53 #define UBOOT_LOAD_BASE 0xfff80000
54 #define UBOOT_SIZE 0x00080000
55 #define UBOOT_ENTRY 0xfffffffc
58 #define EPAPR_MAGIC (0x45504150)
59 #define KERNEL_ADDR 0x1000000
60 #define FDT_ADDR 0x1800000
61 #define RAMDISK_ADDR 0x1900000
66 IRQ2 = PCI_INT (PCIA, PCIB, PCIC, PCIB)
72 #define CPU_FREQ 1150000000
73 #define PLB_FREQ 230000000
74 #define OPB_FREQ 115000000
75 #define EBC_FREQ 115000000
76 #define UART_FREQ 11059200
77 #define SDRAM_NR_BANKS 4
79 /* The SoC could also handle 4 GiB but firmware does not work with that. */
80 /* Maybe it overflows a signed 32 bit number somewhere? */
81 static const ram_addr_t ppc460ex_sdram_bank_sizes
[] = {
82 2 * GiB
, 1 * GiB
, 512 * MiB
, 256 * MiB
, 128 * MiB
, 64 * MiB
,
92 static int sam460ex_load_uboot(void)
95 BlockBackend
*blk
= NULL
;
96 hwaddr base
= FLASH_BASE
| ((hwaddr
)FLASH_BASE_H
<< 32);
97 long bios_size
= FLASH_SIZE
;
100 dinfo
= drive_get(IF_PFLASH
, 0, 0);
102 blk
= blk_by_legacy_dinfo(dinfo
);
103 bios_size
= blk_getlength(blk
);
105 fl_sectors
= (bios_size
+ 65535) >> 16;
107 if (!pflash_cfi01_register(base
, NULL
, "sam460ex.flash", bios_size
,
108 blk
, 64 * KiB
, fl_sectors
,
109 1, 0x89, 0x18, 0x0000, 0x0, 1)) {
110 error_report("Error registering flash memory");
111 /* XXX: return an error instead? */
116 /*error_report("No flash image given with the 'pflash' parameter,"
117 " using default u-boot image");*/
118 base
= UBOOT_LOAD_BASE
| ((hwaddr
)FLASH_BASE_H
<< 32);
119 rom_add_file_fixed(UBOOT_FILENAME
, base
, -1);
125 static int sam460ex_load_device_tree(hwaddr addr
,
129 const char *kernel_cmdline
)
131 uint32_t mem_reg_property
[] = { 0, 0, cpu_to_be32(ramsize
) };
135 uint32_t tb_freq
= CPU_FREQ
;
136 uint32_t clock_freq
= CPU_FREQ
;
139 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, BINARY_DEVICE_TREE_FILE
);
141 error_report("Couldn't find dtb file `%s'", BINARY_DEVICE_TREE_FILE
);
144 fdt
= load_device_tree(filename
, &fdt_size
);
146 error_report("Couldn't load dtb file `%s'", filename
);
152 /* Manipulate device tree in memory. */
154 qemu_fdt_setprop(fdt
, "/memory", "reg", mem_reg_property
,
155 sizeof(mem_reg_property
));
157 /* default FDT doesn't have a /chosen node... */
158 qemu_fdt_add_subnode(fdt
, "/chosen");
160 qemu_fdt_setprop_cell(fdt
, "/chosen", "linux,initrd-start", initrd_base
);
162 qemu_fdt_setprop_cell(fdt
, "/chosen", "linux,initrd-end",
163 (initrd_base
+ initrd_size
));
165 qemu_fdt_setprop_string(fdt
, "/chosen", "bootargs", kernel_cmdline
);
167 /* Copy data from the host device tree into the guest. Since the guest can
168 * directly access the timebase without host involvement, we must expose
169 * the correct frequencies. */
171 tb_freq
= kvmppc_get_tbfreq();
172 clock_freq
= kvmppc_get_clockfreq();
175 qemu_fdt_setprop_cell(fdt
, "/cpus/cpu@0", "clock-frequency",
177 qemu_fdt_setprop_cell(fdt
, "/cpus/cpu@0", "timebase-frequency",
180 /* Remove cpm node if it exists (it is not emulated) */
181 offset
= fdt_path_offset(fdt
, "/cpm");
183 _FDT(fdt_nop_node(fdt
, offset
));
186 /* set serial port clocks */
187 offset
= fdt_node_offset_by_compatible(fdt
, -1, "ns16550");
188 while (offset
>= 0) {
189 _FDT(fdt_setprop_cell(fdt
, offset
, "clock-frequency", UART_FREQ
));
190 offset
= fdt_node_offset_by_compatible(fdt
, offset
, "ns16550");
193 /* some more clocks */
194 qemu_fdt_setprop_cell(fdt
, "/plb", "clock-frequency",
196 qemu_fdt_setprop_cell(fdt
, "/plb/opb", "clock-frequency",
198 qemu_fdt_setprop_cell(fdt
, "/plb/opb/ebc", "clock-frequency",
201 rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE
, fdt
, fdt_size
, addr
);
207 /* Create reset TLB entries for BookE, mapping only the flash memory. */
208 static void mmubooke_create_initial_mapping_uboot(CPUPPCState
*env
)
210 ppcemb_tlb_t
*tlb
= &env
->tlb
.tlbe
[0];
212 /* on reset the flash is mapped by a shadow TLB,
213 * but since we don't implement them we need to use
214 * the same values U-Boot will use to avoid a fault.
217 tlb
->prot
= PAGE_VALID
| ((PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
) << 4);
218 tlb
->size
= 0x10000000; /* up to 0xffffffff */
219 tlb
->EPN
= 0xf0000000 & TARGET_PAGE_MASK
;
220 tlb
->RPN
= (0xf0000000 & TARGET_PAGE_MASK
) | 0x4;
224 /* Create reset TLB entries for BookE, spanning the 32bit addr space. */
225 static void mmubooke_create_initial_mapping(CPUPPCState
*env
,
229 ppcemb_tlb_t
*tlb
= &env
->tlb
.tlbe
[0];
232 tlb
->prot
= PAGE_VALID
| ((PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
) << 4);
233 tlb
->size
= 1 << 31; /* up to 0x80000000 */
234 tlb
->EPN
= va
& TARGET_PAGE_MASK
;
235 tlb
->RPN
= pa
& TARGET_PAGE_MASK
;
239 static void main_cpu_reset(void *opaque
)
241 PowerPCCPU
*cpu
= opaque
;
242 CPUPPCState
*env
= &cpu
->env
;
243 struct boot_info
*bi
= env
->load_info
;
247 /* either we have a kernel to boot or we jump to U-Boot */
248 if (bi
->entry
!= UBOOT_ENTRY
) {
249 env
->gpr
[1] = (16 * MiB
) - 8;
250 env
->gpr
[3] = FDT_ADDR
;
251 env
->nip
= bi
->entry
;
253 /* Create a mapping for the kernel. */
254 mmubooke_create_initial_mapping(env
, 0, 0);
255 env
->gpr
[6] = tswap32(EPAPR_MAGIC
);
256 env
->gpr
[7] = (16 * MiB
) - 8; /* bi->ima_size; */
259 env
->nip
= UBOOT_ENTRY
;
260 mmubooke_create_initial_mapping_uboot(env
);
264 static void sam460ex_init(MachineState
*machine
)
266 MemoryRegion
*address_space_mem
= get_system_memory();
267 MemoryRegion
*isa
= g_new(MemoryRegion
, 1);
268 MemoryRegion
*ram_memories
= g_new(MemoryRegion
, SDRAM_NR_BANKS
);
269 hwaddr ram_bases
[SDRAM_NR_BANKS
] = {0};
270 hwaddr ram_sizes
[SDRAM_NR_BANKS
] = {0};
271 MemoryRegion
*l2cache_ram
= g_new(MemoryRegion
, 1);
272 qemu_irq
*irqs
, *uic
[4];
277 hwaddr entry
= UBOOT_ENTRY
;
278 hwaddr loadaddr
= LOAD_UIMAGE_LOADADDR_INVALID
;
279 target_long initrd_size
= 0;
282 struct boot_info
*boot_info
;
287 cpu
= POWERPC_CPU(cpu_create(machine
->cpu_type
));
289 if (env
->mmu_model
!= POWERPC_MMU_BOOKE
) {
290 error_report("Only MMU model BookE is supported by this machine.");
294 qemu_register_reset(main_cpu_reset
, cpu
);
295 boot_info
= g_malloc0(sizeof(*boot_info
));
296 env
->load_info
= boot_info
;
298 ppc_booke_timers_init(cpu
, CPU_FREQ
, 0);
299 ppc_dcr_init(env
, NULL
, NULL
);
302 ppc4xx_plb_init(env
);
304 /* interrupt controllers */
305 irqs
= g_new0(qemu_irq
, PPCUIC_OUTPUT_NB
);
306 irqs
[PPCUIC_OUTPUT_INT
] = ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_INT
];
307 irqs
[PPCUIC_OUTPUT_CINT
] = ((qemu_irq
*)env
->irq_inputs
)[PPC40x_INPUT_CINT
];
308 uic
[0] = ppcuic_init(env
, irqs
, 0xc0, 0, 1);
309 uic
[1] = ppcuic_init(env
, &uic
[0][30], 0xd0, 0, 1);
310 uic
[2] = ppcuic_init(env
, &uic
[0][10], 0xe0, 0, 1);
311 uic
[3] = ppcuic_init(env
, &uic
[0][16], 0xf0, 0, 1);
313 /* SDRAM controller */
314 /* put all RAM on first bank because board has one slot
315 * and firmware only checks that */
316 machine
->ram_size
= ppc4xx_sdram_adjust(machine
->ram_size
, 1,
317 ram_memories
, ram_bases
, ram_sizes
,
318 ppc460ex_sdram_bank_sizes
);
320 /* FIXME: does 460EX have ECC interrupts? */
321 ppc440_sdram_init(env
, SDRAM_NR_BANKS
, ram_memories
,
322 ram_bases
, ram_sizes
, 1);
324 /* IIC controllers and devices */
325 dev
= sysbus_create_simple(TYPE_PPC4xx_I2C
, 0x4ef600700, uic
[0][2]);
326 i2c
= PPC4xx_I2C(dev
)->bus
;
327 /* SPD EEPROM on RAM module */
328 spd_data
= spd_data_generate(DDR2
, ram_sizes
[0], &err
);
330 warn_report_err(err
);
333 spd_data
[20] = 4; /* SO-DIMM module */
334 smbus_eeprom_init_one(i2c
, 0x50, spd_data
);
337 i2c_create_slave(i2c
, "m41t80", 0x68);
339 dev
= sysbus_create_simple(TYPE_PPC4xx_I2C
, 0x4ef600800, uic
[0][3]);
341 /* External bus controller */
342 ppc405_ebc_init(env
);
345 ppc4xx_cpr_init(env
);
347 /* PLB to AHB bridge */
348 ppc4xx_ahb_init(env
);
351 ppc4xx_sdr_init(env
);
354 ppc4xx_mal_init(env
, 4, 16, &uic
[2][3]);
357 ppc4xx_dma_init(env
, 0x200);
359 /* 256K of L2 cache as memory */
360 ppc4xx_l2sram_init(env
);
361 /* FIXME: remove this after fixing l2sram mapping in ppc440_uc.c? */
362 memory_region_init_ram(l2cache_ram
, NULL
, "ppc440.l2cache_ram", 256 * KiB
,
364 memory_region_add_subregion(address_space_mem
, 0x400000000LL
, l2cache_ram
);
367 sysbus_create_simple(TYPE_PPC4xx_EHCI
, 0x4bffd0400, uic
[2][29]);
368 dev
= qdev_create(NULL
, "sysbus-ohci");
369 qdev_prop_set_string(dev
, "masterbus", "usb-bus.0");
370 qdev_prop_set_uint32(dev
, "num-ports", 6);
371 qdev_init_nofail(dev
);
372 sbdev
= SYS_BUS_DEVICE(dev
);
373 sysbus_mmio_map(sbdev
, 0, 0x4bffd0000);
374 sysbus_connect_irq(sbdev
, 0, uic
[2][30]);
375 usb_create_simple(usb_bus_find(-1), "usb-kbd");
376 usb_create_simple(usb_bus_find(-1), "usb-mouse");
379 ppc460ex_pcie_init(env
);
380 /* All PCI irqs are connected to the same UIC pin (cf. UBoot source) */
381 dev
= sysbus_create_simple("ppc440-pcix-host", 0xc0ec00000, uic
[1][0]);
382 pci_bus
= (PCIBus
*)qdev_get_child_bus(dev
, "pci.0");
384 error_report("couldn't create PCI controller!");
387 memory_region_init_alias(isa
, NULL
, "isa_mmio", get_system_io(),
389 memory_region_add_subregion(get_system_memory(), 0xc08000000, isa
);
392 pci_create_simple(pci_bus
, PCI_DEVFN(6, 0), "sm501");
393 /* SoC has a single SATA port but we don't emulate that yet
394 * However, firmware and usual clients have driver for SiI311x
395 * so add one for convenience by default */
396 if (defaults_enabled()) {
397 pci_create_simple(pci_bus
, -1, "sii3112");
401 * but board has only one wired and two are present in fdt */
402 if (serial_hd(0) != NULL
) {
403 serial_mm_init(address_space_mem
, 0x4ef600300, 0, uic
[1][1],
404 PPC_SERIAL_MM_BAUDBASE
, serial_hd(0),
407 if (serial_hd(1) != NULL
) {
408 serial_mm_init(address_space_mem
, 0x4ef600400, 0, uic
[0][1],
409 PPC_SERIAL_MM_BAUDBASE
, serial_hd(1),
413 /* Load U-Boot image. */
414 if (!machine
->kernel_filename
) {
415 success
= sam460ex_load_uboot();
417 error_report("could not load firmware");
423 if (machine
->kernel_filename
) {
424 success
= load_uimage(machine
->kernel_filename
, &entry
, &loadaddr
,
427 uint64_t elf_entry
, elf_lowaddr
;
429 success
= load_elf(machine
->kernel_filename
, NULL
, NULL
, &elf_entry
,
430 &elf_lowaddr
, NULL
, 1, PPC_ELF_MACHINE
, 0, 0);
432 loadaddr
= elf_lowaddr
;
434 /* XXX try again as binary */
436 error_report("could not load kernel '%s'",
437 machine
->kernel_filename
);
443 if (machine
->initrd_filename
) {
444 initrd_size
= load_image_targphys(machine
->initrd_filename
,
446 machine
->ram_size
- RAMDISK_ADDR
);
447 if (initrd_size
< 0) {
448 error_report("could not load ram disk '%s' at %x",
449 machine
->initrd_filename
, RAMDISK_ADDR
);
454 /* If we're loading a kernel directly, we must load the device tree too. */
455 if (machine
->kernel_filename
) {
458 dt_size
= sam460ex_load_device_tree(FDT_ADDR
, machine
->ram_size
,
459 RAMDISK_ADDR
, initrd_size
,
460 machine
->kernel_cmdline
);
462 boot_info
->dt_base
= FDT_ADDR
;
463 boot_info
->dt_size
= dt_size
;
466 boot_info
->entry
= entry
;
469 static void sam460ex_machine_init(MachineClass
*mc
)
471 mc
->desc
= "aCube Sam460ex";
472 mc
->init
= sam460ex_init
;
473 mc
->default_cpu_type
= POWERPC_CPU_TYPE_NAME("460exb");
474 mc
->default_ram_size
= 512 * MiB
;
477 DEFINE_MACHINE("sam460ex", sam460ex_machine_init
)