xref: /linux/drivers/memory/tegra/tegra30-emc.c (revision 6c6bd2075f01f8c8ae4bc803ba5cd23a3d3db533)
1e34212c7SDmitry Osipenko // SPDX-License-Identifier: GPL-2.0+
2e34212c7SDmitry Osipenko /*
3e34212c7SDmitry Osipenko  * Tegra30 External Memory Controller driver
4e34212c7SDmitry Osipenko  *
5e34212c7SDmitry Osipenko  * Based on downstream driver from NVIDIA and tegra124-emc.c
6e34212c7SDmitry Osipenko  * Copyright (C) 2011-2014 NVIDIA Corporation
7e34212c7SDmitry Osipenko  *
8e34212c7SDmitry Osipenko  * Author: Dmitry Osipenko <digetx@gmail.com>
9e34212c7SDmitry Osipenko  * Copyright (C) 2019 GRATE-DRIVER project
10e34212c7SDmitry Osipenko  */
11e34212c7SDmitry Osipenko 
12e34212c7SDmitry Osipenko #include <linux/clk.h>
13e34212c7SDmitry Osipenko #include <linux/clk/tegra.h>
148cee32b4SThierry Reding #include <linux/debugfs.h>
15e34212c7SDmitry Osipenko #include <linux/delay.h>
16e34212c7SDmitry Osipenko #include <linux/err.h>
17e34212c7SDmitry Osipenko #include <linux/interrupt.h>
18e34212c7SDmitry Osipenko #include <linux/io.h>
19e34212c7SDmitry Osipenko #include <linux/iopoll.h>
20e34212c7SDmitry Osipenko #include <linux/kernel.h>
21e34212c7SDmitry Osipenko #include <linux/module.h>
22e34212c7SDmitry Osipenko #include <linux/of_platform.h>
23e34212c7SDmitry Osipenko #include <linux/platform_device.h>
24e34212c7SDmitry Osipenko #include <linux/sort.h>
25e34212c7SDmitry Osipenko #include <linux/types.h>
26e34212c7SDmitry Osipenko 
27e34212c7SDmitry Osipenko #include <soc/tegra/fuse.h>
28e34212c7SDmitry Osipenko 
29e34212c7SDmitry Osipenko #include "mc.h"
30e34212c7SDmitry Osipenko 
31e34212c7SDmitry Osipenko #define EMC_INTSTATUS				0x000
32e34212c7SDmitry Osipenko #define EMC_INTMASK				0x004
33e34212c7SDmitry Osipenko #define EMC_DBG					0x008
34e34212c7SDmitry Osipenko #define EMC_CFG					0x00c
35e34212c7SDmitry Osipenko #define EMC_REFCTRL				0x020
36e34212c7SDmitry Osipenko #define EMC_TIMING_CONTROL			0x028
37e34212c7SDmitry Osipenko #define EMC_RC					0x02c
38e34212c7SDmitry Osipenko #define EMC_RFC					0x030
39e34212c7SDmitry Osipenko #define EMC_RAS					0x034
40e34212c7SDmitry Osipenko #define EMC_RP					0x038
41e34212c7SDmitry Osipenko #define EMC_R2W					0x03c
42e34212c7SDmitry Osipenko #define EMC_W2R					0x040
43e34212c7SDmitry Osipenko #define EMC_R2P					0x044
44e34212c7SDmitry Osipenko #define EMC_W2P					0x048
45e34212c7SDmitry Osipenko #define EMC_RD_RCD				0x04c
46e34212c7SDmitry Osipenko #define EMC_WR_RCD				0x050
47e34212c7SDmitry Osipenko #define EMC_RRD					0x054
48e34212c7SDmitry Osipenko #define EMC_REXT				0x058
49e34212c7SDmitry Osipenko #define EMC_WDV					0x05c
50e34212c7SDmitry Osipenko #define EMC_QUSE				0x060
51e34212c7SDmitry Osipenko #define EMC_QRST				0x064
52e34212c7SDmitry Osipenko #define EMC_QSAFE				0x068
53e34212c7SDmitry Osipenko #define EMC_RDV					0x06c
54e34212c7SDmitry Osipenko #define EMC_REFRESH				0x070
55e34212c7SDmitry Osipenko #define EMC_BURST_REFRESH_NUM			0x074
56e34212c7SDmitry Osipenko #define EMC_PDEX2WR				0x078
57e34212c7SDmitry Osipenko #define EMC_PDEX2RD				0x07c
58e34212c7SDmitry Osipenko #define EMC_PCHG2PDEN				0x080
59e34212c7SDmitry Osipenko #define EMC_ACT2PDEN				0x084
60e34212c7SDmitry Osipenko #define EMC_AR2PDEN				0x088
61e34212c7SDmitry Osipenko #define EMC_RW2PDEN				0x08c
62e34212c7SDmitry Osipenko #define EMC_TXSR				0x090
63e34212c7SDmitry Osipenko #define EMC_TCKE				0x094
64e34212c7SDmitry Osipenko #define EMC_TFAW				0x098
65e34212c7SDmitry Osipenko #define EMC_TRPAB				0x09c
66e34212c7SDmitry Osipenko #define EMC_TCLKSTABLE				0x0a0
67e34212c7SDmitry Osipenko #define EMC_TCLKSTOP				0x0a4
68e34212c7SDmitry Osipenko #define EMC_TREFBW				0x0a8
69e34212c7SDmitry Osipenko #define EMC_QUSE_EXTRA				0x0ac
70e34212c7SDmitry Osipenko #define EMC_ODT_WRITE				0x0b0
71e34212c7SDmitry Osipenko #define EMC_ODT_READ				0x0b4
72e34212c7SDmitry Osipenko #define EMC_WEXT				0x0b8
73e34212c7SDmitry Osipenko #define EMC_CTT					0x0bc
74e34212c7SDmitry Osipenko #define EMC_MRS_WAIT_CNT			0x0c8
75e34212c7SDmitry Osipenko #define EMC_MRS					0x0cc
76e34212c7SDmitry Osipenko #define EMC_EMRS				0x0d0
77e34212c7SDmitry Osipenko #define EMC_SELF_REF				0x0e0
78e34212c7SDmitry Osipenko #define EMC_MRW					0x0e8
79e34212c7SDmitry Osipenko #define EMC_XM2DQSPADCTRL3			0x0f8
80e34212c7SDmitry Osipenko #define EMC_FBIO_SPARE				0x100
81e34212c7SDmitry Osipenko #define EMC_FBIO_CFG5				0x104
82e34212c7SDmitry Osipenko #define EMC_FBIO_CFG6				0x114
83e34212c7SDmitry Osipenko #define EMC_CFG_RSV				0x120
84e34212c7SDmitry Osipenko #define EMC_AUTO_CAL_CONFIG			0x2a4
85e34212c7SDmitry Osipenko #define EMC_AUTO_CAL_INTERVAL			0x2a8
86e34212c7SDmitry Osipenko #define EMC_AUTO_CAL_STATUS			0x2ac
87e34212c7SDmitry Osipenko #define EMC_STATUS				0x2b4
88e34212c7SDmitry Osipenko #define EMC_CFG_2				0x2b8
89e34212c7SDmitry Osipenko #define EMC_CFG_DIG_DLL				0x2bc
90e34212c7SDmitry Osipenko #define EMC_CFG_DIG_DLL_PERIOD			0x2c0
91e34212c7SDmitry Osipenko #define EMC_CTT_DURATION			0x2d8
92e34212c7SDmitry Osipenko #define EMC_CTT_TERM_CTRL			0x2dc
93e34212c7SDmitry Osipenko #define EMC_ZCAL_INTERVAL			0x2e0
94e34212c7SDmitry Osipenko #define EMC_ZCAL_WAIT_CNT			0x2e4
95e34212c7SDmitry Osipenko #define EMC_ZQ_CAL				0x2ec
96e34212c7SDmitry Osipenko #define EMC_XM2CMDPADCTRL			0x2f0
97e34212c7SDmitry Osipenko #define EMC_XM2DQSPADCTRL2			0x2fc
98e34212c7SDmitry Osipenko #define EMC_XM2DQPADCTRL2			0x304
99e34212c7SDmitry Osipenko #define EMC_XM2CLKPADCTRL			0x308
100e34212c7SDmitry Osipenko #define EMC_XM2COMPPADCTRL			0x30c
101e34212c7SDmitry Osipenko #define EMC_XM2VTTGENPADCTRL			0x310
102e34212c7SDmitry Osipenko #define EMC_XM2VTTGENPADCTRL2			0x314
103e34212c7SDmitry Osipenko #define EMC_XM2QUSEPADCTRL			0x318
104e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_DQS0			0x328
105e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_DQS1			0x32c
106e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_DQS2			0x330
107e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_DQS3			0x334
108e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_DQS4			0x338
109e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_DQS5			0x33c
110e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_DQS6			0x340
111e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_DQS7			0x344
112e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_QUSE0			0x348
113e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_QUSE1			0x34c
114e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_QUSE2			0x350
115e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_QUSE3			0x354
116e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_QUSE4			0x358
117e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_QUSE5			0x35c
118e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_QUSE6			0x360
119e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_QUSE7			0x364
120e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_DQ0			0x368
121e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_DQ1			0x36c
122e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_DQ2			0x370
123e34212c7SDmitry Osipenko #define EMC_DLL_XFORM_DQ3			0x374
124e34212c7SDmitry Osipenko #define EMC_DLI_TRIM_TXDQS0			0x3a8
125e34212c7SDmitry Osipenko #define EMC_DLI_TRIM_TXDQS1			0x3ac
126e34212c7SDmitry Osipenko #define EMC_DLI_TRIM_TXDQS2			0x3b0
127e34212c7SDmitry Osipenko #define EMC_DLI_TRIM_TXDQS3			0x3b4
128e34212c7SDmitry Osipenko #define EMC_DLI_TRIM_TXDQS4			0x3b8
129e34212c7SDmitry Osipenko #define EMC_DLI_TRIM_TXDQS5			0x3bc
130e34212c7SDmitry Osipenko #define EMC_DLI_TRIM_TXDQS6			0x3c0
131e34212c7SDmitry Osipenko #define EMC_DLI_TRIM_TXDQS7			0x3c4
132e34212c7SDmitry Osipenko #define EMC_STALL_THEN_EXE_BEFORE_CLKCHANGE	0x3c8
133e34212c7SDmitry Osipenko #define EMC_STALL_THEN_EXE_AFTER_CLKCHANGE	0x3cc
134e34212c7SDmitry Osipenko #define EMC_UNSTALL_RW_AFTER_CLKCHANGE		0x3d0
135e34212c7SDmitry Osipenko #define EMC_SEL_DPD_CTRL			0x3d8
136e34212c7SDmitry Osipenko #define EMC_PRE_REFRESH_REQ_CNT			0x3dc
137e34212c7SDmitry Osipenko #define EMC_DYN_SELF_REF_CONTROL		0x3e0
138e34212c7SDmitry Osipenko #define EMC_TXSRDLL				0x3e4
139e34212c7SDmitry Osipenko 
140e34212c7SDmitry Osipenko #define EMC_STATUS_TIMING_UPDATE_STALLED	BIT(23)
141e34212c7SDmitry Osipenko 
142e34212c7SDmitry Osipenko #define EMC_MODE_SET_DLL_RESET			BIT(8)
143e34212c7SDmitry Osipenko #define EMC_MODE_SET_LONG_CNT			BIT(26)
144e34212c7SDmitry Osipenko 
145e34212c7SDmitry Osipenko #define EMC_SELF_REF_CMD_ENABLED		BIT(0)
146e34212c7SDmitry Osipenko 
147e34212c7SDmitry Osipenko #define DRAM_DEV_SEL_ALL			(0 << 30)
148e34212c7SDmitry Osipenko #define DRAM_DEV_SEL_0				(2 << 30)
149e34212c7SDmitry Osipenko #define DRAM_DEV_SEL_1				(1 << 30)
150e34212c7SDmitry Osipenko #define DRAM_BROADCAST(num) \
151e34212c7SDmitry Osipenko 	((num) > 1 ? DRAM_DEV_SEL_ALL : DRAM_DEV_SEL_0)
152e34212c7SDmitry Osipenko 
153e34212c7SDmitry Osipenko #define EMC_ZQ_CAL_CMD				BIT(0)
154e34212c7SDmitry Osipenko #define EMC_ZQ_CAL_LONG				BIT(4)
155e34212c7SDmitry Osipenko #define EMC_ZQ_CAL_LONG_CMD_DEV0 \
156e34212c7SDmitry Osipenko 	(DRAM_DEV_SEL_0 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD)
157e34212c7SDmitry Osipenko #define EMC_ZQ_CAL_LONG_CMD_DEV1 \
158e34212c7SDmitry Osipenko 	(DRAM_DEV_SEL_1 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD)
159e34212c7SDmitry Osipenko 
160e34212c7SDmitry Osipenko #define EMC_DBG_READ_MUX_ASSEMBLY		BIT(0)
161e34212c7SDmitry Osipenko #define EMC_DBG_WRITE_MUX_ACTIVE		BIT(1)
162e34212c7SDmitry Osipenko #define EMC_DBG_FORCE_UPDATE			BIT(2)
163e34212c7SDmitry Osipenko #define EMC_DBG_CFG_PRIORITY			BIT(24)
164e34212c7SDmitry Osipenko 
165e34212c7SDmitry Osipenko #define EMC_CFG5_QUSE_MODE_SHIFT		13
166e34212c7SDmitry Osipenko #define EMC_CFG5_QUSE_MODE_MASK			(7 << EMC_CFG5_QUSE_MODE_SHIFT)
167e34212c7SDmitry Osipenko 
168e34212c7SDmitry Osipenko #define EMC_CFG5_QUSE_MODE_INTERNAL_LPBK	2
169e34212c7SDmitry Osipenko #define EMC_CFG5_QUSE_MODE_PULSE_INTERN		3
170e34212c7SDmitry Osipenko 
171e34212c7SDmitry Osipenko #define EMC_SEL_DPD_CTRL_QUSE_DPD_ENABLE	BIT(9)
172e34212c7SDmitry Osipenko 
173e34212c7SDmitry Osipenko #define EMC_XM2COMPPADCTRL_VREF_CAL_ENABLE	BIT(10)
174e34212c7SDmitry Osipenko 
175e34212c7SDmitry Osipenko #define EMC_XM2QUSEPADCTRL_IVREF_ENABLE		BIT(4)
176e34212c7SDmitry Osipenko 
177e34212c7SDmitry Osipenko #define EMC_XM2DQSPADCTRL2_VREF_ENABLE		BIT(5)
178e34212c7SDmitry Osipenko #define EMC_XM2DQSPADCTRL3_VREF_ENABLE		BIT(5)
179e34212c7SDmitry Osipenko 
180e34212c7SDmitry Osipenko #define EMC_AUTO_CAL_STATUS_ACTIVE		BIT(31)
181e34212c7SDmitry Osipenko 
182e34212c7SDmitry Osipenko #define	EMC_FBIO_CFG5_DRAM_TYPE_MASK		0x3
183e34212c7SDmitry Osipenko 
184e34212c7SDmitry Osipenko #define EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK	0x3ff
185e34212c7SDmitry Osipenko #define EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT	16
186e34212c7SDmitry Osipenko #define EMC_MRS_WAIT_CNT_LONG_WAIT_MASK \
187e34212c7SDmitry Osipenko 	(0x3ff << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT)
188e34212c7SDmitry Osipenko 
189e34212c7SDmitry Osipenko #define EMC_REFCTRL_DEV_SEL_MASK		0x3
190e34212c7SDmitry Osipenko #define EMC_REFCTRL_ENABLE			BIT(31)
191e34212c7SDmitry Osipenko #define EMC_REFCTRL_ENABLE_ALL(num) \
192e34212c7SDmitry Osipenko 	(((num) > 1 ? 0 : 2) | EMC_REFCTRL_ENABLE)
193e34212c7SDmitry Osipenko #define EMC_REFCTRL_DISABLE_ALL(num)		((num) > 1 ? 0 : 2)
194e34212c7SDmitry Osipenko 
195e34212c7SDmitry Osipenko #define EMC_CFG_PERIODIC_QRST			BIT(21)
196e34212c7SDmitry Osipenko #define EMC_CFG_DYN_SREF_ENABLE			BIT(28)
197e34212c7SDmitry Osipenko 
198e34212c7SDmitry Osipenko #define EMC_CLKCHANGE_REQ_ENABLE		BIT(0)
199e34212c7SDmitry Osipenko #define EMC_CLKCHANGE_PD_ENABLE			BIT(1)
200e34212c7SDmitry Osipenko #define EMC_CLKCHANGE_SR_ENABLE			BIT(2)
201e34212c7SDmitry Osipenko 
202e34212c7SDmitry Osipenko #define EMC_TIMING_UPDATE			BIT(0)
203e34212c7SDmitry Osipenko 
204e34212c7SDmitry Osipenko #define EMC_REFRESH_OVERFLOW_INT		BIT(3)
205e34212c7SDmitry Osipenko #define EMC_CLKCHANGE_COMPLETE_INT		BIT(4)
206e34212c7SDmitry Osipenko 
207e34212c7SDmitry Osipenko enum emc_dram_type {
208e34212c7SDmitry Osipenko 	DRAM_TYPE_DDR3,
209e34212c7SDmitry Osipenko 	DRAM_TYPE_DDR1,
210e34212c7SDmitry Osipenko 	DRAM_TYPE_LPDDR2,
211e34212c7SDmitry Osipenko 	DRAM_TYPE_DDR2,
212e34212c7SDmitry Osipenko };
213e34212c7SDmitry Osipenko 
214e34212c7SDmitry Osipenko enum emc_dll_change {
215e34212c7SDmitry Osipenko 	DLL_CHANGE_NONE,
216e34212c7SDmitry Osipenko 	DLL_CHANGE_ON,
217e34212c7SDmitry Osipenko 	DLL_CHANGE_OFF
218e34212c7SDmitry Osipenko };
219e34212c7SDmitry Osipenko 
220e34212c7SDmitry Osipenko static const u16 emc_timing_registers[] = {
221e34212c7SDmitry Osipenko 	[0] = EMC_RC,
222e34212c7SDmitry Osipenko 	[1] = EMC_RFC,
223e34212c7SDmitry Osipenko 	[2] = EMC_RAS,
224e34212c7SDmitry Osipenko 	[3] = EMC_RP,
225e34212c7SDmitry Osipenko 	[4] = EMC_R2W,
226e34212c7SDmitry Osipenko 	[5] = EMC_W2R,
227e34212c7SDmitry Osipenko 	[6] = EMC_R2P,
228e34212c7SDmitry Osipenko 	[7] = EMC_W2P,
229e34212c7SDmitry Osipenko 	[8] = EMC_RD_RCD,
230e34212c7SDmitry Osipenko 	[9] = EMC_WR_RCD,
231e34212c7SDmitry Osipenko 	[10] = EMC_RRD,
232e34212c7SDmitry Osipenko 	[11] = EMC_REXT,
233e34212c7SDmitry Osipenko 	[12] = EMC_WEXT,
234e34212c7SDmitry Osipenko 	[13] = EMC_WDV,
235e34212c7SDmitry Osipenko 	[14] = EMC_QUSE,
236e34212c7SDmitry Osipenko 	[15] = EMC_QRST,
237e34212c7SDmitry Osipenko 	[16] = EMC_QSAFE,
238e34212c7SDmitry Osipenko 	[17] = EMC_RDV,
239e34212c7SDmitry Osipenko 	[18] = EMC_REFRESH,
240e34212c7SDmitry Osipenko 	[19] = EMC_BURST_REFRESH_NUM,
241e34212c7SDmitry Osipenko 	[20] = EMC_PRE_REFRESH_REQ_CNT,
242e34212c7SDmitry Osipenko 	[21] = EMC_PDEX2WR,
243e34212c7SDmitry Osipenko 	[22] = EMC_PDEX2RD,
244e34212c7SDmitry Osipenko 	[23] = EMC_PCHG2PDEN,
245e34212c7SDmitry Osipenko 	[24] = EMC_ACT2PDEN,
246e34212c7SDmitry Osipenko 	[25] = EMC_AR2PDEN,
247e34212c7SDmitry Osipenko 	[26] = EMC_RW2PDEN,
248e34212c7SDmitry Osipenko 	[27] = EMC_TXSR,
249e34212c7SDmitry Osipenko 	[28] = EMC_TXSRDLL,
250e34212c7SDmitry Osipenko 	[29] = EMC_TCKE,
251e34212c7SDmitry Osipenko 	[30] = EMC_TFAW,
252e34212c7SDmitry Osipenko 	[31] = EMC_TRPAB,
253e34212c7SDmitry Osipenko 	[32] = EMC_TCLKSTABLE,
254e34212c7SDmitry Osipenko 	[33] = EMC_TCLKSTOP,
255e34212c7SDmitry Osipenko 	[34] = EMC_TREFBW,
256e34212c7SDmitry Osipenko 	[35] = EMC_QUSE_EXTRA,
257e34212c7SDmitry Osipenko 	[36] = EMC_FBIO_CFG6,
258e34212c7SDmitry Osipenko 	[37] = EMC_ODT_WRITE,
259e34212c7SDmitry Osipenko 	[38] = EMC_ODT_READ,
260e34212c7SDmitry Osipenko 	[39] = EMC_FBIO_CFG5,
261e34212c7SDmitry Osipenko 	[40] = EMC_CFG_DIG_DLL,
262e34212c7SDmitry Osipenko 	[41] = EMC_CFG_DIG_DLL_PERIOD,
263e34212c7SDmitry Osipenko 	[42] = EMC_DLL_XFORM_DQS0,
264e34212c7SDmitry Osipenko 	[43] = EMC_DLL_XFORM_DQS1,
265e34212c7SDmitry Osipenko 	[44] = EMC_DLL_XFORM_DQS2,
266e34212c7SDmitry Osipenko 	[45] = EMC_DLL_XFORM_DQS3,
267e34212c7SDmitry Osipenko 	[46] = EMC_DLL_XFORM_DQS4,
268e34212c7SDmitry Osipenko 	[47] = EMC_DLL_XFORM_DQS5,
269e34212c7SDmitry Osipenko 	[48] = EMC_DLL_XFORM_DQS6,
270e34212c7SDmitry Osipenko 	[49] = EMC_DLL_XFORM_DQS7,
271e34212c7SDmitry Osipenko 	[50] = EMC_DLL_XFORM_QUSE0,
272e34212c7SDmitry Osipenko 	[51] = EMC_DLL_XFORM_QUSE1,
273e34212c7SDmitry Osipenko 	[52] = EMC_DLL_XFORM_QUSE2,
274e34212c7SDmitry Osipenko 	[53] = EMC_DLL_XFORM_QUSE3,
275e34212c7SDmitry Osipenko 	[54] = EMC_DLL_XFORM_QUSE4,
276e34212c7SDmitry Osipenko 	[55] = EMC_DLL_XFORM_QUSE5,
277e34212c7SDmitry Osipenko 	[56] = EMC_DLL_XFORM_QUSE6,
278e34212c7SDmitry Osipenko 	[57] = EMC_DLL_XFORM_QUSE7,
279e34212c7SDmitry Osipenko 	[58] = EMC_DLI_TRIM_TXDQS0,
280e34212c7SDmitry Osipenko 	[59] = EMC_DLI_TRIM_TXDQS1,
281e34212c7SDmitry Osipenko 	[60] = EMC_DLI_TRIM_TXDQS2,
282e34212c7SDmitry Osipenko 	[61] = EMC_DLI_TRIM_TXDQS3,
283e34212c7SDmitry Osipenko 	[62] = EMC_DLI_TRIM_TXDQS4,
284e34212c7SDmitry Osipenko 	[63] = EMC_DLI_TRIM_TXDQS5,
285e34212c7SDmitry Osipenko 	[64] = EMC_DLI_TRIM_TXDQS6,
286e34212c7SDmitry Osipenko 	[65] = EMC_DLI_TRIM_TXDQS7,
287e34212c7SDmitry Osipenko 	[66] = EMC_DLL_XFORM_DQ0,
288e34212c7SDmitry Osipenko 	[67] = EMC_DLL_XFORM_DQ1,
289e34212c7SDmitry Osipenko 	[68] = EMC_DLL_XFORM_DQ2,
290e34212c7SDmitry Osipenko 	[69] = EMC_DLL_XFORM_DQ3,
291e34212c7SDmitry Osipenko 	[70] = EMC_XM2CMDPADCTRL,
292e34212c7SDmitry Osipenko 	[71] = EMC_XM2DQSPADCTRL2,
293e34212c7SDmitry Osipenko 	[72] = EMC_XM2DQPADCTRL2,
294e34212c7SDmitry Osipenko 	[73] = EMC_XM2CLKPADCTRL,
295e34212c7SDmitry Osipenko 	[74] = EMC_XM2COMPPADCTRL,
296e34212c7SDmitry Osipenko 	[75] = EMC_XM2VTTGENPADCTRL,
297e34212c7SDmitry Osipenko 	[76] = EMC_XM2VTTGENPADCTRL2,
298e34212c7SDmitry Osipenko 	[77] = EMC_XM2QUSEPADCTRL,
299e34212c7SDmitry Osipenko 	[78] = EMC_XM2DQSPADCTRL3,
300e34212c7SDmitry Osipenko 	[79] = EMC_CTT_TERM_CTRL,
301e34212c7SDmitry Osipenko 	[80] = EMC_ZCAL_INTERVAL,
302e34212c7SDmitry Osipenko 	[81] = EMC_ZCAL_WAIT_CNT,
303e34212c7SDmitry Osipenko 	[82] = EMC_MRS_WAIT_CNT,
304e34212c7SDmitry Osipenko 	[83] = EMC_AUTO_CAL_CONFIG,
305e34212c7SDmitry Osipenko 	[84] = EMC_CTT,
306e34212c7SDmitry Osipenko 	[85] = EMC_CTT_DURATION,
307e34212c7SDmitry Osipenko 	[86] = EMC_DYN_SELF_REF_CONTROL,
308e34212c7SDmitry Osipenko 	[87] = EMC_FBIO_SPARE,
309e34212c7SDmitry Osipenko 	[88] = EMC_CFG_RSV,
310e34212c7SDmitry Osipenko };
311e34212c7SDmitry Osipenko 
312e34212c7SDmitry Osipenko struct emc_timing {
313e34212c7SDmitry Osipenko 	unsigned long rate;
314e34212c7SDmitry Osipenko 
315e34212c7SDmitry Osipenko 	u32 data[ARRAY_SIZE(emc_timing_registers)];
316e34212c7SDmitry Osipenko 
317e34212c7SDmitry Osipenko 	u32 emc_auto_cal_interval;
318e34212c7SDmitry Osipenko 	u32 emc_mode_1;
319e34212c7SDmitry Osipenko 	u32 emc_mode_2;
320e34212c7SDmitry Osipenko 	u32 emc_mode_reset;
321e34212c7SDmitry Osipenko 	u32 emc_zcal_cnt_long;
322e34212c7SDmitry Osipenko 	bool emc_cfg_periodic_qrst;
323e34212c7SDmitry Osipenko 	bool emc_cfg_dyn_self_ref;
324e34212c7SDmitry Osipenko };
325e34212c7SDmitry Osipenko 
326e34212c7SDmitry Osipenko struct tegra_emc {
327e34212c7SDmitry Osipenko 	struct device *dev;
328e34212c7SDmitry Osipenko 	struct tegra_mc *mc;
329e34212c7SDmitry Osipenko 	struct notifier_block clk_nb;
330e34212c7SDmitry Osipenko 	struct clk *clk;
331e34212c7SDmitry Osipenko 	void __iomem *regs;
332e34212c7SDmitry Osipenko 	unsigned int irq;
3330f8bb9daSDmitry Osipenko 	bool bad_state;
334e34212c7SDmitry Osipenko 
3350f8bb9daSDmitry Osipenko 	struct emc_timing *new_timing;
336e34212c7SDmitry Osipenko 	struct emc_timing *timings;
337e34212c7SDmitry Osipenko 	unsigned int num_timings;
338e34212c7SDmitry Osipenko 
339e34212c7SDmitry Osipenko 	u32 mc_override;
340e34212c7SDmitry Osipenko 	u32 emc_cfg;
341e34212c7SDmitry Osipenko 
342e34212c7SDmitry Osipenko 	u32 emc_mode_1;
343e34212c7SDmitry Osipenko 	u32 emc_mode_2;
344e34212c7SDmitry Osipenko 	u32 emc_mode_reset;
345e34212c7SDmitry Osipenko 
346e34212c7SDmitry Osipenko 	bool vref_cal_toggle : 1;
347e34212c7SDmitry Osipenko 	bool zcal_long : 1;
348e34212c7SDmitry Osipenko 	bool dll_on : 1;
3498cee32b4SThierry Reding 
3508cee32b4SThierry Reding 	struct {
3518cee32b4SThierry Reding 		struct dentry *root;
3528cee32b4SThierry Reding 		unsigned long min_rate;
3538cee32b4SThierry Reding 		unsigned long max_rate;
3548cee32b4SThierry Reding 	} debugfs;
355e34212c7SDmitry Osipenko };
356e34212c7SDmitry Osipenko 
3570f8bb9daSDmitry Osipenko static int emc_seq_update_timing(struct tegra_emc *emc)
3580f8bb9daSDmitry Osipenko {
3590f8bb9daSDmitry Osipenko 	u32 val;
3600f8bb9daSDmitry Osipenko 	int err;
3610f8bb9daSDmitry Osipenko 
3620f8bb9daSDmitry Osipenko 	writel_relaxed(EMC_TIMING_UPDATE, emc->regs + EMC_TIMING_CONTROL);
3630f8bb9daSDmitry Osipenko 
3640f8bb9daSDmitry Osipenko 	err = readl_relaxed_poll_timeout_atomic(emc->regs + EMC_STATUS, val,
3650f8bb9daSDmitry Osipenko 				!(val & EMC_STATUS_TIMING_UPDATE_STALLED),
3660f8bb9daSDmitry Osipenko 				1, 200);
3670f8bb9daSDmitry Osipenko 	if (err) {
3680f8bb9daSDmitry Osipenko 		dev_err(emc->dev, "failed to update timing: %d\n", err);
3690f8bb9daSDmitry Osipenko 		return err;
3700f8bb9daSDmitry Osipenko 	}
3710f8bb9daSDmitry Osipenko 
3720f8bb9daSDmitry Osipenko 	return 0;
3730f8bb9daSDmitry Osipenko }
3740f8bb9daSDmitry Osipenko 
375e34212c7SDmitry Osipenko static irqreturn_t tegra_emc_isr(int irq, void *data)
376e34212c7SDmitry Osipenko {
377e34212c7SDmitry Osipenko 	struct tegra_emc *emc = data;
378930c6818SDmitry Osipenko 	u32 intmask = EMC_REFRESH_OVERFLOW_INT;
379e34212c7SDmitry Osipenko 	u32 status;
380e34212c7SDmitry Osipenko 
381e34212c7SDmitry Osipenko 	status = readl_relaxed(emc->regs + EMC_INTSTATUS) & intmask;
382e34212c7SDmitry Osipenko 	if (!status)
383e34212c7SDmitry Osipenko 		return IRQ_NONE;
384e34212c7SDmitry Osipenko 
385e34212c7SDmitry Osipenko 	/* notify about HW problem */
386e34212c7SDmitry Osipenko 	if (status & EMC_REFRESH_OVERFLOW_INT)
387e34212c7SDmitry Osipenko 		dev_err_ratelimited(emc->dev,
388e34212c7SDmitry Osipenko 				    "refresh request overflow timeout\n");
389e34212c7SDmitry Osipenko 
390e34212c7SDmitry Osipenko 	/* clear interrupts */
391e34212c7SDmitry Osipenko 	writel_relaxed(status, emc->regs + EMC_INTSTATUS);
392e34212c7SDmitry Osipenko 
393e34212c7SDmitry Osipenko 	return IRQ_HANDLED;
394e34212c7SDmitry Osipenko }
395e34212c7SDmitry Osipenko 
396e34212c7SDmitry Osipenko static struct emc_timing *emc_find_timing(struct tegra_emc *emc,
397e34212c7SDmitry Osipenko 					  unsigned long rate)
398e34212c7SDmitry Osipenko {
399e34212c7SDmitry Osipenko 	struct emc_timing *timing = NULL;
400e34212c7SDmitry Osipenko 	unsigned int i;
401e34212c7SDmitry Osipenko 
402e34212c7SDmitry Osipenko 	for (i = 0; i < emc->num_timings; i++) {
403e34212c7SDmitry Osipenko 		if (emc->timings[i].rate >= rate) {
404e34212c7SDmitry Osipenko 			timing = &emc->timings[i];
405e34212c7SDmitry Osipenko 			break;
406e34212c7SDmitry Osipenko 		}
407e34212c7SDmitry Osipenko 	}
408e34212c7SDmitry Osipenko 
409e34212c7SDmitry Osipenko 	if (!timing) {
410e34212c7SDmitry Osipenko 		dev_err(emc->dev, "no timing for rate %lu\n", rate);
411e34212c7SDmitry Osipenko 		return NULL;
412e34212c7SDmitry Osipenko 	}
413e34212c7SDmitry Osipenko 
414e34212c7SDmitry Osipenko 	return timing;
415e34212c7SDmitry Osipenko }
416e34212c7SDmitry Osipenko 
417e34212c7SDmitry Osipenko static bool emc_dqs_preset(struct tegra_emc *emc, struct emc_timing *timing,
418e34212c7SDmitry Osipenko 			   bool *schmitt_to_vref)
419e34212c7SDmitry Osipenko {
420e34212c7SDmitry Osipenko 	bool preset = false;
421e34212c7SDmitry Osipenko 	u32 val;
422e34212c7SDmitry Osipenko 
423e34212c7SDmitry Osipenko 	if (timing->data[71] & EMC_XM2DQSPADCTRL2_VREF_ENABLE) {
424e34212c7SDmitry Osipenko 		val = readl_relaxed(emc->regs + EMC_XM2DQSPADCTRL2);
425e34212c7SDmitry Osipenko 
426e34212c7SDmitry Osipenko 		if (!(val & EMC_XM2DQSPADCTRL2_VREF_ENABLE)) {
427e34212c7SDmitry Osipenko 			val |= EMC_XM2DQSPADCTRL2_VREF_ENABLE;
428e34212c7SDmitry Osipenko 			writel_relaxed(val, emc->regs + EMC_XM2DQSPADCTRL2);
429e34212c7SDmitry Osipenko 
430e34212c7SDmitry Osipenko 			preset = true;
431e34212c7SDmitry Osipenko 		}
432e34212c7SDmitry Osipenko 	}
433e34212c7SDmitry Osipenko 
434e34212c7SDmitry Osipenko 	if (timing->data[78] & EMC_XM2DQSPADCTRL3_VREF_ENABLE) {
435e34212c7SDmitry Osipenko 		val = readl_relaxed(emc->regs + EMC_XM2DQSPADCTRL3);
436e34212c7SDmitry Osipenko 
437e34212c7SDmitry Osipenko 		if (!(val & EMC_XM2DQSPADCTRL3_VREF_ENABLE)) {
438e34212c7SDmitry Osipenko 			val |= EMC_XM2DQSPADCTRL3_VREF_ENABLE;
439e34212c7SDmitry Osipenko 			writel_relaxed(val, emc->regs + EMC_XM2DQSPADCTRL3);
440e34212c7SDmitry Osipenko 
441e34212c7SDmitry Osipenko 			preset = true;
442e34212c7SDmitry Osipenko 		}
443e34212c7SDmitry Osipenko 	}
444e34212c7SDmitry Osipenko 
445e34212c7SDmitry Osipenko 	if (timing->data[77] & EMC_XM2QUSEPADCTRL_IVREF_ENABLE) {
446e34212c7SDmitry Osipenko 		val = readl_relaxed(emc->regs + EMC_XM2QUSEPADCTRL);
447e34212c7SDmitry Osipenko 
448e34212c7SDmitry Osipenko 		if (!(val & EMC_XM2QUSEPADCTRL_IVREF_ENABLE)) {
449e34212c7SDmitry Osipenko 			val |= EMC_XM2QUSEPADCTRL_IVREF_ENABLE;
450e34212c7SDmitry Osipenko 			writel_relaxed(val, emc->regs + EMC_XM2QUSEPADCTRL);
451e34212c7SDmitry Osipenko 
452e34212c7SDmitry Osipenko 			*schmitt_to_vref = true;
453e34212c7SDmitry Osipenko 			preset = true;
454e34212c7SDmitry Osipenko 		}
455e34212c7SDmitry Osipenko 	}
456e34212c7SDmitry Osipenko 
457e34212c7SDmitry Osipenko 	return preset;
458e34212c7SDmitry Osipenko }
459e34212c7SDmitry Osipenko 
460e34212c7SDmitry Osipenko static int emc_prepare_mc_clk_cfg(struct tegra_emc *emc, unsigned long rate)
461e34212c7SDmitry Osipenko {
462e34212c7SDmitry Osipenko 	struct tegra_mc *mc = emc->mc;
463e34212c7SDmitry Osipenko 	unsigned int misc0_index = 16;
464e34212c7SDmitry Osipenko 	unsigned int i;
465e34212c7SDmitry Osipenko 	bool same;
466e34212c7SDmitry Osipenko 
467e34212c7SDmitry Osipenko 	for (i = 0; i < mc->num_timings; i++) {
468e34212c7SDmitry Osipenko 		if (mc->timings[i].rate != rate)
469e34212c7SDmitry Osipenko 			continue;
470e34212c7SDmitry Osipenko 
471e34212c7SDmitry Osipenko 		if (mc->timings[i].emem_data[misc0_index] & BIT(27))
472e34212c7SDmitry Osipenko 			same = true;
473e34212c7SDmitry Osipenko 		else
474e34212c7SDmitry Osipenko 			same = false;
475e34212c7SDmitry Osipenko 
476e34212c7SDmitry Osipenko 		return tegra20_clk_prepare_emc_mc_same_freq(emc->clk, same);
477e34212c7SDmitry Osipenko 	}
478e34212c7SDmitry Osipenko 
479e34212c7SDmitry Osipenko 	return -EINVAL;
480e34212c7SDmitry Osipenko }
481e34212c7SDmitry Osipenko 
482e34212c7SDmitry Osipenko static int emc_prepare_timing_change(struct tegra_emc *emc, unsigned long rate)
483e34212c7SDmitry Osipenko {
484e34212c7SDmitry Osipenko 	struct emc_timing *timing = emc_find_timing(emc, rate);
485e34212c7SDmitry Osipenko 	enum emc_dll_change dll_change;
486e34212c7SDmitry Osipenko 	enum emc_dram_type dram_type;
487e34212c7SDmitry Osipenko 	bool schmitt_to_vref = false;
488e34212c7SDmitry Osipenko 	unsigned int pre_wait = 0;
489e34212c7SDmitry Osipenko 	bool qrst_used = false;
490e34212c7SDmitry Osipenko 	unsigned int dram_num;
491e34212c7SDmitry Osipenko 	unsigned int i;
492e34212c7SDmitry Osipenko 	u32 fbio_cfg5;
493e34212c7SDmitry Osipenko 	u32 emc_dbg;
494e34212c7SDmitry Osipenko 	u32 val;
495e34212c7SDmitry Osipenko 	int err;
496e34212c7SDmitry Osipenko 
497e34212c7SDmitry Osipenko 	if (!timing || emc->bad_state)
498e34212c7SDmitry Osipenko 		return -EINVAL;
499e34212c7SDmitry Osipenko 
500e34212c7SDmitry Osipenko 	dev_dbg(emc->dev, "%s: using timing rate %lu for requested rate %lu\n",
501e34212c7SDmitry Osipenko 		__func__, timing->rate, rate);
502e34212c7SDmitry Osipenko 
503e34212c7SDmitry Osipenko 	emc->bad_state = true;
504e34212c7SDmitry Osipenko 
505e34212c7SDmitry Osipenko 	err = emc_prepare_mc_clk_cfg(emc, rate);
506e34212c7SDmitry Osipenko 	if (err) {
507e34212c7SDmitry Osipenko 		dev_err(emc->dev, "mc clock preparation failed: %d\n", err);
508e34212c7SDmitry Osipenko 		return err;
509e34212c7SDmitry Osipenko 	}
510e34212c7SDmitry Osipenko 
511e34212c7SDmitry Osipenko 	emc->vref_cal_toggle = false;
512e34212c7SDmitry Osipenko 	emc->mc_override = mc_readl(emc->mc, MC_EMEM_ARB_OVERRIDE);
513e34212c7SDmitry Osipenko 	emc->emc_cfg = readl_relaxed(emc->regs + EMC_CFG);
514e34212c7SDmitry Osipenko 	emc_dbg = readl_relaxed(emc->regs + EMC_DBG);
515e34212c7SDmitry Osipenko 
516e34212c7SDmitry Osipenko 	if (emc->dll_on == !!(timing->emc_mode_1 & 0x1))
517e34212c7SDmitry Osipenko 		dll_change = DLL_CHANGE_NONE;
518e34212c7SDmitry Osipenko 	else if (timing->emc_mode_1 & 0x1)
519e34212c7SDmitry Osipenko 		dll_change = DLL_CHANGE_ON;
520e34212c7SDmitry Osipenko 	else
521e34212c7SDmitry Osipenko 		dll_change = DLL_CHANGE_OFF;
522e34212c7SDmitry Osipenko 
523e34212c7SDmitry Osipenko 	emc->dll_on = !!(timing->emc_mode_1 & 0x1);
524e34212c7SDmitry Osipenko 
525e34212c7SDmitry Osipenko 	if (timing->data[80] && !readl_relaxed(emc->regs + EMC_ZCAL_INTERVAL))
526e34212c7SDmitry Osipenko 		emc->zcal_long = true;
527e34212c7SDmitry Osipenko 	else
528e34212c7SDmitry Osipenko 		emc->zcal_long = false;
529e34212c7SDmitry Osipenko 
530e34212c7SDmitry Osipenko 	fbio_cfg5 = readl_relaxed(emc->regs + EMC_FBIO_CFG5);
531e34212c7SDmitry Osipenko 	dram_type = fbio_cfg5 & EMC_FBIO_CFG5_DRAM_TYPE_MASK;
532e34212c7SDmitry Osipenko 
533e34212c7SDmitry Osipenko 	dram_num = tegra_mc_get_emem_device_count(emc->mc);
534e34212c7SDmitry Osipenko 
535e34212c7SDmitry Osipenko 	/* disable dynamic self-refresh */
536e34212c7SDmitry Osipenko 	if (emc->emc_cfg & EMC_CFG_DYN_SREF_ENABLE) {
537e34212c7SDmitry Osipenko 		emc->emc_cfg &= ~EMC_CFG_DYN_SREF_ENABLE;
538e34212c7SDmitry Osipenko 		writel_relaxed(emc->emc_cfg, emc->regs + EMC_CFG);
539e34212c7SDmitry Osipenko 
540e34212c7SDmitry Osipenko 		pre_wait = 5;
541e34212c7SDmitry Osipenko 	}
542e34212c7SDmitry Osipenko 
543e34212c7SDmitry Osipenko 	/* update MC arbiter settings */
544e34212c7SDmitry Osipenko 	val = mc_readl(emc->mc, MC_EMEM_ARB_OUTSTANDING_REQ);
545e34212c7SDmitry Osipenko 	if (!(val & MC_EMEM_ARB_OUTSTANDING_REQ_HOLDOFF_OVERRIDE) ||
546e34212c7SDmitry Osipenko 	    ((val & MC_EMEM_ARB_OUTSTANDING_REQ_MAX_MASK) > 0x50)) {
547e34212c7SDmitry Osipenko 
548e34212c7SDmitry Osipenko 		val = MC_EMEM_ARB_OUTSTANDING_REQ_LIMIT_ENABLE |
549e34212c7SDmitry Osipenko 		      MC_EMEM_ARB_OUTSTANDING_REQ_HOLDOFF_OVERRIDE | 0x50;
550e34212c7SDmitry Osipenko 		mc_writel(emc->mc, val, MC_EMEM_ARB_OUTSTANDING_REQ);
551e34212c7SDmitry Osipenko 		mc_writel(emc->mc, MC_TIMING_UPDATE, MC_TIMING_CONTROL);
552e34212c7SDmitry Osipenko 	}
553e34212c7SDmitry Osipenko 
554e34212c7SDmitry Osipenko 	if (emc->mc_override & MC_EMEM_ARB_OVERRIDE_EACK_MASK)
555e34212c7SDmitry Osipenko 		mc_writel(emc->mc,
556e34212c7SDmitry Osipenko 			  emc->mc_override & ~MC_EMEM_ARB_OVERRIDE_EACK_MASK,
557e34212c7SDmitry Osipenko 			  MC_EMEM_ARB_OVERRIDE);
558e34212c7SDmitry Osipenko 
559e34212c7SDmitry Osipenko 	/* check DQ/DQS VREF delay */
560e34212c7SDmitry Osipenko 	if (emc_dqs_preset(emc, timing, &schmitt_to_vref)) {
561e34212c7SDmitry Osipenko 		if (pre_wait < 3)
562e34212c7SDmitry Osipenko 			pre_wait = 3;
563e34212c7SDmitry Osipenko 	}
564e34212c7SDmitry Osipenko 
565e34212c7SDmitry Osipenko 	if (pre_wait) {
566e34212c7SDmitry Osipenko 		err = emc_seq_update_timing(emc);
567e34212c7SDmitry Osipenko 		if (err)
568e34212c7SDmitry Osipenko 			return err;
569e34212c7SDmitry Osipenko 
570e34212c7SDmitry Osipenko 		udelay(pre_wait);
571e34212c7SDmitry Osipenko 	}
572e34212c7SDmitry Osipenko 
573e34212c7SDmitry Osipenko 	/* disable auto-calibration if VREF mode is switching */
574e34212c7SDmitry Osipenko 	if (timing->emc_auto_cal_interval) {
575e34212c7SDmitry Osipenko 		val = readl_relaxed(emc->regs + EMC_XM2COMPPADCTRL);
576e34212c7SDmitry Osipenko 		val ^= timing->data[74];
577e34212c7SDmitry Osipenko 
578e34212c7SDmitry Osipenko 		if (val & EMC_XM2COMPPADCTRL_VREF_CAL_ENABLE) {
579e34212c7SDmitry Osipenko 			writel_relaxed(0, emc->regs + EMC_AUTO_CAL_INTERVAL);
580e34212c7SDmitry Osipenko 
581e34212c7SDmitry Osipenko 			err = readl_relaxed_poll_timeout_atomic(
582e34212c7SDmitry Osipenko 				emc->regs + EMC_AUTO_CAL_STATUS, val,
583e34212c7SDmitry Osipenko 				!(val & EMC_AUTO_CAL_STATUS_ACTIVE), 1, 300);
584e34212c7SDmitry Osipenko 			if (err) {
585e34212c7SDmitry Osipenko 				dev_err(emc->dev,
5865e5eca66SDmitry Osipenko 					"auto-cal finish timeout: %d\n", err);
587e34212c7SDmitry Osipenko 				return err;
588e34212c7SDmitry Osipenko 			}
589e34212c7SDmitry Osipenko 
590e34212c7SDmitry Osipenko 			emc->vref_cal_toggle = true;
591e34212c7SDmitry Osipenko 		}
592e34212c7SDmitry Osipenko 	}
593e34212c7SDmitry Osipenko 
594e34212c7SDmitry Osipenko 	/* program shadow registers */
595e34212c7SDmitry Osipenko 	for (i = 0; i < ARRAY_SIZE(timing->data); i++) {
596e34212c7SDmitry Osipenko 		/* EMC_XM2CLKPADCTRL should be programmed separately */
597e34212c7SDmitry Osipenko 		if (i != 73)
598e34212c7SDmitry Osipenko 			writel_relaxed(timing->data[i],
599e34212c7SDmitry Osipenko 				       emc->regs + emc_timing_registers[i]);
600e34212c7SDmitry Osipenko 	}
601e34212c7SDmitry Osipenko 
602e34212c7SDmitry Osipenko 	err = tegra_mc_write_emem_configuration(emc->mc, timing->rate);
603e34212c7SDmitry Osipenko 	if (err)
604e34212c7SDmitry Osipenko 		return err;
605e34212c7SDmitry Osipenko 
606e34212c7SDmitry Osipenko 	/* DDR3: predict MRS long wait count */
607e34212c7SDmitry Osipenko 	if (dram_type == DRAM_TYPE_DDR3 && dll_change == DLL_CHANGE_ON) {
608e34212c7SDmitry Osipenko 		u32 cnt = 512;
609e34212c7SDmitry Osipenko 
610e34212c7SDmitry Osipenko 		if (emc->zcal_long)
611e34212c7SDmitry Osipenko 			cnt -= dram_num * 256;
612e34212c7SDmitry Osipenko 
613e34212c7SDmitry Osipenko 		val = timing->data[82] & EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK;
614e34212c7SDmitry Osipenko 		if (cnt < val)
615e34212c7SDmitry Osipenko 			cnt = val;
616e34212c7SDmitry Osipenko 
617e34212c7SDmitry Osipenko 		val = timing->data[82] & ~EMC_MRS_WAIT_CNT_LONG_WAIT_MASK;
618e34212c7SDmitry Osipenko 		val |= (cnt << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT) &
619e34212c7SDmitry Osipenko 			EMC_MRS_WAIT_CNT_LONG_WAIT_MASK;
620e34212c7SDmitry Osipenko 
621e34212c7SDmitry Osipenko 		writel_relaxed(val, emc->regs + EMC_MRS_WAIT_CNT);
622e34212c7SDmitry Osipenko 	}
623e34212c7SDmitry Osipenko 
624e34212c7SDmitry Osipenko 	/* this read also completes the writes */
625e34212c7SDmitry Osipenko 	val = readl_relaxed(emc->regs + EMC_SEL_DPD_CTRL);
626e34212c7SDmitry Osipenko 
627e34212c7SDmitry Osipenko 	if (!(val & EMC_SEL_DPD_CTRL_QUSE_DPD_ENABLE) && schmitt_to_vref) {
628e34212c7SDmitry Osipenko 		u32 cur_mode, new_mode;
629e34212c7SDmitry Osipenko 
630e34212c7SDmitry Osipenko 		cur_mode = fbio_cfg5 & EMC_CFG5_QUSE_MODE_MASK;
631e34212c7SDmitry Osipenko 		cur_mode >>= EMC_CFG5_QUSE_MODE_SHIFT;
632e34212c7SDmitry Osipenko 
633e34212c7SDmitry Osipenko 		new_mode = timing->data[39] & EMC_CFG5_QUSE_MODE_MASK;
634e34212c7SDmitry Osipenko 		new_mode >>= EMC_CFG5_QUSE_MODE_SHIFT;
635e34212c7SDmitry Osipenko 
636e34212c7SDmitry Osipenko 		if ((cur_mode != EMC_CFG5_QUSE_MODE_PULSE_INTERN &&
637e34212c7SDmitry Osipenko 		     cur_mode != EMC_CFG5_QUSE_MODE_INTERNAL_LPBK) ||
638e34212c7SDmitry Osipenko 		    (new_mode != EMC_CFG5_QUSE_MODE_PULSE_INTERN &&
639e34212c7SDmitry Osipenko 		     new_mode != EMC_CFG5_QUSE_MODE_INTERNAL_LPBK))
640e34212c7SDmitry Osipenko 			qrst_used = true;
641e34212c7SDmitry Osipenko 	}
642e34212c7SDmitry Osipenko 
643e34212c7SDmitry Osipenko 	/* flow control marker 1 */
644e34212c7SDmitry Osipenko 	writel_relaxed(0x1, emc->regs + EMC_STALL_THEN_EXE_BEFORE_CLKCHANGE);
645e34212c7SDmitry Osipenko 
646e34212c7SDmitry Osipenko 	/* enable periodic reset */
647e34212c7SDmitry Osipenko 	if (qrst_used) {
648e34212c7SDmitry Osipenko 		writel_relaxed(emc_dbg | EMC_DBG_WRITE_MUX_ACTIVE,
649e34212c7SDmitry Osipenko 			       emc->regs + EMC_DBG);
650e34212c7SDmitry Osipenko 		writel_relaxed(emc->emc_cfg | EMC_CFG_PERIODIC_QRST,
651e34212c7SDmitry Osipenko 			       emc->regs + EMC_CFG);
652e34212c7SDmitry Osipenko 		writel_relaxed(emc_dbg, emc->regs + EMC_DBG);
653e34212c7SDmitry Osipenko 	}
654e34212c7SDmitry Osipenko 
655e34212c7SDmitry Osipenko 	/* disable auto-refresh to save time after clock change */
656e34212c7SDmitry Osipenko 	writel_relaxed(EMC_REFCTRL_DISABLE_ALL(dram_num),
657e34212c7SDmitry Osipenko 		       emc->regs + EMC_REFCTRL);
658e34212c7SDmitry Osipenko 
659e34212c7SDmitry Osipenko 	/* turn off DLL and enter self-refresh on DDR3 */
660e34212c7SDmitry Osipenko 	if (dram_type == DRAM_TYPE_DDR3) {
661e34212c7SDmitry Osipenko 		if (dll_change == DLL_CHANGE_OFF)
662e34212c7SDmitry Osipenko 			writel_relaxed(timing->emc_mode_1,
663e34212c7SDmitry Osipenko 				       emc->regs + EMC_EMRS);
664e34212c7SDmitry Osipenko 
665e34212c7SDmitry Osipenko 		writel_relaxed(DRAM_BROADCAST(dram_num) |
666e34212c7SDmitry Osipenko 			       EMC_SELF_REF_CMD_ENABLED,
667e34212c7SDmitry Osipenko 			       emc->regs + EMC_SELF_REF);
668e34212c7SDmitry Osipenko 	}
669e34212c7SDmitry Osipenko 
670e34212c7SDmitry Osipenko 	/* flow control marker 2 */
671e34212c7SDmitry Osipenko 	writel_relaxed(0x1, emc->regs + EMC_STALL_THEN_EXE_AFTER_CLKCHANGE);
672e34212c7SDmitry Osipenko 
673e34212c7SDmitry Osipenko 	/* enable write-active MUX, update unshadowed pad control */
674e34212c7SDmitry Osipenko 	writel_relaxed(emc_dbg | EMC_DBG_WRITE_MUX_ACTIVE, emc->regs + EMC_DBG);
675e34212c7SDmitry Osipenko 	writel_relaxed(timing->data[73], emc->regs + EMC_XM2CLKPADCTRL);
676e34212c7SDmitry Osipenko 
677e34212c7SDmitry Osipenko 	/* restore periodic QRST and disable write-active MUX */
678e34212c7SDmitry Osipenko 	val = !!(emc->emc_cfg & EMC_CFG_PERIODIC_QRST);
679e34212c7SDmitry Osipenko 	if (qrst_used || timing->emc_cfg_periodic_qrst != val) {
680e34212c7SDmitry Osipenko 		if (timing->emc_cfg_periodic_qrst)
681e34212c7SDmitry Osipenko 			emc->emc_cfg |= EMC_CFG_PERIODIC_QRST;
682e34212c7SDmitry Osipenko 		else
683e34212c7SDmitry Osipenko 			emc->emc_cfg &= ~EMC_CFG_PERIODIC_QRST;
684e34212c7SDmitry Osipenko 
685e34212c7SDmitry Osipenko 		writel_relaxed(emc->emc_cfg, emc->regs + EMC_CFG);
686e34212c7SDmitry Osipenko 	}
687e34212c7SDmitry Osipenko 	writel_relaxed(emc_dbg, emc->regs + EMC_DBG);
688e34212c7SDmitry Osipenko 
689e34212c7SDmitry Osipenko 	/* exit self-refresh on DDR3 */
690e34212c7SDmitry Osipenko 	if (dram_type == DRAM_TYPE_DDR3)
691e34212c7SDmitry Osipenko 		writel_relaxed(DRAM_BROADCAST(dram_num),
692e34212c7SDmitry Osipenko 			       emc->regs + EMC_SELF_REF);
693e34212c7SDmitry Osipenko 
694e34212c7SDmitry Osipenko 	/* set DRAM-mode registers */
695e34212c7SDmitry Osipenko 	if (dram_type == DRAM_TYPE_DDR3) {
696e34212c7SDmitry Osipenko 		if (timing->emc_mode_1 != emc->emc_mode_1)
697e34212c7SDmitry Osipenko 			writel_relaxed(timing->emc_mode_1,
698e34212c7SDmitry Osipenko 				       emc->regs + EMC_EMRS);
699e34212c7SDmitry Osipenko 
700e34212c7SDmitry Osipenko 		if (timing->emc_mode_2 != emc->emc_mode_2)
701e34212c7SDmitry Osipenko 			writel_relaxed(timing->emc_mode_2,
702e34212c7SDmitry Osipenko 				       emc->regs + EMC_EMRS);
703e34212c7SDmitry Osipenko 
704e34212c7SDmitry Osipenko 		if (timing->emc_mode_reset != emc->emc_mode_reset ||
705e34212c7SDmitry Osipenko 		    dll_change == DLL_CHANGE_ON) {
706e34212c7SDmitry Osipenko 			val = timing->emc_mode_reset;
707e34212c7SDmitry Osipenko 			if (dll_change == DLL_CHANGE_ON) {
708e34212c7SDmitry Osipenko 				val |= EMC_MODE_SET_DLL_RESET;
709e34212c7SDmitry Osipenko 				val |= EMC_MODE_SET_LONG_CNT;
710e34212c7SDmitry Osipenko 			} else {
711e34212c7SDmitry Osipenko 				val &= ~EMC_MODE_SET_DLL_RESET;
712e34212c7SDmitry Osipenko 			}
713e34212c7SDmitry Osipenko 			writel_relaxed(val, emc->regs + EMC_MRS);
714e34212c7SDmitry Osipenko 		}
715e34212c7SDmitry Osipenko 	} else {
716e34212c7SDmitry Osipenko 		if (timing->emc_mode_2 != emc->emc_mode_2)
717e34212c7SDmitry Osipenko 			writel_relaxed(timing->emc_mode_2,
718e34212c7SDmitry Osipenko 				       emc->regs + EMC_MRW);
719e34212c7SDmitry Osipenko 
720e34212c7SDmitry Osipenko 		if (timing->emc_mode_1 != emc->emc_mode_1)
721e34212c7SDmitry Osipenko 			writel_relaxed(timing->emc_mode_1,
722e34212c7SDmitry Osipenko 				       emc->regs + EMC_MRW);
723e34212c7SDmitry Osipenko 	}
724e34212c7SDmitry Osipenko 
725e34212c7SDmitry Osipenko 	emc->emc_mode_1 = timing->emc_mode_1;
726e34212c7SDmitry Osipenko 	emc->emc_mode_2 = timing->emc_mode_2;
727e34212c7SDmitry Osipenko 	emc->emc_mode_reset = timing->emc_mode_reset;
728e34212c7SDmitry Osipenko 
729e34212c7SDmitry Osipenko 	/* issue ZCAL command if turning ZCAL on */
730e34212c7SDmitry Osipenko 	if (emc->zcal_long) {
731e34212c7SDmitry Osipenko 		writel_relaxed(EMC_ZQ_CAL_LONG_CMD_DEV0,
732e34212c7SDmitry Osipenko 			       emc->regs + EMC_ZQ_CAL);
733e34212c7SDmitry Osipenko 
734e34212c7SDmitry Osipenko 		if (dram_num > 1)
735e34212c7SDmitry Osipenko 			writel_relaxed(EMC_ZQ_CAL_LONG_CMD_DEV1,
736e34212c7SDmitry Osipenko 				       emc->regs + EMC_ZQ_CAL);
737e34212c7SDmitry Osipenko 	}
738e34212c7SDmitry Osipenko 
739e34212c7SDmitry Osipenko 	/* flow control marker 3 */
740e34212c7SDmitry Osipenko 	writel_relaxed(0x1, emc->regs + EMC_UNSTALL_RW_AFTER_CLKCHANGE);
741e34212c7SDmitry Osipenko 
7420f8bb9daSDmitry Osipenko 	/*
7430f8bb9daSDmitry Osipenko 	 * Read and discard an arbitrary MC register (Note: EMC registers
7440f8bb9daSDmitry Osipenko 	 * can't be used) to ensure the register writes are completed.
7450f8bb9daSDmitry Osipenko 	 */
7460f8bb9daSDmitry Osipenko 	mc_readl(emc->mc, MC_EMEM_ARB_OVERRIDE);
7470f8bb9daSDmitry Osipenko 
748e34212c7SDmitry Osipenko 	return 0;
749e34212c7SDmitry Osipenko }
750e34212c7SDmitry Osipenko 
751e34212c7SDmitry Osipenko static int emc_complete_timing_change(struct tegra_emc *emc,
752e34212c7SDmitry Osipenko 				      unsigned long rate)
753e34212c7SDmitry Osipenko {
754930c6818SDmitry Osipenko 	struct emc_timing *timing = emc_find_timing(emc, rate);
755930c6818SDmitry Osipenko 	unsigned int dram_num;
756930c6818SDmitry Osipenko 	int err;
757930c6818SDmitry Osipenko 	u32 v;
758e34212c7SDmitry Osipenko 
759930c6818SDmitry Osipenko 	err = readl_relaxed_poll_timeout_atomic(emc->regs + EMC_INTSTATUS, v,
760930c6818SDmitry Osipenko 						v & EMC_CLKCHANGE_COMPLETE_INT,
761930c6818SDmitry Osipenko 						1, 100);
762930c6818SDmitry Osipenko 	if (err) {
763930c6818SDmitry Osipenko 		dev_err(emc->dev, "emc-car handshake timeout: %d\n", err);
764930c6818SDmitry Osipenko 		return err;
765e34212c7SDmitry Osipenko 	}
766e34212c7SDmitry Osipenko 
767930c6818SDmitry Osipenko 	/* re-enable auto-refresh */
768930c6818SDmitry Osipenko 	dram_num = tegra_mc_get_emem_device_count(emc->mc);
769930c6818SDmitry Osipenko 	writel_relaxed(EMC_REFCTRL_ENABLE_ALL(dram_num),
770930c6818SDmitry Osipenko 		       emc->regs + EMC_REFCTRL);
77151bb73f9SDmitry Osipenko 
772930c6818SDmitry Osipenko 	/* restore auto-calibration */
773930c6818SDmitry Osipenko 	if (emc->vref_cal_toggle)
774930c6818SDmitry Osipenko 		writel_relaxed(timing->emc_auto_cal_interval,
775930c6818SDmitry Osipenko 			       emc->regs + EMC_AUTO_CAL_INTERVAL);
776930c6818SDmitry Osipenko 
777930c6818SDmitry Osipenko 	/* restore dynamic self-refresh */
778930c6818SDmitry Osipenko 	if (timing->emc_cfg_dyn_self_ref) {
779930c6818SDmitry Osipenko 		emc->emc_cfg |= EMC_CFG_DYN_SREF_ENABLE;
780930c6818SDmitry Osipenko 		writel_relaxed(emc->emc_cfg, emc->regs + EMC_CFG);
781930c6818SDmitry Osipenko 	}
782930c6818SDmitry Osipenko 
783930c6818SDmitry Osipenko 	/* set number of clocks to wait after each ZQ command */
784930c6818SDmitry Osipenko 	if (emc->zcal_long)
785930c6818SDmitry Osipenko 		writel_relaxed(timing->emc_zcal_cnt_long,
786930c6818SDmitry Osipenko 			       emc->regs + EMC_ZCAL_WAIT_CNT);
787930c6818SDmitry Osipenko 
788930c6818SDmitry Osipenko 	/* wait for writes to settle */
789930c6818SDmitry Osipenko 	udelay(2);
790930c6818SDmitry Osipenko 
791930c6818SDmitry Osipenko 	/* update restored timing */
792930c6818SDmitry Osipenko 	err = emc_seq_update_timing(emc);
793930c6818SDmitry Osipenko 	if (!err)
794930c6818SDmitry Osipenko 		emc->bad_state = false;
795930c6818SDmitry Osipenko 
796930c6818SDmitry Osipenko 	/* restore early ACK */
797930c6818SDmitry Osipenko 	mc_writel(emc->mc, emc->mc_override, MC_EMEM_ARB_OVERRIDE);
798930c6818SDmitry Osipenko 
799930c6818SDmitry Osipenko 	return err;
800e34212c7SDmitry Osipenko }
801e34212c7SDmitry Osipenko 
802e34212c7SDmitry Osipenko static int emc_unprepare_timing_change(struct tegra_emc *emc,
803e34212c7SDmitry Osipenko 				       unsigned long rate)
804e34212c7SDmitry Osipenko {
80551bb73f9SDmitry Osipenko 	if (!emc->bad_state) {
806e34212c7SDmitry Osipenko 		/* shouldn't ever happen in practice */
807e34212c7SDmitry Osipenko 		dev_err(emc->dev, "timing configuration can't be reverted\n");
808e34212c7SDmitry Osipenko 		emc->bad_state = true;
809e34212c7SDmitry Osipenko 	}
810e34212c7SDmitry Osipenko 
811e34212c7SDmitry Osipenko 	return 0;
812e34212c7SDmitry Osipenko }
813e34212c7SDmitry Osipenko 
814e34212c7SDmitry Osipenko static int emc_clk_change_notify(struct notifier_block *nb,
815e34212c7SDmitry Osipenko 				 unsigned long msg, void *data)
816e34212c7SDmitry Osipenko {
817e34212c7SDmitry Osipenko 	struct tegra_emc *emc = container_of(nb, struct tegra_emc, clk_nb);
818e34212c7SDmitry Osipenko 	struct clk_notifier_data *cnd = data;
819e34212c7SDmitry Osipenko 	int err;
820e34212c7SDmitry Osipenko 
821e34212c7SDmitry Osipenko 	switch (msg) {
822e34212c7SDmitry Osipenko 	case PRE_RATE_CHANGE:
8230f8bb9daSDmitry Osipenko 		/*
8240f8bb9daSDmitry Osipenko 		 * Disable interrupt since read accesses are prohibited after
8250f8bb9daSDmitry Osipenko 		 * stalling.
8260f8bb9daSDmitry Osipenko 		 */
8270f8bb9daSDmitry Osipenko 		disable_irq(emc->irq);
828e34212c7SDmitry Osipenko 		err = emc_prepare_timing_change(emc, cnd->new_rate);
8290f8bb9daSDmitry Osipenko 		enable_irq(emc->irq);
830e34212c7SDmitry Osipenko 		break;
831e34212c7SDmitry Osipenko 
832e34212c7SDmitry Osipenko 	case ABORT_RATE_CHANGE:
833e34212c7SDmitry Osipenko 		err = emc_unprepare_timing_change(emc, cnd->old_rate);
834e34212c7SDmitry Osipenko 		break;
835e34212c7SDmitry Osipenko 
836e34212c7SDmitry Osipenko 	case POST_RATE_CHANGE:
837e34212c7SDmitry Osipenko 		err = emc_complete_timing_change(emc, cnd->new_rate);
838e34212c7SDmitry Osipenko 		break;
839e34212c7SDmitry Osipenko 
840e34212c7SDmitry Osipenko 	default:
841e34212c7SDmitry Osipenko 		return NOTIFY_DONE;
842e34212c7SDmitry Osipenko 	}
843e34212c7SDmitry Osipenko 
844e34212c7SDmitry Osipenko 	return notifier_from_errno(err);
845e34212c7SDmitry Osipenko }
846e34212c7SDmitry Osipenko 
847e34212c7SDmitry Osipenko static int load_one_timing_from_dt(struct tegra_emc *emc,
848e34212c7SDmitry Osipenko 				   struct emc_timing *timing,
849e34212c7SDmitry Osipenko 				   struct device_node *node)
850e34212c7SDmitry Osipenko {
851e34212c7SDmitry Osipenko 	u32 value;
852e34212c7SDmitry Osipenko 	int err;
853e34212c7SDmitry Osipenko 
854e34212c7SDmitry Osipenko 	err = of_property_read_u32(node, "clock-frequency", &value);
855e34212c7SDmitry Osipenko 	if (err) {
856e34212c7SDmitry Osipenko 		dev_err(emc->dev, "timing %pOF: failed to read rate: %d\n",
857e34212c7SDmitry Osipenko 			node, err);
858e34212c7SDmitry Osipenko 		return err;
859e34212c7SDmitry Osipenko 	}
860e34212c7SDmitry Osipenko 
861e34212c7SDmitry Osipenko 	timing->rate = value;
862e34212c7SDmitry Osipenko 
863e34212c7SDmitry Osipenko 	err = of_property_read_u32_array(node, "nvidia,emc-configuration",
864e34212c7SDmitry Osipenko 					 timing->data,
865e34212c7SDmitry Osipenko 					 ARRAY_SIZE(emc_timing_registers));
866e34212c7SDmitry Osipenko 	if (err) {
867e34212c7SDmitry Osipenko 		dev_err(emc->dev,
868e34212c7SDmitry Osipenko 			"timing %pOF: failed to read emc timing data: %d\n",
869e34212c7SDmitry Osipenko 			node, err);
870e34212c7SDmitry Osipenko 		return err;
871e34212c7SDmitry Osipenko 	}
872e34212c7SDmitry Osipenko 
873e34212c7SDmitry Osipenko #define EMC_READ_BOOL(prop, dtprop) \
874e34212c7SDmitry Osipenko 	timing->prop = of_property_read_bool(node, dtprop);
875e34212c7SDmitry Osipenko 
876e34212c7SDmitry Osipenko #define EMC_READ_U32(prop, dtprop) \
877e34212c7SDmitry Osipenko 	err = of_property_read_u32(node, dtprop, &timing->prop); \
878e34212c7SDmitry Osipenko 	if (err) { \
879e34212c7SDmitry Osipenko 		dev_err(emc->dev, \
880e34212c7SDmitry Osipenko 			"timing %pOFn: failed to read " #prop ": %d\n", \
881e34212c7SDmitry Osipenko 			node, err); \
882e34212c7SDmitry Osipenko 		return err; \
883e34212c7SDmitry Osipenko 	}
884e34212c7SDmitry Osipenko 
885e34212c7SDmitry Osipenko 	EMC_READ_U32(emc_auto_cal_interval, "nvidia,emc-auto-cal-interval")
886e34212c7SDmitry Osipenko 	EMC_READ_U32(emc_mode_1, "nvidia,emc-mode-1")
887e34212c7SDmitry Osipenko 	EMC_READ_U32(emc_mode_2, "nvidia,emc-mode-2")
888e34212c7SDmitry Osipenko 	EMC_READ_U32(emc_mode_reset, "nvidia,emc-mode-reset")
889e34212c7SDmitry Osipenko 	EMC_READ_U32(emc_zcal_cnt_long, "nvidia,emc-zcal-cnt-long")
890e34212c7SDmitry Osipenko 	EMC_READ_BOOL(emc_cfg_dyn_self_ref, "nvidia,emc-cfg-dyn-self-ref")
891e34212c7SDmitry Osipenko 	EMC_READ_BOOL(emc_cfg_periodic_qrst, "nvidia,emc-cfg-periodic-qrst")
892e34212c7SDmitry Osipenko 
893e34212c7SDmitry Osipenko #undef EMC_READ_U32
894e34212c7SDmitry Osipenko #undef EMC_READ_BOOL
895e34212c7SDmitry Osipenko 
896e34212c7SDmitry Osipenko 	dev_dbg(emc->dev, "%s: %pOF: rate %lu\n", __func__, node, timing->rate);
897e34212c7SDmitry Osipenko 
898e34212c7SDmitry Osipenko 	return 0;
899e34212c7SDmitry Osipenko }
900e34212c7SDmitry Osipenko 
901e34212c7SDmitry Osipenko static int cmp_timings(const void *_a, const void *_b)
902e34212c7SDmitry Osipenko {
903e34212c7SDmitry Osipenko 	const struct emc_timing *a = _a;
904e34212c7SDmitry Osipenko 	const struct emc_timing *b = _b;
905e34212c7SDmitry Osipenko 
906e34212c7SDmitry Osipenko 	if (a->rate < b->rate)
907e34212c7SDmitry Osipenko 		return -1;
908e34212c7SDmitry Osipenko 
909e34212c7SDmitry Osipenko 	if (a->rate > b->rate)
910e34212c7SDmitry Osipenko 		return 1;
911e34212c7SDmitry Osipenko 
912e34212c7SDmitry Osipenko 	return 0;
913e34212c7SDmitry Osipenko }
914e34212c7SDmitry Osipenko 
915e34212c7SDmitry Osipenko static int emc_check_mc_timings(struct tegra_emc *emc)
916e34212c7SDmitry Osipenko {
917e34212c7SDmitry Osipenko 	struct tegra_mc *mc = emc->mc;
918e34212c7SDmitry Osipenko 	unsigned int i;
919e34212c7SDmitry Osipenko 
920e34212c7SDmitry Osipenko 	if (emc->num_timings != mc->num_timings) {
921e34212c7SDmitry Osipenko 		dev_err(emc->dev, "emc/mc timings number mismatch: %u %u\n",
922e34212c7SDmitry Osipenko 			emc->num_timings, mc->num_timings);
923e34212c7SDmitry Osipenko 		return -EINVAL;
924e34212c7SDmitry Osipenko 	}
925e34212c7SDmitry Osipenko 
926e34212c7SDmitry Osipenko 	for (i = 0; i < mc->num_timings; i++) {
927e34212c7SDmitry Osipenko 		if (emc->timings[i].rate != mc->timings[i].rate) {
928e34212c7SDmitry Osipenko 			dev_err(emc->dev,
929e34212c7SDmitry Osipenko 				"emc/mc timing rate mismatch: %lu %lu\n",
930e34212c7SDmitry Osipenko 				emc->timings[i].rate, mc->timings[i].rate);
931e34212c7SDmitry Osipenko 			return -EINVAL;
932e34212c7SDmitry Osipenko 		}
933e34212c7SDmitry Osipenko 	}
934e34212c7SDmitry Osipenko 
935e34212c7SDmitry Osipenko 	return 0;
936e34212c7SDmitry Osipenko }
937e34212c7SDmitry Osipenko 
938e34212c7SDmitry Osipenko static int emc_load_timings_from_dt(struct tegra_emc *emc,
939e34212c7SDmitry Osipenko 				    struct device_node *node)
940e34212c7SDmitry Osipenko {
941e34212c7SDmitry Osipenko 	struct device_node *child;
942e34212c7SDmitry Osipenko 	struct emc_timing *timing;
943e34212c7SDmitry Osipenko 	int child_count;
944e34212c7SDmitry Osipenko 	int err;
945e34212c7SDmitry Osipenko 
946e34212c7SDmitry Osipenko 	child_count = of_get_child_count(node);
947e34212c7SDmitry Osipenko 	if (!child_count) {
948e34212c7SDmitry Osipenko 		dev_err(emc->dev, "no memory timings in: %pOF\n", node);
949e34212c7SDmitry Osipenko 		return -EINVAL;
950e34212c7SDmitry Osipenko 	}
951e34212c7SDmitry Osipenko 
952e34212c7SDmitry Osipenko 	emc->timings = devm_kcalloc(emc->dev, child_count, sizeof(*timing),
953e34212c7SDmitry Osipenko 				    GFP_KERNEL);
954e34212c7SDmitry Osipenko 	if (!emc->timings)
955e34212c7SDmitry Osipenko 		return -ENOMEM;
956e34212c7SDmitry Osipenko 
957e34212c7SDmitry Osipenko 	emc->num_timings = child_count;
958e34212c7SDmitry Osipenko 	timing = emc->timings;
959e34212c7SDmitry Osipenko 
960e34212c7SDmitry Osipenko 	for_each_child_of_node(node, child) {
961e34212c7SDmitry Osipenko 		err = load_one_timing_from_dt(emc, timing++, child);
962e34212c7SDmitry Osipenko 		if (err) {
963e34212c7SDmitry Osipenko 			of_node_put(child);
964e34212c7SDmitry Osipenko 			return err;
965e34212c7SDmitry Osipenko 		}
966e34212c7SDmitry Osipenko 	}
967e34212c7SDmitry Osipenko 
968e34212c7SDmitry Osipenko 	sort(emc->timings, emc->num_timings, sizeof(*timing), cmp_timings,
969e34212c7SDmitry Osipenko 	     NULL);
970e34212c7SDmitry Osipenko 
971e34212c7SDmitry Osipenko 	err = emc_check_mc_timings(emc);
972e34212c7SDmitry Osipenko 	if (err)
973e34212c7SDmitry Osipenko 		return err;
974e34212c7SDmitry Osipenko 
975e34212c7SDmitry Osipenko 	dev_info(emc->dev,
976e34212c7SDmitry Osipenko 		 "got %u timings for RAM code %u (min %luMHz max %luMHz)\n",
977e34212c7SDmitry Osipenko 		 emc->num_timings,
978e34212c7SDmitry Osipenko 		 tegra_read_ram_code(),
979e34212c7SDmitry Osipenko 		 emc->timings[0].rate / 1000000,
980e34212c7SDmitry Osipenko 		 emc->timings[emc->num_timings - 1].rate / 1000000);
981e34212c7SDmitry Osipenko 
982e34212c7SDmitry Osipenko 	return 0;
983e34212c7SDmitry Osipenko }
984e34212c7SDmitry Osipenko 
985e34212c7SDmitry Osipenko static struct device_node *emc_find_node_by_ram_code(struct device *dev)
986e34212c7SDmitry Osipenko {
987e34212c7SDmitry Osipenko 	struct device_node *np;
988e34212c7SDmitry Osipenko 	u32 value, ram_code;
989e34212c7SDmitry Osipenko 	int err;
990e34212c7SDmitry Osipenko 
991e34212c7SDmitry Osipenko 	ram_code = tegra_read_ram_code();
992e34212c7SDmitry Osipenko 
993e34212c7SDmitry Osipenko 	for_each_child_of_node(dev->of_node, np) {
994e34212c7SDmitry Osipenko 		err = of_property_read_u32(np, "nvidia,ram-code", &value);
995e34212c7SDmitry Osipenko 		if (err || value != ram_code)
996e34212c7SDmitry Osipenko 			continue;
997e34212c7SDmitry Osipenko 
998e34212c7SDmitry Osipenko 		return np;
999e34212c7SDmitry Osipenko 	}
1000e34212c7SDmitry Osipenko 
1001e34212c7SDmitry Osipenko 	dev_err(dev, "no memory timings for RAM code %u found in device-tree\n",
1002e34212c7SDmitry Osipenko 		ram_code);
1003e34212c7SDmitry Osipenko 
1004e34212c7SDmitry Osipenko 	return NULL;
1005e34212c7SDmitry Osipenko }
1006e34212c7SDmitry Osipenko 
1007e34212c7SDmitry Osipenko static int emc_setup_hw(struct tegra_emc *emc)
1008e34212c7SDmitry Osipenko {
1009930c6818SDmitry Osipenko 	u32 intmask = EMC_REFRESH_OVERFLOW_INT;
1010e34212c7SDmitry Osipenko 	u32 fbio_cfg5, emc_cfg, emc_dbg;
1011e34212c7SDmitry Osipenko 	enum emc_dram_type dram_type;
1012e34212c7SDmitry Osipenko 
1013e34212c7SDmitry Osipenko 	fbio_cfg5 = readl_relaxed(emc->regs + EMC_FBIO_CFG5);
1014e34212c7SDmitry Osipenko 	dram_type = fbio_cfg5 & EMC_FBIO_CFG5_DRAM_TYPE_MASK;
1015e34212c7SDmitry Osipenko 
1016e34212c7SDmitry Osipenko 	emc_cfg = readl_relaxed(emc->regs + EMC_CFG_2);
1017e34212c7SDmitry Osipenko 
1018e34212c7SDmitry Osipenko 	/* enable EMC and CAR to handshake on PLL divider/source changes */
1019e34212c7SDmitry Osipenko 	emc_cfg |= EMC_CLKCHANGE_REQ_ENABLE;
1020e34212c7SDmitry Osipenko 
1021e34212c7SDmitry Osipenko 	/* configure clock change mode accordingly to DRAM type */
1022e34212c7SDmitry Osipenko 	switch (dram_type) {
1023e34212c7SDmitry Osipenko 	case DRAM_TYPE_LPDDR2:
1024e34212c7SDmitry Osipenko 		emc_cfg |= EMC_CLKCHANGE_PD_ENABLE;
1025e34212c7SDmitry Osipenko 		emc_cfg &= ~EMC_CLKCHANGE_SR_ENABLE;
1026e34212c7SDmitry Osipenko 		break;
1027e34212c7SDmitry Osipenko 
1028e34212c7SDmitry Osipenko 	default:
1029e34212c7SDmitry Osipenko 		emc_cfg &= ~EMC_CLKCHANGE_SR_ENABLE;
1030e34212c7SDmitry Osipenko 		emc_cfg &= ~EMC_CLKCHANGE_PD_ENABLE;
1031e34212c7SDmitry Osipenko 		break;
1032e34212c7SDmitry Osipenko 	}
1033e34212c7SDmitry Osipenko 
1034e34212c7SDmitry Osipenko 	writel_relaxed(emc_cfg, emc->regs + EMC_CFG_2);
1035e34212c7SDmitry Osipenko 
1036e34212c7SDmitry Osipenko 	/* initialize interrupt */
1037e34212c7SDmitry Osipenko 	writel_relaxed(intmask, emc->regs + EMC_INTMASK);
1038e34212c7SDmitry Osipenko 	writel_relaxed(0xffffffff, emc->regs + EMC_INTSTATUS);
1039e34212c7SDmitry Osipenko 
1040e34212c7SDmitry Osipenko 	/* ensure that unwanted debug features are disabled */
1041e34212c7SDmitry Osipenko 	emc_dbg = readl_relaxed(emc->regs + EMC_DBG);
1042e34212c7SDmitry Osipenko 	emc_dbg |= EMC_DBG_CFG_PRIORITY;
1043e34212c7SDmitry Osipenko 	emc_dbg &= ~EMC_DBG_READ_MUX_ASSEMBLY;
1044e34212c7SDmitry Osipenko 	emc_dbg &= ~EMC_DBG_WRITE_MUX_ACTIVE;
1045e34212c7SDmitry Osipenko 	emc_dbg &= ~EMC_DBG_FORCE_UPDATE;
1046e34212c7SDmitry Osipenko 	writel_relaxed(emc_dbg, emc->regs + EMC_DBG);
1047e34212c7SDmitry Osipenko 
1048e34212c7SDmitry Osipenko 	return 0;
1049e34212c7SDmitry Osipenko }
1050e34212c7SDmitry Osipenko 
1051e34212c7SDmitry Osipenko static long emc_round_rate(unsigned long rate,
1052e34212c7SDmitry Osipenko 			   unsigned long min_rate,
1053e34212c7SDmitry Osipenko 			   unsigned long max_rate,
1054e34212c7SDmitry Osipenko 			   void *arg)
1055e34212c7SDmitry Osipenko {
1056e34212c7SDmitry Osipenko 	struct emc_timing *timing = NULL;
1057e34212c7SDmitry Osipenko 	struct tegra_emc *emc = arg;
1058e34212c7SDmitry Osipenko 	unsigned int i;
1059e34212c7SDmitry Osipenko 
1060e34212c7SDmitry Osipenko 	min_rate = min(min_rate, emc->timings[emc->num_timings - 1].rate);
1061e34212c7SDmitry Osipenko 
1062e34212c7SDmitry Osipenko 	for (i = 0; i < emc->num_timings; i++) {
1063e34212c7SDmitry Osipenko 		if (emc->timings[i].rate < rate && i != emc->num_timings - 1)
1064e34212c7SDmitry Osipenko 			continue;
1065e34212c7SDmitry Osipenko 
1066e34212c7SDmitry Osipenko 		if (emc->timings[i].rate > max_rate) {
1067e34212c7SDmitry Osipenko 			i = max(i, 1u) - 1;
1068e34212c7SDmitry Osipenko 
1069e34212c7SDmitry Osipenko 			if (emc->timings[i].rate < min_rate)
1070e34212c7SDmitry Osipenko 				break;
1071e34212c7SDmitry Osipenko 		}
1072e34212c7SDmitry Osipenko 
1073e34212c7SDmitry Osipenko 		if (emc->timings[i].rate < min_rate)
1074e34212c7SDmitry Osipenko 			continue;
1075e34212c7SDmitry Osipenko 
1076e34212c7SDmitry Osipenko 		timing = &emc->timings[i];
1077e34212c7SDmitry Osipenko 		break;
1078e34212c7SDmitry Osipenko 	}
1079e34212c7SDmitry Osipenko 
1080e34212c7SDmitry Osipenko 	if (!timing) {
1081e34212c7SDmitry Osipenko 		dev_err(emc->dev, "no timing for rate %lu min %lu max %lu\n",
1082e34212c7SDmitry Osipenko 			rate, min_rate, max_rate);
1083e34212c7SDmitry Osipenko 		return -EINVAL;
1084e34212c7SDmitry Osipenko 	}
1085e34212c7SDmitry Osipenko 
1086e34212c7SDmitry Osipenko 	return timing->rate;
1087e34212c7SDmitry Osipenko }
1088e34212c7SDmitry Osipenko 
10898cee32b4SThierry Reding /*
10908cee32b4SThierry Reding  * debugfs interface
10918cee32b4SThierry Reding  *
10928cee32b4SThierry Reding  * The memory controller driver exposes some files in debugfs that can be used
10938cee32b4SThierry Reding  * to control the EMC frequency. The top-level directory can be found here:
10948cee32b4SThierry Reding  *
10958cee32b4SThierry Reding  *   /sys/kernel/debug/emc
10968cee32b4SThierry Reding  *
10978cee32b4SThierry Reding  * It contains the following files:
10988cee32b4SThierry Reding  *
10998cee32b4SThierry Reding  *   - available_rates: This file contains a list of valid, space-separated
11008cee32b4SThierry Reding  *     EMC frequencies.
11018cee32b4SThierry Reding  *
11028cee32b4SThierry Reding  *   - min_rate: Writing a value to this file sets the given frequency as the
11038cee32b4SThierry Reding  *       floor of the permitted range. If this is higher than the currently
11048cee32b4SThierry Reding  *       configured EMC frequency, this will cause the frequency to be
11058cee32b4SThierry Reding  *       increased so that it stays within the valid range.
11068cee32b4SThierry Reding  *
11078cee32b4SThierry Reding  *   - max_rate: Similarily to the min_rate file, writing a value to this file
11088cee32b4SThierry Reding  *       sets the given frequency as the ceiling of the permitted range. If
11098cee32b4SThierry Reding  *       the value is lower than the currently configured EMC frequency, this
11108cee32b4SThierry Reding  *       will cause the frequency to be decreased so that it stays within the
11118cee32b4SThierry Reding  *       valid range.
11128cee32b4SThierry Reding  */
11138cee32b4SThierry Reding 
11148cee32b4SThierry Reding static bool tegra_emc_validate_rate(struct tegra_emc *emc, unsigned long rate)
11158cee32b4SThierry Reding {
11168cee32b4SThierry Reding 	unsigned int i;
11178cee32b4SThierry Reding 
11188cee32b4SThierry Reding 	for (i = 0; i < emc->num_timings; i++)
11198cee32b4SThierry Reding 		if (rate == emc->timings[i].rate)
11208cee32b4SThierry Reding 			return true;
11218cee32b4SThierry Reding 
11228cee32b4SThierry Reding 	return false;
11238cee32b4SThierry Reding }
11248cee32b4SThierry Reding 
11258cee32b4SThierry Reding static int tegra_emc_debug_available_rates_show(struct seq_file *s, void *data)
11268cee32b4SThierry Reding {
11278cee32b4SThierry Reding 	struct tegra_emc *emc = s->private;
11288cee32b4SThierry Reding 	const char *prefix = "";
11298cee32b4SThierry Reding 	unsigned int i;
11308cee32b4SThierry Reding 
11318cee32b4SThierry Reding 	for (i = 0; i < emc->num_timings; i++) {
11328cee32b4SThierry Reding 		seq_printf(s, "%s%lu", prefix, emc->timings[i].rate);
11338cee32b4SThierry Reding 		prefix = " ";
11348cee32b4SThierry Reding 	}
11358cee32b4SThierry Reding 
11368cee32b4SThierry Reding 	seq_puts(s, "\n");
11378cee32b4SThierry Reding 
11388cee32b4SThierry Reding 	return 0;
11398cee32b4SThierry Reding }
11408cee32b4SThierry Reding 
11418cee32b4SThierry Reding static int tegra_emc_debug_available_rates_open(struct inode *inode,
11428cee32b4SThierry Reding 						struct file *file)
11438cee32b4SThierry Reding {
11448cee32b4SThierry Reding 	return single_open(file, tegra_emc_debug_available_rates_show,
11458cee32b4SThierry Reding 			   inode->i_private);
11468cee32b4SThierry Reding }
11478cee32b4SThierry Reding 
11488cee32b4SThierry Reding static const struct file_operations tegra_emc_debug_available_rates_fops = {
11498cee32b4SThierry Reding 	.open = tegra_emc_debug_available_rates_open,
11508cee32b4SThierry Reding 	.read = seq_read,
11518cee32b4SThierry Reding 	.llseek = seq_lseek,
11528cee32b4SThierry Reding 	.release = single_release,
11538cee32b4SThierry Reding };
11548cee32b4SThierry Reding 
11558cee32b4SThierry Reding static int tegra_emc_debug_min_rate_get(void *data, u64 *rate)
11568cee32b4SThierry Reding {
11578cee32b4SThierry Reding 	struct tegra_emc *emc = data;
11588cee32b4SThierry Reding 
11598cee32b4SThierry Reding 	*rate = emc->debugfs.min_rate;
11608cee32b4SThierry Reding 
11618cee32b4SThierry Reding 	return 0;
11628cee32b4SThierry Reding }
11638cee32b4SThierry Reding 
11648cee32b4SThierry Reding static int tegra_emc_debug_min_rate_set(void *data, u64 rate)
11658cee32b4SThierry Reding {
11668cee32b4SThierry Reding 	struct tegra_emc *emc = data;
11678cee32b4SThierry Reding 	int err;
11688cee32b4SThierry Reding 
11698cee32b4SThierry Reding 	if (!tegra_emc_validate_rate(emc, rate))
11708cee32b4SThierry Reding 		return -EINVAL;
11718cee32b4SThierry Reding 
11728cee32b4SThierry Reding 	err = clk_set_min_rate(emc->clk, rate);
11738cee32b4SThierry Reding 	if (err < 0)
11748cee32b4SThierry Reding 		return err;
11758cee32b4SThierry Reding 
11768cee32b4SThierry Reding 	emc->debugfs.min_rate = rate;
11778cee32b4SThierry Reding 
11788cee32b4SThierry Reding 	return 0;
11798cee32b4SThierry Reding }
11808cee32b4SThierry Reding 
11818cee32b4SThierry Reding DEFINE_SIMPLE_ATTRIBUTE(tegra_emc_debug_min_rate_fops,
11828cee32b4SThierry Reding 			tegra_emc_debug_min_rate_get,
11838cee32b4SThierry Reding 			tegra_emc_debug_min_rate_set, "%llu\n");
11848cee32b4SThierry Reding 
11858cee32b4SThierry Reding static int tegra_emc_debug_max_rate_get(void *data, u64 *rate)
11868cee32b4SThierry Reding {
11878cee32b4SThierry Reding 	struct tegra_emc *emc = data;
11888cee32b4SThierry Reding 
11898cee32b4SThierry Reding 	*rate = emc->debugfs.max_rate;
11908cee32b4SThierry Reding 
11918cee32b4SThierry Reding 	return 0;
11928cee32b4SThierry Reding }
11938cee32b4SThierry Reding 
11948cee32b4SThierry Reding static int tegra_emc_debug_max_rate_set(void *data, u64 rate)
11958cee32b4SThierry Reding {
11968cee32b4SThierry Reding 	struct tegra_emc *emc = data;
11978cee32b4SThierry Reding 	int err;
11988cee32b4SThierry Reding 
11998cee32b4SThierry Reding 	if (!tegra_emc_validate_rate(emc, rate))
12008cee32b4SThierry Reding 		return -EINVAL;
12018cee32b4SThierry Reding 
12028cee32b4SThierry Reding 	err = clk_set_max_rate(emc->clk, rate);
12038cee32b4SThierry Reding 	if (err < 0)
12048cee32b4SThierry Reding 		return err;
12058cee32b4SThierry Reding 
12068cee32b4SThierry Reding 	emc->debugfs.max_rate = rate;
12078cee32b4SThierry Reding 
12088cee32b4SThierry Reding 	return 0;
12098cee32b4SThierry Reding }
12108cee32b4SThierry Reding 
12118cee32b4SThierry Reding DEFINE_SIMPLE_ATTRIBUTE(tegra_emc_debug_max_rate_fops,
12128cee32b4SThierry Reding 			tegra_emc_debug_max_rate_get,
12138cee32b4SThierry Reding 			tegra_emc_debug_max_rate_set, "%llu\n");
12148cee32b4SThierry Reding 
12158cee32b4SThierry Reding static void tegra_emc_debugfs_init(struct tegra_emc *emc)
12168cee32b4SThierry Reding {
12178cee32b4SThierry Reding 	struct device *dev = emc->dev;
12188cee32b4SThierry Reding 	unsigned int i;
12198cee32b4SThierry Reding 	int err;
12208cee32b4SThierry Reding 
12218cee32b4SThierry Reding 	emc->debugfs.min_rate = ULONG_MAX;
12228cee32b4SThierry Reding 	emc->debugfs.max_rate = 0;
12238cee32b4SThierry Reding 
12248cee32b4SThierry Reding 	for (i = 0; i < emc->num_timings; i++) {
12258cee32b4SThierry Reding 		if (emc->timings[i].rate < emc->debugfs.min_rate)
12268cee32b4SThierry Reding 			emc->debugfs.min_rate = emc->timings[i].rate;
12278cee32b4SThierry Reding 
12288cee32b4SThierry Reding 		if (emc->timings[i].rate > emc->debugfs.max_rate)
12298cee32b4SThierry Reding 			emc->debugfs.max_rate = emc->timings[i].rate;
12308cee32b4SThierry Reding 	}
12318cee32b4SThierry Reding 
1232a53670e1SDmitry Osipenko 	if (!emc->num_timings) {
1233a53670e1SDmitry Osipenko 		emc->debugfs.min_rate = clk_get_rate(emc->clk);
1234a53670e1SDmitry Osipenko 		emc->debugfs.max_rate = emc->debugfs.min_rate;
1235a53670e1SDmitry Osipenko 	}
1236a53670e1SDmitry Osipenko 
12378cee32b4SThierry Reding 	err = clk_set_rate_range(emc->clk, emc->debugfs.min_rate,
12388cee32b4SThierry Reding 				 emc->debugfs.max_rate);
12398cee32b4SThierry Reding 	if (err < 0) {
12408cee32b4SThierry Reding 		dev_err(dev, "failed to set rate range [%lu-%lu] for %pC\n",
12418cee32b4SThierry Reding 			emc->debugfs.min_rate, emc->debugfs.max_rate,
12428cee32b4SThierry Reding 			emc->clk);
12438cee32b4SThierry Reding 	}
12448cee32b4SThierry Reding 
12458cee32b4SThierry Reding 	emc->debugfs.root = debugfs_create_dir("emc", NULL);
12468cee32b4SThierry Reding 	if (!emc->debugfs.root) {
12478cee32b4SThierry Reding 		dev_err(emc->dev, "failed to create debugfs directory\n");
12488cee32b4SThierry Reding 		return;
12498cee32b4SThierry Reding 	}
12508cee32b4SThierry Reding 
12516cc8823aSDmitry Osipenko 	debugfs_create_file("available_rates", 0444, emc->debugfs.root,
12528cee32b4SThierry Reding 			    emc, &tegra_emc_debug_available_rates_fops);
12536cc8823aSDmitry Osipenko 	debugfs_create_file("min_rate", 0644, emc->debugfs.root,
12548cee32b4SThierry Reding 			    emc, &tegra_emc_debug_min_rate_fops);
12556cc8823aSDmitry Osipenko 	debugfs_create_file("max_rate", 0644, emc->debugfs.root,
12568cee32b4SThierry Reding 			    emc, &tegra_emc_debug_max_rate_fops);
12578cee32b4SThierry Reding }
12588cee32b4SThierry Reding 
1259e34212c7SDmitry Osipenko static int tegra_emc_probe(struct platform_device *pdev)
1260e34212c7SDmitry Osipenko {
1261e34212c7SDmitry Osipenko 	struct device_node *np;
1262e34212c7SDmitry Osipenko 	struct tegra_emc *emc;
1263e34212c7SDmitry Osipenko 	int err;
1264e34212c7SDmitry Osipenko 
1265e34212c7SDmitry Osipenko 	if (of_get_child_count(pdev->dev.of_node) == 0) {
1266e34212c7SDmitry Osipenko 		dev_info(&pdev->dev,
1267e34212c7SDmitry Osipenko 			 "device-tree node doesn't have memory timings\n");
1268030d2829SDmitry Osipenko 		return -ENODEV;
1269e34212c7SDmitry Osipenko 	}
1270e34212c7SDmitry Osipenko 
1271e34212c7SDmitry Osipenko 	np = emc_find_node_by_ram_code(&pdev->dev);
1272e34212c7SDmitry Osipenko 	if (!np)
1273e34212c7SDmitry Osipenko 		return -EINVAL;
1274e34212c7SDmitry Osipenko 
1275e34212c7SDmitry Osipenko 	emc = devm_kzalloc(&pdev->dev, sizeof(*emc), GFP_KERNEL);
1276e34212c7SDmitry Osipenko 	if (!emc) {
1277e34212c7SDmitry Osipenko 		of_node_put(np);
1278e34212c7SDmitry Osipenko 		return -ENOMEM;
1279e34212c7SDmitry Osipenko 	}
1280e34212c7SDmitry Osipenko 
1281*6c6bd207SDmitry Osipenko 	emc->mc = devm_tegra_memory_controller_get(&pdev->dev);
1282*6c6bd207SDmitry Osipenko 	if (IS_ERR(emc->mc))
1283*6c6bd207SDmitry Osipenko 		return PTR_ERR(emc->mc);
1284e34212c7SDmitry Osipenko 
1285e34212c7SDmitry Osipenko 	emc->clk_nb.notifier_call = emc_clk_change_notify;
1286e34212c7SDmitry Osipenko 	emc->dev = &pdev->dev;
1287e34212c7SDmitry Osipenko 
1288e34212c7SDmitry Osipenko 	err = emc_load_timings_from_dt(emc, np);
1289e34212c7SDmitry Osipenko 	of_node_put(np);
1290e34212c7SDmitry Osipenko 	if (err)
1291e34212c7SDmitry Osipenko 		return err;
1292e34212c7SDmitry Osipenko 
1293e34212c7SDmitry Osipenko 	emc->regs = devm_platform_ioremap_resource(pdev, 0);
1294e34212c7SDmitry Osipenko 	if (IS_ERR(emc->regs))
1295e34212c7SDmitry Osipenko 		return PTR_ERR(emc->regs);
1296e34212c7SDmitry Osipenko 
1297e34212c7SDmitry Osipenko 	err = emc_setup_hw(emc);
1298e34212c7SDmitry Osipenko 	if (err)
1299e34212c7SDmitry Osipenko 		return err;
1300e34212c7SDmitry Osipenko 
1301e34212c7SDmitry Osipenko 	err = platform_get_irq(pdev, 0);
1302e34212c7SDmitry Osipenko 	if (err < 0) {
1303e34212c7SDmitry Osipenko 		dev_err(&pdev->dev, "interrupt not specified: %d\n", err);
1304e34212c7SDmitry Osipenko 		return err;
1305e34212c7SDmitry Osipenko 	}
1306e34212c7SDmitry Osipenko 	emc->irq = err;
1307e34212c7SDmitry Osipenko 
1308e34212c7SDmitry Osipenko 	err = devm_request_irq(&pdev->dev, emc->irq, tegra_emc_isr, 0,
1309e34212c7SDmitry Osipenko 			       dev_name(&pdev->dev), emc);
1310e34212c7SDmitry Osipenko 	if (err) {
1311e34212c7SDmitry Osipenko 		dev_err(&pdev->dev, "failed to request irq: %d\n", err);
1312e34212c7SDmitry Osipenko 		return err;
1313e34212c7SDmitry Osipenko 	}
1314e34212c7SDmitry Osipenko 
1315e34212c7SDmitry Osipenko 	tegra20_clk_set_emc_round_callback(emc_round_rate, emc);
1316e34212c7SDmitry Osipenko 
1317e34212c7SDmitry Osipenko 	emc->clk = devm_clk_get(&pdev->dev, "emc");
1318e34212c7SDmitry Osipenko 	if (IS_ERR(emc->clk)) {
1319e34212c7SDmitry Osipenko 		err = PTR_ERR(emc->clk);
1320e34212c7SDmitry Osipenko 		dev_err(&pdev->dev, "failed to get emc clock: %d\n", err);
1321e34212c7SDmitry Osipenko 		goto unset_cb;
1322e34212c7SDmitry Osipenko 	}
1323e34212c7SDmitry Osipenko 
1324e34212c7SDmitry Osipenko 	err = clk_notifier_register(emc->clk, &emc->clk_nb);
1325e34212c7SDmitry Osipenko 	if (err) {
1326e34212c7SDmitry Osipenko 		dev_err(&pdev->dev, "failed to register clk notifier: %d\n",
1327e34212c7SDmitry Osipenko 			err);
1328e34212c7SDmitry Osipenko 		goto unset_cb;
1329e34212c7SDmitry Osipenko 	}
1330e34212c7SDmitry Osipenko 
1331e34212c7SDmitry Osipenko 	platform_set_drvdata(pdev, emc);
13328cee32b4SThierry Reding 	tegra_emc_debugfs_init(emc);
1333e34212c7SDmitry Osipenko 
1334e34212c7SDmitry Osipenko 	return 0;
1335e34212c7SDmitry Osipenko 
1336e34212c7SDmitry Osipenko unset_cb:
1337e34212c7SDmitry Osipenko 	tegra20_clk_set_emc_round_callback(NULL, NULL);
1338e34212c7SDmitry Osipenko 
1339e34212c7SDmitry Osipenko 	return err;
1340e34212c7SDmitry Osipenko }
1341e34212c7SDmitry Osipenko 
1342e34212c7SDmitry Osipenko static int tegra_emc_suspend(struct device *dev)
1343e34212c7SDmitry Osipenko {
1344e34212c7SDmitry Osipenko 	struct tegra_emc *emc = dev_get_drvdata(dev);
134551bb73f9SDmitry Osipenko 	int err;
1346e34212c7SDmitry Osipenko 
134751bb73f9SDmitry Osipenko 	/* take exclusive control over the clock's rate */
134851bb73f9SDmitry Osipenko 	err = clk_rate_exclusive_get(emc->clk);
134951bb73f9SDmitry Osipenko 	if (err) {
135051bb73f9SDmitry Osipenko 		dev_err(emc->dev, "failed to acquire clk: %d\n", err);
135151bb73f9SDmitry Osipenko 		return err;
135251bb73f9SDmitry Osipenko 	}
135351bb73f9SDmitry Osipenko 
135451bb73f9SDmitry Osipenko 	/* suspending in a bad state will hang machine */
135551bb73f9SDmitry Osipenko 	if (WARN(emc->bad_state, "hardware in a bad state\n"))
1356e34212c7SDmitry Osipenko 		return -EINVAL;
1357e34212c7SDmitry Osipenko 
1358e34212c7SDmitry Osipenko 	emc->bad_state = true;
1359e34212c7SDmitry Osipenko 
1360e34212c7SDmitry Osipenko 	return 0;
1361e34212c7SDmitry Osipenko }
1362e34212c7SDmitry Osipenko 
1363e34212c7SDmitry Osipenko static int tegra_emc_resume(struct device *dev)
1364e34212c7SDmitry Osipenko {
1365e34212c7SDmitry Osipenko 	struct tegra_emc *emc = dev_get_drvdata(dev);
1366e34212c7SDmitry Osipenko 
1367e34212c7SDmitry Osipenko 	emc_setup_hw(emc);
1368e34212c7SDmitry Osipenko 	emc->bad_state = false;
1369e34212c7SDmitry Osipenko 
137051bb73f9SDmitry Osipenko 	clk_rate_exclusive_put(emc->clk);
137151bb73f9SDmitry Osipenko 
1372e34212c7SDmitry Osipenko 	return 0;
1373e34212c7SDmitry Osipenko }
1374e34212c7SDmitry Osipenko 
1375e34212c7SDmitry Osipenko static const struct dev_pm_ops tegra_emc_pm_ops = {
1376e34212c7SDmitry Osipenko 	.suspend = tegra_emc_suspend,
1377e34212c7SDmitry Osipenko 	.resume = tegra_emc_resume,
1378e34212c7SDmitry Osipenko };
1379e34212c7SDmitry Osipenko 
1380e34212c7SDmitry Osipenko static const struct of_device_id tegra_emc_of_match[] = {
1381e34212c7SDmitry Osipenko 	{ .compatible = "nvidia,tegra30-emc", },
1382e34212c7SDmitry Osipenko 	{},
1383e34212c7SDmitry Osipenko };
1384e34212c7SDmitry Osipenko 
1385e34212c7SDmitry Osipenko static struct platform_driver tegra_emc_driver = {
1386e34212c7SDmitry Osipenko 	.probe = tegra_emc_probe,
1387e34212c7SDmitry Osipenko 	.driver = {
1388e34212c7SDmitry Osipenko 		.name = "tegra30-emc",
1389e34212c7SDmitry Osipenko 		.of_match_table = tegra_emc_of_match,
1390e34212c7SDmitry Osipenko 		.pm = &tegra_emc_pm_ops,
1391e34212c7SDmitry Osipenko 		.suppress_bind_attrs = true,
1392e34212c7SDmitry Osipenko 	},
1393e34212c7SDmitry Osipenko };
1394e34212c7SDmitry Osipenko 
1395e34212c7SDmitry Osipenko static int __init tegra_emc_init(void)
1396e34212c7SDmitry Osipenko {
1397e34212c7SDmitry Osipenko 	return platform_driver_register(&tegra_emc_driver);
1398e34212c7SDmitry Osipenko }
1399e34212c7SDmitry Osipenko subsys_initcall(tegra_emc_init);
1400