8cb0fdbc942057f9a49be79f13ca898e7842f43d
[goodfet] / firmware / include / jtag.h
1 /*! \file jtag.h
2   \author Travis Goodspeed
3   \brief JTAG handler functions.
4 */
5
6 #include <signal.h>
7 #include <io.h>
8 #include <iomacros.h>
9
10
11 extern unsigned int drwidth;
12
13 #define MSP430MODE 0
14 #define MSP430XMODE 1
15 #define MSP430X2MODE 2
16 extern unsigned int jtag430mode;
17
18 // Generic Commands
19
20 //! Shift n bytes.
21 unsigned long jtagtransn(unsigned long word,
22                          unsigned int bitcount);
23 //! Shift the address width.
24 unsigned long jtag_dr_shiftadr(unsigned long in);
25 //! Shift 8 bits of the IR.
26 unsigned char jtag_ir_shift8(unsigned char);
27 //! Shift 16 bits of the DR.
28 unsigned int jtag_dr_shift16(unsigned int);
29 //! Shift 20 bits of the DR, MSP430 specific.
30 unsigned long jtag_dr_shift20(unsigned long in);
31 //! Stop JTAG, release pins
32 void jtag_stop();
33
34 //! Setup the JTAG pin directions.
35 void jtagsetup();
36
37 // JTAG430 Commands
38
39 //! Start JTAG, unique to the '430.
40 void jtag430_start();
41 //! Reset the TAP state machine, check the fuse.
42 void jtag430_resettap();
43
44 //! Defined in jtag430asm.S
45 void jtag430_tclk_flashpulses(int);
46
47 //High-level Macros follow
48 //! Write data to address.
49 void jtag430_writemem(unsigned int adr, unsigned int data);
50 //! Read data from address
51 unsigned int jtag430_readmem(unsigned int adr);
52 //! Halt the CPU
53 void jtag430_haltcpu();
54 //! Release the CPU
55 void jtag430_releasecpu();
56 //! Set CPU to Instruction Fetch
57 void jtag430_setinstrfetch();
58 //! Set the program counter.
59 void jtag430_setpc(unsigned int adr);
60 //! Write data to address.
61 void jtag430_writeflash(unsigned int adr, unsigned int data);
62
63 //Pins.  Both SPI and JTAG names are acceptable.
64 //#define SS   BIT0
65 #define MOSI BIT1
66 #define MISO BIT2
67 #define SCK  BIT3
68
69 #define TMS BIT0
70 #define TDI BIT1
71 #define TDO BIT2
72 #define TCK BIT3
73
74 #define TCLK TDI
75
76 //These are not on P5
77 #define RST BIT6
78 #define TST BIT0
79
80 //This could be more accurate.
81 //Does it ever need to be?
82 #define JTAGSPEED 20
83 #define JTAGDELAY(x) delay(x)
84
85 #define SETMOSI P5OUT|=MOSI
86 #define CLRMOSI P5OUT&=~MOSI
87 #define SETCLK P5OUT|=SCK
88 #define CLRCLK P5OUT&=~SCK
89 #define READMISO (P5IN&MISO?1:0)
90 #define SETTMS P5OUT|=TMS
91 #define CLRTMS P5OUT&=~TMS
92 #define SETTCK P5OUT|=TCK
93 #define CLRTCK P5OUT&=~TCK
94 #define SETTDI P5OUT|=TDI
95 #define CLRTDI P5OUT&=~TDI
96
97 #define SETTST P4OUT|=TST
98 #define CLRTST P4OUT&=~TST
99 #define SETRST P2OUT|=RST
100 #define CLRRST P2OUT&=~RST
101
102 #define SETTCLK SETTDI
103 #define CLRTCLK CLRTDI
104
105 extern int savedtclk;
106 #define SAVETCLK savedtclk=P5OUT&TCLK;
107 #define RESTORETCLK if(savedtclk) P5OUT|=TCLK; else P5OUT&=~TCLK
108
109 //Replace every "CLRTCK SETTCK" with this.
110 #define TCKTOCK CLRTCK,SETTCK
111
112
113 //16-bit MSP430 JTAG commands, bit-swapped
114 //Rewrite these with MSP430 prefix.
115 #define IR_CNTRL_SIG_16BIT         0xC8   // 0x13
116 #define IR_CNTRL_SIG_CAPTURE       0x28   // 0x14
117 #define IR_CNTRL_SIG_RELEASE       0xA8   // 0x15
118 // Instructions for the JTAG Fuse
119 #define IR_PREPARE_BLOW            0x44   // 0x22
120 #define IR_EX_BLOW                 0x24   // 0x24
121 // Instructions for the JTAG data register
122 #define IR_DATA_16BIT              0x82   // 0x41
123 #define IR_DATA_QUICK              0xC2   // 0x43
124 // Instructions for the JTAG PSA mode
125 #define IR_DATA_PSA                0x22   // 0x44
126 #define IR_SHIFT_OUT_PSA           0x62   // 0x46
127 // Instructions for the JTAG address register
128 #define IR_ADDR_16BIT              0xC1   // 0x83
129 #define IR_ADDR_CAPTURE            0x21   // 0x84
130 #define IR_DATA_TO_ADDR            0xA1   // 0x85
131 // Bypass instruction
132 #define IR_BYPASS                  0xFF   // 0xFF
133
134 //MSP430X2 unique
135 #define IR_COREIP_ID               0xE8   // 0x17 
136 #define IR_DEVICE_ID               0xE1   // 0x87
137
138 //MSP430 or MSP430X
139 #define MSP430JTAGID 0x89
140 //MSP430X2 only
141 #define MSP430X2JTAGID 0x91
142
143 //! Syncs a POR.
144 unsigned int jtag430x2_syncpor();
145 //! Executes an MSP430X2 POR
146 unsigned int jtag430x2_por();
147 //! Power-On Reset
148 void jtag430_por();
149
150 //JTAG commands
151 #define JTAG_IR_SHIFT 0x80
152 #define JTAG_DR_SHIFT 0x81
153 #define JTAG_DR_SHIFT20 0x91
154
155
156 //JTAG430 commands
157 #define JTAG430_HALTCPU 0xA0
158 #define JTAG430_RELEASECPU 0xA1
159 #define JTAG430_SETINSTRFETCH 0xC1
160 #define JTAG430_SETPC 0xC2
161 #define JTAG430_SETREG 0xD2
162 #define JTAG430_GETREG 0xD3
163 #define JTAG430_WRITEMEM 0xE0
164 #define JTAG430_WRITEFLASH 0xE1
165 #define JTAG430_READMEM 0xE2
166 #define JTAG430_ERASEFLASH 0xE3
167 #define JTAG430_ERASECHECK 0xE4
168 #define JTAG430_VERIFYMEM 0xE5
169 #define JTAG430_BLOWFUSE 0xE6
170 #define JTAG430_ISFUSEBLOWN 0xE7
171 #define JTAG430_COREIP_ID 0xF0
172 #define JTAG430_DEVICE_ID 0xF1