Linux 5.1.15
[linux/fpc-iii.git] / drivers / scsi / ufs / cdns-pltfrm.c
blob4a37b4f5716403d1394fee1c5139c88b7b20a167
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Platform UFS Host driver for Cadence controller
5 * Copyright (C) 2018 Cadence Design Systems, Inc.
7 * Authors:
8 * Jan Kotas <jank@cadence.com>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/of.h>
16 #include <linux/time.h>
18 #include "ufshcd-pltfrm.h"
20 #define CDNS_UFS_REG_HCLKDIV 0xFC
22 /**
23 * Sets HCLKDIV register value based on the core_clk
24 * @hba: host controller instance
26 * Return zero for success and non-zero for failure
28 static int cdns_ufs_set_hclkdiv(struct ufs_hba *hba)
30 struct ufs_clk_info *clki;
31 struct list_head *head = &hba->clk_list_head;
32 unsigned long core_clk_rate = 0;
33 u32 core_clk_div = 0;
35 if (list_empty(head))
36 return 0;
38 list_for_each_entry(clki, head, list) {
39 if (IS_ERR_OR_NULL(clki->clk))
40 continue;
41 if (!strcmp(clki->name, "core_clk"))
42 core_clk_rate = clk_get_rate(clki->clk);
45 if (!core_clk_rate) {
46 dev_err(hba->dev, "%s: unable to find core_clk rate\n",
47 __func__);
48 return -EINVAL;
51 core_clk_div = core_clk_rate / USEC_PER_SEC;
53 ufshcd_writel(hba, core_clk_div, CDNS_UFS_REG_HCLKDIV);
54 /**
55 * Make sure the register was updated,
56 * UniPro layer will not work with an incorrect value.
58 mb();
60 return 0;
63 /**
64 * Sets clocks used by the controller
65 * @hba: host controller instance
66 * @on: if true, enable clocks, otherwise disable
67 * @status: notify stage (pre, post change)
69 * Return zero for success and non-zero for failure
71 static int cdns_ufs_setup_clocks(struct ufs_hba *hba, bool on,
72 enum ufs_notify_change_status status)
74 if ((!on) || (status == PRE_CHANGE))
75 return 0;
77 return cdns_ufs_set_hclkdiv(hba);
80 static struct ufs_hba_variant_ops cdns_pltfm_hba_vops = {
81 .name = "cdns-ufs-pltfm",
82 .setup_clocks = cdns_ufs_setup_clocks,
85 /**
86 * cdns_ufs_pltfrm_probe - probe routine of the driver
87 * @pdev: pointer to platform device handle
89 * Return zero for success and non-zero for failure
91 static int cdns_ufs_pltfrm_probe(struct platform_device *pdev)
93 int err;
94 struct device *dev = &pdev->dev;
96 /* Perform generic probe */
97 err = ufshcd_pltfrm_init(pdev, &cdns_pltfm_hba_vops);
98 if (err)
99 dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
101 return err;
105 * cdns_ufs_pltfrm_remove - removes the ufs driver
106 * @pdev: pointer to platform device handle
108 * Always returns 0
110 static int cdns_ufs_pltfrm_remove(struct platform_device *pdev)
112 struct ufs_hba *hba = platform_get_drvdata(pdev);
114 ufshcd_remove(hba);
115 return 0;
118 static const struct of_device_id cdns_ufs_of_match[] = {
119 { .compatible = "cdns,ufshc" },
123 MODULE_DEVICE_TABLE(of, cdns_ufs_of_match);
125 static const struct dev_pm_ops cdns_ufs_dev_pm_ops = {
126 .suspend = ufshcd_pltfrm_suspend,
127 .resume = ufshcd_pltfrm_resume,
128 .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
129 .runtime_resume = ufshcd_pltfrm_runtime_resume,
130 .runtime_idle = ufshcd_pltfrm_runtime_idle,
133 static struct platform_driver cdns_ufs_pltfrm_driver = {
134 .probe = cdns_ufs_pltfrm_probe,
135 .remove = cdns_ufs_pltfrm_remove,
136 .driver = {
137 .name = "cdns-ufshcd",
138 .pm = &cdns_ufs_dev_pm_ops,
139 .of_match_table = cdns_ufs_of_match,
143 module_platform_driver(cdns_ufs_pltfrm_driver);
145 MODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
146 MODULE_DESCRIPTION("Cadence UFS host controller platform driver");
147 MODULE_LICENSE("GPL v2");
148 MODULE_VERSION(UFSHCD_DRIVER_VERSION);