From a5f42858ea7a10dca332a8ac771036f19a6233b4 Mon Sep 17 00:00:00 2001
From: Joerg Wunsch <j@uriah.heep.sax.de>
Date: Wed, 24 Jun 2009 21:32:12 +0000
Subject: [PATCH] Contributed by Zoltan Laday: patch #6825: xmega problems with
 JTAGICEmkII * jtagmkII.c: Many fixes for Xmega devices. * jtagmkII_private.h:
 Add various new constants required for Xmega devices. * avrdude.conf.in: New
 devices: ATXMEGA64A1, ATXMEGA192A1, ATXMEGA256A1, ATXMEGA64A3, ATXMEGA128A3,
 ATXMEGA192A3, ATXMEGA256A3, ATXMEGA256A3B, ATXMEGA16A4, ATXMEGA32A4,
 ATXMEGA64A4, ATXMEGA128A4 * avr.c (avr_read, avr_write): Add more names for
 (Xmega) memory areas that require paged operation.

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@821 81a1dc3b-b13d-400b-aceb-764788c761c2
---
 ChangeLog          |   14 +
 NEWS               |   17 +-
 avr.c              |   17 +-
 avrdude.conf.in    | 1491 ++++++++++++++++++++++++++++++++++++++------
 jtagmkII.c         |  107 +++-
 jtagmkII_private.h |    6 +
 6 files changed, 1447 insertions(+), 205 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 38af1a8d..7f4525d7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2009-06-24  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
+
+	Contributed by Zoltan Laday:
+	patch #6825: xmega problems with JTAGICEmkII
+	* jtagmkII.c: Many fixes for Xmega devices.
+	* jtagmkII_private.h: Add various new constants required for
+	Xmega devices.
+	* avrdude.conf.in: New devices: ATXMEGA64A1, ATXMEGA192A1,
+	ATXMEGA256A1, ATXMEGA64A3, ATXMEGA128A3, ATXMEGA192A3,
+	ATXMEGA256A3, ATXMEGA256A3B, ATXMEGA16A4, ATXMEGA32A4,
+	ATXMEGA64A4, ATXMEGA128A4
+	* avr.c (avr_read, avr_write): Add more names for (Xmega)
+	memory areas that require paged operation.
+
 2009-06-24  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
 
 	* stk500v2.c (stk600_xprog_write_byte): Handle writing fuse bytes.
diff --git a/NEWS b/NEWS
index 07083cca..f1531ef2 100644
--- a/NEWS
+++ b/NEWS
@@ -9,9 +9,22 @@ Current:
 
   * New devices supported:
 
+      - ATXMEGA64A1
+      - ATXMEGA192A1
+      -	ATXMEGA256A1
+      - ATXMEGA64A3
+      - ATXMEGA128A3
+      - ATXMEGA192A3
+      -	ATXMEGA256A3
+      - ATXMEGA256A3B
+      - ATXMEGA16A4
+      - ATXMEGA32A4
+      - ATXMEGA64A4
+      - ATXMEGA128A4
+
   * New programmers supported:
