25 #define REG_NAME_WIDTH (12)
28 #define FLASH_BANK_BASE_S 0x00400000
29 #define FLASH_BANK_BASE_C 0x01000000
32 #define FLASH_BANK0_BASE_SD FLASH_BANK_BASE_S
34 #define FLASH_BANK1_BASE_1024K_SD (FLASH_BANK0_BASE_SD+(1024*1024/2))
36 #define FLASH_BANK1_BASE_2048K_SD (FLASH_BANK0_BASE_SD+(2048*1024/2))
39 #define FLASH_BANK0_BASE_C32 FLASH_BANK_BASE_C
40 #define FLASH_BANK1_BASE_C32 (FLASH_BANK_BASE_C+(2048*1024/2))
42 #define AT91C_EFC_FCMD_GETD (0x0)
43 #define AT91C_EFC_FCMD_WP (0x1)
44 #define AT91C_EFC_FCMD_WPL (0x2)
45 #define AT91C_EFC_FCMD_EWP (0x3)
46 #define AT91C_EFC_FCMD_EWPL (0x4)
47 #define AT91C_EFC_FCMD_EA (0x5)
50 #define AT91C_EFC_FCMD_EPA (0x7)
51 #define AT91C_EFC_FCMD_SLB (0x8)
52 #define AT91C_EFC_FCMD_CLB (0x9)
53 #define AT91C_EFC_FCMD_GLB (0xA)
54 #define AT91C_EFC_FCMD_SFB (0xB)
55 #define AT91C_EFC_FCMD_CFB (0xC)
56 #define AT91C_EFC_FCMD_GFB (0xD)
57 #define AT91C_EFC_FCMD_STUI (0xE)
58 #define AT91C_EFC_FCMD_SPUI (0xF)
60 #define OFFSET_EFC_FMR 0
61 #define OFFSET_EFC_FCR 4
62 #define OFFSET_EFC_FSR 8
63 #define OFFSET_EFC_FRR 12
67 static float _tomhz(uint32_t freq_hz)
71 f = ((float)(freq_hz)) / 1000000.0;
91 #define SAM4_CHIPID_CIDR (0x400E0740)
93 #define SAM4_CHIPID_EXID (0x400E0744)
96 #define SAM4_PMC_BASE (0x400E0400)
97 #define SAM4_PMC_SCSR (SAM4_PMC_BASE + 0x0008)
99 #define SAM4_PMC_PCSR (SAM4_PMC_BASE + 0x0018)
101 #define SAM4_CKGR_UCKR (SAM4_PMC_BASE + 0x001c)
103 #define SAM4_CKGR_MOR (SAM4_PMC_BASE + 0x0020)
105 #define SAM4_CKGR_MCFR (SAM4_PMC_BASE + 0x0024)
107 #define SAM4_CKGR_PLLAR (SAM4_PMC_BASE + 0x0028)
109 #define SAM4_PMC_MCKR (SAM4_PMC_BASE + 0x0030)
111 #define SAM4_PMC_PCK0 (SAM4_PMC_BASE + 0x0040)
113 #define SAM4_PMC_PCK1 (SAM4_PMC_BASE + 0x0044)
115 #define SAM4_PMC_PCK2 (SAM4_PMC_BASE + 0x0048)
117 #define SAM4_PMC_SR (SAM4_PMC_BASE + 0x0068)
119 #define SAM4_PMC_IMR (SAM4_PMC_BASE + 0x006c)
121 #define SAM4_PMC_FSMR (SAM4_PMC_BASE + 0x0070)
123 #define SAM4_PMC_FSPR (SAM4_PMC_BASE + 0x0074)
162 #define SAM4_N_NVM_BITS 3
167 #define SAM4_MAX_FLASH_BANKS 2
229 .name =
"at91sam4c32e",
230 .total_flash_size = 2024 * 1024,
231 .total_sram_size = 256 * 1024,
242 .controller_address = 0x400e0a00,
243 .flash_wait_states = 5,
245 .size_bytes = 1024 * 1024,
257 .controller_address = 0x400e0c00,
258 .flash_wait_states = 5,
260 .size_bytes = 1024 * 1024,
269 .chipid_cidr = 0xA64D0EE0,
270 .name =
"at91sam4c32c",
271 .total_flash_size = 2024 * 1024,
272 .total_sram_size = 256 * 1024,
283 .controller_address = 0x400e0a00,
284 .flash_wait_states = 5,
286 .size_bytes = 1024 * 1024,
298 .controller_address = 0x400e0c00,
299 .flash_wait_states = 5,
301 .size_bytes = 1024 * 1024,
310 .chipid_cidr = 0xA64C0CE0,
311 .name =
"at91sam4c16c",
312 .total_flash_size = 1024 * 1024,
313 .total_sram_size = 128 * 1024,
324 .controller_address = 0x400e0a00,
325 .flash_wait_states = 5,
327 .size_bytes = 1024 * 1024,
343 .chipid_cidr = 0xA64C0AE0,
344 .name =
"at91sam4c8c",
345 .total_flash_size = 512 * 1024,
346 .total_sram_size = 128 * 1024,
357 .controller_address = 0x400e0a00,
358 .flash_wait_states = 5,
360 .size_bytes = 512 * 1024,
376 .chipid_cidr = 0xA64C0CE5,
377 .name =
"at91sam4c4c",
378 .total_flash_size = 256 * 1024,
379 .total_sram_size = 128 * 1024,
390 .controller_address = 0x400e0a00,
391 .flash_wait_states = 5,
393 .size_bytes = 256 * 1024,
411 .chipid_cidr = 0xA3CC0CE0,
412 .name =
"at91sam4e16e",
413 .total_flash_size = 1024 * 1024,
414 .total_sram_size = 128 * 1024,
425 .controller_address = 0x400e0a00,
426 .flash_wait_states = 5,
428 .size_bytes = 1024 * 1024,
446 .chipid_cidr = 0x293B0AE0,
447 .name =
"at91sam4n8a",
448 .total_flash_size = 512 * 1024,
449 .total_sram_size = 64 * 1024,
460 .controller_address = 0x400e0a00,
461 .flash_wait_states = 5,
463 .size_bytes = 512 * 1024,
479 .chipid_cidr = 0x294B0AE0,
480 .name =
"at91sam4n8b",
481 .total_flash_size = 512 * 1024,
482 .total_sram_size = 64 * 1024,
493 .controller_address = 0x400e0a00,
494 .flash_wait_states = 5,
496 .size_bytes = 512 * 1024,
512 .chipid_cidr = 0x295B0AE0,
513 .name =
"at91sam4n8c",
514 .total_flash_size = 512 * 1024,
515 .total_sram_size = 64 * 1024,
526 .controller_address = 0x400e0a00,
527 .flash_wait_states = 5,
529 .size_bytes = 512 * 1024,
545 .chipid_cidr = 0x29460CE0,
546 .name =
"at91sam4n16b",
547 .total_flash_size = 1024 * 1024,
548 .total_sram_size = 80 * 1024,
559 .controller_address = 0x400e0a00,
560 .flash_wait_states = 5,
562 .size_bytes = 1024 * 1024,
578 .chipid_cidr = 0x29560CE0,
579 .name =
"at91sam4n16c",
580 .total_flash_size = 1024 * 1024,
581 .total_sram_size = 80 * 1024,
592 .controller_address = 0x400e0a00,
593 .flash_wait_states = 5,
595 .size_bytes = 1024 * 1024,
613 .chipid_cidr = 0x28AC0CE0,
614 .name =
"at91sam4s16c",
615 .total_flash_size = 1024 * 1024,
616 .total_sram_size = 128 * 1024,
627 .controller_address = 0x400e0a00,
628 .flash_wait_states = 5,
630 .size_bytes = 1024 * 1024,
646 .chipid_cidr = 0x28a70ce0,
647 .name =
"at91sam4sa16c",
648 .total_flash_size = 1024 * 1024,
649 .total_sram_size = 160 * 1024,
661 .controller_address = 0x400e0a00,
662 .flash_wait_states = 5,
664 .size_bytes = 1024 * 1024,
680 .chipid_cidr = 0x289C0CE0,
681 .name =
"at91sam4s16b",
682 .total_flash_size = 1024 * 1024,
683 .total_sram_size = 128 * 1024,
694 .controller_address = 0x400e0a00,
695 .flash_wait_states = 5,
697 .size_bytes = 1024 * 1024,
713 .chipid_cidr = 0x28970CE0,
714 .name =
"at91sam4sa16b",
715 .total_flash_size = 1024 * 1024,
716 .total_sram_size = 160 * 1024,
727 .controller_address = 0x400e0a00,
728 .flash_wait_states = 5,
730 .size_bytes = 1024 * 1024,
746 .chipid_cidr = 0x288C0CE0,
747 .name =
"at91sam4s16a",
748 .total_flash_size = 1024 * 1024,
749 .total_sram_size = 128 * 1024,
760 .controller_address = 0x400e0a00,
761 .flash_wait_states = 5,
763 .size_bytes = 1024 * 1024,
779 .chipid_cidr = 0x28AC0AE0,
780 .name =
"at91sam4s8c",
781 .total_flash_size = 512 * 1024,
782 .total_sram_size = 128 * 1024,
793 .controller_address = 0x400e0a00,
794 .flash_wait_states = 5,
796 .size_bytes = 512 * 1024,
812 .chipid_cidr = 0x289C0AE0,
813 .name =
"at91sam4s8b",
814 .total_flash_size = 512 * 1024,
815 .total_sram_size = 128 * 1024,
826 .controller_address = 0x400e0a00,
827 .flash_wait_states = 5,
829 .size_bytes = 512 * 1024,
845 .chipid_cidr = 0x288C0AE0,
846 .name =
"at91sam4s8a",
847 .total_flash_size = 512 * 1024,
848 .total_sram_size = 128 * 1024,
859 .controller_address = 0x400e0a00,
860 .flash_wait_states = 5,
862 .size_bytes = 512 * 1024,
879 .chipid_cidr = 0x28ab09e0,
880 .name =
"at91sam4s4c",
881 .total_flash_size = 256 * 1024,
882 .total_sram_size = 64 * 1024,
893 .controller_address = 0x400e0a00,
894 .flash_wait_states = 5,
896 .size_bytes = 256 * 1024,
913 .chipid_cidr = 0x289b09e0,
914 .name =
"at91sam4s4b",
915 .total_flash_size = 256 * 1024,
916 .total_sram_size = 64 * 1024,
927 .controller_address = 0x400e0a00,
928 .flash_wait_states = 5,
930 .size_bytes = 256 * 1024,
947 .chipid_cidr = 0x288b09e0,
948 .name =
"at91sam4s4a",
949 .total_flash_size = 256 * 1024,
950 .total_sram_size = 64 * 1024,
961 .controller_address = 0x400e0a00,
962 .flash_wait_states = 5,
964 .size_bytes = 256 * 1024,
981 .chipid_cidr = 0x28ab07e0,
982 .name =
"at91sam4s2c",
983 .total_flash_size = 128 * 1024,
984 .total_sram_size = 64 * 1024,
995 .controller_address = 0x400e0a00,
996 .flash_wait_states = 5,
998 .size_bytes = 128 * 1024,
1000 .sector_size = 8192,
1015 .chipid_cidr = 0x289b07e0,
1016 .name =
"at91sam4s2b",
1017 .total_flash_size = 128 * 1024,
1018 .total_sram_size = 64 * 1024,
1029 .controller_address = 0x400e0a00,
1030 .flash_wait_states = 5,
1032 .size_bytes = 128 * 1024,
1034 .sector_size = 8192,
1049 .chipid_cidr = 0x288b07e0,
1050 .name =
"at91sam4s2a",
1051 .total_flash_size = 128 * 1024,
1052 .total_sram_size = 64 * 1024,
1063 .controller_address = 0x400e0a00,
1064 .flash_wait_states = 5,
1066 .size_bytes = 128 * 1024,
1068 .sector_size = 8192,
1083 .chipid_cidr = 0x29a70ee0,
1084 .name =
"at91sam4sd32c",
1085 .total_flash_size = 2048 * 1024,
1086 .total_sram_size = 160 * 1024,
1098 .controller_address = 0x400e0a00,
1099 .flash_wait_states = 5,
1101 .size_bytes = 1024 * 1024,
1103 .sector_size = 8192,
1114 .controller_address = 0x400e0c00,
1115 .flash_wait_states = 5,
1117 .size_bytes = 1024 * 1024,
1119 .sector_size = 8192,
1127 .chipid_cidr = 0x29970ee0,
1128 .name =
"at91sam4sd32b",
1129 .total_flash_size = 2048 * 1024,
1130 .total_sram_size = 160 * 1024,
1142 .controller_address = 0x400e0a00,
1143 .flash_wait_states = 5,
1145 .size_bytes = 1024 * 1024,
1147 .sector_size = 8192,
1158 .controller_address = 0x400e0c00,
1159 .flash_wait_states = 5,
1161 .size_bytes = 1024 * 1024,
1163 .sector_size = 8192,
1171 .chipid_cidr = 0x29a70ce0,
1172 .name =
"at91sam4sd16c",
1173 .total_flash_size = 1024 * 1024,
1174 .total_sram_size = 160 * 1024,
1186 .controller_address = 0x400e0a00,
1187 .flash_wait_states = 5,
1189 .size_bytes = 512 * 1024,
1191 .sector_size = 8192,
1202 .controller_address = 0x400e0c00,
1203 .flash_wait_states = 5,
1205 .size_bytes = 512 * 1024,
1207 .sector_size = 8192,
1215 .chipid_cidr = 0x29970ce0,
1216 .name =
"at91sam4sd16b",
1217 .total_flash_size = 1024 * 1024,
1218 .total_sram_size = 160 * 1024,
1230 .controller_address = 0x400e0a00,
1231 .flash_wait_states = 5,
1233 .size_bytes = 512 * 1024,
1235 .sector_size = 8192,
1246 .controller_address = 0x400e0c00,
1247 .flash_wait_states = 5,
1249 .size_bytes = 512 * 1024,
1251 .sector_size = 8192,
1259 .chipid_cidr = 0x247e0ae0,
1260 .name =
"atsamg53n19",
1261 .total_flash_size = 512 * 1024,
1262 .total_sram_size = 96 * 1024,
1274 .controller_address = 0x400e0a00,
1275 .flash_wait_states = 5,
1277 .size_bytes = 512 * 1024,
1279 .sector_size = 8192,
1294 .chipid_cidr = 0x24470ae0,
1295 .name =
"atsamg55g19",
1296 .total_flash_size = 512 * 1024,
1297 .total_sram_size = 160 * 1024,
1309 .controller_address = 0x400e0a00,
1310 .flash_wait_states = 5,
1312 .size_bytes = 512 * 1024,
1314 .sector_size = 8192,
1328 .chipid_cidr = 0x24470ae1,
1329 .name =
"atsamg55g19b",
1330 .total_flash_size = 512 * 1024,
1331 .total_sram_size = 160 * 1024,
1343 .controller_address = 0x400e0a00,
1344 .flash_wait_states = 5,
1346 .size_bytes = 512 * 1024,
1348 .sector_size = 8192,
1362 .chipid_cidr = 0x24570ae0,
1363 .name =
"atsamg55j19",
1364 .total_flash_size = 512 * 1024,
1365 .total_sram_size = 160 * 1024,
1377 .controller_address = 0x400e0a00,
1378 .flash_wait_states = 5,
1380 .size_bytes = 512 * 1024,
1382 .sector_size = 8192,
1396 .chipid_cidr = 0x24570ae1,
1397 .name =
"atsamg55j19b",
1398 .total_flash_size = 512 * 1024,
1399 .total_sram_size = 160 * 1024,
1411 .controller_address = 0x400e0a00,
1412 .flash_wait_states = 5,
1414 .size_bytes = 512 * 1024,
1416 .sector_size = 8192,
1456 LOG_DEBUG(
"Status: 0x%08x (lockerror: %d, cmderror: %d, ready: %d)",
1458 ((
unsigned int)((*v >> 2) & 1)),
1459 ((
unsigned int)((*v >> 1) & 1)),
1460 ((
unsigned int)((*v >> 0) & 1)));
1479 LOG_DEBUG(
"Result: 0x%08x", ((
unsigned int)(rv)));
1484 unsigned command,
unsigned argument)
1504 n = (
private->size_bytes /
private->page_size);
1506 LOG_ERROR(
"*BUG*: Embedded flash has only %u pages", (
unsigned)(n));
1511 if (argument >= private->chip->details.n_gpnvms) {
1512 LOG_ERROR(
"*BUG*: Embedded flash has only %d GPNVMs",
1513 private->chip->details.n_gpnvms);
1546 LOG_ERROR(
"flash controller(%d) is not ready! Error",
1547 private->bank_number);
1551 LOG_ERROR(
"Flash controller(%d) is not ready, attempting reset",
1552 private->bank_number);
1562 v = (0x5A << 24) | (argument << 8) |
command;
1563 LOG_DEBUG(
"Command: 0x%08x", ((
unsigned int)(v)));
1586 int64_t ms_now, ms_end;
1603 if (ms_now > ms_end) {
1608 }
while ((v & 1) == 0);
1628 private->chip->cfg.unique_id[0] = 0;
1629 private->chip->cfg.unique_id[1] = 0;
1630 private->chip->cfg.unique_id[2] = 0;
1631 private->chip->cfg.unique_id[3] = 0;
1638 for (x = 0; x < 4; x++) {
1640 private->bank->base + (x * 4),
1644 private->chip->cfg.unique_id[x] = v;
1648 LOG_DEBUG(
"End: R=%d, id = 0x%08x, 0x%08x, 0x%08x, 0x%08x",
1650 (
unsigned int)(private->chip->cfg.unique_id[0]),
1651 (
unsigned int)(private->chip->cfg.unique_id[1]),
1652 (
unsigned int)(private->chip->cfg.unique_id[2]),
1653 (
unsigned int)(private->chip->cfg.unique_id[3]));
1681 uint8_t erase_pages;
1682 switch (num_pages) {
1710 (first_page) | erase_pages,
1727 if (private->bank_number != 0) {
1728 LOG_ERROR(
"GPNVM only works with Bank0");
1732 if (
gpnvm >= private->chip->details.n_gpnvms) {
1733 LOG_ERROR(
"Invalid GPNVM %d, max: %d, ignored",
1734 gpnvm, private->chip->details.n_gpnvms);
1750 *puthere = (v >>
gpnvm) & 1;
1768 if (private->bank_number != 0) {
1769 LOG_ERROR(
"GPNVM only works with Bank0");
1773 if (
gpnvm >= private->chip->details.n_gpnvms) {
1774 LOG_ERROR(
"Invalid GPNVM %d, max: %d, ignored",
1775 gpnvm, private->chip->details.n_gpnvms);
1799 if (private->bank_number != 0) {
1800 LOG_ERROR(
"GPNVM only works with Bank0");
1804 if (
gpnvm >= private->chip->details.n_gpnvms) {
1805 LOG_ERROR(
"Invalid GPNVM %d, max: %d, ignored",
1806 gpnvm, private->chip->details.n_gpnvms);
1851 unsigned start_sector,
1852 unsigned end_sector)
1857 uint32_t pages_per_sector;
1859 pages_per_sector =
private->sector_size /
private->page_size;
1862 while (start_sector <= end_sector) {
1863 pg = start_sector * pages_per_sector;
1881 unsigned start_sector,
1882 unsigned end_sector)
1886 uint32_t pages_per_sector;
1889 pages_per_sector =
private->sector_size /
private->page_size;
1892 while (start_sector <= end_sector) {
1893 pg = start_sector * pages_per_sector;
1908 const char *regname,
1919 v = v & ((1 <<
width)-1);
1929 LOG_USER_N(
"\t%*s: %*" PRIu32
" [0x%0*" PRIx32
"] ",
1956 #define nvpsize2 nvpsize
1997 { 0x19,
"AT91SAM9xx Series" },
1998 { 0x29,
"AT91SAM9XExx Series" },
1999 { 0x34,
"AT91x34 Series" },
2000 { 0x37,
"CAP7 Series" },
2001 { 0x39,
"CAP9 Series" },
2002 { 0x3B,
"CAP11 Series" },
2003 { 0x3C,
"ATSAM4E" },
2004 { 0x40,
"AT91x40 Series" },
2005 { 0x42,
"AT91x42 Series" },
2006 { 0x43,
"SAMG51 Series"
2008 { 0x44,
"SAMG55 Series (49-pin WLCSP)" },
2009 { 0x45,
"SAMG55 Series (64-pin)" },
2010 { 0x47,
"SAMG53 Series"
2012 { 0x55,
"AT91x55 Series" },
2013 { 0x60,
"AT91SAM7Axx Series" },
2014 { 0x61,
"AT91SAM7AQxx Series" },
2015 { 0x63,
"AT91x63 Series" },
2016 { 0x64,
"SAM4CxxC (100-pin version)" },
2017 { 0x66,
"SAM4CxxE (144-pin version)" },
2018 { 0x70,
"AT91SAM7Sxx Series" },
2019 { 0x71,
"AT91SAM7XCxx Series" },
2020 { 0x72,
"AT91SAM7SExx Series" },
2021 { 0x73,
"AT91SAM7Lxx Series" },
2022 { 0x75,
"AT91SAM7Xxx Series" },
2023 { 0x76,
"AT91SAM7SLxx Series" },
2024 { 0x80,
"ATSAM3UxC Series (100-pin version)" },
2025 { 0x81,
"ATSAM3UxE Series (144-pin version)" },
2026 { 0x83,
"ATSAM3A/SAM4A xC Series (100-pin version)"},
2027 { 0x84,
"ATSAM3X/SAM4X xC Series (100-pin version)"},
2028 { 0x85,
"ATSAM3X/SAM4X xE Series (144-pin version)"},
2029 { 0x86,
"ATSAM3X/SAM4X xG Series (208/217-pin version)" },
2030 { 0x88,
"ATSAM3S/SAM4S xA Series (48-pin version)" },
2031 { 0x89,
"ATSAM3S/SAM4S xB Series (64-pin version)" },
2032 { 0x8A,
"ATSAM3S/SAM4S xC Series (100-pin version)"},
2033 { 0x92,
"AT91x92 Series" },
2034 { 0x93,
"ATSAM3NxA Series (48-pin version)" },
2035 { 0x94,
"ATSAM3NxB Series (64-pin version)" },
2036 { 0x95,
"ATSAM3NxC Series (100-pin version)" },
2037 { 0x98,
"ATSAM3SDxA Series (48-pin version)" },
2038 { 0x99,
"ATSAM3SDxB Series (64-pin version)" },
2039 { 0x9A,
"ATSAM3SDxC Series (100-pin version)" },
2040 { 0xA5,
"ATSAM5A" },
2041 { 0xF0,
"AT75Cxx Series" },
2047 "romless or onchip flash",
2048 "embedded flash memory",
2049 "rom(nvpsiz) + embedded flash (nvpsiz2)",
2050 "sram emulating flash",
2065 "4 MHz",
"8 MHz",
"12 MHz",
"reserved"
2101 LOG_USER(
"(startup clks, time= %f uSecs)",
2102 ((
float)(v * 1000000)) / ((
float)(chip->
cfg.
slow_freq)));
2105 v ?
"external xtal" :
"internal RC");
2108 LOG_USER(
"(clock failure enabled: %s)",
2163 LOG_USER(
"(%3.03f Mhz (%" PRIu32
".%03" PRIu32
"khz slowclk)",
2171 uint32_t mula, diva;
2179 LOG_USER(
"\tPLLA Freq: (Disabled,mula = 0)");
2181 LOG_USER(
"\tPLLA Freq: (Disabled,diva = 0)");
2182 else if (diva >= 1) {
2184 LOG_USER(
"\tPLLA Freq: %3.03f MHz",
2191 uint32_t css, pres, fin = 0;
2193 const char *cp =
NULL;
2211 fin = 480 * 1000 * 1000;
2215 cp =
"upll (*ERROR* UPLL is disabled)";
2227 switch (pres & 0x07) {
2230 cp =
"selected clock";
2271 LOG_USER(
"\t\tResult CPU Freq: %3.03f",
2307 #define SAM4_ENTRY(NAME, FUNC) { .address = SAM4_ ## NAME, .struct_offset = offsetof( \
2309 NAME), # NAME, FUNC }
2334 return bank->driver_priv;
2352 possible = ((uint32_t *)(
void *)(((
char *)(&(chip->
cfg))) +
reg->struct_offset));
2355 if (possible == goes_here) {
2379 LOG_ERROR(
"Cannot read SAM4 register: %s @ 0x%08x, Err: %d",
2395 LOG_ERROR(
"Cannot read SAM4 register: %s @ 0x%08x, Error: %d",
2420 LOG_USER(
"%*s: [0x%08" PRIx32
"] -> 0x%08" PRIx32,
2425 if (
reg->explain_func)
2426 (*(
reg->explain_func))(chip);
2436 LOG_USER(
" UniqueId: 0x%08" PRIx32
" 0x%08" PRIx32
" 0x%08" PRIx32
" 0x%08"PRIx32,
2448 uint32_t v[4] = {0};
2463 if (!(private->probed))
2472 for (x = 0; x <
private->nsectors; x++)
2473 bank->sectors[x].is_protected = (!!(v[x >> 5] & (1 << (x % 32))));
2493 chip = calloc(1,
sizeof(
struct sam4_chip));
2508 switch (
bank->base) {
2510 LOG_ERROR(
"Address 0x%08x invalid bank address (try 0x%08x"
2511 "[at91sam4s series] )",
2512 ((
unsigned int)(
bank->base)),
2521 bank->bank_number = 0;
2531 bank->bank_number = 1;
2566 while (details->
name) {
2568 if (details->
chipid_cidr == (private->chip->cfg.CHIPID_CIDR & 0xFFFFFFE0))
2573 if (!details->
name) {
2574 LOG_ERROR(
"SAM4 ChipID 0x%08x not found in table (perhaps you can ID this chip?)",
2575 (
unsigned int)(private->chip->cfg.CHIPID_CIDR));
2577 LOG_INFO(
"SAM4 CHIPID_CIDR: 0x%08" PRIx32
" decodes as follows",
2578 private->chip->cfg.CHIPID_CIDR);
2589 chip =
private->chip;
2602 memcpy(&(private->chip->details),
2604 sizeof(private->chip->details));
2621 int k =
bank->size / 1024;
2628 private->chip->details.name,
2629 private->bank_number,
2650 LOG_ERROR(
"Invalid/unknown bank number");
2659 if (private->chip->probed)
2668 if (
bank->base == private->chip->details.bank[x].base_address) {
2669 bank->size =
private->chip->details.bank[x].size_bytes;
2677 if (!
bank->sectors) {
2678 bank->sectors = calloc(private->nsectors, (
sizeof((
bank->sectors)[0])));
2679 if (!
bank->sectors) {
2683 bank->num_sectors =
private->nsectors;
2685 for (
unsigned int x = 0; x <
bank->num_sectors; x++) {
2686 bank->sectors[x].size =
private->sector_size;
2687 bank->sectors[x].offset = x * (
private->sector_size);
2689 bank->sectors[x].is_erased = -1;
2690 bank->sectors[x].is_protected = -1;
2694 private->probed =
true;
2701 private->bank_number, private->chip->details.n_banks);
2702 if ((private->bank_number + 1) == private->chip->details.n_banks) {
2716 if (
private && private->probed)
2745 if (!(private->probed))
2748 if ((first == 0) && ((last + 1) == private->nsectors)) {
2753 LOG_INFO(
"sam4 does not auto-erase while programming (Erasing relevant sectors)");
2754 LOG_INFO(
"sam4 First: 0x%08x Last: 0x%08x", first, last);
2755 for (
unsigned int i = first; i <= last; i++) {
2758 LOG_INFO(
"Erasing sector: 0x%08x", i);
2760 LOG_ERROR(
"SAM4: Error performing Erase page @ lock region number %u",
2763 LOG_ERROR(
"SAM4: Lock Region %u is locked", i);
2767 LOG_ERROR(
"SAM4: Flash Command error @lock region %u", i);
2788 if (!(private->probed))
2806 adr = pagenum *
private->page_size;
2807 adr = adr +
private->base_address;
2812 private->page_size / 4,
2815 LOG_ERROR(
"SAM4: Flash program failed to read page phys address: 0x%08x",
2816 (
unsigned int)(adr));
2826 r =
target_read_u32(private->chip->target, private->controller_address, &fmr);
2828 LOG_ERROR(
"Error Read failed: read flash mode register");
2836 fmr |= (
private->flash_wait_states << 8);
2838 LOG_DEBUG(
"Flash Mode: 0x%08x", ((
unsigned int)(fmr)));
2839 r =
target_write_u32(private->bank->target, private->controller_address, fmr);
2841 LOG_ERROR(
"Error Write failed: set flash mode register");
2852 adr = pagenum *
private->page_size;
2853 adr = (adr +
private->base_address);
2858 LOG_DEBUG(
"Wr Page %u @ phys address: 0x%08x", pagenum, (
unsigned int)(adr));
2862 private->page_size / 4,
2865 LOG_ERROR(
"SAM4: Failed to write (buffer) page at phys address 0x%08x",
2866 (
unsigned int)(adr));
2877 LOG_ERROR(
"SAM4: Error performing Write page @ phys address 0x%08x",
2878 (
unsigned int)(adr));
2880 LOG_ERROR(
"SAM4: Page @ Phys address 0x%08x is locked", (
unsigned int)(adr));
2884 LOG_ERROR(
"SAM4: Flash Command error @phys address 0x%08x", (
unsigned int)(adr));
2899 unsigned page_offset;
2901 uint8_t *pagebuffer;
2919 if (!(private->probed)) {
2925 LOG_ERROR(
"Flash write error - past end of bank");
2926 LOG_ERROR(
" offset: 0x%08x, count 0x%08x, BankEnd: 0x%08x",
2928 (
unsigned int)(
count),
2929 (
unsigned int)(private->size_bytes));
2934 pagebuffer = malloc(private->page_size);
2936 LOG_ERROR(
"No memory for %d Byte page buffer", (
int)(private->page_size));
2946 page_cur =
offset /
private->page_size;
2947 page_end = (
offset +
count - 1) / private->page_size;
2950 LOG_DEBUG(
"Page start: %d, Page End: %d", (
int)(page_cur), (
int)(page_end));
2960 if (page_cur == page_end) {
2961 LOG_DEBUG(
"Special case, all in one page");
2966 page_offset = (
offset & (
private->page_size-1));
2967 memcpy(pagebuffer + page_offset,
2979 page_offset =
offset & (
private->page_size - 1);
2988 n = (
private->page_size - page_offset);
2989 memcpy(pagebuffer + page_offset,
3005 assert(
offset % private->page_size == 0);
3010 LOG_DEBUG(
"Full Page Loop: cur=%d, end=%d, count = 0x%08x",
3011 (
int)page_cur, (
int)page_end, (
unsigned int)(
count));
3013 while ((page_cur < page_end) &&
3014 (
count >= private->page_size)) {
3018 count -=
private->page_size;
3019 buffer +=
private->page_size;
3025 LOG_DEBUG(
"Terminal partial page, count = 0x%08x", (
unsigned int)(
count));
3058 "Please define bank %d via command: flash bank %s ... ",
3135 if ((strcmp(
CMD_ARGV[0],
"show") == 0) && (strcmp(
CMD_ARGV[1],
"all") == 0))
3145 if (strcmp(
"show",
CMD_ARGV[0]) == 0) {
3173 if (strcmp(
"set",
CMD_ARGV[0]) == 0)
3175 else if ((strcmp(
"clr",
CMD_ARGV[0]) == 0) ||
3176 (strcmp(
"clear",
CMD_ARGV[0]) == 0))
3224 .handler = sam4_handle_gpnvm_command,
3226 .usage =
"[('clr'|'set'|'show') bitnum]",
3227 .help =
"Without arguments, shows all bits in the gpnvm "
3228 "register. Otherwise, clears, sets, or shows one "
3229 "General Purpose Non-Volatile Memory (gpnvm) bit.",
3233 .handler = sam4_handle_info_command,
3235 .help =
"Print information about the current at91sam4 chip "
3236 "and its flash configuration.",
3241 .handler = sam4_handle_slowclk_command,
3243 .usage =
"[clock_hz]",
3244 .help =
"Display or set the slowclock frequency "
3245 "(default 32768 Hz).",
3253 .help =
"at91sam4 flash command group",
3263 .flash_bank_command = sam4_flash_bank_command,
#define FLASH_BANK0_BASE_C32
static int efc_start_command(struct sam4_bank_private *private, unsigned command, unsigned argument)
#define FLASH_BANK1_BASE_2048K_SD
#define AT91C_EFC_FCMD_WPL
#define AT91C_EFC_FCMD_GLB
#define FLASH_BANK_BASE_S
static const struct sam4_reg_list * sam4_get_reg(struct sam4_chip *chip, uint32_t *goes_here)
Given a pointer to where it goes in the structure, determine the register name, address from the all ...
static const char *const eproc_names[]
#define AT91C_EFC_FCMD_EWPL
static int flashd_get_gpnvm(struct sam4_bank_private *private, unsigned gpnvm, unsigned *puthere)
Gets current GPNVM state.
static int efc_perform_command(struct sam4_bank_private *private, unsigned command, unsigned argument, uint32_t *status)
Performs the given command and wait until its completion (or an error).
static const struct sam4_chip_details all_sam4_details[]
static int flashd_lock(struct sam4_bank_private *private, unsigned start_sector, unsigned end_sector)
Locks regions.
static int sam4_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
COMMAND_HANDLER(sam4_handle_info_command)
static int sam4_info(struct flash_bank *bank, struct command_invocation *cmd)
static void sam4_explain_chipid_cidr(struct sam4_chip *chip)
static const char _unknown[]
static int sam4_auto_probe(struct flash_bank *bank)
static const char * _yes_or_no(uint32_t v)
static struct sam4_bank_private * get_sam4_bank_private(struct flash_bank *bank)
static void sam4_explain_ckgr_plla(struct sam4_chip *chip)
#define AT91C_EFC_FCMD_SFB
static uint32_t sam4_reg_fieldname(struct sam4_chip *chip, const char *regname, uint32_t value, unsigned shift, unsigned width)
#define AT91C_EFC_FCMD_EA
static int sam4_get_details(struct sam4_bank_private *private)
#define FLASH_BANK1_BASE_1024K_SD
#define AT91C_EFC_FCMD_EPA
static struct sam4_chip * all_sam4_chips
static int sam4_read_this_reg(struct sam4_chip *chip, uint32_t *goes_here)
#define AT91C_EFC_FCMD_GFB
static void sam4_explain_ckgr_mor(struct sam4_chip *chip)
#define SAM4_ENTRY(NAME, FUNC)
static uint32_t * sam4_get_reg_ptr(struct sam4_cfg *cfg, const struct sam4_reg_list *list)
#define FLASH_BANK_BASE_C
static int flashd_set_gpnvm(struct sam4_bank_private *private, unsigned gpnvm)
Sets the selected GPNVM bit.
static const struct sam4_reg_list sam4_all_regs[]
static int sam4_protect_check(struct flash_bank *bank)
static struct sam4_chip * get_current_sam4(struct command_invocation *cmd)
static int efc_get_status(struct sam4_bank_private *private, uint32_t *v)
Get the current status of the EEFC and the value of some status bits (LOCKE, PROGE).
static const char *const nvpsize[]
static void sam4_explain_mckr(struct sam4_chip *chip)
#define AT91C_EFC_FCMD_EWP
static float _tomhz(uint32_t freq_hz)
static int sam4_page_write(struct sam4_bank_private *private, unsigned pagenum, const uint8_t *buf)
static const struct command_registration at91sam4_exec_command_handlers[]
#define AT91C_EFC_FCMD_WP
static int flashd_get_lock_bits(struct sam4_bank_private *private, uint32_t *v)
Returns a bit field (at most 64) of locked regions within a page.
static int sam4_page_read(struct sam4_bank_private *private, unsigned pagenum, uint8_t *buf)
static int flashd_read_uid(struct sam4_bank_private *private)
Read the unique ID.
#define SAM4_MAX_FLASH_BANKS
static int flashd_clr_gpnvm(struct sam4_bank_private *private, unsigned gpnvm)
Clears the selected GPNVM bit.
static const struct command_registration at91sam4_command_handlers[]
static const char *const nvptype[]
#define FLASH_BANK1_BASE_C32
static void sam4_explain_ckgr_mcfr(struct sam4_chip *chip)
#define FLASH_BANK0_BASE_SD
static int sam4_set_wait(struct sam4_bank_private *private)
#define AT91C_EFC_FCMD_STUI
static void sam4_free_driver_priv(struct flash_bank *bank)
Remove all chips from the internal list without distinguishing which one is owned by this bank.
#define AT91C_EFC_FCMD_GETD
static int flashd_erase_pages(struct sam4_bank_private *private, int first_page, int num_pages, uint32_t *status)
Erases the entire flash.
static int sam4_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
FLASH_BANK_COMMAND_HANDLER(sam4_flash_bank_command)
static int efc_get_result(struct sam4_bank_private *private, uint32_t *v)
Get the result of the last executed command.
static int sam4_read_all_regs(struct sam4_chip *chip)
static const char *const sramsize[]
const struct flash_driver at91sam4_flash
static int sam4_probe(struct flash_bank *bank)
#define AT91C_EFC_FCMD_SPUI
#define AT91C_EFC_FCMD_CLB
static int flashd_erase_entire_bank(struct sam4_bank_private *private)
Erases the entire flash.
static int sam4_get_info(struct sam4_chip *chip)
#define AT91C_EFC_FCMD_SLB
static int flashd_unlock(struct sam4_bank_private *private, unsigned start_sector, unsigned end_sector)
Unlocks all the regions in the given address range.
static int sam4_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static const char *const _rc_freq[]
#define AT91C_EFC_FCMD_CFB
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
void command_print(struct command_invocation *cmd, const char *format,...)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_FLASH_BANK_NOT_PROBED
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
#define LOG_USER(expr ...)
#define LOG_USER_N(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Provides details of a flash bank, available either on-chip or through a major interface.
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
uint32_t controller_address
uint32_t flash_wait_states
struct sam4_bank_private bank[SAM4_MAX_FLASH_BANKS]
unsigned total_flash_size
unsigned gpnvm[SAM4_N_NVM_BITS]
struct sam4_chip_details details
void(* explain_func)(struct sam4_chip *chip)
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
struct target * get_current_target(struct command_context *cmd_ctx)
#define ERROR_TARGET_NOT_HALTED