at91sam3.c 63.3 KB
Newer Older
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2009 by Duane Ellis                                     *
 *   openocd@duaneellis.com                                                *
 *                                                                         *
Olaf Lüke's avatar
Olaf Lüke committed
5
6
7
8
 *   Copyright (C) 2010 by Olaf Lüke (at91sam3s* support)                  *
 *   olaf@uni-paderborn.de                                                 *
 *                                                                         *
 *                                                                         *
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE.  See the         *
 *   GNU General public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
****************************************************************************/

/* Some of the the lower level code was based on code supplied by
 * ATMEL under this copyright. */

/* BEGIN ATMEL COPYRIGHT */
/* ----------------------------------------------------------------------------
30
 *         ATMEL Microcontroller Software Support
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 * ----------------------------------------------------------------------------
 * Copyright (c) 2009, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */
/* END ATMEL COPYRIGHT */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif


Zachary T Welch's avatar
Zachary T Welch committed
64
#include "imp.h"
65
#include <helper/time_support.h>
66
67
68

#define REG_NAME_WIDTH  (12)

Olaf Lüke's avatar
Olaf Lüke committed
69
70
71
// at91sam3u series (has one or two flash banks)
#define FLASH_BANK0_BASE_U   0x00080000
#define FLASH_BANK1_BASE_U   0x00100000
72

Olaf Lüke's avatar
Olaf Lüke committed
73
74
// at91sam3s series (has always one flash bank)
#define FLASH_BANK_BASE_S   0x00400000
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

#define 	AT91C_EFC_FCMD_GETD                 (0x0) // (EFC) Get Flash Descriptor
#define 	AT91C_EFC_FCMD_WP                   (0x1) // (EFC) Write Page
#define 	AT91C_EFC_FCMD_WPL                  (0x2) // (EFC) Write Page and Lock
#define 	AT91C_EFC_FCMD_EWP                  (0x3) // (EFC) Erase Page and Write Page
#define 	AT91C_EFC_FCMD_EWPL                 (0x4) // (EFC) Erase Page and Write Page then Lock
#define 	AT91C_EFC_FCMD_EA                   (0x5) // (EFC) Erase All
// cmd6 is not present int he at91sam3u4/2/1 data sheet table 17-2
// #define 	AT91C_EFC_FCMD_EPL                  (0x6) // (EFC) Erase plane?
// cmd7 is not present int he at91sam3u4/2/1 data sheet table 17-2
// #define 	AT91C_EFC_FCMD_EPA                  (0x7) // (EFC) Erase pages?
#define 	AT91C_EFC_FCMD_SLB                  (0x8) // (EFC) Set Lock Bit
#define 	AT91C_EFC_FCMD_CLB                  (0x9) // (EFC) Clear Lock Bit
#define 	AT91C_EFC_FCMD_GLB                  (0xA) // (EFC) Get Lock Bit
#define 	AT91C_EFC_FCMD_SFB                  (0xB) // (EFC) Set Fuse Bit
#define 	AT91C_EFC_FCMD_CFB                  (0xC) // (EFC) Clear Fuse Bit
#define 	AT91C_EFC_FCMD_GFB                  (0xD) // (EFC) Get Fuse Bit
#define 	AT91C_EFC_FCMD_STUI                 (0xE) // (EFC) Start Read Unique ID
#define 	AT91C_EFC_FCMD_SPUI                 (0xF) // (EFC) Stop Read Unique ID

#define  offset_EFC_FMR   0
#define  offset_EFC_FCR   4
#define  offset_EFC_FSR   8
#define  offset_EFC_FRR   12


101
extern struct flash_driver at91sam3_flash;
102

103
static float
104
_tomhz(uint32_t freq_hz)
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
{
	float f;

	f = ((float)(freq_hz)) / 1000000.0;
	return f;
}

// How the chip is configured.
struct sam3_cfg {
	uint32_t unique_id[4];

