target_t -> struct target
[fw/openocd] / src / flash / lpc288x.c
index e40ec4423b080f89b965e21472100616eaeb90af..dbd0dab51c1f447b4d48a3208cf9c8f52644b819 100644 (file)
 /* F_CLK_TIME */
 #define FCT_CLK_DIV_MASK    0x0FFF
 
-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 uint32_t lpc288x_wait_status_busy(struct flash_bank_s *bank, int timeout);
+static void lpc288x_load_timer(int erase, struct target *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 =
-{
-       .name = "lpc288x",
-       .register_commands = lpc288x_register_commands,
-       .flash_bank_command = lpc288x_flash_bank_command,
-       .erase = lpc288x_erase,
-       .protect = lpc288x_protect,
-       .write = lpc288x_write,
-       .probe = lpc288x_probe,
-       .auto_probe = lpc288x_probe,
-       .erase_check = lpc288x_erase_check,
-       .protect_check = lpc288x_protect_check,
-       .info = lpc288x_info
-};
-
-static int lpc288x_register_commands(struct command_context_s *cmd_ctx)
-{
-       return ERROR_OK;
-}
-
 static uint32_t lpc288x_wait_status_busy(flash_bank_t *bank, int timeout)
 {
        uint32_t status;
-       target_t *target = bank->target;
+       struct target *target = bank->target;
        do
        {
                alive_sleep(1);
                timeout--;
                target_read_u32(target, F_STAT, &status);
-       }while (((status & FS_DONE) == 0) && timeout);
+       } while (((status & FS_DONE) == 0) && timeout);
 
        if (timeout == 0)
        {
@@ -140,8 +111,8 @@ static uint32_t lpc288x_wait_status_busy(flash_bank_t *bank, int timeout)
 /* Read device id register and fill in driver info structure */
 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;
+       struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
+       struct target *target = bank->target;
        uint32_t cidr;
 
        int i = 0;
@@ -166,7 +137,7 @@ static int lpc288x_read_part_info(struct flash_bank_s *bank)
        /* setup the sector info... */
        offset = bank->base;
        bank->num_sectors = 23;
-       bank->sectors = malloc(sizeof(flash_sector_t) * 23);
+       bank->sectors = malloc(sizeof(struct flash_sector) * 23);
 
        for (i = 0; i < 15; i++)
        {
@@ -194,9 +165,9 @@ static int lpc288x_protect_check(struct flash_bank_s *bank)
 }
 
 /* flash_bank LPC288x 0 0 0 0 <target#> <cclk> */
-static int lpc288x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
+FLASH_BANK_COMMAND_HANDLER(lpc288x_flash_bank_command)
 {
-       lpc288x_flash_bank_t *lpc288x_info;
+       struct lpc288x_flash_bank *lpc288x_info;
 
        if (argc < 6)
        {
@@ -204,25 +175,25 @@ static int lpc288x_flash_bank_command(struct command_context_s *cmd_ctx, char *c
                return ERROR_FLASH_BANK_INVALID;
        }
 
-       lpc288x_info = malloc(sizeof(lpc288x_flash_bank_t));
+       lpc288x_info = malloc(sizeof(struct lpc288x_flash_bank));
        bank->driver_priv = lpc288x_info;
 
        /* part wasn't probed for info yet */
        lpc288x_info->cidr = 0;
-       lpc288x_info->cclk = strtoul(args[6], NULL, 0);
+       COMMAND_PARSE_NUMBER(u32, args[6], lpc288x_info->cclk);
 
        return ERROR_OK;
 }
 
-/* The frequency is the AHB clock frequency divided by (CLK_DIV ×3) + 1.
- * This must be programmed such that the Flash Programming clock frequency is 66 kHz ± 20%.
+/* The frequency is the AHB clock frequency divided by (CLK_DIV ×3) + 1.
+ * This must be programmed such that the Flash Programming clock frequency is 66 kHz Â± 20%.
  * AHB = 12 MHz ?
  * 12000000/66000 = 182
  * CLK_DIV = 60 ? */
 static void lpc288x_set_flash_clk(struct flash_bank_s *bank)
 {
        uint32_t clk_time;
-       lpc288x_flash_bank_t *lpc288x_info = bank->driver_priv;
+       struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
        clk_time = (lpc288x_info->cclk / 66000) / 3;
        target_write_u32(bank->target, F_CTRL, FC_CS | FC_WEN);
        target_write_u32(bank->target, F_CLK_TIME, clk_time);
@@ -234,7 +205,7 @@ static void lpc288x_set_flash_clk(struct flash_bank_s *bank)
  * LOAD_TIMER_WRITE            FPT_TIME        = ((1,000,000 / AHB tcyc (in ns)) - 2) / 512
  *                                                                     = 23 (75) (AN10548 72 - is this wrong?)
  * TODO: Sort out timing calcs ;) */
-static void lpc288x_load_timer(int erase, struct target_s *target)
+static void lpc288x_load_timer(int erase, struct target *target)
 {
        if (erase == LOAD_TIMER_ERASE)
        {
@@ -248,7 +219,7 @@ static void lpc288x_load_timer(int erase, struct target_s *target)
 
 static uint32_t lpc288x_system_ready(struct flash_bank_s *bank)
 {
-       lpc288x_flash_bank_t *lpc288x_info = bank->driver_priv;
+       struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
        if (lpc288x_info->cidr == 0)
        {
                return ERROR_FLASH_BANK_NOT_PROBED;
@@ -278,7 +249,7 @@ static int lpc288x_erase(struct flash_bank_s *bank, int first, int last)
 {
        uint32_t status;
        int sector;
-       target_t *target = bank->target;
+       struct target *target = bank->target;
 
        status = lpc288x_system_ready(bank);    /* probed? halted? */
        if (status != ERROR_OK)
@@ -319,7 +290,7 @@ static int lpc288x_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t of
 {
        uint8_t page_buffer[FLASH_PAGE_SIZE];
        uint32_t status, source_offset,dest_offset;
-       target_t *target = bank->target;
+       struct target *target = bank->target;
        uint32_t bytes_remaining = count;
        uint32_t first_sector, last_sector, sector, page;
        int i;
@@ -434,7 +405,7 @@ static int lpc288x_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t of
 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;
+       struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
        int retval;
 
        if (lpc288x_info->cidr != 0)
@@ -464,7 +435,7 @@ static int lpc288x_protect(struct flash_bank_s *bank, int set, int first, int la
 {
        int lockregion, status;
        uint32_t value;
-       target_t *target = bank->target;
+       struct target *target = bank->target;
 
        /* probed? halted? */
        status = lpc288x_system_ready(bank);
@@ -499,3 +470,16 @@ static int lpc288x_protect(struct flash_bank_s *bank, int set, int first, int la
 
        return ERROR_OK;
 }
+
+struct flash_driver lpc288x_flash = {
+               .name = "lpc288x",
+               .flash_bank_command = &lpc288x_flash_bank_command,
+               .erase = &lpc288x_erase,
+               .protect = &lpc288x_protect,
+               .write = &lpc288x_write,
+               .probe = &lpc288x_probe,
+               .auto_probe = &lpc288x_probe,
+               .erase_check = &lpc288x_erase_check,
+               .protect_check = &lpc288x_protect_check,
+               .info = &lpc288x_info,
+       };