xref: /openbmc/qemu/hw/arm/fsl-imx31.c (revision 7a1dc45af581d2b643cdbf33c01fd96271616fbd)
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"
24558df83dSJean-Christophe Dubois #include "hw/arm/fsl-imx31.h"
25558df83dSJean-Christophe Dubois #include "sysemu/sysemu.h"
26558df83dSJean-Christophe Dubois #include "exec/address-spaces.h"
27a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
288228e353SMarc-André Lureau #include "chardev/char.h"
29*d780d056SPhilippe Mathieu-Daudé #include "target/arm/cpu-qom.h"
30558df83dSJean-Christophe Dubois 
fsl_imx31_init(Object * obj)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 
369fc7fc4dSMarkus Armbruster     object_initialize_child(obj, "cpu", &s->cpu, ARM_CPU_TYPE_NAME("arm1136"));
37558df83dSJean-Christophe Dubois 
38db873cc5SMarkus Armbruster     object_initialize_child(obj, "avic", &s->avic, TYPE_IMX_AVIC);
39558df83dSJean-Christophe Dubois 
40db873cc5SMarkus Armbruster     object_initialize_child(obj, "ccm", &s->ccm, TYPE_IMX31_CCM);
41558df83dSJean-Christophe Dubois 
42558df83dSJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_UARTS; i++) {
43db873cc5SMarkus Armbruster         object_initialize_child(obj, "uart[*]", &s->uart[i], TYPE_IMX_SERIAL);
44558df83dSJean-Christophe Dubois     }
45558df83dSJean-Christophe Dubois 
46db873cc5SMarkus Armbruster     object_initialize_child(obj, "gpt", &s->gpt, TYPE_IMX31_GPT);
47558df83dSJean-Christophe Dubois 
48558df83dSJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_EPITS; i++) {
49db873cc5SMarkus Armbruster         object_initialize_child(obj, "epit[*]", &s->epit[i], TYPE_IMX_EPIT);
50558df83dSJean-Christophe Dubois     }
51d4e26d10SJean-Christophe Dubois 
52d4e26d10SJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_I2CS; i++) {
53db873cc5SMarkus Armbruster         object_initialize_child(obj, "i2c[*]", &s->i2c[i], TYPE_IMX_I2C);
54d4e26d10SJean-Christophe Dubois     }
55dde0c4caSJean-Christophe Dubois 
56dde0c4caSJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_GPIOS; i++) {
57db873cc5SMarkus Armbruster         object_initialize_child(obj, "gpio[*]", &s->gpio[i], TYPE_IMX_GPIO);
58dde0c4caSJean-Christophe Dubois     }
59b9e521ddSGuenter Roeck 
60db873cc5SMarkus Armbruster     object_initialize_child(obj, "wdt", &s->wdt, TYPE_IMX2_WDT);
61558df83dSJean-Christophe Dubois }
62558df83dSJean-Christophe Dubois 
fsl_imx31_realize(DeviceState * dev,Error ** errp)63558df83dSJean-Christophe Dubois static void fsl_imx31_realize(DeviceState *dev, Error **errp)
64558df83dSJean-Christophe Dubois {
65558df83dSJean-Christophe Dubois     FslIMX31State *s = FSL_IMX31(dev);
66558df83dSJean-Christophe Dubois     uint16_t i;
67558df83dSJean-Christophe Dubois 
68668f62ecSMarkus Armbruster     if (!qdev_realize(DEVICE(&s->cpu), NULL, errp)) {
69558df83dSJean-Christophe Dubois         return;
70558df83dSJean-Christophe Dubois     }
71558df83dSJean-Christophe Dubois 
72668f62ecSMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->avic), errp)) {
73558df83dSJean-Christophe Dubois         return;
74558df83dSJean-Christophe Dubois     }
75558df83dSJean-Christophe Dubois     sysbus_mmio_map(SYS_BUS_DEVICE(&s->avic), 0, FSL_IMX31_AVIC_ADDR);
76558df83dSJean-Christophe Dubois     sysbus_connect_irq(SYS_BUS_DEVICE(&s->avic), 0,
77558df83dSJean-Christophe Dubois                        qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_IRQ));
78558df83dSJean-Christophe Dubois     sysbus_connect_irq(SYS_BUS_DEVICE(&s->avic), 1,
79558df83dSJean-Christophe Dubois                        qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_FIQ));
80558df83dSJean-Christophe Dubois 
81668f62ecSMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->ccm), errp)) {
82558df83dSJean-Christophe Dubois         return;
83558df83dSJean-Christophe Dubois     }
84558df83dSJean-Christophe Dubois     sysbus_mmio_map(SYS_BUS_DEVICE(&s->ccm), 0, FSL_IMX31_CCM_ADDR);
85558df83dSJean-Christophe Dubois 
86558df83dSJean-Christophe Dubois     /* Initialize all UARTS */
87558df83dSJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_UARTS; i++) {
88558df83dSJean-Christophe Dubois         static const struct {
89558df83dSJean-Christophe Dubois             hwaddr addr;
90558df83dSJean-Christophe Dubois             unsigned int irq;
91558df83dSJean-Christophe Dubois         } serial_table[FSL_IMX31_NUM_UARTS] = {
92558df83dSJean-Christophe Dubois             { FSL_IMX31_UART1_ADDR, FSL_IMX31_UART1_IRQ },
93558df83dSJean-Christophe Dubois             { FSL_IMX31_UART2_ADDR, FSL_IMX31_UART2_IRQ },
94558df83dSJean-Christophe Dubois         };
95558df83dSJean-Christophe Dubois 
969bca0edbSPeter Maydell         qdev_prop_set_chr(DEVICE(&s->uart[i]), "chardev", serial_hd(i));
97558df83dSJean-Christophe Dubois 
98668f62ecSMarkus Armbruster         if (!sysbus_realize(SYS_BUS_DEVICE(&s->uart[i]), errp)) {
99558df83dSJean-Christophe Dubois             return;
100558df83dSJean-Christophe Dubois         }
101558df83dSJean-Christophe Dubois 
102558df83dSJean-Christophe Dubois         sysbus_mmio_map(SYS_BUS_DEVICE(&s->uart[i]), 0, serial_table[i].addr);
103558df83dSJean-Christophe Dubois         sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart[i]), 0,
104558df83dSJean-Christophe Dubois                            qdev_get_gpio_in(DEVICE(&s->avic),
105558df83dSJean-Christophe Dubois                                             serial_table[i].irq));
106558df83dSJean-Christophe Dubois     }
107558df83dSJean-Christophe Dubois 
108cb54d868SJean-Christophe Dubois     s->gpt.ccm = IMX_CCM(&s->ccm);
109558df83dSJean-Christophe Dubois 
110668f62ecSMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpt), errp)) {
111558df83dSJean-Christophe Dubois         return;
112558df83dSJean-Christophe Dubois     }
113558df83dSJean-Christophe Dubois 
114558df83dSJean-Christophe Dubois     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpt), 0, FSL_IMX31_GPT_ADDR);
115558df83dSJean-Christophe Dubois     sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpt), 0,
116558df83dSJean-Christophe Dubois                        qdev_get_gpio_in(DEVICE(&s->avic), FSL_IMX31_GPT_IRQ));
117558df83dSJean-Christophe Dubois 
118558df83dSJean-Christophe Dubois     /* Initialize all EPIT timers */
119558df83dSJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_EPITS; i++) {
120558df83dSJean-Christophe Dubois         static const struct {
121558df83dSJean-Christophe Dubois             hwaddr addr;
122558df83dSJean-Christophe Dubois             unsigned int irq;
123558df83dSJean-Christophe Dubois         } epit_table[FSL_IMX31_NUM_EPITS] = {
124558df83dSJean-Christophe Dubois             { FSL_IMX31_EPIT1_ADDR, FSL_IMX31_EPIT1_IRQ },
125558df83dSJean-Christophe Dubois             { FSL_IMX31_EPIT2_ADDR, FSL_IMX31_EPIT2_IRQ },
126558df83dSJean-Christophe Dubois         };
127558df83dSJean-Christophe Dubois 
128cb54d868SJean-Christophe Dubois         s->epit[i].ccm = IMX_CCM(&s->ccm);
129558df83dSJean-Christophe Dubois 
130668f62ecSMarkus Armbruster         if (!sysbus_realize(SYS_BUS_DEVICE(&s->epit[i]), errp)) {
131558df83dSJean-Christophe Dubois             return;
132558df83dSJean-Christophe Dubois         }
133558df83dSJean-Christophe Dubois 
134558df83dSJean-Christophe Dubois         sysbus_mmio_map(SYS_BUS_DEVICE(&s->epit[i]), 0, epit_table[i].addr);
135558df83dSJean-Christophe Dubois         sysbus_connect_irq(SYS_BUS_DEVICE(&s->epit[i]), 0,
136558df83dSJean-Christophe Dubois                            qdev_get_gpio_in(DEVICE(&s->avic),
137558df83dSJean-Christophe Dubois                                             epit_table[i].irq));
138558df83dSJean-Christophe Dubois     }
139558df83dSJean-Christophe Dubois 
140d4e26d10SJean-Christophe Dubois     /* Initialize all I2C */
141d4e26d10SJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_I2CS; i++) {
142d4e26d10SJean-Christophe Dubois         static const struct {
143d4e26d10SJean-Christophe Dubois             hwaddr addr;
144d4e26d10SJean-Christophe Dubois             unsigned int irq;
145d4e26d10SJean-Christophe Dubois         } i2c_table[FSL_IMX31_NUM_I2CS] = {
146d4e26d10SJean-Christophe Dubois             { FSL_IMX31_I2C1_ADDR, FSL_IMX31_I2C1_IRQ },
147d4e26d10SJean-Christophe Dubois             { FSL_IMX31_I2C2_ADDR, FSL_IMX31_I2C2_IRQ },
148d4e26d10SJean-Christophe Dubois             { FSL_IMX31_I2C3_ADDR, FSL_IMX31_I2C3_IRQ }
149d4e26d10SJean-Christophe Dubois         };
150d4e26d10SJean-Christophe Dubois 
151d4e26d10SJean-Christophe Dubois         /* Initialize the I2C */
152668f62ecSMarkus Armbruster         if (!sysbus_realize(SYS_BUS_DEVICE(&s->i2c[i]), errp)) {
153d4e26d10SJean-Christophe Dubois             return;
154d4e26d10SJean-Christophe Dubois         }
155d4e26d10SJean-Christophe Dubois         /* Map I2C memory */
156d4e26d10SJean-Christophe Dubois         sysbus_mmio_map(SYS_BUS_DEVICE(&s->i2c[i]), 0, i2c_table[i].addr);
157d4e26d10SJean-Christophe Dubois         /* Connect I2C IRQ to PIC */
158d4e26d10SJean-Christophe Dubois         sysbus_connect_irq(SYS_BUS_DEVICE(&s->i2c[i]), 0,
159d4e26d10SJean-Christophe Dubois                            qdev_get_gpio_in(DEVICE(&s->avic),
160d4e26d10SJean-Christophe Dubois                                             i2c_table[i].irq));
161d4e26d10SJean-Christophe Dubois     }
162d4e26d10SJean-Christophe Dubois 
163dde0c4caSJean-Christophe Dubois     /* Initialize all GPIOs */
164dde0c4caSJean-Christophe Dubois     for (i = 0; i < FSL_IMX31_NUM_GPIOS; i++) {
165dde0c4caSJean-Christophe Dubois         static const struct {
166dde0c4caSJean-Christophe Dubois             hwaddr addr;
167dde0c4caSJean-Christophe Dubois             unsigned int irq;
168dde0c4caSJean-Christophe Dubois         } gpio_table[FSL_IMX31_NUM_GPIOS] = {
169dde0c4caSJean-Christophe Dubois             { FSL_IMX31_GPIO1_ADDR, FSL_IMX31_GPIO1_IRQ },
170dde0c4caSJean-Christophe Dubois             { FSL_IMX31_GPIO2_ADDR, FSL_IMX31_GPIO2_IRQ },
171dde0c4caSJean-Christophe Dubois             { FSL_IMX31_GPIO3_ADDR, FSL_IMX31_GPIO3_IRQ }
172dde0c4caSJean-Christophe Dubois         };
173dde0c4caSJean-Christophe Dubois 
1745325cc34SMarkus Armbruster         object_property_set_bool(OBJECT(&s->gpio[i]), "has-edge-sel", false,
175dde0c4caSJean-Christophe Dubois                                  &error_abort);
176668f62ecSMarkus Armbruster         if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio[i]), errp)) {
177dde0c4caSJean-Christophe Dubois             return;
178dde0c4caSJean-Christophe Dubois         }
179dde0c4caSJean-Christophe Dubois         sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio[i]), 0, gpio_table[i].addr);
180dde0c4caSJean-Christophe Dubois         /* Connect GPIO IRQ to PIC */
181dde0c4caSJean-Christophe Dubois         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio[i]), 0,
182dde0c4caSJean-Christophe Dubois                            qdev_get_gpio_in(DEVICE(&s->avic),
183dde0c4caSJean-Christophe Dubois                                             gpio_table[i].irq));
184dde0c4caSJean-Christophe Dubois     }
185dde0c4caSJean-Christophe Dubois 
186b9e521ddSGuenter Roeck     /* Watchdog */
187db873cc5SMarkus Armbruster     sysbus_realize(SYS_BUS_DEVICE(&s->wdt), &error_abort);
188b9e521ddSGuenter Roeck     sysbus_mmio_map(SYS_BUS_DEVICE(&s->wdt), 0, FSL_IMX31_WDT_ADDR);
189b9e521ddSGuenter Roeck 
190558df83dSJean-Christophe Dubois     /* On a real system, the first 16k is a `secure boot rom' */
191419d8524SPhilippe Mathieu-Daudé     if (!memory_region_init_rom(&s->secure_rom, OBJECT(dev), "imx31.secure_rom",
192419d8524SPhilippe Mathieu-Daudé                                 FSL_IMX31_SECURE_ROM_SIZE, errp)) {
193558df83dSJean-Christophe Dubois         return;
194558df83dSJean-Christophe Dubois     }
195558df83dSJean-Christophe Dubois     memory_region_add_subregion(get_system_memory(), FSL_IMX31_SECURE_ROM_ADDR,
196558df83dSJean-Christophe Dubois                                 &s->secure_rom);
197558df83dSJean-Christophe Dubois 
198558df83dSJean-Christophe Dubois     /* There is also a 16k ROM */
199419d8524SPhilippe Mathieu-Daudé     if (!memory_region_init_rom(&s->rom, OBJECT(dev), "imx31.rom",
200419d8524SPhilippe Mathieu-Daudé                                 FSL_IMX31_ROM_SIZE, errp)) {
201558df83dSJean-Christophe Dubois         return;
202558df83dSJean-Christophe Dubois     }
203558df83dSJean-Christophe Dubois     memory_region_add_subregion(get_system_memory(), FSL_IMX31_ROM_ADDR,
204558df83dSJean-Christophe Dubois                                 &s->rom);
205558df83dSJean-Christophe Dubois 
206558df83dSJean-Christophe Dubois     /* initialize internal RAM (16 KB) */
2072198f5f0SPhilippe Mathieu-Daudé     if (!memory_region_init_ram(&s->iram, NULL, "imx31.iram",
2082198f5f0SPhilippe Mathieu-Daudé                                 FSL_IMX31_IRAM_SIZE, errp)) {
209558df83dSJean-Christophe Dubois         return;
210558df83dSJean-Christophe Dubois     }
211558df83dSJean-Christophe Dubois     memory_region_add_subregion(get_system_memory(), FSL_IMX31_IRAM_ADDR,
212558df83dSJean-Christophe Dubois                                 &s->iram);
213558df83dSJean-Christophe Dubois 
214558df83dSJean-Christophe Dubois     /* internal RAM (16 KB) is aliased over 256 MB - 16 KB */
21532b9523aSPhilippe Mathieu-Daudé     memory_region_init_alias(&s->iram_alias, OBJECT(dev), "imx31.iram_alias",
216558df83dSJean-Christophe Dubois                              &s->iram, 0, FSL_IMX31_IRAM_ALIAS_SIZE);
217558df83dSJean-Christophe Dubois     memory_region_add_subregion(get_system_memory(), FSL_IMX31_IRAM_ALIAS_ADDR,
218558df83dSJean-Christophe Dubois                                 &s->iram_alias);
219558df83dSJean-Christophe Dubois }
220558df83dSJean-Christophe Dubois 
fsl_imx31_class_init(ObjectClass * oc,void * data)221558df83dSJean-Christophe Dubois static void fsl_imx31_class_init(ObjectClass *oc, void *data)
222558df83dSJean-Christophe Dubois {
223558df83dSJean-Christophe Dubois     DeviceClass *dc = DEVICE_CLASS(oc);
224558df83dSJean-Christophe Dubois 
225558df83dSJean-Christophe Dubois     dc->realize = fsl_imx31_realize;
226eccfa35eSJean-Christophe Dubois     dc->desc = "i.MX31 SOC";
227e4e05b7bSThomas Huth     /*
228e4e05b7bSThomas Huth      * Reason: uses serial_hds in realize and the kzm board does not
229e4e05b7bSThomas Huth      * support multiple CPUs
230e4e05b7bSThomas Huth      */
231e4e05b7bSThomas Huth     dc->user_creatable = false;
232558df83dSJean-Christophe Dubois }
233558df83dSJean-Christophe Dubois 
234558df83dSJean-Christophe Dubois static const TypeInfo fsl_imx31_type_info = {
235558df83dSJean-Christophe Dubois     .name = TYPE_FSL_IMX31,
236558df83dSJean-Christophe Dubois     .parent = TYPE_DEVICE,
237558df83dSJean-Christophe Dubois     .instance_size = sizeof(FslIMX31State),
238558df83dSJean-Christophe Dubois     .instance_init = fsl_imx31_init,
239558df83dSJean-Christophe Dubois     .class_init = fsl_imx31_class_init,
240558df83dSJean-Christophe Dubois };
241558df83dSJean-Christophe Dubois 
fsl_imx31_register_types(void)242558df83dSJean-Christophe Dubois static void fsl_imx31_register_types(void)
243558df83dSJean-Christophe Dubois {
244558df83dSJean-Christophe Dubois     type_register_static(&fsl_imx31_type_info);
245558df83dSJean-Christophe Dubois }
246558df83dSJean-Christophe Dubois 
247558df83dSJean-Christophe Dubois type_init(fsl_imx31_register_types)
248