1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * PCI glue driver for SPI PXA2xx compatible controllers. 4 * CE4100's SPI device is more or less the same one as found on PXA. 5 * 6 * Copyright (C) 2016, 2021 Intel Corporation 7 */ 8 #include <linux/clk-provider.h> 9 #include <linux/device.h> 10 #include <linux/err.h> 11 #include <linux/module.h> 12 #include <linux/pci.h> 13 #include <linux/pm.h> 14 #include <linux/sprintf.h> 15 #include <linux/string.h> 16 #include <linux/types.h> 17 18 #include <linux/dmaengine.h> 19 #include <linux/platform_data/dma-dw.h> 20 21 #include "spi-pxa2xx.h" 22 23 #define PCI_DEVICE_ID_INTEL_QUARK_X1000 0x0935 24 #define PCI_DEVICE_ID_INTEL_BYT 0x0f0e 25 #define PCI_DEVICE_ID_INTEL_MRFLD 0x1194 26 #define PCI_DEVICE_ID_INTEL_BSW0 0x228e 27 #define PCI_DEVICE_ID_INTEL_BSW1 0x2290 28 #define PCI_DEVICE_ID_INTEL_BSW2 0x22ac 29 #define PCI_DEVICE_ID_INTEL_CE4100 0x2e6a 30 #define PCI_DEVICE_ID_INTEL_LPT0_0 0x9c65 31 #define PCI_DEVICE_ID_INTEL_LPT0_1 0x9c66 32 #define PCI_DEVICE_ID_INTEL_LPT1_0 0x9ce5 33 #define PCI_DEVICE_ID_INTEL_LPT1_1 0x9ce6 34 35 struct pxa_spi_info { 36 int (*setup)(struct pci_dev *pdev, struct pxa2xx_spi_controller *c); 37 }; 38 39 static struct dw_dma_slave byt_tx_param = { .dst_id = 0 }; 40 static struct dw_dma_slave byt_rx_param = { .src_id = 1 }; 41 42 static struct dw_dma_slave mrfld3_tx_param = { .dst_id = 15 }; 43 static struct dw_dma_slave mrfld3_rx_param = { .src_id = 14 }; 44 static struct dw_dma_slave mrfld5_tx_param = { .dst_id = 13 }; 45 static struct dw_dma_slave mrfld5_rx_param = { .src_id = 12 }; 46 static struct dw_dma_slave mrfld6_tx_param = { .dst_id = 11 }; 47 static struct dw_dma_slave mrfld6_rx_param = { .src_id = 10 }; 48 49 static struct dw_dma_slave bsw0_tx_param = { .dst_id = 0 }; 50 static struct dw_dma_slave bsw0_rx_param = { .src_id = 1 }; 51 static struct dw_dma_slave bsw1_tx_param = { .dst_id = 6 }; 52 static struct dw_dma_slave bsw1_rx_param = { .src_id = 7 }; 53 static struct dw_dma_slave bsw2_tx_param = { .dst_id = 8 }; 54 static struct dw_dma_slave bsw2_rx_param = { .src_id = 9 }; 55 56 static struct dw_dma_slave lpt1_tx_param = { .dst_id = 0 }; 57 static struct dw_dma_slave lpt1_rx_param = { .src_id = 1 }; 58 static struct dw_dma_slave lpt0_tx_param = { .dst_id = 2 }; 59 static struct dw_dma_slave lpt0_rx_param = { .src_id = 3 }; 60 61 static void pxa2xx_spi_pci_clk_unregister(void *clk) 62 { 63 clk_unregister(clk); 64 } 65 66 static int pxa2xx_spi_pci_clk_register(struct pci_dev *dev, struct ssp_device *ssp, 67 unsigned long rate) 68 { 69 char buf[40]; 70 71 snprintf(buf, sizeof(buf), "pxa2xx-spi.%d", ssp->port_id); 72 ssp->clk = clk_register_fixed_rate(&dev->dev, buf, NULL, 0, rate); 73 if (IS_ERR(ssp->clk)) 74 return PTR_ERR(ssp->clk); 75 76 return devm_add_action_or_reset(&dev->dev, pxa2xx_spi_pci_clk_unregister, ssp->clk); 77 } 78 79 static bool lpss_dma_filter(struct dma_chan *chan, void *param) 80 { 81 struct dw_dma_slave *dws = param; 82 83 if (dws->dma_dev != chan->device->dev) 84 return false; 85 86 chan->private = dws; 87 return true; 88 } 89 90 static void lpss_dma_put_device(void *dma_dev) 91 { 92 pci_dev_put(dma_dev); 93 } 94 95 static int lpss_spi_setup(struct pci_dev *dev, struct pxa2xx_spi_controller *c) 96 { 97 struct ssp_device *ssp = &c->ssp; 98 struct dw_dma_slave *tx, *rx; 99 struct pci_dev *dma_dev; 100 int ret; 101 102 switch (dev->device) { 103 case PCI_DEVICE_ID_INTEL_BYT: 104 ssp->type = LPSS_BYT_SSP; 105 ssp->port_id = 0; 106 c->tx_param = &byt_tx_param; 107 c->rx_param = &byt_rx_param; 108 break; 109 case PCI_DEVICE_ID_INTEL_BSW0: 110 ssp->type = LPSS_BSW_SSP; 111 ssp->port_id = 0; 112 c->tx_param = &bsw0_tx_param; 113 c->rx_param = &bsw0_rx_param; 114 break; 115 case PCI_DEVICE_ID_INTEL_BSW1: 116 ssp->type = LPSS_BSW_SSP; 117 ssp->port_id = 1; 118 c->tx_param = &bsw1_tx_param; 119 c->rx_param = &bsw1_rx_param; 120 break; 121 case PCI_DEVICE_ID_INTEL_BSW2: 122 ssp->type = LPSS_BSW_SSP; 123 ssp->port_id = 2; 124 c->tx_param = &bsw2_tx_param; 125 c->rx_param = &bsw2_rx_param; 126 break; 127 case PCI_DEVICE_ID_INTEL_LPT0_0: 128 case PCI_DEVICE_ID_INTEL_LPT1_0: 129 ssp->type = LPSS_LPT_SSP; 130 ssp->port_id = 0; 131 c->tx_param = &lpt0_tx_param; 132 c->rx_param = &lpt0_rx_param; 133 break; 134 case PCI_DEVICE_ID_INTEL_LPT0_1: 135 case PCI_DEVICE_ID_INTEL_LPT1_1: 136 ssp->type = LPSS_LPT_SSP; 137 ssp->port_id = 1; 138 c->tx_param = &lpt1_tx_param; 139 c->rx_param = &lpt1_rx_param; 140 break; 141 default: 142 return -ENODEV; 143 } 144 145 c->num_chipselect = 1; 146 147 ret = pxa2xx_spi_pci_clk_register(dev, ssp, 50000000); 148 if (ret) 149 return ret; 150 151 dma_dev = pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0)); 152 ret = devm_add_action_or_reset(&dev->dev, lpss_dma_put_device, dma_dev); 153 if (ret) 154 return ret; 155 156 tx = c->tx_param; 157 tx->dma_dev = &dma_dev->dev; 158 tx->m_master = 0; 159 tx->p_master = 1; 160 161 rx = c->rx_param; 162 rx->dma_dev = &dma_dev->dev; 163 rx->m_master = 0; 164 rx->p_master = 1; 165 166 c->dma_filter = lpss_dma_filter; 167 c->dma_burst_size = 1; 168 c->enable_dma = 1; 169 return 0; 170 } 171 172 static const struct pxa_spi_info lpss_info_config = { 173 .setup = lpss_spi_setup, 174 }; 175 176 static int ce4100_spi_setup(struct pci_dev *dev, struct pxa2xx_spi_controller *c) 177 { 178 struct ssp_device *ssp = &c->ssp; 179 180 ssp->type = PXA25x_SSP; 181 ssp->port_id = dev->devfn; 182 c->num_chipselect = dev->devfn; 183 184 return pxa2xx_spi_pci_clk_register(dev, ssp, 3686400); 185 } 186 187 static const struct pxa_spi_info ce4100_info_config = { 188 .setup = ce4100_spi_setup, 189 }; 190 191 static int mrfld_spi_setup(struct pci_dev *dev, struct pxa2xx_spi_controller *c) 192 { 193 struct ssp_device *ssp = &c->ssp; 194 struct dw_dma_slave *tx, *rx; 195 struct pci_dev *dma_dev; 196 int ret; 197 198 ssp->type = MRFLD_SSP; 199 200 switch (PCI_FUNC(dev->devfn)) { 201 case 0: 202 ssp->port_id = 3; 203 c->num_chipselect = 1; 204 c->tx_param = &mrfld3_tx_param; 205 c->rx_param = &mrfld3_rx_param; 206 break; 207 case 1: 208 ssp->port_id = 5; 209 c->num_chipselect = 4; 210 c->tx_param = &mrfld5_tx_param; 211 c->rx_param = &mrfld5_rx_param; 212 break; 213 case 2: 214 ssp->port_id = 6; 215 c->num_chipselect = 1; 216 c->tx_param = &mrfld6_tx_param; 217 c->rx_param = &mrfld6_rx_param; 218 break; 219 default: 220 return -ENODEV; 221 } 222 223 ret = pxa2xx_spi_pci_clk_register(dev, ssp, 25000000); 224 if (ret) 225 return ret; 226 227 dma_dev = pci_get_slot(dev->bus, PCI_DEVFN(21, 0)); 228 ret = devm_add_action_or_reset(&dev->dev, lpss_dma_put_device, dma_dev); 229 if (ret) 230 return ret; 231 232 tx = c->tx_param; 233 tx->dma_dev = &dma_dev->dev; 234 235 rx = c->rx_param; 236 rx->dma_dev = &dma_dev->dev; 237 238 c->dma_filter = lpss_dma_filter; 239 c->dma_burst_size = 8; 240 c->enable_dma = 1; 241 return 0; 242 } 243 244 static const struct pxa_spi_info mrfld_info_config = { 245 .setup = mrfld_spi_setup, 246 }; 247 248 static int qrk_spi_setup(struct pci_dev *dev, struct pxa2xx_spi_controller *c) 249 { 250 struct ssp_device *ssp = &c->ssp; 251 252 ssp->type = QUARK_X1000_SSP; 253 ssp->port_id = dev->devfn; 254 c->num_chipselect = 1; 255 256 return pxa2xx_spi_pci_clk_register(dev, ssp, 50000000); 257 } 258 259 static const struct pxa_spi_info qrk_info_config = { 260 .setup = qrk_spi_setup, 261 }; 262 263 static int pxa2xx_spi_pci_probe(struct pci_dev *dev, 264 const struct pci_device_id *ent) 265 { 266 const struct pxa_spi_info *info; 267 int ret; 268 struct pxa2xx_spi_controller *pdata; 269 struct ssp_device *ssp; 270 271 ret = pcim_enable_device(dev); 272 if (ret) 273 return ret; 274 275 ret = pcim_iomap_regions(dev, 1 << 0, "PXA2xx SPI"); 276 if (ret) 277 return ret; 278 279 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL); 280 if (!pdata) 281 return -ENOMEM; 282 283 ssp = &pdata->ssp; 284 ssp->dev = &dev->dev; 285 ssp->phys_base = pci_resource_start(dev, 0); 286 ssp->mmio_base = pcim_iomap_table(dev)[0]; 287 288 info = (struct pxa_spi_info *)ent->driver_data; 289 ret = info->setup(dev, pdata); 290 if (ret) 291 return ret; 292 293 pci_set_master(dev); 294 295 ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_ALL_TYPES); 296 if (ret < 0) 297 return ret; 298 ssp->irq = pci_irq_vector(dev, 0); 299 300 return pxa2xx_spi_probe(&dev->dev, ssp); 301 } 302 303 static void pxa2xx_spi_pci_remove(struct pci_dev *dev) 304 { 305 pxa2xx_spi_remove(&dev->dev); 306 } 307 308 static const struct pci_device_id pxa2xx_spi_pci_devices[] = { 309 { PCI_DEVICE_DATA(INTEL, QUARK_X1000, &qrk_info_config) }, 310 { PCI_DEVICE_DATA(INTEL, BYT, &lpss_info_config) }, 311 { PCI_DEVICE_DATA(INTEL, MRFLD, &mrfld_info_config) }, 312 { PCI_DEVICE_DATA(INTEL, BSW0, &lpss_info_config) }, 313 { PCI_DEVICE_DATA(INTEL, BSW1, &lpss_info_config) }, 314 { PCI_DEVICE_DATA(INTEL, BSW2, &lpss_info_config) }, 315 { PCI_DEVICE_DATA(INTEL, CE4100, &ce4100_info_config) }, 316 { PCI_DEVICE_DATA(INTEL, LPT0_0, &lpss_info_config) }, 317 { PCI_DEVICE_DATA(INTEL, LPT0_1, &lpss_info_config) }, 318 { PCI_DEVICE_DATA(INTEL, LPT1_0, &lpss_info_config) }, 319 { PCI_DEVICE_DATA(INTEL, LPT1_1, &lpss_info_config) }, 320 { } 321 }; 322 MODULE_DEVICE_TABLE(pci, pxa2xx_spi_pci_devices); 323 324 static struct pci_driver pxa2xx_spi_pci_driver = { 325 .name = "pxa2xx_spi_pci", 326 .id_table = pxa2xx_spi_pci_devices, 327 .driver = { 328 .pm = pm_ptr(&pxa2xx_spi_pm_ops), 329 }, 330 .probe = pxa2xx_spi_pci_probe, 331 .remove = pxa2xx_spi_pci_remove, 332 }; 333 334 module_pci_driver(pxa2xx_spi_pci_driver); 335 336 MODULE_DESCRIPTION("CE4100/LPSS PCI-SPI glue code for PXA's driver"); 337 MODULE_LICENSE("GPL v2"); 338 MODULE_IMPORT_NS(SPI_PXA2xx); 339 MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>"); 340