xref: /openbmc/qemu/hw/arm/aspeed_soc_common.c (revision f6e33708)
1 /*
2  * ASPEED SoC family
3  *
4  * Andrew Jeffery <andrew@aj.id.au>
5  * Jeremy Kerr <jk@ozlabs.org>
6  *
7  * Copyright 2016 IBM Corp.
8  *
9  * This code is licensed under the GPL version 2 or later.  See
10  * the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "hw/qdev-properties.h"
16 #include "hw/misc/unimp.h"
17 #include "hw/arm/aspeed_soc.h"
18 #include "hw/char/serial.h"
19 
20 
21 const char *aspeed_soc_cpu_type(AspeedSoCClass *sc)
22 {
23     assert(sc->valid_cpu_types);
24     assert(sc->valid_cpu_types[0]);
25     assert(!sc->valid_cpu_types[1]);
26     return sc->valid_cpu_types[0];
27 }
28 
29 qemu_irq aspeed_soc_get_irq(AspeedSoCState *s, int dev)
30 {
31     return ASPEED_SOC_GET_CLASS(s)->get_irq(s, dev);
32 }
33 
34 bool aspeed_soc_uart_realize(AspeedSoCState *s, Error **errp)
35 {
36     AspeedSoCClass *sc = ASPEED_SOC_GET_CLASS(s);
37     SerialMM *smm;
38 
39     for (int i = 0, uart = ASPEED_DEV_UART1; i < sc->uarts_num; i++, uart++) {
40         smm = &s->uart[i];
41 
42         /* Chardev property is set by the machine. */
43         qdev_prop_set_uint8(DEVICE(smm), "regshift", 2);
44         qdev_prop_set_uint32(DEVICE(smm), "baudbase", 38400);
45         qdev_set_legacy_instance_id(DEVICE(smm), sc->memmap[uart], 2);
46         qdev_prop_set_uint8(DEVICE(smm), "endianness", DEVICE_LITTLE_ENDIAN);
47         if (!sysbus_realize(SYS_BUS_DEVICE(smm), errp)) {
48             return false;
49         }
50 
51         sysbus_connect_irq(SYS_BUS_DEVICE(smm), 0, aspeed_soc_get_irq(s, uart));
52         aspeed_mmio_map(s, SYS_BUS_DEVICE(smm), 0, sc->memmap[uart]);
53     }
54 
55     return true;
56 }
57 
58 void aspeed_soc_uart_set_chr(AspeedSoCState *s, int dev, Chardev *chr)
59 {
60     AspeedSoCClass *sc = ASPEED_SOC_GET_CLASS(s);
61     int i = dev - ASPEED_DEV_UART1;
62 
63     g_assert(0 <= i && i < ARRAY_SIZE(s->uart) && i < sc->uarts_num);
64     qdev_prop_set_chr(DEVICE(&s->uart[i]), "chardev", chr);
65 }
66 
67 /*
68  * SDMC should be realized first to get correct RAM size and max size
69  * values
70  */
71 bool aspeed_soc_dram_init(AspeedSoCState *s, Error **errp)
72 {
73     AspeedSoCClass *sc = ASPEED_SOC_GET_CLASS(s);
74     ram_addr_t ram_size, max_ram_size;
75 
76     ram_size = object_property_get_uint(OBJECT(&s->sdmc), "ram-size",
77                                         &error_abort);
78     max_ram_size = object_property_get_uint(OBJECT(&s->sdmc), "max-ram-size",
79                                             &error_abort);
80 
81     memory_region_init(&s->dram_container, OBJECT(s), "ram-container",
82                        max_ram_size);
83     memory_region_add_subregion(&s->dram_container, 0, s->dram_mr);
84 
85     /*
86      * Add a memory region beyond the RAM region to let firmwares scan
87      * the address space with load/store and guess how much RAM the
88      * SoC has.
89      */
90     if (ram_size < max_ram_size) {
91         DeviceState *dev = qdev_new(TYPE_UNIMPLEMENTED_DEVICE);
92 
93         qdev_prop_set_string(dev, "name", "ram-empty");
94         qdev_prop_set_uint64(dev, "size", max_ram_size  - ram_size);
95         if (!sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), errp)) {
96             return false;
97         }
98 
99         memory_region_add_subregion_overlap(&s->dram_container, ram_size,
100                       sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0), -1000);
101     }
102 
103     memory_region_add_subregion(s->memory,
104                       sc->memmap[ASPEED_DEV_SDRAM], &s->dram_container);
105     return true;
106 }
107 
108 void aspeed_mmio_map(AspeedSoCState *s, SysBusDevice *dev, int n, hwaddr addr)
109 {
110     memory_region_add_subregion(s->memory, addr,
111                                 sysbus_mmio_get_region(dev, n));
112 }
113 
114 void aspeed_mmio_map_unimplemented(AspeedSoCState *s, SysBusDevice *dev,
115                                    const char *name, hwaddr addr, uint64_t size)
116 {
117     qdev_prop_set_string(DEVICE(dev), "name", name);
118     qdev_prop_set_uint64(DEVICE(dev), "size", size);
119     sysbus_realize(dev, &error_abort);
120 
121     memory_region_add_subregion_overlap(s->memory, addr,
122                                         sysbus_mmio_get_region(dev, 0), -1000);
123 }
124 
125 static void aspeed_soc_realize(DeviceState *dev, Error **errp)
126 {
127     AspeedSoCState *s = ASPEED_SOC(dev);
128 
129     if (!s->memory) {
130         error_setg(errp, "'memory' link is not set");
131         return;
132     }
133 }
134 
135 static Property aspeed_soc_properties[] = {
136     DEFINE_PROP_LINK("dram", AspeedSoCState, dram_mr, TYPE_MEMORY_REGION,
137                      MemoryRegion *),
138     DEFINE_PROP_LINK("memory", AspeedSoCState, memory, TYPE_MEMORY_REGION,
139                      MemoryRegion *),
140     DEFINE_PROP_END_OF_LIST(),
141 };
142 
143 static void aspeed_soc_class_init(ObjectClass *oc, void *data)
144 {
145     DeviceClass *dc = DEVICE_CLASS(oc);
146 
147     dc->realize = aspeed_soc_realize;
148     device_class_set_props(dc, aspeed_soc_properties);
149 }
150 
151 static const TypeInfo aspeed_soc_types[] = {
152     {
153         .name           = TYPE_ASPEED_SOC,
154         .parent         = TYPE_DEVICE,
155         .instance_size  = sizeof(AspeedSoCState),
156         .class_size     = sizeof(AspeedSoCClass),
157         .class_init     = aspeed_soc_class_init,
158         .abstract       = true,
159     },
160 };
161 
162 DEFINE_TYPES(aspeed_soc_types)
163