1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
28 #include "arm7_9_common.h"
31 static int str9xpec_register_commands(struct command_context_s *cmd_ctx);
32 static int str9xpec_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
33 static int str9xpec_erase(struct flash_bank_s *bank, int first, int last);
34 static int str9xpec_protect(struct flash_bank_s *bank, int set, int first, int last);
35 static int str9xpec_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
36 static int str9xpec_probe(struct flash_bank_s *bank);
37 static int str9xpec_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
38 static int str9xpec_protect_check(struct flash_bank_s *bank);
39 static int str9xpec_erase_check(struct flash_bank_s *bank);
40 static int str9xpec_info(struct flash_bank_s *bank, char *buf, int buf_size);
42 static int str9xpec_erase_area(struct flash_bank_s *bank, int first, int last);
43 static int str9xpec_set_address(struct flash_bank_s *bank, u8 sector);
44 static int str9xpec_write_options(struct flash_bank_s *bank);
46 static int str9xpec_handle_flash_options_cmap_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
47 static int str9xpec_handle_flash_options_lvdthd_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 static int str9xpec_handle_flash_options_lvdsel_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 static int str9xpec_handle_flash_options_lvdwarn_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50 static int str9xpec_handle_flash_options_read_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
51 static int str9xpec_handle_flash_options_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 static int str9xpec_handle_flash_lock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 static int str9xpec_handle_flash_unlock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
54 static int str9xpec_handle_flash_enable_turbo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
55 static int str9xpec_handle_flash_disable_turbo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
57 flash_driver_t str9xpec_flash =
60 .register_commands = str9xpec_register_commands,
61 .flash_bank_command = str9xpec_flash_bank_command,
62 .erase = str9xpec_erase,
63 .protect = str9xpec_protect,
64 .write = str9xpec_write,
65 .probe = str9xpec_probe,
66 .auto_probe = str9xpec_probe,
67 .erase_check = str9xpec_erase_check,
68 .protect_check = str9xpec_protect_check,
72 static int str9xpec_register_commands(struct command_context_s *cmd_ctx)
74 command_t *str9xpec_cmd = register_command(cmd_ctx, NULL, "str9xpec", NULL, COMMAND_ANY, "str9xpec flash specific commands");
76 register_command(cmd_ctx, str9xpec_cmd, "enable_turbo", str9xpec_handle_flash_enable_turbo_command, COMMAND_EXEC,
77 "enable str9xpec turbo mode");
78 register_command(cmd_ctx, str9xpec_cmd, "disable_turbo", str9xpec_handle_flash_disable_turbo_command, COMMAND_EXEC,
79 "disable str9xpec turbo mode");
80 register_command(cmd_ctx, str9xpec_cmd, "options_cmap", str9xpec_handle_flash_options_cmap_command, COMMAND_EXEC,
81 "configure str9xpec boot sector");
82 register_command(cmd_ctx, str9xpec_cmd, "options_lvdthd", str9xpec_handle_flash_options_lvdthd_command, COMMAND_EXEC,
83 "configure str9xpec lvd threshold");
84 register_command(cmd_ctx, str9xpec_cmd, "options_lvdsel", str9xpec_handle_flash_options_lvdsel_command, COMMAND_EXEC,
85 "configure str9xpec lvd selection");
86 register_command(cmd_ctx, str9xpec_cmd, "options_lvdwarn", str9xpec_handle_flash_options_lvdwarn_command, COMMAND_EXEC,
87 "configure str9xpec lvd warning");
88 register_command(cmd_ctx, str9xpec_cmd, "options_read", str9xpec_handle_flash_options_read_command, COMMAND_EXEC,
89 "read str9xpec options");
90 register_command(cmd_ctx, str9xpec_cmd, "options_write", str9xpec_handle_flash_options_write_command, COMMAND_EXEC,
91 "write str9xpec options");
92 register_command(cmd_ctx, str9xpec_cmd, "lock", str9xpec_handle_flash_lock_command, COMMAND_EXEC,
93 "lock str9xpec device");
94 register_command(cmd_ctx, str9xpec_cmd, "unlock", str9xpec_handle_flash_unlock_command, COMMAND_EXEC,
95 "unlock str9xpec device");
96 register_command(cmd_ctx, str9xpec_cmd, "part_id", str9xpec_handle_part_id_command, COMMAND_EXEC,
97 "print part id of str9xpec flash bank <num>");
102 int str9xpec_set_instr(jtag_tap_t *tap, u32 new_instr, tap_state_t end_state)
105 return ERROR_TARGET_INVALID;
108 if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr)
113 field.num_bits = tap->ir_length;
114 field.out_value = calloc(CEIL(field.num_bits, 8), 1);
115 buf_set_u32(field.out_value, 0, field.num_bits, new_instr);
117 field.in_value = NULL;
123 jtag_add_ir_scan(1, &field, end_state);
125 free(field.out_value);
131 static u8 str9xpec_isc_status(jtag_tap_t *tap)
136 if (str9xpec_set_instr(tap, ISC_NOOP, TAP_IRPAUSE) != ERROR_OK)
137 return ISC_STATUS_ERROR;
141 field.out_value = NULL;
143 field.in_value = &status;
149 jtag_add_dr_scan(1, &field, TAP_IDLE);
150 jtag_execute_queue();
152 LOG_DEBUG("status: 0x%2.2x", status);
154 if (status & ISC_STATUS_SECURITY)
155 LOG_INFO("Device Security Bit Set");
160 static int str9xpec_isc_enable(struct flash_bank_s *bank)
164 str9xpec_flash_controller_t *str9xpec_info = bank->driver_priv;
166 tap = str9xpec_info->tap;
168 if (str9xpec_info->isc_enable)
172 if (str9xpec_set_instr(tap, ISC_ENABLE, TAP_IDLE) != ERROR_OK)
173 return ERROR_TARGET_INVALID;
175 /* check ISC status */
176 status = str9xpec_isc_status(tap);
177 if (status & ISC_STATUS_MODE)
179 /* we have entered isc mode */
180 str9xpec_info->isc_enable = 1;
181 LOG_DEBUG("ISC_MODE Enabled");
187 static int str9xpec_isc_disable(struct flash_bank_s *bank)
191 str9xpec_flash_controller_t *str9xpec_info = bank->driver_priv;
193 tap = str9xpec_info->tap;
195 if (!str9xpec_info->isc_enable)
198 if (str9xpec_set_instr(tap, ISC_DISABLE, TAP_IDLE) != ERROR_OK)
199 return ERROR_TARGET_INVALID;
201 /* delay to handle aborts */
204 /* check ISC status */
205 status = str9xpec_isc_status(tap);
206 if (!(status & ISC_STATUS_MODE))
208 /* we have left isc mode */
209 str9xpec_info->isc_enable = 0;
210 LOG_DEBUG("ISC_MODE Disabled");
216 static int str9xpec_read_config(struct flash_bank_s *bank)
222 str9xpec_flash_controller_t *str9xpec_info = bank->driver_priv;
224 tap = str9xpec_info->tap;
226 LOG_DEBUG("ISC_CONFIGURATION");
228 /* execute ISC_CONFIGURATION command */
229 str9xpec_set_instr(tap, ISC_CONFIGURATION, TAP_IRPAUSE);
233 field.out_value = NULL;
235 field.in_value = str9xpec_info->options;
241 jtag_add_dr_scan(1, &field, TAP_IDLE);
242 jtag_execute_queue();
244 status = str9xpec_isc_status(tap);
249 static int str9xpec_build_block_list(struct flash_bank_s *bank)
251 str9xpec_flash_controller_t *str9xpec_info = bank->driver_priv;
255 int b0_sectors = 0, b1_sectors = 0;
257 int b1_size = 0x2000;
281 LOG_ERROR("BUG: unknown bank->size encountered");
285 num_sectors = b0_sectors + b1_sectors;
287 bank->num_sectors = num_sectors;
288 bank->sectors = malloc(sizeof(flash_sector_t) * num_sectors);
289 str9xpec_info->sector_bits = malloc(sizeof(u32) * num_sectors);
293 for (i = 0; i < b0_sectors; i++)
295 bank->sectors[num_sectors].offset = offset;
296 bank->sectors[num_sectors].size = 0x10000;
297 offset += bank->sectors[i].size;
298 bank->sectors[num_sectors].is_erased = -1;
299 bank->sectors[num_sectors].is_protected = 1;
300 str9xpec_info->sector_bits[num_sectors++] = i;
303 for (i = 0; i < b1_sectors; i++)
305 bank->sectors[num_sectors].offset = offset;
306 bank->sectors[num_sectors].size = b1_size;
307 offset += bank->sectors[i].size;
308 bank->sectors[num_sectors].is_erased = -1;
309 bank->sectors[num_sectors].is_protected = 1;
310 str9xpec_info->sector_bits[num_sectors++] = i + 32;
316 /* flash bank str9x <base> <size> 0 0 <target#>
318 static int str9xpec_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
320 str9xpec_flash_controller_t *str9xpec_info;
321 armv4_5_common_t *armv4_5 = NULL;
322 arm7_9_common_t *arm7_9 = NULL;
323 arm_jtag_t *jtag_info = NULL;
327 LOG_WARNING("incomplete flash_bank str9x configuration");
328 return ERROR_FLASH_BANK_INVALID;
331 str9xpec_info = malloc(sizeof(str9xpec_flash_controller_t));
332 bank->driver_priv = str9xpec_info;
334 /* find out jtag position of flash controller
335 * it is always after the arm966 core */
337 armv4_5 = bank->target->arch_info;
338 arm7_9 = armv4_5->arch_info;
339 jtag_info = &arm7_9->jtag_info;
341 str9xpec_info->tap = jtag_TapByAbsPosition( jtag_info->tap->abs_chain_position - 1);
342 str9xpec_info->isc_enable = 0;
344 str9xpec_build_block_list(bank);
346 /* clear option byte register */
347 buf_set_u32(str9xpec_info->options, 0, 64, 0);
352 static int str9xpec_blank_check(struct flash_bank_s *bank, int first, int last)
360 str9xpec_flash_controller_t *str9xpec_info = bank->driver_priv;
362 tap = str9xpec_info->tap;
364 if (!str9xpec_info->isc_enable) {
365 str9xpec_isc_enable( bank );
368 if (!str9xpec_info->isc_enable) {
369 return ERROR_FLASH_OPERATION_FAILED;
372 buffer = calloc(CEIL(64, 8), 1);
374 LOG_DEBUG("blank check: first_bank: %i, last_bank: %i", first, last);
376 for (i = first; i <= last; i++) {
377 buf_set_u32(buffer, str9xpec_info->sector_bits[i], 1, 1);
380 /* execute ISC_BLANK_CHECK command */
381 str9xpec_set_instr(tap, ISC_BLANK_CHECK, TAP_IRPAUSE);
385 field.out_value = buffer;
387 field.in_value = NULL;
393 jtag_add_dr_scan(1, &field, TAP_IDLE);
394 jtag_add_sleep(40000);
396 /* read blank check result */
399 field.out_value = NULL;
401 field.in_value = buffer;
407 jtag_add_dr_scan(1, &field, TAP_IRPAUSE);
408 jtag_execute_queue();
410 status = str9xpec_isc_status(tap);
412 for (i = first; i <= last; i++)
414 if (buf_get_u32(buffer, str9xpec_info->sector_bits[i], 1))
415 bank->sectors[i].is_erased = 0;
417 bank->sectors[i].is_erased = 1;
422 str9xpec_isc_disable(bank);
424 if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
425 return ERROR_FLASH_OPERATION_FAILED;
429 static int str9xpec_protect_check(struct flash_bank_s *bank)
434 str9xpec_flash_controller_t *str9xpec_info = bank->driver_priv;
436 status = str9xpec_read_config(bank);
438 for (i = 0; i < bank->num_sectors; i++)
440 if (buf_get_u32(str9xpec_info->options, str9xpec_info->sector_bits[i], 1))
441 bank->sectors[i].is_protected = 1;
443 bank->sectors[i].is_protected = 0;
446 if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
447 return ERROR_FLASH_OPERATION_FAILED;
451 static int str9xpec_erase_area(struct flash_bank_s *bank, int first, int last)
459 str9xpec_flash_controller_t *str9xpec_info = bank->driver_priv;
461 tap = str9xpec_info->tap;
463 if (!str9xpec_info->isc_enable) {
464 str9xpec_isc_enable( bank );
467 if (!str9xpec_info->isc_enable) {
468 return ISC_STATUS_ERROR;
471 buffer = calloc(CEIL(64, 8), 1);
473 LOG_DEBUG("erase: first_bank: %i, last_bank: %i", first, last);
475 /* last bank: 0xFF signals a full erase (unlock complete device) */
476 /* last bank: 0xFE signals a option byte erase */
479 for (i = 0; i < 64; i++) {
480 buf_set_u32(buffer, i, 1, 1);
483 else if (last == 0xFE)
485 buf_set_u32(buffer, 49, 1, 1);
489 for (i = first; i <= last; i++) {
490 buf_set_u32(buffer, str9xpec_info->sector_bits[i], 1, 1);
494 LOG_DEBUG("ISC_ERASE");
496 /* execute ISC_ERASE command */
497 str9xpec_set_instr(tap, ISC_ERASE, TAP_IRPAUSE);
501 field.out_value = buffer;
503 field.in_value = NULL;
509 jtag_add_dr_scan(1, &field, TAP_IDLE);
510 jtag_execute_queue();
514 /* wait for erase completion */
515 while (!((status = str9xpec_isc_status(tap)) & ISC_STATUS_BUSY)) {
521 str9xpec_isc_disable(bank);
526 static int str9xpec_erase(struct flash_bank_s *bank, int first, int last)
530 status = str9xpec_erase_area(bank, first, last);
532 if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
533 return ERROR_FLASH_OPERATION_FAILED;
538 static int str9xpec_lock_device(struct flash_bank_s *bank)
543 str9xpec_flash_controller_t *str9xpec_info = NULL;
545 str9xpec_info = bank->driver_priv;
546 tap = str9xpec_info->tap;
548 if (!str9xpec_info->isc_enable) {
549 str9xpec_isc_enable( bank );
552 if (!str9xpec_info->isc_enable) {
553 return ISC_STATUS_ERROR;
556 /* set security address */
557 str9xpec_set_address(bank, 0x80);
559 /* execute ISC_PROGRAM command */
560 str9xpec_set_instr(tap, ISC_PROGRAM_SECURITY, TAP_IDLE);
562 str9xpec_set_instr(tap, ISC_NOOP, TAP_IRPAUSE);
567 field.out_value = NULL;
569 field.in_value = &status;
575 jtag_add_dr_scan(1, &field, -1);
576 jtag_execute_queue();
578 } while(!(status & ISC_STATUS_BUSY));
580 str9xpec_isc_disable(bank);
585 static int str9xpec_unlock_device(struct flash_bank_s *bank)
589 status = str9xpec_erase_area(bank, 0, 255);
594 static int str9xpec_protect(struct flash_bank_s *bank, int set, int first, int last)
599 str9xpec_flash_controller_t *str9xpec_info = bank->driver_priv;
601 status = str9xpec_read_config(bank);
603 if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
604 return ERROR_FLASH_OPERATION_FAILED;
606 LOG_DEBUG("protect: first_bank: %i, last_bank: %i", first, last);
608 /* last bank: 0xFF signals a full device protect */
613 status = str9xpec_lock_device(bank);
617 /* perform full erase to unlock device */
618 status = str9xpec_unlock_device(bank);
623 for (i = first; i <= last; i++)
626 buf_set_u32(str9xpec_info->options, str9xpec_info->sector_bits[i], 1, 1);
628 buf_set_u32(str9xpec_info->options, str9xpec_info->sector_bits[i], 1, 0);
631 status = str9xpec_write_options(bank);
634 if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
635 return ERROR_FLASH_OPERATION_FAILED;
640 static int str9xpec_set_address(struct flash_bank_s *bank, u8 sector)
644 str9xpec_flash_controller_t *str9xpec_info = bank->driver_priv;
646 tap = str9xpec_info->tap;
648 /* set flash controller address */
649 str9xpec_set_instr(tap, ISC_ADDRESS_SHIFT, TAP_IRPAUSE);
653 field.out_value = §or;
655 field.in_value = NULL;
661 jtag_add_dr_scan(1, &field, -1);
666 static int str9xpec_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
668 str9xpec_flash_controller_t *str9xpec_info = bank->driver_priv;
669 u32 dwords_remaining = (count / 8);
670 u32 bytes_remaining = (count & 0x00000007);
671 u32 bytes_written = 0;
673 u32 check_address = offset;
678 int first_sector = 0;
681 tap = str9xpec_info->tap;
683 if (!str9xpec_info->isc_enable) {
684 str9xpec_isc_enable(bank);
687 if (!str9xpec_info->isc_enable) {
688 return ERROR_FLASH_OPERATION_FAILED;
693 LOG_WARNING("offset 0x%x breaks required 8-byte alignment", offset);
694 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
697 for (i = 0; i < bank->num_sectors; i++)
699 u32 sec_start = bank->sectors[i].offset;
700 u32 sec_end = sec_start + bank->sectors[i].size;
702 /* check if destination falls within the current sector */
703 if ((check_address >= sec_start) && (check_address < sec_end))
705 /* check if destination ends in the current sector */
706 if (offset + count < sec_end)
707 check_address = offset + count;
709 check_address = sec_end;
712 if ((offset >= sec_start) && (offset < sec_end)){
716 if ((offset + count >= sec_start) && (offset + count < sec_end)){
721 if (check_address != offset + count)
722 return ERROR_FLASH_DST_OUT_OF_BANK;
724 LOG_DEBUG("first_sector: %i, last_sector: %i", first_sector, last_sector);
726 scanbuf = calloc(CEIL(64, 8), 1);
728 LOG_DEBUG("ISC_PROGRAM");
730 for (i = first_sector; i <= last_sector; i++)
732 str9xpec_set_address(bank, str9xpec_info->sector_bits[i]);
734 dwords_remaining = dwords_remaining < (bank->sectors[i].size/8) ? dwords_remaining : (bank->sectors[i].size/8);
736 while (dwords_remaining > 0)
738 str9xpec_set_instr(tap, ISC_PROGRAM, TAP_IRPAUSE);
742 field.out_value = (buffer + bytes_written);
744 field.in_value = NULL;
750 jtag_add_dr_scan(1, &field, TAP_IDLE);
752 /* small delay before polling */
755 str9xpec_set_instr(tap, ISC_NOOP, TAP_IRPAUSE);
760 field.out_value = NULL;
762 field.in_value = scanbuf;
768 jtag_add_dr_scan(1, &field, -1);
769 jtag_execute_queue();
771 status = buf_get_u32(scanbuf, 0, 8);
773 } while(!(status & ISC_STATUS_BUSY));
775 if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
776 return ERROR_FLASH_OPERATION_FAILED;
778 /* if ((status & ISC_STATUS_INT_ERROR) != STR9XPEC_ISC_INTFAIL)
779 return ERROR_FLASH_OPERATION_FAILED; */
788 u8 last_dword[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
791 while(bytes_remaining > 0)
793 last_dword[i++] = *(buffer + bytes_written);
798 str9xpec_set_instr(tap, ISC_PROGRAM, TAP_IRPAUSE);
802 field.out_value = last_dword;
804 field.in_value = NULL;
810 jtag_add_dr_scan(1, &field, TAP_IDLE);
812 /* small delay before polling */
815 str9xpec_set_instr(tap, ISC_NOOP, TAP_IRPAUSE);
820 field.out_value = NULL;
822 field.in_value = scanbuf;
828 jtag_add_dr_scan(1, &field, -1);
829 jtag_execute_queue();
831 status = buf_get_u32(scanbuf, 0, 8);
833 } while(!(status & ISC_STATUS_BUSY));
835 if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
836 return ERROR_FLASH_OPERATION_FAILED;
838 /* if ((status & ISC_STATUS_INT_ERROR) != STR9XPEC_ISC_INTFAIL)
839 return ERROR_FLASH_OPERATION_FAILED; */
844 str9xpec_isc_disable(bank);
849 static int str9xpec_probe(struct flash_bank_s *bank)
854 static int str9xpec_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
861 str9xpec_flash_controller_t *str9xpec_info = NULL;
865 return ERROR_COMMAND_SYNTAX_ERROR;
868 bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
871 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
875 str9xpec_info = bank->driver_priv;
876 tap = str9xpec_info->tap;
878 buffer = calloc(CEIL(32, 8), 1);
880 str9xpec_set_instr(tap, ISC_IDCODE, TAP_IRPAUSE);
884 field.out_value = NULL;
886 field.in_value = buffer;
892 jtag_add_dr_scan(1, &field, TAP_IDLE);
893 jtag_execute_queue();
895 idcode = buf_get_u32(buffer, 0, 32);
897 command_print(cmd_ctx, "str9xpec part id: 0x%8.8x", idcode);
904 static int str9xpec_erase_check(struct flash_bank_s *bank)
906 return str9xpec_blank_check(bank, 0, bank->num_sectors - 1);
909 static int str9xpec_info(struct flash_bank_s *bank, char *buf, int buf_size)
911 snprintf(buf, buf_size, "str9xpec flash driver info" );
915 static int str9xpec_handle_flash_options_read_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
919 str9xpec_flash_controller_t *str9xpec_info = NULL;
923 command_print(cmd_ctx, "str9xpec options_read <bank>");
927 bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
930 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
934 str9xpec_info = bank->driver_priv;
936 status = str9xpec_read_config(bank);
938 if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
939 return ERROR_FLASH_OPERATION_FAILED;
942 if (buf_get_u32(str9xpec_info->options, STR9XPEC_OPT_CSMAPBIT, 1))
943 command_print(cmd_ctx, "CS Map: bank1");
945 command_print(cmd_ctx, "CS Map: bank0");
948 if (buf_get_u32(str9xpec_info->options, STR9XPEC_OPT_OTPBIT, 1))
949 command_print(cmd_ctx, "OTP Lock: OTP Locked");
951 command_print(cmd_ctx, "OTP Lock: OTP Unlocked");
954 if (buf_get_u32(str9xpec_info->options, STR9XPEC_OPT_LVDTHRESBIT, 1))
955 command_print(cmd_ctx, "LVD Threshold: 2.7v");
957 command_print(cmd_ctx, "LVD Threshold: 2.4v");
959 /* LVD reset warning */
960 if (buf_get_u32(str9xpec_info->options, STR9XPEC_OPT_LVDWARNBIT, 1))
961 command_print(cmd_ctx, "LVD Reset Warning: VDD or VDDQ Inputs");
963 command_print(cmd_ctx, "LVD Reset Warning: VDD Input Only");
965 /* LVD reset select */
966 if (buf_get_u32(str9xpec_info->options, STR9XPEC_OPT_LVDSELBIT, 1))
967 command_print(cmd_ctx, "LVD Reset Selection: VDD or VDDQ Inputs");
969 command_print(cmd_ctx, "LVD Reset Selection: VDD Input Only");
974 static int str9xpec_write_options(struct flash_bank_s *bank)
979 str9xpec_flash_controller_t *str9xpec_info = NULL;
981 str9xpec_info = bank->driver_priv;
982 tap = str9xpec_info->tap;
984 /* erase config options first */
985 status = str9xpec_erase_area( bank, 0xFE, 0xFE );
987 if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
990 if (!str9xpec_info->isc_enable) {
991 str9xpec_isc_enable( bank );
994 if (!str9xpec_info->isc_enable) {
995 return ISC_STATUS_ERROR;
998 /* according to data 64th bit has to be set */
999 buf_set_u32(str9xpec_info->options, 63, 1, 1);
1001 /* set option byte address */
1002 str9xpec_set_address(bank, 0x50);
1004 /* execute ISC_PROGRAM command */
1005 str9xpec_set_instr(tap, ISC_PROGRAM, TAP_IRPAUSE);
1008 field.num_bits = 64;
1009 field.out_value = str9xpec_info->options;
1011 field.in_value = NULL;
1017 jtag_add_dr_scan(1, &field, TAP_IDLE);
1019 /* small delay before polling */
1022 str9xpec_set_instr(tap, ISC_NOOP, TAP_IRPAUSE);
1027 field.out_value = NULL;
1029 field.in_value = &status;
1035 jtag_add_dr_scan(1, &field, -1);
1036 jtag_execute_queue();
1038 } while(!(status & ISC_STATUS_BUSY));
1040 str9xpec_isc_disable(bank);
1045 static int str9xpec_handle_flash_options_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1052 command_print(cmd_ctx, "str9xpec options_write <bank>");
1056 bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
1059 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
1063 status = str9xpec_write_options(bank);
1065 if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
1066 return ERROR_FLASH_OPERATION_FAILED;
1071 static int str9xpec_handle_flash_options_cmap_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1074 str9xpec_flash_controller_t *str9xpec_info = NULL;
1078 command_print(cmd_ctx, "str9xpec options_cmap <bank> <bank0|bank1>");
1082 bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
1085 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
1089 str9xpec_info = bank->driver_priv;
1091 if (strcmp(args[1], "bank1") == 0)
1093 buf_set_u32(str9xpec_info->options, STR9XPEC_OPT_CSMAPBIT, 1, 1);
1097 buf_set_u32(str9xpec_info->options, STR9XPEC_OPT_CSMAPBIT, 1, 0);
1103 static int str9xpec_handle_flash_options_lvdthd_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1106 str9xpec_flash_controller_t *str9xpec_info = NULL;
1110 command_print(cmd_ctx, "str9xpec options_lvdthd <bank> <2.4v|2.7v>");
1114 bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
1117 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
1121 str9xpec_info = bank->driver_priv;
1123 if (strcmp(args[1], "2.7v") == 0)
1125 buf_set_u32(str9xpec_info->options, STR9XPEC_OPT_LVDTHRESBIT, 1, 1);
1129 buf_set_u32(str9xpec_info->options, STR9XPEC_OPT_LVDTHRESBIT, 1, 0);
1135 int str9xpec_handle_flash_options_lvdsel_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1138 str9xpec_flash_controller_t *str9xpec_info = NULL;
1142 command_print(cmd_ctx, "str9xpec options_lvdsel <bank> <vdd|vdd_vddq>");
1146 bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
1149 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
1153 str9xpec_info = bank->driver_priv;
1155 if (strcmp(args[1], "vdd_vddq") == 0)
1157 buf_set_u32(str9xpec_info->options, STR9XPEC_OPT_LVDSELBIT, 1, 1);
1161 buf_set_u32(str9xpec_info->options, STR9XPEC_OPT_LVDSELBIT, 1, 0);
1167 static int str9xpec_handle_flash_options_lvdwarn_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1170 str9xpec_flash_controller_t *str9xpec_info = NULL;
1174 command_print(cmd_ctx, "str9xpec options_lvdwarn <bank> <vdd|vdd_vddq>");
1178 bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
1181 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
1185 str9xpec_info = bank->driver_priv;
1187 if (strcmp(args[1], "vdd_vddq") == 0)
1189 buf_set_u32(str9xpec_info->options, STR9XPEC_OPT_LVDWARNBIT, 1, 1);
1193 buf_set_u32(str9xpec_info->options, STR9XPEC_OPT_LVDWARNBIT, 1, 0);
1199 static int str9xpec_handle_flash_lock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1206 command_print(cmd_ctx, "str9xpec lock <bank>");
1210 bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
1213 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
1217 status = str9xpec_lock_device(bank);
1219 if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
1220 return ERROR_FLASH_OPERATION_FAILED;
1225 static int str9xpec_handle_flash_unlock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1232 command_print(cmd_ctx, "str9xpec unlock <bank>");
1236 bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
1239 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
1243 status = str9xpec_unlock_device(bank);
1245 if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
1246 return ERROR_FLASH_OPERATION_FAILED;
1251 static int str9xpec_handle_flash_enable_turbo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1258 str9xpec_flash_controller_t *str9xpec_info = NULL;
1262 command_print(cmd_ctx, "str9xpec enable_turbo <bank>");
1266 bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
1269 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
1273 str9xpec_info = bank->driver_priv;
1275 tap0 = str9xpec_info->tap;
1277 /* remove arm core from chain - enter turbo mode */
1278 tap1 = tap0->next_tap;
1281 /* things are *WRONG* */
1282 command_print(cmd_ctx,"**STR9FLASH** (tap1) invalid chain?");
1285 tap2 = tap1->next_tap;
1288 /* things are *WRONG* */
1289 command_print(cmd_ctx,"**STR9FLASH** (tap2) invalid chain?");
1293 /* enable turbo mode - TURBO-PROG-ENABLE */
1294 str9xpec_set_instr(tap2, 0xD, TAP_IDLE);
1295 if ((retval = jtag_execute_queue()) != ERROR_OK)
1298 /* modify scan chain - str9 core has been removed */
1304 static int str9xpec_handle_flash_disable_turbo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1308 str9xpec_flash_controller_t *str9xpec_info = NULL;
1312 command_print(cmd_ctx, "str9xpec disable_turbo <bank>");
1316 bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
1319 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
1323 str9xpec_info = bank->driver_priv;
1324 tap = str9xpec_info->tap;
1329 /* exit turbo mode via RESET */
1330 str9xpec_set_instr(tap, ISC_NOOP, TAP_RESET);
1331 jtag_execute_queue();
1333 /* restore previous scan chain */
1334 if (tap->next_tap) {
1335 tap->next_tap->enabled = 1;