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
65 static float _tomhz(uint32_t freq_hz)
69 f = ((float)(freq_hz)) / 1000000.0;
89 #define SAM4_CHIPID_CIDR (0x400E0740)
91 #define SAM4_CHIPID_EXID (0x400E0744)
94 #define SAM4_PMC_BASE (0x400E0400)
95 #define SAM4_PMC_SCSR (SAM4_PMC_BASE + 0x0008)
97 #define SAM4_PMC_PCSR (SAM4_PMC_BASE + 0x0018)
99 #define SAM4_CKGR_UCKR (SAM4_PMC_BASE + 0x001c)
101 #define SAM4_CKGR_MOR (SAM4_PMC_BASE + 0x0020)
103 #define SAM4_CKGR_MCFR (SAM4_PMC_BASE + 0x0024)
105 #define SAM4_CKGR_PLLAR (SAM4_PMC_BASE + 0x0028)
107 #define SAM4_PMC_MCKR (SAM4_PMC_BASE + 0x0030)
109 #define SAM4_PMC_PCK0 (SAM4_PMC_BASE + 0x0040)
111 #define SAM4_PMC_PCK1 (SAM4_PMC_BASE + 0x0044)
113 #define SAM4_PMC_PCK2 (SAM4_PMC_BASE + 0x0048)
115 #define SAM4_PMC_SR (SAM4_PMC_BASE + 0x0068)
117 #define SAM4_PMC_IMR (SAM4_PMC_BASE + 0x006c)
119 #define SAM4_PMC_FSMR (SAM4_PMC_BASE + 0x0070)
121 #define SAM4_PMC_FSPR (SAM4_PMC_BASE + 0x0074)
160 #define SAM4_N_NVM_BITS 3
165 #define SAM4_MAX_FLASH_BANKS 2
227 .name =
"at91sam4c32e",
228 .total_flash_size = 2024 * 1024,
229 .total_sram_size = 256 * 1024,
240 .controller_address = 0x400e0a00,
241 .flash_wait_states = 5,
243 .size_bytes = 1024 * 1024,
255 .controller_address = 0x400e0c00,
256 .flash_wait_states = 5,
258 .size_bytes = 1024 * 1024,
267 .chipid_cidr = 0xA64D0EE0,
268 .name =
"at91sam4c32c",
269 .total_flash_size = 2024 * 1024,
270 .total_sram_size = 256 * 1024,
281 .controller_address = 0x400e0a00,
282 .flash_wait_states = 5,
284 .size_bytes = 1024 * 1024,
296 .controller_address = 0x400e0c00,
297 .flash_wait_states = 5,
299 .size_bytes = 1024 * 1024,
308 .chipid_cidr = 0xA64C0CE0,
309 .name =
"at91sam4c16c",
310 .total_flash_size = 1024 * 1024,
311 .total_sram_size = 128 * 1024,
322 .controller_address = 0x400e0a00,
323 .flash_wait_states = 5,
325 .size_bytes = 1024 * 1024,
341 .chipid_cidr = 0xA64C0AE0,
342 .name =
"at91sam4c8c",
343 .total_flash_size = 512 * 1024,
344 .total_sram_size = 128 * 1024,
355 .controller_address = 0x400e0a00,
356 .flash_wait_states = 5,
358 .size_bytes = 512 * 1024,
374 .chipid_cidr = 0xA64C0CE5,
375 .name =
"at91sam4c4c",
376 .total_flash_size = 256 * 1024,
377 .total_sram_size = 128 * 1024,
388 .controller_address = 0x400e0a00,
389 .flash_wait_states = 5,
391 .size_bytes = 256 * 1024,
409 .chipid_cidr = 0xA3CC0CE0,
410 .name =
"at91sam4e16e",
411 .total_flash_size = 1024 * 1024,
412 .total_sram_size = 128 * 1024,
423 .controller_address = 0x400e0a00,
424 .flash_wait_states = 5,
426 .size_bytes = 1024 * 1024,
444 .chipid_cidr = 0x293B0AE0,
445 .name =
"at91sam4n8a",
446 .total_flash_size = 512 * 1024,
447 .total_sram_size = 64 * 1024,
458 .controller_address = 0x400e0a00,
459 .flash_wait_states = 5,
461 .size_bytes = 512 * 1024,
477 .chipid_cidr = 0x294B0AE0,
478 .name =
"at91sam4n8b",
479 .total_flash_size = 512 * 1024,
480 .total_sram_size = 64 * 1024,
491 .controller_address = 0x400e0a00,
492 .flash_wait_states = 5,
494 .size_bytes = 512 * 1024,
510 .chipid_cidr = 0x295B0AE0,
511 .name =
"at91sam4n8c",
512 .total_flash_size = 512 * 1024,
513 .total_sram_size = 64 * 1024,
524 .controller_address = 0x400e0a00,
525 .flash_wait_states = 5,
527 .size_bytes = 512 * 1024,
543 .chipid_cidr = 0x29460CE0,
544 .name =
"at91sam4n16b",
545 .total_flash_size = 1024 * 1024,
546 .total_sram_size = 80 * 1024,
557 .controller_address = 0x400e0a00,
558 .flash_wait_states = 5,
560 .size_bytes = 1024 * 1024,
576 .chipid_cidr = 0x29560CE0,
577 .name =
"at91sam4n16c",
578 .total_flash_size = 1024 * 1024,
579 .total_sram_size = 80 * 1024,
590 .controller_address = 0x400e0a00,
591 .flash_wait_states = 5,
593 .size_bytes = 1024 * 1024,
611 .chipid_cidr = 0x28AC0CE0,
612 .name =
"at91sam4s16c",
613 .total_flash_size = 1024 * 1024,
614 .total_sram_size = 128 * 1024,
625 .controller_address = 0x400e0a00,
626 .flash_wait_states = 5,
628 .size_bytes = 1024 * 1024,
644 .chipid_cidr = 0x28a70ce0,
645 .name =
"at91sam4sa16c",
646 .total_flash_size = 1024 * 1024,
647 .total_sram_size = 160 * 1024,
659 .controller_address = 0x400e0a00,
660 .flash_wait_states = 5,
662 .size_bytes = 1024 * 1024,
678 .chipid_cidr = 0x289C0CE0,
679 .name =
"at91sam4s16b",
680 .total_flash_size = 1024 * 1024,
681 .total_sram_size = 128 * 1024,
692 .controller_address = 0x400e0a00,
693 .flash_wait_states = 5,
695 .size_bytes = 1024 * 1024,
711 .chipid_cidr = 0x28970CE0,
712 .name =
"at91sam4sa16b",
713 .total_flash_size = 1024 * 1024,
714 .total_sram_size = 160 * 1024,
725 .controller_address = 0x400e0a00,
726 .flash_wait_states = 5,
728 .size_bytes = 1024 * 1024,
744 .chipid_cidr = 0x288C0CE0,
745 .name =
"at91sam4s16a",
746 .total_flash_size = 1024 * 1024,
747 .total_sram_size = 128 * 1024,
758 .controller_address = 0x400e0a00,
759 .flash_wait_states = 5,
761 .size_bytes = 1024 * 1024,
777 .chipid_cidr = 0x28AC0AE0,
778 .name =
"at91sam4s8c",
779 .total_flash_size = 512 * 1024,
780 .total_sram_size = 128 * 1024,
791 .controller_address = 0x400e0a00,
792 .flash_wait_states = 5,
794 .size_bytes = 512 * 1024,
810 .chipid_cidr = 0x289C0AE0,
811 .name =
"at91sam4s8b",
812 .total_flash_size = 512 * 1024,
813 .total_sram_size = 128 * 1024,
824 .controller_address = 0x400e0a00,
825 .flash_wait_states = 5,
827 .size_bytes = 512 * 1024,
843 .chipid_cidr = 0x288C0AE0,
844 .name =
"at91sam4s8a",
845 .total_flash_size = 512 * 1024,
846 .total_sram_size = 128 * 1024,
857 .controller_address = 0x400e0a00,
858 .flash_wait_states = 5,
860 .size_bytes = 512 * 1024,
877 .chipid_cidr = 0x28ab09e0,
878 .name =
"at91sam4s4c",
879 .total_flash_size = 256 * 1024,
880 .total_sram_size = 64 * 1024,
891 .controller_address = 0x400e0a00,
892 .flash_wait_states = 5,
894 .size_bytes = 256 * 1024,
911 .chipid_cidr = 0x289b09e0,
912 .name =
"at91sam4s4b",
913 .total_flash_size = 256 * 1024,
914 .total_sram_size = 64 * 1024,
925 .controller_address = 0x400e0a00,
926 .flash_wait_states = 5,
928 .size_bytes = 256 * 1024,
945 .chipid_cidr = 0x288b09e0,
946 .name =
"at91sam4s4a",
947 .total_flash_size = 256 * 1024,
948 .total_sram_size = 64 * 1024,
959 .controller_address = 0x400e0a00,
960 .flash_wait_states = 5,
962 .size_bytes = 256 * 1024,
979 .chipid_cidr = 0x28ab07e0,
980 .name =
"at91sam4s2c",
981 .total_flash_size = 128 * 1024,
982 .total_sram_size = 64 * 1024,
993 .controller_address = 0x400e0a00,
994 .flash_wait_states = 5,
996 .size_bytes = 128 * 1024,
1013 .chipid_cidr = 0x289b07e0,
1014 .name =
"at91sam4s2b",
1015 .total_flash_size = 128 * 1024,
1016 .total_sram_size = 64 * 1024,
1027 .controller_address = 0x400e0a00,
1028 .flash_wait_states = 5,
1030 .size_bytes = 128 * 1024,
1032 .sector_size = 8192,
1047 .chipid_cidr = 0x288b07e0,
1048 .name =
"at91sam4s2a",
1049 .total_flash_size = 128 * 1024,
1050 .total_sram_size = 64 * 1024,
1061 .controller_address = 0x400e0a00,
1062 .flash_wait_states = 5,
1064 .size_bytes = 128 * 1024,
1066 .sector_size = 8192,
1081 .chipid_cidr = 0x29a70ee0,
1082 .name =
"at91sam4sd32c",
1083 .total_flash_size = 2048 * 1024,
1084 .total_sram_size = 160 * 1024,
1096 .controller_address = 0x400e0a00,
1097 .flash_wait_states = 5,
1099 .size_bytes = 1024 * 1024,
1101 .sector_size = 8192,
1112 .controller_address = 0x400e0c00,
1113 .flash_wait_states = 5,
1115 .size_bytes = 1024 * 1024,
1117 .sector_size = 8192,
1125 .chipid_cidr = 0x29970ee0,
1126 .name =
"at91sam4sd32b",
1127 .total_flash_size = 2048 * 1024,
1128 .total_sram_size = 160 * 1024,
1140 .controller_address = 0x400e0a00,
1141 .flash_wait_states = 5,
1143 .size_bytes = 1024 * 1024,
1145 .sector_size = 8192,
1156 .controller_address = 0x400e0c00,
1157 .flash_wait_states = 5,
1159 .size_bytes = 1024 * 1024,
1161 .sector_size = 8192,
1169 .chipid_cidr = 0x29a70ce0,
1170 .name =
"at91sam4sd16c",
1171 .total_flash_size = 1024 * 1024,
1172 .total_sram_size = 160 * 1024,
1184 .controller_address = 0x400e0a00,
1185 .flash_wait_states = 5,
1187 .size_bytes = 512 * 1024,
1189 .sector_size = 8192,
1200 .controller_address = 0x400e0c00,
1201 .flash_wait_states = 5,
1203 .size_bytes = 512 * 1024,
1205 .sector_size = 8192,
1213 .chipid_cidr = 0x29970ce0,
1214 .name =
"at91sam4sd16b",
1215 .total_flash_size = 1024 * 1024,
1216 .total_sram_size = 160 * 1024,
1228 .controller_address = 0x400e0a00,
1229 .flash_wait_states = 5,
1231 .size_bytes = 512 * 1024,
1233 .sector_size = 8192,
1244 .controller_address = 0x400e0c00,
1245 .flash_wait_states = 5,
1247 .size_bytes = 512 * 1024,
1249 .sector_size = 8192,
1257 .chipid_cidr = 0x247e0ae0,
1258 .name =
"atsamg53n19",
1259 .total_flash_size = 512 * 1024,
1260 .total_sram_size = 96 * 1024,
1272 .controller_address = 0x400e0a00,
1273 .flash_wait_states = 5,
1275 .size_bytes = 512 * 1024,
1277 .sector_size = 8192,
1292 .chipid_cidr = 0x24470ae0,
1293 .name =
"atsamg55g19",
1294 .total_flash_size = 512 * 1024,
1295 .total_sram_size = 160 * 1024,
1307 .controller_address = 0x400e0a00,
1308 .flash_wait_states = 5,
1310 .size_bytes = 512 * 1024,
1312 .sector_size = 8192,
1326 .chipid_cidr = 0x24470ae1,
1327 .name =
"atsamg55g19b",
1328 .total_flash_size = 512 * 1024,
1329 .total_sram_size = 160 * 1024,
1341 .controller_address = 0x400e0a00,
1342 .flash_wait_states = 5,
1344 .size_bytes = 512 * 1024,
1346 .sector_size = 8192,
1360 .chipid_cidr = 0x24570ae0,
1361 .name =
"atsamg55j19",
1362 .total_flash_size = 512 * 1024,
1363 .total_sram_size = 160 * 1024,
1375 .controller_address = 0x400e0a00,
1376 .flash_wait_states = 5,
1378 .size_bytes = 512 * 1024,
1380 .sector_size = 8192,
1394 .chipid_cidr = 0x24570ae1,
1395 .name =
"atsamg55j19b",
1396 .total_flash_size = 512 * 1024,
1397 .total_sram_size = 160 * 1024,
1409 .controller_address = 0x400e0a00,
1410 .flash_wait_states = 5,
1412 .size_bytes = 512 * 1024,
1414 .sector_size = 8192,
1454 LOG_DEBUG(
"Status: 0x%08x (lockerror: %d, cmderror: %d, ready: %d)",
1456 ((
unsigned int)((*v >> 2) & 1)),
1457 ((
unsigned int)((*v >> 1) & 1)),
1458 ((
unsigned int)((*v >> 0) & 1)));
1477 LOG_DEBUG(
"Result: 0x%08x", ((
unsigned int)(rv)));
1482 unsigned int command,
unsigned int argument)
1501 n = (
private->size_bytes /
private->page_size);
1503 LOG_ERROR(
"*BUG*: Embedded flash has only %" PRIu32
" pages", n);
1508 if (argument >= private->chip->details.n_gpnvms) {
1509 LOG_ERROR(
"*BUG*: Embedded flash has only %d GPNVMs",
1510 private->chip->details.n_gpnvms);
1543 LOG_ERROR(
"flash controller(%d) is not ready! Error",
1544 private->bank_number);
1548 LOG_ERROR(
"Flash controller(%d) is not ready, attempting reset",
1549 private->bank_number);
1559 v = (0x5A << 24) | (argument << 8) |
command;
1560 LOG_DEBUG(
"Command: 0x%08x", ((
unsigned int)(v)));
1577 unsigned int argument,
1583 int64_t ms_now, ms_end;
1600 if (ms_now > ms_end) {
1605 }
while ((v & 1) == 0);
1625 private->chip->cfg.unique_id[0] = 0;
1626 private->chip->cfg.unique_id[1] = 0;
1627 private->chip->cfg.unique_id[2] = 0;
1628 private->chip->cfg.unique_id[3] = 0;
1635 for (x = 0; x < 4; x++) {
1637 private->bank->base + (x * 4),
1641 private->chip->cfg.unique_id[x] = v;
1645 LOG_DEBUG(
"End: R=%d, id = 0x%08x, 0x%08x, 0x%08x, 0x%08x",
1647 (
unsigned int)(private->chip->cfg.unique_id[0]),
1648 (
unsigned int)(private->chip->cfg.unique_id[1]),
1649 (
unsigned int)(private->chip->cfg.unique_id[2]),
1650 (
unsigned int)(private->chip->cfg.unique_id[3]));
1678 uint8_t erase_pages;
1679 switch (num_pages) {
1707 (first_page) | erase_pages,
1724 if (private->bank_number != 0) {
1725 LOG_ERROR(
"GPNVM only works with Bank0");
1729 if (
gpnvm >= private->chip->details.n_gpnvms) {
1730 LOG_ERROR(
"Invalid GPNVM %d, max: %d, ignored",
1731 gpnvm, private->chip->details.n_gpnvms);
1747 *puthere = (v >>
gpnvm) & 1;
1765 if (private->bank_number != 0) {
1766 LOG_ERROR(
"GPNVM only works with Bank0");
1770 if (
gpnvm >= private->chip->details.n_gpnvms) {
1771 LOG_ERROR(
"Invalid GPNVM %d, max: %d, ignored",
1772 gpnvm, private->chip->details.n_gpnvms);
1796 if (private->bank_number != 0) {
1797 LOG_ERROR(
"GPNVM only works with Bank0");
1801 if (
gpnvm >= private->chip->details.n_gpnvms) {
1802 LOG_ERROR(
"Invalid GPNVM %d, max: %d, ignored",
1803 gpnvm, private->chip->details.n_gpnvms);
1848 unsigned int start_sector,
1849 unsigned int end_sector)
1854 uint32_t pages_per_sector;
1856 pages_per_sector =
private->sector_size /
private->page_size;
1859 while (start_sector <= end_sector) {
1860 pg = start_sector * pages_per_sector;
1878 unsigned int start_sector,
1879 unsigned int end_sector)
1883 uint32_t pages_per_sector;
1886 pages_per_sector =
private->sector_size /
private->page_size;
1889 while (start_sector <= end_sector) {
1890 pg = start_sector * pages_per_sector;
1905 const char *regname,
1916 v = v & ((1 <<
width)-1);
1926 LOG_USER_N(
"\t%*s: %*" PRIu32
" [0x%0*" PRIx32
"] ",
1953 #define nvpsize2 nvpsize
1994 { 0x19,
"AT91SAM9xx Series" },
1995 { 0x29,
"AT91SAM9XExx Series" },
1996 { 0x34,
"AT91x34 Series" },
1997 { 0x37,
"CAP7 Series" },
1998 { 0x39,
"CAP9 Series" },
1999 { 0x3B,
"CAP11 Series" },
2000 { 0x3C,
"ATSAM4E" },
2001 { 0x40,
"AT91x40 Series" },
2002 { 0x42,
"AT91x42 Series" },
2003 { 0x43,
"SAMG51 Series"
2005 { 0x44,
"SAMG55 Series (49-pin WLCSP)" },
2006 { 0x45,
"SAMG55 Series (64-pin)" },
2007 { 0x47,
"SAMG53 Series"
2009 { 0x55,
"AT91x55 Series" },
2010 { 0x60,
"AT91SAM7Axx Series" },
2011 { 0x61,
"AT91SAM7AQxx Series" },
2012 { 0x63,
"AT91x63 Series" },
2013 { 0x64,
"SAM4CxxC (100-pin version)" },
2014 { 0x66,
"SAM4CxxE (144-pin version)" },
2015 { 0x70,
"AT91SAM7Sxx Series" },
2016 { 0x71,
"AT91SAM7XCxx Series" },
2017 { 0x72,
"AT91SAM7SExx Series" },
2018 { 0x73,
"AT91SAM7Lxx Series" },
2019 { 0x75,
"AT91SAM7Xxx Series" },
2020 { 0x76,
"AT91SAM7SLxx Series" },
2021 { 0x80,
"ATSAM3UxC Series (100-pin version)" },
2022 { 0x81,
"ATSAM3UxE Series (144-pin version)" },
2023 { 0x83,
"ATSAM3A/SAM4A xC Series (100-pin version)"},
2024 { 0x84,
"ATSAM3X/SAM4X xC Series (100-pin version)"},
2025 { 0x85,
"ATSAM3X/SAM4X xE Series (144-pin version)"},
2026 { 0x86,
"ATSAM3X/SAM4X xG Series (208/217-pin version)" },
2027 { 0x88,
"ATSAM3S/SAM4S xA Series (48-pin version)" },
2028 { 0x89,
"ATSAM3S/SAM4S xB Series (64-pin version)" },
2029 { 0x8A,
"ATSAM3S/SAM4S xC Series (100-pin version)"},
2030 { 0x92,
"AT91x92 Series" },
2031 { 0x93,
"ATSAM3NxA Series (48-pin version)" },
2032 { 0x94,
"ATSAM3NxB Series (64-pin version)" },
2033 { 0x95,
"ATSAM3NxC Series (100-pin version)" },
2034 { 0x98,
"ATSAM3SDxA Series (48-pin version)" },
2035 { 0x99,
"ATSAM3SDxB Series (64-pin version)" },
2036 { 0x9A,
"ATSAM3SDxC Series (100-pin version)" },
2037 { 0xA5,
"ATSAM5A" },
2038 { 0xF0,
"AT75Cxx Series" },
2044 "romless or onchip flash",
2045 "embedded flash memory",
2046 "rom(nvpsiz) + embedded flash (nvpsiz2)",
2047 "sram emulating flash",
2062 "4 MHz",
"8 MHz",
"12 MHz",
"reserved"
2098 LOG_USER(
"(startup clks, time= %f uSecs)",
2099 ((
float)(v * 1000000)) / ((
float)(chip->
cfg.
slow_freq)));
2102 v ?
"external xtal" :
"internal RC");
2105 LOG_USER(
"(clock failure enabled: %s)",
2160 LOG_USER(
"(%3.03f Mhz (%" PRIu32
".%03" PRIu32
"khz slowclk)",
2168 uint32_t mula, diva;
2176 LOG_USER(
"\tPLLA Freq: (Disabled,mula = 0)");
2178 LOG_USER(
"\tPLLA Freq: (Disabled,diva = 0)");
2179 else if (diva >= 1) {
2181 LOG_USER(
"\tPLLA Freq: %3.03f MHz",
2188 uint32_t css, pres, fin = 0;
2190 const char *cp =
NULL;
2208 fin = 480 * 1000 * 1000;
2212 cp =
"upll (*ERROR* UPLL is disabled)";
2224 switch (pres & 0x07) {
2227 cp =
"selected clock";
2268 LOG_USER(
"\t\tResult CPU Freq: %3.03f",
2304 #define SAM4_ENTRY(NAME, FUNC) { .address = SAM4_ ## NAME, .struct_offset = offsetof( \
2306 NAME), # NAME, FUNC }
2331 return bank->driver_priv;
2349 possible = ((uint32_t *)(
void *)(((
char *)(&(chip->
cfg))) +
reg->struct_offset));
2352 if (possible == goes_here) {
2376 LOG_ERROR(
"Cannot read SAM4 register: %s @ 0x%08" PRIx32
", Err: %d",
2392 LOG_ERROR(
"Cannot read SAM4 register: %s @ 0x%08" PRIx32
", Error: %d",
2417 LOG_USER(
"%*s: [0x%08" PRIx32
"] -> 0x%08" PRIx32,
2422 if (
reg->explain_func)
2423 (*(
reg->explain_func))(chip);
2433 LOG_USER(
" UniqueId: 0x%08" PRIx32
" 0x%08" PRIx32
" 0x%08" PRIx32
" 0x%08"PRIx32,
2445 uint32_t v[4] = {0};
2460 if (!(private->probed))
2469 for (x = 0; x <
private->nsectors; x++)
2470 bank->sectors[x].is_protected = (!!(v[x >> 5] & (1 << (x % 32))));
2490 chip = calloc(1,
sizeof(
struct sam4_chip));
2505 switch (
bank->base) {
2511 bank->bank_number = 0;
2521 bank->bank_number = 1;
2528 "[at91sam4s series] )",
2563 while (details->
name) {
2565 if (details->
chipid_cidr == (private->chip->cfg.CHIPID_CIDR & 0xFFFFFFE0))
2570 if (!details->
name) {
2571 LOG_ERROR(
"SAM4 ChipID 0x%08x not found in table (perhaps you can ID this chip?)",
2572 (
unsigned int)(private->chip->cfg.CHIPID_CIDR));
2574 LOG_INFO(
"SAM4 CHIPID_CIDR: 0x%08" PRIx32
" decodes as follows",
2575 private->chip->cfg.CHIPID_CIDR);
2586 chip =
private->chip;
2599 memcpy(&(private->chip->details),
2601 sizeof(private->chip->details));
2618 int k =
bank->size / 1024;
2625 private->chip->details.name,
2626 private->bank_number,
2647 LOG_ERROR(
"Invalid/unknown bank number");
2656 if (private->chip->probed)
2665 if (
bank->base == private->chip->details.bank[x].base_address) {
2666 bank->size =
private->chip->details.bank[x].size_bytes;
2674 if (!
bank->sectors) {
2675 bank->sectors = calloc(private->nsectors, (
sizeof((
bank->sectors)[0])));
2676 if (!
bank->sectors) {
2680 bank->num_sectors =
private->nsectors;
2682 for (
unsigned int x = 0; x <
bank->num_sectors; x++) {
2683 bank->sectors[x].size =
private->sector_size;
2684 bank->sectors[x].offset = x * (
private->sector_size);
2686 bank->sectors[x].is_erased = -1;
2687 bank->sectors[x].is_protected = -1;
2691 private->probed =
true;
2698 private->bank_number, private->chip->details.n_banks);
2699 if ((private->bank_number + 1) == private->chip->details.n_banks) {
2713 if (
private && private->probed)
2742 if (!(private->probed))
2745 if ((first == 0) && ((last + 1) == private->nsectors)) {
2750 LOG_INFO(
"sam4 does not auto-erase while programming (Erasing relevant sectors)");
2751 LOG_INFO(
"sam4 First: 0x%08x Last: 0x%08x", first, last);
2752 for (
unsigned int i = first; i <= last; i++) {
2755 LOG_INFO(
"Erasing sector: 0x%08x", i);
2757 LOG_ERROR(
"SAM4: Error performing Erase page @ lock region number %u",
2760 LOG_ERROR(
"SAM4: Lock Region %u is locked", i);
2764 LOG_ERROR(
"SAM4: Flash Command error @lock region %u", i);
2785 if (!(private->probed))
2803 adr = pagenum *
private->page_size;
2804 adr = adr +
private->base_address;
2809 private->page_size / 4,
2812 LOG_ERROR(
"SAM4: Flash program failed to read page phys address: 0x%08x",
2813 (
unsigned int)(adr));
2823 r =
target_read_u32(private->chip->target, private->controller_address, &fmr);
2825 LOG_ERROR(
"Error Read failed: read flash mode register");
2833 fmr |= (
private->flash_wait_states << 8);
2835 LOG_DEBUG(
"Flash Mode: 0x%08x", ((
unsigned int)(fmr)));
2836 r =
target_write_u32(private->bank->target, private->controller_address, fmr);
2838 LOG_ERROR(
"Error Write failed: set flash mode register");
2849 adr = pagenum *
private->page_size;
2850 adr = (adr +
private->base_address);
2855 LOG_DEBUG(
"Wr Page %u @ phys address: 0x%08x", pagenum, (
unsigned int)(adr));
2859 private->page_size / 4,
2862 LOG_ERROR(
"SAM4: Failed to write (buffer) page at phys address 0x%08x",
2863 (
unsigned int)(adr));
2874 LOG_ERROR(
"SAM4: Error performing Write page @ phys address 0x%08x",
2875 (
unsigned int)(adr));
2877 LOG_ERROR(
"SAM4: Page @ Phys address 0x%08x is locked", (
unsigned int)(adr));
2881 LOG_ERROR(
"SAM4: Flash Command error @phys address 0x%08x", (
unsigned int)(adr));
2893 unsigned int page_cur;
2894 unsigned int page_end;
2896 unsigned int page_offset;
2898 uint8_t *pagebuffer;
2916 if (!(private->probed)) {
2922 LOG_ERROR(
"Flash write error - past end of bank");
2923 LOG_ERROR(
" offset: 0x%08x, count 0x%08x, BankEnd: 0x%08x",
2925 (
unsigned int)(
count),
2926 (
unsigned int)(private->size_bytes));
2931 pagebuffer = malloc(private->page_size);
2933 LOG_ERROR(
"No memory for %d Byte page buffer", (
int)(private->page_size));
2943 page_cur =
offset /
private->page_size;
2944 page_end = (
offset +
count - 1) / private->page_size;
2947 LOG_DEBUG(
"Page start: %d, Page End: %d", (
int)(page_cur), (
int)(page_end));
2957 if (page_cur == page_end) {
2958 LOG_DEBUG(
"Special case, all in one page");
2963 page_offset = (
offset & (
private->page_size-1));
2964 memcpy(pagebuffer + page_offset,
2976 page_offset =
offset & (
private->page_size - 1);
2985 n = (
private->page_size - page_offset);
2986 memcpy(pagebuffer + page_offset,
3002 assert(
offset % private->page_size == 0);
3007 LOG_DEBUG(
"Full Page Loop: cur=%d, end=%d, count = 0x%08x",
3008 (
int)page_cur, (
int)page_end, (
unsigned int)(
count));
3010 while ((page_cur < page_end) &&
3011 (
count >= private->page_size)) {
3015 count -=
private->page_size;
3016 buffer +=
private->page_size;
3022 LOG_DEBUG(
"Terminal partial page, count = 0x%08x", (
unsigned int)(
count));
3055 "Please define bank %d via command: flash bank %s ... ",
3130 if ((strcmp(
CMD_ARGV[0],
"show") == 0) && (strcmp(
CMD_ARGV[1],
"all") == 0)) {
3142 if (strcmp(
"show",
CMD_ARGV[0]) == 0) {
3170 if (strcmp(
"set",
CMD_ARGV[0]) == 0)
3172 else if ((strcmp(
"clr",
CMD_ARGV[0]) == 0) ||
3173 (strcmp(
"clear",
CMD_ARGV[0]) == 0))
3221 .handler = sam4_handle_gpnvm_command,
3223 .usage =
"[('clr'|'set'|'show') bitnum]",
3224 .help =
"Without arguments, shows all bits in the gpnvm "
3225 "register. Otherwise, clears, sets, or shows one "
3226 "General Purpose Non-Volatile Memory (gpnvm) bit.",
3230 .handler = sam4_handle_info_command,
3232 .help =
"Print information about the current at91sam4 chip "
3233 "and its flash configuration.",
3238 .handler = sam4_handle_slowclk_command,
3240 .usage =
"[clock_hz]",
3241 .help =
"Display or set the slowclock frequency "
3242 "(default 32768 Hz).",
3250 .help =
"at91sam4 flash command group",
3260 .flash_bank_command = sam4_flash_bank_command,
#define FLASH_BANK0_BASE_C32
#define FLASH_BANK1_BASE_2048K_SD
#define AT91C_EFC_FCMD_WPL
static int sam4_page_write(struct sam4_bank_private *private, unsigned int pagenum, const uint8_t *buf)
#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 const struct sam4_chip_details all_sam4_details[]
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 flashd_get_gpnvm(struct sam4_bank_private *private, unsigned int gpnvm, unsigned int *puthere)
Gets current GPNVM state.
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
#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 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 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 flashd_read_uid(struct sam4_bank_private *private)
Read the unique ID.
#define SAM4_MAX_FLASH_BANKS
static const struct command_registration at91sam4_command_handlers[]
static int flashd_set_gpnvm(struct sam4_bank_private *private, unsigned int gpnvm)
Sets the selected GPNVM bit.
static const char *const nvptype[]
static uint32_t sam4_reg_fieldname(struct sam4_chip *chip, const char *regname, uint32_t value, unsigned int shift, unsigned int width)
#define FLASH_BANK1_BASE_C32
static void sam4_explain_ckgr_mcfr(struct sam4_chip *chip)
#define FLASH_BANK0_BASE_SD
static int efc_start_command(struct sam4_bank_private *private, unsigned int command, unsigned int argument)
static int flashd_lock(struct sam4_bank_private *private, unsigned int start_sector, unsigned int end_sector)
Locks regions.
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.
static int flashd_unlock(struct sam4_bank_private *private, unsigned int start_sector, unsigned int end_sector)
Unlocks all the regions in the given address range.
#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)
static int flashd_clr_gpnvm(struct sam4_bank_private *private, unsigned int gpnvm)
Clears the selected GPNVM bit.
#define AT91C_EFC_FCMD_SPUI
static int sam4_page_read(struct sam4_bank_private *private, unsigned int pagenum, uint8_t *buf)
#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 sam4_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static const char *const _rc_freq[]
#define AT91C_EFC_FCMD_CFB
static int efc_perform_command(struct sam4_bank_private *private, unsigned int command, unsigned int argument, uint32_t *status)
Performs the given command and wait until its completion (or an error).
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.
uint64_t buffer
Pointer to data buffer to send over SPI.
#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
unsigned int total_flash_size
struct sam4_bank_private bank[SAM4_MAX_FLASH_BANKS]
unsigned int total_sram_size
unsigned int 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