7bfcba6e776c9c1036fe52742f8d799582039a28
[goodfet] / firmware / apps / jtag / jtag430.c
1 /*! \file jtag430.c
2   \author Travis Goodspeed <travis at radiantmachines.com>
3   \brief MSP430 JTAG (16-bit)
4 */
5
6 #include "platform.h"
7 #include "command.h"
8 #include "jtag.h"
9
10
11 unsigned int jtag430mode=MSP430X2MODE;
12
13 //! Set the program counter.
14 void jtag430_setpc(unsigned int adr){
15   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
16   jtag_dr_shift16(0x3401);// release low byte
17   jtag_ir_shift8(IR_DATA_16BIT);
18   jtag_dr_shift16(0x4030);//Instruction to load PC
19   CLRTCLK;
20   SETTCLK;
21   jtag_dr_shift16(adr);// Value for PC
22   CLRTCLK;
23   jtag_ir_shift8(IR_ADDR_CAPTURE);
24   SETTCLK;
25   CLRTCLK ;// Now PC is set to "PC_Value"
26   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
27   jtag_dr_shift16(0x2401);// low byte controlled by JTAG
28 }
29
30 //! Halt the CPU
31 void jtag430_haltcpu(){
32   //jtag430_setinstrfetch();
33   
34   jtag_ir_shift8(IR_DATA_16BIT);
35   jtag_dr_shift16(0x3FFF);//JMP $+0
36   
37   CLRTCLK;
38   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
39   jtag_dr_shift16(0x2409);//set JTAG_HALT bit
40   SETTCLK;
41 }
42
43 //! Release the CPU
44 void jtag430_releasecpu(){
45   CLRTCLK;
46   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
47   jtag_dr_shift16(0x2401);
48   jtag_ir_shift8(IR_ADDR_CAPTURE);
49   SETTCLK;
50 }
51
52 //! Read data from address
53 unsigned int jtag430_readmem(unsigned int adr){
54   unsigned int toret;
55   jtag430_haltcpu();
56   
57   CLRTCLK;
58   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
59   
60   if(adr>0xFF)
61     jtag_dr_shift16(0x2409);//word read
62   else
63     jtag_dr_shift16(0x2419);//byte read
64   jtag_ir_shift8(IR_ADDR_16BIT);
65   jtag_dr_shift16(adr);//address
66   jtag_ir_shift8(IR_DATA_TO_ADDR);
67   SETTCLK;
68
69   CLRTCLK;
70   toret=jtag_dr_shift16(0x0000);//16 bit return
71   
72   return toret;
73 }
74
75 //! Write data to address.
76 void jtag430_writemem(unsigned int adr, unsigned int data){
77   CLRTCLK;
78   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
79   if(adr>0xFF)
80     jtag_dr_shift16(0x2408);//word write
81   else
82     jtag_dr_shift16(0x2418);//byte write
83   jtag_ir_shift8(IR_ADDR_16BIT);
84   jtag_dr_shift16(adr);
85   jtag_ir_shift8(IR_DATA_TO_ADDR);
86   jtag_dr_shift16(data);
87   SETTCLK;
88 }
89
90 //! Write data to flash memory.  Must be preconfigured.
91 void jtag430_writeflashword(unsigned int adr, unsigned int data){
92   
93   CLRTCLK;
94   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
95   jtag_dr_shift16(0x2408);//word write
96   jtag_ir_shift8(IR_ADDR_16BIT);
97   jtag_dr_shift16(adr);
98   jtag_ir_shift8(IR_DATA_TO_ADDR);
99   jtag_dr_shift16(data);
100   SETTCLK;
101   
102   //Return to read mode.
103   CLRTCLK;
104   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
105   jtag_dr_shift16(0x2409);
106   
107   /*
108   jtag430_writemem(adr,data);
109   CLRTCLK;
110   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
111   jtag_dr_shift16(0x2409);
112   */
113   
114   //Pulse TCLK
115   jtag430_tclk_flashpulses(35); //35 standard
116 }
117
118 //! Configure flash, then write a word.
119 void jtag430_writeflash(unsigned int adr, unsigned int data){
120   jtag430_haltcpu();
121   
122   //FCTL1=0xA540, enabling flash write
123   jtag430_writemem(0x0128, 0xA540);
124   //FCTL2=0xA540, selecting MCLK as source, DIV=1
125   jtag430_writemem(0x012A, 0xA540);
126   //FCTL3=0xA500, should be 0xA540 for Info Seg A on 2xx chips.
127   jtag430_writemem(0x012C, 0xA500); //all but info flash.
128   
129   //Write the word itself.
130   jtag430_writeflashword(adr,data);
131   
132   //FCTL1=0xA500, disabling flash write
133   jtag430_writemem(0x0128, 0xA500);
134   
135   //jtag430_releasecpu();
136 }
137
138
139
140 //! Power-On Reset
141 void jtag430_por(){
142   unsigned int jtagid;
143
144   // Perform Reset
145   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
146   jtag_dr_shift16(0x2C01); // apply
147   jtag_dr_shift16(0x2401); // remove
148   CLRTCLK;
149   SETTCLK;
150   CLRTCLK;
151   SETTCLK;
152   CLRTCLK;
153   jtagid = jtag_ir_shift8(IR_ADDR_CAPTURE); // get JTAG identifier
154   SETTCLK;
155   
156   jtag430_writemem(0x0120, 0x5A80);   // Diabled Watchdog
157 }
158
159
160
161 #define ERASE_GLOB 0xA50E
162 #define ERASE_ALLMAIN 0xA50C
163 #define ERASE_MASS 0xA506
164 #define ERASE_MAIN 0xA504
165 #define ERASE_SGMT 0xA502
166
167 //! Configure flash, then write a word.
168 void jtag430_eraseflash(unsigned int mode, unsigned int adr, unsigned int count){
169   jtag430_haltcpu();
170   
171   //FCTL1= erase mode
172   jtag430_writemem(0x0128, mode);
173   //FCTL2=0xA540, selecting MCLK as source, DIV=1
174   jtag430_writemem(0x012A, 0xA540);
175   //FCTL3=0xA500, should be 0xA540 for Info Seg A on 2xx chips.
176   jtag430_writemem(0x012C, 0xA500);
177   
178   //Write the erase word.
179   jtag430_writemem(adr, 0x55AA);
180   //Return to read mode.
181   CLRTCLK;
182   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
183   jtag_dr_shift16(0x2409);
184   
185   //Send the pulses.
186   jtag430_tclk_flashpulses(count);
187   
188   //FCTL1=0xA500, disabling flash write
189   jtag430_writemem(0x0128, 0xA500);
190   
191   //jtag430_releasecpu();
192 }
193
194
195 //! Reset the TAP state machine.
196 void jtag430_resettap(){
197   int i;
198   // Settle output
199   SETTDI; //430X2
200   SETTMS;
201   //SETTDI; //classic
202   SETTCK;
203
204   // Navigate to reset state.
205   // Should be at least six.
206   for(i=0;i<4;i++){
207     CLRTCK;
208     SETTCK;
209   }
210
211   // test-logic-reset
212   CLRTCK;
213   CLRTMS;
214   SETTCK;
215   SETTMS;
216   // idle
217
218     
219   /* sacred, by spec.
220      Sometimes this isn't necessary.  */
221   // fuse check
222   CLRTMS;
223   delay(50);
224   SETTMS;
225   CLRTMS;
226   delay(50);
227   SETTMS;
228   /**/
229   
230 }
231
232 //! Start JTAG, take pins
233 void jtag430_start(){
234   jtagsetup();
235   
236   //Known-good starting position.
237   //Might be unnecessary.
238   SETTST;
239   SETRST;
240   delay(0xFFFF);
241
242   #ifndef SBWREWRITE
243   //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG
244   CLRRST;
245   delay(100); //100
246   CLRTST;
247   delay(50);  //50
248   SETTST;
249   delay(50);  //50
250   SETRST;
251   P5DIR&=~RST;
252   delay(0xFFFF);
253   #endif
254   
255   //Perform a reset and disable watchdog.
256   jtag430_por();
257   jtag430_writemem(0x120,0x5a80);//disable watchdog
258   
259   jtag430_haltcpu();
260 }
261
262 //! Start normally, not JTAG.
263 void jtag430_stop(){
264   debugstr("Exiting JTAG.");
265   jtagsetup();
266   
267   //Known-good starting position.
268   //Might be unnecessary.
269   //SETTST;
270   CLRTST;
271   SETRST;
272   delay(0xFFFF);
273   
274   //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG
275   CLRRST;
276   delay(0xFFFF);
277   SETRST;
278   //P5DIR&=~RST;
279   //delay(0xFFFF);
280   
281 }
282
283 //! Set CPU to Instruction Fetch
284 void jtag430_setinstrfetch(){
285   
286   jtag_ir_shift8(IR_CNTRL_SIG_CAPTURE);
287
288   // Wait until instruction fetch state.
289   while(1){
290     if (jtag_dr_shift16(0x0000) & 0x0080)
291       return;
292     CLRTCLK;
293     SETTCLK;
294   }
295 }
296
297
298 //! Handles classic MSP430 JTAG commands.  Forwards others to JTAG.
299 void jtag430handle(unsigned char app,
300                    unsigned char verb,
301                    unsigned long len){
302   unsigned long at;
303   unsigned int i, val;
304   
305   //debugstr("Classic MSP430 handler.");
306   
307   
308   /* FIXME
309    * Sometimes JTAG doesn't init correctly.
310    * This restarts the connection if the masked-rom
311    * chip ID cannot be read.  Should print warning
312    * for testing server.
313    */
314   while((i=jtag430_readmem(0xff0))==0xFFFF){
315     jtag430_start();
316     P1OUT^=1;
317   }
318   P1OUT&=~1;
319     
320   switch(verb){
321   case START:
322     //Enter JTAG mode.
323     jtag430_start();
324     //TAP setup, fuse check
325     jtag430_resettap();
326     
327     cmddata[0]=jtag_ir_shift8(IR_BYPASS);    
328     txdata(app,verb,1);
329
330     break;
331   case STOP:
332     jtag430_stop();
333     txdata(app,verb,0);
334     break;
335   case JTAG430_HALTCPU:
336     jtag430_haltcpu();
337     txdata(app,verb,0);
338     break;
339   case JTAG430_RELEASECPU:
340     jtag430_releasecpu();
341     txdata(app,verb,0);
342     break;
343   case JTAG430_SETINSTRFETCH:
344     jtag430_setinstrfetch();
345     txdata(app,verb,0);
346     break;
347     
348   case JTAG430_READMEM:
349   case PEEK:
350     at=cmddatalong[0];
351     
352     //Fetch large blocks for bulk fetches,
353     //small blocks for individual peeks.
354     if(len>5)
355       len=(cmddataword[2]);//always even.
356     else
357       len=2;
358     len&=~1;//clear lsbit
359     
360     txhead(app,verb,len);
361     for(i=0;i<len;i+=2){
362       jtag430_resettap();
363       val=jtag430_readmem(at);
364       
365       at+=2;
366       serial_tx(val&0xFF);
367       serial_tx((val&0xFF00)>>8);
368     }
369     break;
370   case JTAG430_WRITEMEM:
371   case POKE:
372     jtag430_haltcpu();
373     jtag430_writemem(cmddataword[0],cmddataword[2]);
374     cmddataword[0]=jtag430_readmem(cmddataword[0]);
375     txdata(app,verb,2);
376     break;
377     /*
378   case JTAG430_WRITEFLASH:
379
380     //debugstr("Poking flash memory.");
381     jtag430_writeflash(cmddataword[0],cmddataword[2]);
382     
383     //Try again if failure.
384     //if(cmddataword[2]!=jtag430_readmem(cmddataword[0]))
385     //  jtag430_writeflash(cmddataword[0],cmddataword[2]);
386     
387     //Return result.
388     cmddataword[0]=jtag430_readmem(cmddataword[0]);
389     
390     txdata(app,verb,2);
391     break; */
392   case JTAG430_WRITEFLASH:
393     at=cmddataword[0];
394     
395     for(i=0;i<(len>>1)-2;i++){
396       //debugstr("Poking flash memory.");
397       jtag430_writeflash(at+(i<<1),cmddataword[i+2]);
398       //Reflash if needed.  Try this twice to save grace?
399       if(cmddataword[i]!=jtag430_readmem(at))
400         jtag430_writeflash(at+(i<<1),cmddataword[i+2]);
401     }
402     
403     //Return result of first write as a word.
404     cmddataword[0]=jtag430_readmem(cmddataword[0]);
405     
406     txdata(app,verb,2);
407     break;
408   case JTAG430_ERASEFLASH:
409     jtag430_eraseflash(ERASE_MASS,0xFFFE,0x3000);
410     txdata(app,verb,0);
411     break;
412   case JTAG430_SETPC:
413     jtag430_haltcpu();
414     jtag430_setpc(cmddataword[0]);
415     txdata(app,verb,0);
416     break;
417     
418   case JTAG430_COREIP_ID:
419   case JTAG430_DEVICE_ID:
420     cmddataword[0]=0;
421     cmddataword[1]=0;
422     txdata(app,verb,4);
423     break;
424     
425   default:
426     jtaghandle(app,verb,len);
427   }
428   //jtag430_resettap();  //DO NOT UNCOMMENT
429 }