jtag_vpi: ensured constant packet size & endianness
[fw/openocd] / src / flash / nor / ath79.c
index 451e843658b127546723cc34407d04760d87bccd..c551f27223512301ae8a8a5b7cddb6bff2247d82 100644 (file)
@@ -522,6 +522,9 @@ static int ath79_erase(struct flash_bank *bank, int first, int last)
                return ERROR_FLASH_BANK_NOT_PROBED;
        }
 
+       if (ath79_info->dev->erase_cmd == 0x00)
+               return ERROR_FLASH_OPER_UNSUPPORTED;
+
        for (sector = first; sector <= last; sector++) {
                if (bank->sectors[sector].is_protected) {
                        LOG_ERROR("Flash sector %d protected", sector);
@@ -560,7 +563,11 @@ static int ath79_write_page(struct flash_bank *bank, const uint8_t *buffer,
                address,
        };
        int retval;
-       uint32_t i;
+       uint32_t i, pagesize;
+
+       /* if no write pagesize, use reasonable default */
+       pagesize = ath79_info->dev->pagesize ?
+               ath79_info->dev->pagesize : SPIFLASH_DEF_PAGESIZE;
 
        if (address & 0xff) {
                LOG_ERROR("ath79_write_page: unaligned write address: %08x",
@@ -573,7 +580,7 @@ static int ath79_write_page(struct flash_bank *bank, const uint8_t *buffer,
        }
        if (len > ath79_info->dev->pagesize) {
                LOG_ERROR("ath79_write_page: len bigger than page size %d: %d",
-                         ath79_info->dev->pagesize, len);
+                       pagesize, len);
                return ERROR_FAIL;
        }
 
@@ -611,12 +618,16 @@ static int ath79_write_buffer(struct flash_bank *bank, const uint8_t *buffer,
                              uint32_t address, uint32_t len)
 {
        struct ath79_flash_bank *ath79_info = bank->driver_priv;
-       const uint32_t page_size = ath79_info->dev->pagesize;
+       uint32_t page_size;
        int retval;
 
        LOG_DEBUG("%s: address=0x%08" PRIx32 " len=0x%08" PRIx32,
                  __func__, address, len);
 
+       /* if no valid page_size, use reasonable default */
+       page_size = ath79_info->dev->pagesize ?
+               ath79_info->dev->pagesize : SPIFLASH_DEF_PAGESIZE;
+
        while (len > 0) {
                int page_len = len > page_size ? page_size : len;
 
@@ -642,13 +653,6 @@ static int ath79_write(struct flash_bank *bank, const uint8_t *buffer,
        LOG_DEBUG("%s: offset=0x%08" PRIx32 " count=0x%08" PRIx32,
                  __func__, offset, count);
 
-       if (offset < bank->base || offset >= bank->base + bank->size) {
-               LOG_ERROR("Start address out of range");
-               return ERROR_FAIL;
-       }
-
-       offset -= bank->base;
-
        if (target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
@@ -718,13 +722,6 @@ static int ath79_read(struct flash_bank *bank, uint8_t *buffer,
        LOG_DEBUG("%s: offset=0x%08" PRIx32 " count=0x%08" PRIx32,
                  __func__, offset, count);
 
-       if (offset < bank->base || offset >= bank->base + bank->size) {
-               LOG_ERROR("Start address out of range");
-               return ERROR_FAIL;
-       }
-
-       offset -= bank->base;
-
        if (target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
@@ -775,6 +772,7 @@ static int ath79_probe(struct flash_bank *bank)
        struct ath79_flash_bank *ath79_info = bank->driver_priv;
        struct flash_sector *sectors;
        uint32_t id = 0; /* silence uninitialized warning */
+       uint32_t pagesize, sectorsize;
        const struct ath79_target *target_device;
        int retval;
 
@@ -796,7 +794,7 @@ static int ath79_probe(struct flash_bank *bank)
 
        ath79_info->io_base = target_device->io_base;
 
-       LOG_DEBUG("Found device %s at address 0x%" PRIx32,
+       LOG_DEBUG("Found device %s at address " TARGET_ADDR_FMT,
                  target_device->name, bank->base);
 
        retval = read_flash_id(bank, &id);
@@ -820,16 +818,27 @@ static int ath79_probe(struct flash_bank *bank)
 
        /* Set correct size value */
        bank->size = ath79_info->dev->size_in_bytes;
+       if (bank->size <= (1UL << 16))
+               LOG_WARNING("device needs 2-byte addresses - not implemented");
+       if (bank->size > (1UL << 24))
+               LOG_WARNING("device needs paging or 4-byte addresses - not implemented");
+
+       /* if no sectors, treat whole bank as single sector */
+       sectorsize = ath79_info->dev->sectorsize ?
+               ath79_info->dev->sectorsize : ath79_info->dev->size_in_bytes;
 
        /* create and fill sectors array */
-       bank->num_sectors =
-               ath79_info->dev->size_in_bytes / ath79_info->dev->sectorsize;
+       bank->num_sectors = ath79_info->dev->size_in_bytes / sectorsize;
        sectors = calloc(1, sizeof(struct flash_sector) * bank->num_sectors);
        if (!sectors) {
                LOG_ERROR("not enough memory");
                return ERROR_FAIL;
        }
-       ath79_info->spi.page_buf = malloc(ath79_info->dev->pagesize);
+
+       /* if no write pagesize, use reasonable default */
+       pagesize = ath79_info->dev->pagesize ? ath79_info->dev->pagesize : SPIFLASH_DEF_PAGESIZE;
+
+       ath79_info->spi.page_buf = malloc(pagesize);
        if (!ath79_info->spi.page_buf) {
                LOG_ERROR("not enough memory");
                free(sectors);
@@ -837,8 +846,8 @@ static int ath79_probe(struct flash_bank *bank)
        }
 
        for (int sector = 0; sector < bank->num_sectors; sector++) {
-               sectors[sector].offset = sector * ath79_info->dev->sectorsize;
-               sectors[sector].size = ath79_info->dev->sectorsize;
+               sectors[sector].offset = sector * sectorsize;
+               sectors[sector].size = sectorsize;
                sectors[sector].is_erased = 0;
                sectors[sector].is_protected = 1;
        }
@@ -886,7 +895,7 @@ static int get_ath79_info(struct flash_bank *bank, char *buf, int buf_size)
        return ERROR_OK;
 }
 
-struct flash_driver ath79_flash = {
+const struct flash_driver ath79_flash = {
        .name = "ath79",
        .flash_bank_command = ath79_flash_bank_command,
        .erase = ath79_erase,
@@ -898,4 +907,5 @@ struct flash_driver ath79_flash = {
        .erase_check = ath79_flash_blank_check,
        .protect_check = ath79_protect_check,
        .info = get_ath79_info,
+       .free_driver_priv = default_flash_free_driver_priv,
 };