acpi: Add Windows ACPI Emulated Device Table (WAET)
[qemu/ar7.git] / hw / misc / aspeed_sdmc.c
blob7b466bf19a651611e7d2726ee1867531f7a78849
1 /*
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.
8 */
10 #include "qemu/osdep.h"
11 #include "qemu/log.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"
19 #include "trace.h"
20 #include "qemu/units.h"
21 #include "qemu/cutils.h"
22 #include "qapi/visitor.h"
24 /* Protection Key Register */
25 #define R_PROT (0x00 / 4)
26 #define PROT_KEY_UNLOCK 0xFC600309
28 /* Configuration Register */
29 #define R_CONF (0x04 / 4)
31 /* Control/Status Register #1 (ast2500) */
32 #define R_STATUS1 (0x60 / 4)
33 #define PHY_BUSY_STATE BIT(0)
34 #define PHY_PLL_LOCK_STATUS BIT(4)
36 #define R_ECC_TEST_CTRL (0x70 / 4)
37 #define ECC_TEST_FINISHED BIT(12)
38 #define ECC_TEST_FAIL BIT(13)
41 * Configuration register Ox4 (for Aspeed AST2400 SOC)
43 * These are for the record and future use. ASPEED_SDMC_DRAM_SIZE is
44 * what we care about right now as it is checked by U-Boot to
45 * determine the RAM size.
48 #define ASPEED_SDMC_RESERVED 0xFFFFF800 /* 31:11 reserved */
49 #define ASPEED_SDMC_AST2300_COMPAT (1 << 10)
50 #define ASPEED_SDMC_SCRAMBLE_PATTERN (1 << 9)
51 #define ASPEED_SDMC_DATA_SCRAMBLE (1 << 8)
52 #define ASPEED_SDMC_ECC_ENABLE (1 << 7)
53 #define ASPEED_SDMC_VGA_COMPAT (1 << 6) /* readonly */
54 #define ASPEED_SDMC_DRAM_BANK (1 << 5)
55 #define ASPEED_SDMC_DRAM_BURST (1 << 4)
56 #define ASPEED_SDMC_VGA_APERTURE(x) ((x & 0x3) << 2) /* readonly */
57 #define ASPEED_SDMC_VGA_8MB 0x0
58 #define ASPEED_SDMC_VGA_16MB 0x1
59 #define ASPEED_SDMC_VGA_32MB 0x2
60 #define ASPEED_SDMC_VGA_64MB 0x3
61 #define ASPEED_SDMC_DRAM_SIZE(x) (x & 0x3)
62 #define ASPEED_SDMC_DRAM_64MB 0x0
63 #define ASPEED_SDMC_DRAM_128MB 0x1
64 #define ASPEED_SDMC_DRAM_256MB 0x2
65 #define ASPEED_SDMC_DRAM_512MB 0x3
67 #define ASPEED_SDMC_READONLY_MASK \
68 (ASPEED_SDMC_RESERVED | ASPEED_SDMC_VGA_COMPAT | \
69 ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB))
71 * Configuration register Ox4 (for Aspeed AST2500 SOC and higher)
73 * Incompatibilities are annotated in the list. ASPEED_SDMC_HW_VERSION
74 * should be set to 1 for the AST2500 SOC.
76 #define ASPEED_SDMC_HW_VERSION(x) ((x & 0xf) << 28) /* readonly */
77 #define ASPEED_SDMC_SW_VERSION ((x & 0xff) << 20)
78 #define ASPEED_SDMC_CACHE_INITIAL_DONE (1 << 19) /* readonly */
79 #define ASPEED_SDMC_AST2500_RESERVED 0x7C000 /* 18:14 reserved */
80 #define ASPEED_SDMC_CACHE_DDR4_CONF (1 << 13)
81 #define ASPEED_SDMC_CACHE_INITIAL (1 << 12)
82 #define ASPEED_SDMC_CACHE_RANGE_CTRL (1 << 11)
83 #define ASPEED_SDMC_CACHE_ENABLE (1 << 10) /* differs from AST2400 */
84 #define ASPEED_SDMC_DRAM_TYPE (1 << 4) /* differs from AST2400 */
86 /* DRAM size definitions differs */
87 #define ASPEED_SDMC_AST2500_128MB 0x0
88 #define ASPEED_SDMC_AST2500_256MB 0x1
89 #define ASPEED_SDMC_AST2500_512MB 0x2
90 #define ASPEED_SDMC_AST2500_1024MB 0x3
92 #define ASPEED_SDMC_AST2600_256MB 0x0
93 #define ASPEED_SDMC_AST2600_512MB 0x1
94 #define ASPEED_SDMC_AST2600_1024MB 0x2
95 #define ASPEED_SDMC_AST2600_2048MB 0x3
97 #define ASPEED_SDMC_AST2500_READONLY_MASK \
98 (ASPEED_SDMC_HW_VERSION(0xf) | ASPEED_SDMC_CACHE_INITIAL_DONE | \
99 ASPEED_SDMC_AST2500_RESERVED | ASPEED_SDMC_VGA_COMPAT | \
100 ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB))
102 static uint64_t aspeed_sdmc_read(void *opaque, hwaddr addr, unsigned size)
104 AspeedSDMCState *s = ASPEED_SDMC(opaque);
106 addr >>= 2;
108 if (addr >= ARRAY_SIZE(s->regs)) {
109 qemu_log_mask(LOG_GUEST_ERROR,
110 "%s: Out-of-bounds read at offset 0x%" HWADDR_PRIx "\n",
111 __func__, addr);
112 return 0;
115 return s->regs[addr];
118 static void aspeed_sdmc_write(void *opaque, hwaddr addr, uint64_t data,
119 unsigned int size)
121 AspeedSDMCState *s = ASPEED_SDMC(opaque);
122 AspeedSDMCClass *asc = ASPEED_SDMC_GET_CLASS(s);
124 addr >>= 2;
126 if (addr >= ARRAY_SIZE(s->regs)) {
127 qemu_log_mask(LOG_GUEST_ERROR,
128 "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx "\n",
129 __func__, addr);
130 return;
133 if (addr == R_PROT) {
134 s->regs[addr] = (data == PROT_KEY_UNLOCK) ? 1 : 0;
135 return;
138 if (!s->regs[R_PROT]) {
139 qemu_log_mask(LOG_GUEST_ERROR, "%s: SDMC is locked!\n", __func__);
140 return;
143 asc->write(s, addr, data);
146 static const MemoryRegionOps aspeed_sdmc_ops = {
147 .read = aspeed_sdmc_read,
148 .write = aspeed_sdmc_write,
149 .endianness = DEVICE_LITTLE_ENDIAN,
150 .valid.min_access_size = 4,
151 .valid.max_access_size = 4,
154 static int ast2400_rambits(AspeedSDMCState *s)
156 switch (s->ram_size >> 20) {
157 case 64:
158 return ASPEED_SDMC_DRAM_64MB;
159 case 128:
160 return ASPEED_SDMC_DRAM_128MB;
161 case 256:
162 return ASPEED_SDMC_DRAM_256MB;
163 case 512:
164 return ASPEED_SDMC_DRAM_512MB;
165 default:
166 g_assert_not_reached();
167 break;
171 static int ast2500_rambits(AspeedSDMCState *s)
173 switch (s->ram_size >> 20) {
174 case 128:
175 return ASPEED_SDMC_AST2500_128MB;
176 case 256:
177 return ASPEED_SDMC_AST2500_256MB;
178 case 512:
179 return ASPEED_SDMC_AST2500_512MB;
180 case 1024:
181 return ASPEED_SDMC_AST2500_1024MB;
182 default:
183 g_assert_not_reached();
184 break;
188 static int ast2600_rambits(AspeedSDMCState *s)
190 switch (s->ram_size >> 20) {
191 case 256:
192 return ASPEED_SDMC_AST2600_256MB;
193 case 512:
194 return ASPEED_SDMC_AST2600_512MB;
195 case 1024:
196 return ASPEED_SDMC_AST2600_1024MB;
197 case 2048:
198 return ASPEED_SDMC_AST2600_2048MB;
199 default:
200 g_assert_not_reached();
201 break;
205 static void aspeed_sdmc_reset(DeviceState *dev)
207 AspeedSDMCState *s = ASPEED_SDMC(dev);
208 AspeedSDMCClass *asc = ASPEED_SDMC_GET_CLASS(s);
210 memset(s->regs, 0, sizeof(s->regs));
212 /* Set ram size bit and defaults values */
213 s->regs[R_CONF] = asc->compute_conf(s, 0);
216 static void aspeed_sdmc_get_ram_size(Object *obj, Visitor *v, const char *name,
217 void *opaque, Error **errp)
219 AspeedSDMCState *s = ASPEED_SDMC(obj);
220 int64_t value = s->ram_size;
222 visit_type_int(v, name, &value, errp);
225 static void aspeed_sdmc_set_ram_size(Object *obj, Visitor *v, const char *name,
226 void *opaque, Error **errp)
228 int i;
229 char *sz;
230 int64_t value;
231 Error *local_err = NULL;
232 AspeedSDMCState *s = ASPEED_SDMC(obj);
233 AspeedSDMCClass *asc = ASPEED_SDMC_GET_CLASS(s);
235 visit_type_int(v, name, &value, &local_err);
236 if (local_err) {
237 error_propagate(errp, local_err);
238 return;
241 for (i = 0; asc->valid_ram_sizes[i]; i++) {
242 if (value == asc->valid_ram_sizes[i]) {
243 s->ram_size = value;
244 return;
248 sz = size_to_str(value);
249 error_setg(&local_err, "Invalid RAM size %s", sz);
250 g_free(sz);
251 error_propagate(errp, local_err);
254 static void aspeed_sdmc_initfn(Object *obj)
256 object_property_add(obj, "ram-size", "int",
257 aspeed_sdmc_get_ram_size, aspeed_sdmc_set_ram_size,
258 NULL, NULL, NULL);
261 static void aspeed_sdmc_realize(DeviceState *dev, Error **errp)
263 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
264 AspeedSDMCState *s = ASPEED_SDMC(dev);
265 AspeedSDMCClass *asc = ASPEED_SDMC_GET_CLASS(s);
267 s->max_ram_size = asc->max_ram_size;
269 memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_sdmc_ops, s,
270 TYPE_ASPEED_SDMC, 0x1000);
271 sysbus_init_mmio(sbd, &s->iomem);
274 static const VMStateDescription vmstate_aspeed_sdmc = {
275 .name = "aspeed.sdmc",
276 .version_id = 1,
277 .minimum_version_id = 1,
278 .fields = (VMStateField[]) {
279 VMSTATE_UINT32_ARRAY(regs, AspeedSDMCState, ASPEED_SDMC_NR_REGS),
280 VMSTATE_END_OF_LIST()
284 static Property aspeed_sdmc_properties[] = {
285 DEFINE_PROP_UINT64("max-ram-size", AspeedSDMCState, max_ram_size, 0),
286 DEFINE_PROP_END_OF_LIST(),
289 static void aspeed_sdmc_class_init(ObjectClass *klass, void *data)
291 DeviceClass *dc = DEVICE_CLASS(klass);
292 dc->realize = aspeed_sdmc_realize;
293 dc->reset = aspeed_sdmc_reset;
294 dc->desc = "ASPEED SDRAM Memory Controller";
295 dc->vmsd = &vmstate_aspeed_sdmc;
296 device_class_set_props(dc, aspeed_sdmc_properties);
299 static const TypeInfo aspeed_sdmc_info = {
300 .name = TYPE_ASPEED_SDMC,
301 .parent = TYPE_SYS_BUS_DEVICE,
302 .instance_size = sizeof(AspeedSDMCState),
303 .instance_init = aspeed_sdmc_initfn,
304 .class_init = aspeed_sdmc_class_init,
305 .class_size = sizeof(AspeedSDMCClass),
306 .abstract = true,
309 static uint32_t aspeed_2400_sdmc_compute_conf(AspeedSDMCState *s, uint32_t data)
311 uint32_t fixed_conf = ASPEED_SDMC_VGA_COMPAT |
312 ASPEED_SDMC_DRAM_SIZE(ast2400_rambits(s));
314 /* Make sure readonly bits are kept */
315 data &= ~ASPEED_SDMC_READONLY_MASK;
317 return data | fixed_conf;
320 static void aspeed_2400_sdmc_write(AspeedSDMCState *s, uint32_t reg,
321 uint32_t data)
323 switch (reg) {
324 case R_CONF:
325 data = aspeed_2400_sdmc_compute_conf(s, data);
326 break;
327 default:
328 break;
331 s->regs[reg] = data;
334 static const uint64_t
335 aspeed_2400_ram_sizes[] = { 64 * MiB, 128 * MiB, 256 * MiB, 512 * MiB, 0};
337 static void aspeed_2400_sdmc_class_init(ObjectClass *klass, void *data)
339 DeviceClass *dc = DEVICE_CLASS(klass);
340 AspeedSDMCClass *asc = ASPEED_SDMC_CLASS(klass);
342 dc->desc = "ASPEED 2400 SDRAM Memory Controller";
343 asc->max_ram_size = 512 << 20;
344 asc->compute_conf = aspeed_2400_sdmc_compute_conf;
345 asc->write = aspeed_2400_sdmc_write;
346 asc->valid_ram_sizes = aspeed_2400_ram_sizes;
349 static const TypeInfo aspeed_2400_sdmc_info = {
350 .name = TYPE_ASPEED_2400_SDMC,
351 .parent = TYPE_ASPEED_SDMC,
352 .class_init = aspeed_2400_sdmc_class_init,
355 static uint32_t aspeed_2500_sdmc_compute_conf(AspeedSDMCState *s, uint32_t data)
357 uint32_t fixed_conf = ASPEED_SDMC_HW_VERSION(1) |
358 ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB) |
359 ASPEED_SDMC_CACHE_INITIAL_DONE |
360 ASPEED_SDMC_DRAM_SIZE(ast2500_rambits(s));
362 /* Make sure readonly bits are kept */
363 data &= ~ASPEED_SDMC_AST2500_READONLY_MASK;
365 return data | fixed_conf;
368 static void aspeed_2500_sdmc_write(AspeedSDMCState *s, uint32_t reg,
369 uint32_t data)
371 switch (reg) {
372 case R_CONF:
373 data = aspeed_2500_sdmc_compute_conf(s, data);
374 break;
375 case R_STATUS1:
376 /* Will never return 'busy' */
377 data &= ~PHY_BUSY_STATE;
378 break;
379 case R_ECC_TEST_CTRL:
380 /* Always done, always happy */
381 data |= ECC_TEST_FINISHED;
382 data &= ~ECC_TEST_FAIL;
383 break;
384 default:
385 break;
388 s->regs[reg] = data;
391 static const uint64_t
392 aspeed_2500_ram_sizes[] = { 128 * MiB, 256 * MiB, 512 * MiB, 1024 * MiB, 0};
394 static void aspeed_2500_sdmc_class_init(ObjectClass *klass, void *data)
396 DeviceClass *dc = DEVICE_CLASS(klass);
397 AspeedSDMCClass *asc = ASPEED_SDMC_CLASS(klass);
399 dc->desc = "ASPEED 2500 SDRAM Memory Controller";
400 asc->max_ram_size = 1024 << 20;
401 asc->compute_conf = aspeed_2500_sdmc_compute_conf;
402 asc->write = aspeed_2500_sdmc_write;
403 asc->valid_ram_sizes = aspeed_2500_ram_sizes;
406 static const TypeInfo aspeed_2500_sdmc_info = {
407 .name = TYPE_ASPEED_2500_SDMC,
408 .parent = TYPE_ASPEED_SDMC,
409 .class_init = aspeed_2500_sdmc_class_init,
412 static uint32_t aspeed_2600_sdmc_compute_conf(AspeedSDMCState *s, uint32_t data)
414 uint32_t fixed_conf = ASPEED_SDMC_HW_VERSION(3) |
415 ASPEED_SDMC_VGA_APERTURE(ASPEED_SDMC_VGA_64MB) |
416 ASPEED_SDMC_DRAM_SIZE(ast2600_rambits(s));
418 /* Make sure readonly bits are kept (use ast2500 mask) */
419 data &= ~ASPEED_SDMC_AST2500_READONLY_MASK;
421 return data | fixed_conf;
424 static void aspeed_2600_sdmc_write(AspeedSDMCState *s, uint32_t reg,
425 uint32_t data)
427 switch (reg) {
428 case R_CONF:
429 data = aspeed_2600_sdmc_compute_conf(s, data);
430 break;
431 case R_STATUS1:
432 /* Will never return 'busy'. 'lock status' is always set */
433 data &= ~PHY_BUSY_STATE;
434 data |= PHY_PLL_LOCK_STATUS;
435 break;
436 case R_ECC_TEST_CTRL:
437 /* Always done, always happy */
438 data |= ECC_TEST_FINISHED;
439 data &= ~ECC_TEST_FAIL;
440 break;
441 default:
442 break;
445 s->regs[reg] = data;
448 static const uint64_t
449 aspeed_2600_ram_sizes[] = { 256 * MiB, 512 * MiB, 1024 * MiB, 2048 * MiB, 0};
451 static void aspeed_2600_sdmc_class_init(ObjectClass *klass, void *data)
453 DeviceClass *dc = DEVICE_CLASS(klass);
454 AspeedSDMCClass *asc = ASPEED_SDMC_CLASS(klass);
456 dc->desc = "ASPEED 2600 SDRAM Memory Controller";
457 asc->max_ram_size = 2048 << 20;
458 asc->compute_conf = aspeed_2600_sdmc_compute_conf;
459 asc->write = aspeed_2600_sdmc_write;
460 asc->valid_ram_sizes = aspeed_2600_ram_sizes;
463 static const TypeInfo aspeed_2600_sdmc_info = {
464 .name = TYPE_ASPEED_2600_SDMC,
465 .parent = TYPE_ASPEED_SDMC,
466 .class_init = aspeed_2600_sdmc_class_init,
469 static void aspeed_sdmc_register_types(void)
471 type_register_static(&aspeed_sdmc_info);
472 type_register_static(&aspeed_2400_sdmc_info);
473 type_register_static(&aspeed_2500_sdmc_info);
474 type_register_static(&aspeed_2600_sdmc_info);
477 type_init(aspeed_sdmc_register_types);