spi-pxa2xx-pci.c 8.9 KB

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