SPI table updates (some new devices and new info)
[openocd.git] / src / flash / nor / jtagspi.c
index 0ba4dc2..0750fdf 100644 (file)
@@ -32,14 +32,13 @@ struct jtagspi_flash_bank {
        const struct flash_device *dev;
        int probed;
        uint32_t ir;
-       uint32_t dr_len;
 };
 
 FLASH_BANK_COMMAND_HANDLER(jtagspi_flash_bank_command)
 {
        struct jtagspi_flash_bank *info;
 
-       if (CMD_ARGC < 8)
+       if (CMD_ARGC < 7)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
        info = malloc(sizeof(struct jtagspi_flash_bank));
@@ -52,7 +51,6 @@ FLASH_BANK_COMMAND_HANDLER(jtagspi_flash_bank_command)
        info->tap = NULL;
        info->probed = 0;
        COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], info->ir);
-       COMMAND_PARSE_NUMBER(u32, CMD_ARGV[7], info->dr_len);
 
        return ERROR_OK;
 }
@@ -63,9 +61,6 @@ static void jtagspi_set_ir(struct flash_bank *bank)
        struct scan_field field;
        uint8_t buf[4];
 
-       if (buf_get_u32(info->tap->cur_instr, 0, info->tap->ir_length) == info->ir)
-               return;
-
        LOG_DEBUG("loading jtagspi ir");
        buf_set_u32(buf, 0, info->tap->ir_length, info->ir);
        field.num_bits = info->tap->ir_length;
