build: cleanup src/flash/nor directory
[openocd.git] / src / flash / nor / at91sam7.c
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 ****************************************************************************/
22
23 /***************************************************************************
24 *
25 * New flash setup command:
26 *
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>]
32 *
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 ****************************************************************************/
47
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #include "imp.h"
53 #include <helper/binarybuffer.h>
54
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
66
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
76
77 /* MC_FSR bit definitions */
78 #define MC_FSR_FRDY 1
79 #define MC_FSR_EOL 2
80
81 /* AT91SAM7 constants */
82 #define RC_FREQ 32000
83
84 /* Flash timing modes */
85 #define FMR_TIMING_NONE 0
86 #define FMR_TIMING_NVBITS 1
87 #define FMR_TIMING_FLASH 2
88
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
99
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);
103
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);
108
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 };
112
113 static char *EPROC[8] = {
114 "Unknown", "ARM946-E", "ARM7TDMI", "Unknown", "ARM920T", "ARM926EJ-S", "Unknown", "Unknown"
115 };
116
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;
129
130 /* flash auto-detection */
131 uint8_t flash_autodetection;
132
133 /* flash geometry */
134 uint16_t pages_per_sector;
135 uint16_t pagesize;
136 uint16_t pages_in_lockregion;
137
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;
145
146 /* 0: not init
147 * 1: fmcn for nvbits (1uS)
148 * 2: fmcn for flash (1.5uS) */
149 uint8_t flashmode;
150
151 /* main clock status */
152 uint8_t mck_valid;
153 uint32_t mck_freq;
154
155 /* external clock frequency */
156 uint32_t ext_freq;
157
158 };
159
160 #if 0
161 static long SRAMSIZ[16] = {
162 -1,
163 0x0400, /* 1K */
164 0x0800, /* 2K */
165 -1,
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 */
178 };
179 #endif
180
181 static uint32_t at91sam7_get_flash_status(struct target *target, int bank_number)
182 {
183 uint32_t fsr;
184 target_read_u32(target, MC_FSR[bank_number], &fsr);
185
186 return fsr;
187 }
188
189 /* Read clock configuration and set at91sam7_info->mck_freq */
190 static void at91sam7_read_clock_info(struct flash_bank *bank)
191 {
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;
196
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);
205
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;
213
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;
222 }
223 break;
224
225 case 2: /* Reserved */
226 break;
227
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);
245 }
246 break;
247 }
248
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;
257 }
258
259 /* Setup the timimg registers for nvbits or normal flash */
260 static void at91sam7_set_flash_mode(struct flash_bank *bank, int mode)
261 {
262 uint32_t fmr, fmcn = 0, fws = 0;
263 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
264 struct target *target = bank->target;
265
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;
275 }
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;
280 }
281
282 /* hard overclocking */
283 if (fmcn > 0xFF)
284 fmcn = 0xFF;
285
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;
292
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);
296 }
297
298 at91sam7_info->flashmode = mode;
299 }
300
301 static uint32_t at91sam7_wait_status_busy(struct flash_bank *bank, uint32_t waitbits, int timeout)
302 {
303 uint32_t status;
304
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);
309 }
310
311 LOG_DEBUG("status[%i]: 0x%" PRIx32 "", bank->bank_number, status);
312
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");
321 }
322
323 return status;
324 }
325
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)
328 {
329 uint32_t fcr;
330 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
331 struct target *target = bank->target;
332
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);
339
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;
345 }
346
347 if (at91sam7_wait_status_busy(bank, MC_FSR_FRDY, 10)&0x0C)
348 return ERROR_FLASH_OPERATION_FAILED;
349
350 return ERROR_OK;
351 }
352
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)
355 {
356 struct flash_bank *t_bank = bank;
357 struct at91sam7_flash_bank *at91sam7_info;
358 struct target *target = t_bank->target;
359
360 uint16_t bnk, sec;
361 uint16_t arch;
362 uint32_t cidr;
363 uint8_t banks_num = 0;
364 uint16_t num_nvmbits = 0;
365 uint16_t sectors_num = 0;
366 uint16_t pages_per_sector = 0;
367 uint16_t page_size = 0;
368 uint32_t ext_freq;
369 uint32_t bank_size;
370 uint32_t base_address = 0;
371 char *target_name_t = "Unknown";
372
373 at91sam7_info = t_bank->driver_priv;
374
375 if (at91sam7_info->cidr != 0) {
376 /* flash already configured, update clock and check for protected sectors */
377 struct flash_bank *fb = bank;
378 t_bank = fb;
379
380 while (t_bank) {
381 /* re-calculate master clock frequency */
382 at91sam7_read_clock_info(t_bank);
383
384 /* no timming */
385 at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
386
387 /* check protect state */
388 at91sam7_protect_check(t_bank);
389
390 t_bank = fb->next;
391 fb = t_bank;
392 }
393
394 return ERROR_OK;
395 }
396
397 /* Read and parse chip identification register */
398 target_read_u32(target, DBGU_CIDR, &cidr);
399 if (cidr == 0) {
400 LOG_WARNING("Cannot identify target as an AT91SAM");
401 return ERROR_FLASH_OPERATION_FAILED;
402 }
403
404 if (at91sam7_info->flash_autodetection == 0) {
405 /* banks and sectors are already created, based on data from input file */
406 struct flash_bank *fb = bank;
407 t_bank = fb;
408 while (t_bank) {
409 at91sam7_info = t_bank->driver_priv;
410
411 at91sam7_info->cidr = cidr;
412 at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
413 at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
414 at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
415 at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
416 at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
417 at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
418 at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
419 at91sam7_info->cidr_version = cidr&0x001F;
420
421 /* calculate master clock frequency */
422 at91sam7_read_clock_info(t_bank);
423
424 /* no timming */
425 at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
426
427 /* check protect state */
428 at91sam7_protect_check(t_bank);
429
430 t_bank = fb->next;
431 fb = t_bank;
432 }
433
434 return ERROR_OK;
435 }
436
437 arch = (cidr >> 20)&0x00FF;
438
439 /* check flash size */
440 switch ((cidr >> 8)&0x000F) {
441 case FLASH_SIZE_8KB:
442 break;
443
444 case FLASH_SIZE_16KB:
445 banks_num = 1;
446 sectors_num = 8;
447 pages_per_sector = 32;
448 page_size = 64;
449 base_address = 0x00100000;
450 if (arch == 0x70) {
451 num_nvmbits = 2;
452 target_name_t = "AT91SAM7S161/16";
453 }
454 break;
455
456 case FLASH_SIZE_32KB:
457 banks_num = 1;
458 sectors_num = 8;
459 pages_per_sector = 32;
460 page_size = 128;
461 base_address = 0x00100000;
462 if (arch == 0x70) {
463 num_nvmbits = 2;
464 target_name_t = "AT91SAM7S321/32";
465 }
466 if (arch == 0x72) {
467 num_nvmbits = 3;
468 target_name_t = "AT91SAM7SE32";
469 }
470 break;
471
472 case FLASH_SIZE_64KB:
473 banks_num = 1;
474 sectors_num = 16;
475 pages_per_sector = 32;
476 page_size = 128;
477 base_address = 0x00100000;
478 if (arch == 0x70) {
479 num_nvmbits = 2;
480 target_name_t = "AT91SAM7S64";
481 }
482 break;
483
484 case FLASH_SIZE_128KB:
485 banks_num = 1;
486 sectors_num = 8;
487 pages_per_sector = 64;
488 page_size = 256;
489 base_address = 0x00100000;
490 if (arch == 0x70) {
491 num_nvmbits = 2;
492 target_name_t = "AT91SAM7S128";
493 }
494 if (arch == 0x71) {
495 num_nvmbits = 3;
496 target_name_t = "AT91SAM7XC128";
497 }
498 if (arch == 0x72) {
499 num_nvmbits = 3;
500 target_name_t = "AT91SAM7SE128";
501 }
502 if (arch == 0x75) {
503 num_nvmbits = 3;
504 target_name_t = "AT91SAM7X128";
505 }
506 break;
507
508 case FLASH_SIZE_256KB:
509 banks_num = 1;
510 sectors_num = 16;
511 pages_per_sector = 64;
512 page_size = 256;
513 base_address = 0x00100000;
514 if (arch == 0x60) {
515 num_nvmbits = 3;
516 target_name_t = "AT91SAM7A3";
517 }
518 if (arch == 0x70) {
519 num_nvmbits = 2;
520 target_name_t = "AT91SAM7S256";
521 }
522 if (arch == 0x71) {
523 num_nvmbits = 3;
524 target_name_t = "AT91SAM7XC256";
525 }
526 if (arch == 0x72) {
527 num_nvmbits = 3;
528 target_name_t = "AT91SAM7SE256";
529 }
530 if (arch == 0x75) {
531 num_nvmbits = 3;
532 target_name_t = "AT91SAM7X256";
533 }
534 break;
535
536 case FLASH_SIZE_512KB:
537 banks_num = 2;
538 sectors_num = 16;
539 pages_per_sector = 64;
540 page_size = 256;
541 base_address = 0x00100000;
542 if (arch == 0x70) {
543 num_nvmbits = 2;
544 target_name_t = "AT91SAM7S512";
545 }
546 if (arch == 0x71) {
547 num_nvmbits = 3;
548 target_name_t = "AT91SAM7XC512";
549 }
550 if (arch == 0x72) {
551 num_nvmbits = 3;
552 target_name_t = "AT91SAM7SE512";
553 }
554 if (arch == 0x75) {
555 num_nvmbits = 3;
556 target_name_t = "AT91SAM7X512";
557 }
558 break;
559
560 case FLASH_SIZE_1024KB:
561 break;
562
563 case FLASH_SIZE_2048KB:
564 break;
565 }
566
567 if (strcmp(target_name_t, "Unknown") == 0) {
568 LOG_ERROR(
569 "Target autodetection failed! Please specify target parameters in configuration file");
570 return ERROR_FLASH_OPERATION_FAILED;
571 }
572
573 ext_freq = at91sam7_info->ext_freq;
574
575 /* calculate bank size */
576 bank_size = sectors_num * pages_per_sector * page_size;
577
578 for (bnk = 0; bnk < banks_num; bnk++) {
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;
588
589 /* link created bank in 'flash_banks' list */
590 t_bank->next = fb;
591 }
592 t_bank = t_bank->next;
593 }
594
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;
601
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;
609 }
610
611 at91sam7_info = t_bank->driver_priv;
612
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;
622
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;
630
631 /* calculate master clock frequency */
632 at91sam7_read_clock_info(t_bank);
633
634 /* no timming */
635 at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
636
637 /* check protect state */
638 at91sam7_protect_check(t_bank);
639 }
640
641 LOG_DEBUG("nvptyp: 0x%3.3x, arch: 0x%4.4x",
642 at91sam7_info->cidr_nvptyp,
643 at91sam7_info->cidr_arch);
644
645 return ERROR_OK;
646 }
647
648 static int at91sam7_erase_check(struct flash_bank *bank)
649 {
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;
657
658 if (bank->target->state != TARGET_HALTED) {
659 LOG_ERROR("Target not halted");
660 return ERROR_TARGET_NOT_HALTED;
661 }
662
663 /* Configure the flash controller timing */
664 at91sam7_read_clock_info(bank);
665 at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
666
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;
676 }
677 if (blank == 0xFF)
678 bank->sectors[nSector].is_erased = 1;
679 else
680 bank->sectors[nSector].is_erased = 0;
681 }
682
683 if (fast_check)
684 return ERROR_OK;
685
686 LOG_USER("Running slow fallback erase check - add working memory");
687
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;
695
696 for (nByte = 0; nByte < bank->sectors[nSector].size; nByte++) {
697 if (buffer[nByte] != 0xFF) {
698 bank->sectors[nSector].is_erased = 0;
699 break;
700 }
701 }
702 }
703 free(buffer);
704
705 return ERROR_OK;
706 }
707
708 static int at91sam7_protect_check(struct flash_bank *bank)
709 {
710 uint8_t lock_pos, gpnvm_pos;
711 uint32_t status;
712
713 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
714
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;
720 }
721
722 status = at91sam7_get_flash_status(bank->target, bank->bank_number);
723 at91sam7_info->lockbits = (status >> 16);
724
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;
732 }
733
734 /* GPNVM and SECURITY bits apply only for MC_FSR of EFC0 */
735 status = at91sam7_get_flash_status(bank->target, 0);
736
737 at91sam7_info->securitybit = (status >> 4)&0x01;
738 at91sam7_info->nvmbits = (status >> 8)&0xFF;
739
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++;
744 }
745
746 return ERROR_OK;
747 }
748
749 FLASH_BANK_COMMAND_HANDLER(at91sam7_flash_bank_command)
750 {
751 struct flash_bank *t_bank = bank;
752 struct at91sam7_flash_bank *at91sam7_info;
753 struct target *target = t_bank->target;
754
755 uint32_t base_address;
756 uint32_t bank_size;
757 uint32_t ext_freq = 0;
758
759 int chip_width;
760 int bus_width;
761 int banks_num;
762 int num_sectors;
763
764 uint16_t pages_per_sector;
765 uint16_t page_size;
766 uint16_t num_nvmbits;
767
768 char *target_name_t;
769
770 int bnk, sec;
771
772 at91sam7_info = malloc(sizeof(struct at91sam7_flash_bank));
773 t_bank->driver_priv = at91sam7_info;
774
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;
780
781 if (CMD_ARGC < 13) {
782 at91sam7_info->flash_autodetection = 1;
783 return ERROR_OK;
784 }
785
786 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], base_address);
787
788 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], chip_width);
789 COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], bus_width);
790
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);
796
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;
802 }
803
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;
808 }
809
810 target_name_t = calloc(strlen(CMD_ARGV[7]) + 1, sizeof(char));
811 strcpy(target_name_t, CMD_ARGV[7]);
812
813 /* calculate bank size */
814 bank_size = num_sectors * pages_per_sector * page_size;
815
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;
826
827 /* link created bank in 'flash_banks' list */
828 t_bank->next = fb;
829 }
830 t_bank = t_bank->next;
831 }
832
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;
839
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;
847 }
848
849 at91sam7_info = t_bank->driver_priv;
850
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;
858 }
859
860 return ERROR_OK;
861 }
862
863 static int at91sam7_erase(struct flash_bank *bank, int first, int last)
864 {
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;
870
871 if (at91sam7_info->cidr == 0)
872 return ERROR_FLASH_BANK_NOT_PROBED;
873
874 if (bank->target->state != TARGET_HALTED) {
875 LOG_ERROR("Target not halted");
876 return ERROR_TARGET_NOT_HALTED;
877 }
878
879 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
880 return ERROR_FLASH_SECTOR_INVALID;
881
882 erase_all = 0;
883 if ((first == 0) && (last == (bank->num_sectors-1)))
884 erase_all = 1;
885
886 /* Configure the flash controller timing */
887 at91sam7_read_clock_info(bank);
888 at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
889
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;
899
900 if (at91sam7_write(bank, buffer, bank->sectors[first].offset, nbytes) != ERROR_OK)
901 return ERROR_FLASH_OPERATION_FAILED;
902
903 free(buffer);
904 }
905
906 /* mark erased sectors */
907 for (sec = first; sec <= last; sec++)
908 bank->sectors[sec].is_erased = 1;
909
910 return ERROR_OK;
911 }
912
913 static int at91sam7_protect(struct flash_bank *bank, int set, int first, int last)
914 {
915 uint32_t cmd;
916 int sector;
917 uint32_t pagen;
918
919 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
920
921 if (at91sam7_info->cidr == 0)
922 return ERROR_FLASH_BANK_NOT_PROBED;
923
924 if (bank->target->state != TARGET_HALTED) {
925 LOG_ERROR("Target not halted");
926 return ERROR_TARGET_NOT_HALTED;
927 }
928
929 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
930 return ERROR_FLASH_SECTOR_INVALID;
931
932 /* Configure the flash controller timing */
933 at91sam7_read_clock_info(bank);
934 at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);
935
936 for (sector = first; sector <= last; sector++) {
937 if (set)
938 cmd = SLB;
939 else
940 cmd = CLB;
941
942 /* if we lock a page from one sector then entire sector will be locked, also,
943 * if we unlock a page from a locked sector, entire sector will be unlocked */
944 pagen = sector * at91sam7_info->pages_per_sector;
945
946 if (at91sam7_flash_command(bank, cmd, pagen) != ERROR_OK)
947 return ERROR_FLASH_OPERATION_FAILED;
948 }
949
950 at91sam7_protect_check(bank);
951
952 return ERROR_OK;
953 }
954
955 static int at91sam7_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
956 {
957 int retval;
958 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
959 struct target *target = bank->target;
960 uint32_t dst_min_alignment, wcount, bytes_remaining = count;
961 uint32_t first_page, last_page, pagen, buffer_pos;
962
963 if (at91sam7_info->cidr == 0)
964 return ERROR_FLASH_BANK_NOT_PROBED;
965
966 if (bank->target->state != TARGET_HALTED) {
967 LOG_ERROR("Target not halted");
968 return ERROR_TARGET_NOT_HALTED;
969 }
970
971 if (offset + count > bank->size)
972 return ERROR_FLASH_DST_OUT_OF_BANK;
973
974 dst_min_alignment = at91sam7_info->pagesize;
975
976 if (offset % dst_min_alignment) {
977 LOG_WARNING("offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32 "",
978 offset,
979 dst_min_alignment);
980 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
981 }
982
983 if (at91sam7_info->cidr_arch == 0)
984 return ERROR_FLASH_BANK_NOT_PROBED;
985
986 first_page = offset/dst_min_alignment;
987 last_page = DIV_ROUND_UP(offset + count, dst_min_alignment);
988
989 LOG_DEBUG("first_page: %i, last_page: %i, count %i",
990 (int)first_page,
991 (int)last_page,
992 (int)count);
993
994 /* Configure the flash controller timing */
995 at91sam7_read_clock_info(bank);
996 at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
997
998 for (pagen = first_page; pagen < last_page; pagen++) {
999 if (bytes_remaining < dst_min_alignment)
1000 count = bytes_remaining;
1001 else
1002 count = dst_min_alignment;
1003 bytes_remaining -= count;
1004
1005 /* Write one block to the PageWriteBuffer */
1006 buffer_pos = (pagen-first_page)*dst_min_alignment;
1007 wcount = DIV_ROUND_UP(count, 4);
1008 retval = target_write_memory(target, bank->base + pagen*dst_min_alignment, 4,
1009 wcount, buffer + buffer_pos);
1010 if (retval != ERROR_OK)
1011 return retval;
1012
1013 /* Send Write Page command to Flash Controller */
1014 if (at91sam7_flash_command(bank, WP, pagen) != ERROR_OK)
1015 return ERROR_FLASH_OPERATION_FAILED;
1016 LOG_DEBUG("Write flash bank:%i page number:%" PRIi32 "", bank->bank_number, pagen);
1017 }
1018
1019 return ERROR_OK;
1020 }
1021
1022 static int at91sam7_probe(struct flash_bank *bank)
1023 {
1024 /* we can't probe on an at91sam7
1025 * if this is an at91sam7, it has the configured flash */
1026 int retval;
1027
1028 if (bank->target->state != TARGET_HALTED) {
1029 LOG_ERROR("Target not halted");
1030 return ERROR_TARGET_NOT_HALTED;
1031 }
1032
1033 retval = at91sam7_read_part_info(bank);
1034 if (retval != ERROR_OK)
1035 return retval;
1036
1037 return ERROR_OK;
1038 }
1039
1040 static int get_at91sam7_info(struct flash_bank *bank, char *buf, int buf_size)
1041 {
1042 int printed;
1043 struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
1044
1045 if (at91sam7_info->cidr == 0)
1046 return ERROR_FLASH_BANK_NOT_PROBED;
1047
1048 printed = snprintf(buf, buf_size,
1049 "\n at91sam7 driver information: Chip is %s\n",
1050 at91sam7_info->target_name);
1051
1052 buf += printed;
1053 buf_size -= printed;
1054
1055 printed = snprintf(buf,
1056 buf_size,
1057 " Cidr: 0x%8.8" PRIx32 " | Arch: 0x%4.4x | Eproc: %s | Version: 0x%3.3x | "
1058 "Flashsize: 0x%8.8" PRIx32 "\n",
1059 at91sam7_info->cidr,
1060 at91sam7_info->cidr_arch,
1061 EPROC[at91sam7_info->cidr_eproc],
1062 at91sam7_info->cidr_version,
1063 bank->size);
1064
1065 buf += printed;
1066 buf_size -= printed;
1067
1068 printed = snprintf(buf, buf_size,
1069 " Master clock (estimated): %u KHz | External clock: %u KHz\n",
1070 (unsigned)(at91sam7_info->mck_freq / 1000),
1071 (unsigned)(at91sam7_info->ext_freq / 1000));
1072
1073 buf += printed;
1074 buf_size -= printed;
1075
1076 printed = snprintf(buf,
1077 buf_size,
1078 " Pagesize: %i bytes | Lockbits(%i): %i 0x%4.4x | Pages in lock region: %i\n",
1079 at91sam7_info->pagesize,
1080 bank->num_sectors,
1081 at91sam7_info->num_lockbits_on,
1082 at91sam7_info->lockbits,
1083 at91sam7_info->pages_per_sector*at91sam7_info->num_lockbits_on);
1084
1085 buf += printed;
1086 buf_size -= printed;
1087
1088 snprintf(buf, buf_size,
1089 " Securitybit: %i | Nvmbits(%i): %i 0x%1.1x\n",
1090 at91sam7_info->securitybit, at91sam7_info->num_nvmbits,
1091 at91sam7_info->num_nvmbits_on, at91sam7_info->nvmbits);
1092
1093 return ERROR_OK;
1094 }
1095
1096 /*
1097 * On AT91SAM7S: When the gpnvm bits are set with
1098 * > at91sam7 gpnvm bitnr set
1099 * the changes are not visible in the flash controller status register MC_FSR
1100 * until the processor has been reset.
1101 * On the Olimex board this requires a power cycle.
1102 * Note that the AT91SAM7S has the following errata (doc6175.pdf sec 14.1.3):
1103 * The maximum number of write/erase cycles for Non volatile Memory bits is 100. this includes
1104 * Lock Bits (LOCKx), General Purpose NVM bits (GPNVMx) and the Security Bit.
1105 */
1106 COMMAND_HANDLER(at91sam7_handle_gpnvm_command)
1107 {
1108 struct flash_bank *bank;
1109 int bit;
1110 uint8_t flashcmd;
1111 uint32_t status;
1112 struct at91sam7_flash_bank *at91sam7_info;
1113 int retval;
1114
1115 if (CMD_ARGC != 2)
1116 return ERROR_COMMAND_SYNTAX_ERROR;
1117
1118 bank = get_flash_bank_by_num_noprobe(0);
1119 if (bank == NULL)
1120 return ERROR_FLASH_BANK_INVALID;
1121 if (strcmp(bank->driver->name, "at91sam7")) {
1122 command_print(CMD_CTX, "not an at91sam7 flash bank '%s'", CMD_ARGV[0]);
1123 return ERROR_FLASH_BANK_INVALID;
1124 }
1125 if (bank->target->state != TARGET_HALTED) {
1126 LOG_ERROR("target has to be halted to perform flash operation");
1127 return ERROR_TARGET_NOT_HALTED;
1128 }
1129
1130 if (strcmp(CMD_ARGV[1], "set") == 0)
1131 flashcmd = SGPB;
1132 else if (strcmp(CMD_ARGV[1], "clear") == 0)
1133 flashcmd = CGPB;
1134 else
1135 return ERROR_COMMAND_SYNTAX_ERROR;
1136
1137 at91sam7_info = bank->driver_priv;
1138 if (at91sam7_info->cidr == 0) {
1139 retval = at91sam7_read_part_info(bank);
1140 if (retval != ERROR_OK)
1141 return retval;
1142 }
1143
1144 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], bit);
1145 if ((bit < 0) || (bit >= at91sam7_info->num_nvmbits)) {
1146 command_print(CMD_CTX,
1147 "gpnvm bit '#%s' is out of bounds for target %s",
1148 CMD_ARGV[0],
1149 at91sam7_info->target_name);
1150 return ERROR_OK;
1151 }
1152
1153 /* Configure the flash controller timing */
1154 at91sam7_read_clock_info(bank);
1155 at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);
1156
1157 if (at91sam7_flash_command(bank, flashcmd, bit) != ERROR_OK)
1158 return ERROR_FLASH_OPERATION_FAILED;
1159
1160 /* GPNVM and SECURITY bits apply only for MC_FSR of EFC0 */
1161 status = at91sam7_get_flash_status(bank->target, 0);
1162 LOG_DEBUG("at91sam7_handle_gpnvm_command: cmd 0x%x, value %d, status 0x%" PRIx32,
1163 flashcmd,
1164 bit,
1165 status);
1166
1167 /* check protect state */
1168 at91sam7_protect_check(bank);
1169
1170 return ERROR_OK;
1171 }
1172
1173 static const struct command_registration at91sam7_exec_command_handlers[] = {
1174 {
1175 .name = "gpnvm",
1176 .handler = at91sam7_handle_gpnvm_command,
1177 .mode = COMMAND_EXEC,
1178 .help = "set or clear one General Purpose Non-Volatile Memory "
1179 "(gpnvm) bit",
1180 .usage = "bitnum ('set'|'clear')",
1181 },
1182 COMMAND_REGISTRATION_DONE
1183 };
1184 static const struct command_registration at91sam7_command_handlers[] = {
1185 {
1186 .name = "at91sam7",
1187 .mode = COMMAND_ANY,
1188 .help = "at91sam7 flash command group",
1189 .usage = "",
1190 .chain = at91sam7_exec_command_handlers,
1191 },
1192 COMMAND_REGISTRATION_DONE
1193 };
1194
1195 struct flash_driver at91sam7_flash = {
1196 .name = "at91sam7",
1197 .usage = "gpnvm <bit> <set | clear>",
1198 .commands = at91sam7_command_handlers,
1199 .flash_bank_command = at91sam7_flash_bank_command,
1200 .erase = at91sam7_erase,
1201 .protect = at91sam7_protect,
1202 .write = at91sam7_write,
1203 .read = default_flash_read,
1204 .probe = at91sam7_probe,
1205 .auto_probe = at91sam7_probe,
1206 .erase_check = at91sam7_erase_check,
1207 .protect_check = at91sam7_protect_check,
1208 .info = get_at91sam7_info,
1209 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)