summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/arch/mips/common/src/dev_flashop_engine.S
blob: 29ade6a8a4def846c7f9883ccc18315fc5ffb25e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *  
    *  Flash "self-write" module	File: dev_flashop_engine.S
    *  
    *  This module takes care of the case of writing to the flash
    *  memory that CFE is currently reading its code from.
    *
    *  Note: this code is written to be position-independent, even
    *  for non-PIC versions of CFE!  It will be copied (with memcpy)
    *  into the heap for execution.
    *  
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *  
    *********************************************************************  
    *
    *  Copyright 2000,2001,2002,2003
    *  Broadcom Corporation. All rights reserved.
    *  
    *  This software is furnished under license and may be used and 
    *  copied only in accordance with the following terms and 
    *  conditions.  Subject to these conditions, you may download, 
    *  copy, install, use, modify and distribute modified or unmodified 
    *  copies of this software in source and/or binary form.  No title 
    *  or ownership is transferred hereby.
    *  
    *  1) Any source code used, modified or distributed must reproduce 
    *     and retain this copyright notice and list of conditions 
    *     as they appear in the source file.
    *  
    *  2) No right is granted to use any trade name, trademark, or 
    *     logo of Broadcom Corporation.  The "Broadcom Corporation" 
    *     name may not be used to endorse or promote products derived 
    *     from this software without the prior written permission of 
    *     Broadcom Corporation.
    *  
    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 
    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 
    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 
    *     THE POSSIBILITY OF SUCH DAMAGE.
    ********************************************************************* */

#include "sbmips.h"
#include "mipsmacros.h"

#include "bsp_config.h"
#include "dev_newflash.h"

/*  *********************************************************************
    *  Macros
    ********************************************************************* */

#define FLASHCMD_8(base,offset,value) \
	li	t0,value ;	      \
	sb	t0,offset(base)

#define FLASHCMD_16(base,offset,value) \
	li	t0,value ;	      \
	sh	t0,((offset)<<1)(base)

#define FLASHCMD_16B(base,offset,value) \
	li	t0,value ;	      \
	sb	t0,((offset)<<1)(base)


/*  *********************************************************************
    *  flashop_engine
    *  
    *  This routine is written in a PIC method to allow us to do
    *  flash operations without any help from CFE.  We need to do
    *  this when we're not relocated and want to muck with the
    *  flash we're running from.
    *  
    *  This routine follows some simple instructions in a table,
    *  so you can batch up the operations in one place.
    *  
    *  Input parameters: 
    *  	   a0 - pointer to instruction list
    *  	   
    *  Return value:
    *  	   v0 - 0 if all instructions succeeded
    *  	   else less than zero, # of failing instructions
    ********************************************************************* */

                .text

#define reg_op		  t3
#define reg_base	  t4
#define reg_dest	  t5
#define reg_src		  t6
#define reg_cnt		  t7

LEAF(flashop_engine)

instloop:	LR	reg_op,FEINST_OP(a0)	   /* Load instruction */
		LR	reg_base,FEINST_BASE(a0)
		LR	reg_dest,FEINST_DEST(a0)
		LR	reg_src,FEINST_SRC(a0)
		LR	reg_cnt,FEINST_CNT(a0)
		li	v0,0			   /* total of result values */
		li	v1,0			   /* result for this function */

#ifdef __long64
		dli	t0,0x9000000000000000	   /* uncached - XKPHYS */
		or	reg_base,t0		   /* so we can access flash beyond KSEG */
#else
		or	reg_base,K1BASE		   /* 32-bit, regular KSEG */
#endif

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
		bne	reg_op,FEOP_RETURN,99f	   /* Return to main prog */

		j	ra
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_REBOOT,99f	   /* restart system  */

		li	t0,0xBFC00000		   /* jump to boot vector */
		j	t0
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_READ8,99f	   /* Read, 8-bit mode */

		ADD	reg_src,reg_src,reg_base

1:		lbu	t0,0(reg_src)		   /* Copy user data */
		sb	t0,0(reg_dest)
		ADD	reg_src,1
		add	reg_dest,1
		sub	reg_cnt,1
		bgt	reg_cnt,zero,1b

		b	nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_READ16,99f	   /* Read, 16-bit mode */

		ADD	reg_src,reg_src,reg_base

		li	t0,1			   /* test bottom bit */
		and	t0,t0,reg_src		   /* t0 == 1 if odd */
		beq	t0,zero,1f		   /* no odd byte to worry about */

		SUB	reg_src,reg_src,t0	   /* make even value */
		lh	t0,0(reg_src)		   /* interesting byte is odd */
