spi-pxa2xx-pci.c 7.2 KB

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