1d91fd756SAntony Pavlov /*
2d91fd756SAntony Pavlov * QEMU model of the Canon DIGIC boards (cameras indeed :).
3d91fd756SAntony Pavlov *
4d91fd756SAntony Pavlov * Copyright (C) 2013 Antony Pavlov <antonynpavlov@gmail.com>
5d91fd756SAntony Pavlov *
6d91fd756SAntony Pavlov * This model is based on reverse engineering efforts
7d91fd756SAntony Pavlov * made by CHDK (http://chdk.wikia.com) and
8d91fd756SAntony Pavlov * Magic Lantern (http://www.magiclantern.fm) projects
9d91fd756SAntony Pavlov * contributors.
10d91fd756SAntony Pavlov *
11d91fd756SAntony Pavlov * See docs here:
12d91fd756SAntony Pavlov * http://magiclantern.wikia.com/wiki/Register_Map
13d91fd756SAntony Pavlov *
14d91fd756SAntony Pavlov * This program is free software; you can redistribute it and/or modify
15d91fd756SAntony Pavlov * it under the terms of the GNU General Public License as published by
16d91fd756SAntony Pavlov * the Free Software Foundation; either version 2 of the License, or
17d91fd756SAntony Pavlov * (at your option) any later version.
18d91fd756SAntony Pavlov *
19d91fd756SAntony Pavlov * This program is distributed in the hope that it will be useful,
20d91fd756SAntony Pavlov * but WITHOUT ANY WARRANTY; without even the implied warranty of
21d91fd756SAntony Pavlov * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22d91fd756SAntony Pavlov * GNU General Public License for more details.
23d91fd756SAntony Pavlov *
24d91fd756SAntony Pavlov */
25d91fd756SAntony Pavlov
2612b16722SPeter Maydell #include "qemu/osdep.h"
27da34e65cSMarkus Armbruster #include "qapi/error.h"
282c65db5eSPaolo Bonzini #include "qemu/datadir.h"
29d91fd756SAntony Pavlov #include "hw/boards.h"
30d91fd756SAntony Pavlov #include "qemu/error-report.h"
31d91fd756SAntony Pavlov #include "hw/arm/digic.h"
3204234a37SAntony Pavlov #include "hw/block/flash.h"
3304234a37SAntony Pavlov #include "hw/loader.h"
3404234a37SAntony Pavlov #include "sysemu/qtest.h"
354daf95d6SIgor Mammedov #include "qemu/units.h"
364daf95d6SIgor Mammedov #include "qemu/cutils.h"
3704234a37SAntony Pavlov
3804234a37SAntony Pavlov #define DIGIC4_ROM0_BASE 0xf0000000
3904234a37SAntony Pavlov #define DIGIC4_ROM1_BASE 0xf8000000
4004234a37SAntony Pavlov #define DIGIC4_ROM_MAX_SIZE 0x08000000
41d91fd756SAntony Pavlov
42d91fd756SAntony Pavlov typedef struct DigicBoard {
434daf95d6SIgor Mammedov void (*add_rom0)(DigicState *, hwaddr, const char *);
4404234a37SAntony Pavlov const char *rom0_def_filename;
454daf95d6SIgor Mammedov void (*add_rom1)(DigicState *, hwaddr, const char *);
4604234a37SAntony Pavlov const char *rom1_def_filename;
47d91fd756SAntony Pavlov } DigicBoard;
48d91fd756SAntony Pavlov
digic4_board_init(MachineState * machine,DigicBoard * board)494daf95d6SIgor Mammedov static void digic4_board_init(MachineState *machine, DigicBoard *board)
50d91fd756SAntony Pavlov {
51d91fd756SAntony Pavlov Error *err = NULL;
524daf95d6SIgor Mammedov DigicState *s = DIGIC(object_new(TYPE_DIGIC));
534daf95d6SIgor Mammedov MachineClass *mc = MACHINE_GET_CLASS(machine);
54d91fd756SAntony Pavlov
554daf95d6SIgor Mammedov if (machine->ram_size != mc->default_ram_size) {
564daf95d6SIgor Mammedov char *sz = size_to_str(mc->default_ram_size);
574daf95d6SIgor Mammedov error_report("Invalid RAM size, should be %s", sz);
584daf95d6SIgor Mammedov g_free(sz);
594daf95d6SIgor Mammedov exit(EXIT_FAILURE);
604daf95d6SIgor Mammedov }
61d91fd756SAntony Pavlov
62118bfd76SMarkus Armbruster if (!qdev_realize(DEVICE(s), NULL, &err)) {
63c29b77f9SMarkus Armbruster error_reportf_err(err, "Couldn't realize DIGIC SoC: ");
64d91fd756SAntony Pavlov exit(1);
65d91fd756SAntony Pavlov }
66d91fd756SAntony Pavlov
674daf95d6SIgor Mammedov memory_region_add_subregion(get_system_memory(), 0, machine->ram);
6804234a37SAntony Pavlov
6904234a37SAntony Pavlov if (board->add_rom0) {
7043e61243SPaolo Bonzini board->add_rom0(s, DIGIC4_ROM0_BASE,
7143e61243SPaolo Bonzini machine->firmware ?: board->rom0_def_filename);
7204234a37SAntony Pavlov }
7304234a37SAntony Pavlov
7404234a37SAntony Pavlov if (board->add_rom1) {
7543e61243SPaolo Bonzini board->add_rom1(s, DIGIC4_ROM1_BASE,
7643e61243SPaolo Bonzini machine->firmware ?: board->rom1_def_filename);
7704234a37SAntony Pavlov }
7804234a37SAntony Pavlov }
7904234a37SAntony Pavlov
digic_load_rom(DigicState * s,hwaddr addr,hwaddr max_size,const char * filename)804daf95d6SIgor Mammedov static void digic_load_rom(DigicState *s, hwaddr addr,
8143e61243SPaolo Bonzini hwaddr max_size, const char *filename)
8204234a37SAntony Pavlov {
8304234a37SAntony Pavlov target_long rom_size;
8404234a37SAntony Pavlov
8504234a37SAntony Pavlov if (qtest_enabled()) {
8604234a37SAntony Pavlov /* qtest runs no code so don't attempt a ROM load which
8704234a37SAntony Pavlov * could fail and result in a spurious test failure.
8804234a37SAntony Pavlov */
8904234a37SAntony Pavlov return;
9004234a37SAntony Pavlov }
9104234a37SAntony Pavlov
9204234a37SAntony Pavlov if (filename) {
9304234a37SAntony Pavlov char *fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, filename);
9404234a37SAntony Pavlov
9504234a37SAntony Pavlov if (!fn) {
96d448527aSGonglei error_report("Couldn't find rom image '%s'.", filename);
9704234a37SAntony Pavlov exit(1);
9804234a37SAntony Pavlov }
9904234a37SAntony Pavlov
10004234a37SAntony Pavlov rom_size = load_image_targphys(fn, addr, max_size);
10104234a37SAntony Pavlov if (rom_size < 0 || rom_size > max_size) {
102d448527aSGonglei error_report("Couldn't load rom image '%s'.", filename);
10304234a37SAntony Pavlov exit(1);
10404234a37SAntony Pavlov }
1056e05a12fSGonglei g_free(fn);
10604234a37SAntony Pavlov }
10704234a37SAntony Pavlov }
10804234a37SAntony Pavlov
10904234a37SAntony Pavlov /*
11004234a37SAntony Pavlov * Samsung K8P3215UQB
11104234a37SAntony Pavlov * 64M Bit (4Mx16) Page Mode / Multi-Bank NOR Flash Memory
11204234a37SAntony Pavlov */
digic4_add_k8p3215uqb_rom(DigicState * s,hwaddr addr,const char * filename)1134daf95d6SIgor Mammedov static void digic4_add_k8p3215uqb_rom(DigicState *s, hwaddr addr,
11443e61243SPaolo Bonzini const char *filename)
11504234a37SAntony Pavlov {
11604234a37SAntony Pavlov #define FLASH_K8P3215UQB_SIZE (4 * 1024 * 1024)
11704234a37SAntony Pavlov #define FLASH_K8P3215UQB_SECTOR_SIZE (64 * 1024)
11804234a37SAntony Pavlov
119940d5b13SMarkus Armbruster pflash_cfi02_register(addr, "pflash", FLASH_K8P3215UQB_SIZE,
12004234a37SAntony Pavlov NULL, FLASH_K8P3215UQB_SECTOR_SIZE,
12104234a37SAntony Pavlov DIGIC4_ROM_MAX_SIZE / FLASH_K8P3215UQB_SIZE,
12204234a37SAntony Pavlov 4,
12304234a37SAntony Pavlov 0x00EC, 0x007E, 0x0003, 0x0001,
12404234a37SAntony Pavlov 0x0555, 0x2aa, 0);
12504234a37SAntony Pavlov
12643e61243SPaolo Bonzini digic_load_rom(s, addr, FLASH_K8P3215UQB_SIZE, filename);
127d91fd756SAntony Pavlov }
128d91fd756SAntony Pavlov
129d91fd756SAntony Pavlov static DigicBoard digic4_board_canon_a1100 = {
13004234a37SAntony Pavlov .add_rom1 = digic4_add_k8p3215uqb_rom,
13104234a37SAntony Pavlov .rom1_def_filename = "canon-a1100-rom1.bin",
132d91fd756SAntony Pavlov };
133d91fd756SAntony Pavlov
canon_a1100_init(MachineState * machine)1343ef96221SMarcel Apfelbaum static void canon_a1100_init(MachineState *machine)
135d91fd756SAntony Pavlov {
1364daf95d6SIgor Mammedov digic4_board_init(machine, &digic4_board_canon_a1100);
137d91fd756SAntony Pavlov }
138d91fd756SAntony Pavlov
canon_a1100_machine_init(MachineClass * mc)139e264d29dSEduardo Habkost static void canon_a1100_machine_init(MachineClass *mc)
140d91fd756SAntony Pavlov {
141*fd8f71b9SPhilippe Mathieu-Daudé mc->desc = "Canon PowerShot A1100 IS (ARM946)";
142e264d29dSEduardo Habkost mc->init = &canon_a1100_init;
1434672cbd7SPeter Maydell mc->ignore_memory_transaction_failures = true;
1444daf95d6SIgor Mammedov mc->default_ram_size = 64 * MiB;
1454daf95d6SIgor Mammedov mc->default_ram_id = "ram";
146d91fd756SAntony Pavlov }
147d91fd756SAntony Pavlov
148e264d29dSEduardo Habkost DEFINE_MACHINE("canon-a1100", canon_a1100_machine_init)
149