2 * ASPEED SDRAM Memory Controller
4 * Copyright (C) 2016 IBM Corp.
6 * This code is licensed under the GPL version 2 or later. See
7 * the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
12 #include "qemu/module.h"
13 #include "qemu/error-report.h"
14 #include "hw/misc/aspeed_sdmc.h"
15 #include "hw/misc/aspeed_scu.h"
16 #include "hw/qdev-properties.h"
17 #include "migration/vmstate.h"
18 #include "qapi/error.h"
21 /* Protection Key Register */
22 #define R_PROT (0x00 / 4)
23 #define PROT_KEY_UNLOCK 0xFC600309
25 /* Configuration Register */
26 #define R_CONF (0x04 / 4)
28 /* Control/Status Register #1 (ast2500) */
29 #define R_STATUS1 (0x60 / 4)
30 #define PHY_BUSY_STATE BIT(0)
32 #define R_ECC_TEST_CTRL (0x70 / 4)
33 #define ECC_TEST_FINISHED BIT(12)
34 #define ECC_TEST_FAIL BIT(13)
37 * Configuration register Ox4 (for Aspeed AST2400 SOC)
39 * These are for the record and future use. ASPEED_SDMC_DRAM_SIZE is
40 * what we care about right now as it is checked by U-Boot to
41 * determine the RAM size.
44 #define ASPEED_SDMC_RESERVED 0xFFFFF800 /* 31:11 reserved */
45 #define ASPEED_SDMC_AST2300_COMPAT (1 << 10)
46 #define ASPEED_SDMC_SCRAMBLE_PATTERN (1 << 9)
47 #define ASPEED_SDMC_DATA_SCRAMBLE (1 << 8)
48 #define ASPEED_SDMC_ECC_ENABLE (1 << 7)
49 #define ASPEED_SDMC_VGA_COMPAT (1 << 6) /* readonly */
50 #define ASPEED_SDMC_DRAM_BANK (1 << 5)
51 #define ASPEED_SDMC_DRAM_BURST (1 << 4)
52 #define ASPEED_SDMC_VGA_APERTURE(x) ((x & 0x3) << 2) /* readonly */
53 #define ASPEED_SDMC_VGA_8MB 0x0
54 #define ASPEED_SDMC_VGA_16MB 0x1
55 #define ASPEED_SDMC_VGA_32MB 0x2
56 #define ASPEED_SDMC_VGA_64MB 0x3
57 #define ASPEED_SDMC_DRAM_SIZE(x) (x & 0x3)
58 #define ASPEED_SDMC_DRAM_64MB 0x0
59 #define ASPEED_SDMC_DRAM_128MB 0x1
60 #define ASPEED_SDMC_DRAM_256MB 0x2
61 #define ASPEED_SDMC_DRAM_512MB 0x3
63 #define ASPEED_SDMC_READONLY_MASK \
64 (ASPEED_SDMC_RESERVED | ASPEED_SDMC_VGA_COMPAT | \
65 ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB))
67 * Configuration register Ox4 (for Aspeed AST2500 SOC and higher)
69 * Incompatibilities are annotated in the list. ASPEED_SDMC_HW_VERSION
70 * should be set to 1 for the AST2500 SOC.
72 #define ASPEED_SDMC_HW_VERSION(x) ((x & 0xf) << 28) /* readonly */
73 #define ASPEED_SDMC_SW_VERSION ((x & 0xff) << 20)
74 #define ASPEED_SDMC_CACHE_INITIAL_DONE (1 << 19) /* readonly */
75 #define ASPEED_SDMC_AST2500_RESERVED 0x7C000 /* 18:14 reserved */
76 #define ASPEED_SDMC_CACHE_DDR4_CONF (1 << 13)
77 #define ASPEED_SDMC_CACHE_INITIAL (1 << 12)
78 #define ASPEED_SDMC_CACHE_RANGE_CTRL (1 << 11)
79 #define ASPEED_SDMC_CACHE_ENABLE (1 << 10) /* differs from AST2400 */
80 #define ASPEED_SDMC_DRAM_TYPE (1 << 4) /* differs from AST2400 */
82 /* DRAM size definitions differs */
83 #define ASPEED_SDMC_AST2500_128MB 0x0
84 #define ASPEED_SDMC_AST2500_256MB 0x1
85 #define ASPEED_SDMC_AST2500_512MB 0x2
86 #define ASPEED_SDMC_AST2500_1024MB 0x3
88 #define ASPEED_SDMC_AST2500_READONLY_MASK \
89 (ASPEED_SDMC_HW_VERSION(0xf) | ASPEED_SDMC_CACHE_INITIAL_DONE | \
90 ASPEED_SDMC_AST2500_RESERVED | ASPEED_SDMC_VGA_COMPAT | \
91 ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB))
93 static uint64_t aspeed_sdmc_read(void *opaque
, hwaddr addr
, unsigned size
)
95 AspeedSDMCState
*s
= ASPEED_SDMC(opaque
);
99 if (addr
>= ARRAY_SIZE(s
->regs
)) {
100 qemu_log_mask(LOG_GUEST_ERROR
,
101 "%s: Out-of-bounds read at offset 0x%" HWADDR_PRIx
"\n",
106 return s
->regs
[addr
];
109 static void aspeed_sdmc_write(void *opaque
, hwaddr addr
, uint64_t data
,
112 AspeedSDMCState
*s
= ASPEED_SDMC(opaque
);
113 AspeedSDMCClass
*asc
= ASPEED_SDMC_GET_CLASS(s
);
117 if (addr
>= ARRAY_SIZE(s
->regs
)) {
118 qemu_log_mask(LOG_GUEST_ERROR
,
119 "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx
"\n",
124 if (addr
== R_PROT
) {
125 s
->regs
[addr
] = (data
== PROT_KEY_UNLOCK
) ? 1 : 0;
129 if (!s
->regs
[R_PROT
]) {
130 qemu_log_mask(LOG_GUEST_ERROR
, "%s: SDMC is locked!\n", __func__
);
134 asc
->write(s
, addr
, data
);
137 static const MemoryRegionOps aspeed_sdmc_ops
= {
138 .read
= aspeed_sdmc_read
,
139 .write
= aspeed_sdmc_write
,
140 .endianness
= DEVICE_LITTLE_ENDIAN
,
141 .valid
.min_access_size
= 4,
142 .valid
.max_access_size
= 4,
145 static int ast2400_rambits(AspeedSDMCState
*s
)
147 switch (s
->ram_size
>> 20) {
149 return ASPEED_SDMC_DRAM_64MB
;
151 return ASPEED_SDMC_DRAM_128MB
;
153 return ASPEED_SDMC_DRAM_256MB
;
155 return ASPEED_SDMC_DRAM_512MB
;
160 /* use a common default */
161 warn_report("Invalid RAM size 0x%" PRIx64
". Using default 256M",
163 s
->ram_size
= 256 << 20;
164 return ASPEED_SDMC_DRAM_256MB
;
167 static int ast2500_rambits(AspeedSDMCState
*s
)
169 switch (s
->ram_size
>> 20) {
171 return ASPEED_SDMC_AST2500_128MB
;
173 return ASPEED_SDMC_AST2500_256MB
;
175 return ASPEED_SDMC_AST2500_512MB
;
177 return ASPEED_SDMC_AST2500_1024MB
;
182 /* use a common default */
183 warn_report("Invalid RAM size 0x%" PRIx64
". Using default 512M",
185 s
->ram_size
= 512 << 20;
186 return ASPEED_SDMC_AST2500_512MB
;
189 static void aspeed_sdmc_reset(DeviceState
*dev
)
191 AspeedSDMCState
*s
= ASPEED_SDMC(dev
);
192 AspeedSDMCClass
*asc
= ASPEED_SDMC_GET_CLASS(s
);
194 memset(s
->regs
, 0, sizeof(s
->regs
));
196 /* Set ram size bit and defaults values */
197 s
->regs
[R_CONF
] = asc
->compute_conf(s
, 0);
200 static void aspeed_sdmc_realize(DeviceState
*dev
, Error
**errp
)
202 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
203 AspeedSDMCState
*s
= ASPEED_SDMC(dev
);
204 AspeedSDMCClass
*asc
= ASPEED_SDMC_GET_CLASS(s
);
206 s
->max_ram_size
= asc
->max_ram_size
;
208 memory_region_init_io(&s
->iomem
, OBJECT(s
), &aspeed_sdmc_ops
, s
,
209 TYPE_ASPEED_SDMC
, 0x1000);
210 sysbus_init_mmio(sbd
, &s
->iomem
);
213 static const VMStateDescription vmstate_aspeed_sdmc
= {
214 .name
= "aspeed.sdmc",
216 .minimum_version_id
= 1,
217 .fields
= (VMStateField
[]) {
218 VMSTATE_UINT32_ARRAY(regs
, AspeedSDMCState
, ASPEED_SDMC_NR_REGS
),
219 VMSTATE_END_OF_LIST()
223 static Property aspeed_sdmc_properties
[] = {
224 DEFINE_PROP_UINT64("ram-size", AspeedSDMCState
, ram_size
, 0),
225 DEFINE_PROP_UINT64("max-ram-size", AspeedSDMCState
, max_ram_size
, 0),
226 DEFINE_PROP_END_OF_LIST(),
229 static void aspeed_sdmc_class_init(ObjectClass
*klass
, void *data
)
231 DeviceClass
*dc
= DEVICE_CLASS(klass
);
232 dc
->realize
= aspeed_sdmc_realize
;
233 dc
->reset
= aspeed_sdmc_reset
;
234 dc
->desc
= "ASPEED SDRAM Memory Controller";
235 dc
->vmsd
= &vmstate_aspeed_sdmc
;
236 dc
->props
= aspeed_sdmc_properties
;
239 static const TypeInfo aspeed_sdmc_info
= {
240 .name
= TYPE_ASPEED_SDMC
,
241 .parent
= TYPE_SYS_BUS_DEVICE
,
242 .instance_size
= sizeof(AspeedSDMCState
),
243 .class_init
= aspeed_sdmc_class_init
,
244 .class_size
= sizeof(AspeedSDMCClass
),
248 static uint32_t aspeed_2400_sdmc_compute_conf(AspeedSDMCState
*s
, uint32_t data
)
250 uint32_t fixed_conf
= ASPEED_SDMC_VGA_COMPAT
|
251 ASPEED_SDMC_DRAM_SIZE(ast2400_rambits(s
));
253 /* Make sure readonly bits are kept */
254 data
&= ~ASPEED_SDMC_READONLY_MASK
;
256 return data
| fixed_conf
;
259 static void aspeed_2400_sdmc_write(AspeedSDMCState
*s
, uint32_t reg
,
264 data
= aspeed_2400_sdmc_compute_conf(s
, data
);
273 static void aspeed_2400_sdmc_class_init(ObjectClass
*klass
, void *data
)
275 DeviceClass
*dc
= DEVICE_CLASS(klass
);
276 AspeedSDMCClass
*asc
= ASPEED_SDMC_CLASS(klass
);
278 dc
->desc
= "ASPEED 2400 SDRAM Memory Controller";
279 asc
->max_ram_size
= 512 << 20;
280 asc
->compute_conf
= aspeed_2400_sdmc_compute_conf
;
281 asc
->write
= aspeed_2400_sdmc_write
;
284 static const TypeInfo aspeed_2400_sdmc_info
= {
285 .name
= TYPE_ASPEED_2400_SDMC
,
286 .parent
= TYPE_ASPEED_SDMC
,
287 .class_init
= aspeed_2400_sdmc_class_init
,
290 static uint32_t aspeed_2500_sdmc_compute_conf(AspeedSDMCState
*s
, uint32_t data
)
292 uint32_t fixed_conf
= ASPEED_SDMC_HW_VERSION(1) |
293 ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB
) |
294 ASPEED_SDMC_CACHE_INITIAL_DONE
|
295 ASPEED_SDMC_DRAM_SIZE(ast2500_rambits(s
));
297 /* Make sure readonly bits are kept */
298 data
&= ~ASPEED_SDMC_AST2500_READONLY_MASK
;
300 return data
| fixed_conf
;
303 static void aspeed_2500_sdmc_write(AspeedSDMCState
*s
, uint32_t reg
,
308 data
= aspeed_2500_sdmc_compute_conf(s
, data
);
311 /* Will never return 'busy' */
312 data
&= ~PHY_BUSY_STATE
;
314 case R_ECC_TEST_CTRL
:
315 /* Always done, always happy */
316 data
|= ECC_TEST_FINISHED
;
317 data
&= ~ECC_TEST_FAIL
;
326 static void aspeed_2500_sdmc_class_init(ObjectClass
*klass
, void *data
)
328 DeviceClass
*dc
= DEVICE_CLASS(klass
);
329 AspeedSDMCClass
*asc
= ASPEED_SDMC_CLASS(klass
);
331 dc
->desc
= "ASPEED 2500 SDRAM Memory Controller";
332 asc
->max_ram_size
= 1024 << 20;
333 asc
->compute_conf
= aspeed_2500_sdmc_compute_conf
;
334 asc
->write
= aspeed_2500_sdmc_write
;
337 static const TypeInfo aspeed_2500_sdmc_info
= {
338 .name
= TYPE_ASPEED_2500_SDMC
,
339 .parent
= TYPE_ASPEED_SDMC
,
340 .class_init
= aspeed_2500_sdmc_class_init
,
343 static void aspeed_sdmc_register_types(void)
345 type_register_static(&aspeed_sdmc_info
);
346 type_register_static(&aspeed_2400_sdmc_info
);
347 type_register_static(&aspeed_2500_sdmc_info
);
350 type_init(aspeed_sdmc_register_types
);