xref: /openbmc/qemu/hw/arm/fsl-imx6.c (revision efb91426)
1 /*
2  * Copyright (c) 2015 Jean-Christophe Dubois <jcd@tribudubois.net>
3  *
4  * i.MX6 SOC emulation.
5  *
6  * Based on hw/arm/fsl-imx31.c
7  *
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.
12  *
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.
17  *
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/>.
20  */
21 
22 #include "qemu/osdep.h"
23 #include "qapi/error.h"
24 #include "hw/arm/fsl-imx6.h"
25 #include "hw/usb/imx-usb-phy.h"
26 #include "hw/boards.h"
27 #include "hw/qdev-properties.h"
28 #include "sysemu/sysemu.h"
29 #include "chardev/char.h"
30 #include "qemu/error-report.h"
31 #include "qemu/module.h"
32 
33 #define IMX6_ESDHC_CAPABILITIES     0x057834b4
34 
35 #define NAME_SIZE 20
36 
37 static void fsl_imx6_init(Object *obj)
38 {
39     MachineState *ms = MACHINE(qdev_get_machine());
40     FslIMX6State *s = FSL_IMX6(obj);
41     char name[NAME_SIZE];
42     int i;
43 
44     for (i = 0; i < MIN(ms->smp.cpus, FSL_IMX6_NUM_CPUS); i++) {
45         snprintf(name, NAME_SIZE, "cpu%d", i);
46         object_initialize_child(obj, name, &s->cpu[i],
47                                 ARM_CPU_TYPE_NAME("cortex-a9"));
48     }
49 
50     object_initialize_child(obj, "a9mpcore", &s->a9mpcore, TYPE_A9MPCORE_PRIV);
51 
52     object_initialize_child(obj, "ccm", &s->ccm, TYPE_IMX6_CCM);
53 
54     object_initialize_child(obj, "src", &s->src, TYPE_IMX6_SRC);
55 
56     object_initialize_child(obj, "snvs", &s->snvs, TYPE_IMX7_SNVS);
57 
58     for (i = 0; i < FSL_IMX6_NUM_UARTS; i++) {
59         snprintf(name, NAME_SIZE, "uart%d", i + 1);
60         object_initialize_child(obj, name, &s->uart[i], TYPE_IMX_SERIAL);
61     }
62 
63     object_initialize_child(obj, "gpt", &s->gpt, TYPE_IMX6_GPT);
64 
65     for (i = 0; i < FSL_IMX6_NUM_EPITS; i++) {
66         snprintf(name, NAME_SIZE, "epit%d", i + 1);
67         object_initialize_child(obj, name, &s->epit[i], TYPE_IMX_EPIT);
68     }
69 
70     for (i = 0; i < FSL_IMX6_NUM_I2CS; i++) {
71         snprintf(name, NAME_SIZE, "i2c%d", i + 1);
72         object_initialize_child(obj, name, &s->i2c[i], TYPE_IMX_I2C);
73     }
74 
75     for (i = 0; i < FSL_IMX6_NUM_GPIOS; i++) {
76         snprintf(name, NAME_SIZE, "gpio%d", i + 1);
77         object_initialize_child(obj, name, &s->gpio[i], TYPE_IMX_GPIO);
78     }
79 
80     for (i = 0; i < FSL_IMX6_NUM_ESDHCS; i++) {
81         snprintf(name, NAME_SIZE, "sdhc%d", i + 1);
82         object_initialize_child(obj, name, &s->esdhc[i], TYPE_IMX_USDHC);
83     }
84 
85     for (i = 0; i < FSL_IMX6_NUM_USB_PHYS; i++) {
86         snprintf(name, NAME_SIZE, "usbphy%d", i);
87         object_initialize_child(obj, name, &s->usbphy[i], TYPE_IMX_USBPHY);
88     }
89     for (i = 0; i < FSL_IMX6_NUM_USBS; i++) {
90         snprintf(name, NAME_SIZE, "usb%d", i);
91         object_initialize_child(obj, name, &s->usb[i], TYPE_CHIPIDEA);
92     }
93 
94     for (i = 0; i < FSL_IMX6_NUM_ECSPIS; i++) {
95         snprintf(name, NAME_SIZE, "spi%d", i + 1);
96         object_initialize_child(obj, name, &s->spi[i], TYPE_IMX_SPI);
97     }
98     for (i = 0; i < FSL_IMX6_NUM_WDTS; i++) {
99         snprintf(name, NAME_SIZE, "wdt%d", i);
100         object_initialize_child(obj, name, &s->wdt[i], TYPE_IMX2_WDT);
101     }
102 
103 
104     object_initialize_child(obj, "eth", &s->eth, TYPE_IMX_ENET);
105 }
106 
107 static void fsl_imx6_realize(DeviceState *dev, Error **errp)
108 {
109     MachineState *ms = MACHINE(qdev_get_machine());
110     FslIMX6State *s = FSL_IMX6(dev);
111     uint16_t i;
112     unsigned int smp_cpus = ms->smp.cpus;
113 
114     if (smp_cpus > FSL_IMX6_NUM_CPUS) {
115         error_setg(errp, "%s: Only %d CPUs are supported (%d requested)",
116                    TYPE_FSL_IMX6, FSL_IMX6_NUM_CPUS, smp_cpus);
117         return;
118     }
119 
120     for (i = 0; i < smp_cpus; i++) {
121 
122         /* On uniprocessor, the CBAR is set to 0 */
123         if (smp_cpus > 1) {
124             object_property_set_int(OBJECT(&s->cpu[i]), "reset-cbar",
125                                     FSL_IMX6_A9MPCORE_ADDR, &error_abort);
126         }
127 
128         /* All CPU but CPU 0 start in power off mode */
129         if (i) {
130             object_property_set_bool(OBJECT(&s->cpu[i]), "start-powered-off",
131                                      true, &error_abort);
132         }
133 
134         if (!qdev_realize(DEVICE(&s->cpu[i]), NULL, errp)) {
135             return;
136         }
137     }
138 
139     object_property_set_int(OBJECT(&s->a9mpcore), "num-cpu", smp_cpus,
140                             &error_abort);
141 
142     object_property_set_int(OBJECT(&s->a9mpcore), "num-irq",
143                             FSL_IMX6_MAX_IRQ + GIC_INTERNAL, &error_abort);
144 
145     if (!sysbus_realize(SYS_BUS_DEVICE(&s->a9mpcore), errp)) {
146         return;
147     }
148     sysbus_mmio_map(SYS_BUS_DEVICE(&s->a9mpcore), 0, FSL_IMX6_A9MPCORE_ADDR);
149 
150     for (i = 0; i < smp_cpus; i++) {
151         sysbus_connect_irq(SYS_BUS_DEVICE(&s->a9mpcore), i,
152                            qdev_get_gpio_in(DEVICE(&s->cpu[i]), ARM_CPU_IRQ));
153         sysbus_connect_irq(SYS_BUS_DEVICE(&s->a9mpcore), i + smp_cpus,
154                            qdev_get_gpio_in(DEVICE(&s->cpu[i]), ARM_CPU_FIQ));
155     }
156 
157     if (!sysbus_realize(SYS_BUS_DEVICE(&s->ccm), errp)) {
158         return;
159     }
160     sysbus_mmio_map(SYS_BUS_DEVICE(&s->ccm), 0, FSL_IMX6_CCM_ADDR);
161 
162     if (!sysbus_realize(SYS_BUS_DEVICE(&s->src), errp)) {
163         return;
164     }
165     sysbus_mmio_map(SYS_BUS_DEVICE(&s->src), 0, FSL_IMX6_SRC_ADDR);
166 
167     /* Initialize all UARTs */
168     for (i = 0; i < FSL_IMX6_NUM_UARTS; i++) {
169         static const struct {
170             hwaddr addr;
171             unsigned int irq;
172         } serial_table[FSL_IMX6_NUM_UARTS] = {
173             { FSL_IMX6_UART1_ADDR, FSL_IMX6_UART1_IRQ },
174             { FSL_IMX6_UART2_ADDR, FSL_IMX6_UART2_IRQ },
175             { FSL_IMX6_UART3_ADDR, FSL_IMX6_UART3_IRQ },
176             { FSL_IMX6_UART4_ADDR, FSL_IMX6_UART4_IRQ },
177             { FSL_IMX6_UART5_ADDR, FSL_IMX6_UART5_IRQ },
178         };
179 
180         qdev_prop_set_chr(DEVICE(&s->uart[i]), "chardev", serial_hd(i));
181 
182         if (!sysbus_realize(SYS_BUS_DEVICE(&s->uart[i]), errp)) {
183             return;
184         }
185 
186         sysbus_mmio_map(SYS_BUS_DEVICE(&s->uart[i]), 0, serial_table[i].addr);
187         sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart[i]), 0,
188                            qdev_get_gpio_in(DEVICE(&s->a9mpcore),
189                                             serial_table[i].irq));
190     }
191 
192     s->gpt.ccm = IMX_CCM(&s->ccm);
193 
194     if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpt), errp)) {
195         return;
196     }
197 
198     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpt), 0, FSL_IMX6_GPT_ADDR);
199     sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpt), 0,
200                        qdev_get_gpio_in(DEVICE(&s->a9mpcore),
201                                         FSL_IMX6_GPT_IRQ));
202 
203     /* Initialize all EPIT timers */
204     for (i = 0; i < FSL_IMX6_NUM_EPITS; i++) {
205         static const struct {
206             hwaddr addr;
207             unsigned int irq;
208         } epit_table[FSL_IMX6_NUM_EPITS] = {
209             { FSL_IMX6_EPIT1_ADDR, FSL_IMX6_EPIT1_IRQ },
210             { FSL_IMX6_EPIT2_ADDR, FSL_IMX6_EPIT2_IRQ },
211         };
212 
213         s->epit[i].ccm = IMX_CCM(&s->ccm);
214 
215         if (!sysbus_realize(SYS_BUS_DEVICE(&s->epit[i]), errp)) {
216             return;
217         }
218 
219         sysbus_mmio_map(SYS_BUS_DEVICE(&s->epit[i]), 0, epit_table[i].addr);
220         sysbus_connect_irq(SYS_BUS_DEVICE(&s->epit[i]), 0,
221                            qdev_get_gpio_in(DEVICE(&s->a9mpcore),
222                                             epit_table[i].irq));
223     }
224 
225     /* Initialize all I2C */
226     for (i = 0; i < FSL_IMX6_NUM_I2CS; i++) {
227         static const struct {
228             hwaddr addr;
229             unsigned int irq;
230         } i2c_table[FSL_IMX6_NUM_I2CS] = {
231             { FSL_IMX6_I2C1_ADDR, FSL_IMX6_I2C1_IRQ },
232             { FSL_IMX6_I2C2_ADDR, FSL_IMX6_I2C2_IRQ },
233             { FSL_IMX6_I2C3_ADDR, FSL_IMX6_I2C3_IRQ }
234         };
235 
236         if (!sysbus_realize(SYS_BUS_DEVICE(&s->i2c[i]), errp)) {
237             return;
238         }
239 
240         sysbus_mmio_map(SYS_BUS_DEVICE(&s->i2c[i]), 0, i2c_table[i].addr);
241         sysbus_connect_irq(SYS_BUS_DEVICE(&s->i2c[i]), 0,
242                            qdev_get_gpio_in(DEVICE(&s->a9mpcore),
243                                             i2c_table[i].irq));
244     }
245 
246     /* Initialize all GPIOs */
247     for (i = 0; i < FSL_IMX6_NUM_GPIOS; i++) {
248         static const struct {
249             hwaddr addr;
250             unsigned int irq_low;
251             unsigned int irq_high;
252         } gpio_table[FSL_IMX6_NUM_GPIOS] = {
253             {
254                 FSL_IMX6_GPIO1_ADDR,
255                 FSL_IMX6_GPIO1_LOW_IRQ,
256                 FSL_IMX6_GPIO1_HIGH_IRQ
257             },
258             {
259                 FSL_IMX6_GPIO2_ADDR,
260                 FSL_IMX6_GPIO2_LOW_IRQ,
261                 FSL_IMX6_GPIO2_HIGH_IRQ
262             },
263             {
264                 FSL_IMX6_GPIO3_ADDR,
265                 FSL_IMX6_GPIO3_LOW_IRQ,
266                 FSL_IMX6_GPIO3_HIGH_IRQ
267             },
268             {
269                 FSL_IMX6_GPIO4_ADDR,
270                 FSL_IMX6_GPIO4_LOW_IRQ,
271                 FSL_IMX6_GPIO4_HIGH_IRQ
272             },
273             {
274                 FSL_IMX6_GPIO5_ADDR,
275                 FSL_IMX6_GPIO5_LOW_IRQ,
276                 FSL_IMX6_GPIO5_HIGH_IRQ
277             },
278             {
279                 FSL_IMX6_GPIO6_ADDR,
280                 FSL_IMX6_GPIO6_LOW_IRQ,
281                 FSL_IMX6_GPIO6_HIGH_IRQ
282             },
283             {
284                 FSL_IMX6_GPIO7_ADDR,
285                 FSL_IMX6_GPIO7_LOW_IRQ,
286                 FSL_IMX6_GPIO7_HIGH_IRQ
287             },
288         };
289 
290         object_property_set_bool(OBJECT(&s->gpio[i]), "has-edge-sel", true,
291                                  &error_abort);
292         object_property_set_bool(OBJECT(&s->gpio[i]), "has-upper-pin-irq",
293                                  true, &error_abort);
294         if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio[i]), errp)) {
295             return;
296         }
297 
298         sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio[i]), 0, gpio_table[i].addr);
299         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio[i]), 0,
300                            qdev_get_gpio_in(DEVICE(&s->a9mpcore),
301                                             gpio_table[i].irq_low));
302         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio[i]), 1,
303                            qdev_get_gpio_in(DEVICE(&s->a9mpcore),
304                                             gpio_table[i].irq_high));
305     }
306 
307     /* Initialize all SDHC */
308     for (i = 0; i < FSL_IMX6_NUM_ESDHCS; i++) {
309         static const struct {
310             hwaddr addr;
311             unsigned int irq;
312         } esdhc_table[FSL_IMX6_NUM_ESDHCS] = {
313             { FSL_IMX6_uSDHC1_ADDR, FSL_IMX6_uSDHC1_IRQ },
314             { FSL_IMX6_uSDHC2_ADDR, FSL_IMX6_uSDHC2_IRQ },
315             { FSL_IMX6_uSDHC3_ADDR, FSL_IMX6_uSDHC3_IRQ },
316             { FSL_IMX6_uSDHC4_ADDR, FSL_IMX6_uSDHC4_IRQ },
317         };
318 
319         /* UHS-I SDIO3.0 SDR104 1.8V ADMA */
320         object_property_set_uint(OBJECT(&s->esdhc[i]), "sd-spec-version", 3,
321                                  &error_abort);
322         object_property_set_uint(OBJECT(&s->esdhc[i]), "capareg",
323                                  IMX6_ESDHC_CAPABILITIES, &error_abort);
324         object_property_set_uint(OBJECT(&s->esdhc[i]), "vendor",
325                                  SDHCI_VENDOR_IMX, &error_abort);
326         if (!sysbus_realize(SYS_BUS_DEVICE(&s->esdhc[i]), errp)) {
327             return;
328         }
329         sysbus_mmio_map(SYS_BUS_DEVICE(&s->esdhc[i]), 0, esdhc_table[i].addr);
330         sysbus_connect_irq(SYS_BUS_DEVICE(&s->esdhc[i]), 0,
331                            qdev_get_gpio_in(DEVICE(&s->a9mpcore),
332                                             esdhc_table[i].irq));
333     }
334 
335     /* USB */
336     for (i = 0; i < FSL_IMX6_NUM_USB_PHYS; i++) {
337         sysbus_realize(SYS_BUS_DEVICE(&s->usbphy[i]), &error_abort);
338         sysbus_mmio_map(SYS_BUS_DEVICE(&s->usbphy[i]), 0,
339                         FSL_IMX6_USBPHY1_ADDR + i * 0x1000);
340     }
341     for (i = 0; i < FSL_IMX6_NUM_USBS; i++) {
342         static const int FSL_IMX6_USBn_IRQ[] = {
343             FSL_IMX6_USB_OTG_IRQ,
344             FSL_IMX6_USB_HOST1_IRQ,
345             FSL_IMX6_USB_HOST2_IRQ,
346             FSL_IMX6_USB_HOST3_IRQ,
347         };
348 
349         sysbus_realize(SYS_BUS_DEVICE(&s->usb[i]), &error_abort);
350         sysbus_mmio_map(SYS_BUS_DEVICE(&s->usb[i]), 0,
351                         FSL_IMX6_USBOH3_USB_ADDR + i * 0x200);
352         sysbus_connect_irq(SYS_BUS_DEVICE(&s->usb[i]), 0,
353                            qdev_get_gpio_in(DEVICE(&s->a9mpcore),
354                                             FSL_IMX6_USBn_IRQ[i]));
355     }
356 
357     /* Initialize all ECSPI */
358     for (i = 0; i < FSL_IMX6_NUM_ECSPIS; i++) {
359         static const struct {
360             hwaddr addr;
361             unsigned int irq;
362         } spi_table[FSL_IMX6_NUM_ECSPIS] = {
363             { FSL_IMX6_eCSPI1_ADDR, FSL_IMX6_ECSPI1_IRQ },
364             { FSL_IMX6_eCSPI2_ADDR, FSL_IMX6_ECSPI2_IRQ },
365             { FSL_IMX6_eCSPI3_ADDR, FSL_IMX6_ECSPI3_IRQ },
366             { FSL_IMX6_eCSPI4_ADDR, FSL_IMX6_ECSPI4_IRQ },
367             { FSL_IMX6_eCSPI5_ADDR, FSL_IMX6_ECSPI5_IRQ },
368         };
369 
370         /* Initialize the SPI */
371         if (!sysbus_realize(SYS_BUS_DEVICE(&s->spi[i]), errp)) {
372             return;
373         }
374 
375         sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi[i]), 0, spi_table[i].addr);
376         sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi[i]), 0,
377                            qdev_get_gpio_in(DEVICE(&s->a9mpcore),
378                                             spi_table[i].irq));
379     }
380 
381     object_property_set_uint(OBJECT(&s->eth), "phy-num", s->phy_num,
382                              &error_abort);
383     qdev_set_nic_properties(DEVICE(&s->eth), &nd_table[0]);
384     if (!sysbus_realize(SYS_BUS_DEVICE(&s->eth), errp)) {
385         return;
386     }
387     sysbus_mmio_map(SYS_BUS_DEVICE(&s->eth), 0, FSL_IMX6_ENET_ADDR);
388     sysbus_connect_irq(SYS_BUS_DEVICE(&s->eth), 0,
389                        qdev_get_gpio_in(DEVICE(&s->a9mpcore),
390                                         FSL_IMX6_ENET_MAC_IRQ));
391     sysbus_connect_irq(SYS_BUS_DEVICE(&s->eth), 1,
392                        qdev_get_gpio_in(DEVICE(&s->a9mpcore),
393                                         FSL_IMX6_ENET_MAC_1588_IRQ));
394 
395     /*
396      * SNVS
397      */
398     sysbus_realize(SYS_BUS_DEVICE(&s->snvs), &error_abort);
399     sysbus_mmio_map(SYS_BUS_DEVICE(&s->snvs), 0, FSL_IMX6_SNVSHP_ADDR);
400 
401     /*
402      * Watchdog
403      */
404     for (i = 0; i < FSL_IMX6_NUM_WDTS; i++) {
405         static const hwaddr FSL_IMX6_WDOGn_ADDR[FSL_IMX6_NUM_WDTS] = {
406             FSL_IMX6_WDOG1_ADDR,
407             FSL_IMX6_WDOG2_ADDR,
408         };
409         static const int FSL_IMX6_WDOGn_IRQ[FSL_IMX6_NUM_WDTS] = {
410             FSL_IMX6_WDOG1_IRQ,
411             FSL_IMX6_WDOG2_IRQ,
412         };
413 
414         object_property_set_bool(OBJECT(&s->wdt[i]), "pretimeout-support",
415                                  true, &error_abort);
416         sysbus_realize(SYS_BUS_DEVICE(&s->wdt[i]), &error_abort);
417 
418         sysbus_mmio_map(SYS_BUS_DEVICE(&s->wdt[i]), 0, FSL_IMX6_WDOGn_ADDR[i]);
419         sysbus_connect_irq(SYS_BUS_DEVICE(&s->wdt[i]), 0,
420                            qdev_get_gpio_in(DEVICE(&s->a9mpcore),
421                                             FSL_IMX6_WDOGn_IRQ[i]));
422     }
423 
424     /* ROM memory */
425     if (!memory_region_init_rom(&s->rom, OBJECT(dev), "imx6.rom",
426                                 FSL_IMX6_ROM_SIZE, errp)) {
427         return;
428     }
429     memory_region_add_subregion(get_system_memory(), FSL_IMX6_ROM_ADDR,
430                                 &s->rom);
431 
432     /* CAAM memory */
433     if (!memory_region_init_rom(&s->caam, OBJECT(dev), "imx6.caam",
434                                 FSL_IMX6_CAAM_MEM_SIZE, errp)) {
435         return;
436     }
437     memory_region_add_subregion(get_system_memory(), FSL_IMX6_CAAM_MEM_ADDR,
438                                 &s->caam);
439 
440     /* OCRAM memory */
441     if (!memory_region_init_ram(&s->ocram, NULL, "imx6.ocram",
442                                 FSL_IMX6_OCRAM_SIZE, errp)) {
443         return;
444     }
445     memory_region_add_subregion(get_system_memory(), FSL_IMX6_OCRAM_ADDR,
446                                 &s->ocram);
447 
448     /* internal OCRAM (256 KB) is aliased over 1 MB */
449     memory_region_init_alias(&s->ocram_alias, OBJECT(dev), "imx6.ocram_alias",
450                              &s->ocram, 0, FSL_IMX6_OCRAM_ALIAS_SIZE);
451     memory_region_add_subregion(get_system_memory(), FSL_IMX6_OCRAM_ALIAS_ADDR,
452                                 &s->ocram_alias);
453 }
454 
455 static Property fsl_imx6_properties[] = {
456     DEFINE_PROP_UINT32("fec-phy-num", FslIMX6State, phy_num, 0),
457     DEFINE_PROP_END_OF_LIST(),
458 };
459 
460 static void fsl_imx6_class_init(ObjectClass *oc, void *data)
461 {
462     DeviceClass *dc = DEVICE_CLASS(oc);
463 
464     device_class_set_props(dc, fsl_imx6_properties);
465     dc->realize = fsl_imx6_realize;
466     dc->desc = "i.MX6 SOC";
467     /* Reason: Uses serial_hd() in the realize() function */
468     dc->user_creatable = false;
469 }
470 
471 static const TypeInfo fsl_imx6_type_info = {
472     .name = TYPE_FSL_IMX6,
473     .parent = TYPE_DEVICE,
474     .instance_size = sizeof(FslIMX6State),
475     .instance_init = fsl_imx6_init,
476     .class_init = fsl_imx6_class_init,
477 };
478 
479 static void fsl_imx6_register_types(void)
480 {
481     type_register_static(&fsl_imx6_type_info);
482 }
483 
484 type_init(fsl_imx6_register_types)
485