1c2f58c2fSPeter Delevoryas /* 2c2f58c2fSPeter Delevoryas * Copyright (c) Meta Platforms, Inc. and affiliates. (http://www.meta.com) 3c2f58c2fSPeter Delevoryas * 4c2f58c2fSPeter Delevoryas * This code is licensed under the GPL version 2 or later. See the COPYING 5c2f58c2fSPeter Delevoryas * file in the top-level directory. 6c2f58c2fSPeter Delevoryas */ 7c2f58c2fSPeter Delevoryas 8c2f58c2fSPeter Delevoryas #include "qemu/osdep.h" 9778e14ccSPeter Delevoryas #include "qemu/units.h" 10778e14ccSPeter Delevoryas #include "qapi/error.h" 11778e14ccSPeter Delevoryas #include "sysemu/sysemu.h" 12*9cd8c41dSCédric Le Goater #include "sysemu/block-backend.h" 13c2f58c2fSPeter Delevoryas #include "hw/boards.h" 14778e14ccSPeter Delevoryas #include "hw/arm/aspeed_soc.h" 15c2f58c2fSPeter Delevoryas 16c2f58c2fSPeter Delevoryas #define TYPE_FBY35 MACHINE_TYPE_NAME("fby35") 17c2f58c2fSPeter Delevoryas OBJECT_DECLARE_SIMPLE_TYPE(Fby35State, FBY35); 18c2f58c2fSPeter Delevoryas 19c2f58c2fSPeter Delevoryas struct Fby35State { 20c2f58c2fSPeter Delevoryas MachineState parent_obj; 21778e14ccSPeter Delevoryas 22778e14ccSPeter Delevoryas MemoryRegion bmc_memory; 23778e14ccSPeter Delevoryas MemoryRegion bmc_dram; 24778e14ccSPeter Delevoryas MemoryRegion bmc_boot_rom; 25778e14ccSPeter Delevoryas 26778e14ccSPeter Delevoryas AspeedSoCState bmc; 27*9cd8c41dSCédric Le Goater 28*9cd8c41dSCédric Le Goater bool mmio_exec; 29c2f58c2fSPeter Delevoryas }; 30c2f58c2fSPeter Delevoryas 31778e14ccSPeter Delevoryas #define FBY35_BMC_RAM_SIZE (2 * GiB) 32*9cd8c41dSCédric Le Goater #define FBY35_BMC_FIRMWARE_ADDR 0x0 33*9cd8c41dSCédric Le Goater 34*9cd8c41dSCédric Le Goater static void fby35_bmc_write_boot_rom(DriveInfo *dinfo, MemoryRegion *mr, 35*9cd8c41dSCédric Le Goater hwaddr offset, size_t rom_size, 36*9cd8c41dSCédric Le Goater Error **errp) 37*9cd8c41dSCédric Le Goater { 38*9cd8c41dSCédric Le Goater BlockBackend *blk = blk_by_legacy_dinfo(dinfo); 39*9cd8c41dSCédric Le Goater g_autofree void *storage = NULL; 40*9cd8c41dSCédric Le Goater int64_t size; 41*9cd8c41dSCédric Le Goater 42*9cd8c41dSCédric Le Goater /* 43*9cd8c41dSCédric Le Goater * The block backend size should have already been 'validated' by 44*9cd8c41dSCédric Le Goater * the creation of the m25p80 object. 45*9cd8c41dSCédric Le Goater */ 46*9cd8c41dSCédric Le Goater size = blk_getlength(blk); 47*9cd8c41dSCédric Le Goater if (size <= 0) { 48*9cd8c41dSCédric Le Goater error_setg(errp, "failed to get flash size"); 49*9cd8c41dSCédric Le Goater return; 50*9cd8c41dSCédric Le Goater } 51*9cd8c41dSCédric Le Goater 52*9cd8c41dSCédric Le Goater if (rom_size > size) { 53*9cd8c41dSCédric Le Goater rom_size = size; 54*9cd8c41dSCédric Le Goater } 55*9cd8c41dSCédric Le Goater 56*9cd8c41dSCédric Le Goater storage = g_malloc0(rom_size); 57*9cd8c41dSCédric Le Goater if (blk_pread(blk, 0, rom_size, storage, 0) < 0) { 58*9cd8c41dSCédric Le Goater error_setg(errp, "failed to read the initial flash content"); 59*9cd8c41dSCédric Le Goater return; 60*9cd8c41dSCédric Le Goater } 61*9cd8c41dSCédric Le Goater 62*9cd8c41dSCédric Le Goater /* TODO: find a better way to install the ROM */ 63*9cd8c41dSCédric Le Goater memcpy(memory_region_get_ram_ptr(mr) + offset, storage, rom_size); 64*9cd8c41dSCédric Le Goater } 65778e14ccSPeter Delevoryas 66778e14ccSPeter Delevoryas static void fby35_bmc_init(Fby35State *s) 67778e14ccSPeter Delevoryas { 68*9cd8c41dSCédric Le Goater DriveInfo *drive0 = drive_get(IF_MTD, 0, 0); 69*9cd8c41dSCédric Le Goater 70778e14ccSPeter Delevoryas memory_region_init(&s->bmc_memory, OBJECT(s), "bmc-memory", UINT64_MAX); 71778e14ccSPeter Delevoryas memory_region_init_ram(&s->bmc_dram, OBJECT(s), "bmc-dram", 72778e14ccSPeter Delevoryas FBY35_BMC_RAM_SIZE, &error_abort); 73778e14ccSPeter Delevoryas 74778e14ccSPeter Delevoryas object_initialize_child(OBJECT(s), "bmc", &s->bmc, "ast2600-a3"); 75778e14ccSPeter Delevoryas object_property_set_int(OBJECT(&s->bmc), "ram-size", FBY35_BMC_RAM_SIZE, 76778e14ccSPeter Delevoryas &error_abort); 77778e14ccSPeter Delevoryas object_property_set_link(OBJECT(&s->bmc), "memory", OBJECT(&s->bmc_memory), 78778e14ccSPeter Delevoryas &error_abort); 79778e14ccSPeter Delevoryas object_property_set_link(OBJECT(&s->bmc), "dram", OBJECT(&s->bmc_dram), 80778e14ccSPeter Delevoryas &error_abort); 81778e14ccSPeter Delevoryas object_property_set_int(OBJECT(&s->bmc), "hw-strap1", 0x000000C0, 82778e14ccSPeter Delevoryas &error_abort); 83778e14ccSPeter Delevoryas object_property_set_int(OBJECT(&s->bmc), "hw-strap2", 0x00000003, 84778e14ccSPeter Delevoryas &error_abort); 85778e14ccSPeter Delevoryas aspeed_soc_uart_set_chr(&s->bmc, ASPEED_DEV_UART5, serial_hd(0)); 86778e14ccSPeter Delevoryas qdev_realize(DEVICE(&s->bmc), NULL, &error_abort); 87778e14ccSPeter Delevoryas 88778e14ccSPeter Delevoryas aspeed_board_init_flashes(&s->bmc.fmc, "n25q00", 2, 0); 89*9cd8c41dSCédric Le Goater 90*9cd8c41dSCédric Le Goater /* Install first FMC flash content as a boot rom. */ 91*9cd8c41dSCédric Le Goater if (drive0) { 92*9cd8c41dSCédric Le Goater AspeedSMCFlash *fl = &s->bmc.fmc.flashes[0]; 93*9cd8c41dSCédric Le Goater MemoryRegion *boot_rom = g_new(MemoryRegion, 1); 94*9cd8c41dSCédric Le Goater uint64_t size = memory_region_size(&fl->mmio); 95*9cd8c41dSCédric Le Goater 96*9cd8c41dSCédric Le Goater if (s->mmio_exec) { 97*9cd8c41dSCédric Le Goater memory_region_init_alias(boot_rom, NULL, "aspeed.boot_rom", 98*9cd8c41dSCédric Le Goater &fl->mmio, 0, size); 99*9cd8c41dSCédric Le Goater memory_region_add_subregion(&s->bmc_memory, FBY35_BMC_FIRMWARE_ADDR, 100*9cd8c41dSCédric Le Goater boot_rom); 101*9cd8c41dSCédric Le Goater } else { 102*9cd8c41dSCédric Le Goater 103*9cd8c41dSCédric Le Goater memory_region_init_rom(boot_rom, NULL, "aspeed.boot_rom", 104*9cd8c41dSCédric Le Goater size, &error_abort); 105*9cd8c41dSCédric Le Goater memory_region_add_subregion(&s->bmc_memory, FBY35_BMC_FIRMWARE_ADDR, 106*9cd8c41dSCédric Le Goater boot_rom); 107*9cd8c41dSCédric Le Goater fby35_bmc_write_boot_rom(drive0, boot_rom, FBY35_BMC_FIRMWARE_ADDR, 108*9cd8c41dSCédric Le Goater size, &error_abort); 109*9cd8c41dSCédric Le Goater } 110*9cd8c41dSCédric Le Goater } 111778e14ccSPeter Delevoryas } 112778e14ccSPeter Delevoryas 113c2f58c2fSPeter Delevoryas static void fby35_init(MachineState *machine) 114c2f58c2fSPeter Delevoryas { 115778e14ccSPeter Delevoryas Fby35State *s = FBY35(machine); 116778e14ccSPeter Delevoryas 117778e14ccSPeter Delevoryas fby35_bmc_init(s); 118c2f58c2fSPeter Delevoryas } 119c2f58c2fSPeter Delevoryas 120*9cd8c41dSCédric Le Goater 121*9cd8c41dSCédric Le Goater static bool fby35_get_mmio_exec(Object *obj, Error **errp) 122*9cd8c41dSCédric Le Goater { 123*9cd8c41dSCédric Le Goater return FBY35(obj)->mmio_exec; 124*9cd8c41dSCédric Le Goater } 125*9cd8c41dSCédric Le Goater 126*9cd8c41dSCédric Le Goater static void fby35_set_mmio_exec(Object *obj, bool value, Error **errp) 127*9cd8c41dSCédric Le Goater { 128*9cd8c41dSCédric Le Goater FBY35(obj)->mmio_exec = value; 129*9cd8c41dSCédric Le Goater } 130*9cd8c41dSCédric Le Goater 131*9cd8c41dSCédric Le Goater static void fby35_instance_init(Object *obj) 132*9cd8c41dSCédric Le Goater { 133*9cd8c41dSCédric Le Goater FBY35(obj)->mmio_exec = false; 134*9cd8c41dSCédric Le Goater } 135*9cd8c41dSCédric Le Goater 136c2f58c2fSPeter Delevoryas static void fby35_class_init(ObjectClass *oc, void *data) 137c2f58c2fSPeter Delevoryas { 138c2f58c2fSPeter Delevoryas MachineClass *mc = MACHINE_CLASS(oc); 139c2f58c2fSPeter Delevoryas 140c2f58c2fSPeter Delevoryas mc->desc = "Meta Platforms fby35"; 141c2f58c2fSPeter Delevoryas mc->init = fby35_init; 142778e14ccSPeter Delevoryas mc->no_floppy = 1; 143778e14ccSPeter Delevoryas mc->no_cdrom = 1; 144778e14ccSPeter Delevoryas mc->min_cpus = mc->max_cpus = mc->default_cpus = 2; 145*9cd8c41dSCédric Le Goater 146*9cd8c41dSCédric Le Goater object_class_property_add_bool(oc, "execute-in-place", 147*9cd8c41dSCédric Le Goater fby35_get_mmio_exec, 148*9cd8c41dSCédric Le Goater fby35_set_mmio_exec); 149*9cd8c41dSCédric Le Goater object_class_property_set_description(oc, "execute-in-place", 150*9cd8c41dSCédric Le Goater "boot directly from CE0 flash device"); 151c2f58c2fSPeter Delevoryas } 152c2f58c2fSPeter Delevoryas 153c2f58c2fSPeter Delevoryas static const TypeInfo fby35_types[] = { 154c2f58c2fSPeter Delevoryas { 155c2f58c2fSPeter Delevoryas .name = MACHINE_TYPE_NAME("fby35"), 156c2f58c2fSPeter Delevoryas .parent = TYPE_MACHINE, 157c2f58c2fSPeter Delevoryas .class_init = fby35_class_init, 158c2f58c2fSPeter Delevoryas .instance_size = sizeof(Fby35State), 159*9cd8c41dSCédric Le Goater .instance_init = fby35_instance_init, 160c2f58c2fSPeter Delevoryas }, 161c2f58c2fSPeter Delevoryas }; 162c2f58c2fSPeter Delevoryas 163c2f58c2fSPeter Delevoryas DEFINE_TYPES(fby35_types); 164