ocl.c 10.7 KB
Newer Older
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
/***************************************************************************
 *   Copyright (C) 2007 by Pavel Chromy                                    *
 *   chromy@asix.cz                                                        *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "ocl.h"
#include "flash.h"
#include "embeddedice.h"


mifi's avatar
mifi committed
29
30
31
32
static int ocl_register_commands(struct command_context_s *cmd_ctx);
static int ocl_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
static int ocl_erase(struct flash_bank_s *bank, int first, int last);
static int ocl_protect(struct flash_bank_s *bank, int set, int first, int last);
33
static int ocl_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
mifi's avatar
mifi committed
34
35
36
37
38
static int ocl_probe(struct flash_bank_s *bank);
static int ocl_erase_check(struct flash_bank_s *bank);
static int ocl_protect_check(struct flash_bank_s *bank);
static int ocl_info(struct flash_bank_s *bank, char *buf, int buf_size);
static int ocl_auto_probe(struct flash_bank_s *bank);
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

flash_driver_t ocl_flash =
{
	.name = "ocl",
	.register_commands = ocl_register_commands,
	.flash_bank_command = ocl_flash_bank_command,
	.erase = ocl_erase,
	.protect = ocl_protect,
	.write = ocl_write,
	.probe = ocl_probe,
	.erase_check = ocl_erase_check,
	.protect_check = ocl_protect_check,
	.info = ocl_info,
	.auto_probe = ocl_auto_probe
};

typedef struct ocl_priv_s
{
	arm_jtag_t *jtag_info;
58
59
	unsigned int buflen;
	unsigned int bufalign;
60
61
} ocl_priv_t;

mifi's avatar
mifi committed
62
static int ocl_register_commands(struct command_context_s *cmd_ctx)
63
64
65
66
{
	return ERROR_OK;
}

mifi's avatar
mifi committed
67
static int ocl_erase_check(struct flash_bank_s *bank)
68
69
70
71
{
	return ERROR_OK;
}

mifi's avatar
mifi committed
72
static int ocl_protect_check(struct flash_bank_s *bank)
73
74
75
76
77
{
	return ERROR_OK;
}

/* flash_bank ocl 0 0 0 0 <target#> */
mifi's avatar
mifi committed
78
static int ocl_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{
	int retval;
	armv4_5_common_t *armv4_5;
	arm7_9_common_t *arm7_9;
	ocl_priv_t *ocl;

	if (argc < 6)
	{
		LOG_WARNING("incomplete flash_bank ocl configuration");
		return ERROR_FLASH_BANK_INVALID;
	}

	if ((retval = arm7_9_get_arch_pointers(bank->target, &armv4_5, &arm7_9)) != ERROR_OK)
		return retval;

	ocl = bank->driver_priv = malloc(sizeof(ocl_priv_t));
	ocl->jtag_info = &arm7_9->jtag_info;
	ocl->buflen = 0;
	ocl->bufalign = 1;

	return ERROR_OK;
}

mifi's avatar
mifi committed
102
static int ocl_erase(struct flash_bank_s *bank, int first, int last)
103
104
105
{
	ocl_priv_t *ocl = bank->driver_priv;
	int retval;
106
	uint32_t dcc_buffer[3];
107
108
109
110

	/* check preconditions */
	if (bank->num_sectors == 0)
		return ERROR_FLASH_BANK_NOT_PROBED;
zwelch's avatar
zwelch committed
111

112
113
114
115
116
	if (bank->target->state != TARGET_RUNNING)
	{
		LOG_ERROR("target has to be running to communicate with the loader");
		return ERROR_TARGET_NOT_RUNNING;
	}
zwelch's avatar
zwelch committed
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
	if ((first == 0) && (last == bank->num_sectors - 1))
	{
		dcc_buffer[0] = OCL_ERASE_ALL;
		if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
			return retval;
	}
	else
	{
		dcc_buffer[0] = OCL_ERASE_BLOCK;
		dcc_buffer[1] = first;
		dcc_buffer[2] = last;
		if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 3) != ERROR_OK))
			return retval;
	}

	/* wait for response, fixed timeout of 1 s */
	if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
	{
		if (retval == ERROR_TARGET_TIMEOUT)
			LOG_ERROR("loader not responding");
		return retval;
	}

	/* receive response */
	if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer+1, 1) != ERROR_OK))
		return retval;

	if (dcc_buffer[1] != OCL_CMD_DONE)
	{
		if (dcc_buffer[0] == OCL_ERASE_ALL)
duane's avatar
duane committed
148
			LOG_ERROR("loader response to OCL_ERASE_ALL 0x%08" PRIx32 "", dcc_buffer[1]);
149
		else
duane's avatar
duane committed
150
			LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08" PRIx32 "", dcc_buffer[1]);
151
152
153
154
155
156
		return ERROR_FLASH_OPERATION_FAILED;
	}

	return ERROR_OK;
}

