1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Synopsys G210 Test Chip driver
4 *
5 * Copyright (C) 2015-2016 Synopsys, Inc. (www.synopsys.com)
6 *
7 * Authors: Joao Pinto <jpinto@synopsys.com>
8 */
9
10 #include "ufshcd.h"
11 #include "ufshcd-dwc.h"
12 #include "tc-dwc-g210.h"
13
14 #include <linux/pci.h>
15 #include <linux/pm_runtime.h>
16
17 /* Test Chip type expected values */
18 #define TC_G210_20BIT 20
19 #define TC_G210_40BIT 40
20 #define TC_G210_INV 0
21
22 static int tc_type = TC_G210_INV;
23 module_param(tc_type, int, 0);
24 MODULE_PARM_DESC(tc_type, "Test Chip Type (20 = 20-bit, 40 = 40-bit)");
25
tc_dwc_g210_pci_suspend(struct device * dev)26 static int tc_dwc_g210_pci_suspend(struct device *dev)
27 {
28 return ufshcd_system_suspend(dev_get_drvdata(dev));
29 }
30
tc_dwc_g210_pci_resume(struct device * dev)31 static int tc_dwc_g210_pci_resume(struct device *dev)
32 {
33 return ufshcd_system_resume(dev_get_drvdata(dev));
34 }
35
tc_dwc_g210_pci_runtime_suspend(struct device * dev)36 static int tc_dwc_g210_pci_runtime_suspend(struct device *dev)
37 {
38 return ufshcd_runtime_suspend(dev_get_drvdata(dev));
39 }
40
tc_dwc_g210_pci_runtime_resume(struct device * dev)41 static int tc_dwc_g210_pci_runtime_resume(struct device *dev)
42 {
43 return ufshcd_runtime_resume(dev_get_drvdata(dev));
44 }
45
tc_dwc_g210_pci_runtime_idle(struct device * dev)46 static int tc_dwc_g210_pci_runtime_idle(struct device *dev)
47 {
48 return ufshcd_runtime_idle(dev_get_drvdata(dev));
49 }
50
51 /*
52 * struct ufs_hba_dwc_vops - UFS DWC specific variant operations
53 */
54 static struct ufs_hba_variant_ops tc_dwc_g210_pci_hba_vops = {
55 .name = "tc-dwc-g210-pci",
56 .link_startup_notify = ufshcd_dwc_link_startup_notify,
57 };
58
59 /**
60 * tc_dwc_g210_pci_shutdown - main function to put the controller in reset state
61 * @pdev: pointer to PCI device handle
62 */
tc_dwc_g210_pci_shutdown(struct pci_dev * pdev)63 static void tc_dwc_g210_pci_shutdown(struct pci_dev *pdev)
64 {
65 ufshcd_shutdown((struct ufs_hba *)pci_get_drvdata(pdev));
66 }
67
68 /**
69 * tc_dwc_g210_pci_remove - de-allocate PCI/SCSI host and host memory space
70 * data structure memory
71 * @pdev: pointer to PCI handle
72 */
tc_dwc_g210_pci_remove(struct pci_dev * pdev)73 static void tc_dwc_g210_pci_remove(struct pci_dev *pdev)
74 {
75 struct ufs_hba *hba = pci_get_drvdata(pdev);
76
77 pm_runtime_forbid(&pdev->dev);
78 pm_runtime_get_noresume(&pdev->dev);
79 ufshcd_remove(hba);
80 }
81
82 /**
83 * tc_dwc_g210_pci_probe - probe routine of the driver
84 * @pdev: pointer to PCI device handle
85 * @id: PCI device id
86 *
87 * Returns 0 on success, non-zero value on failure
88 */
89 static int
tc_dwc_g210_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)90 tc_dwc_g210_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
91 {
92 struct ufs_hba *hba;
93 void __iomem *mmio_base;
94 int err;
95
96 /* Check Test Chip type and set the specific setup routine */
97 if (tc_type == TC_G210_20BIT) {
98 tc_dwc_g210_pci_hba_vops.phy_initialization =
99 tc_dwc_g210_config_20_bit;
100 } else if (tc_type == TC_G210_40BIT) {
101 tc_dwc_g210_pci_hba_vops.phy_initialization =
102 tc_dwc_g210_config_40_bit;
103 } else {
104 dev_err(&pdev->dev, "test chip version not specified\n");
105 return -EPERM;
106 }
107
108 err = pcim_enable_device(pdev);
109 if (err) {
110 dev_err(&pdev->dev, "pcim_enable_device failed\n");
111 return err;
112 }
113
114 pci_set_master(pdev);
115
116 err = pcim_iomap_regions(pdev, 1 << 0, UFSHCD);
117 if (err < 0) {
118 dev_err(&pdev->dev, "request and iomap failed\n");
119 return err;
120 }
121
122 mmio_base = pcim_iomap_table(pdev)[0];
123
124 err = ufshcd_alloc_host(&pdev->dev, &hba);
125 if (err) {
126 dev_err(&pdev->dev, "Allocation failed\n");
127 return err;
128 }
129
130 hba->vops = &tc_dwc_g210_pci_hba_vops;
131
132 err = ufshcd_init(hba, mmio_base, pdev->irq);
133 if (err) {
134 dev_err(&pdev->dev, "Initialization failed\n");
135 return err;
136 }
137
138 pm_runtime_put_noidle(&pdev->dev);
139 pm_runtime_allow(&pdev->dev);
140
141 return 0;
142 }
143
144 static const struct dev_pm_ops tc_dwc_g210_pci_pm_ops = {
145 .suspend = tc_dwc_g210_pci_suspend,
146 .resume = tc_dwc_g210_pci_resume,
147 .runtime_suspend = tc_dwc_g210_pci_runtime_suspend,
148 .runtime_resume = tc_dwc_g210_pci_runtime_resume,
149 .runtime_idle = tc_dwc_g210_pci_runtime_idle,
150 };
151
152 static const struct pci_device_id tc_dwc_g210_pci_tbl[] = {
153 { PCI_VENDOR_ID_SYNOPSYS, 0xB101, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
154 { PCI_VENDOR_ID_SYNOPSYS, 0xB102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
155 { } /* terminate list */
156 };
157
158 MODULE_DEVICE_TABLE(pci, tc_dwc_g210_pci_tbl);
159
160 static struct pci_driver tc_dwc_g210_pci_driver = {
161 .name = "tc-dwc-g210-pci",
162 .id_table = tc_dwc_g210_pci_tbl,
163 .probe = tc_dwc_g210_pci_probe,
164 .remove = tc_dwc_g210_pci_remove,
165 .shutdown = tc_dwc_g210_pci_shutdown,
166 .driver = {
167 .pm = &tc_dwc_g210_pci_pm_ops
168 },
169 };
170
171 module_pci_driver(tc_dwc_g210_pci_driver);
172
173 MODULE_AUTHOR("Joao Pinto <Joao.Pinto@synopsys.com>");
174 MODULE_DESCRIPTION("Synopsys Test Chip G210 PCI glue driver");
175 MODULE_LICENSE("Dual BSD/GPL");
176