	uint32_t slow_freq;
	uint32_t rc_freq;
	uint32_t mainosc_freq;
	uint32_t plla_freq;
	uint32_t mclk_freq;
	uint32_t cpu_freq;
	uint32_t fclk_freq;
	uint32_t pclk0_freq;
	uint32_t pclk1_freq;
	uint32_t pclk2_freq;


#define SAM3_CHIPID_CIDR          (0x400E0740)
	uint32_t CHIPID_CIDR;
#define SAM3_CHIPID_EXID          (0x400E0744)
	uint32_t CHIPID_EXID;

#define SAM3_SUPC_CR              (0x400E1210)
134
	uint32_t SUPC_CR;
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

#define SAM3_PMC_BASE             (0x400E0400)
#define SAM3_PMC_SCSR             (SAM3_PMC_BASE + 0x0008)
	uint32_t PMC_SCSR;
#define SAM3_PMC_PCSR             (SAM3_PMC_BASE + 0x0018)
	uint32_t PMC_PCSR;
#define SAM3_CKGR_UCKR            (SAM3_PMC_BASE + 0x001c)
	uint32_t CKGR_UCKR;
#define SAM3_CKGR_MOR             (SAM3_PMC_BASE + 0x0020)
	uint32_t CKGR_MOR;
#define SAM3_CKGR_MCFR            (SAM3_PMC_BASE + 0x0024)
	uint32_t CKGR_MCFR;
#define SAM3_CKGR_PLLAR           (SAM3_PMC_BASE + 0x0028)
	uint32_t CKGR_PLLAR;
#define SAM3_PMC_MCKR             (SAM3_PMC_BASE + 0x0030)
	uint32_t PMC_MCKR;
#define SAM3_PMC_PCK0             (SAM3_PMC_BASE + 0x0040)
	uint32_t PMC_PCK0;
#define SAM3_PMC_PCK1             (SAM3_PMC_BASE + 0x0044)
	uint32_t PMC_PCK1;
#define SAM3_PMC_PCK2             (SAM3_PMC_BASE + 0x0048)
	uint32_t PMC_PCK2;
#define SAM3_PMC_SR               (SAM3_PMC_BASE + 0x0068)
	uint32_t PMC_SR;
#define SAM3_PMC_IMR              (SAM3_PMC_BASE + 0x006c)
	uint32_t PMC_IMR;
#define SAM3_PMC_FSMR             (SAM3_PMC_BASE + 0x0070)
	uint32_t PMC_FSMR;
#define SAM3_PMC_FSPR             (SAM3_PMC_BASE + 0x0074)
	uint32_t PMC_FSPR;
};


struct sam3_bank_private {
	int probed;
	// DANGER: THERE ARE DRAGONS HERE..
	// NOTE: If you add more 'ghost' pointers
172
	// be aware that you must *manually* update
173
174
175
176
177
	// these pointers in the function sam3_GetDetails()
	// See the comment "Here there be dragons"

	// so we can find the chip we belong to
	struct sam3_chip *pChip;
178
	// so we can find the orginal bank pointer
179
	struct flash_bank *pBank;
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
	unsigned bank_number;
	uint32_t controller_address;
	uint32_t base_address;
	bool present;
	unsigned size_bytes;
	unsigned nsectors;
	unsigned sector_size;
	unsigned page_size;
};

struct sam3_chip_details {
	// THERE ARE DRAGONS HERE..
	// note: If you add pointers here
	// becareful about them as they
	// may need to be updated inside
	// the function: "sam3_GetDetails()
	// which copy/overwrites the
	// 'runtime' copy of this structure
	uint32_t chipid_cidr;
	const char *name;

	unsigned n_gpnvms;
#define SAM3_N_NVM_BITS 3
	unsigned  gpnvm[SAM3_N_NVM_BITS];
	unsigned  total_flash_size;
	unsigned  total_sram_size;
	unsigned  n_banks;
#define SAM3_MAX_FLASH_BANKS 2
	// these are "initialized" from the global const data
	struct sam3_bank_private bank[SAM3_MAX_FLASH_BANKS];
};


