diff --git a/avrdude/jtag3.c b/avrdude/jtag3.c
index 4cda747..5c3a31a 100644
--- a/avrdude/jtag3.c
+++ b/avrdude/jtag3.c
@@ -842,6 +842,9 @@ int jtag3_recv(PROGRAMMER * pgm, unsigned char **msg) {
   if ((c & RSP3_STATUS_MASK) != RSP3_OK) {
     avrdude_message(MSG_INFO, "%s: bad response to %s command: 0x%02x\n",
                     progname, descr, c);
+    if ( (c == RSP3_FAILED) && ((*resp)[3] == RSP3_FAIL_OCD_LOCKED) ) {
+      avrdude_message(MSG_INFO, "Device is locked! A chip erase is needed to gain full access to the device.\n");
+    }
     free(*resp);
     resp = 0;
     return -1;
@@ -895,6 +898,34 @@ static int jtag3_chip_erase(PROGRAMMER * pgm, AVRPART * p)
   return 0;
 }
 
+/*
+ * UPDI 'chip erase' -> 'enter progmode' with chip erase key
+ */
+static int jtag3_chip_erase_updi(PROGRAMMER * pgm, AVRPART * p)
+{
+  unsigned char buf[8], *resp;
+
+  buf[0] = 1; /* Enable */
+  if (jtag3_setparm(pgm, SCOPE_AVR, SET_GET_CTXT_OPTIONS, PARM3_OPT_CHIP_ERASE_TO_ENTER, buf, 1) < 0)
+    return -1;
+
+  buf[0] = SCOPE_AVR;
+  buf[1] = CMD3_ENTER_PROGMODE;
+  buf[2] = 0;
+
+  if (jtag3_command(pgm, buf, 3, &resp, "enter progmode") < 0)
+    return -1;
+  PDATA(pgm)->prog_enabled = 1;
+
+  buf[0] = 0; /* Disable */
+  if (jtag3_setparm(pgm, SCOPE_AVR, SET_GET_CTXT_OPTIONS, PARM3_OPT_CHIP_ERASE_TO_ENTER, buf, 1) < 0)
+    return -1;
+
+  free(resp);
+  return 0;
+}
+
+
 /*
  * There is no chip erase functionality in debugWire mode.
  */
@@ -2378,7 +2409,7 @@ void jtag3_updi_initpgm(PROGRAMMER * pgm)
   pgm->enable         = jtag3_enable;
   pgm->disable        = jtag3_disable;
   pgm->program_enable = jtag3_program_enable_dummy;
-  pgm->chip_erase     = jtag3_chip_erase;
+  pgm->chip_erase     = jtag3_chip_erase_updi;
   pgm->open           = jtag3_open_updi;
   pgm->close          = jtag3_close;
   pgm->read_byte      = jtag3_read_byte;
diff --git a/avrdude/jtag3_private.h b/avrdude/jtag3_private.h
index 80ffb2a..7a2374f 100644
--- a/avrdude/jtag3_private.h
+++ b/avrdude/jtag3_private.h
@@ -144,6 +144,7 @@
 #  define RSP3_FAIL_WRONG_MODE          0x32 /* progmode vs. non-prog */
 #  define RSP3_FAIL_UNSUPP_MEMORY       0x34 /* unsupported memory type */
 #  define RSP3_FAIL_WRONG_LENGTH        0x35 /* wrong lenth for mem access */
+#  define RSP3_FAIL_OCD_LOCKED          0x44 /* device is locked */
 #  define RSP3_FAIL_NOT_UNDERSTOOD      0x91
 
 /* ICE events */
@@ -168,6 +169,15 @@
 #define MTYPE_PRODSIG     0xc6	/* xmega production signature - undocumented in AVR067 */
 #define MTYPE_SIB         0xD3  /* AVR8X System Information Block */
 
+/*
+ * SET and GET context definitions
+ */
+#define SET_GET_CTXT_CONFIG    0x00 /* Configuration */
+#define SET_GET_CTXT_PHYSICAL  0x01 /* Physical interface related */
+#define SET_GET_CTXT_DEVICE    0x02 /* Device specific settings */
+#define SET_GET_CTXT_OPTIONS   0x03 /* Option-related settings */
+#define SET_GET_CTXT_SESSION   0x04 /* Session-related settings */
+
 /*
  * Parameters are divided into sections, where the section number
  * precedes each parameter address.  There are distinct parameter
@@ -209,12 +219,19 @@
                                  * before/after, bits before/after), 4
                                  * bytes */
 
+/*
+ * Physical context parameters
+ */
 #define PARM3_CLK_MEGA_PROG  0x20 /* section 1, AVR scope, 2 bytes (kHz) */
 #define PARM3_CLK_MEGA_DEBUG 0x21 /* section 1, AVR scope, 2 bytes (kHz) */
 #define PARM3_CLK_XMEGA_JTAG 0x30 /* section 1, AVR scope, 2 bytes (kHz) */
 #define PARM3_CLK_XMEGA_PDI  0x31 /* section 1, AVR scope, 2 bytes (kHz) */
 
-
+/*
+ * Options context parameters
+ */
+#define PARM3_OPT_12V_UPDI_ENABLE      0x06
+#define PARM3_OPT_CHIP_ERASE_TO_ENTER  0x07
 
 /* Xmega erase memory types, for CMND_XMEGA_ERASE */
 #define XMEGA_ERASE_CHIP        0x00
diff --git a/avrdude/main.c b/avrdude/main.c
index abd9655..b762393 100644
--- a/avrdude/main.c
+++ b/avrdude/main.c
@@ -1043,6 +1043,13 @@ int main(int argc, char * argv [])
   sig_again:
     usleep(waittime);
     if (init_ok) {
+      if ( (p->flags & AVRPART_HAS_UPDI) && (erase == 1) && !(uflags & UF_AUTO_ERASE) ) {
+        if (quell_progress < 2) {
+          avrdude_message(MSG_INFO, "%s: erasing chip\n", progname);
+        }
+        exitrc = avr_chip_erase(pgm, p);
+        if(exitrc) goto main_exit;
+      }
       rc = avr_signature(pgm, p);
       if (rc != 0) {
         avrdude_message(MSG_INFO, "%s: error reading signature data, rc=%d\n",
@@ -1189,7 +1196,7 @@ int main(int argc, char * argv [])
     }
   }
 
-  if (init_ok && erase) {
+  if (init_ok && erase && !(p->flags & AVRPART_HAS_UPDI)) {
     /*
      * erase the chip's flash and eeprom memories, this is required
      * before the chip can accept new programming
