Debian Codegraph @

src/drivers/aspeed/common/ast_post.c

raw symbols SCIP
1 /* SPDX-License-Identifier: MIT */
2
3 #define COREBOOT_AST_FAILOVER_TIMEOUT 10000000
4
5 #include <console/console.h>
6 #include <delay.h>
7
8 #include "ast_drv.h"
9 #include "ast_dram_tables.h"
10
11 static void ast_post_chip_2300(struct drm_device *dev);
12 static void ast_post_chip_2500(struct drm_device *dev);
13
14 void ast_enable_vga(struct drm_device *dev)
15 {
16 struct ast_private *ast = dev->dev_private;
17
18 ast_io_write8(ast, AST_IO_VGA_ENABLE_PORT, 0x01);
19 ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, 0x01);
20 }
21
22 void ast_enable_mmio(struct drm_device *dev)
23 {
24 struct ast_private *ast = dev->dev_private;
25
26 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa1, 0xff, 0x04);
27 }
28
29 bool ast_is_vga_enabled(struct drm_device *dev)
30 {
31 struct ast_private *ast = dev->dev_private;
32 u8 ch;
33
34 if (ast->chip == AST1180) {
35 /* TODO 1180 */
36 } else {
37 ch = ast_io_read8(ast, AST_IO_VGA_ENABLE_PORT);
38 return !!(ch & 0x01);
39 }
40 return false;
41 }
42
43 static const u8 extreginfo[] = { 0x0f, 0x04, 0x1c, 0xff };
44 static const u8 extreginfo_ast2300a0[] = { 0x0f, 0x04, 0x1c, 0xff };
45 static const u8 extreginfo_ast2300[] = { 0x0f, 0x04, 0x1f, 0xff };
46
47 static void
48 ast_set_def_ext_reg(struct drm_device *dev)
49 {
50 struct ast_private *ast = dev->dev_private;
51 u8 i, index, reg;
52 uint32_t data;
53 const u8 *ext_reg_info;
54
55 pci_read_config_dword(ast->dev->pdev, 0x08, &data);
56 uint8_t revision = data & 0xff;
57
58 /* reset scratch */
59 for (i = 0x81; i <= 0x9f; i++)
60 ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, 0x00);
61
62 if (ast->chip == AST2300 || ast->chip == AST2400 ||
63 ast->chip == AST2500) {
64 if (revision >= 0x20)
65 ext_reg_info = extreginfo_ast2300;
66 else
67 ext_reg_info = extreginfo_ast2300a0;
68 } else
69 ext_reg_info = extreginfo;
70
71 index = 0xa0;
72 while (*ext_reg_info != 0xff) {
73 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, index, 0x00, *ext_reg_info);
74 index++;
75 ext_reg_info++;
76 }
77
78 /* disable standard IO/MEM decode if secondary */
79 /* ast_set_index_reg-mask(ast, AST_IO_CRTC_PORT, 0xa1, 0xff, 0x3); */
80
81 /* Set Ext. Default */
82 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x8c, 0x00, 0x01);
83 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x00, 0x00);
84
85 /* Enable RAMDAC for A1 */
86 reg = 0x04;
87 if (ast->chip == AST2300 || ast->chip == AST2400 ||
88 ast->chip == AST2500)
89 reg |= 0x20;
90 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0xff, reg);
91 }
92
93 u32 ast_mindwm(struct ast_private *ast, u32 r)
94 {
95 uint32_t timeout;
96 uint32_t data;
97
98 ast_write32(ast, 0xf004, r & 0xffff0000);
99 ast_write32(ast, 0xf000, 0x1);
100
101 timeout = 0;
102 do {
103 data = ast_read32(ast, 0xf004) & 0xffff0000;
104 timeout++;
105 } while ((data != (r & 0xffff0000)) && (timeout < COREBOOT_AST_FAILOVER_TIMEOUT));
106 if (timeout >= COREBOOT_AST_FAILOVER_TIMEOUT)
107 dev_err(dev->pdev, "Timeout while waiting for register\n");
108 return ast_read32(ast, 0x10000 + (r & 0x0000ffff));
109 }
110
111 void ast_moutdwm(struct ast_private *ast, u32 r, u32 v)
112 {
113 uint32_t timeout;
114 uint32_t data;
115
116 ast_write32(ast, 0xf004, r & 0xffff0000);
117 ast_write32(ast, 0xf000, 0x1);
118 timeout = 0;
119 do {
120 data = ast_read32(ast, 0xf004) & 0xffff0000;
121 timeout++;
122 } while ((data != (r & 0xffff0000)) && (timeout < COREBOOT_AST_FAILOVER_TIMEOUT));
123 if (timeout >= COREBOOT_AST_FAILOVER_TIMEOUT)
124 dev_err(dev->pdev, "Timeout while waiting for register\n");
125 ast_write32(ast, 0x10000 + (r & 0x0000ffff), v);
126 }
127
128 /*
129 * AST2100/2150 DLL CBR Setting
130 */
131 #define CBR_SIZE_AST2150 ((16 << 10) - 1)
132 #define CBR_PASSNUM_AST2150 5
133 #define CBR_THRESHOLD_AST2150 10
134 #define CBR_THRESHOLD2_AST2150 10
135 #define TIMEOUT_AST2150 5000000
136
137 #define CBR_PATNUM_AST2150 8
138
139 static const u32 pattern_AST2150[14] = {
140 0xFF00FF00,
141 0xCC33CC33,
142 0xAA55AA55,
143 0xFFFE0001,
144 0x683501FE,
145 0x0F1929B0,
146 0x2D0B4346,
147 0x60767F02,
148 0x6FBE36A6,
149 0x3A253035,
150 0x3019686D,
151 0x41C6167E,
152 0x620152BF,
153 0x20F050E0
154 };
155
156 static u32 mmctestburst2_ast2150(struct ast_private *ast, u32 datagen)
157 {
158 u32 data, timeout;
159
160 ast_moutdwm(ast, 0x1e6e0070, 0x00000000);
161 ast_moutdwm(ast, 0x1e6e0070, 0x00000001 | (datagen << 3));
162 timeout = 0;
163 do {
164 data = ast_mindwm(ast, 0x1e6e0070) & 0x40;
165 if (++timeout > TIMEOUT_AST2150) {
166 ast_moutdwm(ast, 0x1e6e0070, 0x00000000);
167 return 0xffffffff;
168 }
169 } while (!data);
170 ast_moutdwm(ast, 0x1e6e0070, 0x00000000);
171 ast_moutdwm(ast, 0x1e6e0070, 0x00000003 | (datagen << 3));
172 timeout = 0;
173 do {
174 data = ast_mindwm(ast, 0x1e6e0070) & 0x40;
175 if (++timeout > TIMEOUT_AST2150) {
176 ast_moutdwm(ast, 0x1e6e0070, 0x00000000);
177 return 0xffffffff;
178 }
179 } while (!data);
180 data = (ast_mindwm(ast, 0x1e6e0070) & 0x80) >> 7;
181 ast_moutdwm(ast, 0x1e6e0070, 0x00000000);
182 return data;
183 }
184
185 static int cbrtest_ast2150(struct ast_private *ast)
186 {
187 int i;
188
189 for (i = 0; i < 8; i++)
190 if (mmctestburst2_ast2150(ast, i))
191 return 0;
192 return 1;
193 }
194
195 static int cbrscan_ast2150(struct ast_private *ast, int busw)
196 {
197 u32 patcnt, loop;
198
199 for (patcnt = 0; patcnt < CBR_PATNUM_AST2150; patcnt++) {
200 ast_moutdwm(ast, 0x1e6e007c, pattern_AST2150[patcnt]);
201 for (loop = 0; loop < CBR_PASSNUM_AST2150; loop++) {
202 if (cbrtest_ast2150(ast))
203 break;
204 }
205 if (loop == CBR_PASSNUM_AST2150)
206 return 0;
207 }
208 return 1;
209 }
210
211 static void cbrdlli_ast2150(struct ast_private *ast, int busw)
212 {
213 u32 dll_min[4], dll_max[4], dlli, data, passcnt;
214
215 cbr_start:
216 dll_min[0] = dll_min[1] = dll_min[2] = dll_min[3] = 0xff;
217 dll_max[0] = dll_max[1] = dll_max[2] = dll_max[3] = 0x0;
218 passcnt = 0;
219
220 for (dlli = 0; dlli < 100; dlli++) {
221 ast_moutdwm(ast, 0x1e6e0068, dlli | (dlli << 8) | (dlli << 16) | (dlli << 24));
222 data = cbrscan_ast2150(ast, busw);
223 if (data != 0) {
224 if (data & 0x1) {
225 if (dll_min[0] > dlli)
226 dll_min[0] = dlli;
227 if (dll_max[0] < dlli)
228 dll_max[0] = dlli;
229 }
230 passcnt++;
231 } else if (passcnt >= CBR_THRESHOLD_AST2150)
232 goto cbr_start;
233 }
234 if (dll_max[0] == 0 || (dll_max[0]-dll_min[0]) < CBR_THRESHOLD_AST2150)
235 goto cbr_start;
236
237 dlli = dll_min[0] + (((dll_max[0] - dll_min[0]) * 7) >> 4);
238 ast_moutdwm(ast, 0x1e6e0068, dlli | (dlli << 8) | (dlli << 16) | (dlli << 24));
239 }
240
241 static void ast_init_dram_reg(struct drm_device *dev)
242 {
243 struct ast_private *ast = dev->dev_private;
244 u8 j;
245 u32 data, temp, i;
246 uint32_t timeout;
247 const struct ast_dramstruct *dram_reg_info;
248
249 j = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
250
251 if ((j & 0x80) == 0) { /* VGA only */
252 if (ast->chip == AST2000) {
253 dram_reg_info = ast2000_dram_table_data;
254 ast_write32(ast, 0xf004, 0x1e6e0000);
255 ast_write32(ast, 0xf000, 0x1);
256 ast_write32(ast, 0x10100, 0xa8);
257
258 timeout = 0;
259 do {
260 timeout++;
261 } while ((ast_read32(ast, 0x10100) != 0xa8) && (timeout < COREBOOT_AST_FAILOVER_TIMEOUT));
262 if (timeout >= COREBOOT_AST_FAILOVER_TIMEOUT)
263 dev_err(dev->pdev, "Timeout while waiting for register\n");
264 } else {/* AST2100/1100 */
265 if (ast->chip == AST2100 || ast->chip == 2200)
266 dram_reg_info = ast2100_dram_table_data;
267 else
268 dram_reg_info = ast1100_dram_table_data;
269
270 ast_write32(ast, 0xf004, 0x1e6e0000);
271 ast_write32(ast, 0xf000, 0x1);
272 ast_write32(ast, 0x12000, 0x1688A8A8);
273
274 /* Wait up to 2.5 seconds for device initialization / register unlock */
275 for (i = 0; i < 250; i++) {
276 if (ast_read32(ast, 0x12000) == 0x01)
277 break;
278 mdelay(10);
279 }
280 if (ast_read32(ast, 0x12000) != 0x01)
281 dev_err(dev->pdev, "Unable to unlock SCU registers\n");
282
283 ast_write32(ast, 0x10000, 0xfc600309);
284
285 /* Wait up to 2.5 seconds for device initialization / register unlock */
286 for (i = 0; i < 250; i++) {
287 if (ast_read32(ast, 0x10000) == 0x01)
288 break;
289 mdelay(10);
290 }
291 if (ast_read32(ast, 0x10000) != 0x01)
292 dev_err(dev->pdev, "Unable to unlock SDRAM control registers\n");
293 }
294
295 while (dram_reg_info->index != 0xffff) {
296 if (dram_reg_info->index == 0xff00) {/* delay fn */
297 for (i = 0; i < 15; i++)
298 udelay(dram_reg_info->data);
299 } else if (dram_reg_info->index == 0x4 && ast->chip != AST2000) {
300 data = dram_reg_info->data;
301 if (ast->dram_type == AST_DRAM_1Gx16)
302 data = 0x00000d89;
303 else if (ast->dram_type == AST_DRAM_1Gx32)
304 data = 0x00000c8d;
305
306 temp = ast_read32(ast, 0x12070);
307 temp &= 0xc;
308 temp <<= 2;
309 ast_write32(ast, 0x10000 + dram_reg_info->index, data | temp);
310 } else
311 ast_write32(ast, 0x10000 + dram_reg_info->index, dram_reg_info->data);
312 dram_reg_info++;
313 }
314
315 /* AST 2100/2150 DRAM calibration */
316 data = ast_read32(ast, 0x10120);
317 if (data == 0x5061) { /* 266Mhz */
318 data = ast_read32(ast, 0x10004);
319 if (data & 0x40)
320 cbrdlli_ast2150(ast, 16); /* 16 bits */
321 else
322 cbrdlli_ast2150(ast, 32); /* 32 bits */
323 }
324
325 switch (ast->chip) {
326 case AST2000:
327 temp = ast_read32(ast, 0x10140);
328 ast_write32(ast, 0x10140, temp | 0x40);
329 break;
330 case AST1100:
331 case AST2100:
332 case AST2200:
333 case AST2150:
334 temp = ast_read32(ast, 0x1200c);
335 ast_write32(ast, 0x1200c, temp & 0xfffffffd);
336 temp = ast_read32(ast, 0x12040);
337 ast_write32(ast, 0x12040, temp | 0x40);
338 break;
339 default:
340 break;
341 }
342 }
343
344 /* wait ready */
345 /* Wait up to 2.5 seconds for device to become ready */
346 for (i = 0; i < 250; i++) {
347 j = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
348 mdelay(10);
349 if ((j & 0x40) != 0)
350 break;
351 }
352 if ((j & 0x40) == 0)
353 dev_err(dev->pdev, "Timeout while waiting for device to signal ready\n");
354 }
355
356 void ast_post_gpu(struct drm_device *dev)
357 {
358 u32 reg;
359 struct ast_private *ast = dev->dev_private;
360
361 pci_read_config_dword(ast->dev->pdev, 0x04, &reg);
362 reg |= 0x3;
363 pci_write_config_dword(ast->dev->pdev, 0x04, reg);
364
365 ast_enable_vga(dev);
366 ast_open_key(ast);
367 ast_enable_mmio(dev);
368 ast_set_def_ext_reg(dev);
369
370 if (ast->chip == AST2600) {
371 return;
372 } else if (ast->config_mode == ast_use_p2a) {
373 if (ast->chip == AST2500)
374 ast_post_chip_2500(dev);
375 else if (ast->chip == AST2300 || ast->chip == AST2400)
376 ast_post_chip_2300(dev);
377 else
378 ast_init_dram_reg(dev);
379
380 ast_init_3rdtx(dev);
381 } else {
382 if (ast->tx_chip_type != AST_TX_NONE)
383 /* Enable DVO */
384 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xcf, 0x80);
385 }
386 }
387
388 /* AST 2300 DRAM settings */
389 #define AST_DDR3 0
390 #define AST_DDR2 1
391
392 struct ast2300_dram_param {
393 u32 dram_type;
394 u32 dram_chipid;
395 u32 dram_freq;
396 u32 vram_size;
397 u32 odt;
398 u32 wodt;
399 u32 rodt;
400 u32 dram_config;
401 u32 reg_PERIOD;
402 u32 reg_MADJ;
403 u32 reg_SADJ;
404 u32 reg_MRS;
405 u32 reg_EMRS;
406 u32 reg_AC1;
407 u32 reg_AC2;
408 u32 reg_DQSIC;
409 u32 reg_DRV;
410 u32 reg_IOZ;
411 u32 reg_DQIDLY;
412 u32 reg_FREQ;
413 u32 madj_max;
414 u32 dll2_finetune_step;
415 };
416
417 /*
418 * DQSI DLL CBR Setting
419 */
420 #define CBR_SIZE0 ((1 << 10) - 1)
421 #define CBR_SIZE1 ((4 << 10) - 1)
422 #define CBR_SIZE2 ((64 << 10) - 1)
423 #define CBR_PASSNUM 5
424 #define CBR_PASSNUM2 5
425 #define CBR_THRESHOLD 10
426 #define CBR_THRESHOLD2 10
427 #define TIMEOUT 5000000
428 #define CBR_PATNUM 8
429
430 static const u32 pattern[8] = {
431 0xFF00FF00,
432 0xCC33CC33,
433 0xAA55AA55,
434 0x88778877,
435 0x92CC4D6E,
436 0x543D3CDE,
437 0xF1E843C7,
438 0x7C61D253
439 };
440
441 static bool mmc_test(struct ast_private *ast, u32 datagen, u8 test_ctl)
442 {
443 u32 data, timeout;
444
445 ast_moutdwm(ast, 0x1e6e0070, 0x00000000);
446 ast_moutdwm(ast, 0x1e6e0070, (datagen << 3) | test_ctl);
447 timeout = 0;
448 do {
449 data = ast_mindwm(ast, 0x1e6e0070) & 0x3000;
450 if (data & 0x2000)
451 return false;
452 if (++timeout > TIMEOUT) {
453 ast_moutdwm(ast, 0x1e6e0070, 0x00000000);
454 return false;
455 }
456 } while (!data);
457 ast_moutdwm(ast, 0x1e6e0070, 0x0);
458 return true;
459 }
460
461 static u32 mmc_test2(struct ast_private *ast, u32 datagen, u8 test_ctl)
462 {
463 u32 data, timeout;
464
465 ast_moutdwm(ast, 0x1e6e0070, 0x00000000);
466 ast_moutdwm(ast, 0x1e6e0070, (datagen << 3) | test_ctl);
467 timeout = 0;
468 do {
469 data = ast_mindwm(ast, 0x1e6e0070) & 0x1000;
470 if (++timeout > TIMEOUT) {
471 ast_moutdwm(ast, 0x1e6e0070, 0x0);
472 return 0xffffffff;
473 }
474 } while (!data);
475 data = ast_mindwm(ast, 0x1e6e0078);
476 data = (data | (data >> 16)) & 0xffff;
477 ast_moutdwm(ast, 0x1e6e0070, 0x00000000);
478 return data;
479 }
480
481 static bool mmc_test_burst(struct ast_private *ast, u32 datagen)
482 {
483 return mmc_test(ast, datagen, 0xc1);
484 }
485
486 static u32 mmc_test_burst2(struct ast_private *ast, u32 datagen)
487 {
488 return mmc_test2(ast, datagen, 0x41);
489 }
490
491 static bool mmc_test_single(struct ast_private *ast, u32 datagen)
492 {
493 return mmc_test(ast, datagen, 0xc5);
494 }
495
496 static u32 mmc_test_single2(struct ast_private *ast, u32 datagen)
497 {
498 return mmc_test2(ast, datagen, 0x05);
499 }
500
501 static bool mmc_test_single_2500(struct ast_private *ast, u32 datagen)
502 {
503 return mmc_test(ast, datagen, 0x85);
504 }
505
506 static int cbr_test(struct ast_private *ast)
507 {
508 u32 data;
509 int i;
510 data = mmc_test_single2(ast, 0);
511 if ((data & 0xff) && (data & 0xff00))
512 return 0;
513 for (i = 0; i < 8; i++) {
514 data = mmc_test_burst2(ast, i);
515 if ((data & 0xff) && (data & 0xff00))
516 return 0;
517 }
518 if (!data)
519 return 3;
520 else if (data & 0xff)
521 return 2;
522 return 1;
523 }
524
525 static int cbr_scan(struct ast_private *ast)
526 {
527 u32 data, data2, patcnt, loop;
528
529 data2 = 3;
530 for (patcnt = 0; patcnt < CBR_PATNUM; patcnt++) {
531 ast_moutdwm(ast, 0x1e6e007c, pattern[patcnt]);
532 for (loop = 0; loop < CBR_PASSNUM2; loop++) {
533 if ((data = cbr_test(ast)) != 0) {
534 data2 &= data;
535 if (!data2)
536 return 0;
537 break;
538 }
539 }
540 if (loop == CBR_PASSNUM2)
541 return 0;
542 }
543 return data2;
544 }
545
546 static u32 cbr_test2(struct ast_private *ast)
547 {
548 u32 data;
549
550 data = mmc_test_burst2(ast, 0);
551 if (data == 0xffff)
552 return 0;
553 data |= mmc_test_single2(ast, 0);
554 if (data == 0xffff)
555 return 0;
556
557 return ~data & 0xffff;
558 }
559
560 static u32 cbr_scan2(struct ast_private *ast)
561 {
562 u32 data, data2, patcnt, loop;
563
564 data2 = 0xffff;
565 for (patcnt = 0; patcnt < CBR_PATNUM; patcnt++) {
566 ast_moutdwm(ast, 0x1e6e007c, pattern[patcnt]);
567 for (loop = 0; loop < CBR_PASSNUM2; loop++) {
568 if ((data = cbr_test2(ast)) != 0) {
569 data2 &= data;
570 if (!data2)
571 return 0;
572 break;
573 }
574 }
575 if (loop == CBR_PASSNUM2)
576 return 0;
577 }
578 return data2;
579 }
580
581 static bool cbr_test3(struct ast_private *ast)
582 {
583 if (!mmc_test_burst(ast, 0))
584 return false;
585 if (!mmc_test_single(ast, 0))
586 return false;
587 return true;
588 }
589
590 static bool cbr_scan3(struct ast_private *ast)
591 {
592 u32 patcnt, loop;
593
594 for (patcnt = 0; patcnt < CBR_PATNUM; patcnt++) {
595 ast_moutdwm(ast, 0x1e6e007c, pattern[patcnt]);
596 for (loop = 0; loop < 2; loop++) {
597 if (cbr_test3(ast))
598 break;
599 }
600 if (loop == 2)
601 return false;
602 }
603 return true;
604 }
605
606 static bool finetuneDQI_L(struct ast_private *ast, struct ast2300_dram_param *param)
607 {
608 u32 gold_sadj[2], dllmin[16], dllmax[16], dlli, data, cnt, mask, passcnt, retry = 0;
609 bool status = false;
610 FINETUNE_START:
611 for (cnt = 0; cnt < 16; cnt++) {
612 dllmin[cnt] = 0xff;
613 dllmax[cnt] = 0x0;
614 }
615 passcnt = 0;
616 for (dlli = 0; dlli < 76; dlli++) {
617 ast_moutdwm(ast, 0x1E6E0068, 0x00001400 | (dlli << 16) | (dlli << 24));
618 ast_moutdwm(ast, 0x1E6E0074, CBR_SIZE1);
619 data = cbr_scan2(ast);
620 if (data != 0) {
621 mask = 0x00010001;
622 for (cnt = 0; cnt < 16; cnt++) {
623 if (data & mask) {
624 if (dllmin[cnt] > dlli) {
625 dllmin[cnt] = dlli;
626 }
627 if (dllmax[cnt] < dlli) {
628 dllmax[cnt] = dlli;
629 }
630 }
631 mask <<= 1;
632 }
633 passcnt++;
634 } else if (passcnt >= CBR_THRESHOLD2) {
635 break;
636 }
637 }
638 gold_sadj[0] = 0x0;
639 passcnt = 0;
640 for (cnt = 0; cnt < 16; cnt++) {
641 if ((dllmax[cnt] > dllmin[cnt]) && ((dllmax[cnt] - dllmin[cnt]) >= CBR_THRESHOLD2)) {
642 gold_sadj[0] += dllmin[cnt];
643 passcnt++;
644 }
645 }
646 if (retry++ > 10)
647 goto FINETUNE_DONE;
648 if (passcnt != 16) {
649 goto FINETUNE_START;
650 }
651 status = true;
652 FINETUNE_DONE:
653 gold_sadj[0] = gold_sadj[0] >> 4;
654 gold_sadj[1] = gold_sadj[0];
655
656 data = 0;
657 for (cnt = 0; cnt < 8; cnt++) {
658 data >>= 3;
659 if ((dllmax[cnt] > dllmin[cnt]) && ((dllmax[cnt] - dllmin[cnt]) >= CBR_THRESHOLD2)) {
660 dlli = dllmin[cnt];
661 if (gold_sadj[0] >= dlli) {
662 dlli = ((gold_sadj[0] - dlli) * 19) >> 5;
663 if (dlli > 3) {
664 dlli = 3;
665 }
666 } else {
667 dlli = ((dlli - gold_sadj[0]) * 19) >> 5;
668 if (dlli > 4) {
669 dlli = 4;
670 }
671 dlli = (8 - dlli) & 0x7;
672 }
673 data |= dlli << 21;
674 }
675 }
676 ast_moutdwm(ast, 0x1E6E0080, data);
677
678 data = 0;
679 for (cnt = 8; cnt < 16; cnt++) {
680 data >>= 3;
681 if ((dllmax[cnt] > dllmin[cnt]) && ((dllmax[cnt] - dllmin[cnt]) >= CBR_THRESHOLD2)) {
682 dlli = dllmin[cnt];
683 if (gold_sadj[1] >= dlli) {
684 dlli = ((gold_sadj[1] - dlli) * 19) >> 5;
685 if (dlli > 3) {
686 dlli = 3;
687 } else {
688 dlli = (dlli - 1) & 0x7;
689 }
690 } else {
691 dlli = ((dlli - gold_sadj[1]) * 19) >> 5;
692 dlli += 1;
693 if (dlli > 4) {
694 dlli = 4;
695 }
696 dlli = (8 - dlli) & 0x7;
697 }
698 data |= dlli << 21;
699 }
700 }
701 ast_moutdwm(ast, 0x1E6E0084, data);
702 return status;
703 } /* finetuneDQI_L */
704
705 static void finetuneDQSI(struct ast_private *ast)
706 {
707 u32 dlli, dqsip, dqidly;
708 u32 reg_mcr18, reg_mcr0c, passcnt[2], diff;
709 u32 g_dqidly, g_dqsip, g_margin, g_side;
710 u16 pass[32][2][2];
711 char tag[2][76];
712
713 /* Disable DQI CBR */
714 reg_mcr0c = ast_mindwm(ast, 0x1E6E000C);
715 reg_mcr18 = ast_mindwm(ast, 0x1E6E0018);
716 reg_mcr18 &= 0x0000ffff;
717 ast_moutdwm(ast, 0x1E6E0018, reg_mcr18);
718
719 for (dlli = 0; dlli < 76; dlli++) {
720 tag[0][dlli] = 0x0;
721 tag[1][dlli] = 0x0;
722 }
723 for (dqidly = 0; dqidly < 32; dqidly++) {
724 pass[dqidly][0][0] = 0xff;
725 pass[dqidly][0][1] = 0x0;
726 pass[dqidly][1][0] = 0xff;
727 pass[dqidly][1][1] = 0x0;
728 }
729 for (dqidly = 0; dqidly < 32; dqidly++) {
730 passcnt[0] = passcnt[1] = 0;
731 for (dqsip = 0; dqsip < 2; dqsip++) {
732 ast_moutdwm(ast, 0x1E6E000C, 0);
733 ast_moutdwm(ast, 0x1E6E0018, reg_mcr18 | (dqidly << 16) | (dqsip << 23));
734 ast_moutdwm(ast, 0x1E6E000C, reg_mcr0c);
735 for (dlli = 0; dlli < 76; dlli++) {
736 ast_moutdwm(ast, 0x1E6E0068, 0x00001300 | (dlli << 16) | (dlli << 24));
737 ast_moutdwm(ast, 0x1E6E0070, 0);
738 ast_moutdwm(ast, 0x1E6E0074, CBR_SIZE0);
739 if (cbr_scan3(ast)) {
740 if (dlli == 0)
741 break;
742 passcnt[dqsip]++;
743 tag[dqsip][dlli] = 'P';
744 if (dlli < pass[dqidly][dqsip][0])
745 pass[dqidly][dqsip][0] = (u16)dlli;
746 if (dlli > pass[dqidly][dqsip][1])
747 pass[dqidly][dqsip][1] = (u16)dlli;
748 } else if (passcnt[dqsip] >= 5)
749 break;
750 else {
751 pass[dqidly][dqsip][0] = 0xff;
752 pass[dqidly][dqsip][1] = 0x0;
753 }
754 }
755 }
756 if (passcnt[0] == 0 && passcnt[1] == 0)
757 dqidly++;
758 }
759 /* Search margin */
760 g_dqidly = g_dqsip = g_margin = g_side = 0;
761
762 for (dqidly = 0; dqidly < 32; dqidly++) {
763 for (dqsip = 0; dqsip < 2; dqsip++) {
764 if (pass[dqidly][dqsip][0] > pass[dqidly][dqsip][1])
765 continue;
766 diff = pass[dqidly][dqsip][1] - pass[dqidly][dqsip][0];
767 if ((diff+2) < g_margin)
768 continue;
769 passcnt[0] = passcnt[1] = 0;
770 for (dlli = pass[dqidly][dqsip][0]; dlli > 0 && tag[dqsip][dlli] != 0; dlli--, passcnt[0]++);
771 for (dlli = pass[dqidly][dqsip][1]; dlli < 76 && tag[dqsip][dlli] != 0; dlli++, passcnt[1]++);
772 if (passcnt[0] > passcnt[1])
773 passcnt[0] = passcnt[1];
774 passcnt[1] = 0;
775 if (passcnt[0] > g_side)
776 passcnt[1] = passcnt[0] - g_side;
777 if (diff > (g_margin+1) && (passcnt[1] > 0 || passcnt[0] > 8)) {
778 g_margin = diff;
779 g_dqidly = dqidly;
780 g_dqsip = dqsip;
781 g_side = passcnt[0];
782 } else if (passcnt[1] > 1 && g_side < 8) {
783 if (diff > g_margin)
784 g_margin = diff;
785 g_dqidly = dqidly;
786 g_dqsip = dqsip;
787 g_side = passcnt[0];
788 }
789 }
790 }
791 reg_mcr18 = reg_mcr18 | (g_dqidly << 16) | (g_dqsip << 23);
792 ast_moutdwm(ast, 0x1E6E0018, reg_mcr18);
793 }
794
795 static bool cbr_dll2(struct ast_private *ast, struct ast2300_dram_param *param)
796 {
797 u32 dllmin[2], dllmax[2], dlli, data, passcnt, retry = 0;
798 bool status = false;
799
800 finetuneDQSI(ast);
801 if (finetuneDQI_L(ast, param) == false)
802 return status;
803
804 CBR_START2:
805 dllmin[0] = dllmin[1] = 0xff;
806 dllmax[0] = dllmax[1] = 0x0;
807 passcnt = 0;
808 for (dlli = 0; dlli < 76; dlli++) {
809 ast_moutdwm(ast, 0x1E6E0068, 0x00001300 | (dlli << 16) | (dlli << 24));
810 ast_moutdwm(ast, 0x1E6E0074, CBR_SIZE2);
811 data = cbr_scan(ast);
812 if (data != 0) {
813 if (data & 0x1) {
814 if (dllmin[0] > dlli) {
815 dllmin[0] = dlli;
816 }
817 if (dllmax[0] < dlli) {
818 dllmax[0] = dlli;
819 }
820 }
821 if (data & 0x2) {
822 if (dllmin[1] > dlli) {
823 dllmin[1] = dlli;
824 }
825 if (dllmax[1] < dlli) {
826 dllmax[1] = dlli;
827 }
828 }
829 passcnt++;
830 } else if (passcnt >= CBR_THRESHOLD) {
831 break;
832 }
833 }
834 if (retry++ > 10)
835 goto CBR_DONE2;
836 if (dllmax[0] == 0 || (dllmax[0]-dllmin[0]) < CBR_THRESHOLD) {
837 goto CBR_START2;
838 }
839 if (dllmax[1] == 0 || (dllmax[1]-dllmin[1]) < CBR_THRESHOLD) {
840 goto CBR_START2;
841 }
842 status = true;
843 CBR_DONE2:
844 dlli = (dllmin[1] + dllmax[1]) >> 1;
845 dlli <<= 8;
846 dlli += (dllmin[0] + dllmax[0]) >> 1;
847 ast_moutdwm(ast, 0x1E6E0068, ast_mindwm(ast, 0x1E720058) | (dlli << 16));
848 return status;
849 } /* CBRDLL2 */
850
851 static void get_ddr3_info(struct ast_private *ast, struct ast2300_dram_param *param)
852 {
853 u32 trap, trap_AC2, trap_MRS;
854
855 ast_moutdwm(ast, 0x1E6E2000, 0x1688A8A8);
856
857 /* Ger trap info */
858 trap = (ast_mindwm(ast, 0x1E6E2070) >> 25) & 0x3;
859 trap_AC2 = 0x00020000 + (trap << 16);
860 trap_AC2 |= 0x00300000 + ((trap & 0x2) << 19);
861 trap_MRS = 0x00000010 + (trap << 4);
862 trap_MRS |= ((trap & 0x2) << 18);
863
864 param->reg_MADJ = 0x00034C4C;
865 param->reg_SADJ = 0x00001800;
866 param->reg_DRV = 0x000000F0;
867 param->reg_PERIOD = param->dram_freq;
868 param->rodt = 0;
869
870 switch (param->dram_freq) {
871 case 336:
872 ast_moutdwm(ast, 0x1E6E2020, 0x0190);
873 param->wodt = 0;
874 param->reg_AC1 = 0x22202725;
875 param->reg_AC2 = 0xAA007613 | trap_AC2;
876 param->reg_DQSIC = 0x000000BA;
877 param->reg_MRS = 0x04001400 | trap_MRS;
878 param->reg_EMRS = 0x00000000;
879 param->reg_IOZ = 0x00000023;
880 param->reg_DQIDLY = 0x00000074;
881 param->reg_FREQ = 0x00004DC0;
882 param->madj_max = 96;
883 param->dll2_finetune_step = 3;
884 switch (param->dram_chipid) {
885 default:
886 case AST_DRAM_512Mx16:
887 case AST_DRAM_1Gx16:
888 param->reg_AC2 = 0xAA007613 | trap_AC2;
889 break;
890 case AST_DRAM_2Gx16:
891 param->reg_AC2 = 0xAA00761C | trap_AC2;
892 break;
893 case AST_DRAM_4Gx16:
894 param->reg_AC2 = 0xAA007636 | trap_AC2;
895 break;
896 }
897 break;
898 default:
899 case 396:
900 ast_moutdwm(ast, 0x1E6E2020, 0x03F1);
901 param->wodt = 1;
902 param->reg_AC1 = 0x33302825;
903 param->reg_AC2 = 0xCC009617 | trap_AC2;
904 param->reg_DQSIC = 0x000000E2;
905 param->reg_MRS = 0x04001600 | trap_MRS;
906 param->reg_EMRS = 0x00000000;
907 param->reg_IOZ = 0x00000034;
908 param->reg_DRV = 0x000000FA;
909 param->reg_DQIDLY = 0x00000089;
910 param->reg_FREQ = 0x00005040;
911 param->madj_max = 96;
912 param->dll2_finetune_step = 4;
913
914 switch (param->dram_chipid) {
915 default:
916 case AST_DRAM_512Mx16:
917 case AST_DRAM_1Gx16:
918 param->reg_AC2 = 0xCC009617 | trap_AC2;
919 break;
920 case AST_DRAM_2Gx16:
921 param->reg_AC2 = 0xCC009622 | trap_AC2;
922 break;
923 case AST_DRAM_4Gx16:
924 param->reg_AC2 = 0xCC00963F | trap_AC2;
925 break;
926 }
927 break;
928
929 case 408:
930 ast_moutdwm(ast, 0x1E6E2020, 0x01F0);
931 param->wodt = 1;
932 param->reg_AC1 = 0x33302825;
933 param->reg_AC2 = 0xCC009617 | trap_AC2;
934 param->reg_DQSIC = 0x000000E2;
935 param->reg_MRS = 0x04001600 | trap_MRS;
936 param->reg_EMRS = 0x00000000;
937 param->reg_IOZ = 0x00000023;
938 param->reg_DRV = 0x000000FA;
939 param->reg_DQIDLY = 0x00000089;
940 param->reg_FREQ = 0x000050C0;
941 param->madj_max = 96;
942 param->dll2_finetune_step = 4;
943
944 switch (param->dram_chipid) {
945 default:
946 case AST_DRAM_512Mx16:
947 case AST_DRAM_1Gx16:
948 param->reg_AC2 = 0xCC009617 | trap_AC2;
949 break;
950 case AST_DRAM_2Gx16:
951 param->reg_AC2 = 0xCC009622 | trap_AC2;
952 break;
953 case AST_DRAM_4Gx16:
954 param->reg_AC2 = 0xCC00963F | trap_AC2;
955 break;
956 }
957
958 break;
959 case 456:
960 ast_moutdwm(ast, 0x1E6E2020, 0x0230);
961 param->wodt = 0;
962 param->reg_AC1 = 0x33302926;
963 param->reg_AC2 = 0xCD44961A;
964 param->reg_DQSIC = 0x000000FC;
965 param->reg_MRS = 0x00081830;
966 param->reg_EMRS = 0x00000000;
967 param->reg_IOZ = 0x00000045;
968 param->reg_DQIDLY = 0x00000097;
969 param->reg_FREQ = 0x000052C0;
970 param->madj_max = 88;
971 param->dll2_finetune_step = 4;
972 break;
973 case 504:
974 ast_moutdwm(ast, 0x1E6E2020, 0x0270);
975 param->wodt = 1;
976 param->reg_AC1 = 0x33302926;
977 param->reg_AC2 = 0xDE44A61D;
978 param->reg_DQSIC = 0x00000117;
979 param->reg_MRS = 0x00081A30;
980 param->reg_EMRS = 0x00000000;
981 param->reg_IOZ = 0x070000BB;
982 param->reg_DQIDLY = 0x000000A0;
983 param->reg_FREQ = 0x000054C0;
984 param->madj_max = 79;
985 param->dll2_finetune_step = 4;
986 break;
987 case 528:
988 ast_moutdwm(ast, 0x1E6E2020, 0x0290);
989 param->wodt = 1;
990 param->rodt = 1;
991 param->reg_AC1 = 0x33302926;
992 param->reg_AC2 = 0xEF44B61E;
993 param->reg_DQSIC = 0x00000125;
994 param->reg_MRS = 0x00081A30;
995 param->reg_EMRS = 0x00000040;
996 param->reg_DRV = 0x000000F5;
997 param->reg_IOZ = 0x00000023;
998 param->reg_DQIDLY = 0x00000088;
999 param->reg_FREQ = 0x000055C0;
1000 param->madj_max = 76;
1001 param->dll2_finetune_step = 3;
1002 break;
1003 case 576:
1004 ast_moutdwm(ast, 0x1E6E2020, 0x0140);
1005 param->reg_MADJ = 0x00136868;
1006 param->reg_SADJ = 0x00004534;
1007 param->wodt = 1;
1008 param->rodt = 1;
1009 param->reg_AC1 = 0x33302A37;
1010 param->reg_AC2 = 0xEF56B61E;
1011 param->reg_DQSIC = 0x0000013F;
1012 param->reg_MRS = 0x00101A50;
1013 param->reg_EMRS = 0x00000040;
1014 param->reg_DRV = 0x000000FA;
1015 param->reg_IOZ = 0x00000023;
1016 param->reg_DQIDLY = 0x00000078;
1017 param->reg_FREQ = 0x000057C0;
1018 param->madj_max = 136;
1019 param->dll2_finetune_step = 3;
1020 break;
1021 case 600:
1022 ast_moutdwm(ast, 0x1E6E2020, 0x02E1);
1023 param->reg_MADJ = 0x00136868;
1024 param->reg_SADJ = 0x00004534;
1025 param->wodt = 1;
1026 param->rodt = 1;
1027 param->reg_AC1 = 0x32302A37;
1028 param->reg_AC2 = 0xDF56B61F;
1029 param->reg_DQSIC = 0x0000014D;
1030 param->reg_MRS = 0x00101A50;
1031 param->reg_EMRS = 0x00000004;
1032 param->reg_DRV = 0x000000F5;
1033 param->reg_IOZ = 0x00000023;
1034 param->reg_DQIDLY = 0x00000078;
1035 param->reg_FREQ = 0x000058C0;
1036 param->madj_max = 132;
1037 param->dll2_finetune_step = 3;
1038 break;
1039 case 624:
1040 ast_moutdwm(ast, 0x1E6E2020, 0x0160);
1041 param->reg_MADJ = 0x00136868;
1042 param->reg_SADJ = 0x00004534;
1043 param->wodt = 1;
1044 param->rodt = 1;
1045 param->reg_AC1 = 0x32302A37;
1046 param->reg_AC2 = 0xEF56B621;
1047 param->reg_DQSIC = 0x0000015A;
1048 param->reg_MRS = 0x02101A50;
1049 param->reg_EMRS = 0x00000004;
1050 param->reg_DRV = 0x000000F5;
1051 param->reg_IOZ = 0x00000034;
1052 param->reg_DQIDLY = 0x00000078;
1053 param->reg_FREQ = 0x000059C0;
1054 param->madj_max = 128;
1055 param->dll2_finetune_step = 3;
1056 break;
1057 } /* switch freq */
1058
1059 switch (param->dram_chipid) {
1060 case AST_DRAM_512Mx16:
1061 param->dram_config = 0x130;
1062 break;
1063 default:
1064 case AST_DRAM_1Gx16:
1065 param->dram_config = 0x131;
1066 break;
1067 case AST_DRAM_2Gx16:
1068 param->dram_config = 0x132;
1069 break;
1070 case AST_DRAM_4Gx16:
1071 param->dram_config = 0x133;
1072 break;
1073 } /* switch size */
1074
1075 switch (param->vram_size) {
1076 default:
1077 case AST_VIDMEM_SIZE_8M:
1078 param->dram_config |= 0x00;
1079 break;
1080 case AST_VIDMEM_SIZE_16M:
1081 param->dram_config |= 0x04;
1082 break;
1083 case AST_VIDMEM_SIZE_32M:
1084 param->dram_config |= 0x08;
1085 break;
1086 case AST_VIDMEM_SIZE_64M:
1087 param->dram_config |= 0x0c;
1088 break;
1089 }
1090 }
1091
1092 static void ddr3_init(struct ast_private *ast, struct ast2300_dram_param *param)
1093 {
1094 u32 data, data2, retry = 0;
1095 uint32_t timeout;
1096 uint32_t timeout2;
1097
1098 ddr3_init_start:
1099 ast_moutdwm(ast, 0x1E6E0000, 0xFC600309);
1100 ast_moutdwm(ast, 0x1E6E0018, 0x00000100);
1101 ast_moutdwm(ast, 0x1E6E0024, 0x00000000);
1102 ast_moutdwm(ast, 0x1E6E0034, 0x00000000);
1103 udelay(10);
1104 ast_moutdwm(ast, 0x1E6E0064, param->reg_MADJ);
1105 ast_moutdwm(ast, 0x1E6E0068, param->reg_SADJ);
1106 udelay(10);
1107 ast_moutdwm(ast, 0x1E6E0064, param->reg_MADJ | 0xC0000);
1108 udelay(10);
1109
1110 ast_moutdwm(ast, 0x1E6E0004, param->dram_config);
1111 ast_moutdwm(ast, 0x1E6E0008, 0x90040f);
1112 ast_moutdwm(ast, 0x1E6E0010, param->reg_AC1);
1113 ast_moutdwm(ast, 0x1E6E0014, param->reg_AC2);
1114 ast_moutdwm(ast, 0x1E6E0020, param->reg_DQSIC);
1115 ast_moutdwm(ast, 0x1E6E0080, 0x00000000);
1116 ast_moutdwm(ast, 0x1E6E0084, 0x00000000);
1117 ast_moutdwm(ast, 0x1E6E0088, param->reg_DQIDLY);
1118 ast_moutdwm(ast, 0x1E6E0018, 0x4000A170);
1119 ast_moutdwm(ast, 0x1E6E0018, 0x00002370);
1120 ast_moutdwm(ast, 0x1E6E0038, 0x00000000);
1121 ast_moutdwm(ast, 0x1E6E0040, 0xFF444444);
1122 ast_moutdwm(ast, 0x1E6E0044, 0x22222222);
1123 ast_moutdwm(ast, 0x1E6E0048, 0x22222222);
1124 ast_moutdwm(ast, 0x1E6E004C, 0x00000002);
1125 ast_moutdwm(ast, 0x1E6E0050, 0x80000000);
1126 ast_moutdwm(ast, 0x1E6E0050, 0x00000000);
1127 ast_moutdwm(ast, 0x1E6E0054, 0);
1128 ast_moutdwm(ast, 0x1E6E0060, param->reg_DRV);
1129 ast_moutdwm(ast, 0x1E6E006C, param->reg_IOZ);
1130 ast_moutdwm(ast, 0x1E6E0070, 0x00000000);
1131 ast_moutdwm(ast, 0x1E6E0074, 0x00000000);
1132 ast_moutdwm(ast, 0x1E6E0078, 0x00000000);
1133 ast_moutdwm(ast, 0x1E6E007C, 0x00000000);
1134 /* Wait MCLK2X lock to MCLK */
1135 timeout = 0;
1136 do {
1137 data = ast_mindwm(ast, 0x1E6E001C);
1138 timeout++;
1139 } while ((!(data & 0x08000000)) && (timeout < COREBOOT_AST_FAILOVER_TIMEOUT));
1140 if (timeout >= COREBOOT_AST_FAILOVER_TIMEOUT)
1141 dev_err(dev->pdev, "Timeout while waiting for register\n");
1142 data = ast_mindwm(ast, 0x1E6E001C);
1143 data = (data >> 8) & 0xff;
1144 timeout = 0;
1145 while (((data & 0x08) || ((data & 0x7) < 2) || (data < 4)) && (timeout < COREBOOT_AST_FAILOVER_TIMEOUT)) {
1146 data2 = (ast_mindwm(ast, 0x1E6E0064) & 0xfff3ffff) + 4;
1147 if ((data2 & 0xff) > param->madj_max) {
1148 break;
1149 }
1150 ast_moutdwm(ast, 0x1E6E0064, data2);
1151 if (data2 & 0x00100000) {
1152 data2 = ((data2 & 0xff) >> 3) + 3;
1153 } else {
1154 data2 = ((data2 & 0xff) >> 2) + 5;
1155 }
1156 data = ast_mindwm(ast, 0x1E6E0068) & 0xffff00ff;
1157 data2 += data & 0xff;
1158 data = data | (data2 << 8);
1159 ast_moutdwm(ast, 0x1E6E0068, data);
1160 udelay(10);
1161 ast_moutdwm(ast, 0x1E6E0064, ast_mindwm(ast, 0x1E6E0064) | 0xC0000);
1162 udelay(10);
1163 data = ast_mindwm(ast, 0x1E6E0018) & 0xfffff1ff;
1164 ast_moutdwm(ast, 0x1E6E0018, data);
1165 data = data | 0x200;
1166 ast_moutdwm(ast, 0x1E6E0018, data);
1167 timeout2 = 0;
1168 do {
1169 data = ast_mindwm(ast, 0x1E6E001C);
1170 timeout2++;
1171 } while ((!(data & 0x08000000)) && (timeout2 < COREBOOT_AST_FAILOVER_TIMEOUT));
1172 if (timeout2 >= COREBOOT_AST_FAILOVER_TIMEOUT)
1173 dev_err(dev->pdev, "Timeout while waiting for register\n");
1174
1175 data = ast_mindwm(ast, 0x1E6E001C);
1176 data = (data >> 8) & 0xff;
1177
1178 timeout++;
1179 }
1180 if (timeout >= COREBOOT_AST_FAILOVER_TIMEOUT)
1181 dev_err(dev->pdev, "Timeout while waiting for register\n");
1182 ast_moutdwm(ast, 0x1E720058, ast_mindwm(ast, 0x1E6E0068) & 0xffff);
1183 data = ast_mindwm(ast, 0x1E6E0018) | 0xC00;
1184 ast_moutdwm(ast, 0x1E6E0018, data);
1185
1186 ast_moutdwm(ast, 0x1E6E0034, 0x00000001);
1187 ast_moutdwm(ast, 0x1E6E000C, 0x00000040);
1188 udelay(50);
1189 /* Mode Register Setting */
1190 ast_moutdwm(ast, 0x1E6E002C, param->reg_MRS | 0x100);
1191 ast_moutdwm(ast, 0x1E6E0030, param->reg_EMRS);
1192 ast_moutdwm(ast, 0x1E6E0028, 0x00000005);
1193 ast_moutdwm(ast, 0x1E6E0028, 0x00000007);
1194 ast_moutdwm(ast, 0x1E6E0028, 0x00000003);
1195 ast_moutdwm(ast, 0x1E6E0028, 0x00000001);
1196 ast_moutdwm(ast, 0x1E6E002C, param->reg_MRS);
1197 ast_moutdwm(ast, 0x1E6E000C, 0x00005C08);
1198 ast_moutdwm(ast, 0x1E6E0028, 0x00000001);
1199
1200 ast_moutdwm(ast, 0x1E6E000C, 0x00005C01);
1201 data = 0;
1202 if (param->wodt) {
1203 data = 0x300;
1204 }
1205 if (param->rodt) {
1206 data = data | 0x3000 | ((param->reg_AC2 & 0x60000) >> 3);
1207 }
1208 ast_moutdwm(ast, 0x1E6E0034, data | 0x3);
1209
1210 /* Calibrate the DQSI delay */
1211 if ((cbr_dll2(ast, param) == false) && (retry++ < 10))
1212 goto ddr3_init_start;
1213
1214 ast_moutdwm(ast, 0x1E6E0120, param->reg_FREQ);
1215 /* ECC Memory Initialization */
1216 #ifdef ECC
1217 ast_moutdwm(ast, 0x1E6E007C, 0x00000000);
1218 ast_moutdwm(ast, 0x1E6E0070, 0x221);
1219 timeout = 0;
1220 do {
1221 data = ast_mindwm(ast, 0x1E6E0070);
1222 timeout++;
1223 } while ((!(data & 0x00001000)) && (timeout < COREBOOT_AST_FAILOVER_TIMEOUT));
1224 if (timeout >= COREBOOT_AST_FAILOVER_TIMEOUT)
1225 dev_err(dev->pdev, "Timeout while waiting for register\n");
1226 ast_moutdwm(ast, 0x1E6E0070, 0x00000000);
1227 ast_moutdwm(ast, 0x1E6E0050, 0x80000000);
1228 ast_moutdwm(ast, 0x1E6E0050, 0x00000000);
1229 #endif
1230 }
1231
1232 static void get_ddr2_info(struct ast_private *ast, struct ast2300_dram_param *param)
1233 {
1234 u32 trap, trap_AC2, trap_MRS;
1235
1236 ast_moutdwm(ast, 0x1E6E2000, 0x1688A8A8);
1237
1238 /* Ger trap info */
1239 trap = (ast_mindwm(ast, 0x1E6E2070) >> 25) & 0x3;
1240 trap_AC2 = (trap << 20) | (trap << 16);
1241 trap_AC2 += 0x00110000;
1242 trap_MRS = 0x00000040 | (trap << 4);
1243
1244 param->reg_MADJ = 0x00034C4C;
1245 param->reg_SADJ = 0x00001800;
1246 param->reg_DRV = 0x000000F0;
1247 param->reg_PERIOD = param->dram_freq;
1248 param->rodt = 0;
1249
1250 switch (param->dram_freq) {
1251 case 264:
1252 ast_moutdwm(ast, 0x1E6E2020, 0x0130);
1253 param->wodt = 0;
1254 param->reg_AC1 = 0x11101513;
1255 param->reg_AC2 = 0x78117011;
1256 param->reg_DQSIC = 0x00000092;
1257 param->reg_MRS = 0x00000842;
1258 param->reg_EMRS = 0x00000000;
1259 param->reg_DRV = 0x000000F0;
1260 param->reg_IOZ = 0x00000034;
1261 param->reg_DQIDLY = 0x0000005A;
1262 param->reg_FREQ = 0x00004AC0;
1263 param->madj_max = 138;
1264 param->dll2_finetune_step = 3;
1265 break;
1266 case 336:
1267 ast_moutdwm(ast, 0x1E6E2020, 0x0190);
1268 param->wodt = 1;
1269 param->reg_AC1 = 0x22202613;
1270 param->reg_AC2 = 0xAA009016 | trap_AC2;
1271 param->reg_DQSIC = 0x000000BA;
1272 param->reg_MRS = 0x00000A02 | trap_MRS;
1273 param->reg_EMRS = 0x00000040;
1274 param->reg_DRV = 0x000000FA;
1275 param->reg_IOZ = 0x00000034;
1276 param->reg_DQIDLY = 0x00000074;
1277 param->reg_FREQ = 0x00004DC0;
1278 param->madj_max = 96;
1279 param->dll2_finetune_step = 3;
1280 switch (param->dram_chipid) {
1281 default:
1282 case AST_DRAM_512Mx16:
1283 param->reg_AC2 = 0xAA009012 | trap_AC2;
1284 break;
1285 case AST_DRAM_1Gx16:
1286 param->reg_AC2 = 0xAA009016 | trap_AC2;
1287 break;
1288 case AST_DRAM_2Gx16:
1289 param->reg_AC2 = 0xAA009023 | trap_AC2;
1290 break;
1291 case AST_DRAM_4Gx16:
1292 param->reg_AC2 = 0xAA00903B | trap_AC2;
1293 break;
1294 }
1295 break;
1296 default:
1297 case 396:
1298 ast_moutdwm(ast, 0x1E6E2020, 0x03F1);
1299 param->wodt = 1;
1300 param->rodt = 0;
1301 param->reg_AC1 = 0x33302714;
1302 param->reg_AC2 = 0xCC00B01B | trap_AC2;
1303 param->reg_DQSIC = 0x000000E2;
1304 param->reg_MRS = 0x00000C02 | trap_MRS;
1305 param->reg_EMRS = 0x00000040;
1306 param->reg_DRV = 0x000000FA;
1307 param->reg_IOZ = 0x00000034;
1308 param->reg_DQIDLY = 0x00000089;
1309 param->reg_FREQ = 0x00005040;
1310 param->madj_max = 96;
1311 param->dll2_finetune_step = 4;
1312
1313 switch (param->dram_chipid) {
1314 case AST_DRAM_512Mx16:
1315 param->reg_AC2 = 0xCC00B016 | trap_AC2;
1316 break;
1317 default:
1318 case AST_DRAM_1Gx16:
1319 param->reg_AC2 = 0xCC00B01B | trap_AC2;
1320 break;
1321 case AST_DRAM_2Gx16:
1322 param->reg_AC2 = 0xCC00B02B | trap_AC2;
1323 break;
1324 case AST_DRAM_4Gx16:
1325 param->reg_AC2 = 0xCC00B03F | trap_AC2;
1326 break;
1327 }
1328
1329 break;
1330
1331 case 408:
1332 ast_moutdwm(ast, 0x1E6E2020, 0x01F0);
1333 param->wodt = 1;
1334 param->rodt = 0;
1335 param->reg_AC1 = 0x33302714;
1336 param->reg_AC2 = 0xCC00B01B | trap_AC2;
1337 param->reg_DQSIC = 0x000000E2;
1338 param->reg_MRS = 0x00000C02 | trap_MRS;
1339 param->reg_EMRS = 0x00000040;
1340 param->reg_DRV = 0x000000FA;
1341 param->reg_IOZ = 0x00000034;
1342 param->reg_DQIDLY = 0x00000089;
1343 param->reg_FREQ = 0x000050C0;
1344 param->madj_max = 96;
1345 param->dll2_finetune_step = 4;
1346
1347 switch (param->dram_chipid) {
1348 case AST_DRAM_512Mx16:
1349 param->reg_AC2 = 0xCC00B016 | trap_AC2;
1350 break;
1351 default:
1352 case AST_DRAM_1Gx16:
1353 param->reg_AC2 = 0xCC00B01B | trap_AC2;
1354 break;
1355 case AST_DRAM_2Gx16:
1356 param->reg_AC2 = 0xCC00B02B | trap_AC2;
1357 break;
1358 case AST_DRAM_4Gx16:
1359 param->reg_AC2 = 0xCC00B03F | trap_AC2;
1360 break;
1361 }
1362
1363 break;
1364 case 456:
1365 ast_moutdwm(ast, 0x1E6E2020, 0x0230);
1366 param->wodt = 0;
1367 param->reg_AC1 = 0x33302815;
1368 param->reg_AC2 = 0xCD44B01E;
1369 param->reg_DQSIC = 0x000000FC;
1370 param->reg_MRS = 0x00000E72;
1371 param->reg_EMRS = 0x00000000;
1372 param->reg_DRV = 0x00000000;
1373 param->reg_IOZ = 0x00000034;
1374 param->reg_DQIDLY = 0x00000097;
1375 param->reg_FREQ = 0x000052C0;
1376 param->madj_max = 88;
1377 param->dll2_finetune_step = 3;
1378 break;
1379 case 504:
1380 ast_moutdwm(ast, 0x1E6E2020, 0x0261);
1381 param->wodt = 1;
1382 param->rodt = 1;
1383 param->reg_AC1 = 0x33302815;
1384 param->reg_AC2 = 0xDE44C022;
1385 param->reg_DQSIC = 0x00000117;
1386 param->reg_MRS = 0x00000E72;
1387 param->reg_EMRS = 0x00000040;
1388 param->reg_DRV = 0x0000000A;
1389 param->reg_IOZ = 0x00000045;
1390 param->reg_DQIDLY = 0x000000A0;
1391 param->reg_FREQ = 0x000054C0;
1392 param->madj_max = 79;
1393 param->dll2_finetune_step = 3;
1394 break;
1395 case 528:
1396 ast_moutdwm(ast, 0x1E6E2020, 0x0120);
1397 param->wodt = 1;
1398 param->rodt = 1;
1399 param->reg_AC1 = 0x33302815;
1400 param->reg_AC2 = 0xEF44D024;
1401 param->reg_DQSIC = 0x00000125;
1402 param->reg_MRS = 0x00000E72;
1403 param->reg_EMRS = 0x00000004;
1404 param->reg_DRV = 0x000000F9;
1405 param->reg_IOZ = 0x00000045;
1406 param->reg_DQIDLY = 0x000000A7;
1407 param->reg_FREQ = 0x000055C0;
1408 param->madj_max = 76;
1409 param->dll2_finetune_step = 3;
1410 break;
1411 case 552:
1412 ast_moutdwm(ast, 0x1E6E2020, 0x02A1);
1413 param->wodt = 1;
1414 param->rodt = 1;
1415 param->reg_AC1 = 0x43402915;
1416 param->reg_AC2 = 0xFF44E025;
1417 param->reg_DQSIC = 0x00000132;
1418 param->reg_MRS = 0x00000E72;
1419 param->reg_EMRS = 0x00000040;
1420 param->reg_DRV = 0x0000000A;
1421 param->reg_IOZ = 0x00000045;
1422 param->reg_DQIDLY = 0x000000AD;
1423 param->reg_FREQ = 0x000056C0;
1424 param->madj_max = 76;
1425 param->dll2_finetune_step = 3;
1426 break;
1427 case 576:
1428 ast_moutdwm(ast, 0x1E6E2020, 0x0140);
1429 param->wodt = 1;
1430 param->rodt = 1;
1431 param->reg_AC1 = 0x43402915;
1432 param->reg_AC2 = 0xFF44E027;
1433 param->reg_DQSIC = 0x0000013F;
1434 param->reg_MRS = 0x00000E72;
1435 param->reg_EMRS = 0x00000004;
1436 param->reg_DRV = 0x000000F5;
1437 param->reg_IOZ = 0x00000045;
1438 param->reg_DQIDLY = 0x000000B3;
1439 param->reg_FREQ = 0x000057C0;
1440 param->madj_max = 76;
1441 param->dll2_finetune_step = 3;
1442 break;
1443 }
1444
1445 switch (param->dram_chipid) {
1446 case AST_DRAM_512Mx16:
1447 param->dram_config = 0x100;
1448 break;
1449 default:
1450 case AST_DRAM_1Gx16:
1451 param->dram_config = 0x121;
1452 break;
1453 case AST_DRAM_2Gx16:
1454 param->dram_config = 0x122;
1455 break;
1456 case AST_DRAM_4Gx16:
1457 param->dram_config = 0x123;
1458 break;
1459 } /* switch size */
1460
1461 switch (param->vram_size) {
1462 default:
1463 case AST_VIDMEM_SIZE_8M:
1464 param->dram_config |= 0x00;
1465 break;
1466 case AST_VIDMEM_SIZE_16M:
1467 param->dram_config |= 0x04;
1468 break;
1469 case AST_VIDMEM_SIZE_32M:
1470 param->dram_config |= 0x08;
1471 break;
1472 case AST_VIDMEM_SIZE_64M:
1473 param->dram_config |= 0x0c;
1474 break;
1475 }
1476 }
1477
1478 static void ddr2_init(struct ast_private *ast, struct ast2300_dram_param *param)
1479 {
1480 u32 data, data2, retry = 0;
1481 uint32_t timeout;
1482 uint32_t timeout2;
1483
1484 ddr2_init_start:
1485 ast_moutdwm(ast, 0x1E6E0000, 0xFC600309);
1486 ast_moutdwm(ast, 0x1E6E0018, 0x00000100);
1487 ast_moutdwm(ast, 0x1E6E0024, 0x00000000);
1488 ast_moutdwm(ast, 0x1E6E0064, param->reg_MADJ);
1489 ast_moutdwm(ast, 0x1E6E0068, param->reg_SADJ);
1490 udelay(10);
1491 ast_moutdwm(ast, 0x1E6E0064, param->reg_MADJ | 0xC0000);
1492 udelay(10);
1493
1494 ast_moutdwm(ast, 0x1E6E0004, param->dram_config);
1495 ast_moutdwm(ast, 0x1E6E0008, 0x90040f);
1496 ast_moutdwm(ast, 0x1E6E0010, param->reg_AC1);
1497 ast_moutdwm(ast, 0x1E6E0014, param->reg_AC2);
1498 ast_moutdwm(ast, 0x1E6E0020, param->reg_DQSIC);
1499 ast_moutdwm(ast, 0x1E6E0080, 0x00000000);
1500 ast_moutdwm(ast, 0x1E6E0084, 0x00000000);
1501 ast_moutdwm(ast, 0x1E6E0088, param->reg_DQIDLY);
1502 ast_moutdwm(ast, 0x1E6E0018, 0x4000A130);
1503 ast_moutdwm(ast, 0x1E6E0018, 0x00002330);
1504 ast_moutdwm(ast, 0x1E6E0038, 0x00000000);
1505 ast_moutdwm(ast, 0x1E6E0040, 0xFF808000);
1506 ast_moutdwm(ast, 0x1E6E0044, 0x88848466);
1507 ast_moutdwm(ast, 0x1E6E0048, 0x44440008);
1508 ast_moutdwm(ast, 0x1E6E004C, 0x00000000);
1509 ast_moutdwm(ast, 0x1E6E0050, 0x80000000);
1510 ast_moutdwm(ast, 0x1E6E0050, 0x00000000);
1511 ast_moutdwm(ast, 0x1E6E0054, 0);
1512 ast_moutdwm(ast, 0x1E6E0060, param->reg_DRV);
1513 ast_moutdwm(ast, 0x1E6E006C, param->reg_IOZ);
1514 ast_moutdwm(ast, 0x1E6E0070, 0x00000000);
1515 ast_moutdwm(ast, 0x1E6E0074, 0x00000000);
1516 ast_moutdwm(ast, 0x1E6E0078, 0x00000000);
1517 ast_moutdwm(ast, 0x1E6E007C, 0x00000000);
1518
1519 /* Wait MCLK2X lock to MCLK */
1520 timeout = 0;
1521 do {
1522 data = ast_mindwm(ast, 0x1E6E001C);
1523 timeout++;
1524 } while ((!(data & 0x08000000)) && (timeout < COREBOOT_AST_FAILOVER_TIMEOUT));
1525 if (timeout >= COREBOOT_AST_FAILOVER_TIMEOUT)
1526 dev_err(dev->pdev, "Timeout while waiting for register\n");
1527 data = ast_mindwm(ast, 0x1E6E001C);
1528 data = (data >> 8) & 0xff;
1529 timeout = 0;
1530 while (((data & 0x08) || ((data & 0x7) < 2) || (data < 4)) && (timeout < COREBOOT_AST_FAILOVER_TIMEOUT)) {
1531 data2 = (ast_mindwm(ast, 0x1E6E0064) & 0xfff3ffff) + 4;
1532 if ((data2 & 0xff) > param->madj_max) {
1533 break;
1534 }
1535 ast_moutdwm(ast, 0x1E6E0064, data2);
1536 if (data2 & 0x00100000) {
1537 data2 = ((data2 & 0xff) >> 3) + 3;
1538 } else {
1539 data2 = ((data2 & 0xff) >> 2) + 5;
1540 }
1541 data = ast_mindwm(ast, 0x1E6E0068) & 0xffff00ff;
1542 data2 += data & 0xff;
1543 data = data | (data2 << 8);
1544 ast_moutdwm(ast, 0x1E6E0068, data);
1545 udelay(10);
1546 ast_moutdwm(ast, 0x1E6E0064, ast_mindwm(ast, 0x1E6E0064) | 0xC0000);
1547 udelay(10);
1548 data = ast_mindwm(ast, 0x1E6E0018) & 0xfffff1ff;
1549 ast_moutdwm(ast, 0x1E6E0018, data);
1550 data = data | 0x200;
1551 ast_moutdwm(ast, 0x1E6E0018, data);
1552 timeout2 = 0;
1553 do {
1554 data = ast_mindwm(ast, 0x1E6E001C);
1555 timeout2++;
1556 } while ((!(data & 0x08000000)) && (timeout2 < COREBOOT_AST_FAILOVER_TIMEOUT));
1557 if (timeout2 >= COREBOOT_AST_FAILOVER_TIMEOUT)
1558 dev_err(dev->pdev, "Timeout while waiting for register\n");
1559
1560 data = ast_mindwm(ast, 0x1E6E001C);
1561 data = (data >> 8) & 0xff;
1562
1563 timeout++;
1564 }
1565 if (timeout >= COREBOOT_AST_FAILOVER_TIMEOUT)
1566 dev_err(dev->pdev, "Timeout while waiting for register\n");
1567 ast_moutdwm(ast, 0x1E720058, ast_mindwm(ast, 0x1E6E0008) & 0xffff);
1568 data = ast_mindwm(ast, 0x1E6E0018) | 0xC00;
1569 ast_moutdwm(ast, 0x1E6E0018, data);
1570
1571 ast_moutdwm(ast, 0x1E6E0034, 0x00000001);
1572 ast_moutdwm(ast, 0x1E6E000C, 0x00000000);
1573 udelay(50);
1574 /* Mode Register Setting */
1575 ast_moutdwm(ast, 0x1E6E002C, param->reg_MRS | 0x100);
1576 ast_moutdwm(ast, 0x1E6E0030, param->reg_EMRS);
1577 ast_moutdwm(ast, 0x1E6E0028, 0x00000005);
1578 ast_moutdwm(ast, 0x1E6E0028, 0x00000007);
1579 ast_moutdwm(ast, 0x1E6E0028, 0x00000003);
1580 ast_moutdwm(ast, 0x1E6E0028, 0x00000001);
1581
1582 ast_moutdwm(ast, 0x1E6E000C, 0x00005C08);
1583 ast_moutdwm(ast, 0x1E6E002C, param->reg_MRS);
1584 ast_moutdwm(ast, 0x1E6E0028, 0x00000001);
1585 ast_moutdwm(ast, 0x1E6E0030, param->reg_EMRS | 0x380);
1586 ast_moutdwm(ast, 0x1E6E0028, 0x00000003);
1587 ast_moutdwm(ast, 0x1E6E0030, param->reg_EMRS);
1588 ast_moutdwm(ast, 0x1E6E0028, 0x00000003);
1589
1590 ast_moutdwm(ast, 0x1E6E000C, 0x7FFF5C01);
1591 data = 0;
1592 if (param->wodt) {
1593 data = 0x500;
1594 }
1595 if (param->rodt) {
1596 data = data | 0x3000 | ((param->reg_AC2 & 0x60000) >> 3);
1597 }
1598 ast_moutdwm(ast, 0x1E6E0034, data | 0x3);
1599 ast_moutdwm(ast, 0x1E6E0120, param->reg_FREQ);
1600
1601 /* Calibrate the DQSI delay */
1602 if ((cbr_dll2(ast, param) == false) && (retry++ < 10))
1603 goto ddr2_init_start;
1604
1605 /* ECC Memory Initialization */
1606 #ifdef ECC
1607 ast_moutdwm(ast, 0x1E6E007C, 0x00000000);
1608 ast_moutdwm(ast, 0x1E6E0070, 0x221);
1609 timeout = 0;
1610 do {
1611 data = ast_mindwm(ast, 0x1E6E0070);
1612 timeout++;
1613 } while ((!(data & 0x00001000)) && (timeout < COREBOOT_AST_FAILOVER_TIMEOUT));
1614 if (timeout >= COREBOOT_AST_FAILOVER_TIMEOUT)
1615 dev_err(dev->pdev, "Timeout while waiting for register\n");
1616 ast_moutdwm(ast, 0x1E6E0070, 0x00000000);
1617 ast_moutdwm(ast, 0x1E6E0050, 0x80000000);
1618 ast_moutdwm(ast, 0x1E6E0050, 0x00000000);
1619 #endif
1620 }
1621
1622 static void ast_post_chip_2300(struct drm_device *dev)
1623 {
1624 struct ast_private *ast = dev->dev_private;
1625 struct ast2300_dram_param param;
1626 u32 temp;
1627 u8 reg;
1628 uint32_t timeout;
1629
1630 reg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
1631 if ((reg & 0x80) == 0) {/* vga only */
1632 ast_write32(ast, 0xf004, 0x1e6e0000);
1633 ast_write32(ast, 0xf000, 0x1);
1634 ast_write32(ast, 0x12000, 0x1688a8a8);
1635 timeout = 0;
1636 do {
1637 timeout++;
1638 } while ((ast_read32(ast, 0x12000) != 0x1) && (timeout < COREBOOT_AST_FAILOVER_TIMEOUT));
1639 if (timeout >= COREBOOT_AST_FAILOVER_TIMEOUT)
1640 dev_err(dev->pdev, "Timeout while waiting for register\n");
1641
1642 ast_write32(ast, 0x10000, 0xfc600309);
1643 timeout = 0;
1644 do {
1645 timeout++;
1646 } while ((ast_read32(ast, 0x10000) != 0x1) && (timeout < COREBOOT_AST_FAILOVER_TIMEOUT));
1647 if (timeout >= COREBOOT_AST_FAILOVER_TIMEOUT)
1648 dev_err(dev->pdev, "Timeout while waiting for register\n");
1649
1650 /* Slow down CPU/AHB CLK in VGA only mode */
1651 temp = ast_read32(ast, 0x12008);
1652 temp |= 0x73;
1653 ast_write32(ast, 0x12008, temp);
1654
1655 param.dram_freq = 396;
1656 param.dram_type = AST_DDR3;
1657 temp = ast_mindwm(ast, 0x1e6e2070);
1658 if (temp & 0x01000000)
1659 param.dram_type = AST_DDR2;
1660 switch (temp & 0x18000000) {
1661 case 0:
1662 param.dram_chipid = AST_DRAM_512Mx16;
1663 break;
1664 default:
1665 case 0x08000000:
1666 param.dram_chipid = AST_DRAM_1Gx16;
1667 break;
1668 case 0x10000000:
1669 param.dram_chipid = AST_DRAM_2Gx16;
1670 break;
1671 case 0x18000000:
1672 param.dram_chipid = AST_DRAM_4Gx16;
1673 break;
1674 }
1675 switch (temp & 0x0c) {
1676 default:
1677 case 0x00:
1678 param.vram_size = AST_VIDMEM_SIZE_8M;
1679 break;
1680
1681 case 0x04:
1682 param.vram_size = AST_VIDMEM_SIZE_16M;
1683 break;
1684
1685 case 0x08:
1686 param.vram_size = AST_VIDMEM_SIZE_32M;
1687 break;
1688
1689 case 0x0c:
1690 param.vram_size = AST_VIDMEM_SIZE_64M;
1691 break;
1692 }
1693
1694 if (param.dram_type == AST_DDR3) {
1695 get_ddr3_info(ast, &param);
1696 ddr3_init(ast, &param);
1697 } else {
1698 get_ddr2_info(ast, &param);
1699 ddr2_init(ast, &param);
1700 }
1701
1702 temp = ast_mindwm(ast, 0x1e6e2040);
1703 ast_moutdwm(ast, 0x1e6e2040, temp | 0x40);
1704 }
1705
1706 /* wait ready */
1707 timeout = 0;
1708 do {
1709 reg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
1710 timeout++;
1711 } while (((reg & 0x40) == 0) && (timeout < COREBOOT_AST_FAILOVER_TIMEOUT));
1712 if (timeout >= COREBOOT_AST_FAILOVER_TIMEOUT)
1713 dev_err(dev->pdev, "Timeout while waiting for register\n");
1714 }
1715
1716 static bool cbr_test_2500(struct ast_private *ast)
1717 {
1718 ast_moutdwm(ast, 0x1E6E0074, 0x0000FFFF);
1719 ast_moutdwm(ast, 0x1E6E007C, 0xFF00FF00);
1720 if (!mmc_test_burst(ast, 0))
1721 return false;
1722 if (!mmc_test_single_2500(ast, 0))
1723 return false;
1724 return true;
1725 }
1726
1727 static bool ddr_test_2500(struct ast_private *ast)
1728 {
1729 ast_moutdwm(ast, 0x1E6E0074, 0x0000FFFF);
1730 ast_moutdwm(ast, 0x1E6E007C, 0xFF00FF00);
1731 if (!mmc_test_burst(ast, 0))
1732 return false;
1733 if (!mmc_test_burst(ast, 1))
1734 return false;
1735 if (!mmc_test_burst(ast, 2))
1736 return false;
1737 if (!mmc_test_burst(ast, 3))
1738 return false;
1739 if (!mmc_test_single_2500(ast, 0))
1740 return false;
1741 return true;
1742 }
1743
1744 static void ddr_init_common_2500(struct ast_private *ast)
1745 {
1746 ast_moutdwm(ast, 0x1E6E0034, 0x00020080);
1747 ast_moutdwm(ast, 0x1E6E0008, 0x2003000F);
1748 ast_moutdwm(ast, 0x1E6E0038, 0x00000FFF);
1749 ast_moutdwm(ast, 0x1E6E0040, 0x88448844);
1750 ast_moutdwm(ast, 0x1E6E0044, 0x24422288);
1751 ast_moutdwm(ast, 0x1E6E0048, 0x22222222);
1752 ast_moutdwm(ast, 0x1E6E004C, 0x22222222);
1753 ast_moutdwm(ast, 0x1E6E0050, 0x80000000);
1754 ast_moutdwm(ast, 0x1E6E0208, 0x00000000);
1755 ast_moutdwm(ast, 0x1E6E0218, 0x00000000);
1756 ast_moutdwm(ast, 0x1E6E0220, 0x00000000);
1757 ast_moutdwm(ast, 0x1E6E0228, 0x00000000);
1758 ast_moutdwm(ast, 0x1E6E0230, 0x00000000);
1759 ast_moutdwm(ast, 0x1E6E02A8, 0x00000000);
1760 ast_moutdwm(ast, 0x1E6E02B0, 0x00000000);
1761 ast_moutdwm(ast, 0x1E6E0240, 0x86000000);
1762 ast_moutdwm(ast, 0x1E6E0244, 0x00008600);
1763 ast_moutdwm(ast, 0x1E6E0248, 0x80000000);
1764 ast_moutdwm(ast, 0x1E6E024C, 0x80808080);
1765 }
1766
1767 static void ddr_phy_init_2500(struct ast_private *ast)
1768 {
1769 u32 data, pass, timecnt;
1770
1771 pass = 0;
1772 ast_moutdwm(ast, 0x1E6E0060, 0x00000005);
1773 while (!pass) {
1774 for (timecnt = 0; timecnt < TIMEOUT; timecnt++) {
1775 data = ast_mindwm(ast, 0x1E6E0060) & 0x1;
1776 if (!data)
1777 break;
1778 }
1779 if (timecnt != TIMEOUT) {
1780 data = ast_mindwm(ast, 0x1E6E0300) & 0x000A0000;
1781 if (!data)
1782 pass = 1;
1783 }
1784 if (!pass) {
1785 ast_moutdwm(ast, 0x1E6E0060, 0x00000000);
1786 udelay(10); /* delay 10 us */
1787 ast_moutdwm(ast, 0x1E6E0060, 0x00000005);
1788 }
1789 }
1790
1791 ast_moutdwm(ast, 0x1E6E0060, 0x00000006);
1792 }
1793
1794 /*
1795 * Check DRAM Size
1796 * 1Gb : 0x80000000 ~ 0x87FFFFFF
1797 * 2Gb : 0x80000000 ~ 0x8FFFFFFF
1798 * 4Gb : 0x80000000 ~ 0x9FFFFFFF
1799 * 8Gb : 0x80000000 ~ 0xBFFFFFFF
1800 */
1801 static void check_dram_size_2500(struct ast_private *ast, u32 tRFC)
1802 {
1803 u32 reg_04, reg_14;
1804
1805 reg_04 = ast_mindwm(ast, 0x1E6E0004) & 0xfffffffc;
1806 reg_14 = ast_mindwm(ast, 0x1E6E0014) & 0xffffff00;
1807
1808 ast_moutdwm(ast, 0xA0100000, 0x41424344);
1809 ast_moutdwm(ast, 0x90100000, 0x35363738);
1810 ast_moutdwm(ast, 0x88100000, 0x292A2B2C);
1811 ast_moutdwm(ast, 0x80100000, 0x1D1E1F10);
1812
1813 /* Check 8Gbit */
1814 if (ast_mindwm(ast, 0xA0100000) == 0x41424344) {
1815 reg_04 |= 0x03;
1816 reg_14 |= (tRFC >> 24) & 0xFF;
1817 /* Check 4Gbit */
1818 } else if (ast_mindwm(ast, 0x90100000) == 0x35363738) {
1819 reg_04 |= 0x02;
1820 reg_14 |= (tRFC >> 16) & 0xFF;
1821 /* Check 2Gbit */
1822 } else if (ast_mindwm(ast, 0x88100000) == 0x292A2B2C) {
1823 reg_04 |= 0x01;
1824 reg_14 |= (tRFC >> 8) & 0xFF;
1825 } else {
1826 reg_14 |= tRFC & 0xFF;
1827 }
1828 ast_moutdwm(ast, 0x1E6E0004, reg_04);
1829 ast_moutdwm(ast, 0x1E6E0014, reg_14);
1830 }
1831
1832 static void enable_cache_2500(struct ast_private *ast)
1833 {
1834 u32 reg_04, data;
1835
1836 reg_04 = ast_mindwm(ast, 0x1E6E0004);
1837 ast_moutdwm(ast, 0x1E6E0004, reg_04 | 0x1000);
1838
1839 do
1840 data = ast_mindwm(ast, 0x1E6E0004);
1841 while (!(data & 0x80000));
1842 ast_moutdwm(ast, 0x1E6E0004, reg_04 | 0x400);
1843 }
1844
1845 static void set_mpll_2500(struct ast_private *ast)
1846 {
1847 u32 addr, data, param;
1848
1849 /* Reset MMC */
1850 ast_moutdwm(ast, 0x1E6E0000, 0xFC600309);
1851 ast_moutdwm(ast, 0x1E6E0034, 0x00020080);
1852 for (addr = 0x1e6e0004; addr < 0x1e6e0090;) {
1853 ast_moutdwm(ast, addr, 0x0);
1854 addr += 4;
1855 }
1856 ast_moutdwm(ast, 0x1E6E0034, 0x00020000);
1857
1858 ast_moutdwm(ast, 0x1E6E2000, 0x1688A8A8);
1859 data = ast_mindwm(ast, 0x1E6E2070) & 0x00800000;
1860 if (data) {
1861 /* CLKIN = 25MHz */
1862 param = 0x930023E0;
1863 ast_moutdwm(ast, 0x1E6E2160, 0x00011320);
1864 } else {
1865 /* CLKIN = 24MHz */
1866 param = 0x93002400;
1867 }
1868 ast_moutdwm(ast, 0x1E6E2020, param);
1869 udelay(100);
1870 }
1871
1872 static void reset_mmc_2500(struct ast_private *ast)
1873 {
1874 ast_moutdwm(ast, 0x1E78505C, 0x00000004);
1875 ast_moutdwm(ast, 0x1E785044, 0x00000001);
1876 ast_moutdwm(ast, 0x1E785048, 0x00004755);
1877 ast_moutdwm(ast, 0x1E78504C, 0x00000013);
1878 mdelay(100);
1879 ast_moutdwm(ast, 0x1E785054, 0x00000077);
1880 ast_moutdwm(ast, 0x1E6E0000, 0xFC600309);
1881 }
1882
1883 static void ddr3_init_2500(struct ast_private *ast, const u32 *ddr_table)
1884 {
1885 ast_moutdwm(ast, 0x1E6E0004, 0x00000303);
1886 ast_moutdwm(ast, 0x1E6E0010, ddr_table[REGIDX_010]);
1887 ast_moutdwm(ast, 0x1E6E0014, ddr_table[REGIDX_014]);
1888 ast_moutdwm(ast, 0x1E6E0018, ddr_table[REGIDX_018]);
1889 ast_moutdwm(ast, 0x1E6E0020, ddr_table[REGIDX_020]); /* MODEREG4/6 */
1890 ast_moutdwm(ast, 0x1E6E0024, ddr_table[REGIDX_024]); /* MODEREG5 */
1891 ast_moutdwm(ast, 0x1E6E002C, ddr_table[REGIDX_02C] | 0x100); /* MODEREG0/2 */
1892 ast_moutdwm(ast, 0x1E6E0030, ddr_table[REGIDX_030]); /* MODEREG1/3 */
1893
1894 /* DDR PHY Setting */
1895 ast_moutdwm(ast, 0x1E6E0200, 0x02492AAE);
1896 ast_moutdwm(ast, 0x1E6E0204, 0x00001001);
1897 ast_moutdwm(ast, 0x1E6E020C, 0x55E00B0B);
1898 ast_moutdwm(ast, 0x1E6E0210, 0x20000000);
1899 ast_moutdwm(ast, 0x1E6E0214, ddr_table[REGIDX_214]);
1900 ast_moutdwm(ast, 0x1E6E02E0, ddr_table[REGIDX_2E0]);
1901 ast_moutdwm(ast, 0x1E6E02E4, ddr_table[REGIDX_2E4]);
1902 ast_moutdwm(ast, 0x1E6E02E8, ddr_table[REGIDX_2E8]);
1903 ast_moutdwm(ast, 0x1E6E02EC, ddr_table[REGIDX_2EC]);
1904 ast_moutdwm(ast, 0x1E6E02F0, ddr_table[REGIDX_2F0]);
1905 ast_moutdwm(ast, 0x1E6E02F4, ddr_table[REGIDX_2F4]);
1906 ast_moutdwm(ast, 0x1E6E02F8, ddr_table[REGIDX_2F8]);
1907 ast_moutdwm(ast, 0x1E6E0290, 0x00100008);
1908 ast_moutdwm(ast, 0x1E6E02C0, 0x00000006);
1909
1910 /* Controller Setting */
1911 ast_moutdwm(ast, 0x1E6E0034, 0x00020091);
1912
1913 /* Wait DDR PHY init done */
1914 ddr_phy_init_2500(ast);
1915
1916 ast_moutdwm(ast, 0x1E6E0120, ddr_table[REGIDX_PLL]);
1917 ast_moutdwm(ast, 0x1E6E000C, 0x42AA5C81);
1918 ast_moutdwm(ast, 0x1E6E0034, 0x0001AF93);
1919
1920 check_dram_size_2500(ast, ddr_table[REGIDX_RFC]);
1921 enable_cache_2500(ast);
1922 ast_moutdwm(ast, 0x1E6E001C, 0x00000008);
1923 ast_moutdwm(ast, 0x1E6E0038, 0xFFFFFF00);
1924 }
1925
1926 static void ddr4_init_2500(struct ast_private *ast, const u32 *ddr_table)
1927 {
1928 u32 data, data2, pass, retrycnt;
1929 u32 ddr_vref, phy_vref;
1930 u32 min_ddr_vref = 0, min_phy_vref = 0;
1931 u32 max_ddr_vref = 0, max_phy_vref = 0;
1932
1933 ast_moutdwm(ast, 0x1E6E0004, 0x00000313);
1934 ast_moutdwm(ast, 0x1E6E0010, ddr_table[REGIDX_010]);
1935 ast_moutdwm(ast, 0x1E6E0014, ddr_table[REGIDX_014]);
1936 ast_moutdwm(ast, 0x1E6E0018, ddr_table[REGIDX_018]);
1937 ast_moutdwm(ast, 0x1E6E0020, ddr_table[REGIDX_020]); /* MODEREG4/6 */
1938 ast_moutdwm(ast, 0x1E6E0024, ddr_table[REGIDX_024]); /* MODEREG5 */
1939 ast_moutdwm(ast, 0x1E6E002C, ddr_table[REGIDX_02C] | 0x100); /* MODEREG0/2 */
1940 ast_moutdwm(ast, 0x1E6E0030, ddr_table[REGIDX_030]); /* MODEREG1/3 */
1941
1942 /* DDR PHY Setting */
1943 ast_moutdwm(ast, 0x1E6E0200, 0x42492AAE);
1944 ast_moutdwm(ast, 0x1E6E0204, 0x09002000);
1945 ast_moutdwm(ast, 0x1E6E020C, 0x55E00B0B);
1946 ast_moutdwm(ast, 0x1E6E0210, 0x20000000);
1947 ast_moutdwm(ast, 0x1E6E0214, ddr_table[REGIDX_214]);
1948 ast_moutdwm(ast, 0x1E6E02E0, ddr_table[REGIDX_2E0]);
1949 ast_moutdwm(ast, 0x1E6E02E4, ddr_table[REGIDX_2E4]);
1950 ast_moutdwm(ast, 0x1E6E02E8, ddr_table[REGIDX_2E8]);
1951 ast_moutdwm(ast, 0x1E6E02EC, ddr_table[REGIDX_2EC]);
1952 ast_moutdwm(ast, 0x1E6E02F0, ddr_table[REGIDX_2F0]);
1953 ast_moutdwm(ast, 0x1E6E02F4, ddr_table[REGIDX_2F4]);
1954 ast_moutdwm(ast, 0x1E6E02F8, ddr_table[REGIDX_2F8]);
1955 ast_moutdwm(ast, 0x1E6E0290, 0x00100008);
1956 ast_moutdwm(ast, 0x1E6E02C4, 0x3C183C3C);
1957 ast_moutdwm(ast, 0x1E6E02C8, 0x00631E0E);
1958
1959 /* Controller Setting */
1960 ast_moutdwm(ast, 0x1E6E0034, 0x0001A991);
1961
1962 /* Train PHY Vref first */
1963 pass = 0;
1964
1965 for (retrycnt = 0; retrycnt < 4 && pass == 0; retrycnt++) {
1966 max_phy_vref = 0x0;
1967 pass = 0;
1968 ast_moutdwm(ast, 0x1E6E02C0, 0x00001C06);
1969 for (phy_vref = 0x40; phy_vref < 0x80; phy_vref++) {
1970 ast_moutdwm(ast, 0x1E6E000C, 0x00000000);
1971 ast_moutdwm(ast, 0x1E6E0060, 0x00000000);
1972 ast_moutdwm(ast, 0x1E6E02CC, phy_vref | (phy_vref << 8));
1973 /* Fire DFI Init */
1974 ddr_phy_init_2500(ast);
1975 ast_moutdwm(ast, 0x1E6E000C, 0x00005C01);
1976 if (cbr_test_2500(ast)) {
1977 pass++;
1978 data = ast_mindwm(ast, 0x1E6E03D0);
1979 data2 = data >> 8;
1980 data = data & 0xff;
1981 if (data > data2)
1982 data = data2;
1983 if (max_phy_vref < data) {
1984 max_phy_vref = data;
1985 min_phy_vref = phy_vref;
1986 }
1987 } else if (pass > 0)
1988 break;
1989 }
1990 }
1991 ast_moutdwm(ast, 0x1E6E02CC, min_phy_vref | (min_phy_vref << 8));
1992
1993 /* Train DDR Vref next */
1994 pass = 0;
1995
1996 for (retrycnt = 0; retrycnt < 4 && pass == 0; retrycnt++) {
1997 min_ddr_vref = 0xFF;
1998 max_ddr_vref = 0x0;
1999 pass = 0;
2000 for (ddr_vref = 0x00; ddr_vref < 0x40; ddr_vref++) {
2001 ast_moutdwm(ast, 0x1E6E000C, 0x00000000);
2002 ast_moutdwm(ast, 0x1E6E0060, 0x00000000);
2003 ast_moutdwm(ast, 0x1E6E02C0, 0x00000006 | (ddr_vref << 8));
2004 /* Fire DFI Init */
2005 ddr_phy_init_2500(ast);
2006 ast_moutdwm(ast, 0x1E6E000C, 0x00005C01);
2007 if (cbr_test_2500(ast)) {
2008 pass++;
2009 if (min_ddr_vref > ddr_vref)
2010 min_ddr_vref = ddr_vref;
2011 if (max_ddr_vref < ddr_vref)
2012 max_ddr_vref = ddr_vref;
2013 } else if (pass != 0)
2014 break;
2015 }
2016 }
2017
2018 ast_moutdwm(ast, 0x1E6E000C, 0x00000000);
2019 ast_moutdwm(ast, 0x1E6E0060, 0x00000000);
2020 ddr_vref = (min_ddr_vref + max_ddr_vref + 1) >> 1;
2021 ast_moutdwm(ast, 0x1E6E02C0, 0x00000006 | (ddr_vref << 8));
2022
2023 /* Wait DDR PHY init done */
2024 ddr_phy_init_2500(ast);
2025
2026 ast_moutdwm(ast, 0x1E6E0120, ddr_table[REGIDX_PLL]);
2027 ast_moutdwm(ast, 0x1E6E000C, 0x42AA5C81);
2028 ast_moutdwm(ast, 0x1E6E0034, 0x0001AF93);
2029
2030 check_dram_size_2500(ast, ddr_table[REGIDX_RFC]);
2031 enable_cache_2500(ast);
2032 ast_moutdwm(ast, 0x1E6E001C, 0x00000008);
2033 ast_moutdwm(ast, 0x1E6E0038, 0xFFFFFF00);
2034 }
2035
2036 static bool ast_dram_init_2500(struct ast_private *ast)
2037 {
2038 u32 data;
2039 u32 max_tries = 5;
2040
2041 do {
2042 if (max_tries-- == 0)
2043 return false;
2044 set_mpll_2500(ast);
2045 reset_mmc_2500(ast);
2046 ddr_init_common_2500(ast);
2047
2048 data = ast_mindwm(ast, 0x1E6E2070);
2049 if (data & 0x01000000)
2050 ddr4_init_2500(ast, ast2500_ddr4_1600_timing_table);
2051 else
2052 ddr3_init_2500(ast, ast2500_ddr3_1600_timing_table);
2053 } while (!ddr_test_2500(ast));
2054
2055 ast_moutdwm(ast, 0x1E6E2040, ast_mindwm(ast, 0x1E6E2040) | 0x41);
2056
2057 /* Patch code */
2058 data = ast_mindwm(ast, 0x1E6E200C) & 0xF9FFFFFF;
2059 ast_moutdwm(ast, 0x1E6E200C, data | 0x10000000);
2060
2061 return true;
2062 }
2063
2064 void ast_post_chip_2500(struct drm_device *dev)
2065 {
2066 struct ast_private *ast = dev->dev_private;
2067 u32 temp;
2068 u8 reg;
2069
2070 reg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
2071 if ((reg & 0x80) == 0) {/* vga only */
2072 /* Clear bus lock condition */
2073 ast_moutdwm(ast, 0x1e600000, 0xAEED1A03);
2074 ast_moutdwm(ast, 0x1e600084, 0x00010000);
2075 ast_moutdwm(ast, 0x1e600088, 0x00000000);
2076 ast_moutdwm(ast, 0x1e6e2000, 0x1688A8A8);
2077 ast_write32(ast, 0xf004, 0x1e6e0000);
2078 ast_write32(ast, 0xf000, 0x1);
2079 ast_write32(ast, 0x12000, 0x1688a8a8);
2080 while (ast_read32(ast, 0x12000) != 0x1)
2081 ;
2082
2083 ast_write32(ast, 0x10000, 0xfc600309);
2084 while (ast_read32(ast, 0x10000) != 0x1)
2085 ;
2086
2087 /* Slow down CPU/AHB CLK in VGA only mode */
2088 temp = ast_read32(ast, 0x12008);
2089 temp |= 0x73;
2090 ast_write32(ast, 0x12008, temp);
2091
2092 /* Reset USB port to patch USB unknown device issue */
2093 ast_moutdwm(ast, 0x1e6e2090, 0x20000000);
2094 temp = ast_mindwm(ast, 0x1e6e2094);
2095 temp |= 0x00004000;
2096 ast_moutdwm(ast, 0x1e6e2094, temp);
2097 temp = ast_mindwm(ast, 0x1e6e2070);
2098 if (temp & 0x00800000) {
2099 ast_moutdwm(ast, 0x1e6e207c, 0x00800000);
2100 mdelay(100);
2101 ast_moutdwm(ast, 0x1e6e2070, 0x00800000);
2102 }
2103
2104 if (!ast_dram_init_2500(ast))
2105 printk(BIOS_ERR, "AST: DRAM init failed !\n");
2106
2107 temp = ast_mindwm(ast, 0x1e6e2040);
2108 ast_moutdwm(ast, 0x1e6e2040, temp | 0x40);
2109 }
2110
2111 /* wait ready */
2112 do {
2113 reg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
2114 } while ((reg & 0x40) == 0);
2115 }