xref: /openbmc/qemu/hw/ppc/sam460ex.c (revision e018489d8b4c1d85a3851fbe48b0befd2ccfc647)
1 /*
2  * QEMU aCube Sam460ex board emulation
3  *
4  * Copyright (c) 2012 François Revol
5  * Copyright (c) 2016-2019 BALATON Zoltan
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"
15 #include "qemu/units.h"
16 #include "qemu/datadir.h"
17 #include "qemu/error-report.h"
18 #include "qapi/error.h"
19 #include "hw/boards.h"
20 #include "sysemu/kvm.h"
21 #include "kvm_ppc.h"
22 #include "sysemu/device_tree.h"
23 #include "sysemu/block-backend.h"
24 #include "hw/loader.h"
25 #include "elf.h"
26 #include "exec/memory.h"
27 #include "ppc440.h"
28 #include "hw/block/flash.h"
29 #include "sysemu/sysemu.h"
30 #include "sysemu/reset.h"
31 #include "hw/sysbus.h"
32 #include "hw/char/serial.h"
33 #include "hw/i2c/ppc4xx_i2c.h"
34 #include "hw/i2c/smbus_eeprom.h"
35 #include "hw/usb/hcd-ehci.h"
36 #include "hw/ppc/fdt.h"
37 #include "hw/qdev-properties.h"
38 #include "hw/intc/ppc-uic.h"
39 
40 #include <libfdt.h>
41 
42 #define BINARY_DEVICE_TREE_FILE "canyonlands.dtb"
43 #define UBOOT_FILENAME "u-boot-sam460-20100605.bin"
44 /* to extract the official U-Boot bin from the updater: */
45 /* dd bs=1 skip=$(($(stat -c '%s' updater/updater-460) - 0x80000)) \
46      if=updater/updater-460 of=u-boot-sam460-20100605.bin */
47 
48 /* from Sam460 U-Boot include/configs/Sam460ex.h */
49 #define FLASH_BASE             0xfff00000
50 #define FLASH_BASE_H           0x4
51 #define FLASH_SIZE             (1 * MiB)
52 #define UBOOT_LOAD_BASE        0xfff80000
53 #define UBOOT_SIZE             0x00080000
54 #define UBOOT_ENTRY            0xfffffffc
55 
56 /* from U-Boot */
57 #define EPAPR_MAGIC           (0x45504150)
58 #define KERNEL_ADDR           0x1000000
59 #define FDT_ADDR              0x1800000
60 #define RAMDISK_ADDR          0x1900000
61 
62 /* Sam460ex IRQ MAP:
63    IRQ0  = ETH_INT
64    IRQ1  = FPGA_INT
65    IRQ2  = PCI_INT (PCIA, PCIB, PCIC, PCIB)
66    IRQ3  = FPGA_INT2
67    IRQ11 = RTC_INT
68    IRQ12 = SM502_INT
69 */
70 
71 #define CPU_FREQ 1150000000
72 #define PLB_FREQ 230000000
73 #define OPB_FREQ 115000000
74 #define EBC_FREQ 115000000
75 #define UART_FREQ 11059200
76 
77 struct boot_info {
78     uint32_t dt_base;
79     uint32_t dt_size;
80     uint32_t entry;
81 };
82 
83 static int sam460ex_load_uboot(void)
84 {
85     /*
86      * This first creates 1MiB of flash memory mapped at the end of
87      * the 32-bit address space (0xFFF00000..0xFFFFFFFF).
88      *
89      * If_PFLASH unit 0 is defined, the flash memory is initialized
90      * from that block backend.
91      *
92      * Else, it's initialized to zero.  And then 512KiB of ROM get
93      * mapped on top of its second half (0xFFF80000..0xFFFFFFFF),
94      * initialized from u-boot-sam460-20100605.bin.
95      *
96      * This doesn't smell right.
97      *
98      * The physical hardware appears to have 512KiB flash memory.
99      *
100      * TODO Figure out what we really need here, and clean this up.
101      */
102 
103     DriveInfo *dinfo;
104 
105     dinfo = drive_get(IF_PFLASH, 0, 0);
106     if (!pflash_cfi01_register(FLASH_BASE | ((hwaddr)FLASH_BASE_H << 32),
107                                "sam460ex.flash", FLASH_SIZE,
108                                dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
109                                64 * KiB, 1, 0x89, 0x18, 0x0000, 0x0, 1)) {
110         error_report("Error registering flash memory");
111         /* XXX: return an error instead? */
112         exit(1);
113     }
114 
115     if (!dinfo) {
116         /*error_report("No flash image given with the 'pflash' parameter,"
117                 " using default u-boot image");*/
118         rom_add_file_fixed(UBOOT_FILENAME,
119                            UBOOT_LOAD_BASE | ((hwaddr)FLASH_BASE_H << 32),
120                            -1);
121     }
122 
123     return 0;
124 }
125 
126 static int sam460ex_load_device_tree(hwaddr addr,
127                                      uint32_t ramsize,
128                                      hwaddr initrd_base,
129                                      hwaddr initrd_size,
130                                      const char *kernel_cmdline)
131 {
132     uint32_t mem_reg_property[] = { 0, 0, cpu_to_be32(ramsize) };
133     char *filename;
134     int fdt_size;
135     void *fdt;
136     uint32_t tb_freq = CPU_FREQ;
137     uint32_t clock_freq = CPU_FREQ;
138     int offset;
139 
140     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
141     if (!filename) {
142         error_report("Couldn't find dtb file `%s'", BINARY_DEVICE_TREE_FILE);
143         exit(1);
144     }
145     fdt = load_device_tree(filename, &fdt_size);
146     if (!fdt) {
147         error_report("Couldn't load dtb file `%s'", filename);
148         g_free(filename);
149         exit(1);
150     }
151     g_free(filename);
152 
153     /* Manipulate device tree in memory. */
154 
155     qemu_fdt_setprop(fdt, "/memory", "reg", mem_reg_property,
156                      sizeof(mem_reg_property));
157 
158     /* default FDT doesn't have a /chosen node... */
159     qemu_fdt_add_subnode(fdt, "/chosen");
160 
161     qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start", initrd_base);
162 
163     qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
164                           (initrd_base + initrd_size));
165 
166     qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", kernel_cmdline);
167 
168     /* Copy data from the host device tree into the guest. Since the guest can
169      * directly access the timebase without host involvement, we must expose
170      * the correct frequencies. */
171     if (kvm_enabled()) {
172         tb_freq = kvmppc_get_tbfreq();
173         clock_freq = kvmppc_get_clockfreq();
174     }
175 
176     qemu_fdt_setprop_cell(fdt, "/cpus/cpu@0", "clock-frequency",
177                               clock_freq);
178     qemu_fdt_setprop_cell(fdt, "/cpus/cpu@0", "timebase-frequency",
179                               tb_freq);
180 
181     /* Remove cpm node if it exists (it is not emulated) */
182     offset = fdt_path_offset(fdt, "/cpm");
183     if (offset >= 0) {
184         _FDT(fdt_nop_node(fdt, offset));
185     }
186 
187     /* set serial port clocks */
188     offset = fdt_node_offset_by_compatible(fdt, -1, "ns16550");
189     while (offset >= 0) {
190         _FDT(fdt_setprop_cell(fdt, offset, "clock-frequency", UART_FREQ));
191         offset = fdt_node_offset_by_compatible(fdt, offset, "ns16550");
192     }
193 
194     /* some more clocks */
195     qemu_fdt_setprop_cell(fdt, "/plb", "clock-frequency",
196                               PLB_FREQ);
197     qemu_fdt_setprop_cell(fdt, "/plb/opb", "clock-frequency",
198                               OPB_FREQ);
199     qemu_fdt_setprop_cell(fdt, "/plb/opb/ebc", "clock-frequency",
200                               EBC_FREQ);
201 
202     rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
203     g_free(fdt);
204 
205     return fdt_size;
206 }
207 
208 /* Create reset TLB entries for BookE, mapping only the flash memory.  */
209 static void mmubooke_create_initial_mapping_uboot(CPUPPCState *env)
210 {
211     ppcemb_tlb_t *tlb = &env->tlb.tlbe[0];
212 
213     /* on reset the flash is mapped by a shadow TLB,
214      * but since we don't implement them we need to use
215      * the same values U-Boot will use to avoid a fault.
216      */
217     tlb->attr = 0;
218     tlb->prot = PAGE_VALID | ((PAGE_READ | PAGE_WRITE | PAGE_EXEC) << 4);
219     tlb->size = 0x10000000; /* up to 0xffffffff  */
220     tlb->EPN = 0xf0000000 & TARGET_PAGE_MASK;
221     tlb->RPN = (0xf0000000 & TARGET_PAGE_MASK) | 0x4;
222     tlb->PID = 0;
223 }
224 
225 /* Create reset TLB entries for BookE, spanning the 32bit addr space.  */
226 static void mmubooke_create_initial_mapping(CPUPPCState *env,
227                                      target_ulong va,
228                                      hwaddr pa)
229 {
230     ppcemb_tlb_t *tlb = &env->tlb.tlbe[0];
231 
232     tlb->attr = 0;
233     tlb->prot = PAGE_VALID | ((PAGE_READ | PAGE_WRITE | PAGE_EXEC) << 4);
234     tlb->size = 1 << 31; /* up to 0x80000000  */
235     tlb->EPN = va & TARGET_PAGE_MASK;
236     tlb->RPN = pa & TARGET_PAGE_MASK;
237     tlb->PID = 0;
238 }
239 
240 static void main_cpu_reset(void *opaque)
241 {
242     PowerPCCPU *cpu = opaque;
243     CPUPPCState *env = &cpu->env;
244     struct boot_info *bi = env->load_info;
245 
246     cpu_reset(CPU(cpu));
247 
248     /* either we have a kernel to boot or we jump to U-Boot */
249     if (bi->entry != UBOOT_ENTRY) {
250         env->gpr[1] = (16 * MiB) - 8;
251         env->gpr[3] = FDT_ADDR;
252         env->nip = bi->entry;
253 
254         /* Create a mapping for the kernel.  */
255         mmubooke_create_initial_mapping(env, 0, 0);
256         env->gpr[6] = tswap32(EPAPR_MAGIC);
257         env->gpr[7] = (16 * MiB) - 8; /* bi->ima_size; */
258 
259     } else {
260         env->nip = UBOOT_ENTRY;
261         mmubooke_create_initial_mapping_uboot(env);
262     }
263 }
264 
265 static void sam460ex_init(MachineState *machine)
266 {
267     MemoryRegion *address_space_mem = get_system_memory();
268     MemoryRegion *isa = g_new(MemoryRegion, 1);
269     MemoryRegion *l2cache_ram = g_new(MemoryRegion, 1);
270     DeviceState *uic[4];
271     int i;
272     PCIBus *pci_bus;
273     PowerPCCPU *cpu;
274     CPUPPCState *env;
275     I2CBus *i2c;
276     hwaddr entry = UBOOT_ENTRY;
277     target_long initrd_size = 0;
278     DeviceState *dev;
279     SysBusDevice *sbdev;
280     struct boot_info *boot_info;
281     uint8_t *spd_data;
282     int success;
283 
284     cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
285     env = &cpu->env;
286     if (env->mmu_model != POWERPC_MMU_BOOKE) {
287         error_report("Only MMU model BookE is supported by this machine.");
288         exit(1);
289     }
290 
291     qemu_register_reset(main_cpu_reset, cpu);
292     boot_info = g_malloc0(sizeof(*boot_info));
293     env->load_info = boot_info;
294 
295     ppc_booke_timers_init(cpu, CPU_FREQ, 0);
296     ppc_dcr_init(env, NULL, NULL);
297 
298     /* PLB arbitrer */
299     dev = qdev_new(TYPE_PPC4xx_PLB);
300     ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(dev), cpu, &error_fatal);
301     object_unref(OBJECT(dev));
302 
303     /* interrupt controllers */
304     for (i = 0; i < ARRAY_SIZE(uic); i++) {
305         /*
306          * UICs 1, 2 and 3 are cascaded through UIC 0.
307          * input_ints[n] is the interrupt number on UIC 0 which
308          * the INT output of UIC n is connected to. The CINT output
309          * of UIC n connects to input_ints[n] + 1.
310          * The entry in input_ints[] for UIC 0 is ignored, because UIC 0's
311          * INT and CINT outputs are connected to the CPU.
312          */
313         const int input_ints[] = { -1, 30, 10, 16 };
314 
315         uic[i] = qdev_new(TYPE_PPC_UIC);
316         qdev_prop_set_uint32(uic[i], "dcr-base", 0xc0 + i * 0x10);
317         ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(uic[i]), cpu, &error_fatal);
318         object_unref(OBJECT(uic[i]));
319 
320         sbdev = SYS_BUS_DEVICE(uic[i]);
321         if (i == 0) {
322             sysbus_connect_irq(sbdev, PPCUIC_OUTPUT_INT,
323                              qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_INT));
324             sysbus_connect_irq(sbdev, PPCUIC_OUTPUT_CINT,
325                              qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_CINT));
326         } else {
327             sysbus_connect_irq(sbdev, PPCUIC_OUTPUT_INT,
328                                qdev_get_gpio_in(uic[0], input_ints[i]));
329             sysbus_connect_irq(sbdev, PPCUIC_OUTPUT_CINT,
330                                qdev_get_gpio_in(uic[0], input_ints[i] + 1));
331         }
332     }
333 
334     /* SDRAM controller */
335     /* The SoC could also handle 4 GiB but firmware does not work with that. */
336     if (machine->ram_size > 2 * GiB) {
337         error_report("Memory over 2 GiB is not supported");
338         exit(1);
339     }
340     /* Firmware needs at least 64 MiB */
341     if (machine->ram_size < 64 * MiB) {
342         error_report("Memory below 64 MiB is not supported");
343         exit(1);
344     }
345     dev = qdev_new(TYPE_PPC4xx_SDRAM_DDR2);
346     object_property_set_link(OBJECT(dev), "dram", OBJECT(machine->ram),
347                              &error_abort);
348     /*
349      * Put all RAM on first bank because board has one slot
350      * and firmware only checks that
351      */
352     object_property_set_int(OBJECT(dev), "nbanks", 1, &error_abort);
353     ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(dev), cpu, &error_fatal);
354     object_unref(OBJECT(dev));
355     /* FIXME: does 460EX have ECC interrupts? */
356     /* Enable SDRAM memory regions as we may boot without firmware */
357     ppc4xx_sdram_ddr2_enable(PPC4xx_SDRAM_DDR2(dev));
358 
359     /* IIC controllers and devices */
360     dev = sysbus_create_simple(TYPE_PPC4xx_I2C, 0x4ef600700,
361                                qdev_get_gpio_in(uic[0], 2));
362     i2c = PPC4xx_I2C(dev)->bus;
363     /* SPD EEPROM on RAM module */
364     spd_data = spd_data_generate(machine->ram_size < 128 * MiB ? DDR : DDR2,
365                                  machine->ram_size);
366     spd_data[20] = 4; /* SO-DIMM module */
367     smbus_eeprom_init_one(i2c, 0x50, spd_data);
368     /* RTC */
369     i2c_slave_create_simple(i2c, "m41t80", 0x68);
370 
371     dev = sysbus_create_simple(TYPE_PPC4xx_I2C, 0x4ef600800,
372                                qdev_get_gpio_in(uic[0], 3));
373 
374     /* External bus controller */
375     dev = qdev_new(TYPE_PPC4xx_EBC);
376     ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(dev), cpu, &error_fatal);
377     object_unref(OBJECT(dev));
378 
379     /* CPR */
380     ppc4xx_cpr_init(env);
381 
382     /* PLB to AHB bridge */
383     ppc4xx_ahb_init(env);
384 
385     /* System DCRs */
386     ppc4xx_sdr_init(env);
387 
388     /* MAL */
389     dev = qdev_new(TYPE_PPC4xx_MAL);
390     qdev_prop_set_uint32(dev, "txc-num", 4);
391     qdev_prop_set_uint32(dev, "rxc-num", 16);
392     ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(dev), cpu, &error_fatal);
393     object_unref(OBJECT(dev));
394     sbdev = SYS_BUS_DEVICE(dev);
395     for (i = 0; i < ARRAY_SIZE(PPC4xx_MAL(dev)->irqs); i++) {
396         sysbus_connect_irq(sbdev, i, qdev_get_gpio_in(uic[2], 3 + i));
397     }
398 
399     /* DMA */
400     ppc4xx_dma_init(env, 0x200);
401 
402     /* 256K of L2 cache as memory */
403     ppc4xx_l2sram_init(env);
404     /* FIXME: remove this after fixing l2sram mapping in ppc440_uc.c? */
405     memory_region_init_ram(l2cache_ram, NULL, "ppc440.l2cache_ram", 256 * KiB,
406                            &error_abort);
407     memory_region_add_subregion(address_space_mem, 0x400000000LL, l2cache_ram);
408 
409     /* USB */
410     sysbus_create_simple(TYPE_PPC4xx_EHCI, 0x4bffd0400,
411                          qdev_get_gpio_in(uic[2], 29));
412     dev = qdev_new("sysbus-ohci");
413     qdev_prop_set_string(dev, "masterbus", "usb-bus.0");
414     qdev_prop_set_uint32(dev, "num-ports", 6);
415     sbdev = SYS_BUS_DEVICE(dev);
416     sysbus_realize_and_unref(sbdev, &error_fatal);
417     sysbus_mmio_map(sbdev, 0, 0x4bffd0000);
418     sysbus_connect_irq(sbdev, 0, qdev_get_gpio_in(uic[2], 30));
419     usb_create_simple(usb_bus_find(-1), "usb-kbd");
420     usb_create_simple(usb_bus_find(-1), "usb-mouse");
421 
422     /* PCI bus */
423     ppc460ex_pcie_init(env);
424     /* All PCI irqs are connected to the same UIC pin (cf. UBoot source) */
425     dev = sysbus_create_simple("ppc440-pcix-host", 0xc0ec00000,
426                                qdev_get_gpio_in(uic[1], 0));
427     pci_bus = PCI_BUS(qdev_get_child_bus(dev, "pci.0"));
428 
429     memory_region_init_alias(isa, NULL, "isa_mmio", get_system_io(),
430                              0, 0x10000);
431     memory_region_add_subregion(get_system_memory(), 0xc08000000, isa);
432 
433     /* PCI devices */
434     pci_create_simple(pci_bus, PCI_DEVFN(6, 0), "sm501");
435     /* SoC has a single SATA port but we don't emulate that yet
436      * However, firmware and usual clients have driver for SiI311x
437      * so add one for convenience by default */
438     if (defaults_enabled()) {
439         pci_create_simple(pci_bus, -1, "sii3112");
440     }
441 
442     /* SoC has 4 UARTs
443      * but board has only one wired and two are present in fdt */
444     if (serial_hd(0) != NULL) {
445         serial_mm_init(address_space_mem, 0x4ef600300, 0,
446                        qdev_get_gpio_in(uic[1], 1),
447                        PPC_SERIAL_MM_BAUDBASE, serial_hd(0),
448                        DEVICE_BIG_ENDIAN);
449     }
450     if (serial_hd(1) != NULL) {
451         serial_mm_init(address_space_mem, 0x4ef600400, 0,
452                        qdev_get_gpio_in(uic[0], 1),
453                        PPC_SERIAL_MM_BAUDBASE, serial_hd(1),
454                        DEVICE_BIG_ENDIAN);
455     }
456 
457     /* Load U-Boot image. */
458     if (!machine->kernel_filename) {
459         success = sam460ex_load_uboot();
460         if (success < 0) {
461             error_report("could not load firmware");
462             exit(1);
463         }
464     }
465 
466     /* Load kernel. */
467     if (machine->kernel_filename) {
468         hwaddr loadaddr = LOAD_UIMAGE_LOADADDR_INVALID;
469         success = load_uimage(machine->kernel_filename, &entry, &loadaddr,
470                               NULL, NULL, NULL);
471         if (success < 0) {
472             uint64_t elf_entry;
473 
474             success = load_elf(machine->kernel_filename, NULL, NULL, NULL,
475                                &elf_entry, NULL, NULL, NULL,
476                                1, PPC_ELF_MACHINE, 0, 0);
477             entry = elf_entry;
478         }
479         /* XXX try again as binary */
480         if (success < 0) {
481             error_report("could not load kernel '%s'",
482                     machine->kernel_filename);
483             exit(1);
484         }
485     }
486 
487     /* Load initrd. */
488     if (machine->initrd_filename) {
489         initrd_size = load_image_targphys(machine->initrd_filename,
490                                           RAMDISK_ADDR,
491                                           machine->ram_size - RAMDISK_ADDR);
492         if (initrd_size < 0) {
493             error_report("could not load ram disk '%s' at %x",
494                     machine->initrd_filename, RAMDISK_ADDR);
495             exit(1);
496         }
497     }
498 
499     /* If we're loading a kernel directly, we must load the device tree too. */
500     if (machine->kernel_filename) {
501         int dt_size;
502 
503         dt_size = sam460ex_load_device_tree(FDT_ADDR, machine->ram_size,
504                                     RAMDISK_ADDR, initrd_size,
505                                     machine->kernel_cmdline);
506 
507         boot_info->dt_base = FDT_ADDR;
508         boot_info->dt_size = dt_size;
509     }
510 
511     boot_info->entry = entry;
512 }
513 
514 static void sam460ex_machine_init(MachineClass *mc)
515 {
516     mc->desc = "aCube Sam460ex";
517     mc->init = sam460ex_init;
518     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("460exb");
519     mc->default_ram_size = 512 * MiB;
520     mc->default_ram_id = "ppc4xx.sdram";
521 }
522 
523 DEFINE_MACHINE("sam460ex", sam460ex_machine_init)
524