aboutsummaryrefslogtreecommitdiff
path: root/src/flash/nor/virtual.c
blob: 45738ed42f88ff94323b9ba0e06f3e448e074d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/***************************************************************************
 *   Copyright (C) 2010 by Spencer Oliver                                  *
 *   spen@spen-soft.co.uk                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "imp.h"

static struct flash_bank *virtual_get_master_bank(struct flash_bank *bank)
{
	struct flash_bank *master_bank;

	master_bank = get_flash_bank_by_name_noprobe(bank->driver_priv);
	if (master_bank == NULL)
		LOG_ERROR("master flash bank '%s' does not exist", (char *)bank->driver_priv);

	return master_bank;
}

static void virtual_update_bank_info(struct flash_bank *bank)
{
	struct flash_bank *master_bank = virtual_get_master_bank(bank);

	if (master_bank == NULL)
		return;

	/* update the info we do not have */
	bank->size = master_bank->size;
	bank->chip_width = master_bank->chip_width;
	bank->bus_width = master_bank->bus_width;
	bank->num_sectors = master_bank->num_sectors;
	bank->sectors = master_bank->sectors;
}

FLASH_BANK_COMMAND_HANDLER(virtual_flash_bank_command)
{
	if (CMD_ARGC < 7)
		return ERROR_COMMAND_SYNTAX_ERROR;

	/* get the master flash bank */
	const char *bank_name = CMD_ARGV[6];
	struct flash_bank *master_bank = get_flash_bank_by_name_noprobe(bank_name);

	if (master_bank == NULL) {
		LOG_ERROR("master flash bank '%s' does not exist", bank_name);
		return ERROR_FLASH_OPERATION_FAILED;
	}

	/* save master bank name - use this to get settings later */
	bank->driver_priv = strdup(bank_name);

	return ERROR_OK;
}

static int virtual_protect(struct flash_bank *bank, int set, int first, int last)
{
	struct flash_bank *master_bank = virtual_get_master_bank(bank);
	int retval;

	if (master_bank == NULL)
		return ERROR_FLASH_OPERATION_FAILED;

	/* call master handler */
	retval = master_bank->driver->protect(master_bank, set, first, last);
	if (retval != ERROR_OK)
		return retval;

	return ERROR_OK;
}

static int virtual_protect_check(struct flash_bank *bank)
{
	struct flash_bank *master_bank = virtual_get_master_bank(bank);
	int retval;

	if (master_bank == NULL)
		return ERROR_FLASH_OPERATION_FAILED;

	/* call master handler */
	retval = master_bank->driver->protect_check(master_bank);
	if (retval != ERROR_OK)
		return retval;

	return ERROR_OK;
}

static int virtual_erase(struct flash_bank *bank, int first, int last)
{
	struct flash_bank *master_bank = virtual_get_master_bank(bank);
	int retval;

	if (master_bank == NULL)
		return ERROR_FLASH_OPERATION_FAILED;

	/* call master handler */
	retval = master_bank->driver->erase(master_bank, first, last);
	if (retval != ERROR_OK)
		return retval;

	return ERROR_OK;
}

static int virtual_write(struct flash_bank *bank, uint8_t *buffer,
		uint32_t offset, uint32_t count)
{
	struct flash_bank *master_bank = virtual_get_master_bank(bank);
	int retval;

	if (master_bank == NULL)
		return ERROR_FLASH_OPERATION_FAILED;

	/* call master handler */
	retval = master_bank->driver->write(master_bank, buffer, offset, count);
	if (retval != ERROR_OK)
		return retval;

	return ERROR_OK;
}

static int virtual_probe(struct flash_bank *bank)
{
	struct flash_bank *master_bank = virtual_get_master_bank(bank);
	int retval;

	if (master_bank == NULL)
		return ERROR_FLASH_OPERATION_FAILED;

	/* call master handler */
	retval = master_bank->driver->probe(master_bank);
	if (retval != ERROR_OK)
		return retval;

	/* update the info we do not have */
	virtual_update_bank_info(bank);

	return ERROR_OK;
}

static int virtual_auto_probe(struct flash_bank *bank)
{
	struct flash_bank *master_bank = virtual_get_master_bank(bank);
	int retval;

	if (master_bank == NULL)
		return ERROR_FLASH_OPERATION_FAILED;

	/* call master handler */
	retval = master_bank->driver->auto_probe(master_bank);
	if (retval != ERROR_OK)
		return retval;

	/* update the info we do not have */
	virtual_update_bank_info(bank);

	return ERROR_OK;
}

static int virtual_info(struct flash_bank *bank, char *buf, int buf_size)
{
	struct flash_bank *master_bank = virtual_get_master_bank(bank);

	if (master_bank == NULL)
		return ERROR_FLASH_OPERATION_FAILED;

	snprintf(buf, buf_size, "%s driver for flash bank %s at 0x%8.8" PRIx32 "",
			bank->driver->name, master_bank->name, master_bank->base);

	return ERROR_OK;
}

static int virtual_blank_check(struct flash_bank *bank)
{
	struct flash_bank *master_bank = virtual_get_master_bank(bank);
	int retval;

	if (master_bank == NULL)
		return ERROR_FLASH_OPERATION_FAILED;

	/* call master handler */
	retval = master_bank->driver->erase_check(master_bank);
	if (retval != ERROR_OK)
		return retval;

	return ERROR_OK;
}

static int virtual_flash_read(struct flash_bank *bank,
		uint8_t *buffer, uint32_t offset, uint32_t count)
{
	struct flash_bank *master_bank = virtual_get_master_bank(bank);
	int retval;

	if (master_bank == NULL)
		return ERROR_FLASH_OPERATION_FAILED;

	/* call master handler */
	retval = master_bank->driver->read(master_bank, buffer, offset, count);
	if (retval != ERROR_OK)
		return retval;

	return ERROR_OK;
}

struct flash_driver virtual_flash = {
	.name = "virtual",
	.flash_bank_command = virtual_flash_bank_command,
	.erase = virtual_erase,
	.protect = virtual_protect,
	.write = virtual_write,
	.read = virtual_flash_read,
	.probe = virtual_probe,
	.auto_probe = virtual_auto_probe,
	.erase_check = virtual_blank_check,
	.protect_check = virtual_protect_check,
	.info = virtual_info,
};