xref: /openbmc/qemu/hw/arm/cubieboard.c (revision 764a6ee9)
1 /*
2  * cubieboard emulation
3  *
4  * Copyright (C) 2013 Li Guang
5  * Written by Li Guang <lig.fnst@cn.fujitsu.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15  * for more details.
16  */
17 
18 #include "qemu/osdep.h"
19 #include "qapi/error.h"
20 #include "qemu/error-report.h"
21 #include "hw/boards.h"
22 #include "hw/qdev-properties.h"
23 #include "hw/arm/allwinner-a10.h"
24 #include "hw/arm/boot.h"
25 #include "hw/i2c/i2c.h"
26 
27 static struct arm_boot_info cubieboard_binfo = {
28     .loader_start = AW_A10_SDRAM_BASE,
29     .board_id = 0x1008,
30 };
31 
32 static void cubieboard_init(MachineState *machine)
33 {
34     AwA10State *a10;
35     Error *err = NULL;
36     DriveInfo *di;
37     BlockBackend *blk;
38     BusState *bus;
39     DeviceState *carddev;
40     I2CBus *i2c;
41 
42     /* BIOS is not supported by this board */
43     if (machine->firmware) {
44         error_report("BIOS not supported for this machine");
45         exit(1);
46     }
47 
48     /* This board has fixed size RAM (512MiB or 1GiB) */
49     if (machine->ram_size != 512 * MiB &&
50         machine->ram_size != 1 * GiB) {
51         error_report("This machine can only be used with 512MiB or 1GiB RAM");
52         exit(1);
53     }
54 
55     a10 = AW_A10(object_new(TYPE_AW_A10));
56     object_property_add_child(OBJECT(machine), "soc", OBJECT(a10));
57     object_unref(OBJECT(a10));
58 
59     if (!object_property_set_int(OBJECT(&a10->emac), "phy-addr", 1, &err)) {
60         error_reportf_err(err, "Couldn't set phy address: ");
61         exit(1);
62     }
63 
64     if (!object_property_set_int(OBJECT(&a10->timer), "clk0-freq", 32768,
65                                  &err)) {
66         error_reportf_err(err, "Couldn't set clk0 frequency: ");
67         exit(1);
68     }
69 
70     if (!object_property_set_int(OBJECT(&a10->timer), "clk1-freq", 24000000,
71                                  &err)) {
72         error_reportf_err(err, "Couldn't set clk1 frequency: ");
73         exit(1);
74     }
75 
76     if (!qdev_realize(DEVICE(a10), NULL, &err)) {
77         error_reportf_err(err, "Couldn't realize Allwinner A10: ");
78         exit(1);
79     }
80 
81     /* Connect AXP 209 */
82     i2c = I2C_BUS(qdev_get_child_bus(DEVICE(&a10->i2c0), "i2c"));
83     i2c_slave_create_simple(i2c, "axp209_pmu", 0x34);
84 
85     /* Retrieve SD bus */
86     di = drive_get(IF_SD, 0, 0);
87     blk = di ? blk_by_legacy_dinfo(di) : NULL;
88     bus = qdev_get_child_bus(DEVICE(a10), "sd-bus");
89 
90     /* Plug in SD card */
91     carddev = qdev_new(TYPE_SD_CARD);
92     qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
93     qdev_realize_and_unref(carddev, bus, &error_fatal);
94 
95     memory_region_add_subregion(get_system_memory(), AW_A10_SDRAM_BASE,
96                                 machine->ram);
97 
98     /* Load target kernel or start using BootROM */
99     if (!machine->kernel_filename && blk && blk_is_available(blk)) {
100         /* Use Boot ROM to copy data from SD card to SRAM */
101         allwinner_a10_bootrom_setup(a10, blk);
102     }
103     /* TODO create and connect IDE devices for ide_drive_get() */
104 
105     cubieboard_binfo.ram_size = machine->ram_size;
106     arm_load_kernel(&a10->cpu, machine, &cubieboard_binfo);
107 }
108 
109 static void cubieboard_machine_init(MachineClass *mc)
110 {
111     static const char * const valid_cpu_types[] = {
112         ARM_CPU_TYPE_NAME("cortex-a8"),
113         NULL
114     };
115 
116     mc->desc = "cubietech cubieboard (Cortex-A8)";
117     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a8");
118     mc->valid_cpu_types = valid_cpu_types;
119     mc->default_ram_size = 1 * GiB;
120     mc->init = cubieboard_init;
121     mc->block_default_type = IF_IDE;
122     mc->units_per_default_bus = 1;
123     mc->ignore_memory_transaction_failures = true;
124     mc->default_ram_id = "cubieboard.ram";
125 }
126 
127 DEFINE_MACHINE("cubieboard", cubieboard_machine_init)
128