-/***************************************************************************\r
- * Copyright (C) 2009 by Simon Qian *\r
- * SimonQian@SimonQian.com *\r
- * *\r
- * This program is free software; you can redistribute it and/or modify *\r
- * it under the terms of the GNU General Public License as published by *\r
- * the Free Software Foundation; either version 2 of the License, or *\r
- * (at your option) any later version. *\r
- * *\r
- * This program is distributed in the hope that it will be useful, *\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *\r
- * GNU General Public License for more details. *\r
- * *\r
- * You should have received a copy of the GNU General Public License *\r
- * along with this program; if not, write to the *\r
- * Free Software Foundation, Inc., *\r
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *\r
- ***************************************************************************/\r
-#ifdef HAVE_CONFIG_H\r
-#include "config.h"\r
-#endif\r
-\r
-#include "replacements.h"\r
-\r
-#include "avrf.h"\r
-#include "avrt.h"\r
-#include "flash.h"\r
-#include "target.h"\r
-#include "log.h"\r
-#include "algorithm.h"\r
-#include "binarybuffer.h"\r
-\r
-#include <stdlib.h>\r
-#include <string.h>\r
-\r
-/* AVR_JTAG_Instructions */\r
-#define AVR_JTAG_INS_LEN 4\r
-// Public Instructions:\r
-#define AVR_JTAG_INS_EXTEST 0x00\r
-#define AVR_JTAG_INS_IDCODE 0x01\r
-#define AVR_JTAG_INS_SAMPLE_PRELOAD 0x02\r
-#define AVR_JTAG_INS_BYPASS 0x0F\r
-// AVR Specified Public Instructions:\r
-#define AVR_JTAG_INS_AVR_RESET 0x0C\r
-#define AVR_JTAG_INS_PROG_ENABLE 0x04\r
-#define AVR_JTAG_INS_PROG_COMMANDS 0x05\r
-#define AVR_JTAG_INS_PROG_PAGELOAD 0x06\r
-#define AVR_JTAG_INS_PROG_PAGEREAD 0x07\r
-\r
-// Data Registers:\r
-#define AVR_JTAG_REG_Bypass_Len 1\r
-#define AVR_JTAG_REG_DeviceID_Len 32\r
-\r
-#define AVR_JTAG_REG_Reset_Len 1\r
-#define AVR_JTAG_REG_JTAGID_Len 32\r
-#define AVR_JTAG_REG_ProgrammingEnable_Len 16\r
-#define AVR_JTAG_REG_ProgrammingCommand_Len 15\r
-#define AVR_JTAG_REG_FlashDataByte_Len 16\r
-\r
-avrf_type_t avft_chips_info[] = \r
-{\r
-// name, chip_id, flash_page_size, flash_page_num, eeprom_page_size, eeprom_page_num\r
- {"atmega128", 0x9702, 256, 512, 8, 512},\r
-};\r
-\r
-static int avrf_register_commands(struct command_context_s *cmd_ctx);\r
-static int avrf_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);\r
-static int avrf_erase(struct flash_bank_s *bank, int first, int last);\r
-static int avrf_protect(struct flash_bank_s *bank, int set, int first, int last);\r
-static int avrf_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);\r
-static int avrf_probe(struct flash_bank_s *bank);\r
-static int avrf_auto_probe(struct flash_bank_s *bank);\r
-//static int avrf_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);\r
-static int avrf_protect_check(struct flash_bank_s *bank);\r
-static int avrf_info(struct flash_bank_s *bank, char *buf, int buf_size);\r
-\r
-static int avrf_handle_mass_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);\r
-\r
-extern int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out);\r
-extern int avr_jtag_senddat(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int len);\r
-\r
-extern int mcu_write_ir(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int ir_len, int rti);\r
-extern int mcu_write_dr(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int dr_len, int rti);\r
-extern int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti);\r
-extern int mcu_write_dr_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int dr_len, int rti);\r
-extern int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti);\r
-extern int mcu_write_dr_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int dr_len, int rti);\r
-extern int mcu_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti);\r
-extern int mcu_write_dr_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int dr_len, int rti);\r
-extern int mcu_execute_queue(void);\r
-\r
-flash_driver_t avr_flash =\r
-{\r
- .name = "avr",\r
- .register_commands = avrf_register_commands,\r
- .flash_bank_command = avrf_flash_bank_command,\r
- .erase = avrf_erase,\r
- .protect = avrf_protect,\r
- .write = avrf_write,\r
- .probe = avrf_probe,\r
- .auto_probe = avrf_auto_probe,\r
- .erase_check = default_flash_mem_blank_check,\r
- .protect_check = avrf_protect_check,\r
- .info = avrf_info\r
-};\r
-\r
-/* avr program functions */\r
-static int avr_jtag_reset(avr_common_t *avr, u32 reset)\r
-{\r
- avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_AVR_RESET);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, reset ,AVR_JTAG_REG_Reset_Len);\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-static int avr_jtag_read_jtagid(avr_common_t *avr, u32 *id)\r
-{\r
- avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_IDCODE);\r
- avr_jtag_senddat(avr->jtag_info.tap, id, 0, AVR_JTAG_REG_JTAGID_Len);\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-static int avr_jtagprg_enterprogmode(avr_common_t *avr)\r
-{\r
- avr_jtag_reset(avr, 1);\r
- \r
- avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_ENABLE);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0xA370, AVR_JTAG_REG_ProgrammingEnable_Len);\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-static int avr_jtagprg_leaveprogmode(avr_common_t *avr)\r
-{\r
- avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_COMMANDS);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x2300, AVR_JTAG_REG_ProgrammingCommand_Len);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3300, AVR_JTAG_REG_ProgrammingCommand_Len);\r
-\r
- avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_ENABLE);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0, AVR_JTAG_REG_ProgrammingEnable_Len);\r
-\r
- avr_jtag_reset(avr, 0);\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-static int avr_jtagprg_chiperase(avr_common_t *avr)\r
-{\r
- u32 poll_value;\r
- \r
- avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_COMMANDS);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x2380, AVR_JTAG_REG_ProgrammingCommand_Len);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3180, AVR_JTAG_REG_ProgrammingCommand_Len);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len);\r
- \r
- do{\r
- poll_value = 0;\r
- avr_jtag_senddat(avr->jtag_info.tap, &poll_value, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len);\r
- if (ERROR_OK != mcu_execute_queue())\r
- {\r
- return ERROR_FAIL;\r
- }\r
- LOG_DEBUG("poll_value = 0x%04X", poll_value);\r
- }while(!(poll_value & 0x0200));\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-static int avr_jtagprg_writeflashpage(avr_common_t *avr, u8 *page_buf, u32 buf_size, u32 addr, u32 page_size)\r
-{\r
- u32 i, poll_value;\r
- \r
- avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_COMMANDS);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x2310, AVR_JTAG_REG_ProgrammingCommand_Len);\r
- \r
- // load addr high byte\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x0700 | ((addr >> 9) & 0xFF), AVR_JTAG_REG_ProgrammingCommand_Len);\r
- \r
- // load addr low byte\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x0300 | ((addr >> 1) & 0xFF), AVR_JTAG_REG_ProgrammingCommand_Len);\r
- \r
- avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_PAGELOAD);\r
- \r
- for (i = 0; i < page_size; i++)\r
- {\r
- if (i < buf_size)\r
- {\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, page_buf[i], 8);\r
- }\r
- else\r
- {\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0xFF, 8);\r
- }\r
- }\r
- \r
- avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_COMMANDS);\r
- \r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3500, AVR_JTAG_REG_ProgrammingCommand_Len);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len);\r
- avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len);\r
- \r
- do{\r
- poll_value = 0;\r
- avr_jtag_senddat(avr->jtag_info.tap, &poll_value, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len);\r
- if (ERROR_OK != mcu_execute_queue())\r
- {\r
- return ERROR_FAIL;\r
- }\r
- LOG_DEBUG("poll_value = 0x%04X", poll_value);\r
- }while(!(poll_value & 0x0200));\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-/* interface command */\r
-static int avrf_register_commands(struct command_context_s *cmd_ctx)\r
-{\r
- command_t *avr_cmd = register_command(cmd_ctx, NULL, "avr", NULL, COMMAND_ANY, "avr flash specific commands");\r
- \r
- register_command(cmd_ctx, avr_cmd, "mass_erase", avrf_handle_mass_erase_command, COMMAND_EXEC,\r
- "mass erase device");\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-static int avrf_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)\r
-{\r
- avrf_flash_bank_t *avrf_info;\r
- \r
- if (argc < 6)\r
- {\r
- LOG_WARNING("incomplete flash_bank avr configuration");\r
- return ERROR_FLASH_BANK_INVALID;\r
- }\r
- \r
- avrf_info = malloc(sizeof(avrf_flash_bank_t));\r
- bank->driver_priv = avrf_info;\r
- \r
- avrf_info->probed = 0;\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-static int avrf_erase(struct flash_bank_s *bank, int first, int last)\r
-{\r
- LOG_INFO(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-static int avrf_protect(struct flash_bank_s *bank, int set, int first, int last)\r
-{\r
- LOG_INFO(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-static int avrf_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)\r
-{\r
- target_t *target = bank->target;\r
- avr_common_t *avr = target->arch_info;\r
- u32 cur_size, cur_buffer_size, page_size;\r
- \r
- if (bank->target->state != TARGET_HALTED)\r
- {\r
- LOG_ERROR("Target not halted");\r
- return ERROR_TARGET_NOT_HALTED;\r
- }\r
- \r
- page_size = bank->sectors[0].size;\r
- if ((offset % page_size) != 0)\r
- {\r
- LOG_WARNING("offset 0x%x breaks required %d-byte alignment", offset, page_size);\r
- return ERROR_FLASH_DST_BREAKS_ALIGNMENT;\r
- }\r
- \r
- LOG_DEBUG("offset is 0x%08X", offset);\r
- LOG_DEBUG("count is %d", count);\r
- \r
- if (ERROR_OK != avr_jtagprg_enterprogmode(avr))\r
- {\r
- return ERROR_FAIL;\r
- }\r
- \r
- cur_size = 0;\r
- while(count > 0)\r
- {\r
- if (count > page_size)\r
- {\r
- cur_buffer_size = page_size;\r
- }\r
- else\r
- {\r
- cur_buffer_size = count;\r
- }\r
- avr_jtagprg_writeflashpage(avr, buffer + cur_size, cur_buffer_size, offset + cur_size, page_size);\r
- count -= cur_buffer_size;\r
- cur_size += cur_buffer_size;\r
- \r
- keep_alive();\r
- }\r
- \r
- return avr_jtagprg_leaveprogmode(avr);\r
-}\r
-\r
-#define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)\r
-#define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)\r
-#define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)\r
-static int avrf_probe(struct flash_bank_s *bank)\r
-{\r
- target_t *target = bank->target;\r
- avrf_flash_bank_t *avrf_info = bank->driver_priv;\r
- avr_common_t *avr = target->arch_info;\r
- avrf_type_t *avr_info;\r
- int i;\r
- u32 device_id;\r
- \r
- if (bank->target->state != TARGET_HALTED)\r
- {\r
- LOG_ERROR("Target not halted");\r
- return ERROR_TARGET_NOT_HALTED;\r
- }\r
-\r
- avrf_info->probed = 0;\r
- \r
- avr_jtag_read_jtagid(avr, &device_id);\r
- if (ERROR_OK != mcu_execute_queue())\r
- {\r
- return ERROR_FAIL;\r
- }\r
- \r
- LOG_INFO( "device id = 0x%08x", device_id );\r
- if (EXTRACT_MFG(device_id) != 0x1F)\r
- {\r
- LOG_ERROR("0x%X is invalid Manufacturer for avr, 0x%X is expected", EXTRACT_MFG(device_id), 0x1F);\r
- }\r
- \r
- for (i = 0; i < (int)(sizeof(avft_chips_info) / sizeof(avft_chips_info[0])); i++)\r
- {\r
- if (avft_chips_info[i].chip_id == EXTRACT_PART(device_id))\r
- {\r
- avr_info = &avft_chips_info[i];\r
- LOG_INFO("target device is %s", avr_info->name);\r
- break;\r
- }\r
- }\r
- \r
- if (i < (int)(sizeof(avft_chips_info) / sizeof(avft_chips_info[0])))\r
- {\r
- // chip found\r
- bank->base = 0x00000000;\r
- bank->size = (avr_info->flash_page_size * avr_info->flash_page_num);\r
- bank->num_sectors = avr_info->flash_page_num;\r
- bank->sectors = malloc(sizeof(flash_sector_t) * avr_info->flash_page_num);\r
- \r
- for (i = 0; i < avr_info->flash_page_num; i++)\r
- {\r
- bank->sectors[i].offset = i * avr_info->flash_page_size;\r
- bank->sectors[i].size = avr_info->flash_page_size;\r
- bank->sectors[i].is_erased = -1;\r
- bank->sectors[i].is_protected = 1;\r
- }\r
- \r
- avrf_info->probed = 1;\r
- return ERROR_OK;\r
- }\r
- else\r
- {\r
- // chip not supported\r
- LOG_ERROR("0x%X is not support for avr", EXTRACT_PART(device_id));\r
- \r
- avrf_info->probed = 1;\r
- return ERROR_FAIL;\r
- }\r
-}\r
-\r
-static int avrf_auto_probe(struct flash_bank_s *bank)\r
-{\r
- avrf_flash_bank_t *avrf_info = bank->driver_priv;\r
- if (avrf_info->probed)\r
- return ERROR_OK;\r
- return avrf_probe(bank);\r
-}\r
-\r
-static int avrf_protect_check(struct flash_bank_s *bank)\r
-{\r
- LOG_INFO(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-static int avrf_info(struct flash_bank_s *bank, char *buf, int buf_size)\r
-{\r
- target_t *target = bank->target;\r
- avr_common_t *avr = target->arch_info;\r
- avrf_type_t *avr_info;\r
- int i;\r
- u32 device_id;\r
- \r
- if (bank->target->state != TARGET_HALTED)\r
- {\r
- LOG_ERROR("Target not halted");\r
- return ERROR_TARGET_NOT_HALTED;\r
- }\r
- \r
- avr_jtag_read_jtagid(avr, &device_id);\r
- if (ERROR_OK != mcu_execute_queue())\r
- {\r
- return ERROR_FAIL;\r
- }\r
- \r
- LOG_INFO( "device id = 0x%08x", device_id );\r
- if (EXTRACT_MFG(device_id) != 0x1F)\r
- {\r
- LOG_ERROR("0x%X is invalid Manufacturer for avr, 0x%X is expected", EXTRACT_MFG(device_id), 0x1F);\r
- }\r
- \r
- for (i = 0; i < (int)(sizeof(avft_chips_info) / sizeof(avft_chips_info[0])); i++)\r
- {\r
- if (avft_chips_info[i].chip_id == EXTRACT_PART(device_id))\r
- {\r
- avr_info = &avft_chips_info[i];\r
- LOG_INFO("target device is %s", avr_info->name);\r
- \r
- return ERROR_OK;\r
- }\r
- }\r
- \r
- if (i < (int)(sizeof(avft_chips_info) / sizeof(avft_chips_info[0])))\r
- {\r
- // chip found\r
- snprintf(buf, buf_size, "%s - Rev: 0x%X", avr_info->name, EXTRACT_VER(device_id));\r
- return ERROR_OK;\r
- }\r
- else\r
- {\r
- // chip not supported\r
- snprintf(buf, buf_size, "Cannot identify target as a avr\n");\r
- return ERROR_FLASH_OPERATION_FAILED;\r
- }\r
-}\r
-\r
-static int avrf_mass_erase(struct flash_bank_s *bank)\r
-{\r
- target_t *target = bank->target;\r
- avr_common_t *avr = target->arch_info;\r
- \r
- if (target->state != TARGET_HALTED)\r
- {\r
- LOG_ERROR("Target not halted");\r
- return ERROR_TARGET_NOT_HALTED;\r
- }\r
- \r
- if ((ERROR_OK != avr_jtagprg_enterprogmode(avr))\r
- || (ERROR_OK != avr_jtagprg_chiperase(avr))\r
- || (ERROR_OK != avr_jtagprg_leaveprogmode(avr)))\r
- {\r
- return ERROR_FAIL;\r
- }\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-static int avrf_handle_mass_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)\r
-{\r
- flash_bank_t *bank;\r
- int i;\r
- \r
- if (argc < 1)\r
- {\r
- command_print(cmd_ctx, "avr mass_erase <bank>");\r
- return ERROR_OK; \r
- }\r
- \r
- bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));\r
- if (!bank)\r
- {\r
- command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);\r
- return ERROR_OK;\r
- }\r
- \r
- if (avrf_mass_erase(bank) == ERROR_OK)\r
- {\r
- /* set all sectors as erased */\r
- for (i = 0; i < bank->num_sectors; i++)\r
- {\r
- bank->sectors[i].is_erased = 1;\r
- }\r
- \r
- command_print(cmd_ctx, "avr mass erase complete");\r
- }\r
- else\r
- {\r
- command_print(cmd_ctx, "avr mass erase failed");\r
- }\r
- \r
- LOG_DEBUG(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
+/***************************************************************************
+ * Copyright (C) 2009 by Simon Qian *
+ * SimonQian@SimonQian.com *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "replacements.h"
+
+#include "avrf.h"
+#include "avrt.h"
+#include "flash.h"
+#include "target.h"
+#include "log.h"
+#include "algorithm.h"
+#include "binarybuffer.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+/* AVR_JTAG_Instructions */
+#define AVR_JTAG_INS_LEN 4
+// Public Instructions:
+#define AVR_JTAG_INS_EXTEST 0x00
+#define AVR_JTAG_INS_IDCODE 0x01
+#define AVR_JTAG_INS_SAMPLE_PRELOAD 0x02
+#define AVR_JTAG_INS_BYPASS 0x0F
+// AVR Specified Public Instructions:
+#define AVR_JTAG_INS_AVR_RESET 0x0C
+#define AVR_JTAG_INS_PROG_ENABLE 0x04
+#define AVR_JTAG_INS_PROG_COMMANDS 0x05
+#define AVR_JTAG_INS_PROG_PAGELOAD 0x06
+#define AVR_JTAG_INS_PROG_PAGEREAD 0x07
+
+// Data Registers:
+#define AVR_JTAG_REG_Bypass_Len 1
+#define AVR_JTAG_REG_DeviceID_Len 32
+
+#define AVR_JTAG_REG_Reset_Len 1
+#define AVR_JTAG_REG_JTAGID_Len 32
+#define AVR_JTAG_REG_ProgrammingEnable_Len 16
+#define AVR_JTAG_REG_ProgrammingCommand_Len 15
+#define AVR_JTAG_REG_FlashDataByte_Len 16
+
+avrf_type_t avft_chips_info[] =
+{
+// name, chip_id, flash_page_size, flash_page_num, eeprom_page_size, eeprom_page_num
+ {"atmega128", 0x9702, 256, 512, 8, 512},
+};
+
+static int avrf_register_commands(struct command_context_s *cmd_ctx);
+static int avrf_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
+static int avrf_erase(struct flash_bank_s *bank, int first, int last);
+static int avrf_protect(struct flash_bank_s *bank, int set, int first, int last);
+static int avrf_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
+static int avrf_probe(struct flash_bank_s *bank);
+static int avrf_auto_probe(struct flash_bank_s *bank);
+//static int avrf_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
+static int avrf_protect_check(struct flash_bank_s *bank);
+static int avrf_info(struct flash_bank_s *bank, char *buf, int buf_size);
+
+static int avrf_handle_mass_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
+
+extern int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out);
+extern int avr_jtag_senddat(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int len);
+
+extern int mcu_write_ir(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int ir_len, int rti);
+extern int mcu_write_dr(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int dr_len, int rti);
+extern int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti);
+extern int mcu_write_dr_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int dr_len, int rti);
+extern int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti);
+extern int mcu_write_dr_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int dr_len, int rti);
+extern int mcu_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti);
+extern int mcu_write_dr_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int dr_len, int rti);
+extern int mcu_execute_queue(void);
+
+flash_driver_t avr_flash =
+{
+ .name = "avr",
+ .register_commands = avrf_register_commands,
+ .flash_bank_command = avrf_flash_bank_command,
+ .erase = avrf_erase,
+ .protect = avrf_protect,
+ .write = avrf_write,
+ .probe = avrf_probe,
+ .auto_probe = avrf_auto_probe,
+ .erase_check = default_flash_mem_blank_check,
+ .protect_check = avrf_protect_check,
+ .info = avrf_info
+};
+
+/* avr program functions */
+static int avr_jtag_reset(avr_common_t *avr, u32 reset)
+{
+ avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_AVR_RESET);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, reset ,AVR_JTAG_REG_Reset_Len);
+
+ return ERROR_OK;
+}
+
+static int avr_jtag_read_jtagid(avr_common_t *avr, u32 *id)
+{
+ avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_IDCODE);
+ avr_jtag_senddat(avr->jtag_info.tap, id, 0, AVR_JTAG_REG_JTAGID_Len);
+
+ return ERROR_OK;
+}
+
+static int avr_jtagprg_enterprogmode(avr_common_t *avr)
+{
+ avr_jtag_reset(avr, 1);
+
+ avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_ENABLE);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0xA370, AVR_JTAG_REG_ProgrammingEnable_Len);
+
+ return ERROR_OK;
+}
+
+static int avr_jtagprg_leaveprogmode(avr_common_t *avr)
+{
+ avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_COMMANDS);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x2300, AVR_JTAG_REG_ProgrammingCommand_Len);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3300, AVR_JTAG_REG_ProgrammingCommand_Len);
+
+ avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_ENABLE);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0, AVR_JTAG_REG_ProgrammingEnable_Len);
+
+ avr_jtag_reset(avr, 0);
+
+ return ERROR_OK;
+}
+
+static int avr_jtagprg_chiperase(avr_common_t *avr)
+{
+ u32 poll_value;
+
+ avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_COMMANDS);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x2380, AVR_JTAG_REG_ProgrammingCommand_Len);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3180, AVR_JTAG_REG_ProgrammingCommand_Len);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len);
+
+ do{
+ poll_value = 0;
+ avr_jtag_senddat(avr->jtag_info.tap, &poll_value, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len);
+ if (ERROR_OK != mcu_execute_queue())
+ {
+ return ERROR_FAIL;
+ }
+ LOG_DEBUG("poll_value = 0x%04X", poll_value);
+ }while(!(poll_value & 0x0200));
+
+ return ERROR_OK;
+}
+
+static int avr_jtagprg_writeflashpage(avr_common_t *avr, u8 *page_buf, u32 buf_size, u32 addr, u32 page_size)
+{
+ u32 i, poll_value;
+
+ avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_COMMANDS);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x2310, AVR_JTAG_REG_ProgrammingCommand_Len);
+
+ // load addr high byte
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x0700 | ((addr >> 9) & 0xFF), AVR_JTAG_REG_ProgrammingCommand_Len);
+
+ // load addr low byte
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x0300 | ((addr >> 1) & 0xFF), AVR_JTAG_REG_ProgrammingCommand_Len);
+
+ avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_PAGELOAD);
+
+ for (i = 0; i < page_size; i++)
+ {
+ if (i < buf_size)
+ {
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, page_buf[i], 8);
+ }
+ else
+ {
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0xFF, 8);
+ }
+ }
+
+ avr_jtag_sendinstr(avr->jtag_info.tap, NULL, AVR_JTAG_INS_PROG_COMMANDS);
+
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3500, AVR_JTAG_REG_ProgrammingCommand_Len);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len);
+ avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len);
+
+ do{
+ poll_value = 0;
+ avr_jtag_senddat(avr->jtag_info.tap, &poll_value, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len);
+ if (ERROR_OK != mcu_execute_queue())
+ {
+ return ERROR_FAIL;
+ }
+ LOG_DEBUG("poll_value = 0x%04X", poll_value);
+ }while(!(poll_value & 0x0200));
+
+ return ERROR_OK;
+}
+
+/* interface command */
+static int avrf_register_commands(struct command_context_s *cmd_ctx)
+{
+ command_t *avr_cmd = register_command(cmd_ctx, NULL, "avr", NULL, COMMAND_ANY, "avr flash specific commands");
+
+ register_command(cmd_ctx, avr_cmd, "mass_erase", avrf_handle_mass_erase_command, COMMAND_EXEC,
+ "mass erase device");
+
+ return ERROR_OK;
+}
+
+static int avrf_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
+{
+ avrf_flash_bank_t *avrf_info;
+
+ if (argc < 6)
+ {
+ LOG_WARNING("incomplete flash_bank avr configuration");
+ return ERROR_FLASH_BANK_INVALID;
+ }
+
+ avrf_info = malloc(sizeof(avrf_flash_bank_t));
+ bank->driver_priv = avrf_info;
+
+ avrf_info->probed = 0;
+
+ return ERROR_OK;
+}
+
+static int avrf_erase(struct flash_bank_s *bank, int first, int last)
+{
+ LOG_INFO(__FUNCTION__);
+ return ERROR_OK;
+}
+
+static int avrf_protect(struct flash_bank_s *bank, int set, int first, int last)
+{
+ LOG_INFO(__FUNCTION__);
+ return ERROR_OK;
+}
+
+static int avrf_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
+{
+ target_t *target = bank->target;
+ avr_common_t *avr = target->arch_info;
+ u32 cur_size, cur_buffer_size, page_size;
+
+ if (bank->target->state != TARGET_HALTED)
+ {
+ LOG_ERROR("Target not halted");
+ return ERROR_TARGET_NOT_HALTED;
+ }
+
+ page_size = bank->sectors[0].size;
+ if ((offset % page_size) != 0)
+ {
+ LOG_WARNING("offset 0x%x breaks required %d-byte alignment", offset, page_size);
+ return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
+ }
+
+ LOG_DEBUG("offset is 0x%08X", offset);
+ LOG_DEBUG("count is %d", count);
+
+ if (ERROR_OK != avr_jtagprg_enterprogmode(avr))
+ {
+ return ERROR_FAIL;
+ }
+
+ cur_size = 0;
+ while(count > 0)
+ {
+ if (count > page_size)
+ {
+ cur_buffer_size = page_size;
+ }
+ else
+ {
+ cur_buffer_size = count;
+ }
+ avr_jtagprg_writeflashpage(avr, buffer + cur_size, cur_buffer_size, offset + cur_size, page_size);
+ count -= cur_buffer_size;
+ cur_size += cur_buffer_size;
+
+ keep_alive();
+ }
+
+ return avr_jtagprg_leaveprogmode(avr);
+}
+
+#define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
+#define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
+#define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
+static int avrf_probe(struct flash_bank_s *bank)
+{
+ target_t *target = bank->target;
+ avrf_flash_bank_t *avrf_info = bank->driver_priv;
+ avr_common_t *avr = target->arch_info;
+ avrf_type_t *avr_info = NULL;
+ int i;
+ u32 device_id;
+
+ if (bank->target->state != TARGET_HALTED)
+ {
+ LOG_ERROR("Target not halted");
+ return ERROR_TARGET_NOT_HALTED;
+ }
+
+ avrf_info->probed = 0;
+
+ avr_jtag_read_jtagid(avr, &device_id);
+ if (ERROR_OK != mcu_execute_queue())
+ {
+ return ERROR_FAIL;
+ }
+
+ LOG_INFO( "device id = 0x%08x", device_id );
+ if (EXTRACT_MFG(device_id) != 0x1F)
+ {
+ LOG_ERROR("0x%X is invalid Manufacturer for avr, 0x%X is expected", EXTRACT_MFG(device_id), 0x1F);
+ }
+
+ for (i = 0; i < (int)(sizeof(avft_chips_info) / sizeof(avft_chips_info[0])); i++)
+ {
+ if (avft_chips_info[i].chip_id == EXTRACT_PART(device_id))
+ {
+ avr_info = &avft_chips_info[i];
+ LOG_INFO("target device is %s", avr_info->name);
+ break;
+ }
+ }
+
+ if (avr_info != NULL)
+ {
+ // chip found
+ bank->base = 0x00000000;
+ bank->size = (avr_info->flash_page_size * avr_info->flash_page_num);
+ bank->num_sectors = avr_info->flash_page_num;
+ bank->sectors = malloc(sizeof(flash_sector_t) * avr_info->flash_page_num);
+
+ for (i = 0; i < avr_info->flash_page_num; i++)
+ {
+ bank->sectors[i].offset = i * avr_info->flash_page_size;
+ bank->sectors[i].size = avr_info->flash_page_size;
+ bank->sectors[i].is_erased = -1;
+ bank->sectors[i].is_protected = 1;
+ }
+
+ avrf_info->probed = 1;
+ return ERROR_OK;
+ }
+ else
+ {
+ // chip not supported
+ LOG_ERROR("0x%X is not support for avr", EXTRACT_PART(device_id));
+
+ avrf_info->probed = 1;
+ return ERROR_FAIL;
+ }
+}
+
+static int avrf_auto_probe(struct flash_bank_s *bank)
+{
+ avrf_flash_bank_t *avrf_info = bank->driver_priv;
+ if (avrf_info->probed)
+ return ERROR_OK;
+ return avrf_probe(bank);
+}
+
+static int avrf_protect_check(struct flash_bank_s *bank)
+{
+ LOG_INFO(__FUNCTION__);
+ return ERROR_OK;
+}
+
+static int avrf_info(struct flash_bank_s *bank, char *buf, int buf_size)
+{
+ target_t *target = bank->target;
+ avr_common_t *avr = target->arch_info;
+ avrf_type_t *avr_info = NULL;
+ int i;
+ u32 device_id;
+
+ if (bank->target->state != TARGET_HALTED)
+ {
+ LOG_ERROR("Target not halted");
+ return ERROR_TARGET_NOT_HALTED;
+ }
+
+ avr_jtag_read_jtagid(avr, &device_id);
+ if (ERROR_OK != mcu_execute_queue())
+ {
+ return ERROR_FAIL;
+ }
+
+ LOG_INFO( "device id = 0x%08x", device_id );
+ if (EXTRACT_MFG(device_id) != 0x1F)
+ {
+ LOG_ERROR("0x%X is invalid Manufacturer for avr, 0x%X is expected", EXTRACT_MFG(device_id), 0x1F);
+ }
+
+ for (i = 0; i < (int)(sizeof(avft_chips_info) / sizeof(avft_chips_info[0])); i++)
+ {
+ if (avft_chips_info[i].chip_id == EXTRACT_PART(device_id))
+ {
+ avr_info = &avft_chips_info[i];
+ LOG_INFO("target device is %s", avr_info->name);
+
+ break;
+ }
+ }
+
+ if (avr_info != NULL)
+ {
+ // chip found
+ snprintf(buf, buf_size, "%s - Rev: 0x%X", avr_info->name, EXTRACT_VER(device_id));
+ return ERROR_OK;
+ }
+ else
+ {
+ // chip not supported
+ snprintf(buf, buf_size, "Cannot identify target as a avr\n");
+ return ERROR_FLASH_OPERATION_FAILED;
+ }
+}
+
+static int avrf_mass_erase(struct flash_bank_s *bank)
+{
+ target_t *target = bank->target;
+ avr_common_t *avr = target->arch_info;
+
+ if (target->state != TARGET_HALTED)
+ {
+ LOG_ERROR("Target not halted");
+ return ERROR_TARGET_NOT_HALTED;
+ }
+
+ if ((ERROR_OK != avr_jtagprg_enterprogmode(avr))
+ || (ERROR_OK != avr_jtagprg_chiperase(avr))
+ || (ERROR_OK != avr_jtagprg_leaveprogmode(avr)))
+ {
+ return ERROR_FAIL;
+ }
+
+ return ERROR_OK;
+}
+
+static int avrf_handle_mass_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+ flash_bank_t *bank;
+ int i;
+
+ if (argc < 1)
+ {
+ command_print(cmd_ctx, "avr mass_erase <bank>");
+ return ERROR_OK;
+ }
+
+ bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
+ if (!bank)
+ {
+ command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
+ return ERROR_OK;
+ }
+
+ if (avrf_mass_erase(bank) == ERROR_OK)
+ {
+ /* set all sectors as erased */
+ for (i = 0; i < bank->num_sectors; i++)
+ {
+ bank->sectors[i].is_erased = 1;
+ }
+
+ command_print(cmd_ctx, "avr mass erase complete");
+ }
+ else
+ {
+ command_print(cmd_ctx, "avr mass erase failed");
+ }
+
+ LOG_DEBUG(__FUNCTION__);
+ return ERROR_OK;
+}
-/***************************************************************************\r
- * Copyright (C) 2009 by Simon Qian *\r
- * SimonQian@SimonQian.com *\r
- * *\r
- * This program is free software; you can redistribute it and/or modify *\r
- * it under the terms of the GNU General Public License as published by *\r
- * the Free Software Foundation; either version 2 of the License, or *\r
- * (at your option) any later version. *\r
- * *\r
- * This program is distributed in the hope that it will be useful, *\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *\r
- * GNU General Public License for more details. *\r
- * *\r
- * You should have received a copy of the GNU General Public License *\r
- * along with this program; if not, write to the *\r
- * Free Software Foundation, Inc., *\r
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *\r
- ***************************************************************************/\r
-#ifdef HAVE_CONFIG_H\r
-#include "config.h"\r
-#endif\r
-\r
-#include "replacements.h"\r
-\r
-#include "avrt.h"\r
-\r
-#include "register.h"\r
-#include "target.h"\r
-#include "log.h"\r
-#include "jtag.h"\r
-#include "binarybuffer.h"\r
-#include "time_support.h"\r
-#include "breakpoints.h"\r
-#include "fileio.h"\r
-\r
-#include <stdlib.h>\r
-#include <string.h>\r
-\r
-#include <sys/types.h>\r
-#include <unistd.h>\r
-#include <errno.h>\r
-\r
-#define AVR_JTAG_INS_LEN 4\r
-\r
-/* cli handling */\r
-int avr_register_commands(struct command_context_s *cmd_ctx);\r
-\r
-/* forward declarations */\r
-int avr_target_create(struct target_s *target, Jim_Interp *interp);\r
-int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target);\r
-int avr_quit(void);\r
-\r
-int avr_arch_state(struct target_s *target);\r
-int avr_poll(target_t *target);\r
-int avr_halt(target_t *target);\r
-int avr_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution);\r
-int avr_step(struct target_s *target, int current, u32 address, int handle_breakpoints);\r
-\r
-int avr_assert_reset(target_t *target);\r
-int avr_deassert_reset(target_t *target);\r
-int avr_soft_reset_halt(struct target_s *target);\r
-\r
-/* IR and DR functions */\r
-int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out);\r
-int avr_jtag_senddat(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int len);\r
-\r
-int mcu_write_ir(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int ir_len, int rti);\r
-int mcu_write_dr(jtag_tap_t *tap, u8 *dr_in, u8 *dr_out, int dr_len, int rti);\r
-int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti);\r
-int mcu_write_dr_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int dr_len, int rti);\r
-int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti);\r
-int mcu_write_dr_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int dr_len, int rti);\r
-int mcu_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti);\r
-int mcu_write_dr_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int dr_len, int rti);\r
-int mcu_execute_queue(void);\r
-\r
-target_type_t avr_target =\r
-{\r
- .name = "avr",\r
-\r
- .poll = avr_poll,\r
- .arch_state = avr_arch_state,\r
-\r
- .target_request_data = NULL,\r
-\r
- .halt = avr_halt,\r
- .resume = avr_resume,\r
- .step = avr_step,\r
-\r
- .assert_reset = avr_assert_reset,\r
- .deassert_reset = avr_deassert_reset,\r
- .soft_reset_halt = avr_soft_reset_halt,\r
-/*\r
- .get_gdb_reg_list = avr_get_gdb_reg_list,\r
-\r
- .read_memory = avr_read_memory,\r
- .write_memory = avr_write_memory,\r
- .bulk_write_memory = avr_bulk_write_memory,\r
- .checksum_memory = avr_checksum_memory,\r
- .blank_check_memory = avr_blank_check_memory,\r
-\r
- .run_algorithm = avr_run_algorithm,\r
-\r
- .add_breakpoint = avr_add_breakpoint,\r
- .remove_breakpoint = avr_remove_breakpoint,\r
- .add_watchpoint = avr_add_watchpoint,\r
- .remove_watchpoint = avr_remove_watchpoint,\r
-*/\r
- .register_commands = avr_register_commands,\r
- .target_create = avr_target_create,\r
- .init_target = avr_init_target,\r
- .quit = avr_quit,\r
-/*\r
- .virt2phys = avr_virt2phys,\r
- .mmu = avr_mmu\r
-*/\r
-};\r
-\r
-int avr_register_commands(struct command_context_s *cmd_ctx)\r
-{\r
- LOG_DEBUG(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-int avr_target_create(struct target_s *target, Jim_Interp *interp)\r
-{\r
- avr_common_t *avr = calloc(1, sizeof(avr_common_t));\r
- \r
- avr->jtag_info.tap = target->tap;\r
- target->arch_info = avr;\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target)\r
-{\r
- LOG_DEBUG(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-int avr_quit(void)\r
-{\r
- LOG_DEBUG(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-int avr_arch_state(struct target_s *target)\r
-{\r
- LOG_DEBUG(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-int avr_poll(target_t *target)\r
-{\r
- if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))\r
- {\r
- target->state = TARGET_HALTED;\r
- }\r
- \r
- LOG_DEBUG(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-int avr_halt(target_t *target)\r
-{\r
- LOG_DEBUG(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-int avr_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)\r
-{\r
- LOG_DEBUG(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-int avr_step(struct target_s *target, int current, u32 address, int handle_breakpoints)\r
-{\r
- LOG_DEBUG(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-int avr_assert_reset(target_t *target)\r
-{\r
- target->state = TARGET_RESET;\r
- \r
- LOG_DEBUG(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-int avr_deassert_reset(target_t *target)\r
-{\r
- target->state = TARGET_RUNNING;\r
- \r
- LOG_DEBUG(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-int avr_soft_reset_halt(struct target_s *target)\r
-{\r
- LOG_DEBUG(__FUNCTION__);\r
- return ERROR_OK;\r
-}\r
-\r
-int avr_jtag_senddat(jtag_tap_t *tap, u32* dr_in, u32 dr_out, int len)\r
-{\r
- return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);\r
-}\r
-\r
-int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out)\r
-{\r
- return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);\r
-}\r
-\r
-/* IR and DR functions */\r
-int mcu_write_ir(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int ir_len, int rti)\r
-{\r
- if (NULL == tap)\r
- {\r
- LOG_ERROR("invalid tap");\r
- return ERROR_FAIL;\r
- }\r
- if (ir_len != tap->ir_length)\r
- {\r
- LOG_ERROR("invalid ir_len");\r
- return ERROR_FAIL;\r
- }\r
- \r
- {\r
- scan_field_t field[1];\r
- \r
- field[0].tap = tap;\r
- field[0].num_bits = tap->ir_length;\r
- field[0].out_value = ir_out;\r
- field[0].out_mask = NULL;\r
- field[0].in_value = ir_in;\r
- field[0].in_check_value = NULL;\r
- field[0].in_check_mask = NULL;\r
- field[0].in_handler = NULL;\r
- field[0].in_handler_priv = NULL;\r
- jtag_add_plain_ir_scan(sizeof(field) / sizeof(field[0]), field, TAP_IDLE);\r
- }\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-int mcu_write_dr(jtag_tap_t *tap, u8 *dr_in, u8 *dr_out, int dr_len, int rti)\r
-{\r
- if (NULL == tap)\r
- {\r
- LOG_ERROR("invalid tap");\r
- return ERROR_FAIL;\r
- }\r
- \r
- {\r
- scan_field_t field[1];\r
- \r
- field[0].tap = tap;\r
- field[0].num_bits = dr_len;\r
- field[0].out_value = dr_out;\r
- field[0].out_mask = NULL;\r
- field[0].in_value = dr_in;\r
- field[0].in_check_value = NULL;\r
- field[0].in_check_mask = NULL;\r
- field[0].in_handler = NULL;\r
- field[0].in_handler_priv = NULL;\r
- jtag_add_plain_dr_scan(sizeof(field) / sizeof(field[0]), field, TAP_IDLE);\r
- }\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti)\r
-{\r
- if (ir_len > 8)\r
- {\r
- LOG_ERROR("ir_len overflow, maxium is 8");\r
- return ERROR_FAIL;\r
- }\r
- \r
- mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-int mcu_write_dr_u8(jtag_tap_t *tap, u8 *dr_in, u8 dr_out, int dr_len, int rti)\r
-{\r
- if (dr_len > 8)\r
- {\r
- LOG_ERROR("dr_len overflow, maxium is 8");\r
- return ERROR_FAIL;\r
- }\r
- \r
- mcu_write_dr(tap, dr_in, &dr_out, dr_len, rti);\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti)\r
-{\r
- if (ir_len > 16)\r
- {\r
- LOG_ERROR("ir_len overflow, maxium is 16");\r
- return ERROR_FAIL;\r
- }\r
- \r
- mcu_write_ir(tap, (u8*)ir_in, (u8*)&ir_out, ir_len, rti);\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-int mcu_write_dr_u16(jtag_tap_t *tap, u16 *dr_in, u16 dr_out, int dr_len, int rti)\r
-{\r
- if (dr_len > 16)\r
- {\r
- LOG_ERROR("dr_len overflow, maxium is 16");\r
- return ERROR_FAIL;\r
- }\r
- \r
- mcu_write_dr(tap, (u8*)dr_in, (u8*)&dr_out, dr_len, rti);\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-int mcu_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti)\r
-{\r
- if (ir_len > 32)\r
- {\r
- LOG_ERROR("ir_len overflow, maxium is 32");\r
- return ERROR_FAIL;\r
- }\r
- \r
- mcu_write_ir(tap, (u8*)ir_in, (u8*)&ir_out, ir_len, rti);\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-int mcu_write_dr_u32(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int dr_len, int rti)\r
-{\r
- if (dr_len > 32)\r
- {\r
- LOG_ERROR("dr_len overflow, maxium is 32");\r
- return ERROR_FAIL;\r
- }\r
- \r
- mcu_write_dr(tap, (u8*)dr_in, (u8*)&dr_out, dr_len, rti);\r
- \r
- return ERROR_OK;\r
-}\r
-\r
-int mcu_execute_queue(void)\r
-{\r
- return jtag_execute_queue();\r
-}\r
+/***************************************************************************
+ * Copyright (C) 2009 by Simon Qian *
+ * SimonQian@SimonQian.com *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "replacements.h"
+
+#include "avrt.h"
+
+#include "register.h"
+#include "target.h"
+#include "log.h"
+#include "jtag.h"
+#include "binarybuffer.h"
+#include "time_support.h"
+#include "breakpoints.h"
+#include "fileio.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+
+#define AVR_JTAG_INS_LEN 4
+
+/* cli handling */
+int avr_register_commands(struct command_context_s *cmd_ctx);
+
+/* forward declarations */
+int avr_target_create(struct target_s *target, Jim_Interp *interp);
+int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
+int avr_quit(void);
+
+int avr_arch_state(struct target_s *target);
+int avr_poll(target_t *target);
+int avr_halt(target_t *target);
+int avr_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution);
+int avr_step(struct target_s *target, int current, u32 address, int handle_breakpoints);
+
+int avr_assert_reset(target_t *target);
+int avr_deassert_reset(target_t *target);
+int avr_soft_reset_halt(struct target_s *target);
+
+/* IR and DR functions */
+int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out);
+int avr_jtag_senddat(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int len);
+
+int mcu_write_ir(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int ir_len, int rti);
+int mcu_write_dr(jtag_tap_t *tap, u8 *dr_in, u8 *dr_out, int dr_len, int rti);
+int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti);
+int mcu_write_dr_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int dr_len, int rti);
+int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti);
+int mcu_write_dr_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int dr_len, int rti);
+int mcu_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti);
+int mcu_write_dr_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int dr_len, int rti);
+int mcu_execute_queue(void);
+
+target_type_t avr_target =
+{
+ .name = "avr",
+
+ .poll = avr_poll,
+ .arch_state = avr_arch_state,
+
+ .target_request_data = NULL,
+
+ .halt = avr_halt,
+ .resume = avr_resume,
+ .step = avr_step,
+
+ .assert_reset = avr_assert_reset,
+ .deassert_reset = avr_deassert_reset,
+ .soft_reset_halt = avr_soft_reset_halt,
+/*
+ .get_gdb_reg_list = avr_get_gdb_reg_list,
+
+ .read_memory = avr_read_memory,
+ .write_memory = avr_write_memory,
+ .bulk_write_memory = avr_bulk_write_memory,
+ .checksum_memory = avr_checksum_memory,
+ .blank_check_memory = avr_blank_check_memory,
+
+ .run_algorithm = avr_run_algorithm,
+
+ .add_breakpoint = avr_add_breakpoint,
+ .remove_breakpoint = avr_remove_breakpoint,
+ .add_watchpoint = avr_add_watchpoint,
+ .remove_watchpoint = avr_remove_watchpoint,
+*/
+ .register_commands = avr_register_commands,
+ .target_create = avr_target_create,
+ .init_target = avr_init_target,
+ .quit = avr_quit,
+/*
+ .virt2phys = avr_virt2phys,
+ .mmu = avr_mmu
+*/
+};
+
+int avr_register_commands(struct command_context_s *cmd_ctx)
+{
+ LOG_DEBUG(__FUNCTION__);
+ return ERROR_OK;
+}
+
+int avr_target_create(struct target_s *target, Jim_Interp *interp)
+{
+ avr_common_t *avr = calloc(1, sizeof(avr_common_t));
+
+ avr->jtag_info.tap = target->tap;
+ target->arch_info = avr;
+
+ return ERROR_OK;
+}
+
+int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
+{
+ LOG_DEBUG(__FUNCTION__);
+ return ERROR_OK;
+}
+
+int avr_quit(void)
+{
+ LOG_DEBUG(__FUNCTION__);
+ return ERROR_OK;
+}
+
+int avr_arch_state(struct target_s *target)
+{
+ LOG_DEBUG(__FUNCTION__);
+ return ERROR_OK;
+}
+
+int avr_poll(target_t *target)
+{
+ if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
+ {
+ target->state = TARGET_HALTED;
+ }
+
+ LOG_DEBUG(__FUNCTION__);
+ return ERROR_OK;
+}
+
+int avr_halt(target_t *target)
+{
+ LOG_DEBUG(__FUNCTION__);
+ return ERROR_OK;
+}
+
+int avr_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
+{
+ LOG_DEBUG(__FUNCTION__);
+ return ERROR_OK;
+}
+
+int avr_step(struct target_s *target, int current, u32 address, int handle_breakpoints)
+{
+ LOG_DEBUG(__FUNCTION__);
+ return ERROR_OK;
+}
+
+int avr_assert_reset(target_t *target)
+{
+ target->state = TARGET_RESET;
+
+ LOG_DEBUG(__FUNCTION__);
+ return ERROR_OK;
+}
+
+int avr_deassert_reset(target_t *target)
+{
+ target->state = TARGET_RUNNING;
+
+ LOG_DEBUG(__FUNCTION__);
+ return ERROR_OK;
+}
+
+int avr_soft_reset_halt(struct target_s *target)
+{
+ LOG_DEBUG(__FUNCTION__);
+ return ERROR_OK;
+}
+
+int avr_jtag_senddat(jtag_tap_t *tap, u32* dr_in, u32 dr_out, int len)
+{
+ return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);
+}
+
+int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out)
+{
+ return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);
+}
+
+/* IR and DR functions */
+int mcu_write_ir(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int ir_len, int rti)
+{
+ if (NULL == tap)
+ {
+ LOG_ERROR("invalid tap");
+ return ERROR_FAIL;
+ }
+ if (ir_len != tap->ir_length)
+ {
+ LOG_ERROR("invalid ir_len");
+ return ERROR_FAIL;
+ }
+
+ {
+ scan_field_t field[1];
+
+ field[0].tap = tap;
+ field[0].num_bits = tap->ir_length;
+ field[0].out_value = ir_out;
+ field[0].out_mask = NULL;
+ field[0].in_value = ir_in;
+ field[0].in_check_value = NULL;
+ field[0].in_check_mask = NULL;
+ field[0].in_handler = NULL;
+ field[0].in_handler_priv = NULL;
+ jtag_add_plain_ir_scan(sizeof(field) / sizeof(field[0]), field, TAP_IDLE);
+ }
+
+ return ERROR_OK;
+}
+
+int mcu_write_dr(jtag_tap_t *tap, u8 *dr_in, u8 *dr_out, int dr_len, int rti)
+{
+ if (NULL == tap)
+ {
+ LOG_ERROR("invalid tap");
+ return ERROR_FAIL;
+ }
+
+ {
+ scan_field_t field[1];
+
+ field[0].tap = tap;
+ field[0].num_bits = dr_len;
+ field[0].out_value = dr_out;
+ field[0].out_mask = NULL;
+ field[0].in_value = dr_in;
+ field[0].in_check_value = NULL;
+ field[0].in_check_mask = NULL;
+ field[0].in_handler = NULL;
+ field[0].in_handler_priv = NULL;
+ jtag_add_plain_dr_scan(sizeof(field) / sizeof(field[0]), field, TAP_IDLE);
+ }
+
+ return ERROR_OK;
+}
+
+int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti)
+{
+ if (ir_len > 8)
+ {
+ LOG_ERROR("ir_len overflow, maxium is 8");
+ return ERROR_FAIL;
+ }
+
+ mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);
+
+ return ERROR_OK;
+}
+
+int mcu_write_dr_u8(jtag_tap_t *tap, u8 *dr_in, u8 dr_out, int dr_len, int rti)
+{
+ if (dr_len > 8)
+ {
+ LOG_ERROR("dr_len overflow, maxium is 8");
+ return ERROR_FAIL;
+ }
+
+ mcu_write_dr(tap, dr_in, &dr_out, dr_len, rti);
+
+ return ERROR_OK;
+}
+
+int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti)
+{
+ if (ir_len > 16)
+ {
+ LOG_ERROR("ir_len overflow, maxium is 16");
+ return ERROR_FAIL;
+ }
+
+ mcu_write_ir(tap, (u8*)ir_in, (u8*)&ir_out, ir_len, rti);
+
+ return ERROR_OK;
+}
+
+int mcu_write_dr_u16(jtag_tap_t *tap, u16 *dr_in, u16 dr_out, int dr_len, int rti)
+{
+ if (dr_len > 16)
+ {
+ LOG_ERROR("dr_len overflow, maxium is 16");
+ return ERROR_FAIL;
+ }
+
+ mcu_write_dr(tap, (u8*)dr_in, (u8*)&dr_out, dr_len, rti);
+
+ return ERROR_OK;
+}
+
+int mcu_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti)
+{
+ if (ir_len > 32)
+ {
+ LOG_ERROR("ir_len overflow, maxium is 32");
+ return ERROR_FAIL;
+ }
+
+ mcu_write_ir(tap, (u8*)ir_in, (u8*)&ir_out, ir_len, rti);
+
+ return ERROR_OK;
+}
+
+int mcu_write_dr_u32(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int dr_len, int rti)
+{
+ if (dr_len > 32)
+ {
+ LOG_ERROR("dr_len overflow, maxium is 32");
+ return ERROR_FAIL;
+ }
+
+ mcu_write_dr(tap, (u8*)dr_in, (u8*)&dr_out, dr_len, rti);
+
+ return ERROR_OK;
+}
+
+int mcu_execute_queue(void)
+{
+ return jtag_execute_queue();
+}