2 * Qualcomm/Atheros WiSoCs DRAM related
3 * functions for WiSoC families:
6 * - Qualcomm/Atheros QCA953x
7 * - Qualcomm/Atheros QCA955x
8 * - Qualcomm/Atheros QCA956x
10 * Copyright (C) 2016 Piotr Dymacz <piotr@dymacz.pl>
11 * Copyright (C) 2015-2016 Wills Wang <wills.wang@live.com>
12 * Copyright (C) 2014 Qualcomm Atheros, Inc.
13 * Copyright (C) 2008-2010 Atheros Communications Inc.
15 * SPDX-License-Identifier: GPL-2.0
20 #include <asm/addrspace.h>
21 #include <soc/qca_soc_common.h>
22 #include <soc/qca_dram.h>
24 #define QCA_DDR_SIZE_INCREMENT (8 * 1024 * 1024)
27 * Returns size (in bytes) of the DRAM memory
29 * DDR wraps around, write a pattern to 0x00000000
30 * at 8M, 16M, 32M etc. and check when it gets overwritten
32 u32 qca_dram_size(void)
40 #define max_i (QCA_DRAM_MAX_SIZE_VAL / QCA_DDR_SIZE_INCREMENT)
42 for (i = 1; (i < max_i); i++) {
43 *(p + i * QCA_DDR_SIZE_INCREMENT) = (u8)i;
50 #ifndef CONFIG_SKIP_LOWLEVEL_INIT
52 (i * QCA_DDR_SIZE_INCREMENT) : QCA_DRAM_MAX_SIZE_VAL);
56 * something is wrong with relocation,
57 * need to fix it for boards with > 32M of RAM
59 * For now just return 1 MB smaller size
62 (i * QCA_DDR_SIZE_INCREMENT) : QCA_DRAM_MAX_SIZE_VAL) - 1024 * 1024;
67 * Return memory type value from BOOT_STRAP register
69 u32 qca_dram_type(void)
71 #if defined(CONFIG_BOARD_DRAM_TYPE_SDR)
72 #error "SDRAM is not supported!"
73 return RAM_MEMORY_TYPE_SDR;
74 #elif defined(CONFIG_BOARD_DRAM_TYPE_DDR1)
75 return RAM_MEMORY_TYPE_DDR1;
76 #elif defined(CONFIG_BOARD_DRAM_TYPE_DDR2)
77 return RAM_MEMORY_TYPE_DDR2;
81 dram_type = ((qca_soc_reg_read(QCA_RST_BOOTSTRAP_REG)
82 & QCA_RST_BOOTSTRAP_MEM_TYPE_MASK)
83 >> QCA_RST_BOOTSTRAP_MEM_TYPE_SHIFT);
86 case QCA_RST_BOOTSTRAP_MEM_TYPE_SDR_VAL:
87 dram_type = RAM_MEMORY_TYPE_SDR;
89 case QCA_RST_BOOTSTRAP_MEM_TYPE_DDR1_VAL:
90 dram_type = RAM_MEMORY_TYPE_DDR1;
92 case QCA_RST_BOOTSTRAP_MEM_TYPE_DDR2_VAL:
93 dram_type = RAM_MEMORY_TYPE_DDR2;
96 dram_type = RAM_MEMORY_TYPE_UNKNOWN;
105 * Returns DDR width (16 or 32)
107 u32 qca_dram_ddr_width(void)
109 #ifndef CONFIG_BOARD_DRAM_DDR_WIDTH
110 #if (SOC_TYPE & QCA_AR933X_SOC)
113 if (qca_soc_reg_read(QCA_RST_BOOTSTRAP_REG)
114 & QCA_RST_BOOTSTRAP_DDR_WIDTH_32_MASK)
120 return CONFIG_BOARD_DRAM_DDR_WIDTH;
125 * Returns CAS latency, based on setting in DDR_CONFIG register
127 u32 qca_dram_cas_lat(void)
129 #ifndef CONFIG_BOARD_DRAM_CAS_LATENCY
132 reg = (qca_soc_reg_read(QCA_DDR_CFG_REG) & QCA_DDR_CFG_CAS_3LSB_MASK)
133 >> QCA_DDR_CFG_CAS_3LSB_SHIFT;
135 if (qca_soc_reg_read(QCA_DDR_CFG_REG) & QCA_DDR_CFG_CAS_MSB_MASK)
138 /* CAS_LATENCY value in DDR_CONFIG register == 2 * MEM_CAS */
141 return CONFIG_BOARD_DRAM_CAS_LATENCY;
146 * Returns tRCD latency
148 u32 qca_dram_trcd_lat(void)
152 reg = (qca_soc_reg_read(QCA_DDR_CFG_REG) & QCA_DDR_CFG_TRCD_MASK)
153 >> QCA_DDR_CFG_TRCD_SHIFT;
159 * Returns tRP latency
161 u32 qca_dram_trp_lat(void)
165 reg = (qca_soc_reg_read(QCA_DDR_CFG_REG) & QCA_DDR_CFG_TRP_MASK)
166 >> QCA_DDR_CFG_TRP_SHIFT;
172 * Returns tRAS latency
174 u32 qca_dram_tras_lat(void)
178 reg = (qca_soc_reg_read(QCA_DDR_CFG_REG) & QCA_DDR_CFG_TRAS_MASK)
179 >> QCA_DDR_CFG_TRAS_SHIFT;
185 * ===============================================
186 * DQS delay tap controller tune related functions
187 * ===============================================
189 #define DQS_DELAY_TAP_DEFAULT_VAL 8
191 #if (SOC_TYPE & QCA_AR933X_SOC) |\
192 (SOC_TYPE & QCA_AR934X_SOC)
193 #define DQS_DELAY_TAP_MAX_VAL 62
195 #define DQS_DELAY_TAP_MAX_VAL 63
199 * Setup DQS_{0,1,2,3} delay tap control register/s
201 static void qca_ddr_tap_save(u32 tap, u32 ddr_width)
203 #if (SOC_TYPE & QCA_AR933X_SOC) |\
204 (SOC_TYPE & QCA_AR934X_SOC)
207 /* It seems that AR93xx SoCs have two delay chains */
208 if (tap > (DQS_DELAY_TAP_MAX_VAL / 2)) {
209 tap_h = tap - (DQS_DELAY_TAP_MAX_VAL / 2);
210 tap = tap & QCA_DDR_TAP_CTRL_TAP_L_MASK;
211 tap = tap | (tap_h << QCA_DDR_TAP_CTRL_TAP_H_SHIFT);
215 qca_soc_reg_write(QCA_DDR_TAP_CTRL_0_REG, tap);
216 qca_soc_reg_write(QCA_DDR_TAP_CTRL_1_REG, tap);
218 /* Setup DQS2 and DQS3 only for 32-bit DDR interface width */
219 if (ddr_width == 32) {
220 qca_soc_reg_write(QCA_DDR_TAP_CTRL_2_REG, tap);
221 qca_soc_reg_write(QCA_DDR_TAP_CTRL_3_REG, tap);
226 * Only for AR933x we will use different code
227 * for delay tap controller tune as it seems
228 * that this SoC doesn't have DDR BIST.
230 * Below function is universal, so it should
231 * work also for other QC/A WiSoCs and give
232 * same (or very similar) results. The only
233 * difference is that the DDR BIST based
234 * version seems to be much faster.
236 #if (SOC_TYPE & QCA_AR933X_SOC)
238 #define DQS_DELAY_TAP_PATTERN_OFFSET 0x2000
239 #define DQS_DELAY_TAP_PATTERN_SIZE 0x1000
240 #define DQS_DELAY_TAP_TEST_LOOPS 2
243 * Prepare pattern for further tests
245 static inline void qca_ddr_tap_patt(void)
250 /* Prepare 4M (256 x 4 x 4 bytes) pattern */
251 addr = (void *)KSEG1ADDR(DQS_DELAY_TAP_PATTERN_OFFSET);
253 for (i = 0; i < 256; i++) {
256 for (j = 0; j < 8; j++) {
273 * This function is a modified C version of the original
274 * ar933x_ddr_tap_init() function, written in asm,
275 * included in Atheros (Q)SDK code.
277 * Below is a modified version, partially based on:
278 * https://patchwork.ozlabs.org/patch/569046/
280 static void qca_ddr_tap_tune(u32 ddr_width)
282 u32 *addr, *addr_k0, *addr_k1;
283 u32 tap, tap_hi, tap_lo;
293 addr = (void *)KSEG1ADDR(DQS_DELAY_TAP_PATTERN_OFFSET
294 + DQS_DELAY_TAP_PATTERN_SIZE);
297 * Idea here is to test all possible tap values, one by one,
298 * starting from the lowest. We are looking for a range within
299 * the written and read back data is the same. We assume here
300 * that the valid tap range is continuous.
302 * From hardware POV, delay tap controller is used to adjust
305 for (tap = 0; tap <= DQS_DELAY_TAP_MAX_VAL; tap++) {
306 qca_ddr_tap_save(tap, ddr_width);
310 for (i = 0; i < DQS_DELAY_TAP_TEST_LOOPS; i++) {
311 addr_k1 = (void *)KSEG1ADDR(DQS_DELAY_TAP_PATTERN_OFFSET);
312 addr_k0 = (void *)KSEG0ADDR(DQS_DELAY_TAP_PATTERN_OFFSET);
314 while (addr_k1 < addr) {
315 if (*addr_k1++ != *addr_k0++) {
342 /* Calculate final tap value (rounded up average) */
344 tap = (tap_hi + tap_lo + 1) / 2;
346 tap = DQS_DELAY_TAP_DEFAULT_VAL;
349 qca_ddr_tap_save(tap, ddr_width);
352 #else /* SOC_TYPE & QCA_AR933X_SOC */
354 #define DQS_DELAY_TAP_TEST_LOOPS 8
357 * Unknown magic values and registers from Atheros (Q)SDK.
359 * It looks like some test patterns and masks setup,
360 * but it's not confirmed. Used here values are
361 * different, but were tested on real hardware.
363 static inline void qca_ddr_tap_bist_init(void)
365 qca_soc_reg_write(QCA_DDR_PERF_COMP_AHB_GE0_0_REG, 0xAAAAAAAA);
366 qca_soc_reg_write(QCA_DDR_PERF_MASK_AHB_GE0_0_REG, 0xAAAAAAAA);
368 qca_soc_reg_write(QCA_DDR_PERF_COMP_AHB_GE0_1_REG, 0x55555555);
369 qca_soc_reg_write(QCA_DDR_PERF_MASK_AHB_GE0_1_REG, 0x55555555);
371 qca_soc_reg_write(QCA_DDR_PERF_COMP_AHB_GE1_0_REG, 0xAAAAAAAA);
372 qca_soc_reg_write(QCA_DDR_PERF_MASK_AHB_GE1_0_REG, 0xAAAAAAAA);
374 qca_soc_reg_write(QCA_DDR_PERF_COMP_AHB_GE1_1_REG, 0x55555555);
375 qca_soc_reg_write(QCA_DDR_PERF_MASK_AHB_GE1_1_REG, 0x55555555);
379 * This function is a modified C version of the original
380 * ath_ddr_tap_cal() function, written in asm,
381 * included in Atheros (Q)SDK code.
383 * It seems that newer QC/A WiSoCs have some kind of
384 * built-in self-test (BIST) for DDR controller, but
385 * none of the used registers or their values are
386 * described in datasheets, so for now, we will just
387 * use them as in original code.
389 * Below is a modified version, partially based on:
390 * https://patchwork.ozlabs.org/patch/569047/
392 static void qca_ddr_tap_tune(u32 ddr_width)
394 u32 tap, tap_hi, tap_lo;
395 u32 fail, got_lo, reg;
401 /* How many test loops per tested tap value */
402 qca_soc_reg_write(QCA_DDR_PERF_COMP_ADDR_1_REG,
403 (DQS_DELAY_TAP_TEST_LOOPS
404 << QCA_DDR_PERF_COMP_ADDR_1_TEST_CNT_SHIFT));
407 * Unknown magic value, original comment:
408 * "4 Row Address Bits, 4 Column Address Bits, 2 BA bits"
410 qca_soc_reg_write(QCA_DDR_PERF_MASK_ADDR_0_REG, 0xFA5DE83F);
413 * Test all possible tap values, try to find working range
414 * (minimum and maximum delays) and use average value
416 for (tap = 0; tap <= DQS_DELAY_TAP_MAX_VAL; tap++) {
417 qca_ddr_tap_save(tap, ddr_width);
419 qca_ddr_tap_bist_init();
421 /* Enable BIST test and wait for finish */
422 qca_soc_reg_write(QCA_DDR_BIST_REG, QCA_DDR_BIST_TEST_EN_MASK);
425 reg = qca_soc_reg_read(QCA_DDR_BIST_STATUS_REG);
426 } while (!(reg & QCA_DDR_BIST_STATUS_DONE_MASK));
428 /* Disable BIST test */
429 qca_soc_reg_write(QCA_DDR_BIST_REG, 0);
431 /* Check how many tests failed */
432 fail = (reg & QCA_DDR_BIST_STATUS_FAIL_CNT_MASK)
433 >> QCA_DDR_BIST_STATUS_FAIL_CNT_SHIFT;
452 /* Calculate final tap value (rounded up average) */
454 tap = (tap_hi + tap_lo + 1) / 2;
456 tap = DQS_DELAY_TAP_DEFAULT_VAL;
459 qca_ddr_tap_save(tap, ddr_width);
462 #endif /* SOC_TYPE & QCA_AR933X_SOC */
465 * ===============================================
466 * DDR controller initialization related functions
467 * ===============================================
471 * Below defines are "safe" DDR1/DDR2 timing parameters.
472 * They should work for most chips, but not for all.
474 * For different values, user can define target value
475 * of all memory controller related registers.
478 #define DDRx_tMRD_ns 10
479 #define DDRx_tRAS_ns 40
480 #define DDRx_tRCD_ns 15
481 #define DDRx_tRP_ns 15
482 #define DDRx_tRRD_ns 10
483 #define DDRx_tWR_ns 15
484 #define DDRx_tWTR_ns 10
486 #define DDR1_tRFC_ns 75
487 #define DDR2_tRFC_ns 120
489 #define DDR2_tFAW_ns 50
490 #define DDR2_tWL_ns 5
492 #define DDR_addit_lat 0
493 #define DDR_burst_len 8
495 /* All above values are safe for clocks not lower than below values */
496 #define DDR1_timing_clk_max 400
497 #define DDR2_timing_clk_max 533
499 /* Maximum timing values, based on register fields sizes */
500 #define MAX_tFAW BITS(0, 6)
501 #define MAX_tMRD BITS(0, 4)
502 #define MAX_tRAS BITS(0, 5)
503 #define MAX_tRCD BITS(0, 4)
504 #define MAX_tRFC BITS(0, 6)
505 #define MAX_tRP BITS(0, 4)
506 #define MAX_tRRD BITS(0, 4)
507 #define MAX_tRTP BITS(0, 4)
508 #define MAX_tRTW BITS(0, 5)
509 #define MAX_tWL BITS(0, 4)
510 #define MAX_tWR BITS(0, 4)
511 #define MAX_tWTR BITS(0, 5)
514 * Setup DDR_CONFIG register
516 static inline void qca_dram_set_ddr_cfg(u32 mem_cas,
520 #ifndef CONFIG_QCA_DDR_CFG_REG_VAL
524 reg = qca_soc_reg_read(QCA_DDR_CFG_REG);
526 /* Always use page close policy */
527 reg = reg | QCA_DDR_CFG_PAGE_CLOSE_MASK;
529 /* CAS should be (2 * MEM_CAS) or (2 * MEM_CAS) + 1/2/3 */
531 tmp = (tmp << QCA_DDR_CFG_CAS_3LSB_SHIFT) & QCA_DDR_CFG_CAS_3LSB_MASK;
533 tmp = tmp | QCA_DDR_CFG_CAS_MSB_MASK;
536 reg = reg & ~QCA_DDR_CFG_CAS_3LSB_MASK;
540 * Calculate rest of timing related values,
541 * always round up to closest integer
545 tmp = ((DDRx_tMRD_ns * ddr_clk) + 500) / 1000;
549 tmp = (tmp << QCA_DDR_CFG_TMRD_SHIFT) & QCA_DDR_CFG_TMRD_MASK;
550 reg = reg & ~QCA_DDR_CFG_TMRD_MASK;
554 if (mem_type == RAM_MEMORY_TYPE_DDR2) {
555 tmp = ((DDR2_tRFC_ns * ddr_clk) + 500) / 1000;
557 tmp = ((DDR1_tRFC_ns * ddr_clk) + 500) / 1000;
563 tmp = (tmp << QCA_DDR_CFG_TRFC_SHIFT) & QCA_DDR_CFG_TRFC_MASK;
564 reg = reg & ~QCA_DDR_CFG_TRFC_MASK;
568 tmp = ((DDRx_tRRD_ns * ddr_clk) + 500) / 1000;
572 tmp = (tmp << QCA_DDR_CFG_TRRD_SHIFT) & QCA_DDR_CFG_TRRD_MASK;
573 reg = reg & ~QCA_DDR_CFG_TRRD_MASK;
577 tmp = ((DDRx_tRP_ns * ddr_clk) + 500) / 1000;
581 tmp = (tmp << QCA_DDR_CFG_TRP_SHIFT) & QCA_DDR_CFG_TRP_MASK;
582 reg = reg & ~QCA_DDR_CFG_TRP_MASK;
586 tmp = ((DDRx_tRCD_ns * ddr_clk) + 500) / 1000;
590 tmp = (tmp << QCA_DDR_CFG_TRCD_SHIFT) & QCA_DDR_CFG_TRCD_MASK;
591 reg = reg & ~QCA_DDR_CFG_TRCD_MASK;
595 tmp = ((DDRx_tRAS_ns * ddr_clk) + 500) / 1000;
599 tmp = (tmp << QCA_DDR_CFG_TRAS_SHIFT) & QCA_DDR_CFG_TRAS_MASK;
600 reg = reg & ~QCA_DDR_CFG_TRAS_MASK;
603 qca_soc_reg_write(QCA_DDR_CFG_REG, reg);
605 qca_soc_reg_write(QCA_DDR_CFG_REG, CONFIG_QCA_DDR_CFG_REG_VAL);
610 * Setup DDR_CONFIG2 register
612 static inline void qca_dram_set_ddr_cfg2(u32 mem_cas,
617 #ifndef CONFIG_QCA_DDR_CFG2_REG_VAL
621 reg = qca_soc_reg_read(QCA_DDR_CFG2_REG);
624 reg = reg | QCA_DDR_CFG2_CKE_MASK;
626 /* Gate open latency = 2 * MEM_CAS */
628 tmp = (tmp << QCA_DDR_CFG2_GATE_OPEN_LATENCY_SHIFT)
629 & QCA_DDR_CFG2_GATE_OPEN_LATENCY_MASK;
630 reg = reg & ~QCA_DDR_CFG2_GATE_OPEN_LATENCY_MASK;
634 if (mem_type == RAM_MEMORY_TYPE_DDR2) {
635 /* tWTR = 2 * WL + BL + 2 * max(tWTR/tCK, 2) */
636 tmp = 2 * (mem_cas + DDR_addit_lat - 1) + DDR_burst_len + 4;
641 /* tWTR = 2 + BL + (2 * tWTR/tCK) */
642 tmp = 2 + DDR_burst_len
643 + (((DDRx_tWTR_ns * ddr_clk) + 500) / 1000);
649 tmp = (tmp << QCA_DDR_CFG2_TWTR_SHIFT) & QCA_DDR_CFG2_TWTR_MASK;
650 reg = reg & ~QCA_DDR_CFG2_TWTR_MASK;
654 if (ddr_width == 32) {
660 tmp = (tmp << QCA_DDR_CFG2_TRTP_SHIFT) & QCA_DDR_CFG2_TRTP_MASK;
661 reg = reg & ~QCA_DDR_CFG2_TRTP_MASK;
665 if (mem_type == RAM_MEMORY_TYPE_DDR2) {
666 /* tRTW = 2 * (RL + BL/2 + 1 -WL), RL = CL + AL, WL = RL - 1 */
667 tmp = DDR_burst_len + 4;
669 /* tRTW = 2 * (CL + BL/2) */
670 tmp = DDR_burst_len + (2 * mem_cas);
676 tmp = (tmp << QCA_DDR_CFG2_TRTW_SHIFT) & QCA_DDR_CFG2_TRTW_MASK;
677 reg = reg & ~QCA_DDR_CFG2_TRTW_MASK;
681 tmp = ((DDRx_tWR_ns * ddr_clk) + 500) / 1000;
685 tmp = (tmp << QCA_DDR_CFG2_TWR_SHIFT) & QCA_DDR_CFG2_TWR_MASK;
686 reg = reg & ~QCA_DDR_CFG2_TWR_MASK;
689 /* Always use burst length = 8 and type: sequential */
690 tmp = (DDR_burst_len << QCA_DDR_CFG2_BURST_LEN_SHIFT)
691 & QCA_DDR_CFG2_BURST_LEN_MASK;
692 reg = reg & ~(QCA_DDR_CFG2_BURST_LEN_MASK
693 | QCA_DDR_CFG2_BURST_TYPE_MASK);
696 qca_soc_reg_write(QCA_DDR_CFG2_REG, reg);
698 qca_soc_reg_write(QCA_DDR_CFG2_REG, CONFIG_QCA_DDR_CFG2_REG_VAL);
703 * Setup DDR2_CONFIG register (only for DDR2)
705 static inline void qca_dram_set_ddr2_cfg(u32 mem_cas,
708 #ifndef CONFIG_QCA_DDR_DDR2_CFG_REG_VAL
712 reg = qca_soc_reg_read(QCA_DDR_DDR2_CFG_REG);
715 reg = reg | QCA_DDR_DDR2_CFG_DDR2_EN_MASK;
718 tmp = ((DDR2_tFAW_ns * ddr_clk) + 500) / 1000;
722 tmp = (tmp << QCA_DDR_DDR2_CFG_DDR2_TFAW_SHIFT)
723 & QCA_DDR_DDR2_CFG_DDR2_TFAW_MASK;
724 reg = reg & ~QCA_DDR_DDR2_CFG_DDR2_TFAW_MASK;
728 tmp = (2 * mem_cas) - 3;
730 /* For some reason, odd value doesn't work on AR933x (FIXME) */
731 #if (SOC_TYPE & QCA_AR933X_SOC)
736 tmp = (tmp << QCA_DDR_DDR2_CFG_DDR2_TWL_SHIFT)
737 & QCA_DDR_DDR2_CFG_DDR2_TWL_MASK;
738 reg = reg & ~QCA_DDR_DDR2_CFG_DDR2_TWL_MASK;
741 qca_soc_reg_write(QCA_DDR_DDR2_CFG_REG, reg);
743 qca_soc_reg_write(QCA_DDR_DDR2_CFG_REG,
744 CONFIG_QCA_DDR_DDR2_CFG_REG_VAL);
749 * Enables DDR refresh and sets
750 * refresh period based on XTAL
752 static inline void qca_dram_set_en_refresh(void)
755 * Enable DDR refresh and setup refresh period:
756 * 1. We assume 7.8 us maximum average period refresh interval
757 * 2. 7.8 us ~= 0.1282 MHz
758 * 3. For 25 MHz XTAL: (25 / 0.1282) ~= 195
759 * 4. For 40 MHz XTAL: (40 / 0.1282) ~= 312
761 if (qca_xtal_is_40mhz()) {
762 qca_soc_reg_write(QCA_DDR_REFRESH_REG,
763 QCA_DDR_REFRESH_EN_MASK
764 | (312 << QCA_DDR_REFRESH_PERIOD_SHIFT));
766 qca_soc_reg_write(QCA_DDR_REFRESH_REG,
767 QCA_DDR_REFRESH_EN_MASK
768 | (195 << QCA_DDR_REFRESH_PERIOD_SHIFT));
773 * Initial DRAM configuration
775 void qca_dram_init(void)
777 u32 ahb_clk, cpu_clk, ddr_clk, mem_type, tmp_clk;
778 u32 cas_lat, ddr_width, reg, tmp, wr_recovery;
780 mem_type = qca_dram_type();
782 qca_sys_clocks(&cpu_clk, &ddr_clk, &ahb_clk, NULL, NULL);
783 cpu_clk = cpu_clk / 1000000;
784 ddr_clk = ddr_clk / 1000000;
785 ahb_clk = ahb_clk / 1000000;
787 /* Set CAS based on clock, but allow to set static value */
788 #ifndef CONFIG_BOARD_DRAM_CAS_LATENCY
789 if (mem_type == RAM_MEMORY_TYPE_DDR1) {
790 if (ddr_clk <= 266) {
796 if (ddr_clk <= 400) {
798 } else if (ddr_clk <= 533) {
800 } else if (ddr_clk <= 666) {
802 } else if (ddr_clk <= 800) {
809 cas_lat = CONFIG_BOARD_DRAM_CAS_LATENCY;
812 #if (SOC_TYPE & QCA_AR933X_SOC)
813 /* AR933x supports only 16-bit memory */
815 qca_soc_reg_write(QCA_DDR_RD_DATA_THIS_CYCLE_REG, 0xFF);
817 /* For other WiSoCs we can determine DDR width, based on bootstrap */
818 ddr_width = qca_dram_ddr_width();
820 if (ddr_width == 32) {
821 /* For 32-bit clear HALF_WIDTH and set VEC = 0xFF */
822 qca_soc_reg_read_clear(QCA_DDR_CTRL_CFG_REG,
823 QCA_DDR_CTRL_CFG_HALF_WIDTH_MASK);
825 qca_soc_reg_write(QCA_DDR_RD_DATA_THIS_CYCLE_REG, 0xFF);
827 qca_soc_reg_read_set(QCA_DDR_CTRL_CFG_REG,
828 QCA_DDR_CTRL_CFG_HALF_WIDTH_MASK);
830 qca_soc_reg_write(QCA_DDR_RD_DATA_THIS_CYCLE_REG, 0xFFFF);
833 /* If DDR_CLK < 2 * AHB_CLK, set DDR FSM wait control to 0xA24 */
834 if (ddr_clk < (2 * ahb_clk))
835 qca_soc_reg_write(QCA_DDR_FSM_WAIT_CTRL_REG, 0xA24);
837 /* If CPU clock < AHB clock, set SRAM REQ ACK */
838 if (cpu_clk < ahb_clk)
839 qca_soc_reg_read_set(QCA_DDR_CTRL_CFG_REG,
840 QCA_DDR_CTRL_CFG_SRAM_REQ_ACK_MASK);
842 qca_soc_reg_read_clear(QCA_DDR_CTRL_CFG_REG,
843 QCA_DDR_CTRL_CFG_SRAM_REQ_ACK_MASK);
847 * CPU/DDR sync mode only when we don't use
848 * fractional multipliers in PLL/clocks config
852 #if (SOC_TYPE & QCA_AR933X_SOC)
853 reg = qca_soc_reg_read(QCA_PLL_CPU_PLL_DITHER_FRAC_REG);
854 reg = (reg & QCA_PLL_CPU_PLL_DITHER_FRAC_NFRAC_MIN_MASK)
855 >> QCA_PLL_CPU_PLL_DITHER_FRAC_NFRAC_MIN_SHIFT;
860 reg = qca_soc_reg_read(QCA_PLL_CPU_PLL_DITHER_REG);
861 reg = (reg & QCA_PLL_CPU_PLL_DITHER_NFRAC_MIN_MASK)
862 >> QCA_PLL_CPU_PLL_DITHER_NFRAC_MIN_SHIFT;
867 reg = qca_soc_reg_read(QCA_PLL_DDR_PLL_DITHER_REG);
868 reg = (reg & QCA_PLL_DDR_PLL_DITHER_NFRAC_MIN_MASK)
869 >> QCA_PLL_DDR_PLL_DITHER_NFRAC_MIN_SHIFT;
875 if (!tmp && (cpu_clk == ddr_clk)) {
876 #if (SOC_TYPE & QCA_AR933X_SOC)
877 qca_soc_reg_read_set(QCA_DDR_TAP_CTRL_3_REG, (1 << 8));
879 qca_soc_reg_read_set(QCA_DDR_CTRL_CFG_REG,
880 QCA_DDR_CTRL_CFG_CPU_DDR_SYNC_MASK);
883 #if (SOC_TYPE & QCA_AR933X_SOC)
884 qca_soc_reg_read_clear(QCA_DDR_TAP_CTRL_3_REG, (1 << 8));
886 qca_soc_reg_read_clear(QCA_DDR_CTRL_CFG_REG,
887 QCA_DDR_CTRL_CFG_CPU_DDR_SYNC_MASK);
891 /* Check if clock is not too low for our "safe" timing values */
893 if (mem_type == RAM_MEMORY_TYPE_DDR1) {
894 if (tmp_clk < DDR1_timing_clk_max)
895 tmp_clk = DDR1_timing_clk_max;
897 if (tmp_clk < DDR2_timing_clk_max)
898 tmp_clk = DDR2_timing_clk_max;
902 #if (SOC_TYPE & QCA_AR933X_SOC)
903 if (mem_type == RAM_MEMORY_TYPE_DDR2)
904 qca_dram_set_ddr2_cfg(cas_lat, tmp_clk);
906 if (mem_type == RAM_MEMORY_TYPE_DDR2) {
907 qca_soc_reg_read_set(QCA_DDR_CTRL_CFG_REG,
908 QCA_DDR_CTRL_CFG_PAD_DDR2_SEL_MASK);
910 qca_dram_set_ddr2_cfg(cas_lat, tmp_clk);
912 qca_soc_reg_read_clear(QCA_DDR_CTRL_CFG_REG,
913 QCA_DDR_CTRL_CFG_PAD_DDR2_SEL_MASK);
917 /* Setup DDR timing related registers */
918 qca_dram_set_ddr_cfg(cas_lat, tmp_clk, mem_type);
919 qca_dram_set_ddr_cfg2(cas_lat, tmp_clk, mem_type, ddr_width);
922 qca_dram_force_preall();
924 if (mem_type == RAM_MEMORY_TYPE_DDR2) {
925 /* Setup target EMR2 and EMR3 */
926 qca_dram_set_emr2(_ddr_sdram_emr2_val(0, 0, 0));
927 qca_dram_set_emr3(0);
930 /* Enable and reset DLL */
931 qca_dram_set_emr(_ddr_sdram_emr_val(0, 1, 0, 0, 0, 0));
932 qca_dram_set_mr(_ddr_sdram_mr_val(0, 0, 1, 0));
934 /* Precharge all, 2x auto refresh */
935 qca_dram_force_preall();
937 qca_dram_force_aref();
938 qca_dram_force_aref();
940 if (mem_type == RAM_MEMORY_TYPE_DDR2) {
941 /* Setup target MR */
942 wr_recovery = ((DDRx_tWR_ns * tmp_clk) + 1000) / 2000;
943 qca_dram_set_mr(_ddr_sdram_mr_val(0, cas_lat, 0, wr_recovery));
945 /* OCD calibration, target EMR (nDQS disable, weak strength) */
947 _ddr_sdram_emr_val(0, 1, DDR_SDRAM_EMR_OCD_DEFAULT_VAL,
951 _ddr_sdram_emr_val(0, 1, DDR_SDRAM_EMR_OCD_EXIT_VAL,
954 /* Setup target MR */
955 qca_dram_set_mr(_ddr_sdram_mr_val(0, cas_lat, 0, 0));
958 /* Enable DDR refresh and setup refresh period */
959 qca_dram_set_en_refresh();
962 * At this point memory should be fully configured,
963 * so we can perform delay tap controller tune.
965 qca_ddr_tap_tune(ddr_width);