- Replace 'if(' with 'if ('.
[fw/openocd] / src / flash / lpc288x.c
index 3e22dd56cbb930c664aefa25432019d3f5eb0343..e40ec4423b080f89b965e21472100616eaeb90af 100644 (file)
 #include "config.h"
 #endif
 
-#include "replacements.h"
-
 #include "lpc288x.h"
-
-#include "flash.h"
-#include "target.h"
-#include "log.h"
 #include "binarybuffer.h"
-#include "types.h"
 
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
 
 #define LOAD_TIMER_ERASE       0
 #define LOAD_TIMER_WRITE       1
 /* F_CLK_TIME */
 #define FCT_CLK_DIV_MASK    0x0FFF
 
-int lpc288x_register_commands(struct command_context_s *cmd_ctx);
-int lpc288x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
-int lpc288x_erase(struct flash_bank_s *bank, int first, int last);
-int lpc288x_protect(struct flash_bank_s *bank, int set, int first, int last);
-int lpc288x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
-int lpc288x_probe(struct flash_bank_s *bank);
-int lpc288x_auto_probe(struct flash_bank_s *bank);
-int lpc288x_erase_check(struct flash_bank_s *bank);
-int lpc288x_protect_check(struct flash_bank_s *bank);
-int lpc288x_info(struct flash_bank_s *bank, char *buf, int buf_size);
-void lpc288x_set_flash_mode(flash_bank_t *bank, u8 flashplane, int mode);
-u32 lpc288x_wait_status_busy(flash_bank_t *bank, int timeout);
-void lpc288x_load_timer(int erase, struct target_s *target);
-void lpc288x_set_flash_clk(struct flash_bank_s *bank);
-u32 lpc288x_system_ready(struct flash_bank_s *bank);
-int lpc288x_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
+static int lpc288x_register_commands(struct command_context_s *cmd_ctx);
+static int lpc288x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
+static int lpc288x_erase(struct flash_bank_s *bank, int first, int last);
+static int lpc288x_protect(struct flash_bank_s *bank, int set, int first, int last);
+static int lpc288x_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
+static int lpc288x_probe(struct flash_bank_s *bank);
+static int lpc288x_erase_check(struct flash_bank_s *bank);
+static int lpc288x_protect_check(struct flash_bank_s *bank);
+static int lpc288x_info(struct flash_bank_s *bank, char *buf, int buf_size);
+static uint32_t lpc288x_wait_status_busy(flash_bank_t *bank, int timeout);
+static void lpc288x_load_timer(int erase, struct target_s *target);
+static void lpc288x_set_flash_clk(struct flash_bank_s *bank);
+static uint32_t lpc288x_system_ready(struct flash_bank_s *bank);
 
 flash_driver_t lpc288x_flash =
 {
@@ -126,24 +113,23 @@ flash_driver_t lpc288x_flash =
        .info = lpc288x_info
 };
 
-
-int lpc288x_register_commands(struct command_context_s *cmd_ctx)
+static int lpc288x_register_commands(struct command_context_s *cmd_ctx)
 {
        return ERROR_OK;
 }
 
