0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/clk.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/of.h>
0017 #include <linux/time.h>
0018
0019 #include "ufshcd-pltfrm.h"
0020
0021 #define CDNS_UFS_REG_HCLKDIV 0xFC
0022 #define CDNS_UFS_REG_PHY_XCFGD1 0x113C
0023 #define CDNS_UFS_MAX_L4_ATTRS 12
0024
0025 struct cdns_ufs_host {
0026
0027
0028
0029 u32 cdns_ufs_dme_attr_val[CDNS_UFS_MAX_L4_ATTRS];
0030 };
0031
0032
0033
0034
0035
0036
0037 static void cdns_ufs_get_l4_attr(struct ufs_hba *hba)
0038 {
0039 struct cdns_ufs_host *host = ufshcd_get_variant(hba);
0040
0041 ufshcd_dme_get(hba, UIC_ARG_MIB(T_PEERDEVICEID),
0042 &host->cdns_ufs_dme_attr_val[0]);
0043 ufshcd_dme_get(hba, UIC_ARG_MIB(T_PEERCPORTID),
0044 &host->cdns_ufs_dme_attr_val[1]);
0045 ufshcd_dme_get(hba, UIC_ARG_MIB(T_TRAFFICCLASS),
0046 &host->cdns_ufs_dme_attr_val[2]);
0047 ufshcd_dme_get(hba, UIC_ARG_MIB(T_PROTOCOLID),
0048 &host->cdns_ufs_dme_attr_val[3]);
0049 ufshcd_dme_get(hba, UIC_ARG_MIB(T_CPORTFLAGS),
0050 &host->cdns_ufs_dme_attr_val[4]);
0051 ufshcd_dme_get(hba, UIC_ARG_MIB(T_TXTOKENVALUE),
0052 &host->cdns_ufs_dme_attr_val[5]);
0053 ufshcd_dme_get(hba, UIC_ARG_MIB(T_RXTOKENVALUE),
0054 &host->cdns_ufs_dme_attr_val[6]);
0055 ufshcd_dme_get(hba, UIC_ARG_MIB(T_LOCALBUFFERSPACE),
0056 &host->cdns_ufs_dme_attr_val[7]);
0057 ufshcd_dme_get(hba, UIC_ARG_MIB(T_PEERBUFFERSPACE),
0058 &host->cdns_ufs_dme_attr_val[8]);
0059 ufshcd_dme_get(hba, UIC_ARG_MIB(T_CREDITSTOSEND),
0060 &host->cdns_ufs_dme_attr_val[9]);
0061 ufshcd_dme_get(hba, UIC_ARG_MIB(T_CPORTMODE),
0062 &host->cdns_ufs_dme_attr_val[10]);
0063 ufshcd_dme_get(hba, UIC_ARG_MIB(T_CONNECTIONSTATE),
0064 &host->cdns_ufs_dme_attr_val[11]);
0065 }
0066
0067
0068
0069
0070
0071
0072 static void cdns_ufs_set_l4_attr(struct ufs_hba *hba)
0073 {
0074 struct cdns_ufs_host *host = ufshcd_get_variant(hba);
0075
0076 ufshcd_dme_set(hba, UIC_ARG_MIB(T_CONNECTIONSTATE), 0);
0077 ufshcd_dme_set(hba, UIC_ARG_MIB(T_PEERDEVICEID),
0078 host->cdns_ufs_dme_attr_val[0]);
0079 ufshcd_dme_set(hba, UIC_ARG_MIB(T_PEERCPORTID),
0080 host->cdns_ufs_dme_attr_val[1]);
0081 ufshcd_dme_set(hba, UIC_ARG_MIB(T_TRAFFICCLASS),
0082 host->cdns_ufs_dme_attr_val[2]);
0083 ufshcd_dme_set(hba, UIC_ARG_MIB(T_PROTOCOLID),
0084 host->cdns_ufs_dme_attr_val[3]);
0085 ufshcd_dme_set(hba, UIC_ARG_MIB(T_CPORTFLAGS),
0086 host->cdns_ufs_dme_attr_val[4]);
0087 ufshcd_dme_set(hba, UIC_ARG_MIB(T_TXTOKENVALUE),
0088 host->cdns_ufs_dme_attr_val[5]);
0089 ufshcd_dme_set(hba, UIC_ARG_MIB(T_RXTOKENVALUE),
0090 host->cdns_ufs_dme_attr_val[6]);
0091 ufshcd_dme_set(hba, UIC_ARG_MIB(T_LOCALBUFFERSPACE),
0092 host->cdns_ufs_dme_attr_val[7]);
0093 ufshcd_dme_set(hba, UIC_ARG_MIB(T_PEERBUFFERSPACE),
0094 host->cdns_ufs_dme_attr_val[8]);
0095 ufshcd_dme_set(hba, UIC_ARG_MIB(T_CREDITSTOSEND),
0096 host->cdns_ufs_dme_attr_val[9]);
0097 ufshcd_dme_set(hba, UIC_ARG_MIB(T_CPORTMODE),
0098 host->cdns_ufs_dme_attr_val[10]);
0099 ufshcd_dme_set(hba, UIC_ARG_MIB(T_CONNECTIONSTATE),
0100 host->cdns_ufs_dme_attr_val[11]);
0101 }
0102
0103
0104
0105
0106
0107
0108
0109
0110 static int cdns_ufs_set_hclkdiv(struct ufs_hba *hba)
0111 {
0112 struct ufs_clk_info *clki;
0113 struct list_head *head = &hba->clk_list_head;
0114 unsigned long core_clk_rate = 0;
0115 u32 core_clk_div = 0;
0116
0117 if (list_empty(head))
0118 return 0;
0119
0120 list_for_each_entry(clki, head, list) {
0121 if (IS_ERR_OR_NULL(clki->clk))
0122 continue;
0123 if (!strcmp(clki->name, "core_clk"))
0124 core_clk_rate = clk_get_rate(clki->clk);
0125 }
0126
0127 if (!core_clk_rate) {
0128 dev_err(hba->dev, "%s: unable to find core_clk rate\n",
0129 __func__);
0130 return -EINVAL;
0131 }
0132
0133 core_clk_div = core_clk_rate / USEC_PER_SEC;
0134
0135 ufshcd_writel(hba, core_clk_div, CDNS_UFS_REG_HCLKDIV);
0136
0137
0138
0139
0140 mb();
0141
0142 return 0;
0143 }
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153 static int cdns_ufs_hce_enable_notify(struct ufs_hba *hba,
0154 enum ufs_notify_change_status status)
0155 {
0156 if (status != PRE_CHANGE)
0157 return 0;
0158
0159 return cdns_ufs_set_hclkdiv(hba);
0160 }
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170 static void cdns_ufs_hibern8_notify(struct ufs_hba *hba, enum uic_cmd_dme cmd,
0171 enum ufs_notify_change_status status)
0172 {
0173 if (status == PRE_CHANGE && cmd == UIC_CMD_DME_HIBER_ENTER)
0174 cdns_ufs_get_l4_attr(hba);
0175 if (status == POST_CHANGE && cmd == UIC_CMD_DME_HIBER_EXIT)
0176 cdns_ufs_set_l4_attr(hba);
0177 }
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187 static int cdns_ufs_link_startup_notify(struct ufs_hba *hba,
0188 enum ufs_notify_change_status status)
0189 {
0190 if (status != PRE_CHANGE)
0191 return 0;
0192
0193
0194
0195
0196
0197
0198
0199
0200 ufshcd_disable_host_tx_lcc(hba);
0201
0202
0203
0204
0205
0206 hba->ahit = 0;
0207
0208 return 0;
0209 }
0210
0211
0212
0213
0214
0215
0216
0217 static int cdns_ufs_init(struct ufs_hba *hba)
0218 {
0219 int status = 0;
0220 struct cdns_ufs_host *host;
0221 struct device *dev = hba->dev;
0222
0223 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
0224
0225 if (!host)
0226 return -ENOMEM;
0227 ufshcd_set_variant(hba, host);
0228
0229 status = ufshcd_vops_phy_initialization(hba);
0230
0231 return status;
0232 }
0233
0234
0235
0236
0237
0238
0239
0240 static int cdns_ufs_m31_16nm_phy_initialization(struct ufs_hba *hba)
0241 {
0242 u32 data;
0243
0244
0245 data = ufshcd_readl(hba, CDNS_UFS_REG_PHY_XCFGD1);
0246 data |= BIT(24);
0247 ufshcd_writel(hba, data, CDNS_UFS_REG_PHY_XCFGD1);
0248
0249 return 0;
0250 }
0251
0252 static const struct ufs_hba_variant_ops cdns_ufs_pltfm_hba_vops = {
0253 .name = "cdns-ufs-pltfm",
0254 .init = cdns_ufs_init,
0255 .hce_enable_notify = cdns_ufs_hce_enable_notify,
0256 .link_startup_notify = cdns_ufs_link_startup_notify,
0257 .hibern8_notify = cdns_ufs_hibern8_notify,
0258 };
0259
0260 static const struct ufs_hba_variant_ops cdns_ufs_m31_16nm_pltfm_hba_vops = {
0261 .name = "cdns-ufs-pltfm",
0262 .init = cdns_ufs_init,
0263 .hce_enable_notify = cdns_ufs_hce_enable_notify,
0264 .link_startup_notify = cdns_ufs_link_startup_notify,
0265 .phy_initialization = cdns_ufs_m31_16nm_phy_initialization,
0266 .hibern8_notify = cdns_ufs_hibern8_notify,
0267 };
0268
0269 static const struct of_device_id cdns_ufs_of_match[] = {
0270 {
0271 .compatible = "cdns,ufshc",
0272 .data = &cdns_ufs_pltfm_hba_vops,
0273 },
0274 {
0275 .compatible = "cdns,ufshc-m31-16nm",
0276 .data = &cdns_ufs_m31_16nm_pltfm_hba_vops,
0277 },
0278 { },
0279 };
0280
0281 MODULE_DEVICE_TABLE(of, cdns_ufs_of_match);
0282
0283
0284
0285
0286
0287
0288
0289 static int cdns_ufs_pltfrm_probe(struct platform_device *pdev)
0290 {
0291 int err;
0292 const struct of_device_id *of_id;
0293 struct ufs_hba_variant_ops *vops;
0294 struct device *dev = &pdev->dev;
0295
0296 of_id = of_match_node(cdns_ufs_of_match, dev->of_node);
0297 vops = (struct ufs_hba_variant_ops *)of_id->data;
0298
0299
0300 err = ufshcd_pltfrm_init(pdev, vops);
0301 if (err)
0302 dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
0303
0304 return err;
0305 }
0306
0307
0308
0309
0310
0311
0312
0313 static int cdns_ufs_pltfrm_remove(struct platform_device *pdev)
0314 {
0315 struct ufs_hba *hba = platform_get_drvdata(pdev);
0316
0317 ufshcd_remove(hba);
0318 return 0;
0319 }
0320
0321 static const struct dev_pm_ops cdns_ufs_dev_pm_ops = {
0322 SET_SYSTEM_SLEEP_PM_OPS(ufshcd_system_suspend, ufshcd_system_resume)
0323 SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL)
0324 .prepare = ufshcd_suspend_prepare,
0325 .complete = ufshcd_resume_complete,
0326 };
0327
0328 static struct platform_driver cdns_ufs_pltfrm_driver = {
0329 .probe = cdns_ufs_pltfrm_probe,
0330 .remove = cdns_ufs_pltfrm_remove,
0331 .shutdown = ufshcd_pltfrm_shutdown,
0332 .driver = {
0333 .name = "cdns-ufshcd",
0334 .pm = &cdns_ufs_dev_pm_ops,
0335 .of_match_table = cdns_ufs_of_match,
0336 },
0337 };
0338
0339 module_platform_driver(cdns_ufs_pltfrm_driver);
0340
0341 MODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
0342 MODULE_DESCRIPTION("Cadence UFS host controller platform driver");
0343 MODULE_LICENSE("GPL v2");