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 ***************************************************************************/
25 #include <target/avrt.h>
28 /* AVR_JTAG_Instructions */
29 #define AVR_JTAG_INS_LEN 4
30 // Public Instructions:
31 #define AVR_JTAG_INS_EXTEST 0x00
32 #define AVR_JTAG_INS_IDCODE 0x01
33 #define AVR_JTAG_INS_SAMPLE_PRELOAD 0x02
34 #define AVR_JTAG_INS_BYPASS 0x0F
35 // AVR Specified Public Instructions:
36 #define AVR_JTAG_INS_AVR_RESET 0x0C
37 #define AVR_JTAG_INS_PROG_ENABLE 0x04
38 #define AVR_JTAG_INS_PROG_COMMANDS 0x05
39 #define AVR_JTAG_INS_PROG_PAGELOAD 0x06
40 #define AVR_JTAG_INS_PROG_PAGEREAD 0x07
43 #define AVR_JTAG_REG_Bypass_Len 1
44 #define AVR_JTAG_REG_DeviceID_Len 32
46 #define AVR_JTAG_REG_Reset_Len 1
47 #define AVR_JTAG_REG_JTAGID_Len 32
48 #define AVR_JTAG_REG_ProgrammingEnable_Len 16
49 #define AVR_JTAG_REG_ProgrammingCommand_Len 15
50 #define AVR_JTAG_REG_FlashDataByte_Len 16
62 struct avrf_flash_bank
68 static struct avrf_type avft_chips_info
[] =
70 /* name, chip_id, flash_page_size, flash_page_num,
71 * eeprom_page_size, eeprom_page_num
73 {"atmega128", 0x9702, 256, 512, 8, 512},
74 {"at90can128", 0x9781, 256, 512, 8, 512},
77 /* avr program functions */
78 static int avr_jtag_reset(struct avr_common
*avr
, uint32_t reset
)
80 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_AVR_RESET
);
81 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, reset
,AVR_JTAG_REG_Reset_Len
);
86 static int avr_jtag_read_jtagid(struct avr_common
*avr
, uint32_t *id
)
88 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_IDCODE
);
89 avr_jtag_senddat(avr
->jtag_info
.tap
, id
, 0, AVR_JTAG_REG_JTAGID_Len
);
94 static int avr_jtagprg_enterprogmode(struct avr_common
*avr
)
96 avr_jtag_reset(avr
, 1);
98 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_ENABLE
);
99 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0xA370, AVR_JTAG_REG_ProgrammingEnable_Len
);
104 static int avr_jtagprg_leaveprogmode(struct avr_common
*avr
)
106 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_COMMANDS
);
107 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x2300, AVR_JTAG_REG_ProgrammingCommand_Len
);
108 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3300, AVR_JTAG_REG_ProgrammingCommand_Len
);
110 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_ENABLE
);
111 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0, AVR_JTAG_REG_ProgrammingEnable_Len
);
113 avr_jtag_reset(avr
, 0);
118 static int avr_jtagprg_chiperase(struct avr_common
*avr
)
122 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_COMMANDS
);
123 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x2380, AVR_JTAG_REG_ProgrammingCommand_Len
);
124 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3180, AVR_JTAG_REG_ProgrammingCommand_Len
);
125 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len
);
126 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len
);
130 avr_jtag_senddat(avr
->jtag_info
.tap
, &poll_value
, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len
);
131 if (ERROR_OK
!= mcu_execute_queue())
135 LOG_DEBUG("poll_value = 0x%04" PRIx32
"", poll_value
);
136 } while (!(poll_value
& 0x0200));
141 static int avr_jtagprg_writeflashpage(struct avr_common
*avr
, uint8_t *page_buf
, uint32_t buf_size
, uint32_t addr
, uint32_t page_size
)
143 uint32_t i
, poll_value
;
145 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_COMMANDS
);
146 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x2310, AVR_JTAG_REG_ProgrammingCommand_Len
);
148 // load addr high byte
149 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x0700 | ((addr
>> 9) & 0xFF), AVR_JTAG_REG_ProgrammingCommand_Len
);
151 // load addr low byte
152 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x0300 | ((addr
>> 1) & 0xFF), AVR_JTAG_REG_ProgrammingCommand_Len
);
154 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_PAGELOAD
);
156 for (i
= 0; i
< page_size
; i
++)
160 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, page_buf
[i
], 8);
164 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0xFF, 8);
168 avr_jtag_sendinstr(avr
->jtag_info
.tap
, NULL
, AVR_JTAG_INS_PROG_COMMANDS
);
170 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len
);
171 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3500, AVR_JTAG_REG_ProgrammingCommand_Len
);
172 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len
);
173 avr_jtag_senddat(avr
->jtag_info
.tap
, NULL
, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len
);
177 avr_jtag_senddat(avr
->jtag_info
.tap
, &poll_value
, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len
);
178 if (ERROR_OK
!= mcu_execute_queue())
182 LOG_DEBUG("poll_value = 0x%04" PRIx32
"", poll_value
);
183 } while (!(poll_value
& 0x0200));
188 FLASH_BANK_COMMAND_HANDLER(avrf_flash_bank_command
)
190 struct avrf_flash_bank
*avrf_info
;
194 LOG_WARNING("incomplete flash_bank avr configuration");
195 return ERROR_FLASH_BANK_INVALID
;
198 avrf_info
= malloc(sizeof(struct avrf_flash_bank
));
199 bank
->driver_priv
= avrf_info
;
201 avrf_info
->probed
= 0;
206 static int avrf_erase(struct flash_bank
*bank
, int first
, int last
)
208 struct target
*target
= bank
->target
;
209 struct avr_common
*avr
= target
->arch_info
;
212 LOG_DEBUG("%s", __FUNCTION__
);
214 if (target
->state
!= TARGET_HALTED
)
216 LOG_ERROR("Target not halted");
217 return ERROR_TARGET_NOT_HALTED
;
220 status
= avr_jtagprg_enterprogmode(avr
);
221 if (status
!= ERROR_OK
)
224 status
= avr_jtagprg_chiperase(avr
);
225 if (status
!= ERROR_OK
)
228 return avr_jtagprg_leaveprogmode(avr
);
231 static int avrf_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
233 LOG_INFO("%s", __FUNCTION__
);
237 static int avrf_write(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
239 struct target
*target
= bank
->target
;
240 struct avr_common
*avr
= target
->arch_info
;
241 uint32_t cur_size
, cur_buffer_size
, page_size
;
243 if (bank
->target
->state
!= TARGET_HALTED
)
245 LOG_ERROR("Target not halted");
246 return ERROR_TARGET_NOT_HALTED
;
249 page_size
= bank
->sectors
[0].size
;
250 if ((offset
% page_size
) != 0)
252 LOG_WARNING("offset 0x%" PRIx32
" breaks required %" PRIu32
"-byte alignment", offset
, page_size
);
253 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
256 LOG_DEBUG("offset is 0x%08" PRIx32
"", offset
);
257 LOG_DEBUG("count is %" PRId32
"", count
);
259 if (ERROR_OK
!= avr_jtagprg_enterprogmode(avr
))
267 if (count
> page_size
)
269 cur_buffer_size
= page_size
;
273 cur_buffer_size
= count
;
275 avr_jtagprg_writeflashpage(avr
, buffer
+ cur_size
, cur_buffer_size
, offset
+ cur_size
, page_size
);
276 count
-= cur_buffer_size
;
277 cur_size
+= cur_buffer_size
;
282 return avr_jtagprg_leaveprogmode(avr
);
285 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
286 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
287 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
288 static int avrf_probe(struct flash_bank
*bank
)
290 struct target
*target
= bank
->target
;
291 struct avrf_flash_bank
*avrf_info
= bank
->driver_priv
;
292 struct avr_common
*avr
= target
->arch_info
;
293 struct avrf_type
*avr_info
= NULL
;
297 if (bank
->target
->state
!= TARGET_HALTED
)
299 LOG_ERROR("Target not halted");
300 return ERROR_TARGET_NOT_HALTED
;
303 avrf_info
->probed
= 0;
305 avr_jtag_read_jtagid(avr
, &device_id
);
306 if (ERROR_OK
!= mcu_execute_queue())
311 LOG_INFO("device id = 0x%08" PRIx32
"", device_id
);
312 if (EXTRACT_MFG(device_id
) != 0x1F)
314 LOG_ERROR("0x%" PRIx32
" is invalid Manufacturer for avr, 0x%X is expected", EXTRACT_MFG(device_id
), 0x1F);
317 for (i
= 0; i
< (int)ARRAY_SIZE(avft_chips_info
); i
++)
319 if (avft_chips_info
[i
].chip_id
== EXTRACT_PART(device_id
))
321 avr_info
= &avft_chips_info
[i
];
322 LOG_INFO("target device is %s", avr_info
->name
);
327 if (avr_info
!= NULL
)
332 bank
->sectors
= NULL
;
336 bank
->base
= 0x00000000;
337 bank
->size
= (avr_info
->flash_page_size
* avr_info
->flash_page_num
);
338 bank
->num_sectors
= avr_info
->flash_page_num
;
339 bank
->sectors
= malloc(sizeof(struct flash_sector
) * avr_info
->flash_page_num
);
341 for (i
= 0; i
< avr_info
->flash_page_num
; i
++)
343 bank
->sectors
[i
].offset
= i
* avr_info
->flash_page_size
;
344 bank
->sectors
[i
].size
= avr_info
->flash_page_size
;
345 bank
->sectors
[i
].is_erased
= -1;
346 bank
->sectors
[i
].is_protected
= 1;
349 avrf_info
->probed
= 1;
354 // chip not supported
355 LOG_ERROR("0x%" PRIx32
" is not support for avr", EXTRACT_PART(device_id
));
357 avrf_info
->probed
= 1;
362 static int avrf_auto_probe(struct flash_bank
*bank
)
364 struct avrf_flash_bank
*avrf_info
= bank
->driver_priv
;
365 if (avrf_info
->probed
)
367 return avrf_probe(bank
);
370 static int avrf_protect_check(struct flash_bank
*bank
)
372 LOG_INFO("%s", __FUNCTION__
);
376 static int avrf_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
378 struct target
*target
= bank
->target
;
379 struct avr_common
*avr
= target
->arch_info
;
380 struct avrf_type
*avr_info
= NULL
;
384 if (bank
->target
->state
!= TARGET_HALTED
)
386 LOG_ERROR("Target not halted");
387 return ERROR_TARGET_NOT_HALTED
;
390 avr_jtag_read_jtagid(avr
, &device_id
);
391 if (ERROR_OK
!= mcu_execute_queue())
396 LOG_INFO("device id = 0x%08" PRIx32
"", device_id
);
397 if (EXTRACT_MFG(device_id
) != 0x1F)
399 LOG_ERROR("0x%" PRIx32
" is invalid Manufacturer for avr, 0x%X is expected", EXTRACT_MFG(device_id
), 0x1F);
402 for (i
= 0; i
< (int)ARRAY_SIZE(avft_chips_info
); i
++)
404 if (avft_chips_info
[i
].chip_id
== EXTRACT_PART(device_id
))
406 avr_info
= &avft_chips_info
[i
];
407 LOG_INFO("target device is %s", avr_info
->name
);
413 if (avr_info
!= NULL
)
416 snprintf(buf
, buf_size
, "%s - Rev: 0x%" PRIx32
"", avr_info
->name
, EXTRACT_VER(device_id
));
421 // chip not supported
422 snprintf(buf
, buf_size
, "Cannot identify target as a avr\n");
423 return ERROR_FLASH_OPERATION_FAILED
;
427 static int avrf_mass_erase(struct flash_bank
*bank
)
429 struct target
*target
= bank
->target
;
430 struct avr_common
*avr
= target
->arch_info
;
432 if (target
->state
!= TARGET_HALTED
)
434 LOG_ERROR("Target not halted");
435 return ERROR_TARGET_NOT_HALTED
;
438 if ((ERROR_OK
!= avr_jtagprg_enterprogmode(avr
))
439 || (ERROR_OK
!= avr_jtagprg_chiperase(avr
))
440 || (ERROR_OK
!= avr_jtagprg_leaveprogmode(avr
)))
448 COMMAND_HANDLER(avrf_handle_mass_erase_command
)
454 command_print(CMD_CTX
, "avr mass_erase <bank>");
458 struct flash_bank
*bank
;
459 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
460 if (ERROR_OK
!= retval
)
463 if (avrf_mass_erase(bank
) == ERROR_OK
)
465 /* set all sectors as erased */
466 for (i
= 0; i
< bank
->num_sectors
; i
++)
468 bank
->sectors
[i
].is_erased
= 1;
471 command_print(CMD_CTX
, "avr mass erase complete");
475 command_print(CMD_CTX
, "avr mass erase failed");
478 LOG_DEBUG("%s", __FUNCTION__
);
482 static const struct command_registration avrf_exec_command_handlers
[] = {
484 .name
= "mass_erase",
485 .handler
= avrf_handle_mass_erase_command
,
486 .mode
= COMMAND_EXEC
,
487 .help
= "erase entire device",
489 COMMAND_REGISTRATION_DONE
491 static const struct command_registration avrf_command_handlers
[] = {
495 .help
= "AVR flash command group",
496 .chain
= avrf_exec_command_handlers
,
498 COMMAND_REGISTRATION_DONE
501 struct flash_driver avr_flash
= {
503 .commands
= avrf_command_handlers
,
504 .flash_bank_command
= avrf_flash_bank_command
,
506 .protect
= avrf_protect
,
508 .read
= default_flash_read
,
510 .auto_probe
= avrf_auto_probe
,
511 .erase_check
= default_flash_mem_blank_check
,
512 .protect_check
= avrf_protect_check
,