jtag: clean up TAP state name handling
[openocd.git] / src / flash / faux.c
blobc5928ad6d9f32a3ea85c8dac26a6884a1cbb06c4
1 /***************************************************************************
2 * Copyright (C) 2009 Øyvind Harboe *
3 * oyvind.harboe@zylin.com *
4 * *
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. *
9 * *
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. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "flash.h"
25 #include "image.h"
28 static int faux_register_commands(struct command_context_s *cmd_ctx);
29 static int faux_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
30 static int faux_erase(struct flash_bank_s *bank, int first, int last);
31 static int faux_protect(struct flash_bank_s *bank, int set, int first, int last);
32 static int faux_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
33 static int faux_probe(struct flash_bank_s *bank);
34 static int faux_protect_check(struct flash_bank_s *bank);
35 static int faux_info(struct flash_bank_s *bank, char *buf, int buf_size);
37 flash_driver_t faux_flash =
39 .name = "faux",
40 .register_commands = faux_register_commands,
41 .flash_bank_command = faux_flash_bank_command,
42 .erase = faux_erase,
43 .protect = faux_protect,
44 .write = faux_write,
45 .probe = faux_probe,
46 .auto_probe = faux_probe,
47 .erase_check = default_flash_blank_check,
48 .protect_check = faux_protect_check,
49 .info = faux_info
52 typedef struct faux_flash_bank_s
54 struct target_s *target;
55 uint8_t *memory;
56 uint32_t start_address;
57 } faux_flash_bank_t;
59 static const int sectorSize = 0x10000;
62 /* flash bank faux <base> <size> <chip_width> <bus_width> <target#> <driverPath>
64 static int faux_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
66 faux_flash_bank_t *info;
68 if (argc < 6)
70 LOG_WARNING("incomplete flash_bank faux configuration");
71 return ERROR_FLASH_BANK_INVALID;
74 info = malloc(sizeof(faux_flash_bank_t));
75 if (info == NULL)
77 LOG_ERROR("no memory for flash bank info");
78 return ERROR_FAIL;
80 info->memory = malloc(bank->size);
81 if (info == NULL)
83 free(info);
84 LOG_ERROR("no memory for flash bank info");
85 return ERROR_FAIL;
87 bank->driver_priv = info;
89 /* Use 0x10000 as a fixed sector size. */
90 int i = 0;
91 uint32_t offset = 0;
92 bank->num_sectors = bank->size/sectorSize;
93 bank->sectors = malloc(sizeof(flash_sector_t) * bank->num_sectors);
94 for (i = 0; i < bank->num_sectors; i++)
96 bank->sectors[i].offset = offset;
97 bank->sectors[i].size = sectorSize;
98 offset += bank->sectors[i].size;
99 bank->sectors[i].is_erased = -1;
100 bank->sectors[i].is_protected = 0;
103 info->target = get_target(args[5]);
104 if (info->target == NULL)
106 LOG_ERROR("target '%s' not defined", args[5]);
107 free(info->memory);
108 free(info);
109 return ERROR_FAIL;
111 return ERROR_OK;
114 static int faux_register_commands(struct command_context_s *cmd_ctx)
116 return ERROR_OK;
119 static int faux_erase(struct flash_bank_s *bank, int first, int last)
121 faux_flash_bank_t *info = bank->driver_priv;
122 memset(info->memory + first*sectorSize, 0xff, sectorSize*(last-first + 1));
123 return ERROR_OK;
126 static int faux_protect(struct flash_bank_s *bank, int set, int first, int last)
128 LOG_USER("set protection sector %d to %d to %s", first, last, set?"on":"off");
129 return ERROR_OK;
132 static int faux_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
134 faux_flash_bank_t *info = bank->driver_priv;
135 memcpy(info->memory + offset, buffer, count);
136 return ERROR_OK;
139 static int faux_protect_check(struct flash_bank_s *bank)
141 return ERROR_OK;
144 static int faux_info(struct flash_bank_s *bank, char *buf, int buf_size)
146 snprintf(buf, buf_size, "faux flash driver");
147 return ERROR_OK;
150 static int faux_probe(struct flash_bank_s *bank)
152 return ERROR_OK;