xref: /openbmc/qemu/hw/arm/bananapi_m2u.c (revision c36dd41b)
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 "exec/address-spaces.h"
23 #include "qapi/error.h"
24 #include "qemu/error-report.h"
25 #include "hw/boards.h"
26 #include "hw/i2c/i2c.h"
27 #include "hw/qdev-properties.h"
28 #include "hw/arm/allwinner-r40.h"
29 
30 static struct arm_boot_info bpim2u_binfo;
31 
32 /*
33  * R40 can boot from mmc0 and mmc2, and bpim2u has two mmc interface, one is
34  * connected to sdcard and another mount an emmc media.
35  * Attach the mmc driver and try loading bootloader.
36  */
37 static void mmc_attach_drive(AwR40State *s, AwSdHostState *mmc, int unit,
38                              bool load_bootroom, bool *bootroom_loaded)
39 {
40     DriveInfo *di = drive_get(IF_SD, 0, unit);
41     BlockBackend *blk = di ? blk_by_legacy_dinfo(di) : NULL;
42     BusState *bus;
43     DeviceState *carddev;
44 
45     bus = qdev_get_child_bus(DEVICE(mmc), "sd-bus");
46     if (bus == NULL) {
47         error_report("No SD bus found in SOC object");
48         exit(1);
49     }
50 
51     carddev = qdev_new(TYPE_SD_CARD);
52     qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
53     qdev_realize_and_unref(carddev, bus, &error_fatal);
54 
55     if (load_bootroom && blk && blk_is_available(blk)) {
56         /* Use Boot ROM to copy data from SD card to SRAM */
57         *bootroom_loaded = allwinner_r40_bootrom_setup(s, blk, unit);
58     }
59 }
60 
61 static void bpim2u_init(MachineState *machine)
62 {
63     bool bootroom_loaded = false;
64     AwR40State *r40;
65     I2CBus *i2c;
66 
67     /* BIOS is not supported by this board */
68     if (machine->firmware) {
69         error_report("BIOS not supported for this machine");
70         exit(1);
71     }
72 
73     /* Only allow Cortex-A7 for this board */
74     if (strcmp(machine->cpu_type, ARM_CPU_TYPE_NAME("cortex-a7")) != 0) {
75         error_report("This board can only be used with cortex-a7 CPU");
76         exit(1);
77     }
78 
79     r40 = AW_R40(object_new(TYPE_AW_R40));
80     object_property_add_child(OBJECT(machine), "soc", OBJECT(r40));
81     object_unref(OBJECT(r40));
82 
83     /* Setup timer properties */
84     object_property_set_int(OBJECT(r40), "clk0-freq", 32768, &error_abort);
85     object_property_set_int(OBJECT(r40), "clk1-freq", 24 * 1000 * 1000,
86                             &error_abort);
87 
88     /* DRAMC */
89     r40->ram_size = machine->ram_size / MiB;
90     object_property_set_uint(OBJECT(r40), "ram-addr",
91                              r40->memmap[AW_R40_DEV_SDRAM], &error_abort);
92     object_property_set_int(OBJECT(r40), "ram-size",
93                             r40->ram_size, &error_abort);
94 
95     /* GMAC PHY */
96     object_property_set_uint(OBJECT(r40), "gmac-phy-addr", 1, &error_abort);
97 
98     /* Mark R40 object realized */
99     qdev_realize(DEVICE(r40), NULL, &error_abort);
100 
101     /*
102      * Plug in SD card and try load bootrom, R40 has 4 mmc controllers but can
103      * only booting from mmc0 and mmc2.
104      */
105     for (int i = 0; i < AW_R40_NUM_MMCS; i++) {
106         switch (i) {
107         case 0:
108         case 2:
109             mmc_attach_drive(r40, &r40->mmc[i], i,
110                              !machine->kernel_filename && !bootroom_loaded,
111                              &bootroom_loaded);
112             break;
113         default:
114             mmc_attach_drive(r40, &r40->mmc[i], i, false, NULL);
115             break;
116         }
117     }
118 
119     /* Connect AXP221 */
120     i2c = I2C_BUS(qdev_get_child_bus(DEVICE(&r40->i2c0), "i2c"));
121     i2c_slave_create_simple(i2c, "axp221_pmu", 0x34);
122 
123     /* SDRAM */
124     memory_region_add_subregion(get_system_memory(),
125                                 r40->memmap[AW_R40_DEV_SDRAM], machine->ram);
126 
127     bpim2u_binfo.loader_start = r40->memmap[AW_R40_DEV_SDRAM];
128     bpim2u_binfo.ram_size = machine->ram_size;
129     bpim2u_binfo.psci_conduit = QEMU_PSCI_CONDUIT_SMC;
130     arm_load_kernel(ARM_CPU(first_cpu), machine, &bpim2u_binfo);
131 }
132 
133 static void bpim2u_machine_init(MachineClass *mc)
134 {
135     mc->desc = "Bananapi M2U (Cortex-A7)";
136     mc->init = bpim2u_init;
137     mc->min_cpus = AW_R40_NUM_CPUS;
138     mc->max_cpus = AW_R40_NUM_CPUS;
139     mc->default_cpus = AW_R40_NUM_CPUS;
140     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a7");
141     mc->default_ram_size = 1 * GiB;
142     mc->default_ram_id = "bpim2u.ram";
143 }
144 
145 DEFINE_MACHINE("bpim2u", bpim2u_machine_init)
146