43 #define DBGU_CIDR 0xFFFFF240
44 #define CKGR_MCFR 0xFFFFFC24
45 #define CKGR_MOR 0xFFFFFC20
46 #define CKGR_MCFR_MAINRDY 0x10000
47 #define CKGR_PLLR 0xFFFFFC2c
48 #define CKGR_PLLR_DIV 0xff
49 #define CKGR_PLLR_MUL 0x07ff0000
50 #define PMC_MCKR 0xFFFFFC30
51 #define PMC_MCKR_CSS 0x03
52 #define PMC_MCKR_PRES 0x1c
72 #define FMR_TIMING_NONE 0
73 #define FMR_TIMING_NVBITS 1
74 #define FMR_TIMING_FLASH 2
77 #define FLASH_SIZE_8KB 1
78 #define FLASH_SIZE_16KB 2
79 #define FLASH_SIZE_32KB 3
80 #define FLASH_SIZE_64KB 5
81 #define FLASH_SIZE_128KB 7
82 #define FLASH_SIZE_256KB 9
83 #define FLASH_SIZE_512KB 10
84 #define FLASH_SIZE_1024KB 12
85 #define FLASH_SIZE_2048KB 14
96 static const uint32_t
mc_fmr[4] = { 0xFFFFFF60, 0xFFFFFF70, 0xFFFFFF80, 0xFFFFFF90 };
97 static const uint32_t
mc_fcr[4] = { 0xFFFFFF64, 0xFFFFFF74, 0xFFFFFF84, 0xFFFFFF94 };
98 static const uint32_t
mc_fsr[4] = { 0xFFFFFF68, 0xFFFFFF78, 0xFFFFFF88, 0xFFFFFF98 };
101 "Unknown",
"ARM946-E",
"ARM7TDMI",
"Unknown",
"ARM920T",
"ARM926EJ-S",
"Unknown",
"Unknown"
148 static long SRAMSIZ[16] = {
181 uint32_t mckr, mcfr, pllr, mor;
182 unsigned long tmp = 0, mainfreq;
205 tmp =
RC_FREQ / 16ul * (mcfr & 0xffff);
206 }
else if (at91sam7_info->
ext_freq != 0) {
222 mainfreq =
RC_FREQ / 16ul * (mcfr & 0xffff);
227 }
else if ((at91sam7_info->
ext_freq != 0) &&
249 uint32_t fmr, fmcn = 0, fws = 0;
258 fmcn = (at91sam7_info->
mck_freq/10000000ul) + 1;
261 fmcn = (at91sam7_info->
mck_freq/1000000ul) + 1;
265 fmcn = (at91sam7_info->
mck_freq/1000000ul)+
266 (at91sam7_info->
mck_freq/2000000ul) + 1;
274 if (at91sam7_info->
mck_freq <= 33333ul)
277 if (at91sam7_info->
mck_freq > 30000000ul)
281 fmr = fmcn << 16 | fws << 8;
293 bank->bank_number)) & waitbits)) && (
timeout-- > 0)) {
303 LOG_ERROR(
"Lock Error Bit Detected, Operation Abort");
305 LOG_ERROR(
"Invalid command and/or bad keyword, Operation Abort");
307 LOG_ERROR(
"Security Bit Set, Operation Abort");
320 fcr = (0x5A << 24) | ((pagen&0x3FF) << 8) |
cmd;
322 LOG_DEBUG(
"Flash command: 0x%" PRIx32
", flash bank: %i, page number: %u",
324 bank->bank_number + 1,
349 uint8_t banks_num = 0;
350 uint16_t num_nvmbits = 0;
351 uint16_t sectors_num = 0;
352 uint16_t pages_per_sector = 0;
353 uint16_t page_size = 0;
356 uint32_t base_address = 0;
357 char *target_name_t =
"Unknown";
359 at91sam7_info =
bank->driver_priv;
361 if (at91sam7_info->
cidr != 0) {
363 for (
struct flash_bank *t_bank =
bank; t_bank; t_bank = t_bank->next) {
364 if (t_bank->target !=
target)
382 LOG_WARNING(
"Cannot identify target as an AT91SAM");
388 for (
struct flash_bank *t_bank =
bank; t_bank; t_bank = t_bank->next) {
389 if (t_bank->target !=
target)
392 at91sam7_info = t_bank->driver_priv;
394 at91sam7_info->
cidr = cidr;
395 at91sam7_info->
cidr_ext = (cidr >> 31)&0x0001;
397 at91sam7_info->
cidr_arch = (cidr >> 20)&0x00FF;
401 at91sam7_info->
cidr_eproc = (cidr >> 5)&0x0007;
417 arch = (cidr >> 20)&0x00FF;
420 switch ((cidr >> 8)&0x000F) {
427 pages_per_sector = 32;
429 base_address = 0x00100000;
432 target_name_t =
"AT91SAM7S161/16";
439 pages_per_sector = 32;
441 base_address = 0x00100000;
444 target_name_t =
"AT91SAM7S321/32";
448 target_name_t =
"AT91SAM7SE32";
455 pages_per_sector = 32;
457 base_address = 0x00100000;
460 target_name_t =
"AT91SAM7S64";
467 pages_per_sector = 64;
469 base_address = 0x00100000;
472 target_name_t =
"AT91SAM7S128";
476 target_name_t =
"AT91SAM7XC128";
480 target_name_t =
"AT91SAM7SE128";
484 target_name_t =
"AT91SAM7X128";
491 pages_per_sector = 64;
493 base_address = 0x00100000;
496 target_name_t =
"AT91SAM7A3";
500 target_name_t =
"AT91SAM7S256";
504 target_name_t =
"AT91SAM7XC256";
508 target_name_t =
"AT91SAM7SE256";
512 target_name_t =
"AT91SAM7X256";
519 pages_per_sector = 64;
521 base_address = 0x00100000;
524 target_name_t =
"AT91SAM7S512";
528 target_name_t =
"AT91SAM7XC512";
532 target_name_t =
"AT91SAM7SE512";
536 target_name_t =
"AT91SAM7X512";
547 if (strcmp(target_name_t,
"Unknown") == 0) {
549 "Target autodetection failed! Please specify target parameters in configuration file");
556 bank_size = sectors_num * pages_per_sector * page_size;
558 for (bnk = 0; bnk < banks_num; bnk++) {
567 fb->
name =
"sam7_probed";
573 t_bank = t_bank->
next;
577 t_bank->
base = base_address + bnk * bank_size;
578 t_bank->
size = bank_size;
583 for (sec = 0; sec < sectors_num; sec++) {
584 t_bank->
sectors[sec].
offset = sec * pages_per_sector * page_size;
585 t_bank->
sectors[sec].
size = pages_per_sector * page_size;
592 at91sam7_info->
cidr = cidr;
593 at91sam7_info->
cidr_ext = (cidr >> 31)&0x0001;
595 at91sam7_info->
cidr_arch = (cidr >> 20)&0x00FF;
599 at91sam7_info->
cidr_eproc = (cidr >> 5)&0x0007;
607 at91sam7_info->
pagesize = page_size;
620 LOG_DEBUG(
"nvptyp: 0x%3.3x, arch: 0x%4.4x",
643 uint8_t lock_pos, gpnvm_pos;
648 if (at91sam7_info->
cidr == 0)
659 for (lock_pos = 0; lock_pos <
bank->num_sectors; lock_pos++) {
660 if (((
status >> (16 + lock_pos))&(0x0001)) == 1) {
662 bank->sectors[lock_pos].is_protected = 1;
664 bank->sectors[lock_pos].is_protected = 0;
674 for (gpnvm_pos = 0; gpnvm_pos < at91sam7_info->
num_nvmbits; gpnvm_pos++) {
675 if (((
status >> (8 + gpnvm_pos))&(0x01)) == 1)
688 uint32_t base_address;
690 uint32_t ext_freq = 0;
692 unsigned int banks_num;
693 unsigned int num_sectors;
695 uint16_t pages_per_sector;
697 uint16_t num_nvmbits;
703 at91sam7_info->
cidr = 0;
724 ext_freq =
freq * 1000;
728 if ((banks_num == 0) || (num_sectors == 0) ||
729 (pages_per_sector == 0) || (page_size == 0) || (num_nvmbits == 0)) {
735 bank_size = num_sectors * pages_per_sector * page_size;
737 for (
unsigned int bnk = 0; bnk < banks_num; bnk++) {
745 fb->
name =
"sam7_probed";
751 t_bank = t_bank->
next;
755 t_bank->
base = base_address + bnk * bank_size;
756 t_bank->
size = bank_size;
761 for (
unsigned int sec = 0; sec <
num_sectors; sec++) {
762 t_bank->
sectors[sec].
offset = sec * pages_per_sector * page_size;
763 t_bank->
sectors[sec].
size = pages_per_sector * page_size;
775 at91sam7_info->
pagesize = page_size;
786 uint32_t nbytes, pos;
790 if (at91sam7_info->
cidr == 0)
798 if ((last < first) || (last >=
bank->num_sectors))
802 if ((first == 0) && (last == (
bank->num_sectors-1)))
814 nbytes = (last - first + 1) *
bank->sectors[first].size;
815 buffer = malloc(nbytes *
sizeof(uint8_t));
816 for (pos = 0; pos < nbytes; pos++)
828 for (
unsigned int sec = first; sec <= last; sec++)
829 bank->sectors[sec].is_erased = 1;
835 unsigned int first,
unsigned int last)
842 if (at91sam7_info->
cidr == 0)
850 if ((last < first) || (last >=
bank->num_sectors))
857 for (
unsigned int sector = first; sector <= last; sector++) {
881 uint32_t dst_min_alignment, wcount, bytes_remaining =
count;
882 uint32_t first_page, last_page, pagen, buffer_pos;
884 if (at91sam7_info->
cidr == 0)
895 dst_min_alignment = at91sam7_info->
pagesize;
897 if (
offset % dst_min_alignment) {
898 LOG_WARNING(
"offset 0x%" PRIx32
" breaks required alignment 0x%" PRIx32
"",
907 first_page =
offset/dst_min_alignment;
910 LOG_DEBUG(
"first_page: %i, last_page: %i, count %i",
919 for (pagen = first_page; pagen < last_page; pagen++) {
920 if (bytes_remaining < dst_min_alignment)
921 count = bytes_remaining;
923 count = dst_min_alignment;
924 bytes_remaining -=
count;
927 buffer_pos = (pagen-first_page)*dst_min_alignment;
930 wcount,
buffer + buffer_pos);
937 LOG_DEBUG(
"Write flash bank:%u page number:%" PRIu32,
bank->bank_number, pagen);
965 if (at91sam7_info->
cidr == 0)
972 " Cidr: 0x%8.8" PRIx32
" | Arch: 0x%4.4x | Eproc: %s | Version: 0x%3.3x | "
973 "Flashsize: 0x%8.8" PRIx32
"\n",
981 " Master clock (estimated): %u kHz | External clock: %u kHz\n",
982 (
unsigned)(at91sam7_info->
mck_freq / 1000),
983 (
unsigned)(at91sam7_info->
ext_freq / 1000));
986 " Pagesize: %i bytes | Lockbits(%u): %i 0x%4.4x | Pages in lock region: %i\n",
1025 if (strcmp(
bank->driver->name,
"at91sam7")) {
1030 LOG_ERROR(
"target has to be halted to perform flash operation");
1034 if (strcmp(
CMD_ARGV[1],
"set") == 0)
1036 else if (strcmp(
CMD_ARGV[1],
"clear") == 0)
1041 at91sam7_info =
bank->driver_priv;
1042 if (at91sam7_info->
cidr == 0) {
1051 "gpnvm bit '#%s' is out of bounds for target %s",
1066 LOG_DEBUG(
"at91sam7_handle_gpnvm_command: cmd 0x%x, value %d, status 0x%" PRIx32,
1080 .handler = at91sam7_handle_gpnvm_command,
1082 .help =
"set or clear one General Purpose Non-Volatile Memory "
1084 .usage =
"bitnum ('set'|'clear')",
1092 .help =
"at91sam7 flash command group",
1101 .usage =
"gpnvm <bit> <set | clear>",
1103 .flash_bank_command = at91sam7_flash_bank_command,
static int get_at91sam7_info(struct flash_bank *bank, struct command_invocation *cmd)
#define FLASH_SIZE_2048KB
static const struct command_registration at91sam7_command_handlers[]
const struct flash_driver at91sam7_flash
static int at91sam7_erase_check(struct flash_bank *bank)
static const struct command_registration at91sam7_exec_command_handlers[]
static int at91sam7_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
COMMAND_HANDLER(at91sam7_handle_gpnvm_command)
static int at91sam7_flash_command(struct flash_bank *bank, uint8_t cmd, uint16_t pagen)
static void at91sam7_read_clock_info(struct flash_bank *bank)
static uint32_t at91sam7_get_flash_status(struct target *target, int bank_number)
static int at91sam7_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
#define FMR_TIMING_NVBITS
static const char * eproc[8]
static void at91sam7_set_flash_mode(struct flash_bank *bank, int mode)
static uint32_t at91sam7_wait_status_busy(struct flash_bank *bank, uint32_t waitbits, int timeout)
static const uint32_t mc_fmr[4]
static int at91sam7_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static const uint32_t mc_fsr[4]
#define CKGR_MCFR_MAINRDY
static int at91sam7_read_part_info(struct flash_bank *bank)
static const uint32_t mc_fcr[4]
static int at91sam7_protect_check(struct flash_bank *bank)
#define FLASH_SIZE_1024KB
static int at91sam7_probe(struct flash_bank *bank)
FLASH_BANK_COMMAND_HANDLER(at91sam7_flash_bank_command)
Support functions to access arbitrary bits in a byte array.
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_INVALID
#define ERROR_FLASH_SECTOR_INVALID
#define ERROR_FLASH_BANK_NOT_PROBED
#define ERROR_FLASH_OPERATION_FAILED
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
#define ERROR_FLASH_DST_OUT_OF_BANK
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
struct flash_bank * get_flash_bank_by_num_noprobe(unsigned int num)
Returns the flash bank like get_flash_bank_by_num(), without probing.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
void alive_sleep(uint64_t ms)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_DEBUG(expr ...)
static uint32_t bit(uint32_t value, unsigned int b)
uint8_t flash_autodetection
uint16_t pages_per_sector
uint16_t pages_in_lockregion
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.
struct flash_sector * sectors
Array of sectors, allocated and initialized by the flash driver.
const struct flash_driver * driver
Driver for this bank.
target_addr_t base
The base address of this bank.
void * driver_priv
Private driver storage pointer.
uint32_t size
The size of this chip bank, in bytes.
unsigned int num_sectors
The number of sectors on this chip.
struct flash_bank * next
The next flash bank on this chip.
struct target * target
Target to which this bank belongs.
unsigned int bank_number
The 'bank' (or chip number) of this instance.
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...
Describes the geometry and status of a single flash sector within a flash bank.
int is_erased
Indication of erasure status: 0 = not erased, 1 = erased, other = unknown.
uint32_t offset
Bus offset from start of the flash chip (in bytes).
int is_protected
Indication of protection status: 0 = unprotected/unlocked, 1 = protected/locked, other = unknown.
uint32_t size
Number of bytes in this flash sector.
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)
#define ERROR_TARGET_NOT_HALTED
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.