xref: /openbmc/qemu/hw/avr/arduino.c (revision 8fa3b702)
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     /* https://www.arduino.cc/en/Main/ArduinoBoardDuemilanove */
79     mc->desc        = "Arduino Duemilanove (ATmega168)",
80     mc->alias       = "2009";
81     amc->mcu_type   = TYPE_ATMEGA168_MCU;
82     amc->xtal_hz    = 16 * 1000 * 1000;
83 };
84 
85 static void arduino_uno_class_init(ObjectClass *oc, void *data)
86 {
87     MachineClass *mc = MACHINE_CLASS(oc);
88     ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
89 
90     /* https://store.arduino.cc/arduino-uno-rev3 */
91     mc->desc        = "Arduino UNO (ATmega328P)";
92     mc->alias       = "uno";
93     amc->mcu_type   = TYPE_ATMEGA328_MCU;
94     amc->xtal_hz    = 16 * 1000 * 1000;
95 };
96 
97 static void arduino_mega_class_init(ObjectClass *oc, void *data)
98 {
99     MachineClass *mc = MACHINE_CLASS(oc);
100     ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
101 
102     /* https://www.arduino.cc/en/Main/ArduinoBoardMega */
103     mc->desc        = "Arduino Mega (ATmega1280)";
104     mc->alias       = "mega";
105     amc->mcu_type   = TYPE_ATMEGA1280_MCU;
106     amc->xtal_hz    = 16 * 1000 * 1000;
107 };
108 
109 static void arduino_mega2560_class_init(ObjectClass *oc, void *data)
110 {
111     MachineClass *mc = MACHINE_CLASS(oc);
112     ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
113 
114     /* https://store.arduino.cc/arduino-mega-2560-rev3 */
115     mc->desc        = "Arduino Mega 2560 (ATmega2560)";
116     mc->alias       = "mega2560";
117     amc->mcu_type   = TYPE_ATMEGA2560_MCU;
118     amc->xtal_hz    = 16 * 1000 * 1000; /* CSTCE16M0V53-R0 */
119 };
120 
121 static const TypeInfo arduino_machine_types[] = {
122     {
123         .name          = MACHINE_TYPE_NAME("arduino-duemilanove"),
124         .parent        = TYPE_ARDUINO_MACHINE,
125         .class_init    = arduino_duemilanove_class_init,
126     }, {
127         .name          = MACHINE_TYPE_NAME("arduino-uno"),
128         .parent        = TYPE_ARDUINO_MACHINE,
129         .class_init    = arduino_uno_class_init,
130     }, {
131         .name          = MACHINE_TYPE_NAME("arduino-mega"),
132         .parent        = TYPE_ARDUINO_MACHINE,
133         .class_init    = arduino_mega_class_init,
134     }, {
135         .name          = MACHINE_TYPE_NAME("arduino-mega-2560-v3"),
136         .parent        = TYPE_ARDUINO_MACHINE,
137         .class_init    = arduino_mega2560_class_init,
138     }, {
139         .name           = TYPE_ARDUINO_MACHINE,
140         .parent         = TYPE_MACHINE,
141         .instance_size  = sizeof(ArduinoMachineState),
142         .class_size     = sizeof(ArduinoMachineClass),
143         .class_init     = arduino_machine_class_init,
144         .abstract       = true,
145     }
146 };
147 
148 DEFINE_TYPES(arduino_machine_types)
149