-  
-  *
+
+  * Major Xmega fixes for the JTAG ICE mkII (patch #6825)
 
 
 Version 5.6:
diff --git a/avr.c b/avr.c
index 56946da7..e7f2f737 100644
--- a/avr.c
+++ b/avr.c
@@ -171,8 +171,13 @@ int avr_read(PROGRAMMER * pgm, AVRPART * p, char * memtype, int size,
    */
   memset(buf, 0xff, size);
 
-  if ((strcmp(mem->desc, "flash")==0) || (strcmp(mem->desc, "eeprom")==0) ||
-      (strcmp(mem->desc, "boot")==0)) {
+  if ((strcmp(mem->desc, "eeprom")==0) || 
+      (strcmp(mem->desc, "flash")==0) ||
+      (strcmp(mem->desc, "application")==0) ||
+      (strcmp(mem->desc, "apptable")==0) ||
+      (strcmp(mem->desc, "boot")==0) ||
+      (strcmp(mem->desc, "usersig")==0) ||
+      (strcmp(mem->desc, "prodsig")==0)) {
     if (pgm->paged_load != NULL && mem->page_size != 0) {
       /*
        * the programmer supports a paged mode read, perhaps more
@@ -569,8 +574,12 @@ int avr_write(PROGRAMMER * pgm, AVRPART * p, char * memtype, int size,
             progbuf, wsize);
   }
 
-  if ((strcmp(m->desc, "flash")==0) || (strcmp(m->desc, "eeprom")==0) ||
-      (strcmp(m->desc, "boot")==0)) {
+  if ((strcmp(m->desc, "application")==0) || 
+      (strcmp(m->desc, "apptable")==0) ||
+      (strcmp(m->desc, "boot")==0) ||
+      (strcmp(m->desc, "flash")==0) ||
+      (strcmp(m->desc, "prodsig")==0) ||
+      (strcmp(m->desc, "usersig")==0)) {
     if (pgm->paged_write != NULL && m->page_size != 0) {
       /*
        * the programmer supports a paged mode write, perhaps more
diff --git a/avrdude.conf.in b/avrdude.conf.in
index 009ac21c..7f9cd473 100644
--- a/avrdude.conf.in
+++ b/avrdude.conf.in
@@ -13650,223 +13650,1360 @@ part
   ;
 
 #------------------------------------------------------------
-# ATxmega128A1 rev. D (engineering samples)
+# ATXMEGA64A1
 #------------------------------------------------------------
 
 part
-    id               = "x128a1d";
-    desc             = "ATXMEGA128A1REVD";
-    signature        = 0x1E 0x97 0x41;
-    has_jtag         = yes;
-    has_pdi          = yes;
-    nvm_base         = 0x01C0;
+    id		= "x64a1";
+    desc	= "ATXMEGA64A1";
+    signature	= 0x1e 0x96 0x4e;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
 
     memory "eeprom"
-        page_size       = 32;
-        size            = 2048;
-      ;
+        size		= 0x0800;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
-    memory "flash"
-        size            = 0x20000;
-        page_size       = 512;
-      ;
+    memory "application"
+        size		= 0x00010000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00001000;
+        offset		= 0x0080f000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
     memory "boot"
-        size            = 0x2000;
-        page_size       = 512;
-      ;
+        size		= 0x00001000;
+        offset		= 0x00810000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
-# signature is actually in IO address space
-    memory "signature"
-        size            = 3;
-        offset          = 0x90;
-      ;
+    memory "flash"
+        size		= 0x00011000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
-    memory "fuse0"
-        size            = 1;
-        offset          = 0x20;
-      ;
-
-    memory "fuse1"
-        size            = 1;
-        offset          = 0x21;
-      ;
-
-    memory "fuse2"
-        size            = 1;
-        offset          = 0x22;
-      ;
-
-    memory "fuse4"
-        size            = 1;
-        offset          = 0x24;
-      ;
-
-    memory "fuse5"
-        size            = 1;
-        offset          = 0x25;
-      ;
-
-    memory "lockbits"
-        size            = 1;
-        offset          = 0x27;
-      ;
-
-    memory "calibration"
-        size            = 512;
-        offset          = 0x200;
-      ;
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
     memory "usersig"
-        size            = 512;
-      ;
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
-  ;
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
 
 #------------------------------------------------------------
-# ATxmega128A1
+# ATXMEGA128A1
 #------------------------------------------------------------
 
 part
-    id               = "x128a1";
-    desc             = "ATXMEGA128A1";
-    signature        = 0x1E 0x97 0x4C;
-    has_jtag         = yes;
-    has_pdi          = yes;
-    nvm_base         = 0x01C0;
+    id		= "x128a1";
+    desc	= "ATXMEGA128A1";
+    signature	= 0x1e 0x97 0x4c;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
 
     memory "eeprom"
-        page_size       = 32;
-        size            = 2048;
-      ;
+        size		= 0x0800;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
-    memory "flash"
-        size            = 0x20000;
-        page_size       = 512;
-      ;
+    memory "application"
+        size		= 0x00020000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00002000;
+        offset		= 0x0081e000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
     memory "boot"
-        size            = 0x2000;
-        page_size       = 512;
-      ;
+        size		= 0x00002000;
+        offset		= 0x00820000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
-# signature is actually in IO address space
-    memory "signature"
-        size            = 3;
-        offset          = 0x90;
-      ;
+    memory "flash"
+        size		= 0x00022000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
-    memory "fuse0"
-        size            = 1;
-        offset          = 0x20;
-      ;
-
-    memory "fuse1"
-        size            = 1;
-        offset          = 0x21;
-      ;
-
-    memory "fuse2"
-        size            = 1;
-        offset          = 0x22;
-      ;
-
-    memory "fuse4"
-        size            = 1;
-        offset          = 0x24;
-      ;
-
-    memory "fuse5"
-        size            = 1;
-        offset          = 0x25;
-      ;
-
-    memory "lockbits"
-        size            = 1;
-        offset          = 0x27;
-      ;
-
-    memory "calibration"
-        size            = 512;
-      ;
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
     memory "usersig"
-        size            = 512;
-        offset          = 0x200;
-      ;
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
-  ;
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
 
 #------------------------------------------------------------
-# ATxmega256A3
+# ATXMEGA128A1REVD
 #------------------------------------------------------------
 
 part
-    id               = "x256a3";
-    desc             = "ATXMEGA256A3";
-    signature        = 0x1E 0x98 0x42;
-    has_jtag         = yes;
-    has_pdi          = yes;
-    nvm_base         = 0x01C0;
+    id		= "x128a1d";
+    desc	= "ATXMEGA128A1REVD";
+    signature	= 0x1e 0x97 0x41;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
 
     memory "eeprom"
-        page_size       = 32;
-        size            = 4096;
-      ;
+        size		= 0x0800;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
-    memory "flash"
-        size            = 0x40000;
-        page_size       = 512;
-      ;
+    memory "application"
+        size		= 0x00020000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00002000;
+        offset		= 0x0081e000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
     memory "boot"
-        size            = 0x2000;
-        page_size       = 512;
-      ;
+        size		= 0x00002000;
+        offset		= 0x00820000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
-# signature is actually in IO address space
-    memory "signature"
-        size            = 3;
-        offset          = 0x90;
-      ;
+    memory "flash"
+        size		= 0x00022000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
-    memory "fuse0"
-        size            = 1;
-        offset          = 0x20;
-      ;
-
-    memory "fuse1"
-        size            = 1;
-        offset          = 0x21;
-      ;
-
-    memory "fuse2"
-        size            = 1;
-        offset          = 0x22;
-      ;
-
-    memory "fuse4"
-        size            = 1;
-        offset          = 0x24;
-      ;
-
-    memory "fuse5"
-        size            = 1;
-        offset          = 0x25;
-      ;
-
-    memory "lockbits"
-        size            = 1;
-        offset          = 0x27;
-      ;
-
-    memory "calibration"
-        size            = 512;
-      ;
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
 
     memory "usersig"
-        size            = 512;
-        offset          = 0x200;
-      ;
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
+
+#------------------------------------------------------------
+# ATXMEGA192A1
+#------------------------------------------------------------
+
+part
+    id		= "x192a1";
+    desc	= "ATXMEGA192A1";
+    signature	= 0x1e 0x97 0x4e;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
+
+    memory "eeprom"
+        size		= 0x0800;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "application"
+        size		= 0x00030000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00002000;
+        offset		= 0x0082e000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "boot"
+        size		= 0x00002000;
+        offset		= 0x00830000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "flash"
+        size		= 0x00032000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "usersig"
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
+
+#------------------------------------------------------------
+# ATXMEGA256A1
+#------------------------------------------------------------
+
+part
+    id		= "x256a1";
+    desc	= "ATXMEGA256A1";
+    signature	= 0x1e 0x98 0x46;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
+
+    memory "eeprom"
+        size		= 0x1000;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "application"
+        size		= 0x00040000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00002000;
+        offset		= 0x0083e000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "boot"
+        size		= 0x00002000;
+        offset		= 0x00840000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "flash"
+        size		= 0x00042000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "usersig"
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
+
+#------------------------------------------------------------
+# ATXMEGA64A3
+#------------------------------------------------------------
+
+part
+    id		= "x64a3";
+    desc	= "ATXMEGA64A3";
+    signature	= 0x1e 0x96 0x42;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
+
+    memory "eeprom"
+        size		= 0x0800;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "application"
+        size		= 0x00010000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00001000;
+        offset		= 0x0080f000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "boot"
+        size		= 0x00001000;
+        offset		= 0x00810000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "flash"
+        size		= 0x00011000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "usersig"
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
+
+#------------------------------------------------------------
+# ATXMEGA128A3
+#------------------------------------------------------------
+
+part
+    id		= "x128a3";
+    desc	= "ATXMEGA128A3";
+    signature	= 0x1e 0x97 0x42;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
+
+    memory "eeprom"
+        size		= 0x0800;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "application"
+        size		= 0x00020000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00002000;
+        offset		= 0x0081e000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "boot"
+        size		= 0x00002000;
+        offset		= 0x00820000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "flash"
+        size		= 0x00022000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "usersig"
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
+
+#------------------------------------------------------------
+# ATXMEGA192A3
+#------------------------------------------------------------
+
+part
+    id		= "x192a3";
+    desc	= "ATXMEGA192A3";
+    signature	= 0x1e 0x97 0x44;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
+
+    memory "eeprom"
+        size		= 0x0800;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "application"
+        size		= 0x00030000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00002000;
+        offset		= 0x0082e000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "boot"
+        size		= 0x00002000;
+        offset		= 0x00830000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "flash"
+        size		= 0x00032000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "usersig"
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
+
+#------------------------------------------------------------
+# ATXMEGA256A3
+#------------------------------------------------------------
+
+part
+    id		= "x256a3";
+    desc	= "ATXMEGA256A3";
+    signature	= 0x1e 0x98 0x42;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
+
+    memory "eeprom"
+        size		= 0x1000;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "application"
+        size		= 0x00040000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00002000;
+        offset		= 0x0083e000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "boot"
+        size		= 0x00002000;
+        offset		= 0x00840000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "flash"
+        size		= 0x00042000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "usersig"
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
+
+#------------------------------------------------------------
+# ATXMEGA256A3B
+#------------------------------------------------------------
+
+part
+    id		= "x256a3b";
+    desc	= "ATXMEGA256A3B";
+    signature	= 0x1e 0x98 0x43;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
+
+    memory "eeprom"
+        size		= 0x1000;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "application"
+        size		= 0x00040000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00002000;
+        offset		= 0x0083e000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "boot"
+        size		= 0x00002000;
+        offset		= 0x00840000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "flash"
+        size		= 0x00042000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "usersig"
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
+
+#------------------------------------------------------------
+# ATXMEGA16A4
+#------------------------------------------------------------
+
+part
+    id		= "x16a4";
+    desc	= "ATXMEGA16A4";
+    signature	= 0x1e 0x94 0x41;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
+
+    memory "eeprom"
+        size		= 0x0400;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "application"
+        size		= 0x00004000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00001000;
+        offset		= 0x00803000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "boot"
+        size		= 0x00001000;
+        offset		= 0x00804000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "flash"
+        size		= 0x00005000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "usersig"
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
+
+#------------------------------------------------------------
+# ATXMEGA32A4
+#------------------------------------------------------------
+
+part
+    id		= "x32a4";
+    desc	= "ATXMEGA32A4";
+    signature	= 0x1e 0x95 0x41;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
+
+    memory "eeprom"
+        size		= 0x0400;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "application"
+        size		= 0x00008000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00001000;
+        offset		= 0x00807000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "boot"
+        size		= 0x00001000;
+        offset		= 0x00808000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "flash"
+        size		= 0x00009000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "usersig"
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
+
+#------------------------------------------------------------
+# ATXMEGA64A4
+#------------------------------------------------------------
+
+part
+    id		= "x64a4";
+    desc	= "ATXMEGA64A4";
+    signature	= 0x1e 0x96 0x46;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
+
+    memory "eeprom"
+        size		= 0x0800;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "application"
+        size		= 0x00010000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00001000;
+        offset		= 0x0080f000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "boot"
+        size		= 0x00001000;
+        offset		= 0x00810000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "flash"
+        size		= 0x00011000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "usersig"
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
+
+#------------------------------------------------------------
+# ATXMEGA128A4
+#------------------------------------------------------------
+
+part
+    id		= "x128a4";
+    desc	= "ATXMEGA128A4";
+    signature	= 0x1e 0x97 0x46;
+    has_jtag	= yes;
+    has_pdi	= yes;
+    nvm_base	= 0x01c0;
+
+    memory "eeprom"
+        size		= 0x0800;
+        offset		= 0x08c0000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "application"
+        size		= 0x00020000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "apptable"
+        size		= 0x00002000;
+        offset		= 0x0081e000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "boot"
+        size		= 0x00002000;
+        offset		= 0x00820000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "flash"
+        size		= 0x00022000;
+        offset		= 0x0800000;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "prodsig"
+        size		= 0x200;
+        offset		= 0x8e0200;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "usersig"
+        size		= 0x200;
+        offset		= 0x8e0400;
+        page_size	= 0x100;
+        readsize	= 0x100;
+    ;
+
+    memory "signature"
+        size		= 3;
+        offset		= 0x1000090;
+    ;
+
+    memory "fuse0"
+        size		= 1;
+        offset		= 0x8f0020;
+    ;
+
+    memory "fuse1"
+        size		= 1;
+        offset		= 0x8f0021;
+    ;
+
+    memory "fuse2"
+        size		= 1;
+        offset		= 0x8f0022;
+    ;
+
+    memory "fuse4"
+        size		= 1;
+        offset		= 0x8f0024;
+    ;
+
+    memory "fuse5"
+        size		= 1;
+        offset		= 0x8f0025;
+    ;
+
+    memory "lock"
+        size		= 1;
+        offset		= 0x8f0027;
+    ;
+;
 
-  ;
diff --git a/jtagmkII.c b/jtagmkII.c
index b8d03f3a..8e8025ce 100644
--- a/jtagmkII.c
+++ b/jtagmkII.c
@@ -1016,6 +1016,45 @@ static int jtagmkII_program_enable(PROGRAMMER * pgm)
   return 0;
 }
 
+static int jtagmkII_pre_write(PROGRAMMER * pgm)
+{
+  int status;
+  unsigned char *resp, c;
+  unsigned char buf[] = { CMND_0x34, 0x0, 0x0, 0x0, 0x0, 0x0 };
+  if (verbose >= 2)
+    fprintf(stderr, "%s: jtagmkII_pre_write(): Sending pre-write command: ",
+	    progname);
+  jtagmkII_send(pgm, buf, 6);
+
+  status = jtagmkII_recv(pgm, &resp);
+  if (status <= 0) {
+    if (verbose >= 2)
+      putc('\n', stderr);
+    fprintf(stderr,
+	    "%s: jtagmkII_pre_write(): "
+	    "timeout/error communicating with programmer (status %d)\n",
+	    progname, status);
+    return -1;
+  }
+  if (verbose >= 3) {
+    putc('\n', stderr);
+    jtagmkII_prmsg(pgm, resp, status);
+  } else if (verbose == 2)
+    fprintf(stderr, "0x%02x (%d bytes msg)\n", resp[0], status);
+  c = resp[0];
+  free(resp);
+  if (c != RSP_OK) {
+    fprintf(stderr,
+	    "%s: jtagmkII_pre_write(): "
+	    "bad response to pre_write command: %s\n",
+	    progname, jtagmkII_get_rc(c));
+    return -1;
+  }
+
+  return 0;
+}
+
+
 static int jtagmkII_program_disable(PROGRAMMER * pgm)
 {
   int status;
@@ -1509,6 +1548,7 @@ static int jtagmkII_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
   int addr, block_size;
   unsigned char *cmd;
   unsigned char *resp;
+  unsigned char par[4];
   int status, tries;
   long otimeout = serial_recv_timeout;
 
@@ -1526,10 +1566,17 @@ static int jtagmkII_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
 	    progname);
     return -1;
   }
+  if ( p->flags & AVRPART_HAS_PDI )
+  {
+   u32_to_b4( par, m->offset );
+   (void) jtagmkII_setparm( pgm, PAR_PDI_OFFSET_START, par );
+   u32_to_b4( par, m->offset + m->size );
+   (void) jtagmkII_setparm( pgm, PAR_PDI_OFFSET_END, par );
+  }
 
   cmd[0] = CMND_WRITE_MEMORY;
+  cmd[1] = ( p->flags & AVRPART_HAS_PDI ) ? MTYPE_FLASH : MTYPE_FLASH_PAGE;
   if (strcmp(m->desc, "flash") == 0) {
-    cmd[1] = MTYPE_FLASH_PAGE;
     PDATA(pgm)->flash_pageaddr = (unsigned long)-1L;
     page_size = PDATA(pgm)->flash_pagesize;
   } else if (strcmp(m->desc, "eeprom") == 0) {
@@ -1549,11 +1596,12 @@ static int jtagmkII_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
       free(cmd);
       return n_bytes;
     }
-    cmd[1] = MTYPE_EEPROM_PAGE;
+    cmd[1] = ( p->flags & AVRPART_HAS_PDI ) ? MTYPE_EEPROM : MTYPE_EEPROM_PAGE;
     PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L;
     page_size = PDATA(pgm)->eeprom_pagesize;
   }
-
+  if ( cmd[1] == MTYPE_FLASH )  (void)jtagmkII_pre_write(pgm);
+  
   serial_recv_timeout = 100;
   for (addr = 0; addr < n_bytes; addr += page_size) {
     report_progress(addr, n_bytes,NULL);
@@ -1568,7 +1616,7 @@ static int jtagmkII_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
 	      progname, addr, block_size);
 
     u32_to_b4(cmd + 2, page_size);
-    u32_to_b4(cmd + 6, addr);
+    u32_to_b4(cmd + 6, addr+m->offset );
 
     /*
      * The JTAG ICE will refuse to write anything but a full page, at
@@ -1653,14 +1701,16 @@ static int jtagmkII_paged_load(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
   page_size = m->readsize;
 
   cmd[0] = CMND_READ_MEMORY;
-  if (strcmp(m->desc, "flash") == 0) {
-    cmd[1] = MTYPE_FLASH_PAGE;
-  } else if (strcmp(m->desc, "eeprom") == 0) {
-    cmd[1] = MTYPE_EEPROM_PAGE;
+  cmd[1] = ( p->flags & AVRPART_HAS_PDI ) ? MTYPE_FLASH : MTYPE_FLASH_PAGE;
+  if (strcmp(m->desc, "eeprom") == 0) {
+    cmd[1] = ( p->flags & AVRPART_HAS_PDI ) ? MTYPE_EEPROM : MTYPE_EEPROM_PAGE;
     if (pgm->flag & PGM_FL_IS_DW)
       return -1;
+  } else if ( ( strcmp(m->desc, "prodsig") == 0 ) ) {
+    cmd[1] = MTYPE_PRODSIG;
+  } else if ( ( strcmp(m->desc, "usersig") == 0 ) ) {
+    cmd[1] = MTYPE_USERSIG;
   }
-
   serial_recv_timeout = 100;
   for (addr = 0; addr < n_bytes; addr += page_size) {
     report_progress(addr, n_bytes,NULL);
@@ -1675,7 +1725,7 @@ static int jtagmkII_paged_load(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
 	      progname, addr, block_size);
 
     u32_to_b4(cmd + 2, block_size);
-    u32_to_b4(cmd + 6, addr);
+    u32_to_b4(cmd + 6, addr+m->offset );
 
     tries = 0;
 
@@ -1719,7 +1769,7 @@ static int jtagmkII_paged_load(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
       serial_recv_timeout = otimeout;
       return -1;
     }
-    memcpy(m->buf + addr, resp + 1, status);
+    memcpy(m->buf + addr, resp + 1, status-1);
     free(resp);
   }
   serial_recv_timeout = otimeout;
@@ -1746,14 +1796,15 @@ static int jtagmkII_read_byte(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
   cmd[0] = CMND_READ_MEMORY;
   unsupp = 0;
 
+  addr += mem->offset;
+  cmd[1] = ( p->flags & AVRPART_HAS_PDI ) ? MTYPE_FLASH : MTYPE_FLASH_PAGE;
   if (strcmp(mem->desc, "flash") == 0) {
-    cmd[1] = MTYPE_FLASH_PAGE;
     pagesize = mem->page_size;
     paddr = addr & ~(pagesize - 1);
     paddr_ptr = &PDATA(pgm)->flash_pageaddr;
     cache_ptr = PDATA(pgm)->flash_pagecache;
   } else if (strcmp(mem->desc, "eeprom") == 0) {
-    if (pgm->flag & PGM_FL_IS_DW) {
+    if ( (pgm->flag & PGM_FL_IS_DW) || ( p->flags & AVRPART_HAS_PDI ) ) {
       /* debugWire cannot use page access for EEPROM */
       cmd[1] = MTYPE_EEPROM;
     } else {
@@ -1782,6 +1833,12 @@ static int jtagmkII_read_byte(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
     cmd[1] = MTYPE_LOCK_BITS;
     if (pgm->flag & PGM_FL_IS_DW)
       unsupp = 1;
+  } else if (strncmp(mem->desc, "fuse", strlen("fuse")) == 0) {
+    cmd[1] = MTYPE_FUSE_BITS;
+  } else if (strcmp(mem->desc, "usersig") == 0) {
+    cmd[1] = MTYPE_USERSIG;
+  } else if (strcmp(mem->desc, "prodsig") == 0) {
+    cmd[1] = MTYPE_PRODSIG;
   } else if (strcmp(mem->desc, "calibration") == 0) {
     cmd[1] = MTYPE_OSCCAL_BYTE;
     if (pgm->flag & PGM_FL_IS_DW)
@@ -1816,10 +1873,6 @@ static int jtagmkII_read_byte(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
       }
       return 0;
     }
-    else if (mem->offset != 0) {
-      cmd[1] = MTYPE_SRAM;
-      addr += mem->offset;
-    }
 
   }
 
@@ -1919,14 +1972,16 @@ static int jtagmkII_write_byte(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
     fprintf(stderr, "%s: jtagmkII_write_byte(.., %s, 0x%lx, ...)\n",
 	    progname, mem->desc, addr);
 
+  addr += mem->offset;
+
   writedata = data;
   cmd[0] = CMND_WRITE_MEMORY;
+  cmd[1] = ( p->flags & AVRPART_HAS_PDI ) ? MTYPE_FLASH : MTYPE_SPM;
   if (strcmp(mem->desc, "flash") == 0) {
-    cmd[1] = MTYPE_SPM;
-    need_progmode = 0;
-    PDATA(pgm)->flash_pageaddr = (unsigned long)-1L;
-    if (pgm->flag & PGM_FL_IS_DW)
-      unsupp = 1;
+     need_progmode = 0;
+     PDATA(pgm)->flash_pageaddr = (unsigned long)-1L;
+     if (pgm->flag & PGM_FL_IS_DW)
+       unsupp = 1;
   } else if (strcmp(mem->desc, "eeprom") == 0) {
     cmd[1] = MTYPE_EEPROM;
     need_progmode = 0;
@@ -1946,6 +2001,12 @@ static int jtagmkII_write_byte(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
     addr = 2;
     if (pgm->flag & PGM_FL_IS_DW)
       unsupp = 1;
+  } else if (strncmp(mem->desc, "fuse", strlen("fuse")) == 0) {
+    cmd[1] = MTYPE_FUSE_BITS;
+  } else if (strcmp(mem->desc, "usersig") == 0) {
+    cmd[1] = MTYPE_USERSIG;
+  } else if (strcmp(mem->desc, "prodsig") == 0) {
+    cmd[1] = MTYPE_PRODSIG;
   } else if (strcmp(mem->desc, "lock") == 0) {
     cmd[1] = MTYPE_LOCK_BITS;
     if (pgm->flag & PGM_FL_IS_DW)
@@ -2127,6 +2188,8 @@ static int jtagmkII_setparm(PROGRAMMER * pgm, unsigned char parm,
   case PAR_OCD_JTAG_CLK: size = 1; break;
   case PAR_TIMERS_RUNNING: size = 1; break;
   case PAR_DAISY_CHAIN_INFO: size = 4; break;
+  case PAR_PDI_OFFSET_START:
+  case PAR_PDI_OFFSET_END: size = 4; break;
   default:
     fprintf(stderr, "%s: jtagmkII_setparm(): unknown parameter 0x%02x\n",
 	    progname, parm);
diff --git a/jtagmkII_private.h b/jtagmkII_private.h
index 25774642..0f8d89ab 100644
--- a/jtagmkII_private.h
+++ b/jtagmkII_private.h
@@ -103,6 +103,7 @@
 #define CMND_SPI_CMD 0x1D
 #define CMND_WRITE_MEMORY 0x04
 #define CMND_WRITE_PC 0x06
+#define CMND_0x34 0x34
 
 /* ICE responses */
 #define RSP_DEBUGWIRE_SYNC_FAILED 0xAC
@@ -170,6 +171,9 @@
 #define MTYPE_SIGN_JTAG 0xB4	/* signature in programming mode */
 #define MTYPE_OSCCAL_BYTE 0xB5	/* osccal cells in programming mode */
 #define MTYPE_CAN 0xB6		/* CAN mailbox */
+#define MTYPE_FLASH 0xc0	/* xmega flash - undocumented in AVR067 */
+#define MTYPE_USERSIG 0xc5	/* xmega user signature - undocumented in AVR067 */
+#define MTYPE_PRODSIG 0xc6	/* xmega production signature - undocumented in AVR067 */
 
 /* (some) ICE parameters, for CMND_{GET,SET}_PARAMETER */
 #define PAR_HW_VERSION 0x01
@@ -219,6 +223,8 @@
 #define PAR_TARGET_SIGNATURE 0x1D
 #define PAR_DEBUGWIRE_BAUDRATE 0x1E
 #define PAR_PROGRAM_ENTRY_POINT 0x1F
+#define PAR_PDI_OFFSET_START 0x32
+#define PAR_PDI_OFFSET_END 0x33
 #define PAR_PACKET_PARSING_ERRORS 0x40
 #define PAR_VALID_PACKETS_RECEIVED 0x41
 #define PAR_INTERCOMMUNICATION_TX_FAILURES 0x42