struct sam3_chip {
	struct sam3_chip *next;
	int    probed;

	// this is "initialized" from the global const structure
	struct sam3_chip_details details;
Zachary T Welch's avatar
Zachary T Welch committed
219
	struct target *target;
220
221
222
223
224
225
	struct sam3_cfg cfg;
};


struct sam3_reg_list {
	uint32_t address;  size_t struct_offset; const char *name;
226
	void (*explain_func)(struct sam3_chip *pInfo);
227
228
229
230
231
232
};


static struct sam3_chip *all_sam3_chips;

static struct sam3_chip *
233
get_current_sam3(struct command_context *cmd_ctx)
234
{
Zachary T Welch's avatar
Zachary T Welch committed
235
	struct target *t;
236
237
	static struct sam3_chip *p;

238
	t = get_current_target(cmd_ctx);
zwelch's avatar
zwelch committed
239
	if (!t) {
240
		command_print(cmd_ctx, "No current target?");
241
242
243
244
		return NULL;
	}

	p = all_sam3_chips;
zwelch's avatar
zwelch committed
245
	if (!p) {
246
247
		// this should not happen
		// the command is not registered until the chip is created?
248
		command_print(cmd_ctx, "No SAM3 chips exist?");
249
250
251
		return NULL;
	}

zwelch's avatar
zwelch committed
252
253
	while (p) {
		if (p->target == t) {
254
255
256
257
			return p;
		}
		p = p->next;
	}
258
	command_print(cmd_ctx, "Cannot find SAM3 chip?");
259
260
261
262
263
264
	return NULL;
}


