at91sam7.c 33 KB
Newer Older
oharboe's avatar
oharboe committed
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2006 by Magnus Lundin                                   *
 *   lundin@mlu.mine.nu                                                    *
 *                                                                         *
5
6
 *   Copyright (C) 2008 by Gheorghe Guran (atlas)                          *
 *                                                                         *
oharboe's avatar
oharboe committed
7
 *   This program is free software; you can redistribute it and/or modify  *
8
 *   it under the terms of the GNU General public License as published by  *
oharboe's avatar
oharboe committed
9
10
11
12
13
 *   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        *
14
15
 *   MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE.  See the         *
 *   GNU General public License for more details.                          *
oharboe's avatar
oharboe committed
16
 *                                                                         *
17
 *   You should have received a copy of the GNU General public License     *
oharboe's avatar
oharboe committed
18
19
20
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21
****************************************************************************/
oharboe's avatar
oharboe committed
22

23
24
25
/***************************************************************************************************************************************************************************************
*
* New flash setup command:
oharboe's avatar
oharboe committed
26
*
27
* flash bank <driver> <base_addr> <size> <chip_width> <bus_width> <target_number> [<target_name> <banks> <sectors_per_bank> <pages_per_sector> <page_size> <num_nvmbits> <ext_freq_khz>]
oharboe's avatar
oharboe committed
28
*
29
30
31
32
33
34
35
36
37
*   <ext_freq_khz> - MUST be used if clock is from external source,
*                    CAN be used if main oscillator frequency is known (recomended)
* Examples:
*  flash bank at91sam7 0x00100000 0 0 4 0 0 AT91SAM7XC256 1 16 64 256 3 25000                   ==== RECOMENDED ============
*  flash bank at91sam7 0 0 0 0 0 0 0 0 0 0 0 0 25000    (auto-detection, except for clock)      ==== RECOMENDED ============
*  flash bank at91sam7 0x00100000 0 0 4 0 0 AT91SAM7XC256 1 16 64 256 3 0                       ==== NOT RECOMENDED !!! ====
*  flash bank at91sam7 0 0 0 0 0            (old style, full auto-detection)                    ==== NOT RECOMENDED !!! ====
****************************************************************************************************************************************************************************************/

oharboe's avatar
oharboe committed
38
39
40
41
42
43
44
45
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "at91sam7.h"
#include "binarybuffer.h"


mifi's avatar
mifi committed
46
47
48
49
static int at91sam7_register_commands(struct command_context_s *cmd_ctx);
static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
static int at91sam7_erase(struct flash_bank_s *bank, int first, int last);
static int at91sam7_protect(struct flash_bank_s *bank, int set, int first, int last);
50
static int at91sam7_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
mifi's avatar
mifi committed
51
52
53
54
55
56
static int at91sam7_probe(struct flash_bank_s *bank);
//static int at91sam7_auto_probe(struct flash_bank_s *bank);
static int at91sam7_erase_check(struct flash_bank_s *bank);
static int at91sam7_protect_check(struct flash_bank_s *bank);
static int at91sam7_info(struct flash_bank_s *bank, char *buf, int buf_size);

57
static uint32_t at91sam7_get_flash_status(target_t *target, int bank_number);
mifi's avatar
mifi committed
58
static void at91sam7_set_flash_mode(flash_bank_t *bank, int mode);
59
static uint32_t at91sam7_wait_status_busy(flash_bank_t *bank, uint32_t waitbits, int timeout);
zwelch's avatar
zwelch committed
60
static int at91sam7_flash_command(struct flash_bank_s *bank, uint8_t cmd, uint16_t pagen); 
mifi's avatar
mifi committed
61
static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
oharboe's avatar
oharboe committed
62
63
64

flash_driver_t at91sam7_flash =
{
oharboe's avatar
oharboe committed
65
	.name = "at91sam7",
66
67
68
69
70
71
72
73
74
75
	.register_commands = at91sam7_register_commands,
	.flash_bank_command = at91sam7_flash_bank_command,
	.erase = at91sam7_erase,
	.protect = at91sam7_protect,
	.write = at91sam7_write,
	.probe = at91sam7_probe,
	.auto_probe = at91sam7_probe,
	.erase_check = at91sam7_erase_check,
	.protect_check = at91sam7_protect_check,
	.info = at91sam7_info
oharboe's avatar
oharboe committed
76
77
};

78
79
80
static uint32_t MC_FMR[4] = { 0xFFFFFF60, 0xFFFFFF70, 0xFFFFFF80, 0xFFFFFF90 };
static uint32_t MC_FCR[4] = { 0xFFFFFF64, 0xFFFFFF74, 0xFFFFFF84, 0xFFFFFF94 };
static uint32_t MC_FSR[4] = { 0xFFFFFF68, 0xFFFFFF78, 0xFFFFFF88, 0xFFFFFF98 };
oharboe's avatar
oharboe committed
81

