-Wshadow fixes
[openocd/cortex.git] / src / flash / nor / at91sam7.c
blob606cc32b02908b469ed6ff816c43df2976d0eda4
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 "at91sam7.h"
54 #include <helper/binarybuffer.h>
56 static int at91sam7_protect_check(struct flash_bank *bank);
57 static int at91sam7_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
59 static uint32_t at91sam7_get_flash_status(struct target *target, int bank_number);
60 static void at91sam7_set_flash_mode(struct flash_bank *bank, int mode);
61 static uint32_t at91sam7_wait_status_busy(struct flash_bank *bank, uint32_t waitbits, int timeout);
62 static int at91sam7_flash_command(struct flash_bank *bank, uint8_t cmd, uint16_t pagen);
64 static uint32_t MC_FMR[4] = { 0xFFFFFF60, 0xFFFFFF70, 0xFFFFFF80, 0xFFFFFF90 };
65 static uint32_t MC_FCR[4] = { 0xFFFFFF64, 0xFFFFFF74, 0xFFFFFF84, 0xFFFFFF94 };
66 static uint32_t MC_FSR[4] = { 0xFFFFFF68, 0xFFFFFF78, 0xFFFFFF88, 0xFFFFFF98 };
68 static char * EPROC[8]= {"Unknown","ARM946-E","ARM7TDMI","Unknown","ARM920T","ARM926EJ-S","Unknown","Unknown"};
70 #if 0
71 static long SRAMSIZ[16] = {
72 -1,
73 0x0400, /* 1K */
74 0x0800, /* 2K */
75 -1,
76 0x1c000, /* 112K */
77 0x1000, /* 4K */
78 0x14000, /* 80K */
79 0x28000, /* 160K */
80 0x2000, /* 8K */
81 0x4000, /* 16K */
82 0x8000, /* 32K */
83 0x10000, /* 64K */
84 0x20000, /* 128K */
85 0x40000, /* 256K */
86 0x18000, /* 96K */
87 0x80000, /* 512K */
89 #endif
92 static uint32_t at91sam7_get_flash_status(struct target *target, int bank_number)
94 uint32_t fsr;
95 target_read_u32(target, MC_FSR[bank_number], &fsr);
97 return fsr;
100 /* Read clock configuration and set at91sam7_info->mck_freq */
101 static void at91sam7_read_clock_info(struct flash_bank *bank)
103 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
104 struct target *target = bank->target;
105 uint32_t mckr, mcfr, pllr, mor;
106 unsigned long tmp = 0, mainfreq;
108 /* Read Clock Generator Main Oscillator Register */
109 target_read_u32(target, CKGR_MOR, &mor);
110 /* Read Clock Generator Main Clock Frequency Register */
111 target_read_u32(target, CKGR_MCFR, &mcfr);
112 /* Read Master Clock Register*/
113 target_read_u32(target, PMC_MCKR, &mckr);
114 /* Read Clock Generator PLL Register */
115 target_read_u32(target, CKGR_PLLR, &pllr);
117 at91sam7_info->mck_valid = 0;
118 at91sam7_info->mck_freq = 0;
119 switch (mckr & PMC_MCKR_CSS)
121 case 0: /* Slow Clock */
122 at91sam7_info->mck_valid = 1;
123 tmp = RC_FREQ;
124 break;
126 case 1: /* Main Clock */
127 if ((mcfr & CKGR_MCFR_MAINRDY) &&
128 (at91sam7_info->ext_freq == 0))
130 at91sam7_info->mck_valid = 1;
131 tmp = RC_FREQ / 16ul * (mcfr & 0xffff);
133 else if (at91sam7_info->ext_freq != 0)
135 at91sam7_info->mck_valid = 1;
136 tmp = at91sam7_info->ext_freq;
138 break;
140 case 2: /* Reserved */
141 break;
143 case 3: /* PLL Clock */
144 if ((mcfr & CKGR_MCFR_MAINRDY) &&
145 (at91sam7_info->ext_freq == 0))
147 target_read_u32(target, CKGR_PLLR, &pllr);
148 if (!(pllr & CKGR_PLLR_DIV))
149 break; /* 0 Hz */
150 at91sam7_info->mck_valid = 1;
151 mainfreq = RC_FREQ / 16ul * (mcfr & 0xffff);
152 /* Integer arithmetic should have sufficient precision
153 * as long as PLL is properly configured. */
154 tmp = mainfreq / (pllr & CKGR_PLLR_DIV)*
155 (((pllr & CKGR_PLLR_MUL) >> 16) + 1);
157 else if ((at91sam7_info->ext_freq != 0) &&
158 ((pllr&CKGR_PLLR_DIV) != 0))
160 at91sam7_info->mck_valid = 1;
161 tmp = at91sam7_info->ext_freq / (pllr&CKGR_PLLR_DIV)*
162 (((pllr & CKGR_PLLR_MUL) >> 16) + 1);
164 break;
167 /* Prescaler adjust */
168 if ((((mckr & PMC_MCKR_PRES) >> 2) == 7) || (tmp == 0))
170 at91sam7_info->mck_valid = 0;
171 at91sam7_info->mck_freq = 0;
173 else if (((mckr & PMC_MCKR_PRES) >> 2) != 0)
174 at91sam7_info->mck_freq = tmp >> ((mckr & PMC_MCKR_PRES) >> 2);
175 else
176 at91sam7_info->mck_freq = tmp;
179 /* Setup the timimg registers for nvbits or normal flash */
180 static void at91sam7_set_flash_mode(struct flash_bank *bank, int mode)
182 uint32_t fmr, fmcn = 0, fws = 0;
183 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
184 struct target *target = bank->target;
186 if (mode && (mode != at91sam7_info->flashmode))
188 /* Always round up (ceil) */
189 if (mode == FMR_TIMING_NVBITS)
191 if (at91sam7_info->cidr_arch == 0x60)
193 /* AT91SAM7A3 uses master clocks in 100 ns */
194 fmcn = (at91sam7_info->mck_freq/10000000ul) + 1;
196 else
198 /* master clocks in 1uS for ARCH 0x7 types */
199 fmcn = (at91sam7_info->mck_freq/1000000ul) + 1;
202 else if (mode == FMR_TIMING_FLASH)
204 /* main clocks in 1.5uS */
205 fmcn = (at91sam7_info->mck_freq/1000000ul)+
206 (at91sam7_info->mck_freq/2000000ul) + 1;
209 /* hard overclocking */
210 if (fmcn > 0xFF)
211 fmcn = 0xFF;
213 /* Only allow fmcn = 0 if clock period is > 30 us = 33kHz. */
214 if (at91sam7_info->mck_freq <= 33333ul)
215 fmcn = 0;
216 /* Only allow fws = 0 if clock frequency is < 30 MHz. */
217 if (at91sam7_info->mck_freq > 30000000ul)
218 fws = 1;
220 LOG_DEBUG("fmcn[%i]: %i", bank->bank_number, (int)(fmcn));
221 fmr = fmcn << 16 | fws << 8;
222 target_write_u32(target, MC_FMR[bank->bank_number], fmr);
225 at91sam7_info->flashmode = mode;
228 static uint32_t at91sam7_wait_status_busy(struct flash_bank *bank, uint32_t waitbits, int timeout)
230 uint32_t status;
232 while ((!((status = at91sam7_get_flash_status(bank->target, bank->bank_number)) & waitbits)) && (timeout-- > 0))
234 LOG_DEBUG("status[%i]: 0x%" PRIx32 "", (int)bank->bank_number, status);
235 alive_sleep(1);
238 LOG_DEBUG("status[%i]: 0x%" PRIx32 "", bank->bank_number, status);
240 if (status & 0x0C)
242 LOG_ERROR("status register: 0x%" PRIx32 "", status);
243 if (status & 0x4)
244 LOG_ERROR("Lock Error Bit Detected, Operation Abort");
245 if (status & 0x8)
246 LOG_ERROR("Invalid command and/or bad keyword, Operation Abort");
247 if (status & 0x10)
248 LOG_ERROR("Security Bit Set, Operation Abort");
251 return status;
254 /* Send one command to the AT91SAM flash controller */
255 static int at91sam7_flash_command(struct flash_bank *bank, uint8_t cmd, uint16_t pagen)
257 uint32_t fcr;
258 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
259 struct target *target = bank->target;
261 fcr = (0x5A << 24) | ((pagen&0x3FF) << 8) | cmd;
262 target_write_u32(target, MC_FCR[bank->bank_number], fcr);
263 LOG_DEBUG("Flash command: 0x%" PRIx32 ", flash bank: %i, page number: %u", fcr, bank->bank_number + 1, pagen);
265 if ((at91sam7_info->cidr_arch == 0x60) && ((cmd == SLB) | (cmd == CLB)))
267 /* Lock bit manipulation on AT91SAM7A3 waits for FC_FSR bit 1, EOL */
268 if (at91sam7_wait_status_busy(bank, MC_FSR_EOL, 10)&0x0C)
270 return ERROR_FLASH_OPERATION_FAILED;
272 return ERROR_OK;
275 if (at91sam7_wait_status_busy(bank, MC_FSR_FRDY, 10)&0x0C)
277 return ERROR_FLASH_OPERATION_FAILED;
280 return ERROR_OK;
283 /* Read device id register, main clock frequency register and fill in driver info structure */
284 static int at91sam7_read_part_info(struct flash_bank *bank)
286 struct flash_bank *t_bank = bank;
287 struct at91sam7_flash_bank *at91sam7_info;
288 struct target *target = t_bank->target;
290 uint16_t bnk, sec;
291 uint16_t arch;
292 uint32_t cidr;
293 uint8_t banks_num = 0;
294 uint16_t num_nvmbits = 0;
295 uint16_t sectors_num = 0;
296 uint16_t pages_per_sector = 0;
297 uint16_t page_size = 0;
298 uint32_t ext_freq;
299 uint32_t bank_size;
300 uint32_t base_address = 0;
301 char *target_name = "Unknown";
303 at91sam7_info = t_bank->driver_priv;
305 if (at91sam7_info->cidr != 0)
307 /* flash already configured, update clock and check for protected sectors */
308 struct flash_bank *fb = bank;
309 t_bank = fb;
311 while (t_bank)
313 /* re-calculate master clock frequency */
314 at91sam7_read_clock_info(t_bank);
316 /* no timming */
317 at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
319 /* check protect state */
320 at91sam7_protect_check(t_bank);
322 t_bank = fb->next;
323 fb = t_bank;
326 return ERROR_OK;
329 /* Read and parse chip identification register */
330 target_read_u32(target, DBGU_CIDR, &cidr);
331 if (cidr == 0)
333 LOG_WARNING("Cannot identify target as an AT91SAM");
334 return ERROR_FLASH_OPERATION_FAILED;
337 if (at91sam7_info->flash_autodetection == 0)
339 /* banks and sectors are already created, based on data from input file */
340 struct flash_bank *fb = bank;
341 t_bank = fb;
342 while (t_bank)
344 at91sam7_info = t_bank->driver_priv;
346 at91sam7_info->cidr = cidr;
347 at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
348 at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
349 at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
350 at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
351 at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
352 at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
353 at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
354 at91sam7_info->cidr_version = cidr&0x001F;
356 /* calculate master clock frequency */
357 at91sam7_read_clock_info(t_bank);
359 /* no timming */
360 at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
362 /* check protect state */
363 at91sam7_protect_check(t_bank);
365 t_bank = fb->next;
366 fb = t_bank;
369 return ERROR_OK;
372 arch = (cidr >> 20)&0x00FF;
374 /* check flash size */
375 switch ((cidr >> 8)&0x000F)
377 case FLASH_SIZE_8KB:
378 break;
380 case FLASH_SIZE_16KB:
381 banks_num = 1;
382 sectors_num = 8;
383 pages_per_sector = 32;
384 page_size = 64;
385 base_address = 0x00100000;
386 if (arch == 0x70)
388 num_nvmbits = 2;
389 target_name = "AT91SAM7S161/16";
391 break;
393 case FLASH_SIZE_32KB:
394 banks_num = 1;
395 sectors_num = 8;
396 pages_per_sector = 32;
397 page_size = 128;
398 base_address = 0x00100000;
399 if (arch == 0x70)
401 num_nvmbits = 2;
402 target_name = "AT91SAM7S321/32";
404 if (arch == 0x72)
406 num_nvmbits = 3;
407 target_name = "AT91SAM7SE32";
409 break;
411 case FLASH_SIZE_64KB:
412 banks_num = 1;
413 sectors_num = 16;
414 pages_per_sector = 32;
415 page_size = 128;
416 base_address = 0x00100000;
417 if (arch == 0x70)
419 num_nvmbits = 2;
420 target_name = "AT91SAM7S64";
422 break;
424 case FLASH_SIZE_128KB:
425 banks_num = 1;
426 sectors_num = 8;
427 pages_per_sector = 64;
428 page_size = 256;
429 base_address = 0x00100000;
430 if (arch == 0x70)
432 num_nvmbits = 2;
433 target_name = "AT91SAM7S128";
435 if (arch == 0x71)
437 num_nvmbits = 3;
438 target_name = "AT91SAM7XC128";
440 if (arch == 0x72)
442 num_nvmbits = 3;
443 target_name = "AT91SAM7SE128";
445 if (arch == 0x75)
447 num_nvmbits = 3;
448 target_name = "AT91SAM7X128";
450 break;
452 case FLASH_SIZE_256KB:
453 banks_num = 1;
454 sectors_num = 16;
455 pages_per_sector = 64;
456 page_size = 256;
457 base_address = 0x00100000;
458 if (arch == 0x60)
460 num_nvmbits = 3;
461 target_name = "AT91SAM7A3";
463 if (arch == 0x70)
465 num_nvmbits = 2;
466 target_name = "AT91SAM7S256";
468 if (arch == 0x71)
470 num_nvmbits = 3;
471 target_name = "AT91SAM7XC256";
473 if (arch == 0x72)
475 num_nvmbits = 3;
476 target_name = "AT91SAM7SE256";
478 if (arch == 0x75)
480 num_nvmbits = 3;
481 target_name = "AT91SAM7X256";
483 break;
485 case FLASH_SIZE_512KB:
486 banks_num = 2;
487 sectors_num = 16;
488 pages_per_sector = 64;
489 page_size = 256;
490 base_address = 0x00100000;
491 if (arch == 0x70)
493 num_nvmbits = 2;
494 target_name = "AT91SAM7S512";
496 if (arch == 0x71)
498 num_nvmbits = 3;
499 target_name = "AT91SAM7XC512";
501 if (arch == 0x72)
503 num_nvmbits = 3;
504 target_name = "AT91SAM7SE512";
506 if (arch == 0x75)
508 num_nvmbits = 3;
509 target_name = "AT91SAM7X512";
511 break;
513 case FLASH_SIZE_1024KB:
514 break;
516 case FLASH_SIZE_2048KB:
517 break;
520 if (strcmp(target_name, "Unknown") == 0)
522 LOG_ERROR("Target autodetection failed! Please specify target parameters in configuration file");
523 return ERROR_FLASH_OPERATION_FAILED;
526 ext_freq = at91sam7_info->ext_freq;
528 /* calculate bank size */
529 bank_size = sectors_num * pages_per_sector * page_size;
531 for (bnk = 0; bnk < banks_num; bnk++)
533 if (bnk > 0)
535 /* create a new flash bank element */
536 struct flash_bank *fb = malloc(sizeof(struct flash_bank));
537 fb->target = target;
538 fb->driver = bank->driver;
539 fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
540 fb->next = NULL;
542 /* link created bank in 'flash_banks' list and redirect t_bank */
543 t_bank->next = fb;
544 t_bank = fb;
547 t_bank->bank_number = bnk;
548 t_bank->base = base_address + bnk * bank_size;
549 t_bank->size = bank_size;
550 t_bank->chip_width = 0;
551 t_bank->bus_width = 4;
552 t_bank->num_sectors = sectors_num;
554 /* allocate sectors */
555 t_bank->sectors = malloc(sectors_num * sizeof(struct flash_sector));
556 for (sec = 0; sec < sectors_num; sec++)
558 t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
559 t_bank->sectors[sec].size = pages_per_sector * page_size;
560 t_bank->sectors[sec].is_erased = -1;
561 t_bank->sectors[sec].is_protected = -1;
564 at91sam7_info = t_bank->driver_priv;
566 at91sam7_info->cidr = cidr;
567 at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
568 at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
569 at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
570 at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
571 at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
572 at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
573 at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
574 at91sam7_info->cidr_version = cidr&0x001F;
576 at91sam7_info->target_name = target_name;
577 at91sam7_info->flashmode = 0;
578 at91sam7_info->ext_freq = ext_freq;
579 at91sam7_info->num_nvmbits = num_nvmbits;
580 at91sam7_info->num_nvmbits_on = 0;
581 at91sam7_info->pagesize = page_size;
582 at91sam7_info->pages_per_sector = pages_per_sector;
584 /* calculate master clock frequency */
585 at91sam7_read_clock_info(t_bank);
587 /* no timming */
588 at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
590 /* check protect state */
591 at91sam7_protect_check(t_bank);
594 LOG_DEBUG("nvptyp: 0x%3.3x, arch: 0x%4.4x", at91sam7_info->cidr_nvptyp, at91sam7_info->cidr_arch);
596 return ERROR_OK;
599 static int at91sam7_erase_check(struct flash_bank *bank)
601 struct target *target = bank->target;
602 uint16_t retval;
603 uint32_t blank;
604 uint16_t fast_check;
605 uint8_t *buffer;
606 uint16_t nSector;
607 uint16_t nByte;
609 if (bank->target->state != TARGET_HALTED)
611 LOG_ERROR("Target not halted");
612 return ERROR_TARGET_NOT_HALTED;
615 /* Configure the flash controller timing */
616 at91sam7_read_clock_info(bank);
617 at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
619 fast_check = 1;
620 for (nSector = 0; nSector < bank->num_sectors; nSector++)
622 retval = target_blank_check_memory(target, bank->base + bank->sectors[nSector].offset,
623 bank->sectors[nSector].size, &blank);
624 if (retval != ERROR_OK)
626 fast_check = 0;
627 break;
629 if (blank == 0xFF)
630 bank->sectors[nSector].is_erased = 1;
631 else
632 bank->sectors[nSector].is_erased = 0;
635 if (fast_check)
637 return ERROR_OK;
640 LOG_USER("Running slow fallback erase check - add working memory");
642 buffer = malloc(bank->sectors[0].size);
643 for (nSector = 0; nSector < bank->num_sectors; nSector++)
645 bank->sectors[nSector].is_erased = 1;
646 retval = target_read_memory(target, bank->base + bank->sectors[nSector].offset, 4,
647 bank->sectors[nSector].size/4, buffer);
648 if (retval != ERROR_OK)
649 return retval;
651 for (nByte = 0; nByte < bank->sectors[nSector].size; nByte++)
653 if (buffer[nByte] != 0xFF)
655 bank->sectors[nSector].is_erased = 0;
656 break;
660 free(buffer);
662 return ERROR_OK;
665 static int at91sam7_protect_check(struct flash_bank *bank)
667 uint8_t lock_pos, gpnvm_pos;
668 uint32_t status;
670 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
672 if (at91sam7_info->cidr == 0)
674 return ERROR_FLASH_BANK_NOT_PROBED;
676 if (bank->target->state != TARGET_HALTED)
678 LOG_ERROR("Target not halted");
679 return ERROR_TARGET_NOT_HALTED;
682 status = at91sam7_get_flash_status(bank->target, bank->bank_number);
683 at91sam7_info->lockbits = (status >> 16);
685 at91sam7_info->num_lockbits_on = 0;
686 for (lock_pos = 0; lock_pos < bank->num_sectors; lock_pos++)
688 if (((status >> (16 + lock_pos))&(0x0001)) == 1)
690 at91sam7_info->num_lockbits_on++;
691 bank->sectors[lock_pos].is_protected = 1;
693 else
694 bank->sectors[lock_pos].is_protected = 0;
697 /* GPNVM and SECURITY bits apply only for MC_FSR of EFC0 */
698 status = at91sam7_get_flash_status(bank->target, 0);
700 at91sam7_info->securitybit = (status >> 4)&0x01;
701 at91sam7_info->nvmbits = (status >> 8)&0xFF;
703 at91sam7_info->num_nvmbits_on = 0;
704 for (gpnvm_pos = 0; gpnvm_pos < at91sam7_info->num_nvmbits; gpnvm_pos++)
706 if (((status >> (8 + gpnvm_pos))&(0x01)) == 1)
708 at91sam7_info->num_nvmbits_on++;
712 return ERROR_OK;
715 FLASH_BANK_COMMAND_HANDLER(at91sam7_flash_bank_command)
717 struct flash_bank *t_bank = bank;
718 struct at91sam7_flash_bank *at91sam7_info;
719 struct target *target = t_bank->target;
721 uint32_t base_address;
722 uint32_t bank_size;
723 uint32_t ext_freq = 0;
725 int chip_width;
726 int bus_width;
727 int banks_num;
728 int num_sectors;
730 uint16_t pages_per_sector;
731 uint16_t page_size;
732 uint16_t num_nvmbits;
734 char *target_name;
736 int bnk, sec;
738 at91sam7_info = malloc(sizeof(struct at91sam7_flash_bank));
739 t_bank->driver_priv = at91sam7_info;
741 /* part wasn't probed for info yet */
742 at91sam7_info->cidr = 0;
743 at91sam7_info->flashmode = 0;
744 at91sam7_info->ext_freq = 0;
745 at91sam7_info->flash_autodetection = 0;
747 if (CMD_ARGC < 13)
749 at91sam7_info->flash_autodetection = 1;
750 return ERROR_OK;
753 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], base_address);
755 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], chip_width);
756 COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], bus_width);
758 COMMAND_PARSE_NUMBER(int, CMD_ARGV[8], banks_num);
759 COMMAND_PARSE_NUMBER(int, CMD_ARGV[9], num_sectors);
760 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[10], pages_per_sector);
761 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[11], page_size);
762 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[12], num_nvmbits);
764 if (CMD_ARGC == 14) {
765 unsigned long freq;
766 COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[13], freq);
767 ext_freq = freq * 1000;
768 at91sam7_info->ext_freq = ext_freq;
771 if ((bus_width == 0) || (banks_num == 0) || (num_sectors == 0) ||
772 (pages_per_sector == 0) || (page_size == 0) || (num_nvmbits == 0))
774 at91sam7_info->flash_autodetection = 1;
775 return ERROR_OK;
778 target_name = calloc(strlen(CMD_ARGV[7]) + 1, sizeof(char));
779 strcpy(target_name, CMD_ARGV[7]);
781 /* calculate bank size */
782 bank_size = num_sectors * pages_per_sector * page_size;
784 for (bnk = 0; bnk < banks_num; bnk++)
786 if (bnk > 0)
788 /* create a new bank element */
789 struct flash_bank *fb = malloc(sizeof(struct flash_bank));
790 fb->target = target;
791 fb->driver = bank->driver;
792 fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
793 fb->next = NULL;
795 /* link created bank in 'flash_banks' list and redirect t_bank */
796 t_bank->next = fb;
797 t_bank = fb;
800 t_bank->bank_number = bnk;
801 t_bank->base = base_address + bnk * bank_size;
802 t_bank->size = bank_size;
803 t_bank->chip_width = chip_width;
804 t_bank->bus_width = bus_width;
805 t_bank->num_sectors = num_sectors;
807 /* allocate sectors */
808 t_bank->sectors = malloc(num_sectors * sizeof(struct flash_sector));
809 for (sec = 0; sec < num_sectors; sec++)
811 t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
812 t_bank->sectors[sec].size = pages_per_sector * page_size;
813 t_bank->sectors[sec].is_erased = -1;
814 t_bank->sectors[sec].is_protected = -1;
817 at91sam7_info = t_bank->driver_priv;
819 at91sam7_info->target_name = target_name;
820 at91sam7_info->flashmode = 0;
821 at91sam7_info->ext_freq = ext_freq;
822 at91sam7_info->num_nvmbits = num_nvmbits;
823 at91sam7_info->num_nvmbits_on = 0;
824 at91sam7_info->pagesize = page_size;
825 at91sam7_info->pages_per_sector = pages_per_sector;
828 return ERROR_OK;
831 static int at91sam7_erase(struct flash_bank *bank, int first, int last)
833 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
834 int sec;
835 uint32_t nbytes, pos;
836 uint8_t *buffer;
837 uint8_t erase_all;
839 if (at91sam7_info->cidr == 0)
841 return ERROR_FLASH_BANK_NOT_PROBED;
844 if (bank->target->state != TARGET_HALTED)
846 LOG_ERROR("Target not halted");
847 return ERROR_TARGET_NOT_HALTED;
850 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
852 return ERROR_FLASH_SECTOR_INVALID;
855 erase_all = 0;
856 if ((first == 0) && (last == (bank->num_sectors-1)))
858 erase_all = 1;
861 /* Configure the flash controller timing */
862 at91sam7_read_clock_info(bank);
863 at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
865 if (erase_all)
867 if (at91sam7_flash_command(bank, EA, 0) != ERROR_OK)
869 return ERROR_FLASH_OPERATION_FAILED;
872 else
874 /* allocate and clean buffer */
875 nbytes = (last - first + 1) * bank->sectors[first].size;
876 buffer = malloc(nbytes * sizeof(uint8_t));
877 for (pos = 0; pos < nbytes; pos++)
879 buffer[pos] = 0xFF;
882 if (at91sam7_write(bank, buffer, bank->sectors[first].offset, nbytes) != ERROR_OK)
884 return ERROR_FLASH_OPERATION_FAILED;
887 free(buffer);
890 /* mark erased sectors */
891 for (sec = first; sec <= last; sec++)
893 bank->sectors[sec].is_erased = 1;
896 return ERROR_OK;
899 static int at91sam7_protect(struct flash_bank *bank, int set, int first, int last)
901 uint32_t cmd;
902 int sector;
903 uint32_t pagen;
905 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
907 if (at91sam7_info->cidr == 0)
909 return ERROR_FLASH_BANK_NOT_PROBED;
912 if (bank->target->state != TARGET_HALTED)
914 LOG_ERROR("Target not halted");
915 return ERROR_TARGET_NOT_HALTED;
918 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
920 return ERROR_FLASH_SECTOR_INVALID;
923 /* Configure the flash controller timing */
924 at91sam7_read_clock_info(bank);
925 at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);
927 for (sector = first; sector <= last; sector++)
929 if (set)
930 cmd = SLB;
931 else
932 cmd = CLB;
934 /* if we lock a page from one sector then entire sector will be locked, also,
935 * if we unlock a page from a locked sector, entire sector will be unlocked */
936 pagen = sector * at91sam7_info->pages_per_sector;
938 if (at91sam7_flash_command(bank, cmd, pagen) != ERROR_OK)
940 return ERROR_FLASH_OPERATION_FAILED;
944 at91sam7_protect_check(bank);
946 return ERROR_OK;
949 static int at91sam7_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
951 int retval;
952 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
953 struct target *target = bank->target;
954 uint32_t dst_min_alignment, wcount, bytes_remaining = count;
955 uint32_t first_page, last_page, pagen, buffer_pos;
957 if (at91sam7_info->cidr == 0)
959 return ERROR_FLASH_BANK_NOT_PROBED;
962 if (bank->target->state != TARGET_HALTED)
964 LOG_ERROR("Target not halted");
965 return ERROR_TARGET_NOT_HALTED;
968 if (offset + count > bank->size)
969 return ERROR_FLASH_DST_OUT_OF_BANK;
971 dst_min_alignment = at91sam7_info->pagesize;
973 if (offset % dst_min_alignment)
975 LOG_WARNING("offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32 "", offset, dst_min_alignment);
976 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
979 if (at91sam7_info->cidr_arch == 0)
980 return ERROR_FLASH_BANK_NOT_PROBED;
982 first_page = offset/dst_min_alignment;
983 last_page = DIV_ROUND_UP(offset + count, dst_min_alignment);
985 LOG_DEBUG("first_page: %i, last_page: %i, count %i", (int)first_page, (int)last_page, (int)count);
987 /* Configure the flash controller timing */
988 at91sam7_read_clock_info(bank);
989 at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
991 for (pagen = first_page; pagen < last_page; pagen++)
993 if (bytes_remaining < dst_min_alignment)
994 count = bytes_remaining;
995 else
996 count = dst_min_alignment;
997 bytes_remaining -= count;
999 /* Write one block to the PageWriteBuffer */
1000 buffer_pos = (pagen-first_page)*dst_min_alignment;
1001 wcount = DIV_ROUND_UP(count,4);
1002 if ((retval = target_write_memory(target, bank->base + pagen*dst_min_alignment, 4, wcount, buffer + buffer_pos)) != ERROR_OK)
1004 return retval;
1007 /* Send Write Page command to Flash Controller */
1008 if (at91sam7_flash_command(bank, WP, pagen) != ERROR_OK)
1010 return ERROR_FLASH_OPERATION_FAILED;
1012 LOG_DEBUG("Write flash bank:%i page number:%" PRIi32 "", bank->bank_number, pagen);
1015 return ERROR_OK;
1018 static int at91sam7_probe(struct flash_bank *bank)
1020 /* we can't probe on an at91sam7
1021 * if this is an at91sam7, it has the configured flash */
1022 int retval;
1024 if (bank->target->state != TARGET_HALTED)
1026 LOG_ERROR("Target not halted");
1027 return ERROR_TARGET_NOT_HALTED;
1030 retval = at91sam7_read_part_info(bank);
1031 if (retval != ERROR_OK)
1032 return retval;
1034 return ERROR_OK;
1037 static int at91sam7_info(struct flash_bank *bank, char *buf, int buf_size)
1039 int printed;
1040 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
1042 if (at91sam7_info->cidr == 0)
1044 return ERROR_FLASH_BANK_NOT_PROBED;
1047 printed = snprintf(buf, buf_size,
1048 "\n at91sam7 driver information: Chip is %s\n",
1049 at91sam7_info->target_name);
1051 buf += printed;
1052 buf_size -= printed;
1054 printed = snprintf(buf,
1055 buf_size,
1056 " Cidr: 0x%8.8" PRIx32 " | Arch: 0x%4.4x | Eproc: %s | Version: 0x%3.3x | Flashsize: 0x%8.8" PRIx32 "\n",
1057 at91sam7_info->cidr,
1058 at91sam7_info->cidr_arch,
1059 EPROC[at91sam7_info->cidr_eproc],
1060 at91sam7_info->cidr_version,
1061 bank->size);
1063 buf += printed;
1064 buf_size -= printed;
1066 printed = snprintf(buf, buf_size,
1067 " Master clock (estimated): %u KHz | External clock: %u KHz\n",
1068 (unsigned)(at91sam7_info->mck_freq / 1000), (unsigned)(at91sam7_info->ext_freq / 1000));
1070 buf += printed;
1071 buf_size -= printed;
1073 printed = snprintf(buf, buf_size,
1074 " Pagesize: %i bytes | Lockbits(%i): %i 0x%4.4x | Pages in lock region: %i \n",
1075 at91sam7_info->pagesize, bank->num_sectors, at91sam7_info->num_lockbits_on,
1076 at91sam7_info->lockbits, at91sam7_info->pages_per_sector*at91sam7_info->num_lockbits_on);
1078 buf += printed;
1079 buf_size -= printed;
1081 printed = snprintf(buf, buf_size,
1082 " Securitybit: %i | Nvmbits(%i): %i 0x%1.1x\n",
1083 at91sam7_info->securitybit, at91sam7_info->num_nvmbits,
1084 at91sam7_info->num_nvmbits_on, at91sam7_info->nvmbits);
1086 buf += printed;
1087 buf_size -= printed;
1089 return ERROR_OK;
1093 * On AT91SAM7S: When the gpnvm bits are set with
1094 * > at91sam7 gpnvm bitnr set
1095 * the changes are not visible in the flash controller status register MC_FSR
1096 * until the processor has been reset.
1097 * On the Olimex board this requires a power cycle.
1098 * Note that the AT91SAM7S has the following errata (doc6175.pdf sec 14.1.3):
1099 * The maximum number of write/erase cycles for Non volatile Memory bits is 100. this includes
1100 * Lock Bits (LOCKx), General Purpose NVM bits (GPNVMx) and the Security Bit.
1102 COMMAND_HANDLER(at91sam7_handle_gpnvm_command)
1104 struct flash_bank *bank;
1105 int bit;
1106 uint8_t flashcmd;
1107 uint32_t status;
1108 struct at91sam7_flash_bank *at91sam7_info;
1109 int retval;
1111 if (CMD_ARGC != 2)
1113 command_print(CMD_CTX, "at91sam7 gpnvm <bit> <set | clear>");
1114 return ERROR_OK;
1117 bank = get_flash_bank_by_num_noprobe(0);
1118 if (bank == NULL)
1120 return ERROR_FLASH_BANK_INVALID;
1122 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)
1129 LOG_ERROR("target has to be halted to perform flash operation");
1130 return ERROR_TARGET_NOT_HALTED;
1133 if (strcmp(CMD_ARGV[1], "set") == 0)
1135 flashcmd = SGPB;
1137 else if (strcmp(CMD_ARGV[1], "clear") == 0)
1139 flashcmd = CGPB;
1141 else
1143 return ERROR_COMMAND_SYNTAX_ERROR;
1146 at91sam7_info = bank->driver_priv;
1147 if (at91sam7_info->cidr == 0)
1149 retval = at91sam7_read_part_info(bank);
1150 if (retval != ERROR_OK)
1152 return retval;
1156 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], bit);
1157 if ((bit < 0) || (bit >= at91sam7_info->num_nvmbits))
1159 command_print(CMD_CTX, "gpnvm bit '#%s' is out of bounds for target %s", CMD_ARGV[0], at91sam7_info->target_name);
1160 return ERROR_OK;
1163 /* Configure the flash controller timing */
1164 at91sam7_read_clock_info(bank);
1165 at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);
1167 if (at91sam7_flash_command(bank, flashcmd, bit) != ERROR_OK)
1169 return ERROR_FLASH_OPERATION_FAILED;
1172 /* GPNVM and SECURITY bits apply only for MC_FSR of EFC0 */
1173 status = at91sam7_get_flash_status(bank->target, 0);
1174 LOG_DEBUG("at91sam7_handle_gpnvm_command: cmd 0x%x, value %d, status 0x%" PRIx32 " \n", flashcmd, bit, status);
1176 /* check protect state */
1177 at91sam7_protect_check(bank);
1179 return ERROR_OK;
1182 static const struct command_registration at91sam7_exec_command_handlers[] = {
1184 .name = "gpnvm",
1185 .handler = at91sam7_handle_gpnvm_command,
1186 .mode = COMMAND_EXEC,
1187 .help = "set or clear one General Purpose Non-Volatile Memory "
1188 "(gpnvm) bit",
1189 .usage = "bitnum ('set'|'clear')",
1191 COMMAND_REGISTRATION_DONE
1193 static const struct command_registration at91sam7_command_handlers[] = {
1195 .name = "at91sam7",
1196 .mode = COMMAND_ANY,
1197 .help = "at91sam7 flash command group",
1198 .chain = at91sam7_exec_command_handlers,
1200 COMMAND_REGISTRATION_DONE
1203 struct flash_driver at91sam7_flash = {
1204 .name = "at91sam7",
1205 .commands = at91sam7_command_handlers,
1206 .flash_bank_command = at91sam7_flash_bank_command,
1207 .erase = at91sam7_erase,
1208 .protect = at91sam7_protect,
1209 .write = at91sam7_write,
1210 .read = default_flash_read,
1211 .probe = at91sam7_probe,
1212 .auto_probe = at91sam7_probe,
1213 .erase_check = at91sam7_erase_check,
1214 .protect_check = at91sam7_protect_check,
1215 .info = at91sam7_info,