xref: /openbmc/qemu/hw/arm/aspeed.c (revision 80e5db30)
1 /*
2  * OpenPOWER Palmetto BMC
3  *
4  * Andrew Jeffery <andrew@aj.id.au>
5  *
6  * Copyright 2016 IBM Corp.
7  *
8  * This code is licensed under the GPL version 2 or later.  See
9  * the COPYING file in the top-level directory.
10  */
11 
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "qemu-common.h"
15 #include "cpu.h"
16 #include "exec/address-spaces.h"
17 #include "hw/arm/arm.h"
18 #include "hw/arm/aspeed_soc.h"
19 #include "hw/boards.h"
20 #include "qemu/log.h"
21 #include "sysemu/block-backend.h"
22 #include "sysemu/blockdev.h"
23 #include "hw/loader.h"
24 #include "qemu/error-report.h"
25 
26 static struct arm_boot_info aspeed_board_binfo = {
27     .board_id = -1, /* device-tree-only board */
28     .nb_cpus = 1,
29 };
30 
31 typedef struct AspeedBoardState {
32     AspeedSoCState soc;
33     MemoryRegion ram;
34 } AspeedBoardState;
35 
36 typedef struct AspeedBoardConfig {
37     const char *soc_name;
38     uint32_t hw_strap1;
39     const char *fmc_model;
40     const char *spi_model;
41     uint32_t num_cs;
42 } AspeedBoardConfig;
43 
44 enum {
45     PALMETTO_BMC,
46     AST2500_EVB,
47     ROMULUS_BMC,
48 };
49 
50 /* Palmetto hardware value: 0x120CE416 */
51 #define PALMETTO_BMC_HW_STRAP1 (                                        \
52         SCU_AST2400_HW_STRAP_DRAM_SIZE(DRAM_SIZE_256MB) |               \
53         SCU_AST2400_HW_STRAP_DRAM_CONFIG(2 /* DDR3 with CL=6, CWL=5 */) | \
54         SCU_AST2400_HW_STRAP_ACPI_DIS |                                 \
55         SCU_AST2400_HW_STRAP_SET_CLK_SOURCE(AST2400_CLK_48M_IN) |       \
56         SCU_HW_STRAP_VGA_CLASS_CODE |                                   \
57         SCU_HW_STRAP_LPC_RESET_PIN |                                    \
58         SCU_HW_STRAP_SPI_MODE(SCU_HW_STRAP_SPI_M_S_EN) |                \
59         SCU_AST2400_HW_STRAP_SET_CPU_AHB_RATIO(AST2400_CPU_AHB_RATIO_2_1) | \
60         SCU_HW_STRAP_SPI_WIDTH |                                        \
61         SCU_HW_STRAP_VGA_SIZE_SET(VGA_16M_DRAM) |                       \
62         SCU_AST2400_HW_STRAP_BOOT_MODE(AST2400_SPI_BOOT))
63 
64 /* AST2500 evb hardware value: 0xF100C2E6 */
65 #define AST2500_EVB_HW_STRAP1 ((                                        \
66         AST2500_HW_STRAP1_DEFAULTS |                                    \
67         SCU_AST2500_HW_STRAP_SPI_AUTOFETCH_ENABLE |                     \
68         SCU_AST2500_HW_STRAP_GPIO_STRAP_ENABLE |                        \
69         SCU_AST2500_HW_STRAP_UART_DEBUG |                               \
70         SCU_AST2500_HW_STRAP_DDR4_ENABLE |                              \
71         SCU_HW_STRAP_MAC1_RGMII |                                       \
72         SCU_HW_STRAP_MAC0_RGMII) &                                      \
73         ~SCU_HW_STRAP_2ND_BOOT_WDT)
74 
75 /* Romulus hardware value: 0xF10AD206 */
76 #define ROMULUS_BMC_HW_STRAP1 (                                         \
77         AST2500_HW_STRAP1_DEFAULTS |                                    \
78         SCU_AST2500_HW_STRAP_SPI_AUTOFETCH_ENABLE |                     \
79         SCU_AST2500_HW_STRAP_GPIO_STRAP_ENABLE |                        \
80         SCU_AST2500_HW_STRAP_UART_DEBUG |                               \
81         SCU_AST2500_HW_STRAP_DDR4_ENABLE |                              \
82         SCU_AST2500_HW_STRAP_ACPI_ENABLE |                              \
83         SCU_HW_STRAP_SPI_MODE(SCU_HW_STRAP_SPI_MASTER))
84 
85 static const AspeedBoardConfig aspeed_boards[] = {
86     [PALMETTO_BMC] = {
87         .soc_name  = "ast2400-a1",
88         .hw_strap1 = PALMETTO_BMC_HW_STRAP1,
89         .fmc_model = "n25q256a",
90         .spi_model = "mx25l25635e",
91         .num_cs    = 1,
92     },
93     [AST2500_EVB]  = {
94         .soc_name  = "ast2500-a1",
95         .hw_strap1 = AST2500_EVB_HW_STRAP1,
96         .fmc_model = "n25q256a",
97         .spi_model = "mx25l25635e",
98         .num_cs    = 1,
99     },
100     [ROMULUS_BMC]  = {
101         .soc_name  = "ast2500-a1",
102         .hw_strap1 = ROMULUS_BMC_HW_STRAP1,
103         .fmc_model = "n25q256a",
104         .spi_model = "mx66l1g45g",
105         .num_cs    = 2,
106     },
107 };
108 
109 #define FIRMWARE_ADDR 0x0
110 
111 static void write_boot_rom(DriveInfo *dinfo, hwaddr addr, size_t rom_size,
112                            Error **errp)
113 {
114     BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
115     uint8_t *storage;
116 
117     if (rom_size > blk_getlength(blk)) {
118         rom_size = blk_getlength(blk);
119     }
120 
121     storage = g_new0(uint8_t, rom_size);
122     if (blk_pread(blk, 0, storage, rom_size) < 0) {
123         error_setg(errp, "failed to read the initial flash content");
124         return;
125     }
126 
127     rom_add_blob_fixed("aspeed.boot_rom", storage, rom_size, addr);
128     g_free(storage);
129 }
130 
131 static void aspeed_board_init_flashes(AspeedSMCState *s, const char *flashtype,
132                                       Error **errp)
133 {
134     int i ;
135 
136     for (i = 0; i < s->num_cs; ++i) {
137         AspeedSMCFlash *fl = &s->flashes[i];
138         DriveInfo *dinfo = drive_get_next(IF_MTD);
139         qemu_irq cs_line;
140 
141         /*
142          * FIXME: check that we are not using a flash module exceeding
143          * the controller segment size
144          */
145         fl->flash = ssi_create_slave_no_init(s->spi, flashtype);
146         if (dinfo) {
147             qdev_prop_set_drive(fl->flash, "drive", blk_by_legacy_dinfo(dinfo),
148                                 errp);
149         }
150         qdev_init_nofail(fl->flash);
151 
152         cs_line = qdev_get_gpio_in_named(fl->flash, SSI_GPIO_CS, 0);
153         sysbus_connect_irq(SYS_BUS_DEVICE(s), i + 1, cs_line);
154     }
155 }
156 
157 static void aspeed_board_init(MachineState *machine,
158                               const AspeedBoardConfig *cfg)
159 {
160     AspeedBoardState *bmc;
161     AspeedSoCClass *sc;
162     DriveInfo *drive0 = drive_get(IF_MTD, 0, 0);
163 
164     bmc = g_new0(AspeedBoardState, 1);
165     object_initialize(&bmc->soc, (sizeof(bmc->soc)), cfg->soc_name);
166     object_property_add_child(OBJECT(machine), "soc", OBJECT(&bmc->soc),
167                               &error_abort);
168 
169     sc = ASPEED_SOC_GET_CLASS(&bmc->soc);
170 
171     object_property_set_int(OBJECT(&bmc->soc), ram_size, "ram-size",
172                            &error_abort);
173     object_property_set_int(OBJECT(&bmc->soc), cfg->hw_strap1, "hw-strap1",
174                             &error_abort);
175     object_property_set_int(OBJECT(&bmc->soc), cfg->num_cs, "num-cs",
176                             &error_abort);
177     object_property_set_bool(OBJECT(&bmc->soc), true, "realized",
178                              &error_abort);
179 
180     /*
181      * Allocate RAM after the memory controller has checked the size
182      * was valid. If not, a default value is used.
183      */
184     ram_size = object_property_get_int(OBJECT(&bmc->soc), "ram-size",
185                                        &error_abort);
186 
187     memory_region_allocate_system_memory(&bmc->ram, NULL, "ram", ram_size);
188     memory_region_add_subregion(get_system_memory(), sc->info->sdram_base,
189                                 &bmc->ram);
190     object_property_add_const_link(OBJECT(&bmc->soc), "ram", OBJECT(&bmc->ram),
191                                    &error_abort);
192 
193     aspeed_board_init_flashes(&bmc->soc.fmc, cfg->fmc_model, &error_abort);
194     aspeed_board_init_flashes(&bmc->soc.spi[0], cfg->spi_model, &error_abort);
195 
196     /* Install first FMC flash content as a boot rom. */
197     if (drive0) {
198         AspeedSMCFlash *fl = &bmc->soc.fmc.flashes[0];
199         MemoryRegion *boot_rom = g_new(MemoryRegion, 1);
200 
201         /*
202          * create a ROM region using the default mapping window size of
203          * the flash module.
204          */
205         memory_region_init_rom(boot_rom, OBJECT(bmc), "aspeed.boot_rom",
206                                fl->size, &error_abort);
207         memory_region_add_subregion(get_system_memory(), FIRMWARE_ADDR,
208                                     boot_rom);
209         write_boot_rom(drive0, FIRMWARE_ADDR, fl->size, &error_abort);
210     }
211 
212     aspeed_board_binfo.kernel_filename = machine->kernel_filename;
213     aspeed_board_binfo.initrd_filename = machine->initrd_filename;
214     aspeed_board_binfo.kernel_cmdline = machine->kernel_cmdline;
215     aspeed_board_binfo.ram_size = ram_size;
216     aspeed_board_binfo.loader_start = sc->info->sdram_base;
217 
218     arm_load_kernel(ARM_CPU(first_cpu), &aspeed_board_binfo);
219 }
220 
221 static void palmetto_bmc_init(MachineState *machine)
222 {
223     aspeed_board_init(machine, &aspeed_boards[PALMETTO_BMC]);
224 }
225 
226 static void palmetto_bmc_class_init(ObjectClass *oc, void *data)
227 {
228     MachineClass *mc = MACHINE_CLASS(oc);
229 
230     mc->desc = "OpenPOWER Palmetto BMC (ARM926EJ-S)";
231     mc->init = palmetto_bmc_init;
232     mc->max_cpus = 1;
233     mc->no_sdcard = 1;
234     mc->no_floppy = 1;
235     mc->no_cdrom = 1;
236     mc->no_parallel = 1;
237 }
238 
239 static const TypeInfo palmetto_bmc_type = {
240     .name = MACHINE_TYPE_NAME("palmetto-bmc"),
241     .parent = TYPE_MACHINE,
242     .class_init = palmetto_bmc_class_init,
243 };
244 
245 static void ast2500_evb_init(MachineState *machine)
246 {
247     aspeed_board_init(machine, &aspeed_boards[AST2500_EVB]);
248 }
249 
250 static void ast2500_evb_class_init(ObjectClass *oc, void *data)
251 {
252     MachineClass *mc = MACHINE_CLASS(oc);
253 
254     mc->desc = "Aspeed AST2500 EVB (ARM1176)";
255     mc->init = ast2500_evb_init;
256     mc->max_cpus = 1;
257     mc->no_sdcard = 1;
258     mc->no_floppy = 1;
259     mc->no_cdrom = 1;
260     mc->no_parallel = 1;
261 }
262 
263 static const TypeInfo ast2500_evb_type = {
264     .name = MACHINE_TYPE_NAME("ast2500-evb"),
265     .parent = TYPE_MACHINE,
266     .class_init = ast2500_evb_class_init,
267 };
268 
269 static void romulus_bmc_init(MachineState *machine)
270 {
271     aspeed_board_init(machine, &aspeed_boards[ROMULUS_BMC]);
272 }
273 
274 static void romulus_bmc_class_init(ObjectClass *oc, void *data)
275 {
276     MachineClass *mc = MACHINE_CLASS(oc);
277 
278     mc->desc = "OpenPOWER Romulus BMC (ARM1176)";
279     mc->init = romulus_bmc_init;
280     mc->max_cpus = 1;
281     mc->no_sdcard = 1;
282     mc->no_floppy = 1;
283     mc->no_cdrom = 1;
284     mc->no_parallel = 1;
285 }
286 
287 static const TypeInfo romulus_bmc_type = {
288     .name = MACHINE_TYPE_NAME("romulus-bmc"),
289     .parent = TYPE_MACHINE,
290     .class_init = romulus_bmc_class_init,
291 };
292 
293 static void aspeed_machine_init(void)
294 {
295     type_register_static(&palmetto_bmc_type);
296     type_register_static(&ast2500_evb_type);
297     type_register_static(&romulus_bmc_type);
298 }
299 
300 type_init(aspeed_machine_init)
301