-u32 lpc288x_wait_status_busy(flash_bank_t *bank, int timeout)
+static uint32_t lpc288x_wait_status_busy(flash_bank_t *bank, int timeout)
 {
-       u32 status;
+       uint32_t status;
        target_t *target = bank->target;
        do
        {
-               usleep(1000);
+               alive_sleep(1);
                timeout--;
                target_read_u32(target, F_STAT, &status);
        }while (((status & FS_DONE) == 0) && timeout);
-       
-       if(timeout == 0)
+
+       if (timeout == 0)
        {
                LOG_DEBUG("Timedout!");
                return ERROR_FLASH_OPERATION_FAILED;
@@ -152,36 +138,36 @@ u32 lpc288x_wait_status_busy(flash_bank_t *bank, int timeout)
 }
 
 /* Read device id register and fill in driver info structure */
-int lpc288x_read_part_info(struct flash_bank_s *bank)
+static int lpc288x_read_part_info(struct flash_bank_s *bank)
 {
        lpc288x_flash_bank_t *lpc288x_info = bank->driver_priv;
        target_t *target = bank->target;
-       u32 cidr;
-       
+       uint32_t cidr;
+
        int i = 0;
-       u32 offset;
-       
+       uint32_t offset;
+
        if (lpc288x_info->cidr == 0x0102100A)
                return ERROR_OK; /* already probed, multiple probes may cause memory leak, not allowed */
-               
+
        /* Read and parse chip identification register */
        target_read_u32(target, DBGU_CIDR, &cidr);
-       
+
        if (cidr != 0x0102100A)
        {
-               LOG_WARNING("Cannot identify target as an LPC288X (%08X)",cidr);
+               LOG_WARNING("Cannot identify target as an LPC288X (%08" PRIx32 ")",cidr);
                return ERROR_FLASH_OPERATION_FAILED;
        }
-       
+
        lpc288x_info->cidr = cidr;
        lpc288x_info->sector_size_break = 0x000F0000;
        lpc288x_info->target_name = "LPC288x";
-       
+
        /* setup the sector info... */
        offset = bank->base;
        bank->num_sectors = 23;
        bank->sectors = malloc(sizeof(flash_sector_t) * 23);
-       
+
        for (i = 0; i < 15; i++)
        {
                bank->sectors[i].offset = offset;
@@ -198,33 +184,33 @@ int lpc288x_read_part_info(struct flash_bank_s *bank)
                bank->sectors[i].is_erased = -1;
                bank->sectors[i].is_protected = 1;
        }
-       
+
        return ERROR_OK;
 }
 
-int lpc288x_protect_check(struct flash_bank_s *bank)
+static int lpc288x_protect_check(struct flash_bank_s *bank)
 {
-    return ERROR_OK;
+       return ERROR_OK;
 }
 
 /* flash_bank LPC288x 0 0 0 0 <target#> <cclk> */
-int lpc288x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
+static int lpc288x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
 {
        lpc288x_flash_bank_t *lpc288x_info;
-       
+
        if (argc < 6)
        {
                LOG_WARNING("incomplete flash_bank LPC288x configuration");
                return ERROR_FLASH_BANK_INVALID;
        }
-       
+
        lpc288x_info = malloc(sizeof(lpc288x_flash_bank_t));
        bank->driver_priv = lpc288x_info;
-       
+
        /* part wasn't probed for info yet */
        lpc288x_info->cidr = 0;
        lpc288x_info->cclk = strtoul(args[6], NULL, 0);
-       
+
        return ERROR_OK;
 }
 
@@ -233,9 +219,9 @@ int lpc288x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, cha
  * AHB = 12 MHz ?
  * 12000000/66000 = 182
  * CLK_DIV = 60 ? */
-void lpc288x_set_flash_clk(struct flash_bank_s *bank)
+static void lpc288x_set_flash_clk(struct flash_bank_s *bank)
 {
-       u32 clk_time;
+       uint32_t clk_time;
        lpc288x_flash_bank_t *lpc288x_info = bank->driver_priv;
        clk_time = (lpc288x_info->cclk / 66000) / 3;
        target_write_u32(bank->target, F_CTRL, FC_CS | FC_WEN);
@@ -244,11 +230,11 @@ void lpc288x_set_flash_clk(struct flash_bank_s *bank)
 
 /* AHB tcyc (in ns) 83 ns
  * LOAD_TIMER_ERASE            FPT_TIME        = ((400,000,000 / AHB tcyc (in ns)) - 2) / 512
- *                                                                     = 9412 (9500) (AN10548 9375)
+ *                                                                     = 9412 (9500) (AN10548 9375)
  * LOAD_TIMER_WRITE            FPT_TIME        = ((1,000,000 / AHB tcyc (in ns)) - 2) / 512
- *                                                                     = 23 (75) (AN10548 72 - is this wrong?)
+ *                                                                     = 23 (75) (AN10548 72 - is this wrong?)
  * TODO: Sort out timing calcs ;) */
-void lpc288x_load_timer(int erase, struct target_s *target)
+static void lpc288x_load_timer(int erase, struct target_s *target)
 {
        if (erase == LOAD_TIMER_ERASE)
        {
@@ -260,14 +246,14 @@ void lpc288x_load_timer(int erase, struct target_s *target)
        }
 }
 
-u32 lpc288x_system_ready(struct flash_bank_s *bank)
+static uint32_t lpc288x_system_ready(struct flash_bank_s *bank)
 {
        lpc288x_flash_bank_t *lpc288x_info = bank->driver_priv;
        if (lpc288x_info->cidr == 0)
        {
                return ERROR_FLASH_BANK_NOT_PROBED;
        }
-       
+
        if (bank->target->state != TARGET_HALTED)
        {
                LOG_ERROR("Target not halted");
@@ -276,50 +262,50 @@ u32 lpc288x_system_ready(struct flash_bank_s *bank)
        return ERROR_OK;
 }
 
-int lpc288x_erase_check(struct flash_bank_s *bank)
+static int lpc288x_erase_check(struct flash_bank_s *bank)
 {
-       u32 status = lpc288x_system_ready(bank);        /* probed? halted? */
+       uint32_t status = lpc288x_system_ready(bank);   /* probed? halted? */
        if (status != ERROR_OK)
        {
                LOG_INFO("Processor not halted/not probed");
                return status;
        }
-       
+
        return ERROR_OK;
 }
 
-int lpc288x_erase(struct flash_bank_s *bank, int first, int last)
+static int lpc288x_erase(struct flash_bank_s *bank, int first, int last)
 {
-       u32 status;
+       uint32_t status;
        int sector;
        target_t *target = bank->target;
-       
+
        status = lpc288x_system_ready(bank);    /* probed? halted? */
        if (status != ERROR_OK)
        {
                return status;
        }
-       
+
        if ((first < 0) || (last < first) || (last >= bank->num_sectors))
        {
                LOG_INFO("Bad sector range");
                return ERROR_FLASH_SECTOR_INVALID;
        }
-       
+
        /* Configure the flash controller timing */
        lpc288x_set_flash_clk(bank);
-       
+
        for (sector = first; sector <= last; sector++)
        {
                if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
                {
                        return ERROR_FLASH_OPERATION_FAILED;
                }
-               
+
                lpc288x_load_timer(LOAD_TIMER_ERASE,target);
-               
+
                target_write_u32(target, bank->sectors[sector].offset, 0x00);
-               
+
                target_write_u32(target, F_CTRL, FC_PROG_REQ | FC_PROTECT | FC_CS);
        }
        if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
@@ -329,24 +315,25 @@ int lpc288x_erase(struct flash_bank_s *bank, int first, int last)
        return ERROR_OK;
 }
 
-int lpc288x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
+static int lpc288x_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
 {
-       u8 page_buffer[FLASH_PAGE_SIZE];
-       u32 i, status, source_offset,dest_offset;
+       uint8_t page_buffer[FLASH_PAGE_SIZE];
+       uint32_t status, source_offset,dest_offset;
        target_t *target = bank->target;
-       u32 bytes_remaining = count;
-       u32 first_sector, last_sector, sector, page;
-       
+       uint32_t bytes_remaining = count;
+       uint32_t first_sector, last_sector, sector, page;
+       int i;
+
        /* probed? halted? */
        status = lpc288x_system_ready(bank);
        if (status != ERROR_OK)
        {
                return status;
        }
-       
+
        /* Initialise search indices */
        first_sector = last_sector = 0xffffffff;
-       
+
        /* validate the write range... */
        for (i = 0; i < bank->num_sectors; i++)
        {
@@ -358,7 +345,7 @@ int lpc288x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                        /* all writes must start on a sector boundary... */
                        if (offset % bank->sectors[i].size)
                        {
-                               LOG_INFO("offset 0x%x breaks required alignment 0x%x", offset, bank->sectors[i].size);
+                               LOG_INFO("offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32 "", offset, bank->sectors[i].size);
                                return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
                        }
                }
@@ -369,21 +356,21 @@ int lpc288x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                        last_sector = i;
                }
        }
-       
+
        /* Range check... */
        if (first_sector == 0xffffffff || last_sector == 0xffffffff)
        {
-               LOG_INFO("Range check failed %x %x", offset, count);
+               LOG_INFO("Range check failed %" PRIx32 " %" PRIx32 "", offset, count);
                return ERROR_FLASH_DST_OUT_OF_BANK;
        }
-       
+
        /* Configure the flash controller timing */
        lpc288x_set_flash_clk(bank);
-       
+
        /* initialise the offsets */
        source_offset = 0;
        dest_offset = 0;
-       
+
        for (sector = first_sector; sector <= last_sector; sector++)
        {
                for (page = 0; page < bank->sectors[sector].size / FLASH_PAGE_SIZE; page++)
@@ -404,24 +391,24 @@ int lpc288x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                                count = FLASH_PAGE_SIZE;
                                memcpy(page_buffer, &buffer[source_offset], count);
                        }
-                       
+
                        /* Wait for flash to become ready */
                        if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
                        {
                                return ERROR_FLASH_OPERATION_FAILED;
                        }
-                       
+
                        /* fill flash data latches with 1's */
                        target_write_u32(target, F_CTRL, FC_CS | FC_SET_DATA | FC_WEN | FC_FUNC);
-                       
+
                        target_write_u32(target, F_CTRL, FC_CS | FC_WEN | FC_FUNC);
                        /*would be better to use the clean target_write_buffer() interface but
                         * it seems not to be a LOT slower....
                         * bulk_write_memory() is no quicker :(*/
 #if 1
-                       if (target->type->write_memory(target, offset + dest_offset, 4, 128, page_buffer) != ERROR_OK)
+                       if (target_write_memory(target, offset + dest_offset, 4, 128, page_buffer) != ERROR_OK)
                        {
-                               LOG_ERROR("Write failed s %x p %x", sector, page);
+                               LOG_ERROR("Write failed s %" PRIx32 " p %" PRIx32 "", sector, page);
                                return ERROR_FLASH_OPERATION_FAILED;
                        }
 #else
@@ -434,66 +421,66 @@ int lpc288x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                        dest_offset += FLASH_PAGE_SIZE;
                        source_offset += count;
                        bytes_remaining -= count;
-                       
+
                        lpc288x_load_timer(LOAD_TIMER_WRITE, target);
-                       
+
                        target_write_u32(target, F_CTRL, FC_PROG_REQ | FC_PROTECT | FC_FUNC | FC_CS);
                }
        }
-       
+
        return ERROR_OK;
 }
 
