34 #define NUMICRO_SYS_BASE 0x50000000
35 #define NUMICRO_SYS_WRPROT 0x50000100
36 #define NUMICRO_SYS_IPRSTC1 0x50000008
38 #define NUMICRO_SYSCLK_BASE 0x50000200
39 #define NUMICRO_SYSCLK_PWRCON 0x50000200
40 #define NUMICRO_SYSCLK_CLKSEL0 0x50000210
41 #define NUMICRO_SYSCLK_CLKDIV 0x50000218
42 #define NUMICRO_SYSCLK_AHBCLK 0x50000204
44 #define NUMICRO_FLASH_BASE 0x5000C000
45 #define NUMICRO_FLASH_ISPCON 0x5000C000
46 #define NUMICRO_FLASH_ISPADR 0x5000C004
47 #define NUMICRO_FLASH_ISPDAT 0x5000C008
48 #define NUMICRO_FLASH_ISPCMD 0x5000C00C
49 #define NUMICRO_FLASH_ISPTRG 0x5000C010
50 #define NUMICRO_FLASH_CHEAT 0x5000C01C
52 #define NUMICRO_SCS_BASE 0xE000E000
53 #define NUMICRO_SCS_AIRCR 0xE000ED0C
54 #define NUMICRO_SCS_DHCSR 0xE000EDF0
55 #define NUMICRO_SCS_DEMCR 0xE000EDFC
57 #define NUMICRO_APROM_BASE 0x00000000
58 #define NUMICRO_DATA_BASE 0x0001F000
59 #define NUMICRO_LDROM_BASE 0x00100000
60 #define NUMICRO_CONFIG_BASE 0x00300000
62 #define NUMICRO_CONFIG0 0x5000C000
63 #define NUMICRO_CONFIG1 0x5000C004
66 #define PWRCON_OSC22M (1 << 2)
67 #define PWRCON_XTL12M (1 << 0)
69 #define IPRSTC1_CPU_RST (1 << 1)
70 #define IPRSTC1_CHIP_RST (1 << 0)
72 #define AHBCLK_ISP_EN (1 << 2)
73 #define AHBCLK_SRAM_EN (1 << 4)
74 #define AHBCLK_TICK_EN (1 << 5)
76 #define ISPCON_ISPEN (1 << 0)
77 #define ISPCON_BS_AP (0 << 1)
78 #define ISPCON_BS_LP (1 << 1)
79 #define ISPCON_BS_MASK (1 << 1)
80 #define ISPCON_APUEN (1 << 3)
81 #define ISPCON_CFGUEN (1 << 4)
82 #define ISPCON_LDUEN (1 << 5)
83 #define ISPCON_ISPFF (1 << 6)
85 #define CONFIG0_LOCK_MASK (1 << 1)
88 #define ISPCMD_READ 0x00
89 #define ISPCMD_WRITE 0x21
90 #define ISPCMD_ERASE 0x22
91 #define ISPCMD_CHIPERASE 0x26
92 #define ISPCMD_READ_CID 0x0B
93 #define ISPCMD_READ_DID 0x0C
94 #define ISPCMD_READ_UID 0x04
95 #define ISPCMD_VECMAP 0x2E
96 #define ISPTRG_ISPGO (1 << 0)
100 #define REG_KEY2 0x16
101 #define REG_KEY3 0x88
102 #define REG_LOCK 0x00
105 #define NUMICRO_PAGESIZE 512
107 #define NUMICRO_MAX_FLASH_BANKS 4
124 #define NUMICRO_BANKS_GENERAL(aprom_size, data_size, ldrom_size, config_size) \
126 {{NUMICRO_APROM_BASE, (aprom_size)}, \
127 {NUMICRO_DATA_BASE, (data_size)}, \
128 {NUMICRO_LDROM_BASE, (ldrom_size)}, \
129 {NUMICRO_CONFIG_BASE, (config_size)}}
548 LOG_DEBUG(
"NuMicro arm architecture: armv7m\n");
552 LOG_DEBUG(
"NuMicro arm architecture: armv6m\n");
562 uint32_t is_protected;
570 LOG_DEBUG(
"protected = 0x%08" PRIx32
"", is_protected);
571 if (is_protected == 0) {
588 if (is_protected == 1) {
669 LOG_DEBUG(
"timed out waiting for flash");
684 #include "../../../contrib/loaders/flash/numicro/numicro_m0.inc"
688 #include "../../../contrib/loaders/flash/numicro/numicro_m4.inc"
696 uint32_t buffer_size = 1024;
729 LOG_WARNING(
"no working area available, can't do block memory writes");
741 LOG_WARNING(
"no working area available, can't do block memory writes");
756 if (buffer_size <= 256) {
760 LOG_WARNING(
"No large enough working area available, can't do block memory writes");
775 uint32_t thisrun_count = (
count > (buffer_size / 4)) ? (buffer_size / 4) :
count;
783 buf_set_u32(reg_params[2].value, 0, 32, thisrun_count);
786 write_algorithm->
address, 0, 100000, &armv7m_info);
788 LOG_ERROR(
"Error executing NuMicro Flash programming algorithm");
793 buffer += thisrun_count * 4;
794 address += thisrun_count * 4;
795 count -= thisrun_count;
820 LOG_INFO(
"Nuvoton NuMicro: Flash Lock Check...");
833 if ((
config[0] & (1<<7)) == 0)
840 LOG_INFO(
"Flash is secure locked!");
841 LOG_INFO(
"TO UNLOCK FLASH,EXECUTE chip_erase COMMAND!!");
848 for (
unsigned int i = 0; i <
bank->num_sectors; i++)
849 bank->sectors[i].is_protected = set;
867 LOG_INFO(
"Nuvoton NuMicro: Sector Erase ... (%u to %u)", first, last);
878 for (
unsigned int i = first; i <= last; i++) {
882 bank->base +
bank->sectors[i].offset);
900 LOG_DEBUG(
"timed out waiting for flash");
938 LOG_INFO(
"Nuvoton NuMicro: Flash Write ...");
950 assert(
count % 4 == 0);
952 uint32_t words_remaining =
count / 4;
960 LOG_WARNING(
"couldn't use block writes, falling back to single "
964 for (uint32_t i = 0; i <
count; i += 4) {
966 uint8_t padding[4] = {0xff, 0xff, 0xff, 0xff};
996 LOG_DEBUG(
"timed out waiting for flash");
1036 LOG_WARNING(
"NuMicro flash driver: Failed to Get PartID\n");
1040 LOG_INFO(
"Device ID: 0x%08" PRIx32
"", part_id);
1045 LOG_INFO(
"Device Name: %s", (*cpu)->partname);
1055 for (
size_t i = 0; i < cpu->
n_banks; i++) {
1059 PRIx32,
bank->base, *flash_size);
1068 uint32_t flash_size,
offset = 0;
1076 LOG_WARNING(
"NuMicro flash driver: Failed to detect a known part\n");
1082 LOG_WARNING(
"NuMicro flash driver: Failed to detect flash size\n");
1088 bank->num_sectors = num_pages;
1090 bank->size = flash_size;
1092 for (
int i = 0; i < num_pages; i++) {
1095 bank->sectors[i].is_erased = -1;
1096 bank->sectors[i].is_protected = 0;
1101 numicro_info->
probed =
true;
1103 LOG_DEBUG(
"Nuvoton NuMicro: Probed ...");
1112 if (numicro_info->
probed)
1132 bank->driver_priv = bank_info;
1133 bank->write_start_alignment =
bank->write_end_alignment = 4;
1160 LOG_INFO(
"0x%08" PRIx32
": 0x%08" PRIx32, address, ispdat);
1168 uint32_t ispdat, rdat;
1188 LOG_INFO(
"0x%08" PRIx32
": 0x%08" PRIx32, address, ispdat);
1221 .handler = numicro_handle_read_isp_command,
1224 .help =
"read flash through ISP.",
1227 .name =
"write_isp",
1228 .handler = numicro_handle_write_isp_command,
1229 .usage =
"address value",
1231 .help =
"write flash through ISP.",
1234 .name =
"chip_erase",
1235 .handler = numicro_handle_chip_erase_command,
1237 .help =
"chip erase through ISP.",
1247 .help =
"numicro flash command group",
1257 .flash_bank_command = numicro_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)
static struct armv7m_common * target_to_armv7m(struct target *target)
#define ARMV7M_COMMON_MAGIC
Support functions to access arbitrary bits in a byte array.
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int 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 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 CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_FLASH_OPERATION_FAILED
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
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.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
static struct device_config config
void busy_sleep(uint64_t ms)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
#define NUMICRO_FLASH_ISPCON
FLASH_BANK_COMMAND_HANDLER(numicro_flash_bank_command)
static int numicro_init_isp(struct target *target)
static int numicro_probe(struct flash_bank *bank)
static uint32_t m_address_bias_offset
static uint32_t m_page_size
static int numicro_protect_check(struct flash_bank *bank)
static const uint8_t numicro_m4_flash_write_code[]
static int numicro_auto_probe(struct flash_bank *bank)
static int numicro_get_arm_arch(struct target *target)
#define NUMICRO_FLASH_CHEAT
static int numicro_reg_unlock(struct target *target)
#define NUMICRO_FLASH_ISPCMD
static const uint8_t numicro_flash_write_code[]
#define NUMICRO_FLASH_ISPADR
static int numicro_writeblock(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
const struct flash_driver numicro_flash
#define NUMICRO_FLASH_ISPDAT
#define NUMICRO_FLASH_ISPTRG
#define NUMICRO_SYS_WRPROT
static uint32_t numicro_fmc_cmd(struct target *target, uint32_t cmd, uint32_t addr, uint32_t wdata, uint32_t *rdata)
COMMAND_HANDLER(numicro_handle_read_isp_command)
static const struct numicro_cpu_type numicro_parts[]
static int numicro_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static int numicro_get_flash_size(struct flash_bank *bank, const struct numicro_cpu_type *cpu, uint32_t *flash_size)
static int numicro_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
#define CONFIG0_LOCK_MASK
#define NUMICRO_SYSCLK_AHBCLK
static int numicro_get_cpu_type(struct target *target, const struct numicro_cpu_type **cpu)
#define NUMICRO_MAX_FLASH_BANKS
#define NUMICRO_BANKS_GENERAL(aprom_size, data_size, ldrom_size, config_size)
static const struct command_registration numicro_exec_command_handlers[]
static const struct command_registration numicro_command_handlers[]
target_addr_t addr
Start address to search for the control block.
enum arm_arch arch
ARM architecture version.
unsigned int common_magic
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.
struct numicro_flash_bank_type bank[NUMICRO_MAX_FLASH_BANKS]
struct working_area * write_algorithm
const struct numicro_cpu_type * cpu
uint32_t working_area_size
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
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, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
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_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
struct target * get_current_target(struct command_context *cmd_ctx)
#define ERROR_TARGET_NOT_HALTED
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.