doc: Added MIPS target document
[openocd.git] / doc / manual / target / mips.txt
blob32c40b986a098476a6082691b0a9282565f17b50
1 /** @page targetmips OpenOCD MIPS Targets
3 @section ejatgmem EJTAG Memory Addresses
5 An optional uncached and unmapped debug segment dseg (EJTAG area) appears in the address range
6 0xFFFF FFFF FF20 0000 to 0xFFFF FFFF FF3F FFFF. The dseg segment thereby appears in the kseg part of the
7 compatibility segment, and access to kseg is possible with the dseg segment.
9 The dseg segment is subdivided into dmseg (EJTAG memory) segment and the drseg (EJTAG registers) segment. The
10 dmseg segment is used when the probe services the memory segment. The drseg segment is used when the
11 memory-mapped debug registers are accessed. Table 5-2 shows the subdivision and attributes for the segments.
13 dseg is divided in :
15   - dmseg (0xFFFF FFFF FF20 0000 to 0xFFFF FFFF FF2F FFFF)
16   - drseg (0xFFFF FFFF FF30 0000 to 0xFFFF FFFF FF3F FFFF)
18 Because the dseg segment is serviced exclusively by the EJTAG features, there
19 are no physical address per se. Instead the lower 21 bits of the virtual address select
20 the appropriate reference in either EJTAG memory or registers. References are not mapped through the
21 TLB, nor do the accesses appear on the external system memory interface.
23 Both of this memory segments are Uncached.
25 On debug exception (break) CPU jumps to the beginning of dmseg. This some kind of memory shared
26 between CPU and EJTAG dongle.
28 There CPU stops (correct terminology is : stalls, because it stops it's pipeline), and is waiting for some action of dongle.
30 If the dongle gives it instruction, CPU executes it, augments it's PC to 0xFFFF FFFF FF20 0001 - but it again points to dmseg area,
31 so it stops waiting for next instruction.
33 This will all become clear later, after reading following prerequisite chapters.
35 @section impflags Important flags
37 @subsection     pnnw PNnW
39 Indicates read or write of a pending processor access:
41   - 0 : Read processor access, for a fetch/load access
42   - 1 : Write processor access, for a store access
44 This value is defined only when a processor access is pending.
46 Processor will do the action for us : it can for example read internal state (register values),
47 and send us back the information via EJTAG memory (dmseg), or it can take some data from dmseg and write it into the registers or RAM.
49 Every time when it sees address (i.e. when this address is the part of the opcode it is executing, wether it is instruction or data fetch)
50 that falls into dmseg, processor stalls. That acutally meand that CPU stops it's pipeline and it is waitning for dongle to take some action.
52 CPU is now either waiting for dongle to take some data from dmseg (if we requested for CPU do give us internal state, for example),
53 or it will wait for some data from dongle (if it needs following instruction because it did previous, or if the operand address of the currently executed opcode
54 falls somewhere (anywhere) in dmseg (0xff..ff20000 - 0xff..ff2fffff)).
56 Bit PNnW describes character of CPU access to EJTAG memory (the memry where dongle puts/takes data) - CPU can either READ for it (PNnW == 0) or
57 WRITE to it (PNnW == 1).
59 By reading PNnW bit OpenOCD will know if it has to send (PNnW == 0) or to take (PNnW == 1) data (from dmseg, via dongle).
61 @subsection     pracc PrAcc
63 Indicates a pending processor access and controls finishing of a pending processor access.
65 When read:
67   - 0 : No pending processor access
68   - 1 : Pending processor access
70 A write of 0 finishes a processor access if pending;
71 otherwise operation of the processor is UNDEFINED
72 if the bit is written to 0 when no processor access is
73 pending. A write of 1 is ignored.
75 A successful FASTDATA access will clear this bit.
77 As noted above, on any access to dmseg, processor will stall. It waits for dongle to do some action - either to take or put some data.
78 OpenOCD can figure out which action has to be taken by reading PrAcc bit.
80 Once action from dongle has been done, i.e. after the data is taken/put, OpenOCD can signal to CPU to proceed with executing the instruction.
81 This can be the next instruction (if previous was finished before pending), or the same instruction - if for example CPU was waiting on dongle
82 to give it an operand, because it saw in the instruction opcode that operand address is somewhere in dmseg. That prowoked the CPU to stall (it tried operand fetch to dmseg and stopped),
83 and PNnW bit is 0 (CPU does read from dmseg), and PrAcc is 1 (CPU is pending on dmseg access).
85 @subsection spracc      SPrAcc
87 Shifting in a zero value requests completion of the Fastdata access.
89 The PrAcc bit in the EJTAG Control register is overwritten with zero when the access
90 succeeds. (The access succeeds if PrAcc is one and the operation address is in the legal dmseg segment
91 Fastdata area.)
93 When successful, a one is shifted out. Shifting out a zero indicates a Fastdata access failure.
94 Shifting in a one does not complete the Fastdata access and the PrAcc bit is unchanged. Shifting out a
95 one indicates that the access would have been successful if allowed to complete and a zero indicates
96 the access would not have successfully completed.
98 @section fdreg Fastdata Register (TAP Instruction FASTDATA)
100 The width of the Fastdata register is 1 bit.
102 During a Fastdata access, the Fastdata register is written and read, i.e., a bit is
103 shifted in and a bit is shifted out.
105 Also during a Fastdata access, the Fastdata register value shifted in specifies whether the Fastdata
106 access should be completed or not. The value shifted out is a flag that indicates whether the Fastdata access was
107 successful or not (if completion was requested).
109 @section ejtagacc EJTAG Access Implementation
111 OpenOCD reads/writes data to JTAG via mips_m4k_read_memory() and mips_m4k_write_memory() functions defined in src/target/mips_m4k.c.
112 Internally, these functions call mips32_pracc_read_mem() and mips32_pracc_write_mem() defined in src/target/mips32_pracc.c
114 Let's take for example function mips32_pracc_read_mem32() which describes CPU reads (fetches) from dmseg (EJTAG memory) :
116 @code
117 static const uint32_t code[] = {
118                                                                                                                 /* start: */
119         MIPS32_MTC0(15,31,0),                                                           /* move $15 to COP0 DeSave */
120         MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)),                     /* $15 = MIPS32_PRACC_STACK */
121         MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
122         MIPS32_SW(8,0,15),                                                                      /* sw $8,($15) */
123         MIPS32_SW(9,0,15),                                                                      /* sw $9,($15) */
124         MIPS32_SW(10,0,15),                                                                     /* sw $10,($15) */
125         MIPS32_SW(11,0,15),                                                                     /* sw $11,($15) */
127         MIPS32_LUI(8,UPPER16(MIPS32_PRACC_PARAM_IN)),           /* $8 = MIPS32_PRACC_PARAM_IN */
128         MIPS32_ORI(8,8,LOWER16(MIPS32_PRACC_PARAM_IN)),
129         MIPS32_LW(9,0,8),                                                                       /* $9 = mem[$8]; read addr */
130         MIPS32_LW(10,4,8),                                                                      /* $10 = mem[$8 + 4]; read count */
131         MIPS32_LUI(11,UPPER16(MIPS32_PRACC_PARAM_OUT)),         /* $11 = MIPS32_PRACC_PARAM_OUT */
132         MIPS32_ORI(11,11,LOWER16(MIPS32_PRACC_PARAM_OUT)),
133                                                                                                                 /* loop: */
134         MIPS32_BEQ(0,10,8),                                                                     /* beq 0, $10, end */
135         MIPS32_NOP,
137         MIPS32_LW(8,0,9),                                                                       /* lw $8,0($9), Load $8 with the word @mem[$9] */
138         MIPS32_SW(8,0,11),                                                                      /* sw $8,0($11) */
140         MIPS32_ADDI(10,10,NEG16(1)),                                            /* $10-- */
141         MIPS32_ADDI(9,9,4),                                                                     /* $1 += 4 */
142         MIPS32_ADDI(11,11,4),                                                           /* $11 += 4 */
144         MIPS32_B(NEG16(8)),                                                                     /* b loop */
145         MIPS32_NOP,
146                                                                         /* end: */
147         MIPS32_LW(11,0,15),                                                                     /* lw $11,($15) */
148         MIPS32_LW(10,0,15),                                                                     /* lw $10,($15) */
149         MIPS32_LW(9,0,15),                                                                      /* lw $9,($15) */
150         MIPS32_LW(8,0,15),                                                                      /* lw $8,($15) */
151         MIPS32_B(NEG16(27)),                                                            /* b start */
152         MIPS32_MFC0(15,31,0),                                                           /* move COP0 DeSave to $15 */
154 @endcode
156 We have to pass this code to CPU via dongle via dmseg.
158 After debug exception CPU will find itself stalling at the begining of the dmseg. It waits for the first instruction from dongle.
159 This is MIPS32_MTC0(15,31,0), so CPU saves C0 and continues to addr 0xFF20 0001, which falls also to dmseg, so it stalls.
160 Dongle proceeds giving to CPU one by one instruction in this manner.
162 However, things are not so simple. If you take a look at the program, you will see that some instructions take operands. If it has to take
163 operand from the address in dmseg, CPU will stall witing for the dongle to do the action of passing the operand and signal this by putting PrAcc to 0.
164 If this operand is somewhere in RAM, CPU will not stall (it stalls only on dmseg), but it will just take it and proceed to nex instruction. But since PC for next instruction
165 points to dmseg, it will stall, so that dongle can pass next instruction.
167 Some instuctions are jumps (if these are jumps in dmseg addr, CPU will jump and then stall. If this is jump to some address in RAM, CPU will jump and just proceed -
168 will not stall on addresses in RAM).
170 To have information about CPU is currently (does it stalls wanting on operand or it jumped somewhere waiting for next instruction),
171 OpenOCD has to call TAP ADDRESS instruction, which will ask CPU to give us his address within EJTAG memory :
173 @code
174 address = data = 0;
175 mips_ejtag_set_instr(ejtag_info, EJTAG_INST_ADDRESS);
176 mips_ejtag_drscan_32(ejtag_info, &address);
177 @endcode
179 And then, upon the results, we can conclude where it is in our code so far, so we can give it what it wants next :
181 @code
182 if ((address >= MIPS32_PRACC_PARAM_IN)
183         && (address <= MIPS32_PRACC_PARAM_IN + ctx->num_iparam * 4))
185         offset = (address - MIPS32_PRACC_PARAM_IN) / 4;
186         data = ctx->local_iparam[offset];
188 else if ((address >= MIPS32_PRACC_PARAM_OUT)
189         && (address <= MIPS32_PRACC_PARAM_OUT + ctx->num_oparam * 4))
191         offset = (address - MIPS32_PRACC_PARAM_OUT) / 4;
192         data = ctx->local_oparam[offset];
194 else if ((address >= MIPS32_PRACC_TEXT)
195         && (address <= MIPS32_PRACC_TEXT + ctx->code_len * 4))
197         offset = (address - MIPS32_PRACC_TEXT) / 4;
198         data = ctx->code[offset];
200 else if (address == MIPS32_PRACC_STACK)
202         /* save to our debug stack */
203         data = ctx->stack[--ctx->stack_offset];
205 else
207         /* TODO: send JMP 0xFF200000 instruction.
208                 Hopefully processor jump back to start of debug vector */
209         data = 0;
210         LOG_ERROR("Error reading unexpected address 0x%8.8" PRIx32 "", address);
211         return ERROR_JTAG_DEVICE_ERROR;
213 @endcode
215 i.e. if CPU is stalling on addresses in dmseg that are reserved for input parameters, we can conclude that it actually tried to take (read)
216 parametar from there, and saw that address of param falls in dmseg, so it stopped. Obviously, now dongle have to give to it operand.
218 Similarly, mips32_pracc_exec_write() describes CPU writes into EJTAG memory (dmseg).
219 Obvioulsy, code is RO, and CPU can change only parameters :
221 @code
222 mips_ejtag_set_instr(ctx->ejtag_info, EJTAG_INST_DATA);
223 mips_ejtag_drscan_32(ctx->ejtag_info, &data);
225 /* Clear access pending bit */
226 ejtag_ctrl = ejtag_info->ejtag_ctrl & ~EJTAG_CTRL_PRACC;
227 mips_ejtag_set_instr(ctx->ejtag_info, EJTAG_INST_CONTROL);
228 mips_ejtag_drscan_32(ctx->ejtag_info, &ejtag_ctrl);
230 //jtag_add_clocks(5);
231 jtag_execute_queue();
233 if ((address >= MIPS32_PRACC_PARAM_IN)
234         && (address <= MIPS32_PRACC_PARAM_IN + ctx->num_iparam * 4))
236         offset = (address - MIPS32_PRACC_PARAM_IN) / 4;
237         ctx->local_iparam[offset] = data;
239 else if ((address >= MIPS32_PRACC_PARAM_OUT)
240         && (address <= MIPS32_PRACC_PARAM_OUT + ctx->num_oparam * 4))
242         offset = (address - MIPS32_PRACC_PARAM_OUT) / 4;
243         ctx->local_oparam[offset] = data;
245 else if (address == MIPS32_PRACC_STACK)
247         /* save data onto our stack */
248         ctx->stack[ctx->stack_offset++] = data;
250 else
252         LOG_ERROR("Error writing unexpected address 0x%8.8" PRIx32 "", address);
253         return ERROR_JTAG_DEVICE_ERROR;
255 @endcode
257 CPU loops here :
259 @code
260 while (1)
262         if ((retval = wait_for_pracc_rw(ejtag_info, &ejtag_ctrl)) != ERROR_OK)
263                 return retval;
265         address = data = 0;
266         mips_ejtag_set_instr(ejtag_info, EJTAG_INST_ADDRESS);
267         mips_ejtag_drscan_32(ejtag_info, &address);
269         /* Check for read or write */
270         if (ejtag_ctrl & EJTAG_CTRL_PRNW)
271         {
272                 if ((retval = mips32_pracc_exec_write(&ctx, address)) != ERROR_OK)
273                         return retval;
274         }
275         else
276         {
277                 /* Check to see if its reading at the debug vector. The first pass through
278                  * the module is always read at the vector, so the first one we allow.  When
279                  * the second read from the vector occurs we are done and just exit. */
280                 if ((address == MIPS32_PRACC_TEXT) && (pass++))
281                 {
282                         break;
283                 }
285                 if ((retval = mips32_pracc_exec_read(&ctx, address)) != ERROR_OK)
286                         return retval;
287         }
289         if (cycle == 0)
290                 break;
292 @endcode
294 and using presented R (mips32_pracc_exec_read()) and W (mips32_pracc_exec_write()) functions it reads in the code (RO) and reads and writes operands (RW).
296 @section fdimpl OpenOCD FASTDATA Implementation
298 OpenOCD FASTDATA write function, mips32_pracc_fastdata_xfer() is called from bulk_write_memory callback, which writes a count items of 4 bytes
299 to the memory of a target at the an address given.  Because it operates only on whole words, this should be faster than target_write_memory().
301 In order to implement FASTDATA write, mips32_pracc_fastdata_xfer() uses the following handler :
303 @code
304 uint32_t handler_code[] = {
305         /* caution when editing, table is modified below */
306         /* r15 points to the start of this code */
307         MIPS32_SW(8,MIPS32_FASTDATA_HANDLER_SIZE - 4,15),
308         MIPS32_SW(9,MIPS32_FASTDATA_HANDLER_SIZE - 8,15),
309         MIPS32_SW(10,MIPS32_FASTDATA_HANDLER_SIZE - 12,15),
310         MIPS32_SW(11,MIPS32_FASTDATA_HANDLER_SIZE - 16,15),
311         /* start of fastdata area in t0 */
312         MIPS32_LUI(8,UPPER16(MIPS32_PRACC_FASTDATA_AREA)),
313         MIPS32_ORI(8,8,LOWER16(MIPS32_PRACC_FASTDATA_AREA)),
314         MIPS32_LW(9,0,8),                                                               /* start addr in t1 */
315         MIPS32_LW(10,0,8),                                                              /* end addr to t2 */
316                                                                                                         /* loop: */
317         /* 8 */ MIPS32_LW(11,0,0),                                              /* lw t3,[t8 | r9] */
318         /* 9 */ MIPS32_SW(11,0,0),                                              /* sw t3,[r9 | r8] */
319         MIPS32_BNE(10,9,NEG16(3)),                                              /* bne $t2,t1,loop */
320         MIPS32_ADDI(9,9,4),                                                             /* addi t1,t1,4 */
322         MIPS32_LW(8,MIPS32_FASTDATA_HANDLER_SIZE - 4,15),
323         MIPS32_LW(9,MIPS32_FASTDATA_HANDLER_SIZE - 8,15),
324         MIPS32_LW(10,MIPS32_FASTDATA_HANDLER_SIZE - 12,15),
325         MIPS32_LW(11,MIPS32_FASTDATA_HANDLER_SIZE - 16,15),
327         MIPS32_LUI(15,UPPER16(MIPS32_PRACC_TEXT)),
328         MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_TEXT)),
329         MIPS32_JR(15),                                                                  /* jr start */
330         MIPS32_MFC0(15,31,0),                                                   /* move COP0 DeSave to $15 */
332 @endcode
334 In the begining and the end of the handler we have fuction prologue (save the regs that will be clobbered) and epilogue (restore regs),
335 and in the very end, after all the xfer have been done, we do jump to the MIPS32_PRACC_TEXT address, i.e. Debug Exception Vector location.
336 We will use this fact (that we came back to MIPS32_PRACC_TEXT)  to verify later if all the handler is executed (because when in RAM,
337 processor do not stall - it executes all instructions untill one of them do not demand access to dmseg (if one of it's opernads is there)).
339 This handler is put into the RAM and executed from there, and not instruction by  instruction, like in previous simple write
340 (mips_m4k_write_memory()) and read (mips_m4k_read_memory()) functions.
342 N.B. When it is executing this code in RAM, CPU will not stall on instructions, but execute all until it comes to the :
344 @code
345 MIPS32_LW(9,0,8) /* start addr in t1 */
346 @endcode
348 and there it will stall - because it will see that one of the operands have to be fetched from dmseg (EJTAG memory, in this case FASTDATA memory segment).
350 This handler is loaded in the RAM, ath the reserved location "work_area". This work_area is configured in OpenOCD configuration script and should be selected
351 in that way that it is not clobbered (overwritten) by data we want to write-in using FASTDATA.
353 What is executed instruction by instruction which is passed by dongle (via EJATG memory) is small jump code, which jumps at the handler in RAM.
354 CPU stalls on dmseg when receiving these jmp_code instructions, but once it jumps in RAM, CPU do not stall anymore and executes bunch of handler instructions.
355 Untill it comes to the first instruction which has an operand in FASTDATA area. There it stalls and waits on action from probe.
356 It happens actually when CPU comes to this loop :
358 @code
359 MIPS32_LW(9,0,8),                                                               /* start addr in t1 */
360 MIPS32_LW(10,0,8),                                                              /* end addr to t2 */
361                                                                                                 /* loop: */
362 /* 8 */ MIPS32_LW(11,0,0),                                              /* lw t3,[t8 | r9] */
363 /* 9 */ MIPS32_SW(11,0,0),                                              /* sw t3,[r9 | r8] */
364 MIPS32_BNE(10,9,NEG16(3)),                                              /* bne $t2,t1,loop */
365 @endcode
367 and then it stalls because operand in r8 points to FASTDATA area.
369 OpenOCD first verifies that CPU came to this place by :
371 @code
372 /* next fetch to dmseg should be in FASTDATA_AREA, check */
373 address = 0;
374 mips_ejtag_set_instr(ejtag_info, EJTAG_INST_ADDRESS);
375 mips_ejtag_drscan_32(ejtag_info, &address);
377 if (address != MIPS32_PRACC_FASTDATA_AREA)
378         return ERROR_FAIL;
379 @endcode
381 and then passes to CPU start and end address of the loop region for handler in RAM.
383 In the loop in handler, CPU sees that it has to take and operand from FSTDATA area (to write it to the dst in RAM after), and so it stalls, putting PrAcc to "1".
384 OpenOCD fills the data via this loop :
386 @code
387 for (i = 0; i < count; i++)
389         /* Send the data out using fastdata (clears the access pending bit) */
390         mips_ejtag_set_instr(ejtag_info, EJTAG_INST_FASTDATA);
391         if ((retval = mips_ejtag_fastdata_scan(ejtag_info, write_t, buf++)) != ERROR_OK)
392                 return retval;
394 @endcode
396 Each time when OpenOCD fills data to CPU (via dongle, via dmseg), CPU takes it and proceeds in executing the endler. However, since handler is in a assembly loop,
397 CPU comes to next instruction which also fetches data from FASTDATA area. So it stalls.
398 Then OpenOCD fills the data again, from it's (OpenOCD's) loop. And this game continues untill all the data has been filled.
400 After the last data has beend given to CPU it sees that it reached the end address, so it proceeds with next instruction. However, rhis instruction do not point into dmseg, so
401 CPU executes bunch of handler instructions (all prologue) and in the end jumps to MIPS32_PRACC_TEXT address.
403 On it's side, OpenOCD checks in CPU has jumped back to MIPS32_PRACC_TEXT, which is the confirmation that it correclty executed all the rest of the handler in RAM,
404 and that is not stuck somewhere in the RAM, or stalling on some acces in dmseg - that would be an error :
406 @code
407 address = 0;
408 mips_ejtag_set_instr(ejtag_info, EJTAG_INST_ADDRESS);
409 mips_ejtag_drscan_32(ejtag_info, &address);
411 if (address != MIPS32_PRACC_TEXT)
412         LOG_ERROR("mini program did not return to start");
413 @endcode
415 @section fdejtagspec EJTAG spec on FASTDATA access
417 The width of the Fastdata register is 1 bit. During a Fastdata access, the Fastdata register is written and read, i.e., a bit
418 is shifted in and a bit is shifted out. During a Fastdata access, the Fastdata register value shifted in specifies whether
419 the Fastdata access should be completed or not. The value shifted out is a flag that indicates whether the Fastdata
420 access was successful or not (if completion was requested).
422 The FASTDATA access is used for efficient block transfers between dmseg (on the probe) and target memory (on the
423 processor). An "upload" is defined as a sequence of processor loads from target memory and stores to dmseg. A
424 "download" is a sequence of processor loads from dmseg and stores to target memory. The "Fastdata area" specifies
425 the legal range of dmseg addresses (0xFF20.0000 - 0xFF20.000F) that can be used for uploads and downloads. The
426 Data + Fastdata registers (selected with the FASTDATA instruction) allow efficient completion of pending Fastdata
427 area accesses.
428 During Fastdata uploads and downloads, the processor will stall on accesses to the Fastdata area. The PrAcc (processor
429 access pending bit) will be 1 indicating the probe is required to complete the access. Both upload and download
430 accesses are attempted by shifting in a zero SPrAcc value (to request access completion) and shifting out SPrAcc to
431 see if the attempt will be successful (i.e., there was an access pending and a legal Fastdata area address was used).
432 Downloads will also shift in the data to be used to satisfy the load from dmseg’s Fastdata area, while uploads will
433 shift out the data being stored to dmseg’s Fastdata area.
434 As noted above, two conditions must be true for the Fastdata access to succeed. These are:
436  - PrAcc must be 1, i.e., there must be a pending processor access.
437  - The Fastdata operation must use a valid Fastdata area address in dmseg (0xFF20.0000 to 0xFF20.000F).
439 Basically, because FASTDATA area in dmseg is 16 bytes, we transfer (0xFF20.0000 - 0xFF20.000F)
440 FASTDATA scan TAP instruction selects the Data and the Fastdata registers at once.
442 They come in order :
443 TDI -> | Data register| -> | Fastdata register | -> TDO
445 FASTDATA register is 1-bit width register. It takes in SPrAcc bit which should be shifted first,
446 followed by 32 bit of data.
448 Scan width of FASTDTA is 33 bits in total : 33 bits are shifted in and 33 bits are shifted out.
450 First bit that is shifted out is SPrAcc that comes out of Fastdata register and should give us status on FATSDATA write we want to do.
452 @section fdcheck OpenOCD misses FASTDATA check
454 Download flow (probe -> target block transfer) :
456 1) Probe transfer target execution to a loop in target memory doing a fixed number of "loads" to fastdata area of dmseg (and stores to the target download destination.)
458 2) Probe loops attempting to satisfy the loads "expected" from the target.
459    On FASTDATA access "successful" move on to next "load".
460    On FASTDATA access "failure" repeat until "successful" or timeout.
461    (A "failure" is an attempt to satisfy an access when none are pending.)
463 Note: A failure may have a recoverable (and even expected) cause like slow target execution of the load loop. Other failures may be due to unexpected more troublesome causes like an exception while in debug mode or a target hang on a bad target memory access.
465 Shifted out SPrAcc bit inform us that there was CPU access pendingand that it can be complete.
467 Basically, we should do following procedure :
469  - Download (dongle -> CPU) :
470 You shift "download" DATA and FASTDATA[SPrAcc] = 0 (33 bit scan) into the target. If the value of FASTDATA[SPrAcc] shifted out is "1" then an access was pending when you started the scan and it is now complete.
472 If SPrAcc is 0 then no access was pending to the fastdata area. (Repeat attempt to complete the access you expect for this data word. Timeout if you think the access is "long overdue" as something unexpected has happened.)
474  - Upload (CPU -> dongle) :
475 You shift "dummy" DATA and FASTDATA[SPrAcc] = 0 (33 bit scan) into the target. If the value of FASTDATA[SPrAcc] shifted out is "1" then an access was pending when you started the scan and it is now complete. The "upload" is the DATA shifted out of the target.
477 If SPrAcc is 0 then no access was pending to the fastdata area. (Repeat attempt to complete the access you expect for this data word. Timeout if you think the access is "long overdue" as something unexpected has happened.)
479 Basically, if checking first (before scan) if CPU is pending on FASTDATA access (PrAcc is "1"), like this
481 @code
482 wait(ready);
483 do_scan();
484 @endcode
486 which is inefficient, we should do it like this :
488 @code
489 BEGIN :
490         do_scan();
491         if (!was_ready)
492         goto BEGIN;
493 @endcode
495 by checking SPrAcc that we shifted out.
497 If some FASTDATA write fails, OpenOCD will continue with it's loop (on the host side), but CPU will rest pending (on the target side)
498 waiting for correct FASTDATA write.
500 Since OpenOCD goes ahead, it will eventually finish it's loop, and proceede to check if CPU took all the data. But since CPU did not took all the data,
501 it is still turns in handler's loop in RAM, stalling on Fastdata area so this check :
503 @code
504 address = 0;
505 mips_ejtag_set_instr(ejtag_info, EJTAG_INST_ADDRESS);
506 retval = mips_ejtag_drscan_32(ejtag_info, &address);
507 if (retval != ERROR_OK)
508         return retval;
510 if (address != MIPS32_PRACC_TEXT)
511         LOG_ERROR("mini program did not return to start");
512 @endcode
514 fails, and that gives us enough information of the failure.
516 In this case, we can lower the JTAG frquency and try again, bacuse most probable reason of this failure is that we tried FASTDATA upload before CPU arrived to rise PrAcc (i.e. before it was pending on access).
517 However, the reasons for failure might be numerous : reset, exceptions which can occur in debug mode, bus hangs, etc.
519 If lowering the JTAG freq does not work either, we can fall back to more robust solution with patch posted below.
521 To summarize, FASTDATA communication goes as following :
523 -# CPU jumps to Debug Exception Vector Location 0xFF200200 in dmseg and it stalls, pending and waiting for EJTAG to give it first debug instruction and signall it by putting PrAcc to "0"
524 -# When PrAcc goes to "0" CPU execute one opcode sent by EJTAG via DATA reg. Then it pends on next access, waiting for PrAcc to be put to "0" again
525 -# Following this game, OpenOCD first loads handler code in RAM, and then sends the jmp_code - instruction by instruction via DATA reg, which redirects CPU to handler previously set up in RAM
526 -# Once in RAM CPU does not pend on any instruction, but it executes all handler instructions untill first "fetch" to Fastdata area - then it stops and pends.
527 -# So - when it comes to any instruction (opcode) in this handler in RAM which reads (or writes) to Fastdata area (0xF..F20.0000 to 0xF..F20.000F), CPU stops (i.e. stalls access).
528    I.e. it stops on this lw opcode and waits to FASTDATA TAP command from the probe.
529 -# CPU continues only if OpenOCD shifted in SPrAcc "0" (and if the PrAcc was "1"). It shifts-out "1" to tell us that it was OK (processor was stalled, so it can complete the access),
530    and that it continued execution of the handler in RAM.
531 -# If PrAcc was not "1" CPU will not continue (go to next instruction), but will shift-out "0" and keep stalling on the same instruction of my handler in RAM.
532 -# When Fastdata loop is finished, CPU executes all following hadler instructions in RAM (prologue).
533 -# In the end of my handler in RAM, I jumps back to begining of Debug Exception Vector Location 0xFF200200 in dmseg.
534 -# When it jumps back to 0xFF200200 in dmseg processor stops and pends, waiting for OpenOCD to send it instruction via DATA reg and signal it by putting PrAcc to "0".