summaryrefslogtreecommitdiff
path: root/arch/arm/mach-imx/ddr3_freq_imx7d.S
blob: ff767c08af44428eafe9fc1a195e89c17c7a91a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
 * Copyright (C) 2015 Freescale Semiconductor, Inc. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/linkage.h>
#include "hardware.h"

#define DDRC_MSTR		0x0
#define DDRC_STAT		0x4
#define DDRC_MRCTRL0		0x10
#define DDRC_MRCTRL1		0x14
#define DDRC_MRSTAT		0x18
#define DDRC_PWRCTL		0x30
#define DDRC_RFSHCTL3		0x60
#define DDRC_DBG1		0x304
#define DDRC_SWCTL		0x320
#define DDRC_SWSTAT		0x324
#define DDRC_PSTAT		0x3fc
#define DDRC_PCTRL_0		0x490
#define DDRC_ZQCTL0		0x180
#define DDRC_DFIMISC		0x1b0
#define DDRC_DBGCAM		0x308
#define DDRPHY_LP_CON0		0x18
#define IOMUXC_GPR8		0x20
#define DDRPHY_MDLL_CON0	0xb0
#define DDRPHY_MDLL_CON1	0xb4
#define DDRPHY_OFFSETD_CON0	0x50
#define DDRPHY_OFFSETR_CON0	0x20
#define DDRPHY_OFFSETR_CON1	0x24
#define DDRPHY_OFFSETR_CON2	0x28
#define DDRPHY_OFFSETW_CON0	0x30
#define DDRPHY_OFFSETW_CON1	0x34
#define DDRPHY_OFFSETW_CON2	0x38
#define DDRPHY_CA_WLDSKEW_CON0	0x6c
#define DDRPHY_CA_DSKEW_CON0	0x7c
#define DDRPHY_CA_DSKEW_CON1	0x80
#define DDRPHY_CA_DSKEW_CON2	0x84