mifi's avatar
mifi committed
82
static char * EPROC[8]= {"Unknown","ARM946-E","ARM7TDMI","Unknown","ARM920T","ARM926EJ-S","Unknown","Unknown"};
oharboe's avatar
oharboe committed
83

mifi's avatar
mifi committed
84
85
#if 0
static long SRAMSIZ[16] = {
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
	-1,
	0x0400,		/*  1K */
	0x0800,		/*  2K */ 
	-1, 
	0x1c000,	/* 112K */
	0x1000,		/*   4K */
	0x14000,	/*  80K */
	0x28000,	/* 160K */
	0x2000,		/*   8K */
	0x4000,		/*  16K */
	0x8000,		/*  32K */
	0x10000,	/*  64K */
	0x20000,	/* 128K */
	0x40000,	/* 256K */
	0x18000,	/*  96K */
	0x80000,	/* 512K */
oharboe's avatar
oharboe committed
102
};
mifi's avatar
mifi committed
103
#endif
oharboe's avatar
oharboe committed
104

mifi's avatar
mifi committed
105
static int at91sam7_register_commands(struct command_context_s *cmd_ctx)
oharboe's avatar
oharboe committed
106
{
oharboe's avatar
oharboe committed
107
	command_t *at91sam7_cmd = register_command(cmd_ctx, NULL, "at91sam7", NULL, COMMAND_ANY, NULL);
oharboe's avatar
oharboe committed
108

109
	register_command(cmd_ctx, at91sam7_cmd, "gpnvm", at91sam7_handle_gpnvm_command, COMMAND_EXEC,
zwelch's avatar
zwelch committed
110
					"at91sam7 gpnvm <bit> set | clear, set or clear one gpnvm bit");
111
	return ERROR_OK;
oharboe's avatar
oharboe committed
112
113
}

114
static uint32_t at91sam7_get_flash_status(target_t *target, int bank_number)
oharboe's avatar
oharboe committed
115
{
116
	uint32_t fsr;
117
	target_read_u32(target, MC_FSR[bank_number], &fsr);
118

119
	return fsr;
oharboe's avatar
oharboe committed
120
121
}

122
/* Read clock configuration and set at91sam7_info->mck_freq */
mifi's avatar
mifi committed
123
static void at91sam7_read_clock_info(flash_bank_t *bank)
oharboe's avatar
oharboe committed
124
{
125
126
	at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
	target_t *target = bank->target;
127
	uint32_t mckr, mcfr, pllr, mor;
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
	unsigned long tmp = 0, mainfreq;

	/* Read Clock Generator Main Oscillator Register */
	target_read_u32(target, CKGR_MOR, &mor);
	/* Read Clock Generator Main Clock Frequency Register */
	target_read_u32(target, CKGR_MCFR, &mcfr);
	/* Read Master Clock Register*/
	target_read_u32(target, PMC_MCKR, &mckr);
	/* Read Clock Generator PLL Register  */
	target_read_u32(target, CKGR_PLLR, &pllr);
	
	at91sam7_info->mck_valid = 0;
	at91sam7_info->mck_freq = 0;
	switch (mckr & PMC_MCKR_CSS) 
	{
		case 0:			/* Slow Clock */
			at91sam7_info->mck_valid = 1;
			tmp = RC_FREQ;
			break;

		case 1:			/* Main Clock */
			if ((mcfr & CKGR_MCFR_MAINRDY) && 
				(at91sam7_info->ext_freq == 0))
			{
				at91sam7_info->mck_valid = 1;
				tmp = RC_FREQ / 16ul * (mcfr & 0xffff);
			}
			else if (at91sam7_info->ext_freq != 0)
			{
				at91sam7_info->mck_valid = 1;
				tmp = at91sam7_info->ext_freq;
			}
			break;

		case 2:			/* Reserved */
			break;

		case 3:			/* PLL Clock */
			if ((mcfr & CKGR_MCFR_MAINRDY) && 
				(at91sam7_info->ext_freq == 0)) 
			{
				target_read_u32(target, CKGR_PLLR, &pllr);
				if (!(pllr & CKGR_PLLR_DIV))
					break; /* 0 Hz */
				at91sam7_info->mck_valid = 1;
				mainfreq = RC_FREQ / 16ul * (mcfr & 0xffff);
				/* Integer arithmetic should have sufficient precision
				 * as long as PLL is properly configured. */
				tmp = mainfreq / (pllr & CKGR_PLLR_DIV)*
					(((pllr & CKGR_PLLR_MUL) >> 16) + 1);
			}
			else if ((at91sam7_info->ext_freq != 0) &&
				((pllr&CKGR_PLLR_DIV) != 0))
			{
				at91sam7_info->mck_valid = 1;
				tmp = at91sam7_info->ext_freq / (pllr&CKGR_PLLR_DIV)*
					(((pllr & CKGR_PLLR_MUL) >> 16) + 1);
			}
			break;
	}

	/* Prescaler adjust */
	if ( (((mckr & PMC_MCKR_PRES) >> 2) == 7) || (tmp == 0) )
	{
		at91sam7_info->mck_valid = 0;
		at91sam7_info->mck_freq = 0;
	}
	else if (((mckr & PMC_MCKR_PRES) >> 2) != 0)
		at91sam7_info->mck_freq = tmp >> ((mckr & PMC_MCKR_PRES) >> 2);
	else
		at91sam7_info->mck_freq = tmp;
oharboe's avatar
oharboe committed
199
200
201
}

