| 1 | /* |
| 2 | * Bananapi M2U emulation |
| 3 | * |
| 4 | * Copyright (C) 2023 qianfan Zhao <qianfanguijin@163.com> |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "qemu/osdep.h" |
| 21 | #include "qemu/units.h" |
| 22 | #include "system/address-spaces.h" |
| 23 | #include "qapi/error.h" |
| 24 | #include "qemu/error-report.h" |
| 25 | #include "hw/core/boards.h" |
| 26 | #include "hw/i2c/i2c.h" |
| 27 | #include "hw/core/qdev-properties.h" |
| 28 | #include "hw/arm/allwinner-r40.h" |
| 29 | #include "hw/arm/boot.h" |
| 30 | #include "hw/arm/machines-qom.h" |
| 31 | |
| 32 | #define TYPE_BPIM2U_MACHINE MACHINE_TYPE_NAME("bpim2u") |
| 33 | OBJECT_DECLARE_SIMPLE_TYPE(Bpim2uMachineState, BPIM2U_MACHINE) |
| 34 | |
| 35 | struct Bpim2uMachineState { |
| 36 | MachineState parent; |
| 37 | |
| 38 | struct arm_boot_info bootinfo; |
| 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * R40 can boot from mmc0 and mmc2, and bpim2u has two mmc interface, one is |
| 43 | * connected to sdcard and another mount an emmc media. |
| 44 | * Attach the mmc driver and try loading bootloader. |
| 45 | */ |
| 46 | static void mmc_attach_drive(AwR40State *s, AwSdHostState *mmc, int unit, |
| 47 | bool load_bootroom, bool *bootroom_loaded) |
| 48 | { |
| 49 | DriveInfo *di = drive_get(IF_SD, 0, unit); |
| 50 | BlockBackend *blk = di ? blk_by_legacy_dinfo(di) : NULL; |
| 51 | BusState *bus; |
| 52 | DeviceState *carddev; |
| 53 | |
| 54 | bus = qdev_get_child_bus(DEVICE(mmc), "sd-bus"); |
| 55 | if (bus == NULL) { |
| 56 | error_report("No SD bus found in SOC object"); |
| 57 | exit(1); |
| 58 | } |
| 59 | |
| 60 | carddev = qdev_new(TYPE_SD_CARD); |
| 61 | qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal); |
| 62 | qdev_realize_and_unref(carddev, bus, &error_fatal); |
| 63 | |
| 64 | if (load_bootroom && blk && blk_is_available(blk)) { |
| 65 | /* Use Boot ROM to copy data from SD card to SRAM */ |
| 66 | *bootroom_loaded = allwinner_r40_bootrom_setup(s, blk, unit); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static void bpim2u_init(MachineState *machine) |
| 71 | { |
| 72 | Bpim2uMachineState *bpms = BPIM2U_MACHINE(machine); |
| 73 | bool bootroom_loaded = false; |
| 74 | AwR40State *r40; |
| 75 | I2CBus *i2c; |
| 76 | |
| 77 | /* BIOS is not supported by this board */ |
| 78 | if (machine->firmware) { |
| 79 | error_report("BIOS not supported for this machine"); |
| 80 | exit(1); |
| 81 | } |
| 82 | |
| 83 | r40 = AW_R40(object_new(TYPE_AW_R40)); |
| 84 | object_property_add_child(OBJECT(machine), "soc", OBJECT(r40)); |
| 85 | object_unref(OBJECT(r40)); |
| 86 | |
| 87 | /* Setup timer properties */ |
| 88 | object_property_set_int(OBJECT(r40), "clk0-freq", 32768, &error_abort); |
| 89 | object_property_set_int(OBJECT(r40), "clk1-freq", 24 * 1000 * 1000, |
| 90 | &error_abort); |
| 91 | |
| 92 | /* DRAMC */ |
| 93 | r40->ram_size = machine->ram_size / MiB; |
| 94 | object_property_set_uint(OBJECT(r40), "ram-addr", |
| 95 | r40->memmap[AW_R40_DEV_SDRAM], &error_abort); |
| 96 | object_property_set_int(OBJECT(r40), "ram-size", |
| 97 | r40->ram_size, &error_abort); |
| 98 | |
| 99 | /* GMAC PHY */ |
| 100 | object_property_set_uint(OBJECT(r40), "gmac-phy-addr", 1, &error_abort); |
| 101 | |
| 102 | /* Mark R40 object realized */ |
| 103 | qdev_realize(DEVICE(r40), NULL, &error_abort); |
| 104 | |
| 105 | /* |
| 106 | * Plug in SD card and try load bootrom, R40 has 4 mmc controllers but can |
| 107 | * only booting from mmc0 and mmc2. |
| 108 | */ |
| 109 | for (int i = 0; i < AW_R40_NUM_MMCS; i++) { |
| 110 | switch (i) { |
| 111 | case 0: |
| 112 | case 2: |
| 113 | mmc_attach_drive(r40, &r40->mmc[i], i, |
| 114 | !machine->kernel_filename && !bootroom_loaded, |
| 115 | &bootroom_loaded); |
| 116 | break; |
| 117 | default: |
| 118 | mmc_attach_drive(r40, &r40->mmc[i], i, false, NULL); |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* Connect AXP221 */ |
| 124 | i2c = I2C_BUS(qdev_get_child_bus(DEVICE(&r40->i2c0), "i2c")); |
| 125 | i2c_slave_create_simple(i2c, "axp221_pmu", 0x34); |
| 126 | |
| 127 | /* SDRAM */ |
| 128 | memory_region_add_subregion(get_system_memory(), |
| 129 | r40->memmap[AW_R40_DEV_SDRAM], machine->ram); |
| 130 | |
| 131 | bpms->bootinfo.loader_start = r40->memmap[AW_R40_DEV_SDRAM]; |
| 132 | bpms->bootinfo.ram_size = machine->ram_size; |
| 133 | bpms->bootinfo.psci_conduit = QEMU_PSCI_CONDUIT_SMC; |
| 134 | arm_load_kernel(&r40->cpus[0], machine, &bpms->bootinfo); |
| 135 | } |
| 136 | |
| 137 | static void bpim2u_machine_init(MachineClass *mc) |
| 138 | { |
| 139 | static const char * const valid_cpu_types[] = { |
| 140 | ARM_CPU_TYPE_NAME("cortex-a7"), |
| 141 | NULL |
| 142 | }; |
| 143 | |
| 144 | mc->desc = "Bananapi M2U (Cortex-A7)"; |
| 145 | mc->init = bpim2u_init; |
| 146 | mc->min_cpus = AW_R40_NUM_CPUS; |
| 147 | mc->max_cpus = AW_R40_NUM_CPUS; |
| 148 | mc->default_cpus = AW_R40_NUM_CPUS; |
| 149 | mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a7"); |
| 150 | mc->valid_cpu_types = valid_cpu_types; |
| 151 | mc->default_ram_size = 1 * GiB; |
| 152 | mc->default_ram_id = "bpim2u.ram"; |
| 153 | mc->auto_create_sdcard = true; |
| 154 | } |
| 155 | |
| 156 | DEFINE_MACHINE_EXTENDED("bpim2u", MACHINE, Bpim2uMachineState, |
| 157 | bpim2u_machine_init, false, arm_machine_interfaces) |