aboutsummaryrefslogtreecommitdiff
path: root/hw/arm/bananapi_m2u.c
blob: 1d49a006b5357a75e37b9c360221aad6873728fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Bananapi M2U emulation
 *
 * Copyright (C) 2023 qianfan Zhao <qianfanguijin@163.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "qemu/osdep.h"
#include "qemu/units.h"
#include "exec/address-spaces.h"
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "hw/boards.h"
#include "hw/qdev-properties.h"
#include "hw/arm/allwinner-r40.h"

static struct arm_boot_info bpim2u_binfo;

/*
 * R40 can boot from mmc0 and mmc2, and bpim2u has two mmc interface, one is
 * connected to sdcard and another mount an emmc media.
 * Attach the mmc driver and try loading bootloader.
 */
static void mmc_attach_drive(AwR40State *s, AwSdHostState *mmc, int unit,
                             bool load_bootroom, bool *bootroom_loaded)
{
    DriveInfo *di = drive_get(IF_SD, 0, unit);
    BlockBackend *blk = di ? blk_by_legacy_dinfo(di) : NULL;
    BusState *bus;
    DeviceState *carddev;

    bus = qdev_get_child_bus(DEVICE(mmc), "sd-bus");
    if (bus == NULL) {
        error_report("No SD bus found in SOC object");
        exit(1);
    }

    carddev = qdev_new(TYPE_SD_CARD);
    qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
    qdev_realize_and_unref(carddev, bus, &error_fatal);

    if (load_bootroom && blk && blk_is_available(blk)) {
        /* Use Boot ROM to copy data from SD card to SRAM */
        *bootroom_loaded = allwinner_r40_bootrom_setup(s, blk, unit);
    }
}

static void bpim2u_init(MachineState *machine)
{
    bool bootroom_loaded = false;
    AwR40State *r40;

    /* BIOS is not supported by this board */
    if (machine->firmware) {
        error_report("BIOS not supported for this machine");
        exit(1);
    }

    /* Only allow Cortex-A7 for this board */
    if (strcmp(machine->cpu_type, ARM_CPU_TYPE_NAME("cortex-a7")) != 0) {
        error_report("This board can only be used with cortex-a7 CPU");
        exit(1);
    }

    r40 = AW_R40(object_new(TYPE_AW_R40));
    object_property_add_child(OBJECT(machine), "soc", OBJECT(r40));
    object_unref(OBJECT(r40));

    /* Setup timer properties */
    object_property_set_int(OBJECT(r40), "clk0-freq", 32768, &error_abort);
    object_property_set_int(OBJECT(r40), "clk1-freq", 24 * 1000 * 1000,
                            &error_abort);

    /* Mark R40 object realized */
    qdev_realize(DEVICE(r40), NULL, &error_abort);

    /*
     * Plug in SD card and try load bootrom, R40 has 4 mmc controllers but can
     * only booting from mmc0 and mmc2.
     */
    for (int i = 0; i < AW_R40_NUM_MMCS; i++) {
        switch (i) {
        case 0:
        case 2:
            mmc_attach_drive(r40, &r40->mmc[i], i,
                             !machine->kernel_filename && !bootroom_loaded,
                             &bootroom_loaded);
            break;
        default:
            mmc_attach_drive(r40, &r40->mmc[i], i, false, NULL);
            break;
        }
    }

    /* SDRAM */
    memory_region_add_subregion(get_system_memory(),
                                r40->memmap[AW_R40_DEV_SDRAM], machine->ram);

    bpim2u_binfo.loader_start = r40->memmap[AW_R40_DEV_SDRAM];
    bpim2u_binfo.ram_size = machine->ram_size;
    bpim2u_binfo.psci_conduit = QEMU_PSCI_CONDUIT_SMC;
    arm_load_kernel(ARM_CPU(first_cpu), machine, &bpim2u_binfo);
}

static void bpim2u_machine_init(MachineClass *mc)
{
    mc->desc = "Bananapi M2U (Cortex-A7)";
    mc->init = bpim2u_init;
    mc->min_cpus = AW_R40_NUM_CPUS;
    mc->max_cpus = AW_R40_NUM_CPUS;
    mc->default_cpus = AW_R40_NUM_CPUS;
    mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a7");
    mc->default_ram_size = 1 * GiB;
    mc->default_ram_id = "bpim2u.ram";
}

DEFINE_MACHINE("bpim2u", bpim2u_machine_init)