1*9c92ab61SThomas 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 47073a7f0a9SMikko Perttunen enum emc_dram_type dram_type; 47173a7f0a9SMikko Perttunen unsigned int dram_num; 47273a7f0a9SMikko Perttunen 47373a7f0a9SMikko Perttunen struct emc_timing last_timing; 47473a7f0a9SMikko Perttunen struct emc_timing *timings; 47573a7f0a9SMikko Perttunen unsigned int num_timings; 47673a7f0a9SMikko Perttunen }; 47773a7f0a9SMikko Perttunen 47873a7f0a9SMikko Perttunen /* Timing change sequence functions */ 47973a7f0a9SMikko Perttunen 48073a7f0a9SMikko Perttunen static void emc_ccfifo_writel(struct tegra_emc *emc, u32 value, 48173a7f0a9SMikko Perttunen unsigned long offset) 48273a7f0a9SMikko Perttunen { 48373a7f0a9SMikko Perttunen writel(value, emc->regs + EMC_CCFIFO_DATA); 48473a7f0a9SMikko Perttunen writel(offset, emc->regs + EMC_CCFIFO_ADDR); 48573a7f0a9SMikko Perttunen } 48673a7f0a9SMikko Perttunen 48773a7f0a9SMikko Perttunen static void emc_seq_update_timing(struct tegra_emc *emc) 48873a7f0a9SMikko Perttunen { 48973a7f0a9SMikko Perttunen unsigned int i; 49073a7f0a9SMikko Perttunen u32 value; 49173a7f0a9SMikko Perttunen 49273a7f0a9SMikko Perttunen writel(1, emc->regs + EMC_TIMING_CONTROL); 49373a7f0a9SMikko Perttunen 49473a7f0a9SMikko Perttunen for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) { 49573a7f0a9SMikko Perttunen value = readl(emc->regs + EMC_STATUS); 49673a7f0a9SMikko Perttunen if ((value & EMC_STATUS_TIMING_UPDATE_STALLED) == 0) 49773a7f0a9SMikko Perttunen return; 49873a7f0a9SMikko Perttunen udelay(1); 49973a7f0a9SMikko Perttunen } 50073a7f0a9SMikko Perttunen 50173a7f0a9SMikko Perttunen dev_err(emc->dev, "timing update timed out\n"); 50273a7f0a9SMikko Perttunen } 50373a7f0a9SMikko Perttunen 50473a7f0a9SMikko Perttunen static void emc_seq_disable_auto_cal(struct tegra_emc *emc) 50573a7f0a9SMikko Perttunen { 50673a7f0a9SMikko Perttunen unsigned int i; 50773a7f0a9SMikko Perttunen u32 value; 50873a7f0a9SMikko Perttunen 50973a7f0a9SMikko Perttunen writel(0, emc->regs + EMC_AUTO_CAL_INTERVAL); 51073a7f0a9SMikko Perttunen 51173a7f0a9SMikko Perttunen for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) { 51273a7f0a9SMikko Perttunen value = readl(emc->regs + EMC_AUTO_CAL_STATUS); 51373a7f0a9SMikko Perttunen if ((value & EMC_AUTO_CAL_STATUS_ACTIVE) == 0) 51473a7f0a9SMikko Perttunen return; 51573a7f0a9SMikko Perttunen udelay(1); 51673a7f0a9SMikko Perttunen } 51773a7f0a9SMikko Perttunen 51873a7f0a9SMikko Perttunen dev_err(emc->dev, "auto cal disable timed out\n"); 51973a7f0a9SMikko Perttunen } 52073a7f0a9SMikko Perttunen 52173a7f0a9SMikko Perttunen static void emc_seq_wait_clkchange(struct tegra_emc *emc) 52273a7f0a9SMikko Perttunen { 52373a7f0a9SMikko Perttunen unsigned int i; 52473a7f0a9SMikko Perttunen u32 value; 52573a7f0a9SMikko Perttunen 52673a7f0a9SMikko Perttunen for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) { 52773a7f0a9SMikko Perttunen value = readl(emc->regs + EMC_INTSTATUS); 52873a7f0a9SMikko Perttunen if (value & EMC_INTSTATUS_CLKCHANGE_COMPLETE) 52973a7f0a9SMikko Perttunen return; 53073a7f0a9SMikko Perttunen udelay(1); 53173a7f0a9SMikko Perttunen } 53273a7f0a9SMikko Perttunen 53373a7f0a9SMikko Perttunen dev_err(emc->dev, "clock change timed out\n"); 53473a7f0a9SMikko Perttunen } 53573a7f0a9SMikko Perttunen 53673a7f0a9SMikko Perttunen static struct emc_timing *tegra_emc_find_timing(struct tegra_emc *emc, 53773a7f0a9SMikko Perttunen unsigned long rate) 53873a7f0a9SMikko Perttunen { 53973a7f0a9SMikko Perttunen struct emc_timing *timing = NULL; 54073a7f0a9SMikko Perttunen unsigned int i; 54173a7f0a9SMikko Perttunen 54273a7f0a9SMikko Perttunen for (i = 0; i < emc->num_timings; i++) { 54373a7f0a9SMikko Perttunen if (emc->timings[i].rate == rate) { 54473a7f0a9SMikko Perttunen timing = &emc->timings[i]; 54573a7f0a9SMikko Perttunen break; 54673a7f0a9SMikko Perttunen } 54773a7f0a9SMikko Perttunen } 54873a7f0a9SMikko Perttunen 54973a7f0a9SMikko Perttunen if (!timing) { 55073a7f0a9SMikko Perttunen dev_err(emc->dev, "no timing for rate %lu\n", rate); 55173a7f0a9SMikko Perttunen return NULL; 55273a7f0a9SMikko Perttunen } 55373a7f0a9SMikko Perttunen 55473a7f0a9SMikko Perttunen return timing; 55573a7f0a9SMikko Perttunen } 55673a7f0a9SMikko Perttunen 55773a7f0a9SMikko Perttunen int tegra_emc_prepare_timing_change(struct tegra_emc *emc, 55873a7f0a9SMikko Perttunen unsigned long rate) 55973a7f0a9SMikko Perttunen { 56073a7f0a9SMikko Perttunen struct emc_timing *timing = tegra_emc_find_timing(emc, rate); 56173a7f0a9SMikko Perttunen struct emc_timing *last = &emc->last_timing; 56273a7f0a9SMikko Perttunen enum emc_dll_change dll_change; 56373a7f0a9SMikko Perttunen unsigned int pre_wait = 0; 56473a7f0a9SMikko Perttunen u32 val, val2, mask; 56573a7f0a9SMikko Perttunen bool update = false; 56673a7f0a9SMikko Perttunen unsigned int i; 56773a7f0a9SMikko Perttunen 56873a7f0a9SMikko Perttunen if (!timing) 56973a7f0a9SMikko Perttunen return -ENOENT; 57073a7f0a9SMikko Perttunen 57173a7f0a9SMikko Perttunen if ((last->emc_mode_1 & 0x1) == (timing->emc_mode_1 & 0x1)) 57273a7f0a9SMikko Perttunen dll_change = DLL_CHANGE_NONE; 57373a7f0a9SMikko Perttunen else if (timing->emc_mode_1 & 0x1) 57473a7f0a9SMikko Perttunen dll_change = DLL_CHANGE_ON; 57573a7f0a9SMikko Perttunen else 57673a7f0a9SMikko Perttunen dll_change = DLL_CHANGE_OFF; 57773a7f0a9SMikko Perttunen 57873a7f0a9SMikko Perttunen /* Clear CLKCHANGE_COMPLETE interrupts */ 57973a7f0a9SMikko Perttunen writel(EMC_INTSTATUS_CLKCHANGE_COMPLETE, emc->regs + EMC_INTSTATUS); 58073a7f0a9SMikko Perttunen 58173a7f0a9SMikko Perttunen /* Disable dynamic self-refresh */ 58273a7f0a9SMikko Perttunen val = readl(emc->regs + EMC_CFG); 58373a7f0a9SMikko Perttunen if (val & EMC_CFG_PWR_MASK) { 58473a7f0a9SMikko Perttunen val &= ~EMC_CFG_POWER_FEATURES_MASK; 58573a7f0a9SMikko Perttunen writel(val, emc->regs + EMC_CFG); 58673a7f0a9SMikko Perttunen 58773a7f0a9SMikko Perttunen pre_wait = 5; 58873a7f0a9SMikko Perttunen } 58973a7f0a9SMikko Perttunen 59073a7f0a9SMikko Perttunen /* Disable SEL_DPD_CTRL for clock change */ 59173a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3) 59273a7f0a9SMikko Perttunen mask = EMC_SEL_DPD_CTRL_DDR3_MASK; 59373a7f0a9SMikko Perttunen else 59473a7f0a9SMikko Perttunen mask = EMC_SEL_DPD_CTRL_MASK; 59573a7f0a9SMikko Perttunen 59673a7f0a9SMikko Perttunen val = readl(emc->regs + EMC_SEL_DPD_CTRL); 59773a7f0a9SMikko Perttunen if (val & mask) { 59873a7f0a9SMikko Perttunen val &= ~mask; 59973a7f0a9SMikko Perttunen writel(val, emc->regs + EMC_SEL_DPD_CTRL); 60073a7f0a9SMikko Perttunen } 60173a7f0a9SMikko Perttunen 60273a7f0a9SMikko Perttunen /* Prepare DQ/DQS for clock change */ 60373a7f0a9SMikko Perttunen val = readl(emc->regs + EMC_BGBIAS_CTL0); 60473a7f0a9SMikko Perttunen val2 = last->emc_bgbias_ctl0; 60573a7f0a9SMikko Perttunen if (!(timing->emc_bgbias_ctl0 & 60673a7f0a9SMikko Perttunen EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) && 60773a7f0a9SMikko Perttunen (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX)) { 60873a7f0a9SMikko Perttunen val2 &= ~EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX; 60973a7f0a9SMikko Perttunen update = true; 61073a7f0a9SMikko Perttunen } 61173a7f0a9SMikko Perttunen 61273a7f0a9SMikko Perttunen if ((val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD) || 61373a7f0a9SMikko Perttunen (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN)) { 61473a7f0a9SMikko Perttunen update = true; 61573a7f0a9SMikko Perttunen } 61673a7f0a9SMikko Perttunen 61773a7f0a9SMikko Perttunen if (update) { 61873a7f0a9SMikko Perttunen writel(val2, emc->regs + EMC_BGBIAS_CTL0); 61973a7f0a9SMikko Perttunen if (pre_wait < 5) 62073a7f0a9SMikko Perttunen pre_wait = 5; 62173a7f0a9SMikko Perttunen } 62273a7f0a9SMikko Perttunen 62373a7f0a9SMikko Perttunen update = false; 62473a7f0a9SMikko Perttunen val = readl(emc->regs + EMC_XM2DQSPADCTRL2); 62573a7f0a9SMikko Perttunen if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_VREF_ENABLE && 62673a7f0a9SMikko Perttunen !(val & EMC_XM2DQSPADCTRL2_VREF_ENABLE)) { 62773a7f0a9SMikko Perttunen val |= EMC_XM2DQSPADCTRL2_VREF_ENABLE; 62873a7f0a9SMikko Perttunen update = true; 62973a7f0a9SMikko Perttunen } 63073a7f0a9SMikko Perttunen 63173a7f0a9SMikko Perttunen if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE && 63273a7f0a9SMikko Perttunen !(val & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE)) { 63373a7f0a9SMikko Perttunen val |= EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE; 63473a7f0a9SMikko Perttunen update = true; 63573a7f0a9SMikko Perttunen } 63673a7f0a9SMikko Perttunen 63773a7f0a9SMikko Perttunen if (update) { 63873a7f0a9SMikko Perttunen writel(val, emc->regs + EMC_XM2DQSPADCTRL2); 63973a7f0a9SMikko Perttunen if (pre_wait < 30) 64073a7f0a9SMikko Perttunen pre_wait = 30; 64173a7f0a9SMikko Perttunen } 64273a7f0a9SMikko Perttunen 64373a7f0a9SMikko Perttunen /* Wait to settle */ 64473a7f0a9SMikko Perttunen if (pre_wait) { 64573a7f0a9SMikko Perttunen emc_seq_update_timing(emc); 64673a7f0a9SMikko Perttunen udelay(pre_wait); 64773a7f0a9SMikko Perttunen } 64873a7f0a9SMikko Perttunen 64973a7f0a9SMikko Perttunen /* Program CTT_TERM control */ 65073a7f0a9SMikko Perttunen if (last->emc_ctt_term_ctrl != timing->emc_ctt_term_ctrl) { 65173a7f0a9SMikko Perttunen emc_seq_disable_auto_cal(emc); 65273a7f0a9SMikko Perttunen writel(timing->emc_ctt_term_ctrl, 65373a7f0a9SMikko Perttunen emc->regs + EMC_CTT_TERM_CTRL); 65473a7f0a9SMikko Perttunen emc_seq_update_timing(emc); 65573a7f0a9SMikko Perttunen } 65673a7f0a9SMikko Perttunen 65773a7f0a9SMikko Perttunen /* Program burst shadow registers */ 65873a7f0a9SMikko Perttunen for (i = 0; i < ARRAY_SIZE(timing->emc_burst_data); ++i) 65973a7f0a9SMikko Perttunen writel(timing->emc_burst_data[i], 66073a7f0a9SMikko Perttunen emc->regs + emc_burst_regs[i]); 66173a7f0a9SMikko Perttunen 66273a7f0a9SMikko Perttunen writel(timing->emc_xm2dqspadctrl2, emc->regs + EMC_XM2DQSPADCTRL2); 66373a7f0a9SMikko Perttunen writel(timing->emc_zcal_interval, emc->regs + EMC_ZCAL_INTERVAL); 66473a7f0a9SMikko Perttunen 66573a7f0a9SMikko Perttunen tegra_mc_write_emem_configuration(emc->mc, timing->rate); 66673a7f0a9SMikko Perttunen 66773a7f0a9SMikko Perttunen val = timing->emc_cfg & ~EMC_CFG_POWER_FEATURES_MASK; 66873a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, val, EMC_CFG); 66973a7f0a9SMikko Perttunen 67073a7f0a9SMikko Perttunen /* Program AUTO_CAL_CONFIG */ 67173a7f0a9SMikko Perttunen if (timing->emc_auto_cal_config2 != last->emc_auto_cal_config2) 67273a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_auto_cal_config2, 67373a7f0a9SMikko Perttunen EMC_AUTO_CAL_CONFIG2); 67473a7f0a9SMikko Perttunen 67573a7f0a9SMikko Perttunen if (timing->emc_auto_cal_config3 != last->emc_auto_cal_config3) 67673a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_auto_cal_config3, 67773a7f0a9SMikko Perttunen EMC_AUTO_CAL_CONFIG3); 67873a7f0a9SMikko Perttunen 67973a7f0a9SMikko Perttunen if (timing->emc_auto_cal_config != last->emc_auto_cal_config) { 68073a7f0a9SMikko Perttunen val = timing->emc_auto_cal_config; 68173a7f0a9SMikko Perttunen val &= EMC_AUTO_CAL_CONFIG_AUTO_CAL_START; 68273a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, val, EMC_AUTO_CAL_CONFIG); 68373a7f0a9SMikko Perttunen } 68473a7f0a9SMikko Perttunen 68573a7f0a9SMikko Perttunen /* DDR3: predict MRS long wait count */ 68673a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3 && 68773a7f0a9SMikko Perttunen dll_change == DLL_CHANGE_ON) { 68873a7f0a9SMikko Perttunen u32 cnt = 512; 68973a7f0a9SMikko Perttunen 69073a7f0a9SMikko Perttunen if (timing->emc_zcal_interval != 0 && 69173a7f0a9SMikko Perttunen last->emc_zcal_interval == 0) 69273a7f0a9SMikko Perttunen cnt -= emc->dram_num * 256; 69373a7f0a9SMikko Perttunen 69473a7f0a9SMikko Perttunen val = (timing->emc_mrs_wait_cnt 69573a7f0a9SMikko Perttunen & EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK) 69673a7f0a9SMikko Perttunen >> EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT; 69773a7f0a9SMikko Perttunen if (cnt < val) 69873a7f0a9SMikko Perttunen cnt = val; 69973a7f0a9SMikko Perttunen 70073a7f0a9SMikko Perttunen val = timing->emc_mrs_wait_cnt 70173a7f0a9SMikko Perttunen & ~EMC_MRS_WAIT_CNT_LONG_WAIT_MASK; 70273a7f0a9SMikko Perttunen val |= (cnt << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT) 70373a7f0a9SMikko Perttunen & EMC_MRS_WAIT_CNT_LONG_WAIT_MASK; 70473a7f0a9SMikko Perttunen 70573a7f0a9SMikko Perttunen writel(val, emc->regs + EMC_MRS_WAIT_CNT); 70673a7f0a9SMikko Perttunen } 70773a7f0a9SMikko Perttunen 70873a7f0a9SMikko Perttunen val = timing->emc_cfg_2; 70973a7f0a9SMikko Perttunen val &= ~EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR; 71073a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, val, EMC_CFG_2); 71173a7f0a9SMikko Perttunen 71273a7f0a9SMikko Perttunen /* DDR3: Turn off DLL and enter self-refresh */ 71373a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3 && dll_change == DLL_CHANGE_OFF) 71473a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS); 71573a7f0a9SMikko Perttunen 71673a7f0a9SMikko Perttunen /* Disable refresh controller */ 71773a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num), 71873a7f0a9SMikko Perttunen EMC_REFCTRL); 71973a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3) 72073a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num) | 72173a7f0a9SMikko Perttunen EMC_SELF_REF_CMD_ENABLED, 72273a7f0a9SMikko Perttunen EMC_SELF_REF); 72373a7f0a9SMikko Perttunen 72473a7f0a9SMikko Perttunen /* Flow control marker */ 72573a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, 1, EMC_STALL_THEN_EXE_AFTER_CLKCHANGE); 72673a7f0a9SMikko Perttunen 72773a7f0a9SMikko Perttunen /* DDR3: Exit self-refresh */ 72873a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3) 72973a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num), 73073a7f0a9SMikko Perttunen EMC_SELF_REF); 73173a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num) | 73273a7f0a9SMikko Perttunen EMC_REFCTRL_ENABLE, 73373a7f0a9SMikko Perttunen EMC_REFCTRL); 73473a7f0a9SMikko Perttunen 73573a7f0a9SMikko Perttunen /* Set DRAM mode registers */ 73673a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3) { 73773a7f0a9SMikko Perttunen if (timing->emc_mode_1 != last->emc_mode_1) 73873a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS); 73973a7f0a9SMikko Perttunen if (timing->emc_mode_2 != last->emc_mode_2) 74073a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_EMRS2); 74173a7f0a9SMikko Perttunen 74273a7f0a9SMikko Perttunen if ((timing->emc_mode_reset != last->emc_mode_reset) || 74373a7f0a9SMikko Perttunen dll_change == DLL_CHANGE_ON) { 74473a7f0a9SMikko Perttunen val = timing->emc_mode_reset; 74573a7f0a9SMikko Perttunen if (dll_change == DLL_CHANGE_ON) { 74673a7f0a9SMikko Perttunen val |= EMC_MODE_SET_DLL_RESET; 74773a7f0a9SMikko Perttunen val |= EMC_MODE_SET_LONG_CNT; 74873a7f0a9SMikko Perttunen } else { 74973a7f0a9SMikko Perttunen val &= ~EMC_MODE_SET_DLL_RESET; 75073a7f0a9SMikko Perttunen } 75173a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, val, EMC_MRS); 75273a7f0a9SMikko Perttunen } 75373a7f0a9SMikko Perttunen } else { 75473a7f0a9SMikko Perttunen if (timing->emc_mode_2 != last->emc_mode_2) 75573a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_MRW2); 75673a7f0a9SMikko Perttunen if (timing->emc_mode_1 != last->emc_mode_1) 75773a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_MRW); 75873a7f0a9SMikko Perttunen if (timing->emc_mode_4 != last->emc_mode_4) 75973a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_mode_4, EMC_MRW4); 76073a7f0a9SMikko Perttunen } 76173a7f0a9SMikko Perttunen 76273a7f0a9SMikko Perttunen /* Issue ZCAL command if turning ZCAL on */ 76373a7f0a9SMikko Perttunen if (timing->emc_zcal_interval != 0 && last->emc_zcal_interval == 0) { 76473a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV0, EMC_ZQ_CAL); 76573a7f0a9SMikko Perttunen if (emc->dram_num > 1) 76673a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV1, 76773a7f0a9SMikko Perttunen EMC_ZQ_CAL); 76873a7f0a9SMikko Perttunen } 76973a7f0a9SMikko Perttunen 77073a7f0a9SMikko Perttunen /* Write to RO register to remove stall after change */ 77173a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, 0, EMC_CCFIFO_STATUS); 77273a7f0a9SMikko Perttunen 77373a7f0a9SMikko Perttunen if (timing->emc_cfg_2 & EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR) 77473a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_cfg_2, EMC_CFG_2); 77573a7f0a9SMikko Perttunen 77673a7f0a9SMikko Perttunen /* Disable AUTO_CAL for clock change */ 77773a7f0a9SMikko Perttunen emc_seq_disable_auto_cal(emc); 77873a7f0a9SMikko Perttunen 77973a7f0a9SMikko Perttunen /* Read register to wait until programming has settled */ 78073a7f0a9SMikko Perttunen readl(emc->regs + EMC_INTSTATUS); 78173a7f0a9SMikko Perttunen 78273a7f0a9SMikko Perttunen return 0; 78373a7f0a9SMikko Perttunen } 78473a7f0a9SMikko Perttunen 78573a7f0a9SMikko Perttunen void tegra_emc_complete_timing_change(struct tegra_emc *emc, 78673a7f0a9SMikko Perttunen unsigned long rate) 78773a7f0a9SMikko Perttunen { 78873a7f0a9SMikko Perttunen struct emc_timing *timing = tegra_emc_find_timing(emc, rate); 78973a7f0a9SMikko Perttunen struct emc_timing *last = &emc->last_timing; 79073a7f0a9SMikko Perttunen u32 val; 79173a7f0a9SMikko Perttunen 79273a7f0a9SMikko Perttunen if (!timing) 79373a7f0a9SMikko Perttunen return; 79473a7f0a9SMikko Perttunen 79573a7f0a9SMikko Perttunen /* Wait until the state machine has settled */ 79673a7f0a9SMikko Perttunen emc_seq_wait_clkchange(emc); 79773a7f0a9SMikko Perttunen 79873a7f0a9SMikko Perttunen /* Restore AUTO_CAL */ 79973a7f0a9SMikko Perttunen if (timing->emc_ctt_term_ctrl != last->emc_ctt_term_ctrl) 80073a7f0a9SMikko Perttunen writel(timing->emc_auto_cal_interval, 80173a7f0a9SMikko Perttunen emc->regs + EMC_AUTO_CAL_INTERVAL); 80273a7f0a9SMikko Perttunen 80373a7f0a9SMikko Perttunen /* Restore dynamic self-refresh */ 80473a7f0a9SMikko Perttunen if (timing->emc_cfg & EMC_CFG_PWR_MASK) 80573a7f0a9SMikko Perttunen writel(timing->emc_cfg, emc->regs + EMC_CFG); 80673a7f0a9SMikko Perttunen 80773a7f0a9SMikko Perttunen /* Set ZCAL wait count */ 80873a7f0a9SMikko Perttunen writel(timing->emc_zcal_cnt_long, emc->regs + EMC_ZCAL_WAIT_CNT); 80973a7f0a9SMikko Perttunen 81073a7f0a9SMikko Perttunen /* LPDDR3: Turn off BGBIAS if low frequency */ 81173a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_LPDDR3 && 81273a7f0a9SMikko Perttunen timing->emc_bgbias_ctl0 & 81373a7f0a9SMikko Perttunen EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) { 81473a7f0a9SMikko Perttunen val = timing->emc_bgbias_ctl0; 81573a7f0a9SMikko Perttunen val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN; 81673a7f0a9SMikko Perttunen val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD; 81773a7f0a9SMikko Perttunen writel(val, emc->regs + EMC_BGBIAS_CTL0); 81873a7f0a9SMikko Perttunen } else { 81973a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3 && 82073a7f0a9SMikko Perttunen readl(emc->regs + EMC_BGBIAS_CTL0) != 82173a7f0a9SMikko Perttunen timing->emc_bgbias_ctl0) { 82273a7f0a9SMikko Perttunen writel(timing->emc_bgbias_ctl0, 82373a7f0a9SMikko Perttunen emc->regs + EMC_BGBIAS_CTL0); 82473a7f0a9SMikko Perttunen } 82573a7f0a9SMikko Perttunen 82673a7f0a9SMikko Perttunen writel(timing->emc_auto_cal_interval, 82773a7f0a9SMikko Perttunen emc->regs + EMC_AUTO_CAL_INTERVAL); 82873a7f0a9SMikko Perttunen } 82973a7f0a9SMikko Perttunen 83073a7f0a9SMikko Perttunen /* Wait for timing to settle */ 83173a7f0a9SMikko Perttunen udelay(2); 83273a7f0a9SMikko Perttunen 83373a7f0a9SMikko Perttunen /* Reprogram SEL_DPD_CTRL */ 83473a7f0a9SMikko Perttunen writel(timing->emc_sel_dpd_ctrl, emc->regs + EMC_SEL_DPD_CTRL); 83573a7f0a9SMikko Perttunen emc_seq_update_timing(emc); 83673a7f0a9SMikko Perttunen 83773a7f0a9SMikko Perttunen emc->last_timing = *timing; 83873a7f0a9SMikko Perttunen } 83973a7f0a9SMikko Perttunen 84073a7f0a9SMikko Perttunen /* Initialization and deinitialization */ 84173a7f0a9SMikko Perttunen 84273a7f0a9SMikko Perttunen static void emc_read_current_timing(struct tegra_emc *emc, 84373a7f0a9SMikko Perttunen struct emc_timing *timing) 84473a7f0a9SMikko Perttunen { 84573a7f0a9SMikko Perttunen unsigned int i; 84673a7f0a9SMikko Perttunen 84773a7f0a9SMikko Perttunen for (i = 0; i < ARRAY_SIZE(emc_burst_regs); ++i) 84873a7f0a9SMikko Perttunen timing->emc_burst_data[i] = 84973a7f0a9SMikko Perttunen readl(emc->regs + emc_burst_regs[i]); 85073a7f0a9SMikko Perttunen 85173a7f0a9SMikko Perttunen timing->emc_cfg = readl(emc->regs + EMC_CFG); 85273a7f0a9SMikko Perttunen 85373a7f0a9SMikko Perttunen timing->emc_auto_cal_interval = 0; 85473a7f0a9SMikko Perttunen timing->emc_zcal_cnt_long = 0; 85573a7f0a9SMikko Perttunen timing->emc_mode_1 = 0; 85673a7f0a9SMikko Perttunen timing->emc_mode_2 = 0; 85773a7f0a9SMikko Perttunen timing->emc_mode_4 = 0; 85873a7f0a9SMikko Perttunen timing->emc_mode_reset = 0; 85973a7f0a9SMikko Perttunen } 86073a7f0a9SMikko Perttunen 86173a7f0a9SMikko Perttunen static int emc_init(struct tegra_emc *emc) 86273a7f0a9SMikko Perttunen { 86373a7f0a9SMikko Perttunen emc->dram_type = readl(emc->regs + EMC_FBIO_CFG5); 86473a7f0a9SMikko Perttunen emc->dram_type &= EMC_FBIO_CFG5_DRAM_TYPE_MASK; 86573a7f0a9SMikko Perttunen emc->dram_type >>= EMC_FBIO_CFG5_DRAM_TYPE_SHIFT; 86673a7f0a9SMikko Perttunen 86773a7f0a9SMikko Perttunen emc->dram_num = tegra_mc_get_emem_device_count(emc->mc); 86873a7f0a9SMikko Perttunen 86973a7f0a9SMikko Perttunen emc_read_current_timing(emc, &emc->last_timing); 87073a7f0a9SMikko Perttunen 87173a7f0a9SMikko Perttunen return 0; 87273a7f0a9SMikko Perttunen } 87373a7f0a9SMikko Perttunen 87473a7f0a9SMikko Perttunen static int load_one_timing_from_dt(struct tegra_emc *emc, 87573a7f0a9SMikko Perttunen struct emc_timing *timing, 87673a7f0a9SMikko Perttunen struct device_node *node) 87773a7f0a9SMikko Perttunen { 87873a7f0a9SMikko Perttunen u32 value; 87973a7f0a9SMikko Perttunen int err; 88073a7f0a9SMikko Perttunen 88173a7f0a9SMikko Perttunen err = of_property_read_u32(node, "clock-frequency", &value); 88273a7f0a9SMikko Perttunen if (err) { 883c86f9854SRob Herring dev_err(emc->dev, "timing %pOFn: failed to read rate: %d\n", 884c86f9854SRob Herring node, err); 88573a7f0a9SMikko Perttunen return err; 88673a7f0a9SMikko Perttunen } 88773a7f0a9SMikko Perttunen 88873a7f0a9SMikko Perttunen timing->rate = value; 88973a7f0a9SMikko Perttunen 89073a7f0a9SMikko Perttunen err = of_property_read_u32_array(node, "nvidia,emc-configuration", 89173a7f0a9SMikko Perttunen timing->emc_burst_data, 89273a7f0a9SMikko Perttunen ARRAY_SIZE(timing->emc_burst_data)); 89373a7f0a9SMikko Perttunen if (err) { 89473a7f0a9SMikko Perttunen dev_err(emc->dev, 895c86f9854SRob Herring "timing %pOFn: failed to read emc burst data: %d\n", 896c86f9854SRob Herring node, err); 89773a7f0a9SMikko Perttunen return err; 89873a7f0a9SMikko Perttunen } 89973a7f0a9SMikko Perttunen 90073a7f0a9SMikko Perttunen #define EMC_READ_PROP(prop, dtprop) { \ 90173a7f0a9SMikko Perttunen err = of_property_read_u32(node, dtprop, &timing->prop); \ 90273a7f0a9SMikko Perttunen if (err) { \ 903c86f9854SRob Herring dev_err(emc->dev, "timing %pOFn: failed to read " #prop ": %d\n", \ 904c86f9854SRob Herring node, err); \ 90573a7f0a9SMikko Perttunen return err; \ 90673a7f0a9SMikko Perttunen } \ 90773a7f0a9SMikko Perttunen } 90873a7f0a9SMikko Perttunen 90973a7f0a9SMikko Perttunen EMC_READ_PROP(emc_auto_cal_config, "nvidia,emc-auto-cal-config") 91073a7f0a9SMikko Perttunen EMC_READ_PROP(emc_auto_cal_config2, "nvidia,emc-auto-cal-config2") 91173a7f0a9SMikko Perttunen EMC_READ_PROP(emc_auto_cal_config3, "nvidia,emc-auto-cal-config3") 91273a7f0a9SMikko Perttunen EMC_READ_PROP(emc_auto_cal_interval, "nvidia,emc-auto-cal-interval") 91373a7f0a9SMikko Perttunen EMC_READ_PROP(emc_bgbias_ctl0, "nvidia,emc-bgbias-ctl0") 91473a7f0a9SMikko Perttunen EMC_READ_PROP(emc_cfg, "nvidia,emc-cfg") 91573a7f0a9SMikko Perttunen EMC_READ_PROP(emc_cfg_2, "nvidia,emc-cfg-2") 91673a7f0a9SMikko Perttunen EMC_READ_PROP(emc_ctt_term_ctrl, "nvidia,emc-ctt-term-ctrl") 91773a7f0a9SMikko Perttunen EMC_READ_PROP(emc_mode_1, "nvidia,emc-mode-1") 91873a7f0a9SMikko Perttunen EMC_READ_PROP(emc_mode_2, "nvidia,emc-mode-2") 91973a7f0a9SMikko Perttunen EMC_READ_PROP(emc_mode_4, "nvidia,emc-mode-4") 92073a7f0a9SMikko Perttunen EMC_READ_PROP(emc_mode_reset, "nvidia,emc-mode-reset") 92173a7f0a9SMikko Perttunen EMC_READ_PROP(emc_mrs_wait_cnt, "nvidia,emc-mrs-wait-cnt") 92273a7f0a9SMikko Perttunen EMC_READ_PROP(emc_sel_dpd_ctrl, "nvidia,emc-sel-dpd-ctrl") 92373a7f0a9SMikko Perttunen EMC_READ_PROP(emc_xm2dqspadctrl2, "nvidia,emc-xm2dqspadctrl2") 92473a7f0a9SMikko Perttunen EMC_READ_PROP(emc_zcal_cnt_long, "nvidia,emc-zcal-cnt-long") 92573a7f0a9SMikko Perttunen EMC_READ_PROP(emc_zcal_interval, "nvidia,emc-zcal-interval") 92673a7f0a9SMikko Perttunen 92773a7f0a9SMikko Perttunen #undef EMC_READ_PROP 92873a7f0a9SMikko Perttunen 92973a7f0a9SMikko Perttunen return 0; 93073a7f0a9SMikko Perttunen } 93173a7f0a9SMikko Perttunen 93273a7f0a9SMikko Perttunen static int cmp_timings(const void *_a, const void *_b) 93373a7f0a9SMikko Perttunen { 93473a7f0a9SMikko Perttunen const struct emc_timing *a = _a; 93573a7f0a9SMikko Perttunen const struct emc_timing *b = _b; 93673a7f0a9SMikko Perttunen 93773a7f0a9SMikko Perttunen if (a->rate < b->rate) 93873a7f0a9SMikko Perttunen return -1; 93973a7f0a9SMikko Perttunen else if (a->rate == b->rate) 94073a7f0a9SMikko Perttunen return 0; 94173a7f0a9SMikko Perttunen else 94273a7f0a9SMikko Perttunen return 1; 94373a7f0a9SMikko Perttunen } 94473a7f0a9SMikko Perttunen 94573a7f0a9SMikko Perttunen static int tegra_emc_load_timings_from_dt(struct tegra_emc *emc, 94673a7f0a9SMikko Perttunen struct device_node *node) 94773a7f0a9SMikko Perttunen { 94873a7f0a9SMikko Perttunen int child_count = of_get_child_count(node); 94973a7f0a9SMikko Perttunen struct device_node *child; 95073a7f0a9SMikko Perttunen struct emc_timing *timing; 95173a7f0a9SMikko Perttunen unsigned int i = 0; 95273a7f0a9SMikko Perttunen int err; 95373a7f0a9SMikko Perttunen 95473a7f0a9SMikko Perttunen emc->timings = devm_kcalloc(emc->dev, child_count, sizeof(*timing), 95573a7f0a9SMikko Perttunen GFP_KERNEL); 95673a7f0a9SMikko Perttunen if (!emc->timings) 95773a7f0a9SMikko Perttunen return -ENOMEM; 95873a7f0a9SMikko Perttunen 95973a7f0a9SMikko Perttunen emc->num_timings = child_count; 96073a7f0a9SMikko Perttunen 96173a7f0a9SMikko Perttunen for_each_child_of_node(node, child) { 96273a7f0a9SMikko Perttunen timing = &emc->timings[i++]; 96373a7f0a9SMikko Perttunen 96473a7f0a9SMikko Perttunen err = load_one_timing_from_dt(emc, timing, child); 965aafb197fSAmitoj Kaur Chawla if (err) { 966aafb197fSAmitoj Kaur Chawla of_node_put(child); 96773a7f0a9SMikko Perttunen return err; 96873a7f0a9SMikko Perttunen } 969aafb197fSAmitoj Kaur Chawla } 97073a7f0a9SMikko Perttunen 97173a7f0a9SMikko Perttunen sort(emc->timings, emc->num_timings, sizeof(*timing), cmp_timings, 97273a7f0a9SMikko Perttunen NULL); 97373a7f0a9SMikko Perttunen 97473a7f0a9SMikko Perttunen return 0; 97573a7f0a9SMikko Perttunen } 97673a7f0a9SMikko Perttunen 97773a7f0a9SMikko Perttunen static const struct of_device_id tegra_emc_of_match[] = { 97873a7f0a9SMikko Perttunen { .compatible = "nvidia,tegra124-emc" }, 97973a7f0a9SMikko Perttunen {} 98073a7f0a9SMikko Perttunen }; 98173a7f0a9SMikko Perttunen 98273a7f0a9SMikko Perttunen static struct device_node * 98373a7f0a9SMikko Perttunen tegra_emc_find_node_by_ram_code(struct device_node *node, u32 ram_code) 98473a7f0a9SMikko Perttunen { 98573a7f0a9SMikko Perttunen struct device_node *np; 98673a7f0a9SMikko Perttunen int err; 98773a7f0a9SMikko Perttunen 98873a7f0a9SMikko Perttunen for_each_child_of_node(node, np) { 98973a7f0a9SMikko Perttunen u32 value; 99073a7f0a9SMikko Perttunen 99173a7f0a9SMikko Perttunen err = of_property_read_u32(np, "nvidia,ram-code", &value); 992d1122e4bSJulia Lawall if (err || (value != ram_code)) 99373a7f0a9SMikko Perttunen continue; 99473a7f0a9SMikko Perttunen 99573a7f0a9SMikko Perttunen return np; 99673a7f0a9SMikko Perttunen } 99773a7f0a9SMikko Perttunen 99873a7f0a9SMikko Perttunen return NULL; 99973a7f0a9SMikko Perttunen } 100073a7f0a9SMikko Perttunen 10019c77a81fSMikko Perttunen /* Debugfs entry */ 10029c77a81fSMikko Perttunen 10039c77a81fSMikko Perttunen static int emc_debug_rate_get(void *data, u64 *rate) 10049c77a81fSMikko Perttunen { 10059c77a81fSMikko Perttunen struct clk *c = data; 10069c77a81fSMikko Perttunen 10079c77a81fSMikko Perttunen *rate = clk_get_rate(c); 10089c77a81fSMikko Perttunen 10099c77a81fSMikko Perttunen return 0; 10109c77a81fSMikko Perttunen } 10119c77a81fSMikko Perttunen 10129c77a81fSMikko Perttunen static int emc_debug_rate_set(void *data, u64 rate) 10139c77a81fSMikko Perttunen { 10149c77a81fSMikko Perttunen struct clk *c = data; 10159c77a81fSMikko Perttunen 10169c77a81fSMikko Perttunen return clk_set_rate(c, rate); 10179c77a81fSMikko Perttunen } 10189c77a81fSMikko Perttunen 10199c77a81fSMikko Perttunen DEFINE_SIMPLE_ATTRIBUTE(emc_debug_rate_fops, emc_debug_rate_get, 10209c77a81fSMikko Perttunen emc_debug_rate_set, "%lld\n"); 10219c77a81fSMikko Perttunen 102230a636f9SThierry Reding static int emc_debug_supported_rates_show(struct seq_file *s, void *data) 102330a636f9SThierry Reding { 102430a636f9SThierry Reding struct tegra_emc *emc = s->private; 102530a636f9SThierry Reding const char *prefix = ""; 102630a636f9SThierry Reding unsigned int i; 102730a636f9SThierry Reding 102830a636f9SThierry Reding for (i = 0; i < emc->num_timings; i++) { 102930a636f9SThierry Reding struct emc_timing *timing = &emc->timings[i]; 103030a636f9SThierry Reding 103130a636f9SThierry Reding seq_printf(s, "%s%lu", prefix, timing->rate); 103230a636f9SThierry Reding 103330a636f9SThierry Reding prefix = " "; 103430a636f9SThierry Reding } 103530a636f9SThierry Reding 103630a636f9SThierry Reding seq_puts(s, "\n"); 103730a636f9SThierry Reding 103830a636f9SThierry Reding return 0; 103930a636f9SThierry Reding } 104030a636f9SThierry Reding 104130a636f9SThierry Reding static int emc_debug_supported_rates_open(struct inode *inode, 104230a636f9SThierry Reding struct file *file) 104330a636f9SThierry Reding { 104430a636f9SThierry Reding return single_open(file, emc_debug_supported_rates_show, 104530a636f9SThierry Reding inode->i_private); 104630a636f9SThierry Reding } 104730a636f9SThierry Reding 104830a636f9SThierry Reding static const struct file_operations emc_debug_supported_rates_fops = { 104930a636f9SThierry Reding .open = emc_debug_supported_rates_open, 105030a636f9SThierry Reding .read = seq_read, 105130a636f9SThierry Reding .llseek = seq_lseek, 105230a636f9SThierry Reding .release = single_release, 105330a636f9SThierry Reding }; 105430a636f9SThierry Reding 105530a636f9SThierry Reding static void emc_debugfs_init(struct device *dev, struct tegra_emc *emc) 10569c77a81fSMikko Perttunen { 10579c77a81fSMikko Perttunen struct dentry *root, *file; 10589c77a81fSMikko Perttunen struct clk *clk; 10599c77a81fSMikko Perttunen 10609c77a81fSMikko Perttunen root = debugfs_create_dir("emc", NULL); 10619c77a81fSMikko Perttunen if (!root) { 10629c77a81fSMikko Perttunen dev_err(dev, "failed to create debugfs directory\n"); 10639c77a81fSMikko Perttunen return; 10649c77a81fSMikko Perttunen } 10659c77a81fSMikko Perttunen 10669c77a81fSMikko Perttunen clk = clk_get_sys("tegra-clk-debug", "emc"); 10679c77a81fSMikko Perttunen if (IS_ERR(clk)) { 10689c77a81fSMikko Perttunen dev_err(dev, "failed to get debug clock: %ld\n", PTR_ERR(clk)); 10699c77a81fSMikko Perttunen return; 10709c77a81fSMikko Perttunen } 10719c77a81fSMikko Perttunen 10729c77a81fSMikko Perttunen file = debugfs_create_file("rate", S_IRUGO | S_IWUSR, root, clk, 10739c77a81fSMikko Perttunen &emc_debug_rate_fops); 10749c77a81fSMikko Perttunen if (!file) 10759c77a81fSMikko Perttunen dev_err(dev, "failed to create debugfs entry\n"); 107630a636f9SThierry Reding 107730a636f9SThierry Reding file = debugfs_create_file("supported_rates", S_IRUGO, root, emc, 107830a636f9SThierry Reding &emc_debug_supported_rates_fops); 107930a636f9SThierry Reding if (!file) 108030a636f9SThierry Reding dev_err(dev, "failed to create debugfs entry\n"); 10819c77a81fSMikko Perttunen } 10829c77a81fSMikko Perttunen 108373a7f0a9SMikko Perttunen static int tegra_emc_probe(struct platform_device *pdev) 108473a7f0a9SMikko Perttunen { 108573a7f0a9SMikko Perttunen struct platform_device *mc; 108673a7f0a9SMikko Perttunen struct device_node *np; 108773a7f0a9SMikko Perttunen struct tegra_emc *emc; 108873a7f0a9SMikko Perttunen struct resource *res; 108973a7f0a9SMikko Perttunen u32 ram_code; 109073a7f0a9SMikko Perttunen int err; 109173a7f0a9SMikko Perttunen 109273a7f0a9SMikko Perttunen emc = devm_kzalloc(&pdev->dev, sizeof(*emc), GFP_KERNEL); 109373a7f0a9SMikko Perttunen if (!emc) 109473a7f0a9SMikko Perttunen return -ENOMEM; 109573a7f0a9SMikko Perttunen 109673a7f0a9SMikko Perttunen emc->dev = &pdev->dev; 109773a7f0a9SMikko Perttunen 109873a7f0a9SMikko Perttunen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 109973a7f0a9SMikko Perttunen emc->regs = devm_ioremap_resource(&pdev->dev, res); 110073a7f0a9SMikko Perttunen if (IS_ERR(emc->regs)) 110173a7f0a9SMikko Perttunen return PTR_ERR(emc->regs); 110273a7f0a9SMikko Perttunen 110373a7f0a9SMikko Perttunen np = of_parse_phandle(pdev->dev.of_node, "nvidia,memory-controller", 0); 110473a7f0a9SMikko Perttunen if (!np) { 110573a7f0a9SMikko Perttunen dev_err(&pdev->dev, "could not get memory controller\n"); 110673a7f0a9SMikko Perttunen return -ENOENT; 110773a7f0a9SMikko Perttunen } 110873a7f0a9SMikko Perttunen 110973a7f0a9SMikko Perttunen mc = of_find_device_by_node(np); 1110b92f4380SChristophe Jaillet of_node_put(np); 111173a7f0a9SMikko Perttunen if (!mc) 111273a7f0a9SMikko Perttunen return -ENOENT; 111373a7f0a9SMikko Perttunen 111473a7f0a9SMikko Perttunen emc->mc = platform_get_drvdata(mc); 111573a7f0a9SMikko Perttunen if (!emc->mc) 111673a7f0a9SMikko Perttunen return -EPROBE_DEFER; 111773a7f0a9SMikko Perttunen 111873a7f0a9SMikko Perttunen ram_code = tegra_read_ram_code(); 111973a7f0a9SMikko Perttunen 112073a7f0a9SMikko Perttunen np = tegra_emc_find_node_by_ram_code(pdev->dev.of_node, ram_code); 112173a7f0a9SMikko Perttunen if (!np) { 112273a7f0a9SMikko Perttunen dev_err(&pdev->dev, 112373a7f0a9SMikko Perttunen "no memory timings for RAM code %u found in DT\n", 112473a7f0a9SMikko Perttunen ram_code); 112573a7f0a9SMikko Perttunen return -ENOENT; 112673a7f0a9SMikko Perttunen } 112773a7f0a9SMikko Perttunen 112873a7f0a9SMikko Perttunen err = tegra_emc_load_timings_from_dt(emc, np); 112973a7f0a9SMikko Perttunen of_node_put(np); 113073a7f0a9SMikko Perttunen if (err) 113173a7f0a9SMikko Perttunen return err; 113273a7f0a9SMikko Perttunen 113373a7f0a9SMikko Perttunen if (emc->num_timings == 0) { 113473a7f0a9SMikko Perttunen dev_err(&pdev->dev, 113573a7f0a9SMikko Perttunen "no memory timings for RAM code %u registered\n", 113673a7f0a9SMikko Perttunen ram_code); 113773a7f0a9SMikko Perttunen return -ENOENT; 113873a7f0a9SMikko Perttunen } 113973a7f0a9SMikko Perttunen 114073a7f0a9SMikko Perttunen err = emc_init(emc); 114173a7f0a9SMikko Perttunen if (err) { 114273a7f0a9SMikko Perttunen dev_err(&pdev->dev, "EMC initialization failed: %d\n", err); 114373a7f0a9SMikko Perttunen return err; 114473a7f0a9SMikko Perttunen } 114573a7f0a9SMikko Perttunen 114673a7f0a9SMikko Perttunen platform_set_drvdata(pdev, emc); 114773a7f0a9SMikko Perttunen 11489c77a81fSMikko Perttunen if (IS_ENABLED(CONFIG_DEBUG_FS)) 114930a636f9SThierry Reding emc_debugfs_init(&pdev->dev, emc); 11509c77a81fSMikko Perttunen 115173a7f0a9SMikko Perttunen return 0; 115273a7f0a9SMikko Perttunen }; 115373a7f0a9SMikko Perttunen 115473a7f0a9SMikko Perttunen static struct platform_driver tegra_emc_driver = { 115573a7f0a9SMikko Perttunen .probe = tegra_emc_probe, 115673a7f0a9SMikko Perttunen .driver = { 115773a7f0a9SMikko Perttunen .name = "tegra-emc", 115873a7f0a9SMikko Perttunen .of_match_table = tegra_emc_of_match, 115973a7f0a9SMikko Perttunen .suppress_bind_attrs = true, 116073a7f0a9SMikko Perttunen }, 116173a7f0a9SMikko Perttunen }; 116273a7f0a9SMikko Perttunen 116373a7f0a9SMikko Perttunen static int tegra_emc_init(void) 116473a7f0a9SMikko Perttunen { 116573a7f0a9SMikko Perttunen return platform_driver_register(&tegra_emc_driver); 116673a7f0a9SMikko Perttunen } 116773a7f0a9SMikko Perttunen subsys_initcall(tegra_emc_init); 1168