#ifdef __MIPSEB
		sb	t0,0(reg_dest)		   /* interesting byte in low 8 bits */
#else
		srl	t0,t0,8			   /* little endian */
		sb	t0,0(reg_dest)		   /* interesting byte is high 8 bits */
#endif

		ADD	reg_src,2		   /* advance one word (we made addr even above) */
		add	reg_dest,1		   /* dest always written by bytes */
		sub	reg_cnt,1

1:		beq	reg_cnt,zero,nextinst

		lh	t0,0(reg_src)		   /* Copy user data */

#ifdef __MIPSEB
		sb	t0,1(reg_dest)		   /* Big endian to memory */
		srl	t0,t0,8			   /* t0 = 0x1234 -> 0x12 0x34 */
		sb	t0,0(reg_dest)
#else
		sb	t0,0(reg_dest)		   /* little endian */
		srl	t0,t0,8			   /* t0 = 0x1234 -> 0x34 0x12 */
		sb	t0,1(reg_dest)
#endif

		ADD	reg_src,2
		add	reg_dest,2
		sub	reg_cnt,2
		bgt	reg_cnt,1,1b

		beq	reg_cnt,zero,nextinst	   /* no straggler */

		lh	t0,0(reg_src)		   /* interesting byte is odd */
#ifdef __MIPSEB
		srl	t0,t0,8			   /* little endian */
		sb	t0,0(reg_dest)		   /* interesting byte in high 8 bits */
#else
		sb	t0,0(reg_dest)		   /* interesting byte is low 8 bits */
#endif

		b	nextinst
/* CFI - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#if (FLASH_DRIVERS & FLASH_DRIVER_CFI)
99:		bne	reg_op,FEOP_CFIQUERY8,99f   /* CFI Query 8-bit */

		ADD	reg_src,reg_src,reg_base

		FLASHCMD_8(reg_base,FLASH_CFI_QUERY_ADDR,FLASH_CFI_QUERY_MODE)

1:		lbu	t0,0(reg_src)		   /* Copy CFI data */
		sb	t0,0(reg_dest)
		ADD	reg_src,1
		add	reg_dest,1
		sub	reg_cnt,1
		bgt	reg_cnt,zero,1b

		FLASHCMD_8(reg_base,FLASH_CFI_QUERY_ADDR,FLASH_CFI_QUERY_EXIT)

		b	nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_CFIQUERY16,99f   /* CFI Query 16-bit in word mode */

		ADD	reg_src,reg_src,reg_base

		FLASHCMD_16(reg_base,FLASH_CFI_QUERY_ADDR,FLASH_CFI_QUERY_MODE)

1:		lh	t0,0(reg_src)		   /* Copy CFI data */
		sh	t0,0(reg_dest)
		ADD	reg_src,2
		add	reg_dest,2
		sub	reg_cnt,2
		bgt	reg_cnt,zero,1b

		FLASHCMD_16(reg_base,FLASH_CFI_QUERY_ADDR,FLASH_CFI_QUERY_EXIT)

		b	nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_CFIQUERY16B,99f   /* CFI Query 16-bit in byte mode */

		ADD	reg_src,reg_src,reg_base

		FLASHCMD_16B(reg_base,FLASH_CFI_QUERY_ADDR,FLASH_CFI_QUERY_MODE)

1:		lb	t0,0(reg_src)		   /* Copy CFI data */
		sb	t0,0(reg_dest)
		ADD	reg_src,1
		add	reg_dest,1
		sub	reg_cnt,1
		bgt	reg_cnt,zero,1b

		FLASHCMD_16B(reg_base,FLASH_CFI_QUERY_ADDR,FLASH_CFI_QUERY_EXIT)

		b	nextinst
#endif

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_MEMCPY,99f   /* Generic memcpy */

1:		lbu	t0,0(reg_src)
		sb	t0,0(reg_dest)
		add	reg_src,1
		add	reg_dest,1
		sub	reg_cnt,1
		bgt	reg_cnt,zero,1b

		b	nextinst


