31 #define CHECK_STATUS(rc, msg) { \
32 if (rc != ERROR_OK) { \
33 LOG_ERROR("status(%d):%s\n", rc, msg); } }
38 #define PROGRAM_KEY (0x12344321)
39 #define OTP_PROGRAM_KEY (0x87655678)
41 #define FLASH_PROGRAM_MAIN_FROM_SRAM 0x0800005d
42 #define FLASH_PROGRAM_OTP_FROM_SRAM 0x08000061
43 #define FLASH_ERASE_LIST_MAIN_PAGES_FROM_SRAM 0x08000065
44 #define FLASH_MASS_ERASE_MAIN_PAGES_FROM_SRAM 0x08000069
102 {0xFF, 0x00,
"Unknown"},
103 {0x01, 0x00,
"Apollo"},
104 {0x02, 0x00,
"Apollo2"},
105 {0x03, 0x00,
"Unknown"},
106 {0x04, 0x00,
"Unknown"},
107 {0x05, 0x00,
"Apollo"},
111 "Unknown",
"Apollo",
"Apollo2",
"Unknown",
"Unknown",
"Apollo"
129 bank->driver_priv = ambiqmicro_info;
134 ambiqmicro_info->
probed =
false;
144 if (!ambiqmicro_info->
probed) {
156 "class %d (%s) %s\n",
173 uint32_t part_num = 0;
181 LOG_ERROR(
"status(0x%x):Could not read part_num.\n", retval);
185 LOG_DEBUG(
"Part number: 0x%" PRIx32, part_num);
190 ambiqmicro_info->
target_class = (part_num & 0xFF000000) >> 24;
195 bank->base =
bank->bank_number * 0x40000;
205 ambiqmicro_info->
flshsiz = 1024 * 256;
210 LOG_INFO(
"Unknown Class. Using Apollo-64 as default.");
212 bank->base =
bank->bank_number * 0x40000;
220 ambiqmicro_info->
flshsiz = 1024 * 256;
233 LOG_DEBUG(
"num_pages: %" PRIu32
", pagesize: %" PRIu32
", flash: %" PRIu32
", sram: %" PRIu32,
253 if (!ambiqmicro->
probed) {
258 for (i = 0; i < (unsigned)
bank->num_sectors; i++)
259 bank->sectors[i].is_protected = -1;
272 __FILE__, __LINE__, __func__, rc);
277 LOG_ERROR(
"Flash not happy: status(0x%" PRIx32
")", retflash);
285 uint32_t flash_return_address)
287 int retval, retflash;
296 CHECK_STATUS(retval,
"error executing ambiqmicro command");
336 ambiqmicro_info =
bank->driver_priv;
344 if (!ambiqmicro_info->
probed) {
363 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
369 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
375 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
386 CHECK_STATUS(retval,
"error executing ambiqmicro flash mass erase.");
412 if (!ambiqmicro_info->
probed) {
421 if ((last < first) || (last >= ambiqmicro_info->
num_pages))
428 if ((first == 0) && (last == (ambiqmicro_info->
num_pages - 1)))
445 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
451 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
457 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
463 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
469 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
476 LOG_INFO(
"Erasing pages %u to %u on bank %u", first, last,
bank->bank_number);
482 CHECK_STATUS(retval,
"error executing flash page erase");
486 LOG_INFO(
"%u pages erased!", 1+(last-first));
502 unsigned int first,
unsigned int last)
526 uint32_t buffer_pointer = 0x10000010;
528 uint32_t thisrun_count;
532 LOG_ERROR(
"write block must be multiple of 4 bytes in offset & length");
545 if (
count > maxbuffer)
546 thisrun_count = maxbuffer;
548 thisrun_count =
count;
558 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
564 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
570 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
576 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
584 CHECK_STATUS(retval,
"error writing target SRAM parameters.");
588 LOG_DEBUG(
"address = 0x%08" PRIx32, address);
591 CHECK_STATUS(retval,
"error executing ambiqmicro flash write algorithm");
595 address += thisrun_count;
596 count -= thisrun_count;
632 if (ambiqmicro_info->
probed) {
651 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
652 bank->sectors[i].offset = i * ambiqmicro_info->
pagesize;
654 bank->sectors[i].is_erased = -1;
655 bank->sectors[i].is_protected = -1;
661 ambiqmicro_info->
probed =
true;
673 ambiqmicro_info =
bank->driver_priv;
681 if (!ambiqmicro_info->
probed) {
705 CHECK_STATUS(retval,
"error setting target SRAM parameters.");
711 CHECK_STATUS(retval,
"error setting target SRAM parameters.");
717 CHECK_STATUS(retval,
"error setting target SRAM parameters.");
723 CHECK_STATUS(retval,
"error setting target SRAM parameters.");
736 CHECK_STATUS(retval,
"error executing ambiqmicro otp program algorithm");
738 LOG_INFO(
"Programming OTP finished.");
766 uint32_t first, last;
819 .
name =
"mass_erase",
821 .handler = ambiqmicro_handle_mass_erase_command,
823 .help =
"Erase entire device",
826 .name =
"page_erase",
827 .usage =
"<bank> <first> <last>",
828 .handler = ambiqmicro_handle_page_erase_command,
830 .help =
"Erase device pages",
833 .name =
"program_otp",
834 .handler = ambiqmicro_handle_program_otp_command,
836 .usage =
"<bank> <offset> <count>",
838 "Program OTP (assumes you have already written array starting at 0x10000010)",
844 .
name =
"ambiqmicro",
846 .help =
"ambiqmicro flash command group",
847 .usage =
"Support for Ambiq Micro parts.",
854 .
name =
"ambiqmicro",
856 .flash_bank_command = ambiqmicro_flash_bank_command,
const struct flash_driver ambiqmicro_flash
COMMAND_HANDLER(ambiqmicro_handle_mass_erase_command)
#define FLASH_PROGRAM_MAIN_FROM_SRAM
static int ambiqmicro_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
static int check_flash_status(struct target *target, uint32_t address)
Read flash status from bootloader.
static int ambiqmicro_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static const struct command_registration ambiqmicro_command_handlers[]
#define FLASH_MASS_ERASE_MAIN_PAGES_FROM_SRAM
static const uint32_t apollo_flash_size[]
#define FLASH_ERASE_LIST_MAIN_PAGES_FROM_SRAM
static int ambiqmicro_otp_program(struct flash_bank *bank, uint32_t offset, uint32_t count)
static int ambiqmicro_exec_command(struct target *target, uint32_t command, uint32_t flash_return_address)
static int ambiqmicro_mass_erase(struct flash_bank *bank)
static int ambiqmicro_write_block(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static const uint32_t apollo_sram_size[]
static int ambiqmicro_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static struct @4 ambiqmicro_parts[6]
#define CHECK_STATUS(rc, msg)
Check error, log error.
static char * ambiqmicro_classname[6]
#define FLASH_PROGRAM_OTP_FROM_SRAM
static const struct command_registration ambiqmicro_exec_command_handlers[]
static int ambiqmicro_read_part_info(struct flash_bank *bank)
FLASH_BANK_COMMAND_HANDLER(ambiqmicro_flash_bank_command)
static int ambiqmicro_protect_check(struct flash_bank *bank)
static int ambiqmicro_probe(struct flash_bank *bank)
static int get_ambiqmicro_info(struct flash_bank *bank, struct command_invocation *cmd)
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 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_FLASH_SECTOR_INVALID
#define ERROR_FLASH_BANK_NOT_PROBED
#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.
void alive_sleep(uint64_t ms)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
uint32_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.
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 target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
int target_poll(struct target *target)
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
int target_resume(struct target *target, int current, target_addr_t address, int handle_breakpoints, int debug_execution)
Make the target (re)start executing using its saved execution context (possibly with some modificatio...
#define ERROR_TARGET_NOT_HALTED
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.