e1c6dc62437cd92e3def074a829c3fcb2e14970c
[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   TCKTOCK;
203
204   // Navigate to reset state.
205   // Should be at least six.
206   for(i=0;i<4;i++){
207     TCKTOCK;
208   }
209
210   // test-logic-reset
211   CLRTMS;
212   TCKTOCK;
213   SETTMS;
214   // idle
215
216     
217   /* sacred, by spec.
218      Sometimes this isn't necessary.  */
219   // fuse check
220   CLRTMS;
221   delay(50);
222   SETTMS;
223   CLRTMS;
224   delay(50);
225   SETTMS;
226   /**/
227   
228 }
229
230 //! Start JTAG, take pins
231 void jtag430_start(){
232   jtagsetup();
233   
234   //Known-good starting position.
235   //Might be unnecessary.
236   SETTST;
237   SETRST;
238   delay(0xFFFF);
239
240   #ifndef SBWREWRITE
241   //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG
242   CLRRST;
243   delay(100); //100
244   CLRTST;
245   delay(50);  //50
246   SETTST;
247   delay(50);  //50
248   SETRST;
249   P5DIR&=~RST;
250   delay(0xFFFF);
251   #endif
252   
253   //Perform a reset and disable watchdog.
254   jtag430_por();
255   jtag430_writemem(0x120,0x5a80);//disable watchdog
256   
257   jtag430_haltcpu();
258 }
259
260 //! Start normally, not JTAG.
261 void jtag430_stop(){
262   debugstr("Exiting JTAG.");
263   jtagsetup();
264   
265   //Known-good starting position.
266   //Might be unnecessary.
267   //SETTST;
268   CLRTST;
269   SETRST;
270   delay(0xFFFF);
271   
272   //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG
273   CLRRST;
274   delay(0xFFFF);
275   SETRST;
276   //P5DIR&=~RST;
277   //delay(0xFFFF);
278   
279 }
280
281 //! Set CPU to Instruction Fetch
282 void jtag430_setinstrfetch(){
283   
284   jtag_ir_shift8(IR_CNTRL_SIG_CAPTURE);
285
286   // Wait until instruction fetch state.
287   while(1){
288     if (jtag_dr_shift16(0x0000) & 0x0080)
289       return;
290     CLRTCLK;
291     SETTCLK;
292   }
293 }
294
295
296 //! Handles classic MSP430 JTAG commands.  Forwards others to JTAG.
297 void jtag430handle(unsigned char app,
298                    unsigned char verb,
299                    unsigned long len){
300   unsigned long at;
301   unsigned int i, val;
302   
303   //debugstr("Classic MSP430 handler.");
304   
305   
306   /* FIXME
307    * Sometimes JTAG doesn't init correctly.
308    * This restarts the connection if the masked-rom
309    * chip ID cannot be read.  Should print warning
310    * for testing server.
311    */
312   while((i=jtag430_readmem(0xff0))==0xFFFF){
313     jtag430_start();
314     P1OUT^=1;
315   }
316   P1OUT&=~1;
317     
318   switch(verb){
319   case START:
320     //Enter JTAG mode.
321     jtag430_start();
322     //TAP setup, fuse check
323     jtag430_resettap();
324     
325     cmddata[0]=jtag_ir_shift8(IR_BYPASS);    
326     txdata(app,verb,1);
327
328     break;
329   case STOP:
330     jtag430_stop();
331     txdata(app,verb,0);
332     break;
333   case JTAG430_HALTCPU:
334     jtag430_haltcpu();
335     txdata(app,verb,0);
336     break;
337   case JTAG430_RELEASECPU:
338     jtag430_releasecpu();
339     txdata(app,verb,0);
340     break;
341   case JTAG430_SETINSTRFETCH:
342     jtag430_setinstrfetch();
343     txdata(app,verb,0);
344     break;
345     
346   case JTAG430_READMEM:
347   case PEEK:
348     at=cmddatalong[0];
349     
350     //Fetch large blocks for bulk fetches,
351     //small blocks for individual peeks.
352     if(len>5)
353       len=(cmddataword[2]);//always even.
354     else
355       len=2;
356     len&=~1;//clear lsbit
357     
358     txhead(app,verb,len);
359     for(i=0;i<len;i+=2){
360       jtag430_resettap();
361       val=jtag430_readmem(at);
362       
363       at+=2;
364       serial_tx(val&0xFF);
365       serial_tx((val&0xFF00)>>8);
366     }
367     break;
368   case JTAG430_WRITEMEM:
369   case POKE:
370     jtag430_haltcpu();
371     jtag430_writemem(cmddataword[0],cmddataword[2]);
372     cmddataword[0]=jtag430_readmem(cmddataword[0]);
373     txdata(app,verb,2);
374     break;
375     /*
376   case JTAG430_WRITEFLASH:
377
378     //debugstr("Poking flash memory.");
379     jtag430_writeflash(cmddataword[0],cmddataword[2]);
380     
381     //Try again if failure.
382     //if(cmddataword[2]!=jtag430_readmem(cmddataword[0]))
383     //  jtag430_writeflash(cmddataword[0],cmddataword[2]);
384     
385     //Return result.
386     cmddataword[0]=jtag430_readmem(cmddataword[0]);
387     
388     txdata(app,verb,2);
389     break; */
390   case JTAG430_WRITEFLASH:
391     at=cmddataword[0];
392     
393     for(i=0;i<(len>>1)-2;i++){
394       //debugstr("Poking flash memory.");
395       jtag430_writeflash(at+(i<<1),cmddataword[i+2]);
396       //Reflash if needed.  Try this twice to save grace?
397       if(cmddataword[i]!=jtag430_readmem(at))
398         jtag430_writeflash(at+(i<<1),cmddataword[i+2]);
399     }
400     
401     //Return result of first write as a word.
402     cmddataword[0]=jtag430_readmem(cmddataword[0]);
403     
404     txdata(app,verb,2);
405     break;
406   case JTAG430_ERASEFLASH:
407     jtag430_eraseflash(ERASE_MASS,0xFFFE,0x3000);
408     txdata(app,verb,0);
409     break;
410   case JTAG430_SETPC:
411     jtag430_haltcpu();
412     jtag430_setpc(cmddataword[0]);
413     txdata(app,verb,0);
414     break;
415     
416   case JTAG430_COREIP_ID:
417   case JTAG430_DEVICE_ID:
418     cmddataword[0]=0;
419     cmddataword[1]=0;
420     txdata(app,verb,4);
421     break;
422     
423   default:
424     jtaghandle(app,verb,len);
425   }
426   //jtag430_resettap();  //DO NOT UNCOMMENT
427 }