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