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;
204 tmp =
RC_FREQ / 16ul * (mcfr & 0xffff);
205 }
else if (at91sam7_info->
ext_freq != 0) {
220 mainfreq =
RC_FREQ / 16ul * (mcfr & 0xffff);
246 uint32_t fmr, fmcn = 0, fws = 0;
255 fmcn = (at91sam7_info->
mck_freq/10000000ul) + 1;
258 fmcn = (at91sam7_info->
mck_freq/1000000ul) + 1;
262 fmcn = (at91sam7_info->
mck_freq/1000000ul)+
263 (at91sam7_info->
mck_freq/2000000ul) + 1;
271 if (at91sam7_info->
mck_freq <= 33333ul)
274 if (at91sam7_info->
mck_freq > 30000000ul)
278 fmr = fmcn << 16 | fws << 8;
290 bank->bank_number)) & waitbits)) && (
timeout-- > 0)) {
300 LOG_ERROR(
"Lock Error Bit Detected, Operation Abort");
302 LOG_ERROR(
"Invalid command and/or bad keyword, Operation Abort");
304 LOG_ERROR(
"Security Bit Set, Operation Abort");
317 fcr = (0x5A << 24) | ((pagen&0x3FF) << 8) |
cmd;
319 LOG_DEBUG(
"Flash command: 0x%" PRIx32
", flash bank: %u, page number: %" PRIu16,
321 bank->bank_number + 1,
346 uint8_t banks_num = 0;
347 uint16_t num_nvmbits = 0;
348 uint16_t sectors_num = 0;
349 uint16_t pages_per_sector = 0;
353 uint32_t base_address = 0;
354 char *target_name_t =
"Unknown";
356 at91sam7_info =
bank->driver_priv;
358 if (at91sam7_info->
cidr != 0) {
360 for (
struct flash_bank *t_bank =
bank; t_bank; t_bank = t_bank->next) {
361 if (t_bank->target !=
target)
379 LOG_WARNING(
"Cannot identify target as an AT91SAM");
385 for (
struct flash_bank *t_bank =
bank; t_bank; t_bank = t_bank->next) {
386 if (t_bank->target !=
target)
389 at91sam7_info = t_bank->driver_priv;
391 at91sam7_info->
cidr = cidr;
392 at91sam7_info->
cidr_ext = (cidr >> 31)&0x0001;
394 at91sam7_info->
cidr_arch = (cidr >> 20)&0x00FF;
398 at91sam7_info->
cidr_eproc = (cidr >> 5)&0x0007;
414 arch = (cidr >> 20)&0x00FF;
417 switch ((cidr >> 8)&0x000F) {
424 pages_per_sector = 32;
426 base_address = 0x00100000;
429 target_name_t =
"AT91SAM7S161/16";
436 pages_per_sector = 32;
438 base_address = 0x00100000;
441 target_name_t =
"AT91SAM7S321/32";
445 target_name_t =
"AT91SAM7SE32";
452 pages_per_sector = 32;
454 base_address = 0x00100000;
457 target_name_t =
"AT91SAM7S64";
464 pages_per_sector = 64;
466 base_address = 0x00100000;
469 target_name_t =
"AT91SAM7S128";
473 target_name_t =
"AT91SAM7XC128";
477 target_name_t =
"AT91SAM7SE128";
481 target_name_t =
"AT91SAM7X128";
488 pages_per_sector = 64;
490 base_address = 0x00100000;
493 target_name_t =
"AT91SAM7A3";
497 target_name_t =
"AT91SAM7S256";
501 target_name_t =
"AT91SAM7XC256";
505 target_name_t =
"AT91SAM7SE256";
509 target_name_t =
"AT91SAM7X256";
516 pages_per_sector = 64;
518 base_address = 0x00100000;
521 target_name_t =
"AT91SAM7S512";
525 target_name_t =
"AT91SAM7XC512";
529 target_name_t =
"AT91SAM7SE512";
533 target_name_t =
"AT91SAM7X512";
544 if (strcmp(target_name_t,
"Unknown") == 0) {
546 "Target autodetection failed! Please specify target parameters in configuration file");
553 bank_size = sectors_num * pages_per_sector *
page_size;
555 for (bnk = 0; bnk < banks_num; bnk++) {
572 LOG_ERROR(
"No memory for flash driver priv");
575 fb->
name = strdup(
"sam7_probed");
580 t_bank = t_bank->
next;
584 t_bank->
base = base_address + bnk * bank_size;
585 t_bank->
size = bank_size;
590 for (sec = 0; sec < sectors_num; sec++) {
599 at91sam7_info->
cidr = cidr;
600 at91sam7_info->
cidr_ext = (cidr >> 31)&0x0001;
602 at91sam7_info->
cidr_arch = (cidr >> 20)&0x00FF;
606 at91sam7_info->
cidr_eproc = (cidr >> 5)&0x0007;
627 LOG_DEBUG(
"nvptyp: 0x%3.3x, arch: 0x%4.4x",
650 uint8_t lock_pos, gpnvm_pos;
655 if (at91sam7_info->
cidr == 0)
666 for (lock_pos = 0; lock_pos <
bank->num_sectors; lock_pos++) {
667 if (((
status >> (16 + lock_pos))&(0x0001)) == 1) {
669 bank->sectors[lock_pos].is_protected = 1;
671 bank->sectors[lock_pos].is_protected = 0;
681 for (gpnvm_pos = 0; gpnvm_pos < at91sam7_info->
num_nvmbits; gpnvm_pos++) {
682 if (((
status >> (8 + gpnvm_pos))&(0x01)) == 1)
695 uint32_t base_address;
697 uint32_t ext_freq = 0;
699 unsigned int banks_num;
700 unsigned int num_sectors;
702 uint16_t pages_per_sector;
704 uint16_t num_nvmbits;
710 at91sam7_info->
cidr = 0;
731 ext_freq =
freq * 1000;
735 if ((banks_num == 0) || (num_sectors == 0) ||
736 (pages_per_sector == 0) || (
page_size == 0) || (num_nvmbits == 0)) {
742 bank_size = num_sectors * pages_per_sector *
page_size;
744 for (
unsigned int bnk = 0; bnk < banks_num; bnk++) {
760 LOG_ERROR(
"No memory for flash driver priv");
763 fb->
name = strdup(
"sam7_probed");
768 t_bank = t_bank->
next;
772 t_bank->
base = base_address + bnk * bank_size;
773 t_bank->
size = bank_size;
778 for (
unsigned int sec = 0; sec <
num_sectors; sec++) {
803 uint32_t nbytes, pos;
807 if (at91sam7_info->
cidr == 0)
815 if ((last < first) || (last >=
bank->num_sectors))
819 if ((first == 0) && (last == (
bank->num_sectors-1)))
831 nbytes = (last - first + 1) *
bank->sectors[first].size;
832 buffer = malloc(nbytes *
sizeof(uint8_t));
833 for (pos = 0; pos < nbytes; pos++)
845 for (
unsigned int sec = first; sec <= last; sec++)
846 bank->sectors[sec].is_erased = 1;
852 unsigned int first,
unsigned int last)
859 if (at91sam7_info->
cidr == 0)
867 if ((last < first) || (last >=
bank->num_sectors))
874 for (
unsigned int sector = first; sector <= last; sector++) {
898 uint32_t dst_min_alignment, wcount, bytes_remaining =
count;
899 uint32_t first_page, last_page, pagen, buffer_pos;
901 if (at91sam7_info->
cidr == 0)
912 dst_min_alignment = at91sam7_info->
pagesize;
914 if (
offset % dst_min_alignment) {
915 LOG_WARNING(
"offset 0x%" PRIx32
" breaks required alignment 0x%" PRIx32,
924 first_page =
offset/dst_min_alignment;
927 LOG_DEBUG(
"first_page: %i, last_page: %i, count %i",
936 for (pagen = first_page; pagen < last_page; pagen++) {
937 if (bytes_remaining < dst_min_alignment)
938 count = bytes_remaining;
940 count = dst_min_alignment;
941 bytes_remaining -=
count;
944 buffer_pos = (pagen-first_page)*dst_min_alignment;
947 wcount,
buffer + buffer_pos);
954 LOG_DEBUG(
"Write flash bank:%u page number:%" PRIu32,
bank->bank_number, pagen);
982 if (at91sam7_info->
cidr == 0)
989 " Cidr: 0x%8.8" PRIx32
" | Arch: 0x%4.4x | Eproc: %s | Version: 0x%3.3x | "
990 "Flashsize: 0x%8.8" PRIx32
"\n",
998 " Master clock (estimated): %u kHz | External clock: %u kHz\n",
999 (
unsigned)(at91sam7_info->
mck_freq / 1000),
1000 (
unsigned)(at91sam7_info->
ext_freq / 1000));
1003 " Pagesize: %i bytes | Lockbits(%u): %i 0x%4.4x | Pages in lock region: %i\n",
1042 if (strcmp(
bank->driver->name,
"at91sam7")) {
1047 LOG_ERROR(
"target has to be halted to perform flash operation");
1051 if (strcmp(
CMD_ARGV[1],
"set") == 0)
1053 else if (strcmp(
CMD_ARGV[1],
"clear") == 0)
1058 at91sam7_info =
bank->driver_priv;
1059 if (at91sam7_info->
cidr == 0) {
1068 "gpnvm bit '#%s' is out of bounds for target %s",
1083 LOG_DEBUG(
"at91sam7_handle_gpnvm_command: cmd 0x%x, value %d, status 0x%" PRIx32,
1097 .handler = at91sam7_handle_gpnvm_command,
1099 .help =
"set or clear one General Purpose Non-Volatile Memory "
1101 .usage =
"bitnum ('set'|'clear')",
1109 .help =
"at91sam7 flash command group",
1118 .usage =
"gpnvm <bit> <set | clear>",
1120 .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.
uint64_t buffer
Pointer to data buffer to send over SPI.
uint32_t page_size
Page size.
#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.
uint8_t default_padded_value
Default padded value used, normally this matches the flash erased value.
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.
uint8_t erased_value
Erased value.
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.