Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
273 theseven 1
@
2
@
3
@    emBIOS Loader for iPod Nano 3G
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
	mcr	p15, 0, r0,c8,c7
39
	mcr	p15, 0, r0,c13,c0
40
	adr	lr, cacheflush_done
41
flushcache:
42
	mrc	p15, 0, r15,c7,c14,3
43
	bne	flushcache
44
	mov	r0, #0
45
	mcr	p15, 0, r0,c7,c10,4
46
	bx	lr
47
cacheflush_done:
48
	adr	sp, values1
49
	ldmia	sp!, {r4-r12}
50
	mcr	p15, 0, r11,c2,c0
51
	mov	r0, #-1
52
	mcr	p15, 0, r0,c3,c0
53
mmuloop:
54
	str	r12, [r11], #4
55
	add	r12, r12, #0x00100000
56
	cmp	r12, #0x38000000
57
	biccs	r12, r12, #0xc
58
	tst	r12, #0x40000000
59
	beq	mmuloop
60
	mcr	p15, 0, r10,c1,c0
61
dmaloop:
62
	add	r8, r8, #0x10
63
	bic	r9, r9, r4
64
	cmp	r4, r5
65
	movcs	r4, r5
66
	movcs	r8, #0
67
	orr	r9, r9, r4
68
	stmia	r11!, {r6-r9}
69
	add	r6, r6, r4,lsl#1
70
	subs	r5, r5, r4
71
	bne	dmaloop
72
	ldmia	sp!, {r0-r12}        @ R0 = SYSCONBASE (temporary)
