help is now implemented in startup.tcl/help
[fw/openocd] / src / flash / str9xpec.c
index 7a8fb2fec4ef51a6dfb0901b02fc64463e39a3c5..b6f966f64aeaff7490defa7d1b23da379af3bf6d 100644 (file)
 #include <unistd.h>
 #include <getopt.h>
 
-str9xpec_mem_layout_t mem_layout_str9pec[] = {
-       {0x00000000, 0x10000, 0},
-       {0x00010000, 0x10000, 1},
-       {0x00020000, 0x10000, 2},
-       {0x00030000, 0x10000, 3},
-       {0x00040000, 0x10000, 4},
-       {0x00050000, 0x10000, 5},
-       {0x00060000, 0x10000, 6},
-       {0x00070000, 0x10000, 7},
-       {0x00080000, 0x02000, 32},
-       {0x00082000, 0x02000, 33},
-       {0x00084000, 0x02000, 34},
-       {0x00086000, 0x02000, 35}
-};
-
 int str9xpec_register_commands(struct command_context_s *cmd_ctx);
 int str9xpec_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
 int str9xpec_erase(struct flash_bank_s *bank, int first, int last);
@@ -87,6 +72,7 @@ flash_driver_t str9xpec_flash =
        .protect = str9xpec_protect,
        .write = str9xpec_write,
        .probe = str9xpec_probe,
+       .auto_probe = str9xpec_probe,
        .erase_check = str9xpec_erase_check,
        .protect_check = str9xpec_protect_check,
        .info = str9xpec_info
@@ -125,6 +111,12 @@ int str9xpec_register_commands(struct command_context_s *cmd_ctx)
 int str9xpec_set_instr(int chain_pos, u32 new_instr, enum tap_state end_state)
 {
        jtag_device_t *device = jtag_get_device(chain_pos);
+       
+       if (device == NULL)
+       {
+               LOG_DEBUG("Invalid Target");
+               return ERROR_TARGET_INVALID;
+       }
                
        if (buf_get_u32(device->cur_instr, 0, device->ir_length) != new_instr)
        {
@@ -141,7 +133,7 @@ int str9xpec_set_instr(int chain_pos, u32 new_instr, enum tap_state end_state)
                field.in_handler = NULL;
                field.in_handler_priv = NULL;
                
-               jtag_add_ir_scan(1, &field, end_state, NULL);
+               jtag_add_ir_scan(1, &field, end_state);
                
                free(field.out_value);
        }
@@ -154,7 +146,8 @@ u8 str9xpec_isc_status(int chain_pos)
        scan_field_t field;
        u8 status;
        
-       str9xpec_set_instr(chain_pos, ISC_NOOP, TAP_PI);
+       if (str9xpec_set_instr(chain_pos, ISC_NOOP, TAP_PI) != ERROR_OK)
+               return ISC_STATUS_ERROR;
        
        field.device = chain_pos;
        field.num_bits = 8;
@@ -166,13 +159,13 @@ u8 str9xpec_isc_status(int chain_pos)
        field.in_handler = NULL;
        field.in_handler_priv = NULL;
        
-       jtag_add_dr_scan(1, &field, TAP_RTI, NULL);
+       jtag_add_dr_scan(1, &field, TAP_RTI);
        jtag_execute_queue();
        
-       DEBUG("status: 0x%2.2x", status);
+       LOG_DEBUG("status: 0x%2.2x", status);
        
        if (status & ISC_STATUS_SECURITY)
-               INFO("Device Security Bit Set");
+               LOG_INFO("Device Security Bit Set");
        
        return status;
 }
@@ -189,7 +182,8 @@ int str9xpec_isc_enable(struct flash_bank_s *bank)
                return ERROR_OK;
        
        /* enter isc mode */
-       str9xpec_set_instr(chain_pos, ISC_ENABLE, TAP_RTI);
+       if (str9xpec_set_instr(chain_pos, ISC_ENABLE, TAP_RTI) != ERROR_OK)
+               return ERROR_TARGET_INVALID;
        
        /* check ISC status */
        status = str9xpec_isc_status(chain_pos);
