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++) {
575 LOG_ERROR(
"No memory for flash driver priv");
578 fb->
name = strdup(
"sam7_probed");
583 t_bank = t_bank->
next;
587 t_bank->
base = base_address + bnk * bank_size;
588 t_bank->
size = bank_size;
593 for (sec = 0; sec < sectors_num; sec++) {
594 t_bank->
sectors[sec].
offset = sec * pages_per_sector * page_size;
595 t_bank->
sectors[sec].
size = pages_per_sector * page_size;
602 at91sam7_info->
cidr = cidr;
603 at91sam7_info->
cidr_ext = (cidr >> 31)&0x0001;
605 at91sam7_info->
cidr_arch = (cidr >> 20)&0x00FF;
609 at91sam7_info->
cidr_eproc = (cidr >> 5)&0x0007;
617 at91sam7_info->
pagesize = page_size;
630 LOG_DEBUG(
"nvptyp: 0x%3.3x, arch: 0x%4.4x",
653 uint8_t lock_pos, gpnvm_pos;
658 if (at91sam7_info->
cidr == 0)
669 for (lock_pos = 0; lock_pos <
bank->num_sectors; lock_pos++) {
670 if (((
status >> (16 + lock_pos))&(0x0001)) == 1) {
672 bank->sectors[lock_pos].is_protected = 1;
674 bank->sectors[lock_pos].is_protected = 0;
684 for (gpnvm_pos = 0; gpnvm_pos < at91sam7_info->
num_nvmbits; gpnvm_pos++) {
685 if (((
status >> (8 + gpnvm_pos))&(0x01)) == 1)
698 uint32_t base_address;
700 uint32_t ext_freq = 0;
702 unsigned int banks_num;
703 unsigned int num_sectors;
705 uint16_t pages_per_sector;
707 uint16_t num_nvmbits;
713 at91sam7_info->
cidr = 0;
734 ext_freq =
freq * 1000;
738 if ((banks_num == 0) || (num_sectors == 0) ||
739 (pages_per_sector == 0) || (page_size == 0) || (num_nvmbits == 0)) {
745 bank_size = num_sectors * pages_per_sector * page_size;
747 for (
unsigned int bnk = 0; bnk < banks_num; bnk++) {
763 LOG_ERROR(
"No memory for flash driver priv");
766 fb->
name = strdup(
"sam7_probed");
771 t_bank = t_bank->
next;
775 t_bank->
base = base_address + bnk * bank_size;
776 t_bank->
size = bank_size;
781 for (
unsigned int sec = 0; sec <
num_sectors; sec++) {
782 t_bank->
sectors[sec].
offset = sec * pages_per_sector * page_size;
783 t_bank->
sectors[sec].
size = pages_per_sector * page_size;
795 at91sam7_info->
pagesize = page_size;
806 uint32_t nbytes, pos;
810 if (at91sam7_info->
cidr == 0)
818 if ((last < first) || (last >=
bank->num_sectors))
822 if ((first == 0) && (last == (
bank->num_sectors-1)))
834 nbytes = (last - first + 1) *
bank->sectors[first].size;
835 buffer = malloc(nbytes *
sizeof(uint8_t));
836 for (pos = 0; pos < nbytes; pos++)
848 for (
unsigned int sec = first; sec <= last; sec++)
849 bank->sectors[sec].is_erased = 1;
855 unsigned int first,
unsigned int last)
862 if (at91sam7_info->
cidr == 0)
870 if ((last < first) || (last >=
bank->num_sectors))
877 for (
unsigned int sector = first; sector <= last; sector++) {
901 uint32_t dst_min_alignment, wcount, bytes_remaining =
count;
902 uint32_t first_page, last_page, pagen, buffer_pos;
904 if (at91sam7_info->
cidr == 0)
915 dst_min_alignment = at91sam7_info->
pagesize;
917 if (
offset % dst_min_alignment) {
918 LOG_WARNING(
"offset 0x%" PRIx32
" breaks required alignment 0x%" PRIx32
"",
927 first_page =
offset/dst_min_alignment;
930 LOG_DEBUG(
"first_page: %i, last_page: %i, count %i",
939 for (pagen = first_page; pagen < last_page; pagen++) {
940 if (bytes_remaining < dst_min_alignment)
941 count = bytes_remaining;
943 count = dst_min_alignment;
944 bytes_remaining -=
count;
947 buffer_pos = (pagen-first_page)*dst_min_alignment;
950 wcount,
buffer + buffer_pos);
957 LOG_DEBUG(
"Write flash bank:%u page number:%" PRIu32,
bank->bank_number, pagen);
985 if (at91sam7_info->
cidr == 0)
992 " Cidr: 0x%8.8" PRIx32
" | Arch: 0x%4.4x | Eproc: %s | Version: 0x%3.3x | "
993 "Flashsize: 0x%8.8" PRIx32
"\n",
1001 " Master clock (estimated): %u kHz | External clock: %u kHz\n",
1002 (
unsigned)(at91sam7_info->
mck_freq / 1000),
1003 (
unsigned)(at91sam7_info->
ext_freq / 1000));
1006 " Pagesize: %i bytes | Lockbits(%u): %i 0x%4.4x | Pages in lock region: %i\n",
1045 if (strcmp(
bank->driver->name,
"at91sam7")) {
1050 LOG_ERROR(
"target has to be halted to perform flash operation");
1054 if (strcmp(
CMD_ARGV[1],
"set") == 0)
1056 else if (strcmp(
CMD_ARGV[1],
"clear") == 0)
1061 at91sam7_info =
bank->driver_priv;
1062 if (at91sam7_info->
cidr == 0) {
1071 "gpnvm bit '#%s' is out of bounds for target %s",
1086 LOG_DEBUG(
"at91sam7_handle_gpnvm_command: cmd 0x%x, value %d, status 0x%" PRIx32,
1100 .handler = at91sam7_handle_gpnvm_command,
1102 .help =
"set or clear one General Purpose Non-Volatile Memory "
1104 .usage =
"bitnum ('set'|'clear')",
1112 .help =
"at91sam7 flash command group",
1121 .usage =
"gpnvm <bit> <set | clear>",
1123 .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.
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.