1 // SPDX-License-Identifier: GPL-2.0
3 * Platform UFS Host driver for Cadence controller
5 * Copyright (C) 2018 Cadence Design Systems, Inc.
8 * Jan Kotas <jank@cadence.com>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
16 #include <linux/time.h>
18 #include "ufshcd-pltfrm.h"
20 #define CDNS_UFS_REG_HCLKDIV 0xFC
21 #define CDNS_UFS_REG_PHY_XCFGD1 0x113C
24 * Sets HCLKDIV register value based on the core_clk
25 * @hba: host controller instance
27 * Return zero for success and non-zero for failure
29 static int cdns_ufs_set_hclkdiv(struct ufs_hba *hba)
31 struct ufs_clk_info *clki;
32 struct list_head *head = &hba->clk_list_head;
33 unsigned long core_clk_rate = 0;
39 list_for_each_entry(clki, head, list) {
40 if (IS_ERR_OR_NULL(clki->clk))
42 if (!strcmp(clki->name, "core_clk"))
43 core_clk_rate = clk_get_rate(clki->clk);
47 dev_err(hba->dev, "%s: unable to find core_clk rate\n",
52 core_clk_div = core_clk_rate / USEC_PER_SEC;
54 ufshcd_writel(hba, core_clk_div, CDNS_UFS_REG_HCLKDIV);
56 * Make sure the register was updated,
57 * UniPro layer will not work with an incorrect value.
65 * Sets clocks used by the controller
66 * @hba: host controller instance
67 * @on: if true, enable clocks, otherwise disable
68 * @status: notify stage (pre, post change)
70 * Return zero for success and non-zero for failure
72 static int cdns_ufs_setup_clocks(struct ufs_hba *hba, bool on,
73 enum ufs_notify_change_status status)
75 if ((!on) || (status == PRE_CHANGE))
78 return cdns_ufs_set_hclkdiv(hba);
82 * cdns_ufs_init - performs additional ufs initialization
83 * @hba: host controller instance
85 * Returns status of initialization
87 static int cdns_ufs_init(struct ufs_hba *hba)
91 if (hba->vops && hba->vops->phy_initialization)
92 status = hba->vops->phy_initialization(hba);
98 * cdns_ufs_m31_16nm_phy_initialization - performs m31 phy initialization
99 * @hba: host controller instance
103 static int cdns_ufs_m31_16nm_phy_initialization(struct ufs_hba *hba)
107 /* Increase RX_Advanced_Min_ActivateTime_Capability */
108 data = ufshcd_readl(hba, CDNS_UFS_REG_PHY_XCFGD1);
110 ufshcd_writel(hba, data, CDNS_UFS_REG_PHY_XCFGD1);
115 static const struct ufs_hba_variant_ops cdns_ufs_pltfm_hba_vops = {
116 .name = "cdns-ufs-pltfm",
117 .setup_clocks = cdns_ufs_setup_clocks,
120 static const struct ufs_hba_variant_ops cdns_ufs_m31_16nm_pltfm_hba_vops = {
121 .name = "cdns-ufs-pltfm",
122 .init = cdns_ufs_init,
123 .setup_clocks = cdns_ufs_setup_clocks,
124 .phy_initialization = cdns_ufs_m31_16nm_phy_initialization,
127 static const struct of_device_id cdns_ufs_of_match[] = {
129 .compatible = "cdns,ufshc",
130 .data = &cdns_ufs_pltfm_hba_vops,
133 .compatible = "cdns,ufshc-m31-16nm",
134 .data = &cdns_ufs_m31_16nm_pltfm_hba_vops,
139 MODULE_DEVICE_TABLE(of, cdns_ufs_of_match);
142 * cdns_ufs_pltfrm_probe - probe routine of the driver
143 * @pdev: pointer to platform device handle
145 * Return zero for success and non-zero for failure
147 static int cdns_ufs_pltfrm_probe(struct platform_device *pdev)
150 const struct of_device_id *of_id;
151 struct ufs_hba_variant_ops *vops;
152 struct device *dev = &pdev->dev;
154 of_id = of_match_node(cdns_ufs_of_match, dev->of_node);
155 vops = (struct ufs_hba_variant_ops *)of_id->data;
157 /* Perform generic probe */
158 err = ufshcd_pltfrm_init(pdev, vops);
160 dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
166 * cdns_ufs_pltfrm_remove - removes the ufs driver
167 * @pdev: pointer to platform device handle
171 static int cdns_ufs_pltfrm_remove(struct platform_device *pdev)
173 struct ufs_hba *hba = platform_get_drvdata(pdev);
179 static const struct dev_pm_ops cdns_ufs_dev_pm_ops = {
180 .suspend = ufshcd_pltfrm_suspend,
181 .resume = ufshcd_pltfrm_resume,
182 .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
183 .runtime_resume = ufshcd_pltfrm_runtime_resume,
184 .runtime_idle = ufshcd_pltfrm_runtime_idle,
187 static struct platform_driver cdns_ufs_pltfrm_driver = {
188 .probe = cdns_ufs_pltfrm_probe,
189 .remove = cdns_ufs_pltfrm_remove,
191 .name = "cdns-ufshcd",
192 .pm = &cdns_ufs_dev_pm_ops,
193 .of_match_table = cdns_ufs_of_match,
197 module_platform_driver(cdns_ufs_pltfrm_driver);
199 MODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
200 MODULE_DESCRIPTION("Cadence UFS host controller platform driver");
201 MODULE_LICENSE("GPL v2");
202 MODULE_VERSION(UFSHCD_DRIVER_VERSION);