xref: /linux/drivers/memory/tegra/tegra124-emc.c (revision 141267bffd1dc19a76e4d50e3e4829f85a806875)
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>
129c77a81fSMikko Perttunen #include <linux/debugfs.h>
1373a7f0a9SMikko Perttunen #include <linux/delay.h>
1462e59c4eSStephen Boyd #include <linux/io.h>
1573a7f0a9SMikko Perttunen #include <linux/of_address.h>
1673a7f0a9SMikko Perttunen #include <linux/of_platform.h>
1773a7f0a9SMikko Perttunen #include <linux/platform_device.h>
1873a7f0a9SMikko Perttunen #include <linux/sort.h>
1973a7f0a9SMikko Perttunen #include <linux/string.h>
2073a7f0a9SMikko Perttunen 
2173a7f0a9SMikko Perttunen #include <soc/tegra/emc.h>
2273a7f0a9SMikko Perttunen #include <soc/tegra/fuse.h>
2373a7f0a9SMikko Perttunen #include <soc/tegra/mc.h>
2473a7f0a9SMikko Perttunen 
2573a7f0a9SMikko Perttunen #define EMC_FBIO_CFG5				0x104
2673a7f0a9SMikko Perttunen #define	EMC_FBIO_CFG5_DRAM_TYPE_MASK		0x3
2773a7f0a9SMikko Perttunen #define	EMC_FBIO_CFG5_DRAM_TYPE_SHIFT		0
2873a7f0a9SMikko Perttunen 
2973a7f0a9SMikko Perttunen #define EMC_INTSTATUS				0x0
3073a7f0a9SMikko Perttunen #define EMC_INTSTATUS_CLKCHANGE_COMPLETE	BIT(4)
3173a7f0a9SMikko Perttunen 
3273a7f0a9SMikko Perttunen #define EMC_CFG					0xc
3373a7f0a9SMikko Perttunen #define EMC_CFG_DRAM_CLKSTOP_PD			BIT(31)
3473a7f0a9SMikko Perttunen #define EMC_CFG_DRAM_CLKSTOP_SR			BIT(30)
3573a7f0a9SMikko Perttunen #define EMC_CFG_DRAM_ACPD			BIT(29)
3673a7f0a9SMikko Perttunen #define EMC_CFG_DYN_SREF			BIT(28)
3773a7f0a9SMikko Perttunen #define EMC_CFG_PWR_MASK			((0xF << 28) | BIT(18))
3873a7f0a9SMikko Perttunen #define EMC_CFG_DSR_VTTGEN_DRV_EN		BIT(18)
3973a7f0a9SMikko Perttunen 
4073a7f0a9SMikko Perttunen #define EMC_REFCTRL				0x20
4173a7f0a9SMikko Perttunen #define EMC_REFCTRL_DEV_SEL_SHIFT		0
4273a7f0a9SMikko Perttunen #define EMC_REFCTRL_ENABLE			BIT(31)
4373a7f0a9SMikko Perttunen 
4473a7f0a9SMikko Perttunen #define EMC_TIMING_CONTROL			0x28
4573a7f0a9SMikko Perttunen #define EMC_RC					0x2c
4673a7f0a9SMikko Perttunen #define EMC_RFC					0x30
4773a7f0a9SMikko Perttunen #define EMC_RAS					0x34
4873a7f0a9SMikko Perttunen #define EMC_RP					0x38
4973a7f0a9SMikko Perttunen #define EMC_R2W					0x3c
5073a7f0a9SMikko Perttunen #define EMC_W2R					0x40
5173a7f0a9SMikko Perttunen #define EMC_R2P					0x44
5273a7f0a9SMikko Perttunen #define EMC_W2P					0x48
5373a7f0a9SMikko Perttunen #define EMC_RD_RCD				0x4c
5473a7f0a9SMikko Perttunen #define EMC_WR_RCD				0x50
5573a7f0a9SMikko Perttunen #define EMC_RRD					0x54
5673a7f0a9SMikko Perttunen #define EMC_REXT				0x58
5773a7f0a9SMikko Perttunen #define EMC_WDV					0x5c
5873a7f0a9SMikko Perttunen #define EMC_QUSE				0x60
5973a7f0a9SMikko Perttunen #define EMC_QRST				0x64
6073a7f0a9SMikko Perttunen #define EMC_QSAFE				0x68
6173a7f0a9SMikko Perttunen #define EMC_RDV					0x6c
6273a7f0a9SMikko Perttunen #define EMC_REFRESH				0x70
6373a7f0a9SMikko Perttunen #define EMC_BURST_REFRESH_NUM			0x74
6473a7f0a9SMikko Perttunen #define EMC_PDEX2WR				0x78
6573a7f0a9SMikko Perttunen #define EMC_PDEX2RD				0x7c
6673a7f0a9SMikko Perttunen #define EMC_PCHG2PDEN				0x80
6773a7f0a9SMikko Perttunen #define EMC_ACT2PDEN				0x84
6873a7f0a9SMikko Perttunen #define EMC_AR2PDEN				0x88
6973a7f0a9SMikko Perttunen #define EMC_RW2PDEN				0x8c
7073a7f0a9SMikko Perttunen #define EMC_TXSR				0x90
7173a7f0a9SMikko Perttunen #define EMC_TCKE				0x94
7273a7f0a9SMikko Perttunen #define EMC_TFAW				0x98
7373a7f0a9SMikko Perttunen #define EMC_TRPAB				0x9c
7473a7f0a9SMikko Perttunen #define EMC_TCLKSTABLE				0xa0
7573a7f0a9SMikko Perttunen #define EMC_TCLKSTOP				0xa4
7673a7f0a9SMikko Perttunen #define EMC_TREFBW				0xa8
7773a7f0a9SMikko Perttunen #define EMC_ODT_WRITE				0xb0
7873a7f0a9SMikko Perttunen #define EMC_ODT_READ				0xb4
7973a7f0a9SMikko Perttunen #define EMC_WEXT				0xb8
8073a7f0a9SMikko Perttunen #define EMC_CTT					0xbc
8173a7f0a9SMikko Perttunen #define EMC_RFC_SLR				0xc0
8273a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT2			0xc4
8373a7f0a9SMikko Perttunen 
8473a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT			0xc8
8573a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT	0
8673a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK	\
8773a7f0a9SMikko Perttunen 	(0x3FF << EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT)
8873a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT	16
8973a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_LONG_WAIT_MASK		\
9073a7f0a9SMikko Perttunen 	(0x3FF << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT)
9173a7f0a9SMikko Perttunen 
9273a7f0a9SMikko Perttunen #define EMC_MRS					0xcc
9373a7f0a9SMikko Perttunen #define EMC_MODE_SET_DLL_RESET			BIT(8)
9473a7f0a9SMikko Perttunen #define EMC_MODE_SET_LONG_CNT			BIT(26)
9573a7f0a9SMikko Perttunen #define EMC_EMRS				0xd0
9673a7f0a9SMikko Perttunen #define EMC_REF					0xd4
9773a7f0a9SMikko Perttunen #define EMC_PRE					0xd8
9873a7f0a9SMikko Perttunen 
9973a7f0a9SMikko Perttunen #define EMC_SELF_REF				0xe0
10073a7f0a9SMikko Perttunen #define EMC_SELF_REF_CMD_ENABLED		BIT(0)
10173a7f0a9SMikko Perttunen #define EMC_SELF_REF_DEV_SEL_SHIFT		30
10273a7f0a9SMikko Perttunen 
10373a7f0a9SMikko Perttunen #define EMC_MRW					0xe8
10473a7f0a9SMikko Perttunen 
10573a7f0a9SMikko Perttunen #define EMC_MRR					0xec
10673a7f0a9SMikko Perttunen #define EMC_MRR_MA_SHIFT			16
10773a7f0a9SMikko Perttunen #define LPDDR2_MR4_TEMP_SHIFT			0
10873a7f0a9SMikko Perttunen 
10973a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL3			0xf8
11073a7f0a9SMikko Perttunen #define EMC_FBIO_SPARE				0x100
11173a7f0a9SMikko Perttunen 
11273a7f0a9SMikko Perttunen #define EMC_FBIO_CFG6				0x114
11373a7f0a9SMikko Perttunen #define EMC_EMRS2				0x12c
11473a7f0a9SMikko Perttunen #define EMC_MRW2				0x134
11573a7f0a9SMikko Perttunen #define EMC_MRW4				0x13c
11673a7f0a9SMikko Perttunen #define EMC_EINPUT				0x14c
11773a7f0a9SMikko Perttunen #define EMC_EINPUT_DURATION			0x150
11873a7f0a9SMikko Perttunen #define EMC_PUTERM_EXTRA			0x154
11973a7f0a9SMikko Perttunen #define EMC_TCKESR				0x158
12073a7f0a9SMikko Perttunen #define EMC_TPD					0x15c
12173a7f0a9SMikko Perttunen 
12273a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG			0x2a4
12373a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG_AUTO_CAL_START	BIT(31)
12473a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_INTERVAL			0x2a8
12573a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_STATUS			0x2ac
12673a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_STATUS_ACTIVE		BIT(31)
12773a7f0a9SMikko Perttunen #define EMC_STATUS				0x2b4
12873a7f0a9SMikko Perttunen #define EMC_STATUS_TIMING_UPDATE_STALLED	BIT(23)
12973a7f0a9SMikko Perttunen 
13073a7f0a9SMikko Perttunen #define EMC_CFG_2				0x2b8
13173a7f0a9SMikko Perttunen #define EMC_CFG_2_MODE_SHIFT			0
13273a7f0a9SMikko Perttunen #define EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR	BIT(6)
13373a7f0a9SMikko Perttunen 
13473a7f0a9SMikko Perttunen #define EMC_CFG_DIG_DLL				0x2bc
13573a7f0a9SMikko Perttunen #define EMC_CFG_DIG_DLL_PERIOD			0x2c0
13673a7f0a9SMikko Perttunen #define EMC_RDV_MASK				0x2cc
13773a7f0a9SMikko Perttunen #define EMC_WDV_MASK				0x2d0
13873a7f0a9SMikko Perttunen #define EMC_CTT_DURATION			0x2d8
13973a7f0a9SMikko Perttunen #define EMC_CTT_TERM_CTRL			0x2dc
14073a7f0a9SMikko Perttunen #define EMC_ZCAL_INTERVAL			0x2e0
14173a7f0a9SMikko Perttunen #define EMC_ZCAL_WAIT_CNT			0x2e4
14273a7f0a9SMikko Perttunen 
14373a7f0a9SMikko Perttunen #define EMC_ZQ_CAL				0x2ec
14473a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_CMD				BIT(0)
14573a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_LONG				BIT(4)
14673a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_LONG_CMD_DEV0		\
14773a7f0a9SMikko Perttunen 	(DRAM_DEV_SEL_0 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD)
14873a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_LONG_CMD_DEV1		\
14973a7f0a9SMikko Perttunen 	(DRAM_DEV_SEL_1 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD)
15073a7f0a9SMikko Perttunen 
15173a7f0a9SMikko Perttunen #define EMC_XM2CMDPADCTRL			0x2f0
15273a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL			0x2f8
15373a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL2			0x2fc
15473a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE	BIT(0)
15573a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL2_VREF_ENABLE		BIT(5)
15673a7f0a9SMikko Perttunen #define EMC_XM2DQPADCTRL			0x300
15773a7f0a9SMikko Perttunen #define EMC_XM2DQPADCTRL2			0x304
15873a7f0a9SMikko Perttunen #define EMC_XM2CLKPADCTRL			0x308
15973a7f0a9SMikko Perttunen #define EMC_XM2COMPPADCTRL			0x30c
16073a7f0a9SMikko Perttunen #define EMC_XM2VTTGENPADCTRL			0x310
16173a7f0a9SMikko Perttunen #define EMC_XM2VTTGENPADCTRL2			0x314
16273a7f0a9SMikko Perttunen #define EMC_XM2VTTGENPADCTRL3			0x318
16373a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL4			0x320
16473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS0			0x328
16573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS1			0x32c
16673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS2			0x330
16773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS3			0x334
16873a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS4			0x338
16973a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS5			0x33c
17073a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS6			0x340
17173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS7			0x344
17273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE0			0x348
17373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE1			0x34c
17473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE2			0x350
17573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE3			0x354
17673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE4			0x358
17773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE5			0x35c
17873a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE6			0x360
17973a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE7			0x364
18073a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ0			0x368
18173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ1			0x36c
18273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ2			0x370
18373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ3			0x374
18473a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS0			0x3a8
18573a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS1			0x3ac
18673a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS2			0x3b0
18773a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS3			0x3b4
18873a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS4			0x3b8
18973a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS5			0x3bc
19073a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS6			0x3c0
19173a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS7			0x3c4
19273a7f0a9SMikko Perttunen #define EMC_STALL_THEN_EXE_AFTER_CLKCHANGE	0x3cc
19373a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL			0x3d8
19473a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_DATA_SEL_DPD		BIT(8)
19573a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_ODT_SEL_DPD		BIT(5)
19673a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_RESET_SEL_DPD		BIT(4)
19773a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_CA_SEL_DPD		BIT(3)
19873a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_CLK_SEL_DPD		BIT(2)
19973a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_DDR3_MASK	\
20073a7f0a9SMikko Perttunen 	((0xf << 2) | BIT(8))
20173a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_MASK \
20273a7f0a9SMikko Perttunen 	((0x3 << 2) | BIT(5) | BIT(8))
20373a7f0a9SMikko Perttunen #define EMC_PRE_REFRESH_REQ_CNT			0x3dc
20473a7f0a9SMikko Perttunen #define EMC_DYN_SELF_REF_CONTROL		0x3e0
20573a7f0a9SMikko Perttunen #define EMC_TXSRDLL				0x3e4
20673a7f0a9SMikko Perttunen #define EMC_CCFIFO_ADDR				0x3e8
20773a7f0a9SMikko Perttunen #define EMC_CCFIFO_DATA				0x3ec
20873a7f0a9SMikko Perttunen #define EMC_CCFIFO_STATUS			0x3f0
20973a7f0a9SMikko Perttunen #define EMC_CDB_CNTL_1				0x3f4
21073a7f0a9SMikko Perttunen #define EMC_CDB_CNTL_2				0x3f8
21173a7f0a9SMikko Perttunen #define EMC_XM2CLKPADCTRL2			0x3fc
21273a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG2			0x458
21373a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG3			0x45c
21473a7f0a9SMikko Perttunen #define EMC_IBDLY				0x468
21573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR0			0x46c
21673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR1			0x470
21773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR2			0x474
21873a7f0a9SMikko Perttunen #define EMC_DSR_VTTGEN_DRV			0x47c
21973a7f0a9SMikko Perttunen #define EMC_TXDSRVTTGEN				0x480
22073a7f0a9SMikko Perttunen #define EMC_XM2CMDPADCTRL4			0x484
22173a7f0a9SMikko Perttunen #define EMC_XM2CMDPADCTRL5			0x488
22273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS8			0x4a0
22373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS9			0x4a4
22473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS10			0x4a8
22573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS11			0x4ac
22673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS12			0x4b0
22773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS13			0x4b4
22873a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS14			0x4b8
22973a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS15			0x4bc
23073a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE8			0x4c0
23173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE9			0x4c4
23273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE10			0x4c8
23373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE11			0x4cc
23473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE12			0x4d0
23573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE13			0x4d4
23673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE14			0x4d8
23773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE15			0x4dc
23873a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ4			0x4e0
23973a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ5			0x4e4
24073a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ6			0x4e8
24173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ7			0x4ec
24273a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS8			0x520
24373a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS9			0x524
24473a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS10			0x528
24573a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS11			0x52c
24673a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS12			0x530
24773a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS13			0x534
24873a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS14			0x538
24973a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS15			0x53c
25073a7f0a9SMikko Perttunen #define EMC_CDB_CNTL_3				0x540
25173a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL5			0x544
25273a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL6			0x548
25373a7f0a9SMikko Perttunen #define EMC_XM2DQPADCTRL3			0x54c
25473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR3			0x550
25573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR4			0x554
25673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR5			0x558
25773a7f0a9SMikko Perttunen #define EMC_CFG_PIPE				0x560
25873a7f0a9SMikko Perttunen #define EMC_QPOP				0x564
25973a7f0a9SMikko Perttunen #define EMC_QUSE_WIDTH				0x568
26073a7f0a9SMikko Perttunen #define EMC_PUTERM_WIDTH			0x56c
26173a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0				0x570
26273a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX BIT(3)
26373a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN BIT(2)
26473a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD	BIT(1)
26573a7f0a9SMikko Perttunen #define EMC_PUTERM_ADJ				0x574
26673a7f0a9SMikko Perttunen 
26773a7f0a9SMikko Perttunen #define DRAM_DEV_SEL_ALL			0
26873a7f0a9SMikko Perttunen #define DRAM_DEV_SEL_0				(2 << 30)
26973a7f0a9SMikko Perttunen #define DRAM_DEV_SEL_1				(1 << 30)
27073a7f0a9SMikko Perttunen 
27173a7f0a9SMikko Perttunen #define EMC_CFG_POWER_FEATURES_MASK		\
27273a7f0a9SMikko Perttunen 	(EMC_CFG_DYN_SREF | EMC_CFG_DRAM_ACPD | EMC_CFG_DRAM_CLKSTOP_SR | \
27373a7f0a9SMikko Perttunen 	EMC_CFG_DRAM_CLKSTOP_PD | EMC_CFG_DSR_VTTGEN_DRV_EN)
27473a7f0a9SMikko Perttunen #define EMC_REFCTRL_DEV_SEL(n) (((n > 1) ? 0 : 2) << EMC_REFCTRL_DEV_SEL_SHIFT)
27573a7f0a9SMikko Perttunen #define EMC_DRAM_DEV_SEL(n) ((n > 1) ? DRAM_DEV_SEL_ALL : DRAM_DEV_SEL_0)
27673a7f0a9SMikko Perttunen 
27773a7f0a9SMikko Perttunen /* Maximum amount of time in us. to wait for changes to become effective */
27873a7f0a9SMikko Perttunen #define EMC_STATUS_UPDATE_TIMEOUT		1000
27973a7f0a9SMikko Perttunen 
28073a7f0a9SMikko Perttunen enum emc_dram_type {
28173a7f0a9SMikko Perttunen 	DRAM_TYPE_DDR3 = 0,
28273a7f0a9SMikko Perttunen 	DRAM_TYPE_DDR1 = 1,
28373a7f0a9SMikko Perttunen 	DRAM_TYPE_LPDDR3 = 2,
28473a7f0a9SMikko Perttunen 	DRAM_TYPE_DDR2 = 3
28573a7f0a9SMikko Perttunen };
28673a7f0a9SMikko Perttunen 
28773a7f0a9SMikko Perttunen enum emc_dll_change {
28873a7f0a9SMikko Perttunen 	DLL_CHANGE_NONE,
28973a7f0a9SMikko Perttunen 	DLL_CHANGE_ON,
29073a7f0a9SMikko Perttunen 	DLL_CHANGE_OFF
29173a7f0a9SMikko Perttunen };
29273a7f0a9SMikko Perttunen 
29373a7f0a9SMikko Perttunen static const unsigned long emc_burst_regs[] = {
29473a7f0a9SMikko Perttunen 	EMC_RC,
29573a7f0a9SMikko Perttunen 	EMC_RFC,
29673a7f0a9SMikko Perttunen 	EMC_RFC_SLR,
29773a7f0a9SMikko Perttunen 	EMC_RAS,
29873a7f0a9SMikko Perttunen 	EMC_RP,
29973a7f0a9SMikko Perttunen 	EMC_R2W,
30073a7f0a9SMikko Perttunen 	EMC_W2R,
30173a7f0a9SMikko Perttunen 	EMC_R2P,
30273a7f0a9SMikko Perttunen 	EMC_W2P,
30373a7f0a9SMikko Perttunen 	EMC_RD_RCD,
30473a7f0a9SMikko Perttunen 	EMC_WR_RCD,
30573a7f0a9SMikko Perttunen 	EMC_RRD,
30673a7f0a9SMikko Perttunen 	EMC_REXT,
30773a7f0a9SMikko Perttunen 	EMC_WEXT,
30873a7f0a9SMikko Perttunen 	EMC_WDV,
30973a7f0a9SMikko Perttunen 	EMC_WDV_MASK,
31073a7f0a9SMikko Perttunen 	EMC_QUSE,
31173a7f0a9SMikko Perttunen 	EMC_QUSE_WIDTH,
31273a7f0a9SMikko Perttunen 	EMC_IBDLY,
31373a7f0a9SMikko Perttunen 	EMC_EINPUT,
31473a7f0a9SMikko Perttunen 	EMC_EINPUT_DURATION,
31573a7f0a9SMikko Perttunen 	EMC_PUTERM_EXTRA,
31673a7f0a9SMikko Perttunen 	EMC_PUTERM_WIDTH,
31773a7f0a9SMikko Perttunen 	EMC_PUTERM_ADJ,
31873a7f0a9SMikko Perttunen 	EMC_CDB_CNTL_1,
31973a7f0a9SMikko Perttunen 	EMC_CDB_CNTL_2,
32073a7f0a9SMikko Perttunen 	EMC_CDB_CNTL_3,
32173a7f0a9SMikko Perttunen 	EMC_QRST,
32273a7f0a9SMikko Perttunen 	EMC_QSAFE,
32373a7f0a9SMikko Perttunen 	EMC_RDV,
32473a7f0a9SMikko Perttunen 	EMC_RDV_MASK,
32573a7f0a9SMikko Perttunen 	EMC_REFRESH,
32673a7f0a9SMikko Perttunen 	EMC_BURST_REFRESH_NUM,
32773a7f0a9SMikko Perttunen 	EMC_PRE_REFRESH_REQ_CNT,
32873a7f0a9SMikko Perttunen 	EMC_PDEX2WR,
32973a7f0a9SMikko Perttunen 	EMC_PDEX2RD,
33073a7f0a9SMikko Perttunen 	EMC_PCHG2PDEN,
33173a7f0a9SMikko Perttunen 	EMC_ACT2PDEN,
33273a7f0a9SMikko Perttunen 	EMC_AR2PDEN,
33373a7f0a9SMikko Perttunen 	EMC_RW2PDEN,
33473a7f0a9SMikko Perttunen 	EMC_TXSR,
33573a7f0a9SMikko Perttunen 	EMC_TXSRDLL,
33673a7f0a9SMikko Perttunen 	EMC_TCKE,
33773a7f0a9SMikko Perttunen 	EMC_TCKESR,
33873a7f0a9SMikko Perttunen 	EMC_TPD,
33973a7f0a9SMikko Perttunen 	EMC_TFAW,
34073a7f0a9SMikko Perttunen 	EMC_TRPAB,
34173a7f0a9SMikko Perttunen 	EMC_TCLKSTABLE,
34273a7f0a9SMikko Perttunen 	EMC_TCLKSTOP,
34373a7f0a9SMikko Perttunen 	EMC_TREFBW,
34473a7f0a9SMikko Perttunen 	EMC_FBIO_CFG6,
34573a7f0a9SMikko Perttunen 	EMC_ODT_WRITE,
34673a7f0a9SMikko Perttunen 	EMC_ODT_READ,
34773a7f0a9SMikko Perttunen 	EMC_FBIO_CFG5,
34873a7f0a9SMikko Perttunen 	EMC_CFG_DIG_DLL,
34973a7f0a9SMikko Perttunen 	EMC_CFG_DIG_DLL_PERIOD,
35073a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS0,
35173a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS1,
35273a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS2,
35373a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS3,
35473a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS4,
35573a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS5,
35673a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS6,
35773a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS7,
35873a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS8,
35973a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS9,
36073a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS10,
36173a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS11,
36273a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS12,
36373a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS13,
36473a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS14,
36573a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQS15,
36673a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE0,
36773a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE1,
36873a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE2,
36973a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE3,
37073a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE4,
37173a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE5,
37273a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE6,
37373a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE7,
37473a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_ADDR0,
37573a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_ADDR1,
37673a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_ADDR2,
37773a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_ADDR3,
37873a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_ADDR4,
37973a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_ADDR5,
38073a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE8,
38173a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE9,
38273a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE10,
38373a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE11,
38473a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE12,
38573a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE13,
38673a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE14,
38773a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_QUSE15,
38873a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS0,
38973a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS1,
39073a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS2,
39173a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS3,
39273a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS4,
39373a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS5,
39473a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS6,
39573a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS7,
39673a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS8,
39773a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS9,
39873a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS10,
39973a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS11,
40073a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS12,
40173a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS13,
40273a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS14,
40373a7f0a9SMikko Perttunen 	EMC_DLI_TRIM_TXDQS15,
40473a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ0,
40573a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ1,
40673a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ2,
40773a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ3,
40873a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ4,
40973a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ5,
41073a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ6,
41173a7f0a9SMikko Perttunen 	EMC_DLL_XFORM_DQ7,
41273a7f0a9SMikko Perttunen 	EMC_XM2CMDPADCTRL,
41373a7f0a9SMikko Perttunen 	EMC_XM2CMDPADCTRL4,
41473a7f0a9SMikko Perttunen 	EMC_XM2CMDPADCTRL5,
41573a7f0a9SMikko Perttunen 	EMC_XM2DQPADCTRL2,
41673a7f0a9SMikko Perttunen 	EMC_XM2DQPADCTRL3,
41773a7f0a9SMikko Perttunen 	EMC_XM2CLKPADCTRL,
41873a7f0a9SMikko Perttunen 	EMC_XM2CLKPADCTRL2,
41973a7f0a9SMikko Perttunen 	EMC_XM2COMPPADCTRL,
42073a7f0a9SMikko Perttunen 	EMC_XM2VTTGENPADCTRL,
42173a7f0a9SMikko Perttunen 	EMC_XM2VTTGENPADCTRL2,
42273a7f0a9SMikko Perttunen 	EMC_XM2VTTGENPADCTRL3,
42373a7f0a9SMikko Perttunen 	EMC_XM2DQSPADCTRL3,
42473a7f0a9SMikko Perttunen 	EMC_XM2DQSPADCTRL4,
42573a7f0a9SMikko Perttunen 	EMC_XM2DQSPADCTRL5,
42673a7f0a9SMikko Perttunen 	EMC_XM2DQSPADCTRL6,
42773a7f0a9SMikko Perttunen 	EMC_DSR_VTTGEN_DRV,
42873a7f0a9SMikko Perttunen 	EMC_TXDSRVTTGEN,
42973a7f0a9SMikko Perttunen 	EMC_FBIO_SPARE,
43073a7f0a9SMikko Perttunen 	EMC_ZCAL_WAIT_CNT,
43173a7f0a9SMikko Perttunen 	EMC_MRS_WAIT_CNT2,
43273a7f0a9SMikko Perttunen 	EMC_CTT,
43373a7f0a9SMikko Perttunen 	EMC_CTT_DURATION,
43473a7f0a9SMikko Perttunen 	EMC_CFG_PIPE,
43573a7f0a9SMikko Perttunen 	EMC_DYN_SELF_REF_CONTROL,
43673a7f0a9SMikko Perttunen 	EMC_QPOP
43773a7f0a9SMikko Perttunen };
43873a7f0a9SMikko Perttunen 
43973a7f0a9SMikko Perttunen struct emc_timing {
44073a7f0a9SMikko Perttunen 	unsigned long rate;
44173a7f0a9SMikko Perttunen 
44273a7f0a9SMikko Perttunen 	u32 emc_burst_data[ARRAY_SIZE(emc_burst_regs)];
44373a7f0a9SMikko Perttunen 
44473a7f0a9SMikko Perttunen 	u32 emc_auto_cal_config;
44573a7f0a9SMikko Perttunen 	u32 emc_auto_cal_config2;
44673a7f0a9SMikko Perttunen 	u32 emc_auto_cal_config3;
44773a7f0a9SMikko Perttunen 	u32 emc_auto_cal_interval;
44873a7f0a9SMikko Perttunen 	u32 emc_bgbias_ctl0;
44973a7f0a9SMikko Perttunen 	u32 emc_cfg;
45073a7f0a9SMikko Perttunen 	u32 emc_cfg_2;
45173a7f0a9SMikko Perttunen 	u32 emc_ctt_term_ctrl;
45273a7f0a9SMikko Perttunen 	u32 emc_mode_1;
45373a7f0a9SMikko Perttunen 	u32 emc_mode_2;
45473a7f0a9SMikko Perttunen 	u32 emc_mode_4;
45573a7f0a9SMikko Perttunen 	u32 emc_mode_reset;
45673a7f0a9SMikko Perttunen 	u32 emc_mrs_wait_cnt;
45773a7f0a9SMikko Perttunen 	u32 emc_sel_dpd_ctrl;
45873a7f0a9SMikko Perttunen 	u32 emc_xm2dqspadctrl2;
45973a7f0a9SMikko Perttunen 	u32 emc_zcal_cnt_long;
46073a7f0a9SMikko Perttunen 	u32 emc_zcal_interval;
46173a7f0a9SMikko Perttunen };
46273a7f0a9SMikko Perttunen 
46373a7f0a9SMikko Perttunen struct tegra_emc {
46473a7f0a9SMikko Perttunen 	struct device *dev;
46573a7f0a9SMikko Perttunen 
46673a7f0a9SMikko Perttunen 	struct tegra_mc *mc;
46773a7f0a9SMikko Perttunen 
46873a7f0a9SMikko Perttunen 	void __iomem *regs;
46973a7f0a9SMikko Perttunen 
4706b9acd93SThierry Reding 	struct clk *clk;
4716b9acd93SThierry Reding 
47273a7f0a9SMikko Perttunen 	enum emc_dram_type dram_type;
47373a7f0a9SMikko Perttunen 	unsigned int dram_num;
47473a7f0a9SMikko Perttunen 
47573a7f0a9SMikko Perttunen 	struct emc_timing last_timing;
47673a7f0a9SMikko Perttunen 	struct emc_timing *timings;
47773a7f0a9SMikko Perttunen 	unsigned int num_timings;
4786b9acd93SThierry Reding 
4796b9acd93SThierry Reding 	struct {
4806b9acd93SThierry Reding 		struct dentry *root;
4816b9acd93SThierry Reding 		unsigned long min_rate;
4826b9acd93SThierry Reding 		unsigned long max_rate;
4836b9acd93SThierry Reding 	} debugfs;
48473a7f0a9SMikko Perttunen };
48573a7f0a9SMikko Perttunen 
48673a7f0a9SMikko Perttunen /* Timing change sequence functions */
48773a7f0a9SMikko Perttunen 
48873a7f0a9SMikko Perttunen static void emc_ccfifo_writel(struct tegra_emc *emc, u32 value,
48973a7f0a9SMikko Perttunen 			      unsigned long offset)
49073a7f0a9SMikko Perttunen {
49173a7f0a9SMikko Perttunen 	writel(value, emc->regs + EMC_CCFIFO_DATA);
49273a7f0a9SMikko Perttunen 	writel(offset, emc->regs + EMC_CCFIFO_ADDR);
49373a7f0a9SMikko Perttunen }
49473a7f0a9SMikko Perttunen 
49573a7f0a9SMikko Perttunen static void emc_seq_update_timing(struct tegra_emc *emc)
49673a7f0a9SMikko Perttunen {
49773a7f0a9SMikko Perttunen 	unsigned int i;
49873a7f0a9SMikko Perttunen 	u32 value;
49973a7f0a9SMikko Perttunen 
50073a7f0a9SMikko Perttunen 	writel(1, emc->regs + EMC_TIMING_CONTROL);
50173a7f0a9SMikko Perttunen 
50273a7f0a9SMikko Perttunen 	for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
50373a7f0a9SMikko Perttunen 		value = readl(emc->regs + EMC_STATUS);
50473a7f0a9SMikko Perttunen 		if ((value & EMC_STATUS_TIMING_UPDATE_STALLED) == 0)
50573a7f0a9SMikko Perttunen 			return;
50673a7f0a9SMikko Perttunen 		udelay(1);
50773a7f0a9SMikko Perttunen 	}
50873a7f0a9SMikko Perttunen 
50973a7f0a9SMikko Perttunen 	dev_err(emc->dev, "timing update timed out\n");
51073a7f0a9SMikko Perttunen }
51173a7f0a9SMikko Perttunen 
51273a7f0a9SMikko Perttunen static void emc_seq_disable_auto_cal(struct tegra_emc *emc)
51373a7f0a9SMikko Perttunen {
51473a7f0a9SMikko Perttunen 	unsigned int i;
51573a7f0a9SMikko Perttunen 	u32 value;
51673a7f0a9SMikko Perttunen 
51773a7f0a9SMikko Perttunen 	writel(0, emc->regs + EMC_AUTO_CAL_INTERVAL);
51873a7f0a9SMikko Perttunen 
51973a7f0a9SMikko Perttunen 	for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
52073a7f0a9SMikko Perttunen 		value = readl(emc->regs + EMC_AUTO_CAL_STATUS);
52173a7f0a9SMikko Perttunen 		if ((value & EMC_AUTO_CAL_STATUS_ACTIVE) == 0)
52273a7f0a9SMikko Perttunen 			return;
52373a7f0a9SMikko Perttunen 		udelay(1);
52473a7f0a9SMikko Perttunen 	}
52573a7f0a9SMikko Perttunen 
52673a7f0a9SMikko Perttunen 	dev_err(emc->dev, "auto cal disable timed out\n");
52773a7f0a9SMikko Perttunen }
52873a7f0a9SMikko Perttunen 
52973a7f0a9SMikko Perttunen static void emc_seq_wait_clkchange(struct tegra_emc *emc)
53073a7f0a9SMikko Perttunen {
53173a7f0a9SMikko Perttunen 	unsigned int i;
53273a7f0a9SMikko Perttunen 	u32 value;
53373a7f0a9SMikko Perttunen 
53473a7f0a9SMikko Perttunen 	for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
53573a7f0a9SMikko Perttunen 		value = readl(emc->regs + EMC_INTSTATUS);
53673a7f0a9SMikko Perttunen 		if (value & EMC_INTSTATUS_CLKCHANGE_COMPLETE)
53773a7f0a9SMikko Perttunen 			return;
53873a7f0a9SMikko Perttunen 		udelay(1);
53973a7f0a9SMikko Perttunen 	}
54073a7f0a9SMikko Perttunen 
54173a7f0a9SMikko Perttunen 	dev_err(emc->dev, "clock change timed out\n");
54273a7f0a9SMikko Perttunen }
54373a7f0a9SMikko Perttunen 
54473a7f0a9SMikko Perttunen static struct emc_timing *tegra_emc_find_timing(struct tegra_emc *emc,
54573a7f0a9SMikko Perttunen 						unsigned long rate)
54673a7f0a9SMikko Perttunen {
54773a7f0a9SMikko Perttunen 	struct emc_timing *timing = NULL;
54873a7f0a9SMikko Perttunen 	unsigned int i;
54973a7f0a9SMikko Perttunen 
55073a7f0a9SMikko Perttunen 	for (i = 0; i < emc->num_timings; i++) {
55173a7f0a9SMikko Perttunen 		if (emc->timings[i].rate == rate) {
55273a7f0a9SMikko Perttunen 			timing = &emc->timings[i];
55373a7f0a9SMikko Perttunen 			break;
55473a7f0a9SMikko Perttunen 		}
55573a7f0a9SMikko Perttunen 	}
55673a7f0a9SMikko Perttunen 
55773a7f0a9SMikko Perttunen 	if (!timing) {
55873a7f0a9SMikko Perttunen 		dev_err(emc->dev, "no timing for rate %lu\n", rate);
55973a7f0a9SMikko Perttunen 		return NULL;
56073a7f0a9SMikko Perttunen 	}
56173a7f0a9SMikko Perttunen 
56273a7f0a9SMikko Perttunen 	return timing;
56373a7f0a9SMikko Perttunen }
56473a7f0a9SMikko Perttunen 
56573a7f0a9SMikko Perttunen int tegra_emc_prepare_timing_change(struct tegra_emc *emc,
56673a7f0a9SMikko Perttunen 				    unsigned long rate)
56773a7f0a9SMikko Perttunen {
56873a7f0a9SMikko Perttunen 	struct emc_timing *timing = tegra_emc_find_timing(emc, rate);
56973a7f0a9SMikko Perttunen 	struct emc_timing *last = &emc->last_timing;
57073a7f0a9SMikko Perttunen 	enum emc_dll_change dll_change;
57173a7f0a9SMikko Perttunen 	unsigned int pre_wait = 0;
57273a7f0a9SMikko Perttunen 	u32 val, val2, mask;
57373a7f0a9SMikko Perttunen 	bool update = false;
57473a7f0a9SMikko Perttunen 	unsigned int i;
57573a7f0a9SMikko Perttunen 
57673a7f0a9SMikko Perttunen 	if (!timing)
57773a7f0a9SMikko Perttunen 		return -ENOENT;
57873a7f0a9SMikko Perttunen 
57973a7f0a9SMikko Perttunen 	if ((last->emc_mode_1 & 0x1) == (timing->emc_mode_1 & 0x1))
58073a7f0a9SMikko Perttunen 		dll_change = DLL_CHANGE_NONE;
58173a7f0a9SMikko Perttunen 	else if (timing->emc_mode_1 & 0x1)
58273a7f0a9SMikko Perttunen 		dll_change = DLL_CHANGE_ON;
58373a7f0a9SMikko Perttunen 	else
58473a7f0a9SMikko Perttunen 		dll_change = DLL_CHANGE_OFF;
58573a7f0a9SMikko Perttunen 
58673a7f0a9SMikko Perttunen 	/* Clear CLKCHANGE_COMPLETE interrupts */
58773a7f0a9SMikko Perttunen 	writel(EMC_INTSTATUS_CLKCHANGE_COMPLETE, emc->regs + EMC_INTSTATUS);
58873a7f0a9SMikko Perttunen 
58973a7f0a9SMikko Perttunen 	/* Disable dynamic self-refresh */
59073a7f0a9SMikko Perttunen 	val = readl(emc->regs + EMC_CFG);
59173a7f0a9SMikko Perttunen 	if (val & EMC_CFG_PWR_MASK) {
59273a7f0a9SMikko Perttunen 		val &= ~EMC_CFG_POWER_FEATURES_MASK;
59373a7f0a9SMikko Perttunen 		writel(val, emc->regs + EMC_CFG);
59473a7f0a9SMikko Perttunen 
59573a7f0a9SMikko Perttunen 		pre_wait = 5;
59673a7f0a9SMikko Perttunen 	}
59773a7f0a9SMikko Perttunen 
59873a7f0a9SMikko Perttunen 	/* Disable SEL_DPD_CTRL for clock change */
59973a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_DDR3)
60073a7f0a9SMikko Perttunen 		mask = EMC_SEL_DPD_CTRL_DDR3_MASK;
60173a7f0a9SMikko Perttunen 	else
60273a7f0a9SMikko Perttunen 		mask = EMC_SEL_DPD_CTRL_MASK;
60373a7f0a9SMikko Perttunen 
60473a7f0a9SMikko Perttunen 	val = readl(emc->regs + EMC_SEL_DPD_CTRL);
60573a7f0a9SMikko Perttunen 	if (val & mask) {
60673a7f0a9SMikko Perttunen 		val &= ~mask;
60773a7f0a9SMikko Perttunen 		writel(val, emc->regs + EMC_SEL_DPD_CTRL);
60873a7f0a9SMikko Perttunen 	}
60973a7f0a9SMikko Perttunen 
61073a7f0a9SMikko Perttunen 	/* Prepare DQ/DQS for clock change */
61173a7f0a9SMikko Perttunen 	val = readl(emc->regs + EMC_BGBIAS_CTL0);
61273a7f0a9SMikko Perttunen 	val2 = last->emc_bgbias_ctl0;
61373a7f0a9SMikko Perttunen 	if (!(timing->emc_bgbias_ctl0 &
61473a7f0a9SMikko Perttunen 	      EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) &&
61573a7f0a9SMikko Perttunen 	    (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX)) {
61673a7f0a9SMikko Perttunen 		val2 &= ~EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX;
61773a7f0a9SMikko Perttunen 		update = true;
61873a7f0a9SMikko Perttunen 	}
61973a7f0a9SMikko Perttunen 
62073a7f0a9SMikko Perttunen 	if ((val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD) ||
62173a7f0a9SMikko Perttunen 	    (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN)) {
62273a7f0a9SMikko Perttunen 		update = true;
62373a7f0a9SMikko Perttunen 	}
62473a7f0a9SMikko Perttunen 
62573a7f0a9SMikko Perttunen 	if (update) {
62673a7f0a9SMikko Perttunen 		writel(val2, emc->regs + EMC_BGBIAS_CTL0);
62773a7f0a9SMikko Perttunen 		if (pre_wait < 5)
62873a7f0a9SMikko Perttunen 			pre_wait = 5;
62973a7f0a9SMikko Perttunen 	}
63073a7f0a9SMikko Perttunen 
63173a7f0a9SMikko Perttunen 	update = false;
63273a7f0a9SMikko Perttunen 	val = readl(emc->regs + EMC_XM2DQSPADCTRL2);
63373a7f0a9SMikko Perttunen 	if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_VREF_ENABLE &&
63473a7f0a9SMikko Perttunen 	    !(val & EMC_XM2DQSPADCTRL2_VREF_ENABLE)) {
63573a7f0a9SMikko Perttunen 		val |= EMC_XM2DQSPADCTRL2_VREF_ENABLE;
63673a7f0a9SMikko Perttunen 		update = true;
63773a7f0a9SMikko Perttunen 	}
63873a7f0a9SMikko Perttunen 
63973a7f0a9SMikko Perttunen 	if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE &&
64073a7f0a9SMikko Perttunen 	    !(val & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE)) {
64173a7f0a9SMikko Perttunen 		val |= EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE;
64273a7f0a9SMikko Perttunen 		update = true;
64373a7f0a9SMikko Perttunen 	}
64473a7f0a9SMikko Perttunen 
64573a7f0a9SMikko Perttunen 	if (update) {
64673a7f0a9SMikko Perttunen 		writel(val, emc->regs + EMC_XM2DQSPADCTRL2);
64773a7f0a9SMikko Perttunen 		if (pre_wait < 30)
64873a7f0a9SMikko Perttunen 			pre_wait = 30;
64973a7f0a9SMikko Perttunen 	}
65073a7f0a9SMikko Perttunen 
65173a7f0a9SMikko Perttunen 	/* Wait to settle */
65273a7f0a9SMikko Perttunen 	if (pre_wait) {
65373a7f0a9SMikko Perttunen 		emc_seq_update_timing(emc);
65473a7f0a9SMikko Perttunen 		udelay(pre_wait);
65573a7f0a9SMikko Perttunen 	}
65673a7f0a9SMikko Perttunen 
65773a7f0a9SMikko Perttunen 	/* Program CTT_TERM control */
65873a7f0a9SMikko Perttunen 	if (last->emc_ctt_term_ctrl != timing->emc_ctt_term_ctrl) {
65973a7f0a9SMikko Perttunen 		emc_seq_disable_auto_cal(emc);
66073a7f0a9SMikko Perttunen 		writel(timing->emc_ctt_term_ctrl,
66173a7f0a9SMikko Perttunen 		       emc->regs + EMC_CTT_TERM_CTRL);
66273a7f0a9SMikko Perttunen 		emc_seq_update_timing(emc);
66373a7f0a9SMikko Perttunen 	}
66473a7f0a9SMikko Perttunen 
66573a7f0a9SMikko Perttunen 	/* Program burst shadow registers */
66673a7f0a9SMikko Perttunen 	for (i = 0; i < ARRAY_SIZE(timing->emc_burst_data); ++i)
66773a7f0a9SMikko Perttunen 		writel(timing->emc_burst_data[i],
66873a7f0a9SMikko Perttunen 		       emc->regs + emc_burst_regs[i]);
66973a7f0a9SMikko Perttunen 
67073a7f0a9SMikko Perttunen 	writel(timing->emc_xm2dqspadctrl2, emc->regs + EMC_XM2DQSPADCTRL2);
67173a7f0a9SMikko Perttunen 	writel(timing->emc_zcal_interval, emc->regs + EMC_ZCAL_INTERVAL);
67273a7f0a9SMikko Perttunen 
67373a7f0a9SMikko Perttunen 	tegra_mc_write_emem_configuration(emc->mc, timing->rate);
67473a7f0a9SMikko Perttunen 
67573a7f0a9SMikko Perttunen 	val = timing->emc_cfg & ~EMC_CFG_POWER_FEATURES_MASK;
67673a7f0a9SMikko Perttunen 	emc_ccfifo_writel(emc, val, EMC_CFG);
67773a7f0a9SMikko Perttunen 
67873a7f0a9SMikko Perttunen 	/* Program AUTO_CAL_CONFIG */
67973a7f0a9SMikko Perttunen 	if (timing->emc_auto_cal_config2 != last->emc_auto_cal_config2)
68073a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, timing->emc_auto_cal_config2,
68173a7f0a9SMikko Perttunen 				  EMC_AUTO_CAL_CONFIG2);
68273a7f0a9SMikko Perttunen 
68373a7f0a9SMikko Perttunen 	if (timing->emc_auto_cal_config3 != last->emc_auto_cal_config3)
68473a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, timing->emc_auto_cal_config3,
68573a7f0a9SMikko Perttunen 				  EMC_AUTO_CAL_CONFIG3);
68673a7f0a9SMikko Perttunen 
68773a7f0a9SMikko Perttunen 	if (timing->emc_auto_cal_config != last->emc_auto_cal_config) {
68873a7f0a9SMikko Perttunen 		val = timing->emc_auto_cal_config;
68973a7f0a9SMikko Perttunen 		val &= EMC_AUTO_CAL_CONFIG_AUTO_CAL_START;
69073a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, val, EMC_AUTO_CAL_CONFIG);
69173a7f0a9SMikko Perttunen 	}
69273a7f0a9SMikko Perttunen 
69373a7f0a9SMikko Perttunen 	/* DDR3: predict MRS long wait count */
69473a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_DDR3 &&
69573a7f0a9SMikko Perttunen 	    dll_change == DLL_CHANGE_ON) {
69673a7f0a9SMikko Perttunen 		u32 cnt = 512;
69773a7f0a9SMikko Perttunen 
69873a7f0a9SMikko Perttunen 		if (timing->emc_zcal_interval != 0 &&
69973a7f0a9SMikko Perttunen 		    last->emc_zcal_interval == 0)
70073a7f0a9SMikko Perttunen 			cnt -= emc->dram_num * 256;
70173a7f0a9SMikko Perttunen 
70273a7f0a9SMikko Perttunen 		val = (timing->emc_mrs_wait_cnt
70373a7f0a9SMikko Perttunen 			& EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK)
70473a7f0a9SMikko Perttunen 			>> EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT;
70573a7f0a9SMikko Perttunen 		if (cnt < val)
70673a7f0a9SMikko Perttunen 			cnt = val;
70773a7f0a9SMikko Perttunen 
70873a7f0a9SMikko Perttunen 		val = timing->emc_mrs_wait_cnt
70973a7f0a9SMikko Perttunen 			& ~EMC_MRS_WAIT_CNT_LONG_WAIT_MASK;
71073a7f0a9SMikko Perttunen 		val |= (cnt << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT)
71173a7f0a9SMikko Perttunen 			& EMC_MRS_WAIT_CNT_LONG_WAIT_MASK;
71273a7f0a9SMikko Perttunen 
71373a7f0a9SMikko Perttunen 		writel(val, emc->regs + EMC_MRS_WAIT_CNT);
71473a7f0a9SMikko Perttunen 	}
71573a7f0a9SMikko Perttunen 
71673a7f0a9SMikko Perttunen 	val = timing->emc_cfg_2;
71773a7f0a9SMikko Perttunen 	val &= ~EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR;
71873a7f0a9SMikko Perttunen 	emc_ccfifo_writel(emc, val, EMC_CFG_2);
71973a7f0a9SMikko Perttunen 
72073a7f0a9SMikko Perttunen 	/* DDR3: Turn off DLL and enter self-refresh */
72173a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_DDR3 && dll_change == DLL_CHANGE_OFF)
72273a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS);
72373a7f0a9SMikko Perttunen 
72473a7f0a9SMikko Perttunen 	/* Disable refresh controller */
72573a7f0a9SMikko Perttunen 	emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num),
72673a7f0a9SMikko Perttunen 			  EMC_REFCTRL);
72773a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_DDR3)
72873a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num) |
72973a7f0a9SMikko Perttunen 				       EMC_SELF_REF_CMD_ENABLED,
73073a7f0a9SMikko Perttunen 				  EMC_SELF_REF);
73173a7f0a9SMikko Perttunen 
73273a7f0a9SMikko Perttunen 	/* Flow control marker */
73373a7f0a9SMikko Perttunen 	emc_ccfifo_writel(emc, 1, EMC_STALL_THEN_EXE_AFTER_CLKCHANGE);
73473a7f0a9SMikko Perttunen 
73573a7f0a9SMikko Perttunen 	/* DDR3: Exit self-refresh */
73673a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_DDR3)
73773a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num),
73873a7f0a9SMikko Perttunen 				  EMC_SELF_REF);
73973a7f0a9SMikko Perttunen 	emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num) |
74073a7f0a9SMikko Perttunen 			       EMC_REFCTRL_ENABLE,
74173a7f0a9SMikko Perttunen 			  EMC_REFCTRL);
74273a7f0a9SMikko Perttunen 
74373a7f0a9SMikko Perttunen 	/* Set DRAM mode registers */
74473a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_DDR3) {
74573a7f0a9SMikko Perttunen 		if (timing->emc_mode_1 != last->emc_mode_1)
74673a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS);
74773a7f0a9SMikko Perttunen 		if (timing->emc_mode_2 != last->emc_mode_2)
74873a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_EMRS2);
74973a7f0a9SMikko Perttunen 
75073a7f0a9SMikko Perttunen 		if ((timing->emc_mode_reset != last->emc_mode_reset) ||
75173a7f0a9SMikko Perttunen 		    dll_change == DLL_CHANGE_ON) {
75273a7f0a9SMikko Perttunen 			val = timing->emc_mode_reset;
75373a7f0a9SMikko Perttunen 			if (dll_change == DLL_CHANGE_ON) {
75473a7f0a9SMikko Perttunen 				val |= EMC_MODE_SET_DLL_RESET;
75573a7f0a9SMikko Perttunen 				val |= EMC_MODE_SET_LONG_CNT;
75673a7f0a9SMikko Perttunen 			} else {
75773a7f0a9SMikko Perttunen 				val &= ~EMC_MODE_SET_DLL_RESET;
75873a7f0a9SMikko Perttunen 			}
75973a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, val, EMC_MRS);
76073a7f0a9SMikko Perttunen 		}
76173a7f0a9SMikko Perttunen 	} else {
76273a7f0a9SMikko Perttunen 		if (timing->emc_mode_2 != last->emc_mode_2)
76373a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_MRW2);
76473a7f0a9SMikko Perttunen 		if (timing->emc_mode_1 != last->emc_mode_1)
76573a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_MRW);
76673a7f0a9SMikko Perttunen 		if (timing->emc_mode_4 != last->emc_mode_4)
76773a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, timing->emc_mode_4, EMC_MRW4);
76873a7f0a9SMikko Perttunen 	}
76973a7f0a9SMikko Perttunen 
77073a7f0a9SMikko Perttunen 	/*  Issue ZCAL command if turning ZCAL on */
77173a7f0a9SMikko Perttunen 	if (timing->emc_zcal_interval != 0 && last->emc_zcal_interval == 0) {
77273a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV0, EMC_ZQ_CAL);
77373a7f0a9SMikko Perttunen 		if (emc->dram_num > 1)
77473a7f0a9SMikko Perttunen 			emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV1,
77573a7f0a9SMikko Perttunen 					  EMC_ZQ_CAL);
77673a7f0a9SMikko Perttunen 	}
77773a7f0a9SMikko Perttunen 
77873a7f0a9SMikko Perttunen 	/*  Write to RO register to remove stall after change */
77973a7f0a9SMikko Perttunen 	emc_ccfifo_writel(emc, 0, EMC_CCFIFO_STATUS);
78073a7f0a9SMikko Perttunen 
78173a7f0a9SMikko Perttunen 	if (timing->emc_cfg_2 & EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR)
78273a7f0a9SMikko Perttunen 		emc_ccfifo_writel(emc, timing->emc_cfg_2, EMC_CFG_2);
78373a7f0a9SMikko Perttunen 
78473a7f0a9SMikko Perttunen 	/* Disable AUTO_CAL for clock change */
78573a7f0a9SMikko Perttunen 	emc_seq_disable_auto_cal(emc);
78673a7f0a9SMikko Perttunen 
78773a7f0a9SMikko Perttunen 	/* Read register to wait until programming has settled */
78873a7f0a9SMikko Perttunen 	readl(emc->regs + EMC_INTSTATUS);
78973a7f0a9SMikko Perttunen 
79073a7f0a9SMikko Perttunen 	return 0;
79173a7f0a9SMikko Perttunen }
79273a7f0a9SMikko Perttunen 
79373a7f0a9SMikko Perttunen void tegra_emc_complete_timing_change(struct tegra_emc *emc,
79473a7f0a9SMikko Perttunen 				      unsigned long rate)
79573a7f0a9SMikko Perttunen {
79673a7f0a9SMikko Perttunen 	struct emc_timing *timing = tegra_emc_find_timing(emc, rate);
79773a7f0a9SMikko Perttunen 	struct emc_timing *last = &emc->last_timing;
79873a7f0a9SMikko Perttunen 	u32 val;
79973a7f0a9SMikko Perttunen 
80073a7f0a9SMikko Perttunen 	if (!timing)
80173a7f0a9SMikko Perttunen 		return;
80273a7f0a9SMikko Perttunen 
80373a7f0a9SMikko Perttunen 	/* Wait until the state machine has settled */
80473a7f0a9SMikko Perttunen 	emc_seq_wait_clkchange(emc);
80573a7f0a9SMikko Perttunen 
80673a7f0a9SMikko Perttunen 	/* Restore AUTO_CAL */
80773a7f0a9SMikko Perttunen 	if (timing->emc_ctt_term_ctrl != last->emc_ctt_term_ctrl)
80873a7f0a9SMikko Perttunen 		writel(timing->emc_auto_cal_interval,
80973a7f0a9SMikko Perttunen 		       emc->regs + EMC_AUTO_CAL_INTERVAL);
81073a7f0a9SMikko Perttunen 
81173a7f0a9SMikko Perttunen 	/* Restore dynamic self-refresh */
81273a7f0a9SMikko Perttunen 	if (timing->emc_cfg & EMC_CFG_PWR_MASK)
81373a7f0a9SMikko Perttunen 		writel(timing->emc_cfg, emc->regs + EMC_CFG);
81473a7f0a9SMikko Perttunen 
81573a7f0a9SMikko Perttunen 	/* Set ZCAL wait count */
81673a7f0a9SMikko Perttunen 	writel(timing->emc_zcal_cnt_long, emc->regs + EMC_ZCAL_WAIT_CNT);
81773a7f0a9SMikko Perttunen 
81873a7f0a9SMikko Perttunen 	/* LPDDR3: Turn off BGBIAS if low frequency */
81973a7f0a9SMikko Perttunen 	if (emc->dram_type == DRAM_TYPE_LPDDR3 &&
82073a7f0a9SMikko Perttunen 	    timing->emc_bgbias_ctl0 &
82173a7f0a9SMikko Perttunen 	      EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) {
82273a7f0a9SMikko Perttunen 		val = timing->emc_bgbias_ctl0;
82373a7f0a9SMikko Perttunen 		val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN;
82473a7f0a9SMikko Perttunen 		val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD;
82573a7f0a9SMikko Perttunen 		writel(val, emc->regs + EMC_BGBIAS_CTL0);
82673a7f0a9SMikko Perttunen 	} else {
82773a7f0a9SMikko Perttunen 		if (emc->dram_type == DRAM_TYPE_DDR3 &&
82873a7f0a9SMikko Perttunen 		    readl(emc->regs + EMC_BGBIAS_CTL0) !=
82973a7f0a9SMikko Perttunen 		      timing->emc_bgbias_ctl0) {
83073a7f0a9SMikko Perttunen 			writel(timing->emc_bgbias_ctl0,
83173a7f0a9SMikko Perttunen 			       emc->regs + EMC_BGBIAS_CTL0);
83273a7f0a9SMikko Perttunen 		}
83373a7f0a9SMikko Perttunen 
83473a7f0a9SMikko Perttunen 		writel(timing->emc_auto_cal_interval,
83573a7f0a9SMikko Perttunen 		       emc->regs + EMC_AUTO_CAL_INTERVAL);
83673a7f0a9SMikko Perttunen 	}
83773a7f0a9SMikko Perttunen 
83873a7f0a9SMikko Perttunen 	/* Wait for timing to settle */
83973a7f0a9SMikko Perttunen 	udelay(2);
84073a7f0a9SMikko Perttunen 
84173a7f0a9SMikko Perttunen 	/* Reprogram SEL_DPD_CTRL */
84273a7f0a9SMikko Perttunen 	writel(timing->emc_sel_dpd_ctrl, emc->regs + EMC_SEL_DPD_CTRL);
84373a7f0a9SMikko Perttunen 	emc_seq_update_timing(emc);
84473a7f0a9SMikko Perttunen 
84573a7f0a9SMikko Perttunen 	emc->last_timing = *timing;
84673a7f0a9SMikko Perttunen }
84773a7f0a9SMikko Perttunen 
84873a7f0a9SMikko Perttunen /* Initialization and deinitialization */
84973a7f0a9SMikko Perttunen 
85073a7f0a9SMikko Perttunen static void emc_read_current_timing(struct tegra_emc *emc,
85173a7f0a9SMikko Perttunen 				    struct emc_timing *timing)
85273a7f0a9SMikko Perttunen {
85373a7f0a9SMikko Perttunen 	unsigned int i;
85473a7f0a9SMikko Perttunen 
85573a7f0a9SMikko Perttunen 	for (i = 0; i < ARRAY_SIZE(emc_burst_regs); ++i)
85673a7f0a9SMikko Perttunen 		timing->emc_burst_data[i] =
85773a7f0a9SMikko Perttunen 			readl(emc->regs + emc_burst_regs[i]);
85873a7f0a9SMikko Perttunen 
85973a7f0a9SMikko Perttunen 	timing->emc_cfg = readl(emc->regs + EMC_CFG);
86073a7f0a9SMikko Perttunen 
86173a7f0a9SMikko Perttunen 	timing->emc_auto_cal_interval = 0;
86273a7f0a9SMikko Perttunen 	timing->emc_zcal_cnt_long = 0;
86373a7f0a9SMikko Perttunen 	timing->emc_mode_1 = 0;
86473a7f0a9SMikko Perttunen 	timing->emc_mode_2 = 0;
86573a7f0a9SMikko Perttunen 	timing->emc_mode_4 = 0;
86673a7f0a9SMikko Perttunen 	timing->emc_mode_reset = 0;
86773a7f0a9SMikko Perttunen }
86873a7f0a9SMikko Perttunen 
86973a7f0a9SMikko Perttunen static int emc_init(struct tegra_emc *emc)
87073a7f0a9SMikko Perttunen {
87173a7f0a9SMikko Perttunen 	emc->dram_type = readl(emc->regs + EMC_FBIO_CFG5);
87273a7f0a9SMikko Perttunen 	emc->dram_type &= EMC_FBIO_CFG5_DRAM_TYPE_MASK;
87373a7f0a9SMikko Perttunen 	emc->dram_type >>= EMC_FBIO_CFG5_DRAM_TYPE_SHIFT;
87473a7f0a9SMikko Perttunen 
87573a7f0a9SMikko Perttunen 	emc->dram_num = tegra_mc_get_emem_device_count(emc->mc);
87673a7f0a9SMikko Perttunen 
87773a7f0a9SMikko Perttunen 	emc_read_current_timing(emc, &emc->last_timing);
87873a7f0a9SMikko Perttunen 
87973a7f0a9SMikko Perttunen 	return 0;
88073a7f0a9SMikko Perttunen }
88173a7f0a9SMikko Perttunen 
88273a7f0a9SMikko Perttunen static int load_one_timing_from_dt(struct tegra_emc *emc,
88373a7f0a9SMikko Perttunen 				   struct emc_timing *timing,
88473a7f0a9SMikko Perttunen 				   struct device_node *node)
88573a7f0a9SMikko Perttunen {
88673a7f0a9SMikko Perttunen 	u32 value;
88773a7f0a9SMikko Perttunen 	int err;
88873a7f0a9SMikko Perttunen 
88973a7f0a9SMikko Perttunen 	err = of_property_read_u32(node, "clock-frequency", &value);
89073a7f0a9SMikko Perttunen 	if (err) {
891c86f9854SRob Herring 		dev_err(emc->dev, "timing %pOFn: failed to read rate: %d\n",
892c86f9854SRob Herring 			node, err);
89373a7f0a9SMikko Perttunen 		return err;
89473a7f0a9SMikko Perttunen 	}
89573a7f0a9SMikko Perttunen 
89673a7f0a9SMikko Perttunen 	timing->rate = value;
89773a7f0a9SMikko Perttunen 
89873a7f0a9SMikko Perttunen 	err = of_property_read_u32_array(node, "nvidia,emc-configuration",
89973a7f0a9SMikko Perttunen 					 timing->emc_burst_data,
90073a7f0a9SMikko Perttunen 					 ARRAY_SIZE(timing->emc_burst_data));
90173a7f0a9SMikko Perttunen 	if (err) {
90273a7f0a9SMikko Perttunen 		dev_err(emc->dev,
903c86f9854SRob Herring 			"timing %pOFn: failed to read emc burst data: %d\n",
904c86f9854SRob Herring 			node, err);
90573a7f0a9SMikko Perttunen 		return err;
90673a7f0a9SMikko Perttunen 	}
90773a7f0a9SMikko Perttunen 
90873a7f0a9SMikko Perttunen #define EMC_READ_PROP(prop, dtprop) { \
90973a7f0a9SMikko Perttunen 	err = of_property_read_u32(node, dtprop, &timing->prop); \
91073a7f0a9SMikko Perttunen 	if (err) { \
911c86f9854SRob Herring 		dev_err(emc->dev, "timing %pOFn: failed to read " #prop ": %d\n", \
912c86f9854SRob Herring 			node, err); \
91373a7f0a9SMikko Perttunen 		return err; \
91473a7f0a9SMikko Perttunen 	} \
91573a7f0a9SMikko Perttunen }
91673a7f0a9SMikko Perttunen 
91773a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_auto_cal_config, "nvidia,emc-auto-cal-config")
91873a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_auto_cal_config2, "nvidia,emc-auto-cal-config2")
91973a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_auto_cal_config3, "nvidia,emc-auto-cal-config3")
92073a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_auto_cal_interval, "nvidia,emc-auto-cal-interval")
92173a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_bgbias_ctl0, "nvidia,emc-bgbias-ctl0")
92273a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_cfg, "nvidia,emc-cfg")
92373a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_cfg_2, "nvidia,emc-cfg-2")
92473a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_ctt_term_ctrl, "nvidia,emc-ctt-term-ctrl")
92573a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_mode_1, "nvidia,emc-mode-1")
92673a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_mode_2, "nvidia,emc-mode-2")
92773a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_mode_4, "nvidia,emc-mode-4")
92873a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_mode_reset, "nvidia,emc-mode-reset")
92973a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_mrs_wait_cnt, "nvidia,emc-mrs-wait-cnt")
93073a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_sel_dpd_ctrl, "nvidia,emc-sel-dpd-ctrl")
93173a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_xm2dqspadctrl2, "nvidia,emc-xm2dqspadctrl2")
93273a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_zcal_cnt_long, "nvidia,emc-zcal-cnt-long")
93373a7f0a9SMikko Perttunen 	EMC_READ_PROP(emc_zcal_interval, "nvidia,emc-zcal-interval")
93473a7f0a9SMikko Perttunen 
93573a7f0a9SMikko Perttunen #undef EMC_READ_PROP
93673a7f0a9SMikko Perttunen 
93773a7f0a9SMikko Perttunen 	return 0;
93873a7f0a9SMikko Perttunen }
93973a7f0a9SMikko Perttunen 
94073a7f0a9SMikko Perttunen static int cmp_timings(const void *_a, const void *_b)
94173a7f0a9SMikko Perttunen {
94273a7f0a9SMikko Perttunen 	const struct emc_timing *a = _a;
94373a7f0a9SMikko Perttunen 	const struct emc_timing *b = _b;
94473a7f0a9SMikko Perttunen 
94573a7f0a9SMikko Perttunen 	if (a->rate < b->rate)
94673a7f0a9SMikko Perttunen 		return -1;
94773a7f0a9SMikko Perttunen 	else if (a->rate == b->rate)
94873a7f0a9SMikko Perttunen 		return 0;
94973a7f0a9SMikko Perttunen 	else
95073a7f0a9SMikko Perttunen 		return 1;
95173a7f0a9SMikko Perttunen }
95273a7f0a9SMikko Perttunen 
95373a7f0a9SMikko Perttunen static int tegra_emc_load_timings_from_dt(struct tegra_emc *emc,
95473a7f0a9SMikko Perttunen 					  struct device_node *node)
95573a7f0a9SMikko Perttunen {
95673a7f0a9SMikko Perttunen 	int child_count = of_get_child_count(node);
95773a7f0a9SMikko Perttunen 	struct device_node *child;
95873a7f0a9SMikko Perttunen 	struct emc_timing *timing;
95973a7f0a9SMikko Perttunen 	unsigned int i = 0;
96073a7f0a9SMikko Perttunen 	int err;
96173a7f0a9SMikko Perttunen 
96273a7f0a9SMikko Perttunen 	emc->timings = devm_kcalloc(emc->dev, child_count, sizeof(*timing),
96373a7f0a9SMikko Perttunen 				    GFP_KERNEL);
96473a7f0a9SMikko Perttunen 	if (!emc->timings)
96573a7f0a9SMikko Perttunen 		return -ENOMEM;
96673a7f0a9SMikko Perttunen 
96773a7f0a9SMikko Perttunen 	emc->num_timings = child_count;
96873a7f0a9SMikko Perttunen 
96973a7f0a9SMikko Perttunen 	for_each_child_of_node(node, child) {
97073a7f0a9SMikko Perttunen 		timing = &emc->timings[i++];
97173a7f0a9SMikko Perttunen 
97273a7f0a9SMikko Perttunen 		err = load_one_timing_from_dt(emc, timing, child);
973aafb197fSAmitoj Kaur Chawla 		if (err) {
974aafb197fSAmitoj Kaur Chawla 			of_node_put(child);
97573a7f0a9SMikko Perttunen 			return err;
97673a7f0a9SMikko Perttunen 		}
977aafb197fSAmitoj Kaur Chawla 	}
97873a7f0a9SMikko Perttunen 
97973a7f0a9SMikko Perttunen 	sort(emc->timings, emc->num_timings, sizeof(*timing), cmp_timings,
98073a7f0a9SMikko Perttunen 	     NULL);
98173a7f0a9SMikko Perttunen 
98273a7f0a9SMikko Perttunen 	return 0;
98373a7f0a9SMikko Perttunen }
98473a7f0a9SMikko Perttunen 
98573a7f0a9SMikko Perttunen static const struct of_device_id tegra_emc_of_match[] = {
98673a7f0a9SMikko Perttunen 	{ .compatible = "nvidia,tegra124-emc" },
98773a7f0a9SMikko Perttunen 	{}
98873a7f0a9SMikko Perttunen };
98973a7f0a9SMikko Perttunen 
99073a7f0a9SMikko Perttunen static struct device_node *
99173a7f0a9SMikko Perttunen tegra_emc_find_node_by_ram_code(struct device_node *node, u32 ram_code)
99273a7f0a9SMikko Perttunen {
99373a7f0a9SMikko Perttunen 	struct device_node *np;
99473a7f0a9SMikko Perttunen 	int err;
99573a7f0a9SMikko Perttunen 
99673a7f0a9SMikko Perttunen 	for_each_child_of_node(node, np) {
99773a7f0a9SMikko Perttunen 		u32 value;
99873a7f0a9SMikko Perttunen 
99973a7f0a9SMikko Perttunen 		err = of_property_read_u32(np, "nvidia,ram-code", &value);
1000d1122e4bSJulia Lawall 		if (err || (value != ram_code))
100173a7f0a9SMikko Perttunen 			continue;
100273a7f0a9SMikko Perttunen 
100373a7f0a9SMikko Perttunen 		return np;
100473a7f0a9SMikko Perttunen 	}
100573a7f0a9SMikko Perttunen 
100673a7f0a9SMikko Perttunen 	return NULL;
100773a7f0a9SMikko Perttunen }
100873a7f0a9SMikko Perttunen 
10096b9acd93SThierry Reding /*
10106b9acd93SThierry Reding  * debugfs interface
10116b9acd93SThierry Reding  *
10126b9acd93SThierry Reding  * The memory controller driver exposes some files in debugfs that can be used
10136b9acd93SThierry Reding  * to control the EMC frequency. The top-level directory can be found here:
10146b9acd93SThierry Reding  *
10156b9acd93SThierry Reding  *   /sys/kernel/debug/emc
10166b9acd93SThierry Reding  *
10176b9acd93SThierry Reding  * It contains the following files:
10186b9acd93SThierry Reding  *
10196b9acd93SThierry Reding  *   - available_rates: This file contains a list of valid, space-separated
10206b9acd93SThierry Reding  *     EMC frequencies.
10216b9acd93SThierry Reding  *
10226b9acd93SThierry Reding  *   - min_rate: Writing a value to this file sets the given frequency as the
10236b9acd93SThierry Reding  *       floor of the permitted range. If this is higher than the currently
10246b9acd93SThierry Reding  *       configured EMC frequency, this will cause the frequency to be
10256b9acd93SThierry Reding  *       increased so that it stays within the valid range.
10266b9acd93SThierry Reding  *
10276b9acd93SThierry Reding  *   - max_rate: Similarily to the min_rate file, writing a value to this file
10286b9acd93SThierry Reding  *       sets the given frequency as the ceiling of the permitted range. If
10296b9acd93SThierry Reding  *       the value is lower than the currently configured EMC frequency, this
10306b9acd93SThierry Reding  *       will cause the frequency to be decreased so that it stays within the
10316b9acd93SThierry Reding  *       valid range.
10326b9acd93SThierry Reding  */
10339c77a81fSMikko Perttunen 
10346b9acd93SThierry Reding static bool tegra_emc_validate_rate(struct tegra_emc *emc, unsigned long rate)
10359c77a81fSMikko Perttunen {
10366b9acd93SThierry Reding 	unsigned int i;
10379c77a81fSMikko Perttunen 
10386b9acd93SThierry Reding 	for (i = 0; i < emc->num_timings; i++)
10396b9acd93SThierry Reding 		if (rate == emc->timings[i].rate)
10406b9acd93SThierry Reding 			return true;
10419c77a81fSMikko Perttunen 
10426b9acd93SThierry Reding 	return false;
10439c77a81fSMikko Perttunen }
10449c77a81fSMikko Perttunen 
10456b9acd93SThierry Reding static int tegra_emc_debug_available_rates_show(struct seq_file *s,
10466b9acd93SThierry Reding 						void *data)
104730a636f9SThierry Reding {
104830a636f9SThierry Reding 	struct tegra_emc *emc = s->private;
104930a636f9SThierry Reding 	const char *prefix = "";
105030a636f9SThierry Reding 	unsigned int i;
105130a636f9SThierry Reding 
105230a636f9SThierry Reding 	for (i = 0; i < emc->num_timings; i++) {
10536b9acd93SThierry Reding 		seq_printf(s, "%s%lu", prefix, emc->timings[i].rate);
105430a636f9SThierry Reding 		prefix = " ";
105530a636f9SThierry Reding 	}
105630a636f9SThierry Reding 
105730a636f9SThierry Reding 	seq_puts(s, "\n");
105830a636f9SThierry Reding 
105930a636f9SThierry Reding 	return 0;
106030a636f9SThierry Reding }
106130a636f9SThierry Reding 
10626b9acd93SThierry Reding static int tegra_emc_debug_available_rates_open(struct inode *inode,
106330a636f9SThierry Reding 						struct file *file)
106430a636f9SThierry Reding {
10656b9acd93SThierry Reding 	return single_open(file, tegra_emc_debug_available_rates_show,
106630a636f9SThierry Reding 			   inode->i_private);
106730a636f9SThierry Reding }
106830a636f9SThierry Reding 
10696b9acd93SThierry Reding static const struct file_operations tegra_emc_debug_available_rates_fops = {
10706b9acd93SThierry Reding 	.open = tegra_emc_debug_available_rates_open,
107130a636f9SThierry Reding 	.read = seq_read,
107230a636f9SThierry Reding 	.llseek = seq_lseek,
107330a636f9SThierry Reding 	.release = single_release,
107430a636f9SThierry Reding };
107530a636f9SThierry Reding 
10766b9acd93SThierry Reding static int tegra_emc_debug_min_rate_get(void *data, u64 *rate)
10776b9acd93SThierry Reding {
10786b9acd93SThierry Reding 	struct tegra_emc *emc = data;
10796b9acd93SThierry Reding 
10806b9acd93SThierry Reding 	*rate = emc->debugfs.min_rate;
10816b9acd93SThierry Reding 
10826b9acd93SThierry Reding 	return 0;
10836b9acd93SThierry Reding }
10846b9acd93SThierry Reding 
10856b9acd93SThierry Reding static int tegra_emc_debug_min_rate_set(void *data, u64 rate)
10866b9acd93SThierry Reding {
10876b9acd93SThierry Reding 	struct tegra_emc *emc = data;
10886b9acd93SThierry Reding 	int err;
10896b9acd93SThierry Reding 
10906b9acd93SThierry Reding 	if (!tegra_emc_validate_rate(emc, rate))
10916b9acd93SThierry Reding 		return -EINVAL;
10926b9acd93SThierry Reding 
10936b9acd93SThierry Reding 	err = clk_set_min_rate(emc->clk, rate);
10946b9acd93SThierry Reding 	if (err < 0)
10956b9acd93SThierry Reding 		return err;
10966b9acd93SThierry Reding 
10976b9acd93SThierry Reding 	emc->debugfs.min_rate = rate;
10986b9acd93SThierry Reding 
10996b9acd93SThierry Reding 	return 0;
11006b9acd93SThierry Reding }
11016b9acd93SThierry Reding 
11026b9acd93SThierry Reding DEFINE_SIMPLE_ATTRIBUTE(tegra_emc_debug_min_rate_fops,
11036b9acd93SThierry Reding 			tegra_emc_debug_min_rate_get,
11046b9acd93SThierry Reding 			tegra_emc_debug_min_rate_set, "%llu\n");
11056b9acd93SThierry Reding 
11066b9acd93SThierry Reding static int tegra_emc_debug_max_rate_get(void *data, u64 *rate)
11076b9acd93SThierry Reding {
11086b9acd93SThierry Reding 	struct tegra_emc *emc = data;
11096b9acd93SThierry Reding 
11106b9acd93SThierry Reding 	*rate = emc->debugfs.max_rate;
11116b9acd93SThierry Reding 
11126b9acd93SThierry Reding 	return 0;
11136b9acd93SThierry Reding }
11146b9acd93SThierry Reding 
11156b9acd93SThierry Reding static int tegra_emc_debug_max_rate_set(void *data, u64 rate)
11166b9acd93SThierry Reding {
11176b9acd93SThierry Reding 	struct tegra_emc *emc = data;
11186b9acd93SThierry Reding 	int err;
11196b9acd93SThierry Reding 
11206b9acd93SThierry Reding 	if (!tegra_emc_validate_rate(emc, rate))
11216b9acd93SThierry Reding 		return -EINVAL;
11226b9acd93SThierry Reding 
11236b9acd93SThierry Reding 	err = clk_set_max_rate(emc->clk, rate);
11246b9acd93SThierry Reding 	if (err < 0)
11256b9acd93SThierry Reding 		return err;
11266b9acd93SThierry Reding 
11276b9acd93SThierry Reding 	emc->debugfs.max_rate = rate;
11286b9acd93SThierry Reding 
11296b9acd93SThierry Reding 	return 0;
11306b9acd93SThierry Reding }
11316b9acd93SThierry Reding 
11326b9acd93SThierry Reding DEFINE_SIMPLE_ATTRIBUTE(tegra_emc_debug_max_rate_fops,
11336b9acd93SThierry Reding 			tegra_emc_debug_max_rate_get,
11346b9acd93SThierry Reding 			tegra_emc_debug_max_rate_set, "%llu\n");
11356b9acd93SThierry Reding 
113630a636f9SThierry Reding static void emc_debugfs_init(struct device *dev, struct tegra_emc *emc)
11379c77a81fSMikko Perttunen {
11386b9acd93SThierry Reding 	unsigned int i;
11396b9acd93SThierry Reding 	int err;
11409c77a81fSMikko Perttunen 
11416b9acd93SThierry Reding 	emc->clk = devm_clk_get(dev, "emc");
11426b9acd93SThierry Reding 	if (IS_ERR(emc->clk)) {
11436b9acd93SThierry Reding 		if (PTR_ERR(emc->clk) != -ENODEV) {
11446b9acd93SThierry Reding 			dev_err(dev, "failed to get EMC clock: %ld\n",
11456b9acd93SThierry Reding 				PTR_ERR(emc->clk));
11466b9acd93SThierry Reding 			return;
11476b9acd93SThierry Reding 		}
11486b9acd93SThierry Reding 	}
11496b9acd93SThierry Reding 
11506b9acd93SThierry Reding 	emc->debugfs.min_rate = ULONG_MAX;
11516b9acd93SThierry Reding 	emc->debugfs.max_rate = 0;
11526b9acd93SThierry Reding 
11536b9acd93SThierry Reding 	for (i = 0; i < emc->num_timings; i++) {
11546b9acd93SThierry Reding 		if (emc->timings[i].rate < emc->debugfs.min_rate)
11556b9acd93SThierry Reding 			emc->debugfs.min_rate = emc->timings[i].rate;
11566b9acd93SThierry Reding 
11576b9acd93SThierry Reding 		if (emc->timings[i].rate > emc->debugfs.max_rate)
11586b9acd93SThierry Reding 			emc->debugfs.max_rate = emc->timings[i].rate;
11596b9acd93SThierry Reding 	}
11606b9acd93SThierry Reding 
1161*141267bfSDmitry Osipenko 	if (!emc->num_timings) {
1162*141267bfSDmitry Osipenko 		emc->debugfs.min_rate = clk_get_rate(emc->clk);
1163*141267bfSDmitry Osipenko 		emc->debugfs.max_rate = emc->debugfs.min_rate;
1164*141267bfSDmitry Osipenko 	}
1165*141267bfSDmitry Osipenko 
11666b9acd93SThierry Reding 	err = clk_set_rate_range(emc->clk, emc->debugfs.min_rate,
11676b9acd93SThierry Reding 				 emc->debugfs.max_rate);
11686b9acd93SThierry Reding 	if (err < 0) {
11696b9acd93SThierry Reding 		dev_err(dev, "failed to set rate range [%lu-%lu] for %pC\n",
11706b9acd93SThierry Reding 			emc->debugfs.min_rate, emc->debugfs.max_rate,
11716b9acd93SThierry Reding 			emc->clk);
11726b9acd93SThierry Reding 		return;
11736b9acd93SThierry Reding 	}
11746b9acd93SThierry Reding 
11756b9acd93SThierry Reding 	emc->debugfs.root = debugfs_create_dir("emc", NULL);
11766b9acd93SThierry Reding 	if (!emc->debugfs.root) {
11779c77a81fSMikko Perttunen 		dev_err(dev, "failed to create debugfs directory\n");
11789c77a81fSMikko Perttunen 		return;
11799c77a81fSMikko Perttunen 	}
11809c77a81fSMikko Perttunen 
11816b9acd93SThierry Reding 	debugfs_create_file("available_rates", S_IRUGO, emc->debugfs.root, emc,
11826b9acd93SThierry Reding 			    &tegra_emc_debug_available_rates_fops);
11836b9acd93SThierry Reding 	debugfs_create_file("min_rate", S_IRUGO | S_IWUSR, emc->debugfs.root,
11846b9acd93SThierry Reding 			    emc, &tegra_emc_debug_min_rate_fops);
11856b9acd93SThierry Reding 	debugfs_create_file("max_rate", S_IRUGO | S_IWUSR, emc->debugfs.root,
11866b9acd93SThierry Reding 			    emc, &tegra_emc_debug_max_rate_fops);
11879c77a81fSMikko Perttunen }
11889c77a81fSMikko Perttunen 
118973a7f0a9SMikko Perttunen static int tegra_emc_probe(struct platform_device *pdev)
119073a7f0a9SMikko Perttunen {
119173a7f0a9SMikko Perttunen 	struct platform_device *mc;
119273a7f0a9SMikko Perttunen 	struct device_node *np;
119373a7f0a9SMikko Perttunen 	struct tegra_emc *emc;
119473a7f0a9SMikko Perttunen 	struct resource *res;
119573a7f0a9SMikko Perttunen 	u32 ram_code;
119673a7f0a9SMikko Perttunen 	int err;
119773a7f0a9SMikko Perttunen 
119873a7f0a9SMikko Perttunen 	emc = devm_kzalloc(&pdev->dev, sizeof(*emc), GFP_KERNEL);
119973a7f0a9SMikko Perttunen 	if (!emc)
120073a7f0a9SMikko Perttunen 		return -ENOMEM;
120173a7f0a9SMikko Perttunen 
120273a7f0a9SMikko Perttunen 	emc->dev = &pdev->dev;
120373a7f0a9SMikko Perttunen 
120473a7f0a9SMikko Perttunen 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
120573a7f0a9SMikko Perttunen 	emc->regs = devm_ioremap_resource(&pdev->dev, res);
120673a7f0a9SMikko Perttunen 	if (IS_ERR(emc->regs))
120773a7f0a9SMikko Perttunen 		return PTR_ERR(emc->regs);
120873a7f0a9SMikko Perttunen 
120973a7f0a9SMikko Perttunen 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,memory-controller", 0);
121073a7f0a9SMikko Perttunen 	if (!np) {
121173a7f0a9SMikko Perttunen 		dev_err(&pdev->dev, "could not get memory controller\n");
121273a7f0a9SMikko Perttunen 		return -ENOENT;
121373a7f0a9SMikko Perttunen 	}
121473a7f0a9SMikko Perttunen 
121573a7f0a9SMikko Perttunen 	mc = of_find_device_by_node(np);
1216b92f4380SChristophe Jaillet 	of_node_put(np);
121773a7f0a9SMikko Perttunen 	if (!mc)
121873a7f0a9SMikko Perttunen 		return -ENOENT;
121973a7f0a9SMikko Perttunen 
122073a7f0a9SMikko Perttunen 	emc->mc = platform_get_drvdata(mc);
122173a7f0a9SMikko Perttunen 	if (!emc->mc)
122273a7f0a9SMikko Perttunen 		return -EPROBE_DEFER;
122373a7f0a9SMikko Perttunen 
122473a7f0a9SMikko Perttunen 	ram_code = tegra_read_ram_code();
122573a7f0a9SMikko Perttunen 
122673a7f0a9SMikko Perttunen 	np = tegra_emc_find_node_by_ram_code(pdev->dev.of_node, ram_code);
122773a7f0a9SMikko Perttunen 	if (!np) {
122873a7f0a9SMikko Perttunen 		dev_err(&pdev->dev,
122973a7f0a9SMikko Perttunen 			"no memory timings for RAM code %u found in DT\n",
123073a7f0a9SMikko Perttunen 			ram_code);
123173a7f0a9SMikko Perttunen 		return -ENOENT;
123273a7f0a9SMikko Perttunen 	}
123373a7f0a9SMikko Perttunen 
123473a7f0a9SMikko Perttunen 	err = tegra_emc_load_timings_from_dt(emc, np);
123573a7f0a9SMikko Perttunen 	of_node_put(np);
123673a7f0a9SMikko Perttunen 	if (err)
123773a7f0a9SMikko Perttunen 		return err;
123873a7f0a9SMikko Perttunen 
123973a7f0a9SMikko Perttunen 	if (emc->num_timings == 0) {
124073a7f0a9SMikko Perttunen 		dev_err(&pdev->dev,
124173a7f0a9SMikko Perttunen 			"no memory timings for RAM code %u registered\n",
124273a7f0a9SMikko Perttunen 			ram_code);
124373a7f0a9SMikko Perttunen 		return -ENOENT;
124473a7f0a9SMikko Perttunen 	}
124573a7f0a9SMikko Perttunen 
124673a7f0a9SMikko Perttunen 	err = emc_init(emc);
124773a7f0a9SMikko Perttunen 	if (err) {
124873a7f0a9SMikko Perttunen 		dev_err(&pdev->dev, "EMC initialization failed: %d\n", err);
124973a7f0a9SMikko Perttunen 		return err;
125073a7f0a9SMikko Perttunen 	}
125173a7f0a9SMikko Perttunen 
125273a7f0a9SMikko Perttunen 	platform_set_drvdata(pdev, emc);
125373a7f0a9SMikko Perttunen 
12549c77a81fSMikko Perttunen 	if (IS_ENABLED(CONFIG_DEBUG_FS))
125530a636f9SThierry Reding 		emc_debugfs_init(&pdev->dev, emc);
12569c77a81fSMikko Perttunen 
125773a7f0a9SMikko Perttunen 	return 0;
125873a7f0a9SMikko Perttunen };
125973a7f0a9SMikko Perttunen 
126073a7f0a9SMikko Perttunen static struct platform_driver tegra_emc_driver = {
126173a7f0a9SMikko Perttunen 	.probe = tegra_emc_probe,
126273a7f0a9SMikko Perttunen 	.driver = {
126373a7f0a9SMikko Perttunen 		.name = "tegra-emc",
126473a7f0a9SMikko Perttunen 		.of_match_table = tegra_emc_of_match,
126573a7f0a9SMikko Perttunen 		.suppress_bind_attrs = true,
126673a7f0a9SMikko Perttunen 	},
126773a7f0a9SMikko Perttunen };
126873a7f0a9SMikko Perttunen 
126973a7f0a9SMikko Perttunen static int tegra_emc_init(void)
127073a7f0a9SMikko Perttunen {
127173a7f0a9SMikko Perttunen 	return platform_driver_register(&tegra_emc_driver);
127273a7f0a9SMikko Perttunen }
127373a7f0a9SMikko Perttunen subsys_initcall(tegra_emc_init);
1274