Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
0004  *
0005  * Author:
0006  *  Mikko Perttunen <mperttunen@nvidia.com>
0007  */
0008 
0009 #include <linux/clk-provider.h>
0010 #include <linux/clk.h>
0011 #include <linux/clkdev.h>
0012 #include <linux/clk/tegra.h>
0013 #include <linux/debugfs.h>
0014 #include <linux/delay.h>
0015 #include <linux/interconnect-provider.h>
0016 #include <linux/io.h>
0017 #include <linux/module.h>
0018 #include <linux/mutex.h>
0019 #include <linux/of_address.h>
0020 #include <linux/of_platform.h>
0021 #include <linux/platform_device.h>
0022 #include <linux/pm_opp.h>
0023 #include <linux/sort.h>
0024 #include <linux/string.h>
0025 
0026 #include <soc/tegra/fuse.h>
0027 #include <soc/tegra/mc.h>
0028 
0029 #include "mc.h"
0030 
0031 #define EMC_FBIO_CFG5               0x104
0032 #define EMC_FBIO_CFG5_DRAM_TYPE_MASK        0x3
0033 #define EMC_FBIO_CFG5_DRAM_TYPE_SHIFT       0
0034 #define EMC_FBIO_CFG5_DRAM_WIDTH_X64        BIT(4)
0035 
0036 #define EMC_INTSTATUS               0x0
0037 #define EMC_INTSTATUS_CLKCHANGE_COMPLETE    BIT(4)
0038 
0039 #define EMC_CFG                 0xc
0040 #define EMC_CFG_DRAM_CLKSTOP_PD         BIT(31)
0041 #define EMC_CFG_DRAM_CLKSTOP_SR         BIT(30)
0042 #define EMC_CFG_DRAM_ACPD           BIT(29)
0043 #define EMC_CFG_DYN_SREF            BIT(28)
0044 #define EMC_CFG_PWR_MASK            ((0xF << 28) | BIT(18))
0045 #define EMC_CFG_DSR_VTTGEN_DRV_EN       BIT(18)
0046 
0047 #define EMC_REFCTRL             0x20
0048 #define EMC_REFCTRL_DEV_SEL_SHIFT       0
0049 #define EMC_REFCTRL_ENABLE          BIT(31)
0050 
0051 #define EMC_TIMING_CONTROL          0x28
0052 #define EMC_RC                  0x2c
0053 #define EMC_RFC                 0x30
0054 #define EMC_RAS                 0x34
0055 #define EMC_RP                  0x38
0056 #define EMC_R2W                 0x3c
0057 #define EMC_W2R                 0x40
0058 #define EMC_R2P                 0x44
0059 #define EMC_W2P                 0x48
0060 #define EMC_RD_RCD              0x4c
0061 #define EMC_WR_RCD              0x50
0062 #define EMC_RRD                 0x54
0063 #define EMC_REXT                0x58
0064 #define EMC_WDV                 0x5c
0065 #define EMC_QUSE                0x60
0066 #define EMC_QRST                0x64
0067 #define EMC_QSAFE               0x68
0068 #define EMC_RDV                 0x6c
0069 #define EMC_REFRESH             0x70
0070 #define EMC_BURST_REFRESH_NUM           0x74
0071 #define EMC_PDEX2WR             0x78
0072 #define EMC_PDEX2RD             0x7c
0073 #define EMC_PCHG2PDEN               0x80
0074 #define EMC_ACT2PDEN                0x84
0075 #define EMC_AR2PDEN             0x88
0076 #define EMC_RW2PDEN             0x8c
0077 #define EMC_TXSR                0x90
0078 #define EMC_TCKE                0x94
0079 #define EMC_TFAW                0x98
0080 #define EMC_TRPAB               0x9c
0081 #define EMC_TCLKSTABLE              0xa0
0082 #define EMC_TCLKSTOP                0xa4
0083 #define EMC_TREFBW              0xa8
0084 #define EMC_ODT_WRITE               0xb0
0085 #define EMC_ODT_READ                0xb4
0086 #define EMC_WEXT                0xb8
0087 #define EMC_CTT                 0xbc
0088 #define EMC_RFC_SLR             0xc0
0089 #define EMC_MRS_WAIT_CNT2           0xc4
0090 
0091 #define EMC_MRS_WAIT_CNT            0xc8
0092 #define EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT   0
0093 #define EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK    \
0094     (0x3FF << EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT)
0095 #define EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT    16
0096 #define EMC_MRS_WAIT_CNT_LONG_WAIT_MASK     \
0097     (0x3FF << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT)
0098 
0099 #define EMC_MRS                 0xcc
0100 #define EMC_MODE_SET_DLL_RESET          BIT(8)
0101 #define EMC_MODE_SET_LONG_CNT           BIT(26)
0102 #define EMC_EMRS                0xd0
0103 #define EMC_REF                 0xd4
0104 #define EMC_PRE                 0xd8
0105 
0106 #define EMC_SELF_REF                0xe0
0107 #define EMC_SELF_REF_CMD_ENABLED        BIT(0)
0108 #define EMC_SELF_REF_DEV_SEL_SHIFT      30
0109 
0110 #define EMC_MRW                 0xe8
0111 
0112 #define EMC_MRR                 0xec
0113 #define EMC_MRR_MA_SHIFT            16
0114 #define LPDDR2_MR4_TEMP_SHIFT           0
0115 
0116 #define EMC_XM2DQSPADCTRL3          0xf8
0117 #define EMC_FBIO_SPARE              0x100
0118 
0119 #define EMC_FBIO_CFG6               0x114
0120 #define EMC_EMRS2               0x12c
0121 #define EMC_MRW2                0x134
0122 #define EMC_MRW4                0x13c
0123 #define EMC_EINPUT              0x14c
0124 #define EMC_EINPUT_DURATION         0x150
0125 #define EMC_PUTERM_EXTRA            0x154
0126 #define EMC_TCKESR              0x158
0127 #define EMC_TPD                 0x15c
0128 
0129 #define EMC_AUTO_CAL_CONFIG         0x2a4
0130 #define EMC_AUTO_CAL_CONFIG_AUTO_CAL_START  BIT(31)
0131 #define EMC_AUTO_CAL_INTERVAL           0x2a8
0132 #define EMC_AUTO_CAL_STATUS         0x2ac
0133 #define EMC_AUTO_CAL_STATUS_ACTIVE      BIT(31)
0134 #define EMC_STATUS              0x2b4
0135 #define EMC_STATUS_TIMING_UPDATE_STALLED    BIT(23)
0136 
0137 #define EMC_CFG_2               0x2b8
0138 #define EMC_CFG_2_MODE_SHIFT            0
0139 #define EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR  BIT(6)
0140 
0141 #define EMC_CFG_DIG_DLL             0x2bc
0142 #define EMC_CFG_DIG_DLL_PERIOD          0x2c0
0143 #define EMC_RDV_MASK                0x2cc
0144 #define EMC_WDV_MASK                0x2d0
0145 #define EMC_CTT_DURATION            0x2d8
0146 #define EMC_CTT_TERM_CTRL           0x2dc
0147 #define EMC_ZCAL_INTERVAL           0x2e0
0148 #define EMC_ZCAL_WAIT_CNT           0x2e4
0149 
0150 #define EMC_ZQ_CAL              0x2ec
0151 #define EMC_ZQ_CAL_CMD              BIT(0)
0152 #define EMC_ZQ_CAL_LONG             BIT(4)
0153 #define EMC_ZQ_CAL_LONG_CMD_DEV0        \
0154     (DRAM_DEV_SEL_0 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD)
0155 #define EMC_ZQ_CAL_LONG_CMD_DEV1        \
0156     (DRAM_DEV_SEL_1 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD)
0157 
0158 #define EMC_XM2CMDPADCTRL           0x2f0
0159 #define EMC_XM2DQSPADCTRL           0x2f8
0160 #define EMC_XM2DQSPADCTRL2          0x2fc
0161 #define EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE BIT(0)
0162 #define EMC_XM2DQSPADCTRL2_VREF_ENABLE      BIT(5)
0163 #define EMC_XM2DQPADCTRL            0x300
0164 #define EMC_XM2DQPADCTRL2           0x304
0165 #define EMC_XM2CLKPADCTRL           0x308
0166 #define EMC_XM2COMPPADCTRL          0x30c
0167 #define EMC_XM2VTTGENPADCTRL            0x310
0168 #define EMC_XM2VTTGENPADCTRL2           0x314
0169 #define EMC_XM2VTTGENPADCTRL3           0x318
0170 #define EMC_XM2DQSPADCTRL4          0x320
0171 #define EMC_DLL_XFORM_DQS0          0x328
0172 #define EMC_DLL_XFORM_DQS1          0x32c
0173 #define EMC_DLL_XFORM_DQS2          0x330
0174 #define EMC_DLL_XFORM_DQS3          0x334
0175 #define EMC_DLL_XFORM_DQS4          0x338
0176 #define EMC_DLL_XFORM_DQS5          0x33c
0177 #define EMC_DLL_XFORM_DQS6          0x340
0178 #define EMC_DLL_XFORM_DQS7          0x344
0179 #define EMC_DLL_XFORM_QUSE0         0x348
0180 #define EMC_DLL_XFORM_QUSE1         0x34c
0181 #define EMC_DLL_XFORM_QUSE2         0x350
0182 #define EMC_DLL_XFORM_QUSE3         0x354
0183 #define EMC_DLL_XFORM_QUSE4         0x358
0184 #define EMC_DLL_XFORM_QUSE5         0x35c
0185 #define EMC_DLL_XFORM_QUSE6         0x360
0186 #define EMC_DLL_XFORM_QUSE7         0x364
0187 #define EMC_DLL_XFORM_DQ0           0x368
0188 #define EMC_DLL_XFORM_DQ1           0x36c
0189 #define EMC_DLL_XFORM_DQ2           0x370
0190 #define EMC_DLL_XFORM_DQ3           0x374
0191 #define EMC_DLI_TRIM_TXDQS0         0x3a8
0192 #define EMC_DLI_TRIM_TXDQS1         0x3ac
0193 #define EMC_DLI_TRIM_TXDQS2         0x3b0
0194 #define EMC_DLI_TRIM_TXDQS3         0x3b4
0195 #define EMC_DLI_TRIM_TXDQS4         0x3b8
0196 #define EMC_DLI_TRIM_TXDQS5         0x3bc
0197 #define EMC_DLI_TRIM_TXDQS6         0x3c0
0198 #define EMC_DLI_TRIM_TXDQS7         0x3c4
0199 #define EMC_STALL_THEN_EXE_AFTER_CLKCHANGE  0x3cc
0200 #define EMC_SEL_DPD_CTRL            0x3d8
0201 #define EMC_SEL_DPD_CTRL_DATA_SEL_DPD       BIT(8)
0202 #define EMC_SEL_DPD_CTRL_ODT_SEL_DPD        BIT(5)
0203 #define EMC_SEL_DPD_CTRL_RESET_SEL_DPD      BIT(4)
0204 #define EMC_SEL_DPD_CTRL_CA_SEL_DPD     BIT(3)
0205 #define EMC_SEL_DPD_CTRL_CLK_SEL_DPD        BIT(2)
0206 #define EMC_SEL_DPD_CTRL_DDR3_MASK  \
0207     ((0xf << 2) | BIT(8))
0208 #define EMC_SEL_DPD_CTRL_MASK \
0209     ((0x3 << 2) | BIT(5) | BIT(8))
0210 #define EMC_PRE_REFRESH_REQ_CNT         0x3dc
0211 #define EMC_DYN_SELF_REF_CONTROL        0x3e0
0212 #define EMC_TXSRDLL             0x3e4
0213 #define EMC_CCFIFO_ADDR             0x3e8
0214 #define EMC_CCFIFO_DATA             0x3ec
0215 #define EMC_CCFIFO_STATUS           0x3f0
0216 #define EMC_CDB_CNTL_1              0x3f4
0217 #define EMC_CDB_CNTL_2              0x3f8
0218 #define EMC_XM2CLKPADCTRL2          0x3fc
0219 #define EMC_AUTO_CAL_CONFIG2            0x458
0220 #define EMC_AUTO_CAL_CONFIG3            0x45c
0221 #define EMC_IBDLY               0x468
0222 #define EMC_DLL_XFORM_ADDR0         0x46c
0223 #define EMC_DLL_XFORM_ADDR1         0x470
0224 #define EMC_DLL_XFORM_ADDR2         0x474
0225 #define EMC_DSR_VTTGEN_DRV          0x47c
0226 #define EMC_TXDSRVTTGEN             0x480
0227 #define EMC_XM2CMDPADCTRL4          0x484
0228 #define EMC_XM2CMDPADCTRL5          0x488
0229 #define EMC_DLL_XFORM_DQS8          0x4a0
0230 #define EMC_DLL_XFORM_DQS9          0x4a4
0231 #define EMC_DLL_XFORM_DQS10         0x4a8
0232 #define EMC_DLL_XFORM_DQS11         0x4ac
0233 #define EMC_DLL_XFORM_DQS12         0x4b0
0234 #define EMC_DLL_XFORM_DQS13         0x4b4
0235 #define EMC_DLL_XFORM_DQS14         0x4b8
0236 #define EMC_DLL_XFORM_DQS15         0x4bc
0237 #define EMC_DLL_XFORM_QUSE8         0x4c0
0238 #define EMC_DLL_XFORM_QUSE9         0x4c4
0239 #define EMC_DLL_XFORM_QUSE10            0x4c8
0240 #define EMC_DLL_XFORM_QUSE11            0x4cc
0241 #define EMC_DLL_XFORM_QUSE12            0x4d0
0242 #define EMC_DLL_XFORM_QUSE13            0x4d4
0243 #define EMC_DLL_XFORM_QUSE14            0x4d8
0244 #define EMC_DLL_XFORM_QUSE15            0x4dc
0245 #define EMC_DLL_XFORM_DQ4           0x4e0
0246 #define EMC_DLL_XFORM_DQ5           0x4e4
0247 #define EMC_DLL_XFORM_DQ6           0x4e8
0248 #define EMC_DLL_XFORM_DQ7           0x4ec
0249 #define EMC_DLI_TRIM_TXDQS8         0x520
0250 #define EMC_DLI_TRIM_TXDQS9         0x524
0251 #define EMC_DLI_TRIM_TXDQS10            0x528
0252 #define EMC_DLI_TRIM_TXDQS11            0x52c
0253 #define EMC_DLI_TRIM_TXDQS12            0x530
0254 #define EMC_DLI_TRIM_TXDQS13            0x534
0255 #define EMC_DLI_TRIM_TXDQS14            0x538
0256 #define EMC_DLI_TRIM_TXDQS15            0x53c
0257 #define EMC_CDB_CNTL_3              0x540
0258 #define EMC_XM2DQSPADCTRL5          0x544
0259 #define EMC_XM2DQSPADCTRL6          0x548
0260 #define EMC_XM2DQPADCTRL3           0x54c
0261 #define EMC_DLL_XFORM_ADDR3         0x550
0262 #define EMC_DLL_XFORM_ADDR4         0x554
0263 #define EMC_DLL_XFORM_ADDR5         0x558
0264 #define EMC_CFG_PIPE                0x560
0265 #define EMC_QPOP                0x564
0266 #define EMC_QUSE_WIDTH              0x568
0267 #define EMC_PUTERM_WIDTH            0x56c
0268 #define EMC_BGBIAS_CTL0             0x570
0269 #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX BIT(3)
0270 #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN BIT(2)
0271 #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD    BIT(1)
0272 #define EMC_PUTERM_ADJ              0x574
0273 
0274 #define DRAM_DEV_SEL_ALL            0
0275 #define DRAM_DEV_SEL_0              BIT(31)
0276 #define DRAM_DEV_SEL_1              BIT(30)
0277 
0278 #define EMC_CFG_POWER_FEATURES_MASK     \
0279     (EMC_CFG_DYN_SREF | EMC_CFG_DRAM_ACPD | EMC_CFG_DRAM_CLKSTOP_SR | \
0280     EMC_CFG_DRAM_CLKSTOP_PD | EMC_CFG_DSR_VTTGEN_DRV_EN)
0281 #define EMC_REFCTRL_DEV_SEL(n) (((n > 1) ? 0 : 2) << EMC_REFCTRL_DEV_SEL_SHIFT)
0282 #define EMC_DRAM_DEV_SEL(n) ((n > 1) ? DRAM_DEV_SEL_ALL : DRAM_DEV_SEL_0)
0283 
0284 /* Maximum amount of time in us. to wait for changes to become effective */
0285 #define EMC_STATUS_UPDATE_TIMEOUT       1000
0286 
0287 enum emc_dram_type {
0288     DRAM_TYPE_DDR3 = 0,
0289     DRAM_TYPE_DDR1 = 1,
0290     DRAM_TYPE_LPDDR3 = 2,
0291     DRAM_TYPE_DDR2 = 3
0292 };
0293 
0294 enum emc_dll_change {
0295     DLL_CHANGE_NONE,
0296     DLL_CHANGE_ON,
0297     DLL_CHANGE_OFF
0298 };
0299 
0300 static const unsigned long emc_burst_regs[] = {
0301     EMC_RC,
0302     EMC_RFC,
0303     EMC_RFC_SLR,
0304     EMC_RAS,
0305     EMC_RP,
0306     EMC_R2W,
0307     EMC_W2R,
0308     EMC_R2P,
0309     EMC_W2P,
0310     EMC_RD_RCD,
0311     EMC_WR_RCD,
0312     EMC_RRD,
0313     EMC_REXT,
0314     EMC_WEXT,
0315     EMC_WDV,
0316     EMC_WDV_MASK,
0317     EMC_QUSE,
0318     EMC_QUSE_WIDTH,
0319     EMC_IBDLY,
0320     EMC_EINPUT,
0321     EMC_EINPUT_DURATION,
0322     EMC_PUTERM_EXTRA,
0323     EMC_PUTERM_WIDTH,
0324     EMC_PUTERM_ADJ,
0325     EMC_CDB_CNTL_1,
0326     EMC_CDB_CNTL_2,
0327     EMC_CDB_CNTL_3,
0328     EMC_QRST,
0329     EMC_QSAFE,
0330     EMC_RDV,
0331     EMC_RDV_MASK,
0332     EMC_REFRESH,
0333     EMC_BURST_REFRESH_NUM,
0334     EMC_PRE_REFRESH_REQ_CNT,
0335     EMC_PDEX2WR,
0336     EMC_PDEX2RD,
0337     EMC_PCHG2PDEN,
0338     EMC_ACT2PDEN,
0339     EMC_AR2PDEN,
0340     EMC_RW2PDEN,
0341     EMC_TXSR,
0342     EMC_TXSRDLL,
0343     EMC_TCKE,
0344     EMC_TCKESR,
0345     EMC_TPD,
0346     EMC_TFAW,
0347     EMC_TRPAB,
0348     EMC_TCLKSTABLE,
0349     EMC_TCLKSTOP,
0350     EMC_TREFBW,
0351     EMC_FBIO_CFG6,
0352     EMC_ODT_WRITE,
0353     EMC_ODT_READ,
0354     EMC_FBIO_CFG5,
0355     EMC_CFG_DIG_DLL,
0356     EMC_CFG_DIG_DLL_PERIOD,
0357     EMC_DLL_XFORM_DQS0,
0358     EMC_DLL_XFORM_DQS1,
0359     EMC_DLL_XFORM_DQS2,
0360     EMC_DLL_XFORM_DQS3,
0361     EMC_DLL_XFORM_DQS4,
0362     EMC_DLL_XFORM_DQS5,
0363     EMC_DLL_XFORM_DQS6,
0364     EMC_DLL_XFORM_DQS7,
0365     EMC_DLL_XFORM_DQS8,
0366     EMC_DLL_XFORM_DQS9,
0367     EMC_DLL_XFORM_DQS10,
0368     EMC_DLL_XFORM_DQS11,
0369     EMC_DLL_XFORM_DQS12,
0370     EMC_DLL_XFORM_DQS13,
0371     EMC_DLL_XFORM_DQS14,
0372     EMC_DLL_XFORM_DQS15,
0373     EMC_DLL_XFORM_QUSE0,
0374     EMC_DLL_XFORM_QUSE1,
0375     EMC_DLL_XFORM_QUSE2,
0376     EMC_DLL_XFORM_QUSE3,
0377     EMC_DLL_XFORM_QUSE4,
0378     EMC_DLL_XFORM_QUSE5,
0379     EMC_DLL_XFORM_QUSE6,
0380     EMC_DLL_XFORM_QUSE7,
0381     EMC_DLL_XFORM_ADDR0,
0382     EMC_DLL_XFORM_ADDR1,
0383     EMC_DLL_XFORM_ADDR2,
0384     EMC_DLL_XFORM_ADDR3,
0385     EMC_DLL_XFORM_ADDR4,
0386     EMC_DLL_XFORM_ADDR5,
0387     EMC_DLL_XFORM_QUSE8,
0388     EMC_DLL_XFORM_QUSE9,
0389     EMC_DLL_XFORM_QUSE10,
0390     EMC_DLL_XFORM_QUSE11,
0391     EMC_DLL_XFORM_QUSE12,
0392     EMC_DLL_XFORM_QUSE13,
0393     EMC_DLL_XFORM_QUSE14,
0394     EMC_DLL_XFORM_QUSE15,
0395     EMC_DLI_TRIM_TXDQS0,
0396     EMC_DLI_TRIM_TXDQS1,
0397     EMC_DLI_TRIM_TXDQS2,
0398     EMC_DLI_TRIM_TXDQS3,
0399     EMC_DLI_TRIM_TXDQS4,
0400     EMC_DLI_TRIM_TXDQS5,
0401     EMC_DLI_TRIM_TXDQS6,
0402     EMC_DLI_TRIM_TXDQS7,
0403     EMC_DLI_TRIM_TXDQS8,
0404     EMC_DLI_TRIM_TXDQS9,
0405     EMC_DLI_TRIM_TXDQS10,
0406     EMC_DLI_TRIM_TXDQS11,
0407     EMC_DLI_TRIM_TXDQS12,
0408     EMC_DLI_TRIM_TXDQS13,
0409     EMC_DLI_TRIM_TXDQS14,
0410     EMC_DLI_TRIM_TXDQS15,
0411     EMC_DLL_XFORM_DQ0,
0412     EMC_DLL_XFORM_DQ1,
0413     EMC_DLL_XFORM_DQ2,
0414     EMC_DLL_XFORM_DQ3,
0415     EMC_DLL_XFORM_DQ4,
0416     EMC_DLL_XFORM_DQ5,
0417     EMC_DLL_XFORM_DQ6,
0418     EMC_DLL_XFORM_DQ7,
0419     EMC_XM2CMDPADCTRL,
0420     EMC_XM2CMDPADCTRL4,
0421     EMC_XM2CMDPADCTRL5,
0422     EMC_XM2DQPADCTRL2,
0423     EMC_XM2DQPADCTRL3,
0424     EMC_XM2CLKPADCTRL,
0425     EMC_XM2CLKPADCTRL2,
0426     EMC_XM2COMPPADCTRL,
0427     EMC_XM2VTTGENPADCTRL,
0428     EMC_XM2VTTGENPADCTRL2,
0429     EMC_XM2VTTGENPADCTRL3,
0430     EMC_XM2DQSPADCTRL3,
0431     EMC_XM2DQSPADCTRL4,
0432     EMC_XM2DQSPADCTRL5,
0433     EMC_XM2DQSPADCTRL6,
0434     EMC_DSR_VTTGEN_DRV,
0435     EMC_TXDSRVTTGEN,
0436     EMC_FBIO_SPARE,
0437     EMC_ZCAL_WAIT_CNT,
0438     EMC_MRS_WAIT_CNT2,
0439     EMC_CTT,
0440     EMC_CTT_DURATION,
0441     EMC_CFG_PIPE,
0442     EMC_DYN_SELF_REF_CONTROL,
0443     EMC_QPOP
0444 };
0445 
0446 struct emc_timing {
0447     unsigned long rate;
0448 
0449     u32 emc_burst_data[ARRAY_SIZE(emc_burst_regs)];
0450 
0451     u32 emc_auto_cal_config;
0452     u32 emc_auto_cal_config2;
0453     u32 emc_auto_cal_config3;
0454     u32 emc_auto_cal_interval;
0455     u32 emc_bgbias_ctl0;
0456     u32 emc_cfg;
0457     u32 emc_cfg_2;
0458     u32 emc_ctt_term_ctrl;
0459     u32 emc_mode_1;
0460     u32 emc_mode_2;
0461     u32 emc_mode_4;
0462     u32 emc_mode_reset;
0463     u32 emc_mrs_wait_cnt;
0464     u32 emc_sel_dpd_ctrl;
0465     u32 emc_xm2dqspadctrl2;
0466     u32 emc_zcal_cnt_long;
0467     u32 emc_zcal_interval;
0468 };
0469 
0470 enum emc_rate_request_type {
0471     EMC_RATE_DEBUG,
0472     EMC_RATE_ICC,
0473     EMC_RATE_TYPE_MAX,
0474 };
0475 
0476 struct emc_rate_request {
0477     unsigned long min_rate;
0478     unsigned long max_rate;
0479 };
0480 
0481 struct tegra_emc {
0482     struct device *dev;
0483 
0484     struct tegra_mc *mc;
0485 
0486     void __iomem *regs;
0487 
0488     struct clk *clk;
0489 
0490     enum emc_dram_type dram_type;
0491     unsigned int dram_bus_width;
0492     unsigned int dram_num;
0493 
0494     struct emc_timing last_timing;
0495     struct emc_timing *timings;
0496     unsigned int num_timings;
0497 
0498     struct {
0499         struct dentry *root;
0500         unsigned long min_rate;
0501         unsigned long max_rate;
0502     } debugfs;
0503 
0504     struct icc_provider provider;
0505 
0506     /*
0507      * There are multiple sources in the EMC driver which could request
0508      * a min/max clock rate, these rates are contained in this array.
0509      */
0510     struct emc_rate_request requested_rate[EMC_RATE_TYPE_MAX];
0511 
0512     /* protect shared rate-change code path */
0513     struct mutex rate_lock;
0514 };
0515 
0516 /* Timing change sequence functions */
0517 
0518 static void emc_ccfifo_writel(struct tegra_emc *emc, u32 value,
0519                   unsigned long offset)
0520 {
0521     writel(value, emc->regs + EMC_CCFIFO_DATA);
0522     writel(offset, emc->regs + EMC_CCFIFO_ADDR);
0523 }
0524 
0525 static void emc_seq_update_timing(struct tegra_emc *emc)
0526 {
0527     unsigned int i;
0528     u32 value;
0529 
0530     writel(1, emc->regs + EMC_TIMING_CONTROL);
0531 
0532     for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
0533         value = readl(emc->regs + EMC_STATUS);
0534         if ((value & EMC_STATUS_TIMING_UPDATE_STALLED) == 0)
0535             return;
0536         udelay(1);
0537     }
0538 
0539     dev_err(emc->dev, "timing update timed out\n");
0540 }
0541 
0542 static void emc_seq_disable_auto_cal(struct tegra_emc *emc)
0543 {
0544     unsigned int i;
0545     u32 value;
0546 
0547     writel(0, emc->regs + EMC_AUTO_CAL_INTERVAL);
0548 
0549     for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
0550         value = readl(emc->regs + EMC_AUTO_CAL_STATUS);
0551         if ((value & EMC_AUTO_CAL_STATUS_ACTIVE) == 0)
0552             return;
0553         udelay(1);
0554     }
0555 
0556     dev_err(emc->dev, "auto cal disable timed out\n");
0557 }
0558 
0559 static void emc_seq_wait_clkchange(struct tegra_emc *emc)
0560 {
0561     unsigned int i;
0562     u32 value;
0563 
0564     for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
0565         value = readl(emc->regs + EMC_INTSTATUS);
0566         if (value & EMC_INTSTATUS_CLKCHANGE_COMPLETE)
0567             return;
0568         udelay(1);
0569     }
0570 
0571     dev_err(emc->dev, "clock change timed out\n");
0572 }
0573 
0574 static struct emc_timing *tegra_emc_find_timing(struct tegra_emc *emc,
0575                         unsigned long rate)
0576 {
0577     struct emc_timing *timing = NULL;
0578     unsigned int i;
0579 
0580     for (i = 0; i < emc->num_timings; i++) {
0581         if (emc->timings[i].rate == rate) {
0582             timing = &emc->timings[i];
0583             break;
0584         }
0585     }
0586 
0587     if (!timing) {
0588         dev_err(emc->dev, "no timing for rate %lu\n", rate);
0589         return NULL;
0590     }
0591 
0592     return timing;
0593 }
0594 
0595 static int tegra_emc_prepare_timing_change(struct tegra_emc *emc,
0596                        unsigned long rate)
0597 {
0598     struct emc_timing *timing = tegra_emc_find_timing(emc, rate);
0599     struct emc_timing *last = &emc->last_timing;
0600     enum emc_dll_change dll_change;
0601     unsigned int pre_wait = 0;
0602     u32 val, val2, mask;
0603     bool update = false;
0604     unsigned int i;
0605 
0606     if (!timing)
0607         return -ENOENT;
0608 
0609     if ((last->emc_mode_1 & 0x1) == (timing->emc_mode_1 & 0x1))
0610         dll_change = DLL_CHANGE_NONE;
0611     else if (timing->emc_mode_1 & 0x1)
0612         dll_change = DLL_CHANGE_ON;
0613     else
0614         dll_change = DLL_CHANGE_OFF;
0615 
0616     /* Clear CLKCHANGE_COMPLETE interrupts */
0617     writel(EMC_INTSTATUS_CLKCHANGE_COMPLETE, emc->regs + EMC_INTSTATUS);
0618 
0619     /* Disable dynamic self-refresh */
0620     val = readl(emc->regs + EMC_CFG);
0621     if (val & EMC_CFG_PWR_MASK) {
0622         val &= ~EMC_CFG_POWER_FEATURES_MASK;
0623         writel(val, emc->regs + EMC_CFG);
0624 
0625         pre_wait = 5;
0626     }
0627 
0628     /* Disable SEL_DPD_CTRL for clock change */
0629     if (emc->dram_type == DRAM_TYPE_DDR3)
0630         mask = EMC_SEL_DPD_CTRL_DDR3_MASK;
0631     else
0632         mask = EMC_SEL_DPD_CTRL_MASK;
0633 
0634     val = readl(emc->regs + EMC_SEL_DPD_CTRL);
0635     if (val & mask) {
0636         val &= ~mask;
0637         writel(val, emc->regs + EMC_SEL_DPD_CTRL);
0638     }
0639 
0640     /* Prepare DQ/DQS for clock change */
0641     val = readl(emc->regs + EMC_BGBIAS_CTL0);
0642     val2 = last->emc_bgbias_ctl0;
0643     if (!(timing->emc_bgbias_ctl0 &
0644           EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) &&
0645         (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX)) {
0646         val2 &= ~EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX;
0647         update = true;
0648     }
0649 
0650     if ((val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD) ||
0651         (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN)) {
0652         update = true;
0653     }
0654 
0655     if (update) {
0656         writel(val2, emc->regs + EMC_BGBIAS_CTL0);
0657         if (pre_wait < 5)
0658             pre_wait = 5;
0659     }
0660 
0661     update = false;
0662     val = readl(emc->regs + EMC_XM2DQSPADCTRL2);
0663     if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_VREF_ENABLE &&
0664         !(val & EMC_XM2DQSPADCTRL2_VREF_ENABLE)) {
0665         val |= EMC_XM2DQSPADCTRL2_VREF_ENABLE;
0666         update = true;
0667     }
0668 
0669     if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE &&
0670         !(val & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE)) {
0671         val |= EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE;
0672         update = true;
0673     }
0674 
0675     if (update) {
0676         writel(val, emc->regs + EMC_XM2DQSPADCTRL2);
0677         if (pre_wait < 30)
0678             pre_wait = 30;
0679     }
0680 
0681     /* Wait to settle */
0682     if (pre_wait) {
0683         emc_seq_update_timing(emc);
0684         udelay(pre_wait);
0685     }
0686 
0687     /* Program CTT_TERM control */
0688     if (last->emc_ctt_term_ctrl != timing->emc_ctt_term_ctrl) {
0689         emc_seq_disable_auto_cal(emc);
0690         writel(timing->emc_ctt_term_ctrl,
0691                emc->regs + EMC_CTT_TERM_CTRL);
0692         emc_seq_update_timing(emc);
0693     }
0694 
0695     /* Program burst shadow registers */
0696     for (i = 0; i < ARRAY_SIZE(timing->emc_burst_data); ++i)
0697         writel(timing->emc_burst_data[i],
0698                emc->regs + emc_burst_regs[i]);
0699 
0700     writel(timing->emc_xm2dqspadctrl2, emc->regs + EMC_XM2DQSPADCTRL2);
0701     writel(timing->emc_zcal_interval, emc->regs + EMC_ZCAL_INTERVAL);
0702 
0703     tegra_mc_write_emem_configuration(emc->mc, timing->rate);
0704 
0705     val = timing->emc_cfg & ~EMC_CFG_POWER_FEATURES_MASK;
0706     emc_ccfifo_writel(emc, val, EMC_CFG);
0707 
0708     /* Program AUTO_CAL_CONFIG */
0709     if (timing->emc_auto_cal_config2 != last->emc_auto_cal_config2)
0710         emc_ccfifo_writel(emc, timing->emc_auto_cal_config2,
0711                   EMC_AUTO_CAL_CONFIG2);
0712 
0713     if (timing->emc_auto_cal_config3 != last->emc_auto_cal_config3)
0714         emc_ccfifo_writel(emc, timing->emc_auto_cal_config3,
0715                   EMC_AUTO_CAL_CONFIG3);
0716 
0717     if (timing->emc_auto_cal_config != last->emc_auto_cal_config) {
0718         val = timing->emc_auto_cal_config;
0719         val &= EMC_AUTO_CAL_CONFIG_AUTO_CAL_START;
0720         emc_ccfifo_writel(emc, val, EMC_AUTO_CAL_CONFIG);
0721     }
0722 
0723     /* DDR3: predict MRS long wait count */
0724     if (emc->dram_type == DRAM_TYPE_DDR3 &&
0725         dll_change == DLL_CHANGE_ON) {
0726         u32 cnt = 512;
0727 
0728         if (timing->emc_zcal_interval != 0 &&
0729             last->emc_zcal_interval == 0)
0730             cnt -= emc->dram_num * 256;
0731 
0732         val = (timing->emc_mrs_wait_cnt
0733             & EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK)
0734             >> EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT;
0735         if (cnt < val)
0736             cnt = val;
0737 
0738         val = timing->emc_mrs_wait_cnt
0739             & ~EMC_MRS_WAIT_CNT_LONG_WAIT_MASK;
0740         val |= (cnt << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT)
0741             & EMC_MRS_WAIT_CNT_LONG_WAIT_MASK;
0742 
0743         writel(val, emc->regs + EMC_MRS_WAIT_CNT);
0744     }
0745 
0746     val = timing->emc_cfg_2;
0747     val &= ~EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR;
0748     emc_ccfifo_writel(emc, val, EMC_CFG_2);
0749 
0750     /* DDR3: Turn off DLL and enter self-refresh */
0751     if (emc->dram_type == DRAM_TYPE_DDR3 && dll_change == DLL_CHANGE_OFF)
0752         emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS);
0753 
0754     /* Disable refresh controller */
0755     emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num),
0756               EMC_REFCTRL);
0757     if (emc->dram_type == DRAM_TYPE_DDR3)
0758         emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num) |
0759                        EMC_SELF_REF_CMD_ENABLED,
0760                   EMC_SELF_REF);
0761 
0762     /* Flow control marker */
0763     emc_ccfifo_writel(emc, 1, EMC_STALL_THEN_EXE_AFTER_CLKCHANGE);
0764 
0765     /* DDR3: Exit self-refresh */
0766     if (emc->dram_type == DRAM_TYPE_DDR3)
0767         emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num),
0768                   EMC_SELF_REF);
0769     emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num) |
0770                    EMC_REFCTRL_ENABLE,
0771               EMC_REFCTRL);
0772 
0773     /* Set DRAM mode registers */
0774     if (emc->dram_type == DRAM_TYPE_DDR3) {
0775         if (timing->emc_mode_1 != last->emc_mode_1)
0776             emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS);
0777         if (timing->emc_mode_2 != last->emc_mode_2)
0778             emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_EMRS2);
0779 
0780         if ((timing->emc_mode_reset != last->emc_mode_reset) ||
0781             dll_change == DLL_CHANGE_ON) {
0782             val = timing->emc_mode_reset;
0783             if (dll_change == DLL_CHANGE_ON) {
0784                 val |= EMC_MODE_SET_DLL_RESET;
0785                 val |= EMC_MODE_SET_LONG_CNT;
0786             } else {
0787                 val &= ~EMC_MODE_SET_DLL_RESET;
0788             }
0789             emc_ccfifo_writel(emc, val, EMC_MRS);
0790         }
0791     } else {
0792         if (timing->emc_mode_2 != last->emc_mode_2)
0793             emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_MRW2);
0794         if (timing->emc_mode_1 != last->emc_mode_1)
0795             emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_MRW);
0796         if (timing->emc_mode_4 != last->emc_mode_4)
0797             emc_ccfifo_writel(emc, timing->emc_mode_4, EMC_MRW4);
0798     }
0799 
0800     /*  Issue ZCAL command if turning ZCAL on */
0801     if (timing->emc_zcal_interval != 0 && last->emc_zcal_interval == 0) {
0802         emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV0, EMC_ZQ_CAL);
0803         if (emc->dram_num > 1)
0804             emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV1,
0805                       EMC_ZQ_CAL);
0806     }
0807 
0808     /*  Write to RO register to remove stall after change */
0809     emc_ccfifo_writel(emc, 0, EMC_CCFIFO_STATUS);
0810 
0811     if (timing->emc_cfg_2 & EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR)
0812         emc_ccfifo_writel(emc, timing->emc_cfg_2, EMC_CFG_2);
0813 
0814     /* Disable AUTO_CAL for clock change */
0815     emc_seq_disable_auto_cal(emc);
0816 
0817     /* Read register to wait until programming has settled */
0818     readl(emc->regs + EMC_INTSTATUS);
0819 
0820     return 0;
0821 }
0822 
0823 static void tegra_emc_complete_timing_change(struct tegra_emc *emc,
0824                          unsigned long rate)
0825 {
0826     struct emc_timing *timing = tegra_emc_find_timing(emc, rate);
0827     struct emc_timing *last = &emc->last_timing;
0828     u32 val;
0829 
0830     if (!timing)
0831         return;
0832 
0833     /* Wait until the state machine has settled */
0834     emc_seq_wait_clkchange(emc);
0835 
0836     /* Restore AUTO_CAL */
0837     if (timing->emc_ctt_term_ctrl != last->emc_ctt_term_ctrl)
0838         writel(timing->emc_auto_cal_interval,
0839                emc->regs + EMC_AUTO_CAL_INTERVAL);
0840 
0841     /* Restore dynamic self-refresh */
0842     if (timing->emc_cfg & EMC_CFG_PWR_MASK)
0843         writel(timing->emc_cfg, emc->regs + EMC_CFG);
0844 
0845     /* Set ZCAL wait count */
0846     writel(timing->emc_zcal_cnt_long, emc->regs + EMC_ZCAL_WAIT_CNT);
0847 
0848     /* LPDDR3: Turn off BGBIAS if low frequency */
0849     if (emc->dram_type == DRAM_TYPE_LPDDR3 &&
0850         timing->emc_bgbias_ctl0 &
0851           EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) {
0852         val = timing->emc_bgbias_ctl0;
0853         val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN;
0854         val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD;
0855         writel(val, emc->regs + EMC_BGBIAS_CTL0);
0856     } else {
0857         if (emc->dram_type == DRAM_TYPE_DDR3 &&
0858             readl(emc->regs + EMC_BGBIAS_CTL0) !=
0859               timing->emc_bgbias_ctl0) {
0860             writel(timing->emc_bgbias_ctl0,
0861                    emc->regs + EMC_BGBIAS_CTL0);
0862         }
0863 
0864         writel(timing->emc_auto_cal_interval,
0865                emc->regs + EMC_AUTO_CAL_INTERVAL);
0866     }
0867 
0868     /* Wait for timing to settle */
0869     udelay(2);
0870 
0871     /* Reprogram SEL_DPD_CTRL */
0872     writel(timing->emc_sel_dpd_ctrl, emc->regs + EMC_SEL_DPD_CTRL);
0873     emc_seq_update_timing(emc);
0874 
0875     emc->last_timing = *timing;
0876 }
0877 
0878 /* Initialization and deinitialization */
0879 
0880 static void emc_read_current_timing(struct tegra_emc *emc,
0881                     struct emc_timing *timing)
0882 {
0883     unsigned int i;
0884 
0885     for (i = 0; i < ARRAY_SIZE(emc_burst_regs); ++i)
0886         timing->emc_burst_data[i] =
0887             readl(emc->regs + emc_burst_regs[i]);
0888 
0889     timing->emc_cfg = readl(emc->regs + EMC_CFG);
0890 
0891     timing->emc_auto_cal_interval = 0;
0892     timing->emc_zcal_cnt_long = 0;
0893     timing->emc_mode_1 = 0;
0894     timing->emc_mode_2 = 0;
0895     timing->emc_mode_4 = 0;
0896     timing->emc_mode_reset = 0;
0897 }
0898 
0899 static int emc_init(struct tegra_emc *emc)
0900 {
0901     emc->dram_type = readl(emc->regs + EMC_FBIO_CFG5);
0902 
0903     if (emc->dram_type & EMC_FBIO_CFG5_DRAM_WIDTH_X64)
0904         emc->dram_bus_width = 64;
0905     else
0906         emc->dram_bus_width = 32;
0907 
0908     dev_info_once(emc->dev, "%ubit DRAM bus\n", emc->dram_bus_width);
0909 
0910     emc->dram_type &= EMC_FBIO_CFG5_DRAM_TYPE_MASK;
0911     emc->dram_type >>= EMC_FBIO_CFG5_DRAM_TYPE_SHIFT;
0912 
0913     emc->dram_num = tegra_mc_get_emem_device_count(emc->mc);
0914 
0915     emc_read_current_timing(emc, &emc->last_timing);
0916 
0917     return 0;
0918 }
0919 
0920 static int load_one_timing_from_dt(struct tegra_emc *emc,
0921                    struct emc_timing *timing,
0922                    struct device_node *node)
0923 {
0924     u32 value;
0925     int err;
0926 
0927     err = of_property_read_u32(node, "clock-frequency", &value);
0928     if (err) {
0929         dev_err(emc->dev, "timing %pOFn: failed to read rate: %d\n",
0930             node, err);
0931         return err;
0932     }
0933 
0934     timing->rate = value;
0935 
0936     err = of_property_read_u32_array(node, "nvidia,emc-configuration",
0937                      timing->emc_burst_data,
0938                      ARRAY_SIZE(timing->emc_burst_data));
0939     if (err) {
0940         dev_err(emc->dev,
0941             "timing %pOFn: failed to read emc burst data: %d\n",
0942             node, err);
0943         return err;
0944     }
0945 
0946 #define EMC_READ_PROP(prop, dtprop) { \
0947     err = of_property_read_u32(node, dtprop, &timing->prop); \
0948     if (err) { \
0949         dev_err(emc->dev, "timing %pOFn: failed to read " #prop ": %d\n", \
0950             node, err); \
0951         return err; \
0952     } \
0953 }
0954 
0955     EMC_READ_PROP(emc_auto_cal_config, "nvidia,emc-auto-cal-config")
0956     EMC_READ_PROP(emc_auto_cal_config2, "nvidia,emc-auto-cal-config2")
0957     EMC_READ_PROP(emc_auto_cal_config3, "nvidia,emc-auto-cal-config3")
0958     EMC_READ_PROP(emc_auto_cal_interval, "nvidia,emc-auto-cal-interval")
0959     EMC_READ_PROP(emc_bgbias_ctl0, "nvidia,emc-bgbias-ctl0")
0960     EMC_READ_PROP(emc_cfg, "nvidia,emc-cfg")
0961     EMC_READ_PROP(emc_cfg_2, "nvidia,emc-cfg-2")
0962     EMC_READ_PROP(emc_ctt_term_ctrl, "nvidia,emc-ctt-term-ctrl")
0963     EMC_READ_PROP(emc_mode_1, "nvidia,emc-mode-1")
0964     EMC_READ_PROP(emc_mode_2, "nvidia,emc-mode-2")
0965     EMC_READ_PROP(emc_mode_4, "nvidia,emc-mode-4")
0966     EMC_READ_PROP(emc_mode_reset, "nvidia,emc-mode-reset")
0967     EMC_READ_PROP(emc_mrs_wait_cnt, "nvidia,emc-mrs-wait-cnt")
0968     EMC_READ_PROP(emc_sel_dpd_ctrl, "nvidia,emc-sel-dpd-ctrl")
0969     EMC_READ_PROP(emc_xm2dqspadctrl2, "nvidia,emc-xm2dqspadctrl2")
0970     EMC_READ_PROP(emc_zcal_cnt_long, "nvidia,emc-zcal-cnt-long")
0971     EMC_READ_PROP(emc_zcal_interval, "nvidia,emc-zcal-interval")
0972 
0973 #undef EMC_READ_PROP
0974 
0975     return 0;
0976 }
0977 
0978 static int cmp_timings(const void *_a, const void *_b)
0979 {
0980     const struct emc_timing *a = _a;
0981     const struct emc_timing *b = _b;
0982 
0983     if (a->rate < b->rate)
0984         return -1;
0985     else if (a->rate == b->rate)
0986         return 0;
0987     else
0988         return 1;
0989 }
0990 
0991 static int tegra_emc_load_timings_from_dt(struct tegra_emc *emc,
0992                       struct device_node *node)
0993 {
0994     int child_count = of_get_child_count(node);
0995     struct device_node *child;
0996     struct emc_timing *timing;
0997     unsigned int i = 0;
0998     int err;
0999 
1000     emc->timings = devm_kcalloc(emc->dev, child_count, sizeof(*timing),
1001                     GFP_KERNEL);
1002     if (!emc->timings)
1003         return -ENOMEM;
1004 
1005     emc->num_timings = child_count;
1006 
1007     for_each_child_of_node(node, child) {
1008         timing = &emc->timings[i++];
1009 
1010         err = load_one_timing_from_dt(emc, timing, child);
1011         if (err) {
1012             of_node_put(child);
1013             return err;
1014         }
1015     }
1016 
1017     sort(emc->timings, emc->num_timings, sizeof(*timing), cmp_timings,
1018          NULL);
1019 
1020     return 0;
1021 }
1022 
1023 static const struct of_device_id tegra_emc_of_match[] = {
1024     { .compatible = "nvidia,tegra124-emc" },
1025     { .compatible = "nvidia,tegra132-emc" },
1026     {}
1027 };
1028 MODULE_DEVICE_TABLE(of, tegra_emc_of_match);
1029 
1030 static struct device_node *
1031 tegra_emc_find_node_by_ram_code(struct device_node *node, u32 ram_code)
1032 {
1033     struct device_node *np;
1034     int err;
1035 
1036     for_each_child_of_node(node, np) {
1037         u32 value;
1038 
1039         err = of_property_read_u32(np, "nvidia,ram-code", &value);
1040         if (err || (value != ram_code))
1041             continue;
1042 
1043         return np;
1044     }
1045 
1046     return NULL;
1047 }
1048 
1049 static void tegra_emc_rate_requests_init(struct tegra_emc *emc)
1050 {
1051     unsigned int i;
1052 
1053     for (i = 0; i < EMC_RATE_TYPE_MAX; i++) {
1054         emc->requested_rate[i].min_rate = 0;
1055         emc->requested_rate[i].max_rate = ULONG_MAX;
1056     }
1057 }
1058 
1059 static int emc_request_rate(struct tegra_emc *emc,
1060                 unsigned long new_min_rate,
1061                 unsigned long new_max_rate,
1062                 enum emc_rate_request_type type)
1063 {
1064     struct emc_rate_request *req = emc->requested_rate;
1065     unsigned long min_rate = 0, max_rate = ULONG_MAX;
1066     unsigned int i;
1067     int err;
1068 
1069     /* select minimum and maximum rates among the requested rates */
1070     for (i = 0; i < EMC_RATE_TYPE_MAX; i++, req++) {
1071         if (i == type) {
1072             min_rate = max(new_min_rate, min_rate);
1073             max_rate = min(new_max_rate, max_rate);
1074         } else {
1075             min_rate = max(req->min_rate, min_rate);
1076             max_rate = min(req->max_rate, max_rate);
1077         }
1078     }
1079 
1080     if (min_rate > max_rate) {
1081         dev_err_ratelimited(emc->dev, "%s: type %u: out of range: %lu %lu\n",
1082                     __func__, type, min_rate, max_rate);
1083         return -ERANGE;
1084     }
1085 
1086     /*
1087      * EMC rate-changes should go via OPP API because it manages voltage
1088      * changes.
1089      */
1090     err = dev_pm_opp_set_rate(emc->dev, min_rate);
1091     if (err)
1092         return err;
1093 
1094     emc->requested_rate[type].min_rate = new_min_rate;
1095     emc->requested_rate[type].max_rate = new_max_rate;
1096 
1097     return 0;
1098 }
1099 
1100 static int emc_set_min_rate(struct tegra_emc *emc, unsigned long rate,
1101                 enum emc_rate_request_type type)
1102 {
1103     struct emc_rate_request *req = &emc->requested_rate[type];
1104     int ret;
1105 
1106     mutex_lock(&emc->rate_lock);
1107     ret = emc_request_rate(emc, rate, req->max_rate, type);
1108     mutex_unlock(&emc->rate_lock);
1109 
1110     return ret;
1111 }
1112 
1113 static int emc_set_max_rate(struct tegra_emc *emc, unsigned long rate,
1114                 enum emc_rate_request_type type)
1115 {
1116     struct emc_rate_request *req = &emc->requested_rate[type];
1117     int ret;
1118 
1119     mutex_lock(&emc->rate_lock);
1120     ret = emc_request_rate(emc, req->min_rate, rate, type);
1121     mutex_unlock(&emc->rate_lock);
1122 
1123     return ret;
1124 }
1125 
1126 /*
1127  * debugfs interface
1128  *
1129  * The memory controller driver exposes some files in debugfs that can be used
1130  * to control the EMC frequency. The top-level directory can be found here:
1131  *
1132  *   /sys/kernel/debug/emc
1133  *
1134  * It contains the following files:
1135  *
1136  *   - available_rates: This file contains a list of valid, space-separated
1137  *     EMC frequencies.
1138  *
1139  *   - min_rate: Writing a value to this file sets the given frequency as the
1140  *       floor of the permitted range. If this is higher than the currently
1141  *       configured EMC frequency, this will cause the frequency to be
1142  *       increased so that it stays within the valid range.
1143  *
1144  *   - max_rate: Similarily to the min_rate file, writing a value to this file
1145  *       sets the given frequency as the ceiling of the permitted range. If
1146  *       the value is lower than the currently configured EMC frequency, this
1147  *       will cause the frequency to be decreased so that it stays within the
1148  *       valid range.
1149  */
1150 
1151 static bool tegra_emc_validate_rate(struct tegra_emc *emc, unsigned long rate)
1152 {
1153     unsigned int i;
1154 
1155     for (i = 0; i < emc->num_timings; i++)
1156         if (rate == emc->timings[i].rate)
1157             return true;
1158 
1159     return false;
1160 }
1161 
1162 static int tegra_emc_debug_available_rates_show(struct seq_file *s,
1163                         void *data)
1164 {
1165     struct tegra_emc *emc = s->private;
1166     const char *prefix = "";
1167     unsigned int i;
1168 
1169     for (i = 0; i < emc->num_timings; i++) {
1170         seq_printf(s, "%s%lu", prefix, emc->timings[i].rate);
1171         prefix = " ";
1172     }
1173 
1174     seq_puts(s, "\n");
1175 
1176     return 0;
1177 }
1178 
1179 DEFINE_SHOW_ATTRIBUTE(tegra_emc_debug_available_rates);
1180 
1181 static int tegra_emc_debug_min_rate_get(void *data, u64 *rate)
1182 {
1183     struct tegra_emc *emc = data;
1184 
1185     *rate = emc->debugfs.min_rate;
1186 
1187     return 0;
1188 }
1189 
1190 static int tegra_emc_debug_min_rate_set(void *data, u64 rate)
1191 {
1192     struct tegra_emc *emc = data;
1193     int err;
1194 
1195     if (!tegra_emc_validate_rate(emc, rate))
1196         return -EINVAL;
1197 
1198     err = emc_set_min_rate(emc, rate, EMC_RATE_DEBUG);
1199     if (err < 0)
1200         return err;
1201 
1202     emc->debugfs.min_rate = rate;
1203 
1204     return 0;
1205 }
1206 
1207 DEFINE_DEBUGFS_ATTRIBUTE(tegra_emc_debug_min_rate_fops,
1208             tegra_emc_debug_min_rate_get,
1209             tegra_emc_debug_min_rate_set, "%llu\n");
1210 
1211 static int tegra_emc_debug_max_rate_get(void *data, u64 *rate)
1212 {
1213     struct tegra_emc *emc = data;
1214 
1215     *rate = emc->debugfs.max_rate;
1216 
1217     return 0;
1218 }
1219 
1220 static int tegra_emc_debug_max_rate_set(void *data, u64 rate)
1221 {
1222     struct tegra_emc *emc = data;
1223     int err;
1224 
1225     if (!tegra_emc_validate_rate(emc, rate))
1226         return -EINVAL;
1227 
1228     err = emc_set_max_rate(emc, rate, EMC_RATE_DEBUG);
1229     if (err < 0)
1230         return err;
1231 
1232     emc->debugfs.max_rate = rate;
1233 
1234     return 0;
1235 }
1236 
1237 DEFINE_DEBUGFS_ATTRIBUTE(tegra_emc_debug_max_rate_fops,
1238             tegra_emc_debug_max_rate_get,
1239             tegra_emc_debug_max_rate_set, "%llu\n");
1240 
1241 static void emc_debugfs_init(struct device *dev, struct tegra_emc *emc)
1242 {
1243     unsigned int i;
1244     int err;
1245 
1246     emc->debugfs.min_rate = ULONG_MAX;
1247     emc->debugfs.max_rate = 0;
1248 
1249     for (i = 0; i < emc->num_timings; i++) {
1250         if (emc->timings[i].rate < emc->debugfs.min_rate)
1251             emc->debugfs.min_rate = emc->timings[i].rate;
1252 
1253         if (emc->timings[i].rate > emc->debugfs.max_rate)
1254             emc->debugfs.max_rate = emc->timings[i].rate;
1255     }
1256 
1257     if (!emc->num_timings) {
1258         emc->debugfs.min_rate = clk_get_rate(emc->clk);
1259         emc->debugfs.max_rate = emc->debugfs.min_rate;
1260     }
1261 
1262     err = clk_set_rate_range(emc->clk, emc->debugfs.min_rate,
1263                  emc->debugfs.max_rate);
1264     if (err < 0) {
1265         dev_err(dev, "failed to set rate range [%lu-%lu] for %pC\n",
1266             emc->debugfs.min_rate, emc->debugfs.max_rate,
1267             emc->clk);
1268         return;
1269     }
1270 
1271     emc->debugfs.root = debugfs_create_dir("emc", NULL);
1272 
1273     debugfs_create_file("available_rates", 0444, emc->debugfs.root, emc,
1274                 &tegra_emc_debug_available_rates_fops);
1275     debugfs_create_file("min_rate", 0644, emc->debugfs.root,
1276                 emc, &tegra_emc_debug_min_rate_fops);
1277     debugfs_create_file("max_rate", 0644, emc->debugfs.root,
1278                 emc, &tegra_emc_debug_max_rate_fops);
1279 }
1280 
1281 static inline struct tegra_emc *
1282 to_tegra_emc_provider(struct icc_provider *provider)
1283 {
1284     return container_of(provider, struct tegra_emc, provider);
1285 }
1286 
1287 static struct icc_node_data *
1288 emc_of_icc_xlate_extended(struct of_phandle_args *spec, void *data)
1289 {
1290     struct icc_provider *provider = data;
1291     struct icc_node_data *ndata;
1292     struct icc_node *node;
1293 
1294     /* External Memory is the only possible ICC route */
1295     list_for_each_entry(node, &provider->nodes, node_list) {
1296         if (node->id != TEGRA_ICC_EMEM)
1297             continue;
1298 
1299         ndata = kzalloc(sizeof(*ndata), GFP_KERNEL);
1300         if (!ndata)
1301             return ERR_PTR(-ENOMEM);
1302 
1303         /*
1304          * SRC and DST nodes should have matching TAG in order to have
1305          * it set by default for a requested path.
1306          */
1307         ndata->tag = TEGRA_MC_ICC_TAG_ISO;
1308         ndata->node = node;
1309 
1310         return ndata;
1311     }
1312 
1313     return ERR_PTR(-EPROBE_DEFER);
1314 }
1315 
1316 static int emc_icc_set(struct icc_node *src, struct icc_node *dst)
1317 {
1318     struct tegra_emc *emc = to_tegra_emc_provider(dst->provider);
1319     unsigned long long peak_bw = icc_units_to_bps(dst->peak_bw);
1320     unsigned long long avg_bw = icc_units_to_bps(dst->avg_bw);
1321     unsigned long long rate = max(avg_bw, peak_bw);
1322     unsigned int dram_data_bus_width_bytes;
1323     const unsigned int ddr = 2;
1324     int err;
1325 
1326     /*
1327      * Tegra124 EMC runs on a clock rate of SDRAM bus. This means that
1328      * EMC clock rate is twice smaller than the peak data rate because
1329      * data is sampled on both EMC clock edges.
1330      */
1331     dram_data_bus_width_bytes = emc->dram_bus_width / 8;
1332     do_div(rate, ddr * dram_data_bus_width_bytes);
1333     rate = min_t(u64, rate, U32_MAX);
1334 
1335     err = emc_set_min_rate(emc, rate, EMC_RATE_ICC);
1336     if (err)
1337         return err;
1338 
1339     return 0;
1340 }
1341 
1342 static int tegra_emc_interconnect_init(struct tegra_emc *emc)
1343 {
1344     const struct tegra_mc_soc *soc = emc->mc->soc;
1345     struct icc_node *node;
1346     int err;
1347 
1348     emc->provider.dev = emc->dev;
1349     emc->provider.set = emc_icc_set;
1350     emc->provider.data = &emc->provider;
1351     emc->provider.aggregate = soc->icc_ops->aggregate;
1352     emc->provider.xlate_extended = emc_of_icc_xlate_extended;
1353 
1354     err = icc_provider_add(&emc->provider);
1355     if (err)
1356         goto err_msg;
1357 
1358     /* create External Memory Controller node */
1359     node = icc_node_create(TEGRA_ICC_EMC);
1360     if (IS_ERR(node)) {
1361         err = PTR_ERR(node);
1362         goto del_provider;
1363     }
1364 
1365     node->name = "External Memory Controller";
1366     icc_node_add(node, &emc->provider);
1367 
1368     /* link External Memory Controller to External Memory (DRAM) */
1369     err = icc_link_create(node, TEGRA_ICC_EMEM);
1370     if (err)
1371         goto remove_nodes;
1372 
1373     /* create External Memory node */
1374     node = icc_node_create(TEGRA_ICC_EMEM);
1375     if (IS_ERR(node)) {
1376         err = PTR_ERR(node);
1377         goto remove_nodes;
1378     }
1379 
1380     node->name = "External Memory (DRAM)";
1381     icc_node_add(node, &emc->provider);
1382 
1383     return 0;
1384 
1385 remove_nodes:
1386     icc_nodes_remove(&emc->provider);
1387 del_provider:
1388     icc_provider_del(&emc->provider);
1389 err_msg:
1390     dev_err(emc->dev, "failed to initialize ICC: %d\n", err);
1391 
1392     return err;
1393 }
1394 
1395 static int tegra_emc_opp_table_init(struct tegra_emc *emc)
1396 {
1397     u32 hw_version = BIT(tegra_sku_info.soc_speedo_id);
1398     int opp_token, err;
1399 
1400     err = dev_pm_opp_set_supported_hw(emc->dev, &hw_version, 1);
1401     if (err < 0) {
1402         dev_err(emc->dev, "failed to set OPP supported HW: %d\n", err);
1403         return err;
1404     }
1405     opp_token = err;
1406 
1407     err = dev_pm_opp_of_add_table(emc->dev);
1408     if (err) {
1409         if (err == -ENODEV)
1410             dev_err(emc->dev, "OPP table not found, please update your device tree\n");
1411         else
1412             dev_err(emc->dev, "failed to add OPP table: %d\n", err);
1413 
1414         goto put_hw_table;
1415     }
1416 
1417     dev_info_once(emc->dev, "OPP HW ver. 0x%x, current clock rate %lu MHz\n",
1418               hw_version, clk_get_rate(emc->clk) / 1000000);
1419 
1420     /* first dummy rate-set initializes voltage state */
1421     err = dev_pm_opp_set_rate(emc->dev, clk_get_rate(emc->clk));
1422     if (err) {
1423         dev_err(emc->dev, "failed to initialize OPP clock: %d\n", err);
1424         goto remove_table;
1425     }
1426 
1427     return 0;
1428 
1429 remove_table:
1430     dev_pm_opp_of_remove_table(emc->dev);
1431 put_hw_table:
1432     dev_pm_opp_put_supported_hw(opp_token);
1433 
1434     return err;
1435 }
1436 
1437 static void devm_tegra_emc_unset_callback(void *data)
1438 {
1439     tegra124_clk_set_emc_callbacks(NULL, NULL);
1440 }
1441 
1442 static int tegra_emc_probe(struct platform_device *pdev)
1443 {
1444     struct device_node *np;
1445     struct tegra_emc *emc;
1446     u32 ram_code;
1447     int err;
1448 
1449     emc = devm_kzalloc(&pdev->dev, sizeof(*emc), GFP_KERNEL);
1450     if (!emc)
1451         return -ENOMEM;
1452 
1453     mutex_init(&emc->rate_lock);
1454     emc->dev = &pdev->dev;
1455 
1456     emc->regs = devm_platform_ioremap_resource(pdev, 0);
1457     if (IS_ERR(emc->regs))
1458         return PTR_ERR(emc->regs);
1459 
1460     emc->mc = devm_tegra_memory_controller_get(&pdev->dev);
1461     if (IS_ERR(emc->mc))
1462         return PTR_ERR(emc->mc);
1463 
1464     ram_code = tegra_read_ram_code();
1465 
1466     np = tegra_emc_find_node_by_ram_code(pdev->dev.of_node, ram_code);
1467     if (np) {
1468         err = tegra_emc_load_timings_from_dt(emc, np);
1469         of_node_put(np);
1470         if (err)
1471             return err;
1472     } else {
1473         dev_info_once(&pdev->dev,
1474                   "no memory timings for RAM code %u found in DT\n",
1475                   ram_code);
1476     }
1477 
1478     err = emc_init(emc);
1479     if (err) {
1480         dev_err(&pdev->dev, "EMC initialization failed: %d\n", err);
1481         return err;
1482     }
1483 
1484     platform_set_drvdata(pdev, emc);
1485 
1486     tegra124_clk_set_emc_callbacks(tegra_emc_prepare_timing_change,
1487                        tegra_emc_complete_timing_change);
1488 
1489     err = devm_add_action_or_reset(&pdev->dev, devm_tegra_emc_unset_callback,
1490                        NULL);
1491     if (err)
1492         return err;
1493 
1494     emc->clk = devm_clk_get(&pdev->dev, "emc");
1495     if (IS_ERR(emc->clk)) {
1496         err = PTR_ERR(emc->clk);
1497         dev_err(&pdev->dev, "failed to get EMC clock: %d\n", err);
1498         return err;
1499     }
1500 
1501     err = tegra_emc_opp_table_init(emc);
1502     if (err)
1503         return err;
1504 
1505     tegra_emc_rate_requests_init(emc);
1506 
1507     if (IS_ENABLED(CONFIG_DEBUG_FS))
1508         emc_debugfs_init(&pdev->dev, emc);
1509 
1510     tegra_emc_interconnect_init(emc);
1511 
1512     /*
1513      * Don't allow the kernel module to be unloaded. Unloading adds some
1514      * extra complexity which doesn't really worth the effort in a case of
1515      * this driver.
1516      */
1517     try_module_get(THIS_MODULE);
1518 
1519     return 0;
1520 };
1521 
1522 static struct platform_driver tegra_emc_driver = {
1523     .probe = tegra_emc_probe,
1524     .driver = {
1525         .name = "tegra-emc",
1526         .of_match_table = tegra_emc_of_match,
1527         .suppress_bind_attrs = true,
1528         .sync_state = icc_sync_state,
1529     },
1530 };
1531 module_platform_driver(tegra_emc_driver);
1532 
1533 MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
1534 MODULE_DESCRIPTION("NVIDIA Tegra124 EMC driver");
1535 MODULE_LICENSE("GPL v2");