22 #define FLASH_PAGE_SIZE 512
23 #define FLASH_ERASE_TIME 100000
24 #define FLASH_PROGRAM_TIME 1000
27 #define CHIPID 0xE0000000
28 #define FEAT0 0xE0000100
29 #define FEAT1 0xE0000104
30 #define FEAT2 0xE0000108
31 #define FEAT3 0xE000010C
33 #define EXPECTED_CHIPID 0x209CE02B
36 #define FCTR 0x20200000
37 #define FPTR 0x20200008
38 #define FTCTR 0x2020000C
39 #define FBWST 0x20200010
40 #define FCRA 0x2020001C
41 #define FMSSTART 0x20200020
42 #define FMSSTOP 0x20200024
43 #define FMS16 0x20200028
44 #define FMSW0 0x2020002C
45 #define FMSW1 0x20200030
46 #define FMSW2 0x20200034
47 #define FMSW3 0x20200038
49 #define EECMD 0x20200080
50 #define EEADDR 0x20200084
51 #define EEWDATA 0x20200088
52 #define EERDATA 0x2020008C
53 #define EEWSTATE 0x20200090
54 #define EECLKDIV 0x20200094
55 #define EEPWRDWN 0x20200098
56 #define EEMSSTART 0x2020009C
57 #define EEMSSTOP 0x202000A0
58 #define EEMSSIG 0x202000A4
60 #define INT_CLR_ENABLE 0x20200FD8
61 #define INT_SET_ENABLE 0x20200FDC
62 #define INT_STATUS 0x20200FE0
63 #define INT_ENABLE 0x20200FE4
64 #define INT_CLR_STATUS 0x20200FE8
65 #define INT_SET_STATUS 0x20200FEC
68 #define INTSRC_END_OF_PROG (1 << 28)
69 #define INTSRC_END_OF_BIST (1 << 27)
70 #define INTSRC_END_OF_RDWR (1 << 26)
71 #define INTSRC_END_OF_MISR (1 << 2)
72 #define INTSRC_END_OF_BURN (1 << 1)
73 #define INTSRC_END_OF_ERASE (1 << 0)
76 #define FCTR_FS_LOADREQ (1 << 15)
77 #define FCTR_FS_CACHECLR (1 << 14)
78 #define FCTR_FS_CACHEBYP (1 << 13)
79 #define FCTR_FS_PROGREQ (1 << 12)
80 #define FCTR_FS_RLS (1 << 11)
81 #define FCTR_FS_PDL (1 << 10)
82 #define FCTR_FS_PD (1 << 9)
83 #define FCTR_FS_WPB (1 << 7)
84 #define FCTR_FS_ISS (1 << 6)
85 #define FCTR_FS_RLD (1 << 5)
86 #define FCTR_FS_DCR (1 << 4)
87 #define FCTR_FS_WEB (1 << 2)
88 #define FCTR_FS_WRE (1 << 1)
89 #define FCTR_FS_CS (1 << 0)
91 #define FPTR_EN_T (1 << 15)
93 #define FTCTR_FS_BYPASS_R (1 << 29)
94 #define FTCTR_FS_BYPASS_W (1 << 28)
96 #define FMSSTOP_MISR_START (1 << 17)
98 #define EEMSSTOP_STRTBIST (1 << 31)
101 #define ISS_CUSTOMER_START1 (0x830)
102 #define ISS_CUSTOMER_END1 (0xA00)
103 #define ISS_CUSTOMER_SIZE1 (ISS_CUSTOMER_END1 - ISS_CUSTOMER_START1)
104 #define ISS_CUSTOMER_NWORDS1 (ISS_CUSTOMER_SIZE1 / 4)
105 #define ISS_CUSTOMER_START2 (0xA40)
106 #define ISS_CUSTOMER_END2 (0xC00)
107 #define ISS_CUSTOMER_SIZE2 (ISS_CUSTOMER_END2 - ISS_CUSTOMER_START2)
108 #define ISS_CUSTOMER_NWORDS2 (ISS_CUSTOMER_SIZE2 / 4)
109 #define ISS_CUSTOMER_SIZE (ISS_CUSTOMER_SIZE1 + ISS_CUSTOMER_SIZE2)
164 uint32_t addr_from, uint32_t addr_to,
165 uint32_t signature[4]);
167 static uint32_t
lpc2900_calc_tr(uint32_t clock_var, uint32_t time_var);
191 }
while (((int_status &
mask) == 0) && (
timeout != 0));
216 fcra = (lpc2900_info->
clk_sys_fmc / (3 * 66000)) - 1;
259 uint32_t iss_secured_field[0x230/16][4];
261 (uint8_t *)iss_secured_field);
275 for (
unsigned int sector = 0; sector <
bank->num_sectors; sector++) {
276 unsigned int index_t;
280 index_t = sector + 11;
281 else if (sector <= 7)
282 index_t = sector + 27;
284 index_t = sector - 8;
286 bank->sectors[sector].is_protected = -1;
288 if ((iss_secured_field[index_t][0] == 0x00000000) &&
289 (iss_secured_field[index_t][1] == 0x00000000) &&
290 (iss_secured_field[index_t][2] == 0x00000000) &&
291 (iss_secured_field[index_t][3] == 0x00000000))
292 bank->sectors[sector].is_protected = 1;
294 if ((iss_secured_field[index_t][0] == 0xFFFFFFFF) &&
295 (iss_secured_field[index_t][1] == 0xFFFFFFFF) &&
296 (iss_secured_field[index_t][2] == 0xFFFFFFFF) &&
297 (iss_secured_field[index_t][3] == 0xFFFFFFFF))
298 bank->sectors[sector].is_protected = 0;
315 uint32_t signature[4])
332 uint8_t sig_buf[4 * 4];
354 for (
unsigned int sector = 0; sector <
bank->num_sectors; sector++) {
356 if (address < (
bank->sectors[sector].offset +
bank->sectors[sector].size))
376 if ((pagenum < 4) || (pagenum > 7)) {
377 LOG_ERROR(
"Refuse to burn index sector page %d", pagenum);
408 LOG_ERROR(
"Index sector write failed @ page %d", pagenum);
431 LOG_ERROR(
"Index sector write failed @ page %d", pagenum);
455 uint32_t tr_val = (uint32_t)((((time_var / 1e6) * clock_var) + 511.0) / 512.0);
472 uint32_t signature[4];
496 signature[3], signature[2], signature[1], signature[0]);
518 lpc2900_info->
risky = 0;
556 LOG_ERROR(
"Write operation to file %s failed", filename);
581 #define ISS_PASSWORD "I_know_what_I_am_doing"
585 if (!lpc2900_info->
risky) {
591 "Potentially dangerous operation allowed in next command!");
612 if (!lpc2900_info->
risky) {
616 lpc2900_info->
risky = 0;
640 LOG_ERROR(
"Only one section allowed in image file.");
645 LOG_ERROR(
"Incorrect image file size. Expected %d, "
663 LOG_ERROR(
"couldn't read from file '%s'", filename);
679 LOG_ERROR(
"couldn't read from file '%s'", filename);
711 if (!lpc2900_info->
risky) {
713 "(use 'password' command first)");
716 lpc2900_info->
risky = 0;
719 unsigned int first, last;
722 if ((first >=
bank->num_sectors) ||
723 (last >=
bank->num_sectors) ||
732 if ((first <= 4) || (last >= 8)) {
734 for (
unsigned int sector = first; sector <= last; sector++) {
736 memset(&page[0xB0 + 16*sector], 0, 16);
737 else if (sector >= 8)
738 memset(&page[0x00 + 16*(sector - 8)], 0, 16);
743 LOG_ERROR(
"failed to update index sector page 6");
749 if ((first <= 7) && (last >= 5)) {
751 for (
unsigned int sector = first; sector <= last; sector++) {
752 if ((sector >= 5) && (sector <= 7))
753 memset(&page[0x00 + 16*(sector - 5)], 0, 16);
758 LOG_ERROR(
"failed to update index sector page 7");
764 "Sectors security will become effective after next power cycle");
768 LOG_ERROR(
"Cannot determine sector security status");
792 if (!lpc2900_info->
risky) {
794 "(use 'password' command first)");
797 lpc2900_info->
risky = 0;
805 page[0x30 + 15] = 0x7F;
806 page[0x30 + 11] = 0x7F;
807 page[0x30 + 7] = 0x7F;
808 page[0x30 + 3] = 0x7F;
813 LOG_ERROR(
"failed to update index sector page 5");
817 LOG_INFO(
"JTAG security set. Good bye!");
828 .handler = lpc2900_handle_signature_command,
830 .help =
"Calculate and display signature of flash bank.",
833 .name =
"read_custom",
834 .handler = lpc2900_handle_read_custom_command,
836 .usage =
"bank_id filename",
837 .help =
"Copies 912 bytes of customer information "
838 "from index sector into file.",
842 .handler = lpc2900_handle_password_command,
844 .usage =
"bank_id password",
845 .help =
"Enter fixed password to enable 'dangerous' options.",
848 .name =
"write_custom",
849 .handler = lpc2900_handle_write_custom_command,
851 .usage =
"bank_id filename ('bin'|'ihex'|'elf'|'s19')",
852 .help =
"Copies 912 bytes of customer info from file "
856 .name =
"secure_sector",
857 .handler = lpc2900_handle_secure_sector_command,
859 .usage =
"bank_id first_sector last_sector",
860 .help =
"Activate sector security for a range of sectors. "
861 "It will be effective after a power cycle.",
864 .name =
"secure_jtag",
865 .handler = lpc2900_handle_secure_jtag_command,
868 .help =
"Disable the JTAG port. "
869 "It will be effective after a power cycle.",
878 .help =
"LPC2900 flash command group",
894 bank->driver_priv = lpc2900_info;
904 uint32_t clock_limit;
908 LOG_WARNING(
"flash clock must be at least %" PRIu32
" kHz",
909 (clock_limit / 1000));
916 LOG_WARNING(
"flash clock must be a maximum of %" PRIu32
" kHz",
917 (clock_limit / 1000));
939 unsigned int last_unsecured_sector;
940 bool has_unsecured_sector;
950 if ((last < first) || (last >=
bank->num_sectors)) {
964 last_unsecured_sector = -1;
965 has_unsecured_sector =
false;
966 for (
unsigned int sector = first; sector <= last; sector++) {
967 if (!
bank->sectors[sector].is_protected) {
968 last_unsecured_sector = sector;
969 has_unsecured_sector =
true;
977 if (!has_unsecured_sector)
992 for (
unsigned int sector = first; sector <= last_unsecured_sector; sector++) {
996 if (!
bank->sectors[sector].is_protected) {
1006 if (sector == last_unsecured_sector) {
1055 static const uint32_t write_target_code[] = {
1104 for (
unsigned int sector = 0; sector <
bank->num_sectors; sector++) {
1108 (
bank->sectors[sector].offset +
bank->sectors[sector].size)) &&
1113 if (!
bank->sectors[sector].is_protected) {
1129 const uint32_t target_code_size =
sizeof(write_target_code);
1136 buffer_size + target_code_size,
1139 buffer_size -= 1 *
KiB;
1140 if (buffer_size < 2 *
KiB) {
1141 LOG_INFO(
"no (large enough) working area, falling back to host mode");
1152 uint8_t code[
sizeof(write_target_code)];
1157 LOG_ERROR(
"Unable to write block write code to target");
1169 while (
count != 0) {
1170 uint32_t this_npages;
1171 const uint8_t *this_buffer;
1178 memset(&page, 0xff,
sizeof(page));
1183 this_buffer = &page[0];
1188 memset(&page, 0xff,
sizeof(page));
1191 this_buffer = &page[0];
1195 this_npages = (
count < buffer_size) ?
1201 unsigned int sector = start_sector + 1;
1202 while (sector < bank->num_sectors) {
1204 if (
bank->sectors[sector].is_protected) {
1206 if ((
bank->sectors[sector].offset -
bank->base) <
1210 (
bank->sectors[sector].offset -
1222 if (
bank->sectors[start_sector].is_protected) {
1227 if (start_sector ==
bank->num_sectors - 1)
1231 uint32_t nskip =
bank->sectors[start_sector].size -
1243 LOG_ERROR(
"Unable to write data to target");
1251 buf_set_u32(reg_params[2].value, 0, 32, this_npages);
1261 (warea->
address) + buffer_size,
1262 (warea->
address) + buffer_size + target_code_size - 4,
1267 LOG_ERROR(
"Execution of flash algorithm failed.");
1287 while (
count != 0) {
1387 uint32_t feat0, feat1, feat2, feat3;
1394 bank->base = 0x20000000;
1397 uint32_t num_64k_sectors = (feat2 >> 16) & 0xFF;
1398 uint32_t num_8k_sectors = (feat2 >> 0) & 0xFF;
1399 bank->num_sectors = num_64k_sectors + num_8k_sectors;
1400 bank->size =
KiB * (64 * num_64k_sectors + 8 * num_8k_sectors);
1404 if ((feat1 & 0x30) == 0x30) {
1406 if ((feat1 & 0x0C) == 0x0C)
1411 uint32_t package_code = feat0 & 0x0F;
1412 uint32_t itcm_code = (feat1 >> 16) & 0x1F;
1416 if ((package_code == 4) && (itcm_code == 5)) {
1420 if (package_code == 2) {
1424 else if (
bank->size == 256*
KiB)
1426 else if (
bank->size == 512*
KiB)
1430 }
else if (package_code == 4) {
1432 if ((
bank->size == 256*
KiB) && (feat3 == 0xFFFFFFE9))
1434 else if ((
bank->size == 512*
KiB) && (feat3 == 0xFFFFFCF0))
1436 else if ((
bank->size == 512*
KiB) && (feat3 == 0xFFFFFFF1))
1438 else if ((
bank->size == 768*
KiB) && (feat3 == 0xFFFFFCF8))
1440 else if ((
bank->size == 768*
KiB) && (feat3 == 0xFFFFFFF9))
1444 }
else if (package_code == 5) {
1446 lpc2900_info->
target_name = (
bank->size == 0) ?
"LPC2930" :
"LPC2939";
1453 "%08" PRIx32
":%08" PRIx32
":%08" PRIx32
":%08" PRIx32
")",
1454 feat0, feat1, feat2, feat3);
1459 LOG_INFO(
"Flash bank %u: Device %s, %" PRIu32
1460 " KiB in %u sectors",
1466 if (
bank->num_sectors == 0) {
1467 LOG_WARNING(
"Flashless device cannot be handled");
1479 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
1481 bank->sectors[i].is_erased = -1;
1482 bank->sectors[i].is_protected = -1;
1485 bank->sectors[i].size = 8 *
KiB;
1487 bank->sectors[i].size = 64 *
KiB;
1492 bank->sectors[i].size = 0;
1493 LOG_ERROR(
"Never heard about sector %u", i);
1503 LOG_ERROR(
"Cannot determine sector security status");
1523 LOG_INFO(
"Processor not halted/not probed");
1530 for (
unsigned int sector = 0; sector <
bank->num_sectors; sector++) {
1531 uint32_t signature[4];
1533 bank->sectors[sector].offset + (
bank->sectors[sector].size - 1), signature);
1540 if (
bank->sectors[sector].size == 8*
KiB) {
1541 bank->sectors[sector].is_erased =
1542 (signature[3] == 0x01ABAAAA) &&
1543 (signature[2] == 0xAAAAAAAA) &&
1544 (signature[1] == 0xAAAAAAAA) &&
1545 (signature[0] == 0xAAA00AAA);
1547 if (
bank->sectors[sector].size == 64*
KiB) {
1548 bank->sectors[sector].is_erased =
1549 (signature[3] == 0x11801222) &&
1550 (signature[2] == 0xB88844FF) &&
1551 (signature[1] == 0x11A22008) &&
1552 (signature[0] == 0x2B1BFE44);
1575 .flash_bank_command = lpc2900_flash_bank_command,
void init_reg_param(struct reg_param *param, char *reg_name, uint32_t size, enum param_direction direction)
void destroy_reg_param(struct reg_param *param)
Holds the interface to ARM cores.
Support functions to access arbitrary bits in a byte array.
static void buf_set_u32(uint8_t *_buffer, unsigned first, unsigned num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
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 CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
#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_COMMAND_ARGUMENT_INVALID
#define ERROR_FLASH_BANK_INVALID
#define ERROR_FLASH_SECTOR_INVALID
#define ERROR_FLASH_BANK_NOT_PROBED
#define ERROR_FLASH_OPERATION_FAILED
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 default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
int fileio_write(struct fileio *fileio, size_t size, const void *buffer, size_t *size_written)
int fileio_close(struct fileio *fileio)
int fileio_open(struct fileio **fileio, const char *url, enum fileio_access access_type, enum fileio_type type)
void image_close(struct image *image)
int image_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
int image_open(struct image *image, const char *url, const char *type_string)
void alive_sleep(uint64_t ms)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
static int lpc2900_probe(struct flash_bank *bank)
Try and identify the device.
#define FLASH_PROGRAM_TIME
static int lpc2900_write_index_page(struct flash_bank *bank, int pagenum, uint8_t page[FLASH_PAGE_SIZE])
Write one page to the index sector.
static uint32_t lpc2900_run_bist128(struct flash_bank *bank, uint32_t addr_from, uint32_t addr_to, uint32_t signature[4])
Use BIST to calculate a 128-bit hash value over a range of flash.
static unsigned int lpc2900_address2sector(struct flash_bank *bank, uint32_t offset)
Return sector number for given address.
#define INTSRC_END_OF_ERASE
FLASH_BANK_COMMAND_HANDLER(lpc2900_flash_bank_command)
Evaluate flash bank command.
static const struct command_registration lpc2900_command_handlers[]
static uint32_t lpc2900_calc_tr(uint32_t clock_var, uint32_t time_var)
Calculate FPTR.TR register value for desired program/erase time.
static void lpc2900_setup(struct flash_bank *bank)
Set up the flash for erase/program operations.
const struct flash_driver lpc2900_flash
#define ISS_CUSTOMER_SIZE
static uint32_t lpc2900_is_ready(struct flash_bank *bank)
Check if device is ready.
static int lpc2900_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Erase sector(s).
#define ISS_CUSTOMER_SIZE2
COMMAND_HANDLER(lpc2900_handle_signature_command)
Command to determine the signature of the whole flash.
#define ISS_CUSTOMER_SIZE1
#define ISS_CUSTOMER_START1
#define FMSSTOP_MISR_START
#define INTSRC_END_OF_BURN
static int lpc2900_erase_check(struct flash_bank *bank)
Run a blank check for each sector.
#define ISS_CUSTOMER_NWORDS1
static int lpc2900_protect_check(struct flash_bank *bank)
Get protection (sector security) status.
#define ISS_CUSTOMER_START2
static uint32_t lpc2900_wait_status(struct flash_bank *bank, uint32_t mask, int timeout)
Wait for an event in mask to occur in INT_STATUS.
#define INTSRC_END_OF_MISR
#define ISS_CUSTOMER_NWORDS2
static uint32_t lpc2900_read_security_status(struct flash_bank *bank)
Read the status of sector security from the index sector.
static const struct command_registration lpc2900_exec_command_handlers[]
static int lpc2900_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Write data to flash.
unsigned int common_magic
enum arm_state core_state
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...
Describes the geometry and status of a single flash sector within a flash bank.
unsigned int num_sections
struct imagesection * sections
target_addr_t base_address
Private data for lpc2900 flash driver.
uint32_t max_ram_block
Maximum contiguous block of internal SRAM (bytes).
char * target_name
String holding device name.
uint32_t chipid
Holds the value read from CHIPID register.
uint32_t clk_sys_fmc
System clock frequency.
uint32_t risky
Flag to indicate that dangerous operations are possible.
bool is_probed
This flag is set when the device has been successfully probed.
void target_free_all_working_areas(struct target *target)
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
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_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
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.
void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
#define ERROR_TARGET_NOT_HALTED
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.