• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Universal Flash Storage Host controller Platform bus based glue driver
4  * Copyright (C) 2011-2013 Samsung India Software Operations
5  *
6  * Authors:
7  *	Santosh Yaraganavi <santosh.sy@samsung.com>
8  *	Vinayak Holikatti <h.vinayak@samsung.com>
9  */
10 
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/of.h>
14 
15 #include "ufshcd.h"
16 #include "ufshcd-pltfrm.h"
17 #include "unipro.h"
18 
19 #define UFSHCD_DEFAULT_LANES_PER_DIRECTION		2
20 
ufshcd_parse_clock_info(struct ufs_hba * hba)21 static int ufshcd_parse_clock_info(struct ufs_hba *hba)
22 {
23 	int ret = 0;
24 	int cnt;
25 	int i;
26 	struct device *dev = hba->dev;
27 	struct device_node *np = dev->of_node;
28 	char *name;
29 	u32 *clkfreq = NULL;
30 	struct ufs_clk_info *clki;
31 	int len = 0;
32 	size_t sz = 0;
33 
34 	if (!np)
35 		goto out;
36 
37 	cnt = of_property_count_strings(np, "clock-names");
38 	if (!cnt || (cnt == -EINVAL)) {
39 		dev_info(dev, "%s: Unable to find clocks, assuming enabled\n",
40 				__func__);
41 	} else if (cnt < 0) {
42 		dev_err(dev, "%s: count clock strings failed, err %d\n",
43 				__func__, cnt);
44 		ret = cnt;
45 	}
46 
47 	if (cnt <= 0)
48 		goto out;
49 
50 	if (!of_get_property(np, "freq-table-hz", &len)) {
51 		dev_info(dev, "freq-table-hz property not specified\n");
52 		goto out;
53 	}
54 
55 	if (len <= 0)
56 		goto out;
57 
58 	sz = len / sizeof(*clkfreq);
59 	if (sz != 2 * cnt) {
60 		dev_err(dev, "%s len mismatch\n", "freq-table-hz");
61 		ret = -EINVAL;
62 		goto out;
63 	}
64 
65 	clkfreq = devm_kcalloc(dev, sz, sizeof(*clkfreq),
66 			       GFP_KERNEL);
67 	if (!clkfreq) {
68 		ret = -ENOMEM;
69 		goto out;
70 	}
71 
72 	ret = of_property_read_u32_array(np, "freq-table-hz",
73 			clkfreq, sz);
74 	if (ret && (ret != -EINVAL)) {
75 		dev_err(dev, "%s: error reading array %d\n",
76 				"freq-table-hz", ret);
77 		return ret;
78 	}
79 
80 	for (i = 0; i < sz; i += 2) {
81 		ret = of_property_read_string_index(np,
82 				"clock-names", i/2, (const char **)&name);
83 		if (ret)
84 			goto out;
85 
86 		clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
87 		if (!clki) {
88 			ret = -ENOMEM;
89 			goto out;
90 		}
91 
92 		clki->min_freq = clkfreq[i];
93 		clki->max_freq = clkfreq[i+1];
94 		clki->name = devm_kstrdup(dev, name, GFP_KERNEL);
95 		if (!strcmp(name, "ref_clk"))
96 			clki->keep_link_active = true;
97 		dev_dbg(dev, "%s: min %u max %u name %s\n", "freq-table-hz",
98 				clki->min_freq, clki->max_freq, clki->name);
99 		list_add_tail(&clki->list, &hba->clk_list_head);
100 	}
101 out:
102 	return ret;
103 }
104 
105 #define MAX_PROP_SIZE 32
ufshcd_populate_vreg(struct device * dev,const char * name,struct ufs_vreg ** out_vreg)106 static int ufshcd_populate_vreg(struct device *dev, const char *name,
107 		struct ufs_vreg **out_vreg)
108 {
109 	int ret = 0;
110 	char prop_name[MAX_PROP_SIZE];
111 	struct ufs_vreg *vreg = NULL;
112 	struct device_node *np = dev->of_node;
113 
114 	if (!np) {
115 		dev_err(dev, "%s: non DT initialization\n", __func__);
116 		goto out;
117 	}
118 
119 	snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", name);
120 	if (!of_parse_phandle(np, prop_name, 0)) {
121 		dev_info(dev, "%s: Unable to find %s regulator, assuming enabled\n",
122 				__func__, prop_name);
123 		goto out;
124 	}
125 
126 	vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
127 	if (!vreg)
128 		return -ENOMEM;
129 
130 	vreg->name = devm_kstrdup(dev, name, GFP_KERNEL);
131 
132 	snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
133 	if (of_property_read_u32(np, prop_name, &vreg->max_uA)) {
134 		dev_info(dev, "%s: unable to find %s\n", __func__, prop_name);
135 		vreg->max_uA = 0;
136 	}
137 
138 	if (!strcmp(name, "vcc")) {
139 		if (of_property_read_bool(np, "vcc-supply-1p8")) {
140 			vreg->min_uV = UFS_VREG_VCC_1P8_MIN_UV;
141 			vreg->max_uV = UFS_VREG_VCC_1P8_MAX_UV;
142 		} else {
143 			vreg->min_uV = UFS_VREG_VCC_MIN_UV;
144 			vreg->max_uV = UFS_VREG_VCC_MAX_UV;
145 		}
146 	} else if (!strcmp(name, "vccq")) {
147 		vreg->min_uV = UFS_VREG_VCCQ_MIN_UV;
148 		vreg->max_uV = UFS_VREG_VCCQ_MAX_UV;
149 	} else if (!strcmp(name, "vccq2")) {
150 		vreg->min_uV = UFS_VREG_VCCQ2_MIN_UV;
151 		vreg->max_uV = UFS_VREG_VCCQ2_MAX_UV;
152 	}
153 
154 	goto out;
155 
156 out:
157 	if (!ret)
158 		*out_vreg = vreg;
159 	return ret;
160 }
161 
162 /**
163  * ufshcd_parse_regulator_info - get regulator info from device tree
164  * @hba: per adapter instance
165  *
166  * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
167  * If any of the supplies are not defined it is assumed that they are always-on
168  * and hence return zero. If the property is defined but parsing is failed
169  * then return corresponding error.
170  */
ufshcd_parse_regulator_info(struct ufs_hba * hba)171 static int ufshcd_parse_regulator_info(struct ufs_hba *hba)
172 {
173 	int err;
174 	struct device *dev = hba->dev;
175 	struct ufs_vreg_info *info = &hba->vreg_info;
176 
177 	err = ufshcd_populate_vreg(dev, "vdd-hba", &info->vdd_hba);
178 	if (err)
179 		goto out;
180 
181 	err = ufshcd_populate_vreg(dev, "vcc", &info->vcc);
182 	if (err)
183 		goto out;
184 
185 	err = ufshcd_populate_vreg(dev, "vccq", &info->vccq);
186 	if (err)
187 		goto out;
188 
189 	err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2);
190 out:
191 	return err;
192 }
193 
194 #ifdef CONFIG_PM
195 /**
196  * ufshcd_pltfrm_suspend - suspend power management function
197  * @dev: pointer to device handle
198  *
199  * Returns 0 if successful
200  * Returns non-zero otherwise
201  */
ufshcd_pltfrm_suspend(struct device * dev)202 int ufshcd_pltfrm_suspend(struct device *dev)
203 {
204 	return ufshcd_system_suspend(dev_get_drvdata(dev));
205 }
206 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_suspend);
207 
208 /**
209  * ufshcd_pltfrm_resume - resume power management function
210  * @dev: pointer to device handle
211  *
212  * Returns 0 if successful
213  * Returns non-zero otherwise
214  */
ufshcd_pltfrm_resume(struct device * dev)215 int ufshcd_pltfrm_resume(struct device *dev)
216 {
217 	return ufshcd_system_resume(dev_get_drvdata(dev));
218 }
219 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_resume);
220 
ufshcd_pltfrm_runtime_suspend(struct device * dev)221 int ufshcd_pltfrm_runtime_suspend(struct device *dev)
222 {
223 	return ufshcd_runtime_suspend(dev_get_drvdata(dev));
224 }
225 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_suspend);
226 
ufshcd_pltfrm_runtime_resume(struct device * dev)227 int ufshcd_pltfrm_runtime_resume(struct device *dev)
228 {
229 	return ufshcd_runtime_resume(dev_get_drvdata(dev));
230 }
231 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_resume);
232 
ufshcd_pltfrm_runtime_idle(struct device * dev)233 int ufshcd_pltfrm_runtime_idle(struct device *dev)
234 {
235 	return ufshcd_runtime_idle(dev_get_drvdata(dev));
236 }
237 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_idle);
238 
239 #endif /* CONFIG_PM */
240 
ufshcd_pltfrm_shutdown(struct platform_device * pdev)241 void ufshcd_pltfrm_shutdown(struct platform_device *pdev)
242 {
243 	ufshcd_shutdown((struct ufs_hba *)platform_get_drvdata(pdev));
244 }
245 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_shutdown);
246 
ufshcd_init_lanes_per_dir(struct ufs_hba * hba)247 static void ufshcd_init_lanes_per_dir(struct ufs_hba *hba)
248 {
249 	struct device *dev = hba->dev;
250 	int ret;
251 
252 	ret = of_property_read_u32(dev->of_node, "lanes-per-direction",
253 		&hba->lanes_per_direction);
254 	if (ret) {
255 		dev_dbg(hba->dev,
256 			"%s: failed to read lanes-per-direction, ret=%d\n",
257 			__func__, ret);
258 		hba->lanes_per_direction = UFSHCD_DEFAULT_LANES_PER_DIRECTION;
259 	}
260 }
261 
262 /**
263  * ufshcd_get_pwr_dev_param - get finally agreed attributes for
264  *                            power mode change
265  * @pltfrm_param: pointer to platform parameters
266  * @dev_max: pointer to device attributes
267  * @agreed_pwr: returned agreed attributes
268  *
269  * Returns 0 on success, non-zero value on failure
270  */
ufshcd_get_pwr_dev_param(struct ufs_dev_params * pltfrm_param,struct ufs_pa_layer_attr * dev_max,struct ufs_pa_layer_attr * agreed_pwr)271 int ufshcd_get_pwr_dev_param(struct ufs_dev_params *pltfrm_param,
272 			     struct ufs_pa_layer_attr *dev_max,
273 			     struct ufs_pa_layer_attr *agreed_pwr)
274 {
275 	int min_pltfrm_gear;
276 	int min_dev_gear;
277 	bool is_dev_sup_hs = false;
278 	bool is_pltfrm_max_hs = false;
279 
280 	if (dev_max->pwr_rx == FAST_MODE)
281 		is_dev_sup_hs = true;
282 
283 	if (pltfrm_param->desired_working_mode == UFS_HS_MODE) {
284 		is_pltfrm_max_hs = true;
285 		min_pltfrm_gear = min_t(u32, pltfrm_param->hs_rx_gear,
286 					pltfrm_param->hs_tx_gear);
287 	} else {
288 		min_pltfrm_gear = min_t(u32, pltfrm_param->pwm_rx_gear,
289 					pltfrm_param->pwm_tx_gear);
290 	}
291 
292 	/*
293 	 * device doesn't support HS but
294 	 * pltfrm_param->desired_working_mode is HS,
295 	 * thus device and pltfrm_param don't agree
296 	 */
297 	if (!is_dev_sup_hs && is_pltfrm_max_hs) {
298 		pr_info("%s: device doesn't support HS\n",
299 			__func__);
300 		return -ENOTSUPP;
301 	} else if (is_dev_sup_hs && is_pltfrm_max_hs) {
302 		/*
303 		 * since device supports HS, it supports FAST_MODE.
304 		 * since pltfrm_param->desired_working_mode is also HS
305 		 * then final decision (FAST/FASTAUTO) is done according
306 		 * to pltfrm_params as it is the restricting factor
307 		 */
308 		agreed_pwr->pwr_rx = pltfrm_param->rx_pwr_hs;
309 		agreed_pwr->pwr_tx = agreed_pwr->pwr_rx;
310 	} else {
311 		/*
312 		 * here pltfrm_param->desired_working_mode is PWM.
313 		 * it doesn't matter whether device supports HS or PWM,
314 		 * in both cases pltfrm_param->desired_working_mode will
315 		 * determine the mode
316 		 */
317 		agreed_pwr->pwr_rx = pltfrm_param->rx_pwr_pwm;
318 		agreed_pwr->pwr_tx = agreed_pwr->pwr_rx;
319 	}
320 
321 	/*
322 	 * we would like tx to work in the minimum number of lanes
323 	 * between device capability and vendor preferences.
324 	 * the same decision will be made for rx
325 	 */
326 	agreed_pwr->lane_tx = min_t(u32, dev_max->lane_tx,
327 				    pltfrm_param->tx_lanes);
328 	agreed_pwr->lane_rx = min_t(u32, dev_max->lane_rx,
329 				    pltfrm_param->rx_lanes);
330 
331 	/* device maximum gear is the minimum between device rx and tx gears */
332 	min_dev_gear = min_t(u32, dev_max->gear_rx, dev_max->gear_tx);
333 
334 	/*
335 	 * if both device capabilities and vendor pre-defined preferences are
336 	 * both HS or both PWM then set the minimum gear to be the chosen
337 	 * working gear.
338 	 * if one is PWM and one is HS then the one that is PWM get to decide
339 	 * what is the gear, as it is the one that also decided previously what
340 	 * pwr the device will be configured to.
341 	 */
342 	if ((is_dev_sup_hs && is_pltfrm_max_hs) ||
343 	    (!is_dev_sup_hs && !is_pltfrm_max_hs)) {
344 		agreed_pwr->gear_rx =
345 			min_t(u32, min_dev_gear, min_pltfrm_gear);
346 	} else if (!is_dev_sup_hs) {
347 		agreed_pwr->gear_rx = min_dev_gear;
348 	} else {
349 		agreed_pwr->gear_rx = min_pltfrm_gear;
350 	}
351 	agreed_pwr->gear_tx = agreed_pwr->gear_rx;
352 
353 	agreed_pwr->hs_rate = pltfrm_param->hs_rate;
354 
355 	return 0;
356 }
357 EXPORT_SYMBOL_GPL(ufshcd_get_pwr_dev_param);
358 
359 /**
360  * ufshcd_pltfrm_init - probe routine of the driver
361  * @pdev: pointer to Platform device handle
362  * @vops: pointer to variant ops
363  *
364  * Returns 0 on success, non-zero value on failure
365  */
ufshcd_pltfrm_init(struct platform_device * pdev,const struct ufs_hba_variant_ops * vops)366 int ufshcd_pltfrm_init(struct platform_device *pdev,
367 		       const struct ufs_hba_variant_ops *vops)
368 {
369 	struct ufs_hba *hba;
370 	void __iomem *mmio_base;
371 	int irq, err;
372 	struct device *dev = &pdev->dev;
373 
374 	mmio_base = devm_platform_ioremap_resource(pdev, 0);
375 	if (IS_ERR(mmio_base)) {
376 		err = PTR_ERR(mmio_base);
377 		goto out;
378 	}
379 
380 	irq = platform_get_irq(pdev, 0);
381 	if (irq < 0) {
382 		err = irq;
383 		goto out;
384 	}
385 
386 	err = ufshcd_alloc_host(dev, &hba);
387 	if (err) {
388 		dev_err(&pdev->dev, "Allocation failed\n");
389 		goto out;
390 	}
391 
392 	hba->vops = vops;
393 
394 	err = ufshcd_parse_clock_info(hba);
395 	if (err) {
396 		dev_err(&pdev->dev, "%s: clock parse failed %d\n",
397 				__func__, err);
398 		goto dealloc_host;
399 	}
400 	err = ufshcd_parse_regulator_info(hba);
401 	if (err) {
402 		dev_err(&pdev->dev, "%s: regulator init failed %d\n",
403 				__func__, err);
404 		goto dealloc_host;
405 	}
406 
407 	ufshcd_init_lanes_per_dir(hba);
408 
409 	err = ufshcd_init(hba, mmio_base, irq);
410 	if (err) {
411 		dev_err(dev, "Initialization failed\n");
412 		goto dealloc_host;
413 	}
414 
415 	pm_runtime_set_active(&pdev->dev);
416 	pm_runtime_enable(&pdev->dev);
417 
418 	return 0;
419 
420 dealloc_host:
421 	ufshcd_dealloc_host(hba);
422 out:
423 	return err;
424 }
425 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_init);
426 
427 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
428 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
429 MODULE_DESCRIPTION("UFS host controller Platform bus based glue driver");
430 MODULE_LICENSE("GPL");
431 MODULE_VERSION(UFSHCD_DRIVER_VERSION);
432