// these are used to *initialize* the "pChip->details" structure.
static const struct sam3_chip_details all_sam3_details[] = {
Olaf Lüke's avatar
Olaf Lüke committed
265
	// Start at91sam3u* series
266
267
268
269
270
	{
		.chipid_cidr    = 0x28100960,
		.name           = "at91sam3u4e",
		.total_flash_size     = 256 * 1024,
		.total_sram_size      = 52 * 1024,
271
		.n_gpnvms       = 3,
272
273
274
275
276
277
		.n_banks        = 2,

		// System boots at address 0x0
		// gpnvm[1] = selects boot code
		//     if gpnvm[1] == 0
		//         boot is via "SAMBA" (rom)
278
		//     else
279
280
281
282
283
284
285
286
287
288
		//         boot is via FLASH
		//         Selection is via gpnvm[2]
		//     endif
		//
		// NOTE: banks 0 & 1 switch places
		//     if gpnvm[2] == 0
		//         Bank0 is the boot rom
		//      else
		//         Bank1 is the boot rom
		//      endif
289
290
291
//		.bank[0] = {
		{
		  {
292
293
294
295
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
Olaf Lüke's avatar
Olaf Lüke committed
296
			.base_address = FLASH_BANK0_BASE_U,
297
298
299
300
301
302
			.controller_address = 0x400e0800,
			.present = 1,
			.size_bytes = 128 * 1024,
			.nsectors   = 16,
			.sector_size = 8192,
			.page_size   = 256,
303
		  },
304

305
306
//		.bank[1] = {
		  {
307
308
309
310
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 1,
Olaf Lüke's avatar
Olaf Lüke committed
311
			.base_address = FLASH_BANK1_BASE_U,
312
313
314
315
316
317
			.controller_address = 0x400e0a00,
			.present = 1,
			.size_bytes = 128 * 1024,
			.nsectors   = 16,
			.sector_size = 8192,
			.page_size   = 256,
318
		  },
319
320
321
322
323
324
325
326
		},
	},

	{
		.chipid_cidr    = 0x281a0760,
		.name           = "at91sam3u2e",
		.total_flash_size     = 128 * 1024,
		.total_sram_size      =  36 * 1024,
327
		.n_gpnvms       = 2,
328
329
330
331
332
333
		.n_banks        = 1,

		// System boots at address 0x0
		// gpnvm[1] = selects boot code
		//     if gpnvm[1] == 0
		//         boot is via "SAMBA" (rom)
334
		//     else
335
336
337
		//         boot is via FLASH
		//         Selection is via gpnvm[2]
		//     endif
338
339
340
//		.bank[0] = {
		{
		  {
341
342
343
344
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
Olaf Lüke's avatar
Olaf Lüke committed
345
			.base_address = FLASH_BANK0_BASE_U,
346
347
348
349
350
351
			.controller_address = 0x400e0800,
			.present = 1,
			.size_bytes = 128 * 1024,
			.nsectors   = 16,
			.sector_size = 8192,
			.page_size   = 256,
352
353
354
		  },
//		  .bank[1] = {
		  {
355
356
357
			.present = 0,
			.probed = 0,
			.bank_number = 1,
358
		  },
359
360
361
362
363
364
365
		},
	},
	{
		.chipid_cidr    = 0x28190560,
		.name           = "at91sam3u1e",
		.total_flash_size     = 64 * 1024,
		.total_sram_size      = 20 * 1024,
366
		.n_gpnvms       = 2,
367
368
369
370
371
372
		.n_banks        = 1,

		// System boots at address 0x0
		// gpnvm[1] = selects boot code
		//     if gpnvm[1] == 0
		//         boot is via "SAMBA" (rom)
373
		//     else
374
375
376
377
		//         boot is via FLASH
		//         Selection is via gpnvm[2]
		//     endif
		//
378

379
380
381
//		.bank[0] = {
		{
		  {
382
383
384
385
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
Olaf Lüke's avatar
Olaf Lüke committed
386
			.base_address = FLASH_BANK0_BASE_U,
387
388
389
390
391
392
			.controller_address = 0x400e0800,
			.present = 1,
			.size_bytes =  64 * 1024,
			.nsectors   =  8,
			.sector_size = 8192,
			.page_size   = 256,
393
		  },
394

395
396
//		.bank[1] = {
		  {
397
398
399
			.present = 0,
			.probed = 0,
			.bank_number = 1,
400
		  },
401
402
		},
	},
403

404
405
406
407
408
	{
		.chipid_cidr    = 0x28000960,
		.name           = "at91sam3u4c",
		.total_flash_size     = 256 * 1024,
		.total_sram_size      = 52 * 1024,
409
		.n_gpnvms       = 3,
410
411
412
413
414
415
		.n_banks        = 2,

		// System boots at address 0x0
		// gpnvm[1] = selects boot code
		//     if gpnvm[1] == 0
		//         boot is via "SAMBA" (rom)
416
		//     else
417
418
419
420
421
422
423
424
425
426
		//         boot is via FLASH
		//         Selection is via gpnvm[2]
		//     endif
		//
		// NOTE: banks 0 & 1 switch places
		//     if gpnvm[2] == 0
		//         Bank0 is the boot rom
		//      else
		//         Bank1 is the boot rom
		//      endif
427
428
429
		{
		  {
//		.bank[0] = {
430
431
432
433
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
Olaf Lüke's avatar
Olaf Lüke committed
434
			.base_address = FLASH_BANK0_BASE_U,
435
436
437
438
439
440
			.controller_address = 0x400e0800,
			.present = 1,
			.size_bytes = 128 * 1024,
			.nsectors   = 16,
			.sector_size = 8192,
			.page_size   = 256,
441
442
443
		  },
//		.bank[1] = {
		  {
444
445
446
447
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 1,
Olaf Lüke's avatar
Olaf Lüke committed
448
			.base_address = FLASH_BANK1_BASE_U,
449
450
451
452
453
454
			.controller_address = 0x400e0a00,
			.present = 1,
			.size_bytes = 128 * 1024,
			.nsectors   = 16,
			.sector_size = 8192,
			.page_size   = 256,
455
		  },
456
457
458
459
460
461
462
463
		},
	},

	{
		.chipid_cidr    = 0x280a0760,
		.name           = "at91sam3u2c",
		.total_flash_size     = 128 * 1024,
		.total_sram_size      = 36 * 1024,
464
		.n_gpnvms       = 2,
465
466
467
468
469
470
		.n_banks        = 1,

		// System boots at address 0x0
		// gpnvm[1] = selects boot code
		//     if gpnvm[1] == 0
		//         boot is via "SAMBA" (rom)
471
		//     else
472
473
474
		//         boot is via FLASH
		//         Selection is via gpnvm[2]
		//     endif
475
476
477
		{
//		.bank[0] = {
		  {
478
479
480
481
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
Olaf Lüke's avatar
Olaf Lüke committed
482
			.base_address = FLASH_BANK0_BASE_U,
483
484
485
486
487
488
			.controller_address = 0x400e0800,
			.present = 1,
			.size_bytes = 128 * 1024,
			.nsectors   = 16,
			.sector_size = 8192,
			.page_size   = 256,
489
490
491
		  },
//		.bank[1] = {
		  {
492
493
494
			.present = 0,
			.probed = 0,
			.bank_number = 1,
495
		  },
496
497
498
499
500
501
502
		},
	},
	{
		.chipid_cidr    = 0x28090560,
		.name           = "at91sam3u1c",
		.total_flash_size     = 64 * 1024,
		.total_sram_size      = 20 * 1024,
503
		.n_gpnvms       = 2,
504
505
506
507
508
509
		.n_banks        = 1,

		// System boots at address 0x0
		// gpnvm[1] = selects boot code
		//     if gpnvm[1] == 0
		//         boot is via "SAMBA" (rom)
510
		//     else
511
512
513
514
		//         boot is via FLASH
		//         Selection is via gpnvm[2]
		//     endif
		//
515

516
517
518
		{
//		.bank[0] = {
		  {
519
520
521
522
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
Olaf Lüke's avatar
Olaf Lüke committed
523
			.base_address = FLASH_BANK0_BASE_U,
524
525
526
527
528
529
			.controller_address = 0x400e0800,
			.present = 1,
			.size_bytes =  64 * 1024,
			.nsectors   =  8,
			.sector_size = 8192,
			.page_size   = 256,
530
531
532
		  },
//		.bank[1] = {
		  {
533
534
535
			.present = 0,
			.probed = 0,
			.bank_number = 1,
536
537

		  },
538
539
540
		},
	},

Olaf Lüke's avatar
Olaf Lüke committed
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
	// Start at91sam3s* series

	// Note: The preliminary at91sam3s datasheet says on page 302
	// that the flash controller is at address 0x400E0800.
	// This is _not_ the case, the controller resides at address 0x400e0a0.
	{
		.chipid_cidr    = 0x28A00960,
		.name           = "at91sam3s4c",
		.total_flash_size     = 256 * 1024,
		.total_sram_size      = 48 * 1024,
		.n_gpnvms       = 2,
		.n_banks        = 1,
		{
//		.bank[0] = {
		  {
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
			.base_address = FLASH_BANK_BASE_S,

			.controller_address = 0x400e0a00,
			.present = 1,
			.size_bytes =  256 * 1024,
			.nsectors   =  32,
			.sector_size = 8192,
			.page_size   = 256,
		  },
//		.bank[1] = {
		  {
			.present = 0,
			.probed = 0,
			.bank_number = 1,

		  },
		},
	},

	{
		.chipid_cidr    = 0x28900960,
		.name           = "at91sam3s4b",
		.total_flash_size     = 256 * 1024,
		.total_sram_size      = 48 * 1024,
		.n_gpnvms       = 2,
		.n_banks        = 1,
		{
//		.bank[0] = {
		  {
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
			.base_address = FLASH_BANK_BASE_S,

			.controller_address = 0x400e0a00,
			.present = 1,
			.size_bytes =  256 * 1024,
			.nsectors   =  32,
			.sector_size = 8192,
			.page_size   = 256,
		  },
//		.bank[1] = {
		  {
			.present = 0,
			.probed = 0,
			.bank_number = 1,

		  },
		},
	},
	{
		.chipid_cidr    = 0x28800960,
		.name           = "at91sam3s4a",
		.total_flash_size     = 256 * 1024,
		.total_sram_size      = 48 * 1024,
		.n_gpnvms       = 2,
		.n_banks        = 1,
		{
//		.bank[0] = {
		  {
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
			.base_address = FLASH_BANK_BASE_S,

			.controller_address = 0x400e0a00,
			.present = 1,
			.size_bytes =  256 * 1024,
			.nsectors   =  32,
			.sector_size = 8192,
			.page_size   = 256,
		  },
//		.bank[1] = {
		  {
			.present = 0,
			.probed = 0,
			.bank_number = 1,

		  },
		},
	},
	{
		.chipid_cidr    = 0x28AA0760,
		.name           = "at91sam3s2c",
		.total_flash_size     = 128 * 1024,
		.total_sram_size      = 32 * 1024,
		.n_gpnvms       = 2,
		.n_banks        = 1,
		{
//		.bank[0] = {
		  {
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
			.base_address = FLASH_BANK_BASE_S,

			.controller_address = 0x400e0a00,
			.present = 1,
			.size_bytes =  128 * 1024,
			.nsectors   =  16,
			.sector_size = 8192,
			.page_size   = 256,
		  },
//		.bank[1] = {
		  {
			.present = 0,
			.probed = 0,
			.bank_number = 1,

		  },
		},
	},
	{
		.chipid_cidr    = 0x289A0760,
		.name           = "at91sam3s2b",
		.total_flash_size     = 128 * 1024,
		.total_sram_size      = 32 * 1024,
		.n_gpnvms       = 2,
		.n_banks        = 1,
		{
//		.bank[0] = {
		  {
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
			.base_address = FLASH_BANK_BASE_S,

			.controller_address = 0x400e0a00,
			.present = 1,
			.size_bytes =  128 * 1024,
			.nsectors   =  16,
			.sector_size = 8192,
			.page_size   = 256,
		  },
//		.bank[1] = {
		  {
			.present = 0,
			.probed = 0,
			.bank_number = 1,

		  },
		},
	},
	{
		.chipid_cidr    = 0x288A0760,
		.name           = "at91sam3s2a",
		.total_flash_size     = 128 * 1024,
		.total_sram_size      = 32 * 1024,
		.n_gpnvms       = 2,
		.n_banks        = 1,
		{
//		.bank[0] = {
		  {
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
			.base_address = FLASH_BANK_BASE_S,

			.controller_address = 0x400e0a00,
			.present = 1,
			.size_bytes =  128 * 1024,
			.nsectors   =  16,
			.sector_size = 8192,
			.page_size   = 256,
		  },
//		.bank[1] = {
		  {
			.present = 0,
			.probed = 0,
			.bank_number = 1,

		  },
		},
	},
	{
		.chipid_cidr    = 0x28A90560,
		.name           = "at91sam3s1c",
		.total_flash_size     = 64 * 1024,
		.total_sram_size      = 16 * 1024,
		.n_gpnvms       = 2,
		.n_banks        = 1,
		{
//		.bank[0] = {
		  {
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
			.base_address = FLASH_BANK_BASE_S,

			.controller_address = 0x400e0a00,
			.present = 1,
			.size_bytes =  64 * 1024,
			.nsectors   =  8,
			.sector_size = 8192,
			.page_size   = 256,
		  },
//		.bank[1] = {
		  {
			.present = 0,
			.probed = 0,
			.bank_number = 1,

		  },
		},
	},
	{
		.chipid_cidr    = 0x28990560,
		.name           = "at91sam3s1b",
		.total_flash_size     = 64 * 1024,
		.total_sram_size      = 16 * 1024,
		.n_gpnvms       = 2,
		.n_banks        = 1,
		{
//		.bank[0] = {
		  {
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
			.base_address = FLASH_BANK_BASE_S,

			.controller_address = 0x400e0a00,
			.present = 1,
			.size_bytes =  64 * 1024,
			.nsectors   =  8,
			.sector_size = 8192,
			.page_size   = 256,
		  },
//		.bank[1] = {
		  {
			.present = 0,
			.probed = 0,
			.bank_number = 1,

		  },
		},
	},
	{
		.chipid_cidr    = 0x28890560,
		.name           = "at91sam3s1a",
		.total_flash_size     = 64 * 1024,
		.total_sram_size      = 16 * 1024,
		.n_gpnvms       = 2,
		.n_banks        = 1,
		{
//		.bank[0] = {
		  {
			.probed = 0,
			.pChip  = NULL,
			.pBank  = NULL,
			.bank_number = 0,
			.base_address = FLASH_BANK_BASE_S,

			.controller_address = 0x400e0a00,
			.present = 1,
			.size_bytes =  64 * 1024,
			.nsectors   =  8,
			.sector_size = 8192,
			.page_size   = 256,
		  },
//		.bank[1] = {
		  {
			.present = 0,
			.probed = 0,
			.bank_number = 1,

		  },
		},
	},
835
	// terminate
836
	{
837
838
839
840
841
842
843
844
845
846
847
848
849
850
		.chipid_cidr	= 0,
		.name			= NULL,
	}
};

