28 #define NUMICRO_SYS_BASE 0x50000000
29 #define NUMICRO_SYS_WRPROT 0x50000100
30 #define NUMICRO_SYS_IPRSTC1 0x50000008
32 #define NUMICRO_SYSCLK_BASE 0x50000200
33 #define NUMICRO_SYSCLK_PWRCON 0x50000200
34 #define NUMICRO_SYSCLK_CLKSEL0 0x50000210
35 #define NUMICRO_SYSCLK_CLKDIV 0x50000218
36 #define NUMICRO_SYSCLK_AHBCLK 0x50000204
38 #define NUMICRO_FLASH_BASE 0x5000C000
39 #define NUMICRO_FLASH_ISPCON 0x5000C000
40 #define NUMICRO_FLASH_ISPADR 0x5000C004
41 #define NUMICRO_FLASH_ISPDAT 0x5000C008
42 #define NUMICRO_FLASH_ISPCMD 0x5000C00C
43 #define NUMICRO_FLASH_ISPTRG 0x5000C010
44 #define NUMICRO_FLASH_CHEAT 0x5000C01C
46 #define NUMICRO_SCS_BASE 0xE000E000
47 #define NUMICRO_SCS_AIRCR 0xE000ED0C
48 #define NUMICRO_SCS_DHCSR 0xE000EDF0
49 #define NUMICRO_SCS_DEMCR 0xE000EDFC
51 #define NUMICRO_APROM_BASE 0x00000000
52 #define NUMICRO_DATA_BASE 0x0001F000
53 #define NUMICRO_LDROM_BASE 0x00100000
54 #define NUMICRO_CONFIG_BASE 0x00300000
56 #define NUMICRO_CONFIG0 0x5000C000
57 #define NUMICRO_CONFIG1 0x5000C004
60 #define PWRCON_OSC22M (1 << 2)
61 #define PWRCON_XTL12M (1 << 0)
63 #define IPRSTC1_CPU_RST (1 << 1)
64 #define IPRSTC1_CHIP_RST (1 << 0)
66 #define AHBCLK_ISP_EN (1 << 2)
67 #define AHBCLK_SRAM_EN (1 << 4)
68 #define AHBCLK_TICK_EN (1 << 5)
70 #define ISPCON_ISPEN (1 << 0)
71 #define ISPCON_BS_AP (0 << 1)
72 #define ISPCON_BS_LP (1 << 1)
73 #define ISPCON_BS_MASK (1 << 1)
74 #define ISPCON_APUEN (1 << 3)
75 #define ISPCON_CFGUEN (1 << 4)
76 #define ISPCON_LDUEN (1 << 5)
77 #define ISPCON_ISPFF (1 << 6)
79 #define CONFIG0_LOCK_MASK (1 << 1)
82 #define ISPCMD_READ 0x00
83 #define ISPCMD_WRITE 0x21
84 #define ISPCMD_ERASE 0x22
85 #define ISPCMD_CHIPERASE 0x26
86 #define ISPCMD_READ_CID 0x0B
87 #define ISPCMD_READ_DID 0x0C
88 #define ISPCMD_READ_UID 0x04
89 #define ISPCMD_VECMAP 0x2E
90 #define ISPTRG_ISPGO (1 << 0)
99 #define NUMICRO_PAGESIZE 512
101 #define NUMICRO_MAX_FLASH_BANKS 4
118 #define NUMICRO_BANKS_NUC100(aprom_size) \
120 { {NUMICRO_APROM_BASE, (aprom_size)}, {NUMICRO_DATA_BASE, 4*1024}, {NUMICRO_LDROM_BASE, 4*1024}, \
121 {NUMICRO_CONFIG_BASE, 1024} }
123 #define NUMICRO_BANKS_M051(aprom_size) \
125 { {NUMICRO_APROM_BASE, (aprom_size)}, {NUMICRO_DATA_BASE, 4*1024}, {NUMICRO_LDROM_BASE, 4*1024}, \
126 {NUMICRO_CONFIG_BASE, 1024} }
128 #define NUMICRO_BANKS_MINI51(aprom_size) \
130 { {NUMICRO_APROM_BASE, (aprom_size)}, {NUMICRO_LDROM_BASE, 2*1024}, {NUMICRO_CONFIG_BASE, 512} }
132 #define NUMICRO_BANKS_NANO(aprom_size) \
134 { {NUMICRO_APROM_BASE, (aprom_size)}, {NUMICRO_DATA_BASE, 4*1024}, {NUMICRO_LDROM_BASE, 4*1024}, \
135 {NUMICRO_CONFIG_BASE, 1024} }
137 #define NUMICRO_BANKS_NUC400(aprom_size) \
139 { {NUMICRO_APROM_BASE, (aprom_size)}, {NUMICRO_DATA_BASE, 4*1024}, {NUMICRO_LDROM_BASE, 16*1024}, \
140 {NUMICRO_CONFIG_BASE, 1024} }
1131 uint32_t is_protected;
1139 LOG_DEBUG(
"protected = 0x%08" PRIx32
"", is_protected);
1140 if (is_protected == 0) {
1157 if (is_protected == 1) {
1239 LOG_DEBUG(
"timed out waiting for flash");
1307 0x04, 0xC0, 0x00, 0x50,
1308 0x08, 0xC0, 0x00, 0x50,
1309 0x10, 0xC0, 0x00, 0x50,
1310 0x00, 0xC0, 0x00, 0x50
1317 uint32_t buffer_size = 1024;
1349 LOG_WARNING(
"no working area available, can't do block memory writes");
1361 if (buffer_size <= 256) {
1365 LOG_WARNING(
"No large enough working area available, can't do block memory writes");
1380 uint32_t thisrun_count = (
count > (buffer_size / 4)) ? (buffer_size / 4) :
count;
1388 buf_set_u32(reg_params[2].value, 0, 32, thisrun_count);
1391 write_algorithm->
address, 0, 100000, &armv7m_info);
1393 LOG_ERROR(
"Error executing NuMicro Flash programming algorithm");
1398 buffer += thisrun_count * 4;
1399 address += thisrun_count * 4;
1400 count -= thisrun_count;
1425 LOG_INFO(
"Nuvoton NuMicro: Flash Lock Check...");
1437 if ((
config[0] & (1<<7)) == 0)
1438 LOG_INFO(
"CBS=0: Boot From LPROM");
1440 LOG_INFO(
"CBS=1: Boot From APROM");
1444 LOG_INFO(
"Flash is secure locked!");
1445 LOG_INFO(
"TO UNLOCK FLASH,EXECUTE chip_erase COMMAND!!");
1452 for (
unsigned int i = 0; i <
bank->num_sectors; i++)
1453 bank->sectors[i].is_protected = set;
1471 LOG_INFO(
"Nuvoton NuMicro: Sector Erase ... (%u to %u)", first, last);
1481 for (
unsigned int i = first; i <= last; i++) {
1483 bank->base +
bank->sectors[i].offset);
1501 LOG_DEBUG(
"timed out waiting for flash");
1539 LOG_INFO(
"Nuvoton NuMicro: Flash Write ...");
1550 assert(
count % 4 == 0);
1552 uint32_t words_remaining =
count / 4;
1560 LOG_WARNING(
"couldn't use block writes, falling back to single "
1564 for (uint32_t i = 0; i <
count; i += 4) {
1588 LOG_DEBUG(
"timed out waiting for flash");
1625 LOG_WARNING(
"NuMicro flash driver: Failed to Get PartID\n");
1629 LOG_INFO(
"Device ID: 0x%08" PRIx32
"", part_id);
1634 LOG_INFO(
"Device Name: %s", (*cpu)->partname);
1644 for (
size_t i = 0; i < cpu->
n_banks; i++) {
1648 PRIx32,
bank->base, *flash_size);
1657 uint32_t flash_size,
offset = 0;
1665 LOG_WARNING(
"NuMicro flash driver: Failed to detect a known part\n");
1671 LOG_WARNING(
"NuMicro flash driver: Failed to detect flash size\n");
1677 bank->num_sectors = num_pages;
1679 bank->size = flash_size;
1681 for (
int i = 0; i < num_pages; i++) {
1684 bank->sectors[i].is_erased = -1;
1685 bank->sectors[i].is_protected = 0;
1690 numicro_info->
probed =
true;
1692 LOG_DEBUG(
"Nuvoton NuMicro: Probed ...");
1701 if (numicro_info->
probed)
1721 bank->driver_priv = bank_info;
1722 bank->write_start_alignment =
bank->write_end_alignment = 4;
1749 LOG_INFO(
"0x%08" PRIx32
": 0x%08" PRIx32, address, ispdat);
1757 uint32_t ispdat, rdat;
1776 LOG_INFO(
"0x%08" PRIx32
": 0x%08" PRIx32, address, ispdat);
1808 .handler = numicro_handle_read_isp_command,
1811 .help =
"read flash through ISP.",
1814 .name =
"write_isp",
1815 .handler = numicro_handle_write_isp_command,
1816 .usage =
"address value",
1818 .help =
"write flash through ISP.",
1821 .name =
"chip_erase",
1822 .handler = numicro_handle_chip_erase_command,
1824 .help =
"chip erase through ISP.",
1834 .help =
"numicro flash command group",
1844 .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)
#define ARMV7M_COMMON_MAGIC
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 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)
#define NUMICRO_BANKS_M051(aprom_size)
static int numicro_init_isp(struct target *target)
static int numicro_probe(struct flash_bank *bank)
static int numicro_protect_check(struct flash_bank *bank)
static int numicro_auto_probe(struct flash_bank *bank)
#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_BANKS_MINI51(aprom_size)
#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 NUMICRO_BANKS_NUC100(aprom_size)
#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
static const struct command_registration numicro_exec_command_handlers[]
#define NUMICRO_BANKS_NANO(aprom_size)
static const struct command_registration numicro_command_handlers[]
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_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_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_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.