flash/nor: make all working area pointers local
[openocd/jflash.git] / src / flash / nor / at91sam7.c
blob95ba084b5e1d41b827ffa490e9c8ecbe0fcc488b
1 /***************************************************************************
2 * Copyright (C) 2006 by Magnus Lundin *
3 * lundin@mlu.mine.nu *
4 * *
5 * Copyright (C) 2008 by Gheorghe Guran (atlas) *
6 * *
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 *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE. See the *
15 * GNU General public License for more details. *
16 * *
17 * You should have received a copy of the GNU General public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ****************************************************************************/
23 /***************************************************************************
25 * New flash setup command:
27 * flash bank <driver> <base_addr> <size> <chip_width> <bus_width> <target_id>
28 * [<chip_type> <banks>
29 * <sectors_per_bank> <pages_per_sector>
30 * <page_size> <num_nvmbits>
31 * <ext_freq_khz>]
33 * <ext_freq_khz> - MUST be used if clock is from external source,
34 * CAN be used if main oscillator frequency is known (recommended)
35 * Examples:
36 * ==== RECOMMENDED (covers clock speed) ============
37 * flash bank at91sam7 0x00100000 0 0 4 $_TARGETNAME AT91SAM7XC256 1 16 64 256 3 25000
38 * (if auto-detect fails; provides clock spec)
39 * flash bank at91sam7 0 0 0 0 $_TARGETNAME 0 0 0 0 0 0 25000
40 * (auto-detect everything except the clock)
41 * ==== NOT RECOMMENDED !!! (clock speed is not configured) ====
42 * flash bank at91sam7 0x00100000 0 0 4 $_TARGETNAME AT91SAM7XC256 1 16 64 256 3 0
43 * (if auto-detect fails)
44 * flash bank at91sam7 0 0 0 0 $_TARGETNAME
45 * (old style, auto-detect everything)
46 ****************************************************************************/
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
52 #include "imp.h"
53 #include <helper/binarybuffer.h>
55 /* AT91SAM7 control registers */
56 #define DBGU_CIDR 0xFFFFF240
57 #define CKGR_MCFR 0xFFFFFC24
58 #define CKGR_MOR 0xFFFFFC20
59 #define CKGR_MCFR_MAINRDY 0x10000
60 #define CKGR_PLLR 0xFFFFFC2c
61 #define CKGR_PLLR_DIV 0xff
62 #define CKGR_PLLR_MUL 0x07ff0000
63 #define PMC_MCKR 0xFFFFFC30
64 #define PMC_MCKR_CSS 0x03
65 #define PMC_MCKR_PRES 0x1c
67 /* Flash Controller Commands */
68 #define WP 0x01
69 #define SLB 0x02
70 #define WPL 0x03
71 #define CLB 0x04
72 #define EA 0x08
73 #define SGPB 0x0B
74 #define CGPB 0x0D
75 #define SSB 0x0F
77 /* MC_FSR bit definitions */
78 #define MC_FSR_FRDY 1
79 #define MC_FSR_EOL 2
81 /* AT91SAM7 constants */
82 #define RC_FREQ 32000
84 /* Flash timing modes */
85 #define FMR_TIMING_NONE 0
86 #define FMR_TIMING_NVBITS 1
87 #define FMR_TIMING_FLASH 2
89 /* Flash size constants */
90 #define FLASH_SIZE_8KB 1
91 #define FLASH_SIZE_16KB 2
92 #define FLASH_SIZE_32KB 3
93 #define FLASH_SIZE_64KB 5
94 #define FLASH_SIZE_128KB 7
95 #define FLASH_SIZE_256KB 9
96 #define FLASH_SIZE_512KB 10
97 #define FLASH_SIZE_1024KB 12
98 #define FLASH_SIZE_2048KB 14
100 static int at91sam7_protect_check(struct flash_bank *bank);
101 static int at91sam7_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset,
102 uint32_t count);
104 static uint32_t at91sam7_get_flash_status(struct target *target, int bank_number);
105 static void at91sam7_set_flash_mode(struct flash_bank *bank, int mode);
106 static uint32_t at91sam7_wait_status_busy(struct flash_bank *bank, uint32_t waitbits, int timeout);
107 static int at91sam7_flash_command(struct flash_bank *bank, uint8_t cmd, uint16_t pagen);
109 static uint32_t MC_FMR[4] = { 0xFFFFFF60, 0xFFFFFF70, 0xFFFFFF80, 0xFFFFFF90 };
110 static uint32_t MC_FCR[4] = { 0xFFFFFF64, 0xFFFFFF74, 0xFFFFFF84, 0xFFFFFF94 };
111 static uint32_t MC_FSR[4] = { 0xFFFFFF68, 0xFFFFFF78, 0xFFFFFF88, 0xFFFFFF98 };
113 static char *EPROC[8] = {
114 "Unknown", "ARM946-E", "ARM7TDMI", "Unknown", "ARM920T", "ARM926EJ-S", "Unknown", "Unknown"
117 struct at91sam7_flash_bank {
118 /* chip id register */
119 uint32_t cidr;
120 uint16_t cidr_ext;
121 uint16_t cidr_nvptyp;
122 uint16_t cidr_arch;
123 uint16_t cidr_sramsiz;
124 uint16_t cidr_nvpsiz;
125 uint16_t cidr_nvpsiz2;
126 uint16_t cidr_eproc;
127 uint16_t cidr_version;
128 const char *target_name;
130 /* flash auto-detection */
131 uint8_t flash_autodetection;
133 /* flash geometry */
134 uint16_t pages_per_sector;
135 uint16_t pagesize;
136 uint16_t pages_in_lockregion;
138 /* nv memory bits */
139 uint16_t num_lockbits_on;
140 uint16_t lockbits;
141 uint16_t num_nvmbits;
142 uint16_t num_nvmbits_on;
143 uint16_t nvmbits;
144 uint8_t securitybit;
146 /* 0: not init
147 * 1: fmcn for nvbits (1uS)
148 * 2: fmcn for flash (1.5uS) */
149 uint8_t flashmode;
151 /* main clock status */
152 uint8_t mck_valid;
153 uint32_t mck_freq;
155 /* external clock frequency */
156 uint32_t ext_freq;
160 #if 0
161 static long SRAMSIZ[16] = {
163 0x0400, /* 1K */
164 0x0800, /* 2K */
166 0x1c000, /* 112K */
167 0x1000, /* 4K */
168 0x14000, /* 80K */
169 0x28000, /* 160K */
170 0x2000, /* 8K */
171 0x4000, /* 16K */
172 0x8000, /* 32K */
173 0x10000, /* 64K */
174 0x20000, /* 128K */
175 0x40000, /* 256K */
176 0x18000, /* 96K */
177 0x80000, /* 512K */
179 #endif
181 static uint32_t at91sam7_get_flash_status(struct target *target, int bank_number)
183 uint32_t fsr;
184 target_read_u32(target, MC_FSR[bank_number], &fsr);
186 return fsr;
189 /* Read clock configuration and set at91sam7_info->mck_freq */
190 static void at91sam7_read_clock_info(struct flash_bank *bank)
192 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
193 struct target *target = bank->target;
194 uint32_t mckr, mcfr, pllr, mor;
195 unsigned long tmp = 0, mainfreq;
197 /* Read Clock Generator Main Oscillator Register */
198 target_read_u32(target, CKGR_MOR, &mor);
199 /* Read Clock Generator Main Clock Frequency Register */
200 target_read_u32(target, CKGR_MCFR, &mcfr);
201 /* Read Master Clock Register*/
202 target_read_u32(target, PMC_MCKR, &mckr);
203 /* Read Clock Generator PLL Register */
204 target_read_u32(target, CKGR_PLLR, &pllr);
206 at91sam7_info->mck_valid = 0;
207 at91sam7_info->mck_freq = 0;
208 switch (mckr & PMC_MCKR_CSS) {
209 case 0: /* Slow Clock */
210 at91sam7_info->mck_valid = 1;
211 tmp = RC_FREQ;
212 break;
214 case 1: /* Main Clock */
215 if ((mcfr & CKGR_MCFR_MAINRDY) &&
216 (at91sam7_info->ext_freq == 0)) {
217 at91sam7_info->mck_valid = 1;
218 tmp = RC_FREQ / 16ul * (mcfr & 0xffff);
219 } else if (at91sam7_info->ext_freq != 0) {
220 at91sam7_info->mck_valid = 1;
221 tmp = at91sam7_info->ext_freq;
223 break;
225 case 2: /* Reserved */
226 break;
228 case 3: /* PLL Clock */
229 if ((mcfr & CKGR_MCFR_MAINRDY) &&
230 (at91sam7_info->ext_freq == 0)) {
231 target_read_u32(target, CKGR_PLLR, &pllr);
232 if (!(pllr & CKGR_PLLR_DIV))
233 break; /* 0 Hz */
234 at91sam7_info->mck_valid = 1;
235 mainfreq = RC_FREQ / 16ul * (mcfr & 0xffff);
236 /* Integer arithmetic should have sufficient precision
237 * as long as PLL is properly configured. */
238 tmp = mainfreq / (pllr & CKGR_PLLR_DIV)*
239 (((pllr & CKGR_PLLR_MUL) >> 16) + 1);
240 } else if ((at91sam7_info->ext_freq != 0) &&
241 ((pllr&CKGR_PLLR_DIV) != 0)) {
242 at91sam7_info->mck_valid = 1;
243 tmp = at91sam7_info->ext_freq / (pllr&CKGR_PLLR_DIV)*
244 (((pllr & CKGR_PLLR_MUL) >> 16) + 1);
246 break;
249 /* Prescaler adjust */
250 if ((((mckr & PMC_MCKR_PRES) >> 2) == 7) || (tmp == 0)) {
251 at91sam7_info->mck_valid = 0;
252 at91sam7_info->mck_freq = 0;
253 } else if (((mckr & PMC_MCKR_PRES) >> 2) != 0)
254 at91sam7_info->mck_freq = tmp >> ((mckr & PMC_MCKR_PRES) >> 2);
255 else
256 at91sam7_info->mck_freq = tmp;
259 /* Setup the timimg registers for nvbits or normal flash */
260 static void at91sam7_set_flash_mode(struct flash_bank *bank, int mode)
262 uint32_t fmr, fmcn = 0, fws = 0;
263 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
264 struct target *target = bank->target;
266 if (mode && (mode != at91sam7_info->flashmode)) {
267 /* Always round up (ceil) */
268 if (mode == FMR_TIMING_NVBITS) {
269 if (at91sam7_info->cidr_arch == 0x60) {
270 /* AT91SAM7A3 uses master clocks in 100 ns */
271 fmcn = (at91sam7_info->mck_freq/10000000ul) + 1;
272 } else {
273 /* master clocks in 1uS for ARCH 0x7 types */
274 fmcn = (at91sam7_info->mck_freq/1000000ul) + 1;
276 } else if (mode == FMR_TIMING_FLASH) {
277 /* main clocks in 1.5uS */
278 fmcn = (at91sam7_info->mck_freq/1000000ul)+
279 (at91sam7_info->mck_freq/2000000ul) + 1;
282 /* hard overclocking */
283 if (fmcn > 0xFF)
284 fmcn = 0xFF;
286 /* Only allow fmcn = 0 if clock period is > 30 us = 33kHz. */
287 if (at91sam7_info->mck_freq <= 33333ul)
288 fmcn = 0;
289 /* Only allow fws = 0 if clock frequency is < 30 MHz. */
290 if (at91sam7_info->mck_freq > 30000000ul)
291 fws = 1;
293 LOG_DEBUG("fmcn[%i]: %i", bank->bank_number, (int)(fmcn));
294 fmr = fmcn << 16 | fws << 8;
295 target_write_u32(target, MC_FMR[bank->bank_number], fmr);
298 at91sam7_info->flashmode = mode;
301 static uint32_t at91sam7_wait_status_busy(struct flash_bank *bank, uint32_t waitbits, int timeout)
303 uint32_t status;
305 while ((!((status = at91sam7_get_flash_status(bank->target,
306 bank->bank_number)) & waitbits)) && (timeout-- > 0)) {
307 LOG_DEBUG("status[%i]: 0x%" PRIx32 "", (int)bank->bank_number, status);
308 alive_sleep(1);
311 LOG_DEBUG("status[%i]: 0x%" PRIx32 "", bank->bank_number, status);
313 if (status & 0x0C) {
314 LOG_ERROR("status register: 0x%" PRIx32 "", status);
315 if (status & 0x4)
316 LOG_ERROR("Lock Error Bit Detected, Operation Abort");
317 if (status & 0x8)
318 LOG_ERROR("Invalid command and/or bad keyword, Operation Abort");
319 if (status & 0x10)
320 LOG_ERROR("Security Bit Set, Operation Abort");
323 return status;
326 /* Send one command to the AT91SAM flash controller */
327 static int at91sam7_flash_command(struct flash_bank *bank, uint8_t cmd, uint16_t pagen)
329 uint32_t fcr;
330 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
331 struct target *target = bank->target;
333 fcr = (0x5A << 24) | ((pagen&0x3FF) << 8) | cmd;
334 target_write_u32(target, MC_FCR[bank->bank_number], fcr);
335 LOG_DEBUG("Flash command: 0x%" PRIx32 ", flash bank: %i, page number: %u",
336 fcr,
337 bank->bank_number + 1,
338 pagen);
340 if ((at91sam7_info->cidr_arch == 0x60) && ((cmd == SLB) | (cmd == CLB))) {
341 /* Lock bit manipulation on AT91SAM7A3 waits for FC_FSR bit 1, EOL */
342 if (at91sam7_wait_status_busy(bank, MC_FSR_EOL, 10)&0x0C)
343 return ERROR_FLASH_OPERATION_FAILED;
344 return ERROR_OK;
347 if (at91sam7_wait_status_busy(bank, MC_FSR_FRDY, 10)&0x0C)
348 return ERROR_FLASH_OPERATION_FAILED;
350 return ERROR_OK;
353 /* Read device id register, main clock frequency register and fill in driver info structure */
354 static int at91sam7_read_part_info(struct flash_bank *bank)
356 struct at91sam7_flash_bank *at91sam7_info;
357 struct target *target = bank->target;
359 uint16_t bnk, sec;
360 uint16_t arch;
361 uint32_t cidr;
362 uint8_t banks_num = 0;
363 uint16_t num_nvmbits = 0;
364 uint16_t sectors_num = 0;
365 uint16_t pages_per_sector = 0;
366 uint16_t page_size = 0;
367 uint32_t ext_freq;
368 uint32_t bank_size;
369 uint32_t base_address = 0;
370 char *target_name_t = "Unknown";
372 at91sam7_info = bank->driver_priv;
374 if (at91sam7_info->cidr != 0) {
375 /* flash already configured, update clock and check for protected sectors */
376 struct flash_bank *fb = bank;
377 struct flash_bank *t_bank = bank;
379 while (t_bank) {
380 /* re-calculate master clock frequency */
381 at91sam7_read_clock_info(t_bank);
383 /* no timming */
384 at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
386 /* check protect state */
387 at91sam7_protect_check(t_bank);
389 t_bank = fb->next;
390 fb = t_bank;
393 return ERROR_OK;
396 /* Read and parse chip identification register */
397 target_read_u32(target, DBGU_CIDR, &cidr);
398 if (cidr == 0) {
399 LOG_WARNING("Cannot identify target as an AT91SAM");
400 return ERROR_FLASH_OPERATION_FAILED;
403 if (at91sam7_info->flash_autodetection == 0) {
404 /* banks and sectors are already created, based on data from input file */
405 struct flash_bank *fb = bank;
406 struct flash_bank *t_bank = bank;
407 while (t_bank) {
408 at91sam7_info = t_bank->driver_priv;
410 at91sam7_info->cidr = cidr;
411 at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
412 at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
413 at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
414 at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
415 at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
416 at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
417 at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
418 at91sam7_info->cidr_version = cidr&0x001F;
420 /* calculate master clock frequency */
421 at91sam7_read_clock_info(t_bank);
423 /* no timming */
424 at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
426 /* check protect state */
427 at91sam7_protect_check(t_bank);
429 t_bank = fb->next;
430 fb = t_bank;
433 return ERROR_OK;
436 arch = (cidr >> 20)&0x00FF;
438 /* check flash size */
439 switch ((cidr >> 8)&0x000F) {
440 case FLASH_SIZE_8KB:
441 break;
443 case FLASH_SIZE_16KB:
444 banks_num = 1;
445 sectors_num = 8;
446 pages_per_sector = 32;
447 page_size = 64;
448 base_address = 0x00100000;
449 if (arch == 0x70) {
450 num_nvmbits = 2;
451 target_name_t = "AT91SAM7S161/16";
453 break;
455 case FLASH_SIZE_32KB:
456 banks_num = 1;
457 sectors_num = 8;
458 pages_per_sector = 32;
459 page_size = 128;
460 base_address = 0x00100000;
461 if (arch == 0x70) {
462 num_nvmbits = 2;
463 target_name_t = "AT91SAM7S321/32";
465 if (arch == 0x72) {
466 num_nvmbits = 3;
467 target_name_t = "AT91SAM7SE32";
469 break;
471 case FLASH_SIZE_64KB:
472 banks_num = 1;
473 sectors_num = 16;
474 pages_per_sector = 32;
475 page_size = 128;
476 base_address = 0x00100000;
477 if (arch == 0x70) {
478 num_nvmbits = 2;
479 target_name_t = "AT91SAM7S64";
481 break;
483 case FLASH_SIZE_128KB:
484 banks_num = 1;
485 sectors_num = 8;
486 pages_per_sector = 64;
487 page_size = 256;
488 base_address = 0x00100000;
489 if (arch == 0x70) {
490 num_nvmbits = 2;
491 target_name_t = "AT91SAM7S128";
493 if (arch == 0x71) {
494 num_nvmbits = 3;
495 target_name_t = "AT91SAM7XC128";
497 if (arch == 0x72) {
498 num_nvmbits = 3;
499 target_name_t = "AT91SAM7SE128";
501 if (arch == 0x75) {
502 num_nvmbits = 3;
503 target_name_t = "AT91SAM7X128";
505 break;
507 case FLASH_SIZE_256KB:
508 banks_num = 1;
509 sectors_num = 16;
510 pages_per_sector = 64;
511 page_size = 256;
512 base_address = 0x00100000;
513 if (arch == 0x60) {
514 num_nvmbits = 3;
515 target_name_t = "AT91SAM7A3";
517 if (arch == 0x70) {
518 num_nvmbits = 2;
519 target_name_t = "AT91SAM7S256";
521 if (arch == 0x71) {
522 num_nvmbits = 3;
523 target_name_t = "AT91SAM7XC256";
525 if (arch == 0x72) {
526 num_nvmbits = 3;
527 target_name_t = "AT91SAM7SE256";
529 if (arch == 0x75) {
530 num_nvmbits = 3;
531 target_name_t = "AT91SAM7X256";
533 break;
535 case FLASH_SIZE_512KB:
536 banks_num = 2;
537 sectors_num = 16;
538 pages_per_sector = 64;
539 page_size = 256;
540 base_address = 0x00100000;
541 if (arch == 0x70) {
542 num_nvmbits = 2;
543 target_name_t = "AT91SAM7S512";
545 if (arch == 0x71) {
546 num_nvmbits = 3;
547 target_name_t = "AT91SAM7XC512";
549 if (arch == 0x72) {
550 num_nvmbits = 3;
551 target_name_t = "AT91SAM7SE512";
553 if (arch == 0x75) {
554 num_nvmbits = 3;
555 target_name_t = "AT91SAM7X512";
557 break;
559 case FLASH_SIZE_1024KB:
560 break;
562 case FLASH_SIZE_2048KB:
563 break;
566 if (strcmp(target_name_t, "Unknown") == 0) {
567 LOG_ERROR(
568 "Target autodetection failed! Please specify target parameters in configuration file");
569 return ERROR_FLASH_OPERATION_FAILED;
572 ext_freq = at91sam7_info->ext_freq;
574 /* calculate bank size */
575 bank_size = sectors_num * pages_per_sector * page_size;
577 for (bnk = 0; bnk < banks_num; bnk++) {
578 struct flash_bank *t_bank = bank;
579 if (bnk > 0) {
580 if (!t_bank->next) {
581 /* create a new flash bank element */
582 struct flash_bank *fb = malloc(sizeof(struct flash_bank));
583 fb->target = target;
584 fb->driver = bank->driver;
585 fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
586 fb->name = "sam7_probed";
587 fb->next = NULL;
589 /* link created bank in 'flash_banks' list */
590 t_bank->next = fb;
592 t_bank = t_bank->next;
595 t_bank->bank_number = bnk;
596 t_bank->base = base_address + bnk * bank_size;
597 t_bank->size = bank_size;
598 t_bank->chip_width = 0;
599 t_bank->bus_width = 4;
600 t_bank->num_sectors = sectors_num;
602 /* allocate sectors */
603 t_bank->sectors = malloc(sectors_num * sizeof(struct flash_sector));
604 for (sec = 0; sec < sectors_num; sec++) {
605 t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
606 t_bank->sectors[sec].size = pages_per_sector * page_size;
607 t_bank->sectors[sec].is_erased = -1;
608 t_bank->sectors[sec].is_protected = -1;
611 at91sam7_info = t_bank->driver_priv;
613 at91sam7_info->cidr = cidr;
614 at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
615 at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
616 at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
617 at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
618 at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
619 at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
620 at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
621 at91sam7_info->cidr_version = cidr&0x001F;
623 at91sam7_info->target_name = target_name_t;
624 at91sam7_info->flashmode = 0;
625 at91sam7_info->ext_freq = ext_freq;
626 at91sam7_info->num_nvmbits = num_nvmbits;
627 at91sam7_info->num_nvmbits_on = 0;
628 at91sam7_info->pagesize = page_size;
629 at91sam7_info->pages_per_sector = pages_per_sector;
631 /* calculate master clock frequency */
632 at91sam7_read_clock_info(t_bank);
634 /* no timming */
635 at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
637 /* check protect state */
638 at91sam7_protect_check(t_bank);
641 LOG_DEBUG("nvptyp: 0x%3.3x, arch: 0x%4.4x",
642 at91sam7_info->cidr_nvptyp,
643 at91sam7_info->cidr_arch);
645 return ERROR_OK;
648 static int at91sam7_erase_check(struct flash_bank *bank)
650 struct target *target = bank->target;
651 uint16_t retval;
652 uint32_t blank;
653 uint16_t fast_check;
654 uint8_t *buffer;
655 uint16_t nSector;
656 uint16_t nByte;
658 if (bank->target->state != TARGET_HALTED) {
659 LOG_ERROR("Target not halted");
660 return ERROR_TARGET_NOT_HALTED;
663 /* Configure the flash controller timing */
664 at91sam7_read_clock_info(bank);
665 at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
667 fast_check = 1;
668 for (nSector = 0; nSector < bank->num_sectors; nSector++) {
669 retval = target_blank_check_memory(target,
670 bank->base + bank->sectors[nSector].offset,
671 bank->sectors[nSector].size,
672 &blank);
673 if (retval != ERROR_OK) {
674 fast_check = 0;
675 break;
677 if (blank == 0xFF)
678 bank->sectors[nSector].is_erased = 1;
679 else
680 bank->sectors[nSector].is_erased = 0;
683 if (fast_check)
684 return ERROR_OK;
686 LOG_USER("Running slow fallback erase check - add working memory");
688 buffer = malloc(bank->sectors[0].size);
689 for (nSector = 0; nSector < bank->num_sectors; nSector++) {
690 bank->sectors[nSector].is_erased = 1;
691 retval = target_read_memory(target, bank->base + bank->sectors[nSector].offset, 4,
692 bank->sectors[nSector].size/4, buffer);
693 if (retval != ERROR_OK)
694 return retval;
696 for (nByte = 0; nByte < bank->sectors[nSector].size; nByte++) {
697 if (buffer[nByte] != 0xFF) {
698 bank->sectors[nSector].is_erased = 0;
699 break;
703 free(buffer);
705 return ERROR_OK;
708 static int at91sam7_protect_check(struct flash_bank *bank)
710 uint8_t lock_pos, gpnvm_pos;
711 uint32_t status;
713 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
715 if (at91sam7_info->cidr == 0)
716 return ERROR_FLASH_BANK_NOT_PROBED;
717 if (bank->target->state != TARGET_HALTED) {
718 LOG_ERROR("Target not halted");
719 return ERROR_TARGET_NOT_HALTED;
722 status = at91sam7_get_flash_status(bank->target, bank->bank_number);
723 at91sam7_info->lockbits = (status >> 16);
725 at91sam7_info->num_lockbits_on = 0;
726 for (lock_pos = 0; lock_pos < bank->num_sectors; lock_pos++) {
727 if (((status >> (16 + lock_pos))&(0x0001)) == 1) {
728 at91sam7_info->num_lockbits_on++;
729 bank->sectors[lock_pos].is_protected = 1;
730 } else
731 bank->sectors[lock_pos].is_protected = 0;
734 /* GPNVM and SECURITY bits apply only for MC_FSR of EFC0 */
735 status = at91sam7_get_flash_status(bank->target, 0);
737 at91sam7_info->securitybit = (status >> 4)&0x01;
738 at91sam7_info->nvmbits = (status >> 8)&0xFF;
740 at91sam7_info->num_nvmbits_on = 0;
741 for (gpnvm_pos = 0; gpnvm_pos < at91sam7_info->num_nvmbits; gpnvm_pos++) {
742 if (((status >> (8 + gpnvm_pos))&(0x01)) == 1)
743 at91sam7_info->num_nvmbits_on++;
746 return ERROR_OK;
749 FLASH_BANK_COMMAND_HANDLER(at91sam7_flash_bank_command)
751 struct flash_bank *t_bank = bank;
752 struct at91sam7_flash_bank *at91sam7_info;
753 struct target *target = t_bank->target;
755 uint32_t base_address;
756 uint32_t bank_size;
757 uint32_t ext_freq = 0;
759 int chip_width;
760 int bus_width;
761 int banks_num;
762 int num_sectors;
764 uint16_t pages_per_sector;
765 uint16_t page_size;
766 uint16_t num_nvmbits;
768 char *target_name_t;
770 int bnk, sec;
772 at91sam7_info = malloc(sizeof(struct at91sam7_flash_bank));
773 t_bank->driver_priv = at91sam7_info;
775 /* part wasn't probed for info yet */
776 at91sam7_info->cidr = 0;
777 at91sam7_info->flashmode = 0;
778 at91sam7_info->ext_freq = 0;
779 at91sam7_info->flash_autodetection = 0;
781 if (CMD_ARGC < 13) {
782 at91sam7_info->flash_autodetection = 1;
783 return ERROR_OK;
786 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], base_address);
788 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], chip_width);
789 COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], bus_width);
791 COMMAND_PARSE_NUMBER(int, CMD_ARGV[8], banks_num);
792 COMMAND_PARSE_NUMBER(int, CMD_ARGV[9], num_sectors);
793 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[10], pages_per_sector);
794 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[11], page_size);
795 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[12], num_nvmbits);
797 if (CMD_ARGC == 14) {
798 unsigned long freq;
799 COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[13], freq);
800 ext_freq = freq * 1000;
801 at91sam7_info->ext_freq = ext_freq;
804 if ((bus_width == 0) || (banks_num == 0) || (num_sectors == 0) ||
805 (pages_per_sector == 0) || (page_size == 0) || (num_nvmbits == 0)) {
806 at91sam7_info->flash_autodetection = 1;
807 return ERROR_OK;
810 target_name_t = calloc(strlen(CMD_ARGV[7]) + 1, sizeof(char));
811 strcpy(target_name_t, CMD_ARGV[7]);
813 /* calculate bank size */
814 bank_size = num_sectors * pages_per_sector * page_size;
816 for (bnk = 0; bnk < banks_num; bnk++) {
817 if (bnk > 0) {
818 if (!t_bank->next) {
819 /* create a new bank element */
820 struct flash_bank *fb = malloc(sizeof(struct flash_bank));
821 fb->target = target;
822 fb->driver = bank->driver;
823 fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
824 fb->name = "sam7_probed";
825 fb->next = NULL;
827 /* link created bank in 'flash_banks' list */
828 t_bank->next = fb;
830 t_bank = t_bank->next;
833 t_bank->bank_number = bnk;
834 t_bank->base = base_address + bnk * bank_size;
835 t_bank->size = bank_size;
836 t_bank->chip_width = chip_width;
837 t_bank->bus_width = bus_width;
838 t_bank->num_sectors = num_sectors;
840 /* allocate sectors */
841 t_bank->sectors = malloc(num_sectors * sizeof(struct flash_sector));
842 for (sec = 0; sec < num_sectors; sec++) {
843 t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
844 t_bank->sectors[sec].size = pages_per_sector * page_size;
845 t_bank->sectors[sec].is_erased = -1;
846 t_bank->sectors[sec].is_protected = -1;
849 at91sam7_info = t_bank->driver_priv;
851 at91sam7_info->target_name = target_name_t;
852 at91sam7_info->flashmode = 0;
853 at91sam7_info->ext_freq = ext_freq;
854 at91sam7_info->num_nvmbits = num_nvmbits;
855 at91sam7_info->num_nvmbits_on = 0;
856 at91sam7_info->pagesize = page_size;
857 at91sam7_info->pages_per_sector = pages_per_sector;
860 return ERROR_OK;
863 static int at91sam7_erase(struct flash_bank *bank, int first, int last)
865 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
866 int sec;
867 uint32_t nbytes, pos;
868 uint8_t *buffer;
869 uint8_t erase_all;
871 if (at91sam7_info->cidr == 0)
872 return ERROR_FLASH_BANK_NOT_PROBED;
874 if (bank->target->state != TARGET_HALTED) {
875 LOG_ERROR("Target not halted");
876 return ERROR_TARGET_NOT_HALTED;
879 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
880 return ERROR_FLASH_SECTOR_INVALID;
882 erase_all = 0;
883 if ((first == 0) && (last == (bank->num_sectors-1)))
884 erase_all = 1;
886 /* Configure the flash controller timing */
887 at91sam7_read_clock_info(bank);
888 at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
890 if (erase_all) {
891 if (at91sam7_flash_command(bank, EA, 0) != ERROR_OK)
892 return ERROR_FLASH_OPERATION_FAILED;
893 } else {
894 /* allocate and clean buffer */
895 nbytes = (last - first + 1) * bank->sectors[first].size;
896 buffer = malloc(nbytes * sizeof(uint8_t));
897 for (pos = 0; pos < nbytes; pos++)
898 buffer[pos] = 0xFF;
900 if (at91sam7_write(bank, buffer, bank->sectors[first].offset, nbytes) != ERROR_OK) {
901 free(buffer);
902 return ERROR_FLASH_OPERATION_FAILED;
905 free(buffer);
908 /* mark erased sectors */
909 for (sec = first; sec <= last; sec++)
910 bank->sectors[sec].is_erased = 1;
912 return ERROR_OK;
915 static int at91sam7_protect(struct flash_bank *bank, int set, int first, int last)
917 uint32_t cmd;
918 int sector;
919 uint32_t pagen;
921 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
923 if (at91sam7_info->cidr == 0)
924 return ERROR_FLASH_BANK_NOT_PROBED;
926 if (bank->target->state != TARGET_HALTED) {
927 LOG_ERROR("Target not halted");
928 return ERROR_TARGET_NOT_HALTED;
931 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
932 return ERROR_FLASH_SECTOR_INVALID;
934 /* Configure the flash controller timing */
935 at91sam7_read_clock_info(bank);
936 at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);
938 for (sector = first; sector <= last; sector++) {
939 if (set)
940 cmd = SLB;
941 else
942 cmd = CLB;
944 /* if we lock a page from one sector then entire sector will be locked, also,
945 * if we unlock a page from a locked sector, entire sector will be unlocked */
946 pagen = sector * at91sam7_info->pages_per_sector;
948 if (at91sam7_flash_command(bank, cmd, pagen) != ERROR_OK)
949 return ERROR_FLASH_OPERATION_FAILED;
952 at91sam7_protect_check(bank);
954 return ERROR_OK;
957 static int at91sam7_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
959 int retval;
960 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
961 struct target *target = bank->target;
962 uint32_t dst_min_alignment, wcount, bytes_remaining = count;
963 uint32_t first_page, last_page, pagen, buffer_pos;
965 if (at91sam7_info->cidr == 0)
966 return ERROR_FLASH_BANK_NOT_PROBED;
968 if (bank->target->state != TARGET_HALTED) {
969 LOG_ERROR("Target not halted");
970 return ERROR_TARGET_NOT_HALTED;
973 if (offset + count > bank->size)
974 return ERROR_FLASH_DST_OUT_OF_BANK;
976 dst_min_alignment = at91sam7_info->pagesize;
978 if (offset % dst_min_alignment) {
979 LOG_WARNING("offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32 "",
980 offset,
981 dst_min_alignment);
982 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
985 if (at91sam7_info->cidr_arch == 0)
986 return ERROR_FLASH_BANK_NOT_PROBED;
988 first_page = offset/dst_min_alignment;
989 last_page = DIV_ROUND_UP(offset + count, dst_min_alignment);
991 LOG_DEBUG("first_page: %i, last_page: %i, count %i",
992 (int)first_page,
993 (int)last_page,
994 (int)count);
996 /* Configure the flash controller timing */
997 at91sam7_read_clock_info(bank);
998 at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
1000 for (pagen = first_page; pagen < last_page; pagen++) {
1001 if (bytes_remaining < dst_min_alignment)
1002 count = bytes_remaining;
1003 else
1004 count = dst_min_alignment;
1005 bytes_remaining -= count;
1007 /* Write one block to the PageWriteBuffer */
1008 buffer_pos = (pagen-first_page)*dst_min_alignment;
1009 wcount = DIV_ROUND_UP(count, 4);
1010 retval = target_write_memory(target, bank->base + pagen*dst_min_alignment, 4,
1011 wcount, buffer + buffer_pos);
1012 if (retval != ERROR_OK)
1013 return retval;
1015 /* Send Write Page command to Flash Controller */
1016 if (at91sam7_flash_command(bank, WP, pagen) != ERROR_OK)
1017 return ERROR_FLASH_OPERATION_FAILED;
1018 LOG_DEBUG("Write flash bank:%i page number:%" PRIi32 "", bank->bank_number, pagen);
1021 return ERROR_OK;
1024 static int at91sam7_probe(struct flash_bank *bank)
1026 /* we can't probe on an at91sam7
1027 * if this is an at91sam7, it has the configured flash */
1028 int retval;
1030 if (bank->target->state != TARGET_HALTED) {
1031 LOG_ERROR("Target not halted");
1032 return ERROR_TARGET_NOT_HALTED;
1035 retval = at91sam7_read_part_info(bank);
1036 if (retval != ERROR_OK)
1037 return retval;
1039 return ERROR_OK;
1042 static int get_at91sam7_info(struct flash_bank *bank, char *buf, int buf_size)
1044 int printed;
1045 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
1047 if (at91sam7_info->cidr == 0)
1048 return ERROR_FLASH_BANK_NOT_PROBED;
1050 printed = snprintf(buf, buf_size,
1051 "\n at91sam7 driver information: Chip is %s\n",
1052 at91sam7_info->target_name);
1054 buf += printed;
1055 buf_size -= printed;
1057 printed = snprintf(buf,
1058 buf_size,
1059 " Cidr: 0x%8.8" PRIx32 " | Arch: 0x%4.4x | Eproc: %s | Version: 0x%3.3x | "
1060 "Flashsize: 0x%8.8" PRIx32 "\n",
1061 at91sam7_info->cidr,
1062 at91sam7_info->cidr_arch,
1063 EPROC[at91sam7_info->cidr_eproc],
1064 at91sam7_info->cidr_version,
1065 bank->size);
1067 buf += printed;
1068 buf_size -= printed;
1070 printed = snprintf(buf, buf_size,
1071 " Master clock (estimated): %u KHz | External clock: %u KHz\n",
1072 (unsigned)(at91sam7_info->mck_freq / 1000),
1073 (unsigned)(at91sam7_info->ext_freq / 1000));
1075 buf += printed;
1076 buf_size -= printed;
1078 printed = snprintf(buf,
1079 buf_size,
1080 " Pagesize: %i bytes | Lockbits(%i): %i 0x%4.4x | Pages in lock region: %i\n",
1081 at91sam7_info->pagesize,
1082 bank->num_sectors,
1083 at91sam7_info->num_lockbits_on,
1084 at91sam7_info->lockbits,
1085 at91sam7_info->pages_per_sector*at91sam7_info->num_lockbits_on);
1087 buf += printed;
1088 buf_size -= printed;
1090 snprintf(buf, buf_size,
1091 " Securitybit: %i | Nvmbits(%i): %i 0x%1.1x\n",
1092 at91sam7_info->securitybit, at91sam7_info->num_nvmbits,
1093 at91sam7_info->num_nvmbits_on, at91sam7_info->nvmbits);
1095 return ERROR_OK;
1099 * On AT91SAM7S: When the gpnvm bits are set with
1100 * > at91sam7 gpnvm bitnr set
1101 * the changes are not visible in the flash controller status register MC_FSR
1102 * until the processor has been reset.
1103 * On the Olimex board this requires a power cycle.
1104 * Note that the AT91SAM7S has the following errata (doc6175.pdf sec 14.1.3):
1105 * The maximum number of write/erase cycles for Non volatile Memory bits is 100. this includes
1106 * Lock Bits (LOCKx), General Purpose NVM bits (GPNVMx) and the Security Bit.
1108 COMMAND_HANDLER(at91sam7_handle_gpnvm_command)
1110 struct flash_bank *bank;
1111 int bit;
1112 uint8_t flashcmd;
1113 uint32_t status;
1114 struct at91sam7_flash_bank *at91sam7_info;
1115 int retval;
1117 if (CMD_ARGC != 2)
1118 return ERROR_COMMAND_SYNTAX_ERROR;
1120 bank = get_flash_bank_by_num_noprobe(0);
1121 if (bank == NULL)
1122 return ERROR_FLASH_BANK_INVALID;
1123 if (strcmp(bank->driver->name, "at91sam7")) {
1124 command_print(CMD_CTX, "not an at91sam7 flash bank '%s'", CMD_ARGV[0]);
1125 return ERROR_FLASH_BANK_INVALID;
1127 if (bank->target->state != TARGET_HALTED) {
1128 LOG_ERROR("target has to be halted to perform flash operation");
1129 return ERROR_TARGET_NOT_HALTED;
1132 if (strcmp(CMD_ARGV[1], "set") == 0)
1133 flashcmd = SGPB;
1134 else if (strcmp(CMD_ARGV[1], "clear") == 0)
1135 flashcmd = CGPB;
1136 else
1137 return ERROR_COMMAND_SYNTAX_ERROR;
1139 at91sam7_info = bank->driver_priv;
1140 if (at91sam7_info->cidr == 0) {
1141 retval = at91sam7_read_part_info(bank);
1142 if (retval != ERROR_OK)
1143 return retval;
1146 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], bit);
1147 if ((bit < 0) || (bit >= at91sam7_info->num_nvmbits)) {
1148 command_print(CMD_CTX,
1149 "gpnvm bit '#%s' is out of bounds for target %s",
1150 CMD_ARGV[0],
1151 at91sam7_info->target_name);
1152 return ERROR_OK;
1155 /* Configure the flash controller timing */
1156 at91sam7_read_clock_info(bank);
1157 at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);
1159 if (at91sam7_flash_command(bank, flashcmd, bit) != ERROR_OK)
1160 return ERROR_FLASH_OPERATION_FAILED;
1162 /* GPNVM and SECURITY bits apply only for MC_FSR of EFC0 */
1163 status = at91sam7_get_flash_status(bank->target, 0);
1164 LOG_DEBUG("at91sam7_handle_gpnvm_command: cmd 0x%x, value %d, status 0x%" PRIx32,
1165 flashcmd,
1166 bit,
1167 status);
1169 /* check protect state */
1170 at91sam7_protect_check(bank);
1172 return ERROR_OK;
1175 static const struct command_registration at91sam7_exec_command_handlers[] = {
1177 .name = "gpnvm",
1178 .handler = at91sam7_handle_gpnvm_command,
1179 .mode = COMMAND_EXEC,
1180 .help = "set or clear one General Purpose Non-Volatile Memory "
1181 "(gpnvm) bit",
1182 .usage = "bitnum ('set'|'clear')",
1184 COMMAND_REGISTRATION_DONE
1186 static const struct command_registration at91sam7_command_handlers[] = {
1188 .name = "at91sam7",
1189 .mode = COMMAND_ANY,
1190 .help = "at91sam7 flash command group",
1191 .usage = "",
1192 .chain = at91sam7_exec_command_handlers,
1194 COMMAND_REGISTRATION_DONE
1197 struct flash_driver at91sam7_flash = {
1198 .name = "at91sam7",
1199 .usage = "gpnvm <bit> <set | clear>",
1200 .commands = at91sam7_command_handlers,
1201 .flash_bank_command = at91sam7_flash_bank_command,
1202 .erase = at91sam7_erase,
1203 .protect = at91sam7_protect,
1204 .write = at91sam7_write,
1205 .read = default_flash_read,
1206 .probe = at91sam7_probe,
1207 .auto_probe = at91sam7_probe,
1208 .erase_check = at91sam7_erase_check,
1209 .protect_check = at91sam7_protect_check,
1210 .info = get_at91sam7_info,