xref: /openbmc/qemu/hw/arm/versatilepb.c (revision f6e33708)
1 /*
2  * ARM Versatile Platform/Application Baseboard System emulation.
3  *
4  * Copyright (c) 2005-2007 CodeSourcery.
5  * Written by Paul Brook
6  *
7  * This code is licensed under the GPL.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "hw/sysbus.h"
13 #include "migration/vmstate.h"
14 #include "hw/arm/boot.h"
15 #include "hw/net/smc91c111.h"
16 #include "net/net.h"
17 #include "sysemu/sysemu.h"
18 #include "hw/pci/pci.h"
19 #include "hw/i2c/i2c.h"
20 #include "hw/i2c/arm_sbcon_i2c.h"
21 #include "hw/irq.h"
22 #include "hw/boards.h"
23 #include "hw/block/flash.h"
24 #include "qemu/error-report.h"
25 #include "hw/char/pl011.h"
26 #include "hw/sd/sd.h"
27 #include "qom/object.h"
28 #include "audio/audio.h"
29 #include "target/arm/cpu-qom.h"
30 
31 #define VERSATILE_FLASH_ADDR 0x34000000
32 #define VERSATILE_FLASH_SIZE (64 * 1024 * 1024)
33 #define VERSATILE_FLASH_SECT_SIZE (256 * 1024)
34 
35 /* Primary interrupt controller.  */
36 
37 #define TYPE_VERSATILE_PB_SIC "versatilepb_sic"
38 OBJECT_DECLARE_SIMPLE_TYPE(vpb_sic_state, VERSATILE_PB_SIC)
39 
40 struct vpb_sic_state {
41     SysBusDevice parent_obj;
42 
43     MemoryRegion iomem;
44     uint32_t level;
45     uint32_t mask;
46     uint32_t pic_enable;
47     qemu_irq parent[32];
48     int irq;
49 };
50 
51 static const VMStateDescription vmstate_vpb_sic = {
52     .name = "versatilepb_sic",
53     .version_id = 1,
54     .minimum_version_id = 1,
55     .fields = (const VMStateField[]) {
56         VMSTATE_UINT32(level, vpb_sic_state),
57         VMSTATE_UINT32(mask, vpb_sic_state),
58         VMSTATE_UINT32(pic_enable, vpb_sic_state),
59         VMSTATE_END_OF_LIST()
60     }
61 };
62 
63 static void vpb_sic_update(vpb_sic_state *s)
64 {
65     uint32_t flags;
66 
67     flags = s->level & s->mask;
68     qemu_set_irq(s->parent[s->irq], flags != 0);
69 }
70 
71 static void vpb_sic_update_pic(vpb_sic_state *s)
72 {
73     int i;
74     uint32_t mask;
75 
76     for (i = 21; i <= 30; i++) {
77         mask = 1u << i;
78         if (!(s->pic_enable & mask))
79             continue;
80         qemu_set_irq(s->parent[i], (s->level & mask) != 0);
81     }
82 }
83 
84 static void vpb_sic_set_irq(void *opaque, int irq, int level)
85 {
86     vpb_sic_state *s = (vpb_sic_state *)opaque;
87     if (level)
88         s->level |= 1u << irq;
89     else
90         s->level &= ~(1u << irq);
91     if (s->pic_enable & (1u << irq))
92         qemu_set_irq(s->parent[irq], level);
93     vpb_sic_update(s);
94 }
95 
96 static uint64_t vpb_sic_read(void *opaque, hwaddr offset,
97                              unsigned size)
98 {
99     vpb_sic_state *s = (vpb_sic_state *)opaque;
100 
101     switch (offset >> 2) {
102     case 0: /* STATUS */
103         return s->level & s->mask;
104     case 1: /* RAWSTAT */
105         return s->level;
106     case 2: /* ENABLE */
107         return s->mask;
108     case 4: /* SOFTINT */
109         return s->level & 1;
110     case 8: /* PICENABLE */
111         return s->pic_enable;
112     default:
113         printf ("vpb_sic_read: Bad register offset 0x%x\n", (int)offset);
114         return 0;
115     }
116 }
117 
118 static void vpb_sic_write(void *opaque, hwaddr offset,
119                           uint64_t value, unsigned size)
120 {
121     vpb_sic_state *s = (vpb_sic_state *)opaque;
122 
123     switch (offset >> 2) {
124     case 2: /* ENSET */
125         s->mask |= value;
126         break;
127     case 3: /* ENCLR */
128         s->mask &= ~value;
129         break;
130     case 4: /* SOFTINTSET */
131         if (value)
132             s->mask |= 1;
133         break;
134     case 5: /* SOFTINTCLR */
135         if (value)
136             s->mask &= ~1u;
137         break;
138     case 8: /* PICENSET */
139         s->pic_enable |= (value & 0x7fe00000);
140         vpb_sic_update_pic(s);
141         break;
142     case 9: /* PICENCLR */
143         s->pic_enable &= ~value;
144         vpb_sic_update_pic(s);
145         break;
146     default:
147         printf ("vpb_sic_write: Bad register offset 0x%x\n", (int)offset);
148         return;
149     }
150     vpb_sic_update(s);
151 }
152 
153 static const MemoryRegionOps vpb_sic_ops = {
154     .read = vpb_sic_read,
155     .write = vpb_sic_write,
156     .endianness = DEVICE_NATIVE_ENDIAN,
157 };
158 
159 static void vpb_sic_init(Object *obj)
160 {
161     DeviceState *dev = DEVICE(obj);
162     vpb_sic_state *s = VERSATILE_PB_SIC(obj);
163     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
164     int i;
165 
166     qdev_init_gpio_in(dev, vpb_sic_set_irq, 32);
167     for (i = 0; i < 32; i++) {
168         sysbus_init_irq(sbd, &s->parent[i]);
169     }
170     s->irq = 31;
171     memory_region_init_io(&s->iomem, obj, &vpb_sic_ops, s,
172                           "vpb-sic", 0x1000);
173     sysbus_init_mmio(sbd, &s->iomem);
174 }
175 
176 /* Board init.  */
177 
178 /* The AB and PB boards both use the same core, just with different
179    peripherals and expansion busses.  For now we emulate a subset of the
180    PB peripherals and just change the board ID.  */
181 
182 static struct arm_boot_info versatile_binfo;
183 
184 static void versatile_init(MachineState *machine, int board_id)
185 {
186     Object *cpuobj;
187     ARMCPU *cpu;
188     MemoryRegion *sysmem = get_system_memory();
189     qemu_irq pic[32];
190     qemu_irq sic[32];
191     DeviceState *dev, *sysctl;
192     SysBusDevice *busdev;
193     DeviceState *pl041;
194     PCIBus *pci_bus;
195     I2CBus *i2c;
196     int n;
197     DriveInfo *dinfo;
198 
199     if (machine->ram_size > 0x10000000) {
200         /* Device starting at address 0x10000000,
201          * and memory cannot overlap with devices.
202          * Refuse to run rather than behaving very confusingly.
203          */
204         error_report("versatilepb: memory size must not exceed 256MB");
205         exit(1);
206     }
207 
208     cpuobj = object_new(machine->cpu_type);
209 
210     /* By default ARM1176 CPUs have EL3 enabled.  This board does not
211      * currently support EL3 so the CPU EL3 property is disabled before
212      * realization.
213      */
214     if (object_property_find(cpuobj, "has_el3")) {
215         object_property_set_bool(cpuobj, "has_el3", false, &error_fatal);
216     }
217 
218     qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
219 
220     cpu = ARM_CPU(cpuobj);
221 
222     /* ??? RAM should repeat to fill physical memory space.  */
223     /* SDRAM at address zero.  */
224     memory_region_add_subregion(sysmem, 0, machine->ram);
225 
226     sysctl = qdev_new("realview_sysctl");
227     qdev_prop_set_uint32(sysctl, "sys_id", 0x41007004);
228     qdev_prop_set_uint32(sysctl, "proc_id", 0x02000000);
229     sysbus_realize_and_unref(SYS_BUS_DEVICE(sysctl), &error_fatal);
230     sysbus_mmio_map(SYS_BUS_DEVICE(sysctl), 0, 0x10000000);
231 
232     dev = sysbus_create_varargs("pl190", 0x10140000,
233                                 qdev_get_gpio_in(DEVICE(cpu), ARM_CPU_IRQ),
234                                 qdev_get_gpio_in(DEVICE(cpu), ARM_CPU_FIQ),
235                                 NULL);
236     for (n = 0; n < 32; n++) {
237         pic[n] = qdev_get_gpio_in(dev, n);
238     }
239     dev = sysbus_create_simple(TYPE_VERSATILE_PB_SIC, 0x10003000, NULL);
240     for (n = 0; n < 32; n++) {
241         sysbus_connect_irq(SYS_BUS_DEVICE(dev), n, pic[n]);
242         sic[n] = qdev_get_gpio_in(dev, n);
243     }
244 
245     sysbus_create_simple("pl050_keyboard", 0x10006000, sic[3]);
246     sysbus_create_simple("pl050_mouse", 0x10007000, sic[4]);
247 
248     dev = qdev_new("versatile_pci");
249     busdev = SYS_BUS_DEVICE(dev);
250     sysbus_realize_and_unref(busdev, &error_fatal);
251     sysbus_mmio_map(busdev, 0, 0x10001000); /* PCI controller regs */
252     sysbus_mmio_map(busdev, 1, 0x41000000); /* PCI self-config */
253     sysbus_mmio_map(busdev, 2, 0x42000000); /* PCI config */
254     sysbus_mmio_map(busdev, 3, 0x43000000); /* PCI I/O */
255     sysbus_mmio_map(busdev, 4, 0x44000000); /* PCI memory window 1 */
256     sysbus_mmio_map(busdev, 5, 0x50000000); /* PCI memory window 2 */
257     sysbus_mmio_map(busdev, 6, 0x60000000); /* PCI memory window 3 */
258     sysbus_connect_irq(busdev, 0, sic[27]);
259     sysbus_connect_irq(busdev, 1, sic[28]);
260     sysbus_connect_irq(busdev, 2, sic[29]);
261     sysbus_connect_irq(busdev, 3, sic[30]);
262     pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci");
263 
264     if (qemu_find_nic_info("smc91c111", true, NULL)) {
265         smc91c111_init(0x10010000, sic[25]);
266     }
267     pci_init_nic_devices(pci_bus, "rtl8139");
268 
269     if (machine_usb(machine)) {
270         pci_create_simple(pci_bus, -1, "pci-ohci");
271     }
272     n = drive_get_max_bus(IF_SCSI);
273     while (n >= 0) {
274         dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a"));
275         lsi53c8xx_handle_legacy_cmdline(dev);
276         n--;
277     }
278 
279     pl011_create(0x101f1000, pic[12], serial_hd(0));
280     pl011_create(0x101f2000, pic[13], serial_hd(1));
281     pl011_create(0x101f3000, pic[14], serial_hd(2));
282     pl011_create(0x10009000, sic[6], serial_hd(3));
283 
284     dev = qdev_new("pl080");
285     object_property_set_link(OBJECT(dev), "downstream", OBJECT(sysmem),
286                              &error_fatal);
287     busdev = SYS_BUS_DEVICE(dev);
288     sysbus_realize_and_unref(busdev, &error_fatal);
289     sysbus_mmio_map(busdev, 0, 0x10130000);
290     sysbus_connect_irq(busdev, 0, pic[17]);
291 
292     sysbus_create_simple("sp804", 0x101e2000, pic[4]);
293     sysbus_create_simple("sp804", 0x101e3000, pic[5]);
294 
295     sysbus_create_simple("pl061", 0x101e4000, pic[6]);
296     sysbus_create_simple("pl061", 0x101e5000, pic[7]);
297     sysbus_create_simple("pl061", 0x101e6000, pic[8]);
298     sysbus_create_simple("pl061", 0x101e7000, pic[9]);
299 
300     /* The versatile/PB actually has a modified Color LCD controller
301        that includes hardware cursor support from the PL111.  */
302     dev = sysbus_create_simple("pl110_versatile", 0x10120000, pic[16]);
303     /* Wire up the mux control signals from the SYS_CLCD register */
304     qdev_connect_gpio_out(sysctl, 0, qdev_get_gpio_in(dev, 0));
305 
306     dev = sysbus_create_varargs("pl181", 0x10005000, sic[22], sic[1], NULL);
307     dinfo = drive_get(IF_SD, 0, 0);
308     if (dinfo) {
309         DeviceState *card;
310 
311         card = qdev_new(TYPE_SD_CARD);
312         qdev_prop_set_drive_err(card, "drive", blk_by_legacy_dinfo(dinfo),
313                                 &error_fatal);
314         qdev_realize_and_unref(card, qdev_get_child_bus(dev, "sd-bus"),
315                                &error_fatal);
316     }
317 
318     dev = sysbus_create_varargs("pl181", 0x1000b000, sic[23], sic[2], NULL);
319     dinfo = drive_get(IF_SD, 0, 1);
320     if (dinfo) {
321         DeviceState *card;
322 
323         card = qdev_new(TYPE_SD_CARD);
324         qdev_prop_set_drive_err(card, "drive", blk_by_legacy_dinfo(dinfo),
325                                 &error_fatal);
326         qdev_realize_and_unref(card, qdev_get_child_bus(dev, "sd-bus"),
327                                &error_fatal);
328     }
329 
330     /* Add PL031 Real Time Clock. */
331     sysbus_create_simple("pl031", 0x101e8000, pic[10]);
332 
333     dev = sysbus_create_simple(TYPE_ARM_SBCON_I2C, 0x10002000, NULL);
334     i2c = (I2CBus *)qdev_get_child_bus(dev, "i2c");
335     i2c_slave_create_simple(i2c, "ds1338", 0x68);
336 
337     /* Add PL041 AACI Interface to the LM4549 codec */
338     pl041 = qdev_new("pl041");
339     qdev_prop_set_uint32(pl041, "nc_fifo_depth", 512);
340     if (machine->audiodev) {
341         qdev_prop_set_string(pl041, "audiodev", machine->audiodev);
342     }
343     sysbus_realize_and_unref(SYS_BUS_DEVICE(pl041), &error_fatal);
344     sysbus_mmio_map(SYS_BUS_DEVICE(pl041), 0, 0x10004000);
345     sysbus_connect_irq(SYS_BUS_DEVICE(pl041), 0, sic[24]);
346 
347     /* Memory map for Versatile/PB:  */
348     /* 0x10000000 System registers.  */
349     /* 0x10001000 PCI controller config registers.  */
350     /* 0x10002000 Serial bus interface.  */
351     /*  0x10003000 Secondary interrupt controller.  */
352     /* 0x10004000 AACI (audio).  */
353     /*  0x10005000 MMCI0.  */
354     /*  0x10006000 KMI0 (keyboard).  */
355     /*  0x10007000 KMI1 (mouse).  */
356     /* 0x10008000 Character LCD Interface.  */
357     /*  0x10009000 UART3.  */
358     /* 0x1000a000 Smart card 1.  */
359     /*  0x1000b000 MMCI1.  */
360     /*  0x10010000 Ethernet.  */
361     /* 0x10020000 USB.  */
362     /* 0x10100000 SSMC.  */
363     /* 0x10110000 MPMC.  */
364     /*  0x10120000 CLCD Controller.  */
365     /*  0x10130000 DMA Controller.  */
366     /*  0x10140000 Vectored interrupt controller.  */
367     /* 0x101d0000 AHB Monitor Interface.  */
368     /* 0x101e0000 System Controller.  */
369     /* 0x101e1000 Watchdog Interface.  */
370     /* 0x101e2000 Timer 0/1.  */
371     /* 0x101e3000 Timer 2/3.  */
372     /* 0x101e4000 GPIO port 0.  */
373     /* 0x101e5000 GPIO port 1.  */
374     /* 0x101e6000 GPIO port 2.  */
375     /* 0x101e7000 GPIO port 3.  */
376     /* 0x101e8000 RTC.  */
377     /* 0x101f0000 Smart card 0.  */
378     /*  0x101f1000 UART0.  */
379     /*  0x101f2000 UART1.  */
380     /*  0x101f3000 UART2.  */
381     /* 0x101f4000 SSPI.  */
382     /* 0x34000000 NOR Flash */
383 
384     dinfo = drive_get(IF_PFLASH, 0, 0);
385     pflash_cfi01_register(VERSATILE_FLASH_ADDR, "versatile.flash",
386                           VERSATILE_FLASH_SIZE,
387                           dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
388                           VERSATILE_FLASH_SECT_SIZE,
389                           4, 0x0089, 0x0018, 0x0000, 0x0, 0);
390 
391     versatile_binfo.ram_size = machine->ram_size;
392     versatile_binfo.board_id = board_id;
393     arm_load_kernel(cpu, machine, &versatile_binfo);
394 }
395 
396 static void vpb_init(MachineState *machine)
397 {
398     versatile_init(machine, 0x183);
399 }
400 
401 static void vab_init(MachineState *machine)
402 {
403     versatile_init(machine, 0x25e);
404 }
405 
406 static void versatilepb_class_init(ObjectClass *oc, void *data)
407 {
408     MachineClass *mc = MACHINE_CLASS(oc);
409 
410     mc->desc = "ARM Versatile/PB (ARM926EJ-S)";
411     mc->init = vpb_init;
412     mc->block_default_type = IF_SCSI;
413     mc->ignore_memory_transaction_failures = true;
414     mc->default_cpu_type = ARM_CPU_TYPE_NAME("arm926");
415     mc->default_ram_id = "versatile.ram";
416 
417     machine_add_audiodev_property(mc);
418 }
419 
420 static const TypeInfo versatilepb_type = {
421     .name = MACHINE_TYPE_NAME("versatilepb"),
422     .parent = TYPE_MACHINE,
423     .class_init = versatilepb_class_init,
424 };
425 
426 static void versatileab_class_init(ObjectClass *oc, void *data)
427 {
428     MachineClass *mc = MACHINE_CLASS(oc);
429 
430     mc->desc = "ARM Versatile/AB (ARM926EJ-S)";
431     mc->init = vab_init;
432     mc->block_default_type = IF_SCSI;
433     mc->ignore_memory_transaction_failures = true;
434     mc->default_cpu_type = ARM_CPU_TYPE_NAME("arm926");
435     mc->default_ram_id = "versatile.ram";
436 
437     machine_add_audiodev_property(mc);
438 }
439 
440 static const TypeInfo versatileab_type = {
441     .name = MACHINE_TYPE_NAME("versatileab"),
442     .parent = TYPE_MACHINE,
443     .class_init = versatileab_class_init,
444 };
445 
446 static void versatile_machine_init(void)
447 {
448     type_register_static(&versatilepb_type);
449     type_register_static(&versatileab_type);
450 }
451 
452 type_init(versatile_machine_init)
453 
454 static void vpb_sic_class_init(ObjectClass *klass, void *data)
455 {
456     DeviceClass *dc = DEVICE_CLASS(klass);
457 
458     dc->vmsd = &vmstate_vpb_sic;
459 }
460 
461 static const TypeInfo vpb_sic_info = {
462     .name          = TYPE_VERSATILE_PB_SIC,
463     .parent        = TYPE_SYS_BUS_DEVICE,
464     .instance_size = sizeof(vpb_sic_state),
465     .instance_init = vpb_sic_init,
466     .class_init    = vpb_sic_class_init,
467 };
468 
469 static void versatilepb_register_types(void)
470 {
471     type_register_static(&vpb_sic_info);
472 }
473 
474 type_init(versatilepb_register_types)
475