xref: /qemu/hw/arm/fsl-imx31.c (revision 32b9523ad5b44dea87792d5d8f71a87e8cc5803b)
1558df83dSJean-Christophe Dubois /*
2558df83dSJean-Christophe Dubois  * Copyright (c) 2013 Jean-Christophe Dubois <jcd@tribudubois.net>
3558df83dSJean-Christophe Dubois  *
4558df83dSJean-Christophe Dubois  * i.MX31 SOC emulation.
5558df83dSJean-Christophe Dubois  *
6558df83dSJean-Christophe Dubois  * Based on hw/arm/fsl-imx31.c
7558df83dSJean-Christophe Dubois  *
8558df83dSJean-Christophe Dubois  *  This program is free software; you can redistribute it and/or modify it
9558df83dSJean-Christophe Dubois  *  under the terms of the GNU General Public License as published by the
10558df83dSJean-Christophe Dubois  *  Free Software Foundation; either version 2 of the License, or
11558df83dSJean-Christophe Dubois  *  (at your option) any later version.
12558df83dSJean-Christophe Dubois  *
13558df83dSJean-Christophe Dubois  *  This program is distributed in the hope that it will be useful, but WITHOUT
14558df83dSJean-Christophe Dubois  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15558df83dSJean-Christophe Dubois  *  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16558df83dSJean-Christophe Dubois  *  for more details.
17558df83dSJean-Christophe Dubois  *
18558df83dSJean-Christophe Dubois  *  You should have received a copy of the GNU General Public License along
19558df83dSJean-Christophe Dubois  *  with this program; if not, see <http://www.gnu.org/licenses/>.
20558df83dSJean-Christophe Dubois  */
21558df83dSJean-Christophe Dubois 
2212b16722SPeter Maydell #include "qemu/osdep.h"
23da34e65cSMarkus Armbruster #include "qapi/error.h"
244771d756SPaolo Bonzini #include "cpu.h"
25558df83dSJean-Christophe Dubois #include "hw/arm/fsl-imx31.h"
26558df83dSJean-Christophe Dubois #include "sysemu/sysemu.h"
27558df83dSJean-Christophe Dubois #include "exec/address-spaces.h"
28a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
298228e353SMarc-André Lureau #include "chardev/char.h"
30558df83dSJean-Christophe Dubois 
31558df83dSJean-Christophe Dubois static void fsl_imx31_init(Object *obj)
32558df83dSJean-Christophe Dubois {
33558df83dSJean-Christophe Dubois     FslIMX31State *s = FSL_IMX31(obj);
34558df83dSJean-Christophe Dubois     int i;
35558df83dSJean-Christophe Dubois 
36eaa9a878SPhilippe Mathieu-Daudé     object_initialize_child(obj, "cpu", &s->cpu, sizeof(s->cpu),
37eaa9a878SPhilippe Mathieu-Daudé                             ARM_CPU_TYPE_NAME("arm1136"),
38eaa9a878SPhilippe Mathieu-Daudé                             &error_abort, NULL);
39558df83dSJean-Christophe Dubois 
40aac409c9SThomas Huth     sysbus_init_child_obj(obj, "avic", &s->avic, sizeof(s->avic),
41aac409c9SThomas Huth                           TYPE_IMX_AVIC);
42558df83dSJean-Christophe Dubois 
43aac409c9SThomas Huth     sysbus_init_child_obj(obj, "ccm", &s->ccm, sizeof(s->ccm), TYPE_IMX31_CCM);
44558df83dSJean-Christophe Dubois 
45558df83dSJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_UARTS; i++) {
46aac409c9SThomas Huth         sysbus_init_child_obj(obj, "uart[*]", &s->uart[i], sizeof(s->uart[i]),
47aac409c9SThomas Huth                               TYPE_IMX_SERIAL);
48558df83dSJean-Christophe Dubois     }
49558df83dSJean-Christophe Dubois 
50aac409c9SThomas Huth     sysbus_init_child_obj(obj, "gpt", &s->gpt, sizeof(s->gpt), TYPE_IMX31_GPT);
51558df83dSJean-Christophe Dubois 
52558df83dSJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_EPITS; i++) {
53aac409c9SThomas Huth         sysbus_init_child_obj(obj, "epit[*]", &s->epit[i], sizeof(s->epit[i]),
54aac409c9SThomas Huth                               TYPE_IMX_EPIT);
55558df83dSJean-Christophe Dubois     }
56d4e26d10SJean-Christophe Dubois 
57d4e26d10SJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_I2CS; i++) {
58aac409c9SThomas Huth         sysbus_init_child_obj(obj, "i2c[*]", &s->i2c[i], sizeof(s->i2c[i]),
59aac409c9SThomas Huth                               TYPE_IMX_I2C);
60d4e26d10SJean-Christophe Dubois     }
61dde0c4caSJean-Christophe Dubois 
62dde0c4caSJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_GPIOS; i++) {
63aac409c9SThomas Huth         sysbus_init_child_obj(obj, "gpio[*]", &s->gpio[i], sizeof(s->gpio[i]),
64aac409c9SThomas Huth                               TYPE_IMX_GPIO);
65dde0c4caSJean-Christophe Dubois     }
66558df83dSJean-Christophe Dubois }
67558df83dSJean-Christophe Dubois 
68558df83dSJean-Christophe Dubois static void fsl_imx31_realize(DeviceState *dev, Error **errp)
69558df83dSJean-Christophe Dubois {
70558df83dSJean-Christophe Dubois     FslIMX31State *s = FSL_IMX31(dev);
71558df83dSJean-Christophe Dubois     uint16_t i;
72558df83dSJean-Christophe Dubois     Error *err = NULL;
73558df83dSJean-Christophe Dubois 
74558df83dSJean-Christophe Dubois     object_property_set_bool(OBJECT(&s->cpu), true, "realized", &err);
75558df83dSJean-Christophe Dubois     if (err) {
76558df83dSJean-Christophe Dubois         error_propagate(errp, err);
77558df83dSJean-Christophe Dubois         return;
78558df83dSJean-Christophe Dubois     }
79558df83dSJean-Christophe Dubois 
80558df83dSJean-Christophe Dubois     object_property_set_bool(OBJECT(&s->avic), true, "realized", &err);
81558df83dSJean-Christophe Dubois     if (err) {
82558df83dSJean-Christophe Dubois         error_propagate(errp, err);
83558df83dSJean-Christophe Dubois         return;
84558df83dSJean-Christophe Dubois     }
85558df83dSJean-Christophe Dubois     sysbus_mmio_map(SYS_BUS_DEVICE(&s->avic), 0, FSL_IMX31_AVIC_ADDR);
86558df83dSJean-Christophe Dubois     sysbus_connect_irq(SYS_BUS_DEVICE(&s->avic), 0,
87558df83dSJean-Christophe Dubois                        qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_IRQ));
88558df83dSJean-Christophe Dubois     sysbus_connect_irq(SYS_BUS_DEVICE(&s->avic), 1,
89558df83dSJean-Christophe Dubois                        qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_FIQ));
90558df83dSJean-Christophe Dubois 
91558df83dSJean-Christophe Dubois     object_property_set_bool(OBJECT(&s->ccm), true, "realized", &err);
92558df83dSJean-Christophe Dubois     if (err) {
93558df83dSJean-Christophe Dubois         error_propagate(errp, err);
94558df83dSJean-Christophe Dubois         return;
95558df83dSJean-Christophe Dubois     }
96558df83dSJean-Christophe Dubois     sysbus_mmio_map(SYS_BUS_DEVICE(&s->ccm), 0, FSL_IMX31_CCM_ADDR);
97558df83dSJean-Christophe Dubois 
98558df83dSJean-Christophe Dubois     /* Initialize all UARTS */
99558df83dSJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_UARTS; i++) {
100558df83dSJean-Christophe Dubois         static const struct {
101558df83dSJean-Christophe Dubois             hwaddr addr;
102558df83dSJean-Christophe Dubois             unsigned int irq;
103558df83dSJean-Christophe Dubois         } serial_table[FSL_IMX31_NUM_UARTS] = {
104558df83dSJean-Christophe Dubois             { FSL_IMX31_UART1_ADDR, FSL_IMX31_UART1_IRQ },
105558df83dSJean-Christophe Dubois             { FSL_IMX31_UART2_ADDR, FSL_IMX31_UART2_IRQ },
106558df83dSJean-Christophe Dubois         };
107558df83dSJean-Christophe Dubois 
1089bca0edbSPeter Maydell         qdev_prop_set_chr(DEVICE(&s->uart[i]), "chardev", serial_hd(i));
109558df83dSJean-Christophe Dubois 
110558df83dSJean-Christophe Dubois         object_property_set_bool(OBJECT(&s->uart[i]), true, "realized", &err);
111558df83dSJean-Christophe Dubois         if (err) {
112558df83dSJean-Christophe Dubois             error_propagate(errp, err);
113558df83dSJean-Christophe Dubois             return;
114558df83dSJean-Christophe Dubois         }
115558df83dSJean-Christophe Dubois 
116558df83dSJean-Christophe Dubois         sysbus_mmio_map(SYS_BUS_DEVICE(&s->uart[i]), 0, serial_table[i].addr);
117558df83dSJean-Christophe Dubois         sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart[i]), 0,
118558df83dSJean-Christophe Dubois                            qdev_get_gpio_in(DEVICE(&s->avic),
119558df83dSJean-Christophe Dubois                                             serial_table[i].irq));
120558df83dSJean-Christophe Dubois     }
121558df83dSJean-Christophe Dubois 
122cb54d868SJean-Christophe Dubois     s->gpt.ccm = IMX_CCM(&s->ccm);
123558df83dSJean-Christophe Dubois 
124558df83dSJean-Christophe Dubois     object_property_set_bool(OBJECT(&s->gpt), true, "realized", &err);
125558df83dSJean-Christophe Dubois     if (err) {
126558df83dSJean-Christophe Dubois         error_propagate(errp, err);
127558df83dSJean-Christophe Dubois         return;
128558df83dSJean-Christophe Dubois     }
129558df83dSJean-Christophe Dubois 
130558df83dSJean-Christophe Dubois     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpt), 0, FSL_IMX31_GPT_ADDR);
131558df83dSJean-Christophe Dubois     sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpt), 0,
132558df83dSJean-Christophe Dubois                        qdev_get_gpio_in(DEVICE(&s->avic), FSL_IMX31_GPT_IRQ));
133558df83dSJean-Christophe Dubois 
134558df83dSJean-Christophe Dubois     /* Initialize all EPIT timers */
135558df83dSJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_EPITS; i++) {
136558df83dSJean-Christophe Dubois         static const struct {
137558df83dSJean-Christophe Dubois             hwaddr addr;
138558df83dSJean-Christophe Dubois             unsigned int irq;
139558df83dSJean-Christophe Dubois         } epit_table[FSL_IMX31_NUM_EPITS] = {
140558df83dSJean-Christophe Dubois             { FSL_IMX31_EPIT1_ADDR, FSL_IMX31_EPIT1_IRQ },
141558df83dSJean-Christophe Dubois             { FSL_IMX31_EPIT2_ADDR, FSL_IMX31_EPIT2_IRQ },
142558df83dSJean-Christophe Dubois         };
143558df83dSJean-Christophe Dubois 
144cb54d868SJean-Christophe Dubois         s->epit[i].ccm = IMX_CCM(&s->ccm);
145558df83dSJean-Christophe Dubois 
146558df83dSJean-Christophe Dubois         object_property_set_bool(OBJECT(&s->epit[i]), true, "realized", &err);
147558df83dSJean-Christophe Dubois         if (err) {
148558df83dSJean-Christophe Dubois             error_propagate(errp, err);
149558df83dSJean-Christophe Dubois             return;
150558df83dSJean-Christophe Dubois         }
151558df83dSJean-Christophe Dubois 
152558df83dSJean-Christophe Dubois         sysbus_mmio_map(SYS_BUS_DEVICE(&s->epit[i]), 0, epit_table[i].addr);
153558df83dSJean-Christophe Dubois         sysbus_connect_irq(SYS_BUS_DEVICE(&s->epit[i]), 0,
154558df83dSJean-Christophe Dubois                            qdev_get_gpio_in(DEVICE(&s->avic),
155558df83dSJean-Christophe Dubois                                             epit_table[i].irq));
156558df83dSJean-Christophe Dubois     }
157558df83dSJean-Christophe Dubois 
158d4e26d10SJean-Christophe Dubois     /* Initialize all I2C */
159d4e26d10SJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_I2CS; i++) {
160d4e26d10SJean-Christophe Dubois         static const struct {
161d4e26d10SJean-Christophe Dubois             hwaddr addr;
162d4e26d10SJean-Christophe Dubois             unsigned int irq;
163d4e26d10SJean-Christophe Dubois         } i2c_table[FSL_IMX31_NUM_I2CS] = {
164d4e26d10SJean-Christophe Dubois             { FSL_IMX31_I2C1_ADDR, FSL_IMX31_I2C1_IRQ },
165d4e26d10SJean-Christophe Dubois             { FSL_IMX31_I2C2_ADDR, FSL_IMX31_I2C2_IRQ },
166d4e26d10SJean-Christophe Dubois             { FSL_IMX31_I2C3_ADDR, FSL_IMX31_I2C3_IRQ }
167d4e26d10SJean-Christophe Dubois         };
168d4e26d10SJean-Christophe Dubois 
169d4e26d10SJean-Christophe Dubois         /* Initialize the I2C */
170d4e26d10SJean-Christophe Dubois         object_property_set_bool(OBJECT(&s->i2c[i]), true, "realized", &err);
171d4e26d10SJean-Christophe Dubois         if (err) {
172d4e26d10SJean-Christophe Dubois             error_propagate(errp, err);
173d4e26d10SJean-Christophe Dubois             return;
174d4e26d10SJean-Christophe Dubois         }
175d4e26d10SJean-Christophe Dubois         /* Map I2C memory */
176d4e26d10SJean-Christophe Dubois         sysbus_mmio_map(SYS_BUS_DEVICE(&s->i2c[i]), 0, i2c_table[i].addr);
177d4e26d10SJean-Christophe Dubois         /* Connect I2C IRQ to PIC */
178d4e26d10SJean-Christophe Dubois         sysbus_connect_irq(SYS_BUS_DEVICE(&s->i2c[i]), 0,
179d4e26d10SJean-Christophe Dubois                            qdev_get_gpio_in(DEVICE(&s->avic),
180d4e26d10SJean-Christophe Dubois                                             i2c_table[i].irq));
181d4e26d10SJean-Christophe Dubois     }
182d4e26d10SJean-Christophe Dubois 
183dde0c4caSJean-Christophe Dubois     /* Initialize all GPIOs */
184dde0c4caSJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_GPIOS; i++) {
185dde0c4caSJean-Christophe Dubois         static const struct {
186dde0c4caSJean-Christophe Dubois             hwaddr addr;
187dde0c4caSJean-Christophe Dubois             unsigned int irq;
188dde0c4caSJean-Christophe Dubois         } gpio_table[FSL_IMX31_NUM_GPIOS] = {
189dde0c4caSJean-Christophe Dubois             { FSL_IMX31_GPIO1_ADDR, FSL_IMX31_GPIO1_IRQ },
190dde0c4caSJean-Christophe Dubois             { FSL_IMX31_GPIO2_ADDR, FSL_IMX31_GPIO2_IRQ },
191dde0c4caSJean-Christophe Dubois             { FSL_IMX31_GPIO3_ADDR, FSL_IMX31_GPIO3_IRQ }
192dde0c4caSJean-Christophe Dubois         };
193dde0c4caSJean-Christophe Dubois 
194dde0c4caSJean-Christophe Dubois         object_property_set_bool(OBJECT(&s->gpio[i]), false, "has-edge-sel",
195dde0c4caSJean-Christophe Dubois                                  &error_abort);
196dde0c4caSJean-Christophe Dubois         object_property_set_bool(OBJECT(&s->gpio[i]), true, "realized", &err);
197dde0c4caSJean-Christophe Dubois         if (err) {
198dde0c4caSJean-Christophe Dubois             error_propagate(errp, err);
199dde0c4caSJean-Christophe Dubois             return;
200dde0c4caSJean-Christophe Dubois         }
201dde0c4caSJean-Christophe Dubois         sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio[i]), 0, gpio_table[i].addr);
202dde0c4caSJean-Christophe Dubois         /* Connect GPIO IRQ to PIC */
203dde0c4caSJean-Christophe Dubois         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio[i]), 0,
204dde0c4caSJean-Christophe Dubois                            qdev_get_gpio_in(DEVICE(&s->avic),
205dde0c4caSJean-Christophe Dubois                                             gpio_table[i].irq));
206dde0c4caSJean-Christophe Dubois     }
207dde0c4caSJean-Christophe Dubois 
208558df83dSJean-Christophe Dubois     /* On a real system, the first 16k is a `secure boot rom' */
209*32b9523aSPhilippe Mathieu-Daudé     memory_region_init_rom(&s->secure_rom, OBJECT(dev), "imx31.secure_rom",
210558df83dSJean-Christophe Dubois                            FSL_IMX31_SECURE_ROM_SIZE, &err);
211558df83dSJean-Christophe Dubois     if (err) {
212558df83dSJean-Christophe Dubois         error_propagate(errp, err);
213558df83dSJean-Christophe Dubois         return;
214558df83dSJean-Christophe Dubois     }
215558df83dSJean-Christophe Dubois     memory_region_add_subregion(get_system_memory(), FSL_IMX31_SECURE_ROM_ADDR,
216558df83dSJean-Christophe Dubois                                 &s->secure_rom);
217558df83dSJean-Christophe Dubois 
218558df83dSJean-Christophe Dubois     /* There is also a 16k ROM */
219*32b9523aSPhilippe Mathieu-Daudé     memory_region_init_rom(&s->rom, OBJECT(dev), "imx31.rom",
220558df83dSJean-Christophe Dubois                            FSL_IMX31_ROM_SIZE, &err);
221558df83dSJean-Christophe Dubois     if (err) {
222558df83dSJean-Christophe Dubois         error_propagate(errp, err);
223558df83dSJean-Christophe Dubois         return;
224558df83dSJean-Christophe Dubois     }
225558df83dSJean-Christophe Dubois     memory_region_add_subregion(get_system_memory(), FSL_IMX31_ROM_ADDR,
226558df83dSJean-Christophe Dubois                                 &s->rom);
227558df83dSJean-Christophe Dubois 
228558df83dSJean-Christophe Dubois     /* initialize internal RAM (16 KB) */
22998a99ce0SPeter Maydell     memory_region_init_ram(&s->iram, NULL, "imx31.iram", FSL_IMX31_IRAM_SIZE,
230558df83dSJean-Christophe Dubois                            &err);
231558df83dSJean-Christophe Dubois     if (err) {
232558df83dSJean-Christophe Dubois         error_propagate(errp, err);
233558df83dSJean-Christophe Dubois         return;
234558df83dSJean-Christophe Dubois     }
235558df83dSJean-Christophe Dubois     memory_region_add_subregion(get_system_memory(), FSL_IMX31_IRAM_ADDR,
236558df83dSJean-Christophe Dubois                                 &s->iram);
237558df83dSJean-Christophe Dubois 
238558df83dSJean-Christophe Dubois     /* internal RAM (16 KB) is aliased over 256 MB - 16 KB */
239*32b9523aSPhilippe Mathieu-Daudé     memory_region_init_alias(&s->iram_alias, OBJECT(dev), "imx31.iram_alias",
240558df83dSJean-Christophe Dubois                              &s->iram, 0, FSL_IMX31_IRAM_ALIAS_SIZE);
241558df83dSJean-Christophe Dubois     memory_region_add_subregion(get_system_memory(), FSL_IMX31_IRAM_ALIAS_ADDR,
242558df83dSJean-Christophe Dubois                                 &s->iram_alias);
243558df83dSJean-Christophe Dubois }
244558df83dSJean-Christophe Dubois 
245558df83dSJean-Christophe Dubois static void fsl_imx31_class_init(ObjectClass *oc, void *data)
246558df83dSJean-Christophe Dubois {
247558df83dSJean-Christophe Dubois     DeviceClass *dc = DEVICE_CLASS(oc);
248558df83dSJean-Christophe Dubois 
249558df83dSJean-Christophe Dubois     dc->realize = fsl_imx31_realize;
250eccfa35eSJean-Christophe Dubois     dc->desc = "i.MX31 SOC";
251e4e05b7bSThomas Huth     /*
252e4e05b7bSThomas Huth      * Reason: uses serial_hds in realize and the kzm board does not
253e4e05b7bSThomas Huth      * support multiple CPUs
254e4e05b7bSThomas Huth      */
255e4e05b7bSThomas Huth     dc->user_creatable = false;
256558df83dSJean-Christophe Dubois }
257558df83dSJean-Christophe Dubois 
258558df83dSJean-Christophe Dubois static const TypeInfo fsl_imx31_type_info = {
259558df83dSJean-Christophe Dubois     .name = TYPE_FSL_IMX31,
260558df83dSJean-Christophe Dubois     .parent = TYPE_DEVICE,
261558df83dSJean-Christophe Dubois     .instance_size = sizeof(FslIMX31State),
262558df83dSJean-Christophe Dubois     .instance_init = fsl_imx31_init,
263558df83dSJean-Christophe Dubois     .class_init = fsl_imx31_class_init,
264558df83dSJean-Christophe Dubois };
265558df83dSJean-Christophe Dubois 
266558df83dSJean-Christophe Dubois static void fsl_imx31_register_types(void)
267558df83dSJean-Christophe Dubois {
268558df83dSJean-Christophe Dubois     type_register_static(&fsl_imx31_type_info);
269558df83dSJean-Christophe Dubois }
270558df83dSJean-Christophe Dubois 
271558df83dSJean-Christophe Dubois type_init(fsl_imx31_register_types)
272