Ben Bodley TEKNIQUE <ben@teknique.com> - support for the 1Mb Spansion Flash S29AL008D.
[openocd.git] / src / flash / cfi.c
index a4b95f08bd4c19b71bd15803ab3dae9ca7f357aa..3a1e9f1a1928deb677b166472a37ef074cb1eb1b 100644 (file)
@@ -94,6 +94,9 @@ cfi_fixup_t cfi_jedec_fixups[] = {
        {CFI_MFR_AMD, 0x2223, cfi_fixup_non_cfi, NULL},
        {CFI_MFR_AMD, 0x22ab, cfi_fixup_non_cfi, NULL},
        {CFI_MFR_FUJITSU, 0x226b, cfi_fixup_non_cfi, NULL},
+       {CFI_MFR_AMIC, 0xb31a, cfi_fixup_non_cfi, NULL},
+       {CFI_MFR_MX, 0x225b, cfi_fixup_non_cfi, NULL},
+       {CFI_MFR_AMD, 0x225b, cfi_fixup_non_cfi, NULL},
        {0, 0, NULL, NULL}
 };
 
@@ -106,6 +109,9 @@ cfi_fixup_t cfi_0002_fixups[] = {
        {CFI_MFR_SST, 0x2780, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
        {CFI_MFR_ATMEL, 0x00C8, cfi_fixup_atmel_reversed_erase_regions, NULL},
        {CFI_MFR_FUJITSU, 0x226b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
+       {CFI_MFR_AMIC, 0xb31a, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
+       {CFI_MFR_MX, 0x225b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
+       {CFI_MFR_AMD, 0x225b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
        {CFI_MFR_ANY, CFI_ID_ANY, cfi_fixup_0002_erase_regions, NULL},
        {0, 0, NULL, NULL}
 };
@@ -269,7 +275,7 @@ u8 cfi_intel_wait_status_busy(flash_bank_t *bank, int timeout)
        while ((!((status = cfi_get_u8(bank, 0, 0x0)) & 0x80)) && (timeout-- > 0))
        {
                LOG_DEBUG("status: 0x%x", status);
-               usleep(1000);
+               alive_sleep(1);
        }
 
        /* mask out bit 0 (reserved) */
@@ -329,7 +335,7 @@ int cfi_spansion_wait_status_busy(flash_bank_t *bank, int timeout)
                }
 
                oldstatus = status;
-               usleep(1000);
+               alive_sleep(1);
        } while (timeout-- > 0);
 
        LOG_ERROR("timeout, status: 0x%x", status);
@@ -743,6 +749,7 @@ int cfi_erase(struct flash_bank_s *bank, int first, int last)
 
        if (bank->target->state != TARGET_HALTED)
        {
+               LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
 
@@ -874,6 +881,7 @@ int cfi_protect(struct flash_bank_s *bank, int set, int first, int last)
 
        if (bank->target->state != TARGET_HALTED)
        {
+               LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
 
@@ -1589,6 +1597,74 @@ int cfi_spansion_write_word(struct flash_bank_s *bank, u8 *word, u32 address)
        return ERROR_OK;
 }
 
+int cfi_spansion_write_words(struct flash_bank_s *bank, u8 *word, u32 wordcount, u32 address)
+{
+       cfi_flash_bank_t *cfi_info = bank->driver_priv;
+       target_t *target = bank->target;
+       u8 command[8];
+       cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
+
+       /* Calculate buffer size and boundary mask */
+       u32 buffersize = 1UL << cfi_info->max_buf_write_size;
+       u32 buffermask = buffersize-1;
+       u32 bufferwsize;
+
+       /* Check for valid range */
+       if (address & buffermask)
+       {
+               LOG_ERROR("Write address at base 0x%x, address %x not aligned to 2^%d boundary", bank->base, address, cfi_info->max_buf_write_size);
+               return ERROR_FLASH_OPERATION_FAILED;
+       }
+       switch(bank->chip_width)
+       {
+       case 4 : bufferwsize = buffersize / 4; break;
+       case 2 : bufferwsize = buffersize / 2; break;
+       case 1 : bufferwsize = buffersize; break;
+       default:
+               LOG_ERROR("Unsupported chip width %d", bank->chip_width);
+               return ERROR_FLASH_OPERATION_FAILED;
+       }
+
+       /* Check for valid size */
+       if (wordcount > bufferwsize)
+       {
+               LOG_ERROR("Number of data words %d exceeds available buffersize %d", wordcount, buffersize);
+               return ERROR_FLASH_OPERATION_FAILED;
+       }
+
+       // Unlock
+       cfi_command(bank, 0xaa, command);
+       target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command);
+
+       cfi_command(bank, 0x55, command);
+       target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock2), bank->bus_width, 1, command);
+
+       // Buffer load command
+       cfi_command(bank, 0x25, command);
+       target->type->write_memory(target, address, bank->bus_width, 1, command);
+
+       /* Write buffer wordcount-1 and data words */
+       cfi_command(bank, bufferwsize-1, command);
+       target->type->write_memory(target, address, bank->bus_width, 1, command);
+
+       target->type->write_memory(target, address, bank->bus_width, bufferwsize, word);
+
+       /* Commit write operation */
+       cfi_command(bank, 0x29, command);
+       target->type->write_memory(target, address, bank->bus_width, 1, command);
+
+       if (cfi_spansion_wait_status_busy(bank, 1000 * (1 << cfi_info->word_write_timeout_max)) != ERROR_OK)
+       {
+               cfi_command(bank, 0xf0, command);
+               target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
+
+               LOG_ERROR("couldn't write block at base 0x%x, address %x, size %x", bank->base, address, bufferwsize);
+               return ERROR_FLASH_OPERATION_FAILED;
+       }
+
+       return ERROR_OK;
+}
+
 int cfi_write_word(struct flash_bank_s *bank, u8 *word, u32 address)
 {
        cfi_flash_bank_t *cfi_info = bank->driver_priv;
@@ -1621,8 +1697,7 @@ int cfi_write_words(struct flash_bank_s *bank, u8 *word, u32 wordcount, u32 addr
                        return cfi_intel_write_words(bank, word, wordcount, address);
                        break;
                case 2:
-                       /* return cfi_spansion_write_words(bank, word, address); */
-                       LOG_ERROR("cfi primary command set %i unimplemented - FIXME", cfi_info->pri_id);
+                       return cfi_spansion_write_words(bank, word, wordcount, address); 
                        break;
                default:
                        LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
@@ -1645,7 +1720,10 @@ int cfi_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
        int retval;
 
        if (bank->target->state != TARGET_HALTED)
+       {
+               LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
+       }
 
        if (offset + count > bank->size)
                return ERROR_FLASH_DST_OUT_OF_BANK;
@@ -1738,21 +1816,25 @@ int cfi_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                        /* fall back to memory writes */
                        while (count >= bank->bus_width)
                        {
+                               int fallback;
                                if ((write_p & 0xff) == 0)
                                {
                                        LOG_INFO("Programming at %08x, count %08x bytes remaining", write_p, count);
                                }
+                               fallback = 1;
                                if ((bufferwsize > 0) && (count >= buffersize) && !(write_p & buffermask))
                                {
                                        retval = cfi_write_words(bank, buffer, bufferwsize, write_p);
-                                       if (retval != ERROR_OK)
-                                               return retval;
-
-                                       buffer += buffersize;
-                                       write_p += buffersize;
-                                       count -= buffersize;
+                                       if (retval == ERROR_OK)
+                                       {
+                                               buffer += buffersize;
+                                               write_p += buffersize;
+                                               count -= buffersize;
+                                               fallback=0;
+                                       }
                                }
-                               else
+                               /* try the slow way? */
+                               if (fallback)
                                {
                                        for (i = 0; i < bank->bus_width; i++)
                                                current_word[i] = 0;
@@ -1869,6 +1951,7 @@ int cfi_probe(struct flash_bank_s *bank)
 
        if (bank->target->state != TARGET_HALTED)
        {
+               LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
 
@@ -2167,6 +2250,7 @@ int cfi_protect_check(struct flash_bank_s *bank)
 
        if (bank->target->state != TARGET_HALTED)
        {
+               LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
 
@@ -2246,7 +2330,7 @@ int cfi_info(struct flash_bank_s *bank, char *buf, int buf_size)
                printed = snprintf(buf, buf_size, "size: 0x%x, interface desc: %i, max buffer write size: %x\n",
                                   1 << cfi_info->dev_size,
                                   cfi_info->interface_desc,
-                                  cfi_info->max_buf_write_size);
+                                  1 << cfi_info->max_buf_write_size);
        buf += printed;
        buf_size -= printed;
 

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)