/* Setup the timimg registers for nvbits or normal flash */
mifi's avatar
mifi committed
202
static void at91sam7_set_flash_mode(flash_bank_t *bank, int mode)
oharboe's avatar
oharboe committed
203
{
204
	uint32_t fmr, fmcn = 0, fws = 0;
205
206
207
208
209
210
211
212
213
214
215
	at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
	target_t *target = bank->target;

	if (mode && (mode != at91sam7_info->flashmode))
	{
		/* Always round up (ceil) */
		if (mode == FMR_TIMING_NVBITS)
		{
			if (at91sam7_info->cidr_arch == 0x60)
			{
				/* AT91SAM7A3 uses master clocks in 100 ns */
zwelch's avatar
zwelch committed
216
				fmcn = (at91sam7_info->mck_freq/10000000ul) + 1;
217
218
219
220
			}
			else
			{
				/* master clocks in 1uS for ARCH 0x7 types */
zwelch's avatar
zwelch committed
221
				fmcn = (at91sam7_info->mck_freq/1000000ul) + 1;
222
223
224
225
226
227
			}
		}
		else if (mode == FMR_TIMING_FLASH)
		{
			/* main clocks in 1.5uS */
			fmcn = (at91sam7_info->mck_freq/1000000ul)+
zwelch's avatar
zwelch committed
228
				(at91sam7_info->mck_freq/2000000ul) + 1;
229
230
231
232
233
234
		}

		/* hard overclocking */
		if (fmcn > 0xFF)
			fmcn = 0xFF;

zwelch's avatar
zwelch committed
235
		/* Only allow fmcn = 0 if clock period is > 30 us = 33kHz. */
236
237
		if (at91sam7_info->mck_freq <= 33333ul)
			fmcn = 0;
zwelch's avatar
zwelch committed
238
		/* Only allow fws = 0 if clock frequency is < 30 MHz. */
239
240
241
		if (at91sam7_info->mck_freq > 30000000ul)
			fws = 1;

duane's avatar
duane committed
242
		LOG_DEBUG("fmcn[%i]: %i", bank->bank_number, (int)(fmcn));
243
244
245
246
247
		fmr = fmcn << 16 | fws << 8;
		target_write_u32(target, MC_FMR[bank->bank_number], fmr);
	}

	at91sam7_info->flashmode = mode;
oharboe's avatar
oharboe committed
248
249
}

250
static uint32_t at91sam7_wait_status_busy(flash_bank_t *bank, uint32_t waitbits, int timeout)
oharboe's avatar
oharboe committed
251
{
252
	uint32_t status;
253
254
255

	while ((!((status = at91sam7_get_flash_status(bank->target, bank->bank_number)) & waitbits)) && (timeout-- > 0))
	{
duane's avatar
duane committed
256
		LOG_DEBUG("status[%i]: 0x%" PRIx32 "", (int)bank->bank_number, status);
257
258
259
		alive_sleep(1);
	}

duane's avatar
duane committed
260
	LOG_DEBUG("status[%i]: 0x%" PRIx32 "", bank->bank_number, status);
261
262
263

	if (status & 0x0C)
	{
duane's avatar
duane committed
264
		LOG_ERROR("status register: 0x%" PRIx32 "", status);
265
266
267
268
269
270
271
272
273
		if (status & 0x4)
			LOG_ERROR("Lock Error Bit Detected, Operation Abort");
		if (status & 0x8)
			LOG_ERROR("Invalid command and/or bad keyword, Operation Abort");
		if (status & 0x10)
			LOG_ERROR("Security Bit Set, Operation Abort");
	}

	return status;
oharboe's avatar
oharboe committed
274
275
276
}

