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