1 /***************************************************************************
2 * Copyright (C) 2009 by Simon Qian *
3 * SimonQian@SimonQian.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
26 #include <target/avrt.h>
29 /* AVR_JTAG_Instructions */
30 #define AVR_JTAG_INS_LEN 4
31 // Public Instructions:
32 #define AVR_JTAG_INS_EXTEST 0x00
33 #define AVR_JTAG_INS_IDCODE 0x01
34 #define AVR_JTAG_INS_SAMPLE_PRELOAD 0x02
35 #define AVR_JTAG_INS_BYPASS 0x0F
36 // AVR Specified Public Instructions:
37 #define AVR_JTAG_INS_AVR_RESET 0x0C
38 #define AVR_JTAG_INS_PROG_ENABLE 0x04
39 #define AVR_JTAG_INS_PROG_COMMANDS 0x05
40 #define AVR_JTAG_INS_PROG_PAGELOAD 0x06
41 #define AVR_JTAG_INS_PROG_PAGEREAD 0x07
44 #define AVR_JTAG_REG_Bypass_Len 1
45 #define AVR_JTAG_REG_DeviceID_Len 32
47 #define AVR_JTAG_REG_Reset_Len 1
48 #define AVR_JTAG_REG_JTAGID_Len 32
49 #define AVR_JTAG_REG_ProgrammingEnable_Len 16
50 #define AVR_JTAG_REG_ProgrammingCommand_Len 15
51 #define AVR_JTAG_REG_FlashDataByte_Len 16
53 struct avrf_type avft_chips_info
[] =
55 /* name, chip_id, flash_page_size, flash_page_num,
56 * eeprom_page_size, eeprom_page_num
58 {"atmega128", 0x9702, 256, 512, 8, 512},
59 {"at90can128", 0x9781, 256, 512, 8, 512},
62 int avr_jtag_sendinstr(struct jtag_tap
*tap
, uint8_t *ir_in
, uint8_t ir_out
);
63 int avr_jtag_senddat(struct jtag_tap
*tap
, uint32_t *dr_in
, uint32_t dr_out
, int len
);
65 int mcu_write_ir(struct jtag_tap
*tap
, uint8_t *ir_in
, uint8_t *ir_out
, int ir_len
, int rti
);
66 int mcu_write_dr(struct jtag_tap
*tap
, uint8_t *ir_in
, uint8_t *ir_out
, int dr_len
, int rti
);
67 int mcu_write_ir_u8(struct jtag_tap
*tap
, uint8_t *ir_in
, uint8_t ir_out
, int ir_len
, int rti
);
68 int mcu_write_dr_u8(struct jtag_tap
*tap
, uint8_t *ir_in
, uint8_t ir_out
, int dr_len
, int rti
);
69 int mcu_write_ir_u16(struct jtag_tap
*tap
, uint16_t *ir_in
, uint16_t ir_out
, int ir_len
, int rti
);
70 int mcu_write_dr_u16(struct jtag_tap
*tap
, uint16_t *ir_in
, uint16_t ir_out
, int dr_len
, int rti
);
71 int mcu_write_ir_u32(struct jtag_tap
*tap
, uint32_t *ir_in
, uint32_t ir_out
, int ir_len
, int rti
);
72 int mcu_write_dr_u32(struct jtag_tap
*tap
, uint32_t *ir_in
, uint32_t ir_out
, int dr_len
, int rti
);
73 int mcu_execute_queue(void);
75 /* avr program functions */
76 static int avr_jtag_reset(struct avr_common
*avr
, uint32_t reset
)
78 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_AVR_RESET
);
79 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, reset
,AVR_JTAG_REG_Reset_Len
);
84 static int avr_jtag_read_jtagid(struct avr_common
*avr
, uint32_t *id
)
86 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_IDCODE
);
87 avr_jtag_senddat(avr
->jtag_info
.tap
, id
, 0, AVR_JTAG_REG_JTAGID_Len
);
92 static int avr_jtagprg_enterprogmode(struct avr_common
*avr
)
94 avr_jtag_reset(avr
, 1);
96 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_ENABLE
);
97 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0xA370, AVR_JTAG_REG_ProgrammingEnable_Len
);
102 static int avr_jtagprg_leaveprogmode(struct avr_common
*avr
)
104 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_COMMANDS
);
105 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x2300, AVR_JTAG_REG_ProgrammingCommand_Len
);
106 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3300, AVR_JTAG_REG_ProgrammingCommand_Len
);
108 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_ENABLE
);
109 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0, AVR_JTAG_REG_ProgrammingEnable_Len
);
111 avr_jtag_reset(avr
, 0);
116 static int avr_jtagprg_chiperase(struct avr_common
*avr
)
120 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_COMMANDS
);
121 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x2380, AVR_JTAG_REG_ProgrammingCommand_Len
);
122 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3180, AVR_JTAG_REG_ProgrammingCommand_Len
);
123 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len
);
124 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len
);
128 avr_jtag_senddat(avr
->jtag_info
.tap
, &poll_value
, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len
);
129 if (ERROR_OK
!= mcu_execute_queue())
133 LOG_DEBUG("poll_value = 0x%04" PRIx32
"", poll_value
);
134 } while (!(poll_value
& 0x0200));
139 static int avr_jtagprg_writeflashpage(struct avr_common
*avr
, uint8_t *page_buf
, uint32_t buf_size
, uint32_t addr
, uint32_t page_size
)
141 uint32_t i
, poll_value
;
143 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_COMMANDS
);
144 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x2310, AVR_JTAG_REG_ProgrammingCommand_Len
);
146 // load addr high byte
147 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x0700 | ((addr
>> 9) & 0xFF), AVR_JTAG_REG_ProgrammingCommand_Len
);
149 // load addr low byte
150 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x0300 | ((addr
>> 1) & 0xFF), AVR_JTAG_REG_ProgrammingCommand_Len
);
152 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_PAGELOAD
);
154 for (i
= 0; i
< page_size
; i
++)
158 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, page_buf
[i
], 8);
162 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0xFF, 8);
166 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_COMMANDS
);
168 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len
);
169 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3500, AVR_JTAG_REG_ProgrammingCommand_Len
);
170 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len
);
171 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len
);
175 avr_jtag_senddat(avr
->jtag_info
.tap
, &poll_value
, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len
);
176 if (ERROR_OK
!= mcu_execute_queue())
180 LOG_DEBUG("poll_value = 0x%04" PRIx32
"", poll_value
);
181 } while (!(poll_value
& 0x0200));
186 FLASH_BANK_COMMAND_HANDLER(avrf_flash_bank_command
)
188 struct avrf_flash_bank
*avrf_info
;
192 LOG_WARNING("incomplete flash_bank avr configuration");
193 return ERROR_FLASH_BANK_INVALID
;
196 avrf_info
= malloc(sizeof(struct avrf_flash_bank
));
197 bank
->driver_priv
= avrf_info
;
199 avrf_info
->probed
= 0;
204 static int avrf_erase(struct flash_bank
*bank
, int first
, int last
)
206 struct target
*target
= bank
->target
;
207 struct avr_common
*avr
= target
->arch_info
;
210 LOG_DEBUG("%s", __FUNCTION__
);
212 if (target
->state
!= TARGET_HALTED
)
214 LOG_ERROR("Target not halted");
215 return ERROR_TARGET_NOT_HALTED
;
218 status
= avr_jtagprg_enterprogmode(avr
);
219 if (status
!= ERROR_OK
)
222 status
= avr_jtagprg_chiperase(avr
);
223 if (status
!= ERROR_OK
)
226 return avr_jtagprg_leaveprogmode(avr
);
229 static int avrf_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
231 LOG_INFO("%s", __FUNCTION__
);
235 static int avrf_write(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
237 struct target
*target
= bank
->target
;
238 struct avr_common
*avr
= target
->arch_info
;
239 uint32_t cur_size
, cur_buffer_size
, page_size
;
241 if (bank
->target
->state
!= TARGET_HALTED
)
243 LOG_ERROR("Target not halted");
244 return ERROR_TARGET_NOT_HALTED
;
247 page_size
= bank
->sectors
[0].size
;
248 if ((offset
% page_size
) != 0)
250 LOG_WARNING("offset 0x%" PRIx32
" breaks required %" PRIu32
"-byte alignment", offset
, page_size
);
251 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
254 LOG_DEBUG("offset is 0x%08" PRIx32
"", offset
);
255 LOG_DEBUG("count is %" PRId32
"", count
);
257 if (ERROR_OK
!= avr_jtagprg_enterprogmode(avr
))
265 if (count
> page_size
)
267 cur_buffer_size
= page_size
;
271 cur_buffer_size
= count
;
273 avr_jtagprg_writeflashpage(avr
, buffer
+ cur_size
, cur_buffer_size
, offset
+ cur_size
, page_size
);
274 count
-= cur_buffer_size
;
275 cur_size
+= cur_buffer_size
;
280 return avr_jtagprg_leaveprogmode(avr
);
283 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
284 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
285 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
286 static int avrf_probe(struct flash_bank
*bank
)
288 struct target
*target
= bank
->target
;
289 struct avrf_flash_bank
*avrf_info
= bank
->driver_priv
;
290 struct avr_common
*avr
= target
->arch_info
;
291 struct avrf_type
*avr_info
= NULL
;
295 if (bank
->target
->state
!= TARGET_HALTED
)
297 LOG_ERROR("Target not halted");
298 return ERROR_TARGET_NOT_HALTED
;
301 avrf_info
->probed
= 0;
303 avr_jtag_read_jtagid(avr
, &device_id
);
304 if (ERROR_OK
!= mcu_execute_queue())
309 LOG_INFO("device id = 0x%08" PRIx32
"", device_id
);
310 if (EXTRACT_MFG(device_id
) != 0x1F)
312 LOG_ERROR("0x%" PRIx32
" is invalid Manufacturer for avr, 0x%X is expected", EXTRACT_MFG(device_id
), 0x1F);
315 for (i
= 0; i
< (int)ARRAY_SIZE(avft_chips_info
); i
++)
317 if (avft_chips_info
[i
].chip_id
== EXTRACT_PART(device_id
))
319 avr_info
= &avft_chips_info
[i
];
320 LOG_INFO("target device is %s", avr_info
->name
);
325 if (avr_info
!= NULL
)
328 bank
->base
= 0x00000000;
329 bank
->size
= (avr_info
->flash_page_size
* avr_info
->flash_page_num
);
330 bank
->num_sectors
= avr_info
->flash_page_num
;
331 bank
->sectors
= malloc(sizeof(struct flash_sector
) * avr_info
->flash_page_num
);
333 for (i
= 0; i
< avr_info
->flash_page_num
; i
++)
335 bank
->sectors
[i
].offset
= i
* avr_info
->flash_page_size
;
336 bank
->sectors
[i
].size
= avr_info
->flash_page_size
;
337 bank
->sectors
[i
].is_erased
= -1;
338 bank
->sectors
[i
].is_protected
= 1;
341 avrf_info
->probed
= 1;
346 // chip not supported
347 LOG_ERROR("0x%" PRIx32
" is not support for avr", EXTRACT_PART(device_id
));
349 avrf_info
->probed
= 1;
354 static int avrf_auto_probe(struct flash_bank
*bank
)
356 struct avrf_flash_bank
*avrf_info
= bank
->driver_priv
;
357 if (avrf_info
->probed
)
359 return avrf_probe(bank
);
362 static int avrf_protect_check(struct flash_bank
*bank
)
364 LOG_INFO("%s", __FUNCTION__
);
368 static int avrf_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
370 struct target
*target
= bank
->target
;
371 struct avr_common
*avr
= target
->arch_info
;
372 struct avrf_type
*avr_info
= NULL
;
376 if (bank
->target
->state
!= TARGET_HALTED
)
378 LOG_ERROR("Target not halted");
379 return ERROR_TARGET_NOT_HALTED
;
382 avr_jtag_read_jtagid(avr
, &device_id
);
383 if (ERROR_OK
!= mcu_execute_queue())
388 LOG_INFO("device id = 0x%08" PRIx32
"", device_id
);
389 if (EXTRACT_MFG(device_id
) != 0x1F)
391 LOG_ERROR("0x%" PRIx32
" is invalid Manufacturer for avr, 0x%X is expected", EXTRACT_MFG(device_id
), 0x1F);
394 for (i
= 0; i
< (int)ARRAY_SIZE(avft_chips_info
); i
++)
396 if (avft_chips_info
[i
].chip_id
== EXTRACT_PART(device_id
))
398 avr_info
= &avft_chips_info
[i
];
399 LOG_INFO("target device is %s", avr_info
->name
);
405 if (avr_info
!= NULL
)
408 snprintf(buf
, buf_size
, "%s - Rev: 0x%" PRIx32
"", avr_info
->name
, EXTRACT_VER(device_id
));
413 // chip not supported
414 snprintf(buf
, buf_size
, "Cannot identify target as a avr\n");
415 return ERROR_FLASH_OPERATION_FAILED
;
419 static int avrf_mass_erase(struct flash_bank
*bank
)
421 struct target
*target
= bank
->target
;
422 struct avr_common
*avr
= target
->arch_info
;
424 if (target
->state
!= TARGET_HALTED
)
426 LOG_ERROR("Target not halted");
427 return ERROR_TARGET_NOT_HALTED
;
430 if ((ERROR_OK
!= avr_jtagprg_enterprogmode(avr
))
431 || (ERROR_OK
!= avr_jtagprg_chiperase(avr
))
432 || (ERROR_OK
!= avr_jtagprg_leaveprogmode(avr
)))
440 COMMAND_HANDLER(avrf_handle_mass_erase_command
)
446 command_print(CMD_CTX
, "avr mass_erase <bank>");
450 struct flash_bank
*bank
;
451 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
452 if (ERROR_OK
!= retval
)
455 if (avrf_mass_erase(bank
) == ERROR_OK
)
457 /* set all sectors as erased */
458 for (i
= 0; i
< bank
->num_sectors
; i
++)
460 bank
->sectors
[i
].is_erased
= 1;
463 command_print(CMD_CTX
, "avr mass erase complete");
467 command_print(CMD_CTX
, "avr mass erase failed");
470 LOG_DEBUG("%s", __FUNCTION__
);
474 static const struct command_registration avrf_exec_command_handlers
[] = {
476 .name
= "mass_erase",
477 .handler
= avrf_handle_mass_erase_command
,
478 .mode
= COMMAND_EXEC
,
479 .help
= "erase entire device",
481 COMMAND_REGISTRATION_DONE
483 static const struct command_registration avrf_command_handlers
[] = {
487 .help
= "AVR flash command group",
488 .chain
= avrf_exec_command_handlers
,
490 COMMAND_REGISTRATION_DONE
493 struct flash_driver avr_flash
= {
495 .commands
= avrf_command_handlers
,
496 .flash_bank_command
= avrf_flash_bank_command
,
498 .protect
= avrf_protect
,
501 .auto_probe
= avrf_auto_probe
,
502 .erase_check
= default_flash_mem_blank_check
,
503 .protect_check
= avrf_protect_check
,