92851ed0c34b67c03e43a1857f0b9a3377c4cfb8
[openocd.git] / src / flash / nor / faux.c
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
23
24 #include "imp.h"
25 #include <target/image.h>
26 #include "hello.h"
27
28
29 struct faux_flash_bank
30 {
31 struct target *target;
32 uint8_t *memory;
33 uint32_t start_address;
34 };
35
36 static const int sectorSize = 0x10000;
37
38
39 /* flash bank faux <base> <size> <chip_width> <bus_width> <target#> <driverPath>
40 */
41 FLASH_BANK_COMMAND_HANDLER(faux_flash_bank_command)
42 {
43 struct faux_flash_bank *info;
44
45 if (CMD_ARGC < 6)
46 {
47 LOG_WARNING("incomplete flash_bank faux configuration");
48 return ERROR_FLASH_BANK_INVALID;
49 }
50
51 info = malloc(sizeof(struct faux_flash_bank));
52 if (info == NULL)
53 {
54 LOG_ERROR("no memory for flash bank info");
55 return ERROR_FAIL;
56 }
57 info->memory = malloc(bank->size);
58 if (info == NULL)
59 {
60 free(info);
61 LOG_ERROR("no memory for flash bank info");
62 return ERROR_FAIL;
63 }
64 bank->driver_priv = info;
65
66 /* Use 0x10000 as a fixed sector size. */
67 int i = 0;
68 uint32_t offset = 0;
69 bank->num_sectors = bank->size/sectorSize;
70 bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
71 for (i = 0; i < bank->num_sectors; i++)
72 {
73 bank->sectors[i].offset = offset;
74 bank->sectors[i].size = sectorSize;
75 offset += bank->sectors[i].size;
76 bank->sectors[i].is_erased = -1;
77 bank->sectors[i].is_protected = 0;
78 }
79
80 info->target = get_target(CMD_ARGV[5]);
81 if (info->target == NULL)
82 {
83 LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
84 free(info->memory);
85 free(info);
86 return ERROR_FAIL;
87 }
88 return ERROR_OK;
89 }
90
91 static int faux_erase(struct flash_bank *bank, int first, int last)
92 {
93 struct faux_flash_bank *info = bank->driver_priv;
94 memset(info->memory + first*sectorSize, 0xff, sectorSize*(last-first + 1));
95 return ERROR_OK;
96 }
97
98 static int faux_protect(struct flash_bank *bank, int set, int first, int last)
99 {
100 LOG_USER("set protection sector %d to %d to %s", first, last, set?"on":"off");
101 return ERROR_OK;
102 }
103
104 static int faux_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
105 {
106 struct faux_flash_bank *info = bank->driver_priv;
107 memcpy(info->memory + offset, buffer, count);
108 return ERROR_OK;
109 }
110
111 static int faux_protect_check(struct flash_bank *bank)
112 {
113 return ERROR_OK;
114 }
115
116 static int faux_info(struct flash_bank *bank, char *buf, int buf_size)
117 {
118 snprintf(buf, buf_size, "faux flash driver");
119 return ERROR_OK;
120 }
121
122 static int faux_probe(struct flash_bank *bank)
123 {
124 return ERROR_OK;
125 }
126
127 static const struct command_registration faux_command_handlers[] = {
128 {
129 .name = "faux",
130 .mode = COMMAND_ANY,
131 .help = "faux flash command group",
132 .chain = hello_command_handlers,
133 },
134 COMMAND_REGISTRATION_DONE
135 };
136
137 struct flash_driver faux_flash = {
138 .name = "faux",
139 .commands = faux_command_handlers,
140 .flash_bank_command = faux_flash_bank_command,
141 .erase = faux_erase,
142 .protect = faux_protect,
143 .write = faux_write,
144 .read = default_flash_read,
145 .probe = faux_probe,
146 .auto_probe = faux_probe,
147 .erase_check = default_flash_blank_check,
148 .protect_check = faux_protect_check,
149 .info = faux_info
150 };