GNU Linux-libre 4.14.328-gnu1
[releases.git] / drivers / scsi / ufs / ufshcd-pci.c
1 /*
2  * Universal Flash Storage Host controller PCI glue driver
3  *
4  * This code is based on drivers/scsi/ufs/ufshcd-pci.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 "ufshcd.h"
37 #include <linux/pci.h>
38 #include <linux/pm_runtime.h>
39
40 static int ufs_intel_disable_lcc(struct ufs_hba *hba)
41 {
42         u32 attr = UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE);
43         u32 lcc_enable = 0;
44
45         ufshcd_dme_get(hba, attr, &lcc_enable);
46         if (lcc_enable)
47                 ufshcd_dme_set(hba, attr, 0);
48
49         return 0;
50 }
51
52 static int ufs_intel_link_startup_notify(struct ufs_hba *hba,
53                                          enum ufs_notify_change_status status)
54 {
55         int err = 0;
56
57         switch (status) {
58         case PRE_CHANGE:
59                 err = ufs_intel_disable_lcc(hba);
60                 break;
61         case POST_CHANGE:
62                 break;
63         default:
64                 break;
65         }
66
67         return err;
68 }
69
70 static struct ufs_hba_variant_ops ufs_intel_cnl_hba_vops = {
71         .name                   = "intel-pci",
72         .link_startup_notify    = ufs_intel_link_startup_notify,
73 };
74
75 #ifdef CONFIG_PM_SLEEP
76 /**
77  * ufshcd_pci_suspend - suspend power management function
78  * @pdev: pointer to PCI device handle
79  * @state: power state
80  *
81  * Returns 0 if successful
82  * Returns non-zero otherwise
83  */
84 static int ufshcd_pci_suspend(struct device *dev)
85 {
86         return ufshcd_system_suspend(dev_get_drvdata(dev));
87 }
88
89 /**
90  * ufshcd_pci_resume - resume power management function
91  * @pdev: pointer to PCI device handle
92  *
93  * Returns 0 if successful
94  * Returns non-zero otherwise
95  */
96 static int ufshcd_pci_resume(struct device *dev)
97 {
98         return ufshcd_system_resume(dev_get_drvdata(dev));
99 }
100
101 /**
102  * ufshcd_pci_poweroff - suspend-to-disk poweroff function
103  * @dev: pointer to PCI device handle
104  *
105  * Returns 0 if successful
106  * Returns non-zero otherwise
107  */
108 static int ufshcd_pci_poweroff(struct device *dev)
109 {
110         struct ufs_hba *hba = dev_get_drvdata(dev);
111         int spm_lvl = hba->spm_lvl;
112         int ret;
113
114         /*
115          * For poweroff we need to set the UFS device to PowerDown mode.
116          * Force spm_lvl to ensure that.
117          */
118         hba->spm_lvl = 5;
119         ret = ufshcd_system_suspend(hba);
120         hba->spm_lvl = spm_lvl;
121         return ret;
122 }
123
124 #endif /* !CONFIG_PM_SLEEP */
125
126 #ifdef CONFIG_PM
127 static int ufshcd_pci_runtime_suspend(struct device *dev)
128 {
129         return ufshcd_runtime_suspend(dev_get_drvdata(dev));
130 }
131 static int ufshcd_pci_runtime_resume(struct device *dev)
132 {
133         return ufshcd_runtime_resume(dev_get_drvdata(dev));
134 }
135 static int ufshcd_pci_runtime_idle(struct device *dev)
136 {
137         return ufshcd_runtime_idle(dev_get_drvdata(dev));
138 }
139 #endif /* !CONFIG_PM */
140
141 /**
142  * ufshcd_pci_shutdown - main function to put the controller in reset state
143  * @pdev: pointer to PCI device handle
144  */
145 static void ufshcd_pci_shutdown(struct pci_dev *pdev)
146 {
147         ufshcd_shutdown((struct ufs_hba *)pci_get_drvdata(pdev));
148 }
149
150 /**
151  * ufshcd_pci_remove - de-allocate PCI/SCSI host and host memory space
152  *              data structure memory
153  * @pdev - pointer to PCI handle
154  */
155 static void ufshcd_pci_remove(struct pci_dev *pdev)
156 {
157         struct ufs_hba *hba = pci_get_drvdata(pdev);
158
159         pm_runtime_forbid(&pdev->dev);
160         pm_runtime_get_noresume(&pdev->dev);
161         ufshcd_remove(hba);
162         ufshcd_dealloc_host(hba);
163 }
164
165 /**
166  * ufshcd_pci_probe - probe routine of the driver
167  * @pdev: pointer to PCI device handle
168  * @id: PCI device id
169  *
170  * Returns 0 on success, non-zero value on failure
171  */
172 static int
173 ufshcd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
174 {
175         struct ufs_hba *hba;
176         void __iomem *mmio_base;
177         int err;
178
179         err = pcim_enable_device(pdev);
180         if (err) {
181                 dev_err(&pdev->dev, "pcim_enable_device failed\n");
182                 return err;
183         }
184
185         pci_set_master(pdev);
186
187         err = pcim_iomap_regions(pdev, 1 << 0, UFSHCD);
188         if (err < 0) {
189                 dev_err(&pdev->dev, "request and iomap failed\n");
190                 return err;
191         }
192
193         mmio_base = pcim_iomap_table(pdev)[0];
194
195         err = ufshcd_alloc_host(&pdev->dev, &hba);
196         if (err) {
197                 dev_err(&pdev->dev, "Allocation failed\n");
198                 return err;
199         }
200
201         hba->vops = (struct ufs_hba_variant_ops *)id->driver_data;
202
203         err = ufshcd_init(hba, mmio_base, pdev->irq);
204         if (err) {
205                 dev_err(&pdev->dev, "Initialization failed\n");
206                 ufshcd_dealloc_host(hba);
207                 return err;
208         }
209
210         pci_set_drvdata(pdev, hba);
211         pm_runtime_put_noidle(&pdev->dev);
212         pm_runtime_allow(&pdev->dev);
213
214         return 0;
215 }
216
217 static const struct dev_pm_ops ufshcd_pci_pm_ops = {
218 #ifdef CONFIG_PM_SLEEP
219         .suspend        = ufshcd_pci_suspend,
220         .resume         = ufshcd_pci_resume,
221         .freeze         = ufshcd_pci_suspend,
222         .thaw           = ufshcd_pci_resume,
223         .poweroff       = ufshcd_pci_poweroff,
224         .restore        = ufshcd_pci_resume,
225 #endif
226         SET_RUNTIME_PM_OPS(ufshcd_pci_runtime_suspend,
227                            ufshcd_pci_runtime_resume,
228                            ufshcd_pci_runtime_idle)
229 };
230
231 static const struct pci_device_id ufshcd_pci_tbl[] = {
232         { PCI_VENDOR_ID_SAMSUNG, 0xC00C, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
233         { PCI_VDEVICE(INTEL, 0x9DFA), (kernel_ulong_t)&ufs_intel_cnl_hba_vops },
234         { }     /* terminate list */
235 };
236
237 MODULE_DEVICE_TABLE(pci, ufshcd_pci_tbl);
238
239 static struct pci_driver ufshcd_pci_driver = {
240         .name = UFSHCD,
241         .id_table = ufshcd_pci_tbl,
242         .probe = ufshcd_pci_probe,
243         .remove = ufshcd_pci_remove,
244         .shutdown = ufshcd_pci_shutdown,
245         .driver = {
246                 .pm = &ufshcd_pci_pm_ops
247         },
248 };
249
250 module_pci_driver(ufshcd_pci_driver);
251
252 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
253 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
254 MODULE_DESCRIPTION("UFS host controller PCI glue driver");
255 MODULE_LICENSE("GPL");
256 MODULE_VERSION(UFSHCD_DRIVER_VERSION);