2 * Copyright (c) 2015 Jean-Christophe Dubois <jcd@tribudubois.net>
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 "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"
33 #define IMX6_ESDHC_CAPABILITIES 0x057834b4
37 static void fsl_imx6_init(Object
*obj
)
39 MachineState
*ms
= MACHINE(qdev_get_machine());
40 FslIMX6State
*s
= FSL_IMX6(obj
);
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
], sizeof(s
->cpu
[i
]),
47 ARM_CPU_TYPE_NAME("cortex-a9"),
51 sysbus_init_child_obj(obj
, "a9mpcore", &s
->a9mpcore
, sizeof(s
->a9mpcore
),
54 sysbus_init_child_obj(obj
, "ccm", &s
->ccm
, sizeof(s
->ccm
), TYPE_IMX6_CCM
);
56 sysbus_init_child_obj(obj
, "src", &s
->src
, sizeof(s
->src
), TYPE_IMX6_SRC
);
58 for (i
= 0; i
< FSL_IMX6_NUM_UARTS
; i
++) {
59 snprintf(name
, NAME_SIZE
, "uart%d", i
+ 1);
60 sysbus_init_child_obj(obj
, name
, &s
->uart
[i
], sizeof(s
->uart
[i
]),
64 sysbus_init_child_obj(obj
, "gpt", &s
->gpt
, sizeof(s
->gpt
), TYPE_IMX6_GPT
);
66 for (i
= 0; i
< FSL_IMX6_NUM_EPITS
; i
++) {
67 snprintf(name
, NAME_SIZE
, "epit%d", i
+ 1);
68 sysbus_init_child_obj(obj
, name
, &s
->epit
[i
], sizeof(s
->epit
[i
]),
72 for (i
= 0; i
< FSL_IMX6_NUM_I2CS
; i
++) {
73 snprintf(name
, NAME_SIZE
, "i2c%d", i
+ 1);
74 sysbus_init_child_obj(obj
, name
, &s
->i2c
[i
], sizeof(s
->i2c
[i
]),
78 for (i
= 0; i
< FSL_IMX6_NUM_GPIOS
; i
++) {
79 snprintf(name
, NAME_SIZE
, "gpio%d", i
+ 1);
80 sysbus_init_child_obj(obj
, name
, &s
->gpio
[i
], sizeof(s
->gpio
[i
]),
84 for (i
= 0; i
< FSL_IMX6_NUM_ESDHCS
; i
++) {
85 snprintf(name
, NAME_SIZE
, "sdhc%d", i
+ 1);
86 sysbus_init_child_obj(obj
, name
, &s
->esdhc
[i
], sizeof(s
->esdhc
[i
]),
90 for (i
= 0; i
< FSL_IMX6_NUM_USB_PHYS
; i
++) {
91 snprintf(name
, NAME_SIZE
, "usbphy%d", i
);
92 sysbus_init_child_obj(obj
, name
, &s
->usbphy
[i
], sizeof(s
->usbphy
[i
]),
95 for (i
= 0; i
< FSL_IMX6_NUM_USBS
; i
++) {
96 snprintf(name
, NAME_SIZE
, "usb%d", i
);
97 sysbus_init_child_obj(obj
, name
, &s
->usb
[i
], sizeof(s
->usb
[i
]),
101 for (i
= 0; i
< FSL_IMX6_NUM_ECSPIS
; i
++) {
102 snprintf(name
, NAME_SIZE
, "spi%d", i
+ 1);
103 sysbus_init_child_obj(obj
, name
, &s
->spi
[i
], sizeof(s
->spi
[i
]),
106 for (i
= 0; i
< FSL_IMX6_NUM_WDTS
; i
++) {
107 snprintf(name
, NAME_SIZE
, "wdt%d", i
);
108 sysbus_init_child_obj(obj
, name
, &s
->wdt
[i
], sizeof(s
->wdt
[i
]),
113 sysbus_init_child_obj(obj
, "eth", &s
->eth
, sizeof(s
->eth
), TYPE_IMX_ENET
);
116 static void fsl_imx6_realize(DeviceState
*dev
, Error
**errp
)
118 MachineState
*ms
= MACHINE(qdev_get_machine());
119 FslIMX6State
*s
= FSL_IMX6(dev
);
122 unsigned int smp_cpus
= ms
->smp
.cpus
;
124 if (smp_cpus
> FSL_IMX6_NUM_CPUS
) {
125 error_setg(errp
, "%s: Only %d CPUs are supported (%d requested)",
126 TYPE_FSL_IMX6
, FSL_IMX6_NUM_CPUS
, smp_cpus
);
130 for (i
= 0; i
< smp_cpus
; i
++) {
132 /* On uniprocessor, the CBAR is set to 0 */
134 object_property_set_int(OBJECT(&s
->cpu
[i
]), FSL_IMX6_A9MPCORE_ADDR
,
135 "reset-cbar", &error_abort
);
138 /* All CPU but CPU 0 start in power off mode */
140 object_property_set_bool(OBJECT(&s
->cpu
[i
]), true,
141 "start-powered-off", &error_abort
);
144 object_property_set_bool(OBJECT(&s
->cpu
[i
]), true, "realized", &err
);
146 error_propagate(errp
, err
);
151 object_property_set_int(OBJECT(&s
->a9mpcore
), smp_cpus
, "num-cpu",
154 object_property_set_int(OBJECT(&s
->a9mpcore
),
155 FSL_IMX6_MAX_IRQ
+ GIC_INTERNAL
, "num-irq",
158 object_property_set_bool(OBJECT(&s
->a9mpcore
), true, "realized", &err
);
160 error_propagate(errp
, err
);
163 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->a9mpcore
), 0, FSL_IMX6_A9MPCORE_ADDR
);
165 for (i
= 0; i
< smp_cpus
; i
++) {
166 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->a9mpcore
), i
,
167 qdev_get_gpio_in(DEVICE(&s
->cpu
[i
]), ARM_CPU_IRQ
));
168 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->a9mpcore
), i
+ smp_cpus
,
169 qdev_get_gpio_in(DEVICE(&s
->cpu
[i
]), ARM_CPU_FIQ
));
172 object_property_set_bool(OBJECT(&s
->ccm
), true, "realized", &err
);
174 error_propagate(errp
, err
);
177 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->ccm
), 0, FSL_IMX6_CCM_ADDR
);
179 object_property_set_bool(OBJECT(&s
->src
), true, "realized", &err
);
181 error_propagate(errp
, err
);
184 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->src
), 0, FSL_IMX6_SRC_ADDR
);
186 /* Initialize all UARTs */
187 for (i
= 0; i
< FSL_IMX6_NUM_UARTS
; i
++) {
188 static const struct {
191 } serial_table
[FSL_IMX6_NUM_UARTS
] = {
192 { FSL_IMX6_UART1_ADDR
, FSL_IMX6_UART1_IRQ
},
193 { FSL_IMX6_UART2_ADDR
, FSL_IMX6_UART2_IRQ
},
194 { FSL_IMX6_UART3_ADDR
, FSL_IMX6_UART3_IRQ
},
195 { FSL_IMX6_UART4_ADDR
, FSL_IMX6_UART4_IRQ
},
196 { FSL_IMX6_UART5_ADDR
, FSL_IMX6_UART5_IRQ
},
199 qdev_prop_set_chr(DEVICE(&s
->uart
[i
]), "chardev", serial_hd(i
));
201 object_property_set_bool(OBJECT(&s
->uart
[i
]), true, "realized", &err
);
203 error_propagate(errp
, err
);
207 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->uart
[i
]), 0, serial_table
[i
].addr
);
208 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->uart
[i
]), 0,
209 qdev_get_gpio_in(DEVICE(&s
->a9mpcore
),
210 serial_table
[i
].irq
));
213 s
->gpt
.ccm
= IMX_CCM(&s
->ccm
);
215 object_property_set_bool(OBJECT(&s
->gpt
), true, "realized", &err
);
217 error_propagate(errp
, err
);
221 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->gpt
), 0, FSL_IMX6_GPT_ADDR
);
222 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->gpt
), 0,
223 qdev_get_gpio_in(DEVICE(&s
->a9mpcore
),
226 /* Initialize all EPIT timers */
227 for (i
= 0; i
< FSL_IMX6_NUM_EPITS
; i
++) {
228 static const struct {
231 } epit_table
[FSL_IMX6_NUM_EPITS
] = {
232 { FSL_IMX6_EPIT1_ADDR
, FSL_IMX6_EPIT1_IRQ
},
233 { FSL_IMX6_EPIT2_ADDR
, FSL_IMX6_EPIT2_IRQ
},
236 s
->epit
[i
].ccm
= IMX_CCM(&s
->ccm
);
238 object_property_set_bool(OBJECT(&s
->epit
[i
]), true, "realized", &err
);
240 error_propagate(errp
, err
);
244 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->epit
[i
]), 0, epit_table
[i
].addr
);
245 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->epit
[i
]), 0,
246 qdev_get_gpio_in(DEVICE(&s
->a9mpcore
),
250 /* Initialize all I2C */
251 for (i
= 0; i
< FSL_IMX6_NUM_I2CS
; i
++) {
252 static const struct {
255 } i2c_table
[FSL_IMX6_NUM_I2CS
] = {
256 { FSL_IMX6_I2C1_ADDR
, FSL_IMX6_I2C1_IRQ
},
257 { FSL_IMX6_I2C2_ADDR
, FSL_IMX6_I2C2_IRQ
},
258 { FSL_IMX6_I2C3_ADDR
, FSL_IMX6_I2C3_IRQ
}
261 object_property_set_bool(OBJECT(&s
->i2c
[i
]), true, "realized", &err
);
263 error_propagate(errp
, err
);
267 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->i2c
[i
]), 0, i2c_table
[i
].addr
);
268 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->i2c
[i
]), 0,
269 qdev_get_gpio_in(DEVICE(&s
->a9mpcore
),
273 /* Initialize all GPIOs */
274 for (i
= 0; i
< FSL_IMX6_NUM_GPIOS
; i
++) {
275 static const struct {
277 unsigned int irq_low
;
278 unsigned int irq_high
;
279 } gpio_table
[FSL_IMX6_NUM_GPIOS
] = {
282 FSL_IMX6_GPIO1_LOW_IRQ
,
283 FSL_IMX6_GPIO1_HIGH_IRQ
287 FSL_IMX6_GPIO2_LOW_IRQ
,
288 FSL_IMX6_GPIO2_HIGH_IRQ
292 FSL_IMX6_GPIO3_LOW_IRQ
,
293 FSL_IMX6_GPIO3_HIGH_IRQ
297 FSL_IMX6_GPIO4_LOW_IRQ
,
298 FSL_IMX6_GPIO4_HIGH_IRQ
302 FSL_IMX6_GPIO5_LOW_IRQ
,
303 FSL_IMX6_GPIO5_HIGH_IRQ
307 FSL_IMX6_GPIO6_LOW_IRQ
,
308 FSL_IMX6_GPIO6_HIGH_IRQ
312 FSL_IMX6_GPIO7_LOW_IRQ
,
313 FSL_IMX6_GPIO7_HIGH_IRQ
317 object_property_set_bool(OBJECT(&s
->gpio
[i
]), true, "has-edge-sel",
319 object_property_set_bool(OBJECT(&s
->gpio
[i
]), true, "has-upper-pin-irq",
321 object_property_set_bool(OBJECT(&s
->gpio
[i
]), true, "realized", &err
);
323 error_propagate(errp
, err
);
327 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->gpio
[i
]), 0, gpio_table
[i
].addr
);
328 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->gpio
[i
]), 0,
329 qdev_get_gpio_in(DEVICE(&s
->a9mpcore
),
330 gpio_table
[i
].irq_low
));
331 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->gpio
[i
]), 1,
332 qdev_get_gpio_in(DEVICE(&s
->a9mpcore
),
333 gpio_table
[i
].irq_high
));
336 /* Initialize all SDHC */
337 for (i
= 0; i
< FSL_IMX6_NUM_ESDHCS
; i
++) {
338 static const struct {
341 } esdhc_table
[FSL_IMX6_NUM_ESDHCS
] = {
342 { FSL_IMX6_uSDHC1_ADDR
, FSL_IMX6_uSDHC1_IRQ
},
343 { FSL_IMX6_uSDHC2_ADDR
, FSL_IMX6_uSDHC2_IRQ
},
344 { FSL_IMX6_uSDHC3_ADDR
, FSL_IMX6_uSDHC3_IRQ
},
345 { FSL_IMX6_uSDHC4_ADDR
, FSL_IMX6_uSDHC4_IRQ
},
348 /* UHS-I SDIO3.0 SDR104 1.8V ADMA */
349 object_property_set_uint(OBJECT(&s
->esdhc
[i
]), 3, "sd-spec-version",
351 object_property_set_uint(OBJECT(&s
->esdhc
[i
]), IMX6_ESDHC_CAPABILITIES
,
353 object_property_set_bool(OBJECT(&s
->esdhc
[i
]), true, "realized", &err
);
355 error_propagate(errp
, err
);
358 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->esdhc
[i
]), 0, esdhc_table
[i
].addr
);
359 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->esdhc
[i
]), 0,
360 qdev_get_gpio_in(DEVICE(&s
->a9mpcore
),
361 esdhc_table
[i
].irq
));
365 for (i
= 0; i
< FSL_IMX6_NUM_USB_PHYS
; i
++) {
366 object_property_set_bool(OBJECT(&s
->usbphy
[i
]), true, "realized",
368 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->usbphy
[i
]), 0,
369 FSL_IMX6_USBPHY1_ADDR
+ i
* 0x1000);
371 for (i
= 0; i
< FSL_IMX6_NUM_USBS
; i
++) {
372 static const int FSL_IMX6_USBn_IRQ
[] = {
373 FSL_IMX6_USB_OTG_IRQ
,
374 FSL_IMX6_USB_HOST1_IRQ
,
375 FSL_IMX6_USB_HOST2_IRQ
,
376 FSL_IMX6_USB_HOST3_IRQ
,
379 object_property_set_bool(OBJECT(&s
->usb
[i
]), true, "realized",
381 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->usb
[i
]), 0,
382 FSL_IMX6_USBOH3_USB_ADDR
+ i
* 0x200);
383 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->usb
[i
]), 0,
384 qdev_get_gpio_in(DEVICE(&s
->a9mpcore
),
385 FSL_IMX6_USBn_IRQ
[i
]));
388 /* Initialize all ECSPI */
389 for (i
= 0; i
< FSL_IMX6_NUM_ECSPIS
; i
++) {
390 static const struct {
393 } spi_table
[FSL_IMX6_NUM_ECSPIS
] = {
394 { FSL_IMX6_eCSPI1_ADDR
, FSL_IMX6_ECSPI1_IRQ
},
395 { FSL_IMX6_eCSPI2_ADDR
, FSL_IMX6_ECSPI2_IRQ
},
396 { FSL_IMX6_eCSPI3_ADDR
, FSL_IMX6_ECSPI3_IRQ
},
397 { FSL_IMX6_eCSPI4_ADDR
, FSL_IMX6_ECSPI4_IRQ
},
398 { FSL_IMX6_eCSPI5_ADDR
, FSL_IMX6_ECSPI5_IRQ
},
401 /* Initialize the SPI */
402 object_property_set_bool(OBJECT(&s
->spi
[i
]), true, "realized", &err
);
404 error_propagate(errp
, err
);
408 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->spi
[i
]), 0, spi_table
[i
].addr
);
409 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->spi
[i
]), 0,
410 qdev_get_gpio_in(DEVICE(&s
->a9mpcore
),
414 qdev_set_nic_properties(DEVICE(&s
->eth
), &nd_table
[0]);
415 object_property_set_bool(OBJECT(&s
->eth
), true, "realized", &err
);
417 error_propagate(errp
, err
);
420 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->eth
), 0, FSL_IMX6_ENET_ADDR
);
421 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->eth
), 0,
422 qdev_get_gpio_in(DEVICE(&s
->a9mpcore
),
423 FSL_IMX6_ENET_MAC_IRQ
));
424 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->eth
), 1,
425 qdev_get_gpio_in(DEVICE(&s
->a9mpcore
),
426 FSL_IMX6_ENET_MAC_1588_IRQ
));
431 for (i
= 0; i
< FSL_IMX6_NUM_WDTS
; i
++) {
432 static const hwaddr FSL_IMX6_WDOGn_ADDR
[FSL_IMX6_NUM_WDTS
] = {
437 object_property_set_bool(OBJECT(&s
->wdt
[i
]), true, "realized",
440 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->wdt
[i
]), 0, FSL_IMX6_WDOGn_ADDR
[i
]);
444 memory_region_init_rom(&s
->rom
, OBJECT(dev
), "imx6.rom",
445 FSL_IMX6_ROM_SIZE
, &err
);
447 error_propagate(errp
, err
);
450 memory_region_add_subregion(get_system_memory(), FSL_IMX6_ROM_ADDR
,
454 memory_region_init_rom(&s
->caam
, OBJECT(dev
), "imx6.caam",
455 FSL_IMX6_CAAM_MEM_SIZE
, &err
);
457 error_propagate(errp
, err
);
460 memory_region_add_subregion(get_system_memory(), FSL_IMX6_CAAM_MEM_ADDR
,
464 memory_region_init_ram(&s
->ocram
, NULL
, "imx6.ocram", FSL_IMX6_OCRAM_SIZE
,
467 error_propagate(errp
, err
);
470 memory_region_add_subregion(get_system_memory(), FSL_IMX6_OCRAM_ADDR
,
473 /* internal OCRAM (256 KB) is aliased over 1 MB */
474 memory_region_init_alias(&s
->ocram_alias
, OBJECT(dev
), "imx6.ocram_alias",
475 &s
->ocram
, 0, FSL_IMX6_OCRAM_ALIAS_SIZE
);
476 memory_region_add_subregion(get_system_memory(), FSL_IMX6_OCRAM_ALIAS_ADDR
,
480 static void fsl_imx6_class_init(ObjectClass
*oc
, void *data
)
482 DeviceClass
*dc
= DEVICE_CLASS(oc
);
484 dc
->realize
= fsl_imx6_realize
;
485 dc
->desc
= "i.MX6 SOC";
486 /* Reason: Uses serial_hd() in the realize() function */
487 dc
->user_creatable
= false;
490 static const TypeInfo fsl_imx6_type_info
= {
491 .name
= TYPE_FSL_IMX6
,
492 .parent
= TYPE_DEVICE
,
493 .instance_size
= sizeof(FslIMX6State
),
494 .instance_init
= fsl_imx6_init
,
495 .class_init
= fsl_imx6_class_init
,
498 static void fsl_imx6_register_types(void)
500 type_register_static(&fsl_imx6_type_info
);
503 type_init(fsl_imx6_register_types
)