/* Send one command to the AT91SAM flash controller */
zwelch's avatar
zwelch committed
277
static int at91sam7_flash_command(struct flash_bank_s *bank, uint8_t cmd, uint16_t pagen)
oharboe's avatar
oharboe committed
278
{
279
	uint32_t fcr;
280
281
282
	at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
	target_t *target = bank->target;

zwelch's avatar
zwelch committed
283
	fcr = (0x5A << 24) | ((pagen&0x3FF) << 8) | cmd; 
284
	target_write_u32(target, MC_FCR[bank->bank_number], fcr);
zwelch's avatar
zwelch committed
285
	LOG_DEBUG("Flash command: 0x%" PRIx32 ", flash bank: %i, page number: %u", fcr, bank->bank_number + 1, pagen);
286

zwelch's avatar
zwelch committed
287
	if ((at91sam7_info->cidr_arch == 0x60) && ((cmd == SLB) | (cmd == CLB)))
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
	{
		/* Lock bit manipulation on AT91SAM7A3 waits for FC_FSR bit 1, EOL */
		if (at91sam7_wait_status_busy(bank, MC_FSR_EOL, 10)&0x0C)
		{
			return ERROR_FLASH_OPERATION_FAILED;
		}
		return ERROR_OK;
	}

	if (at91sam7_wait_status_busy(bank, MC_FSR_FRDY, 10)&0x0C) 
	{
		return ERROR_FLASH_OPERATION_FAILED;
	}

	return ERROR_OK;
oharboe's avatar
oharboe committed
303
304
305
}

/* Read device id register, main clock frequency register and fill in driver info structure */
mifi's avatar
mifi committed
306
static int at91sam7_read_part_info(struct flash_bank_s *bank)
oharboe's avatar
oharboe committed
307
{
308
309
310
311
	flash_bank_t *t_bank = bank;
	at91sam7_flash_bank_t *at91sam7_info;
	target_t *target = t_bank->target;

zwelch's avatar
zwelch committed
312
313
	uint16_t bnk, sec;
	uint16_t arch;
314
	uint32_t cidr;
315
	uint8_t banks_num = 0;
zwelch's avatar
zwelch committed
316
317
318
319
	uint16_t num_nvmbits = 0;
	uint16_t sectors_num = 0;
	uint16_t pages_per_sector = 0;
	uint16_t page_size = 0;
320
321
322
	uint32_t ext_freq;
	uint32_t bank_size;
	uint32_t base_address = 0;
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
367
368
	char *target_name = "Unknown";

	at91sam7_info = t_bank->driver_priv;

	if (at91sam7_info->cidr != 0)
	{
		/* flash already configured, update clock and check for protected sectors */
		flash_bank_t *fb = bank;
		t_bank = fb;

		while (t_bank)
		{
			/* re-calculate master clock frequency */
			at91sam7_read_clock_info(t_bank);

			/* no timming */
			at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);

			/* check protect state */
			at91sam7_protect_check(t_bank);

			t_bank = fb->next;
			fb = t_bank;
		}

		return ERROR_OK;
	}

	/* Read and parse chip identification register */
	target_read_u32(target, DBGU_CIDR, &cidr);
	if (cidr == 0)
	{
		LOG_WARNING("Cannot identify target as an AT91SAM");
		return ERROR_FLASH_OPERATION_FAILED;
	}

	if (at91sam7_info->flash_autodetection == 0)
	{
		/* banks and sectors are already created, based on data from input file */
		flash_bank_t *fb = bank;
		t_bank = fb;
		while (t_bank)
		{
			at91sam7_info = t_bank->driver_priv;

			at91sam7_info->cidr = cidr;
zwelch's avatar
zwelch committed
369
370
371
372
373
374
375
			at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
			at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
			at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
			at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
			at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
			at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
			at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
			at91sam7_info->cidr_version = cidr&0x001F;

			/* calculate master clock frequency */
			at91sam7_read_clock_info(t_bank);

			/* no timming */
			at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);

			/* check protect state */
			at91sam7_protect_check(t_bank);

			t_bank = fb->next;
			fb = t_bank;
		}

		return ERROR_OK;
	}

zwelch's avatar
zwelch committed
394
	arch = (cidr >> 20)&0x00FF;
395
396

	/* check flash size */