-int lpc288x_probe(struct flash_bank_s *bank)
+static int lpc288x_probe(struct flash_bank_s *bank)
 {
        /* we only deal with LPC2888 so flash config is fixed */
        lpc288x_flash_bank_t *lpc288x_info = bank->driver_priv;
        int retval;
-       
+
        if (lpc288x_info->cidr != 0)
        {
                return ERROR_OK; /* already probed */
        }
-       
+
        if (bank->target->state != TARGET_HALTED)
        {
                LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
-       
+
        retval = lpc288x_read_part_info(bank);
        if (retval != ERROR_OK)
                return retval;
        return ERROR_OK;
 }
 
-int lpc288x_info(struct flash_bank_s *bank, char *buf, int buf_size)
+static int lpc288x_info(struct flash_bank_s *bank, char *buf, int buf_size)
 {
        snprintf(buf, buf_size, "lpc288x flash driver");
        return ERROR_OK;
 }
 
-int lpc288x_protect(struct flash_bank_s *bank, int set, int first, int last)
+static int lpc288x_protect(struct flash_bank_s *bank, int set, int first, int last)
 {
        int lockregion, status;
-       u32 value;
+       uint32_t value;
        target_t *target = bank->target;
-       
+
        /* probed? halted? */
-       status = lpc288x_system_ready(bank);   
+       status = lpc288x_system_ready(bank);
        if (status != ERROR_OK)
        {
                return status;
        }
-       
+
        if ((first < 0) || (last < first) || (last >= bank->num_sectors))
        {
                return ERROR_FLASH_SECTOR_INVALID;
        }
-       
+
        /* Configure the flash controller timing */
-       lpc288x_set_flash_clk(bank);   
-       
+       lpc288x_set_flash_clk(bank);
+
        for (lockregion = first; lockregion <= last; lockregion++)
        {
                if (set)
@@ -509,6 +496,6 @@ int lpc288x_protect(struct flash_bank_s *bank, int set, int first, int last)
                target_write_u32(target, bank->sectors[lockregion].offset, value);
                target_write_u32(target, F_CTRL, FC_LOAD_REQ | FC_PROTECT | FC_WEN | FC_FUNC | FC_CS);
        }
-       
+
        return ERROR_OK;
 }