45738ed42f88ff94323b9ba0e06f3e448e074d07
[openocd.git] / src / flash / nor / virtual.c
1 /***************************************************************************
2 * Copyright (C) 2010 by Spencer Oliver *
3 * spen@spen-soft.co.uk *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
19 ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "imp.h"
26
27 static struct flash_bank *virtual_get_master_bank(struct flash_bank *bank)
28 {
29 struct flash_bank *master_bank;
30
31 master_bank = get_flash_bank_by_name_noprobe(bank->driver_priv);
32 if (master_bank == NULL)
33 LOG_ERROR("master flash bank '%s' does not exist", (char *)bank->driver_priv);
34
35 return master_bank;
36 }
37
38 static void virtual_update_bank_info(struct flash_bank *bank)
39 {
40 struct flash_bank *master_bank = virtual_get_master_bank(bank);
41
42 if (master_bank == NULL)
43 return;
44
45 /* update the info we do not have */
46 bank->size = master_bank->size;
47 bank->chip_width = master_bank->chip_width;
48 bank->bus_width = master_bank->bus_width;
49 bank->num_sectors = master_bank->num_sectors;
50 bank->sectors = master_bank->sectors;
51 }
52
53 FLASH_BANK_COMMAND_HANDLER(virtual_flash_bank_command)
54 {
55 if (CMD_ARGC < 7)
56 return ERROR_COMMAND_SYNTAX_ERROR;
57
58 /* get the master flash bank */
59 const char *bank_name = CMD_ARGV[6];
60 struct flash_bank *master_bank = get_flash_bank_by_name_noprobe(bank_name);
61
62 if (master_bank == NULL) {
63 LOG_ERROR("master flash bank '%s' does not exist", bank_name);
64 return ERROR_FLASH_OPERATION_FAILED;
65 }
66
67 /* save master bank name - use this to get settings later */
68 bank->driver_priv = strdup(bank_name);
69
70 return ERROR_OK;
71 }
72
73 static int virtual_protect(struct flash_bank *bank, int set, int first, int last)
74 {
75 struct flash_bank *master_bank = virtual_get_master_bank(bank);
76 int retval;
77
78 if (master_bank == NULL)
79 return ERROR_FLASH_OPERATION_FAILED;
80
81 /* call master handler */
82 retval = master_bank->driver->protect(master_bank, set, first, last);
83 if (retval != ERROR_OK)
84 return retval;
85
86 return ERROR_OK;
87 }
88
89 static int virtual_protect_check(struct flash_bank *bank)
90 {
91 struct flash_bank *master_bank = virtual_get_master_bank(bank);
92 int retval;
93
94 if (master_bank == NULL)
95 return ERROR_FLASH_OPERATION_FAILED;
96
97 /* call master handler */
98 retval = master_bank->driver->protect_check(master_bank);
99 if (retval != ERROR_OK)
100 return retval;
101
102 return ERROR_OK;
103 }
104
105 static int virtual_erase(struct flash_bank *bank, int first, int last)
106 {
107 struct flash_bank *master_bank = virtual_get_master_bank(bank);
108 int retval;
109
110 if (master_bank == NULL)
111 return ERROR_FLASH_OPERATION_FAILED;
112
113 /* call master handler */
114 retval = master_bank->driver->erase(master_bank, first, last);
115 if (retval != ERROR_OK)
116 return retval;
117
118 return ERROR_OK;
119 }
120
121 static int virtual_write(struct flash_bank *bank, uint8_t *buffer,
122 uint32_t offset, uint32_t count)
123 {
124 struct flash_bank *master_bank = virtual_get_master_bank(bank);
125 int retval;
126
127 if (master_bank == NULL)
128 return ERROR_FLASH_OPERATION_FAILED;
129
130 /* call master handler */
131 retval = master_bank->driver->write(master_bank, buffer, offset, count);
132 if (retval != ERROR_OK)
133 return retval;
134
135 return ERROR_OK;
136 }
137
138 static int virtual_probe(struct flash_bank *bank)
139 {
140 struct flash_bank *master_bank = virtual_get_master_bank(bank);
141 int retval;
142
143 if (master_bank == NULL)
144 return ERROR_FLASH_OPERATION_FAILED;
145
146 /* call master handler */
147 retval = master_bank->driver->probe(master_bank);
148 if (retval != ERROR_OK)
149 return retval;
150
151 /* update the info we do not have */
152 virtual_update_bank_info(bank);
153
154 return ERROR_OK;
155 }
156
157 static int virtual_auto_probe(struct flash_bank *bank)
158 {
159 struct flash_bank *master_bank = virtual_get_master_bank(bank);
160 int retval;
161
162 if (master_bank == NULL)
163 return ERROR_FLASH_OPERATION_FAILED;
164
165 /* call master handler */
166 retval = master_bank->driver->auto_probe(master_bank);
167 if (retval != ERROR_OK)
168 return retval;
169
170 /* update the info we do not have */
171 virtual_update_bank_info(bank);
172
173 return ERROR_OK;
174 }
175
176 static int virtual_info(struct flash_bank *bank, char *buf, int buf_size)
177 {
178 struct flash_bank *master_bank = virtual_get_master_bank(bank);
179
180 if (master_bank == NULL)
181 return ERROR_FLASH_OPERATION_FAILED;
182
183 snprintf(buf, buf_size, "%s driver for flash bank %s at 0x%8.8" PRIx32 "",
184 bank->driver->name, master_bank->name, master_bank->base);
185
186 return ERROR_OK;
187 }
188
189 static int virtual_blank_check(struct flash_bank *bank)
190 {
191 struct flash_bank *master_bank = virtual_get_master_bank(bank);
192 int retval;
193
194 if (master_bank == NULL)
195 return ERROR_FLASH_OPERATION_FAILED;
196
197 /* call master handler */
198 retval = master_bank->driver->erase_check(master_bank);
199 if (retval != ERROR_OK)
200 return retval;
201
202 return ERROR_OK;
203 }
204
205 static int virtual_flash_read(struct flash_bank *bank,
206 uint8_t *buffer, uint32_t offset, uint32_t count)
207 {
208 struct flash_bank *master_bank = virtual_get_master_bank(bank);
209 int retval;
210
211 if (master_bank == NULL)
212 return ERROR_FLASH_OPERATION_FAILED;
213
214 /* call master handler */
215 retval = master_bank->driver->read(master_bank, buffer, offset, count);
216 if (retval != ERROR_OK)
217 return retval;
218
219 return ERROR_OK;
220 }
221
222 struct flash_driver virtual_flash = {
223 .name = "virtual",
224 .flash_bank_command = virtual_flash_bank_command,
225 .erase = virtual_erase,
226 .protect = virtual_protect,
227 .write = virtual_write,
228 .read = virtual_flash_read,
229 .probe = virtual_probe,
230 .auto_probe = virtual_auto_probe,
231 .erase_check = virtual_blank_check,
232 .protect_check = virtual_protect_check,
233 .info = virtual_info,
234 };