1 /***************************************************************************
2 * Copyright (C) 2006 by Magnus Lundin *
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. *
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. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 /***************************************************************************
22 There are some things to notice
24 * AT91SAM7S64 is tested
25 * All AT91SAM7Sxx and AT91SAM7Xxx should work but is not tested
26 * All parameters are identified from onchip configuartion registers
28 * The flash controller handles erases automatically on a page (128/265 byte) basis
29 * Only an EraseAll command is supported by the controller
30 * Partial erases can be implemented in software by writing one 0xFFFFFFFF word to
31 * some location in every page in the region to be erased
33 * Lock regions (sectors) are 32 or 64 pages
35 ***************************************************************************/
40 #include "replacements.h"
47 #include "binarybuffer.h"
54 int at91sam7_register_commands(struct command_context_s *cmd_ctx);
55 int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
56 int at91sam7_erase(struct flash_bank_s *bank, int first, int last);
57 int at91sam7_protect(struct flash_bank_s *bank, int set, int first, int last);
58 int at91sam7_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
59 int at91sam7_probe(struct flash_bank_s *bank);
60 int at91sam7_erase_check(struct flash_bank_s *bank);
61 int at91sam7_protect_check(struct flash_bank_s *bank);
62 int at91sam7_info(struct flash_bank_s *bank, char *buf, int buf_size);
64 u32 at91sam7_get_flash_status(flash_bank_t *bank);
65 void at91sam7_set_flash_mode(flash_bank_t *bank,int mode);
66 u32 at91sam7_wait_status_busy(flash_bank_t *bank, u32 waitbits, int timeout);
67 int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 flash_driver_t at91sam7_flash =
72 .register_commands = at91sam7_register_commands,
73 .flash_bank_command = at91sam7_flash_bank_command,
74 .erase = at91sam7_erase,
75 .protect = at91sam7_protect,
76 .write = at91sam7_write,
77 .probe = at91sam7_probe,
78 .erase_check = at91sam7_erase_check,
79 .protect_check = at91sam7_protect_check,
84 char * EPROC[8]= {"Unknown","ARM946-E","ARM7TDMI","Unknown","ARM920T","ARM926EJ-S","Unknown","Unknown"};
100 0x200000, /* 2048K */
123 int at91sam7_register_commands(struct command_context_s *cmd_ctx)
125 command_t *at91sam7_cmd = register_command(cmd_ctx, NULL, "at91sam7", NULL, COMMAND_ANY, NULL);
126 register_command(cmd_ctx, at91sam7_cmd, "gpnvm", at91sam7_handle_gpnvm_command, COMMAND_EXEC,
127 "at91sam7 gpnvm <num> <bit> set|clear, set or clear at91sam7 gpnvm bit");
132 u32 at91sam7_get_flash_status(flash_bank_t *bank)
134 target_t *target = bank->target;
137 target_read_u32(target, MC_FSR, &fsr);
142 /** Read clock configuration and set at91sam7_info->usec_clocks*/
143 void at91sam7_read_clock_info(flash_bank_t *bank)
145 at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
146 target_t *target = bank->target;
147 u32 mckr, mcfr, pllr;
148 unsigned long tmp = 0, mainfreq;
150 /* Read main clock freqency register */
151 target_read_u32(target, CKGR_MCFR, &mcfr);
152 /* Read master clock register */
153 target_read_u32(target, PMC_MCKR, &mckr);
154 /* Read Clock Generator PLL Register */
155 target_read_u32(target, CKGR_PLLR, &pllr);
157 at91sam7_info->mck_valid = 0;
158 switch (mckr & PMC_MCKR_CSS) {
159 case 0: /* Slow Clock */
160 at91sam7_info->mck_valid = 1;
163 case 1: /* Main Clock */
164 if (mcfr & CKGR_MCFR_MAINRDY)
166 at91sam7_info->mck_valid = 1;
167 mainfreq = RC_FREQ / 16ul * (mcfr & 0xffff);
172 case 2: /* Reserved */
174 case 3: /* PLL Clock */
175 if (mcfr & CKGR_MCFR_MAINRDY)
177 target_read_u32(target, CKGR_PLLR, &pllr);
178 if (!(pllr & CKGR_PLLR_DIV))
180 at91sam7_info->mck_valid = 1;
181 mainfreq = RC_FREQ / 16ul * (mcfr & 0xffff);
182 /* Integer arithmetic should have sufficient precision
183 as long as PLL is properly configured. */
184 tmp = mainfreq / (pllr & CKGR_PLLR_DIV) *
185 (((pllr & CKGR_PLLR_MUL) >> 16) + 1);
190 /* Prescaler adjust */
191 if (((mckr & PMC_MCKR_PRES) >> 2) == 7)
192 at91sam7_info->mck_valid = 0;
194 at91sam7_info->mck_freq = tmp >> ((mckr & PMC_MCKR_PRES) >> 2);
196 /* Forget old flash timing */
197 at91sam7_set_flash_mode(bank,FMR_TIMING_NONE);
200 /* Setup the timimg registers for nvbits or normal flash */
201 void at91sam7_set_flash_mode(flash_bank_t *bank,int mode)
203 u32 fmr, fmcn = 0, fws = 0;
204 at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
205 target_t *target = bank->target;
207 if (mode && (mode != at91sam7_info->flashmode))
209 /* Always round up (ceil) */
210 if (mode==FMR_TIMING_NVBITS)
212 if (at91sam7_info->cidr_arch == 0x60)
214 /* AT91SAM7A3 uses master clocks in 100 ns */
215 fmcn = (at91sam7_info->mck_freq/10000000ul)+1;
219 /* master clocks in 1uS for ARCH 0x7 types */
220 fmcn = (at91sam7_info->mck_freq/1000000ul)+1;
223 else if (mode==FMR_TIMING_FLASH)
224 /* main clocks in 1.5uS */
225 fmcn = (at91sam7_info->mck_freq/666666ul)+1;
227 /* Only allow fmcn=0 if clock period is > 30 us = 33kHz. */
228 if (at91sam7_info->mck_freq <= 33333ul)
230 /* Only allow fws=0 if clock frequency is < 30 MHz. */
231 if (at91sam7_info->mck_freq > 30000000ul)
234 DEBUG("fmcn: %i", fmcn);
235 fmr = fmcn << 16 | fws << 8;
236 target_write_u32(target, MC_FMR, fmr);
239 at91sam7_info->flashmode = mode;
242 u32 at91sam7_wait_status_busy(flash_bank_t *bank, u32 waitbits, int timeout)
246 while ((!((status = at91sam7_get_flash_status(bank)) & waitbits)) && (timeout-- > 0))
248 DEBUG("status: 0x%x", status);
252 DEBUG("status: 0x%x", status);
256 ERROR("status register: 0x%x", status);
258 ERROR("Lock Error Bit Detected, Operation Abort");
260 ERROR("Invalid command and/or bad keyword, Operation Abort");
262 ERROR("Security Bit Set, Operation Abort");
269 /* Send one command to the AT91SAM flash controller */
270 int at91sam7_flash_command(struct flash_bank_s *bank,u8 cmd,u16 pagen)
273 at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
274 target_t *target = bank->target;
276 fcr = (0x5A<<24) | (pagen<<8) | cmd;
277 target_write_u32(target, MC_FCR, fcr);
278 DEBUG("Flash command: 0x%x, pagenumber:%u", fcr, pagen);
280 if ((at91sam7_info->cidr_arch == 0x60)&&((cmd==SLB)|(cmd==CLB)))
282 /* Lock bit manipulation on AT91SAM7A3 waits for FC_FSR bit 1, EOL */
283 if (at91sam7_wait_status_busy(bank, MC_FSR_EOL, 10)&0x0C)
285 return ERROR_FLASH_OPERATION_FAILED;
290 if (at91sam7_wait_status_busy(bank, MC_FSR_FRDY, 10)&0x0C)
292 return ERROR_FLASH_OPERATION_FAILED;
297 /* Read device id register, main clock frequency register and fill in driver info structure */
298 int at91sam7_read_part_info(struct flash_bank_s *bank)
300 at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
301 target_t *target = bank->target;
304 if (bank->target->state != TARGET_HALTED)
306 return ERROR_TARGET_NOT_HALTED;
309 /* Read and parse chip identification register */
310 target_read_u32(target, DBGU_CIDR, &cidr);
314 WARNING("Cannot identify target as an AT91SAM");
315 return ERROR_FLASH_OPERATION_FAILED;
318 at91sam7_info->cidr = cidr;
319 at91sam7_info->cidr_ext = (cidr>>31)&0x0001;
320 at91sam7_info->cidr_nvptyp = (cidr>>28)&0x0007;
321 at91sam7_info->cidr_arch = (cidr>>20)&0x00FF;
322 at91sam7_info->cidr_sramsiz = (cidr>>16)&0x000F;
323 at91sam7_info->cidr_nvpsiz2 = (cidr>>12)&0x000F;
324 at91sam7_info->cidr_nvpsiz = (cidr>>8)&0x000F;
325 at91sam7_info->cidr_eproc = (cidr>>5)&0x0007;
326 at91sam7_info->cidr_version = cidr&0x001F;
327 bank->size = NVPSIZ[at91sam7_info->cidr_nvpsiz];
328 at91sam7_info->target_name = "Unknown";
330 /* Support just for bulk erase of the whole device */
331 bank->num_sectors = 1;
332 bank->sectors = malloc(sizeof(flash_sector_t));
333 bank->sectors[0].offset = 0;
334 bank->sectors[0].size = bank->size;
335 bank->sectors[0].is_erased = -1;
336 bank->sectors[0].is_protected = -1;
338 DEBUG("nvptyp: 0x%3.3x, arch: 0x%4.4x", at91sam7_info->cidr_nvptyp, at91sam7_info->cidr_arch );
340 /* Read main and master clock freqency register */
341 at91sam7_read_clock_info(bank);
343 status = at91sam7_get_flash_status(bank);
344 at91sam7_info->lockbits = status>>16;
345 at91sam7_info->securitybit = (status>>4)&0x01;
347 if (at91sam7_info->cidr_arch == 0x70 )
349 at91sam7_info->num_nvmbits = 2;
350 at91sam7_info->nvmbits = (status>>8)&0x03;
351 bank->base = 0x100000;
353 if (bank->size==0x40000) /* AT91SAM7S256 */
355 at91sam7_info->target_name = "AT91SAM7S256";
356 at91sam7_info->num_lockbits = 16;
357 at91sam7_info->pagesize = 256;
358 at91sam7_info->pages_in_lockregion = 64;
359 at91sam7_info->num_pages = 16*64;
361 if (bank->size==0x20000) /* AT91SAM7S128 */
363 at91sam7_info->target_name = "AT91SAM7S128";
364 at91sam7_info->num_lockbits = 8;
365 at91sam7_info->pagesize = 256;
366 at91sam7_info->pages_in_lockregion = 64;
367 at91sam7_info->num_pages = 8*64;
369 if (bank->size==0x10000) /* AT91SAM7S64 */
371 at91sam7_info->target_name = "AT91SAM7S64";
372 at91sam7_info->num_lockbits = 16;
373 at91sam7_info->pagesize = 128;
374 at91sam7_info->pages_in_lockregion = 32;
375 at91sam7_info->num_pages = 16*32;
377 if (bank->size==0x08000) /* AT91SAM7S321/32 */
379 at91sam7_info->target_name = "AT91SAM7S321/32";
380 at91sam7_info->num_lockbits = 8;
381 at91sam7_info->pagesize = 128;
382 at91sam7_info->pages_in_lockregion = 32;
383 at91sam7_info->num_pages = 8*32;
389 if (at91sam7_info->cidr_arch == 0x71 )
391 at91sam7_info->num_nvmbits = 3;
392 at91sam7_info->nvmbits = (status>>8)&0x07;
393 bank->base = 0x100000;
395 if (bank->size==0x40000) /* AT91SAM7XC256 */
397 at91sam7_info->target_name = "AT91SAM7XC256";
398 at91sam7_info->num_lockbits = 16;
399 at91sam7_info->pagesize = 256;
400 at91sam7_info->pages_in_lockregion = 64;
401 at91sam7_info->num_pages = 16*64;
403 if (bank->size==0x20000) /* AT91SAM7XC128 */
405 at91sam7_info->target_name = "AT91SAM7XC128";
406 at91sam7_info->num_lockbits = 8;
407 at91sam7_info->pagesize = 256;
408 at91sam7_info->pages_in_lockregion = 64;
409 at91sam7_info->num_pages = 8*64;
415 if (at91sam7_info->cidr_arch == 0x72 )
417 at91sam7_info->num_nvmbits = 2;
418 at91sam7_info->nvmbits = (status>>8)&0x03;
419 bank->base = 0x100000;
421 if (bank->size==0x80000) /* AT91SAM7SE512 */
423 at91sam7_info->target_name = "AT91SAM7SE512";
424 at91sam7_info->num_lockbits = 32;
425 at91sam7_info->pagesize = 256;
426 at91sam7_info->pages_in_lockregion = 64;
427 at91sam7_info->num_pages = 32*64;
429 if (bank->size==0x40000)
431 at91sam7_info->target_name = "AT91SAM7SE256";
432 at91sam7_info->num_lockbits = 16;
433 at91sam7_info->pagesize = 256;
434 at91sam7_info->pages_in_lockregion = 64;
435 at91sam7_info->num_pages = 16*64;
437 if (bank->size==0x08000)
439 at91sam7_info->target_name = "AT91SAM7SE32";
440 at91sam7_info->num_lockbits = 8;
441 at91sam7_info->pagesize = 128;
442 at91sam7_info->pages_in_lockregion = 32;
443 at91sam7_info->num_pages = 8*32;
449 if (at91sam7_info->cidr_arch == 0x75 )
451 at91sam7_info->num_nvmbits = 3;
452 at91sam7_info->nvmbits = (status>>8)&0x07;
453 bank->base = 0x100000;
455 if (bank->size==0x40000) /* AT91SAM7X256 */
457 at91sam7_info->target_name = "AT91SAM7X256";
458 at91sam7_info->num_lockbits = 16;
459 at91sam7_info->pagesize = 256;
460 at91sam7_info->pages_in_lockregion = 64;
461 at91sam7_info->num_pages = 16*64;
463 if (bank->size==0x20000) /* AT91SAM7X128 */
465 at91sam7_info->target_name = "AT91SAM7X128";
466 at91sam7_info->num_lockbits = 8;
467 at91sam7_info->pagesize = 256;
468 at91sam7_info->pages_in_lockregion = 64;
469 at91sam7_info->num_pages = 8*64;
475 if (at91sam7_info->cidr_arch == 0x60 )
477 at91sam7_info->num_nvmbits = 3;
478 at91sam7_info->nvmbits = (status>>8)&0x07;
479 bank->base = 0x100000;
482 if (bank->size == 0x40000) /* AT91SAM7A3 */
484 at91sam7_info->target_name = "AT91SAM7A3";
485 at91sam7_info->num_lockbits = 16;
486 at91sam7_info->pagesize = 256;
487 at91sam7_info->pages_in_lockregion = 16;
488 at91sam7_info->num_pages = 16*64;
493 WARNING("at91sam7 flash only tested for AT91SAM7Sxx series");
498 int at91sam7_erase_check(struct flash_bank_s *bank)
500 at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
502 if (!at91sam7_info->working_area_size)
512 int at91sam7_protect_check(struct flash_bank_s *bank)
516 at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
518 if (at91sam7_info->cidr == 0)
520 at91sam7_read_part_info(bank);
523 if (at91sam7_info->cidr == 0)
525 WARNING("Cannot identify target as an AT91SAM");
526 return ERROR_FLASH_OPERATION_FAILED;
529 status = at91sam7_get_flash_status(bank);
530 at91sam7_info->lockbits = status >> 16;
535 /* flash_bank at91sam7 0 0 0 0 <target#>
537 int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
539 at91sam7_flash_bank_t *at91sam7_info;
543 WARNING("incomplete flash_bank at91sam7 configuration");
544 return ERROR_FLASH_BANK_INVALID;
547 at91sam7_info = malloc(sizeof(at91sam7_flash_bank_t));
548 bank->driver_priv = at91sam7_info;
550 /* part wasn't probed for info yet */
551 at91sam7_info->cidr = 0;
556 int at91sam7_erase(struct flash_bank_s *bank, int first, int last)
558 at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
560 if (bank->target->state != TARGET_HALTED)
562 return ERROR_TARGET_NOT_HALTED;
565 if (at91sam7_info->cidr == 0)
567 at91sam7_read_part_info(bank);
570 if (at91sam7_info->cidr == 0)
572 WARNING("Cannot identify target as an AT91SAM");
573 return ERROR_FLASH_OPERATION_FAILED;
576 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
578 if ((first == 0) && (last == (at91sam7_info->num_lockbits-1)))
580 WARNING("Sector numbers based on lockbit count, probably a deprecated script");
581 last = bank->num_sectors-1;
583 else return ERROR_FLASH_SECTOR_INVALID;
586 if ((first != 0) || (last != (bank->num_sectors-1)))
588 WARNING("Can only erase the whole flash area, pages are autoerased on write");
589 return ERROR_FLASH_OPERATION_FAILED;
592 /* Configure the flash controller timing */
593 at91sam7_read_clock_info(bank);
594 at91sam7_set_flash_mode(bank,FMR_TIMING_FLASH);
596 return at91sam7_flash_command(bank, EA, 0);
599 int at91sam7_protect(struct flash_bank_s *bank, int set, int first, int last)
601 u32 cmd, pagen, status;
604 at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
606 if (bank->target->state != TARGET_HALTED)
608 return ERROR_TARGET_NOT_HALTED;
611 if ((first < 0) || (last < first) || (last >= at91sam7_info->num_lockbits))
613 return ERROR_FLASH_SECTOR_INVALID;
616 if (at91sam7_info->cidr == 0)
618 at91sam7_read_part_info(bank);
621 if (at91sam7_info->cidr == 0)
623 WARNING("Cannot identify target as an AT91SAM");
624 return ERROR_FLASH_OPERATION_FAILED;
627 /* Configure the flash controller timing */
628 at91sam7_read_clock_info(bank);
629 at91sam7_set_flash_mode(bank,FMR_TIMING_NVBITS);
631 for (lockregion=first;lockregion<=last;lockregion++)
633 pagen = lockregion*at91sam7_info->pages_in_lockregion;
638 if (at91sam7_flash_command(bank, cmd, pagen) != ERROR_OK)
640 return ERROR_FLASH_OPERATION_FAILED;
644 status = at91sam7_get_flash_status(bank);
645 at91sam7_info->lockbits = status>>16;
651 int at91sam7_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
653 at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
654 target_t *target = bank->target;
655 u32 dst_min_alignment, wcount, bytes_remaining = count;
656 u32 first_page, last_page, pagen, buffer_pos;
658 if (bank->target->state != TARGET_HALTED)
660 return ERROR_TARGET_NOT_HALTED;
663 if (at91sam7_info->cidr == 0)
665 at91sam7_read_part_info(bank);
668 if (at91sam7_info->cidr == 0)
670 WARNING("Cannot identify target as an AT91SAM");
671 return ERROR_FLASH_OPERATION_FAILED;
674 if (offset + count > bank->size)
675 return ERROR_FLASH_DST_OUT_OF_BANK;
677 dst_min_alignment = at91sam7_info->pagesize;
679 if (offset % dst_min_alignment)
681 WARNING("offset 0x%x breaks required alignment 0x%x", offset, dst_min_alignment);
682 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
685 if (at91sam7_info->cidr_arch == 0)
686 return ERROR_FLASH_BANK_NOT_PROBED;
688 first_page = offset/dst_min_alignment;
689 last_page = CEIL(offset + count, dst_min_alignment);
691 DEBUG("first_page: %i, last_page: %i, count %i", first_page, last_page, count);
693 /* Configure the flash controller timing */
694 at91sam7_read_clock_info(bank);
695 at91sam7_set_flash_mode(bank,FMR_TIMING_FLASH);
697 for (pagen=first_page; pagen<last_page; pagen++) {
698 if (bytes_remaining<dst_min_alignment)
699 count = bytes_remaining;
701 count = dst_min_alignment;
702 bytes_remaining -= count;
704 /* Write one block to the PageWriteBuffer */
705 buffer_pos = (pagen-first_page)*dst_min_alignment;
706 wcount = CEIL(count,4);
707 target->type->write_memory(target, bank->base, 4, wcount, buffer+buffer_pos);
709 /* Send Write Page command to Flash Controller */
710 if (at91sam7_flash_command(bank, WP, pagen) != ERROR_OK)
712 return ERROR_FLASH_OPERATION_FAILED;
714 DEBUG("Write page number:%i", pagen);
721 int at91sam7_probe(struct flash_bank_s *bank)
723 /* we can't probe on an at91sam7
724 * if this is an at91sam7, it has the configured flash
726 at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
728 if (at91sam7_info->cidr == 0)
730 at91sam7_read_part_info(bank);
733 if (at91sam7_info->cidr == 0)
735 WARNING("Cannot identify target as an AT91SAM");
736 return ERROR_FLASH_OPERATION_FAILED;
742 int at91sam7_info(struct flash_bank_s *bank, char *buf, int buf_size)
745 at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
747 at91sam7_read_part_info(bank);
749 if (at91sam7_info->cidr == 0)
751 printed = snprintf(buf, buf_size, "Cannot identify target as an AT91SAM\n");
754 return ERROR_FLASH_OPERATION_FAILED;
757 printed = snprintf(buf, buf_size, "\nat91sam7 information: Chip is %s\n",at91sam7_info->target_name);
761 printed = snprintf(buf, buf_size, "cidr: 0x%8.8x, arch: 0x%4.4x, eproc: %s, version:0x%3.3x, flashsize: 0x%8.8x\n", at91sam7_info->cidr, at91sam7_info->cidr_arch, EPROC[at91sam7_info->cidr_eproc], at91sam7_info->cidr_version, bank->size);
765 printed = snprintf(buf, buf_size, "master clock(estimated): %ikHz \n", at91sam7_info->mck_freq / 1000);
769 if (at91sam7_info->num_lockbits>0) {
770 printed = snprintf(buf, buf_size, "pagesize: %i, lockbits: %i 0x%4.4x, pages in lock region: %i \n", at91sam7_info->pagesize, at91sam7_info->num_lockbits, at91sam7_info->lockbits,at91sam7_info->num_pages/at91sam7_info->num_lockbits);
775 printed = snprintf(buf, buf_size, "securitybit: %i, nvmbits: 0x%1.1x\n", at91sam7_info->securitybit, at91sam7_info->nvmbits);
783 * On AT91SAM7S: When the gpnmv bits are set with
784 * > at91sam7 gpnvm 0 bitnr set
785 * the changes are not visible in the flash controller status register MC_FSR
786 * until the processor has been reset.
787 * On the Olimex board this requires a power cycle.
788 * Note that the AT91SAM7S has the following errata (doc6175.pdf sec 14.1.3):
789 * The maximum number of write/erase cycles for Non Volatile Memory bits is 100. This includes
790 * Lock Bits (LOCKx), General Purpose NVM bits (GPNVMx) and the Security Bit.
792 int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
799 at91sam7_flash_bank_t *at91sam7_info;
803 command_print(cmd_ctx, "at91sam7 gpnvm <num> <bit> <set|clear>");
807 bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
813 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
817 at91sam7_info = bank->driver_priv;
819 if (bank->target->state != TARGET_HALTED)
821 return ERROR_TARGET_NOT_HALTED;
824 if (at91sam7_info->cidr == 0)
826 at91sam7_read_part_info(bank);
829 if (at91sam7_info->cidr == 0)
831 WARNING("Cannot identify target as an AT91SAM");
832 return ERROR_FLASH_OPERATION_FAILED;
835 if ((bit<0) || (at91sam7_info->num_nvmbits <= bit))
837 command_print(cmd_ctx, "gpnvm bit '#%s' is out of bounds for target %s", args[1],at91sam7_info->target_name);
841 if (strcmp(value, "set") == 0)
845 else if (strcmp(value, "clear") == 0)
851 command_print(cmd_ctx, "usage: at91sam7 gpnvm <num> <bit> <set|clear>");
855 /* Configure the flash controller timing */
856 at91sam7_read_clock_info(bank);
857 at91sam7_set_flash_mode(bank,FMR_TIMING_NVBITS);
859 if (at91sam7_flash_command(bank, flashcmd, (u16)(bit)) != ERROR_OK)
861 return ERROR_FLASH_OPERATION_FAILED;
864 status = at91sam7_get_flash_status(bank);
865 DEBUG("at91sam7_handle_gpnvm_command: cmd 0x%x, value 0x%x, status 0x%x \n",flashcmd,bit,status);
866 at91sam7_info->nvmbits = (status>>8)&((1<<at91sam7_info->num_nvmbits)-1);