flash : Add support for Atmel at91sam4sa16b
[fw/openocd] / src / flash / nor / efm32.c
index 6e2ebce25767e34e2533f7fe007583475c04751c..fa45ea131f9316793cf53d4c5b84e7831c7cac1b 100644 (file)
  *                                                                         *
  *   Copyright (C) 2013 by Roman Dmitrienko                                *
  *   me@iamroman.org                                                       *
- *
+ *                                                                         *
+ *   Copyright (C) 2014 Nemui Trinomius                                    *
+ *   nemuisan_kawausogasuki@live.jp                                        *
+ *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   the Free Software Foundation; either version 2 of the License, or     *
@@ -43,6 +46,9 @@
 #define EFM_FAMILY_ID_TINY_GECKO        73
 #define EFM_FAMILY_ID_LEOPARD_GECKO     74
 #define EFM_FAMILY_ID_WONDER_GECKO      75
+#define EFM_FAMILY_ID_ZERO_GECKO        76
+#define EZR_FAMILY_ID_WONDER_GECKO             120
+#define EZR_FAMILY_ID_LEOPARD_GECKO            121
 
 #define EFM32_FLASH_ERASE_TMO           100
 #define EFM32_FLASH_WDATAREADY_TMO      100
@@ -57,7 +63,7 @@
 #define EFM32_MSC_LOCK_BITS             (EFM32_MSC_INFO_BASE+0x4000)
 #define EFM32_MSC_DEV_INFO              (EFM32_MSC_INFO_BASE+0x8000)
 
-/* PAGE_SIZE is only present in Leopard and Giant Gecko MCUs */
+/* PAGE_SIZE is only present in Leopard, Giant and Wonder Gecko MCUs */
 #define EFM32_MSC_DI_PAGE_SIZE          (EFM32_MSC_DEV_INFO+0x1e7)
 #define EFM32_MSC_DI_FLASH_SZ           (EFM32_MSC_DEV_INFO+0x1f8)
 #define EFM32_MSC_DI_RAM_SZ             (EFM32_MSC_DEV_INFO+0x1fa)
@@ -98,7 +104,7 @@ struct efm32_info {
        uint16_t page_size;
 };
 
