Merge tag '9p-for-5.9-rc1' of git://github.com/martinetd/linux
[linux-2.6-microblaze.git] / drivers / scsi / ufs / ufshcd-pci.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Universal Flash Storage Host controller PCI glue driver
4  *
5  * This code is based on drivers/scsi/ufs/ufshcd-pci.c
6  * Copyright (C) 2011-2013 Samsung India Software Operations
7  *
8  * Authors:
9  *      Santosh Yaraganavi <santosh.sy@samsung.com>
10  *      Vinayak Holikatti <h.vinayak@samsung.com>
11  */
12
13 #include "ufshcd.h"
14 #include <linux/pci.h>
15 #include <linux/pm_runtime.h>
16
17 static int ufs_intel_disable_lcc(struct ufs_hba *hba)
18 {
19         u32 attr = UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE);
20         u32 lcc_enable = 0;
21
22         ufshcd_dme_get(hba, attr, &lcc_enable);
23         if (lcc_enable)
24                 ufshcd_disable_host_tx_lcc(hba);
25
26         return 0;
27 }
28
29 static int ufs_intel_link_startup_notify(struct ufs_hba *hba,
30                                          enum ufs_notify_change_status status)
31 {
32         int err = 0;
33
34         switch (status) {
35         case PRE_CHANGE:
36                 err = ufs_intel_disable_lcc(hba);
37                 break;
38         case POST_CHANGE:
39                 break;
40         default:
41                 break;
42         }
43
44         return err;
45 }
46
47 static struct ufs_hba_variant_ops ufs_intel_cnl_hba_vops = {
48         .name                   = "intel-pci",
49         .link_startup_notify    = ufs_intel_link_startup_notify,
50 };
51
52 #ifdef CONFIG_PM_SLEEP
53 /**
54  * ufshcd_pci_suspend - suspend power management function
55  * @dev: pointer to PCI device handle
56  *
57  * Returns 0 if successful
58  * Returns non-zero otherwise
59  */
60 static int ufshcd_pci_suspend(struct device *dev)
61 {
62         return ufshcd_system_suspend(dev_get_drvdata(dev));
63 }
64
65 /**
66  * ufshcd_pci_resume - resume power management function
67  * @dev: pointer to PCI device handle
68  *
69  * Returns 0 if successful
70  * Returns non-zero otherwise
71  */
72 static int ufshcd_pci_resume(struct device *dev)
73 {
74         return ufshcd_system_resume(dev_get_drvdata(dev));
75 }
76 #endif /* !CONFIG_PM_SLEEP */
77
78 #ifdef CONFIG_PM
79 static int ufshcd_pci_runtime_suspend(struct device *dev)
80 {
81         return ufshcd_runtime_suspend(dev_get_drvdata(dev));
82 }
83 static int ufshcd_pci_runtime_resume(struct device *dev)
84 {
85         return ufshcd_runtime_resume(dev_get_drvdata(dev));
86 }
87 static int ufshcd_pci_runtime_idle(struct device *dev)
88 {
89         return ufshcd_runtime_idle(dev_get_drvdata(dev));
90 }
91 #endif /* !CONFIG_PM */
92
93 /**
94  * ufshcd_pci_shutdown - main function to put the controller in reset state
95  * @pdev: pointer to PCI device handle
96  */
97 static void ufshcd_pci_shutdown(struct pci_dev *pdev)
98 {
99         ufshcd_shutdown((struct ufs_hba *)pci_get_drvdata(pdev));
100 }
101
102 /**
103  * ufshcd_pci_remove - de-allocate PCI/SCSI host and host memory space
104  *              data structure memory
105  * @pdev: pointer to PCI handle
106  */
107 static void ufshcd_pci_remove(struct pci_dev *pdev)
108 {
109         struct ufs_hba *hba = pci_get_drvdata(pdev);
110
111         pm_runtime_forbid(&pdev->dev);
112         pm_runtime_get_noresume(&pdev->dev);
113         ufshcd_remove(hba);
114         ufshcd_dealloc_host(hba);
115 }
116
117 /**
118  * ufshcd_pci_probe - probe routine of the driver
119  * @pdev: pointer to PCI device handle
120  * @id: PCI device id
121  *
122  * Returns 0 on success, non-zero value on failure
123  */
124 static int
125 ufshcd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
126 {
127         struct ufs_hba *hba;
128         void __iomem *mmio_base;
129         int err;
130
131         err = pcim_enable_device(pdev);
132         if (err) {
133                 dev_err(&pdev->dev, "pcim_enable_device failed\n");
134                 return err;
135         }
136
137         pci_set_master(pdev);
138
139         err = pcim_iomap_regions(pdev, 1 << 0, UFSHCD);
140         if (err < 0) {
141                 dev_err(&pdev->dev, "request and iomap failed\n");
142                 return err;
143         }
144
145         mmio_base = pcim_iomap_table(pdev)[0];
146
147         err = ufshcd_alloc_host(&pdev->dev, &hba);
148         if (err) {
149                 dev_err(&pdev->dev, "Allocation failed\n");
150                 return err;
151         }
152
153         hba->vops = (struct ufs_hba_variant_ops *)id->driver_data;
154
155         err = ufshcd_init(hba, mmio_base, pdev->irq);
156         if (err) {
157                 dev_err(&pdev->dev, "Initialization failed\n");
158                 ufshcd_dealloc_host(hba);
159                 return err;
160         }
161
162         pci_set_drvdata(pdev, hba);
163         pm_runtime_put_noidle(&pdev->dev);
164         pm_runtime_allow(&pdev->dev);
165
166         return 0;
167 }
168
169 static const struct dev_pm_ops ufshcd_pci_pm_ops = {
170         SET_SYSTEM_SLEEP_PM_OPS(ufshcd_pci_suspend,
171                                 ufshcd_pci_resume)
172         SET_RUNTIME_PM_OPS(ufshcd_pci_runtime_suspend,
173                            ufshcd_pci_runtime_resume,
174                            ufshcd_pci_runtime_idle)
175 };
176
177 static const struct pci_device_id ufshcd_pci_tbl[] = {
178         { PCI_VENDOR_ID_SAMSUNG, 0xC00C, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
179         { PCI_VDEVICE(INTEL, 0x9DFA), (kernel_ulong_t)&ufs_intel_cnl_hba_vops },
180         { PCI_VDEVICE(INTEL, 0x4B41), (kernel_ulong_t)&ufs_intel_cnl_hba_vops },
181         { PCI_VDEVICE(INTEL, 0x4B43), (kernel_ulong_t)&ufs_intel_cnl_hba_vops },
182         { }     /* terminate list */
183 };
184
185 MODULE_DEVICE_TABLE(pci, ufshcd_pci_tbl);
186
187 static struct pci_driver ufshcd_pci_driver = {
188         .name = UFSHCD,
189         .id_table = ufshcd_pci_tbl,
190         .probe = ufshcd_pci_probe,
191         .remove = ufshcd_pci_remove,
192         .shutdown = ufshcd_pci_shutdown,
193         .driver = {
194                 .pm = &ufshcd_pci_pm_ops
195         },
196 };
197
198 module_pci_driver(ufshcd_pci_driver);
199
200 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
201 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
202 MODULE_DESCRIPTION("UFS host controller PCI glue driver");
203 MODULE_LICENSE("GPL");
204 MODULE_VERSION(UFSHCD_DRIVER_VERSION);