Skip to content

Commit f75b533

Browse files
committed
aspeed/smc: QOMify AspeedSMCFlash
AspeedSMCFlash is a small structure representing the AHB memory window through which the contents of a flash device can be accessed with MMIOs. Introduce an AspeedSMCFlash SysBusDevice model and attach the associated memory region to the newly instantiated objects. Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Signed-off-by: Cédric Le Goater <[email protected]>
1 parent 10f915e commit f75b533

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

hw/ssi/aspeed_smc.c

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,18 @@ static const MemoryRegionOps aspeed_smc_ops = {
11011101
.endianness = DEVICE_LITTLE_ENDIAN,
11021102
};
11031103

1104+
static void aspeed_smc_instance_init(Object *obj)
1105+
{
1106+
AspeedSMCState *s = ASPEED_SMC(obj);
1107+
AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
1108+
int i;
1109+
1110+
for (i = 0; i < asc->max_peripherals; i++) {
1111+
object_initialize_child(obj, "flash[*]", &s->flashes[i],
1112+
TYPE_ASPEED_SMC_FLASH);
1113+
}
1114+
}
1115+
11041116
/*
11051117
* Initialize the custom address spaces for DMAs
11061118
*/
@@ -1123,7 +1135,6 @@ static void aspeed_smc_realize(DeviceState *dev, Error **errp)
11231135
AspeedSMCState *s = ASPEED_SMC(dev);
11241136
AspeedSMCClass *asc = ASPEED_SMC_GET_CLASS(s);
11251137
int i;
1126-
char name[32];
11271138
hwaddr offset = 0;
11281139

11291140
/* keep a copy under AspeedSMCState to speed up accesses */
@@ -1170,8 +1181,6 @@ static void aspeed_smc_realize(DeviceState *dev, Error **errp)
11701181
&s->mmio_flash, 0, asc->flash_window_size);
11711182
sysbus_init_mmio(sbd, &s->mmio_flash_alias);
11721183

1173-
s->flashes = g_new0(AspeedSMCFlash, asc->max_peripherals);
1174-
11751184
/*
11761185
* Let's create a sub memory region for each possible peripheral. All
11771186
* have a configurable memory segment in the overall flash mapping
@@ -1182,12 +1191,17 @@ static void aspeed_smc_realize(DeviceState *dev, Error **errp)
11821191
for (i = 0; i < asc->max_peripherals; ++i) {
11831192
AspeedSMCFlash *fl = &s->flashes[i];
11841193

1185-
snprintf(name, sizeof(name), TYPE_ASPEED_SMC ".flash.%d", i);
1194+
if (!object_property_set_link(OBJECT(fl), "controller", OBJECT(s),
1195+
errp)) {
1196+
return;
1197+
}
1198+
if (!object_property_set_uint(OBJECT(fl), "cs", i, errp)) {
1199+
return;
1200+
}
1201+
if (!sysbus_realize(SYS_BUS_DEVICE(fl), errp)) {
1202+
return;
1203+
}
11861204

1187-
fl->cs = i;
1188-
fl->controller = s;
1189-
memory_region_init_io(&fl->mmio, OBJECT(s), &aspeed_smc_flash_ops,
1190-
fl, name, asc->segments[i].size);
11911205
memory_region_add_subregion(&s->mmio_flash, offset, &fl->mmio);
11921206
offset += asc->segments[i].size;
11931207
}
@@ -1231,12 +1245,57 @@ static void aspeed_smc_class_init(ObjectClass *klass, void *data)
12311245
static const TypeInfo aspeed_smc_info = {
12321246
.name = TYPE_ASPEED_SMC,
12331247
.parent = TYPE_SYS_BUS_DEVICE,
1248+
.instance_init = aspeed_smc_instance_init,
12341249
.instance_size = sizeof(AspeedSMCState),
12351250
.class_size = sizeof(AspeedSMCClass),
12361251
.class_init = aspeed_smc_class_init,
12371252
.abstract = true,
12381253
};
12391254

1255+
static void aspeed_smc_flash_realize(DeviceState *dev, Error **errp)
1256+
{
1257+
AspeedSMCFlash *s = ASPEED_SMC_FLASH(dev);
1258+
AspeedSMCClass *asc;
1259+
g_autofree char *name = g_strdup_printf(TYPE_ASPEED_SMC_FLASH ".%d", s->cs);
1260+
1261+
if (!s->controller) {
1262+
error_setg(errp, TYPE_ASPEED_SMC_FLASH ": 'controller' link not set");
1263+
return;
1264+
}
1265+
1266+
asc = ASPEED_SMC_GET_CLASS(s->controller);
1267+
1268+
/*
1269+
* Use the default segment value to size the memory region. This
1270+
* can be changed by FW at runtime.
1271+
*/
1272+
memory_region_init_io(&s->mmio, OBJECT(s), &aspeed_smc_flash_ops,
1273+
s, name, asc->segments[s->cs].size);
1274+
sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio);
1275+
}
1276+
1277+
static Property aspeed_smc_flash_properties[] = {
1278+
DEFINE_PROP_UINT8("cs", AspeedSMCFlash, cs, 0),
1279+
DEFINE_PROP_LINK("controller", AspeedSMCFlash, controller, TYPE_ASPEED_SMC,
1280+
AspeedSMCState *),
1281+
DEFINE_PROP_END_OF_LIST(),
1282+
};
1283+
1284+
static void aspeed_smc_flash_class_init(ObjectClass *klass, void *data)
1285+
{
1286+
DeviceClass *dc = DEVICE_CLASS(klass);
1287+
1288+
dc->desc = "Aspeed SMC Flash device region";
1289+
dc->realize = aspeed_smc_flash_realize;
1290+
device_class_set_props(dc, aspeed_smc_flash_properties);
1291+
}
1292+
1293+
static const TypeInfo aspeed_smc_flash_info = {
1294+
.name = TYPE_ASPEED_SMC_FLASH,
1295+
.parent = TYPE_SYS_BUS_DEVICE,
1296+
.instance_size = sizeof(AspeedSMCFlash),
1297+
.class_init = aspeed_smc_flash_class_init,
1298+
};
12401299

