helper/command: change prototype of command_print/command_print_sameline
[fw/openocd] / src / flash / nor / em357.c
index 412c9def2e39080bbf77e4298c57175f0248dca8..38fb73189aaa5c9c2865d6fd77a3ed40157293fa 100644 (file)
@@ -19,9 +19,7 @@
  *   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.             *
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -88,7 +86,6 @@ struct em357_options {
 
 struct em357_flash_bank {
        struct em357_options option_bytes;
-       struct working_area *write_algorithm;
        int ppage_size;
        int probed;
 };
@@ -107,7 +104,6 @@ FLASH_BANK_COMMAND_HANDLER(em357_flash_bank_command)
        em357_info = malloc(sizeof(struct em357_flash_bank));
        bank->driver_priv = em357_info;
 
-       em357_info->write_algorithm = NULL;
        em357_info->probed = 0;
 
        return ERROR_OK;
@@ -360,6 +356,9 @@ static int em357_erase(struct flash_bank *bank, int first, int last)
        if ((first == 0) && (last == (bank->num_sectors - 1)))
                return em357_mass_erase(bank);
 
+       /* Enable FPEC clock */
+       target_write_u32(target, EM357_FPEC_CLK, 0x00000001);
+
        /* unlock flash registers */
        int retval = target_write_u32(target, EM357_FLASH_KEYR, KEY1);
        if (retval != ERROR_OK)
@@ -454,12 +453,12 @@ static int em357_protect(struct flash_bank *bank, int set, int first, int last)
        return em357_write_options(bank);
 }
 
