xref: /openbmc/linux/arch/arm/mach-omap2/sdrc2xxx.c (revision 8e3bd351d1d2505e17d0b10c17bf8d7655eb9faf)
196609ef4SPaul Walmsley /*
296609ef4SPaul Walmsley  * linux/arch/arm/mach-omap2/sdrc2xxx.c
396609ef4SPaul Walmsley  *
496609ef4SPaul Walmsley  * SDRAM timing related functions for OMAP2xxx
596609ef4SPaul Walmsley  *
6f2ab9977SPaul Walmsley  * Copyright (C) 2005, 2008 Texas Instruments Inc.
7f2ab9977SPaul Walmsley  * Copyright (C) 2005, 2008 Nokia Corporation
896609ef4SPaul Walmsley  *
996609ef4SPaul Walmsley  * Tony Lindgren <tony@atomide.com>
10f2ab9977SPaul Walmsley  * Paul Walmsley
11f2ab9977SPaul Walmsley  * Richard Woodruff <r-woodruff2@ti.com>
1296609ef4SPaul Walmsley  *
1396609ef4SPaul Walmsley  * This program is free software; you can redistribute it and/or modify
1496609ef4SPaul Walmsley  * it under the terms of the GNU General Public License version 2 as
1596609ef4SPaul Walmsley  * published by the Free Software Foundation.
1696609ef4SPaul Walmsley  */
1796609ef4SPaul Walmsley 
1896609ef4SPaul Walmsley #include <linux/module.h>
1996609ef4SPaul Walmsley #include <linux/kernel.h>
2096609ef4SPaul Walmsley #include <linux/device.h>
2196609ef4SPaul Walmsley #include <linux/list.h>
2296609ef4SPaul Walmsley #include <linux/errno.h>
2396609ef4SPaul Walmsley #include <linux/delay.h>
2496609ef4SPaul Walmsley #include <linux/clk.h>
2596609ef4SPaul Walmsley #include <linux/io.h>
2696609ef4SPaul Walmsley 
2796609ef4SPaul Walmsley #include <mach/common.h>
2896609ef4SPaul Walmsley #include <mach/clock.h>
2996609ef4SPaul Walmsley #include <mach/sram.h>
3096609ef4SPaul Walmsley 
3196609ef4SPaul Walmsley #include "prm.h"
32c0bf3132SRussell King #include "clock.h"
3396609ef4SPaul Walmsley #include <mach/sdrc.h>
3496609ef4SPaul Walmsley #include "sdrc.h"
3596609ef4SPaul Walmsley 
3696609ef4SPaul Walmsley /* Memory timing, DLL mode flags */
3796609ef4SPaul Walmsley #define M_DDR		1
3896609ef4SPaul Walmsley #define M_LOCK_CTRL	(1 << 2)
3996609ef4SPaul Walmsley #define M_UNLOCK	0
4096609ef4SPaul Walmsley #define M_LOCK		1
4196609ef4SPaul Walmsley 
4296609ef4SPaul Walmsley 
4396609ef4SPaul Walmsley static struct memory_timings mem_timings;
4496609ef4SPaul Walmsley static u32 curr_perf_level = CORE_CLK_SRC_DPLL_X2;
4596609ef4SPaul Walmsley 
46f2ab9977SPaul Walmsley static u32 omap2xxx_sdrc_get_slow_dll_ctrl(void)
4796609ef4SPaul Walmsley {
4896609ef4SPaul Walmsley 	return mem_timings.slow_dll_ctrl;
4996609ef4SPaul Walmsley }
5096609ef4SPaul Walmsley 
51f2ab9977SPaul Walmsley static u32 omap2xxx_sdrc_get_fast_dll_ctrl(void)
5296609ef4SPaul Walmsley {
5396609ef4SPaul Walmsley 	return mem_timings.fast_dll_ctrl;
5496609ef4SPaul Walmsley }
5596609ef4SPaul Walmsley 
56f2ab9977SPaul Walmsley static u32 omap2xxx_sdrc_get_type(void)
5796609ef4SPaul Walmsley {
5896609ef4SPaul Walmsley 	return mem_timings.m_type;
5996609ef4SPaul Walmsley }
6096609ef4SPaul Walmsley 
6196609ef4SPaul Walmsley /*
6296609ef4SPaul Walmsley  * Check the DLL lock state, and return tue if running in unlock mode.
6396609ef4SPaul Walmsley  * This is needed to compensate for the shifted DLL value in unlock mode.
6496609ef4SPaul Walmsley  */
65f2ab9977SPaul Walmsley u32 omap2xxx_sdrc_dll_is_unlocked(void)
6696609ef4SPaul Walmsley {
6796609ef4SPaul Walmsley 	/* dlla and dllb are a set */
6896609ef4SPaul Walmsley 	u32 dll_state = sdrc_read_reg(SDRC_DLLA_CTRL);
6996609ef4SPaul Walmsley 
7096609ef4SPaul Walmsley 	if ((dll_state & (1 << 2)) == (1 << 2))
7196609ef4SPaul Walmsley 		return 1;
7296609ef4SPaul Walmsley 	else
7396609ef4SPaul Walmsley 		return 0;
7496609ef4SPaul Walmsley }
7596609ef4SPaul Walmsley 
7696609ef4SPaul Walmsley /*
7796609ef4SPaul Walmsley  * 'level' is the value to store to CM_CLKSEL2_PLL.CORE_CLK_SRC.
7896609ef4SPaul Walmsley  * Practical values are CORE_CLK_SRC_DPLL (for CORE_CLK = DPLL_CLK) or
7996609ef4SPaul Walmsley  * CORE_CLK_SRC_DPLL_X2 (for CORE_CLK = * DPLL_CLK * 2)
80f2ab9977SPaul Walmsley  *
81f2ab9977SPaul Walmsley  * Used by the clock framework during CORE DPLL changes
8296609ef4SPaul Walmsley  */
83f2ab9977SPaul Walmsley u32 omap2xxx_sdrc_reprogram(u32 level, u32 force)
8496609ef4SPaul Walmsley {
8596609ef4SPaul Walmsley 	u32 dll_ctrl, m_type;
8696609ef4SPaul Walmsley 	u32 prev = curr_perf_level;
8796609ef4SPaul Walmsley 	unsigned long flags;
8896609ef4SPaul Walmsley 
8996609ef4SPaul Walmsley 	if ((curr_perf_level == level) && !force)
9096609ef4SPaul Walmsley 		return prev;
9196609ef4SPaul Walmsley 
9296609ef4SPaul Walmsley 	if (level == CORE_CLK_SRC_DPLL)
93f2ab9977SPaul Walmsley 		dll_ctrl = omap2xxx_sdrc_get_slow_dll_ctrl();
9496609ef4SPaul Walmsley 	else if (level == CORE_CLK_SRC_DPLL_X2)
95f2ab9977SPaul Walmsley 		dll_ctrl = omap2xxx_sdrc_get_fast_dll_ctrl();
9696609ef4SPaul Walmsley 	else
9796609ef4SPaul Walmsley 		return prev;
9896609ef4SPaul Walmsley 
99f2ab9977SPaul Walmsley 	m_type = omap2xxx_sdrc_get_type();
10096609ef4SPaul Walmsley 
10196609ef4SPaul Walmsley 	local_irq_save(flags);
102*8e3bd351STony Lindgren 	if (cpu_is_omap2420())
103*8e3bd351STony Lindgren 		__raw_writel(0xffff, OMAP2420_PRCM_VOLTSETUP);
104*8e3bd351STony Lindgren 	else
105*8e3bd351STony Lindgren 		__raw_writel(0xffff, OMAP2430_PRCM_VOLTSETUP);
10696609ef4SPaul Walmsley 	omap2_sram_reprogram_sdrc(level, dll_ctrl, m_type);
10796609ef4SPaul Walmsley 	curr_perf_level = level;
10896609ef4SPaul Walmsley 	local_irq_restore(flags);
10996609ef4SPaul Walmsley 
11096609ef4SPaul Walmsley 	return prev;
11196609ef4SPaul Walmsley }
11296609ef4SPaul Walmsley 
113f2ab9977SPaul Walmsley /* Used by the clock framework during CORE DPLL changes */
114f2ab9977SPaul Walmsley void omap2xxx_sdrc_init_params(u32 force_lock_to_unlock_mode)
11596609ef4SPaul Walmsley {
11696609ef4SPaul Walmsley 	unsigned long dll_cnt;
11796609ef4SPaul Walmsley 	u32 fast_dll = 0;
11896609ef4SPaul Walmsley 
11996609ef4SPaul Walmsley 	/* DDR = 1, SDR = 0 */
12096609ef4SPaul Walmsley 	mem_timings.m_type = !((sdrc_read_reg(SDRC_MR_0) & 0x3) == 0x1);
12196609ef4SPaul Walmsley 
12296609ef4SPaul Walmsley 	/* 2422 es2.05 and beyond has a single SIP DDR instead of 2 like others.
12396609ef4SPaul Walmsley 	 * In the case of 2422, its ok to use CS1 instead of CS0.
12496609ef4SPaul Walmsley 	 */
12596609ef4SPaul Walmsley 	if (cpu_is_omap2422())
12696609ef4SPaul Walmsley 		mem_timings.base_cs = 1;
12796609ef4SPaul Walmsley 	else
12896609ef4SPaul Walmsley 		mem_timings.base_cs = 0;
12996609ef4SPaul Walmsley 
13096609ef4SPaul Walmsley 	if (mem_timings.m_type != M_DDR)
13196609ef4SPaul Walmsley 		return;
13296609ef4SPaul Walmsley 
13396609ef4SPaul Walmsley 	/* With DDR we need to determine the low frequency DLL value */
13496609ef4SPaul Walmsley 	if (((mem_timings.fast_dll_ctrl & (1 << 2)) == M_LOCK_CTRL))
13596609ef4SPaul Walmsley 		mem_timings.dll_mode = M_UNLOCK;
13696609ef4SPaul Walmsley 	else
13796609ef4SPaul Walmsley 		mem_timings.dll_mode = M_LOCK;
13896609ef4SPaul Walmsley 
13996609ef4SPaul Walmsley 	if (mem_timings.base_cs == 0) {
14096609ef4SPaul Walmsley 		fast_dll = sdrc_read_reg(SDRC_DLLA_CTRL);
14196609ef4SPaul Walmsley 		dll_cnt = sdrc_read_reg(SDRC_DLLA_STATUS) & 0xff00;
14296609ef4SPaul Walmsley 	} else {
14396609ef4SPaul Walmsley 		fast_dll = sdrc_read_reg(SDRC_DLLB_CTRL);
14496609ef4SPaul Walmsley 		dll_cnt = sdrc_read_reg(SDRC_DLLB_STATUS) & 0xff00;
14596609ef4SPaul Walmsley 	}
14696609ef4SPaul Walmsley 	if (force_lock_to_unlock_mode) {
14796609ef4SPaul Walmsley 		fast_dll &= ~0xff00;
14896609ef4SPaul Walmsley 		fast_dll |= dll_cnt;		/* Current lock mode */
14996609ef4SPaul Walmsley 	}
15096609ef4SPaul Walmsley 	/* set fast timings with DLL filter disabled */
15196609ef4SPaul Walmsley 	mem_timings.fast_dll_ctrl = (fast_dll | (3 << 8));
15296609ef4SPaul Walmsley 
15396609ef4SPaul Walmsley 	/* No disruptions, DDR will be offline & C-ABI not followed */
15496609ef4SPaul Walmsley 	omap2_sram_ddr_init(&mem_timings.slow_dll_ctrl,
15596609ef4SPaul Walmsley 			    mem_timings.fast_dll_ctrl,
15696609ef4SPaul Walmsley 			    mem_timings.base_cs,
15796609ef4SPaul Walmsley 			    force_lock_to_unlock_mode);
15896609ef4SPaul Walmsley 	mem_timings.slow_dll_ctrl &= 0xff00;	/* Keep lock value */
15996609ef4SPaul Walmsley 
16096609ef4SPaul Walmsley 	/* Turn status into unlock ctrl */
16196609ef4SPaul Walmsley 	mem_timings.slow_dll_ctrl |=
16296609ef4SPaul Walmsley 		((mem_timings.fast_dll_ctrl & 0xF) | (1 << 2));
16396609ef4SPaul Walmsley 
16496609ef4SPaul Walmsley 	/* 90 degree phase for anything below 133Mhz + disable DLL filter */
16596609ef4SPaul Walmsley 	mem_timings.slow_dll_ctrl |= ((1 << 1) | (3 << 8));
16696609ef4SPaul Walmsley }
167