From 3d5beb40dfab471ca70f7c766ada1d81739e4f3e Mon Sep 17 00:00:00 2001
From: Joerg Wunsch <j@uriah.heep.sax.de>
Date: Tue, 7 Oct 2014 15:21:14 +0000
Subject: [PATCH] bug #42267: jtag3isp fails to read lock and fuse bytes
 directly after changing lock byte * stk500v2.c (stk500isp_write_byte): As a
 workaround for broken tool firmware, add 10 ms of delay before returning from
 any single-byte write operation.

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1337 81a1dc3b-b13d-400b-aceb-764788c761c2
---
 ChangeLog  |  8 ++++++++
 NEWS       |  2 ++
 stk500v2.c | 23 ++++++++++-------------
 3 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 428cbdfd..a16699e0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2014-10-07  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
+
+	bug #42267: jtag3isp fails to read lock and fuse bytes directly
+	after changing lock byte
+	* stk500v2.c (stk500isp_write_byte): As a workaround for broken
+	tool firmware, add 10 ms of delay before returning from any
+	single-byte write operation.
+
 2014-10-06  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
 
 	* stk500v2.c: Use stk500isp_read_byte/stk500isp_write_byte for
diff --git a/NEWS b/NEWS
index 99c1068d..e952b983 100644
--- a/NEWS
+++ b/NEWS
@@ -22,6 +22,8 @@ Current:
 
   * Bugfixes
     - bug #22248: Read efuse error (partial fix)
+    - bug #42267: jtag3isp fails to read lock and fuse bytes directly
+                  after changing lock byte
 
   * Internals:
     - ...
diff --git a/stk500v2.c b/stk500v2.c
index fd8b5e1b..53b92917 100644
--- a/stk500v2.c
+++ b/stk500v2.c
@@ -2241,19 +2241,16 @@ static int stk500isp_write_byte(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
     return -1;
   }
 
-  if (buf[0] == CMD_PROGRAM_FUSE_ISP) {
-    /*
-     * The JTAGICE3 needs one leave progmode / enter progmode cycle
-     * after writing fuses, before being able to read them back.
-     */
-    buf[0] = CMD_LEAVE_PROGMODE_ISP;
-    buf[1] = 1; // preDelay;
-    buf[2] = 1; // postDelay;
-
-    stk500v2_command(pgm, buf, 3, sizeof(buf));
-
-    pgm->program_enable(pgm, p);
-  }
+  /*
+   * Prevent verification readback to be too fast, see
+   * https://savannah.nongnu.org/bugs/index.php?42267
+   *
+   * After all, this is just an ugly hack working around some
+   * brokeness in the Atmel firmware starting with the AVRISPmkII (the
+   * old JTAGICEmkII isn't affected).  Let's hope 10 ms of additional
+   * delay are good enough for everyone.
+   */
+  usleep(10000);
 
   return 0;
 }