target/adi_v5_swd: improve error check while updating DP_SELECT 80/4980/4
authorAntonio Borneo <borneo.antonio@gmail.com>
Thu, 28 Feb 2019 11:29:49 +0000 (12:29 +0100)
committerTomas Vanek <vanekt@fbl.cz>
Thu, 14 Mar 2019 14:17:48 +0000 (14:17 +0000)
Write to register DP_SELECT can fail, but both functions
swd_queue_dp_bankselect() and swd_queue_ap_bankselect() return
void and do not propagate the error.

Change the return type of the two functions to int and check the
returned value.
Invalidate the cached value of DP_SELECT if the write fails.

Change-Id: Iba6ef8b0c2332e7f7efb66337d558fb7a4a0d39c
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: http://openocd.zylin.com/4980
Tested-by: jenkins
Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
src/target/adi_v5_swd.c

index d099f4e..d92e296 100644 (file)
@@ -166,22 +166,26 @@ static int swd_queue_ap_abort(struct adiv5_dap *dap, uint8_t *ack)
 }
 
 /** Select the DP register bank matching bits 7:4 of reg. */
-static void swd_queue_dp_bankselect(struct adiv5_dap *dap, unsigned reg)
+static int swd_queue_dp_bankselect(struct adiv5_dap *dap, unsigned reg)
 {
        /* Only register address 4 is banked. */
        if ((reg & 0xf) != 4)
-               return;
+               return ERROR_OK;
 
        uint32_t select_dp_bank = (reg & 0x000000F0) >> 4;
        uint32_t sel = select_dp_bank
                        | (dap->select & (DP_SELECT_APSEL | DP_SELECT_APBANK));
 
        if (sel == dap->select)
-               return;
+               return ERROR_OK;
 
        dap->select = sel;
 
-       swd_queue_dp_write(dap, DP_SELECT, sel);
+       int retval = swd_queue_dp_write(dap, DP_SELECT, sel);
+       if (retval != ERROR_OK)
+               dap->select = DP_SELECT_INVALID;
+
+       return retval;
 }
 
 static int swd_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
@@ -194,7 +198,10 @@ static int swd_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
        if (retval != ERROR_OK)
                return retval;
 
-       swd_queue_dp_bankselect(dap, reg);
+       retval = swd_queue_dp_bankselect(dap, reg);
+       if (retval != ERROR_OK)
+               return retval;
+
        swd->read_reg(swd_cmd(true,  false, reg), data, 0);
 
        return check_sync(dap);
@@ -211,17 +218,29 @@ static int swd_queue_dp_write(struct adiv5_dap *dap, unsigned reg,
                return retval;
 
        swd_finish_read(dap);
-       if (reg == DP_SELECT)
+       if (reg == DP_SELECT) {
                dap->select = data & (DP_SELECT_APSEL | DP_SELECT_APBANK | DP_SELECT_DPBANK);
-       else
-               swd_queue_dp_bankselect(dap, reg);
+
+               swd->write_reg(swd_cmd(false,  false, reg), data, 0);
+
+               retval = check_sync(dap);
+               if (retval != ERROR_OK)
+                       dap->select = DP_SELECT_INVALID;
+
+               return retval;
+       }
+
+       retval = swd_queue_dp_bankselect(dap, reg);
+       if (retval != ERROR_OK)
+               return retval;
+
        swd->write_reg(swd_cmd(false,  false, reg), data, 0);
 
        return check_sync(dap);
 }
 
 /** Select the AP register bank matching bits 7:4 of reg. */
-static void swd_queue_ap_bankselect(struct adiv5_ap *ap, unsigned reg)
+static int swd_queue_ap_bankselect(struct adiv5_ap *ap, unsigned reg)
 {
        struct adiv5_dap *dap = ap->dap;
        uint32_t sel = ((uint32_t)ap->ap_num << 24)
@@ -229,11 +248,15 @@ static void swd_queue_ap_bankselect(struct adiv5_ap *ap, unsigned reg)
                        | (dap->select & DP_SELECT_DPBANK);
 
        if (sel == dap->select)
-               return;
+               return ERROR_OK;
 
        dap->select = sel;
 
-       swd_queue_dp_write(dap, DP_SELECT, sel);
+       int retval = swd_queue_dp_write(dap, DP_SELECT, sel);
+       if (retval != ERROR_OK)
+               dap->select = DP_SELECT_INVALID;
+
+       return retval;
 }
 
 static int swd_queue_ap_read(struct adiv5_ap *ap, unsigned reg,
@@ -247,7 +270,10 @@ static int swd_queue_ap_read(struct adiv5_ap *ap, unsigned reg,
        if (retval != ERROR_OK)
                return retval;
 
-       swd_queue_ap_bankselect(ap, reg);
+       retval = swd_queue_ap_bankselect(ap, reg);
+       if (retval != ERROR_OK)
+               return retval;
+
        swd->read_reg(swd_cmd(true,  true, reg), dap->last_read, ap->memaccess_tck);
        dap->last_read = data;
 
@@ -266,7 +292,10 @@ static int swd_queue_ap_write(struct adiv5_ap *ap, unsigned reg,
                return retval;
 
        swd_finish_read(dap);
-       swd_queue_ap_bankselect(ap, reg);
+       retval = swd_queue_ap_bankselect(ap, reg);
+       if (retval != ERROR_OK)
+               return retval;
+
        swd->write_reg(swd_cmd(false,  true, reg), data, ap->memaccess_tck);
 
        return check_sync(dap);