xref: /qemu/hw/arm/allwinner-a10.c (revision 9fc7fc4d3909817555ce0af6bcb69dff1606140d)
19158fa54Sliguang /*
29158fa54Sliguang  * Allwinner A10 SoC emulation
39158fa54Sliguang  *
49158fa54Sliguang  * Copyright (C) 2013 Li Guang
59158fa54Sliguang  * Written by Li Guang <lig.fnst@cn.fujitsu.com>
69158fa54Sliguang  *
79158fa54Sliguang  * This program is free software; you can redistribute it and/or modify it
89158fa54Sliguang  * under the terms of the GNU General Public License as published by the
99158fa54Sliguang  * Free Software Foundation; either version 2 of the License, or
109158fa54Sliguang  * (at your option) any later version.
119158fa54Sliguang  *
129158fa54Sliguang  * This program is distributed in the hope that it will be useful, but WITHOUT
139158fa54Sliguang  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
149158fa54Sliguang  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
159158fa54Sliguang  * for more details.
169158fa54Sliguang  */
179158fa54Sliguang 
1812b16722SPeter Maydell #include "qemu/osdep.h"
195a720b1eSMarkus Armbruster #include "exec/address-spaces.h"
20da34e65cSMarkus Armbruster #include "qapi/error.h"
210b8fa32fSMarkus Armbruster #include "qemu/module.h"
224771d756SPaolo Bonzini #include "cpu.h"
239158fa54Sliguang #include "hw/sysbus.h"
249158fa54Sliguang #include "hw/arm/allwinner-a10.h"
25ead07aa4SPhilippe Mathieu-Daudé #include "hw/misc/unimp.h"
2646517dd4SMarkus Armbruster #include "sysemu/sysemu.h"
277abc8cabSGuenter Roeck #include "hw/boards.h"
287abc8cabSGuenter Roeck #include "hw/usb/hcd-ohci.h"
299158fa54Sliguang 
3082e48382SNiek Linnenbank #define AW_A10_MMC0_BASE        0x01c0f000
317f0ec989SPhilippe Mathieu-Daudé #define AW_A10_PIC_REG_BASE     0x01c20400
327f0ec989SPhilippe Mathieu-Daudé #define AW_A10_PIT_REG_BASE     0x01c20c00
337f0ec989SPhilippe Mathieu-Daudé #define AW_A10_UART0_REG_BASE   0x01c28000
347f0ec989SPhilippe Mathieu-Daudé #define AW_A10_EMAC_BASE        0x01c0b000
357abc8cabSGuenter Roeck #define AW_A10_EHCI_BASE        0x01c14000
367abc8cabSGuenter Roeck #define AW_A10_OHCI_BASE        0x01c14400
377f0ec989SPhilippe Mathieu-Daudé #define AW_A10_SATA_BASE        0x01c18000
38a9ad9e73SNiek Linnenbank #define AW_A10_RTC_BASE         0x01c20d00
397f0ec989SPhilippe Mathieu-Daudé 
409158fa54Sliguang static void aw_a10_init(Object *obj)
419158fa54Sliguang {
429158fa54Sliguang     AwA10State *s = AW_A10(obj);
439158fa54Sliguang 
44*9fc7fc4dSMarkus Armbruster     object_initialize_child(obj, "cpu", &s->cpu,
45*9fc7fc4dSMarkus Armbruster                             ARM_CPU_TYPE_NAME("cortex-a8"));
469158fa54Sliguang 
47cf3fccfaSThomas Huth     sysbus_init_child_obj(obj, "intc", &s->intc, sizeof(s->intc),
48cf3fccfaSThomas Huth                           TYPE_AW_A10_PIC);
499158fa54Sliguang 
50cf3fccfaSThomas Huth     sysbus_init_child_obj(obj, "timer", &s->timer, sizeof(s->timer),
51cf3fccfaSThomas Huth                           TYPE_AW_A10_PIT);
52db7dfd4cSBeniamino Galvani 
53cf3fccfaSThomas Huth     sysbus_init_child_obj(obj, "emac", &s->emac, sizeof(s->emac), TYPE_AW_EMAC);
54dca62576SPeter Crosthwaite 
55cf3fccfaSThomas Huth     sysbus_init_child_obj(obj, "sata", &s->sata, sizeof(s->sata),
56cf3fccfaSThomas Huth                           TYPE_ALLWINNER_AHCI);
577abc8cabSGuenter Roeck 
587abc8cabSGuenter Roeck     if (machine_usb(current_machine)) {
597abc8cabSGuenter Roeck         int i;
607abc8cabSGuenter Roeck 
617abc8cabSGuenter Roeck         for (i = 0; i < AW_A10_NUM_USB; i++) {
627abc8cabSGuenter Roeck             sysbus_init_child_obj(obj, "ehci[*]", OBJECT(&s->ehci[i]),
637abc8cabSGuenter Roeck                                   sizeof(s->ehci[i]), TYPE_PLATFORM_EHCI);
647abc8cabSGuenter Roeck             sysbus_init_child_obj(obj, "ohci[*]", OBJECT(&s->ohci[i]),
657abc8cabSGuenter Roeck                                   sizeof(s->ohci[i]), TYPE_SYSBUS_OHCI);
667abc8cabSGuenter Roeck         }
677abc8cabSGuenter Roeck     }
6882e48382SNiek Linnenbank 
6982e48382SNiek Linnenbank     sysbus_init_child_obj(obj, "mmc0", &s->mmc0, sizeof(s->mmc0),
7082e48382SNiek Linnenbank                           TYPE_AW_SDHOST_SUN4I);
71a9ad9e73SNiek Linnenbank 
72a9ad9e73SNiek Linnenbank     sysbus_init_child_obj(obj, "rtc", &s->rtc, sizeof(s->rtc),
73a9ad9e73SNiek Linnenbank                           TYPE_AW_RTC_SUN4I);
749158fa54Sliguang }
759158fa54Sliguang 
769158fa54Sliguang static void aw_a10_realize(DeviceState *dev, Error **errp)
779158fa54Sliguang {
789158fa54Sliguang     AwA10State *s = AW_A10(dev);
799158fa54Sliguang     SysBusDevice *sysbusdev;
809158fa54Sliguang     Error *err = NULL;
819158fa54Sliguang 
829158fa54Sliguang     object_property_set_bool(OBJECT(&s->cpu), true, "realized", &err);
839158fa54Sliguang     if (err != NULL) {
849158fa54Sliguang         error_propagate(errp, err);
859158fa54Sliguang         return;
869158fa54Sliguang     }
879158fa54Sliguang 
889158fa54Sliguang     object_property_set_bool(OBJECT(&s->intc), true, "realized", &err);
899158fa54Sliguang     if (err != NULL) {
909158fa54Sliguang         error_propagate(errp, err);
919158fa54Sliguang         return;
929158fa54Sliguang     }
939158fa54Sliguang     sysbusdev = SYS_BUS_DEVICE(&s->intc);
949158fa54Sliguang     sysbus_mmio_map(sysbusdev, 0, AW_A10_PIC_REG_BASE);
95af4ba4edSPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 0,
96af4ba4edSPhilippe Mathieu-Daudé                        qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_IRQ));
97af4ba4edSPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 1,
98af4ba4edSPhilippe Mathieu-Daudé                        qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_FIQ));
99f8a865d3SPhilippe Mathieu-Daudé     qdev_pass_gpios(DEVICE(&s->intc), dev, NULL);
1009158fa54Sliguang 
1019158fa54Sliguang     object_property_set_bool(OBJECT(&s->timer), true, "realized", &err);
1029158fa54Sliguang     if (err != NULL) {
1039158fa54Sliguang         error_propagate(errp, err);
1049158fa54Sliguang         return;
1059158fa54Sliguang     }
1069158fa54Sliguang     sysbusdev = SYS_BUS_DEVICE(&s->timer);
1079158fa54Sliguang     sysbus_mmio_map(sysbusdev, 0, AW_A10_PIT_REG_BASE);
108f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 0, qdev_get_gpio_in(dev, 22));
109f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 1, qdev_get_gpio_in(dev, 23));
110f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 2, qdev_get_gpio_in(dev, 24));
111f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 3, qdev_get_gpio_in(dev, 25));
112f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 4, qdev_get_gpio_in(dev, 67));
113f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 5, qdev_get_gpio_in(dev, 68));
1149158fa54Sliguang 
115ead07aa4SPhilippe Mathieu-Daudé     memory_region_init_ram(&s->sram_a, OBJECT(dev), "sram A", 48 * KiB,
116ead07aa4SPhilippe Mathieu-Daudé                            &error_fatal);
117ead07aa4SPhilippe Mathieu-Daudé     memory_region_add_subregion(get_system_memory(), 0x00000000, &s->sram_a);
118ead07aa4SPhilippe Mathieu-Daudé     create_unimplemented_device("a10-sram-ctrl", 0x01c00000, 4 * KiB);
119ead07aa4SPhilippe Mathieu-Daudé 
1208aabc543SThomas Huth     /* FIXME use qdev NIC properties instead of nd_table[] */
1218aabc543SThomas Huth     if (nd_table[0].used) {
1228aabc543SThomas Huth         qemu_check_nic_model(&nd_table[0], TYPE_AW_EMAC);
1238aabc543SThomas Huth         qdev_set_nic_properties(DEVICE(&s->emac), &nd_table[0]);
1248aabc543SThomas Huth     }
125db7dfd4cSBeniamino Galvani     object_property_set_bool(OBJECT(&s->emac), true, "realized", &err);
126db7dfd4cSBeniamino Galvani     if (err != NULL) {
127db7dfd4cSBeniamino Galvani         error_propagate(errp, err);
128db7dfd4cSBeniamino Galvani         return;
129db7dfd4cSBeniamino Galvani     }
130db7dfd4cSBeniamino Galvani     sysbusdev = SYS_BUS_DEVICE(&s->emac);
131db7dfd4cSBeniamino Galvani     sysbus_mmio_map(sysbusdev, 0, AW_A10_EMAC_BASE);
132f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 0, qdev_get_gpio_in(dev, 55));
133db7dfd4cSBeniamino Galvani 
134dca62576SPeter Crosthwaite     object_property_set_bool(OBJECT(&s->sata), true, "realized", &err);
135dca62576SPeter Crosthwaite     if (err) {
136dca62576SPeter Crosthwaite         error_propagate(errp, err);
137dca62576SPeter Crosthwaite         return;
138dca62576SPeter Crosthwaite     }
139dca62576SPeter Crosthwaite     sysbus_mmio_map(SYS_BUS_DEVICE(&s->sata), 0, AW_A10_SATA_BASE);
140f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(SYS_BUS_DEVICE(&s->sata), 0, qdev_get_gpio_in(dev, 56));
141dca62576SPeter Crosthwaite 
1429bca0edbSPeter Maydell     /* FIXME use a qdev chardev prop instead of serial_hd() */
143f8a865d3SPhilippe Mathieu-Daudé     serial_mm_init(get_system_memory(), AW_A10_UART0_REG_BASE, 2,
144f8a865d3SPhilippe Mathieu-Daudé                    qdev_get_gpio_in(dev, 1),
1459bca0edbSPeter Maydell                    115200, serial_hd(0), DEVICE_NATIVE_ENDIAN);
1467abc8cabSGuenter Roeck 
1477abc8cabSGuenter Roeck     if (machine_usb(current_machine)) {
1487abc8cabSGuenter Roeck         int i;
1497abc8cabSGuenter Roeck 
1507abc8cabSGuenter Roeck         for (i = 0; i < AW_A10_NUM_USB; i++) {
1517abc8cabSGuenter Roeck             char bus[16];
1527abc8cabSGuenter Roeck 
1537abc8cabSGuenter Roeck             sprintf(bus, "usb-bus.%d", i);
1547abc8cabSGuenter Roeck 
1557abc8cabSGuenter Roeck             object_property_set_bool(OBJECT(&s->ehci[i]), true,
1567abc8cabSGuenter Roeck                                      "companion-enable", &error_fatal);
1577abc8cabSGuenter Roeck             object_property_set_bool(OBJECT(&s->ehci[i]), true, "realized",
1587abc8cabSGuenter Roeck                                      &error_fatal);
1597abc8cabSGuenter Roeck             sysbus_mmio_map(SYS_BUS_DEVICE(&s->ehci[i]), 0,
1607abc8cabSGuenter Roeck                             AW_A10_EHCI_BASE + i * 0x8000);
1617abc8cabSGuenter Roeck             sysbus_connect_irq(SYS_BUS_DEVICE(&s->ehci[i]), 0,
1627abc8cabSGuenter Roeck                                qdev_get_gpio_in(dev, 39 + i));
1637abc8cabSGuenter Roeck 
1647abc8cabSGuenter Roeck             object_property_set_str(OBJECT(&s->ohci[i]), bus, "masterbus",
1657abc8cabSGuenter Roeck                                     &error_fatal);
1667abc8cabSGuenter Roeck             object_property_set_bool(OBJECT(&s->ohci[i]), true, "realized",
1677abc8cabSGuenter Roeck                                      &error_fatal);
1687abc8cabSGuenter Roeck             sysbus_mmio_map(SYS_BUS_DEVICE(&s->ohci[i]), 0,
1697abc8cabSGuenter Roeck                             AW_A10_OHCI_BASE + i * 0x8000);
1707abc8cabSGuenter Roeck             sysbus_connect_irq(SYS_BUS_DEVICE(&s->ohci[i]), 0,
1717abc8cabSGuenter Roeck                                qdev_get_gpio_in(dev, 64 + i));
1727abc8cabSGuenter Roeck         }
1737abc8cabSGuenter Roeck     }
17482e48382SNiek Linnenbank 
17582e48382SNiek Linnenbank     /* SD/MMC */
17682e48382SNiek Linnenbank     qdev_init_nofail(DEVICE(&s->mmc0));
17782e48382SNiek Linnenbank     sysbus_mmio_map(SYS_BUS_DEVICE(&s->mmc0), 0, AW_A10_MMC0_BASE);
17882e48382SNiek Linnenbank     sysbus_connect_irq(SYS_BUS_DEVICE(&s->mmc0), 0, qdev_get_gpio_in(dev, 32));
17982e48382SNiek Linnenbank     object_property_add_alias(OBJECT(s), "sd-bus", OBJECT(&s->mmc0),
180d2623129SMarkus Armbruster                               "sd-bus");
181a9ad9e73SNiek Linnenbank 
182a9ad9e73SNiek Linnenbank     /* RTC */
183a9ad9e73SNiek Linnenbank     qdev_init_nofail(DEVICE(&s->rtc));
184a9ad9e73SNiek Linnenbank     sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s->rtc), 0, AW_A10_RTC_BASE, 10);
1859158fa54Sliguang }
1869158fa54Sliguang 
1879158fa54Sliguang static void aw_a10_class_init(ObjectClass *oc, void *data)
1889158fa54Sliguang {
1899158fa54Sliguang     DeviceClass *dc = DEVICE_CLASS(oc);
1909158fa54Sliguang 
1919158fa54Sliguang     dc->realize = aw_a10_realize;
1928aabc543SThomas Huth     /* Reason: Uses serial_hds and nd_table in realize function */
193dc89a180SThomas Huth     dc->user_creatable = false;
1949158fa54Sliguang }
1959158fa54Sliguang 
1969158fa54Sliguang static const TypeInfo aw_a10_type_info = {
1979158fa54Sliguang     .name = TYPE_AW_A10,
1989158fa54Sliguang     .parent = TYPE_DEVICE,
1999158fa54Sliguang     .instance_size = sizeof(AwA10State),
2009158fa54Sliguang     .instance_init = aw_a10_init,
2019158fa54Sliguang     .class_init = aw_a10_class_init,
2029158fa54Sliguang };
2039158fa54Sliguang 
2049158fa54Sliguang static void aw_a10_register_types(void)
2059158fa54Sliguang {
2069158fa54Sliguang     type_register_static(&aw_a10_type_info);
2079158fa54Sliguang }
2089158fa54Sliguang 
2099158fa54Sliguang type_init(aw_a10_register_types)
210