-static int efm32x_write(struct flash_bank *bank, uint8_t *buffer,
+static int efm32x_write(struct flash_bank *bank, const uint8_t *buffer,
        uint32_t offset, uint32_t count);
 
 static int efm32x_get_flash_size(struct flash_bank *bank, uint16_t *flash_sz)
@@ -141,9 +147,11 @@ static int efm32x_read_info(struct flash_bank *bank,
        if (((cpuid >> 4) & 0xfff) == 0xc23) {
                /* Cortex M3 device */
        } else if (((cpuid >> 4) & 0xfff) == 0xc24) {
-               /* Cortex M4 device */
+               /* Cortex M4 device(WONDER GECKO) */
+       } else if (((cpuid >> 4) & 0xfff) == 0xc60) {
+               /* Cortex M0plus device(ZERO GECKO) */
        } else {
-               LOG_ERROR("Target is not CortexM3 or M4");
+               LOG_ERROR("Target is not Cortex-Mx Device");
                return ERROR_FAIL;
        }
 
@@ -170,6 +178,8 @@ static int efm32x_read_info(struct flash_bank *bank,
        if (EFM_FAMILY_ID_GECKO == efm32_info->part_family ||
                        EFM_FAMILY_ID_TINY_GECKO == efm32_info->part_family)
                efm32_info->page_size = 512;
+       else if (EFM_FAMILY_ID_ZERO_GECKO == efm32_info->part_family)
+               efm32_info->page_size = 1024;
        else if (EFM_FAMILY_ID_GIANT_GECKO == efm32_info->part_family ||
                        EFM_FAMILY_ID_LEOPARD_GECKO == efm32_info->part_family) {
                if (efm32_info->prod_rev >= 18) {
@@ -194,7 +204,9 @@ static int efm32x_read_info(struct flash_bank *bank,
                        LOG_ERROR("Invalid page size %u", efm32_info->page_size);
                        return ERROR_FAIL;
                }
-       } else if (EFM_FAMILY_ID_WONDER_GECKO == efm32_info->part_family) {
+       } else if (EFM_FAMILY_ID_WONDER_GECKO == efm32_info->part_family ||
+                       EZR_FAMILY_ID_WONDER_GECKO == efm32_info->part_family ||
+                       EZR_FAMILY_ID_LEOPARD_GECKO == efm32_info->part_family) {
                uint8_t pg_size = 0;
                ret = target_read_u8(bank->target, EFM32_MSC_DI_PAGE_SIZE,
                        &pg_size);
@@ -499,7 +511,7 @@ static int efm32x_protect(struct flash_bank *bank, int set, int first, int last)
        return ERROR_OK;
 }
 
-static int efm32x_write_block(struct flash_bank *bank, uint8_t *buf,
+static int efm32x_write_block(struct flash_bank *bank, const uint8_t *buf,
        uint32_t offset, uint32_t count)
 {
        struct target *target = bank->target;
@@ -735,7 +747,7 @@ static int efm32x_write_word(struct flash_bank *bank, uint32_t addr,
        return ERROR_OK;
 }
 
-static int efm32x_write(struct flash_bank *bank, uint8_t *buffer,
+static int efm32x_write(struct flash_bank *bank, const uint8_t *buffer,
                uint32_t offset, uint32_t count)
 {
        struct target *target = bank->target;
@@ -838,11 +850,16 @@ static int efm32x_probe(struct flash_bank *bank)
                        LOG_INFO("Tiny Gecko MCU detected");
                        break;
                case EFM_FAMILY_ID_LEOPARD_GECKO:
+               case EZR_FAMILY_ID_LEOPARD_GECKO:
                        LOG_INFO("Leopard Gecko MCU detected");
                        break;
                case EFM_FAMILY_ID_WONDER_GECKO:
+               case EZR_FAMILY_ID_WONDER_GECKO:
                        LOG_INFO("Wonder Gecko MCU detected");
                        break;
+               case EFM_FAMILY_ID_ZERO_GECKO:
+                       LOG_INFO("Zero Gecko MCU detected");
+                       break;
                default:
                        LOG_ERROR("Unsupported MCU family %d",
                                efm32_mcu_info.part_family);
@@ -933,7 +950,15 @@ static int get_efm32x_info(struct flash_bank *bank, char *buf, int buf_size)
                return ret;
        }
 
-       printed = snprintf(buf, buf_size, "EFM32 ");
+       switch (info.part_family) {
+               case EZR_FAMILY_ID_WONDER_GECKO:
+               case EZR_FAMILY_ID_LEOPARD_GECKO:
+                       printed = snprintf(buf, buf_size, "EZR32 ");
+                       break;
+               default:
+                       printed = snprintf(buf, buf_size, "EFM32 ");
+       }
+
        buf += printed;
        buf_size -= printed;
 
@@ -951,11 +976,16 @@ static int get_efm32x_info(struct flash_bank *bank, char *buf, int buf_size)
                        printed = snprintf(buf, buf_size, "Tiny Gecko");
                        break;
                case EFM_FAMILY_ID_LEOPARD_GECKO:
+               case EZR_FAMILY_ID_LEOPARD_GECKO:
                        printed = snprintf(buf, buf_size, "Leopard Gecko");
                        break;
                case EFM_FAMILY_ID_WONDER_GECKO:
+               case EZR_FAMILY_ID_WONDER_GECKO:
                        printed = snprintf(buf, buf_size, "Wonder Gecko");
                        break;
+               case EFM_FAMILY_ID_ZERO_GECKO:
+                       printed = snprintf(buf, buf_size, "Zero Gecko");
+                       break;
        }
 
        buf += printed;