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" 284771d756SPaolo Bonzini #include "qemu-common.h" 294771d756SPaolo Bonzini #include "cpu.h" 30d91fd756SAntony Pavlov #include "hw/boards.h" 31d91fd756SAntony Pavlov #include "exec/address-spaces.h" 32d91fd756SAntony Pavlov #include "qemu/error-report.h" 33d91fd756SAntony Pavlov #include "hw/arm/digic.h" 3404234a37SAntony Pavlov #include "hw/block/flash.h" 3504234a37SAntony Pavlov #include "hw/loader.h" 3604234a37SAntony Pavlov #include "sysemu/sysemu.h" 3704234a37SAntony Pavlov #include "sysemu/qtest.h" 384daf95d6SIgor Mammedov #include "qemu/units.h" 394daf95d6SIgor Mammedov #include "qemu/cutils.h" 4004234a37SAntony Pavlov 4104234a37SAntony Pavlov #define DIGIC4_ROM0_BASE 0xf0000000 4204234a37SAntony Pavlov #define DIGIC4_ROM1_BASE 0xf8000000 4304234a37SAntony Pavlov #define DIGIC4_ROM_MAX_SIZE 0x08000000 44d91fd756SAntony Pavlov 45d91fd756SAntony Pavlov typedef struct DigicBoard { 464daf95d6SIgor Mammedov void (*add_rom0)(DigicState *, hwaddr, const char *); 4704234a37SAntony Pavlov const char *rom0_def_filename; 484daf95d6SIgor Mammedov void (*add_rom1)(DigicState *, hwaddr, const char *); 4904234a37SAntony Pavlov const char *rom1_def_filename; 50d91fd756SAntony Pavlov } DigicBoard; 51d91fd756SAntony Pavlov 524daf95d6SIgor Mammedov static void digic4_board_init(MachineState *machine, DigicBoard *board) 53d91fd756SAntony Pavlov { 54d91fd756SAntony Pavlov Error *err = NULL; 554daf95d6SIgor Mammedov DigicState *s = DIGIC(object_new(TYPE_DIGIC)); 564daf95d6SIgor Mammedov MachineClass *mc = MACHINE_GET_CLASS(machine); 57d91fd756SAntony Pavlov 584daf95d6SIgor Mammedov if (machine->ram_size != mc->default_ram_size) { 594daf95d6SIgor Mammedov char *sz = size_to_str(mc->default_ram_size); 604daf95d6SIgor Mammedov error_report("Invalid RAM size, should be %s", sz); 614daf95d6SIgor Mammedov g_free(sz); 624daf95d6SIgor Mammedov exit(EXIT_FAILURE); 634daf95d6SIgor Mammedov } 64d91fd756SAntony Pavlov 65118bfd76SMarkus Armbruster if (!qdev_realize(DEVICE(s), NULL, &err)) { 66c29b77f9SMarkus Armbruster error_reportf_err(err, "Couldn't realize DIGIC SoC: "); 67d91fd756SAntony Pavlov exit(1); 68d91fd756SAntony Pavlov } 69d91fd756SAntony Pavlov 704daf95d6SIgor Mammedov memory_region_add_subregion(get_system_memory(), 0, machine->ram); 7104234a37SAntony Pavlov 7204234a37SAntony Pavlov if (board->add_rom0) { 73*43e61243SPaolo Bonzini board->add_rom0(s, DIGIC4_ROM0_BASE, 74*43e61243SPaolo Bonzini machine->firmware ?: board->rom0_def_filename); 7504234a37SAntony Pavlov } 7604234a37SAntony Pavlov 7704234a37SAntony Pavlov if (board->add_rom1) { 78*43e61243SPaolo Bonzini board->add_rom1(s, DIGIC4_ROM1_BASE, 79*43e61243SPaolo Bonzini machine->firmware ?: board->rom1_def_filename); 8004234a37SAntony Pavlov } 8104234a37SAntony Pavlov } 8204234a37SAntony Pavlov 834daf95d6SIgor Mammedov static void digic_load_rom(DigicState *s, hwaddr addr, 84*43e61243SPaolo Bonzini hwaddr max_size, const char *filename) 8504234a37SAntony Pavlov { 8604234a37SAntony Pavlov target_long rom_size; 8704234a37SAntony Pavlov 8804234a37SAntony Pavlov if (qtest_enabled()) { 8904234a37SAntony Pavlov /* qtest runs no code so don't attempt a ROM load which 9004234a37SAntony Pavlov * could fail and result in a spurious test failure. 9104234a37SAntony Pavlov */ 9204234a37SAntony Pavlov return; 9304234a37SAntony Pavlov } 9404234a37SAntony Pavlov 9504234a37SAntony Pavlov if (filename) { 9604234a37SAntony Pavlov char *fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, filename); 9704234a37SAntony Pavlov 9804234a37SAntony Pavlov if (!fn) { 99d448527aSGonglei error_report("Couldn't find rom image '%s'.", filename); 10004234a37SAntony Pavlov exit(1); 10104234a37SAntony Pavlov } 10204234a37SAntony Pavlov 10304234a37SAntony Pavlov rom_size = load_image_targphys(fn, addr, max_size); 10404234a37SAntony Pavlov if (rom_size < 0 || rom_size > max_size) { 105d448527aSGonglei error_report("Couldn't load rom image '%s'.", filename); 10604234a37SAntony Pavlov exit(1); 10704234a37SAntony Pavlov } 1086e05a12fSGonglei g_free(fn); 10904234a37SAntony Pavlov } 11004234a37SAntony Pavlov } 11104234a37SAntony Pavlov 11204234a37SAntony Pavlov /* 11304234a37SAntony Pavlov * Samsung K8P3215UQB 11404234a37SAntony Pavlov * 64M Bit (4Mx16) Page Mode / Multi-Bank NOR Flash Memory 11504234a37SAntony Pavlov */ 1164daf95d6SIgor Mammedov static void digic4_add_k8p3215uqb_rom(DigicState *s, hwaddr addr, 117*43e61243SPaolo Bonzini const char *filename) 11804234a37SAntony Pavlov { 11904234a37SAntony Pavlov #define FLASH_K8P3215UQB_SIZE (4 * 1024 * 1024) 12004234a37SAntony Pavlov #define FLASH_K8P3215UQB_SECTOR_SIZE (64 * 1024) 12104234a37SAntony Pavlov 122940d5b13SMarkus Armbruster pflash_cfi02_register(addr, "pflash", FLASH_K8P3215UQB_SIZE, 12304234a37SAntony Pavlov NULL, FLASH_K8P3215UQB_SECTOR_SIZE, 12404234a37SAntony Pavlov DIGIC4_ROM_MAX_SIZE / FLASH_K8P3215UQB_SIZE, 12504234a37SAntony Pavlov 4, 12604234a37SAntony Pavlov 0x00EC, 0x007E, 0x0003, 0x0001, 12704234a37SAntony Pavlov 0x0555, 0x2aa, 0); 12804234a37SAntony Pavlov 129*43e61243SPaolo Bonzini digic_load_rom(s, addr, FLASH_K8P3215UQB_SIZE, filename); 130d91fd756SAntony Pavlov } 131d91fd756SAntony Pavlov 132d91fd756SAntony Pavlov static DigicBoard digic4_board_canon_a1100 = { 13304234a37SAntony Pavlov .add_rom1 = digic4_add_k8p3215uqb_rom, 13404234a37SAntony Pavlov .rom1_def_filename = "canon-a1100-rom1.bin", 135d91fd756SAntony Pavlov }; 136d91fd756SAntony Pavlov 1373ef96221SMarcel Apfelbaum static void canon_a1100_init(MachineState *machine) 138d91fd756SAntony Pavlov { 1394daf95d6SIgor Mammedov digic4_board_init(machine, &digic4_board_canon_a1100); 140d91fd756SAntony Pavlov } 141d91fd756SAntony Pavlov 142e264d29dSEduardo Habkost static void canon_a1100_machine_init(MachineClass *mc) 143d91fd756SAntony Pavlov { 144e264d29dSEduardo Habkost mc->desc = "Canon PowerShot A1100 IS"; 145e264d29dSEduardo Habkost mc->init = &canon_a1100_init; 1464672cbd7SPeter Maydell mc->ignore_memory_transaction_failures = true; 1474daf95d6SIgor Mammedov mc->default_ram_size = 64 * MiB; 1484daf95d6SIgor Mammedov mc->default_ram_id = "ram"; 149d91fd756SAntony Pavlov } 150d91fd756SAntony Pavlov 151e264d29dSEduardo Habkost DEFINE_MACHINE("canon-a1100", canon_a1100_machine_init) 152