blob: a72a4ba78125b09a135c781533d46b1cbf0bc042 [file] [log] [blame]
Vinayak Holikatti03b17812013-02-26 18:04:45 +05301/*
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
Vinayak Holikatti03b17812013-02-26 18:04:45 +053036#include <linux/platform_device.h>
Sujit Reddy Thumma62694732013-07-30 00:36:00 +053037#include <linux/pm_runtime.h>
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +030038#include <linux/of.h>
Vinayak Holikatti03b17812013-02-26 18:04:45 +053039
Seungwon Jeon2953f852013-06-27 13:31:54 +090040#include "ufshcd.h"
Yaniv Gardi47555a52015-10-28 13:15:49 +020041#include "ufshcd-pltfrm.h"
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +030042
Yaniv Gardi54b879b2016-03-10 17:37:05 +020043#define UFSHCD_DEFAULT_LANES_PER_DIRECTION 2
44
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +030045static int ufshcd_parse_clock_info(struct ufs_hba *hba)
46{
47 int ret = 0;
48 int cnt;
49 int i;
50 struct device *dev = hba->dev;
51 struct device_node *np = dev->of_node;
52 char *name;
53 u32 *clkfreq = NULL;
54 struct ufs_clk_info *clki;
Sahitya Tummala4cff6d992014-09-25 15:32:33 +030055 int len = 0;
56 size_t sz = 0;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +030057
58 if (!np)
59 goto out;
60
61 INIT_LIST_HEAD(&hba->clk_list_head);
62
63 cnt = of_property_count_strings(np, "clock-names");
64 if (!cnt || (cnt == -EINVAL)) {
65 dev_info(dev, "%s: Unable to find clocks, assuming enabled\n",
66 __func__);
67 } else if (cnt < 0) {
68 dev_err(dev, "%s: count clock strings failed, err %d\n",
69 __func__, cnt);
70 ret = cnt;
71 }
72
73 if (cnt <= 0)
74 goto out;
75
Sahitya Tummala4cff6d992014-09-25 15:32:33 +030076 if (!of_get_property(np, "freq-table-hz", &len)) {
77 dev_info(dev, "freq-table-hz property not specified\n");
78 goto out;
79 }
80
81 if (len <= 0)
82 goto out;
83
84 sz = len / sizeof(*clkfreq);
85 if (sz != 2 * cnt) {
86 dev_err(dev, "%s len mismatch\n", "freq-table-hz");
87 ret = -EINVAL;
88 goto out;
89 }
90
91 clkfreq = devm_kzalloc(dev, sz * sizeof(*clkfreq),
92 GFP_KERNEL);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +030093 if (!clkfreq) {
94 ret = -ENOMEM;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +030095 goto out;
96 }
97
Sahitya Tummala4cff6d992014-09-25 15:32:33 +030098 ret = of_property_read_u32_array(np, "freq-table-hz",
99 clkfreq, sz);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300100 if (ret && (ret != -EINVAL)) {
Sahitya Tummala4cff6d992014-09-25 15:32:33 +0300101 dev_err(dev, "%s: error reading array %d\n",
102 "freq-table-hz", ret);
Dolev Ravive8cb64d2014-10-23 13:25:17 +0300103 return ret;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300104 }
105
Sahitya Tummala4cff6d992014-09-25 15:32:33 +0300106 for (i = 0; i < sz; i += 2) {
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300107 ret = of_property_read_string_index(np,
Sahitya Tummala4cff6d992014-09-25 15:32:33 +0300108 "clock-names", i/2, (const char **)&name);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300109 if (ret)
Dolev Ravive8cb64d2014-10-23 13:25:17 +0300110 goto out;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300111
112 clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
113 if (!clki) {
114 ret = -ENOMEM;
Dolev Ravive8cb64d2014-10-23 13:25:17 +0300115 goto out;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300116 }
117
Sahitya Tummala4cff6d992014-09-25 15:32:33 +0300118 clki->min_freq = clkfreq[i];
119 clki->max_freq = clkfreq[i+1];
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300120 clki->name = kstrdup(name, GFP_KERNEL);
Sahitya Tummala4cff6d992014-09-25 15:32:33 +0300121 dev_dbg(dev, "%s: min %u max %u name %s\n", "freq-table-hz",
122 clki->min_freq, clki->max_freq, clki->name);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300123 list_add_tail(&clki->list, &hba->clk_list_head);
124 }
Sahitya Tummala4cff6d992014-09-25 15:32:33 +0300125out:
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300126 return ret;
127}
128
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300129#define MAX_PROP_SIZE 32
130static int ufshcd_populate_vreg(struct device *dev, const char *name,
131 struct ufs_vreg **out_vreg)
132{
133 int ret = 0;
134 char prop_name[MAX_PROP_SIZE];
135 struct ufs_vreg *vreg = NULL;
136 struct device_node *np = dev->of_node;
137
138 if (!np) {
139 dev_err(dev, "%s: non DT initialization\n", __func__);
140 goto out;
141 }
142
143 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", name);
144 if (!of_parse_phandle(np, prop_name, 0)) {
145 dev_info(dev, "%s: Unable to find %s regulator, assuming enabled\n",
146 __func__, prop_name);
147 goto out;
148 }
149
150 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
Dolev Raviv758581b2014-10-23 13:25:15 +0300151 if (!vreg)
152 return -ENOMEM;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300153
154 vreg->name = kstrdup(name, GFP_KERNEL);
155
Raviv Shvili6a771a62014-09-25 15:32:24 +0300156 /* if fixed regulator no need further initialization */
157 snprintf(prop_name, MAX_PROP_SIZE, "%s-fixed-regulator", name);
158 if (of_property_read_bool(np, prop_name))
159 goto out;
160
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300161 snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
162 ret = of_property_read_u32(np, prop_name, &vreg->max_uA);
163 if (ret) {
164 dev_err(dev, "%s: unable to find %s err %d\n",
165 __func__, prop_name, ret);
Subhash Jadavaniafa3dfd2016-10-27 17:25:58 -0700166 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300167 }
168
169 vreg->min_uA = 0;
170 if (!strcmp(name, "vcc")) {
171 if (of_property_read_bool(np, "vcc-supply-1p8")) {
172 vreg->min_uV = UFS_VREG_VCC_1P8_MIN_UV;
173 vreg->max_uV = UFS_VREG_VCC_1P8_MAX_UV;
174 } else {
175 vreg->min_uV = UFS_VREG_VCC_MIN_UV;
176 vreg->max_uV = UFS_VREG_VCC_MAX_UV;
177 }
178 } else if (!strcmp(name, "vccq")) {
179 vreg->min_uV = UFS_VREG_VCCQ_MIN_UV;
180 vreg->max_uV = UFS_VREG_VCCQ_MAX_UV;
181 } else if (!strcmp(name, "vccq2")) {
182 vreg->min_uV = UFS_VREG_VCCQ2_MIN_UV;
183 vreg->max_uV = UFS_VREG_VCCQ2_MAX_UV;
184 }
185
186 goto out;
187
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300188out:
189 if (!ret)
190 *out_vreg = vreg;
191 return ret;
192}
193
194/**
195 * ufshcd_parse_regulator_info - get regulator info from device tree
196 * @hba: per adapter instance
197 *
198 * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
199 * If any of the supplies are not defined it is assumed that they are always-on
200 * and hence return zero. If the property is defined but parsing is failed
201 * then return corresponding error.
202 */
203static int ufshcd_parse_regulator_info(struct ufs_hba *hba)
204{
205 int err;
206 struct device *dev = hba->dev;
207 struct ufs_vreg_info *info = &hba->vreg_info;
208
Raviv Shvili6a771a62014-09-25 15:32:24 +0300209 err = ufshcd_populate_vreg(dev, "vdd-hba", &info->vdd_hba);
210 if (err)
211 goto out;
212
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300213 err = ufshcd_populate_vreg(dev, "vcc", &info->vcc);
214 if (err)
215 goto out;
216
217 err = ufshcd_populate_vreg(dev, "vccq", &info->vccq);
218 if (err)
219 goto out;
220
221 err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2);
222out:
223 return err;
224}
225
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530226#ifdef CONFIG_PM
227/**
228 * ufshcd_pltfrm_suspend - suspend power management function
229 * @dev: pointer to device handle
230 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300231 * Returns 0 if successful
232 * Returns non-zero otherwise
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530233 */
Yaniv Gardi47555a52015-10-28 13:15:49 +0200234int ufshcd_pltfrm_suspend(struct device *dev)
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530235{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300236 return ufshcd_system_suspend(dev_get_drvdata(dev));
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530237}
Yaniv Gardi47555a52015-10-28 13:15:49 +0200238EXPORT_SYMBOL_GPL(ufshcd_pltfrm_suspend);
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530239
240/**
241 * ufshcd_pltfrm_resume - resume power management function
242 * @dev: pointer to device handle
243 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300244 * Returns 0 if successful
245 * Returns non-zero otherwise
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530246 */
Yaniv Gardi47555a52015-10-28 13:15:49 +0200247int ufshcd_pltfrm_resume(struct device *dev)
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530248{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300249 return ufshcd_system_resume(dev_get_drvdata(dev));
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530250}
Yaniv Gardi47555a52015-10-28 13:15:49 +0200251EXPORT_SYMBOL_GPL(ufshcd_pltfrm_resume);
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530252
Yaniv Gardi47555a52015-10-28 13:15:49 +0200253int ufshcd_pltfrm_runtime_suspend(struct device *dev)
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530254{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300255 return ufshcd_runtime_suspend(dev_get_drvdata(dev));
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530256}
Yaniv Gardi47555a52015-10-28 13:15:49 +0200257EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_suspend);
258
259int ufshcd_pltfrm_runtime_resume(struct device *dev)
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530260{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300261 return ufshcd_runtime_resume(dev_get_drvdata(dev));
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530262}
Yaniv Gardi47555a52015-10-28 13:15:49 +0200263EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_resume);
264
265int ufshcd_pltfrm_runtime_idle(struct device *dev)
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530266{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300267 return ufshcd_runtime_idle(dev_get_drvdata(dev));
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530268}
Yaniv Gardi47555a52015-10-28 13:15:49 +0200269EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_idle);
270
Rafael J. Wysocki4f7ad522014-12-14 23:13:55 +0100271#endif /* CONFIG_PM */
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530272
Yaniv Gardi47555a52015-10-28 13:15:49 +0200273void ufshcd_pltfrm_shutdown(struct platform_device *pdev)
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300274{
275 ufshcd_shutdown((struct ufs_hba *)platform_get_drvdata(pdev));
276}
Yaniv Gardi47555a52015-10-28 13:15:49 +0200277EXPORT_SYMBOL_GPL(ufshcd_pltfrm_shutdown);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300278
Yaniv Gardi54b879b2016-03-10 17:37:05 +0200279static void ufshcd_init_lanes_per_dir(struct ufs_hba *hba)
280{
281 struct device *dev = hba->dev;
282 int ret;
283
284 ret = of_property_read_u32(dev->of_node, "lanes-per-direction",
285 &hba->lanes_per_direction);
286 if (ret) {
287 dev_dbg(hba->dev,
288 "%s: failed to read lanes-per-direction, ret=%d\n",
289 __func__, ret);
290 hba->lanes_per_direction = UFSHCD_DEFAULT_LANES_PER_DIRECTION;
291 }
292}
293
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530294/**
Yaniv Gardi47555a52015-10-28 13:15:49 +0200295 * ufshcd_pltfrm_init - probe routine of the driver
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530296 * @pdev: pointer to Platform device handle
Yaniv Gardi47555a52015-10-28 13:15:49 +0200297 * @vops: pointer to variant ops
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530298 *
299 * Returns 0 on success, non-zero value on failure
300 */
Yaniv Gardi47555a52015-10-28 13:15:49 +0200301int ufshcd_pltfrm_init(struct platform_device *pdev,
302 struct ufs_hba_variant_ops *vops)
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530303{
304 struct ufs_hba *hba;
305 void __iomem *mmio_base;
306 struct resource *mem_res;
Seungwon Jeon2953f852013-06-27 13:31:54 +0900307 int irq, err;
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530308 struct device *dev = &pdev->dev;
309
310 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Seungwon Jeon2953f852013-06-27 13:31:54 +0900311 mmio_base = devm_ioremap_resource(dev, mem_res);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300312 if (IS_ERR(*(void **)&mmio_base)) {
313 err = PTR_ERR(*(void **)&mmio_base);
Seungwon Jeon2953f852013-06-27 13:31:54 +0900314 goto out;
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530315 }
316
Seungwon Jeon2953f852013-06-27 13:31:54 +0900317 irq = platform_get_irq(pdev, 0);
318 if (irq < 0) {
319 dev_err(dev, "IRQ resource not available\n");
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530320 err = -ENODEV;
Seungwon Jeon2953f852013-06-27 13:31:54 +0900321 goto out;
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530322 }
323
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300324 err = ufshcd_alloc_host(dev, &hba);
325 if (err) {
326 dev_err(&pdev->dev, "Allocation failed\n");
327 goto out;
328 }
329
Yaniv Gardi47555a52015-10-28 13:15:49 +0200330 hba->vops = vops;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300331
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300332 err = ufshcd_parse_clock_info(hba);
333 if (err) {
334 dev_err(&pdev->dev, "%s: clock parse failed %d\n",
335 __func__, err);
Yaniv Gardi47555a52015-10-28 13:15:49 +0200336 goto dealloc_host;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300337 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300338 err = ufshcd_parse_regulator_info(hba);
339 if (err) {
340 dev_err(&pdev->dev, "%s: regulator init failed %d\n",
341 __func__, err);
Yaniv Gardi47555a52015-10-28 13:15:49 +0200342 goto dealloc_host;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300343 }
344
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530345 pm_runtime_set_active(&pdev->dev);
346 pm_runtime_enable(&pdev->dev);
347
Yaniv Gardi54b879b2016-03-10 17:37:05 +0200348 ufshcd_init_lanes_per_dir(hba);
349
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300350 err = ufshcd_init(hba, mmio_base, irq);
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530351 if (err) {
Colin Ian Kingbad97642015-11-28 16:33:56 +0000352 dev_err(dev, "Initialization failed\n");
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530353 goto out_disable_rpm;
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530354 }
355
356 platform_set_drvdata(pdev, hba);
357
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530358 return 0;
359
360out_disable_rpm:
361 pm_runtime_disable(&pdev->dev);
362 pm_runtime_set_suspended(&pdev->dev);
Yaniv Gardi47555a52015-10-28 13:15:49 +0200363dealloc_host:
364 ufshcd_dealloc_host(hba);
Seungwon Jeon2953f852013-06-27 13:31:54 +0900365out:
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530366 return err;
367}
Yaniv Gardi47555a52015-10-28 13:15:49 +0200368EXPORT_SYMBOL_GPL(ufshcd_pltfrm_init);
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530369
370MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
371MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Joao Pintocc4959c2016-05-11 12:21:25 +0100372MODULE_DESCRIPTION("UFS host controller Platform bus based glue driver");
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530373MODULE_LICENSE("GPL");
374MODULE_VERSION(UFSHCD_DRIVER_VERSION);