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