#define ANADIG_DIGPROG		0x800

	.align 3

	.macro	switch_to_below_100m

	ldr	r7, =0x2
	str	r7, [r4, #DDRC_DBG1]

	ldr	r6, =0x36000000
1:
	ldr	r7, [r4, #DDRC_DBGCAM]
	and	r7, r7, r6
	cmp	r7, r6
	bne	1b

	ldr	r6, =0x1
2:
	ldr	r7, [r4, #DDRC_MRSTAT]
	and	r7, r7, r6
	cmp	r7, r6
	beq	2b

	ldr	r7, =0x10f0
	str	r7, [r4, #DDRC_MRCTRL0]
	ldr	r7, =0x0
	str	r7, [r4, #DDRC_MRCTRL1]
	ldr	r7, =0x800010f0
	str	r7, [r4, #DDRC_MRCTRL0]

	ldr	r6, =0x1
3:
	ldr	r7, [r4, #DDRC_MRSTAT]
	and	r7, r7, r6
	cmp	r7, r6
	beq	3b

	ldr	r7, =0x20f0
	str	r7, [r4, #DDRC_MRCTRL0]
	ldr	r7, =0x8
	str	r7, [r4, #DDRC_MRCTRL1]
	ldr	r7, =0x800020f0
	str	r7, [r4, #DDRC_MRCTRL0]

	ldr	r6, =0x1
4:
	ldr	r7, [r4, #DDRC_MRSTAT]
	and	r7, r7, r6
	cmp	r7, r6
	beq	4b

	ldr	r7, =0x10f0
	str	r7, [r4, #DDRC_MRCTRL0]
	ldr	r7, =0x1
	str	r7, [r4, #DDRC_MRCTRL1]
	ldr	r7, =0x800010f0
	str	r7, [r4, #DDRC_MRCTRL0]

	ldr	r7, =0x20
	str	r7, [r4, #DDRC_PWRCTL]

	ldr	r6, =0x23
5:
	ldr	r7, [r4, #DDRC_STAT]
	and	r7, r7, r6
	cmp	r7, r6
	bne	5b

	ldr	r7, =0x0
	str	r7, [r4, #DDRC_SWCTL]

	ldr	r7, =0x03048001
	str	r7, [r4, #DDRC_MSTR]

	ldr	r7, =0x1
	str	r7, [r4, #DDRC_SWCTL]

	ldr	r6, =0x1
6:
	ldr	r7, [r4, #DDRC_SWSTAT]
	and	r7, r7, r6
	cmp	r7, r6
	bne	6b

	ldr	r7, =0x10010100
	str	r7, [r5, #0x4]

	ldr	r6, =24000000
	cmp	r0, r6
	bne	7f

	/* dram alt sel set to OSC */
	ldr	r7, =0x10000000
	ldr	r8, =0xa080
	str	r7, [r2, r8]
	/* dram root set to from dram alt, div by 1 */
	ldr	r7, =0x11000000
	ldr	r8, =0x9880
	str	r7, [r2, r8]
	b	8f
7:
	/* dram alt sel set to pfd0_392m */
	ldr	r7, =0x15000000
	ldr	r8, =0xa080
	str	r7, [r2, r8]
	/* dram root set to from dram alt, div by 4 */
	ldr	r7, =0x11000003
	ldr	r8, =0x9880
	str	r7, [r2, r8]
8:
	ldr	r7, =0x202ffd0
	str	r7, [r5, #DDRPHY_MDLL_CON0]

	ldr	r7, =0x1000007f
	str	r7, [r5, #DDRPHY_OFFSETD_CON0]

	ldr	r7, =0x7f7f7f7f
	str	r7, [r5, #DDRPHY_OFFSETR_CON0]
	str	r7, [r5, #DDRPHY_OFFSETR_CON1]
	ldr	r7, =0x7f
	str	r7, [r5, #DDRPHY_OFFSETR_CON2]

	ldr	r7, =0x7f7f7f7f
	str	r7, [r5, #DDRPHY_OFFSETW_CON0]
	str	r7, [r5, #DDRPHY_OFFSETW_CON1]
	ldr	r7, =0x7f
	str	r7, [r5, #DDRPHY_OFFSETW_CON2]

	ldr	r7, [r9, #ANADIG_DIGPROG]
	and	r7, r7, #0x11
	cmp	r7, #0x11
	bne	20f

	ldr	r7, =0x0
	str	r7, [r5, #DDRPHY_CA_WLDSKEW_CON0]
	ldr	r7, =0x60606060
	str	r7, [r5, #DDRPHY_CA_DSKEW_CON0]
	str	r7, [r5, #DDRPHY_CA_DSKEW_CON1]
	ldr	r7, =0x00006060
	str	r7, [r5, #DDRPHY_CA_DSKEW_CON2]
	b	21f
20:
	ldr	r7, =0x0
	str	r7, [r5, #DDRPHY_CA_DSKEW_CON0]
	str	r7, [r5, #DDRPHY_CA_DSKEW_CON1]
	str	r7, [r5, #DDRPHY_CA_DSKEW_CON2]
21:
	ldr	r7, =0x1100007f
	str	r7, [r5, #DDRPHY_OFFSETD_CON0]
	ldr	r7, =0x1000007f
	str	r7, [r5, #DDRPHY_OFFSETD_CON0]

	ldr	r7, =0x0
	str	r7, [r4, #DDRC_PWRCTL]

	ldr	r6, =0x1
9:
	ldr	r7, [r4, #DDRC_MRSTAT]
	and	r7, r7, r6
	cmp	r7, r6
	beq	9b

	ldr	r7, =0xf0
	str	r7, [r4, #DDRC_MRCTRL0]
	ldr	r7, =0x820
	str	r7, [r4, #DDRC_MRCTRL1]
	ldr	r7, =0x800000f0
	str	r7, [r4, #DDRC_MRCTRL0]

	ldr	r6, =0x1
10:
	ldr	r7, [r4, #DDRC_MRSTAT]
	and	r7, r7, r6
	cmp	r7, r6
	beq	10b

	ldr	r7, =0x800020
	str	r7, [r4, #DDRC_ZQCTL0]

	ldr	r7, =0x0
	str	r7, [r4, #DDRC_DBG1]

	/* enable auto self-refresh */
	ldr	r7, [r4, #DDRC_PWRCTL]
	orr	r7, r7, #(1 << 0)
	str	r7, [r4, #DDRC_PWRCTL]

	.endm

	.macro	switch_to_533m

	ldr	r7, =0x2
	str	r7, [r4, #DDRC_DBG1]

	ldr	r7, =0x78
	str	r7, [r3, #IOMUXC_GPR8]
	orr	r7, r7, #0x100
	str	r7, [r3, #IOMUXC_GPR8]

	ldr	r6, =0x30000000
11:
	ldr	r7, [r4, #DDRC_DBGCAM]
	and	r7, r7, r6
	cmp	r7, r6
	bne	11b

	ldr	r6, =0x1
12:
	ldr	r7, [r4, #DDRC_MRSTAT]
	and	r7, r7, r6
	cmp	r7, r6
	beq	12b

	ldr	r7, =0x10f0
	str	r7, [r4, #DDRC_MRCTRL0]
	ldr	r7, =0x1
	str	r7, [r4, #DDRC_MRCTRL1]
	ldr	r7, =0x800010f0
	str	r7, [r4, #DDRC_MRCTRL0]

	ldr	r7, =0x20
	str	r7, [r4, #DDRC_PWRCTL]

	ldr	r6, =0x23
13:
	ldr	r7, [r4, #DDRC_STAT]
	and	r7, r7, r6
	cmp	r7, r6
	bne	13b

	ldr	r7, =0x03040001
	str	r7, [r4, #DDRC_MSTR]

	ldr	r7, =0x40800020
	str	r7, [r4, #DDRC_ZQCTL0]


	ldr	r7, =0x10210100
	str	r7, [r5, #0x4]

	/* dram root set to from dram main, div by 2 */
	ldr	r7, =0x10000001
	ldr	r8, =0x9880
	str	r7, [r2, r8]

	ldr	r7, =0x1010007e
	str	r7, [r5, #DDRPHY_MDLL_CON0]

	ldr	r7, =0x10000008
	str	r7, [r5, #DDRPHY_OFFSETD_CON0]

	ldr	r7, =0x08080808
	str	r7, [r5, #DDRPHY_OFFSETR_CON0]
	str	r7, [r5, #DDRPHY_OFFSETR_CON1]
	ldr	r7, =0x8
	str	r7, [r5, #DDRPHY_OFFSETR_CON2]

	ldr	r7, =0x08080808
	str	r7, [r5, #DDRPHY_OFFSETW_CON0]
	str	r7, [r5, #DDRPHY_OFFSETW_CON1]
	ldr	r7, =0x8
	str	r7, [r5, #DDRPHY_OFFSETW_CON2]

	ldr	r7, [r9, #ANADIG_DIGPROG]
	and	r7, r7, #0x11
	cmp	r7, #0x11
	bne	22f

	ldr	r7, =0x40404040
	str	r7, [r5, #DDRPHY_CA_WLDSKEW_CON0]
	ldr	r7, =0x18181818
	str	r7, [r5, #DDRPHY_CA_DSKEW_CON0]
	str	r7, [r5, #DDRPHY_CA_DSKEW_CON1]
	ldr	r7, =0x40401818
	str	r7, [r5, #DDRPHY_CA_DSKEW_CON2]
	b	23f
22:
	ldr	r7, =0x0
	str	r7, [r5, #DDRPHY_CA_DSKEW_CON0]
	str	r7, [r5, #DDRPHY_CA_DSKEW_CON1]
	str	r7, [r5, #DDRPHY_CA_DSKEW_CON2]
23:
	ldr	r7, =0x11000008
	str	r7, [r5, #DDRPHY_OFFSETD_CON0]
	ldr	r7, =0x10000008
	str	r7, [r5, #DDRPHY_OFFSETD_CON0]

	ldr	r6, =0x4
14:
	ldr	r7, [r5, #DDRPHY_MDLL_CON1]
	and	r7, r7, r6
	cmp	r7, r6
	bne	14b

	ldr	r7, =0x1
	str	r7, [r4, #DDRC_RFSHCTL3]
	ldr	r7, =0x3
	str	r7, [r4, #DDRC_RFSHCTL3]

	ldr	r7, =0x0
	str	r7, [r4, #DDRC_PWRCTL]

	ldr	r6, =0x1
15:
	ldr	r7, [r4, #DDRC_MRSTAT]
	and	r7, r7, r6
	cmp	r7, r6
	beq	15b

	ldr	r7, =0x10f0
	str	r7, [r4, #DDRC_MRCTRL0]
	ldr	r7, =0x0
	str	r7, [r4, #DDRC_MRCTRL1]
	ldr	r7, =0x800010f0
	str	r7, [r4, #DDRC_MRCTRL0]

	ldr	r6, =0x1
16:
	ldr	r7, [r4, #DDRC_MRSTAT]
	and	r7, r7, r6
	cmp	r7, r6
	beq	16b

	ldr	r7, =0xf0
	str	r7, [r4, #DDRC_MRCTRL0]
	ldr	r7, =0x930
	str	r7, [r4, #DDRC_MRCTRL1]
	ldr	r7, =0x800000f0
	str	r7, [r4, #DDRC_MRCTRL0]

	ldr	r7, =0x0
	str	r7, [r4, #DDRC_RFSHCTL3]
	ldr	r7, =0x2
	str	r7, [r4, #DDRC_RFSHCTL3]

	ldr	r6, =0x1
17:
	ldr	r7, [r4, #DDRC_MRSTAT]
	and	r7, r7, r6
	cmp	r7, r6
	beq	17b

	ldr	r7, =0xf0
	str	r7, [r4, #DDRC_MRCTRL0]
	ldr	r7, =0x930
	str	r7, [r4, #DDRC_MRCTRL1]
	ldr	r7, =0x800000f0
	str	r7, [r4, #DDRC_MRCTRL0]

	ldr	r6, =0x1
18:
	ldr	r7, [r4, #DDRC_MRSTAT]
	and	r7, r7, r6
	cmp	r7, r6
	beq	18b

	ldr	r7, =0x20f0
	str	r7, [r4, #DDRC_MRCTRL0]
	ldr	r7, =0x408
	str	r7, [r4, #DDRC_MRCTRL1]
	ldr	r7, =0x800020f0
	str	r7, [r4, #DDRC_MRCTRL0]

	ldr	r6, =0x1
19:
	ldr	r7, [r4, #DDRC_MRSTAT]
	and	r7, r7, r6
	cmp	r7, r6
	beq	19b

	ldr	r7, =0x10f0
	str	r7, [r4, #DDRC_MRCTRL0]
	ldr	r7, =0x4
	str	r7, [r4, #DDRC_MRCTRL1]
	ldr	r7, =0x800010f0
	str	r7, [r4, #DDRC_MRCTRL0]

	ldr	r7, =0x0
	str	r7, [r4, #DDRC_DBG1]

	/* enable auto self-refresh */
	ldr	r7, [r4, #DDRC_PWRCTL]
	orr	r7, r7, #(1 << 0)
	str	r7, [r4, #DDRC_PWRCTL]

	.endm

ENTRY(imx7d_ddr3_freq_change)
	push	{r2 - r9}

	/*
	 * To ensure no page table walks occur in DDR, we
	 * have a another page table stored in IRAM that only
	 * contains entries pointing to IRAM, AIPS1 and AIPS2.
	 * We need to set the TTBR1 to the new IRAM TLB.
	 * Do the following steps:
	 * 1. Flush the Branch Target Address Cache (BTAC)
	 * 2. Set TTBR1 to point to IRAM page table.
	 * 3. Disable page table walks in TTBR0 (PD0 = 1)
	 * 4. Set TTBR0.N=1, implying 0-2G is translated by TTBR0
	 *     and 2-4G is translated by TTBR1.
	 */
	ldr	r6, =0x0
	mcr	p15, 0, r6, c8, c3, 0

	ldr	r6, =iram_tlb_phys_addr
	ldr	r7, [r6]

	/* Disable Branch Prediction, Z bit in SCTLR. */
	mrc	p15, 0, r6, c1, c0, 0
	bic	r6, r6, #0x800
	mcr	p15, 0, r6, c1, c0, 0

	/* Flush the Branch Target Address Cache (BTAC) */
	ldr	r6, =0x0
	mcr	p15, 0, r6, c7, c1, 6

	dsb
	isb
	/* Store the IRAM table in TTBR1 */
	mcr	p15, 0, r7, c2, c0, 1

	/* Read TTBCR and set PD0=1, N = 1 */
	mrc	p15, 0, r6, c2, c0, 2
	orr	r6, r6, #0x11
	mcr	p15, 0, r6, c2, c0, 2

	dsb
	isb

	/* flush the TLB */
	ldr	r6, =0x0
	mcr	p15, 0, r6, c8, c3, 0

	dsb
	isb

	ldr	r2, =IMX_IO_P2V(MX7D_CCM_BASE_ADDR)
	ldr	r3, =IMX_IO_P2V(MX7D_IOMUXC_GPR_BASE_ADDR)
	ldr	r4, =IMX_IO_P2V(MX7D_DDRC_BASE_ADDR)
	ldr	r5, =IMX_IO_P2V(MX7D_DDRC_PHY_BASE_ADDR)
	ldr	r9, =IMX_IO_P2V(MX7D_ANATOP_BASE_ADDR)

	ldr	r6, =100000000
	cmp	r0, r6
	bgt	set_to_533m

set_to_below_100m:
	switch_to_below_100m
	b	done

set_to_533m:
	switch_to_533m
	b	done

done:
	/* Enable L1 data cache. */
	mrc	p15, 0, r6, c1, c0, 0
	orr	r6, r6, #0x4
	mcr	p15, 0, r6, c1, c0, 0

	/* Restore the TTBCR */
	dsb
	isb

	/* Read TTBCR and set PD0=0, N = 0 */
	mrc	p15, 0, r6, c2, c0, 2
	bic	r6, r6, #0x11
	mcr	p15, 0, r6, c2, c0, 2
	dsb
	isb

	/* flush the TLB */
	ldr	r6, =0x0
	mcr	p15, 0, r6, c8, c3, 0

	dsb
	isb

	/* Enable Branch Prediction, Z bit in SCTLR. */
	mrc	p15, 0, r6, c1, c0, 0
	orr	r6, r6, #0x800
	mcr	p15, 0, r6, c1, c0, 0

	/* Flush the Branch Target Address Cache (BTAC) */
	ldr	r6, =0x0
	mcr	p15, 0, r6, c7, c1, 6

	dsb
	isb

	nop
	nop
	nop
	nop
	nop

	nop
	nop
	nop
	nop
	nop

	nop
	nop
	nop
	nop
	nop

	nop
	nop
	nop
	nop
	nop

	nop
	nop
	nop
	nop
	nop

	/* Restore registers */
	pop	{r2 - r9}
	mov	pc, lr
	.ltorg
ENDPROC(imx7d_ddr3_freq_change)