/* AMD  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#if (FLASH_DRIVERS & FLASH_DRIVER_AMD)
99:		bne	reg_op,FEOP_AMD_ERASE8,99f   /* AMD erase (8-bit) */

		ADD	reg_dest,reg_dest,reg_base

	/* Do an "unlock write" sequence  (cycles 1-2) */

		FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
		FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)

	/* send the erase command (cycle 3) */

		FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_ERASE_3)

	/* Do an "unlock write" sequence (cycles 4-5) */

		FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
		FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)

        /* Send the "erase sector" qualifier (cycle 6) */

		FLASHCMD_8(reg_dest,0,AMD_FLASH_ERASE_SEC_6)

	/* Wait for the erase to complete */

1:		lb	t0,0(reg_dest)		# get byte
		and	t0,0xFF			# test hi byte
		bne	t0,0xFF,1b		# go till bit is set

		b	nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_AMD_ERASE16,99f   /* AMD erase (16-bit in word mode) */

		ADD	reg_dest,reg_dest,reg_base

	/* Do an "unlock write" sequence  (cycles 1-2) */

		FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
		FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)

	/* send the erase command (cycle 3) */

		FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_ERASE_3)

	/* Do an "unlock write" sequence (cycles 4-5) */

		FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
		FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)

        /* Send the "erase sector" qualifier (cycle 6) */

		FLASHCMD_16(reg_dest,0,AMD_FLASH_ERASE_SEC_6)

	/* Wait for the erase to complete */

1:		lh	t0,0(reg_dest)		# get word
		and	t0,0xFF			# test byte
		bne	t0,0xFF,1b		# go till erased

		b	nextinst

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_AMD_ERASE16B,99f   /* AMD erase (16-bit in byte mode) */

		ADD	reg_dest,reg_dest,reg_base

	/* Do an "unlock write" sequence  (cycles 1-2) */

		FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
		FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)

	/* send the erase command (cycle 3) */

		FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_ERASE_3)

	/* Do an "unlock write" sequence (cycles 4-5) */

		FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
		FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)

        /* Send the "erase sector" qualifier (cycle 6) */

		FLASHCMD_16B(reg_dest,0,AMD_FLASH_ERASE_SEC_6)

	/* Wait for the erase to complete */

1:		lh	t0,0(reg_dest)		# get word
		and	t0,0xFF			# test byte
		bne	t0,0xFF,1b		# go till erased

		b	nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_AMD_PGM8,99f	/* AMD 8-bit program */

		ADD	reg_dest,reg_dest,reg_base

	/* Do an "unlock write" sequence  (cycles 1-2) */
11:
		FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
		FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)

	/* Send a program command (cycle 3) */

	 	FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_PROGRAM)

	/* Write a byte (cycle 4) */

		lbu	t0,0(reg_src)
		sb	t0,0(reg_dest)	# t0 = byte written to flash
		
	/* Wait for write to complete */

1:		lbu	t2,0(reg_dest)	# t2 = byte from flash

		and	t1,t2,0x80	# done if bit7 of flash
		and	t0,t0,0x80	# is same as bit7 of data
		beq	t1,t0,2f

		and	t1,t2,0x20	# not done if bit5
		bne	t1,0x20,1b	# is still set
2:

	/* next byte...	 */

		add	reg_src,1	# next source byte
		ADD	reg_dest,1	# next dest byte
		sub	reg_cnt,1	# one less count
		bgt	reg_cnt,0,11b


		b	nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_AMD_PGM16,99f	/* AMD 16-bit program */

		ADD	reg_dest,reg_dest,reg_base

	/* Do an "unlock write" sequence  (cycles 1-2) */
11:
		FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
		FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)

	/* Send a program command (cycle 3) */

	 	FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_PROGRAM)

	/* Write a byte (cycle 4) */

		lh	t0,0(reg_src)
		sh	t0,0(reg_dest)	# t0 = byte written to flash
		
	/* Wait for write to complete */

1:		lh	t2,0(reg_dest)	# t2 = byte from flash

		and	t1,t2,0x80	# done if bit7 of flash
		and	t0,t0,0x80	# is same as bit7 of data
		beq	t1,t0,2f

		and	t1,t2,0x20	# not done if bit5
		bne	t1,0x20,1b	# is still set
