From f2c179de58c63b2202a490d283fd8fa216680ce7 Mon Sep 17 00:00:00 2001
From: fauxpark <fauxpark@gmail.com>
Date: Wed, 21 Aug 2019 15:18:52 +1000
Subject: [PATCH] Remove superfluous JTAG disable code (#6445)

* Remove superfluous JTAG disable code

* 32A has differently named register

* Accidentally some operators

* 32A also has different JTAG pins

* Wrap disable_jtag() in an ifndef

* Document this new define

* Rename the define, it conflicts with a LUFA thing

Also, move the ifndef wrapping to the call in keyboard_setup()
---
 docs/faq_debug.md                               | 17 +++++------------
 docs/zh-cn/faq_debug.md                         | 17 -----------------
 keyboards/amj96/matrix.c                        |  3 ---
 keyboards/clueboard/17/17.c                     |  4 ----
 keyboards/clueboard/66/rev2/rev2.c              |  4 ----
 .../clueboard/66_hotswap/prototype/prototype.c  |  4 ----
 keyboards/ergodone/matrix.c                     |  4 ----
 keyboards/handwired/promethium/matrix.c         |  7 -------
 keyboards/hhkb/rn42/rn42.c                      |  4 ----
 keyboards/hid_liber/matrix.c                    |  4 ----
 keyboards/hotdox/matrix.c                       |  4 ----
 keyboards/launchpad/rev1/rev1.c                 |  7 +------
 keyboards/lets_split/sockets/sockets.h          |  1 -
 keyboards/miniaxe/config.h                      |  2 --
 keyboards/sx60/matrix.c                         |  7 -------
 keyboards/vitamins_included/matrix.c            |  6 ------
 keyboards/vitamins_included/rev1/rev1.h         |  1 -
 tmk_core/common/keyboard.c                      | 11 +++++++++--
 tmk_core/common/keyboard.h                      |  2 --
 19 files changed, 15 insertions(+), 94 deletions(-)

diff --git a/docs/faq_debug.md b/docs/faq_debug.md
index f6cc729b6..5cc20251d 100644
--- a/docs/faq_debug.md
+++ b/docs/faq_debug.md
@@ -184,22 +184,15 @@ Pressing any key during sleep should wake host.
 
 Arduino Leonardo and micro have **ATMega32U4** and can be used for TMK, though Arduino bootloader may be a problem.
 
+## Enabling JTAG
 
-## Using PF4-7 Pins of USB AVR?
-You need to set JTD bit of MCUCR yourself to use PF4-7 as GPIO. Those pins are configured to serve JTAG function by default. MCUs like ATMega*U* or AT90USB* are affected with this.
+By default, the JTAG debugging interface is disabled as soon as the keyboard starts up. JTAG-capable MCUs come from the factory with the `JTAGEN` fuse set, and it takes over certain pins of the MCU that the board may be using for the switch matrix, LEDs, etc.
 
-If you are using Teensy this isn't needed. Teensy is shipped with JTAGEN fuse bit unprogrammed to disable the function.
+If you would like to keep JTAG enabled, just add the following to your `config.h`:
 
-See this code.
+```c
+#define NO_JTAG_DISABLE
 ```
-    // JTAG disable for PORT F. write JTD bit twice within four cycles.
-    MCUCR |= (1<<JTD);
-    MCUCR |= (1<<JTD);
-```
-https://github.com/tmk/tmk_keyboard/blob/master/keyboard/hbkb/matrix.c#L67
-
-And read **26.5.1 MCU Control Register – MCUCR** of ATMega32U4 datasheet.
-
 
 ## Adding LED Indicators of Lock Keys
 You need your own LED indicators for CapsLock, ScrollLock and NumLock? See this post.
diff --git a/docs/zh-cn/faq_debug.md b/docs/zh-cn/faq_debug.md
index ca8b3fd25..a3cc7c510 100644
--- a/docs/zh-cn/faq_debug.md
+++ b/docs/zh-cn/faq_debug.md
@@ -173,23 +173,6 @@ EXTRAKEY_ENABLE = yes          # 音频控制和系统控制
 
 Arduino Leonardo和micro使用**ATMega32U4**,该芯片TMK可用,但Arduino的bootloader会导致问题。
 
-
-## 在USB AVR使用PF4-7针脚?
-你要置位MCUCR寄存器JTD位来将PF4-7设置为GPIO。这些针脚默认是JTAG功能。 像ATMega*U* or AT90USB*这样的MCU会受影响。
-
-如果是用Teensy的话就不需要了。Tennsy自带JTAGEN位未编程来失能该功能。
-<!--翻译问题:上句可能有错,原文为:Teensy is shipped with JTAGEN fuse bit unprogrammed to disable the function. -->
-代码如下。
-```
-    // F接口JTAG失能。在四个周期内写入两次JTD位。
-    MCUCR |= (1<<JTD);
-    MCUCR |= (1<<JTD);
-```
-https://github.com/tmk/tmk_keyboard/blob/master/keyboard/hbkb/matrix.c#L67
-
-阅读ATMega32U4的datasheet中的**26.5.1 MCU Control Register – MCUCR**。
-
-
 ## 为锁定键添加指示灯
 你要自制CapsLock, ScrollLock 和 NumLock指示灯?见下文。
 
diff --git a/keyboards/amj96/matrix.c b/keyboards/amj96/matrix.c
index e41bbec72..5d00ea98f 100644
--- a/keyboards/amj96/matrix.c
+++ b/keyboards/amj96/matrix.c
@@ -63,9 +63,6 @@ void matrix_init(void)
 #ifdef UART_RGB_ENABLE
     uart_rgb_init();
 #endif
-    // disable JTAG
-    MCUCR = _BV(JTD);
-    MCUCR = _BV(JTD);
 
     // 85 REST
     DDRD |= _BV(PD7);
diff --git a/keyboards/clueboard/17/17.c b/keyboards/clueboard/17/17.c
index 627aa236d..9e8ca644e 100644
--- a/keyboards/clueboard/17/17.c
+++ b/keyboards/clueboard/17/17.c
@@ -6,10 +6,6 @@ void matrix_init_kb(void) {
     // put your keyboard start-up code here
     // runs once when the firmware starts up
     matrix_init_user();
-
-    // JTAG disable for PORT F. write JTD bit twice within four cycles.
-    MCUCR |= (1<<JTD);
-    MCUCR |= (1<<JTD);
 };
 
 void led_set_kb(uint8_t usb_led) {
diff --git a/keyboards/clueboard/66/rev2/rev2.c b/keyboards/clueboard/66/rev2/rev2.c
index 1a35b87b8..d676b6d5f 100644
--- a/keyboards/clueboard/66/rev2/rev2.c
+++ b/keyboards/clueboard/66/rev2/rev2.c
@@ -8,10 +8,6 @@ void matrix_init_kb(void) {
 	// runs once when the firmware starts up
 	matrix_init_user();
 	led_init_ports();
-
-    // JTAG disable for PORT F. write JTD bit twice within four cycles.
-    MCUCR |= (1<<JTD);
-    MCUCR |= (1<<JTD);
 }
 
 
diff --git a/keyboards/clueboard/66_hotswap/prototype/prototype.c b/keyboards/clueboard/66_hotswap/prototype/prototype.c
index 991d4b8fa..7c307be61 100644
--- a/keyboards/clueboard/66_hotswap/prototype/prototype.c
+++ b/keyboards/clueboard/66_hotswap/prototype/prototype.c
@@ -8,10 +8,6 @@ void matrix_init_kb(void) {
 	// runs once when the firmware starts up
 	matrix_init_user();
 	led_init_ports();
-
-    // JTAG disable for PORT F. write JTD bit twice within four cycles.
-    MCUCR |= (1<<JTD);
-    MCUCR |= (1<<JTD);
 }
 
 
diff --git a/keyboards/ergodone/matrix.c b/keyboards/ergodone/matrix.c
index 2eb8f24ba..4a8230aa8 100644
--- a/keyboards/ergodone/matrix.c
+++ b/keyboards/ergodone/matrix.c
@@ -77,10 +77,6 @@ uint8_t matrix_cols(void)
 
 void matrix_init(void)
 {
-  // disable JTAG
-  MCUCR = (1<<JTD);
-  MCUCR = (1<<JTD);
-
   unselect_rows();
   init_cols();
 
diff --git a/keyboards/handwired/promethium/matrix.c b/keyboards/handwired/promethium/matrix.c
index 3b8e5af82..b4a4c6396 100644
--- a/keyboards/handwired/promethium/matrix.c
+++ b/keyboards/handwired/promethium/matrix.c
@@ -104,13 +104,6 @@ uint8_t matrix_cols(void) {
 }
 
 void matrix_init(void) {
-
-    // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
-    #if  (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
-        MCUCR |= _BV(JTD);
-        MCUCR |= _BV(JTD);
-    #endif
-
     // initialize row and col
     unselect_rows();
     init_cols();
diff --git a/keyboards/hhkb/rn42/rn42.c b/keyboards/hhkb/rn42/rn42.c
index 154da4e10..06665425c 100644
--- a/keyboards/hhkb/rn42/rn42.c
+++ b/keyboards/hhkb/rn42/rn42.c
@@ -26,10 +26,6 @@ host_driver_t rn42_driver = {
 
 void rn42_init(void)
 {
-    // JTAG disable for PORT F. write JTD bit twice within four cycles.
-    MCUCR |= (1<<JTD);
-    MCUCR |= (1<<JTD);
-
     // PF7: BT connection control(high: connect, low: disconnect)
     rn42_autoconnect();
 
diff --git a/keyboards/hid_liber/matrix.c b/keyboards/hid_liber/matrix.c
index de154890d..05554a24c 100755
--- a/keyboards/hid_liber/matrix.c
+++ b/keyboards/hid_liber/matrix.c
@@ -172,10 +172,6 @@ uint8_t matrix_cols(void)
 
 void matrix_init(void)
 {
-    // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
-    MCUCR |= (1<<JTD);
-    MCUCR |= (1<<JTD);
-
     // initialize row and col
     setup_io_pins();
     setup_leds();
diff --git a/keyboards/hotdox/matrix.c b/keyboards/hotdox/matrix.c
index 9d3a98db2..11a1142d4 100644
--- a/keyboards/hotdox/matrix.c
+++ b/keyboards/hotdox/matrix.c
@@ -77,10 +77,6 @@ uint8_t matrix_cols(void)
 
 void matrix_init(void)
 {
-  // disable JTAG
-  MCUCR = (1<<JTD);
-  MCUCR = (1<<JTD);
-
   unselect_rows();
   init_cols();
 
diff --git a/keyboards/launchpad/rev1/rev1.c b/keyboards/launchpad/rev1/rev1.c
index a7aec80e1..cff4edf9f 100644
--- a/keyboards/launchpad/rev1/rev1.c
+++ b/keyboards/launchpad/rev1/rev1.c
@@ -1,14 +1,9 @@
 #include "launchpad.h"
 
 void matrix_init_kb(void) {
-	
-	// JTAG disable for PORT F. write JTD bit twice within four cycles.
-    MCUCR |= (1<<JTD);
-    MCUCR |= (1<<JTD);
-	
 	// Undo init of RX LED, we use that port currently
 	//DDRB &= ~(1<<0);
 	
 	matrix_init_user();
 	
-};
\ No newline at end of file
+};
diff --git a/keyboards/lets_split/sockets/sockets.h b/keyboards/lets_split/sockets/sockets.h
index 0ee93c020..a1a3f00a0 100644
--- a/keyboards/lets_split/sockets/sockets.h
+++ b/keyboards/lets_split/sockets/sockets.h
@@ -1,5 +1,4 @@
 #pragma once
-#define DISABLE_JTAG // The keyboard uses PF4 and PF7, which are used by JTAG.
 
 #include "lets_split.h"
 
diff --git a/keyboards/miniaxe/config.h b/keyboards/miniaxe/config.h
index db8509717..b0b9d4c47 100644
--- a/keyboards/miniaxe/config.h
+++ b/keyboards/miniaxe/config.h
@@ -237,5 +237,3 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 //#define EE_HANDS
 #define I2C_MASTER_LEFT
 //#define I2C_MASTER_RIGHT
-
-#define DISABLE_JTAG
diff --git a/keyboards/sx60/matrix.c b/keyboards/sx60/matrix.c
index e8e9d6574..58647d13b 100644
--- a/keyboards/sx60/matrix.c
+++ b/keyboards/sx60/matrix.c
@@ -113,13 +113,6 @@ uint8_t matrix_cols(void) {
 }
 
 void matrix_init(void) {
-
-    /* To use PORTF disable JTAG with writing JTD bit twice within four cycles. */
-    #if  (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
-        MCUCR |= _BV(JTD);
-        MCUCR |= _BV(JTD);
-    #endif
-
     mcp23018_status = true;
 
     /* initialize row and col */
diff --git a/keyboards/vitamins_included/matrix.c b/keyboards/vitamins_included/matrix.c
index 1f5071c69..d44e70f08 100644
--- a/keyboards/vitamins_included/matrix.c
+++ b/keyboards/vitamins_included/matrix.c
@@ -136,12 +136,6 @@ bool has_usb(void) {
 
 void matrix_init(void)
 {
-#ifdef DISABLE_JTAG
-  // JTAG disable for PORT F. write JTD bit twice within four cycles.
-  MCUCR |= (1<<JTD);
-  MCUCR |= (1<<JTD);
-#endif
-
     // initialize row and col
 #if (DIODE_DIRECTION == COL2ROW)
     unselect_rows();
diff --git a/keyboards/vitamins_included/rev1/rev1.h b/keyboards/vitamins_included/rev1/rev1.h
index aa5029034..ee97e09cf 100644
--- a/keyboards/vitamins_included/rev1/rev1.h
+++ b/keyboards/vitamins_included/rev1/rev1.h
@@ -1,6 +1,5 @@
 #ifndef REV1_H
 #define REV1_H
-#define DISABLE_JTAG // The keyboard uses PF4, PF5 and PF7, which are used by JTAG.
 #define EE_HANDS // This isn't optional for the vitamins included
 
 #include QMK_KEYBOARD_H
diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c
index 85d252548..8f0257cf6 100644
--- a/tmk_core/common/keyboard.c
+++ b/tmk_core/common/keyboard.c
@@ -130,10 +130,15 @@ static inline bool has_ghost_in_row(uint8_t row, matrix_row_t rowdata)
 #endif
 
 void disable_jtag(void) {
-// To use PORTF disable JTAG with writing JTD bit twice within four cycles.
-#if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
+// To use PF4-7 (PC2-5 on ATmega32A), disable JTAG by writing JTD bit twice within four cycles.
+#if (defined(__AVR_AT90USB646__)  || defined(__AVR_AT90USB647__)  || \
+     defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || \
+     defined(__AVR_ATmega16U4__)  || defined(__AVR_ATmega32U4__))
     MCUCR |= _BV(JTD);
     MCUCR |= _BV(JTD);
+#elif defined(__AVR_ATmega32A__)
+    MCUCSR |= _BV(JTD);
+    MCUCSR |= _BV(JTD);
 #endif
 }
 
@@ -184,7 +189,9 @@ void keyboard_post_init_kb(void) {
  * FIXME: needs doc
  */
 void keyboard_setup(void) {
+#ifndef NO_JTAG_DISABLE
     disable_jtag();
+#endif
     matrix_setup();
     keyboard_pre_init_kb();
 }
diff --git a/tmk_core/common/keyboard.h b/tmk_core/common/keyboard.h
index bf8b71fb7..19f98c74e 100644
--- a/tmk_core/common/keyboard.h
+++ b/tmk_core/common/keyboard.h
@@ -57,8 +57,6 @@ static inline bool IS_RELEASED(keyevent_t event) { return (!IS_NOEVENT(event) &&
     .time = (timer_read() | 1)                          \
 }
 
-void disable_jtag(void);
-
 /* it runs once at early stage of startup before keyboard_init. */
 void keyboard_setup(void);
 /* it runs once after initializing host side protocol, debug and MCU peripherals. */
-- 
2.39.5