xref: /openbmc/linux/arch/arm/mach-omap2/sdrc2xxx.c (revision 139563ad27e7baad7935b8113940f0d804cf513b)
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 
27ce491cf8STony Lindgren #include <plat/clock.h>
28ce491cf8STony Lindgren #include <plat/sram.h>
29ee0839c2STony Lindgren #include <plat/sdrc.h>
3096609ef4SPaul Walmsley 
31dbc04161STony Lindgren #include "soc.h"
32ee0839c2STony Lindgren #include "iomap.h"
33ee0839c2STony Lindgren #include "common.h"
34*139563adSPaul Walmsley #include "prm2xxx.h"
35c0bf3132SRussell King #include "clock.h"
3696609ef4SPaul Walmsley #include "sdrc.h"
3796609ef4SPaul Walmsley 
3896609ef4SPaul Walmsley /* Memory timing, DLL mode flags */
3996609ef4SPaul Walmsley #define M_DDR		1
4096609ef4SPaul Walmsley #define M_LOCK_CTRL	(1 << 2)
4196609ef4SPaul Walmsley #define M_UNLOCK	0
4296609ef4SPaul Walmsley #define M_LOCK		1
4396609ef4SPaul Walmsley 
4496609ef4SPaul Walmsley 
4596609ef4SPaul Walmsley static struct memory_timings mem_timings;
4696609ef4SPaul Walmsley static u32 curr_perf_level = CORE_CLK_SRC_DPLL_X2;
4796609ef4SPaul Walmsley 
48f2ab9977SPaul Walmsley static u32 omap2xxx_sdrc_get_slow_dll_ctrl(void)
4996609ef4SPaul Walmsley {
5096609ef4SPaul Walmsley 	return mem_timings.slow_dll_ctrl;
5196609ef4SPaul Walmsley }
5296609ef4SPaul Walmsley 
53f2ab9977SPaul Walmsley static u32 omap2xxx_sdrc_get_fast_dll_ctrl(void)
5496609ef4SPaul Walmsley {
5596609ef4SPaul Walmsley 	return mem_timings.fast_dll_ctrl;
5696609ef4SPaul Walmsley }
5796609ef4SPaul Walmsley 
58f2ab9977SPaul Walmsley static u32 omap2xxx_sdrc_get_type(void)
5996609ef4SPaul Walmsley {
6096609ef4SPaul Walmsley 	return mem_timings.m_type;
6196609ef4SPaul Walmsley }
6296609ef4SPaul Walmsley 
6396609ef4SPaul Walmsley /*
6496609ef4SPaul Walmsley  * Check the DLL lock state, and return tue if running in unlock mode.
6596609ef4SPaul Walmsley  * This is needed to compensate for the shifted DLL value in unlock mode.
6696609ef4SPaul Walmsley  */
67f2ab9977SPaul Walmsley u32 omap2xxx_sdrc_dll_is_unlocked(void)
6896609ef4SPaul Walmsley {
6996609ef4SPaul Walmsley 	/* dlla and dllb are a set */
7096609ef4SPaul Walmsley 	u32 dll_state = sdrc_read_reg(SDRC_DLLA_CTRL);
7196609ef4SPaul Walmsley 
7296609ef4SPaul Walmsley 	if ((dll_state & (1 << 2)) == (1 << 2))
7396609ef4SPaul Walmsley 		return 1;
7496609ef4SPaul Walmsley 	else
7596609ef4SPaul Walmsley 		return 0;
7696609ef4SPaul Walmsley }
7796609ef4SPaul Walmsley 
7896609ef4SPaul Walmsley /*
7996609ef4SPaul Walmsley  * 'level' is the value to store to CM_CLKSEL2_PLL.CORE_CLK_SRC.
8096609ef4SPaul Walmsley  * Practical values are CORE_CLK_SRC_DPLL (for CORE_CLK = DPLL_CLK) or
8196609ef4SPaul Walmsley  * CORE_CLK_SRC_DPLL_X2 (for CORE_CLK = * DPLL_CLK * 2)
82f2ab9977SPaul Walmsley  *
83f2ab9977SPaul Walmsley  * Used by the clock framework during CORE DPLL changes
8496609ef4SPaul Walmsley  */
85f2ab9977SPaul Walmsley u32 omap2xxx_sdrc_reprogram(u32 level, u32 force)
8696609ef4SPaul Walmsley {
8796609ef4SPaul Walmsley 	u32 dll_ctrl, m_type;
8896609ef4SPaul Walmsley 	u32 prev = curr_perf_level;
8996609ef4SPaul Walmsley 	unsigned long flags;
9096609ef4SPaul Walmsley 
9196609ef4SPaul Walmsley 	if ((curr_perf_level == level) && !force)
9296609ef4SPaul Walmsley 		return prev;
9396609ef4SPaul Walmsley 
9496609ef4SPaul Walmsley 	if (level == CORE_CLK_SRC_DPLL)
95f2ab9977SPaul Walmsley 		dll_ctrl = omap2xxx_sdrc_get_slow_dll_ctrl();
9696609ef4SPaul Walmsley 	else if (level == CORE_CLK_SRC_DPLL_X2)
97f2ab9977SPaul Walmsley 		dll_ctrl = omap2xxx_sdrc_get_fast_dll_ctrl();
9896609ef4SPaul Walmsley 	else
9996609ef4SPaul Walmsley 		return prev;
10096609ef4SPaul Walmsley 
101f2ab9977SPaul Walmsley 	m_type = omap2xxx_sdrc_get_type();
10296609ef4SPaul Walmsley 
10396609ef4SPaul Walmsley 	local_irq_save(flags);
104c4d7e58fSPaul Walmsley 	/*
105c4d7e58fSPaul Walmsley 	 * XXX These calls should be abstracted out through a
106c4d7e58fSPaul Walmsley 	 * prm2xxx.c function
107c4d7e58fSPaul Walmsley 	 */
1088e3bd351STony Lindgren 	if (cpu_is_omap2420())
1098e3bd351STony Lindgren 		__raw_writel(0xffff, OMAP2420_PRCM_VOLTSETUP);
1108e3bd351STony Lindgren 	else
1118e3bd351STony Lindgren 		__raw_writel(0xffff, OMAP2430_PRCM_VOLTSETUP);
11296609ef4SPaul Walmsley 	omap2_sram_reprogram_sdrc(level, dll_ctrl, m_type);
11396609ef4SPaul Walmsley 	curr_perf_level = level;
11496609ef4SPaul Walmsley 	local_irq_restore(flags);
11596609ef4SPaul Walmsley 
11696609ef4SPaul Walmsley 	return prev;
11796609ef4SPaul Walmsley }
11896609ef4SPaul Walmsley 
119f2ab9977SPaul Walmsley /* Used by the clock framework during CORE DPLL changes */
120f2ab9977SPaul Walmsley void omap2xxx_sdrc_init_params(u32 force_lock_to_unlock_mode)
12196609ef4SPaul Walmsley {
12296609ef4SPaul Walmsley 	unsigned long dll_cnt;
12396609ef4SPaul Walmsley 	u32 fast_dll = 0;
12496609ef4SPaul Walmsley 
12596609ef4SPaul Walmsley 	/* DDR = 1, SDR = 0 */
12696609ef4SPaul Walmsley 	mem_timings.m_type = !((sdrc_read_reg(SDRC_MR_0) & 0x3) == 0x1);
12796609ef4SPaul Walmsley 
12896609ef4SPaul Walmsley 	/* 2422 es2.05 and beyond has a single SIP DDR instead of 2 like others.
12996609ef4SPaul Walmsley 	 * In the case of 2422, its ok to use CS1 instead of CS0.
13096609ef4SPaul Walmsley 	 */
13196609ef4SPaul Walmsley 	if (cpu_is_omap2422())
13296609ef4SPaul Walmsley 		mem_timings.base_cs = 1;
13396609ef4SPaul Walmsley 	else
13496609ef4SPaul Walmsley 		mem_timings.base_cs = 0;
13596609ef4SPaul Walmsley 
13696609ef4SPaul Walmsley 	if (mem_timings.m_type != M_DDR)
13796609ef4SPaul Walmsley 		return;
13896609ef4SPaul Walmsley 
13996609ef4SPaul Walmsley 	/* With DDR we need to determine the low frequency DLL value */
14096609ef4SPaul Walmsley 	if (((mem_timings.fast_dll_ctrl & (1 << 2)) == M_LOCK_CTRL))
14196609ef4SPaul Walmsley 		mem_timings.dll_mode = M_UNLOCK;
14296609ef4SPaul Walmsley 	else
14396609ef4SPaul Walmsley 		mem_timings.dll_mode = M_LOCK;
14496609ef4SPaul Walmsley 
14596609ef4SPaul Walmsley 	if (mem_timings.base_cs == 0) {
14696609ef4SPaul Walmsley 		fast_dll = sdrc_read_reg(SDRC_DLLA_CTRL);
14796609ef4SPaul Walmsley 		dll_cnt = sdrc_read_reg(SDRC_DLLA_STATUS) & 0xff00;
14896609ef4SPaul Walmsley 	} else {
14996609ef4SPaul Walmsley 		fast_dll = sdrc_read_reg(SDRC_DLLB_CTRL);
15096609ef4SPaul Walmsley 		dll_cnt = sdrc_read_reg(SDRC_DLLB_STATUS) & 0xff00;
15196609ef4SPaul Walmsley 	}
15296609ef4SPaul Walmsley 	if (force_lock_to_unlock_mode) {
15396609ef4SPaul Walmsley 		fast_dll &= ~0xff00;
15496609ef4SPaul Walmsley 		fast_dll |= dll_cnt;		/* Current lock mode */
15596609ef4SPaul Walmsley 	}
15696609ef4SPaul Walmsley 	/* set fast timings with DLL filter disabled */
15796609ef4SPaul Walmsley 	mem_timings.fast_dll_ctrl = (fast_dll | (3 << 8));
15896609ef4SPaul Walmsley 
15996609ef4SPaul Walmsley 	/* No disruptions, DDR will be offline & C-ABI not followed */
16096609ef4SPaul Walmsley 	omap2_sram_ddr_init(&mem_timings.slow_dll_ctrl,
16196609ef4SPaul Walmsley 			    mem_timings.fast_dll_ctrl,
16296609ef4SPaul Walmsley 			    mem_timings.base_cs,
16396609ef4SPaul Walmsley 			    force_lock_to_unlock_mode);
16496609ef4SPaul Walmsley 	mem_timings.slow_dll_ctrl &= 0xff00;	/* Keep lock value */
16596609ef4SPaul Walmsley 
16696609ef4SPaul Walmsley 	/* Turn status into unlock ctrl */
16796609ef4SPaul Walmsley 	mem_timings.slow_dll_ctrl |=
16896609ef4SPaul Walmsley 		((mem_timings.fast_dll_ctrl & 0xF) | (1 << 2));
16996609ef4SPaul Walmsley 
17096609ef4SPaul Walmsley 	/* 90 degree phase for anything below 133Mhz + disable DLL filter */
17196609ef4SPaul Walmsley 	mem_timings.slow_dll_ctrl |= ((1 << 1) | (3 << 8));
17296609ef4SPaul Walmsley }
173