zwelch's avatar
zwelch committed
397
	switch ((cidr >> 8)&0x000F)
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
	{
		case FLASH_SIZE_8KB:
			break;

		case FLASH_SIZE_16KB:
			banks_num = 1;
			sectors_num = 8;
			pages_per_sector = 32;
			page_size  = 64;
			base_address = 0x00100000;
			if (arch == 0x70)
			{
				num_nvmbits = 2;
				target_name = "AT91SAM7S161/16";
			}
			break;

		case FLASH_SIZE_32KB:
			banks_num = 1;
			sectors_num = 8;
			pages_per_sector = 32;
			page_size  = 128;
			base_address = 0x00100000;
			if (arch == 0x70)
			{
				num_nvmbits = 2;
				target_name = "AT91SAM7S321/32";
			}
			if (arch == 0x72)
			{
				num_nvmbits = 3;
				target_name = "AT91SAM7SE32";
			}
			break;

		case FLASH_SIZE_64KB:
			banks_num = 1;
			sectors_num = 16;
			pages_per_sector = 32;
			page_size  = 128;
			base_address = 0x00100000;
			if (arch == 0x70)
			{
				num_nvmbits = 2;
				target_name = "AT91SAM7S64";
			}
			break;

		case FLASH_SIZE_128KB:
			banks_num = 1;
			sectors_num = 8;
			pages_per_sector = 64;
			page_size  = 256;
			base_address = 0x00100000;
			if (arch == 0x70)
			{
				num_nvmbits = 2;
				target_name = "AT91SAM7S128";
			}
			if (arch == 0x71)
			{
				num_nvmbits = 3;
				target_name = "AT91SAM7XC128";
			}
			if (arch == 0x72)
			{
				num_nvmbits = 3;
				target_name = "AT91SAM7SE128";
			}
			if (arch == 0x75)
			{
				num_nvmbits = 3;
				target_name = "AT91SAM7X128";
			}
			break;

		case FLASH_SIZE_256KB:
			banks_num = 1;
			sectors_num = 16;
			pages_per_sector = 64;
			page_size  = 256;
			base_address = 0x00100000;
			if (arch == 0x60)
			{
				num_nvmbits = 3;
				target_name = "AT91SAM7A3";
			}
			if (arch == 0x70)
			{
				num_nvmbits = 2;
				target_name = "AT91SAM7S256";
			}
			if (arch == 0x71)
			{
				num_nvmbits = 3;
				target_name = "AT91SAM7XC256";
			}
			if (arch == 0x72)
			{
				num_nvmbits = 3;
				target_name = "AT91SAM7SE256";
			}
			if (arch == 0x75)
			{
				num_nvmbits = 3;
				target_name = "AT91SAM7X256";
			}
			break;

		case FLASH_SIZE_512KB:
			banks_num = 2;
			sectors_num = 16;
			pages_per_sector = 64;
			page_size  = 256;
			base_address = 0x00100000;
			if (arch == 0x70)
			{
				num_nvmbits = 2;
				target_name = "AT91SAM7S512";
			}
			if (arch == 0x71)
			{
				num_nvmbits = 3;
				target_name = "AT91SAM7XC512";
			}
			if (arch == 0x72)
			{
				num_nvmbits = 3;
				target_name = "AT91SAM7SE512";
			}
			if (arch == 0x75)
			{
				num_nvmbits = 3;
				target_name = "AT91SAM7X512";
			}
			break;

		case FLASH_SIZE_1024KB:
			break;

		case FLASH_SIZE_2048KB:
			break;
	}

	if (strcmp(target_name, "Unknown") == 0)
	{
		LOG_ERROR("Target autodetection failed! Please specify target parameters in configuration file");
		return ERROR_FLASH_OPERATION_FAILED;
	}

	ext_freq = at91sam7_info->ext_freq;

	/* calculate bank size  */
	bank_size = sectors_num * pages_per_sector * page_size;

zwelch's avatar
zwelch committed
553
	for (bnk = 0; bnk<banks_num; bnk++)
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
	{
		if (bnk > 0)
		{
			/* create a new flash bank element */
			flash_bank_t *fb = malloc(sizeof(flash_bank_t));
			fb->target = target;
			fb->driver = &at91sam7_flash;
			fb->driver_priv = malloc(sizeof(at91sam7_flash_bank_t));
			fb->next = NULL;

			/* link created bank in 'flash_banks' list and redirect t_bank */
			t_bank->next = fb;
			t_bank = fb;
		}

		t_bank->bank_number = bnk;
		t_bank->base = base_address + bnk * bank_size;
		t_bank->size = bank_size;
		t_bank->chip_width = 0;
		t_bank->bus_width = 4;
		t_bank->num_sectors = sectors_num;

		/* allocate sectors */
		t_bank->sectors = malloc(sectors_num * sizeof(flash_sector_t));
zwelch's avatar
zwelch committed
578
		for (sec = 0; sec<sectors_num; sec++)
579
580
581
582
583
584
585
586
587
588
		{
			t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
			t_bank->sectors[sec].size = pages_per_sector * page_size;
			t_bank->sectors[sec].is_erased = -1;
			t_bank->sectors[sec].is_protected = -1;
		}

		at91sam7_info = t_bank->driver_priv;

		at91sam7_info->cidr = cidr;
zwelch's avatar
zwelch committed
589
590
591
592
593
594
595
		at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
		at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
		at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
		at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
		at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
		at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
		at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
		at91sam7_info->cidr_version = cidr&0x001F;

		at91sam7_info->target_name  = target_name;
		at91sam7_info->flashmode = 0;
		at91sam7_info->ext_freq = ext_freq;
		at91sam7_info->num_nvmbits = num_nvmbits;
		at91sam7_info->num_nvmbits_on = 0;
		at91sam7_info->pagesize = page_size;
		at91sam7_info->pages_per_sector = pages_per_sector;

		/* calculate master clock frequency */
		at91sam7_read_clock_info(t_bank);

		/* no timming */
		at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);

		/* check protect state */
		at91sam7_protect_check(t_bank);
	}

	LOG_DEBUG("nvptyp: 0x%3.3x, arch: 0x%4.4x", at91sam7_info->cidr_nvptyp, at91sam7_info->cidr_arch );

	return ERROR_OK;
