Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
159 theseven 1
@
2
@
3
@    emBIOS Loader for iPod Nano 2G
4
@
5
@    Copyright 2010 TheSeven
6
@
7
@
8
@    This file is part of emBIOS.
9
@
10
@    emBIOS is free software: you can redistribute it and/or
11
@    modify it under the terms of the GNU General Public License as
12
@    published by the Free Software Foundation, either version 2 of the
13
@    License, or (at your option) any later version.
14
@
15
@    emBIOS is distributed in the hope that it will be useful,
16
@    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
@    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18
@    See the GNU General Public License for more details.
19
@
20
@    You should have received a copy of the GNU General Public License along
21
@    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
22
@
23
@
24
 
25
 
26
#include "build/version.h"
27
 
28
 
29
.global _start
30
_start:
31
 
32
	msr	cpsr_c, #0xd3
33
	mrc	p15, 0, r0,c1,c0
34
	bic	r0, r0, #1
35
	mcr	p15, 0, r0,c1,c0
36
	mov	r0, #0
37
	mcr	p15, 0, r0,c7,c5
38
	adr	lr, cacheflush_done
39
 
40
flushcache:
41
	mov	r0, #0
42
flushcache_loop:
43
	mcr	p15, 0, r0,c7,c14,2
44
	add	r10, r0, #0x10
45
	mcr	p15, 0, r10,c7,c14,2
46
	add	r10, r10, #0x10
47
	mcr	p15, 0, r10,c7,c14,2
48
	add	r10, r10, #0x10
49
	mcr	p15, 0, r10,c7,c14,2
50
	adds	r0, r0, #0x04000000
51
	bne	flushcache_loop
52
	mcr	p15, 0, r0,c7,c10,4
53
	mov	pc, lr
54
 
55
cacheflush_done:
56
	adr	lr, values1
57
	ldmia	lr!, {r0-r13}
