net/colo-compare.c: Fix a crash in COLO Primary.
[qemu/ar7.git] / hw / arm / fsl-imx31.c
blobec8239a967a7c59b75dda2c75c3341c6e30ca822
1 /*
2 * Copyright (c) 2013 Jean-Christophe Dubois <jcd@tribudubois.net>
4 * i.MX31 SOC emulation.
6 * Based on hw/arm/fsl-imx31.c
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
23 #include "qapi/error.h"
24 #include "qemu-common.h"
25 #include "cpu.h"
26 #include "hw/arm/fsl-imx31.h"
27 #include "sysemu/sysemu.h"
28 #include "exec/address-spaces.h"
29 #include "hw/boards.h"
30 #include "chardev/char.h"
32 static void fsl_imx31_init(Object *obj)
34 FslIMX31State *s = FSL_IMX31(obj);
35 int i;
37 object_initialize(&s->cpu, sizeof(s->cpu), "arm1136-" TYPE_ARM_CPU);
39 sysbus_init_child_obj(obj, "avic", &s->avic, sizeof(s->avic),
40 TYPE_IMX_AVIC);
42 sysbus_init_child_obj(obj, "ccm", &s->ccm, sizeof(s->ccm), TYPE_IMX31_CCM);
44 for (i = 0; i < FSL_IMX31_NUM_UARTS; i++) {
45 sysbus_init_child_obj(obj, "uart[*]", &s->uart[i], sizeof(s->uart[i]),
46 TYPE_IMX_SERIAL);
49 sysbus_init_child_obj(obj, "gpt", &s->gpt, sizeof(s->gpt), TYPE_IMX31_GPT);
51 for (i = 0; i < FSL_IMX31_NUM_EPITS; i++) {
52 sysbus_init_child_obj(obj, "epit[*]", &s->epit[i], sizeof(s->epit[i]),
53 TYPE_IMX_EPIT);
56 for (i = 0; i < FSL_IMX31_NUM_I2CS; i++) {
57 sysbus_init_child_obj(obj, "i2c[*]", &s->i2c[i], sizeof(s->i2c[i]),
58 TYPE_IMX_I2C);
61 for (i = 0; i < FSL_IMX31_NUM_GPIOS; i++) {
62 sysbus_init_child_obj(obj, "gpio[*]", &s->gpio[i], sizeof(s->gpio[i]),
63 TYPE_IMX_GPIO);
67 static void fsl_imx31_realize(DeviceState *dev, Error **errp)
69 FslIMX31State *s = FSL_IMX31(dev);
70 uint16_t i;
71 Error *err = NULL;
73 object_property_set_bool(OBJECT(&s->cpu), true, "realized", &err);
74 if (err) {
75 error_propagate(errp, err);
76 return;
79 object_property_set_bool(OBJECT(&s->avic), true, "realized", &err);
80 if (err) {
81 error_propagate(errp, err);
82 return;
84 sysbus_mmio_map(SYS_BUS_DEVICE(&s->avic), 0, FSL_IMX31_AVIC_ADDR);
85 sysbus_connect_irq(SYS_BUS_DEVICE(&s->avic), 0,
86 qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_IRQ));
87 sysbus_connect_irq(SYS_BUS_DEVICE(&s->avic), 1,
88 qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_FIQ));
90 object_property_set_bool(OBJECT(&s->ccm), true, "realized", &err);
91 if (err) {
92 error_propagate(errp, err);
93 return;
95 sysbus_mmio_map(SYS_BUS_DEVICE(&s->ccm), 0, FSL_IMX31_CCM_ADDR);
97 /* Initialize all UARTS */
98 for (i = 0; i < FSL_IMX31_NUM_UARTS; i++) {
99 static const struct {
100 hwaddr addr;
101 unsigned int irq;
102 } serial_table[FSL_IMX31_NUM_UARTS] = {
103 { FSL_IMX31_UART1_ADDR, FSL_IMX31_UART1_IRQ },
104 { FSL_IMX31_UART2_ADDR, FSL_IMX31_UART2_IRQ },
107 qdev_prop_set_chr(DEVICE(&s->uart[i]), "chardev", serial_hd(i));
109 object_property_set_bool(OBJECT(&s->uart[i]), true, "realized", &err);
110 if (err) {
111 error_propagate(errp, err);
112 return;
115 sysbus_mmio_map(SYS_BUS_DEVICE(&s->uart[i]), 0, serial_table[i].addr);
116 sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart[i]), 0,
117 qdev_get_gpio_in(DEVICE(&s->avic),
118 serial_table[i].irq));
121 s->gpt.ccm = IMX_CCM(&s->ccm);
123 object_property_set_bool(OBJECT(&s->gpt), true, "realized", &err);
124 if (err) {
125 error_propagate(errp, err);
126 return;
129 sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpt), 0, FSL_IMX31_GPT_ADDR);
130 sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpt), 0,
131 qdev_get_gpio_in(DEVICE(&s->avic), FSL_IMX31_GPT_IRQ));
133 /* Initialize all EPIT timers */
134 for (i = 0; i < FSL_IMX31_NUM_EPITS; i++) {
135 static const struct {
136 hwaddr addr;
137 unsigned int irq;
138 } epit_table[FSL_IMX31_NUM_EPITS] = {
139 { FSL_IMX31_EPIT1_ADDR, FSL_IMX31_EPIT1_IRQ },
140 { FSL_IMX31_EPIT2_ADDR, FSL_IMX31_EPIT2_IRQ },
143 s->epit[i].ccm = IMX_CCM(&s->ccm);
145 object_property_set_bool(OBJECT(&s->epit[i]), true, "realized", &err);
146 if (err) {
147 error_propagate(errp, err);
148 return;
151 sysbus_mmio_map(SYS_BUS_DEVICE(&s->epit[i]), 0, epit_table[i].addr);
152 sysbus_connect_irq(SYS_BUS_DEVICE(&s->epit[i]), 0,
153 qdev_get_gpio_in(DEVICE(&s->avic),
154 epit_table[i].irq));
157 /* Initialize all I2C */
158 for (i = 0; i < FSL_IMX31_NUM_I2CS; i++) {
159 static const struct {
160 hwaddr addr;
161 unsigned int irq;
162 } i2c_table[FSL_IMX31_NUM_I2CS] = {
163 { FSL_IMX31_I2C1_ADDR, FSL_IMX31_I2C1_IRQ },
164 { FSL_IMX31_I2C2_ADDR, FSL_IMX31_I2C2_IRQ },
165 { FSL_IMX31_I2C3_ADDR, FSL_IMX31_I2C3_IRQ }
168 /* Initialize the I2C */
169 object_property_set_bool(OBJECT(&s->i2c[i]), true, "realized", &err);
170 if (err) {
171 error_propagate(errp, err);
172 return;
174 /* Map I2C memory */
175 sysbus_mmio_map(SYS_BUS_DEVICE(&s->i2c[i]), 0, i2c_table[i].addr);
176 /* Connect I2C IRQ to PIC */
177 sysbus_connect_irq(SYS_BUS_DEVICE(&s->i2c[i]), 0,
178 qdev_get_gpio_in(DEVICE(&s->avic),
179 i2c_table[i].irq));
182 /* Initialize all GPIOs */
183 for (i = 0; i < FSL_IMX31_NUM_GPIOS; i++) {
184 static const struct {
185 hwaddr addr;
186 unsigned int irq;
187 } gpio_table[FSL_IMX31_NUM_GPIOS] = {
188 { FSL_IMX31_GPIO1_ADDR, FSL_IMX31_GPIO1_IRQ },
189 { FSL_IMX31_GPIO2_ADDR, FSL_IMX31_GPIO2_IRQ },
190 { FSL_IMX31_GPIO3_ADDR, FSL_IMX31_GPIO3_IRQ }
193 object_property_set_bool(OBJECT(&s->gpio[i]), false, "has-edge-sel",
194 &error_abort);
195 object_property_set_bool(OBJECT(&s->gpio[i]), true, "realized", &err);
196 if (err) {
197 error_propagate(errp, err);
198 return;
200 sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio[i]), 0, gpio_table[i].addr);
201 /* Connect GPIO IRQ to PIC */
202 sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio[i]), 0,
203 qdev_get_gpio_in(DEVICE(&s->avic),
204 gpio_table[i].irq));
207 /* On a real system, the first 16k is a `secure boot rom' */
208 memory_region_init_rom(&s->secure_rom, NULL, "imx31.secure_rom",
209 FSL_IMX31_SECURE_ROM_SIZE, &err);
210 if (err) {
211 error_propagate(errp, err);
212 return;
214 memory_region_add_subregion(get_system_memory(), FSL_IMX31_SECURE_ROM_ADDR,
215 &s->secure_rom);
217 /* There is also a 16k ROM */
218 memory_region_init_rom(&s->rom, NULL, "imx31.rom",
219 FSL_IMX31_ROM_SIZE, &err);
220 if (err) {
221 error_propagate(errp, err);
222 return;
224 memory_region_add_subregion(get_system_memory(), FSL_IMX31_ROM_ADDR,
225 &s->rom);
227 /* initialize internal RAM (16 KB) */
228 memory_region_init_ram(&s->iram, NULL, "imx31.iram", FSL_IMX31_IRAM_SIZE,
229 &err);
230 if (err) {
231 error_propagate(errp, err);
232 return;
234 memory_region_add_subregion(get_system_memory(), FSL_IMX31_IRAM_ADDR,
235 &s->iram);
237 /* internal RAM (16 KB) is aliased over 256 MB - 16 KB */
238 memory_region_init_alias(&s->iram_alias, NULL, "imx31.iram_alias",
239 &s->iram, 0, FSL_IMX31_IRAM_ALIAS_SIZE);
240 memory_region_add_subregion(get_system_memory(), FSL_IMX31_IRAM_ALIAS_ADDR,
241 &s->iram_alias);
244 static void fsl_imx31_class_init(ObjectClass *oc, void *data)
246 DeviceClass *dc = DEVICE_CLASS(oc);
248 dc->realize = fsl_imx31_realize;
249 dc->desc = "i.MX31 SOC";
251 * Reason: uses serial_hds in realize and the kzm board does not
252 * support multiple CPUs
254 dc->user_creatable = false;
257 static const TypeInfo fsl_imx31_type_info = {
258 .name = TYPE_FSL_IMX31,
259 .parent = TYPE_DEVICE,
260 .instance_size = sizeof(FslIMX31State),
261 .instance_init = fsl_imx31_init,
262 .class_init = fsl_imx31_class_init,
265 static void fsl_imx31_register_types(void)
267 type_register_static(&fsl_imx31_type_info);
270 type_init(fsl_imx31_register_types)