mifi's avatar
mifi committed
157
static int ocl_protect(struct flash_bank_s *bank, int set, int first, int last)
158
159
160
161
{
	return ERROR_OK;
}

162
static int ocl_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
163
164
165
{
	ocl_priv_t *ocl = bank->driver_priv;
	int retval;
166
167
	uint32_t *dcc_buffer;
	uint32_t *dcc_bufptr;
168
169
	int byteofs;
	int runlen;
170
	uint32_t chksum;
zwelch's avatar
zwelch committed
171

172
173
174
	int i;

	/* check preconditions */
zwelch's avatar
zwelch committed
175
	if (ocl->buflen == 0 || ocl->bufalign == 0)
176
177
178
179
180
181
182
183
184
		return ERROR_FLASH_BANK_NOT_PROBED;

	if (bank->target->state != TARGET_RUNNING)
	{
		LOG_ERROR("target has to be running to communicate with the loader");
		return ERROR_TARGET_NOT_RUNNING;
	}

	/* allocate buffer for max. ocl buffer + overhead */
185
	dcc_buffer = malloc(sizeof(uint32_t)*(ocl->buflen/4+3));
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

	while (count)
	{
		if (count + (offset % ocl->bufalign) > ocl->buflen)
			runlen = ocl->buflen - (offset % ocl->bufalign);
		else
			runlen = count;

		dcc_buffer[0] = OCL_FLASH_BLOCK | runlen;
		dcc_buffer[1] = offset;
		dcc_bufptr = &dcc_buffer[2];

		*dcc_bufptr = 0xffffffff;
		byteofs = (offset % ocl->bufalign) % 4;
		chksum = OCL_CHKS_INIT;

		/* copy data to DCC buffer in proper byte order and properly aligned */
		for (i=0; i<runlen; i++)
		{
			switch (byteofs++)
			{
				case 0:
					*dcc_bufptr &= *(buffer++) | 0xffffff00;
					break;
				case 1:
zwelch's avatar
zwelch committed
211
					*dcc_bufptr &= ((*(buffer++)) << 8) | 0xffff00ff;
212
213
					break;
				case 2:
zwelch's avatar
zwelch committed
214
					*dcc_bufptr &= ((*(buffer++)) << 16) | 0xff00ffff;
215
216
					break;
				case 3:
zwelch's avatar
zwelch committed
217
					*dcc_bufptr &= ((*(buffer++)) << 24) | 0x00ffffff;
218
219
220
221
222
223
224
225
226
227
228
229
					chksum ^= *(dcc_bufptr++);
					*dcc_bufptr = 0xffffffff;
					byteofs = 0;
					break;
			}
		}

		/* add the remaining word to checksum */
		if (byteofs)
			chksum ^= *(dcc_bufptr++);

		*(dcc_bufptr++) = chksum;
zwelch's avatar
zwelch committed
230

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
		/* send the data */
		if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer)) != ERROR_OK)
		{
			free(dcc_buffer);
		  return retval;
		}

		/* wait for response, fixed timeout of 1 s */
		if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
		{
			if (retval == ERROR_TARGET_TIMEOUT)
				LOG_ERROR("loader not responding");
			free(dcc_buffer);
			return retval;
		}

		/* receive response */
		if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
		{
			free(dcc_buffer);
			return retval;
		}

		if (dcc_buffer[0] != OCL_CMD_DONE)
		{
duane's avatar
duane committed
256
			LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08" PRIx32 "", dcc_buffer[0]);
257
258
259
260
261
262
263
264
265
266
267
268
			free(dcc_buffer);
			return ERROR_FLASH_OPERATION_FAILED;
		}

		count -= runlen;
		offset += runlen;
	}

	free(dcc_buffer);
	return ERROR_OK;
}

