flash: nor: ath79: remove base calculation
[openocd.git] / src / flash / nor / ath79.c
index 451e843658b127546723cc34407d04760d87bccd..520f6c5b93732c179ed0ef65ddfe96ccf27a4a63 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;
 
@@ -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;
        }
@@ -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,
 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)