@@ -197,7 +191,7 @@ int str9xpec_isc_enable(struct flash_bank_s *bank)
        {
                /* we have entered isc mode */
                str9xpec_info->isc_enable = 1;
-               DEBUG("ISC_MODE Enabled");
+               LOG_DEBUG("ISC_MODE Enabled");
        }
        
        return ERROR_OK;
@@ -214,7 +208,8 @@ int str9xpec_isc_disable(struct flash_bank_s *bank)
        if (!str9xpec_info->isc_enable)
                return ERROR_OK;
        
-       str9xpec_set_instr(chain_pos, ISC_DISABLE, TAP_RTI);
+       if (str9xpec_set_instr(chain_pos, ISC_DISABLE, TAP_RTI) != ERROR_OK)
+               return ERROR_TARGET_INVALID;
        
        /* delay to handle aborts */
        jtag_add_sleep(50);
@@ -225,7 +220,7 @@ int str9xpec_isc_disable(struct flash_bank_s *bank)
        {
                /* we have left isc mode */
                str9xpec_info->isc_enable = 0;
-               DEBUG("ISC_MODE Disabled");
+               LOG_DEBUG("ISC_MODE Disabled");
        }
        
        return ERROR_OK;
@@ -241,7 +236,7 @@ int str9xpec_read_config(struct flash_bank_s *bank)
        
        chain_pos = str9xpec_info->chain_pos;
        
-       DEBUG("ISC_CONFIGURATION");
+       LOG_DEBUG("ISC_CONFIGURATION");
        
        /* execute ISC_CONFIGURATION command */
        str9xpec_set_instr(chain_pos, ISC_CONFIGURATION, TAP_PI);
@@ -256,7 +251,7 @@ int str9xpec_read_config(struct flash_bank_s *bank)
        field.in_handler = NULL;
        field.in_handler_priv = NULL;
        
-       jtag_add_dr_scan(1, &field, TAP_RTI, NULL);
+       jtag_add_dr_scan(1, &field, TAP_RTI);
        jtag_execute_queue();
        
        status = str9xpec_isc_status(chain_pos);
@@ -269,22 +264,38 @@ int str9xpec_build_block_list(struct flash_bank_s *bank)
        str9xpec_flash_controller_t *str9xpec_info = bank->driver_priv;
        
        int i;
-       int num_sectors = 0, b0_sectors = 0;
-               
+       int num_sectors;
+       int b0_sectors = 0, b1_sectors = 0;
+       u32 offset = 0;
+       int b1_size = 0x2000;
+       
        switch (bank->size)
        {
-               case 256 * 1024:
+               case (256 * 1024):
                        b0_sectors = 4;
                        break;
-               case 512 * 1024:
+               case (512 * 1024):
                        b0_sectors = 8;
                        break;
+               case (1024 * 1024):
+                       b0_sectors = 16;
+                       break;
+               case (2048 * 1024):
+                       b0_sectors = 32;
+                       break;
+               case (128 * 1024):
+                       b1_size = 0x4000;
+                       b1_sectors = 8;
+                       break;
+               case (32 * 1024):
+                       b1_sectors = 4;
+                       break;
                default:
-                       ERROR("BUG: unknown bank->size encountered");
+                       LOG_ERROR("BUG: unknown bank->size encountered");
                        exit(-1);
        }
        
-       num_sectors = b0_sectors + 4;
+       num_sectors = b0_sectors + b1_sectors;
        
        bank->num_sectors = num_sectors;
        bank->sectors = malloc(sizeof(flash_sector_t) * num_sectors);
