xref: /qemu/hw/arm/digic_boards.c (revision d7a84021)
1 /*
2  * QEMU model of the Canon DIGIC boards (cameras indeed :).
3  *
4  * Copyright (C) 2013 Antony Pavlov <antonynpavlov@gmail.com>
5  *
6  * This model is based on reverse engineering efforts
7  * made by CHDK (http://chdk.wikia.com) and
8  * Magic Lantern (http://www.magiclantern.fm) projects
9  * contributors.
10  *
11  * See docs here:
12  *   http://magiclantern.wikia.com/wiki/Register_Map
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  */
25 
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "qemu-common.h"
29 #include "qemu/datadir.h"
30 #include "cpu.h"
31 #include "hw/boards.h"
32 #include "exec/address-spaces.h"
33 #include "qemu/error-report.h"
34 #include "hw/arm/digic.h"
35 #include "hw/block/flash.h"
36 #include "hw/loader.h"
37 #include "sysemu/sysemu.h"
38 #include "sysemu/qtest.h"
39 #include "qemu/units.h"
40 #include "qemu/cutils.h"
41 
42 #define DIGIC4_ROM0_BASE      0xf0000000
43 #define DIGIC4_ROM1_BASE      0xf8000000
44 #define DIGIC4_ROM_MAX_SIZE   0x08000000
45 
46 typedef struct DigicBoard {
47     void (*add_rom0)(DigicState *, hwaddr, const char *);
48     const char *rom0_def_filename;
49     void (*add_rom1)(DigicState *, hwaddr, const char *);
50     const char *rom1_def_filename;
51 } DigicBoard;
52 
53 static void digic4_board_init(MachineState *machine, DigicBoard *board)
54 {
55     Error *err = NULL;
56     DigicState *s = DIGIC(object_new(TYPE_DIGIC));
57     MachineClass *mc = MACHINE_GET_CLASS(machine);
58 
59     if (machine->ram_size != mc->default_ram_size) {
60         char *sz = size_to_str(mc->default_ram_size);
61         error_report("Invalid RAM size, should be %s", sz);
62         g_free(sz);
63         exit(EXIT_FAILURE);
64     }
65 
66     if (!qdev_realize(DEVICE(s), NULL, &err)) {
67         error_reportf_err(err, "Couldn't realize DIGIC SoC: ");
68         exit(1);
69     }
70 
71     memory_region_add_subregion(get_system_memory(), 0, machine->ram);
72 
73     if (board->add_rom0) {
74         board->add_rom0(s, DIGIC4_ROM0_BASE,
75                         machine->firmware ?: board->rom0_def_filename);
76     }
77 
78     if (board->add_rom1) {
79         board->add_rom1(s, DIGIC4_ROM1_BASE,
80                         machine->firmware ?: board->rom1_def_filename);
81     }
82 }
83 
84 static void digic_load_rom(DigicState *s, hwaddr addr,
85                            hwaddr max_size, const char *filename)
86 {
87     target_long rom_size;
88 
89     if (qtest_enabled()) {
90         /* qtest runs no code so don't attempt a ROM load which
91          * could fail and result in a spurious test failure.
92          */
93         return;
94     }
95 
96     if (filename) {
97         char *fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, filename);
98 
99         if (!fn) {
100             error_report("Couldn't find rom image '%s'.", filename);
101             exit(1);
102         }
103 
104         rom_size = load_image_targphys(fn, addr, max_size);
105         if (rom_size < 0 || rom_size > max_size) {
106             error_report("Couldn't load rom image '%s'.", filename);
107             exit(1);
108         }
109         g_free(fn);
110     }
111 }
112 
113 /*
114  * Samsung K8P3215UQB
115  * 64M Bit (4Mx16) Page Mode / Multi-Bank NOR Flash Memory
116  */
117 static void digic4_add_k8p3215uqb_rom(DigicState *s, hwaddr addr,
118                                       const char *filename)
119 {
120 #define FLASH_K8P3215UQB_SIZE (4 * 1024 * 1024)
121 #define FLASH_K8P3215UQB_SECTOR_SIZE (64 * 1024)
122 
123     pflash_cfi02_register(addr, "pflash", FLASH_K8P3215UQB_SIZE,
124                           NULL, FLASH_K8P3215UQB_SECTOR_SIZE,
125                           DIGIC4_ROM_MAX_SIZE / FLASH_K8P3215UQB_SIZE,
126                           4,
127                           0x00EC, 0x007E, 0x0003, 0x0001,
128                           0x0555, 0x2aa, 0);
129 
130     digic_load_rom(s, addr, FLASH_K8P3215UQB_SIZE, filename);
131 }
132 
133 static DigicBoard digic4_board_canon_a1100 = {
134     .add_rom1 = digic4_add_k8p3215uqb_rom,
135     .rom1_def_filename = "canon-a1100-rom1.bin",
136 };
137 
138 static void canon_a1100_init(MachineState *machine)
139 {
140     digic4_board_init(machine, &digic4_board_canon_a1100);
141 }
142 
143 static void canon_a1100_machine_init(MachineClass *mc)
144 {
145     mc->desc = "Canon PowerShot A1100 IS (ARM946)";
146     mc->init = &canon_a1100_init;
147     mc->ignore_memory_transaction_failures = true;
148     mc->default_ram_size = 64 * MiB;
149     mc->default_ram_id = "ram";
150 }
151 
152 DEFINE_MACHINE("canon-a1100", canon_a1100_machine_init)
153