]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/sam460ex.c
ppc4xx: Use ram_addr_t in ppc4xx_sdram_adjust()
[mirror_qemu.git] / hw / ppc / sam460ex.c
CommitLineData
4b387f9e
BZ
1/*
2 * QEMU aCube Sam460ex board emulation
3 *
4 * Copyright (c) 2012 François Revol
08fd9917 5 * Copyright (c) 2016-2019 BALATON Zoltan
4b387f9e
BZ
6 *
7 * This file is derived from hw/ppc440_bamboo.c,
8 * the copyright for that material belongs to the original owners.
9 *
10 * This work is licensed under the GNU GPL license version 2 or later.
11 *
12 */
13
14#include "qemu/osdep.h"
ab3dd749 15#include "qemu/units.h"
4b387f9e 16#include "qemu-common.h"
4b387f9e
BZ
17#include "qemu/error-report.h"
18#include "qapi/error.h"
19#include "hw/hw.h"
4b387f9e
BZ
20#include "hw/boards.h"
21#include "sysemu/kvm.h"
22#include "kvm_ppc.h"
23#include "sysemu/device_tree.h"
24#include "sysemu/block-backend.h"
25#include "hw/loader.h"
26#include "elf.h"
27#include "exec/address-spaces.h"
28#include "exec/memory.h"
72a56a1f
MT
29#include "ppc440.h"
30#include "ppc405.h"
4b387f9e
BZ
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"
ad633de6 39#include "hw/ppc/fdt.h"
4b387f9e 40
43f7868d
GR
41#include <libfdt.h>
42
4b387f9e
BZ
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 */
48
49/* from Sam460 U-Boot include/configs/Sam460ex.h */
50#define FLASH_BASE 0xfff00000
51#define FLASH_BASE_H 0x4
ab3dd749 52#define FLASH_SIZE (1 * MiB)
4b387f9e
BZ
53#define UBOOT_LOAD_BASE 0xfff80000
54#define UBOOT_SIZE 0x00080000
55#define UBOOT_ENTRY 0xfffffffc
56
57/* from U-Boot */
58#define EPAPR_MAGIC (0x45504150)
59#define KERNEL_ADDR 0x1000000
60#define FDT_ADDR 0x1800000
61#define RAMDISK_ADDR 0x1900000
62
63/* Sam460ex IRQ MAP:
64 IRQ0 = ETH_INT
65 IRQ1 = FPGA_INT
66 IRQ2 = PCI_INT (PCIA, PCIB, PCIC, PCIB)
67 IRQ3 = FPGA_INT2
68 IRQ11 = RTC_INT
69 IRQ12 = SM502_INT
70*/
71
f8815532 72#define CPU_FREQ 1150000000
43f7868d
GR
73#define PLB_FREQ 230000000
74#define OPB_FREQ 115000000
75#define EBC_FREQ 115000000
76#define UART_FREQ 11059200
4b387f9e
BZ
77#define SDRAM_NR_BANKS 4
78
79/* FIXME: See u-boot.git 8ac41e, also fix in ppc440_uc.c */
7d8ccf58 80static const ram_addr_t ppc460ex_sdram_bank_sizes[] = {
ab3dd749 81 1 * GiB, 512 * MiB, 256 * MiB, 128 * MiB, 64 * MiB, 32 * MiB, 0
4b387f9e
BZ
82};
83
84struct boot_info {
85 uint32_t dt_base;
86 uint32_t dt_size;
87 uint32_t entry;
88};
89
4b387f9e
BZ
90static int sam460ex_load_uboot(void)
91{
92 DriveInfo *dinfo;
93 BlockBackend *blk = NULL;
94 hwaddr base = FLASH_BASE | ((hwaddr)FLASH_BASE_H << 32);
95 long bios_size = FLASH_SIZE;
96 int fl_sectors;
97
98 dinfo = drive_get(IF_PFLASH, 0, 0);
99 if (dinfo) {
100 blk = blk_by_legacy_dinfo(dinfo);
101 bios_size = blk_getlength(blk);
102 }
103 fl_sectors = (bios_size + 65535) >> 16;
104
105 if (!pflash_cfi01_register(base, NULL, "sam460ex.flash", bios_size,
ab3dd749 106 blk, 64 * KiB, fl_sectors,
4b387f9e 107 1, 0x89, 0x18, 0x0000, 0x0, 1)) {
371b74e2 108 error_report("Error registering flash memory");
4b387f9e
BZ
109 /* XXX: return an error instead? */
110 exit(1);
111 }
112
113 if (!blk) {
114 /*error_report("No flash image given with the 'pflash' parameter,"
115 " using default u-boot image");*/
116 base = UBOOT_LOAD_BASE | ((hwaddr)FLASH_BASE_H << 32);
117 rom_add_file_fixed(UBOOT_FILENAME, base, -1);
118 }
119
120 return 0;
121}
122
123static int sam460ex_load_device_tree(hwaddr addr,
124 uint32_t ramsize,
125 hwaddr initrd_base,
126 hwaddr initrd_size,
127 const char *kernel_cmdline)
128{
4b387f9e
BZ
129 uint32_t mem_reg_property[] = { 0, 0, cpu_to_be32(ramsize) };
130 char *filename;
131 int fdt_size;
132 void *fdt;
f8815532
BZ
133 uint32_t tb_freq = CPU_FREQ;
134 uint32_t clock_freq = CPU_FREQ;
43f7868d 135 int offset;
4b387f9e
BZ
136
137 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
138 if (!filename) {
51b0d834
DG
139 error_report("Couldn't find dtb file `%s'", BINARY_DEVICE_TREE_FILE);
140 exit(1);
4b387f9e
BZ
141 }
142 fdt = load_device_tree(filename, &fdt_size);
51b0d834
DG
143 if (!fdt) {
144 error_report("Couldn't load dtb file `%s'", filename);
3cc702d6 145 g_free(filename);
51b0d834 146 exit(1);
4b387f9e 147 }
3cc702d6 148 g_free(filename);
4b387f9e
BZ
149
150 /* Manipulate device tree in memory. */
151
e753f331
DG
152 qemu_fdt_setprop(fdt, "/memory", "reg", mem_reg_property,
153 sizeof(mem_reg_property));
4b387f9e
BZ
154
155 /* default FDT doesn't have a /chosen node... */
156 qemu_fdt_add_subnode(fdt, "/chosen");
157
e753f331 158 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start", initrd_base);
4b387f9e 159
e753f331
DG
160 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
161 (initrd_base + initrd_size));
4b387f9e 162
e753f331 163 qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", kernel_cmdline);
4b387f9e
BZ
164
165 /* Copy data from the host device tree into the guest. Since the guest can
166 * directly access the timebase without host involvement, we must expose
167 * the correct frequencies. */
168 if (kvm_enabled()) {
169 tb_freq = kvmppc_get_tbfreq();
170 clock_freq = kvmppc_get_clockfreq();
171 }
172
173 qemu_fdt_setprop_cell(fdt, "/cpus/cpu@0", "clock-frequency",
174 clock_freq);
175 qemu_fdt_setprop_cell(fdt, "/cpus/cpu@0", "timebase-frequency",
176 tb_freq);
177
43f7868d
GR
178 /* Remove cpm node if it exists (it is not emulated) */
179 offset = fdt_path_offset(fdt, "/cpm");
180 if (offset >= 0) {
ad633de6 181 _FDT(fdt_nop_node(fdt, offset));
43f7868d
GR
182 }
183
184 /* set serial port clocks */
185 offset = fdt_node_offset_by_compatible(fdt, -1, "ns16550");
186 while (offset >= 0) {
ad633de6 187 _FDT(fdt_setprop_cell(fdt, offset, "clock-frequency", UART_FREQ));
43f7868d
GR
188 offset = fdt_node_offset_by_compatible(fdt, offset, "ns16550");
189 }
190
191 /* some more clocks */
192 qemu_fdt_setprop_cell(fdt, "/plb", "clock-frequency",
193 PLB_FREQ);
194 qemu_fdt_setprop_cell(fdt, "/plb/opb", "clock-frequency",
195 OPB_FREQ);
196 qemu_fdt_setprop_cell(fdt, "/plb/opb/ebc", "clock-frequency",
197 EBC_FREQ);
198
4b387f9e
BZ
199 rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
200 g_free(fdt);
4b387f9e 201
51b0d834 202 return fdt_size;
4b387f9e
BZ
203}
204
205/* Create reset TLB entries for BookE, mapping only the flash memory. */
206static void mmubooke_create_initial_mapping_uboot(CPUPPCState *env)
207{
208 ppcemb_tlb_t *tlb = &env->tlb.tlbe[0];
209
210 /* on reset the flash is mapped by a shadow TLB,
211 * but since we don't implement them we need to use
212 * the same values U-Boot will use to avoid a fault.
213 */
214 tlb->attr = 0;
215 tlb->prot = PAGE_VALID | ((PAGE_READ | PAGE_WRITE | PAGE_EXEC) << 4);
216 tlb->size = 0x10000000; /* up to 0xffffffff */
217 tlb->EPN = 0xf0000000 & TARGET_PAGE_MASK;
218 tlb->RPN = (0xf0000000 & TARGET_PAGE_MASK) | 0x4;
219 tlb->PID = 0;
220}
221
222/* Create reset TLB entries for BookE, spanning the 32bit addr space. */
223static void mmubooke_create_initial_mapping(CPUPPCState *env,
224 target_ulong va,
225 hwaddr pa)
226{
227 ppcemb_tlb_t *tlb = &env->tlb.tlbe[0];
228
229 tlb->attr = 0;
230 tlb->prot = PAGE_VALID | ((PAGE_READ | PAGE_WRITE | PAGE_EXEC) << 4);
231 tlb->size = 1 << 31; /* up to 0x80000000 */
232 tlb->EPN = va & TARGET_PAGE_MASK;
233 tlb->RPN = pa & TARGET_PAGE_MASK;
234 tlb->PID = 0;
235}
236
237static void main_cpu_reset(void *opaque)
238{
239 PowerPCCPU *cpu = opaque;
240 CPUPPCState *env = &cpu->env;
241 struct boot_info *bi = env->load_info;
242
243 cpu_reset(CPU(cpu));
244
245 /* either we have a kernel to boot or we jump to U-Boot */
246 if (bi->entry != UBOOT_ENTRY) {
ab3dd749 247 env->gpr[1] = (16 * MiB) - 8;
4b387f9e
BZ
248 env->gpr[3] = FDT_ADDR;
249 env->nip = bi->entry;
250
251 /* Create a mapping for the kernel. */
252 mmubooke_create_initial_mapping(env, 0, 0);
253 env->gpr[6] = tswap32(EPAPR_MAGIC);
ab3dd749 254 env->gpr[7] = (16 * MiB) - 8; /* bi->ima_size; */
4b387f9e
BZ
255
256 } else {
257 env->nip = UBOOT_ENTRY;
258 mmubooke_create_initial_mapping_uboot(env);
259 }
260}
261
262static void sam460ex_init(MachineState *machine)
263{
264 MemoryRegion *address_space_mem = get_system_memory();
265 MemoryRegion *isa = g_new(MemoryRegion, 1);
266 MemoryRegion *ram_memories = g_new(MemoryRegion, SDRAM_NR_BANKS);
08fd9917
BZ
267 hwaddr ram_bases[SDRAM_NR_BANKS] = {0};
268 hwaddr ram_sizes[SDRAM_NR_BANKS] = {0};
4b387f9e
BZ
269 MemoryRegion *l2cache_ram = g_new(MemoryRegion, 1);
270 qemu_irq *irqs, *uic[4];
271 PCIBus *pci_bus;
272 PowerPCCPU *cpu;
273 CPUPPCState *env;
08fd9917 274 I2CBus *i2c;
4b387f9e 275 hwaddr entry = UBOOT_ENTRY;
f831f955 276 hwaddr loadaddr = LOAD_UIMAGE_LOADADDR_INVALID;
4b387f9e
BZ
277 target_long initrd_size = 0;
278 DeviceState *dev;
279 SysBusDevice *sbdev;
4b387f9e 280 struct boot_info *boot_info;
08fd9917
BZ
281 uint8_t *spd_data;
282 Error *err = NULL;
283 int success;
4b387f9e
BZ
284
285 cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
286 env = &cpu->env;
287 if (env->mmu_model != POWERPC_MMU_BOOKE) {
288 error_report("Only MMU model BookE is supported by this machine.");
289 exit(1);
290 }
291
4b387f9e
BZ
292 qemu_register_reset(main_cpu_reset, cpu);
293 boot_info = g_malloc0(sizeof(*boot_info));
294 env->load_info = boot_info;
295
f8815532 296 ppc_booke_timers_init(cpu, CPU_FREQ, 0);
4b387f9e
BZ
297 ppc_dcr_init(env, NULL, NULL);
298
299 /* PLB arbitrer */
300 ppc4xx_plb_init(env);
301
302 /* interrupt controllers */
0989e6d1 303 irqs = g_new0(qemu_irq, PPCUIC_OUTPUT_NB);
4b387f9e
BZ
304 irqs[PPCUIC_OUTPUT_INT] = ((qemu_irq *)env->irq_inputs)[PPC40x_INPUT_INT];
305 irqs[PPCUIC_OUTPUT_CINT] = ((qemu_irq *)env->irq_inputs)[PPC40x_INPUT_CINT];
306 uic[0] = ppcuic_init(env, irqs, 0xc0, 0, 1);
307 uic[1] = ppcuic_init(env, &uic[0][30], 0xd0, 0, 1);
308 uic[2] = ppcuic_init(env, &uic[0][10], 0xe0, 0, 1);
309 uic[3] = ppcuic_init(env, &uic[0][16], 0xf0, 0, 1);
310
311 /* SDRAM controller */
4b387f9e
BZ
312 /* put all RAM on first bank because board has one slot
313 * and firmware only checks that */
314 machine->ram_size = ppc4xx_sdram_adjust(machine->ram_size, 1,
315 ram_memories, ram_bases, ram_sizes,
316 ppc460ex_sdram_bank_sizes);
317
318 /* FIXME: does 460EX have ECC interrupts? */
319 ppc440_sdram_init(env, SDRAM_NR_BANKS, ram_memories,
320 ram_bases, ram_sizes, 1);
321
08fd9917 322 /* IIC controllers and devices */
4b387f9e 323 dev = sysbus_create_simple(TYPE_PPC4xx_I2C, 0x4ef600700, uic[0][2]);
08fd9917
BZ
324 i2c = PPC4xx_I2C(dev)->bus;
325 /* SPD EEPROM on RAM module */
326 spd_data = spd_data_generate(DDR2, ram_sizes[0], &err);
327 if (err) {
328 warn_report_err(err);
329 }
330 if (spd_data) {
331 spd_data[20] = 4; /* SO-DIMM module */
332 smbus_eeprom_init_one(i2c, 0x50, spd_data);
333 }
334 /* RTC */
335 i2c_create_slave(i2c, "m41t80", 0x68);
4b387f9e
BZ
336
337 dev = sysbus_create_simple(TYPE_PPC4xx_I2C, 0x4ef600800, uic[0][3]);
4b387f9e
BZ
338
339 /* External bus controller */
340 ppc405_ebc_init(env);
341
342 /* CPR */
343 ppc4xx_cpr_init(env);
344
345 /* PLB to AHB bridge */
346 ppc4xx_ahb_init(env);
347
348 /* System DCRs */
349 ppc4xx_sdr_init(env);
350
351 /* MAL */
352 ppc4xx_mal_init(env, 4, 16, &uic[2][3]);
353
3c409c19
BZ
354 /* DMA */
355 ppc4xx_dma_init(env, 0x200);
356
4b387f9e
BZ
357 /* 256K of L2 cache as memory */
358 ppc4xx_l2sram_init(env);
359 /* FIXME: remove this after fixing l2sram mapping in ppc440_uc.c? */
ab3dd749 360 memory_region_init_ram(l2cache_ram, NULL, "ppc440.l2cache_ram", 256 * KiB,
4b387f9e
BZ
361 &error_abort);
362 memory_region_add_subregion(address_space_mem, 0x400000000LL, l2cache_ram);
363
364 /* USB */
365 sysbus_create_simple(TYPE_PPC4xx_EHCI, 0x4bffd0400, uic[2][29]);
366 dev = qdev_create(NULL, "sysbus-ohci");
367 qdev_prop_set_string(dev, "masterbus", "usb-bus.0");
368 qdev_prop_set_uint32(dev, "num-ports", 6);
369 qdev_init_nofail(dev);
370 sbdev = SYS_BUS_DEVICE(dev);
371 sysbus_mmio_map(sbdev, 0, 0x4bffd0000);
372 sysbus_connect_irq(sbdev, 0, uic[2][30]);
373 usb_create_simple(usb_bus_find(-1), "usb-kbd");
374 usb_create_simple(usb_bus_find(-1), "usb-mouse");
375
376 /* PCI bus */
377 ppc460ex_pcie_init(env);
6484ab3d
BZ
378 /* All PCI irqs are connected to the same UIC pin (cf. UBoot source) */
379 dev = sysbus_create_simple("ppc440-pcix-host", 0xc0ec00000, uic[1][0]);
4b387f9e
BZ
380 pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci.0");
381 if (!pci_bus) {
382 error_report("couldn't create PCI controller!");
383 exit(1);
384 }
385 memory_region_init_alias(isa, NULL, "isa_mmio", get_system_io(),
386 0, 0x10000);
387 memory_region_add_subregion(get_system_memory(), 0xc08000000, isa);
388
389 /* PCI devices */
390 pci_create_simple(pci_bus, PCI_DEVFN(6, 0), "sm501");
391 /* SoC has a single SATA port but we don't emulate that yet
392 * However, firmware and usual clients have driver for SiI311x
393 * so add one for convenience by default */
394 if (defaults_enabled()) {
395 pci_create_simple(pci_bus, -1, "sii3112");
396 }
397
398 /* SoC has 4 UARTs
399 * but board has only one wired and two are present in fdt */
9bca0edb 400 if (serial_hd(0) != NULL) {
4b387f9e 401 serial_mm_init(address_space_mem, 0x4ef600300, 0, uic[1][1],
9bca0edb 402 PPC_SERIAL_MM_BAUDBASE, serial_hd(0),
4b387f9e
BZ
403 DEVICE_BIG_ENDIAN);
404 }
9bca0edb 405 if (serial_hd(1) != NULL) {
4b387f9e 406 serial_mm_init(address_space_mem, 0x4ef600400, 0, uic[0][1],
9bca0edb 407 PPC_SERIAL_MM_BAUDBASE, serial_hd(1),
4b387f9e
BZ
408 DEVICE_BIG_ENDIAN);
409 }
410
411 /* Load U-Boot image. */
412 if (!machine->kernel_filename) {
413 success = sam460ex_load_uboot();
414 if (success < 0) {
371b74e2 415 error_report("could not load firmware");
4b387f9e
BZ
416 exit(1);
417 }
418 }
419
420 /* Load kernel. */
421 if (machine->kernel_filename) {
422 success = load_uimage(machine->kernel_filename, &entry, &loadaddr,
423 NULL, NULL, NULL);
424 if (success < 0) {
425 uint64_t elf_entry, elf_lowaddr;
426
427 success = load_elf(machine->kernel_filename, NULL, NULL, &elf_entry,
428 &elf_lowaddr, NULL, 1, PPC_ELF_MACHINE, 0, 0);
429 entry = elf_entry;
430 loadaddr = elf_lowaddr;
431 }
432 /* XXX try again as binary */
433 if (success < 0) {
371b74e2 434 error_report("could not load kernel '%s'",
4b387f9e
BZ
435 machine->kernel_filename);
436 exit(1);
437 }
438 }
439
440 /* Load initrd. */
441 if (machine->initrd_filename) {
442 initrd_size = load_image_targphys(machine->initrd_filename,
443 RAMDISK_ADDR,
444 machine->ram_size - RAMDISK_ADDR);
445 if (initrd_size < 0) {
371b74e2 446 error_report("could not load ram disk '%s' at %x",
4b387f9e
BZ
447 machine->initrd_filename, RAMDISK_ADDR);
448 exit(1);
449 }
450 }
451
452 /* If we're loading a kernel directly, we must load the device tree too. */
453 if (machine->kernel_filename) {
454 int dt_size;
455
456 dt_size = sam460ex_load_device_tree(FDT_ADDR, machine->ram_size,
457 RAMDISK_ADDR, initrd_size,
458 machine->kernel_cmdline);
4b387f9e
BZ
459
460 boot_info->dt_base = FDT_ADDR;
461 boot_info->dt_size = dt_size;
462 }
463
464 boot_info->entry = entry;
465}
466
467static void sam460ex_machine_init(MachineClass *mc)
468{
469 mc->desc = "aCube Sam460ex";
470 mc->init = sam460ex_init;
471 mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("460exb");
d23b6caa 472 mc->default_ram_size = 512 * MiB;
4b387f9e
BZ
473}
474
475DEFINE_MACHINE("sam460ex", sam460ex_machine_init)