/* Globals above */
/***********************************************************************
 **********************************************************************
 **********************************************************************
 **********************************************************************
 **********************************************************************
 **********************************************************************/
/* *ATMEL* style code - from the SAM3 driver code */

851
852
/**
 * Get the current status of the EEFC and
853
854
855
856
 * the value of some status bits (LOCKE, PROGE).
 * @param pPrivate - info about the bank
 * @param v        - result goes here
 */
857
static int
858
EFC_GetStatus(struct sam3_bank_private *pPrivate, uint32_t *v)
859
860
{
	int r;
861
	r = target_read_u32(pPrivate->pChip->target, pPrivate->controller_address + offset_EFC_FSR, v);
862
	LOG_DEBUG("Status: 0x%08x (lockerror: %d, cmderror: %d, ready: %d)",
863
864
865
866
			  (unsigned int)(*v),
			  ((unsigned int)((*v >> 2) & 1)),
			  ((unsigned int)((*v >> 1) & 1)),
			  ((unsigned int)((*v >> 0) & 1)));
867

868
869
870
	return r;
}

871
872
/**
 * Get the result of the last executed command.
873
874
875
 * @param pPrivate - info about the bank
 * @param v        - result goes here
 */
876
static int
877
EFC_GetResult(struct sam3_bank_private *pPrivate, uint32_t *v)
878
879
880
{
	int r;
	uint32_t rv;
881
	r = target_read_u32(pPrivate->pChip->target, pPrivate->controller_address + offset_EFC_FRR, &rv);
zwelch's avatar
zwelch committed
882
	if (v) {
883
884
885
886
887
888
889
890
		*v = rv;
	}
	LOG_DEBUG("Result: 0x%08x", ((unsigned int)(rv)));
	return r;
}

