1 /***************************************************************************
2 * Copyright (C) 2009 Øyvind Harboe *
3 * oyvind.harboe@zylin.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
24 #include <target/image.h>
27 struct faux_flash_bank {
28 struct target *target;
30 uint32_t start_address;
33 static const int sector_size = 0x10000;
36 /* flash bank faux <base> <size> <chip_width> <bus_width> <target#> <driverPath>
38 FLASH_BANK_COMMAND_HANDLER(faux_flash_bank_command)
40 struct faux_flash_bank *info;
43 return ERROR_COMMAND_SYNTAX_ERROR;
45 info = malloc(sizeof(struct faux_flash_bank));
47 LOG_ERROR("no memory for flash bank info");
50 info->memory = malloc(bank->size);
53 LOG_ERROR("no memory for flash bank info");
56 bank->driver_priv = info;
58 /* Use 0x10000 as a fixed sector size. */
60 bank->num_sectors = bank->size/sector_size;
61 bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
62 for (unsigned int i = 0; i < bank->num_sectors; i++) {
63 bank->sectors[i].offset = offset;
64 bank->sectors[i].size = sector_size;
65 offset += bank->sectors[i].size;
66 bank->sectors[i].is_erased = -1;
67 bank->sectors[i].is_protected = 0;
70 info->target = get_target(CMD_ARGV[5]);
72 LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
80 static int faux_erase(struct flash_bank *bank, unsigned int first,
83 struct faux_flash_bank *info = bank->driver_priv;
84 memset(info->memory + first*sector_size, 0xff, sector_size*(last-first + 1));
88 static int faux_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
90 struct faux_flash_bank *info = bank->driver_priv;
91 memcpy(info->memory + offset, buffer, count);
95 static int faux_info(struct flash_bank *bank, struct command_invocation *cmd)
97 command_print_sameline(cmd, "faux flash driver");
101 static int faux_probe(struct flash_bank *bank)
106 static const struct command_registration faux_command_handlers[] = {
110 .help = "faux flash command group",
111 .chain = hello_command_handlers,
114 COMMAND_REGISTRATION_DONE
117 const struct flash_driver faux_flash = {
119 .commands = faux_command_handlers,
120 .flash_bank_command = faux_flash_bank_command,
123 .read = default_flash_read,
125 .auto_probe = faux_probe,
126 .erase_check = default_flash_blank_check,
128 .free_driver_priv = default_flash_free_driver_priv,