Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Platform UFS Host driver for Cadence controller
0004  *
0005  * Copyright (C) 2018 Cadence Design Systems, Inc.
0006  *
0007  * Authors:
0008  *  Jan Kotas <jank@cadence.com>
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      * cdns_ufs_dme_attr_val - for storing L4 attributes
0028      */
0029     u32 cdns_ufs_dme_attr_val[CDNS_UFS_MAX_L4_ATTRS];
0030 };
0031 
0032 /**
0033  * cdns_ufs_get_l4_attr - get L4 attributes on local side
0034  * @hba: per adapter instance
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  * cdns_ufs_set_l4_attr - set L4 attributes on local side
0069  * @hba: per adapter instance
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  * cdns_ufs_set_hclkdiv()
0105  * Sets HCLKDIV register value based on the core_clk
0106  * @hba: host controller instance
0107  *
0108  * Return zero for success and non-zero for failure
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      * Make sure the register was updated,
0138      * UniPro layer will not work with an incorrect value.
0139      */
0140     mb();
0141 
0142     return 0;
0143 }
0144 
0145 /**
0146  * cdns_ufs_hce_enable_notify()
0147  * Called before and after HCE enable bit is set.
0148  * @hba: host controller instance
0149  * @status: notify stage (pre, post change)
0150  *
0151  * Return zero for success and non-zero for failure
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  * cdns_ufs_hibern8_notify()
0164  * Called around hibern8 enter/exit.
0165  * @hba: host controller instance
0166  * @cmd: UIC Command
0167  * @status: notify stage (pre, post change)
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  * cdns_ufs_link_startup_notify()
0181  * Called before and after Link startup is carried out.
0182  * @hba: host controller instance
0183  * @status: notify stage (pre, post change)
0184  *
0185  * Return zero for success and non-zero for failure
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      * Some UFS devices have issues if LCC is enabled.
0195      * So we are setting PA_Local_TX_LCC_Enable to 0
0196      * before link startup which will make sure that both host
0197      * and device TX LCC are disabled once link startup is
0198      * completed.
0199      */
0200     ufshcd_disable_host_tx_lcc(hba);
0201 
0202     /*
0203      * Disabling Autohibern8 feature in cadence UFS
0204      * to mask unexpected interrupt trigger.
0205      */
0206     hba->ahit = 0;
0207 
0208     return 0;
0209 }
0210 
0211 /**
0212  * cdns_ufs_init - performs additional ufs initialization
0213  * @hba: host controller instance
0214  *
0215  * Returns status of initialization
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  * cdns_ufs_m31_16nm_phy_initialization - performs m31 phy initialization
0236  * @hba: host controller instance
0237  *
0238  * Always returns 0
0239  */
0240 static int cdns_ufs_m31_16nm_phy_initialization(struct ufs_hba *hba)
0241 {
0242     u32 data;
0243 
0244     /* Increase RX_Advanced_Min_ActivateTime_Capability */
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  * cdns_ufs_pltfrm_probe - probe routine of the driver
0285  * @pdev: pointer to platform device handle
0286  *
0287  * Return zero for success and non-zero for failure
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     /* Perform generic probe */
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  * cdns_ufs_pltfrm_remove - removes the ufs driver
0309  * @pdev: pointer to platform device handle
0310  *
0311  * Always returns 0
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");