@@ -294,22 +305,24 @@ int str9xpec_build_block_list(struct flash_bank_s *bank)
        
        for (i = 0; i < b0_sectors; i++)
        {
-               bank->sectors[num_sectors].offset = mem_layout_str9pec[i].sector_start;
-               bank->sectors[num_sectors].size = mem_layout_str9pec[i].sector_size;
+               bank->sectors[num_sectors].offset = offset;
+               bank->sectors[num_sectors].size = 0x10000;
+               offset += bank->sectors[i].size;
                bank->sectors[num_sectors].is_erased = -1;
                bank->sectors[num_sectors].is_protected = 1;
-               str9xpec_info->sector_bits[num_sectors++] = mem_layout_str9pec[i].sector_bit;
+               str9xpec_info->sector_bits[num_sectors++] = i;
        }
-       
-       for (i = 8; i < 12; i++)
+
+       for (i = 0; i < b1_sectors; i++)
        {
-               bank->sectors[num_sectors].offset = mem_layout_str9pec[i].sector_start;
-               bank->sectors[num_sectors].size = mem_layout_str9pec[i].sector_size;
+               bank->sectors[num_sectors].offset = offset;
+               bank->sectors[num_sectors].size = b1_size;
+               offset += bank->sectors[i].size;
                bank->sectors[num_sectors].is_erased = -1;
                bank->sectors[num_sectors].is_protected = 1;
-               str9xpec_info->sector_bits[num_sectors++] = mem_layout_str9pec[i].sector_bit;
+               str9xpec_info->sector_bits[num_sectors++] = i + 32;
        }
-       
+               
        return ERROR_OK;
 }
 
@@ -324,19 +337,13 @@ int str9xpec_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, ch
        
        if (argc < 6)
        {
-               WARNING("incomplete flash_bank str9x configuration");
+               LOG_WARNING("incomplete flash_bank str9x configuration");
                return ERROR_FLASH_BANK_INVALID;
        }
        
        str9xpec_info = malloc(sizeof(str9xpec_flash_controller_t));
        bank->driver_priv = str9xpec_info;
        
-       if (bank->base != 0x00000000)
-       {
-               WARNING("overriding flash base address for STR91x device with 0x00000000");
-               bank->base = 0x00000000;
-       }
-
        /* find out jtag position of flash controller
         * it is always after the arm966 core */
        
@@ -378,7 +385,7 @@ int str9xpec_blank_check(struct flash_bank_s *bank, int first, int last)
        
        buffer = calloc(CEIL(64, 8), 1);
 
-       DEBUG("blank check: first_bank: %i, last_bank: %i", first, last);
+       LOG_DEBUG("blank check: first_bank: %i, last_bank: %i", first, last);
        
        for (i = first; i <= last; i++) {
                buf_set_u32(buffer, str9xpec_info->sector_bits[i], 1, 1);
@@ -397,7 +404,7 @@ int str9xpec_blank_check(struct flash_bank_s *bank, int first, int last)
        field.in_handler = NULL;
        field.in_handler_priv = NULL;
        
-       jtag_add_dr_scan(1, &field, TAP_RTI, NULL);
+       jtag_add_dr_scan(1, &field, TAP_RTI);
        jtag_add_sleep(40000);
        
        /* read blank check result */
@@ -411,7 +418,7 @@ int str9xpec_blank_check(struct flash_bank_s *bank, int first, int last)
        field.in_handler = NULL;
        field.in_handler_priv = NULL;
        
-       jtag_add_dr_scan(1, &field, TAP_PI, NULL);
+       jtag_add_dr_scan(1, &field, TAP_PI);
        jtag_execute_queue();
        
        status = str9xpec_isc_status(chain_pos);
@@ -477,7 +484,7 @@ int str9xpec_erase_area(struct flash_bank_s *bank, int first, int last)
        
        buffer = calloc(CEIL(64, 8), 1);
        
-       DEBUG("erase: first_bank: %i, last_bank: %i", first, last);
+       LOG_DEBUG("erase: first_bank: %i, last_bank: %i", first, last);
        
        /* last bank: 0xFF signals a full erase (unlock complete device) */
        /* last bank: 0xFE signals a option byte erase */
@@ -498,7 +505,7 @@ int str9xpec_erase_area(struct flash_bank_s *bank, int first, int last)
                }
        }
        
