Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 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> |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame^] | 38 | #include <linux/pm_runtime.h> |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 39 | |
| 40 | #ifdef CONFIG_PM |
| 41 | /** |
| 42 | * ufshcd_pci_suspend - suspend power management function |
| 43 | * @pdev: pointer to PCI device handle |
| 44 | * @state: power state |
| 45 | * |
| 46 | * Returns -ENOSYS |
| 47 | */ |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame^] | 48 | static int ufshcd_pci_suspend(struct device *dev) |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 49 | { |
| 50 | /* |
| 51 | * TODO: |
| 52 | * 1. Call ufshcd_suspend |
| 53 | * 2. Do bus specific power management |
| 54 | */ |
| 55 | |
| 56 | return -ENOSYS; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * ufshcd_pci_resume - resume power management function |
| 61 | * @pdev: pointer to PCI device handle |
| 62 | * |
| 63 | * Returns -ENOSYS |
| 64 | */ |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame^] | 65 | static int ufshcd_pci_resume(struct device *dev) |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 66 | { |
| 67 | /* |
| 68 | * TODO: |
| 69 | * 1. Call ufshcd_resume. |
| 70 | * 2. Do bus specific wake up |
| 71 | */ |
| 72 | |
| 73 | return -ENOSYS; |
| 74 | } |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame^] | 75 | #else |
| 76 | #define ufshcd_pci_suspend NULL |
| 77 | #define ufshcd_pci_resume NULL |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 78 | #endif /* CONFIG_PM */ |
| 79 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame^] | 80 | #ifdef CONFIG_PM_RUNTIME |
| 81 | static int ufshcd_pci_runtime_suspend(struct device *dev) |
| 82 | { |
| 83 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 84 | |
| 85 | if (!hba) |
| 86 | return 0; |
| 87 | |
| 88 | return ufshcd_runtime_suspend(hba); |
| 89 | } |
| 90 | static int ufshcd_pci_runtime_resume(struct device *dev) |
| 91 | { |
| 92 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 93 | |
| 94 | if (!hba) |
| 95 | return 0; |
| 96 | |
| 97 | return ufshcd_runtime_resume(hba); |
| 98 | } |
| 99 | static int ufshcd_pci_runtime_idle(struct device *dev) |
| 100 | { |
| 101 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 102 | |
| 103 | if (!hba) |
| 104 | return 0; |
| 105 | |
| 106 | return ufshcd_runtime_idle(hba); |
| 107 | } |
| 108 | #else /* !CONFIG_PM_RUNTIME */ |
| 109 | #define ufshcd_pci_runtime_suspend NULL |
| 110 | #define ufshcd_pci_runtime_resume NULL |
| 111 | #define ufshcd_pci_runtime_idle NULL |
| 112 | #endif /* CONFIG_PM_RUNTIME */ |
| 113 | |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 114 | /** |
| 115 | * ufshcd_pci_shutdown - main function to put the controller in reset state |
| 116 | * @pdev: pointer to PCI device handle |
| 117 | */ |
| 118 | static void ufshcd_pci_shutdown(struct pci_dev *pdev) |
| 119 | { |
| 120 | ufshcd_hba_stop((struct ufs_hba *)pci_get_drvdata(pdev)); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * ufshcd_pci_remove - de-allocate PCI/SCSI host and host memory space |
| 125 | * data structure memory |
| 126 | * @pdev - pointer to PCI handle |
| 127 | */ |
| 128 | static void ufshcd_pci_remove(struct pci_dev *pdev) |
| 129 | { |
| 130 | struct ufs_hba *hba = pci_get_drvdata(pdev); |
| 131 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame^] | 132 | pm_runtime_forbid(&pdev->dev); |
| 133 | pm_runtime_get_noresume(&pdev->dev); |
| 134 | |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 135 | disable_irq(pdev->irq); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 136 | ufshcd_remove(hba); |
| 137 | pci_release_regions(pdev); |
| 138 | pci_set_drvdata(pdev, NULL); |
| 139 | pci_clear_master(pdev); |
| 140 | pci_disable_device(pdev); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * ufshcd_set_dma_mask - Set dma mask based on the controller |
| 145 | * addressing capability |
| 146 | * @pdev: PCI device structure |
| 147 | * |
| 148 | * Returns 0 for success, non-zero for failure |
| 149 | */ |
| 150 | static int ufshcd_set_dma_mask(struct pci_dev *pdev) |
| 151 | { |
| 152 | int err; |
| 153 | |
| 154 | if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) |
| 155 | && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) |
| 156 | return 0; |
| 157 | err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); |
| 158 | if (!err) |
| 159 | err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); |
| 160 | return err; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * ufshcd_pci_probe - probe routine of the driver |
| 165 | * @pdev: pointer to PCI device handle |
| 166 | * @id: PCI device id |
| 167 | * |
| 168 | * Returns 0 on success, non-zero value on failure |
| 169 | */ |
| 170 | static int |
| 171 | ufshcd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 172 | { |
| 173 | struct ufs_hba *hba; |
| 174 | void __iomem *mmio_base; |
| 175 | int err; |
| 176 | |
| 177 | err = pci_enable_device(pdev); |
| 178 | if (err) { |
| 179 | dev_err(&pdev->dev, "pci_enable_device failed\n"); |
| 180 | goto out_error; |
| 181 | } |
| 182 | |
| 183 | pci_set_master(pdev); |
| 184 | |
| 185 | |
| 186 | err = pci_request_regions(pdev, UFSHCD); |
| 187 | if (err < 0) { |
| 188 | dev_err(&pdev->dev, "request regions failed\n"); |
| 189 | goto out_disable; |
| 190 | } |
| 191 | |
| 192 | mmio_base = pci_ioremap_bar(pdev, 0); |
| 193 | if (!mmio_base) { |
| 194 | dev_err(&pdev->dev, "memory map failed\n"); |
| 195 | err = -ENOMEM; |
| 196 | goto out_release_regions; |
| 197 | } |
| 198 | |
| 199 | err = ufshcd_set_dma_mask(pdev); |
| 200 | if (err) { |
| 201 | dev_err(&pdev->dev, "set dma mask failed\n"); |
| 202 | goto out_iounmap; |
| 203 | } |
| 204 | |
| 205 | err = ufshcd_init(&pdev->dev, &hba, mmio_base, pdev->irq); |
| 206 | if (err) { |
| 207 | dev_err(&pdev->dev, "Initialization failed\n"); |
| 208 | goto out_iounmap; |
| 209 | } |
| 210 | |
| 211 | pci_set_drvdata(pdev, hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame^] | 212 | pm_runtime_put_noidle(&pdev->dev); |
| 213 | pm_runtime_allow(&pdev->dev); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 214 | |
| 215 | return 0; |
| 216 | |
| 217 | out_iounmap: |
| 218 | iounmap(mmio_base); |
| 219 | out_release_regions: |
| 220 | pci_release_regions(pdev); |
| 221 | out_disable: |
| 222 | pci_clear_master(pdev); |
| 223 | pci_disable_device(pdev); |
| 224 | out_error: |
| 225 | return err; |
| 226 | } |
| 227 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame^] | 228 | static const struct dev_pm_ops ufshcd_pci_pm_ops = { |
| 229 | .suspend = ufshcd_pci_suspend, |
| 230 | .resume = ufshcd_pci_resume, |
| 231 | .runtime_suspend = ufshcd_pci_runtime_suspend, |
| 232 | .runtime_resume = ufshcd_pci_runtime_resume, |
| 233 | .runtime_idle = ufshcd_pci_runtime_idle, |
| 234 | }; |
| 235 | |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 236 | static DEFINE_PCI_DEVICE_TABLE(ufshcd_pci_tbl) = { |
| 237 | { PCI_VENDOR_ID_SAMSUNG, 0xC00C, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, |
| 238 | { } /* terminate list */ |
| 239 | }; |
| 240 | |
| 241 | MODULE_DEVICE_TABLE(pci, ufshcd_pci_tbl); |
| 242 | |
| 243 | static struct pci_driver ufshcd_pci_driver = { |
| 244 | .name = UFSHCD, |
| 245 | .id_table = ufshcd_pci_tbl, |
| 246 | .probe = ufshcd_pci_probe, |
| 247 | .remove = ufshcd_pci_remove, |
| 248 | .shutdown = ufshcd_pci_shutdown, |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame^] | 249 | .driver = { |
| 250 | .pm = &ufshcd_pci_pm_ops |
| 251 | }, |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | module_pci_driver(ufshcd_pci_driver); |
| 255 | |
| 256 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 257 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
| 258 | MODULE_DESCRIPTION("UFS host controller PCI glue driver"); |
| 259 | MODULE_LICENSE("GPL"); |
| 260 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |