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
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"
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 "sysemu/char.h"
32 static void fsl_imx31_init(Object
*obj
)
34 FslIMX31State
*s
= FSL_IMX31(obj
);
37 object_initialize(&s
->cpu
, sizeof(s
->cpu
), "arm1136-" TYPE_ARM_CPU
);
39 object_initialize(&s
->avic
, sizeof(s
->avic
), TYPE_IMX_AVIC
);
40 qdev_set_parent_bus(DEVICE(&s
->avic
), sysbus_get_default());
42 object_initialize(&s
->ccm
, sizeof(s
->ccm
), TYPE_IMX31_CCM
);
43 qdev_set_parent_bus(DEVICE(&s
->ccm
), sysbus_get_default());
45 for (i
= 0; i
< FSL_IMX31_NUM_UARTS
; i
++) {
46 object_initialize(&s
->uart
[i
], sizeof(s
->uart
[i
]), TYPE_IMX_SERIAL
);
47 qdev_set_parent_bus(DEVICE(&s
->uart
[i
]), sysbus_get_default());
50 object_initialize(&s
->gpt
, sizeof(s
->gpt
), TYPE_IMX31_GPT
);
51 qdev_set_parent_bus(DEVICE(&s
->gpt
), sysbus_get_default());
53 for (i
= 0; i
< FSL_IMX31_NUM_EPITS
; i
++) {
54 object_initialize(&s
->epit
[i
], sizeof(s
->epit
[i
]), TYPE_IMX_EPIT
);
55 qdev_set_parent_bus(DEVICE(&s
->epit
[i
]), sysbus_get_default());
58 for (i
= 0; i
< FSL_IMX31_NUM_I2CS
; i
++) {
59 object_initialize(&s
->i2c
[i
], sizeof(s
->i2c
[i
]), TYPE_IMX_I2C
);
60 qdev_set_parent_bus(DEVICE(&s
->i2c
[i
]), sysbus_get_default());
63 for (i
= 0; i
< FSL_IMX31_NUM_GPIOS
; i
++) {
64 object_initialize(&s
->gpio
[i
], sizeof(s
->gpio
[i
]), TYPE_IMX_GPIO
);
65 qdev_set_parent_bus(DEVICE(&s
->gpio
[i
]), sysbus_get_default());
69 static void fsl_imx31_realize(DeviceState
*dev
, Error
**errp
)
71 FslIMX31State
*s
= FSL_IMX31(dev
);
75 object_property_set_bool(OBJECT(&s
->cpu
), true, "realized", &err
);
77 error_propagate(errp
, err
);
81 object_property_set_bool(OBJECT(&s
->avic
), true, "realized", &err
);
83 error_propagate(errp
, err
);
86 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->avic
), 0, FSL_IMX31_AVIC_ADDR
);
87 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->avic
), 0,
88 qdev_get_gpio_in(DEVICE(&s
->cpu
), ARM_CPU_IRQ
));
89 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->avic
), 1,
90 qdev_get_gpio_in(DEVICE(&s
->cpu
), ARM_CPU_FIQ
));
92 object_property_set_bool(OBJECT(&s
->ccm
), true, "realized", &err
);
94 error_propagate(errp
, err
);
97 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->ccm
), 0, FSL_IMX31_CCM_ADDR
);
99 /* Initialize all UARTS */
100 for (i
= 0; i
< FSL_IMX31_NUM_UARTS
; i
++) {
101 static const struct {
104 } serial_table
[FSL_IMX31_NUM_UARTS
] = {
105 { FSL_IMX31_UART1_ADDR
, FSL_IMX31_UART1_IRQ
},
106 { FSL_IMX31_UART2_ADDR
, FSL_IMX31_UART2_IRQ
},
109 if (i
< MAX_SERIAL_PORTS
) {
110 CharDriverState
*chr
;
116 snprintf(label
, sizeof(label
), "imx31.uart%d", i
);
117 chr
= qemu_chr_new(label
, "null");
120 qdev_prop_set_chr(DEVICE(&s
->uart
[i
]), "chardev", chr
);
123 object_property_set_bool(OBJECT(&s
->uart
[i
]), true, "realized", &err
);
125 error_propagate(errp
, err
);
129 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->uart
[i
]), 0, serial_table
[i
].addr
);
130 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->uart
[i
]), 0,
131 qdev_get_gpio_in(DEVICE(&s
->avic
),
132 serial_table
[i
].irq
));
135 s
->gpt
.ccm
= IMX_CCM(&s
->ccm
);
137 object_property_set_bool(OBJECT(&s
->gpt
), true, "realized", &err
);
139 error_propagate(errp
, err
);
143 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->gpt
), 0, FSL_IMX31_GPT_ADDR
);
144 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->gpt
), 0,
145 qdev_get_gpio_in(DEVICE(&s
->avic
), FSL_IMX31_GPT_IRQ
));
147 /* Initialize all EPIT timers */
148 for (i
= 0; i
< FSL_IMX31_NUM_EPITS
; i
++) {
149 static const struct {
152 } epit_table
[FSL_IMX31_NUM_EPITS
] = {
153 { FSL_IMX31_EPIT1_ADDR
, FSL_IMX31_EPIT1_IRQ
},
154 { FSL_IMX31_EPIT2_ADDR
, FSL_IMX31_EPIT2_IRQ
},
157 s
->epit
[i
].ccm
= IMX_CCM(&s
->ccm
);
159 object_property_set_bool(OBJECT(&s
->epit
[i
]), true, "realized", &err
);
161 error_propagate(errp
, err
);
165 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->epit
[i
]), 0, epit_table
[i
].addr
);
166 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->epit
[i
]), 0,
167 qdev_get_gpio_in(DEVICE(&s
->avic
),
171 /* Initialize all I2C */
172 for (i
= 0; i
< FSL_IMX31_NUM_I2CS
; i
++) {
173 static const struct {
176 } i2c_table
[FSL_IMX31_NUM_I2CS
] = {
177 { FSL_IMX31_I2C1_ADDR
, FSL_IMX31_I2C1_IRQ
},
178 { FSL_IMX31_I2C2_ADDR
, FSL_IMX31_I2C2_IRQ
},
179 { FSL_IMX31_I2C3_ADDR
, FSL_IMX31_I2C3_IRQ
}
182 /* Initialize the I2C */
183 object_property_set_bool(OBJECT(&s
->i2c
[i
]), true, "realized", &err
);
185 error_propagate(errp
, err
);
189 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->i2c
[i
]), 0, i2c_table
[i
].addr
);
190 /* Connect I2C IRQ to PIC */
191 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->i2c
[i
]), 0,
192 qdev_get_gpio_in(DEVICE(&s
->avic
),
196 /* Initialize all GPIOs */
197 for (i
= 0; i
< FSL_IMX31_NUM_GPIOS
; i
++) {
198 static const struct {
201 } gpio_table
[FSL_IMX31_NUM_GPIOS
] = {
202 { FSL_IMX31_GPIO1_ADDR
, FSL_IMX31_GPIO1_IRQ
},
203 { FSL_IMX31_GPIO2_ADDR
, FSL_IMX31_GPIO2_IRQ
},
204 { FSL_IMX31_GPIO3_ADDR
, FSL_IMX31_GPIO3_IRQ
}
207 object_property_set_bool(OBJECT(&s
->gpio
[i
]), false, "has-edge-sel",
209 object_property_set_bool(OBJECT(&s
->gpio
[i
]), true, "realized", &err
);
211 error_propagate(errp
, err
);
214 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->gpio
[i
]), 0, gpio_table
[i
].addr
);
215 /* Connect GPIO IRQ to PIC */
216 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->gpio
[i
]), 0,
217 qdev_get_gpio_in(DEVICE(&s
->avic
),
221 /* On a real system, the first 16k is a `secure boot rom' */
222 memory_region_init_rom(&s
->secure_rom
, NULL
, "imx31.secure_rom",
223 FSL_IMX31_SECURE_ROM_SIZE
, &err
);
225 error_propagate(errp
, err
);
228 memory_region_add_subregion(get_system_memory(), FSL_IMX31_SECURE_ROM_ADDR
,
231 /* There is also a 16k ROM */
232 memory_region_init_rom(&s
->rom
, NULL
, "imx31.rom",
233 FSL_IMX31_ROM_SIZE
, &err
);
235 error_propagate(errp
, err
);
238 memory_region_add_subregion(get_system_memory(), FSL_IMX31_ROM_ADDR
,
241 /* initialize internal RAM (16 KB) */
242 memory_region_init_ram(&s
->iram
, NULL
, "imx31.iram", FSL_IMX31_IRAM_SIZE
,
245 error_propagate(errp
, err
);
248 memory_region_add_subregion(get_system_memory(), FSL_IMX31_IRAM_ADDR
,
250 vmstate_register_ram_global(&s
->iram
);
252 /* internal RAM (16 KB) is aliased over 256 MB - 16 KB */
253 memory_region_init_alias(&s
->iram_alias
, NULL
, "imx31.iram_alias",
254 &s
->iram
, 0, FSL_IMX31_IRAM_ALIAS_SIZE
);
255 memory_region_add_subregion(get_system_memory(), FSL_IMX31_IRAM_ALIAS_ADDR
,
259 static void fsl_imx31_class_init(ObjectClass
*oc
, void *data
)
261 DeviceClass
*dc
= DEVICE_CLASS(oc
);
263 dc
->realize
= fsl_imx31_realize
;
266 * Reason: creates an ARM CPU, thus use after free(), see
267 * arm_cpu_class_init()
269 dc
->cannot_destroy_with_object_finalize_yet
= true;
270 dc
->desc
= "i.MX31 SOC";
273 static const TypeInfo fsl_imx31_type_info
= {
274 .name
= TYPE_FSL_IMX31
,
275 .parent
= TYPE_DEVICE
,
276 .instance_size
= sizeof(FslIMX31State
),
277 .instance_init
= fsl_imx31_init
,
278 .class_init
= fsl_imx31_class_init
,
281 static void fsl_imx31_register_types(void)
283 type_register_static(&fsl_imx31_type_info
);
286 type_init(fsl_imx31_register_types
)