-       DEBUG("ISC_ERASE");
+       LOG_DEBUG("ISC_ERASE");
        
        /* execute ISC_ERASE command */
        str9xpec_set_instr(chain_pos, ISC_ERASE, TAP_PI);
@@ -513,7 +520,7 @@ int str9xpec_erase_area(struct flash_bank_s *bank, int first, int last)
        field.in_handler = NULL;
        field.in_handler_priv = NULL;
        
-       jtag_add_dr_scan(1, &field, TAP_RTI, NULL);
+       jtag_add_dr_scan(1, &field, TAP_RTI);
        jtag_execute_queue();
        
        jtag_add_sleep(10);
@@ -579,7 +586,7 @@ int str9xpec_lock_device(struct flash_bank_s *bank)
                field.in_handler = NULL;
                field.in_handler_priv = NULL;
                
-               jtag_add_dr_scan(1, &field, -1, NULL);
+               jtag_add_dr_scan(1, &field, -1);
                jtag_execute_queue();
                
        } while(!(status & ISC_STATUS_BUSY));
@@ -610,7 +617,7 @@ int str9xpec_protect(struct flash_bank_s *bank, int set, int first, int last)
        if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
                return ERROR_FLASH_OPERATION_FAILED;
 
-       DEBUG("protect: first_bank: %i, last_bank: %i", first, last);
+       LOG_DEBUG("protect: first_bank: %i, last_bank: %i", first, last);
        
        /* last bank: 0xFF signals a full device protect */
        if (last == 0xFF)
@@ -665,7 +672,7 @@ int str9xpec_set_address(struct flash_bank_s *bank, u8 sector)
        field.in_handler = NULL;
        field.in_handler_priv = NULL;
        
-       jtag_add_dr_scan(1, &field, -1, NULL);
+       jtag_add_dr_scan(1, &field, -1);
                
        return ERROR_OK;
 }
@@ -697,7 +704,7 @@ int str9xpec_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
        
        if (offset & 0x7)
        {
-               WARNING("offset 0x%x breaks required 8-byte alignment", offset);
+               LOG_WARNING("offset 0x%x breaks required 8-byte alignment", offset);
                return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
        }
        
@@ -728,11 +735,11 @@ int str9xpec_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
        if (check_address != offset + count)
                return ERROR_FLASH_DST_OUT_OF_BANK;
 
-       DEBUG("first_sector: %i, last_sector: %i", first_sector, last_sector);
+       LOG_DEBUG("first_sector: %i, last_sector: %i", first_sector, last_sector);
        
        scanbuf = calloc(CEIL(64, 8), 1);
        
-       DEBUG("ISC_PROGRAM");
+       LOG_DEBUG("ISC_PROGRAM");
        
        for (i = first_sector; i <= last_sector; i++)
        {
@@ -754,7 +761,7 @@ int str9xpec_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                        field.in_handler = NULL;
                        field.in_handler_priv = NULL;
                        
-                       jtag_add_dr_scan(1, &field, TAP_RTI, NULL);
+                       jtag_add_dr_scan(1, &field, TAP_RTI);
                        
                        /* small delay before polling */
                        jtag_add_sleep(50);
@@ -772,7 +779,7 @@ int str9xpec_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                                field.in_handler = NULL;
                                field.in_handler_priv = NULL;
                                
-                               jtag_add_dr_scan(1, &field, -1, NULL);
+                               jtag_add_dr_scan(1, &field, -1);
                                jtag_execute_queue();
                                
                                status = buf_get_u32(scanbuf, 0, 8);
@@ -780,10 +787,10 @@ int str9xpec_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                        } while(!(status & ISC_STATUS_BUSY));
                        
                        if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
-                               return ERROR_FLASH_OPERATION_FAILED; 
+                               return ERROR_FLASH_OPERATION_FAILED;
                        
