blob: 925b0ec7ec54d26d3490b91210c7424678360f42 [file] [log] [blame]
Vinayak Holikattie0eca632013-02-25 21:44:33 +05301/*
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>
Sujit Reddy Thumma62694732013-07-30 00:36:00 +053038#include <linux/pm_runtime.h>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053039
Adrian Hunter2c87ea92017-06-06 14:35:31 +030040static 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
52static 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
70static 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
Adrian Hunter7b573382017-06-06 14:35:30 +030075#ifdef CONFIG_PM_SLEEP
Vinayak Holikattie0eca632013-02-25 21:44:33 +053076/**
77 * ufshcd_pci_suspend - suspend power management function
78 * @pdev: pointer to PCI device handle
79 * @state: power state
80 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +030081 * Returns 0 if successful
82 * Returns non-zero otherwise
Vinayak Holikattie0eca632013-02-25 21:44:33 +053083 */
Sujit Reddy Thumma62694732013-07-30 00:36:00 +053084static int ufshcd_pci_suspend(struct device *dev)
Vinayak Holikattie0eca632013-02-25 21:44:33 +053085{
Subhash Jadavani57d104c2014-09-25 15:32:30 +030086 return ufshcd_system_suspend(dev_get_drvdata(dev));
Vinayak Holikattie0eca632013-02-25 21:44:33 +053087}
88
89/**
90 * ufshcd_pci_resume - resume power management function
91 * @pdev: pointer to PCI device handle
92 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +030093 * Returns 0 if successful
94 * Returns non-zero otherwise
Vinayak Holikattie0eca632013-02-25 21:44:33 +053095 */
Sujit Reddy Thumma62694732013-07-30 00:36:00 +053096static int ufshcd_pci_resume(struct device *dev)
Vinayak Holikattie0eca632013-02-25 21:44:33 +053097{
Subhash Jadavani57d104c2014-09-25 15:32:30 +030098 return ufshcd_system_resume(dev_get_drvdata(dev));
Vinayak Holikattie0eca632013-02-25 21:44:33 +053099}
Adrian Hunter7b573382017-06-06 14:35:30 +0300100#endif /* !CONFIG_PM_SLEEP */
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530101
Adrian Hunter7b573382017-06-06 14:35:30 +0300102#ifdef CONFIG_PM
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530103static int ufshcd_pci_runtime_suspend(struct device *dev)
104{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300105 return ufshcd_runtime_suspend(dev_get_drvdata(dev));
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530106}
107static int ufshcd_pci_runtime_resume(struct device *dev)
108{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300109 return ufshcd_runtime_resume(dev_get_drvdata(dev));
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530110}
111static int ufshcd_pci_runtime_idle(struct device *dev)
112{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300113 return ufshcd_runtime_idle(dev_get_drvdata(dev));
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530114}
Adrian Hunter7b573382017-06-06 14:35:30 +0300115#endif /* !CONFIG_PM */
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530116
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530117/**
118 * ufshcd_pci_shutdown - main function to put the controller in reset state
119 * @pdev: pointer to PCI device handle
120 */
121static void ufshcd_pci_shutdown(struct pci_dev *pdev)
122{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300123 ufshcd_shutdown((struct ufs_hba *)pci_get_drvdata(pdev));
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530124}
125
126/**
127 * ufshcd_pci_remove - de-allocate PCI/SCSI host and host memory space
128 * data structure memory
129 * @pdev - pointer to PCI handle
130 */
131static void ufshcd_pci_remove(struct pci_dev *pdev)
132{
133 struct ufs_hba *hba = pci_get_drvdata(pdev);
134
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530135 pm_runtime_forbid(&pdev->dev);
136 pm_runtime_get_noresume(&pdev->dev);
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530137 ufshcd_remove(hba);
Subhash Jadavaniafa3dfd2016-10-27 17:25:58 -0700138 ufshcd_dealloc_host(hba);
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530139}
140
141/**
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530142 * ufshcd_pci_probe - probe routine of the driver
143 * @pdev: pointer to PCI device handle
144 * @id: PCI device id
145 *
146 * Returns 0 on success, non-zero value on failure
147 */
148static int
149ufshcd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
150{
151 struct ufs_hba *hba;
152 void __iomem *mmio_base;
153 int err;
154
Akinobu Mita36f4f3b2013-07-30 00:36:01 +0530155 err = pcim_enable_device(pdev);
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530156 if (err) {
Akinobu Mita36f4f3b2013-07-30 00:36:01 +0530157 dev_err(&pdev->dev, "pcim_enable_device failed\n");
158 return err;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530159 }
160
161 pci_set_master(pdev);
162
Akinobu Mita36f4f3b2013-07-30 00:36:01 +0530163 err = pcim_iomap_regions(pdev, 1 << 0, UFSHCD);
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530164 if (err < 0) {
Akinobu Mita36f4f3b2013-07-30 00:36:01 +0530165 dev_err(&pdev->dev, "request and iomap failed\n");
166 return err;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530167 }
168
Akinobu Mita36f4f3b2013-07-30 00:36:01 +0530169 mmio_base = pcim_iomap_table(pdev)[0];
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530170
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300171 err = ufshcd_alloc_host(&pdev->dev, &hba);
172 if (err) {
173 dev_err(&pdev->dev, "Allocation failed\n");
174 return err;
175 }
176
Adrian Hunter2c87ea92017-06-06 14:35:31 +0300177 hba->vops = (struct ufs_hba_variant_ops *)id->driver_data;
178
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300179 err = ufshcd_init(hba, mmio_base, pdev->irq);
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530180 if (err) {
181 dev_err(&pdev->dev, "Initialization failed\n");
Subhash Jadavaniafa3dfd2016-10-27 17:25:58 -0700182 ufshcd_dealloc_host(hba);
Akinobu Mita36f4f3b2013-07-30 00:36:01 +0530183 return err;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530184 }
185
186 pci_set_drvdata(pdev, hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530187 pm_runtime_put_noidle(&pdev->dev);
188 pm_runtime_allow(&pdev->dev);
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530189
190 return 0;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530191}
192
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530193static const struct dev_pm_ops ufshcd_pci_pm_ops = {
Adrian Hunter7b573382017-06-06 14:35:30 +0300194 SET_SYSTEM_SLEEP_PM_OPS(ufshcd_pci_suspend,
195 ufshcd_pci_resume)
196 SET_RUNTIME_PM_OPS(ufshcd_pci_runtime_suspend,
197 ufshcd_pci_runtime_resume,
198 ufshcd_pci_runtime_idle)
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530199};
200
Benoit Taine9baa3c32014-08-08 15:56:03 +0200201static const struct pci_device_id ufshcd_pci_tbl[] = {
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530202 { PCI_VENDOR_ID_SAMSUNG, 0xC00C, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
Adrian Hunter2c87ea92017-06-06 14:35:31 +0300203 { PCI_VDEVICE(INTEL, 0x9DFA), (kernel_ulong_t)&ufs_intel_cnl_hba_vops },
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530204 { } /* terminate list */
205};
206
207MODULE_DEVICE_TABLE(pci, ufshcd_pci_tbl);
208
209static struct pci_driver ufshcd_pci_driver = {
210 .name = UFSHCD,
211 .id_table = ufshcd_pci_tbl,
212 .probe = ufshcd_pci_probe,
213 .remove = ufshcd_pci_remove,
214 .shutdown = ufshcd_pci_shutdown,
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530215 .driver = {
216 .pm = &ufshcd_pci_pm_ops
217 },
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530218};
219
220module_pci_driver(ufshcd_pci_driver);
221
222MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
223MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
224MODULE_DESCRIPTION("UFS host controller PCI glue driver");
225MODULE_LICENSE("GPL");
226MODULE_VERSION(UFSHCD_DRIVER_VERSION);