mifi's avatar
mifi committed
269
static int ocl_probe(struct flash_bank_s *bank)
270
271
272
{
	ocl_priv_t *ocl = bank->driver_priv;
	int retval;
273
	uint32_t dcc_buffer[1];
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
	int sectsize;
	int i;

	/* purge pending data in DCC */
	embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);

	dcc_buffer[0] = OCL_PROBE;
	if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
		return retval;

	/* wait for response, fixed timeout of 1 s */
	if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
	{
		if (retval == ERROR_TARGET_TIMEOUT)
			LOG_ERROR("loader not responding");
		return retval;
	}

	/* receive response */
	if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
		return retval;
zwelch's avatar
zwelch committed
295

296
297
	if (dcc_buffer[0] != OCL_CMD_DONE)
	{
duane's avatar
duane committed
298
		LOG_ERROR("loader response to OCL_PROBE 0x%08" PRIx32 "", dcc_buffer[0]);
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
		return ERROR_FLASH_OPERATION_FAILED;
	}

	/* receive and fill in parameters, detection of loader is important, receive it one by one */
	if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
		|| (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
		return retval;
	bank->base = dcc_buffer[0];

	if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
		|| (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
		return retval;
	bank->size = dcc_buffer[0];

	if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
		|| (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
		return retval;
	bank->num_sectors = dcc_buffer[0];

	if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
		|| (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
		return retval;
	ocl->buflen = dcc_buffer[0] & 0xffff;
	ocl->bufalign = dcc_buffer[0] >> 16;

	bank->sectors = realloc(bank->sectors, sizeof(flash_sector_t)*bank->num_sectors);
	if (bank->num_sectors == 0)
	{
		LOG_ERROR("number of sectors shall be non zero value");
		return ERROR_FLASH_BANK_INVALID;
	}
	if (bank->size % bank->num_sectors) {
		LOG_ERROR("bank size not divisible by number of sectors");
		return ERROR_FLASH_BANK_INVALID;
	}
	sectsize = bank->size / bank->num_sectors;
	for (i=0; i<bank->num_sectors; i++)
	{
		bank->sectors[i].offset = i * sectsize;
		bank->sectors[i].size = sectsize;
		bank->sectors[i].is_erased = -1;
		bank->sectors[i].is_protected = -1;
	}

	if (ocl->bufalign == 0)
		ocl->bufalign = 1;

	if (ocl->buflen == 0)
	{
		LOG_ERROR("buflen shall be non zero value");
		return ERROR_FLASH_BANK_INVALID;
	}

	if ((ocl->bufalign > ocl->buflen) || (ocl->buflen % ocl->bufalign))
	{
		LOG_ERROR("buflen is not multiple of bufalign");
		return ERROR_FLASH_BANK_INVALID;
	}

	if (ocl->buflen % 4)
	{
		LOG_ERROR("buflen shall be divisible by 4");
		return ERROR_FLASH_BANK_INVALID;
	}

	return ERROR_OK;
}

mifi's avatar
mifi committed
367
static int ocl_info(struct flash_bank_s *bank, char *buf, int buf_size)
368
369
370
371
{
	return ERROR_OK;
}

mifi's avatar
mifi committed
372
static int ocl_auto_probe(struct flash_bank_s *bank)
373
374
375
{
	ocl_priv_t *ocl = bank->driver_priv;

zwelch's avatar
zwelch committed
376
	if (ocl->buflen == 0 || ocl->bufalign == 0)
377
378
379
380
		return ERROR_FLASH_BANK_NOT_PROBED;

	return ERROR_OK;
}