Merge tag 'irqchip-fixes-5.10-1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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 int ufs_intel_ehl_init(struct ufs_hba *hba)
48 {
49         hba->quirks |= UFSHCD_QUIRK_BROKEN_AUTO_HIBERN8;
50         return 0;
51 }
52
53 static struct ufs_hba_variant_ops ufs_intel_cnl_hba_vops = {
54         .name                   = "intel-pci",
55         .link_startup_notify    = ufs_intel_link_startup_notify,
56 };
57
58 static struct ufs_hba_variant_ops ufs_intel_ehl_hba_vops = {
59         .name                   = "intel-pci",
60         .init                   = ufs_intel_ehl_init,
61         .link_startup_notify    = ufs_intel_link_startup_notify,
62 };
63
64 #ifdef CONFIG_PM_SLEEP
65 /**
66  * ufshcd_pci_suspend - suspend 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_suspend(struct device *dev)
73 {
74         return ufshcd_system_suspend(dev_get_drvdata(dev));
75 }
76
77 /**
78  * ufshcd_pci_resume - resume power management function
79  * @dev: pointer to PCI device handle
80  *
81  * Returns 0 if successful
82  * Returns non-zero otherwise
83  */
84 static int ufshcd_pci_resume(struct device *dev)
85 {
86         return ufshcd_system_resume(dev_get_drvdata(dev));
87 }
88 #endif /* !CONFIG_PM_SLEEP */
89
90 #ifdef CONFIG_PM
91 static int ufshcd_pci_runtime_suspend(struct device *dev)
92 {
93         return ufshcd_runtime_suspend(dev_get_drvdata(dev));
94 }
95 static int ufshcd_pci_runtime_resume(struct device *dev)
96 {
97         return ufshcd_runtime_resume(dev_get_drvdata(dev));
98 }
99 static int ufshcd_pci_runtime_idle(struct device *dev)
100 {
101         return ufshcd_runtime_idle(dev_get_drvdata(dev));
102 }
103 #endif /* !CONFIG_PM */
104
105 /**
106  * ufshcd_pci_shutdown - main function to put the controller in reset state
107  * @pdev: pointer to PCI device handle
108  */
109 static void ufshcd_pci_shutdown(struct pci_dev *pdev)
110 {
111         ufshcd_shutdown((struct ufs_hba *)pci_get_drvdata(pdev));
112 }
113
114 /**
115  * ufshcd_pci_remove - de-allocate PCI/SCSI host and host memory space
116  *              data structure memory
117  * @pdev: pointer to PCI handle
118  */
119 static void ufshcd_pci_remove(struct pci_dev *pdev)
120 {
121         struct ufs_hba *hba = pci_get_drvdata(pdev);
122
123         pm_runtime_forbid(&pdev->dev);
124         pm_runtime_get_noresume(&pdev->dev);
125         ufshcd_remove(hba);
126         ufshcd_dealloc_host(hba);
127 }
128
129 /**
130  * ufshcd_pci_probe - probe routine of the driver
131  * @pdev: pointer to PCI device handle
132  * @id: PCI device id
133  *
134  * Returns 0 on success, non-zero value on failure
135  */
136 static int
137 ufshcd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
138 {
139         struct ufs_hba *hba;
140         void __iomem *mmio_base;
141         int err;
142
143         err = pcim_enable_device(pdev);
144         if (err) {
145                 dev_err(&pdev->dev, "pcim_enable_device failed\n");
146                 return err;
147         }
148
149         pci_set_master(pdev);
150
151         err = pcim_iomap_regions(pdev, 1 << 0, UFSHCD);
152         if (err < 0) {
153                 dev_err(&pdev->dev, "request and iomap failed\n");
154                 return err;
155         }
156
157         mmio_base = pcim_iomap_table(pdev)[0];
158
159         err = ufshcd_alloc_host(&pdev->dev, &hba);
160         if (err) {
161                 dev_err(&pdev->dev, "Allocation failed\n");
162                 return err;
163         }
164
165         hba->vops = (struct ufs_hba_variant_ops *)id->driver_data;
166
167         err = ufshcd_init(hba, mmio_base, pdev->irq);
168         if (err) {
169                 dev_err(&pdev->dev, "Initialization failed\n");
170                 ufshcd_dealloc_host(hba);
171                 return err;
172         }
173
174         pci_set_drvdata(pdev, hba);
175         pm_runtime_put_noidle(&pdev->dev);
176         pm_runtime_allow(&pdev->dev);
177
178         return 0;
179 }
180
181 static const struct dev_pm_ops ufshcd_pci_pm_ops = {
182         SET_SYSTEM_SLEEP_PM_OPS(ufshcd_pci_suspend,
183                                 ufshcd_pci_resume)
184         SET_RUNTIME_PM_OPS(ufshcd_pci_runtime_suspend,
185                            ufshcd_pci_runtime_resume,
186                            ufshcd_pci_runtime_idle)
187 };
188
189 static const struct pci_device_id ufshcd_pci_tbl[] = {
190         { PCI_VENDOR_ID_SAMSUNG, 0xC00C, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
191         { PCI_VDEVICE(INTEL, 0x9DFA), (kernel_ulong_t)&ufs_intel_cnl_hba_vops },
192         { PCI_VDEVICE(INTEL, 0x4B41), (kernel_ulong_t)&ufs_intel_ehl_hba_vops },
193         { PCI_VDEVICE(INTEL, 0x4B43), (kernel_ulong_t)&ufs_intel_ehl_hba_vops },
194         { }     /* terminate list */
195 };
196
197 MODULE_DEVICE_TABLE(pci, ufshcd_pci_tbl);
198
199 static struct pci_driver ufshcd_pci_driver = {
200         .name = UFSHCD,
201         .id_table = ufshcd_pci_tbl,
202         .probe = ufshcd_pci_probe,
203         .remove = ufshcd_pci_remove,
204         .shutdown = ufshcd_pci_shutdown,
205         .driver = {
206                 .pm = &ufshcd_pci_pm_ops
207         },
208 };
209
210 module_pci_driver(ufshcd_pci_driver);
211
212 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
213 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
214 MODULE_DESCRIPTION("UFS host controller PCI glue driver");
215 MODULE_LICENSE("GPL");
216 MODULE_VERSION(UFSHCD_DRIVER_VERSION);