xref: /linux/drivers/memory/tegra/tegra124-emc.c (revision 380def2d4cf257663de42618e57134afeded32dd)
19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
273a7f0a9SMikko Perttunen /*
373a7f0a9SMikko Perttunen  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
473a7f0a9SMikko Perttunen  *
573a7f0a9SMikko Perttunen  * Author:
673a7f0a9SMikko Perttunen  *	Mikko Perttunen <mperttunen@nvidia.com>
773a7f0a9SMikko Perttunen  */
873a7f0a9SMikko Perttunen 
973a7f0a9SMikko Perttunen #include <linux/clk-provider.h>
1073a7f0a9SMikko Perttunen #include <linux/clk.h>
1173a7f0a9SMikko Perttunen #include <linux/clkdev.h>
12281462e5SDmitry Osipenko #include <linux/clk/tegra.h>
139c77a81fSMikko Perttunen #include <linux/debugfs.h>
1473a7f0a9SMikko Perttunen #include <linux/delay.h>
15*380def2dSDmitry Osipenko #include <linux/interconnect-provider.h>
1662e59c4eSStephen Boyd #include <linux/io.h>
17281462e5SDmitry Osipenko #include <linux/module.h>
18*380def2dSDmitry Osipenko #include <linux/mutex.h>
1973a7f0a9SMikko Perttunen #include <linux/of_address.h>
2073a7f0a9SMikko Perttunen #include <linux/of_platform.h>
2173a7f0a9SMikko Perttunen #include <linux/platform_device.h>
22*380def2dSDmitry Osipenko #include <linux/pm_opp.h>
2373a7f0a9SMikko Perttunen #include <linux/sort.h>
2473a7f0a9SMikko Perttunen #include <linux/string.h>
2573a7f0a9SMikko Perttunen 
2673a7f0a9SMikko Perttunen #include <soc/tegra/fuse.h>
2773a7f0a9SMikko Perttunen #include <soc/tegra/mc.h>
2873a7f0a9SMikko Perttunen 
29*380def2dSDmitry Osipenko #include "mc.h"
30*380def2dSDmitry Osipenko 
3173a7f0a9SMikko Perttunen #define EMC_FBIO_CFG5				0x104
3273a7f0a9SMikko Perttunen #define	EMC_FBIO_CFG5_DRAM_TYPE_MASK		0x3
3373a7f0a9SMikko Perttunen #define	EMC_FBIO_CFG5_DRAM_TYPE_SHIFT		0
34*380def2dSDmitry Osipenko #define EMC_FBIO_CFG5_DRAM_WIDTH_X64		BIT(4)
3573a7f0a9SMikko Perttunen 
3673a7f0a9SMikko Perttunen #define EMC_INTSTATUS				0x0
3773a7f0a9SMikko Perttunen #define EMC_INTSTATUS_CLKCHANGE_COMPLETE	BIT(4)
3873a7f0a9SMikko Perttunen 
3973a7f0a9SMikko Perttunen #define EMC_CFG					0xc
4073a7f0a9SMikko Perttunen #define EMC_CFG_DRAM_CLKSTOP_PD			BIT(31)
4173a7f0a9SMikko Perttunen #define EMC_CFG_DRAM_CLKSTOP_SR			BIT(30)
4273a7f0a9SMikko Perttunen #define EMC_CFG_DRAM_ACPD			BIT(29)
4373a7f0a9SMikko Perttunen #define EMC_CFG_DYN_SREF			BIT(28)
4473a7f0a9SMikko Perttunen #define EMC_CFG_PWR_MASK			((0xF << 28) | BIT(18))
4573a7f0a9SMikko Perttunen #define EMC_CFG_DSR_VTTGEN_DRV_EN		BIT(18)
4673a7f0a9SMikko Perttunen 
4773a7f0a9SMikko Perttunen #define EMC_REFCTRL				0x20
4873a7f0a9SMikko Perttunen #define EMC_REFCTRL_DEV_SEL_SHIFT		0
4973a7f0a9SMikko Perttunen #define EMC_REFCTRL_ENABLE			BIT(31)
5073a7f0a9SMikko Perttunen 
5173a7f0a9SMikko Perttunen #define EMC_TIMING_CONTROL			0x28
5273a7f0a9SMikko Perttunen #define EMC_RC					0x2c
5373a7f0a9SMikko Perttunen #define EMC_RFC					0x30
5473a7f0a9SMikko Perttunen #define EMC_RAS					0x34
5573a7f0a9SMikko Perttunen #define EMC_RP					0x38
5673a7f0a9SMikko Perttunen #define EMC_R2W					0x3c
5773a7f0a9SMikko Perttunen #define EMC_W2R					0x40
5873a7f0a9SMikko Perttunen #define EMC_R2P					0x44
5973a7f0a9SMikko Perttunen #define EMC_W2P					0x48
6073a7f0a9SMikko Perttunen #define EMC_RD_RCD				0x4c
6173a7f0a9SMikko Perttunen #define EMC_WR_RCD				0x50
6273a7f0a9SMikko Perttunen #define EMC_RRD					0x54
6373a7f0a9SMikko Perttunen #define EMC_REXT				0x58
6473a7f0a9SMikko Perttunen #define EMC_WDV					0x5c
6573a7f0a9SMikko Perttunen #define EMC_QUSE				0x60
6673a7f0a9SMikko Perttunen #define EMC_QRST				0x64
6773a7f0a9SMikko Perttunen #define EMC_QSAFE				0x68
6873a7f0a9SMikko Perttunen #define EMC_RDV					0x6c
6973a7f0a9SMikko Perttunen #define EMC_REFRESH				0x70
7073a7f0a9SMikko Perttunen #define EMC_BURST_REFRESH_NUM			0x74
7173a7f0a9SMikko Perttunen #define EMC_PDEX2WR				0x78
7273a7f0a9SMikko Perttunen #define EMC_PDEX2RD				0x7c
7373a7f0a9SMikko Perttunen #define EMC_PCHG2PDEN				0x80
7473a7f0a9SMikko Perttunen #define EMC_ACT2PDEN				0x84
7573a7f0a9SMikko Perttunen #define EMC_AR2PDEN				0x88
7673a7f0a9SMikko Perttunen #define EMC_RW2PDEN				0x8c
7773a7f0a9SMikko Perttunen #define EMC_TXSR				0x90
7873a7f0a9SMikko Perttunen #define EMC_TCKE				0x94
7973a7f0a9SMikko Perttunen #define EMC_TFAW				0x98
8073a7f0a9SMikko Perttunen #define EMC_TRPAB				0x9c
8173a7f0a9SMikko Perttunen #define EMC_TCLKSTABLE				0xa0
8273a7f0a9SMikko Perttunen #define EMC_TCLKSTOP				0xa4
8373a7f0a9SMikko Perttunen #define EMC_TREFBW				0xa8
8473a7f0a9SMikko Perttunen #define EMC_ODT_WRITE				0xb0
8573a7f0a9SMikko Perttunen #define EMC_ODT_READ				0xb4
8673a7f0a9SMikko Perttunen #define EMC_WEXT				0xb8
8773a7f0a9SMikko Perttunen #define EMC_CTT					0xbc
8873a7f0a9SMikko Perttunen #define EMC_RFC_SLR				0xc0
8973a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT2			0xc4
9073a7f0a9SMikko Perttunen 
9173a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT			0xc8
9273a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT	0
9373a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK	\
9473a7f0a9SMikko Perttunen 	(0x3FF << EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT)
9573a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT	16
9673a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_LONG_WAIT_MASK		\
9773a7f0a9SMikko Perttunen 	(0x3FF << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT)
9873a7f0a9SMikko Perttunen 
9973a7f0a9SMikko Perttunen #define EMC_MRS					0xcc
10073a7f0a9SMikko Perttunen #define EMC_MODE_SET_DLL_RESET			BIT(8)
10173a7f0a9SMikko Perttunen #define EMC_MODE_SET_LONG_CNT			BIT(26)
10273a7f0a9SMikko Perttunen #define EMC_EMRS				0xd0
10373a7f0a9SMikko Perttunen #define EMC_REF					0xd4
10473a7f0a9SMikko Perttunen #define EMC_PRE					0xd8
10573a7f0a9SMikko Perttunen 
10673a7f0a9SMikko Perttunen #define EMC_SELF_REF				0xe0
10773a7f0a9SMikko Perttunen #define EMC_SELF_REF_CMD_ENABLED		BIT(0)
10873a7f0a9SMikko Perttunen #define EMC_SELF_REF_DEV_SEL_SHIFT		30
10973a7f0a9SMikko Perttunen 
11073a7f0a9SMikko Perttunen #define EMC_MRW					0xe8
11173a7f0a9SMikko Perttunen 
11273a7f0a9SMikko Perttunen #define EMC_MRR					0xec
11373a7f0a9SMikko Perttunen #define EMC_MRR_MA_SHIFT			16
11473a7f0a9SMikko Perttunen #define LPDDR2_MR4_TEMP_SHIFT			0
11573a7f0a9SMikko Perttunen 
11673a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL3			0xf8
11773a7f0a9SMikko Perttunen #define EMC_FBIO_SPARE				0x100
11873a7f0a9SMikko Perttunen 
11973a7f0a9SMikko Perttunen #define EMC_FBIO_CFG6				0x114
12073a7f0a9SMikko Perttunen #define EMC_EMRS2				0x12c
12173a7f0a9SMikko Perttunen #define EMC_MRW2				0x134
12273a7f0a9SMikko Perttunen #define EMC_MRW4				0x13c
12373a7f0a9SMikko Perttunen #define EMC_EINPUT				0x14c
12473a7f0a9SMikko Perttunen #define EMC_EINPUT_DURATION			0x150
12573a7f0a9SMikko Perttunen #define EMC_PUTERM_EXTRA			0x154
12673a7f0a9SMikko Perttunen #define EMC_TCKESR				0x158
12773a7f0a9SMikko Perttunen #define EMC_TPD					0x15c
12873a7f0a9SMikko Perttunen 
12973a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG			0x2a4
13073a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG_AUTO_CAL_START	BIT(31)
13173a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_INTERVAL			0x2a8
13273a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_STATUS			0x2ac
13373a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_STATUS_ACTIVE		BIT(31)
13473a7f0a9SMikko Perttunen #define EMC_STATUS				0x2b4
13573a7f0a9SMikko Perttunen #define EMC_STATUS_TIMING_UPDATE_STALLED	BIT(23)
13673a7f0a9SMikko Perttunen 
13773a7f0a9SMikko Perttunen #define EMC_CFG_2				0x2b8
13873a7f0a9SMikko Perttunen #define EMC_CFG_2_MODE_SHIFT			0
13973a7f0a9SMikko Perttunen #define EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR	BIT(6)
14073a7f0a9SMikko Perttunen 
14173a7f0a9SMikko Perttunen #define EMC_CFG_DIG_DLL				0x2bc
14273a7f0a9SMikko Perttunen #define EMC_CFG_DIG_DLL_PERIOD			0x2c0
14373a7f0a9SMikko Perttunen #define EMC_RDV_MASK				0x2cc
14473a7f0a9SMikko Perttunen #define EMC_WDV_MASK				0x2d0
14573a7f0a9SMikko Perttunen #define EMC_CTT_DURATION			0x2d8
14673a7f0a9SMikko Perttunen #define EMC_CTT_TERM_CTRL			0x2dc
14773a7f0a9SMikko Perttunen #define EMC_ZCAL_INTERVAL			0x2e0
14873a7f0a9SMikko Perttunen #define EMC_ZCAL_WAIT_CNT			0x2e4
14973a7f0a9SMikko Perttunen 
15073a7f0a9SMikko Perttunen #define EMC_ZQ_CAL				0x2ec
15173a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_CMD				BIT(0)
15273a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_LONG				BIT(4)
15373a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_LONG_CMD_DEV0		\
15473a7f0a9SMikko Perttunen 	(DRAM_DEV_SEL_0 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD)
15573a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_LONG_CMD_DEV1		\
15673a7f0a9SMikko Perttunen 	(DRAM_DEV_SEL_1 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD)
15773a7f0a9SMikko Perttunen 
15873a7f0a9SMikko Perttunen #define EMC_XM2CMDPADCTRL			0x2f0
15973a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL			0x2f8
16073a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL2			0x2fc
16173a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE	BIT(0)
16273a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL2_VREF_ENABLE		BIT(5)
16373a7f0a9SMikko Perttunen #define EMC_XM2DQPADCTRL			0x300
16473a7f0a9SMikko Perttunen #define EMC_XM2DQPADCTRL2			0x304
16573a7f0a9SMikko Perttunen #define EMC_XM2CLKPADCTRL			0x308
16673a7f0a9SMikko Perttunen #define EMC_XM2COMPPADCTRL			0x30c
16773a7f0a9SMikko Perttunen #define EMC_XM2VTTGENPADCTRL			0x310
16873a7f0a9SMikko Perttunen #define EMC_XM2VTTGENPADCTRL2			0x314
16973a7f0a9SMikko Perttunen #define EMC_XM2VTTGENPADCTRL3			0x318
17073a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL4			0x320
17173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS0			0x328
17273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS1			0x32c
17373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS2			0x330
17473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS3			0x334
17573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS4			0x338
17673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS5			0x33c
17773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS6			0x340
17873a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS7			0x344
17973a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE0			0x348
18073a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE1			0x34c
18173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE2			0x350
18273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE3			0x354
18373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE4			0x358
18473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE5			0x35c
18573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE6			0x360
18673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE7			0x364
18773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ0			0x368
18873a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ1			0x36c
18973a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ2			0x370
19073a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ3			0x374
19173a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS0			0x3a8
19273a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS1			0x3ac
19373a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS2			0x3b0
19473a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS3			0x3b4
19573a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS4			0x3b8
19673a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS5			0x3bc
19773a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS6			0x3c0
19873a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS7			0x3c4
19973a7f0a9SMikko Perttunen #define EMC_STALL_THEN_EXE_AFTER_CLKCHANGE	0x3cc
20073a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL			0x3d8
20173a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_DATA_SEL_DPD		BIT(8)
20273a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_ODT_SEL_DPD		BIT(5)
20373a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_RESET_SEL_DPD		BIT(4)
20473a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_CA_SEL_DPD		BIT(3)
20573a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_CLK_SEL_DPD		BIT(2)
20673a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_DDR3_MASK	\
20773a7f0a9SMikko Perttunen 	((0xf << 2) | BIT(8))
20873a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_MASK \
20973a7f0a9SMikko Perttunen 	((0x3 << 2) | BIT(5) | BIT(8))
21073a7f0a9SMikko Perttunen #define EMC_PRE_REFRESH_REQ_CNT			0x3dc
21173a7f0a9SMikko Perttunen #define EMC_DYN_SELF_REF_CONTROL		0x3e0
21273a7f0a9SMikko Perttunen #define EMC_TXSRDLL				0x3e4
21373a7f0a9SMikko Perttunen #define EMC_CCFIFO_ADDR				0x3e8
21473a7f0a9SMikko Perttunen #define EMC_CCFIFO_DATA				0x3ec
21573a7f0a9SMikko Perttunen #define EMC_CCFIFO_STATUS			0x3f0
21673a7f0a9SMikko Perttunen #define EMC_CDB_CNTL_1				0x3f4
21773a7f0a9SMikko Perttunen #define EMC_CDB_CNTL_2				0x3f8
21873a7f0a9SMikko Perttunen #define EMC_XM2CLKPADCTRL2			0x3fc
21973a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG2			0x458
22073a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG3			0x45c
22173a7f0a9SMikko Perttunen #define EMC_IBDLY				0x468
22273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR0			0x46c
22373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR1			0x470
22473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR2			0x474
22573a7f0a9SMikko Perttunen #define EMC_DSR_VTTGEN_DRV			0x47c
22673a7f0a9SMikko Perttunen #define EMC_TXDSRVTTGEN				0x480
22773a7f0a9SMikko Perttunen #define EMC_XM2CMDPADCTRL4			0x484
22873a7f0a9SMikko Perttunen #define EMC_XM2CMDPADCTRL5			0x488
22973a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS8			0x4a0
23073a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS9			0x4a4
23173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS10			0x4a8
23273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS11			0x4ac
23373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS12			0x4b0
23473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS13			0x4b4
23573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS14			0x4b8
23673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS15			0x4bc
23773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE8			0x4c0
23873a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE9			0x4c4
23973a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE10			0x4c8
24073a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE11			0x4cc
24173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE12			0x4d0
24273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE13			0x4d4
24373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE14			0x4d8
24473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE15			0x4dc
24573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ4			0x4e0
24673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ5			0x4e4
24773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ6			0x4e8
24873a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ7			0x4ec
24973a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS8			0x520
25073a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS9			0x524
25173a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS10			0x528
25273a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS11			0x52c
25373a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS12			0x530
25473a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS13			0x534
25573a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS14			0x538
25673a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS15			0x53c
25773a7f0a9SMikko Perttunen #define EMC_CDB_CNTL_3				0x540
25873a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL5			0x544
25973a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL6			0x548
26073a7f0a9SMikko Perttunen #define EMC_XM2DQPADCTRL3			0x54c
26173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR3			0x550
26273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR4			0x554
26373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR5			0x558
26473a7f0a9SMikko Perttunen #define EMC_CFG_PIPE				0x560
26573a7f0a9SMikko Perttunen #define EMC_QPOP				0x564
26673a7f0a9SMikko Perttunen #define EMC_QUSE_WIDTH				0x568
26773a7f0a9SMikko Perttunen #define EMC_PUTERM_WIDTH			0x56c
26873a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0				0x570
26973a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX BIT(3)
27073a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN BIT(2)
27173a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD	BIT(1)
27273a7f0a9SMikko Perttunen #define EMC_PUTERM_ADJ				0x574
27373a7f0a9SMikko Perttunen 
27473a7f0a9SMikko Perttunen #define DRAM_DEV_SEL_ALL			0
27573a7f0a9SMikko Perttunen #define DRAM_DEV_SEL_0				(2 << 30)
27673a7f0a9SMikko Perttunen #define DRAM_DEV_SEL_1				(1 << 30)
27773a7f0a9SMikko Perttunen 
27873a7f0a9SMikko Perttunen #define EMC_CFG_POWER_FEATURES_MASK		\
27973a7f0a9SMikko Perttunen 	(EMC_CFG_DYN_SREF | EMC_CFG_DRAM_ACPD | EMC_CFG_DRAM_CLKSTOP_SR | \
28073a7f0a9SMikko Perttunen 	EMC_CFG_DRAM_CLKSTOP_PD | EMC_CFG_DSR_VTTGEN_DRV_EN)
28173a7f0a9SMikko Perttunen #define EMC_REFCTRL_DEV_SEL(n) (((n > 1) ? 0 : 2) << EMC_REFCTRL_DEV_SEL_SHIFT)
28273a7f0a9SMikko Perttunen #define EMC_DRAM_DEV_SEL(n) ((n > 1) ? DRAM_DEV_SEL_ALL : DRAM_DEV_SEL_0)
28373a7f0a9SMikko Perttunen 
28473a7f0a9SMikko Perttunen /* Maximum amount of time in us. to wait for changes to become effective */
28573a7f0a9SMikko Perttunen #define EMC_STATUS_UPDATE_TIMEOUT		1000
28673a7f0a9SMikko Perttunen 
28773a7f0a9SMikko Perttunen enum emc_dram_type {
28873a7f0a9SMikko Perttunen 	DRAM_TYPE_DDR3 = 0,
28973a7f0a9SMikko Perttunen 	DRAM_TYPE_DDR1 = 1,
29073a7f0a9SMikko Perttunen 	DRAM_TYPE_LPDDR3 = 2,
29173a7f0a9SMikko Perttunen 	DRAM_TYPE_DDR2 = 3
29273a7f0a9SMikko Perttunen };
29373a7f0a9SMikko Perttunen 
29473a7f0a9SMikko Perttunen enum emc_dll_change {
29573a7f0a9SMikko Perttunen 	DLL_CHANGE_NONE,
29673a7f0a9SMikko Perttunen 	DLL_CHANGE_ON,
29773a7f0a9SMikko Perttunen 	DLL_CHANGE_OFF
29873a7f0a9SMikko Perttunen };
29973a7f0a9SMikko Perttunen 
30073a7f0a9SMikko Perttunen static const unsigned long emc_burst_regs[] = {
30173a7f0a9SMikko Perttunen 	EMC_RC,
30273a7f0a9SMikko Perttunen 	EMC_RFC,
30373a7f0a9SMikko Perttunen 	EMC_RFC_SLR,
30473a7f0a9SMikko Perttunen 	EMC_RAS,
30573a7f0a9SMikko Perttunen 	EMC_RP,
30673a7f0a9SMikko Perttunen 	EMC_R2W,
30773a7f0a9SMikko Perttunen 	EMC_W2R,
30873a7f0a9SMikko Perttunen 	EMC_R2P,
30973a7f0a9SMikko Perttunen 	EMC_W2P,
31073a7f0a9SMikko Perttunen 	EMC_RD_RCD,
31173a7f0a9SMikko Perttunen 	EMC_WR_RCD,
31273a7f0a9SMikko Perttunen 	EMC_RRD,
31373a7f0a9SMikko Perttunen 	EMC_REXT,
31473a7f0a9SMikko Perttunen 	EMC_WEXT,
31573a7f0a9SMikko Perttunen 	EMC_WDV,
31673a7f0a9SMikko Perttunen 	EMC_WDV_MASK,
31773a7f0a9SMikko Perttunen 	EMC_QUSE,
31873a7f0a9SMikko Perttunen 	EMC_QUSE_WIDTH,
31973a7f0a9SMikko Perttunen 	EMC_IBDLY,
32073a7f0a9SMikko Perttunen 	EMC_EINPUT,
32173a7f0a9SMikko Perttunen 	EMC_EINPUT_DURATION,
32273a7f0a9SMikko Perttunen 	EMC_PUTERM_EXTRA,
32373a7f0a9SMikko Perttunen 	EMC_PUTERM_WIDTH,
32473a7f0a9SMikko Perttunen 	EMC_PUTERM_ADJ,
32573a7f0a9SMikko Perttunen 	EMC_CDB_CNTL_1,
32673a7f0a9SMikko Perttunen 	EMC_CDB_CNTL_2,
32773a7f0a9SMikko Perttunen 	EMC_CDB_CNTL_3,
32873a7f0a9SMikko Perttunen 	EMC_QRST,
32973a7f0a9SMikko Perttunen 	EMC_QSAFE,
33073a7f0a9SMikko Perttunen 	EMC_RDV,
33173a7f0a9SMikko Perttunen 	EMC_RDV_MASK,
33273a7f0a9SMikko Perttunen 	EMC_REFRESH,
33373a7f0a9SMikko Perttunen 	EMC_BURST_REFRESH_NUM,
33473a7f0a9SMikko Perttunen 	EMC_PRE_REFRESH_REQ_CNT,
33573a7f0a9SMikko Perttunen 	EMC_PDEX2WR,
33673a7f0a9SMikko Perttunen 	EMC_PDEX2RD,
33773a7f0a9SMikko Perttunen 	EMC_PCHG2PDEN,
33873a7f0a9SMikko Perttunen 	EMC_ACT2PDEN,
33973a7f0a9SMikko Perttunen 	EMC_AR2PDEN,
34073a7f0a9SMikko Perttunen 	EMC_RW2PDEN,
34173a7f0a9SMikko Perttunen 	EMC_TXSR,
34273a7f0a9SMikko Perttunen 	EMC_TXSRDLL,
34373a7f0a9SMikko Perttunen 	EMC_TCKE,
34473a7f0a9SMikko Perttunen 	EMC_TCKESR,
34573a7f0a9SMikko Perttunen 	EMC_TPD,
34673a7f0a9SMikko Perttunen 	EMC_TFAW,
34773a7f0a9SMikko Perttunen 	EMC_TRPAB,
34873a7f0a9SMikko Perttunen 	EMC_TCLKSTABLE,
34973a7f0a9SMikko Perttunen 	EMC_TCLKSTOP,
35073a7f0a9SMikko Perttunen 	EMC_TREFBW,
35173a7f0a9SMikko Perttunen 	EMC_FBIO_CFG6,
35273a7f0a9SMikko Perttunen 	EMC_ODT_WRITE,
35373a7f0a9SMikko Perttunen 	EMC_ODT_READ,
35473a7f0a9SMikko Perttunen 	EMC_FBIO_CFG5,
35573a7f0a9SMikko Perttunen 	EMC_CFG_DIG_DLL,
35673a7f0a9SMikko Perttunen 	EMC_CFG_DIG_DLL_PERIOD,
35773a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS0,
35873a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS1,
35973a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS2,
36073a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS3,
36173a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS4,
36273a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS5,
36373a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS6,
36473a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS7,
36573a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS8,
36673a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS9,
36773a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS10,
36873a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS11,
36973a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS12,
37073a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS13,
37173a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS14,
37273a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS15,
37373a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE0,
37473a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE1,
37573a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE2,
37673a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE3,
37773a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE4,
37873a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE5,
37973a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE6,
38073a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE7,
38173a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_ADDR0,
38273a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_ADDR1,
38373a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_ADDR2,
38473a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_ADDR3,
38573a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_ADDR4,
38673a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_ADDR5,
38773a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE8,
38873a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE9,
38973a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE10,
39073a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE11,
39173a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE12,
39273a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE13,
39373a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE14,
39473a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE15,
39573a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS0,
39673a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS1,
39773a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS2,
39873a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS3,
39973a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS4,
40073a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS5,
40173a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS6,
40273a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS7,
40373a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS8,
40473a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS9,
40573a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS10,
40673a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS11,
40773a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS12,
40873a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS13,
40973a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS14,
41073a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS15,
41173a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ0,
41273a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ1,
41373a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ2,
41473a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ3,
41573a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ4,
41673a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ5,
41773a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ6,
41873a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ7,
41973a7f0a9SMikko Perttunen 	EMC_XM2CMDPADCTRL,
42073a7f0a9SMikko Perttunen 	EMC_XM2CMDPADCTRL4,
42173a7f0a9SMikko Perttunen 	EMC_XM2CMDPADCTRL5,
42273a7f0a9SMikko Perttunen 	EMC_XM2DQPADCTRL2,
42373a7f0a9SMikko Perttunen 	EMC_XM2DQPADCTRL3,
42473a7f0a9SMikko Perttunen 	EMC_XM2CLKPADCTRL,
42573a7f0a9SMikko Perttunen 	EMC_XM2CLKPADCTRL2,
42673a7f0a9SMikko Perttunen 	EMC_XM2COMPPADCTRL,
42773a7f0a9SMikko Perttunen 	EMC_XM2VTTGENPADCTRL,
42873a7f0a9SMikko Perttunen 	EMC_XM2VTTGENPADCTRL2,
42973a7f0a9SMikko Perttunen 	EMC_XM2VTTGENPADCTRL3,
43073a7f0a9SMikko Perttunen 	EMC_XM2DQSPADCTRL3,
43173a7f0a9SMikko Perttunen 	EMC_XM2DQSPADCTRL4,
43273a7f0a9SMikko Perttunen 	EMC_XM2DQSPADCTRL5,
43373a7f0a9SMikko Perttunen 	EMC_XM2DQSPADCTRL6,
43473a7f0a9SMikko Perttunen 	EMC_DSR_VTTGEN_DRV,
43573a7f0a9SMikko Perttunen 	EMC_TXDSRVTTGEN,
43673a7f0a9SMikko Perttunen 	EMC_FBIO_SPARE,
43773a7f0a9SMikko Perttunen 	EMC_ZCAL_WAIT_CNT,
43873a7f0a9SMikko Perttunen 	EMC_MRS_WAIT_CNT2,
43973a7f0a9SMikko Perttunen 	EMC_CTT,
44073a7f0a9SMikko Perttunen 	EMC_CTT_DURATION,
44173a7f0a9SMikko Perttunen 	EMC_CFG_PIPE,
44273a7f0a9SMikko Perttunen 	EMC_DYN_SELF_REF_CONTROL,
44373a7f0a9SMikko Perttunen 	EMC_QPOP
44473a7f0a9SMikko Perttunen };
44573a7f0a9SMikko Perttunen 
44673a7f0a9SMikko Perttunen struct emc_timing {
44773a7f0a9SMikko Perttunen 	unsigned long rate;
44873a7f0a9SMikko Perttunen 
44973a7f0a9SMikko Perttunen 	u32 emc_burst_data[ARRAY_SIZE(emc_burst_regs)];
45073a7f0a9SMikko Perttunen 
45173a7f0a9SMikko Perttunen 	u32 emc_auto_cal_config;
45273a7f0a9SMikko Perttunen 	u32 emc_auto_cal_config2;
45373a7f0a9SMikko Perttunen 	u32 emc_auto_cal_config3;
45473a7f0a9SMikko Perttunen 	u32 emc_auto_cal_interval;
45573a7f0a9SMikko Perttunen 	u32 emc_bgbias_ctl0;
45673a7f0a9SMikko Perttunen 	u32 emc_cfg;
45773a7f0a9SMikko Perttunen 	u32 emc_cfg_2;
45873a7f0a9SMikko Perttunen 	u32 emc_ctt_term_ctrl;
45973a7f0a9SMikko Perttunen 	u32 emc_mode_1;
46073a7f0a9SMikko Perttunen 	u32 emc_mode_2;
46173a7f0a9SMikko Perttunen 	u32 emc_mode_4;
46273a7f0a9SMikko Perttunen 	u32 emc_mode_reset;
46373a7f0a9SMikko Perttunen 	u32 emc_mrs_wait_cnt;
46473a7f0a9SMikko Perttunen 	u32 emc_sel_dpd_ctrl;
46573a7f0a9SMikko Perttunen 	u32 emc_xm2dqspadctrl2;
46673a7f0a9SMikko Perttunen 	u32 emc_zcal_cnt_long;
46773a7f0a9SMikko Perttunen 	u32 emc_zcal_interval;
46873a7f0a9SMikko Perttunen };
46973a7f0a9SMikko Perttunen 
470*380def2dSDmitry Osipenko enum emc_rate_request_type {
471*380def2dSDmitry Osipenko 	EMC_RATE_DEBUG,
472*380def2dSDmitry Osipenko 	EMC_RATE_ICC,
473*380def2dSDmitry Osipenko 	EMC_RATE_TYPE_MAX,
474*380def2dSDmitry Osipenko };
475*380def2dSDmitry Osipenko 
476*380def2dSDmitry Osipenko struct emc_rate_request {
477*380def2dSDmitry Osipenko 	unsigned long min_rate;
478*380def2dSDmitry Osipenko 	unsigned long max_rate;
479*380def2dSDmitry Osipenko };
480*380def2dSDmitry Osipenko 
48173a7f0a9SMikko Perttunen struct tegra_emc {
48273a7f0a9SMikko Perttunen 	struct device *dev;
48373a7f0a9SMikko Perttunen 
48473a7f0a9SMikko Perttunen 	struct tegra_mc *mc;
48573a7f0a9SMikko Perttunen 
48673a7f0a9SMikko Perttunen 	void __iomem *regs;
48773a7f0a9SMikko Perttunen 
4886b9acd93SThierry Reding 	struct clk *clk;
4896b9acd93SThierry Reding 
49073a7f0a9SMikko Perttunen 	enum emc_dram_type dram_type;
491*380def2dSDmitry Osipenko 	unsigned int dram_bus_width;
49273a7f0a9SMikko Perttunen 	unsigned int dram_num;
49373a7f0a9SMikko Perttunen 
49473a7f0a9SMikko Perttunen 	struct emc_timing last_timing;
49573a7f0a9SMikko Perttunen 	struct emc_timing *timings;
49673a7f0a9SMikko Perttunen 	unsigned int num_timings;
4976b9acd93SThierry Reding 
4986b9acd93SThierry Reding 	struct {
4996b9acd93SThierry Reding 		struct dentry *root;
5006b9acd93SThierry Reding 		unsigned long min_rate;
5016b9acd93SThierry Reding 		unsigned long max_rate;
5026b9acd93SThierry Reding 	} debugfs;
503*380def2dSDmitry Osipenko 
504*380def2dSDmitry Osipenko 	struct icc_provider provider;
505*380def2dSDmitry Osipenko 
506*380def2dSDmitry Osipenko 	/*
507*380def2dSDmitry Osipenko 	 * There are multiple sources in the EMC driver which could request
508*380def2dSDmitry Osipenko 	 * a min/max clock rate, these rates are contained in this array.
509*380def2dSDmitry Osipenko 	 */
510*380def2dSDmitry Osipenko 	struct emc_rate_request requested_rate[EMC_RATE_TYPE_MAX];
511*380def2dSDmitry Osipenko 
512*380def2dSDmitry Osipenko 	/* protect shared rate-change code path */
513*380def2dSDmitry Osipenko 	struct mutex rate_lock;
51473a7f0a9SMikko Perttunen };
51573a7f0a9SMikko Perttunen 
51673a7f0a9SMikko Perttunen /* Timing change sequence functions */
51773a7f0a9SMikko Perttunen 
51873a7f0a9SMikko Perttunen static void emc_ccfifo_writel(struct tegra_emc *emc, u32 value,
51973a7f0a9SMikko Perttunen 			      unsigned long offset)
52073a7f0a9SMikko Perttunen {
52173a7f0a9SMikko Perttunen 	writel(value, emc->regs + EMC_CCFIFO_DATA);
52273a7f0a9SMikko Perttunen 	writel(offset, emc->regs + EMC_CCFIFO_ADDR);
52373a7f0a9SMikko Perttunen }
52473a7f0a9SMikko Perttunen 
52573a7f0a9SMikko Perttunen static void emc_seq_update_timing(struct tegra_emc *emc)
52673a7f0a9SMikko Perttunen {
52773a7f0a9SMikko Perttunen 	unsigned int i;
52873a7f0a9SMikko Perttunen 	u32 value;
52973a7f0a9SMikko Perttunen 
53073a7f0a9SMikko Perttunen 	writel(1, emc->regs + EMC_TIMING_CONTROL);
53173a7f0a9SMikko Perttunen 
53273a7f0a9SMikko Perttunen 	for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
53373a7f0a9SMikko Perttunen 		value = readl(emc->regs + EMC_STATUS);
53473a7f0a9SMikko Perttunen 		if ((value & EMC_STATUS_TIMING_UPDATE_STALLED) == 0)
53573a7f0a9SMikko Perttunen 			return;
53673a7f0a9SMikko Perttunen 		udelay(1);
53773a7f0a9SMikko Perttunen 	}
53873a7f0a9SMikko Perttunen 
53973a7f0a9SMikko Perttunen 	dev_err(emc->dev, "timing update timed out\n");
54073a7f0a9SMikko Perttunen }
54173a7f0a9SMikko Perttunen 
54273a7f0a9SMikko Perttunen static void emc_seq_disable_auto_cal(struct tegra_emc *emc)
54373a7f0a9SMikko Perttunen {
54473a7f0a9SMikko Perttunen 	unsigned int i;
54573a7f0a9SMikko Perttunen 	u32 value;
54673a7f0a9SMikko Perttunen 
54773a7f0a9SMikko Perttunen 	writel(0, emc->regs + EMC_AUTO_CAL_INTERVAL);
54873a7f0a9SMikko Perttunen 
54973a7f0a9SMikko Perttunen 	for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
55073a7f0a9SMikko Perttunen 		value = readl(emc->regs + EMC_AUTO_CAL_STATUS);
55173a7f0a9SMikko Perttunen 		if ((value & EMC_AUTO_CAL_STATUS_ACTIVE) == 0)
55273a7f0a9SMikko Perttunen 			return;
55373a7f0a9SMikko Perttunen 		udelay(1);
55473a7f0a9SMikko Perttunen 	}
55573a7f0a9SMikko Perttunen 
55673a7f0a9SMikko Perttunen 	dev_err(emc->dev, "auto cal disable timed out\n");
55773a7f0a9SMikko Perttunen }
55873a7f0a9SMikko Perttunen 
55973a7f0a9SMikko Perttunen static void emc_seq_wait_clkchange(struct tegra_emc *emc)
56073a7f0a9SMikko Perttunen {
56173a7f0a9SMikko Perttunen 	unsigned int i;
56273a7f0a9SMikko Perttunen 	u32 value;
56373a7f0a9SMikko Perttunen 
56473a7f0a9SMikko Perttunen 	for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
56573a7f0a9SMikko Perttunen 		value = readl(emc->regs + EMC_INTSTATUS);
56673a7f0a9SMikko Perttunen 		if (value & EMC_INTSTATUS_CLKCHANGE_COMPLETE)
56773a7f0a9SMikko Perttunen 			return;
56873a7f0a9SMikko Perttunen 		udelay(1);
56973a7f0a9SMikko Perttunen 	}
57073a7f0a9SMikko Perttunen 
57173a7f0a9SMikko Perttunen 	dev_err(emc->dev, "clock change timed out\n");
57273a7f0a9SMikko Perttunen }
57373a7f0a9SMikko Perttunen 
57473a7f0a9SMikko Perttunen static struct emc_timing *tegra_emc_find_timing(struct tegra_emc *emc,
57573a7f0a9SMikko Perttunen 						unsigned long rate)
57673a7f0a9SMikko Perttunen {
57773a7f0a9SMikko Perttunen 	struct emc_timing *timing = NULL;
57873a7f0a9SMikko Perttunen 	unsigned int i;
57973a7f0a9SMikko Perttunen 
58073a7f0a9SMikko Perttunen 	for (i = 0; i < emc->num_timings; i++) {
58173a7f0a9SMikko Perttunen 		if (emc->timings[i].rate == rate) {
58273a7f0a9SMikko Perttunen 			timing = &emc->timings[i];
58373a7f0a9SMikko Perttunen 			break;
58473a7f0a9SMikko Perttunen 		}
58573a7f0a9SMikko Perttunen 	}
58673a7f0a9SMikko Perttunen 
58773a7f0a9SMikko Perttunen 	if (!timing) {
58873a7f0a9SMikko Perttunen 		dev_err(emc->dev, "no timing for rate %lu\n", rate);
58973a7f0a9SMikko Perttunen 		return NULL;
59073a7f0a9SMikko Perttunen 	}
59173a7f0a9SMikko Perttunen 
59273a7f0a9SMikko Perttunen 	return timing;
59373a7f0a9SMikko Perttunen }
59473a7f0a9SMikko Perttunen 
595281462e5SDmitry Osipenko static int tegra_emc_prepare_timing_change(struct tegra_emc *emc,
59673a7f0a9SMikko Perttunen 					   unsigned long rate)
59773a7f0a9SMikko Perttunen {
59873a7f0a9SMikko Perttunen 	struct emc_timing *timing = tegra_emc_find_timing(emc, rate);
59973a7f0a9SMikko Perttunen 	struct emc_timing *last = &emc->last_timing;
60073a7f0a9SMikko Perttunen 	enum emc_dll_change dll_change;
60173a7f0a9SMikko Perttunen 	unsigned int pre_wait = 0;
60273a7f0a9SMikko Perttunen 	u32 val, val2, mask;
60373a7f0a9SMikko Perttunen 	bool update = false;
60473a7f0a9SMikko Perttunen 	unsigned int i;
60573a7f0a9SMikko Perttunen 
60673a7f0a9SMikko Perttunen 	if (!timing)
60773a7f0a9SMikko Perttunen 		return -ENOENT;
60873a7f0a9SMikko Perttunen 
60973a7f0a9SMikko Perttunen 	if ((last->emc_mode_1 & 0x1) == (timing->emc_mode_1 & 0x1))
61073a7f0a9SMikko Perttunen 		dll_change = DLL_CHANGE_NONE;
61173a7f0a9SMikko Perttunen 	else if (timing->emc_mode_1 & 0x1)
61273a7f0a9SMikko Perttunen 		dll_change = DLL_CHANGE_ON;
61373a7f0a9SMikko Perttunen 	else
61473a7f0a9SMikko Perttunen 		dll_change = DLL_CHANGE_OFF;
61573a7f0a9SMikko Perttunen 
61673a7f0a9SMikko Perttunen 	/* Clear CLKCHANGE_COMPLETE interrupts */
61773a7f0a9SMikko Perttunen 	writel(EMC_INTSTATUS_CLKCHANGE_COMPLETE, emc->regs + EMC_INTSTATUS);
61873a7f0a9SMikko Perttunen 
61973a7f0a9SMikko Perttunen 	/* Disable dynamic self-refresh */
62073a7f0a9SMikko Perttunen 	val = readl(emc->regs + EMC_CFG);
62173a7f0a9SMikko Perttunen 	if (val & EMC_CFG_PWR_MASK) {
62273a7f0a9SMikko Perttunen 		val &= ~EMC_CFG_POWER_FEATURES_MASK;
62373a7f0a9SMikko Perttunen 		writel(val, emc->regs + EMC_CFG);
62473a7f0a9SMikko Perttunen 
62573a7f0a9SMikko Perttunen 		pre_wait = 5;
62673a7f0a9SMikko Perttunen 	}
62773a7f0a9SMikko Perttunen 
62873a7f0a9SMikko Perttunen 	/* Disable SEL_DPD_CTRL for clock change */
62973a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_DDR3)
63073a7f0a9SMikko Perttunen 		mask = EMC_SEL_DPD_CTRL_DDR3_MASK;
63173a7f0a9SMikko Perttunen 	else
63273a7f0a9SMikko Perttunen 		mask = EMC_SEL_DPD_CTRL_MASK;
63373a7f0a9SMikko Perttunen 
63473a7f0a9SMikko Perttunen 	val = readl(emc->regs + EMC_SEL_DPD_CTRL);
63573a7f0a9SMikko Perttunen 	if (val & mask) {
63673a7f0a9SMikko Perttunen 		val &= ~mask;
63773a7f0a9SMikko Perttunen 		writel(val, emc->regs + EMC_SEL_DPD_CTRL);
63873a7f0a9SMikko Perttunen 	}
63973a7f0a9SMikko Perttunen 
64073a7f0a9SMikko Perttunen 	/* Prepare DQ/DQS for clock change */
64173a7f0a9SMikko Perttunen 	val = readl(emc->regs + EMC_BGBIAS_CTL0);
64273a7f0a9SMikko Perttunen 	val2 = last->emc_bgbias_ctl0;
64373a7f0a9SMikko Perttunen 	if (!(timing->emc_bgbias_ctl0 &
64473a7f0a9SMikko Perttunen 	      EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) &&
64573a7f0a9SMikko Perttunen 	    (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX)) {
64673a7f0a9SMikko Perttunen 		val2 &= ~EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX;
64773a7f0a9SMikko Perttunen 		update = true;
64873a7f0a9SMikko Perttunen 	}
64973a7f0a9SMikko Perttunen 
65073a7f0a9SMikko Perttunen 	if ((val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD) ||
65173a7f0a9SMikko Perttunen 	    (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN)) {
65273a7f0a9SMikko Perttunen 		update = true;
65373a7f0a9SMikko Perttunen 	}
65473a7f0a9SMikko Perttunen 
65573a7f0a9SMikko Perttunen 	if (update) {
65673a7f0a9SMikko Perttunen 		writel(val2, emc->regs + EMC_BGBIAS_CTL0);
65773a7f0a9SMikko Perttunen 		if (pre_wait < 5)
65873a7f0a9SMikko Perttunen 			pre_wait = 5;
65973a7f0a9SMikko Perttunen 	}
66073a7f0a9SMikko Perttunen 
66173a7f0a9SMikko Perttunen 	update = false;
66273a7f0a9SMikko Perttunen 	val = readl(emc->regs + EMC_XM2DQSPADCTRL2);
66373a7f0a9SMikko Perttunen 	if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_VREF_ENABLE &&
66473a7f0a9SMikko Perttunen 	    !(val & EMC_XM2DQSPADCTRL2_VREF_ENABLE)) {
66573a7f0a9SMikko Perttunen 		val |= EMC_XM2DQSPADCTRL2_VREF_ENABLE;
66673a7f0a9SMikko Perttunen 		update = true;
66773a7f0a9SMikko Perttunen 	}
66873a7f0a9SMikko Perttunen 
66973a7f0a9SMikko Perttunen 	if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE &&
67073a7f0a9SMikko Perttunen 	    !(val & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE)) {
67173a7f0a9SMikko Perttunen 		val |= EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE;
67273a7f0a9SMikko Perttunen 		update = true;
67373a7f0a9SMikko Perttunen 	}
67473a7f0a9SMikko Perttunen 
67573a7f0a9SMikko Perttunen 	if (update) {
67673a7f0a9SMikko Perttunen 		writel(val, emc->regs + EMC_XM2DQSPADCTRL2);
67773a7f0a9SMikko Perttunen 		if (pre_wait < 30)
67873a7f0a9SMikko Perttunen 			pre_wait = 30;
67973a7f0a9SMikko Perttunen 	}
68073a7f0a9SMikko Perttunen 
68173a7f0a9SMikko Perttunen 	/* Wait to settle */
68273a7f0a9SMikko Perttunen 	if (pre_wait) {
68373a7f0a9SMikko Perttunen 		emc_seq_update_timing(emc);
68473a7f0a9SMikko Perttunen 		udelay(pre_wait);
68573a7f0a9SMikko Perttunen 	}
68673a7f0a9SMikko Perttunen 
68773a7f0a9SMikko Perttunen 	/* Program CTT_TERM control */
68873a7f0a9SMikko Perttunen 	if (last->emc_ctt_term_ctrl != timing->emc_ctt_term_ctrl) {
68973a7f0a9SMikko Perttunen 		emc_seq_disable_auto_cal(emc);
69073a7f0a9SMikko Perttunen 		writel(timing->emc_ctt_term_ctrl,
69173a7f0a9SMikko Perttunen 		       emc->regs + EMC_CTT_TERM_CTRL);
69273a7f0a9SMikko Perttunen 		emc_seq_update_timing(emc);
69373a7f0a9SMikko Perttunen 	}
69473a7f0a9SMikko Perttunen 
69573a7f0a9SMikko Perttunen 	/* Program burst shadow registers */
69673a7f0a9SMikko Perttunen 	for (i = 0; i < ARRAY_SIZE(timing->emc_burst_data); ++i)
69773a7f0a9SMikko Perttunen 		writel(timing->emc_burst_data[i],
69873a7f0a9SMikko Perttunen 		       emc->regs + emc_burst_regs[i]);
69973a7f0a9SMikko Perttunen 
70073a7f0a9SMikko Perttunen 	writel(timing->emc_xm2dqspadctrl2, emc->regs + EMC_XM2DQSPADCTRL2);
70173a7f0a9SMikko Perttunen 	writel(timing->emc_zcal_interval, emc->regs + EMC_ZCAL_INTERVAL);
70273a7f0a9SMikko Perttunen 
70373a7f0a9SMikko Perttunen 	tegra_mc_write_emem_configuration(emc->mc, timing->rate);
70473a7f0a9SMikko Perttunen 
70573a7f0a9SMikko Perttunen 	val = timing->emc_cfg & ~EMC_CFG_POWER_FEATURES_MASK;
70673a7f0a9SMikko Perttunen 	emc_ccfifo_writel(emc, val, EMC_CFG);
70773a7f0a9SMikko Perttunen 
70873a7f0a9SMikko Perttunen 	/* Program AUTO_CAL_CONFIG */
70973a7f0a9SMikko Perttunen 	if (timing->emc_auto_cal_config2 != last->emc_auto_cal_config2)
71073a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, timing->emc_auto_cal_config2,
71173a7f0a9SMikko Perttunen 				  EMC_AUTO_CAL_CONFIG2);
71273a7f0a9SMikko Perttunen 
71373a7f0a9SMikko Perttunen 	if (timing->emc_auto_cal_config3 != last->emc_auto_cal_config3)
71473a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, timing->emc_auto_cal_config3,
71573a7f0a9SMikko Perttunen 				  EMC_AUTO_CAL_CONFIG3);
71673a7f0a9SMikko Perttunen 
71773a7f0a9SMikko Perttunen 	if (timing->emc_auto_cal_config != last->emc_auto_cal_config) {
71873a7f0a9SMikko Perttunen 		val = timing->emc_auto_cal_config;
71973a7f0a9SMikko Perttunen 		val &= EMC_AUTO_CAL_CONFIG_AUTO_CAL_START;
72073a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, val, EMC_AUTO_CAL_CONFIG);
72173a7f0a9SMikko Perttunen 	}
72273a7f0a9SMikko Perttunen 
72373a7f0a9SMikko Perttunen 	/* DDR3: predict MRS long wait count */
72473a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_DDR3 &&
72573a7f0a9SMikko Perttunen 	    dll_change == DLL_CHANGE_ON) {
72673a7f0a9SMikko Perttunen 		u32 cnt = 512;
72773a7f0a9SMikko Perttunen 
72873a7f0a9SMikko Perttunen 		if (timing->emc_zcal_interval != 0 &&
72973a7f0a9SMikko Perttunen 		    last->emc_zcal_interval == 0)
73073a7f0a9SMikko Perttunen 			cnt -= emc->dram_num * 256;
73173a7f0a9SMikko Perttunen 
73273a7f0a9SMikko Perttunen 		val = (timing->emc_mrs_wait_cnt
73373a7f0a9SMikko Perttunen 			& EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK)
73473a7f0a9SMikko Perttunen 			>> EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT;
73573a7f0a9SMikko Perttunen 		if (cnt < val)
73673a7f0a9SMikko Perttunen 			cnt = val;
73773a7f0a9SMikko Perttunen 
73873a7f0a9SMikko Perttunen 		val = timing->emc_mrs_wait_cnt
73973a7f0a9SMikko Perttunen 			& ~EMC_MRS_WAIT_CNT_LONG_WAIT_MASK;
74073a7f0a9SMikko Perttunen 		val |= (cnt << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT)
74173a7f0a9SMikko Perttunen 			& EMC_MRS_WAIT_CNT_LONG_WAIT_MASK;
74273a7f0a9SMikko Perttunen 
74373a7f0a9SMikko Perttunen 		writel(val, emc->regs + EMC_MRS_WAIT_CNT);
74473a7f0a9SMikko Perttunen 	}
74573a7f0a9SMikko Perttunen 
74673a7f0a9SMikko Perttunen 	val = timing->emc_cfg_2;
74773a7f0a9SMikko Perttunen 	val &= ~EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR;
74873a7f0a9SMikko Perttunen 	emc_ccfifo_writel(emc, val, EMC_CFG_2);
74973a7f0a9SMikko Perttunen 
75073a7f0a9SMikko Perttunen 	/* DDR3: Turn off DLL and enter self-refresh */
75173a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_DDR3 && dll_change == DLL_CHANGE_OFF)
75273a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS);
75373a7f0a9SMikko Perttunen 
75473a7f0a9SMikko Perttunen 	/* Disable refresh controller */
75573a7f0a9SMikko Perttunen 	emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num),
75673a7f0a9SMikko Perttunen 			  EMC_REFCTRL);
75773a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_DDR3)
75873a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num) |
75973a7f0a9SMikko Perttunen 				       EMC_SELF_REF_CMD_ENABLED,
76073a7f0a9SMikko Perttunen 				  EMC_SELF_REF);
76173a7f0a9SMikko Perttunen 
76273a7f0a9SMikko Perttunen 	/* Flow control marker */
76373a7f0a9SMikko Perttunen 	emc_ccfifo_writel(emc, 1, EMC_STALL_THEN_EXE_AFTER_CLKCHANGE);
76473a7f0a9SMikko Perttunen 
76573a7f0a9SMikko Perttunen 	/* DDR3: Exit self-refresh */
76673a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_DDR3)
76773a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num),
76873a7f0a9SMikko Perttunen 				  EMC_SELF_REF);
76973a7f0a9SMikko Perttunen 	emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num) |
77073a7f0a9SMikko Perttunen 			       EMC_REFCTRL_ENABLE,
77173a7f0a9SMikko Perttunen 			  EMC_REFCTRL);
77273a7f0a9SMikko Perttunen 
77373a7f0a9SMikko Perttunen 	/* Set DRAM mode registers */
77473a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_DDR3) {
77573a7f0a9SMikko Perttunen 		if (timing->emc_mode_1 != last->emc_mode_1)
77673a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS);
77773a7f0a9SMikko Perttunen 		if (timing->emc_mode_2 != last->emc_mode_2)
77873a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_EMRS2);
77973a7f0a9SMikko Perttunen 
78073a7f0a9SMikko Perttunen 		if ((timing->emc_mode_reset != last->emc_mode_reset) ||
78173a7f0a9SMikko Perttunen 		    dll_change == DLL_CHANGE_ON) {
78273a7f0a9SMikko Perttunen 			val = timing->emc_mode_reset;
78373a7f0a9SMikko Perttunen 			if (dll_change == DLL_CHANGE_ON) {
78473a7f0a9SMikko Perttunen 				val |= EMC_MODE_SET_DLL_RESET;
78573a7f0a9SMikko Perttunen 				val |= EMC_MODE_SET_LONG_CNT;
78673a7f0a9SMikko Perttunen 			} else {
78773a7f0a9SMikko Perttunen 				val &= ~EMC_MODE_SET_DLL_RESET;
78873a7f0a9SMikko Perttunen 			}
78973a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, val, EMC_MRS);
79073a7f0a9SMikko Perttunen 		}
79173a7f0a9SMikko Perttunen 	} else {
79273a7f0a9SMikko Perttunen 		if (timing->emc_mode_2 != last->emc_mode_2)
79373a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_MRW2);
79473a7f0a9SMikko Perttunen 		if (timing->emc_mode_1 != last->emc_mode_1)
79573a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_MRW);
79673a7f0a9SMikko Perttunen 		if (timing->emc_mode_4 != last->emc_mode_4)
79773a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, timing->emc_mode_4, EMC_MRW4);
79873a7f0a9SMikko Perttunen 	}
79973a7f0a9SMikko Perttunen 
80073a7f0a9SMikko Perttunen 	/*  Issue ZCAL command if turning ZCAL on */
80173a7f0a9SMikko Perttunen 	if (timing->emc_zcal_interval != 0 && last->emc_zcal_interval == 0) {
80273a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV0, EMC_ZQ_CAL);
80373a7f0a9SMikko Perttunen 		if (emc->dram_num > 1)
80473a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV1,
80573a7f0a9SMikko Perttunen 					  EMC_ZQ_CAL);
80673a7f0a9SMikko Perttunen 	}
80773a7f0a9SMikko Perttunen 
80873a7f0a9SMikko Perttunen 	/*  Write to RO register to remove stall after change */
80973a7f0a9SMikko Perttunen 	emc_ccfifo_writel(emc, 0, EMC_CCFIFO_STATUS);
81073a7f0a9SMikko Perttunen 
81173a7f0a9SMikko Perttunen 	if (timing->emc_cfg_2 & EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR)
81273a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, timing->emc_cfg_2, EMC_CFG_2);
81373a7f0a9SMikko Perttunen 
81473a7f0a9SMikko Perttunen 	/* Disable AUTO_CAL for clock change */
81573a7f0a9SMikko Perttunen 	emc_seq_disable_auto_cal(emc);
81673a7f0a9SMikko Perttunen 
81773a7f0a9SMikko Perttunen 	/* Read register to wait until programming has settled */
81873a7f0a9SMikko Perttunen 	readl(emc->regs + EMC_INTSTATUS);
81973a7f0a9SMikko Perttunen 
82073a7f0a9SMikko Perttunen 	return 0;
82173a7f0a9SMikko Perttunen }
82273a7f0a9SMikko Perttunen 
823281462e5SDmitry Osipenko static void tegra_emc_complete_timing_change(struct tegra_emc *emc,
82473a7f0a9SMikko Perttunen 					     unsigned long rate)
82573a7f0a9SMikko Perttunen {
82673a7f0a9SMikko Perttunen 	struct emc_timing *timing = tegra_emc_find_timing(emc, rate);
82773a7f0a9SMikko Perttunen 	struct emc_timing *last = &emc->last_timing;
82873a7f0a9SMikko Perttunen 	u32 val;
82973a7f0a9SMikko Perttunen 
83073a7f0a9SMikko Perttunen 	if (!timing)
83173a7f0a9SMikko Perttunen 		return;
83273a7f0a9SMikko Perttunen 
83373a7f0a9SMikko Perttunen 	/* Wait until the state machine has settled */
83473a7f0a9SMikko Perttunen 	emc_seq_wait_clkchange(emc);
83573a7f0a9SMikko Perttunen 
83673a7f0a9SMikko Perttunen 	/* Restore AUTO_CAL */
83773a7f0a9SMikko Perttunen 	if (timing->emc_ctt_term_ctrl != last->emc_ctt_term_ctrl)
83873a7f0a9SMikko Perttunen 		writel(timing->emc_auto_cal_interval,
83973a7f0a9SMikko Perttunen 		       emc->regs + EMC_AUTO_CAL_INTERVAL);
84073a7f0a9SMikko Perttunen 
84173a7f0a9SMikko Perttunen 	/* Restore dynamic self-refresh */
84273a7f0a9SMikko Perttunen 	if (timing->emc_cfg & EMC_CFG_PWR_MASK)
84373a7f0a9SMikko Perttunen 		writel(timing->emc_cfg, emc->regs + EMC_CFG);
84473a7f0a9SMikko Perttunen 
84573a7f0a9SMikko Perttunen 	/* Set ZCAL wait count */
84673a7f0a9SMikko Perttunen 	writel(timing->emc_zcal_cnt_long, emc->regs + EMC_ZCAL_WAIT_CNT);
84773a7f0a9SMikko Perttunen 
84873a7f0a9SMikko Perttunen 	/* LPDDR3: Turn off BGBIAS if low frequency */
84973a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_LPDDR3 &&
85073a7f0a9SMikko Perttunen 	    timing->emc_bgbias_ctl0 &
85173a7f0a9SMikko Perttunen 	      EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) {
85273a7f0a9SMikko Perttunen 		val = timing->emc_bgbias_ctl0;
85373a7f0a9SMikko Perttunen 		val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN;
85473a7f0a9SMikko Perttunen 		val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD;
85573a7f0a9SMikko Perttunen 		writel(val, emc->regs + EMC_BGBIAS_CTL0);
85673a7f0a9SMikko Perttunen 	} else {
85773a7f0a9SMikko Perttunen 		if (emc->dram_type == DRAM_TYPE_DDR3 &&
85873a7f0a9SMikko Perttunen 		    readl(emc->regs + EMC_BGBIAS_CTL0) !=
85973a7f0a9SMikko Perttunen 		      timing->emc_bgbias_ctl0) {
86073a7f0a9SMikko Perttunen 			writel(timing->emc_bgbias_ctl0,
86173a7f0a9SMikko Perttunen 			       emc->regs + EMC_BGBIAS_CTL0);
86273a7f0a9SMikko Perttunen 		}
86373a7f0a9SMikko Perttunen 
86473a7f0a9SMikko Perttunen 		writel(timing->emc_auto_cal_interval,
86573a7f0a9SMikko Perttunen 		       emc->regs + EMC_AUTO_CAL_INTERVAL);
86673a7f0a9SMikko Perttunen 	}
86773a7f0a9SMikko Perttunen 
86873a7f0a9SMikko Perttunen 	/* Wait for timing to settle */
86973a7f0a9SMikko Perttunen 	udelay(2);
87073a7f0a9SMikko Perttunen 
87173a7f0a9SMikko Perttunen 	/* Reprogram SEL_DPD_CTRL */
87273a7f0a9SMikko Perttunen 	writel(timing->emc_sel_dpd_ctrl, emc->regs + EMC_SEL_DPD_CTRL);
87373a7f0a9SMikko Perttunen 	emc_seq_update_timing(emc);
87473a7f0a9SMikko Perttunen 
87573a7f0a9SMikko Perttunen 	emc->last_timing = *timing;
87673a7f0a9SMikko Perttunen }
87773a7f0a9SMikko Perttunen 
87873a7f0a9SMikko Perttunen /* Initialization and deinitialization */
87973a7f0a9SMikko Perttunen 
88073a7f0a9SMikko Perttunen static void emc_read_current_timing(struct tegra_emc *emc,
88173a7f0a9SMikko Perttunen 				    struct emc_timing *timing)
88273a7f0a9SMikko Perttunen {
88373a7f0a9SMikko Perttunen 	unsigned int i;
88473a7f0a9SMikko Perttunen 
88573a7f0a9SMikko Perttunen 	for (i = 0; i < ARRAY_SIZE(emc_burst_regs); ++i)
88673a7f0a9SMikko Perttunen 		timing->emc_burst_data[i] =
88773a7f0a9SMikko Perttunen 			readl(emc->regs + emc_burst_regs[i]);
88873a7f0a9SMikko Perttunen 
88973a7f0a9SMikko Perttunen 	timing->emc_cfg = readl(emc->regs + EMC_CFG);
89073a7f0a9SMikko Perttunen 
89173a7f0a9SMikko Perttunen 	timing->emc_auto_cal_interval = 0;
89273a7f0a9SMikko Perttunen 	timing->emc_zcal_cnt_long = 0;
89373a7f0a9SMikko Perttunen 	timing->emc_mode_1 = 0;
89473a7f0a9SMikko Perttunen 	timing->emc_mode_2 = 0;
89573a7f0a9SMikko Perttunen 	timing->emc_mode_4 = 0;
89673a7f0a9SMikko Perttunen 	timing->emc_mode_reset = 0;
89773a7f0a9SMikko Perttunen }
89873a7f0a9SMikko Perttunen 
89973a7f0a9SMikko Perttunen static int emc_init(struct tegra_emc *emc)
90073a7f0a9SMikko Perttunen {
90173a7f0a9SMikko Perttunen 	emc->dram_type = readl(emc->regs + EMC_FBIO_CFG5);
902*380def2dSDmitry Osipenko 
903*380def2dSDmitry Osipenko 	if (emc->dram_type & EMC_FBIO_CFG5_DRAM_WIDTH_X64)
904*380def2dSDmitry Osipenko 		emc->dram_bus_width = 64;
905*380def2dSDmitry Osipenko 	else
906*380def2dSDmitry Osipenko 		emc->dram_bus_width = 32;
907*380def2dSDmitry Osipenko 
908*380def2dSDmitry Osipenko 	dev_info(emc->dev, "%ubit DRAM bus\n", emc->dram_bus_width);
909*380def2dSDmitry Osipenko 
91073a7f0a9SMikko Perttunen 	emc->dram_type &= EMC_FBIO_CFG5_DRAM_TYPE_MASK;
91173a7f0a9SMikko Perttunen 	emc->dram_type >>= EMC_FBIO_CFG5_DRAM_TYPE_SHIFT;
91273a7f0a9SMikko Perttunen 
91373a7f0a9SMikko Perttunen 	emc->dram_num = tegra_mc_get_emem_device_count(emc->mc);
91473a7f0a9SMikko Perttunen 
91573a7f0a9SMikko Perttunen 	emc_read_current_timing(emc, &emc->last_timing);
91673a7f0a9SMikko Perttunen 
91773a7f0a9SMikko Perttunen 	return 0;
91873a7f0a9SMikko Perttunen }
91973a7f0a9SMikko Perttunen 
92073a7f0a9SMikko Perttunen static int load_one_timing_from_dt(struct tegra_emc *emc,
92173a7f0a9SMikko Perttunen 				   struct emc_timing *timing,
92273a7f0a9SMikko Perttunen 				   struct device_node *node)
92373a7f0a9SMikko Perttunen {
92473a7f0a9SMikko Perttunen 	u32 value;
92573a7f0a9SMikko Perttunen 	int err;
92673a7f0a9SMikko Perttunen 
92773a7f0a9SMikko Perttunen 	err = of_property_read_u32(node, "clock-frequency", &value);
92873a7f0a9SMikko Perttunen 	if (err) {
929c86f9854SRob Herring 		dev_err(emc->dev, "timing %pOFn: failed to read rate: %d\n",
930c86f9854SRob Herring 			node, err);
93173a7f0a9SMikko Perttunen 		return err;
93273a7f0a9SMikko Perttunen 	}
93373a7f0a9SMikko Perttunen 
93473a7f0a9SMikko Perttunen 	timing->rate = value;
93573a7f0a9SMikko Perttunen 
93673a7f0a9SMikko Perttunen 	err = of_property_read_u32_array(node, "nvidia,emc-configuration",
93773a7f0a9SMikko Perttunen 					 timing->emc_burst_data,
93873a7f0a9SMikko Perttunen 					 ARRAY_SIZE(timing->emc_burst_data));
93973a7f0a9SMikko Perttunen 	if (err) {
94073a7f0a9SMikko Perttunen 		dev_err(emc->dev,
941c86f9854SRob Herring 			"timing %pOFn: failed to read emc burst data: %d\n",
942c86f9854SRob Herring 			node, err);
94373a7f0a9SMikko Perttunen 		return err;
94473a7f0a9SMikko Perttunen 	}
94573a7f0a9SMikko Perttunen 
94673a7f0a9SMikko Perttunen #define EMC_READ_PROP(prop, dtprop) { \
94773a7f0a9SMikko Perttunen 	err = of_property_read_u32(node, dtprop, &timing->prop); \
94873a7f0a9SMikko Perttunen 	if (err) { \
949c86f9854SRob Herring 		dev_err(emc->dev, "timing %pOFn: failed to read " #prop ": %d\n", \
950c86f9854SRob Herring 			node, err); \
95173a7f0a9SMikko Perttunen 		return err; \
95273a7f0a9SMikko Perttunen 	} \
95373a7f0a9SMikko Perttunen }
95473a7f0a9SMikko Perttunen 
95573a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_auto_cal_config, "nvidia,emc-auto-cal-config")
95673a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_auto_cal_config2, "nvidia,emc-auto-cal-config2")
95773a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_auto_cal_config3, "nvidia,emc-auto-cal-config3")
95873a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_auto_cal_interval, "nvidia,emc-auto-cal-interval")
95973a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_bgbias_ctl0, "nvidia,emc-bgbias-ctl0")
96073a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_cfg, "nvidia,emc-cfg")
96173a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_cfg_2, "nvidia,emc-cfg-2")
96273a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_ctt_term_ctrl, "nvidia,emc-ctt-term-ctrl")
96373a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_mode_1, "nvidia,emc-mode-1")
96473a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_mode_2, "nvidia,emc-mode-2")
96573a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_mode_4, "nvidia,emc-mode-4")
96673a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_mode_reset, "nvidia,emc-mode-reset")
96773a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_mrs_wait_cnt, "nvidia,emc-mrs-wait-cnt")
96873a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_sel_dpd_ctrl, "nvidia,emc-sel-dpd-ctrl")
96973a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_xm2dqspadctrl2, "nvidia,emc-xm2dqspadctrl2")
97073a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_zcal_cnt_long, "nvidia,emc-zcal-cnt-long")
97173a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_zcal_interval, "nvidia,emc-zcal-interval")
97273a7f0a9SMikko Perttunen 
97373a7f0a9SMikko Perttunen #undef EMC_READ_PROP
97473a7f0a9SMikko Perttunen 
97573a7f0a9SMikko Perttunen 	return 0;
97673a7f0a9SMikko Perttunen }
97773a7f0a9SMikko Perttunen 
97873a7f0a9SMikko Perttunen static int cmp_timings(const void *_a, const void *_b)
97973a7f0a9SMikko Perttunen {
98073a7f0a9SMikko Perttunen 	const struct emc_timing *a = _a;
98173a7f0a9SMikko Perttunen 	const struct emc_timing *b = _b;
98273a7f0a9SMikko Perttunen 
98373a7f0a9SMikko Perttunen 	if (a->rate < b->rate)
98473a7f0a9SMikko Perttunen 		return -1;
98573a7f0a9SMikko Perttunen 	else if (a->rate == b->rate)
98673a7f0a9SMikko Perttunen 		return 0;
98773a7f0a9SMikko Perttunen 	else
98873a7f0a9SMikko Perttunen 		return 1;
98973a7f0a9SMikko Perttunen }
99073a7f0a9SMikko Perttunen 
99173a7f0a9SMikko Perttunen static int tegra_emc_load_timings_from_dt(struct tegra_emc *emc,
99273a7f0a9SMikko Perttunen 					  struct device_node *node)
99373a7f0a9SMikko Perttunen {
99473a7f0a9SMikko Perttunen 	int child_count = of_get_child_count(node);
99573a7f0a9SMikko Perttunen 	struct device_node *child;
99673a7f0a9SMikko Perttunen 	struct emc_timing *timing;
99773a7f0a9SMikko Perttunen 	unsigned int i = 0;
99873a7f0a9SMikko Perttunen 	int err;
99973a7f0a9SMikko Perttunen 
100073a7f0a9SMikko Perttunen 	emc->timings = devm_kcalloc(emc->dev, child_count, sizeof(*timing),
100173a7f0a9SMikko Perttunen 				    GFP_KERNEL);
100273a7f0a9SMikko Perttunen 	if (!emc->timings)
100373a7f0a9SMikko Perttunen 		return -ENOMEM;
100473a7f0a9SMikko Perttunen 
100573a7f0a9SMikko Perttunen 	emc->num_timings = child_count;
100673a7f0a9SMikko Perttunen 
100773a7f0a9SMikko Perttunen 	for_each_child_of_node(node, child) {
100873a7f0a9SMikko Perttunen 		timing = &emc->timings[i++];
100973a7f0a9SMikko Perttunen 
101073a7f0a9SMikko Perttunen 		err = load_one_timing_from_dt(emc, timing, child);
1011aafb197fSAmitoj Kaur Chawla 		if (err) {
1012aafb197fSAmitoj Kaur Chawla 			of_node_put(child);
101373a7f0a9SMikko Perttunen 			return err;
101473a7f0a9SMikko Perttunen 		}
1015aafb197fSAmitoj Kaur Chawla 	}
101673a7f0a9SMikko Perttunen 
101773a7f0a9SMikko Perttunen 	sort(emc->timings, emc->num_timings, sizeof(*timing), cmp_timings,
101873a7f0a9SMikko Perttunen 	     NULL);
101973a7f0a9SMikko Perttunen 
102073a7f0a9SMikko Perttunen 	return 0;
102173a7f0a9SMikko Perttunen }
102273a7f0a9SMikko Perttunen 
102373a7f0a9SMikko Perttunen static const struct of_device_id tegra_emc_of_match[] = {
102473a7f0a9SMikko Perttunen 	{ .compatible = "nvidia,tegra124-emc" },
102546c01923SThierry Reding 	{ .compatible = "nvidia,tegra132-emc" },
102673a7f0a9SMikko Perttunen 	{}
102773a7f0a9SMikko Perttunen };
1028281462e5SDmitry Osipenko MODULE_DEVICE_TABLE(of, tegra_emc_of_match);
102973a7f0a9SMikko Perttunen 
103073a7f0a9SMikko Perttunen static struct device_node *
103173a7f0a9SMikko Perttunen tegra_emc_find_node_by_ram_code(struct device_node *node, u32 ram_code)
103273a7f0a9SMikko Perttunen {
103373a7f0a9SMikko Perttunen 	struct device_node *np;
103473a7f0a9SMikko Perttunen 	int err;
103573a7f0a9SMikko Perttunen 
103673a7f0a9SMikko Perttunen 	for_each_child_of_node(node, np) {
103773a7f0a9SMikko Perttunen 		u32 value;
103873a7f0a9SMikko Perttunen 
103973a7f0a9SMikko Perttunen 		err = of_property_read_u32(np, "nvidia,ram-code", &value);
1040d1122e4bSJulia Lawall 		if (err || (value != ram_code))
104173a7f0a9SMikko Perttunen 			continue;
104273a7f0a9SMikko Perttunen 
104373a7f0a9SMikko Perttunen 		return np;
104473a7f0a9SMikko Perttunen 	}
104573a7f0a9SMikko Perttunen 
104673a7f0a9SMikko Perttunen 	return NULL;
104773a7f0a9SMikko Perttunen }
104873a7f0a9SMikko Perttunen 
1049*380def2dSDmitry Osipenko static void tegra_emc_rate_requests_init(struct tegra_emc *emc)
1050*380def2dSDmitry Osipenko {
1051*380def2dSDmitry Osipenko 	unsigned int i;
1052*380def2dSDmitry Osipenko 
1053*380def2dSDmitry Osipenko 	for (i = 0; i < EMC_RATE_TYPE_MAX; i++) {
1054*380def2dSDmitry Osipenko 		emc->requested_rate[i].min_rate = 0;
1055*380def2dSDmitry Osipenko 		emc->requested_rate[i].max_rate = ULONG_MAX;
1056*380def2dSDmitry Osipenko 	}
1057*380def2dSDmitry Osipenko }
1058*380def2dSDmitry Osipenko 
1059*380def2dSDmitry Osipenko static int emc_request_rate(struct tegra_emc *emc,
1060*380def2dSDmitry Osipenko 			    unsigned long new_min_rate,
1061*380def2dSDmitry Osipenko 			    unsigned long new_max_rate,
1062*380def2dSDmitry Osipenko 			    enum emc_rate_request_type type)
1063*380def2dSDmitry Osipenko {
1064*380def2dSDmitry Osipenko 	struct emc_rate_request *req = emc->requested_rate;
1065*380def2dSDmitry Osipenko 	unsigned long min_rate = 0, max_rate = ULONG_MAX;
1066*380def2dSDmitry Osipenko 	unsigned int i;
1067*380def2dSDmitry Osipenko 	int err;
1068*380def2dSDmitry Osipenko 
1069*380def2dSDmitry Osipenko 	/* select minimum and maximum rates among the requested rates */
1070*380def2dSDmitry Osipenko 	for (i = 0; i < EMC_RATE_TYPE_MAX; i++, req++) {
1071*380def2dSDmitry Osipenko 		if (i == type) {
1072*380def2dSDmitry Osipenko 			min_rate = max(new_min_rate, min_rate);
1073*380def2dSDmitry Osipenko 			max_rate = min(new_max_rate, max_rate);
1074*380def2dSDmitry Osipenko 		} else {
1075*380def2dSDmitry Osipenko 			min_rate = max(req->min_rate, min_rate);
1076*380def2dSDmitry Osipenko 			max_rate = min(req->max_rate, max_rate);
1077*380def2dSDmitry Osipenko 		}
1078*380def2dSDmitry Osipenko 	}
1079*380def2dSDmitry Osipenko 
1080*380def2dSDmitry Osipenko 	if (min_rate > max_rate) {
1081*380def2dSDmitry Osipenko 		dev_err_ratelimited(emc->dev, "%s: type %u: out of range: %lu %lu\n",
1082*380def2dSDmitry Osipenko 				    __func__, type, min_rate, max_rate);
1083*380def2dSDmitry Osipenko 		return -ERANGE;
1084*380def2dSDmitry Osipenko 	}
1085*380def2dSDmitry Osipenko 
1086*380def2dSDmitry Osipenko 	/*
1087*380def2dSDmitry Osipenko 	 * EMC rate-changes should go via OPP API because it manages voltage
1088*380def2dSDmitry Osipenko 	 * changes.
1089*380def2dSDmitry Osipenko 	 */
1090*380def2dSDmitry Osipenko 	err = dev_pm_opp_set_rate(emc->dev, min_rate);
1091*380def2dSDmitry Osipenko 	if (err)
1092*380def2dSDmitry Osipenko 		return err;
1093*380def2dSDmitry Osipenko 
1094*380def2dSDmitry Osipenko 	emc->requested_rate[type].min_rate = new_min_rate;
1095*380def2dSDmitry Osipenko 	emc->requested_rate[type].max_rate = new_max_rate;
1096*380def2dSDmitry Osipenko 
1097*380def2dSDmitry Osipenko 	return 0;
1098*380def2dSDmitry Osipenko }
1099*380def2dSDmitry Osipenko 
1100*380def2dSDmitry Osipenko static int emc_set_min_rate(struct tegra_emc *emc, unsigned long rate,
1101*380def2dSDmitry Osipenko 			    enum emc_rate_request_type type)
1102*380def2dSDmitry Osipenko {
1103*380def2dSDmitry Osipenko 	struct emc_rate_request *req = &emc->requested_rate[type];
1104*380def2dSDmitry Osipenko 	int ret;
1105*380def2dSDmitry Osipenko 
1106*380def2dSDmitry Osipenko 	mutex_lock(&emc->rate_lock);
1107*380def2dSDmitry Osipenko 	ret = emc_request_rate(emc, rate, req->max_rate, type);
1108*380def2dSDmitry Osipenko 	mutex_unlock(&emc->rate_lock);
1109*380def2dSDmitry Osipenko 
1110*380def2dSDmitry Osipenko 	return ret;
1111*380def2dSDmitry Osipenko }
1112*380def2dSDmitry Osipenko 
1113*380def2dSDmitry Osipenko static int emc_set_max_rate(struct tegra_emc *emc, unsigned long rate,
1114*380def2dSDmitry Osipenko 			    enum emc_rate_request_type type)
1115*380def2dSDmitry Osipenko {
1116*380def2dSDmitry Osipenko 	struct emc_rate_request *req = &emc->requested_rate[type];
1117*380def2dSDmitry Osipenko 	int ret;
1118*380def2dSDmitry Osipenko 
1119*380def2dSDmitry Osipenko 	mutex_lock(&emc->rate_lock);
1120*380def2dSDmitry Osipenko 	ret = emc_request_rate(emc, req->min_rate, rate, type);
1121*380def2dSDmitry Osipenko 	mutex_unlock(&emc->rate_lock);
1122*380def2dSDmitry Osipenko 
1123*380def2dSDmitry Osipenko 	return ret;
1124*380def2dSDmitry Osipenko }
1125*380def2dSDmitry Osipenko 
11266b9acd93SThierry Reding /*
11276b9acd93SThierry Reding  * debugfs interface
11286b9acd93SThierry Reding  *
11296b9acd93SThierry Reding  * The memory controller driver exposes some files in debugfs that can be used
11306b9acd93SThierry Reding  * to control the EMC frequency. The top-level directory can be found here:
11316b9acd93SThierry Reding  *
11326b9acd93SThierry Reding  *   /sys/kernel/debug/emc
11336b9acd93SThierry Reding  *
11346b9acd93SThierry Reding  * It contains the following files:
11356b9acd93SThierry Reding  *
11366b9acd93SThierry Reding  *   - available_rates: This file contains a list of valid, space-separated
11376b9acd93SThierry Reding  *     EMC frequencies.
11386b9acd93SThierry Reding  *
11396b9acd93SThierry Reding  *   - min_rate: Writing a value to this file sets the given frequency as the
11406b9acd93SThierry Reding  *       floor of the permitted range. If this is higher than the currently
11416b9acd93SThierry Reding  *       configured EMC frequency, this will cause the frequency to be
11426b9acd93SThierry Reding  *       increased so that it stays within the valid range.
11436b9acd93SThierry Reding  *
11446b9acd93SThierry Reding  *   - max_rate: Similarily to the min_rate file, writing a value to this file
11456b9acd93SThierry Reding  *       sets the given frequency as the ceiling of the permitted range. If
11466b9acd93SThierry Reding  *       the value is lower than the currently configured EMC frequency, this
11476b9acd93SThierry Reding  *       will cause the frequency to be decreased so that it stays within the
11486b9acd93SThierry Reding  *       valid range.
11496b9acd93SThierry Reding  */
11509c77a81fSMikko Perttunen 
11516b9acd93SThierry Reding static bool tegra_emc_validate_rate(struct tegra_emc *emc, unsigned long rate)
11529c77a81fSMikko Perttunen {
11536b9acd93SThierry Reding 	unsigned int i;
11549c77a81fSMikko Perttunen 
11556b9acd93SThierry Reding 	for (i = 0; i < emc->num_timings; i++)
11566b9acd93SThierry Reding 		if (rate == emc->timings[i].rate)
11576b9acd93SThierry Reding 			return true;
11589c77a81fSMikko Perttunen 
11596b9acd93SThierry Reding 	return false;
11609c77a81fSMikko Perttunen }
11619c77a81fSMikko Perttunen 
11626b9acd93SThierry Reding static int tegra_emc_debug_available_rates_show(struct seq_file *s,
11636b9acd93SThierry Reding 						void *data)
116430a636f9SThierry Reding {
116530a636f9SThierry Reding 	struct tegra_emc *emc = s->private;
116630a636f9SThierry Reding 	const char *prefix = "";
116730a636f9SThierry Reding 	unsigned int i;
116830a636f9SThierry Reding 
116930a636f9SThierry Reding 	for (i = 0; i < emc->num_timings; i++) {
11706b9acd93SThierry Reding 		seq_printf(s, "%s%lu", prefix, emc->timings[i].rate);
117130a636f9SThierry Reding 		prefix = " ";
117230a636f9SThierry Reding 	}
117330a636f9SThierry Reding 
117430a636f9SThierry Reding 	seq_puts(s, "\n");
117530a636f9SThierry Reding 
117630a636f9SThierry Reding 	return 0;
117730a636f9SThierry Reding }
117830a636f9SThierry Reding 
117967a344e8SQinglang Miao DEFINE_SHOW_ATTRIBUTE(tegra_emc_debug_available_rates);
118030a636f9SThierry Reding 
11816b9acd93SThierry Reding static int tegra_emc_debug_min_rate_get(void *data, u64 *rate)
11826b9acd93SThierry Reding {
11836b9acd93SThierry Reding 	struct tegra_emc *emc = data;
11846b9acd93SThierry Reding 
11856b9acd93SThierry Reding 	*rate = emc->debugfs.min_rate;
11866b9acd93SThierry Reding 
11876b9acd93SThierry Reding 	return 0;
11886b9acd93SThierry Reding }
11896b9acd93SThierry Reding 
11906b9acd93SThierry Reding static int tegra_emc_debug_min_rate_set(void *data, u64 rate)
11916b9acd93SThierry Reding {
11926b9acd93SThierry Reding 	struct tegra_emc *emc = data;
11936b9acd93SThierry Reding 	int err;
11946b9acd93SThierry Reding 
11956b9acd93SThierry Reding 	if (!tegra_emc_validate_rate(emc, rate))
11966b9acd93SThierry Reding 		return -EINVAL;
11976b9acd93SThierry Reding 
1198*380def2dSDmitry Osipenko 	err = emc_set_min_rate(emc, rate, EMC_RATE_DEBUG);
11996b9acd93SThierry Reding 	if (err < 0)
12006b9acd93SThierry Reding 		return err;
12016b9acd93SThierry Reding 
12026b9acd93SThierry Reding 	emc->debugfs.min_rate = rate;
12036b9acd93SThierry Reding 
12046b9acd93SThierry Reding 	return 0;
12056b9acd93SThierry Reding }
12066b9acd93SThierry Reding 
12076b9acd93SThierry Reding DEFINE_SIMPLE_ATTRIBUTE(tegra_emc_debug_min_rate_fops,
12086b9acd93SThierry Reding 			tegra_emc_debug_min_rate_get,
12096b9acd93SThierry Reding 			tegra_emc_debug_min_rate_set, "%llu\n");
12106b9acd93SThierry Reding 
12116b9acd93SThierry Reding static int tegra_emc_debug_max_rate_get(void *data, u64 *rate)
12126b9acd93SThierry Reding {
12136b9acd93SThierry Reding 	struct tegra_emc *emc = data;
12146b9acd93SThierry Reding 
12156b9acd93SThierry Reding 	*rate = emc->debugfs.max_rate;
12166b9acd93SThierry Reding 
12176b9acd93SThierry Reding 	return 0;
12186b9acd93SThierry Reding }
12196b9acd93SThierry Reding 
12206b9acd93SThierry Reding static int tegra_emc_debug_max_rate_set(void *data, u64 rate)
12216b9acd93SThierry Reding {
12226b9acd93SThierry Reding 	struct tegra_emc *emc = data;
12236b9acd93SThierry Reding 	int err;
12246b9acd93SThierry Reding 
12256b9acd93SThierry Reding 	if (!tegra_emc_validate_rate(emc, rate))
12266b9acd93SThierry Reding 		return -EINVAL;
12276b9acd93SThierry Reding 
1228*380def2dSDmitry Osipenko 	err = emc_set_max_rate(emc, rate, EMC_RATE_DEBUG);
12296b9acd93SThierry Reding 	if (err < 0)
12306b9acd93SThierry Reding 		return err;
12316b9acd93SThierry Reding 
12326b9acd93SThierry Reding 	emc->debugfs.max_rate = rate;
12336b9acd93SThierry Reding 
12346b9acd93SThierry Reding 	return 0;
12356b9acd93SThierry Reding }
12366b9acd93SThierry Reding 
12376b9acd93SThierry Reding DEFINE_SIMPLE_ATTRIBUTE(tegra_emc_debug_max_rate_fops,
12386b9acd93SThierry Reding 			tegra_emc_debug_max_rate_get,
12396b9acd93SThierry Reding 			tegra_emc_debug_max_rate_set, "%llu\n");
12406b9acd93SThierry Reding 
124130a636f9SThierry Reding static void emc_debugfs_init(struct device *dev, struct tegra_emc *emc)
12429c77a81fSMikko Perttunen {
12436b9acd93SThierry Reding 	unsigned int i;
12446b9acd93SThierry Reding 	int err;
12459c77a81fSMikko Perttunen 
12466b9acd93SThierry Reding 	emc->debugfs.min_rate = ULONG_MAX;
12476b9acd93SThierry Reding 	emc->debugfs.max_rate = 0;
12486b9acd93SThierry Reding 
12496b9acd93SThierry Reding 	for (i = 0; i < emc->num_timings; i++) {
12506b9acd93SThierry Reding 		if (emc->timings[i].rate < emc->debugfs.min_rate)
12516b9acd93SThierry Reding 			emc->debugfs.min_rate = emc->timings[i].rate;
12526b9acd93SThierry Reding 
12536b9acd93SThierry Reding 		if (emc->timings[i].rate > emc->debugfs.max_rate)
12546b9acd93SThierry Reding 			emc->debugfs.max_rate = emc->timings[i].rate;
12556b9acd93SThierry Reding 	}
12566b9acd93SThierry Reding 
1257141267bfSDmitry Osipenko 	if (!emc->num_timings) {
1258141267bfSDmitry Osipenko 		emc->debugfs.min_rate = clk_get_rate(emc->clk);
1259141267bfSDmitry Osipenko 		emc->debugfs.max_rate = emc->debugfs.min_rate;
1260141267bfSDmitry Osipenko 	}
1261141267bfSDmitry Osipenko 
12626b9acd93SThierry Reding 	err = clk_set_rate_range(emc->clk, emc->debugfs.min_rate,
12636b9acd93SThierry Reding 				 emc->debugfs.max_rate);
12646b9acd93SThierry Reding 	if (err < 0) {
12656b9acd93SThierry Reding 		dev_err(dev, "failed to set rate range [%lu-%lu] for %pC\n",
12666b9acd93SThierry Reding 			emc->debugfs.min_rate, emc->debugfs.max_rate,
12676b9acd93SThierry Reding 			emc->clk);
12686b9acd93SThierry Reding 		return;
12696b9acd93SThierry Reding 	}
12706b9acd93SThierry Reding 
12716b9acd93SThierry Reding 	emc->debugfs.root = debugfs_create_dir("emc", NULL);
12726b9acd93SThierry Reding 	if (!emc->debugfs.root) {
12739c77a81fSMikko Perttunen 		dev_err(dev, "failed to create debugfs directory\n");
12749c77a81fSMikko Perttunen 		return;
12759c77a81fSMikko Perttunen 	}
12769c77a81fSMikko Perttunen 
12776cc8823aSDmitry Osipenko 	debugfs_create_file("available_rates", 0444, emc->debugfs.root, emc,
12786b9acd93SThierry Reding 			    &tegra_emc_debug_available_rates_fops);
12796cc8823aSDmitry Osipenko 	debugfs_create_file("min_rate", 0644, emc->debugfs.root,
12806b9acd93SThierry Reding 			    emc, &tegra_emc_debug_min_rate_fops);
12816cc8823aSDmitry Osipenko 	debugfs_create_file("max_rate", 0644, emc->debugfs.root,
12826b9acd93SThierry Reding 			    emc, &tegra_emc_debug_max_rate_fops);
12839c77a81fSMikko Perttunen }
12849c77a81fSMikko Perttunen 
1285*380def2dSDmitry Osipenko static inline struct tegra_emc *
1286*380def2dSDmitry Osipenko to_tegra_emc_provider(struct icc_provider *provider)
1287*380def2dSDmitry Osipenko {
1288*380def2dSDmitry Osipenko 	return container_of(provider, struct tegra_emc, provider);
1289*380def2dSDmitry Osipenko }
1290*380def2dSDmitry Osipenko 
1291*380def2dSDmitry Osipenko static struct icc_node_data *
1292*380def2dSDmitry Osipenko emc_of_icc_xlate_extended(struct of_phandle_args *spec, void *data)
1293*380def2dSDmitry Osipenko {
1294*380def2dSDmitry Osipenko 	struct icc_provider *provider = data;
1295*380def2dSDmitry Osipenko 	struct icc_node_data *ndata;
1296*380def2dSDmitry Osipenko 	struct icc_node *node;
1297*380def2dSDmitry Osipenko 
1298*380def2dSDmitry Osipenko 	/* External Memory is the only possible ICC route */
1299*380def2dSDmitry Osipenko 	list_for_each_entry(node, &provider->nodes, node_list) {
1300*380def2dSDmitry Osipenko 		if (node->id != TEGRA_ICC_EMEM)
1301*380def2dSDmitry Osipenko 			continue;
1302*380def2dSDmitry Osipenko 
1303*380def2dSDmitry Osipenko 		ndata = kzalloc(sizeof(*ndata), GFP_KERNEL);
1304*380def2dSDmitry Osipenko 		if (!ndata)
1305*380def2dSDmitry Osipenko 			return ERR_PTR(-ENOMEM);
1306*380def2dSDmitry Osipenko 
1307*380def2dSDmitry Osipenko 		/*
1308*380def2dSDmitry Osipenko 		 * SRC and DST nodes should have matching TAG in order to have
1309*380def2dSDmitry Osipenko 		 * it set by default for a requested path.
1310*380def2dSDmitry Osipenko 		 */
1311*380def2dSDmitry Osipenko 		ndata->tag = TEGRA_MC_ICC_TAG_ISO;
1312*380def2dSDmitry Osipenko 		ndata->node = node;
1313*380def2dSDmitry Osipenko 
1314*380def2dSDmitry Osipenko 		return ndata;
1315*380def2dSDmitry Osipenko 	}
1316*380def2dSDmitry Osipenko 
1317*380def2dSDmitry Osipenko 	return ERR_PTR(-EPROBE_DEFER);
1318*380def2dSDmitry Osipenko }
1319*380def2dSDmitry Osipenko 
1320*380def2dSDmitry Osipenko static int emc_icc_set(struct icc_node *src, struct icc_node *dst)
1321*380def2dSDmitry Osipenko {
1322*380def2dSDmitry Osipenko 	struct tegra_emc *emc = to_tegra_emc_provider(dst->provider);
1323*380def2dSDmitry Osipenko 	unsigned long long peak_bw = icc_units_to_bps(dst->peak_bw);
1324*380def2dSDmitry Osipenko 	unsigned long long avg_bw = icc_units_to_bps(dst->avg_bw);
1325*380def2dSDmitry Osipenko 	unsigned long long rate = max(avg_bw, peak_bw);
1326*380def2dSDmitry Osipenko 	unsigned int dram_data_bus_width_bytes;
1327*380def2dSDmitry Osipenko 	const unsigned int ddr = 2;
1328*380def2dSDmitry Osipenko 	int err;
1329*380def2dSDmitry Osipenko 
1330*380def2dSDmitry Osipenko 	/*
1331*380def2dSDmitry Osipenko 	 * Tegra124 EMC runs on a clock rate of SDRAM bus. This means that
1332*380def2dSDmitry Osipenko 	 * EMC clock rate is twice smaller than the peak data rate because
1333*380def2dSDmitry Osipenko 	 * data is sampled on both EMC clock edges.
1334*380def2dSDmitry Osipenko 	 */
1335*380def2dSDmitry Osipenko 	dram_data_bus_width_bytes = emc->dram_bus_width / 8;
1336*380def2dSDmitry Osipenko 	do_div(rate, ddr * dram_data_bus_width_bytes);
1337*380def2dSDmitry Osipenko 	rate = min_t(u64, rate, U32_MAX);
1338*380def2dSDmitry Osipenko 
1339*380def2dSDmitry Osipenko 	err = emc_set_min_rate(emc, rate, EMC_RATE_ICC);
1340*380def2dSDmitry Osipenko 	if (err)
1341*380def2dSDmitry Osipenko 		return err;
1342*380def2dSDmitry Osipenko 
1343*380def2dSDmitry Osipenko 	return 0;
1344*380def2dSDmitry Osipenko }
1345*380def2dSDmitry Osipenko 
1346*380def2dSDmitry Osipenko static int tegra_emc_interconnect_init(struct tegra_emc *emc)
1347*380def2dSDmitry Osipenko {
1348*380def2dSDmitry Osipenko 	const struct tegra_mc_soc *soc = emc->mc->soc;
1349*380def2dSDmitry Osipenko 	struct icc_node *node;
1350*380def2dSDmitry Osipenko 	int err;
1351*380def2dSDmitry Osipenko 
1352*380def2dSDmitry Osipenko 	emc->provider.dev = emc->dev;
1353*380def2dSDmitry Osipenko 	emc->provider.set = emc_icc_set;
1354*380def2dSDmitry Osipenko 	emc->provider.data = &emc->provider;
1355*380def2dSDmitry Osipenko 	emc->provider.aggregate = soc->icc_ops->aggregate;
1356*380def2dSDmitry Osipenko 	emc->provider.xlate_extended = emc_of_icc_xlate_extended;
1357*380def2dSDmitry Osipenko 
1358*380def2dSDmitry Osipenko 	err = icc_provider_add(&emc->provider);
1359*380def2dSDmitry Osipenko 	if (err)
1360*380def2dSDmitry Osipenko 		goto err_msg;
1361*380def2dSDmitry Osipenko 
1362*380def2dSDmitry Osipenko 	/* create External Memory Controller node */
1363*380def2dSDmitry Osipenko 	node = icc_node_create(TEGRA_ICC_EMC);
1364*380def2dSDmitry Osipenko 	if (IS_ERR(node)) {
1365*380def2dSDmitry Osipenko 		err = PTR_ERR(node);
1366*380def2dSDmitry Osipenko 		goto del_provider;
1367*380def2dSDmitry Osipenko 	}
1368*380def2dSDmitry Osipenko 
1369*380def2dSDmitry Osipenko 	node->name = "External Memory Controller";
1370*380def2dSDmitry Osipenko 	icc_node_add(node, &emc->provider);
1371*380def2dSDmitry Osipenko 
1372*380def2dSDmitry Osipenko 	/* link External Memory Controller to External Memory (DRAM) */
1373*380def2dSDmitry Osipenko 	err = icc_link_create(node, TEGRA_ICC_EMEM);
1374*380def2dSDmitry Osipenko 	if (err)
1375*380def2dSDmitry Osipenko 		goto remove_nodes;
1376*380def2dSDmitry Osipenko 
1377*380def2dSDmitry Osipenko 	/* create External Memory node */
1378*380def2dSDmitry Osipenko 	node = icc_node_create(TEGRA_ICC_EMEM);
1379*380def2dSDmitry Osipenko 	if (IS_ERR(node)) {
1380*380def2dSDmitry Osipenko 		err = PTR_ERR(node);
1381*380def2dSDmitry Osipenko 		goto remove_nodes;
1382*380def2dSDmitry Osipenko 	}
1383*380def2dSDmitry Osipenko 
1384*380def2dSDmitry Osipenko 	node->name = "External Memory (DRAM)";
1385*380def2dSDmitry Osipenko 	icc_node_add(node, &emc->provider);
1386*380def2dSDmitry Osipenko 
1387*380def2dSDmitry Osipenko 	return 0;
1388*380def2dSDmitry Osipenko 
1389*380def2dSDmitry Osipenko remove_nodes:
1390*380def2dSDmitry Osipenko 	icc_nodes_remove(&emc->provider);
1391*380def2dSDmitry Osipenko del_provider:
1392*380def2dSDmitry Osipenko 	icc_provider_del(&emc->provider);
1393*380def2dSDmitry Osipenko err_msg:
1394*380def2dSDmitry Osipenko 	dev_err(emc->dev, "failed to initialize ICC: %d\n", err);
1395*380def2dSDmitry Osipenko 
1396*380def2dSDmitry Osipenko 	return err;
1397*380def2dSDmitry Osipenko }
1398*380def2dSDmitry Osipenko 
1399*380def2dSDmitry Osipenko static int tegra_emc_opp_table_init(struct tegra_emc *emc)
1400*380def2dSDmitry Osipenko {
1401*380def2dSDmitry Osipenko 	u32 hw_version = BIT(tegra_sku_info.soc_speedo_id);
1402*380def2dSDmitry Osipenko 	struct opp_table *clk_opp_table, *hw_opp_table;
1403*380def2dSDmitry Osipenko 	int err;
1404*380def2dSDmitry Osipenko 
1405*380def2dSDmitry Osipenko 	clk_opp_table = dev_pm_opp_set_clkname(emc->dev, NULL);
1406*380def2dSDmitry Osipenko 	err = PTR_ERR_OR_ZERO(clk_opp_table);
1407*380def2dSDmitry Osipenko 	if (err) {
1408*380def2dSDmitry Osipenko 		dev_err(emc->dev, "failed to set OPP clk: %d\n", err);
1409*380def2dSDmitry Osipenko 		return err;
1410*380def2dSDmitry Osipenko 	}
1411*380def2dSDmitry Osipenko 
1412*380def2dSDmitry Osipenko 	hw_opp_table = dev_pm_opp_set_supported_hw(emc->dev, &hw_version, 1);
1413*380def2dSDmitry Osipenko 	err = PTR_ERR_OR_ZERO(hw_opp_table);
1414*380def2dSDmitry Osipenko 	if (err) {
1415*380def2dSDmitry Osipenko 		dev_err(emc->dev, "failed to set OPP supported HW: %d\n", err);
1416*380def2dSDmitry Osipenko 		goto put_clk_table;
1417*380def2dSDmitry Osipenko 	}
1418*380def2dSDmitry Osipenko 
1419*380def2dSDmitry Osipenko 	err = dev_pm_opp_of_add_table(emc->dev);
1420*380def2dSDmitry Osipenko 	if (err) {
1421*380def2dSDmitry Osipenko 		if (err == -ENODEV)
1422*380def2dSDmitry Osipenko 			dev_err(emc->dev, "OPP table not found, please update your device tree\n");
1423*380def2dSDmitry Osipenko 		else
1424*380def2dSDmitry Osipenko 			dev_err(emc->dev, "failed to add OPP table: %d\n", err);
1425*380def2dSDmitry Osipenko 
1426*380def2dSDmitry Osipenko 		goto put_hw_table;
1427*380def2dSDmitry Osipenko 	}
1428*380def2dSDmitry Osipenko 
1429*380def2dSDmitry Osipenko 	dev_info(emc->dev, "OPP HW ver. 0x%x, current clock rate %lu MHz\n",
1430*380def2dSDmitry Osipenko 		 hw_version, clk_get_rate(emc->clk) / 1000000);
1431*380def2dSDmitry Osipenko 
1432*380def2dSDmitry Osipenko 	/* first dummy rate-set initializes voltage state */
1433*380def2dSDmitry Osipenko 	err = dev_pm_opp_set_rate(emc->dev, clk_get_rate(emc->clk));
1434*380def2dSDmitry Osipenko 	if (err) {
1435*380def2dSDmitry Osipenko 		dev_err(emc->dev, "failed to initialize OPP clock: %d\n", err);
1436*380def2dSDmitry Osipenko 		goto remove_table;
1437*380def2dSDmitry Osipenko 	}
1438*380def2dSDmitry Osipenko 
1439*380def2dSDmitry Osipenko 	return 0;
1440*380def2dSDmitry Osipenko 
1441*380def2dSDmitry Osipenko remove_table:
1442*380def2dSDmitry Osipenko 	dev_pm_opp_of_remove_table(emc->dev);
1443*380def2dSDmitry Osipenko put_hw_table:
1444*380def2dSDmitry Osipenko 	dev_pm_opp_put_supported_hw(hw_opp_table);
1445*380def2dSDmitry Osipenko put_clk_table:
1446*380def2dSDmitry Osipenko 	dev_pm_opp_put_clkname(clk_opp_table);
1447*380def2dSDmitry Osipenko 
1448*380def2dSDmitry Osipenko 	return err;
1449*380def2dSDmitry Osipenko }
1450*380def2dSDmitry Osipenko 
1451*380def2dSDmitry Osipenko static void devm_tegra_emc_unset_callback(void *data)
1452*380def2dSDmitry Osipenko {
1453*380def2dSDmitry Osipenko 	tegra124_clk_set_emc_callbacks(NULL, NULL);
1454*380def2dSDmitry Osipenko }
1455*380def2dSDmitry Osipenko 
145673a7f0a9SMikko Perttunen static int tegra_emc_probe(struct platform_device *pdev)
145773a7f0a9SMikko Perttunen {
145873a7f0a9SMikko Perttunen 	struct device_node *np;
145973a7f0a9SMikko Perttunen 	struct tegra_emc *emc;
146073a7f0a9SMikko Perttunen 	u32 ram_code;
146173a7f0a9SMikko Perttunen 	int err;
146273a7f0a9SMikko Perttunen 
146373a7f0a9SMikko Perttunen 	emc = devm_kzalloc(&pdev->dev, sizeof(*emc), GFP_KERNEL);
146473a7f0a9SMikko Perttunen 	if (!emc)
146573a7f0a9SMikko Perttunen 		return -ENOMEM;
146673a7f0a9SMikko Perttunen 
1467*380def2dSDmitry Osipenko 	mutex_init(&emc->rate_lock);
146873a7f0a9SMikko Perttunen 	emc->dev = &pdev->dev;
146973a7f0a9SMikko Perttunen 
14704e84d0a6SDmitry Osipenko 	emc->regs = devm_platform_ioremap_resource(pdev, 0);
147173a7f0a9SMikko Perttunen 	if (IS_ERR(emc->regs))
147273a7f0a9SMikko Perttunen 		return PTR_ERR(emc->regs);
147373a7f0a9SMikko Perttunen 
14746c6bd207SDmitry Osipenko 	emc->mc = devm_tegra_memory_controller_get(&pdev->dev);
14756c6bd207SDmitry Osipenko 	if (IS_ERR(emc->mc))
14766c6bd207SDmitry Osipenko 		return PTR_ERR(emc->mc);
147773a7f0a9SMikko Perttunen 
147873a7f0a9SMikko Perttunen 	ram_code = tegra_read_ram_code();
147973a7f0a9SMikko Perttunen 
148073a7f0a9SMikko Perttunen 	np = tegra_emc_find_node_by_ram_code(pdev->dev.of_node, ram_code);
14819c56679dSDmitry Osipenko 	if (np) {
148273a7f0a9SMikko Perttunen 		err = tegra_emc_load_timings_from_dt(emc, np);
148373a7f0a9SMikko Perttunen 		of_node_put(np);
148473a7f0a9SMikko Perttunen 		if (err)
148573a7f0a9SMikko Perttunen 			return err;
14869c56679dSDmitry Osipenko 	} else {
14879c56679dSDmitry Osipenko 		dev_info(&pdev->dev,
14889c56679dSDmitry Osipenko 			 "no memory timings for RAM code %u found in DT\n",
148973a7f0a9SMikko Perttunen 			 ram_code);
149073a7f0a9SMikko Perttunen 	}
149173a7f0a9SMikko Perttunen 
149273a7f0a9SMikko Perttunen 	err = emc_init(emc);
149373a7f0a9SMikko Perttunen 	if (err) {
149473a7f0a9SMikko Perttunen 		dev_err(&pdev->dev, "EMC initialization failed: %d\n", err);
149573a7f0a9SMikko Perttunen 		return err;
149673a7f0a9SMikko Perttunen 	}
149773a7f0a9SMikko Perttunen 
149873a7f0a9SMikko Perttunen 	platform_set_drvdata(pdev, emc);
149973a7f0a9SMikko Perttunen 
1500281462e5SDmitry Osipenko 	tegra124_clk_set_emc_callbacks(tegra_emc_prepare_timing_change,
1501281462e5SDmitry Osipenko 				       tegra_emc_complete_timing_change);
1502281462e5SDmitry Osipenko 
1503*380def2dSDmitry Osipenko 	err = devm_add_action_or_reset(&pdev->dev, devm_tegra_emc_unset_callback,
1504*380def2dSDmitry Osipenko 				       NULL);
1505*380def2dSDmitry Osipenko 	if (err)
1506*380def2dSDmitry Osipenko 		return err;
1507*380def2dSDmitry Osipenko 
1508*380def2dSDmitry Osipenko 	emc->clk = devm_clk_get(&pdev->dev, "emc");
1509*380def2dSDmitry Osipenko 	if (IS_ERR(emc->clk)) {
1510*380def2dSDmitry Osipenko 		err = PTR_ERR(emc->clk);
1511*380def2dSDmitry Osipenko 		dev_err(&pdev->dev, "failed to get EMC clock: %d\n", err);
1512*380def2dSDmitry Osipenko 		return err;
1513*380def2dSDmitry Osipenko 	}
1514*380def2dSDmitry Osipenko 
1515*380def2dSDmitry Osipenko 	err = tegra_emc_opp_table_init(emc);
1516*380def2dSDmitry Osipenko 	if (err)
1517*380def2dSDmitry Osipenko 		return err;
1518*380def2dSDmitry Osipenko 
1519*380def2dSDmitry Osipenko 	tegra_emc_rate_requests_init(emc);
1520*380def2dSDmitry Osipenko 
15219c77a81fSMikko Perttunen 	if (IS_ENABLED(CONFIG_DEBUG_FS))
152230a636f9SThierry Reding 		emc_debugfs_init(&pdev->dev, emc);
15239c77a81fSMikko Perttunen 
1524*380def2dSDmitry Osipenko 	tegra_emc_interconnect_init(emc);
1525*380def2dSDmitry Osipenko 
1526281462e5SDmitry Osipenko 	/*
1527281462e5SDmitry Osipenko 	 * Don't allow the kernel module to be unloaded. Unloading adds some
1528281462e5SDmitry Osipenko 	 * extra complexity which doesn't really worth the effort in a case of
1529281462e5SDmitry Osipenko 	 * this driver.
1530281462e5SDmitry Osipenko 	 */
1531281462e5SDmitry Osipenko 	try_module_get(THIS_MODULE);
1532281462e5SDmitry Osipenko 
153373a7f0a9SMikko Perttunen 	return 0;
153473a7f0a9SMikko Perttunen };
153573a7f0a9SMikko Perttunen 
153673a7f0a9SMikko Perttunen static struct platform_driver tegra_emc_driver = {
153773a7f0a9SMikko Perttunen 	.probe = tegra_emc_probe,
153873a7f0a9SMikko Perttunen 	.driver = {
153973a7f0a9SMikko Perttunen 		.name = "tegra-emc",
154073a7f0a9SMikko Perttunen 		.of_match_table = tegra_emc_of_match,
154173a7f0a9SMikko Perttunen 		.suppress_bind_attrs = true,
1542*380def2dSDmitry Osipenko 		.sync_state = icc_sync_state,
154373a7f0a9SMikko Perttunen 	},
154473a7f0a9SMikko Perttunen };
1545281462e5SDmitry Osipenko module_platform_driver(tegra_emc_driver);
154673a7f0a9SMikko Perttunen 
1547281462e5SDmitry Osipenko MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
1548281462e5SDmitry Osipenko MODULE_DESCRIPTION("NVIDIA Tegra124 EMC driver");
1549281462e5SDmitry Osipenko MODULE_LICENSE("GPL v2");
1550