1*73a7f0a9SMikko Perttunen /* 2*73a7f0a9SMikko Perttunen * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. 3*73a7f0a9SMikko Perttunen * 4*73a7f0a9SMikko Perttunen * Author: 5*73a7f0a9SMikko Perttunen * Mikko Perttunen <mperttunen@nvidia.com> 6*73a7f0a9SMikko Perttunen * 7*73a7f0a9SMikko Perttunen * This software is licensed under the terms of the GNU General Public 8*73a7f0a9SMikko Perttunen * License version 2, as published by the Free Software Foundation, and 9*73a7f0a9SMikko Perttunen * may be copied, distributed, and modified under those terms. 10*73a7f0a9SMikko Perttunen * 11*73a7f0a9SMikko Perttunen * This program is distributed in the hope that it will be useful, 12*73a7f0a9SMikko Perttunen * but WITHOUT ANY WARRANTY; without even the implied warranty of 13*73a7f0a9SMikko Perttunen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*73a7f0a9SMikko Perttunen * GNU General Public License for more details. 15*73a7f0a9SMikko Perttunen * 16*73a7f0a9SMikko Perttunen */ 17*73a7f0a9SMikko Perttunen 18*73a7f0a9SMikko Perttunen #include <linux/clk-provider.h> 19*73a7f0a9SMikko Perttunen #include <linux/clk.h> 20*73a7f0a9SMikko Perttunen #include <linux/clkdev.h> 21*73a7f0a9SMikko Perttunen #include <linux/delay.h> 22*73a7f0a9SMikko Perttunen #include <linux/of_address.h> 23*73a7f0a9SMikko Perttunen #include <linux/of_platform.h> 24*73a7f0a9SMikko Perttunen #include <linux/platform_device.h> 25*73a7f0a9SMikko Perttunen #include <linux/sort.h> 26*73a7f0a9SMikko Perttunen #include <linux/string.h> 27*73a7f0a9SMikko Perttunen 28*73a7f0a9SMikko Perttunen #include <soc/tegra/emc.h> 29*73a7f0a9SMikko Perttunen #include <soc/tegra/fuse.h> 30*73a7f0a9SMikko Perttunen #include <soc/tegra/mc.h> 31*73a7f0a9SMikko Perttunen 32*73a7f0a9SMikko Perttunen #define EMC_FBIO_CFG5 0x104 33*73a7f0a9SMikko Perttunen #define EMC_FBIO_CFG5_DRAM_TYPE_MASK 0x3 34*73a7f0a9SMikko Perttunen #define EMC_FBIO_CFG5_DRAM_TYPE_SHIFT 0 35*73a7f0a9SMikko Perttunen 36*73a7f0a9SMikko Perttunen #define EMC_INTSTATUS 0x0 37*73a7f0a9SMikko Perttunen #define EMC_INTSTATUS_CLKCHANGE_COMPLETE BIT(4) 38*73a7f0a9SMikko Perttunen 39*73a7f0a9SMikko Perttunen #define EMC_CFG 0xc 40*73a7f0a9SMikko Perttunen #define EMC_CFG_DRAM_CLKSTOP_PD BIT(31) 41*73a7f0a9SMikko Perttunen #define EMC_CFG_DRAM_CLKSTOP_SR BIT(30) 42*73a7f0a9SMikko Perttunen #define EMC_CFG_DRAM_ACPD BIT(29) 43*73a7f0a9SMikko Perttunen #define EMC_CFG_DYN_SREF BIT(28) 44*73a7f0a9SMikko Perttunen #define EMC_CFG_PWR_MASK ((0xF << 28) | BIT(18)) 45*73a7f0a9SMikko Perttunen #define EMC_CFG_DSR_VTTGEN_DRV_EN BIT(18) 46*73a7f0a9SMikko Perttunen 47*73a7f0a9SMikko Perttunen #define EMC_REFCTRL 0x20 48*73a7f0a9SMikko Perttunen #define EMC_REFCTRL_DEV_SEL_SHIFT 0 49*73a7f0a9SMikko Perttunen #define EMC_REFCTRL_ENABLE BIT(31) 50*73a7f0a9SMikko Perttunen 51*73a7f0a9SMikko Perttunen #define EMC_TIMING_CONTROL 0x28 52*73a7f0a9SMikko Perttunen #define EMC_RC 0x2c 53*73a7f0a9SMikko Perttunen #define EMC_RFC 0x30 54*73a7f0a9SMikko Perttunen #define EMC_RAS 0x34 55*73a7f0a9SMikko Perttunen #define EMC_RP 0x38 56*73a7f0a9SMikko Perttunen #define EMC_R2W 0x3c 57*73a7f0a9SMikko Perttunen #define EMC_W2R 0x40 58*73a7f0a9SMikko Perttunen #define EMC_R2P 0x44 59*73a7f0a9SMikko Perttunen #define EMC_W2P 0x48 60*73a7f0a9SMikko Perttunen #define EMC_RD_RCD 0x4c 61*73a7f0a9SMikko Perttunen #define EMC_WR_RCD 0x50 62*73a7f0a9SMikko Perttunen #define EMC_RRD 0x54 63*73a7f0a9SMikko Perttunen #define EMC_REXT 0x58 64*73a7f0a9SMikko Perttunen #define EMC_WDV 0x5c 65*73a7f0a9SMikko Perttunen #define EMC_QUSE 0x60 66*73a7f0a9SMikko Perttunen #define EMC_QRST 0x64 67*73a7f0a9SMikko Perttunen #define EMC_QSAFE 0x68 68*73a7f0a9SMikko Perttunen #define EMC_RDV 0x6c 69*73a7f0a9SMikko Perttunen #define EMC_REFRESH 0x70 70*73a7f0a9SMikko Perttunen #define EMC_BURST_REFRESH_NUM 0x74 71*73a7f0a9SMikko Perttunen #define EMC_PDEX2WR 0x78 72*73a7f0a9SMikko Perttunen #define EMC_PDEX2RD 0x7c 73*73a7f0a9SMikko Perttunen #define EMC_PCHG2PDEN 0x80 74*73a7f0a9SMikko Perttunen #define EMC_ACT2PDEN 0x84 75*73a7f0a9SMikko Perttunen #define EMC_AR2PDEN 0x88 76*73a7f0a9SMikko Perttunen #define EMC_RW2PDEN 0x8c 77*73a7f0a9SMikko Perttunen #define EMC_TXSR 0x90 78*73a7f0a9SMikko Perttunen #define EMC_TCKE 0x94 79*73a7f0a9SMikko Perttunen #define EMC_TFAW 0x98 80*73a7f0a9SMikko Perttunen #define EMC_TRPAB 0x9c 81*73a7f0a9SMikko Perttunen #define EMC_TCLKSTABLE 0xa0 82*73a7f0a9SMikko Perttunen #define EMC_TCLKSTOP 0xa4 83*73a7f0a9SMikko Perttunen #define EMC_TREFBW 0xa8 84*73a7f0a9SMikko Perttunen #define EMC_ODT_WRITE 0xb0 85*73a7f0a9SMikko Perttunen #define EMC_ODT_READ 0xb4 86*73a7f0a9SMikko Perttunen #define EMC_WEXT 0xb8 87*73a7f0a9SMikko Perttunen #define EMC_CTT 0xbc 88*73a7f0a9SMikko Perttunen #define EMC_RFC_SLR 0xc0 89*73a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT2 0xc4 90*73a7f0a9SMikko Perttunen 91*73a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT 0xc8 92*73a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT 0 93*73a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK \ 94*73a7f0a9SMikko Perttunen (0x3FF << EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT) 95*73a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT 16 96*73a7f0a9SMikko Perttunen #define EMC_MRS_WAIT_CNT_LONG_WAIT_MASK \ 97*73a7f0a9SMikko Perttunen (0x3FF << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT) 98*73a7f0a9SMikko Perttunen 99*73a7f0a9SMikko Perttunen #define EMC_MRS 0xcc 100*73a7f0a9SMikko Perttunen #define EMC_MODE_SET_DLL_RESET BIT(8) 101*73a7f0a9SMikko Perttunen #define EMC_MODE_SET_LONG_CNT BIT(26) 102*73a7f0a9SMikko Perttunen #define EMC_EMRS 0xd0 103*73a7f0a9SMikko Perttunen #define EMC_REF 0xd4 104*73a7f0a9SMikko Perttunen #define EMC_PRE 0xd8 105*73a7f0a9SMikko Perttunen 106*73a7f0a9SMikko Perttunen #define EMC_SELF_REF 0xe0 107*73a7f0a9SMikko Perttunen #define EMC_SELF_REF_CMD_ENABLED BIT(0) 108*73a7f0a9SMikko Perttunen #define EMC_SELF_REF_DEV_SEL_SHIFT 30 109*73a7f0a9SMikko Perttunen 110*73a7f0a9SMikko Perttunen #define EMC_MRW 0xe8 111*73a7f0a9SMikko Perttunen 112*73a7f0a9SMikko Perttunen #define EMC_MRR 0xec 113*73a7f0a9SMikko Perttunen #define EMC_MRR_MA_SHIFT 16 114*73a7f0a9SMikko Perttunen #define LPDDR2_MR4_TEMP_SHIFT 0 115*73a7f0a9SMikko Perttunen 116*73a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL3 0xf8 117*73a7f0a9SMikko Perttunen #define EMC_FBIO_SPARE 0x100 118*73a7f0a9SMikko Perttunen 119*73a7f0a9SMikko Perttunen #define EMC_FBIO_CFG6 0x114 120*73a7f0a9SMikko Perttunen #define EMC_EMRS2 0x12c 121*73a7f0a9SMikko Perttunen #define EMC_MRW2 0x134 122*73a7f0a9SMikko Perttunen #define EMC_MRW4 0x13c 123*73a7f0a9SMikko Perttunen #define EMC_EINPUT 0x14c 124*73a7f0a9SMikko Perttunen #define EMC_EINPUT_DURATION 0x150 125*73a7f0a9SMikko Perttunen #define EMC_PUTERM_EXTRA 0x154 126*73a7f0a9SMikko Perttunen #define EMC_TCKESR 0x158 127*73a7f0a9SMikko Perttunen #define EMC_TPD 0x15c 128*73a7f0a9SMikko Perttunen 129*73a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG 0x2a4 130*73a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG_AUTO_CAL_START BIT(31) 131*73a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_INTERVAL 0x2a8 132*73a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_STATUS 0x2ac 133*73a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_STATUS_ACTIVE BIT(31) 134*73a7f0a9SMikko Perttunen #define EMC_STATUS 0x2b4 135*73a7f0a9SMikko Perttunen #define EMC_STATUS_TIMING_UPDATE_STALLED BIT(23) 136*73a7f0a9SMikko Perttunen 137*73a7f0a9SMikko Perttunen #define EMC_CFG_2 0x2b8 138*73a7f0a9SMikko Perttunen #define EMC_CFG_2_MODE_SHIFT 0 139*73a7f0a9SMikko Perttunen #define EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR BIT(6) 140*73a7f0a9SMikko Perttunen 141*73a7f0a9SMikko Perttunen #define EMC_CFG_DIG_DLL 0x2bc 142*73a7f0a9SMikko Perttunen #define EMC_CFG_DIG_DLL_PERIOD 0x2c0 143*73a7f0a9SMikko Perttunen #define EMC_RDV_MASK 0x2cc 144*73a7f0a9SMikko Perttunen #define EMC_WDV_MASK 0x2d0 145*73a7f0a9SMikko Perttunen #define EMC_CTT_DURATION 0x2d8 146*73a7f0a9SMikko Perttunen #define EMC_CTT_TERM_CTRL 0x2dc 147*73a7f0a9SMikko Perttunen #define EMC_ZCAL_INTERVAL 0x2e0 148*73a7f0a9SMikko Perttunen #define EMC_ZCAL_WAIT_CNT 0x2e4 149*73a7f0a9SMikko Perttunen 150*73a7f0a9SMikko Perttunen #define EMC_ZQ_CAL 0x2ec 151*73a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_CMD BIT(0) 152*73a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_LONG BIT(4) 153*73a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_LONG_CMD_DEV0 \ 154*73a7f0a9SMikko Perttunen (DRAM_DEV_SEL_0 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD) 155*73a7f0a9SMikko Perttunen #define EMC_ZQ_CAL_LONG_CMD_DEV1 \ 156*73a7f0a9SMikko Perttunen (DRAM_DEV_SEL_1 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD) 157*73a7f0a9SMikko Perttunen 158*73a7f0a9SMikko Perttunen #define EMC_XM2CMDPADCTRL 0x2f0 159*73a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL 0x2f8 160*73a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL2 0x2fc 161*73a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE BIT(0) 162*73a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL2_VREF_ENABLE BIT(5) 163*73a7f0a9SMikko Perttunen #define EMC_XM2DQPADCTRL 0x300 164*73a7f0a9SMikko Perttunen #define EMC_XM2DQPADCTRL2 0x304 165*73a7f0a9SMikko Perttunen #define EMC_XM2CLKPADCTRL 0x308 166*73a7f0a9SMikko Perttunen #define EMC_XM2COMPPADCTRL 0x30c 167*73a7f0a9SMikko Perttunen #define EMC_XM2VTTGENPADCTRL 0x310 168*73a7f0a9SMikko Perttunen #define EMC_XM2VTTGENPADCTRL2 0x314 169*73a7f0a9SMikko Perttunen #define EMC_XM2VTTGENPADCTRL3 0x318 170*73a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL4 0x320 171*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS0 0x328 172*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS1 0x32c 173*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS2 0x330 174*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS3 0x334 175*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS4 0x338 176*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS5 0x33c 177*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS6 0x340 178*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS7 0x344 179*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE0 0x348 180*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE1 0x34c 181*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE2 0x350 182*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE3 0x354 183*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE4 0x358 184*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE5 0x35c 185*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE6 0x360 186*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE7 0x364 187*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ0 0x368 188*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ1 0x36c 189*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ2 0x370 190*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ3 0x374 191*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS0 0x3a8 192*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS1 0x3ac 193*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS2 0x3b0 194*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS3 0x3b4 195*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS4 0x3b8 196*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS5 0x3bc 197*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS6 0x3c0 198*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS7 0x3c4 199*73a7f0a9SMikko Perttunen #define EMC_STALL_THEN_EXE_AFTER_CLKCHANGE 0x3cc 200*73a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL 0x3d8 201*73a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_DATA_SEL_DPD BIT(8) 202*73a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_ODT_SEL_DPD BIT(5) 203*73a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_RESET_SEL_DPD BIT(4) 204*73a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_CA_SEL_DPD BIT(3) 205*73a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_CLK_SEL_DPD BIT(2) 206*73a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_DDR3_MASK \ 207*73a7f0a9SMikko Perttunen ((0xf << 2) | BIT(8)) 208*73a7f0a9SMikko Perttunen #define EMC_SEL_DPD_CTRL_MASK \ 209*73a7f0a9SMikko Perttunen ((0x3 << 2) | BIT(5) | BIT(8)) 210*73a7f0a9SMikko Perttunen #define EMC_PRE_REFRESH_REQ_CNT 0x3dc 211*73a7f0a9SMikko Perttunen #define EMC_DYN_SELF_REF_CONTROL 0x3e0 212*73a7f0a9SMikko Perttunen #define EMC_TXSRDLL 0x3e4 213*73a7f0a9SMikko Perttunen #define EMC_CCFIFO_ADDR 0x3e8 214*73a7f0a9SMikko Perttunen #define EMC_CCFIFO_DATA 0x3ec 215*73a7f0a9SMikko Perttunen #define EMC_CCFIFO_STATUS 0x3f0 216*73a7f0a9SMikko Perttunen #define EMC_CDB_CNTL_1 0x3f4 217*73a7f0a9SMikko Perttunen #define EMC_CDB_CNTL_2 0x3f8 218*73a7f0a9SMikko Perttunen #define EMC_XM2CLKPADCTRL2 0x3fc 219*73a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG2 0x458 220*73a7f0a9SMikko Perttunen #define EMC_AUTO_CAL_CONFIG3 0x45c 221*73a7f0a9SMikko Perttunen #define EMC_IBDLY 0x468 222*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR0 0x46c 223*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR1 0x470 224*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR2 0x474 225*73a7f0a9SMikko Perttunen #define EMC_DSR_VTTGEN_DRV 0x47c 226*73a7f0a9SMikko Perttunen #define EMC_TXDSRVTTGEN 0x480 227*73a7f0a9SMikko Perttunen #define EMC_XM2CMDPADCTRL4 0x484 228*73a7f0a9SMikko Perttunen #define EMC_XM2CMDPADCTRL5 0x488 229*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS8 0x4a0 230*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS9 0x4a4 231*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS10 0x4a8 232*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS11 0x4ac 233*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS12 0x4b0 234*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS13 0x4b4 235*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS14 0x4b8 236*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQS15 0x4bc 237*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE8 0x4c0 238*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE9 0x4c4 239*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE10 0x4c8 240*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE11 0x4cc 241*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE12 0x4d0 242*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE13 0x4d4 243*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE14 0x4d8 244*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_QUSE15 0x4dc 245*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ4 0x4e0 246*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ5 0x4e4 247*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ6 0x4e8 248*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_DQ7 0x4ec 249*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS8 0x520 250*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS9 0x524 251*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS10 0x528 252*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS11 0x52c 253*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS12 0x530 254*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS13 0x534 255*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS14 0x538 256*73a7f0a9SMikko Perttunen #define EMC_DLI_TRIM_TXDQS15 0x53c 257*73a7f0a9SMikko Perttunen #define EMC_CDB_CNTL_3 0x540 258*73a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL5 0x544 259*73a7f0a9SMikko Perttunen #define EMC_XM2DQSPADCTRL6 0x548 260*73a7f0a9SMikko Perttunen #define EMC_XM2DQPADCTRL3 0x54c 261*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR3 0x550 262*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR4 0x554 263*73a7f0a9SMikko Perttunen #define EMC_DLL_XFORM_ADDR5 0x558 264*73a7f0a9SMikko Perttunen #define EMC_CFG_PIPE 0x560 265*73a7f0a9SMikko Perttunen #define EMC_QPOP 0x564 266*73a7f0a9SMikko Perttunen #define EMC_QUSE_WIDTH 0x568 267*73a7f0a9SMikko Perttunen #define EMC_PUTERM_WIDTH 0x56c 268*73a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0 0x570 269*73a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX BIT(3) 270*73a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN BIT(2) 271*73a7f0a9SMikko Perttunen #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD BIT(1) 272*73a7f0a9SMikko Perttunen #define EMC_PUTERM_ADJ 0x574 273*73a7f0a9SMikko Perttunen 274*73a7f0a9SMikko Perttunen #define DRAM_DEV_SEL_ALL 0 275*73a7f0a9SMikko Perttunen #define DRAM_DEV_SEL_0 (2 << 30) 276*73a7f0a9SMikko Perttunen #define DRAM_DEV_SEL_1 (1 << 30) 277*73a7f0a9SMikko Perttunen 278*73a7f0a9SMikko Perttunen #define EMC_CFG_POWER_FEATURES_MASK \ 279*73a7f0a9SMikko Perttunen (EMC_CFG_DYN_SREF | EMC_CFG_DRAM_ACPD | EMC_CFG_DRAM_CLKSTOP_SR | \ 280*73a7f0a9SMikko Perttunen EMC_CFG_DRAM_CLKSTOP_PD | EMC_CFG_DSR_VTTGEN_DRV_EN) 281*73a7f0a9SMikko Perttunen #define EMC_REFCTRL_DEV_SEL(n) (((n > 1) ? 0 : 2) << EMC_REFCTRL_DEV_SEL_SHIFT) 282*73a7f0a9SMikko Perttunen #define EMC_DRAM_DEV_SEL(n) ((n > 1) ? DRAM_DEV_SEL_ALL : DRAM_DEV_SEL_0) 283*73a7f0a9SMikko Perttunen 284*73a7f0a9SMikko Perttunen /* Maximum amount of time in us. to wait for changes to become effective */ 285*73a7f0a9SMikko Perttunen #define EMC_STATUS_UPDATE_TIMEOUT 1000 286*73a7f0a9SMikko Perttunen 287*73a7f0a9SMikko Perttunen enum emc_dram_type { 288*73a7f0a9SMikko Perttunen DRAM_TYPE_DDR3 = 0, 289*73a7f0a9SMikko Perttunen DRAM_TYPE_DDR1 = 1, 290*73a7f0a9SMikko Perttunen DRAM_TYPE_LPDDR3 = 2, 291*73a7f0a9SMikko Perttunen DRAM_TYPE_DDR2 = 3 292*73a7f0a9SMikko Perttunen }; 293*73a7f0a9SMikko Perttunen 294*73a7f0a9SMikko Perttunen enum emc_dll_change { 295*73a7f0a9SMikko Perttunen DLL_CHANGE_NONE, 296*73a7f0a9SMikko Perttunen DLL_CHANGE_ON, 297*73a7f0a9SMikko Perttunen DLL_CHANGE_OFF 298*73a7f0a9SMikko Perttunen }; 299*73a7f0a9SMikko Perttunen 300*73a7f0a9SMikko Perttunen static const unsigned long emc_burst_regs[] = { 301*73a7f0a9SMikko Perttunen EMC_RC, 302*73a7f0a9SMikko Perttunen EMC_RFC, 303*73a7f0a9SMikko Perttunen EMC_RFC_SLR, 304*73a7f0a9SMikko Perttunen EMC_RAS, 305*73a7f0a9SMikko Perttunen EMC_RP, 306*73a7f0a9SMikko Perttunen EMC_R2W, 307*73a7f0a9SMikko Perttunen EMC_W2R, 308*73a7f0a9SMikko Perttunen EMC_R2P, 309*73a7f0a9SMikko Perttunen EMC_W2P, 310*73a7f0a9SMikko Perttunen EMC_RD_RCD, 311*73a7f0a9SMikko Perttunen EMC_WR_RCD, 312*73a7f0a9SMikko Perttunen EMC_RRD, 313*73a7f0a9SMikko Perttunen EMC_REXT, 314*73a7f0a9SMikko Perttunen EMC_WEXT, 315*73a7f0a9SMikko Perttunen EMC_WDV, 316*73a7f0a9SMikko Perttunen EMC_WDV_MASK, 317*73a7f0a9SMikko Perttunen EMC_QUSE, 318*73a7f0a9SMikko Perttunen EMC_QUSE_WIDTH, 319*73a7f0a9SMikko Perttunen EMC_IBDLY, 320*73a7f0a9SMikko Perttunen EMC_EINPUT, 321*73a7f0a9SMikko Perttunen EMC_EINPUT_DURATION, 322*73a7f0a9SMikko Perttunen EMC_PUTERM_EXTRA, 323*73a7f0a9SMikko Perttunen EMC_PUTERM_WIDTH, 324*73a7f0a9SMikko Perttunen EMC_PUTERM_ADJ, 325*73a7f0a9SMikko Perttunen EMC_CDB_CNTL_1, 326*73a7f0a9SMikko Perttunen EMC_CDB_CNTL_2, 327*73a7f0a9SMikko Perttunen EMC_CDB_CNTL_3, 328*73a7f0a9SMikko Perttunen EMC_QRST, 329*73a7f0a9SMikko Perttunen EMC_QSAFE, 330*73a7f0a9SMikko Perttunen EMC_RDV, 331*73a7f0a9SMikko Perttunen EMC_RDV_MASK, 332*73a7f0a9SMikko Perttunen EMC_REFRESH, 333*73a7f0a9SMikko Perttunen EMC_BURST_REFRESH_NUM, 334*73a7f0a9SMikko Perttunen EMC_PRE_REFRESH_REQ_CNT, 335*73a7f0a9SMikko Perttunen EMC_PDEX2WR, 336*73a7f0a9SMikko Perttunen EMC_PDEX2RD, 337*73a7f0a9SMikko Perttunen EMC_PCHG2PDEN, 338*73a7f0a9SMikko Perttunen EMC_ACT2PDEN, 339*73a7f0a9SMikko Perttunen EMC_AR2PDEN, 340*73a7f0a9SMikko Perttunen EMC_RW2PDEN, 341*73a7f0a9SMikko Perttunen EMC_TXSR, 342*73a7f0a9SMikko Perttunen EMC_TXSRDLL, 343*73a7f0a9SMikko Perttunen EMC_TCKE, 344*73a7f0a9SMikko Perttunen EMC_TCKESR, 345*73a7f0a9SMikko Perttunen EMC_TPD, 346*73a7f0a9SMikko Perttunen EMC_TFAW, 347*73a7f0a9SMikko Perttunen EMC_TRPAB, 348*73a7f0a9SMikko Perttunen EMC_TCLKSTABLE, 349*73a7f0a9SMikko Perttunen EMC_TCLKSTOP, 350*73a7f0a9SMikko Perttunen EMC_TREFBW, 351*73a7f0a9SMikko Perttunen EMC_FBIO_CFG6, 352*73a7f0a9SMikko Perttunen EMC_ODT_WRITE, 353*73a7f0a9SMikko Perttunen EMC_ODT_READ, 354*73a7f0a9SMikko Perttunen EMC_FBIO_CFG5, 355*73a7f0a9SMikko Perttunen EMC_CFG_DIG_DLL, 356*73a7f0a9SMikko Perttunen EMC_CFG_DIG_DLL_PERIOD, 357*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS0, 358*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS1, 359*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS2, 360*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS3, 361*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS4, 362*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS5, 363*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS6, 364*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS7, 365*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS8, 366*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS9, 367*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS10, 368*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS11, 369*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS12, 370*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS13, 371*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS14, 372*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQS15, 373*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE0, 374*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE1, 375*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE2, 376*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE3, 377*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE4, 378*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE5, 379*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE6, 380*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE7, 381*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_ADDR0, 382*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_ADDR1, 383*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_ADDR2, 384*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_ADDR3, 385*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_ADDR4, 386*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_ADDR5, 387*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE8, 388*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE9, 389*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE10, 390*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE11, 391*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE12, 392*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE13, 393*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE14, 394*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_QUSE15, 395*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS0, 396*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS1, 397*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS2, 398*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS3, 399*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS4, 400*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS5, 401*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS6, 402*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS7, 403*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS8, 404*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS9, 405*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS10, 406*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS11, 407*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS12, 408*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS13, 409*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS14, 410*73a7f0a9SMikko Perttunen EMC_DLI_TRIM_TXDQS15, 411*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ0, 412*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ1, 413*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ2, 414*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ3, 415*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ4, 416*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ5, 417*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ6, 418*73a7f0a9SMikko Perttunen EMC_DLL_XFORM_DQ7, 419*73a7f0a9SMikko Perttunen EMC_XM2CMDPADCTRL, 420*73a7f0a9SMikko Perttunen EMC_XM2CMDPADCTRL4, 421*73a7f0a9SMikko Perttunen EMC_XM2CMDPADCTRL5, 422*73a7f0a9SMikko Perttunen EMC_XM2DQPADCTRL2, 423*73a7f0a9SMikko Perttunen EMC_XM2DQPADCTRL3, 424*73a7f0a9SMikko Perttunen EMC_XM2CLKPADCTRL, 425*73a7f0a9SMikko Perttunen EMC_XM2CLKPADCTRL2, 426*73a7f0a9SMikko Perttunen EMC_XM2COMPPADCTRL, 427*73a7f0a9SMikko Perttunen EMC_XM2VTTGENPADCTRL, 428*73a7f0a9SMikko Perttunen EMC_XM2VTTGENPADCTRL2, 429*73a7f0a9SMikko Perttunen EMC_XM2VTTGENPADCTRL3, 430*73a7f0a9SMikko Perttunen EMC_XM2DQSPADCTRL3, 431*73a7f0a9SMikko Perttunen EMC_XM2DQSPADCTRL4, 432*73a7f0a9SMikko Perttunen EMC_XM2DQSPADCTRL5, 433*73a7f0a9SMikko Perttunen EMC_XM2DQSPADCTRL6, 434*73a7f0a9SMikko Perttunen EMC_DSR_VTTGEN_DRV, 435*73a7f0a9SMikko Perttunen EMC_TXDSRVTTGEN, 436*73a7f0a9SMikko Perttunen EMC_FBIO_SPARE, 437*73a7f0a9SMikko Perttunen EMC_ZCAL_WAIT_CNT, 438*73a7f0a9SMikko Perttunen EMC_MRS_WAIT_CNT2, 439*73a7f0a9SMikko Perttunen EMC_CTT, 440*73a7f0a9SMikko Perttunen EMC_CTT_DURATION, 441*73a7f0a9SMikko Perttunen EMC_CFG_PIPE, 442*73a7f0a9SMikko Perttunen EMC_DYN_SELF_REF_CONTROL, 443*73a7f0a9SMikko Perttunen EMC_QPOP 444*73a7f0a9SMikko Perttunen }; 445*73a7f0a9SMikko Perttunen 446*73a7f0a9SMikko Perttunen struct emc_timing { 447*73a7f0a9SMikko Perttunen unsigned long rate; 448*73a7f0a9SMikko Perttunen 449*73a7f0a9SMikko Perttunen u32 emc_burst_data[ARRAY_SIZE(emc_burst_regs)]; 450*73a7f0a9SMikko Perttunen 451*73a7f0a9SMikko Perttunen u32 emc_auto_cal_config; 452*73a7f0a9SMikko Perttunen u32 emc_auto_cal_config2; 453*73a7f0a9SMikko Perttunen u32 emc_auto_cal_config3; 454*73a7f0a9SMikko Perttunen u32 emc_auto_cal_interval; 455*73a7f0a9SMikko Perttunen u32 emc_bgbias_ctl0; 456*73a7f0a9SMikko Perttunen u32 emc_cfg; 457*73a7f0a9SMikko Perttunen u32 emc_cfg_2; 458*73a7f0a9SMikko Perttunen u32 emc_ctt_term_ctrl; 459*73a7f0a9SMikko Perttunen u32 emc_mode_1; 460*73a7f0a9SMikko Perttunen u32 emc_mode_2; 461*73a7f0a9SMikko Perttunen u32 emc_mode_4; 462*73a7f0a9SMikko Perttunen u32 emc_mode_reset; 463*73a7f0a9SMikko Perttunen u32 emc_mrs_wait_cnt; 464*73a7f0a9SMikko Perttunen u32 emc_sel_dpd_ctrl; 465*73a7f0a9SMikko Perttunen u32 emc_xm2dqspadctrl2; 466*73a7f0a9SMikko Perttunen u32 emc_zcal_cnt_long; 467*73a7f0a9SMikko Perttunen u32 emc_zcal_interval; 468*73a7f0a9SMikko Perttunen }; 469*73a7f0a9SMikko Perttunen 470*73a7f0a9SMikko Perttunen struct tegra_emc { 471*73a7f0a9SMikko Perttunen struct device *dev; 472*73a7f0a9SMikko Perttunen 473*73a7f0a9SMikko Perttunen struct tegra_mc *mc; 474*73a7f0a9SMikko Perttunen 475*73a7f0a9SMikko Perttunen void __iomem *regs; 476*73a7f0a9SMikko Perttunen 477*73a7f0a9SMikko Perttunen enum emc_dram_type dram_type; 478*73a7f0a9SMikko Perttunen unsigned int dram_num; 479*73a7f0a9SMikko Perttunen 480*73a7f0a9SMikko Perttunen struct emc_timing last_timing; 481*73a7f0a9SMikko Perttunen struct emc_timing *timings; 482*73a7f0a9SMikko Perttunen unsigned int num_timings; 483*73a7f0a9SMikko Perttunen }; 484*73a7f0a9SMikko Perttunen 485*73a7f0a9SMikko Perttunen /* Timing change sequence functions */ 486*73a7f0a9SMikko Perttunen 487*73a7f0a9SMikko Perttunen static void emc_ccfifo_writel(struct tegra_emc *emc, u32 value, 488*73a7f0a9SMikko Perttunen unsigned long offset) 489*73a7f0a9SMikko Perttunen { 490*73a7f0a9SMikko Perttunen writel(value, emc->regs + EMC_CCFIFO_DATA); 491*73a7f0a9SMikko Perttunen writel(offset, emc->regs + EMC_CCFIFO_ADDR); 492*73a7f0a9SMikko Perttunen } 493*73a7f0a9SMikko Perttunen 494*73a7f0a9SMikko Perttunen static void emc_seq_update_timing(struct tegra_emc *emc) 495*73a7f0a9SMikko Perttunen { 496*73a7f0a9SMikko Perttunen unsigned int i; 497*73a7f0a9SMikko Perttunen u32 value; 498*73a7f0a9SMikko Perttunen 499*73a7f0a9SMikko Perttunen writel(1, emc->regs + EMC_TIMING_CONTROL); 500*73a7f0a9SMikko Perttunen 501*73a7f0a9SMikko Perttunen for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) { 502*73a7f0a9SMikko Perttunen value = readl(emc->regs + EMC_STATUS); 503*73a7f0a9SMikko Perttunen if ((value & EMC_STATUS_TIMING_UPDATE_STALLED) == 0) 504*73a7f0a9SMikko Perttunen return; 505*73a7f0a9SMikko Perttunen udelay(1); 506*73a7f0a9SMikko Perttunen } 507*73a7f0a9SMikko Perttunen 508*73a7f0a9SMikko Perttunen dev_err(emc->dev, "timing update timed out\n"); 509*73a7f0a9SMikko Perttunen } 510*73a7f0a9SMikko Perttunen 511*73a7f0a9SMikko Perttunen static void emc_seq_disable_auto_cal(struct tegra_emc *emc) 512*73a7f0a9SMikko Perttunen { 513*73a7f0a9SMikko Perttunen unsigned int i; 514*73a7f0a9SMikko Perttunen u32 value; 515*73a7f0a9SMikko Perttunen 516*73a7f0a9SMikko Perttunen writel(0, emc->regs + EMC_AUTO_CAL_INTERVAL); 517*73a7f0a9SMikko Perttunen 518*73a7f0a9SMikko Perttunen for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) { 519*73a7f0a9SMikko Perttunen value = readl(emc->regs + EMC_AUTO_CAL_STATUS); 520*73a7f0a9SMikko Perttunen if ((value & EMC_AUTO_CAL_STATUS_ACTIVE) == 0) 521*73a7f0a9SMikko Perttunen return; 522*73a7f0a9SMikko Perttunen udelay(1); 523*73a7f0a9SMikko Perttunen } 524*73a7f0a9SMikko Perttunen 525*73a7f0a9SMikko Perttunen dev_err(emc->dev, "auto cal disable timed out\n"); 526*73a7f0a9SMikko Perttunen } 527*73a7f0a9SMikko Perttunen 528*73a7f0a9SMikko Perttunen static void emc_seq_wait_clkchange(struct tegra_emc *emc) 529*73a7f0a9SMikko Perttunen { 530*73a7f0a9SMikko Perttunen unsigned int i; 531*73a7f0a9SMikko Perttunen u32 value; 532*73a7f0a9SMikko Perttunen 533*73a7f0a9SMikko Perttunen for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) { 534*73a7f0a9SMikko Perttunen value = readl(emc->regs + EMC_INTSTATUS); 535*73a7f0a9SMikko Perttunen if (value & EMC_INTSTATUS_CLKCHANGE_COMPLETE) 536*73a7f0a9SMikko Perttunen return; 537*73a7f0a9SMikko Perttunen udelay(1); 538*73a7f0a9SMikko Perttunen } 539*73a7f0a9SMikko Perttunen 540*73a7f0a9SMikko Perttunen dev_err(emc->dev, "clock change timed out\n"); 541*73a7f0a9SMikko Perttunen } 542*73a7f0a9SMikko Perttunen 543*73a7f0a9SMikko Perttunen static struct emc_timing *tegra_emc_find_timing(struct tegra_emc *emc, 544*73a7f0a9SMikko Perttunen unsigned long rate) 545*73a7f0a9SMikko Perttunen { 546*73a7f0a9SMikko Perttunen struct emc_timing *timing = NULL; 547*73a7f0a9SMikko Perttunen unsigned int i; 548*73a7f0a9SMikko Perttunen 549*73a7f0a9SMikko Perttunen for (i = 0; i < emc->num_timings; i++) { 550*73a7f0a9SMikko Perttunen if (emc->timings[i].rate == rate) { 551*73a7f0a9SMikko Perttunen timing = &emc->timings[i]; 552*73a7f0a9SMikko Perttunen break; 553*73a7f0a9SMikko Perttunen } 554*73a7f0a9SMikko Perttunen } 555*73a7f0a9SMikko Perttunen 556*73a7f0a9SMikko Perttunen if (!timing) { 557*73a7f0a9SMikko Perttunen dev_err(emc->dev, "no timing for rate %lu\n", rate); 558*73a7f0a9SMikko Perttunen return NULL; 559*73a7f0a9SMikko Perttunen } 560*73a7f0a9SMikko Perttunen 561*73a7f0a9SMikko Perttunen return timing; 562*73a7f0a9SMikko Perttunen } 563*73a7f0a9SMikko Perttunen 564*73a7f0a9SMikko Perttunen int tegra_emc_prepare_timing_change(struct tegra_emc *emc, 565*73a7f0a9SMikko Perttunen unsigned long rate) 566*73a7f0a9SMikko Perttunen { 567*73a7f0a9SMikko Perttunen struct emc_timing *timing = tegra_emc_find_timing(emc, rate); 568*73a7f0a9SMikko Perttunen struct emc_timing *last = &emc->last_timing; 569*73a7f0a9SMikko Perttunen enum emc_dll_change dll_change; 570*73a7f0a9SMikko Perttunen unsigned int pre_wait = 0; 571*73a7f0a9SMikko Perttunen u32 val, val2, mask; 572*73a7f0a9SMikko Perttunen bool update = false; 573*73a7f0a9SMikko Perttunen unsigned int i; 574*73a7f0a9SMikko Perttunen 575*73a7f0a9SMikko Perttunen if (!timing) 576*73a7f0a9SMikko Perttunen return -ENOENT; 577*73a7f0a9SMikko Perttunen 578*73a7f0a9SMikko Perttunen if ((last->emc_mode_1 & 0x1) == (timing->emc_mode_1 & 0x1)) 579*73a7f0a9SMikko Perttunen dll_change = DLL_CHANGE_NONE; 580*73a7f0a9SMikko Perttunen else if (timing->emc_mode_1 & 0x1) 581*73a7f0a9SMikko Perttunen dll_change = DLL_CHANGE_ON; 582*73a7f0a9SMikko Perttunen else 583*73a7f0a9SMikko Perttunen dll_change = DLL_CHANGE_OFF; 584*73a7f0a9SMikko Perttunen 585*73a7f0a9SMikko Perttunen /* Clear CLKCHANGE_COMPLETE interrupts */ 586*73a7f0a9SMikko Perttunen writel(EMC_INTSTATUS_CLKCHANGE_COMPLETE, emc->regs + EMC_INTSTATUS); 587*73a7f0a9SMikko Perttunen 588*73a7f0a9SMikko Perttunen /* Disable dynamic self-refresh */ 589*73a7f0a9SMikko Perttunen val = readl(emc->regs + EMC_CFG); 590*73a7f0a9SMikko Perttunen if (val & EMC_CFG_PWR_MASK) { 591*73a7f0a9SMikko Perttunen val &= ~EMC_CFG_POWER_FEATURES_MASK; 592*73a7f0a9SMikko Perttunen writel(val, emc->regs + EMC_CFG); 593*73a7f0a9SMikko Perttunen 594*73a7f0a9SMikko Perttunen pre_wait = 5; 595*73a7f0a9SMikko Perttunen } 596*73a7f0a9SMikko Perttunen 597*73a7f0a9SMikko Perttunen /* Disable SEL_DPD_CTRL for clock change */ 598*73a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3) 599*73a7f0a9SMikko Perttunen mask = EMC_SEL_DPD_CTRL_DDR3_MASK; 600*73a7f0a9SMikko Perttunen else 601*73a7f0a9SMikko Perttunen mask = EMC_SEL_DPD_CTRL_MASK; 602*73a7f0a9SMikko Perttunen 603*73a7f0a9SMikko Perttunen val = readl(emc->regs + EMC_SEL_DPD_CTRL); 604*73a7f0a9SMikko Perttunen if (val & mask) { 605*73a7f0a9SMikko Perttunen val &= ~mask; 606*73a7f0a9SMikko Perttunen writel(val, emc->regs + EMC_SEL_DPD_CTRL); 607*73a7f0a9SMikko Perttunen } 608*73a7f0a9SMikko Perttunen 609*73a7f0a9SMikko Perttunen /* Prepare DQ/DQS for clock change */ 610*73a7f0a9SMikko Perttunen val = readl(emc->regs + EMC_BGBIAS_CTL0); 611*73a7f0a9SMikko Perttunen val2 = last->emc_bgbias_ctl0; 612*73a7f0a9SMikko Perttunen if (!(timing->emc_bgbias_ctl0 & 613*73a7f0a9SMikko Perttunen EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) && 614*73a7f0a9SMikko Perttunen (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX)) { 615*73a7f0a9SMikko Perttunen val2 &= ~EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX; 616*73a7f0a9SMikko Perttunen update = true; 617*73a7f0a9SMikko Perttunen } 618*73a7f0a9SMikko Perttunen 619*73a7f0a9SMikko Perttunen if ((val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD) || 620*73a7f0a9SMikko Perttunen (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN)) { 621*73a7f0a9SMikko Perttunen update = true; 622*73a7f0a9SMikko Perttunen } 623*73a7f0a9SMikko Perttunen 624*73a7f0a9SMikko Perttunen if (update) { 625*73a7f0a9SMikko Perttunen writel(val2, emc->regs + EMC_BGBIAS_CTL0); 626*73a7f0a9SMikko Perttunen if (pre_wait < 5) 627*73a7f0a9SMikko Perttunen pre_wait = 5; 628*73a7f0a9SMikko Perttunen } 629*73a7f0a9SMikko Perttunen 630*73a7f0a9SMikko Perttunen update = false; 631*73a7f0a9SMikko Perttunen val = readl(emc->regs + EMC_XM2DQSPADCTRL2); 632*73a7f0a9SMikko Perttunen if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_VREF_ENABLE && 633*73a7f0a9SMikko Perttunen !(val & EMC_XM2DQSPADCTRL2_VREF_ENABLE)) { 634*73a7f0a9SMikko Perttunen val |= EMC_XM2DQSPADCTRL2_VREF_ENABLE; 635*73a7f0a9SMikko Perttunen update = true; 636*73a7f0a9SMikko Perttunen } 637*73a7f0a9SMikko Perttunen 638*73a7f0a9SMikko Perttunen if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE && 639*73a7f0a9SMikko Perttunen !(val & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE)) { 640*73a7f0a9SMikko Perttunen val |= EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE; 641*73a7f0a9SMikko Perttunen update = true; 642*73a7f0a9SMikko Perttunen } 643*73a7f0a9SMikko Perttunen 644*73a7f0a9SMikko Perttunen if (update) { 645*73a7f0a9SMikko Perttunen writel(val, emc->regs + EMC_XM2DQSPADCTRL2); 646*73a7f0a9SMikko Perttunen if (pre_wait < 30) 647*73a7f0a9SMikko Perttunen pre_wait = 30; 648*73a7f0a9SMikko Perttunen } 649*73a7f0a9SMikko Perttunen 650*73a7f0a9SMikko Perttunen /* Wait to settle */ 651*73a7f0a9SMikko Perttunen if (pre_wait) { 652*73a7f0a9SMikko Perttunen emc_seq_update_timing(emc); 653*73a7f0a9SMikko Perttunen udelay(pre_wait); 654*73a7f0a9SMikko Perttunen } 655*73a7f0a9SMikko Perttunen 656*73a7f0a9SMikko Perttunen /* Program CTT_TERM control */ 657*73a7f0a9SMikko Perttunen if (last->emc_ctt_term_ctrl != timing->emc_ctt_term_ctrl) { 658*73a7f0a9SMikko Perttunen emc_seq_disable_auto_cal(emc); 659*73a7f0a9SMikko Perttunen writel(timing->emc_ctt_term_ctrl, 660*73a7f0a9SMikko Perttunen emc->regs + EMC_CTT_TERM_CTRL); 661*73a7f0a9SMikko Perttunen emc_seq_update_timing(emc); 662*73a7f0a9SMikko Perttunen } 663*73a7f0a9SMikko Perttunen 664*73a7f0a9SMikko Perttunen /* Program burst shadow registers */ 665*73a7f0a9SMikko Perttunen for (i = 0; i < ARRAY_SIZE(timing->emc_burst_data); ++i) 666*73a7f0a9SMikko Perttunen writel(timing->emc_burst_data[i], 667*73a7f0a9SMikko Perttunen emc->regs + emc_burst_regs[i]); 668*73a7f0a9SMikko Perttunen 669*73a7f0a9SMikko Perttunen writel(timing->emc_xm2dqspadctrl2, emc->regs + EMC_XM2DQSPADCTRL2); 670*73a7f0a9SMikko Perttunen writel(timing->emc_zcal_interval, emc->regs + EMC_ZCAL_INTERVAL); 671*73a7f0a9SMikko Perttunen 672*73a7f0a9SMikko Perttunen tegra_mc_write_emem_configuration(emc->mc, timing->rate); 673*73a7f0a9SMikko Perttunen 674*73a7f0a9SMikko Perttunen val = timing->emc_cfg & ~EMC_CFG_POWER_FEATURES_MASK; 675*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, val, EMC_CFG); 676*73a7f0a9SMikko Perttunen 677*73a7f0a9SMikko Perttunen /* Program AUTO_CAL_CONFIG */ 678*73a7f0a9SMikko Perttunen if (timing->emc_auto_cal_config2 != last->emc_auto_cal_config2) 679*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_auto_cal_config2, 680*73a7f0a9SMikko Perttunen EMC_AUTO_CAL_CONFIG2); 681*73a7f0a9SMikko Perttunen 682*73a7f0a9SMikko Perttunen if (timing->emc_auto_cal_config3 != last->emc_auto_cal_config3) 683*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_auto_cal_config3, 684*73a7f0a9SMikko Perttunen EMC_AUTO_CAL_CONFIG3); 685*73a7f0a9SMikko Perttunen 686*73a7f0a9SMikko Perttunen if (timing->emc_auto_cal_config != last->emc_auto_cal_config) { 687*73a7f0a9SMikko Perttunen val = timing->emc_auto_cal_config; 688*73a7f0a9SMikko Perttunen val &= EMC_AUTO_CAL_CONFIG_AUTO_CAL_START; 689*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, val, EMC_AUTO_CAL_CONFIG); 690*73a7f0a9SMikko Perttunen } 691*73a7f0a9SMikko Perttunen 692*73a7f0a9SMikko Perttunen /* DDR3: predict MRS long wait count */ 693*73a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3 && 694*73a7f0a9SMikko Perttunen dll_change == DLL_CHANGE_ON) { 695*73a7f0a9SMikko Perttunen u32 cnt = 512; 696*73a7f0a9SMikko Perttunen 697*73a7f0a9SMikko Perttunen if (timing->emc_zcal_interval != 0 && 698*73a7f0a9SMikko Perttunen last->emc_zcal_interval == 0) 699*73a7f0a9SMikko Perttunen cnt -= emc->dram_num * 256; 700*73a7f0a9SMikko Perttunen 701*73a7f0a9SMikko Perttunen val = (timing->emc_mrs_wait_cnt 702*73a7f0a9SMikko Perttunen & EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK) 703*73a7f0a9SMikko Perttunen >> EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT; 704*73a7f0a9SMikko Perttunen if (cnt < val) 705*73a7f0a9SMikko Perttunen cnt = val; 706*73a7f0a9SMikko Perttunen 707*73a7f0a9SMikko Perttunen val = timing->emc_mrs_wait_cnt 708*73a7f0a9SMikko Perttunen & ~EMC_MRS_WAIT_CNT_LONG_WAIT_MASK; 709*73a7f0a9SMikko Perttunen val |= (cnt << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT) 710*73a7f0a9SMikko Perttunen & EMC_MRS_WAIT_CNT_LONG_WAIT_MASK; 711*73a7f0a9SMikko Perttunen 712*73a7f0a9SMikko Perttunen writel(val, emc->regs + EMC_MRS_WAIT_CNT); 713*73a7f0a9SMikko Perttunen } 714*73a7f0a9SMikko Perttunen 715*73a7f0a9SMikko Perttunen val = timing->emc_cfg_2; 716*73a7f0a9SMikko Perttunen val &= ~EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR; 717*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, val, EMC_CFG_2); 718*73a7f0a9SMikko Perttunen 719*73a7f0a9SMikko Perttunen /* DDR3: Turn off DLL and enter self-refresh */ 720*73a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3 && dll_change == DLL_CHANGE_OFF) 721*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS); 722*73a7f0a9SMikko Perttunen 723*73a7f0a9SMikko Perttunen /* Disable refresh controller */ 724*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num), 725*73a7f0a9SMikko Perttunen EMC_REFCTRL); 726*73a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3) 727*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num) | 728*73a7f0a9SMikko Perttunen EMC_SELF_REF_CMD_ENABLED, 729*73a7f0a9SMikko Perttunen EMC_SELF_REF); 730*73a7f0a9SMikko Perttunen 731*73a7f0a9SMikko Perttunen /* Flow control marker */ 732*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, 1, EMC_STALL_THEN_EXE_AFTER_CLKCHANGE); 733*73a7f0a9SMikko Perttunen 734*73a7f0a9SMikko Perttunen /* DDR3: Exit self-refresh */ 735*73a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3) 736*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num), 737*73a7f0a9SMikko Perttunen EMC_SELF_REF); 738*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num) | 739*73a7f0a9SMikko Perttunen EMC_REFCTRL_ENABLE, 740*73a7f0a9SMikko Perttunen EMC_REFCTRL); 741*73a7f0a9SMikko Perttunen 742*73a7f0a9SMikko Perttunen /* Set DRAM mode registers */ 743*73a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3) { 744*73a7f0a9SMikko Perttunen if (timing->emc_mode_1 != last->emc_mode_1) 745*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS); 746*73a7f0a9SMikko Perttunen if (timing->emc_mode_2 != last->emc_mode_2) 747*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_EMRS2); 748*73a7f0a9SMikko Perttunen 749*73a7f0a9SMikko Perttunen if ((timing->emc_mode_reset != last->emc_mode_reset) || 750*73a7f0a9SMikko Perttunen dll_change == DLL_CHANGE_ON) { 751*73a7f0a9SMikko Perttunen val = timing->emc_mode_reset; 752*73a7f0a9SMikko Perttunen if (dll_change == DLL_CHANGE_ON) { 753*73a7f0a9SMikko Perttunen val |= EMC_MODE_SET_DLL_RESET; 754*73a7f0a9SMikko Perttunen val |= EMC_MODE_SET_LONG_CNT; 755*73a7f0a9SMikko Perttunen } else { 756*73a7f0a9SMikko Perttunen val &= ~EMC_MODE_SET_DLL_RESET; 757*73a7f0a9SMikko Perttunen } 758*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, val, EMC_MRS); 759*73a7f0a9SMikko Perttunen } 760*73a7f0a9SMikko Perttunen } else { 761*73a7f0a9SMikko Perttunen if (timing->emc_mode_2 != last->emc_mode_2) 762*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_MRW2); 763*73a7f0a9SMikko Perttunen if (timing->emc_mode_1 != last->emc_mode_1) 764*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_MRW); 765*73a7f0a9SMikko Perttunen if (timing->emc_mode_4 != last->emc_mode_4) 766*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_mode_4, EMC_MRW4); 767*73a7f0a9SMikko Perttunen } 768*73a7f0a9SMikko Perttunen 769*73a7f0a9SMikko Perttunen /* Issue ZCAL command if turning ZCAL on */ 770*73a7f0a9SMikko Perttunen if (timing->emc_zcal_interval != 0 && last->emc_zcal_interval == 0) { 771*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV0, EMC_ZQ_CAL); 772*73a7f0a9SMikko Perttunen if (emc->dram_num > 1) 773*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV1, 774*73a7f0a9SMikko Perttunen EMC_ZQ_CAL); 775*73a7f0a9SMikko Perttunen } 776*73a7f0a9SMikko Perttunen 777*73a7f0a9SMikko Perttunen /* Write to RO register to remove stall after change */ 778*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, 0, EMC_CCFIFO_STATUS); 779*73a7f0a9SMikko Perttunen 780*73a7f0a9SMikko Perttunen if (timing->emc_cfg_2 & EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR) 781*73a7f0a9SMikko Perttunen emc_ccfifo_writel(emc, timing->emc_cfg_2, EMC_CFG_2); 782*73a7f0a9SMikko Perttunen 783*73a7f0a9SMikko Perttunen /* Disable AUTO_CAL for clock change */ 784*73a7f0a9SMikko Perttunen emc_seq_disable_auto_cal(emc); 785*73a7f0a9SMikko Perttunen 786*73a7f0a9SMikko Perttunen /* Read register to wait until programming has settled */ 787*73a7f0a9SMikko Perttunen readl(emc->regs + EMC_INTSTATUS); 788*73a7f0a9SMikko Perttunen 789*73a7f0a9SMikko Perttunen return 0; 790*73a7f0a9SMikko Perttunen } 791*73a7f0a9SMikko Perttunen 792*73a7f0a9SMikko Perttunen void tegra_emc_complete_timing_change(struct tegra_emc *emc, 793*73a7f0a9SMikko Perttunen unsigned long rate) 794*73a7f0a9SMikko Perttunen { 795*73a7f0a9SMikko Perttunen struct emc_timing *timing = tegra_emc_find_timing(emc, rate); 796*73a7f0a9SMikko Perttunen struct emc_timing *last = &emc->last_timing; 797*73a7f0a9SMikko Perttunen u32 val; 798*73a7f0a9SMikko Perttunen 799*73a7f0a9SMikko Perttunen if (!timing) 800*73a7f0a9SMikko Perttunen return; 801*73a7f0a9SMikko Perttunen 802*73a7f0a9SMikko Perttunen /* Wait until the state machine has settled */ 803*73a7f0a9SMikko Perttunen emc_seq_wait_clkchange(emc); 804*73a7f0a9SMikko Perttunen 805*73a7f0a9SMikko Perttunen /* Restore AUTO_CAL */ 806*73a7f0a9SMikko Perttunen if (timing->emc_ctt_term_ctrl != last->emc_ctt_term_ctrl) 807*73a7f0a9SMikko Perttunen writel(timing->emc_auto_cal_interval, 808*73a7f0a9SMikko Perttunen emc->regs + EMC_AUTO_CAL_INTERVAL); 809*73a7f0a9SMikko Perttunen 810*73a7f0a9SMikko Perttunen /* Restore dynamic self-refresh */ 811*73a7f0a9SMikko Perttunen if (timing->emc_cfg & EMC_CFG_PWR_MASK) 812*73a7f0a9SMikko Perttunen writel(timing->emc_cfg, emc->regs + EMC_CFG); 813*73a7f0a9SMikko Perttunen 814*73a7f0a9SMikko Perttunen /* Set ZCAL wait count */ 815*73a7f0a9SMikko Perttunen writel(timing->emc_zcal_cnt_long, emc->regs + EMC_ZCAL_WAIT_CNT); 816*73a7f0a9SMikko Perttunen 817*73a7f0a9SMikko Perttunen /* LPDDR3: Turn off BGBIAS if low frequency */ 818*73a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_LPDDR3 && 819*73a7f0a9SMikko Perttunen timing->emc_bgbias_ctl0 & 820*73a7f0a9SMikko Perttunen EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) { 821*73a7f0a9SMikko Perttunen val = timing->emc_bgbias_ctl0; 822*73a7f0a9SMikko Perttunen val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN; 823*73a7f0a9SMikko Perttunen val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD; 824*73a7f0a9SMikko Perttunen writel(val, emc->regs + EMC_BGBIAS_CTL0); 825*73a7f0a9SMikko Perttunen } else { 826*73a7f0a9SMikko Perttunen if (emc->dram_type == DRAM_TYPE_DDR3 && 827*73a7f0a9SMikko Perttunen readl(emc->regs + EMC_BGBIAS_CTL0) != 828*73a7f0a9SMikko Perttunen timing->emc_bgbias_ctl0) { 829*73a7f0a9SMikko Perttunen writel(timing->emc_bgbias_ctl0, 830*73a7f0a9SMikko Perttunen emc->regs + EMC_BGBIAS_CTL0); 831*73a7f0a9SMikko Perttunen } 832*73a7f0a9SMikko Perttunen 833*73a7f0a9SMikko Perttunen writel(timing->emc_auto_cal_interval, 834*73a7f0a9SMikko Perttunen emc->regs + EMC_AUTO_CAL_INTERVAL); 835*73a7f0a9SMikko Perttunen } 836*73a7f0a9SMikko Perttunen 837*73a7f0a9SMikko Perttunen /* Wait for timing to settle */ 838*73a7f0a9SMikko Perttunen udelay(2); 839*73a7f0a9SMikko Perttunen 840*73a7f0a9SMikko Perttunen /* Reprogram SEL_DPD_CTRL */ 841*73a7f0a9SMikko Perttunen writel(timing->emc_sel_dpd_ctrl, emc->regs + EMC_SEL_DPD_CTRL); 842*73a7f0a9SMikko Perttunen emc_seq_update_timing(emc); 843*73a7f0a9SMikko Perttunen 844*73a7f0a9SMikko Perttunen emc->last_timing = *timing; 845*73a7f0a9SMikko Perttunen } 846*73a7f0a9SMikko Perttunen 847*73a7f0a9SMikko Perttunen /* Initialization and deinitialization */ 848*73a7f0a9SMikko Perttunen 849*73a7f0a9SMikko Perttunen static void emc_read_current_timing(struct tegra_emc *emc, 850*73a7f0a9SMikko Perttunen struct emc_timing *timing) 851*73a7f0a9SMikko Perttunen { 852*73a7f0a9SMikko Perttunen unsigned int i; 853*73a7f0a9SMikko Perttunen 854*73a7f0a9SMikko Perttunen for (i = 0; i < ARRAY_SIZE(emc_burst_regs); ++i) 855*73a7f0a9SMikko Perttunen timing->emc_burst_data[i] = 856*73a7f0a9SMikko Perttunen readl(emc->regs + emc_burst_regs[i]); 857*73a7f0a9SMikko Perttunen 858*73a7f0a9SMikko Perttunen timing->emc_cfg = readl(emc->regs + EMC_CFG); 859*73a7f0a9SMikko Perttunen 860*73a7f0a9SMikko Perttunen timing->emc_auto_cal_interval = 0; 861*73a7f0a9SMikko Perttunen timing->emc_zcal_cnt_long = 0; 862*73a7f0a9SMikko Perttunen timing->emc_mode_1 = 0; 863*73a7f0a9SMikko Perttunen timing->emc_mode_2 = 0; 864*73a7f0a9SMikko Perttunen timing->emc_mode_4 = 0; 865*73a7f0a9SMikko Perttunen timing->emc_mode_reset = 0; 866*73a7f0a9SMikko Perttunen } 867*73a7f0a9SMikko Perttunen 868*73a7f0a9SMikko Perttunen static int emc_init(struct tegra_emc *emc) 869*73a7f0a9SMikko Perttunen { 870*73a7f0a9SMikko Perttunen emc->dram_type = readl(emc->regs + EMC_FBIO_CFG5); 871*73a7f0a9SMikko Perttunen emc->dram_type &= EMC_FBIO_CFG5_DRAM_TYPE_MASK; 872*73a7f0a9SMikko Perttunen emc->dram_type >>= EMC_FBIO_CFG5_DRAM_TYPE_SHIFT; 873*73a7f0a9SMikko Perttunen 874*73a7f0a9SMikko Perttunen emc->dram_num = tegra_mc_get_emem_device_count(emc->mc); 875*73a7f0a9SMikko Perttunen 876*73a7f0a9SMikko Perttunen emc_read_current_timing(emc, &emc->last_timing); 877*73a7f0a9SMikko Perttunen 878*73a7f0a9SMikko Perttunen return 0; 879*73a7f0a9SMikko Perttunen } 880*73a7f0a9SMikko Perttunen 881*73a7f0a9SMikko Perttunen static int load_one_timing_from_dt(struct tegra_emc *emc, 882*73a7f0a9SMikko Perttunen struct emc_timing *timing, 883*73a7f0a9SMikko Perttunen struct device_node *node) 884*73a7f0a9SMikko Perttunen { 885*73a7f0a9SMikko Perttunen u32 value; 886*73a7f0a9SMikko Perttunen int err; 887*73a7f0a9SMikko Perttunen 888*73a7f0a9SMikko Perttunen err = of_property_read_u32(node, "clock-frequency", &value); 889*73a7f0a9SMikko Perttunen if (err) { 890*73a7f0a9SMikko Perttunen dev_err(emc->dev, "timing %s: failed to read rate: %d\n", 891*73a7f0a9SMikko Perttunen node->name, err); 892*73a7f0a9SMikko Perttunen return err; 893*73a7f0a9SMikko Perttunen } 894*73a7f0a9SMikko Perttunen 895*73a7f0a9SMikko Perttunen timing->rate = value; 896*73a7f0a9SMikko Perttunen 897*73a7f0a9SMikko Perttunen err = of_property_read_u32_array(node, "nvidia,emc-configuration", 898*73a7f0a9SMikko Perttunen timing->emc_burst_data, 899*73a7f0a9SMikko Perttunen ARRAY_SIZE(timing->emc_burst_data)); 900*73a7f0a9SMikko Perttunen if (err) { 901*73a7f0a9SMikko Perttunen dev_err(emc->dev, 902*73a7f0a9SMikko Perttunen "timing %s: failed to read emc burst data: %d\n", 903*73a7f0a9SMikko Perttunen node->name, err); 904*73a7f0a9SMikko Perttunen return err; 905*73a7f0a9SMikko Perttunen } 906*73a7f0a9SMikko Perttunen 907*73a7f0a9SMikko Perttunen #define EMC_READ_PROP(prop, dtprop) { \ 908*73a7f0a9SMikko Perttunen err = of_property_read_u32(node, dtprop, &timing->prop); \ 909*73a7f0a9SMikko Perttunen if (err) { \ 910*73a7f0a9SMikko Perttunen dev_err(emc->dev, "timing %s: failed to read " #prop ": %d\n", \ 911*73a7f0a9SMikko Perttunen node->name, err); \ 912*73a7f0a9SMikko Perttunen return err; \ 913*73a7f0a9SMikko Perttunen } \ 914*73a7f0a9SMikko Perttunen } 915*73a7f0a9SMikko Perttunen 916*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_auto_cal_config, "nvidia,emc-auto-cal-config") 917*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_auto_cal_config2, "nvidia,emc-auto-cal-config2") 918*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_auto_cal_config3, "nvidia,emc-auto-cal-config3") 919*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_auto_cal_interval, "nvidia,emc-auto-cal-interval") 920*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_bgbias_ctl0, "nvidia,emc-bgbias-ctl0") 921*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_cfg, "nvidia,emc-cfg") 922*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_cfg_2, "nvidia,emc-cfg-2") 923*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_ctt_term_ctrl, "nvidia,emc-ctt-term-ctrl") 924*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_mode_1, "nvidia,emc-mode-1") 925*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_mode_2, "nvidia,emc-mode-2") 926*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_mode_4, "nvidia,emc-mode-4") 927*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_mode_reset, "nvidia,emc-mode-reset") 928*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_mrs_wait_cnt, "nvidia,emc-mrs-wait-cnt") 929*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_sel_dpd_ctrl, "nvidia,emc-sel-dpd-ctrl") 930*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_xm2dqspadctrl2, "nvidia,emc-xm2dqspadctrl2") 931*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_zcal_cnt_long, "nvidia,emc-zcal-cnt-long") 932*73a7f0a9SMikko Perttunen EMC_READ_PROP(emc_zcal_interval, "nvidia,emc-zcal-interval") 933*73a7f0a9SMikko Perttunen 934*73a7f0a9SMikko Perttunen #undef EMC_READ_PROP 935*73a7f0a9SMikko Perttunen 936*73a7f0a9SMikko Perttunen return 0; 937*73a7f0a9SMikko Perttunen } 938*73a7f0a9SMikko Perttunen 939*73a7f0a9SMikko Perttunen static int cmp_timings(const void *_a, const void *_b) 940*73a7f0a9SMikko Perttunen { 941*73a7f0a9SMikko Perttunen const struct emc_timing *a = _a; 942*73a7f0a9SMikko Perttunen const struct emc_timing *b = _b; 943*73a7f0a9SMikko Perttunen 944*73a7f0a9SMikko Perttunen if (a->rate < b->rate) 945*73a7f0a9SMikko Perttunen return -1; 946*73a7f0a9SMikko Perttunen else if (a->rate == b->rate) 947*73a7f0a9SMikko Perttunen return 0; 948*73a7f0a9SMikko Perttunen else 949*73a7f0a9SMikko Perttunen return 1; 950*73a7f0a9SMikko Perttunen } 951*73a7f0a9SMikko Perttunen 952*73a7f0a9SMikko Perttunen static int tegra_emc_load_timings_from_dt(struct tegra_emc *emc, 953*73a7f0a9SMikko Perttunen struct device_node *node) 954*73a7f0a9SMikko Perttunen { 955*73a7f0a9SMikko Perttunen int child_count = of_get_child_count(node); 956*73a7f0a9SMikko Perttunen struct device_node *child; 957*73a7f0a9SMikko Perttunen struct emc_timing *timing; 958*73a7f0a9SMikko Perttunen unsigned int i = 0; 959*73a7f0a9SMikko Perttunen int err; 960*73a7f0a9SMikko Perttunen 961*73a7f0a9SMikko Perttunen emc->timings = devm_kcalloc(emc->dev, child_count, sizeof(*timing), 962*73a7f0a9SMikko Perttunen GFP_KERNEL); 963*73a7f0a9SMikko Perttunen if (!emc->timings) 964*73a7f0a9SMikko Perttunen return -ENOMEM; 965*73a7f0a9SMikko Perttunen 966*73a7f0a9SMikko Perttunen emc->num_timings = child_count; 967*73a7f0a9SMikko Perttunen 968*73a7f0a9SMikko Perttunen for_each_child_of_node(node, child) { 969*73a7f0a9SMikko Perttunen timing = &emc->timings[i++]; 970*73a7f0a9SMikko Perttunen 971*73a7f0a9SMikko Perttunen err = load_one_timing_from_dt(emc, timing, child); 972*73a7f0a9SMikko Perttunen if (err) 973*73a7f0a9SMikko Perttunen return err; 974*73a7f0a9SMikko Perttunen } 975*73a7f0a9SMikko Perttunen 976*73a7f0a9SMikko Perttunen sort(emc->timings, emc->num_timings, sizeof(*timing), cmp_timings, 977*73a7f0a9SMikko Perttunen NULL); 978*73a7f0a9SMikko Perttunen 979*73a7f0a9SMikko Perttunen return 0; 980*73a7f0a9SMikko Perttunen } 981*73a7f0a9SMikko Perttunen 982*73a7f0a9SMikko Perttunen static const struct of_device_id tegra_emc_of_match[] = { 983*73a7f0a9SMikko Perttunen { .compatible = "nvidia,tegra124-emc" }, 984*73a7f0a9SMikko Perttunen {} 985*73a7f0a9SMikko Perttunen }; 986*73a7f0a9SMikko Perttunen 987*73a7f0a9SMikko Perttunen static struct device_node * 988*73a7f0a9SMikko Perttunen tegra_emc_find_node_by_ram_code(struct device_node *node, u32 ram_code) 989*73a7f0a9SMikko Perttunen { 990*73a7f0a9SMikko Perttunen struct device_node *np; 991*73a7f0a9SMikko Perttunen int err; 992*73a7f0a9SMikko Perttunen 993*73a7f0a9SMikko Perttunen for_each_child_of_node(node, np) { 994*73a7f0a9SMikko Perttunen u32 value; 995*73a7f0a9SMikko Perttunen 996*73a7f0a9SMikko Perttunen err = of_property_read_u32(np, "nvidia,ram-code", &value); 997*73a7f0a9SMikko Perttunen if (err || (value != ram_code)) { 998*73a7f0a9SMikko Perttunen of_node_put(np); 999*73a7f0a9SMikko Perttunen continue; 1000*73a7f0a9SMikko Perttunen } 1001*73a7f0a9SMikko Perttunen 1002*73a7f0a9SMikko Perttunen return np; 1003*73a7f0a9SMikko Perttunen } 1004*73a7f0a9SMikko Perttunen 1005*73a7f0a9SMikko Perttunen return NULL; 1006*73a7f0a9SMikko Perttunen } 1007*73a7f0a9SMikko Perttunen 1008*73a7f0a9SMikko Perttunen static int tegra_emc_probe(struct platform_device *pdev) 1009*73a7f0a9SMikko Perttunen { 1010*73a7f0a9SMikko Perttunen struct platform_device *mc; 1011*73a7f0a9SMikko Perttunen struct device_node *np; 1012*73a7f0a9SMikko Perttunen struct tegra_emc *emc; 1013*73a7f0a9SMikko Perttunen struct resource *res; 1014*73a7f0a9SMikko Perttunen u32 ram_code; 1015*73a7f0a9SMikko Perttunen int err; 1016*73a7f0a9SMikko Perttunen 1017*73a7f0a9SMikko Perttunen emc = devm_kzalloc(&pdev->dev, sizeof(*emc), GFP_KERNEL); 1018*73a7f0a9SMikko Perttunen if (!emc) 1019*73a7f0a9SMikko Perttunen return -ENOMEM; 1020*73a7f0a9SMikko Perttunen 1021*73a7f0a9SMikko Perttunen emc->dev = &pdev->dev; 1022*73a7f0a9SMikko Perttunen 1023*73a7f0a9SMikko Perttunen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1024*73a7f0a9SMikko Perttunen emc->regs = devm_ioremap_resource(&pdev->dev, res); 1025*73a7f0a9SMikko Perttunen if (IS_ERR(emc->regs)) 1026*73a7f0a9SMikko Perttunen return PTR_ERR(emc->regs); 1027*73a7f0a9SMikko Perttunen 1028*73a7f0a9SMikko Perttunen np = of_parse_phandle(pdev->dev.of_node, "nvidia,memory-controller", 0); 1029*73a7f0a9SMikko Perttunen if (!np) { 1030*73a7f0a9SMikko Perttunen dev_err(&pdev->dev, "could not get memory controller\n"); 1031*73a7f0a9SMikko Perttunen return -ENOENT; 1032*73a7f0a9SMikko Perttunen } 1033*73a7f0a9SMikko Perttunen 1034*73a7f0a9SMikko Perttunen mc = of_find_device_by_node(np); 1035*73a7f0a9SMikko Perttunen if (!mc) 1036*73a7f0a9SMikko Perttunen return -ENOENT; 1037*73a7f0a9SMikko Perttunen 1038*73a7f0a9SMikko Perttunen of_node_put(np); 1039*73a7f0a9SMikko Perttunen 1040*73a7f0a9SMikko Perttunen emc->mc = platform_get_drvdata(mc); 1041*73a7f0a9SMikko Perttunen if (!emc->mc) 1042*73a7f0a9SMikko Perttunen return -EPROBE_DEFER; 1043*73a7f0a9SMikko Perttunen 1044*73a7f0a9SMikko Perttunen ram_code = tegra_read_ram_code(); 1045*73a7f0a9SMikko Perttunen 1046*73a7f0a9SMikko Perttunen np = tegra_emc_find_node_by_ram_code(pdev->dev.of_node, ram_code); 1047*73a7f0a9SMikko Perttunen if (!np) { 1048*73a7f0a9SMikko Perttunen dev_err(&pdev->dev, 1049*73a7f0a9SMikko Perttunen "no memory timings for RAM code %u found in DT\n", 1050*73a7f0a9SMikko Perttunen ram_code); 1051*73a7f0a9SMikko Perttunen return -ENOENT; 1052*73a7f0a9SMikko Perttunen } 1053*73a7f0a9SMikko Perttunen 1054*73a7f0a9SMikko Perttunen err = tegra_emc_load_timings_from_dt(emc, np); 1055*73a7f0a9SMikko Perttunen 1056*73a7f0a9SMikko Perttunen of_node_put(np); 1057*73a7f0a9SMikko Perttunen 1058*73a7f0a9SMikko Perttunen if (err) 1059*73a7f0a9SMikko Perttunen return err; 1060*73a7f0a9SMikko Perttunen 1061*73a7f0a9SMikko Perttunen if (emc->num_timings == 0) { 1062*73a7f0a9SMikko Perttunen dev_err(&pdev->dev, 1063*73a7f0a9SMikko Perttunen "no memory timings for RAM code %u registered\n", 1064*73a7f0a9SMikko Perttunen ram_code); 1065*73a7f0a9SMikko Perttunen return -ENOENT; 1066*73a7f0a9SMikko Perttunen } 1067*73a7f0a9SMikko Perttunen 1068*73a7f0a9SMikko Perttunen err = emc_init(emc); 1069*73a7f0a9SMikko Perttunen if (err) { 1070*73a7f0a9SMikko Perttunen dev_err(&pdev->dev, "EMC initialization failed: %d\n", err); 1071*73a7f0a9SMikko Perttunen return err; 1072*73a7f0a9SMikko Perttunen } 1073*73a7f0a9SMikko Perttunen 1074*73a7f0a9SMikko Perttunen platform_set_drvdata(pdev, emc); 1075*73a7f0a9SMikko Perttunen 1076*73a7f0a9SMikko Perttunen return 0; 1077*73a7f0a9SMikko Perttunen }; 1078*73a7f0a9SMikko Perttunen 1079*73a7f0a9SMikko Perttunen static struct platform_driver tegra_emc_driver = { 1080*73a7f0a9SMikko Perttunen .probe = tegra_emc_probe, 1081*73a7f0a9SMikko Perttunen .driver = { 1082*73a7f0a9SMikko Perttunen .name = "tegra-emc", 1083*73a7f0a9SMikko Perttunen .of_match_table = tegra_emc_of_match, 1084*73a7f0a9SMikko Perttunen .suppress_bind_attrs = true, 1085*73a7f0a9SMikko Perttunen }, 1086*73a7f0a9SMikko Perttunen }; 1087*73a7f0a9SMikko Perttunen 1088*73a7f0a9SMikko Perttunen static int tegra_emc_init(void) 1089*73a7f0a9SMikko Perttunen { 1090*73a7f0a9SMikko Perttunen return platform_driver_register(&tegra_emc_driver); 1091*73a7f0a9SMikko Perttunen } 1092*73a7f0a9SMikko Perttunen subsys_initcall(tegra_emc_init); 1093