1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
296609ef4SPaul Walmsley /*
396609ef4SPaul Walmsley * SDRAM timing related functions for OMAP2xxx
496609ef4SPaul Walmsley *
5f2ab9977SPaul Walmsley * Copyright (C) 2005, 2008 Texas Instruments Inc.
6f2ab9977SPaul Walmsley * Copyright (C) 2005, 2008 Nokia Corporation
796609ef4SPaul Walmsley *
896609ef4SPaul Walmsley * Tony Lindgren <tony@atomide.com>
9f2ab9977SPaul Walmsley * Paul Walmsley
10f2ab9977SPaul Walmsley * Richard Woodruff <r-woodruff2@ti.com>
1196609ef4SPaul Walmsley */
1296609ef4SPaul Walmsley
1396609ef4SPaul Walmsley #include <linux/module.h>
1496609ef4SPaul Walmsley #include <linux/kernel.h>
1596609ef4SPaul Walmsley #include <linux/device.h>
1696609ef4SPaul Walmsley #include <linux/list.h>
1796609ef4SPaul Walmsley #include <linux/errno.h>
1896609ef4SPaul Walmsley #include <linux/delay.h>
1996609ef4SPaul Walmsley #include <linux/clk.h>
2096609ef4SPaul Walmsley #include <linux/io.h>
2196609ef4SPaul Walmsley
22dbc04161STony Lindgren #include "soc.h"
23ee0839c2STony Lindgren #include "iomap.h"
24ee0839c2STony Lindgren #include "common.h"
25139563adSPaul Walmsley #include "prm2xxx.h"
26c0bf3132SRussell King #include "clock.h"
2796609ef4SPaul Walmsley #include "sdrc.h"
28bf027ca1STony Lindgren #include "sram.h"
2996609ef4SPaul Walmsley
3096609ef4SPaul Walmsley /* Memory timing, DLL mode flags */
3196609ef4SPaul Walmsley #define M_DDR 1
3296609ef4SPaul Walmsley #define M_LOCK_CTRL (1 << 2)
3396609ef4SPaul Walmsley #define M_UNLOCK 0
3496609ef4SPaul Walmsley #define M_LOCK 1
3596609ef4SPaul Walmsley
3696609ef4SPaul Walmsley
3796609ef4SPaul Walmsley static struct memory_timings mem_timings;
3896609ef4SPaul Walmsley static u32 curr_perf_level = CORE_CLK_SRC_DPLL_X2;
3996609ef4SPaul Walmsley
omap2xxx_sdrc_get_slow_dll_ctrl(void)40f2ab9977SPaul Walmsley static u32 omap2xxx_sdrc_get_slow_dll_ctrl(void)
4196609ef4SPaul Walmsley {
4296609ef4SPaul Walmsley return mem_timings.slow_dll_ctrl;
4396609ef4SPaul Walmsley }
4496609ef4SPaul Walmsley
omap2xxx_sdrc_get_fast_dll_ctrl(void)45f2ab9977SPaul Walmsley static u32 omap2xxx_sdrc_get_fast_dll_ctrl(void)
4696609ef4SPaul Walmsley {
4796609ef4SPaul Walmsley return mem_timings.fast_dll_ctrl;
4896609ef4SPaul Walmsley }
4996609ef4SPaul Walmsley
omap2xxx_sdrc_get_type(void)50f2ab9977SPaul Walmsley static u32 omap2xxx_sdrc_get_type(void)
5196609ef4SPaul Walmsley {
5296609ef4SPaul Walmsley return mem_timings.m_type;
5396609ef4SPaul Walmsley }
5496609ef4SPaul Walmsley
5596609ef4SPaul Walmsley /*
5696609ef4SPaul Walmsley * Check the DLL lock state, and return tue if running in unlock mode.
5796609ef4SPaul Walmsley * This is needed to compensate for the shifted DLL value in unlock mode.
5896609ef4SPaul Walmsley */
omap2xxx_sdrc_dll_is_unlocked(void)59f2ab9977SPaul Walmsley u32 omap2xxx_sdrc_dll_is_unlocked(void)
6096609ef4SPaul Walmsley {
6196609ef4SPaul Walmsley /* dlla and dllb are a set */
6296609ef4SPaul Walmsley u32 dll_state = sdrc_read_reg(SDRC_DLLA_CTRL);
6396609ef4SPaul Walmsley
6496609ef4SPaul Walmsley if ((dll_state & (1 << 2)) == (1 << 2))
6596609ef4SPaul Walmsley return 1;
6696609ef4SPaul Walmsley else
6796609ef4SPaul Walmsley return 0;
6896609ef4SPaul Walmsley }
6996609ef4SPaul Walmsley
7096609ef4SPaul Walmsley /*
7196609ef4SPaul Walmsley * 'level' is the value to store to CM_CLKSEL2_PLL.CORE_CLK_SRC.
7296609ef4SPaul Walmsley * Practical values are CORE_CLK_SRC_DPLL (for CORE_CLK = DPLL_CLK) or
7396609ef4SPaul Walmsley * CORE_CLK_SRC_DPLL_X2 (for CORE_CLK = * DPLL_CLK * 2)
74f2ab9977SPaul Walmsley *
75f2ab9977SPaul Walmsley * Used by the clock framework during CORE DPLL changes
7696609ef4SPaul Walmsley */
omap2xxx_sdrc_reprogram(u32 level,u32 force)77f2ab9977SPaul Walmsley u32 omap2xxx_sdrc_reprogram(u32 level, u32 force)
7896609ef4SPaul Walmsley {
7996609ef4SPaul Walmsley u32 dll_ctrl, m_type;
8096609ef4SPaul Walmsley u32 prev = curr_perf_level;
8196609ef4SPaul Walmsley unsigned long flags;
8296609ef4SPaul Walmsley
8396609ef4SPaul Walmsley if ((curr_perf_level == level) && !force)
8496609ef4SPaul Walmsley return prev;
8596609ef4SPaul Walmsley
8696609ef4SPaul Walmsley if (level == CORE_CLK_SRC_DPLL)
87f2ab9977SPaul Walmsley dll_ctrl = omap2xxx_sdrc_get_slow_dll_ctrl();
8896609ef4SPaul Walmsley else if (level == CORE_CLK_SRC_DPLL_X2)
89f2ab9977SPaul Walmsley dll_ctrl = omap2xxx_sdrc_get_fast_dll_ctrl();
9096609ef4SPaul Walmsley else
9196609ef4SPaul Walmsley return prev;
9296609ef4SPaul Walmsley
93f2ab9977SPaul Walmsley m_type = omap2xxx_sdrc_get_type();
9496609ef4SPaul Walmsley
9596609ef4SPaul Walmsley local_irq_save(flags);
96c4d7e58fSPaul Walmsley /*
97c4d7e58fSPaul Walmsley * XXX These calls should be abstracted out through a
98c4d7e58fSPaul Walmsley * prm2xxx.c function
99c4d7e58fSPaul Walmsley */
1008e3bd351STony Lindgren if (cpu_is_omap2420())
101edfaf05cSVictor Kamensky writel_relaxed(0xffff, OMAP2420_PRCM_VOLTSETUP);
1028e3bd351STony Lindgren else
103edfaf05cSVictor Kamensky writel_relaxed(0xffff, OMAP2430_PRCM_VOLTSETUP);
10496609ef4SPaul Walmsley omap2_sram_reprogram_sdrc(level, dll_ctrl, m_type);
10596609ef4SPaul Walmsley curr_perf_level = level;
10696609ef4SPaul Walmsley local_irq_restore(flags);
10796609ef4SPaul Walmsley
10896609ef4SPaul Walmsley return prev;
10996609ef4SPaul Walmsley }
11096609ef4SPaul Walmsley
111f2ab9977SPaul Walmsley /* Used by the clock framework during CORE DPLL changes */
omap2xxx_sdrc_init_params(u32 force_lock_to_unlock_mode)112f2ab9977SPaul Walmsley void omap2xxx_sdrc_init_params(u32 force_lock_to_unlock_mode)
11396609ef4SPaul Walmsley {
11496609ef4SPaul Walmsley unsigned long dll_cnt;
11596609ef4SPaul Walmsley u32 fast_dll = 0;
11696609ef4SPaul Walmsley
11796609ef4SPaul Walmsley /* DDR = 1, SDR = 0 */
11896609ef4SPaul Walmsley mem_timings.m_type = !((sdrc_read_reg(SDRC_MR_0) & 0x3) == 0x1);
11996609ef4SPaul Walmsley
12096609ef4SPaul Walmsley /* 2422 es2.05 and beyond has a single SIP DDR instead of 2 like others.
12196609ef4SPaul Walmsley * In the case of 2422, its ok to use CS1 instead of CS0.
12296609ef4SPaul Walmsley */
12396609ef4SPaul Walmsley if (cpu_is_omap2422())
12496609ef4SPaul Walmsley mem_timings.base_cs = 1;
12596609ef4SPaul Walmsley else
12696609ef4SPaul Walmsley mem_timings.base_cs = 0;
12796609ef4SPaul Walmsley
12896609ef4SPaul Walmsley if (mem_timings.m_type != M_DDR)
12996609ef4SPaul Walmsley return;
13096609ef4SPaul Walmsley
13196609ef4SPaul Walmsley /* With DDR we need to determine the low frequency DLL value */
13296609ef4SPaul Walmsley if (((mem_timings.fast_dll_ctrl & (1 << 2)) == M_LOCK_CTRL))
13396609ef4SPaul Walmsley mem_timings.dll_mode = M_UNLOCK;
13496609ef4SPaul Walmsley else
13596609ef4SPaul Walmsley mem_timings.dll_mode = M_LOCK;
13696609ef4SPaul Walmsley
13796609ef4SPaul Walmsley if (mem_timings.base_cs == 0) {
13896609ef4SPaul Walmsley fast_dll = sdrc_read_reg(SDRC_DLLA_CTRL);
13996609ef4SPaul Walmsley dll_cnt = sdrc_read_reg(SDRC_DLLA_STATUS) & 0xff00;
14096609ef4SPaul Walmsley } else {
14196609ef4SPaul Walmsley fast_dll = sdrc_read_reg(SDRC_DLLB_CTRL);
14296609ef4SPaul Walmsley dll_cnt = sdrc_read_reg(SDRC_DLLB_STATUS) & 0xff00;
14396609ef4SPaul Walmsley }
14496609ef4SPaul Walmsley if (force_lock_to_unlock_mode) {
14596609ef4SPaul Walmsley fast_dll &= ~0xff00;
14696609ef4SPaul Walmsley fast_dll |= dll_cnt; /* Current lock mode */
14796609ef4SPaul Walmsley }
14896609ef4SPaul Walmsley /* set fast timings with DLL filter disabled */
14996609ef4SPaul Walmsley mem_timings.fast_dll_ctrl = (fast_dll | (3 << 8));
15096609ef4SPaul Walmsley
15196609ef4SPaul Walmsley /* No disruptions, DDR will be offline & C-ABI not followed */
15296609ef4SPaul Walmsley omap2_sram_ddr_init(&mem_timings.slow_dll_ctrl,
15396609ef4SPaul Walmsley mem_timings.fast_dll_ctrl,
15496609ef4SPaul Walmsley mem_timings.base_cs,
15596609ef4SPaul Walmsley force_lock_to_unlock_mode);
15696609ef4SPaul Walmsley mem_timings.slow_dll_ctrl &= 0xff00; /* Keep lock value */
15796609ef4SPaul Walmsley
15896609ef4SPaul Walmsley /* Turn status into unlock ctrl */
15996609ef4SPaul Walmsley mem_timings.slow_dll_ctrl |=
16096609ef4SPaul Walmsley ((mem_timings.fast_dll_ctrl & 0xF) | (1 << 2));
16196609ef4SPaul Walmsley
1626a53bc75SRussell King /* 90 degree phase for anything below 133MHz + disable DLL filter */
16396609ef4SPaul Walmsley mem_timings.slow_dll_ctrl |= ((1 << 1) | (3 << 8));
16496609ef4SPaul Walmsley }
165