Merge tag 'fixes-v5.11' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner...
[linux-2.6-microblaze.git] / drivers / scsi / ufs / cdns-pltfrm.c
1 // 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
20 #define CDNS_UFS_REG_HCLKDIV    0xFC
21 #define CDNS_UFS_REG_PHY_XCFGD1 0x113C
22 #define CDNS_UFS_MAX_L4_ATTRS 12
23
24 struct cdns_ufs_host {
25         /**
26          * cdns_ufs_dme_attr_val - for storing L4 attributes
27          */
28         u32 cdns_ufs_dme_attr_val[CDNS_UFS_MAX_L4_ATTRS];
29 };
30
31 /**
32  * cdns_ufs_get_l4_attr - get L4 attributes on local side
33  * @hba: per adapter instance
34  *
35  */
36 static void cdns_ufs_get_l4_attr(struct ufs_hba *hba)
37 {
38         struct cdns_ufs_host *host = ufshcd_get_variant(hba);
39
40         ufshcd_dme_get(hba, UIC_ARG_MIB(T_PEERDEVICEID),
41                        &host->cdns_ufs_dme_attr_val[0]);
42         ufshcd_dme_get(hba, UIC_ARG_MIB(T_PEERCPORTID),
43                        &host->cdns_ufs_dme_attr_val[1]);
44         ufshcd_dme_get(hba, UIC_ARG_MIB(T_TRAFFICCLASS),
45                        &host->cdns_ufs_dme_attr_val[2]);
46         ufshcd_dme_get(hba, UIC_ARG_MIB(T_PROTOCOLID),
47                        &host->cdns_ufs_dme_attr_val[3]);
48         ufshcd_dme_get(hba, UIC_ARG_MIB(T_CPORTFLAGS),
49                        &host->cdns_ufs_dme_attr_val[4]);
50         ufshcd_dme_get(hba, UIC_ARG_MIB(T_TXTOKENVALUE),
51                        &host->cdns_ufs_dme_attr_val[5]);
52         ufshcd_dme_get(hba, UIC_ARG_MIB(T_RXTOKENVALUE),
53                        &host->cdns_ufs_dme_attr_val[6]);
54         ufshcd_dme_get(hba, UIC_ARG_MIB(T_LOCALBUFFERSPACE),
55                        &host->cdns_ufs_dme_attr_val[7]);
56         ufshcd_dme_get(hba, UIC_ARG_MIB(T_PEERBUFFERSPACE),
57                        &host->cdns_ufs_dme_attr_val[8]);
58         ufshcd_dme_get(hba, UIC_ARG_MIB(T_CREDITSTOSEND),
59                        &host->cdns_ufs_dme_attr_val[9]);
60         ufshcd_dme_get(hba, UIC_ARG_MIB(T_CPORTMODE),
61                        &host->cdns_ufs_dme_attr_val[10]);
62         ufshcd_dme_get(hba, UIC_ARG_MIB(T_CONNECTIONSTATE),
63                        &host->cdns_ufs_dme_attr_val[11]);
64 }
65
66 /**
67  * cdns_ufs_set_l4_attr - set L4 attributes on local side
68  * @hba: per adapter instance
69  *
70  */
71 static void cdns_ufs_set_l4_attr(struct ufs_hba *hba)
72 {
73         struct cdns_ufs_host *host = ufshcd_get_variant(hba);
74
75         ufshcd_dme_set(hba, UIC_ARG_MIB(T_CONNECTIONSTATE), 0);
76         ufshcd_dme_set(hba, UIC_ARG_MIB(T_PEERDEVICEID),
77                        host->cdns_ufs_dme_attr_val[0]);
78         ufshcd_dme_set(hba, UIC_ARG_MIB(T_PEERCPORTID),
79                        host->cdns_ufs_dme_attr_val[1]);
80         ufshcd_dme_set(hba, UIC_ARG_MIB(T_TRAFFICCLASS),
81                        host->cdns_ufs_dme_attr_val[2]);
82         ufshcd_dme_set(hba, UIC_ARG_MIB(T_PROTOCOLID),
83                        host->cdns_ufs_dme_attr_val[3]);
84         ufshcd_dme_set(hba, UIC_ARG_MIB(T_CPORTFLAGS),
85                        host->cdns_ufs_dme_attr_val[4]);
86         ufshcd_dme_set(hba, UIC_ARG_MIB(T_TXTOKENVALUE),
87                        host->cdns_ufs_dme_attr_val[5]);
88         ufshcd_dme_set(hba, UIC_ARG_MIB(T_RXTOKENVALUE),
89                        host->cdns_ufs_dme_attr_val[6]);
90         ufshcd_dme_set(hba, UIC_ARG_MIB(T_LOCALBUFFERSPACE),
91                        host->cdns_ufs_dme_attr_val[7]);
92         ufshcd_dme_set(hba, UIC_ARG_MIB(T_PEERBUFFERSPACE),
93                        host->cdns_ufs_dme_attr_val[8]);
94         ufshcd_dme_set(hba, UIC_ARG_MIB(T_CREDITSTOSEND),
95                        host->cdns_ufs_dme_attr_val[9]);
96         ufshcd_dme_set(hba, UIC_ARG_MIB(T_CPORTMODE),
97                        host->cdns_ufs_dme_attr_val[10]);
98         ufshcd_dme_set(hba, UIC_ARG_MIB(T_CONNECTIONSTATE),
99                        host->cdns_ufs_dme_attr_val[11]);
100 }
101
102 /**
103  * Sets HCLKDIV register value based on the core_clk
104  * @hba: host controller instance
105  *
106  * Return zero for success and non-zero for failure
107  */
108 static int cdns_ufs_set_hclkdiv(struct ufs_hba *hba)
109 {
110         struct ufs_clk_info *clki;
111         struct list_head *head = &hba->clk_list_head;
112         unsigned long core_clk_rate = 0;
113         u32 core_clk_div = 0;
114
115         if (list_empty(head))
116                 return 0;
117
118         list_for_each_entry(clki, head, list) {
119                 if (IS_ERR_OR_NULL(clki->clk))
120                         continue;
121                 if (!strcmp(clki->name, "core_clk"))
122                         core_clk_rate = clk_get_rate(clki->clk);
123         }
124
125         if (!core_clk_rate) {
126                 dev_err(hba->dev, "%s: unable to find core_clk rate\n",
127                         __func__);
128                 return -EINVAL;
129         }
130
131         core_clk_div = core_clk_rate / USEC_PER_SEC;
132
133         ufshcd_writel(hba, core_clk_div, CDNS_UFS_REG_HCLKDIV);
134         /**
135          * Make sure the register was updated,
136          * UniPro layer will not work with an incorrect value.
137          */
138         mb();
139
140         return 0;
141 }
142
143 /**
144  * Called before and after HCE enable bit is set.
145  * @hba: host controller instance
146  * @status: notify stage (pre, post change)
147  *
148  * Return zero for success and non-zero for failure
149  */
150 static int cdns_ufs_hce_enable_notify(struct ufs_hba *hba,
151                                       enum ufs_notify_change_status status)
152 {
153         if (status != PRE_CHANGE)
154                 return 0;
155
156         return cdns_ufs_set_hclkdiv(hba);
157 }
158
159 /**
160  * Called around hibern8 enter/exit.
161  * @hba: host controller instance
162  * @cmd: UIC Command
163  * @status: notify stage (pre, post change)
164  *
165  */
166 static void cdns_ufs_hibern8_notify(struct ufs_hba *hba, enum uic_cmd_dme cmd,
167                                     enum ufs_notify_change_status status)
168 {
169         if (status == PRE_CHANGE && cmd == UIC_CMD_DME_HIBER_ENTER)
170                 cdns_ufs_get_l4_attr(hba);
171         if (status == POST_CHANGE && cmd == UIC_CMD_DME_HIBER_EXIT)
172                 cdns_ufs_set_l4_attr(hba);
173 }
174
175 /**
176  * Called before and after Link startup is carried out.
177  * @hba: host controller instance
178  * @status: notify stage (pre, post change)
179  *
180  * Return zero for success and non-zero for failure
181  */
182 static int cdns_ufs_link_startup_notify(struct ufs_hba *hba,
183                                         enum ufs_notify_change_status status)
184 {
185         if (status != PRE_CHANGE)
186                 return 0;
187
188         /*
189          * Some UFS devices have issues if LCC is enabled.
190          * So we are setting PA_Local_TX_LCC_Enable to 0
191          * before link startup which will make sure that both host
192          * and device TX LCC are disabled once link startup is
193          * completed.
194          */
195         ufshcd_disable_host_tx_lcc(hba);
196
197         /*
198          * Disabling Autohibern8 feature in cadence UFS
199          * to mask unexpected interrupt trigger.
200          */
201         hba->ahit = 0;
202
203         return 0;
204 }
205
206 /**
207  * cdns_ufs_init - performs additional ufs initialization
208  * @hba: host controller instance
209  *
210  * Returns status of initialization
211  */
212 static int cdns_ufs_init(struct ufs_hba *hba)
213 {
214         int status = 0;
215         struct cdns_ufs_host *host;
216         struct device *dev = hba->dev;
217
218         host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
219
220         if (!host)
221                 return -ENOMEM;
222         ufshcd_set_variant(hba, host);
223
224         if (hba->vops && hba->vops->phy_initialization)
225                 status = hba->vops->phy_initialization(hba);
226
227         return status;
228 }
229
230 /**
231  * cdns_ufs_m31_16nm_phy_initialization - performs m31 phy initialization
232  * @hba: host controller instance
233  *
234  * Always returns 0
235  */
236 static int cdns_ufs_m31_16nm_phy_initialization(struct ufs_hba *hba)
237 {
238         u32 data;
239
240         /* Increase RX_Advanced_Min_ActivateTime_Capability */
241         data = ufshcd_readl(hba, CDNS_UFS_REG_PHY_XCFGD1);
242         data |= BIT(24);
243         ufshcd_writel(hba, data, CDNS_UFS_REG_PHY_XCFGD1);
244
245         return 0;
246 }
247
248 static const struct ufs_hba_variant_ops cdns_ufs_pltfm_hba_vops = {
249         .name = "cdns-ufs-pltfm",
250         .init = cdns_ufs_init,
251         .hce_enable_notify = cdns_ufs_hce_enable_notify,
252         .link_startup_notify = cdns_ufs_link_startup_notify,
253         .hibern8_notify = cdns_ufs_hibern8_notify,
254 };
255
256 static const struct ufs_hba_variant_ops cdns_ufs_m31_16nm_pltfm_hba_vops = {
257         .name = "cdns-ufs-pltfm",
258         .init = cdns_ufs_init,
259         .hce_enable_notify = cdns_ufs_hce_enable_notify,
260         .link_startup_notify = cdns_ufs_link_startup_notify,
261         .phy_initialization = cdns_ufs_m31_16nm_phy_initialization,
262         .hibern8_notify = cdns_ufs_hibern8_notify,
263 };
264
265 static const struct of_device_id cdns_ufs_of_match[] = {
266         {
267                 .compatible = "cdns,ufshc",
268                 .data =  &cdns_ufs_pltfm_hba_vops,
269         },
270         {
271                 .compatible = "cdns,ufshc-m31-16nm",
272                 .data =  &cdns_ufs_m31_16nm_pltfm_hba_vops,
273         },
274         { },
275 };
276
277 MODULE_DEVICE_TABLE(of, cdns_ufs_of_match);
278
279 /**
280  * cdns_ufs_pltfrm_probe - probe routine of the driver
281  * @pdev: pointer to platform device handle
282  *
283  * Return zero for success and non-zero for failure
284  */
285 static int cdns_ufs_pltfrm_probe(struct platform_device *pdev)
286 {
287         int err;
288         const struct of_device_id *of_id;
289         struct ufs_hba_variant_ops *vops;
290         struct device *dev = &pdev->dev;
291
292         of_id = of_match_node(cdns_ufs_of_match, dev->of_node);
293         vops = (struct ufs_hba_variant_ops *)of_id->data;
294
295         /* Perform generic probe */
296         err = ufshcd_pltfrm_init(pdev, vops);
297         if (err)
298                 dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
299
300         return err;
301 }
302
303 /**
304  * cdns_ufs_pltfrm_remove - removes the ufs driver
305  * @pdev: pointer to platform device handle
306  *
307  * Always returns 0
308  */
309 static int cdns_ufs_pltfrm_remove(struct platform_device *pdev)
310 {
311         struct ufs_hba *hba =  platform_get_drvdata(pdev);
312
313         ufshcd_remove(hba);
314         return 0;
315 }
316
317 static const struct dev_pm_ops cdns_ufs_dev_pm_ops = {
318         .suspend         = ufshcd_pltfrm_suspend,
319         .resume          = ufshcd_pltfrm_resume,
320         .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
321         .runtime_resume  = ufshcd_pltfrm_runtime_resume,
322         .runtime_idle    = ufshcd_pltfrm_runtime_idle,
323 };
324
325 static struct platform_driver cdns_ufs_pltfrm_driver = {
326         .probe  = cdns_ufs_pltfrm_probe,
327         .remove = cdns_ufs_pltfrm_remove,
328         .shutdown = ufshcd_pltfrm_shutdown,
329         .driver = {
330                 .name   = "cdns-ufshcd",
331                 .pm     = &cdns_ufs_dev_pm_ops,
332                 .of_match_table = cdns_ufs_of_match,
333         },
334 };
335
336 module_platform_driver(cdns_ufs_pltfrm_driver);
337
338 MODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
339 MODULE_DESCRIPTION("Cadence UFS host controller platform driver");
340 MODULE_LICENSE("GPL v2");
341 MODULE_VERSION(UFSHCD_DRIVER_VERSION);