73
	str	r1, [r0,#0x48]
74
	str	r2, [r0,#0x4c]
75
	mov	r2, #0               @ R2 = 0
76
	str	r2, [r0]
77
	str	r2, [r0,#0x4]
78
	mov	r1, #7
79
	str	r1, [r0,#0x44]
80
	mov	r1, #0x30
81
	str	r1, [r0,#0x44]
82
	str	r2, [r0,#0x60]
83
	str	r3, [r0,#0x28]
84
	str	r4, [r0,#0x38]
85
	mov	r1, #0x34
86
	str	r1, [r0,#0x44]
87
	orr	r1, r1, #0x40000
88
	str	r1, [r0,#0x44]
89
waitpll:
90
	ldr	r1, [r0,#0x40]
91
	tst	r1, #4
92
	beq	waitpll
93
	str	r5, [r0,#0x04]
94
	mov	r1, #0x3000
95
	str	r1, [r0]
96
	mov	r1, #0x8000
97
	orr	r3, r1, #0x80000000
98
	str	r3, [r0,#0x8]
99
	str	r3, [r0,#0xc]
100
	str	r1, [r0,#0x10]
101
	str	r1, [r0,#0x14]
102
	mov	r3, #3
103
	str	r3, [r6]
104
	orr	r1, r0, #0x00200000  @ R1 = TIMERBASE
105
	mov	r4, #0x440
106
	str	r4, [r1,#0xa0]
107
	mov	r4, #0xb
108
	str	r4, [r1,#0xb0]
109
	mvn	r4, r2
110
	str	r4, [r1,#0xa8]
111
	str	r3, [r1,#0xa4]
112
	orr	r3, r1, #0x00800000  @ R3 = GPIOBASE (temporary)
113
gpioloop:
114
	ldr	r4, [sp], #4
115
	str	r4, [r3], #0xc
116
	str	r2, [r3], #0x4
117
	str	r2, [r3], #0x10
118
	tst	r3, #0x200
119
	beq	gpioloop
120
	str	r7, [r3]
121
	add	r7, r7, #0x100
122
	str	r7, [r3]
123
	ldr	r7, [r3,#-0x30]
124
	bic	r7, r7, #0x40
125
	str	r7, [r3,#-0x30]
126
	ldr	r7, [r3,#-0x34]
127
	orr	r7, r7, #0x40
128
	str	r7, [r3,#-0x34]
129
	add	r5, r0, #0x00100000  @ R5 = I2CBASE
130
	bl	i2cwaitrdy           @ R3 = I2C address (temporary)
131
	mov	r7, #0x40            @ R4 = I2C data (temporary)
132
	str	r7, [r5,#8]          @ R6 = I2C scratch register (temporary)
133
	bl	i2cwaitrdy           @ R7 = I2C return address (temporary)
134
	str	r2, [r5,#0x14]       @ R0 = coldboot flag (temporary)
135
	bl	i2cwaitrdy
136
	str	r2, [r5,#0x18]
137
	bl	i2cwaitrdy
138
	mov	r6, #0x80
139
	str	r6, [r5,#0x4]
140
	bl	i2cwaitrdy
141
	str	r2, [r5]
142
	bl	i2cwaitrdy
143
	str	r2, [r5,#0x4]
144
	bl	i2cwaitrdy
145
	str	r7, [r5,#0xc]
146
	bl	i2cwaitrdy
147
	mov	r6, #0x184
148
	str	r6, [r5]
149
	bl	i2cwaitrdy
150
	mov	r6, #0x10
151
	str	r6, [r5,#0x4]
152
	bl	i2cwaitrdy
153
	mov	r3, #0x16
154
	mvn	r4, r2
155
	bl	pmutransfer
156
	and	r0, r4, #7
157
	mov	r3, #0xc
158
	mvn	r4, r2
159
	bl	pmutransfer
160
	and	r4, r4, #8
161
	orr	r0, r0, r4
162
pmuloop:
163
	ldrb	r3, [sp], #1
164
	ldrb	r4, [sp], #1
165
	cmp	r3, #0xff
166
	adrne	lr, pmuloop
167
	bne	pmutransfer
168
	mov	r3, #0x19
169
	mvn	r4, r2
170
	bl	pmutransfer
171
	tst	r4, #1
172
	beq	pwrok
173
	mov	r3, #0x4b
174
	mvn	r4, r2
175
	bl	pmutransfer
176
	tst	r4, #4
177
	bne	pwrok
178
	mov	r3, #0x12
179
	mvn	r4, r2
180
	bl	pmutransfer
181
	tst	r4, #4
182
	bne	pwrok
183
poweroff:
184
	mov	r3, #0xc
185
	mov	r4, #1
186
	bl	pmutransfer
187
	b	poweroff
188
 
189
values1:
190
	.word 0x00000FFF @ R4 - DMA maximum transfer size
191
	.word 0x00012C00 @ R5 - LCD framebuffer pixel count
192
	.word 0x22010000 @ R6 - LCD framebuffer address
193
	.word 0x38300040 @ R7 - LCD data port address
194
	.word 0x2203D000 @ R8 - LCD DMA linked list address
195
	.word 0x74240000 @ R9 - LCD DMA channel settings
196
	.word 0x0005107D @ R10 - CP15r1
197
	.word 0x2203C000 @ R11 - First level page table
198
	.word 0x00000C1E @ R12 - Default segment flags
199
	.word 0x3C500000 @ R0 - SYSCON base
200
	.word 0x2007CE65 @ R1 - PWRCON(0)
201
	.word 0x0003EFC9 @ R2 - PWRCON(1)
202
	.word 0x01002401 @ R3
203
	.word 0x00007E90 @ R4
204
	.word 0x00404101 @ R5
205
	.word 0x38501000 @ R6
206
	.word 0x00060400 @ R7
207
	.word 0x0000080D @ R8
208
	.word 0x0006105D @ R9
209
	.word 0x001FB621 @ R10
210
	.word 0x0790682B @ R11
211
	.word 0x00008040 @ R12
212
 
213
	.word 0x5322222F @ PCON0
214
	.word 0xEEEEEE00 @ PCON1
215
	.word 0x2332EEEE @ PCON2
216
	.word 0x3333E222 @ PCON3
217
	.word 0x33333333 @ PCON4
218
	.word 0x33333333 @ PCON5
219
	.word 0x3F000E33 @ PCON6
220
	.word 0xEEEEEEEE @ PCON7
221
	.word 0xEEEEEEEE @ PCON8
222
	.word 0xEEEEEEEE @ PCON9
223
	.word 0xEEEEEEEE @ PCONA
224
	.word 0xEE00EE0E @ PCONB
225
	.word 0xEEEE0EEE @ PCONC
226
	.word 0xEEEEEEEE @ PCOND
227
	.word 0x202222EE @ PCONE
228
	.word 0xEEEE0E22 @ PCONF
229
 
230
	.byte 0x0c, 0    @ PMU init address-data pairs
231
	.byte 0x2d, 0x15
232
	.byte 0x2e, 1
233
	.byte 0x37, 0x15
234
	.byte 0x38, 4
235
	.byte 0x2f, 0x15
236
	.byte 0x30, 1
237
	.byte 0x3b, 0
238
	.byte 0x3c, 0x8c
239
	.byte 0x13, 3
240
	.byte 0x14, 0
241
	.byte 0x15, 0
242
	.byte 0x22, 0x2f
243
	.byte 0x23, 1
244
	.byte 0x24, 0
245
	.byte 0x25, 0x10
246
	.byte 0x26, 9
247
	.byte 0x27, 1
248
	.byte 0x1a, 0x6f
249
	.byte 0x1b, 0
250
	.byte 0x1c, 0
251
	.byte 0x1d, 0x59
252
	.byte 0x33, 9
253
	.byte 0x34, 1
254
	.byte 0x39, 0x18
255
	.byte 0x3a, 0
256
	.byte 0x18, 0xa
257
	.byte 0x19, 4
258
	.byte 0x58, 0
259
	.byte 0x07, 0xb0
260
	.byte 0x08, 3
261
	.byte 0x09, 0xfe
262
	.byte 0x0a, 0xfc
263
	.byte 0x0b, 0xff
264
	.byte 0x86, 0xfd
265
	.byte 0x0d, 0xdf
266
	.byte 0x0e, 0xaa
267
	.byte 0x0f, 0x4a
268
	.byte 0x10, 5
269
	.byte 0x11, 0x27
270
	.byte 0x17, 1
271
	.byte 0x2a, 5
272
	.byte 0xff, 0xff @ PMU init terminator
273
 
274
lcddata_ref:
275
	.hword lcddata_0 - lcddata_ref
276
	.hword lcddata_1 - lcddata_ref - 2
277
	.hword lcddata_2 - lcddata_ref - 4
278
	.hword lcddata_3 - lcddata_ref - 6
279
lcddata_0:
280
	.hword 0x4011
281
	.hword 0xc78f
282
	.hword 0x8135
283
	.byte 0x00
284
	.hword 0x813a
285
	.byte 0x06
286
	.hword 0x8136
287
	.byte 0x00
288
	.hword 0x4013
289
	.hword 0x4029
290
	.hword 0xc000
291
lcddata_1:
292
	.hword 0x95b0
293
	.byte 0x3a
294
	.byte 0x3a
295
	.byte 0x80
296
	.byte 0x80
297
	.byte 0x0a
298
	.byte 0x0a
299
	.byte 0x0a
300
	.byte 0x0a
301
	.byte 0x0a
302
	.byte 0x0a
303
	.byte 0x0a
304
	.byte 0x0a
305
	.byte 0x3c
306
	.byte 0x30
307
	.byte 0x0f
308
	.byte 0x00
309
	.byte 0x01
310
	.byte 0x54
311
	.byte 0x06
312
	.byte 0x66
313
	.byte 0x66
314
	.hword 0x81b8
315
	.byte 0xd8
316
	.hword 0x9eb1
317
	.byte 0x14
318
	.byte 0x59
319
	.byte 0x00
320
	.byte 0x15
321
	.byte 0x57
322
	.byte 0x27
323
	.byte 0x04
324
	.byte 0x85
325
	.byte 0x14
326
	.byte 0x59
327
	.byte 0x00
328
	.byte 0x15
329
	.byte 0x57
330
	.byte 0x27
331
	.byte 0x04
332
	.byte 0x85
333
	.byte 0x14
334
	.byte 0x09
335
	.byte 0x15
336
	.byte 0x57
337
	.byte 0x27
338
	.byte 0x04
339
	.byte 0x05
340
	.byte 0x14
341
	.byte 0x09
342
	.byte 0x15
343
	.byte 0x57
344
	.byte 0x27
345
	.byte 0x04
346
	.byte 0x05
347
	.hword 0x40d2
348
	.hword 0x0101
349
	.hword 0x8de0
350
	.byte 0x00
351
	.byte 0x00
352
	.byte 0x00
353
	.byte 0x05
354
	.byte 0x0b
355
	.byte 0x12
356
	.byte 0x16
357
	.byte 0x1f
358
	.byte 0x25
359
	.byte 0x22
360
	.byte 0x24
361
	.byte 0x29
362
	.byte 0x1c
363
	.hword 0x8de1
364
	.byte 0x08
365
	.byte 0x01
366
	.byte 0x01
367
	.byte 0x06
368
	.byte 0x0b
369
	.byte 0x11
370
	.byte 0x15
371
	.byte 0x1f
372
	.byte 0x27
373
	.byte 0x26
374
	.byte 0x29
375
	.byte 0x2f
376
	.byte 0x1e
377
	.hword 0x8de2
378
	.byte 0x07
379
	.byte 0x01
380
	.byte 0x01
381
	.byte 0x05
382
	.byte 0x09
383
	.byte 0x0f
384
	.byte 0x13
385
	.byte 0x1e
386
	.byte 0x26
387
	.byte 0x25
388
	.byte 0x28
389
	.byte 0x2e
390
	.byte 0x1e
391
	.hword 0x8de3
392
	.byte 0x0d
393
	.byte 0x00
394
	.byte 0x00
395
	.byte 0x00
396
	.byte 0x05
397
	.byte 0x0b
398
	.byte 0x12
399
	.byte 0x16
400
	.byte 0x1f
401
	.byte 0x25
402
	.byte 0x22
403
	.byte 0x24
404
	.byte 0x29
405
	.byte 0x1c
406
	.hword 0x8de4
407
	.byte 0x08
408
	.byte 0x01
409
	.byte 0x01
410
	.byte 0x06
411
	.byte 0x0b
412
	.byte 0x11
413
	.byte 0x15
414
	.byte 0x1f
415
	.byte 0x27
416
	.byte 0x26
417
	.byte 0x29
418
	.byte 0x2f
419
	.byte 0x1e
420
	.hword 0x8de5
421
	.byte 0x07
422
	.byte 0x01
423
	.byte 0x01
424
	.byte 0x05
425
	.byte 0x09
426
	.byte 0x0f
427
	.byte 0x13
428
	.byte 0x1e
429
	.byte 0x26
430
	.byte 0x25
431
	.byte 0x28
432
	.byte 0x2e
433
	.byte 0x1e
434
	.hword 0x813a
435
	.byte 0x06
436
	.hword 0x81c2
437
	.byte 0x00
438
	.hword 0x8135
439
	.byte 0x00
440
	.hword 0x4011
441
	.hword 0xce88
442
	.hword 0x4013
443
	.hword 0x4029
444
lcddata_2:
445
	.hword 0xc000
446
lcddata_3:
447
	.hword 0x4008
448
	.hword 0x0808
449
	.hword 0x8110
450
	.byte 0x13
451
	.hword 0x4011
452
	.hword 0x0300
453
	.hword 0x4012
454
	.hword 0x0101
455
	.hword 0x4013
456
	.hword 0x0a03
457
	.hword 0x4014
458
	.hword 0x0a0e
459
	.hword 0x4015
460
	.hword 0x0a19
461
	.hword 0x4016
462
	.hword 0x2402
463
	.hword 0x8118
464
	.byte 0x01
465
	.hword 0x8190
466
	.byte 0x21
467
	.hword 0x4300
468
	.hword 0x0506
469
	.hword 0x4301
470
	.hword 0x0402
471
	.hword 0x4302
472
	.hword 0x0706
473
	.hword 0x4303
474
	.hword 0x0303
475
	.hword 0x4304
476
	.hword 0x0100
477
	.hword 0x4305
478
	.hword 0x0207
479
	.hword 0x4306
480
	.hword 0x170e
481
	.hword 0x4307
482
	.hword 0x0506
483
	.hword 0x4308
484
	.hword 0x0402
485
	.hword 0x4309
486
	.hword 0x0706
487
	.hword 0x430a
488
	.hword 0x0303
489
	.hword 0x430b
490
	.hword 0x0100
491
	.hword 0x430c
492
	.hword 0x0207
493
	.hword 0x430d
494
	.hword 0x170e
495
	.hword 0x4310
496
	.hword 0x0606
497
	.hword 0x4311
498
	.hword 0x0506
499
	.hword 0x4312
500
	.hword 0x0707
501
	.hword 0x4313
502
	.hword 0x0303
503
	.hword 0x4314
504
	.hword 0x0200
505
	.hword 0x4315
506
	.hword 0x0307
507
	.hword 0x4316
508
	.hword 0x170e
509
	.hword 0x4317
510
	.hword 0x0606
511
	.hword 0x4318
512
	.hword 0x0506
513
	.hword 0x4319
514
	.hword 0x0707
515
	.hword 0x431a
516
	.hword 0x0303
517
	.hword 0x431b
518
	.hword 0x0200
519
	.hword 0x431c
520
	.hword 0x0307
521
	.hword 0x431d
522
	.hword 0x170e
523
	.hword 0x4320
524
	.hword 0x0707
525
	.hword 0x4321
526
	.hword 0x0005
527
	.hword 0x4322
528
	.hword 0x0705
529
	.hword 0x4323
530
	.hword 0x0303
531
	.hword 0x4324
532
	.hword 0x0300
533
	.hword 0x4325
534
	.hword 0x0707
535
	.hword 0x4326
536
	.hword 0x1f16
537
	.hword 0x4327
538
	.hword 0x0707
539
	.hword 0x4328
540
	.hword 0x0005
541
	.hword 0x4329
542
	.hword 0x0705
543
	.hword 0x432a
544
	.hword 0x0303
545
	.hword 0x432b
546
	.hword 0x0300
547
	.hword 0x432c
548
	.hword 0x0707
549
	.hword 0x432d
550
	.hword 0x1f16
551
	.hword 0x4400
552
	.hword 0x001d
553
	.hword 0x4401
554
	.hword 0x0001
555
	.hword 0x4205
556
	.hword 0x0060
557
	.hword 0x8107
558
	.byte 0x01
559
	.hword 0x8131
560
	.byte 0x71
561
	.hword 0x4110
562
	.hword 0x0001
563
	.hword 0x4100
564
	.hword 0x17b0
565
	.hword 0x4101
566
	.hword 0x0220
567
	.hword 0x4102
568
	.hword 0x009b
569
	.hword 0x4103
570
	.hword 0x1200
571
	.hword 0x4105
572
	.hword 0x0103
573
	.hword 0x4106
574
	.hword 0x0105
575
	.hword 0x8107
576
	.byte 0x21
577
	.hword 0x4102
578
	.hword 0x00bd
579
	.hword 0x4001
580
	.hword 0x0110
581
	.hword 0x4003
582
	.hword 0x0230
583
	.hword 0x4002
584
	.hword 0x0500
585
	.hword 0x8107
586
	.byte 0x31
587
	.hword 0x4130
588
	.byte 0x07
589
	.hword 0xc744
590
	.hword 0x4030
591
	.hword 0x03ff
592
	.hword 0xce88
593
	.hword 0x8107
594
	.byte 0x72
595
	.hword 0xe454
596
	.hword 0x4007
597
	.hword 0x0173
598
	.hword 0xc000
599
 
600
font:
601
	.byte 0, 0, 0, 0, 0
602
	.byte 0, 0, 95, 0, 0
603
	.byte 0, 7, 0, 7, 0
604
	.byte 20, 127, 20, 127, 20
605
	.byte 36, 42, 127, 42, 18
606
	.byte 35, 19, 8, 100, 98
607
	.byte 54, 73, 85, 34, 80
608
	.byte 5, 3, 0, 0, 0
609
	.byte 28, 34, 65, 0, 0
610
	.byte 0, 0, 65, 34, 28
611
	.byte 20, 8, 62, 8, 20
612
	.byte 8, 8, 62, 8, 8
613
	.byte 0, -96, 96, 0, 0
614
	.byte 8, 8, 8, 8, 8
615
	.byte 0, 96, 96, 0, 0
616
	.byte 32, 16, 8, 4, 2
617
	.byte 62, 81, 73, 69, 62
618
	.byte 0, 66, 127, 64, 0
619
	.byte 66, 97, 81, 73, 70
620
	.byte 33, 65, 69, 75, 49
621
	.byte 24, 20, 18, 127, 16
622
	.byte 39, 69, 69, 69, 57
623
	.byte 60, 74, 73, 73, 48
624
	.byte 1, 113, 9, 5, 3
625
	.byte 54, 73, 73, 73, 54
626
	.byte 6, 73, 73, 41, 30
627
	.byte 0, 54, 54, 0, 0
628
	.byte 0, 86, 54, 0, 0
629
	.byte 8, 20, 34, 65, 0
630
	.byte 20, 20, 20, 20, 20
631
	.byte 0, 65, 34, 20, 8
632
	.byte 2, 1, 81, 9, 6
633
	.byte 50, 73, 121, 65, 62
634
	.byte 124, 18, 17, 18, 124
635
	.byte 127, 73, 73, 73, 62
636
	.byte 62, 65, 65, 65, 34
637
	.byte 127, 65, 65, 34, 28
638
	.byte 127, 73, 73, 73, 65
639
	.byte 127, 9, 9, 9, 1
640
	.byte 62, 65, 73, 73, 58
641
	.byte 127, 8, 8, 8, 127
642
	.byte 0, 65, 127, 65, 0
643
	.byte 32, 64, 65, 63, 1
644
	.byte 127, 8, 20, 34, 65
645
	.byte 127, 64, 64, 64, 64
646
	.byte 127, 2, 12, 2, 127
647
	.byte 127, 4, 8, 16, 127
648
	.byte 62, 65, 65, 65, 62
649
	.byte 127, 9, 9, 9, 6
650
	.byte 62, 65, 81, 33, 94
651
	.byte 127, 9, 25, 41, 70
652
	.byte 38, 73, 73, 73, 50
653
	.byte 1, 1, 127, 1, 1
654
	.byte 63, 64, 64, 64, 63
655
	.byte 31, 32, 64, 32, 31
656
	.byte 127, 32, 24, 32, 127
657
	.byte 99, 20, 8, 20, 99
658
	.byte 3, 4, 120, 4, 3
659
	.byte 97, 81, 73, 69, 67
660
	.byte 0, 127, 65, 65, 0
661
	.byte 2, 4, 8, 16, 32
662
	.byte 0, 65, 65, 127, 0
663
	.byte 4, 2, 1, 2, 4
664
	.byte 64, 64, 64, 64, 64
665
	.byte 1, 2, 4, 0, 0
666
	.byte 32, 84, 84, 84, 120
667
	.byte 127, 68, 68, 68, 56
668
	.byte 56, 68, 68, 68, 40
669
	.byte 56, 68, 68, 68, 127
670
	.byte 56, 84, 84, 84, 24
671
	.byte 8, 126, 9, 1, 2
672
	.byte 8, 84, 84, 84, 60
673
	.byte 127, 4, 4, 4, 120
674
	.byte 0, 68, 125, 64, 0
675
	.byte 32, 64, 64, 61, 0
676
	.byte 127, 16, 40, 68, 0
677
	.byte 0, 65, 127, 64, 0
678
	.byte 124, 4, 24, 4, 120
679
	.byte 124, 8, 4, 4, 120
680
	.byte 56, 68, 68, 68, 56
681
	.byte 124, 20, 20, 20, 24
682
	.byte 8, 20, 20, 20, 124
683
	.byte 124, 8, 4, 4, 8
684
	.byte 72, 84, 84, 84, 32
685
	.byte 4, 63, 68, 64, 32
686
	.byte 60, 64, 64, 32, 124
687
	.byte 28, 32, 64, 32, 28
688
	.byte 60, 64, 56, 64, 60
689
	.byte 68, 40, 16, 40, 68
690
	.byte 12, 80, 80, 80, 60
691
	.byte 68, 100, 84, 76, 68
692
	.byte 0, 8, 54, 65, 0
693
	.byte 0, 0, 119, 0, 0
694
	.byte 0, 65, 54, 8, 0
695
	.byte 2, 1, 2, 4, 2
696
 
697
	.code 16
698
	.align 1
699
 
700
thumb_nrv2e_d8:
701
	mov r7,r2
702
        mov r4,#1; neg r5,r4 @ r5= -1 initial condition
703
        lsl r4,#31 @ 1<<31: refill next time
704
        mov r6,#5
705
        lsl r6,#8 @ 0x500 @ nrv2e M2_MAX_OFFSET
706
        b top_n2e
707
 
708
nrv2e_done:
709
	blx flushcache
710
	bx r7
711
 
712
get1_n2e: @ In: Carry set [from adding 0x80000000 (1<<31) to itself]
713
        ldrb r4,[r0] @ zero-extend next byte
714
        adc r4,r4 @ double and insert CarryIn as low bit
715
        add r0,#1
716
        lsl r4,#24 @ move to top byte, and set CarryOut from old bit 8
717
        mov pc,lr @ return, stay in current (THUMB) mode
718
 
719
lit_n2e:
720
        ldrb r3,[r0]; add r0,#1
721
        strb r3,[r2]; add r2,#1
722
top_n2e:
723
        add r4,r4; mov lr,pc; beq get1_n2e; bcs lit_n2e
724
        mov r1,#1; b getoff_n2e
725
 
726
off_n2e:
727
        sub r1,#1
728
        add r4,r4; mov lr,pc; beq get1_n2e; adc r1,r1
729
getoff_n2e:
730
        add r4,r4; mov lr,pc; beq get1_n2e; adc r1,r1
731
        add r4,r4; mov lr,pc; beq get1_n2e; bcc off_n2e
732
 
733
        sub r3,r1,#3 @ set Carry
734
        mov r1,#0 @ Carry unaffected
735
        blo offprev_n2e @ r1 was 2; tests Carry only
736
        lsl r3,#8
737
        ldrb r5,[r0]; add r0,#1 @ low 7+1 r4
738
        orr r5,r3
739
        mvn r5,r5; beq nrv2e_done @ r5= ~r5
740
        asr r5,#1; bcs lenlast_n2e
741
        b lenmore_n2e
742
 
743
offprev_n2e:
744
        add r4,r4; mov lr,pc; beq get1_n2e; bcs lenlast_n2e
745
lenmore_n2e:
746
        mov r1,#1
747
        add r4,r4; mov lr,pc; beq get1_n2e; bcs lenlast_n2e
748
len_n2e:
749
        add r4,r4; mov lr,pc; beq get1_n2e; adc r1,r1
750
        add r4,r4; mov lr,pc; beq get1_n2e; bcc len_n2e
751
        add r1,#6-2
752
        b gotlen_n2e
753
 
754
lenlast_n2e:
755
        add r4,r4; mov lr,pc; beq get1_n2e; adc r1,r1 @ 0,1,2,3
756
        add r1,#2
757
gotlen_n2e: @ 'cmn': add the inputs, set condition codes, discard the sum
758
        cmn r6,r5; bcs near_n2e @ within M2_MAX_OFFSET
759
        add r1,#1 @ too far away, so minimum match length is 3
760
near_n2e:
761
        ldrb r3,[r2] @ force cacheline allocate
762
copy_n2e:
763
        ldrb r3,[r2,r5]
764
        strb r3,[r2]; add r2,#1
765
        sub r1,#1; bne copy_n2e
766
        b top_n2e
767
 
768
	.code 32
769
	.align 2
770
 
771
foundfile:
772
	ldr	r12, [r11,#0xc]
773
	ldr	r11, [r11,#0x8]
774
	tst	r12, #0x80000000
775
	bic	r12, r12, #0xff000000
776
	bic	r12, r12, #0xf00000
777
	bne	compressed
778
    	mov	r1, #0x08000000
779
	bl	bootflash_read
780
	mov	lr, #0x08000000
781
	b	flushcache
782
compressed:
783
    	mov	r1, #0x09000000
784
	bl	bootflash_read
785
	mov	r0, #0x09000000
786
	mov	r2, #0x08000000
787
	blx	thumb_nrv2e_d8
788
 
789
 
790
rendertext:
791
	ldrb	r3, [r0], #1
792
	cmp	r3, #0
793
	moveq	pc, lr
794
	adr	r6, font
795
	sub	r3, r3, #0x20
796
	cmp	r3, #0x5f
797
	addcc	r6, r3,lsl#2
798
	addcc	r6, r3
799
	mov	r3, #5
800
rendertext_col:
801
	mov	r7, r1
802
	ldrb	r9, [r6], #1
803
rendertext_row:
804
	tst	r9, #1
805
	strneh	r2, [r7]
806
	add	r7, r7, #640
807
	movs	r9, r9,lsr#1
808
	bne	rendertext_row
809
	add	r1, r1, #2
810
	subs	r3, r3, #1
811
	bne	rendertext_col
812
	add	r1, r1, #2
813
	b	rendertext
814
 
815
spisend:
816
	mov	r6, #1
817
	str	r6, [r7,#0x34]
818
spisend_wait:
819
	ldr	r6, [r7,#0x8]
820
	and	r6, r6, #0x1f0
821
	cmp	r6, #0x100
822
	beq	spisend_wait
823
	str	r0, [r7,#0x10]
824
spisend_wait2:
825
	ldr	r6, [r7,#0x8]
826
	ands	r6, r6, #0x3e00
827
	beq	spisend_wait2
828
	ldr	r0, [r7,#0x20]
829
	mov	pc, lr
830
 
831
spirecv:
832
	str	r0, [r7,#0x34]
833
	ldr	r6, [r7,#0x4]
834
	orr	r6, r6, #1
835
	str	r6, [r7,#0x4]
836
spirecv_loop:
837
	ldr	r6, [r7,#0x8]
838
	ands	r6, r6, #0x3e00
839
	beq	spirecv_loop
840
	ldr	r6, [r7,#0x20]
841
	strb	r6, [r1], #1
842
	subs	r0, r0, #1
843
	bne	spirecv_loop
844
	ldr	r6, [r7,#0x4]
845
	bic	r6, r6, #1
846
	str	r6, [r7,#0x4]
847
	mov	pc, lr
848
 
849
bootflash_read:
850
	mov	sp, lr
851
bootflash_wait:
852
	ldr	r0, [r3,#0x4]
853
	bic	r0, r0, #1
854
	str	r0, [r3,#0x4]
855
	mov	r0, #5
856
	bl	spisend
857
	mov	r0, #0xff
858
	bl	spisend
859
	tst	r0, #1
860
	ldr	r0, [r3,#0x4]
861
	orr	r0, r0, #1
862
	str	r0, [r3,#0x4]
863
	bne	bootflash_wait
864
	ldr	r0, [r3,#0x4]
865
	bic	r0, r0, #1
866
	str	r0, [r3,#0x4]
867
	mov	r0, #3
868
	bl	spisend
869
	mov	r0, r11,lsr#16
870
	bl	spisend
871
	mov	r0, r11,lsr#8
872
	and	r0, r0, #0xff
873
	bl	spisend
874
	and	r0, r11, #0xff
875
	bl	spisend
876
	mov	r0, r12
877
	bl	spirecv
878
	ldr	r0, [r3,#0x4]
879
	orr	r0, r0, #1
880
	str	r0, [r3,#0x4]
881
	mov	pc, sp
882
 
883
lcdbyteseq:
884
	mov	r9, r0,lsr#8
885
	and	r0, r0, #0xff
886
	bl	sendlcdc
887
lcdbyteseqloop:
888
	ldrb	r0, [sp], #1
889
	bl	sendlcdd
890
	subs	r9, r9, #1
891
	bne	lcdbyteseqloop
892
	b	lcdloop
893
 
894
sendlcdc:
895
	ldr	r6, [r4,#0x1c]
896
	ands	r6, r6, #0x10
897
	bne	sendlcdc
898
	str	r0, [r4,#0x04]
899
	mov	pc, lr
900
 
901
sendlcdd:
902
	ldr	r6, [r4,#0x1c]
903
	ands	r6, r6, #0x10
904
	bne	sendlcdd
905
	str	r0, [r4,#0x40]
906
	mov	pc, lr
907
 
908
i2cwaitrdy:
909
	ldr	r6, [r5,#0x10]
910
	cmp	r6, #0
911
	bne	i2cwaitrdy
912
	mov	pc, lr
913
 
914
i2cwait:
915
	ldr	r6, [r5]
916
	tst	r6, #0x10
917
	beq	i2cwait
918
	mov	pc, lr
919
 
920
pmutransfer:
921
	mov	r7, lr
922
	mov	lr, #0xb7
923
	str	lr, [r5]
924
	mov	lr, #0xe6
925
	str	lr, [r5,#0xc]
926
	mov	lr, #0xf0
927
	str	lr, [r5,#0x4]
928
	bl	i2cwait
929
	str	r3, [r5,#0xc]
930
	mov	lr, #0xb7
931
	str	lr, [r5]
932
	bl	i2cwait
933
	cmn	r4, #1
934
	beq	pmuskip
935
	str	r4, [r5,#0xc]
936
	mov	lr, #0xb7
937
	str	lr, [r5]
938
	bl	i2cwait
939
	mov	lr, #0xd0
940
	b	pmudone
941
pmuskip:
942
	mov	lr, #0xe7
943
	str	lr, [r5,#0xc]
944
	mov	lr, #0xb0
945
	str	lr, [r5,#0x4]
946
	mov	lr, #0xb7
947
	str	lr, [r5]
948
	bl	i2cwait
949
	mov	lr, #0x37
950
	str	lr, [r5]
951
	bl	i2cwait
952
	ldr	r3, [r5,#0xc]
953
	mov	lr, #0x90
954
pmudone:
955
	str	lr, [r5,#0x4]
956
	mov	lr, #0xb7
957
	str	lr, [r5]
958
pmuwait:
959
	ldr	lr, [r5,#0x4]
960
	tst	lr, #0x20
961
	bne	pmuwait
962
	mov	pc, r7
963
 
964
udelay:
965
	ldr	r6, [r1,#0xb4]
966
	add	r0, r0, r6
967
udelayloop:
968
	ldr	r6, [r1,#0xb4]
969
	cmp	r6, r0
970
	bmi	udelayloop
971
	mov	pc, lr
972
 
973
 
974
pwrok:
975
	sub	r7, r5, #0x04500000  @ R7 = MIUBASE (temporary)
976
	cmp	r0, #0
977
	moveq	r6, #0x11
978
	streq	r6, [r7]
979
	str	r8, [r7]
980
	str	r2, [r7,#0xf0]
981
	str	r9, [r7,#0x8]
982
	str	r10, [r7,#0x10]
983
	mov	r8, #0x1800
984
	orr	r6, r8, #0x45
985
	str	r6, [r7,#0x200]
986
	str	r6, [r7,#0x204]
987
	str	r8, [r7,#0x210]
988
	str	r8, [r7,#0x214]
989
	str	r6, [r7,#0x220]
990
	str	r6, [r7,#0x224]
991
	add	r6, r6, #0x40
992
	str	r6, [r7,#0x230]
993
	str	r6, [r7,#0x234]
994
	mov	r6, #0x19
995
	str	r6, [r7,#0x14]
996
	str	r6, [r7,#0x18]
997
	str	r11, [r7,#0x1c]
998
	ldr	r6, [r7,#0x314]
999
	bic	r6, r6, #0x10
1000
	str	r6, [r7,#0x314]
1001
	add	r8, r7, #0x2c
1002
	mov	r9, #0x24
1003
sdramloop:
1004
	ldr	r6, [r8]
1005
	bic	r6, r6, #0x01000000
1006
	str	r6, [r8], #4
1007
	subs	r9, r9, #1
1008
	bne	sdramloop
1009
	mov	r6, #0x540
1010
	str	r6, [r7,#0x1cc]
1011
	ldr	r6, [r7,#0x1d4]
1012
	orr	r6, r6, #0x80
1013
	str	r6, [r7,#0x1d4]
1014
	mov	r6, #0x33
1015
	str	r6, [r7,#0x4]
1016
	str	r6, [r7,#0x4]
1017
	orr	r8, r6, #0x200
1018
	str	r8, [r7,#0x4]
1019
	str	r6, [r7,#0x4]
1020
	str	r6, [r7,#0x4]
1021
	str	r6, [r7,#0x4]
1022
	orr	r8, r8, #0x100
1023
	str	r8, [r7,#0x4]
1024
	str	r6, [r7,#0x4]
1025
	str	r6, [r7,#0x4]
1026
	str	r6, [r7,#0x4]
1027
	str	r8, [r7,#0x4]
1028
	str	r6, [r7,#0x4]
1029
	str	r6, [r7,#0x4]
1030
	str	r6, [r7,#0x4]
1031
	cmp	r0, #0
1032
	strne	r6, [r7,#0xc]
1033
	bicne	r8, r8, #0x200
1034
	strne	r8, [r7,#0x4]
1035
	strne	r6, [r7,#0x4]
1036
	strne	r6, [r7,#0x4]
1037
	strne	r6, [r7,#0x4]
1038
	strne	r12, [r7,#0xc]
1039
	strne	r8, [r7,#0x4]
1040
	strne	r6, [r7,#0x4]
1041
	strne	r6, [r7,#0x4]
1042
	strne	r6, [r7,#0x4]
1043
	ldr	r6, [r7,#0x8]
1044
	orr	r6, r6, #0x61000
1045
	str	r6, [r7,#0x8]
1046
	orr	r3, r1, #0x00800000  @ R3 = GPIOBASE
1047
	sub	r4, r1, #0x04400000  @ R4 = LCDBASE
1048
	ldr	r0, [r3,#0xc4]
1049
	and	r8, r0, #0x30        @ R8 = LCD type GPIO bits << 4
1050
	add	sp, sp, r8,lsr#3
1051
	ldrh	r0, [sp]
1052
	add	sp, sp, r0
1053
	ldr	r0, val_80100db0
1054
	str	r0, [r4]
1055
	mov	r0, #0x33
1056
	str	r0, [r4,#0x20]
1057
lcdloop:
1058
	adr	lr, lcdloop
1059
	ldrb	r6, [sp], #1
1060
	ldrb	r7, [sp], #1
1061
	orr	r6, r6, r7,lsl#8
1062
	mov	r7, r6,lsr#14
1063
	bics	r0, r6, #0xc000
1064
	add	pc, pc, r7,lsl#2
1065
val_80100db0:
1066
	.word 0x80100db0
1067
	b	sendlcdd
1068
	b	sendlcdc
1069
	bne	lcdbyteseq
1070
	movne	r0, r0,lsl#4
1071
	bne	udelay
1072
	mov	r10, #0x22000000
1073
	orr	r10, r10, #0x10000
1074
	add	r11, r10, #0x25800
1075
	mov	r0, #-1
1076
fillbuff:
1077
	str	r0, [r11,#-4]!
1078
	cmp	r10, r11
1079
	bne	fillbuff
1080
	mov	r1, r11
1081
	adr	r0, text
1082
	bl	rendertext
1083
	add	r1, r11, #0x2800
1084
printerror:
1085
	bl	rendertext
1086
	bl	flushcache
1087
	cmp	r8, #0x30
1088
	bne	leadisprotocol
1089
	mov	r9, #0x200
1090
	add	r0, r9, #0x10
1091
	bl	sendlcdc
1092
	mov	r0, #0
1093
	bl	sendlcdd
1094
	add	r0, r9, #0x11
1095
	bl	sendlcdc
1096
	sub	r0, r9, #0xc1
1097
	bl	sendlcdd
1098
	add	r0, r9, #0x12
1099
	bl	sendlcdc
1100
	mov	r0, #0
1101
	bl	sendlcdd
1102
	add	r0, r9, #0x13
1103
	bl	sendlcdc
1104
	mov	r0, #0xef
1105
	bl	sendlcdd
1106
	mov	r0, r9
1107
	bl	sendlcdc
1108
	mov	r0, #0
1109
	bl	sendlcdd
1110
	add	r0, r9, #1
1111
	bl	sendlcdc
1112
	mov	r0, #0
1113
	bl	sendlcdd
1114
	add	r0, r9, #2
1115
	bl	sendlcdc
1116
	b	blit
1117
leadisprotocol:
1118
	mov	r0, #0x2a
1119
	bl	sendlcdc
1120
	mov	r0, #0
1121
	bl	sendlcdd
1122
	mov	r0, #0
1123
	bl	sendlcdd
1124
	mov	r0, #1
1125
	bl	sendlcdd
1126
	mov	r0, #0x3f
1127
	bl	sendlcdd
1128
	mov	r0, #0x2b
1129
	bl	sendlcdc
1130
	mov	r0, #0
1131
	bl	sendlcdd
1132
	mov	r0, #0
1133
	bl	sendlcdd
1134
	mov	r0, #0
1135
	bl	sendlcdd
1136
	mov	r0, #0xef
1137
	bl	sendlcdd
1138
	mov	r0, #0x2c
1139
	bl	sendlcdc
1140
blit:
1141
	sub	r7, r4, #0x00100000  @ R7 = DMA0BASE (temporary)
1142
	mov	r0, #1
1143
	str	r0, [r7,#0x30]
1144
	mov	r0, #0x22000000
1145
	orr	r0, r0, #0x3d000
1146
	add	r1, r7, #0x100
1147
	mov	r13, #0x8800
1148
	orr	r13, r13, #0xc1
1149
	ldmia	r0, {r9-r12}
1150
	stmia	r1, {r9-r13}
1151
	tst	r5, #0xff
1152
failed:
1153
	bne	failed
1154
	mov	r3, #0x2b
1155
	mov	r4, #4
1156
	bl	pmutransfer
1157
	mov	r3, #0x28
1158
	mov	r4, #14
1159
	bl	pmutransfer
1160
	mov	r3, #0x29
1161
	mov	r4, #1
1162
	bl	pmutransfer
1163
	sub	r7, r5, #0x00300000  @ R7 = SPIBASE
1164
	orr	r3, r5, #0x00900000  @ R3 = GPIOBASE
1165
	mov	r0, #0xf
1166
	str	r0, [r7,#0x8]
1167
	ldr	r0, [r7]
1168
	orr	r0, r0, #0xc
1169
	str	r0, [r7]
1170
	mov	r0, #4
1171
	str	r0, [r7,#0x30]
1172
	mov	r0, #6
1173
	str	r0, [r7,#0xc]
1174
	ldr	r0, val_00010618
1175
	str	r0, [r7,#0x4]
1176
	ldr	r0, [r7]
1177
	orr	r0, r0, #0xc
1178
	str	r0, [r7]
1179
	mov	r0, #1
1180
	str	r0, [r7]
1181
    	sub	r1, r9, #0x1000
1182
	mov	r11, #0
1183
	mov	r12, #0x1000
1184
	bl	bootflash_read
1185
    	sub	r11, r9, #0x1000
1186
	ldrd	r4, bootfilename
1187
searchfile:
1188
	ldrd	r12, [r11]
1189
	cmp	r4, r12
1190
	cmpeq	r5, r13
1191
	beq	foundfile
1192
	add	r11, r11, #0x10
1193
	tst	r11, #0x10000
1194
	beq	searchfile
1195
	adr	r0, errormessage
1196
	sub	r4, r3, #0x04c00000  @ R4 = LCDBASE
1197
	mov	r1, #0x22000000
1198
	orr	r1, r1, #0x13c00
1199
	b	printerror
1200
 
1201
val_00010618:
1202
	.word 0x00010618
1203
 
1204
bootfilename:
1205
	.ascii "embios  "
1206
 
1207
text:
1208
	.ascii "emBIOS Loader v"
1209
	.ascii VERSION
1210
	.ascii " r"
1211
	.ascii VERSION_SVN
1212
	.ascii "\0"
1213
	.ascii "Loading emBIOS...\0"
1214
 
1215
errormessage:
1216
	.ascii "File not found!\0"