173a7f0a9SMikko Perttunen /* 273a7f0a9SMikko Perttunen * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. 373a7f0a9SMikko Perttunen * 473a7f0a9SMikko Perttunen * Author: 573a7f0a9SMikko Perttunen * Mikko Perttunen <mperttunen@nvidia.com> 673a7f0a9SMikko Perttunen * 773a7f0a9SMikko Perttunen * This software is licensed under the terms of the GNU General Public 873a7f0a9SMikko Perttunen * License version 2, as published by the Free Software Foundation, and 973a7f0a9SMikko Perttunen * may be copied, distributed, and modified under those terms. 1073a7f0a9SMikko Perttunen * 1173a7f0a9SMikko Perttunen * This program is distributed in the hope that it will be useful, 1273a7f0a9SMikko Perttunen * but WITHOUT ANY WARRANTY; without even the implied warranty of 1373a7f0a9SMikko Perttunen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1473a7f0a9SMikko Perttunen * GNU General Public License for more details. 1573a7f0a9SMikko Perttunen * 1673a7f0a9SMikko Perttunen */ 1773a7f0a9SMikko Perttunen 1873a7f0a9SMikko Perttunen #include <linux/clk-provider.h> 1973a7f0a9SMikko Perttunen #include <linux/clk.h> 2073a7f0a9SMikko Perttunen #include <linux/clkdev.h> 219c77a81fSMikko Perttunen #include <linux/debugfs.h> 2273a7f0a9SMikko Perttunen #include <linux/delay.h> 2373a7f0a9SMikko Perttunen #include <linux/of_address.h> 2473a7f0a9SMikko Perttunen #include <linux/of_platform.h> 2573a7f0a9SMikko Perttunen #include <linux/platform_device.h> 2673a7f0a9SMikko Perttunen #include <linux/sort.h> 2773a7f0a9SMikko Perttunen #include <linux/string.h> 2873a7f0a9SMikko Perttunen 2973a7f0a9SMikko Perttunen #include <soc/tegra/emc.h> 3073a7f0a9SMikko Perttunen #include <soc/tegra/fuse.h> 3173a7f0a9SMikko Perttunen #include <soc/tegra/mc.h> 3273a7f0a9SMikko Perttunen 3373a7f0a9SMikko Perttunen #define EMC_FBIO_CFG5 0x104 3473a7f0a9SMikko Perttunen #define EMC_FBIO_CFG5_DRAM_TYPE_MASK 0x3 3573a7f0a9SMikko Perttunen #define EMC_FBIO_CFG5_DRAM_TYPE_SHIFT 0 3673a7f0a9SMikko Perttunen 3773a7f0a9SMikko Perttunen #define EMC_INTSTATUS 0x0 3873a7f0a9SMikko Perttunen #define EMC_INTSTATUS_CLKCHANGE_COMPLETE BIT(4) 3973a7f0a9SMikko Perttunen 4073a7f0a9SMikko Perttunen #define EMC_CFG 0xc 4173a7f0a9SMikko Perttunen #define EMC_CFG_DRAM_CLKSTOP_PD BIT(31) 4273a7f0a9SMikko Perttunen #define EMC_CFG_DRAM_CLKSTOP_SR BIT(30) 4373a7f0a9SMikko Perttunen #define EMC_CFG_DRAM_ACPD BIT(29) 4473a7f0a9SMikko Perttunen #define EMC_CFG_DYN_SREF BIT(28) 4573a7f0a9SMikko Perttunen #define EMC_CFG_PWR_MASK ((0xF << 28) | BIT(18)) 4673a7f0a9SMikko Perttunen #define EMC_CFG_DSR_VTTGEN_DRV_EN BIT(18) 4773a7f0a9SMikko Perttunen 4873a7f0a9SMikko Perttunen #define EMC_REFCTRL 0x20 4973a7f0a9SMikko Perttunen #define EMC_REFCTRL_DEV_SEL_SHIFT 0 5073a7f0a9SMikko Perttunen #define EMC_REFCTRL_ENABLE BIT(31) 5173a7f0a9SMikko Perttunen 5273a7f0a9SMikko Perttunen #define EMC_TIMING_CONTROL 0x28 5373a7f0a9SMikko Perttunen #define EMC_RC 0x2c 5473a7f0a9SMikko Perttunen #define EMC_RFC 0x30 5573a7f0a9SMikko Perttunen #define EMC_RAS 0x34 5673a7f0a9SMikko Perttunen #define EMC_RP 0x38 5773a7f0a9SMikko Perttunen #define EMC_R2W 0x3c 5873a7f0a9SMikko Perttunen #define EMC_W2R 0x40 5973a7f0a9SMikko Perttunen #define EMC_R2P 0x44 6073a7f0a9SMikko Perttunen #define EMC_W2P 0x48 6173a7f0a9SMikko Perttunen #define EMC_RD_RCD 0x4c 6273a7f0a9SMikko Perttunen #define EMC_WR_RCD 0x50 6373a7f0a9SMikko Perttunen #define EMC_RRD 0x54 6473a7f0a9SMikko Perttunen #define EMC_REXT 0x58 6573a7f0a9SMikko Perttunen #define EMC_WDV 0x5c 6673a7f0a9SMikko Perttunen #define EMC_QUSE 0x60 6773a7f0a9SMikko Perttunen #define EMC_QRST 0x64 6873a7f0a9SMikko Perttunen #define EMC_QSAFE 0x68 6973a7f0a9SMikko Perttunen #define EMC_RDV 0x6c 7073a7f0a9SMikko Perttunen #define EMC_REFRESH 0x70 7173a7f0a9SMikko Perttunen #define EMC_BURST_REFRESH_NUM 0x74 7273a7f0a9SMikko Perttunen #define EMC_PDEX2WR 0x78 7373a7f0a9SMikko Perttunen #define EMC_PDEX2RD 0x7c 7473a7f0a9SMikko Perttunen #define EMC_PCHG2PDEN 0x80 7573a7f0a9SMikko Perttunen #define EMC_ACT2PDEN 0x84 7673a7f0a9SMikko Perttunen #define EMC_AR2PDEN 0x88 7773a7f0a9SMikko Perttunen #define EMC_RW2PDEN 0x8c 7873a7f0a9SMikko Perttunen #define EMC_TXSR 0x90 7973a7f0a9SMikko Perttunen #define EMC_TCKE 0x94 8073a7f0a9SMikko Perttunen #define EMC_TFAW 0x98 8173a7f0a9SMikko Perttunen #define EMC_TRPAB 0x9c 8273a7f0a9SMikko Perttunen #define EMC_TCLKSTABLE 0xa0 8373a7f0a9SMikko Perttunen #define EMC_TCLKSTOP 0xa4 8473a7f0a9SMikko Perttunen #define EMC_TREFBW 0xa8 8573a7f0a9SMikko Perttunen #define EMC_ODT_WRITE 0xb0 8673a7f0a9SMikko Perttunen #define EMC_ODT_READ 0xb4 8773a7f0a9SMikko Perttunen #define EMC_WEXT 0xb8 8873a7f0a9SMikko Perttunen #define EMC_CTT 0xbc 8973a7f0a9SMikko Perttunen #define EMC_RFC_SLR 0xc0 9073a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT2 0xc4 9173a7f0a9SMikko Perttunen 9273a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT 0xc8 9373a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT 0 9473a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK \ 9573a7f0a9SMikko Perttunen (0x3FF << EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT) 9673a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT 16 9773a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_LONG_WAIT_MASK \ 9873a7f0a9SMikko Perttunen (0x3FF << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT) 9973a7f0a9SMikko Perttunen 10073a7f0a9SMikko Perttunen #define EMC_MRS 0xcc 10173a7f0a9SMikko Perttunen #define EMC_MODE_SET_DLL_RESET BIT(8) 10273a7f0a9SMikko Perttunen #define EMC_MODE_SET_LONG_CNT BIT(26) 10373a7f0a9SMikko Perttunen #define EMC_EMRS 0xd0 10473a7f0a9SMikko Perttunen #define EMC_REF 0xd4 10573a7f0a9SMikko Perttunen #define EMC_PRE 0xd8 10673a7f0a9SMikko Perttunen 10773a7f0a9SMikko Perttunen #define EMC_SELF_REF 0xe0 10873a7f0a9SMikko Perttunen #define EMC_SELF_REF_CMD_ENABLED BIT(0) 10973a7f0a9SMikko Perttunen #define EMC_SELF_REF_DEV_SEL_SHIFT 30 11073a7f0a9SMikko Perttunen 11173a7f0a9SMikko Perttunen #define EMC_MRW 0xe8 11273a7f0a9SMikko Perttunen 11373a7f0a9SMikko Perttunen #define EMC_MRR 0xec 11473a7f0a9SMikko Perttunen #define EMC_MRR_MA_SHIFT 16 11573a7f0a9SMikko Perttunen #define LPDDR2_MR4_TEMP_SHIFT 0 11673a7f0a9SMikko Perttunen 11773a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL3 0xf8 11873a7f0a9SMikko Perttunen #define EMC_FBIO_SPARE 0x100 11973a7f0a9SMikko Perttunen 12073a7f0a9SMikko Perttunen #define EMC_FBIO_CFG6 0x114 12173a7f0a9SMikko Perttunen #define EMC_EMRS2 0x12c 12273a7f0a9SMikko Perttunen #define EMC_MRW2 0x134 12373a7f0a9SMikko Perttunen #define EMC_MRW4 0x13c 12473a7f0a9SMikko Perttunen #define EMC_EINPUT 0x14c 12573a7f0a9SMikko Perttunen #define EMC_EINPUT_DURATION 0x150 12673a7f0a9SMikko Perttunen #define EMC_PUTERM_EXTRA 0x154 12773a7f0a9SMikko Perttunen #define EMC_TCKESR 0x158 12873a7f0a9SMikko Perttunen #define EMC_TPD 0x15c 12973a7f0a9SMikko Perttunen 13073a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG 0x2a4 13173a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG_AUTO_CAL_START BIT(31) 13273a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_INTERVAL 0x2a8 13373a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_STATUS 0x2ac 13473a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_STATUS_ACTIVE BIT(31) 13573a7f0a9SMikko Perttunen #define EMC_STATUS 0x2b4 13673a7f0a9SMikko Perttunen #define EMC_STATUS_TIMING_UPDATE_STALLED BIT(23) 13773a7f0a9SMikko Perttunen 13873a7f0a9SMikko Perttunen #define EMC_CFG_2 0x2b8 13973a7f0a9SMikko Perttunen #define EMC_CFG_2_MODE_SHIFT 0 14073a7f0a9SMikko Perttunen #define EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR BIT(6) 14173a7f0a9SMikko Perttunen 14273a7f0a9SMikko Perttunen #define EMC_CFG_DIG_DLL 0x2bc 14373a7f0a9SMikko Perttunen #define EMC_CFG_DIG_DLL_PERIOD 0x2c0 14473a7f0a9SMikko Perttunen #define EMC_RDV_MASK 0x2cc 14573a7f0a9SMikko Perttunen #define EMC_WDV_MASK 0x2d0 14673a7f0a9SMikko Perttunen #define EMC_CTT_DURATION 0x2d8 14773a7f0a9SMikko Perttunen #define EMC_CTT_TERM_CTRL 0x2dc 14873a7f0a9SMikko Perttunen #define EMC_ZCAL_INTERVAL 0x2e0 14973a7f0a9SMikko Perttunen #define EMC_ZCAL_WAIT_CNT 0x2e4 15073a7f0a9SMikko Perttunen 15173a7f0a9SMikko Perttunen #define EMC_ZQ_CAL 0x2ec 15273a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_CMD BIT(0) 15373a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_LONG BIT(4) 15473a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_LONG_CMD_DEV0 \ 15573a7f0a9SMikko Perttunen (DRAM_DEV_SEL_0 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD) 15673a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_LONG_CMD_DEV1 \ 15773a7f0a9SMikko Perttunen (DRAM_DEV_SEL_1 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD) 15873a7f0a9SMikko Perttunen 15973a7f0a9SMikko Perttunen #define EMC_XM2CMDPADCTRL 0x2f0 16073a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL 0x2f8 16173a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL2 0x2fc 16273a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE BIT(0) 16373a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL2_VREF_ENABLE BIT(5) 16473a7f0a9SMikko Perttunen #define EMC_XM2DQPADCTRL 0x300 16573a7f0a9SMikko Perttunen #define EMC_XM2DQPADCTRL2 0x304 16673a7f0a9SMikko Perttunen #define EMC_XM2CLKPADCTRL 0x308 16773a7f0a9SMikko Perttunen #define EMC_XM2COMPPADCTRL 0x30c 16873a7f0a9SMikko Perttunen #define EMC_XM2VTTGENPADCTRL 0x310 16973a7f0a9SMikko Perttunen #define EMC_XM2VTTGENPADCTRL2 0x314 17073a7f0a9SMikko Perttunen #define EMC_XM2VTTGENPADCTRL3 0x318 17173a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL4 0x320 17273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS0 0x328 17373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS1 0x32c 17473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS2 0x330 17573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS3 0x334 17673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS4 0x338 17773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS5 0x33c 17873a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS6 0x340 17973a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS7 0x344 18073a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE0 0x348 18173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE1 0x34c 18273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE2 0x350 18373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE3 0x354 18473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE4 0x358 18573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE5 0x35c 18673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE6 0x360 18773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE7 0x364 18873a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ0 0x368 18973a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ1 0x36c 19073a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ2 0x370 19173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ3 0x374 19273a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS0 0x3a8 19373a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS1 0x3ac 19473a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS2 0x3b0 19573a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS3 0x3b4 19673a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS4 0x3b8 19773a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS5 0x3bc 19873a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS6 0x3c0 19973a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS7 0x3c4 20073a7f0a9SMikko Perttunen #define EMC_STALL_THEN_EXE_AFTER_CLKCHANGE 0x3cc 20173a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL 0x3d8 20273a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_DATA_SEL_DPD BIT(8) 20373a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_ODT_SEL_DPD BIT(5) 20473a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_RESET_SEL_DPD BIT(4) 20573a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_CA_SEL_DPD BIT(3) 20673a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_CLK_SEL_DPD BIT(2) 20773a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_DDR3_MASK \ 20873a7f0a9SMikko Perttunen ((0xf << 2) | BIT(8)) 20973a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_MASK \ 21073a7f0a9SMikko Perttunen ((0x3 << 2) | BIT(5) | BIT(8)) 21173a7f0a9SMikko Perttunen #define EMC_PRE_REFRESH_REQ_CNT 0x3dc 21273a7f0a9SMikko Perttunen #define EMC_DYN_SELF_REF_CONTROL 0x3e0 21373a7f0a9SMikko Perttunen #define EMC_TXSRDLL 0x3e4 21473a7f0a9SMikko Perttunen #define EMC_CCFIFO_ADDR 0x3e8 21573a7f0a9SMikko Perttunen #define EMC_CCFIFO_DATA 0x3ec 21673a7f0a9SMikko Perttunen #define EMC_CCFIFO_STATUS 0x3f0 21773a7f0a9SMikko Perttunen #define EMC_CDB_CNTL_1 0x3f4 21873a7f0a9SMikko Perttunen #define EMC_CDB_CNTL_2 0x3f8 21973a7f0a9SMikko Perttunen #define EMC_XM2CLKPADCTRL2 0x3fc 22073a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG2 0x458 22173a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG3 0x45c 22273a7f0a9SMikko Perttunen #define EMC_IBDLY 0x468 22373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR0 0x46c 22473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR1 0x470 22573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR2 0x474 22673a7f0a9SMikko Perttunen #define EMC_DSR_VTTGEN_DRV 0x47c 22773a7f0a9SMikko Perttunen #define EMC_TXDSRVTTGEN 0x480 22873a7f0a9SMikko Perttunen #define EMC_XM2CMDPADCTRL4 0x484 22973a7f0a9SMikko Perttunen #define EMC_XM2CMDPADCTRL5 0x488 23073a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS8 0x4a0 23173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS9 0x4a4 23273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS10 0x4a8 23373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS11 0x4ac 23473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS12 0x4b0 23573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS13 0x4b4 23673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS14 0x4b8 23773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS15 0x4bc 23873a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE8 0x4c0 23973a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE9 0x4c4 24073a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE10 0x4c8 24173a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE11 0x4cc 24273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE12 0x4d0 24373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE13 0x4d4 24473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE14 0x4d8 24573a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE15 0x4dc 24673a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ4 0x4e0 24773a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ5 0x4e4 24873a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ6 0x4e8 24973a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ7 0x4ec 25073a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS8 0x520 25173a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS9 0x524 25273a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS10 0x528 25373a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS11 0x52c 25473a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS12 0x530 25573a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS13 0x534 25673a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS14 0x538 25773a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS15 0x53c 25873a7f0a9SMikko Perttunen #define EMC_CDB_CNTL_3 0x540 25973a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL5 0x544 26073a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL6 0x548 26173a7f0a9SMikko Perttunen #define EMC_XM2DQPADCTRL3 0x54c 26273a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR3 0x550 26373a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR4 0x554 26473a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR5 0x558 26573a7f0a9SMikko Perttunen #define EMC_CFG_PIPE 0x560 26673a7f0a9SMikko Perttunen #define EMC_QPOP 0x564 26773a7f0a9SMikko Perttunen #define EMC_QUSE_WIDTH 0x568 26873a7f0a9SMikko Perttunen #define EMC_PUTERM_WIDTH 0x56c 26973a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0 0x570 27073a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX BIT(3) 27173a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN BIT(2) 27273a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD BIT(1) 27373a7f0a9SMikko Perttunen #define EMC_PUTERM_ADJ 0x574 27473a7f0a9SMikko Perttunen 27573a7f0a9SMikko Perttunen #define DRAM_DEV_SEL_ALL 0 27673a7f0a9SMikko Perttunen #define DRAM_DEV_SEL_0 (2 << 30) 27773a7f0a9SMikko Perttunen #define DRAM_DEV_SEL_1 (1 << 30) 27873a7f0a9SMikko Perttunen 27973a7f0a9SMikko Perttunen #define EMC_CFG_POWER_FEATURES_MASK \ 28073a7f0a9SMikko Perttunen (EMC_CFG_DYN_SREF | EMC_CFG_DRAM_ACPD | EMC_CFG_DRAM_CLKSTOP_SR | \ 28173a7f0a9SMikko Perttunen EMC_CFG_DRAM_CLKSTOP_PD | EMC_CFG_DSR_VTTGEN_DRV_EN) 28273a7f0a9SMikko Perttunen #define EMC_REFCTRL_DEV_SEL(n) (((n > 1) ? 0 : 2) << EMC_REFCTRL_DEV_SEL_SHIFT) 28373a7f0a9SMikko Perttunen #define EMC_DRAM_DEV_SEL(n) ((n > 1) ? DRAM_DEV_SEL_ALL : DRAM_DEV_SEL_0) 28473a7f0a9SMikko Perttunen 28573a7f0a9SMikko Perttunen /* Maximum amount of time in us. to wait for changes to become effective */ 28673a7f0a9SMikko Perttunen #define EMC_STATUS_UPDATE_TIMEOUT 1000 28773a7f0a9SMikko Perttunen 28873a7f0a9SMikko Perttunen enum emc_dram_type { 28973a7f0a9SMikko Perttunen DRAM_TYPE_DDR3 = 0, 29073a7f0a9SMikko Perttunen DRAM_TYPE_DDR1 = 1, 29173a7f0a9SMikko Perttunen DRAM_TYPE_LPDDR3 = 2, 29273a7f0a9SMikko Perttunen DRAM_TYPE_DDR2 = 3 29373a7f0a9SMikko Perttunen }; 29473a7f0a9SMikko Perttunen 29573a7f0a9SMikko Perttunen enum emc_dll_change { 29673a7f0a9SMikko Perttunen DLL_CHANGE_NONE, 29773a7f0a9SMikko Perttunen DLL_CHANGE_ON, 29873a7f0a9SMikko Perttunen DLL_CHANGE_OFF 29973a7f0a9SMikko Perttunen }; 30073a7f0a9SMikko Perttunen 30173a7f0a9SMikko Perttunen static const unsigned long emc_burst_regs[] = { 30273a7f0a9SMikko Perttunen EMC_RC, 30373a7f0a9SMikko Perttunen EMC_RFC, 30473a7f0a9SMikko Perttunen EMC_RFC_SLR, 30573a7f0a9SMikko Perttunen EMC_RAS, 30673a7f0a9SMikko Perttunen EMC_RP, 30773a7f0a9SMikko Perttunen EMC_R2W, 30873a7f0a9SMikko Perttunen EMC_W2R, 30973a7f0a9SMikko Perttunen EMC_R2P, 31073a7f0a9SMikko Perttunen EMC_W2P, 31173a7f0a9SMikko Perttunen EMC_RD_RCD, 31273a7f0a9SMikko Perttunen EMC_WR_RCD, 31373a7f0a9SMikko Perttunen EMC_RRD, 31473a7f0a9SMikko Perttunen EMC_REXT, 31573a7f0a9SMikko Perttunen EMC_WEXT, 31673a7f0a9SMikko Perttunen EMC_WDV, 31773a7f0a9SMikko Perttunen EMC_WDV_MASK, 31873a7f0a9SMikko Perttunen EMC_QUSE, 31973a7f0a9SMikko Perttunen EMC_QUSE_WIDTH, 32073a7f0a9SMikko Perttunen EMC_IBDLY, 32173a7f0a9SMikko Perttunen EMC_EINPUT, 32273a7f0a9SMikko Perttunen EMC_EINPUT_DURATION, 32373a7f0a9SMikko Perttunen EMC_PUTERM_EXTRA, 32473a7f0a9SMikko Perttunen EMC_PUTERM_WIDTH, 32573a7f0a9SMikko Perttunen EMC_PUTERM_ADJ, 32673a7f0a9SMikko Perttunen EMC_CDB_CNTL_1, 32773a7f0a9SMikko Perttunen EMC_CDB_CNTL_2, 32873a7f0a9SMikko Perttunen EMC_CDB_CNTL_3, 32973a7f0a9SMikko Perttunen EMC_QRST, 33073a7f0a9SMikko Perttunen EMC_QSAFE, 33173a7f0a9SMikko Perttunen EMC_RDV, 33273a7f0a9SMikko Perttunen EMC_RDV_MASK, 33373a7f0a9SMikko Perttunen EMC_REFRESH, 33473a7f0a9SMikko Perttunen EMC_BURST_REFRESH_NUM, 33573a7f0a9SMikko Perttunen EMC_PRE_REFRESH_REQ_CNT, 33673a7f0a9SMikko Perttunen EMC_PDEX2WR, 33773a7f0a9SMikko Perttunen EMC_PDEX2RD, 33873a7f0a9SMikko Perttunen EMC_PCHG2PDEN, 33973a7f0a9SMikko Perttunen EMC_ACT2PDEN, 34073a7f0a9SMikko Perttunen EMC_AR2PDEN, 34173a7f0a9SMikko Perttunen EMC_RW2PDEN, 34273a7f0a9SMikko Perttunen EMC_TXSR, 34373a7f0a9SMikko Perttunen EMC_TXSRDLL, 34473a7f0a9SMikko Perttunen EMC_TCKE, 34573a7f0a9SMikko Perttunen EMC_TCKESR, 34673a7f0a9SMikko Perttunen EMC_TPD, 34773a7f0a9SMikko Perttunen EMC_TFAW, 34873a7f0a9SMikko Perttunen EMC_TRPAB, 34973a7f0a9SMikko Perttunen EMC_TCLKSTABLE, 35073a7f0a9SMikko Perttunen EMC_TCLKSTOP, 35173a7f0a9SMikko Perttunen EMC_TREFBW, 35273a7f0a9SMikko Perttunen EMC_FBIO_CFG6, 35373a7f0a9SMikko Perttunen EMC_ODT_WRITE, 35473a7f0a9SMikko Perttunen EMC_ODT_READ, 35573a7f0a9SMikko Perttunen EMC_FBIO_CFG5, 35673a7f0a9SMikko Perttunen EMC_CFG_DIG_DLL, 35773a7f0a9SMikko Perttunen EMC_CFG_DIG_DLL_PERIOD, 35873a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS0, 35973a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS1, 36073a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS2, 36173a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS3, 36273a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS4, 36373a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS5, 36473a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS6, 36573a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS7, 36673a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS8, 36773a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS9, 36873a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS10, 36973a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS11, 37073a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS12, 37173a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS13, 37273a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS14, 37373a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS15, 37473a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE0, 37573a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE1, 37673a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE2, 37773a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE3, 37873a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE4, 37973a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE5, 38073a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE6, 38173a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE7, 38273a7f0a9SMikko Perttunen EMC_DLL_XFORM_ADDR0, 38373a7f0a9SMikko Perttunen EMC_DLL_XFORM_ADDR1, 38473a7f0a9SMikko Perttunen EMC_DLL_XFORM_ADDR2, 38573a7f0a9SMikko Perttunen EMC_DLL_XFORM_ADDR3, 38673a7f0a9SMikko Perttunen EMC_DLL_XFORM_ADDR4, 38773a7f0a9SMikko Perttunen EMC_DLL_XFORM_ADDR5, 38873a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE8, 38973a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE9, 39073a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE10, 39173a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE11, 39273a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE12, 39373a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE13, 39473a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE14, 39573a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE15, 39673a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS0, 39773a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS1, 39873a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS2, 39973a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS3, 40073a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS4, 40173a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS5, 40273a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS6, 40373a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS7, 40473a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS8, 40573a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS9, 40673a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS10, 40773a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS11, 40873a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS12, 40973a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS13, 41073a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS14, 41173a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS15, 41273a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ0, 41373a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ1, 41473a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ2, 41573a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ3, 41673a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ4, 41773a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ5, 41873a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ6, 41973a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ7, 42073a7f0a9SMikko Perttunen EMC_XM2CMDPADCTRL, 42173a7f0a9SMikko Perttunen EMC_XM2CMDPADCTRL4, 42273a7f0a9SMikko Perttunen EMC_XM2CMDPADCTRL5, 42373a7f0a9SMikko Perttunen EMC_XM2DQPADCTRL2, 42473a7f0a9SMikko Perttunen EMC_XM2DQPADCTRL3, 42573a7f0a9SMikko Perttunen EMC_XM2CLKPADCTRL, 42673a7f0a9SMikko Perttunen EMC_XM2CLKPADCTRL2, 42773a7f0a9SMikko Perttunen EMC_XM2COMPPADCTRL, 42873a7f0a9SMikko Perttunen EMC_XM2VTTGENPADCTRL, 42973a7f0a9SMikko Perttunen EMC_XM2VTTGENPADCTRL2, 43073a7f0a9SMikko Perttunen EMC_XM2VTTGENPADCTRL3, 43173a7f0a9SMikko Perttunen EMC_XM2DQSPADCTRL3, 43273a7f0a9SMikko Perttunen EMC_XM2DQSPADCTRL4, 43373a7f0a9SMikko Perttunen EMC_XM2DQSPADCTRL5, 43473a7f0a9SMikko Perttunen EMC_XM2DQSPADCTRL6, 43573a7f0a9SMikko Perttunen EMC_DSR_VTTGEN_DRV, 43673a7f0a9SMikko Perttunen EMC_TXDSRVTTGEN, 43773a7f0a9SMikko Perttunen EMC_FBIO_SPARE, 43873a7f0a9SMikko Perttunen EMC_ZCAL_WAIT_CNT, 43973a7f0a9SMikko Perttunen EMC_MRS_WAIT_CNT2, 44073a7f0a9SMikko Perttunen EMC_CTT, 44173a7f0a9SMikko Perttunen EMC_CTT_DURATION, 44273a7f0a9SMikko Perttunen EMC_CFG_PIPE, 44373a7f0a9SMikko Perttunen EMC_DYN_SELF_REF_CONTROL, 44473a7f0a9SMikko Perttunen EMC_QPOP 44573a7f0a9SMikko Perttunen }; 44673a7f0a9SMikko Perttunen 44773a7f0a9SMikko Perttunen struct emc_timing { 44873a7f0a9SMikko Perttunen unsigned long rate; 44973a7f0a9SMikko Perttunen 45073a7f0a9SMikko Perttunen u32 emc_burst_data[ARRAY_SIZE(emc_burst_regs)]; 45173a7f0a9SMikko Perttunen 45273a7f0a9SMikko Perttunen u32 emc_auto_cal_config; 45373a7f0a9SMikko Perttunen u32 emc_auto_cal_config2; 45473a7f0a9SMikko Perttunen u32 emc_auto_cal_config3; 45573a7f0a9SMikko Perttunen u32 emc_auto_cal_interval; 45673a7f0a9SMikko Perttunen u32 emc_bgbias_ctl0; 45773a7f0a9SMikko Perttunen u32 emc_cfg; 45873a7f0a9SMikko Perttunen u32 emc_cfg_2; 45973a7f0a9SMikko Perttunen u32 emc_ctt_term_ctrl; 46073a7f0a9SMikko Perttunen u32 emc_mode_1; 46173a7f0a9SMikko Perttunen u32 emc_mode_2; 46273a7f0a9SMikko Perttunen u32 emc_mode_4; 46373a7f0a9SMikko Perttunen u32 emc_mode_reset; 46473a7f0a9SMikko Perttunen u32 emc_mrs_wait_cnt; 46573a7f0a9SMikko Perttunen u32 emc_sel_dpd_ctrl; 46673a7f0a9SMikko Perttunen u32 emc_xm2dqspadctrl2; 46773a7f0a9SMikko Perttunen u32 emc_zcal_cnt_long; 46873a7f0a9SMikko Perttunen u32 emc_zcal_interval; 46973a7f0a9SMikko Perttunen }; 47073a7f0a9SMikko Perttunen 47173a7f0a9SMikko Perttunen struct tegra_emc { 47273a7f0a9SMikko Perttunen struct device *dev; 47373a7f0a9SMikko Perttunen 47473a7f0a9SMikko Perttunen struct tegra_mc *mc; 47573a7f0a9SMikko Perttunen 47673a7f0a9SMikko Perttunen void __iomem *regs; 47773a7f0a9SMikko Perttunen 47873a7f0a9SMikko Perttunen enum emc_dram_type dram_type; 47973a7f0a9SMikko Perttunen unsigned int dram_num; 48073a7f0a9SMikko Perttunen 48173a7f0a9SMikko Perttunen struct emc_timing last_timing; 48273a7f0a9SMikko Perttunen struct emc_timing *timings; 48373a7f0a9SMikko Perttunen unsigned int num_timings; 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) { 89173a7f0a9SMikko Perttunen dev_err(emc->dev, "timing %s: failed to read rate: %d\n", 89273a7f0a9SMikko Perttunen node->name, 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, 90373a7f0a9SMikko Perttunen "timing %s: failed to read emc burst data: %d\n", 90473a7f0a9SMikko Perttunen node->name, 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) { \ 91173a7f0a9SMikko Perttunen dev_err(emc->dev, "timing %s: failed to read " #prop ": %d\n", \ 91273a7f0a9SMikko Perttunen node->name, 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); 97373a7f0a9SMikko Perttunen if (err) 97473a7f0a9SMikko Perttunen return err; 97573a7f0a9SMikko Perttunen } 97673a7f0a9SMikko Perttunen 97773a7f0a9SMikko Perttunen sort(emc->timings, emc->num_timings, sizeof(*timing), cmp_timings, 97873a7f0a9SMikko Perttunen NULL); 97973a7f0a9SMikko Perttunen 98073a7f0a9SMikko Perttunen return 0; 98173a7f0a9SMikko Perttunen } 98273a7f0a9SMikko Perttunen 98373a7f0a9SMikko Perttunen static const struct of_device_id tegra_emc_of_match[] = { 98473a7f0a9SMikko Perttunen { .compatible = "nvidia,tegra124-emc" }, 98573a7f0a9SMikko Perttunen {} 98673a7f0a9SMikko Perttunen }; 98773a7f0a9SMikko Perttunen 98873a7f0a9SMikko Perttunen static struct device_node * 98973a7f0a9SMikko Perttunen tegra_emc_find_node_by_ram_code(struct device_node *node, u32 ram_code) 99073a7f0a9SMikko Perttunen { 99173a7f0a9SMikko Perttunen struct device_node *np; 99273a7f0a9SMikko Perttunen int err; 99373a7f0a9SMikko Perttunen 99473a7f0a9SMikko Perttunen for_each_child_of_node(node, np) { 99573a7f0a9SMikko Perttunen u32 value; 99673a7f0a9SMikko Perttunen 99773a7f0a9SMikko Perttunen err = of_property_read_u32(np, "nvidia,ram-code", &value); 99873a7f0a9SMikko Perttunen if (err || (value != ram_code)) { 99973a7f0a9SMikko Perttunen of_node_put(np); 100073a7f0a9SMikko Perttunen continue; 100173a7f0a9SMikko Perttunen } 100273a7f0a9SMikko Perttunen 100373a7f0a9SMikko Perttunen return np; 100473a7f0a9SMikko Perttunen } 100573a7f0a9SMikko Perttunen 100673a7f0a9SMikko Perttunen return NULL; 100773a7f0a9SMikko Perttunen } 100873a7f0a9SMikko Perttunen 10099c77a81fSMikko Perttunen /* Debugfs entry */ 10109c77a81fSMikko Perttunen 10119c77a81fSMikko Perttunen static int emc_debug_rate_get(void *data, u64 *rate) 10129c77a81fSMikko Perttunen { 10139c77a81fSMikko Perttunen struct clk *c = data; 10149c77a81fSMikko Perttunen 10159c77a81fSMikko Perttunen *rate = clk_get_rate(c); 10169c77a81fSMikko Perttunen 10179c77a81fSMikko Perttunen return 0; 10189c77a81fSMikko Perttunen } 10199c77a81fSMikko Perttunen 10209c77a81fSMikko Perttunen static int emc_debug_rate_set(void *data, u64 rate) 10219c77a81fSMikko Perttunen { 10229c77a81fSMikko Perttunen struct clk *c = data; 10239c77a81fSMikko Perttunen 10249c77a81fSMikko Perttunen return clk_set_rate(c, rate); 10259c77a81fSMikko Perttunen } 10269c77a81fSMikko Perttunen 10279c77a81fSMikko Perttunen DEFINE_SIMPLE_ATTRIBUTE(emc_debug_rate_fops, emc_debug_rate_get, 10289c77a81fSMikko Perttunen emc_debug_rate_set, "%lld\n"); 10299c77a81fSMikko Perttunen 1030*30a636f9SThierry Reding static int emc_debug_supported_rates_show(struct seq_file *s, void *data) 1031*30a636f9SThierry Reding { 1032*30a636f9SThierry Reding struct tegra_emc *emc = s->private; 1033*30a636f9SThierry Reding const char *prefix = ""; 1034*30a636f9SThierry Reding unsigned int i; 1035*30a636f9SThierry Reding 1036*30a636f9SThierry Reding for (i = 0; i < emc->num_timings; i++) { 1037*30a636f9SThierry Reding struct emc_timing *timing = &emc->timings[i]; 1038*30a636f9SThierry Reding 1039*30a636f9SThierry Reding seq_printf(s, "%s%lu", prefix, timing->rate); 1040*30a636f9SThierry Reding 1041*30a636f9SThierry Reding prefix = " "; 1042*30a636f9SThierry Reding } 1043*30a636f9SThierry Reding 1044*30a636f9SThierry Reding seq_puts(s, "\n"); 1045*30a636f9SThierry Reding 1046*30a636f9SThierry Reding return 0; 1047*30a636f9SThierry Reding } 1048*30a636f9SThierry Reding 1049*30a636f9SThierry Reding static int emc_debug_supported_rates_open(struct inode *inode, 1050*30a636f9SThierry Reding struct file *file) 1051*30a636f9SThierry Reding { 1052*30a636f9SThierry Reding return single_open(file, emc_debug_supported_rates_show, 1053*30a636f9SThierry Reding inode->i_private); 1054*30a636f9SThierry Reding } 1055*30a636f9SThierry Reding 1056*30a636f9SThierry Reding static const struct file_operations emc_debug_supported_rates_fops = { 1057*30a636f9SThierry Reding .open = emc_debug_supported_rates_open, 1058*30a636f9SThierry Reding .read = seq_read, 1059*30a636f9SThierry Reding .llseek = seq_lseek, 1060*30a636f9SThierry Reding .release = single_release, 1061*30a636f9SThierry Reding }; 1062*30a636f9SThierry Reding 1063*30a636f9SThierry Reding static void emc_debugfs_init(struct device *dev, struct tegra_emc *emc) 10649c77a81fSMikko Perttunen { 10659c77a81fSMikko Perttunen struct dentry *root, *file; 10669c77a81fSMikko Perttunen struct clk *clk; 10679c77a81fSMikko Perttunen 10689c77a81fSMikko Perttunen root = debugfs_create_dir("emc", NULL); 10699c77a81fSMikko Perttunen if (!root) { 10709c77a81fSMikko Perttunen dev_err(dev, "failed to create debugfs directory\n"); 10719c77a81fSMikko Perttunen return; 10729c77a81fSMikko Perttunen } 10739c77a81fSMikko Perttunen 10749c77a81fSMikko Perttunen clk = clk_get_sys("tegra-clk-debug", "emc"); 10759c77a81fSMikko Perttunen if (IS_ERR(clk)) { 10769c77a81fSMikko Perttunen dev_err(dev, "failed to get debug clock: %ld\n", PTR_ERR(clk)); 10779c77a81fSMikko Perttunen return; 10789c77a81fSMikko Perttunen } 10799c77a81fSMikko Perttunen 10809c77a81fSMikko Perttunen file = debugfs_create_file("rate", S_IRUGO | S_IWUSR, root, clk, 10819c77a81fSMikko Perttunen &emc_debug_rate_fops); 10829c77a81fSMikko Perttunen if (!file) 10839c77a81fSMikko Perttunen dev_err(dev, "failed to create debugfs entry\n"); 1084*30a636f9SThierry Reding 1085*30a636f9SThierry Reding file = debugfs_create_file("supported_rates", S_IRUGO, root, emc, 1086*30a636f9SThierry Reding &emc_debug_supported_rates_fops); 1087*30a636f9SThierry Reding if (!file) 1088*30a636f9SThierry Reding dev_err(dev, "failed to create debugfs entry\n"); 10899c77a81fSMikko Perttunen } 10909c77a81fSMikko Perttunen 109173a7f0a9SMikko Perttunen static int tegra_emc_probe(struct platform_device *pdev) 109273a7f0a9SMikko Perttunen { 109373a7f0a9SMikko Perttunen struct platform_device *mc; 109473a7f0a9SMikko Perttunen struct device_node *np; 109573a7f0a9SMikko Perttunen struct tegra_emc *emc; 109673a7f0a9SMikko Perttunen struct resource *res; 109773a7f0a9SMikko Perttunen u32 ram_code; 109873a7f0a9SMikko Perttunen int err; 109973a7f0a9SMikko Perttunen 110073a7f0a9SMikko Perttunen emc = devm_kzalloc(&pdev->dev, sizeof(*emc), GFP_KERNEL); 110173a7f0a9SMikko Perttunen if (!emc) 110273a7f0a9SMikko Perttunen return -ENOMEM; 110373a7f0a9SMikko Perttunen 110473a7f0a9SMikko Perttunen emc->dev = &pdev->dev; 110573a7f0a9SMikko Perttunen 110673a7f0a9SMikko Perttunen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 110773a7f0a9SMikko Perttunen emc->regs = devm_ioremap_resource(&pdev->dev, res); 110873a7f0a9SMikko Perttunen if (IS_ERR(emc->regs)) 110973a7f0a9SMikko Perttunen return PTR_ERR(emc->regs); 111073a7f0a9SMikko Perttunen 111173a7f0a9SMikko Perttunen np = of_parse_phandle(pdev->dev.of_node, "nvidia,memory-controller", 0); 111273a7f0a9SMikko Perttunen if (!np) { 111373a7f0a9SMikko Perttunen dev_err(&pdev->dev, "could not get memory controller\n"); 111473a7f0a9SMikko Perttunen return -ENOENT; 111573a7f0a9SMikko Perttunen } 111673a7f0a9SMikko Perttunen 111773a7f0a9SMikko Perttunen mc = of_find_device_by_node(np); 111873a7f0a9SMikko Perttunen if (!mc) 111973a7f0a9SMikko Perttunen return -ENOENT; 112073a7f0a9SMikko Perttunen 112173a7f0a9SMikko Perttunen of_node_put(np); 112273a7f0a9SMikko Perttunen 112373a7f0a9SMikko Perttunen emc->mc = platform_get_drvdata(mc); 112473a7f0a9SMikko Perttunen if (!emc->mc) 112573a7f0a9SMikko Perttunen return -EPROBE_DEFER; 112673a7f0a9SMikko Perttunen 112773a7f0a9SMikko Perttunen ram_code = tegra_read_ram_code(); 112873a7f0a9SMikko Perttunen 112973a7f0a9SMikko Perttunen np = tegra_emc_find_node_by_ram_code(pdev->dev.of_node, ram_code); 113073a7f0a9SMikko Perttunen if (!np) { 113173a7f0a9SMikko Perttunen dev_err(&pdev->dev, 113273a7f0a9SMikko Perttunen "no memory timings for RAM code %u found in DT\n", 113373a7f0a9SMikko Perttunen ram_code); 113473a7f0a9SMikko Perttunen return -ENOENT; 113573a7f0a9SMikko Perttunen } 113673a7f0a9SMikko Perttunen 113773a7f0a9SMikko Perttunen err = tegra_emc_load_timings_from_dt(emc, np); 113873a7f0a9SMikko Perttunen 113973a7f0a9SMikko Perttunen of_node_put(np); 114073a7f0a9SMikko Perttunen 114173a7f0a9SMikko Perttunen if (err) 114273a7f0a9SMikko Perttunen return err; 114373a7f0a9SMikko Perttunen 114473a7f0a9SMikko Perttunen if (emc->num_timings == 0) { 114573a7f0a9SMikko Perttunen dev_err(&pdev->dev, 114673a7f0a9SMikko Perttunen "no memory timings for RAM code %u registered\n", 114773a7f0a9SMikko Perttunen ram_code); 114873a7f0a9SMikko Perttunen return -ENOENT; 114973a7f0a9SMikko Perttunen } 115073a7f0a9SMikko Perttunen 115173a7f0a9SMikko Perttunen err = emc_init(emc); 115273a7f0a9SMikko Perttunen if (err) { 115373a7f0a9SMikko Perttunen dev_err(&pdev->dev, "EMC initialization failed: %d\n", err); 115473a7f0a9SMikko Perttunen return err; 115573a7f0a9SMikko Perttunen } 115673a7f0a9SMikko Perttunen 115773a7f0a9SMikko Perttunen platform_set_drvdata(pdev, emc); 115873a7f0a9SMikko Perttunen 11599c77a81fSMikko Perttunen if (IS_ENABLED(CONFIG_DEBUG_FS)) 1160*30a636f9SThierry Reding emc_debugfs_init(&pdev->dev, emc); 11619c77a81fSMikko Perttunen 116273a7f0a9SMikko Perttunen return 0; 116373a7f0a9SMikko Perttunen }; 116473a7f0a9SMikko Perttunen 116573a7f0a9SMikko Perttunen static struct platform_driver tegra_emc_driver = { 116673a7f0a9SMikko Perttunen .probe = tegra_emc_probe, 116773a7f0a9SMikko Perttunen .driver = { 116873a7f0a9SMikko Perttunen .name = "tegra-emc", 116973a7f0a9SMikko Perttunen .of_match_table = tegra_emc_of_match, 117073a7f0a9SMikko Perttunen .suppress_bind_attrs = true, 117173a7f0a9SMikko Perttunen }, 117273a7f0a9SMikko Perttunen }; 117373a7f0a9SMikko Perttunen 117473a7f0a9SMikko Perttunen static int tegra_emc_init(void) 117573a7f0a9SMikko Perttunen { 117673a7f0a9SMikko Perttunen return platform_driver_register(&tegra_emc_driver); 117773a7f0a9SMikko Perttunen } 117873a7f0a9SMikko Perttunen subsys_initcall(tegra_emc_init); 1179