GNU Linux-libre 4.9.292-gnu1
[releases.git] / drivers / spi / spi-pxa2xx-pci.c
1 /*
2  * CE4100's SPI device is more or less the same one as found on PXA
3  *
4  * Copyright (C) 2016, Intel Corporation
5  */
6 #include <linux/clk-provider.h>
7 #include <linux/module.h>
8 #include <linux/of_device.h>
9 #include <linux/pci.h>
10 #include <linux/platform_device.h>
11 #include <linux/spi/pxa2xx_spi.h>
12
13 #include <linux/dmaengine.h>
14 #include <linux/platform_data/dma-dw.h>
15
16 enum {
17         PORT_QUARK_X1000,
18         PORT_BYT,
19         PORT_MRFLD,
20         PORT_BSW0,
21         PORT_BSW1,
22         PORT_BSW2,
23         PORT_CE4100,
24         PORT_LPT0,
25         PORT_LPT1,
26 };
27
28 struct pxa_spi_info {
29         enum pxa_ssp_type type;
30         int port_id;
31         int num_chipselect;
32         unsigned long max_clk_rate;
33
34         /* DMA channel request parameters */
35         bool (*dma_filter)(struct dma_chan *chan, void *param);
36         void *tx_param;
37         void *rx_param;
38
39         int (*setup)(struct pci_dev *pdev, struct pxa_spi_info *c);
40 };
41
42 static struct dw_dma_slave byt_tx_param = { .dst_id = 0 };
43 static struct dw_dma_slave byt_rx_param = { .src_id = 1 };
44
45 static struct dw_dma_slave bsw0_tx_param = { .dst_id = 0 };
46 static struct dw_dma_slave bsw0_rx_param = { .src_id = 1 };
47 static struct dw_dma_slave bsw1_tx_param = { .dst_id = 6 };
48 static struct dw_dma_slave bsw1_rx_param = { .src_id = 7 };
49 static struct dw_dma_slave bsw2_tx_param = { .dst_id = 8 };
50 static struct dw_dma_slave bsw2_rx_param = { .src_id = 9 };
51
52 static struct dw_dma_slave lpt1_tx_param = { .dst_id = 0 };
53 static struct dw_dma_slave lpt1_rx_param = { .src_id = 1 };
54 static struct dw_dma_slave lpt0_tx_param = { .dst_id = 2 };
55 static struct dw_dma_slave lpt0_rx_param = { .src_id = 3 };
56
57 static bool lpss_dma_filter(struct dma_chan *chan, void *param)
58 {
59         struct dw_dma_slave *dws = param;
60
61         if (dws->dma_dev != chan->device->dev)
62                 return false;
63
64         chan->private = dws;
65         return true;
66 }
67
68 static int lpss_spi_setup(struct pci_dev *dev, struct pxa_spi_info *c)
69 {
70         struct pci_dev *dma_dev;
71
72         c->num_chipselect = 1;
73         c->max_clk_rate = 50000000;
74
75         dma_dev = pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
76
77         if (c->tx_param) {
78                 struct dw_dma_slave *slave = c->tx_param;
79
80                 slave->dma_dev = &dma_dev->dev;
81                 slave->m_master = 0;
82                 slave->p_master = 1;
83         }
84
85         if (c->rx_param) {
86                 struct dw_dma_slave *slave = c->rx_param;
87
88                 slave->dma_dev = &dma_dev->dev;
89                 slave->m_master = 0;
90                 slave->p_master = 1;
91         }
92
93         c->dma_filter = lpss_dma_filter;
94         return 0;
95 }
96
97 static int mrfld_spi_setup(struct pci_dev *dev, struct pxa_spi_info *c)
98 {
99         switch (PCI_FUNC(dev->devfn)) {
100         case 0:
101                 c->port_id = 3;
102                 c->num_chipselect = 1;
103                 break;
104         case 1:
105                 c->port_id = 5;
106                 c->num_chipselect = 4;
107                 break;
108         case 2:
109                 c->port_id = 6;
110                 c->num_chipselect = 1;
111                 break;
112         default:
113                 return -ENODEV;
114         }
115         return 0;
116 }
117
118 static struct pxa_spi_info spi_info_configs[] = {
119         [PORT_CE4100] = {
120                 .type = PXA25x_SSP,
121                 .port_id =  -1,
122                 .num_chipselect = -1,
123                 .max_clk_rate = 3686400,
124         },
125         [PORT_BYT] = {
126                 .type = LPSS_BYT_SSP,
127                 .port_id = 0,
128                 .setup = lpss_spi_setup,
129                 .tx_param = &byt_tx_param,
130                 .rx_param = &byt_rx_param,
131         },
132         [PORT_BSW0] = {
133                 .type = LPSS_BSW_SSP,
134                 .port_id = 0,
135                 .setup = lpss_spi_setup,
136                 .tx_param = &bsw0_tx_param,
137                 .rx_param = &bsw0_rx_param,
138         },
139         [PORT_BSW1] = {
140                 .type = LPSS_BSW_SSP,
141                 .port_id = 1,
142                 .setup = lpss_spi_setup,
143                 .tx_param = &bsw1_tx_param,
144                 .rx_param = &bsw1_rx_param,
145         },
146         [PORT_BSW2] = {
147                 .type = LPSS_BSW_SSP,
148                 .port_id = 2,
149                 .setup = lpss_spi_setup,
150                 .tx_param = &bsw2_tx_param,
151                 .rx_param = &bsw2_rx_param,
152         },
153         [PORT_MRFLD] = {
154                 .type = PXA27x_SSP,
155                 .max_clk_rate = 25000000,
156                 .setup = mrfld_spi_setup,
157         },
158         [PORT_QUARK_X1000] = {
159                 .type = QUARK_X1000_SSP,
160                 .port_id = -1,
161                 .num_chipselect = 1,
162                 .max_clk_rate = 50000000,
163         },
164         [PORT_LPT0] = {
165                 .type = LPSS_LPT_SSP,
166                 .port_id = 0,
167                 .setup = lpss_spi_setup,
168                 .tx_param = &lpt0_tx_param,
169                 .rx_param = &lpt0_rx_param,
170         },
171         [PORT_LPT1] = {
172                 .type = LPSS_LPT_SSP,
173                 .port_id = 1,
174                 .setup = lpss_spi_setup,
175                 .tx_param = &lpt1_tx_param,
176                 .rx_param = &lpt1_rx_param,
177         },
178 };
179
180 static int pxa2xx_spi_pci_probe(struct pci_dev *dev,
181                 const struct pci_device_id *ent)
182 {
183         struct platform_device_info pi;
184         int ret;
185         struct platform_device *pdev;
186         struct pxa2xx_spi_master spi_pdata;
187         struct ssp_device *ssp;
188         struct pxa_spi_info *c;
189         char buf[40];
190
191         ret = pcim_enable_device(dev);
192         if (ret)
193                 return ret;
194
195         ret = pcim_iomap_regions(dev, 1 << 0, "PXA2xx SPI");
196         if (ret)
197                 return ret;
198
199         c = &spi_info_configs[ent->driver_data];
200         if (c->setup) {
201                 ret = c->setup(dev, c);
202                 if (ret)
203                         return ret;
204         }
205
206         memset(&spi_pdata, 0, sizeof(spi_pdata));
207         spi_pdata.num_chipselect = (c->num_chipselect > 0) ? c->num_chipselect : dev->devfn;
208         spi_pdata.dma_filter = c->dma_filter;
209         spi_pdata.tx_param = c->tx_param;
210         spi_pdata.rx_param = c->rx_param;
211         spi_pdata.enable_dma = c->rx_param && c->tx_param;
212
213         ssp = &spi_pdata.ssp;
214         ssp->phys_base = pci_resource_start(dev, 0);
215         ssp->mmio_base = pcim_iomap_table(dev)[0];
216         ssp->irq = dev->irq;
217         ssp->port_id = (c->port_id >= 0) ? c->port_id : dev->devfn;
218         ssp->type = c->type;
219
220         snprintf(buf, sizeof(buf), "pxa2xx-spi.%d", ssp->port_id);
221         ssp->clk = clk_register_fixed_rate(&dev->dev, buf , NULL, 0,
222                                            c->max_clk_rate);
223          if (IS_ERR(ssp->clk))
224                 return PTR_ERR(ssp->clk);
225
226         memset(&pi, 0, sizeof(pi));
227         pi.fwnode = dev->dev.fwnode;
228         pi.parent = &dev->dev;
229         pi.name = "pxa2xx-spi";
230         pi.id = ssp->port_id;
231         pi.data = &spi_pdata;
232         pi.size_data = sizeof(spi_pdata);
233
234         pdev = platform_device_register_full(&pi);
235         if (IS_ERR(pdev)) {
236                 clk_unregister(ssp->clk);
237                 return PTR_ERR(pdev);
238         }
239
240         pci_set_drvdata(dev, pdev);
241
242         return 0;
243 }
244
245 static void pxa2xx_spi_pci_remove(struct pci_dev *dev)
246 {
247         struct platform_device *pdev = pci_get_drvdata(dev);
248         struct pxa2xx_spi_master *spi_pdata;
249
250         spi_pdata = dev_get_platdata(&pdev->dev);
251
252         platform_device_unregister(pdev);
253         clk_unregister(spi_pdata->ssp.clk);
254 }
255
256 static const struct pci_device_id pxa2xx_spi_pci_devices[] = {
257         { PCI_VDEVICE(INTEL, 0x0935), PORT_QUARK_X1000 },
258         { PCI_VDEVICE(INTEL, 0x0f0e), PORT_BYT },
259         { PCI_VDEVICE(INTEL, 0x1194), PORT_MRFLD },
260         { PCI_VDEVICE(INTEL, 0x228e), PORT_BSW0 },
261         { PCI_VDEVICE(INTEL, 0x2290), PORT_BSW1 },
262         { PCI_VDEVICE(INTEL, 0x22ac), PORT_BSW2 },
263         { PCI_VDEVICE(INTEL, 0x2e6a), PORT_CE4100 },
264         { PCI_VDEVICE(INTEL, 0x9ce5), PORT_LPT0 },
265         { PCI_VDEVICE(INTEL, 0x9ce6), PORT_LPT1 },
266         { }
267 };
268 MODULE_DEVICE_TABLE(pci, pxa2xx_spi_pci_devices);
269
270 static struct pci_driver pxa2xx_spi_pci_driver = {
271         .name           = "pxa2xx_spi_pci",
272         .id_table       = pxa2xx_spi_pci_devices,
273         .probe          = pxa2xx_spi_pci_probe,
274         .remove         = pxa2xx_spi_pci_remove,
275 };
276
277 module_pci_driver(pxa2xx_spi_pci_driver);
278
279 MODULE_DESCRIPTION("CE4100/LPSS PCI-SPI glue code for PXA's driver");
280 MODULE_LICENSE("GPL v2");
281 MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");