2:

	/* next byte...	 */

		add	reg_src,2	# next source word
		ADD	reg_dest,2	# next dest word
		sub	reg_cnt,2	# one less count
		bgt	reg_cnt,0,11b

		b	nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_AMD_PGM16B,99f	/* AMD 16-bit pgm in 8-bit mode */

		ADD	reg_dest,reg_dest,reg_base

	/* Do an "unlock write" sequence  (cycles 1-2) */
11:
		FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
		FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)

	/* Send a program command (cycle 3) */

	 	FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_PROGRAM)

	/* Write a byte (cycle 4) */

		lb	t0,0(reg_src)
		sb	t0,0(reg_dest)	# t0 = byte written to flash
		
	/* Wait for write to complete */

1:		lb	t2,0(reg_dest)	# t2 = byte from flash

		and	t1,t2,0x80	# done if bit7 of flash
		and	t0,t0,0x80	# is same as bit7 of data
		beq	t1,t0,2f

		and	t1,t2,0x20	# not done if bit5
		bne	t1,0x20,1b	# is still set
2:

	/* next byte...	 */

		add	reg_src,1	# next source word
		ADD	reg_dest,1	# next dest word
		sub	reg_cnt,1	# one less count
		bgt	reg_cnt,0,11b

		b	nextinst

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:           bne     reg_op,FEOP_AMD_DEVCODE8,99f   /* AMD 8-bit - Boot Block Location */

              ADD     reg_src,reg_src,reg_base

              FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
              FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)
              FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_AUTOSEL)
      
              lbu     t0,AMD_FLASH_DEVCODE8(reg_src)
              sb      t0,0(reg_dest)
              li      t0,AMD_FLASH_RESET
              sb      t0,0(reg_src)


              b       nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:           bne     reg_op,FEOP_AMD_DEVCODE16,99f   /* AMD 8-bit - Boot Block Location */

              ADD     reg_src,reg_src,reg_base

              FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
              FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)
              FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_AUTOSEL)
      
              lw      t0,0(reg_src)
#ifdef __MIPSEB
	      srl     t0,t0,8	/* ((3-AMD_FLASH_DEVCODE16)*8) */
#else
	      srl     t0,t0,16	/* (AMD_FLASH_DEVCODE16*8) */
#endif
              sb      t0,0(reg_dest)
              li      t0,AMD_FLASH_RESET
              sb      t0,0(reg_src)
              
              b       nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:           bne     reg_op,FEOP_AMD_DEVCODE16B,99f   /* AMD 8-bit - Boot Block Location */
      
              ADD     reg_src,reg_src,reg_base
      
              FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
              FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)
              FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_AUTOSEL)
      
              lw      t0,0(reg_src)
#ifdef __MIPSEB
#else
	      srl     t0,t0,16			/* (AMD_FLASH_DEVCODE16B*8)*/
#endif

              sb      t0,0(reg_dest)
              li      t0,AMD_FLASH_RESET
              sb      t0,0(reg_src)
      
              b       nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:           bne     reg_op,FEOP_AMD_MANID8,99f   /* AMD 8-bit - Boot Block Location */

              ADD     reg_src,reg_src,reg_base

              FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
              FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)
              FLASHCMD_8(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_AUTOSEL)
      
              lbu     t0,AMD_FLASH_MANID(reg_src)
              sb      t0,0(reg_dest)
              li      t0,AMD_FLASH_RESET
              sb      t0,0(reg_src)


              b       nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:           bne     reg_op,FEOP_AMD_MANID16,99f   /* AMD 8-bit - Boot Block Location */

              ADD     reg_src,reg_src,reg_base

              FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
              FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)
              FLASHCMD_16(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_AUTOSEL)
      
              lw      t0,0(reg_src)
#ifdef __MIPSEB
	      srl     t0,t0,((3-AMD_FLASH_MANID)*8)
#else
	      srl     t0,t0,(AMD_FLASH_MANID*8)