-                       //if ((status & ISC_STATUS_INT_ERROR) != STR9XPEC_ISC_INTFAIL)
-                       //      return ERROR_FLASH_OPERATION_FAILED; 
+                       /if ((status & ISC_STATUS_INT_ERROR) != STR9XPEC_ISC_INTFAIL)
+                               return ERROR_FLASH_OPERATION_FAILED; */
                
                        dwords_remaining--;
                        bytes_written += 8;
@@ -814,7 +821,7 @@ int str9xpec_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                field.in_handler = NULL;
                field.in_handler_priv = NULL;
                
-               jtag_add_dr_scan(1, &field, TAP_RTI, NULL);
+               jtag_add_dr_scan(1, &field, TAP_RTI);
                
                /* small delay before polling */
                jtag_add_sleep(50);
@@ -832,7 +839,7 @@ int str9xpec_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                        field.in_handler = NULL;
                        field.in_handler_priv = NULL;
                        
-                       jtag_add_dr_scan(1, &field, -1, NULL);
+                       jtag_add_dr_scan(1, &field, -1);
                        jtag_execute_queue();
                        
                        status = buf_get_u32(scanbuf, 0, 8);
@@ -842,8 +849,8 @@ int str9xpec_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
                        return ERROR_FLASH_OPERATION_FAILED;
                
-               //if ((status & ISC_STATUS_INT_ERROR) != STR9XPEC_ISC_INTFAIL)
-               //      return ERROR_FLASH_OPERATION_FAILED; 
+               /if ((status & ISC_STATUS_INT_ERROR) != STR9XPEC_ISC_INTFAIL)
+                       return ERROR_FLASH_OPERATION_FAILED; */
        }
 
        free(scanbuf);
@@ -869,8 +876,7 @@ int str9xpec_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd
 
        if (argc < 1)
        {
-               command_print(cmd_ctx, "usage: str9xpec part_id <num>");
-               return ERROR_OK;
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
        
        bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
@@ -897,7 +903,7 @@ int str9xpec_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd
        field.in_handler = NULL;
        field.in_handler_priv = NULL;
        
-       jtag_add_dr_scan(1, &field, TAP_RTI, NULL);
+       jtag_add_dr_scan(1, &field, TAP_RTI);
        jtag_execute_queue();
        
        idcode = buf_get_u32(buffer, 0, 32);
@@ -990,7 +996,10 @@ int str9xpec_write_options(struct flash_bank_s *bank)
        chain_pos = str9xpec_info->chain_pos;
        
        /* erase config options first */
-       str9xpec_erase_area( bank, 0xFE, 0xFE );
+       status = str9xpec_erase_area( bank, 0xFE, 0xFE );
+       
+       if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
+               return status; 
        
        if (!str9xpec_info->isc_enable) {
                str9xpec_isc_enable( bank );
@@ -1019,7 +1028,7 @@ int str9xpec_write_options(struct flash_bank_s *bank)
        field.in_handler = NULL;
        field.in_handler_priv = NULL;
        
-       jtag_add_dr_scan(1, &field, TAP_RTI, NULL);
+       jtag_add_dr_scan(1, &field, TAP_RTI);
        
        /* small delay before polling */
        jtag_add_sleep(50);
@@ -1037,7 +1046,7 @@ int str9xpec_write_options(struct flash_bank_s *bank)
                field.in_handler = NULL;
                field.in_handler_priv = NULL;
                
-               jtag_add_dr_scan(1, &field, -1, NULL);
+               jtag_add_dr_scan(1, &field, -1);
                jtag_execute_queue();
                
        } while(!(status & ISC_STATUS_BUSY));
@@ -1327,6 +1336,7 @@ int str9xpec_handle_flash_disable_turbo_command(struct command_context_s *cmd_ct
        if( str9xpec_info->devarm ) {
                dev0->next = str9xpec_info->devarm;
                jtag_num_devices++;
+               str9xpec_info->devarm = NULL;
        }
        
        return ERROR_OK;