arm_adi_v5: Fix packed and unaligned memory writes
[openocd.git] / src / target / arm_adi_v5.c
index 1572861dc2c2157ad630d3cbcbd033eb9acaff2f..309296acfa6d37342356291c2424d1d1b1bd45bb 100644 (file)
@@ -23,7 +23,7 @@
  *   You should have received a copy of the GNU General Public License     *
  *   along with this program; if not, write to the                         *
  *   Free Software Foundation, Inc.,                                       *
- *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
  ***************************************************************************/
 
 /**
@@ -137,8 +137,9 @@ void dap_ap_select(struct adiv5_dap *dap, uint8_t ap)
 int dap_setup_accessport(struct adiv5_dap *dap, uint32_t csw, uint32_t tar)
 {
        int retval;
+       csw = csw | CSW_DBGSWENABLE | CSW_MASTER_DEBUG | CSW_HPROT |
+               dap->apcsw[dap->ap_current >> 24];
 
-       csw = csw | CSW_DBGSWENABLE | CSW_MASTER_DEBUG | CSW_HPROT;
        if (csw != dap->ap_csw_value) {
                /* LOG_DEBUG("DAP: Set CSW %x",csw); */
                retval = dap_queue_ap_write(dap, AP_REG_CSW, csw);
@@ -260,41 +261,16 @@ int mem_ap_write_atomic_u32(struct adiv5_dap *dap, uint32_t address,
        return dap_run(dap);
 }
 