static int
EFC_StartCommand(struct sam3_bank_private *pPrivate,
891
				 unsigned command, unsigned argument)
892
893
894
895
896
897
898
899
900
901
902
903
904
{
	uint32_t n,v;
	int r;
	int retry;

	retry = 0;
 do_retry:

    // Check command & argument
    switch (command) {

	case AT91C_EFC_FCMD_WP:
	case AT91C_EFC_FCMD_WPL:
905
	case AT91C_EFC_FCMD_EWP:
906
907
908
909
910
911
	case AT91C_EFC_FCMD_EWPL:
		// case AT91C_EFC_FCMD_EPL:
		// case AT91C_EFC_FCMD_EPA:
	case AT91C_EFC_FCMD_SLB:
	case AT91C_EFC_FCMD_CLB:
		n = (pPrivate->size_bytes / pPrivate->page_size);
zwelch's avatar
zwelch committed
912
		if (argument >= n) {
913
914
915
			LOG_ERROR("*BUG*: Embedded flash has only %u pages", (unsigned)(n));
		}
		break;
916

917
918
	case AT91C_EFC_FCMD_SFB:
	case AT91C_EFC_FCMD_CFB:
zwelch's avatar
zwelch committed
919
		if (argument >= pPrivate->pChip->details.n_gpnvms) {
920
			LOG_ERROR("*BUG*: Embedded flash has only %d GPNVMs",
921
					  pPrivate->pChip->details.n_gpnvms);
922
923
		}
		break;
924

925
926
927
928
929
930
	case AT91C_EFC_FCMD_GETD:
	case AT91C_EFC_FCMD_EA:
	case AT91C_EFC_FCMD_GLB:
	case AT91C_EFC_FCMD_GFB:
	case AT91C_EFC_FCMD_STUI:
	case AT91C_EFC_FCMD_SPUI:
zwelch's avatar
zwelch committed
931
		if (argument != 0) {
932
			LOG_ERROR("Argument is meaningless for cmd: %d", command);
933
934
935
936
937
938
939
		}
		break;
	default:
		LOG_ERROR("Unknown command %d", command);
		break;
    }

zwelch's avatar
zwelch committed
940
	if (command == AT91C_EFC_FCMD_SPUI) {
941
942
943
944
945
946
		// this is a very special situation.
		// Situation (1) - error/retry - see below
		//      And we are being called recursively
		// Situation (2) - normal, finished reading unique id
	} else {
		// it should be "ready"
947
		EFC_GetStatus(pPrivate, &v);
zwelch's avatar
zwelch committed
948
		if (v & 1) {
949
950
951
			// then it is ready
			// we go on
		} else {
zwelch's avatar
zwelch committed
952
			if (retry) {
953
954
				// we have done this before
				// the controller is not responding.
955
				LOG_ERROR("flash controller(%d) is not ready! Error", pPrivate->bank_number);
956
957
958
				return ERROR_FAIL;
			} else {
				retry++;
959
				LOG_ERROR("Flash controller(%d) is not ready, attempting reset",
960
						  pPrivate->bank_number);
961
				// we do that by issuing the *STOP* command
962
				EFC_StartCommand(pPrivate, AT91C_EFC_FCMD_SPUI, 0);
963
				// above is recursive, and further recursion is blocked by
964
				// if (command == AT91C_EFC_FCMD_SPUI) above
965
966
967
968
969
970
				goto do_retry;
			}
		}
	}

	v = (0x5A << 24) | (argument << 8) | command;
971
	LOG_DEBUG("Command: 0x%08x", ((unsigned int)(v)));
972
	r = target_write_u32(pPrivate->pBank->target,
973
974
						  pPrivate->controller_address + offset_EFC_FCR,
						  v);
zwelch's avatar
zwelch committed
975
	if (r != ERROR_OK) {
976
977
978
979
980
		LOG_DEBUG("Error Write failed");
	}
	return r;
}

981
982
/**
 * Performs the given command and wait until its completion (or an error).
983
984
985
986
987
 * @param pPrivate - info about the bank
 * @param command  - Command to perform.
 * @param argument - Optional command argument.
 * @param status   - put command status bits here
 */
988
989
990
991
static int
EFC_PerformCommand(struct sam3_bank_private *pPrivate,
					unsigned command,
					unsigned argument,
992
993
994
995
996
997
998
					uint32_t *status)
{

	int r;
	uint32_t v;
	long long ms_now, ms_end;

999
	// default
zwelch's avatar
zwelch committed
1000
	if (status) {
For faster browsing, not all history is shown. View entire blame