-static int em357_write_block(struct flash_bank *bank, uint8_t *buffer,
+static int em357_write_block(struct flash_bank *bank, const uint8_t *buffer,
        uint32_t offset, uint32_t count)
 {
-       struct em357_flash_bank *em357_info = bank->driver_priv;
        struct target *target = bank->target;
        uint32_t buffer_size = 16384;
+       struct working_area *write_algorithm;
        struct working_area *source;
        uint32_t address = bank->base + offset;
        struct reg_param reg_params[4];
@@ -497,14 +496,13 @@ static int em357_write_block(struct flash_bank *bank, uint8_t *buffer,
 
        /* flash write code */
        if (target_alloc_working_area(target, sizeof(em357_flash_write_code),
-                       &em357_info->write_algorithm) != ERROR_OK) {
+                       &write_algorithm) != ERROR_OK) {
                LOG_WARNING("no working area available, can't do block memory writes");
                return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
        }
-       ;
 
-       retval = target_write_buffer(target, em357_info->write_algorithm->address,
-                       sizeof(em357_flash_write_code), (uint8_t *)em357_flash_write_code);
+       retval = target_write_buffer(target, write_algorithm->address,
+                       sizeof(em357_flash_write_code), em357_flash_write_code);
        if (retval != ERROR_OK)
                return retval;
 
@@ -512,20 +510,18 @@ static int em357_write_block(struct flash_bank *bank, uint8_t *buffer,
        while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK) {
                buffer_size /= 2;
                if (buffer_size <= 256) {
-                       /* if we already allocated the writing code, but failed to get a
+                       /* we already allocated the writing code, but failed to get a
                         * buffer, free the algorithm */
-                       if (em357_info->write_algorithm)
-                               target_free_working_area(target, em357_info->write_algorithm);
+                       target_free_working_area(target, write_algorithm);
 
                        LOG_WARNING(
                                "no large enough working area available, can't do block memory writes");
                        return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
                }
        }
-       ;
 
        armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
-       armv7m_info.core_mode = ARMV7M_MODE_ANY;
+       armv7m_info.core_mode = ARM_MODE_THREAD;
 
        init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
        init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
@@ -546,7 +542,7 @@ static int em357_write_block(struct flash_bank *bank, uint8_t *buffer,
                buf_set_u32(reg_params[3].value, 0, 32, 0);
 
                retval = target_run_algorithm(target, 0, NULL, 4, reg_params,
-                               em357_info->write_algorithm->address, 0, 10000, &armv7m_info);
+                               write_algorithm->address, 0, 10000, &armv7m_info);
                if (retval != ERROR_OK) {
                        LOG_ERROR("error executing em357 flash write algorithm");
                        break;
@@ -574,7 +570,7 @@ static int em357_write_block(struct flash_bank *bank, uint8_t *buffer,
        }
 
        target_free_working_area(target, source);
-       target_free_working_area(target, em357_info->write_algorithm);
+       target_free_working_area(target, write_algorithm);
 
        destroy_reg_param(&reg_params[0]);
        destroy_reg_param(&reg_params[1]);
@@ -584,7 +580,7 @@ static int em357_write_block(struct flash_bank *bank, uint8_t *buffer,
        return retval;
 }
 
-static int em357_write(struct flash_bank *bank, uint8_t *buffer,
+static int em357_write(struct flash_bank *bank, const uint8_t *buffer,
        uint32_t offset, uint32_t count)
 {
        struct target *target = bank->target;
@@ -612,6 +608,8 @@ static int em357_write(struct flash_bank *bank, uint8_t *buffer,
        if (retval != ERROR_OK)
                return retval;
 
+       target_write_u32(target, EM357_FPEC_CLK, 0x00000001);
+
        /* multiple half words (2-byte) to be programmed? */
        if (words_remaining > 0) {
                /* try using a block write */
@@ -683,14 +681,45 @@ static int em357_probe(struct flash_bank *bank)
 
        em357_info->probed = 0;
 
+       switch (bank->size) {
+               case 0x10000:
+                       /* 64k -- 64 1k pages */
+                       num_pages = 64;
+                       page_size = 1024;
+                       break;
+               case 0x20000:
+                       /* 128k -- 128 1k pages */
+                       num_pages = 128;
+                       page_size = 1024;
+                       break;
+               case 0x30000:
+                       /* 192k -- 96 2k pages */
+                       num_pages = 96;
+                       page_size = 2048;
+                       break;
+               case 0x40000:
+                       /* 256k -- 128 2k pages */
+                       num_pages = 128;
+                       page_size = 2048;
+                       break;
+               case 0x80000:
+                       /* 512k -- 256 2k pages */
+                       num_pages = 256;
+                       page_size = 2048;
+                       break;
+               default:
+                       LOG_WARNING("No size specified for em357 flash driver, assuming 192k!");
+                       num_pages = 96;
+                       page_size = 2048;
+                       break;
+       }
+
        /* Enable FPEC CLK */
        int retval = target_write_u32(target, EM357_FPEC_CLK, 0x00000001);
        if (retval != ERROR_OK)
                return retval;
 
-       page_size = 2048;
        em357_info->ppage_size = 4;
-       num_pages = 96;
 
        LOG_INFO("flash size = %dkbytes", num_pages*page_size/1024);
 
@@ -724,13 +753,6 @@ static int em357_auto_probe(struct flash_bank *bank)
        return em357_probe(bank);
 }
 
-
-static int get_em357_info(struct flash_bank *bank, char *buf, int buf_size)
-{
-       snprintf(buf, buf_size, "em357\n");
-       return ERROR_OK;
-}
-
 COMMAND_HANDLER(em357_handle_lock_command)
 {
        struct target *target = NULL;
@@ -754,7 +776,7 @@ COMMAND_HANDLER(em357_handle_lock_command)
        }
 
        if (em357_erase_options(bank) != ERROR_OK) {
-               command_print(CMD_CTX, "em357 failed to erase options");
+               command_print(CMD, "em357 failed to erase options");
                return ERROR_OK;
        }
 
@@ -762,11 +784,11 @@ COMMAND_HANDLER(em357_handle_lock_command)
        em357_info->option_bytes.RDP = 0;
 
        if (em357_write_options(bank) != ERROR_OK) {
-               command_print(CMD_CTX, "em357 failed to lock device");
+               command_print(CMD, "em357 failed to lock device");
                return ERROR_OK;
        }
 
-       command_print(CMD_CTX, "em357 locked");
+       command_print(CMD, "em357 locked");
 
        return ERROR_OK;
 }
@@ -791,16 +813,16 @@ COMMAND_HANDLER(em357_handle_unlock_command)
        }
 
        if (em357_erase_options(bank) != ERROR_OK) {
-               command_print(CMD_CTX, "em357 failed to unlock device");
+               command_print(CMD, "em357 failed to unlock device");
                return ERROR_OK;
        }
 
        if (em357_write_options(bank) != ERROR_OK) {
-               command_print(CMD_CTX, "em357 failed to lock device");
+               command_print(CMD, "em357 failed to lock device");
                return ERROR_OK;
        }
 
-       command_print(CMD_CTX, "em357 unlocked.\n"
+       command_print(CMD, "em357 unlocked.\n"
                "INFO: a reset or power cycle is required "
                "for the new settings to take effect.");
 
@@ -816,6 +838,9 @@ static int em357_mass_erase(struct flash_bank *bank)
                return ERROR_TARGET_NOT_HALTED;
        }
 
+       /* Make sure the flash clock is on */
+       target_write_u32(target, EM357_FPEC_CLK, 0x00000001);
+
        /* unlock option flash registers */
        int retval = target_write_u32(target, EM357_FLASH_KEYR, KEY1);
        if (retval != ERROR_OK)
@@ -861,9 +886,9 @@ COMMAND_HANDLER(em357_handle_mass_erase_command)
                for (i = 0; i < bank->num_sectors; i++)
                        bank->sectors[i].is_erased = 1;
 
-               command_print(CMD_CTX, "em357 mass erase complete");
+               command_print(CMD, "em357 mass erase complete");
        } else
-               command_print(CMD_CTX, "em357 mass erase failed");
+               command_print(CMD, "em357 mass erase failed");
 
        return retval;
 }
@@ -904,7 +929,7 @@ static const struct command_registration em357_command_handlers[] = {
        COMMAND_REGISTRATION_DONE
 };
 
-struct flash_driver em357_flash = {
+const struct flash_driver em357_flash = {
        .name = "em357",
        .commands = em357_command_handlers,
        .flash_bank_command = em357_flash_bank_command,
@@ -916,5 +941,5 @@ struct flash_driver em357_flash = {
        .auto_probe = em357_auto_probe,
        .erase_check = default_flash_blank_check,
        .protect_check = em357_protect_check,
-       .info = get_em357_info,
+       .free_driver_priv = default_flash_free_driver_priv,
 };