-/*****************************************************************************
-*                                                                            *
-* mem_ap_write_buf(struct adiv5_dap *dap, uint8_t *buffer, int count, uint32_t address, bool addr_incr) *
-*                                                                            *
-* Write a buffer in target order (little endian)                             *
-*                                                                            *
-*****************************************************************************/
 int mem_ap_write_buf_u32(struct adiv5_dap *dap, const uint8_t *buffer, int count, uint32_t address, bool addr_incr)
 {
        int wcount, blocksize, writecount, errorcount = 0, retval = ERROR_OK;
        uint32_t adr = address;
-       const uint8_t *pBuffer = buffer;
-       uint32_t incr_flag = CSW_ADDRINC_OFF;
+       uint32_t incr_flag = addr_incr ? CSW_ADDRINC_SINGLE : CSW_ADDRINC_OFF;
 
-       count >>= 2;
-       wcount = count;
-
-       /* if we have an unaligned access - reorder data */
-       if (adr & 0x3u) {
-               for (writecount = 0; writecount < count; writecount++) {
-                       int i;
-                       uint32_t outvalue;
-                       memcpy(&outvalue, pBuffer, sizeof(uint32_t));
-
-                       for (i = 0; i < 4; i++) {
-                               *((uint8_t *)pBuffer + (adr & 0x3)) = outvalue;
-                               outvalue >>= 8;
-                               adr++;
-                       }
-                       pBuffer += sizeof(uint32_t);
-               }
-       }
+       wcount = count >> 2;
 
        while (wcount > 0) {
-               /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
+               /* Adjust to write blocks within boundaries aligned to the TAR auto-increment size */
                blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
                if (wcount < blocksize)
                        blocksize = wcount;
@@ -303,27 +279,27 @@ int mem_ap_write_buf_u32(struct adiv5_dap *dap, const uint8_t *buffer, int count
                if (blocksize == 0)
                        blocksize = 1;
 
-               if (addr_incr)
-                       incr_flag = CSW_ADDRINC_SINGLE;
-
                retval = dap_setup_accessport(dap, CSW_32BIT | incr_flag, address);
                if (retval != ERROR_OK)
                        return retval;
 
                for (writecount = 0; writecount < blocksize; writecount++) {
-                       uint32_t tmp;
-                       tmp = buf_get_u32(buffer + 4 * writecount, 0, 32);
-                       retval = dap_queue_ap_write(dap, AP_REG_DRW, tmp);
+                       uint32_t outvalue = 0;
+                       outvalue |= (uint32_t)*buffer++ << 8 * (adr++ & 3);
+                       outvalue |= (uint32_t)*buffer++ << 8 * (adr++ & 3);
+                       outvalue |= (uint32_t)*buffer++ << 8 * (adr++ & 3);
+                       outvalue |= (uint32_t)*buffer++ << 8 * (adr++ & 3);
+
+                       retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
                        if (retval != ERROR_OK)
                                break;
                }
 
                retval = dap_run(dap);
                if (retval == ERROR_OK) {
-                       wcount = wcount - blocksize;
+                       wcount -= blocksize;
                        if (addr_incr)
-                               address = address + 4 * blocksize;
-                       buffer = buffer + 4 * blocksize;
+                               address += 4 * blocksize;
                } else
                        errorcount++;
 
@@ -340,14 +316,14 @@ static int mem_ap_write_buf_packed_u16(struct adiv5_dap *dap,
                const uint8_t *buffer, int count, uint32_t address)
 {
        int retval = ERROR_OK;
-       int wcount, blocksize, writecount, i;
+       int wcount, blocksize, writecount;
 
        wcount = count >> 1;
 
        while (wcount > 0) {
                int nbytes;
 
-               /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
+               /* Adjust to write blocks within boundaries aligned to the TAR auto-increment size */
                blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
 
                if (wcount < blocksize)
@@ -375,18 +351,17 @@ static int mem_ap_write_buf_packed_u16(struct adiv5_dap *dap,
                                        return retval;
                                }
 
-                               address += nbytes >> 1;
+                               address += nbytes;
+                               buffer += nbytes;
                        } else {
-                               uint32_t outvalue;
-                               memcpy(&outvalue, buffer, sizeof(uint32_t));
+                               assert(nbytes == 4);
 
-                               for (i = 0; i < nbytes; i++) {
-                                       *((uint8_t *)buffer + (address & 0x3)) = outvalue;
-                                       outvalue >>= 8;
-                                       address++;
-                               }
+                               uint32_t outvalue = 0;
+                               outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+                               outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+                               outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+                               outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
 
-                               memcpy(&outvalue, buffer, sizeof(uint32_t));
                                retval = dap_queue_ap_write(dap,
                                                AP_REG_DRW, outvalue);
                                if (retval != ERROR_OK)
@@ -401,7 +376,6 @@ static int mem_ap_write_buf_packed_u16(struct adiv5_dap *dap,
                                }
                        }
 
-                       buffer += nbytes >> 1;
                        writecount -= nbytes >> 1;
 
                } while (writecount);
@@ -415,16 +389,18 @@ int mem_ap_write_buf_u16(struct adiv5_dap *dap, const uint8_t *buffer, int count
 {
        int retval = ERROR_OK;
 
-       if (count >= 4)
+       if (dap->packed_transfers && count >= 4)
                return mem_ap_write_buf_packed_u16(dap, buffer, count, address);
 
        while (count > 0) {
                retval = dap_setup_accessport(dap, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
                if (retval != ERROR_OK)
                        return retval;
-               uint16_t svalue;
-               memcpy(&svalue, buffer, sizeof(uint16_t));
-               uint32_t outvalue = (uint32_t)svalue << 8 * (address & 0x3);
+
+               uint32_t outvalue = 0;
+               outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+               outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+
                retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
                if (retval != ERROR_OK)
                        break;
@@ -434,8 +410,6 @@ int mem_ap_write_buf_u16(struct adiv5_dap *dap, const uint8_t *buffer, int count
                        break;
 
                count -= 2;
-               address += 2;
-               buffer += 2;
        }
 
        return retval;
@@ -445,14 +419,14 @@ static int mem_ap_write_buf_packed_u8(struct adiv5_dap *dap,
                const uint8_t *buffer, int count, uint32_t address)
 {
        int retval = ERROR_OK;
-       int wcount, blocksize, writecount, i;
+       int wcount, blocksize, writecount;
 
        wcount = count;
 
        while (wcount > 0) {
                int nbytes;
 
-               /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
+               /* Adjust to write blocks within boundaries aligned to the TAR auto-increment size */
                blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
 
                if (wcount < blocksize)
@@ -476,17 +450,16 @@ static int mem_ap_write_buf_packed_u8(struct adiv5_dap *dap,
                                }
 
                                address += nbytes;
+                               buffer += nbytes;
                        } else {
-                               uint32_t outvalue;
-                               memcpy(&outvalue, buffer, sizeof(uint32_t));
+                               assert(nbytes == 4);
 
-                               for (i = 0; i < nbytes; i++) {
-                                       *((uint8_t *)buffer + (address & 0x3)) = outvalue;
-                                       outvalue >>= 8;
-                                       address++;
-                               }
+                               uint32_t outvalue = 0;
+                               outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+                               outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+                               outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
+                               outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
 
-                               memcpy(&outvalue, buffer, sizeof(uint32_t));
                                retval = dap_queue_ap_write(dap,
                                                AP_REG_DRW, outvalue);
                                if (retval != ERROR_OK)
@@ -501,7 +474,6 @@ static int mem_ap_write_buf_packed_u8(struct adiv5_dap *dap,
                                }
                        }
 
-                       buffer += nbytes;
                        writecount -= nbytes;
 
                } while (writecount);
@@ -515,14 +487,14 @@ int mem_ap_write_buf_u8(struct adiv5_dap *dap, const uint8_t *buffer, int count,
 {
        int retval = ERROR_OK;
 
-       if (count >= 4)
+       if (dap->packed_transfers && count >= 4)
                return mem_ap_write_buf_packed_u8(dap, buffer, count, address);
 
        while (count > 0) {
                retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
                if (retval != ERROR_OK)
                        return retval;
-               uint32_t outvalue = (uint32_t)*buffer << 8 * (address & 0x3);
+               uint32_t outvalue = (uint32_t)*buffer++ << 8 * (address++ & 0x3);
                retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
                if (retval != ERROR_OK)
                        break;
@@ -532,20 +504,11 @@ int mem_ap_write_buf_u8(struct adiv5_dap *dap, const uint8_t *buffer, int count,
                        break;
 
                count--;
-               address++;
-               buffer++;
        }
 
        return retval;
 }
 
-/* FIXME don't import ... this is a temporary workaround for the
- * mem_ap_read_buf_u32() mess, until it's no longer JTAG-specific.
- */
-extern int adi_jtag_dp_scan(struct adiv5_dap *dap,
-               uint8_t instr, uint8_t reg_addr, uint8_t RnW,
-               uint8_t *outvalue, uint8_t *invalue, uint8_t *ack);
-
 /**
  * Synchronously read a block of 32-bit words into a buffer
  * @param dap The DAP connected to the MEM-AP.
@@ -588,38 +551,7 @@ int mem_ap_read_buf_u32(struct adiv5_dap *dap, uint8_t *buffer,
                if (retval != ERROR_OK)
                        return retval;
 
-               /* FIXME remove these three calls to adi_jtag_dp_scan(),
-                * so this routine becomes transport-neutral.  Be careful
-                * not to cause performance problems with JTAG; would it
-                * suffice to loop over dap_queue_ap_read(), or would that
-                * be slower when JTAG is the chosen transport?
-                */
-
-               /* Scan out first read */
-               retval = adi_jtag_dp_scan(dap, JTAG_DP_APACC, AP_REG_DRW,
-                               DPAP_READ, 0, NULL, NULL);
-               if (retval != ERROR_OK)
-                       return retval;
-               for (readcount = 0; readcount < blocksize - 1; readcount++) {
-                       /* Scan out next read; scan in posted value for the
-                        * previous one.  Assumes read is acked "OK/FAULT",
-                        * and CTRL_STAT says that meant "OK".
-                        */
-                       retval = adi_jtag_dp_scan(dap, JTAG_DP_APACC, AP_REG_DRW,
-                                       DPAP_READ, 0, buffer + 4 * readcount,
-                                       &dap->ack);
-                       if (retval != ERROR_OK)
-                               return retval;
-               }
-
-               /* Scan in last posted value; RDBUFF has no other effect,
-                * assuming ack is OK/FAULT and CTRL_STAT says "OK".
-                */
-               retval = adi_jtag_dp_scan(dap, JTAG_DP_DPACC, DP_RDBUFF,
-                               DPAP_READ, 0, buffer + 4 * readcount,
-                               &dap->ack);
-               if (retval != ERROR_OK)
-                       return retval;
+               retval = dap_queue_ap_read_block(dap, AP_REG_DRW, blocksize, buffer);
 
                retval = dap_run(dap);
                if (retval != ERROR_OK) {
@@ -722,7 +654,7 @@ int mem_ap_read_buf_u16(struct adiv5_dap *dap, uint8_t *buffer,
        uint32_t invalue, i;
        int retval = ERROR_OK;
 
-       if (count >= 4)
+       if (dap->packed_transfers && count >= 4)
                return mem_ap_read_buf_packed_u16(dap, buffer, count, address);
 
        while (count > 0) {
@@ -824,7 +756,7 @@ int mem_ap_read_buf_u8(struct adiv5_dap *dap, uint8_t *buffer,
        uint32_t invalue;
        int retval = ERROR_OK;
 
-       if (count >= 4)
+       if (dap->packed_transfers && count >= 4)
                return mem_ap_read_buf_packed_u8(dap, buffer, count, address);
 
        while (count > 0) {
@@ -1201,6 +1133,29 @@ int ahbap_debugport_init(struct adiv5_dap *dap)
 
        dap_syssec(dap);
 
+       /* check that we support packed transfers */
+       uint32_t csw;
+
+       retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_PACKED, 0);
+       if (retval != ERROR_OK)
+               return retval;
+
+       retval = dap_queue_ap_read(dap, AP_REG_CSW, &csw);
+       if (retval != ERROR_OK)
+               return retval;
+
+       retval = dap_run(dap);
+       if (retval != ERROR_OK)
+               return retval;
+
+       if (csw & CSW_ADDRINC_PACKED)
+               dap->packed_transfers = true;
+       else
+               dap->packed_transfers = false;
+
+       LOG_DEBUG("MEM_AP Packed Transfers: %s",
+                       dap->packed_transfers ? "enabled" : "disabled");
+
        return ERROR_OK;
 }
 
@@ -1885,6 +1840,39 @@ COMMAND_HANDLER(dap_apsel_command)
        return retval;
 }
 
+COMMAND_HANDLER(dap_apcsw_command)
+{
+       struct target *target = get_current_target(CMD_CTX);
+       struct arm *arm = target_to_arm(target);
+       struct adiv5_dap *dap = arm->dap;
+
+       uint32_t apcsw = dap->apcsw[dap->apsel], sprot = 0;
+
+       switch (CMD_ARGC) {
+       case 0:
+               command_print(CMD_CTX, "apsel %" PRIi32 " selected, csw 0x%8.8" PRIx32,
+                       (dap->apsel), apcsw);
+               break;
+       case 1:
+               COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], sprot);
+               /* AP address is in bits 31:24 of DP_SELECT */
+               if (sprot > 1)
+                       return ERROR_COMMAND_SYNTAX_ERROR;
+               if (sprot)
+                       apcsw |= CSW_SPROT;
+               else
+                       apcsw &= ~CSW_SPROT;
+               break;
+       default:
+               return ERROR_COMMAND_SYNTAX_ERROR;
+       }
+       dap->apcsw[dap->apsel] = apcsw;
+
+       return 0;
+}
+
+
+
 COMMAND_HANDLER(dap_apid_command)
 {
        struct target *target = get_current_target(CMD_CTX);
@@ -1939,6 +1927,14 @@ static const struct command_registration dap_commands[] = {
                        "and display the result",
                .usage = "[ap_num]",
        },
+       {
+               .name = "apcsw",
+               .handler = dap_apcsw_command,
+               .mode = COMMAND_EXEC,
+               .help = "Set csw access bit ",
+               .usage = "[sprot]",
+       },
+
        {
                .name = "apid",
                .handler = dap_apid_command,

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)