oharboe's avatar
oharboe committed
619
620
}

mifi's avatar
mifi committed
621
static int at91sam7_erase_check(struct flash_bank_s *bank)
oharboe's avatar
oharboe committed
622
{
623
	target_t *target = bank->target;
zwelch's avatar
zwelch committed
624
	uint16_t retval;
625
	uint32_t blank;
zwelch's avatar
zwelch committed
626
	uint16_t fast_check;
627
	uint8_t *buffer;
zwelch's avatar
zwelch committed
628
629
	uint16_t nSector;
	uint16_t nByte;
630
631
632
633
634
635
636
637
638
639
640
641

	if (bank->target->state != TARGET_HALTED)
	{
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	/* Configure the flash controller timing */
	at91sam7_read_clock_info(bank); 
	at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);

	fast_check = 1;
zwelch's avatar
zwelch committed
642
	for (nSector = 0; nSector<bank->num_sectors; nSector++)
643
	{
zwelch's avatar
zwelch committed
644
		retval = target_blank_check_memory(target, bank->base + bank->sectors[nSector].offset,
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
			bank->sectors[nSector].size, &blank);
		if (retval != ERROR_OK)
		{
			fast_check = 0;
			break;
		}
		if (blank == 0xFF)
			bank->sectors[nSector].is_erased = 1;
		else
			bank->sectors[nSector].is_erased = 0;
	}

	if (fast_check)
	{
		return ERROR_OK;
	}

	LOG_USER("Running slow fallback erase check - add working memory");

	buffer = malloc(bank->sectors[0].size);
zwelch's avatar
zwelch committed
665
	for (nSector = 0; nSector<bank->num_sectors; nSector++)
666
667
	{
		bank->sectors[nSector].is_erased = 1;
zwelch's avatar
zwelch committed
668
		retval = target_read_memory(target, bank->base + bank->sectors[nSector].offset, 4,
669
670
671
672
			bank->sectors[nSector].size/4, buffer);
		if (retval != ERROR_OK)
			return retval;

zwelch's avatar
zwelch committed
673
		for (nByte = 0; nByte<bank->sectors[nSector].size; nByte++)
674
675
676
677
678
679
680
681
682
683
684
		{
			if (buffer[nByte] != 0xFF)
			{
				bank->sectors[nSector].is_erased = 0;
				break;
			}
		}
	}
	free(buffer);

	return ERROR_OK;
oharboe's avatar
oharboe committed
685
686
}

