xref: /openbmc/qemu/hw/avr/arduino.c (revision 5e437d3c)
1 /*
2  * QEMU Arduino boards
3  *
4  * Copyright (c) 2019-2020 Philippe Mathieu-Daudé
5  *
6  * This work is licensed under the terms of the GNU GPLv2 or later.
7  * See the COPYING file in the top-level directory.
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 /* TODO: Implement the use of EXTRAM */
12 
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "hw/boards.h"
16 #include "atmega.h"
17 #include "boot.h"
18 #include "qom/object.h"
19 
20 struct ArduinoMachineState {
21     /*< private >*/
22     MachineState parent_obj;
23     /*< public >*/
24     AtmegaMcuState mcu;
25 };
26 typedef struct ArduinoMachineState ArduinoMachineState;
27 
28 struct ArduinoMachineClass {
29     /*< private >*/
30     MachineClass parent_class;
31     /*< public >*/
32     const char *mcu_type;
33     uint64_t xtal_hz;
34 };
35 typedef struct ArduinoMachineClass ArduinoMachineClass;
36 
37 #define TYPE_ARDUINO_MACHINE \
38         MACHINE_TYPE_NAME("arduino")
39 DECLARE_OBJ_CHECKERS(ArduinoMachineState, ArduinoMachineClass,
40                      ARDUINO_MACHINE, TYPE_ARDUINO_MACHINE)
41 
42 static void arduino_machine_init(MachineState *machine)
43 {
44     ArduinoMachineClass *amc = ARDUINO_MACHINE_GET_CLASS(machine);
45     ArduinoMachineState *ams = ARDUINO_MACHINE(machine);
46 
47     object_initialize_child(OBJECT(machine), "mcu", &ams->mcu, amc->mcu_type);
48     object_property_set_uint(OBJECT(&ams->mcu), "xtal-frequency-hz",
49                              amc->xtal_hz, &error_abort);
50     sysbus_realize(SYS_BUS_DEVICE(&ams->mcu), &error_abort);
51 
52     if (machine->firmware) {
53         if (!avr_load_firmware(&ams->mcu.cpu, machine,
54                                &ams->mcu.flash, machine->firmware)) {
55             exit(1);
56         }
57     }
58 }
59 
60 static void arduino_machine_class_init(ObjectClass *oc, void *data)
61 {
62     MachineClass *mc = MACHINE_CLASS(oc);
63 
64     mc->init = arduino_machine_init;
65     mc->default_cpus = 1;
66     mc->min_cpus = mc->default_cpus;
67     mc->max_cpus = mc->default_cpus;
68     mc->no_floppy = 1;
69     mc->no_cdrom = 1;
70     mc->no_parallel = 1;
71 }
72 
73 static void arduino_duemilanove_class_init(ObjectClass *oc, void *data)
74 {
75     MachineClass *mc = MACHINE_CLASS(oc);
76     ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
77 
78     /*
79      * https://www.arduino.cc/en/Main/ArduinoBoardDuemilanove
80      * https://www.arduino.cc/en/uploads/Main/arduino-duemilanove-schematic.pdf
81      */
82     mc->desc        = "Arduino Duemilanove (ATmega168)",
83     mc->alias       = "2009";
84     amc->mcu_type   = TYPE_ATMEGA168_MCU;
85     amc->xtal_hz    = 16 * 1000 * 1000;
86 };
87 
88 static void arduino_uno_class_init(ObjectClass *oc, void *data)
89 {
90     MachineClass *mc = MACHINE_CLASS(oc);
91     ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
92 
93     /*
94      * https://store.arduino.cc/arduino-uno-rev3
95      * https://www.arduino.cc/en/uploads/Main/arduino-uno-schematic.pdf
96      */
97     mc->desc        = "Arduino UNO (ATmega328P)";
98     mc->alias       = "uno";
99     amc->mcu_type   = TYPE_ATMEGA328_MCU;
100     amc->xtal_hz    = 16 * 1000 * 1000;
101 };
102 
103 static void arduino_mega_class_init(ObjectClass *oc, void *data)
104 {
105     MachineClass *mc = MACHINE_CLASS(oc);
106     ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
107 
108     /*
109      * https://www.arduino.cc/en/Main/ArduinoBoardMega
110      * https://www.arduino.cc/en/uploads/Main/arduino-mega2560-schematic.pdf
111      */
112     mc->desc        = "Arduino Mega (ATmega1280)";
113     mc->alias       = "mega";
114     amc->mcu_type   = TYPE_ATMEGA1280_MCU;
115     amc->xtal_hz    = 16 * 1000 * 1000;
116 };
117 
118 static void arduino_mega2560_class_init(ObjectClass *oc, void *data)
119 {
120     MachineClass *mc = MACHINE_CLASS(oc);
121     ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
122 
123     /*
124      * https://store.arduino.cc/arduino-mega-2560-rev3
125      * https://www.arduino.cc/en/uploads/Main/arduino-mega2560_R3-sch.pdf
126      */
127     mc->desc        = "Arduino Mega 2560 (ATmega2560)";
128     mc->alias       = "mega2560";
129     amc->mcu_type   = TYPE_ATMEGA2560_MCU;
130     amc->xtal_hz    = 16 * 1000 * 1000; /* CSTCE16M0V53-R0 */
131 };
132 
133 static const TypeInfo arduino_machine_types[] = {
134     {
135         .name          = MACHINE_TYPE_NAME("arduino-duemilanove"),
136         .parent        = TYPE_ARDUINO_MACHINE,
137         .class_init    = arduino_duemilanove_class_init,
138     }, {
139         .name          = MACHINE_TYPE_NAME("arduino-uno"),
140         .parent        = TYPE_ARDUINO_MACHINE,
141         .class_init    = arduino_uno_class_init,
142     }, {
143         .name          = MACHINE_TYPE_NAME("arduino-mega"),
144         .parent        = TYPE_ARDUINO_MACHINE,
145         .class_init    = arduino_mega_class_init,
146     }, {
147         .name          = MACHINE_TYPE_NAME("arduino-mega-2560-v3"),
148         .parent        = TYPE_ARDUINO_MACHINE,
149         .class_init    = arduino_mega2560_class_init,
150     }, {
151         .name           = TYPE_ARDUINO_MACHINE,
152         .parent         = TYPE_MACHINE,
153         .instance_size  = sizeof(ArduinoMachineState),
154         .class_size     = sizeof(ArduinoMachineClass),
155         .class_init     = arduino_machine_class_init,
156         .abstract       = true,
157     }
158 };
159 
160 DEFINE_TYPES(arduino_machine_types)
161