#endif
              sb      t0,0(reg_dest)
              li      t0,AMD_FLASH_RESET
              sb      t0,0(reg_src)
              
              b       nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:           bne     reg_op,FEOP_AMD_MANID16B,99f   /* AMD 8-bit - Boot Block Location */
      
              ADD     reg_src,reg_src,reg_base
      
              FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_MAGIC_1)
              FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_2,AMD_FLASH_MAGIC_2)
              FLASHCMD_16B(reg_base,AMD_FLASH_MAGIC_ADDR_1,AMD_FLASH_AUTOSEL)
      
              lbu     t0,AMD_FLASH_MANID(reg_src)

              sb      t0,0(reg_dest)
              li      t0,AMD_FLASH_RESET
              sb      t0,0(reg_src)
      
              b       nextinst        

#endif

/* INTEL  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#if (FLASH_DRIVERS & FLASH_DRIVER_INTEL)
99:		bne	reg_op,FEOP_INTEL_ERASE8,99f	/* Intel erase 8-bit */

		ADD	reg_dest,reg_dest,reg_base

		FLASHCMD_8(reg_dest,0,INTEL_FLASH_ERASE_BLOCK)
		FLASHCMD_8(reg_dest,0,INTEL_FLASH_ERASE_CONFIRM)

1:		lbu	t0,0(reg_dest)	/* loop till bit 7 is set */
		andi	t0,0x80
		beq	t0,zero,1b

		FLASHCMD_8(reg_dest,0,INTEL_FLASH_READ_MODE)

		b	nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_INTEL_ERASE16,99f	/* Intel erase 16-bit */

		ADD	reg_dest,reg_dest,reg_base

/* XXX copy of 8-bit erase, is this right? */

		FLASHCMD_8(reg_dest,0,INTEL_FLASH_ERASE_BLOCK)
		FLASHCMD_8(reg_dest,0,INTEL_FLASH_ERASE_CONFIRM)

1:		lbu	t0,0(reg_dest)	/* loop till bit 7 is set */
		andi	t0,0x80
		beq	t0,zero,1b

		FLASHCMD_8(reg_dest,0,INTEL_FLASH_READ_MODE)

		b	nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_INTEL_PGM8,99f     /* Intel 8-bit program */

		ADD	reg_dest,reg_dest,reg_base

11:		FLASHCMD_8(reg_dest,0,INTEL_FLASH_PROGRAM)

		lbu	t0,0(reg_src)
		sb	t0,0(reg_dest)

1:		lbu	t0,0(reg_dest)	/* loop till bit 7 is set */
		andi	t0,0x80
		beq	t0,zero,1b

		lbu	t0,0(reg_dest)	/* contains final result */
		/* If good, bits 1, 3, 4 will not be set */

		add     reg_src,1
		ADD     reg_dest,1
		sub     reg_cnt,1
		bgt	reg_cnt,zero,11b

		FLASHCMD_8(reg_dest,0,INTEL_FLASH_READ_MODE)

		b	nextinst
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		bne	reg_op,FEOP_INTEL_PGM16,99f    /* Intel 16-bit prog */

		ADD	reg_dest,reg_dest,reg_base

11:		FLASHCMD_16(reg_dest,0,INTEL_FLASH_PROGRAM)

		lh	t0,0(reg_src)
		sh	t0,0(reg_dest)

1:		lh	t0,0(reg_dest)	/* loop till bit 7 is set */
		andi	t0,0x80
		beq	t0,zero,1b

		lh	t0,0(reg_dest)	/* contains final result */
		/* If good, bits 1, 3, 4 will not be set */

		add     reg_src,2
		ADD     reg_dest,2
		sub     reg_cnt,2
		bgt	reg_cnt,zero,11b

		FLASHCMD_16(reg_dest,0,INTEL_FLASH_READ_MODE)

		b	nextinst
#endif
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99:		li      v1,-1			  /* invalid command */

nextinst:	SR	v1,FEINST_RESULT(a0)	  /* store result of instruction */
		ADD	v0,v0,v1		  /* add to total */
		ADD	a0,FEINST_SIZE		  /* advance to next instr. */
		b	instloop

flashop_engine_end:
		nop

END(flashop_engine)

		.sdata

		.globl	flashop_engine_ptr
		.globl	flashop_engine_len

flashop_engine_ptr:
		_VECT_	flashop_engine
flashop_engine_len:
		.word	flashop_engine_end-flashop_engine



/*  *********************************************************************
    *  end
    ********************************************************************* */