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