@@ -84,28 +79,53 @@ static int jtagspi_cmd(struct flash_bank *bank, uint8_t cmd,
                uint32_t *addr, uint8_t *data, int len)
 {
        struct jtagspi_flash_bank *info = bank->driver_priv;
-       struct scan_field fields[3];
-       uint8_t cmd_buf[4];
+       struct scan_field fields[6];
+       uint8_t marker = 1;
+       uint8_t xfer_bits_buf[4];
+       uint8_t addr_buf[3];
        uint8_t *data_buf;
+       uint32_t xfer_bits;
        int is_read, lenb, n;
 
        /* LOG_DEBUG("cmd=0x%02x len=%i", cmd, len); */
 
+       is_read = (len < 0);
+       if (is_read)
+               len = -len;
+
        n = 0;
+
+       fields[n].num_bits = 1;
+       fields[n].out_value = &marker;
+       fields[n].in_value = NULL;
+       n++;
+
+       xfer_bits = 8 + len - 1;
+       /* cmd + read/write - 1 due to the counter implementation */
+       if (addr)
+               xfer_bits += 24;
+       h_u32_to_be(xfer_bits_buf, xfer_bits);
+       flip_u8(xfer_bits_buf, xfer_bits_buf, 4);
+       fields[n].num_bits = 32;
+       fields[n].out_value = xfer_bits_buf;
+       fields[n].in_value = NULL;
+       n++;
+
+       cmd = flip_u32(cmd, 8);
        fields[n].num_bits = 8;
-       cmd_buf[0] = cmd;
-       if (addr) {
-               h_u24_to_be(cmd_buf + 1, *addr);
-               fields[n].num_bits += 24;
-       }
-       flip_u8(cmd_buf, cmd_buf, 4);
-       fields[n].out_value = cmd_buf;
+       fields[n].out_value = &cmd;
        fields[n].in_value = NULL;
        n++;
 
-       is_read = (len < 0);
-       if (is_read)
-               len = -len;
+       if (addr) {
+               h_u24_to_be(addr_buf, *addr);
+               flip_u8(addr_buf, addr_buf, 3);
+               fields[n].num_bits = 24;
+               fields[n].out_value = addr_buf;
+               fields[n].in_value = NULL;
+               n++;
+       }
+
        lenb = DIV_ROUND_UP(len, 8);
        data_buf = malloc(lenb);
        if (lenb > 0) {
@@ -114,10 +134,11 @@ static int jtagspi_cmd(struct flash_bank *bank, uint8_t cmd,
                        return ERROR_FAIL;
                }
                if (is_read) {
-                       fields[n].num_bits = info->dr_len;
+                       fields[n].num_bits = jtag_tap_count_enabled();
                        fields[n].out_value = NULL;
                        fields[n].in_value = NULL;
                        n++;
+
                        fields[n].out_value = NULL;
                        fields[n].in_value = data_buf;
                } else {
@@ -130,6 +151,7 @@ static int jtagspi_cmd(struct flash_bank *bank, uint8_t cmd,
        }
 
        jtagspi_set_ir(bank);
+       /* passing from an IR scan to SHIFT-DR clears BYPASS registers */
        jtag_add_dr_scan(info->tap, n, fields, TAP_IDLE);
        jtag_execute_queue();
 
@@ -144,7 +166,7 @@ static int jtagspi_probe(struct flash_bank *bank)
        struct jtagspi_flash_bank *info = bank->driver_priv;
        struct flash_sector *sectors;
        uint8_t in_buf[3];
-       uint32_t id;
+       uint32_t id, sectorsize;
 
        if (info->probed)
                free(bank->sectors);
@@ -177,10 +199,17 @@ static int jtagspi_probe(struct flash_bank *bank)
 
        /* Set correct size value */
        bank->size = 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 = info->dev->sectorsize ?
+               info->dev->sectorsize : info->dev->size_in_bytes;
 
        /* create and fill sectors array */
-       bank->num_sectors =
-               info->dev->size_in_bytes / info->dev->sectorsize;
+       bank->num_sectors = info->dev->size_in_bytes / sectorsize;
        sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
        if (sectors == NULL) {
                LOG_ERROR("not enough memory");
@@ -188,8 +217,8 @@ static int jtagspi_probe(struct flash_bank *bank)
        }
 
        for (int sector = 0; sector < bank->num_sectors; sector++) {
-               sectors[sector].offset = sector * info->dev->sectorsize;
-               sectors[sector].size = info->dev->sectorsize;
+               sectors[sector].offset = sector * sectorsize;
+               sectors[sector].size = sectorsize;
                sectors[sector].is_erased = -1;
                sectors[sector].is_protected = 0;
        }
@@ -202,22 +231,23 @@ static int jtagspi_probe(struct flash_bank *bank)
 static void jtagspi_read_status(struct flash_bank *bank, uint32_t *status)
 {
        uint8_t buf;
-       jtagspi_cmd(bank, SPIFLASH_READ_STATUS, NULL, &buf, -8);
-       *status = buf;
-       /* LOG_DEBUG("status=0x%08" PRIx32, *status); */
+       if (jtagspi_cmd(bank, SPIFLASH_READ_STATUS, NULL, &buf, -8) == ERROR_OK) {
+               *status = buf;
+               /* LOG_DEBUG("status=0x%08" PRIx32, *status); */
+       }
 }
 
 static int jtagspi_wait(struct flash_bank *bank, int timeout_ms)
 {
        uint32_t status;
-       long long t0 = timeval_ms();
-       long long dt;
+       int64_t t0 = timeval_ms();
+       int64_t dt;
 
        do {
                dt = timeval_ms() - t0;
                jtagspi_read_status(bank, &status);
                if ((status & SPIFLASH_BSY_BIT) == 0) {
-                       LOG_DEBUG("waited %lld ms", dt);
+                       LOG_DEBUG("waited %" PRId64 " ms", dt);
                        return ERROR_OK;
                }
                alive_sleep(1);
@@ -244,14 +274,17 @@ static int jtagspi_bulk_erase(struct flash_bank *bank)
 {
        struct jtagspi_flash_bank *info = bank->driver_priv;
        int retval;
-       long long t0 = timeval_ms();
+       int64_t t0 = timeval_ms();
+
+       if (info->dev->chip_erase_cmd == 0x00)
+               return ERROR_FLASH_OPER_UNSUPPORTED;
 
        retval = jtagspi_write_enable(bank);
        if (retval != ERROR_OK)
                return retval;
        jtagspi_cmd(bank, info->dev->chip_erase_cmd, NULL, NULL, 0);
        retval = jtagspi_wait(bank, bank->num_sectors*JTAGSPI_MAX_TIMEOUT);
-       LOG_INFO("took %lld ms", timeval_ms() - t0);
+       LOG_INFO("took %" PRId64 " ms", timeval_ms() - t0);
        return retval;
 }
 
@@ -259,14 +292,14 @@ static int jtagspi_sector_erase(struct flash_bank *bank, int sector)
 {
        struct jtagspi_flash_bank *info = bank->driver_priv;
        int retval;
-       long long t0 = timeval_ms();
+       int64_t t0 = timeval_ms();
 
        retval = jtagspi_write_enable(bank);
        if (retval != ERROR_OK)
                return retval;
        jtagspi_cmd(bank, info->dev->erase_cmd, &bank->sectors[sector].offset, NULL, 0);
        retval = jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT);
-       LOG_INFO("sector %d took %lld ms", sector, timeval_ms() - t0);
+       LOG_INFO("sector %d took %" PRId64 " ms", sector, timeval_ms() - t0);
        return retval;
 }
 
@@ -305,6 +338,9 @@ static int jtagspi_erase(struct flash_bank *bank, int first, int last)
                        LOG_WARNING("Bulk flash erase failed. Falling back to sector erase.");
        }
 
+       if (info->dev->erase_cmd == 0x00)
+               return ERROR_FLASH_OPER_UNSUPPORTED;
+
        for (sector = first; sector <= last; sector++) {
                retval = jtagspi_sector_erase(bank, sector);
                if (retval != ERROR_OK) {
@@ -320,21 +356,11 @@ static int jtagspi_protect(struct flash_bank *bank, int set, int first, int last
 {
        int sector;
 
-       if ((first < 0) || (last < first) || (last >= bank->num_sectors)) {
-               LOG_ERROR("Flash sector invalid");
-               return ERROR_FLASH_SECTOR_INVALID;
-       }
-
        for (sector = first; sector <= last; sector++)
                bank->sectors[sector].is_protected = set;
        return ERROR_OK;
 }
 
-static int jtagspi_protect_check(struct flash_bank *bank)
-{
-       return ERROR_OK;
-}
-
 static int jtagspi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
 {
        struct jtagspi_flash_bank *info = bank->driver_priv;
@@ -363,16 +389,19 @@ static int jtagspi_write(struct flash_bank *bank, const uint8_t *buffer, uint32_
 {
        struct jtagspi_flash_bank *info = bank->driver_priv;
        int retval;
-       uint32_t n;
+       uint32_t n, pagesize;
 
        if (!(info->probed)) {
                LOG_ERROR("Flash bank not yet probed.");
                return ERROR_FLASH_BANK_NOT_PROBED;
        }
 
-       for (n = 0; n < count; n += info->dev->pagesize) {
+       /* if no write pagesize, use reasonable default */
+       pagesize = info->dev->pagesize ? info->dev->pagesize : SPIFLASH_DEF_PAGESIZE;
+
+       for (n = 0; n < count; n += pagesize) {
                retval = jtagspi_page_write(bank, buffer + n, offset + n,
-                               MIN(count - n, info->dev->pagesize));
+                               MIN(count - n, pagesize));
                if (retval != ERROR_OK) {
                        LOG_ERROR("page write error");
                        return retval;
@@ -408,6 +437,6 @@ struct flash_driver jtagspi_flash = {
        .probe = jtagspi_probe,
        .auto_probe = jtagspi_probe,
        .erase_check = default_flash_blank_check,
-       .protect_check = jtagspi_protect_check,
-       .info = jtagspi_info
+       .info = jtagspi_info,
+       .free_driver_priv = default_flash_free_driver_priv,
 };