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