12411300
/*
12421301
* The Segment Registers of the AST2400 and AST2500 have a 8MB
@@ -1625,6 +1684,7 @@ static const TypeInfo aspeed_2600_spi2_info = {
16251684

16261685
static void aspeed_smc_register_types(void)
16271686
{
1687+
type_register_static(&aspeed_smc_flash_info);
16281688
type_register_static(&aspeed_smc_info);
16291689
type_register_static(&aspeed_2400_smc_info);
16301690
type_register_static(&aspeed_2400_fmc_info);

include/hw/ssi/aspeed_smc.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,23 @@
3030
#include "qom/object.h"
3131

3232
struct AspeedSMCState;
33-
typedef struct AspeedSMCFlash {
34-
struct AspeedSMCState *controller;
3533

34+
#define TYPE_ASPEED_SMC_FLASH "aspeed.smc.flash"
35+
OBJECT_DECLARE_SIMPLE_TYPE(AspeedSMCFlash, ASPEED_SMC_FLASH)
36+
struct AspeedSMCFlash {
37+
SysBusDevice parent_obj;
38+
39+
struct AspeedSMCState *controller;
3640
uint8_t cs;
3741

3842
MemoryRegion mmio;
39-
} AspeedSMCFlash;
43+
};
4044

4145
#define TYPE_ASPEED_SMC "aspeed.smc"
4246
OBJECT_DECLARE_TYPE(AspeedSMCState, AspeedSMCClass, ASPEED_SMC)
4347

4448
#define ASPEED_SMC_R_MAX (0x100 / 4)
49+
#define ASPEED_SMC_CS_MAX 5
4550

4651
struct AspeedSMCState {
4752
SysBusDevice parent_obj;
@@ -72,7 +77,7 @@ struct AspeedSMCState {
7277
MemoryRegion *dram_mr;
7378
AddressSpace dram_as;
7479

75-
AspeedSMCFlash *flashes;
80+
AspeedSMCFlash flashes[ASPEED_SMC_CS_MAX];
7681

7782
uint8_t snoop_index;
7883
uint8_t snoop_dummies;

0 commit comments

Comments
 (0)