35874a897e797c21fd22b77547f33aa5f5829498
[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   //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG
243   CLRRST;
244   delay(100); //100
245   CLRTST;
246   delay(50);  //50
247   SETTST;
248   delay(50);  //50
249   SETRST;
250   P5DIR&=~RST;
251   delay(0xFFFF);
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     txdata(app,verb,0);
326     break;
327   case STOP:
328     jtag430_stop();
329     txdata(app,verb,0);
330     break;
331   case JTAG430_HALTCPU:
332     jtag430_haltcpu();
333     txdata(app,verb,0);
334     break;
335   case JTAG430_RELEASECPU:
336     jtag430_releasecpu();
337     txdata(app,verb,0);
338     break;
339   case JTAG430_SETINSTRFETCH:
340     jtag430_setinstrfetch();
341     txdata(app,verb,0);
342     break;
343     
344   case JTAG430_READMEM:
345   case PEEK:
346     at=cmddatalong[0];
347     
348     //Fetch large blocks for bulk fetches,
349     //small blocks for individual peeks.
350     if(len>5)
351       len=(cmddataword[2]);//always even.
352     else
353       len=2;
354     len&=~1;//clear lsbit
355     
356     txhead(app,verb,len);
357     for(i=0;i<len;i+=2){
358       jtag430_resettap();
359       val=jtag430_readmem(at);
360       
361       at+=2;
362       serial_tx(val&0xFF);
363       serial_tx((val&0xFF00)>>8);
364     }
365     break;
366   case JTAG430_WRITEMEM:
367   case POKE:
368     jtag430_haltcpu();
369     jtag430_writemem(cmddataword[0],cmddataword[2]);
370     cmddataword[0]=jtag430_readmem(cmddataword[0]);
371     txdata(app,verb,2);
372     break;
373     /*
374   case JTAG430_WRITEFLASH:
375
376     //debugstr("Poking flash memory.");
377     jtag430_writeflash(cmddataword[0],cmddataword[2]);
378     
379     //Try again if failure.
380     //if(cmddataword[2]!=jtag430_readmem(cmddataword[0]))
381     //  jtag430_writeflash(cmddataword[0],cmddataword[2]);
382     
383     //Return result.
384     cmddataword[0]=jtag430_readmem(cmddataword[0]);
385     
386     txdata(app,verb,2);
387     break; */
388   case JTAG430_WRITEFLASH:
389     at=cmddataword[0];
390     
391     for(i=0;i<(len>>1)-2;i++){
392       //debugstr("Poking flash memory.");
393       jtag430_writeflash(at+(i<<1),cmddataword[i+2]);
394       //Reflash if needed.  Try this twice to save grace?
395       if(cmddataword[i]!=jtag430_readmem(at))
396         jtag430_writeflash(at+(i<<1),cmddataword[i+2]);
397     }
398     
399     //Return result of first write as a word.
400     cmddataword[0]=jtag430_readmem(cmddataword[0]);
401     
402     txdata(app,verb,2);
403     break;
404   case JTAG430_ERASEFLASH:
405     jtag430_eraseflash(ERASE_MASS,0xFFFE,0x3000);
406     txdata(app,verb,0);
407     break;
408   case JTAG430_SETPC:
409     jtag430_haltcpu();
410     jtag430_setpc(cmddataword[0]);
411     txdata(app,verb,0);
412     break;
413     
414   case JTAG430_COREIP_ID:
415   case JTAG430_DEVICE_ID:
416     cmddataword[0]=0;
417     cmddataword[1]=0;
418     txdata(app,verb,4);
419     break;
420     
421   default:
422     jtaghandle(app,verb,len);
423   }
424   //jtag430_resettap();  //DO NOT UNCOMMENT
425 }