blob: b2af04c57a39b3f7675b83de97ea54196dde0fab [file] [log] [blame]
Janek Kotasd90996d2018-09-20 13:08:30 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Platform UFS Host driver for Cadence controller
4 *
5 * Copyright (C) 2018 Cadence Design Systems, Inc.
6 *
7 * Authors:
8 * Jan Kotas <jank@cadence.com>
9 *
10 */
11
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>
17
18#include "ufshcd-pltfrm.h"
19
Jan Kotas4fed62b2019-03-27 14:44:05 +000020#define CDNS_UFS_REG_HCLKDIV 0xFC
21#define CDNS_UFS_REG_PHY_XCFGD1 0x113C
Janek Kotasd90996d2018-09-20 13:08:30 +000022
23/**
24 * Sets HCLKDIV register value based on the core_clk
25 * @hba: host controller instance
26 *
27 * Return zero for success and non-zero for failure
28 */
29static int cdns_ufs_set_hclkdiv(struct ufs_hba *hba)
30{
31 struct ufs_clk_info *clki;
32 struct list_head *head = &hba->clk_list_head;
33 unsigned long core_clk_rate = 0;
34 u32 core_clk_div = 0;
35
36 if (list_empty(head))
37 return 0;
38
39 list_for_each_entry(clki, head, list) {
40 if (IS_ERR_OR_NULL(clki->clk))
41 continue;
42 if (!strcmp(clki->name, "core_clk"))
43 core_clk_rate = clk_get_rate(clki->clk);
44 }
45
46 if (!core_clk_rate) {
47 dev_err(hba->dev, "%s: unable to find core_clk rate\n",
48 __func__);
49 return -EINVAL;
50 }
51
52 core_clk_div = core_clk_rate / USEC_PER_SEC;
53
54 ufshcd_writel(hba, core_clk_div, CDNS_UFS_REG_HCLKDIV);
55 /**
56 * Make sure the register was updated,
57 * UniPro layer will not work with an incorrect value.
58 */
59 mb();
60
61 return 0;
62}
63
64/**
Anil Varughesecb8b3352019-08-02 12:21:12 +010065 * Called before and after HCE enable bit is set.
Janek Kotasd90996d2018-09-20 13:08:30 +000066 * @hba: host controller instance
Janek Kotasd90996d2018-09-20 13:08:30 +000067 * @status: notify stage (pre, post change)
68 *
69 * Return zero for success and non-zero for failure
70 */
Anil Varughesecb8b3352019-08-02 12:21:12 +010071static int cdns_ufs_hce_enable_notify(struct ufs_hba *hba,
72 enum ufs_notify_change_status status)
Janek Kotasd90996d2018-09-20 13:08:30 +000073{
Anil Varughesecb8b3352019-08-02 12:21:12 +010074 if (status != PRE_CHANGE)
Janek Kotasd90996d2018-09-20 13:08:30 +000075 return 0;
76
77 return cdns_ufs_set_hclkdiv(hba);
78}
79
Jan Kotas4fed62b2019-03-27 14:44:05 +000080/**
Anil Varughesee55f24f2019-08-13 08:42:50 +010081 * Called before and after Link startup is carried out.
82 * @hba: host controller instance
83 * @status: notify stage (pre, post change)
84 *
85 * Return zero for success and non-zero for failure
86 */
87static int cdns_ufs_link_startup_notify(struct ufs_hba *hba,
88 enum ufs_notify_change_status status)
89{
90 if (status != PRE_CHANGE)
91 return 0;
92
93 /*
94 * Some UFS devices have issues if LCC is enabled.
95 * So we are setting PA_Local_TX_LCC_Enable to 0
96 * before link startup which will make sure that both host
97 * and device TX LCC are disabled once link startup is
98 * completed.
99 */
100 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE), 0);
101
102 return 0;
103}
104
105/**
Jan Kotas4fed62b2019-03-27 14:44:05 +0000106 * cdns_ufs_init - performs additional ufs initialization
107 * @hba: host controller instance
108 *
109 * Returns status of initialization
110 */
111static int cdns_ufs_init(struct ufs_hba *hba)
112{
113 int status = 0;
114
115 if (hba->vops && hba->vops->phy_initialization)
116 status = hba->vops->phy_initialization(hba);
117
118 return status;
119}
120
121/**
122 * cdns_ufs_m31_16nm_phy_initialization - performs m31 phy initialization
123 * @hba: host controller instance
124 *
125 * Always returns 0
126 */
127static int cdns_ufs_m31_16nm_phy_initialization(struct ufs_hba *hba)
128{
129 u32 data;
130
131 /* Increase RX_Advanced_Min_ActivateTime_Capability */
132 data = ufshcd_readl(hba, CDNS_UFS_REG_PHY_XCFGD1);
133 data |= BIT(24);
134 ufshcd_writel(hba, data, CDNS_UFS_REG_PHY_XCFGD1);
135
136 return 0;
137}
138
139static const struct ufs_hba_variant_ops cdns_ufs_pltfm_hba_vops = {
Janek Kotasd90996d2018-09-20 13:08:30 +0000140 .name = "cdns-ufs-pltfm",
Anil Varughesecb8b3352019-08-02 12:21:12 +0100141 .hce_enable_notify = cdns_ufs_hce_enable_notify,
Anil Varughesee55f24f2019-08-13 08:42:50 +0100142 .link_startup_notify = cdns_ufs_link_startup_notify,
Janek Kotasd90996d2018-09-20 13:08:30 +0000143};
144
Jan Kotas4fed62b2019-03-27 14:44:05 +0000145static const struct ufs_hba_variant_ops cdns_ufs_m31_16nm_pltfm_hba_vops = {
146 .name = "cdns-ufs-pltfm",
147 .init = cdns_ufs_init,
Anil Varughesecb8b3352019-08-02 12:21:12 +0100148 .hce_enable_notify = cdns_ufs_hce_enable_notify,
Anil Varughesee55f24f2019-08-13 08:42:50 +0100149 .link_startup_notify = cdns_ufs_link_startup_notify,
Jan Kotas4fed62b2019-03-27 14:44:05 +0000150 .phy_initialization = cdns_ufs_m31_16nm_phy_initialization,
151};
152
153static const struct of_device_id cdns_ufs_of_match[] = {
154 {
155 .compatible = "cdns,ufshc",
156 .data = &cdns_ufs_pltfm_hba_vops,
157 },
158 {
159 .compatible = "cdns,ufshc-m31-16nm",
160 .data = &cdns_ufs_m31_16nm_pltfm_hba_vops,
161 },
162 { },
163};
164
165MODULE_DEVICE_TABLE(of, cdns_ufs_of_match);
166
Janek Kotasd90996d2018-09-20 13:08:30 +0000167/**
168 * cdns_ufs_pltfrm_probe - probe routine of the driver
169 * @pdev: pointer to platform device handle
170 *
171 * Return zero for success and non-zero for failure
172 */
173static int cdns_ufs_pltfrm_probe(struct platform_device *pdev)
174{
175 int err;
Jan Kotas4fed62b2019-03-27 14:44:05 +0000176 const struct of_device_id *of_id;
177 struct ufs_hba_variant_ops *vops;
Janek Kotasd90996d2018-09-20 13:08:30 +0000178 struct device *dev = &pdev->dev;
179
Jan Kotas4fed62b2019-03-27 14:44:05 +0000180 of_id = of_match_node(cdns_ufs_of_match, dev->of_node);
181 vops = (struct ufs_hba_variant_ops *)of_id->data;
182
Janek Kotasd90996d2018-09-20 13:08:30 +0000183 /* Perform generic probe */
Jan Kotas4fed62b2019-03-27 14:44:05 +0000184 err = ufshcd_pltfrm_init(pdev, vops);
Janek Kotasd90996d2018-09-20 13:08:30 +0000185 if (err)
186 dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
187
188 return err;
189}
190
191/**
192 * cdns_ufs_pltfrm_remove - removes the ufs driver
193 * @pdev: pointer to platform device handle
194 *
195 * Always returns 0
196 */
197static int cdns_ufs_pltfrm_remove(struct platform_device *pdev)
198{
199 struct ufs_hba *hba = platform_get_drvdata(pdev);
200
201 ufshcd_remove(hba);
202 return 0;
203}
204
Janek Kotasd90996d2018-09-20 13:08:30 +0000205static const struct dev_pm_ops cdns_ufs_dev_pm_ops = {
206 .suspend = ufshcd_pltfrm_suspend,
207 .resume = ufshcd_pltfrm_resume,
208 .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
209 .runtime_resume = ufshcd_pltfrm_runtime_resume,
210 .runtime_idle = ufshcd_pltfrm_runtime_idle,
211};
212
213static struct platform_driver cdns_ufs_pltfrm_driver = {
214 .probe = cdns_ufs_pltfrm_probe,
215 .remove = cdns_ufs_pltfrm_remove,
216 .driver = {
217 .name = "cdns-ufshcd",
Janek Kotasd90996d2018-09-20 13:08:30 +0000218 .pm = &cdns_ufs_dev_pm_ops,
219 .of_match_table = cdns_ufs_of_match,
220 },
221};
222
223module_platform_driver(cdns_ufs_pltfrm_driver);
224
225MODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
226MODULE_DESCRIPTION("Cadence UFS host controller platform driver");
227MODULE_LICENSE("GPL v2");
228MODULE_VERSION(UFSHCD_DRIVER_VERSION);