mifi's avatar
mifi committed
687
static int at91sam7_protect_check(struct flash_bank_s *bank)
oharboe's avatar
oharboe committed
688
{
689
	uint8_t lock_pos, gpnvm_pos;
690
	uint32_t status;
691
692
693
694
695
696
697
698
699
700
701
702
703
704

	at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;

	if (at91sam7_info->cidr == 0)
	{
		return ERROR_FLASH_BANK_NOT_PROBED;
	}
	if (bank->target->state != TARGET_HALTED)
	{
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	status = at91sam7_get_flash_status(bank->target, bank->bank_number);
zwelch's avatar
zwelch committed
705
	at91sam7_info->lockbits = (status >> 16);
706
707

	at91sam7_info->num_lockbits_on = 0;
zwelch's avatar
zwelch committed
708
	for (lock_pos = 0; lock_pos<bank->num_sectors; lock_pos++)
709
	{
zwelch's avatar
zwelch committed
710
		if ( ((status >> (16 + lock_pos))&(0x0001)) == 1)
711
712
713
714
715
716
717
718
719
720
721
		{
			at91sam7_info->num_lockbits_on++;
			bank->sectors[lock_pos].is_protected = 1;
		}
		else
			bank->sectors[lock_pos].is_protected = 0;
	}

	/* GPNVM and SECURITY bits apply only for MC_FSR of EFC0 */
	status = at91sam7_get_flash_status(bank->target, 0);

zwelch's avatar
zwelch committed
722
723
	at91sam7_info->securitybit = (status >> 4)&0x01;
	at91sam7_info->nvmbits = (status >> 8)&0xFF;
724
725

	at91sam7_info->num_nvmbits_on = 0;
zwelch's avatar
zwelch committed
726
	for (gpnvm_pos = 0; gpnvm_pos<at91sam7_info->num_nvmbits; gpnvm_pos++)
727
	{
zwelch's avatar
zwelch committed
728
		if ( ((status >> (8 + gpnvm_pos))&(0x01)) == 1)
729
730
731
732
733
734
		{
			at91sam7_info->num_nvmbits_on++;
		}
	}

	return ERROR_OK;
oharboe's avatar
oharboe committed
735
736
}

737
738
739
740
741
742
743
744
745
746
/***************************************************************************************************************************************************************************************
# flash bank <driver> <base_addr> <size> <chip_width> <bus_width> <target_number> [<target_name> <banks> <sectors_per_bank> <pages_per_sector> <page_size> <num_nvmbits> <ext_freq_khz>]
#   <ext_freq_khz> - MUST be used if clock is from external source
#                    CAN be used if main oscillator frequency is known
# Examples:
#  flash bank at91sam7 0x00100000 0 0 4 0 0 AT91SAM7XC256 1 16 64 256 3 25000                   ==== RECOMENDED ============
#  flash bank at91sam7 0 0 0 0 0 0 0 0 0 0 0 0 25000    (auto-detection, except for clock)      ==== RECOMENDED ============
#  flash bank at91sam7 0x00100000 0 0 4 0 0 AT91SAM7XC256 1 16 64 256 3 0                       ==== NOT RECOMENDED !!! ====
#  flash bank at91sam7 0 0 0 0 0                        (old style, full auto-detection)        ==== NOT RECOMENDED !!! ====
****************************************************************************************************************************************************************************************/
mifi's avatar
mifi committed
747
static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
oharboe's avatar
oharboe committed
748
{
749
750
751
752
	flash_bank_t *t_bank = bank;
	at91sam7_flash_bank_t *at91sam7_info;
	target_t *target = t_bank->target;

753
754
755
	uint32_t base_address;
	uint32_t bank_size;
	uint32_t ext_freq;
756
757
758
759
760
761

	int chip_width;
	int bus_width;
	int banks_num;
	int num_sectors;

zwelch's avatar
zwelch committed
762
763
764
	uint16_t pages_per_sector;
	uint16_t page_size;
	uint16_t num_nvmbits;
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805

	char *target_name;

	int bnk, sec;

	at91sam7_info = malloc(sizeof(at91sam7_flash_bank_t));
	t_bank->driver_priv = at91sam7_info;

	/* part wasn't probed for info yet */
	at91sam7_info->cidr = 0;
	at91sam7_info->flashmode = 0;
	at91sam7_info->ext_freq = 0;
	at91sam7_info->flash_autodetection = 0;

	if (argc == 14)
	{
		ext_freq = atol(args[13]) * 1000;
		at91sam7_info->ext_freq = ext_freq;
	}

	if ((argc != 14)                ||
		(atoi(args[4]) == 0)        ||  /* bus width */
		(atoi(args[8]) == 0)        ||  /* banks number */
		(atoi(args[9]) == 0)        ||  /* sectors per bank */
		(atoi(args[10]) == 0)       ||  /* pages per sector */
		(atoi(args[11]) == 0)       ||  /* page size */
		(atoi(args[12]) == 0))          /* nvmbits number */
	{
		at91sam7_info->flash_autodetection = 1;
		return ERROR_OK;
	}

	base_address = strtoul(args[1], NULL, 0);
	chip_width = atoi(args[3]);
	bus_width = atoi(args[4]);
	banks_num = atoi(args[8]);
	num_sectors = atoi(args[9]);
	pages_per_sector = atoi(args[10]);
	page_size = atoi(args[11]);
	num_nvmbits = atoi(args[12]);

zwelch's avatar
zwelch committed
806
	target_name = calloc(strlen(args[7]) + 1, sizeof(char));
807
808
809
810
811
	strcpy(target_name, args[7]);

	/* calculate bank size  */
	bank_size = num_sectors * pages_per_sector * page_size;

zwelch's avatar
zwelch committed
812
	for (bnk = 0; bnk<banks_num; bnk++)
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
	{
		if (bnk > 0)
		{
			/* create a new bank element */
			flash_bank_t *fb = malloc(sizeof(flash_bank_t));
			fb->target = target;
			fb->driver = &at91sam7_flash;
			fb->driver_priv = malloc(sizeof(at91sam7_flash_bank_t));
			fb->next = NULL;

			/* link created bank in 'flash_banks' list and redirect t_bank */
			t_bank->next = fb;
			t_bank = fb;
		}

		t_bank->bank_number = bnk;
		t_bank->base = base_address + bnk * bank_size;
		t_bank->size = bank_size;
		t_bank->chip_width = chip_width;
		t_bank->bus_width = bus_width;
		t_bank->num_sectors = num_sectors;

		/* allocate sectors */
		t_bank->sectors = malloc(num_sectors * sizeof(flash_sector_t));
zwelch's avatar
zwelch committed
837
		for (sec = 0; sec<num_sectors; sec++)
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
		{
			t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
			t_bank->sectors[sec].size = pages_per_sector * page_size;
			t_bank->sectors[sec].is_erased = -1;
			t_bank->sectors[sec].is_protected = -1;
		}

		at91sam7_info = t_bank->driver_priv;

		at91sam7_info->target_name  = target_name;
		at91sam7_info->flashmode = 0;
		at91sam7_info->ext_freq  = ext_freq;
		at91sam7_info->num_nvmbits = num_nvmbits;
		at91sam7_info->num_nvmbits_on = 0;
		at91sam7_info->pagesize = page_size;
		at91sam7_info->pages_per_sector = pages_per_sector;
	}

	return ERROR_OK;
oharboe's avatar
oharboe committed
857
858
}

mifi's avatar
mifi committed
859
static int at91sam7_erase(struct flash_bank_s *bank, int first, int last)
oharboe's avatar
oharboe committed
860
{
861
862
	at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
	int sec;
863
	uint32_t nbytes, pos;
864
865
	uint8_t *buffer;
	uint8_t erase_all;
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892

	if (at91sam7_info->cidr == 0)
	{
		return ERROR_FLASH_BANK_NOT_PROBED;
	}

	if (bank->target->state != TARGET_HALTED)
	{
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	if ((first < 0) || (last < first) || (last >= bank->num_sectors))
	{
		return ERROR_FLASH_SECTOR_INVALID;
	}

	erase_all = 0;
	if ((first == 0) && (last == (bank->num_sectors-1)))
	{
		erase_all = 1;
	}

	/* Configure the flash controller timing */
	at91sam7_read_clock_info(bank);
	at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);

zwelch's avatar
zwelch committed
893
	if (erase_all)
894
895
896
897
898
899
900
901
902
903
	{
		if (at91sam7_flash_command(bank, EA, 0) != ERROR_OK) 
		{
			return ERROR_FLASH_OPERATION_FAILED;
		}
	}
	else
	{
		/* allocate and clean buffer  */
		nbytes = (last - first + 1) * bank->sectors[first].size;
904
		buffer = malloc(nbytes * sizeof(uint8_t));
zwelch's avatar
zwelch committed
905
		for (pos = 0; pos<nbytes; pos++)
906
907
908
909
910
911
912
913
914
915
916
917
918
		{
			buffer[pos] = 0xFF;
		}

		if ( at91sam7_write(bank, buffer, bank->sectors[first].offset, nbytes) != ERROR_OK)
		{
			return ERROR_FLASH_OPERATION_FAILED;
		}

		free(buffer);
	}

	/* mark erased sectors */
zwelch's avatar
zwelch committed
919
	for (sec = first; sec <= last; sec++)
920
921
922
923
924
	{
		bank->sectors[sec].is_erased = 1;
	}

	return ERROR_OK;
oharboe's avatar
oharboe committed
925
926
}

mifi's avatar
mifi committed
927
static int at91sam7_protect(struct flash_bank_s *bank, int set, int first, int last)
oharboe's avatar
oharboe committed
928
{
929
	uint32_t cmd;
930
	int sector;
931
	uint32_t pagen;
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954

	at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;

	if (at91sam7_info->cidr == 0)
	{
		return ERROR_FLASH_BANK_NOT_PROBED;
	}

	if (bank->target->state != TARGET_HALTED)
	{
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	if ((first < 0) || (last < first) || (last >= bank->num_sectors))
	{
		return ERROR_FLASH_SECTOR_INVALID;
	}

	/* Configure the flash controller timing */
	at91sam7_read_clock_info(bank);
	at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);

zwelch's avatar
zwelch committed
955
	for (sector = first; sector <= last; sector++)
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
	{
		if (set)
			cmd = SLB;
		else
			cmd = CLB;

		/* if we lock a page from one sector then entire sector will be locked, also,
		 * if we unlock a page from a locked sector, entire sector will be unlocked   */
		pagen = sector * at91sam7_info->pages_per_sector;

		if (at91sam7_flash_command(bank, cmd, pagen) != ERROR_OK)
		{
			return ERROR_FLASH_OPERATION_FAILED;
		}
	}

	at91sam7_protect_check(bank);

	return ERROR_OK;
oharboe's avatar
oharboe committed
975
976
}

977
static int at91sam7_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
oharboe's avatar
oharboe committed
978
{
979
	int retval;
980
981
	at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
	target_t *target = bank->target;
982
983
	uint32_t dst_min_alignment, wcount, bytes_remaining = count;
	uint32_t first_page, last_page, pagen, buffer_pos;
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

	if (at91sam7_info->cidr == 0)
	{
		return ERROR_FLASH_BANK_NOT_PROBED;
	}

	if (bank->target->state != TARGET_HALTED)
	{
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	if (offset + count > bank->size)
		return ERROR_FLASH_DST_OUT_OF_BANK;

	dst_min_alignment = at91sam7_info->pagesize;

For faster browsing, not all history is shown. View entire blame