xref: /openbmc/qemu/hw/arm/allwinner-r40.c (revision e999bea8)
1 /*
2  * Allwinner R40/A40i/T3 System on Chip emulation
3  *
4  * Copyright (C) 2023 qianfan Zhao <qianfanguijin@163.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
22 #include "qemu/error-report.h"
23 #include "qemu/bswap.h"
24 #include "qemu/module.h"
25 #include "qemu/units.h"
26 #include "hw/boards.h"
27 #include "hw/qdev-core.h"
28 #include "hw/sysbus.h"
29 #include "hw/char/serial.h"
30 #include "hw/misc/unimp.h"
31 #include "hw/usb/hcd-ehci.h"
32 #include "hw/loader.h"
33 #include "sysemu/sysemu.h"
34 #include "hw/arm/allwinner-r40.h"
35 #include "hw/misc/allwinner-r40-dramc.h"
36 #include "target/arm/cpu-qom.h"
37 #include "target/arm/gtimer.h"
38 
39 /* Memory map */
40 const hwaddr allwinner_r40_memmap[] = {
41     [AW_R40_DEV_SRAM_A1]    = 0x00000000,
42     [AW_R40_DEV_SRAM_A2]    = 0x00004000,
43     [AW_R40_DEV_SRAM_A3]    = 0x00008000,
44     [AW_R40_DEV_SRAM_A4]    = 0x0000b400,
45     [AW_R40_DEV_SRAMC]      = 0x01c00000,
46     [AW_R40_DEV_EMAC]       = 0x01c0b000,
47     [AW_R40_DEV_MMC0]       = 0x01c0f000,
48     [AW_R40_DEV_MMC1]       = 0x01c10000,
49     [AW_R40_DEV_MMC2]       = 0x01c11000,
50     [AW_R40_DEV_MMC3]       = 0x01c12000,
51     [AW_R40_DEV_AHCI]       = 0x01c18000,
52     [AW_R40_DEV_EHCI1]      = 0x01c19000,
53     [AW_R40_DEV_OHCI1]      = 0x01c19400,
54     [AW_R40_DEV_EHCI2]      = 0x01c1c000,
55     [AW_R40_DEV_OHCI2]      = 0x01c1c400,
56     [AW_R40_DEV_CCU]        = 0x01c20000,
57     [AW_R40_DEV_PIT]        = 0x01c20c00,
58     [AW_R40_DEV_WDT]        = 0x01c20c90,
59     [AW_R40_DEV_UART0]      = 0x01c28000,
60     [AW_R40_DEV_UART1]      = 0x01c28400,
61     [AW_R40_DEV_UART2]      = 0x01c28800,
62     [AW_R40_DEV_UART3]      = 0x01c28c00,
63     [AW_R40_DEV_UART4]      = 0x01c29000,
64     [AW_R40_DEV_UART5]      = 0x01c29400,
65     [AW_R40_DEV_UART6]      = 0x01c29800,
66     [AW_R40_DEV_UART7]      = 0x01c29c00,
67     [AW_R40_DEV_TWI0]       = 0x01c2ac00,
68     [AW_R40_DEV_GMAC]       = 0x01c50000,
69     [AW_R40_DEV_DRAMCOM]    = 0x01c62000,
70     [AW_R40_DEV_DRAMCTL]    = 0x01c63000,
71     [AW_R40_DEV_DRAMPHY]    = 0x01c65000,
72     [AW_R40_DEV_GIC_DIST]   = 0x01c81000,
73     [AW_R40_DEV_GIC_CPU]    = 0x01c82000,
74     [AW_R40_DEV_GIC_HYP]    = 0x01c84000,
75     [AW_R40_DEV_GIC_VCPU]   = 0x01c86000,
76     [AW_R40_DEV_SDRAM]      = 0x40000000
77 };
78 
79 /* List of unimplemented devices */
80 struct AwR40Unimplemented {
81     const char *device_name;
82     hwaddr base;
83     hwaddr size;
84 };
85 
86 static struct AwR40Unimplemented r40_unimplemented[] = {
87     { "d-engine",   0x01000000, 4 * MiB },
88     { "d-inter",    0x01400000, 128 * KiB },
89     { "dma",        0x01c02000, 4 * KiB },
90     { "nfdc",       0x01c03000, 4 * KiB },
91     { "ts",         0x01c04000, 4 * KiB },
92     { "spi0",       0x01c05000, 4 * KiB },
93     { "spi1",       0x01c06000, 4 * KiB },
94     { "cs0",        0x01c09000, 4 * KiB },
95     { "keymem",     0x01c0a000, 4 * KiB },
96     { "usb0-otg",   0x01c13000, 4 * KiB },
97     { "usb0-host",  0x01c14000, 4 * KiB },
98     { "crypto",     0x01c15000, 4 * KiB },
99     { "spi2",       0x01c17000, 4 * KiB },
100     { "usb1-phy",   0x01c19800, 2 * KiB },
101     { "sid",        0x01c1b000, 4 * KiB },
102     { "usb2-phy",   0x01c1c800, 2 * KiB },
103     { "cs1",        0x01c1d000, 4 * KiB },
104     { "spi3",       0x01c1f000, 4 * KiB },
105     { "rtc",        0x01c20400, 1 * KiB },
106     { "pio",        0x01c20800, 1 * KiB },
107     { "owa",        0x01c21000, 1 * KiB },
108     { "ac97",       0x01c21400, 1 * KiB },
109     { "cir0",       0x01c21800, 1 * KiB },
110     { "cir1",       0x01c21c00, 1 * KiB },
111     { "pcm0",       0x01c22000, 1 * KiB },
112     { "pcm1",       0x01c22400, 1 * KiB },
113     { "pcm2",       0x01c22800, 1 * KiB },
114     { "audio",      0x01c22c00, 1 * KiB },
115     { "keypad",     0x01c23000, 1 * KiB },
116     { "pwm",        0x01c23400, 1 * KiB },
117     { "keyadc",     0x01c24400, 1 * KiB },
118     { "ths",        0x01c24c00, 1 * KiB },
119     { "rtp",        0x01c25000, 1 * KiB },
120     { "pmu",        0x01c25400, 1 * KiB },
121     { "cpu-cfg",    0x01c25c00, 1 * KiB },
122     { "uart0",      0x01c28000, 1 * KiB },
123     { "uart1",      0x01c28400, 1 * KiB },
124     { "uart2",      0x01c28800, 1 * KiB },
125     { "uart3",      0x01c28c00, 1 * KiB },
126     { "uart4",      0x01c29000, 1 * KiB },
127     { "uart5",      0x01c29400, 1 * KiB },
128     { "uart6",      0x01c29800, 1 * KiB },
129     { "uart7",      0x01c29c00, 1 * KiB },
130     { "ps20",       0x01c2a000, 1 * KiB },
131     { "ps21",       0x01c2a400, 1 * KiB },
132     { "twi1",       0x01c2b000, 1 * KiB },
133     { "twi2",       0x01c2b400, 1 * KiB },
134     { "twi3",       0x01c2b800, 1 * KiB },
135     { "twi4",       0x01c2c000, 1 * KiB },
136     { "scr",        0x01c2c400, 1 * KiB },
137     { "tvd-top",    0x01c30000, 4 * KiB },
138     { "tvd0",       0x01c31000, 4 * KiB },
139     { "tvd1",       0x01c32000, 4 * KiB },
140     { "tvd2",       0x01c33000, 4 * KiB },
141     { "tvd3",       0x01c34000, 4 * KiB },
142     { "gpu",        0x01c40000, 64 * KiB },
143     { "hstmr",      0x01c60000, 4 * KiB },
144     { "tcon-top",   0x01c70000, 4 * KiB },
145     { "lcd0",       0x01c71000, 4 * KiB },
146     { "lcd1",       0x01c72000, 4 * KiB },
147     { "tv0",        0x01c73000, 4 * KiB },
148     { "tv1",        0x01c74000, 4 * KiB },
149     { "tve-top",    0x01c90000, 16 * KiB },
150     { "tve0",       0x01c94000, 16 * KiB },
151     { "tve1",       0x01c98000, 16 * KiB },
152     { "mipi_dsi",   0x01ca0000, 4 * KiB },
153     { "mipi_dphy",  0x01ca1000, 4 * KiB },
154     { "ve",         0x01d00000, 1024 * KiB },
155     { "mp",         0x01e80000, 128 * KiB },
156     { "hdmi",       0x01ee0000, 128 * KiB },
157     { "prcm",       0x01f01400, 1 * KiB },
158     { "debug",      0x3f500000, 64 * KiB },
159     { "cpubist",    0x3f501000, 4 * KiB },
160     { "dcu",        0x3fff0000, 64 * KiB },
161     { "hstmr",      0x01c60000, 4 * KiB },
162     { "brom",       0xffff0000, 36 * KiB }
163 };
164 
165 /* Per Processor Interrupts */
166 enum {
167     AW_R40_GIC_PPI_MAINT     =  9,
168     AW_R40_GIC_PPI_HYPTIMER  = 10,
169     AW_R40_GIC_PPI_VIRTTIMER = 11,
170     AW_R40_GIC_PPI_SECTIMER  = 13,
171     AW_R40_GIC_PPI_PHYSTIMER = 14
172 };
173 
174 /* Shared Processor Interrupts */
175 enum {
176     AW_R40_GIC_SPI_UART0     =  1,
177     AW_R40_GIC_SPI_UART1     =  2,
178     AW_R40_GIC_SPI_UART2     =  3,
179     AW_R40_GIC_SPI_UART3     =  4,
180     AW_R40_GIC_SPI_TWI0      =  7,
181     AW_R40_GIC_SPI_UART4     = 17,
182     AW_R40_GIC_SPI_UART5     = 18,
183     AW_R40_GIC_SPI_UART6     = 19,
184     AW_R40_GIC_SPI_UART7     = 20,
185     AW_R40_GIC_SPI_TIMER0    = 22,
186     AW_R40_GIC_SPI_TIMER1    = 23,
187     AW_R40_GIC_SPI_MMC0      = 32,
188     AW_R40_GIC_SPI_MMC1      = 33,
189     AW_R40_GIC_SPI_MMC2      = 34,
190     AW_R40_GIC_SPI_MMC3      = 35,
191     AW_R40_GIC_SPI_EMAC      = 55,
192     AW_R40_GIC_SPI_AHCI      = 56,
193     AW_R40_GIC_SPI_OHCI1     = 64,
194     AW_R40_GIC_SPI_OHCI2     = 65,
195     AW_R40_GIC_SPI_EHCI1     = 76,
196     AW_R40_GIC_SPI_EHCI2     = 78,
197     AW_R40_GIC_SPI_GMAC      = 85,
198 };
199 
200 /* Allwinner R40 general constants */
201 enum {
202     AW_R40_GIC_NUM_SPI       = 128
203 };
204 
205 #define BOOT0_MAGIC             "eGON.BT0"
206 
207 /* The low 8-bits of the 'boot_media' field in the SPL header */
208 #define SUNXI_BOOTED_FROM_MMC0  0
209 #define SUNXI_BOOTED_FROM_NAND  1
210 #define SUNXI_BOOTED_FROM_MMC2  2
211 #define SUNXI_BOOTED_FROM_SPI   3
212 
213 struct boot_file_head {
214     uint32_t            b_instruction;
215     uint8_t             magic[8];
216     uint32_t            check_sum;
217     uint32_t            length;
218     uint32_t            pub_head_size;
219     uint32_t            fel_script_address;
220     uint32_t            fel_uEnv_length;
221     uint32_t            dt_name_offset;
222     uint32_t            dram_size;
223     uint32_t            boot_media;
224     uint32_t            string_pool[13];
225 };
226 
227 bool allwinner_r40_bootrom_setup(AwR40State *s, BlockBackend *blk, int unit)
228 {
229     const int64_t rom_size = 32 * KiB;
230     g_autofree uint8_t *buffer = g_new0(uint8_t, rom_size);
231     struct boot_file_head *head = (struct boot_file_head *)buffer;
232 
233     if (blk_pread(blk, 8 * KiB, rom_size, buffer, 0) < 0) {
234         error_setg(&error_fatal, "%s: failed to read BlockBackend data",
235                    __func__);
236         return false;
237     }
238 
239     /* we only check the magic string here. */
240     if (memcmp(head->magic, BOOT0_MAGIC, sizeof(head->magic))) {
241         return false;
242     }
243 
244     /*
245      * Simulate the behavior of the bootROM, it will change the boot_media
246      * flag to indicate where the chip is booting from. R40 can boot from
247      * mmc0 or mmc2, the default value of boot_media is zero
248      * (SUNXI_BOOTED_FROM_MMC0), let's fix this flag when it is booting from
249      * the others.
250      */
251     if (unit == 2) {
252         head->boot_media = cpu_to_le32(SUNXI_BOOTED_FROM_MMC2);
253     } else {
254         head->boot_media = cpu_to_le32(SUNXI_BOOTED_FROM_MMC0);
255     }
256 
257     rom_add_blob("allwinner-r40.bootrom", buffer, rom_size,
258                   rom_size, s->memmap[AW_R40_DEV_SRAM_A1],
259                   NULL, NULL, NULL, NULL, false);
260     return true;
261 }
262 
263 static void allwinner_r40_init(Object *obj)
264 {
265     static const char *mmc_names[AW_R40_NUM_MMCS] = {
266         "mmc0", "mmc1", "mmc2", "mmc3"
267     };
268     AwR40State *s = AW_R40(obj);
269 
270     s->memmap = allwinner_r40_memmap;
271 
272     for (int i = 0; i < AW_R40_NUM_CPUS; i++) {
273         object_initialize_child(obj, "cpu[*]", &s->cpus[i],
274                                 ARM_CPU_TYPE_NAME("cortex-a7"));
275     }
276 
277     object_initialize_child(obj, "gic", &s->gic, TYPE_ARM_GIC);
278 
279     object_initialize_child(obj, "timer", &s->timer, TYPE_AW_A10_PIT);
280     object_property_add_alias(obj, "clk0-freq", OBJECT(&s->timer),
281                               "clk0-freq");
282     object_property_add_alias(obj, "clk1-freq", OBJECT(&s->timer),
283                               "clk1-freq");
284 
285     object_initialize_child(obj, "wdt", &s->wdt, TYPE_AW_WDT_SUN4I);
286 
287     object_initialize_child(obj, "ccu", &s->ccu, TYPE_AW_R40_CCU);
288 
289     for (int i = 0; i < AW_R40_NUM_MMCS; i++) {
290         object_initialize_child(obj, mmc_names[i], &s->mmc[i],
291                                 TYPE_AW_SDHOST_SUN50I_A64);
292     }
293 
294     object_initialize_child(obj, "sata", &s->sata, TYPE_ALLWINNER_AHCI);
295 
296     for (size_t i = 0; i < AW_R40_NUM_USB; i++) {
297         object_initialize_child(obj, "ehci[*]", &s->ehci[i],
298                                 TYPE_PLATFORM_EHCI);
299         object_initialize_child(obj, "ohci[*]", &s->ohci[i],
300                                 TYPE_SYSBUS_OHCI);
301     }
302 
303     object_initialize_child(obj, "twi0", &s->i2c0, TYPE_AW_I2C_SUN6I);
304 
305     object_initialize_child(obj, "emac", &s->emac, TYPE_AW_EMAC);
306     object_initialize_child(obj, "gmac", &s->gmac, TYPE_AW_SUN8I_EMAC);
307     object_property_add_alias(obj, "gmac-phy-addr",
308                               OBJECT(&s->gmac), "phy-addr");
309 
310     object_initialize_child(obj, "dramc", &s->dramc, TYPE_AW_R40_DRAMC);
311     object_property_add_alias(obj, "ram-addr", OBJECT(&s->dramc),
312                              "ram-addr");
313     object_property_add_alias(obj, "ram-size", OBJECT(&s->dramc),
314                               "ram-size");
315 
316     object_initialize_child(obj, "sramc", &s->sramc, TYPE_AW_SRAMC_SUN8I_R40);
317 }
318 
319 static void allwinner_r40_realize(DeviceState *dev, Error **errp)
320 {
321     const char *r40_nic_models[] = { "gmac", "emac", NULL };
322     AwR40State *s = AW_R40(dev);
323 
324     /* CPUs */
325     for (unsigned i = 0; i < AW_R40_NUM_CPUS; i++) {
326 
327         /*
328          * Disable secondary CPUs. Guest EL3 firmware will start
329          * them via CPU reset control registers.
330          */
331         qdev_prop_set_bit(DEVICE(&s->cpus[i]), "start-powered-off",
332                           i > 0);
333 
334         /* All exception levels required */
335         qdev_prop_set_bit(DEVICE(&s->cpus[i]), "has_el3", true);
336         qdev_prop_set_bit(DEVICE(&s->cpus[i]), "has_el2", true);
337 
338         /* Mark realized */
339         qdev_realize(DEVICE(&s->cpus[i]), NULL, &error_fatal);
340     }
341 
342     /* Generic Interrupt Controller */
343     qdev_prop_set_uint32(DEVICE(&s->gic), "num-irq", AW_R40_GIC_NUM_SPI +
344                                                      GIC_INTERNAL);
345     qdev_prop_set_uint32(DEVICE(&s->gic), "revision", 2);
346     qdev_prop_set_uint32(DEVICE(&s->gic), "num-cpu", AW_R40_NUM_CPUS);
347     qdev_prop_set_bit(DEVICE(&s->gic), "has-security-extensions", false);
348     qdev_prop_set_bit(DEVICE(&s->gic), "has-virtualization-extensions", true);
349     sysbus_realize(SYS_BUS_DEVICE(&s->gic), &error_fatal);
350 
351     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gic), 0, s->memmap[AW_R40_DEV_GIC_DIST]);
352     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gic), 1, s->memmap[AW_R40_DEV_GIC_CPU]);
353     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gic), 2, s->memmap[AW_R40_DEV_GIC_HYP]);
354     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gic), 3, s->memmap[AW_R40_DEV_GIC_VCPU]);
355 
356     /*
357      * Wire the outputs from each CPU's generic timer and the GICv2
358      * maintenance interrupt signal to the appropriate GIC PPI inputs,
359      * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs.
360      */
361     for (unsigned i = 0; i < AW_R40_NUM_CPUS; i++) {
362         DeviceState *cpudev = DEVICE(&s->cpus[i]);
363         int ppibase = AW_R40_GIC_NUM_SPI + i * GIC_INTERNAL + GIC_NR_SGIS;
364         int irq;
365         /*
366          * Mapping from the output timer irq lines from the CPU to the
367          * GIC PPI inputs used for this board.
368          */
369         const int timer_irq[] = {
370             [GTIMER_PHYS] = AW_R40_GIC_PPI_PHYSTIMER,
371             [GTIMER_VIRT] = AW_R40_GIC_PPI_VIRTTIMER,
372             [GTIMER_HYP]  = AW_R40_GIC_PPI_HYPTIMER,
373             [GTIMER_SEC]  = AW_R40_GIC_PPI_SECTIMER,
374         };
375 
376         /* Connect CPU timer outputs to GIC PPI inputs */
377         for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
378             qdev_connect_gpio_out(cpudev, irq,
379                                   qdev_get_gpio_in(DEVICE(&s->gic),
380                                                    ppibase + timer_irq[irq]));
381         }
382 
383         /* Connect GIC outputs to CPU interrupt inputs */
384         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gic), i,
385                            qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
386         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gic), i + AW_R40_NUM_CPUS,
387                            qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
388         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gic), i + (2 * AW_R40_NUM_CPUS),
389                            qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
390         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gic), i + (3 * AW_R40_NUM_CPUS),
391                            qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
392 
393         /* GIC maintenance signal */
394         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gic), i + (4 * AW_R40_NUM_CPUS),
395                            qdev_get_gpio_in(DEVICE(&s->gic),
396                                             ppibase + AW_R40_GIC_PPI_MAINT));
397     }
398 
399     /* Timer */
400     sysbus_realize(SYS_BUS_DEVICE(&s->timer), &error_fatal);
401     sysbus_mmio_map(SYS_BUS_DEVICE(&s->timer), 0, s->memmap[AW_R40_DEV_PIT]);
402     sysbus_connect_irq(SYS_BUS_DEVICE(&s->timer), 0,
403                        qdev_get_gpio_in(DEVICE(&s->gic),
404                        AW_R40_GIC_SPI_TIMER0));
405     sysbus_connect_irq(SYS_BUS_DEVICE(&s->timer), 1,
406                        qdev_get_gpio_in(DEVICE(&s->gic),
407                        AW_R40_GIC_SPI_TIMER1));
408 
409     /* SRAM */
410     sysbus_realize(SYS_BUS_DEVICE(&s->sramc), &error_fatal);
411     sysbus_mmio_map(SYS_BUS_DEVICE(&s->sramc), 0, s->memmap[AW_R40_DEV_SRAMC]);
412 
413     memory_region_init_ram(&s->sram_a1, OBJECT(dev), "sram A1",
414                             16 * KiB, &error_abort);
415     memory_region_init_ram(&s->sram_a2, OBJECT(dev), "sram A2",
416                             16 * KiB, &error_abort);
417     memory_region_init_ram(&s->sram_a3, OBJECT(dev), "sram A3",
418                             13 * KiB, &error_abort);
419     memory_region_init_ram(&s->sram_a4, OBJECT(dev), "sram A4",
420                             3 * KiB, &error_abort);
421     memory_region_add_subregion(get_system_memory(),
422                                 s->memmap[AW_R40_DEV_SRAM_A1], &s->sram_a1);
423     memory_region_add_subregion(get_system_memory(),
424                                 s->memmap[AW_R40_DEV_SRAM_A2], &s->sram_a2);
425     memory_region_add_subregion(get_system_memory(),
426                                 s->memmap[AW_R40_DEV_SRAM_A3], &s->sram_a3);
427     memory_region_add_subregion(get_system_memory(),
428                                 s->memmap[AW_R40_DEV_SRAM_A4], &s->sram_a4);
429 
430     /* Clock Control Unit */
431     sysbus_realize(SYS_BUS_DEVICE(&s->ccu), &error_fatal);
432     sysbus_mmio_map(SYS_BUS_DEVICE(&s->ccu), 0, s->memmap[AW_R40_DEV_CCU]);
433 
434     /* SATA / AHCI */
435     sysbus_realize(SYS_BUS_DEVICE(&s->sata), &error_fatal);
436     sysbus_mmio_map(SYS_BUS_DEVICE(&s->sata), 0,
437                     allwinner_r40_memmap[AW_R40_DEV_AHCI]);
438     sysbus_connect_irq(SYS_BUS_DEVICE(&s->sata), 0,
439                        qdev_get_gpio_in(DEVICE(&s->gic), AW_R40_GIC_SPI_AHCI));
440 
441     /* USB */
442     for (size_t i = 0; i < AW_R40_NUM_USB; i++) {
443         g_autofree char *bus = g_strdup_printf("usb-bus.%zu", i);
444 
445         object_property_set_bool(OBJECT(&s->ehci[i]), "companion-enable", true,
446                                  &error_fatal);
447         sysbus_realize(SYS_BUS_DEVICE(&s->ehci[i]), &error_fatal);
448         sysbus_mmio_map(SYS_BUS_DEVICE(&s->ehci[i]), 0,
449                         allwinner_r40_memmap[i ? AW_R40_DEV_EHCI2
450                                                : AW_R40_DEV_EHCI1]);
451         sysbus_connect_irq(SYS_BUS_DEVICE(&s->ehci[i]), 0,
452                            qdev_get_gpio_in(DEVICE(&s->gic),
453                                             i ? AW_R40_GIC_SPI_EHCI2
454                                               : AW_R40_GIC_SPI_EHCI1));
455 
456         object_property_set_str(OBJECT(&s->ohci[i]), "masterbus", bus,
457                                 &error_fatal);
458         sysbus_realize(SYS_BUS_DEVICE(&s->ohci[i]), &error_fatal);
459         sysbus_mmio_map(SYS_BUS_DEVICE(&s->ohci[i]), 0,
460                         allwinner_r40_memmap[i ? AW_R40_DEV_OHCI2
461                                                : AW_R40_DEV_OHCI1]);
462         sysbus_connect_irq(SYS_BUS_DEVICE(&s->ohci[i]), 0,
463                            qdev_get_gpio_in(DEVICE(&s->gic),
464                                             i ? AW_R40_GIC_SPI_OHCI2
465                                               : AW_R40_GIC_SPI_OHCI1));
466     }
467 
468     /* SD/MMC */
469     for (int i = 0; i < AW_R40_NUM_MMCS; i++) {
470         qemu_irq irq = qdev_get_gpio_in(DEVICE(&s->gic),
471                                         AW_R40_GIC_SPI_MMC0 + i);
472         const hwaddr addr = s->memmap[AW_R40_DEV_MMC0 + i];
473 
474         object_property_set_link(OBJECT(&s->mmc[i]), "dma-memory",
475                                  OBJECT(get_system_memory()), &error_fatal);
476         sysbus_realize(SYS_BUS_DEVICE(&s->mmc[i]), &error_fatal);
477         sysbus_mmio_map(SYS_BUS_DEVICE(&s->mmc[i]), 0, addr);
478         sysbus_connect_irq(SYS_BUS_DEVICE(&s->mmc[i]), 0, irq);
479     }
480 
481     /* UART0. For future clocktree API: All UARTS are connected to APB2_CLK. */
482     for (int i = 0; i < AW_R40_NUM_UARTS; i++) {
483         static const int uart_irqs[AW_R40_NUM_UARTS] = {
484             AW_R40_GIC_SPI_UART0,
485             AW_R40_GIC_SPI_UART1,
486             AW_R40_GIC_SPI_UART2,
487             AW_R40_GIC_SPI_UART3,
488             AW_R40_GIC_SPI_UART4,
489             AW_R40_GIC_SPI_UART5,
490             AW_R40_GIC_SPI_UART6,
491             AW_R40_GIC_SPI_UART7,
492         };
493         const hwaddr addr = s->memmap[AW_R40_DEV_UART0 + i];
494 
495         serial_mm_init(get_system_memory(), addr, 2,
496                        qdev_get_gpio_in(DEVICE(&s->gic), uart_irqs[i]),
497                        115200, serial_hd(i), DEVICE_NATIVE_ENDIAN);
498     }
499 
500     /* I2C */
501     sysbus_realize(SYS_BUS_DEVICE(&s->i2c0), &error_fatal);
502     sysbus_mmio_map(SYS_BUS_DEVICE(&s->i2c0), 0, s->memmap[AW_R40_DEV_TWI0]);
503     sysbus_connect_irq(SYS_BUS_DEVICE(&s->i2c0), 0,
504                        qdev_get_gpio_in(DEVICE(&s->gic), AW_R40_GIC_SPI_TWI0));
505 
506     /* DRAMC */
507     sysbus_realize(SYS_BUS_DEVICE(&s->dramc), &error_fatal);
508     sysbus_mmio_map(SYS_BUS_DEVICE(&s->dramc), 0,
509                     s->memmap[AW_R40_DEV_DRAMCOM]);
510     sysbus_mmio_map(SYS_BUS_DEVICE(&s->dramc), 1,
511                     s->memmap[AW_R40_DEV_DRAMCTL]);
512     sysbus_mmio_map(SYS_BUS_DEVICE(&s->dramc), 2,
513                     s->memmap[AW_R40_DEV_DRAMPHY]);
514 
515     /* nic support gmac and emac */
516     for (int i = 0; i < ARRAY_SIZE(r40_nic_models) - 1; i++) {
517         NICInfo *nic = &nd_table[i];
518 
519         if (!nic->used) {
520             continue;
521         }
522         if (qemu_show_nic_models(nic->model, r40_nic_models)) {
523             exit(0);
524         }
525 
526         switch (qemu_find_nic_model(nic, r40_nic_models, r40_nic_models[0])) {
527         case 0: /* gmac */
528             qdev_set_nic_properties(DEVICE(&s->gmac), nic);
529             break;
530         case 1: /* emac */
531             qdev_set_nic_properties(DEVICE(&s->emac), nic);
532             break;
533         default:
534             exit(1);
535             break;
536         }
537     }
538 
539     /* GMAC */
540     object_property_set_link(OBJECT(&s->gmac), "dma-memory",
541                                      OBJECT(get_system_memory()), &error_fatal);
542     sysbus_realize(SYS_BUS_DEVICE(&s->gmac), &error_fatal);
543     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gmac), 0, s->memmap[AW_R40_DEV_GMAC]);
544     sysbus_connect_irq(SYS_BUS_DEVICE(&s->gmac), 0,
545                        qdev_get_gpio_in(DEVICE(&s->gic), AW_R40_GIC_SPI_GMAC));
546 
547     /* EMAC */
548     sysbus_realize(SYS_BUS_DEVICE(&s->emac), &error_fatal);
549     sysbus_mmio_map(SYS_BUS_DEVICE(&s->emac), 0, s->memmap[AW_R40_DEV_EMAC]);
550     sysbus_connect_irq(SYS_BUS_DEVICE(&s->emac), 0,
551                        qdev_get_gpio_in(DEVICE(&s->gic), AW_R40_GIC_SPI_EMAC));
552 
553     /* WDT */
554     sysbus_realize(SYS_BUS_DEVICE(&s->wdt), &error_fatal);
555     sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s->wdt), 0,
556                             allwinner_r40_memmap[AW_R40_DEV_WDT], 1);
557 
558     /* Unimplemented devices */
559     for (unsigned i = 0; i < ARRAY_SIZE(r40_unimplemented); i++) {
560         create_unimplemented_device(r40_unimplemented[i].device_name,
561                                     r40_unimplemented[i].base,
562                                     r40_unimplemented[i].size);
563     }
564 }
565 
566 static void allwinner_r40_class_init(ObjectClass *oc, void *data)
567 {
568     DeviceClass *dc = DEVICE_CLASS(oc);
569 
570     dc->realize = allwinner_r40_realize;
571     /* Reason: uses serial_hd() in realize function */
572     dc->user_creatable = false;
573 }
574 
575 static const TypeInfo allwinner_r40_type_info = {
576     .name = TYPE_AW_R40,
577     .parent = TYPE_DEVICE,
578     .instance_size = sizeof(AwR40State),
579     .instance_init = allwinner_r40_init,
580     .class_init = allwinner_r40_class_init,
581 };
582 
583 static void allwinner_r40_register_types(void)
584 {
585     type_register_static(&allwinner_r40_type_info);
586 }
587 
588 type_init(allwinner_r40_register_types)
589