Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * PCI glue driver for SPI PXA2xx compatible controllers.
0004  * CE4100's SPI device is more or less the same one as found on PXA.
0005  *
0006  * Copyright (C) 2016, 2021 Intel Corporation
0007  */
0008 #include <linux/clk-provider.h>
0009 #include <linux/module.h>
0010 #include <linux/pci.h>
0011 #include <linux/platform_device.h>
0012 
0013 #include <linux/spi/pxa2xx_spi.h>
0014 
0015 #include <linux/dmaengine.h>
0016 #include <linux/platform_data/dma-dw.h>
0017 
0018 #define PCI_DEVICE_ID_INTEL_QUARK_X1000     0x0935
0019 #define PCI_DEVICE_ID_INTEL_BYT         0x0f0e
0020 #define PCI_DEVICE_ID_INTEL_MRFLD       0x1194
0021 #define PCI_DEVICE_ID_INTEL_BSW0        0x228e
0022 #define PCI_DEVICE_ID_INTEL_BSW1        0x2290
0023 #define PCI_DEVICE_ID_INTEL_BSW2        0x22ac
0024 #define PCI_DEVICE_ID_INTEL_CE4100      0x2e6a
0025 #define PCI_DEVICE_ID_INTEL_LPT0_0      0x9c65
0026 #define PCI_DEVICE_ID_INTEL_LPT0_1      0x9c66
0027 #define PCI_DEVICE_ID_INTEL_LPT1_0      0x9ce5
0028 #define PCI_DEVICE_ID_INTEL_LPT1_1      0x9ce6
0029 
0030 struct pxa_spi_info {
0031     int (*setup)(struct pci_dev *pdev, struct pxa2xx_spi_controller *c);
0032 };
0033 
0034 static struct dw_dma_slave byt_tx_param = { .dst_id = 0 };
0035 static struct dw_dma_slave byt_rx_param = { .src_id = 1 };
0036 
0037 static struct dw_dma_slave mrfld3_tx_param = { .dst_id = 15 };
0038 static struct dw_dma_slave mrfld3_rx_param = { .src_id = 14 };
0039 static struct dw_dma_slave mrfld5_tx_param = { .dst_id = 13 };
0040 static struct dw_dma_slave mrfld5_rx_param = { .src_id = 12 };
0041 static struct dw_dma_slave mrfld6_tx_param = { .dst_id = 11 };
0042 static struct dw_dma_slave mrfld6_rx_param = { .src_id = 10 };
0043 
0044 static struct dw_dma_slave bsw0_tx_param = { .dst_id = 0 };
0045 static struct dw_dma_slave bsw0_rx_param = { .src_id = 1 };
0046 static struct dw_dma_slave bsw1_tx_param = { .dst_id = 6 };
0047 static struct dw_dma_slave bsw1_rx_param = { .src_id = 7 };
0048 static struct dw_dma_slave bsw2_tx_param = { .dst_id = 8 };
0049 static struct dw_dma_slave bsw2_rx_param = { .src_id = 9 };
0050 
0051 static struct dw_dma_slave lpt1_tx_param = { .dst_id = 0 };
0052 static struct dw_dma_slave lpt1_rx_param = { .src_id = 1 };
0053 static struct dw_dma_slave lpt0_tx_param = { .dst_id = 2 };
0054 static struct dw_dma_slave lpt0_rx_param = { .src_id = 3 };
0055 
0056 static void pxa2xx_spi_pci_clk_unregister(void *clk)
0057 {
0058     clk_unregister(clk);
0059 }
0060 
0061 static int pxa2xx_spi_pci_clk_register(struct pci_dev *dev, struct ssp_device *ssp,
0062                        unsigned long rate)
0063 {
0064     char buf[40];
0065 
0066     snprintf(buf, sizeof(buf), "pxa2xx-spi.%d", ssp->port_id);
0067     ssp->clk = clk_register_fixed_rate(&dev->dev, buf, NULL, 0, rate);
0068     if (IS_ERR(ssp->clk))
0069         return PTR_ERR(ssp->clk);
0070 
0071     return devm_add_action_or_reset(&dev->dev, pxa2xx_spi_pci_clk_unregister, ssp->clk);
0072 }
0073 
0074 static bool lpss_dma_filter(struct dma_chan *chan, void *param)
0075 {
0076     struct dw_dma_slave *dws = param;
0077 
0078     if (dws->dma_dev != chan->device->dev)
0079         return false;
0080 
0081     chan->private = dws;
0082     return true;
0083 }
0084 
0085 static void lpss_dma_put_device(void *dma_dev)
0086 {
0087     pci_dev_put(dma_dev);
0088 }
0089 
0090 static int lpss_spi_setup(struct pci_dev *dev, struct pxa2xx_spi_controller *c)
0091 {
0092     struct ssp_device *ssp = &c->ssp;
0093     struct dw_dma_slave *tx, *rx;
0094     struct pci_dev *dma_dev;
0095     int ret;
0096 
0097     switch (dev->device) {
0098     case PCI_DEVICE_ID_INTEL_BYT:
0099         ssp->type = LPSS_BYT_SSP;
0100         ssp->port_id = 0;
0101         c->tx_param = &byt_tx_param;
0102         c->rx_param = &byt_rx_param;
0103         break;
0104     case PCI_DEVICE_ID_INTEL_BSW0:
0105         ssp->type = LPSS_BSW_SSP;
0106         ssp->port_id = 0;
0107         c->tx_param = &bsw0_tx_param;
0108         c->rx_param = &bsw0_rx_param;
0109         break;
0110     case PCI_DEVICE_ID_INTEL_BSW1:
0111         ssp->type = LPSS_BSW_SSP;
0112         ssp->port_id = 1;
0113         c->tx_param = &bsw1_tx_param;
0114         c->rx_param = &bsw1_rx_param;
0115         break;
0116     case PCI_DEVICE_ID_INTEL_BSW2:
0117         ssp->type = LPSS_BSW_SSP;
0118         ssp->port_id = 2;
0119         c->tx_param = &bsw2_tx_param;
0120         c->rx_param = &bsw2_rx_param;
0121         break;
0122     case PCI_DEVICE_ID_INTEL_LPT0_0:
0123     case PCI_DEVICE_ID_INTEL_LPT1_0:
0124         ssp->type = LPSS_LPT_SSP;
0125         ssp->port_id = 0;
0126         c->tx_param = &lpt0_tx_param;
0127         c->rx_param = &lpt0_rx_param;
0128         break;
0129     case PCI_DEVICE_ID_INTEL_LPT0_1:
0130     case PCI_DEVICE_ID_INTEL_LPT1_1:
0131         ssp->type = LPSS_LPT_SSP;
0132         ssp->port_id = 1;
0133         c->tx_param = &lpt1_tx_param;
0134         c->rx_param = &lpt1_rx_param;
0135         break;
0136     default:
0137         return -ENODEV;
0138     }
0139 
0140     c->num_chipselect = 1;
0141 
0142     ret = pxa2xx_spi_pci_clk_register(dev, ssp, 50000000);
0143     if (ret)
0144         return ret;
0145 
0146     dma_dev = pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
0147     ret = devm_add_action_or_reset(&dev->dev, lpss_dma_put_device, dma_dev);
0148     if (ret)
0149         return ret;
0150 
0151     tx = c->tx_param;
0152     tx->dma_dev = &dma_dev->dev;
0153     tx->m_master = 0;
0154     tx->p_master = 1;
0155 
0156     rx = c->rx_param;
0157     rx->dma_dev = &dma_dev->dev;
0158     rx->m_master = 0;
0159     rx->p_master = 1;
0160 
0161     c->dma_filter = lpss_dma_filter;
0162     c->dma_burst_size = 1;
0163     c->enable_dma = 1;
0164     return 0;
0165 }
0166 
0167 static const struct pxa_spi_info lpss_info_config = {
0168     .setup = lpss_spi_setup,
0169 };
0170 
0171 static int ce4100_spi_setup(struct pci_dev *dev, struct pxa2xx_spi_controller *c)
0172 {
0173     struct ssp_device *ssp = &c->ssp;
0174 
0175     ssp->type = PXA25x_SSP;
0176     ssp->port_id = dev->devfn;
0177     c->num_chipselect = dev->devfn;
0178 
0179     return pxa2xx_spi_pci_clk_register(dev, ssp, 3686400);
0180 }
0181 
0182 static const struct pxa_spi_info ce4100_info_config = {
0183     .setup = ce4100_spi_setup,
0184 };
0185 
0186 static int mrfld_spi_setup(struct pci_dev *dev, struct pxa2xx_spi_controller *c)
0187 {
0188     struct ssp_device *ssp = &c->ssp;
0189     struct dw_dma_slave *tx, *rx;
0190     struct pci_dev *dma_dev;
0191     int ret;
0192 
0193     ssp->type = MRFLD_SSP;
0194 
0195     switch (PCI_FUNC(dev->devfn)) {
0196     case 0:
0197         ssp->port_id = 3;
0198         c->num_chipselect = 1;
0199         c->tx_param = &mrfld3_tx_param;
0200         c->rx_param = &mrfld3_rx_param;
0201         break;
0202     case 1:
0203         ssp->port_id = 5;
0204         c->num_chipselect = 4;
0205         c->tx_param = &mrfld5_tx_param;
0206         c->rx_param = &mrfld5_rx_param;
0207         break;
0208     case 2:
0209         ssp->port_id = 6;
0210         c->num_chipselect = 1;
0211         c->tx_param = &mrfld6_tx_param;
0212         c->rx_param = &mrfld6_rx_param;
0213         break;
0214     default:
0215         return -ENODEV;
0216     }
0217 
0218     ret = pxa2xx_spi_pci_clk_register(dev, ssp, 25000000);
0219     if (ret)
0220         return ret;
0221 
0222     dma_dev = pci_get_slot(dev->bus, PCI_DEVFN(21, 0));
0223     ret = devm_add_action_or_reset(&dev->dev, lpss_dma_put_device, dma_dev);
0224     if (ret)
0225         return ret;
0226 
0227     tx = c->tx_param;
0228     tx->dma_dev = &dma_dev->dev;
0229 
0230     rx = c->rx_param;
0231     rx->dma_dev = &dma_dev->dev;
0232 
0233     c->dma_filter = lpss_dma_filter;
0234     c->dma_burst_size = 8;
0235     c->enable_dma = 1;
0236     return 0;
0237 }
0238 
0239 static const struct pxa_spi_info mrfld_info_config = {
0240     .setup = mrfld_spi_setup,
0241 };
0242 
0243 static int qrk_spi_setup(struct pci_dev *dev, struct pxa2xx_spi_controller *c)
0244 {
0245     struct ssp_device *ssp = &c->ssp;
0246 
0247     ssp->type = QUARK_X1000_SSP;
0248     ssp->port_id = dev->devfn;
0249     c->num_chipselect = 1;
0250 
0251     return pxa2xx_spi_pci_clk_register(dev, ssp, 50000000);
0252 }
0253 
0254 static const struct pxa_spi_info qrk_info_config = {
0255     .setup = qrk_spi_setup,
0256 };
0257 
0258 static int pxa2xx_spi_pci_probe(struct pci_dev *dev,
0259         const struct pci_device_id *ent)
0260 {
0261     const struct pxa_spi_info *info;
0262     struct platform_device_info pi;
0263     int ret;
0264     struct platform_device *pdev;
0265     struct pxa2xx_spi_controller spi_pdata;
0266     struct ssp_device *ssp;
0267 
0268     ret = pcim_enable_device(dev);
0269     if (ret)
0270         return ret;
0271 
0272     ret = pcim_iomap_regions(dev, 1 << 0, "PXA2xx SPI");
0273     if (ret)
0274         return ret;
0275 
0276     memset(&spi_pdata, 0, sizeof(spi_pdata));
0277 
0278     ssp = &spi_pdata.ssp;
0279     ssp->dev = &dev->dev;
0280     ssp->phys_base = pci_resource_start(dev, 0);
0281     ssp->mmio_base = pcim_iomap_table(dev)[0];
0282 
0283     info = (struct pxa_spi_info *)ent->driver_data;
0284     ret = info->setup(dev, &spi_pdata);
0285     if (ret)
0286         return ret;
0287 
0288     pci_set_master(dev);
0289 
0290     ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_ALL_TYPES);
0291     if (ret < 0)
0292         return ret;
0293     ssp->irq = pci_irq_vector(dev, 0);
0294 
0295     memset(&pi, 0, sizeof(pi));
0296     pi.fwnode = dev_fwnode(&dev->dev);
0297     pi.parent = &dev->dev;
0298     pi.name = "pxa2xx-spi";
0299     pi.id = ssp->port_id;
0300     pi.data = &spi_pdata;
0301     pi.size_data = sizeof(spi_pdata);
0302 
0303     pdev = platform_device_register_full(&pi);
0304     if (IS_ERR(pdev))
0305         return PTR_ERR(pdev);
0306 
0307     pci_set_drvdata(dev, pdev);
0308 
0309     return 0;
0310 }
0311 
0312 static void pxa2xx_spi_pci_remove(struct pci_dev *dev)
0313 {
0314     struct platform_device *pdev = pci_get_drvdata(dev);
0315 
0316     platform_device_unregister(pdev);
0317 }
0318 
0319 static const struct pci_device_id pxa2xx_spi_pci_devices[] = {
0320     { PCI_DEVICE_DATA(INTEL, QUARK_X1000, &qrk_info_config) },
0321     { PCI_DEVICE_DATA(INTEL, BYT, &lpss_info_config) },
0322     { PCI_DEVICE_DATA(INTEL, MRFLD, &mrfld_info_config) },
0323     { PCI_DEVICE_DATA(INTEL, BSW0, &lpss_info_config) },
0324     { PCI_DEVICE_DATA(INTEL, BSW1, &lpss_info_config) },
0325     { PCI_DEVICE_DATA(INTEL, BSW2, &lpss_info_config) },
0326     { PCI_DEVICE_DATA(INTEL, CE4100, &ce4100_info_config) },
0327     { PCI_DEVICE_DATA(INTEL, LPT0_0, &lpss_info_config) },
0328     { PCI_DEVICE_DATA(INTEL, LPT0_1, &lpss_info_config) },
0329     { PCI_DEVICE_DATA(INTEL, LPT1_0, &lpss_info_config) },
0330     { PCI_DEVICE_DATA(INTEL, LPT1_1, &lpss_info_config) },
0331     { }
0332 };
0333 MODULE_DEVICE_TABLE(pci, pxa2xx_spi_pci_devices);
0334 
0335 static struct pci_driver pxa2xx_spi_pci_driver = {
0336     .name           = "pxa2xx_spi_pci",
0337     .id_table       = pxa2xx_spi_pci_devices,
0338     .probe          = pxa2xx_spi_pci_probe,
0339     .remove         = pxa2xx_spi_pci_remove,
0340 };
0341 
0342 module_pci_driver(pxa2xx_spi_pci_driver);
0343 
0344 MODULE_DESCRIPTION("CE4100/LPSS PCI-SPI glue code for PXA's driver");
0345 MODULE_LICENSE("GPL v2");
0346 MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");