58
	str	r1, [r0,#0x88]
59
	str	r2, [r0,#0xf0]
60
	str	r4, [r3,#0x38]
61
	mov	r1, #0
62
	str	r7, [r0]
63
	str	r1, [r0,#0x04]
64
	str	r8, [r0,#0x10]
65
	str	r1, [r0,#0x14]
66
	str	r9, [r0,#0x20]
67
	str	r1, [r0,#0x24]
68
	str	r10, [r0,#0x30]
69
	str	r1, [r0,#0x34]
70
	str	r9, [r0,#0x40]
71
	str	r1, [r0,#0x44]
72
	str	r11, [r0,#0x50]
73
	str	r1, [r0,#0x54]
74
	str	r12, [r0,#0x60]
75
	str	r1, [r0,#0x64]
76
	str	r13, [r0,#0xa0]
77
	str	r1, [r0,#0xa4]
78
	str	r5, [r0,#0xb0]
79
	mov	r3, #1
80
	str	r3, [r0,#0xb4]
81
	str	r6, [r0,#0xc0]
82
	ldmia	lr, {r4-r14}
83
	str	r1, [r0,#0xc4]
84
	str	r6, [r0,#0xd0]
85
	str	r1, [r0,#0xd4]
86
	str	r6, [r0,#0xe0]
87
	str	r1, [r0,#0xe4]
88
	str	r7, [r0,#0xf0]
89
	str	r1, [r0,#0xf4]
90
	ldr	r3, [r8,#0x24]
91
	orr	r3, r3, #3
92
	str	r3, [r8,#0x24]
93
	ldr	r3, [r8,#0x28]
94
	bic	r3, r3, #0x4000
95
	str	r3, [r8,#0x28]
96
	mov	r3, #0x3C400000
97
	str	r1, [r3]
98
	str	r12, [r8,#0x0c]
99
	ldr	r3, [r8,#0x24]
100
	orr	r3, r3, #0x54
101
	str	r3, [r8,#0x24]
102
	mov	r3, #0x2000
103
waitpll1:
104
	subs	r3, r3, #1
105
	bne	waitpll1
106
	ldr	r3, [r8]
107
	bic	r3, r3, r5
108
	orr	r3, r3, r4
109
	str	r3, [r8]
110
	ldr	r3, [r8,#0x24]
111
	bic	r3, r3, #1
112
	str	r3, [r8,#0x24]
113
	mov	r3, #0x84
114
	str	r3, [r0,#0x68]
115
	str	r13, [r8,#0x04]
116
	mov	r3, #0xa000
117
waitpll2:
118
	subs	r3, r3, #1
119
	bne	waitpll2
120
	ldr	r3, [r8,#0x24]
121
	orr	r3, r3, #1
122
	str	r3, [r8,#0x24]
123
	mov	r3, #0x6000
124
waitpll3:
125
	subs	r3, r3, #1
126
	bne	waitpll3
127
	ldr	r3, [r8]
128
	bic	r3, r3, r9
129
	orr	r3, r3, r10
130
	str	r3, [r8]
131
	ldr	r3, [r8,#0x3c]
132
	bic	r3, r3, #0x400
133
	orr	r3, r3, #0x280
134
	str	r3, [r8,#0x3c]
135
	ldr	r3, [r8,#0x10]
136
	bic	r3, r3, #0x1f8
137
	orr	r3, r3, #0x37
138
	str	r3, [r8,#0x10]
139
	mrc     p15, 0, r3,c1,c0
140
	orr     r3, r3, #0xc0000000
141
	mcr     p15, 0, r3,c1,c0
142
	str	r11, [r8,#0x28]
143
	str	r14, [r8,#0x40]
144
	sub	r6, r0, #0x00800000
145
	mov	r5, #0xf700
146
	str	r5, [r6,#0x88]
147
	mov	r5, #0x1100
148
	orr	r5, r5, #0x11
149
	str	r5, [r0,#0x88]
150
	add	r4, r6, #0x00200000
151
	str	r1, [r4,#0x08]
179 theseven 152
	mov	r5, #0xf3
159 theseven 153
	str	r5, [r4]
154
	mov	r5, #0x10
155
	str	r5, [r4,#0x04]
156
	b	setuppmu
157
 
158
pmuinitdata:
159
	.byte 3, 0x39, 0x18, 0, 0x8c
194 theseven 160
	.byte 9, 0x13, 3, 0, 0, 7, 0, 5, 2, 0x6b, 0
159 theseven 161
	.byte 4, 0x1e, 0x12, 1, 0, 0x10
162
	.byte 6, 0x22, 0x2f, 1, 0, 0x10, 9, 1
163
	.byte 6, 0x31, 0x15, 1, 0x0f, 3, 0x15, 0
164
	.byte 1, 0x43, 0
165
	.byte 1, 0x4e, 0
166
	.byte 1, 0x58, 0
167
	.byte 5, 0x07, 0xb0, 0x0f, 0xfe, 0xfc, 0xff
168
	.byte 5, 0x0d, 0xdf, 0xaa, 0x4a, 5, 0x27
169
	.byte 0
170
	.align 2
171
 
172
val_006a49a5:
173
	.word 0x006a49a5
174
 
175
values1:
176
	.word 0x3CF00000 @ R0
177
	.word 0x00001111 @ R1
178
	.word 0x00000221 @ R2
179
	.word 0x38200000 @ R3
180
val_00990999:
181
	.word 0x00990999 @ R4
182
	.word 0x41001111 @ R5
183
	.word 0x11114444 @ R6
184
	.word 0x11221111 @ R7
185
	.word 0x11001744 @ R8
186
	.word 0x33333333 @ R9
187
	.word 0x11113333 @ R10
188
	.word 0x77721151 @ R11
189
	.word 0x12111111 @ R12
190
	.word 0x11111000 @ R13
191
	.word 0x60007000 @ R4
192
	.word 0x7F007F00 @ R5
193
	.word 0x44444444 @ R6
194
	.word 0x22222221 @ R7
195
	.word 0x3C500000 @ R8
196
	.word 0xFF00FF00 @ R9
197
	.word 0x20803180 @ R10
198
	.word 0xFFDFF7CF @ R11
199
	.word 0x00061800 @ R12
200
	.word 0x00021200 @ R13
201
	.word 0xFFFFEF7E @ R14
202
 
203
setuppmu:
204
	mov	sp, pc
205
	bl	getpowerok
206
	beq	poweroff
207
	adr	r2, pmuinitdata
208
pmuinitloop:
209
	ldrb	r3, [r2], #1
210
	movs	r3, r3
211
	beq	pmuinitdone
212
	ldrb	r1, [r2], #1
213
	mov	r0, #0xe6
214
	bl	i2csend
215
	b	pmuinitloop
216
pmuinitdone:
217
	orr	r12, r12, #0x00600000
218
	mov	r1, #1
219
	ldr	r0, [r12,#0x100]
220
	bic	r0, r0, #0x0f
221
	orr	r0, r0,	#1
222
	str	r0, [r12,#0x100]
223
	str	r1, [r12,#0x10c]
224
	sub	r11, r12, #0x04d00000
225
	ldr	r0, [r11,#0x80]
226
	bic	r0, r0, #0x20000
227
	str	r0, [r11,#0x80]
228
	ldr	r0, [r11]
229
	bic	r0, r0, #0xc00
230
	orr	r0, r0, #0x500
231
	str	r0, [r11]
232
	mov	r0, #3
233
	str	r0, [r11,#0x04]
234
	mov	r0, #1
235
	bl	sleepms
236
	mov	r0, #0x200
237
	orr	r0, r0, #0xed
238
	str	r0, [r11,#0x08]
239
	ldr	r0, val_006a49a5
240
	str	r0, [r11,#0x10]
241
	mov	r0, #0x37
242
	str	r0, [r11,#0x0c]
243
	ldr	r1, [r11,#0x04]
244
	bic	r1, r1, #0x700
245
	orr	r0, r1, #0x200
246
	str	r0, [r11,#0x04]
247
	orr	r0, r1, #0x300
248
	str	r0, [r11,#0x04]
249
	str	r0, [r11,#0x04]
250
	orr	r0, r1, #0x100
251
	str	r0, [r11,#0x04]
252
	orr	r0, r1, #0x500
253
	str	r0, [r11,#0x04]
254
	ldr	r0, [r11,#0x08]
255
	orr	r0, r0, #0x1000
256
	str	r0, [r11,#0x08]
257
	ldr	r0, val_00990999
258
	str	r0, [r11,#0x38]
259
	ldr	r0, [r12,#0xf0]
260
	bic	r0, r0, #0xff000000
261
	bic	r0, r0, #0x00ff0000
262
	orr	r0, r0,	#0x22000000
263
	orr	r0, r0,	#0x00220000
264
	str	r0, [r12,#0xf0]
265
	mov	r0, #0xf0
266
	str	r0, [r12,#0xfc]
267
	sub	r10, r12, #0x00d00000
268
	ldr	r0, [r10]
269
	mov	r0, r0,lsr#20
270
	mov	r0, r0,lsl#20
271
	str	r0, [r10]
272
	mov	r1, #0x3a000
273
	orr	r1, r1, #0x980
274
	str	r1, [r10,#0x08]
275
	orr	r0, r0, #0x300000
276
	str	r0, [r10]
277
	bl	initlcd
278
	mov	r0, #0x3f
279
	mcr	p15, 0, r0,c6,c0,1   @ CS0: 4GB at offset 0 - everything
280
	mcr	p15, 0, r0,c6,c0,0   @ DS0: 4GB at offset 0 - everything
281
	mov	r0, #0x31
282
	mcr	p15, 0, r0,c6,c1,1   @ CS1: SRAM/SDRAM mirror
283
	mcr	p15, 0, r0,c6,c1,0   @ DS1: SRAM/SDRAM mirror
284
	add	r0, r0, #0x08000000
285
	mcr	p15, 0, r0,c6,c2,1   @ CS2: SDRAM
286
	mcr	p15, 0, r0,c6,c2,0   @ DS2: SDRAM
287
	mov	r0, #0x23
288
	add	r0, r0, #0x22000000
289
	mcr	p15, 0, r0,c6,c3,1   @ CS3: SRAM
290
	mcr	p15, 0, r0,c6,c3,0   @ DS3: SRAM
291
	mov	r0, #0x27
292
	add	r0, r0, #0x24000000
293
	mcr	p15, 0, r0,c6,c4,1   @ CS4: NOR flash
294
	mcr	p15, 0, r0,c6,c4,0   @ DS4: NOR flash
295
	mcr	p15, 0, r1,c6,c5,1   @ CS5: unused
296
	mcr	p15, 0, r1,c6,c5,0   @ DS5: unused
297
	mcr	p15, 0, r1,c6,c6,1   @ CS6: unused
298
	mcr	p15, 0, r1,c6,c6,0   @ DS6: unused
299
	mcr	p15, 0, r1,c6,c7,1   @ CS7: unused
300
	mcr	p15, 0, r1,c6,c7,0   @ DS7: unused
301
	mov	r0, #0x1e
302
	mcr	p15, 0, r0,c2,c0, 1  @ CS1-4: cacheable
303
	mcr	p15, 0, r0,c2,c0, 0  @ DS1-4: cacheable
304
	mcr	p15, 0, r0,c3,c0, 0  @ DS1-4: write cacheable
305
	mov	r0, #0x300
306
	add	r0, r0, #0xff
307
	mcr	p15, 0, r0,c5,c0, 1  @ CS0-4: full access
308
	mcr	p15, 0, r0,c5,c0, 0  @ DS0-4: full access
309
	mrc	p15, 0, r0,c1,c0
310
	orr	r0, r0, #5
311
	orr	r0, r0, #0x1000
312
	mcr	p15, 0, r0,c1,c0     @ Re-enable the Protection Unit and caches
313
	mov	r11, #0x22000000
314
	orr	r11, r11, #0x10000
315
	mov	r0, r11
316
	mov	r3, #0x2d00
317
	orr	r3, r3, #0x60
318
	sub	r2, r1, #1
319
fillbuff:
320
	str	r2, [r0], #4
321
	subs	r3, r3, #1
322
	bne	fillbuff
323
	mov	r0, r11
324
	adr	r3, text
325
	bl	rendertext
326
	add	r0, r11, #0xb00
327
	bl	rendertext
328
	add	r0, r11, #0x2100
329
	bl	rendertext
330
	mov	r0, #0
331
	mov	r1, #175
332
	mov	r2, #0
333
	mov	r3, #131
334
	str	r11, [sp]
335
	bl	displaylcd
336
	mov	r0, #0xe6
196 theseven 337
	mov	r1, #0x2a
338
	mov	r2, #6
339
	bl	i2csendbyte
340
	mov	r0, #0xe6
159 theseven 341
	mov	r1, #0x2b
342
	mov	r2, #4
343
	bl	i2csendbyte
344
	mov	r0, #0xe6
345
	mov	r1, #0x28
346
	mov	r2, #10
347
	bl	i2csendbyte
348
	mov	r0, #0xe6
349
	mov	r1, #0x29
350
	mov	r2, #1
351
	bl	i2csendbyte
352
	orr	r12, r12, #0xcf00000
353
	ldr	r0, [r12,#0xe4]
354
	tst	r0, #0x40
355
	beq	recoverymode
356
	b	decompress
357
 
358
 
359
rendertext:
360
	stmfd	sp!, {r4-r6,lr}
361
rendertext_next:
362
	ldrb	r12, [r3], #1
363
	cmp	r12, #0
364
	beq	rendertext_done
365
	cmn	r2, #1
366
	beq	rendertext_nobg
367
	mov	r6, r0
368
	mov	r4, #8
369
rendertext_bgrow:
370
	mov	r5, #6
371
rendertext_bgcol:
372
	cmn	r2, #2
373
	strneh	r2, [r6], #2
374
	bne	rendertext_noblend
375
	ldrh	lr, [r6]
376
	tst	lr, #1
377
	orrne	lr, lr, #0x10000
378
	mov	lr, lr,lsr#1
379
	bic	lr, lr, #0x1000
380
	bic	lr, lr, #0x84
381
	strh	lr, [r6], #2
382
rendertext_noblend:
383
	subs	r5, r5, #1
384
	bne	rendertext_bgcol
385
	add	r6, r6, #340
386
	subs	r4, r4, #1
387
	bne	rendertext_bgrow
388
rendertext_nobg:
389
	adr	r5, font
390
	sub	r12, r12, #0x20
391
	cmp	r12, #0x5f
392
	addcc	r5, r12,lsl#2
393
	addcc	r5, r12
394
	mov	r12, #5
395
rendertext_col:
396
	mov	r6, r0
397
	ldrb	r4, [r5], #1
398
rendertext_row:
399
	tst	r4, #1
400
	strneh	r1, [r6]
401
	add	r6, r6, #352
402
	movs	r4, r4,lsr#1
403
	bne	rendertext_row
404
	add	r0, r0, #2
405
	subs	r12, r12, #1
406
	bne	rendertext_col
407
	add	r0, r0, #2
408
	b	rendertext_next
409
rendertext_done:
410
	ldmfd sp!, {r4-r6,pc}
411
 
412
 
413
text:
414
	.ascii "emBIOS Loader v"
415
	.ascii VERSION
416
	.ascii " r"
417
	.ascii VERSION_SVN
418
	.ascii "\0"
419
	.ascii "Switch HOLD on for recovery\0"
420
	.ascii "Loading emBIOS...\0"
421
 
422
 
423
font:
424
	.byte 0, 0, 0, 0, 0
425
	.byte 0, 0, 95, 0, 0
426
	.byte 0, 7, 0, 7, 0
427
	.byte 20, 127, 20, 127, 20
428
	.byte 36, 42, 127, 42, 18
429
	.byte 35, 19, 8, 100, 98
430
	.byte 54, 73, 85, 34, 80
431
	.byte 5, 3, 0, 0, 0
432
	.byte 28, 34, 65, 0, 0
433
	.byte 0, 0, 65, 34, 28
434
	.byte 20, 8, 62, 8, 20
435
	.byte 8, 8, 62, 8, 8
436
	.byte 0, -96, 96, 0, 0
437
	.byte 8, 8, 8, 8, 8
438
	.byte 0, 96, 96, 0, 0
439
	.byte 32, 16, 8, 4, 2
440
	.byte 62, 81, 73, 69, 62
441
	.byte 0, 66, 127, 64, 0
442
	.byte 66, 97, 81, 73, 70
443
	.byte 33, 65, 69, 75, 49
444
	.byte 24, 20, 18, 127, 16
445
	.byte 39, 69, 69, 69, 57
446
	.byte 60, 74, 73, 73, 48
447
	.byte 1, 113, 9, 5, 3
448
	.byte 54, 73, 73, 73, 54
449
	.byte 6, 73, 73, 41, 30
450
	.byte 0, 54, 54, 0, 0
451
	.byte 0, 86, 54, 0, 0
452
	.byte 8, 20, 34, 65, 0
453
	.byte 20, 20, 20, 20, 20
454
	.byte 0, 65, 34, 20, 8
455
	.byte 2, 1, 81, 9, 6
456
	.byte 50, 73, 121, 65, 62
457
	.byte 124, 18, 17, 18, 124
458
	.byte 127, 73, 73, 73, 62
459
	.byte 62, 65, 65, 65, 34
460
	.byte 127, 65, 65, 34, 28
461
	.byte 127, 73, 73, 73, 65
462
	.byte 127, 9, 9, 9, 1
463
	.byte 62, 65, 73, 73, 58
464
	.byte 127, 8, 8, 8, 127
465
	.byte 0, 65, 127, 65, 0
466
	.byte 32, 64, 65, 63, 1
467
	.byte 127, 8, 20, 34, 65
468
	.byte 127, 64, 64, 64, 64
469
	.byte 127, 2, 12, 2, 127
470
	.byte 127, 4, 8, 16, 127
471
	.byte 62, 65, 65, 65, 62
472
	.byte 127, 9, 9, 9, 6
473
	.byte 62, 65, 81, 33, 94
474
	.byte 127, 9, 25, 41, 70
475
	.byte 38, 73, 73, 73, 50
476
	.byte 1, 1, 127, 1, 1
477
	.byte 63, 64, 64, 64, 63
478
	.byte 31, 32, 64, 32, 31
479
	.byte 127, 32, 24, 32, 127
480
	.byte 99, 20, 8, 20, 99
481
	.byte 3, 4, 120, 4, 3
482
	.byte 97, 81, 73, 69, 67
483
	.byte 0, 127, 65, 65, 0
484
	.byte 2, 4, 8, 16, 32
485
	.byte 0, 65, 65, 127, 0
486
	.byte 4, 2, 1, 2, 4
487
	.byte 64, 64, 64, 64, 64
488
	.byte 1, 2, 4, 0, 0
489
	.byte 32, 84, 84, 84, 120
490
	.byte 127, 68, 68, 68, 56
491
	.byte 56, 68, 68, 68, 40
492
	.byte 56, 68, 68, 68, 127
493
	.byte 56, 84, 84, 84, 24
494
	.byte 8, 126, 9, 1, 2
495
	.byte 8, 84, 84, 84, 60
496
	.byte 127, 4, 4, 4, 120
497
	.byte 0, 68, 125, 64, 0
498
	.byte 32, 64, 64, 61, 0
499
	.byte 127, 16, 40, 68, 0
500
	.byte 0, 65, 127, 64, 0
501
	.byte 124, 4, 24, 4, 120
502
	.byte 124, 8, 4, 4, 120
503
	.byte 56, 68, 68, 68, 56
504
	.byte 124, 20, 20, 20, 24
505
	.byte 8, 20, 20, 20, 124
506
	.byte 124, 8, 4, 4, 8
507
	.byte 72, 84, 84, 84, 32
508
	.byte 4, 63, 68, 64, 32
509
	.byte 60, 64, 64, 32, 124
510
	.byte 28, 32, 64, 32, 28
511
	.byte 60, 64, 56, 64, 60
512
	.byte 68, 40, 16, 40, 68
513
	.byte 12, 80, 80, 80, 60
514
	.byte 68, 100, 84, 76, 68
515
	.byte 0, 8, 54, 65, 0
516
	.byte 0, 0, 119, 0, 0
517
	.byte 0, 65, 54, 8, 0
518
	.byte 2, 1, 2, 4, 2
519
 
520
 
521
lcd_inittab_type2:
522
	.byte 5, 10
523
	.byte 3, 0xa4
524
	.byte 4, 0x01
525
	.byte 5, 150
526
	.byte 3, 0x01
527
	.byte 2, 0x01
528
	.byte 2, 0x00
529
	.byte 3, 0x02
530
	.byte 2, 0x03
531
	.byte 2, 0x00
532
	.byte 3, 0x03
533
	.byte 2, 0x12
534
	.byte 2, 0x30
535
	.byte 3, 0x08
536
	.byte 2, 0x04
537
	.byte 2, 0x04
538
	.byte 3, 0x0e
539
	.byte 4, 0x10
540
	.byte 3, 0x70
541
	.byte 2, 0x10
542
	.byte 2, 0x00
543
	.byte 3, 0x71
544
	.byte 4, 0x01
545
	.byte 3, 0x30
546
	.byte 4, 0x02
547
	.byte 3, 0x31
548
	.byte 2, 0x04
549
	.byte 2, 0x00
550
	.byte 3, 0x32
551
	.byte 4, 0x07
552
	.byte 3, 0x33
553
	.byte 2, 0x05
554
	.byte 2, 0x00
555
	.byte 3, 0x34
556
	.byte 4, 0x07
557
	.byte 3, 0x35
558
	.byte 2, 0x07
559
	.byte 2, 0x03
560
	.byte 3, 0x36
561
	.byte 2, 0x05
562
	.byte 2, 0x07
563
	.byte 3, 0x37
564
	.byte 4, 0x05
565
	.byte 3, 0x38
566
	.byte 2, 0x14
567
	.byte 2, 0x04
568
	.byte 3, 0x39
569
	.byte 4, 0x0e
570
	.byte 3, 0x40
571
	.byte 2, 0x02
572
	.byte 2, 0x02
573
	.byte 3, 0x41
574
	.byte 4, 0x03
575
	.byte 3, 0x42
576
	.byte 4, 0x00
577
	.byte 3, 0x43
578
	.byte 2, 0x02
579
	.byte 2, 0x00
580
	.byte 3, 0x44
581
	.byte 2, 0x07
582
	.byte 2, 0x07
583
	.byte 3, 0x45
584
	.byte 2, 0x04
585
	.byte 2, 0x07
586
	.byte 3, 0x46
587
	.byte 2, 0x05
588
	.byte 2, 0x05
589
	.byte 3, 0x47
590
	.byte 4, 0x02
591
	.byte 3, 0x48
592
	.byte 4, 0x04
593
	.byte 3, 0x49
594
	.byte 4, 0x04
595
	.byte 3, 0x60
596
	.byte 2, 0x02
597
	.byte 2, 0x02
598
	.byte 3, 0x61
599
	.byte 4, 0x03
600
	.byte 3, 0x62
601
	.byte 4, 0x00
602
	.byte 3, 0x63
603
	.byte 2, 0x02
604
	.byte 2, 0x00
605
	.byte 3, 0x64
606
	.byte 2, 0x07
607
	.byte 2, 0x07
608
	.byte 3, 0x65
609
	.byte 2, 0x04
610
	.byte 2, 0x07
611
	.byte 3, 0x66
612
	.byte 2, 0x05
613
	.byte 2, 0x05
614
	.byte 3, 0x68
615
	.byte 4, 0x04
616
	.byte 3, 0x69
617
	.byte 4, 0x04
618
	.byte 3, 0x07
619
	.byte 4, 0x01
620
	.byte 3, 0x18
621
	.byte 4, 0x01
622
	.byte 3, 0x10
623
	.byte 2, 0x16
624
	.byte 2, 0x90
625
	.byte 3, 0x11
626
	.byte 2, 0x01
627
	.byte 2, 0x00
628
	.byte 3, 0x12
629
	.byte 2, 0x01
630
	.byte 2, 0x17
631
	.byte 3, 0x13
632
	.byte 2, 0x0f
633
	.byte 2, 0x80
634
	.byte 3, 0x12
635
	.byte 2, 0x01
636
	.byte 2, 0x37
637
	.byte 3, 0x20
638
	.byte 4, 0x00
639
	.byte 3, 0x21
640
	.byte 4, 0x00
641
	.byte 3, 0x50
642
	.byte 4, 0x00
643
	.byte 3, 0x51
644
	.byte 4, 0xaf
645
	.byte 3, 0x52
646
	.byte 4, 0x00
647
	.byte 3, 0x53
648
	.byte 4, 0x83
649
	.byte 3, 0x90
650
	.byte 4, 0x03
651
	.byte 3, 0x91
652
	.byte 4, 0x00
653
	.byte 3, 0x92
654
	.byte 2, 0x01
655
	.byte 2, 0x01
656
	.byte 3, 0x98
657
	.byte 2, 0x04
658
	.byte 2, 0x00
659
	.byte 3, 0x99
660
	.byte 2, 0x13
661
	.byte 2, 0x02
662
	.byte 3, 0x9a
663
	.byte 2, 0x02
664
	.byte 2, 0x02
665
	.byte 3, 0x9b
666
	.byte 2, 0x02
667
	.byte 2, 0x00
668
	.byte 5, 100
669
	.byte 3, 0x07
670
	.byte 4, 0x21
671
	.byte 3, 0x12
672
	.byte 2, 0x01
673
	.byte 2, 0x37
674
	.byte 5, 1
675
	.byte 3, 0x07
676
	.byte 4, 0x21
677
	.byte 3, 0x12
678
	.byte 2, 0x11
679
	.byte 2, 0x37
680
	.byte 5, 100
681
	.byte 3, 0x07
682
	.byte 2, 0x02
683
	.byte 2, 0x33
684
	.byte 0
685
 
686
lcd_inittab_type3:
687
	.byte 1, 0x01
688
	.byte 5, 5
689
	.byte 1, 0x11
690
	.byte 5, 120
691
	.byte 1, 0x3a
692
	.byte 2, 0x65
693
	.byte 1, 0xab
694
	.byte 1, 0x35
695
	.byte 2, 0x00
696
	.byte 1, 0xf2
697
	.byte 2, 0x01
698
	.byte 1, 0xe0
699
	.byte 2, 0x71
700
	.byte 2, 0x76
701
	.byte 2, 0x25
702
	.byte 2, 0x01
703
	.byte 2, 0xa5
704
	.byte 2, 0x09
705
	.byte 2, 0x15
706
	.byte 2, 0x11
707
	.byte 1, 0xe1
708
	.byte 2, 0x40
709
	.byte 2, 0x21
710
	.byte 2, 0x64
711
	.byte 2, 0x13
712
	.byte 2, 0xf3
713
	.byte 2, 0x0b
714
	.byte 2, 0x00
715
	.byte 2, 0x00
716
	.byte 1, 0xe2
717
	.byte 2, 0x71
718
	.byte 2, 0x65
719
	.byte 2, 0x24
720
	.byte 2, 0x08
721
	.byte 2, 0x97
722
	.byte 2, 0x01
723
	.byte 2, 0x15
724
	.byte 2, 0x11
725
	.byte 1, 0xe3
726
	.byte 2, 0x51
727
	.byte 2, 0x01
728
	.byte 2, 0x62
729
	.byte 2, 0x13
730
	.byte 2, 0xf3
731
	.byte 2, 0x0b
732
	.byte 2, 0x00
733
	.byte 2, 0x00
734
	.byte 1, 0xe4
735
	.byte 2, 0x71
736
	.byte 2, 0x57
737
	.byte 2, 0x31
738
	.byte 2, 0x01
739
	.byte 2, 0x82
740
	.byte 2, 0x04
741
	.byte 2, 0x1f
742
	.byte 2, 0x11
743
	.byte 1, 0xe5
744
	.byte 2, 0x64
745
	.byte 2, 0x41
746
	.byte 2, 0x64
747
	.byte 2, 0x19
748
	.byte 2, 0xb3
749
	.byte 2, 0x09
750
	.byte 2, 0x00
751
	.byte 2, 0x00
752
	.byte 1, 0x29
753
	.byte 0
754
 
755
lcd_inittab_type7:
756
	.byte 1, 0x01
757
	.byte 5, 10
758
	.byte 1, 0xb4
759
	.byte 2, 0x00
760
	.byte 1, 0xb6
761
	.byte 2, 0x01
762
	.byte 1, 0xb7
763
	.byte 2, 0x00
764
	.byte 2, 0x00
765
	.byte 2, 0x02
766
	.byte 2, 0x00
767
	.byte 2, 0x06
768
	.byte 2, 0x26
769
	.byte 2, 0x2d
770
	.byte 2, 0x27
771
	.byte 2, 0x55
772
	.byte 2, 0x27
773
	.byte 1, 0xb8
774
	.byte 2, 0x10
775
	.byte 1, 0xb9
776
	.byte 2, 0x52
777
	.byte 2, 0x12
778
	.byte 2, 0x03
779
	.byte 1, 0xc0
780
	.byte 2, 0x0a
781
	.byte 2, 0x10
782
	.byte 2, 0x10
783
	.byte 1, 0xc2
784
	.byte 2, 0x14
785
	.byte 2, 0x23
786
	.byte 1, 0xc3
787
	.byte 2, 0x12
788
	.byte 2, 0x23
789
	.byte 1, 0xc6
790
	.byte 2, 0x48
791
	.byte 1, 0xe0
792
	.byte 2, 0x20
793
	.byte 2, 0x71
794
	.byte 2, 0x17
795
	.byte 2, 0x09
796
	.byte 2, 0x70
797
	.byte 2, 0x0c
798
	.byte 2, 0x13
799
	.byte 2, 0x25
800
	.byte 1, 0xe1
801
	.byte 2, 0x37
802
	.byte 2, 0x00
803
	.byte 2, 0x63
804
	.byte 2, 0x11
805
	.byte 2, 0xd9
806
	.byte 2, 0x00
807
	.byte 2, 0x12
808
	.byte 2, 0x01
809
	.byte 1, 0xe2
810
	.byte 2, 0x42
811
	.byte 2, 0x42
812
	.byte 2, 0x60
813
	.byte 2, 0x08
814
	.byte 2, 0xb4
815
	.byte 2, 0x07
816
	.byte 2, 0x0e
817
	.byte 2, 0x90
818
	.byte 1, 0xe3
819
	.byte 2, 0x47
820
	.byte 2, 0x60
821
	.byte 2, 0x66
822
	.byte 2, 0x09
823
	.byte 2, 0x6a
824
	.byte 2, 0x02
825
	.byte 2, 0x0e
826
	.byte 2, 0x09
827
	.byte 1, 0xe4
828
	.byte 2, 0x11
829
	.byte 2, 0x40
830
	.byte 2, 0x03
831
	.byte 2, 0x0a
832
	.byte 2, 0xc1
833
	.byte 2, 0x0d
834
	.byte 2, 0x17
835
	.byte 2, 0x30
836
	.byte 1, 0xe5
837
	.byte 2, 0x00
838
	.byte 2, 0x30
839
	.byte 2, 0x77
840
	.byte 2, 0x1c
841
	.byte 2, 0xfb
842
	.byte 2, 0x00
843
	.byte 2, 0x13
844
	.byte 2, 0x07
845
	.byte 1, 0xe6
846
	.byte 2, 0x01
847
	.byte 1, 0x35
848
	.byte 2, 0x00
849
	.byte 1, 0x36
850
	.byte 2, 0x00
851
	.byte 1, 0xf2
852
	.byte 2, 0x40
853
	.byte 1, 0xf3
854
	.byte 2, 0x50
855
	.byte 1, 0xfb
856
	.byte 2, 0x01
857
	.byte 1, 0x11
858
	.byte 5, 200
859
	.byte 1, 0xb1
860
	.byte 2, 0x16
861
	.byte 2, 0x03
862
	.byte 1, 0xb2
863
	.byte 2, 0x17
864
	.byte 2, 0x03
865
	.byte 1, 0x3a
866
	.byte 2, 0x65
867
	.byte 1, 0x29
868
	.byte 0
869
 
870
 
871
recoverytext:
872
	.ascii "Entered recovery mode\0"
873
	.ascii "Connect via USB\0"
874
 
875
 
876
.align 1
877
 
878
 
879
.code 16
880
thumb_nrv2e_d8:
881
	mov r7,r2
882
        mov r4,#1; neg r5,r4 @ r5= -1 initial condition
883
        lsl r4,#31 @ 1<<31: refill next time
884
        mov r6,#5
885
        lsl r6,#8 @ 0x500 @ nrv2e M2_MAX_OFFSET
886
        b top_n2e
887
 
888
nrv2e_done:
889
	bx r7
890
 
891
get1_n2e: @ In: Carry set [from adding 0x80000000 (1<<31) to itself]
892
          ldrb r4,[r0] @ zero-extend next byte
893
        adc r4,r4 @ double and insert CarryIn as low bit
894
          add r0,#1
895
        lsl r4,#24 @ move to top byte, and set CarryOut from old bit 8
896
        mov pc,lr @ return, stay in current (THUMB) mode
897
 
898
lit_n2e:
899
        ldrb r3,[r0]; add r0,#1
900
        strb r3,[r2]; add r2,#1
901
top_n2e:
902
        add r4,r4; mov lr,pc; beq get1_n2e; bcs lit_n2e
903
        mov r1,#1; b getoff_n2e
904
 
905
off_n2e:
906
        sub r1,#1
907
        add r4,r4; mov lr,pc; beq get1_n2e; adc r1,r1
908
getoff_n2e:
909
        add r4,r4; mov lr,pc; beq get1_n2e; adc r1,r1
910
        add r4,r4; mov lr,pc; beq get1_n2e; bcc off_n2e
911
 
912
        sub r3,r1,#3 @ set Carry
913
        mov r1,#0 @ Carry unaffected
914
        blo offprev_n2e @ r1 was 2; tests Carry only
915
        lsl r3,#8
916
        ldrb r5,[r0]; add r0,#1 @ low 7+1 r4
917
        orr r5,r3
918
        mvn r5,r5; beq nrv2e_done @ r5= ~r5
919
        asr r5,#1; bcs lenlast_n2e
920
        b lenmore_n2e
921
 
922
offprev_n2e:
923
        add r4,r4; mov lr,pc; beq get1_n2e; bcs lenlast_n2e
924
lenmore_n2e:
925
        mov r1,#1
926
        add r4,r4; mov lr,pc; beq get1_n2e; bcs lenlast_n2e
927
len_n2e:
928
        add r4,r4; mov lr,pc; beq get1_n2e; adc r1,r1
929
        add r4,r4; mov lr,pc; beq get1_n2e; bcc len_n2e
930
        add r1,#6-2
931
        b gotlen_n2e
932
 
933
lenlast_n2e:
934
        add r4,r4; mov lr,pc; beq get1_n2e; adc r1,r1 @ 0,1,2,3
935
        add r1,#2
936
gotlen_n2e: @ 'cmn': add the inputs, set condition codes, discard the sum
937
        cmn r6,r5; bcs near_n2e @ within M2_MAX_OFFSET
938
        add r1,#1 @ too far away, so minimum match length is 3
939
near_n2e:
940
        ldrb r3,[r2] @ force cacheline allocate
941
copy_n2e:
942
        ldrb r3,[r2,r5]
943
        strb r3,[r2]; add r2,#1
944
        sub r1,#1; bne copy_n2e
945
        b top_n2e
946
 
947
 
948
.code 32
949
.align 2
950
 
951
 
952
decompress:
953
	mov	r0, #0x24000000
954
	add	r0, r0, #0xa000
955
	mov	r2, #0x08000000
956
	adr	r12, thumb_nrv2e_d8 + 1
957
	bx	r12
958
 
959
 
960
recoverymode:
961
	add	r0, r11, #0x2100
962
	mov	r1, #0
963
	mov	r2, #0xff00
964
	orr	r2, r2, #0xff
965
	adr	r3, recoverytext
966
	bl	rendertext
967
	add	r0, r11, #0x2c00
968
	bl	rendertext
969
	mov	r0, #0
970
	mov	r1, #175
971
	mov	r2, #0
972
	mov	r3, #131
973
	str	r11, [sp]
974
	bl	displaylcd
975
	mov	r12, #0
976
	mov	r11, #1
977
MOV R8, #0x38800000
978
MOV R9, #0x3C400000
979
ADD R1, R9, #0x00100000    @ Enable USB clocks
980
LDR R0, [R1,#0x28]
981
BIC R0, R0, #0x4000
982
STR R0, [R1,#0x28]
983
LDR R0, [R1,#0x40]
984
BIC R0, R0, #0x800
985
STR R0, [R1,#0x40]
986
 
987
LDR R0, [R8,#0xE00]        @ PHY clock enable
988
BIC R0, R0, #3
989
STR R0, [R8,#0xE00]
990
 
991
LDR SP, [R8,#0x804]
992
ORR R0, SP, #2
993
STR R0, [R8,#0x804]        @ USB2 Gadget: Soft disconnect
994
 
995
BL sleep10ms
996
MOV R9, #0x3C400000
997
STR R12, [R9]              @ USB2 PHY: Power on
998
STR R11, [R9,#0x1C]        @ USB2 PHY: Undocumented
999
MOV R0, #0xE00
1000
ORR R0, R0, #0x3F
1001
STR R0, [R9,#0x44]         @ USB2 PHY: Undocumented
1002
LDR R0, [R9,#0x04]
1003
BIC R0, R0, #3
1004
STR R0, [R9,#0x04]         @ USB2 PHY: Clock is 48MHz
1005
LDR R4, [R9,#0x08]
1006
ORR R1, R4, #1
1007
STR R1, [R9,#0x08]         @ USB2 PHY: Assert Software Reset
1008
BL sleep10ms
1009
STR R12, [R9,#0x08]         @ USB2 PHY: Deassert Software Reset
1010
BL sleep10ms
1011
 
1012
STR R11, [R8,#0x10]        @ USB2 Gadget: Assert Core Software Reset
1013
waitcorereset:
1014
LDR R0, [R8,#0x10]         @ USB2 Gadget: Wait for Core to reset
1015
TST R0, #1
1016
BNE waitcorereset
1017
TST R0, #0x80000000        @ USB2 Gadget: Wait for AHB IDLE
1018
BEQ waitcorereset
1019
 
1020
MOV R0, #0xB6
1021
STR R0, [R8,#0x24]         @ USB2 Gadget: RX FIFO size: 728 bytes
1022
ORR R0, R0, #0x840000
1023
STR R0, [R8,#0x28]         @ USB2 Gadget: Non-periodic TX FIFO size: 528 bytes
1024
MOV R0, #0x26
1025
STR R0, [R8,#0x08]         @ USB2 Gadget: DMA Enable, Burst Length: 4, Mask Interrupts
1026
MOV R0, #0x1400
1027
ADD R0, R0, #8
1028
STR R0, [R8,#0x0C]         @ USB2 Gadget: PHY IF is 16bit, Turnaround 5
1029
STR SP, [R8,#0x804]        @ USB2 Gadget: Soft reconnect
1030
 
1031
@ fallthrough
1032
 
1033
mainloop:
1034
  LDR R3, [R8,#0x14]         @ Global USB interrupts
1035
  TST R3, #0x00001000        @ BUS reset
1036
  BEQ noreset
1037
    MOV R2, #0x500
1038
    STR R2, [R8,#0x804]
1039
    MOV R2, #4
1040
    STR R2, [R8,#0x800]        @ USB2 Gadget: Device Address 0, STALL on non-zero length status stage
1041
    MOV R2, #0x8000
1042
    STR R2, [R8,#0x900]        @ USB2 Gadget: Endpoint 0 IN Control: ACTIVE
1043
    STR R2, [R8,#0xB00]        @ USB2 Gadget: Endpoint 0 OUT Control: ACTIVE
1044
    SUB R5, R12, #1
1045
    STR R5, [R8,#0x908]        @ USB2 Gadget: Endpoint 0 IN Interrupt: ALL
1046
    STR R5, [R8,#0xB08]        @ USB2 Gadget: Endpoint 0 OUT Interrupt: ALL
1047
    LDR R2, val_20080040
1048
    STR R2, [R8,#0xB10]        @ USB2 Gadget: Endpoint 0 OUT Transfer Size: 64 Bytes, 1 Packet, 1 Setup Packet
1049
    MOV R2, #0x22000000
1050
    ORR R2, R2, #0x10000
1051
    STR R2, [R8,#0xB14]        @ USB2 Gadget: Endpoint 0 OUT DMA Address: 0x22010000
1052
    LDR R2, [R8,#0xB00]
1053
    ORR R2, R2, #0x84000000
1054
    STR R2, [R8,#0xB00]        @ USB2 Gadget: Endpoint 0 OUT Control: ENABLE CLEARNAK
1055
    LDR R2, val_00088210
1056
    STR R2, [R8,#0x960]        @ USB2 Gadget: Endpoint 3 IN Control: ACTIVE BULK, 528 byte packets
1057
    STR R2, [R8,#0xB80]        @ USB2 Gadget: Endpoint 4 OUT Control: ACTIVE BULK, 528 byte packets
1058
    STR R5, [R8,#0x968]        @ USB2 Gadget: Endpoint 3 IN Interrupt: ALL
1059
    STR R5, [R8,#0xB88]        @ USB2 Gadget: Endpoint 4 OUT Interrupt: ALL
1060
    LDR R2, val_00080210
1061
    STR R2, [R8,#0xB90]        @ USB2 Gadget: Endpoint 4 OUT Transfer Size: 528 Bytes, 1 Packet
1062
    MOV R2, #0x22000000
1063
    ORR R2, R2, #0x12000
1064
    STR R2, [R8,#0xB94]        @ USB2 Gadget: Endpoint 4 OUT DMA Address: 0x22012000
1065
    LDR R2, [R8,#0xB80]
1066
    ORR R2, R2, #0x94000000
1067
    STR R2, [R8,#0xB80]        @ USB2 Gadget: Endpoint 4 OUT Control: ENABLE CLEARNAK DATA0
1068
    STR R5, [R8,#0x810]        @ USB2 Gadget: IN Endpoint Interrupt Mask: ALL
1069
    STR R5, [R8,#0x814]        @ USB2 Gadget: OUT Endpoint Interrupt Mask: ALL
1070
    STR R5, [R8,#0x81C]        @ USB2 Gadget: Enable interrupts on all endpoints
1071
  noreset:
1072
  TST R3, #0x00040000        @ IN endpoint event
1073
  BEQ noinevent
1074
    LDR R4, [R8,#0x908]        @ Just ACK them all...
1075
    STR R4, [R8,#0x908]
1076
    LDR R4, [R8,#0x968]
1077
    STR R4, [R8,#0x968]
1078
  noinevent:
1079
  TST R3, #0x00080000        @ OUT endpoint event
1080
  BEQ nooutevent
1081
    LDR R4, [R8,#0xB08]
1082
    MOVS R4, R4                @ Event on OUT EP0
1083
    BEQ noep0out
1084
      TST R4, #8                 @ SETUP phase done
1085
      BEQ controldone
1086
        BL flushcache
1087
        MOV R5, #0x22000000
1088
        ORR R5, R5, #0x10000
1089
        LDRB R6, [R5,#0x01]        @ Get request type
1090
        CMP R6, #0
1091
          BEQ GET_STATUS
1092
        CMP R6, #1
1093
          BEQ CLEAR_FEATURE
1094
        CMP R6, #3
1095
          BEQ SET_FEATURE
1096
        CMP R6, #5
1097
          BEQ SET_ADDRESS
1098
        CMP R6, #6
1099
          BEQ GET_DESCRIPTOR
1100
        CMP R6, #8
1101
          BEQ GET_CONFIGURATION
1102
        CMP R6, #9
1103
          BEQ SET_CONFIGURATION
1104
        ctrlstall:
1105
        LDR R1, [R8,#0x900]
1106
        ORR R1, R1, #0x00200000
1107
        STR R1, [R8,#0x900]        @ Stall IN EP0
1108
        LDR R1, [R8,#0xB00]
1109
        ORR R1, R1, #0x00200000
1110
        STR R1, [R8,#0xB00]        @ Stall OUT EP0
1111
      controldone:
1112
      LDR R1, val_20080040
1113
      STR R1, [R8,#0xB10]        @ OUT EP0: 64 Bytes, 1 Packet, 1 Setup Packet
1114
      MOV R1, #0x22000000
1115
      ORR R1, R1, #0x10000
1116
      STR R1, [R8,#0xB14]        @ OUT EP0: DMA address
1117
      LDR R1, [R8,#0xB00]
1118
      ORR R1, R1, #0x84000000
1119
      STR R1, [R8,#0xB00]        @ OUT EP0: Enable ClearNAK
1120
    noep0out:
1121
    STR R4, [R8,#0xB08]        @ ACK it, whatever it was...
1122
    LDR R4, [R8,#0xB88]
1123
    MOVS R4, R4                @ Event on OUT EP4
1124
    BEQ noep1out
1125
      TST R4, #1                 @ XFER complete
1126
      BEQ datadone
1127
        BL flushcache
1128
        MOV R0, #0x22000000
1129
        ORR LR, R0, #0x800
1130
        ORR R0, R0, #0x12000
1131
        LDR R1, [R0]
1132
        LDR R2, [R0,#0x04]
1133
        CMP R1, #0                 @ EXECUTE, no feedback
1134
          MCREQ p15, 0, R12,c7,c5    @ Flush ICache
1135
          LDREQ SP, [R0,#0x08]
1136
          MOVEQ PC, R2
1137
        CMP R1, #1                 @ READ
1138
        BNE noread
1139
          LDR R1, [R0,#0x08]
1140
          MOV R6, R1,LSL#2
1141
          MOV R0, #0x22000000
1142
          ORR R0, R0, #0x13000
1143
          ORR R0, R0, #0x10
1144
          copydata:
1145
            LDR R5, [R2], #4
1146
            STR R5, [R0], #4
1147
            SUBS R1, R1, #1
1148
          BNE copydata
1149
          ADD R1, R6, #0x10
1150
          B sendsuccesscustomsize
1151
        noread:
1152
        CMP R1, #2                 @ WRITE
1153
        BNE sendunknownfunc
1154
          LDR R1, [R0,#0x08]
1155
          ADD R0, R0, #0x10
1156
          copydata2:
1157
            LDR R5, [R0], #4
1158
            STR R5, [R2], #4
1159
            SUBS R1, R1, #1
1160
          BNE copydata2
1161
	  BL flushcache
1162
          MOV R1, #0x10
1163
          @ fallthrough
1164
        sendsuccesscustomsize:
1165
          MOV R2, #0x22000000
1166
          ORR R2, R2, #0x13000
1167
          STMIA R2, {R11, R12}
1168
          @ fallthrough
1169
        sendlast2zero:
1170
          STR R12, [R2,#0x08]
1171
          STR R12, [R2,#0x0C]
1172
          @ fallthrough
1173
        datasend:
1174
          BL flushcache
1175
          LDR R0, val_00088210
1176
          STR R0, [R8,#0x960]        @ EP3 IN: ACTIVE BULK, 528 byte packets
1177
          ORR R1, R1, #0x20000000    @ 1 Packet at a time
1178
          ORR R1, R1, #0x00080000    @ 1 Packet
1179
          STR R1, [R8,#0x970]        @ EP3 IN: 1 Packet, 1 Packet at a time, Size as in R1
1180
          STR R2, [R8,#0x974]        @ EP3 IN: DMA address
1181
          LDR R1, [R8,#0x960]
1182
          ORR R1, R1, #0x84000000
1183
          STR R1, [R8,#0x960]        @ EP3 IN: Enable ClearNAK
1184
      datadone:
1185
      LDR R1, val_00080210
1186
      STR R1, [R8,#0xB90]        @ OUT EP4: 528 Bytes, 1 Packet
1187
      MOV R1, #0x22000000
1188
      ORR R1, R1, #0x12000
1189
      STR R1, [R8,#0xB94]        @ Out EP4: DMA address
1190
      LDR R1, [R8,#0xB80]
1191
      ORR R1, R1, #0x84000000
1192
      STR R1, [R8,#0xB80]        @ Out EP4: Enable ClearNAK
1193
    noep1out:
1194
    STR R4, [R8,#0xB88]        @ ACK it, whatever it was...
1195
  nooutevent:
1196
  STR R3, [R8,#0x14]         @ ACK it, whatever it was...
1197
B mainloop
1198
 
1199
sendunknownfunc:
1200
  MOV R0, #2
1201
  MOV R1, #0x10
1202
  MOV R2, #0x08400000
1203
  STMIA R2, {R0, R12}
1204
B sendlast2zero
1205
 
1206
GET_DESCRIPTOR:
1207
  LDRB R7, [R5,#3]           @ Descriptor type
1208
  CMP R7, #1
1209
    ADREQ R0, devicedescriptor
1210
    BEQ senddescriptor
1211
  CMP R7, #2
1212
    ADREQ R0, configurationdescriptor
1213
    MOVEQ R1, #0x20
1214
    BEQ senddescriptorcustomsize
1215
  CMP R7, #3
1216
  BNE ctrlstall
1217
  LDRB R7, [R5,#2]           @ String descriptor index
1218
  CMP R7, #0
1219
    ADREQ R0, langstringdescriptor
1220
    BEQ senddescriptor
1221
  CMP R7, #1
1222
  CMPNE R7, #2
1223
    ADREQ R0, devnamestringdescriptor
1224
  BNE ctrlstall
1225
@ fallthrough
1226
 
1227
senddescriptor:
1228
  LDRB R1, [R0]              @ Descriptor length
1229
@ fallthrough
1230
 
1231
senddescriptorcustomsize:
1232
  LDRH R5, [R5,#0x06]        @ Requested length
1233
  CMP R5, R1
1234
  MOVLO R1, R5
1235
  MOV R2, #0x22000000
1236
  ORR R2, R2, #0x11000
1237
  ADD R6, R1, R2
1238
  copydescriptor:
1239
    LDR R5, [R0], #4
1240
    STR R5, [R2], #4
1241
    CMP R2, R6
1242
  BCC copydescriptor
1243
B ctrlsend
1244
 
1245
GET_STATUS:
1246
  LDRB R1, [R5]
1247
  CMP R1, #0x80
1248
  MOV R0, #0x22000000
1249
  ORR R0, R0, #0x11000
1250
  STREQ R11, [R0]
1251
  STRNE R12, [R0]
1252
  MOV R1, #0x00000002
1253
B ctrlsend
1254
 
1255
CLEAR_FEATURE:
1256
  LDRB R2, [R5]
1257
  CMP R2, #2
1258
  LDREQ R2, [R5,#2]
1259
  BICEQ R2, R2, #0x00800000
1260
  CMPEQ R2, #0x00010000
1261
@ fallthrough
1262
 
1263
SET_CONFIGURATION:
1264
  LDREQ R2, [R8,#0x960]
1265
  ORREQ R2, R2, #0x10000000
1266
  STREQ R2, [R8,#0x960]      @ EP3 IN: Set DATA0 PID
1267
  LDREQ R2, [R8,#0xB80]
1268
  ORREQ R2, R2, #0x10000000
1269
  STREQ R2, [R8,#0xB80]      @ EP4 OUT: Set DATA0 PID
1270
B SET_FEATURE              @ zero-length ACK
1271
 
1272
SET_ADDRESS:
1273
  LDRH R2, [R5,#0x02]        @ new address
1274
  LDR R1, [R8,#0x800]
1275
  BIC R1, R1, #0x000007F0
1276
  ORR R1, R1, R2,LSL#4
1277
  STR R1, [R8,#0x800]        @ set new address
1278
@ fallthrough
1279
 
1280
SET_FEATURE:
1281
  MOV R1, #0                 @ zero-length ACK
1282
B ctrlsend
1283
 
1284
GET_CONFIGURATION:
1285
  MOV R1, #0x00000001
1286
  STR R1, [R0]
1287
@ fallthrough
1288
 
1289
ctrlsend:
1290
  BL flushcache
1291
  MOV R0, #0x22000000        @ Buffer to be sent
1292
  ORR R0, R0, #0x11000
1293
  MOV R2, #0x00009800
1294
  STR R2, [R8,#0x900]        @ EP0 IN: ACTIVE
1295
  ORR R1, R1, #0x00080000    @ 1 Packet
1296
  STR R1, [R8,#0x910]        @ EP0 IN: 1 Packet, Size as in R1
1297
  STR R0, [R8,#0x914]        @ EP0 IN: DMA address
1298
  LDR R1, [R8,#0x900]
1299
  ORR R1, R1, #0x84000000
1300
  STR R1, [R8,#0x900]        @ EP0 IN: Enable ClearNAK
1301
B controldone
1302
 
1303
val_00080210:
1304
.word 0x00080210
1305
 
1306
val_00088210:
1307
.word 0x00088210
1308
 
1309
val_20080040:
1310
.word 0x20080040
1311
 
1312
devicedescriptor:
1313
.word 0x02000112
1314
.word 0x40FFFFFF
1315
.word 0xE112FFFF
1316
.word 0x02010001
1317
.word 0x00010100
1318
 
1319
configurationdescriptor:
1320
.word 0x00200209
1321
.word 0xC0000101
1322
.word 0x00040932
1323
.word 0xFFFF0200
1324
.word 0x050700FF
1325
.word 0x02100204
1326
.word 0x83050701
1327
.word 0x01021002
1328
 
1329
langstringdescriptor:
1330
.word 0x04090304
1331
 
1332
devnamestringdescriptor:
1333
.byte 0x38
1334
.byte 3
1335
.ascii "e\0m\0B\0I\0O\0S\0 \0L\0o\0a\0d\0e\0r\0 \0R\0e\0c\0o\0v\0e\0r\0y\0 \0I\0N\0\062\0G\0"
1336
 
1337
 
1338
lcd_initmapping:
1339
	.word	lcd_inittab_type3 - lcd_initmapping_ref
1340
	.word	lcd_inittab_type7 - lcd_initmapping_ref
1341
	.word	lcd_inittab_type2 - lcd_initmapping_ref
1342
	.word	lcd_inittab_type3 - lcd_initmapping_ref
1343
 
1344
initlcd:
1345
	stmfd	sp!, {r4,r5,lr}
1346
	bl	detectlcd
1347
	sub	r12, r12, #0x04900000
1348
	adr	r5, lcd_initmapping
1349
	add	r5, r5, r0,lsl#2
1350
	ldr	r5, [r5]
1351
	add	r5, r5, pc
1352
	mov	r0, #0xc00
1353
lcd_initmapping_ref:
1354
	orr	r0, r0, #1
1355
	str	r0, [r12]
1356
	mov	r0, #0x7f00
1357
	orr	r0, r0, #0xff
1358
	str	r0, [r12,#0x24]
1359
	mov	r0, #0
1360
	str	r0, [r12,#0x28]
1361
	mov	r0, #1
1362
	bl	sleepms
1363
	mov	r0, #1
1364
	str	r0, [r12,#0x28]
1365
	mov	r0, #5
1366
	bl	sleepms
1367
initlcd_loop:
1368
	adr	lr, initlcd_loop
1369
	ldrb	r1, [r5], #1
1370
	ldrb	r0, [r5], #1
1371
	add	pc, pc, r1,lsl#2
1372
	nop
1373
	ldmfd	sp!, {r4,r5,pc}
1374
	b	sendlcdc
1375
	b	sendlcdd
1376
	b	sendlcd2c
1377
	b	sendlcd2d
1378
	b	sleepms
1379
 
1380
sendlcd2c:
1381
	ldr	r4, [r12,#0x1c]
1382
	ands	r4, r4, #0x10
1383
	bne	sendlcd2c
1384
	mov	r4, r0,lsr#8
1385
	str	r4, [r12,#0x04]
1386
	and	r0, r0, #0xff
1387
sendlcdc:
1388
	ldr	r4, [r12,#0x1c]
1389
	ands	r4, r4, #0x10
1390
	bne	sendlcdc
1391
	str	r0, [r12,#0x04]
1392
	mov	pc, lr
1393
sendlcd2d:
1394
	ldr	r4, [r12,#0x1c]
1395
	ands	r4, r4, #0x10
1396
	bne	sendlcd2d
1397
	mov	r4, r0,lsr#8
1398
	str	r4, [r12,#0x40]
1399
	and	r0, r0, #0xff
1400
sendlcdd:
1401
	ldr	r4, [r12,#0x1c]
1402
	ands	r4, r4, #0x10
1403
	bne	sendlcdd
1404
	str	r0, [r12,#0x40]
1405
	mov	pc, lr
1406
 
1407
detectlcd:
1408
	mov	r12, #0x3c000000
1409
	orr	r12, r12, #0xf00000
1410
	ldr	r0, [r12,#0xd0]
1411
	bic	r0, r0, #0x0f
1412
	str	r0, [r12,#0xd0]
1413
	ldr	r0, [r12,#0xe0]
1414
	bic	r0, r0, #0xf0
1415
	str	r0, [r12,#0xe0]
1416
	ldr	r0, [r12,#0xd4]
1417
	and	r0, r0, #1
1418
	ldr	r1, [r12,#0xe4]
1419
	and	r1, r1, #2
1420
	orr	r0, r0, r1
1421
	mov	pc, lr
1422
 
1423
displaylcd:
1424
	stmfd	sp!, {r0,r1,r4,lr}
1425
	bl	displaylcd_sync
1426
	bl	detectlcd
1427
	sub	r12, r12, #0x04900000
1428
	cmp	r0, #2
1429
	bne	displaylcd_othertypes
1430
	mov	r0, #0x50
1431
	bl	sendlcd2c
1432
	ldr	r0, [sp]
1433
	bl	sendlcd2d
1434
	mov	r0, #0x51
1435
	bl	sendlcd2c
1436
	ldr	r0, [sp,#0x04]
1437
	bl	sendlcd2d
1438
	mov	r0, #0x52
1439
	bl	sendlcd2c
1440
	mov	r0, r2
1441
	bl	sendlcd2d
1442
	mov	r0, #0x53
1443
	bl	sendlcd2c
1444
	mov	r0, r3
1445
	bl	sendlcd2d
1446
	mov	r0, #0x20
1447
	bl	sendlcd2c
1448
	ldr	r0, [sp]
1449
	bl	sendlcd2d
1450
	mov	r0, #0x21
1451
	bl	sendlcd2c
1452
	mov	r0, r2
1453
	bl	sendlcd2d
1454
	mov	r0, #0x22
1455
	bl	sendlcd2c
1456
	b	displaylcd_blit
1457
displaylcd_othertypes:
1458
	mov	r0, #0x2a
1459
	bl	sendlcdc
1460
	ldr	r0, [sp]
1461
	bl	sendlcd2d
1462
	ldr	r0, [sp,#0x04]
1463
	bl	sendlcd2d
1464
	mov	r0, #0x2b
1465
	bl	sendlcdc
1466
	mov	r0, r2
1467
	bl	sendlcd2d
1468
	mov	r0, r3
1469
	bl	sendlcd2d
1470
	mov	r0, #0x2c
1471
	bl	sendlcdc
1472
displaylcd_blit:
1473
	ldmia	sp, {r0,r1}
1474
	sub	r1, r0
1475
	add	r1, r1, #1
1476
	sub	r3, r2
1477
	add	r3, r3, #1
1478
	mul	r2, r1, r3
1479
	ldr	r1, [sp,#0x10]
1480
	cmp	r1, #0x40000000
1481
	bne	displaylcd_dma
1482
displaylcd_pixel:
1483
	ldr	r0, [sp,#0x14]
1484
	bl	sendlcd2d
1485
	subs	r2, r2, #1
1486
	bne	displaylcd_pixel
1487
	ldmfd	sp!, {r0,r1,r4,pc}
1488
displaylcd_dma:
1489
	mov	r3, #0x38000000
1490
	add	r3, r3, #0x400000
1491
	mov	r0, #0x20000000
1492
	orr	r0, r0, #0x180000
1493
	str	r0, [r3,#0x104]
1494
	str	r1, [r3,#0x100]
1495
	mov	r0, r2,lsr#1
1496
	sub	r0, r0, #1
1497
	str	r0, [r3,#0x108]
1498
	bl	flushcache
1499
	mov	r0, #4
1500
	str	r0, [r3,#0x114]
1501
	ldmfd	sp!, {r0,r1,r4,pc}
1502
 
1503
displaylcd_sync:
1504
	mov	r1, #0x38000000
1505
	add	r1, r1, #0x400000
1506
displaylcd_sync_wait:
1507
	ldr	r0, [r1,#0x184]
1508
	tst	r0, #0x40000
1509
	bne	displaylcd_sync_wait
1510
	mov	pc, lr
1511
 
1512
 
1513
getpowerok:
1514
	stmfd	sp!, {lr}
1515
	mov	r0, #0xe6
1516
	mov	r1, #0x19
1517
	bl	i2crecvbyte
1518
	eor	r0, r0, #1
1519
	ands	r0, r0, #1
1520
	ldmnefd	sp!, {pc}
1521
	mov	r0, #0xe6
1522
	mov	r1, #0x4b
1523
	bl	i2crecvbyte
1524
	ands	r0, r0, #4
1525
	movne	r0, #1
1526
	ldmnefd	sp!, {pc}
1527
	mov	r0, #0xe6
1528
	mov	r1, #0x12
1529
	bl	i2crecvbyte
1530
	ands	r0, r0, #4
1531
	movne	r0, #1
1532
	ldmfd	sp!, {pc}
1533
 
1534
poweroff:
1535
	mov	r0, #0xe6
1536
	mov	r1, #0x0c
1537
	mov	r2, #0x01
1538
	bl	i2csendbyte
1539
	b	poweroff
1540
 
1541
i2csendbyte:
1542
	mov	r3, #0
1543
@fallthrough
1544
 
1545
i2csend:
1546
	stmfd	sp!, {r4,lr}
1547
	mov	r12, #0x3C000000
1548
	add	r12, r12, #0x00900000
1549
	mov	r4, #0
1550
	str	r4, [r12,#0x08]
1551
	str	r0, [r12,#0x0c]
1552
	mov	r4, #0xf0
1553
	str	r4, [r12,#0x04]
179 theseven 1554
	mov	r4, #0xb7
159 theseven 1555
	str	r4, [r12]
1556
	bl	i2cwait
1557
	str	r1, [r12,#0x0c]
1558
	str	r4, [r12]
1559
	bl	i2cwait
1560
	movs	r3, r3
1561
	moveq	r0, r2
1562
i2csend_write:
1563
	ldrne	r0, [r2], #1
1564
	str	r0, [r12,#0x0c]
1565
	str	r4, [r12]
1566
	bl	i2cwait
1567
	subs	r3, r3, #1
1568
	bhi	i2csend_write
1569
	mov	r0, #0xd0
1570
	str	r0, [r12,#0x04]
1571
	str	r4, [r12]
1572
i2csend_wait:
1573
	ldr	r0, [r12,#0x04]
1574
	tst	r0, #0x20
1575
	bne	i2csend_wait
1576
	ldmfd	sp!, {r4,pc}
1577
 
1578
i2crecvbyte:
1579
	mov	r2, #0
1580
	mov	r3, #1
1581
@fallthrough
1582
 
1583
i2crecv:
1584
	stmfd	sp!, {r0,r4,lr}
1585
	mov	r12, #0x3C000000
1586
	add	r12, r12, #0x00900000
1587
	mov	r4, #0
1588
	str	r4, [r12,#0x08]
1589
	str	r0, [r12,#0x0c]
1590
	mov	r4, #0xf0
1591
	str	r4, [r12,#0x04]
179 theseven 1592
	mov	r4, #0xb7
159 theseven 1593
	str	r4, [r12]
1594
	bl	i2cwait
1595
	str	r1, [r12,#0x0c]
1596
	str	r4, [r12]
1597
	bl	i2cwait
1598
	ldr	r0, [sp]
1599
	orr	r0, r0, #1
1600
	str	r1, [r12,#0x0c]
1601
	mov	r0, #0xb0
1602
	str	r0, [r12,#0x04]
1603
	str	r4, [r12]
1604
	bl	i2cwait
1605
i2crecv_read:
1606
	subs	r3, r3, #1
179 theseven 1607
	moveq	r4, #0x37
159 theseven 1608
	str	r4, [r12]
1609
	bl	i2cwait
1610
	ldr	r0, [r12,#0x0c]
1611
	movs	r2, r2
1612
	strne	r0, [r2], #1
1613
	movs	r3, r3
1614
	bne	i2crecv_read
1615
	mov	r1, #0x90
1616
	str	r1, [r12,#0x04]
179 theseven 1617
	mov	r1, #0xb7
159 theseven 1618
	str	r1, [r12]
1619
i2crecv_wait:
1620
	ldr	r1, [r12,#0x04]
1621
	tst	r1, #0x20
1622
	bne	i2crecv_wait
1623
	ldmfd	sp!, {r0,r4,pc}
1624
 
1625
i2cwait:
1626
	ldr	r0, [r12]
1627
	tst	r0, #0x10
1628
	beq	i2cwait
1629
	mov	pc, lr
1630
 
1631
 
1632
sleep10ms:
1633
	mov	r0, #10
1634
@ fallthrough
1635
 
1636
sleepms:
1637
	mov	r2, #0xc8
1638
	mul	r1, r0, r2
1639
	mov	r2, #0x3c000000
1640
	orr	r2, r2, #0x700000
1641
	ldr	r0, [r2,#0x80]
1642
	ldr	r0, [r2,#0x84]
1643
	add	r1, r1, r0
1644
sleepmsloop:
1645
	ldr	r0, [r2,#0x80]
1646
	ldr	r0, [r2,#0x84]
1647
	cmp	r0, r1
1648
	bmi	sleepmsloop
1649
	mov	pc, lr